diff options
Diffstat (limited to 'security/keys/request_key.c')
-rw-r--r-- | security/keys/request_key.c | 166 |
1 files changed, 122 insertions, 44 deletions
diff --git a/security/keys/request_key.c b/security/keys/request_key.c index 0ea52d25a6bd..a3dc0d460def 100644 --- a/security/keys/request_key.c +++ b/security/keys/request_key.c | |||
@@ -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 | */ |
45 | void complete_request_key(struct key_construction *cons, int error) | 51 | void complete_request_key(struct key_construction *cons, int error) |
46 | { | 52 | { |
@@ -58,23 +64,33 @@ void complete_request_key(struct key_construction *cons, int error) | |||
58 | } | 64 | } |
59 | EXPORT_SYMBOL(complete_request_key); | 65 | EXPORT_SYMBOL(complete_request_key); |
60 | 66 | ||
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 | */ | ||
61 | static int umh_keys_init(struct subprocess_info *info) | 74 | static int umh_keys_init(struct subprocess_info *info) |
62 | { | 75 | { |
63 | struct cred *cred = (struct cred*)current_cred(); | 76 | struct cred *cred = (struct cred*)current_cred(); |
64 | struct key *keyring = info->data; | 77 | struct key *keyring = info->data; |
65 | /* | 78 | |
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); | 79 | return install_session_keyring_to_cred(cred, keyring); |
70 | } | 80 | } |
71 | 81 | ||
82 | /* | ||
83 | * Clean up a usermode helper with session keyring. | ||
84 | */ | ||
72 | static void umh_keys_cleanup(struct subprocess_info *info) | 85 | static void umh_keys_cleanup(struct subprocess_info *info) |
73 | { | 86 | { |
74 | struct key *keyring = info->data; | 87 | struct key *keyring = info->data; |
75 | key_put(keyring); | 88 | key_put(keyring); |
76 | } | 89 | } |
77 | 90 | ||
91 | /* | ||
92 | * Call a usermode helper with a specific session keyring. | ||
93 | */ | ||
78 | static int call_usermodehelper_keys(char *path, char **argv, char **envp, | 94 | static int call_usermodehelper_keys(char *path, char **argv, char **envp, |
79 | struct key *session_keyring, enum umh_wait wait) | 95 | struct key *session_keyring, enum umh_wait wait) |
80 | { | 96 | { |
@@ -91,7 +107,7 @@ static int call_usermodehelper_keys(char *path, char **argv, char **envp, | |||
91 | } | 107 | } |
92 | 108 | ||
93 | /* | 109 | /* |
94 | * request userspace finish the construction of a key | 110 | * Request userspace finish the construction of a key |
95 | * - execute "/sbin/request-key <op> <key> <uid> <gid> <keyring> <keyring> <keyring>" | 111 | * - execute "/sbin/request-key <op> <key> <uid> <gid> <keyring> <keyring> <keyring>" |
96 | */ | 112 | */ |
97 | static int call_sbin_request_key(struct key_construction *cons, | 113 | static int call_sbin_request_key(struct key_construction *cons, |
@@ -198,8 +214,9 @@ error_alloc: | |||
198 | } | 214 | } |
199 | 215 | ||
200 | /* | 216 | /* |
201 | * call out to userspace for key construction | 217 | * Call out to userspace for key construction. |
202 | * - we ignore program failure and go on key status instead | 218 | * |
219 | * Program failure is ignored in favour of key status. | ||
203 | */ | 220 | */ |
204 | static int construct_key(struct key *key, const void *callout_info, | 221 | static int construct_key(struct key *key, const void *callout_info, |
205 | size_t callout_len, void *aux, | 222 | size_t callout_len, void *aux, |
@@ -246,9 +263,10 @@ static int construct_key(struct key *key, const void *callout_info, | |||
246 | } | 263 | } |
247 | 264 | ||
248 | /* | 265 | /* |
249 | * get the appropriate destination keyring for the request | 266 | * Get the appropriate destination keyring for the request. |
250 | * - we return whatever keyring we select with an extra reference upon it which | 267 | * |
251 | * the caller must release | 268 | * The keyring selected is returned with an extra reference upon it which the |
269 | * caller must release. | ||
252 | */ | 270 | */ |
253 | static void construct_get_dest_keyring(struct key **_dest_keyring) | 271 | static void construct_get_dest_keyring(struct key **_dest_keyring) |
254 | { | 272 | { |
@@ -321,9 +339,11 @@ static void construct_get_dest_keyring(struct key **_dest_keyring) | |||
321 | } | 339 | } |
322 | 340 | ||
323 | /* | 341 | /* |
324 | * allocate a new key in under-construction state and attempt to link it in to | 342 | * Allocate a new key in under-construction state and attempt to link it in to |
325 | * the requested place | 343 | * the requested keyring. |
326 | * - may return a key that's already under construction instead | 344 | * |
345 | * May return a key that's already under construction instead if there was a | ||
346 | * race between two thread calling request_key(). | ||
327 | */ | 347 | */ |
328 | static int construct_alloc_key(struct key_type *type, | 348 | static int construct_alloc_key(struct key_type *type, |
329 | const char *description, | 349 | const char *description, |
@@ -332,8 +352,8 @@ static int construct_alloc_key(struct key_type *type, | |||
332 | struct key_user *user, | 352 | struct key_user *user, |
333 | struct key **_key) | 353 | struct key **_key) |
334 | { | 354 | { |
335 | struct keyring_list *prealloc; | ||
336 | const struct cred *cred = current_cred(); | 355 | const struct cred *cred = current_cred(); |
356 | unsigned long prealloc; | ||
337 | struct key *key; | 357 | struct key *key; |
338 | key_ref_t key_ref; | 358 | key_ref_t key_ref; |
339 | int ret; | 359 | int ret; |
@@ -414,7 +434,7 @@ alloc_failed: | |||
414 | } | 434 | } |
415 | 435 | ||
416 | /* | 436 | /* |
417 | * commence key construction | 437 | * Commence key construction. |
418 | */ | 438 | */ |
419 | static struct key *construct_key_and_link(struct key_type *type, | 439 | static struct key *construct_key_and_link(struct key_type *type, |
420 | const char *description, | 440 | const char *description, |
@@ -465,12 +485,32 @@ construction_failed: | |||
465 | return ERR_PTR(ret); | 485 | return ERR_PTR(ret); |
466 | } | 486 | } |
467 | 487 | ||
468 | /* | 488 | /** |
469 | * request a key | 489 | * request_key_and_link - Request a key and cache it in a keyring. |
470 | * - search the process's keyrings | 490 | * @type: The type of key we want. |
471 | * - check the list of keys being created or updated | 491 | * @description: The searchable description of the key. |
472 | * - 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). |
473 | * - 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. | ||
474 | */ | 514 | */ |
475 | struct key *request_key_and_link(struct key_type *type, | 515 | struct key *request_key_and_link(struct key_type *type, |
476 | const char *description, | 516 | const char *description, |
@@ -524,8 +564,16 @@ error: | |||
524 | return key; | 564 | return key; |
525 | } | 565 | } |
526 | 566 | ||
527 | /* | 567 | /** |
528 | * wait for construction of a key to complete | 568 | * wait_for_key_construction - Wait for construction of a key to complete |
569 | * @key: The key being waited for. | ||
570 | * @intr: Whether to wait interruptibly. | ||
571 | * | ||
572 | * Wait for a key to finish being constructed. | ||
573 | * | ||
574 | * Returns 0 if successful; -ERESTARTSYS if the wait was interrupted; -ENOKEY | ||
575 | * if the key was negated; or -EKEYREVOKED or -EKEYEXPIRED if the key was | ||
576 | * revoked or expired. | ||
529 | */ | 577 | */ |
530 | int wait_for_key_construction(struct key *key, bool intr) | 578 | int wait_for_key_construction(struct key *key, bool intr) |
531 | { | 579 | { |
@@ -542,12 +590,19 @@ int wait_for_key_construction(struct key *key, bool intr) | |||
542 | } | 590 | } |
543 | EXPORT_SYMBOL(wait_for_key_construction); | 591 | EXPORT_SYMBOL(wait_for_key_construction); |
544 | 592 | ||
545 | /* | 593 | /** |
546 | * request a key | 594 | * request_key - Request a key and wait for construction |
547 | * - search the process's keyrings | 595 | * @type: Type of key. |
548 | * - check the list of keys being created or updated | 596 | * @description: The searchable description of the key. |
549 | * - call out to userspace for a key if supplementary info was provided | 597 | * @callout_info: The data to pass to the instantiation upcall (or NULL). |
550 | * - waits uninterruptible for creation to complete | 598 | * |
599 | * As for request_key_and_link() except that it does not add the returned key | ||
600 | * to a keyring if found, new keys are always allocated in the user's quota, | ||
601 | * the callout_info must be a NUL-terminated string and no auxiliary data can | ||
602 | * be passed. | ||
603 | * | ||
604 | * Furthermore, it then works as wait_for_key_construction() to wait for the | ||
605 | * completion of keys undergoing construction with a non-interruptible wait. | ||
551 | */ | 606 | */ |
552 | struct key *request_key(struct key_type *type, | 607 | struct key *request_key(struct key_type *type, |
553 | const char *description, | 608 | const char *description, |
@@ -572,12 +627,19 @@ struct key *request_key(struct key_type *type, | |||
572 | } | 627 | } |
573 | EXPORT_SYMBOL(request_key); | 628 | EXPORT_SYMBOL(request_key); |
574 | 629 | ||
575 | /* | 630 | /** |
576 | * request a key with auxiliary data for the upcaller | 631 | * request_key_with_auxdata - Request a key with auxiliary data for the upcaller |
577 | * - search the process's keyrings | 632 | * @type: The type of key we want. |
578 | * - check the list of keys being created or updated | 633 | * @description: The searchable description of the key. |
579 | * - call out to userspace for a key if supplementary info was provided | 634 | * @callout_info: The data to pass to the instantiation upcall (or NULL). |
580 | * - waits uninterruptible for creation to complete | 635 | * @callout_len: The length of callout_info. |
636 | * @aux: Auxiliary data for the upcall. | ||
637 | * | ||
638 | * As for request_key_and_link() except that it does not add the returned key | ||
639 | * to a keyring if found and new keys are always allocated in the user's quota. | ||
640 | * | ||
641 | * Furthermore, it then works as wait_for_key_construction() to wait for the | ||
642 | * completion of keys undergoing construction with a non-interruptible wait. | ||
581 | */ | 643 | */ |
582 | struct key *request_key_with_auxdata(struct key_type *type, | 644 | struct key *request_key_with_auxdata(struct key_type *type, |
583 | const char *description, | 645 | const char *description, |
@@ -602,10 +664,18 @@ struct key *request_key_with_auxdata(struct key_type *type, | |||
602 | EXPORT_SYMBOL(request_key_with_auxdata); | 664 | EXPORT_SYMBOL(request_key_with_auxdata); |
603 | 665 | ||
604 | /* | 666 | /* |
605 | * request a key (allow async construction) | 667 | * request_key_async - Request a key (allow async construction) |
606 | * - search the process's keyrings | 668 | * @type: Type of key. |
607 | * - check the list of keys being created or updated | 669 | * @description: The searchable description of the key. |
608 | * - call out to userspace for a key if supplementary info was provided | 670 | * @callout_info: The data to pass to the instantiation upcall (or NULL). |
671 | * @callout_len: The length of callout_info. | ||
672 | * | ||
673 | * As for request_key_and_link() except that it does not add the returned key | ||
674 | * to a keyring if found, new keys are always allocated in the user's quota and | ||
675 | * no auxiliary data can be passed. | ||
676 | * | ||
677 | * The caller should call wait_for_key_construction() to wait for the | ||
678 | * completion of the returned key if it is still undergoing construction. | ||
609 | */ | 679 | */ |
610 | struct key *request_key_async(struct key_type *type, | 680 | struct key *request_key_async(struct key_type *type, |
611 | const char *description, | 681 | const char *description, |
@@ -620,9 +690,17 @@ EXPORT_SYMBOL(request_key_async); | |||
620 | 690 | ||
621 | /* | 691 | /* |
622 | * request a key with auxiliary data for the upcaller (allow async construction) | 692 | * request a key with auxiliary data for the upcaller (allow async construction) |
623 | * - search the process's keyrings | 693 | * @type: Type of key. |
624 | * - check the list of keys being created or updated | 694 | * @description: The searchable description of the key. |
625 | * - call out to userspace for a key if supplementary info was provided | 695 | * @callout_info: The data to pass to the instantiation upcall (or NULL). |
696 | * @callout_len: The length of callout_info. | ||
697 | * @aux: Auxiliary data for the upcall. | ||
698 | * | ||
699 | * As for request_key_and_link() except that it does not add the returned key | ||
700 | * to a keyring if found and new keys are always allocated in the user's quota. | ||
701 | * | ||
702 | * The caller should call wait_for_key_construction() to wait for the | ||
703 | * completion of the returned key if it is still undergoing construction. | ||
626 | */ | 704 | */ |
627 | struct key *request_key_async_with_auxdata(struct key_type *type, | 705 | struct key *request_key_async_with_auxdata(struct key_type *type, |
628 | const char *description, | 706 | const char *description, |