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_match(const struct key *keyring, const void *criterion);
52 static void keyring_revoke(struct key *keyring);
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 .match = keyring_match,
63 .revoke = keyring_revoke,
64 .destroy = keyring_destroy,
65 .describe = keyring_describe,
69 EXPORT_SYMBOL(key_type_keyring);
72 * semaphore to serialise link/link calls to prevent two link calls in parallel
75 static DECLARE_RWSEM(keyring_serialise_link_sem);
77 /*****************************************************************************/
79 * publish the name of a keyring so that it can be found by name (if it has
82 void keyring_publish_name(struct key *keyring)
86 if (keyring->description) {
87 bucket = keyring_hash(keyring->description);
89 write_lock(&keyring_name_lock);
91 if (!keyring_name_hash[bucket].next)
92 INIT_LIST_HEAD(&keyring_name_hash[bucket]);
94 list_add_tail(&keyring->type_data.link,
95 &keyring_name_hash[bucket]);
97 write_unlock(&keyring_name_lock);
100 } /* end keyring_publish_name() */
102 /*****************************************************************************/
104 * initialise a keyring
105 * - we object if we were given any data
107 static int keyring_instantiate(struct key *keyring,
108 const void *data, size_t datalen)
114 /* make the keyring available by name if it has one */
115 keyring_publish_name(keyring);
121 } /* end keyring_instantiate() */
123 /*****************************************************************************/
125 * match keyrings on their name
127 static int keyring_match(const struct key *keyring, const void *description)
129 return keyring->description &&
130 strcmp(keyring->description, description) == 0;
132 } /* end keyring_match() */
134 /*****************************************************************************/
136 * dispose of the data dangling from the corpse of a keyring
138 static void keyring_destroy(struct key *keyring)
140 struct keyring_list *klist;
143 if (keyring->description) {
144 write_lock(&keyring_name_lock);
146 if (keyring->type_data.link.next != NULL &&
147 !list_empty(&keyring->type_data.link))
148 list_del(&keyring->type_data.link);
150 write_unlock(&keyring_name_lock);
153 klist = rcu_dereference(keyring->payload.subscriptions);
155 for (loop = klist->nkeys - 1; loop >= 0; loop--)
156 key_put(klist->keys[loop]);
160 } /* end keyring_destroy() */
162 /*****************************************************************************/
164 * describe the keyring
166 static void keyring_describe(const struct key *keyring, struct seq_file *m)
168 struct keyring_list *klist;
170 if (keyring->description) {
171 seq_puts(m, keyring->description);
174 seq_puts(m, "[anon]");
178 klist = rcu_dereference(keyring->payload.subscriptions);
180 seq_printf(m, ": %u/%u", klist->nkeys, klist->maxkeys);
182 seq_puts(m, ": empty");
185 } /* end keyring_describe() */
187 /*****************************************************************************/
189 * read a list of key IDs from the keyring's contents
190 * - the keyring's semaphore is read-locked
192 static long keyring_read(const struct key *keyring,
193 char __user *buffer, size_t buflen)
195 struct keyring_list *klist;
201 klist = rcu_dereference(keyring->payload.subscriptions);
204 /* calculate how much data we could return */
205 qty = klist->nkeys * sizeof(key_serial_t);
207 if (buffer && buflen > 0) {
211 /* copy the IDs of the subscribed keys into the
215 for (loop = 0; loop < klist->nkeys; loop++) {
216 key = klist->keys[loop];
218 tmp = sizeof(key_serial_t);
222 if (copy_to_user(buffer,
240 } /* end keyring_read() */
242 /*****************************************************************************/
244 * allocate a keyring and link into the destination keyring
246 struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
247 struct task_struct *ctx, unsigned long flags,
253 keyring = key_alloc(&key_type_keyring, description,
255 (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
258 if (!IS_ERR(keyring)) {
259 ret = key_instantiate_and_link(keyring, NULL, 0, dest, NULL);
262 keyring = ERR_PTR(ret);
268 } /* end keyring_alloc() */
270 /*****************************************************************************/
272 * search the supplied keyring tree for a key that matches the criterion
273 * - perform a breadth-then-depth search up to the prescribed limit
274 * - we only find keys on which we have search permission
275 * - we use the supplied match function to see if the description (or other
276 * feature of interest) matches
277 * - we rely on RCU to prevent the keyring lists from disappearing on us
278 * - we return -EAGAIN if we didn't find any matching key
279 * - we return -ENOKEY if we only found negative matching keys
280 * - we propagate the possession attribute from the keyring ref to the key ref
282 key_ref_t keyring_search_aux(key_ref_t keyring_ref,
283 struct task_struct *context,
284 struct key_type *type,
285 const void *description,
286 key_match_func_t match)
289 struct keyring_list *keylist;
291 } stack[KEYRING_SEARCH_MAX_DEPTH];
293 struct keyring_list *keylist;
295 unsigned long possessed, kflags;
296 struct key *keyring, *key;
301 keyring = key_ref_to_ptr(keyring_ref);
302 possessed = is_key_possessed(keyring_ref);
305 /* top keyring must have search permission to begin the search */
306 err = key_task_permission(keyring_ref, context, KEY_SEARCH);
308 key_ref = ERR_PTR(err);
312 key_ref = ERR_PTR(-ENOTDIR);
313 if (keyring->type != &key_type_keyring)
318 now = current_kernel_time();
322 /* firstly we should check to see if this top-level keyring is what we
324 key_ref = ERR_PTR(-EAGAIN);
325 kflags = keyring->flags;
326 if (keyring->type == type && match(keyring, description)) {
329 /* check it isn't negative and hasn't expired or been
331 if (kflags & (1 << KEY_FLAG_REVOKED))
333 if (key->expiry && now.tv_sec >= key->expiry)
335 key_ref = ERR_PTR(-ENOKEY);
336 if (kflags & (1 << KEY_FLAG_NEGATIVE))
341 /* otherwise, the top keyring must not be revoked, expired, or
342 * negatively instantiated if we are to search it */
343 key_ref = ERR_PTR(-EAGAIN);
344 if (kflags & ((1 << KEY_FLAG_REVOKED) | (1 << KEY_FLAG_NEGATIVE)) ||
345 (keyring->expiry && now.tv_sec >= keyring->expiry))
348 /* start processing a new keyring */
350 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
351 goto not_this_keyring;
353 keylist = rcu_dereference(keyring->payload.subscriptions);
355 goto not_this_keyring;
357 /* iterate through the keys in this keyring first */
358 for (kix = 0; kix < keylist->nkeys; kix++) {
359 key = keylist->keys[kix];
362 /* ignore keys not of this type */
363 if (key->type != type)
366 /* skip revoked keys and expired keys */
367 if (kflags & (1 << KEY_FLAG_REVOKED))
370 if (key->expiry && now.tv_sec >= key->expiry)
373 /* keys that don't match */
374 if (!match(key, description))
377 /* key must have search permissions */
378 if (key_task_permission(make_key_ref(key, possessed),
379 context, KEY_SEARCH) < 0)
382 /* we set a different error code if we pass a negative key */
383 if (kflags & (1 << KEY_FLAG_NEGATIVE)) {
391 /* search through the keyrings nested in this one */
394 for (; kix < keylist->nkeys; kix++) {
395 key = keylist->keys[kix];
396 if (key->type != &key_type_keyring)
399 /* recursively search nested keyrings
400 * - only search keyrings for which we have search permission
402 if (sp >= KEYRING_SEARCH_MAX_DEPTH)
405 if (key_task_permission(make_key_ref(key, possessed),
406 context, KEY_SEARCH) < 0)
409 /* stack the current position */
410 stack[sp].keylist = keylist;
414 /* begin again with the new keyring */
419 /* the keyring we're looking at was disqualified or didn't contain a
423 /* resume the processing of a keyring higher up in the tree */
425 keylist = stack[sp].keylist;
426 kix = stack[sp].kix + 1;
430 key_ref = ERR_PTR(err);
433 /* we found a viable match */
435 atomic_inc(&key->usage);
437 key_ref = make_key_ref(key, possessed);
443 } /* end keyring_search_aux() */
445 /*****************************************************************************/
447 * search the supplied keyring tree for a key that matches the criterion
448 * - perform a breadth-then-depth search up to the prescribed limit
449 * - we only find keys on which we have search permission
450 * - we readlock the keyrings as we search down the tree
451 * - we return -EAGAIN if we didn't find any matching key
452 * - we return -ENOKEY if we only found negative matching keys
454 key_ref_t keyring_search(key_ref_t keyring,
455 struct key_type *type,
456 const char *description)
459 return ERR_PTR(-ENOKEY);
461 return keyring_search_aux(keyring, current,
462 type, description, type->match);
464 } /* end keyring_search() */
466 EXPORT_SYMBOL(keyring_search);
468 /*****************************************************************************/
470 * search the given keyring only (no recursion)
471 * - keyring must be locked by caller
472 * - caller must guarantee that the keyring is a keyring
474 key_ref_t __keyring_search_one(key_ref_t keyring_ref,
475 const struct key_type *ktype,
476 const char *description,
479 struct keyring_list *klist;
480 unsigned long possessed;
481 struct key *keyring, *key;
484 keyring = key_ref_to_ptr(keyring_ref);
485 possessed = is_key_possessed(keyring_ref);
489 klist = rcu_dereference(keyring->payload.subscriptions);
491 for (loop = 0; loop < klist->nkeys; loop++) {
492 key = klist->keys[loop];
494 if (key->type == ktype &&
495 (!key->type->match ||
496 key->type->match(key, description)) &&
497 key_permission(make_key_ref(key, possessed),
499 !test_bit(KEY_FLAG_REVOKED, &key->flags)
506 return ERR_PTR(-ENOKEY);
509 atomic_inc(&key->usage);
511 return make_key_ref(key, possessed);
513 } /* end __keyring_search_one() */
515 /*****************************************************************************/
517 * find a keyring with the specified name
518 * - all named keyrings are searched
519 * - only find keyrings with search permission for the process
520 * - only find keyrings with a serial number greater than the one specified
522 struct key *find_keyring_by_name(const char *name, key_serial_t bound)
527 keyring = ERR_PTR(-EINVAL);
531 bucket = keyring_hash(name);
533 read_lock(&keyring_name_lock);
535 if (keyring_name_hash[bucket].next) {
536 /* search this hash bucket for a keyring with a matching name
537 * that's readable and that hasn't been revoked */
538 list_for_each_entry(keyring,
539 &keyring_name_hash[bucket],
542 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
545 if (strcmp(keyring->description, name) != 0)
548 if (key_permission(make_key_ref(keyring, 0),
552 /* found a potential candidate, but we still need to
553 * check the serial number */
554 if (keyring->serial <= bound)
557 /* we've got a match */
558 atomic_inc(&keyring->usage);
559 read_unlock(&keyring_name_lock);
564 read_unlock(&keyring_name_lock);
565 keyring = ERR_PTR(-ENOKEY);
570 } /* end find_keyring_by_name() */
572 /*****************************************************************************/
574 * see if a cycle will will be created by inserting acyclic tree B in acyclic
575 * tree A at the topmost level (ie: as a direct child of A)
576 * - since we are adding B to A at the top level, checking for cycles should
577 * just be a matter of seeing if node A is somewhere in tree B
579 static int keyring_detect_cycle(struct key *A, struct key *B)
582 struct keyring_list *keylist;
584 } stack[KEYRING_SEARCH_MAX_DEPTH];
586 struct keyring_list *keylist;
587 struct key *subtree, *key;
599 /* start processing a new keyring */
601 if (test_bit(KEY_FLAG_REVOKED, &subtree->flags))
602 goto not_this_keyring;
604 keylist = rcu_dereference(subtree->payload.subscriptions);
606 goto not_this_keyring;
610 /* iterate through the remaining keys in this keyring */
611 for (; kix < keylist->nkeys; kix++) {
612 key = keylist->keys[kix];
617 /* recursively check nested keyrings */
618 if (key->type == &key_type_keyring) {
619 if (sp >= KEYRING_SEARCH_MAX_DEPTH)
622 /* stack the current position */
623 stack[sp].keylist = keylist;
627 /* begin again with the new keyring */
633 /* the keyring we're looking at was disqualified or didn't contain a
637 /* resume the checking of a keyring higher up in the tree */
639 keylist = stack[sp].keylist;
640 kix = stack[sp].kix + 1;
644 ret = 0; /* no cycles detected */
658 } /* end keyring_detect_cycle() */
660 /*****************************************************************************/
662 * dispose of a keyring list after the RCU grace period
664 static void keyring_link_rcu_disposal(struct rcu_head *rcu)
666 struct keyring_list *klist =
667 container_of(rcu, struct keyring_list, rcu);
671 } /* end keyring_link_rcu_disposal() */
673 /*****************************************************************************/
675 * dispose of a keyring list after the RCU grace period, freeing the unlinked
678 static void keyring_unlink_rcu_disposal(struct rcu_head *rcu)
680 struct keyring_list *klist =
681 container_of(rcu, struct keyring_list, rcu);
683 key_put(klist->keys[klist->delkey]);
686 } /* end keyring_unlink_rcu_disposal() */
688 /*****************************************************************************/
690 * link a key into to a keyring
691 * - must be called with the keyring's semaphore write-locked
692 * - discard already extant link to matching key if there is one
694 int __key_link(struct key *keyring, struct key *key)
696 struct keyring_list *klist, *nklist;
702 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
706 if (keyring->type != &key_type_keyring)
709 /* serialise link/link calls to prevent parallel calls causing a
710 * cycle when applied to two keyring in opposite orders */
711 down_write(&keyring_serialise_link_sem);
713 /* check that we aren't going to create a cycle adding one keyring to
715 if (key->type == &key_type_keyring) {
716 ret = keyring_detect_cycle(keyring, key);
721 /* see if there's a matching key we can displace */
722 klist = keyring->payload.subscriptions;
724 if (klist && klist->nkeys > 0) {
725 struct key_type *type = key->type;
727 for (loop = klist->nkeys - 1; loop >= 0; loop--) {
728 if (klist->keys[loop]->type == type &&
729 strcmp(klist->keys[loop]->description,
730 key->description) == 0
732 /* found a match - replace with new key */
733 size = sizeof(struct key *) * klist->maxkeys;
734 size += sizeof(*klist);
735 BUG_ON(size > PAGE_SIZE);
738 nklist = kmemdup(klist, size, GFP_KERNEL);
742 /* replace matched key */
743 atomic_inc(&key->usage);
744 nklist->keys[loop] = key;
747 keyring->payload.subscriptions,
750 /* dispose of the old keyring list and the
752 klist->delkey = loop;
753 call_rcu(&klist->rcu,
754 keyring_unlink_rcu_disposal);
761 /* check that we aren't going to overrun the user's quota */
762 ret = key_payload_reserve(keyring,
763 keyring->datalen + KEYQUOTA_LINK_BYTES);
767 klist = keyring->payload.subscriptions;
769 if (klist && klist->nkeys < klist->maxkeys) {
770 /* there's sufficient slack space to add directly */
771 atomic_inc(&key->usage);
773 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 up_write(&keyring_serialise_link_sem);
824 /* undo the quota changes */
825 key_payload_reserve(keyring,
826 keyring->datalen - KEYQUOTA_LINK_BYTES);
829 } /* end __key_link() */
831 /*****************************************************************************/
833 * link a key to a keyring
835 int key_link(struct key *keyring, struct key *key)
842 down_write(&keyring->sem);
843 ret = __key_link(keyring, key);
844 up_write(&keyring->sem);
848 } /* end key_link() */
850 EXPORT_SYMBOL(key_link);
852 /*****************************************************************************/
854 * unlink the first link to a key from a keyring
856 int key_unlink(struct key *keyring, struct key *key)
858 struct keyring_list *klist, *nklist;
865 if (keyring->type != &key_type_keyring)
868 down_write(&keyring->sem);
870 klist = keyring->payload.subscriptions;
872 /* search the keyring for the key */
873 for (loop = 0; loop < klist->nkeys; loop++)
874 if (klist->keys[loop] == key)
878 up_write(&keyring->sem);
883 /* we need to copy the key list for RCU purposes */
884 nklist = kmalloc(sizeof(*klist) +
885 sizeof(struct key *) * klist->maxkeys,
889 nklist->maxkeys = klist->maxkeys;
890 nklist->nkeys = klist->nkeys - 1;
893 memcpy(&nklist->keys[0],
895 loop * sizeof(struct key *));
897 if (loop < nklist->nkeys)
898 memcpy(&nklist->keys[loop],
899 &klist->keys[loop + 1],
900 (nklist->nkeys - loop) * sizeof(struct key *));
902 /* adjust the user's quota */
903 key_payload_reserve(keyring,
904 keyring->datalen - KEYQUOTA_LINK_BYTES);
906 rcu_assign_pointer(keyring->payload.subscriptions, nklist);
908 up_write(&keyring->sem);
910 /* schedule for later cleanup */
911 klist->delkey = loop;
912 call_rcu(&klist->rcu, keyring_unlink_rcu_disposal);
920 up_write(&keyring->sem);
923 } /* end key_unlink() */
925 EXPORT_SYMBOL(key_unlink);
927 /*****************************************************************************/
929 * dispose of a keyring list after the RCU grace period, releasing the keys it
932 static void keyring_clear_rcu_disposal(struct rcu_head *rcu)
934 struct keyring_list *klist;
937 klist = container_of(rcu, struct keyring_list, rcu);
939 for (loop = klist->nkeys - 1; loop >= 0; loop--)
940 key_put(klist->keys[loop]);
944 } /* end keyring_clear_rcu_disposal() */
946 /*****************************************************************************/
948 * clear the specified process keyring
949 * - implements keyctl(KEYCTL_CLEAR)
951 int keyring_clear(struct key *keyring)
953 struct keyring_list *klist;
957 if (keyring->type == &key_type_keyring) {
958 /* detach the pointer block with the locks held */
959 down_write(&keyring->sem);
961 klist = keyring->payload.subscriptions;
963 /* adjust the quota */
964 key_payload_reserve(keyring,
965 sizeof(struct keyring_list));
967 rcu_assign_pointer(keyring->payload.subscriptions,
971 up_write(&keyring->sem);
973 /* free the keys after the locks have been dropped */
975 call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
982 } /* end keyring_clear() */
984 EXPORT_SYMBOL(keyring_clear);
986 /*****************************************************************************/
988 * dispose of the links from a revoked keyring
989 * - called with the key sem write-locked
991 static void keyring_revoke(struct key *keyring)
993 struct keyring_list *klist = keyring->payload.subscriptions;
995 /* adjust the quota */
996 key_payload_reserve(keyring, 0);
999 rcu_assign_pointer(keyring->payload.subscriptions, NULL);
1000 call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
1003 } /* end keyring_revoke() */