1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) 2004-2005, 2008, 2013 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
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 <keys/keyring-type.h>
16 #include <keys/user-type.h>
17 #include <linux/assoc_array_priv.h>
18 #include <linux/uaccess.h>
22 * When plumbing the depths of the key tree, this sets a hard limit
23 * set on how deep we're willing to go.
25 #define KEYRING_SEARCH_MAX_DEPTH 6
28 * We keep all named keyrings in a hash to speed looking them up.
30 #define KEYRING_NAME_HASH_SIZE (1 << 5)
33 * We mark pointers we pass to the associative array with bit 1 set if
34 * they're keyrings and clear otherwise.
36 #define KEYRING_PTR_SUBTYPE 0x2UL
38 static inline bool keyring_ptr_is_keyring(const struct assoc_array_ptr *x)
40 return (unsigned long)x & KEYRING_PTR_SUBTYPE;
42 static inline struct key *keyring_ptr_to_key(const struct assoc_array_ptr *x)
44 void *object = assoc_array_ptr_to_leaf(x);
45 return (struct key *)((unsigned long)object & ~KEYRING_PTR_SUBTYPE);
47 static inline void *keyring_key_to_ptr(struct key *key)
49 if (key->type == &key_type_keyring)
50 return (void *)((unsigned long)key | KEYRING_PTR_SUBTYPE);
54 static struct list_head keyring_name_hash[KEYRING_NAME_HASH_SIZE];
55 static DEFINE_RWLOCK(keyring_name_lock);
57 static inline unsigned keyring_hash(const char *desc)
62 bucket += (unsigned char)*desc;
64 return bucket & (KEYRING_NAME_HASH_SIZE - 1);
68 * The keyring key type definition. Keyrings are simply keys of this type and
69 * can be treated as ordinary keys in addition to having their own special
72 static int keyring_preparse(struct key_preparsed_payload *prep);
73 static void keyring_free_preparse(struct key_preparsed_payload *prep);
74 static int keyring_instantiate(struct key *keyring,
75 struct key_preparsed_payload *prep);
76 static void keyring_revoke(struct key *keyring);
77 static void keyring_destroy(struct key *keyring);
78 static void keyring_describe(const struct key *keyring, struct seq_file *m);
79 static long keyring_read(const struct key *keyring,
80 char __user *buffer, size_t buflen);
82 struct key_type key_type_keyring = {
85 .preparse = keyring_preparse,
86 .free_preparse = keyring_free_preparse,
87 .instantiate = keyring_instantiate,
88 .revoke = keyring_revoke,
89 .destroy = keyring_destroy,
90 .describe = keyring_describe,
93 EXPORT_SYMBOL(key_type_keyring);
96 * Semaphore to serialise link/link calls to prevent two link calls in parallel
97 * introducing a cycle.
99 static DEFINE_MUTEX(keyring_serialise_link_lock);
102 * Publish the name of a keyring so that it can be found by name (if it has
105 static void keyring_publish_name(struct key *keyring)
109 if (keyring->description) {
110 bucket = keyring_hash(keyring->description);
112 write_lock(&keyring_name_lock);
114 if (!keyring_name_hash[bucket].next)
115 INIT_LIST_HEAD(&keyring_name_hash[bucket]);
117 list_add_tail(&keyring->name_link,
118 &keyring_name_hash[bucket]);
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 unsigned long hash_key_type_and_desc(const 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;
179 acc = mult_64x32_and_fold(type, desc_len + 13);
180 acc = mult_64x32_and_fold(acc, 9207);
188 memcpy(&piece, description, n);
191 acc = mult_64x32_and_fold(acc, piece);
192 acc = mult_64x32_and_fold(acc, 9207);
195 /* Fold the hash down to 32 bits if need be. */
197 if (ASSOC_ARRAY_KEY_CHUNK_SIZE == 32)
200 /* Squidge all the keyrings into a separate part of the tree to
201 * ordinary keys by making sure the lowest level segment in the hash is
202 * zero for keyrings and non-zero otherwise.
204 if (index_key->type != &key_type_keyring && (hash & fan_mask) == 0)
205 return hash | (hash >> (ASSOC_ARRAY_KEY_CHUNK_SIZE - level_shift)) | 1;
206 if (index_key->type == &key_type_keyring && (hash & fan_mask) != 0)
207 return (hash + (hash << level_shift)) & ~fan_mask;
212 * Build the next index key chunk.
214 * On 32-bit systems the index key is laid out as:
217 * hash desclen typeptr desc[]
222 * hash desclen typeptr desc[]
224 * We return it one word-sized chunk at a time.
226 static unsigned long keyring_get_key_chunk(const void *data, int level)
228 const struct keyring_index_key *index_key = data;
229 unsigned long chunk = 0;
231 int desc_len = index_key->desc_len, n = sizeof(chunk);
233 level /= ASSOC_ARRAY_KEY_CHUNK_SIZE;
236 return hash_key_type_and_desc(index_key);
238 return ((unsigned long)index_key->type << 8) | desc_len;
241 return (u8)((unsigned long)index_key->type >>
242 (ASSOC_ARRAY_KEY_CHUNK_SIZE - 8));
247 offset += sizeof(chunk) - 1;
248 offset += (level - 3) * sizeof(chunk);
249 if (offset >= desc_len)
257 chunk |= ((u8*)index_key->description)[--offset];
258 } while (--desc_len > 0);
262 chunk |= (u8)((unsigned long)index_key->type >>
263 (ASSOC_ARRAY_KEY_CHUNK_SIZE - 8));
269 static unsigned long keyring_get_object_key_chunk(const void *object, int level)
271 const struct key *key = keyring_ptr_to_key(object);
272 return keyring_get_key_chunk(&key->index_key, level);
275 static bool keyring_compare_object(const void *object, const void *data)
277 const struct keyring_index_key *index_key = data;
278 const struct key *key = keyring_ptr_to_key(object);
280 return key->index_key.type == index_key->type &&
281 key->index_key.desc_len == index_key->desc_len &&
282 memcmp(key->index_key.description, index_key->description,
283 index_key->desc_len) == 0;
287 * Compare the index keys of a pair of objects and determine the bit position
288 * at which they differ - if they differ.
290 static int keyring_diff_objects(const void *object, const void *data)
292 const struct key *key_a = keyring_ptr_to_key(object);
293 const struct keyring_index_key *a = &key_a->index_key;
294 const struct keyring_index_key *b = data;
295 unsigned long seg_a, seg_b;
299 seg_a = hash_key_type_and_desc(a);
300 seg_b = hash_key_type_and_desc(b);
301 if ((seg_a ^ seg_b) != 0)
304 /* The number of bits contributed by the hash is controlled by a
305 * constant in the assoc_array headers. Everything else thereafter we
306 * can deal with as being machine word-size dependent.
308 level += ASSOC_ARRAY_KEY_CHUNK_SIZE / 8;
311 if ((seg_a ^ seg_b) != 0)
314 /* The next bit may not work on big endian */
316 seg_a = (unsigned long)a->type;
317 seg_b = (unsigned long)b->type;
318 if ((seg_a ^ seg_b) != 0)
321 level += sizeof(unsigned long);
322 if (a->desc_len == 0)
326 if (((unsigned long)a->description | (unsigned long)b->description) &
327 (sizeof(unsigned long) - 1)) {
329 seg_a = *(unsigned long *)(a->description + i);
330 seg_b = *(unsigned long *)(b->description + i);
331 if ((seg_a ^ seg_b) != 0)
333 i += sizeof(unsigned long);
334 } while (i < (a->desc_len & (sizeof(unsigned long) - 1)));
337 for (; i < a->desc_len; i++) {
338 seg_a = *(unsigned char *)(a->description + i);
339 seg_b = *(unsigned char *)(b->description + i);
340 if ((seg_a ^ seg_b) != 0)
350 i = level * 8 + __ffs(seg_a ^ seg_b);
355 * Free an object after stripping the keyring flag off of the pointer.
357 static void keyring_free_object(void *object)
359 key_put(keyring_ptr_to_key(object));
363 * Operations for keyring management by the index-tree routines.
365 static const struct assoc_array_ops keyring_assoc_array_ops = {
366 .get_key_chunk = keyring_get_key_chunk,
367 .get_object_key_chunk = keyring_get_object_key_chunk,
368 .compare_object = keyring_compare_object,
369 .diff_objects = keyring_diff_objects,
370 .free_object = keyring_free_object,
374 * Clean up a keyring when it is destroyed. Unpublish its name if it had one
375 * and dispose of its data.
377 * The garbage collector detects the final key_put(), removes the keyring from
378 * the serial number tree and then does RCU synchronisation before coming here,
379 * so we shouldn't need to worry about code poking around here with the RCU
380 * readlock held by this time.
382 static void keyring_destroy(struct key *keyring)
384 if (keyring->description) {
385 write_lock(&keyring_name_lock);
387 if (keyring->name_link.next != NULL &&
388 !list_empty(&keyring->name_link))
389 list_del(&keyring->name_link);
391 write_unlock(&keyring_name_lock);
394 if (keyring->restrict_link) {
395 struct key_restriction *keyres = keyring->restrict_link;
397 key_put(keyres->key);
401 assoc_array_destroy(&keyring->keys, &keyring_assoc_array_ops);
405 * Describe a keyring for /proc.
407 static void keyring_describe(const struct key *keyring, struct seq_file *m)
409 if (keyring->description)
410 seq_puts(m, keyring->description);
412 seq_puts(m, "[anon]");
414 if (key_is_positive(keyring)) {
415 if (keyring->keys.nr_leaves_on_tree != 0)
416 seq_printf(m, ": %lu", keyring->keys.nr_leaves_on_tree);
418 seq_puts(m, ": empty");
422 struct keyring_read_iterator_context {
425 key_serial_t __user *buffer;
428 static int keyring_read_iterator(const void *object, void *data)
430 struct keyring_read_iterator_context *ctx = data;
431 const struct key *key = keyring_ptr_to_key(object);
434 kenter("{%s,%d},,{%zu/%zu}",
435 key->type->name, key->serial, ctx->count, ctx->buflen);
437 if (ctx->count >= ctx->buflen)
440 ret = put_user(key->serial, ctx->buffer);
444 ctx->count += sizeof(key->serial);
449 * Read a list of key IDs from the keyring's contents in binary form
451 * The keyring's semaphore is read-locked by the caller. This prevents someone
452 * from modifying it under us - which could cause us to read key IDs multiple
455 static long keyring_read(const struct key *keyring,
456 char __user *buffer, size_t buflen)
458 struct keyring_read_iterator_context ctx;
461 kenter("{%d},,%zu", key_serial(keyring), buflen);
463 if (buflen & (sizeof(key_serial_t) - 1))
466 /* Copy as many key IDs as fit into the buffer */
467 if (buffer && buflen) {
468 ctx.buffer = (key_serial_t __user *)buffer;
471 ret = assoc_array_iterate(&keyring->keys,
472 keyring_read_iterator, &ctx);
474 kleave(" = %ld [iterate]", ret);
479 /* Return the size of the buffer needed */
480 ret = keyring->keys.nr_leaves_on_tree * sizeof(key_serial_t);
482 kleave("= %ld [ok]", ret);
484 kleave("= %ld [buffer too small]", ret);
489 * Allocate a keyring and link into the destination keyring.
491 struct key *keyring_alloc(const char *description, kuid_t uid, kgid_t gid,
492 const struct cred *cred, key_perm_t perm,
494 struct key_restriction *restrict_link,
500 keyring = key_alloc(&key_type_keyring, description,
501 uid, gid, cred, perm, flags, restrict_link);
502 if (!IS_ERR(keyring)) {
503 ret = key_instantiate_and_link(keyring, NULL, 0, dest, NULL);
506 keyring = ERR_PTR(ret);
512 EXPORT_SYMBOL(keyring_alloc);
515 * restrict_link_reject - Give -EPERM to restrict link
516 * @keyring: The keyring being added to.
517 * @type: The type of key being added.
518 * @payload: The payload of the key intended to be added.
519 * @restriction_key: Keys providing additional data for evaluating restriction.
521 * Reject the addition of any links to a keyring. It can be overridden by
522 * passing KEY_ALLOC_BYPASS_RESTRICTION to key_instantiate_and_link() when
523 * adding a key to a keyring.
525 * This is meant to be stored in a key_restriction structure which is passed
526 * in the restrict_link parameter to keyring_alloc().
528 int restrict_link_reject(struct key *keyring,
529 const struct key_type *type,
530 const union key_payload *payload,
531 struct key *restriction_key)
537 * By default, we keys found by getting an exact match on their descriptions.
539 bool key_default_cmp(const struct key *key,
540 const struct key_match_data *match_data)
542 return strcmp(key->description, match_data->raw_data) == 0;
546 * Iteration function to consider each key found.
548 static int keyring_search_iterator(const void *object, void *iterator_data)
550 struct keyring_search_context *ctx = iterator_data;
551 const struct key *key = keyring_ptr_to_key(object);
552 unsigned long kflags = READ_ONCE(key->flags);
553 short state = READ_ONCE(key->state);
555 kenter("{%d}", key->serial);
557 /* ignore keys not of this type */
558 if (key->type != ctx->index_key.type) {
559 kleave(" = 0 [!type]");
563 /* skip invalidated, revoked and expired keys */
564 if (ctx->flags & KEYRING_SEARCH_DO_STATE_CHECK) {
565 time64_t expiry = READ_ONCE(key->expiry);
567 if (kflags & ((1 << KEY_FLAG_INVALIDATED) |
568 (1 << KEY_FLAG_REVOKED))) {
569 ctx->result = ERR_PTR(-EKEYREVOKED);
570 kleave(" = %d [invrev]", ctx->skipped_ret);
574 if (expiry && ctx->now >= expiry) {
575 if (!(ctx->flags & KEYRING_SEARCH_SKIP_EXPIRED))
576 ctx->result = ERR_PTR(-EKEYEXPIRED);
577 kleave(" = %d [expire]", ctx->skipped_ret);
582 /* keys that don't match */
583 if (!ctx->match_data.cmp(key, &ctx->match_data)) {
584 kleave(" = 0 [!match]");
588 /* key must have search permissions */
589 if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM) &&
590 key_task_permission(make_key_ref(key, ctx->possessed),
591 ctx->cred, KEY_NEED_SEARCH) < 0) {
592 ctx->result = ERR_PTR(-EACCES);
593 kleave(" = %d [!perm]", ctx->skipped_ret);
597 if (ctx->flags & KEYRING_SEARCH_DO_STATE_CHECK) {
598 /* we set a different error code if we pass a negative key */
600 ctx->result = ERR_PTR(state);
601 kleave(" = %d [neg]", ctx->skipped_ret);
607 ctx->result = make_key_ref(key, ctx->possessed);
608 kleave(" = 1 [found]");
612 return ctx->skipped_ret;
616 * Search inside a keyring for a key. We can search by walking to it
617 * directly based on its index-key or we can iterate over the entire
618 * tree looking for it, based on the match function.
620 static int search_keyring(struct key *keyring, struct keyring_search_context *ctx)
622 if (ctx->match_data.lookup_type == KEYRING_SEARCH_LOOKUP_DIRECT) {
625 object = assoc_array_find(&keyring->keys,
626 &keyring_assoc_array_ops,
628 return object ? ctx->iterator(object, ctx) : 0;
630 return assoc_array_iterate(&keyring->keys, ctx->iterator, ctx);
634 * Search a tree of keyrings that point to other keyrings up to the maximum
637 static bool search_nested_keyrings(struct key *keyring,
638 struct keyring_search_context *ctx)
642 struct assoc_array_node *node;
644 } stack[KEYRING_SEARCH_MAX_DEPTH];
646 struct assoc_array_shortcut *shortcut;
647 struct assoc_array_node *node;
648 struct assoc_array_ptr *ptr;
652 kenter("{%d},{%s,%s}",
654 ctx->index_key.type->name,
655 ctx->index_key.description);
657 #define STATE_CHECKS (KEYRING_SEARCH_NO_STATE_CHECK | KEYRING_SEARCH_DO_STATE_CHECK)
658 BUG_ON((ctx->flags & STATE_CHECKS) == 0 ||
659 (ctx->flags & STATE_CHECKS) == STATE_CHECKS);
661 /* Check to see if this top-level keyring is what we are looking for
662 * and whether it is valid or not.
664 if (ctx->match_data.lookup_type == KEYRING_SEARCH_LOOKUP_ITERATE ||
665 keyring_compare_object(keyring, &ctx->index_key)) {
666 ctx->skipped_ret = 2;
667 switch (ctx->iterator(keyring_key_to_ptr(keyring), ctx)) {
677 ctx->skipped_ret = 0;
679 /* Start processing a new keyring */
681 kdebug("descend to %d", keyring->serial);
682 if (keyring->flags & ((1 << KEY_FLAG_INVALIDATED) |
683 (1 << KEY_FLAG_REVOKED)))
684 goto not_this_keyring;
686 /* Search through the keys in this keyring before its searching its
689 if (search_keyring(keyring, ctx))
692 /* Then manually iterate through the keyrings nested in this one.
694 * Start from the root node of the index tree. Because of the way the
695 * hash function has been set up, keyrings cluster on the leftmost
696 * branch of the root node (root slot 0) or in the root node itself.
697 * Non-keyrings avoid the leftmost branch of the root entirely (root
700 ptr = READ_ONCE(keyring->keys.root);
702 goto not_this_keyring;
704 if (assoc_array_ptr_is_shortcut(ptr)) {
705 /* If the root is a shortcut, either the keyring only contains
706 * keyring pointers (everything clusters behind root slot 0) or
707 * doesn't contain any keyring pointers.
709 shortcut = assoc_array_ptr_to_shortcut(ptr);
710 if ((shortcut->index_key[0] & ASSOC_ARRAY_FAN_MASK) != 0)
711 goto not_this_keyring;
713 ptr = READ_ONCE(shortcut->next_node);
714 node = assoc_array_ptr_to_node(ptr);
718 node = assoc_array_ptr_to_node(ptr);
719 ptr = node->slots[0];
720 if (!assoc_array_ptr_is_meta(ptr))
724 /* Descend to a more distal node in this keyring's content tree and go
728 if (assoc_array_ptr_is_shortcut(ptr)) {
729 shortcut = assoc_array_ptr_to_shortcut(ptr);
730 ptr = READ_ONCE(shortcut->next_node);
731 BUG_ON(!assoc_array_ptr_is_node(ptr));
733 node = assoc_array_ptr_to_node(ptr);
736 kdebug("begin_node");
739 /* Go through the slots in a node */
740 for (; slot < ASSOC_ARRAY_FAN_OUT; slot++) {
741 ptr = READ_ONCE(node->slots[slot]);
743 if (assoc_array_ptr_is_meta(ptr) && node->back_pointer)
744 goto descend_to_node;
746 if (!keyring_ptr_is_keyring(ptr))
749 key = keyring_ptr_to_key(ptr);
751 if (sp >= KEYRING_SEARCH_MAX_DEPTH) {
752 if (ctx->flags & KEYRING_SEARCH_DETECT_TOO_DEEP) {
753 ctx->result = ERR_PTR(-ELOOP);
756 goto not_this_keyring;
759 /* Search a nested keyring */
760 if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM) &&
761 key_task_permission(make_key_ref(key, ctx->possessed),
762 ctx->cred, KEY_NEED_SEARCH) < 0)
765 /* stack the current position */
766 stack[sp].keyring = keyring;
767 stack[sp].node = node;
768 stack[sp].slot = slot;
771 /* begin again with the new keyring */
773 goto descend_to_keyring;
776 /* We've dealt with all the slots in the current node, so now we need
777 * to ascend to the parent and continue processing there.
779 ptr = READ_ONCE(node->back_pointer);
780 slot = node->parent_slot;
782 if (ptr && assoc_array_ptr_is_shortcut(ptr)) {
783 shortcut = assoc_array_ptr_to_shortcut(ptr);
784 ptr = READ_ONCE(shortcut->back_pointer);
785 slot = shortcut->parent_slot;
788 goto not_this_keyring;
789 node = assoc_array_ptr_to_node(ptr);
792 /* If we've ascended to the root (zero backpointer), we must have just
793 * finished processing the leftmost branch rather than the root slots -
794 * so there can't be any more keyrings for us to find.
796 if (node->back_pointer) {
797 kdebug("ascend %d", slot);
801 /* The keyring we're looking at was disqualified or didn't contain a
805 kdebug("not_this_keyring %d", sp);
811 /* Resume the processing of a keyring higher up in the tree */
813 keyring = stack[sp].keyring;
814 node = stack[sp].node;
815 slot = stack[sp].slot + 1;
816 kdebug("ascend to %d [%d]", keyring->serial, slot);
819 /* We found a viable match */
821 key = key_ref_to_ptr(ctx->result);
823 if (!(ctx->flags & KEYRING_SEARCH_NO_UPDATE_TIME)) {
824 key->last_used_at = ctx->now;
825 keyring->last_used_at = ctx->now;
827 stack[--sp].keyring->last_used_at = ctx->now;
834 * keyring_search_rcu - Search a keyring tree for a matching key under RCU
835 * @keyring_ref: A pointer to the keyring with possession indicator.
836 * @ctx: The keyring search context.
838 * Search the supplied keyring tree for a key that matches the criteria given.
839 * The root keyring and any linked keyrings must grant Search permission to the
840 * caller to be searchable and keys can only be found if they too grant Search
841 * to the caller. The possession flag on the root keyring pointer controls use
842 * of the possessor bits in permissions checking of the entire tree. In
843 * addition, the LSM gets to forbid keyring searches and key matches.
845 * The search is performed as a breadth-then-depth search up to the prescribed
846 * limit (KEYRING_SEARCH_MAX_DEPTH). The caller must hold the RCU read lock to
847 * prevent keyrings from being destroyed or rearranged whilst they are being
850 * Keys are matched to the type provided and are then filtered by the match
851 * function, which is given the description to use in any way it sees fit. The
852 * match function may use any attributes of a key that it wishes to to
853 * determine the match. Normally the match function from the key type would be
856 * RCU can be used to prevent the keyring key lists from disappearing without
857 * the need to take lots of locks.
859 * Returns a pointer to the found key and increments the key usage count if
860 * successful; -EAGAIN if no matching keys were found, or if expired or revoked
861 * keys were found; -ENOKEY if only negative keys were found; -ENOTDIR if the
862 * specified keyring wasn't a keyring.
864 * In the case of a successful return, the possession attribute from
865 * @keyring_ref is propagated to the returned key reference.
867 key_ref_t keyring_search_rcu(key_ref_t keyring_ref,
868 struct keyring_search_context *ctx)
873 ctx->iterator = keyring_search_iterator;
874 ctx->possessed = is_key_possessed(keyring_ref);
875 ctx->result = ERR_PTR(-EAGAIN);
877 keyring = key_ref_to_ptr(keyring_ref);
880 if (keyring->type != &key_type_keyring)
881 return ERR_PTR(-ENOTDIR);
883 if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM)) {
884 err = key_task_permission(keyring_ref, ctx->cred, KEY_NEED_SEARCH);
889 ctx->now = ktime_get_real_seconds();
890 if (search_nested_keyrings(keyring, ctx))
891 __key_get(key_ref_to_ptr(ctx->result));
896 * keyring_search - Search the supplied keyring tree for a matching key
897 * @keyring: The root of the keyring tree to be searched.
898 * @type: The type of keyring we want to find.
899 * @description: The name of the keyring we want to find.
901 * As keyring_search_rcu() above, but using the current task's credentials and
902 * type's default matching function and preferred search method.
904 key_ref_t keyring_search(key_ref_t keyring,
905 struct key_type *type,
906 const char *description)
908 struct keyring_search_context ctx = {
909 .index_key.type = type,
910 .index_key.description = description,
911 .index_key.desc_len = strlen(description),
912 .cred = current_cred(),
913 .match_data.cmp = key_default_cmp,
914 .match_data.raw_data = description,
915 .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
916 .flags = KEYRING_SEARCH_DO_STATE_CHECK,
921 if (type->match_preparse) {
922 ret = type->match_preparse(&ctx.match_data);
928 key = keyring_search_rcu(keyring, &ctx);
931 if (type->match_free)
932 type->match_free(&ctx.match_data);
935 EXPORT_SYMBOL(keyring_search);
937 static struct key_restriction *keyring_restriction_alloc(
938 key_restrict_link_func_t check)
940 struct key_restriction *keyres =
941 kzalloc(sizeof(struct key_restriction), GFP_KERNEL);
944 return ERR_PTR(-ENOMEM);
946 keyres->check = check;
952 * Semaphore to serialise restriction setup to prevent reference count
953 * cycles through restriction key pointers.
955 static DECLARE_RWSEM(keyring_serialise_restrict_sem);
958 * Check for restriction cycles that would prevent keyring garbage collection.
959 * keyring_serialise_restrict_sem must be held.
961 static bool keyring_detect_restriction_cycle(const struct key *dest_keyring,
962 struct key_restriction *keyres)
964 while (keyres && keyres->key &&
965 keyres->key->type == &key_type_keyring) {
966 if (keyres->key == dest_keyring)
969 keyres = keyres->key->restrict_link;
976 * keyring_restrict - Look up and apply a restriction to a keyring
977 * @keyring_ref: The keyring to be restricted
978 * @type: The key type that will provide the restriction checker.
979 * @restriction: The restriction options to apply to the keyring
981 * Look up a keyring and apply a restriction to it. The restriction is managed
982 * by the specific key type, but can be configured by the options specified in
983 * the restriction string.
985 int keyring_restrict(key_ref_t keyring_ref, const char *type,
986 const char *restriction)
989 struct key_type *restrict_type = NULL;
990 struct key_restriction *restrict_link;
993 keyring = key_ref_to_ptr(keyring_ref);
996 if (keyring->type != &key_type_keyring)
1000 restrict_link = keyring_restriction_alloc(restrict_link_reject);
1002 restrict_type = key_type_lookup(type);
1004 if (IS_ERR(restrict_type))
1005 return PTR_ERR(restrict_type);
1007 if (!restrict_type->lookup_restriction) {
1012 restrict_link = restrict_type->lookup_restriction(restriction);
1015 if (IS_ERR(restrict_link)) {
1016 ret = PTR_ERR(restrict_link);
1020 down_write(&keyring->sem);
1021 down_write(&keyring_serialise_restrict_sem);
1023 if (keyring->restrict_link)
1025 else if (keyring_detect_restriction_cycle(keyring, restrict_link))
1028 keyring->restrict_link = restrict_link;
1030 up_write(&keyring_serialise_restrict_sem);
1031 up_write(&keyring->sem);
1034 key_put(restrict_link->key);
1035 kfree(restrict_link);
1040 key_type_put(restrict_type);
1044 EXPORT_SYMBOL(keyring_restrict);
1047 * Search the given keyring for a key that might be updated.
1049 * The caller must guarantee that the keyring is a keyring and that the
1050 * permission is granted to modify the keyring as no check is made here. The
1051 * caller must also hold a lock on the keyring semaphore.
1053 * Returns a pointer to the found key with usage count incremented if
1054 * successful and returns NULL if not found. Revoked and invalidated keys are
1057 * If successful, the possession indicator is propagated from the keyring ref
1058 * to the returned key reference.
1060 key_ref_t find_key_to_update(key_ref_t keyring_ref,
1061 const struct keyring_index_key *index_key)
1063 struct key *keyring, *key;
1066 keyring = key_ref_to_ptr(keyring_ref);
1068 kenter("{%d},{%s,%s}",
1069 keyring->serial, index_key->type->name, index_key->description);
1071 object = assoc_array_find(&keyring->keys, &keyring_assoc_array_ops,
1081 key = keyring_ptr_to_key(object);
1082 if (key->flags & ((1 << KEY_FLAG_INVALIDATED) |
1083 (1 << KEY_FLAG_REVOKED))) {
1084 kleave(" = NULL [x]");
1088 kleave(" = {%d}", key->serial);
1089 return make_key_ref(key, is_key_possessed(keyring_ref));
1093 * Find a keyring with the specified name.
1095 * Only keyrings that have nonzero refcount, are not revoked, and are owned by a
1096 * user in the current user namespace are considered. If @uid_keyring is %true,
1097 * the keyring additionally must have been allocated as a user or user session
1098 * keyring; otherwise, it must grant Search permission directly to the caller.
1100 * Returns a pointer to the keyring with the keyring's refcount having being
1101 * incremented on success. -ENOKEY is returned if a key could not be found.
1103 struct key *find_keyring_by_name(const char *name, bool uid_keyring)
1105 struct key *keyring;
1109 return ERR_PTR(-EINVAL);
1111 bucket = keyring_hash(name);
1113 read_lock(&keyring_name_lock);
1115 if (keyring_name_hash[bucket].next) {
1116 /* search this hash bucket for a keyring with a matching name
1117 * that's readable and that hasn't been revoked */
1118 list_for_each_entry(keyring,
1119 &keyring_name_hash[bucket],
1122 if (!kuid_has_mapping(current_user_ns(), keyring->user->uid))
1125 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
1128 if (strcmp(keyring->description, name) != 0)
1132 if (!test_bit(KEY_FLAG_UID_KEYRING,
1136 if (key_permission(make_key_ref(keyring, 0),
1137 KEY_NEED_SEARCH) < 0)
1141 /* we've got a match but we might end up racing with
1142 * key_cleanup() if the keyring is currently 'dead'
1143 * (ie. it has a zero usage count) */
1144 if (!refcount_inc_not_zero(&keyring->usage))
1146 keyring->last_used_at = ktime_get_real_seconds();
1151 keyring = ERR_PTR(-ENOKEY);
1153 read_unlock(&keyring_name_lock);
1157 static int keyring_detect_cycle_iterator(const void *object,
1158 void *iterator_data)
1160 struct keyring_search_context *ctx = iterator_data;
1161 const struct key *key = keyring_ptr_to_key(object);
1163 kenter("{%d}", key->serial);
1165 /* We might get a keyring with matching index-key that is nonetheless a
1166 * different keyring. */
1167 if (key != ctx->match_data.raw_data)
1170 ctx->result = ERR_PTR(-EDEADLK);
1175 * See if a cycle will will be created by inserting acyclic tree B in acyclic
1176 * tree A at the topmost level (ie: as a direct child of A).
1178 * Since we are adding B to A at the top level, checking for cycles should just
1179 * be a matter of seeing if node A is somewhere in tree B.
1181 static int keyring_detect_cycle(struct key *A, struct key *B)
1183 struct keyring_search_context ctx = {
1184 .index_key = A->index_key,
1185 .match_data.raw_data = A,
1186 .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
1187 .iterator = keyring_detect_cycle_iterator,
1188 .flags = (KEYRING_SEARCH_NO_STATE_CHECK |
1189 KEYRING_SEARCH_NO_UPDATE_TIME |
1190 KEYRING_SEARCH_NO_CHECK_PERM |
1191 KEYRING_SEARCH_DETECT_TOO_DEEP),
1195 search_nested_keyrings(B, &ctx);
1197 return PTR_ERR(ctx.result) == -EAGAIN ? 0 : PTR_ERR(ctx.result);
1201 * Lock keyring for link.
1203 int __key_link_lock(struct key *keyring,
1204 const struct keyring_index_key *index_key)
1205 __acquires(&keyring->sem)
1206 __acquires(&keyring_serialise_link_lock)
1208 if (keyring->type != &key_type_keyring)
1211 down_write(&keyring->sem);
1213 /* Serialise link/link calls to prevent parallel calls causing a cycle
1214 * when linking two keyring in opposite orders.
1216 if (index_key->type == &key_type_keyring)
1217 mutex_lock(&keyring_serialise_link_lock);
1223 * Lock keyrings for move (link/unlink combination).
1225 int __key_move_lock(struct key *l_keyring, struct key *u_keyring,
1226 const struct keyring_index_key *index_key)
1227 __acquires(&l_keyring->sem)
1228 __acquires(&u_keyring->sem)
1229 __acquires(&keyring_serialise_link_lock)
1231 if (l_keyring->type != &key_type_keyring ||
1232 u_keyring->type != &key_type_keyring)
1235 /* We have to be very careful here to take the keyring locks in the
1236 * right order, lest we open ourselves to deadlocking against another
1239 if (l_keyring < u_keyring) {
1240 down_write(&l_keyring->sem);
1241 down_write_nested(&u_keyring->sem, 1);
1243 down_write(&u_keyring->sem);
1244 down_write_nested(&l_keyring->sem, 1);
1247 /* Serialise link/link calls to prevent parallel calls causing a cycle
1248 * when linking two keyring in opposite orders.
1250 if (index_key->type == &key_type_keyring)
1251 mutex_lock(&keyring_serialise_link_lock);
1257 * Preallocate memory so that a key can be linked into to a keyring.
1259 int __key_link_begin(struct key *keyring,
1260 const struct keyring_index_key *index_key,
1261 struct assoc_array_edit **_edit)
1263 struct assoc_array_edit *edit;
1267 keyring->serial, index_key->type->name, index_key->description);
1269 BUG_ON(index_key->desc_len == 0);
1270 BUG_ON(*_edit != NULL);
1275 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
1278 /* Create an edit script that will insert/replace the key in the
1281 edit = assoc_array_insert(&keyring->keys,
1282 &keyring_assoc_array_ops,
1286 ret = PTR_ERR(edit);
1290 /* If we're not replacing a link in-place then we're going to need some
1293 if (!edit->dead_leaf) {
1294 ret = key_payload_reserve(keyring,
1295 keyring->datalen + KEYQUOTA_LINK_BYTES);
1305 assoc_array_cancel_edit(edit);
1307 kleave(" = %d", ret);
1312 * Check already instantiated keys aren't going to be a problem.
1314 * The caller must have called __key_link_begin(). Don't need to call this for
1315 * keys that were created since __key_link_begin() was called.
1317 int __key_link_check_live_key(struct key *keyring, struct key *key)
1319 if (key->type == &key_type_keyring)
1320 /* check that we aren't going to create a cycle by linking one
1321 * keyring to another */
1322 return keyring_detect_cycle(keyring, key);
1327 * Link a key into to a keyring.
1329 * Must be called with __key_link_begin() having being called. Discards any
1330 * already extant link to matching key if there is one, so that each keyring
1331 * holds at most one link to any given key of a particular type+description
1334 void __key_link(struct key *key, struct assoc_array_edit **_edit)
1337 assoc_array_insert_set_object(*_edit, keyring_key_to_ptr(key));
1338 assoc_array_apply_edit(*_edit);
1343 * Finish linking a key into to a keyring.
1345 * Must be called with __key_link_begin() having being called.
1347 void __key_link_end(struct key *keyring,
1348 const struct keyring_index_key *index_key,
1349 struct assoc_array_edit *edit)
1350 __releases(&keyring->sem)
1351 __releases(&keyring_serialise_link_lock)
1353 BUG_ON(index_key->type == NULL);
1354 kenter("%d,%s,", keyring->serial, index_key->type->name);
1357 if (!edit->dead_leaf) {
1358 key_payload_reserve(keyring,
1359 keyring->datalen - KEYQUOTA_LINK_BYTES);
1361 assoc_array_cancel_edit(edit);
1363 up_write(&keyring->sem);
1365 if (index_key->type == &key_type_keyring)
1366 mutex_unlock(&keyring_serialise_link_lock);
1370 * Check addition of keys to restricted keyrings.
1372 static int __key_link_check_restriction(struct key *keyring, struct key *key)
1374 if (!keyring->restrict_link || !keyring->restrict_link->check)
1376 return keyring->restrict_link->check(keyring, key->type, &key->payload,
1377 keyring->restrict_link->key);
1381 * key_link - Link a key to a keyring
1382 * @keyring: The keyring to make the link in.
1383 * @key: The key to link to.
1385 * Make a link in a keyring to a key, such that the keyring holds a reference
1386 * on that key and the key can potentially be found by searching that keyring.
1388 * This function will write-lock the keyring's semaphore and will consume some
1389 * of the user's key data quota to hold the link.
1391 * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring,
1392 * -EKEYREVOKED if the keyring has been revoked, -ENFILE if the keyring is
1393 * full, -EDQUOT if there is insufficient key data quota remaining to add
1394 * another link or -ENOMEM if there's insufficient memory.
1396 * It is assumed that the caller has checked that it is permitted for a link to
1397 * be made (the keyring should have Write permission and the key Link
1400 int key_link(struct key *keyring, struct key *key)
1402 struct assoc_array_edit *edit = NULL;
1405 kenter("{%d,%d}", keyring->serial, refcount_read(&keyring->usage));
1410 ret = __key_link_lock(keyring, &key->index_key);
1414 ret = __key_link_begin(keyring, &key->index_key, &edit);
1418 kdebug("begun {%d,%d}", keyring->serial, refcount_read(&keyring->usage));
1419 ret = __key_link_check_restriction(keyring, key);
1421 ret = __key_link_check_live_key(keyring, key);
1423 __key_link(key, &edit);
1426 __key_link_end(keyring, &key->index_key, edit);
1428 kleave(" = %d {%d,%d}", ret, keyring->serial, refcount_read(&keyring->usage));
1431 EXPORT_SYMBOL(key_link);
1434 * Lock a keyring for unlink.
1436 static int __key_unlink_lock(struct key *keyring)
1437 __acquires(&keyring->sem)
1439 if (keyring->type != &key_type_keyring)
1442 down_write(&keyring->sem);
1447 * Begin the process of unlinking a key from a keyring.
1449 static int __key_unlink_begin(struct key *keyring, struct key *key,
1450 struct assoc_array_edit **_edit)
1452 struct assoc_array_edit *edit;
1454 BUG_ON(*_edit != NULL);
1456 edit = assoc_array_delete(&keyring->keys, &keyring_assoc_array_ops,
1459 return PTR_ERR(edit);
1469 * Apply an unlink change.
1471 static void __key_unlink(struct key *keyring, struct key *key,
1472 struct assoc_array_edit **_edit)
1474 assoc_array_apply_edit(*_edit);
1476 key_payload_reserve(keyring, keyring->datalen - KEYQUOTA_LINK_BYTES);
1480 * Finish unlinking a key from to a keyring.
1482 static void __key_unlink_end(struct key *keyring,
1484 struct assoc_array_edit *edit)
1485 __releases(&keyring->sem)
1488 assoc_array_cancel_edit(edit);
1489 up_write(&keyring->sem);
1493 * key_unlink - Unlink the first link to a key from a keyring.
1494 * @keyring: The keyring to remove the link from.
1495 * @key: The key the link is to.
1497 * Remove a link from a keyring to a key.
1499 * This function will write-lock the keyring's semaphore.
1501 * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring, -ENOENT if
1502 * the key isn't linked to by the keyring or -ENOMEM if there's insufficient
1505 * It is assumed that the caller has checked that it is permitted for a link to
1506 * be removed (the keyring should have Write permission; no permissions are
1507 * required on the key).
1509 int key_unlink(struct key *keyring, struct key *key)
1511 struct assoc_array_edit *edit = NULL;
1517 ret = __key_unlink_lock(keyring);
1521 ret = __key_unlink_begin(keyring, key, &edit);
1523 __key_unlink(keyring, key, &edit);
1524 __key_unlink_end(keyring, key, edit);
1527 EXPORT_SYMBOL(key_unlink);
1530 * key_move - Move a key from one keyring to another
1531 * @key: The key to move
1532 * @from_keyring: The keyring to remove the link from.
1533 * @to_keyring: The keyring to make the link in.
1534 * @flags: Qualifying flags, such as KEYCTL_MOVE_EXCL.
1536 * Make a link in @to_keyring to a key, such that the keyring holds a reference
1537 * on that key and the key can potentially be found by searching that keyring
1538 * whilst simultaneously removing a link to the key from @from_keyring.
1540 * This function will write-lock both keyring's semaphores and will consume
1541 * some of the user's key data quota to hold the link on @to_keyring.
1543 * Returns 0 if successful, -ENOTDIR if either keyring isn't a keyring,
1544 * -EKEYREVOKED if either keyring has been revoked, -ENFILE if the second
1545 * keyring is full, -EDQUOT if there is insufficient key data quota remaining
1546 * to add another link or -ENOMEM if there's insufficient memory. If
1547 * KEYCTL_MOVE_EXCL is set, then -EEXIST will be returned if there's already a
1548 * matching key in @to_keyring.
1550 * It is assumed that the caller has checked that it is permitted for a link to
1551 * be made (the keyring should have Write permission and the key Link
1554 int key_move(struct key *key,
1555 struct key *from_keyring,
1556 struct key *to_keyring,
1559 struct assoc_array_edit *from_edit = NULL, *to_edit = NULL;
1562 kenter("%d,%d,%d", key->serial, from_keyring->serial, to_keyring->serial);
1564 if (from_keyring == to_keyring)
1568 key_check(from_keyring);
1569 key_check(to_keyring);
1571 ret = __key_move_lock(from_keyring, to_keyring, &key->index_key);
1574 ret = __key_unlink_begin(from_keyring, key, &from_edit);
1577 ret = __key_link_begin(to_keyring, &key->index_key, &to_edit);
1582 if (to_edit->dead_leaf && (flags & KEYCTL_MOVE_EXCL))
1585 ret = __key_link_check_restriction(to_keyring, key);
1588 ret = __key_link_check_live_key(to_keyring, key);
1592 __key_unlink(from_keyring, key, &from_edit);
1593 __key_link(key, &to_edit);
1595 __key_link_end(to_keyring, &key->index_key, to_edit);
1596 __key_unlink_end(from_keyring, key, from_edit);
1598 kleave(" = %d", ret);
1601 EXPORT_SYMBOL(key_move);
1604 * keyring_clear - Clear a keyring
1605 * @keyring: The keyring to clear.
1607 * Clear the contents of the specified keyring.
1609 * Returns 0 if successful or -ENOTDIR if the keyring isn't a keyring.
1611 int keyring_clear(struct key *keyring)
1613 struct assoc_array_edit *edit;
1616 if (keyring->type != &key_type_keyring)
1619 down_write(&keyring->sem);
1621 edit = assoc_array_clear(&keyring->keys, &keyring_assoc_array_ops);
1623 ret = PTR_ERR(edit);
1626 assoc_array_apply_edit(edit);
1627 key_payload_reserve(keyring, 0);
1631 up_write(&keyring->sem);
1634 EXPORT_SYMBOL(keyring_clear);
1637 * Dispose of the links from a revoked keyring.
1639 * This is called with the key sem write-locked.
1641 static void keyring_revoke(struct key *keyring)
1643 struct assoc_array_edit *edit;
1645 edit = assoc_array_clear(&keyring->keys, &keyring_assoc_array_ops);
1646 if (!IS_ERR(edit)) {
1648 assoc_array_apply_edit(edit);
1649 key_payload_reserve(keyring, 0);
1653 static bool keyring_gc_select_iterator(void *object, void *iterator_data)
1655 struct key *key = keyring_ptr_to_key(object);
1656 time64_t *limit = iterator_data;
1658 if (key_is_dead(key, *limit))
1664 static int keyring_gc_check_iterator(const void *object, void *iterator_data)
1666 const struct key *key = keyring_ptr_to_key(object);
1667 time64_t *limit = iterator_data;
1670 return key_is_dead(key, *limit);
1674 * Garbage collect pointers from a keyring.
1676 * Not called with any locks held. The keyring's key struct will not be
1677 * deallocated under us as only our caller may deallocate it.
1679 void keyring_gc(struct key *keyring, time64_t limit)
1683 kenter("%x{%s}", keyring->serial, keyring->description ?: "");
1685 if (keyring->flags & ((1 << KEY_FLAG_INVALIDATED) |
1686 (1 << KEY_FLAG_REVOKED)))
1689 /* scan the keyring looking for dead keys */
1691 result = assoc_array_iterate(&keyring->keys,
1692 keyring_gc_check_iterator, &limit);
1702 down_write(&keyring->sem);
1703 assoc_array_gc(&keyring->keys, &keyring_assoc_array_ops,
1704 keyring_gc_select_iterator, &limit);
1705 up_write(&keyring->sem);
1710 * Garbage collect restriction pointers from a keyring.
1712 * Keyring restrictions are associated with a key type, and must be cleaned
1713 * up if the key type is unregistered. The restriction is altered to always
1714 * reject additional keys so a keyring cannot be opened up by unregistering
1717 * Not called with any keyring locks held. The keyring's key struct will not
1718 * be deallocated under us as only our caller may deallocate it.
1720 * The caller is required to hold key_types_sem and dead_type->sem. This is
1721 * fulfilled by key_gc_keytype() holding the locks on behalf of
1722 * key_garbage_collector(), which it invokes on a workqueue.
1724 void keyring_restriction_gc(struct key *keyring, struct key_type *dead_type)
1726 struct key_restriction *keyres;
1728 kenter("%x{%s}", keyring->serial, keyring->description ?: "");
1731 * keyring->restrict_link is only assigned at key allocation time
1732 * or with the key type locked, so the only values that could be
1733 * concurrently assigned to keyring->restrict_link are for key
1734 * types other than dead_type. Given this, it's ok to check
1735 * the key type before acquiring keyring->sem.
1737 if (!dead_type || !keyring->restrict_link ||
1738 keyring->restrict_link->keytype != dead_type) {
1739 kleave(" [no restriction gc]");
1743 /* Lock the keyring to ensure that a link is not in progress */
1744 down_write(&keyring->sem);
1746 keyres = keyring->restrict_link;
1748 keyres->check = restrict_link_reject;
1750 key_put(keyres->key);
1752 keyres->keytype = NULL;
1754 up_write(&keyring->sem);
1756 kleave(" [restriction gc]");