request_key.c 13 KB

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