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/seq_file.h>
17 #include <linux/err.h>
18 #include <asm/uaccess.h>
22 * when plumbing the depths of the key tree, this sets a hard limit set on how
23 * deep we're willing to go
25 #define KEYRING_SEARCH_MAX_DEPTH 6
28 * we keep all named keyrings in a hash to speed looking them up
30 #define KEYRING_NAME_HASH_SIZE (1 << 5)
32 static struct list_head keyring_name_hash
[KEYRING_NAME_HASH_SIZE
];
33 static DEFINE_RWLOCK(keyring_name_lock
);
35 static inline unsigned keyring_hash(const char *desc
)
40 bucket
+= (unsigned char) *desc
;
42 return bucket
& (KEYRING_NAME_HASH_SIZE
- 1);
46 * the keyring type definition
48 static int keyring_instantiate(struct key
*keyring
,
49 const void *data
, size_t datalen
);
50 static int keyring_duplicate(struct key
*keyring
, const struct key
*source
);
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 .duplicate
= keyring_duplicate
,
62 .match
= keyring_match
,
63 .destroy
= keyring_destroy
,
64 .describe
= keyring_describe
,
69 * semaphore to serialise link/link calls to prevent two link calls in parallel
72 DECLARE_RWSEM(keyring_serialise_link_sem
);
74 /*****************************************************************************/
76 * publish the name of a keyring so that it can be found by name (if it has
79 void keyring_publish_name(struct key
*keyring
)
83 if (keyring
->description
) {
84 bucket
= keyring_hash(keyring
->description
);
86 write_lock(&keyring_name_lock
);
88 if (!keyring_name_hash
[bucket
].next
)
89 INIT_LIST_HEAD(&keyring_name_hash
[bucket
]);
91 list_add_tail(&keyring
->type_data
.link
,
92 &keyring_name_hash
[bucket
]);
94 write_unlock(&keyring_name_lock
);
97 } /* end keyring_publish_name() */
99 /*****************************************************************************/
101 * initialise a keyring
102 * - we object if we were given any data
104 static int keyring_instantiate(struct key
*keyring
,
105 const void *data
, size_t datalen
)
111 /* make the keyring available by name if it has one */
112 keyring_publish_name(keyring
);
118 } /* end keyring_instantiate() */
120 /*****************************************************************************/
122 * duplicate the list of subscribed keys from a source keyring into this one
124 static int keyring_duplicate(struct key
*keyring
, const struct key
*source
)
126 struct keyring_list
*sklist
, *klist
;
131 const unsigned limit
=
132 (PAGE_SIZE
- sizeof(*klist
)) / sizeof(struct key
*);
136 /* find out how many keys are currently linked */
138 sklist
= rcu_dereference(source
->payload
.subscriptions
);
144 /* allocate a new payload and stuff load with key links */
148 max
= (max
+ 3) & ~3;
153 size
= sizeof(*klist
) + sizeof(struct key
*) * max
;
154 klist
= kmalloc(size
, GFP_KERNEL
);
160 sklist
= rcu_dereference(source
->payload
.subscriptions
);
162 klist
->maxkeys
= max
;
163 klist
->nkeys
= sklist
->nkeys
;
166 sklist
->nkeys
* sizeof(struct key
*));
168 for (loop
= klist
->nkeys
- 1; loop
>= 0; loop
--)
169 atomic_inc(&klist
->keys
[loop
]->usage
);
173 rcu_assign_pointer(keyring
->payload
.subscriptions
, klist
);
180 } /* end keyring_duplicate() */
182 /*****************************************************************************/
184 * match keyrings on their name
186 static int keyring_match(const struct key
*keyring
, const void *description
)
188 return keyring
->description
&&
189 strcmp(keyring
->description
, description
) == 0;
191 } /* end keyring_match() */
193 /*****************************************************************************/
195 * dispose of the data dangling from the corpse of a keyring
197 static void keyring_destroy(struct key
*keyring
)
199 struct keyring_list
*klist
;
202 if (keyring
->description
) {
203 write_lock(&keyring_name_lock
);
205 if (keyring
->type_data
.link
.next
!= NULL
&&
206 !list_empty(&keyring
->type_data
.link
))
207 list_del(&keyring
->type_data
.link
);
209 write_unlock(&keyring_name_lock
);
212 klist
= rcu_dereference(keyring
->payload
.subscriptions
);
214 for (loop
= klist
->nkeys
- 1; loop
>= 0; loop
--)
215 key_put(klist
->keys
[loop
]);
219 } /* end keyring_destroy() */
221 /*****************************************************************************/
223 * describe the keyring
225 static void keyring_describe(const struct key
*keyring
, struct seq_file
*m
)
227 struct keyring_list
*klist
;
229 if (keyring
->description
) {
230 seq_puts(m
, keyring
->description
);
233 seq_puts(m
, "[anon]");
237 klist
= rcu_dereference(keyring
->payload
.subscriptions
);
239 seq_printf(m
, ": %u/%u", klist
->nkeys
, klist
->maxkeys
);
241 seq_puts(m
, ": empty");
244 } /* end keyring_describe() */
246 /*****************************************************************************/
248 * read a list of key IDs from the keyring's contents
249 * - the keyring's semaphore is read-locked
251 static long keyring_read(const struct key
*keyring
,
252 char __user
*buffer
, size_t buflen
)
254 struct keyring_list
*klist
;
260 klist
= rcu_dereference(keyring
->payload
.subscriptions
);
263 /* calculate how much data we could return */
264 qty
= klist
->nkeys
* sizeof(key_serial_t
);
266 if (buffer
&& buflen
> 0) {
270 /* copy the IDs of the subscribed keys into the
274 for (loop
= 0; loop
< klist
->nkeys
; loop
++) {
275 key
= klist
->keys
[loop
];
277 tmp
= sizeof(key_serial_t
);
281 if (copy_to_user(buffer
,
299 } /* end keyring_read() */
301 /*****************************************************************************/
303 * allocate a keyring and link into the destination keyring
305 struct key
*keyring_alloc(const char *description
, uid_t uid
, gid_t gid
,
306 int not_in_quota
, struct key
*dest
)
311 keyring
= key_alloc(&key_type_keyring
, description
,
312 uid
, gid
, KEY_POS_ALL
| KEY_USR_ALL
, not_in_quota
);
314 if (!IS_ERR(keyring
)) {
315 ret
= key_instantiate_and_link(keyring
, NULL
, 0, dest
, NULL
);
318 keyring
= ERR_PTR(ret
);
324 } /* end keyring_alloc() */
326 /*****************************************************************************/
328 * search the supplied keyring tree for a key that matches the criterion
329 * - perform a breadth-then-depth search up to the prescribed limit
330 * - we only find keys on which we have search permission
331 * - we use the supplied match function to see if the description (or other
332 * feature of interest) matches
333 * - we rely on RCU to prevent the keyring lists from disappearing on us
334 * - we return -EAGAIN if we didn't find any matching key
335 * - we return -ENOKEY if we only found negative matching keys
336 * - we propagate the possession attribute from the keyring ref to the key ref
338 key_ref_t
keyring_search_aux(key_ref_t keyring_ref
,
339 struct task_struct
*context
,
340 struct key_type
*type
,
341 const void *description
,
342 key_match_func_t match
)
345 struct keyring_list
*keylist
;
347 } stack
[KEYRING_SEARCH_MAX_DEPTH
];
349 struct keyring_list
*keylist
;
351 unsigned long possessed
;
352 struct key
*keyring
, *key
;
357 keyring
= key_ref_to_ptr(keyring_ref
);
358 possessed
= is_key_possessed(keyring_ref
);
361 /* top keyring must have search permission to begin the search */
362 key_ref
= ERR_PTR(-EACCES
);
363 if (!key_task_permission(keyring_ref
, context
, KEY_SEARCH
))
366 key_ref
= ERR_PTR(-ENOTDIR
);
367 if (keyring
->type
!= &key_type_keyring
)
372 now
= current_kernel_time();
376 /* start processing a new keyring */
378 if (test_bit(KEY_FLAG_REVOKED
, &keyring
->flags
))
379 goto not_this_keyring
;
381 keylist
= rcu_dereference(keyring
->payload
.subscriptions
);
383 goto not_this_keyring
;
385 /* iterate through the keys in this keyring first */
386 for (kix
= 0; kix
< keylist
->nkeys
; kix
++) {
387 key
= keylist
->keys
[kix
];
389 /* ignore keys not of this type */
390 if (key
->type
!= type
)
393 /* skip revoked keys and expired keys */
394 if (test_bit(KEY_FLAG_REVOKED
, &key
->flags
))
397 if (key
->expiry
&& now
.tv_sec
>= key
->expiry
)
400 /* keys that don't match */
401 if (!match(key
, description
))
404 /* key must have search permissions */
405 if (!key_task_permission(make_key_ref(key
, possessed
),
406 context
, KEY_SEARCH
))
409 /* we set a different error code if we find a negative key */
410 if (test_bit(KEY_FLAG_NEGATIVE
, &key
->flags
)) {
418 /* search through the keyrings nested in this one */
421 for (; kix
< keylist
->nkeys
; kix
++) {
422 key
= keylist
->keys
[kix
];
423 if (key
->type
!= &key_type_keyring
)
426 /* recursively search nested keyrings
427 * - only search keyrings for which we have search permission
429 if (sp
>= KEYRING_SEARCH_MAX_DEPTH
)
432 if (!key_task_permission(make_key_ref(key
, possessed
),
433 context
, KEY_SEARCH
))
436 /* stack the current position */
437 stack
[sp
].keylist
= keylist
;
441 /* begin again with the new keyring */
446 /* the keyring we're looking at was disqualified or didn't contain a
450 /* resume the processing of a keyring higher up in the tree */
452 keylist
= stack
[sp
].keylist
;
453 kix
= stack
[sp
].kix
+ 1;
457 key_ref
= ERR_PTR(err
);
460 /* we found a viable match */
462 atomic_inc(&key
->usage
);
464 key_ref
= make_key_ref(key
, possessed
);
470 } /* end keyring_search_aux() */
472 /*****************************************************************************/
474 * search the supplied keyring tree for a key that matches the criterion
475 * - perform a breadth-then-depth search up to the prescribed limit
476 * - we only find keys on which we have search permission
477 * - we readlock the keyrings as we search down the tree
478 * - we return -EAGAIN if we didn't find any matching key
479 * - we return -ENOKEY if we only found negative matching keys
481 key_ref_t
keyring_search(key_ref_t keyring
,
482 struct key_type
*type
,
483 const char *description
)
486 return ERR_PTR(-ENOKEY
);
488 return keyring_search_aux(keyring
, current
,
489 type
, description
, type
->match
);
491 } /* end keyring_search() */
493 EXPORT_SYMBOL(keyring_search
);
495 /*****************************************************************************/
497 * search the given keyring only (no recursion)
498 * - keyring must be locked by caller
500 key_ref_t
__keyring_search_one(key_ref_t keyring_ref
,
501 const struct key_type
*ktype
,
502 const char *description
,
505 struct keyring_list
*klist
;
506 unsigned long possessed
;
507 struct key
*keyring
, *key
;
510 keyring
= key_ref_to_ptr(keyring_ref
);
511 possessed
= is_key_possessed(keyring_ref
);
515 klist
= rcu_dereference(keyring
->payload
.subscriptions
);
517 for (loop
= 0; loop
< klist
->nkeys
; loop
++) {
518 key
= klist
->keys
[loop
];
520 if (key
->type
== ktype
&&
521 (!key
->type
->match
||
522 key
->type
->match(key
, description
)) &&
523 key_permission(make_key_ref(key
, possessed
),
525 !test_bit(KEY_FLAG_REVOKED
, &key
->flags
)
532 return ERR_PTR(-ENOKEY
);
535 atomic_inc(&key
->usage
);
537 return make_key_ref(key
, possessed
);
539 } /* end __keyring_search_one() */
541 /*****************************************************************************/
543 * search for an instantiation authorisation key matching a target key
544 * - the RCU read lock must be held by the caller
545 * - a target_id of zero specifies any valid token
547 struct key
*keyring_search_instkey(struct key
*keyring
,
548 key_serial_t target_id
)
550 struct request_key_auth
*rka
;
551 struct keyring_list
*klist
;
555 klist
= rcu_dereference(keyring
->payload
.subscriptions
);
557 for (loop
= 0; loop
< klist
->nkeys
; loop
++) {
558 instkey
= klist
->keys
[loop
];
560 if (instkey
->type
!= &key_type_request_key_auth
)
563 rka
= instkey
->payload
.data
;
564 if (target_id
&& rka
->target_key
->serial
!= target_id
)
567 /* the auth key is revoked during instantiation */
568 if (!test_bit(KEY_FLAG_REVOKED
, &instkey
->flags
))
571 instkey
= ERR_PTR(-EKEYREVOKED
);
576 instkey
= ERR_PTR(-EACCES
);
580 atomic_inc(&instkey
->usage
);
584 } /* end keyring_search_instkey() */
586 /*****************************************************************************/
588 * find a keyring with the specified name
589 * - all named keyrings are searched
590 * - only find keyrings with search permission for the process
591 * - only find keyrings with a serial number greater than the one specified
593 struct key
*find_keyring_by_name(const char *name
, key_serial_t bound
)
598 keyring
= ERR_PTR(-EINVAL
);
602 bucket
= keyring_hash(name
);
604 read_lock(&keyring_name_lock
);
606 if (keyring_name_hash
[bucket
].next
) {
607 /* search this hash bucket for a keyring with a matching name
608 * that's readable and that hasn't been revoked */
609 list_for_each_entry(keyring
,
610 &keyring_name_hash
[bucket
],
613 if (test_bit(KEY_FLAG_REVOKED
, &keyring
->flags
))
616 if (strcmp(keyring
->description
, name
) != 0)
619 if (!key_permission(make_key_ref(keyring
, 0),
623 /* found a potential candidate, but we still need to
624 * check the serial number */
625 if (keyring
->serial
<= bound
)
628 /* we've got a match */
629 atomic_inc(&keyring
->usage
);
630 read_unlock(&keyring_name_lock
);
635 read_unlock(&keyring_name_lock
);
636 keyring
= ERR_PTR(-ENOKEY
);
641 } /* end find_keyring_by_name() */
643 /*****************************************************************************/
645 * see if a cycle will will be created by inserting acyclic tree B in acyclic
646 * tree A at the topmost level (ie: as a direct child of A)
647 * - since we are adding B to A at the top level, checking for cycles should
648 * just be a matter of seeing if node A is somewhere in tree B
650 static int keyring_detect_cycle(struct key
*A
, struct key
*B
)
653 struct keyring_list
*keylist
;
655 } stack
[KEYRING_SEARCH_MAX_DEPTH
];
657 struct keyring_list
*keylist
;
658 struct key
*subtree
, *key
;
670 /* start processing a new keyring */
672 if (test_bit(KEY_FLAG_REVOKED
, &subtree
->flags
))
673 goto not_this_keyring
;
675 keylist
= rcu_dereference(subtree
->payload
.subscriptions
);
677 goto not_this_keyring
;
681 /* iterate through the remaining keys in this keyring */
682 for (; kix
< keylist
->nkeys
; kix
++) {
683 key
= keylist
->keys
[kix
];
688 /* recursively check nested keyrings */
689 if (key
->type
== &key_type_keyring
) {
690 if (sp
>= KEYRING_SEARCH_MAX_DEPTH
)
693 /* stack the current position */
694 stack
[sp
].keylist
= keylist
;
698 /* begin again with the new keyring */
704 /* the keyring we're looking at was disqualified or didn't contain a
708 /* resume the checking of a keyring higher up in the tree */
710 keylist
= stack
[sp
].keylist
;
711 kix
= stack
[sp
].kix
+ 1;
715 ret
= 0; /* no cycles detected */
729 } /* end keyring_detect_cycle() */
731 /*****************************************************************************/
733 * dispose of a keyring list after the RCU grace period
735 static void keyring_link_rcu_disposal(struct rcu_head
*rcu
)
737 struct keyring_list
*klist
=
738 container_of(rcu
, struct keyring_list
, rcu
);
742 } /* end keyring_link_rcu_disposal() */
744 /*****************************************************************************/
746 * link a key into to a keyring
747 * - must be called with the keyring's semaphore write-locked
749 int __key_link(struct key
*keyring
, struct key
*key
)
751 struct keyring_list
*klist
, *nklist
;
757 if (test_bit(KEY_FLAG_REVOKED
, &keyring
->flags
))
761 if (keyring
->type
!= &key_type_keyring
)
764 /* serialise link/link calls to prevent parallel calls causing a
765 * cycle when applied to two keyring in opposite orders */
766 down_write(&keyring_serialise_link_sem
);
768 /* check that we aren't going to create a cycle adding one keyring to
770 if (key
->type
== &key_type_keyring
) {
771 ret
= keyring_detect_cycle(keyring
, key
);
776 /* check that we aren't going to overrun the user's quota */
777 ret
= key_payload_reserve(keyring
,
778 keyring
->datalen
+ KEYQUOTA_LINK_BYTES
);
782 klist
= keyring
->payload
.subscriptions
;
784 if (klist
&& klist
->nkeys
< klist
->maxkeys
) {
785 /* there's sufficient slack space to add directly */
786 atomic_inc(&key
->usage
);
788 klist
->keys
[klist
->nkeys
] = key
;
796 /* grow the key list */
799 max
+= klist
->maxkeys
;
804 size
= sizeof(*klist
) + sizeof(struct key
*) * max
;
805 if (size
> PAGE_SIZE
)
809 nklist
= kmalloc(size
, GFP_KERNEL
);
812 nklist
->maxkeys
= max
;
816 nklist
->nkeys
= klist
->nkeys
;
819 sizeof(struct key
*) * klist
->nkeys
);
822 /* add the key into the new space */
823 atomic_inc(&key
->usage
);
824 nklist
->keys
[nklist
->nkeys
++] = key
;
826 rcu_assign_pointer(keyring
->payload
.subscriptions
, nklist
);
828 /* dispose of the old keyring list */
830 call_rcu(&klist
->rcu
, keyring_link_rcu_disposal
);
836 up_write(&keyring_serialise_link_sem
);
841 /* undo the quota changes */
842 key_payload_reserve(keyring
,
843 keyring
->datalen
- KEYQUOTA_LINK_BYTES
);
846 } /* end __key_link() */
848 /*****************************************************************************/
850 * link a key to a keyring
852 int key_link(struct key
*keyring
, struct key
*key
)
859 down_write(&keyring
->sem
);
860 ret
= __key_link(keyring
, key
);
861 up_write(&keyring
->sem
);
865 } /* end key_link() */
867 EXPORT_SYMBOL(key_link
);
869 /*****************************************************************************/
871 * dispose of a keyring list after the RCU grace period, freeing the unlinked
874 static void keyring_unlink_rcu_disposal(struct rcu_head
*rcu
)
876 struct keyring_list
*klist
=
877 container_of(rcu
, struct keyring_list
, rcu
);
879 key_put(klist
->keys
[klist
->delkey
]);
882 } /* end keyring_unlink_rcu_disposal() */
884 /*****************************************************************************/
886 * unlink the first link to a key from a keyring
888 int key_unlink(struct key
*keyring
, struct key
*key
)
890 struct keyring_list
*klist
, *nklist
;
897 if (keyring
->type
!= &key_type_keyring
)
900 down_write(&keyring
->sem
);
902 klist
= keyring
->payload
.subscriptions
;
904 /* search the keyring for the key */
905 for (loop
= 0; loop
< klist
->nkeys
; loop
++)
906 if (klist
->keys
[loop
] == key
)
910 up_write(&keyring
->sem
);
915 /* we need to copy the key list for RCU purposes */
916 nklist
= kmalloc(sizeof(*klist
) +
917 sizeof(struct key
*) * klist
->maxkeys
,
921 nklist
->maxkeys
= klist
->maxkeys
;
922 nklist
->nkeys
= klist
->nkeys
- 1;
925 memcpy(&nklist
->keys
[0],
927 loop
* sizeof(struct key
*));
929 if (loop
< nklist
->nkeys
)
930 memcpy(&nklist
->keys
[loop
],
931 &klist
->keys
[loop
+ 1],
932 (nklist
->nkeys
- loop
) * sizeof(struct key
*));
934 /* adjust the user's quota */
935 key_payload_reserve(keyring
,
936 keyring
->datalen
- KEYQUOTA_LINK_BYTES
);
938 rcu_assign_pointer(keyring
->payload
.subscriptions
, nklist
);
940 up_write(&keyring
->sem
);
942 /* schedule for later cleanup */
943 klist
->delkey
= loop
;
944 call_rcu(&klist
->rcu
, keyring_unlink_rcu_disposal
);
952 up_write(&keyring
->sem
);
955 } /* end key_unlink() */
957 EXPORT_SYMBOL(key_unlink
);
959 /*****************************************************************************/
961 * dispose of a keyring list after the RCU grace period, releasing the keys it
964 static void keyring_clear_rcu_disposal(struct rcu_head
*rcu
)
966 struct keyring_list
*klist
;
969 klist
= container_of(rcu
, struct keyring_list
, rcu
);
971 for (loop
= klist
->nkeys
- 1; loop
>= 0; loop
--)
972 key_put(klist
->keys
[loop
]);
976 } /* end keyring_clear_rcu_disposal() */
978 /*****************************************************************************/
980 * clear the specified process keyring
981 * - implements keyctl(KEYCTL_CLEAR)
983 int keyring_clear(struct key
*keyring
)
985 struct keyring_list
*klist
;
989 if (keyring
->type
== &key_type_keyring
) {
990 /* detach the pointer block with the locks held */
991 down_write(&keyring
->sem
);
993 klist
= keyring
->payload
.subscriptions
;
995 /* adjust the quota */
996 key_payload_reserve(keyring
,
997 sizeof(struct keyring_list
));
999 rcu_assign_pointer(keyring
->payload
.subscriptions
,
1003 up_write(&keyring
->sem
);
1005 /* free the keys after the locks have been dropped */
1007 call_rcu(&klist
->rcu
, keyring_clear_rcu_disposal
);
1014 } /* end keyring_clear() */
1016 EXPORT_SYMBOL(keyring_clear
);