request_key.c 14 KB

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