request_key.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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/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. * call to complete the construction of a key
  39. */
  40. void complete_request_key(struct key_construction *cons, int error)
  41. {
  42. kenter("{%d,%d},%d", cons->key->serial, cons->authkey->serial, error);
  43. if (error < 0)
  44. key_negate_and_link(cons->key, key_negative_timeout, NULL,
  45. cons->authkey);
  46. else
  47. key_revoke(cons->authkey);
  48. key_put(cons->key);
  49. key_put(cons->authkey);
  50. kfree(cons);
  51. }
  52. EXPORT_SYMBOL(complete_request_key);
  53. static int umh_keys_init(struct subprocess_info *info)
  54. {
  55. struct cred *cred = (struct cred*)current_cred();
  56. struct key *keyring = info->data;
  57. /*
  58. * This is called in context of freshly forked kthread before
  59. * kernel_execve(), we can just change our ->session_keyring.
  60. */
  61. return install_session_keyring_to_cred(cred, keyring);
  62. }
  63. static void umh_keys_cleanup(struct subprocess_info *info)
  64. {
  65. struct key *keyring = info->data;
  66. key_put(keyring);
  67. }
  68. static int call_usermodehelper_keys(char *path, char **argv, char **envp,
  69. struct key *session_keyring, enum umh_wait wait)
  70. {
  71. gfp_t gfp_mask = (wait == UMH_NO_WAIT) ? GFP_ATOMIC : GFP_KERNEL;
  72. struct subprocess_info *info =
  73. call_usermodehelper_setup(path, argv, envp, gfp_mask);
  74. if (!info)
  75. return -ENOMEM;
  76. call_usermodehelper_setfns(info, umh_keys_init, umh_keys_cleanup,
  77. key_get(session_keyring));
  78. return call_usermodehelper_exec(info, wait);
  79. }
  80. /*
  81. * request userspace finish the construction of a key
  82. * - execute "/sbin/request-key <op> <key> <uid> <gid> <keyring> <keyring> <keyring>"
  83. */
  84. static int call_sbin_request_key(struct key_construction *cons,
  85. const char *op,
  86. void *aux)
  87. {
  88. const struct cred *cred = current_cred();
  89. key_serial_t prkey, sskey;
  90. struct key *key = cons->key, *authkey = cons->authkey, *keyring,
  91. *session;
  92. char *argv[9], *envp[3], uid_str[12], gid_str[12];
  93. char key_str[12], keyring_str[3][12];
  94. char desc[20];
  95. int ret, i;
  96. kenter("{%d},{%d},%s", key->serial, authkey->serial, op);
  97. ret = install_user_keyrings();
  98. if (ret < 0)
  99. goto error_alloc;
  100. /* allocate a new session keyring */
  101. sprintf(desc, "_req.%u", key->serial);
  102. cred = get_current_cred();
  103. keyring = keyring_alloc(desc, cred->fsuid, cred->fsgid, cred,
  104. KEY_ALLOC_QUOTA_OVERRUN, NULL);
  105. put_cred(cred);
  106. if (IS_ERR(keyring)) {
  107. ret = PTR_ERR(keyring);
  108. goto error_alloc;
  109. }
  110. /* attach the auth key to the session keyring */
  111. ret = key_link(keyring, authkey);
  112. if (ret < 0)
  113. goto error_link;
  114. /* record the UID and GID */
  115. sprintf(uid_str, "%d", cred->fsuid);
  116. sprintf(gid_str, "%d", cred->fsgid);
  117. /* we say which key is under construction */
  118. sprintf(key_str, "%d", key->serial);
  119. /* we specify the process's default keyrings */
  120. sprintf(keyring_str[0], "%d",
  121. cred->thread_keyring ? cred->thread_keyring->serial : 0);
  122. prkey = 0;
  123. if (cred->tgcred->process_keyring)
  124. prkey = cred->tgcred->process_keyring->serial;
  125. sprintf(keyring_str[1], "%d", prkey);
  126. rcu_read_lock();
  127. session = rcu_dereference(cred->tgcred->session_keyring);
  128. if (!session)
  129. session = cred->user->session_keyring;
  130. sskey = session->serial;
  131. rcu_read_unlock();
  132. sprintf(keyring_str[2], "%d", sskey);
  133. /* set up a minimal environment */
  134. i = 0;
  135. envp[i++] = "HOME=/";
  136. envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
  137. envp[i] = NULL;
  138. /* set up the argument list */
  139. i = 0;
  140. argv[i++] = "/sbin/request-key";
  141. argv[i++] = (char *) op;
  142. argv[i++] = key_str;
  143. argv[i++] = uid_str;
  144. argv[i++] = gid_str;
  145. argv[i++] = keyring_str[0];
  146. argv[i++] = keyring_str[1];
  147. argv[i++] = keyring_str[2];
  148. argv[i] = NULL;
  149. /* do it */
  150. ret = call_usermodehelper_keys(argv[0], argv, envp, keyring,
  151. UMH_WAIT_PROC);
  152. kdebug("usermode -> 0x%x", ret);
  153. if (ret >= 0) {
  154. /* ret is the exit/wait code */
  155. if (test_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags) ||
  156. key_validate(key) < 0)
  157. ret = -ENOKEY;
  158. else
  159. /* ignore any errors from userspace if the key was
  160. * instantiated */
  161. ret = 0;
  162. }
  163. error_link:
  164. key_put(keyring);
  165. error_alloc:
  166. complete_request_key(cons, ret);
  167. kleave(" = %d", ret);
  168. return ret;
  169. }
  170. /*
  171. * call out to userspace for key construction
  172. * - we ignore program failure and go on key status instead
  173. */
  174. static int construct_key(struct key *key, const void *callout_info,
  175. size_t callout_len, void *aux,
  176. struct key *dest_keyring)
  177. {
  178. struct key_construction *cons;
  179. request_key_actor_t actor;
  180. struct key *authkey;
  181. int ret;
  182. kenter("%d,%p,%zu,%p", key->serial, callout_info, callout_len, aux);
  183. cons = kmalloc(sizeof(*cons), GFP_KERNEL);
  184. if (!cons)
  185. return -ENOMEM;
  186. /* allocate an authorisation key */
  187. authkey = request_key_auth_new(key, callout_info, callout_len,
  188. dest_keyring);
  189. if (IS_ERR(authkey)) {
  190. kfree(cons);
  191. ret = PTR_ERR(authkey);
  192. authkey = NULL;
  193. } else {
  194. cons->authkey = key_get(authkey);
  195. cons->key = key_get(key);
  196. /* make the call */
  197. actor = call_sbin_request_key;
  198. if (key->type->request_key)
  199. actor = key->type->request_key;
  200. ret = actor(cons, "create", aux);
  201. /* check that the actor called complete_request_key() prior to
  202. * returning an error */
  203. WARN_ON(ret < 0 &&
  204. !test_bit(KEY_FLAG_REVOKED, &authkey->flags));
  205. key_put(authkey);
  206. }
  207. kleave(" = %d", ret);
  208. return ret;
  209. }
  210. /*
  211. * get the appropriate destination keyring for the request
  212. * - we return whatever keyring we select with an extra reference upon it which
  213. * the caller must release
  214. */
  215. static void construct_get_dest_keyring(struct key **_dest_keyring)
  216. {
  217. struct request_key_auth *rka;
  218. const struct cred *cred = current_cred();
  219. struct key *dest_keyring = *_dest_keyring, *authkey;
  220. kenter("%p", dest_keyring);
  221. /* find the appropriate keyring */
  222. if (dest_keyring) {
  223. /* the caller supplied one */
  224. key_get(dest_keyring);
  225. } else {
  226. /* use a default keyring; falling through the cases until we
  227. * find one that we actually have */
  228. switch (cred->jit_keyring) {
  229. case KEY_REQKEY_DEFL_DEFAULT:
  230. case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
  231. if (cred->request_key_auth) {
  232. authkey = cred->request_key_auth;
  233. down_read(&authkey->sem);
  234. rka = authkey->payload.data;
  235. if (!test_bit(KEY_FLAG_REVOKED,
  236. &authkey->flags))
  237. dest_keyring =
  238. key_get(rka->dest_keyring);
  239. up_read(&authkey->sem);
  240. if (dest_keyring)
  241. break;
  242. }
  243. case KEY_REQKEY_DEFL_THREAD_KEYRING:
  244. dest_keyring = key_get(cred->thread_keyring);
  245. if (dest_keyring)
  246. break;
  247. case KEY_REQKEY_DEFL_PROCESS_KEYRING:
  248. dest_keyring = key_get(cred->tgcred->process_keyring);
  249. if (dest_keyring)
  250. break;
  251. case KEY_REQKEY_DEFL_SESSION_KEYRING:
  252. rcu_read_lock();
  253. dest_keyring = key_get(
  254. rcu_dereference(cred->tgcred->session_keyring));
  255. rcu_read_unlock();
  256. if (dest_keyring)
  257. break;
  258. case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
  259. dest_keyring =
  260. key_get(cred->user->session_keyring);
  261. break;
  262. case KEY_REQKEY_DEFL_USER_KEYRING:
  263. dest_keyring = key_get(cred->user->uid_keyring);
  264. break;
  265. case KEY_REQKEY_DEFL_GROUP_KEYRING:
  266. default:
  267. BUG();
  268. }
  269. }
  270. *_dest_keyring = dest_keyring;
  271. kleave(" [dk %d]", key_serial(dest_keyring));
  272. return;
  273. }
  274. /*
  275. * allocate a new key in under-construction state and attempt to link it in to
  276. * the requested place
  277. * - may return a key that's already under construction instead
  278. */
  279. static int construct_alloc_key(struct key_type *type,
  280. const char *description,
  281. struct key *dest_keyring,
  282. unsigned long flags,
  283. struct key_user *user,
  284. struct key **_key)
  285. {
  286. struct keyring_list *prealloc;
  287. const struct cred *cred = current_cred();
  288. struct key *key;
  289. key_ref_t key_ref;
  290. int ret;
  291. kenter("%s,%s,,,", type->name, description);
  292. *_key = NULL;
  293. mutex_lock(&user->cons_lock);
  294. key = key_alloc(type, description, cred->fsuid, cred->fsgid, cred,
  295. KEY_POS_ALL, flags);
  296. if (IS_ERR(key))
  297. goto alloc_failed;
  298. set_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags);
  299. if (dest_keyring) {
  300. ret = __key_link_begin(dest_keyring, type, description,
  301. &prealloc);
  302. if (ret < 0)
  303. goto link_prealloc_failed;
  304. }
  305. /* attach the key to the destination keyring under lock, but we do need
  306. * to do another check just in case someone beat us to it whilst we
  307. * waited for locks */
  308. mutex_lock(&key_construction_mutex);
  309. key_ref = search_process_keyrings(type, description, type->match, cred);
  310. if (!IS_ERR(key_ref))
  311. goto key_already_present;
  312. if (dest_keyring)
  313. __key_link(dest_keyring, key, &prealloc);
  314. mutex_unlock(&key_construction_mutex);
  315. if (dest_keyring)
  316. __key_link_end(dest_keyring, type, prealloc);
  317. mutex_unlock(&user->cons_lock);
  318. *_key = key;
  319. kleave(" = 0 [%d]", key_serial(key));
  320. return 0;
  321. /* the key is now present - we tell the caller that we found it by
  322. * returning -EINPROGRESS */
  323. key_already_present:
  324. key_put(key);
  325. mutex_unlock(&key_construction_mutex);
  326. key = key_ref_to_ptr(key_ref);
  327. if (dest_keyring) {
  328. ret = __key_link_check_live_key(dest_keyring, key);
  329. if (ret == 0)
  330. __key_link(dest_keyring, key, &prealloc);
  331. __key_link_end(dest_keyring, type, prealloc);
  332. if (ret < 0)
  333. goto link_check_failed;
  334. }
  335. mutex_unlock(&user->cons_lock);
  336. *_key = key;
  337. kleave(" = -EINPROGRESS [%d]", key_serial(key));
  338. return -EINPROGRESS;
  339. link_check_failed:
  340. mutex_unlock(&user->cons_lock);
  341. key_put(key);
  342. kleave(" = %d [linkcheck]", ret);
  343. return ret;
  344. link_prealloc_failed:
  345. mutex_unlock(&user->cons_lock);
  346. kleave(" = %d [prelink]", ret);
  347. return ret;
  348. alloc_failed:
  349. mutex_unlock(&user->cons_lock);
  350. kleave(" = %ld", PTR_ERR(key));
  351. return PTR_ERR(key);
  352. }
  353. /*
  354. * commence key construction
  355. */
  356. static struct key *construct_key_and_link(struct key_type *type,
  357. const char *description,
  358. const char *callout_info,
  359. size_t callout_len,
  360. void *aux,
  361. struct key *dest_keyring,
  362. unsigned long flags)
  363. {
  364. struct key_user *user;
  365. struct key *key;
  366. int ret;
  367. kenter("");
  368. user = key_user_lookup(current_fsuid(), current_user_ns());
  369. if (!user)
  370. return ERR_PTR(-ENOMEM);
  371. construct_get_dest_keyring(&dest_keyring);
  372. ret = construct_alloc_key(type, description, dest_keyring, flags, user,
  373. &key);
  374. key_user_put(user);
  375. if (ret == 0) {
  376. ret = construct_key(key, callout_info, callout_len, aux,
  377. dest_keyring);
  378. if (ret < 0) {
  379. kdebug("cons failed");
  380. goto construction_failed;
  381. }
  382. } else if (ret == -EINPROGRESS) {
  383. ret = 0;
  384. } else {
  385. key = ERR_PTR(ret);
  386. }
  387. key_put(dest_keyring);
  388. kleave(" = key %d", key_serial(key));
  389. return key;
  390. construction_failed:
  391. key_negate_and_link(key, key_negative_timeout, NULL, NULL);
  392. key_put(key);
  393. key_put(dest_keyring);
  394. kleave(" = %d", ret);
  395. return ERR_PTR(ret);
  396. }
  397. /*
  398. * request a key
  399. * - search the process's keyrings
  400. * - check the list of keys being created or updated
  401. * - call out to userspace for a key if supplementary info was provided
  402. * - cache the key in an appropriate keyring
  403. */
  404. struct key *request_key_and_link(struct key_type *type,
  405. const char *description,
  406. const void *callout_info,
  407. size_t callout_len,
  408. void *aux,
  409. struct key *dest_keyring,
  410. unsigned long flags)
  411. {
  412. const struct cred *cred = current_cred();
  413. struct key *key;
  414. key_ref_t key_ref;
  415. int ret;
  416. kenter("%s,%s,%p,%zu,%p,%p,%lx",
  417. type->name, description, callout_info, callout_len, aux,
  418. dest_keyring, flags);
  419. /* search all the process keyrings for a key */
  420. key_ref = search_process_keyrings(type, description, type->match,
  421. cred);
  422. if (!IS_ERR(key_ref)) {
  423. key = key_ref_to_ptr(key_ref);
  424. if (dest_keyring) {
  425. construct_get_dest_keyring(&dest_keyring);
  426. ret = key_link(dest_keyring, key);
  427. key_put(dest_keyring);
  428. if (ret < 0) {
  429. key_put(key);
  430. key = ERR_PTR(ret);
  431. goto error;
  432. }
  433. }
  434. } else if (PTR_ERR(key_ref) != -EAGAIN) {
  435. key = ERR_CAST(key_ref);
  436. } else {
  437. /* the search failed, but the keyrings were searchable, so we
  438. * should consult userspace if we can */
  439. key = ERR_PTR(-ENOKEY);
  440. if (!callout_info)
  441. goto error;
  442. key = construct_key_and_link(type, description, callout_info,
  443. callout_len, aux, dest_keyring,
  444. flags);
  445. }
  446. error:
  447. kleave(" = %p", key);
  448. return key;
  449. }
  450. /*
  451. * wait for construction of a key to complete
  452. */
  453. int wait_for_key_construction(struct key *key, bool intr)
  454. {
  455. int ret;
  456. ret = wait_on_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT,
  457. intr ? key_wait_bit_intr : key_wait_bit,
  458. intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
  459. if (ret < 0)
  460. return ret;
  461. if (test_bit(KEY_FLAG_NEGATIVE, &key->flags))
  462. return -ENOKEY;
  463. return key_validate(key);
  464. }
  465. EXPORT_SYMBOL(wait_for_key_construction);
  466. /*
  467. * request a key
  468. * - search the process's keyrings
  469. * - check the list of keys being created or updated
  470. * - call out to userspace for a key if supplementary info was provided
  471. * - waits uninterruptible for creation to complete
  472. */
  473. struct key *request_key(struct key_type *type,
  474. const char *description,
  475. const char *callout_info)
  476. {
  477. struct key *key;
  478. size_t callout_len = 0;
  479. int ret;
  480. if (callout_info)
  481. callout_len = strlen(callout_info);
  482. key = request_key_and_link(type, description, callout_info, callout_len,
  483. NULL, NULL, KEY_ALLOC_IN_QUOTA);
  484. if (!IS_ERR(key)) {
  485. ret = wait_for_key_construction(key, false);
  486. if (ret < 0) {
  487. key_put(key);
  488. return ERR_PTR(ret);
  489. }
  490. }
  491. return key;
  492. }
  493. EXPORT_SYMBOL(request_key);
  494. /*
  495. * request a key with auxiliary data for the upcaller
  496. * - search the process's keyrings
  497. * - check the list of keys being created or updated
  498. * - call out to userspace for a key if supplementary info was provided
  499. * - waits uninterruptible for creation to complete
  500. */
  501. struct key *request_key_with_auxdata(struct key_type *type,
  502. const char *description,
  503. const void *callout_info,
  504. size_t callout_len,
  505. void *aux)
  506. {
  507. struct key *key;
  508. int ret;
  509. key = request_key_and_link(type, description, callout_info, callout_len,
  510. aux, NULL, KEY_ALLOC_IN_QUOTA);
  511. if (!IS_ERR(key)) {
  512. ret = wait_for_key_construction(key, false);
  513. if (ret < 0) {
  514. key_put(key);
  515. return ERR_PTR(ret);
  516. }
  517. }
  518. return key;
  519. }
  520. EXPORT_SYMBOL(request_key_with_auxdata);
  521. /*
  522. * request a key (allow async construction)
  523. * - search the process's keyrings
  524. * - check the list of keys being created or updated
  525. * - call out to userspace for a key if supplementary info was provided
  526. */
  527. struct key *request_key_async(struct key_type *type,
  528. const char *description,
  529. const void *callout_info,
  530. size_t callout_len)
  531. {
  532. return request_key_and_link(type, description, callout_info,
  533. callout_len, NULL, NULL,
  534. KEY_ALLOC_IN_QUOTA);
  535. }
  536. EXPORT_SYMBOL(request_key_async);
  537. /*
  538. * request a key with auxiliary data for the upcaller (allow async construction)
  539. * - search the process's keyrings
  540. * - check the list of keys being created or updated
  541. * - call out to userspace for a key if supplementary info was provided
  542. */
  543. struct key *request_key_async_with_auxdata(struct key_type *type,
  544. const char *description,
  545. const void *callout_info,
  546. size_t callout_len,
  547. void *aux)
  548. {
  549. return request_key_and_link(type, description, callout_info,
  550. callout_len, aux, NULL, KEY_ALLOC_IN_QUOTA);
  551. }
  552. EXPORT_SYMBOL(request_key_async_with_auxdata);