xref: /openbmc/linux/security/keys/keyring.c (revision db1d1d57e97700b4131fe80556dc6ef976d534c4)
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_duplicate(struct key *keyring, const struct key *source);
521da177e4SLinus Torvalds static int keyring_match(const struct key *keyring, const void *criterion);
531da177e4SLinus Torvalds static void keyring_destroy(struct key *keyring);
541da177e4SLinus Torvalds static void keyring_describe(const struct key *keyring, struct seq_file *m);
551da177e4SLinus Torvalds static long keyring_read(const struct key *keyring,
561da177e4SLinus Torvalds 			 char __user *buffer, size_t buflen);
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds struct key_type key_type_keyring = {
591da177e4SLinus Torvalds 	.name		= "keyring",
601da177e4SLinus Torvalds 	.def_datalen	= sizeof(struct keyring_list),
611da177e4SLinus Torvalds 	.instantiate	= keyring_instantiate,
621da177e4SLinus Torvalds 	.duplicate	= keyring_duplicate,
631da177e4SLinus Torvalds 	.match		= keyring_match,
641da177e4SLinus Torvalds 	.destroy	= keyring_destroy,
651da177e4SLinus Torvalds 	.describe	= keyring_describe,
661da177e4SLinus Torvalds 	.read		= keyring_read,
671da177e4SLinus Torvalds };
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds /*
701da177e4SLinus Torvalds  * semaphore to serialise link/link calls to prevent two link calls in parallel
711da177e4SLinus Torvalds  * introducing a cycle
721da177e4SLinus Torvalds  */
731da177e4SLinus Torvalds DECLARE_RWSEM(keyring_serialise_link_sem);
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds /*****************************************************************************/
761da177e4SLinus Torvalds /*
771da177e4SLinus Torvalds  * publish the name of a keyring so that it can be found by name (if it has
781da177e4SLinus Torvalds  * one)
791da177e4SLinus Torvalds  */
801da177e4SLinus Torvalds void keyring_publish_name(struct key *keyring)
811da177e4SLinus Torvalds {
821da177e4SLinus Torvalds 	int bucket;
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds 	if (keyring->description) {
851da177e4SLinus Torvalds 		bucket = keyring_hash(keyring->description);
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds 		write_lock(&keyring_name_lock);
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds 		if (!keyring_name_hash[bucket].next)
901da177e4SLinus Torvalds 			INIT_LIST_HEAD(&keyring_name_hash[bucket]);
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds 		list_add_tail(&keyring->type_data.link,
931da177e4SLinus Torvalds 			      &keyring_name_hash[bucket]);
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds 		write_unlock(&keyring_name_lock);
961da177e4SLinus Torvalds 	}
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds } /* end keyring_publish_name() */
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds /*****************************************************************************/
1011da177e4SLinus Torvalds /*
1021da177e4SLinus Torvalds  * initialise a keyring
1031da177e4SLinus Torvalds  * - we object if we were given any data
1041da177e4SLinus Torvalds  */
1051da177e4SLinus Torvalds static int keyring_instantiate(struct key *keyring,
1061da177e4SLinus Torvalds 			       const void *data, size_t datalen)
1071da177e4SLinus Torvalds {
1081da177e4SLinus Torvalds 	int ret;
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds 	ret = -EINVAL;
1111da177e4SLinus Torvalds 	if (datalen == 0) {
1121da177e4SLinus Torvalds 		/* make the keyring available by name if it has one */
1131da177e4SLinus Torvalds 		keyring_publish_name(keyring);
1141da177e4SLinus Torvalds 		ret = 0;
1151da177e4SLinus Torvalds 	}
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds 	return ret;
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds } /* end keyring_instantiate() */
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds /*****************************************************************************/
1221da177e4SLinus Torvalds /*
1231da177e4SLinus Torvalds  * duplicate the list of subscribed keys from a source keyring into this one
1241da177e4SLinus Torvalds  */
1251da177e4SLinus Torvalds static int keyring_duplicate(struct key *keyring, const struct key *source)
1261da177e4SLinus Torvalds {
1271da177e4SLinus Torvalds 	struct keyring_list *sklist, *klist;
1281da177e4SLinus Torvalds 	unsigned max;
1291da177e4SLinus Torvalds 	size_t size;
1301da177e4SLinus Torvalds 	int loop, ret;
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds 	const unsigned limit =
133a4014d8fSDavid Howells 		(PAGE_SIZE - sizeof(*klist)) / sizeof(struct key *);
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds 	ret = 0;
1361da177e4SLinus Torvalds 
13776d8aeabSDavid Howells 	/* find out how many keys are currently linked */
13876d8aeabSDavid Howells 	rcu_read_lock();
13976d8aeabSDavid Howells 	sklist = rcu_dereference(source->payload.subscriptions);
14076d8aeabSDavid Howells 	max = 0;
14176d8aeabSDavid Howells 	if (sklist)
1421da177e4SLinus Torvalds 		max = sklist->nkeys;
14376d8aeabSDavid Howells 	rcu_read_unlock();
14476d8aeabSDavid Howells 
14576d8aeabSDavid Howells 	/* allocate a new payload and stuff load with key links */
14676d8aeabSDavid Howells 	if (max > 0) {
1471da177e4SLinus Torvalds 		BUG_ON(max > limit);
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 		max = (max + 3) & ~3;
1501da177e4SLinus Torvalds 		if (max > limit)
1511da177e4SLinus Torvalds 			max = limit;
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds 		ret = -ENOMEM;
154a4014d8fSDavid Howells 		size = sizeof(*klist) + sizeof(struct key *) * max;
1551da177e4SLinus Torvalds 		klist = kmalloc(size, GFP_KERNEL);
1561da177e4SLinus Torvalds 		if (!klist)
1571da177e4SLinus Torvalds 			goto error;
1581da177e4SLinus Torvalds 
15976d8aeabSDavid Howells 		/* set links */
16076d8aeabSDavid Howells 		rcu_read_lock();
16176d8aeabSDavid Howells 		sklist = rcu_dereference(source->payload.subscriptions);
16276d8aeabSDavid Howells 
1631da177e4SLinus Torvalds 		klist->maxkeys = max;
1641da177e4SLinus Torvalds 		klist->nkeys = sklist->nkeys;
1651da177e4SLinus Torvalds 		memcpy(klist->keys,
1661da177e4SLinus Torvalds 		       sklist->keys,
167a4014d8fSDavid Howells 		       sklist->nkeys * sizeof(struct key *));
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 		for (loop = klist->nkeys - 1; loop >= 0; loop--)
1701da177e4SLinus Torvalds 			atomic_inc(&klist->keys[loop]->usage);
1711da177e4SLinus Torvalds 
17276d8aeabSDavid Howells 		rcu_read_unlock();
17376d8aeabSDavid Howells 
17476d8aeabSDavid Howells 		rcu_assign_pointer(keyring->payload.subscriptions, klist);
1751da177e4SLinus Torvalds 		ret = 0;
1761da177e4SLinus Torvalds 	}
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds  error:
1791da177e4SLinus Torvalds 	return ret;
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds } /* end keyring_duplicate() */
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds /*****************************************************************************/
1841da177e4SLinus Torvalds /*
1851da177e4SLinus Torvalds  * match keyrings on their name
1861da177e4SLinus Torvalds  */
1871da177e4SLinus Torvalds static int keyring_match(const struct key *keyring, const void *description)
1881da177e4SLinus Torvalds {
1891da177e4SLinus Torvalds 	return keyring->description &&
1901da177e4SLinus Torvalds 		strcmp(keyring->description, description) == 0;
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds } /* end keyring_match() */
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds /*****************************************************************************/
1951da177e4SLinus Torvalds /*
1961da177e4SLinus Torvalds  * dispose of the data dangling from the corpse of a keyring
1971da177e4SLinus Torvalds  */
1981da177e4SLinus Torvalds static void keyring_destroy(struct key *keyring)
1991da177e4SLinus Torvalds {
2001da177e4SLinus Torvalds 	struct keyring_list *klist;
2011da177e4SLinus Torvalds 	int loop;
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds 	if (keyring->description) {
2041da177e4SLinus Torvalds 		write_lock(&keyring_name_lock);
20594efe72fSDavid Howells 
20694efe72fSDavid Howells 		if (keyring->type_data.link.next != NULL &&
20794efe72fSDavid Howells 		    !list_empty(&keyring->type_data.link))
2081da177e4SLinus Torvalds 			list_del(&keyring->type_data.link);
20994efe72fSDavid Howells 
2101da177e4SLinus Torvalds 		write_unlock(&keyring_name_lock);
2111da177e4SLinus Torvalds 	}
2121da177e4SLinus Torvalds 
21376d8aeabSDavid Howells 	klist = rcu_dereference(keyring->payload.subscriptions);
2141da177e4SLinus Torvalds 	if (klist) {
2151da177e4SLinus Torvalds 		for (loop = klist->nkeys - 1; loop >= 0; loop--)
2161da177e4SLinus Torvalds 			key_put(klist->keys[loop]);
2171da177e4SLinus Torvalds 		kfree(klist);
2181da177e4SLinus Torvalds 	}
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds } /* end keyring_destroy() */
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds /*****************************************************************************/
2231da177e4SLinus Torvalds /*
2241da177e4SLinus Torvalds  * describe the keyring
2251da177e4SLinus Torvalds  */
2261da177e4SLinus Torvalds static void keyring_describe(const struct key *keyring, struct seq_file *m)
2271da177e4SLinus Torvalds {
2281da177e4SLinus Torvalds 	struct keyring_list *klist;
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds 	if (keyring->description) {
2311da177e4SLinus Torvalds 		seq_puts(m, keyring->description);
2321da177e4SLinus Torvalds 	}
2331da177e4SLinus Torvalds 	else {
2341da177e4SLinus Torvalds 		seq_puts(m, "[anon]");
2351da177e4SLinus Torvalds 	}
2361da177e4SLinus Torvalds 
23776d8aeabSDavid Howells 	rcu_read_lock();
23876d8aeabSDavid Howells 	klist = rcu_dereference(keyring->payload.subscriptions);
2391da177e4SLinus Torvalds 	if (klist)
2401da177e4SLinus Torvalds 		seq_printf(m, ": %u/%u", klist->nkeys, klist->maxkeys);
2411da177e4SLinus Torvalds 	else
2421da177e4SLinus Torvalds 		seq_puts(m, ": empty");
24376d8aeabSDavid Howells 	rcu_read_unlock();
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds } /* end keyring_describe() */
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds /*****************************************************************************/
2481da177e4SLinus Torvalds /*
2491da177e4SLinus Torvalds  * read a list of key IDs from the keyring's contents
25076d8aeabSDavid Howells  * - the keyring's semaphore is read-locked
2511da177e4SLinus Torvalds  */
2521da177e4SLinus Torvalds static long keyring_read(const struct key *keyring,
2531da177e4SLinus Torvalds 			 char __user *buffer, size_t buflen)
2541da177e4SLinus Torvalds {
2551da177e4SLinus Torvalds 	struct keyring_list *klist;
2561da177e4SLinus Torvalds 	struct key *key;
2571da177e4SLinus Torvalds 	size_t qty, tmp;
2581da177e4SLinus Torvalds 	int loop, ret;
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 	ret = 0;
26176d8aeabSDavid Howells 	klist = rcu_dereference(keyring->payload.subscriptions);
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 	if (klist) {
2641da177e4SLinus Torvalds 		/* calculate how much data we could return */
2651da177e4SLinus Torvalds 		qty = klist->nkeys * sizeof(key_serial_t);
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds 		if (buffer && buflen > 0) {
2681da177e4SLinus Torvalds 			if (buflen > qty)
2691da177e4SLinus Torvalds 				buflen = qty;
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds 			/* copy the IDs of the subscribed keys into the
2721da177e4SLinus Torvalds 			 * buffer */
2731da177e4SLinus Torvalds 			ret = -EFAULT;
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 			for (loop = 0; loop < klist->nkeys; loop++) {
2761da177e4SLinus Torvalds 				key = klist->keys[loop];
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds 				tmp = sizeof(key_serial_t);
2791da177e4SLinus Torvalds 				if (tmp > buflen)
2801da177e4SLinus Torvalds 					tmp = buflen;
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 				if (copy_to_user(buffer,
2831da177e4SLinus Torvalds 						 &key->serial,
2841da177e4SLinus Torvalds 						 tmp) != 0)
2851da177e4SLinus Torvalds 					goto error;
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 				buflen -= tmp;
2881da177e4SLinus Torvalds 				if (buflen == 0)
2891da177e4SLinus Torvalds 					break;
2901da177e4SLinus Torvalds 				buffer += tmp;
2911da177e4SLinus Torvalds 			}
2921da177e4SLinus Torvalds 		}
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 		ret = qty;
2951da177e4SLinus Torvalds 	}
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds  error:
2981da177e4SLinus Torvalds 	return ret;
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds } /* end keyring_read() */
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds /*****************************************************************************/
3031da177e4SLinus Torvalds /*
3041da177e4SLinus Torvalds  * allocate a keyring and link into the destination keyring
3051da177e4SLinus Torvalds  */
3061da177e4SLinus Torvalds struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
3071da177e4SLinus Torvalds 			  int not_in_quota, struct key *dest)
3081da177e4SLinus Torvalds {
3091da177e4SLinus Torvalds 	struct key *keyring;
3101da177e4SLinus Torvalds 	int ret;
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 	keyring = key_alloc(&key_type_keyring, description,
31329db9190SDavid Howells 			    uid, gid,
31429db9190SDavid Howells 			    (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
31529db9190SDavid Howells 			    not_in_quota);
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds 	if (!IS_ERR(keyring)) {
3183e30148cSDavid Howells 		ret = key_instantiate_and_link(keyring, NULL, 0, dest, NULL);
3191da177e4SLinus Torvalds 		if (ret < 0) {
3201da177e4SLinus Torvalds 			key_put(keyring);
3211da177e4SLinus Torvalds 			keyring = ERR_PTR(ret);
3221da177e4SLinus Torvalds 		}
3231da177e4SLinus Torvalds 	}
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds 	return keyring;
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds } /* end keyring_alloc() */
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds /*****************************************************************************/
3301da177e4SLinus Torvalds /*
3311da177e4SLinus Torvalds  * search the supplied keyring tree for a key that matches the criterion
3321da177e4SLinus Torvalds  * - perform a breadth-then-depth search up to the prescribed limit
3331da177e4SLinus Torvalds  * - we only find keys on which we have search permission
3341da177e4SLinus Torvalds  * - we use the supplied match function to see if the description (or other
3351da177e4SLinus Torvalds  *   feature of interest) matches
3363e30148cSDavid Howells  * - we rely on RCU to prevent the keyring lists from disappearing on us
3371da177e4SLinus Torvalds  * - we return -EAGAIN if we didn't find any matching key
3381da177e4SLinus Torvalds  * - we return -ENOKEY if we only found negative matching keys
339664cceb0SDavid Howells  * - we propagate the possession attribute from the keyring ref to the key ref
3401da177e4SLinus Torvalds  */
341664cceb0SDavid Howells key_ref_t keyring_search_aux(key_ref_t keyring_ref,
3423e30148cSDavid Howells 			     struct task_struct *context,
3431da177e4SLinus Torvalds 			     struct key_type *type,
3441da177e4SLinus Torvalds 			     const void *description,
3451da177e4SLinus Torvalds 			     key_match_func_t match)
3461da177e4SLinus Torvalds {
3471da177e4SLinus Torvalds 	struct {
34876d8aeabSDavid Howells 		struct keyring_list *keylist;
3491da177e4SLinus Torvalds 		int kix;
3501da177e4SLinus Torvalds 	} stack[KEYRING_SEARCH_MAX_DEPTH];
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds 	struct keyring_list *keylist;
3531da177e4SLinus Torvalds 	struct timespec now;
354664cceb0SDavid Howells 	unsigned long possessed;
355664cceb0SDavid Howells 	struct key *keyring, *key;
356664cceb0SDavid Howells 	key_ref_t key_ref;
3571da177e4SLinus Torvalds 	long err;
35876d8aeabSDavid Howells 	int sp, kix;
3591da177e4SLinus Torvalds 
360664cceb0SDavid Howells 	keyring = key_ref_to_ptr(keyring_ref);
361664cceb0SDavid Howells 	possessed = is_key_possessed(keyring_ref);
3621da177e4SLinus Torvalds 	key_check(keyring);
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds 	/* top keyring must have search permission to begin the search */
36529db9190SDavid Howells         err = key_task_permission(keyring_ref, context, KEY_SEARCH);
36629db9190SDavid Howells 	if (err < 0) {
36729db9190SDavid Howells 		key_ref = ERR_PTR(err);
3681da177e4SLinus Torvalds 		goto error;
36929db9190SDavid Howells 	}
3701da177e4SLinus Torvalds 
371664cceb0SDavid Howells 	key_ref = ERR_PTR(-ENOTDIR);
3721da177e4SLinus Torvalds 	if (keyring->type != &key_type_keyring)
3731da177e4SLinus Torvalds 		goto error;
3741da177e4SLinus Torvalds 
375664cceb0SDavid Howells 	rcu_read_lock();
376664cceb0SDavid Howells 
3771da177e4SLinus Torvalds 	now = current_kernel_time();
3781da177e4SLinus Torvalds 	err = -EAGAIN;
3791da177e4SLinus Torvalds 	sp = 0;
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds 	/* start processing a new keyring */
3821da177e4SLinus Torvalds descend:
38376d8aeabSDavid Howells 	if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
3841da177e4SLinus Torvalds 		goto not_this_keyring;
3851da177e4SLinus Torvalds 
38676d8aeabSDavid Howells 	keylist = rcu_dereference(keyring->payload.subscriptions);
3871da177e4SLinus Torvalds 	if (!keylist)
3881da177e4SLinus Torvalds 		goto not_this_keyring;
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds 	/* iterate through the keys in this keyring first */
3911da177e4SLinus Torvalds 	for (kix = 0; kix < keylist->nkeys; kix++) {
3921da177e4SLinus Torvalds 		key = keylist->keys[kix];
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds 		/* ignore keys not of this type */
3951da177e4SLinus Torvalds 		if (key->type != type)
3961da177e4SLinus Torvalds 			continue;
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 		/* skip revoked keys and expired keys */
39976d8aeabSDavid Howells 		if (test_bit(KEY_FLAG_REVOKED, &key->flags))
4001da177e4SLinus Torvalds 			continue;
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds 		if (key->expiry && now.tv_sec >= key->expiry)
4031da177e4SLinus Torvalds 			continue;
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds 		/* keys that don't match */
4061da177e4SLinus Torvalds 		if (!match(key, description))
4071da177e4SLinus Torvalds 			continue;
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds 		/* key must have search permissions */
41029db9190SDavid Howells 		if (key_task_permission(make_key_ref(key, possessed),
41129db9190SDavid Howells 					context, KEY_SEARCH) < 0)
4121da177e4SLinus Torvalds 			continue;
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds 		/* we set a different error code if we find a negative key */
41576d8aeabSDavid Howells 		if (test_bit(KEY_FLAG_NEGATIVE, &key->flags)) {
4161da177e4SLinus Torvalds 			err = -ENOKEY;
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:
42676d8aeabSDavid Howells 	for (; kix < keylist->nkeys; kix++) {
4271da177e4SLinus Torvalds 		key = keylist->keys[kix];
4281da177e4SLinus Torvalds 		if (key->type != &key_type_keyring)
42976d8aeabSDavid Howells 			continue;
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds 		/* recursively search nested keyrings
4321da177e4SLinus Torvalds 		 * - only search keyrings for which we have search permission
4331da177e4SLinus Torvalds 		 */
4341da177e4SLinus Torvalds 		if (sp >= KEYRING_SEARCH_MAX_DEPTH)
43576d8aeabSDavid Howells 			continue;
4361da177e4SLinus Torvalds 
4370f6ed7c2SDavid Howells 		if (key_task_permission(make_key_ref(key, possessed),
43829db9190SDavid Howells 					context, KEY_SEARCH) < 0)
43976d8aeabSDavid Howells 			continue;
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds 		/* stack the current position */
44276d8aeabSDavid Howells 		stack[sp].keylist = keylist;
4431da177e4SLinus Torvalds 		stack[sp].kix = kix;
4441da177e4SLinus Torvalds 		sp++;
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds 		/* begin again with the new keyring */
4471da177e4SLinus Torvalds 		keyring = key;
4481da177e4SLinus Torvalds 		goto descend;
4491da177e4SLinus Torvalds 	}
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 	/* the keyring we're looking at was disqualified or didn't contain a
4521da177e4SLinus Torvalds 	 * matching key */
4531da177e4SLinus Torvalds not_this_keyring:
4541da177e4SLinus Torvalds 	if (sp > 0) {
4551da177e4SLinus Torvalds 		/* resume the processing of a keyring higher up in the tree */
4561da177e4SLinus Torvalds 		sp--;
45776d8aeabSDavid Howells 		keylist = stack[sp].keylist;
4581da177e4SLinus Torvalds 		kix = stack[sp].kix + 1;
4591da177e4SLinus Torvalds 		goto ascend;
4601da177e4SLinus Torvalds 	}
4611da177e4SLinus Torvalds 
462664cceb0SDavid Howells 	key_ref = ERR_PTR(err);
463664cceb0SDavid Howells 	goto error_2;
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds 	/* we found a viable match */
4661da177e4SLinus Torvalds found:
4671da177e4SLinus Torvalds 	atomic_inc(&key->usage);
4681da177e4SLinus Torvalds 	key_check(key);
469664cceb0SDavid Howells 	key_ref = make_key_ref(key, possessed);
470664cceb0SDavid Howells error_2:
47176d8aeabSDavid Howells 	rcu_read_unlock();
472664cceb0SDavid Howells error:
473664cceb0SDavid Howells 	return key_ref;
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds } /* end keyring_search_aux() */
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds /*****************************************************************************/
4781da177e4SLinus Torvalds /*
4791da177e4SLinus Torvalds  * search the supplied keyring tree for a key that matches the criterion
4801da177e4SLinus Torvalds  * - perform a breadth-then-depth search up to the prescribed limit
4811da177e4SLinus Torvalds  * - we only find keys on which we have search permission
4821da177e4SLinus Torvalds  * - we readlock the keyrings as we search down the tree
4831da177e4SLinus Torvalds  * - we return -EAGAIN if we didn't find any matching key
4841da177e4SLinus Torvalds  * - we return -ENOKEY if we only found negative matching keys
4851da177e4SLinus Torvalds  */
486664cceb0SDavid Howells key_ref_t keyring_search(key_ref_t keyring,
4871da177e4SLinus Torvalds 			 struct key_type *type,
4881da177e4SLinus Torvalds 			 const char *description)
4891da177e4SLinus Torvalds {
4903e30148cSDavid Howells 	if (!type->match)
4913e30148cSDavid Howells 		return ERR_PTR(-ENOKEY);
4923e30148cSDavid Howells 
4933e30148cSDavid Howells 	return keyring_search_aux(keyring, current,
4943e30148cSDavid Howells 				  type, description, type->match);
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds } /* end keyring_search() */
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds EXPORT_SYMBOL(keyring_search);
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds /*****************************************************************************/
5011da177e4SLinus Torvalds /*
5021da177e4SLinus Torvalds  * search the given keyring only (no recursion)
5031da177e4SLinus Torvalds  * - keyring must be locked by caller
5041da177e4SLinus Torvalds  */
505664cceb0SDavid Howells key_ref_t __keyring_search_one(key_ref_t keyring_ref,
5061da177e4SLinus Torvalds 			       const struct key_type *ktype,
5071da177e4SLinus Torvalds 			       const char *description,
5081da177e4SLinus Torvalds 			       key_perm_t perm)
5091da177e4SLinus Torvalds {
5101da177e4SLinus Torvalds 	struct keyring_list *klist;
511664cceb0SDavid Howells 	unsigned long possessed;
512664cceb0SDavid Howells 	struct key *keyring, *key;
5131da177e4SLinus Torvalds 	int loop;
5141da177e4SLinus Torvalds 
515664cceb0SDavid Howells 	keyring = key_ref_to_ptr(keyring_ref);
516664cceb0SDavid Howells 	possessed = is_key_possessed(keyring_ref);
517664cceb0SDavid Howells 
51876d8aeabSDavid Howells 	rcu_read_lock();
51976d8aeabSDavid Howells 
52076d8aeabSDavid Howells 	klist = rcu_dereference(keyring->payload.subscriptions);
5211da177e4SLinus Torvalds 	if (klist) {
5221da177e4SLinus Torvalds 		for (loop = 0; loop < klist->nkeys; loop++) {
5231da177e4SLinus Torvalds 			key = klist->keys[loop];
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds 			if (key->type == ktype &&
5263e30148cSDavid Howells 			    (!key->type->match ||
5273e30148cSDavid Howells 			     key->type->match(key, description)) &&
528664cceb0SDavid Howells 			    key_permission(make_key_ref(key, possessed),
529*db1d1d57SDavid Howells 					   perm) == 0 &&
53076d8aeabSDavid Howells 			    !test_bit(KEY_FLAG_REVOKED, &key->flags)
5311da177e4SLinus Torvalds 			    )
5321da177e4SLinus Torvalds 				goto found;
5331da177e4SLinus Torvalds 		}
5341da177e4SLinus Torvalds 	}
5351da177e4SLinus Torvalds 
536664cceb0SDavid Howells 	rcu_read_unlock();
537664cceb0SDavid Howells 	return ERR_PTR(-ENOKEY);
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds  found:
5401da177e4SLinus Torvalds 	atomic_inc(&key->usage);
54176d8aeabSDavid Howells 	rcu_read_unlock();
542664cceb0SDavid Howells 	return make_key_ref(key, possessed);
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds } /* end __keyring_search_one() */
5451da177e4SLinus Torvalds 
5461da177e4SLinus Torvalds /*****************************************************************************/
5471da177e4SLinus Torvalds /*
5483e30148cSDavid Howells  * search for an instantiation authorisation key matching a target key
5493e30148cSDavid Howells  * - the RCU read lock must be held by the caller
5503e30148cSDavid Howells  * - a target_id of zero specifies any valid token
5513e30148cSDavid Howells  */
5523e30148cSDavid Howells struct key *keyring_search_instkey(struct key *keyring,
5533e30148cSDavid Howells 				   key_serial_t target_id)
5543e30148cSDavid Howells {
5553e30148cSDavid Howells 	struct request_key_auth *rka;
5563e30148cSDavid Howells 	struct keyring_list *klist;
5573e30148cSDavid Howells 	struct key *instkey;
5583e30148cSDavid Howells 	int loop;
5593e30148cSDavid Howells 
5603e30148cSDavid Howells 	klist = rcu_dereference(keyring->payload.subscriptions);
5613e30148cSDavid Howells 	if (klist) {
5623e30148cSDavid Howells 		for (loop = 0; loop < klist->nkeys; loop++) {
5633e30148cSDavid Howells 			instkey = klist->keys[loop];
5643e30148cSDavid Howells 
5653e30148cSDavid Howells 			if (instkey->type != &key_type_request_key_auth)
5663e30148cSDavid Howells 				continue;
5673e30148cSDavid Howells 
5683e30148cSDavid Howells 			rka = instkey->payload.data;
5693e30148cSDavid Howells 			if (target_id && rka->target_key->serial != target_id)
5703e30148cSDavid Howells 				continue;
5713e30148cSDavid Howells 
5723e30148cSDavid Howells 			/* the auth key is revoked during instantiation */
5733e30148cSDavid Howells 			if (!test_bit(KEY_FLAG_REVOKED, &instkey->flags))
5743e30148cSDavid Howells 				goto found;
5753e30148cSDavid Howells 
5763e30148cSDavid Howells 			instkey = ERR_PTR(-EKEYREVOKED);
5773e30148cSDavid Howells 			goto error;
5783e30148cSDavid Howells 		}
5793e30148cSDavid Howells 	}
5803e30148cSDavid Howells 
5813e30148cSDavid Howells 	instkey = ERR_PTR(-EACCES);
5823e30148cSDavid Howells 	goto error;
5833e30148cSDavid Howells 
5843e30148cSDavid Howells found:
5853e30148cSDavid Howells 	atomic_inc(&instkey->usage);
5863e30148cSDavid Howells error:
5873e30148cSDavid Howells 	return instkey;
5883e30148cSDavid Howells 
5893e30148cSDavid Howells } /* end keyring_search_instkey() */
5903e30148cSDavid Howells 
5913e30148cSDavid Howells /*****************************************************************************/
5923e30148cSDavid Howells /*
5931da177e4SLinus Torvalds  * find a keyring with the specified name
5941da177e4SLinus Torvalds  * - all named keyrings are searched
5951da177e4SLinus Torvalds  * - only find keyrings with search permission for the process
5961da177e4SLinus Torvalds  * - only find keyrings with a serial number greater than the one specified
5971da177e4SLinus Torvalds  */
5981da177e4SLinus Torvalds struct key *find_keyring_by_name(const char *name, key_serial_t bound)
5991da177e4SLinus Torvalds {
6001da177e4SLinus Torvalds 	struct key *keyring;
6011da177e4SLinus Torvalds 	int bucket;
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds 	keyring = ERR_PTR(-EINVAL);
6041da177e4SLinus Torvalds 	if (!name)
6051da177e4SLinus Torvalds 		goto error;
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds 	bucket = keyring_hash(name);
6081da177e4SLinus Torvalds 
6091da177e4SLinus Torvalds 	read_lock(&keyring_name_lock);
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds 	if (keyring_name_hash[bucket].next) {
6121da177e4SLinus Torvalds 		/* search this hash bucket for a keyring with a matching name
6131da177e4SLinus Torvalds 		 * that's readable and that hasn't been revoked */
6141da177e4SLinus Torvalds 		list_for_each_entry(keyring,
6151da177e4SLinus Torvalds 				    &keyring_name_hash[bucket],
6161da177e4SLinus Torvalds 				    type_data.link
6171da177e4SLinus Torvalds 				    ) {
61876d8aeabSDavid Howells 			if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
6191da177e4SLinus Torvalds 				continue;
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds 			if (strcmp(keyring->description, name) != 0)
6221da177e4SLinus Torvalds 				continue;
6231da177e4SLinus Torvalds 
6240f6ed7c2SDavid Howells 			if (key_permission(make_key_ref(keyring, 0),
62529db9190SDavid Howells 					   KEY_SEARCH) < 0)
6261da177e4SLinus Torvalds 				continue;
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds 			/* found a potential candidate, but we still need to
6291da177e4SLinus Torvalds 			 * check the serial number */
6301da177e4SLinus Torvalds 			if (keyring->serial <= bound)
6311da177e4SLinus Torvalds 				continue;
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds 			/* we've got a match */
6341da177e4SLinus Torvalds 			atomic_inc(&keyring->usage);
6351da177e4SLinus Torvalds 			read_unlock(&keyring_name_lock);
6361da177e4SLinus Torvalds 			goto error;
6371da177e4SLinus Torvalds 		}
6381da177e4SLinus Torvalds 	}
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds 	read_unlock(&keyring_name_lock);
6411da177e4SLinus Torvalds 	keyring = ERR_PTR(-ENOKEY);
6421da177e4SLinus Torvalds 
6431da177e4SLinus Torvalds  error:
6441da177e4SLinus Torvalds 	return keyring;
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds } /* end find_keyring_by_name() */
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds /*****************************************************************************/
6491da177e4SLinus Torvalds /*
6501da177e4SLinus Torvalds  * see if a cycle will will be created by inserting acyclic tree B in acyclic
6511da177e4SLinus Torvalds  * tree A at the topmost level (ie: as a direct child of A)
6521da177e4SLinus Torvalds  * - since we are adding B to A at the top level, checking for cycles should
6531da177e4SLinus Torvalds  *   just be a matter of seeing if node A is somewhere in tree B
6541da177e4SLinus Torvalds  */
6551da177e4SLinus Torvalds static int keyring_detect_cycle(struct key *A, struct key *B)
6561da177e4SLinus Torvalds {
6571da177e4SLinus Torvalds 	struct {
65876d8aeabSDavid Howells 		struct keyring_list *keylist;
6591da177e4SLinus Torvalds 		int kix;
6601da177e4SLinus Torvalds 	} stack[KEYRING_SEARCH_MAX_DEPTH];
6611da177e4SLinus Torvalds 
6621da177e4SLinus Torvalds 	struct keyring_list *keylist;
6631da177e4SLinus Torvalds 	struct key *subtree, *key;
6641da177e4SLinus Torvalds 	int sp, kix, ret;
6651da177e4SLinus Torvalds 
66676d8aeabSDavid Howells 	rcu_read_lock();
66776d8aeabSDavid Howells 
6681da177e4SLinus Torvalds 	ret = -EDEADLK;
6691da177e4SLinus Torvalds 	if (A == B)
67076d8aeabSDavid Howells 		goto cycle_detected;
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds 	subtree = B;
6731da177e4SLinus Torvalds 	sp = 0;
6741da177e4SLinus Torvalds 
6751da177e4SLinus Torvalds 	/* start processing a new keyring */
6761da177e4SLinus Torvalds  descend:
67776d8aeabSDavid Howells 	if (test_bit(KEY_FLAG_REVOKED, &subtree->flags))
6781da177e4SLinus Torvalds 		goto not_this_keyring;
6791da177e4SLinus Torvalds 
68076d8aeabSDavid Howells 	keylist = rcu_dereference(subtree->payload.subscriptions);
6811da177e4SLinus Torvalds 	if (!keylist)
6821da177e4SLinus Torvalds 		goto not_this_keyring;
6831da177e4SLinus Torvalds 	kix = 0;
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds  ascend:
6861da177e4SLinus Torvalds 	/* iterate through the remaining keys in this keyring */
6871da177e4SLinus Torvalds 	for (; kix < keylist->nkeys; kix++) {
6881da177e4SLinus Torvalds 		key = keylist->keys[kix];
6891da177e4SLinus Torvalds 
6901da177e4SLinus Torvalds 		if (key == A)
6911da177e4SLinus Torvalds 			goto cycle_detected;
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds 		/* recursively check nested keyrings */
6941da177e4SLinus Torvalds 		if (key->type == &key_type_keyring) {
6951da177e4SLinus Torvalds 			if (sp >= KEYRING_SEARCH_MAX_DEPTH)
6961da177e4SLinus Torvalds 				goto too_deep;
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds 			/* stack the current position */
69976d8aeabSDavid Howells 			stack[sp].keylist = keylist;
7001da177e4SLinus Torvalds 			stack[sp].kix = kix;
7011da177e4SLinus Torvalds 			sp++;
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds 			/* begin again with the new keyring */
7041da177e4SLinus Torvalds 			subtree = key;
7051da177e4SLinus Torvalds 			goto descend;
7061da177e4SLinus Torvalds 		}
7071da177e4SLinus Torvalds 	}
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds 	/* the keyring we're looking at was disqualified or didn't contain a
7101da177e4SLinus Torvalds 	 * matching key */
7111da177e4SLinus Torvalds  not_this_keyring:
7121da177e4SLinus Torvalds 	if (sp > 0) {
7131da177e4SLinus Torvalds 		/* resume the checking of a keyring higher up in the tree */
7141da177e4SLinus Torvalds 		sp--;
71576d8aeabSDavid Howells 		keylist = stack[sp].keylist;
7161da177e4SLinus Torvalds 		kix = stack[sp].kix + 1;
7171da177e4SLinus Torvalds 		goto ascend;
7181da177e4SLinus Torvalds 	}
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds 	ret = 0; /* no cycles detected */
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds  error:
72376d8aeabSDavid Howells 	rcu_read_unlock();
7241da177e4SLinus Torvalds 	return ret;
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds  too_deep:
7271da177e4SLinus Torvalds 	ret = -ELOOP;
72876d8aeabSDavid Howells 	goto error;
72976d8aeabSDavid Howells 
7301da177e4SLinus Torvalds  cycle_detected:
7311da177e4SLinus Torvalds 	ret = -EDEADLK;
7321da177e4SLinus Torvalds 	goto error;
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds } /* end keyring_detect_cycle() */
7351da177e4SLinus Torvalds 
7361da177e4SLinus Torvalds /*****************************************************************************/
7371da177e4SLinus Torvalds /*
73876d8aeabSDavid Howells  * dispose of a keyring list after the RCU grace period
73976d8aeabSDavid Howells  */
74076d8aeabSDavid Howells static void keyring_link_rcu_disposal(struct rcu_head *rcu)
74176d8aeabSDavid Howells {
74276d8aeabSDavid Howells 	struct keyring_list *klist =
74376d8aeabSDavid Howells 		container_of(rcu, struct keyring_list, rcu);
74476d8aeabSDavid Howells 
74576d8aeabSDavid Howells 	kfree(klist);
74676d8aeabSDavid Howells 
74776d8aeabSDavid Howells } /* end keyring_link_rcu_disposal() */
74876d8aeabSDavid Howells 
74976d8aeabSDavid Howells /*****************************************************************************/
75076d8aeabSDavid Howells /*
7511da177e4SLinus Torvalds  * link a key into to a keyring
75276d8aeabSDavid Howells  * - must be called with the keyring's semaphore write-locked
7531da177e4SLinus Torvalds  */
7541da177e4SLinus Torvalds int __key_link(struct key *keyring, struct key *key)
7551da177e4SLinus Torvalds {
7561da177e4SLinus Torvalds 	struct keyring_list *klist, *nklist;
7571da177e4SLinus Torvalds 	unsigned max;
7581da177e4SLinus Torvalds 	size_t size;
7591da177e4SLinus Torvalds 	int ret;
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds 	ret = -EKEYREVOKED;
76276d8aeabSDavid Howells 	if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
7631da177e4SLinus Torvalds 		goto error;
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds 	ret = -ENOTDIR;
7661da177e4SLinus Torvalds 	if (keyring->type != &key_type_keyring)
7671da177e4SLinus Torvalds 		goto error;
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds 	/* serialise link/link calls to prevent parallel calls causing a
7701da177e4SLinus Torvalds 	 * cycle when applied to two keyring in opposite orders */
7711da177e4SLinus Torvalds 	down_write(&keyring_serialise_link_sem);
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds 	/* check that we aren't going to create a cycle adding one keyring to
7741da177e4SLinus Torvalds 	 * another */
7751da177e4SLinus Torvalds 	if (key->type == &key_type_keyring) {
7761da177e4SLinus Torvalds 		ret = keyring_detect_cycle(keyring, key);
7771da177e4SLinus Torvalds 		if (ret < 0)
7781da177e4SLinus Torvalds 			goto error2;
7791da177e4SLinus Torvalds 	}
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds 	/* check that we aren't going to overrun the user's quota */
7821da177e4SLinus Torvalds 	ret = key_payload_reserve(keyring,
7831da177e4SLinus Torvalds 				  keyring->datalen + KEYQUOTA_LINK_BYTES);
7841da177e4SLinus Torvalds 	if (ret < 0)
7851da177e4SLinus Torvalds 		goto error2;
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds 	klist = keyring->payload.subscriptions;
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds 	if (klist && klist->nkeys < klist->maxkeys) {
7901da177e4SLinus Torvalds 		/* there's sufficient slack space to add directly */
7911da177e4SLinus Torvalds 		atomic_inc(&key->usage);
7921da177e4SLinus Torvalds 
79376d8aeabSDavid Howells 		klist->keys[klist->nkeys] = key;
79476d8aeabSDavid Howells 		smp_wmb();
79576d8aeabSDavid Howells 		klist->nkeys++;
79676d8aeabSDavid Howells 		smp_wmb();
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds 		ret = 0;
7991da177e4SLinus Torvalds 	}
8001da177e4SLinus Torvalds 	else {
8011da177e4SLinus Torvalds 		/* grow the key list */
8021da177e4SLinus Torvalds 		max = 4;
8031da177e4SLinus Torvalds 		if (klist)
8041da177e4SLinus Torvalds 			max += klist->maxkeys;
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds 		ret = -ENFILE;
80776d8aeabSDavid Howells 		if (max > 65535)
80876d8aeabSDavid Howells 			goto error3;
809a4014d8fSDavid Howells 		size = sizeof(*klist) + sizeof(struct key *) * max;
8101da177e4SLinus Torvalds 		if (size > PAGE_SIZE)
8111da177e4SLinus Torvalds 			goto error3;
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds 		ret = -ENOMEM;
8141da177e4SLinus Torvalds 		nklist = kmalloc(size, GFP_KERNEL);
8151da177e4SLinus Torvalds 		if (!nklist)
8161da177e4SLinus Torvalds 			goto error3;
8171da177e4SLinus Torvalds 		nklist->maxkeys = max;
8181da177e4SLinus Torvalds 		nklist->nkeys = 0;
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds 		if (klist) {
8211da177e4SLinus Torvalds 			nklist->nkeys = klist->nkeys;
8221da177e4SLinus Torvalds 			memcpy(nklist->keys,
8231da177e4SLinus Torvalds 			       klist->keys,
8241da177e4SLinus Torvalds 			       sizeof(struct key *) * klist->nkeys);
8251da177e4SLinus Torvalds 		}
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds 		/* add the key into the new space */
8281da177e4SLinus Torvalds 		atomic_inc(&key->usage);
8291da177e4SLinus Torvalds 		nklist->keys[nklist->nkeys++] = key;
83076d8aeabSDavid Howells 
83176d8aeabSDavid Howells 		rcu_assign_pointer(keyring->payload.subscriptions, nklist);
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds 		/* dispose of the old keyring list */
83476d8aeabSDavid Howells 		if (klist)
83576d8aeabSDavid Howells 			call_rcu(&klist->rcu, keyring_link_rcu_disposal);
8361da177e4SLinus Torvalds 
8371da177e4SLinus Torvalds 		ret = 0;
8381da177e4SLinus Torvalds 	}
8391da177e4SLinus Torvalds 
8401da177e4SLinus Torvalds  error2:
8411da177e4SLinus Torvalds 	up_write(&keyring_serialise_link_sem);
8421da177e4SLinus Torvalds  error:
8431da177e4SLinus Torvalds 	return ret;
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds  error3:
8461da177e4SLinus Torvalds 	/* undo the quota changes */
8471da177e4SLinus Torvalds 	key_payload_reserve(keyring,
8481da177e4SLinus Torvalds 			    keyring->datalen - KEYQUOTA_LINK_BYTES);
8491da177e4SLinus Torvalds 	goto error2;
8501da177e4SLinus Torvalds 
8511da177e4SLinus Torvalds } /* end __key_link() */
8521da177e4SLinus Torvalds 
8531da177e4SLinus Torvalds /*****************************************************************************/
8541da177e4SLinus Torvalds /*
8551da177e4SLinus Torvalds  * link a key to a keyring
8561da177e4SLinus Torvalds  */
8571da177e4SLinus Torvalds int key_link(struct key *keyring, struct key *key)
8581da177e4SLinus Torvalds {
8591da177e4SLinus Torvalds 	int ret;
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds 	key_check(keyring);
8621da177e4SLinus Torvalds 	key_check(key);
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds 	down_write(&keyring->sem);
8651da177e4SLinus Torvalds 	ret = __key_link(keyring, key);
8661da177e4SLinus Torvalds 	up_write(&keyring->sem);
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds 	return ret;
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds } /* end key_link() */
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds EXPORT_SYMBOL(key_link);
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds /*****************************************************************************/
8751da177e4SLinus Torvalds /*
87676d8aeabSDavid Howells  * dispose of a keyring list after the RCU grace period, freeing the unlinked
87776d8aeabSDavid Howells  * key
87876d8aeabSDavid Howells  */
87976d8aeabSDavid Howells static void keyring_unlink_rcu_disposal(struct rcu_head *rcu)
88076d8aeabSDavid Howells {
88176d8aeabSDavid Howells 	struct keyring_list *klist =
88276d8aeabSDavid Howells 		container_of(rcu, struct keyring_list, rcu);
88376d8aeabSDavid Howells 
88476d8aeabSDavid Howells 	key_put(klist->keys[klist->delkey]);
88576d8aeabSDavid Howells 	kfree(klist);
88676d8aeabSDavid Howells 
88776d8aeabSDavid Howells } /* end keyring_unlink_rcu_disposal() */
88876d8aeabSDavid Howells 
88976d8aeabSDavid Howells /*****************************************************************************/
89076d8aeabSDavid Howells /*
8911da177e4SLinus Torvalds  * unlink the first link to a key from a keyring
8921da177e4SLinus Torvalds  */
8931da177e4SLinus Torvalds int key_unlink(struct key *keyring, struct key *key)
8941da177e4SLinus Torvalds {
89576d8aeabSDavid Howells 	struct keyring_list *klist, *nklist;
8961da177e4SLinus Torvalds 	int loop, ret;
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds 	key_check(keyring);
8991da177e4SLinus Torvalds 	key_check(key);
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds 	ret = -ENOTDIR;
9021da177e4SLinus Torvalds 	if (keyring->type != &key_type_keyring)
9031da177e4SLinus Torvalds 		goto error;
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds 	down_write(&keyring->sem);
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds 	klist = keyring->payload.subscriptions;
9081da177e4SLinus Torvalds 	if (klist) {
9091da177e4SLinus Torvalds 		/* search the keyring for the key */
9101da177e4SLinus Torvalds 		for (loop = 0; loop < klist->nkeys; loop++)
9111da177e4SLinus Torvalds 			if (klist->keys[loop] == key)
9121da177e4SLinus Torvalds 				goto key_is_present;
9131da177e4SLinus Torvalds 	}
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds 	up_write(&keyring->sem);
9161da177e4SLinus Torvalds 	ret = -ENOENT;
9171da177e4SLinus Torvalds 	goto error;
9181da177e4SLinus Torvalds 
9191da177e4SLinus Torvalds key_is_present:
92076d8aeabSDavid Howells 	/* we need to copy the key list for RCU purposes */
921a4014d8fSDavid Howells 	nklist = kmalloc(sizeof(*klist) +
922a4014d8fSDavid Howells 			 sizeof(struct key *) * klist->maxkeys,
92376d8aeabSDavid Howells 			 GFP_KERNEL);
92476d8aeabSDavid Howells 	if (!nklist)
92576d8aeabSDavid Howells 		goto nomem;
92676d8aeabSDavid Howells 	nklist->maxkeys = klist->maxkeys;
92776d8aeabSDavid Howells 	nklist->nkeys = klist->nkeys - 1;
92876d8aeabSDavid Howells 
92976d8aeabSDavid Howells 	if (loop > 0)
93076d8aeabSDavid Howells 		memcpy(&nklist->keys[0],
93176d8aeabSDavid Howells 		       &klist->keys[0],
932a4014d8fSDavid Howells 		       loop * sizeof(struct key *));
93376d8aeabSDavid Howells 
93476d8aeabSDavid Howells 	if (loop < nklist->nkeys)
93576d8aeabSDavid Howells 		memcpy(&nklist->keys[loop],
93676d8aeabSDavid Howells 		       &klist->keys[loop + 1],
937a4014d8fSDavid Howells 		       (nklist->nkeys - loop) * sizeof(struct key *));
93876d8aeabSDavid Howells 
9391da177e4SLinus Torvalds 	/* adjust the user's quota */
9401da177e4SLinus Torvalds 	key_payload_reserve(keyring,
9411da177e4SLinus Torvalds 			    keyring->datalen - KEYQUOTA_LINK_BYTES);
9421da177e4SLinus Torvalds 
94376d8aeabSDavid Howells 	rcu_assign_pointer(keyring->payload.subscriptions, nklist);
9441da177e4SLinus Torvalds 
9451da177e4SLinus Torvalds 	up_write(&keyring->sem);
94676d8aeabSDavid Howells 
94776d8aeabSDavid Howells 	/* schedule for later cleanup */
94876d8aeabSDavid Howells 	klist->delkey = loop;
94976d8aeabSDavid Howells 	call_rcu(&klist->rcu, keyring_unlink_rcu_disposal);
95076d8aeabSDavid Howells 
9511da177e4SLinus Torvalds 	ret = 0;
9521da177e4SLinus Torvalds 
9531da177e4SLinus Torvalds error:
9541da177e4SLinus Torvalds 	return ret;
95576d8aeabSDavid Howells nomem:
95676d8aeabSDavid Howells 	ret = -ENOMEM;
95776d8aeabSDavid Howells 	up_write(&keyring->sem);
95876d8aeabSDavid Howells 	goto error;
9591da177e4SLinus Torvalds 
9601da177e4SLinus Torvalds } /* end key_unlink() */
9611da177e4SLinus Torvalds 
9621da177e4SLinus Torvalds EXPORT_SYMBOL(key_unlink);
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds /*****************************************************************************/
9651da177e4SLinus Torvalds /*
96676d8aeabSDavid Howells  * dispose of a keyring list after the RCU grace period, releasing the keys it
96776d8aeabSDavid Howells  * links to
96876d8aeabSDavid Howells  */
96976d8aeabSDavid Howells static void keyring_clear_rcu_disposal(struct rcu_head *rcu)
97076d8aeabSDavid Howells {
97176d8aeabSDavid Howells 	struct keyring_list *klist;
97276d8aeabSDavid Howells 	int loop;
97376d8aeabSDavid Howells 
97476d8aeabSDavid Howells 	klist = container_of(rcu, struct keyring_list, rcu);
97576d8aeabSDavid Howells 
97676d8aeabSDavid Howells 	for (loop = klist->nkeys - 1; loop >= 0; loop--)
97776d8aeabSDavid Howells 		key_put(klist->keys[loop]);
97876d8aeabSDavid Howells 
97976d8aeabSDavid Howells 	kfree(klist);
98076d8aeabSDavid Howells 
98176d8aeabSDavid Howells } /* end keyring_clear_rcu_disposal() */
98276d8aeabSDavid Howells 
98376d8aeabSDavid Howells /*****************************************************************************/
98476d8aeabSDavid Howells /*
9851da177e4SLinus Torvalds  * clear the specified process keyring
9861da177e4SLinus Torvalds  * - implements keyctl(KEYCTL_CLEAR)
9871da177e4SLinus Torvalds  */
9881da177e4SLinus Torvalds int keyring_clear(struct key *keyring)
9891da177e4SLinus Torvalds {
9901da177e4SLinus Torvalds 	struct keyring_list *klist;
99176d8aeabSDavid Howells 	int ret;
9921da177e4SLinus Torvalds 
9931da177e4SLinus Torvalds 	ret = -ENOTDIR;
9941da177e4SLinus Torvalds 	if (keyring->type == &key_type_keyring) {
9951da177e4SLinus Torvalds 		/* detach the pointer block with the locks held */
9961da177e4SLinus Torvalds 		down_write(&keyring->sem);
9971da177e4SLinus Torvalds 
9981da177e4SLinus Torvalds 		klist = keyring->payload.subscriptions;
9991da177e4SLinus Torvalds 		if (klist) {
10001da177e4SLinus Torvalds 			/* adjust the quota */
10011da177e4SLinus Torvalds 			key_payload_reserve(keyring,
10021da177e4SLinus Torvalds 					    sizeof(struct keyring_list));
10031da177e4SLinus Torvalds 
100476d8aeabSDavid Howells 			rcu_assign_pointer(keyring->payload.subscriptions,
100576d8aeabSDavid Howells 					   NULL);
10061da177e4SLinus Torvalds 		}
10071da177e4SLinus Torvalds 
10081da177e4SLinus Torvalds 		up_write(&keyring->sem);
10091da177e4SLinus Torvalds 
10101da177e4SLinus Torvalds 		/* free the keys after the locks have been dropped */
101176d8aeabSDavid Howells 		if (klist)
101276d8aeabSDavid Howells 			call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
10131da177e4SLinus Torvalds 
10141da177e4SLinus Torvalds 		ret = 0;
10151da177e4SLinus Torvalds 	}
10161da177e4SLinus Torvalds 
10171da177e4SLinus Torvalds 	return ret;
10181da177e4SLinus Torvalds 
10191da177e4SLinus Torvalds } /* end keyring_clear() */
10201da177e4SLinus Torvalds 
10211da177e4SLinus Torvalds EXPORT_SYMBOL(keyring_clear);
1022