request_key.c 13 KB

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