request_key.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /* request_key.c: request a key from userspace
  2. *
  3. * Copyright (C) 2004-5 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. struct key_construction {
  20. struct list_head link; /* link in construction queue */
  21. struct key *key; /* key being constructed */
  22. };
  23. /* when waiting for someone else's keys, you get added to this */
  24. DECLARE_WAIT_QUEUE_HEAD(request_key_conswq);
  25. /*****************************************************************************/
  26. /*
  27. * request userspace finish the construction of a key
  28. * - execute "/sbin/request-key <op> <key> <uid> <gid> <keyring> <keyring> <keyring>"
  29. */
  30. static int call_sbin_request_key(struct key *key,
  31. struct key *authkey,
  32. const char *op)
  33. {
  34. struct task_struct *tsk = current;
  35. key_serial_t prkey, sskey;
  36. struct key *keyring;
  37. char *argv[9], *envp[3], uid_str[12], gid_str[12];
  38. char key_str[12], keyring_str[3][12];
  39. char desc[20];
  40. int ret, i;
  41. kenter("{%d},{%d},%s", key->serial, authkey->serial, op);
  42. /* allocate a new session keyring */
  43. sprintf(desc, "_req.%u", key->serial);
  44. keyring = keyring_alloc(desc, current->fsuid, current->fsgid,
  45. current, 1, NULL);
  46. if (IS_ERR(keyring)) {
  47. ret = PTR_ERR(keyring);
  48. goto error_alloc;
  49. }
  50. /* attach the auth key to the session keyring */
  51. ret = __key_link(keyring, authkey);
  52. if (ret < 0)
  53. goto error_link;
  54. /* record the UID and GID */
  55. sprintf(uid_str, "%d", current->fsuid);
  56. sprintf(gid_str, "%d", current->fsgid);
  57. /* we say which key is under construction */
  58. sprintf(key_str, "%d", key->serial);
  59. /* we specify the process's default keyrings */
  60. sprintf(keyring_str[0], "%d",
  61. tsk->thread_keyring ? tsk->thread_keyring->serial : 0);
  62. prkey = 0;
  63. if (tsk->signal->process_keyring)
  64. prkey = tsk->signal->process_keyring->serial;
  65. sprintf(keyring_str[1], "%d", prkey);
  66. if (tsk->signal->session_keyring) {
  67. rcu_read_lock();
  68. sskey = rcu_dereference(tsk->signal->session_keyring)->serial;
  69. rcu_read_unlock();
  70. }
  71. else {
  72. sskey = tsk->user->session_keyring->serial;
  73. }
  74. sprintf(keyring_str[2], "%d", sskey);
  75. /* set up a minimal environment */
  76. i = 0;
  77. envp[i++] = "HOME=/";
  78. envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
  79. envp[i] = NULL;
  80. /* set up the argument list */
  81. i = 0;
  82. argv[i++] = "/sbin/request-key";
  83. argv[i++] = (char *) op;
  84. argv[i++] = key_str;
  85. argv[i++] = uid_str;
  86. argv[i++] = gid_str;
  87. argv[i++] = keyring_str[0];
  88. argv[i++] = keyring_str[1];
  89. argv[i++] = keyring_str[2];
  90. argv[i] = NULL;
  91. /* do it */
  92. ret = call_usermodehelper_keys(argv[0], argv, envp, keyring, 1);
  93. error_link:
  94. key_put(keyring);
  95. error_alloc:
  96. kleave(" = %d", ret);
  97. return ret;
  98. } /* end call_sbin_request_key() */
  99. /*****************************************************************************/
  100. /*
  101. * call out to userspace for the key
  102. * - called with the construction sem held, but the sem is dropped here
  103. * - we ignore program failure and go on key status instead
  104. */
  105. static struct key *__request_key_construction(struct key_type *type,
  106. const char *description,
  107. const char *callout_info)
  108. {
  109. request_key_actor_t actor;
  110. struct key_construction cons;
  111. struct timespec now;
  112. struct key *key, *authkey;
  113. int ret, negated;
  114. kenter("%s,%s,%s", type->name, description, callout_info);
  115. /* create a key and add it to the queue */
  116. key = key_alloc(type, description,
  117. current->fsuid, current->fsgid,
  118. current, KEY_POS_ALL, 0);
  119. if (IS_ERR(key))
  120. goto alloc_failed;
  121. set_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags);
  122. cons.key = key;
  123. list_add_tail(&cons.link, &key->user->consq);
  124. /* we drop the construction sem here on behalf of the caller */
  125. up_write(&key_construction_sem);
  126. /* allocate an authorisation key */
  127. authkey = request_key_auth_new(key, callout_info);
  128. if (IS_ERR(authkey)) {
  129. ret = PTR_ERR(authkey);
  130. authkey = NULL;
  131. goto alloc_authkey_failed;
  132. }
  133. /* make the call */
  134. actor = call_sbin_request_key;
  135. if (type->request_key)
  136. actor = type->request_key;
  137. ret = actor(key, authkey, "create");
  138. if (ret < 0)
  139. goto request_failed;
  140. /* if the key wasn't instantiated, then we want to give an error */
  141. ret = -ENOKEY;
  142. if (!test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
  143. goto request_failed;
  144. key_revoke(authkey);
  145. key_put(authkey);
  146. down_write(&key_construction_sem);
  147. list_del(&cons.link);
  148. up_write(&key_construction_sem);
  149. /* also give an error if the key was negatively instantiated */
  150. check_not_negative:
  151. if (test_bit(KEY_FLAG_NEGATIVE, &key->flags)) {
  152. key_put(key);
  153. key = ERR_PTR(-ENOKEY);
  154. }
  155. out:
  156. kleave(" = %p", key);
  157. return key;
  158. request_failed:
  159. key_revoke(authkey);
  160. key_put(authkey);
  161. alloc_authkey_failed:
  162. /* it wasn't instantiated
  163. * - remove from construction queue
  164. * - mark the key as dead
  165. */
  166. negated = 0;
  167. down_write(&key_construction_sem);
  168. list_del(&cons.link);
  169. /* check it didn't get instantiated between the check and the down */
  170. if (!test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
  171. set_bit(KEY_FLAG_NEGATIVE, &key->flags);
  172. set_bit(KEY_FLAG_INSTANTIATED, &key->flags);
  173. negated = 1;
  174. }
  175. clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags);
  176. up_write(&key_construction_sem);
  177. if (!negated)
  178. goto check_not_negative; /* surprisingly, the key got
  179. * instantiated */
  180. /* set the timeout and store in the session keyring if we can */
  181. now = current_kernel_time();
  182. key->expiry = now.tv_sec + key_negative_timeout;
  183. if (current->signal->session_keyring) {
  184. struct key *keyring;
  185. rcu_read_lock();
  186. keyring = rcu_dereference(current->signal->session_keyring);
  187. atomic_inc(&keyring->usage);
  188. rcu_read_unlock();
  189. key_link(keyring, key);
  190. key_put(keyring);
  191. }
  192. key_put(key);
  193. /* notify anyone who was waiting */
  194. wake_up_all(&request_key_conswq);
  195. key = ERR_PTR(ret);
  196. goto out;
  197. alloc_failed:
  198. up_write(&key_construction_sem);
  199. goto out;
  200. } /* end __request_key_construction() */
  201. /*****************************************************************************/
  202. /*
  203. * call out to userspace to request the key
  204. * - we check the construction queue first to see if an appropriate key is
  205. * already being constructed by userspace
  206. */
  207. static struct key *request_key_construction(struct key_type *type,
  208. const char *description,
  209. struct key_user *user,
  210. const char *callout_info)
  211. {
  212. struct key_construction *pcons;
  213. struct key *key, *ckey;
  214. DECLARE_WAITQUEUE(myself, current);
  215. kenter("%s,%s,{%d},%s",
  216. type->name, description, user->uid, callout_info);
  217. /* see if there's such a key under construction already */
  218. down_write(&key_construction_sem);
  219. list_for_each_entry(pcons, &user->consq, link) {
  220. ckey = pcons->key;
  221. if (ckey->type != type)
  222. continue;
  223. if (type->match(ckey, description))
  224. goto found_key_under_construction;
  225. }
  226. /* see about getting userspace to construct the key */
  227. key = __request_key_construction(type, description, callout_info);
  228. error:
  229. kleave(" = %p", key);
  230. return key;
  231. /* someone else has the same key under construction
  232. * - we want to keep an eye on their key
  233. */
  234. found_key_under_construction:
  235. atomic_inc(&ckey->usage);
  236. up_write(&key_construction_sem);
  237. /* wait for the key to be completed one way or another */
  238. add_wait_queue(&request_key_conswq, &myself);
  239. for (;;) {
  240. set_current_state(TASK_INTERRUPTIBLE);
  241. if (!test_bit(KEY_FLAG_USER_CONSTRUCT, &ckey->flags))
  242. break;
  243. if (signal_pending(current))
  244. break;
  245. schedule();
  246. }
  247. set_current_state(TASK_RUNNING);
  248. remove_wait_queue(&request_key_conswq, &myself);
  249. /* we'll need to search this process's keyrings to see if the key is
  250. * now there since we can't automatically assume it's also available
  251. * there */
  252. key_put(ckey);
  253. ckey = NULL;
  254. key = NULL; /* request a retry */
  255. goto error;
  256. } /* end request_key_construction() */
  257. /*****************************************************************************/
  258. /*
  259. * link a freshly minted key to an appropriate destination keyring
  260. */
  261. static void request_key_link(struct key *key, struct key *dest_keyring)
  262. {
  263. struct task_struct *tsk = current;
  264. struct key *drop = NULL;
  265. kenter("{%d},%p", key->serial, dest_keyring);
  266. /* find the appropriate keyring */
  267. if (!dest_keyring) {
  268. switch (tsk->jit_keyring) {
  269. case KEY_REQKEY_DEFL_DEFAULT:
  270. case KEY_REQKEY_DEFL_THREAD_KEYRING:
  271. dest_keyring = tsk->thread_keyring;
  272. if (dest_keyring)
  273. break;
  274. case KEY_REQKEY_DEFL_PROCESS_KEYRING:
  275. dest_keyring = tsk->signal->process_keyring;
  276. if (dest_keyring)
  277. break;
  278. case KEY_REQKEY_DEFL_SESSION_KEYRING:
  279. rcu_read_lock();
  280. dest_keyring = key_get(
  281. rcu_dereference(tsk->signal->session_keyring));
  282. rcu_read_unlock();
  283. drop = dest_keyring;
  284. if (dest_keyring)
  285. break;
  286. case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
  287. dest_keyring = current->user->session_keyring;
  288. break;
  289. case KEY_REQKEY_DEFL_USER_KEYRING:
  290. dest_keyring = current->user->uid_keyring;
  291. break;
  292. case KEY_REQKEY_DEFL_GROUP_KEYRING:
  293. default:
  294. BUG();
  295. }
  296. }
  297. /* and attach the key to it */
  298. key_link(dest_keyring, key);
  299. key_put(drop);
  300. kleave("");
  301. } /* end request_key_link() */
  302. /*****************************************************************************/
  303. /*
  304. * request a key
  305. * - search the process's keyrings
  306. * - check the list of keys being created or updated
  307. * - call out to userspace for a key if supplementary info was provided
  308. * - cache the key in an appropriate keyring
  309. */
  310. struct key *request_key_and_link(struct key_type *type,
  311. const char *description,
  312. const char *callout_info,
  313. struct key *dest_keyring)
  314. {
  315. struct key_user *user;
  316. struct key *key;
  317. key_ref_t key_ref;
  318. kenter("%s,%s,%s,%p",
  319. type->name, description, callout_info, dest_keyring);
  320. /* search all the process keyrings for a key */
  321. key_ref = search_process_keyrings(type, description, type->match,
  322. current);
  323. kdebug("search 1: %p", key_ref);
  324. if (!IS_ERR(key_ref)) {
  325. key = key_ref_to_ptr(key_ref);
  326. }
  327. else if (PTR_ERR(key_ref) != -EAGAIN) {
  328. key = ERR_PTR(PTR_ERR(key_ref));
  329. }
  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. /* - get hold of the user's construction queue */
  337. user = key_user_lookup(current->fsuid);
  338. if (!user)
  339. goto nomem;
  340. for (;;) {
  341. if (signal_pending(current))
  342. goto interrupted;
  343. /* ask userspace (returns NULL if it waited on a key
  344. * being constructed) */
  345. key = request_key_construction(type, description,
  346. user, callout_info);
  347. if (key)
  348. break;
  349. /* someone else made the key we want, so we need to
  350. * search again as it might now be available to us */
  351. key_ref = search_process_keyrings(type, description,
  352. type->match,
  353. current);
  354. kdebug("search 2: %p", key_ref);
  355. if (!IS_ERR(key_ref)) {
  356. key = key_ref_to_ptr(key_ref);
  357. break;
  358. }
  359. if (PTR_ERR(key_ref) != -EAGAIN) {
  360. key = ERR_PTR(PTR_ERR(key_ref));
  361. break;
  362. }
  363. }
  364. key_user_put(user);
  365. /* link the new key into the appropriate keyring */
  366. if (!IS_ERR(key))
  367. request_key_link(key, dest_keyring);
  368. }
  369. error:
  370. kleave(" = %p", key);
  371. return key;
  372. nomem:
  373. key = ERR_PTR(-ENOMEM);
  374. goto error;
  375. interrupted:
  376. key_user_put(user);
  377. key = ERR_PTR(-EINTR);
  378. goto error;
  379. } /* end request_key_and_link() */
  380. /*****************************************************************************/
  381. /*
  382. * request a key
  383. * - search the process's keyrings
  384. * - check the list of keys being created or updated
  385. * - call out to userspace for a key if supplementary info was provided
  386. */
  387. struct key *request_key(struct key_type *type,
  388. const char *description,
  389. const char *callout_info)
  390. {
  391. return request_key_and_link(type, description, callout_info, NULL);
  392. } /* end request_key() */
  393. EXPORT_SYMBOL(request_key);