xref: /linux/security/keys/keyring.c (revision 3ef975893041b9f826475298c802b5c046d0ba16)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Keyring handling
3  *
4  * Copyright (C) 2004-2005, 2008, 2013 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  */
7 
8 #include <linux/export.h>
9 #include <linux/init.h>
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/security.h>
13 #include <linux/seq_file.h>
14 #include <linux/err.h>
15 #include <linux/user_namespace.h>
16 #include <linux/nsproxy.h>
17 #include <keys/keyring-type.h>
18 #include <keys/user-type.h>
19 #include <linux/assoc_array_priv.h>
20 #include <linux/uaccess.h>
21 #include <net/net_namespace.h>
22 #include "internal.h"
23 
24 /*
25  * When plumbing the depths of the key tree, this sets a hard limit
26  * set on how deep we're willing to go.
27  */
28 #define KEYRING_SEARCH_MAX_DEPTH 6
29 
30 /*
31  * We mark pointers we pass to the associative array with bit 1 set if
32  * they're keyrings and clear otherwise.
33  */
34 #define KEYRING_PTR_SUBTYPE	0x2UL
35 
36 static inline bool keyring_ptr_is_keyring(const struct assoc_array_ptr *x)
37 {
38 	return (unsigned long)x & KEYRING_PTR_SUBTYPE;
39 }
40 static inline struct key *keyring_ptr_to_key(const struct assoc_array_ptr *x)
41 {
42 	void *object = assoc_array_ptr_to_leaf(x);
43 	return (struct key *)((unsigned long)object & ~KEYRING_PTR_SUBTYPE);
44 }
45 static inline void *keyring_key_to_ptr(struct key *key)
46 {
47 	if (key->type == &key_type_keyring)
48 		return (void *)((unsigned long)key | KEYRING_PTR_SUBTYPE);
49 	return key;
50 }
51 
52 static DEFINE_RWLOCK(keyring_name_lock);
53 
54 /*
55  * Clean up the bits of user_namespace that belong to us.
56  */
57 void key_free_user_ns(struct user_namespace *ns)
58 {
59 	write_lock(&keyring_name_lock);
60 	list_del_init(&ns->keyring_name_list);
61 	write_unlock(&keyring_name_lock);
62 
63 	key_put(ns->user_keyring_register);
64 #ifdef CONFIG_PERSISTENT_KEYRINGS
65 	key_put(ns->persistent_keyring_register);
66 #endif
67 }
68 
69 /*
70  * The keyring key type definition.  Keyrings are simply keys of this type and
71  * can be treated as ordinary keys in addition to having their own special
72  * operations.
73  */
74 static int keyring_preparse(struct key_preparsed_payload *prep);
75 static void keyring_free_preparse(struct key_preparsed_payload *prep);
76 static int keyring_instantiate(struct key *keyring,
77 			       struct key_preparsed_payload *prep);
78 static void keyring_revoke(struct key *keyring);
79 static void keyring_destroy(struct key *keyring);
80 static void keyring_describe(const struct key *keyring, struct seq_file *m);
81 static long keyring_read(const struct key *keyring,
82 			 char *buffer, size_t buflen);
83 
84 struct key_type key_type_keyring = {
85 	.name		= "keyring",
86 	.def_datalen	= 0,
87 	.preparse	= keyring_preparse,
88 	.free_preparse	= keyring_free_preparse,
89 	.instantiate	= keyring_instantiate,
90 	.revoke		= keyring_revoke,
91 	.destroy	= keyring_destroy,
92 	.describe	= keyring_describe,
93 	.read		= keyring_read,
94 };
95 EXPORT_SYMBOL(key_type_keyring);
96 
97 /*
98  * Semaphore to serialise link/link calls to prevent two link calls in parallel
99  * introducing a cycle.
100  */
101 static DEFINE_MUTEX(keyring_serialise_link_lock);
102 
103 /*
104  * Publish the name of a keyring so that it can be found by name (if it has
105  * one and it doesn't begin with a dot).
106  */
107 static void keyring_publish_name(struct key *keyring)
108 {
109 	struct user_namespace *ns = current_user_ns();
110 
111 	if (keyring->description &&
112 	    keyring->description[0] &&
113 	    keyring->description[0] != '.') {
114 		write_lock(&keyring_name_lock);
115 		list_add_tail(&keyring->name_link, &ns->keyring_name_list);
116 		write_unlock(&keyring_name_lock);
117 	}
118 }
119 
120 /*
121  * Preparse a keyring payload
122  */
123 static int keyring_preparse(struct key_preparsed_payload *prep)
124 {
125 	return prep->datalen != 0 ? -EINVAL : 0;
126 }
127 
128 /*
129  * Free a preparse of a user defined key payload
130  */
131 static void keyring_free_preparse(struct key_preparsed_payload *prep)
132 {
133 }
134 
135 /*
136  * Initialise a keyring.
137  *
138  * Returns 0 on success, -EINVAL if given any data.
139  */
140 static int keyring_instantiate(struct key *keyring,
141 			       struct key_preparsed_payload *prep)
142 {
143 	assoc_array_init(&keyring->keys);
144 	/* make the keyring available by name if it has one */
145 	keyring_publish_name(keyring);
146 	return 0;
147 }
148 
149 /*
150  * Multiply 64-bits by 32-bits to 96-bits and fold back to 64-bit.  Ideally we'd
151  * fold the carry back too, but that requires inline asm.
152  */
153 static u64 mult_64x32_and_fold(u64 x, u32 y)
154 {
155 	u64 hi = (u64)(u32)(x >> 32) * y;
156 	u64 lo = (u64)(u32)(x) * y;
157 	return lo + ((u64)(u32)hi << 32) + (u32)(hi >> 32);
158 }
159 
160 /*
161  * Hash a key type and description.
162  */
163 static void hash_key_type_and_desc(struct keyring_index_key *index_key)
164 {
165 	const unsigned level_shift = ASSOC_ARRAY_LEVEL_STEP;
166 	const unsigned long fan_mask = ASSOC_ARRAY_FAN_MASK;
167 	const char *description = index_key->description;
168 	unsigned long hash, type;
169 	u32 piece;
170 	u64 acc;
171 	int n, desc_len = index_key->desc_len;
172 
173 	type = (unsigned long)index_key->type;
174 	acc = mult_64x32_and_fold(type, desc_len + 13);
175 	acc = mult_64x32_and_fold(acc, 9207);
176 	piece = (unsigned long)index_key->domain_tag;
177 	acc = mult_64x32_and_fold(acc, piece);
178 	acc = mult_64x32_and_fold(acc, 9207);
179 
180 	for (;;) {
181 		n = desc_len;
182 		if (n <= 0)
183 			break;
184 		if (n > 4)
185 			n = 4;
186 		piece = 0;
187 		memcpy(&piece, description, n);
188 		description += n;
189 		desc_len -= n;
190 		acc = mult_64x32_and_fold(acc, piece);
191 		acc = mult_64x32_and_fold(acc, 9207);
192 	}
193 
194 	/* Fold the hash down to 32 bits if need be. */
195 	hash = acc;
196 	if (ASSOC_ARRAY_KEY_CHUNK_SIZE == 32)
197 		hash ^= acc >> 32;
198 
199 	/* Squidge all the keyrings into a separate part of the tree to
200 	 * ordinary keys by making sure the lowest level segment in the hash is
201 	 * zero for keyrings and non-zero otherwise.
202 	 */
203 	if (index_key->type != &key_type_keyring && (hash & fan_mask) == 0)
204 		hash |= (hash >> (ASSOC_ARRAY_KEY_CHUNK_SIZE - level_shift)) | 1;
205 	else if (index_key->type == &key_type_keyring && (hash & fan_mask) != 0)
206 		hash = (hash + (hash << level_shift)) & ~fan_mask;
207 	index_key->hash = hash;
208 }
209 
210 /*
211  * Finalise an index key to include a part of the description actually in the
212  * index key, to set the domain tag and to calculate the hash.
213  */
214 void key_set_index_key(struct keyring_index_key *index_key)
215 {
216 	static struct key_tag default_domain_tag = { .usage = REFCOUNT_INIT(1), };
217 	size_t n = min_t(size_t, index_key->desc_len, sizeof(index_key->desc));
218 
219 	memcpy(index_key->desc, index_key->description, n);
220 
221 	if (!index_key->domain_tag) {
222 		if (index_key->type->flags & KEY_TYPE_NET_DOMAIN)
223 			index_key->domain_tag = current->nsproxy->net_ns->key_domain;
224 		else
225 			index_key->domain_tag = &default_domain_tag;
226 	}
227 
228 	hash_key_type_and_desc(index_key);
229 }
230 
231 /**
232  * key_put_tag - Release a ref on a tag.
233  * @tag: The tag to release.
234  *
235  * This releases a reference the given tag and returns true if that ref was the
236  * last one.
237  */
238 bool key_put_tag(struct key_tag *tag)
239 {
240 	if (refcount_dec_and_test(&tag->usage)) {
241 		kfree_rcu(tag, rcu);
242 		return true;
243 	}
244 
245 	return false;
246 }
247 
248 /**
249  * key_remove_domain - Kill off a key domain and gc its keys
250  * @domain_tag: The domain tag to release.
251  *
252  * This marks a domain tag as being dead and releases a ref on it.  If that
253  * wasn't the last reference, the garbage collector is poked to try and delete
254  * all keys that were in the domain.
255  */
256 void key_remove_domain(struct key_tag *domain_tag)
257 {
258 	domain_tag->removed = true;
259 	if (!key_put_tag(domain_tag))
260 		key_schedule_gc_links();
261 }
262 
263 /*
264  * Build the next index key chunk.
265  *
266  * We return it one word-sized chunk at a time.
267  */
268 static unsigned long keyring_get_key_chunk(const void *data, int level)
269 {
270 	const struct keyring_index_key *index_key = data;
271 	unsigned long chunk = 0;
272 	const u8 *d;
273 	int desc_len = index_key->desc_len, n = sizeof(chunk);
274 	unsigned int offset;
275 
276 	level /= ASSOC_ARRAY_KEY_CHUNK_SIZE;
277 	switch (level) {
278 	case 0:
279 		return index_key->hash;
280 	case 1:
281 		return index_key->x;
282 	case 2:
283 		return (unsigned long)index_key->type;
284 	case 3:
285 		return (unsigned long)index_key->domain_tag;
286 	default:
287 		level -= 4;
288 		offset = sizeof(index_key->desc) + level * sizeof(long);
289 		if (desc_len <= offset)
290 			return 0;
291 
292 		d = index_key->description + offset;
293 		desc_len -= offset;
294 		if (desc_len > n)
295 			desc_len = n;
296 		d += desc_len;
297 		do {
298 			chunk <<= 8;
299 			chunk |= *--d;
300 		} while (--desc_len > 0);
301 		return chunk;
302 	}
303 }
304 
305 static unsigned long keyring_get_object_key_chunk(const void *object, int level)
306 {
307 	const struct key *key = keyring_ptr_to_key(object);
308 	return keyring_get_key_chunk(&key->index_key, level);
309 }
310 
311 static bool keyring_compare_object(const void *object, const void *data)
312 {
313 	const struct keyring_index_key *index_key = data;
314 	const struct key *key = keyring_ptr_to_key(object);
315 
316 	return key->index_key.type == index_key->type &&
317 		key->index_key.domain_tag == index_key->domain_tag &&
318 		key->index_key.desc_len == index_key->desc_len &&
319 		memcmp(key->index_key.description, index_key->description,
320 		       index_key->desc_len) == 0;
321 }
322 
323 /*
324  * Compare the index keys of a pair of objects and determine the bit position
325  * at which they differ - if they differ.
326  */
327 static int keyring_diff_objects(const void *object, const void *data)
328 {
329 	const struct key *key_a = keyring_ptr_to_key(object);
330 	const struct keyring_index_key *a = &key_a->index_key;
331 	const struct keyring_index_key *b = data;
332 	unsigned long seg_a, seg_b;
333 	int level, i;
334 
335 	level = 0;
336 	seg_a = a->hash;
337 	seg_b = b->hash;
338 	if ((seg_a ^ seg_b) != 0)
339 		goto differ;
340 	level += ASSOC_ARRAY_KEY_CHUNK_SIZE / 8;
341 
342 	/* The number of bits contributed by the hash is controlled by a
343 	 * constant in the assoc_array headers.  Everything else thereafter we
344 	 * can deal with as being machine word-size dependent.
345 	 */
346 	seg_a = a->x;
347 	seg_b = b->x;
348 	if ((seg_a ^ seg_b) != 0)
349 		goto differ;
350 	level += sizeof(unsigned long);
351 
352 	/* The next bit may not work on big endian */
353 	seg_a = (unsigned long)a->type;
354 	seg_b = (unsigned long)b->type;
355 	if ((seg_a ^ seg_b) != 0)
356 		goto differ;
357 	level += sizeof(unsigned long);
358 
359 	seg_a = (unsigned long)a->domain_tag;
360 	seg_b = (unsigned long)b->domain_tag;
361 	if ((seg_a ^ seg_b) != 0)
362 		goto differ;
363 	level += sizeof(unsigned long);
364 
365 	i = sizeof(a->desc);
366 	if (a->desc_len <= i)
367 		goto same;
368 
369 	for (; i < a->desc_len; i++) {
370 		seg_a = *(unsigned char *)(a->description + i);
371 		seg_b = *(unsigned char *)(b->description + i);
372 		if ((seg_a ^ seg_b) != 0)
373 			goto differ_plus_i;
374 	}
375 
376 same:
377 	return -1;
378 
379 differ_plus_i:
380 	level += i - (int)sizeof(a->desc);
381 differ:
382 	i = level * 8 + __ffs(seg_a ^ seg_b);
383 	return i;
384 }
385 
386 /*
387  * Free an object after stripping the keyring flag off of the pointer.
388  */
389 static void keyring_free_object(void *object)
390 {
391 	key_put(keyring_ptr_to_key(object));
392 }
393 
394 /*
395  * Operations for keyring management by the index-tree routines.
396  */
397 static const struct assoc_array_ops keyring_assoc_array_ops = {
398 	.get_key_chunk		= keyring_get_key_chunk,
399 	.get_object_key_chunk	= keyring_get_object_key_chunk,
400 	.compare_object		= keyring_compare_object,
401 	.diff_objects		= keyring_diff_objects,
402 	.free_object		= keyring_free_object,
403 };
404 
405 /*
406  * Clean up a keyring when it is destroyed.  Unpublish its name if it had one
407  * and dispose of its data.
408  *
409  * The garbage collector detects the final key_put(), removes the keyring from
410  * the serial number tree and then does RCU synchronisation before coming here,
411  * so we shouldn't need to worry about code poking around here with the RCU
412  * readlock held by this time.
413  */
414 static void keyring_destroy(struct key *keyring)
415 {
416 	if (keyring->description) {
417 		write_lock(&keyring_name_lock);
418 
419 		if (keyring->name_link.next != NULL &&
420 		    !list_empty(&keyring->name_link))
421 			list_del(&keyring->name_link);
422 
423 		write_unlock(&keyring_name_lock);
424 	}
425 
426 	if (keyring->restrict_link) {
427 		struct key_restriction *keyres = keyring->restrict_link;
428 
429 		key_put(keyres->key);
430 		kfree(keyres);
431 	}
432 
433 	assoc_array_destroy(&keyring->keys, &keyring_assoc_array_ops);
434 }
435 
436 /*
437  * Describe a keyring for /proc.
438  */
439 static void keyring_describe(const struct key *keyring, struct seq_file *m)
440 {
441 	if (keyring->description)
442 		seq_puts(m, keyring->description);
443 	else
444 		seq_puts(m, "[anon]");
445 
446 	if (key_is_positive(keyring)) {
447 		if (keyring->keys.nr_leaves_on_tree != 0)
448 			seq_printf(m, ": %lu", keyring->keys.nr_leaves_on_tree);
449 		else
450 			seq_puts(m, ": empty");
451 	}
452 }
453 
454 struct keyring_read_iterator_context {
455 	size_t			buflen;
456 	size_t			count;
457 	key_serial_t		*buffer;
458 };
459 
460 static int keyring_read_iterator(const void *object, void *data)
461 {
462 	struct keyring_read_iterator_context *ctx = data;
463 	const struct key *key = keyring_ptr_to_key(object);
464 
465 	kenter("{%s,%d},,{%zu/%zu}",
466 	       key->type->name, key->serial, ctx->count, ctx->buflen);
467 
468 	if (ctx->count >= ctx->buflen)
469 		return 1;
470 
471 	*ctx->buffer++ = key->serial;
472 	ctx->count += sizeof(key->serial);
473 	return 0;
474 }
475 
476 /*
477  * Read a list of key IDs from the keyring's contents in binary form
478  *
479  * The keyring's semaphore is read-locked by the caller.  This prevents someone
480  * from modifying it under us - which could cause us to read key IDs multiple
481  * times.
482  */
483 static long keyring_read(const struct key *keyring,
484 			 char *buffer, size_t buflen)
485 {
486 	struct keyring_read_iterator_context ctx;
487 	long ret;
488 
489 	kenter("{%d},,%zu", key_serial(keyring), buflen);
490 
491 	if (buflen & (sizeof(key_serial_t) - 1))
492 		return -EINVAL;
493 
494 	/* Copy as many key IDs as fit into the buffer */
495 	if (buffer && buflen) {
496 		ctx.buffer = (key_serial_t *)buffer;
497 		ctx.buflen = buflen;
498 		ctx.count = 0;
499 		ret = assoc_array_iterate(&keyring->keys,
500 					  keyring_read_iterator, &ctx);
501 		if (ret < 0) {
502 			kleave(" = %ld [iterate]", ret);
503 			return ret;
504 		}
505 	}
506 
507 	/* Return the size of the buffer needed */
508 	ret = keyring->keys.nr_leaves_on_tree * sizeof(key_serial_t);
509 	if (ret <= buflen)
510 		kleave("= %ld [ok]", ret);
511 	else
512 		kleave("= %ld [buffer too small]", ret);
513 	return ret;
514 }
515 
516 /*
517  * Allocate a keyring and link into the destination keyring.
518  */
519 struct key *keyring_alloc(const char *description, kuid_t uid, kgid_t gid,
520 			  const struct cred *cred, key_perm_t perm,
521 			  unsigned long flags,
522 			  struct key_restriction *restrict_link,
523 			  struct key *dest)
524 {
525 	struct key *keyring;
526 	int ret;
527 
528 	keyring = key_alloc(&key_type_keyring, description,
529 			    uid, gid, cred, perm, flags, restrict_link);
530 	if (!IS_ERR(keyring)) {
531 		ret = key_instantiate_and_link(keyring, NULL, 0, dest, NULL);
532 		if (ret < 0) {
533 			key_put(keyring);
534 			keyring = ERR_PTR(ret);
535 		}
536 	}
537 
538 	return keyring;
539 }
540 EXPORT_SYMBOL(keyring_alloc);
541 
542 /**
543  * restrict_link_reject - Give -EPERM to restrict link
544  * @keyring: The keyring being added to.
545  * @type: The type of key being added.
546  * @payload: The payload of the key intended to be added.
547  * @restriction_key: Keys providing additional data for evaluating restriction.
548  *
549  * Reject the addition of any links to a keyring.  It can be overridden by
550  * passing KEY_ALLOC_BYPASS_RESTRICTION to key_instantiate_and_link() when
551  * adding a key to a keyring.
552  *
553  * This is meant to be stored in a key_restriction structure which is passed
554  * in the restrict_link parameter to keyring_alloc().
555  */
556 int restrict_link_reject(struct key *keyring,
557 			 const struct key_type *type,
558 			 const union key_payload *payload,
559 			 struct key *restriction_key)
560 {
561 	return -EPERM;
562 }
563 
564 /*
565  * By default, we keys found by getting an exact match on their descriptions.
566  */
567 bool key_default_cmp(const struct key *key,
568 		     const struct key_match_data *match_data)
569 {
570 	return strcmp(key->description, match_data->raw_data) == 0;
571 }
572 
573 /*
574  * Iteration function to consider each key found.
575  */
576 static int keyring_search_iterator(const void *object, void *iterator_data)
577 {
578 	struct keyring_search_context *ctx = iterator_data;
579 	const struct key *key = keyring_ptr_to_key(object);
580 	unsigned long kflags = READ_ONCE(key->flags);
581 	short state = key_read_state(key);
582 
583 	kenter("{%d}", key->serial);
584 
585 	/* ignore keys not of this type */
586 	if (key->type != ctx->index_key.type) {
587 		kleave(" = 0 [!type]");
588 		return 0;
589 	}
590 
591 	/* skip invalidated, revoked and expired keys */
592 	if (ctx->flags & KEYRING_SEARCH_DO_STATE_CHECK) {
593 		time64_t expiry = READ_ONCE(key->expiry);
594 
595 		if (kflags & ((1 << KEY_FLAG_INVALIDATED) |
596 			      (1 << KEY_FLAG_REVOKED))) {
597 			ctx->result = ERR_PTR(-EKEYREVOKED);
598 			kleave(" = %d [invrev]", ctx->skipped_ret);
599 			goto skipped;
600 		}
601 
602 		if (expiry && ctx->now >= expiry) {
603 			if (!(ctx->flags & KEYRING_SEARCH_SKIP_EXPIRED))
604 				ctx->result = ERR_PTR(-EKEYEXPIRED);
605 			kleave(" = %d [expire]", ctx->skipped_ret);
606 			goto skipped;
607 		}
608 	}
609 
610 	/* keys that don't match */
611 	if (!ctx->match_data.cmp(key, &ctx->match_data)) {
612 		kleave(" = 0 [!match]");
613 		return 0;
614 	}
615 
616 	/* key must have search permissions */
617 	if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM) &&
618 	    key_task_permission(make_key_ref(key, ctx->possessed),
619 				ctx->cred, KEY_NEED_SEARCH) < 0) {
620 		ctx->result = ERR_PTR(-EACCES);
621 		kleave(" = %d [!perm]", ctx->skipped_ret);
622 		goto skipped;
623 	}
624 
625 	if (ctx->flags & KEYRING_SEARCH_DO_STATE_CHECK) {
626 		/* we set a different error code if we pass a negative key */
627 		if (state < 0) {
628 			ctx->result = ERR_PTR(state);
629 			kleave(" = %d [neg]", ctx->skipped_ret);
630 			goto skipped;
631 		}
632 	}
633 
634 	/* Found */
635 	ctx->result = make_key_ref(key, ctx->possessed);
636 	kleave(" = 1 [found]");
637 	return 1;
638 
639 skipped:
640 	return ctx->skipped_ret;
641 }
642 
643 /*
644  * Search inside a keyring for a key.  We can search by walking to it
645  * directly based on its index-key or we can iterate over the entire
646  * tree looking for it, based on the match function.
647  */
648 static int search_keyring(struct key *keyring, struct keyring_search_context *ctx)
649 {
650 	if (ctx->match_data.lookup_type == KEYRING_SEARCH_LOOKUP_DIRECT) {
651 		const void *object;
652 
653 		object = assoc_array_find(&keyring->keys,
654 					  &keyring_assoc_array_ops,
655 					  &ctx->index_key);
656 		return object ? ctx->iterator(object, ctx) : 0;
657 	}
658 	return assoc_array_iterate(&keyring->keys, ctx->iterator, ctx);
659 }
660 
661 /*
662  * Search a tree of keyrings that point to other keyrings up to the maximum
663  * depth.
664  */
665 static bool search_nested_keyrings(struct key *keyring,
666 				   struct keyring_search_context *ctx)
667 {
668 	struct {
669 		struct key *keyring;
670 		struct assoc_array_node *node;
671 		int slot;
672 	} stack[KEYRING_SEARCH_MAX_DEPTH];
673 
674 	struct assoc_array_shortcut *shortcut;
675 	struct assoc_array_node *node;
676 	struct assoc_array_ptr *ptr;
677 	struct key *key;
678 	int sp = 0, slot;
679 
680 	kenter("{%d},{%s,%s}",
681 	       keyring->serial,
682 	       ctx->index_key.type->name,
683 	       ctx->index_key.description);
684 
685 #define STATE_CHECKS (KEYRING_SEARCH_NO_STATE_CHECK | KEYRING_SEARCH_DO_STATE_CHECK)
686 	BUG_ON((ctx->flags & STATE_CHECKS) == 0 ||
687 	       (ctx->flags & STATE_CHECKS) == STATE_CHECKS);
688 
689 	if (ctx->index_key.description)
690 		key_set_index_key(&ctx->index_key);
691 
692 	/* Check to see if this top-level keyring is what we are looking for
693 	 * and whether it is valid or not.
694 	 */
695 	if (ctx->match_data.lookup_type == KEYRING_SEARCH_LOOKUP_ITERATE ||
696 	    keyring_compare_object(keyring, &ctx->index_key)) {
697 		ctx->skipped_ret = 2;
698 		switch (ctx->iterator(keyring_key_to_ptr(keyring), ctx)) {
699 		case 1:
700 			goto found;
701 		case 2:
702 			return false;
703 		default:
704 			break;
705 		}
706 	}
707 
708 	ctx->skipped_ret = 0;
709 
710 	/* Start processing a new keyring */
711 descend_to_keyring:
712 	kdebug("descend to %d", keyring->serial);
713 	if (keyring->flags & ((1 << KEY_FLAG_INVALIDATED) |
714 			      (1 << KEY_FLAG_REVOKED)))
715 		goto not_this_keyring;
716 
717 	/* Search through the keys in this keyring before its searching its
718 	 * subtrees.
719 	 */
720 	if (search_keyring(keyring, ctx))
721 		goto found;
722 
723 	/* Then manually iterate through the keyrings nested in this one.
724 	 *
725 	 * Start from the root node of the index tree.  Because of the way the
726 	 * hash function has been set up, keyrings cluster on the leftmost
727 	 * branch of the root node (root slot 0) or in the root node itself.
728 	 * Non-keyrings avoid the leftmost branch of the root entirely (root
729 	 * slots 1-15).
730 	 */
731 	if (!(ctx->flags & KEYRING_SEARCH_RECURSE))
732 		goto not_this_keyring;
733 
734 	ptr = READ_ONCE(keyring->keys.root);
735 	if (!ptr)
736 		goto not_this_keyring;
737 
738 	if (assoc_array_ptr_is_shortcut(ptr)) {
739 		/* If the root is a shortcut, either the keyring only contains
740 		 * keyring pointers (everything clusters behind root slot 0) or
741 		 * doesn't contain any keyring pointers.
742 		 */
743 		shortcut = assoc_array_ptr_to_shortcut(ptr);
744 		if ((shortcut->index_key[0] & ASSOC_ARRAY_FAN_MASK) != 0)
745 			goto not_this_keyring;
746 
747 		ptr = READ_ONCE(shortcut->next_node);
748 		node = assoc_array_ptr_to_node(ptr);
749 		goto begin_node;
750 	}
751 
752 	node = assoc_array_ptr_to_node(ptr);
753 	ptr = node->slots[0];
754 	if (!assoc_array_ptr_is_meta(ptr))
755 		goto begin_node;
756 
757 descend_to_node:
758 	/* Descend to a more distal node in this keyring's content tree and go
759 	 * through that.
760 	 */
761 	kdebug("descend");
762 	if (assoc_array_ptr_is_shortcut(ptr)) {
763 		shortcut = assoc_array_ptr_to_shortcut(ptr);
764 		ptr = READ_ONCE(shortcut->next_node);
765 		BUG_ON(!assoc_array_ptr_is_node(ptr));
766 	}
767 	node = assoc_array_ptr_to_node(ptr);
768 
769 begin_node:
770 	kdebug("begin_node");
771 	slot = 0;
772 ascend_to_node:
773 	/* Go through the slots in a node */
774 	for (; slot < ASSOC_ARRAY_FAN_OUT; slot++) {
775 		ptr = READ_ONCE(node->slots[slot]);
776 
777 		if (assoc_array_ptr_is_meta(ptr)) {
778 			if (node->back_pointer ||
779 			    assoc_array_ptr_is_shortcut(ptr))
780 				goto descend_to_node;
781 		}
782 
783 		if (!keyring_ptr_is_keyring(ptr))
784 			continue;
785 
786 		key = keyring_ptr_to_key(ptr);
787 
788 		if (sp >= KEYRING_SEARCH_MAX_DEPTH) {
789 			if (ctx->flags & KEYRING_SEARCH_DETECT_TOO_DEEP) {
790 				ctx->result = ERR_PTR(-ELOOP);
791 				return false;
792 			}
793 			goto not_this_keyring;
794 		}
795 
796 		/* Search a nested keyring */
797 		if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM) &&
798 		    key_task_permission(make_key_ref(key, ctx->possessed),
799 					ctx->cred, KEY_NEED_SEARCH) < 0)
800 			continue;
801 
802 		/* stack the current position */
803 		stack[sp].keyring = keyring;
804 		stack[sp].node = node;
805 		stack[sp].slot = slot;
806 		sp++;
807 
808 		/* begin again with the new keyring */
809 		keyring = key;
810 		goto descend_to_keyring;
811 	}
812 
813 	/* We've dealt with all the slots in the current node, so now we need
814 	 * to ascend to the parent and continue processing there.
815 	 */
816 	ptr = READ_ONCE(node->back_pointer);
817 	slot = node->parent_slot;
818 
819 	if (ptr && assoc_array_ptr_is_shortcut(ptr)) {
820 		shortcut = assoc_array_ptr_to_shortcut(ptr);
821 		ptr = READ_ONCE(shortcut->back_pointer);
822 		slot = shortcut->parent_slot;
823 	}
824 	if (!ptr)
825 		goto not_this_keyring;
826 	node = assoc_array_ptr_to_node(ptr);
827 	slot++;
828 
829 	/* If we've ascended to the root (zero backpointer), we must have just
830 	 * finished processing the leftmost branch rather than the root slots -
831 	 * so there can't be any more keyrings for us to find.
832 	 */
833 	if (node->back_pointer) {
834 		kdebug("ascend %d", slot);
835 		goto ascend_to_node;
836 	}
837 
838 	/* The keyring we're looking at was disqualified or didn't contain a
839 	 * matching key.
840 	 */
841 not_this_keyring:
842 	kdebug("not_this_keyring %d", sp);
843 	if (sp <= 0) {
844 		kleave(" = false");
845 		return false;
846 	}
847 
848 	/* Resume the processing of a keyring higher up in the tree */
849 	sp--;
850 	keyring = stack[sp].keyring;
851 	node = stack[sp].node;
852 	slot = stack[sp].slot + 1;
853 	kdebug("ascend to %d [%d]", keyring->serial, slot);
854 	goto ascend_to_node;
855 
856 	/* We found a viable match */
857 found:
858 	key = key_ref_to_ptr(ctx->result);
859 	key_check(key);
860 	if (!(ctx->flags & KEYRING_SEARCH_NO_UPDATE_TIME)) {
861 		key->last_used_at = ctx->now;
862 		keyring->last_used_at = ctx->now;
863 		while (sp > 0)
864 			stack[--sp].keyring->last_used_at = ctx->now;
865 	}
866 	kleave(" = true");
867 	return true;
868 }
869 
870 /**
871  * keyring_search_rcu - Search a keyring tree for a matching key under RCU
872  * @keyring_ref: A pointer to the keyring with possession indicator.
873  * @ctx: The keyring search context.
874  *
875  * Search the supplied keyring tree for a key that matches the criteria given.
876  * The root keyring and any linked keyrings must grant Search permission to the
877  * caller to be searchable and keys can only be found if they too grant Search
878  * to the caller. The possession flag on the root keyring pointer controls use
879  * of the possessor bits in permissions checking of the entire tree.  In
880  * addition, the LSM gets to forbid keyring searches and key matches.
881  *
882  * The search is performed as a breadth-then-depth search up to the prescribed
883  * limit (KEYRING_SEARCH_MAX_DEPTH).  The caller must hold the RCU read lock to
884  * prevent keyrings from being destroyed or rearranged whilst they are being
885  * searched.
886  *
887  * Keys are matched to the type provided and are then filtered by the match
888  * function, which is given the description to use in any way it sees fit.  The
889  * match function may use any attributes of a key that it wishes to
890  * determine the match.  Normally the match function from the key type would be
891  * used.
892  *
893  * RCU can be used to prevent the keyring key lists from disappearing without
894  * the need to take lots of locks.
895  *
896  * Returns a pointer to the found key and increments the key usage count if
897  * successful; -EAGAIN if no matching keys were found, or if expired or revoked
898  * keys were found; -ENOKEY if only negative keys were found; -ENOTDIR if the
899  * specified keyring wasn't a keyring.
900  *
901  * In the case of a successful return, the possession attribute from
902  * @keyring_ref is propagated to the returned key reference.
903  */
904 key_ref_t keyring_search_rcu(key_ref_t keyring_ref,
905 			     struct keyring_search_context *ctx)
906 {
907 	struct key *keyring;
908 	long err;
909 
910 	ctx->iterator = keyring_search_iterator;
911 	ctx->possessed = is_key_possessed(keyring_ref);
912 	ctx->result = ERR_PTR(-EAGAIN);
913 
914 	keyring = key_ref_to_ptr(keyring_ref);
915 	key_check(keyring);
916 
917 	if (keyring->type != &key_type_keyring)
918 		return ERR_PTR(-ENOTDIR);
919 
920 	if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM)) {
921 		err = key_task_permission(keyring_ref, ctx->cred, KEY_NEED_SEARCH);
922 		if (err < 0)
923 			return ERR_PTR(err);
924 	}
925 
926 	ctx->now = ktime_get_real_seconds();
927 	if (search_nested_keyrings(keyring, ctx))
928 		__key_get(key_ref_to_ptr(ctx->result));
929 	return ctx->result;
930 }
931 
932 /**
933  * keyring_search - Search the supplied keyring tree for a matching key
934  * @keyring: The root of the keyring tree to be searched.
935  * @type: The type of keyring we want to find.
936  * @description: The name of the keyring we want to find.
937  * @recurse: True to search the children of @keyring also
938  *
939  * As keyring_search_rcu() above, but using the current task's credentials and
940  * type's default matching function and preferred search method.
941  */
942 key_ref_t keyring_search(key_ref_t keyring,
943 			 struct key_type *type,
944 			 const char *description,
945 			 bool recurse)
946 {
947 	struct keyring_search_context ctx = {
948 		.index_key.type		= type,
949 		.index_key.description	= description,
950 		.index_key.desc_len	= strlen(description),
951 		.cred			= current_cred(),
952 		.match_data.cmp		= key_default_cmp,
953 		.match_data.raw_data	= description,
954 		.match_data.lookup_type	= KEYRING_SEARCH_LOOKUP_DIRECT,
955 		.flags			= KEYRING_SEARCH_DO_STATE_CHECK,
956 	};
957 	key_ref_t key;
958 	int ret;
959 
960 	if (recurse)
961 		ctx.flags |= KEYRING_SEARCH_RECURSE;
962 	if (type->match_preparse) {
963 		ret = type->match_preparse(&ctx.match_data);
964 		if (ret < 0)
965 			return ERR_PTR(ret);
966 	}
967 
968 	rcu_read_lock();
969 	key = keyring_search_rcu(keyring, &ctx);
970 	rcu_read_unlock();
971 
972 	if (type->match_free)
973 		type->match_free(&ctx.match_data);
974 	return key;
975 }
976 EXPORT_SYMBOL(keyring_search);
977 
978 static struct key_restriction *keyring_restriction_alloc(
979 	key_restrict_link_func_t check)
980 {
981 	struct key_restriction *keyres =
982 		kzalloc_obj(struct key_restriction);
983 
984 	if (!keyres)
985 		return ERR_PTR(-ENOMEM);
986 
987 	keyres->check = check;
988 
989 	return keyres;
990 }
991 
992 /*
993  * Semaphore to serialise restriction setup to prevent reference count
994  * cycles through restriction key pointers.
995  */
996 static DECLARE_RWSEM(keyring_serialise_restrict_sem);
997 
998 /*
999  * Check for restriction cycles that would prevent keyring garbage collection.
1000  * keyring_serialise_restrict_sem must be held.
1001  */
1002 static bool keyring_detect_restriction_cycle(const struct key *dest_keyring,
1003 					     struct key_restriction *keyres)
1004 {
1005 	while (keyres && keyres->key &&
1006 	       keyres->key->type == &key_type_keyring) {
1007 		if (keyres->key == dest_keyring)
1008 			return true;
1009 
1010 		keyres = keyres->key->restrict_link;
1011 	}
1012 
1013 	return false;
1014 }
1015 
1016 /**
1017  * keyring_restrict - Look up and apply a restriction to a keyring
1018  * @keyring_ref: The keyring to be restricted
1019  * @type: The key type that will provide the restriction checker.
1020  * @restriction: The restriction options to apply to the keyring
1021  *
1022  * Look up a keyring and apply a restriction to it.  The restriction is managed
1023  * by the specific key type, but can be configured by the options specified in
1024  * the restriction string.
1025  */
1026 int keyring_restrict(key_ref_t keyring_ref, const char *type,
1027 		     const char *restriction)
1028 {
1029 	struct key *keyring;
1030 	struct key_type *restrict_type = NULL;
1031 	struct key_restriction *restrict_link;
1032 	int ret = 0;
1033 
1034 	keyring = key_ref_to_ptr(keyring_ref);
1035 	key_check(keyring);
1036 
1037 	if (keyring->type != &key_type_keyring)
1038 		return -ENOTDIR;
1039 
1040 	if (!type) {
1041 		restrict_link = keyring_restriction_alloc(restrict_link_reject);
1042 	} else {
1043 		restrict_type = key_type_lookup(type);
1044 
1045 		if (IS_ERR(restrict_type))
1046 			return PTR_ERR(restrict_type);
1047 
1048 		if (!restrict_type->lookup_restriction) {
1049 			ret = -ENOENT;
1050 			goto error;
1051 		}
1052 
1053 		restrict_link = restrict_type->lookup_restriction(restriction);
1054 	}
1055 
1056 	if (IS_ERR(restrict_link)) {
1057 		ret = PTR_ERR(restrict_link);
1058 		goto error;
1059 	}
1060 
1061 	down_write(&keyring->sem);
1062 	down_write(&keyring_serialise_restrict_sem);
1063 
1064 	if (keyring->restrict_link) {
1065 		ret = -EEXIST;
1066 	} else if (keyring_detect_restriction_cycle(keyring, restrict_link)) {
1067 		ret = -EDEADLK;
1068 	} else {
1069 		keyring->restrict_link = restrict_link;
1070 		notify_key(keyring, NOTIFY_KEY_SETATTR, 0);
1071 	}
1072 
1073 	up_write(&keyring_serialise_restrict_sem);
1074 	up_write(&keyring->sem);
1075 
1076 	if (ret < 0) {
1077 		key_put(restrict_link->key);
1078 		kfree(restrict_link);
1079 	}
1080 
1081 error:
1082 	if (restrict_type)
1083 		key_type_put(restrict_type);
1084 
1085 	return ret;
1086 }
1087 EXPORT_SYMBOL(keyring_restrict);
1088 
1089 /*
1090  * Search the given keyring for a key that might be updated.
1091  *
1092  * The caller must guarantee that the keyring is a keyring and that the
1093  * permission is granted to modify the keyring as no check is made here.  The
1094  * caller must also hold a lock on the keyring semaphore.
1095  *
1096  * Returns a pointer to the found key with usage count incremented if
1097  * successful and returns NULL if not found.  Revoked and invalidated keys are
1098  * skipped over.
1099  *
1100  * If successful, the possession indicator is propagated from the keyring ref
1101  * to the returned key reference.
1102  */
1103 key_ref_t find_key_to_update(key_ref_t keyring_ref,
1104 			     const struct keyring_index_key *index_key)
1105 {
1106 	struct key *keyring, *key;
1107 	const void *object;
1108 
1109 	keyring = key_ref_to_ptr(keyring_ref);
1110 
1111 	kenter("{%d},{%s,%s}",
1112 	       keyring->serial, index_key->type->name, index_key->description);
1113 
1114 	guard(rcu)();
1115 	object = assoc_array_find(&keyring->keys, &keyring_assoc_array_ops,
1116 				  index_key);
1117 
1118 	if (object)
1119 		goto found;
1120 
1121 	kleave(" = NULL");
1122 	return NULL;
1123 
1124 found:
1125 	key = keyring_ptr_to_key(object);
1126 	if (key->flags & ((1 << KEY_FLAG_INVALIDATED) |
1127 			  (1 << KEY_FLAG_REVOKED))) {
1128 		kleave(" = NULL [x]");
1129 		return NULL;
1130 	}
1131 	__key_get(key);
1132 	kleave(" = {%d}", key->serial);
1133 	return make_key_ref(key, is_key_possessed(keyring_ref));
1134 }
1135 
1136 /*
1137  * Find a keyring with the specified name.
1138  *
1139  * Only keyrings that have nonzero refcount, are not revoked, and are owned by a
1140  * user in the current user namespace are considered.  If @uid_keyring is %true,
1141  * the keyring additionally must have been allocated as a user or user session
1142  * keyring; otherwise, it must grant Search permission directly to the caller.
1143  *
1144  * Returns a pointer to the keyring with the keyring's refcount having being
1145  * incremented on success.  -ENOKEY is returned if a key could not be found.
1146  */
1147 struct key *find_keyring_by_name(const char *name, bool uid_keyring)
1148 {
1149 	struct user_namespace *ns = current_user_ns();
1150 	struct key *keyring;
1151 
1152 	if (!name)
1153 		return ERR_PTR(-EINVAL);
1154 
1155 	read_lock(&keyring_name_lock);
1156 
1157 	/* Search this hash bucket for a keyring with a matching name that
1158 	 * grants Search permission and that hasn't been revoked
1159 	 */
1160 	list_for_each_entry(keyring, &ns->keyring_name_list, name_link) {
1161 		if (!kuid_has_mapping(ns, keyring->user->uid))
1162 			continue;
1163 
1164 		if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
1165 			continue;
1166 
1167 		if (strcmp(keyring->description, name) != 0)
1168 			continue;
1169 
1170 		if (uid_keyring) {
1171 			if (!test_bit(KEY_FLAG_UID_KEYRING,
1172 				      &keyring->flags))
1173 				continue;
1174 		} else {
1175 			if (key_permission(make_key_ref(keyring, 0),
1176 					   KEY_NEED_SEARCH) < 0)
1177 				continue;
1178 		}
1179 
1180 		/* we've got a match but we might end up racing with
1181 		 * key_cleanup() if the keyring is currently 'dead'
1182 		 * (ie. it has a zero usage count) */
1183 		if (!refcount_inc_not_zero(&keyring->usage))
1184 			continue;
1185 		keyring->last_used_at = ktime_get_real_seconds();
1186 		goto out;
1187 	}
1188 
1189 	keyring = ERR_PTR(-ENOKEY);
1190 out:
1191 	read_unlock(&keyring_name_lock);
1192 	return keyring;
1193 }
1194 
1195 static int keyring_detect_cycle_iterator(const void *object,
1196 					 void *iterator_data)
1197 {
1198 	struct keyring_search_context *ctx = iterator_data;
1199 	const struct key *key = keyring_ptr_to_key(object);
1200 
1201 	kenter("{%d}", key->serial);
1202 
1203 	/* We might get a keyring with matching index-key that is nonetheless a
1204 	 * different keyring. */
1205 	if (key != ctx->match_data.raw_data)
1206 		return 0;
1207 
1208 	ctx->result = ERR_PTR(-EDEADLK);
1209 	return 1;
1210 }
1211 
1212 /*
1213  * See if a cycle will be created by inserting acyclic tree B in acyclic
1214  * tree A at the topmost level (ie: as a direct child of A).
1215  *
1216  * Since we are adding B to A at the top level, checking for cycles should just
1217  * be a matter of seeing if node A is somewhere in tree B.
1218  */
1219 static int keyring_detect_cycle(struct key *A, struct key *B)
1220 {
1221 	struct keyring_search_context ctx = {
1222 		.index_key		= A->index_key,
1223 		.match_data.raw_data	= A,
1224 		.match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
1225 		.iterator		= keyring_detect_cycle_iterator,
1226 		.flags			= (KEYRING_SEARCH_NO_STATE_CHECK |
1227 					   KEYRING_SEARCH_NO_UPDATE_TIME |
1228 					   KEYRING_SEARCH_NO_CHECK_PERM |
1229 					   KEYRING_SEARCH_DETECT_TOO_DEEP |
1230 					   KEYRING_SEARCH_RECURSE),
1231 	};
1232 
1233 	rcu_read_lock();
1234 	search_nested_keyrings(B, &ctx);
1235 	rcu_read_unlock();
1236 	return PTR_ERR(ctx.result) == -EAGAIN ? 0 : PTR_ERR(ctx.result);
1237 }
1238 
1239 /*
1240  * Lock keyring for link.
1241  */
1242 int __key_link_lock(struct key *keyring,
1243 		    const struct keyring_index_key *index_key)
1244 	__acquires(&keyring->sem)
1245 	__acquires(&keyring_serialise_link_lock)
1246 {
1247 	if (keyring->type != &key_type_keyring)
1248 		return -ENOTDIR;
1249 
1250 	down_write(&keyring->sem);
1251 
1252 	/* Serialise link/link calls to prevent parallel calls causing a cycle
1253 	 * when linking two keyring in opposite orders.
1254 	 */
1255 	if (index_key->type == &key_type_keyring)
1256 		mutex_lock(&keyring_serialise_link_lock);
1257 
1258 	return 0;
1259 }
1260 
1261 /*
1262  * Lock keyrings for move (link/unlink combination).
1263  */
1264 int __key_move_lock(struct key *l_keyring, struct key *u_keyring,
1265 		    const struct keyring_index_key *index_key)
1266 	__acquires(&l_keyring->sem)
1267 	__acquires(&u_keyring->sem)
1268 	__acquires(&keyring_serialise_link_lock)
1269 {
1270 	if (l_keyring->type != &key_type_keyring ||
1271 	    u_keyring->type != &key_type_keyring)
1272 		return -ENOTDIR;
1273 
1274 	/* We have to be very careful here to take the keyring locks in the
1275 	 * right order, lest we open ourselves to deadlocking against another
1276 	 * move operation.
1277 	 */
1278 	if (l_keyring < u_keyring) {
1279 		down_write(&l_keyring->sem);
1280 		down_write_nested(&u_keyring->sem, 1);
1281 	} else {
1282 		down_write(&u_keyring->sem);
1283 		down_write_nested(&l_keyring->sem, 1);
1284 	}
1285 
1286 	/* Serialise link/link calls to prevent parallel calls causing a cycle
1287 	 * when linking two keyring in opposite orders.
1288 	 */
1289 	if (index_key->type == &key_type_keyring)
1290 		mutex_lock(&keyring_serialise_link_lock);
1291 
1292 	return 0;
1293 }
1294 
1295 /*
1296  * Preallocate memory so that a key can be linked into to a keyring.
1297  */
1298 int __key_link_begin(struct key *keyring,
1299 		     const struct keyring_index_key *index_key,
1300 		     struct assoc_array_edit **_edit)
1301 {
1302 	struct assoc_array_edit *edit;
1303 	int ret;
1304 
1305 	kenter("%d,%s,%s,",
1306 	       keyring->serial, index_key->type->name, index_key->description);
1307 
1308 	BUG_ON(index_key->desc_len == 0);
1309 	BUG_ON(*_edit != NULL);
1310 
1311 	*_edit = NULL;
1312 
1313 	ret = -EKEYREVOKED;
1314 	if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
1315 		goto error;
1316 
1317 	/* Create an edit script that will insert/replace the key in the
1318 	 * keyring tree.
1319 	 */
1320 	edit = assoc_array_insert(&keyring->keys,
1321 				  &keyring_assoc_array_ops,
1322 				  index_key,
1323 				  NULL);
1324 	if (IS_ERR(edit)) {
1325 		ret = PTR_ERR(edit);
1326 		goto error;
1327 	}
1328 
1329 	/* If we're not replacing a link in-place then we're going to need some
1330 	 * extra quota.
1331 	 */
1332 	if (!edit->dead_leaf) {
1333 		ret = key_payload_reserve(keyring,
1334 					  keyring->datalen + KEYQUOTA_LINK_BYTES);
1335 		if (ret < 0)
1336 			goto error_cancel;
1337 	}
1338 
1339 	*_edit = edit;
1340 	kleave(" = 0");
1341 	return 0;
1342 
1343 error_cancel:
1344 	assoc_array_cancel_edit(edit);
1345 error:
1346 	kleave(" = %d", ret);
1347 	return ret;
1348 }
1349 
1350 /*
1351  * Check already instantiated keys aren't going to be a problem.
1352  *
1353  * The caller must have called __key_link_begin(). Don't need to call this for
1354  * keys that were created since __key_link_begin() was called.
1355  */
1356 int __key_link_check_live_key(struct key *keyring, struct key *key)
1357 {
1358 	if (key->type == &key_type_keyring)
1359 		/* check that we aren't going to create a cycle by linking one
1360 		 * keyring to another */
1361 		return keyring_detect_cycle(keyring, key);
1362 	return 0;
1363 }
1364 
1365 /*
1366  * Link a key into to a keyring.
1367  *
1368  * Must be called with __key_link_begin() having being called.  Discards any
1369  * already extant link to matching key if there is one, so that each keyring
1370  * holds at most one link to any given key of a particular type+description
1371  * combination.
1372  */
1373 void __key_link(struct key *keyring, struct key *key,
1374 		struct assoc_array_edit **_edit)
1375 {
1376 	__key_get(key);
1377 	assoc_array_insert_set_object(*_edit, keyring_key_to_ptr(key));
1378 	assoc_array_apply_edit(*_edit);
1379 	*_edit = NULL;
1380 	notify_key(keyring, NOTIFY_KEY_LINKED, key_serial(key));
1381 }
1382 
1383 /*
1384  * Finish linking a key into to a keyring.
1385  *
1386  * Must be called with __key_link_begin() having being called.
1387  */
1388 void __key_link_end(struct key *keyring,
1389 		    const struct keyring_index_key *index_key,
1390 		    struct assoc_array_edit *edit)
1391 	__releases(&keyring->sem)
1392 	__releases(&keyring_serialise_link_lock)
1393 {
1394 	BUG_ON(index_key->type == NULL);
1395 	kenter("%d,%s,", keyring->serial, index_key->type->name);
1396 
1397 	if (edit) {
1398 		if (!edit->dead_leaf) {
1399 			key_payload_reserve(keyring,
1400 				keyring->datalen - KEYQUOTA_LINK_BYTES);
1401 		}
1402 		assoc_array_cancel_edit(edit);
1403 	}
1404 	up_write(&keyring->sem);
1405 
1406 	if (index_key->type == &key_type_keyring)
1407 		mutex_unlock(&keyring_serialise_link_lock);
1408 }
1409 
1410 /*
1411  * Check addition of keys to restricted keyrings.
1412  */
1413 static int __key_link_check_restriction(struct key *keyring, struct key *key)
1414 {
1415 	if (!keyring->restrict_link || !keyring->restrict_link->check)
1416 		return 0;
1417 	return keyring->restrict_link->check(keyring, key->type, &key->payload,
1418 					     keyring->restrict_link->key);
1419 }
1420 
1421 /**
1422  * key_link - Link a key to a keyring
1423  * @keyring: The keyring to make the link in.
1424  * @key: The key to link to.
1425  *
1426  * Make a link in a keyring to a key, such that the keyring holds a reference
1427  * on that key and the key can potentially be found by searching that keyring.
1428  *
1429  * This function will write-lock the keyring's semaphore and will consume some
1430  * of the user's key data quota to hold the link.
1431  *
1432  * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring,
1433  * -EKEYREVOKED if the keyring has been revoked, -ENFILE if the keyring is
1434  * full, -EDQUOT if there is insufficient key data quota remaining to add
1435  * another link or -ENOMEM if there's insufficient memory.
1436  *
1437  * It is assumed that the caller has checked that it is permitted for a link to
1438  * be made (the keyring should have Write permission and the key Link
1439  * permission).
1440  */
1441 int key_link(struct key *keyring, struct key *key)
1442 {
1443 	struct assoc_array_edit *edit = NULL;
1444 	int ret;
1445 
1446 	kenter("{%d,%d}", keyring->serial, refcount_read(&keyring->usage));
1447 
1448 	key_check(keyring);
1449 	key_check(key);
1450 
1451 	ret = __key_link_lock(keyring, &key->index_key);
1452 	if (ret < 0)
1453 		goto error;
1454 
1455 	ret = __key_link_begin(keyring, &key->index_key, &edit);
1456 	if (ret < 0)
1457 		goto error_end;
1458 
1459 	kdebug("begun {%d,%d}", keyring->serial, refcount_read(&keyring->usage));
1460 	ret = __key_link_check_restriction(keyring, key);
1461 	if (ret == 0)
1462 		ret = __key_link_check_live_key(keyring, key);
1463 	if (ret == 0)
1464 		__key_link(keyring, key, &edit);
1465 
1466 error_end:
1467 	__key_link_end(keyring, &key->index_key, edit);
1468 error:
1469 	kleave(" = %d {%d,%d}", ret, keyring->serial, refcount_read(&keyring->usage));
1470 	return ret;
1471 }
1472 EXPORT_SYMBOL(key_link);
1473 
1474 /*
1475  * Lock a keyring for unlink.
1476  */
1477 static int __key_unlink_lock(struct key *keyring)
1478 	__acquires(&keyring->sem)
1479 {
1480 	if (keyring->type != &key_type_keyring)
1481 		return -ENOTDIR;
1482 
1483 	down_write(&keyring->sem);
1484 	return 0;
1485 }
1486 
1487 /*
1488  * Begin the process of unlinking a key from a keyring.
1489  */
1490 static int __key_unlink_begin(struct key *keyring, struct key *key,
1491 			      struct assoc_array_edit **_edit)
1492 {
1493 	struct assoc_array_edit *edit;
1494 
1495 	BUG_ON(*_edit != NULL);
1496 
1497 	edit = assoc_array_delete(&keyring->keys, &keyring_assoc_array_ops,
1498 				  &key->index_key);
1499 	if (IS_ERR(edit))
1500 		return PTR_ERR(edit);
1501 
1502 	if (!edit)
1503 		return -ENOENT;
1504 
1505 	*_edit = edit;
1506 	return 0;
1507 }
1508 
1509 /*
1510  * Apply an unlink change.
1511  */
1512 static void __key_unlink(struct key *keyring, struct key *key,
1513 			 struct assoc_array_edit **_edit)
1514 {
1515 	assoc_array_apply_edit(*_edit);
1516 	notify_key(keyring, NOTIFY_KEY_UNLINKED, key_serial(key));
1517 	*_edit = NULL;
1518 	key_payload_reserve(keyring, keyring->datalen - KEYQUOTA_LINK_BYTES);
1519 }
1520 
1521 /*
1522  * Finish unlinking a key from to a keyring.
1523  */
1524 static void __key_unlink_end(struct key *keyring,
1525 			     struct key *key,
1526 			     struct assoc_array_edit *edit)
1527 	__releases(&keyring->sem)
1528 {
1529 	if (edit)
1530 		assoc_array_cancel_edit(edit);
1531 	up_write(&keyring->sem);
1532 }
1533 
1534 /**
1535  * key_unlink - Unlink the first link to a key from a keyring.
1536  * @keyring: The keyring to remove the link from.
1537  * @key: The key the link is to.
1538  *
1539  * Remove a link from a keyring to a key.
1540  *
1541  * This function will write-lock the keyring's semaphore.
1542  *
1543  * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring, -ENOENT if
1544  * the key isn't linked to by the keyring or -ENOMEM if there's insufficient
1545  * memory.
1546  *
1547  * It is assumed that the caller has checked that it is permitted for a link to
1548  * be removed (the keyring should have Write permission; no permissions are
1549  * required on the key).
1550  */
1551 int key_unlink(struct key *keyring, struct key *key)
1552 {
1553 	struct assoc_array_edit *edit = NULL;
1554 	int ret;
1555 
1556 	key_check(keyring);
1557 	key_check(key);
1558 
1559 	ret = __key_unlink_lock(keyring);
1560 	if (ret < 0)
1561 		return ret;
1562 
1563 	ret = __key_unlink_begin(keyring, key, &edit);
1564 	if (ret == 0)
1565 		__key_unlink(keyring, key, &edit);
1566 	__key_unlink_end(keyring, key, edit);
1567 	return ret;
1568 }
1569 EXPORT_SYMBOL(key_unlink);
1570 
1571 /**
1572  * key_move - Move a key from one keyring to another
1573  * @key: The key to move
1574  * @from_keyring: The keyring to remove the link from.
1575  * @to_keyring: The keyring to make the link in.
1576  * @flags: Qualifying flags, such as KEYCTL_MOVE_EXCL.
1577  *
1578  * Make a link in @to_keyring to a key, such that the keyring holds a reference
1579  * on that key and the key can potentially be found by searching that keyring
1580  * whilst simultaneously removing a link to the key from @from_keyring.
1581  *
1582  * This function will write-lock both keyring's semaphores and will consume
1583  * some of the user's key data quota to hold the link on @to_keyring.
1584  *
1585  * Returns 0 if successful, -ENOTDIR if either keyring isn't a keyring,
1586  * -EKEYREVOKED if either keyring has been revoked, -ENFILE if the second
1587  * keyring is full, -EDQUOT if there is insufficient key data quota remaining
1588  * to add another link or -ENOMEM if there's insufficient memory.  If
1589  * KEYCTL_MOVE_EXCL is set, then -EEXIST will be returned if there's already a
1590  * matching key in @to_keyring.
1591  *
1592  * It is assumed that the caller has checked that it is permitted for a link to
1593  * be made (the keyring should have Write permission and the key Link
1594  * permission).
1595  */
1596 int key_move(struct key *key,
1597 	     struct key *from_keyring,
1598 	     struct key *to_keyring,
1599 	     unsigned int flags)
1600 {
1601 	struct assoc_array_edit *from_edit = NULL, *to_edit = NULL;
1602 	int ret;
1603 
1604 	kenter("%d,%d,%d", key->serial, from_keyring->serial, to_keyring->serial);
1605 
1606 	if (from_keyring == to_keyring)
1607 		return 0;
1608 
1609 	key_check(key);
1610 	key_check(from_keyring);
1611 	key_check(to_keyring);
1612 
1613 	ret = __key_move_lock(from_keyring, to_keyring, &key->index_key);
1614 	if (ret < 0)
1615 		goto out;
1616 	ret = __key_unlink_begin(from_keyring, key, &from_edit);
1617 	if (ret < 0)
1618 		goto error;
1619 	ret = __key_link_begin(to_keyring, &key->index_key, &to_edit);
1620 	if (ret < 0)
1621 		goto error;
1622 
1623 	ret = -EEXIST;
1624 	if (to_edit->dead_leaf && (flags & KEYCTL_MOVE_EXCL))
1625 		goto error;
1626 
1627 	ret = __key_link_check_restriction(to_keyring, key);
1628 	if (ret < 0)
1629 		goto error;
1630 	ret = __key_link_check_live_key(to_keyring, key);
1631 	if (ret < 0)
1632 		goto error;
1633 
1634 	__key_unlink(from_keyring, key, &from_edit);
1635 	__key_link(to_keyring, key, &to_edit);
1636 error:
1637 	__key_link_end(to_keyring, &key->index_key, to_edit);
1638 	__key_unlink_end(from_keyring, key, from_edit);
1639 out:
1640 	kleave(" = %d", ret);
1641 	return ret;
1642 }
1643 EXPORT_SYMBOL(key_move);
1644 
1645 /**
1646  * keyring_clear - Clear a keyring
1647  * @keyring: The keyring to clear.
1648  *
1649  * Clear the contents of the specified keyring.
1650  *
1651  * Returns 0 if successful or -ENOTDIR if the keyring isn't a keyring.
1652  */
1653 int keyring_clear(struct key *keyring)
1654 {
1655 	struct assoc_array_edit *edit;
1656 	int ret;
1657 
1658 	if (keyring->type != &key_type_keyring)
1659 		return -ENOTDIR;
1660 
1661 	down_write(&keyring->sem);
1662 
1663 	edit = assoc_array_clear(&keyring->keys, &keyring_assoc_array_ops);
1664 	if (IS_ERR(edit)) {
1665 		ret = PTR_ERR(edit);
1666 	} else {
1667 		if (edit)
1668 			assoc_array_apply_edit(edit);
1669 		notify_key(keyring, NOTIFY_KEY_CLEARED, 0);
1670 		key_payload_reserve(keyring, 0);
1671 		ret = 0;
1672 	}
1673 
1674 	up_write(&keyring->sem);
1675 	return ret;
1676 }
1677 EXPORT_SYMBOL(keyring_clear);
1678 
1679 /*
1680  * Dispose of the links from a revoked keyring.
1681  *
1682  * This is called with the key sem write-locked.
1683  */
1684 static void keyring_revoke(struct key *keyring)
1685 {
1686 	struct assoc_array_edit *edit;
1687 
1688 	edit = assoc_array_clear(&keyring->keys, &keyring_assoc_array_ops);
1689 	if (!IS_ERR(edit)) {
1690 		if (edit)
1691 			assoc_array_apply_edit(edit);
1692 		key_payload_reserve(keyring, 0);
1693 	}
1694 }
1695 
1696 static bool keyring_gc_select_iterator(void *object, void *iterator_data)
1697 {
1698 	struct key *key = keyring_ptr_to_key(object);
1699 	time64_t *limit = iterator_data;
1700 
1701 	if (key_is_dead(key, *limit))
1702 		return false;
1703 	key_get(key);
1704 	return true;
1705 }
1706 
1707 static int keyring_gc_check_iterator(const void *object, void *iterator_data)
1708 {
1709 	const struct key *key = keyring_ptr_to_key(object);
1710 	time64_t *limit = iterator_data;
1711 
1712 	key_check(key);
1713 	return key_is_dead(key, *limit);
1714 }
1715 
1716 /*
1717  * Garbage collect pointers from a keyring.
1718  *
1719  * Not called with any locks held.  The keyring's key struct will not be
1720  * deallocated under us as only our caller may deallocate it.
1721  */
1722 void keyring_gc(struct key *keyring, time64_t limit)
1723 {
1724 	int result;
1725 
1726 	kenter("%x{%s}", keyring->serial, keyring->description ?: "");
1727 
1728 	if (keyring->flags & ((1 << KEY_FLAG_INVALIDATED) |
1729 			      (1 << KEY_FLAG_REVOKED)))
1730 		goto dont_gc;
1731 
1732 	/* scan the keyring looking for dead keys */
1733 	rcu_read_lock();
1734 	result = assoc_array_iterate(&keyring->keys,
1735 				     keyring_gc_check_iterator, &limit);
1736 	rcu_read_unlock();
1737 	if (result == true)
1738 		goto do_gc;
1739 
1740 dont_gc:
1741 	kleave(" [no gc]");
1742 	return;
1743 
1744 do_gc:
1745 	down_write(&keyring->sem);
1746 	assoc_array_gc(&keyring->keys, &keyring_assoc_array_ops,
1747 		       keyring_gc_select_iterator, &limit);
1748 	up_write(&keyring->sem);
1749 	kleave(" [gc]");
1750 }
1751 
1752 /*
1753  * Garbage collect restriction pointers from a keyring.
1754  *
1755  * Keyring restrictions are associated with a key type, and must be cleaned
1756  * up if the key type is unregistered. The restriction is altered to always
1757  * reject additional keys so a keyring cannot be opened up by unregistering
1758  * a key type.
1759  *
1760  * Not called with any keyring locks held. The keyring's key struct will not
1761  * be deallocated under us as only our caller may deallocate it.
1762  *
1763  * The caller is required to hold key_types_sem and dead_type->sem. This is
1764  * fulfilled by key_gc_keytype() holding the locks on behalf of
1765  * key_garbage_collector(), which it invokes on a workqueue.
1766  */
1767 void keyring_restriction_gc(struct key *keyring, struct key_type *dead_type)
1768 {
1769 	struct key_restriction *keyres;
1770 
1771 	kenter("%x{%s}", keyring->serial, keyring->description ?: "");
1772 
1773 	/*
1774 	 * keyring->restrict_link is only assigned at key allocation time
1775 	 * or with the key type locked, so the only values that could be
1776 	 * concurrently assigned to keyring->restrict_link are for key
1777 	 * types other than dead_type. Given this, it's ok to check
1778 	 * the key type before acquiring keyring->sem.
1779 	 */
1780 	if (!dead_type || !keyring->restrict_link ||
1781 	    keyring->restrict_link->keytype != dead_type) {
1782 		kleave(" [no restriction gc]");
1783 		return;
1784 	}
1785 
1786 	/* Lock the keyring to ensure that a link is not in progress */
1787 	down_write(&keyring->sem);
1788 
1789 	keyres = keyring->restrict_link;
1790 
1791 	keyres->check = restrict_link_reject;
1792 
1793 	key_put(keyres->key);
1794 	keyres->key = NULL;
1795 	keyres->keytype = NULL;
1796 
1797 	up_write(&keyring->sem);
1798 
1799 	kleave(" [restriction gc]");
1800 }
1801