aboutsummaryrefslogtreecommitdiffstats
path: root/security/keys/key.c
diff options
context:
space:
mode:
authorDavid Howells <dhowells@redhat.com>2011-01-20 11:38:33 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2011-01-21 17:59:30 -0500
commit973c9f4f49ca96a53bcf6384c4c59ccd26c33906 (patch)
treee3535a43c1e5cb5f0c06c040f58bc25c9b869fd1 /security/keys/key.c
parenta8b17ed019bd40d3bfa20439d9c36a99f9be9180 (diff)
KEYS: Fix up comments in key management code
Fix up comments in the key management code. No functional changes. Signed-off-by: David Howells <dhowells@redhat.com> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'security/keys/key.c')
-rw-r--r--security/keys/key.c240
1 files changed, 173 insertions, 67 deletions
diff --git a/security/keys/key.c b/security/keys/key.c
index 3a92d7365870..84d4eb568b08 100644
--- a/security/keys/key.c
+++ b/security/keys/key.c
@@ -39,10 +39,10 @@ static DECLARE_RWSEM(key_types_sem);
39static void key_cleanup(struct work_struct *work); 39static void key_cleanup(struct work_struct *work);
40static DECLARE_WORK(key_cleanup_task, key_cleanup); 40static DECLARE_WORK(key_cleanup_task, key_cleanup);
41 41
42/* we serialise key instantiation and link */ 42/* We serialise key instantiation and link */
43DEFINE_MUTEX(key_construction_mutex); 43DEFINE_MUTEX(key_construction_mutex);
44 44
45/* any key who's type gets unegistered will be re-typed to this */ 45/* Any key who's type gets unegistered will be re-typed to this */
46static struct key_type key_type_dead = { 46static struct key_type key_type_dead = {
47 .name = "dead", 47 .name = "dead",
48}; 48};
@@ -57,8 +57,8 @@ void __key_check(const struct key *key)
57#endif 57#endif
58 58
59/* 59/*
60 * get the key quota record for a user, allocating a new record if one doesn't 60 * Get the key quota record for a user, allocating a new record if one doesn't
61 * already exist 61 * already exist.
62 */ 62 */
63struct key_user *key_user_lookup(uid_t uid, struct user_namespace *user_ns) 63struct key_user *key_user_lookup(uid_t uid, struct user_namespace *user_ns)
64{ 64{
@@ -66,7 +66,7 @@ struct key_user *key_user_lookup(uid_t uid, struct user_namespace *user_ns)
66 struct rb_node *parent = NULL; 66 struct rb_node *parent = NULL;
67 struct rb_node **p; 67 struct rb_node **p;
68 68
69 try_again: 69try_again:
70 p = &key_user_tree.rb_node; 70 p = &key_user_tree.rb_node;
71 spin_lock(&key_user_lock); 71 spin_lock(&key_user_lock);
72 72
@@ -123,16 +123,16 @@ struct key_user *key_user_lookup(uid_t uid, struct user_namespace *user_ns)
123 goto out; 123 goto out;
124 124
125 /* okay - we found a user record for this UID */ 125 /* okay - we found a user record for this UID */
126 found: 126found:
127 atomic_inc(&user->usage); 127 atomic_inc(&user->usage);
128 spin_unlock(&key_user_lock); 128 spin_unlock(&key_user_lock);
129 kfree(candidate); 129 kfree(candidate);
130 out: 130out:
131 return user; 131 return user;
132} 132}
133 133
134/* 134/*
135 * dispose of a user structure 135 * Dispose of a user structure
136 */ 136 */
137void key_user_put(struct key_user *user) 137void key_user_put(struct key_user *user)
138{ 138{
@@ -146,9 +146,8 @@ void key_user_put(struct key_user *user)
146} 146}
147 147
148/* 148/*
149 * assign a key the next unique serial number 149 * Allocate a serial number for a key. These are assigned randomly to avoid
150 * - these are assigned randomly to avoid security issues through covert 150 * security issues through covert channel problems.
151 * channel problems
152 */ 151 */
153static inline void key_alloc_serial(struct key *key) 152static inline void key_alloc_serial(struct key *key)
154{ 153{
@@ -208,14 +207,34 @@ serial_exists:
208 } 207 }
209} 208}
210 209
211/* 210/**
212 * allocate a key of the specified type 211 * key_alloc - Allocate a key of the specified type.
213 * - update the user's quota to reflect the existence of the key 212 * @type: The type of key to allocate.
214 * - called from a key-type operation with key_types_sem read-locked by 213 * @desc: The key description to allow the key to be searched out.
215 * key_create_or_update() 214 * @uid: The owner of the new key.
216 * - this prevents unregistration of the key type 215 * @gid: The group ID for the new key's group permissions.
217 * - upon return the key is as yet uninstantiated; the caller needs to either 216 * @cred: The credentials specifying UID namespace.
218 * instantiate the key or discard it before returning 217 * @perm: The permissions mask of the new key.
218 * @flags: Flags specifying quota properties.
219 *
220 * Allocate a key of the specified type with the attributes given. The key is
221 * returned in an uninstantiated state and the caller needs to instantiate the
222 * key before returning.
223 *
224 * The user's key count quota is updated to reflect the creation of the key and
225 * the user's key data quota has the default for the key type reserved. The
226 * instantiation function should amend this as necessary. If insufficient
227 * quota is available, -EDQUOT will be returned.
228 *
229 * The LSM security modules can prevent a key being created, in which case
230 * -EACCES will be returned.
231 *
232 * Returns a pointer to the new key if successful and an error code otherwise.
233 *
234 * Note that the caller needs to ensure the key type isn't uninstantiated.
235 * Internally this can be done by locking key_types_sem. Externally, this can
236 * be done by either never unregistering the key type, or making sure
237 * key_alloc() calls don't race with module unloading.
219 */ 238 */
220struct key *key_alloc(struct key_type *type, const char *desc, 239struct key *key_alloc(struct key_type *type, const char *desc,
221 uid_t uid, gid_t gid, const struct cred *cred, 240 uid_t uid, gid_t gid, const struct cred *cred,
@@ -338,11 +357,18 @@ no_quota:
338 key = ERR_PTR(-EDQUOT); 357 key = ERR_PTR(-EDQUOT);
339 goto error; 358 goto error;
340} 359}
341
342EXPORT_SYMBOL(key_alloc); 360EXPORT_SYMBOL(key_alloc);
343 361
344/* 362/**
345 * reserve an amount of quota for the key's payload 363 * key_payload_reserve - Adjust data quota reservation for the key's payload
364 * @key: The key to make the reservation for.
365 * @datalen: The amount of data payload the caller now wants.
366 *
367 * Adjust the amount of the owning user's key data quota that a key reserves.
368 * If the amount is increased, then -EDQUOT may be returned if there isn't
369 * enough free quota available.
370 *
371 * If successful, 0 is returned.
346 */ 372 */
347int key_payload_reserve(struct key *key, size_t datalen) 373int key_payload_reserve(struct key *key, size_t datalen)
348{ 374{
@@ -376,12 +402,13 @@ int key_payload_reserve(struct key *key, size_t datalen)
376 402
377 return ret; 403 return ret;
378} 404}
379
380EXPORT_SYMBOL(key_payload_reserve); 405EXPORT_SYMBOL(key_payload_reserve);
381 406
382/* 407/*
383 * instantiate a key and link it into the target keyring atomically 408 * Instantiate a key and link it into the target keyring atomically. Must be
384 * - called with the target keyring's semaphore writelocked 409 * called with the target keyring's semaphore writelocked. The target key's
410 * semaphore need not be locked as instantiation is serialised by
411 * key_construction_mutex.
385 */ 412 */
386static int __key_instantiate_and_link(struct key *key, 413static int __key_instantiate_and_link(struct key *key,
387 const void *data, 414 const void *data,
@@ -432,8 +459,21 @@ static int __key_instantiate_and_link(struct key *key,
432 return ret; 459 return ret;
433} 460}
434 461
435/* 462/**
436 * instantiate a key and link it into the target keyring atomically 463 * key_instantiate_and_link - Instantiate a key and link it into the keyring.
464 * @key: The key to instantiate.
465 * @data: The data to use to instantiate the keyring.
466 * @datalen: The length of @data.
467 * @keyring: Keyring to create a link in on success (or NULL).
468 * @authkey: The authorisation token permitting instantiation.
469 *
470 * Instantiate a key that's in the uninstantiated state using the provided data
471 * and, if successful, link it in to the destination keyring if one is
472 * supplied.
473 *
474 * If successful, 0 is returned, the authorisation token is revoked and anyone
475 * waiting for the key is woken up. If the key was already instantiated,
476 * -EBUSY will be returned.
437 */ 477 */
438int key_instantiate_and_link(struct key *key, 478int key_instantiate_and_link(struct key *key,
439 const void *data, 479 const void *data,
@@ -462,8 +502,24 @@ int key_instantiate_and_link(struct key *key,
462 502
463EXPORT_SYMBOL(key_instantiate_and_link); 503EXPORT_SYMBOL(key_instantiate_and_link);
464 504
465/* 505/**
466 * negatively instantiate a key and link it into the target keyring atomically 506 * key_negate_and_link - Negatively instantiate a key and link it into the keyring.
507 * @key: The key to instantiate.
508 * @timeout: The timeout on the negative key.
509 * @keyring: Keyring to create a link in on success (or NULL).
510 * @authkey: The authorisation token permitting instantiation.
511 *
512 * Negatively instantiate a key that's in the uninstantiated state and, if
513 * successful, set its timeout and link it in to the destination keyring if one
514 * is supplied. The key and any links to the key will be automatically garbage
515 * collected after the timeout expires.
516 *
517 * Negative keys are used to rate limit repeated request_key() calls by causing
518 * them to return -ENOKEY until the negative key expires.
519 *
520 * If successful, 0 is returned, the authorisation token is revoked and anyone
521 * waiting for the key is woken up. If the key was already instantiated,
522 * -EBUSY will be returned.
467 */ 523 */
468int key_negate_and_link(struct key *key, 524int key_negate_and_link(struct key *key,
469 unsigned timeout, 525 unsigned timeout,
@@ -525,15 +581,18 @@ int key_negate_and_link(struct key *key,
525EXPORT_SYMBOL(key_negate_and_link); 581EXPORT_SYMBOL(key_negate_and_link);
526 582
527/* 583/*
528 * do cleaning up in process context so that we don't have to disable 584 * Garbage collect keys in process context so that we don't have to disable
529 * interrupts all over the place 585 * interrupts all over the place.
586 *
587 * key_put() schedules this rather than trying to do the cleanup itself, which
588 * means key_put() doesn't have to sleep.
530 */ 589 */
531static void key_cleanup(struct work_struct *work) 590static void key_cleanup(struct work_struct *work)
532{ 591{
533 struct rb_node *_n; 592 struct rb_node *_n;
534 struct key *key; 593 struct key *key;
535 594
536 go_again: 595go_again:
537 /* look for a dead key in the tree */ 596 /* look for a dead key in the tree */
538 spin_lock(&key_serial_lock); 597 spin_lock(&key_serial_lock);
539 598
@@ -547,7 +606,7 @@ static void key_cleanup(struct work_struct *work)
547 spin_unlock(&key_serial_lock); 606 spin_unlock(&key_serial_lock);
548 return; 607 return;
549 608
550 found_dead_key: 609found_dead_key:
551 /* we found a dead key - once we've removed it from the tree, we can 610 /* we found a dead key - once we've removed it from the tree, we can
552 * drop the lock */ 611 * drop the lock */
553 rb_erase(&key->serial_node, &key_serial_tree); 612 rb_erase(&key->serial_node, &key_serial_tree);
@@ -586,10 +645,13 @@ static void key_cleanup(struct work_struct *work)
586 goto go_again; 645 goto go_again;
587} 646}
588 647
589/* 648/**
590 * dispose of a reference to a key 649 * key_put - Discard a reference to a key.
591 * - when all the references are gone, we schedule the cleanup task to come and 650 * @key: The key to discard a reference from.
592 * pull it out of the tree in definite process context 651 *
652 * Discard a reference to a key, and when all the references are gone, we
653 * schedule the cleanup task to come and pull it out of the tree in process
654 * context at some later time.
593 */ 655 */
594void key_put(struct key *key) 656void key_put(struct key *key)
595{ 657{
@@ -600,11 +662,10 @@ void key_put(struct key *key)
600 schedule_work(&key_cleanup_task); 662 schedule_work(&key_cleanup_task);
601 } 663 }
602} 664}
603
604EXPORT_SYMBOL(key_put); 665EXPORT_SYMBOL(key_put);
605 666
606/* 667/*
607 * find a key by its serial number 668 * Find a key by its serial number.
608 */ 669 */
609struct key *key_lookup(key_serial_t id) 670struct key *key_lookup(key_serial_t id)
610{ 671{
@@ -626,11 +687,11 @@ struct key *key_lookup(key_serial_t id)
626 goto found; 687 goto found;
627 } 688 }
628 689
629 not_found: 690not_found:
630 key = ERR_PTR(-ENOKEY); 691 key = ERR_PTR(-ENOKEY);
631 goto error; 692 goto error;
632 693
633 found: 694found:
634 /* pretend it doesn't exist if it is awaiting deletion */ 695 /* pretend it doesn't exist if it is awaiting deletion */
635 if (atomic_read(&key->usage) == 0) 696 if (atomic_read(&key->usage) == 0)
636 goto not_found; 697 goto not_found;
@@ -640,14 +701,16 @@ struct key *key_lookup(key_serial_t id)
640 */ 701 */
641 atomic_inc(&key->usage); 702 atomic_inc(&key->usage);
642 703
643 error: 704error:
644 spin_unlock(&key_serial_lock); 705 spin_unlock(&key_serial_lock);
645 return key; 706 return key;
646} 707}
647 708
648/* 709/*
649 * find and lock the specified key type against removal 710 * Find and lock the specified key type against removal.
650 * - we return with the sem readlocked 711 *
712 * We return with the sem read-locked if successful. If the type wasn't
713 * available -ENOKEY is returned instead.
651 */ 714 */
652struct key_type *key_type_lookup(const char *type) 715struct key_type *key_type_lookup(const char *type)
653{ 716{
@@ -665,12 +728,12 @@ struct key_type *key_type_lookup(const char *type)
665 up_read(&key_types_sem); 728 up_read(&key_types_sem);
666 ktype = ERR_PTR(-ENOKEY); 729 ktype = ERR_PTR(-ENOKEY);
667 730
668 found_kernel_type: 731found_kernel_type:
669 return ktype; 732 return ktype;
670} 733}
671 734
672/* 735/*
673 * unlock a key type 736 * Unlock a key type locked by key_type_lookup().
674 */ 737 */
675void key_type_put(struct key_type *ktype) 738void key_type_put(struct key_type *ktype)
676{ 739{
@@ -678,9 +741,10 @@ void key_type_put(struct key_type *ktype)
678} 741}
679 742
680/* 743/*
681 * attempt to update an existing key 744 * Attempt to update an existing key.
682 * - the key has an incremented refcount 745 *
683 * - we need to put the key if we get an error 746 * The key is given to us with an incremented refcount that we need to discard
747 * if we get an error.
684 */ 748 */
685static inline key_ref_t __key_update(key_ref_t key_ref, 749static inline key_ref_t __key_update(key_ref_t key_ref,
686 const void *payload, size_t plen) 750 const void *payload, size_t plen)
@@ -717,9 +781,30 @@ error:
717 goto out; 781 goto out;
718} 782}
719 783
720/* 784/**
721 * search the specified keyring for a key of the same description; if one is 785 * key_create_or_update - Update or create and instantiate a key.
722 * found, update it, otherwise add a new one 786 * @keyring_ref: A pointer to the destination keyring with possession flag.
787 * @type: The type of key.
788 * @description: The searchable description for the key.
789 * @payload: The data to use to instantiate or update the key.
790 * @plen: The length of @payload.
791 * @perm: The permissions mask for a new key.
792 * @flags: The quota flags for a new key.
793 *
794 * Search the destination keyring for a key of the same description and if one
795 * is found, update it, otherwise create and instantiate a new one and create a
796 * link to it from that keyring.
797 *
798 * If perm is KEY_PERM_UNDEF then an appropriate key permissions mask will be
799 * concocted.
800 *
801 * Returns a pointer to the new key if successful, -ENODEV if the key type
802 * wasn't available, -ENOTDIR if the keyring wasn't a keyring, -EACCES if the
803 * caller isn't permitted to modify the keyring or the LSM did not permit
804 * creation of the key.
805 *
806 * On success, the possession flag from the keyring ref will be tacked on to
807 * the key ref before it is returned.
723 */ 808 */
724key_ref_t key_create_or_update(key_ref_t keyring_ref, 809key_ref_t key_create_or_update(key_ref_t keyring_ref,
725 const char *type, 810 const char *type,
@@ -827,11 +912,20 @@ key_ref_t key_create_or_update(key_ref_t keyring_ref,
827 key_ref = __key_update(key_ref, payload, plen); 912 key_ref = __key_update(key_ref, payload, plen);
828 goto error; 913 goto error;
829} 914}
830
831EXPORT_SYMBOL(key_create_or_update); 915EXPORT_SYMBOL(key_create_or_update);
832 916
833/* 917/**
834 * update a key 918 * key_update - Update a key's contents.
919 * @key_ref: The pointer (plus possession flag) to the key.
920 * @payload: The data to be used to update the key.
921 * @plen: The length of @payload.
922 *
923 * Attempt to update the contents of a key with the given payload data. The
924 * caller must be granted Write permission on the key. Negative keys can be
925 * instantiated by this method.
926 *
927 * Returns 0 on success, -EACCES if not permitted and -EOPNOTSUPP if the key
928 * type does not support updating. The key type may return other errors.
835 */ 929 */
836int key_update(key_ref_t key_ref, const void *payload, size_t plen) 930int key_update(key_ref_t key_ref, const void *payload, size_t plen)
837{ 931{
@@ -861,11 +955,16 @@ int key_update(key_ref_t key_ref, const void *payload, size_t plen)
861 error: 955 error:
862 return ret; 956 return ret;
863} 957}
864
865EXPORT_SYMBOL(key_update); 958EXPORT_SYMBOL(key_update);
866 959
867/* 960/**
868 * revoke a key 961 * key_revoke - Revoke a key.
962 * @key: The key to be revoked.
963 *
964 * Mark a key as being revoked and ask the type to free up its resources. The
965 * revocation timeout is set and the key and all its links will be
966 * automatically garbage collected after key_gc_delay amount of time if they
967 * are not manually dealt with first.
869 */ 968 */
870void key_revoke(struct key *key) 969void key_revoke(struct key *key)
871{ 970{
@@ -894,11 +993,15 @@ void key_revoke(struct key *key)
894 993
895 up_write(&key->sem); 994 up_write(&key->sem);
896} 995}
897
898EXPORT_SYMBOL(key_revoke); 996EXPORT_SYMBOL(key_revoke);
899 997
900/* 998/**
901 * register a type of key 999 * register_key_type - Register a type of key.
1000 * @ktype: The new key type.
1001 *
1002 * Register a new key type.
1003 *
1004 * Returns 0 on success or -EEXIST if a type of this name already exists.
902 */ 1005 */
903int register_key_type(struct key_type *ktype) 1006int register_key_type(struct key_type *ktype)
904{ 1007{
@@ -918,15 +1021,19 @@ int register_key_type(struct key_type *ktype)
918 list_add(&ktype->link, &key_types_list); 1021 list_add(&ktype->link, &key_types_list);
919 ret = 0; 1022 ret = 0;
920 1023
921 out: 1024out:
922 up_write(&key_types_sem); 1025 up_write(&key_types_sem);
923 return ret; 1026 return ret;
924} 1027}
925
926EXPORT_SYMBOL(register_key_type); 1028EXPORT_SYMBOL(register_key_type);
927 1029
928/* 1030/**
929 * unregister a type of key 1031 * unregister_key_type - Unregister a type of key.
1032 * @ktype: The key type.
1033 *
1034 * Unregister a key type and mark all the extant keys of this type as dead.
1035 * Those keys of this type are then destroyed to get rid of their payloads and
1036 * they and their links will be garbage collected as soon as possible.
930 */ 1037 */
931void unregister_key_type(struct key_type *ktype) 1038void unregister_key_type(struct key_type *ktype)
932{ 1039{
@@ -974,11 +1081,10 @@ void unregister_key_type(struct key_type *ktype)
974 1081
975 key_schedule_gc(0); 1082 key_schedule_gc(0);
976} 1083}
977
978EXPORT_SYMBOL(unregister_key_type); 1084EXPORT_SYMBOL(unregister_key_type);
979 1085
980/* 1086/*
981 * initialise the key management stuff 1087 * Initialise the key management state.
982 */ 1088 */
983void __init key_init(void) 1089void __init key_init(void)
984{ 1090{