request_key.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. /* Request a key from userspace
  2. *
  3. * Copyright (C) 2004-2007 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  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.
  10. *
  11. * See Documentation/security/keys-request-key.txt
  12. */
  13. #include <linux/module.h>
  14. #include <linux/sched.h>
  15. #include <linux/kmod.h>
  16. #include <linux/err.h>
  17. #include <linux/keyctl.h>
  18. #include <linux/slab.h>
  19. #include "internal.h"
  20. #define key_negative_timeout 60 /* default timeout on a negative key's existence */
  21. /*
  22. * wait_on_bit() sleep function for uninterruptible waiting
  23. */
  24. static int key_wait_bit(void *flags)
  25. {
  26. schedule();
  27. return 0;
  28. }
  29. /*
  30. * wait_on_bit() sleep function for interruptible waiting
  31. */
  32. static int key_wait_bit_intr(void *flags)
  33. {
  34. schedule();
  35. return signal_pending(current) ? -ERESTARTSYS : 0;
  36. }
  37. /**
  38. * complete_request_key - Complete the construction of a key.
  39. * @cons: The key construction record.
  40. * @error: The success or failute of the construction.
  41. *
  42. * Complete the attempt to construct a key. The key will be negated
  43. * if an error is indicated. The authorisation key will be revoked
  44. * unconditionally.
  45. */
  46. void complete_request_key(struct key_construction *cons, int error)
  47. {
  48. kenter("{%d,%d},%d", cons->key->serial, cons->authkey->serial, error);
  49. if (error < 0)
  50. key_negate_and_link(cons->key, key_negative_timeout, NULL,
  51. cons->authkey);
  52. else
  53. key_revoke(cons->authkey);
  54. key_put(cons->key);
  55. key_put(cons->authkey);
  56. kfree(cons);
  57. }
  58. EXPORT_SYMBOL(complete_request_key);
  59. /*
  60. * Initialise a usermode helper that is going to have a specific session
  61. * keyring.
  62. *
  63. * This is called in context of freshly forked kthread before kernel_execve(),
  64. * so we can simply install the desired session_keyring at this point.
  65. */
  66. static int umh_keys_init(struct subprocess_info *info, struct cred *cred)
  67. {
  68. struct key *keyring = info->data;
  69. return install_session_keyring_to_cred(cred, keyring);
  70. }
  71. /*
  72. * Clean up a usermode helper with session keyring.
  73. */
  74. static void umh_keys_cleanup(struct subprocess_info *info)
  75. {
  76. struct key *keyring = info->data;
  77. key_put(keyring);
  78. }
  79. /*
  80. * Call a usermode helper with a specific session keyring.
  81. */
  82. static int call_usermodehelper_keys(char *path, char **argv, char **envp,
  83. struct key *session_keyring, int wait)
  84. {
  85. struct subprocess_info *info;
  86. info = call_usermodehelper_setup(path, argv, envp, GFP_KERNEL,
  87. umh_keys_init, umh_keys_cleanup,
  88. session_keyring);
  89. if (!info)
  90. return -ENOMEM;
  91. key_get(session_keyring);
  92. return call_usermodehelper_exec(info, wait);
  93. }
  94. /*
  95. * Request userspace finish the construction of a key
  96. * - execute "/sbin/request-key <op> <key> <uid> <gid> <keyring> <keyring> <keyring>"
  97. */
  98. static int call_sbin_request_key(struct key_construction *cons,
  99. const char *op,
  100. void *aux)
  101. {
  102. const struct cred *cred = current_cred();
  103. key_serial_t prkey, sskey;
  104. struct key *key = cons->key, *authkey = cons->authkey, *keyring,
  105. *session;
  106. char *argv[9], *envp[3], uid_str[12], gid_str[12];
  107. char key_str[12], keyring_str[3][12];
  108. char desc[20];
  109. int ret, i;
  110. kenter("{%d},{%d},%s", key->serial, authkey->serial, op);
  111. ret = install_user_keyrings();
  112. if (ret < 0)
  113. goto error_alloc;
  114. /* allocate a new session keyring */
  115. sprintf(desc, "_req.%u", key->serial);
  116. cred = get_current_cred();
  117. keyring = keyring_alloc(desc, cred->fsuid, cred->fsgid, cred,
  118. KEY_POS_ALL | KEY_USR_VIEW | KEY_USR_READ,
  119. KEY_ALLOC_QUOTA_OVERRUN, NULL);
  120. put_cred(cred);
  121. if (IS_ERR(keyring)) {
  122. ret = PTR_ERR(keyring);
  123. goto error_alloc;
  124. }
  125. /* attach the auth key to the session keyring */
  126. ret = key_link(keyring, authkey);
  127. if (ret < 0)
  128. goto error_link;
  129. /* record the UID and GID */
  130. sprintf(uid_str, "%d", from_kuid(&init_user_ns, cred->fsuid));
  131. sprintf(gid_str, "%d", from_kgid(&init_user_ns, cred->fsgid));
  132. /* we say which key is under construction */
  133. sprintf(key_str, "%d", key->serial);
  134. /* we specify the process's default keyrings */
  135. sprintf(keyring_str[0], "%d",
  136. cred->thread_keyring ? cred->thread_keyring->serial : 0);
  137. prkey = 0;
  138. if (cred->process_keyring)
  139. prkey = cred->process_keyring->serial;
  140. sprintf(keyring_str[1], "%d", prkey);
  141. rcu_read_lock();
  142. session = rcu_dereference(cred->session_keyring);
  143. if (!session)
  144. session = cred->user->session_keyring;
  145. sskey = session->serial;
  146. rcu_read_unlock();
  147. sprintf(keyring_str[2], "%d", sskey);
  148. /* set up a minimal environment */
  149. i = 0;
  150. envp[i++] = "HOME=/";
  151. envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
  152. envp[i] = NULL;
  153. /* set up the argument list */
  154. i = 0;
  155. argv[i++] = "/sbin/request-key";
  156. argv[i++] = (char *) op;
  157. argv[i++] = key_str;
  158. argv[i++] = uid_str;
  159. argv[i++] = gid_str;
  160. argv[i++] = keyring_str[0];
  161. argv[i++] = keyring_str[1];
  162. argv[i++] = keyring_str[2];
  163. argv[i] = NULL;
  164. /* do it */
  165. ret = call_usermodehelper_keys(argv[0], argv, envp, keyring,
  166. UMH_WAIT_PROC);
  167. kdebug("usermode -> 0x%x", ret);
  168. if (ret >= 0) {
  169. /* ret is the exit/wait code */
  170. if (test_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags) ||
  171. key_validate(key) < 0)
  172. ret = -ENOKEY;
  173. else
  174. /* ignore any errors from userspace if the key was
  175. * instantiated */
  176. ret = 0;
  177. }
  178. error_link:
  179. key_put(keyring);
  180. error_alloc:
  181. complete_request_key(cons, ret);
  182. kleave(" = %d", ret);
  183. return ret;
  184. }
  185. /*
  186. * Call out to userspace for key construction.
  187. *
  188. * Program failure is ignored in favour of key status.
  189. */
  190. static int construct_key(struct key *key, const void *callout_info,
  191. size_t callout_len, void *aux,
  192. struct key *dest_keyring)
  193. {
  194. struct key_construction *cons;
  195. request_key_actor_t actor;
  196. struct key *authkey;
  197. int ret;
  198. kenter("%d,%p,%zu,%p", key->serial, callout_info, callout_len, aux);
  199. cons = kmalloc(sizeof(*cons), GFP_KERNEL);
  200. if (!cons)
  201. return -ENOMEM;
  202. /* allocate an authorisation key */
  203. authkey = request_key_auth_new(key, callout_info, callout_len,
  204. dest_keyring);
  205. if (IS_ERR(authkey)) {
  206. kfree(cons);
  207. ret = PTR_ERR(authkey);
  208. authkey = NULL;
  209. } else {
  210. cons->authkey = key_get(authkey);
  211. cons->key = key_get(key);
  212. /* make the call */
  213. actor = call_sbin_request_key;
  214. if (key->type->request_key)
  215. actor = key->type->request_key;
  216. ret = actor(cons, "create", aux);
  217. /* check that the actor called complete_request_key() prior to
  218. * returning an error */
  219. WARN_ON(ret < 0 &&
  220. !test_bit(KEY_FLAG_REVOKED, &authkey->flags));
  221. key_put(authkey);
  222. }
  223. kleave(" = %d", ret);
  224. return ret;
  225. }
  226. /*
  227. * Get the appropriate destination keyring for the request.
  228. *
  229. * The keyring selected is returned with an extra reference upon it which the
  230. * caller must release.
  231. */
  232. static void construct_get_dest_keyring(struct key **_dest_keyring)
  233. {
  234. struct request_key_auth *rka;
  235. const struct cred *cred = current_cred();
  236. struct key *dest_keyring = *_dest_keyring, *authkey;
  237. kenter("%p", dest_keyring);
  238. /* find the appropriate keyring */
  239. if (dest_keyring) {
  240. /* the caller supplied one */
  241. key_get(dest_keyring);
  242. } else {
  243. /* use a default keyring; falling through the cases until we
  244. * find one that we actually have */
  245. switch (cred->jit_keyring) {
  246. case KEY_REQKEY_DEFL_DEFAULT:
  247. case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
  248. if (cred->request_key_auth) {
  249. authkey = cred->request_key_auth;
  250. down_read(&authkey->sem);
  251. rka = authkey->payload.data;
  252. if (!test_bit(KEY_FLAG_REVOKED,
  253. &authkey->flags))
  254. dest_keyring =
  255. key_get(rka->dest_keyring);
  256. up_read(&authkey->sem);
  257. if (dest_keyring)
  258. break;
  259. }
  260. case KEY_REQKEY_DEFL_THREAD_KEYRING:
  261. dest_keyring = key_get(cred->thread_keyring);
  262. if (dest_keyring)
  263. break;
  264. case KEY_REQKEY_DEFL_PROCESS_KEYRING:
  265. dest_keyring = key_get(cred->process_keyring);
  266. if (dest_keyring)
  267. break;
  268. case KEY_REQKEY_DEFL_SESSION_KEYRING:
  269. rcu_read_lock();
  270. dest_keyring = key_get(
  271. rcu_dereference(cred->session_keyring));
  272. rcu_read_unlock();
  273. if (dest_keyring)
  274. break;
  275. case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
  276. dest_keyring =
  277. key_get(cred->user->session_keyring);
  278. break;
  279. case KEY_REQKEY_DEFL_USER_KEYRING:
  280. dest_keyring = key_get(cred->user->uid_keyring);
  281. break;
  282. case KEY_REQKEY_DEFL_GROUP_KEYRING:
  283. default:
  284. BUG();
  285. }
  286. }
  287. *_dest_keyring = dest_keyring;
  288. kleave(" [dk %d]", key_serial(dest_keyring));
  289. return;
  290. }
  291. /*
  292. * Allocate a new key in under-construction state and attempt to link it in to
  293. * the requested keyring.
  294. *
  295. * May return a key that's already under construction instead if there was a
  296. * race between two thread calling request_key().
  297. */
  298. static int construct_alloc_key(struct keyring_search_context *ctx,
  299. struct key *dest_keyring,
  300. unsigned long flags,
  301. struct key_user *user,
  302. struct key **_key)
  303. {
  304. struct assoc_array_edit *edit;
  305. struct key *key;
  306. key_perm_t perm;
  307. key_ref_t key_ref;
  308. int ret;
  309. kenter("%s,%s,,,",
  310. ctx->index_key.type->name, ctx->index_key.description);
  311. *_key = NULL;
  312. mutex_lock(&user->cons_lock);
  313. perm = KEY_POS_VIEW | KEY_POS_SEARCH | KEY_POS_LINK | KEY_POS_SETATTR;
  314. perm |= KEY_USR_VIEW;
  315. if (ctx->index_key.type->read)
  316. perm |= KEY_POS_READ;
  317. if (ctx->index_key.type == &key_type_keyring ||
  318. ctx->index_key.type->update)
  319. perm |= KEY_POS_WRITE;
  320. key = key_alloc(ctx->index_key.type, ctx->index_key.description,
  321. ctx->cred->fsuid, ctx->cred->fsgid, ctx->cred,
  322. perm, flags);
  323. if (IS_ERR(key))
  324. goto alloc_failed;
  325. set_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags);
  326. if (dest_keyring) {
  327. ret = __key_link_begin(dest_keyring, &ctx->index_key, &edit);
  328. if (ret < 0)
  329. goto link_prealloc_failed;
  330. }
  331. /* attach the key to the destination keyring under lock, but we do need
  332. * to do another check just in case someone beat us to it whilst we
  333. * waited for locks */
  334. mutex_lock(&key_construction_mutex);
  335. key_ref = search_process_keyrings(ctx);
  336. if (!IS_ERR(key_ref))
  337. goto key_already_present;
  338. if (dest_keyring)
  339. __key_link(key, &edit);
  340. mutex_unlock(&key_construction_mutex);
  341. if (dest_keyring)
  342. __key_link_end(dest_keyring, &ctx->index_key, edit);
  343. mutex_unlock(&user->cons_lock);
  344. *_key = key;
  345. kleave(" = 0 [%d]", key_serial(key));
  346. return 0;
  347. /* the key is now present - we tell the caller that we found it by
  348. * returning -EINPROGRESS */
  349. key_already_present:
  350. key_put(key);
  351. mutex_unlock(&key_construction_mutex);
  352. key = key_ref_to_ptr(key_ref);
  353. if (dest_keyring) {
  354. ret = __key_link_check_live_key(dest_keyring, key);
  355. if (ret == 0)
  356. __key_link(key, &edit);
  357. __key_link_end(dest_keyring, &ctx->index_key, edit);
  358. if (ret < 0)
  359. goto link_check_failed;
  360. }
  361. mutex_unlock(&user->cons_lock);
  362. *_key = key;
  363. kleave(" = -EINPROGRESS [%d]", key_serial(key));
  364. return -EINPROGRESS;
  365. link_check_failed:
  366. mutex_unlock(&user->cons_lock);
  367. key_put(key);
  368. kleave(" = %d [linkcheck]", ret);
  369. return ret;
  370. link_prealloc_failed:
  371. mutex_unlock(&user->cons_lock);
  372. kleave(" = %d [prelink]", ret);
  373. return ret;
  374. alloc_failed:
  375. mutex_unlock(&user->cons_lock);
  376. kleave(" = %ld", PTR_ERR(key));
  377. return PTR_ERR(key);
  378. }
  379. /*
  380. * Commence key construction.
  381. */
  382. static struct key *construct_key_and_link(struct keyring_search_context *ctx,
  383. const char *callout_info,
  384. size_t callout_len,
  385. void *aux,
  386. struct key *dest_keyring,
  387. unsigned long flags)
  388. {
  389. struct key_user *user;
  390. struct key *key;
  391. int ret;
  392. kenter("");
  393. user = key_user_lookup(current_fsuid());
  394. if (!user)
  395. return ERR_PTR(-ENOMEM);
  396. construct_get_dest_keyring(&dest_keyring);
  397. ret = construct_alloc_key(ctx, dest_keyring, flags, user, &key);
  398. key_user_put(user);
  399. if (ret == 0) {
  400. ret = construct_key(key, callout_info, callout_len, aux,
  401. dest_keyring);
  402. if (ret < 0) {
  403. kdebug("cons failed");
  404. goto construction_failed;
  405. }
  406. } else if (ret == -EINPROGRESS) {
  407. ret = 0;
  408. } else {
  409. goto couldnt_alloc_key;
  410. }
  411. key_put(dest_keyring);
  412. kleave(" = key %d", key_serial(key));
  413. return key;
  414. construction_failed:
  415. key_negate_and_link(key, key_negative_timeout, NULL, NULL);
  416. key_put(key);
  417. couldnt_alloc_key:
  418. key_put(dest_keyring);
  419. kleave(" = %d", ret);
  420. return ERR_PTR(ret);
  421. }
  422. /**
  423. * request_key_and_link - Request a key and cache it in a keyring.
  424. * @type: The type of key we want.
  425. * @description: The searchable description of the key.
  426. * @callout_info: The data to pass to the instantiation upcall (or NULL).
  427. * @callout_len: The length of callout_info.
  428. * @aux: Auxiliary data for the upcall.
  429. * @dest_keyring: Where to cache the key.
  430. * @flags: Flags to key_alloc().
  431. *
  432. * A key matching the specified criteria is searched for in the process's
  433. * keyrings and returned with its usage count incremented if found. Otherwise,
  434. * if callout_info is not NULL, a key will be allocated and some service
  435. * (probably in userspace) will be asked to instantiate it.
  436. *
  437. * If successfully found or created, the key will be linked to the destination
  438. * keyring if one is provided.
  439. *
  440. * Returns a pointer to the key if successful; -EACCES, -ENOKEY, -EKEYREVOKED
  441. * or -EKEYEXPIRED if an inaccessible, negative, revoked or expired key was
  442. * found; -ENOKEY if no key was found and no @callout_info was given; -EDQUOT
  443. * if insufficient key quota was available to create a new key; or -ENOMEM if
  444. * insufficient memory was available.
  445. *
  446. * If the returned key was created, then it may still be under construction,
  447. * and wait_for_key_construction() should be used to wait for that to complete.
  448. */
  449. struct key *request_key_and_link(struct key_type *type,
  450. const char *description,
  451. const void *callout_info,
  452. size_t callout_len,
  453. void *aux,
  454. struct key *dest_keyring,
  455. unsigned long flags)
  456. {
  457. struct keyring_search_context ctx = {
  458. .index_key.type = type,
  459. .index_key.description = description,
  460. .cred = current_cred(),
  461. .match = type->match,
  462. .match_data = description,
  463. .flags = KEYRING_SEARCH_LOOKUP_DIRECT,
  464. };
  465. struct key *key;
  466. key_ref_t key_ref;
  467. int ret;
  468. kenter("%s,%s,%p,%zu,%p,%p,%lx",
  469. ctx.index_key.type->name, ctx.index_key.description,
  470. callout_info, callout_len, aux, dest_keyring, flags);
  471. /* search all the process keyrings for a key */
  472. key_ref = search_process_keyrings(&ctx);
  473. if (!IS_ERR(key_ref)) {
  474. key = key_ref_to_ptr(key_ref);
  475. if (dest_keyring) {
  476. construct_get_dest_keyring(&dest_keyring);
  477. ret = key_link(dest_keyring, key);
  478. key_put(dest_keyring);
  479. if (ret < 0) {
  480. key_put(key);
  481. key = ERR_PTR(ret);
  482. goto error;
  483. }
  484. }
  485. } else if (PTR_ERR(key_ref) != -EAGAIN) {
  486. key = ERR_CAST(key_ref);
  487. } else {
  488. /* the search failed, but the keyrings were searchable, so we
  489. * should consult userspace if we can */
  490. key = ERR_PTR(-ENOKEY);
  491. if (!callout_info)
  492. goto error;
  493. key = construct_key_and_link(&ctx, callout_info, callout_len,
  494. aux, dest_keyring, flags);
  495. }
  496. error:
  497. kleave(" = %p", key);
  498. return key;
  499. }
  500. /**
  501. * wait_for_key_construction - Wait for construction of a key to complete
  502. * @key: The key being waited for.
  503. * @intr: Whether to wait interruptibly.
  504. *
  505. * Wait for a key to finish being constructed.
  506. *
  507. * Returns 0 if successful; -ERESTARTSYS if the wait was interrupted; -ENOKEY
  508. * if the key was negated; or -EKEYREVOKED or -EKEYEXPIRED if the key was
  509. * revoked or expired.
  510. */
  511. int wait_for_key_construction(struct key *key, bool intr)
  512. {
  513. int ret;
  514. ret = wait_on_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT,
  515. intr ? key_wait_bit_intr : key_wait_bit,
  516. intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
  517. if (ret < 0)
  518. return ret;
  519. if (test_bit(KEY_FLAG_NEGATIVE, &key->flags)) {
  520. smp_rmb();
  521. return key->type_data.reject_error;
  522. }
  523. return key_validate(key);
  524. }
  525. EXPORT_SYMBOL(wait_for_key_construction);
  526. /**
  527. * request_key - Request a key and wait for construction
  528. * @type: Type of key.
  529. * @description: The searchable description of the key.
  530. * @callout_info: The data to pass to the instantiation upcall (or NULL).
  531. *
  532. * As for request_key_and_link() except that it does not add the returned key
  533. * to a keyring if found, new keys are always allocated in the user's quota,
  534. * the callout_info must be a NUL-terminated string and no auxiliary data can
  535. * be passed.
  536. *
  537. * Furthermore, it then works as wait_for_key_construction() to wait for the
  538. * completion of keys undergoing construction with a non-interruptible wait.
  539. */
  540. struct key *request_key(struct key_type *type,
  541. const char *description,
  542. const char *callout_info)
  543. {
  544. struct key *key;
  545. size_t callout_len = 0;
  546. int ret;
  547. if (callout_info)
  548. callout_len = strlen(callout_info);
  549. key = request_key_and_link(type, description, callout_info, callout_len,
  550. NULL, NULL, KEY_ALLOC_IN_QUOTA);
  551. if (!IS_ERR(key)) {
  552. ret = wait_for_key_construction(key, false);
  553. if (ret < 0) {
  554. key_put(key);
  555. return ERR_PTR(ret);
  556. }
  557. }
  558. return key;
  559. }
  560. EXPORT_SYMBOL(request_key);
  561. /**
  562. * request_key_with_auxdata - Request a key with auxiliary data for the upcaller
  563. * @type: The type of key we want.
  564. * @description: The searchable description of the key.
  565. * @callout_info: The data to pass to the instantiation upcall (or NULL).
  566. * @callout_len: The length of callout_info.
  567. * @aux: Auxiliary data for the upcall.
  568. *
  569. * As for request_key_and_link() except that it does not add the returned key
  570. * to a keyring if found and new keys are always allocated in the user's quota.
  571. *
  572. * Furthermore, it then works as wait_for_key_construction() to wait for the
  573. * completion of keys undergoing construction with a non-interruptible wait.
  574. */
  575. struct key *request_key_with_auxdata(struct key_type *type,
  576. const char *description,
  577. const void *callout_info,
  578. size_t callout_len,
  579. void *aux)
  580. {
  581. struct key *key;
  582. int ret;
  583. key = request_key_and_link(type, description, callout_info, callout_len,
  584. aux, NULL, KEY_ALLOC_IN_QUOTA);
  585. if (!IS_ERR(key)) {
  586. ret = wait_for_key_construction(key, false);
  587. if (ret < 0) {
  588. key_put(key);
  589. return ERR_PTR(ret);
  590. }
  591. }
  592. return key;
  593. }
  594. EXPORT_SYMBOL(request_key_with_auxdata);
  595. /*
  596. * request_key_async - Request a key (allow async construction)
  597. * @type: Type of key.
  598. * @description: The searchable description of the key.
  599. * @callout_info: The data to pass to the instantiation upcall (or NULL).
  600. * @callout_len: The length of callout_info.
  601. *
  602. * As for request_key_and_link() except that it does not add the returned key
  603. * to a keyring if found, new keys are always allocated in the user's quota and
  604. * no auxiliary data can be passed.
  605. *
  606. * The caller should call wait_for_key_construction() to wait for the
  607. * completion of the returned key if it is still undergoing construction.
  608. */
  609. struct key *request_key_async(struct key_type *type,
  610. const char *description,
  611. const void *callout_info,
  612. size_t callout_len)
  613. {
  614. return request_key_and_link(type, description, callout_info,
  615. callout_len, NULL, NULL,
  616. KEY_ALLOC_IN_QUOTA);
  617. }
  618. EXPORT_SYMBOL(request_key_async);
  619. /*
  620. * request a key with auxiliary data for the upcaller (allow async construction)
  621. * @type: Type of key.
  622. * @description: The searchable description of the key.
  623. * @callout_info: The data to pass to the instantiation upcall (or NULL).
  624. * @callout_len: The length of callout_info.
  625. * @aux: Auxiliary data for the upcall.
  626. *
  627. * As for request_key_and_link() except that it does not add the returned key
  628. * to a keyring if found and new keys are always allocated in the user's quota.
  629. *
  630. * The caller should call wait_for_key_construction() to wait for the
  631. * completion of the returned key if it is still undergoing construction.
  632. */
  633. struct key *request_key_async_with_auxdata(struct key_type *type,
  634. const char *description,
  635. const void *callout_info,
  636. size_t callout_len,
  637. void *aux)
  638. {
  639. return request_key_and_link(type, description, callout_info,
  640. callout_len, aux, NULL, KEY_ALLOC_IN_QUOTA);
  641. }
  642. EXPORT_SYMBOL(request_key_async_with_auxdata);