request_key.c 12 KB

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