3 * Copyright (C) 2004-2005, 2008, 2013 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #include <linux/export.h>
13 #include <linux/init.h>
14 #include <linux/sched.h>
15 #include <linux/slab.h>
16 #include <linux/security.h>
17 #include <linux/seq_file.h>
18 #include <linux/err.h>
19 #include <linux/user_namespace.h>
20 #include <linux/nsproxy.h>
21 #include <keys/keyring-type.h>
22 #include <keys/user-type.h>
23 #include <linux/assoc_array_priv.h>
24 #include <linux/uaccess.h>
25 #include <net/net_namespace.h>
29 * When plumbing the depths of the key tree, this sets a hard limit
30 * set on how deep we're willing to go.
32 #define KEYRING_SEARCH_MAX_DEPTH 6
35 * We mark pointers we pass to the associative array with bit 1 set if
36 * they're keyrings and clear otherwise.
38 #define KEYRING_PTR_SUBTYPE 0x2UL
40 static inline bool keyring_ptr_is_keyring(const struct assoc_array_ptr *x)
42 return (unsigned long)x & KEYRING_PTR_SUBTYPE;
44 static inline struct key *keyring_ptr_to_key(const struct assoc_array_ptr *x)
46 void *object = assoc_array_ptr_to_leaf(x);
47 return (struct key *)((unsigned long)object & ~KEYRING_PTR_SUBTYPE);
49 static inline void *keyring_key_to_ptr(struct key *key)
51 if (key->type == &key_type_keyring)
52 return (void *)((unsigned long)key | KEYRING_PTR_SUBTYPE);
56 static DEFINE_RWLOCK(keyring_name_lock);
59 * Clean up the bits of user_namespace that belong to us.
61 void key_free_user_ns(struct user_namespace *ns)
63 write_lock(&keyring_name_lock);
64 list_del_init(&ns->keyring_name_list);
65 write_unlock(&keyring_name_lock);
67 key_put(ns->user_keyring_register);
68 #ifdef CONFIG_PERSISTENT_KEYRINGS
69 key_put(ns->persistent_keyring_register);
74 * The keyring key type definition. Keyrings are simply keys of this type and
75 * can be treated as ordinary keys in addition to having their own special
78 static int keyring_preparse(struct key_preparsed_payload *prep);
79 static void keyring_free_preparse(struct key_preparsed_payload *prep);
80 static int keyring_instantiate(struct key *keyring,
81 struct key_preparsed_payload *prep);
82 static void keyring_revoke(struct key *keyring);
83 static void keyring_destroy(struct key *keyring);
84 static void keyring_describe(const struct key *keyring, struct seq_file *m);
85 static long keyring_read(const struct key *keyring,
86 char __user *buffer, size_t buflen);
88 struct key_type key_type_keyring = {
91 .preparse = keyring_preparse,
92 .free_preparse = keyring_free_preparse,
93 .instantiate = keyring_instantiate,
94 .revoke = keyring_revoke,
95 .destroy = keyring_destroy,
96 .describe = keyring_describe,
99 EXPORT_SYMBOL(key_type_keyring);
102 * Semaphore to serialise link/link calls to prevent two link calls in parallel
103 * introducing a cycle.
105 static DEFINE_MUTEX(keyring_serialise_link_lock);
108 * Publish the name of a keyring so that it can be found by name (if it has
109 * one and it doesn't begin with a dot).
111 static void keyring_publish_name(struct key *keyring)
113 struct user_namespace *ns = current_user_ns();
115 if (keyring->description &&
116 keyring->description[0] &&
117 keyring->description[0] != '.') {
118 write_lock(&keyring_name_lock);
119 list_add_tail(&keyring->name_link, &ns->keyring_name_list);
120 write_unlock(&keyring_name_lock);
125 * Preparse a keyring payload
127 static int keyring_preparse(struct key_preparsed_payload *prep)
129 return prep->datalen != 0 ? -EINVAL : 0;
133 * Free a preparse of a user defined key payload
135 static void keyring_free_preparse(struct key_preparsed_payload *prep)
140 * Initialise a keyring.
142 * Returns 0 on success, -EINVAL if given any data.
144 static int keyring_instantiate(struct key *keyring,
145 struct key_preparsed_payload *prep)
147 assoc_array_init(&keyring->keys);
148 /* make the keyring available by name if it has one */
149 keyring_publish_name(keyring);
154 * Multiply 64-bits by 32-bits to 96-bits and fold back to 64-bit. Ideally we'd
155 * fold the carry back too, but that requires inline asm.
157 static u64 mult_64x32_and_fold(u64 x, u32 y)
159 u64 hi = (u64)(u32)(x >> 32) * y;
160 u64 lo = (u64)(u32)(x) * y;
161 return lo + ((u64)(u32)hi << 32) + (u32)(hi >> 32);
165 * Hash a key type and description.
167 static void hash_key_type_and_desc(struct keyring_index_key *index_key)
169 const unsigned level_shift = ASSOC_ARRAY_LEVEL_STEP;
170 const unsigned long fan_mask = ASSOC_ARRAY_FAN_MASK;
171 const char *description = index_key->description;
172 unsigned long hash, type;
175 int n, desc_len = index_key->desc_len;
177 type = (unsigned long)index_key->type;
178 acc = mult_64x32_and_fold(type, desc_len + 13);
179 acc = mult_64x32_and_fold(acc, 9207);
180 piece = (unsigned long)index_key->domain_tag;
181 acc = mult_64x32_and_fold(acc, piece);
182 acc = mult_64x32_and_fold(acc, 9207);
191 memcpy(&piece, description, n);
194 acc = mult_64x32_and_fold(acc, piece);
195 acc = mult_64x32_and_fold(acc, 9207);
198 /* Fold the hash down to 32 bits if need be. */
200 if (ASSOC_ARRAY_KEY_CHUNK_SIZE == 32)
203 /* Squidge all the keyrings into a separate part of the tree to
204 * ordinary keys by making sure the lowest level segment in the hash is
205 * zero for keyrings and non-zero otherwise.
207 if (index_key->type != &key_type_keyring && (hash & fan_mask) == 0)
208 hash |= (hash >> (ASSOC_ARRAY_KEY_CHUNK_SIZE - level_shift)) | 1;
209 else if (index_key->type == &key_type_keyring && (hash & fan_mask) != 0)
210 hash = (hash + (hash << level_shift)) & ~fan_mask;
211 index_key->hash = hash;
215 * Finalise an index key to include a part of the description actually in the
216 * index key, to set the domain tag and to calculate the hash.
218 void key_set_index_key(struct keyring_index_key *index_key)
220 static struct key_tag default_domain_tag = { .usage = REFCOUNT_INIT(1), };
221 size_t n = min_t(size_t, index_key->desc_len, sizeof(index_key->desc));
223 memcpy(index_key->desc, index_key->description, n);
225 if (!index_key->domain_tag) {
226 if (index_key->type->flags & KEY_TYPE_NET_DOMAIN)
227 index_key->domain_tag = current->nsproxy->net_ns->key_domain;
229 index_key->domain_tag = &default_domain_tag;
232 hash_key_type_and_desc(index_key);
236 * key_put_tag - Release a ref on a tag.
237 * @tag: The tag to release.
239 * This releases a reference the given tag and returns true if that ref was the
242 bool key_put_tag(struct key_tag *tag)
244 if (refcount_dec_and_test(&tag->usage)) {
253 * key_remove_domain - Kill off a key domain and gc its keys
254 * @domain_tag: The domain tag to release.
256 * This marks a domain tag as being dead and releases a ref on it. If that
257 * wasn't the last reference, the garbage collector is poked to try and delete
258 * all keys that were in the domain.
260 void key_remove_domain(struct key_tag *domain_tag)
262 domain_tag->removed = true;
263 if (!key_put_tag(domain_tag))
264 key_schedule_gc_links();
268 * Build the next index key chunk.
270 * We return it one word-sized chunk at a time.
272 static unsigned long keyring_get_key_chunk(const void *data, int level)
274 const struct keyring_index_key *index_key = data;
275 unsigned long chunk = 0;
277 int desc_len = index_key->desc_len, n = sizeof(chunk);
279 level /= ASSOC_ARRAY_KEY_CHUNK_SIZE;
282 return index_key->hash;
286 return (unsigned long)index_key->type;
288 return (unsigned long)index_key->domain_tag;
291 if (desc_len <= sizeof(index_key->desc))
294 d = index_key->description + sizeof(index_key->desc);
295 d += level * sizeof(long);
296 desc_len -= sizeof(index_key->desc);
302 } while (--desc_len > 0);
307 static unsigned long keyring_get_object_key_chunk(const void *object, int level)
309 const struct key *key = keyring_ptr_to_key(object);
310 return keyring_get_key_chunk(&key->index_key, level);
313 static bool keyring_compare_object(const void *object, const void *data)
315 const struct keyring_index_key *index_key = data;
316 const struct key *key = keyring_ptr_to_key(object);
318 return key->index_key.type == index_key->type &&
319 key->index_key.domain_tag == index_key->domain_tag &&
320 key->index_key.desc_len == index_key->desc_len &&
321 memcmp(key->index_key.description, index_key->description,
322 index_key->desc_len) == 0;
326 * Compare the index keys of a pair of objects and determine the bit position
327 * at which they differ - if they differ.
329 static int keyring_diff_objects(const void *object, const void *data)
331 const struct key *key_a = keyring_ptr_to_key(object);
332 const struct keyring_index_key *a = &key_a->index_key;
333 const struct keyring_index_key *b = data;
334 unsigned long seg_a, seg_b;
340 if ((seg_a ^ seg_b) != 0)
342 level += ASSOC_ARRAY_KEY_CHUNK_SIZE / 8;
344 /* The number of bits contributed by the hash is controlled by a
345 * constant in the assoc_array headers. Everything else thereafter we
346 * can deal with as being machine word-size dependent.
350 if ((seg_a ^ seg_b) != 0)
352 level += sizeof(unsigned long);
354 /* The next bit may not work on big endian */
355 seg_a = (unsigned long)a->type;
356 seg_b = (unsigned long)b->type;
357 if ((seg_a ^ seg_b) != 0)
359 level += sizeof(unsigned long);
361 seg_a = (unsigned long)a->domain_tag;
362 seg_b = (unsigned long)b->domain_tag;
363 if ((seg_a ^ seg_b) != 0)
365 level += sizeof(unsigned long);
368 if (a->desc_len <= i)
371 for (; i < a->desc_len; i++) {
372 seg_a = *(unsigned char *)(a->description + i);
373 seg_b = *(unsigned char *)(b->description + i);
374 if ((seg_a ^ seg_b) != 0)
384 i = level * 8 + __ffs(seg_a ^ seg_b);
389 * Free an object after stripping the keyring flag off of the pointer.
391 static void keyring_free_object(void *object)
393 key_put(keyring_ptr_to_key(object));
397 * Operations for keyring management by the index-tree routines.
399 static const struct assoc_array_ops keyring_assoc_array_ops = {
400 .get_key_chunk = keyring_get_key_chunk,
401 .get_object_key_chunk = keyring_get_object_key_chunk,
402 .compare_object = keyring_compare_object,
403 .diff_objects = keyring_diff_objects,
404 .free_object = keyring_free_object,
408 * Clean up a keyring when it is destroyed. Unpublish its name if it had one
409 * and dispose of its data.
411 * The garbage collector detects the final key_put(), removes the keyring from
412 * the serial number tree and then does RCU synchronisation before coming here,
413 * so we shouldn't need to worry about code poking around here with the RCU
414 * readlock held by this time.
416 static void keyring_destroy(struct key *keyring)
418 if (keyring->description) {
419 write_lock(&keyring_name_lock);
421 if (keyring->name_link.next != NULL &&
422 !list_empty(&keyring->name_link))
423 list_del(&keyring->name_link);
425 write_unlock(&keyring_name_lock);
428 if (keyring->restrict_link) {
429 struct key_restriction *keyres = keyring->restrict_link;
431 key_put(keyres->key);
435 assoc_array_destroy(&keyring->keys, &keyring_assoc_array_ops);
439 * Describe a keyring for /proc.
441 static void keyring_describe(const struct key *keyring, struct seq_file *m)
443 if (keyring->description)
444 seq_puts(m, keyring->description);
446 seq_puts(m, "[anon]");
448 if (key_is_positive(keyring)) {
449 if (keyring->keys.nr_leaves_on_tree != 0)
450 seq_printf(m, ": %lu", keyring->keys.nr_leaves_on_tree);
452 seq_puts(m, ": empty");
456 struct keyring_read_iterator_context {
459 key_serial_t __user *buffer;
462 static int keyring_read_iterator(const void *object, void *data)
464 struct keyring_read_iterator_context *ctx = data;
465 const struct key *key = keyring_ptr_to_key(object);
468 kenter("{%s,%d},,{%zu/%zu}",
469 key->type->name, key->serial, ctx->count, ctx->buflen);
471 if (ctx->count >= ctx->buflen)
474 ret = put_user(key->serial, ctx->buffer);
478 ctx->count += sizeof(key->serial);
483 * Read a list of key IDs from the keyring's contents in binary form
485 * The keyring's semaphore is read-locked by the caller. This prevents someone
486 * from modifying it under us - which could cause us to read key IDs multiple
489 static long keyring_read(const struct key *keyring,
490 char __user *buffer, size_t buflen)
492 struct keyring_read_iterator_context ctx;
495 kenter("{%d},,%zu", key_serial(keyring), buflen);
497 if (buflen & (sizeof(key_serial_t) - 1))
500 /* Copy as many key IDs as fit into the buffer */
501 if (buffer && buflen) {
502 ctx.buffer = (key_serial_t __user *)buffer;
505 ret = assoc_array_iterate(&keyring->keys,
506 keyring_read_iterator, &ctx);
508 kleave(" = %ld [iterate]", ret);
513 /* Return the size of the buffer needed */
514 ret = keyring->keys.nr_leaves_on_tree * sizeof(key_serial_t);
516 kleave("= %ld [ok]", ret);
518 kleave("= %ld [buffer too small]", ret);
523 * keyring_alloc - Allocate a keyring and link into the destination
524 * @description: The key description to allow the key to be searched out.
525 * @uid: The owner of the new key.
526 * @gid: The group ID for the new key's group permissions.
527 * @cred: The credentials specifying UID namespace.
528 * @acl: The ACL to attach to the new key.
529 * @flags: Flags specifying quota properties.
530 * @restrict_link: Optional link restriction for new keyrings.
531 * @dest: Destination keyring.
533 struct key *keyring_alloc(const char *description, kuid_t uid, kgid_t gid,
534 const struct cred *cred, struct key_acl *acl,
536 struct key_restriction *restrict_link,
542 keyring = key_alloc(&key_type_keyring, description,
543 uid, gid, cred, acl, flags, restrict_link);
544 if (!IS_ERR(keyring)) {
545 ret = key_instantiate_and_link(keyring, NULL, 0, dest, NULL);
548 keyring = ERR_PTR(ret);
554 EXPORT_SYMBOL(keyring_alloc);
557 * restrict_link_reject - Give -EPERM to restrict link
558 * @keyring: The keyring being added to.
559 * @type: The type of key being added.
560 * @payload: The payload of the key intended to be added.
561 * @restriction_key: Keys providing additional data for evaluating restriction.
563 * Reject the addition of any links to a keyring. It can be overridden by
564 * passing KEY_ALLOC_BYPASS_RESTRICTION to key_instantiate_and_link() when
565 * adding a key to a keyring.
567 * This is meant to be stored in a key_restriction structure which is passed
568 * in the restrict_link parameter to keyring_alloc().
570 int restrict_link_reject(struct key *keyring,
571 const struct key_type *type,
572 const union key_payload *payload,
573 struct key *restriction_key)
579 * By default, we keys found by getting an exact match on their descriptions.
581 bool key_default_cmp(const struct key *key,
582 const struct key_match_data *match_data)
584 return strcmp(key->description, match_data->raw_data) == 0;
588 * Iteration function to consider each key found.
590 static int keyring_search_iterator(const void *object, void *iterator_data)
592 struct keyring_search_context *ctx = iterator_data;
593 const struct key *key = keyring_ptr_to_key(object);
594 unsigned long kflags = READ_ONCE(key->flags);
595 short state = READ_ONCE(key->state);
597 kenter("{%d}", key->serial);
599 /* ignore keys not of this type */
600 if (key->type != ctx->index_key.type) {
601 kleave(" = 0 [!type]");
605 /* skip invalidated, revoked and expired keys */
606 if (ctx->flags & KEYRING_SEARCH_DO_STATE_CHECK) {
607 time64_t expiry = READ_ONCE(key->expiry);
609 if (kflags & ((1 << KEY_FLAG_INVALIDATED) |
610 (1 << KEY_FLAG_REVOKED))) {
611 ctx->result = ERR_PTR(-EKEYREVOKED);
612 kleave(" = %d [invrev]", ctx->skipped_ret);
616 if (expiry && ctx->now >= expiry) {
617 if (!(ctx->flags & KEYRING_SEARCH_SKIP_EXPIRED))
618 ctx->result = ERR_PTR(-EKEYEXPIRED);
619 kleave(" = %d [expire]", ctx->skipped_ret);
624 /* keys that don't match */
625 if (!ctx->match_data.cmp(key, &ctx->match_data)) {
626 kleave(" = 0 [!match]");
630 /* key must have search permissions */
631 if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM) &&
632 key_task_permission(make_key_ref(key, ctx->possessed),
633 ctx->cred, KEY_NEED_SEARCH) < 0) {
634 ctx->result = ERR_PTR(-EACCES);
635 kleave(" = %d [!perm]", ctx->skipped_ret);
639 if (ctx->flags & KEYRING_SEARCH_DO_STATE_CHECK) {
640 /* we set a different error code if we pass a negative key */
642 ctx->result = ERR_PTR(state);
643 kleave(" = %d [neg]", ctx->skipped_ret);
649 ctx->result = make_key_ref(key, ctx->possessed);
650 kleave(" = 1 [found]");
654 return ctx->skipped_ret;
658 * Search inside a keyring for a key. We can search by walking to it
659 * directly based on its index-key or we can iterate over the entire
660 * tree looking for it, based on the match function.
662 static int search_keyring(struct key *keyring, struct keyring_search_context *ctx)
664 if (ctx->match_data.lookup_type == KEYRING_SEARCH_LOOKUP_DIRECT) {
667 object = assoc_array_find(&keyring->keys,
668 &keyring_assoc_array_ops,
670 return object ? ctx->iterator(object, ctx) : 0;
672 return assoc_array_iterate(&keyring->keys, ctx->iterator, ctx);
676 * Search a tree of keyrings that point to other keyrings up to the maximum
679 static bool search_nested_keyrings(struct key *keyring,
680 struct keyring_search_context *ctx)
684 struct assoc_array_node *node;
686 } stack[KEYRING_SEARCH_MAX_DEPTH];
688 struct assoc_array_shortcut *shortcut;
689 struct assoc_array_node *node;
690 struct assoc_array_ptr *ptr;
694 kenter("{%d},{%s,%s}",
696 ctx->index_key.type->name,
697 ctx->index_key.description);
699 #define STATE_CHECKS (KEYRING_SEARCH_NO_STATE_CHECK | KEYRING_SEARCH_DO_STATE_CHECK)
700 BUG_ON((ctx->flags & STATE_CHECKS) == 0 ||
701 (ctx->flags & STATE_CHECKS) == STATE_CHECKS);
703 if (ctx->index_key.description)
704 key_set_index_key(&ctx->index_key);
706 /* Check to see if this top-level keyring is what we are looking for
707 * and whether it is valid or not.
709 if (ctx->match_data.lookup_type == KEYRING_SEARCH_LOOKUP_ITERATE ||
710 keyring_compare_object(keyring, &ctx->index_key)) {
711 ctx->skipped_ret = 2;
712 switch (ctx->iterator(keyring_key_to_ptr(keyring), ctx)) {
722 ctx->skipped_ret = 0;
724 /* Start processing a new keyring */
726 kdebug("descend to %d", keyring->serial);
727 if (keyring->flags & ((1 << KEY_FLAG_INVALIDATED) |
728 (1 << KEY_FLAG_REVOKED)))
729 goto not_this_keyring;
731 /* Search through the keys in this keyring before its searching its
734 if (search_keyring(keyring, ctx))
737 /* Then manually iterate through the keyrings nested in this one.
739 * Start from the root node of the index tree. Because of the way the
740 * hash function has been set up, keyrings cluster on the leftmost
741 * branch of the root node (root slot 0) or in the root node itself.
742 * Non-keyrings avoid the leftmost branch of the root entirely (root
745 if (!(ctx->flags & KEYRING_SEARCH_RECURSE))
746 goto not_this_keyring;
748 ptr = READ_ONCE(keyring->keys.root);
750 goto not_this_keyring;
752 if (assoc_array_ptr_is_shortcut(ptr)) {
753 /* If the root is a shortcut, either the keyring only contains
754 * keyring pointers (everything clusters behind root slot 0) or
755 * doesn't contain any keyring pointers.
757 shortcut = assoc_array_ptr_to_shortcut(ptr);
758 if ((shortcut->index_key[0] & ASSOC_ARRAY_FAN_MASK) != 0)
759 goto not_this_keyring;
761 ptr = READ_ONCE(shortcut->next_node);
762 node = assoc_array_ptr_to_node(ptr);
766 node = assoc_array_ptr_to_node(ptr);
767 ptr = node->slots[0];
768 if (!assoc_array_ptr_is_meta(ptr))
772 /* Descend to a more distal node in this keyring's content tree and go
776 if (assoc_array_ptr_is_shortcut(ptr)) {
777 shortcut = assoc_array_ptr_to_shortcut(ptr);
778 ptr = READ_ONCE(shortcut->next_node);
779 BUG_ON(!assoc_array_ptr_is_node(ptr));
781 node = assoc_array_ptr_to_node(ptr);
784 kdebug("begin_node");
787 /* Go through the slots in a node */
788 for (; slot < ASSOC_ARRAY_FAN_OUT; slot++) {
789 ptr = READ_ONCE(node->slots[slot]);
791 if (assoc_array_ptr_is_meta(ptr) && node->back_pointer)
792 goto descend_to_node;
794 if (!keyring_ptr_is_keyring(ptr))
797 key = keyring_ptr_to_key(ptr);
799 if (sp >= KEYRING_SEARCH_MAX_DEPTH) {
800 if (ctx->flags & KEYRING_SEARCH_DETECT_TOO_DEEP) {
801 ctx->result = ERR_PTR(-ELOOP);
804 goto not_this_keyring;
807 /* Search a nested keyring */
808 if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM) &&
809 key_task_permission(make_key_ref(key, ctx->possessed),
810 ctx->cred, KEY_NEED_SEARCH) < 0)
813 /* stack the current position */
814 stack[sp].keyring = keyring;
815 stack[sp].node = node;
816 stack[sp].slot = slot;
819 /* begin again with the new keyring */
821 goto descend_to_keyring;
824 /* We've dealt with all the slots in the current node, so now we need
825 * to ascend to the parent and continue processing there.
827 ptr = READ_ONCE(node->back_pointer);
828 slot = node->parent_slot;
830 if (ptr && assoc_array_ptr_is_shortcut(ptr)) {
831 shortcut = assoc_array_ptr_to_shortcut(ptr);
832 ptr = READ_ONCE(shortcut->back_pointer);
833 slot = shortcut->parent_slot;
836 goto not_this_keyring;
837 node = assoc_array_ptr_to_node(ptr);
840 /* If we've ascended to the root (zero backpointer), we must have just
841 * finished processing the leftmost branch rather than the root slots -
842 * so there can't be any more keyrings for us to find.
844 if (node->back_pointer) {
845 kdebug("ascend %d", slot);
849 /* The keyring we're looking at was disqualified or didn't contain a
853 kdebug("not_this_keyring %d", sp);
859 /* Resume the processing of a keyring higher up in the tree */
861 keyring = stack[sp].keyring;
862 node = stack[sp].node;
863 slot = stack[sp].slot + 1;
864 kdebug("ascend to %d [%d]", keyring->serial, slot);
867 /* We found a viable match */
869 key = key_ref_to_ptr(ctx->result);
871 if (!(ctx->flags & KEYRING_SEARCH_NO_UPDATE_TIME)) {
872 key->last_used_at = ctx->now;
873 keyring->last_used_at = ctx->now;
875 stack[--sp].keyring->last_used_at = ctx->now;
882 * keyring_search_rcu - Search a keyring tree for a matching key under RCU
883 * @keyring_ref: A pointer to the keyring with possession indicator.
884 * @ctx: The keyring search context.
886 * Search the supplied keyring tree for a key that matches the criteria given.
887 * The root keyring and any linked keyrings must grant Search permission to the
888 * caller to be searchable and keys can only be found if they too grant Search
889 * to the caller. The possession flag on the root keyring pointer controls use
890 * of the possessor bits in permissions checking of the entire tree. In
891 * addition, the LSM gets to forbid keyring searches and key matches.
893 * The search is performed as a breadth-then-depth search up to the prescribed
894 * limit (KEYRING_SEARCH_MAX_DEPTH). The caller must hold the RCU read lock to
895 * prevent keyrings from being destroyed or rearranged whilst they are being
898 * Keys are matched to the type provided and are then filtered by the match
899 * function, which is given the description to use in any way it sees fit. The
900 * match function may use any attributes of a key that it wishes to to
901 * determine the match. Normally the match function from the key type would be
904 * RCU can be used to prevent the keyring key lists from disappearing without
905 * the need to take lots of locks.
907 * Returns a pointer to the found key and increments the key usage count if
908 * successful; -EAGAIN if no matching keys were found, or if expired or revoked
909 * keys were found; -ENOKEY if only negative keys were found; -ENOTDIR if the
910 * specified keyring wasn't a keyring.
912 * In the case of a successful return, the possession attribute from
913 * @keyring_ref is propagated to the returned key reference.
915 key_ref_t keyring_search_rcu(key_ref_t keyring_ref,
916 struct keyring_search_context *ctx)
921 ctx->iterator = keyring_search_iterator;
922 ctx->possessed = is_key_possessed(keyring_ref);
923 ctx->result = ERR_PTR(-EAGAIN);
925 keyring = key_ref_to_ptr(keyring_ref);
928 if (keyring->type != &key_type_keyring)
929 return ERR_PTR(-ENOTDIR);
931 if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM)) {
932 err = key_task_permission(keyring_ref, ctx->cred, KEY_NEED_SEARCH);
937 ctx->now = ktime_get_real_seconds();
938 if (search_nested_keyrings(keyring, ctx))
939 __key_get(key_ref_to_ptr(ctx->result));
944 * keyring_search - Search the supplied keyring tree for a matching key
945 * @keyring: The root of the keyring tree to be searched.
946 * @type: The type of keyring we want to find.
947 * @description: The name of the keyring we want to find.
948 * @recurse: True to search the children of @keyring also
950 * As keyring_search_rcu() above, but using the current task's credentials and
951 * type's default matching function and preferred search method.
953 key_ref_t keyring_search(key_ref_t keyring,
954 struct key_type *type,
955 const char *description,
958 struct keyring_search_context ctx = {
959 .index_key.type = type,
960 .index_key.description = description,
961 .index_key.desc_len = strlen(description),
962 .cred = current_cred(),
963 .match_data.cmp = key_default_cmp,
964 .match_data.raw_data = description,
965 .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
966 .flags = KEYRING_SEARCH_DO_STATE_CHECK,
972 ctx.flags |= KEYRING_SEARCH_RECURSE;
973 if (type->match_preparse) {
974 ret = type->match_preparse(&ctx.match_data);
980 key = keyring_search_rcu(keyring, &ctx);
983 if (type->match_free)
984 type->match_free(&ctx.match_data);
987 EXPORT_SYMBOL(keyring_search);
989 static struct key_restriction *keyring_restriction_alloc(
990 key_restrict_link_func_t check)
992 struct key_restriction *keyres =
993 kzalloc(sizeof(struct key_restriction), GFP_KERNEL);
996 return ERR_PTR(-ENOMEM);
998 keyres->check = check;
1004 * Semaphore to serialise restriction setup to prevent reference count
1005 * cycles through restriction key pointers.
1007 static DECLARE_RWSEM(keyring_serialise_restrict_sem);
1010 * Check for restriction cycles that would prevent keyring garbage collection.
1011 * keyring_serialise_restrict_sem must be held.
1013 static bool keyring_detect_restriction_cycle(const struct key *dest_keyring,
1014 struct key_restriction *keyres)
1016 while (keyres && keyres->key &&
1017 keyres->key->type == &key_type_keyring) {
1018 if (keyres->key == dest_keyring)
1021 keyres = keyres->key->restrict_link;
1028 * keyring_restrict - Look up and apply a restriction to a keyring
1029 * @keyring_ref: The keyring to be restricted
1030 * @type: The key type that will provide the restriction checker.
1031 * @restriction: The restriction options to apply to the keyring
1033 * Look up a keyring and apply a restriction to it. The restriction is managed
1034 * by the specific key type, but can be configured by the options specified in
1035 * the restriction string.
1037 int keyring_restrict(key_ref_t keyring_ref, const char *type,
1038 const char *restriction)
1040 struct key *keyring;
1041 struct key_type *restrict_type = NULL;
1042 struct key_restriction *restrict_link;
1045 keyring = key_ref_to_ptr(keyring_ref);
1048 if (keyring->type != &key_type_keyring)
1052 restrict_link = keyring_restriction_alloc(restrict_link_reject);
1054 restrict_type = key_type_lookup(type);
1056 if (IS_ERR(restrict_type))
1057 return PTR_ERR(restrict_type);
1059 if (!restrict_type->lookup_restriction) {
1064 restrict_link = restrict_type->lookup_restriction(restriction);
1067 if (IS_ERR(restrict_link)) {
1068 ret = PTR_ERR(restrict_link);
1072 down_write(&keyring->sem);
1073 down_write(&keyring_serialise_restrict_sem);
1075 if (keyring->restrict_link)
1077 else if (keyring_detect_restriction_cycle(keyring, restrict_link))
1080 keyring->restrict_link = restrict_link;
1082 up_write(&keyring_serialise_restrict_sem);
1083 up_write(&keyring->sem);
1086 key_put(restrict_link->key);
1087 kfree(restrict_link);
1092 key_type_put(restrict_type);
1096 EXPORT_SYMBOL(keyring_restrict);
1099 * Search the given keyring for a key that might be updated.
1101 * The caller must guarantee that the keyring is a keyring and that the
1102 * permission is granted to modify the keyring as no check is made here. The
1103 * caller must also hold a lock on the keyring semaphore.
1105 * Returns a pointer to the found key with usage count incremented if
1106 * successful and returns NULL if not found. Revoked and invalidated keys are
1109 * If successful, the possession indicator is propagated from the keyring ref
1110 * to the returned key reference.
1112 key_ref_t find_key_to_update(key_ref_t keyring_ref,
1113 const struct keyring_index_key *index_key)
1115 struct key *keyring, *key;
1118 keyring = key_ref_to_ptr(keyring_ref);
1120 kenter("{%d},{%s,%s}",
1121 keyring->serial, index_key->type->name, index_key->description);
1123 object = assoc_array_find(&keyring->keys, &keyring_assoc_array_ops,
1133 key = keyring_ptr_to_key(object);
1134 if (key->flags & ((1 << KEY_FLAG_INVALIDATED) |
1135 (1 << KEY_FLAG_REVOKED))) {
1136 kleave(" = NULL [x]");
1140 kleave(" = {%d}", key->serial);
1141 return make_key_ref(key, is_key_possessed(keyring_ref));
1145 * Find a keyring with the specified name.
1147 * Only keyrings that have nonzero refcount, are not revoked, and are owned by
1148 * a user in the current user namespace are considered. If @uid_keyring is
1149 * %true, the keyring additionally must have been allocated as a user or user
1150 * session keyring; otherwise, it must grant JOIN permission directly to the
1151 * caller (ie. not through possession).
1153 * Returns a pointer to the keyring with the keyring's refcount having being
1154 * incremented on success. -ENOKEY is returned if a key could not be found.
1156 struct key *find_keyring_by_name(const char *name, bool uid_keyring)
1158 struct user_namespace *ns = current_user_ns();
1159 struct key *keyring;
1162 return ERR_PTR(-EINVAL);
1164 read_lock(&keyring_name_lock);
1166 /* Search this hash bucket for a keyring with a matching name that
1167 * grants Search permission and that hasn't been revoked
1169 list_for_each_entry(keyring, &ns->keyring_name_list, name_link) {
1170 if (!kuid_has_mapping(ns, keyring->user->uid))
1173 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
1176 if (strcmp(keyring->description, name) != 0)
1180 if (!test_bit(KEY_FLAG_UID_KEYRING,
1184 if (key_permission(make_key_ref(keyring, 0),
1189 /* we've got a match but we might end up racing with
1190 * key_cleanup() if the keyring is currently 'dead'
1191 * (ie. it has a zero usage count) */
1192 if (!refcount_inc_not_zero(&keyring->usage))
1194 keyring->last_used_at = ktime_get_real_seconds();
1198 keyring = ERR_PTR(-ENOKEY);
1200 read_unlock(&keyring_name_lock);
1204 static int keyring_detect_cycle_iterator(const void *object,
1205 void *iterator_data)
1207 struct keyring_search_context *ctx = iterator_data;
1208 const struct key *key = keyring_ptr_to_key(object);
1210 kenter("{%d}", key->serial);
1212 /* We might get a keyring with matching index-key that is nonetheless a
1213 * different keyring. */
1214 if (key != ctx->match_data.raw_data)
1217 ctx->result = ERR_PTR(-EDEADLK);
1222 * See if a cycle will will be created by inserting acyclic tree B in acyclic
1223 * tree A at the topmost level (ie: as a direct child of A).
1225 * Since we are adding B to A at the top level, checking for cycles should just
1226 * be a matter of seeing if node A is somewhere in tree B.
1228 static int keyring_detect_cycle(struct key *A, struct key *B)
1230 struct keyring_search_context ctx = {
1231 .index_key = A->index_key,
1232 .match_data.raw_data = A,
1233 .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
1234 .iterator = keyring_detect_cycle_iterator,
1235 .flags = (KEYRING_SEARCH_NO_STATE_CHECK |
1236 KEYRING_SEARCH_NO_UPDATE_TIME |
1237 KEYRING_SEARCH_NO_CHECK_PERM |
1238 KEYRING_SEARCH_DETECT_TOO_DEEP |
1239 KEYRING_SEARCH_RECURSE),
1243 search_nested_keyrings(B, &ctx);
1245 return PTR_ERR(ctx.result) == -EAGAIN ? 0 : PTR_ERR(ctx.result);
1249 * Lock keyring for link.
1251 int __key_link_lock(struct key *keyring,
1252 const struct keyring_index_key *index_key)
1253 __acquires(&keyring->sem)
1254 __acquires(&keyring_serialise_link_lock)
1256 if (keyring->type != &key_type_keyring)
1259 down_write(&keyring->sem);
1261 /* Serialise link/link calls to prevent parallel calls causing a cycle
1262 * when linking two keyring in opposite orders.
1264 if (index_key->type == &key_type_keyring)
1265 mutex_lock(&keyring_serialise_link_lock);
1271 * Lock keyrings for move (link/unlink combination).
1273 int __key_move_lock(struct key *l_keyring, struct key *u_keyring,
1274 const struct keyring_index_key *index_key)
1275 __acquires(&l_keyring->sem)
1276 __acquires(&u_keyring->sem)
1277 __acquires(&keyring_serialise_link_lock)
1279 if (l_keyring->type != &key_type_keyring ||
1280 u_keyring->type != &key_type_keyring)
1283 /* We have to be very careful here to take the keyring locks in the
1284 * right order, lest we open ourselves to deadlocking against another
1287 if (l_keyring < u_keyring) {
1288 down_write(&l_keyring->sem);
1289 down_write_nested(&u_keyring->sem, 1);
1291 down_write(&u_keyring->sem);
1292 down_write_nested(&l_keyring->sem, 1);
1295 /* Serialise link/link calls to prevent parallel calls causing a cycle
1296 * when linking two keyring in opposite orders.
1298 if (index_key->type == &key_type_keyring)
1299 mutex_lock(&keyring_serialise_link_lock);
1305 * Preallocate memory so that a key can be linked into to a keyring.
1307 int __key_link_begin(struct key *keyring,
1308 const struct keyring_index_key *index_key,
1309 struct assoc_array_edit **_edit)
1311 struct assoc_array_edit *edit;
1315 keyring->serial, index_key->type->name, index_key->description);
1317 BUG_ON(index_key->desc_len == 0);
1318 BUG_ON(*_edit != NULL);
1323 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
1326 /* Create an edit script that will insert/replace the key in the
1329 edit = assoc_array_insert(&keyring->keys,
1330 &keyring_assoc_array_ops,
1334 ret = PTR_ERR(edit);
1338 /* If we're not replacing a link in-place then we're going to need some
1341 if (!edit->dead_leaf) {
1342 ret = key_payload_reserve(keyring,
1343 keyring->datalen + KEYQUOTA_LINK_BYTES);
1353 assoc_array_cancel_edit(edit);
1355 kleave(" = %d", ret);
1360 * Check already instantiated keys aren't going to be a problem.
1362 * The caller must have called __key_link_begin(). Don't need to call this for
1363 * keys that were created since __key_link_begin() was called.
1365 int __key_link_check_live_key(struct key *keyring, struct key *key)
1367 if (key->type == &key_type_keyring)
1368 /* check that we aren't going to create a cycle by linking one
1369 * keyring to another */
1370 return keyring_detect_cycle(keyring, key);
1375 * Link a key into to a keyring.
1377 * Must be called with __key_link_begin() having being called. Discards any
1378 * already extant link to matching key if there is one, so that each keyring
1379 * holds at most one link to any given key of a particular type+description
1382 void __key_link(struct key *key, struct assoc_array_edit **_edit)
1385 assoc_array_insert_set_object(*_edit, keyring_key_to_ptr(key));
1386 assoc_array_apply_edit(*_edit);
1391 * Finish linking a key into to a keyring.
1393 * Must be called with __key_link_begin() having being called.
1395 void __key_link_end(struct key *keyring,
1396 const struct keyring_index_key *index_key,
1397 struct assoc_array_edit *edit)
1398 __releases(&keyring->sem)
1399 __releases(&keyring_serialise_link_lock)
1401 BUG_ON(index_key->type == NULL);
1402 kenter("%d,%s,", keyring->serial, index_key->type->name);
1405 if (!edit->dead_leaf) {
1406 key_payload_reserve(keyring,
1407 keyring->datalen - KEYQUOTA_LINK_BYTES);
1409 assoc_array_cancel_edit(edit);
1411 up_write(&keyring->sem);
1413 if (index_key->type == &key_type_keyring)
1414 mutex_unlock(&keyring_serialise_link_lock);
1418 * Check addition of keys to restricted keyrings.
1420 static int __key_link_check_restriction(struct key *keyring, struct key *key)
1422 if (!keyring->restrict_link || !keyring->restrict_link->check)
1424 return keyring->restrict_link->check(keyring, key->type, &key->payload,
1425 keyring->restrict_link->key);
1429 * key_link - Link a key to a keyring
1430 * @keyring: The keyring to make the link in.
1431 * @key: The key to link to.
1433 * Make a link in a keyring to a key, such that the keyring holds a reference
1434 * on that key and the key can potentially be found by searching that keyring.
1436 * This function will write-lock the keyring's semaphore and will consume some
1437 * of the user's key data quota to hold the link.
1439 * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring,
1440 * -EKEYREVOKED if the keyring has been revoked, -ENFILE if the keyring is
1441 * full, -EDQUOT if there is insufficient key data quota remaining to add
1442 * another link or -ENOMEM if there's insufficient memory.
1444 * It is assumed that the caller has checked that it is permitted for a link to
1445 * be made (the keyring should have Write permission and the key Link
1448 int key_link(struct key *keyring, struct key *key)
1450 struct assoc_array_edit *edit = NULL;
1453 kenter("{%d,%d}", keyring->serial, refcount_read(&keyring->usage));
1458 ret = __key_link_lock(keyring, &key->index_key);
1462 ret = __key_link_begin(keyring, &key->index_key, &edit);
1466 kdebug("begun {%d,%d}", keyring->serial, refcount_read(&keyring->usage));
1467 ret = __key_link_check_restriction(keyring, key);
1469 ret = __key_link_check_live_key(keyring, key);
1471 __key_link(key, &edit);
1474 __key_link_end(keyring, &key->index_key, edit);
1476 kleave(" = %d {%d,%d}", ret, keyring->serial, refcount_read(&keyring->usage));
1479 EXPORT_SYMBOL(key_link);
1482 * Lock a keyring for unlink.
1484 static int __key_unlink_lock(struct key *keyring)
1485 __acquires(&keyring->sem)
1487 if (keyring->type != &key_type_keyring)
1490 down_write(&keyring->sem);
1495 * Begin the process of unlinking a key from a keyring.
1497 static int __key_unlink_begin(struct key *keyring, struct key *key,
1498 struct assoc_array_edit **_edit)
1500 struct assoc_array_edit *edit;
1502 BUG_ON(*_edit != NULL);
1504 edit = assoc_array_delete(&keyring->keys, &keyring_assoc_array_ops,
1507 return PTR_ERR(edit);
1517 * Apply an unlink change.
1519 static void __key_unlink(struct key *keyring, struct key *key,
1520 struct assoc_array_edit **_edit)
1522 assoc_array_apply_edit(*_edit);
1524 key_payload_reserve(keyring, keyring->datalen - KEYQUOTA_LINK_BYTES);
1528 * Finish unlinking a key from to a keyring.
1530 static void __key_unlink_end(struct key *keyring,
1532 struct assoc_array_edit *edit)
1533 __releases(&keyring->sem)
1536 assoc_array_cancel_edit(edit);
1537 up_write(&keyring->sem);
1541 * key_unlink - Unlink the first link to a key from a keyring.
1542 * @keyring: The keyring to remove the link from.
1543 * @key: The key the link is to.
1545 * Remove a link from a keyring to a key.
1547 * This function will write-lock the keyring's semaphore.
1549 * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring, -ENOENT if
1550 * the key isn't linked to by the keyring or -ENOMEM if there's insufficient
1553 * It is assumed that the caller has checked that it is permitted for a link to
1554 * be removed (the keyring should have Write permission; no permissions are
1555 * required on the key).
1557 int key_unlink(struct key *keyring, struct key *key)
1559 struct assoc_array_edit *edit = NULL;
1565 ret = __key_unlink_lock(keyring);
1569 ret = __key_unlink_begin(keyring, key, &edit);
1571 __key_unlink(keyring, key, &edit);
1572 __key_unlink_end(keyring, key, edit);
1575 EXPORT_SYMBOL(key_unlink);
1578 * key_move - Move a key from one keyring to another
1579 * @key: The key to move
1580 * @from_keyring: The keyring to remove the link from.
1581 * @to_keyring: The keyring to make the link in.
1582 * @flags: Qualifying flags, such as KEYCTL_MOVE_EXCL.
1584 * Make a link in @to_keyring to a key, such that the keyring holds a reference
1585 * on that key and the key can potentially be found by searching that keyring
1586 * whilst simultaneously removing a link to the key from @from_keyring.
1588 * This function will write-lock both keyring's semaphores and will consume
1589 * some of the user's key data quota to hold the link on @to_keyring.
1591 * Returns 0 if successful, -ENOTDIR if either keyring isn't a keyring,
1592 * -EKEYREVOKED if either keyring has been revoked, -ENFILE if the second
1593 * keyring is full, -EDQUOT if there is insufficient key data quota remaining
1594 * to add another link or -ENOMEM if there's insufficient memory. If
1595 * KEYCTL_MOVE_EXCL is set, then -EEXIST will be returned if there's already a
1596 * matching key in @to_keyring.
1598 * It is assumed that the caller has checked that it is permitted for a link to
1599 * be made (the keyring should have Write permission and the key Link
1602 int key_move(struct key *key,
1603 struct key *from_keyring,
1604 struct key *to_keyring,
1607 struct assoc_array_edit *from_edit = NULL, *to_edit = NULL;
1610 kenter("%d,%d,%d", key->serial, from_keyring->serial, to_keyring->serial);
1612 if (from_keyring == to_keyring)
1616 key_check(from_keyring);
1617 key_check(to_keyring);
1619 ret = __key_move_lock(from_keyring, to_keyring, &key->index_key);
1622 ret = __key_unlink_begin(from_keyring, key, &from_edit);
1625 ret = __key_link_begin(to_keyring, &key->index_key, &to_edit);
1630 if (to_edit->dead_leaf && (flags & KEYCTL_MOVE_EXCL))
1633 ret = __key_link_check_restriction(to_keyring, key);
1636 ret = __key_link_check_live_key(to_keyring, key);
1640 __key_unlink(from_keyring, key, &from_edit);
1641 __key_link(key, &to_edit);
1643 __key_link_end(to_keyring, &key->index_key, to_edit);
1644 __key_unlink_end(from_keyring, key, from_edit);
1646 kleave(" = %d", ret);
1649 EXPORT_SYMBOL(key_move);
1652 * keyring_clear - Clear a keyring
1653 * @keyring: The keyring to clear.
1655 * Clear the contents of the specified keyring.
1657 * Returns 0 if successful or -ENOTDIR if the keyring isn't a keyring.
1659 int keyring_clear(struct key *keyring)
1661 struct assoc_array_edit *edit;
1664 if (keyring->type != &key_type_keyring)
1667 down_write(&keyring->sem);
1669 edit = assoc_array_clear(&keyring->keys, &keyring_assoc_array_ops);
1671 ret = PTR_ERR(edit);
1674 assoc_array_apply_edit(edit);
1675 key_payload_reserve(keyring, 0);
1679 up_write(&keyring->sem);
1682 EXPORT_SYMBOL(keyring_clear);
1685 * Dispose of the links from a revoked keyring.
1687 * This is called with the key sem write-locked.
1689 static void keyring_revoke(struct key *keyring)
1691 struct assoc_array_edit *edit;
1693 edit = assoc_array_clear(&keyring->keys, &keyring_assoc_array_ops);
1694 if (!IS_ERR(edit)) {
1696 assoc_array_apply_edit(edit);
1697 key_payload_reserve(keyring, 0);
1701 static bool keyring_gc_select_iterator(void *object, void *iterator_data)
1703 struct key *key = keyring_ptr_to_key(object);
1704 time64_t *limit = iterator_data;
1706 if (key_is_dead(key, *limit))
1712 static int keyring_gc_check_iterator(const void *object, void *iterator_data)
1714 const struct key *key = keyring_ptr_to_key(object);
1715 time64_t *limit = iterator_data;
1718 return key_is_dead(key, *limit);
1722 * Garbage collect pointers from a keyring.
1724 * Not called with any locks held. The keyring's key struct will not be
1725 * deallocated under us as only our caller may deallocate it.
1727 void keyring_gc(struct key *keyring, time64_t limit)
1731 kenter("%x{%s}", keyring->serial, keyring->description ?: "");
1733 if (keyring->flags & ((1 << KEY_FLAG_INVALIDATED) |
1734 (1 << KEY_FLAG_REVOKED)))
1737 /* scan the keyring looking for dead keys */
1739 result = assoc_array_iterate(&keyring->keys,
1740 keyring_gc_check_iterator, &limit);
1750 down_write(&keyring->sem);
1751 assoc_array_gc(&keyring->keys, &keyring_assoc_array_ops,
1752 keyring_gc_select_iterator, &limit);
1753 up_write(&keyring->sem);
1758 * Garbage collect restriction pointers from a keyring.
1760 * Keyring restrictions are associated with a key type, and must be cleaned
1761 * up if the key type is unregistered. The restriction is altered to always
1762 * reject additional keys so a keyring cannot be opened up by unregistering
1765 * Not called with any keyring locks held. The keyring's key struct will not
1766 * be deallocated under us as only our caller may deallocate it.
1768 * The caller is required to hold key_types_sem and dead_type->sem. This is
1769 * fulfilled by key_gc_keytype() holding the locks on behalf of
1770 * key_garbage_collector(), which it invokes on a workqueue.
1772 void keyring_restriction_gc(struct key *keyring, struct key_type *dead_type)
1774 struct key_restriction *keyres;
1776 kenter("%x{%s}", keyring->serial, keyring->description ?: "");
1779 * keyring->restrict_link is only assigned at key allocation time
1780 * or with the key type locked, so the only values that could be
1781 * concurrently assigned to keyring->restrict_link are for key
1782 * types other than dead_type. Given this, it's ok to check
1783 * the key type before acquiring keyring->sem.
1785 if (!dead_type || !keyring->restrict_link ||
1786 keyring->restrict_link->keytype != dead_type) {
1787 kleave(" [no restriction gc]");
1791 /* Lock the keyring to ensure that a link is not in progress */
1792 down_write(&keyring->sem);
1794 keyres = keyring->restrict_link;
1796 keyres->check = restrict_link_reject;
1798 key_put(keyres->key);
1800 keyres->keytype = NULL;
1802 up_write(&keyring->sem);
1804 kleave(" [restriction gc]");