process_keys.c 20 KB

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