request_key.c 12 KB

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