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>
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 = {
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);
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);
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;
149 p = &key_serial_tree.rb_node;
153 xkey = rb_entry(parent, struct key, serial_node);
155 if (key->serial < xkey->serial)
157 else if (key->serial > xkey->serial)
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
175 static inline void key_alloc_serial(struct key *key)
177 struct rb_node *parent, **p;
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;
187 key_serial_next = key->serial + 1;
190 p = &key_serial_tree.rb_node;
194 xkey = rb_entry(parent, struct key, serial_node);
196 if (key->serial < xkey->serial)
198 else if (key->serial > xkey->serial)
205 /* we found a key with the proposed serial number - walk the tree from
206 * that point looking for the next unused serial number */
209 key->serial = key_serial_next;
212 key_serial_next = key->serial + 1;
214 if (!parent->rb_parent)
215 p = &key_serial_tree.rb_node;
216 else if (parent->rb_parent->rb_left == parent)
217 p = &parent->rb_parent->rb_left;
219 p = &parent->rb_parent->rb_right;
221 parent = rb_next(parent);
225 xkey = rb_entry(parent, struct key, serial_node);
226 if (key->serial < xkey->serial)
230 /* we've found a suitable hole - arrange for this key to occupy it */
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, key_perm_t perm,
253 struct key_user *user = NULL;
255 size_t desclen, quotalen;
258 key = ERR_PTR(-EINVAL);
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);
270 /* check that the user's quota permits allocation of another key and
273 spin_lock(&user->lock);
274 if (user->qnkeys + 1 >= KEYQUOTA_MAX_KEYS ||
275 user->qnbytes + quotalen >= KEYQUOTA_MAX_BYTES
280 user->qnbytes += quotalen;
281 spin_unlock(&user->lock);
284 /* allocate and initialise the key and its description */
285 key = kmem_cache_alloc(key_jar, SLAB_KERNEL);
290 key->description = kmalloc(desclen, GFP_KERNEL);
291 if (!key->description)
294 memcpy(key->description, desc, desclen);
297 atomic_set(&key->usage, 1);
298 init_rwsem(&key->sem);
301 key->quotalen = quotalen;
302 key->datalen = type->def_datalen;
308 key->payload.data = NULL;
309 key->security = NULL;
312 key->flags |= 1 << KEY_FLAG_IN_QUOTA;
314 memset(&key->type_data, 0, sizeof(key->type_data));
317 key->magic = KEY_DEBUG_MAGIC;
320 /* let the security module know about the key */
321 ret = security_key_alloc(key);
325 /* publish the key by giving it a serial number */
326 atomic_inc(&user->nkeys);
327 key_alloc_serial(key);
333 kfree(key->description);
334 kmem_cache_free(key_jar, key);
336 spin_lock(&user->lock);
338 user->qnbytes -= quotalen;
339 spin_unlock(&user->lock);
346 kmem_cache_free(key_jar, key);
349 spin_lock(&user->lock);
351 user->qnbytes -= quotalen;
352 spin_unlock(&user->lock);
356 key = ERR_PTR(-ENOMEM);
360 spin_unlock(&user->lock);
362 key = ERR_PTR(-EDQUOT);
365 } /* end key_alloc() */
367 EXPORT_SYMBOL(key_alloc);
369 /*****************************************************************************/
371 * reserve an amount of quota for the key's payload
373 int key_payload_reserve(struct key *key, size_t datalen)
375 int delta = (int) datalen - key->datalen;
380 /* contemplate the quota adjustment */
381 if (delta != 0 && test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
382 spin_lock(&key->user->lock);
385 key->user->qnbytes + delta > KEYQUOTA_MAX_BYTES
390 key->user->qnbytes += delta;
391 key->quotalen += delta;
393 spin_unlock(&key->user->lock);
396 /* change the recorded data length if that didn't generate an error */
398 key->datalen = datalen;
402 } /* end key_payload_reserve() */
404 EXPORT_SYMBOL(key_payload_reserve);
406 /*****************************************************************************/
408 * instantiate a key and link it into the target keyring atomically
409 * - called with the target keyring's semaphore writelocked
411 static int __key_instantiate_and_link(struct key *key,
425 down_write(&key_construction_sem);
427 /* can't instantiate twice */
428 if (!test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
429 /* instantiate the key */
430 ret = key->type->instantiate(key, data, datalen);
433 /* mark the key as being instantiated */
434 atomic_inc(&key->user->nikeys);
435 set_bit(KEY_FLAG_INSTANTIATED, &key->flags);
437 if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags))
440 /* and link it into the destination keyring */
442 ret = __key_link(keyring, key);
444 /* disable the authorisation key */
450 up_write(&key_construction_sem);
452 /* wake up anyone waiting for a key to be constructed */
454 wake_up_all(&request_key_conswq);
458 } /* end __key_instantiate_and_link() */
460 /*****************************************************************************/
462 * instantiate a key and link it into the target keyring atomically
464 int key_instantiate_and_link(struct key *key,
473 down_write(&keyring->sem);
475 ret = __key_instantiate_and_link(key, data, datalen, keyring, instkey);
478 up_write(&keyring->sem);
482 } /* end key_instantiate_and_link() */
484 EXPORT_SYMBOL(key_instantiate_and_link);
486 /*****************************************************************************/
488 * negatively instantiate a key and link it into the target keyring atomically
490 int key_negate_and_link(struct key *key,
505 down_write(&keyring->sem);
507 down_write(&key_construction_sem);
509 /* can't instantiate twice */
510 if (!test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
511 /* mark the key as being negatively instantiated */
512 atomic_inc(&key->user->nikeys);
513 set_bit(KEY_FLAG_NEGATIVE, &key->flags);
514 set_bit(KEY_FLAG_INSTANTIATED, &key->flags);
515 now = current_kernel_time();
516 key->expiry = now.tv_sec + timeout;
518 if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags))
523 /* and link it into the destination keyring */
525 ret = __key_link(keyring, key);
527 /* disable the authorisation key */
532 up_write(&key_construction_sem);
535 up_write(&keyring->sem);
537 /* wake up anyone waiting for a key to be constructed */
539 wake_up_all(&request_key_conswq);
543 } /* end key_negate_and_link() */
545 EXPORT_SYMBOL(key_negate_and_link);
547 /*****************************************************************************/
549 * do cleaning up in process context so that we don't have to disable
550 * interrupts all over the place
552 static void key_cleanup(void *data)
558 /* look for a dead key in the tree */
559 spin_lock(&key_serial_lock);
561 for (_n = rb_first(&key_serial_tree); _n; _n = rb_next(_n)) {
562 key = rb_entry(_n, struct key, serial_node);
564 if (atomic_read(&key->usage) == 0)
568 spin_unlock(&key_serial_lock);
572 /* we found a dead key - once we've removed it from the tree, we can
574 rb_erase(&key->serial_node, &key_serial_tree);
575 spin_unlock(&key_serial_lock);
579 security_key_free(key);
581 /* deal with the user's key tracking and quota */
582 if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
583 spin_lock(&key->user->lock);
585 key->user->qnbytes -= key->quotalen;
586 spin_unlock(&key->user->lock);
589 atomic_dec(&key->user->nkeys);
590 if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
591 atomic_dec(&key->user->nikeys);
593 key_user_put(key->user);
595 /* now throw away the key memory */
596 if (key->type->destroy)
597 key->type->destroy(key);
599 kfree(key->description);
602 key->magic = KEY_DEBUG_MAGIC_X;
604 kmem_cache_free(key_jar, key);
606 /* there may, of course, be more than one key to destroy */
609 } /* end key_cleanup() */
611 /*****************************************************************************/
613 * dispose of a reference to a key
614 * - when all the references are gone, we schedule the cleanup task to come and
615 * pull it out of the tree in definite process context
617 void key_put(struct key *key)
622 if (atomic_dec_and_test(&key->usage))
623 schedule_work(&key_cleanup_task);
626 } /* end key_put() */
628 EXPORT_SYMBOL(key_put);
630 /*****************************************************************************/
632 * find a key by its serial number
634 struct key *key_lookup(key_serial_t id)
639 spin_lock(&key_serial_lock);
641 /* search the tree for the specified key */
642 n = key_serial_tree.rb_node;
644 key = rb_entry(n, struct key, serial_node);
646 if (id < key->serial)
648 else if (id > key->serial)
655 key = ERR_PTR(-ENOKEY);
659 /* pretend it doesn't exist if it's dead */
660 if (atomic_read(&key->usage) == 0 ||
661 test_bit(KEY_FLAG_DEAD, &key->flags) ||
662 key->type == &key_type_dead)
665 /* this races with key_put(), but that doesn't matter since key_put()
666 * doesn't actually change the key
668 atomic_inc(&key->usage);
671 spin_unlock(&key_serial_lock);
674 } /* end key_lookup() */
676 /*****************************************************************************/
678 * find and lock the specified key type against removal
679 * - we return with the sem readlocked
681 struct key_type *key_type_lookup(const char *type)
683 struct key_type *ktype;
685 down_read(&key_types_sem);
687 /* look up the key type to see if it's one of the registered kernel
689 list_for_each_entry(ktype, &key_types_list, link) {
690 if (strcmp(ktype->name, type) == 0)
691 goto found_kernel_type;
694 up_read(&key_types_sem);
695 ktype = ERR_PTR(-ENOKEY);
700 } /* end key_type_lookup() */
702 /*****************************************************************************/
706 void key_type_put(struct key_type *ktype)
708 up_read(&key_types_sem);
710 } /* end key_type_put() */
712 /*****************************************************************************/
714 * attempt to update an existing key
715 * - the key has an incremented refcount
716 * - we need to put the key if we get an error
718 static inline key_ref_t __key_update(key_ref_t key_ref,
719 const void *payload, size_t plen)
721 struct key *key = key_ref_to_ptr(key_ref);
724 /* need write permission on the key to update it */
725 ret = key_permission(key_ref, KEY_WRITE);
730 if (!key->type->update)
733 down_write(&key->sem);
735 ret = key->type->update(key, payload, plen);
737 /* updating a negative key instantiates it */
738 clear_bit(KEY_FLAG_NEGATIVE, &key->flags);
749 key_ref = ERR_PTR(ret);
752 } /* end __key_update() */
754 /*****************************************************************************/
756 * search the specified keyring for a key of the same description; if one is
757 * found, update it, otherwise add a new one
759 key_ref_t key_create_or_update(key_ref_t keyring_ref,
761 const char *description,
766 struct key_type *ktype;
767 struct key *keyring, *key = NULL;
772 /* look up the key type to see if it's one of the registered kernel
774 ktype = key_type_lookup(type);
776 key_ref = ERR_PTR(-ENODEV);
780 key_ref = ERR_PTR(-EINVAL);
781 if (!ktype->match || !ktype->instantiate)
784 keyring = key_ref_to_ptr(keyring_ref);
788 key_ref = ERR_PTR(-ENOTDIR);
789 if (keyring->type != &key_type_keyring)
792 down_write(&keyring->sem);
794 /* if we're going to allocate a new key, we're going to have
795 * to modify the keyring */
796 ret = key_permission(keyring_ref, KEY_WRITE);
798 key_ref = ERR_PTR(ret);
802 /* if it's possible to update this type of key, search for an existing
803 * key of the same type and description in the destination keyring and
804 * update that instead if possible
807 key_ref = __keyring_search_one(keyring_ref, ktype, description,
809 if (!IS_ERR(key_ref))
810 goto found_matching_key;
813 /* decide on the permissions we want */
814 perm = KEY_POS_VIEW | KEY_POS_SEARCH | KEY_POS_LINK | KEY_POS_SETATTR;
815 perm |= KEY_USR_VIEW | KEY_USR_SEARCH | KEY_USR_LINK | KEY_USR_SETATTR;
818 perm |= KEY_POS_READ | KEY_USR_READ;
820 if (ktype == &key_type_keyring || ktype->update)
821 perm |= KEY_USR_WRITE;
823 /* allocate a new key */
824 key = key_alloc(ktype, description, current->fsuid, current->fsgid,
827 key_ref = ERR_PTR(PTR_ERR(key));
831 /* instantiate it and link it into the target keyring */
832 ret = __key_instantiate_and_link(key, payload, plen, keyring, NULL);
835 key_ref = ERR_PTR(ret);
839 key_ref = make_key_ref(key, is_key_possessed(keyring_ref));
842 up_write(&keyring->sem);
849 /* we found a matching key, so we're going to try to update it
850 * - we can drop the locks first as we have the key pinned
852 up_write(&keyring->sem);
855 key_ref = __key_update(key_ref, payload, plen);
858 } /* end key_create_or_update() */
860 EXPORT_SYMBOL(key_create_or_update);
862 /*****************************************************************************/
866 int key_update(key_ref_t key_ref, const void *payload, size_t plen)
868 struct key *key = key_ref_to_ptr(key_ref);
873 /* the key must be writable */
874 ret = key_permission(key_ref, KEY_WRITE);
878 /* attempt to update it if supported */
880 if (key->type->update) {
881 down_write(&key->sem);
883 ret = key->type->update(key, payload, plen);
885 /* updating a negative key instantiates it */
886 clear_bit(KEY_FLAG_NEGATIVE, &key->flags);
894 } /* end key_update() */
896 EXPORT_SYMBOL(key_update);
898 /*****************************************************************************/
902 void key_revoke(struct key *key)
906 /* make sure no one's trying to change or use the key when we mark
908 down_write(&key->sem);
909 set_bit(KEY_FLAG_REVOKED, &key->flags);
912 } /* end key_revoke() */
914 EXPORT_SYMBOL(key_revoke);
916 /*****************************************************************************/
918 * register a type of key
920 int register_key_type(struct key_type *ktype)
926 down_write(&key_types_sem);
928 /* disallow key types with the same name */
929 list_for_each_entry(p, &key_types_list, link) {
930 if (strcmp(p->name, ktype->name) == 0)
935 list_add(&ktype->link, &key_types_list);
939 up_write(&key_types_sem);
942 } /* end register_key_type() */
944 EXPORT_SYMBOL(register_key_type);
946 /*****************************************************************************/
948 * unregister a type of key
950 void unregister_key_type(struct key_type *ktype)
955 down_write(&key_types_sem);
957 /* withdraw the key type */
958 list_del_init(&ktype->link);
960 /* mark all the keys of this type dead */
961 spin_lock(&key_serial_lock);
963 for (_n = rb_first(&key_serial_tree); _n; _n = rb_next(_n)) {
964 key = rb_entry(_n, struct key, serial_node);
966 if (key->type == ktype)
967 key->type = &key_type_dead;
970 spin_unlock(&key_serial_lock);
972 /* make sure everyone revalidates their keys */
975 /* we should now be able to destroy the payloads of all the keys of
976 * this type with impunity */
977 spin_lock(&key_serial_lock);
979 for (_n = rb_first(&key_serial_tree); _n; _n = rb_next(_n)) {
980 key = rb_entry(_n, struct key, serial_node);
982 if (key->type == ktype) {
985 memset(&key->payload, 0xbd, sizeof(key->payload));
989 spin_unlock(&key_serial_lock);
990 up_write(&key_types_sem);
992 } /* end unregister_key_type() */
994 EXPORT_SYMBOL(unregister_key_type);
996 /*****************************************************************************/
998 * initialise the key management stuff
1000 void __init key_init(void)
1002 /* allocate a slab in which we can store keys */
1003 key_jar = kmem_cache_create("key_jar", sizeof(struct key),
1004 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1006 /* add the special key types */
1007 list_add_tail(&key_type_keyring.link, &key_types_list);
1008 list_add_tail(&key_type_dead.link, &key_types_list);
1009 list_add_tail(&key_type_user.link, &key_types_list);
1011 /* record the root user tracking */
1012 rb_link_node(&root_key_user.node,
1014 &key_user_tree.rb_node);
1016 rb_insert_color(&root_key_user.node,
1019 /* record root's user standard keyrings */
1020 key_check(&root_user_keyring);
1021 key_check(&root_session_keyring);
1023 __key_insert_serial(&root_user_keyring);
1024 __key_insert_serial(&root_session_keyring);
1026 keyring_publish_name(&root_user_keyring);
1027 keyring_publish_name(&root_session_keyring);
1029 /* link the two root keyrings together */
1030 key_link(&root_session_keyring, &root_user_keyring);
1032 } /* end key_init() */