1 /* key.c: basic authentication token and access key management
3 * Copyright (C) 2004 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/workqueue.h>
17 #include <linux/err.h>
20 static kmem_cache_t *key_jar;
21 static key_serial_t key_serial_next = 3;
22 struct rb_root key_serial_tree; /* tree of keys indexed by serial */
23 DEFINE_SPINLOCK(key_serial_lock);
25 struct rb_root key_user_tree; /* tree of quota records indexed by UID */
26 DEFINE_SPINLOCK(key_user_lock);
28 static LIST_HEAD(key_types_list);
29 static DECLARE_RWSEM(key_types_sem);
31 static void key_cleanup(void *data);
32 static DECLARE_WORK(key_cleanup_task, key_cleanup, NULL);
34 /* we serialise key instantiation and link */
35 DECLARE_RWSEM(key_construction_sem);
37 /* any key who's type gets unegistered will be re-typed to this */
38 struct key_type key_type_dead = {
43 void __key_check(const struct key *key)
45 printk("__key_check: key %p {%08x} should be {%08x}\n",
46 key, key->magic, KEY_DEBUG_MAGIC);
51 /*****************************************************************************/
53 * get the key quota record for a user, allocating a new record if one doesn't
56 struct key_user *key_user_lookup(uid_t uid)
58 struct key_user *candidate = NULL, *user;
59 struct rb_node *parent = NULL;
60 struct rb_node **p = &key_user_tree.rb_node;
63 spin_lock(&key_user_lock);
65 /* search the tree for a user record with a matching UID */
68 user = rb_entry(parent, struct key_user, node);
72 else if (uid > user->uid)
78 /* if we get here, we failed to find a match in the tree */
80 /* allocate a candidate user record if we don't already have
82 spin_unlock(&key_user_lock);
85 candidate = kmalloc(sizeof(struct key_user), GFP_KERNEL);
86 if (unlikely(!candidate))
89 /* the allocation may have scheduled, so we need to repeat the
90 * search lest someone else added the record whilst we were
95 /* if we get here, then the user record still hadn't appeared on the
96 * second pass - so we use the candidate record */
97 atomic_set(&candidate->usage, 1);
98 atomic_set(&candidate->nkeys, 0);
99 atomic_set(&candidate->nikeys, 0);
100 candidate->uid = uid;
101 candidate->qnkeys = 0;
102 candidate->qnbytes = 0;
103 spin_lock_init(&candidate->lock);
104 INIT_LIST_HEAD(&candidate->consq);
106 rb_link_node(&candidate->node, parent, p);
107 rb_insert_color(&candidate->node, &key_user_tree);
108 spin_unlock(&key_user_lock);
112 /* okay - we found a user record for this UID */
114 atomic_inc(&user->usage);
115 spin_unlock(&key_user_lock);
121 } /* end key_user_lookup() */
123 /*****************************************************************************/
125 * dispose of a user structure
127 void key_user_put(struct key_user *user)
129 if (atomic_dec_and_lock(&user->usage, &key_user_lock)) {
130 rb_erase(&user->node, &key_user_tree);
131 spin_unlock(&key_user_lock);
136 } /* end key_user_put() */
138 /*****************************************************************************/
140 * insert a key with a fixed serial number
142 static void __init __key_insert_serial(struct key *key)
144 struct rb_node *parent, **p;
148 p = &key_serial_tree.rb_node;
152 xkey = rb_entry(parent, struct key, serial_node);
154 if (key->serial < xkey->serial)
156 else if (key->serial > xkey->serial)
162 /* we've found a suitable hole - arrange for this key to occupy it */
163 rb_link_node(&key->serial_node, parent, p);
164 rb_insert_color(&key->serial_node, &key_serial_tree);
166 } /* end __key_insert_serial() */
168 /*****************************************************************************/
170 * assign a key the next unique serial number
171 * - we work through all the serial numbers between 2 and 2^31-1 in turn and
174 static inline void key_alloc_serial(struct key *key)
176 struct rb_node *parent, **p;
179 spin_lock(&key_serial_lock);
181 /* propose a likely serial number and look for a hole for it in the
182 * serial number tree */
183 key->serial = key_serial_next;
186 key_serial_next = key->serial + 1;
189 p = &key_serial_tree.rb_node;
193 xkey = rb_entry(parent, struct key, serial_node);
195 if (key->serial < xkey->serial)
197 else if (key->serial > xkey->serial)
204 /* we found a key with the proposed serial number - walk the tree from
205 * that point looking for the next unused serial number */
208 key->serial = key_serial_next;
211 key_serial_next = key->serial + 1;
213 if (!parent->rb_parent)
214 p = &key_serial_tree.rb_node;
215 else if (parent->rb_parent->rb_left == parent)
216 p = &parent->rb_parent->rb_left;
218 p = &parent->rb_parent->rb_right;
220 parent = rb_next(parent);
224 xkey = rb_entry(parent, struct key, serial_node);
225 if (key->serial < xkey->serial)
229 /* we've found a suitable hole - arrange for this key to occupy it */
231 rb_link_node(&key->serial_node, parent, p);
232 rb_insert_color(&key->serial_node, &key_serial_tree);
234 spin_unlock(&key_serial_lock);
236 } /* end key_alloc_serial() */
238 /*****************************************************************************/
240 * allocate a key of the specified type
241 * - update the user's quota to reflect the existence of the key
242 * - called from a key-type operation with key_types_sem read-locked by either
243 * key_create_or_update() or by key_duplicate(); this prevents unregistration
245 * - upon return the key is as yet uninstantiated; the caller needs to either
246 * instantiate the key or discard it before returning
248 struct key *key_alloc(struct key_type *type, const char *desc,
249 uid_t uid, gid_t gid, key_perm_t perm,
252 struct key_user *user = NULL;
254 size_t desclen, quotalen;
256 key = ERR_PTR(-EINVAL);
260 desclen = strlen(desc) + 1;
261 quotalen = desclen + type->def_datalen;
263 /* get hold of the key tracking for this user */
264 user = key_user_lookup(uid);
268 /* check that the user's quota permits allocation of another key and
271 spin_lock(&user->lock);
272 if (user->qnkeys + 1 >= KEYQUOTA_MAX_KEYS &&
273 user->qnbytes + quotalen >= KEYQUOTA_MAX_BYTES
278 user->qnbytes += quotalen;
279 spin_unlock(&user->lock);
282 /* allocate and initialise the key and its description */
283 key = kmem_cache_alloc(key_jar, SLAB_KERNEL);
288 key->description = kmalloc(desclen, GFP_KERNEL);
289 if (!key->description)
292 memcpy(key->description, desc, desclen);
295 atomic_set(&key->usage, 1);
296 rwlock_init(&key->lock);
297 init_rwsem(&key->sem);
300 key->quotalen = quotalen;
301 key->datalen = type->def_datalen;
307 key->payload.data = NULL;
310 key->flags |= KEY_FLAG_IN_QUOTA;
312 memset(&key->type_data, 0, sizeof(key->type_data));
315 key->magic = KEY_DEBUG_MAGIC;
318 /* publish the key by giving it a serial number */
319 atomic_inc(&user->nkeys);
320 key_alloc_serial(key);
326 kmem_cache_free(key_jar, key);
329 spin_lock(&user->lock);
331 user->qnbytes -= quotalen;
332 spin_unlock(&user->lock);
336 key = ERR_PTR(-ENOMEM);
340 spin_unlock(&user->lock);
342 key = ERR_PTR(-EDQUOT);
345 } /* end key_alloc() */
347 EXPORT_SYMBOL(key_alloc);
349 /*****************************************************************************/
351 * reserve an amount of quota for the key's payload
353 int key_payload_reserve(struct key *key, size_t datalen)
355 int delta = (int) datalen - key->datalen;
360 /* contemplate the quota adjustment */
361 if (delta != 0 && key->flags & KEY_FLAG_IN_QUOTA) {
362 spin_lock(&key->user->lock);
365 key->user->qnbytes + delta > KEYQUOTA_MAX_BYTES
370 key->user->qnbytes += delta;
371 key->quotalen += delta;
373 spin_unlock(&key->user->lock);
376 /* change the recorded data length if that didn't generate an error */
378 key->datalen = datalen;
382 } /* end key_payload_reserve() */
384 EXPORT_SYMBOL(key_payload_reserve);
386 /*****************************************************************************/
388 * instantiate a key and link it into the target keyring atomically
389 * - called with the target keyring's semaphore writelocked
391 static int __key_instantiate_and_link(struct key *key,
404 down_write(&key_construction_sem);
406 /* can't instantiate twice */
407 if (!(key->flags & KEY_FLAG_INSTANTIATED)) {
408 /* instantiate the key */
409 ret = key->type->instantiate(key, data, datalen);
412 /* mark the key as being instantiated */
413 write_lock(&key->lock);
415 atomic_inc(&key->user->nikeys);
416 key->flags |= KEY_FLAG_INSTANTIATED;
418 if (key->flags & KEY_FLAG_USER_CONSTRUCT) {
419 key->flags &= ~KEY_FLAG_USER_CONSTRUCT;
423 write_unlock(&key->lock);
425 /* and link it into the destination keyring */
427 ret = __key_link(keyring, key);
431 up_write(&key_construction_sem);
433 /* wake up anyone waiting for a key to be constructed */
435 wake_up_all(&request_key_conswq);
439 } /* end __key_instantiate_and_link() */
441 /*****************************************************************************/
443 * instantiate a key and link it into the target keyring atomically
445 int key_instantiate_and_link(struct key *key,
453 down_write(&keyring->sem);
455 ret = __key_instantiate_and_link(key, data, datalen, keyring);
458 up_write(&keyring->sem);
461 } /* end key_instantiate_and_link() */
463 EXPORT_SYMBOL(key_instantiate_and_link);
465 /*****************************************************************************/
467 * negatively instantiate a key and link it into the target keyring atomically
469 int key_negate_and_link(struct key *key,
483 down_write(&keyring->sem);
485 down_write(&key_construction_sem);
487 /* can't instantiate twice */
488 if (!(key->flags & KEY_FLAG_INSTANTIATED)) {
489 /* mark the key as being negatively instantiated */
490 write_lock(&key->lock);
492 atomic_inc(&key->user->nikeys);
493 key->flags |= KEY_FLAG_INSTANTIATED | KEY_FLAG_NEGATIVE;
494 now = current_kernel_time();
495 key->expiry = now.tv_sec + timeout;
497 if (key->flags & KEY_FLAG_USER_CONSTRUCT) {
498 key->flags &= ~KEY_FLAG_USER_CONSTRUCT;
502 write_unlock(&key->lock);
505 /* and link it into the destination keyring */
507 ret = __key_link(keyring, key);
510 up_write(&key_construction_sem);
513 up_write(&keyring->sem);
515 /* wake up anyone waiting for a key to be constructed */
517 wake_up_all(&request_key_conswq);
521 } /* end key_negate_and_link() */
523 EXPORT_SYMBOL(key_negate_and_link);
525 /*****************************************************************************/
527 * do cleaning up in process context so that we don't have to disable
528 * interrupts all over the place
530 static void key_cleanup(void *data)
536 /* look for a dead key in the tree */
537 spin_lock(&key_serial_lock);
539 for (_n = rb_first(&key_serial_tree); _n; _n = rb_next(_n)) {
540 key = rb_entry(_n, struct key, serial_node);
542 if (atomic_read(&key->usage) == 0)
546 spin_unlock(&key_serial_lock);
550 /* we found a dead key - once we've removed it from the tree, we can
552 rb_erase(&key->serial_node, &key_serial_tree);
553 spin_unlock(&key_serial_lock);
555 /* deal with the user's key tracking and quota */
556 if (key->flags & KEY_FLAG_IN_QUOTA) {
557 spin_lock(&key->user->lock);
559 key->user->qnbytes -= key->quotalen;
560 spin_unlock(&key->user->lock);
563 atomic_dec(&key->user->nkeys);
564 if (key->flags & KEY_FLAG_INSTANTIATED)
565 atomic_dec(&key->user->nikeys);
567 key_user_put(key->user);
569 /* now throw away the key memory */
570 if (key->type->destroy)
571 key->type->destroy(key);
573 kfree(key->description);
576 key->magic = KEY_DEBUG_MAGIC_X;
578 kmem_cache_free(key_jar, key);
580 /* there may, of course, be more than one key to destroy */
583 } /* end key_cleanup() */
585 /*****************************************************************************/
587 * dispose of a reference to a key
588 * - when all the references are gone, we schedule the cleanup task to come and
589 * pull it out of the tree in definite process context
591 void key_put(struct key *key)
596 if (atomic_dec_and_test(&key->usage))
597 schedule_work(&key_cleanup_task);
600 } /* end key_put() */
602 EXPORT_SYMBOL(key_put);
604 /*****************************************************************************/
606 * find a key by its serial number
608 struct key *key_lookup(key_serial_t id)
613 spin_lock(&key_serial_lock);
615 /* search the tree for the specified key */
616 n = key_serial_tree.rb_node;
618 key = rb_entry(n, struct key, serial_node);
620 if (id < key->serial)
622 else if (id > key->serial)
629 key = ERR_PTR(-ENOKEY);
633 /* pretent doesn't exist if it's dead */
634 if (atomic_read(&key->usage) == 0 ||
635 (key->flags & KEY_FLAG_DEAD) ||
636 key->type == &key_type_dead)
639 /* this races with key_put(), but that doesn't matter since key_put()
640 * doesn't actually change the key
642 atomic_inc(&key->usage);
645 spin_unlock(&key_serial_lock);
648 } /* end key_lookup() */
650 /*****************************************************************************/
652 * find and lock the specified key type against removal
653 * - we return with the sem readlocked
655 struct key_type *key_type_lookup(const char *type)
657 struct key_type *ktype;
659 down_read(&key_types_sem);
661 /* look up the key type to see if it's one of the registered kernel
663 list_for_each_entry(ktype, &key_types_list, link) {
664 if (strcmp(ktype->name, type) == 0)
665 goto found_kernel_type;
668 up_read(&key_types_sem);
669 ktype = ERR_PTR(-ENOKEY);
674 } /* end key_type_lookup() */
676 /*****************************************************************************/
680 void key_type_put(struct key_type *ktype)
682 up_read(&key_types_sem);
684 } /* end key_type_put() */
686 /*****************************************************************************/
688 * attempt to update an existing key
689 * - the key has an incremented refcount
690 * - we need to put the key if we get an error
692 static inline struct key *__key_update(struct key *key, const void *payload,
697 /* need write permission on the key to update it */
699 if (!key_permission(key, KEY_WRITE))
703 if (!key->type->update)
706 down_write(&key->sem);
708 ret = key->type->update(key, payload, plen);
711 /* updating a negative key instantiates it */
712 write_lock(&key->lock);
713 key->flags &= ~KEY_FLAG_NEGATIVE;
714 write_unlock(&key->lock);
729 } /* end __key_update() */
731 /*****************************************************************************/
733 * search the specified keyring for a key of the same description; if one is
734 * found, update it, otherwise add a new one
736 struct key *key_create_or_update(struct key *keyring,
738 const char *description,
743 struct key_type *ktype;
744 struct key *key = NULL;
750 /* look up the key type to see if it's one of the registered kernel
752 ktype = key_type_lookup(type);
754 key = ERR_PTR(-ENODEV);
759 if (!ktype->match || !ktype->instantiate)
762 /* search for an existing key of the same type and description in the
763 * destination keyring
765 down_write(&keyring->sem);
767 key = __keyring_search_one(keyring, ktype, description, 0);
769 goto found_matching_key;
771 /* if we're going to allocate a new key, we're going to have to modify
774 if (!key_permission(keyring, KEY_WRITE))
777 /* decide on the permissions we want */
778 perm = KEY_USR_VIEW | KEY_USR_SEARCH | KEY_USR_LINK;
781 perm |= KEY_USR_READ;
783 if (ktype == &key_type_keyring || ktype->update)
784 perm |= KEY_USR_WRITE;
786 /* allocate a new key */
787 key = key_alloc(ktype, description, current->fsuid, current->fsgid,
794 /* instantiate it and link it into the target keyring */
795 ret = __key_instantiate_and_link(key, payload, plen, keyring);
802 up_write(&keyring->sem);
809 /* we found a matching key, so we're going to try to update it
810 * - we can drop the locks first as we have the key pinned
812 up_write(&keyring->sem);
815 key = __key_update(key, payload, plen);
818 } /* end key_create_or_update() */
820 EXPORT_SYMBOL(key_create_or_update);
822 /*****************************************************************************/
826 int key_update(struct key *key, const void *payload, size_t plen)
832 /* the key must be writable */
834 if (!key_permission(key, KEY_WRITE))
837 /* attempt to update it if supported */
839 if (key->type->update) {
840 down_write(&key->sem);
841 ret = key->type->update(key, payload, plen);
844 /* updating a negative key instantiates it */
845 write_lock(&key->lock);
846 key->flags &= ~KEY_FLAG_NEGATIVE;
847 write_unlock(&key->lock);
856 } /* end key_update() */
858 EXPORT_SYMBOL(key_update);
860 /*****************************************************************************/
862 * duplicate a key, potentially with a revised description
863 * - must be supported by the keytype (keyrings for instance can be duplicated)
865 struct key *key_duplicate(struct key *source, const char *desc)
873 desc = source->description;
875 down_read(&key_types_sem);
878 if (!source->type->duplicate)
881 /* allocate and instantiate a key */
882 key = key_alloc(source->type, desc, current->fsuid, current->fsgid,
887 down_read(&source->sem);
888 ret = key->type->duplicate(key, source);
889 up_read(&source->sem);
893 atomic_inc(&key->user->nikeys);
895 write_lock(&key->lock);
896 key->flags |= KEY_FLAG_INSTANTIATED;
897 write_unlock(&key->lock);
900 up_read(&key_types_sem);
907 up_read(&key_types_sem);
911 } /* end key_duplicate() */
913 /*****************************************************************************/
917 void key_revoke(struct key *key)
921 /* make sure no one's trying to change or use the key when we mark
923 down_write(&key->sem);
924 write_lock(&key->lock);
925 key->flags |= KEY_FLAG_REVOKED;
926 write_unlock(&key->lock);
929 } /* end key_revoke() */
931 EXPORT_SYMBOL(key_revoke);
933 /*****************************************************************************/
935 * register a type of key
937 int register_key_type(struct key_type *ktype)
943 down_write(&key_types_sem);
945 /* disallow key types with the same name */
946 list_for_each_entry(p, &key_types_list, link) {
947 if (strcmp(p->name, ktype->name) == 0)
952 list_add(&ktype->link, &key_types_list);
956 up_write(&key_types_sem);
959 } /* end register_key_type() */
961 EXPORT_SYMBOL(register_key_type);
963 /*****************************************************************************/
965 * unregister a type of key
967 void unregister_key_type(struct key_type *ktype)
972 down_write(&key_types_sem);
974 /* withdraw the key type */
975 list_del_init(&ktype->link);
977 /* need to withdraw all keys of this type */
978 spin_lock(&key_serial_lock);
980 for (_n = rb_first(&key_serial_tree); _n; _n = rb_next(_n)) {
981 key = rb_entry(_n, struct key, serial_node);
983 if (key->type != ktype)
986 write_lock(&key->lock);
987 key->type = &key_type_dead;
988 write_unlock(&key->lock);
990 /* there shouldn't be anyone looking at the description or
994 memset(&key->payload, 0xbd, sizeof(key->payload));
997 spin_unlock(&key_serial_lock);
998 up_write(&key_types_sem);
1000 } /* end unregister_key_type() */
1002 EXPORT_SYMBOL(unregister_key_type);
1004 /*****************************************************************************/
1006 * initialise the key management stuff
1008 void __init key_init(void)
1010 /* allocate a slab in which we can store keys */
1011 key_jar = kmem_cache_create("key_jar", sizeof(struct key),
1012 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1014 /* add the special key types */
1015 list_add_tail(&key_type_keyring.link, &key_types_list);
1016 list_add_tail(&key_type_dead.link, &key_types_list);
1017 list_add_tail(&key_type_user.link, &key_types_list);
1019 /* record the root user tracking */
1020 rb_link_node(&root_key_user.node,
1022 &key_user_tree.rb_node);
1024 rb_insert_color(&root_key_user.node,
1027 /* record root's user standard keyrings */
1028 key_check(&root_user_keyring);
1029 key_check(&root_session_keyring);
1031 __key_insert_serial(&root_user_keyring);
1032 __key_insert_serial(&root_session_keyring);
1034 keyring_publish_name(&root_user_keyring);
1035 keyring_publish_name(&root_session_keyring);
1037 /* link the two root keyrings together */
1038 key_link(&root_session_keyring, &root_user_keyring);
1039 } /* end key_init() */