xref: /openbmc/linux/security/keys/keyring.c (revision 233e4735f2a45d9e641c2488b8d7afeb1f377dac)
169664cf1SDavid Howells /* Keyring handling
21da177e4SLinus Torvalds  *
369664cf1SDavid Howells  * Copyright (C) 2004-2005, 2008 Red Hat, Inc. All Rights Reserved.
41da177e4SLinus Torvalds  * Written by David Howells (dhowells@redhat.com)
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or
71da177e4SLinus Torvalds  * modify it under the terms of the GNU General Public License
81da177e4SLinus Torvalds  * as published by the Free Software Foundation; either version
91da177e4SLinus Torvalds  * 2 of the License, or (at your option) any later version.
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds #include <linux/module.h>
131da177e4SLinus Torvalds #include <linux/init.h>
141da177e4SLinus Torvalds #include <linux/sched.h>
151da177e4SLinus Torvalds #include <linux/slab.h>
1629db9190SDavid Howells #include <linux/security.h>
171da177e4SLinus Torvalds #include <linux/seq_file.h>
181da177e4SLinus Torvalds #include <linux/err.h>
19e9e349b0SDavid Howells #include <keys/keyring-type.h>
20512ea3bcSChihau Chau #include <linux/uaccess.h>
211da177e4SLinus Torvalds #include "internal.h"
221da177e4SLinus Torvalds 
23f0641cbaSDavid Howells #define rcu_dereference_locked_keyring(keyring)				\
24f0641cbaSDavid Howells 	(rcu_dereference_protected(					\
25f0641cbaSDavid Howells 		(keyring)->payload.subscriptions,			\
26f0641cbaSDavid Howells 		rwsem_is_locked((struct rw_semaphore *)&(keyring)->sem)))
27f0641cbaSDavid Howells 
28*233e4735SDavid Howells #define rcu_deref_link_locked(klist, index, keyring)			\
29*233e4735SDavid Howells 	(rcu_dereference_protected(					\
30*233e4735SDavid Howells 		(klist)->keys[index],					\
31*233e4735SDavid Howells 		rwsem_is_locked((struct rw_semaphore *)&(keyring)->sem)))
32*233e4735SDavid Howells 
33ceb73c12SDavid Howells #define KEY_LINK_FIXQUOTA 1UL
34ceb73c12SDavid Howells 
351da177e4SLinus Torvalds /*
36973c9f4fSDavid Howells  * When plumbing the depths of the key tree, this sets a hard limit
37973c9f4fSDavid Howells  * set on how deep we're willing to go.
381da177e4SLinus Torvalds  */
391da177e4SLinus Torvalds #define KEYRING_SEARCH_MAX_DEPTH 6
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds /*
42973c9f4fSDavid Howells  * We keep all named keyrings in a hash to speed looking them up.
431da177e4SLinus Torvalds  */
441da177e4SLinus Torvalds #define KEYRING_NAME_HASH_SIZE	(1 << 5)
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds static struct list_head	keyring_name_hash[KEYRING_NAME_HASH_SIZE];
471da177e4SLinus Torvalds static DEFINE_RWLOCK(keyring_name_lock);
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds static inline unsigned keyring_hash(const char *desc)
501da177e4SLinus Torvalds {
511da177e4SLinus Torvalds 	unsigned bucket = 0;
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds 	for (; *desc; desc++)
541da177e4SLinus Torvalds 		bucket += (unsigned char)*desc;
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds 	return bucket & (KEYRING_NAME_HASH_SIZE - 1);
571da177e4SLinus Torvalds }
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds /*
60973c9f4fSDavid Howells  * The keyring key type definition.  Keyrings are simply keys of this type and
61973c9f4fSDavid Howells  * can be treated as ordinary keys in addition to having their own special
62973c9f4fSDavid Howells  * operations.
631da177e4SLinus Torvalds  */
641da177e4SLinus Torvalds static int keyring_instantiate(struct key *keyring,
651da177e4SLinus Torvalds 			       const void *data, size_t datalen);
661da177e4SLinus Torvalds static int keyring_match(const struct key *keyring, const void *criterion);
6731204ed9SDavid Howells static void keyring_revoke(struct key *keyring);
681da177e4SLinus Torvalds static void keyring_destroy(struct key *keyring);
691da177e4SLinus Torvalds static void keyring_describe(const struct key *keyring, struct seq_file *m);
701da177e4SLinus Torvalds static long keyring_read(const struct key *keyring,
711da177e4SLinus Torvalds 			 char __user *buffer, size_t buflen);
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds struct key_type key_type_keyring = {
741da177e4SLinus Torvalds 	.name		= "keyring",
751da177e4SLinus Torvalds 	.def_datalen	= sizeof(struct keyring_list),
761da177e4SLinus Torvalds 	.instantiate	= keyring_instantiate,
771da177e4SLinus Torvalds 	.match		= keyring_match,
7831204ed9SDavid Howells 	.revoke		= keyring_revoke,
791da177e4SLinus Torvalds 	.destroy	= keyring_destroy,
801da177e4SLinus Torvalds 	.describe	= keyring_describe,
811da177e4SLinus Torvalds 	.read		= keyring_read,
821da177e4SLinus Torvalds };
837318226eSDavid Howells EXPORT_SYMBOL(key_type_keyring);
847318226eSDavid Howells 
851da177e4SLinus Torvalds /*
86973c9f4fSDavid Howells  * Semaphore to serialise link/link calls to prevent two link calls in parallel
87973c9f4fSDavid Howells  * introducing a cycle.
881da177e4SLinus Torvalds  */
891ae8f407SAdrian Bunk static DECLARE_RWSEM(keyring_serialise_link_sem);
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds /*
92973c9f4fSDavid Howells  * Publish the name of a keyring so that it can be found by name (if it has
93973c9f4fSDavid Howells  * one).
941da177e4SLinus Torvalds  */
9569664cf1SDavid Howells static void keyring_publish_name(struct key *keyring)
961da177e4SLinus Torvalds {
971da177e4SLinus Torvalds 	int bucket;
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds 	if (keyring->description) {
1001da177e4SLinus Torvalds 		bucket = keyring_hash(keyring->description);
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds 		write_lock(&keyring_name_lock);
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds 		if (!keyring_name_hash[bucket].next)
1051da177e4SLinus Torvalds 			INIT_LIST_HEAD(&keyring_name_hash[bucket]);
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds 		list_add_tail(&keyring->type_data.link,
1081da177e4SLinus Torvalds 			      &keyring_name_hash[bucket]);
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds 		write_unlock(&keyring_name_lock);
1111da177e4SLinus Torvalds 	}
112a8b17ed0SDavid Howells }
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds /*
115973c9f4fSDavid Howells  * Initialise a keyring.
116973c9f4fSDavid Howells  *
117973c9f4fSDavid Howells  * Returns 0 on success, -EINVAL if given any data.
1181da177e4SLinus Torvalds  */
1191da177e4SLinus Torvalds static int keyring_instantiate(struct key *keyring,
1201da177e4SLinus Torvalds 			       const void *data, size_t datalen)
1211da177e4SLinus Torvalds {
1221da177e4SLinus Torvalds 	int ret;
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds 	ret = -EINVAL;
1251da177e4SLinus Torvalds 	if (datalen == 0) {
1261da177e4SLinus Torvalds 		/* make the keyring available by name if it has one */
1271da177e4SLinus Torvalds 		keyring_publish_name(keyring);
1281da177e4SLinus Torvalds 		ret = 0;
1291da177e4SLinus Torvalds 	}
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds 	return ret;
132a8b17ed0SDavid Howells }
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds /*
135973c9f4fSDavid Howells  * Match keyrings on their name
1361da177e4SLinus Torvalds  */
1371da177e4SLinus Torvalds static int keyring_match(const struct key *keyring, const void *description)
1381da177e4SLinus Torvalds {
1391da177e4SLinus Torvalds 	return keyring->description &&
1401da177e4SLinus Torvalds 		strcmp(keyring->description, description) == 0;
141a8b17ed0SDavid Howells }
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds /*
144973c9f4fSDavid Howells  * Clean up a keyring when it is destroyed.  Unpublish its name if it had one
145973c9f4fSDavid Howells  * and dispose of its data.
146*233e4735SDavid Howells  *
147*233e4735SDavid Howells  * The garbage collector detects the final key_put(), removes the keyring from
148*233e4735SDavid Howells  * the serial number tree and then does RCU synchronisation before coming here,
149*233e4735SDavid Howells  * so we shouldn't need to worry about code poking around here with the RCU
150*233e4735SDavid Howells  * readlock held by this time.
1511da177e4SLinus Torvalds  */
1521da177e4SLinus Torvalds static void keyring_destroy(struct key *keyring)
1531da177e4SLinus Torvalds {
1541da177e4SLinus Torvalds 	struct keyring_list *klist;
1551da177e4SLinus Torvalds 	int loop;
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds 	if (keyring->description) {
1581da177e4SLinus Torvalds 		write_lock(&keyring_name_lock);
15994efe72fSDavid Howells 
16094efe72fSDavid Howells 		if (keyring->type_data.link.next != NULL &&
16194efe72fSDavid Howells 		    !list_empty(&keyring->type_data.link))
1621da177e4SLinus Torvalds 			list_del(&keyring->type_data.link);
16394efe72fSDavid Howells 
1641da177e4SLinus Torvalds 		write_unlock(&keyring_name_lock);
1651da177e4SLinus Torvalds 	}
1661da177e4SLinus Torvalds 
167*233e4735SDavid Howells 	klist = rcu_access_pointer(keyring->payload.subscriptions);
1681da177e4SLinus Torvalds 	if (klist) {
1691da177e4SLinus Torvalds 		for (loop = klist->nkeys - 1; loop >= 0; loop--)
170*233e4735SDavid Howells 			key_put(rcu_access_pointer(klist->keys[loop]));
1711da177e4SLinus Torvalds 		kfree(klist);
1721da177e4SLinus Torvalds 	}
173a8b17ed0SDavid Howells }
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds /*
176973c9f4fSDavid Howells  * Describe a keyring for /proc.
1771da177e4SLinus Torvalds  */
1781da177e4SLinus Torvalds static void keyring_describe(const struct key *keyring, struct seq_file *m)
1791da177e4SLinus Torvalds {
1801da177e4SLinus Torvalds 	struct keyring_list *klist;
1811da177e4SLinus Torvalds 
182c8563473Swzt.wzt@gmail.com 	if (keyring->description)
1831da177e4SLinus Torvalds 		seq_puts(m, keyring->description);
184c8563473Swzt.wzt@gmail.com 	else
1851da177e4SLinus Torvalds 		seq_puts(m, "[anon]");
1861da177e4SLinus Torvalds 
18778b7280cSDavid Howells 	if (key_is_instantiated(keyring)) {
18876d8aeabSDavid Howells 		rcu_read_lock();
18976d8aeabSDavid Howells 		klist = rcu_dereference(keyring->payload.subscriptions);
1901da177e4SLinus Torvalds 		if (klist)
1911da177e4SLinus Torvalds 			seq_printf(m, ": %u/%u", klist->nkeys, klist->maxkeys);
1921da177e4SLinus Torvalds 		else
1931da177e4SLinus Torvalds 			seq_puts(m, ": empty");
19476d8aeabSDavid Howells 		rcu_read_unlock();
195a8b17ed0SDavid Howells 	}
19678b7280cSDavid Howells }
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds /*
199973c9f4fSDavid Howells  * Read a list of key IDs from the keyring's contents in binary form
200973c9f4fSDavid Howells  *
201973c9f4fSDavid Howells  * The keyring's semaphore is read-locked by the caller.
2021da177e4SLinus Torvalds  */
2031da177e4SLinus Torvalds static long keyring_read(const struct key *keyring,
2041da177e4SLinus Torvalds 			 char __user *buffer, size_t buflen)
2051da177e4SLinus Torvalds {
2061da177e4SLinus Torvalds 	struct keyring_list *klist;
2071da177e4SLinus Torvalds 	struct key *key;
2081da177e4SLinus Torvalds 	size_t qty, tmp;
2091da177e4SLinus Torvalds 	int loop, ret;
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds 	ret = 0;
212f0641cbaSDavid Howells 	klist = rcu_dereference_locked_keyring(keyring);
2131da177e4SLinus Torvalds 	if (klist) {
2141da177e4SLinus Torvalds 		/* calculate how much data we could return */
2151da177e4SLinus Torvalds 		qty = klist->nkeys * sizeof(key_serial_t);
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 		if (buffer && buflen > 0) {
2181da177e4SLinus Torvalds 			if (buflen > qty)
2191da177e4SLinus Torvalds 				buflen = qty;
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds 			/* copy the IDs of the subscribed keys into the
2221da177e4SLinus Torvalds 			 * buffer */
2231da177e4SLinus Torvalds 			ret = -EFAULT;
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 			for (loop = 0; loop < klist->nkeys; loop++) {
226*233e4735SDavid Howells 				key = rcu_deref_link_locked(klist, loop,
227*233e4735SDavid Howells 							    keyring);
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds 				tmp = sizeof(key_serial_t);
2301da177e4SLinus Torvalds 				if (tmp > buflen)
2311da177e4SLinus Torvalds 					tmp = buflen;
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds 				if (copy_to_user(buffer,
2341da177e4SLinus Torvalds 						 &key->serial,
2351da177e4SLinus Torvalds 						 tmp) != 0)
2361da177e4SLinus Torvalds 					goto error;
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds 				buflen -= tmp;
2391da177e4SLinus Torvalds 				if (buflen == 0)
2401da177e4SLinus Torvalds 					break;
2411da177e4SLinus Torvalds 				buffer += tmp;
2421da177e4SLinus Torvalds 			}
2431da177e4SLinus Torvalds 		}
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds 		ret = qty;
2461da177e4SLinus Torvalds 	}
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds error:
2491da177e4SLinus Torvalds 	return ret;
250a8b17ed0SDavid Howells }
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds /*
253973c9f4fSDavid Howells  * Allocate a keyring and link into the destination keyring.
2541da177e4SLinus Torvalds  */
2551da177e4SLinus Torvalds struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
256d84f4f99SDavid Howells 			  const struct cred *cred, unsigned long flags,
257d720024eSMichael LeMay 			  struct key *dest)
2581da177e4SLinus Torvalds {
2591da177e4SLinus Torvalds 	struct key *keyring;
2601da177e4SLinus Torvalds 	int ret;
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 	keyring = key_alloc(&key_type_keyring, description,
263d84f4f99SDavid Howells 			    uid, gid, cred,
26429db9190SDavid Howells 			    (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
2657e047ef5SDavid Howells 			    flags);
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds 	if (!IS_ERR(keyring)) {
2683e30148cSDavid Howells 		ret = key_instantiate_and_link(keyring, NULL, 0, dest, NULL);
2691da177e4SLinus Torvalds 		if (ret < 0) {
2701da177e4SLinus Torvalds 			key_put(keyring);
2711da177e4SLinus Torvalds 			keyring = ERR_PTR(ret);
2721da177e4SLinus Torvalds 		}
2731da177e4SLinus Torvalds 	}
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 	return keyring;
276a8b17ed0SDavid Howells }
2771da177e4SLinus Torvalds 
278973c9f4fSDavid Howells /**
279973c9f4fSDavid Howells  * keyring_search_aux - Search a keyring tree for a key matching some criteria
280973c9f4fSDavid Howells  * @keyring_ref: A pointer to the keyring with possession indicator.
281973c9f4fSDavid Howells  * @cred: The credentials to use for permissions checks.
282973c9f4fSDavid Howells  * @type: The type of key to search for.
283973c9f4fSDavid Howells  * @description: Parameter for @match.
284973c9f4fSDavid Howells  * @match: Function to rule on whether or not a key is the one required.
28578b7280cSDavid Howells  * @no_state_check: Don't check if a matching key is bad
286973c9f4fSDavid Howells  *
287973c9f4fSDavid Howells  * Search the supplied keyring tree for a key that matches the criteria given.
288973c9f4fSDavid Howells  * The root keyring and any linked keyrings must grant Search permission to the
289973c9f4fSDavid Howells  * caller to be searchable and keys can only be found if they too grant Search
290973c9f4fSDavid Howells  * to the caller. The possession flag on the root keyring pointer controls use
291973c9f4fSDavid Howells  * of the possessor bits in permissions checking of the entire tree.  In
292973c9f4fSDavid Howells  * addition, the LSM gets to forbid keyring searches and key matches.
293973c9f4fSDavid Howells  *
294973c9f4fSDavid Howells  * The search is performed as a breadth-then-depth search up to the prescribed
295973c9f4fSDavid Howells  * limit (KEYRING_SEARCH_MAX_DEPTH).
296973c9f4fSDavid Howells  *
297973c9f4fSDavid Howells  * Keys are matched to the type provided and are then filtered by the match
298973c9f4fSDavid Howells  * function, which is given the description to use in any way it sees fit.  The
299973c9f4fSDavid Howells  * match function may use any attributes of a key that it wishes to to
300973c9f4fSDavid Howells  * determine the match.  Normally the match function from the key type would be
301973c9f4fSDavid Howells  * used.
302973c9f4fSDavid Howells  *
303973c9f4fSDavid Howells  * RCU is used to prevent the keyring key lists from disappearing without the
304973c9f4fSDavid Howells  * need to take lots of locks.
305973c9f4fSDavid Howells  *
306973c9f4fSDavid Howells  * Returns a pointer to the found key and increments the key usage count if
307973c9f4fSDavid Howells  * successful; -EAGAIN if no matching keys were found, or if expired or revoked
308973c9f4fSDavid Howells  * keys were found; -ENOKEY if only negative keys were found; -ENOTDIR if the
309973c9f4fSDavid Howells  * specified keyring wasn't a keyring.
310973c9f4fSDavid Howells  *
311973c9f4fSDavid Howells  * In the case of a successful return, the possession attribute from
312973c9f4fSDavid Howells  * @keyring_ref is propagated to the returned key reference.
3131da177e4SLinus Torvalds  */
314664cceb0SDavid Howells key_ref_t keyring_search_aux(key_ref_t keyring_ref,
315d84f4f99SDavid Howells 			     const struct cred *cred,
3161da177e4SLinus Torvalds 			     struct key_type *type,
3171da177e4SLinus Torvalds 			     const void *description,
31878b7280cSDavid Howells 			     key_match_func_t match,
31978b7280cSDavid Howells 			     bool no_state_check)
3201da177e4SLinus Torvalds {
3211da177e4SLinus Torvalds 	struct {
32276d8aeabSDavid Howells 		struct keyring_list *keylist;
3231da177e4SLinus Torvalds 		int kix;
3241da177e4SLinus Torvalds 	} stack[KEYRING_SEARCH_MAX_DEPTH];
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 	struct keyring_list *keylist;
3271da177e4SLinus Torvalds 	struct timespec now;
328dceba994SKevin Coffman 	unsigned long possessed, kflags;
329664cceb0SDavid Howells 	struct key *keyring, *key;
330664cceb0SDavid Howells 	key_ref_t key_ref;
3311da177e4SLinus Torvalds 	long err;
332efde8b6eSDavid Howells 	int sp, nkeys, kix;
3331da177e4SLinus Torvalds 
334664cceb0SDavid Howells 	keyring = key_ref_to_ptr(keyring_ref);
335664cceb0SDavid Howells 	possessed = is_key_possessed(keyring_ref);
3361da177e4SLinus Torvalds 	key_check(keyring);
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds 	/* top keyring must have search permission to begin the search */
339d84f4f99SDavid Howells 	err = key_task_permission(keyring_ref, cred, KEY_SEARCH);
34029db9190SDavid Howells 	if (err < 0) {
34129db9190SDavid Howells 		key_ref = ERR_PTR(err);
3421da177e4SLinus Torvalds 		goto error;
34329db9190SDavid Howells 	}
3441da177e4SLinus Torvalds 
345664cceb0SDavid Howells 	key_ref = ERR_PTR(-ENOTDIR);
3461da177e4SLinus Torvalds 	if (keyring->type != &key_type_keyring)
3471da177e4SLinus Torvalds 		goto error;
3481da177e4SLinus Torvalds 
349664cceb0SDavid Howells 	rcu_read_lock();
350664cceb0SDavid Howells 
3511da177e4SLinus Torvalds 	now = current_kernel_time();
3521da177e4SLinus Torvalds 	err = -EAGAIN;
3531da177e4SLinus Torvalds 	sp = 0;
3541da177e4SLinus Torvalds 
355dceba994SKevin Coffman 	/* firstly we should check to see if this top-level keyring is what we
356dceba994SKevin Coffman 	 * are looking for */
357dceba994SKevin Coffman 	key_ref = ERR_PTR(-EAGAIN);
358dceba994SKevin Coffman 	kflags = keyring->flags;
359dceba994SKevin Coffman 	if (keyring->type == type && match(keyring, description)) {
360dceba994SKevin Coffman 		key = keyring;
36178b7280cSDavid Howells 		if (no_state_check)
36278b7280cSDavid Howells 			goto found;
363dceba994SKevin Coffman 
364dceba994SKevin Coffman 		/* check it isn't negative and hasn't expired or been
365dceba994SKevin Coffman 		 * revoked */
366dceba994SKevin Coffman 		if (kflags & (1 << KEY_FLAG_REVOKED))
367dceba994SKevin Coffman 			goto error_2;
368dceba994SKevin Coffman 		if (key->expiry && now.tv_sec >= key->expiry)
369dceba994SKevin Coffman 			goto error_2;
370fdd1b945SDavid Howells 		key_ref = ERR_PTR(key->type_data.reject_error);
371dceba994SKevin Coffman 		if (kflags & (1 << KEY_FLAG_NEGATIVE))
372dceba994SKevin Coffman 			goto error_2;
373dceba994SKevin Coffman 		goto found;
374dceba994SKevin Coffman 	}
375dceba994SKevin Coffman 
376dceba994SKevin Coffman 	/* otherwise, the top keyring must not be revoked, expired, or
377dceba994SKevin Coffman 	 * negatively instantiated if we are to search it */
378dceba994SKevin Coffman 	key_ref = ERR_PTR(-EAGAIN);
379dceba994SKevin Coffman 	if (kflags & ((1 << KEY_FLAG_REVOKED) | (1 << KEY_FLAG_NEGATIVE)) ||
380dceba994SKevin Coffman 	    (keyring->expiry && now.tv_sec >= keyring->expiry))
381dceba994SKevin Coffman 		goto error_2;
382dceba994SKevin Coffman 
3831da177e4SLinus Torvalds 	/* start processing a new keyring */
3841da177e4SLinus Torvalds descend:
38576d8aeabSDavid Howells 	if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
3861da177e4SLinus Torvalds 		goto not_this_keyring;
3871da177e4SLinus Torvalds 
38876d8aeabSDavid Howells 	keylist = rcu_dereference(keyring->payload.subscriptions);
3891da177e4SLinus Torvalds 	if (!keylist)
3901da177e4SLinus Torvalds 		goto not_this_keyring;
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds 	/* iterate through the keys in this keyring first */
393efde8b6eSDavid Howells 	nkeys = keylist->nkeys;
394efde8b6eSDavid Howells 	smp_rmb();
395efde8b6eSDavid Howells 	for (kix = 0; kix < nkeys; kix++) {
396*233e4735SDavid Howells 		key = rcu_dereference(keylist->keys[kix]);
397dceba994SKevin Coffman 		kflags = key->flags;
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 		/* ignore keys not of this type */
4001da177e4SLinus Torvalds 		if (key->type != type)
4011da177e4SLinus Torvalds 			continue;
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds 		/* skip revoked keys and expired keys */
40478b7280cSDavid Howells 		if (!no_state_check) {
405dceba994SKevin Coffman 			if (kflags & (1 << KEY_FLAG_REVOKED))
4061da177e4SLinus Torvalds 				continue;
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds 			if (key->expiry && now.tv_sec >= key->expiry)
4091da177e4SLinus Torvalds 				continue;
41078b7280cSDavid Howells 		}
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds 		/* keys that don't match */
4131da177e4SLinus Torvalds 		if (!match(key, description))
4141da177e4SLinus Torvalds 			continue;
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 		/* key must have search permissions */
41729db9190SDavid Howells 		if (key_task_permission(make_key_ref(key, possessed),
418d84f4f99SDavid Howells 					cred, KEY_SEARCH) < 0)
4191da177e4SLinus Torvalds 			continue;
4201da177e4SLinus Torvalds 
42178b7280cSDavid Howells 		if (no_state_check)
42278b7280cSDavid Howells 			goto found;
42378b7280cSDavid Howells 
424dceba994SKevin Coffman 		/* we set a different error code if we pass a negative key */
425dceba994SKevin Coffman 		if (kflags & (1 << KEY_FLAG_NEGATIVE)) {
426fdd1b945SDavid Howells 			err = key->type_data.reject_error;
4271da177e4SLinus Torvalds 			continue;
4281da177e4SLinus Torvalds 		}
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 		goto found;
4311da177e4SLinus Torvalds 	}
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds 	/* search through the keyrings nested in this one */
4341da177e4SLinus Torvalds 	kix = 0;
4351da177e4SLinus Torvalds ascend:
436efde8b6eSDavid Howells 	nkeys = keylist->nkeys;
437efde8b6eSDavid Howells 	smp_rmb();
438efde8b6eSDavid Howells 	for (; kix < nkeys; kix++) {
439*233e4735SDavid Howells 		key = rcu_dereference(keylist->keys[kix]);
4401da177e4SLinus Torvalds 		if (key->type != &key_type_keyring)
44176d8aeabSDavid Howells 			continue;
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds 		/* recursively search nested keyrings
4441da177e4SLinus Torvalds 		 * - only search keyrings for which we have search permission
4451da177e4SLinus Torvalds 		 */
4461da177e4SLinus Torvalds 		if (sp >= KEYRING_SEARCH_MAX_DEPTH)
44776d8aeabSDavid Howells 			continue;
4481da177e4SLinus Torvalds 
4490f6ed7c2SDavid Howells 		if (key_task_permission(make_key_ref(key, possessed),
450d84f4f99SDavid Howells 					cred, KEY_SEARCH) < 0)
45176d8aeabSDavid Howells 			continue;
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds 		/* stack the current position */
45476d8aeabSDavid Howells 		stack[sp].keylist = keylist;
4551da177e4SLinus Torvalds 		stack[sp].kix = kix;
4561da177e4SLinus Torvalds 		sp++;
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds 		/* begin again with the new keyring */
4591da177e4SLinus Torvalds 		keyring = key;
4601da177e4SLinus Torvalds 		goto descend;
4611da177e4SLinus Torvalds 	}
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds 	/* the keyring we're looking at was disqualified or didn't contain a
4641da177e4SLinus Torvalds 	 * matching key */
4651da177e4SLinus Torvalds not_this_keyring:
4661da177e4SLinus Torvalds 	if (sp > 0) {
4671da177e4SLinus Torvalds 		/* resume the processing of a keyring higher up in the tree */
4681da177e4SLinus Torvalds 		sp--;
46976d8aeabSDavid Howells 		keylist = stack[sp].keylist;
4701da177e4SLinus Torvalds 		kix = stack[sp].kix + 1;
4711da177e4SLinus Torvalds 		goto ascend;
4721da177e4SLinus Torvalds 	}
4731da177e4SLinus Torvalds 
474664cceb0SDavid Howells 	key_ref = ERR_PTR(err);
475664cceb0SDavid Howells 	goto error_2;
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds 	/* we found a viable match */
4781da177e4SLinus Torvalds found:
4791da177e4SLinus Torvalds 	atomic_inc(&key->usage);
4801da177e4SLinus Torvalds 	key_check(key);
481664cceb0SDavid Howells 	key_ref = make_key_ref(key, possessed);
482664cceb0SDavid Howells error_2:
48376d8aeabSDavid Howells 	rcu_read_unlock();
484664cceb0SDavid Howells error:
485664cceb0SDavid Howells 	return key_ref;
486a8b17ed0SDavid Howells }
4871da177e4SLinus Torvalds 
488973c9f4fSDavid Howells /**
489973c9f4fSDavid Howells  * keyring_search - Search the supplied keyring tree for a matching key
490973c9f4fSDavid Howells  * @keyring: The root of the keyring tree to be searched.
491973c9f4fSDavid Howells  * @type: The type of keyring we want to find.
492973c9f4fSDavid Howells  * @description: The name of the keyring we want to find.
493973c9f4fSDavid Howells  *
494973c9f4fSDavid Howells  * As keyring_search_aux() above, but using the current task's credentials and
495973c9f4fSDavid Howells  * type's default matching function.
4961da177e4SLinus Torvalds  */
497664cceb0SDavid Howells key_ref_t keyring_search(key_ref_t keyring,
4981da177e4SLinus Torvalds 			 struct key_type *type,
4991da177e4SLinus Torvalds 			 const char *description)
5001da177e4SLinus Torvalds {
5013e30148cSDavid Howells 	if (!type->match)
5023e30148cSDavid Howells 		return ERR_PTR(-ENOKEY);
5033e30148cSDavid Howells 
504d84f4f99SDavid Howells 	return keyring_search_aux(keyring, current->cred,
50578b7280cSDavid Howells 				  type, description, type->match, false);
506a8b17ed0SDavid Howells }
5071da177e4SLinus Torvalds EXPORT_SYMBOL(keyring_search);
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds /*
510973c9f4fSDavid Howells  * Search the given keyring only (no recursion).
511973c9f4fSDavid Howells  *
512973c9f4fSDavid Howells  * The caller must guarantee that the keyring is a keyring and that the
513973c9f4fSDavid Howells  * permission is granted to search the keyring as no check is made here.
514973c9f4fSDavid Howells  *
515973c9f4fSDavid Howells  * RCU is used to make it unnecessary to lock the keyring key list here.
516973c9f4fSDavid Howells  *
517973c9f4fSDavid Howells  * Returns a pointer to the found key with usage count incremented if
518973c9f4fSDavid Howells  * successful and returns -ENOKEY if not found.  Revoked keys and keys not
519973c9f4fSDavid Howells  * providing the requested permission are skipped over.
520973c9f4fSDavid Howells  *
521973c9f4fSDavid Howells  * If successful, the possession indicator is propagated from the keyring ref
522973c9f4fSDavid Howells  * to the returned key reference.
5231da177e4SLinus Torvalds  */
524664cceb0SDavid Howells key_ref_t __keyring_search_one(key_ref_t keyring_ref,
5251da177e4SLinus Torvalds 			       const struct key_type *ktype,
5261da177e4SLinus Torvalds 			       const char *description,
5271da177e4SLinus Torvalds 			       key_perm_t perm)
5281da177e4SLinus Torvalds {
5291da177e4SLinus Torvalds 	struct keyring_list *klist;
530664cceb0SDavid Howells 	unsigned long possessed;
531664cceb0SDavid Howells 	struct key *keyring, *key;
532efde8b6eSDavid Howells 	int nkeys, loop;
5331da177e4SLinus Torvalds 
534664cceb0SDavid Howells 	keyring = key_ref_to_ptr(keyring_ref);
535664cceb0SDavid Howells 	possessed = is_key_possessed(keyring_ref);
536664cceb0SDavid Howells 
53776d8aeabSDavid Howells 	rcu_read_lock();
53876d8aeabSDavid Howells 
53976d8aeabSDavid Howells 	klist = rcu_dereference(keyring->payload.subscriptions);
5401da177e4SLinus Torvalds 	if (klist) {
541efde8b6eSDavid Howells 		nkeys = klist->nkeys;
542efde8b6eSDavid Howells 		smp_rmb();
543efde8b6eSDavid Howells 		for (loop = 0; loop < nkeys ; loop++) {
544*233e4735SDavid Howells 			key = rcu_dereference(klist->keys[loop]);
5451da177e4SLinus Torvalds 			if (key->type == ktype &&
5463e30148cSDavid Howells 			    (!key->type->match ||
5473e30148cSDavid Howells 			     key->type->match(key, description)) &&
548664cceb0SDavid Howells 			    key_permission(make_key_ref(key, possessed),
549db1d1d57SDavid Howells 					   perm) == 0 &&
55076d8aeabSDavid Howells 			    !test_bit(KEY_FLAG_REVOKED, &key->flags)
5511da177e4SLinus Torvalds 			    )
5521da177e4SLinus Torvalds 				goto found;
5531da177e4SLinus Torvalds 		}
5541da177e4SLinus Torvalds 	}
5551da177e4SLinus Torvalds 
556664cceb0SDavid Howells 	rcu_read_unlock();
557664cceb0SDavid Howells 	return ERR_PTR(-ENOKEY);
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds found:
5601da177e4SLinus Torvalds 	atomic_inc(&key->usage);
56176d8aeabSDavid Howells 	rcu_read_unlock();
562664cceb0SDavid Howells 	return make_key_ref(key, possessed);
563a8b17ed0SDavid Howells }
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds /*
566973c9f4fSDavid Howells  * Find a keyring with the specified name.
567973c9f4fSDavid Howells  *
568973c9f4fSDavid Howells  * All named keyrings in the current user namespace are searched, provided they
569973c9f4fSDavid Howells  * grant Search permission directly to the caller (unless this check is
570973c9f4fSDavid Howells  * skipped).  Keyrings whose usage points have reached zero or who have been
571973c9f4fSDavid Howells  * revoked are skipped.
572973c9f4fSDavid Howells  *
573973c9f4fSDavid Howells  * Returns a pointer to the keyring with the keyring's refcount having being
574973c9f4fSDavid Howells  * incremented on success.  -ENOKEY is returned if a key could not be found.
5751da177e4SLinus Torvalds  */
57669664cf1SDavid Howells struct key *find_keyring_by_name(const char *name, bool skip_perm_check)
5771da177e4SLinus Torvalds {
5781da177e4SLinus Torvalds 	struct key *keyring;
5791da177e4SLinus Torvalds 	int bucket;
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds 	if (!name)
582cea7daa3SToshiyuki Okajima 		return ERR_PTR(-EINVAL);
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds 	bucket = keyring_hash(name);
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds 	read_lock(&keyring_name_lock);
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds 	if (keyring_name_hash[bucket].next) {
5891da177e4SLinus Torvalds 		/* search this hash bucket for a keyring with a matching name
5901da177e4SLinus Torvalds 		 * that's readable and that hasn't been revoked */
5911da177e4SLinus Torvalds 		list_for_each_entry(keyring,
5921da177e4SLinus Torvalds 				    &keyring_name_hash[bucket],
5931da177e4SLinus Torvalds 				    type_data.link
5941da177e4SLinus Torvalds 				    ) {
5952ea190d0SSerge E. Hallyn 			if (keyring->user->user_ns != current_user_ns())
5962ea190d0SSerge E. Hallyn 				continue;
5972ea190d0SSerge E. Hallyn 
59876d8aeabSDavid Howells 			if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
5991da177e4SLinus Torvalds 				continue;
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds 			if (strcmp(keyring->description, name) != 0)
6021da177e4SLinus Torvalds 				continue;
6031da177e4SLinus Torvalds 
60469664cf1SDavid Howells 			if (!skip_perm_check &&
60569664cf1SDavid Howells 			    key_permission(make_key_ref(keyring, 0),
60629db9190SDavid Howells 					   KEY_SEARCH) < 0)
6071da177e4SLinus Torvalds 				continue;
6081da177e4SLinus Torvalds 
609cea7daa3SToshiyuki Okajima 			/* we've got a match but we might end up racing with
610cea7daa3SToshiyuki Okajima 			 * key_cleanup() if the keyring is currently 'dead'
611cea7daa3SToshiyuki Okajima 			 * (ie. it has a zero usage count) */
612cea7daa3SToshiyuki Okajima 			if (!atomic_inc_not_zero(&keyring->usage))
613cea7daa3SToshiyuki Okajima 				continue;
614cea7daa3SToshiyuki Okajima 			goto out;
6151da177e4SLinus Torvalds 		}
6161da177e4SLinus Torvalds 	}
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds 	keyring = ERR_PTR(-ENOKEY);
619cea7daa3SToshiyuki Okajima out:
620cea7daa3SToshiyuki Okajima 	read_unlock(&keyring_name_lock);
6211da177e4SLinus Torvalds 	return keyring;
622a8b17ed0SDavid Howells }
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds /*
625973c9f4fSDavid Howells  * See if a cycle will will be created by inserting acyclic tree B in acyclic
626973c9f4fSDavid Howells  * tree A at the topmost level (ie: as a direct child of A).
627973c9f4fSDavid Howells  *
628973c9f4fSDavid Howells  * Since we are adding B to A at the top level, checking for cycles should just
629973c9f4fSDavid Howells  * be a matter of seeing if node A is somewhere in tree B.
6301da177e4SLinus Torvalds  */
6311da177e4SLinus Torvalds static int keyring_detect_cycle(struct key *A, struct key *B)
6321da177e4SLinus Torvalds {
6331da177e4SLinus Torvalds 	struct {
63476d8aeabSDavid Howells 		struct keyring_list *keylist;
6351da177e4SLinus Torvalds 		int kix;
6361da177e4SLinus Torvalds 	} stack[KEYRING_SEARCH_MAX_DEPTH];
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds 	struct keyring_list *keylist;
6391da177e4SLinus Torvalds 	struct key *subtree, *key;
640efde8b6eSDavid Howells 	int sp, nkeys, kix, ret;
6411da177e4SLinus Torvalds 
64276d8aeabSDavid Howells 	rcu_read_lock();
64376d8aeabSDavid Howells 
6441da177e4SLinus Torvalds 	ret = -EDEADLK;
6451da177e4SLinus Torvalds 	if (A == B)
64676d8aeabSDavid Howells 		goto cycle_detected;
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds 	subtree = B;
6491da177e4SLinus Torvalds 	sp = 0;
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds 	/* start processing a new keyring */
6521da177e4SLinus Torvalds descend:
65376d8aeabSDavid Howells 	if (test_bit(KEY_FLAG_REVOKED, &subtree->flags))
6541da177e4SLinus Torvalds 		goto not_this_keyring;
6551da177e4SLinus Torvalds 
65676d8aeabSDavid Howells 	keylist = rcu_dereference(subtree->payload.subscriptions);
6571da177e4SLinus Torvalds 	if (!keylist)
6581da177e4SLinus Torvalds 		goto not_this_keyring;
6591da177e4SLinus Torvalds 	kix = 0;
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds ascend:
6621da177e4SLinus Torvalds 	/* iterate through the remaining keys in this keyring */
663efde8b6eSDavid Howells 	nkeys = keylist->nkeys;
664efde8b6eSDavid Howells 	smp_rmb();
665efde8b6eSDavid Howells 	for (; kix < nkeys; kix++) {
666*233e4735SDavid Howells 		key = rcu_dereference(keylist->keys[kix]);
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds 		if (key == A)
6691da177e4SLinus Torvalds 			goto cycle_detected;
6701da177e4SLinus Torvalds 
6711da177e4SLinus Torvalds 		/* recursively check nested keyrings */
6721da177e4SLinus Torvalds 		if (key->type == &key_type_keyring) {
6731da177e4SLinus Torvalds 			if (sp >= KEYRING_SEARCH_MAX_DEPTH)
6741da177e4SLinus Torvalds 				goto too_deep;
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds 			/* stack the current position */
67776d8aeabSDavid Howells 			stack[sp].keylist = keylist;
6781da177e4SLinus Torvalds 			stack[sp].kix = kix;
6791da177e4SLinus Torvalds 			sp++;
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds 			/* begin again with the new keyring */
6821da177e4SLinus Torvalds 			subtree = key;
6831da177e4SLinus Torvalds 			goto descend;
6841da177e4SLinus Torvalds 		}
6851da177e4SLinus Torvalds 	}
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds 	/* the keyring we're looking at was disqualified or didn't contain a
6881da177e4SLinus Torvalds 	 * matching key */
6891da177e4SLinus Torvalds not_this_keyring:
6901da177e4SLinus Torvalds 	if (sp > 0) {
6911da177e4SLinus Torvalds 		/* resume the checking of a keyring higher up in the tree */
6921da177e4SLinus Torvalds 		sp--;
69376d8aeabSDavid Howells 		keylist = stack[sp].keylist;
6941da177e4SLinus Torvalds 		kix = stack[sp].kix + 1;
6951da177e4SLinus Torvalds 		goto ascend;
6961da177e4SLinus Torvalds 	}
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds 	ret = 0; /* no cycles detected */
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds error:
70176d8aeabSDavid Howells 	rcu_read_unlock();
7021da177e4SLinus Torvalds 	return ret;
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds too_deep:
7051da177e4SLinus Torvalds 	ret = -ELOOP;
70676d8aeabSDavid Howells 	goto error;
70776d8aeabSDavid Howells 
7081da177e4SLinus Torvalds cycle_detected:
7091da177e4SLinus Torvalds 	ret = -EDEADLK;
7101da177e4SLinus Torvalds 	goto error;
711a8b17ed0SDavid Howells }
7121da177e4SLinus Torvalds 
71376d8aeabSDavid Howells /*
714973c9f4fSDavid Howells  * Dispose of a keyring list after the RCU grace period, freeing the unlinked
715cab8eb59SDavid Howells  * key
716cab8eb59SDavid Howells  */
717cab8eb59SDavid Howells static void keyring_unlink_rcu_disposal(struct rcu_head *rcu)
718cab8eb59SDavid Howells {
719cab8eb59SDavid Howells 	struct keyring_list *klist =
720cab8eb59SDavid Howells 		container_of(rcu, struct keyring_list, rcu);
721cab8eb59SDavid Howells 
7224be929beSAlexey Dobriyan 	if (klist->delkey != USHRT_MAX)
723*233e4735SDavid Howells 		key_put(rcu_access_pointer(klist->keys[klist->delkey]));
724cab8eb59SDavid Howells 	kfree(klist);
725f70e2e06SDavid Howells }
726cab8eb59SDavid Howells 
727cab8eb59SDavid Howells /*
728973c9f4fSDavid Howells  * Preallocate memory so that a key can be linked into to a keyring.
7291da177e4SLinus Torvalds  */
730f70e2e06SDavid Howells int __key_link_begin(struct key *keyring, const struct key_type *type,
731ceb73c12SDavid Howells 		     const char *description, unsigned long *_prealloc)
732f70e2e06SDavid Howells 	__acquires(&keyring->sem)
7331da177e4SLinus Torvalds {
7341da177e4SLinus Torvalds 	struct keyring_list *klist, *nklist;
735ceb73c12SDavid Howells 	unsigned long prealloc;
7361da177e4SLinus Torvalds 	unsigned max;
7371da177e4SLinus Torvalds 	size_t size;
738cab8eb59SDavid Howells 	int loop, ret;
7391da177e4SLinus Torvalds 
740f70e2e06SDavid Howells 	kenter("%d,%s,%s,", key_serial(keyring), type->name, description);
741f70e2e06SDavid Howells 
742f70e2e06SDavid Howells 	if (keyring->type != &key_type_keyring)
743f70e2e06SDavid Howells 		return -ENOTDIR;
744f70e2e06SDavid Howells 
745f70e2e06SDavid Howells 	down_write(&keyring->sem);
746f70e2e06SDavid Howells 
7471da177e4SLinus Torvalds 	ret = -EKEYREVOKED;
74876d8aeabSDavid Howells 	if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
749f70e2e06SDavid Howells 		goto error_krsem;
7501da177e4SLinus Torvalds 
751f70e2e06SDavid Howells 	/* serialise link/link calls to prevent parallel calls causing a cycle
752f70e2e06SDavid Howells 	 * when linking two keyring in opposite orders */
753f70e2e06SDavid Howells 	if (type == &key_type_keyring)
7541da177e4SLinus Torvalds 		down_write(&keyring_serialise_link_sem);
7551da177e4SLinus Torvalds 
756f70e2e06SDavid Howells 	klist = rcu_dereference_locked_keyring(keyring);
7571da177e4SLinus Torvalds 
758cab8eb59SDavid Howells 	/* see if there's a matching key we can displace */
759cab8eb59SDavid Howells 	if (klist && klist->nkeys > 0) {
760cab8eb59SDavid Howells 		for (loop = klist->nkeys - 1; loop >= 0; loop--) {
761*233e4735SDavid Howells 			struct key *key = rcu_deref_link_locked(klist, loop,
762*233e4735SDavid Howells 								keyring);
763*233e4735SDavid Howells 			if (key->type == type &&
764*233e4735SDavid Howells 			    strcmp(key->description, description) == 0) {
765*233e4735SDavid Howells 				/* Found a match - we'll replace the link with
766*233e4735SDavid Howells 				 * one to the new key.  We record the slot
767*233e4735SDavid Howells 				 * position.
768*233e4735SDavid Howells 				 */
769*233e4735SDavid Howells 				klist->delkey = loop;
770*233e4735SDavid Howells 				prealloc = 0;
771cab8eb59SDavid Howells 				goto done;
772cab8eb59SDavid Howells 			}
773cab8eb59SDavid Howells 		}
774cab8eb59SDavid Howells 	}
775cab8eb59SDavid Howells 
7761da177e4SLinus Torvalds 	/* check that we aren't going to overrun the user's quota */
7771da177e4SLinus Torvalds 	ret = key_payload_reserve(keyring,
7781da177e4SLinus Torvalds 				  keyring->datalen + KEYQUOTA_LINK_BYTES);
7791da177e4SLinus Torvalds 	if (ret < 0)
780f70e2e06SDavid Howells 		goto error_sem;
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds 	if (klist && klist->nkeys < klist->maxkeys) {
783f70e2e06SDavid Howells 		/* there's sufficient slack space to append directly */
784*233e4735SDavid Howells 		klist->delkey = klist->nkeys;
785ceb73c12SDavid Howells 		prealloc = KEY_LINK_FIXQUOTA;
786512ea3bcSChihau Chau 	} else {
7871da177e4SLinus Torvalds 		/* grow the key list */
7881da177e4SLinus Torvalds 		max = 4;
7891da177e4SLinus Torvalds 		if (klist)
7901da177e4SLinus Torvalds 			max += klist->maxkeys;
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds 		ret = -ENFILE;
7934be929beSAlexey Dobriyan 		if (max > USHRT_MAX - 1)
794f70e2e06SDavid Howells 			goto error_quota;
795a4014d8fSDavid Howells 		size = sizeof(*klist) + sizeof(struct key *) * max;
7961da177e4SLinus Torvalds 		if (size > PAGE_SIZE)
797f70e2e06SDavid Howells 			goto error_quota;
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds 		ret = -ENOMEM;
8001da177e4SLinus Torvalds 		nklist = kmalloc(size, GFP_KERNEL);
8011da177e4SLinus Torvalds 		if (!nklist)
802f70e2e06SDavid Howells 			goto error_quota;
8031da177e4SLinus Torvalds 
804f70e2e06SDavid Howells 		nklist->maxkeys = max;
8051da177e4SLinus Torvalds 		if (klist) {
806f70e2e06SDavid Howells 			memcpy(nklist->keys, klist->keys,
8071da177e4SLinus Torvalds 			       sizeof(struct key *) * klist->nkeys);
808f70e2e06SDavid Howells 			nklist->delkey = klist->nkeys;
809f70e2e06SDavid Howells 			nklist->nkeys = klist->nkeys + 1;
8104be929beSAlexey Dobriyan 			klist->delkey = USHRT_MAX;
811f70e2e06SDavid Howells 		} else {
812f70e2e06SDavid Howells 			nklist->nkeys = 1;
813f70e2e06SDavid Howells 			nklist->delkey = 0;
8141da177e4SLinus Torvalds 		}
8151da177e4SLinus Torvalds 
8161da177e4SLinus Torvalds 		/* add the key into the new space */
817*233e4735SDavid Howells 		RCU_INIT_POINTER(nklist->keys[nklist->delkey], NULL);
818*233e4735SDavid Howells 		prealloc = (unsigned long)nklist | KEY_LINK_FIXQUOTA;
8191da177e4SLinus Torvalds 	}
8201da177e4SLinus Torvalds 
821cab8eb59SDavid Howells done:
822ceb73c12SDavid Howells 	*_prealloc = prealloc;
823f70e2e06SDavid Howells 	kleave(" = 0");
824f70e2e06SDavid Howells 	return 0;
8251da177e4SLinus Torvalds 
826f70e2e06SDavid Howells error_quota:
8271da177e4SLinus Torvalds 	/* undo the quota changes */
8281da177e4SLinus Torvalds 	key_payload_reserve(keyring,
8291da177e4SLinus Torvalds 			    keyring->datalen - KEYQUOTA_LINK_BYTES);
830f70e2e06SDavid Howells error_sem:
831f70e2e06SDavid Howells 	if (type == &key_type_keyring)
832f70e2e06SDavid Howells 		up_write(&keyring_serialise_link_sem);
833f70e2e06SDavid Howells error_krsem:
834f70e2e06SDavid Howells 	up_write(&keyring->sem);
835f70e2e06SDavid Howells 	kleave(" = %d", ret);
836f70e2e06SDavid Howells 	return ret;
837f70e2e06SDavid Howells }
8381da177e4SLinus Torvalds 
839f70e2e06SDavid Howells /*
840973c9f4fSDavid Howells  * Check already instantiated keys aren't going to be a problem.
841973c9f4fSDavid Howells  *
842973c9f4fSDavid Howells  * The caller must have called __key_link_begin(). Don't need to call this for
843973c9f4fSDavid Howells  * keys that were created since __key_link_begin() was called.
844f70e2e06SDavid Howells  */
845f70e2e06SDavid Howells int __key_link_check_live_key(struct key *keyring, struct key *key)
846f70e2e06SDavid Howells {
847f70e2e06SDavid Howells 	if (key->type == &key_type_keyring)
848f70e2e06SDavid Howells 		/* check that we aren't going to create a cycle by linking one
849f70e2e06SDavid Howells 		 * keyring to another */
850f70e2e06SDavid Howells 		return keyring_detect_cycle(keyring, key);
851f70e2e06SDavid Howells 	return 0;
852f70e2e06SDavid Howells }
8531da177e4SLinus Torvalds 
854f70e2e06SDavid Howells /*
855973c9f4fSDavid Howells  * Link a key into to a keyring.
856973c9f4fSDavid Howells  *
857973c9f4fSDavid Howells  * Must be called with __key_link_begin() having being called.  Discards any
858973c9f4fSDavid Howells  * already extant link to matching key if there is one, so that each keyring
859973c9f4fSDavid Howells  * holds at most one link to any given key of a particular type+description
860973c9f4fSDavid Howells  * combination.
861f70e2e06SDavid Howells  */
862f70e2e06SDavid Howells void __key_link(struct key *keyring, struct key *key,
863ceb73c12SDavid Howells 		unsigned long *_prealloc)
864f70e2e06SDavid Howells {
865f70e2e06SDavid Howells 	struct keyring_list *klist, *nklist;
866*233e4735SDavid Howells 	struct key *discard;
867f70e2e06SDavid Howells 
868ceb73c12SDavid Howells 	nklist = (struct keyring_list *)(*_prealloc & ~KEY_LINK_FIXQUOTA);
869ceb73c12SDavid Howells 	*_prealloc = 0;
870f70e2e06SDavid Howells 
871f70e2e06SDavid Howells 	kenter("%d,%d,%p", keyring->serial, key->serial, nklist);
872f70e2e06SDavid Howells 
8736d528b08SDavid Howells 	klist = rcu_dereference_locked_keyring(keyring);
874f70e2e06SDavid Howells 
875f70e2e06SDavid Howells 	atomic_inc(&key->usage);
876f70e2e06SDavid Howells 
877f70e2e06SDavid Howells 	/* there's a matching key we can displace or an empty slot in a newly
878f70e2e06SDavid Howells 	 * allocated list we can fill */
879f70e2e06SDavid Howells 	if (nklist) {
880*233e4735SDavid Howells 		kdebug("reissue %hu/%hu/%hu",
881f70e2e06SDavid Howells 		       nklist->delkey, nklist->nkeys, nklist->maxkeys);
882f70e2e06SDavid Howells 
883*233e4735SDavid Howells 		RCU_INIT_POINTER(nklist->keys[nklist->delkey], key);
884f70e2e06SDavid Howells 
885f70e2e06SDavid Howells 		rcu_assign_pointer(keyring->payload.subscriptions, nklist);
886f70e2e06SDavid Howells 
887f70e2e06SDavid Howells 		/* dispose of the old keyring list and, if there was one, the
888f70e2e06SDavid Howells 		 * displaced key */
889f70e2e06SDavid Howells 		if (klist) {
890f70e2e06SDavid Howells 			kdebug("dispose %hu/%hu/%hu",
891f70e2e06SDavid Howells 			       klist->delkey, klist->nkeys, klist->maxkeys);
892f70e2e06SDavid Howells 			call_rcu(&klist->rcu, keyring_unlink_rcu_disposal);
893f70e2e06SDavid Howells 		}
894*233e4735SDavid Howells 	} else if (klist->delkey < klist->nkeys) {
895*233e4735SDavid Howells 		kdebug("replace %hu/%hu/%hu",
896*233e4735SDavid Howells 		       klist->delkey, klist->nkeys, klist->maxkeys);
897*233e4735SDavid Howells 
898*233e4735SDavid Howells 		discard = rcu_dereference_protected(
899*233e4735SDavid Howells 			klist->keys[klist->delkey],
900*233e4735SDavid Howells 			rwsem_is_locked(&keyring->sem));
901*233e4735SDavid Howells 		rcu_assign_pointer(klist->keys[klist->delkey], key);
902*233e4735SDavid Howells 		/* The garbage collector will take care of RCU
903*233e4735SDavid Howells 		 * synchronisation */
904*233e4735SDavid Howells 		key_put(discard);
905f70e2e06SDavid Howells 	} else {
906f70e2e06SDavid Howells 		/* there's sufficient slack space to append directly */
907*233e4735SDavid Howells 		kdebug("append %hu/%hu/%hu",
908*233e4735SDavid Howells 		       klist->delkey, klist->nkeys, klist->maxkeys);
909*233e4735SDavid Howells 
910*233e4735SDavid Howells 		RCU_INIT_POINTER(klist->keys[klist->delkey], key);
911f70e2e06SDavid Howells 		smp_wmb();
912f70e2e06SDavid Howells 		klist->nkeys++;
913f70e2e06SDavid Howells 	}
914f70e2e06SDavid Howells }
915f70e2e06SDavid Howells 
916f70e2e06SDavid Howells /*
917973c9f4fSDavid Howells  * Finish linking a key into to a keyring.
918973c9f4fSDavid Howells  *
919973c9f4fSDavid Howells  * Must be called with __key_link_begin() having being called.
920f70e2e06SDavid Howells  */
921f70e2e06SDavid Howells void __key_link_end(struct key *keyring, struct key_type *type,
922ceb73c12SDavid Howells 		    unsigned long prealloc)
923f70e2e06SDavid Howells 	__releases(&keyring->sem)
924f70e2e06SDavid Howells {
925f70e2e06SDavid Howells 	BUG_ON(type == NULL);
926f70e2e06SDavid Howells 	BUG_ON(type->name == NULL);
927ceb73c12SDavid Howells 	kenter("%d,%s,%lx", keyring->serial, type->name, prealloc);
928f70e2e06SDavid Howells 
929f70e2e06SDavid Howells 	if (type == &key_type_keyring)
930f70e2e06SDavid Howells 		up_write(&keyring_serialise_link_sem);
931f70e2e06SDavid Howells 
932f70e2e06SDavid Howells 	if (prealloc) {
933ceb73c12SDavid Howells 		if (prealloc & KEY_LINK_FIXQUOTA)
934f70e2e06SDavid Howells 			key_payload_reserve(keyring,
935ceb73c12SDavid Howells 					    keyring->datalen -
936ceb73c12SDavid Howells 					    KEYQUOTA_LINK_BYTES);
937ceb73c12SDavid Howells 		kfree((struct keyring_list *)(prealloc & ~KEY_LINK_FIXQUOTA));
938f70e2e06SDavid Howells 	}
939f70e2e06SDavid Howells 	up_write(&keyring->sem);
940f70e2e06SDavid Howells }
941f70e2e06SDavid Howells 
942973c9f4fSDavid Howells /**
943973c9f4fSDavid Howells  * key_link - Link a key to a keyring
944973c9f4fSDavid Howells  * @keyring: The keyring to make the link in.
945973c9f4fSDavid Howells  * @key: The key to link to.
946973c9f4fSDavid Howells  *
947973c9f4fSDavid Howells  * Make a link in a keyring to a key, such that the keyring holds a reference
948973c9f4fSDavid Howells  * on that key and the key can potentially be found by searching that keyring.
949973c9f4fSDavid Howells  *
950973c9f4fSDavid Howells  * This function will write-lock the keyring's semaphore and will consume some
951973c9f4fSDavid Howells  * of the user's key data quota to hold the link.
952973c9f4fSDavid Howells  *
953973c9f4fSDavid Howells  * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring,
954973c9f4fSDavid Howells  * -EKEYREVOKED if the keyring has been revoked, -ENFILE if the keyring is
955973c9f4fSDavid Howells  * full, -EDQUOT if there is insufficient key data quota remaining to add
956973c9f4fSDavid Howells  * another link or -ENOMEM if there's insufficient memory.
957973c9f4fSDavid Howells  *
958973c9f4fSDavid Howells  * It is assumed that the caller has checked that it is permitted for a link to
959973c9f4fSDavid Howells  * be made (the keyring should have Write permission and the key Link
960973c9f4fSDavid Howells  * permission).
9611da177e4SLinus Torvalds  */
9621da177e4SLinus Torvalds int key_link(struct key *keyring, struct key *key)
9631da177e4SLinus Torvalds {
964ceb73c12SDavid Howells 	unsigned long prealloc;
9651da177e4SLinus Torvalds 	int ret;
9661da177e4SLinus Torvalds 
9671da177e4SLinus Torvalds 	key_check(keyring);
9681da177e4SLinus Torvalds 	key_check(key);
9691da177e4SLinus Torvalds 
970f70e2e06SDavid Howells 	ret = __key_link_begin(keyring, key->type, key->description, &prealloc);
971f70e2e06SDavid Howells 	if (ret == 0) {
972f70e2e06SDavid Howells 		ret = __key_link_check_live_key(keyring, key);
973f70e2e06SDavid Howells 		if (ret == 0)
974f70e2e06SDavid Howells 			__key_link(keyring, key, &prealloc);
975f70e2e06SDavid Howells 		__key_link_end(keyring, key->type, prealloc);
976f70e2e06SDavid Howells 	}
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds 	return ret;
979f70e2e06SDavid Howells }
9801da177e4SLinus Torvalds EXPORT_SYMBOL(key_link);
9811da177e4SLinus Torvalds 
982973c9f4fSDavid Howells /**
983973c9f4fSDavid Howells  * key_unlink - Unlink the first link to a key from a keyring.
984973c9f4fSDavid Howells  * @keyring: The keyring to remove the link from.
985973c9f4fSDavid Howells  * @key: The key the link is to.
986973c9f4fSDavid Howells  *
987973c9f4fSDavid Howells  * Remove a link from a keyring to a key.
988973c9f4fSDavid Howells  *
989973c9f4fSDavid Howells  * This function will write-lock the keyring's semaphore.
990973c9f4fSDavid Howells  *
991973c9f4fSDavid Howells  * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring, -ENOENT if
992973c9f4fSDavid Howells  * the key isn't linked to by the keyring or -ENOMEM if there's insufficient
993973c9f4fSDavid Howells  * memory.
994973c9f4fSDavid Howells  *
995973c9f4fSDavid Howells  * It is assumed that the caller has checked that it is permitted for a link to
996973c9f4fSDavid Howells  * be removed (the keyring should have Write permission; no permissions are
997973c9f4fSDavid Howells  * required on the key).
9981da177e4SLinus Torvalds  */
9991da177e4SLinus Torvalds int key_unlink(struct key *keyring, struct key *key)
10001da177e4SLinus Torvalds {
100176d8aeabSDavid Howells 	struct keyring_list *klist, *nklist;
10021da177e4SLinus Torvalds 	int loop, ret;
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds 	key_check(keyring);
10051da177e4SLinus Torvalds 	key_check(key);
10061da177e4SLinus Torvalds 
10071da177e4SLinus Torvalds 	ret = -ENOTDIR;
10081da177e4SLinus Torvalds 	if (keyring->type != &key_type_keyring)
10091da177e4SLinus Torvalds 		goto error;
10101da177e4SLinus Torvalds 
10111da177e4SLinus Torvalds 	down_write(&keyring->sem);
10121da177e4SLinus Torvalds 
1013f0641cbaSDavid Howells 	klist = rcu_dereference_locked_keyring(keyring);
10141da177e4SLinus Torvalds 	if (klist) {
10151da177e4SLinus Torvalds 		/* search the keyring for the key */
10161da177e4SLinus Torvalds 		for (loop = 0; loop < klist->nkeys; loop++)
1017*233e4735SDavid Howells 			if (rcu_access_pointer(klist->keys[loop]) == key)
10181da177e4SLinus Torvalds 				goto key_is_present;
10191da177e4SLinus Torvalds 	}
10201da177e4SLinus Torvalds 
10211da177e4SLinus Torvalds 	up_write(&keyring->sem);
10221da177e4SLinus Torvalds 	ret = -ENOENT;
10231da177e4SLinus Torvalds 	goto error;
10241da177e4SLinus Torvalds 
10251da177e4SLinus Torvalds key_is_present:
102676d8aeabSDavid Howells 	/* we need to copy the key list for RCU purposes */
1027a4014d8fSDavid Howells 	nklist = kmalloc(sizeof(*klist) +
1028a4014d8fSDavid Howells 			 sizeof(struct key *) * klist->maxkeys,
102976d8aeabSDavid Howells 			 GFP_KERNEL);
103076d8aeabSDavid Howells 	if (!nklist)
103176d8aeabSDavid Howells 		goto nomem;
103276d8aeabSDavid Howells 	nklist->maxkeys = klist->maxkeys;
103376d8aeabSDavid Howells 	nklist->nkeys = klist->nkeys - 1;
103476d8aeabSDavid Howells 
103576d8aeabSDavid Howells 	if (loop > 0)
103676d8aeabSDavid Howells 		memcpy(&nklist->keys[0],
103776d8aeabSDavid Howells 		       &klist->keys[0],
1038a4014d8fSDavid Howells 		       loop * sizeof(struct key *));
103976d8aeabSDavid Howells 
104076d8aeabSDavid Howells 	if (loop < nklist->nkeys)
104176d8aeabSDavid Howells 		memcpy(&nklist->keys[loop],
104276d8aeabSDavid Howells 		       &klist->keys[loop + 1],
1043a4014d8fSDavid Howells 		       (nklist->nkeys - loop) * sizeof(struct key *));
104476d8aeabSDavid Howells 
10451da177e4SLinus Torvalds 	/* adjust the user's quota */
10461da177e4SLinus Torvalds 	key_payload_reserve(keyring,
10471da177e4SLinus Torvalds 			    keyring->datalen - KEYQUOTA_LINK_BYTES);
10481da177e4SLinus Torvalds 
104976d8aeabSDavid Howells 	rcu_assign_pointer(keyring->payload.subscriptions, nklist);
10501da177e4SLinus Torvalds 
10511da177e4SLinus Torvalds 	up_write(&keyring->sem);
105276d8aeabSDavid Howells 
105376d8aeabSDavid Howells 	/* schedule for later cleanup */
105476d8aeabSDavid Howells 	klist->delkey = loop;
105576d8aeabSDavid Howells 	call_rcu(&klist->rcu, keyring_unlink_rcu_disposal);
105676d8aeabSDavid Howells 
10571da177e4SLinus Torvalds 	ret = 0;
10581da177e4SLinus Torvalds 
10591da177e4SLinus Torvalds error:
10601da177e4SLinus Torvalds 	return ret;
106176d8aeabSDavid Howells nomem:
106276d8aeabSDavid Howells 	ret = -ENOMEM;
106376d8aeabSDavid Howells 	up_write(&keyring->sem);
106476d8aeabSDavid Howells 	goto error;
1065a8b17ed0SDavid Howells }
10661da177e4SLinus Torvalds EXPORT_SYMBOL(key_unlink);
10671da177e4SLinus Torvalds 
10681da177e4SLinus Torvalds /*
1069973c9f4fSDavid Howells  * Dispose of a keyring list after the RCU grace period, releasing the keys it
1070973c9f4fSDavid Howells  * links to.
107176d8aeabSDavid Howells  */
107276d8aeabSDavid Howells static void keyring_clear_rcu_disposal(struct rcu_head *rcu)
107376d8aeabSDavid Howells {
107476d8aeabSDavid Howells 	struct keyring_list *klist;
107576d8aeabSDavid Howells 	int loop;
107676d8aeabSDavid Howells 
107776d8aeabSDavid Howells 	klist = container_of(rcu, struct keyring_list, rcu);
107876d8aeabSDavid Howells 
107976d8aeabSDavid Howells 	for (loop = klist->nkeys - 1; loop >= 0; loop--)
1080*233e4735SDavid Howells 		key_put(rcu_access_pointer(klist->keys[loop]));
108176d8aeabSDavid Howells 
108276d8aeabSDavid Howells 	kfree(klist);
1083a8b17ed0SDavid Howells }
108476d8aeabSDavid Howells 
1085973c9f4fSDavid Howells /**
1086973c9f4fSDavid Howells  * keyring_clear - Clear a keyring
1087973c9f4fSDavid Howells  * @keyring: The keyring to clear.
1088973c9f4fSDavid Howells  *
1089973c9f4fSDavid Howells  * Clear the contents of the specified keyring.
1090973c9f4fSDavid Howells  *
1091973c9f4fSDavid Howells  * Returns 0 if successful or -ENOTDIR if the keyring isn't a keyring.
10921da177e4SLinus Torvalds  */
10931da177e4SLinus Torvalds int keyring_clear(struct key *keyring)
10941da177e4SLinus Torvalds {
10951da177e4SLinus Torvalds 	struct keyring_list *klist;
109676d8aeabSDavid Howells 	int ret;
10971da177e4SLinus Torvalds 
10981da177e4SLinus Torvalds 	ret = -ENOTDIR;
10991da177e4SLinus Torvalds 	if (keyring->type == &key_type_keyring) {
11001da177e4SLinus Torvalds 		/* detach the pointer block with the locks held */
11011da177e4SLinus Torvalds 		down_write(&keyring->sem);
11021da177e4SLinus Torvalds 
1103f0641cbaSDavid Howells 		klist = rcu_dereference_locked_keyring(keyring);
11041da177e4SLinus Torvalds 		if (klist) {
11051da177e4SLinus Torvalds 			/* adjust the quota */
11061da177e4SLinus Torvalds 			key_payload_reserve(keyring,
11071da177e4SLinus Torvalds 					    sizeof(struct keyring_list));
11081da177e4SLinus Torvalds 
110976d8aeabSDavid Howells 			rcu_assign_pointer(keyring->payload.subscriptions,
111076d8aeabSDavid Howells 					   NULL);
11111da177e4SLinus Torvalds 		}
11121da177e4SLinus Torvalds 
11131da177e4SLinus Torvalds 		up_write(&keyring->sem);
11141da177e4SLinus Torvalds 
11151da177e4SLinus Torvalds 		/* free the keys after the locks have been dropped */
111676d8aeabSDavid Howells 		if (klist)
111776d8aeabSDavid Howells 			call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
11181da177e4SLinus Torvalds 
11191da177e4SLinus Torvalds 		ret = 0;
11201da177e4SLinus Torvalds 	}
11211da177e4SLinus Torvalds 
11221da177e4SLinus Torvalds 	return ret;
1123a8b17ed0SDavid Howells }
11241da177e4SLinus Torvalds EXPORT_SYMBOL(keyring_clear);
112531204ed9SDavid Howells 
112631204ed9SDavid Howells /*
1127973c9f4fSDavid Howells  * Dispose of the links from a revoked keyring.
1128973c9f4fSDavid Howells  *
1129973c9f4fSDavid Howells  * This is called with the key sem write-locked.
113031204ed9SDavid Howells  */
113131204ed9SDavid Howells static void keyring_revoke(struct key *keyring)
113231204ed9SDavid Howells {
1133f0641cbaSDavid Howells 	struct keyring_list *klist;
1134f0641cbaSDavid Howells 
1135f0641cbaSDavid Howells 	klist = rcu_dereference_locked_keyring(keyring);
113631204ed9SDavid Howells 
113731204ed9SDavid Howells 	/* adjust the quota */
113831204ed9SDavid Howells 	key_payload_reserve(keyring, 0);
113931204ed9SDavid Howells 
114031204ed9SDavid Howells 	if (klist) {
114131204ed9SDavid Howells 		rcu_assign_pointer(keyring->payload.subscriptions, NULL);
114231204ed9SDavid Howells 		call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
114331204ed9SDavid Howells 	}
1144a8b17ed0SDavid Howells }
11455d135440SDavid Howells 
11465d135440SDavid Howells /*
1147973c9f4fSDavid Howells  * Determine whether a key is dead.
11485d135440SDavid Howells  */
11495d135440SDavid Howells static bool key_is_dead(struct key *key, time_t limit)
11505d135440SDavid Howells {
11515d135440SDavid Howells 	return test_bit(KEY_FLAG_DEAD, &key->flags) ||
11525d135440SDavid Howells 		(key->expiry > 0 && key->expiry <= limit);
11535d135440SDavid Howells }
11545d135440SDavid Howells 
11555d135440SDavid Howells /*
1156973c9f4fSDavid Howells  * Collect garbage from the contents of a keyring, replacing the old list with
1157973c9f4fSDavid Howells  * a new one with the pointers all shuffled down.
1158973c9f4fSDavid Howells  *
1159973c9f4fSDavid Howells  * Dead keys are classed as oned that are flagged as being dead or are revoked,
1160973c9f4fSDavid Howells  * expired or negative keys that were revoked or expired before the specified
1161973c9f4fSDavid Howells  * limit.
11625d135440SDavid Howells  */
11635d135440SDavid Howells void keyring_gc(struct key *keyring, time_t limit)
11645d135440SDavid Howells {
11655d135440SDavid Howells 	struct keyring_list *klist, *new;
11665d135440SDavid Howells 	struct key *key;
11675d135440SDavid Howells 	int loop, keep, max;
11685d135440SDavid Howells 
1169c08ef808SDavid Howells 	kenter("{%x,%s}", key_serial(keyring), keyring->description);
11705d135440SDavid Howells 
11715d135440SDavid Howells 	down_write(&keyring->sem);
11725d135440SDavid Howells 
1173f0641cbaSDavid Howells 	klist = rcu_dereference_locked_keyring(keyring);
11745d135440SDavid Howells 	if (!klist)
1175c08ef808SDavid Howells 		goto no_klist;
11765d135440SDavid Howells 
11775d135440SDavid Howells 	/* work out how many subscriptions we're keeping */
11785d135440SDavid Howells 	keep = 0;
11795d135440SDavid Howells 	for (loop = klist->nkeys - 1; loop >= 0; loop--)
1180*233e4735SDavid Howells 		if (!key_is_dead(rcu_deref_link_locked(klist, loop, keyring),
1181*233e4735SDavid Howells 				 limit))
11825d135440SDavid Howells 			keep++;
11835d135440SDavid Howells 
11845d135440SDavid Howells 	if (keep == klist->nkeys)
11855d135440SDavid Howells 		goto just_return;
11865d135440SDavid Howells 
11875d135440SDavid Howells 	/* allocate a new keyring payload */
11885d135440SDavid Howells 	max = roundup(keep, 4);
11895d135440SDavid Howells 	new = kmalloc(sizeof(struct keyring_list) + max * sizeof(struct key *),
11905d135440SDavid Howells 		      GFP_KERNEL);
11915d135440SDavid Howells 	if (!new)
1192c08ef808SDavid Howells 		goto nomem;
11935d135440SDavid Howells 	new->maxkeys = max;
11945d135440SDavid Howells 	new->nkeys = 0;
11955d135440SDavid Howells 	new->delkey = 0;
11965d135440SDavid Howells 
11975d135440SDavid Howells 	/* install the live keys
11985d135440SDavid Howells 	 * - must take care as expired keys may be updated back to life
11995d135440SDavid Howells 	 */
12005d135440SDavid Howells 	keep = 0;
12015d135440SDavid Howells 	for (loop = klist->nkeys - 1; loop >= 0; loop--) {
1202*233e4735SDavid Howells 		key = rcu_deref_link_locked(klist, loop, keyring);
12035d135440SDavid Howells 		if (!key_is_dead(key, limit)) {
12045d135440SDavid Howells 			if (keep >= max)
12055d135440SDavid Howells 				goto discard_new;
1206*233e4735SDavid Howells 			RCU_INIT_POINTER(new->keys[keep++], key_get(key));
12075d135440SDavid Howells 		}
12085d135440SDavid Howells 	}
12095d135440SDavid Howells 	new->nkeys = keep;
12105d135440SDavid Howells 
12115d135440SDavid Howells 	/* adjust the quota */
12125d135440SDavid Howells 	key_payload_reserve(keyring,
12135d135440SDavid Howells 			    sizeof(struct keyring_list) +
12145d135440SDavid Howells 			    KEYQUOTA_LINK_BYTES * keep);
12155d135440SDavid Howells 
12165d135440SDavid Howells 	if (keep == 0) {
12175d135440SDavid Howells 		rcu_assign_pointer(keyring->payload.subscriptions, NULL);
12185d135440SDavid Howells 		kfree(new);
12195d135440SDavid Howells 	} else {
12205d135440SDavid Howells 		rcu_assign_pointer(keyring->payload.subscriptions, new);
12215d135440SDavid Howells 	}
12225d135440SDavid Howells 
12235d135440SDavid Howells 	up_write(&keyring->sem);
12245d135440SDavid Howells 
12255d135440SDavid Howells 	call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
12265d135440SDavid Howells 	kleave(" [yes]");
12275d135440SDavid Howells 	return;
12285d135440SDavid Howells 
12295d135440SDavid Howells discard_new:
12305d135440SDavid Howells 	new->nkeys = keep;
12315d135440SDavid Howells 	keyring_clear_rcu_disposal(&new->rcu);
1232c08ef808SDavid Howells 	up_write(&keyring->sem);
1233c08ef808SDavid Howells 	kleave(" [discard]");
1234c08ef808SDavid Howells 	return;
1235c08ef808SDavid Howells 
12365d135440SDavid Howells just_return:
12375d135440SDavid Howells 	up_write(&keyring->sem);
1238c08ef808SDavid Howells 	kleave(" [no dead]");
1239c08ef808SDavid Howells 	return;
1240c08ef808SDavid Howells 
1241c08ef808SDavid Howells no_klist:
1242c08ef808SDavid Howells 	up_write(&keyring->sem);
1243c08ef808SDavid Howells 	kleave(" [no_klist]");
1244c08ef808SDavid Howells 	return;
1245c08ef808SDavid Howells 
1246c08ef808SDavid Howells nomem:
1247c08ef808SDavid Howells 	up_write(&keyring->sem);
1248c08ef808SDavid Howells 	kleave(" [oom]");
12495d135440SDavid Howells }
1250