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