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_destroy(struct key *keyring);
53 static void keyring_describe(const struct key *keyring, struct seq_file *m);
54 static long keyring_read(const struct key *keyring,
55 char __user *buffer, size_t buflen);
57 struct key_type key_type_keyring = {
59 .def_datalen = sizeof(struct keyring_list),
60 .instantiate = keyring_instantiate,
61 .match = keyring_match,
62 .destroy = keyring_destroy,
63 .describe = keyring_describe,
68 * semaphore to serialise link/link calls to prevent two link calls in parallel
71 static DECLARE_RWSEM(keyring_serialise_link_sem);
73 /*****************************************************************************/
75 * publish the name of a keyring so that it can be found by name (if it has
78 void keyring_publish_name(struct key *keyring)
82 if (keyring->description) {
83 bucket = keyring_hash(keyring->description);
85 write_lock(&keyring_name_lock);
87 if (!keyring_name_hash[bucket].next)
88 INIT_LIST_HEAD(&keyring_name_hash[bucket]);
90 list_add_tail(&keyring->type_data.link,
91 &keyring_name_hash[bucket]);
93 write_unlock(&keyring_name_lock);
96 } /* end keyring_publish_name() */
98 /*****************************************************************************/
100 * initialise a keyring
101 * - we object if we were given any data
103 static int keyring_instantiate(struct key *keyring,
104 const void *data, size_t datalen)
110 /* make the keyring available by name if it has one */
111 keyring_publish_name(keyring);
117 } /* end keyring_instantiate() */
119 /*****************************************************************************/
121 * match keyrings on their name
123 static int keyring_match(const struct key *keyring, const void *description)
125 return keyring->description &&
126 strcmp(keyring->description, description) == 0;
128 } /* end keyring_match() */
130 /*****************************************************************************/
132 * dispose of the data dangling from the corpse of a keyring
134 static void keyring_destroy(struct key *keyring)
136 struct keyring_list *klist;
139 if (keyring->description) {
140 write_lock(&keyring_name_lock);
142 if (keyring->type_data.link.next != NULL &&
143 !list_empty(&keyring->type_data.link))
144 list_del(&keyring->type_data.link);
146 write_unlock(&keyring_name_lock);
149 klist = rcu_dereference(keyring->payload.subscriptions);
151 for (loop = klist->nkeys - 1; loop >= 0; loop--)
152 key_put(klist->keys[loop]);
156 } /* end keyring_destroy() */
158 /*****************************************************************************/
160 * describe the keyring
162 static void keyring_describe(const struct key *keyring, struct seq_file *m)
164 struct keyring_list *klist;
166 if (keyring->description) {
167 seq_puts(m, keyring->description);
170 seq_puts(m, "[anon]");
174 klist = rcu_dereference(keyring->payload.subscriptions);
176 seq_printf(m, ": %u/%u", klist->nkeys, klist->maxkeys);
178 seq_puts(m, ": empty");
181 } /* end keyring_describe() */
183 /*****************************************************************************/
185 * read a list of key IDs from the keyring's contents
186 * - the keyring's semaphore is read-locked
188 static long keyring_read(const struct key *keyring,
189 char __user *buffer, size_t buflen)
191 struct keyring_list *klist;
197 klist = rcu_dereference(keyring->payload.subscriptions);
200 /* calculate how much data we could return */
201 qty = klist->nkeys * sizeof(key_serial_t);
203 if (buffer && buflen > 0) {
207 /* copy the IDs of the subscribed keys into the
211 for (loop = 0; loop < klist->nkeys; loop++) {
212 key = klist->keys[loop];
214 tmp = sizeof(key_serial_t);
218 if (copy_to_user(buffer,
236 } /* end keyring_read() */
238 /*****************************************************************************/
240 * allocate a keyring and link into the destination keyring
242 struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
243 struct task_struct *ctx, int not_in_quota,
249 keyring = key_alloc(&key_type_keyring, description,
251 (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
254 if (!IS_ERR(keyring)) {
255 ret = key_instantiate_and_link(keyring, NULL, 0, dest, NULL);
258 keyring = ERR_PTR(ret);
264 } /* end keyring_alloc() */
266 /*****************************************************************************/
268 * search the supplied keyring tree for a key that matches the criterion
269 * - perform a breadth-then-depth search up to the prescribed limit
270 * - we only find keys on which we have search permission
271 * - we use the supplied match function to see if the description (or other
272 * feature of interest) matches
273 * - we rely on RCU to prevent the keyring lists from disappearing on us
274 * - we return -EAGAIN if we didn't find any matching key
275 * - we return -ENOKEY if we only found negative matching keys
276 * - we propagate the possession attribute from the keyring ref to the key ref
278 key_ref_t keyring_search_aux(key_ref_t keyring_ref,
279 struct task_struct *context,
280 struct key_type *type,
281 const void *description,
282 key_match_func_t match)
285 struct keyring_list *keylist;
287 } stack[KEYRING_SEARCH_MAX_DEPTH];
289 struct keyring_list *keylist;
291 unsigned long possessed;
292 struct key *keyring, *key;
297 keyring = key_ref_to_ptr(keyring_ref);
298 possessed = is_key_possessed(keyring_ref);
301 /* top keyring must have search permission to begin the search */
302 err = key_task_permission(keyring_ref, context, KEY_SEARCH);
304 key_ref = ERR_PTR(err);
308 key_ref = ERR_PTR(-ENOTDIR);
309 if (keyring->type != &key_type_keyring)
314 now = current_kernel_time();
318 /* start processing a new keyring */
320 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
321 goto not_this_keyring;
323 keylist = rcu_dereference(keyring->payload.subscriptions);
325 goto not_this_keyring;
327 /* iterate through the keys in this keyring first */
328 for (kix = 0; kix < keylist->nkeys; kix++) {
329 key = keylist->keys[kix];
331 /* ignore keys not of this type */
332 if (key->type != type)
335 /* skip revoked keys and expired keys */
336 if (test_bit(KEY_FLAG_REVOKED, &key->flags))
339 if (key->expiry && now.tv_sec >= key->expiry)
342 /* keys that don't match */
343 if (!match(key, description))
346 /* key must have search permissions */
347 if (key_task_permission(make_key_ref(key, possessed),
348 context, KEY_SEARCH) < 0)
351 /* we set a different error code if we find a negative key */
352 if (test_bit(KEY_FLAG_NEGATIVE, &key->flags)) {
360 /* search through the keyrings nested in this one */
363 for (; kix < keylist->nkeys; kix++) {
364 key = keylist->keys[kix];
365 if (key->type != &key_type_keyring)
368 /* recursively search nested keyrings
369 * - only search keyrings for which we have search permission
371 if (sp >= KEYRING_SEARCH_MAX_DEPTH)
374 if (key_task_permission(make_key_ref(key, possessed),
375 context, KEY_SEARCH) < 0)
378 /* stack the current position */
379 stack[sp].keylist = keylist;
383 /* begin again with the new keyring */
388 /* the keyring we're looking at was disqualified or didn't contain a
392 /* resume the processing of a keyring higher up in the tree */
394 keylist = stack[sp].keylist;
395 kix = stack[sp].kix + 1;
399 key_ref = ERR_PTR(err);
402 /* we found a viable match */
404 atomic_inc(&key->usage);
406 key_ref = make_key_ref(key, possessed);
412 } /* end keyring_search_aux() */
414 /*****************************************************************************/
416 * search the supplied keyring tree for a key that matches the criterion
417 * - perform a breadth-then-depth search up to the prescribed limit
418 * - we only find keys on which we have search permission
419 * - we readlock the keyrings as we search down the tree
420 * - we return -EAGAIN if we didn't find any matching key
421 * - we return -ENOKEY if we only found negative matching keys
423 key_ref_t keyring_search(key_ref_t keyring,
424 struct key_type *type,
425 const char *description)
428 return ERR_PTR(-ENOKEY);
430 return keyring_search_aux(keyring, current,
431 type, description, type->match);
433 } /* end keyring_search() */
435 EXPORT_SYMBOL(keyring_search);
437 /*****************************************************************************/
439 * search the given keyring only (no recursion)
440 * - keyring must be locked by caller
441 * - caller must guarantee that the keyring is a keyring
443 key_ref_t __keyring_search_one(key_ref_t keyring_ref,
444 const struct key_type *ktype,
445 const char *description,
448 struct keyring_list *klist;
449 unsigned long possessed;
450 struct key *keyring, *key;
453 keyring = key_ref_to_ptr(keyring_ref);
454 possessed = is_key_possessed(keyring_ref);
458 klist = rcu_dereference(keyring->payload.subscriptions);
460 for (loop = 0; loop < klist->nkeys; loop++) {
461 key = klist->keys[loop];
463 if (key->type == ktype &&
464 (!key->type->match ||
465 key->type->match(key, description)) &&
466 key_permission(make_key_ref(key, possessed),
468 !test_bit(KEY_FLAG_REVOKED, &key->flags)
475 return ERR_PTR(-ENOKEY);
478 atomic_inc(&key->usage);
480 return make_key_ref(key, possessed);
482 } /* end __keyring_search_one() */
484 /*****************************************************************************/
486 * find a keyring with the specified name
487 * - all named keyrings are searched
488 * - only find keyrings with search permission for the process
489 * - only find keyrings with a serial number greater than the one specified
491 struct key *find_keyring_by_name(const char *name, key_serial_t bound)
496 keyring = ERR_PTR(-EINVAL);
500 bucket = keyring_hash(name);
502 read_lock(&keyring_name_lock);
504 if (keyring_name_hash[bucket].next) {
505 /* search this hash bucket for a keyring with a matching name
506 * that's readable and that hasn't been revoked */
507 list_for_each_entry(keyring,
508 &keyring_name_hash[bucket],
511 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
514 if (strcmp(keyring->description, name) != 0)
517 if (key_permission(make_key_ref(keyring, 0),
521 /* found a potential candidate, but we still need to
522 * check the serial number */
523 if (keyring->serial <= bound)
526 /* we've got a match */
527 atomic_inc(&keyring->usage);
528 read_unlock(&keyring_name_lock);
533 read_unlock(&keyring_name_lock);
534 keyring = ERR_PTR(-ENOKEY);
539 } /* end find_keyring_by_name() */
541 /*****************************************************************************/
543 * see if a cycle will will be created by inserting acyclic tree B in acyclic
544 * tree A at the topmost level (ie: as a direct child of A)
545 * - since we are adding B to A at the top level, checking for cycles should
546 * just be a matter of seeing if node A is somewhere in tree B
548 static int keyring_detect_cycle(struct key *A, struct key *B)
551 struct keyring_list *keylist;
553 } stack[KEYRING_SEARCH_MAX_DEPTH];
555 struct keyring_list *keylist;
556 struct key *subtree, *key;
568 /* start processing a new keyring */
570 if (test_bit(KEY_FLAG_REVOKED, &subtree->flags))
571 goto not_this_keyring;
573 keylist = rcu_dereference(subtree->payload.subscriptions);
575 goto not_this_keyring;
579 /* iterate through the remaining keys in this keyring */
580 for (; kix < keylist->nkeys; kix++) {
581 key = keylist->keys[kix];
586 /* recursively check nested keyrings */
587 if (key->type == &key_type_keyring) {
588 if (sp >= KEYRING_SEARCH_MAX_DEPTH)
591 /* stack the current position */
592 stack[sp].keylist = keylist;
596 /* begin again with the new keyring */
602 /* the keyring we're looking at was disqualified or didn't contain a
606 /* resume the checking of a keyring higher up in the tree */
608 keylist = stack[sp].keylist;
609 kix = stack[sp].kix + 1;
613 ret = 0; /* no cycles detected */
627 } /* end keyring_detect_cycle() */
629 /*****************************************************************************/
631 * dispose of a keyring list after the RCU grace period
633 static void keyring_link_rcu_disposal(struct rcu_head *rcu)
635 struct keyring_list *klist =
636 container_of(rcu, struct keyring_list, rcu);
640 } /* end keyring_link_rcu_disposal() */
642 /*****************************************************************************/
644 * dispose of a keyring list after the RCU grace period, freeing the unlinked
647 static void keyring_unlink_rcu_disposal(struct rcu_head *rcu)
649 struct keyring_list *klist =
650 container_of(rcu, struct keyring_list, rcu);
652 key_put(klist->keys[klist->delkey]);
655 } /* end keyring_unlink_rcu_disposal() */
657 /*****************************************************************************/
659 * link a key into to a keyring
660 * - must be called with the keyring's semaphore write-locked
661 * - discard already extant link to matching key if there is one
663 int __key_link(struct key *keyring, struct key *key)
665 struct keyring_list *klist, *nklist;
671 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
675 if (keyring->type != &key_type_keyring)
678 /* serialise link/link calls to prevent parallel calls causing a
679 * cycle when applied to two keyring in opposite orders */
680 down_write(&keyring_serialise_link_sem);
682 /* check that we aren't going to create a cycle adding one keyring to
684 if (key->type == &key_type_keyring) {
685 ret = keyring_detect_cycle(keyring, key);
690 /* see if there's a matching key we can displace */
691 klist = keyring->payload.subscriptions;
693 if (klist && klist->nkeys > 0) {
694 struct key_type *type = key->type;
696 for (loop = klist->nkeys - 1; loop >= 0; loop--) {
697 if (klist->keys[loop]->type == type &&
698 strcmp(klist->keys[loop]->description,
699 key->description) == 0
701 /* found a match - replace with new key */
702 size = sizeof(struct key *) * klist->maxkeys;
703 size += sizeof(*klist);
704 BUG_ON(size > PAGE_SIZE);
707 nklist = kmalloc(size, GFP_KERNEL);
711 memcpy(nklist, klist, size);
713 /* replace matched key */
714 atomic_inc(&key->usage);
715 nklist->keys[loop] = key;
718 keyring->payload.subscriptions,
721 /* dispose of the old keyring list and the
723 klist->delkey = loop;
724 call_rcu(&klist->rcu,
725 keyring_unlink_rcu_disposal);
732 /* check that we aren't going to overrun the user's quota */
733 ret = key_payload_reserve(keyring,
734 keyring->datalen + KEYQUOTA_LINK_BYTES);
738 klist = keyring->payload.subscriptions;
740 if (klist && klist->nkeys < klist->maxkeys) {
741 /* there's sufficient slack space to add directly */
742 atomic_inc(&key->usage);
744 klist->keys[klist->nkeys] = key;
750 /* grow the key list */
753 max += klist->maxkeys;
758 size = sizeof(*klist) + sizeof(struct key *) * max;
759 if (size > PAGE_SIZE)
763 nklist = kmalloc(size, GFP_KERNEL);
766 nklist->maxkeys = max;
770 nklist->nkeys = klist->nkeys;
773 sizeof(struct key *) * klist->nkeys);
776 /* add the key into the new space */
777 atomic_inc(&key->usage);
778 nklist->keys[nklist->nkeys++] = key;
780 rcu_assign_pointer(keyring->payload.subscriptions, nklist);
782 /* dispose of the old keyring list */
784 call_rcu(&klist->rcu, keyring_link_rcu_disposal);
790 up_write(&keyring_serialise_link_sem);
795 /* undo the quota changes */
796 key_payload_reserve(keyring,
797 keyring->datalen - KEYQUOTA_LINK_BYTES);
800 } /* end __key_link() */
802 /*****************************************************************************/
804 * link a key to a keyring
806 int key_link(struct key *keyring, struct key *key)
813 down_write(&keyring->sem);
814 ret = __key_link(keyring, key);
815 up_write(&keyring->sem);
819 } /* end key_link() */
821 EXPORT_SYMBOL(key_link);
823 /*****************************************************************************/
825 * unlink the first link to a key from a keyring
827 int key_unlink(struct key *keyring, struct key *key)
829 struct keyring_list *klist, *nklist;
836 if (keyring->type != &key_type_keyring)
839 down_write(&keyring->sem);
841 klist = keyring->payload.subscriptions;
843 /* search the keyring for the key */
844 for (loop = 0; loop < klist->nkeys; loop++)
845 if (klist->keys[loop] == key)
849 up_write(&keyring->sem);
854 /* we need to copy the key list for RCU purposes */
855 nklist = kmalloc(sizeof(*klist) +
856 sizeof(struct key *) * klist->maxkeys,
860 nklist->maxkeys = klist->maxkeys;
861 nklist->nkeys = klist->nkeys - 1;
864 memcpy(&nklist->keys[0],
866 loop * sizeof(struct key *));
868 if (loop < nklist->nkeys)
869 memcpy(&nklist->keys[loop],
870 &klist->keys[loop + 1],
871 (nklist->nkeys - loop) * sizeof(struct key *));
873 /* adjust the user's quota */
874 key_payload_reserve(keyring,
875 keyring->datalen - KEYQUOTA_LINK_BYTES);
877 rcu_assign_pointer(keyring->payload.subscriptions, nklist);
879 up_write(&keyring->sem);
881 /* schedule for later cleanup */
882 klist->delkey = loop;
883 call_rcu(&klist->rcu, keyring_unlink_rcu_disposal);
891 up_write(&keyring->sem);
894 } /* end key_unlink() */
896 EXPORT_SYMBOL(key_unlink);
898 /*****************************************************************************/
900 * dispose of a keyring list after the RCU grace period, releasing the keys it
903 static void keyring_clear_rcu_disposal(struct rcu_head *rcu)
905 struct keyring_list *klist;
908 klist = container_of(rcu, struct keyring_list, rcu);
910 for (loop = klist->nkeys - 1; loop >= 0; loop--)
911 key_put(klist->keys[loop]);
915 } /* end keyring_clear_rcu_disposal() */
917 /*****************************************************************************/
919 * clear the specified process keyring
920 * - implements keyctl(KEYCTL_CLEAR)
922 int keyring_clear(struct key *keyring)
924 struct keyring_list *klist;
928 if (keyring->type == &key_type_keyring) {
929 /* detach the pointer block with the locks held */
930 down_write(&keyring->sem);
932 klist = keyring->payload.subscriptions;
934 /* adjust the quota */
935 key_payload_reserve(keyring,
936 sizeof(struct keyring_list));
938 rcu_assign_pointer(keyring->payload.subscriptions,
942 up_write(&keyring->sem);
944 /* free the keys after the locks have been dropped */
946 call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
953 } /* end keyring_clear() */
955 EXPORT_SYMBOL(keyring_clear);