process_keys.c 20 KB

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