request_key.c 12 KB

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