1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* Request a key from userspace 3 * 4 * Copyright (C) 2004-2007 Red Hat, Inc. All Rights Reserved. 5 * Written by David Howells (dhowells@redhat.com) 6 * 7 * See Documentation/security/keys/request-key.rst 8 */ 9 10 #include <linux/export.h> 11 #include <linux/sched.h> 12 #include <linux/kmod.h> 13 #include <linux/err.h> 14 #include <linux/keyctl.h> 15 #include <linux/slab.h> 16 #include <net/net_namespace.h> 17 #include "internal.h" 18 #include <keys/request_key_auth-type.h> 19 20 #define key_negative_timeout 60 /* default timeout on a negative key's existence */ 21 22 static struct key *check_cached_key(struct keyring_search_context *ctx) 23 { 24 #ifdef CONFIG_KEYS_REQUEST_CACHE 25 struct key *key = current->cached_requested_key; 26 27 if (key && 28 ctx->match_data.cmp(key, &ctx->match_data) && 29 !(key->flags & ((1 << KEY_FLAG_INVALIDATED) | 30 (1 << KEY_FLAG_REVOKED)))) 31 return key_get(key); 32 #endif 33 return NULL; 34 } 35 36 static void cache_requested_key(struct key *key) 37 { 38 #ifdef CONFIG_KEYS_REQUEST_CACHE 39 struct task_struct *t = current; 40 41 key_put(t->cached_requested_key); 42 t->cached_requested_key = key_get(key); 43 set_tsk_thread_flag(t, TIF_NOTIFY_RESUME); 44 #endif 45 } 46 47 /** 48 * complete_request_key - Complete the construction of a key. 49 * @authkey: The authorisation key. 50 * @error: The success or failute of the construction. 51 * 52 * Complete the attempt to construct a key. The key will be negated 53 * if an error is indicated. The authorisation key will be revoked 54 * unconditionally. 55 */ 56 void complete_request_key(struct key *authkey, int error) 57 { 58 struct request_key_auth *rka = get_request_key_auth(authkey); 59 struct key *key = rka->target_key; 60 61 kenter("%d{%d},%d", authkey->serial, key->serial, error); 62 63 if (error < 0) 64 key_negate_and_link(key, key_negative_timeout, NULL, authkey); 65 else 66 key_revoke(authkey); 67 } 68 EXPORT_SYMBOL(complete_request_key); 69 70 /* 71 * Initialise a usermode helper that is going to have a specific session 72 * keyring. 73 * 74 * This is called in context of freshly forked kthread before kernel_execve(), 75 * so we can simply install the desired session_keyring at this point. 76 */ 77 static int umh_keys_init(struct subprocess_info *info, struct cred *cred) 78 { 79 struct key *keyring = info->data; 80 81 return install_session_keyring_to_cred(cred, keyring); 82 } 83 84 /* 85 * Clean up a usermode helper with session keyring. 86 */ 87 static void umh_keys_cleanup(struct subprocess_info *info) 88 { 89 struct key *keyring = info->data; 90 key_put(keyring); 91 } 92 93 /* 94 * Call a usermode helper with a specific session keyring. 95 */ 96 static int call_usermodehelper_keys(const char *path, char **argv, char **envp, 97 struct key *session_keyring, int wait) 98 { 99 struct subprocess_info *info; 100 101 info = call_usermodehelper_setup(path, argv, envp, GFP_KERNEL, 102 umh_keys_init, umh_keys_cleanup, 103 session_keyring); 104 if (!info) 105 return -ENOMEM; 106 107 key_get(session_keyring); 108 return call_usermodehelper_exec(info, wait); 109 } 110 111 /* 112 * Request userspace finish the construction of a key 113 * - execute "/sbin/request-key <op> <key> <uid> <gid> <keyring> <keyring> <keyring>" 114 */ 115 static int call_sbin_request_key(struct key *authkey, void *aux) 116 { 117 static char const request_key[] = "/sbin/request-key"; 118 struct request_key_auth *rka = get_request_key_auth(authkey); 119 const struct cred *cred = current_cred(); 120 key_serial_t prkey, sskey; 121 struct key *key = rka->target_key, *keyring, *session, *user_session; 122 char *argv[9], *envp[3], uid_str[12], gid_str[12]; 123 char key_str[12], keyring_str[3][12]; 124 char desc[20]; 125 int ret, i; 126 127 kenter("{%d},{%d},%s", key->serial, authkey->serial, rka->op); 128 129 ret = look_up_user_keyrings(NULL, &user_session); 130 if (ret < 0) 131 goto error_us; 132 133 /* allocate a new session keyring */ 134 sprintf(desc, "_req.%u", key->serial); 135 136 cred = get_current_cred(); 137 keyring = keyring_alloc(desc, cred->fsuid, cred->fsgid, cred, 138 NULL, KEY_ALLOC_QUOTA_OVERRUN, NULL, NULL); 139 put_cred(cred); 140 if (IS_ERR(keyring)) { 141 ret = PTR_ERR(keyring); 142 goto error_alloc; 143 } 144 145 /* attach the auth key to the session keyring */ 146 ret = key_link(keyring, authkey); 147 if (ret < 0) 148 goto error_link; 149 150 /* record the UID and GID */ 151 sprintf(uid_str, "%d", from_kuid(&init_user_ns, cred->fsuid)); 152 sprintf(gid_str, "%d", from_kgid(&init_user_ns, cred->fsgid)); 153 154 /* we say which key is under construction */ 155 sprintf(key_str, "%d", key->serial); 156 157 /* we specify the process's default keyrings */ 158 sprintf(keyring_str[0], "%d", 159 cred->thread_keyring ? cred->thread_keyring->serial : 0); 160 161 prkey = 0; 162 if (cred->process_keyring) 163 prkey = cred->process_keyring->serial; 164 sprintf(keyring_str[1], "%d", prkey); 165 166 session = cred->session_keyring; 167 if (!session) 168 session = user_session; 169 sskey = session->serial; 170 171 sprintf(keyring_str[2], "%d", sskey); 172 173 /* set up a minimal environment */ 174 i = 0; 175 envp[i++] = "HOME=/"; 176 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; 177 envp[i] = NULL; 178 179 /* set up the argument list */ 180 i = 0; 181 argv[i++] = (char *)request_key; 182 argv[i++] = (char *)rka->op; 183 argv[i++] = key_str; 184 argv[i++] = uid_str; 185 argv[i++] = gid_str; 186 argv[i++] = keyring_str[0]; 187 argv[i++] = keyring_str[1]; 188 argv[i++] = keyring_str[2]; 189 argv[i] = NULL; 190 191 /* do it */ 192 ret = call_usermodehelper_keys(request_key, argv, envp, keyring, 193 UMH_WAIT_PROC); 194 kdebug("usermode -> 0x%x", ret); 195 if (ret >= 0) { 196 /* ret is the exit/wait code */ 197 if (test_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags) || 198 key_validate(key) < 0) 199 ret = -ENOKEY; 200 else 201 /* ignore any errors from userspace if the key was 202 * instantiated */ 203 ret = 0; 204 } 205 206 error_link: 207 key_put(keyring); 208 209 error_alloc: 210 key_put(user_session); 211 error_us: 212 complete_request_key(authkey, ret); 213 kleave(" = %d", ret); 214 return ret; 215 } 216 217 /* 218 * Call out to userspace for key construction. 219 * 220 * Program failure is ignored in favour of key status. 221 */ 222 static int construct_key(struct key *key, const void *callout_info, 223 size_t callout_len, void *aux, 224 struct key *dest_keyring) 225 { 226 request_key_actor_t actor; 227 struct key *authkey; 228 int ret; 229 230 kenter("%d,%p,%zu,%p", key->serial, callout_info, callout_len, aux); 231 232 /* allocate an authorisation key */ 233 authkey = request_key_auth_new(key, "create", callout_info, callout_len, 234 dest_keyring); 235 if (IS_ERR(authkey)) 236 return PTR_ERR(authkey); 237 238 /* Make the call */ 239 actor = call_sbin_request_key; 240 if (key->type->request_key) 241 actor = key->type->request_key; 242 243 ret = actor(authkey, aux); 244 245 /* check that the actor called complete_request_key() prior to 246 * returning an error */ 247 WARN_ON(ret < 0 && 248 !test_bit(KEY_FLAG_INVALIDATED, &authkey->flags)); 249 250 key_put(authkey); 251 kleave(" = %d", ret); 252 return ret; 253 } 254 255 /* 256 * Get the appropriate destination keyring for the request. 257 * 258 * The keyring selected is returned with an extra reference upon it which the 259 * caller must release. 260 */ 261 static int construct_get_dest_keyring(struct key **_dest_keyring) 262 { 263 struct request_key_auth *rka; 264 const struct cred *cred = current_cred(); 265 struct key *dest_keyring = *_dest_keyring, *authkey; 266 int ret; 267 268 kenter("%p", dest_keyring); 269 270 /* find the appropriate keyring */ 271 if (dest_keyring) { 272 /* the caller supplied one */ 273 key_get(dest_keyring); 274 } else { 275 bool do_perm_check = true; 276 277 /* use a default keyring; falling through the cases until we 278 * find one that we actually have */ 279 switch (cred->jit_keyring) { 280 case KEY_REQKEY_DEFL_DEFAULT: 281 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING: 282 if (cred->request_key_auth) { 283 authkey = cred->request_key_auth; 284 down_read(&authkey->sem); 285 rka = get_request_key_auth(authkey); 286 if (!test_bit(KEY_FLAG_REVOKED, 287 &authkey->flags)) 288 dest_keyring = 289 key_get(rka->dest_keyring); 290 up_read(&authkey->sem); 291 if (dest_keyring) { 292 do_perm_check = false; 293 break; 294 } 295 } 296 297 /* fall through */ 298 case KEY_REQKEY_DEFL_THREAD_KEYRING: 299 dest_keyring = key_get(cred->thread_keyring); 300 if (dest_keyring) 301 break; 302 303 /* fall through */ 304 case KEY_REQKEY_DEFL_PROCESS_KEYRING: 305 dest_keyring = key_get(cred->process_keyring); 306 if (dest_keyring) 307 break; 308 309 /* fall through */ 310 case KEY_REQKEY_DEFL_SESSION_KEYRING: 311 dest_keyring = key_get(cred->session_keyring); 312 313 if (dest_keyring) 314 break; 315 316 /* fall through */ 317 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING: 318 ret = look_up_user_keyrings(NULL, &dest_keyring); 319 if (ret < 0) 320 return ret; 321 break; 322 323 case KEY_REQKEY_DEFL_USER_KEYRING: 324 ret = look_up_user_keyrings(&dest_keyring, NULL); 325 if (ret < 0) 326 return ret; 327 break; 328 329 case KEY_REQKEY_DEFL_GROUP_KEYRING: 330 default: 331 BUG(); 332 } 333 334 /* 335 * Require Write permission on the keyring. This is essential 336 * because the default keyring may be the session keyring, and 337 * joining a keyring only requires Search permission. 338 * 339 * However, this check is skipped for the "requestor keyring" so 340 * that /sbin/request-key can itself use request_key() to add 341 * keys to the original requestor's destination keyring. 342 */ 343 if (dest_keyring && do_perm_check) { 344 ret = key_permission(make_key_ref(dest_keyring, 1), 345 KEY_NEED_WRITE); 346 if (ret) { 347 key_put(dest_keyring); 348 return ret; 349 } 350 } 351 } 352 353 *_dest_keyring = dest_keyring; 354 kleave(" [dk %d]", key_serial(dest_keyring)); 355 return 0; 356 } 357 358 /* 359 * Allocate a new key in under-construction state and attempt to link it in to 360 * the requested keyring. 361 * 362 * May return a key that's already under construction instead if there was a 363 * race between two thread calling request_key(). 364 */ 365 static int construct_alloc_key(struct keyring_search_context *ctx, 366 struct key *dest_keyring, 367 unsigned long flags, 368 struct key_user *user, 369 struct key_acl *acl, 370 struct key **_key) 371 { 372 struct assoc_array_edit *edit = NULL; 373 struct key *key; 374 key_ref_t key_ref; 375 int ret; 376 377 kenter("%s,%s,,,", 378 ctx->index_key.type->name, ctx->index_key.description); 379 380 *_key = NULL; 381 mutex_lock(&user->cons_lock); 382 383 key = key_alloc(ctx->index_key.type, ctx->index_key.description, 384 ctx->cred->fsuid, ctx->cred->fsgid, ctx->cred, 385 acl, flags, NULL); 386 if (IS_ERR(key)) 387 goto alloc_failed; 388 389 set_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags); 390 391 if (dest_keyring) { 392 ret = __key_link_lock(dest_keyring, &ctx->index_key); 393 if (ret < 0) 394 goto link_lock_failed; 395 ret = __key_link_begin(dest_keyring, &ctx->index_key, &edit); 396 if (ret < 0) 397 goto link_prealloc_failed; 398 } 399 400 /* attach the key to the destination keyring under lock, but we do need 401 * to do another check just in case someone beat us to it whilst we 402 * waited for locks */ 403 mutex_lock(&key_construction_mutex); 404 405 rcu_read_lock(); 406 key_ref = search_process_keyrings_rcu(ctx); 407 rcu_read_unlock(); 408 if (!IS_ERR(key_ref)) 409 goto key_already_present; 410 411 if (dest_keyring) 412 __key_link(key, &edit); 413 414 mutex_unlock(&key_construction_mutex); 415 if (dest_keyring) 416 __key_link_end(dest_keyring, &ctx->index_key, edit); 417 mutex_unlock(&user->cons_lock); 418 *_key = key; 419 kleave(" = 0 [%d]", key_serial(key)); 420 return 0; 421 422 /* the key is now present - we tell the caller that we found it by 423 * returning -EINPROGRESS */ 424 key_already_present: 425 key_put(key); 426 mutex_unlock(&key_construction_mutex); 427 key = key_ref_to_ptr(key_ref); 428 if (dest_keyring) { 429 ret = __key_link_check_live_key(dest_keyring, key); 430 if (ret == 0) 431 __key_link(key, &edit); 432 __key_link_end(dest_keyring, &ctx->index_key, edit); 433 if (ret < 0) 434 goto link_check_failed; 435 } 436 mutex_unlock(&user->cons_lock); 437 *_key = key; 438 kleave(" = -EINPROGRESS [%d]", key_serial(key)); 439 return -EINPROGRESS; 440 441 link_check_failed: 442 mutex_unlock(&user->cons_lock); 443 key_put(key); 444 kleave(" = %d [linkcheck]", ret); 445 return ret; 446 447 link_prealloc_failed: 448 __key_link_end(dest_keyring, &ctx->index_key, edit); 449 link_lock_failed: 450 mutex_unlock(&user->cons_lock); 451 key_put(key); 452 kleave(" = %d [prelink]", ret); 453 return ret; 454 455 alloc_failed: 456 mutex_unlock(&user->cons_lock); 457 kleave(" = %ld", PTR_ERR(key)); 458 return PTR_ERR(key); 459 } 460 461 /* 462 * Commence key construction. 463 */ 464 static struct key *construct_key_and_link(struct keyring_search_context *ctx, 465 const char *callout_info, 466 size_t callout_len, 467 void *aux, 468 struct key_acl *acl, 469 struct key *dest_keyring, 470 unsigned long flags) 471 { 472 struct key_user *user; 473 struct key *key; 474 int ret; 475 476 kenter(""); 477 478 if (ctx->index_key.type == &key_type_keyring) 479 return ERR_PTR(-EPERM); 480 481 ret = construct_get_dest_keyring(&dest_keyring); 482 if (ret) 483 goto error; 484 485 user = key_user_lookup(current_fsuid()); 486 if (!user) { 487 ret = -ENOMEM; 488 goto error_put_dest_keyring; 489 } 490 491 ret = construct_alloc_key(ctx, dest_keyring, flags, user, acl, &key); 492 key_user_put(user); 493 494 if (ret == 0) { 495 ret = construct_key(key, callout_info, callout_len, aux, 496 dest_keyring); 497 if (ret < 0) { 498 kdebug("cons failed"); 499 goto construction_failed; 500 } 501 } else if (ret == -EINPROGRESS) { 502 ret = 0; 503 } else { 504 goto error_put_dest_keyring; 505 } 506 507 key_put(dest_keyring); 508 kleave(" = key %d", key_serial(key)); 509 return key; 510 511 construction_failed: 512 key_negate_and_link(key, key_negative_timeout, NULL, NULL); 513 key_put(key); 514 error_put_dest_keyring: 515 key_put(dest_keyring); 516 error: 517 kleave(" = %d", ret); 518 return ERR_PTR(ret); 519 } 520 521 /** 522 * request_key_and_link - Request a key and cache it in a keyring. 523 * @type: The type of key we want. 524 * @description: The searchable description of the key. 525 * @domain_tag: The domain in which the key operates. 526 * @callout_info: The data to pass to the instantiation upcall (or NULL). 527 * @callout_len: The length of callout_info. 528 * @aux: Auxiliary data for the upcall. 529 * @acl: The ACL to attach if a new key is created. 530 * @dest_keyring: Where to cache the key. 531 * @flags: Flags to key_alloc(). 532 * 533 * A key matching the specified criteria (type, description, domain_tag) is 534 * searched for in the process's keyrings and returned with its usage count 535 * incremented if found. Otherwise, if callout_info is not NULL, a key will be 536 * allocated and some service (probably in userspace) will be asked to 537 * instantiate it. 538 * 539 * If successfully found or created, the key will be linked to the destination 540 * keyring if one is provided. 541 * 542 * Returns a pointer to the key if successful; -EACCES, -ENOKEY, -EKEYREVOKED 543 * or -EKEYEXPIRED if an inaccessible, negative, revoked or expired key was 544 * found; -ENOKEY if no key was found and no @callout_info was given; -EDQUOT 545 * if insufficient key quota was available to create a new key; or -ENOMEM if 546 * insufficient memory was available. 547 * 548 * If the returned key was created, then it may still be under construction, 549 * and wait_for_key_construction() should be used to wait for that to complete. 550 */ 551 struct key *request_key_and_link(struct key_type *type, 552 const char *description, 553 struct key_tag *domain_tag, 554 const void *callout_info, 555 size_t callout_len, 556 void *aux, 557 struct key_acl *acl, 558 struct key *dest_keyring, 559 unsigned long flags) 560 { 561 struct keyring_search_context ctx = { 562 .index_key.type = type, 563 .index_key.domain_tag = domain_tag, 564 .index_key.description = description, 565 .index_key.desc_len = strlen(description), 566 .cred = current_cred(), 567 .match_data.cmp = key_default_cmp, 568 .match_data.raw_data = description, 569 .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT, 570 .flags = (KEYRING_SEARCH_DO_STATE_CHECK | 571 KEYRING_SEARCH_SKIP_EXPIRED | 572 KEYRING_SEARCH_RECURSE), 573 }; 574 struct key *key; 575 key_ref_t key_ref; 576 int ret; 577 578 kenter("%s,%s,%p,%zu,%p,%p,%lx", 579 ctx.index_key.type->name, ctx.index_key.description, 580 callout_info, callout_len, aux, dest_keyring, flags); 581 582 if (type->match_preparse) { 583 ret = type->match_preparse(&ctx.match_data); 584 if (ret < 0) { 585 key = ERR_PTR(ret); 586 goto error; 587 } 588 } 589 590 key = check_cached_key(&ctx); 591 if (key) 592 return key; 593 594 /* search all the process keyrings for a key */ 595 rcu_read_lock(); 596 key_ref = search_process_keyrings_rcu(&ctx); 597 rcu_read_unlock(); 598 599 if (!IS_ERR(key_ref)) { 600 if (dest_keyring) { 601 ret = key_task_permission(key_ref, current_cred(), 602 KEY_NEED_LINK); 603 if (ret < 0) { 604 key_ref_put(key_ref); 605 key = ERR_PTR(ret); 606 goto error_free; 607 } 608 } 609 610 key = key_ref_to_ptr(key_ref); 611 if (dest_keyring) { 612 ret = key_link(dest_keyring, key); 613 if (ret < 0) { 614 key_put(key); 615 key = ERR_PTR(ret); 616 goto error_free; 617 } 618 } 619 620 /* Only cache the key on immediate success */ 621 cache_requested_key(key); 622 } else if (PTR_ERR(key_ref) != -EAGAIN) { 623 key = ERR_CAST(key_ref); 624 } else { 625 /* the search failed, but the keyrings were searchable, so we 626 * should consult userspace if we can */ 627 key = ERR_PTR(-ENOKEY); 628 if (!callout_info) 629 goto error_free; 630 631 key = construct_key_and_link(&ctx, callout_info, callout_len, 632 aux, acl, dest_keyring, flags); 633 } 634 635 error_free: 636 if (type->match_free) 637 type->match_free(&ctx.match_data); 638 error: 639 kleave(" = %p", key); 640 return key; 641 } 642 643 /** 644 * wait_for_key_construction - Wait for construction of a key to complete 645 * @key: The key being waited for. 646 * @intr: Whether to wait interruptibly. 647 * 648 * Wait for a key to finish being constructed. 649 * 650 * Returns 0 if successful; -ERESTARTSYS if the wait was interrupted; -ENOKEY 651 * if the key was negated; or -EKEYREVOKED or -EKEYEXPIRED if the key was 652 * revoked or expired. 653 */ 654 int wait_for_key_construction(struct key *key, bool intr) 655 { 656 int ret; 657 658 ret = wait_on_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT, 659 intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE); 660 if (ret) 661 return -ERESTARTSYS; 662 ret = key_read_state(key); 663 if (ret < 0) 664 return ret; 665 return key_validate(key); 666 } 667 EXPORT_SYMBOL(wait_for_key_construction); 668 669 /** 670 * request_key_tag - Request a key and wait for construction 671 * @type: Type of key. 672 * @description: The searchable description of the key. 673 * @domain_tag: The domain in which the key operates. 674 * @callout_info: The data to pass to the instantiation upcall (or NULL). 675 * @acl: The ACL to attach if a new key is created. 676 * 677 * As for request_key_and_link() except that it does not add the returned key 678 * to a keyring if found, new keys are always allocated in the user's quota, 679 * the callout_info must be a NUL-terminated string and no auxiliary data can 680 * be passed. 681 * 682 * Furthermore, it then works as wait_for_key_construction() to wait for the 683 * completion of keys undergoing construction with a non-interruptible wait. 684 */ 685 struct key *request_key_tag(struct key_type *type, 686 const char *description, 687 struct key_tag *domain_tag, 688 const char *callout_info, 689 struct key_acl *acl) 690 { 691 struct key *key; 692 size_t callout_len = 0; 693 int ret; 694 695 if (callout_info) 696 callout_len = strlen(callout_info); 697 key = request_key_and_link(type, description, domain_tag, 698 callout_info, callout_len, 699 NULL, acl, NULL, KEY_ALLOC_IN_QUOTA); 700 if (!IS_ERR(key)) { 701 ret = wait_for_key_construction(key, false); 702 if (ret < 0) { 703 key_put(key); 704 return ERR_PTR(ret); 705 } 706 } 707 return key; 708 } 709 EXPORT_SYMBOL(request_key_tag); 710 711 /** 712 * request_key_with_auxdata - Request a key with auxiliary data for the upcaller 713 * @type: The type of key we want. 714 * @description: The searchable description of the key. 715 * @domain_tag: The domain in which the key operates. 716 * @callout_info: The data to pass to the instantiation upcall (or NULL). 717 * @callout_len: The length of callout_info. 718 * @aux: Auxiliary data for the upcall. 719 * @acl: The ACL to attach if a new key is created. 720 * 721 * As for request_key_and_link() except that it does not add the returned key 722 * to a keyring if found and new keys are always allocated in the user's quota. 723 * 724 * Furthermore, it then works as wait_for_key_construction() to wait for the 725 * completion of keys undergoing construction with a non-interruptible wait. 726 */ 727 struct key *request_key_with_auxdata(struct key_type *type, 728 const char *description, 729 struct key_tag *domain_tag, 730 const void *callout_info, 731 size_t callout_len, 732 void *aux, 733 struct key_acl *acl) 734 { 735 struct key *key; 736 int ret; 737 738 key = request_key_and_link(type, description, domain_tag, 739 callout_info, callout_len, 740 aux, acl, NULL, KEY_ALLOC_IN_QUOTA); 741 if (!IS_ERR(key)) { 742 ret = wait_for_key_construction(key, false); 743 if (ret < 0) { 744 key_put(key); 745 return ERR_PTR(ret); 746 } 747 } 748 return key; 749 } 750 EXPORT_SYMBOL(request_key_with_auxdata); 751 752 /** 753 * request_key_rcu - Request key from RCU-read-locked context 754 * @type: The type of key we want. 755 * @description: The name of the key we want. 756 * @domain_tag: The domain in which the key operates. 757 * 758 * Request a key from a context that we may not sleep in (such as RCU-mode 759 * pathwalk). Keys under construction are ignored. 760 * 761 * Return a pointer to the found key if successful, -ENOKEY if we couldn't find 762 * a key or some other error if the key found was unsuitable or inaccessible. 763 */ 764 struct key *request_key_rcu(struct key_type *type, 765 const char *description, 766 struct key_tag *domain_tag) 767 { 768 struct keyring_search_context ctx = { 769 .index_key.type = type, 770 .index_key.domain_tag = domain_tag, 771 .index_key.description = description, 772 .index_key.desc_len = strlen(description), 773 .cred = current_cred(), 774 .match_data.cmp = key_default_cmp, 775 .match_data.raw_data = description, 776 .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT, 777 .flags = (KEYRING_SEARCH_DO_STATE_CHECK | 778 KEYRING_SEARCH_SKIP_EXPIRED), 779 }; 780 struct key *key; 781 key_ref_t key_ref; 782 783 kenter("%s,%s", type->name, description); 784 785 key = check_cached_key(&ctx); 786 if (key) 787 return key; 788 789 /* search all the process keyrings for a key */ 790 key_ref = search_process_keyrings_rcu(&ctx); 791 if (IS_ERR(key_ref)) { 792 key = ERR_CAST(key_ref); 793 if (PTR_ERR(key_ref) == -EAGAIN) 794 key = ERR_PTR(-ENOKEY); 795 } else { 796 key = key_ref_to_ptr(key_ref); 797 cache_requested_key(key); 798 } 799 800 kleave(" = %p", key); 801 return key; 802 } 803 EXPORT_SYMBOL(request_key_rcu); 804