aboutsummaryrefslogtreecommitdiffstats
path: root/security/keys/request_key.c
diff options
context:
space:
mode:
authorGlenn Elliott <gelliott@cs.unc.edu>2012-03-04 19:47:13 -0500
committerGlenn Elliott <gelliott@cs.unc.edu>2012-03-04 19:47:13 -0500
commitc71c03bda1e86c9d5198c5d83f712e695c4f2a1e (patch)
treeecb166cb3e2b7e2adb3b5e292245fefd23381ac8 /security/keys/request_key.c
parentea53c912f8a86a8567697115b6a0d8152beee5c8 (diff)
parent6a00f206debf8a5c8899055726ad127dbeeed098 (diff)
Merge branch 'mpi-master' into wip-k-fmlpwip-k-fmlp
Conflicts: litmus/sched_cedf.c
Diffstat (limited to 'security/keys/request_key.c')
-rw-r--r--security/keys/request_key.c180
1 files changed, 128 insertions, 52 deletions
diff --git a/security/keys/request_key.c b/security/keys/request_key.c
index 0088dd8bf68a..82465328c39b 100644
--- a/security/keys/request_key.c
+++ b/security/keys/request_key.c
@@ -8,7 +8,7 @@
8 * as published by the Free Software Foundation; either version 8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version. 9 * 2 of the License, or (at your option) any later version.
10 * 10 *
11 * See Documentation/keys-request-key.txt 11 * See Documentation/security/keys-request-key.txt
12 */ 12 */
13 13
14#include <linux/module.h> 14#include <linux/module.h>
@@ -39,8 +39,14 @@ static int key_wait_bit_intr(void *flags)
39 return signal_pending(current) ? -ERESTARTSYS : 0; 39 return signal_pending(current) ? -ERESTARTSYS : 0;
40} 40}
41 41
42/* 42/**
43 * call to complete the construction of a key 43 * complete_request_key - Complete the construction of a key.
44 * @cons: The key construction record.
45 * @error: The success or failute of the construction.
46 *
47 * Complete the attempt to construct a key. The key will be negated
48 * if an error is indicated. The authorisation key will be revoked
49 * unconditionally.
44 */ 50 */
45void complete_request_key(struct key_construction *cons, int error) 51void complete_request_key(struct key_construction *cons, int error)
46{ 52{
@@ -58,23 +64,32 @@ void complete_request_key(struct key_construction *cons, int error)
58} 64}
59EXPORT_SYMBOL(complete_request_key); 65EXPORT_SYMBOL(complete_request_key);
60 66
61static int umh_keys_init(struct subprocess_info *info) 67/*
68 * Initialise a usermode helper that is going to have a specific session
69 * keyring.
70 *
71 * This is called in context of freshly forked kthread before kernel_execve(),
72 * so we can simply install the desired session_keyring at this point.
73 */
74static int umh_keys_init(struct subprocess_info *info, struct cred *cred)
62{ 75{
63 struct cred *cred = (struct cred*)current_cred();
64 struct key *keyring = info->data; 76 struct key *keyring = info->data;
65 /* 77
66 * This is called in context of freshly forked kthread before
67 * kernel_execve(), we can just change our ->session_keyring.
68 */
69 return install_session_keyring_to_cred(cred, keyring); 78 return install_session_keyring_to_cred(cred, keyring);
70} 79}
71 80
81/*
82 * Clean up a usermode helper with session keyring.
83 */
72static void umh_keys_cleanup(struct subprocess_info *info) 84static void umh_keys_cleanup(struct subprocess_info *info)
73{ 85{
74 struct key *keyring = info->data; 86 struct key *keyring = info->data;
75 key_put(keyring); 87 key_put(keyring);
76} 88}
77 89
90/*
91 * Call a usermode helper with a specific session keyring.
92 */
78static int call_usermodehelper_keys(char *path, char **argv, char **envp, 93static int call_usermodehelper_keys(char *path, char **argv, char **envp,
79 struct key *session_keyring, enum umh_wait wait) 94 struct key *session_keyring, enum umh_wait wait)
80{ 95{
@@ -91,7 +106,7 @@ static int call_usermodehelper_keys(char *path, char **argv, char **envp,
91} 106}
92 107
93/* 108/*
94 * request userspace finish the construction of a key 109 * Request userspace finish the construction of a key
95 * - execute "/sbin/request-key <op> <key> <uid> <gid> <keyring> <keyring> <keyring>" 110 * - execute "/sbin/request-key <op> <key> <uid> <gid> <keyring> <keyring> <keyring>"
96 */ 111 */
97static int call_sbin_request_key(struct key_construction *cons, 112static int call_sbin_request_key(struct key_construction *cons,
@@ -198,8 +213,9 @@ error_alloc:
198} 213}
199 214
200/* 215/*
201 * call out to userspace for key construction 216 * Call out to userspace for key construction.
202 * - we ignore program failure and go on key status instead 217 *
218 * Program failure is ignored in favour of key status.
203 */ 219 */
204static int construct_key(struct key *key, const void *callout_info, 220static int construct_key(struct key *key, const void *callout_info,
205 size_t callout_len, void *aux, 221 size_t callout_len, void *aux,
@@ -246,9 +262,10 @@ static int construct_key(struct key *key, const void *callout_info,
246} 262}
247 263
248/* 264/*
249 * get the appropriate destination keyring for the request 265 * Get the appropriate destination keyring for the request.
250 * - we return whatever keyring we select with an extra reference upon it which 266 *
251 * the caller must release 267 * The keyring selected is returned with an extra reference upon it which the
268 * caller must release.
252 */ 269 */
253static void construct_get_dest_keyring(struct key **_dest_keyring) 270static void construct_get_dest_keyring(struct key **_dest_keyring)
254{ 271{
@@ -321,9 +338,11 @@ static void construct_get_dest_keyring(struct key **_dest_keyring)
321} 338}
322 339
323/* 340/*
324 * allocate a new key in under-construction state and attempt to link it in to 341 * Allocate a new key in under-construction state and attempt to link it in to
325 * the requested place 342 * the requested keyring.
326 * - may return a key that's already under construction instead 343 *
344 * May return a key that's already under construction instead if there was a
345 * race between two thread calling request_key().
327 */ 346 */
328static int construct_alloc_key(struct key_type *type, 347static int construct_alloc_key(struct key_type *type,
329 const char *description, 348 const char *description,
@@ -332,8 +351,8 @@ static int construct_alloc_key(struct key_type *type,
332 struct key_user *user, 351 struct key_user *user,
333 struct key **_key) 352 struct key **_key)
334{ 353{
335 struct keyring_list *prealloc;
336 const struct cred *cred = current_cred(); 354 const struct cred *cred = current_cred();
355 unsigned long prealloc;
337 struct key *key; 356 struct key *key;
338 key_ref_t key_ref; 357 key_ref_t key_ref;
339 int ret; 358 int ret;
@@ -403,7 +422,6 @@ link_check_failed:
403 return ret; 422 return ret;
404 423
405link_prealloc_failed: 424link_prealloc_failed:
406 up_write(&dest_keyring->sem);
407 mutex_unlock(&user->cons_lock); 425 mutex_unlock(&user->cons_lock);
408 kleave(" = %d [prelink]", ret); 426 kleave(" = %d [prelink]", ret);
409 return ret; 427 return ret;
@@ -415,7 +433,7 @@ alloc_failed:
415} 433}
416 434
417/* 435/*
418 * commence key construction 436 * Commence key construction.
419 */ 437 */
420static struct key *construct_key_and_link(struct key_type *type, 438static struct key *construct_key_and_link(struct key_type *type,
421 const char *description, 439 const char *description,
@@ -451,7 +469,7 @@ static struct key *construct_key_and_link(struct key_type *type,
451 } else if (ret == -EINPROGRESS) { 469 } else if (ret == -EINPROGRESS) {
452 ret = 0; 470 ret = 0;
453 } else { 471 } else {
454 key = ERR_PTR(ret); 472 goto couldnt_alloc_key;
455 } 473 }
456 474
457 key_put(dest_keyring); 475 key_put(dest_keyring);
@@ -461,17 +479,38 @@ static struct key *construct_key_and_link(struct key_type *type,
461construction_failed: 479construction_failed:
462 key_negate_and_link(key, key_negative_timeout, NULL, NULL); 480 key_negate_and_link(key, key_negative_timeout, NULL, NULL);
463 key_put(key); 481 key_put(key);
482couldnt_alloc_key:
464 key_put(dest_keyring); 483 key_put(dest_keyring);
465 kleave(" = %d", ret); 484 kleave(" = %d", ret);
466 return ERR_PTR(ret); 485 return ERR_PTR(ret);
467} 486}
468 487
469/* 488/**
470 * request a key 489 * request_key_and_link - Request a key and cache it in a keyring.
471 * - search the process's keyrings 490 * @type: The type of key we want.
472 * - check the list of keys being created or updated 491 * @description: The searchable description of the key.
473 * - call out to userspace for a key if supplementary info was provided 492 * @callout_info: The data to pass to the instantiation upcall (or NULL).
474 * - cache the key in an appropriate keyring 493 * @callout_len: The length of callout_info.
494 * @aux: Auxiliary data for the upcall.
495 * @dest_keyring: Where to cache the key.
496 * @flags: Flags to key_alloc().
497 *
498 * A key matching the specified criteria is searched for in the process's
499 * keyrings and returned with its usage count incremented if found. Otherwise,
500 * if callout_info is not NULL, a key will be allocated and some service
501 * (probably in userspace) will be asked to instantiate it.
502 *
503 * If successfully found or created, the key will be linked to the destination
504 * keyring if one is provided.
505 *
506 * Returns a pointer to the key if successful; -EACCES, -ENOKEY, -EKEYREVOKED
507 * or -EKEYEXPIRED if an inaccessible, negative, revoked or expired key was
508 * found; -ENOKEY if no key was found and no @callout_info was given; -EDQUOT
509 * if insufficient key quota was available to create a new key; or -ENOMEM if
510 * insufficient memory was available.
511 *
512 * If the returned key was created, then it may still be under construction,
513 * and wait_for_key_construction() should be used to wait for that to complete.
475 */ 514 */
476struct key *request_key_and_link(struct key_type *type, 515struct key *request_key_and_link(struct key_type *type,
477 const char *description, 516 const char *description,
@@ -491,8 +530,7 @@ struct key *request_key_and_link(struct key_type *type,
491 dest_keyring, flags); 530 dest_keyring, flags);
492 531
493 /* search all the process keyrings for a key */ 532 /* search all the process keyrings for a key */
494 key_ref = search_process_keyrings(type, description, type->match, 533 key_ref = search_process_keyrings(type, description, type->match, cred);
495 cred);
496 534
497 if (!IS_ERR(key_ref)) { 535 if (!IS_ERR(key_ref)) {
498 key = key_ref_to_ptr(key_ref); 536 key = key_ref_to_ptr(key_ref);
@@ -525,8 +563,16 @@ error:
525 return key; 563 return key;
526} 564}
527 565
528/* 566/**
529 * wait for construction of a key to complete 567 * wait_for_key_construction - Wait for construction of a key to complete
568 * @key: The key being waited for.
569 * @intr: Whether to wait interruptibly.
570 *
571 * Wait for a key to finish being constructed.
572 *
573 * Returns 0 if successful; -ERESTARTSYS if the wait was interrupted; -ENOKEY
574 * if the key was negated; or -EKEYREVOKED or -EKEYEXPIRED if the key was
575 * revoked or expired.
530 */ 576 */
531int wait_for_key_construction(struct key *key, bool intr) 577int wait_for_key_construction(struct key *key, bool intr)
532{ 578{
@@ -538,17 +584,24 @@ int wait_for_key_construction(struct key *key, bool intr)
538 if (ret < 0) 584 if (ret < 0)
539 return ret; 585 return ret;
540 if (test_bit(KEY_FLAG_NEGATIVE, &key->flags)) 586 if (test_bit(KEY_FLAG_NEGATIVE, &key->flags))
541 return -ENOKEY; 587 return key->type_data.reject_error;
542 return key_validate(key); 588 return key_validate(key);
543} 589}
544EXPORT_SYMBOL(wait_for_key_construction); 590EXPORT_SYMBOL(wait_for_key_construction);
545 591
546/* 592/**
547 * request a key 593 * request_key - Request a key and wait for construction
548 * - search the process's keyrings 594 * @type: Type of key.
549 * - check the list of keys being created or updated 595 * @description: The searchable description of the key.
550 * - call out to userspace for a key if supplementary info was provided 596 * @callout_info: The data to pass to the instantiation upcall (or NULL).
551 * - waits uninterruptible for creation to complete 597 *
598 * As for request_key_and_link() except that it does not add the returned key
599 * to a keyring if found, new keys are always allocated in the user's quota,
600 * the callout_info must be a NUL-terminated string and no auxiliary data can
601 * be passed.
602 *
603 * Furthermore, it then works as wait_for_key_construction() to wait for the
604 * completion of keys undergoing construction with a non-interruptible wait.
552 */ 605 */
553struct key *request_key(struct key_type *type, 606struct key *request_key(struct key_type *type,
554 const char *description, 607 const char *description,
@@ -573,12 +626,19 @@ struct key *request_key(struct key_type *type,
573} 626}
574EXPORT_SYMBOL(request_key); 627EXPORT_SYMBOL(request_key);
575 628
576/* 629/**
577 * request a key with auxiliary data for the upcaller 630 * request_key_with_auxdata - Request a key with auxiliary data for the upcaller
578 * - search the process's keyrings 631 * @type: The type of key we want.
579 * - check the list of keys being created or updated 632 * @description: The searchable description of the key.
580 * - call out to userspace for a key if supplementary info was provided 633 * @callout_info: The data to pass to the instantiation upcall (or NULL).
581 * - waits uninterruptible for creation to complete 634 * @callout_len: The length of callout_info.
635 * @aux: Auxiliary data for the upcall.
636 *
637 * As for request_key_and_link() except that it does not add the returned key
638 * to a keyring if found and new keys are always allocated in the user's quota.
639 *
640 * Furthermore, it then works as wait_for_key_construction() to wait for the
641 * completion of keys undergoing construction with a non-interruptible wait.
582 */ 642 */
583struct key *request_key_with_auxdata(struct key_type *type, 643struct key *request_key_with_auxdata(struct key_type *type,
584 const char *description, 644 const char *description,
@@ -603,10 +663,18 @@ struct key *request_key_with_auxdata(struct key_type *type,
603EXPORT_SYMBOL(request_key_with_auxdata); 663EXPORT_SYMBOL(request_key_with_auxdata);
604 664
605/* 665/*
606 * request a key (allow async construction) 666 * request_key_async - Request a key (allow async construction)
607 * - search the process's keyrings 667 * @type: Type of key.
608 * - check the list of keys being created or updated 668 * @description: The searchable description of the key.
609 * - call out to userspace for a key if supplementary info was provided 669 * @callout_info: The data to pass to the instantiation upcall (or NULL).
670 * @callout_len: The length of callout_info.
671 *
672 * As for request_key_and_link() except that it does not add the returned key
673 * to a keyring if found, new keys are always allocated in the user's quota and
674 * no auxiliary data can be passed.
675 *
676 * The caller should call wait_for_key_construction() to wait for the
677 * completion of the returned key if it is still undergoing construction.
610 */ 678 */
611struct key *request_key_async(struct key_type *type, 679struct key *request_key_async(struct key_type *type,
612 const char *description, 680 const char *description,
@@ -621,9 +689,17 @@ EXPORT_SYMBOL(request_key_async);
621 689
622/* 690/*
623 * request a key with auxiliary data for the upcaller (allow async construction) 691 * request a key with auxiliary data for the upcaller (allow async construction)
624 * - search the process's keyrings 692 * @type: Type of key.
625 * - check the list of keys being created or updated 693 * @description: The searchable description of the key.
626 * - call out to userspace for a key if supplementary info was provided 694 * @callout_info: The data to pass to the instantiation upcall (or NULL).
695 * @callout_len: The length of callout_info.
696 * @aux: Auxiliary data for the upcall.
697 *
698 * As for request_key_and_link() except that it does not add the returned key
699 * to a keyring if found and new keys are always allocated in the user's quota.
700 *
701 * The caller should call wait_for_key_construction() to wait for the
702 * completion of the returned key if it is still undergoing construction.
627 */ 703 */
628struct key *request_key_async_with_auxdata(struct key_type *type, 704struct key *request_key_async_with_auxdata(struct key_type *type,
629 const char *description, 705 const char *description,