process_keys.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  1. /* Manage a process's keyrings
  2. *
  3. * Copyright (C) 2004-2005, 2008 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/init.h>
  13. #include <linux/sched.h>
  14. #include <linux/keyctl.h>
  15. #include <linux/fs.h>
  16. #include <linux/err.h>
  17. #include <linux/mutex.h>
  18. #include <linux/security.h>
  19. #include <linux/user_namespace.h>
  20. #include <asm/uaccess.h>
  21. #include "internal.h"
  22. /* Session keyring create vs join semaphore */
  23. static DEFINE_MUTEX(key_session_mutex);
  24. /* User keyring creation semaphore */
  25. static DEFINE_MUTEX(key_user_keyring_mutex);
  26. /* The root user's tracking struct */
  27. struct key_user root_key_user = {
  28. .usage = ATOMIC_INIT(3),
  29. .cons_lock = __MUTEX_INITIALIZER(root_key_user.cons_lock),
  30. .lock = __SPIN_LOCK_UNLOCKED(root_key_user.lock),
  31. .nkeys = ATOMIC_INIT(2),
  32. .nikeys = ATOMIC_INIT(2),
  33. .uid = 0,
  34. .user_ns = &init_user_ns,
  35. };
  36. /*
  37. * Install the user and user session keyrings for the current process's UID.
  38. */
  39. int install_user_keyrings(void)
  40. {
  41. struct user_struct *user;
  42. const struct cred *cred;
  43. struct key *uid_keyring, *session_keyring;
  44. char buf[20];
  45. int ret;
  46. cred = current_cred();
  47. user = cred->user;
  48. kenter("%p{%u}", user, user->uid);
  49. if (user->uid_keyring && user->session_keyring) {
  50. kleave(" = 0 [exist]");
  51. return 0;
  52. }
  53. mutex_lock(&key_user_keyring_mutex);
  54. ret = 0;
  55. if (!user->uid_keyring) {
  56. /* get the UID-specific keyring
  57. * - there may be one in existence already as it may have been
  58. * pinned by a session, but the user_struct pointing to it
  59. * may have been destroyed by setuid */
  60. sprintf(buf, "_uid.%u", user->uid);
  61. uid_keyring = find_keyring_by_name(buf, true);
  62. if (IS_ERR(uid_keyring)) {
  63. uid_keyring = keyring_alloc(buf, user->uid, (gid_t) -1,
  64. cred, KEY_ALLOC_IN_QUOTA,
  65. NULL);
  66. if (IS_ERR(uid_keyring)) {
  67. ret = PTR_ERR(uid_keyring);
  68. goto error;
  69. }
  70. }
  71. /* get a default session keyring (which might also exist
  72. * already) */
  73. sprintf(buf, "_uid_ses.%u", user->uid);
  74. session_keyring = find_keyring_by_name(buf, true);
  75. if (IS_ERR(session_keyring)) {
  76. session_keyring =
  77. keyring_alloc(buf, user->uid, (gid_t) -1,
  78. cred, KEY_ALLOC_IN_QUOTA, NULL);
  79. if (IS_ERR(session_keyring)) {
  80. ret = PTR_ERR(session_keyring);
  81. goto error_release;
  82. }
  83. /* we install a link from the user session keyring to
  84. * the user keyring */
  85. ret = key_link(session_keyring, uid_keyring);
  86. if (ret < 0)
  87. goto error_release_both;
  88. }
  89. /* install the keyrings */
  90. user->uid_keyring = uid_keyring;
  91. user->session_keyring = session_keyring;
  92. }
  93. mutex_unlock(&key_user_keyring_mutex);
  94. kleave(" = 0");
  95. return 0;
  96. error_release_both:
  97. key_put(session_keyring);
  98. error_release:
  99. key_put(uid_keyring);
  100. error:
  101. mutex_unlock(&key_user_keyring_mutex);
  102. kleave(" = %d", ret);
  103. return ret;
  104. }
  105. /*
  106. * Install a thread keyring to the given credentials struct if it didn't have
  107. * one already. This is allowed to overrun the quota.
  108. *
  109. * Return: 0 if a thread keyring is now present; -errno on failure.
  110. */
  111. int install_thread_keyring_to_cred(struct cred *new)
  112. {
  113. struct key *keyring;
  114. if (new->thread_keyring)
  115. return 0;
  116. keyring = keyring_alloc("_tid", new->uid, new->gid, new,
  117. KEY_ALLOC_QUOTA_OVERRUN, NULL);
  118. if (IS_ERR(keyring))
  119. return PTR_ERR(keyring);
  120. new->thread_keyring = keyring;
  121. return 0;
  122. }
  123. /*
  124. * Install a thread keyring to the current task if it didn't have one already.
  125. *
  126. * Return: 0 if a thread keyring is now present; -errno on failure.
  127. */
  128. static int install_thread_keyring(void)
  129. {
  130. struct cred *new;
  131. int ret;
  132. new = prepare_creds();
  133. if (!new)
  134. return -ENOMEM;
  135. ret = install_thread_keyring_to_cred(new);
  136. if (ret < 0) {
  137. abort_creds(new);
  138. return ret;
  139. }
  140. return commit_creds(new);
  141. }
  142. /*
  143. * Install a process keyring to the given credentials struct if it didn't have
  144. * one already. This is allowed to overrun the quota.
  145. *
  146. * Return: 0 if a process keyring is now present; -errno on failure.
  147. */
  148. int install_process_keyring_to_cred(struct cred *new)
  149. {
  150. struct key *keyring;
  151. int ret;
  152. if (new->tgcred->process_keyring)
  153. return 0;
  154. keyring = keyring_alloc("_pid", new->uid, new->gid,
  155. new, KEY_ALLOC_QUOTA_OVERRUN, NULL);
  156. if (IS_ERR(keyring))
  157. return PTR_ERR(keyring);
  158. spin_lock_irq(&new->tgcred->lock);
  159. if (!new->tgcred->process_keyring) {
  160. new->tgcred->process_keyring = keyring;
  161. keyring = NULL;
  162. ret = 0;
  163. } else {
  164. ret = -EEXIST;
  165. }
  166. spin_unlock_irq(&new->tgcred->lock);
  167. key_put(keyring);
  168. return ret;
  169. }
  170. /*
  171. * Install a process keyring to the current task if it didn't have one already.
  172. *
  173. * Return: 0 if a process keyring is now present; -errno on failure.
  174. */
  175. static int install_process_keyring(void)
  176. {
  177. struct cred *new;
  178. int ret;
  179. new = prepare_creds();
  180. if (!new)
  181. return -ENOMEM;
  182. ret = install_process_keyring_to_cred(new);
  183. if (ret < 0) {
  184. abort_creds(new);
  185. return ret;
  186. }
  187. return commit_creds(new);
  188. }
  189. /*
  190. * Install the given keyring as the session keyring of the given credentials
  191. * struct, replacing the existing one if any. If the given keyring is NULL,
  192. * then install a new anonymous session keyring.
  193. *
  194. * Return: 0 on success; -errno on failure.
  195. */
  196. int install_session_keyring_to_cred(struct cred *cred, struct key *keyring)
  197. {
  198. unsigned long flags;
  199. struct key *old;
  200. might_sleep();
  201. /* create an empty session keyring */
  202. if (!keyring) {
  203. flags = KEY_ALLOC_QUOTA_OVERRUN;
  204. if (cred->tgcred->session_keyring)
  205. flags = KEY_ALLOC_IN_QUOTA;
  206. keyring = keyring_alloc("_ses", cred->uid, cred->gid,
  207. cred, flags, NULL);
  208. if (IS_ERR(keyring))
  209. return PTR_ERR(keyring);
  210. } else {
  211. atomic_inc(&keyring->usage);
  212. }
  213. /* install the keyring */
  214. spin_lock_irq(&cred->tgcred->lock);
  215. old = cred->tgcred->session_keyring;
  216. rcu_assign_pointer(cred->tgcred->session_keyring, keyring);
  217. spin_unlock_irq(&cred->tgcred->lock);
  218. /* we're using RCU on the pointer, but there's no point synchronising
  219. * on it if it didn't previously point to anything */
  220. if (old) {
  221. synchronize_rcu();
  222. key_put(old);
  223. }
  224. return 0;
  225. }
  226. /*
  227. * Install the given keyring as the session keyring of the current task,
  228. * replacing the existing one if any. If the given keyring is NULL, then
  229. * install a new anonymous session keyring.
  230. *
  231. * Return: 0 on success; -errno on failure.
  232. */
  233. static int install_session_keyring(struct key *keyring)
  234. {
  235. struct cred *new;
  236. int ret;
  237. new = prepare_creds();
  238. if (!new)
  239. return -ENOMEM;
  240. ret = install_session_keyring_to_cred(new, keyring);
  241. if (ret < 0) {
  242. abort_creds(new);
  243. return ret;
  244. }
  245. return commit_creds(new);
  246. }
  247. /*
  248. * Handle the fsuid changing.
  249. */
  250. void key_fsuid_changed(struct task_struct *tsk)
  251. {
  252. /* update the ownership of the thread keyring */
  253. BUG_ON(!tsk->cred);
  254. if (tsk->cred->thread_keyring) {
  255. down_write(&tsk->cred->thread_keyring->sem);
  256. tsk->cred->thread_keyring->uid = tsk->cred->fsuid;
  257. up_write(&tsk->cred->thread_keyring->sem);
  258. }
  259. }
  260. /*
  261. * Handle the fsgid changing.
  262. */
  263. void key_fsgid_changed(struct task_struct *tsk)
  264. {
  265. /* update the ownership of the thread keyring */
  266. BUG_ON(!tsk->cred);
  267. if (tsk->cred->thread_keyring) {
  268. down_write(&tsk->cred->thread_keyring->sem);
  269. tsk->cred->thread_keyring->gid = tsk->cred->fsgid;
  270. up_write(&tsk->cred->thread_keyring->sem);
  271. }
  272. }
  273. /*
  274. * Search the process keyrings attached to the supplied cred for the first
  275. * matching key.
  276. *
  277. * The search criteria are the type and the match function. The description is
  278. * given to the match function as a parameter, but doesn't otherwise influence
  279. * the search. Typically the match function will compare the description
  280. * parameter to the key's description.
  281. *
  282. * This can only search keyrings that grant Search permission to the supplied
  283. * credentials. Keyrings linked to searched keyrings will also be searched if
  284. * they grant Search permission too. Keys can only be found if they grant
  285. * Search permission to the credentials.
  286. *
  287. * Returns a pointer to the key with the key usage count incremented if
  288. * successful, -EAGAIN if we didn't find any matching key or -ENOKEY if we only
  289. * matched negative keys.
  290. *
  291. * In the case of a successful return, the possession attribute is set on the
  292. * returned key reference.
  293. */
  294. key_ref_t search_my_process_keyrings(struct key_type *type,
  295. const void *description,
  296. key_match_func_t match,
  297. bool no_state_check,
  298. const struct cred *cred)
  299. {
  300. key_ref_t key_ref, ret, err;
  301. /* we want to return -EAGAIN or -ENOKEY if any of the keyrings were
  302. * searchable, but we failed to find a key or we found a negative key;
  303. * otherwise we want to return a sample error (probably -EACCES) if
  304. * none of the keyrings were searchable
  305. *
  306. * in terms of priority: success > -ENOKEY > -EAGAIN > other error
  307. */
  308. key_ref = NULL;
  309. ret = NULL;
  310. err = ERR_PTR(-EAGAIN);
  311. /* search the thread keyring first */
  312. if (cred->thread_keyring) {
  313. key_ref = keyring_search_aux(
  314. make_key_ref(cred->thread_keyring, 1),
  315. cred, type, description, match, no_state_check);
  316. if (!IS_ERR(key_ref))
  317. goto found;
  318. switch (PTR_ERR(key_ref)) {
  319. case -EAGAIN: /* no key */
  320. if (ret)
  321. break;
  322. case -ENOKEY: /* negative key */
  323. ret = key_ref;
  324. break;
  325. default:
  326. err = key_ref;
  327. break;
  328. }
  329. }
  330. /* search the process keyring second */
  331. if (cred->tgcred->process_keyring) {
  332. key_ref = keyring_search_aux(
  333. make_key_ref(cred->tgcred->process_keyring, 1),
  334. cred, type, description, match, no_state_check);
  335. if (!IS_ERR(key_ref))
  336. goto found;
  337. switch (PTR_ERR(key_ref)) {
  338. case -EAGAIN: /* no key */
  339. if (ret)
  340. break;
  341. case -ENOKEY: /* negative key */
  342. ret = key_ref;
  343. break;
  344. default:
  345. err = key_ref;
  346. break;
  347. }
  348. }
  349. /* search the session keyring */
  350. if (cred->tgcred->session_keyring) {
  351. rcu_read_lock();
  352. key_ref = keyring_search_aux(
  353. make_key_ref(rcu_dereference(
  354. cred->tgcred->session_keyring),
  355. 1),
  356. cred, type, description, match, no_state_check);
  357. rcu_read_unlock();
  358. if (!IS_ERR(key_ref))
  359. goto found;
  360. switch (PTR_ERR(key_ref)) {
  361. case -EAGAIN: /* no key */
  362. if (ret)
  363. break;
  364. case -ENOKEY: /* negative key */
  365. ret = key_ref;
  366. break;
  367. default:
  368. err = key_ref;
  369. break;
  370. }
  371. }
  372. /* or search the user-session keyring */
  373. else if (cred->user->session_keyring) {
  374. key_ref = keyring_search_aux(
  375. make_key_ref(cred->user->session_keyring, 1),
  376. cred, type, description, match, no_state_check);
  377. if (!IS_ERR(key_ref))
  378. goto found;
  379. switch (PTR_ERR(key_ref)) {
  380. case -EAGAIN: /* no key */
  381. if (ret)
  382. break;
  383. case -ENOKEY: /* negative key */
  384. ret = key_ref;
  385. break;
  386. default:
  387. err = key_ref;
  388. break;
  389. }
  390. }
  391. /* no key - decide on the error we're going to go for */
  392. key_ref = ret ? ret : err;
  393. found:
  394. return key_ref;
  395. }
  396. /*
  397. * Search the process keyrings attached to the supplied cred for the first
  398. * matching key in the manner of search_my_process_keyrings(), but also search
  399. * the keys attached to the assumed authorisation key using its credentials if
  400. * one is available.
  401. *
  402. * Return same as search_my_process_keyrings().
  403. */
  404. key_ref_t search_process_keyrings(struct key_type *type,
  405. const void *description,
  406. key_match_func_t match,
  407. const struct cred *cred)
  408. {
  409. struct request_key_auth *rka;
  410. key_ref_t key_ref, ret = ERR_PTR(-EACCES), err;
  411. might_sleep();
  412. key_ref = search_my_process_keyrings(type, description, match,
  413. false, cred);
  414. if (!IS_ERR(key_ref))
  415. goto found;
  416. err = key_ref;
  417. /* if this process has an instantiation authorisation key, then we also
  418. * search the keyrings of the process mentioned there
  419. * - we don't permit access to request_key auth keys via this method
  420. */
  421. if (cred->request_key_auth &&
  422. cred == current_cred() &&
  423. type != &key_type_request_key_auth
  424. ) {
  425. /* defend against the auth key being revoked */
  426. down_read(&cred->request_key_auth->sem);
  427. if (key_validate(cred->request_key_auth) == 0) {
  428. rka = cred->request_key_auth->payload.data;
  429. key_ref = search_process_keyrings(type, description,
  430. match, rka->cred);
  431. up_read(&cred->request_key_auth->sem);
  432. if (!IS_ERR(key_ref))
  433. goto found;
  434. ret = key_ref;
  435. } else {
  436. up_read(&cred->request_key_auth->sem);
  437. }
  438. }
  439. /* no key - decide on the error we're going to go for */
  440. if (err == ERR_PTR(-ENOKEY) || ret == ERR_PTR(-ENOKEY))
  441. key_ref = ERR_PTR(-ENOKEY);
  442. else if (err == ERR_PTR(-EACCES))
  443. key_ref = ret;
  444. else
  445. key_ref = err;
  446. found:
  447. return key_ref;
  448. }
  449. /*
  450. * See if the key we're looking at is the target key.
  451. */
  452. int lookup_user_key_possessed(const struct key *key, const void *target)
  453. {
  454. return key == target;
  455. }
  456. /*
  457. * Look up a key ID given us by userspace with a given permissions mask to get
  458. * the key it refers to.
  459. *
  460. * Flags can be passed to request that special keyrings be created if referred
  461. * to directly, to permit partially constructed keys to be found and to skip
  462. * validity and permission checks on the found key.
  463. *
  464. * Returns a pointer to the key with an incremented usage count if successful;
  465. * -EINVAL if the key ID is invalid; -ENOKEY if the key ID does not correspond
  466. * to a key or the best found key was a negative key; -EKEYREVOKED or
  467. * -EKEYEXPIRED if the best found key was revoked or expired; -EACCES if the
  468. * found key doesn't grant the requested permit or the LSM denied access to it;
  469. * or -ENOMEM if a special keyring couldn't be created.
  470. *
  471. * In the case of a successful return, the possession attribute is set on the
  472. * returned key reference.
  473. */
  474. key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
  475. key_perm_t perm)
  476. {
  477. struct request_key_auth *rka;
  478. const struct cred *cred;
  479. struct key *key;
  480. key_ref_t key_ref, skey_ref;
  481. int ret;
  482. try_again:
  483. cred = get_current_cred();
  484. key_ref = ERR_PTR(-ENOKEY);
  485. switch (id) {
  486. case KEY_SPEC_THREAD_KEYRING:
  487. if (!cred->thread_keyring) {
  488. if (!(lflags & KEY_LOOKUP_CREATE))
  489. goto error;
  490. ret = install_thread_keyring();
  491. if (ret < 0) {
  492. key_ref = ERR_PTR(ret);
  493. goto error;
  494. }
  495. goto reget_creds;
  496. }
  497. key = cred->thread_keyring;
  498. atomic_inc(&key->usage);
  499. key_ref = make_key_ref(key, 1);
  500. break;
  501. case KEY_SPEC_PROCESS_KEYRING:
  502. if (!cred->tgcred->process_keyring) {
  503. if (!(lflags & KEY_LOOKUP_CREATE))
  504. goto error;
  505. ret = install_process_keyring();
  506. if (ret < 0) {
  507. key_ref = ERR_PTR(ret);
  508. goto error;
  509. }
  510. goto reget_creds;
  511. }
  512. key = cred->tgcred->process_keyring;
  513. atomic_inc(&key->usage);
  514. key_ref = make_key_ref(key, 1);
  515. break;
  516. case KEY_SPEC_SESSION_KEYRING:
  517. if (!cred->tgcred->session_keyring) {
  518. /* always install a session keyring upon access if one
  519. * doesn't exist yet */
  520. ret = install_user_keyrings();
  521. if (ret < 0)
  522. goto error;
  523. if (lflags & KEY_LOOKUP_CREATE)
  524. ret = join_session_keyring(NULL);
  525. else
  526. ret = install_session_keyring(
  527. cred->user->session_keyring);
  528. if (ret < 0)
  529. goto error;
  530. goto reget_creds;
  531. } else if (cred->tgcred->session_keyring ==
  532. cred->user->session_keyring &&
  533. lflags & KEY_LOOKUP_CREATE) {
  534. ret = join_session_keyring(NULL);
  535. if (ret < 0)
  536. goto error;
  537. goto reget_creds;
  538. }
  539. rcu_read_lock();
  540. key = rcu_dereference(cred->tgcred->session_keyring);
  541. atomic_inc(&key->usage);
  542. rcu_read_unlock();
  543. key_ref = make_key_ref(key, 1);
  544. break;
  545. case KEY_SPEC_USER_KEYRING:
  546. if (!cred->user->uid_keyring) {
  547. ret = install_user_keyrings();
  548. if (ret < 0)
  549. goto error;
  550. }
  551. key = cred->user->uid_keyring;
  552. atomic_inc(&key->usage);
  553. key_ref = make_key_ref(key, 1);
  554. break;
  555. case KEY_SPEC_USER_SESSION_KEYRING:
  556. if (!cred->user->session_keyring) {
  557. ret = install_user_keyrings();
  558. if (ret < 0)
  559. goto error;
  560. }
  561. key = cred->user->session_keyring;
  562. atomic_inc(&key->usage);
  563. key_ref = make_key_ref(key, 1);
  564. break;
  565. case KEY_SPEC_GROUP_KEYRING:
  566. /* group keyrings are not yet supported */
  567. key_ref = ERR_PTR(-EINVAL);
  568. goto error;
  569. case KEY_SPEC_REQKEY_AUTH_KEY:
  570. key = cred->request_key_auth;
  571. if (!key)
  572. goto error;
  573. atomic_inc(&key->usage);
  574. key_ref = make_key_ref(key, 1);
  575. break;
  576. case KEY_SPEC_REQUESTOR_KEYRING:
  577. if (!cred->request_key_auth)
  578. goto error;
  579. down_read(&cred->request_key_auth->sem);
  580. if (test_bit(KEY_FLAG_REVOKED,
  581. &cred->request_key_auth->flags)) {
  582. key_ref = ERR_PTR(-EKEYREVOKED);
  583. key = NULL;
  584. } else {
  585. rka = cred->request_key_auth->payload.data;
  586. key = rka->dest_keyring;
  587. atomic_inc(&key->usage);
  588. }
  589. up_read(&cred->request_key_auth->sem);
  590. if (!key)
  591. goto error;
  592. key_ref = make_key_ref(key, 1);
  593. break;
  594. default:
  595. key_ref = ERR_PTR(-EINVAL);
  596. if (id < 1)
  597. goto error;
  598. key = key_lookup(id);
  599. if (IS_ERR(key)) {
  600. key_ref = ERR_CAST(key);
  601. goto error;
  602. }
  603. key_ref = make_key_ref(key, 0);
  604. /* check to see if we possess the key */
  605. skey_ref = search_process_keyrings(key->type, key,
  606. lookup_user_key_possessed,
  607. cred);
  608. if (!IS_ERR(skey_ref)) {
  609. key_put(key);
  610. key_ref = skey_ref;
  611. }
  612. break;
  613. }
  614. /* unlink does not use the nominated key in any way, so can skip all
  615. * the permission checks as it is only concerned with the keyring */
  616. if (lflags & KEY_LOOKUP_FOR_UNLINK) {
  617. ret = 0;
  618. goto error;
  619. }
  620. if (!(lflags & KEY_LOOKUP_PARTIAL)) {
  621. ret = wait_for_key_construction(key, true);
  622. switch (ret) {
  623. case -ERESTARTSYS:
  624. goto invalid_key;
  625. default:
  626. if (perm)
  627. goto invalid_key;
  628. case 0:
  629. break;
  630. }
  631. } else if (perm) {
  632. ret = key_validate(key);
  633. if (ret < 0)
  634. goto invalid_key;
  635. }
  636. ret = -EIO;
  637. if (!(lflags & KEY_LOOKUP_PARTIAL) &&
  638. !test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
  639. goto invalid_key;
  640. /* check the permissions */
  641. ret = key_task_permission(key_ref, cred, perm);
  642. if (ret < 0)
  643. goto invalid_key;
  644. error:
  645. put_cred(cred);
  646. return key_ref;
  647. invalid_key:
  648. key_ref_put(key_ref);
  649. key_ref = ERR_PTR(ret);
  650. goto error;
  651. /* if we attempted to install a keyring, then it may have caused new
  652. * creds to be installed */
  653. reget_creds:
  654. put_cred(cred);
  655. goto try_again;
  656. }
  657. /*
  658. * Join the named keyring as the session keyring if possible else attempt to
  659. * create a new one of that name and join that.
  660. *
  661. * If the name is NULL, an empty anonymous keyring will be installed as the
  662. * session keyring.
  663. *
  664. * Named session keyrings are joined with a semaphore held to prevent the
  665. * keyrings from going away whilst the attempt is made to going them and also
  666. * to prevent a race in creating compatible session keyrings.
  667. */
  668. long join_session_keyring(const char *name)
  669. {
  670. const struct cred *old;
  671. struct cred *new;
  672. struct key *keyring;
  673. long ret, serial;
  674. /* only permit this if there's a single thread in the thread group -
  675. * this avoids us having to adjust the creds on all threads and risking
  676. * ENOMEM */
  677. if (!current_is_single_threaded())
  678. return -EMLINK;
  679. new = prepare_creds();
  680. if (!new)
  681. return -ENOMEM;
  682. old = current_cred();
  683. /* if no name is provided, install an anonymous keyring */
  684. if (!name) {
  685. ret = install_session_keyring_to_cred(new, NULL);
  686. if (ret < 0)
  687. goto error;
  688. serial = new->tgcred->session_keyring->serial;
  689. ret = commit_creds(new);
  690. if (ret == 0)
  691. ret = serial;
  692. goto okay;
  693. }
  694. /* allow the user to join or create a named keyring */
  695. mutex_lock(&key_session_mutex);
  696. /* look for an existing keyring of this name */
  697. keyring = find_keyring_by_name(name, false);
  698. if (PTR_ERR(keyring) == -ENOKEY) {
  699. /* not found - try and create a new one */
  700. keyring = keyring_alloc(name, old->uid, old->gid, old,
  701. KEY_ALLOC_IN_QUOTA, NULL);
  702. if (IS_ERR(keyring)) {
  703. ret = PTR_ERR(keyring);
  704. goto error2;
  705. }
  706. } else if (IS_ERR(keyring)) {
  707. ret = PTR_ERR(keyring);
  708. goto error2;
  709. }
  710. /* we've got a keyring - now to install it */
  711. ret = install_session_keyring_to_cred(new, keyring);
  712. if (ret < 0)
  713. goto error2;
  714. commit_creds(new);
  715. mutex_unlock(&key_session_mutex);
  716. ret = keyring->serial;
  717. key_put(keyring);
  718. okay:
  719. return ret;
  720. error2:
  721. mutex_unlock(&key_session_mutex);
  722. error:
  723. abort_creds(new);
  724. return ret;
  725. }
  726. /*
  727. * Replace a process's session keyring on behalf of one of its children when
  728. * the target process is about to resume userspace execution.
  729. */
  730. void key_replace_session_keyring(void)
  731. {
  732. const struct cred *old;
  733. struct cred *new;
  734. if (!current->replacement_session_keyring)
  735. return;
  736. write_lock_irq(&tasklist_lock);
  737. new = current->replacement_session_keyring;
  738. current->replacement_session_keyring = NULL;
  739. write_unlock_irq(&tasklist_lock);
  740. if (!new)
  741. return;
  742. old = current_cred();
  743. new-> uid = old-> uid;
  744. new-> euid = old-> euid;
  745. new-> suid = old-> suid;
  746. new->fsuid = old->fsuid;
  747. new-> gid = old-> gid;
  748. new-> egid = old-> egid;
  749. new-> sgid = old-> sgid;
  750. new->fsgid = old->fsgid;
  751. new->user = get_uid(old->user);
  752. new->user_ns = get_user_ns(new->user_ns);
  753. new->group_info = get_group_info(old->group_info);
  754. new->securebits = old->securebits;
  755. new->cap_inheritable = old->cap_inheritable;
  756. new->cap_permitted = old->cap_permitted;
  757. new->cap_effective = old->cap_effective;
  758. new->cap_ambient = old->cap_ambient;
  759. new->cap_bset = old->cap_bset;
  760. new->jit_keyring = old->jit_keyring;
  761. new->thread_keyring = key_get(old->thread_keyring);
  762. new->tgcred->tgid = old->tgcred->tgid;
  763. new->tgcred->process_keyring = key_get(old->tgcred->process_keyring);
  764. security_transfer_creds(new, old);
  765. commit_creds(new);
  766. }