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_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
337 struct key
*keyring_search_aux(struct key
*keyring
,
338 struct task_struct
*context
,
339 struct key_type
*type
,
340 const void *description
,
341 key_match_func_t match
)
344 struct keyring_list
*keylist
;
346 } stack
[KEYRING_SEARCH_MAX_DEPTH
];
348 struct keyring_list
*keylist
;
358 /* top keyring must have search permission to begin the search */
359 key
= ERR_PTR(-EACCES
);
360 if (!key_task_permission(keyring
, context
, KEY_SEARCH
))
363 key
= ERR_PTR(-ENOTDIR
);
364 if (keyring
->type
!= &key_type_keyring
)
367 now
= current_kernel_time();
371 /* start processing a new keyring */
373 if (test_bit(KEY_FLAG_REVOKED
, &keyring
->flags
))
374 goto not_this_keyring
;
376 keylist
= rcu_dereference(keyring
->payload
.subscriptions
);
378 goto not_this_keyring
;
380 /* iterate through the keys in this keyring first */
381 for (kix
= 0; kix
< keylist
->nkeys
; kix
++) {
382 key
= keylist
->keys
[kix
];
384 /* ignore keys not of this type */
385 if (key
->type
!= type
)
388 /* skip revoked keys and expired keys */
389 if (test_bit(KEY_FLAG_REVOKED
, &key
->flags
))
392 if (key
->expiry
&& now
.tv_sec
>= key
->expiry
)
395 /* keys that don't match */
396 if (!match(key
, description
))
399 /* key must have search permissions */
400 if (!key_task_permission(key
, context
, KEY_SEARCH
))
403 /* we set a different error code if we find a negative key */
404 if (test_bit(KEY_FLAG_NEGATIVE
, &key
->flags
)) {
412 /* search through the keyrings nested in this one */
415 for (; kix
< keylist
->nkeys
; kix
++) {
416 key
= keylist
->keys
[kix
];
417 if (key
->type
!= &key_type_keyring
)
420 /* recursively search nested keyrings
421 * - only search keyrings for which we have search permission
423 if (sp
>= KEYRING_SEARCH_MAX_DEPTH
)
426 if (!key_task_permission(key
, context
, KEY_SEARCH
))
429 /* stack the current position */
430 stack
[sp
].keylist
= keylist
;
434 /* begin again with the new keyring */
439 /* the keyring we're looking at was disqualified or didn't contain a
443 /* resume the processing of a keyring higher up in the tree */
445 keylist
= stack
[sp
].keylist
;
446 kix
= stack
[sp
].kix
+ 1;
453 /* we found a viable match */
455 atomic_inc(&key
->usage
);
461 } /* end keyring_search_aux() */
463 /*****************************************************************************/
465 * search the supplied keyring tree for a key that matches the criterion
466 * - perform a breadth-then-depth search up to the prescribed limit
467 * - we only find keys on which we have search permission
468 * - we readlock the keyrings as we search down the tree
469 * - we return -EAGAIN if we didn't find any matching key
470 * - we return -ENOKEY if we only found negative matching keys
472 struct key
*keyring_search(struct key
*keyring
,
473 struct key_type
*type
,
474 const char *description
)
477 return ERR_PTR(-ENOKEY
);
479 return keyring_search_aux(keyring
, current
,
480 type
, description
, type
->match
);
482 } /* end keyring_search() */
484 EXPORT_SYMBOL(keyring_search
);
486 /*****************************************************************************/
488 * search the given keyring only (no recursion)
489 * - keyring must be locked by caller
491 struct key
*__keyring_search_one(struct key
*keyring
,
492 const struct key_type
*ktype
,
493 const char *description
,
496 struct keyring_list
*klist
;
502 klist
= rcu_dereference(keyring
->payload
.subscriptions
);
504 for (loop
= 0; loop
< klist
->nkeys
; loop
++) {
505 key
= klist
->keys
[loop
];
507 if (key
->type
== ktype
&&
508 (!key
->type
->match
||
509 key
->type
->match(key
, description
)) &&
510 key_permission(key
, perm
) &&
511 !test_bit(KEY_FLAG_REVOKED
, &key
->flags
)
517 key
= ERR_PTR(-ENOKEY
);
521 atomic_inc(&key
->usage
);
526 } /* end __keyring_search_one() */
528 /*****************************************************************************/
530 * search for an instantiation authorisation key matching a target key
531 * - the RCU read lock must be held by the caller
532 * - a target_id of zero specifies any valid token
534 struct key
*keyring_search_instkey(struct key
*keyring
,
535 key_serial_t target_id
)
537 struct request_key_auth
*rka
;
538 struct keyring_list
*klist
;
542 klist
= rcu_dereference(keyring
->payload
.subscriptions
);
544 for (loop
= 0; loop
< klist
->nkeys
; loop
++) {
545 instkey
= klist
->keys
[loop
];
547 if (instkey
->type
!= &key_type_request_key_auth
)
550 rka
= instkey
->payload
.data
;
551 if (target_id
&& rka
->target_key
->serial
!= target_id
)
554 /* the auth key is revoked during instantiation */
555 if (!test_bit(KEY_FLAG_REVOKED
, &instkey
->flags
))
558 instkey
= ERR_PTR(-EKEYREVOKED
);
563 instkey
= ERR_PTR(-EACCES
);
567 atomic_inc(&instkey
->usage
);
571 } /* end keyring_search_instkey() */
573 /*****************************************************************************/
575 * find a keyring with the specified name
576 * - all named keyrings are searched
577 * - only find keyrings with search permission for the process
578 * - only find keyrings with a serial number greater than the one specified
580 struct key
*find_keyring_by_name(const char *name
, key_serial_t bound
)
585 keyring
= ERR_PTR(-EINVAL
);
589 bucket
= keyring_hash(name
);
591 read_lock(&keyring_name_lock
);
593 if (keyring_name_hash
[bucket
].next
) {
594 /* search this hash bucket for a keyring with a matching name
595 * that's readable and that hasn't been revoked */
596 list_for_each_entry(keyring
,
597 &keyring_name_hash
[bucket
],
600 if (test_bit(KEY_FLAG_REVOKED
, &keyring
->flags
))
603 if (strcmp(keyring
->description
, name
) != 0)
606 if (!key_permission(keyring
, KEY_SEARCH
))
609 /* found a potential candidate, but we still need to
610 * check the serial number */
611 if (keyring
->serial
<= bound
)
614 /* we've got a match */
615 atomic_inc(&keyring
->usage
);
616 read_unlock(&keyring_name_lock
);
621 read_unlock(&keyring_name_lock
);
622 keyring
= ERR_PTR(-ENOKEY
);
627 } /* end find_keyring_by_name() */
629 /*****************************************************************************/
631 * see if a cycle will will be created by inserting acyclic tree B in acyclic
632 * tree A at the topmost level (ie: as a direct child of A)
633 * - since we are adding B to A at the top level, checking for cycles should
634 * just be a matter of seeing if node A is somewhere in tree B
636 static int keyring_detect_cycle(struct key
*A
, struct key
*B
)
639 struct keyring_list
*keylist
;
641 } stack
[KEYRING_SEARCH_MAX_DEPTH
];
643 struct keyring_list
*keylist
;
644 struct key
*subtree
, *key
;
656 /* start processing a new keyring */
658 if (test_bit(KEY_FLAG_REVOKED
, &subtree
->flags
))
659 goto not_this_keyring
;
661 keylist
= rcu_dereference(subtree
->payload
.subscriptions
);
663 goto not_this_keyring
;
667 /* iterate through the remaining keys in this keyring */
668 for (; kix
< keylist
->nkeys
; kix
++) {
669 key
= keylist
->keys
[kix
];
674 /* recursively check nested keyrings */
675 if (key
->type
== &key_type_keyring
) {
676 if (sp
>= KEYRING_SEARCH_MAX_DEPTH
)
679 /* stack the current position */
680 stack
[sp
].keylist
= keylist
;
684 /* begin again with the new keyring */
690 /* the keyring we're looking at was disqualified or didn't contain a
694 /* resume the checking of a keyring higher up in the tree */
696 keylist
= stack
[sp
].keylist
;
697 kix
= stack
[sp
].kix
+ 1;
701 ret
= 0; /* no cycles detected */
715 } /* end keyring_detect_cycle() */
717 /*****************************************************************************/
719 * dispose of a keyring list after the RCU grace period
721 static void keyring_link_rcu_disposal(struct rcu_head
*rcu
)
723 struct keyring_list
*klist
=
724 container_of(rcu
, struct keyring_list
, rcu
);
728 } /* end keyring_link_rcu_disposal() */
730 /*****************************************************************************/
732 * link a key into to a keyring
733 * - must be called with the keyring's semaphore write-locked
735 int __key_link(struct key
*keyring
, struct key
*key
)
737 struct keyring_list
*klist
, *nklist
;
743 if (test_bit(KEY_FLAG_REVOKED
, &keyring
->flags
))
747 if (keyring
->type
!= &key_type_keyring
)
750 /* serialise link/link calls to prevent parallel calls causing a
751 * cycle when applied to two keyring in opposite orders */
752 down_write(&keyring_serialise_link_sem
);
754 /* check that we aren't going to create a cycle adding one keyring to
756 if (key
->type
== &key_type_keyring
) {
757 ret
= keyring_detect_cycle(keyring
, key
);
762 /* check that we aren't going to overrun the user's quota */
763 ret
= key_payload_reserve(keyring
,
764 keyring
->datalen
+ KEYQUOTA_LINK_BYTES
);
768 klist
= keyring
->payload
.subscriptions
;
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
;
782 /* grow the key list */
785 max
+= klist
->maxkeys
;
790 size
= sizeof(*klist
) + sizeof(struct key
*) * max
;
791 if (size
> PAGE_SIZE
)
795 nklist
= kmalloc(size
, GFP_KERNEL
);
798 nklist
->maxkeys
= max
;
802 nklist
->nkeys
= klist
->nkeys
;
805 sizeof(struct key
*) * klist
->nkeys
);
808 /* add the key into the new space */
809 atomic_inc(&key
->usage
);
810 nklist
->keys
[nklist
->nkeys
++] = key
;
812 rcu_assign_pointer(keyring
->payload
.subscriptions
, nklist
);
814 /* dispose of the old keyring list */
816 call_rcu(&klist
->rcu
, keyring_link_rcu_disposal
);
822 up_write(&keyring_serialise_link_sem
);
827 /* undo the quota changes */
828 key_payload_reserve(keyring
,
829 keyring
->datalen
- KEYQUOTA_LINK_BYTES
);
832 } /* end __key_link() */
834 /*****************************************************************************/
836 * link a key to a keyring
838 int key_link(struct key
*keyring
, struct key
*key
)
845 down_write(&keyring
->sem
);
846 ret
= __key_link(keyring
, key
);
847 up_write(&keyring
->sem
);
851 } /* end key_link() */
853 EXPORT_SYMBOL(key_link
);
855 /*****************************************************************************/
857 * dispose of a keyring list after the RCU grace period, freeing the unlinked
860 static void keyring_unlink_rcu_disposal(struct rcu_head
*rcu
)
862 struct keyring_list
*klist
=
863 container_of(rcu
, struct keyring_list
, rcu
);
865 key_put(klist
->keys
[klist
->delkey
]);
868 } /* end keyring_unlink_rcu_disposal() */
870 /*****************************************************************************/
872 * unlink the first link to a key from a keyring
874 int key_unlink(struct key
*keyring
, struct key
*key
)
876 struct keyring_list
*klist
, *nklist
;
883 if (keyring
->type
!= &key_type_keyring
)
886 down_write(&keyring
->sem
);
888 klist
= keyring
->payload
.subscriptions
;
890 /* search the keyring for the key */
891 for (loop
= 0; loop
< klist
->nkeys
; loop
++)
892 if (klist
->keys
[loop
] == key
)
896 up_write(&keyring
->sem
);
901 /* we need to copy the key list for RCU purposes */
902 nklist
= kmalloc(sizeof(*klist
) +
903 sizeof(struct key
*) * klist
->maxkeys
,
907 nklist
->maxkeys
= klist
->maxkeys
;
908 nklist
->nkeys
= klist
->nkeys
- 1;
911 memcpy(&nklist
->keys
[0],
913 loop
* sizeof(struct key
*));
915 if (loop
< nklist
->nkeys
)
916 memcpy(&nklist
->keys
[loop
],
917 &klist
->keys
[loop
+ 1],
918 (nklist
->nkeys
- loop
) * sizeof(struct key
*));
920 /* adjust the user's quota */
921 key_payload_reserve(keyring
,
922 keyring
->datalen
- KEYQUOTA_LINK_BYTES
);
924 rcu_assign_pointer(keyring
->payload
.subscriptions
, nklist
);
926 up_write(&keyring
->sem
);
928 /* schedule for later cleanup */
929 klist
->delkey
= loop
;
930 call_rcu(&klist
->rcu
, keyring_unlink_rcu_disposal
);
938 up_write(&keyring
->sem
);
941 } /* end key_unlink() */
943 EXPORT_SYMBOL(key_unlink
);
945 /*****************************************************************************/
947 * dispose of a keyring list after the RCU grace period, releasing the keys it
950 static void keyring_clear_rcu_disposal(struct rcu_head
*rcu
)
952 struct keyring_list
*klist
;
955 klist
= container_of(rcu
, struct keyring_list
, rcu
);
957 for (loop
= klist
->nkeys
- 1; loop
>= 0; loop
--)
958 key_put(klist
->keys
[loop
]);
962 } /* end keyring_clear_rcu_disposal() */
964 /*****************************************************************************/
966 * clear the specified process keyring
967 * - implements keyctl(KEYCTL_CLEAR)
969 int keyring_clear(struct key
*keyring
)
971 struct keyring_list
*klist
;
975 if (keyring
->type
== &key_type_keyring
) {
976 /* detach the pointer block with the locks held */
977 down_write(&keyring
->sem
);
979 klist
= keyring
->payload
.subscriptions
;
981 /* adjust the quota */
982 key_payload_reserve(keyring
,
983 sizeof(struct keyring_list
));
985 rcu_assign_pointer(keyring
->payload
.subscriptions
,
989 up_write(&keyring
->sem
);
991 /* free the keys after the locks have been dropped */
993 call_rcu(&klist
->rcu
, keyring_clear_rcu_disposal
);
1000 } /* end keyring_clear() */
1002 EXPORT_SYMBOL(keyring_clear
);