12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /* user_defined.c: user defined key type
31da177e4SLinus Torvalds *
41da177e4SLinus Torvalds * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
51da177e4SLinus Torvalds * Written by David Howells (dhowells@redhat.com)
61da177e4SLinus Torvalds */
71da177e4SLinus Torvalds
8876979c9SPaul Gortmaker #include <linux/export.h>
91da177e4SLinus Torvalds #include <linux/init.h>
101da177e4SLinus Torvalds #include <linux/slab.h>
111da177e4SLinus Torvalds #include <linux/seq_file.h>
121da177e4SLinus Torvalds #include <linux/err.h>
132aa349f6SDavid Howells #include <keys/user-type.h>
147c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
151da177e4SLinus Torvalds #include "internal.h"
161da177e4SLinus Torvalds
179f6ed2caSJeff Layton static int logon_vet_description(const char *desc);
189f6ed2caSJeff Layton
191da177e4SLinus Torvalds /*
201da177e4SLinus Torvalds * user defined keys take an arbitrary string as the description and an
211da177e4SLinus Torvalds * arbitrary blob of data as the payload
221da177e4SLinus Torvalds */
231da177e4SLinus Torvalds struct key_type key_type_user = {
241da177e4SLinus Torvalds .name = "user",
25f9167789SDavid Howells .preparse = user_preparse,
26f9167789SDavid Howells .free_preparse = user_free_preparse,
27f9167789SDavid Howells .instantiate = generic_key_instantiate,
281da177e4SLinus Torvalds .update = user_update,
2931204ed9SDavid Howells .revoke = user_revoke,
301da177e4SLinus Torvalds .destroy = user_destroy,
311da177e4SLinus Torvalds .describe = user_describe,
321da177e4SLinus Torvalds .read = user_read,
331da177e4SLinus Torvalds };
341da177e4SLinus Torvalds
3516c29b67SMichael Halcrow EXPORT_SYMBOL_GPL(key_type_user);
3616c29b67SMichael Halcrow
371da177e4SLinus Torvalds /*
389f6ed2caSJeff Layton * This key type is essentially the same as key_type_user, but it does
399f6ed2caSJeff Layton * not define a .read op. This is suitable for storing username and
409f6ed2caSJeff Layton * password pairs in the keyring that you do not want to be readable
419f6ed2caSJeff Layton * from userspace.
429f6ed2caSJeff Layton */
439f6ed2caSJeff Layton struct key_type key_type_logon = {
449f6ed2caSJeff Layton .name = "logon",
45f9167789SDavid Howells .preparse = user_preparse,
46f9167789SDavid Howells .free_preparse = user_free_preparse,
47f9167789SDavid Howells .instantiate = generic_key_instantiate,
489f6ed2caSJeff Layton .update = user_update,
499f6ed2caSJeff Layton .revoke = user_revoke,
509f6ed2caSJeff Layton .destroy = user_destroy,
519f6ed2caSJeff Layton .describe = user_describe,
529f6ed2caSJeff Layton .vet_description = logon_vet_description,
539f6ed2caSJeff Layton };
549f6ed2caSJeff Layton EXPORT_SYMBOL_GPL(key_type_logon);
559f6ed2caSJeff Layton
569f6ed2caSJeff Layton /*
57f9167789SDavid Howells * Preparse a user defined key payload
581da177e4SLinus Torvalds */
user_preparse(struct key_preparsed_payload * prep)59f9167789SDavid Howells int user_preparse(struct key_preparsed_payload *prep)
601da177e4SLinus Torvalds {
6176d8aeabSDavid Howells struct user_key_payload *upayload;
62cf7f601cSDavid Howells size_t datalen = prep->datalen;
631da177e4SLinus Torvalds
64cf7f601cSDavid Howells if (datalen <= 0 || datalen > 32767 || !prep->data)
65f9167789SDavid Howells return -EINVAL;
661da177e4SLinus Torvalds
6776d8aeabSDavid Howells upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
6876d8aeabSDavid Howells if (!upayload)
69f9167789SDavid Howells return -ENOMEM;
701da177e4SLinus Torvalds
7176d8aeabSDavid Howells /* attach the data */
72f9167789SDavid Howells prep->quotalen = datalen;
73146aa8b1SDavid Howells prep->payload.data[0] = upayload;
7476d8aeabSDavid Howells upayload->datalen = datalen;
75cf7f601cSDavid Howells memcpy(upayload->data, prep->data, datalen);
76f9167789SDavid Howells return 0;
77a8b17ed0SDavid Howells }
78f9167789SDavid Howells EXPORT_SYMBOL_GPL(user_preparse);
7931204ed9SDavid Howells
80f9167789SDavid Howells /*
81f9167789SDavid Howells * Free a preparse of a user defined key payload
82f9167789SDavid Howells */
user_free_preparse(struct key_preparsed_payload * prep)83f9167789SDavid Howells void user_free_preparse(struct key_preparsed_payload *prep)
84f9167789SDavid Howells {
85*453431a5SWaiman Long kfree_sensitive(prep->payload.data[0]);
86f9167789SDavid Howells }
87f9167789SDavid Howells EXPORT_SYMBOL_GPL(user_free_preparse);
882aa349f6SDavid Howells
user_free_payload_rcu(struct rcu_head * head)896966c749SEric Biggers static void user_free_payload_rcu(struct rcu_head *head)
906966c749SEric Biggers {
916966c749SEric Biggers struct user_key_payload *payload;
926966c749SEric Biggers
936966c749SEric Biggers payload = container_of(head, struct user_key_payload, rcu);
94*453431a5SWaiman Long kfree_sensitive(payload);
956966c749SEric Biggers }
966966c749SEric Biggers
971da177e4SLinus Torvalds /*
981da177e4SLinus Torvalds * update a user defined key
9976d8aeabSDavid Howells * - the key's semaphore is write-locked
1001da177e4SLinus Torvalds */
user_update(struct key * key,struct key_preparsed_payload * prep)101cf7f601cSDavid Howells int user_update(struct key *key, struct key_preparsed_payload *prep)
1021da177e4SLinus Torvalds {
103898de7d0SDavid Howells struct user_key_payload *zap = NULL;
1041da177e4SLinus Torvalds int ret;
1051da177e4SLinus Torvalds
1061da177e4SLinus Torvalds /* check the quota and attach the new data */
107898de7d0SDavid Howells ret = key_payload_reserve(key, prep->datalen);
108898de7d0SDavid Howells if (ret < 0)
109898de7d0SDavid Howells return ret;
1101da177e4SLinus Torvalds
1111da177e4SLinus Torvalds /* attach the new data, displacing the old */
112898de7d0SDavid Howells key->expiry = prep->expiry;
113363b02daSDavid Howells if (key_is_positive(key))
1140837e49aSDavid Howells zap = dereference_key_locked(key);
115898de7d0SDavid Howells rcu_assign_keypointer(key, prep->payload.data[0]);
116898de7d0SDavid Howells prep->payload.data[0] = NULL;
1171da177e4SLinus Torvalds
1189f35a33bSDavid Howells if (zap)
1196966c749SEric Biggers call_rcu(&zap->rcu, user_free_payload_rcu);
1201da177e4SLinus Torvalds return ret;
121a8b17ed0SDavid Howells }
1222aa349f6SDavid Howells EXPORT_SYMBOL_GPL(user_update);
1232aa349f6SDavid Howells
1241da177e4SLinus Torvalds /*
12531204ed9SDavid Howells * dispose of the links from a revoked keyring
12631204ed9SDavid Howells * - called with the key sem write-locked
12731204ed9SDavid Howells */
user_revoke(struct key * key)12831204ed9SDavid Howells void user_revoke(struct key *key)
12931204ed9SDavid Howells {
1300837e49aSDavid Howells struct user_key_payload *upayload = user_key_payload_locked(key);
13131204ed9SDavid Howells
13231204ed9SDavid Howells /* clear the quota */
13331204ed9SDavid Howells key_payload_reserve(key, 0);
13431204ed9SDavid Howells
13531204ed9SDavid Howells if (upayload) {
136f6b24579SMimi Zohar rcu_assign_keypointer(key, NULL);
1376966c749SEric Biggers call_rcu(&upayload->rcu, user_free_payload_rcu);
13831204ed9SDavid Howells }
139a8b17ed0SDavid Howells }
14031204ed9SDavid Howells
14131204ed9SDavid Howells EXPORT_SYMBOL(user_revoke);
14231204ed9SDavid Howells
14331204ed9SDavid Howells /*
14431204ed9SDavid Howells * dispose of the data dangling from the corpse of a user key
1451da177e4SLinus Torvalds */
user_destroy(struct key * key)1462aa349f6SDavid Howells void user_destroy(struct key *key)
1471da177e4SLinus Torvalds {
148146aa8b1SDavid Howells struct user_key_payload *upayload = key->payload.data[0];
14976d8aeabSDavid Howells
150*453431a5SWaiman Long kfree_sensitive(upayload);
151a8b17ed0SDavid Howells }
1521da177e4SLinus Torvalds
1532aa349f6SDavid Howells EXPORT_SYMBOL_GPL(user_destroy);
1542aa349f6SDavid Howells
1551da177e4SLinus Torvalds /*
15676d8aeabSDavid Howells * describe the user key
1571da177e4SLinus Torvalds */
user_describe(const struct key * key,struct seq_file * m)1582aa349f6SDavid Howells void user_describe(const struct key *key, struct seq_file *m)
1591da177e4SLinus Torvalds {
1601da177e4SLinus Torvalds seq_puts(m, key->description);
161363b02daSDavid Howells if (key_is_positive(key))
1621da177e4SLinus Torvalds seq_printf(m, ": %u", key->datalen);
163a8b17ed0SDavid Howells }
1641da177e4SLinus Torvalds
1652aa349f6SDavid Howells EXPORT_SYMBOL_GPL(user_describe);
1662aa349f6SDavid Howells
1671da177e4SLinus Torvalds /*
1681da177e4SLinus Torvalds * read the key data
16976d8aeabSDavid Howells * - the key's semaphore is read-locked
1701da177e4SLinus Torvalds */
user_read(const struct key * key,char * buffer,size_t buflen)171d3ec10aaSWaiman Long long user_read(const struct key *key, char *buffer, size_t buflen)
1721da177e4SLinus Torvalds {
173146aa8b1SDavid Howells const struct user_key_payload *upayload;
17476d8aeabSDavid Howells long ret;
17576d8aeabSDavid Howells
1760837e49aSDavid Howells upayload = user_key_payload_locked(key);
17776d8aeabSDavid Howells ret = upayload->datalen;
1781da177e4SLinus Torvalds
1791da177e4SLinus Torvalds /* we can return the data as is */
1801da177e4SLinus Torvalds if (buffer && buflen > 0) {
18176d8aeabSDavid Howells if (buflen > upayload->datalen)
18276d8aeabSDavid Howells buflen = upayload->datalen;
1831da177e4SLinus Torvalds
184d3ec10aaSWaiman Long memcpy(buffer, upayload->data, buflen);
1851da177e4SLinus Torvalds }
1861da177e4SLinus Torvalds
1871da177e4SLinus Torvalds return ret;
188a8b17ed0SDavid Howells }
1892aa349f6SDavid Howells
1902aa349f6SDavid Howells EXPORT_SYMBOL_GPL(user_read);
1919f6ed2caSJeff Layton
1929f6ed2caSJeff Layton /* Vet the description for a "logon" key */
logon_vet_description(const char * desc)1939f6ed2caSJeff Layton static int logon_vet_description(const char *desc)
1949f6ed2caSJeff Layton {
1959f6ed2caSJeff Layton char *p;
1969f6ed2caSJeff Layton
1979f6ed2caSJeff Layton /* require a "qualified" description string */
1989f6ed2caSJeff Layton p = strchr(desc, ':');
1999f6ed2caSJeff Layton if (!p)
2009f6ed2caSJeff Layton return -EINVAL;
2019f6ed2caSJeff Layton
2029f6ed2caSJeff Layton /* also reject description with ':' as first char */
2039f6ed2caSJeff Layton if (p == desc)
2049f6ed2caSJeff Layton return -EINVAL;
2059f6ed2caSJeff Layton
2069f6ed2caSJeff Layton return 0;
2079f6ed2caSJeff Layton }
208