xref: /openbmc/linux/security/keys/keyring.c (revision 24f68eb5bf14a74027946970a18bc902e19d986a)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
269664cf1SDavid Howells /* Keyring handling
31da177e4SLinus Torvalds  *
4b2a4df20SDavid Howells  * Copyright (C) 2004-2005, 2008, 2013 Red Hat, Inc. All Rights Reserved.
51da177e4SLinus Torvalds  * Written by David Howells (dhowells@redhat.com)
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
8876979c9SPaul Gortmaker #include <linux/export.h>
91da177e4SLinus Torvalds #include <linux/init.h>
101da177e4SLinus Torvalds #include <linux/sched.h>
111da177e4SLinus Torvalds #include <linux/slab.h>
1229db9190SDavid Howells #include <linux/security.h>
131da177e4SLinus Torvalds #include <linux/seq_file.h>
141da177e4SLinus Torvalds #include <linux/err.h>
15b206f281SDavid Howells #include <linux/user_namespace.h>
169b242610SDavid Howells #include <linux/nsproxy.h>
17e9e349b0SDavid Howells #include <keys/keyring-type.h>
18b2a4df20SDavid Howells #include <keys/user-type.h>
19b2a4df20SDavid Howells #include <linux/assoc_array_priv.h>
20512ea3bcSChihau Chau #include <linux/uaccess.h>
219b242610SDavid Howells #include <net/net_namespace.h>
221da177e4SLinus Torvalds #include "internal.h"
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds /*
25973c9f4fSDavid Howells  * When plumbing the depths of the key tree, this sets a hard limit
26973c9f4fSDavid Howells  * set on how deep we're willing to go.
271da177e4SLinus Torvalds  */
281da177e4SLinus Torvalds #define KEYRING_SEARCH_MAX_DEPTH 6
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds /*
31b2a4df20SDavid Howells  * We mark pointers we pass to the associative array with bit 1 set if
32b2a4df20SDavid Howells  * they're keyrings and clear otherwise.
33b2a4df20SDavid Howells  */
34b2a4df20SDavid Howells #define KEYRING_PTR_SUBTYPE	0x2UL
35b2a4df20SDavid Howells 
keyring_ptr_is_keyring(const struct assoc_array_ptr * x)36b2a4df20SDavid Howells static inline bool keyring_ptr_is_keyring(const struct assoc_array_ptr *x)
37b2a4df20SDavid Howells {
38b2a4df20SDavid Howells 	return (unsigned long)x & KEYRING_PTR_SUBTYPE;
39b2a4df20SDavid Howells }
keyring_ptr_to_key(const struct assoc_array_ptr * x)40b2a4df20SDavid Howells static inline struct key *keyring_ptr_to_key(const struct assoc_array_ptr *x)
41b2a4df20SDavid Howells {
42b2a4df20SDavid Howells 	void *object = assoc_array_ptr_to_leaf(x);
43b2a4df20SDavid Howells 	return (struct key *)((unsigned long)object & ~KEYRING_PTR_SUBTYPE);
44b2a4df20SDavid Howells }
keyring_key_to_ptr(struct key * key)45b2a4df20SDavid Howells static inline void *keyring_key_to_ptr(struct key *key)
46b2a4df20SDavid Howells {
47b2a4df20SDavid Howells 	if (key->type == &key_type_keyring)
48b2a4df20SDavid Howells 		return (void *)((unsigned long)key | KEYRING_PTR_SUBTYPE);
49b2a4df20SDavid Howells 	return key;
50b2a4df20SDavid Howells }
51b2a4df20SDavid Howells 
521da177e4SLinus Torvalds static DEFINE_RWLOCK(keyring_name_lock);
531da177e4SLinus Torvalds 
54b206f281SDavid Howells /*
55b206f281SDavid Howells  * Clean up the bits of user_namespace that belong to us.
56b206f281SDavid Howells  */
key_free_user_ns(struct user_namespace * ns)57b206f281SDavid Howells void key_free_user_ns(struct user_namespace *ns)
581da177e4SLinus Torvalds {
59b206f281SDavid Howells 	write_lock(&keyring_name_lock);
60b206f281SDavid Howells 	list_del_init(&ns->keyring_name_list);
61b206f281SDavid Howells 	write_unlock(&keyring_name_lock);
621da177e4SLinus Torvalds 
630f44e4d9SDavid Howells 	key_put(ns->user_keyring_register);
64b206f281SDavid Howells #ifdef CONFIG_PERSISTENT_KEYRINGS
65b206f281SDavid Howells 	key_put(ns->persistent_keyring_register);
66b206f281SDavid Howells #endif
671da177e4SLinus Torvalds }
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds /*
70973c9f4fSDavid Howells  * The keyring key type definition.  Keyrings are simply keys of this type and
71973c9f4fSDavid Howells  * can be treated as ordinary keys in addition to having their own special
72973c9f4fSDavid Howells  * operations.
731da177e4SLinus Torvalds  */
745d19e20bSDavid Howells static int keyring_preparse(struct key_preparsed_payload *prep);
755d19e20bSDavid Howells static void keyring_free_preparse(struct key_preparsed_payload *prep);
761da177e4SLinus Torvalds static int keyring_instantiate(struct key *keyring,
77cf7f601cSDavid Howells 			       struct key_preparsed_payload *prep);
7831204ed9SDavid Howells static void keyring_revoke(struct key *keyring);
791da177e4SLinus Torvalds static void keyring_destroy(struct key *keyring);
801da177e4SLinus Torvalds static void keyring_describe(const struct key *keyring, struct seq_file *m);
811da177e4SLinus Torvalds static long keyring_read(const struct key *keyring,
8272e9be6bSVincenzo Frascino 			 char *buffer, size_t buflen);
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds struct key_type key_type_keyring = {
851da177e4SLinus Torvalds 	.name		= "keyring",
86b2a4df20SDavid Howells 	.def_datalen	= 0,
875d19e20bSDavid Howells 	.preparse	= keyring_preparse,
885d19e20bSDavid Howells 	.free_preparse	= keyring_free_preparse,
891da177e4SLinus Torvalds 	.instantiate	= keyring_instantiate,
9031204ed9SDavid Howells 	.revoke		= keyring_revoke,
911da177e4SLinus Torvalds 	.destroy	= keyring_destroy,
921da177e4SLinus Torvalds 	.describe	= keyring_describe,
931da177e4SLinus Torvalds 	.read		= keyring_read,
941da177e4SLinus Torvalds };
957318226eSDavid Howells EXPORT_SYMBOL(key_type_keyring);
967318226eSDavid Howells 
971da177e4SLinus Torvalds /*
98973c9f4fSDavid Howells  * Semaphore to serialise link/link calls to prevent two link calls in parallel
99973c9f4fSDavid Howells  * introducing a cycle.
1001da177e4SLinus Torvalds  */
1013be59f74SDavid Howells static DEFINE_MUTEX(keyring_serialise_link_lock);
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds /*
104973c9f4fSDavid Howells  * Publish the name of a keyring so that it can be found by name (if it has
105b206f281SDavid Howells  * one and it doesn't begin with a dot).
1061da177e4SLinus Torvalds  */
keyring_publish_name(struct key * keyring)10769664cf1SDavid Howells static void keyring_publish_name(struct key *keyring)
1081da177e4SLinus Torvalds {
109b206f281SDavid Howells 	struct user_namespace *ns = current_user_ns();
1101da177e4SLinus Torvalds 
111b206f281SDavid Howells 	if (keyring->description &&
112b206f281SDavid Howells 	    keyring->description[0] &&
113b206f281SDavid Howells 	    keyring->description[0] != '.') {
1141da177e4SLinus Torvalds 		write_lock(&keyring_name_lock);
115b206f281SDavid Howells 		list_add_tail(&keyring->name_link, &ns->keyring_name_list);
1161da177e4SLinus Torvalds 		write_unlock(&keyring_name_lock);
1171da177e4SLinus Torvalds 	}
118a8b17ed0SDavid Howells }
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds /*
1215d19e20bSDavid Howells  * Preparse a keyring payload
1225d19e20bSDavid Howells  */
keyring_preparse(struct key_preparsed_payload * prep)1235d19e20bSDavid Howells static int keyring_preparse(struct key_preparsed_payload *prep)
1245d19e20bSDavid Howells {
1255d19e20bSDavid Howells 	return prep->datalen != 0 ? -EINVAL : 0;
1265d19e20bSDavid Howells }
1275d19e20bSDavid Howells 
1285d19e20bSDavid Howells /*
1295d19e20bSDavid Howells  * Free a preparse of a user defined key payload
1305d19e20bSDavid Howells  */
keyring_free_preparse(struct key_preparsed_payload * prep)1315d19e20bSDavid Howells static void keyring_free_preparse(struct key_preparsed_payload *prep)
1325d19e20bSDavid Howells {
1335d19e20bSDavid Howells }
1345d19e20bSDavid Howells 
1355d19e20bSDavid Howells /*
136973c9f4fSDavid Howells  * Initialise a keyring.
137973c9f4fSDavid Howells  *
138973c9f4fSDavid Howells  * Returns 0 on success, -EINVAL if given any data.
1391da177e4SLinus Torvalds  */
keyring_instantiate(struct key * keyring,struct key_preparsed_payload * prep)1401da177e4SLinus Torvalds static int keyring_instantiate(struct key *keyring,
141cf7f601cSDavid Howells 			       struct key_preparsed_payload *prep)
1421da177e4SLinus Torvalds {
143b2a4df20SDavid Howells 	assoc_array_init(&keyring->keys);
1441da177e4SLinus Torvalds 	/* make the keyring available by name if it has one */
1451da177e4SLinus Torvalds 	keyring_publish_name(keyring);
1465d19e20bSDavid Howells 	return 0;
147a8b17ed0SDavid Howells }
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds /*
150b2a4df20SDavid Howells  * Multiply 64-bits by 32-bits to 96-bits and fold back to 64-bit.  Ideally we'd
151b2a4df20SDavid Howells  * fold the carry back too, but that requires inline asm.
1521da177e4SLinus Torvalds  */
mult_64x32_and_fold(u64 x,u32 y)153b2a4df20SDavid Howells static u64 mult_64x32_and_fold(u64 x, u32 y)
1541da177e4SLinus Torvalds {
155b2a4df20SDavid Howells 	u64 hi = (u64)(u32)(x >> 32) * y;
156b2a4df20SDavid Howells 	u64 lo = (u64)(u32)(x) * y;
157b2a4df20SDavid Howells 	return lo + ((u64)(u32)hi << 32) + (u32)(hi >> 32);
158a8b17ed0SDavid Howells }
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds /*
161b2a4df20SDavid Howells  * Hash a key type and description.
162b2a4df20SDavid Howells  */
hash_key_type_and_desc(struct keyring_index_key * index_key)163355ef8e1SDavid Howells static void hash_key_type_and_desc(struct keyring_index_key *index_key)
164b2a4df20SDavid Howells {
165b2a4df20SDavid Howells 	const unsigned level_shift = ASSOC_ARRAY_LEVEL_STEP;
166d54e58b7SDavid Howells 	const unsigned long fan_mask = ASSOC_ARRAY_FAN_MASK;
167b2a4df20SDavid Howells 	const char *description = index_key->description;
168b2a4df20SDavid Howells 	unsigned long hash, type;
169b2a4df20SDavid Howells 	u32 piece;
170b2a4df20SDavid Howells 	u64 acc;
171b2a4df20SDavid Howells 	int n, desc_len = index_key->desc_len;
172b2a4df20SDavid Howells 
173b2a4df20SDavid Howells 	type = (unsigned long)index_key->type;
174b2a4df20SDavid Howells 	acc = mult_64x32_and_fold(type, desc_len + 13);
175b2a4df20SDavid Howells 	acc = mult_64x32_and_fold(acc, 9207);
1763b6e4de0SDavid Howells 	piece = (unsigned long)index_key->domain_tag;
1773b6e4de0SDavid Howells 	acc = mult_64x32_and_fold(acc, piece);
1783b6e4de0SDavid Howells 	acc = mult_64x32_and_fold(acc, 9207);
179f771fde8SDavid Howells 
180b2a4df20SDavid Howells 	for (;;) {
181b2a4df20SDavid Howells 		n = desc_len;
182b2a4df20SDavid Howells 		if (n <= 0)
183b2a4df20SDavid Howells 			break;
184b2a4df20SDavid Howells 		if (n > 4)
185b2a4df20SDavid Howells 			n = 4;
186b2a4df20SDavid Howells 		piece = 0;
187b2a4df20SDavid Howells 		memcpy(&piece, description, n);
188b2a4df20SDavid Howells 		description += n;
189b2a4df20SDavid Howells 		desc_len -= n;
190b2a4df20SDavid Howells 		acc = mult_64x32_and_fold(acc, piece);
191b2a4df20SDavid Howells 		acc = mult_64x32_and_fold(acc, 9207);
192b2a4df20SDavid Howells 	}
193b2a4df20SDavid Howells 
194b2a4df20SDavid Howells 	/* Fold the hash down to 32 bits if need be. */
195b2a4df20SDavid Howells 	hash = acc;
196b2a4df20SDavid Howells 	if (ASSOC_ARRAY_KEY_CHUNK_SIZE == 32)
197b2a4df20SDavid Howells 		hash ^= acc >> 32;
198b2a4df20SDavid Howells 
199b2a4df20SDavid Howells 	/* Squidge all the keyrings into a separate part of the tree to
200b2a4df20SDavid Howells 	 * ordinary keys by making sure the lowest level segment in the hash is
201b2a4df20SDavid Howells 	 * zero for keyrings and non-zero otherwise.
202b2a4df20SDavid Howells 	 */
203d54e58b7SDavid Howells 	if (index_key->type != &key_type_keyring && (hash & fan_mask) == 0)
204355ef8e1SDavid Howells 		hash |= (hash >> (ASSOC_ARRAY_KEY_CHUNK_SIZE - level_shift)) | 1;
205355ef8e1SDavid Howells 	else if (index_key->type == &key_type_keyring && (hash & fan_mask) != 0)
206355ef8e1SDavid Howells 		hash = (hash + (hash << level_shift)) & ~fan_mask;
207355ef8e1SDavid Howells 	index_key->hash = hash;
208355ef8e1SDavid Howells }
209355ef8e1SDavid Howells 
210355ef8e1SDavid Howells /*
211355ef8e1SDavid Howells  * Finalise an index key to include a part of the description actually in the
2123b6e4de0SDavid Howells  * index key, to set the domain tag and to calculate the hash.
213355ef8e1SDavid Howells  */
key_set_index_key(struct keyring_index_key * index_key)214355ef8e1SDavid Howells void key_set_index_key(struct keyring_index_key *index_key)
215355ef8e1SDavid Howells {
2163b6e4de0SDavid Howells 	static struct key_tag default_domain_tag = { .usage = REFCOUNT_INIT(1), };
217355ef8e1SDavid Howells 	size_t n = min_t(size_t, index_key->desc_len, sizeof(index_key->desc));
2183b6e4de0SDavid Howells 
219355ef8e1SDavid Howells 	memcpy(index_key->desc, index_key->description, n);
220355ef8e1SDavid Howells 
221a58946c1SDavid Howells 	if (!index_key->domain_tag) {
2229b242610SDavid Howells 		if (index_key->type->flags & KEY_TYPE_NET_DOMAIN)
2239b242610SDavid Howells 			index_key->domain_tag = current->nsproxy->net_ns->key_domain;
2249b242610SDavid Howells 		else
2253b6e4de0SDavid Howells 			index_key->domain_tag = &default_domain_tag;
226a58946c1SDavid Howells 	}
227a58946c1SDavid Howells 
228355ef8e1SDavid Howells 	hash_key_type_and_desc(index_key);
229b2a4df20SDavid Howells }
230b2a4df20SDavid Howells 
2313b6e4de0SDavid Howells /**
2323b6e4de0SDavid Howells  * key_put_tag - Release a ref on a tag.
2333b6e4de0SDavid Howells  * @tag: The tag to release.
2343b6e4de0SDavid Howells  *
2353b6e4de0SDavid Howells  * This releases a reference the given tag and returns true if that ref was the
2363b6e4de0SDavid Howells  * last one.
2373b6e4de0SDavid Howells  */
key_put_tag(struct key_tag * tag)2383b6e4de0SDavid Howells bool key_put_tag(struct key_tag *tag)
2393b6e4de0SDavid Howells {
2403b6e4de0SDavid Howells 	if (refcount_dec_and_test(&tag->usage)) {
2413b6e4de0SDavid Howells 		kfree_rcu(tag, rcu);
2423b6e4de0SDavid Howells 		return true;
2433b6e4de0SDavid Howells 	}
2443b6e4de0SDavid Howells 
2453b6e4de0SDavid Howells 	return false;
2463b6e4de0SDavid Howells }
2473b6e4de0SDavid Howells 
248218e6424SDavid Howells /**
249218e6424SDavid Howells  * key_remove_domain - Kill off a key domain and gc its keys
250218e6424SDavid Howells  * @domain_tag: The domain tag to release.
251218e6424SDavid Howells  *
252218e6424SDavid Howells  * This marks a domain tag as being dead and releases a ref on it.  If that
253218e6424SDavid Howells  * wasn't the last reference, the garbage collector is poked to try and delete
254218e6424SDavid Howells  * all keys that were in the domain.
255218e6424SDavid Howells  */
key_remove_domain(struct key_tag * domain_tag)256218e6424SDavid Howells void key_remove_domain(struct key_tag *domain_tag)
257218e6424SDavid Howells {
258218e6424SDavid Howells 	domain_tag->removed = true;
259218e6424SDavid Howells 	if (!key_put_tag(domain_tag))
260218e6424SDavid Howells 		key_schedule_gc_links();
261b2a4df20SDavid Howells }
262b2a4df20SDavid Howells 
263b2a4df20SDavid Howells /*
264b2a4df20SDavid Howells  * Build the next index key chunk.
265b2a4df20SDavid Howells  *
266b2a4df20SDavid Howells  * We return it one word-sized chunk at a time.
267b2a4df20SDavid Howells  */
keyring_get_key_chunk(const void * data,int level)268b2a4df20SDavid Howells static unsigned long keyring_get_key_chunk(const void *data, int level)
269b2a4df20SDavid Howells {
270b2a4df20SDavid Howells 	const struct keyring_index_key *index_key = data;
271b2a4df20SDavid Howells 	unsigned long chunk = 0;
272f771fde8SDavid Howells 	const u8 *d;
273b2a4df20SDavid Howells 	int desc_len = index_key->desc_len, n = sizeof(chunk);
274b2a4df20SDavid Howells 
275b2a4df20SDavid Howells 	level /= ASSOC_ARRAY_KEY_CHUNK_SIZE;
276b2a4df20SDavid Howells 	switch (level) {
277b2a4df20SDavid Howells 	case 0:
278355ef8e1SDavid Howells 		return index_key->hash;
279b2a4df20SDavid Howells 	case 1:
280f771fde8SDavid Howells 		return index_key->x;
281b2a4df20SDavid Howells 	case 2:
282f771fde8SDavid Howells 		return (unsigned long)index_key->type;
2833b6e4de0SDavid Howells 	case 3:
2843b6e4de0SDavid Howells 		return (unsigned long)index_key->domain_tag;
285b2a4df20SDavid Howells 	default:
2863b6e4de0SDavid Howells 		level -= 4;
287f771fde8SDavid Howells 		if (desc_len <= sizeof(index_key->desc))
288b2a4df20SDavid Howells 			return 0;
289f771fde8SDavid Howells 
290f771fde8SDavid Howells 		d = index_key->description + sizeof(index_key->desc);
291f771fde8SDavid Howells 		d += level * sizeof(long);
292f771fde8SDavid Howells 		desc_len -= sizeof(index_key->desc);
293b2a4df20SDavid Howells 		if (desc_len > n)
294b2a4df20SDavid Howells 			desc_len = n;
295b2a4df20SDavid Howells 		do {
296b2a4df20SDavid Howells 			chunk <<= 8;
297f771fde8SDavid Howells 			chunk |= *d++;
298b2a4df20SDavid Howells 		} while (--desc_len > 0);
299b2a4df20SDavid Howells 		return chunk;
300b2a4df20SDavid Howells 	}
301b2a4df20SDavid Howells }
302b2a4df20SDavid Howells 
keyring_get_object_key_chunk(const void * object,int level)303b2a4df20SDavid Howells static unsigned long keyring_get_object_key_chunk(const void *object, int level)
304b2a4df20SDavid Howells {
305b2a4df20SDavid Howells 	const struct key *key = keyring_ptr_to_key(object);
306b2a4df20SDavid Howells 	return keyring_get_key_chunk(&key->index_key, level);
307b2a4df20SDavid Howells }
308b2a4df20SDavid Howells 
keyring_compare_object(const void * object,const void * data)309b2a4df20SDavid Howells static bool keyring_compare_object(const void *object, const void *data)
310b2a4df20SDavid Howells {
311b2a4df20SDavid Howells 	const struct keyring_index_key *index_key = data;
312b2a4df20SDavid Howells 	const struct key *key = keyring_ptr_to_key(object);
313b2a4df20SDavid Howells 
314b2a4df20SDavid Howells 	return key->index_key.type == index_key->type &&
3153b6e4de0SDavid Howells 		key->index_key.domain_tag == index_key->domain_tag &&
316b2a4df20SDavid Howells 		key->index_key.desc_len == index_key->desc_len &&
317b2a4df20SDavid Howells 		memcmp(key->index_key.description, index_key->description,
318b2a4df20SDavid Howells 		       index_key->desc_len) == 0;
319b2a4df20SDavid Howells }
320b2a4df20SDavid Howells 
321b2a4df20SDavid Howells /*
322b2a4df20SDavid Howells  * Compare the index keys of a pair of objects and determine the bit position
323b2a4df20SDavid Howells  * at which they differ - if they differ.
324b2a4df20SDavid Howells  */
keyring_diff_objects(const void * object,const void * data)32523fd78d7SDavid Howells static int keyring_diff_objects(const void *object, const void *data)
326b2a4df20SDavid Howells {
32723fd78d7SDavid Howells 	const struct key *key_a = keyring_ptr_to_key(object);
328b2a4df20SDavid Howells 	const struct keyring_index_key *a = &key_a->index_key;
32923fd78d7SDavid Howells 	const struct keyring_index_key *b = data;
330b2a4df20SDavid Howells 	unsigned long seg_a, seg_b;
331b2a4df20SDavid Howells 	int level, i;
332b2a4df20SDavid Howells 
333b2a4df20SDavid Howells 	level = 0;
334355ef8e1SDavid Howells 	seg_a = a->hash;
335355ef8e1SDavid Howells 	seg_b = b->hash;
336b2a4df20SDavid Howells 	if ((seg_a ^ seg_b) != 0)
337b2a4df20SDavid Howells 		goto differ;
338f771fde8SDavid Howells 	level += ASSOC_ARRAY_KEY_CHUNK_SIZE / 8;
339b2a4df20SDavid Howells 
340b2a4df20SDavid Howells 	/* The number of bits contributed by the hash is controlled by a
341b2a4df20SDavid Howells 	 * constant in the assoc_array headers.  Everything else thereafter we
342b2a4df20SDavid Howells 	 * can deal with as being machine word-size dependent.
343b2a4df20SDavid Howells 	 */
344f771fde8SDavid Howells 	seg_a = a->x;
345f771fde8SDavid Howells 	seg_b = b->x;
346b2a4df20SDavid Howells 	if ((seg_a ^ seg_b) != 0)
347b2a4df20SDavid Howells 		goto differ;
348f771fde8SDavid Howells 	level += sizeof(unsigned long);
349b2a4df20SDavid Howells 
350b2a4df20SDavid Howells 	/* The next bit may not work on big endian */
351b2a4df20SDavid Howells 	seg_a = (unsigned long)a->type;
352b2a4df20SDavid Howells 	seg_b = (unsigned long)b->type;
353b2a4df20SDavid Howells 	if ((seg_a ^ seg_b) != 0)
354b2a4df20SDavid Howells 		goto differ;
355b2a4df20SDavid Howells 	level += sizeof(unsigned long);
356b2a4df20SDavid Howells 
3573b6e4de0SDavid Howells 	seg_a = (unsigned long)a->domain_tag;
3583b6e4de0SDavid Howells 	seg_b = (unsigned long)b->domain_tag;
359b2a4df20SDavid Howells 	if ((seg_a ^ seg_b) != 0)
3603b6e4de0SDavid Howells 		goto differ;
3613b6e4de0SDavid Howells 	level += sizeof(unsigned long);
3623b6e4de0SDavid Howells 
363f771fde8SDavid Howells 	i = sizeof(a->desc);
364f771fde8SDavid Howells 	if (a->desc_len <= i)
365f771fde8SDavid Howells 		goto same;
366b2a4df20SDavid Howells 
367b2a4df20SDavid Howells 	for (; i < a->desc_len; i++) {
368b2a4df20SDavid Howells 		seg_a = *(unsigned char *)(a->description + i);
369b2a4df20SDavid Howells 		seg_b = *(unsigned char *)(b->description + i);
370b2a4df20SDavid Howells 		if ((seg_a ^ seg_b) != 0)
371b2a4df20SDavid Howells 			goto differ_plus_i;
372b2a4df20SDavid Howells 	}
373b2a4df20SDavid Howells 
374b2a4df20SDavid Howells same:
375b2a4df20SDavid Howells 	return -1;
376b2a4df20SDavid Howells 
377b2a4df20SDavid Howells differ_plus_i:
378b2a4df20SDavid Howells 	level += i;
379b2a4df20SDavid Howells differ:
380b2a4df20SDavid Howells 	i = level * 8 + __ffs(seg_a ^ seg_b);
381b2a4df20SDavid Howells 	return i;
382b2a4df20SDavid Howells }
383b2a4df20SDavid Howells 
384b2a4df20SDavid Howells /*
385b2a4df20SDavid Howells  * Free an object after stripping the keyring flag off of the pointer.
386b2a4df20SDavid Howells  */
keyring_free_object(void * object)387b2a4df20SDavid Howells static void keyring_free_object(void *object)
388b2a4df20SDavid Howells {
389b2a4df20SDavid Howells 	key_put(keyring_ptr_to_key(object));
390b2a4df20SDavid Howells }
391b2a4df20SDavid Howells 
392b2a4df20SDavid Howells /*
393b2a4df20SDavid Howells  * Operations for keyring management by the index-tree routines.
394b2a4df20SDavid Howells  */
395b2a4df20SDavid Howells static const struct assoc_array_ops keyring_assoc_array_ops = {
396b2a4df20SDavid Howells 	.get_key_chunk		= keyring_get_key_chunk,
397b2a4df20SDavid Howells 	.get_object_key_chunk	= keyring_get_object_key_chunk,
398b2a4df20SDavid Howells 	.compare_object		= keyring_compare_object,
399b2a4df20SDavid Howells 	.diff_objects		= keyring_diff_objects,
400b2a4df20SDavid Howells 	.free_object		= keyring_free_object,
401b2a4df20SDavid Howells };
402b2a4df20SDavid Howells 
403b2a4df20SDavid Howells /*
404973c9f4fSDavid Howells  * Clean up a keyring when it is destroyed.  Unpublish its name if it had one
405973c9f4fSDavid Howells  * and dispose of its data.
406233e4735SDavid Howells  *
407233e4735SDavid Howells  * The garbage collector detects the final key_put(), removes the keyring from
408233e4735SDavid Howells  * the serial number tree and then does RCU synchronisation before coming here,
409233e4735SDavid Howells  * so we shouldn't need to worry about code poking around here with the RCU
410233e4735SDavid Howells  * readlock held by this time.
4111da177e4SLinus Torvalds  */
keyring_destroy(struct key * keyring)4121da177e4SLinus Torvalds static void keyring_destroy(struct key *keyring)
4131da177e4SLinus Torvalds {
4141da177e4SLinus Torvalds 	if (keyring->description) {
4151da177e4SLinus Torvalds 		write_lock(&keyring_name_lock);
41694efe72fSDavid Howells 
417146aa8b1SDavid Howells 		if (keyring->name_link.next != NULL &&
418146aa8b1SDavid Howells 		    !list_empty(&keyring->name_link))
419146aa8b1SDavid Howells 			list_del(&keyring->name_link);
42094efe72fSDavid Howells 
4211da177e4SLinus Torvalds 		write_unlock(&keyring_name_lock);
4221da177e4SLinus Torvalds 	}
4231da177e4SLinus Torvalds 
4242b6aa412SMat Martineau 	if (keyring->restrict_link) {
4252b6aa412SMat Martineau 		struct key_restriction *keyres = keyring->restrict_link;
4262b6aa412SMat Martineau 
4272b6aa412SMat Martineau 		key_put(keyres->key);
4282b6aa412SMat Martineau 		kfree(keyres);
4292b6aa412SMat Martineau 	}
4302b6aa412SMat Martineau 
431b2a4df20SDavid Howells 	assoc_array_destroy(&keyring->keys, &keyring_assoc_array_ops);
432a8b17ed0SDavid Howells }
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds /*
435973c9f4fSDavid Howells  * Describe a keyring for /proc.
4361da177e4SLinus Torvalds  */
keyring_describe(const struct key * keyring,struct seq_file * m)4371da177e4SLinus Torvalds static void keyring_describe(const struct key *keyring, struct seq_file *m)
4381da177e4SLinus Torvalds {
439c8563473Swzt.wzt@gmail.com 	if (keyring->description)
4401da177e4SLinus Torvalds 		seq_puts(m, keyring->description);
441c8563473Swzt.wzt@gmail.com 	else
4421da177e4SLinus Torvalds 		seq_puts(m, "[anon]");
4431da177e4SLinus Torvalds 
444363b02daSDavid Howells 	if (key_is_positive(keyring)) {
445b2a4df20SDavid Howells 		if (keyring->keys.nr_leaves_on_tree != 0)
446b2a4df20SDavid Howells 			seq_printf(m, ": %lu", keyring->keys.nr_leaves_on_tree);
4471da177e4SLinus Torvalds 		else
4481da177e4SLinus Torvalds 			seq_puts(m, ": empty");
449a8b17ed0SDavid Howells 	}
45078b7280cSDavid Howells }
4511da177e4SLinus Torvalds 
452b2a4df20SDavid Howells struct keyring_read_iterator_context {
453e645016aSEric Biggers 	size_t			buflen;
454b2a4df20SDavid Howells 	size_t			count;
455796e46f9SJann Horn 	key_serial_t		*buffer;
456b2a4df20SDavid Howells };
457b2a4df20SDavid Howells 
keyring_read_iterator(const void * object,void * data)458b2a4df20SDavid Howells static int keyring_read_iterator(const void *object, void *data)
459b2a4df20SDavid Howells {
460b2a4df20SDavid Howells 	struct keyring_read_iterator_context *ctx = data;
461b2a4df20SDavid Howells 	const struct key *key = keyring_ptr_to_key(object);
462b2a4df20SDavid Howells 
463b2a4df20SDavid Howells 	kenter("{%s,%d},,{%zu/%zu}",
464e645016aSEric Biggers 	       key->type->name, key->serial, ctx->count, ctx->buflen);
465b2a4df20SDavid Howells 
466e645016aSEric Biggers 	if (ctx->count >= ctx->buflen)
467b2a4df20SDavid Howells 		return 1;
468b2a4df20SDavid Howells 
469d3ec10aaSWaiman Long 	*ctx->buffer++ = key->serial;
470b2a4df20SDavid Howells 	ctx->count += sizeof(key->serial);
471b2a4df20SDavid Howells 	return 0;
472b2a4df20SDavid Howells }
473b2a4df20SDavid Howells 
4741da177e4SLinus Torvalds /*
475973c9f4fSDavid Howells  * Read a list of key IDs from the keyring's contents in binary form
476973c9f4fSDavid Howells  *
477b2a4df20SDavid Howells  * The keyring's semaphore is read-locked by the caller.  This prevents someone
478b2a4df20SDavid Howells  * from modifying it under us - which could cause us to read key IDs multiple
479b2a4df20SDavid Howells  * times.
4801da177e4SLinus Torvalds  */
keyring_read(const struct key * keyring,char * buffer,size_t buflen)4811da177e4SLinus Torvalds static long keyring_read(const struct key *keyring,
482796e46f9SJann Horn 			 char *buffer, size_t buflen)
4831da177e4SLinus Torvalds {
484b2a4df20SDavid Howells 	struct keyring_read_iterator_context ctx;
4853239b6f2SEric Biggers 	long ret;
4861da177e4SLinus Torvalds 
487b2a4df20SDavid Howells 	kenter("{%d},,%zu", key_serial(keyring), buflen);
4881da177e4SLinus Torvalds 
489b2a4df20SDavid Howells 	if (buflen & (sizeof(key_serial_t) - 1))
490b2a4df20SDavid Howells 		return -EINVAL;
4911da177e4SLinus Torvalds 
4923239b6f2SEric Biggers 	/* Copy as many key IDs as fit into the buffer */
4933239b6f2SEric Biggers 	if (buffer && buflen) {
494796e46f9SJann Horn 		ctx.buffer = (key_serial_t *)buffer;
495e645016aSEric Biggers 		ctx.buflen = buflen;
496b2a4df20SDavid Howells 		ctx.count = 0;
4973239b6f2SEric Biggers 		ret = assoc_array_iterate(&keyring->keys,
4983239b6f2SEric Biggers 					  keyring_read_iterator, &ctx);
499b2a4df20SDavid Howells 		if (ret < 0) {
5003239b6f2SEric Biggers 			kleave(" = %ld [iterate]", ret);
5011da177e4SLinus Torvalds 			return ret;
502a8b17ed0SDavid Howells 		}
5033239b6f2SEric Biggers 	}
5041da177e4SLinus Torvalds 
5053239b6f2SEric Biggers 	/* Return the size of the buffer needed */
5063239b6f2SEric Biggers 	ret = keyring->keys.nr_leaves_on_tree * sizeof(key_serial_t);
5073239b6f2SEric Biggers 	if (ret <= buflen)
5083239b6f2SEric Biggers 		kleave("= %ld [ok]", ret);
5093239b6f2SEric Biggers 	else
5103239b6f2SEric Biggers 		kleave("= %ld [buffer too small]", ret);
5113239b6f2SEric Biggers 	return ret;
512b2a4df20SDavid Howells }
513b2a4df20SDavid Howells 
514028db3e2SLinus Torvalds /*
515028db3e2SLinus Torvalds  * Allocate a keyring and link into the destination keyring.
5161da177e4SLinus Torvalds  */
keyring_alloc(const char * description,kuid_t uid,kgid_t gid,const struct cred * cred,key_perm_t perm,unsigned long flags,struct key_restriction * restrict_link,struct key * dest)5179a56c2dbSEric W. Biederman struct key *keyring_alloc(const char *description, kuid_t uid, kgid_t gid,
518028db3e2SLinus Torvalds 			  const struct cred *cred, key_perm_t perm,
5195ac7eaceSDavid Howells 			  unsigned long flags,
5202b6aa412SMat Martineau 			  struct key_restriction *restrict_link,
5215ac7eaceSDavid Howells 			  struct key *dest)
5221da177e4SLinus Torvalds {
5231da177e4SLinus Torvalds 	struct key *keyring;
5241da177e4SLinus Torvalds 	int ret;
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds 	keyring = key_alloc(&key_type_keyring, description,
527028db3e2SLinus Torvalds 			    uid, gid, cred, perm, flags, restrict_link);
5281da177e4SLinus Torvalds 	if (!IS_ERR(keyring)) {
5293e30148cSDavid Howells 		ret = key_instantiate_and_link(keyring, NULL, 0, dest, NULL);
5301da177e4SLinus Torvalds 		if (ret < 0) {
5311da177e4SLinus Torvalds 			key_put(keyring);
5321da177e4SLinus Torvalds 			keyring = ERR_PTR(ret);
5331da177e4SLinus Torvalds 		}
5341da177e4SLinus Torvalds 	}
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds 	return keyring;
537a8b17ed0SDavid Howells }
538f8aa23a5SDavid Howells EXPORT_SYMBOL(keyring_alloc);
5391da177e4SLinus Torvalds 
5405ac7eaceSDavid Howells /**
5415ac7eaceSDavid Howells  * restrict_link_reject - Give -EPERM to restrict link
5425ac7eaceSDavid Howells  * @keyring: The keyring being added to.
5435ac7eaceSDavid Howells  * @type: The type of key being added.
5445ac7eaceSDavid Howells  * @payload: The payload of the key intended to be added.
5459fd16537SDavid Howells  * @restriction_key: Keys providing additional data for evaluating restriction.
5465ac7eaceSDavid Howells  *
5475ac7eaceSDavid Howells  * Reject the addition of any links to a keyring.  It can be overridden by
5485ac7eaceSDavid Howells  * passing KEY_ALLOC_BYPASS_RESTRICTION to key_instantiate_and_link() when
5495ac7eaceSDavid Howells  * adding a key to a keyring.
5505ac7eaceSDavid Howells  *
5512b6aa412SMat Martineau  * This is meant to be stored in a key_restriction structure which is passed
5522b6aa412SMat Martineau  * in the restrict_link parameter to keyring_alloc().
5535ac7eaceSDavid Howells  */
restrict_link_reject(struct key * keyring,const struct key_type * type,const union key_payload * payload,struct key * restriction_key)5545ac7eaceSDavid Howells int restrict_link_reject(struct key *keyring,
5555ac7eaceSDavid Howells 			 const struct key_type *type,
556aaf66c88SMat Martineau 			 const union key_payload *payload,
557aaf66c88SMat Martineau 			 struct key *restriction_key)
5585ac7eaceSDavid Howells {
5595ac7eaceSDavid Howells 	return -EPERM;
5605ac7eaceSDavid Howells }
5615ac7eaceSDavid Howells 
562b2a4df20SDavid Howells /*
563c06cfb08SDavid Howells  * By default, we keys found by getting an exact match on their descriptions.
564c06cfb08SDavid Howells  */
key_default_cmp(const struct key * key,const struct key_match_data * match_data)5650c903ab6SDavid Howells bool key_default_cmp(const struct key *key,
566c06cfb08SDavid Howells 		     const struct key_match_data *match_data)
567c06cfb08SDavid Howells {
568c06cfb08SDavid Howells 	return strcmp(key->description, match_data->raw_data) == 0;
569c06cfb08SDavid Howells }
570c06cfb08SDavid Howells 
571c06cfb08SDavid Howells /*
572b2a4df20SDavid Howells  * Iteration function to consider each key found.
573b2a4df20SDavid Howells  */
keyring_search_iterator(const void * object,void * iterator_data)574b2a4df20SDavid Howells static int keyring_search_iterator(const void *object, void *iterator_data)
575b2a4df20SDavid Howells {
576b2a4df20SDavid Howells 	struct keyring_search_context *ctx = iterator_data;
577b2a4df20SDavid Howells 	const struct key *key = keyring_ptr_to_key(object);
578363b02daSDavid Howells 	unsigned long kflags = READ_ONCE(key->flags);
579363b02daSDavid Howells 	short state = READ_ONCE(key->state);
580b2a4df20SDavid Howells 
581b2a4df20SDavid Howells 	kenter("{%d}", key->serial);
582b2a4df20SDavid Howells 
583b2a4df20SDavid Howells 	/* ignore keys not of this type */
584b2a4df20SDavid Howells 	if (key->type != ctx->index_key.type) {
585b2a4df20SDavid Howells 		kleave(" = 0 [!type]");
586b2a4df20SDavid Howells 		return 0;
587b2a4df20SDavid Howells 	}
588b2a4df20SDavid Howells 
589b2a4df20SDavid Howells 	/* skip invalidated, revoked and expired keys */
590b2a4df20SDavid Howells 	if (ctx->flags & KEYRING_SEARCH_DO_STATE_CHECK) {
591074d5898SBaolin Wang 		time64_t expiry = READ_ONCE(key->expiry);
5929d6c8711SEric Biggers 
593b2a4df20SDavid Howells 		if (kflags & ((1 << KEY_FLAG_INVALIDATED) |
594b2a4df20SDavid Howells 			      (1 << KEY_FLAG_REVOKED))) {
595b2a4df20SDavid Howells 			ctx->result = ERR_PTR(-EKEYREVOKED);
596b2a4df20SDavid Howells 			kleave(" = %d [invrev]", ctx->skipped_ret);
597b2a4df20SDavid Howells 			goto skipped;
598b2a4df20SDavid Howells 		}
599b2a4df20SDavid Howells 
600074d5898SBaolin Wang 		if (expiry && ctx->now >= expiry) {
6010b0a8415SDavid Howells 			if (!(ctx->flags & KEYRING_SEARCH_SKIP_EXPIRED))
602b2a4df20SDavid Howells 				ctx->result = ERR_PTR(-EKEYEXPIRED);
603b2a4df20SDavid Howells 			kleave(" = %d [expire]", ctx->skipped_ret);
604b2a4df20SDavid Howells 			goto skipped;
605b2a4df20SDavid Howells 		}
606b2a4df20SDavid Howells 	}
607b2a4df20SDavid Howells 
608b2a4df20SDavid Howells 	/* keys that don't match */
60946291959SDavid Howells 	if (!ctx->match_data.cmp(key, &ctx->match_data)) {
610b2a4df20SDavid Howells 		kleave(" = 0 [!match]");
611b2a4df20SDavid Howells 		return 0;
612b2a4df20SDavid Howells 	}
613b2a4df20SDavid Howells 
614b2a4df20SDavid Howells 	/* key must have search permissions */
615b2a4df20SDavid Howells 	if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM) &&
616b2a4df20SDavid Howells 	    key_task_permission(make_key_ref(key, ctx->possessed),
617f5895943SDavid Howells 				ctx->cred, KEY_NEED_SEARCH) < 0) {
618b2a4df20SDavid Howells 		ctx->result = ERR_PTR(-EACCES);
619b2a4df20SDavid Howells 		kleave(" = %d [!perm]", ctx->skipped_ret);
620b2a4df20SDavid Howells 		goto skipped;
621b2a4df20SDavid Howells 	}
622b2a4df20SDavid Howells 
623b2a4df20SDavid Howells 	if (ctx->flags & KEYRING_SEARCH_DO_STATE_CHECK) {
624b2a4df20SDavid Howells 		/* we set a different error code if we pass a negative key */
625363b02daSDavid Howells 		if (state < 0) {
626363b02daSDavid Howells 			ctx->result = ERR_PTR(state);
627b2a4df20SDavid Howells 			kleave(" = %d [neg]", ctx->skipped_ret);
628b2a4df20SDavid Howells 			goto skipped;
629b2a4df20SDavid Howells 		}
630b2a4df20SDavid Howells 	}
631b2a4df20SDavid Howells 
632b2a4df20SDavid Howells 	/* Found */
633b2a4df20SDavid Howells 	ctx->result = make_key_ref(key, ctx->possessed);
634b2a4df20SDavid Howells 	kleave(" = 1 [found]");
635b2a4df20SDavid Howells 	return 1;
636b2a4df20SDavid Howells 
637b2a4df20SDavid Howells skipped:
638b2a4df20SDavid Howells 	return ctx->skipped_ret;
639b2a4df20SDavid Howells }
640b2a4df20SDavid Howells 
641b2a4df20SDavid Howells /*
642b2a4df20SDavid Howells  * Search inside a keyring for a key.  We can search by walking to it
643b2a4df20SDavid Howells  * directly based on its index-key or we can iterate over the entire
644b2a4df20SDavid Howells  * tree looking for it, based on the match function.
645b2a4df20SDavid Howells  */
search_keyring(struct key * keyring,struct keyring_search_context * ctx)646b2a4df20SDavid Howells static int search_keyring(struct key *keyring, struct keyring_search_context *ctx)
647b2a4df20SDavid Howells {
64846291959SDavid Howells 	if (ctx->match_data.lookup_type == KEYRING_SEARCH_LOOKUP_DIRECT) {
649b2a4df20SDavid Howells 		const void *object;
650b2a4df20SDavid Howells 
651b2a4df20SDavid Howells 		object = assoc_array_find(&keyring->keys,
652b2a4df20SDavid Howells 					  &keyring_assoc_array_ops,
653b2a4df20SDavid Howells 					  &ctx->index_key);
654b2a4df20SDavid Howells 		return object ? ctx->iterator(object, ctx) : 0;
655b2a4df20SDavid Howells 	}
656b2a4df20SDavid Howells 	return assoc_array_iterate(&keyring->keys, ctx->iterator, ctx);
657b2a4df20SDavid Howells }
658b2a4df20SDavid Howells 
659b2a4df20SDavid Howells /*
660b2a4df20SDavid Howells  * Search a tree of keyrings that point to other keyrings up to the maximum
661b2a4df20SDavid Howells  * depth.
662b2a4df20SDavid Howells  */
search_nested_keyrings(struct key * keyring,struct keyring_search_context * ctx)663b2a4df20SDavid Howells static bool search_nested_keyrings(struct key *keyring,
664b2a4df20SDavid Howells 				   struct keyring_search_context *ctx)
665b2a4df20SDavid Howells {
666b2a4df20SDavid Howells 	struct {
667b2a4df20SDavid Howells 		struct key *keyring;
668b2a4df20SDavid Howells 		struct assoc_array_node *node;
669b2a4df20SDavid Howells 		int slot;
670b2a4df20SDavid Howells 	} stack[KEYRING_SEARCH_MAX_DEPTH];
671b2a4df20SDavid Howells 
672b2a4df20SDavid Howells 	struct assoc_array_shortcut *shortcut;
673b2a4df20SDavid Howells 	struct assoc_array_node *node;
674b2a4df20SDavid Howells 	struct assoc_array_ptr *ptr;
675b2a4df20SDavid Howells 	struct key *key;
676b2a4df20SDavid Howells 	int sp = 0, slot;
677b2a4df20SDavid Howells 
678b2a4df20SDavid Howells 	kenter("{%d},{%s,%s}",
679b2a4df20SDavid Howells 	       keyring->serial,
680b2a4df20SDavid Howells 	       ctx->index_key.type->name,
681b2a4df20SDavid Howells 	       ctx->index_key.description);
682b2a4df20SDavid Howells 
683054f6180SDavid Howells #define STATE_CHECKS (KEYRING_SEARCH_NO_STATE_CHECK | KEYRING_SEARCH_DO_STATE_CHECK)
684054f6180SDavid Howells 	BUG_ON((ctx->flags & STATE_CHECKS) == 0 ||
685054f6180SDavid Howells 	       (ctx->flags & STATE_CHECKS) == STATE_CHECKS);
686054f6180SDavid Howells 
687f771fde8SDavid Howells 	if (ctx->index_key.description)
688f771fde8SDavid Howells 		key_set_index_key(&ctx->index_key);
689f771fde8SDavid Howells 
690b2a4df20SDavid Howells 	/* Check to see if this top-level keyring is what we are looking for
691b2a4df20SDavid Howells 	 * and whether it is valid or not.
692b2a4df20SDavid Howells 	 */
69346291959SDavid Howells 	if (ctx->match_data.lookup_type == KEYRING_SEARCH_LOOKUP_ITERATE ||
694b2a4df20SDavid Howells 	    keyring_compare_object(keyring, &ctx->index_key)) {
695b2a4df20SDavid Howells 		ctx->skipped_ret = 2;
696b2a4df20SDavid Howells 		switch (ctx->iterator(keyring_key_to_ptr(keyring), ctx)) {
697b2a4df20SDavid Howells 		case 1:
698b2a4df20SDavid Howells 			goto found;
699b2a4df20SDavid Howells 		case 2:
700b2a4df20SDavid Howells 			return false;
701b2a4df20SDavid Howells 		default:
702b2a4df20SDavid Howells 			break;
703b2a4df20SDavid Howells 		}
704b2a4df20SDavid Howells 	}
705b2a4df20SDavid Howells 
706b2a4df20SDavid Howells 	ctx->skipped_ret = 0;
707b2a4df20SDavid Howells 
708b2a4df20SDavid Howells 	/* Start processing a new keyring */
709b2a4df20SDavid Howells descend_to_keyring:
710b2a4df20SDavid Howells 	kdebug("descend to %d", keyring->serial);
711b2a4df20SDavid Howells 	if (keyring->flags & ((1 << KEY_FLAG_INVALIDATED) |
712b2a4df20SDavid Howells 			      (1 << KEY_FLAG_REVOKED)))
713b2a4df20SDavid Howells 		goto not_this_keyring;
714b2a4df20SDavid Howells 
715b2a4df20SDavid Howells 	/* Search through the keys in this keyring before its searching its
716b2a4df20SDavid Howells 	 * subtrees.
717b2a4df20SDavid Howells 	 */
718b2a4df20SDavid Howells 	if (search_keyring(keyring, ctx))
719b2a4df20SDavid Howells 		goto found;
720b2a4df20SDavid Howells 
721b2a4df20SDavid Howells 	/* Then manually iterate through the keyrings nested in this one.
722b2a4df20SDavid Howells 	 *
723b2a4df20SDavid Howells 	 * Start from the root node of the index tree.  Because of the way the
724b2a4df20SDavid Howells 	 * hash function has been set up, keyrings cluster on the leftmost
725b2a4df20SDavid Howells 	 * branch of the root node (root slot 0) or in the root node itself.
726b2a4df20SDavid Howells 	 * Non-keyrings avoid the leftmost branch of the root entirely (root
727b2a4df20SDavid Howells 	 * slots 1-15).
728b2a4df20SDavid Howells 	 */
729dcf49dbcSDavid Howells 	if (!(ctx->flags & KEYRING_SEARCH_RECURSE))
730dcf49dbcSDavid Howells 		goto not_this_keyring;
731dcf49dbcSDavid Howells 
732381f20fcSDavidlohr Bueso 	ptr = READ_ONCE(keyring->keys.root);
733b2a4df20SDavid Howells 	if (!ptr)
734b2a4df20SDavid Howells 		goto not_this_keyring;
735b2a4df20SDavid Howells 
736b2a4df20SDavid Howells 	if (assoc_array_ptr_is_shortcut(ptr)) {
737b2a4df20SDavid Howells 		/* If the root is a shortcut, either the keyring only contains
738b2a4df20SDavid Howells 		 * keyring pointers (everything clusters behind root slot 0) or
739b2a4df20SDavid Howells 		 * doesn't contain any keyring pointers.
740b2a4df20SDavid Howells 		 */
741b2a4df20SDavid Howells 		shortcut = assoc_array_ptr_to_shortcut(ptr);
742b2a4df20SDavid Howells 		if ((shortcut->index_key[0] & ASSOC_ARRAY_FAN_MASK) != 0)
743b2a4df20SDavid Howells 			goto not_this_keyring;
744b2a4df20SDavid Howells 
745381f20fcSDavidlohr Bueso 		ptr = READ_ONCE(shortcut->next_node);
746b2a4df20SDavid Howells 		node = assoc_array_ptr_to_node(ptr);
747b2a4df20SDavid Howells 		goto begin_node;
748b2a4df20SDavid Howells 	}
749b2a4df20SDavid Howells 
750b2a4df20SDavid Howells 	node = assoc_array_ptr_to_node(ptr);
751b2a4df20SDavid Howells 	ptr = node->slots[0];
752b2a4df20SDavid Howells 	if (!assoc_array_ptr_is_meta(ptr))
753b2a4df20SDavid Howells 		goto begin_node;
754b2a4df20SDavid Howells 
755b2a4df20SDavid Howells descend_to_node:
756b2a4df20SDavid Howells 	/* Descend to a more distal node in this keyring's content tree and go
757b2a4df20SDavid Howells 	 * through that.
758b2a4df20SDavid Howells 	 */
759b2a4df20SDavid Howells 	kdebug("descend");
760b2a4df20SDavid Howells 	if (assoc_array_ptr_is_shortcut(ptr)) {
761b2a4df20SDavid Howells 		shortcut = assoc_array_ptr_to_shortcut(ptr);
762381f20fcSDavidlohr Bueso 		ptr = READ_ONCE(shortcut->next_node);
763b2a4df20SDavid Howells 		BUG_ON(!assoc_array_ptr_is_node(ptr));
764b2a4df20SDavid Howells 	}
7659c5e45dfSDavid Howells 	node = assoc_array_ptr_to_node(ptr);
766b2a4df20SDavid Howells 
767b2a4df20SDavid Howells begin_node:
768b2a4df20SDavid Howells 	kdebug("begin_node");
769b2a4df20SDavid Howells 	slot = 0;
770b2a4df20SDavid Howells ascend_to_node:
771b2a4df20SDavid Howells 	/* Go through the slots in a node */
772b2a4df20SDavid Howells 	for (; slot < ASSOC_ARRAY_FAN_OUT; slot++) {
773381f20fcSDavidlohr Bueso 		ptr = READ_ONCE(node->slots[slot]);
774b2a4df20SDavid Howells 
775*3e79ad15SChen Ridong 		if (assoc_array_ptr_is_meta(ptr)) {
776*3e79ad15SChen Ridong 			if (node->back_pointer ||
777*3e79ad15SChen Ridong 			    assoc_array_ptr_is_shortcut(ptr))
778b2a4df20SDavid Howells 				goto descend_to_node;
779*3e79ad15SChen Ridong 		}
780b2a4df20SDavid Howells 
781b2a4df20SDavid Howells 		if (!keyring_ptr_is_keyring(ptr))
782b2a4df20SDavid Howells 			continue;
783b2a4df20SDavid Howells 
784b2a4df20SDavid Howells 		key = keyring_ptr_to_key(ptr);
785b2a4df20SDavid Howells 
786b2a4df20SDavid Howells 		if (sp >= KEYRING_SEARCH_MAX_DEPTH) {
787b2a4df20SDavid Howells 			if (ctx->flags & KEYRING_SEARCH_DETECT_TOO_DEEP) {
788b2a4df20SDavid Howells 				ctx->result = ERR_PTR(-ELOOP);
789b2a4df20SDavid Howells 				return false;
790b2a4df20SDavid Howells 			}
791b2a4df20SDavid Howells 			goto not_this_keyring;
792b2a4df20SDavid Howells 		}
793b2a4df20SDavid Howells 
794b2a4df20SDavid Howells 		/* Search a nested keyring */
795b2a4df20SDavid Howells 		if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM) &&
796b2a4df20SDavid Howells 		    key_task_permission(make_key_ref(key, ctx->possessed),
797f5895943SDavid Howells 					ctx->cred, KEY_NEED_SEARCH) < 0)
798b2a4df20SDavid Howells 			continue;
799b2a4df20SDavid Howells 
800b2a4df20SDavid Howells 		/* stack the current position */
801b2a4df20SDavid Howells 		stack[sp].keyring = keyring;
802b2a4df20SDavid Howells 		stack[sp].node = node;
803b2a4df20SDavid Howells 		stack[sp].slot = slot;
804b2a4df20SDavid Howells 		sp++;
805b2a4df20SDavid Howells 
806b2a4df20SDavid Howells 		/* begin again with the new keyring */
807b2a4df20SDavid Howells 		keyring = key;
808b2a4df20SDavid Howells 		goto descend_to_keyring;
809b2a4df20SDavid Howells 	}
810b2a4df20SDavid Howells 
811b2a4df20SDavid Howells 	/* We've dealt with all the slots in the current node, so now we need
812b2a4df20SDavid Howells 	 * to ascend to the parent and continue processing there.
813b2a4df20SDavid Howells 	 */
814381f20fcSDavidlohr Bueso 	ptr = READ_ONCE(node->back_pointer);
815b2a4df20SDavid Howells 	slot = node->parent_slot;
816b2a4df20SDavid Howells 
817b2a4df20SDavid Howells 	if (ptr && assoc_array_ptr_is_shortcut(ptr)) {
818b2a4df20SDavid Howells 		shortcut = assoc_array_ptr_to_shortcut(ptr);
819381f20fcSDavidlohr Bueso 		ptr = READ_ONCE(shortcut->back_pointer);
820b2a4df20SDavid Howells 		slot = shortcut->parent_slot;
821b2a4df20SDavid Howells 	}
822b2a4df20SDavid Howells 	if (!ptr)
823b2a4df20SDavid Howells 		goto not_this_keyring;
824b2a4df20SDavid Howells 	node = assoc_array_ptr_to_node(ptr);
825b2a4df20SDavid Howells 	slot++;
826b2a4df20SDavid Howells 
827b2a4df20SDavid Howells 	/* If we've ascended to the root (zero backpointer), we must have just
828b2a4df20SDavid Howells 	 * finished processing the leftmost branch rather than the root slots -
829b2a4df20SDavid Howells 	 * so there can't be any more keyrings for us to find.
830b2a4df20SDavid Howells 	 */
831b2a4df20SDavid Howells 	if (node->back_pointer) {
832b2a4df20SDavid Howells 		kdebug("ascend %d", slot);
833b2a4df20SDavid Howells 		goto ascend_to_node;
834b2a4df20SDavid Howells 	}
835b2a4df20SDavid Howells 
836b2a4df20SDavid Howells 	/* The keyring we're looking at was disqualified or didn't contain a
837b2a4df20SDavid Howells 	 * matching key.
838b2a4df20SDavid Howells 	 */
839b2a4df20SDavid Howells not_this_keyring:
840b2a4df20SDavid Howells 	kdebug("not_this_keyring %d", sp);
841b2a4df20SDavid Howells 	if (sp <= 0) {
842b2a4df20SDavid Howells 		kleave(" = false");
843b2a4df20SDavid Howells 		return false;
844b2a4df20SDavid Howells 	}
845b2a4df20SDavid Howells 
846b2a4df20SDavid Howells 	/* Resume the processing of a keyring higher up in the tree */
847b2a4df20SDavid Howells 	sp--;
848b2a4df20SDavid Howells 	keyring = stack[sp].keyring;
849b2a4df20SDavid Howells 	node = stack[sp].node;
850b2a4df20SDavid Howells 	slot = stack[sp].slot + 1;
851b2a4df20SDavid Howells 	kdebug("ascend to %d [%d]", keyring->serial, slot);
852b2a4df20SDavid Howells 	goto ascend_to_node;
853b2a4df20SDavid Howells 
854b2a4df20SDavid Howells 	/* We found a viable match */
855b2a4df20SDavid Howells found:
856b2a4df20SDavid Howells 	key = key_ref_to_ptr(ctx->result);
857b2a4df20SDavid Howells 	key_check(key);
858b2a4df20SDavid Howells 	if (!(ctx->flags & KEYRING_SEARCH_NO_UPDATE_TIME)) {
859074d5898SBaolin Wang 		key->last_used_at = ctx->now;
860074d5898SBaolin Wang 		keyring->last_used_at = ctx->now;
861b2a4df20SDavid Howells 		while (sp > 0)
862074d5898SBaolin Wang 			stack[--sp].keyring->last_used_at = ctx->now;
863b2a4df20SDavid Howells 	}
864b2a4df20SDavid Howells 	kleave(" = true");
865b2a4df20SDavid Howells 	return true;
866b2a4df20SDavid Howells }
867b2a4df20SDavid Howells 
868973c9f4fSDavid Howells /**
869e59428f7SDavid Howells  * keyring_search_rcu - Search a keyring tree for a matching key under RCU
870973c9f4fSDavid Howells  * @keyring_ref: A pointer to the keyring with possession indicator.
8714bdf0bc3SDavid Howells  * @ctx: The keyring search context.
872973c9f4fSDavid Howells  *
873973c9f4fSDavid Howells  * Search the supplied keyring tree for a key that matches the criteria given.
874973c9f4fSDavid Howells  * The root keyring and any linked keyrings must grant Search permission to the
875973c9f4fSDavid Howells  * caller to be searchable and keys can only be found if they too grant Search
876973c9f4fSDavid Howells  * to the caller. The possession flag on the root keyring pointer controls use
877973c9f4fSDavid Howells  * of the possessor bits in permissions checking of the entire tree.  In
878973c9f4fSDavid Howells  * addition, the LSM gets to forbid keyring searches and key matches.
879973c9f4fSDavid Howells  *
880973c9f4fSDavid Howells  * The search is performed as a breadth-then-depth search up to the prescribed
881e59428f7SDavid Howells  * limit (KEYRING_SEARCH_MAX_DEPTH).  The caller must hold the RCU read lock to
882e59428f7SDavid Howells  * prevent keyrings from being destroyed or rearranged whilst they are being
883e59428f7SDavid Howells  * searched.
884973c9f4fSDavid Howells  *
885973c9f4fSDavid Howells  * Keys are matched to the type provided and are then filtered by the match
886973c9f4fSDavid Howells  * function, which is given the description to use in any way it sees fit.  The
887328c95dbSRandy Dunlap  * match function may use any attributes of a key that it wishes to
888973c9f4fSDavid Howells  * determine the match.  Normally the match function from the key type would be
889973c9f4fSDavid Howells  * used.
890973c9f4fSDavid Howells  *
891b2a4df20SDavid Howells  * RCU can be used to prevent the keyring key lists from disappearing without
892b2a4df20SDavid Howells  * the need to take lots of locks.
893973c9f4fSDavid Howells  *
894973c9f4fSDavid Howells  * Returns a pointer to the found key and increments the key usage count if
895973c9f4fSDavid Howells  * successful; -EAGAIN if no matching keys were found, or if expired or revoked
896973c9f4fSDavid Howells  * keys were found; -ENOKEY if only negative keys were found; -ENOTDIR if the
897973c9f4fSDavid Howells  * specified keyring wasn't a keyring.
898973c9f4fSDavid Howells  *
899973c9f4fSDavid Howells  * In the case of a successful return, the possession attribute from
900973c9f4fSDavid Howells  * @keyring_ref is propagated to the returned key reference.
9011da177e4SLinus Torvalds  */
keyring_search_rcu(key_ref_t keyring_ref,struct keyring_search_context * ctx)902e59428f7SDavid Howells key_ref_t keyring_search_rcu(key_ref_t keyring_ref,
9034bdf0bc3SDavid Howells 			     struct keyring_search_context *ctx)
9041da177e4SLinus Torvalds {
90531d5a79dSDavid Howells 	struct key *keyring;
9061da177e4SLinus Torvalds 	long err;
907b2a4df20SDavid Howells 
908b2a4df20SDavid Howells 	ctx->iterator = keyring_search_iterator;
909b2a4df20SDavid Howells 	ctx->possessed = is_key_possessed(keyring_ref);
910b2a4df20SDavid Howells 	ctx->result = ERR_PTR(-EAGAIN);
9111da177e4SLinus Torvalds 
912664cceb0SDavid Howells 	keyring = key_ref_to_ptr(keyring_ref);
9131da177e4SLinus Torvalds 	key_check(keyring);
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds 	if (keyring->type != &key_type_keyring)
916b2a4df20SDavid Howells 		return ERR_PTR(-ENOTDIR);
917b2a4df20SDavid Howells 
918b2a4df20SDavid Howells 	if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM)) {
919f5895943SDavid Howells 		err = key_task_permission(keyring_ref, ctx->cred, KEY_NEED_SEARCH);
920b2a4df20SDavid Howells 		if (err < 0)
921b2a4df20SDavid Howells 			return ERR_PTR(err);
922b2a4df20SDavid Howells 	}
9231da177e4SLinus Torvalds 
924074d5898SBaolin Wang 	ctx->now = ktime_get_real_seconds();
925b2a4df20SDavid Howells 	if (search_nested_keyrings(keyring, ctx))
926b2a4df20SDavid Howells 		__key_get(key_ref_to_ptr(ctx->result));
927b2a4df20SDavid Howells 	return ctx->result;
928a8b17ed0SDavid Howells }
9291da177e4SLinus Torvalds 
930973c9f4fSDavid Howells /**
931973c9f4fSDavid Howells  * keyring_search - Search the supplied keyring tree for a matching key
932973c9f4fSDavid Howells  * @keyring: The root of the keyring tree to be searched.
933973c9f4fSDavid Howells  * @type: The type of keyring we want to find.
934973c9f4fSDavid Howells  * @description: The name of the keyring we want to find.
935dcf49dbcSDavid Howells  * @recurse: True to search the children of @keyring also
936973c9f4fSDavid Howells  *
937e59428f7SDavid Howells  * As keyring_search_rcu() above, but using the current task's credentials and
938b2a4df20SDavid Howells  * type's default matching function and preferred search method.
9391da177e4SLinus Torvalds  */
keyring_search(key_ref_t keyring,struct key_type * type,const char * description,bool recurse)940664cceb0SDavid Howells key_ref_t keyring_search(key_ref_t keyring,
9411da177e4SLinus Torvalds 			 struct key_type *type,
942dcf49dbcSDavid Howells 			 const char *description,
943dcf49dbcSDavid Howells 			 bool recurse)
9441da177e4SLinus Torvalds {
9454bdf0bc3SDavid Howells 	struct keyring_search_context ctx = {
9464bdf0bc3SDavid Howells 		.index_key.type		= type,
9474bdf0bc3SDavid Howells 		.index_key.description	= description,
948ede0fa98SEric Biggers 		.index_key.desc_len	= strlen(description),
9494bdf0bc3SDavid Howells 		.cred			= current_cred(),
950c06cfb08SDavid Howells 		.match_data.cmp		= key_default_cmp,
95146291959SDavid Howells 		.match_data.raw_data	= description,
95246291959SDavid Howells 		.match_data.lookup_type	= KEYRING_SEARCH_LOOKUP_DIRECT,
95346291959SDavid Howells 		.flags			= KEYRING_SEARCH_DO_STATE_CHECK,
9544bdf0bc3SDavid Howells 	};
95546291959SDavid Howells 	key_ref_t key;
95646291959SDavid Howells 	int ret;
9574bdf0bc3SDavid Howells 
958dcf49dbcSDavid Howells 	if (recurse)
959dcf49dbcSDavid Howells 		ctx.flags |= KEYRING_SEARCH_RECURSE;
96046291959SDavid Howells 	if (type->match_preparse) {
96146291959SDavid Howells 		ret = type->match_preparse(&ctx.match_data);
96246291959SDavid Howells 		if (ret < 0)
96346291959SDavid Howells 			return ERR_PTR(ret);
96446291959SDavid Howells 	}
96546291959SDavid Howells 
966e59428f7SDavid Howells 	rcu_read_lock();
967e59428f7SDavid Howells 	key = keyring_search_rcu(keyring, &ctx);
968e59428f7SDavid Howells 	rcu_read_unlock();
96946291959SDavid Howells 
97046291959SDavid Howells 	if (type->match_free)
97146291959SDavid Howells 		type->match_free(&ctx.match_data);
97246291959SDavid Howells 	return key;
973a8b17ed0SDavid Howells }
9741da177e4SLinus Torvalds EXPORT_SYMBOL(keyring_search);
9751da177e4SLinus Torvalds 
keyring_restriction_alloc(key_restrict_link_func_t check)9766563c91fSMat Martineau static struct key_restriction *keyring_restriction_alloc(
9776563c91fSMat Martineau 	key_restrict_link_func_t check)
9786563c91fSMat Martineau {
9796563c91fSMat Martineau 	struct key_restriction *keyres =
9806563c91fSMat Martineau 		kzalloc(sizeof(struct key_restriction), GFP_KERNEL);
9816563c91fSMat Martineau 
9826563c91fSMat Martineau 	if (!keyres)
9836563c91fSMat Martineau 		return ERR_PTR(-ENOMEM);
9846563c91fSMat Martineau 
9856563c91fSMat Martineau 	keyres->check = check;
9866563c91fSMat Martineau 
9876563c91fSMat Martineau 	return keyres;
9886563c91fSMat Martineau }
9896563c91fSMat Martineau 
9906563c91fSMat Martineau /*
9916563c91fSMat Martineau  * Semaphore to serialise restriction setup to prevent reference count
9926563c91fSMat Martineau  * cycles through restriction key pointers.
9936563c91fSMat Martineau  */
9946563c91fSMat Martineau static DECLARE_RWSEM(keyring_serialise_restrict_sem);
9956563c91fSMat Martineau 
9966563c91fSMat Martineau /*
9976563c91fSMat Martineau  * Check for restriction cycles that would prevent keyring garbage collection.
9986563c91fSMat Martineau  * keyring_serialise_restrict_sem must be held.
9996563c91fSMat Martineau  */
keyring_detect_restriction_cycle(const struct key * dest_keyring,struct key_restriction * keyres)10006563c91fSMat Martineau static bool keyring_detect_restriction_cycle(const struct key *dest_keyring,
10016563c91fSMat Martineau 					     struct key_restriction *keyres)
10026563c91fSMat Martineau {
10036563c91fSMat Martineau 	while (keyres && keyres->key &&
10046563c91fSMat Martineau 	       keyres->key->type == &key_type_keyring) {
10056563c91fSMat Martineau 		if (keyres->key == dest_keyring)
10066563c91fSMat Martineau 			return true;
10076563c91fSMat Martineau 
10086563c91fSMat Martineau 		keyres = keyres->key->restrict_link;
10096563c91fSMat Martineau 	}
10106563c91fSMat Martineau 
10116563c91fSMat Martineau 	return false;
10126563c91fSMat Martineau }
10136563c91fSMat Martineau 
10146563c91fSMat Martineau /**
10156563c91fSMat Martineau  * keyring_restrict - Look up and apply a restriction to a keyring
10169fd16537SDavid Howells  * @keyring_ref: The keyring to be restricted
10179fd16537SDavid Howells  * @type: The key type that will provide the restriction checker.
10186563c91fSMat Martineau  * @restriction: The restriction options to apply to the keyring
10199fd16537SDavid Howells  *
10209fd16537SDavid Howells  * Look up a keyring and apply a restriction to it.  The restriction is managed
10219fd16537SDavid Howells  * by the specific key type, but can be configured by the options specified in
10229fd16537SDavid Howells  * the restriction string.
10236563c91fSMat Martineau  */
keyring_restrict(key_ref_t keyring_ref,const char * type,const char * restriction)10246563c91fSMat Martineau int keyring_restrict(key_ref_t keyring_ref, const char *type,
10256563c91fSMat Martineau 		     const char *restriction)
10266563c91fSMat Martineau {
10276563c91fSMat Martineau 	struct key *keyring;
10286563c91fSMat Martineau 	struct key_type *restrict_type = NULL;
10296563c91fSMat Martineau 	struct key_restriction *restrict_link;
10306563c91fSMat Martineau 	int ret = 0;
10316563c91fSMat Martineau 
10326563c91fSMat Martineau 	keyring = key_ref_to_ptr(keyring_ref);
10336563c91fSMat Martineau 	key_check(keyring);
10346563c91fSMat Martineau 
10356563c91fSMat Martineau 	if (keyring->type != &key_type_keyring)
10366563c91fSMat Martineau 		return -ENOTDIR;
10376563c91fSMat Martineau 
10386563c91fSMat Martineau 	if (!type) {
10396563c91fSMat Martineau 		restrict_link = keyring_restriction_alloc(restrict_link_reject);
10406563c91fSMat Martineau 	} else {
10416563c91fSMat Martineau 		restrict_type = key_type_lookup(type);
10426563c91fSMat Martineau 
10436563c91fSMat Martineau 		if (IS_ERR(restrict_type))
10446563c91fSMat Martineau 			return PTR_ERR(restrict_type);
10456563c91fSMat Martineau 
10466563c91fSMat Martineau 		if (!restrict_type->lookup_restriction) {
10476563c91fSMat Martineau 			ret = -ENOENT;
10486563c91fSMat Martineau 			goto error;
10496563c91fSMat Martineau 		}
10506563c91fSMat Martineau 
10516563c91fSMat Martineau 		restrict_link = restrict_type->lookup_restriction(restriction);
10526563c91fSMat Martineau 	}
10536563c91fSMat Martineau 
10546563c91fSMat Martineau 	if (IS_ERR(restrict_link)) {
10556563c91fSMat Martineau 		ret = PTR_ERR(restrict_link);
10566563c91fSMat Martineau 		goto error;
10576563c91fSMat Martineau 	}
10586563c91fSMat Martineau 
10596563c91fSMat Martineau 	down_write(&keyring->sem);
10606563c91fSMat Martineau 	down_write(&keyring_serialise_restrict_sem);
10616563c91fSMat Martineau 
1062f7e47677SDavid Howells 	if (keyring->restrict_link) {
10636563c91fSMat Martineau 		ret = -EEXIST;
1064f7e47677SDavid Howells 	} else if (keyring_detect_restriction_cycle(keyring, restrict_link)) {
10656563c91fSMat Martineau 		ret = -EDEADLK;
1066f7e47677SDavid Howells 	} else {
10676563c91fSMat Martineau 		keyring->restrict_link = restrict_link;
1068f7e47677SDavid Howells 		notify_key(keyring, NOTIFY_KEY_SETATTR, 0);
1069f7e47677SDavid Howells 	}
10706563c91fSMat Martineau 
10716563c91fSMat Martineau 	up_write(&keyring_serialise_restrict_sem);
10726563c91fSMat Martineau 	up_write(&keyring->sem);
10736563c91fSMat Martineau 
10746563c91fSMat Martineau 	if (ret < 0) {
10756563c91fSMat Martineau 		key_put(restrict_link->key);
10766563c91fSMat Martineau 		kfree(restrict_link);
10776563c91fSMat Martineau 	}
10786563c91fSMat Martineau 
10796563c91fSMat Martineau error:
10806563c91fSMat Martineau 	if (restrict_type)
10816563c91fSMat Martineau 		key_type_put(restrict_type);
10826563c91fSMat Martineau 
10836563c91fSMat Martineau 	return ret;
10846563c91fSMat Martineau }
10856563c91fSMat Martineau EXPORT_SYMBOL(keyring_restrict);
10866563c91fSMat Martineau 
10871da177e4SLinus Torvalds /*
1088b2a4df20SDavid Howells  * Search the given keyring for a key that might be updated.
1089973c9f4fSDavid Howells  *
1090973c9f4fSDavid Howells  * The caller must guarantee that the keyring is a keyring and that the
1091b2a4df20SDavid Howells  * permission is granted to modify the keyring as no check is made here.  The
1092b2a4df20SDavid Howells  * caller must also hold a lock on the keyring semaphore.
1093973c9f4fSDavid Howells  *
1094973c9f4fSDavid Howells  * Returns a pointer to the found key with usage count incremented if
1095b2a4df20SDavid Howells  * successful and returns NULL if not found.  Revoked and invalidated keys are
1096b2a4df20SDavid Howells  * skipped over.
1097973c9f4fSDavid Howells  *
1098973c9f4fSDavid Howells  * If successful, the possession indicator is propagated from the keyring ref
1099973c9f4fSDavid Howells  * to the returned key reference.
11001da177e4SLinus Torvalds  */
find_key_to_update(key_ref_t keyring_ref,const struct keyring_index_key * index_key)1101b2a4df20SDavid Howells key_ref_t find_key_to_update(key_ref_t keyring_ref,
1102e57e8669SDavid Howells 			     const struct keyring_index_key *index_key)
11031da177e4SLinus Torvalds {
1104664cceb0SDavid Howells 	struct key *keyring, *key;
1105b2a4df20SDavid Howells 	const void *object;
11061da177e4SLinus Torvalds 
1107664cceb0SDavid Howells 	keyring = key_ref_to_ptr(keyring_ref);
1108664cceb0SDavid Howells 
1109b2a4df20SDavid Howells 	kenter("{%d},{%s,%s}",
1110b2a4df20SDavid Howells 	       keyring->serial, index_key->type->name, index_key->description);
111176d8aeabSDavid Howells 
1112b2a4df20SDavid Howells 	object = assoc_array_find(&keyring->keys, &keyring_assoc_array_ops,
1113b2a4df20SDavid Howells 				  index_key);
1114b2a4df20SDavid Howells 
1115b2a4df20SDavid Howells 	if (object)
11161da177e4SLinus Torvalds 		goto found;
11171da177e4SLinus Torvalds 
1118b2a4df20SDavid Howells 	kleave(" = NULL");
1119b2a4df20SDavid Howells 	return NULL;
11201da177e4SLinus Torvalds 
11211da177e4SLinus Torvalds found:
1122b2a4df20SDavid Howells 	key = keyring_ptr_to_key(object);
1123b2a4df20SDavid Howells 	if (key->flags & ((1 << KEY_FLAG_INVALIDATED) |
1124b2a4df20SDavid Howells 			  (1 << KEY_FLAG_REVOKED))) {
1125b2a4df20SDavid Howells 		kleave(" = NULL [x]");
1126b2a4df20SDavid Howells 		return NULL;
1127b2a4df20SDavid Howells 	}
1128ccc3e6d9SDavid Howells 	__key_get(key);
1129b2a4df20SDavid Howells 	kleave(" = {%d}", key->serial);
1130b2a4df20SDavid Howells 	return make_key_ref(key, is_key_possessed(keyring_ref));
1131a8b17ed0SDavid Howells }
11321da177e4SLinus Torvalds 
11331da177e4SLinus Torvalds /*
1134973c9f4fSDavid Howells  * Find a keyring with the specified name.
1135973c9f4fSDavid Howells  *
1136028db3e2SLinus Torvalds  * Only keyrings that have nonzero refcount, are not revoked, and are owned by a
1137028db3e2SLinus Torvalds  * user in the current user namespace are considered.  If @uid_keyring is %true,
1138028db3e2SLinus Torvalds  * the keyring additionally must have been allocated as a user or user session
1139028db3e2SLinus Torvalds  * keyring; otherwise, it must grant Search permission directly to the caller.
1140973c9f4fSDavid Howells  *
1141973c9f4fSDavid Howells  * Returns a pointer to the keyring with the keyring's refcount having being
1142973c9f4fSDavid Howells  * incremented on success.  -ENOKEY is returned if a key could not be found.
11431da177e4SLinus Torvalds  */
find_keyring_by_name(const char * name,bool uid_keyring)1144237bbd29SEric Biggers struct key *find_keyring_by_name(const char *name, bool uid_keyring)
11451da177e4SLinus Torvalds {
1146b206f281SDavid Howells 	struct user_namespace *ns = current_user_ns();
11471da177e4SLinus Torvalds 	struct key *keyring;
11481da177e4SLinus Torvalds 
11491da177e4SLinus Torvalds 	if (!name)
1150cea7daa3SToshiyuki Okajima 		return ERR_PTR(-EINVAL);
11511da177e4SLinus Torvalds 
11521da177e4SLinus Torvalds 	read_lock(&keyring_name_lock);
11531da177e4SLinus Torvalds 
1154b206f281SDavid Howells 	/* Search this hash bucket for a keyring with a matching name that
1155b206f281SDavid Howells 	 * grants Search permission and that hasn't been revoked
1156b206f281SDavid Howells 	 */
1157b206f281SDavid Howells 	list_for_each_entry(keyring, &ns->keyring_name_list, name_link) {
1158b206f281SDavid Howells 		if (!kuid_has_mapping(ns, keyring->user->uid))
11592ea190d0SSerge E. Hallyn 			continue;
11602ea190d0SSerge E. Hallyn 
116176d8aeabSDavid Howells 		if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
11621da177e4SLinus Torvalds 			continue;
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds 		if (strcmp(keyring->description, name) != 0)
11651da177e4SLinus Torvalds 			continue;
11661da177e4SLinus Torvalds 
1167237bbd29SEric Biggers 		if (uid_keyring) {
1168237bbd29SEric Biggers 			if (!test_bit(KEY_FLAG_UID_KEYRING,
1169237bbd29SEric Biggers 				      &keyring->flags))
1170237bbd29SEric Biggers 				continue;
1171237bbd29SEric Biggers 		} else {
1172237bbd29SEric Biggers 			if (key_permission(make_key_ref(keyring, 0),
1173028db3e2SLinus Torvalds 					   KEY_NEED_SEARCH) < 0)
11741da177e4SLinus Torvalds 				continue;
1175237bbd29SEric Biggers 		}
11761da177e4SLinus Torvalds 
1177cea7daa3SToshiyuki Okajima 		/* we've got a match but we might end up racing with
1178cea7daa3SToshiyuki Okajima 		 * key_cleanup() if the keyring is currently 'dead'
1179cea7daa3SToshiyuki Okajima 		 * (ie. it has a zero usage count) */
1180fff29291SElena Reshetova 		if (!refcount_inc_not_zero(&keyring->usage))
1181cea7daa3SToshiyuki Okajima 			continue;
1182074d5898SBaolin Wang 		keyring->last_used_at = ktime_get_real_seconds();
1183cea7daa3SToshiyuki Okajima 		goto out;
11841da177e4SLinus Torvalds 	}
11851da177e4SLinus Torvalds 
11861da177e4SLinus Torvalds 	keyring = ERR_PTR(-ENOKEY);
1187cea7daa3SToshiyuki Okajima out:
1188cea7daa3SToshiyuki Okajima 	read_unlock(&keyring_name_lock);
11891da177e4SLinus Torvalds 	return keyring;
1190a8b17ed0SDavid Howells }
11911da177e4SLinus Torvalds 
keyring_detect_cycle_iterator(const void * object,void * iterator_data)1192b2a4df20SDavid Howells static int keyring_detect_cycle_iterator(const void *object,
1193b2a4df20SDavid Howells 					 void *iterator_data)
1194b2a4df20SDavid Howells {
1195b2a4df20SDavid Howells 	struct keyring_search_context *ctx = iterator_data;
1196b2a4df20SDavid Howells 	const struct key *key = keyring_ptr_to_key(object);
1197b2a4df20SDavid Howells 
1198b2a4df20SDavid Howells 	kenter("{%d}", key->serial);
1199b2a4df20SDavid Howells 
1200979e0d74SDavid Howells 	/* We might get a keyring with matching index-key that is nonetheless a
1201979e0d74SDavid Howells 	 * different keyring. */
120246291959SDavid Howells 	if (key != ctx->match_data.raw_data)
1203979e0d74SDavid Howells 		return 0;
1204979e0d74SDavid Howells 
1205b2a4df20SDavid Howells 	ctx->result = ERR_PTR(-EDEADLK);
1206b2a4df20SDavid Howells 	return 1;
1207b2a4df20SDavid Howells }
1208b2a4df20SDavid Howells 
12091da177e4SLinus Torvalds /*
1210328c95dbSRandy Dunlap  * See if a cycle will be created by inserting acyclic tree B in acyclic
1211973c9f4fSDavid Howells  * tree A at the topmost level (ie: as a direct child of A).
1212973c9f4fSDavid Howells  *
1213973c9f4fSDavid Howells  * Since we are adding B to A at the top level, checking for cycles should just
1214973c9f4fSDavid Howells  * be a matter of seeing if node A is somewhere in tree B.
12151da177e4SLinus Torvalds  */
keyring_detect_cycle(struct key * A,struct key * B)12161da177e4SLinus Torvalds static int keyring_detect_cycle(struct key *A, struct key *B)
12171da177e4SLinus Torvalds {
1218b2a4df20SDavid Howells 	struct keyring_search_context ctx = {
1219b2a4df20SDavid Howells 		.index_key		= A->index_key,
122046291959SDavid Howells 		.match_data.raw_data	= A,
122146291959SDavid Howells 		.match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
1222b2a4df20SDavid Howells 		.iterator		= keyring_detect_cycle_iterator,
122346291959SDavid Howells 		.flags			= (KEYRING_SEARCH_NO_STATE_CHECK |
1224b2a4df20SDavid Howells 					   KEYRING_SEARCH_NO_UPDATE_TIME |
1225b2a4df20SDavid Howells 					   KEYRING_SEARCH_NO_CHECK_PERM |
1226dcf49dbcSDavid Howells 					   KEYRING_SEARCH_DETECT_TOO_DEEP |
1227dcf49dbcSDavid Howells 					   KEYRING_SEARCH_RECURSE),
1228b2a4df20SDavid Howells 	};
12291da177e4SLinus Torvalds 
123076d8aeabSDavid Howells 	rcu_read_lock();
1231b2a4df20SDavid Howells 	search_nested_keyrings(B, &ctx);
123276d8aeabSDavid Howells 	rcu_read_unlock();
1233b2a4df20SDavid Howells 	return PTR_ERR(ctx.result) == -EAGAIN ? 0 : PTR_ERR(ctx.result);
1234f70e2e06SDavid Howells }
1235cab8eb59SDavid Howells 
1236cab8eb59SDavid Howells /*
1237df593ee2SDavid Howells  * Lock keyring for link.
1238df593ee2SDavid Howells  */
__key_link_lock(struct key * keyring,const struct keyring_index_key * index_key)1239df593ee2SDavid Howells int __key_link_lock(struct key *keyring,
1240df593ee2SDavid Howells 		    const struct keyring_index_key *index_key)
1241df593ee2SDavid Howells 	__acquires(&keyring->sem)
1242df593ee2SDavid Howells 	__acquires(&keyring_serialise_link_lock)
1243df593ee2SDavid Howells {
1244df593ee2SDavid Howells 	if (keyring->type != &key_type_keyring)
1245df593ee2SDavid Howells 		return -ENOTDIR;
1246df593ee2SDavid Howells 
1247df593ee2SDavid Howells 	down_write(&keyring->sem);
1248df593ee2SDavid Howells 
1249df593ee2SDavid Howells 	/* Serialise link/link calls to prevent parallel calls causing a cycle
1250df593ee2SDavid Howells 	 * when linking two keyring in opposite orders.
1251df593ee2SDavid Howells 	 */
1252df593ee2SDavid Howells 	if (index_key->type == &key_type_keyring)
1253df593ee2SDavid Howells 		mutex_lock(&keyring_serialise_link_lock);
1254df593ee2SDavid Howells 
1255df593ee2SDavid Howells 	return 0;
1256df593ee2SDavid Howells }
1257df593ee2SDavid Howells 
1258df593ee2SDavid Howells /*
1259ed0ac5c7SDavid Howells  * Lock keyrings for move (link/unlink combination).
1260ed0ac5c7SDavid Howells  */
__key_move_lock(struct key * l_keyring,struct key * u_keyring,const struct keyring_index_key * index_key)1261ed0ac5c7SDavid Howells int __key_move_lock(struct key *l_keyring, struct key *u_keyring,
1262ed0ac5c7SDavid Howells 		    const struct keyring_index_key *index_key)
1263ed0ac5c7SDavid Howells 	__acquires(&l_keyring->sem)
1264ed0ac5c7SDavid Howells 	__acquires(&u_keyring->sem)
1265ed0ac5c7SDavid Howells 	__acquires(&keyring_serialise_link_lock)
1266ed0ac5c7SDavid Howells {
1267ed0ac5c7SDavid Howells 	if (l_keyring->type != &key_type_keyring ||
1268ed0ac5c7SDavid Howells 	    u_keyring->type != &key_type_keyring)
1269ed0ac5c7SDavid Howells 		return -ENOTDIR;
1270ed0ac5c7SDavid Howells 
1271ed0ac5c7SDavid Howells 	/* We have to be very careful here to take the keyring locks in the
1272ed0ac5c7SDavid Howells 	 * right order, lest we open ourselves to deadlocking against another
1273ed0ac5c7SDavid Howells 	 * move operation.
1274ed0ac5c7SDavid Howells 	 */
1275ed0ac5c7SDavid Howells 	if (l_keyring < u_keyring) {
1276ed0ac5c7SDavid Howells 		down_write(&l_keyring->sem);
1277ed0ac5c7SDavid Howells 		down_write_nested(&u_keyring->sem, 1);
1278ed0ac5c7SDavid Howells 	} else {
1279ed0ac5c7SDavid Howells 		down_write(&u_keyring->sem);
1280ed0ac5c7SDavid Howells 		down_write_nested(&l_keyring->sem, 1);
1281ed0ac5c7SDavid Howells 	}
1282ed0ac5c7SDavid Howells 
1283ed0ac5c7SDavid Howells 	/* Serialise link/link calls to prevent parallel calls causing a cycle
1284ed0ac5c7SDavid Howells 	 * when linking two keyring in opposite orders.
1285ed0ac5c7SDavid Howells 	 */
1286ed0ac5c7SDavid Howells 	if (index_key->type == &key_type_keyring)
1287ed0ac5c7SDavid Howells 		mutex_lock(&keyring_serialise_link_lock);
1288ed0ac5c7SDavid Howells 
1289ed0ac5c7SDavid Howells 	return 0;
1290ed0ac5c7SDavid Howells }
1291ed0ac5c7SDavid Howells 
1292ed0ac5c7SDavid Howells /*
1293973c9f4fSDavid Howells  * Preallocate memory so that a key can be linked into to a keyring.
12941da177e4SLinus Torvalds  */
__key_link_begin(struct key * keyring,const struct keyring_index_key * index_key,struct assoc_array_edit ** _edit)1295b2a4df20SDavid Howells int __key_link_begin(struct key *keyring,
1296b2a4df20SDavid Howells 		     const struct keyring_index_key *index_key,
1297b2a4df20SDavid Howells 		     struct assoc_array_edit **_edit)
12981da177e4SLinus Torvalds {
1299b2a4df20SDavid Howells 	struct assoc_array_edit *edit;
1300b2a4df20SDavid Howells 	int ret;
13011da177e4SLinus Torvalds 
130216feef43SDavid Howells 	kenter("%d,%s,%s,",
1303b2a4df20SDavid Howells 	       keyring->serial, index_key->type->name, index_key->description);
1304b2a4df20SDavid Howells 
1305b2a4df20SDavid Howells 	BUG_ON(index_key->desc_len == 0);
1306df593ee2SDavid Howells 	BUG_ON(*_edit != NULL);
1307f70e2e06SDavid Howells 
1308df593ee2SDavid Howells 	*_edit = NULL;
1309f70e2e06SDavid Howells 
13101da177e4SLinus Torvalds 	ret = -EKEYREVOKED;
131176d8aeabSDavid Howells 	if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
1312df593ee2SDavid Howells 		goto error;
13131da177e4SLinus Torvalds 
1314b2a4df20SDavid Howells 	/* Create an edit script that will insert/replace the key in the
1315b2a4df20SDavid Howells 	 * keyring tree.
1316b2a4df20SDavid Howells 	 */
1317b2a4df20SDavid Howells 	edit = assoc_array_insert(&keyring->keys,
1318b2a4df20SDavid Howells 				  &keyring_assoc_array_ops,
1319b2a4df20SDavid Howells 				  index_key,
1320b2a4df20SDavid Howells 				  NULL);
1321b2a4df20SDavid Howells 	if (IS_ERR(edit)) {
1322b2a4df20SDavid Howells 		ret = PTR_ERR(edit);
1323df593ee2SDavid Howells 		goto error;
1324034faeb9SDavid Howells 	}
1325034faeb9SDavid Howells 
1326034faeb9SDavid Howells 	/* If we're not replacing a link in-place then we're going to need some
1327034faeb9SDavid Howells 	 * extra quota.
1328034faeb9SDavid Howells 	 */
1329034faeb9SDavid Howells 	if (!edit->dead_leaf) {
1330034faeb9SDavid Howells 		ret = key_payload_reserve(keyring,
1331034faeb9SDavid Howells 					  keyring->datalen + KEYQUOTA_LINK_BYTES);
1332034faeb9SDavid Howells 		if (ret < 0)
1333034faeb9SDavid Howells 			goto error_cancel;
13341da177e4SLinus Torvalds 	}
13351da177e4SLinus Torvalds 
1336b2a4df20SDavid Howells 	*_edit = edit;
1337f70e2e06SDavid Howells 	kleave(" = 0");
1338f70e2e06SDavid Howells 	return 0;
13391da177e4SLinus Torvalds 
1340034faeb9SDavid Howells error_cancel:
1341034faeb9SDavid Howells 	assoc_array_cancel_edit(edit);
1342df593ee2SDavid Howells error:
1343f70e2e06SDavid Howells 	kleave(" = %d", ret);
1344f70e2e06SDavid Howells 	return ret;
1345f70e2e06SDavid Howells }
13461da177e4SLinus Torvalds 
1347f70e2e06SDavid Howells /*
1348973c9f4fSDavid Howells  * Check already instantiated keys aren't going to be a problem.
1349973c9f4fSDavid Howells  *
1350973c9f4fSDavid Howells  * The caller must have called __key_link_begin(). Don't need to call this for
1351973c9f4fSDavid Howells  * keys that were created since __key_link_begin() was called.
1352f70e2e06SDavid Howells  */
__key_link_check_live_key(struct key * keyring,struct key * key)1353f70e2e06SDavid Howells int __key_link_check_live_key(struct key *keyring, struct key *key)
1354f70e2e06SDavid Howells {
1355f70e2e06SDavid Howells 	if (key->type == &key_type_keyring)
1356f70e2e06SDavid Howells 		/* check that we aren't going to create a cycle by linking one
1357f70e2e06SDavid Howells 		 * keyring to another */
1358f70e2e06SDavid Howells 		return keyring_detect_cycle(keyring, key);
1359f70e2e06SDavid Howells 	return 0;
1360f70e2e06SDavid Howells }
13611da177e4SLinus Torvalds 
1362f70e2e06SDavid Howells /*
1363973c9f4fSDavid Howells  * Link a key into to a keyring.
1364973c9f4fSDavid Howells  *
1365973c9f4fSDavid Howells  * Must be called with __key_link_begin() having being called.  Discards any
1366973c9f4fSDavid Howells  * already extant link to matching key if there is one, so that each keyring
1367973c9f4fSDavid Howells  * holds at most one link to any given key of a particular type+description
1368973c9f4fSDavid Howells  * combination.
1369f70e2e06SDavid Howells  */
__key_link(struct key * keyring,struct key * key,struct assoc_array_edit ** _edit)1370f7e47677SDavid Howells void __key_link(struct key *keyring, struct key *key,
1371f7e47677SDavid Howells 		struct assoc_array_edit **_edit)
1372f70e2e06SDavid Howells {
1373ccc3e6d9SDavid Howells 	__key_get(key);
1374b2a4df20SDavid Howells 	assoc_array_insert_set_object(*_edit, keyring_key_to_ptr(key));
1375b2a4df20SDavid Howells 	assoc_array_apply_edit(*_edit);
1376b2a4df20SDavid Howells 	*_edit = NULL;
1377f7e47677SDavid Howells 	notify_key(keyring, NOTIFY_KEY_LINKED, key_serial(key));
1378f70e2e06SDavid Howells }
1379f70e2e06SDavid Howells 
1380f70e2e06SDavid Howells /*
1381973c9f4fSDavid Howells  * Finish linking a key into to a keyring.
1382973c9f4fSDavid Howells  *
1383973c9f4fSDavid Howells  * Must be called with __key_link_begin() having being called.
1384f70e2e06SDavid Howells  */
__key_link_end(struct key * keyring,const struct keyring_index_key * index_key,struct assoc_array_edit * edit)138516feef43SDavid Howells void __key_link_end(struct key *keyring,
138616feef43SDavid Howells 		    const struct keyring_index_key *index_key,
1387b2a4df20SDavid Howells 		    struct assoc_array_edit *edit)
1388f70e2e06SDavid Howells 	__releases(&keyring->sem)
13893be59f74SDavid Howells 	__releases(&keyring_serialise_link_lock)
1390f70e2e06SDavid Howells {
139116feef43SDavid Howells 	BUG_ON(index_key->type == NULL);
1392b2a4df20SDavid Howells 	kenter("%d,%s,", keyring->serial, index_key->type->name);
1393f70e2e06SDavid Howells 
1394ca4da5ddSColin Ian King 	if (edit) {
1395ca4da5ddSColin Ian King 		if (!edit->dead_leaf) {
1396f70e2e06SDavid Howells 			key_payload_reserve(keyring,
1397b2a4df20SDavid Howells 				keyring->datalen - KEYQUOTA_LINK_BYTES);
1398ca4da5ddSColin Ian King 		}
1399b2a4df20SDavid Howells 		assoc_array_cancel_edit(edit);
1400f70e2e06SDavid Howells 	}
1401f70e2e06SDavid Howells 	up_write(&keyring->sem);
1402df593ee2SDavid Howells 
1403df593ee2SDavid Howells 	if (index_key->type == &key_type_keyring)
1404df593ee2SDavid Howells 		mutex_unlock(&keyring_serialise_link_lock);
1405f70e2e06SDavid Howells }
1406f70e2e06SDavid Howells 
14075ac7eaceSDavid Howells /*
14085ac7eaceSDavid Howells  * Check addition of keys to restricted keyrings.
14095ac7eaceSDavid Howells  */
__key_link_check_restriction(struct key * keyring,struct key * key)14105ac7eaceSDavid Howells static int __key_link_check_restriction(struct key *keyring, struct key *key)
14115ac7eaceSDavid Howells {
14122b6aa412SMat Martineau 	if (!keyring->restrict_link || !keyring->restrict_link->check)
14135ac7eaceSDavid Howells 		return 0;
14142b6aa412SMat Martineau 	return keyring->restrict_link->check(keyring, key->type, &key->payload,
14152b6aa412SMat Martineau 					     keyring->restrict_link->key);
14165ac7eaceSDavid Howells }
14175ac7eaceSDavid Howells 
1418973c9f4fSDavid Howells /**
1419973c9f4fSDavid Howells  * key_link - Link a key to a keyring
1420973c9f4fSDavid Howells  * @keyring: The keyring to make the link in.
1421973c9f4fSDavid Howells  * @key: The key to link to.
1422973c9f4fSDavid Howells  *
1423973c9f4fSDavid Howells  * Make a link in a keyring to a key, such that the keyring holds a reference
1424973c9f4fSDavid Howells  * on that key and the key can potentially be found by searching that keyring.
1425973c9f4fSDavid Howells  *
1426973c9f4fSDavid Howells  * This function will write-lock the keyring's semaphore and will consume some
1427973c9f4fSDavid Howells  * of the user's key data quota to hold the link.
1428973c9f4fSDavid Howells  *
1429973c9f4fSDavid Howells  * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring,
1430973c9f4fSDavid Howells  * -EKEYREVOKED if the keyring has been revoked, -ENFILE if the keyring is
1431973c9f4fSDavid Howells  * full, -EDQUOT if there is insufficient key data quota remaining to add
1432973c9f4fSDavid Howells  * another link or -ENOMEM if there's insufficient memory.
1433973c9f4fSDavid Howells  *
1434973c9f4fSDavid Howells  * It is assumed that the caller has checked that it is permitted for a link to
1435973c9f4fSDavid Howells  * be made (the keyring should have Write permission and the key Link
1436973c9f4fSDavid Howells  * permission).
14371da177e4SLinus Torvalds  */
key_link(struct key * keyring,struct key * key)14381da177e4SLinus Torvalds int key_link(struct key *keyring, struct key *key)
14391da177e4SLinus Torvalds {
1440df593ee2SDavid Howells 	struct assoc_array_edit *edit = NULL;
14411da177e4SLinus Torvalds 	int ret;
14421da177e4SLinus Torvalds 
1443fff29291SElena Reshetova 	kenter("{%d,%d}", keyring->serial, refcount_read(&keyring->usage));
1444b2a4df20SDavid Howells 
14451da177e4SLinus Torvalds 	key_check(keyring);
14461da177e4SLinus Torvalds 	key_check(key);
14471da177e4SLinus Torvalds 
1448df593ee2SDavid Howells 	ret = __key_link_lock(keyring, &key->index_key);
1449df593ee2SDavid Howells 	if (ret < 0)
1450df593ee2SDavid Howells 		goto error;
1451df593ee2SDavid Howells 
1452b2a4df20SDavid Howells 	ret = __key_link_begin(keyring, &key->index_key, &edit);
1453df593ee2SDavid Howells 	if (ret < 0)
1454df593ee2SDavid Howells 		goto error_end;
1455df593ee2SDavid Howells 
1456fff29291SElena Reshetova 	kdebug("begun {%d,%d}", keyring->serial, refcount_read(&keyring->usage));
14575ac7eaceSDavid Howells 	ret = __key_link_check_restriction(keyring, key);
14585ac7eaceSDavid Howells 	if (ret == 0)
1459f70e2e06SDavid Howells 		ret = __key_link_check_live_key(keyring, key);
1460f70e2e06SDavid Howells 	if (ret == 0)
1461f7e47677SDavid Howells 		__key_link(keyring, key, &edit);
14621da177e4SLinus Torvalds 
1463df593ee2SDavid Howells error_end:
1464df593ee2SDavid Howells 	__key_link_end(keyring, &key->index_key, edit);
1465df593ee2SDavid Howells error:
1466fff29291SElena Reshetova 	kleave(" = %d {%d,%d}", ret, keyring->serial, refcount_read(&keyring->usage));
14671da177e4SLinus Torvalds 	return ret;
1468f70e2e06SDavid Howells }
14691da177e4SLinus Torvalds EXPORT_SYMBOL(key_link);
14701da177e4SLinus Torvalds 
1471eb0f68cbSDavid Howells /*
1472eb0f68cbSDavid Howells  * Lock a keyring for unlink.
1473eb0f68cbSDavid Howells  */
__key_unlink_lock(struct key * keyring)1474eb0f68cbSDavid Howells static int __key_unlink_lock(struct key *keyring)
1475eb0f68cbSDavid Howells 	__acquires(&keyring->sem)
1476eb0f68cbSDavid Howells {
1477eb0f68cbSDavid Howells 	if (keyring->type != &key_type_keyring)
1478eb0f68cbSDavid Howells 		return -ENOTDIR;
1479eb0f68cbSDavid Howells 
1480eb0f68cbSDavid Howells 	down_write(&keyring->sem);
1481eb0f68cbSDavid Howells 	return 0;
1482eb0f68cbSDavid Howells }
1483eb0f68cbSDavid Howells 
1484eb0f68cbSDavid Howells /*
1485eb0f68cbSDavid Howells  * Begin the process of unlinking a key from a keyring.
1486eb0f68cbSDavid Howells  */
__key_unlink_begin(struct key * keyring,struct key * key,struct assoc_array_edit ** _edit)1487eb0f68cbSDavid Howells static int __key_unlink_begin(struct key *keyring, struct key *key,
1488eb0f68cbSDavid Howells 			      struct assoc_array_edit **_edit)
1489eb0f68cbSDavid Howells {
1490eb0f68cbSDavid Howells 	struct assoc_array_edit *edit;
1491eb0f68cbSDavid Howells 
1492eb0f68cbSDavid Howells 	BUG_ON(*_edit != NULL);
1493eb0f68cbSDavid Howells 
1494eb0f68cbSDavid Howells 	edit = assoc_array_delete(&keyring->keys, &keyring_assoc_array_ops,
1495eb0f68cbSDavid Howells 				  &key->index_key);
1496eb0f68cbSDavid Howells 	if (IS_ERR(edit))
1497eb0f68cbSDavid Howells 		return PTR_ERR(edit);
1498eb0f68cbSDavid Howells 
1499eb0f68cbSDavid Howells 	if (!edit)
1500eb0f68cbSDavid Howells 		return -ENOENT;
1501eb0f68cbSDavid Howells 
1502eb0f68cbSDavid Howells 	*_edit = edit;
1503eb0f68cbSDavid Howells 	return 0;
1504eb0f68cbSDavid Howells }
1505eb0f68cbSDavid Howells 
1506eb0f68cbSDavid Howells /*
1507eb0f68cbSDavid Howells  * Apply an unlink change.
1508eb0f68cbSDavid Howells  */
__key_unlink(struct key * keyring,struct key * key,struct assoc_array_edit ** _edit)1509eb0f68cbSDavid Howells static void __key_unlink(struct key *keyring, struct key *key,
1510eb0f68cbSDavid Howells 			 struct assoc_array_edit **_edit)
1511eb0f68cbSDavid Howells {
1512eb0f68cbSDavid Howells 	assoc_array_apply_edit(*_edit);
1513f7e47677SDavid Howells 	notify_key(keyring, NOTIFY_KEY_UNLINKED, key_serial(key));
1514eb0f68cbSDavid Howells 	*_edit = NULL;
1515eb0f68cbSDavid Howells 	key_payload_reserve(keyring, keyring->datalen - KEYQUOTA_LINK_BYTES);
1516eb0f68cbSDavid Howells }
1517eb0f68cbSDavid Howells 
1518eb0f68cbSDavid Howells /*
1519eb0f68cbSDavid Howells  * Finish unlinking a key from to a keyring.
1520eb0f68cbSDavid Howells  */
__key_unlink_end(struct key * keyring,struct key * key,struct assoc_array_edit * edit)1521eb0f68cbSDavid Howells static void __key_unlink_end(struct key *keyring,
1522eb0f68cbSDavid Howells 			     struct key *key,
1523eb0f68cbSDavid Howells 			     struct assoc_array_edit *edit)
1524eb0f68cbSDavid Howells 	__releases(&keyring->sem)
1525eb0f68cbSDavid Howells {
1526eb0f68cbSDavid Howells 	if (edit)
1527eb0f68cbSDavid Howells 		assoc_array_cancel_edit(edit);
1528eb0f68cbSDavid Howells 	up_write(&keyring->sem);
1529eb0f68cbSDavid Howells }
1530eb0f68cbSDavid Howells 
1531973c9f4fSDavid Howells /**
1532973c9f4fSDavid Howells  * key_unlink - Unlink the first link to a key from a keyring.
1533973c9f4fSDavid Howells  * @keyring: The keyring to remove the link from.
1534973c9f4fSDavid Howells  * @key: The key the link is to.
1535973c9f4fSDavid Howells  *
1536973c9f4fSDavid Howells  * Remove a link from a keyring to a key.
1537973c9f4fSDavid Howells  *
1538973c9f4fSDavid Howells  * This function will write-lock the keyring's semaphore.
1539973c9f4fSDavid Howells  *
1540973c9f4fSDavid Howells  * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring, -ENOENT if
1541973c9f4fSDavid Howells  * the key isn't linked to by the keyring or -ENOMEM if there's insufficient
1542973c9f4fSDavid Howells  * memory.
1543973c9f4fSDavid Howells  *
1544973c9f4fSDavid Howells  * It is assumed that the caller has checked that it is permitted for a link to
1545973c9f4fSDavid Howells  * be removed (the keyring should have Write permission; no permissions are
1546973c9f4fSDavid Howells  * required on the key).
15471da177e4SLinus Torvalds  */
key_unlink(struct key * keyring,struct key * key)15481da177e4SLinus Torvalds int key_unlink(struct key *keyring, struct key *key)
15491da177e4SLinus Torvalds {
1550eb0f68cbSDavid Howells 	struct assoc_array_edit *edit = NULL;
1551b2a4df20SDavid Howells 	int ret;
15521da177e4SLinus Torvalds 
15531da177e4SLinus Torvalds 	key_check(keyring);
15541da177e4SLinus Torvalds 	key_check(key);
15551da177e4SLinus Torvalds 
1556eb0f68cbSDavid Howells 	ret = __key_unlink_lock(keyring);
1557eb0f68cbSDavid Howells 	if (ret < 0)
1558eb0f68cbSDavid Howells 		return ret;
15591da177e4SLinus Torvalds 
1560eb0f68cbSDavid Howells 	ret = __key_unlink_begin(keyring, key, &edit);
1561eb0f68cbSDavid Howells 	if (ret == 0)
1562eb0f68cbSDavid Howells 		__key_unlink(keyring, key, &edit);
1563eb0f68cbSDavid Howells 	__key_unlink_end(keyring, key, edit);
1564b2a4df20SDavid Howells 	return ret;
1565a8b17ed0SDavid Howells }
15661da177e4SLinus Torvalds EXPORT_SYMBOL(key_unlink);
15671da177e4SLinus Torvalds 
1568973c9f4fSDavid Howells /**
1569ed0ac5c7SDavid Howells  * key_move - Move a key from one keyring to another
1570ed0ac5c7SDavid Howells  * @key: The key to move
1571ed0ac5c7SDavid Howells  * @from_keyring: The keyring to remove the link from.
1572ed0ac5c7SDavid Howells  * @to_keyring: The keyring to make the link in.
1573ed0ac5c7SDavid Howells  * @flags: Qualifying flags, such as KEYCTL_MOVE_EXCL.
1574ed0ac5c7SDavid Howells  *
1575ed0ac5c7SDavid Howells  * Make a link in @to_keyring to a key, such that the keyring holds a reference
1576ed0ac5c7SDavid Howells  * on that key and the key can potentially be found by searching that keyring
1577ed0ac5c7SDavid Howells  * whilst simultaneously removing a link to the key from @from_keyring.
1578ed0ac5c7SDavid Howells  *
1579ed0ac5c7SDavid Howells  * This function will write-lock both keyring's semaphores and will consume
1580ed0ac5c7SDavid Howells  * some of the user's key data quota to hold the link on @to_keyring.
1581ed0ac5c7SDavid Howells  *
1582ed0ac5c7SDavid Howells  * Returns 0 if successful, -ENOTDIR if either keyring isn't a keyring,
1583ed0ac5c7SDavid Howells  * -EKEYREVOKED if either keyring has been revoked, -ENFILE if the second
1584ed0ac5c7SDavid Howells  * keyring is full, -EDQUOT if there is insufficient key data quota remaining
1585ed0ac5c7SDavid Howells  * to add another link or -ENOMEM if there's insufficient memory.  If
1586ed0ac5c7SDavid Howells  * KEYCTL_MOVE_EXCL is set, then -EEXIST will be returned if there's already a
1587ed0ac5c7SDavid Howells  * matching key in @to_keyring.
1588ed0ac5c7SDavid Howells  *
1589ed0ac5c7SDavid Howells  * It is assumed that the caller has checked that it is permitted for a link to
1590ed0ac5c7SDavid Howells  * be made (the keyring should have Write permission and the key Link
1591ed0ac5c7SDavid Howells  * permission).
1592ed0ac5c7SDavid Howells  */
key_move(struct key * key,struct key * from_keyring,struct key * to_keyring,unsigned int flags)1593ed0ac5c7SDavid Howells int key_move(struct key *key,
1594ed0ac5c7SDavid Howells 	     struct key *from_keyring,
1595ed0ac5c7SDavid Howells 	     struct key *to_keyring,
1596ed0ac5c7SDavid Howells 	     unsigned int flags)
1597ed0ac5c7SDavid Howells {
1598ed0ac5c7SDavid Howells 	struct assoc_array_edit *from_edit = NULL, *to_edit = NULL;
1599ed0ac5c7SDavid Howells 	int ret;
1600ed0ac5c7SDavid Howells 
1601ed0ac5c7SDavid Howells 	kenter("%d,%d,%d", key->serial, from_keyring->serial, to_keyring->serial);
1602ed0ac5c7SDavid Howells 
1603ed0ac5c7SDavid Howells 	if (from_keyring == to_keyring)
1604ed0ac5c7SDavid Howells 		return 0;
1605ed0ac5c7SDavid Howells 
1606ed0ac5c7SDavid Howells 	key_check(key);
1607ed0ac5c7SDavid Howells 	key_check(from_keyring);
1608ed0ac5c7SDavid Howells 	key_check(to_keyring);
1609ed0ac5c7SDavid Howells 
1610ed0ac5c7SDavid Howells 	ret = __key_move_lock(from_keyring, to_keyring, &key->index_key);
1611ed0ac5c7SDavid Howells 	if (ret < 0)
1612ed0ac5c7SDavid Howells 		goto out;
1613ed0ac5c7SDavid Howells 	ret = __key_unlink_begin(from_keyring, key, &from_edit);
1614ed0ac5c7SDavid Howells 	if (ret < 0)
1615ed0ac5c7SDavid Howells 		goto error;
1616ed0ac5c7SDavid Howells 	ret = __key_link_begin(to_keyring, &key->index_key, &to_edit);
1617ed0ac5c7SDavid Howells 	if (ret < 0)
1618ed0ac5c7SDavid Howells 		goto error;
1619ed0ac5c7SDavid Howells 
1620ed0ac5c7SDavid Howells 	ret = -EEXIST;
1621ed0ac5c7SDavid Howells 	if (to_edit->dead_leaf && (flags & KEYCTL_MOVE_EXCL))
1622ed0ac5c7SDavid Howells 		goto error;
1623ed0ac5c7SDavid Howells 
1624ed0ac5c7SDavid Howells 	ret = __key_link_check_restriction(to_keyring, key);
1625ed0ac5c7SDavid Howells 	if (ret < 0)
1626ed0ac5c7SDavid Howells 		goto error;
1627ed0ac5c7SDavid Howells 	ret = __key_link_check_live_key(to_keyring, key);
1628ed0ac5c7SDavid Howells 	if (ret < 0)
1629ed0ac5c7SDavid Howells 		goto error;
1630ed0ac5c7SDavid Howells 
1631ed0ac5c7SDavid Howells 	__key_unlink(from_keyring, key, &from_edit);
1632f7e47677SDavid Howells 	__key_link(to_keyring, key, &to_edit);
1633ed0ac5c7SDavid Howells error:
1634ed0ac5c7SDavid Howells 	__key_link_end(to_keyring, &key->index_key, to_edit);
1635ed0ac5c7SDavid Howells 	__key_unlink_end(from_keyring, key, from_edit);
1636ed0ac5c7SDavid Howells out:
1637ed0ac5c7SDavid Howells 	kleave(" = %d", ret);
1638ed0ac5c7SDavid Howells 	return ret;
1639ed0ac5c7SDavid Howells }
1640ed0ac5c7SDavid Howells EXPORT_SYMBOL(key_move);
1641ed0ac5c7SDavid Howells 
1642ed0ac5c7SDavid Howells /**
1643973c9f4fSDavid Howells  * keyring_clear - Clear a keyring
1644973c9f4fSDavid Howells  * @keyring: The keyring to clear.
1645973c9f4fSDavid Howells  *
1646973c9f4fSDavid Howells  * Clear the contents of the specified keyring.
1647973c9f4fSDavid Howells  *
1648973c9f4fSDavid Howells  * Returns 0 if successful or -ENOTDIR if the keyring isn't a keyring.
16491da177e4SLinus Torvalds  */
keyring_clear(struct key * keyring)16501da177e4SLinus Torvalds int keyring_clear(struct key *keyring)
16511da177e4SLinus Torvalds {
1652b2a4df20SDavid Howells 	struct assoc_array_edit *edit;
165376d8aeabSDavid Howells 	int ret;
16541da177e4SLinus Torvalds 
1655b2a4df20SDavid Howells 	if (keyring->type != &key_type_keyring)
1656b2a4df20SDavid Howells 		return -ENOTDIR;
1657b2a4df20SDavid Howells 
16581da177e4SLinus Torvalds 	down_write(&keyring->sem);
16591da177e4SLinus Torvalds 
1660b2a4df20SDavid Howells 	edit = assoc_array_clear(&keyring->keys, &keyring_assoc_array_ops);
1661b2a4df20SDavid Howells 	if (IS_ERR(edit)) {
1662b2a4df20SDavid Howells 		ret = PTR_ERR(edit);
1663b2a4df20SDavid Howells 	} else {
1664b2a4df20SDavid Howells 		if (edit)
1665b2a4df20SDavid Howells 			assoc_array_apply_edit(edit);
1666f7e47677SDavid Howells 		notify_key(keyring, NOTIFY_KEY_CLEARED, 0);
1667b2a4df20SDavid Howells 		key_payload_reserve(keyring, 0);
16681da177e4SLinus Torvalds 		ret = 0;
16691da177e4SLinus Torvalds 	}
16701da177e4SLinus Torvalds 
1671b2a4df20SDavid Howells 	up_write(&keyring->sem);
16721da177e4SLinus Torvalds 	return ret;
1673a8b17ed0SDavid Howells }
16741da177e4SLinus Torvalds EXPORT_SYMBOL(keyring_clear);
167531204ed9SDavid Howells 
167631204ed9SDavid Howells /*
1677973c9f4fSDavid Howells  * Dispose of the links from a revoked keyring.
1678973c9f4fSDavid Howells  *
1679973c9f4fSDavid Howells  * This is called with the key sem write-locked.
168031204ed9SDavid Howells  */
keyring_revoke(struct key * keyring)168131204ed9SDavid Howells static void keyring_revoke(struct key *keyring)
168231204ed9SDavid Howells {
1683b2a4df20SDavid Howells 	struct assoc_array_edit *edit;
1684f0641cbaSDavid Howells 
1685b2a4df20SDavid Howells 	edit = assoc_array_clear(&keyring->keys, &keyring_assoc_array_ops);
1686b2a4df20SDavid Howells 	if (!IS_ERR(edit)) {
1687b2a4df20SDavid Howells 		if (edit)
1688b2a4df20SDavid Howells 			assoc_array_apply_edit(edit);
168931204ed9SDavid Howells 		key_payload_reserve(keyring, 0);
169031204ed9SDavid Howells 	}
1691a8b17ed0SDavid Howells }
16925d135440SDavid Howells 
keyring_gc_select_iterator(void * object,void * iterator_data)169362fe3182SDavid Howells static bool keyring_gc_select_iterator(void *object, void *iterator_data)
1694b2a4df20SDavid Howells {
1695b2a4df20SDavid Howells 	struct key *key = keyring_ptr_to_key(object);
1696074d5898SBaolin Wang 	time64_t *limit = iterator_data;
1697b2a4df20SDavid Howells 
1698b2a4df20SDavid Howells 	if (key_is_dead(key, *limit))
1699b2a4df20SDavid Howells 		return false;
1700b2a4df20SDavid Howells 	key_get(key);
1701b2a4df20SDavid Howells 	return true;
1702b2a4df20SDavid Howells }
1703b2a4df20SDavid Howells 
keyring_gc_check_iterator(const void * object,void * iterator_data)170462fe3182SDavid Howells static int keyring_gc_check_iterator(const void *object, void *iterator_data)
170562fe3182SDavid Howells {
170662fe3182SDavid Howells 	const struct key *key = keyring_ptr_to_key(object);
1707074d5898SBaolin Wang 	time64_t *limit = iterator_data;
170862fe3182SDavid Howells 
170962fe3182SDavid Howells 	key_check(key);
171062fe3182SDavid Howells 	return key_is_dead(key, *limit);
171162fe3182SDavid Howells }
171262fe3182SDavid Howells 
17135d135440SDavid Howells /*
171462fe3182SDavid Howells  * Garbage collect pointers from a keyring.
1715973c9f4fSDavid Howells  *
171662fe3182SDavid Howells  * Not called with any locks held.  The keyring's key struct will not be
171762fe3182SDavid Howells  * deallocated under us as only our caller may deallocate it.
17185d135440SDavid Howells  */
keyring_gc(struct key * keyring,time64_t limit)1719074d5898SBaolin Wang void keyring_gc(struct key *keyring, time64_t limit)
17205d135440SDavid Howells {
172162fe3182SDavid Howells 	int result;
17225d135440SDavid Howells 
172362fe3182SDavid Howells 	kenter("%x{%s}", keyring->serial, keyring->description ?: "");
172462fe3182SDavid Howells 
172562fe3182SDavid Howells 	if (keyring->flags & ((1 << KEY_FLAG_INVALIDATED) |
172662fe3182SDavid Howells 			      (1 << KEY_FLAG_REVOKED)))
172762fe3182SDavid Howells 		goto dont_gc;
172862fe3182SDavid Howells 
172962fe3182SDavid Howells 	/* scan the keyring looking for dead keys */
173062fe3182SDavid Howells 	rcu_read_lock();
173162fe3182SDavid Howells 	result = assoc_array_iterate(&keyring->keys,
173262fe3182SDavid Howells 				     keyring_gc_check_iterator, &limit);
173362fe3182SDavid Howells 	rcu_read_unlock();
173462fe3182SDavid Howells 	if (result == true)
173562fe3182SDavid Howells 		goto do_gc;
173662fe3182SDavid Howells 
173762fe3182SDavid Howells dont_gc:
173862fe3182SDavid Howells 	kleave(" [no gc]");
173962fe3182SDavid Howells 	return;
174062fe3182SDavid Howells 
174162fe3182SDavid Howells do_gc:
17425d135440SDavid Howells 	down_write(&keyring->sem);
1743b2a4df20SDavid Howells 	assoc_array_gc(&keyring->keys, &keyring_assoc_array_ops,
174462fe3182SDavid Howells 		       keyring_gc_select_iterator, &limit);
17455d135440SDavid Howells 	up_write(&keyring->sem);
174662fe3182SDavid Howells 	kleave(" [gc]");
17475d135440SDavid Howells }
17482b6aa412SMat Martineau 
17492b6aa412SMat Martineau /*
17502b6aa412SMat Martineau  * Garbage collect restriction pointers from a keyring.
17512b6aa412SMat Martineau  *
17522b6aa412SMat Martineau  * Keyring restrictions are associated with a key type, and must be cleaned
17532b6aa412SMat Martineau  * up if the key type is unregistered. The restriction is altered to always
17542b6aa412SMat Martineau  * reject additional keys so a keyring cannot be opened up by unregistering
17552b6aa412SMat Martineau  * a key type.
17562b6aa412SMat Martineau  *
17572b6aa412SMat Martineau  * Not called with any keyring locks held. The keyring's key struct will not
17582b6aa412SMat Martineau  * be deallocated under us as only our caller may deallocate it.
17592b6aa412SMat Martineau  *
17602b6aa412SMat Martineau  * The caller is required to hold key_types_sem and dead_type->sem. This is
17612b6aa412SMat Martineau  * fulfilled by key_gc_keytype() holding the locks on behalf of
17622b6aa412SMat Martineau  * key_garbage_collector(), which it invokes on a workqueue.
17632b6aa412SMat Martineau  */
keyring_restriction_gc(struct key * keyring,struct key_type * dead_type)17642b6aa412SMat Martineau void keyring_restriction_gc(struct key *keyring, struct key_type *dead_type)
17652b6aa412SMat Martineau {
17662b6aa412SMat Martineau 	struct key_restriction *keyres;
17672b6aa412SMat Martineau 
17682b6aa412SMat Martineau 	kenter("%x{%s}", keyring->serial, keyring->description ?: "");
17692b6aa412SMat Martineau 
17702b6aa412SMat Martineau 	/*
17712b6aa412SMat Martineau 	 * keyring->restrict_link is only assigned at key allocation time
17722b6aa412SMat Martineau 	 * or with the key type locked, so the only values that could be
17732b6aa412SMat Martineau 	 * concurrently assigned to keyring->restrict_link are for key
17742b6aa412SMat Martineau 	 * types other than dead_type. Given this, it's ok to check
17752b6aa412SMat Martineau 	 * the key type before acquiring keyring->sem.
17762b6aa412SMat Martineau 	 */
17772b6aa412SMat Martineau 	if (!dead_type || !keyring->restrict_link ||
17782b6aa412SMat Martineau 	    keyring->restrict_link->keytype != dead_type) {
17792b6aa412SMat Martineau 		kleave(" [no restriction gc]");
17802b6aa412SMat Martineau 		return;
17812b6aa412SMat Martineau 	}
17822b6aa412SMat Martineau 
17832b6aa412SMat Martineau 	/* Lock the keyring to ensure that a link is not in progress */
17842b6aa412SMat Martineau 	down_write(&keyring->sem);
17852b6aa412SMat Martineau 
17862b6aa412SMat Martineau 	keyres = keyring->restrict_link;
17872b6aa412SMat Martineau 
17882b6aa412SMat Martineau 	keyres->check = restrict_link_reject;
17892b6aa412SMat Martineau 
17902b6aa412SMat Martineau 	key_put(keyres->key);
17912b6aa412SMat Martineau 	keyres->key = NULL;
17922b6aa412SMat Martineau 	keyres->keytype = NULL;
17932b6aa412SMat Martineau 
17942b6aa412SMat Martineau 	up_write(&keyring->sem);
17952b6aa412SMat Martineau 
17962b6aa412SMat Martineau 	kleave(" [restriction gc]");
17972b6aa412SMat Martineau }
1798