3 * Copyright (C) 2004-2005, 2008 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 <keys/keyring-type.h>
20 #include <asm/uaccess.h>
24 * when plumbing the depths of the key tree, this sets a hard limit set on how
25 * deep we're willing to go
27 #define KEYRING_SEARCH_MAX_DEPTH 6
30 * we keep all named keyrings in a hash to speed looking them up
32 #define KEYRING_NAME_HASH_SIZE (1 << 5)
34 static struct list_head keyring_name_hash
[KEYRING_NAME_HASH_SIZE
];
35 static DEFINE_RWLOCK(keyring_name_lock
);
37 static inline unsigned keyring_hash(const char *desc
)
42 bucket
+= (unsigned char) *desc
;
44 return bucket
& (KEYRING_NAME_HASH_SIZE
- 1);
48 * the keyring type definition
50 static int keyring_instantiate(struct key
*keyring
,
51 const void *data
, size_t datalen
);
52 static int keyring_match(const struct key
*keyring
, const void *criterion
);
53 static void keyring_revoke(struct key
*keyring
);
54 static void keyring_destroy(struct key
*keyring
);
55 static void keyring_describe(const struct key
*keyring
, struct seq_file
*m
);
56 static long keyring_read(const struct key
*keyring
,
57 char __user
*buffer
, size_t buflen
);
59 struct key_type key_type_keyring
= {
61 .def_datalen
= sizeof(struct keyring_list
),
62 .instantiate
= keyring_instantiate
,
63 .match
= keyring_match
,
64 .revoke
= keyring_revoke
,
65 .destroy
= keyring_destroy
,
66 .describe
= keyring_describe
,
70 EXPORT_SYMBOL(key_type_keyring
);
73 * semaphore to serialise link/link calls to prevent two link calls in parallel
76 static DECLARE_RWSEM(keyring_serialise_link_sem
);
78 /*****************************************************************************/
80 * publish the name of a keyring so that it can be found by name (if it has
83 static void keyring_publish_name(struct key
*keyring
)
87 if (keyring
->description
) {
88 bucket
= keyring_hash(keyring
->description
);
90 write_lock(&keyring_name_lock
);
92 if (!keyring_name_hash
[bucket
].next
)
93 INIT_LIST_HEAD(&keyring_name_hash
[bucket
]);
95 list_add_tail(&keyring
->type_data
.link
,
96 &keyring_name_hash
[bucket
]);
98 write_unlock(&keyring_name_lock
);
101 } /* end keyring_publish_name() */
103 /*****************************************************************************/
105 * initialise a keyring
106 * - we object if we were given any data
108 static int keyring_instantiate(struct key
*keyring
,
109 const void *data
, size_t datalen
)
115 /* make the keyring available by name if it has one */
116 keyring_publish_name(keyring
);
122 } /* end keyring_instantiate() */
124 /*****************************************************************************/
126 * match keyrings on their name
128 static int keyring_match(const struct key
*keyring
, const void *description
)
130 return keyring
->description
&&
131 strcmp(keyring
->description
, description
) == 0;
133 } /* end keyring_match() */
135 /*****************************************************************************/
137 * dispose of the data dangling from the corpse of a keyring
139 static void keyring_destroy(struct key
*keyring
)
141 struct keyring_list
*klist
;
144 if (keyring
->description
) {
145 write_lock(&keyring_name_lock
);
147 if (keyring
->type_data
.link
.next
!= NULL
&&
148 !list_empty(&keyring
->type_data
.link
))
149 list_del(&keyring
->type_data
.link
);
151 write_unlock(&keyring_name_lock
);
154 klist
= rcu_dereference(keyring
->payload
.subscriptions
);
156 for (loop
= klist
->nkeys
- 1; loop
>= 0; loop
--)
157 key_put(klist
->keys
[loop
]);
161 } /* end keyring_destroy() */
163 /*****************************************************************************/
165 * describe the keyring
167 static void keyring_describe(const struct key
*keyring
, struct seq_file
*m
)
169 struct keyring_list
*klist
;
171 if (keyring
->description
) {
172 seq_puts(m
, keyring
->description
);
175 seq_puts(m
, "[anon]");
179 klist
= rcu_dereference(keyring
->payload
.subscriptions
);
181 seq_printf(m
, ": %u/%u", klist
->nkeys
, klist
->maxkeys
);
183 seq_puts(m
, ": empty");
186 } /* end keyring_describe() */
188 /*****************************************************************************/
190 * read a list of key IDs from the keyring's contents
191 * - the keyring's semaphore is read-locked
193 static long keyring_read(const struct key
*keyring
,
194 char __user
*buffer
, size_t buflen
)
196 struct keyring_list
*klist
;
202 klist
= rcu_dereference(keyring
->payload
.subscriptions
);
205 /* calculate how much data we could return */
206 qty
= klist
->nkeys
* sizeof(key_serial_t
);
208 if (buffer
&& buflen
> 0) {
212 /* copy the IDs of the subscribed keys into the
216 for (loop
= 0; loop
< klist
->nkeys
; loop
++) {
217 key
= klist
->keys
[loop
];
219 tmp
= sizeof(key_serial_t
);
223 if (copy_to_user(buffer
,
241 } /* end keyring_read() */
243 /*****************************************************************************/
245 * allocate a keyring and link into the destination keyring
247 struct key
*keyring_alloc(const char *description
, uid_t uid
, gid_t gid
,
248 const struct cred
*cred
, unsigned long flags
,
254 keyring
= key_alloc(&key_type_keyring
, description
,
256 (KEY_POS_ALL
& ~KEY_POS_SETATTR
) | KEY_USR_ALL
,
259 if (!IS_ERR(keyring
)) {
260 ret
= key_instantiate_and_link(keyring
, NULL
, 0, dest
, NULL
);
263 keyring
= ERR_PTR(ret
);
269 } /* end keyring_alloc() */
271 /*****************************************************************************/
273 * search the supplied keyring tree for a key that matches the criterion
274 * - perform a breadth-then-depth search up to the prescribed limit
275 * - we only find keys on which we have search permission
276 * - we use the supplied match function to see if the description (or other
277 * feature of interest) matches
278 * - we rely on RCU to prevent the keyring lists from disappearing on us
279 * - we return -EAGAIN if we didn't find any matching key
280 * - we return -ENOKEY if we only found negative matching keys
281 * - we propagate the possession attribute from the keyring ref to the key ref
283 key_ref_t
keyring_search_aux(key_ref_t keyring_ref
,
284 const struct cred
*cred
,
285 struct key_type
*type
,
286 const void *description
,
287 key_match_func_t match
)
290 struct keyring_list
*keylist
;
292 } stack
[KEYRING_SEARCH_MAX_DEPTH
];
294 struct keyring_list
*keylist
;
296 unsigned long possessed
, kflags
;
297 struct key
*keyring
, *key
;
302 keyring
= key_ref_to_ptr(keyring_ref
);
303 possessed
= is_key_possessed(keyring_ref
);
306 /* top keyring must have search permission to begin the search */
307 err
= key_task_permission(keyring_ref
, cred
, KEY_SEARCH
);
309 key_ref
= ERR_PTR(err
);
313 key_ref
= ERR_PTR(-ENOTDIR
);
314 if (keyring
->type
!= &key_type_keyring
)
319 now
= current_kernel_time();
323 /* firstly we should check to see if this top-level keyring is what we
325 key_ref
= ERR_PTR(-EAGAIN
);
326 kflags
= keyring
->flags
;
327 if (keyring
->type
== type
&& match(keyring
, description
)) {
330 /* check it isn't negative and hasn't expired or been
332 if (kflags
& (1 << KEY_FLAG_REVOKED
))
334 if (key
->expiry
&& now
.tv_sec
>= key
->expiry
)
336 key_ref
= ERR_PTR(-ENOKEY
);
337 if (kflags
& (1 << KEY_FLAG_NEGATIVE
))
342 /* otherwise, the top keyring must not be revoked, expired, or
343 * negatively instantiated if we are to search it */
344 key_ref
= ERR_PTR(-EAGAIN
);
345 if (kflags
& ((1 << KEY_FLAG_REVOKED
) | (1 << KEY_FLAG_NEGATIVE
)) ||
346 (keyring
->expiry
&& now
.tv_sec
>= keyring
->expiry
))
349 /* start processing a new keyring */
351 if (test_bit(KEY_FLAG_REVOKED
, &keyring
->flags
))
352 goto not_this_keyring
;
354 keylist
= rcu_dereference(keyring
->payload
.subscriptions
);
356 goto not_this_keyring
;
358 /* iterate through the keys in this keyring first */
359 for (kix
= 0; kix
< keylist
->nkeys
; kix
++) {
360 key
= keylist
->keys
[kix
];
363 /* ignore keys not of this type */
364 if (key
->type
!= type
)
367 /* skip revoked keys and expired keys */
368 if (kflags
& (1 << KEY_FLAG_REVOKED
))
371 if (key
->expiry
&& now
.tv_sec
>= key
->expiry
)
374 /* keys that don't match */
375 if (!match(key
, description
))
378 /* key must have search permissions */
379 if (key_task_permission(make_key_ref(key
, possessed
),
380 cred
, KEY_SEARCH
) < 0)
383 /* we set a different error code if we pass a negative key */
384 if (kflags
& (1 << KEY_FLAG_NEGATIVE
)) {
392 /* search through the keyrings nested in this one */
395 for (; kix
< keylist
->nkeys
; kix
++) {
396 key
= keylist
->keys
[kix
];
397 if (key
->type
!= &key_type_keyring
)
400 /* recursively search nested keyrings
401 * - only search keyrings for which we have search permission
403 if (sp
>= KEYRING_SEARCH_MAX_DEPTH
)
406 if (key_task_permission(make_key_ref(key
, possessed
),
407 cred
, KEY_SEARCH
) < 0)
410 /* stack the current position */
411 stack
[sp
].keylist
= keylist
;
415 /* begin again with the new keyring */
420 /* the keyring we're looking at was disqualified or didn't contain a
424 /* resume the processing of a keyring higher up in the tree */
426 keylist
= stack
[sp
].keylist
;
427 kix
= stack
[sp
].kix
+ 1;
431 key_ref
= ERR_PTR(err
);
434 /* we found a viable match */
436 atomic_inc(&key
->usage
);
438 key_ref
= make_key_ref(key
, possessed
);
444 } /* end keyring_search_aux() */
446 /*****************************************************************************/
448 * search the supplied keyring tree for a key that matches the criterion
449 * - perform a breadth-then-depth search up to the prescribed limit
450 * - we only find keys on which we have search permission
451 * - we readlock the keyrings as we search down the tree
452 * - we return -EAGAIN if we didn't find any matching key
453 * - we return -ENOKEY if we only found negative matching keys
455 key_ref_t
keyring_search(key_ref_t keyring
,
456 struct key_type
*type
,
457 const char *description
)
460 return ERR_PTR(-ENOKEY
);
462 return keyring_search_aux(keyring
, current
->cred
,
463 type
, description
, type
->match
);
465 } /* end keyring_search() */
467 EXPORT_SYMBOL(keyring_search
);
469 /*****************************************************************************/
471 * search the given keyring only (no recursion)
472 * - keyring must be locked by caller
473 * - caller must guarantee that the keyring is a keyring
475 key_ref_t
__keyring_search_one(key_ref_t keyring_ref
,
476 const struct key_type
*ktype
,
477 const char *description
,
480 struct keyring_list
*klist
;
481 unsigned long possessed
;
482 struct key
*keyring
, *key
;
485 keyring
= key_ref_to_ptr(keyring_ref
);
486 possessed
= is_key_possessed(keyring_ref
);
490 klist
= rcu_dereference(keyring
->payload
.subscriptions
);
492 for (loop
= 0; loop
< klist
->nkeys
; loop
++) {
493 key
= klist
->keys
[loop
];
495 if (key
->type
== ktype
&&
496 (!key
->type
->match
||
497 key
->type
->match(key
, description
)) &&
498 key_permission(make_key_ref(key
, possessed
),
500 !test_bit(KEY_FLAG_REVOKED
, &key
->flags
)
507 return ERR_PTR(-ENOKEY
);
510 atomic_inc(&key
->usage
);
512 return make_key_ref(key
, possessed
);
514 } /* end __keyring_search_one() */
516 /*****************************************************************************/
518 * find a keyring with the specified name
519 * - all named keyrings are searched
520 * - normally only finds keyrings with search permission for the current process
522 struct key
*find_keyring_by_name(const char *name
, bool skip_perm_check
)
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 (keyring
->user
->user_ns
!= current_user_ns())
545 if (test_bit(KEY_FLAG_REVOKED
, &keyring
->flags
))
548 if (strcmp(keyring
->description
, name
) != 0)
551 if (!skip_perm_check
&&
552 key_permission(make_key_ref(keyring
, 0),
556 /* we've got a match */
557 atomic_inc(&keyring
->usage
);
558 read_unlock(&keyring_name_lock
);
563 read_unlock(&keyring_name_lock
);
564 keyring
= ERR_PTR(-ENOKEY
);
569 } /* end find_keyring_by_name() */
571 /*****************************************************************************/
573 * see if a cycle will will be created by inserting acyclic tree B in acyclic
574 * tree A at the topmost level (ie: as a direct child of A)
575 * - since we are adding B to A at the top level, checking for cycles should
576 * just be a matter of seeing if node A is somewhere in tree B
578 static int keyring_detect_cycle(struct key
*A
, struct key
*B
)
581 struct keyring_list
*keylist
;
583 } stack
[KEYRING_SEARCH_MAX_DEPTH
];
585 struct keyring_list
*keylist
;
586 struct key
*subtree
, *key
;
598 /* start processing a new keyring */
600 if (test_bit(KEY_FLAG_REVOKED
, &subtree
->flags
))
601 goto not_this_keyring
;
603 keylist
= rcu_dereference(subtree
->payload
.subscriptions
);
605 goto not_this_keyring
;
609 /* iterate through the remaining keys in this keyring */
610 for (; kix
< keylist
->nkeys
; kix
++) {
611 key
= keylist
->keys
[kix
];
616 /* recursively check nested keyrings */
617 if (key
->type
== &key_type_keyring
) {
618 if (sp
>= KEYRING_SEARCH_MAX_DEPTH
)
621 /* stack the current position */
622 stack
[sp
].keylist
= keylist
;
626 /* begin again with the new keyring */
632 /* the keyring we're looking at was disqualified or didn't contain a
636 /* resume the checking of a keyring higher up in the tree */
638 keylist
= stack
[sp
].keylist
;
639 kix
= stack
[sp
].kix
+ 1;
643 ret
= 0; /* no cycles detected */
657 } /* end keyring_detect_cycle() */
659 /*****************************************************************************/
661 * dispose of a keyring list after the RCU grace period
663 static void keyring_link_rcu_disposal(struct rcu_head
*rcu
)
665 struct keyring_list
*klist
=
666 container_of(rcu
, struct keyring_list
, rcu
);
670 } /* end keyring_link_rcu_disposal() */
672 /*****************************************************************************/
674 * dispose of a keyring list after the RCU grace period, freeing the unlinked
677 static void keyring_unlink_rcu_disposal(struct rcu_head
*rcu
)
679 struct keyring_list
*klist
=
680 container_of(rcu
, struct keyring_list
, rcu
);
682 key_put(klist
->keys
[klist
->delkey
]);
685 } /* end keyring_unlink_rcu_disposal() */
687 /*****************************************************************************/
689 * link a key into to a keyring
690 * - must be called with the keyring's semaphore write-locked
691 * - discard already extant link to matching key if there is one
693 int __key_link(struct key
*keyring
, struct key
*key
)
695 struct keyring_list
*klist
, *nklist
;
701 if (test_bit(KEY_FLAG_REVOKED
, &keyring
->flags
))
705 if (keyring
->type
!= &key_type_keyring
)
708 /* serialise link/link calls to prevent parallel calls causing a
709 * cycle when applied to two keyring in opposite orders */
710 down_write(&keyring_serialise_link_sem
);
712 /* check that we aren't going to create a cycle adding one keyring to
714 if (key
->type
== &key_type_keyring
) {
715 ret
= keyring_detect_cycle(keyring
, key
);
720 /* see if there's a matching key we can displace */
721 klist
= keyring
->payload
.subscriptions
;
723 if (klist
&& klist
->nkeys
> 0) {
724 struct key_type
*type
= key
->type
;
726 for (loop
= klist
->nkeys
- 1; loop
>= 0; loop
--) {
727 if (klist
->keys
[loop
]->type
== type
&&
728 strcmp(klist
->keys
[loop
]->description
,
729 key
->description
) == 0
731 /* found a match - replace with new key */
732 size
= sizeof(struct key
*) * klist
->maxkeys
;
733 size
+= sizeof(*klist
);
734 BUG_ON(size
> PAGE_SIZE
);
737 nklist
= kmemdup(klist
, size
, GFP_KERNEL
);
741 /* replace matched key */
742 atomic_inc(&key
->usage
);
743 nklist
->keys
[loop
] = key
;
746 keyring
->payload
.subscriptions
,
749 /* dispose of the old keyring list and the
751 klist
->delkey
= loop
;
752 call_rcu(&klist
->rcu
,
753 keyring_unlink_rcu_disposal
);
760 /* check that we aren't going to overrun the user's quota */
761 ret
= key_payload_reserve(keyring
,
762 keyring
->datalen
+ KEYQUOTA_LINK_BYTES
);
766 klist
= keyring
->payload
.subscriptions
;
768 if (klist
&& klist
->nkeys
< klist
->maxkeys
) {
769 /* there's sufficient slack space to add directly */
770 atomic_inc(&key
->usage
);
772 klist
->keys
[klist
->nkeys
] = key
;
778 /* grow the key list */
781 max
+= klist
->maxkeys
;
786 size
= sizeof(*klist
) + sizeof(struct key
*) * max
;
787 if (size
> PAGE_SIZE
)
791 nklist
= kmalloc(size
, GFP_KERNEL
);
794 nklist
->maxkeys
= max
;
798 nklist
->nkeys
= klist
->nkeys
;
801 sizeof(struct key
*) * klist
->nkeys
);
804 /* add the key into the new space */
805 atomic_inc(&key
->usage
);
806 nklist
->keys
[nklist
->nkeys
++] = key
;
808 rcu_assign_pointer(keyring
->payload
.subscriptions
, nklist
);
810 /* dispose of the old keyring list */
812 call_rcu(&klist
->rcu
, keyring_link_rcu_disposal
);
818 up_write(&keyring_serialise_link_sem
);
823 /* undo the quota changes */
824 key_payload_reserve(keyring
,
825 keyring
->datalen
- KEYQUOTA_LINK_BYTES
);
828 } /* end __key_link() */
830 /*****************************************************************************/
832 * link a key to a keyring
834 int key_link(struct key
*keyring
, struct key
*key
)
841 down_write(&keyring
->sem
);
842 ret
= __key_link(keyring
, key
);
843 up_write(&keyring
->sem
);
847 } /* end key_link() */
849 EXPORT_SYMBOL(key_link
);
851 /*****************************************************************************/
853 * unlink the first link to a key from a keyring
855 int key_unlink(struct key
*keyring
, struct key
*key
)
857 struct keyring_list
*klist
, *nklist
;
864 if (keyring
->type
!= &key_type_keyring
)
867 down_write(&keyring
->sem
);
869 klist
= keyring
->payload
.subscriptions
;
871 /* search the keyring for the key */
872 for (loop
= 0; loop
< klist
->nkeys
; loop
++)
873 if (klist
->keys
[loop
] == key
)
877 up_write(&keyring
->sem
);
882 /* we need to copy the key list for RCU purposes */
883 nklist
= kmalloc(sizeof(*klist
) +
884 sizeof(struct key
*) * klist
->maxkeys
,
888 nklist
->maxkeys
= klist
->maxkeys
;
889 nklist
->nkeys
= klist
->nkeys
- 1;
892 memcpy(&nklist
->keys
[0],
894 loop
* sizeof(struct key
*));
896 if (loop
< nklist
->nkeys
)
897 memcpy(&nklist
->keys
[loop
],
898 &klist
->keys
[loop
+ 1],
899 (nklist
->nkeys
- loop
) * sizeof(struct key
*));
901 /* adjust the user's quota */
902 key_payload_reserve(keyring
,
903 keyring
->datalen
- KEYQUOTA_LINK_BYTES
);
905 rcu_assign_pointer(keyring
->payload
.subscriptions
, nklist
);
907 up_write(&keyring
->sem
);
909 /* schedule for later cleanup */
910 klist
->delkey
= loop
;
911 call_rcu(&klist
->rcu
, keyring_unlink_rcu_disposal
);
919 up_write(&keyring
->sem
);
922 } /* end key_unlink() */
924 EXPORT_SYMBOL(key_unlink
);
926 /*****************************************************************************/
928 * dispose of a keyring list after the RCU grace period, releasing the keys it
931 static void keyring_clear_rcu_disposal(struct rcu_head
*rcu
)
933 struct keyring_list
*klist
;
936 klist
= container_of(rcu
, struct keyring_list
, rcu
);
938 for (loop
= klist
->nkeys
- 1; loop
>= 0; loop
--)
939 key_put(klist
->keys
[loop
]);
943 } /* end keyring_clear_rcu_disposal() */
945 /*****************************************************************************/
947 * clear the specified process keyring
948 * - implements keyctl(KEYCTL_CLEAR)
950 int keyring_clear(struct key
*keyring
)
952 struct keyring_list
*klist
;
956 if (keyring
->type
== &key_type_keyring
) {
957 /* detach the pointer block with the locks held */
958 down_write(&keyring
->sem
);
960 klist
= keyring
->payload
.subscriptions
;
962 /* adjust the quota */
963 key_payload_reserve(keyring
,
964 sizeof(struct keyring_list
));
966 rcu_assign_pointer(keyring
->payload
.subscriptions
,
970 up_write(&keyring
->sem
);
972 /* free the keys after the locks have been dropped */
974 call_rcu(&klist
->rcu
, keyring_clear_rcu_disposal
);
981 } /* end keyring_clear() */
983 EXPORT_SYMBOL(keyring_clear
);
985 /*****************************************************************************/
987 * dispose of the links from a revoked keyring
988 * - called with the key sem write-locked
990 static void keyring_revoke(struct key
*keyring
)
992 struct keyring_list
*klist
= keyring
->payload
.subscriptions
;
994 /* adjust the quota */
995 key_payload_reserve(keyring
, 0);
998 rcu_assign_pointer(keyring
->payload
.subscriptions
, NULL
);
999 call_rcu(&klist
->rcu
, keyring_clear_rcu_disposal
);
1002 } /* end keyring_revoke() */