[PATCH] Keys: Remove incorrect and obsolete '!' operators
[linux-2.6/libata-dev.git] / security / keys / keyring.c
blobc7a0ab1cfda35aacd868504c3033417c58c9e9e7
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>
20 #include "internal.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)
38 unsigned bucket = 0;
40 for (; *desc; 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_duplicate(struct key *keyring, const struct key *source);
52 static int keyring_match(const struct key *keyring, const void *criterion);
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 = {
59 .name = "keyring",
60 .def_datalen = sizeof(struct keyring_list),
61 .instantiate = keyring_instantiate,
62 .duplicate = keyring_duplicate,
63 .match = keyring_match,
64 .destroy = keyring_destroy,
65 .describe = keyring_describe,
66 .read = keyring_read,
70 * semaphore to serialise link/link calls to prevent two link calls in parallel
71 * introducing a cycle
73 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
78 * one)
80 void keyring_publish_name(struct key *keyring)
82 int bucket;
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)
108 int ret;
110 ret = -EINVAL;
111 if (datalen == 0) {
112 /* make the keyring available by name if it has one */
113 keyring_publish_name(keyring);
114 ret = 0;
117 return ret;
119 } /* end keyring_instantiate() */
121 /*****************************************************************************/
123 * duplicate the list of subscribed keys from a source keyring into this one
125 static int keyring_duplicate(struct key *keyring, const struct key *source)
127 struct keyring_list *sklist, *klist;
128 unsigned max;
129 size_t size;
130 int loop, ret;
132 const unsigned limit =
133 (PAGE_SIZE - sizeof(*klist)) / sizeof(struct key *);
135 ret = 0;
137 /* find out how many keys are currently linked */
138 rcu_read_lock();
139 sklist = rcu_dereference(source->payload.subscriptions);
140 max = 0;
141 if (sklist)
142 max = sklist->nkeys;
143 rcu_read_unlock();
145 /* allocate a new payload and stuff load with key links */
146 if (max > 0) {
147 BUG_ON(max > limit);
149 max = (max + 3) & ~3;
150 if (max > limit)
151 max = limit;
153 ret = -ENOMEM;
154 size = sizeof(*klist) + sizeof(struct key *) * max;
155 klist = kmalloc(size, GFP_KERNEL);
156 if (!klist)
157 goto error;
159 /* set links */
160 rcu_read_lock();
161 sklist = rcu_dereference(source->payload.subscriptions);
163 klist->maxkeys = max;
164 klist->nkeys = sklist->nkeys;
165 memcpy(klist->keys,
166 sklist->keys,
167 sklist->nkeys * sizeof(struct key *));
169 for (loop = klist->nkeys - 1; loop >= 0; loop--)
170 atomic_inc(&klist->keys[loop]->usage);
172 rcu_read_unlock();
174 rcu_assign_pointer(keyring->payload.subscriptions, klist);
175 ret = 0;
178 error:
179 return ret;
181 } /* end keyring_duplicate() */
183 /*****************************************************************************/
185 * match keyrings on their name
187 static int keyring_match(const struct key *keyring, const void *description)
189 return keyring->description &&
190 strcmp(keyring->description, description) == 0;
192 } /* end keyring_match() */
194 /*****************************************************************************/
196 * dispose of the data dangling from the corpse of a keyring
198 static void keyring_destroy(struct key *keyring)
200 struct keyring_list *klist;
201 int loop;
203 if (keyring->description) {
204 write_lock(&keyring_name_lock);
206 if (keyring->type_data.link.next != NULL &&
207 !list_empty(&keyring->type_data.link))
208 list_del(&keyring->type_data.link);
210 write_unlock(&keyring_name_lock);
213 klist = rcu_dereference(keyring->payload.subscriptions);
214 if (klist) {
215 for (loop = klist->nkeys - 1; loop >= 0; loop--)
216 key_put(klist->keys[loop]);
217 kfree(klist);
220 } /* end keyring_destroy() */
222 /*****************************************************************************/
224 * describe the keyring
226 static void keyring_describe(const struct key *keyring, struct seq_file *m)
228 struct keyring_list *klist;
230 if (keyring->description) {
231 seq_puts(m, keyring->description);
233 else {
234 seq_puts(m, "[anon]");
237 rcu_read_lock();
238 klist = rcu_dereference(keyring->payload.subscriptions);
239 if (klist)
240 seq_printf(m, ": %u/%u", klist->nkeys, klist->maxkeys);
241 else
242 seq_puts(m, ": empty");
243 rcu_read_unlock();
245 } /* end keyring_describe() */
247 /*****************************************************************************/
249 * read a list of key IDs from the keyring's contents
250 * - the keyring's semaphore is read-locked
252 static long keyring_read(const struct key *keyring,
253 char __user *buffer, size_t buflen)
255 struct keyring_list *klist;
256 struct key *key;
257 size_t qty, tmp;
258 int loop, ret;
260 ret = 0;
261 klist = rcu_dereference(keyring->payload.subscriptions);
263 if (klist) {
264 /* calculate how much data we could return */
265 qty = klist->nkeys * sizeof(key_serial_t);
267 if (buffer && buflen > 0) {
268 if (buflen > qty)
269 buflen = qty;
271 /* copy the IDs of the subscribed keys into the
272 * buffer */
273 ret = -EFAULT;
275 for (loop = 0; loop < klist->nkeys; loop++) {
276 key = klist->keys[loop];
278 tmp = sizeof(key_serial_t);
279 if (tmp > buflen)
280 tmp = buflen;
282 if (copy_to_user(buffer,
283 &key->serial,
284 tmp) != 0)
285 goto error;
287 buflen -= tmp;
288 if (buflen == 0)
289 break;
290 buffer += tmp;
294 ret = qty;
297 error:
298 return ret;
300 } /* end keyring_read() */
302 /*****************************************************************************/
304 * allocate a keyring and link into the destination keyring
306 struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
307 int not_in_quota, struct key *dest)
309 struct key *keyring;
310 int ret;
312 keyring = key_alloc(&key_type_keyring, description,
313 uid, gid,
314 (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
315 not_in_quota);
317 if (!IS_ERR(keyring)) {
318 ret = key_instantiate_and_link(keyring, NULL, 0, dest, NULL);
319 if (ret < 0) {
320 key_put(keyring);
321 keyring = ERR_PTR(ret);
325 return keyring;
327 } /* end keyring_alloc() */
329 /*****************************************************************************/
331 * search the supplied keyring tree for a key that matches the criterion
332 * - perform a breadth-then-depth search up to the prescribed limit
333 * - we only find keys on which we have search permission
334 * - we use the supplied match function to see if the description (or other
335 * feature of interest) matches
336 * - we rely on RCU to prevent the keyring lists from disappearing on us
337 * - we return -EAGAIN if we didn't find any matching key
338 * - we return -ENOKEY if we only found negative matching keys
339 * - we propagate the possession attribute from the keyring ref to the key ref
341 key_ref_t keyring_search_aux(key_ref_t keyring_ref,
342 struct task_struct *context,
343 struct key_type *type,
344 const void *description,
345 key_match_func_t match)
347 struct {
348 struct keyring_list *keylist;
349 int kix;
350 } stack[KEYRING_SEARCH_MAX_DEPTH];
352 struct keyring_list *keylist;
353 struct timespec now;
354 unsigned long possessed;
355 struct key *keyring, *key;
356 key_ref_t key_ref;
357 long err;
358 int sp, kix;
360 keyring = key_ref_to_ptr(keyring_ref);
361 possessed = is_key_possessed(keyring_ref);
362 key_check(keyring);
364 /* top keyring must have search permission to begin the search */
365 err = key_task_permission(keyring_ref, context, KEY_SEARCH);
366 if (err < 0) {
367 key_ref = ERR_PTR(err);
368 goto error;
371 key_ref = ERR_PTR(-ENOTDIR);
372 if (keyring->type != &key_type_keyring)
373 goto error;
375 rcu_read_lock();
377 now = current_kernel_time();
378 err = -EAGAIN;
379 sp = 0;
381 /* start processing a new keyring */
382 descend:
383 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
384 goto not_this_keyring;
386 keylist = rcu_dereference(keyring->payload.subscriptions);
387 if (!keylist)
388 goto not_this_keyring;
390 /* iterate through the keys in this keyring first */
391 for (kix = 0; kix < keylist->nkeys; kix++) {
392 key = keylist->keys[kix];
394 /* ignore keys not of this type */
395 if (key->type != type)
396 continue;
398 /* skip revoked keys and expired keys */
399 if (test_bit(KEY_FLAG_REVOKED, &key->flags))
400 continue;
402 if (key->expiry && now.tv_sec >= key->expiry)
403 continue;
405 /* keys that don't match */
406 if (!match(key, description))
407 continue;
409 /* key must have search permissions */
410 if (key_task_permission(make_key_ref(key, possessed),
411 context, KEY_SEARCH) < 0)
412 continue;
414 /* we set a different error code if we find a negative key */
415 if (test_bit(KEY_FLAG_NEGATIVE, &key->flags)) {
416 err = -ENOKEY;
417 continue;
420 goto found;
423 /* search through the keyrings nested in this one */
424 kix = 0;
425 ascend:
426 for (; kix < keylist->nkeys; kix++) {
427 key = keylist->keys[kix];
428 if (key->type != &key_type_keyring)
429 continue;
431 /* recursively search nested keyrings
432 * - only search keyrings for which we have search permission
434 if (sp >= KEYRING_SEARCH_MAX_DEPTH)
435 continue;
437 if (key_task_permission(make_key_ref(key, possessed),
438 context, KEY_SEARCH) < 0)
439 continue;
441 /* stack the current position */
442 stack[sp].keylist = keylist;
443 stack[sp].kix = kix;
444 sp++;
446 /* begin again with the new keyring */
447 keyring = key;
448 goto descend;
451 /* the keyring we're looking at was disqualified or didn't contain a
452 * matching key */
453 not_this_keyring:
454 if (sp > 0) {
455 /* resume the processing of a keyring higher up in the tree */
456 sp--;
457 keylist = stack[sp].keylist;
458 kix = stack[sp].kix + 1;
459 goto ascend;
462 key_ref = ERR_PTR(err);
463 goto error_2;
465 /* we found a viable match */
466 found:
467 atomic_inc(&key->usage);
468 key_check(key);
469 key_ref = make_key_ref(key, possessed);
470 error_2:
471 rcu_read_unlock();
472 error:
473 return key_ref;
475 } /* end keyring_search_aux() */
477 /*****************************************************************************/
479 * search the supplied keyring tree for a key that matches the criterion
480 * - perform a breadth-then-depth search up to the prescribed limit
481 * - we only find keys on which we have search permission
482 * - we readlock the keyrings as we search down the tree
483 * - we return -EAGAIN if we didn't find any matching key
484 * - we return -ENOKEY if we only found negative matching keys
486 key_ref_t keyring_search(key_ref_t keyring,
487 struct key_type *type,
488 const char *description)
490 if (!type->match)
491 return ERR_PTR(-ENOKEY);
493 return keyring_search_aux(keyring, current,
494 type, description, type->match);
496 } /* end keyring_search() */
498 EXPORT_SYMBOL(keyring_search);
500 /*****************************************************************************/
502 * search the given keyring only (no recursion)
503 * - keyring must be locked by caller
505 key_ref_t __keyring_search_one(key_ref_t keyring_ref,
506 const struct key_type *ktype,
507 const char *description,
508 key_perm_t perm)
510 struct keyring_list *klist;
511 unsigned long possessed;
512 struct key *keyring, *key;
513 int loop;
515 keyring = key_ref_to_ptr(keyring_ref);
516 possessed = is_key_possessed(keyring_ref);
518 rcu_read_lock();
520 klist = rcu_dereference(keyring->payload.subscriptions);
521 if (klist) {
522 for (loop = 0; loop < klist->nkeys; loop++) {
523 key = klist->keys[loop];
525 if (key->type == ktype &&
526 (!key->type->match ||
527 key->type->match(key, description)) &&
528 key_permission(make_key_ref(key, possessed),
529 perm) < 0 &&
530 !test_bit(KEY_FLAG_REVOKED, &key->flags)
532 goto found;
536 rcu_read_unlock();
537 return ERR_PTR(-ENOKEY);
539 found:
540 atomic_inc(&key->usage);
541 rcu_read_unlock();
542 return make_key_ref(key, possessed);
544 } /* end __keyring_search_one() */
546 /*****************************************************************************/
548 * search for an instantiation authorisation key matching a target key
549 * - the RCU read lock must be held by the caller
550 * - a target_id of zero specifies any valid token
552 struct key *keyring_search_instkey(struct key *keyring,
553 key_serial_t target_id)
555 struct request_key_auth *rka;
556 struct keyring_list *klist;
557 struct key *instkey;
558 int loop;
560 klist = rcu_dereference(keyring->payload.subscriptions);
561 if (klist) {
562 for (loop = 0; loop < klist->nkeys; loop++) {
563 instkey = klist->keys[loop];
565 if (instkey->type != &key_type_request_key_auth)
566 continue;
568 rka = instkey->payload.data;
569 if (target_id && rka->target_key->serial != target_id)
570 continue;
572 /* the auth key is revoked during instantiation */
573 if (!test_bit(KEY_FLAG_REVOKED, &instkey->flags))
574 goto found;
576 instkey = ERR_PTR(-EKEYREVOKED);
577 goto error;
581 instkey = ERR_PTR(-EACCES);
582 goto error;
584 found:
585 atomic_inc(&instkey->usage);
586 error:
587 return instkey;
589 } /* end keyring_search_instkey() */
591 /*****************************************************************************/
593 * find a keyring with the specified name
594 * - all named keyrings are searched
595 * - only find keyrings with search permission for the process
596 * - only find keyrings with a serial number greater than the one specified
598 struct key *find_keyring_by_name(const char *name, key_serial_t bound)
600 struct key *keyring;
601 int bucket;
603 keyring = ERR_PTR(-EINVAL);
604 if (!name)
605 goto error;
607 bucket = keyring_hash(name);
609 read_lock(&keyring_name_lock);
611 if (keyring_name_hash[bucket].next) {
612 /* search this hash bucket for a keyring with a matching name
613 * that's readable and that hasn't been revoked */
614 list_for_each_entry(keyring,
615 &keyring_name_hash[bucket],
616 type_data.link
618 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
619 continue;
621 if (strcmp(keyring->description, name) != 0)
622 continue;
624 if (key_permission(make_key_ref(keyring, 0),
625 KEY_SEARCH) < 0)
626 continue;
628 /* found a potential candidate, but we still need to
629 * check the serial number */
630 if (keyring->serial <= bound)
631 continue;
633 /* we've got a match */
634 atomic_inc(&keyring->usage);
635 read_unlock(&keyring_name_lock);
636 goto error;
640 read_unlock(&keyring_name_lock);
641 keyring = ERR_PTR(-ENOKEY);
643 error:
644 return keyring;
646 } /* end find_keyring_by_name() */
648 /*****************************************************************************/
650 * see if a cycle will will be created by inserting acyclic tree B in acyclic
651 * tree A at the topmost level (ie: as a direct child of A)
652 * - since we are adding B to A at the top level, checking for cycles should
653 * just be a matter of seeing if node A is somewhere in tree B
655 static int keyring_detect_cycle(struct key *A, struct key *B)
657 struct {
658 struct keyring_list *keylist;
659 int kix;
660 } stack[KEYRING_SEARCH_MAX_DEPTH];
662 struct keyring_list *keylist;
663 struct key *subtree, *key;
664 int sp, kix, ret;
666 rcu_read_lock();
668 ret = -EDEADLK;
669 if (A == B)
670 goto cycle_detected;
672 subtree = B;
673 sp = 0;
675 /* start processing a new keyring */
676 descend:
677 if (test_bit(KEY_FLAG_REVOKED, &subtree->flags))
678 goto not_this_keyring;
680 keylist = rcu_dereference(subtree->payload.subscriptions);
681 if (!keylist)
682 goto not_this_keyring;
683 kix = 0;
685 ascend:
686 /* iterate through the remaining keys in this keyring */
687 for (; kix < keylist->nkeys; kix++) {
688 key = keylist->keys[kix];
690 if (key == A)
691 goto cycle_detected;
693 /* recursively check nested keyrings */
694 if (key->type == &key_type_keyring) {
695 if (sp >= KEYRING_SEARCH_MAX_DEPTH)
696 goto too_deep;
698 /* stack the current position */
699 stack[sp].keylist = keylist;
700 stack[sp].kix = kix;
701 sp++;
703 /* begin again with the new keyring */
704 subtree = key;
705 goto descend;
709 /* the keyring we're looking at was disqualified or didn't contain a
710 * matching key */
711 not_this_keyring:
712 if (sp > 0) {
713 /* resume the checking of a keyring higher up in the tree */
714 sp--;
715 keylist = stack[sp].keylist;
716 kix = stack[sp].kix + 1;
717 goto ascend;
720 ret = 0; /* no cycles detected */
722 error:
723 rcu_read_unlock();
724 return ret;
726 too_deep:
727 ret = -ELOOP;
728 goto error;
730 cycle_detected:
731 ret = -EDEADLK;
732 goto error;
734 } /* end keyring_detect_cycle() */
736 /*****************************************************************************/
738 * dispose of a keyring list after the RCU grace period
740 static void keyring_link_rcu_disposal(struct rcu_head *rcu)
742 struct keyring_list *klist =
743 container_of(rcu, struct keyring_list, rcu);
745 kfree(klist);
747 } /* end keyring_link_rcu_disposal() */
749 /*****************************************************************************/
751 * link a key into to a keyring
752 * - must be called with the keyring's semaphore write-locked
754 int __key_link(struct key *keyring, struct key *key)
756 struct keyring_list *klist, *nklist;
757 unsigned max;
758 size_t size;
759 int ret;
761 ret = -EKEYREVOKED;
762 if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
763 goto error;
765 ret = -ENOTDIR;
766 if (keyring->type != &key_type_keyring)
767 goto error;
769 /* serialise link/link calls to prevent parallel calls causing a
770 * cycle when applied to two keyring in opposite orders */
771 down_write(&keyring_serialise_link_sem);
773 /* check that we aren't going to create a cycle adding one keyring to
774 * another */
775 if (key->type == &key_type_keyring) {
776 ret = keyring_detect_cycle(keyring, key);
777 if (ret < 0)
778 goto error2;
781 /* check that we aren't going to overrun the user's quota */
782 ret = key_payload_reserve(keyring,
783 keyring->datalen + KEYQUOTA_LINK_BYTES);
784 if (ret < 0)
785 goto error2;
787 klist = keyring->payload.subscriptions;
789 if (klist && klist->nkeys < klist->maxkeys) {
790 /* there's sufficient slack space to add directly */
791 atomic_inc(&key->usage);
793 klist->keys[klist->nkeys] = key;
794 smp_wmb();
795 klist->nkeys++;
796 smp_wmb();
798 ret = 0;
800 else {
801 /* grow the key list */
802 max = 4;
803 if (klist)
804 max += klist->maxkeys;
806 ret = -ENFILE;
807 if (max > 65535)
808 goto error3;
809 size = sizeof(*klist) + sizeof(struct key *) * max;
810 if (size > PAGE_SIZE)
811 goto error3;
813 ret = -ENOMEM;
814 nklist = kmalloc(size, GFP_KERNEL);
815 if (!nklist)
816 goto error3;
817 nklist->maxkeys = max;
818 nklist->nkeys = 0;
820 if (klist) {
821 nklist->nkeys = klist->nkeys;
822 memcpy(nklist->keys,
823 klist->keys,
824 sizeof(struct key *) * klist->nkeys);
827 /* add the key into the new space */
828 atomic_inc(&key->usage);
829 nklist->keys[nklist->nkeys++] = key;
831 rcu_assign_pointer(keyring->payload.subscriptions, nklist);
833 /* dispose of the old keyring list */
834 if (klist)
835 call_rcu(&klist->rcu, keyring_link_rcu_disposal);
837 ret = 0;
840 error2:
841 up_write(&keyring_serialise_link_sem);
842 error:
843 return ret;
845 error3:
846 /* undo the quota changes */
847 key_payload_reserve(keyring,
848 keyring->datalen - KEYQUOTA_LINK_BYTES);
849 goto error2;
851 } /* end __key_link() */
853 /*****************************************************************************/
855 * link a key to a keyring
857 int key_link(struct key *keyring, struct key *key)
859 int ret;
861 key_check(keyring);
862 key_check(key);
864 down_write(&keyring->sem);
865 ret = __key_link(keyring, key);
866 up_write(&keyring->sem);
868 return ret;
870 } /* end key_link() */
872 EXPORT_SYMBOL(key_link);
874 /*****************************************************************************/
876 * dispose of a keyring list after the RCU grace period, freeing the unlinked
877 * key
879 static void keyring_unlink_rcu_disposal(struct rcu_head *rcu)
881 struct keyring_list *klist =
882 container_of(rcu, struct keyring_list, rcu);
884 key_put(klist->keys[klist->delkey]);
885 kfree(klist);
887 } /* end keyring_unlink_rcu_disposal() */
889 /*****************************************************************************/
891 * unlink the first link to a key from a keyring
893 int key_unlink(struct key *keyring, struct key *key)
895 struct keyring_list *klist, *nklist;
896 int loop, ret;
898 key_check(keyring);
899 key_check(key);
901 ret = -ENOTDIR;
902 if (keyring->type != &key_type_keyring)
903 goto error;
905 down_write(&keyring->sem);
907 klist = keyring->payload.subscriptions;
908 if (klist) {
909 /* search the keyring for the key */
910 for (loop = 0; loop < klist->nkeys; loop++)
911 if (klist->keys[loop] == key)
912 goto key_is_present;
915 up_write(&keyring->sem);
916 ret = -ENOENT;
917 goto error;
919 key_is_present:
920 /* we need to copy the key list for RCU purposes */
921 nklist = kmalloc(sizeof(*klist) +
922 sizeof(struct key *) * klist->maxkeys,
923 GFP_KERNEL);
924 if (!nklist)
925 goto nomem;
926 nklist->maxkeys = klist->maxkeys;
927 nklist->nkeys = klist->nkeys - 1;
929 if (loop > 0)
930 memcpy(&nklist->keys[0],
931 &klist->keys[0],
932 loop * sizeof(struct key *));
934 if (loop < nklist->nkeys)
935 memcpy(&nklist->keys[loop],
936 &klist->keys[loop + 1],
937 (nklist->nkeys - loop) * sizeof(struct key *));
939 /* adjust the user's quota */
940 key_payload_reserve(keyring,
941 keyring->datalen - KEYQUOTA_LINK_BYTES);
943 rcu_assign_pointer(keyring->payload.subscriptions, nklist);
945 up_write(&keyring->sem);
947 /* schedule for later cleanup */
948 klist->delkey = loop;
949 call_rcu(&klist->rcu, keyring_unlink_rcu_disposal);
951 ret = 0;
953 error:
954 return ret;
955 nomem:
956 ret = -ENOMEM;
957 up_write(&keyring->sem);
958 goto error;
960 } /* end key_unlink() */
962 EXPORT_SYMBOL(key_unlink);
964 /*****************************************************************************/
966 * dispose of a keyring list after the RCU grace period, releasing the keys it
967 * links to
969 static void keyring_clear_rcu_disposal(struct rcu_head *rcu)
971 struct keyring_list *klist;
972 int loop;
974 klist = container_of(rcu, struct keyring_list, rcu);
976 for (loop = klist->nkeys - 1; loop >= 0; loop--)
977 key_put(klist->keys[loop]);
979 kfree(klist);
981 } /* end keyring_clear_rcu_disposal() */
983 /*****************************************************************************/
985 * clear the specified process keyring
986 * - implements keyctl(KEYCTL_CLEAR)
988 int keyring_clear(struct key *keyring)
990 struct keyring_list *klist;
991 int ret;
993 ret = -ENOTDIR;
994 if (keyring->type == &key_type_keyring) {
995 /* detach the pointer block with the locks held */
996 down_write(&keyring->sem);
998 klist = keyring->payload.subscriptions;
999 if (klist) {
1000 /* adjust the quota */
1001 key_payload_reserve(keyring,
1002 sizeof(struct keyring_list));
1004 rcu_assign_pointer(keyring->payload.subscriptions,
1005 NULL);
1008 up_write(&keyring->sem);
1010 /* free the keys after the locks have been dropped */
1011 if (klist)
1012 call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
1014 ret = 0;
1017 return ret;
1019 } /* end keyring_clear() */
1021 EXPORT_SYMBOL(keyring_clear);