[PATCH] keys: discard the contents of a key on revocation
[wrt350n-kernel.git] / security / keys / key.c
blob3601fddca9f22155a7f30654ee77b321b4866eac
1 /* key.c: basic authentication token and access key management
3 * Copyright (C) 2004-6 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/workqueue.h>
18 #include <linux/err.h>
19 #include "internal.h"
21 static kmem_cache_t *key_jar;
22 static key_serial_t key_serial_next = 3;
23 struct rb_root key_serial_tree; /* tree of keys indexed by serial */
24 DEFINE_SPINLOCK(key_serial_lock);
26 struct rb_root key_user_tree; /* tree of quota records indexed by UID */
27 DEFINE_SPINLOCK(key_user_lock);
29 static LIST_HEAD(key_types_list);
30 static DECLARE_RWSEM(key_types_sem);
32 static void key_cleanup(void *data);
33 static DECLARE_WORK(key_cleanup_task, key_cleanup, NULL);
35 /* we serialise key instantiation and link */
36 DECLARE_RWSEM(key_construction_sem);
38 /* any key who's type gets unegistered will be re-typed to this */
39 static struct key_type key_type_dead = {
40 .name = "dead",
43 #ifdef KEY_DEBUGGING
44 void __key_check(const struct key *key)
46 printk("__key_check: key %p {%08x} should be {%08x}\n",
47 key, key->magic, KEY_DEBUG_MAGIC);
48 BUG();
50 #endif
52 /*****************************************************************************/
54 * get the key quota record for a user, allocating a new record if one doesn't
55 * already exist
57 struct key_user *key_user_lookup(uid_t uid)
59 struct key_user *candidate = NULL, *user;
60 struct rb_node *parent = NULL;
61 struct rb_node **p;
63 try_again:
64 p = &key_user_tree.rb_node;
65 spin_lock(&key_user_lock);
67 /* search the tree for a user record with a matching UID */
68 while (*p) {
69 parent = *p;
70 user = rb_entry(parent, struct key_user, node);
72 if (uid < user->uid)
73 p = &(*p)->rb_left;
74 else if (uid > user->uid)
75 p = &(*p)->rb_right;
76 else
77 goto found;
80 /* if we get here, we failed to find a match in the tree */
81 if (!candidate) {
82 /* allocate a candidate user record if we don't already have
83 * one */
84 spin_unlock(&key_user_lock);
86 user = NULL;
87 candidate = kmalloc(sizeof(struct key_user), GFP_KERNEL);
88 if (unlikely(!candidate))
89 goto out;
91 /* the allocation may have scheduled, so we need to repeat the
92 * search lest someone else added the record whilst we were
93 * asleep */
94 goto try_again;
97 /* if we get here, then the user record still hadn't appeared on the
98 * second pass - so we use the candidate record */
99 atomic_set(&candidate->usage, 1);
100 atomic_set(&candidate->nkeys, 0);
101 atomic_set(&candidate->nikeys, 0);
102 candidate->uid = uid;
103 candidate->qnkeys = 0;
104 candidate->qnbytes = 0;
105 spin_lock_init(&candidate->lock);
106 INIT_LIST_HEAD(&candidate->consq);
108 rb_link_node(&candidate->node, parent, p);
109 rb_insert_color(&candidate->node, &key_user_tree);
110 spin_unlock(&key_user_lock);
111 user = candidate;
112 goto out;
114 /* okay - we found a user record for this UID */
115 found:
116 atomic_inc(&user->usage);
117 spin_unlock(&key_user_lock);
118 kfree(candidate);
119 out:
120 return user;
122 } /* end key_user_lookup() */
124 /*****************************************************************************/
126 * dispose of a user structure
128 void key_user_put(struct key_user *user)
130 if (atomic_dec_and_lock(&user->usage, &key_user_lock)) {
131 rb_erase(&user->node, &key_user_tree);
132 spin_unlock(&key_user_lock);
134 kfree(user);
137 } /* end key_user_put() */
139 /*****************************************************************************/
141 * insert a key with a fixed serial number
143 static void __init __key_insert_serial(struct key *key)
145 struct rb_node *parent, **p;
146 struct key *xkey;
148 parent = NULL;
149 p = &key_serial_tree.rb_node;
151 while (*p) {
152 parent = *p;
153 xkey = rb_entry(parent, struct key, serial_node);
155 if (key->serial < xkey->serial)
156 p = &(*p)->rb_left;
157 else if (key->serial > xkey->serial)
158 p = &(*p)->rb_right;
159 else
160 BUG();
163 /* we've found a suitable hole - arrange for this key to occupy it */
164 rb_link_node(&key->serial_node, parent, p);
165 rb_insert_color(&key->serial_node, &key_serial_tree);
167 } /* end __key_insert_serial() */
169 /*****************************************************************************/
171 * assign a key the next unique serial number
172 * - we work through all the serial numbers between 2 and 2^31-1 in turn and
173 * then wrap
175 static inline void key_alloc_serial(struct key *key)
177 struct rb_node *parent, **p;
178 struct key *xkey;
180 spin_lock(&key_serial_lock);
182 /* propose a likely serial number and look for a hole for it in the
183 * serial number tree */
184 key->serial = key_serial_next;
185 if (key->serial < 3)
186 key->serial = 3;
187 key_serial_next = key->serial + 1;
189 parent = NULL;
190 p = &key_serial_tree.rb_node;
192 while (*p) {
193 parent = *p;
194 xkey = rb_entry(parent, struct key, serial_node);
196 if (key->serial < xkey->serial)
197 p = &(*p)->rb_left;
198 else if (key->serial > xkey->serial)
199 p = &(*p)->rb_right;
200 else
201 goto serial_exists;
203 goto insert_here;
205 /* we found a key with the proposed serial number - walk the tree from
206 * that point looking for the next unused serial number */
207 serial_exists:
208 for (;;) {
209 key->serial = key_serial_next;
210 if (key->serial < 2)
211 key->serial = 2;
212 key_serial_next = key->serial + 1;
214 if (!rb_parent(parent))
215 p = &key_serial_tree.rb_node;
216 else if (rb_parent(parent)->rb_left == parent)
217 p = &(rb_parent(parent)->rb_left);
218 else
219 p = &(rb_parent(parent)->rb_right);
221 parent = rb_next(parent);
222 if (!parent)
223 break;
225 xkey = rb_entry(parent, struct key, serial_node);
226 if (key->serial < xkey->serial)
227 goto insert_here;
230 /* we've found a suitable hole - arrange for this key to occupy it */
231 insert_here:
232 rb_link_node(&key->serial_node, parent, p);
233 rb_insert_color(&key->serial_node, &key_serial_tree);
235 spin_unlock(&key_serial_lock);
237 } /* end key_alloc_serial() */
239 /*****************************************************************************/
241 * allocate a key of the specified type
242 * - update the user's quota to reflect the existence of the key
243 * - called from a key-type operation with key_types_sem read-locked by
244 * key_create_or_update()
245 * - this prevents unregistration of the key type
246 * - upon return the key is as yet uninstantiated; the caller needs to either
247 * instantiate the key or discard it before returning
249 struct key *key_alloc(struct key_type *type, const char *desc,
250 uid_t uid, gid_t gid, struct task_struct *ctx,
251 key_perm_t perm, unsigned long flags)
253 struct key_user *user = NULL;
254 struct key *key;
255 size_t desclen, quotalen;
256 int ret;
258 key = ERR_PTR(-EINVAL);
259 if (!desc || !*desc)
260 goto error;
262 desclen = strlen(desc) + 1;
263 quotalen = desclen + type->def_datalen;
265 /* get hold of the key tracking for this user */
266 user = key_user_lookup(uid);
267 if (!user)
268 goto no_memory_1;
270 /* check that the user's quota permits allocation of another key and
271 * its description */
272 if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) {
273 spin_lock(&user->lock);
274 if (!(flags & KEY_ALLOC_QUOTA_OVERRUN)) {
275 if (user->qnkeys + 1 >= KEYQUOTA_MAX_KEYS ||
276 user->qnbytes + quotalen >= KEYQUOTA_MAX_BYTES
278 goto no_quota;
281 user->qnkeys++;
282 user->qnbytes += quotalen;
283 spin_unlock(&user->lock);
286 /* allocate and initialise the key and its description */
287 key = kmem_cache_alloc(key_jar, SLAB_KERNEL);
288 if (!key)
289 goto no_memory_2;
291 if (desc) {
292 key->description = kmalloc(desclen, GFP_KERNEL);
293 if (!key->description)
294 goto no_memory_3;
296 memcpy(key->description, desc, desclen);
299 atomic_set(&key->usage, 1);
300 init_rwsem(&key->sem);
301 key->type = type;
302 key->user = user;
303 key->quotalen = quotalen;
304 key->datalen = type->def_datalen;
305 key->uid = uid;
306 key->gid = gid;
307 key->perm = perm;
308 key->flags = 0;
309 key->expiry = 0;
310 key->payload.data = NULL;
311 key->security = NULL;
313 if (!(flags & KEY_ALLOC_NOT_IN_QUOTA))
314 key->flags |= 1 << KEY_FLAG_IN_QUOTA;
316 memset(&key->type_data, 0, sizeof(key->type_data));
318 #ifdef KEY_DEBUGGING
319 key->magic = KEY_DEBUG_MAGIC;
320 #endif
322 /* let the security module know about the key */
323 ret = security_key_alloc(key, ctx, flags);
324 if (ret < 0)
325 goto security_error;
327 /* publish the key by giving it a serial number */
328 atomic_inc(&user->nkeys);
329 key_alloc_serial(key);
331 error:
332 return key;
334 security_error:
335 kfree(key->description);
336 kmem_cache_free(key_jar, key);
337 if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) {
338 spin_lock(&user->lock);
339 user->qnkeys--;
340 user->qnbytes -= quotalen;
341 spin_unlock(&user->lock);
343 key_user_put(user);
344 key = ERR_PTR(ret);
345 goto error;
347 no_memory_3:
348 kmem_cache_free(key_jar, key);
349 no_memory_2:
350 if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) {
351 spin_lock(&user->lock);
352 user->qnkeys--;
353 user->qnbytes -= quotalen;
354 spin_unlock(&user->lock);
356 key_user_put(user);
357 no_memory_1:
358 key = ERR_PTR(-ENOMEM);
359 goto error;
361 no_quota:
362 spin_unlock(&user->lock);
363 key_user_put(user);
364 key = ERR_PTR(-EDQUOT);
365 goto error;
367 } /* end key_alloc() */
369 EXPORT_SYMBOL(key_alloc);
371 /*****************************************************************************/
373 * reserve an amount of quota for the key's payload
375 int key_payload_reserve(struct key *key, size_t datalen)
377 int delta = (int) datalen - key->datalen;
378 int ret = 0;
380 key_check(key);
382 /* contemplate the quota adjustment */
383 if (delta != 0 && test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
384 spin_lock(&key->user->lock);
386 if (delta > 0 &&
387 key->user->qnbytes + delta > KEYQUOTA_MAX_BYTES
389 ret = -EDQUOT;
391 else {
392 key->user->qnbytes += delta;
393 key->quotalen += delta;
395 spin_unlock(&key->user->lock);
398 /* change the recorded data length if that didn't generate an error */
399 if (ret == 0)
400 key->datalen = datalen;
402 return ret;
404 } /* end key_payload_reserve() */
406 EXPORT_SYMBOL(key_payload_reserve);
408 /*****************************************************************************/
410 * instantiate a key and link it into the target keyring atomically
411 * - called with the target keyring's semaphore writelocked
413 static int __key_instantiate_and_link(struct key *key,
414 const void *data,
415 size_t datalen,
416 struct key *keyring,
417 struct key *instkey)
419 int ret, awaken;
421 key_check(key);
422 key_check(keyring);
424 awaken = 0;
425 ret = -EBUSY;
427 down_write(&key_construction_sem);
429 /* can't instantiate twice */
430 if (!test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
431 /* instantiate the key */
432 ret = key->type->instantiate(key, data, datalen);
434 if (ret == 0) {
435 /* mark the key as being instantiated */
436 atomic_inc(&key->user->nikeys);
437 set_bit(KEY_FLAG_INSTANTIATED, &key->flags);
439 if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags))
440 awaken = 1;
442 /* and link it into the destination keyring */
443 if (keyring)
444 ret = __key_link(keyring, key);
446 /* disable the authorisation key */
447 if (instkey)
448 key_revoke(instkey);
452 up_write(&key_construction_sem);
454 /* wake up anyone waiting for a key to be constructed */
455 if (awaken)
456 wake_up_all(&request_key_conswq);
458 return ret;
460 } /* end __key_instantiate_and_link() */
462 /*****************************************************************************/
464 * instantiate a key and link it into the target keyring atomically
466 int key_instantiate_and_link(struct key *key,
467 const void *data,
468 size_t datalen,
469 struct key *keyring,
470 struct key *instkey)
472 int ret;
474 if (keyring)
475 down_write(&keyring->sem);
477 ret = __key_instantiate_and_link(key, data, datalen, keyring, instkey);
479 if (keyring)
480 up_write(&keyring->sem);
482 return ret;
484 } /* end key_instantiate_and_link() */
486 EXPORT_SYMBOL(key_instantiate_and_link);
488 /*****************************************************************************/
490 * negatively instantiate a key and link it into the target keyring atomically
492 int key_negate_and_link(struct key *key,
493 unsigned timeout,
494 struct key *keyring,
495 struct key *instkey)
497 struct timespec now;
498 int ret, awaken;
500 key_check(key);
501 key_check(keyring);
503 awaken = 0;
504 ret = -EBUSY;
506 if (keyring)
507 down_write(&keyring->sem);
509 down_write(&key_construction_sem);
511 /* can't instantiate twice */
512 if (!test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
513 /* mark the key as being negatively instantiated */
514 atomic_inc(&key->user->nikeys);
515 set_bit(KEY_FLAG_NEGATIVE, &key->flags);
516 set_bit(KEY_FLAG_INSTANTIATED, &key->flags);
517 now = current_kernel_time();
518 key->expiry = now.tv_sec + timeout;
520 if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags))
521 awaken = 1;
523 ret = 0;
525 /* and link it into the destination keyring */
526 if (keyring)
527 ret = __key_link(keyring, key);
529 /* disable the authorisation key */
530 if (instkey)
531 key_revoke(instkey);
534 up_write(&key_construction_sem);
536 if (keyring)
537 up_write(&keyring->sem);
539 /* wake up anyone waiting for a key to be constructed */
540 if (awaken)
541 wake_up_all(&request_key_conswq);
543 return ret;
545 } /* end key_negate_and_link() */
547 EXPORT_SYMBOL(key_negate_and_link);
549 /*****************************************************************************/
551 * do cleaning up in process context so that we don't have to disable
552 * interrupts all over the place
554 static void key_cleanup(void *data)
556 struct rb_node *_n;
557 struct key *key;
559 go_again:
560 /* look for a dead key in the tree */
561 spin_lock(&key_serial_lock);
563 for (_n = rb_first(&key_serial_tree); _n; _n = rb_next(_n)) {
564 key = rb_entry(_n, struct key, serial_node);
566 if (atomic_read(&key->usage) == 0)
567 goto found_dead_key;
570 spin_unlock(&key_serial_lock);
571 return;
573 found_dead_key:
574 /* we found a dead key - once we've removed it from the tree, we can
575 * drop the lock */
576 rb_erase(&key->serial_node, &key_serial_tree);
577 spin_unlock(&key_serial_lock);
579 key_check(key);
581 security_key_free(key);
583 /* deal with the user's key tracking and quota */
584 if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
585 spin_lock(&key->user->lock);
586 key->user->qnkeys--;
587 key->user->qnbytes -= key->quotalen;
588 spin_unlock(&key->user->lock);
591 atomic_dec(&key->user->nkeys);
592 if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
593 atomic_dec(&key->user->nikeys);
595 key_user_put(key->user);
597 /* now throw away the key memory */
598 if (key->type->destroy)
599 key->type->destroy(key);
601 kfree(key->description);
603 #ifdef KEY_DEBUGGING
604 key->magic = KEY_DEBUG_MAGIC_X;
605 #endif
606 kmem_cache_free(key_jar, key);
608 /* there may, of course, be more than one key to destroy */
609 goto go_again;
611 } /* end key_cleanup() */
613 /*****************************************************************************/
615 * dispose of a reference to a key
616 * - when all the references are gone, we schedule the cleanup task to come and
617 * pull it out of the tree in definite process context
619 void key_put(struct key *key)
621 if (key) {
622 key_check(key);
624 if (atomic_dec_and_test(&key->usage))
625 schedule_work(&key_cleanup_task);
628 } /* end key_put() */
630 EXPORT_SYMBOL(key_put);
632 /*****************************************************************************/
634 * find a key by its serial number
636 struct key *key_lookup(key_serial_t id)
638 struct rb_node *n;
639 struct key *key;
641 spin_lock(&key_serial_lock);
643 /* search the tree for the specified key */
644 n = key_serial_tree.rb_node;
645 while (n) {
646 key = rb_entry(n, struct key, serial_node);
648 if (id < key->serial)
649 n = n->rb_left;
650 else if (id > key->serial)
651 n = n->rb_right;
652 else
653 goto found;
656 not_found:
657 key = ERR_PTR(-ENOKEY);
658 goto error;
660 found:
661 /* pretend it doesn't exist if it's dead */
662 if (atomic_read(&key->usage) == 0 ||
663 test_bit(KEY_FLAG_DEAD, &key->flags) ||
664 key->type == &key_type_dead)
665 goto not_found;
667 /* this races with key_put(), but that doesn't matter since key_put()
668 * doesn't actually change the key
670 atomic_inc(&key->usage);
672 error:
673 spin_unlock(&key_serial_lock);
674 return key;
676 } /* end key_lookup() */
678 /*****************************************************************************/
680 * find and lock the specified key type against removal
681 * - we return with the sem readlocked
683 struct key_type *key_type_lookup(const char *type)
685 struct key_type *ktype;
687 down_read(&key_types_sem);
689 /* look up the key type to see if it's one of the registered kernel
690 * types */
691 list_for_each_entry(ktype, &key_types_list, link) {
692 if (strcmp(ktype->name, type) == 0)
693 goto found_kernel_type;
696 up_read(&key_types_sem);
697 ktype = ERR_PTR(-ENOKEY);
699 found_kernel_type:
700 return ktype;
702 } /* end key_type_lookup() */
704 /*****************************************************************************/
706 * unlock a key type
708 void key_type_put(struct key_type *ktype)
710 up_read(&key_types_sem);
712 } /* end key_type_put() */
714 /*****************************************************************************/
716 * attempt to update an existing key
717 * - the key has an incremented refcount
718 * - we need to put the key if we get an error
720 static inline key_ref_t __key_update(key_ref_t key_ref,
721 const void *payload, size_t plen)
723 struct key *key = key_ref_to_ptr(key_ref);
724 int ret;
726 /* need write permission on the key to update it */
727 ret = key_permission(key_ref, KEY_WRITE);
728 if (ret < 0)
729 goto error;
731 ret = -EEXIST;
732 if (!key->type->update)
733 goto error;
735 down_write(&key->sem);
737 ret = key->type->update(key, payload, plen);
738 if (ret == 0)
739 /* updating a negative key instantiates it */
740 clear_bit(KEY_FLAG_NEGATIVE, &key->flags);
742 up_write(&key->sem);
744 if (ret < 0)
745 goto error;
746 out:
747 return key_ref;
749 error:
750 key_put(key);
751 key_ref = ERR_PTR(ret);
752 goto out;
754 } /* end __key_update() */
756 /*****************************************************************************/
758 * search the specified keyring for a key of the same description; if one is
759 * found, update it, otherwise add a new one
761 key_ref_t key_create_or_update(key_ref_t keyring_ref,
762 const char *type,
763 const char *description,
764 const void *payload,
765 size_t plen,
766 unsigned long flags)
768 struct key_type *ktype;
769 struct key *keyring, *key = NULL;
770 key_perm_t perm;
771 key_ref_t key_ref;
772 int ret;
774 /* look up the key type to see if it's one of the registered kernel
775 * types */
776 ktype = key_type_lookup(type);
777 if (IS_ERR(ktype)) {
778 key_ref = ERR_PTR(-ENODEV);
779 goto error;
782 key_ref = ERR_PTR(-EINVAL);
783 if (!ktype->match || !ktype->instantiate)
784 goto error_2;
786 keyring = key_ref_to_ptr(keyring_ref);
788 key_check(keyring);
790 key_ref = ERR_PTR(-ENOTDIR);
791 if (keyring->type != &key_type_keyring)
792 goto error_2;
794 down_write(&keyring->sem);
796 /* if we're going to allocate a new key, we're going to have
797 * to modify the keyring */
798 ret = key_permission(keyring_ref, KEY_WRITE);
799 if (ret < 0) {
800 key_ref = ERR_PTR(ret);
801 goto error_3;
804 /* if it's possible to update this type of key, search for an existing
805 * key of the same type and description in the destination keyring and
806 * update that instead if possible
808 if (ktype->update) {
809 key_ref = __keyring_search_one(keyring_ref, ktype, description,
811 if (!IS_ERR(key_ref))
812 goto found_matching_key;
815 /* decide on the permissions we want */
816 perm = KEY_POS_VIEW | KEY_POS_SEARCH | KEY_POS_LINK | KEY_POS_SETATTR;
817 perm |= KEY_USR_VIEW | KEY_USR_SEARCH | KEY_USR_LINK | KEY_USR_SETATTR;
819 if (ktype->read)
820 perm |= KEY_POS_READ | KEY_USR_READ;
822 if (ktype == &key_type_keyring || ktype->update)
823 perm |= KEY_USR_WRITE;
825 /* allocate a new key */
826 key = key_alloc(ktype, description, current->fsuid, current->fsgid,
827 current, perm, flags);
828 if (IS_ERR(key)) {
829 key_ref = ERR_PTR(PTR_ERR(key));
830 goto error_3;
833 /* instantiate it and link it into the target keyring */
834 ret = __key_instantiate_and_link(key, payload, plen, keyring, NULL);
835 if (ret < 0) {
836 key_put(key);
837 key_ref = ERR_PTR(ret);
838 goto error_3;
841 key_ref = make_key_ref(key, is_key_possessed(keyring_ref));
843 error_3:
844 up_write(&keyring->sem);
845 error_2:
846 key_type_put(ktype);
847 error:
848 return key_ref;
850 found_matching_key:
851 /* we found a matching key, so we're going to try to update it
852 * - we can drop the locks first as we have the key pinned
854 up_write(&keyring->sem);
855 key_type_put(ktype);
857 key_ref = __key_update(key_ref, payload, plen);
858 goto error;
860 } /* end key_create_or_update() */
862 EXPORT_SYMBOL(key_create_or_update);
864 /*****************************************************************************/
866 * update a key
868 int key_update(key_ref_t key_ref, const void *payload, size_t plen)
870 struct key *key = key_ref_to_ptr(key_ref);
871 int ret;
873 key_check(key);
875 /* the key must be writable */
876 ret = key_permission(key_ref, KEY_WRITE);
877 if (ret < 0)
878 goto error;
880 /* attempt to update it if supported */
881 ret = -EOPNOTSUPP;
882 if (key->type->update) {
883 down_write(&key->sem);
885 ret = key->type->update(key, payload, plen);
886 if (ret == 0)
887 /* updating a negative key instantiates it */
888 clear_bit(KEY_FLAG_NEGATIVE, &key->flags);
890 up_write(&key->sem);
893 error:
894 return ret;
896 } /* end key_update() */
898 EXPORT_SYMBOL(key_update);
900 /*****************************************************************************/
902 * revoke a key
904 void key_revoke(struct key *key)
906 key_check(key);
908 /* make sure no one's trying to change or use the key when we mark
909 * it */
910 down_write(&key->sem);
911 set_bit(KEY_FLAG_REVOKED, &key->flags);
913 if (key->type->revoke)
914 key->type->revoke(key);
916 up_write(&key->sem);
918 } /* end key_revoke() */
920 EXPORT_SYMBOL(key_revoke);
922 /*****************************************************************************/
924 * register a type of key
926 int register_key_type(struct key_type *ktype)
928 struct key_type *p;
929 int ret;
931 ret = -EEXIST;
932 down_write(&key_types_sem);
934 /* disallow key types with the same name */
935 list_for_each_entry(p, &key_types_list, link) {
936 if (strcmp(p->name, ktype->name) == 0)
937 goto out;
940 /* store the type */
941 list_add(&ktype->link, &key_types_list);
942 ret = 0;
944 out:
945 up_write(&key_types_sem);
946 return ret;
948 } /* end register_key_type() */
950 EXPORT_SYMBOL(register_key_type);
952 /*****************************************************************************/
954 * unregister a type of key
956 void unregister_key_type(struct key_type *ktype)
958 struct rb_node *_n;
959 struct key *key;
961 down_write(&key_types_sem);
963 /* withdraw the key type */
964 list_del_init(&ktype->link);
966 /* mark all the keys of this type dead */
967 spin_lock(&key_serial_lock);
969 for (_n = rb_first(&key_serial_tree); _n; _n = rb_next(_n)) {
970 key = rb_entry(_n, struct key, serial_node);
972 if (key->type == ktype)
973 key->type = &key_type_dead;
976 spin_unlock(&key_serial_lock);
978 /* make sure everyone revalidates their keys */
979 synchronize_rcu();
981 /* we should now be able to destroy the payloads of all the keys of
982 * this type with impunity */
983 spin_lock(&key_serial_lock);
985 for (_n = rb_first(&key_serial_tree); _n; _n = rb_next(_n)) {
986 key = rb_entry(_n, struct key, serial_node);
988 if (key->type == ktype) {
989 if (ktype->destroy)
990 ktype->destroy(key);
991 memset(&key->payload, 0xbd, sizeof(key->payload));
995 spin_unlock(&key_serial_lock);
996 up_write(&key_types_sem);
998 } /* end unregister_key_type() */
1000 EXPORT_SYMBOL(unregister_key_type);
1002 /*****************************************************************************/
1004 * initialise the key management stuff
1006 void __init key_init(void)
1008 /* allocate a slab in which we can store keys */
1009 key_jar = kmem_cache_create("key_jar", sizeof(struct key),
1010 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1012 /* add the special key types */
1013 list_add_tail(&key_type_keyring.link, &key_types_list);
1014 list_add_tail(&key_type_dead.link, &key_types_list);
1015 list_add_tail(&key_type_user.link, &key_types_list);
1017 /* record the root user tracking */
1018 rb_link_node(&root_key_user.node,
1019 NULL,
1020 &key_user_tree.rb_node);
1022 rb_insert_color(&root_key_user.node,
1023 &key_user_tree);
1025 /* record root's user standard keyrings */
1026 key_check(&root_user_keyring);
1027 key_check(&root_session_keyring);
1029 __key_insert_serial(&root_user_keyring);
1030 __key_insert_serial(&root_session_keyring);
1032 keyring_publish_name(&root_user_keyring);
1033 keyring_publish_name(&root_session_keyring);
1035 /* link the two root keyrings together */
1036 key_link(&root_session_keyring, &root_user_keyring);
1038 } /* end key_init() */