xref: /openbmc/linux/security/keys/keyring.c (revision cab8eb594e84b434d20412fc5a3985b0bee3ab9f)
11da177e4SLinus Torvalds /* keyring.c: keyring handling
21da177e4SLinus Torvalds  *
33e30148cSDavid Howells  * Copyright (C) 2004-5 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>
191da177e4SLinus Torvalds #include <asm/uaccess.h>
201da177e4SLinus Torvalds #include "internal.h"
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds /*
231da177e4SLinus Torvalds  * when plumbing the depths of the key tree, this sets a hard limit set on how
241da177e4SLinus Torvalds  * deep we're willing to go
251da177e4SLinus Torvalds  */
261da177e4SLinus Torvalds #define KEYRING_SEARCH_MAX_DEPTH 6
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds /*
291da177e4SLinus Torvalds  * we keep all named keyrings in a hash to speed looking them up
301da177e4SLinus Torvalds  */
311da177e4SLinus Torvalds #define KEYRING_NAME_HASH_SIZE	(1 << 5)
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds static struct list_head	keyring_name_hash[KEYRING_NAME_HASH_SIZE];
341da177e4SLinus Torvalds static DEFINE_RWLOCK(keyring_name_lock);
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds static inline unsigned keyring_hash(const char *desc)
371da177e4SLinus Torvalds {
381da177e4SLinus Torvalds 	unsigned bucket = 0;
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds 	for (; *desc; desc++)
411da177e4SLinus Torvalds 		bucket += (unsigned char) *desc;
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds 	return bucket & (KEYRING_NAME_HASH_SIZE - 1);
441da177e4SLinus Torvalds }
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds /*
471da177e4SLinus Torvalds  * the keyring type definition
481da177e4SLinus Torvalds  */
491da177e4SLinus Torvalds static int keyring_instantiate(struct key *keyring,
501da177e4SLinus Torvalds 			       const void *data, size_t datalen);
511da177e4SLinus Torvalds static int keyring_match(const struct key *keyring, const void *criterion);
521da177e4SLinus Torvalds static void keyring_destroy(struct key *keyring);
531da177e4SLinus Torvalds static void keyring_describe(const struct key *keyring, struct seq_file *m);
541da177e4SLinus Torvalds static long keyring_read(const struct key *keyring,
551da177e4SLinus Torvalds 			 char __user *buffer, size_t buflen);
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds struct key_type key_type_keyring = {
581da177e4SLinus Torvalds 	.name		= "keyring",
591da177e4SLinus Torvalds 	.def_datalen	= sizeof(struct keyring_list),
601da177e4SLinus Torvalds 	.instantiate	= keyring_instantiate,
611da177e4SLinus Torvalds 	.match		= keyring_match,
621da177e4SLinus Torvalds 	.destroy	= keyring_destroy,
631da177e4SLinus Torvalds 	.describe	= keyring_describe,
641da177e4SLinus Torvalds 	.read		= keyring_read,
651da177e4SLinus Torvalds };
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds /*
681da177e4SLinus Torvalds  * semaphore to serialise link/link calls to prevent two link calls in parallel
691da177e4SLinus Torvalds  * introducing a cycle
701da177e4SLinus Torvalds  */
711ae8f407SAdrian Bunk static DECLARE_RWSEM(keyring_serialise_link_sem);
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds /*****************************************************************************/
741da177e4SLinus Torvalds /*
751da177e4SLinus Torvalds  * publish the name of a keyring so that it can be found by name (if it has
761da177e4SLinus Torvalds  * one)
771da177e4SLinus Torvalds  */
781da177e4SLinus Torvalds void keyring_publish_name(struct key *keyring)
791da177e4SLinus Torvalds {
801da177e4SLinus Torvalds 	int bucket;
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds 	if (keyring->description) {
831da177e4SLinus Torvalds 		bucket = keyring_hash(keyring->description);
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds 		write_lock(&keyring_name_lock);
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds 		if (!keyring_name_hash[bucket].next)
881da177e4SLinus Torvalds 			INIT_LIST_HEAD(&keyring_name_hash[bucket]);
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds 		list_add_tail(&keyring->type_data.link,
911da177e4SLinus Torvalds 			      &keyring_name_hash[bucket]);
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds 		write_unlock(&keyring_name_lock);
941da177e4SLinus Torvalds 	}
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds } /* end keyring_publish_name() */
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds /*****************************************************************************/
991da177e4SLinus Torvalds /*
1001da177e4SLinus Torvalds  * initialise a keyring
1011da177e4SLinus Torvalds  * - we object if we were given any data
1021da177e4SLinus Torvalds  */
1031da177e4SLinus Torvalds static int keyring_instantiate(struct key *keyring,
1041da177e4SLinus Torvalds 			       const void *data, size_t datalen)
1051da177e4SLinus Torvalds {
1061da177e4SLinus Torvalds 	int ret;
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds 	ret = -EINVAL;
1091da177e4SLinus Torvalds 	if (datalen == 0) {
1101da177e4SLinus Torvalds 		/* make the keyring available by name if it has one */
1111da177e4SLinus Torvalds 		keyring_publish_name(keyring);
1121da177e4SLinus Torvalds 		ret = 0;
1131da177e4SLinus Torvalds 	}
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds 	return ret;
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds } /* end keyring_instantiate() */
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds /*****************************************************************************/
1201da177e4SLinus Torvalds /*
1211da177e4SLinus Torvalds  * match keyrings on their name
1221da177e4SLinus Torvalds  */
1231da177e4SLinus Torvalds static int keyring_match(const struct key *keyring, const void *description)
1241da177e4SLinus Torvalds {
1251da177e4SLinus Torvalds 	return keyring->description &&
1261da177e4SLinus Torvalds 		strcmp(keyring->description, description) == 0;
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds } /* end keyring_match() */
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds /*****************************************************************************/
1311da177e4SLinus Torvalds /*
1321da177e4SLinus Torvalds  * dispose of the data dangling from the corpse of a keyring
1331da177e4SLinus Torvalds  */
1341da177e4SLinus Torvalds static void keyring_destroy(struct key *keyring)
1351da177e4SLinus Torvalds {
1361da177e4SLinus Torvalds 	struct keyring_list *klist;
1371da177e4SLinus Torvalds 	int loop;
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds 	if (keyring->description) {
1401da177e4SLinus Torvalds 		write_lock(&keyring_name_lock);
14194efe72fSDavid Howells 
14294efe72fSDavid Howells 		if (keyring->type_data.link.next != NULL &&
14394efe72fSDavid Howells 		    !list_empty(&keyring->type_data.link))
1441da177e4SLinus Torvalds 			list_del(&keyring->type_data.link);
14594efe72fSDavid Howells 
1461da177e4SLinus Torvalds 		write_unlock(&keyring_name_lock);
1471da177e4SLinus Torvalds 	}
1481da177e4SLinus Torvalds 
14976d8aeabSDavid Howells 	klist = rcu_dereference(keyring->payload.subscriptions);
1501da177e4SLinus Torvalds 	if (klist) {
1511da177e4SLinus Torvalds 		for (loop = klist->nkeys - 1; loop >= 0; loop--)
1521da177e4SLinus Torvalds 			key_put(klist->keys[loop]);
1531da177e4SLinus Torvalds 		kfree(klist);
1541da177e4SLinus Torvalds 	}
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds } /* end keyring_destroy() */
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds /*****************************************************************************/
1591da177e4SLinus Torvalds /*
1601da177e4SLinus Torvalds  * describe the keyring
1611da177e4SLinus Torvalds  */
1621da177e4SLinus Torvalds static void keyring_describe(const struct key *keyring, struct seq_file *m)
1631da177e4SLinus Torvalds {
1641da177e4SLinus Torvalds 	struct keyring_list *klist;
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 	if (keyring->description) {
1671da177e4SLinus Torvalds 		seq_puts(m, keyring->description);
1681da177e4SLinus Torvalds 	}
1691da177e4SLinus Torvalds 	else {
1701da177e4SLinus Torvalds 		seq_puts(m, "[anon]");
1711da177e4SLinus Torvalds 	}
1721da177e4SLinus Torvalds 
17376d8aeabSDavid Howells 	rcu_read_lock();
17476d8aeabSDavid Howells 	klist = rcu_dereference(keyring->payload.subscriptions);
1751da177e4SLinus Torvalds 	if (klist)
1761da177e4SLinus Torvalds 		seq_printf(m, ": %u/%u", klist->nkeys, klist->maxkeys);
1771da177e4SLinus Torvalds 	else
1781da177e4SLinus Torvalds 		seq_puts(m, ": empty");
17976d8aeabSDavid Howells 	rcu_read_unlock();
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds } /* end keyring_describe() */
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds /*****************************************************************************/
1841da177e4SLinus Torvalds /*
1851da177e4SLinus Torvalds  * read a list of key IDs from the keyring's contents
18676d8aeabSDavid Howells  * - the keyring's semaphore is read-locked
1871da177e4SLinus Torvalds  */
1881da177e4SLinus Torvalds static long keyring_read(const struct key *keyring,
1891da177e4SLinus Torvalds 			 char __user *buffer, size_t buflen)
1901da177e4SLinus Torvalds {
1911da177e4SLinus Torvalds 	struct keyring_list *klist;
1921da177e4SLinus Torvalds 	struct key *key;
1931da177e4SLinus Torvalds 	size_t qty, tmp;
1941da177e4SLinus Torvalds 	int loop, ret;
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds 	ret = 0;
19776d8aeabSDavid Howells 	klist = rcu_dereference(keyring->payload.subscriptions);
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds 	if (klist) {
2001da177e4SLinus Torvalds 		/* calculate how much data we could return */
2011da177e4SLinus Torvalds 		qty = klist->nkeys * sizeof(key_serial_t);
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds 		if (buffer && buflen > 0) {
2041da177e4SLinus Torvalds 			if (buflen > qty)
2051da177e4SLinus Torvalds 				buflen = qty;
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds 			/* copy the IDs of the subscribed keys into the
2081da177e4SLinus Torvalds 			 * buffer */
2091da177e4SLinus Torvalds 			ret = -EFAULT;
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds 			for (loop = 0; loop < klist->nkeys; loop++) {
2121da177e4SLinus Torvalds 				key = klist->keys[loop];
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 				tmp = sizeof(key_serial_t);
2151da177e4SLinus Torvalds 				if (tmp > buflen)
2161da177e4SLinus Torvalds 					tmp = buflen;
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds 				if (copy_to_user(buffer,
2191da177e4SLinus Torvalds 						 &key->serial,
2201da177e4SLinus Torvalds 						 tmp) != 0)
2211da177e4SLinus Torvalds 					goto error;
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds 				buflen -= tmp;
2241da177e4SLinus Torvalds 				if (buflen == 0)
2251da177e4SLinus Torvalds 					break;
2261da177e4SLinus Torvalds 				buffer += tmp;
2271da177e4SLinus Torvalds 			}
2281da177e4SLinus Torvalds 		}
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds 		ret = qty;
2311da177e4SLinus Torvalds 	}
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds  error:
2341da177e4SLinus Torvalds 	return ret;
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds } /* end keyring_read() */
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds /*****************************************************************************/
2391da177e4SLinus Torvalds /*
2401da177e4SLinus Torvalds  * allocate a keyring and link into the destination keyring
2411da177e4SLinus Torvalds  */
2421da177e4SLinus Torvalds struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
2431da177e4SLinus Torvalds 			  int not_in_quota, struct key *dest)
2441da177e4SLinus Torvalds {
2451da177e4SLinus Torvalds 	struct key *keyring;
2461da177e4SLinus Torvalds 	int ret;
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds 	keyring = key_alloc(&key_type_keyring, description,
24929db9190SDavid Howells 			    uid, gid,
25029db9190SDavid Howells 			    (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
25129db9190SDavid Howells 			    not_in_quota);
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds 	if (!IS_ERR(keyring)) {
2543e30148cSDavid Howells 		ret = key_instantiate_and_link(keyring, NULL, 0, dest, NULL);
2551da177e4SLinus Torvalds 		if (ret < 0) {
2561da177e4SLinus Torvalds 			key_put(keyring);
2571da177e4SLinus Torvalds 			keyring = ERR_PTR(ret);
2581da177e4SLinus Torvalds 		}
2591da177e4SLinus Torvalds 	}
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 	return keyring;
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds } /* end keyring_alloc() */
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds /*****************************************************************************/
2661da177e4SLinus Torvalds /*
2671da177e4SLinus Torvalds  * search the supplied keyring tree for a key that matches the criterion
2681da177e4SLinus Torvalds  * - perform a breadth-then-depth search up to the prescribed limit
2691da177e4SLinus Torvalds  * - we only find keys on which we have search permission
2701da177e4SLinus Torvalds  * - we use the supplied match function to see if the description (or other
2711da177e4SLinus Torvalds  *   feature of interest) matches
2723e30148cSDavid Howells  * - we rely on RCU to prevent the keyring lists from disappearing on us
2731da177e4SLinus Torvalds  * - we return -EAGAIN if we didn't find any matching key
2741da177e4SLinus Torvalds  * - we return -ENOKEY if we only found negative matching keys
275664cceb0SDavid Howells  * - we propagate the possession attribute from the keyring ref to the key ref
2761da177e4SLinus Torvalds  */
277664cceb0SDavid Howells key_ref_t keyring_search_aux(key_ref_t keyring_ref,
2783e30148cSDavid Howells 			     struct task_struct *context,
2791da177e4SLinus Torvalds 			     struct key_type *type,
2801da177e4SLinus Torvalds 			     const void *description,
2811da177e4SLinus Torvalds 			     key_match_func_t match)
2821da177e4SLinus Torvalds {
2831da177e4SLinus Torvalds 	struct {
28476d8aeabSDavid Howells 		struct keyring_list *keylist;
2851da177e4SLinus Torvalds 		int kix;
2861da177e4SLinus Torvalds 	} stack[KEYRING_SEARCH_MAX_DEPTH];
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds 	struct keyring_list *keylist;
2891da177e4SLinus Torvalds 	struct timespec now;
290664cceb0SDavid Howells 	unsigned long possessed;
291664cceb0SDavid Howells 	struct key *keyring, *key;
292664cceb0SDavid Howells 	key_ref_t key_ref;
2931da177e4SLinus Torvalds 	long err;
29476d8aeabSDavid Howells 	int sp, kix;
2951da177e4SLinus Torvalds 
296664cceb0SDavid Howells 	keyring = key_ref_to_ptr(keyring_ref);
297664cceb0SDavid Howells 	possessed = is_key_possessed(keyring_ref);
2981da177e4SLinus Torvalds 	key_check(keyring);
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds 	/* top keyring must have search permission to begin the search */
30129db9190SDavid Howells         err = key_task_permission(keyring_ref, context, KEY_SEARCH);
30229db9190SDavid Howells 	if (err < 0) {
30329db9190SDavid Howells 		key_ref = ERR_PTR(err);
3041da177e4SLinus Torvalds 		goto error;
30529db9190SDavid Howells 	}
3061da177e4SLinus Torvalds 
307664cceb0SDavid Howells 	key_ref = ERR_PTR(-ENOTDIR);
3081da177e4SLinus Torvalds 	if (keyring->type != &key_type_keyring)
3091da177e4SLinus Torvalds 		goto error;
3101da177e4SLinus Torvalds 
311664cceb0SDavid Howells 	rcu_read_lock();
312664cceb0SDavid Howells 
3131da177e4SLinus Torvalds 	now = current_kernel_time();
3141da177e4SLinus Torvalds 	err = -EAGAIN;
3151da177e4SLinus Torvalds 	sp = 0;
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds 	/* start processing a new keyring */
3181da177e4SLinus Torvalds descend:
31976d8aeabSDavid Howells 	if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
3201da177e4SLinus Torvalds 		goto not_this_keyring;
3211da177e4SLinus Torvalds 
32276d8aeabSDavid Howells 	keylist = rcu_dereference(keyring->payload.subscriptions);
3231da177e4SLinus Torvalds 	if (!keylist)
3241da177e4SLinus Torvalds 		goto not_this_keyring;
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 	/* iterate through the keys in this keyring first */
3271da177e4SLinus Torvalds 	for (kix = 0; kix < keylist->nkeys; kix++) {
3281da177e4SLinus Torvalds 		key = keylist->keys[kix];
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 		/* ignore keys not of this type */
3311da177e4SLinus Torvalds 		if (key->type != type)
3321da177e4SLinus Torvalds 			continue;
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 		/* skip revoked keys and expired keys */
33576d8aeabSDavid Howells 		if (test_bit(KEY_FLAG_REVOKED, &key->flags))
3361da177e4SLinus Torvalds 			continue;
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds 		if (key->expiry && now.tv_sec >= key->expiry)
3391da177e4SLinus Torvalds 			continue;
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 		/* keys that don't match */
3421da177e4SLinus Torvalds 		if (!match(key, description))
3431da177e4SLinus Torvalds 			continue;
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 		/* key must have search permissions */
34629db9190SDavid Howells 		if (key_task_permission(make_key_ref(key, possessed),
34729db9190SDavid Howells 					context, KEY_SEARCH) < 0)
3481da177e4SLinus Torvalds 			continue;
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds 		/* we set a different error code if we find a negative key */
35176d8aeabSDavid Howells 		if (test_bit(KEY_FLAG_NEGATIVE, &key->flags)) {
3521da177e4SLinus Torvalds 			err = -ENOKEY;
3531da177e4SLinus Torvalds 			continue;
3541da177e4SLinus Torvalds 		}
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds 		goto found;
3571da177e4SLinus Torvalds 	}
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds 	/* search through the keyrings nested in this one */
3601da177e4SLinus Torvalds 	kix = 0;
3611da177e4SLinus Torvalds ascend:
36276d8aeabSDavid Howells 	for (; kix < keylist->nkeys; kix++) {
3631da177e4SLinus Torvalds 		key = keylist->keys[kix];
3641da177e4SLinus Torvalds 		if (key->type != &key_type_keyring)
36576d8aeabSDavid Howells 			continue;
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 		/* recursively search nested keyrings
3681da177e4SLinus Torvalds 		 * - only search keyrings for which we have search permission
3691da177e4SLinus Torvalds 		 */
3701da177e4SLinus Torvalds 		if (sp >= KEYRING_SEARCH_MAX_DEPTH)
37176d8aeabSDavid Howells 			continue;
3721da177e4SLinus Torvalds 
3730f6ed7c2SDavid Howells 		if (key_task_permission(make_key_ref(key, possessed),
37429db9190SDavid Howells 					context, KEY_SEARCH) < 0)
37576d8aeabSDavid Howells 			continue;
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds 		/* stack the current position */
37876d8aeabSDavid Howells 		stack[sp].keylist = keylist;
3791da177e4SLinus Torvalds 		stack[sp].kix = kix;
3801da177e4SLinus Torvalds 		sp++;
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 		/* begin again with the new keyring */
3831da177e4SLinus Torvalds 		keyring = key;
3841da177e4SLinus Torvalds 		goto descend;
3851da177e4SLinus Torvalds 	}
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 	/* the keyring we're looking at was disqualified or didn't contain a
3881da177e4SLinus Torvalds 	 * matching key */
3891da177e4SLinus Torvalds not_this_keyring:
3901da177e4SLinus Torvalds 	if (sp > 0) {
3911da177e4SLinus Torvalds 		/* resume the processing of a keyring higher up in the tree */
3921da177e4SLinus Torvalds 		sp--;
39376d8aeabSDavid Howells 		keylist = stack[sp].keylist;
3941da177e4SLinus Torvalds 		kix = stack[sp].kix + 1;
3951da177e4SLinus Torvalds 		goto ascend;
3961da177e4SLinus Torvalds 	}
3971da177e4SLinus Torvalds 
398664cceb0SDavid Howells 	key_ref = ERR_PTR(err);
399664cceb0SDavid Howells 	goto error_2;
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 	/* we found a viable match */
4021da177e4SLinus Torvalds found:
4031da177e4SLinus Torvalds 	atomic_inc(&key->usage);
4041da177e4SLinus Torvalds 	key_check(key);
405664cceb0SDavid Howells 	key_ref = make_key_ref(key, possessed);
406664cceb0SDavid Howells error_2:
40776d8aeabSDavid Howells 	rcu_read_unlock();
408664cceb0SDavid Howells error:
409664cceb0SDavid Howells 	return key_ref;
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds } /* end keyring_search_aux() */
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds /*****************************************************************************/
4141da177e4SLinus Torvalds /*
4151da177e4SLinus Torvalds  * search the supplied keyring tree for a key that matches the criterion
4161da177e4SLinus Torvalds  * - perform a breadth-then-depth search up to the prescribed limit
4171da177e4SLinus Torvalds  * - we only find keys on which we have search permission
4181da177e4SLinus Torvalds  * - we readlock the keyrings as we search down the tree
4191da177e4SLinus Torvalds  * - we return -EAGAIN if we didn't find any matching key
4201da177e4SLinus Torvalds  * - we return -ENOKEY if we only found negative matching keys
4211da177e4SLinus Torvalds  */
422664cceb0SDavid Howells key_ref_t keyring_search(key_ref_t keyring,
4231da177e4SLinus Torvalds 			 struct key_type *type,
4241da177e4SLinus Torvalds 			 const char *description)
4251da177e4SLinus Torvalds {
4263e30148cSDavid Howells 	if (!type->match)
4273e30148cSDavid Howells 		return ERR_PTR(-ENOKEY);
4283e30148cSDavid Howells 
4293e30148cSDavid Howells 	return keyring_search_aux(keyring, current,
4303e30148cSDavid Howells 				  type, description, type->match);
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds } /* end keyring_search() */
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds EXPORT_SYMBOL(keyring_search);
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds /*****************************************************************************/
4371da177e4SLinus Torvalds /*
4381da177e4SLinus Torvalds  * search the given keyring only (no recursion)
4391da177e4SLinus Torvalds  * - keyring must be locked by caller
4401da177e4SLinus Torvalds  */
441664cceb0SDavid Howells key_ref_t __keyring_search_one(key_ref_t keyring_ref,
4421da177e4SLinus Torvalds 			       const struct key_type *ktype,
4431da177e4SLinus Torvalds 			       const char *description,
4441da177e4SLinus Torvalds 			       key_perm_t perm)
4451da177e4SLinus Torvalds {
4461da177e4SLinus Torvalds 	struct keyring_list *klist;
447664cceb0SDavid Howells 	unsigned long possessed;
448664cceb0SDavid Howells 	struct key *keyring, *key;
4491da177e4SLinus Torvalds 	int loop;
4501da177e4SLinus Torvalds 
451664cceb0SDavid Howells 	keyring = key_ref_to_ptr(keyring_ref);
452664cceb0SDavid Howells 	possessed = is_key_possessed(keyring_ref);
453664cceb0SDavid Howells 
45476d8aeabSDavid Howells 	rcu_read_lock();
45576d8aeabSDavid Howells 
45676d8aeabSDavid Howells 	klist = rcu_dereference(keyring->payload.subscriptions);
4571da177e4SLinus Torvalds 	if (klist) {
4581da177e4SLinus Torvalds 		for (loop = 0; loop < klist->nkeys; loop++) {
4591da177e4SLinus Torvalds 			key = klist->keys[loop];
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds 			if (key->type == ktype &&
4623e30148cSDavid Howells 			    (!key->type->match ||
4633e30148cSDavid Howells 			     key->type->match(key, description)) &&
464664cceb0SDavid Howells 			    key_permission(make_key_ref(key, possessed),
465db1d1d57SDavid Howells 					   perm) == 0 &&
46676d8aeabSDavid Howells 			    !test_bit(KEY_FLAG_REVOKED, &key->flags)
4671da177e4SLinus Torvalds 			    )
4681da177e4SLinus Torvalds 				goto found;
4691da177e4SLinus Torvalds 		}
4701da177e4SLinus Torvalds 	}
4711da177e4SLinus Torvalds 
472664cceb0SDavid Howells 	rcu_read_unlock();
473664cceb0SDavid Howells 	return ERR_PTR(-ENOKEY);
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds  found:
4761da177e4SLinus Torvalds 	atomic_inc(&key->usage);
47776d8aeabSDavid Howells 	rcu_read_unlock();
478664cceb0SDavid Howells 	return make_key_ref(key, possessed);
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds } /* end __keyring_search_one() */
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds /*****************************************************************************/
4831da177e4SLinus Torvalds /*
4843e30148cSDavid Howells  * search for an instantiation authorisation key matching a target key
4853e30148cSDavid Howells  * - the RCU read lock must be held by the caller
4863e30148cSDavid Howells  * - a target_id of zero specifies any valid token
4873e30148cSDavid Howells  */
4883e30148cSDavid Howells struct key *keyring_search_instkey(struct key *keyring,
4893e30148cSDavid Howells 				   key_serial_t target_id)
4903e30148cSDavid Howells {
4913e30148cSDavid Howells 	struct request_key_auth *rka;
4923e30148cSDavid Howells 	struct keyring_list *klist;
4933e30148cSDavid Howells 	struct key *instkey;
4943e30148cSDavid Howells 	int loop;
4953e30148cSDavid Howells 
4963e30148cSDavid Howells 	klist = rcu_dereference(keyring->payload.subscriptions);
4973e30148cSDavid Howells 	if (klist) {
4983e30148cSDavid Howells 		for (loop = 0; loop < klist->nkeys; loop++) {
4993e30148cSDavid Howells 			instkey = klist->keys[loop];
5003e30148cSDavid Howells 
5013e30148cSDavid Howells 			if (instkey->type != &key_type_request_key_auth)
5023e30148cSDavid Howells 				continue;
5033e30148cSDavid Howells 
5043e30148cSDavid Howells 			rka = instkey->payload.data;
5053e30148cSDavid Howells 			if (target_id && rka->target_key->serial != target_id)
5063e30148cSDavid Howells 				continue;
5073e30148cSDavid Howells 
5083e30148cSDavid Howells 			/* the auth key is revoked during instantiation */
5093e30148cSDavid Howells 			if (!test_bit(KEY_FLAG_REVOKED, &instkey->flags))
5103e30148cSDavid Howells 				goto found;
5113e30148cSDavid Howells 
5123e30148cSDavid Howells 			instkey = ERR_PTR(-EKEYREVOKED);
5133e30148cSDavid Howells 			goto error;
5143e30148cSDavid Howells 		}
5153e30148cSDavid Howells 	}
5163e30148cSDavid Howells 
5173e30148cSDavid Howells 	instkey = ERR_PTR(-EACCES);
5183e30148cSDavid Howells 	goto error;
5193e30148cSDavid Howells 
5203e30148cSDavid Howells found:
5213e30148cSDavid Howells 	atomic_inc(&instkey->usage);
5223e30148cSDavid Howells error:
5233e30148cSDavid Howells 	return instkey;
5243e30148cSDavid Howells 
5253e30148cSDavid Howells } /* end keyring_search_instkey() */
5263e30148cSDavid Howells 
5273e30148cSDavid Howells /*****************************************************************************/
5283e30148cSDavid Howells /*
5291da177e4SLinus Torvalds  * find a keyring with the specified name
5301da177e4SLinus Torvalds  * - all named keyrings are searched
5311da177e4SLinus Torvalds  * - only find keyrings with search permission for the process
5321da177e4SLinus Torvalds  * - only find keyrings with a serial number greater than the one specified
5331da177e4SLinus Torvalds  */
5341da177e4SLinus Torvalds struct key *find_keyring_by_name(const char *name, key_serial_t bound)
5351da177e4SLinus Torvalds {
5361da177e4SLinus Torvalds 	struct key *keyring;
5371da177e4SLinus Torvalds 	int bucket;
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds 	keyring = ERR_PTR(-EINVAL);
5401da177e4SLinus Torvalds 	if (!name)
5411da177e4SLinus Torvalds 		goto error;
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds 	bucket = keyring_hash(name);
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds 	read_lock(&keyring_name_lock);
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds 	if (keyring_name_hash[bucket].next) {
5481da177e4SLinus Torvalds 		/* search this hash bucket for a keyring with a matching name
5491da177e4SLinus Torvalds 		 * that's readable and that hasn't been revoked */
5501da177e4SLinus Torvalds 		list_for_each_entry(keyring,
5511da177e4SLinus Torvalds 				    &keyring_name_hash[bucket],
5521da177e4SLinus Torvalds 				    type_data.link
5531da177e4SLinus Torvalds 				    ) {
55476d8aeabSDavid Howells 			if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
5551da177e4SLinus Torvalds 				continue;
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds 			if (strcmp(keyring->description, name) != 0)
5581da177e4SLinus Torvalds 				continue;
5591da177e4SLinus Torvalds 
5600f6ed7c2SDavid Howells 			if (key_permission(make_key_ref(keyring, 0),
56129db9190SDavid Howells 					   KEY_SEARCH) < 0)
5621da177e4SLinus Torvalds 				continue;
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds 			/* found a potential candidate, but we still need to
5651da177e4SLinus Torvalds 			 * check the serial number */
5661da177e4SLinus Torvalds 			if (keyring->serial <= bound)
5671da177e4SLinus Torvalds 				continue;
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds 			/* we've got a match */
5701da177e4SLinus Torvalds 			atomic_inc(&keyring->usage);
5711da177e4SLinus Torvalds 			read_unlock(&keyring_name_lock);
5721da177e4SLinus Torvalds 			goto error;
5731da177e4SLinus Torvalds 		}
5741da177e4SLinus Torvalds 	}
5751da177e4SLinus Torvalds 
5761da177e4SLinus Torvalds 	read_unlock(&keyring_name_lock);
5771da177e4SLinus Torvalds 	keyring = ERR_PTR(-ENOKEY);
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds  error:
5801da177e4SLinus Torvalds 	return keyring;
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds } /* end find_keyring_by_name() */
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds /*****************************************************************************/
5851da177e4SLinus Torvalds /*
5861da177e4SLinus Torvalds  * see if a cycle will will be created by inserting acyclic tree B in acyclic
5871da177e4SLinus Torvalds  * tree A at the topmost level (ie: as a direct child of A)
5881da177e4SLinus Torvalds  * - since we are adding B to A at the top level, checking for cycles should
5891da177e4SLinus Torvalds  *   just be a matter of seeing if node A is somewhere in tree B
5901da177e4SLinus Torvalds  */
5911da177e4SLinus Torvalds static int keyring_detect_cycle(struct key *A, struct key *B)
5921da177e4SLinus Torvalds {
5931da177e4SLinus Torvalds 	struct {
59476d8aeabSDavid Howells 		struct keyring_list *keylist;
5951da177e4SLinus Torvalds 		int kix;
5961da177e4SLinus Torvalds 	} stack[KEYRING_SEARCH_MAX_DEPTH];
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds 	struct keyring_list *keylist;
5991da177e4SLinus Torvalds 	struct key *subtree, *key;
6001da177e4SLinus Torvalds 	int sp, kix, ret;
6011da177e4SLinus Torvalds 
60276d8aeabSDavid Howells 	rcu_read_lock();
60376d8aeabSDavid Howells 
6041da177e4SLinus Torvalds 	ret = -EDEADLK;
6051da177e4SLinus Torvalds 	if (A == B)
60676d8aeabSDavid Howells 		goto cycle_detected;
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds 	subtree = B;
6091da177e4SLinus Torvalds 	sp = 0;
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds 	/* start processing a new keyring */
6121da177e4SLinus Torvalds  descend:
61376d8aeabSDavid Howells 	if (test_bit(KEY_FLAG_REVOKED, &subtree->flags))
6141da177e4SLinus Torvalds 		goto not_this_keyring;
6151da177e4SLinus Torvalds 
61676d8aeabSDavid Howells 	keylist = rcu_dereference(subtree->payload.subscriptions);
6171da177e4SLinus Torvalds 	if (!keylist)
6181da177e4SLinus Torvalds 		goto not_this_keyring;
6191da177e4SLinus Torvalds 	kix = 0;
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds  ascend:
6221da177e4SLinus Torvalds 	/* iterate through the remaining keys in this keyring */
6231da177e4SLinus Torvalds 	for (; kix < keylist->nkeys; kix++) {
6241da177e4SLinus Torvalds 		key = keylist->keys[kix];
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds 		if (key == A)
6271da177e4SLinus Torvalds 			goto cycle_detected;
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds 		/* recursively check nested keyrings */
6301da177e4SLinus Torvalds 		if (key->type == &key_type_keyring) {
6311da177e4SLinus Torvalds 			if (sp >= KEYRING_SEARCH_MAX_DEPTH)
6321da177e4SLinus Torvalds 				goto too_deep;
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds 			/* stack the current position */
63576d8aeabSDavid Howells 			stack[sp].keylist = keylist;
6361da177e4SLinus Torvalds 			stack[sp].kix = kix;
6371da177e4SLinus Torvalds 			sp++;
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds 			/* begin again with the new keyring */
6401da177e4SLinus Torvalds 			subtree = key;
6411da177e4SLinus Torvalds 			goto descend;
6421da177e4SLinus Torvalds 		}
6431da177e4SLinus Torvalds 	}
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds 	/* the keyring we're looking at was disqualified or didn't contain a
6461da177e4SLinus Torvalds 	 * matching key */
6471da177e4SLinus Torvalds  not_this_keyring:
6481da177e4SLinus Torvalds 	if (sp > 0) {
6491da177e4SLinus Torvalds 		/* resume the checking of a keyring higher up in the tree */
6501da177e4SLinus Torvalds 		sp--;
65176d8aeabSDavid Howells 		keylist = stack[sp].keylist;
6521da177e4SLinus Torvalds 		kix = stack[sp].kix + 1;
6531da177e4SLinus Torvalds 		goto ascend;
6541da177e4SLinus Torvalds 	}
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds 	ret = 0; /* no cycles detected */
6571da177e4SLinus Torvalds 
6581da177e4SLinus Torvalds  error:
65976d8aeabSDavid Howells 	rcu_read_unlock();
6601da177e4SLinus Torvalds 	return ret;
6611da177e4SLinus Torvalds 
6621da177e4SLinus Torvalds  too_deep:
6631da177e4SLinus Torvalds 	ret = -ELOOP;
66476d8aeabSDavid Howells 	goto error;
66576d8aeabSDavid Howells 
6661da177e4SLinus Torvalds  cycle_detected:
6671da177e4SLinus Torvalds 	ret = -EDEADLK;
6681da177e4SLinus Torvalds 	goto error;
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds } /* end keyring_detect_cycle() */
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds /*****************************************************************************/
6731da177e4SLinus Torvalds /*
67476d8aeabSDavid Howells  * dispose of a keyring list after the RCU grace period
67576d8aeabSDavid Howells  */
67676d8aeabSDavid Howells static void keyring_link_rcu_disposal(struct rcu_head *rcu)
67776d8aeabSDavid Howells {
67876d8aeabSDavid Howells 	struct keyring_list *klist =
67976d8aeabSDavid Howells 		container_of(rcu, struct keyring_list, rcu);
68076d8aeabSDavid Howells 
68176d8aeabSDavid Howells 	kfree(klist);
68276d8aeabSDavid Howells 
68376d8aeabSDavid Howells } /* end keyring_link_rcu_disposal() */
68476d8aeabSDavid Howells 
68576d8aeabSDavid Howells /*****************************************************************************/
68676d8aeabSDavid Howells /*
687*cab8eb59SDavid Howells  * dispose of a keyring list after the RCU grace period, freeing the unlinked
688*cab8eb59SDavid Howells  * key
689*cab8eb59SDavid Howells  */
690*cab8eb59SDavid Howells static void keyring_unlink_rcu_disposal(struct rcu_head *rcu)
691*cab8eb59SDavid Howells {
692*cab8eb59SDavid Howells 	struct keyring_list *klist =
693*cab8eb59SDavid Howells 		container_of(rcu, struct keyring_list, rcu);
694*cab8eb59SDavid Howells 
695*cab8eb59SDavid Howells 	key_put(klist->keys[klist->delkey]);
696*cab8eb59SDavid Howells 	kfree(klist);
697*cab8eb59SDavid Howells 
698*cab8eb59SDavid Howells } /* end keyring_unlink_rcu_disposal() */
699*cab8eb59SDavid Howells 
700*cab8eb59SDavid Howells /*****************************************************************************/
701*cab8eb59SDavid Howells /*
7021da177e4SLinus Torvalds  * link a key into to a keyring
70376d8aeabSDavid Howells  * - must be called with the keyring's semaphore write-locked
704*cab8eb59SDavid Howells  * - discard already extant link to matching key if there is one
7051da177e4SLinus Torvalds  */
7061da177e4SLinus Torvalds int __key_link(struct key *keyring, struct key *key)
7071da177e4SLinus Torvalds {
7081da177e4SLinus Torvalds 	struct keyring_list *klist, *nklist;
7091da177e4SLinus Torvalds 	unsigned max;
7101da177e4SLinus Torvalds 	size_t size;
711*cab8eb59SDavid Howells 	int loop, ret;
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds 	ret = -EKEYREVOKED;
71476d8aeabSDavid Howells 	if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
7151da177e4SLinus Torvalds 		goto error;
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds 	ret = -ENOTDIR;
7181da177e4SLinus Torvalds 	if (keyring->type != &key_type_keyring)
7191da177e4SLinus Torvalds 		goto error;
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds 	/* serialise link/link calls to prevent parallel calls causing a
7221da177e4SLinus Torvalds 	 * cycle when applied to two keyring in opposite orders */
7231da177e4SLinus Torvalds 	down_write(&keyring_serialise_link_sem);
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds 	/* check that we aren't going to create a cycle adding one keyring to
7261da177e4SLinus Torvalds 	 * another */
7271da177e4SLinus Torvalds 	if (key->type == &key_type_keyring) {
7281da177e4SLinus Torvalds 		ret = keyring_detect_cycle(keyring, key);
7291da177e4SLinus Torvalds 		if (ret < 0)
7301da177e4SLinus Torvalds 			goto error2;
7311da177e4SLinus Torvalds 	}
7321da177e4SLinus Torvalds 
733*cab8eb59SDavid Howells 	/* see if there's a matching key we can displace */
734*cab8eb59SDavid Howells 	klist = keyring->payload.subscriptions;
735*cab8eb59SDavid Howells 
736*cab8eb59SDavid Howells 	if (klist && klist->nkeys > 0) {
737*cab8eb59SDavid Howells 		struct key_type *type = key->type;
738*cab8eb59SDavid Howells 
739*cab8eb59SDavid Howells 		for (loop = klist->nkeys - 1; loop >= 0; loop--) {
740*cab8eb59SDavid Howells 			if (klist->keys[loop]->type == type &&
741*cab8eb59SDavid Howells 			    strcmp(klist->keys[loop]->description,
742*cab8eb59SDavid Howells 				   key->description) == 0
743*cab8eb59SDavid Howells 			    ) {
744*cab8eb59SDavid Howells 				/* found a match - replace with new key */
745*cab8eb59SDavid Howells 				size = sizeof(struct key *) * klist->maxkeys;
746*cab8eb59SDavid Howells 				size += sizeof(*klist);
747*cab8eb59SDavid Howells 				BUG_ON(size > PAGE_SIZE);
748*cab8eb59SDavid Howells 
749*cab8eb59SDavid Howells 				ret = -ENOMEM;
750*cab8eb59SDavid Howells 				nklist = kmalloc(size, GFP_KERNEL);
751*cab8eb59SDavid Howells 				if (!nklist)
752*cab8eb59SDavid Howells 					goto error2;
753*cab8eb59SDavid Howells 
754*cab8eb59SDavid Howells 				memcpy(nklist, klist, size);
755*cab8eb59SDavid Howells 
756*cab8eb59SDavid Howells 				/* replace matched key */
757*cab8eb59SDavid Howells 				atomic_inc(&key->usage);
758*cab8eb59SDavid Howells 				nklist->keys[loop] = key;
759*cab8eb59SDavid Howells 
760*cab8eb59SDavid Howells 				rcu_assign_pointer(
761*cab8eb59SDavid Howells 					keyring->payload.subscriptions,
762*cab8eb59SDavid Howells 					nklist);
763*cab8eb59SDavid Howells 
764*cab8eb59SDavid Howells 				/* dispose of the old keyring list and the
765*cab8eb59SDavid Howells 				 * displaced key */
766*cab8eb59SDavid Howells 				klist->delkey = loop;
767*cab8eb59SDavid Howells 				call_rcu(&klist->rcu,
768*cab8eb59SDavid Howells 					 keyring_unlink_rcu_disposal);
769*cab8eb59SDavid Howells 
770*cab8eb59SDavid Howells 				goto done;
771*cab8eb59SDavid Howells 			}
772*cab8eb59SDavid Howells 		}
773*cab8eb59SDavid Howells 	}
774*cab8eb59SDavid 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)
7791da177e4SLinus Torvalds 		goto error2;
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds 	klist = keyring->payload.subscriptions;
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds 	if (klist && klist->nkeys < klist->maxkeys) {
7841da177e4SLinus Torvalds 		/* there's sufficient slack space to add directly */
7851da177e4SLinus Torvalds 		atomic_inc(&key->usage);
7861da177e4SLinus Torvalds 
78776d8aeabSDavid Howells 		klist->keys[klist->nkeys] = key;
78876d8aeabSDavid Howells 		smp_wmb();
78976d8aeabSDavid Howells 		klist->nkeys++;
79076d8aeabSDavid Howells 		smp_wmb();
7911da177e4SLinus Torvalds 	}
7921da177e4SLinus Torvalds 	else {
7931da177e4SLinus Torvalds 		/* grow the key list */
7941da177e4SLinus Torvalds 		max = 4;
7951da177e4SLinus Torvalds 		if (klist)
7961da177e4SLinus Torvalds 			max += klist->maxkeys;
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds 		ret = -ENFILE;
79976d8aeabSDavid Howells 		if (max > 65535)
80076d8aeabSDavid Howells 			goto error3;
801a4014d8fSDavid Howells 		size = sizeof(*klist) + sizeof(struct key *) * max;
8021da177e4SLinus Torvalds 		if (size > PAGE_SIZE)
8031da177e4SLinus Torvalds 			goto error3;
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds 		ret = -ENOMEM;
8061da177e4SLinus Torvalds 		nklist = kmalloc(size, GFP_KERNEL);
8071da177e4SLinus Torvalds 		if (!nklist)
8081da177e4SLinus Torvalds 			goto error3;
8091da177e4SLinus Torvalds 		nklist->maxkeys = max;
8101da177e4SLinus Torvalds 		nklist->nkeys = 0;
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds 		if (klist) {
8131da177e4SLinus Torvalds 			nklist->nkeys = klist->nkeys;
8141da177e4SLinus Torvalds 			memcpy(nklist->keys,
8151da177e4SLinus Torvalds 			       klist->keys,
8161da177e4SLinus Torvalds 			       sizeof(struct key *) * klist->nkeys);
8171da177e4SLinus Torvalds 		}
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds 		/* add the key into the new space */
8201da177e4SLinus Torvalds 		atomic_inc(&key->usage);
8211da177e4SLinus Torvalds 		nklist->keys[nklist->nkeys++] = key;
82276d8aeabSDavid Howells 
82376d8aeabSDavid Howells 		rcu_assign_pointer(keyring->payload.subscriptions, nklist);
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds 		/* dispose of the old keyring list */
82676d8aeabSDavid Howells 		if (klist)
82776d8aeabSDavid Howells 			call_rcu(&klist->rcu, keyring_link_rcu_disposal);
8281da177e4SLinus Torvalds 	}
8291da177e4SLinus Torvalds 
830*cab8eb59SDavid Howells done:
831*cab8eb59SDavid Howells 	ret = 0;
8321da177e4SLinus Torvalds error2:
8331da177e4SLinus Torvalds 	up_write(&keyring_serialise_link_sem);
8341da177e4SLinus Torvalds error:
8351da177e4SLinus Torvalds 	return ret;
8361da177e4SLinus Torvalds 
8371da177e4SLinus Torvalds error3:
8381da177e4SLinus Torvalds 	/* undo the quota changes */
8391da177e4SLinus Torvalds 	key_payload_reserve(keyring,
8401da177e4SLinus Torvalds 			    keyring->datalen - KEYQUOTA_LINK_BYTES);
8411da177e4SLinus Torvalds 	goto error2;
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds } /* end __key_link() */
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds /*****************************************************************************/
8461da177e4SLinus Torvalds /*
8471da177e4SLinus Torvalds  * link a key to a keyring
8481da177e4SLinus Torvalds  */
8491da177e4SLinus Torvalds int key_link(struct key *keyring, struct key *key)
8501da177e4SLinus Torvalds {
8511da177e4SLinus Torvalds 	int ret;
8521da177e4SLinus Torvalds 
8531da177e4SLinus Torvalds 	key_check(keyring);
8541da177e4SLinus Torvalds 	key_check(key);
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds 	down_write(&keyring->sem);
8571da177e4SLinus Torvalds 	ret = __key_link(keyring, key);
8581da177e4SLinus Torvalds 	up_write(&keyring->sem);
8591da177e4SLinus Torvalds 
8601da177e4SLinus Torvalds 	return ret;
8611da177e4SLinus Torvalds 
8621da177e4SLinus Torvalds } /* end key_link() */
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds EXPORT_SYMBOL(key_link);
8651da177e4SLinus Torvalds 
8661da177e4SLinus Torvalds /*****************************************************************************/
8671da177e4SLinus Torvalds /*
8681da177e4SLinus Torvalds  * unlink the first link to a key from a keyring
8691da177e4SLinus Torvalds  */
8701da177e4SLinus Torvalds int key_unlink(struct key *keyring, struct key *key)
8711da177e4SLinus Torvalds {
87276d8aeabSDavid Howells 	struct keyring_list *klist, *nklist;
8731da177e4SLinus Torvalds 	int loop, ret;
8741da177e4SLinus Torvalds 
8751da177e4SLinus Torvalds 	key_check(keyring);
8761da177e4SLinus Torvalds 	key_check(key);
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds 	ret = -ENOTDIR;
8791da177e4SLinus Torvalds 	if (keyring->type != &key_type_keyring)
8801da177e4SLinus Torvalds 		goto error;
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 	down_write(&keyring->sem);
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 	klist = keyring->payload.subscriptions;
8851da177e4SLinus Torvalds 	if (klist) {
8861da177e4SLinus Torvalds 		/* search the keyring for the key */
8871da177e4SLinus Torvalds 		for (loop = 0; loop < klist->nkeys; loop++)
8881da177e4SLinus Torvalds 			if (klist->keys[loop] == key)
8891da177e4SLinus Torvalds 				goto key_is_present;
8901da177e4SLinus Torvalds 	}
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds 	up_write(&keyring->sem);
8931da177e4SLinus Torvalds 	ret = -ENOENT;
8941da177e4SLinus Torvalds 	goto error;
8951da177e4SLinus Torvalds 
8961da177e4SLinus Torvalds key_is_present:
89776d8aeabSDavid Howells 	/* we need to copy the key list for RCU purposes */
898a4014d8fSDavid Howells 	nklist = kmalloc(sizeof(*klist) +
899a4014d8fSDavid Howells 			 sizeof(struct key *) * klist->maxkeys,
90076d8aeabSDavid Howells 			 GFP_KERNEL);
90176d8aeabSDavid Howells 	if (!nklist)
90276d8aeabSDavid Howells 		goto nomem;
90376d8aeabSDavid Howells 	nklist->maxkeys = klist->maxkeys;
90476d8aeabSDavid Howells 	nklist->nkeys = klist->nkeys - 1;
90576d8aeabSDavid Howells 
90676d8aeabSDavid Howells 	if (loop > 0)
90776d8aeabSDavid Howells 		memcpy(&nklist->keys[0],
90876d8aeabSDavid Howells 		       &klist->keys[0],
909a4014d8fSDavid Howells 		       loop * sizeof(struct key *));
91076d8aeabSDavid Howells 
91176d8aeabSDavid Howells 	if (loop < nklist->nkeys)
91276d8aeabSDavid Howells 		memcpy(&nklist->keys[loop],
91376d8aeabSDavid Howells 		       &klist->keys[loop + 1],
914a4014d8fSDavid Howells 		       (nklist->nkeys - loop) * sizeof(struct key *));
91576d8aeabSDavid Howells 
9161da177e4SLinus Torvalds 	/* adjust the user's quota */
9171da177e4SLinus Torvalds 	key_payload_reserve(keyring,
9181da177e4SLinus Torvalds 			    keyring->datalen - KEYQUOTA_LINK_BYTES);
9191da177e4SLinus Torvalds 
92076d8aeabSDavid Howells 	rcu_assign_pointer(keyring->payload.subscriptions, nklist);
9211da177e4SLinus Torvalds 
9221da177e4SLinus Torvalds 	up_write(&keyring->sem);
92376d8aeabSDavid Howells 
92476d8aeabSDavid Howells 	/* schedule for later cleanup */
92576d8aeabSDavid Howells 	klist->delkey = loop;
92676d8aeabSDavid Howells 	call_rcu(&klist->rcu, keyring_unlink_rcu_disposal);
92776d8aeabSDavid Howells 
9281da177e4SLinus Torvalds 	ret = 0;
9291da177e4SLinus Torvalds 
9301da177e4SLinus Torvalds error:
9311da177e4SLinus Torvalds 	return ret;
93276d8aeabSDavid Howells nomem:
93376d8aeabSDavid Howells 	ret = -ENOMEM;
93476d8aeabSDavid Howells 	up_write(&keyring->sem);
93576d8aeabSDavid Howells 	goto error;
9361da177e4SLinus Torvalds 
9371da177e4SLinus Torvalds } /* end key_unlink() */
9381da177e4SLinus Torvalds 
9391da177e4SLinus Torvalds EXPORT_SYMBOL(key_unlink);
9401da177e4SLinus Torvalds 
9411da177e4SLinus Torvalds /*****************************************************************************/
9421da177e4SLinus Torvalds /*
94376d8aeabSDavid Howells  * dispose of a keyring list after the RCU grace period, releasing the keys it
94476d8aeabSDavid Howells  * links to
94576d8aeabSDavid Howells  */
94676d8aeabSDavid Howells static void keyring_clear_rcu_disposal(struct rcu_head *rcu)
94776d8aeabSDavid Howells {
94876d8aeabSDavid Howells 	struct keyring_list *klist;
94976d8aeabSDavid Howells 	int loop;
95076d8aeabSDavid Howells 
95176d8aeabSDavid Howells 	klist = container_of(rcu, struct keyring_list, rcu);
95276d8aeabSDavid Howells 
95376d8aeabSDavid Howells 	for (loop = klist->nkeys - 1; loop >= 0; loop--)
95476d8aeabSDavid Howells 		key_put(klist->keys[loop]);
95576d8aeabSDavid Howells 
95676d8aeabSDavid Howells 	kfree(klist);
95776d8aeabSDavid Howells 
95876d8aeabSDavid Howells } /* end keyring_clear_rcu_disposal() */
95976d8aeabSDavid Howells 
96076d8aeabSDavid Howells /*****************************************************************************/
96176d8aeabSDavid Howells /*
9621da177e4SLinus Torvalds  * clear the specified process keyring
9631da177e4SLinus Torvalds  * - implements keyctl(KEYCTL_CLEAR)
9641da177e4SLinus Torvalds  */
9651da177e4SLinus Torvalds int keyring_clear(struct key *keyring)
9661da177e4SLinus Torvalds {
9671da177e4SLinus Torvalds 	struct keyring_list *klist;
96876d8aeabSDavid Howells 	int ret;
9691da177e4SLinus Torvalds 
9701da177e4SLinus Torvalds 	ret = -ENOTDIR;
9711da177e4SLinus Torvalds 	if (keyring->type == &key_type_keyring) {
9721da177e4SLinus Torvalds 		/* detach the pointer block with the locks held */
9731da177e4SLinus Torvalds 		down_write(&keyring->sem);
9741da177e4SLinus Torvalds 
9751da177e4SLinus Torvalds 		klist = keyring->payload.subscriptions;
9761da177e4SLinus Torvalds 		if (klist) {
9771da177e4SLinus Torvalds 			/* adjust the quota */
9781da177e4SLinus Torvalds 			key_payload_reserve(keyring,
9791da177e4SLinus Torvalds 					    sizeof(struct keyring_list));
9801da177e4SLinus Torvalds 
98176d8aeabSDavid Howells 			rcu_assign_pointer(keyring->payload.subscriptions,
98276d8aeabSDavid Howells 					   NULL);
9831da177e4SLinus Torvalds 		}
9841da177e4SLinus Torvalds 
9851da177e4SLinus Torvalds 		up_write(&keyring->sem);
9861da177e4SLinus Torvalds 
9871da177e4SLinus Torvalds 		/* free the keys after the locks have been dropped */
98876d8aeabSDavid Howells 		if (klist)
98976d8aeabSDavid Howells 			call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds 		ret = 0;
9921da177e4SLinus Torvalds 	}
9931da177e4SLinus Torvalds 
9941da177e4SLinus Torvalds 	return ret;
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds } /* end keyring_clear() */
9971da177e4SLinus Torvalds 
9981da177e4SLinus Torvalds EXPORT_SYMBOL(keyring_clear);
999