request_key.c 13 KB

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