process_keys.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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) {
  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. case -ENOKEY: /* negative key */
  319. ret = key_ref;
  320. break;
  321. default:
  322. err = key_ref;
  323. break;
  324. }
  325. }
  326. /* search the session keyring */
  327. if (cred->session_keyring) {
  328. rcu_read_lock();
  329. key_ref = keyring_search_aux(
  330. make_key_ref(rcu_dereference(cred->session_keyring), 1),
  331. cred, type, description, match, no_state_check);
  332. rcu_read_unlock();
  333. if (!IS_ERR(key_ref))
  334. goto found;
  335. switch (PTR_ERR(key_ref)) {
  336. case -EAGAIN: /* no key */
  337. if (ret)
  338. break;
  339. case -ENOKEY: /* negative key */
  340. ret = key_ref;
  341. break;
  342. default:
  343. err = key_ref;
  344. break;
  345. }
  346. }
  347. /* or search the user-session keyring */
  348. else if (cred->user->session_keyring) {
  349. key_ref = keyring_search_aux(
  350. make_key_ref(cred->user->session_keyring, 1),
  351. cred, type, description, match, no_state_check);
  352. if (!IS_ERR(key_ref))
  353. goto found;
  354. switch (PTR_ERR(key_ref)) {
  355. case -EAGAIN: /* no key */
  356. if (ret)
  357. break;
  358. case -ENOKEY: /* negative key */
  359. ret = key_ref;
  360. break;
  361. default:
  362. err = key_ref;
  363. break;
  364. }
  365. }
  366. /* no key - decide on the error we're going to go for */
  367. key_ref = ret ? ret : err;
  368. found:
  369. return key_ref;
  370. }
  371. /*
  372. * Search the process keyrings attached to the supplied cred for the first
  373. * matching key in the manner of search_my_process_keyrings(), but also search
  374. * the keys attached to the assumed authorisation key using its credentials if
  375. * one is available.
  376. *
  377. * Return same as search_my_process_keyrings().
  378. */
  379. key_ref_t search_process_keyrings(struct key_type *type,
  380. const void *description,
  381. key_match_func_t match,
  382. const struct cred *cred)
  383. {
  384. struct request_key_auth *rka;
  385. key_ref_t key_ref, ret = ERR_PTR(-EACCES), err;
  386. might_sleep();
  387. key_ref = search_my_process_keyrings(type, description, match,
  388. false, cred);
  389. if (!IS_ERR(key_ref))
  390. goto found;
  391. err = key_ref;
  392. /* if this process has an instantiation authorisation key, then we also
  393. * search the keyrings of the process mentioned there
  394. * - we don't permit access to request_key auth keys via this method
  395. */
  396. if (cred->request_key_auth &&
  397. cred == current_cred() &&
  398. type != &key_type_request_key_auth
  399. ) {
  400. /* defend against the auth key being revoked */
  401. down_read(&cred->request_key_auth->sem);
  402. if (key_validate(cred->request_key_auth) == 0) {
  403. rka = cred->request_key_auth->payload.data;
  404. key_ref = search_process_keyrings(type, description,
  405. match, rka->cred);
  406. up_read(&cred->request_key_auth->sem);
  407. if (!IS_ERR(key_ref))
  408. goto found;
  409. ret = key_ref;
  410. } else {
  411. up_read(&cred->request_key_auth->sem);
  412. }
  413. }
  414. /* no key - decide on the error we're going to go for */
  415. if (err == ERR_PTR(-ENOKEY) || ret == ERR_PTR(-ENOKEY))
  416. key_ref = ERR_PTR(-ENOKEY);
  417. else if (err == ERR_PTR(-EACCES))
  418. key_ref = ret;
  419. else
  420. key_ref = err;
  421. found:
  422. return key_ref;
  423. }
  424. /*
  425. * See if the key we're looking at is the target key.
  426. */
  427. int lookup_user_key_possessed(const struct key *key, const void *target)
  428. {
  429. return key == target;
  430. }
  431. /*
  432. * Look up a key ID given us by userspace with a given permissions mask to get
  433. * the key it refers to.
  434. *
  435. * Flags can be passed to request that special keyrings be created if referred
  436. * to directly, to permit partially constructed keys to be found and to skip
  437. * validity and permission checks on the found key.
  438. *
  439. * Returns a pointer to the key with an incremented usage count if successful;
  440. * -EINVAL if the key ID is invalid; -ENOKEY if the key ID does not correspond
  441. * to a key or the best found key was a negative key; -EKEYREVOKED or
  442. * -EKEYEXPIRED if the best found key was revoked or expired; -EACCES if the
  443. * found key doesn't grant the requested permit or the LSM denied access to it;
  444. * or -ENOMEM if a special keyring couldn't be created.
  445. *
  446. * In the case of a successful return, the possession attribute is set on the
  447. * returned key reference.
  448. */
  449. key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
  450. key_perm_t perm)
  451. {
  452. struct request_key_auth *rka;
  453. const struct cred *cred;
  454. struct key *key;
  455. key_ref_t key_ref, skey_ref;
  456. int ret;
  457. try_again:
  458. cred = get_current_cred();
  459. key_ref = ERR_PTR(-ENOKEY);
  460. switch (id) {
  461. case KEY_SPEC_THREAD_KEYRING:
  462. if (!cred->thread_keyring) {
  463. if (!(lflags & KEY_LOOKUP_CREATE))
  464. goto error;
  465. ret = install_thread_keyring();
  466. if (ret < 0) {
  467. key_ref = ERR_PTR(ret);
  468. goto error;
  469. }
  470. goto reget_creds;
  471. }
  472. key = cred->thread_keyring;
  473. atomic_inc(&key->usage);
  474. key_ref = make_key_ref(key, 1);
  475. break;
  476. case KEY_SPEC_PROCESS_KEYRING:
  477. if (!cred->process_keyring) {
  478. if (!(lflags & KEY_LOOKUP_CREATE))
  479. goto error;
  480. ret = install_process_keyring();
  481. if (ret < 0) {
  482. key_ref = ERR_PTR(ret);
  483. goto error;
  484. }
  485. goto reget_creds;
  486. }
  487. key = cred->process_keyring;
  488. atomic_inc(&key->usage);
  489. key_ref = make_key_ref(key, 1);
  490. break;
  491. case KEY_SPEC_SESSION_KEYRING:
  492. if (!cred->session_keyring) {
  493. /* always install a session keyring upon access if one
  494. * doesn't exist yet */
  495. ret = install_user_keyrings();
  496. if (ret < 0)
  497. goto error;
  498. if (lflags & KEY_LOOKUP_CREATE)
  499. ret = join_session_keyring(NULL);
  500. else
  501. ret = install_session_keyring(
  502. cred->user->session_keyring);
  503. if (ret < 0)
  504. goto error;
  505. goto reget_creds;
  506. } else if (cred->session_keyring ==
  507. cred->user->session_keyring &&
  508. lflags & KEY_LOOKUP_CREATE) {
  509. ret = join_session_keyring(NULL);
  510. if (ret < 0)
  511. goto error;
  512. goto reget_creds;
  513. }
  514. rcu_read_lock();
  515. key = rcu_dereference(cred->session_keyring);
  516. atomic_inc(&key->usage);
  517. rcu_read_unlock();
  518. key_ref = make_key_ref(key, 1);
  519. break;
  520. case KEY_SPEC_USER_KEYRING:
  521. if (!cred->user->uid_keyring) {
  522. ret = install_user_keyrings();
  523. if (ret < 0)
  524. goto error;
  525. }
  526. key = cred->user->uid_keyring;
  527. atomic_inc(&key->usage);
  528. key_ref = make_key_ref(key, 1);
  529. break;
  530. case KEY_SPEC_USER_SESSION_KEYRING:
  531. if (!cred->user->session_keyring) {
  532. ret = install_user_keyrings();
  533. if (ret < 0)
  534. goto error;
  535. }
  536. key = cred->user->session_keyring;
  537. atomic_inc(&key->usage);
  538. key_ref = make_key_ref(key, 1);
  539. break;
  540. case KEY_SPEC_GROUP_KEYRING:
  541. /* group keyrings are not yet supported */
  542. key_ref = ERR_PTR(-EINVAL);
  543. goto error;
  544. case KEY_SPEC_REQKEY_AUTH_KEY:
  545. key = cred->request_key_auth;
  546. if (!key)
  547. goto error;
  548. atomic_inc(&key->usage);
  549. key_ref = make_key_ref(key, 1);
  550. break;
  551. case KEY_SPEC_REQUESTOR_KEYRING:
  552. if (!cred->request_key_auth)
  553. goto error;
  554. down_read(&cred->request_key_auth->sem);
  555. if (test_bit(KEY_FLAG_REVOKED,
  556. &cred->request_key_auth->flags)) {
  557. key_ref = ERR_PTR(-EKEYREVOKED);
  558. key = NULL;
  559. } else {
  560. rka = cred->request_key_auth->payload.data;
  561. key = rka->dest_keyring;
  562. atomic_inc(&key->usage);
  563. }
  564. up_read(&cred->request_key_auth->sem);
  565. if (!key)
  566. goto error;
  567. key_ref = make_key_ref(key, 1);
  568. break;
  569. default:
  570. key_ref = ERR_PTR(-EINVAL);
  571. if (id < 1)
  572. goto error;
  573. key = key_lookup(id);
  574. if (IS_ERR(key)) {
  575. key_ref = ERR_CAST(key);
  576. goto error;
  577. }
  578. key_ref = make_key_ref(key, 0);
  579. /* check to see if we possess the key */
  580. skey_ref = search_process_keyrings(key->type, key,
  581. lookup_user_key_possessed,
  582. cred);
  583. if (!IS_ERR(skey_ref)) {
  584. key_put(key);
  585. key_ref = skey_ref;
  586. }
  587. break;
  588. }
  589. /* unlink does not use the nominated key in any way, so can skip all
  590. * the permission checks as it is only concerned with the keyring */
  591. if (lflags & KEY_LOOKUP_FOR_UNLINK) {
  592. ret = 0;
  593. goto error;
  594. }
  595. if (!(lflags & KEY_LOOKUP_PARTIAL)) {
  596. ret = wait_for_key_construction(key, true);
  597. switch (ret) {
  598. case -ERESTARTSYS:
  599. goto invalid_key;
  600. default:
  601. if (perm)
  602. goto invalid_key;
  603. case 0:
  604. break;
  605. }
  606. } else if (perm) {
  607. ret = key_validate(key);
  608. if (ret < 0)
  609. goto invalid_key;
  610. }
  611. ret = -EIO;
  612. if (!(lflags & KEY_LOOKUP_PARTIAL) &&
  613. !test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
  614. goto invalid_key;
  615. /* check the permissions */
  616. ret = key_task_permission(key_ref, cred, perm);
  617. if (ret < 0)
  618. goto invalid_key;
  619. key->last_used_at = current_kernel_time().tv_sec;
  620. error:
  621. put_cred(cred);
  622. return key_ref;
  623. invalid_key:
  624. key_ref_put(key_ref);
  625. key_ref = ERR_PTR(ret);
  626. goto error;
  627. /* if we attempted to install a keyring, then it may have caused new
  628. * creds to be installed */
  629. reget_creds:
  630. put_cred(cred);
  631. goto try_again;
  632. }
  633. /*
  634. * Join the named keyring as the session keyring if possible else attempt to
  635. * create a new one of that name and join that.
  636. *
  637. * If the name is NULL, an empty anonymous keyring will be installed as the
  638. * session keyring.
  639. *
  640. * Named session keyrings are joined with a semaphore held to prevent the
  641. * keyrings from going away whilst the attempt is made to going them and also
  642. * to prevent a race in creating compatible session keyrings.
  643. */
  644. long join_session_keyring(const char *name)
  645. {
  646. const struct cred *old;
  647. struct cred *new;
  648. struct key *keyring;
  649. long ret, serial;
  650. new = prepare_creds();
  651. if (!new)
  652. return -ENOMEM;
  653. old = current_cred();
  654. /* if no name is provided, install an anonymous keyring */
  655. if (!name) {
  656. ret = install_session_keyring_to_cred(new, NULL);
  657. if (ret < 0)
  658. goto error;
  659. serial = new->session_keyring->serial;
  660. ret = commit_creds(new);
  661. if (ret == 0)
  662. ret = serial;
  663. goto okay;
  664. }
  665. /* allow the user to join or create a named keyring */
  666. mutex_lock(&key_session_mutex);
  667. /* look for an existing keyring of this name */
  668. keyring = find_keyring_by_name(name, false);
  669. if (PTR_ERR(keyring) == -ENOKEY) {
  670. /* not found - try and create a new one */
  671. keyring = keyring_alloc(
  672. name, old->uid, old->gid, old,
  673. KEY_POS_ALL | KEY_USR_VIEW | KEY_USR_READ | KEY_USR_LINK,
  674. KEY_ALLOC_IN_QUOTA, NULL);
  675. if (IS_ERR(keyring)) {
  676. ret = PTR_ERR(keyring);
  677. goto error2;
  678. }
  679. } else if (IS_ERR(keyring)) {
  680. ret = PTR_ERR(keyring);
  681. goto error2;
  682. } else if (keyring == new->session_keyring) {
  683. ret = 0;
  684. goto error2;
  685. }
  686. /* we've got a keyring - now to install it */
  687. ret = install_session_keyring_to_cred(new, keyring);
  688. if (ret < 0)
  689. goto error2;
  690. commit_creds(new);
  691. mutex_unlock(&key_session_mutex);
  692. ret = keyring->serial;
  693. key_put(keyring);
  694. okay:
  695. return ret;
  696. error2:
  697. mutex_unlock(&key_session_mutex);
  698. error:
  699. abort_creds(new);
  700. return ret;
  701. }
  702. /*
  703. * Replace a process's session keyring on behalf of one of its children when
  704. * the target process is about to resume userspace execution.
  705. */
  706. void key_change_session_keyring(struct callback_head *twork)
  707. {
  708. const struct cred *old = current_cred();
  709. struct cred *new = container_of(twork, struct cred, rcu);
  710. if (unlikely(current->flags & PF_EXITING)) {
  711. put_cred(new);
  712. return;
  713. }
  714. new-> uid = old-> uid;
  715. new-> euid = old-> euid;
  716. new-> suid = old-> suid;
  717. new->fsuid = old->fsuid;
  718. new-> gid = old-> gid;
  719. new-> egid = old-> egid;
  720. new-> sgid = old-> sgid;
  721. new->fsgid = old->fsgid;
  722. new->user = get_uid(old->user);
  723. new->user_ns = get_user_ns(new->user_ns);
  724. new->group_info = get_group_info(old->group_info);
  725. new->securebits = old->securebits;
  726. new->cap_inheritable = old->cap_inheritable;
  727. new->cap_permitted = old->cap_permitted;
  728. new->cap_effective = old->cap_effective;
  729. new->cap_bset = old->cap_bset;
  730. new->jit_keyring = old->jit_keyring;
  731. new->thread_keyring = key_get(old->thread_keyring);
  732. new->process_keyring = key_get(old->process_keyring);
  733. security_transfer_creds(new, old);
  734. commit_creds(new);
  735. }