1 /* keyring.c: keyring handling
3 * Copyright (C) 2004-5 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/module.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 <asm/uaccess.h>
23 * when plumbing the depths of the key tree, this sets a hard limit set on how
24 * deep we're willing to go
26 #define KEYRING_SEARCH_MAX_DEPTH 6
29 * we keep all named keyrings in a hash to speed looking them up
31 #define KEYRING_NAME_HASH_SIZE (1 << 5)
33 static struct list_head keyring_name_hash[KEYRING_NAME_HASH_SIZE];
34 static DEFINE_RWLOCK(keyring_name_lock);
36 static inline unsigned keyring_hash(const char *desc)
41 bucket += (unsigned char) *desc;
43 return bucket & (KEYRING_NAME_HASH_SIZE - 1);
47 * the keyring type definition
49 static int keyring_instantiate(struct key *keyring,
50 const void *data, size_t datalen);
51 static int keyring_duplicate(struct key *keyring, const struct key *source);
52 static int keyring_match(const struct key *keyring, const void *criterion);
53 static void keyring_destroy(struct key *keyring);
54 static void keyring_describe(const struct key *keyring, struct seq_file *m);
55 static long keyring_read(const struct key *keyring,
56 char __user *buffer, size_t buflen);
58 struct key_type key_type_keyring = {
60 .def_datalen = sizeof(struct keyring_list),
61 .instantiate = keyring_instantiate,
62 .duplicate = keyring_duplicate,
63 .match = keyring_match,
64 .destroy = keyring_destroy,
65 .describe = keyring_describe,
70 * semaphore to serialise link/link calls to prevent two link calls in parallel
73 DECLARE_RWSEM(keyring_serialise_link_sem);
75 /*****************************************************************************/
77 * publish the name of a keyring so that it can be found by name (if it has
80 void keyring_publish_name(struct key *keyring)
84 if (keyring->description) {
85 bucket = keyring_hash(keyring->description);
87 write_lock(&keyring_name_lock);
89 if (!keyring_name_hash[bucket].next)
90 INIT_LIST_HEAD(&keyring_name_hash[bucket]);
92 list_add_tail(&keyring->type_data.link,
93 &keyring_name_hash[bucket]);
95 write_unlock(&keyring_name_lock);
98 } /* end keyring_publish_name() */
100 /*****************************************************************************/
102 * initialise a keyring
103 * - we object if we were given any data
105 static int keyring_instantiate(struct key *keyring,
106 const void *data, size_t datalen)
112 /* make the keyring available by name if it has one */
113 keyring_publish_name(keyring);
119 } /* end keyring_instantiate() */
121 /*****************************************************************************/
123 * duplicate the list of subscribed keys from a source keyring into this one
125 static int keyring_duplicate(struct key *keyring, const struct key *source)
127 struct keyring_list *sklist, *klist;
132 const unsigned limit =
133 (PAGE_SIZE - sizeof(*klist)) / sizeof(struct key *);
137 /* find out how many keys are currently linked */
139 sklist = rcu_dereference(source->payload.subscriptions);
145 /* allocate a new payload and stuff load with key links */
149 max = (max + 3) & ~3;
154 size = sizeof(*klist) + sizeof(struct key *) * max;
155 klist = kmalloc(size, GFP_KERNEL);
161 sklist = rcu_dereference(source->payload.subscriptions);
163 klist->maxkeys = max;
164 klist->nkeys = sklist->nkeys;
167 sklist->nkeys * sizeof(struct key *));
169 for (loop = klist->nkeys - 1; loop >= 0; loop--)
170 atomic_inc(&klist->keys[loop]->usage);
174 rcu_assign_pointer(keyring->payload.subscriptions, klist);
181 } /* end keyring_duplicate() */
183 /*****************************************************************************/
185 * match keyrings on their name
187 static int keyring_match(const struct key *keyring, const void *description)
189 return keyring->description &&
190 strcmp(keyring->description, description) == 0;
192 } /* end keyring_match() */
194 /*****************************************************************************/
196 * dispose of the data dangling from the corpse of a keyring
198 static void keyring_destroy(struct key *keyring)
200 struct keyring_list *klist;
203 if (keyring->description) {
204 write_lock(&keyring_name_lock);
206 if (keyring->type_data.link.next != NULL &&
207 !list_empty(&keyring->type_data.link))
208 list_del(&keyring->type_data.link);
210 write_unlock(&keyring_name_lock);
213 klist = rcu_dereference(keyring->payload.subscriptions);
215 for (loop = klist->nkeys - 1; loop >= 0; loop--)
216 key_put(klist->keys[loop]);
220 } /* end keyring_destroy() */
222 /*****************************************************************************/
224 * describe the keyring
226 static void keyring_describe(const struct key *keyring, struct seq_file *m)
228 struct keyring_list *klist;
230 if (keyring->description) {
231 seq_puts(m, keyring->description);
234 seq_puts(m, "[anon]");
238 klist = rcu_dereference(keyring->payload.subscriptions);
240 seq_printf(m, ": %u/%u", klist->nkeys, klist->maxkeys);
242 seq_puts(m, ": empty");
245 } /* end keyring_describe() */
247 /*****************************************************************************/
249 * read a list of key IDs from the keyring's contents
250 * - the keyring's semaphore is read-locked
252 static long keyring_read(const struct key *keyring,
253 char __user *buffer, size_t buflen)
255 struct keyring_list *klist;
261 klist = rcu_dereference(keyring->payload.subscriptions);
264 /* calculate how much data we could return */
265 qty = klist->nkeys * sizeof(key_serial_t);
267 if (buffer && buflen > 0) {
271 /* copy the IDs of the subscribed keys into the
275 for (loop = 0; loop < klist->nkeys; loop++) {
276 key = klist->keys[loop];
278 tmp = sizeof(key_serial_t);
282 if (copy_to_user(buffer,
300 } /* end keyring_read() */
302 /*****************************************************************************/
304 * allocate a keyring and link into the destination keyring
306 struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
307 int not_in_quota, struct key *dest)
312 keyring = key_alloc(&key_type_keyring, description,
314 (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
317 if (!IS_ERR(keyring)) {
318 ret = key_instantiate_and_link(keyring, NULL, 0, dest, NULL);
321 keyring = ERR_PTR(ret);
327 } /* end keyring_alloc() */
329 /*****************************************************************************/
331 * search the supplied keyring tree for a key that matches the criterion
332 * - perform a breadth-then-depth search up to the prescribed limit
333 * - we only find keys on which we have search permission
334 * - we use the supplied match function to see if the description (or other
335 * feature of interest) matches
336 * - we rely on RCU to prevent the keyring lists from disappearing on us
337 * - we return -EAGAIN if we didn't find any matching key
338 * - we return -ENOKEY if we only found negative matching keys
339 * - we propagate the possession attribute from the keyring ref to the key ref
341 key_ref_t keyring_search_aux(key_ref_t keyring_ref,
342 struct task_struct *context,
343 struct key_type *type,
344 const void *description,
345 key_match_func_t match)
348 struct keyring_list *keylist;
350 } stack[KEYRING_SEARCH_MAX_DEPTH];
352 struct keyring_list *keylist;
354 unsigned long possessed;
355 struct key *keyring, *key;
360 keyring = key_ref_to_ptr(keyring_ref);
361 possessed = is_key_possessed(keyring_ref);
364 /* top keyring must have search permission to begin the search */
365 err = key_task_permission(keyring_ref, context, KEY_SEARCH);
367 key_ref = ERR_PTR(err);
371 key_ref = ERR_PTR(-ENOTDIR);
372 if (keyring->type != &key_type_keyring)
377 now = current_kernel_time();
381 /* start processing a new keyring */
383 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
384 goto not_this_keyring;
386 keylist = rcu_dereference(keyring->payload.subscriptions);
388 goto not_this_keyring;
390 /* iterate through the keys in this keyring first */
391 for (kix = 0; kix < keylist->nkeys; kix++) {
392 key = keylist->keys[kix];
394 /* ignore keys not of this type */
395 if (key->type != type)
398 /* skip revoked keys and expired keys */
399 if (test_bit(KEY_FLAG_REVOKED, &key->flags))
402 if (key->expiry && now.tv_sec >= key->expiry)
405 /* keys that don't match */
406 if (!match(key, description))
409 /* key must have search permissions */
410 if (key_task_permission(make_key_ref(key, possessed),
411 context, KEY_SEARCH) < 0)
414 /* we set a different error code if we find a negative key */
415 if (test_bit(KEY_FLAG_NEGATIVE, &key->flags)) {
423 /* search through the keyrings nested in this one */
426 for (; kix < keylist->nkeys; kix++) {
427 key = keylist->keys[kix];
428 if (key->type != &key_type_keyring)
431 /* recursively search nested keyrings
432 * - only search keyrings for which we have search permission
434 if (sp >= KEYRING_SEARCH_MAX_DEPTH)
437 if (key_task_permission(make_key_ref(key, possessed),
438 context, KEY_SEARCH) < 0)
441 /* stack the current position */
442 stack[sp].keylist = keylist;
446 /* begin again with the new keyring */
451 /* the keyring we're looking at was disqualified or didn't contain a
455 /* resume the processing of a keyring higher up in the tree */
457 keylist = stack[sp].keylist;
458 kix = stack[sp].kix + 1;
462 key_ref = ERR_PTR(err);
465 /* we found a viable match */
467 atomic_inc(&key->usage);
469 key_ref = make_key_ref(key, possessed);
475 } /* end keyring_search_aux() */
477 /*****************************************************************************/
479 * search the supplied keyring tree for a key that matches the criterion
480 * - perform a breadth-then-depth search up to the prescribed limit
481 * - we only find keys on which we have search permission
482 * - we readlock the keyrings as we search down the tree
483 * - we return -EAGAIN if we didn't find any matching key
484 * - we return -ENOKEY if we only found negative matching keys
486 key_ref_t keyring_search(key_ref_t keyring,
487 struct key_type *type,
488 const char *description)
491 return ERR_PTR(-ENOKEY);
493 return keyring_search_aux(keyring, current,
494 type, description, type->match);
496 } /* end keyring_search() */
498 EXPORT_SYMBOL(keyring_search);
500 /*****************************************************************************/
502 * search the given keyring only (no recursion)
503 * - keyring must be locked by caller
505 key_ref_t __keyring_search_one(key_ref_t keyring_ref,
506 const struct key_type *ktype,
507 const char *description,
510 struct keyring_list *klist;
511 unsigned long possessed;
512 struct key *keyring, *key;
515 keyring = key_ref_to_ptr(keyring_ref);
516 possessed = is_key_possessed(keyring_ref);
520 klist = rcu_dereference(keyring->payload.subscriptions);
522 for (loop = 0; loop < klist->nkeys; loop++) {
523 key = klist->keys[loop];
525 if (key->type == ktype &&
526 (!key->type->match ||
527 key->type->match(key, description)) &&
528 key_permission(make_key_ref(key, possessed),
530 !test_bit(KEY_FLAG_REVOKED, &key->flags)
537 return ERR_PTR(-ENOKEY);
540 atomic_inc(&key->usage);
542 return make_key_ref(key, possessed);
544 } /* end __keyring_search_one() */
546 /*****************************************************************************/
548 * search for an instantiation authorisation key matching a target key
549 * - the RCU read lock must be held by the caller
550 * - a target_id of zero specifies any valid token
552 struct key *keyring_search_instkey(struct key *keyring,
553 key_serial_t target_id)
555 struct request_key_auth *rka;
556 struct keyring_list *klist;
560 klist = rcu_dereference(keyring->payload.subscriptions);
562 for (loop = 0; loop < klist->nkeys; loop++) {
563 instkey = klist->keys[loop];
565 if (instkey->type != &key_type_request_key_auth)
568 rka = instkey->payload.data;
569 if (target_id && rka->target_key->serial != target_id)
572 /* the auth key is revoked during instantiation */
573 if (!test_bit(KEY_FLAG_REVOKED, &instkey->flags))
576 instkey = ERR_PTR(-EKEYREVOKED);
581 instkey = ERR_PTR(-EACCES);
585 atomic_inc(&instkey->usage);
589 } /* end keyring_search_instkey() */
591 /*****************************************************************************/
593 * find a keyring with the specified name
594 * - all named keyrings are searched
595 * - only find keyrings with search permission for the process
596 * - only find keyrings with a serial number greater than the one specified
598 struct key *find_keyring_by_name(const char *name, key_serial_t bound)
603 keyring = ERR_PTR(-EINVAL);
607 bucket = keyring_hash(name);
609 read_lock(&keyring_name_lock);
611 if (keyring_name_hash[bucket].next) {
612 /* search this hash bucket for a keyring with a matching name
613 * that's readable and that hasn't been revoked */
614 list_for_each_entry(keyring,
615 &keyring_name_hash[bucket],
618 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
621 if (strcmp(keyring->description, name) != 0)
624 if (key_permission(make_key_ref(keyring, 0),
628 /* found a potential candidate, but we still need to
629 * check the serial number */
630 if (keyring->serial <= bound)
633 /* we've got a match */
634 atomic_inc(&keyring->usage);
635 read_unlock(&keyring_name_lock);
640 read_unlock(&keyring_name_lock);
641 keyring = ERR_PTR(-ENOKEY);
646 } /* end find_keyring_by_name() */
648 /*****************************************************************************/
650 * see if a cycle will will be created by inserting acyclic tree B in acyclic
651 * tree A at the topmost level (ie: as a direct child of A)
652 * - since we are adding B to A at the top level, checking for cycles should
653 * just be a matter of seeing if node A is somewhere in tree B
655 static int keyring_detect_cycle(struct key *A, struct key *B)
658 struct keyring_list *keylist;
660 } stack[KEYRING_SEARCH_MAX_DEPTH];
662 struct keyring_list *keylist;
663 struct key *subtree, *key;
675 /* start processing a new keyring */
677 if (test_bit(KEY_FLAG_REVOKED, &subtree->flags))
678 goto not_this_keyring;
680 keylist = rcu_dereference(subtree->payload.subscriptions);
682 goto not_this_keyring;
686 /* iterate through the remaining keys in this keyring */
687 for (; kix < keylist->nkeys; kix++) {
688 key = keylist->keys[kix];
693 /* recursively check nested keyrings */
694 if (key->type == &key_type_keyring) {
695 if (sp >= KEYRING_SEARCH_MAX_DEPTH)
698 /* stack the current position */
699 stack[sp].keylist = keylist;
703 /* begin again with the new keyring */
709 /* the keyring we're looking at was disqualified or didn't contain a
713 /* resume the checking of a keyring higher up in the tree */
715 keylist = stack[sp].keylist;
716 kix = stack[sp].kix + 1;
720 ret = 0; /* no cycles detected */
734 } /* end keyring_detect_cycle() */
736 /*****************************************************************************/
738 * dispose of a keyring list after the RCU grace period
740 static void keyring_link_rcu_disposal(struct rcu_head *rcu)
742 struct keyring_list *klist =
743 container_of(rcu, struct keyring_list, rcu);
747 } /* end keyring_link_rcu_disposal() */
749 /*****************************************************************************/
751 * link a key into to a keyring
752 * - must be called with the keyring's semaphore write-locked
754 int __key_link(struct key *keyring, struct key *key)
756 struct keyring_list *klist, *nklist;
762 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
766 if (keyring->type != &key_type_keyring)
769 /* serialise link/link calls to prevent parallel calls causing a
770 * cycle when applied to two keyring in opposite orders */
771 down_write(&keyring_serialise_link_sem);
773 /* check that we aren't going to create a cycle adding one keyring to
775 if (key->type == &key_type_keyring) {
776 ret = keyring_detect_cycle(keyring, key);
781 /* check that we aren't going to overrun the user's quota */
782 ret = key_payload_reserve(keyring,
783 keyring->datalen + KEYQUOTA_LINK_BYTES);
787 klist = keyring->payload.subscriptions;
789 if (klist && klist->nkeys < klist->maxkeys) {
790 /* there's sufficient slack space to add directly */
791 atomic_inc(&key->usage);
793 klist->keys[klist->nkeys] = key;
801 /* grow the key list */
804 max += klist->maxkeys;
809 size = sizeof(*klist) + sizeof(struct key *) * max;
810 if (size > PAGE_SIZE)
814 nklist = kmalloc(size, GFP_KERNEL);
817 nklist->maxkeys = max;
821 nklist->nkeys = klist->nkeys;
824 sizeof(struct key *) * klist->nkeys);
827 /* add the key into the new space */
828 atomic_inc(&key->usage);
829 nklist->keys[nklist->nkeys++] = key;
831 rcu_assign_pointer(keyring->payload.subscriptions, nklist);
833 /* dispose of the old keyring list */
835 call_rcu(&klist->rcu, keyring_link_rcu_disposal);
841 up_write(&keyring_serialise_link_sem);
846 /* undo the quota changes */
847 key_payload_reserve(keyring,
848 keyring->datalen - KEYQUOTA_LINK_BYTES);
851 } /* end __key_link() */
853 /*****************************************************************************/
855 * link a key to a keyring
857 int key_link(struct key *keyring, struct key *key)
864 down_write(&keyring->sem);
865 ret = __key_link(keyring, key);
866 up_write(&keyring->sem);
870 } /* end key_link() */
872 EXPORT_SYMBOL(key_link);
874 /*****************************************************************************/
876 * dispose of a keyring list after the RCU grace period, freeing the unlinked
879 static void keyring_unlink_rcu_disposal(struct rcu_head *rcu)
881 struct keyring_list *klist =
882 container_of(rcu, struct keyring_list, rcu);
884 key_put(klist->keys[klist->delkey]);
887 } /* end keyring_unlink_rcu_disposal() */
889 /*****************************************************************************/
891 * unlink the first link to a key from a keyring
893 int key_unlink(struct key *keyring, struct key *key)
895 struct keyring_list *klist, *nklist;
902 if (keyring->type != &key_type_keyring)
905 down_write(&keyring->sem);
907 klist = keyring->payload.subscriptions;
909 /* search the keyring for the key */
910 for (loop = 0; loop < klist->nkeys; loop++)
911 if (klist->keys[loop] == key)
915 up_write(&keyring->sem);
920 /* we need to copy the key list for RCU purposes */
921 nklist = kmalloc(sizeof(*klist) +
922 sizeof(struct key *) * klist->maxkeys,
926 nklist->maxkeys = klist->maxkeys;
927 nklist->nkeys = klist->nkeys - 1;
930 memcpy(&nklist->keys[0],
932 loop * sizeof(struct key *));
934 if (loop < nklist->nkeys)
935 memcpy(&nklist->keys[loop],
936 &klist->keys[loop + 1],
937 (nklist->nkeys - loop) * sizeof(struct key *));
939 /* adjust the user's quota */
940 key_payload_reserve(keyring,
941 keyring->datalen - KEYQUOTA_LINK_BYTES);
943 rcu_assign_pointer(keyring->payload.subscriptions, nklist);
945 up_write(&keyring->sem);
947 /* schedule for later cleanup */
948 klist->delkey = loop;
949 call_rcu(&klist->rcu, keyring_unlink_rcu_disposal);
957 up_write(&keyring->sem);
960 } /* end key_unlink() */
962 EXPORT_SYMBOL(key_unlink);
964 /*****************************************************************************/
966 * dispose of a keyring list after the RCU grace period, releasing the keys it
969 static void keyring_clear_rcu_disposal(struct rcu_head *rcu)
971 struct keyring_list *klist;
974 klist = container_of(rcu, struct keyring_list, rcu);
976 for (loop = klist->nkeys - 1; loop >= 0; loop--)
977 key_put(klist->keys[loop]);
981 } /* end keyring_clear_rcu_disposal() */
983 /*****************************************************************************/
985 * clear the specified process keyring
986 * - implements keyctl(KEYCTL_CLEAR)
988 int keyring_clear(struct key *keyring)
990 struct keyring_list *klist;
994 if (keyring->type == &key_type_keyring) {
995 /* detach the pointer block with the locks held */
996 down_write(&keyring->sem);
998 klist = keyring->payload.subscriptions;
1000 /* adjust the quota */
1001 key_payload_reserve(keyring,
1002 sizeof(struct keyring_list));
1004 rcu_assign_pointer(keyring->payload.subscriptions,
1008 up_write(&keyring->sem);
1010 /* free the keys after the locks have been dropped */
1012 call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
1019 } /* end keyring_clear() */
1021 EXPORT_SYMBOL(keyring_clear);