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/random.h>
19 #include <linux/err.h>
22 static kmem_cache_t *key_jar;
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 * - these are assigned randomly to avoid security issues through covert
175 static inline void key_alloc_serial(struct key *key)
177 struct rb_node *parent, **p;
180 /* propose a random serial number and look for a hole for it in the
181 * serial number tree */
183 get_random_bytes(&key->serial, sizeof(key->serial));
185 key->serial >>= 1; /* negative numbers are not permitted */
186 } while (key->serial < 3);
188 spin_lock(&key_serial_lock);
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 */
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);
219 p = &(rb_parent(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, struct task_struct *ctx,
251 key_perm_t perm, unsigned long flags)
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
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
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);
292 key->description = kmalloc(desclen, GFP_KERNEL);
293 if (!key->description)
296 memcpy(key->description, desc, desclen);
299 atomic_set(&key->usage, 1);
300 init_rwsem(&key->sem);
303 key->quotalen = quotalen;
304 key->datalen = type->def_datalen;
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));
319 key->magic = KEY_DEBUG_MAGIC;
322 /* let the security module know about the key */
323 ret = security_key_alloc(key, ctx, flags);
327 /* publish the key by giving it a serial number */
328 atomic_inc(&user->nkeys);
329 key_alloc_serial(key);
335 kfree(key->description);
336 kmem_cache_free(key_jar, key);
337 if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) {
338 spin_lock(&user->lock);
340 user->qnbytes -= quotalen;
341 spin_unlock(&user->lock);
348 kmem_cache_free(key_jar, key);
350 if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) {
351 spin_lock(&user->lock);
353 user->qnbytes -= quotalen;
354 spin_unlock(&user->lock);
358 key = ERR_PTR(-ENOMEM);
362 spin_unlock(&user->lock);
364 key = ERR_PTR(-EDQUOT);
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;
382 /* contemplate the quota adjustment */
383 if (delta != 0 && test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
384 spin_lock(&key->user->lock);
387 key->user->qnbytes + delta > KEYQUOTA_MAX_BYTES
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 */
400 key->datalen = datalen;
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,
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);
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))
442 /* and link it into the destination keyring */
444 ret = __key_link(keyring, key);
446 /* disable the authorisation key */
452 up_write(&key_construction_sem);
454 /* wake up anyone waiting for a key to be constructed */
456 wake_up_all(&request_key_conswq);
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,
475 down_write(&keyring->sem);
477 ret = __key_instantiate_and_link(key, data, datalen, keyring, instkey);
480 up_write(&keyring->sem);
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,
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))
525 /* and link it into the destination keyring */
527 ret = __key_link(keyring, key);
529 /* disable the authorisation key */
534 up_write(&key_construction_sem);
537 up_write(&keyring->sem);
539 /* wake up anyone waiting for a key to be constructed */
541 wake_up_all(&request_key_conswq);
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)
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)
570 spin_unlock(&key_serial_lock);
574 /* we found a dead key - once we've removed it from the tree, we can
576 rb_erase(&key->serial_node, &key_serial_tree);
577 spin_unlock(&key_serial_lock);
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);
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);
604 key->magic = KEY_DEBUG_MAGIC_X;
606 kmem_cache_free(key_jar, key);
608 /* there may, of course, be more than one key to destroy */
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)
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)
641 spin_lock(&key_serial_lock);
643 /* search the tree for the specified key */
644 n = key_serial_tree.rb_node;
646 key = rb_entry(n, struct key, serial_node);
648 if (id < key->serial)
650 else if (id > key->serial)
657 key = ERR_PTR(-ENOKEY);
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)
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);
673 spin_unlock(&key_serial_lock);
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
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);
702 } /* end key_type_lookup() */
704 /*****************************************************************************/
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);
726 /* need write permission on the key to update it */
727 ret = key_permission(key_ref, KEY_WRITE);
732 if (!key->type->update)
735 down_write(&key->sem);
737 ret = key->type->update(key, payload, plen);
739 /* updating a negative key instantiates it */
740 clear_bit(KEY_FLAG_NEGATIVE, &key->flags);
751 key_ref = ERR_PTR(ret);
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,
763 const char *description,
768 struct key_type *ktype;
769 struct key *keyring, *key = NULL;
774 /* look up the key type to see if it's one of the registered kernel
776 ktype = key_type_lookup(type);
778 key_ref = ERR_PTR(-ENODEV);
782 key_ref = ERR_PTR(-EINVAL);
783 if (!ktype->match || !ktype->instantiate)
786 keyring = key_ref_to_ptr(keyring_ref);
790 key_ref = ERR_PTR(-ENOTDIR);
791 if (keyring->type != &key_type_keyring)
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);
800 key_ref = ERR_PTR(ret);
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
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;
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);
829 key_ref = ERR_PTR(PTR_ERR(key));
833 /* instantiate it and link it into the target keyring */
834 ret = __key_instantiate_and_link(key, payload, plen, keyring, NULL);
837 key_ref = ERR_PTR(ret);
841 key_ref = make_key_ref(key, is_key_possessed(keyring_ref));
844 up_write(&keyring->sem);
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);
857 key_ref = __key_update(key_ref, payload, plen);
860 } /* end key_create_or_update() */
862 EXPORT_SYMBOL(key_create_or_update);
864 /*****************************************************************************/
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);
875 /* the key must be writable */
876 ret = key_permission(key_ref, KEY_WRITE);
880 /* attempt to update it if supported */
882 if (key->type->update) {
883 down_write(&key->sem);
885 ret = key->type->update(key, payload, plen);
887 /* updating a negative key instantiates it */
888 clear_bit(KEY_FLAG_NEGATIVE, &key->flags);
896 } /* end key_update() */
898 EXPORT_SYMBOL(key_update);
900 /*****************************************************************************/
904 void key_revoke(struct key *key)
908 /* make sure no one's trying to change or use the key when we mark
910 down_write(&key->sem);
911 set_bit(KEY_FLAG_REVOKED, &key->flags);
913 if (key->type->revoke)
914 key->type->revoke(key);
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)
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)
941 list_add(&ktype->link, &key_types_list);
945 up_write(&key_types_sem);
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)
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 */
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) {
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,
1020 &key_user_tree.rb_node);
1022 rb_insert_color(&root_key_user.node,
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() */