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/security.h>
17 #include <linux/workqueue.h>
18 #include <linux/err.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 struct key_type key_type_dead = {
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);
52 /*****************************************************************************/
54 * get the key quota record for a user, allocating a new record if one doesn't
57 struct key_user *key_user_lookup(uid_t uid)
59 struct key_user *candidate = NULL, *user;
60 struct rb_node *parent = NULL;
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 */
70 user = rb_entry(parent, struct key_user, node);
74 else if (uid > user->uid)
80 /* if we get here, we failed to find a match in the tree */
82 /* allocate a candidate user record if we don't already have
84 spin_unlock(&key_user_lock);
87 candidate = kmalloc(sizeof(struct key_user), GFP_KERNEL);
88 if (unlikely(!candidate))
91 /* the allocation may have scheduled, so we need to repeat the
92 * search lest someone else added the record whilst we were
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);
114 /* okay - we found a user record for this UID */
116 atomic_inc(&user->usage);
117 spin_unlock(&key_user_lock);
123 } /* end key_user_lookup() */
125 /*****************************************************************************/
127 * dispose of a user structure
129 void key_user_put(struct key_user *user)
131 if (atomic_dec_and_lock(&user->usage, &key_user_lock)) {
132 rb_erase(&user->node, &key_user_tree);
133 spin_unlock(&key_user_lock);
138 } /* end key_user_put() */
140 /*****************************************************************************/
142 * insert a key with a fixed serial number
144 static void __init __key_insert_serial(struct key *key)
146 struct rb_node *parent, **p;
150 p = &key_serial_tree.rb_node;
154 xkey = rb_entry(parent, struct key, serial_node);
156 if (key->serial < xkey->serial)
158 else if (key->serial > xkey->serial)
164 /* we've found a suitable hole - arrange for this key to occupy it */
165 rb_link_node(&key->serial_node, parent, p);
166 rb_insert_color(&key->serial_node, &key_serial_tree);
168 } /* end __key_insert_serial() */
170 /*****************************************************************************/
172 * assign a key the next unique serial number
173 * - we work through all the serial numbers between 2 and 2^31-1 in turn and
176 static inline void key_alloc_serial(struct key *key)
178 struct rb_node *parent, **p;
181 spin_lock(&key_serial_lock);
183 /* propose a likely serial number and look for a hole for it in the
184 * serial number tree */
185 key->serial = key_serial_next;
188 key_serial_next = key->serial + 1;
191 p = &key_serial_tree.rb_node;
195 xkey = rb_entry(parent, struct key, serial_node);
197 if (key->serial < xkey->serial)
199 else if (key->serial > xkey->serial)
206 /* we found a key with the proposed serial number - walk the tree from
207 * that point looking for the next unused serial number */
210 key->serial = key_serial_next;
213 key_serial_next = key->serial + 1;
215 if (!parent->rb_parent)
216 p = &key_serial_tree.rb_node;
217 else if (parent->rb_parent->rb_left == parent)
218 p = &parent->rb_parent->rb_left;
220 p = &parent->rb_parent->rb_right;
222 parent = rb_next(parent);
226 xkey = rb_entry(parent, struct key, serial_node);
227 if (key->serial < xkey->serial)
231 /* we've found a suitable hole - arrange for this key to occupy it */
233 rb_link_node(&key->serial_node, parent, p);
234 rb_insert_color(&key->serial_node, &key_serial_tree);
236 spin_unlock(&key_serial_lock);
238 } /* end key_alloc_serial() */
240 /*****************************************************************************/
242 * allocate a key of the specified type
243 * - update the user's quota to reflect the existence of the key
244 * - called from a key-type operation with key_types_sem read-locked by either
245 * key_create_or_update() or by key_duplicate(); this prevents unregistration
247 * - upon return the key is as yet uninstantiated; the caller needs to either
248 * instantiate the key or discard it before returning
250 struct key *key_alloc(struct key_type *type, const char *desc,
251 uid_t uid, gid_t gid, key_perm_t perm,
254 struct key_user *user = NULL;
256 size_t desclen, quotalen;
259 key = ERR_PTR(-EINVAL);
263 desclen = strlen(desc) + 1;
264 quotalen = desclen + type->def_datalen;
266 /* get hold of the key tracking for this user */
267 user = key_user_lookup(uid);
271 /* check that the user's quota permits allocation of another key and
274 spin_lock(&user->lock);
275 if (user->qnkeys + 1 >= KEYQUOTA_MAX_KEYS &&
276 user->qnbytes + quotalen >= KEYQUOTA_MAX_BYTES
281 user->qnbytes += quotalen;
282 spin_unlock(&user->lock);
285 /* allocate and initialise the key and its description */
286 key = kmem_cache_alloc(key_jar, SLAB_KERNEL);
291 key->description = kmalloc(desclen, GFP_KERNEL);
292 if (!key->description)
295 memcpy(key->description, desc, desclen);
298 atomic_set(&key->usage, 1);
299 init_rwsem(&key->sem);
302 key->quotalen = quotalen;
303 key->datalen = type->def_datalen;
309 key->payload.data = NULL;
310 key->security = NULL;
313 key->flags |= 1 << KEY_FLAG_IN_QUOTA;
315 memset(&key->type_data, 0, sizeof(key->type_data));
318 key->magic = KEY_DEBUG_MAGIC;
321 /* let the security module know about the key */
322 ret = security_key_alloc(key);
326 /* publish the key by giving it a serial number */
327 atomic_inc(&user->nkeys);
328 key_alloc_serial(key);
334 kfree(key->description);
335 kmem_cache_free(key_jar, key);
337 spin_lock(&user->lock);
339 user->qnbytes -= quotalen;
340 spin_unlock(&user->lock);
347 kmem_cache_free(key_jar, key);
350 spin_lock(&user->lock);
352 user->qnbytes -= quotalen;
353 spin_unlock(&user->lock);
357 key = ERR_PTR(-ENOMEM);
361 spin_unlock(&user->lock);
363 key = ERR_PTR(-EDQUOT);
366 } /* end key_alloc() */
368 EXPORT_SYMBOL(key_alloc);
370 /*****************************************************************************/
372 * reserve an amount of quota for the key's payload
374 int key_payload_reserve(struct key *key, size_t datalen)
376 int delta = (int) datalen - key->datalen;
381 /* contemplate the quota adjustment */
382 if (delta != 0 && test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
383 spin_lock(&key->user->lock);
386 key->user->qnbytes + delta > KEYQUOTA_MAX_BYTES
391 key->user->qnbytes += delta;
392 key->quotalen += delta;
394 spin_unlock(&key->user->lock);
397 /* change the recorded data length if that didn't generate an error */
399 key->datalen = datalen;
403 } /* end key_payload_reserve() */
405 EXPORT_SYMBOL(key_payload_reserve);
407 /*****************************************************************************/
409 * instantiate a key and link it into the target keyring atomically
410 * - called with the target keyring's semaphore writelocked
412 static int __key_instantiate_and_link(struct key *key,
426 down_write(&key_construction_sem);
428 /* can't instantiate twice */
429 if (!test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
430 /* instantiate the key */
431 ret = key->type->instantiate(key, data, datalen);
434 /* mark the key as being instantiated */
435 atomic_inc(&key->user->nikeys);
436 set_bit(KEY_FLAG_INSTANTIATED, &key->flags);
438 if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags))
441 /* and link it into the destination keyring */
443 ret = __key_link(keyring, key);
445 /* disable the authorisation key */
451 up_write(&key_construction_sem);
453 /* wake up anyone waiting for a key to be constructed */
455 wake_up_all(&request_key_conswq);
459 } /* end __key_instantiate_and_link() */
461 /*****************************************************************************/
463 * instantiate a key and link it into the target keyring atomically
465 int key_instantiate_and_link(struct key *key,
474 down_write(&keyring->sem);
476 ret = __key_instantiate_and_link(key, data, datalen, keyring, instkey);
479 up_write(&keyring->sem);
483 } /* end key_instantiate_and_link() */
485 EXPORT_SYMBOL(key_instantiate_and_link);
487 /*****************************************************************************/
489 * negatively instantiate a key and link it into the target keyring atomically
491 int key_negate_and_link(struct key *key,
506 down_write(&keyring->sem);
508 down_write(&key_construction_sem);
510 /* can't instantiate twice */
511 if (!test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
512 /* mark the key as being negatively instantiated */
513 atomic_inc(&key->user->nikeys);
514 set_bit(KEY_FLAG_NEGATIVE, &key->flags);
515 set_bit(KEY_FLAG_INSTANTIATED, &key->flags);
516 now = current_kernel_time();
517 key->expiry = now.tv_sec + timeout;
519 if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags))
524 /* and link it into the destination keyring */
526 ret = __key_link(keyring, key);
528 /* disable the authorisation key */
533 up_write(&key_construction_sem);
536 up_write(&keyring->sem);
538 /* wake up anyone waiting for a key to be constructed */
540 wake_up_all(&request_key_conswq);
544 } /* end key_negate_and_link() */
546 EXPORT_SYMBOL(key_negate_and_link);
548 /*****************************************************************************/
550 * do cleaning up in process context so that we don't have to disable
551 * interrupts all over the place
553 static void key_cleanup(void *data)
559 /* look for a dead key in the tree */
560 spin_lock(&key_serial_lock);
562 for (_n = rb_first(&key_serial_tree); _n; _n = rb_next(_n)) {
563 key = rb_entry(_n, struct key, serial_node);
565 if (atomic_read(&key->usage) == 0)
569 spin_unlock(&key_serial_lock);
573 /* we found a dead key - once we've removed it from the tree, we can
575 rb_erase(&key->serial_node, &key_serial_tree);
576 spin_unlock(&key_serial_lock);
580 security_key_free(key);
582 /* deal with the user's key tracking and quota */
583 if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
584 spin_lock(&key->user->lock);
586 key->user->qnbytes -= key->quotalen;
587 spin_unlock(&key->user->lock);
590 atomic_dec(&key->user->nkeys);
591 if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
592 atomic_dec(&key->user->nikeys);
594 key_user_put(key->user);
596 /* now throw away the key memory */
597 if (key->type->destroy)
598 key->type->destroy(key);
600 kfree(key->description);
603 key->magic = KEY_DEBUG_MAGIC_X;
605 kmem_cache_free(key_jar, key);
607 /* there may, of course, be more than one key to destroy */
610 } /* end key_cleanup() */
612 /*****************************************************************************/
614 * dispose of a reference to a key
615 * - when all the references are gone, we schedule the cleanup task to come and
616 * pull it out of the tree in definite process context
618 void key_put(struct key *key)
623 if (atomic_dec_and_test(&key->usage))
624 schedule_work(&key_cleanup_task);
627 } /* end key_put() */
629 EXPORT_SYMBOL(key_put);
631 /*****************************************************************************/
633 * find a key by its serial number
635 struct key *key_lookup(key_serial_t id)
640 spin_lock(&key_serial_lock);
642 /* search the tree for the specified key */
643 n = key_serial_tree.rb_node;
645 key = rb_entry(n, struct key, serial_node);
647 if (id < key->serial)
649 else if (id > key->serial)
656 key = ERR_PTR(-ENOKEY);
660 /* pretend it doesn't exist if it's dead */
661 if (atomic_read(&key->usage) == 0 ||
662 test_bit(KEY_FLAG_DEAD, &key->flags) ||
663 key->type == &key_type_dead)
666 /* this races with key_put(), but that doesn't matter since key_put()
667 * doesn't actually change the key
669 atomic_inc(&key->usage);
672 spin_unlock(&key_serial_lock);
675 } /* end key_lookup() */
677 /*****************************************************************************/
679 * find and lock the specified key type against removal
680 * - we return with the sem readlocked
682 struct key_type *key_type_lookup(const char *type)
684 struct key_type *ktype;
686 down_read(&key_types_sem);
688 /* look up the key type to see if it's one of the registered kernel
690 list_for_each_entry(ktype, &key_types_list, link) {
691 if (strcmp(ktype->name, type) == 0)
692 goto found_kernel_type;
695 up_read(&key_types_sem);
696 ktype = ERR_PTR(-ENOKEY);
701 } /* end key_type_lookup() */
703 /*****************************************************************************/
707 void key_type_put(struct key_type *ktype)
709 up_read(&key_types_sem);
711 } /* end key_type_put() */
713 /*****************************************************************************/
715 * attempt to update an existing key
716 * - the key has an incremented refcount
717 * - we need to put the key if we get an error
719 static inline key_ref_t __key_update(key_ref_t key_ref,
720 const void *payload, size_t plen)
722 struct key *key = key_ref_to_ptr(key_ref);
725 /* need write permission on the key to update it */
726 ret = key_permission(key_ref, KEY_WRITE);
731 if (!key->type->update)
734 down_write(&key->sem);
736 ret = key->type->update(key, payload, plen);
738 /* updating a negative key instantiates it */
739 clear_bit(KEY_FLAG_NEGATIVE, &key->flags);
750 key_ref = ERR_PTR(ret);
753 } /* end __key_update() */
755 /*****************************************************************************/
757 * search the specified keyring for a key of the same description; if one is
758 * found, update it, otherwise add a new one
760 key_ref_t key_create_or_update(key_ref_t keyring_ref,
762 const char *description,
767 struct key_type *ktype;
768 struct key *keyring, *key = NULL;
773 /* look up the key type to see if it's one of the registered kernel
775 ktype = key_type_lookup(type);
777 key_ref = ERR_PTR(-ENODEV);
781 key_ref = ERR_PTR(-EINVAL);
782 if (!ktype->match || !ktype->instantiate)
785 keyring = key_ref_to_ptr(keyring_ref);
789 down_write(&keyring->sem);
791 /* if we're going to allocate a new key, we're going to have
792 * to modify the keyring */
793 ret = key_permission(keyring_ref, KEY_WRITE);
795 key_ref = ERR_PTR(ret);
799 /* search for an existing key of the same type and description in the
800 * destination keyring
802 key_ref = __keyring_search_one(keyring_ref, ktype, description, 0);
803 if (!IS_ERR(key_ref))
804 goto found_matching_key;
806 /* decide on the permissions we want */
807 perm = KEY_POS_VIEW | KEY_POS_SEARCH | KEY_POS_LINK | KEY_POS_SETATTR;
808 perm |= KEY_USR_VIEW | KEY_USR_SEARCH | KEY_USR_LINK | KEY_USR_SETATTR;
811 perm |= KEY_POS_READ | KEY_USR_READ;
813 if (ktype == &key_type_keyring || ktype->update)
814 perm |= KEY_USR_WRITE;
816 /* allocate a new key */
817 key = key_alloc(ktype, description, current->fsuid, current->fsgid,
820 key_ref = ERR_PTR(PTR_ERR(key));
824 /* instantiate it and link it into the target keyring */
825 ret = __key_instantiate_and_link(key, payload, plen, keyring, NULL);
828 key_ref = ERR_PTR(ret);
832 key_ref = make_key_ref(key, is_key_possessed(keyring_ref));
835 up_write(&keyring->sem);
842 /* we found a matching key, so we're going to try to update it
843 * - we can drop the locks first as we have the key pinned
845 up_write(&keyring->sem);
848 key_ref = __key_update(key_ref, payload, plen);
851 } /* end key_create_or_update() */
853 EXPORT_SYMBOL(key_create_or_update);
855 /*****************************************************************************/
859 int key_update(key_ref_t key_ref, const void *payload, size_t plen)
861 struct key *key = key_ref_to_ptr(key_ref);
866 /* the key must be writable */
867 ret = key_permission(key_ref, KEY_WRITE);
871 /* attempt to update it if supported */
873 if (key->type->update) {
874 down_write(&key->sem);
876 ret = key->type->update(key, payload, plen);
878 /* updating a negative key instantiates it */
879 clear_bit(KEY_FLAG_NEGATIVE, &key->flags);
887 } /* end key_update() */
889 EXPORT_SYMBOL(key_update);
891 /*****************************************************************************/
893 * duplicate a key, potentially with a revised description
894 * - must be supported by the keytype (keyrings for instance can be duplicated)
896 struct key *key_duplicate(struct key *source, const char *desc)
904 desc = source->description;
906 down_read(&key_types_sem);
909 if (!source->type->duplicate)
912 /* allocate and instantiate a key */
913 key = key_alloc(source->type, desc, current->fsuid, current->fsgid,
918 down_read(&source->sem);
919 ret = key->type->duplicate(key, source);
920 up_read(&source->sem);
924 atomic_inc(&key->user->nikeys);
925 set_bit(KEY_FLAG_INSTANTIATED, &key->flags);
928 up_read(&key_types_sem);
935 up_read(&key_types_sem);
939 } /* end key_duplicate() */
941 /*****************************************************************************/
945 void key_revoke(struct key *key)
949 /* make sure no one's trying to change or use the key when we mark
951 down_write(&key->sem);
952 set_bit(KEY_FLAG_REVOKED, &key->flags);
955 } /* end key_revoke() */
957 EXPORT_SYMBOL(key_revoke);
959 /*****************************************************************************/
961 * register a type of key
963 int register_key_type(struct key_type *ktype)
969 down_write(&key_types_sem);
971 /* disallow key types with the same name */
972 list_for_each_entry(p, &key_types_list, link) {
973 if (strcmp(p->name, ktype->name) == 0)
978 list_add(&ktype->link, &key_types_list);
982 up_write(&key_types_sem);
985 } /* end register_key_type() */
987 EXPORT_SYMBOL(register_key_type);
989 /*****************************************************************************/
991 * unregister a type of key
993 void unregister_key_type(struct key_type *ktype)
998 down_write(&key_types_sem);
1000 /* withdraw the key type */
1001 list_del_init(&ktype->link);
1003 /* mark all the keys of this type dead */
1004 spin_lock(&key_serial_lock);
1006 for (_n = rb_first(&key_serial_tree); _n; _n = rb_next(_n)) {
1007 key = rb_entry(_n, struct key, serial_node);
1009 if (key->type == ktype)
1010 key->type = &key_type_dead;
1013 spin_unlock(&key_serial_lock);
1015 /* make sure everyone revalidates their keys */
1018 /* we should now be able to destroy the payloads of all the keys of
1019 * this type with impunity */
1020 spin_lock(&key_serial_lock);
1022 for (_n = rb_first(&key_serial_tree); _n; _n = rb_next(_n)) {
1023 key = rb_entry(_n, struct key, serial_node);
1025 if (key->type == ktype) {
1027 ktype->destroy(key);
1028 memset(&key->payload, 0xbd, sizeof(key->payload));
1032 spin_unlock(&key_serial_lock);
1033 up_write(&key_types_sem);
1035 } /* end unregister_key_type() */
1037 EXPORT_SYMBOL(unregister_key_type);
1039 /*****************************************************************************/
1041 * initialise the key management stuff
1043 void __init key_init(void)
1045 /* allocate a slab in which we can store keys */
1046 key_jar = kmem_cache_create("key_jar", sizeof(struct key),
1047 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1049 /* add the special key types */
1050 list_add_tail(&key_type_keyring.link, &key_types_list);
1051 list_add_tail(&key_type_dead.link, &key_types_list);
1052 list_add_tail(&key_type_user.link, &key_types_list);
1054 /* record the root user tracking */
1055 rb_link_node(&root_key_user.node,
1057 &key_user_tree.rb_node);
1059 rb_insert_color(&root_key_user.node,
1062 /* record root's user standard keyrings */
1063 key_check(&root_user_keyring);
1064 key_check(&root_session_keyring);
1066 __key_insert_serial(&root_user_keyring);
1067 __key_insert_serial(&root_session_keyring);
1069 keyring_publish_name(&root_user_keyring);
1070 keyring_publish_name(&root_session_keyring);
1072 /* link the two root keyrings together */
1073 key_link(&root_session_keyring, &root_user_keyring);
1075 } /* end key_init() */