process_keys.c 20 KB

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