request_key.c 12 KB

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