request_key.c 13 KB

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