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 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 int not_in_quota
, struct key
*dest
)
248 keyring
= key_alloc(&key_type_keyring
, description
,
250 (KEY_POS_ALL
& ~KEY_POS_SETATTR
) | KEY_USR_ALL
,
253 if (!IS_ERR(keyring
)) {
254 ret
= key_instantiate_and_link(keyring
, NULL
, 0, dest
, NULL
);
257 keyring
= ERR_PTR(ret
);
263 } /* end keyring_alloc() */
265 /*****************************************************************************/
267 * search the supplied keyring tree for a key that matches the criterion
268 * - perform a breadth-then-depth search up to the prescribed limit
269 * - we only find keys on which we have search permission
270 * - we use the supplied match function to see if the description (or other
271 * feature of interest) matches
272 * - we rely on RCU to prevent the keyring lists from disappearing on us
273 * - we return -EAGAIN if we didn't find any matching key
274 * - we return -ENOKEY if we only found negative matching keys
275 * - we propagate the possession attribute from the keyring ref to the key ref
277 key_ref_t
keyring_search_aux(key_ref_t keyring_ref
,
278 struct task_struct
*context
,
279 struct key_type
*type
,
280 const void *description
,
281 key_match_func_t match
)
284 struct keyring_list
*keylist
;
286 } stack
[KEYRING_SEARCH_MAX_DEPTH
];
288 struct keyring_list
*keylist
;
290 unsigned long possessed
;
291 struct key
*keyring
, *key
;
296 keyring
= key_ref_to_ptr(keyring_ref
);
297 possessed
= is_key_possessed(keyring_ref
);
300 /* top keyring must have search permission to begin the search */
301 err
= key_task_permission(keyring_ref
, context
, KEY_SEARCH
);
303 key_ref
= ERR_PTR(err
);
307 key_ref
= ERR_PTR(-ENOTDIR
);
308 if (keyring
->type
!= &key_type_keyring
)
313 now
= current_kernel_time();
317 /* start processing a new keyring */
319 if (test_bit(KEY_FLAG_REVOKED
, &keyring
->flags
))
320 goto not_this_keyring
;
322 keylist
= rcu_dereference(keyring
->payload
.subscriptions
);
324 goto not_this_keyring
;
326 /* iterate through the keys in this keyring first */
327 for (kix
= 0; kix
< keylist
->nkeys
; kix
++) {
328 key
= keylist
->keys
[kix
];
330 /* ignore keys not of this type */
331 if (key
->type
!= type
)
334 /* skip revoked keys and expired keys */
335 if (test_bit(KEY_FLAG_REVOKED
, &key
->flags
))
338 if (key
->expiry
&& now
.tv_sec
>= key
->expiry
)
341 /* keys that don't match */
342 if (!match(key
, description
))
345 /* key must have search permissions */
346 if (key_task_permission(make_key_ref(key
, possessed
),
347 context
, KEY_SEARCH
) < 0)
350 /* we set a different error code if we find a negative key */
351 if (test_bit(KEY_FLAG_NEGATIVE
, &key
->flags
)) {
359 /* search through the keyrings nested in this one */
362 for (; kix
< keylist
->nkeys
; kix
++) {
363 key
= keylist
->keys
[kix
];
364 if (key
->type
!= &key_type_keyring
)
367 /* recursively search nested keyrings
368 * - only search keyrings for which we have search permission
370 if (sp
>= KEYRING_SEARCH_MAX_DEPTH
)
373 if (key_task_permission(make_key_ref(key
, possessed
),
374 context
, KEY_SEARCH
) < 0)
377 /* stack the current position */
378 stack
[sp
].keylist
= keylist
;
382 /* begin again with the new keyring */
387 /* the keyring we're looking at was disqualified or didn't contain a
391 /* resume the processing of a keyring higher up in the tree */
393 keylist
= stack
[sp
].keylist
;
394 kix
= stack
[sp
].kix
+ 1;
398 key_ref
= ERR_PTR(err
);
401 /* we found a viable match */
403 atomic_inc(&key
->usage
);
405 key_ref
= make_key_ref(key
, possessed
);
411 } /* end keyring_search_aux() */
413 /*****************************************************************************/
415 * search the supplied keyring tree for a key that matches the criterion
416 * - perform a breadth-then-depth search up to the prescribed limit
417 * - we only find keys on which we have search permission
418 * - we readlock the keyrings as we search down the tree
419 * - we return -EAGAIN if we didn't find any matching key
420 * - we return -ENOKEY if we only found negative matching keys
422 key_ref_t
keyring_search(key_ref_t keyring
,
423 struct key_type
*type
,
424 const char *description
)
427 return ERR_PTR(-ENOKEY
);
429 return keyring_search_aux(keyring
, current
,
430 type
, description
, type
->match
);
432 } /* end keyring_search() */
434 EXPORT_SYMBOL(keyring_search
);
436 /*****************************************************************************/
438 * search the given keyring only (no recursion)
439 * - keyring must be locked by caller
441 key_ref_t
__keyring_search_one(key_ref_t keyring_ref
,
442 const struct key_type
*ktype
,
443 const char *description
,
446 struct keyring_list
*klist
;
447 unsigned long possessed
;
448 struct key
*keyring
, *key
;
451 keyring
= key_ref_to_ptr(keyring_ref
);
452 possessed
= is_key_possessed(keyring_ref
);
456 klist
= rcu_dereference(keyring
->payload
.subscriptions
);
458 for (loop
= 0; loop
< klist
->nkeys
; loop
++) {
459 key
= klist
->keys
[loop
];
461 if (key
->type
== ktype
&&
462 (!key
->type
->match
||
463 key
->type
->match(key
, description
)) &&
464 key_permission(make_key_ref(key
, possessed
),
466 !test_bit(KEY_FLAG_REVOKED
, &key
->flags
)
473 return ERR_PTR(-ENOKEY
);
476 atomic_inc(&key
->usage
);
478 return make_key_ref(key
, possessed
);
480 } /* end __keyring_search_one() */
482 /*****************************************************************************/
484 * search for an instantiation authorisation key matching a target key
485 * - the RCU read lock must be held by the caller
486 * - a target_id of zero specifies any valid token
488 struct key
*keyring_search_instkey(struct key
*keyring
,
489 key_serial_t target_id
)
491 struct request_key_auth
*rka
;
492 struct keyring_list
*klist
;
496 klist
= rcu_dereference(keyring
->payload
.subscriptions
);
498 for (loop
= 0; loop
< klist
->nkeys
; loop
++) {
499 instkey
= klist
->keys
[loop
];
501 if (instkey
->type
!= &key_type_request_key_auth
)
504 rka
= instkey
->payload
.data
;
505 if (target_id
&& rka
->target_key
->serial
!= target_id
)
508 /* the auth key is revoked during instantiation */
509 if (!test_bit(KEY_FLAG_REVOKED
, &instkey
->flags
))
512 instkey
= ERR_PTR(-EKEYREVOKED
);
517 instkey
= ERR_PTR(-EACCES
);
521 atomic_inc(&instkey
->usage
);
525 } /* end keyring_search_instkey() */
527 /*****************************************************************************/
529 * find a keyring with the specified name
530 * - all named keyrings are searched
531 * - only find keyrings with search permission for the process
532 * - only find keyrings with a serial number greater than the one specified
534 struct key
*find_keyring_by_name(const char *name
, key_serial_t bound
)
539 keyring
= ERR_PTR(-EINVAL
);
543 bucket
= keyring_hash(name
);
545 read_lock(&keyring_name_lock
);
547 if (keyring_name_hash
[bucket
].next
) {
548 /* search this hash bucket for a keyring with a matching name
549 * that's readable and that hasn't been revoked */
550 list_for_each_entry(keyring
,
551 &keyring_name_hash
[bucket
],
554 if (test_bit(KEY_FLAG_REVOKED
, &keyring
->flags
))
557 if (strcmp(keyring
->description
, name
) != 0)
560 if (key_permission(make_key_ref(keyring
, 0),
564 /* found a potential candidate, but we still need to
565 * check the serial number */
566 if (keyring
->serial
<= bound
)
569 /* we've got a match */
570 atomic_inc(&keyring
->usage
);
571 read_unlock(&keyring_name_lock
);
576 read_unlock(&keyring_name_lock
);
577 keyring
= ERR_PTR(-ENOKEY
);
582 } /* end find_keyring_by_name() */
584 /*****************************************************************************/
586 * see if a cycle will will be created by inserting acyclic tree B in acyclic
587 * tree A at the topmost level (ie: as a direct child of A)
588 * - since we are adding B to A at the top level, checking for cycles should
589 * just be a matter of seeing if node A is somewhere in tree B
591 static int keyring_detect_cycle(struct key
*A
, struct key
*B
)
594 struct keyring_list
*keylist
;
596 } stack
[KEYRING_SEARCH_MAX_DEPTH
];
598 struct keyring_list
*keylist
;
599 struct key
*subtree
, *key
;
611 /* start processing a new keyring */
613 if (test_bit(KEY_FLAG_REVOKED
, &subtree
->flags
))
614 goto not_this_keyring
;
616 keylist
= rcu_dereference(subtree
->payload
.subscriptions
);
618 goto not_this_keyring
;
622 /* iterate through the remaining keys in this keyring */
623 for (; kix
< keylist
->nkeys
; kix
++) {
624 key
= keylist
->keys
[kix
];
629 /* recursively check nested keyrings */
630 if (key
->type
== &key_type_keyring
) {
631 if (sp
>= KEYRING_SEARCH_MAX_DEPTH
)
634 /* stack the current position */
635 stack
[sp
].keylist
= keylist
;
639 /* begin again with the new keyring */
645 /* the keyring we're looking at was disqualified or didn't contain a
649 /* resume the checking of a keyring higher up in the tree */
651 keylist
= stack
[sp
].keylist
;
652 kix
= stack
[sp
].kix
+ 1;
656 ret
= 0; /* no cycles detected */
670 } /* end keyring_detect_cycle() */
672 /*****************************************************************************/
674 * dispose of a keyring list after the RCU grace period
676 static void keyring_link_rcu_disposal(struct rcu_head
*rcu
)
678 struct keyring_list
*klist
=
679 container_of(rcu
, struct keyring_list
, rcu
);
683 } /* end keyring_link_rcu_disposal() */
685 /*****************************************************************************/
687 * link a key into to a keyring
688 * - must be called with the keyring's semaphore write-locked
690 int __key_link(struct key
*keyring
, struct key
*key
)
692 struct keyring_list
*klist
, *nklist
;
698 if (test_bit(KEY_FLAG_REVOKED
, &keyring
->flags
))
702 if (keyring
->type
!= &key_type_keyring
)
705 /* serialise link/link calls to prevent parallel calls causing a
706 * cycle when applied to two keyring in opposite orders */
707 down_write(&keyring_serialise_link_sem
);
709 /* check that we aren't going to create a cycle adding one keyring to
711 if (key
->type
== &key_type_keyring
) {
712 ret
= keyring_detect_cycle(keyring
, key
);
717 /* check that we aren't going to overrun the user's quota */
718 ret
= key_payload_reserve(keyring
,
719 keyring
->datalen
+ KEYQUOTA_LINK_BYTES
);
723 klist
= keyring
->payload
.subscriptions
;
725 if (klist
&& klist
->nkeys
< klist
->maxkeys
) {
726 /* there's sufficient slack space to add directly */
727 atomic_inc(&key
->usage
);
729 klist
->keys
[klist
->nkeys
] = key
;
737 /* grow the key list */
740 max
+= klist
->maxkeys
;
745 size
= sizeof(*klist
) + sizeof(struct key
*) * max
;
746 if (size
> PAGE_SIZE
)
750 nklist
= kmalloc(size
, GFP_KERNEL
);
753 nklist
->maxkeys
= max
;
757 nklist
->nkeys
= klist
->nkeys
;
760 sizeof(struct key
*) * klist
->nkeys
);
763 /* add the key into the new space */
764 atomic_inc(&key
->usage
);
765 nklist
->keys
[nklist
->nkeys
++] = key
;
767 rcu_assign_pointer(keyring
->payload
.subscriptions
, nklist
);
769 /* dispose of the old keyring list */
771 call_rcu(&klist
->rcu
, keyring_link_rcu_disposal
);
777 up_write(&keyring_serialise_link_sem
);
782 /* undo the quota changes */
783 key_payload_reserve(keyring
,
784 keyring
->datalen
- KEYQUOTA_LINK_BYTES
);
787 } /* end __key_link() */
789 /*****************************************************************************/
791 * link a key to a keyring
793 int key_link(struct key
*keyring
, struct key
*key
)
800 down_write(&keyring
->sem
);
801 ret
= __key_link(keyring
, key
);
802 up_write(&keyring
->sem
);
806 } /* end key_link() */
808 EXPORT_SYMBOL(key_link
);
810 /*****************************************************************************/
812 * dispose of a keyring list after the RCU grace period, freeing the unlinked
815 static void keyring_unlink_rcu_disposal(struct rcu_head
*rcu
)
817 struct keyring_list
*klist
=
818 container_of(rcu
, struct keyring_list
, rcu
);
820 key_put(klist
->keys
[klist
->delkey
]);
823 } /* end keyring_unlink_rcu_disposal() */
825 /*****************************************************************************/
827 * unlink the first link to a key from a keyring
829 int key_unlink(struct key
*keyring
, struct key
*key
)
831 struct keyring_list
*klist
, *nklist
;
838 if (keyring
->type
!= &key_type_keyring
)
841 down_write(&keyring
->sem
);
843 klist
= keyring
->payload
.subscriptions
;
845 /* search the keyring for the key */
846 for (loop
= 0; loop
< klist
->nkeys
; loop
++)
847 if (klist
->keys
[loop
] == key
)
851 up_write(&keyring
->sem
);
856 /* we need to copy the key list for RCU purposes */
857 nklist
= kmalloc(sizeof(*klist
) +
858 sizeof(struct key
*) * klist
->maxkeys
,
862 nklist
->maxkeys
= klist
->maxkeys
;
863 nklist
->nkeys
= klist
->nkeys
- 1;
866 memcpy(&nklist
->keys
[0],
868 loop
* sizeof(struct key
*));
870 if (loop
< nklist
->nkeys
)
871 memcpy(&nklist
->keys
[loop
],
872 &klist
->keys
[loop
+ 1],
873 (nklist
->nkeys
- loop
) * sizeof(struct key
*));
875 /* adjust the user's quota */
876 key_payload_reserve(keyring
,
877 keyring
->datalen
- KEYQUOTA_LINK_BYTES
);
879 rcu_assign_pointer(keyring
->payload
.subscriptions
, nklist
);
881 up_write(&keyring
->sem
);
883 /* schedule for later cleanup */
884 klist
->delkey
= loop
;
885 call_rcu(&klist
->rcu
, keyring_unlink_rcu_disposal
);
893 up_write(&keyring
->sem
);
896 } /* end key_unlink() */
898 EXPORT_SYMBOL(key_unlink
);
900 /*****************************************************************************/
902 * dispose of a keyring list after the RCU grace period, releasing the keys it
905 static void keyring_clear_rcu_disposal(struct rcu_head
*rcu
)
907 struct keyring_list
*klist
;
910 klist
= container_of(rcu
, struct keyring_list
, rcu
);
912 for (loop
= klist
->nkeys
- 1; loop
>= 0; loop
--)
913 key_put(klist
->keys
[loop
]);
917 } /* end keyring_clear_rcu_disposal() */
919 /*****************************************************************************/
921 * clear the specified process keyring
922 * - implements keyctl(KEYCTL_CLEAR)
924 int keyring_clear(struct key
*keyring
)
926 struct keyring_list
*klist
;
930 if (keyring
->type
== &key_type_keyring
) {
931 /* detach the pointer block with the locks held */
932 down_write(&keyring
->sem
);
934 klist
= keyring
->payload
.subscriptions
;
936 /* adjust the quota */
937 key_payload_reserve(keyring
,
938 sizeof(struct keyring_list
));
940 rcu_assign_pointer(keyring
->payload
.subscriptions
,
944 up_write(&keyring
->sem
);
946 /* free the keys after the locks have been dropped */
948 call_rcu(&klist
->rcu
, keyring_clear_rcu_disposal
);
955 } /* end keyring_clear() */
957 EXPORT_SYMBOL(keyring_clear
);