request_key.c 22 KB

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