xref: /OK3568_Linux_fs/kernel/security/keys/key.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /* Basic authentication token and access key management
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright (C) 2004-2008 Red Hat, Inc. All Rights Reserved.
5*4882a593Smuzhiyun  * Written by David Howells (dhowells@redhat.com)
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <linux/export.h>
9*4882a593Smuzhiyun #include <linux/init.h>
10*4882a593Smuzhiyun #include <linux/poison.h>
11*4882a593Smuzhiyun #include <linux/sched.h>
12*4882a593Smuzhiyun #include <linux/slab.h>
13*4882a593Smuzhiyun #include <linux/security.h>
14*4882a593Smuzhiyun #include <linux/workqueue.h>
15*4882a593Smuzhiyun #include <linux/random.h>
16*4882a593Smuzhiyun #include <linux/ima.h>
17*4882a593Smuzhiyun #include <linux/err.h>
18*4882a593Smuzhiyun #include "internal.h"
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun struct kmem_cache *key_jar;
21*4882a593Smuzhiyun struct rb_root		key_serial_tree; /* tree of keys indexed by serial */
22*4882a593Smuzhiyun DEFINE_SPINLOCK(key_serial_lock);
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun struct rb_root	key_user_tree; /* tree of quota records indexed by UID */
25*4882a593Smuzhiyun DEFINE_SPINLOCK(key_user_lock);
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun unsigned int key_quota_root_maxkeys = 1000000;	/* root's key count quota */
28*4882a593Smuzhiyun unsigned int key_quota_root_maxbytes = 25000000; /* root's key space quota */
29*4882a593Smuzhiyun unsigned int key_quota_maxkeys = 200;		/* general key count quota */
30*4882a593Smuzhiyun unsigned int key_quota_maxbytes = 20000;	/* general key space quota */
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun static LIST_HEAD(key_types_list);
33*4882a593Smuzhiyun static DECLARE_RWSEM(key_types_sem);
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun /* We serialise key instantiation and link */
36*4882a593Smuzhiyun DEFINE_MUTEX(key_construction_mutex);
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun #ifdef KEY_DEBUGGING
__key_check(const struct key * key)39*4882a593Smuzhiyun void __key_check(const struct key *key)
40*4882a593Smuzhiyun {
41*4882a593Smuzhiyun 	printk("__key_check: key %p {%08x} should be {%08x}\n",
42*4882a593Smuzhiyun 	       key, key->magic, KEY_DEBUG_MAGIC);
43*4882a593Smuzhiyun 	BUG();
44*4882a593Smuzhiyun }
45*4882a593Smuzhiyun #endif
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun /*
48*4882a593Smuzhiyun  * Get the key quota record for a user, allocating a new record if one doesn't
49*4882a593Smuzhiyun  * already exist.
50*4882a593Smuzhiyun  */
key_user_lookup(kuid_t uid)51*4882a593Smuzhiyun struct key_user *key_user_lookup(kuid_t uid)
52*4882a593Smuzhiyun {
53*4882a593Smuzhiyun 	struct key_user *candidate = NULL, *user;
54*4882a593Smuzhiyun 	struct rb_node *parent, **p;
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun try_again:
57*4882a593Smuzhiyun 	parent = NULL;
58*4882a593Smuzhiyun 	p = &key_user_tree.rb_node;
59*4882a593Smuzhiyun 	spin_lock(&key_user_lock);
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 	/* search the tree for a user record with a matching UID */
62*4882a593Smuzhiyun 	while (*p) {
63*4882a593Smuzhiyun 		parent = *p;
64*4882a593Smuzhiyun 		user = rb_entry(parent, struct key_user, node);
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 		if (uid_lt(uid, user->uid))
67*4882a593Smuzhiyun 			p = &(*p)->rb_left;
68*4882a593Smuzhiyun 		else if (uid_gt(uid, user->uid))
69*4882a593Smuzhiyun 			p = &(*p)->rb_right;
70*4882a593Smuzhiyun 		else
71*4882a593Smuzhiyun 			goto found;
72*4882a593Smuzhiyun 	}
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	/* if we get here, we failed to find a match in the tree */
75*4882a593Smuzhiyun 	if (!candidate) {
76*4882a593Smuzhiyun 		/* allocate a candidate user record if we don't already have
77*4882a593Smuzhiyun 		 * one */
78*4882a593Smuzhiyun 		spin_unlock(&key_user_lock);
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 		user = NULL;
81*4882a593Smuzhiyun 		candidate = kmalloc(sizeof(struct key_user), GFP_KERNEL);
82*4882a593Smuzhiyun 		if (unlikely(!candidate))
83*4882a593Smuzhiyun 			goto out;
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 		/* the allocation may have scheduled, so we need to repeat the
86*4882a593Smuzhiyun 		 * search lest someone else added the record whilst we were
87*4882a593Smuzhiyun 		 * asleep */
88*4882a593Smuzhiyun 		goto try_again;
89*4882a593Smuzhiyun 	}
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	/* if we get here, then the user record still hadn't appeared on the
92*4882a593Smuzhiyun 	 * second pass - so we use the candidate record */
93*4882a593Smuzhiyun 	refcount_set(&candidate->usage, 1);
94*4882a593Smuzhiyun 	atomic_set(&candidate->nkeys, 0);
95*4882a593Smuzhiyun 	atomic_set(&candidate->nikeys, 0);
96*4882a593Smuzhiyun 	candidate->uid = uid;
97*4882a593Smuzhiyun 	candidate->qnkeys = 0;
98*4882a593Smuzhiyun 	candidate->qnbytes = 0;
99*4882a593Smuzhiyun 	spin_lock_init(&candidate->lock);
100*4882a593Smuzhiyun 	mutex_init(&candidate->cons_lock);
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 	rb_link_node(&candidate->node, parent, p);
103*4882a593Smuzhiyun 	rb_insert_color(&candidate->node, &key_user_tree);
104*4882a593Smuzhiyun 	spin_unlock(&key_user_lock);
105*4882a593Smuzhiyun 	user = candidate;
106*4882a593Smuzhiyun 	goto out;
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	/* okay - we found a user record for this UID */
109*4882a593Smuzhiyun found:
110*4882a593Smuzhiyun 	refcount_inc(&user->usage);
111*4882a593Smuzhiyun 	spin_unlock(&key_user_lock);
112*4882a593Smuzhiyun 	kfree(candidate);
113*4882a593Smuzhiyun out:
114*4882a593Smuzhiyun 	return user;
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun /*
118*4882a593Smuzhiyun  * Dispose of a user structure
119*4882a593Smuzhiyun  */
key_user_put(struct key_user * user)120*4882a593Smuzhiyun void key_user_put(struct key_user *user)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun 	if (refcount_dec_and_lock(&user->usage, &key_user_lock)) {
123*4882a593Smuzhiyun 		rb_erase(&user->node, &key_user_tree);
124*4882a593Smuzhiyun 		spin_unlock(&key_user_lock);
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 		kfree(user);
127*4882a593Smuzhiyun 	}
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun /*
131*4882a593Smuzhiyun  * Allocate a serial number for a key.  These are assigned randomly to avoid
132*4882a593Smuzhiyun  * security issues through covert channel problems.
133*4882a593Smuzhiyun  */
key_alloc_serial(struct key * key)134*4882a593Smuzhiyun static inline void key_alloc_serial(struct key *key)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun 	struct rb_node *parent, **p;
137*4882a593Smuzhiyun 	struct key *xkey;
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	/* propose a random serial number and look for a hole for it in the
140*4882a593Smuzhiyun 	 * serial number tree */
141*4882a593Smuzhiyun 	do {
142*4882a593Smuzhiyun 		get_random_bytes(&key->serial, sizeof(key->serial));
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 		key->serial >>= 1; /* negative numbers are not permitted */
145*4882a593Smuzhiyun 	} while (key->serial < 3);
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	spin_lock(&key_serial_lock);
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun attempt_insertion:
150*4882a593Smuzhiyun 	parent = NULL;
151*4882a593Smuzhiyun 	p = &key_serial_tree.rb_node;
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 	while (*p) {
154*4882a593Smuzhiyun 		parent = *p;
155*4882a593Smuzhiyun 		xkey = rb_entry(parent, struct key, serial_node);
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 		if (key->serial < xkey->serial)
158*4882a593Smuzhiyun 			p = &(*p)->rb_left;
159*4882a593Smuzhiyun 		else if (key->serial > xkey->serial)
160*4882a593Smuzhiyun 			p = &(*p)->rb_right;
161*4882a593Smuzhiyun 		else
162*4882a593Smuzhiyun 			goto serial_exists;
163*4882a593Smuzhiyun 	}
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	/* we've found a suitable hole - arrange for this key to occupy it */
166*4882a593Smuzhiyun 	rb_link_node(&key->serial_node, parent, p);
167*4882a593Smuzhiyun 	rb_insert_color(&key->serial_node, &key_serial_tree);
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	spin_unlock(&key_serial_lock);
170*4882a593Smuzhiyun 	return;
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	/* we found a key with the proposed serial number - walk the tree from
173*4882a593Smuzhiyun 	 * that point looking for the next unused serial number */
174*4882a593Smuzhiyun serial_exists:
175*4882a593Smuzhiyun 	for (;;) {
176*4882a593Smuzhiyun 		key->serial++;
177*4882a593Smuzhiyun 		if (key->serial < 3) {
178*4882a593Smuzhiyun 			key->serial = 3;
179*4882a593Smuzhiyun 			goto attempt_insertion;
180*4882a593Smuzhiyun 		}
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 		parent = rb_next(parent);
183*4882a593Smuzhiyun 		if (!parent)
184*4882a593Smuzhiyun 			goto attempt_insertion;
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 		xkey = rb_entry(parent, struct key, serial_node);
187*4882a593Smuzhiyun 		if (key->serial < xkey->serial)
188*4882a593Smuzhiyun 			goto attempt_insertion;
189*4882a593Smuzhiyun 	}
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun /**
193*4882a593Smuzhiyun  * key_alloc - Allocate a key of the specified type.
194*4882a593Smuzhiyun  * @type: The type of key to allocate.
195*4882a593Smuzhiyun  * @desc: The key description to allow the key to be searched out.
196*4882a593Smuzhiyun  * @uid: The owner of the new key.
197*4882a593Smuzhiyun  * @gid: The group ID for the new key's group permissions.
198*4882a593Smuzhiyun  * @cred: The credentials specifying UID namespace.
199*4882a593Smuzhiyun  * @perm: The permissions mask of the new key.
200*4882a593Smuzhiyun  * @flags: Flags specifying quota properties.
201*4882a593Smuzhiyun  * @restrict_link: Optional link restriction for new keyrings.
202*4882a593Smuzhiyun  *
203*4882a593Smuzhiyun  * Allocate a key of the specified type with the attributes given.  The key is
204*4882a593Smuzhiyun  * returned in an uninstantiated state and the caller needs to instantiate the
205*4882a593Smuzhiyun  * key before returning.
206*4882a593Smuzhiyun  *
207*4882a593Smuzhiyun  * The restrict_link structure (if not NULL) will be freed when the
208*4882a593Smuzhiyun  * keyring is destroyed, so it must be dynamically allocated.
209*4882a593Smuzhiyun  *
210*4882a593Smuzhiyun  * The user's key count quota is updated to reflect the creation of the key and
211*4882a593Smuzhiyun  * the user's key data quota has the default for the key type reserved.  The
212*4882a593Smuzhiyun  * instantiation function should amend this as necessary.  If insufficient
213*4882a593Smuzhiyun  * quota is available, -EDQUOT will be returned.
214*4882a593Smuzhiyun  *
215*4882a593Smuzhiyun  * The LSM security modules can prevent a key being created, in which case
216*4882a593Smuzhiyun  * -EACCES will be returned.
217*4882a593Smuzhiyun  *
218*4882a593Smuzhiyun  * Returns a pointer to the new key if successful and an error code otherwise.
219*4882a593Smuzhiyun  *
220*4882a593Smuzhiyun  * Note that the caller needs to ensure the key type isn't uninstantiated.
221*4882a593Smuzhiyun  * Internally this can be done by locking key_types_sem.  Externally, this can
222*4882a593Smuzhiyun  * be done by either never unregistering the key type, or making sure
223*4882a593Smuzhiyun  * key_alloc() calls don't race with module unloading.
224*4882a593Smuzhiyun  */
key_alloc(struct key_type * type,const char * desc,kuid_t uid,kgid_t gid,const struct cred * cred,key_perm_t perm,unsigned long flags,struct key_restriction * restrict_link)225*4882a593Smuzhiyun struct key *key_alloc(struct key_type *type, const char *desc,
226*4882a593Smuzhiyun 		      kuid_t uid, kgid_t gid, const struct cred *cred,
227*4882a593Smuzhiyun 		      key_perm_t perm, unsigned long flags,
228*4882a593Smuzhiyun 		      struct key_restriction *restrict_link)
229*4882a593Smuzhiyun {
230*4882a593Smuzhiyun 	struct key_user *user = NULL;
231*4882a593Smuzhiyun 	struct key *key;
232*4882a593Smuzhiyun 	size_t desclen, quotalen;
233*4882a593Smuzhiyun 	int ret;
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	key = ERR_PTR(-EINVAL);
236*4882a593Smuzhiyun 	if (!desc || !*desc)
237*4882a593Smuzhiyun 		goto error;
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	if (type->vet_description) {
240*4882a593Smuzhiyun 		ret = type->vet_description(desc);
241*4882a593Smuzhiyun 		if (ret < 0) {
242*4882a593Smuzhiyun 			key = ERR_PTR(ret);
243*4882a593Smuzhiyun 			goto error;
244*4882a593Smuzhiyun 		}
245*4882a593Smuzhiyun 	}
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 	desclen = strlen(desc);
248*4882a593Smuzhiyun 	quotalen = desclen + 1 + type->def_datalen;
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 	/* get hold of the key tracking for this user */
251*4882a593Smuzhiyun 	user = key_user_lookup(uid);
252*4882a593Smuzhiyun 	if (!user)
253*4882a593Smuzhiyun 		goto no_memory_1;
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 	/* check that the user's quota permits allocation of another key and
256*4882a593Smuzhiyun 	 * its description */
257*4882a593Smuzhiyun 	if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) {
258*4882a593Smuzhiyun 		unsigned maxkeys = uid_eq(uid, GLOBAL_ROOT_UID) ?
259*4882a593Smuzhiyun 			key_quota_root_maxkeys : key_quota_maxkeys;
260*4882a593Smuzhiyun 		unsigned maxbytes = uid_eq(uid, GLOBAL_ROOT_UID) ?
261*4882a593Smuzhiyun 			key_quota_root_maxbytes : key_quota_maxbytes;
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun 		spin_lock(&user->lock);
264*4882a593Smuzhiyun 		if (!(flags & KEY_ALLOC_QUOTA_OVERRUN)) {
265*4882a593Smuzhiyun 			if (user->qnkeys + 1 > maxkeys ||
266*4882a593Smuzhiyun 			    user->qnbytes + quotalen > maxbytes ||
267*4882a593Smuzhiyun 			    user->qnbytes + quotalen < user->qnbytes)
268*4882a593Smuzhiyun 				goto no_quota;
269*4882a593Smuzhiyun 		}
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun 		user->qnkeys++;
272*4882a593Smuzhiyun 		user->qnbytes += quotalen;
273*4882a593Smuzhiyun 		spin_unlock(&user->lock);
274*4882a593Smuzhiyun 	}
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 	/* allocate and initialise the key and its description */
277*4882a593Smuzhiyun 	key = kmem_cache_zalloc(key_jar, GFP_KERNEL);
278*4882a593Smuzhiyun 	if (!key)
279*4882a593Smuzhiyun 		goto no_memory_2;
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 	key->index_key.desc_len = desclen;
282*4882a593Smuzhiyun 	key->index_key.description = kmemdup(desc, desclen + 1, GFP_KERNEL);
283*4882a593Smuzhiyun 	if (!key->index_key.description)
284*4882a593Smuzhiyun 		goto no_memory_3;
285*4882a593Smuzhiyun 	key->index_key.type = type;
286*4882a593Smuzhiyun 	key_set_index_key(&key->index_key);
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun 	refcount_set(&key->usage, 1);
289*4882a593Smuzhiyun 	init_rwsem(&key->sem);
290*4882a593Smuzhiyun 	lockdep_set_class(&key->sem, &type->lock_class);
291*4882a593Smuzhiyun 	key->user = user;
292*4882a593Smuzhiyun 	key->quotalen = quotalen;
293*4882a593Smuzhiyun 	key->datalen = type->def_datalen;
294*4882a593Smuzhiyun 	key->uid = uid;
295*4882a593Smuzhiyun 	key->gid = gid;
296*4882a593Smuzhiyun 	key->perm = perm;
297*4882a593Smuzhiyun 	key->restrict_link = restrict_link;
298*4882a593Smuzhiyun 	key->last_used_at = ktime_get_real_seconds();
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 	if (!(flags & KEY_ALLOC_NOT_IN_QUOTA))
301*4882a593Smuzhiyun 		key->flags |= 1 << KEY_FLAG_IN_QUOTA;
302*4882a593Smuzhiyun 	if (flags & KEY_ALLOC_BUILT_IN)
303*4882a593Smuzhiyun 		key->flags |= 1 << KEY_FLAG_BUILTIN;
304*4882a593Smuzhiyun 	if (flags & KEY_ALLOC_UID_KEYRING)
305*4882a593Smuzhiyun 		key->flags |= 1 << KEY_FLAG_UID_KEYRING;
306*4882a593Smuzhiyun 	if (flags & KEY_ALLOC_SET_KEEP)
307*4882a593Smuzhiyun 		key->flags |= 1 << KEY_FLAG_KEEP;
308*4882a593Smuzhiyun 
309*4882a593Smuzhiyun #ifdef KEY_DEBUGGING
310*4882a593Smuzhiyun 	key->magic = KEY_DEBUG_MAGIC;
311*4882a593Smuzhiyun #endif
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun 	/* let the security module know about the key */
314*4882a593Smuzhiyun 	ret = security_key_alloc(key, cred, flags);
315*4882a593Smuzhiyun 	if (ret < 0)
316*4882a593Smuzhiyun 		goto security_error;
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 	/* publish the key by giving it a serial number */
319*4882a593Smuzhiyun 	refcount_inc(&key->domain_tag->usage);
320*4882a593Smuzhiyun 	atomic_inc(&user->nkeys);
321*4882a593Smuzhiyun 	key_alloc_serial(key);
322*4882a593Smuzhiyun 
323*4882a593Smuzhiyun error:
324*4882a593Smuzhiyun 	return key;
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun security_error:
327*4882a593Smuzhiyun 	kfree(key->description);
328*4882a593Smuzhiyun 	kmem_cache_free(key_jar, key);
329*4882a593Smuzhiyun 	if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) {
330*4882a593Smuzhiyun 		spin_lock(&user->lock);
331*4882a593Smuzhiyun 		user->qnkeys--;
332*4882a593Smuzhiyun 		user->qnbytes -= quotalen;
333*4882a593Smuzhiyun 		spin_unlock(&user->lock);
334*4882a593Smuzhiyun 	}
335*4882a593Smuzhiyun 	key_user_put(user);
336*4882a593Smuzhiyun 	key = ERR_PTR(ret);
337*4882a593Smuzhiyun 	goto error;
338*4882a593Smuzhiyun 
339*4882a593Smuzhiyun no_memory_3:
340*4882a593Smuzhiyun 	kmem_cache_free(key_jar, key);
341*4882a593Smuzhiyun no_memory_2:
342*4882a593Smuzhiyun 	if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) {
343*4882a593Smuzhiyun 		spin_lock(&user->lock);
344*4882a593Smuzhiyun 		user->qnkeys--;
345*4882a593Smuzhiyun 		user->qnbytes -= quotalen;
346*4882a593Smuzhiyun 		spin_unlock(&user->lock);
347*4882a593Smuzhiyun 	}
348*4882a593Smuzhiyun 	key_user_put(user);
349*4882a593Smuzhiyun no_memory_1:
350*4882a593Smuzhiyun 	key = ERR_PTR(-ENOMEM);
351*4882a593Smuzhiyun 	goto error;
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun no_quota:
354*4882a593Smuzhiyun 	spin_unlock(&user->lock);
355*4882a593Smuzhiyun 	key_user_put(user);
356*4882a593Smuzhiyun 	key = ERR_PTR(-EDQUOT);
357*4882a593Smuzhiyun 	goto error;
358*4882a593Smuzhiyun }
359*4882a593Smuzhiyun EXPORT_SYMBOL(key_alloc);
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun /**
362*4882a593Smuzhiyun  * key_payload_reserve - Adjust data quota reservation for the key's payload
363*4882a593Smuzhiyun  * @key: The key to make the reservation for.
364*4882a593Smuzhiyun  * @datalen: The amount of data payload the caller now wants.
365*4882a593Smuzhiyun  *
366*4882a593Smuzhiyun  * Adjust the amount of the owning user's key data quota that a key reserves.
367*4882a593Smuzhiyun  * If the amount is increased, then -EDQUOT may be returned if there isn't
368*4882a593Smuzhiyun  * enough free quota available.
369*4882a593Smuzhiyun  *
370*4882a593Smuzhiyun  * If successful, 0 is returned.
371*4882a593Smuzhiyun  */
key_payload_reserve(struct key * key,size_t datalen)372*4882a593Smuzhiyun int key_payload_reserve(struct key *key, size_t datalen)
373*4882a593Smuzhiyun {
374*4882a593Smuzhiyun 	int delta = (int)datalen - key->datalen;
375*4882a593Smuzhiyun 	int ret = 0;
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun 	key_check(key);
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun 	/* contemplate the quota adjustment */
380*4882a593Smuzhiyun 	if (delta != 0 && test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
381*4882a593Smuzhiyun 		unsigned maxbytes = uid_eq(key->user->uid, GLOBAL_ROOT_UID) ?
382*4882a593Smuzhiyun 			key_quota_root_maxbytes : key_quota_maxbytes;
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun 		spin_lock(&key->user->lock);
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun 		if (delta > 0 &&
387*4882a593Smuzhiyun 		    (key->user->qnbytes + delta > maxbytes ||
388*4882a593Smuzhiyun 		     key->user->qnbytes + delta < key->user->qnbytes)) {
389*4882a593Smuzhiyun 			ret = -EDQUOT;
390*4882a593Smuzhiyun 		}
391*4882a593Smuzhiyun 		else {
392*4882a593Smuzhiyun 			key->user->qnbytes += delta;
393*4882a593Smuzhiyun 			key->quotalen += delta;
394*4882a593Smuzhiyun 		}
395*4882a593Smuzhiyun 		spin_unlock(&key->user->lock);
396*4882a593Smuzhiyun 	}
397*4882a593Smuzhiyun 
398*4882a593Smuzhiyun 	/* change the recorded data length if that didn't generate an error */
399*4882a593Smuzhiyun 	if (ret == 0)
400*4882a593Smuzhiyun 		key->datalen = datalen;
401*4882a593Smuzhiyun 
402*4882a593Smuzhiyun 	return ret;
403*4882a593Smuzhiyun }
404*4882a593Smuzhiyun EXPORT_SYMBOL(key_payload_reserve);
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun /*
407*4882a593Smuzhiyun  * Change the key state to being instantiated.
408*4882a593Smuzhiyun  */
mark_key_instantiated(struct key * key,int reject_error)409*4882a593Smuzhiyun static void mark_key_instantiated(struct key *key, int reject_error)
410*4882a593Smuzhiyun {
411*4882a593Smuzhiyun 	/* Commit the payload before setting the state; barrier versus
412*4882a593Smuzhiyun 	 * key_read_state().
413*4882a593Smuzhiyun 	 */
414*4882a593Smuzhiyun 	smp_store_release(&key->state,
415*4882a593Smuzhiyun 			  (reject_error < 0) ? reject_error : KEY_IS_POSITIVE);
416*4882a593Smuzhiyun }
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun /*
419*4882a593Smuzhiyun  * Instantiate a key and link it into the target keyring atomically.  Must be
420*4882a593Smuzhiyun  * called with the target keyring's semaphore writelocked.  The target key's
421*4882a593Smuzhiyun  * semaphore need not be locked as instantiation is serialised by
422*4882a593Smuzhiyun  * key_construction_mutex.
423*4882a593Smuzhiyun  */
__key_instantiate_and_link(struct key * key,struct key_preparsed_payload * prep,struct key * keyring,struct key * authkey,struct assoc_array_edit ** _edit)424*4882a593Smuzhiyun static int __key_instantiate_and_link(struct key *key,
425*4882a593Smuzhiyun 				      struct key_preparsed_payload *prep,
426*4882a593Smuzhiyun 				      struct key *keyring,
427*4882a593Smuzhiyun 				      struct key *authkey,
428*4882a593Smuzhiyun 				      struct assoc_array_edit **_edit)
429*4882a593Smuzhiyun {
430*4882a593Smuzhiyun 	int ret, awaken;
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun 	key_check(key);
433*4882a593Smuzhiyun 	key_check(keyring);
434*4882a593Smuzhiyun 
435*4882a593Smuzhiyun 	awaken = 0;
436*4882a593Smuzhiyun 	ret = -EBUSY;
437*4882a593Smuzhiyun 
438*4882a593Smuzhiyun 	mutex_lock(&key_construction_mutex);
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun 	/* can't instantiate twice */
441*4882a593Smuzhiyun 	if (key->state == KEY_IS_UNINSTANTIATED) {
442*4882a593Smuzhiyun 		/* instantiate the key */
443*4882a593Smuzhiyun 		ret = key->type->instantiate(key, prep);
444*4882a593Smuzhiyun 
445*4882a593Smuzhiyun 		if (ret == 0) {
446*4882a593Smuzhiyun 			/* mark the key as being instantiated */
447*4882a593Smuzhiyun 			atomic_inc(&key->user->nikeys);
448*4882a593Smuzhiyun 			mark_key_instantiated(key, 0);
449*4882a593Smuzhiyun 			notify_key(key, NOTIFY_KEY_INSTANTIATED, 0);
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun 			if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags))
452*4882a593Smuzhiyun 				awaken = 1;
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun 			/* and link it into the destination keyring */
455*4882a593Smuzhiyun 			if (keyring) {
456*4882a593Smuzhiyun 				if (test_bit(KEY_FLAG_KEEP, &keyring->flags))
457*4882a593Smuzhiyun 					set_bit(KEY_FLAG_KEEP, &key->flags);
458*4882a593Smuzhiyun 
459*4882a593Smuzhiyun 				__key_link(keyring, key, _edit);
460*4882a593Smuzhiyun 			}
461*4882a593Smuzhiyun 
462*4882a593Smuzhiyun 			/* disable the authorisation key */
463*4882a593Smuzhiyun 			if (authkey)
464*4882a593Smuzhiyun 				key_invalidate(authkey);
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun 			if (prep->expiry != TIME64_MAX) {
467*4882a593Smuzhiyun 				key->expiry = prep->expiry;
468*4882a593Smuzhiyun 				key_schedule_gc(prep->expiry + key_gc_delay);
469*4882a593Smuzhiyun 			}
470*4882a593Smuzhiyun 		}
471*4882a593Smuzhiyun 	}
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun 	mutex_unlock(&key_construction_mutex);
474*4882a593Smuzhiyun 
475*4882a593Smuzhiyun 	/* wake up anyone waiting for a key to be constructed */
476*4882a593Smuzhiyun 	if (awaken)
477*4882a593Smuzhiyun 		wake_up_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT);
478*4882a593Smuzhiyun 
479*4882a593Smuzhiyun 	return ret;
480*4882a593Smuzhiyun }
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun /**
483*4882a593Smuzhiyun  * key_instantiate_and_link - Instantiate a key and link it into the keyring.
484*4882a593Smuzhiyun  * @key: The key to instantiate.
485*4882a593Smuzhiyun  * @data: The data to use to instantiate the keyring.
486*4882a593Smuzhiyun  * @datalen: The length of @data.
487*4882a593Smuzhiyun  * @keyring: Keyring to create a link in on success (or NULL).
488*4882a593Smuzhiyun  * @authkey: The authorisation token permitting instantiation.
489*4882a593Smuzhiyun  *
490*4882a593Smuzhiyun  * Instantiate a key that's in the uninstantiated state using the provided data
491*4882a593Smuzhiyun  * and, if successful, link it in to the destination keyring if one is
492*4882a593Smuzhiyun  * supplied.
493*4882a593Smuzhiyun  *
494*4882a593Smuzhiyun  * If successful, 0 is returned, the authorisation token is revoked and anyone
495*4882a593Smuzhiyun  * waiting for the key is woken up.  If the key was already instantiated,
496*4882a593Smuzhiyun  * -EBUSY will be returned.
497*4882a593Smuzhiyun  */
key_instantiate_and_link(struct key * key,const void * data,size_t datalen,struct key * keyring,struct key * authkey)498*4882a593Smuzhiyun int key_instantiate_and_link(struct key *key,
499*4882a593Smuzhiyun 			     const void *data,
500*4882a593Smuzhiyun 			     size_t datalen,
501*4882a593Smuzhiyun 			     struct key *keyring,
502*4882a593Smuzhiyun 			     struct key *authkey)
503*4882a593Smuzhiyun {
504*4882a593Smuzhiyun 	struct key_preparsed_payload prep;
505*4882a593Smuzhiyun 	struct assoc_array_edit *edit = NULL;
506*4882a593Smuzhiyun 	int ret;
507*4882a593Smuzhiyun 
508*4882a593Smuzhiyun 	memset(&prep, 0, sizeof(prep));
509*4882a593Smuzhiyun 	prep.data = data;
510*4882a593Smuzhiyun 	prep.datalen = datalen;
511*4882a593Smuzhiyun 	prep.quotalen = key->type->def_datalen;
512*4882a593Smuzhiyun 	prep.expiry = TIME64_MAX;
513*4882a593Smuzhiyun 	if (key->type->preparse) {
514*4882a593Smuzhiyun 		ret = key->type->preparse(&prep);
515*4882a593Smuzhiyun 		if (ret < 0)
516*4882a593Smuzhiyun 			goto error;
517*4882a593Smuzhiyun 	}
518*4882a593Smuzhiyun 
519*4882a593Smuzhiyun 	if (keyring) {
520*4882a593Smuzhiyun 		ret = __key_link_lock(keyring, &key->index_key);
521*4882a593Smuzhiyun 		if (ret < 0)
522*4882a593Smuzhiyun 			goto error;
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun 		ret = __key_link_begin(keyring, &key->index_key, &edit);
525*4882a593Smuzhiyun 		if (ret < 0)
526*4882a593Smuzhiyun 			goto error_link_end;
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun 		if (keyring->restrict_link && keyring->restrict_link->check) {
529*4882a593Smuzhiyun 			struct key_restriction *keyres = keyring->restrict_link;
530*4882a593Smuzhiyun 
531*4882a593Smuzhiyun 			ret = keyres->check(keyring, key->type, &prep.payload,
532*4882a593Smuzhiyun 					    keyres->key);
533*4882a593Smuzhiyun 			if (ret < 0)
534*4882a593Smuzhiyun 				goto error_link_end;
535*4882a593Smuzhiyun 		}
536*4882a593Smuzhiyun 	}
537*4882a593Smuzhiyun 
538*4882a593Smuzhiyun 	ret = __key_instantiate_and_link(key, &prep, keyring, authkey, &edit);
539*4882a593Smuzhiyun 
540*4882a593Smuzhiyun error_link_end:
541*4882a593Smuzhiyun 	if (keyring)
542*4882a593Smuzhiyun 		__key_link_end(keyring, &key->index_key, edit);
543*4882a593Smuzhiyun 
544*4882a593Smuzhiyun error:
545*4882a593Smuzhiyun 	if (key->type->preparse)
546*4882a593Smuzhiyun 		key->type->free_preparse(&prep);
547*4882a593Smuzhiyun 	return ret;
548*4882a593Smuzhiyun }
549*4882a593Smuzhiyun 
550*4882a593Smuzhiyun EXPORT_SYMBOL(key_instantiate_and_link);
551*4882a593Smuzhiyun 
552*4882a593Smuzhiyun /**
553*4882a593Smuzhiyun  * key_reject_and_link - Negatively instantiate a key and link it into the keyring.
554*4882a593Smuzhiyun  * @key: The key to instantiate.
555*4882a593Smuzhiyun  * @timeout: The timeout on the negative key.
556*4882a593Smuzhiyun  * @error: The error to return when the key is hit.
557*4882a593Smuzhiyun  * @keyring: Keyring to create a link in on success (or NULL).
558*4882a593Smuzhiyun  * @authkey: The authorisation token permitting instantiation.
559*4882a593Smuzhiyun  *
560*4882a593Smuzhiyun  * Negatively instantiate a key that's in the uninstantiated state and, if
561*4882a593Smuzhiyun  * successful, set its timeout and stored error and link it in to the
562*4882a593Smuzhiyun  * destination keyring if one is supplied.  The key and any links to the key
563*4882a593Smuzhiyun  * will be automatically garbage collected after the timeout expires.
564*4882a593Smuzhiyun  *
565*4882a593Smuzhiyun  * Negative keys are used to rate limit repeated request_key() calls by causing
566*4882a593Smuzhiyun  * them to return the stored error code (typically ENOKEY) until the negative
567*4882a593Smuzhiyun  * key expires.
568*4882a593Smuzhiyun  *
569*4882a593Smuzhiyun  * If successful, 0 is returned, the authorisation token is revoked and anyone
570*4882a593Smuzhiyun  * waiting for the key is woken up.  If the key was already instantiated,
571*4882a593Smuzhiyun  * -EBUSY will be returned.
572*4882a593Smuzhiyun  */
key_reject_and_link(struct key * key,unsigned timeout,unsigned error,struct key * keyring,struct key * authkey)573*4882a593Smuzhiyun int key_reject_and_link(struct key *key,
574*4882a593Smuzhiyun 			unsigned timeout,
575*4882a593Smuzhiyun 			unsigned error,
576*4882a593Smuzhiyun 			struct key *keyring,
577*4882a593Smuzhiyun 			struct key *authkey)
578*4882a593Smuzhiyun {
579*4882a593Smuzhiyun 	struct assoc_array_edit *edit = NULL;
580*4882a593Smuzhiyun 	int ret, awaken, link_ret = 0;
581*4882a593Smuzhiyun 
582*4882a593Smuzhiyun 	key_check(key);
583*4882a593Smuzhiyun 	key_check(keyring);
584*4882a593Smuzhiyun 
585*4882a593Smuzhiyun 	awaken = 0;
586*4882a593Smuzhiyun 	ret = -EBUSY;
587*4882a593Smuzhiyun 
588*4882a593Smuzhiyun 	if (keyring) {
589*4882a593Smuzhiyun 		if (keyring->restrict_link)
590*4882a593Smuzhiyun 			return -EPERM;
591*4882a593Smuzhiyun 
592*4882a593Smuzhiyun 		link_ret = __key_link_lock(keyring, &key->index_key);
593*4882a593Smuzhiyun 		if (link_ret == 0) {
594*4882a593Smuzhiyun 			link_ret = __key_link_begin(keyring, &key->index_key, &edit);
595*4882a593Smuzhiyun 			if (link_ret < 0)
596*4882a593Smuzhiyun 				__key_link_end(keyring, &key->index_key, edit);
597*4882a593Smuzhiyun 		}
598*4882a593Smuzhiyun 	}
599*4882a593Smuzhiyun 
600*4882a593Smuzhiyun 	mutex_lock(&key_construction_mutex);
601*4882a593Smuzhiyun 
602*4882a593Smuzhiyun 	/* can't instantiate twice */
603*4882a593Smuzhiyun 	if (key->state == KEY_IS_UNINSTANTIATED) {
604*4882a593Smuzhiyun 		/* mark the key as being negatively instantiated */
605*4882a593Smuzhiyun 		atomic_inc(&key->user->nikeys);
606*4882a593Smuzhiyun 		mark_key_instantiated(key, -error);
607*4882a593Smuzhiyun 		notify_key(key, NOTIFY_KEY_INSTANTIATED, -error);
608*4882a593Smuzhiyun 		key->expiry = ktime_get_real_seconds() + timeout;
609*4882a593Smuzhiyun 		key_schedule_gc(key->expiry + key_gc_delay);
610*4882a593Smuzhiyun 
611*4882a593Smuzhiyun 		if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags))
612*4882a593Smuzhiyun 			awaken = 1;
613*4882a593Smuzhiyun 
614*4882a593Smuzhiyun 		ret = 0;
615*4882a593Smuzhiyun 
616*4882a593Smuzhiyun 		/* and link it into the destination keyring */
617*4882a593Smuzhiyun 		if (keyring && link_ret == 0)
618*4882a593Smuzhiyun 			__key_link(keyring, key, &edit);
619*4882a593Smuzhiyun 
620*4882a593Smuzhiyun 		/* disable the authorisation key */
621*4882a593Smuzhiyun 		if (authkey)
622*4882a593Smuzhiyun 			key_invalidate(authkey);
623*4882a593Smuzhiyun 	}
624*4882a593Smuzhiyun 
625*4882a593Smuzhiyun 	mutex_unlock(&key_construction_mutex);
626*4882a593Smuzhiyun 
627*4882a593Smuzhiyun 	if (keyring && link_ret == 0)
628*4882a593Smuzhiyun 		__key_link_end(keyring, &key->index_key, edit);
629*4882a593Smuzhiyun 
630*4882a593Smuzhiyun 	/* wake up anyone waiting for a key to be constructed */
631*4882a593Smuzhiyun 	if (awaken)
632*4882a593Smuzhiyun 		wake_up_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT);
633*4882a593Smuzhiyun 
634*4882a593Smuzhiyun 	return ret == 0 ? link_ret : ret;
635*4882a593Smuzhiyun }
636*4882a593Smuzhiyun EXPORT_SYMBOL(key_reject_and_link);
637*4882a593Smuzhiyun 
638*4882a593Smuzhiyun /**
639*4882a593Smuzhiyun  * key_put - Discard a reference to a key.
640*4882a593Smuzhiyun  * @key: The key to discard a reference from.
641*4882a593Smuzhiyun  *
642*4882a593Smuzhiyun  * Discard a reference to a key, and when all the references are gone, we
643*4882a593Smuzhiyun  * schedule the cleanup task to come and pull it out of the tree in process
644*4882a593Smuzhiyun  * context at some later time.
645*4882a593Smuzhiyun  */
key_put(struct key * key)646*4882a593Smuzhiyun void key_put(struct key *key)
647*4882a593Smuzhiyun {
648*4882a593Smuzhiyun 	if (key) {
649*4882a593Smuzhiyun 		key_check(key);
650*4882a593Smuzhiyun 
651*4882a593Smuzhiyun 		if (refcount_dec_and_test(&key->usage))
652*4882a593Smuzhiyun 			schedule_work(&key_gc_work);
653*4882a593Smuzhiyun 	}
654*4882a593Smuzhiyun }
655*4882a593Smuzhiyun EXPORT_SYMBOL(key_put);
656*4882a593Smuzhiyun 
657*4882a593Smuzhiyun /*
658*4882a593Smuzhiyun  * Find a key by its serial number.
659*4882a593Smuzhiyun  */
key_lookup(key_serial_t id)660*4882a593Smuzhiyun struct key *key_lookup(key_serial_t id)
661*4882a593Smuzhiyun {
662*4882a593Smuzhiyun 	struct rb_node *n;
663*4882a593Smuzhiyun 	struct key *key;
664*4882a593Smuzhiyun 
665*4882a593Smuzhiyun 	spin_lock(&key_serial_lock);
666*4882a593Smuzhiyun 
667*4882a593Smuzhiyun 	/* search the tree for the specified key */
668*4882a593Smuzhiyun 	n = key_serial_tree.rb_node;
669*4882a593Smuzhiyun 	while (n) {
670*4882a593Smuzhiyun 		key = rb_entry(n, struct key, serial_node);
671*4882a593Smuzhiyun 
672*4882a593Smuzhiyun 		if (id < key->serial)
673*4882a593Smuzhiyun 			n = n->rb_left;
674*4882a593Smuzhiyun 		else if (id > key->serial)
675*4882a593Smuzhiyun 			n = n->rb_right;
676*4882a593Smuzhiyun 		else
677*4882a593Smuzhiyun 			goto found;
678*4882a593Smuzhiyun 	}
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun not_found:
681*4882a593Smuzhiyun 	key = ERR_PTR(-ENOKEY);
682*4882a593Smuzhiyun 	goto error;
683*4882a593Smuzhiyun 
684*4882a593Smuzhiyun found:
685*4882a593Smuzhiyun 	/* A key is allowed to be looked up only if someone still owns a
686*4882a593Smuzhiyun 	 * reference to it - otherwise it's awaiting the gc.
687*4882a593Smuzhiyun 	 */
688*4882a593Smuzhiyun 	if (!refcount_inc_not_zero(&key->usage))
689*4882a593Smuzhiyun 		goto not_found;
690*4882a593Smuzhiyun 
691*4882a593Smuzhiyun error:
692*4882a593Smuzhiyun 	spin_unlock(&key_serial_lock);
693*4882a593Smuzhiyun 	return key;
694*4882a593Smuzhiyun }
695*4882a593Smuzhiyun 
696*4882a593Smuzhiyun /*
697*4882a593Smuzhiyun  * Find and lock the specified key type against removal.
698*4882a593Smuzhiyun  *
699*4882a593Smuzhiyun  * We return with the sem read-locked if successful.  If the type wasn't
700*4882a593Smuzhiyun  * available -ENOKEY is returned instead.
701*4882a593Smuzhiyun  */
key_type_lookup(const char * type)702*4882a593Smuzhiyun struct key_type *key_type_lookup(const char *type)
703*4882a593Smuzhiyun {
704*4882a593Smuzhiyun 	struct key_type *ktype;
705*4882a593Smuzhiyun 
706*4882a593Smuzhiyun 	down_read(&key_types_sem);
707*4882a593Smuzhiyun 
708*4882a593Smuzhiyun 	/* look up the key type to see if it's one of the registered kernel
709*4882a593Smuzhiyun 	 * types */
710*4882a593Smuzhiyun 	list_for_each_entry(ktype, &key_types_list, link) {
711*4882a593Smuzhiyun 		if (strcmp(ktype->name, type) == 0)
712*4882a593Smuzhiyun 			goto found_kernel_type;
713*4882a593Smuzhiyun 	}
714*4882a593Smuzhiyun 
715*4882a593Smuzhiyun 	up_read(&key_types_sem);
716*4882a593Smuzhiyun 	ktype = ERR_PTR(-ENOKEY);
717*4882a593Smuzhiyun 
718*4882a593Smuzhiyun found_kernel_type:
719*4882a593Smuzhiyun 	return ktype;
720*4882a593Smuzhiyun }
721*4882a593Smuzhiyun 
key_set_timeout(struct key * key,unsigned timeout)722*4882a593Smuzhiyun void key_set_timeout(struct key *key, unsigned timeout)
723*4882a593Smuzhiyun {
724*4882a593Smuzhiyun 	time64_t expiry = 0;
725*4882a593Smuzhiyun 
726*4882a593Smuzhiyun 	/* make the changes with the locks held to prevent races */
727*4882a593Smuzhiyun 	down_write(&key->sem);
728*4882a593Smuzhiyun 
729*4882a593Smuzhiyun 	if (timeout > 0)
730*4882a593Smuzhiyun 		expiry = ktime_get_real_seconds() + timeout;
731*4882a593Smuzhiyun 
732*4882a593Smuzhiyun 	key->expiry = expiry;
733*4882a593Smuzhiyun 	key_schedule_gc(key->expiry + key_gc_delay);
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun 	up_write(&key->sem);
736*4882a593Smuzhiyun }
737*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(key_set_timeout);
738*4882a593Smuzhiyun 
739*4882a593Smuzhiyun /*
740*4882a593Smuzhiyun  * Unlock a key type locked by key_type_lookup().
741*4882a593Smuzhiyun  */
key_type_put(struct key_type * ktype)742*4882a593Smuzhiyun void key_type_put(struct key_type *ktype)
743*4882a593Smuzhiyun {
744*4882a593Smuzhiyun 	up_read(&key_types_sem);
745*4882a593Smuzhiyun }
746*4882a593Smuzhiyun 
747*4882a593Smuzhiyun /*
748*4882a593Smuzhiyun  * Attempt to update an existing key.
749*4882a593Smuzhiyun  *
750*4882a593Smuzhiyun  * The key is given to us with an incremented refcount that we need to discard
751*4882a593Smuzhiyun  * if we get an error.
752*4882a593Smuzhiyun  */
__key_update(key_ref_t key_ref,struct key_preparsed_payload * prep)753*4882a593Smuzhiyun static inline key_ref_t __key_update(key_ref_t key_ref,
754*4882a593Smuzhiyun 				     struct key_preparsed_payload *prep)
755*4882a593Smuzhiyun {
756*4882a593Smuzhiyun 	struct key *key = key_ref_to_ptr(key_ref);
757*4882a593Smuzhiyun 	int ret;
758*4882a593Smuzhiyun 
759*4882a593Smuzhiyun 	/* need write permission on the key to update it */
760*4882a593Smuzhiyun 	ret = key_permission(key_ref, KEY_NEED_WRITE);
761*4882a593Smuzhiyun 	if (ret < 0)
762*4882a593Smuzhiyun 		goto error;
763*4882a593Smuzhiyun 
764*4882a593Smuzhiyun 	ret = -EEXIST;
765*4882a593Smuzhiyun 	if (!key->type->update)
766*4882a593Smuzhiyun 		goto error;
767*4882a593Smuzhiyun 
768*4882a593Smuzhiyun 	down_write(&key->sem);
769*4882a593Smuzhiyun 
770*4882a593Smuzhiyun 	ret = key->type->update(key, prep);
771*4882a593Smuzhiyun 	if (ret == 0) {
772*4882a593Smuzhiyun 		/* Updating a negative key positively instantiates it */
773*4882a593Smuzhiyun 		mark_key_instantiated(key, 0);
774*4882a593Smuzhiyun 		notify_key(key, NOTIFY_KEY_UPDATED, 0);
775*4882a593Smuzhiyun 	}
776*4882a593Smuzhiyun 
777*4882a593Smuzhiyun 	up_write(&key->sem);
778*4882a593Smuzhiyun 
779*4882a593Smuzhiyun 	if (ret < 0)
780*4882a593Smuzhiyun 		goto error;
781*4882a593Smuzhiyun out:
782*4882a593Smuzhiyun 	return key_ref;
783*4882a593Smuzhiyun 
784*4882a593Smuzhiyun error:
785*4882a593Smuzhiyun 	key_put(key);
786*4882a593Smuzhiyun 	key_ref = ERR_PTR(ret);
787*4882a593Smuzhiyun 	goto out;
788*4882a593Smuzhiyun }
789*4882a593Smuzhiyun 
790*4882a593Smuzhiyun /**
791*4882a593Smuzhiyun  * key_create_or_update - Update or create and instantiate a key.
792*4882a593Smuzhiyun  * @keyring_ref: A pointer to the destination keyring with possession flag.
793*4882a593Smuzhiyun  * @type: The type of key.
794*4882a593Smuzhiyun  * @description: The searchable description for the key.
795*4882a593Smuzhiyun  * @payload: The data to use to instantiate or update the key.
796*4882a593Smuzhiyun  * @plen: The length of @payload.
797*4882a593Smuzhiyun  * @perm: The permissions mask for a new key.
798*4882a593Smuzhiyun  * @flags: The quota flags for a new key.
799*4882a593Smuzhiyun  *
800*4882a593Smuzhiyun  * Search the destination keyring for a key of the same description and if one
801*4882a593Smuzhiyun  * is found, update it, otherwise create and instantiate a new one and create a
802*4882a593Smuzhiyun  * link to it from that keyring.
803*4882a593Smuzhiyun  *
804*4882a593Smuzhiyun  * If perm is KEY_PERM_UNDEF then an appropriate key permissions mask will be
805*4882a593Smuzhiyun  * concocted.
806*4882a593Smuzhiyun  *
807*4882a593Smuzhiyun  * Returns a pointer to the new key if successful, -ENODEV if the key type
808*4882a593Smuzhiyun  * wasn't available, -ENOTDIR if the keyring wasn't a keyring, -EACCES if the
809*4882a593Smuzhiyun  * caller isn't permitted to modify the keyring or the LSM did not permit
810*4882a593Smuzhiyun  * creation of the key.
811*4882a593Smuzhiyun  *
812*4882a593Smuzhiyun  * On success, the possession flag from the keyring ref will be tacked on to
813*4882a593Smuzhiyun  * the key ref before it is returned.
814*4882a593Smuzhiyun  */
key_create_or_update(key_ref_t keyring_ref,const char * type,const char * description,const void * payload,size_t plen,key_perm_t perm,unsigned long flags)815*4882a593Smuzhiyun key_ref_t key_create_or_update(key_ref_t keyring_ref,
816*4882a593Smuzhiyun 			       const char *type,
817*4882a593Smuzhiyun 			       const char *description,
818*4882a593Smuzhiyun 			       const void *payload,
819*4882a593Smuzhiyun 			       size_t plen,
820*4882a593Smuzhiyun 			       key_perm_t perm,
821*4882a593Smuzhiyun 			       unsigned long flags)
822*4882a593Smuzhiyun {
823*4882a593Smuzhiyun 	struct keyring_index_key index_key = {
824*4882a593Smuzhiyun 		.description	= description,
825*4882a593Smuzhiyun 	};
826*4882a593Smuzhiyun 	struct key_preparsed_payload prep;
827*4882a593Smuzhiyun 	struct assoc_array_edit *edit = NULL;
828*4882a593Smuzhiyun 	const struct cred *cred = current_cred();
829*4882a593Smuzhiyun 	struct key *keyring, *key = NULL;
830*4882a593Smuzhiyun 	key_ref_t key_ref;
831*4882a593Smuzhiyun 	int ret;
832*4882a593Smuzhiyun 	struct key_restriction *restrict_link = NULL;
833*4882a593Smuzhiyun 
834*4882a593Smuzhiyun 	/* look up the key type to see if it's one of the registered kernel
835*4882a593Smuzhiyun 	 * types */
836*4882a593Smuzhiyun 	index_key.type = key_type_lookup(type);
837*4882a593Smuzhiyun 	if (IS_ERR(index_key.type)) {
838*4882a593Smuzhiyun 		key_ref = ERR_PTR(-ENODEV);
839*4882a593Smuzhiyun 		goto error;
840*4882a593Smuzhiyun 	}
841*4882a593Smuzhiyun 
842*4882a593Smuzhiyun 	key_ref = ERR_PTR(-EINVAL);
843*4882a593Smuzhiyun 	if (!index_key.type->instantiate ||
844*4882a593Smuzhiyun 	    (!index_key.description && !index_key.type->preparse))
845*4882a593Smuzhiyun 		goto error_put_type;
846*4882a593Smuzhiyun 
847*4882a593Smuzhiyun 	keyring = key_ref_to_ptr(keyring_ref);
848*4882a593Smuzhiyun 
849*4882a593Smuzhiyun 	key_check(keyring);
850*4882a593Smuzhiyun 
851*4882a593Smuzhiyun 	if (!(flags & KEY_ALLOC_BYPASS_RESTRICTION))
852*4882a593Smuzhiyun 		restrict_link = keyring->restrict_link;
853*4882a593Smuzhiyun 
854*4882a593Smuzhiyun 	key_ref = ERR_PTR(-ENOTDIR);
855*4882a593Smuzhiyun 	if (keyring->type != &key_type_keyring)
856*4882a593Smuzhiyun 		goto error_put_type;
857*4882a593Smuzhiyun 
858*4882a593Smuzhiyun 	memset(&prep, 0, sizeof(prep));
859*4882a593Smuzhiyun 	prep.data = payload;
860*4882a593Smuzhiyun 	prep.datalen = plen;
861*4882a593Smuzhiyun 	prep.quotalen = index_key.type->def_datalen;
862*4882a593Smuzhiyun 	prep.expiry = TIME64_MAX;
863*4882a593Smuzhiyun 	if (index_key.type->preparse) {
864*4882a593Smuzhiyun 		ret = index_key.type->preparse(&prep);
865*4882a593Smuzhiyun 		if (ret < 0) {
866*4882a593Smuzhiyun 			key_ref = ERR_PTR(ret);
867*4882a593Smuzhiyun 			goto error_free_prep;
868*4882a593Smuzhiyun 		}
869*4882a593Smuzhiyun 		if (!index_key.description)
870*4882a593Smuzhiyun 			index_key.description = prep.description;
871*4882a593Smuzhiyun 		key_ref = ERR_PTR(-EINVAL);
872*4882a593Smuzhiyun 		if (!index_key.description)
873*4882a593Smuzhiyun 			goto error_free_prep;
874*4882a593Smuzhiyun 	}
875*4882a593Smuzhiyun 	index_key.desc_len = strlen(index_key.description);
876*4882a593Smuzhiyun 	key_set_index_key(&index_key);
877*4882a593Smuzhiyun 
878*4882a593Smuzhiyun 	ret = __key_link_lock(keyring, &index_key);
879*4882a593Smuzhiyun 	if (ret < 0) {
880*4882a593Smuzhiyun 		key_ref = ERR_PTR(ret);
881*4882a593Smuzhiyun 		goto error_free_prep;
882*4882a593Smuzhiyun 	}
883*4882a593Smuzhiyun 
884*4882a593Smuzhiyun 	ret = __key_link_begin(keyring, &index_key, &edit);
885*4882a593Smuzhiyun 	if (ret < 0) {
886*4882a593Smuzhiyun 		key_ref = ERR_PTR(ret);
887*4882a593Smuzhiyun 		goto error_link_end;
888*4882a593Smuzhiyun 	}
889*4882a593Smuzhiyun 
890*4882a593Smuzhiyun 	if (restrict_link && restrict_link->check) {
891*4882a593Smuzhiyun 		ret = restrict_link->check(keyring, index_key.type,
892*4882a593Smuzhiyun 					   &prep.payload, restrict_link->key);
893*4882a593Smuzhiyun 		if (ret < 0) {
894*4882a593Smuzhiyun 			key_ref = ERR_PTR(ret);
895*4882a593Smuzhiyun 			goto error_link_end;
896*4882a593Smuzhiyun 		}
897*4882a593Smuzhiyun 	}
898*4882a593Smuzhiyun 
899*4882a593Smuzhiyun 	/* if we're going to allocate a new key, we're going to have
900*4882a593Smuzhiyun 	 * to modify the keyring */
901*4882a593Smuzhiyun 	ret = key_permission(keyring_ref, KEY_NEED_WRITE);
902*4882a593Smuzhiyun 	if (ret < 0) {
903*4882a593Smuzhiyun 		key_ref = ERR_PTR(ret);
904*4882a593Smuzhiyun 		goto error_link_end;
905*4882a593Smuzhiyun 	}
906*4882a593Smuzhiyun 
907*4882a593Smuzhiyun 	/* if it's possible to update this type of key, search for an existing
908*4882a593Smuzhiyun 	 * key of the same type and description in the destination keyring and
909*4882a593Smuzhiyun 	 * update that instead if possible
910*4882a593Smuzhiyun 	 */
911*4882a593Smuzhiyun 	if (index_key.type->update) {
912*4882a593Smuzhiyun 		key_ref = find_key_to_update(keyring_ref, &index_key);
913*4882a593Smuzhiyun 		if (key_ref)
914*4882a593Smuzhiyun 			goto found_matching_key;
915*4882a593Smuzhiyun 	}
916*4882a593Smuzhiyun 
917*4882a593Smuzhiyun 	/* if the client doesn't provide, decide on the permissions we want */
918*4882a593Smuzhiyun 	if (perm == KEY_PERM_UNDEF) {
919*4882a593Smuzhiyun 		perm = KEY_POS_VIEW | KEY_POS_SEARCH | KEY_POS_LINK | KEY_POS_SETATTR;
920*4882a593Smuzhiyun 		perm |= KEY_USR_VIEW;
921*4882a593Smuzhiyun 
922*4882a593Smuzhiyun 		if (index_key.type->read)
923*4882a593Smuzhiyun 			perm |= KEY_POS_READ;
924*4882a593Smuzhiyun 
925*4882a593Smuzhiyun 		if (index_key.type == &key_type_keyring ||
926*4882a593Smuzhiyun 		    index_key.type->update)
927*4882a593Smuzhiyun 			perm |= KEY_POS_WRITE;
928*4882a593Smuzhiyun 	}
929*4882a593Smuzhiyun 
930*4882a593Smuzhiyun 	/* allocate a new key */
931*4882a593Smuzhiyun 	key = key_alloc(index_key.type, index_key.description,
932*4882a593Smuzhiyun 			cred->fsuid, cred->fsgid, cred, perm, flags, NULL);
933*4882a593Smuzhiyun 	if (IS_ERR(key)) {
934*4882a593Smuzhiyun 		key_ref = ERR_CAST(key);
935*4882a593Smuzhiyun 		goto error_link_end;
936*4882a593Smuzhiyun 	}
937*4882a593Smuzhiyun 
938*4882a593Smuzhiyun 	/* instantiate it and link it into the target keyring */
939*4882a593Smuzhiyun 	ret = __key_instantiate_and_link(key, &prep, keyring, NULL, &edit);
940*4882a593Smuzhiyun 	if (ret < 0) {
941*4882a593Smuzhiyun 		key_put(key);
942*4882a593Smuzhiyun 		key_ref = ERR_PTR(ret);
943*4882a593Smuzhiyun 		goto error_link_end;
944*4882a593Smuzhiyun 	}
945*4882a593Smuzhiyun 
946*4882a593Smuzhiyun 	ima_post_key_create_or_update(keyring, key, payload, plen,
947*4882a593Smuzhiyun 				      flags, true);
948*4882a593Smuzhiyun 
949*4882a593Smuzhiyun 	key_ref = make_key_ref(key, is_key_possessed(keyring_ref));
950*4882a593Smuzhiyun 
951*4882a593Smuzhiyun error_link_end:
952*4882a593Smuzhiyun 	__key_link_end(keyring, &index_key, edit);
953*4882a593Smuzhiyun error_free_prep:
954*4882a593Smuzhiyun 	if (index_key.type->preparse)
955*4882a593Smuzhiyun 		index_key.type->free_preparse(&prep);
956*4882a593Smuzhiyun error_put_type:
957*4882a593Smuzhiyun 	key_type_put(index_key.type);
958*4882a593Smuzhiyun error:
959*4882a593Smuzhiyun 	return key_ref;
960*4882a593Smuzhiyun 
961*4882a593Smuzhiyun  found_matching_key:
962*4882a593Smuzhiyun 	/* we found a matching key, so we're going to try to update it
963*4882a593Smuzhiyun 	 * - we can drop the locks first as we have the key pinned
964*4882a593Smuzhiyun 	 */
965*4882a593Smuzhiyun 	__key_link_end(keyring, &index_key, edit);
966*4882a593Smuzhiyun 
967*4882a593Smuzhiyun 	key = key_ref_to_ptr(key_ref);
968*4882a593Smuzhiyun 	if (test_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags)) {
969*4882a593Smuzhiyun 		ret = wait_for_key_construction(key, true);
970*4882a593Smuzhiyun 		if (ret < 0) {
971*4882a593Smuzhiyun 			key_ref_put(key_ref);
972*4882a593Smuzhiyun 			key_ref = ERR_PTR(ret);
973*4882a593Smuzhiyun 			goto error_free_prep;
974*4882a593Smuzhiyun 		}
975*4882a593Smuzhiyun 	}
976*4882a593Smuzhiyun 
977*4882a593Smuzhiyun 	key_ref = __key_update(key_ref, &prep);
978*4882a593Smuzhiyun 
979*4882a593Smuzhiyun 	if (!IS_ERR(key_ref))
980*4882a593Smuzhiyun 		ima_post_key_create_or_update(keyring, key,
981*4882a593Smuzhiyun 					      payload, plen,
982*4882a593Smuzhiyun 					      flags, false);
983*4882a593Smuzhiyun 
984*4882a593Smuzhiyun 	goto error_free_prep;
985*4882a593Smuzhiyun }
986*4882a593Smuzhiyun EXPORT_SYMBOL(key_create_or_update);
987*4882a593Smuzhiyun 
988*4882a593Smuzhiyun /**
989*4882a593Smuzhiyun  * key_update - Update a key's contents.
990*4882a593Smuzhiyun  * @key_ref: The pointer (plus possession flag) to the key.
991*4882a593Smuzhiyun  * @payload: The data to be used to update the key.
992*4882a593Smuzhiyun  * @plen: The length of @payload.
993*4882a593Smuzhiyun  *
994*4882a593Smuzhiyun  * Attempt to update the contents of a key with the given payload data.  The
995*4882a593Smuzhiyun  * caller must be granted Write permission on the key.  Negative keys can be
996*4882a593Smuzhiyun  * instantiated by this method.
997*4882a593Smuzhiyun  *
998*4882a593Smuzhiyun  * Returns 0 on success, -EACCES if not permitted and -EOPNOTSUPP if the key
999*4882a593Smuzhiyun  * type does not support updating.  The key type may return other errors.
1000*4882a593Smuzhiyun  */
key_update(key_ref_t key_ref,const void * payload,size_t plen)1001*4882a593Smuzhiyun int key_update(key_ref_t key_ref, const void *payload, size_t plen)
1002*4882a593Smuzhiyun {
1003*4882a593Smuzhiyun 	struct key_preparsed_payload prep;
1004*4882a593Smuzhiyun 	struct key *key = key_ref_to_ptr(key_ref);
1005*4882a593Smuzhiyun 	int ret;
1006*4882a593Smuzhiyun 
1007*4882a593Smuzhiyun 	key_check(key);
1008*4882a593Smuzhiyun 
1009*4882a593Smuzhiyun 	/* the key must be writable */
1010*4882a593Smuzhiyun 	ret = key_permission(key_ref, KEY_NEED_WRITE);
1011*4882a593Smuzhiyun 	if (ret < 0)
1012*4882a593Smuzhiyun 		return ret;
1013*4882a593Smuzhiyun 
1014*4882a593Smuzhiyun 	/* attempt to update it if supported */
1015*4882a593Smuzhiyun 	if (!key->type->update)
1016*4882a593Smuzhiyun 		return -EOPNOTSUPP;
1017*4882a593Smuzhiyun 
1018*4882a593Smuzhiyun 	memset(&prep, 0, sizeof(prep));
1019*4882a593Smuzhiyun 	prep.data = payload;
1020*4882a593Smuzhiyun 	prep.datalen = plen;
1021*4882a593Smuzhiyun 	prep.quotalen = key->type->def_datalen;
1022*4882a593Smuzhiyun 	prep.expiry = TIME64_MAX;
1023*4882a593Smuzhiyun 	if (key->type->preparse) {
1024*4882a593Smuzhiyun 		ret = key->type->preparse(&prep);
1025*4882a593Smuzhiyun 		if (ret < 0)
1026*4882a593Smuzhiyun 			goto error;
1027*4882a593Smuzhiyun 	}
1028*4882a593Smuzhiyun 
1029*4882a593Smuzhiyun 	down_write(&key->sem);
1030*4882a593Smuzhiyun 
1031*4882a593Smuzhiyun 	ret = key->type->update(key, &prep);
1032*4882a593Smuzhiyun 	if (ret == 0) {
1033*4882a593Smuzhiyun 		/* Updating a negative key positively instantiates it */
1034*4882a593Smuzhiyun 		mark_key_instantiated(key, 0);
1035*4882a593Smuzhiyun 		notify_key(key, NOTIFY_KEY_UPDATED, 0);
1036*4882a593Smuzhiyun 	}
1037*4882a593Smuzhiyun 
1038*4882a593Smuzhiyun 	up_write(&key->sem);
1039*4882a593Smuzhiyun 
1040*4882a593Smuzhiyun error:
1041*4882a593Smuzhiyun 	if (key->type->preparse)
1042*4882a593Smuzhiyun 		key->type->free_preparse(&prep);
1043*4882a593Smuzhiyun 	return ret;
1044*4882a593Smuzhiyun }
1045*4882a593Smuzhiyun EXPORT_SYMBOL(key_update);
1046*4882a593Smuzhiyun 
1047*4882a593Smuzhiyun /**
1048*4882a593Smuzhiyun  * key_revoke - Revoke a key.
1049*4882a593Smuzhiyun  * @key: The key to be revoked.
1050*4882a593Smuzhiyun  *
1051*4882a593Smuzhiyun  * Mark a key as being revoked and ask the type to free up its resources.  The
1052*4882a593Smuzhiyun  * revocation timeout is set and the key and all its links will be
1053*4882a593Smuzhiyun  * automatically garbage collected after key_gc_delay amount of time if they
1054*4882a593Smuzhiyun  * are not manually dealt with first.
1055*4882a593Smuzhiyun  */
key_revoke(struct key * key)1056*4882a593Smuzhiyun void key_revoke(struct key *key)
1057*4882a593Smuzhiyun {
1058*4882a593Smuzhiyun 	time64_t time;
1059*4882a593Smuzhiyun 
1060*4882a593Smuzhiyun 	key_check(key);
1061*4882a593Smuzhiyun 
1062*4882a593Smuzhiyun 	/* make sure no one's trying to change or use the key when we mark it
1063*4882a593Smuzhiyun 	 * - we tell lockdep that we might nest because we might be revoking an
1064*4882a593Smuzhiyun 	 *   authorisation key whilst holding the sem on a key we've just
1065*4882a593Smuzhiyun 	 *   instantiated
1066*4882a593Smuzhiyun 	 */
1067*4882a593Smuzhiyun 	down_write_nested(&key->sem, 1);
1068*4882a593Smuzhiyun 	if (!test_and_set_bit(KEY_FLAG_REVOKED, &key->flags)) {
1069*4882a593Smuzhiyun 		notify_key(key, NOTIFY_KEY_REVOKED, 0);
1070*4882a593Smuzhiyun 		if (key->type->revoke)
1071*4882a593Smuzhiyun 			key->type->revoke(key);
1072*4882a593Smuzhiyun 
1073*4882a593Smuzhiyun 		/* set the death time to no more than the expiry time */
1074*4882a593Smuzhiyun 		time = ktime_get_real_seconds();
1075*4882a593Smuzhiyun 		if (key->revoked_at == 0 || key->revoked_at > time) {
1076*4882a593Smuzhiyun 			key->revoked_at = time;
1077*4882a593Smuzhiyun 			key_schedule_gc(key->revoked_at + key_gc_delay);
1078*4882a593Smuzhiyun 		}
1079*4882a593Smuzhiyun 	}
1080*4882a593Smuzhiyun 
1081*4882a593Smuzhiyun 	up_write(&key->sem);
1082*4882a593Smuzhiyun }
1083*4882a593Smuzhiyun EXPORT_SYMBOL(key_revoke);
1084*4882a593Smuzhiyun 
1085*4882a593Smuzhiyun /**
1086*4882a593Smuzhiyun  * key_invalidate - Invalidate a key.
1087*4882a593Smuzhiyun  * @key: The key to be invalidated.
1088*4882a593Smuzhiyun  *
1089*4882a593Smuzhiyun  * Mark a key as being invalidated and have it cleaned up immediately.  The key
1090*4882a593Smuzhiyun  * is ignored by all searches and other operations from this point.
1091*4882a593Smuzhiyun  */
key_invalidate(struct key * key)1092*4882a593Smuzhiyun void key_invalidate(struct key *key)
1093*4882a593Smuzhiyun {
1094*4882a593Smuzhiyun 	kenter("%d", key_serial(key));
1095*4882a593Smuzhiyun 
1096*4882a593Smuzhiyun 	key_check(key);
1097*4882a593Smuzhiyun 
1098*4882a593Smuzhiyun 	if (!test_bit(KEY_FLAG_INVALIDATED, &key->flags)) {
1099*4882a593Smuzhiyun 		down_write_nested(&key->sem, 1);
1100*4882a593Smuzhiyun 		if (!test_and_set_bit(KEY_FLAG_INVALIDATED, &key->flags)) {
1101*4882a593Smuzhiyun 			notify_key(key, NOTIFY_KEY_INVALIDATED, 0);
1102*4882a593Smuzhiyun 			key_schedule_gc_links();
1103*4882a593Smuzhiyun 		}
1104*4882a593Smuzhiyun 		up_write(&key->sem);
1105*4882a593Smuzhiyun 	}
1106*4882a593Smuzhiyun }
1107*4882a593Smuzhiyun EXPORT_SYMBOL(key_invalidate);
1108*4882a593Smuzhiyun 
1109*4882a593Smuzhiyun /**
1110*4882a593Smuzhiyun  * generic_key_instantiate - Simple instantiation of a key from preparsed data
1111*4882a593Smuzhiyun  * @key: The key to be instantiated
1112*4882a593Smuzhiyun  * @prep: The preparsed data to load.
1113*4882a593Smuzhiyun  *
1114*4882a593Smuzhiyun  * Instantiate a key from preparsed data.  We assume we can just copy the data
1115*4882a593Smuzhiyun  * in directly and clear the old pointers.
1116*4882a593Smuzhiyun  *
1117*4882a593Smuzhiyun  * This can be pointed to directly by the key type instantiate op pointer.
1118*4882a593Smuzhiyun  */
generic_key_instantiate(struct key * key,struct key_preparsed_payload * prep)1119*4882a593Smuzhiyun int generic_key_instantiate(struct key *key, struct key_preparsed_payload *prep)
1120*4882a593Smuzhiyun {
1121*4882a593Smuzhiyun 	int ret;
1122*4882a593Smuzhiyun 
1123*4882a593Smuzhiyun 	pr_devel("==>%s()\n", __func__);
1124*4882a593Smuzhiyun 
1125*4882a593Smuzhiyun 	ret = key_payload_reserve(key, prep->quotalen);
1126*4882a593Smuzhiyun 	if (ret == 0) {
1127*4882a593Smuzhiyun 		rcu_assign_keypointer(key, prep->payload.data[0]);
1128*4882a593Smuzhiyun 		key->payload.data[1] = prep->payload.data[1];
1129*4882a593Smuzhiyun 		key->payload.data[2] = prep->payload.data[2];
1130*4882a593Smuzhiyun 		key->payload.data[3] = prep->payload.data[3];
1131*4882a593Smuzhiyun 		prep->payload.data[0] = NULL;
1132*4882a593Smuzhiyun 		prep->payload.data[1] = NULL;
1133*4882a593Smuzhiyun 		prep->payload.data[2] = NULL;
1134*4882a593Smuzhiyun 		prep->payload.data[3] = NULL;
1135*4882a593Smuzhiyun 	}
1136*4882a593Smuzhiyun 	pr_devel("<==%s() = %d\n", __func__, ret);
1137*4882a593Smuzhiyun 	return ret;
1138*4882a593Smuzhiyun }
1139*4882a593Smuzhiyun EXPORT_SYMBOL(generic_key_instantiate);
1140*4882a593Smuzhiyun 
1141*4882a593Smuzhiyun /**
1142*4882a593Smuzhiyun  * register_key_type - Register a type of key.
1143*4882a593Smuzhiyun  * @ktype: The new key type.
1144*4882a593Smuzhiyun  *
1145*4882a593Smuzhiyun  * Register a new key type.
1146*4882a593Smuzhiyun  *
1147*4882a593Smuzhiyun  * Returns 0 on success or -EEXIST if a type of this name already exists.
1148*4882a593Smuzhiyun  */
register_key_type(struct key_type * ktype)1149*4882a593Smuzhiyun int register_key_type(struct key_type *ktype)
1150*4882a593Smuzhiyun {
1151*4882a593Smuzhiyun 	struct key_type *p;
1152*4882a593Smuzhiyun 	int ret;
1153*4882a593Smuzhiyun 
1154*4882a593Smuzhiyun 	memset(&ktype->lock_class, 0, sizeof(ktype->lock_class));
1155*4882a593Smuzhiyun 
1156*4882a593Smuzhiyun 	ret = -EEXIST;
1157*4882a593Smuzhiyun 	down_write(&key_types_sem);
1158*4882a593Smuzhiyun 
1159*4882a593Smuzhiyun 	/* disallow key types with the same name */
1160*4882a593Smuzhiyun 	list_for_each_entry(p, &key_types_list, link) {
1161*4882a593Smuzhiyun 		if (strcmp(p->name, ktype->name) == 0)
1162*4882a593Smuzhiyun 			goto out;
1163*4882a593Smuzhiyun 	}
1164*4882a593Smuzhiyun 
1165*4882a593Smuzhiyun 	/* store the type */
1166*4882a593Smuzhiyun 	list_add(&ktype->link, &key_types_list);
1167*4882a593Smuzhiyun 
1168*4882a593Smuzhiyun 	pr_notice("Key type %s registered\n", ktype->name);
1169*4882a593Smuzhiyun 	ret = 0;
1170*4882a593Smuzhiyun 
1171*4882a593Smuzhiyun out:
1172*4882a593Smuzhiyun 	up_write(&key_types_sem);
1173*4882a593Smuzhiyun 	return ret;
1174*4882a593Smuzhiyun }
1175*4882a593Smuzhiyun EXPORT_SYMBOL(register_key_type);
1176*4882a593Smuzhiyun 
1177*4882a593Smuzhiyun /**
1178*4882a593Smuzhiyun  * unregister_key_type - Unregister a type of key.
1179*4882a593Smuzhiyun  * @ktype: The key type.
1180*4882a593Smuzhiyun  *
1181*4882a593Smuzhiyun  * Unregister a key type and mark all the extant keys of this type as dead.
1182*4882a593Smuzhiyun  * Those keys of this type are then destroyed to get rid of their payloads and
1183*4882a593Smuzhiyun  * they and their links will be garbage collected as soon as possible.
1184*4882a593Smuzhiyun  */
unregister_key_type(struct key_type * ktype)1185*4882a593Smuzhiyun void unregister_key_type(struct key_type *ktype)
1186*4882a593Smuzhiyun {
1187*4882a593Smuzhiyun 	down_write(&key_types_sem);
1188*4882a593Smuzhiyun 	list_del_init(&ktype->link);
1189*4882a593Smuzhiyun 	downgrade_write(&key_types_sem);
1190*4882a593Smuzhiyun 	key_gc_keytype(ktype);
1191*4882a593Smuzhiyun 	pr_notice("Key type %s unregistered\n", ktype->name);
1192*4882a593Smuzhiyun 	up_read(&key_types_sem);
1193*4882a593Smuzhiyun }
1194*4882a593Smuzhiyun EXPORT_SYMBOL(unregister_key_type);
1195*4882a593Smuzhiyun 
1196*4882a593Smuzhiyun /*
1197*4882a593Smuzhiyun  * Initialise the key management state.
1198*4882a593Smuzhiyun  */
key_init(void)1199*4882a593Smuzhiyun void __init key_init(void)
1200*4882a593Smuzhiyun {
1201*4882a593Smuzhiyun 	/* allocate a slab in which we can store keys */
1202*4882a593Smuzhiyun 	key_jar = kmem_cache_create("key_jar", sizeof(struct key),
1203*4882a593Smuzhiyun 			0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
1204*4882a593Smuzhiyun 
1205*4882a593Smuzhiyun 	/* add the special key types */
1206*4882a593Smuzhiyun 	list_add_tail(&key_type_keyring.link, &key_types_list);
1207*4882a593Smuzhiyun 	list_add_tail(&key_type_dead.link, &key_types_list);
1208*4882a593Smuzhiyun 	list_add_tail(&key_type_user.link, &key_types_list);
1209*4882a593Smuzhiyun 	list_add_tail(&key_type_logon.link, &key_types_list);
1210*4882a593Smuzhiyun 
1211*4882a593Smuzhiyun 	/* record the root user tracking */
1212*4882a593Smuzhiyun 	rb_link_node(&root_key_user.node,
1213*4882a593Smuzhiyun 		     NULL,
1214*4882a593Smuzhiyun 		     &key_user_tree.rb_node);
1215*4882a593Smuzhiyun 
1216*4882a593Smuzhiyun 	rb_insert_color(&root_key_user.node,
1217*4882a593Smuzhiyun 			&key_user_tree);
1218*4882a593Smuzhiyun }
1219