[PATCH] Input: export input_dev data via sysfs attributes
[linux-2.6/linux-2.6-openrd.git] / security / keys / keyring.c
blob0639396dd441eabdc27418b66df7673a749419e8
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>
19 #include "internal.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)
37 unsigned bucket = 0;
39 for (; *desc; 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 = {
58 .name = "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,
65 .read = keyring_read,
69 * semaphore to serialise link/link calls to prevent two link calls in parallel
70 * introducing a cycle
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
77 * one)
79 void keyring_publish_name(struct key *keyring)
81 int bucket;
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)
107 int ret;
109 ret = -EINVAL;
110 if (datalen == 0) {
111 /* make the keyring available by name if it has one */
112 keyring_publish_name(keyring);
113 ret = 0;
116 return ret;
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;
127 unsigned max;
128 size_t size;
129 int loop, ret;
131 const unsigned limit =
132 (PAGE_SIZE - sizeof(*klist)) / sizeof(struct key *);
134 ret = 0;
136 /* find out how many keys are currently linked */
137 rcu_read_lock();
138 sklist = rcu_dereference(source->payload.subscriptions);
139 max = 0;
140 if (sklist)
141 max = sklist->nkeys;
142 rcu_read_unlock();
144 /* allocate a new payload and stuff load with key links */
145 if (max > 0) {
146 BUG_ON(max > limit);
148 max = (max + 3) & ~3;
149 if (max > limit)
150 max = limit;
152 ret = -ENOMEM;
153 size = sizeof(*klist) + sizeof(struct key *) * max;
154 klist = kmalloc(size, GFP_KERNEL);
155 if (!klist)
156 goto error;
158 /* set links */
159 rcu_read_lock();
160 sklist = rcu_dereference(source->payload.subscriptions);
162 klist->maxkeys = max;
163 klist->nkeys = sklist->nkeys;
164 memcpy(klist->keys,
165 sklist->keys,
166 sklist->nkeys * sizeof(struct key *));
168 for (loop = klist->nkeys - 1; loop >= 0; loop--)
169 atomic_inc(&klist->keys[loop]->usage);
171 rcu_read_unlock();
173 rcu_assign_pointer(keyring->payload.subscriptions, klist);
174 ret = 0;
177 error:
178 return ret;
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;
200 int loop;
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);
213 if (klist) {
214 for (loop = klist->nkeys - 1; loop >= 0; loop--)
215 key_put(klist->keys[loop]);
216 kfree(klist);
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);
232 else {
233 seq_puts(m, "[anon]");
236 rcu_read_lock();
237 klist = rcu_dereference(keyring->payload.subscriptions);
238 if (klist)
239 seq_printf(m, ": %u/%u", klist->nkeys, klist->maxkeys);
240 else
241 seq_puts(m, ": empty");
242 rcu_read_unlock();
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;
255 struct key *key;
256 size_t qty, tmp;
257 int loop, ret;
259 ret = 0;
260 klist = rcu_dereference(keyring->payload.subscriptions);
262 if (klist) {
263 /* calculate how much data we could return */
264 qty = klist->nkeys * sizeof(key_serial_t);
266 if (buffer && buflen > 0) {
267 if (buflen > qty)
268 buflen = qty;
270 /* copy the IDs of the subscribed keys into the
271 * buffer */
272 ret = -EFAULT;
274 for (loop = 0; loop < klist->nkeys; loop++) {
275 key = klist->keys[loop];
277 tmp = sizeof(key_serial_t);
278 if (tmp > buflen)
279 tmp = buflen;
281 if (copy_to_user(buffer,
282 &key->serial,
283 tmp) != 0)
284 goto error;
286 buflen -= tmp;
287 if (buflen == 0)
288 break;
289 buffer += tmp;
293 ret = qty;
296 error:
297 return ret;
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)
308 struct key *keyring;
309 int ret;
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);
316 if (ret < 0) {
317 key_put(keyring);
318 keyring = ERR_PTR(ret);
322 return keyring;
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)
344 struct {
345 struct keyring_list *keylist;
346 int kix;
347 } stack[KEYRING_SEARCH_MAX_DEPTH];
349 struct keyring_list *keylist;
350 struct timespec now;
351 unsigned long possessed;
352 struct key *keyring, *key;
353 key_ref_t key_ref;
354 long err;
355 int sp, kix;
357 keyring = key_ref_to_ptr(keyring_ref);
358 possessed = is_key_possessed(keyring_ref);
359 key_check(keyring);
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))
364 goto error;
366 key_ref = ERR_PTR(-ENOTDIR);
367 if (keyring->type != &key_type_keyring)
368 goto error;
370 rcu_read_lock();
372 now = current_kernel_time();
373 err = -EAGAIN;
374 sp = 0;
376 /* start processing a new keyring */
377 descend:
378 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
379 goto not_this_keyring;
381 keylist = rcu_dereference(keyring->payload.subscriptions);
382 if (!keylist)
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)
391 continue;
393 /* skip revoked keys and expired keys */
394 if (test_bit(KEY_FLAG_REVOKED, &key->flags))
395 continue;
397 if (key->expiry && now.tv_sec >= key->expiry)
398 continue;
400 /* keys that don't match */
401 if (!match(key, description))
402 continue;
404 /* key must have search permissions */
405 if (!key_task_permission(make_key_ref(key, possessed),
406 context, KEY_SEARCH))
407 continue;
409 /* we set a different error code if we find a negative key */
410 if (test_bit(KEY_FLAG_NEGATIVE, &key->flags)) {
411 err = -ENOKEY;
412 continue;
415 goto found;
418 /* search through the keyrings nested in this one */
419 kix = 0;
420 ascend:
421 for (; kix < keylist->nkeys; kix++) {
422 key = keylist->keys[kix];
423 if (key->type != &key_type_keyring)
424 continue;
426 /* recursively search nested keyrings
427 * - only search keyrings for which we have search permission
429 if (sp >= KEYRING_SEARCH_MAX_DEPTH)
430 continue;
432 if (!key_task_permission(make_key_ref(key, possessed),
433 context, KEY_SEARCH))
434 continue;
436 /* stack the current position */
437 stack[sp].keylist = keylist;
438 stack[sp].kix = kix;
439 sp++;
441 /* begin again with the new keyring */
442 keyring = key;
443 goto descend;
446 /* the keyring we're looking at was disqualified or didn't contain a
447 * matching key */
448 not_this_keyring:
449 if (sp > 0) {
450 /* resume the processing of a keyring higher up in the tree */
451 sp--;
452 keylist = stack[sp].keylist;
453 kix = stack[sp].kix + 1;
454 goto ascend;
457 key_ref = ERR_PTR(err);
458 goto error_2;
460 /* we found a viable match */
461 found:
462 atomic_inc(&key->usage);
463 key_check(key);
464 key_ref = make_key_ref(key, possessed);
465 error_2:
466 rcu_read_unlock();
467 error:
468 return key_ref;
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)
485 if (!type->match)
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,
503 key_perm_t perm)
505 struct keyring_list *klist;
506 unsigned long possessed;
507 struct key *keyring, *key;
508 int loop;
510 keyring = key_ref_to_ptr(keyring_ref);
511 possessed = is_key_possessed(keyring_ref);
513 rcu_read_lock();
515 klist = rcu_dereference(keyring->payload.subscriptions);
516 if (klist) {
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),
524 perm) &&
525 !test_bit(KEY_FLAG_REVOKED, &key->flags)
527 goto found;
531 rcu_read_unlock();
532 return ERR_PTR(-ENOKEY);
534 found:
535 atomic_inc(&key->usage);
536 rcu_read_unlock();
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;
552 struct key *instkey;
553 int loop;
555 klist = rcu_dereference(keyring->payload.subscriptions);
556 if (klist) {
557 for (loop = 0; loop < klist->nkeys; loop++) {
558 instkey = klist->keys[loop];
560 if (instkey->type != &key_type_request_key_auth)
561 continue;
563 rka = instkey->payload.data;
564 if (target_id && rka->target_key->serial != target_id)
565 continue;
567 /* the auth key is revoked during instantiation */
568 if (!test_bit(KEY_FLAG_REVOKED, &instkey->flags))
569 goto found;
571 instkey = ERR_PTR(-EKEYREVOKED);
572 goto error;
576 instkey = ERR_PTR(-EACCES);
577 goto error;
579 found:
580 atomic_inc(&instkey->usage);
581 error:
582 return instkey;
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)
595 struct key *keyring;
596 int bucket;
598 keyring = ERR_PTR(-EINVAL);
599 if (!name)
600 goto error;
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],
611 type_data.link
613 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
614 continue;
616 if (strcmp(keyring->description, name) != 0)
617 continue;
619 if (!key_permission(make_key_ref(keyring, 0),
620 KEY_SEARCH))
621 continue;
623 /* found a potential candidate, but we still need to
624 * check the serial number */
625 if (keyring->serial <= bound)
626 continue;
628 /* we've got a match */
629 atomic_inc(&keyring->usage);
630 read_unlock(&keyring_name_lock);
631 goto error;
635 read_unlock(&keyring_name_lock);
636 keyring = ERR_PTR(-ENOKEY);
638 error:
639 return keyring;
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)
652 struct {
653 struct keyring_list *keylist;
654 int kix;
655 } stack[KEYRING_SEARCH_MAX_DEPTH];
657 struct keyring_list *keylist;
658 struct key *subtree, *key;
659 int sp, kix, ret;
661 rcu_read_lock();
663 ret = -EDEADLK;
664 if (A == B)
665 goto cycle_detected;
667 subtree = B;
668 sp = 0;
670 /* start processing a new keyring */
671 descend:
672 if (test_bit(KEY_FLAG_REVOKED, &subtree->flags))
673 goto not_this_keyring;
675 keylist = rcu_dereference(subtree->payload.subscriptions);
676 if (!keylist)
677 goto not_this_keyring;
678 kix = 0;
680 ascend:
681 /* iterate through the remaining keys in this keyring */
682 for (; kix < keylist->nkeys; kix++) {
683 key = keylist->keys[kix];
685 if (key == A)
686 goto cycle_detected;
688 /* recursively check nested keyrings */
689 if (key->type == &key_type_keyring) {
690 if (sp >= KEYRING_SEARCH_MAX_DEPTH)
691 goto too_deep;
693 /* stack the current position */
694 stack[sp].keylist = keylist;
695 stack[sp].kix = kix;
696 sp++;
698 /* begin again with the new keyring */
699 subtree = key;
700 goto descend;
704 /* the keyring we're looking at was disqualified or didn't contain a
705 * matching key */
706 not_this_keyring:
707 if (sp > 0) {
708 /* resume the checking of a keyring higher up in the tree */
709 sp--;
710 keylist = stack[sp].keylist;
711 kix = stack[sp].kix + 1;
712 goto ascend;
715 ret = 0; /* no cycles detected */
717 error:
718 rcu_read_unlock();
719 return ret;
721 too_deep:
722 ret = -ELOOP;
723 goto error;
725 cycle_detected:
726 ret = -EDEADLK;
727 goto error;
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);
740 kfree(klist);
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;
752 unsigned max;
753 size_t size;
754 int ret;
756 ret = -EKEYREVOKED;
757 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
758 goto error;
760 ret = -ENOTDIR;
761 if (keyring->type != &key_type_keyring)
762 goto error;
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
769 * another */
770 if (key->type == &key_type_keyring) {
771 ret = keyring_detect_cycle(keyring, key);
772 if (ret < 0)
773 goto error2;
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);
779 if (ret < 0)
780 goto error2;
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;
789 smp_wmb();
790 klist->nkeys++;
791 smp_wmb();
793 ret = 0;
795 else {
796 /* grow the key list */
797 max = 4;
798 if (klist)
799 max += klist->maxkeys;
801 ret = -ENFILE;
802 if (max > 65535)
803 goto error3;
804 size = sizeof(*klist) + sizeof(struct key *) * max;
805 if (size > PAGE_SIZE)
806 goto error3;
808 ret = -ENOMEM;
809 nklist = kmalloc(size, GFP_KERNEL);
810 if (!nklist)
811 goto error3;
812 nklist->maxkeys = max;
813 nklist->nkeys = 0;
815 if (klist) {
816 nklist->nkeys = klist->nkeys;
817 memcpy(nklist->keys,
818 klist->keys,
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 */
829 if (klist)
830 call_rcu(&klist->rcu, keyring_link_rcu_disposal);
832 ret = 0;
835 error2:
836 up_write(&keyring_serialise_link_sem);
837 error:
838 return ret;
840 error3:
841 /* undo the quota changes */
842 key_payload_reserve(keyring,
843 keyring->datalen - KEYQUOTA_LINK_BYTES);
844 goto error2;
846 } /* end __key_link() */
848 /*****************************************************************************/
850 * link a key to a keyring
852 int key_link(struct key *keyring, struct key *key)
854 int ret;
856 key_check(keyring);
857 key_check(key);
859 down_write(&keyring->sem);
860 ret = __key_link(keyring, key);
861 up_write(&keyring->sem);
863 return ret;
865 } /* end key_link() */
867 EXPORT_SYMBOL(key_link);
869 /*****************************************************************************/
871 * dispose of a keyring list after the RCU grace period, freeing the unlinked
872 * key
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]);
880 kfree(klist);
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;
891 int loop, ret;
893 key_check(keyring);
894 key_check(key);
896 ret = -ENOTDIR;
897 if (keyring->type != &key_type_keyring)
898 goto error;
900 down_write(&keyring->sem);
902 klist = keyring->payload.subscriptions;
903 if (klist) {
904 /* search the keyring for the key */
905 for (loop = 0; loop < klist->nkeys; loop++)
906 if (klist->keys[loop] == key)
907 goto key_is_present;
910 up_write(&keyring->sem);
911 ret = -ENOENT;
912 goto error;
914 key_is_present:
915 /* we need to copy the key list for RCU purposes */
916 nklist = kmalloc(sizeof(*klist) +
917 sizeof(struct key *) * klist->maxkeys,
918 GFP_KERNEL);
919 if (!nklist)
920 goto nomem;
921 nklist->maxkeys = klist->maxkeys;
922 nklist->nkeys = klist->nkeys - 1;
924 if (loop > 0)
925 memcpy(&nklist->keys[0],
926 &klist->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);
946 ret = 0;
948 error:
949 return ret;
950 nomem:
951 ret = -ENOMEM;
952 up_write(&keyring->sem);
953 goto error;
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
962 * links to
964 static void keyring_clear_rcu_disposal(struct rcu_head *rcu)
966 struct keyring_list *klist;
967 int loop;
969 klist = container_of(rcu, struct keyring_list, rcu);
971 for (loop = klist->nkeys - 1; loop >= 0; loop--)
972 key_put(klist->keys[loop]);
974 kfree(klist);
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;
986 int ret;
988 ret = -ENOTDIR;
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;
994 if (klist) {
995 /* adjust the quota */
996 key_payload_reserve(keyring,
997 sizeof(struct keyring_list));
999 rcu_assign_pointer(keyring->payload.subscriptions,
1000 NULL);
1003 up_write(&keyring->sem);
1005 /* free the keys after the locks have been dropped */
1006 if (klist)
1007 call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
1009 ret = 0;
1012 return ret;
1014 } /* end keyring_clear() */
1016 EXPORT_SYMBOL(keyring_clear);