request_key.c 13 KB

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