^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* user_defined.c: user defined key type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Written by David Howells (dhowells@redhat.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <keys/user-type.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) static int logon_vet_description(const char *desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * user defined keys take an arbitrary string as the description and an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * arbitrary blob of data as the payload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) struct key_type key_type_user = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) .name = "user",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) .preparse = user_preparse,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) .free_preparse = user_free_preparse,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) .instantiate = generic_key_instantiate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) .update = user_update,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) .revoke = user_revoke,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) .destroy = user_destroy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) .describe = user_describe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) .read = user_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) EXPORT_SYMBOL_GPL(key_type_user);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * This key type is essentially the same as key_type_user, but it does
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * not define a .read op. This is suitable for storing username and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * password pairs in the keyring that you do not want to be readable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * from userspace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct key_type key_type_logon = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) .name = "logon",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) .preparse = user_preparse,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) .free_preparse = user_free_preparse,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) .instantiate = generic_key_instantiate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) .update = user_update,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) .revoke = user_revoke,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) .destroy = user_destroy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) .describe = user_describe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) .vet_description = logon_vet_description,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) EXPORT_SYMBOL_GPL(key_type_logon);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * Preparse a user defined key payload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) int user_preparse(struct key_preparsed_payload *prep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct user_key_payload *upayload;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) size_t datalen = prep->datalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (datalen <= 0 || datalen > 32767 || !prep->data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (!upayload)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) /* attach the data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) prep->quotalen = datalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) prep->payload.data[0] = upayload;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) upayload->datalen = datalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) memcpy(upayload->data, prep->data, datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) EXPORT_SYMBOL_GPL(user_preparse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * Free a preparse of a user defined key payload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) void user_free_preparse(struct key_preparsed_payload *prep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) kfree_sensitive(prep->payload.data[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) EXPORT_SYMBOL_GPL(user_free_preparse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) static void user_free_payload_rcu(struct rcu_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) struct user_key_payload *payload;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) payload = container_of(head, struct user_key_payload, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) kfree_sensitive(payload);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * update a user defined key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * - the key's semaphore is write-locked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) int user_update(struct key *key, struct key_preparsed_payload *prep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) struct user_key_payload *zap = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) /* check the quota and attach the new data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) ret = key_payload_reserve(key, prep->datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) /* attach the new data, displacing the old */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) key->expiry = prep->expiry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (key_is_positive(key))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) zap = dereference_key_locked(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) rcu_assign_keypointer(key, prep->payload.data[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) prep->payload.data[0] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) if (zap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) call_rcu(&zap->rcu, user_free_payload_rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) EXPORT_SYMBOL_GPL(user_update);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) * dispose of the links from a revoked keyring
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * - called with the key sem write-locked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) void user_revoke(struct key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) struct user_key_payload *upayload = user_key_payload_locked(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) /* clear the quota */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) key_payload_reserve(key, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (upayload) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) rcu_assign_keypointer(key, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) call_rcu(&upayload->rcu, user_free_payload_rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) EXPORT_SYMBOL(user_revoke);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * dispose of the data dangling from the corpse of a user key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) void user_destroy(struct key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) struct user_key_payload *upayload = key->payload.data[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) kfree_sensitive(upayload);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) EXPORT_SYMBOL_GPL(user_destroy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * describe the user key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) void user_describe(const struct key *key, struct seq_file *m)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) seq_puts(m, key->description);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) if (key_is_positive(key))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) seq_printf(m, ": %u", key->datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) EXPORT_SYMBOL_GPL(user_describe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) * read the key data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) * - the key's semaphore is read-locked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) long user_read(const struct key *key, char *buffer, size_t buflen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) const struct user_key_payload *upayload;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) upayload = user_key_payload_locked(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) ret = upayload->datalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) /* we can return the data as is */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (buffer && buflen > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (buflen > upayload->datalen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) buflen = upayload->datalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) memcpy(buffer, upayload->data, buflen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) EXPORT_SYMBOL_GPL(user_read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) /* Vet the description for a "logon" key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) static int logon_vet_description(const char *desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) /* require a "qualified" description string */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) p = strchr(desc, ':');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) /* also reject description with ':' as first char */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) if (p == desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) }