3 * Copyright (C) 2004-2005, 2008 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 <keys/keyring-type.h>
20 #include <linux/uaccess.h>
23 #define rcu_dereference_locked_keyring(keyring) \
24 (rcu_dereference_protected( \
25 (keyring)->payload.subscriptions, \
26 rwsem_is_locked((struct rw_semaphore *)&(keyring)->sem)))
29 * when plumbing the depths of the key tree, this sets a hard limit set on how
30 * deep we're willing to go
32 #define KEYRING_SEARCH_MAX_DEPTH 6
35 * we keep all named keyrings in a hash to speed looking them up
37 #define KEYRING_NAME_HASH_SIZE (1 << 5)
39 static struct list_head keyring_name_hash[KEYRING_NAME_HASH_SIZE];
40 static DEFINE_RWLOCK(keyring_name_lock);
42 static inline unsigned keyring_hash(const char *desc)
47 bucket += (unsigned char)*desc;
49 return bucket & (KEYRING_NAME_HASH_SIZE - 1);
53 * the keyring type definition
55 static int keyring_instantiate(struct key *keyring,
56 const void *data, size_t datalen);
57 static int keyring_match(const struct key *keyring, const void *criterion);
58 static void keyring_revoke(struct key *keyring);
59 static void keyring_destroy(struct key *keyring);
60 static void keyring_describe(const struct key *keyring, struct seq_file *m);
61 static long keyring_read(const struct key *keyring,
62 char __user *buffer, size_t buflen);
64 struct key_type key_type_keyring = {
66 .def_datalen = sizeof(struct keyring_list),
67 .instantiate = keyring_instantiate,
68 .match = keyring_match,
69 .revoke = keyring_revoke,
70 .destroy = keyring_destroy,
71 .describe = keyring_describe,
75 EXPORT_SYMBOL(key_type_keyring);
78 * semaphore to serialise link/link calls to prevent two link calls in parallel
81 static DECLARE_RWSEM(keyring_serialise_link_sem);
83 /*****************************************************************************/
85 * publish the name of a keyring so that it can be found by name (if it has
88 static void keyring_publish_name(struct key *keyring)
92 if (keyring->description) {
93 bucket = keyring_hash(keyring->description);
95 write_lock(&keyring_name_lock);
97 if (!keyring_name_hash[bucket].next)
98 INIT_LIST_HEAD(&keyring_name_hash[bucket]);
100 list_add_tail(&keyring->type_data.link,
101 &keyring_name_hash[bucket]);
103 write_unlock(&keyring_name_lock);
106 } /* end keyring_publish_name() */
108 /*****************************************************************************/
110 * initialise a keyring
111 * - we object if we were given any data
113 static int keyring_instantiate(struct key *keyring,
114 const void *data, size_t datalen)
120 /* make the keyring available by name if it has one */
121 keyring_publish_name(keyring);
127 } /* end keyring_instantiate() */
129 /*****************************************************************************/
131 * match keyrings on their name
133 static int keyring_match(const struct key *keyring, const void *description)
135 return keyring->description &&
136 strcmp(keyring->description, description) == 0;
138 } /* end keyring_match() */
140 /*****************************************************************************/
142 * dispose of the data dangling from the corpse of a keyring
144 static void keyring_destroy(struct key *keyring)
146 struct keyring_list *klist;
149 if (keyring->description) {
150 write_lock(&keyring_name_lock);
152 if (keyring->type_data.link.next != NULL &&
153 !list_empty(&keyring->type_data.link))
154 list_del(&keyring->type_data.link);
156 write_unlock(&keyring_name_lock);
159 klist = rcu_dereference_check(keyring->payload.subscriptions,
160 rcu_read_lock_held() ||
161 atomic_read(&keyring->usage) == 0);
163 for (loop = klist->nkeys - 1; loop >= 0; loop--)
164 key_put(klist->keys[loop]);
168 } /* end keyring_destroy() */
170 /*****************************************************************************/
172 * describe the keyring
174 static void keyring_describe(const struct key *keyring, struct seq_file *m)
176 struct keyring_list *klist;
178 if (keyring->description)
179 seq_puts(m, keyring->description);
181 seq_puts(m, "[anon]");
184 klist = rcu_dereference(keyring->payload.subscriptions);
186 seq_printf(m, ": %u/%u", klist->nkeys, klist->maxkeys);
188 seq_puts(m, ": empty");
191 } /* end keyring_describe() */
193 /*****************************************************************************/
195 * read a list of key IDs from the keyring's contents
196 * - the keyring's semaphore is read-locked
198 static long keyring_read(const struct key *keyring,
199 char __user *buffer, size_t buflen)
201 struct keyring_list *klist;
207 klist = rcu_dereference_locked_keyring(keyring);
209 /* calculate how much data we could return */
210 qty = klist->nkeys * sizeof(key_serial_t);
212 if (buffer && buflen > 0) {
216 /* copy the IDs of the subscribed keys into the
220 for (loop = 0; loop < klist->nkeys; loop++) {
221 key = klist->keys[loop];
223 tmp = sizeof(key_serial_t);
227 if (copy_to_user(buffer,
245 } /* end keyring_read() */
247 /*****************************************************************************/
249 * allocate a keyring and link into the destination keyring
251 struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
252 const struct cred *cred, unsigned long flags,
258 keyring = key_alloc(&key_type_keyring, description,
260 (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
263 if (!IS_ERR(keyring)) {
264 ret = key_instantiate_and_link(keyring, NULL, 0, dest, NULL);
267 keyring = ERR_PTR(ret);
273 } /* end keyring_alloc() */
275 /*****************************************************************************/
277 * search the supplied keyring tree for a key that matches the criterion
278 * - perform a breadth-then-depth search up to the prescribed limit
279 * - we only find keys on which we have search permission
280 * - we use the supplied match function to see if the description (or other
281 * feature of interest) matches
282 * - we rely on RCU to prevent the keyring lists from disappearing on us
283 * - we return -EAGAIN if we didn't find any matching key
284 * - we return -ENOKEY if we only found negative matching keys
285 * - we propagate the possession attribute from the keyring ref to the key ref
287 key_ref_t keyring_search_aux(key_ref_t keyring_ref,
288 const struct cred *cred,
289 struct key_type *type,
290 const void *description,
291 key_match_func_t match)
294 struct keyring_list *keylist;
296 } stack[KEYRING_SEARCH_MAX_DEPTH];
298 struct keyring_list *keylist;
300 unsigned long possessed, kflags;
301 struct key *keyring, *key;
306 keyring = key_ref_to_ptr(keyring_ref);
307 possessed = is_key_possessed(keyring_ref);
310 /* top keyring must have search permission to begin the search */
311 err = key_task_permission(keyring_ref, cred, KEY_SEARCH);
313 key_ref = ERR_PTR(err);
317 key_ref = ERR_PTR(-ENOTDIR);
318 if (keyring->type != &key_type_keyring)
323 now = current_kernel_time();
327 /* firstly we should check to see if this top-level keyring is what we
329 key_ref = ERR_PTR(-EAGAIN);
330 kflags = keyring->flags;
331 if (keyring->type == type && match(keyring, description)) {
334 /* check it isn't negative and hasn't expired or been
336 if (kflags & (1 << KEY_FLAG_REVOKED))
338 if (key->expiry && now.tv_sec >= key->expiry)
340 key_ref = ERR_PTR(-ENOKEY);
341 if (kflags & (1 << KEY_FLAG_NEGATIVE))
346 /* otherwise, the top keyring must not be revoked, expired, or
347 * negatively instantiated if we are to search it */
348 key_ref = ERR_PTR(-EAGAIN);
349 if (kflags & ((1 << KEY_FLAG_REVOKED) | (1 << KEY_FLAG_NEGATIVE)) ||
350 (keyring->expiry && now.tv_sec >= keyring->expiry))
353 /* start processing a new keyring */
355 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
356 goto not_this_keyring;
358 keylist = rcu_dereference(keyring->payload.subscriptions);
360 goto not_this_keyring;
362 /* iterate through the keys in this keyring first */
363 for (kix = 0; kix < keylist->nkeys; kix++) {
364 key = keylist->keys[kix];
367 /* ignore keys not of this type */
368 if (key->type != type)
371 /* skip revoked keys and expired keys */
372 if (kflags & (1 << KEY_FLAG_REVOKED))
375 if (key->expiry && now.tv_sec >= key->expiry)
378 /* keys that don't match */
379 if (!match(key, description))
382 /* key must have search permissions */
383 if (key_task_permission(make_key_ref(key, possessed),
384 cred, KEY_SEARCH) < 0)
387 /* we set a different error code if we pass a negative key */
388 if (kflags & (1 << KEY_FLAG_NEGATIVE)) {
396 /* search through the keyrings nested in this one */
399 for (; kix < keylist->nkeys; kix++) {
400 key = keylist->keys[kix];
401 if (key->type != &key_type_keyring)
404 /* recursively search nested keyrings
405 * - only search keyrings for which we have search permission
407 if (sp >= KEYRING_SEARCH_MAX_DEPTH)
410 if (key_task_permission(make_key_ref(key, possessed),
411 cred, KEY_SEARCH) < 0)
414 /* stack the current position */
415 stack[sp].keylist = keylist;
419 /* begin again with the new keyring */
424 /* the keyring we're looking at was disqualified or didn't contain a
428 /* resume the processing of a keyring higher up in the tree */
430 keylist = stack[sp].keylist;
431 kix = stack[sp].kix + 1;
435 key_ref = ERR_PTR(err);
438 /* we found a viable match */
440 atomic_inc(&key->usage);
442 key_ref = make_key_ref(key, possessed);
448 } /* end keyring_search_aux() */
450 /*****************************************************************************/
452 * search the supplied keyring tree for a key that matches the criterion
453 * - perform a breadth-then-depth search up to the prescribed limit
454 * - we only find keys on which we have search permission
455 * - we readlock the keyrings as we search down the tree
456 * - we return -EAGAIN if we didn't find any matching key
457 * - we return -ENOKEY if we only found negative matching keys
459 key_ref_t keyring_search(key_ref_t keyring,
460 struct key_type *type,
461 const char *description)
464 return ERR_PTR(-ENOKEY);
466 return keyring_search_aux(keyring, current->cred,
467 type, description, type->match);
469 } /* end keyring_search() */
471 EXPORT_SYMBOL(keyring_search);
473 /*****************************************************************************/
475 * search the given keyring only (no recursion)
476 * - keyring must be locked by caller
477 * - caller must guarantee that the keyring is a keyring
479 key_ref_t __keyring_search_one(key_ref_t keyring_ref,
480 const struct key_type *ktype,
481 const char *description,
484 struct keyring_list *klist;
485 unsigned long possessed;
486 struct key *keyring, *key;
489 keyring = key_ref_to_ptr(keyring_ref);
490 possessed = is_key_possessed(keyring_ref);
494 klist = rcu_dereference(keyring->payload.subscriptions);
496 for (loop = 0; loop < klist->nkeys; loop++) {
497 key = klist->keys[loop];
499 if (key->type == ktype &&
500 (!key->type->match ||
501 key->type->match(key, description)) &&
502 key_permission(make_key_ref(key, possessed),
504 !test_bit(KEY_FLAG_REVOKED, &key->flags)
511 return ERR_PTR(-ENOKEY);
514 atomic_inc(&key->usage);
516 return make_key_ref(key, possessed);
518 } /* end __keyring_search_one() */
520 /*****************************************************************************/
522 * find a keyring with the specified name
523 * - all named keyrings are searched
524 * - normally only finds keyrings with search permission for the current process
526 struct key *find_keyring_by_name(const char *name, bool skip_perm_check)
532 return ERR_PTR(-EINVAL);
534 bucket = keyring_hash(name);
536 read_lock(&keyring_name_lock);
538 if (keyring_name_hash[bucket].next) {
539 /* search this hash bucket for a keyring with a matching name
540 * that's readable and that hasn't been revoked */
541 list_for_each_entry(keyring,
542 &keyring_name_hash[bucket],
545 if (keyring->user->user_ns != current_user_ns())
548 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
551 if (strcmp(keyring->description, name) != 0)
554 if (!skip_perm_check &&
555 key_permission(make_key_ref(keyring, 0),
559 /* we've got a match but we might end up racing with
560 * key_cleanup() if the keyring is currently 'dead'
561 * (ie. it has a zero usage count) */
562 if (!atomic_inc_not_zero(&keyring->usage))
568 keyring = ERR_PTR(-ENOKEY);
570 read_unlock(&keyring_name_lock);
573 } /* end find_keyring_by_name() */
575 /*****************************************************************************/
577 * see if a cycle will will be created by inserting acyclic tree B in acyclic
578 * tree A at the topmost level (ie: as a direct child of A)
579 * - since we are adding B to A at the top level, checking for cycles should
580 * just be a matter of seeing if node A is somewhere in tree B
582 static int keyring_detect_cycle(struct key *A, struct key *B)
585 struct keyring_list *keylist;
587 } stack[KEYRING_SEARCH_MAX_DEPTH];
589 struct keyring_list *keylist;
590 struct key *subtree, *key;
602 /* start processing a new keyring */
604 if (test_bit(KEY_FLAG_REVOKED, &subtree->flags))
605 goto not_this_keyring;
607 keylist = rcu_dereference(subtree->payload.subscriptions);
609 goto not_this_keyring;
613 /* iterate through the remaining keys in this keyring */
614 for (; kix < keylist->nkeys; kix++) {
615 key = keylist->keys[kix];
620 /* recursively check nested keyrings */
621 if (key->type == &key_type_keyring) {
622 if (sp >= KEYRING_SEARCH_MAX_DEPTH)
625 /* stack the current position */
626 stack[sp].keylist = keylist;
630 /* begin again with the new keyring */
636 /* the keyring we're looking at was disqualified or didn't contain a
640 /* resume the checking of a keyring higher up in the tree */
642 keylist = stack[sp].keylist;
643 kix = stack[sp].kix + 1;
647 ret = 0; /* no cycles detected */
661 } /* end keyring_detect_cycle() */
663 /*****************************************************************************/
665 * dispose of a keyring list after the RCU grace period
667 static void keyring_link_rcu_disposal(struct rcu_head *rcu)
669 struct keyring_list *klist =
670 container_of(rcu, struct keyring_list, rcu);
674 } /* end keyring_link_rcu_disposal() */
676 /*****************************************************************************/
678 * dispose of a keyring list after the RCU grace period, freeing the unlinked
681 static void keyring_unlink_rcu_disposal(struct rcu_head *rcu)
683 struct keyring_list *klist =
684 container_of(rcu, struct keyring_list, rcu);
686 key_put(klist->keys[klist->delkey]);
689 } /* end keyring_unlink_rcu_disposal() */
691 /*****************************************************************************/
693 * link a key into to a keyring
694 * - must be called with the keyring's semaphore write-locked
695 * - discard already extant link to matching key if there is one
697 int __key_link(struct key *keyring, struct key *key)
699 struct keyring_list *klist, *nklist;
705 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
709 if (keyring->type != &key_type_keyring)
712 /* do some special keyring->keyring link checks */
713 if (key->type == &key_type_keyring) {
714 /* serialise link/link calls to prevent parallel calls causing
715 * a cycle when applied to two keyring in opposite orders */
716 down_write(&keyring_serialise_link_sem);
718 /* check that we aren't going to create a cycle adding one
719 * keyring to another */
720 ret = keyring_detect_cycle(keyring, key);
725 /* see if there's a matching key we can displace */
726 klist = rcu_dereference_locked_keyring(keyring);
727 if (klist && klist->nkeys > 0) {
728 struct key_type *type = key->type;
730 for (loop = klist->nkeys - 1; loop >= 0; loop--) {
731 if (klist->keys[loop]->type == type &&
732 strcmp(klist->keys[loop]->description,
733 key->description) == 0
735 /* found a match - replace with new key */
736 size = sizeof(struct key *) * klist->maxkeys;
737 size += sizeof(*klist);
738 BUG_ON(size > PAGE_SIZE);
741 nklist = kmemdup(klist, size, GFP_KERNEL);
745 /* replace matched key */
746 atomic_inc(&key->usage);
747 nklist->keys[loop] = key;
750 keyring->payload.subscriptions,
753 /* dispose of the old keyring list and the
755 klist->delkey = loop;
756 call_rcu(&klist->rcu,
757 keyring_unlink_rcu_disposal);
764 /* check that we aren't going to overrun the user's quota */
765 ret = key_payload_reserve(keyring,
766 keyring->datalen + KEYQUOTA_LINK_BYTES);
770 if (klist && klist->nkeys < klist->maxkeys) {
771 /* there's sufficient slack space to add directly */
772 atomic_inc(&key->usage);
774 klist->keys[klist->nkeys] = key;
779 /* grow the key list */
782 max += klist->maxkeys;
787 size = sizeof(*klist) + sizeof(struct key *) * max;
788 if (size > PAGE_SIZE)
792 nklist = kmalloc(size, GFP_KERNEL);
795 nklist->maxkeys = max;
799 nklist->nkeys = klist->nkeys;
802 sizeof(struct key *) * klist->nkeys);
805 /* add the key into the new space */
806 atomic_inc(&key->usage);
807 nklist->keys[nklist->nkeys++] = key;
809 rcu_assign_pointer(keyring->payload.subscriptions, nklist);
811 /* dispose of the old keyring list */
813 call_rcu(&klist->rcu, keyring_link_rcu_disposal);
819 if (key->type == &key_type_keyring)
820 up_write(&keyring_serialise_link_sem);
825 /* undo the quota changes */
826 key_payload_reserve(keyring,
827 keyring->datalen - KEYQUOTA_LINK_BYTES);
830 } /* end __key_link() */
832 /*****************************************************************************/
834 * link a key to a keyring
836 int key_link(struct key *keyring, struct key *key)
843 down_write(&keyring->sem);
844 ret = __key_link(keyring, key);
845 up_write(&keyring->sem);
849 } /* end key_link() */
851 EXPORT_SYMBOL(key_link);
853 /*****************************************************************************/
855 * unlink the first link to a key from a keyring
857 int key_unlink(struct key *keyring, struct key *key)
859 struct keyring_list *klist, *nklist;
866 if (keyring->type != &key_type_keyring)
869 down_write(&keyring->sem);
871 klist = rcu_dereference_locked_keyring(keyring);
873 /* search the keyring for the key */
874 for (loop = 0; loop < klist->nkeys; loop++)
875 if (klist->keys[loop] == key)
879 up_write(&keyring->sem);
884 /* we need to copy the key list for RCU purposes */
885 nklist = kmalloc(sizeof(*klist) +
886 sizeof(struct key *) * klist->maxkeys,
890 nklist->maxkeys = klist->maxkeys;
891 nklist->nkeys = klist->nkeys - 1;
894 memcpy(&nklist->keys[0],
896 loop * sizeof(struct key *));
898 if (loop < nklist->nkeys)
899 memcpy(&nklist->keys[loop],
900 &klist->keys[loop + 1],
901 (nklist->nkeys - loop) * sizeof(struct key *));
903 /* adjust the user's quota */
904 key_payload_reserve(keyring,
905 keyring->datalen - KEYQUOTA_LINK_BYTES);
907 rcu_assign_pointer(keyring->payload.subscriptions, nklist);
909 up_write(&keyring->sem);
911 /* schedule for later cleanup */
912 klist->delkey = loop;
913 call_rcu(&klist->rcu, keyring_unlink_rcu_disposal);
921 up_write(&keyring->sem);
924 } /* end key_unlink() */
926 EXPORT_SYMBOL(key_unlink);
928 /*****************************************************************************/
930 * dispose of a keyring list after the RCU grace period, releasing the keys it
933 static void keyring_clear_rcu_disposal(struct rcu_head *rcu)
935 struct keyring_list *klist;
938 klist = container_of(rcu, struct keyring_list, rcu);
940 for (loop = klist->nkeys - 1; loop >= 0; loop--)
941 key_put(klist->keys[loop]);
945 } /* end keyring_clear_rcu_disposal() */
947 /*****************************************************************************/
949 * clear the specified process keyring
950 * - implements keyctl(KEYCTL_CLEAR)
952 int keyring_clear(struct key *keyring)
954 struct keyring_list *klist;
958 if (keyring->type == &key_type_keyring) {
959 /* detach the pointer block with the locks held */
960 down_write(&keyring->sem);
962 klist = rcu_dereference_locked_keyring(keyring);
964 /* adjust the quota */
965 key_payload_reserve(keyring,
966 sizeof(struct keyring_list));
968 rcu_assign_pointer(keyring->payload.subscriptions,
972 up_write(&keyring->sem);
974 /* free the keys after the locks have been dropped */
976 call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
983 } /* end keyring_clear() */
985 EXPORT_SYMBOL(keyring_clear);
987 /*****************************************************************************/
989 * dispose of the links from a revoked keyring
990 * - called with the key sem write-locked
992 static void keyring_revoke(struct key *keyring)
994 struct keyring_list *klist;
996 klist = rcu_dereference_locked_keyring(keyring);
998 /* adjust the quota */
999 key_payload_reserve(keyring, 0);
1002 rcu_assign_pointer(keyring->payload.subscriptions, NULL);
1003 call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
1006 } /* end keyring_revoke() */
1009 * Determine whether a key is dead
1011 static bool key_is_dead(struct key *key, time_t limit)
1013 return test_bit(KEY_FLAG_DEAD, &key->flags) ||
1014 (key->expiry > 0 && key->expiry <= limit);
1018 * Collect garbage from the contents of a keyring
1020 void keyring_gc(struct key *keyring, time_t limit)
1022 struct keyring_list *klist, *new;
1024 int loop, keep, max;
1026 kenter("{%x,%s}", key_serial(keyring), keyring->description);
1028 down_write(&keyring->sem);
1030 klist = rcu_dereference_locked_keyring(keyring);
1034 /* work out how many subscriptions we're keeping */
1036 for (loop = klist->nkeys - 1; loop >= 0; loop--)
1037 if (!key_is_dead(klist->keys[loop], limit))
1040 if (keep == klist->nkeys)
1043 /* allocate a new keyring payload */
1044 max = roundup(keep, 4);
1045 new = kmalloc(sizeof(struct keyring_list) + max * sizeof(struct key *),
1053 /* install the live keys
1054 * - must take care as expired keys may be updated back to life
1057 for (loop = klist->nkeys - 1; loop >= 0; loop--) {
1058 key = klist->keys[loop];
1059 if (!key_is_dead(key, limit)) {
1062 new->keys[keep++] = key_get(key);
1067 /* adjust the quota */
1068 key_payload_reserve(keyring,
1069 sizeof(struct keyring_list) +
1070 KEYQUOTA_LINK_BYTES * keep);
1073 rcu_assign_pointer(keyring->payload.subscriptions, NULL);
1076 rcu_assign_pointer(keyring->payload.subscriptions, new);
1079 up_write(&keyring->sem);
1081 call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
1087 keyring_clear_rcu_disposal(&new->rcu);
1088 up_write(&keyring->sem);
1089 kleave(" [discard]");
1093 up_write(&keyring->sem);
1094 kleave(" [no dead]");
1098 up_write(&keyring->sem);
1099 kleave(" [no_klist]");
1103 up_write(&keyring->sem);