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_revoke(struct key
*keyring
);
53 static void keyring_destroy(struct key
*keyring
);
54 static void keyring_describe(const struct key
*keyring
, struct seq_file
*m
);
55 static long keyring_read(const struct key
*keyring
,
56 char __user
*buffer
, size_t buflen
);
58 struct key_type key_type_keyring
= {
60 .def_datalen
= sizeof(struct keyring_list
),
61 .instantiate
= keyring_instantiate
,
62 .match
= keyring_match
,
63 .revoke
= keyring_revoke
,
64 .destroy
= keyring_destroy
,
65 .describe
= keyring_describe
,
70 * semaphore to serialise link/link calls to prevent two link calls in parallel
73 static DECLARE_RWSEM(keyring_serialise_link_sem
);
75 /*****************************************************************************/
77 * publish the name of a keyring so that it can be found by name (if it has
80 void keyring_publish_name(struct key
*keyring
)
84 if (keyring
->description
) {
85 bucket
= keyring_hash(keyring
->description
);
87 write_lock(&keyring_name_lock
);
89 if (!keyring_name_hash
[bucket
].next
)
90 INIT_LIST_HEAD(&keyring_name_hash
[bucket
]);
92 list_add_tail(&keyring
->type_data
.link
,
93 &keyring_name_hash
[bucket
]);
95 write_unlock(&keyring_name_lock
);
98 } /* end keyring_publish_name() */
100 /*****************************************************************************/
102 * initialise a keyring
103 * - we object if we were given any data
105 static int keyring_instantiate(struct key
*keyring
,
106 const void *data
, size_t datalen
)
112 /* make the keyring available by name if it has one */
113 keyring_publish_name(keyring
);
119 } /* end keyring_instantiate() */
121 /*****************************************************************************/
123 * match keyrings on their name
125 static int keyring_match(const struct key
*keyring
, const void *description
)
127 return keyring
->description
&&
128 strcmp(keyring
->description
, description
) == 0;
130 } /* end keyring_match() */
132 /*****************************************************************************/
134 * dispose of the data dangling from the corpse of a keyring
136 static void keyring_destroy(struct key
*keyring
)
138 struct keyring_list
*klist
;
141 if (keyring
->description
) {
142 write_lock(&keyring_name_lock
);
144 if (keyring
->type_data
.link
.next
!= NULL
&&
145 !list_empty(&keyring
->type_data
.link
))
146 list_del(&keyring
->type_data
.link
);
148 write_unlock(&keyring_name_lock
);
151 klist
= rcu_dereference(keyring
->payload
.subscriptions
);
153 for (loop
= klist
->nkeys
- 1; loop
>= 0; loop
--)
154 key_put(klist
->keys
[loop
]);
158 } /* end keyring_destroy() */
160 /*****************************************************************************/
162 * describe the keyring
164 static void keyring_describe(const struct key
*keyring
, struct seq_file
*m
)
166 struct keyring_list
*klist
;
168 if (keyring
->description
) {
169 seq_puts(m
, keyring
->description
);
172 seq_puts(m
, "[anon]");
176 klist
= rcu_dereference(keyring
->payload
.subscriptions
);
178 seq_printf(m
, ": %u/%u", klist
->nkeys
, klist
->maxkeys
);
180 seq_puts(m
, ": empty");
183 } /* end keyring_describe() */
185 /*****************************************************************************/
187 * read a list of key IDs from the keyring's contents
188 * - the keyring's semaphore is read-locked
190 static long keyring_read(const struct key
*keyring
,
191 char __user
*buffer
, size_t buflen
)
193 struct keyring_list
*klist
;
199 klist
= rcu_dereference(keyring
->payload
.subscriptions
);
202 /* calculate how much data we could return */
203 qty
= klist
->nkeys
* sizeof(key_serial_t
);
205 if (buffer
&& buflen
> 0) {
209 /* copy the IDs of the subscribed keys into the
213 for (loop
= 0; loop
< klist
->nkeys
; loop
++) {
214 key
= klist
->keys
[loop
];
216 tmp
= sizeof(key_serial_t
);
220 if (copy_to_user(buffer
,
238 } /* end keyring_read() */
240 /*****************************************************************************/
242 * allocate a keyring and link into the destination keyring
244 struct key
*keyring_alloc(const char *description
, uid_t uid
, gid_t gid
,
245 struct task_struct
*ctx
, unsigned long flags
,
251 keyring
= key_alloc(&key_type_keyring
, description
,
253 (KEY_POS_ALL
& ~KEY_POS_SETATTR
) | KEY_USR_ALL
,
256 if (!IS_ERR(keyring
)) {
257 ret
= key_instantiate_and_link(keyring
, NULL
, 0, dest
, NULL
);
260 keyring
= ERR_PTR(ret
);
266 } /* end keyring_alloc() */
268 /*****************************************************************************/
270 * search the supplied keyring tree for a key that matches the criterion
271 * - perform a breadth-then-depth search up to the prescribed limit
272 * - we only find keys on which we have search permission
273 * - we use the supplied match function to see if the description (or other
274 * feature of interest) matches
275 * - we rely on RCU to prevent the keyring lists from disappearing on us
276 * - we return -EAGAIN if we didn't find any matching key
277 * - we return -ENOKEY if we only found negative matching keys
278 * - we propagate the possession attribute from the keyring ref to the key ref
280 key_ref_t
keyring_search_aux(key_ref_t keyring_ref
,
281 struct task_struct
*context
,
282 struct key_type
*type
,
283 const void *description
,
284 key_match_func_t match
)
287 struct keyring_list
*keylist
;
289 } stack
[KEYRING_SEARCH_MAX_DEPTH
];
291 struct keyring_list
*keylist
;
293 unsigned long possessed
;
294 struct key
*keyring
, *key
;
299 keyring
= key_ref_to_ptr(keyring_ref
);
300 possessed
= is_key_possessed(keyring_ref
);
303 /* top keyring must have search permission to begin the search */
304 err
= key_task_permission(keyring_ref
, context
, KEY_SEARCH
);
306 key_ref
= ERR_PTR(err
);
310 key_ref
= ERR_PTR(-ENOTDIR
);
311 if (keyring
->type
!= &key_type_keyring
)
316 now
= current_kernel_time();
320 /* start processing a new keyring */
322 if (test_bit(KEY_FLAG_REVOKED
, &keyring
->flags
))
323 goto not_this_keyring
;
325 keylist
= rcu_dereference(keyring
->payload
.subscriptions
);
327 goto not_this_keyring
;
329 /* iterate through the keys in this keyring first */
330 for (kix
= 0; kix
< keylist
->nkeys
; kix
++) {
331 key
= keylist
->keys
[kix
];
333 /* ignore keys not of this type */
334 if (key
->type
!= type
)
337 /* skip revoked keys and expired keys */
338 if (test_bit(KEY_FLAG_REVOKED
, &key
->flags
))
341 if (key
->expiry
&& now
.tv_sec
>= key
->expiry
)
344 /* keys that don't match */
345 if (!match(key
, description
))
348 /* key must have search permissions */
349 if (key_task_permission(make_key_ref(key
, possessed
),
350 context
, KEY_SEARCH
) < 0)
353 /* we set a different error code if we find a negative key */
354 if (test_bit(KEY_FLAG_NEGATIVE
, &key
->flags
)) {
362 /* search through the keyrings nested in this one */
365 for (; kix
< keylist
->nkeys
; kix
++) {
366 key
= keylist
->keys
[kix
];
367 if (key
->type
!= &key_type_keyring
)
370 /* recursively search nested keyrings
371 * - only search keyrings for which we have search permission
373 if (sp
>= KEYRING_SEARCH_MAX_DEPTH
)
376 if (key_task_permission(make_key_ref(key
, possessed
),
377 context
, KEY_SEARCH
) < 0)
380 /* stack the current position */
381 stack
[sp
].keylist
= keylist
;
385 /* begin again with the new keyring */
390 /* the keyring we're looking at was disqualified or didn't contain a
394 /* resume the processing of a keyring higher up in the tree */
396 keylist
= stack
[sp
].keylist
;
397 kix
= stack
[sp
].kix
+ 1;
401 key_ref
= ERR_PTR(err
);
404 /* we found a viable match */
406 atomic_inc(&key
->usage
);
408 key_ref
= make_key_ref(key
, possessed
);
414 } /* end keyring_search_aux() */
416 /*****************************************************************************/
418 * search the supplied keyring tree for a key that matches the criterion
419 * - perform a breadth-then-depth search up to the prescribed limit
420 * - we only find keys on which we have search permission
421 * - we readlock the keyrings as we search down the tree
422 * - we return -EAGAIN if we didn't find any matching key
423 * - we return -ENOKEY if we only found negative matching keys
425 key_ref_t
keyring_search(key_ref_t keyring
,
426 struct key_type
*type
,
427 const char *description
)
430 return ERR_PTR(-ENOKEY
);
432 return keyring_search_aux(keyring
, current
,
433 type
, description
, type
->match
);
435 } /* end keyring_search() */
437 EXPORT_SYMBOL(keyring_search
);
439 /*****************************************************************************/
441 * search the given keyring only (no recursion)
442 * - keyring must be locked by caller
443 * - caller must guarantee that the keyring is a keyring
445 key_ref_t
__keyring_search_one(key_ref_t keyring_ref
,
446 const struct key_type
*ktype
,
447 const char *description
,
450 struct keyring_list
*klist
;
451 unsigned long possessed
;
452 struct key
*keyring
, *key
;
455 keyring
= key_ref_to_ptr(keyring_ref
);
456 possessed
= is_key_possessed(keyring_ref
);
460 klist
= rcu_dereference(keyring
->payload
.subscriptions
);
462 for (loop
= 0; loop
< klist
->nkeys
; loop
++) {
463 key
= klist
->keys
[loop
];
465 if (key
->type
== ktype
&&
466 (!key
->type
->match
||
467 key
->type
->match(key
, description
)) &&
468 key_permission(make_key_ref(key
, possessed
),
470 !test_bit(KEY_FLAG_REVOKED
, &key
->flags
)
477 return ERR_PTR(-ENOKEY
);
480 atomic_inc(&key
->usage
);
482 return make_key_ref(key
, possessed
);
484 } /* end __keyring_search_one() */
486 /*****************************************************************************/
488 * find a keyring with the specified name
489 * - all named keyrings are searched
490 * - only find keyrings with search permission for the process
491 * - only find keyrings with a serial number greater than the one specified
493 struct key
*find_keyring_by_name(const char *name
, key_serial_t bound
)
498 keyring
= ERR_PTR(-EINVAL
);
502 bucket
= keyring_hash(name
);
504 read_lock(&keyring_name_lock
);
506 if (keyring_name_hash
[bucket
].next
) {
507 /* search this hash bucket for a keyring with a matching name
508 * that's readable and that hasn't been revoked */
509 list_for_each_entry(keyring
,
510 &keyring_name_hash
[bucket
],
513 if (test_bit(KEY_FLAG_REVOKED
, &keyring
->flags
))
516 if (strcmp(keyring
->description
, name
) != 0)
519 if (key_permission(make_key_ref(keyring
, 0),
523 /* found a potential candidate, but we still need to
524 * check the serial number */
525 if (keyring
->serial
<= bound
)
528 /* we've got a match */
529 atomic_inc(&keyring
->usage
);
530 read_unlock(&keyring_name_lock
);
535 read_unlock(&keyring_name_lock
);
536 keyring
= ERR_PTR(-ENOKEY
);
541 } /* end find_keyring_by_name() */
543 /*****************************************************************************/
545 * see if a cycle will will be created by inserting acyclic tree B in acyclic
546 * tree A at the topmost level (ie: as a direct child of A)
547 * - since we are adding B to A at the top level, checking for cycles should
548 * just be a matter of seeing if node A is somewhere in tree B
550 static int keyring_detect_cycle(struct key
*A
, struct key
*B
)
553 struct keyring_list
*keylist
;
555 } stack
[KEYRING_SEARCH_MAX_DEPTH
];
557 struct keyring_list
*keylist
;
558 struct key
*subtree
, *key
;
570 /* start processing a new keyring */
572 if (test_bit(KEY_FLAG_REVOKED
, &subtree
->flags
))
573 goto not_this_keyring
;
575 keylist
= rcu_dereference(subtree
->payload
.subscriptions
);
577 goto not_this_keyring
;
581 /* iterate through the remaining keys in this keyring */
582 for (; kix
< keylist
->nkeys
; kix
++) {
583 key
= keylist
->keys
[kix
];
588 /* recursively check nested keyrings */
589 if (key
->type
== &key_type_keyring
) {
590 if (sp
>= KEYRING_SEARCH_MAX_DEPTH
)
593 /* stack the current position */
594 stack
[sp
].keylist
= keylist
;
598 /* begin again with the new keyring */
604 /* the keyring we're looking at was disqualified or didn't contain a
608 /* resume the checking of a keyring higher up in the tree */
610 keylist
= stack
[sp
].keylist
;
611 kix
= stack
[sp
].kix
+ 1;
615 ret
= 0; /* no cycles detected */
629 } /* end keyring_detect_cycle() */
631 /*****************************************************************************/
633 * dispose of a keyring list after the RCU grace period
635 static void keyring_link_rcu_disposal(struct rcu_head
*rcu
)
637 struct keyring_list
*klist
=
638 container_of(rcu
, struct keyring_list
, rcu
);
642 } /* end keyring_link_rcu_disposal() */
644 /*****************************************************************************/
646 * dispose of a keyring list after the RCU grace period, freeing the unlinked
649 static void keyring_unlink_rcu_disposal(struct rcu_head
*rcu
)
651 struct keyring_list
*klist
=
652 container_of(rcu
, struct keyring_list
, rcu
);
654 key_put(klist
->keys
[klist
->delkey
]);
657 } /* end keyring_unlink_rcu_disposal() */
659 /*****************************************************************************/
661 * link a key into to a keyring
662 * - must be called with the keyring's semaphore write-locked
663 * - discard already extant link to matching key if there is one
665 int __key_link(struct key
*keyring
, struct key
*key
)
667 struct keyring_list
*klist
, *nklist
;
673 if (test_bit(KEY_FLAG_REVOKED
, &keyring
->flags
))
677 if (keyring
->type
!= &key_type_keyring
)
680 /* serialise link/link calls to prevent parallel calls causing a
681 * cycle when applied to two keyring in opposite orders */
682 down_write(&keyring_serialise_link_sem
);
684 /* check that we aren't going to create a cycle adding one keyring to
686 if (key
->type
== &key_type_keyring
) {
687 ret
= keyring_detect_cycle(keyring
, key
);
692 /* see if there's a matching key we can displace */
693 klist
= keyring
->payload
.subscriptions
;
695 if (klist
&& klist
->nkeys
> 0) {
696 struct key_type
*type
= key
->type
;
698 for (loop
= klist
->nkeys
- 1; loop
>= 0; loop
--) {
699 if (klist
->keys
[loop
]->type
== type
&&
700 strcmp(klist
->keys
[loop
]->description
,
701 key
->description
) == 0
703 /* found a match - replace with new key */
704 size
= sizeof(struct key
*) * klist
->maxkeys
;
705 size
+= sizeof(*klist
);
706 BUG_ON(size
> PAGE_SIZE
);
709 nklist
= kmalloc(size
, GFP_KERNEL
);
713 memcpy(nklist
, klist
, size
);
715 /* replace matched key */
716 atomic_inc(&key
->usage
);
717 nklist
->keys
[loop
] = key
;
720 keyring
->payload
.subscriptions
,
723 /* dispose of the old keyring list and the
725 klist
->delkey
= loop
;
726 call_rcu(&klist
->rcu
,
727 keyring_unlink_rcu_disposal
);
734 /* check that we aren't going to overrun the user's quota */
735 ret
= key_payload_reserve(keyring
,
736 keyring
->datalen
+ KEYQUOTA_LINK_BYTES
);
740 klist
= keyring
->payload
.subscriptions
;
742 if (klist
&& klist
->nkeys
< klist
->maxkeys
) {
743 /* there's sufficient slack space to add directly */
744 atomic_inc(&key
->usage
);
746 klist
->keys
[klist
->nkeys
] = key
;
752 /* grow the key list */
755 max
+= klist
->maxkeys
;
760 size
= sizeof(*klist
) + sizeof(struct key
*) * max
;
761 if (size
> PAGE_SIZE
)
765 nklist
= kmalloc(size
, GFP_KERNEL
);
768 nklist
->maxkeys
= max
;
772 nklist
->nkeys
= klist
->nkeys
;
775 sizeof(struct key
*) * klist
->nkeys
);
778 /* add the key into the new space */
779 atomic_inc(&key
->usage
);
780 nklist
->keys
[nklist
->nkeys
++] = key
;
782 rcu_assign_pointer(keyring
->payload
.subscriptions
, nklist
);
784 /* dispose of the old keyring list */
786 call_rcu(&klist
->rcu
, keyring_link_rcu_disposal
);
792 up_write(&keyring_serialise_link_sem
);
797 /* undo the quota changes */
798 key_payload_reserve(keyring
,
799 keyring
->datalen
- KEYQUOTA_LINK_BYTES
);
802 } /* end __key_link() */
804 /*****************************************************************************/
806 * link a key to a keyring
808 int key_link(struct key
*keyring
, struct key
*key
)
815 down_write(&keyring
->sem
);
816 ret
= __key_link(keyring
, key
);
817 up_write(&keyring
->sem
);
821 } /* end key_link() */
823 EXPORT_SYMBOL(key_link
);
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
);
959 /*****************************************************************************/
961 * dispose of the links from a revoked keyring
962 * - called with the key sem write-locked
964 static void keyring_revoke(struct key
*keyring
)
966 struct keyring_list
*klist
= keyring
->payload
.subscriptions
;
968 /* adjust the quota */
969 key_payload_reserve(keyring
, 0);
972 rcu_assign_pointer(keyring
->payload
.subscriptions
, NULL
);
973 call_rcu(&klist
->rcu
, keyring_clear_rcu_disposal
);
976 } /* end keyring_revoke() */