xref: /openbmc/linux/security/keys/keyring.c (revision efde8b6e16f11e7d1681c68d86c7fd51053cada7)
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 
28ceb73c12SDavid Howells #define KEY_LINK_FIXQUOTA 1UL
29ceb73c12SDavid Howells 
301da177e4SLinus Torvalds /*
31973c9f4fSDavid Howells  * When plumbing the depths of the key tree, this sets a hard limit
32973c9f4fSDavid Howells  * set on how deep we're willing to go.
331da177e4SLinus Torvalds  */
341da177e4SLinus Torvalds #define KEYRING_SEARCH_MAX_DEPTH 6
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds /*
37973c9f4fSDavid Howells  * We keep all named keyrings in a hash to speed looking them up.
381da177e4SLinus Torvalds  */
391da177e4SLinus Torvalds #define KEYRING_NAME_HASH_SIZE	(1 << 5)
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds static struct list_head	keyring_name_hash[KEYRING_NAME_HASH_SIZE];
421da177e4SLinus Torvalds static DEFINE_RWLOCK(keyring_name_lock);
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds static inline unsigned keyring_hash(const char *desc)
451da177e4SLinus Torvalds {
461da177e4SLinus Torvalds 	unsigned bucket = 0;
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds 	for (; *desc; desc++)
491da177e4SLinus Torvalds 		bucket += (unsigned char)*desc;
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds 	return bucket & (KEYRING_NAME_HASH_SIZE - 1);
521da177e4SLinus Torvalds }
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds /*
55973c9f4fSDavid Howells  * The keyring key type definition.  Keyrings are simply keys of this type and
56973c9f4fSDavid Howells  * can be treated as ordinary keys in addition to having their own special
57973c9f4fSDavid Howells  * operations.
581da177e4SLinus Torvalds  */
591da177e4SLinus Torvalds static int keyring_instantiate(struct key *keyring,
601da177e4SLinus Torvalds 			       const void *data, size_t datalen);
611da177e4SLinus Torvalds static int keyring_match(const struct key *keyring, const void *criterion);
6231204ed9SDavid Howells static void keyring_revoke(struct key *keyring);
631da177e4SLinus Torvalds static void keyring_destroy(struct key *keyring);
641da177e4SLinus Torvalds static void keyring_describe(const struct key *keyring, struct seq_file *m);
651da177e4SLinus Torvalds static long keyring_read(const struct key *keyring,
661da177e4SLinus Torvalds 			 char __user *buffer, size_t buflen);
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds struct key_type key_type_keyring = {
691da177e4SLinus Torvalds 	.name		= "keyring",
701da177e4SLinus Torvalds 	.def_datalen	= sizeof(struct keyring_list),
711da177e4SLinus Torvalds 	.instantiate	= keyring_instantiate,
721da177e4SLinus Torvalds 	.match		= keyring_match,
7331204ed9SDavid Howells 	.revoke		= keyring_revoke,
741da177e4SLinus Torvalds 	.destroy	= keyring_destroy,
751da177e4SLinus Torvalds 	.describe	= keyring_describe,
761da177e4SLinus Torvalds 	.read		= keyring_read,
771da177e4SLinus Torvalds };
787318226eSDavid Howells EXPORT_SYMBOL(key_type_keyring);
797318226eSDavid Howells 
801da177e4SLinus Torvalds /*
81973c9f4fSDavid Howells  * Semaphore to serialise link/link calls to prevent two link calls in parallel
82973c9f4fSDavid Howells  * introducing a cycle.
831da177e4SLinus Torvalds  */
841ae8f407SAdrian Bunk static DECLARE_RWSEM(keyring_serialise_link_sem);
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds /*
87973c9f4fSDavid Howells  * Publish the name of a keyring so that it can be found by name (if it has
88973c9f4fSDavid Howells  * one).
891da177e4SLinus Torvalds  */
9069664cf1SDavid Howells static void keyring_publish_name(struct key *keyring)
911da177e4SLinus Torvalds {
921da177e4SLinus Torvalds 	int bucket;
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds 	if (keyring->description) {
951da177e4SLinus Torvalds 		bucket = keyring_hash(keyring->description);
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds 		write_lock(&keyring_name_lock);
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds 		if (!keyring_name_hash[bucket].next)
1001da177e4SLinus Torvalds 			INIT_LIST_HEAD(&keyring_name_hash[bucket]);
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds 		list_add_tail(&keyring->type_data.link,
1031da177e4SLinus Torvalds 			      &keyring_name_hash[bucket]);
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds 		write_unlock(&keyring_name_lock);
1061da177e4SLinus Torvalds 	}
107a8b17ed0SDavid Howells }
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds /*
110973c9f4fSDavid Howells  * Initialise a keyring.
111973c9f4fSDavid Howells  *
112973c9f4fSDavid Howells  * Returns 0 on success, -EINVAL if given any data.
1131da177e4SLinus Torvalds  */
1141da177e4SLinus Torvalds static int keyring_instantiate(struct key *keyring,
1151da177e4SLinus Torvalds 			       const void *data, size_t datalen)
1161da177e4SLinus Torvalds {
1171da177e4SLinus Torvalds 	int ret;
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds 	ret = -EINVAL;
1201da177e4SLinus Torvalds 	if (datalen == 0) {
1211da177e4SLinus Torvalds 		/* make the keyring available by name if it has one */
1221da177e4SLinus Torvalds 		keyring_publish_name(keyring);
1231da177e4SLinus Torvalds 		ret = 0;
1241da177e4SLinus Torvalds 	}
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds 	return ret;
127a8b17ed0SDavid Howells }
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds /*
130973c9f4fSDavid Howells  * Match keyrings on their name
1311da177e4SLinus Torvalds  */
1321da177e4SLinus Torvalds static int keyring_match(const struct key *keyring, const void *description)
1331da177e4SLinus Torvalds {
1341da177e4SLinus Torvalds 	return keyring->description &&
1351da177e4SLinus Torvalds 		strcmp(keyring->description, description) == 0;
136a8b17ed0SDavid Howells }
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds /*
139973c9f4fSDavid Howells  * Clean up a keyring when it is destroyed.  Unpublish its name if it had one
140973c9f4fSDavid Howells  * and dispose of its data.
1411da177e4SLinus Torvalds  */
1421da177e4SLinus Torvalds static void keyring_destroy(struct key *keyring)
1431da177e4SLinus Torvalds {
1441da177e4SLinus Torvalds 	struct keyring_list *klist;
1451da177e4SLinus Torvalds 	int loop;
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds 	if (keyring->description) {
1481da177e4SLinus Torvalds 		write_lock(&keyring_name_lock);
14994efe72fSDavid Howells 
15094efe72fSDavid Howells 		if (keyring->type_data.link.next != NULL &&
15194efe72fSDavid Howells 		    !list_empty(&keyring->type_data.link))
1521da177e4SLinus Torvalds 			list_del(&keyring->type_data.link);
15394efe72fSDavid Howells 
1541da177e4SLinus Torvalds 		write_unlock(&keyring_name_lock);
1551da177e4SLinus Torvalds 	}
1561da177e4SLinus Torvalds 
157e7b0a61bSPaul E. McKenney 	klist = rcu_dereference_check(keyring->payload.subscriptions,
158e7b0a61bSPaul E. McKenney 				      atomic_read(&keyring->usage) == 0);
1591da177e4SLinus Torvalds 	if (klist) {
1601da177e4SLinus Torvalds 		for (loop = klist->nkeys - 1; loop >= 0; loop--)
1611da177e4SLinus Torvalds 			key_put(klist->keys[loop]);
1621da177e4SLinus Torvalds 		kfree(klist);
1631da177e4SLinus Torvalds 	}
164a8b17ed0SDavid Howells }
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds /*
167973c9f4fSDavid Howells  * Describe a keyring for /proc.
1681da177e4SLinus Torvalds  */
1691da177e4SLinus Torvalds static void keyring_describe(const struct key *keyring, struct seq_file *m)
1701da177e4SLinus Torvalds {
1711da177e4SLinus Torvalds 	struct keyring_list *klist;
1721da177e4SLinus Torvalds 
173c8563473Swzt.wzt@gmail.com 	if (keyring->description)
1741da177e4SLinus Torvalds 		seq_puts(m, keyring->description);
175c8563473Swzt.wzt@gmail.com 	else
1761da177e4SLinus Torvalds 		seq_puts(m, "[anon]");
1771da177e4SLinus Torvalds 
17878b7280cSDavid Howells 	if (key_is_instantiated(keyring)) {
17976d8aeabSDavid Howells 		rcu_read_lock();
18076d8aeabSDavid Howells 		klist = rcu_dereference(keyring->payload.subscriptions);
1811da177e4SLinus Torvalds 		if (klist)
1821da177e4SLinus Torvalds 			seq_printf(m, ": %u/%u", klist->nkeys, klist->maxkeys);
1831da177e4SLinus Torvalds 		else
1841da177e4SLinus Torvalds 			seq_puts(m, ": empty");
18576d8aeabSDavid Howells 		rcu_read_unlock();
186a8b17ed0SDavid Howells 	}
18778b7280cSDavid Howells }
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds /*
190973c9f4fSDavid Howells  * Read a list of key IDs from the keyring's contents in binary form
191973c9f4fSDavid Howells  *
192973c9f4fSDavid Howells  * The keyring's semaphore is read-locked by the caller.
1931da177e4SLinus Torvalds  */
1941da177e4SLinus Torvalds static long keyring_read(const struct key *keyring,
1951da177e4SLinus Torvalds 			 char __user *buffer, size_t buflen)
1961da177e4SLinus Torvalds {
1971da177e4SLinus Torvalds 	struct keyring_list *klist;
1981da177e4SLinus Torvalds 	struct key *key;
1991da177e4SLinus Torvalds 	size_t qty, tmp;
2001da177e4SLinus Torvalds 	int loop, ret;
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds 	ret = 0;
203f0641cbaSDavid Howells 	klist = rcu_dereference_locked_keyring(keyring);
2041da177e4SLinus Torvalds 	if (klist) {
2051da177e4SLinus Torvalds 		/* calculate how much data we could return */
2061da177e4SLinus Torvalds 		qty = klist->nkeys * sizeof(key_serial_t);
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds 		if (buffer && buflen > 0) {
2091da177e4SLinus Torvalds 			if (buflen > qty)
2101da177e4SLinus Torvalds 				buflen = qty;
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds 			/* copy the IDs of the subscribed keys into the
2131da177e4SLinus Torvalds 			 * buffer */
2141da177e4SLinus Torvalds 			ret = -EFAULT;
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds 			for (loop = 0; loop < klist->nkeys; loop++) {
2171da177e4SLinus Torvalds 				key = klist->keys[loop];
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 				tmp = sizeof(key_serial_t);
2201da177e4SLinus Torvalds 				if (tmp > buflen)
2211da177e4SLinus Torvalds 					tmp = buflen;
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds 				if (copy_to_user(buffer,
2241da177e4SLinus Torvalds 						 &key->serial,
2251da177e4SLinus Torvalds 						 tmp) != 0)
2261da177e4SLinus Torvalds 					goto error;
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds 				buflen -= tmp;
2291da177e4SLinus Torvalds 				if (buflen == 0)
2301da177e4SLinus Torvalds 					break;
2311da177e4SLinus Torvalds 				buffer += tmp;
2321da177e4SLinus Torvalds 			}
2331da177e4SLinus Torvalds 		}
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 		ret = qty;
2361da177e4SLinus Torvalds 	}
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds error:
2391da177e4SLinus Torvalds 	return ret;
240a8b17ed0SDavid Howells }
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds /*
243973c9f4fSDavid Howells  * Allocate a keyring and link into the destination keyring.
2441da177e4SLinus Torvalds  */
2451da177e4SLinus Torvalds struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
246d84f4f99SDavid Howells 			  const struct cred *cred, unsigned long flags,
247d720024eSMichael LeMay 			  struct key *dest)
2481da177e4SLinus Torvalds {
2491da177e4SLinus Torvalds 	struct key *keyring;
2501da177e4SLinus Torvalds 	int ret;
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds 	keyring = key_alloc(&key_type_keyring, description,
253d84f4f99SDavid Howells 			    uid, gid, cred,
25429db9190SDavid Howells 			    (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
2557e047ef5SDavid Howells 			    flags);
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds 	if (!IS_ERR(keyring)) {
2583e30148cSDavid Howells 		ret = key_instantiate_and_link(keyring, NULL, 0, dest, NULL);
2591da177e4SLinus Torvalds 		if (ret < 0) {
2601da177e4SLinus Torvalds 			key_put(keyring);
2611da177e4SLinus Torvalds 			keyring = ERR_PTR(ret);
2621da177e4SLinus Torvalds 		}
2631da177e4SLinus Torvalds 	}
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 	return keyring;
266a8b17ed0SDavid Howells }
2671da177e4SLinus Torvalds 
268973c9f4fSDavid Howells /**
269973c9f4fSDavid Howells  * keyring_search_aux - Search a keyring tree for a key matching some criteria
270973c9f4fSDavid Howells  * @keyring_ref: A pointer to the keyring with possession indicator.
271973c9f4fSDavid Howells  * @cred: The credentials to use for permissions checks.
272973c9f4fSDavid Howells  * @type: The type of key to search for.
273973c9f4fSDavid Howells  * @description: Parameter for @match.
274973c9f4fSDavid Howells  * @match: Function to rule on whether or not a key is the one required.
27578b7280cSDavid Howells  * @no_state_check: Don't check if a matching key is bad
276973c9f4fSDavid Howells  *
277973c9f4fSDavid Howells  * Search the supplied keyring tree for a key that matches the criteria given.
278973c9f4fSDavid Howells  * The root keyring and any linked keyrings must grant Search permission to the
279973c9f4fSDavid Howells  * caller to be searchable and keys can only be found if they too grant Search
280973c9f4fSDavid Howells  * to the caller. The possession flag on the root keyring pointer controls use
281973c9f4fSDavid Howells  * of the possessor bits in permissions checking of the entire tree.  In
282973c9f4fSDavid Howells  * addition, the LSM gets to forbid keyring searches and key matches.
283973c9f4fSDavid Howells  *
284973c9f4fSDavid Howells  * The search is performed as a breadth-then-depth search up to the prescribed
285973c9f4fSDavid Howells  * limit (KEYRING_SEARCH_MAX_DEPTH).
286973c9f4fSDavid Howells  *
287973c9f4fSDavid Howells  * Keys are matched to the type provided and are then filtered by the match
288973c9f4fSDavid Howells  * function, which is given the description to use in any way it sees fit.  The
289973c9f4fSDavid Howells  * match function may use any attributes of a key that it wishes to to
290973c9f4fSDavid Howells  * determine the match.  Normally the match function from the key type would be
291973c9f4fSDavid Howells  * used.
292973c9f4fSDavid Howells  *
293973c9f4fSDavid Howells  * RCU is used to prevent the keyring key lists from disappearing without the
294973c9f4fSDavid Howells  * need to take lots of locks.
295973c9f4fSDavid Howells  *
296973c9f4fSDavid Howells  * Returns a pointer to the found key and increments the key usage count if
297973c9f4fSDavid Howells  * successful; -EAGAIN if no matching keys were found, or if expired or revoked
298973c9f4fSDavid Howells  * keys were found; -ENOKEY if only negative keys were found; -ENOTDIR if the
299973c9f4fSDavid Howells  * specified keyring wasn't a keyring.
300973c9f4fSDavid Howells  *
301973c9f4fSDavid Howells  * In the case of a successful return, the possession attribute from
302973c9f4fSDavid Howells  * @keyring_ref is propagated to the returned key reference.
3031da177e4SLinus Torvalds  */
304664cceb0SDavid Howells key_ref_t keyring_search_aux(key_ref_t keyring_ref,
305d84f4f99SDavid Howells 			     const struct cred *cred,
3061da177e4SLinus Torvalds 			     struct key_type *type,
3071da177e4SLinus Torvalds 			     const void *description,
30878b7280cSDavid Howells 			     key_match_func_t match,
30978b7280cSDavid Howells 			     bool no_state_check)
3101da177e4SLinus Torvalds {
3111da177e4SLinus Torvalds 	struct {
31276d8aeabSDavid Howells 		struct keyring_list *keylist;
3131da177e4SLinus Torvalds 		int kix;
3141da177e4SLinus Torvalds 	} stack[KEYRING_SEARCH_MAX_DEPTH];
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds 	struct keyring_list *keylist;
3171da177e4SLinus Torvalds 	struct timespec now;
318dceba994SKevin Coffman 	unsigned long possessed, kflags;
319664cceb0SDavid Howells 	struct key *keyring, *key;
320664cceb0SDavid Howells 	key_ref_t key_ref;
3211da177e4SLinus Torvalds 	long err;
322*efde8b6eSDavid Howells 	int sp, nkeys, kix;
3231da177e4SLinus Torvalds 
324664cceb0SDavid Howells 	keyring = key_ref_to_ptr(keyring_ref);
325664cceb0SDavid Howells 	possessed = is_key_possessed(keyring_ref);
3261da177e4SLinus Torvalds 	key_check(keyring);
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds 	/* top keyring must have search permission to begin the search */
329d84f4f99SDavid Howells 	err = key_task_permission(keyring_ref, cred, KEY_SEARCH);
33029db9190SDavid Howells 	if (err < 0) {
33129db9190SDavid Howells 		key_ref = ERR_PTR(err);
3321da177e4SLinus Torvalds 		goto error;
33329db9190SDavid Howells 	}
3341da177e4SLinus Torvalds 
335664cceb0SDavid Howells 	key_ref = ERR_PTR(-ENOTDIR);
3361da177e4SLinus Torvalds 	if (keyring->type != &key_type_keyring)
3371da177e4SLinus Torvalds 		goto error;
3381da177e4SLinus Torvalds 
339664cceb0SDavid Howells 	rcu_read_lock();
340664cceb0SDavid Howells 
3411da177e4SLinus Torvalds 	now = current_kernel_time();
3421da177e4SLinus Torvalds 	err = -EAGAIN;
3431da177e4SLinus Torvalds 	sp = 0;
3441da177e4SLinus Torvalds 
345dceba994SKevin Coffman 	/* firstly we should check to see if this top-level keyring is what we
346dceba994SKevin Coffman 	 * are looking for */
347dceba994SKevin Coffman 	key_ref = ERR_PTR(-EAGAIN);
348dceba994SKevin Coffman 	kflags = keyring->flags;
349dceba994SKevin Coffman 	if (keyring->type == type && match(keyring, description)) {
350dceba994SKevin Coffman 		key = keyring;
35178b7280cSDavid Howells 		if (no_state_check)
35278b7280cSDavid Howells 			goto found;
353dceba994SKevin Coffman 
354dceba994SKevin Coffman 		/* check it isn't negative and hasn't expired or been
355dceba994SKevin Coffman 		 * revoked */
356dceba994SKevin Coffman 		if (kflags & (1 << KEY_FLAG_REVOKED))
357dceba994SKevin Coffman 			goto error_2;
358dceba994SKevin Coffman 		if (key->expiry && now.tv_sec >= key->expiry)
359dceba994SKevin Coffman 			goto error_2;
360fdd1b945SDavid Howells 		key_ref = ERR_PTR(key->type_data.reject_error);
361dceba994SKevin Coffman 		if (kflags & (1 << KEY_FLAG_NEGATIVE))
362dceba994SKevin Coffman 			goto error_2;
363dceba994SKevin Coffman 		goto found;
364dceba994SKevin Coffman 	}
365dceba994SKevin Coffman 
366dceba994SKevin Coffman 	/* otherwise, the top keyring must not be revoked, expired, or
367dceba994SKevin Coffman 	 * negatively instantiated if we are to search it */
368dceba994SKevin Coffman 	key_ref = ERR_PTR(-EAGAIN);
369dceba994SKevin Coffman 	if (kflags & ((1 << KEY_FLAG_REVOKED) | (1 << KEY_FLAG_NEGATIVE)) ||
370dceba994SKevin Coffman 	    (keyring->expiry && now.tv_sec >= keyring->expiry))
371dceba994SKevin Coffman 		goto error_2;
372dceba994SKevin Coffman 
3731da177e4SLinus Torvalds 	/* start processing a new keyring */
3741da177e4SLinus Torvalds descend:
37576d8aeabSDavid Howells 	if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
3761da177e4SLinus Torvalds 		goto not_this_keyring;
3771da177e4SLinus Torvalds 
37876d8aeabSDavid Howells 	keylist = rcu_dereference(keyring->payload.subscriptions);
3791da177e4SLinus Torvalds 	if (!keylist)
3801da177e4SLinus Torvalds 		goto not_this_keyring;
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 	/* iterate through the keys in this keyring first */
383*efde8b6eSDavid Howells 	nkeys = keylist->nkeys;
384*efde8b6eSDavid Howells 	smp_rmb();
385*efde8b6eSDavid Howells 	for (kix = 0; kix < nkeys; kix++) {
3861da177e4SLinus Torvalds 		key = keylist->keys[kix];
387dceba994SKevin Coffman 		kflags = key->flags;
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 		/* ignore keys not of this type */
3901da177e4SLinus Torvalds 		if (key->type != type)
3911da177e4SLinus Torvalds 			continue;
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds 		/* skip revoked keys and expired keys */
39478b7280cSDavid Howells 		if (!no_state_check) {
395dceba994SKevin Coffman 			if (kflags & (1 << KEY_FLAG_REVOKED))
3961da177e4SLinus Torvalds 				continue;
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 			if (key->expiry && now.tv_sec >= key->expiry)
3991da177e4SLinus Torvalds 				continue;
40078b7280cSDavid Howells 		}
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds 		/* keys that don't match */
4031da177e4SLinus Torvalds 		if (!match(key, description))
4041da177e4SLinus Torvalds 			continue;
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds 		/* key must have search permissions */
40729db9190SDavid Howells 		if (key_task_permission(make_key_ref(key, possessed),
408d84f4f99SDavid Howells 					cred, KEY_SEARCH) < 0)
4091da177e4SLinus Torvalds 			continue;
4101da177e4SLinus Torvalds 
41178b7280cSDavid Howells 		if (no_state_check)
41278b7280cSDavid Howells 			goto found;
41378b7280cSDavid Howells 
414dceba994SKevin Coffman 		/* we set a different error code if we pass a negative key */
415dceba994SKevin Coffman 		if (kflags & (1 << KEY_FLAG_NEGATIVE)) {
416fdd1b945SDavid Howells 			err = key->type_data.reject_error;
4171da177e4SLinus Torvalds 			continue;
4181da177e4SLinus Torvalds 		}
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds 		goto found;
4211da177e4SLinus Torvalds 	}
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds 	/* search through the keyrings nested in this one */
4241da177e4SLinus Torvalds 	kix = 0;
4251da177e4SLinus Torvalds ascend:
426*efde8b6eSDavid Howells 	nkeys = keylist->nkeys;
427*efde8b6eSDavid Howells 	smp_rmb();
428*efde8b6eSDavid Howells 	for (; kix < nkeys; kix++) {
4291da177e4SLinus Torvalds 		key = keylist->keys[kix];
4301da177e4SLinus Torvalds 		if (key->type != &key_type_keyring)
43176d8aeabSDavid Howells 			continue;
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds 		/* recursively search nested keyrings
4341da177e4SLinus Torvalds 		 * - only search keyrings for which we have search permission
4351da177e4SLinus Torvalds 		 */
4361da177e4SLinus Torvalds 		if (sp >= KEYRING_SEARCH_MAX_DEPTH)
43776d8aeabSDavid Howells 			continue;
4381da177e4SLinus Torvalds 
4390f6ed7c2SDavid Howells 		if (key_task_permission(make_key_ref(key, possessed),
440d84f4f99SDavid Howells 					cred, KEY_SEARCH) < 0)
44176d8aeabSDavid Howells 			continue;
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds 		/* stack the current position */
44476d8aeabSDavid Howells 		stack[sp].keylist = keylist;
4451da177e4SLinus Torvalds 		stack[sp].kix = kix;
4461da177e4SLinus Torvalds 		sp++;
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds 		/* begin again with the new keyring */
4491da177e4SLinus Torvalds 		keyring = key;
4501da177e4SLinus Torvalds 		goto descend;
4511da177e4SLinus Torvalds 	}
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds 	/* the keyring we're looking at was disqualified or didn't contain a
4541da177e4SLinus Torvalds 	 * matching key */
4551da177e4SLinus Torvalds not_this_keyring:
4561da177e4SLinus Torvalds 	if (sp > 0) {
4571da177e4SLinus Torvalds 		/* resume the processing of a keyring higher up in the tree */
4581da177e4SLinus Torvalds 		sp--;
45976d8aeabSDavid Howells 		keylist = stack[sp].keylist;
4601da177e4SLinus Torvalds 		kix = stack[sp].kix + 1;
4611da177e4SLinus Torvalds 		goto ascend;
4621da177e4SLinus Torvalds 	}
4631da177e4SLinus Torvalds 
464664cceb0SDavid Howells 	key_ref = ERR_PTR(err);
465664cceb0SDavid Howells 	goto error_2;
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds 	/* we found a viable match */
4681da177e4SLinus Torvalds found:
4691da177e4SLinus Torvalds 	atomic_inc(&key->usage);
4701da177e4SLinus Torvalds 	key_check(key);
471664cceb0SDavid Howells 	key_ref = make_key_ref(key, possessed);
472664cceb0SDavid Howells error_2:
47376d8aeabSDavid Howells 	rcu_read_unlock();
474664cceb0SDavid Howells error:
475664cceb0SDavid Howells 	return key_ref;
476a8b17ed0SDavid Howells }
4771da177e4SLinus Torvalds 
478973c9f4fSDavid Howells /**
479973c9f4fSDavid Howells  * keyring_search - Search the supplied keyring tree for a matching key
480973c9f4fSDavid Howells  * @keyring: The root of the keyring tree to be searched.
481973c9f4fSDavid Howells  * @type: The type of keyring we want to find.
482973c9f4fSDavid Howells  * @description: The name of the keyring we want to find.
483973c9f4fSDavid Howells  *
484973c9f4fSDavid Howells  * As keyring_search_aux() above, but using the current task's credentials and
485973c9f4fSDavid Howells  * type's default matching function.
4861da177e4SLinus Torvalds  */
487664cceb0SDavid Howells key_ref_t keyring_search(key_ref_t keyring,
4881da177e4SLinus Torvalds 			 struct key_type *type,
4891da177e4SLinus Torvalds 			 const char *description)
4901da177e4SLinus Torvalds {
4913e30148cSDavid Howells 	if (!type->match)
4923e30148cSDavid Howells 		return ERR_PTR(-ENOKEY);
4933e30148cSDavid Howells 
494d84f4f99SDavid Howells 	return keyring_search_aux(keyring, current->cred,
49578b7280cSDavid Howells 				  type, description, type->match, false);
496a8b17ed0SDavid Howells }
4971da177e4SLinus Torvalds EXPORT_SYMBOL(keyring_search);
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds /*
500973c9f4fSDavid Howells  * Search the given keyring only (no recursion).
501973c9f4fSDavid Howells  *
502973c9f4fSDavid Howells  * The caller must guarantee that the keyring is a keyring and that the
503973c9f4fSDavid Howells  * permission is granted to search the keyring as no check is made here.
504973c9f4fSDavid Howells  *
505973c9f4fSDavid Howells  * RCU is used to make it unnecessary to lock the keyring key list here.
506973c9f4fSDavid Howells  *
507973c9f4fSDavid Howells  * Returns a pointer to the found key with usage count incremented if
508973c9f4fSDavid Howells  * successful and returns -ENOKEY if not found.  Revoked keys and keys not
509973c9f4fSDavid Howells  * providing the requested permission are skipped over.
510973c9f4fSDavid Howells  *
511973c9f4fSDavid Howells  * If successful, the possession indicator is propagated from the keyring ref
512973c9f4fSDavid Howells  * to the returned key reference.
5131da177e4SLinus Torvalds  */
514664cceb0SDavid Howells key_ref_t __keyring_search_one(key_ref_t keyring_ref,
5151da177e4SLinus Torvalds 			       const struct key_type *ktype,
5161da177e4SLinus Torvalds 			       const char *description,
5171da177e4SLinus Torvalds 			       key_perm_t perm)
5181da177e4SLinus Torvalds {
5191da177e4SLinus Torvalds 	struct keyring_list *klist;
520664cceb0SDavid Howells 	unsigned long possessed;
521664cceb0SDavid Howells 	struct key *keyring, *key;
522*efde8b6eSDavid Howells 	int nkeys, loop;
5231da177e4SLinus Torvalds 
524664cceb0SDavid Howells 	keyring = key_ref_to_ptr(keyring_ref);
525664cceb0SDavid Howells 	possessed = is_key_possessed(keyring_ref);
526664cceb0SDavid Howells 
52776d8aeabSDavid Howells 	rcu_read_lock();
52876d8aeabSDavid Howells 
52976d8aeabSDavid Howells 	klist = rcu_dereference(keyring->payload.subscriptions);
5301da177e4SLinus Torvalds 	if (klist) {
531*efde8b6eSDavid Howells 		nkeys = klist->nkeys;
532*efde8b6eSDavid Howells 		smp_rmb();
533*efde8b6eSDavid Howells 		for (loop = 0; loop < nkeys ; loop++) {
5341da177e4SLinus Torvalds 			key = klist->keys[loop];
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds 			if (key->type == ktype &&
5373e30148cSDavid Howells 			    (!key->type->match ||
5383e30148cSDavid Howells 			     key->type->match(key, description)) &&
539664cceb0SDavid Howells 			    key_permission(make_key_ref(key, possessed),
540db1d1d57SDavid Howells 					   perm) == 0 &&
54176d8aeabSDavid Howells 			    !test_bit(KEY_FLAG_REVOKED, &key->flags)
5421da177e4SLinus Torvalds 			    )
5431da177e4SLinus Torvalds 				goto found;
5441da177e4SLinus Torvalds 		}
5451da177e4SLinus Torvalds 	}
5461da177e4SLinus Torvalds 
547664cceb0SDavid Howells 	rcu_read_unlock();
548664cceb0SDavid Howells 	return ERR_PTR(-ENOKEY);
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds found:
5511da177e4SLinus Torvalds 	atomic_inc(&key->usage);
55276d8aeabSDavid Howells 	rcu_read_unlock();
553664cceb0SDavid Howells 	return make_key_ref(key, possessed);
554a8b17ed0SDavid Howells }
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds /*
557973c9f4fSDavid Howells  * Find a keyring with the specified name.
558973c9f4fSDavid Howells  *
559973c9f4fSDavid Howells  * All named keyrings in the current user namespace are searched, provided they
560973c9f4fSDavid Howells  * grant Search permission directly to the caller (unless this check is
561973c9f4fSDavid Howells  * skipped).  Keyrings whose usage points have reached zero or who have been
562973c9f4fSDavid Howells  * revoked are skipped.
563973c9f4fSDavid Howells  *
564973c9f4fSDavid Howells  * Returns a pointer to the keyring with the keyring's refcount having being
565973c9f4fSDavid Howells  * incremented on success.  -ENOKEY is returned if a key could not be found.
5661da177e4SLinus Torvalds  */
56769664cf1SDavid Howells struct key *find_keyring_by_name(const char *name, bool skip_perm_check)
5681da177e4SLinus Torvalds {
5691da177e4SLinus Torvalds 	struct key *keyring;
5701da177e4SLinus Torvalds 	int bucket;
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 	if (!name)
573cea7daa3SToshiyuki Okajima 		return ERR_PTR(-EINVAL);
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds 	bucket = keyring_hash(name);
5761da177e4SLinus Torvalds 
5771da177e4SLinus Torvalds 	read_lock(&keyring_name_lock);
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds 	if (keyring_name_hash[bucket].next) {
5801da177e4SLinus Torvalds 		/* search this hash bucket for a keyring with a matching name
5811da177e4SLinus Torvalds 		 * that's readable and that hasn't been revoked */
5821da177e4SLinus Torvalds 		list_for_each_entry(keyring,
5831da177e4SLinus Torvalds 				    &keyring_name_hash[bucket],
5841da177e4SLinus Torvalds 				    type_data.link
5851da177e4SLinus Torvalds 				    ) {
5862ea190d0SSerge E. Hallyn 			if (keyring->user->user_ns != current_user_ns())
5872ea190d0SSerge E. Hallyn 				continue;
5882ea190d0SSerge E. Hallyn 
58976d8aeabSDavid Howells 			if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
5901da177e4SLinus Torvalds 				continue;
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds 			if (strcmp(keyring->description, name) != 0)
5931da177e4SLinus Torvalds 				continue;
5941da177e4SLinus Torvalds 
59569664cf1SDavid Howells 			if (!skip_perm_check &&
59669664cf1SDavid Howells 			    key_permission(make_key_ref(keyring, 0),
59729db9190SDavid Howells 					   KEY_SEARCH) < 0)
5981da177e4SLinus Torvalds 				continue;
5991da177e4SLinus Torvalds 
600cea7daa3SToshiyuki Okajima 			/* we've got a match but we might end up racing with
601cea7daa3SToshiyuki Okajima 			 * key_cleanup() if the keyring is currently 'dead'
602cea7daa3SToshiyuki Okajima 			 * (ie. it has a zero usage count) */
603cea7daa3SToshiyuki Okajima 			if (!atomic_inc_not_zero(&keyring->usage))
604cea7daa3SToshiyuki Okajima 				continue;
605cea7daa3SToshiyuki Okajima 			goto out;
6061da177e4SLinus Torvalds 		}
6071da177e4SLinus Torvalds 	}
6081da177e4SLinus Torvalds 
6091da177e4SLinus Torvalds 	keyring = ERR_PTR(-ENOKEY);
610cea7daa3SToshiyuki Okajima out:
611cea7daa3SToshiyuki Okajima 	read_unlock(&keyring_name_lock);
6121da177e4SLinus Torvalds 	return keyring;
613a8b17ed0SDavid Howells }
6141da177e4SLinus Torvalds 
6151da177e4SLinus Torvalds /*
616973c9f4fSDavid Howells  * See if a cycle will will be created by inserting acyclic tree B in acyclic
617973c9f4fSDavid Howells  * tree A at the topmost level (ie: as a direct child of A).
618973c9f4fSDavid Howells  *
619973c9f4fSDavid Howells  * Since we are adding B to A at the top level, checking for cycles should just
620973c9f4fSDavid Howells  * be a matter of seeing if node A is somewhere in tree B.
6211da177e4SLinus Torvalds  */
6221da177e4SLinus Torvalds static int keyring_detect_cycle(struct key *A, struct key *B)
6231da177e4SLinus Torvalds {
6241da177e4SLinus Torvalds 	struct {
62576d8aeabSDavid Howells 		struct keyring_list *keylist;
6261da177e4SLinus Torvalds 		int kix;
6271da177e4SLinus Torvalds 	} stack[KEYRING_SEARCH_MAX_DEPTH];
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds 	struct keyring_list *keylist;
6301da177e4SLinus Torvalds 	struct key *subtree, *key;
631*efde8b6eSDavid Howells 	int sp, nkeys, kix, ret;
6321da177e4SLinus Torvalds 
63376d8aeabSDavid Howells 	rcu_read_lock();
63476d8aeabSDavid Howells 
6351da177e4SLinus Torvalds 	ret = -EDEADLK;
6361da177e4SLinus Torvalds 	if (A == B)
63776d8aeabSDavid Howells 		goto cycle_detected;
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds 	subtree = B;
6401da177e4SLinus Torvalds 	sp = 0;
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds 	/* start processing a new keyring */
6431da177e4SLinus Torvalds descend:
64476d8aeabSDavid Howells 	if (test_bit(KEY_FLAG_REVOKED, &subtree->flags))
6451da177e4SLinus Torvalds 		goto not_this_keyring;
6461da177e4SLinus Torvalds 
64776d8aeabSDavid Howells 	keylist = rcu_dereference(subtree->payload.subscriptions);
6481da177e4SLinus Torvalds 	if (!keylist)
6491da177e4SLinus Torvalds 		goto not_this_keyring;
6501da177e4SLinus Torvalds 	kix = 0;
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds ascend:
6531da177e4SLinus Torvalds 	/* iterate through the remaining keys in this keyring */
654*efde8b6eSDavid Howells 	nkeys = keylist->nkeys;
655*efde8b6eSDavid Howells 	smp_rmb();
656*efde8b6eSDavid Howells 	for (; kix < nkeys; kix++) {
6571da177e4SLinus Torvalds 		key = keylist->keys[kix];
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds 		if (key == A)
6601da177e4SLinus Torvalds 			goto cycle_detected;
6611da177e4SLinus Torvalds 
6621da177e4SLinus Torvalds 		/* recursively check nested keyrings */
6631da177e4SLinus Torvalds 		if (key->type == &key_type_keyring) {
6641da177e4SLinus Torvalds 			if (sp >= KEYRING_SEARCH_MAX_DEPTH)
6651da177e4SLinus Torvalds 				goto too_deep;
6661da177e4SLinus Torvalds 
6671da177e4SLinus Torvalds 			/* stack the current position */
66876d8aeabSDavid Howells 			stack[sp].keylist = keylist;
6691da177e4SLinus Torvalds 			stack[sp].kix = kix;
6701da177e4SLinus Torvalds 			sp++;
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds 			/* begin again with the new keyring */
6731da177e4SLinus Torvalds 			subtree = key;
6741da177e4SLinus Torvalds 			goto descend;
6751da177e4SLinus Torvalds 		}
6761da177e4SLinus Torvalds 	}
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds 	/* the keyring we're looking at was disqualified or didn't contain a
6791da177e4SLinus Torvalds 	 * matching key */
6801da177e4SLinus Torvalds not_this_keyring:
6811da177e4SLinus Torvalds 	if (sp > 0) {
6821da177e4SLinus Torvalds 		/* resume the checking of a keyring higher up in the tree */
6831da177e4SLinus Torvalds 		sp--;
68476d8aeabSDavid Howells 		keylist = stack[sp].keylist;
6851da177e4SLinus Torvalds 		kix = stack[sp].kix + 1;
6861da177e4SLinus Torvalds 		goto ascend;
6871da177e4SLinus Torvalds 	}
6881da177e4SLinus Torvalds 
6891da177e4SLinus Torvalds 	ret = 0; /* no cycles detected */
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds error:
69276d8aeabSDavid Howells 	rcu_read_unlock();
6931da177e4SLinus Torvalds 	return ret;
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds too_deep:
6961da177e4SLinus Torvalds 	ret = -ELOOP;
69776d8aeabSDavid Howells 	goto error;
69876d8aeabSDavid Howells 
6991da177e4SLinus Torvalds cycle_detected:
7001da177e4SLinus Torvalds 	ret = -EDEADLK;
7011da177e4SLinus Torvalds 	goto error;
702a8b17ed0SDavid Howells }
7031da177e4SLinus Torvalds 
70476d8aeabSDavid Howells /*
705973c9f4fSDavid Howells  * Dispose of a keyring list after the RCU grace period, freeing the unlinked
706cab8eb59SDavid Howells  * key
707cab8eb59SDavid Howells  */
708cab8eb59SDavid Howells static void keyring_unlink_rcu_disposal(struct rcu_head *rcu)
709cab8eb59SDavid Howells {
710cab8eb59SDavid Howells 	struct keyring_list *klist =
711cab8eb59SDavid Howells 		container_of(rcu, struct keyring_list, rcu);
712cab8eb59SDavid Howells 
7134be929beSAlexey Dobriyan 	if (klist->delkey != USHRT_MAX)
714cab8eb59SDavid Howells 		key_put(klist->keys[klist->delkey]);
715cab8eb59SDavid Howells 	kfree(klist);
716f70e2e06SDavid Howells }
717cab8eb59SDavid Howells 
718cab8eb59SDavid Howells /*
719973c9f4fSDavid Howells  * Preallocate memory so that a key can be linked into to a keyring.
7201da177e4SLinus Torvalds  */
721f70e2e06SDavid Howells int __key_link_begin(struct key *keyring, const struct key_type *type,
722ceb73c12SDavid Howells 		     const char *description, unsigned long *_prealloc)
723f70e2e06SDavid Howells 	__acquires(&keyring->sem)
7241da177e4SLinus Torvalds {
7251da177e4SLinus Torvalds 	struct keyring_list *klist, *nklist;
726ceb73c12SDavid Howells 	unsigned long prealloc;
7271da177e4SLinus Torvalds 	unsigned max;
7281da177e4SLinus Torvalds 	size_t size;
729cab8eb59SDavid Howells 	int loop, ret;
7301da177e4SLinus Torvalds 
731f70e2e06SDavid Howells 	kenter("%d,%s,%s,", key_serial(keyring), type->name, description);
732f70e2e06SDavid Howells 
733f70e2e06SDavid Howells 	if (keyring->type != &key_type_keyring)
734f70e2e06SDavid Howells 		return -ENOTDIR;
735f70e2e06SDavid Howells 
736f70e2e06SDavid Howells 	down_write(&keyring->sem);
737f70e2e06SDavid Howells 
7381da177e4SLinus Torvalds 	ret = -EKEYREVOKED;
73976d8aeabSDavid Howells 	if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
740f70e2e06SDavid Howells 		goto error_krsem;
7411da177e4SLinus Torvalds 
742f70e2e06SDavid Howells 	/* serialise link/link calls to prevent parallel calls causing a cycle
743f70e2e06SDavid Howells 	 * when linking two keyring in opposite orders */
744f70e2e06SDavid Howells 	if (type == &key_type_keyring)
7451da177e4SLinus Torvalds 		down_write(&keyring_serialise_link_sem);
7461da177e4SLinus Torvalds 
747f70e2e06SDavid Howells 	klist = rcu_dereference_locked_keyring(keyring);
7481da177e4SLinus Torvalds 
749cab8eb59SDavid Howells 	/* see if there's a matching key we can displace */
750cab8eb59SDavid Howells 	if (klist && klist->nkeys > 0) {
751cab8eb59SDavid Howells 		for (loop = klist->nkeys - 1; loop >= 0; loop--) {
752cab8eb59SDavid Howells 			if (klist->keys[loop]->type == type &&
753cab8eb59SDavid Howells 			    strcmp(klist->keys[loop]->description,
754f70e2e06SDavid Howells 				   description) == 0
755cab8eb59SDavid Howells 			    ) {
756f70e2e06SDavid Howells 				/* found a match - we'll replace this one with
757f70e2e06SDavid Howells 				 * the new key */
758cab8eb59SDavid Howells 				size = sizeof(struct key *) * klist->maxkeys;
759cab8eb59SDavid Howells 				size += sizeof(*klist);
760cab8eb59SDavid Howells 				BUG_ON(size > PAGE_SIZE);
761cab8eb59SDavid Howells 
762cab8eb59SDavid Howells 				ret = -ENOMEM;
76348ad504eSEric Sesterhenn 				nklist = kmemdup(klist, size, GFP_KERNEL);
764cab8eb59SDavid Howells 				if (!nklist)
765f70e2e06SDavid Howells 					goto error_sem;
766cab8eb59SDavid Howells 
767f70e2e06SDavid Howells 				/* note replacement slot */
768f70e2e06SDavid Howells 				klist->delkey = nklist->delkey = loop;
769ceb73c12SDavid Howells 				prealloc = (unsigned long)nklist;
770cab8eb59SDavid Howells 				goto done;
771cab8eb59SDavid Howells 			}
772cab8eb59SDavid Howells 		}
773cab8eb59SDavid Howells 	}
774cab8eb59SDavid Howells 
7751da177e4SLinus Torvalds 	/* check that we aren't going to overrun the user's quota */
7761da177e4SLinus Torvalds 	ret = key_payload_reserve(keyring,
7771da177e4SLinus Torvalds 				  keyring->datalen + KEYQUOTA_LINK_BYTES);
7781da177e4SLinus Torvalds 	if (ret < 0)
779f70e2e06SDavid Howells 		goto error_sem;
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds 	if (klist && klist->nkeys < klist->maxkeys) {
782f70e2e06SDavid Howells 		/* there's sufficient slack space to append directly */
783f70e2e06SDavid Howells 		nklist = NULL;
784ceb73c12SDavid Howells 		prealloc = KEY_LINK_FIXQUOTA;
785512ea3bcSChihau Chau 	} else {
7861da177e4SLinus Torvalds 		/* grow the key list */
7871da177e4SLinus Torvalds 		max = 4;
7881da177e4SLinus Torvalds 		if (klist)
7891da177e4SLinus Torvalds 			max += klist->maxkeys;
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds 		ret = -ENFILE;
7924be929beSAlexey Dobriyan 		if (max > USHRT_MAX - 1)
793f70e2e06SDavid Howells 			goto error_quota;
794a4014d8fSDavid Howells 		size = sizeof(*klist) + sizeof(struct key *) * max;
7951da177e4SLinus Torvalds 		if (size > PAGE_SIZE)
796f70e2e06SDavid Howells 			goto error_quota;
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds 		ret = -ENOMEM;
7991da177e4SLinus Torvalds 		nklist = kmalloc(size, GFP_KERNEL);
8001da177e4SLinus Torvalds 		if (!nklist)
801f70e2e06SDavid Howells 			goto error_quota;
8021da177e4SLinus Torvalds 
803f70e2e06SDavid Howells 		nklist->maxkeys = max;
8041da177e4SLinus Torvalds 		if (klist) {
805f70e2e06SDavid Howells 			memcpy(nklist->keys, klist->keys,
8061da177e4SLinus Torvalds 			       sizeof(struct key *) * klist->nkeys);
807f70e2e06SDavid Howells 			nklist->delkey = klist->nkeys;
808f70e2e06SDavid Howells 			nklist->nkeys = klist->nkeys + 1;
8094be929beSAlexey Dobriyan 			klist->delkey = USHRT_MAX;
810f70e2e06SDavid Howells 		} else {
811f70e2e06SDavid Howells 			nklist->nkeys = 1;
812f70e2e06SDavid Howells 			nklist->delkey = 0;
8131da177e4SLinus Torvalds 		}
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds 		/* add the key into the new space */
816f70e2e06SDavid Howells 		nklist->keys[nklist->delkey] = NULL;
8171da177e4SLinus Torvalds 	}
8181da177e4SLinus Torvalds 
819ceb73c12SDavid Howells 	prealloc = (unsigned long)nklist | KEY_LINK_FIXQUOTA;
820cab8eb59SDavid Howells done:
821ceb73c12SDavid Howells 	*_prealloc = prealloc;
822f70e2e06SDavid Howells 	kleave(" = 0");
823f70e2e06SDavid Howells 	return 0;
8241da177e4SLinus Torvalds 
825f70e2e06SDavid Howells error_quota:
8261da177e4SLinus Torvalds 	/* undo the quota changes */
8271da177e4SLinus Torvalds 	key_payload_reserve(keyring,
8281da177e4SLinus Torvalds 			    keyring->datalen - KEYQUOTA_LINK_BYTES);
829f70e2e06SDavid Howells error_sem:
830f70e2e06SDavid Howells 	if (type == &key_type_keyring)
831f70e2e06SDavid Howells 		up_write(&keyring_serialise_link_sem);
832f70e2e06SDavid Howells error_krsem:
833f70e2e06SDavid Howells 	up_write(&keyring->sem);
834f70e2e06SDavid Howells 	kleave(" = %d", ret);
835f70e2e06SDavid Howells 	return ret;
836f70e2e06SDavid Howells }
8371da177e4SLinus Torvalds 
838f70e2e06SDavid Howells /*
839973c9f4fSDavid Howells  * Check already instantiated keys aren't going to be a problem.
840973c9f4fSDavid Howells  *
841973c9f4fSDavid Howells  * The caller must have called __key_link_begin(). Don't need to call this for
842973c9f4fSDavid Howells  * keys that were created since __key_link_begin() was called.
843f70e2e06SDavid Howells  */
844f70e2e06SDavid Howells int __key_link_check_live_key(struct key *keyring, struct key *key)
845f70e2e06SDavid Howells {
846f70e2e06SDavid Howells 	if (key->type == &key_type_keyring)
847f70e2e06SDavid Howells 		/* check that we aren't going to create a cycle by linking one
848f70e2e06SDavid Howells 		 * keyring to another */
849f70e2e06SDavid Howells 		return keyring_detect_cycle(keyring, key);
850f70e2e06SDavid Howells 	return 0;
851f70e2e06SDavid Howells }
8521da177e4SLinus Torvalds 
853f70e2e06SDavid Howells /*
854973c9f4fSDavid Howells  * Link a key into to a keyring.
855973c9f4fSDavid Howells  *
856973c9f4fSDavid Howells  * Must be called with __key_link_begin() having being called.  Discards any
857973c9f4fSDavid Howells  * already extant link to matching key if there is one, so that each keyring
858973c9f4fSDavid Howells  * holds at most one link to any given key of a particular type+description
859973c9f4fSDavid Howells  * combination.
860f70e2e06SDavid Howells  */
861f70e2e06SDavid Howells void __key_link(struct key *keyring, struct key *key,
862ceb73c12SDavid Howells 		unsigned long *_prealloc)
863f70e2e06SDavid Howells {
864f70e2e06SDavid Howells 	struct keyring_list *klist, *nklist;
865f70e2e06SDavid Howells 
866ceb73c12SDavid Howells 	nklist = (struct keyring_list *)(*_prealloc & ~KEY_LINK_FIXQUOTA);
867ceb73c12SDavid Howells 	*_prealloc = 0;
868f70e2e06SDavid Howells 
869f70e2e06SDavid Howells 	kenter("%d,%d,%p", keyring->serial, key->serial, nklist);
870f70e2e06SDavid Howells 
8716d528b08SDavid Howells 	klist = rcu_dereference_locked_keyring(keyring);
872f70e2e06SDavid Howells 
873f70e2e06SDavid Howells 	atomic_inc(&key->usage);
874f70e2e06SDavid Howells 
875f70e2e06SDavid Howells 	/* there's a matching key we can displace or an empty slot in a newly
876f70e2e06SDavid Howells 	 * allocated list we can fill */
877f70e2e06SDavid Howells 	if (nklist) {
878f70e2e06SDavid Howells 		kdebug("replace %hu/%hu/%hu",
879f70e2e06SDavid Howells 		       nklist->delkey, nklist->nkeys, nklist->maxkeys);
880f70e2e06SDavid Howells 
881f70e2e06SDavid Howells 		nklist->keys[nklist->delkey] = key;
882f70e2e06SDavid Howells 
883f70e2e06SDavid Howells 		rcu_assign_pointer(keyring->payload.subscriptions, nklist);
884f70e2e06SDavid Howells 
885f70e2e06SDavid Howells 		/* dispose of the old keyring list and, if there was one, the
886f70e2e06SDavid Howells 		 * displaced key */
887f70e2e06SDavid Howells 		if (klist) {
888f70e2e06SDavid Howells 			kdebug("dispose %hu/%hu/%hu",
889f70e2e06SDavid Howells 			       klist->delkey, klist->nkeys, klist->maxkeys);
890f70e2e06SDavid Howells 			call_rcu(&klist->rcu, keyring_unlink_rcu_disposal);
891f70e2e06SDavid Howells 		}
892f70e2e06SDavid Howells 	} else {
893f70e2e06SDavid Howells 		/* there's sufficient slack space to append directly */
894f70e2e06SDavid Howells 		klist->keys[klist->nkeys] = key;
895f70e2e06SDavid Howells 		smp_wmb();
896f70e2e06SDavid Howells 		klist->nkeys++;
897f70e2e06SDavid Howells 	}
898f70e2e06SDavid Howells }
899f70e2e06SDavid Howells 
900f70e2e06SDavid Howells /*
901973c9f4fSDavid Howells  * Finish linking a key into to a keyring.
902973c9f4fSDavid Howells  *
903973c9f4fSDavid Howells  * Must be called with __key_link_begin() having being called.
904f70e2e06SDavid Howells  */
905f70e2e06SDavid Howells void __key_link_end(struct key *keyring, struct key_type *type,
906ceb73c12SDavid Howells 		    unsigned long prealloc)
907f70e2e06SDavid Howells 	__releases(&keyring->sem)
908f70e2e06SDavid Howells {
909f70e2e06SDavid Howells 	BUG_ON(type == NULL);
910f70e2e06SDavid Howells 	BUG_ON(type->name == NULL);
911ceb73c12SDavid Howells 	kenter("%d,%s,%lx", keyring->serial, type->name, prealloc);
912f70e2e06SDavid Howells 
913f70e2e06SDavid Howells 	if (type == &key_type_keyring)
914f70e2e06SDavid Howells 		up_write(&keyring_serialise_link_sem);
915f70e2e06SDavid Howells 
916f70e2e06SDavid Howells 	if (prealloc) {
917ceb73c12SDavid Howells 		if (prealloc & KEY_LINK_FIXQUOTA)
918f70e2e06SDavid Howells 			key_payload_reserve(keyring,
919ceb73c12SDavid Howells 					    keyring->datalen -
920ceb73c12SDavid Howells 					    KEYQUOTA_LINK_BYTES);
921ceb73c12SDavid Howells 		kfree((struct keyring_list *)(prealloc & ~KEY_LINK_FIXQUOTA));
922f70e2e06SDavid Howells 	}
923f70e2e06SDavid Howells 	up_write(&keyring->sem);
924f70e2e06SDavid Howells }
925f70e2e06SDavid Howells 
926973c9f4fSDavid Howells /**
927973c9f4fSDavid Howells  * key_link - Link a key to a keyring
928973c9f4fSDavid Howells  * @keyring: The keyring to make the link in.
929973c9f4fSDavid Howells  * @key: The key to link to.
930973c9f4fSDavid Howells  *
931973c9f4fSDavid Howells  * Make a link in a keyring to a key, such that the keyring holds a reference
932973c9f4fSDavid Howells  * on that key and the key can potentially be found by searching that keyring.
933973c9f4fSDavid Howells  *
934973c9f4fSDavid Howells  * This function will write-lock the keyring's semaphore and will consume some
935973c9f4fSDavid Howells  * of the user's key data quota to hold the link.
936973c9f4fSDavid Howells  *
937973c9f4fSDavid Howells  * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring,
938973c9f4fSDavid Howells  * -EKEYREVOKED if the keyring has been revoked, -ENFILE if the keyring is
939973c9f4fSDavid Howells  * full, -EDQUOT if there is insufficient key data quota remaining to add
940973c9f4fSDavid Howells  * another link or -ENOMEM if there's insufficient memory.
941973c9f4fSDavid Howells  *
942973c9f4fSDavid Howells  * It is assumed that the caller has checked that it is permitted for a link to
943973c9f4fSDavid Howells  * be made (the keyring should have Write permission and the key Link
944973c9f4fSDavid Howells  * permission).
9451da177e4SLinus Torvalds  */
9461da177e4SLinus Torvalds int key_link(struct key *keyring, struct key *key)
9471da177e4SLinus Torvalds {
948ceb73c12SDavid Howells 	unsigned long prealloc;
9491da177e4SLinus Torvalds 	int ret;
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds 	key_check(keyring);
9521da177e4SLinus Torvalds 	key_check(key);
9531da177e4SLinus Torvalds 
954f70e2e06SDavid Howells 	ret = __key_link_begin(keyring, key->type, key->description, &prealloc);
955f70e2e06SDavid Howells 	if (ret == 0) {
956f70e2e06SDavid Howells 		ret = __key_link_check_live_key(keyring, key);
957f70e2e06SDavid Howells 		if (ret == 0)
958f70e2e06SDavid Howells 			__key_link(keyring, key, &prealloc);
959f70e2e06SDavid Howells 		__key_link_end(keyring, key->type, prealloc);
960f70e2e06SDavid Howells 	}
9611da177e4SLinus Torvalds 
9621da177e4SLinus Torvalds 	return ret;
963f70e2e06SDavid Howells }
9641da177e4SLinus Torvalds EXPORT_SYMBOL(key_link);
9651da177e4SLinus Torvalds 
966973c9f4fSDavid Howells /**
967973c9f4fSDavid Howells  * key_unlink - Unlink the first link to a key from a keyring.
968973c9f4fSDavid Howells  * @keyring: The keyring to remove the link from.
969973c9f4fSDavid Howells  * @key: The key the link is to.
970973c9f4fSDavid Howells  *
971973c9f4fSDavid Howells  * Remove a link from a keyring to a key.
972973c9f4fSDavid Howells  *
973973c9f4fSDavid Howells  * This function will write-lock the keyring's semaphore.
974973c9f4fSDavid Howells  *
975973c9f4fSDavid Howells  * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring, -ENOENT if
976973c9f4fSDavid Howells  * the key isn't linked to by the keyring or -ENOMEM if there's insufficient
977973c9f4fSDavid Howells  * memory.
978973c9f4fSDavid Howells  *
979973c9f4fSDavid Howells  * It is assumed that the caller has checked that it is permitted for a link to
980973c9f4fSDavid Howells  * be removed (the keyring should have Write permission; no permissions are
981973c9f4fSDavid Howells  * required on the key).
9821da177e4SLinus Torvalds  */
9831da177e4SLinus Torvalds int key_unlink(struct key *keyring, struct key *key)
9841da177e4SLinus Torvalds {
98576d8aeabSDavid Howells 	struct keyring_list *klist, *nklist;
9861da177e4SLinus Torvalds 	int loop, ret;
9871da177e4SLinus Torvalds 
9881da177e4SLinus Torvalds 	key_check(keyring);
9891da177e4SLinus Torvalds 	key_check(key);
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds 	ret = -ENOTDIR;
9921da177e4SLinus Torvalds 	if (keyring->type != &key_type_keyring)
9931da177e4SLinus Torvalds 		goto error;
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds 	down_write(&keyring->sem);
9961da177e4SLinus Torvalds 
997f0641cbaSDavid Howells 	klist = rcu_dereference_locked_keyring(keyring);
9981da177e4SLinus Torvalds 	if (klist) {
9991da177e4SLinus Torvalds 		/* search the keyring for the key */
10001da177e4SLinus Torvalds 		for (loop = 0; loop < klist->nkeys; loop++)
10011da177e4SLinus Torvalds 			if (klist->keys[loop] == key)
10021da177e4SLinus Torvalds 				goto key_is_present;
10031da177e4SLinus Torvalds 	}
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds 	up_write(&keyring->sem);
10061da177e4SLinus Torvalds 	ret = -ENOENT;
10071da177e4SLinus Torvalds 	goto error;
10081da177e4SLinus Torvalds 
10091da177e4SLinus Torvalds key_is_present:
101076d8aeabSDavid Howells 	/* we need to copy the key list for RCU purposes */
1011a4014d8fSDavid Howells 	nklist = kmalloc(sizeof(*klist) +
1012a4014d8fSDavid Howells 			 sizeof(struct key *) * klist->maxkeys,
101376d8aeabSDavid Howells 			 GFP_KERNEL);
101476d8aeabSDavid Howells 	if (!nklist)
101576d8aeabSDavid Howells 		goto nomem;
101676d8aeabSDavid Howells 	nklist->maxkeys = klist->maxkeys;
101776d8aeabSDavid Howells 	nklist->nkeys = klist->nkeys - 1;
101876d8aeabSDavid Howells 
101976d8aeabSDavid Howells 	if (loop > 0)
102076d8aeabSDavid Howells 		memcpy(&nklist->keys[0],
102176d8aeabSDavid Howells 		       &klist->keys[0],
1022a4014d8fSDavid Howells 		       loop * sizeof(struct key *));
102376d8aeabSDavid Howells 
102476d8aeabSDavid Howells 	if (loop < nklist->nkeys)
102576d8aeabSDavid Howells 		memcpy(&nklist->keys[loop],
102676d8aeabSDavid Howells 		       &klist->keys[loop + 1],
1027a4014d8fSDavid Howells 		       (nklist->nkeys - loop) * sizeof(struct key *));
102876d8aeabSDavid Howells 
10291da177e4SLinus Torvalds 	/* adjust the user's quota */
10301da177e4SLinus Torvalds 	key_payload_reserve(keyring,
10311da177e4SLinus Torvalds 			    keyring->datalen - KEYQUOTA_LINK_BYTES);
10321da177e4SLinus Torvalds 
103376d8aeabSDavid Howells 	rcu_assign_pointer(keyring->payload.subscriptions, nklist);
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds 	up_write(&keyring->sem);
103676d8aeabSDavid Howells 
103776d8aeabSDavid Howells 	/* schedule for later cleanup */
103876d8aeabSDavid Howells 	klist->delkey = loop;
103976d8aeabSDavid Howells 	call_rcu(&klist->rcu, keyring_unlink_rcu_disposal);
104076d8aeabSDavid Howells 
10411da177e4SLinus Torvalds 	ret = 0;
10421da177e4SLinus Torvalds 
10431da177e4SLinus Torvalds error:
10441da177e4SLinus Torvalds 	return ret;
104576d8aeabSDavid Howells nomem:
104676d8aeabSDavid Howells 	ret = -ENOMEM;
104776d8aeabSDavid Howells 	up_write(&keyring->sem);
104876d8aeabSDavid Howells 	goto error;
1049a8b17ed0SDavid Howells }
10501da177e4SLinus Torvalds EXPORT_SYMBOL(key_unlink);
10511da177e4SLinus Torvalds 
10521da177e4SLinus Torvalds /*
1053973c9f4fSDavid Howells  * Dispose of a keyring list after the RCU grace period, releasing the keys it
1054973c9f4fSDavid Howells  * links to.
105576d8aeabSDavid Howells  */
105676d8aeabSDavid Howells static void keyring_clear_rcu_disposal(struct rcu_head *rcu)
105776d8aeabSDavid Howells {
105876d8aeabSDavid Howells 	struct keyring_list *klist;
105976d8aeabSDavid Howells 	int loop;
106076d8aeabSDavid Howells 
106176d8aeabSDavid Howells 	klist = container_of(rcu, struct keyring_list, rcu);
106276d8aeabSDavid Howells 
106376d8aeabSDavid Howells 	for (loop = klist->nkeys - 1; loop >= 0; loop--)
106476d8aeabSDavid Howells 		key_put(klist->keys[loop]);
106576d8aeabSDavid Howells 
106676d8aeabSDavid Howells 	kfree(klist);
1067a8b17ed0SDavid Howells }
106876d8aeabSDavid Howells 
1069973c9f4fSDavid Howells /**
1070973c9f4fSDavid Howells  * keyring_clear - Clear a keyring
1071973c9f4fSDavid Howells  * @keyring: The keyring to clear.
1072973c9f4fSDavid Howells  *
1073973c9f4fSDavid Howells  * Clear the contents of the specified keyring.
1074973c9f4fSDavid Howells  *
1075973c9f4fSDavid Howells  * Returns 0 if successful or -ENOTDIR if the keyring isn't a keyring.
10761da177e4SLinus Torvalds  */
10771da177e4SLinus Torvalds int keyring_clear(struct key *keyring)
10781da177e4SLinus Torvalds {
10791da177e4SLinus Torvalds 	struct keyring_list *klist;
108076d8aeabSDavid Howells 	int ret;
10811da177e4SLinus Torvalds 
10821da177e4SLinus Torvalds 	ret = -ENOTDIR;
10831da177e4SLinus Torvalds 	if (keyring->type == &key_type_keyring) {
10841da177e4SLinus Torvalds 		/* detach the pointer block with the locks held */
10851da177e4SLinus Torvalds 		down_write(&keyring->sem);
10861da177e4SLinus Torvalds 
1087f0641cbaSDavid Howells 		klist = rcu_dereference_locked_keyring(keyring);
10881da177e4SLinus Torvalds 		if (klist) {
10891da177e4SLinus Torvalds 			/* adjust the quota */
10901da177e4SLinus Torvalds 			key_payload_reserve(keyring,
10911da177e4SLinus Torvalds 					    sizeof(struct keyring_list));
10921da177e4SLinus Torvalds 
109376d8aeabSDavid Howells 			rcu_assign_pointer(keyring->payload.subscriptions,
109476d8aeabSDavid Howells 					   NULL);
10951da177e4SLinus Torvalds 		}
10961da177e4SLinus Torvalds 
10971da177e4SLinus Torvalds 		up_write(&keyring->sem);
10981da177e4SLinus Torvalds 
10991da177e4SLinus Torvalds 		/* free the keys after the locks have been dropped */
110076d8aeabSDavid Howells 		if (klist)
110176d8aeabSDavid Howells 			call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
11021da177e4SLinus Torvalds 
11031da177e4SLinus Torvalds 		ret = 0;
11041da177e4SLinus Torvalds 	}
11051da177e4SLinus Torvalds 
11061da177e4SLinus Torvalds 	return ret;
1107a8b17ed0SDavid Howells }
11081da177e4SLinus Torvalds EXPORT_SYMBOL(keyring_clear);
110931204ed9SDavid Howells 
111031204ed9SDavid Howells /*
1111973c9f4fSDavid Howells  * Dispose of the links from a revoked keyring.
1112973c9f4fSDavid Howells  *
1113973c9f4fSDavid Howells  * This is called with the key sem write-locked.
111431204ed9SDavid Howells  */
111531204ed9SDavid Howells static void keyring_revoke(struct key *keyring)
111631204ed9SDavid Howells {
1117f0641cbaSDavid Howells 	struct keyring_list *klist;
1118f0641cbaSDavid Howells 
1119f0641cbaSDavid Howells 	klist = rcu_dereference_locked_keyring(keyring);
112031204ed9SDavid Howells 
112131204ed9SDavid Howells 	/* adjust the quota */
112231204ed9SDavid Howells 	key_payload_reserve(keyring, 0);
112331204ed9SDavid Howells 
112431204ed9SDavid Howells 	if (klist) {
112531204ed9SDavid Howells 		rcu_assign_pointer(keyring->payload.subscriptions, NULL);
112631204ed9SDavid Howells 		call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
112731204ed9SDavid Howells 	}
1128a8b17ed0SDavid Howells }
11295d135440SDavid Howells 
11305d135440SDavid Howells /*
1131973c9f4fSDavid Howells  * Determine whether a key is dead.
11325d135440SDavid Howells  */
11335d135440SDavid Howells static bool key_is_dead(struct key *key, time_t limit)
11345d135440SDavid Howells {
11355d135440SDavid Howells 	return test_bit(KEY_FLAG_DEAD, &key->flags) ||
11365d135440SDavid Howells 		(key->expiry > 0 && key->expiry <= limit);
11375d135440SDavid Howells }
11385d135440SDavid Howells 
11395d135440SDavid Howells /*
1140973c9f4fSDavid Howells  * Collect garbage from the contents of a keyring, replacing the old list with
1141973c9f4fSDavid Howells  * a new one with the pointers all shuffled down.
1142973c9f4fSDavid Howells  *
1143973c9f4fSDavid Howells  * Dead keys are classed as oned that are flagged as being dead or are revoked,
1144973c9f4fSDavid Howells  * expired or negative keys that were revoked or expired before the specified
1145973c9f4fSDavid Howells  * limit.
11465d135440SDavid Howells  */
11475d135440SDavid Howells void keyring_gc(struct key *keyring, time_t limit)
11485d135440SDavid Howells {
11495d135440SDavid Howells 	struct keyring_list *klist, *new;
11505d135440SDavid Howells 	struct key *key;
11515d135440SDavid Howells 	int loop, keep, max;
11525d135440SDavid Howells 
1153c08ef808SDavid Howells 	kenter("{%x,%s}", key_serial(keyring), keyring->description);
11545d135440SDavid Howells 
11555d135440SDavid Howells 	down_write(&keyring->sem);
11565d135440SDavid Howells 
1157f0641cbaSDavid Howells 	klist = rcu_dereference_locked_keyring(keyring);
11585d135440SDavid Howells 	if (!klist)
1159c08ef808SDavid Howells 		goto no_klist;
11605d135440SDavid Howells 
11615d135440SDavid Howells 	/* work out how many subscriptions we're keeping */
11625d135440SDavid Howells 	keep = 0;
11635d135440SDavid Howells 	for (loop = klist->nkeys - 1; loop >= 0; loop--)
1164c08ef808SDavid Howells 		if (!key_is_dead(klist->keys[loop], limit))
11655d135440SDavid Howells 			keep++;
11665d135440SDavid Howells 
11675d135440SDavid Howells 	if (keep == klist->nkeys)
11685d135440SDavid Howells 		goto just_return;
11695d135440SDavid Howells 
11705d135440SDavid Howells 	/* allocate a new keyring payload */
11715d135440SDavid Howells 	max = roundup(keep, 4);
11725d135440SDavid Howells 	new = kmalloc(sizeof(struct keyring_list) + max * sizeof(struct key *),
11735d135440SDavid Howells 		      GFP_KERNEL);
11745d135440SDavid Howells 	if (!new)
1175c08ef808SDavid Howells 		goto nomem;
11765d135440SDavid Howells 	new->maxkeys = max;
11775d135440SDavid Howells 	new->nkeys = 0;
11785d135440SDavid Howells 	new->delkey = 0;
11795d135440SDavid Howells 
11805d135440SDavid Howells 	/* install the live keys
11815d135440SDavid Howells 	 * - must take care as expired keys may be updated back to life
11825d135440SDavid Howells 	 */
11835d135440SDavid Howells 	keep = 0;
11845d135440SDavid Howells 	for (loop = klist->nkeys - 1; loop >= 0; loop--) {
11855d135440SDavid Howells 		key = klist->keys[loop];
11865d135440SDavid Howells 		if (!key_is_dead(key, limit)) {
11875d135440SDavid Howells 			if (keep >= max)
11885d135440SDavid Howells 				goto discard_new;
11895d135440SDavid Howells 			new->keys[keep++] = key_get(key);
11905d135440SDavid Howells 		}
11915d135440SDavid Howells 	}
11925d135440SDavid Howells 	new->nkeys = keep;
11935d135440SDavid Howells 
11945d135440SDavid Howells 	/* adjust the quota */
11955d135440SDavid Howells 	key_payload_reserve(keyring,
11965d135440SDavid Howells 			    sizeof(struct keyring_list) +
11975d135440SDavid Howells 			    KEYQUOTA_LINK_BYTES * keep);
11985d135440SDavid Howells 
11995d135440SDavid Howells 	if (keep == 0) {
12005d135440SDavid Howells 		rcu_assign_pointer(keyring->payload.subscriptions, NULL);
12015d135440SDavid Howells 		kfree(new);
12025d135440SDavid Howells 	} else {
12035d135440SDavid Howells 		rcu_assign_pointer(keyring->payload.subscriptions, new);
12045d135440SDavid Howells 	}
12055d135440SDavid Howells 
12065d135440SDavid Howells 	up_write(&keyring->sem);
12075d135440SDavid Howells 
12085d135440SDavid Howells 	call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
12095d135440SDavid Howells 	kleave(" [yes]");
12105d135440SDavid Howells 	return;
12115d135440SDavid Howells 
12125d135440SDavid Howells discard_new:
12135d135440SDavid Howells 	new->nkeys = keep;
12145d135440SDavid Howells 	keyring_clear_rcu_disposal(&new->rcu);
1215c08ef808SDavid Howells 	up_write(&keyring->sem);
1216c08ef808SDavid Howells 	kleave(" [discard]");
1217c08ef808SDavid Howells 	return;
1218c08ef808SDavid Howells 
12195d135440SDavid Howells just_return:
12205d135440SDavid Howells 	up_write(&keyring->sem);
1221c08ef808SDavid Howells 	kleave(" [no dead]");
1222c08ef808SDavid Howells 	return;
1223c08ef808SDavid Howells 
1224c08ef808SDavid Howells no_klist:
1225c08ef808SDavid Howells 	up_write(&keyring->sem);
1226c08ef808SDavid Howells 	kleave(" [no_klist]");
1227c08ef808SDavid Howells 	return;
1228c08ef808SDavid Howells 
1229c08ef808SDavid Howells nomem:
1230c08ef808SDavid Howells 	up_write(&keyring->sem);
1231c08ef808SDavid Howells 	kleave(" [oom]");
12325d135440SDavid Howells }
1233