request_key.c 14 KB

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