process_keys.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. /* Management of 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. /*
  38. * install user and user session keyrings for a particular UID
  39. */
  40. int install_user_keyrings(void)
  41. {
  42. struct user_struct *user;
  43. const struct cred *cred;
  44. struct key *uid_keyring, *session_keyring;
  45. char buf[20];
  46. int ret;
  47. cred = current_cred();
  48. user = cred->user;
  49. kenter("%p{%u}", user, user->uid);
  50. if (user->uid_keyring) {
  51. kleave(" = 0 [exist]");
  52. return 0;
  53. }
  54. mutex_lock(&key_user_keyring_mutex);
  55. ret = 0;
  56. if (!user->uid_keyring) {
  57. /* get the UID-specific keyring
  58. * - there may be one in existence already as it may have been
  59. * pinned by a session, but the user_struct pointing to it
  60. * may have been destroyed by setuid */
  61. sprintf(buf, "_uid.%u", user->uid);
  62. uid_keyring = find_keyring_by_name(buf, true);
  63. if (IS_ERR(uid_keyring)) {
  64. uid_keyring = keyring_alloc(buf, user->uid, (gid_t) -1,
  65. cred, KEY_ALLOC_IN_QUOTA,
  66. NULL);
  67. if (IS_ERR(uid_keyring)) {
  68. ret = PTR_ERR(uid_keyring);
  69. goto error;
  70. }
  71. }
  72. /* get a default session keyring (which might also exist
  73. * already) */
  74. sprintf(buf, "_uid_ses.%u", user->uid);
  75. session_keyring = find_keyring_by_name(buf, true);
  76. if (IS_ERR(session_keyring)) {
  77. session_keyring =
  78. keyring_alloc(buf, user->uid, (gid_t) -1,
  79. cred, KEY_ALLOC_IN_QUOTA, NULL);
  80. if (IS_ERR(session_keyring)) {
  81. ret = PTR_ERR(session_keyring);
  82. goto error_release;
  83. }
  84. /* we install a link from the user session keyring to
  85. * the user keyring */
  86. ret = key_link(session_keyring, uid_keyring);
  87. if (ret < 0)
  88. goto error_release_both;
  89. }
  90. /* install the keyrings */
  91. user->uid_keyring = uid_keyring;
  92. user->session_keyring = session_keyring;
  93. }
  94. mutex_unlock(&key_user_keyring_mutex);
  95. kleave(" = 0");
  96. return 0;
  97. error_release_both:
  98. key_put(session_keyring);
  99. error_release:
  100. key_put(uid_keyring);
  101. error:
  102. mutex_unlock(&key_user_keyring_mutex);
  103. kleave(" = %d", ret);
  104. return ret;
  105. }
  106. /*
  107. * install a fresh thread keyring directly to new credentials
  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. * - returns -EEXIST if there was already a process keyring, 0 if one installed,
  140. * and other -ve on any other error
  141. */
  142. int install_process_keyring_to_cred(struct cred *new)
  143. {
  144. struct key *keyring;
  145. int ret;
  146. if (new->tgcred->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. spin_lock_irq(&new->tgcred->lock);
  153. if (!new->tgcred->process_keyring) {
  154. new->tgcred->process_keyring = keyring;
  155. keyring = NULL;
  156. ret = 0;
  157. } else {
  158. ret = -EEXIST;
  159. }
  160. spin_unlock_irq(&new->tgcred->lock);
  161. key_put(keyring);
  162. return ret;
  163. }
  164. /*
  165. * make sure a process keyring is installed
  166. * - we
  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 ?: 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->tgcred->session_keyring)
  194. flags = KEY_ALLOC_IN_QUOTA;
  195. keyring = keyring_alloc("_ses", cred->uid, cred->gid,
  196. cred, flags, NULL);
  197. if (IS_ERR(keyring))
  198. return PTR_ERR(keyring);
  199. } else {
  200. atomic_inc(&keyring->usage);
  201. }
  202. /* install the keyring */
  203. spin_lock_irq(&cred->tgcred->lock);
  204. old = cred->tgcred->session_keyring;
  205. rcu_assign_pointer(cred->tgcred->session_keyring, keyring);
  206. spin_unlock_irq(&cred->tgcred->lock);
  207. /* we're using RCU on the pointer, but there's no point synchronising
  208. * on it if it didn't previously point to anything */
  209. if (old) {
  210. synchronize_rcu();
  211. key_put(old);
  212. }
  213. return 0;
  214. }
  215. /*
  216. * install a session keyring, discarding the old one
  217. * - if a keyring is not supplied, an empty one is invented
  218. */
  219. static int install_session_keyring(struct key *keyring)
  220. {
  221. struct cred *new;
  222. int ret;
  223. new = prepare_creds();
  224. if (!new)
  225. return -ENOMEM;
  226. ret = install_session_keyring_to_cred(new, NULL);
  227. if (ret < 0) {
  228. abort_creds(new);
  229. return ret;
  230. }
  231. return commit_creds(new);
  232. }
  233. /*****************************************************************************/
  234. /*
  235. * the filesystem user ID changed
  236. */
  237. void key_fsuid_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->uid = tsk->cred->fsuid;
  244. up_write(&tsk->cred->thread_keyring->sem);
  245. }
  246. } /* end key_fsuid_changed() */
  247. /*****************************************************************************/
  248. /*
  249. * the filesystem group ID changed
  250. */
  251. void key_fsgid_changed(struct task_struct *tsk)
  252. {
  253. /* update the ownership of the thread keyring */
  254. BUG_ON(!tsk->cred);
  255. if (tsk->cred->thread_keyring) {
  256. down_write(&tsk->cred->thread_keyring->sem);
  257. tsk->cred->thread_keyring->gid = tsk->cred->fsgid;
  258. up_write(&tsk->cred->thread_keyring->sem);
  259. }
  260. } /* end key_fsgid_changed() */
  261. /*****************************************************************************/
  262. /*
  263. * search only my process keyrings for the first matching key
  264. * - we use the supplied match function to see if the description (or other
  265. * feature of interest) matches
  266. * - we return -EAGAIN if we didn't find any matching key
  267. * - we return -ENOKEY if we found only negative matching keys
  268. */
  269. key_ref_t search_my_process_keyrings(struct key_type *type,
  270. const void *description,
  271. key_match_func_t match,
  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);
  290. if (!IS_ERR(key_ref))
  291. goto found;
  292. switch (PTR_ERR(key_ref)) {
  293. case -EAGAIN: /* no key */
  294. if (ret)
  295. break;
  296. case -ENOKEY: /* negative key */
  297. ret = key_ref;
  298. break;
  299. default:
  300. err = key_ref;
  301. break;
  302. }
  303. }
  304. /* search the process keyring second */
  305. if (cred->tgcred->process_keyring) {
  306. key_ref = keyring_search_aux(
  307. make_key_ref(cred->tgcred->process_keyring, 1),
  308. cred, type, description, match);
  309. if (!IS_ERR(key_ref))
  310. goto found;
  311. switch (PTR_ERR(key_ref)) {
  312. case -EAGAIN: /* no key */
  313. if (ret)
  314. break;
  315. case -ENOKEY: /* negative key */
  316. ret = key_ref;
  317. break;
  318. default:
  319. err = key_ref;
  320. break;
  321. }
  322. }
  323. /* search the session keyring */
  324. if (cred->tgcred->session_keyring) {
  325. rcu_read_lock();
  326. key_ref = keyring_search_aux(
  327. make_key_ref(rcu_dereference(
  328. cred->tgcred->session_keyring),
  329. 1),
  330. cred, type, description, match);
  331. rcu_read_unlock();
  332. if (!IS_ERR(key_ref))
  333. goto found;
  334. switch (PTR_ERR(key_ref)) {
  335. case -EAGAIN: /* no key */
  336. if (ret)
  337. break;
  338. case -ENOKEY: /* negative key */
  339. ret = key_ref;
  340. break;
  341. default:
  342. err = key_ref;
  343. break;
  344. }
  345. }
  346. /* or search the user-session keyring */
  347. else if (cred->user->session_keyring) {
  348. key_ref = keyring_search_aux(
  349. make_key_ref(cred->user->session_keyring, 1),
  350. cred, type, description, match);
  351. if (!IS_ERR(key_ref))
  352. goto found;
  353. switch (PTR_ERR(key_ref)) {
  354. case -EAGAIN: /* no key */
  355. if (ret)
  356. break;
  357. case -ENOKEY: /* negative key */
  358. ret = key_ref;
  359. break;
  360. default:
  361. err = key_ref;
  362. break;
  363. }
  364. }
  365. /* no key - decide on the error we're going to go for */
  366. key_ref = ret ? ret : err;
  367. found:
  368. return key_ref;
  369. }
  370. /*****************************************************************************/
  371. /*
  372. * search the process keyrings for the first matching key
  373. * - we use the supplied match function to see if the description (or other
  374. * feature of interest) matches
  375. * - we return -EAGAIN if we didn't find any matching key
  376. * - we return -ENOKEY if we found only negative matching keys
  377. */
  378. key_ref_t search_process_keyrings(struct key_type *type,
  379. const void *description,
  380. key_match_func_t match,
  381. const struct cred *cred)
  382. {
  383. struct request_key_auth *rka;
  384. key_ref_t key_ref, ret = ERR_PTR(-EACCES), err;
  385. might_sleep();
  386. key_ref = search_my_process_keyrings(type, description, match, cred);
  387. if (!IS_ERR(key_ref))
  388. goto found;
  389. err = key_ref;
  390. /* if this process has an instantiation authorisation key, then we also
  391. * search the keyrings of the process mentioned there
  392. * - we don't permit access to request_key auth keys via this method
  393. */
  394. if (cred->request_key_auth &&
  395. cred == current_cred() &&
  396. type != &key_type_request_key_auth
  397. ) {
  398. /* defend against the auth key being revoked */
  399. down_read(&cred->request_key_auth->sem);
  400. if (key_validate(cred->request_key_auth) == 0) {
  401. rka = cred->request_key_auth->payload.data;
  402. key_ref = search_process_keyrings(type, description,
  403. match, rka->cred);
  404. up_read(&cred->request_key_auth->sem);
  405. if (!IS_ERR(key_ref))
  406. goto found;
  407. ret = key_ref;
  408. } else {
  409. up_read(&cred->request_key_auth->sem);
  410. }
  411. }
  412. /* no key - decide on the error we're going to go for */
  413. if (err == ERR_PTR(-ENOKEY) || ret == ERR_PTR(-ENOKEY))
  414. key_ref = ERR_PTR(-ENOKEY);
  415. else if (err == ERR_PTR(-EACCES))
  416. key_ref = ret;
  417. else
  418. key_ref = err;
  419. found:
  420. return key_ref;
  421. } /* end search_process_keyrings() */
  422. /*****************************************************************************/
  423. /*
  424. * see if the key we're looking at is the target key
  425. */
  426. int lookup_user_key_possessed(const struct key *key, const void *target)
  427. {
  428. return key == target;
  429. } /* end lookup_user_key_possessed() */
  430. /*****************************************************************************/
  431. /*
  432. * lookup a key given a key ID from userspace with a given permissions mask
  433. * - don't create special keyrings unless so requested
  434. * - partially constructed keys aren't found unless requested
  435. */
  436. key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
  437. key_perm_t perm)
  438. {
  439. struct request_key_auth *rka;
  440. const struct cred *cred;
  441. struct key *key;
  442. key_ref_t key_ref, skey_ref;
  443. int ret;
  444. try_again:
  445. cred = get_current_cred();
  446. key_ref = ERR_PTR(-ENOKEY);
  447. switch (id) {
  448. case KEY_SPEC_THREAD_KEYRING:
  449. if (!cred->thread_keyring) {
  450. if (!(lflags & KEY_LOOKUP_CREATE))
  451. goto error;
  452. ret = install_thread_keyring();
  453. if (ret < 0) {
  454. key_ref = ERR_PTR(ret);
  455. goto error;
  456. }
  457. goto reget_creds;
  458. }
  459. key = cred->thread_keyring;
  460. atomic_inc(&key->usage);
  461. key_ref = make_key_ref(key, 1);
  462. break;
  463. case KEY_SPEC_PROCESS_KEYRING:
  464. if (!cred->tgcred->process_keyring) {
  465. if (!(lflags & KEY_LOOKUP_CREATE))
  466. goto error;
  467. ret = install_process_keyring();
  468. if (ret < 0) {
  469. key_ref = ERR_PTR(ret);
  470. goto error;
  471. }
  472. goto reget_creds;
  473. }
  474. key = cred->tgcred->process_keyring;
  475. atomic_inc(&key->usage);
  476. key_ref = make_key_ref(key, 1);
  477. break;
  478. case KEY_SPEC_SESSION_KEYRING:
  479. if (!cred->tgcred->session_keyring) {
  480. /* always install a session keyring upon access if one
  481. * doesn't exist yet */
  482. ret = install_user_keyrings();
  483. if (ret < 0)
  484. goto error;
  485. ret = install_session_keyring(
  486. cred->user->session_keyring);
  487. if (ret < 0)
  488. goto error;
  489. goto reget_creds;
  490. }
  491. rcu_read_lock();
  492. key = rcu_dereference(cred->tgcred->session_keyring);
  493. atomic_inc(&key->usage);
  494. rcu_read_unlock();
  495. key_ref = make_key_ref(key, 1);
  496. break;
  497. case KEY_SPEC_USER_KEYRING:
  498. if (!cred->user->uid_keyring) {
  499. ret = install_user_keyrings();
  500. if (ret < 0)
  501. goto error;
  502. }
  503. key = cred->user->uid_keyring;
  504. atomic_inc(&key->usage);
  505. key_ref = make_key_ref(key, 1);
  506. break;
  507. case KEY_SPEC_USER_SESSION_KEYRING:
  508. if (!cred->user->session_keyring) {
  509. ret = install_user_keyrings();
  510. if (ret < 0)
  511. goto error;
  512. }
  513. key = cred->user->session_keyring;
  514. atomic_inc(&key->usage);
  515. key_ref = make_key_ref(key, 1);
  516. break;
  517. case KEY_SPEC_GROUP_KEYRING:
  518. /* group keyrings are not yet supported */
  519. key_ref = ERR_PTR(-EINVAL);
  520. goto error;
  521. case KEY_SPEC_REQKEY_AUTH_KEY:
  522. key = cred->request_key_auth;
  523. if (!key)
  524. goto error;
  525. atomic_inc(&key->usage);
  526. key_ref = make_key_ref(key, 1);
  527. break;
  528. case KEY_SPEC_REQUESTOR_KEYRING:
  529. if (!cred->request_key_auth)
  530. goto error;
  531. down_read(&cred->request_key_auth->sem);
  532. if (cred->request_key_auth->flags & KEY_FLAG_REVOKED) {
  533. key_ref = ERR_PTR(-EKEYREVOKED);
  534. key = NULL;
  535. } else {
  536. rka = cred->request_key_auth->payload.data;
  537. key = rka->dest_keyring;
  538. atomic_inc(&key->usage);
  539. }
  540. up_read(&cred->request_key_auth->sem);
  541. if (!key)
  542. goto error;
  543. key_ref = make_key_ref(key, 1);
  544. break;
  545. default:
  546. key_ref = ERR_PTR(-EINVAL);
  547. if (id < 1)
  548. goto error;
  549. key = key_lookup(id);
  550. if (IS_ERR(key)) {
  551. key_ref = ERR_CAST(key);
  552. goto error;
  553. }
  554. key_ref = make_key_ref(key, 0);
  555. /* check to see if we possess the key */
  556. skey_ref = search_process_keyrings(key->type, key,
  557. lookup_user_key_possessed,
  558. cred);
  559. if (!IS_ERR(skey_ref)) {
  560. key_put(key);
  561. key_ref = skey_ref;
  562. }
  563. break;
  564. }
  565. /* unlink does not use the nominated key in any way, so can skip all
  566. * the permission checks as it is only concerned with the keyring */
  567. if (lflags & KEY_LOOKUP_FOR_UNLINK) {
  568. ret = 0;
  569. goto error;
  570. }
  571. if (!(lflags & KEY_LOOKUP_PARTIAL)) {
  572. ret = wait_for_key_construction(key, true);
  573. switch (ret) {
  574. case -ERESTARTSYS:
  575. goto invalid_key;
  576. default:
  577. if (perm)
  578. goto invalid_key;
  579. case 0:
  580. break;
  581. }
  582. } else if (perm) {
  583. ret = key_validate(key);
  584. if (ret < 0)
  585. goto invalid_key;
  586. }
  587. ret = -EIO;
  588. if (!(lflags & KEY_LOOKUP_PARTIAL) &&
  589. !test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
  590. goto invalid_key;
  591. /* check the permissions */
  592. ret = key_task_permission(key_ref, cred, perm);
  593. if (ret < 0)
  594. goto invalid_key;
  595. error:
  596. put_cred(cred);
  597. return key_ref;
  598. invalid_key:
  599. key_ref_put(key_ref);
  600. key_ref = ERR_PTR(ret);
  601. goto error;
  602. /* if we attempted to install a keyring, then it may have caused new
  603. * creds to be installed */
  604. reget_creds:
  605. put_cred(cred);
  606. goto try_again;
  607. } /* end lookup_user_key() */
  608. /*****************************************************************************/
  609. /*
  610. * join the named keyring as the session keyring if possible, or attempt to
  611. * create a new one of that name if not
  612. * - if the name is NULL, an empty anonymous keyring is installed instead
  613. * - named session keyring joining is done with a semaphore held
  614. */
  615. long join_session_keyring(const char *name)
  616. {
  617. const struct cred *old;
  618. struct cred *new;
  619. struct key *keyring;
  620. long ret, serial;
  621. /* only permit this if there's a single thread in the thread group -
  622. * this avoids us having to adjust the creds on all threads and risking
  623. * ENOMEM */
  624. if (!current_is_single_threaded())
  625. return -EMLINK;
  626. new = prepare_creds();
  627. if (!new)
  628. return -ENOMEM;
  629. old = current_cred();
  630. /* if no name is provided, install an anonymous keyring */
  631. if (!name) {
  632. ret = install_session_keyring_to_cred(new, NULL);
  633. if (ret < 0)
  634. goto error;
  635. serial = new->tgcred->session_keyring->serial;
  636. ret = commit_creds(new);
  637. if (ret == 0)
  638. ret = serial;
  639. goto okay;
  640. }
  641. /* allow the user to join or create a named keyring */
  642. mutex_lock(&key_session_mutex);
  643. /* look for an existing keyring of this name */
  644. keyring = find_keyring_by_name(name, false);
  645. if (PTR_ERR(keyring) == -ENOKEY) {
  646. /* not found - try and create a new one */
  647. keyring = keyring_alloc(name, old->uid, old->gid, old,
  648. KEY_ALLOC_IN_QUOTA, NULL);
  649. if (IS_ERR(keyring)) {
  650. ret = PTR_ERR(keyring);
  651. goto error2;
  652. }
  653. } else if (IS_ERR(keyring)) {
  654. ret = PTR_ERR(keyring);
  655. goto error2;
  656. }
  657. /* we've got a keyring - now to install it */
  658. ret = install_session_keyring_to_cred(new, keyring);
  659. if (ret < 0)
  660. goto error2;
  661. commit_creds(new);
  662. mutex_unlock(&key_session_mutex);
  663. ret = keyring->serial;
  664. key_put(keyring);
  665. okay:
  666. return ret;
  667. error2:
  668. mutex_unlock(&key_session_mutex);
  669. error:
  670. abort_creds(new);
  671. return ret;
  672. }
  673. /*
  674. * Replace a process's session keyring when that process resumes userspace on
  675. * behalf of one of its children
  676. */
  677. void key_replace_session_keyring(void)
  678. {
  679. const struct cred *old;
  680. struct cred *new;
  681. if (!current->replacement_session_keyring)
  682. return;
  683. write_lock_irq(&tasklist_lock);
  684. new = current->replacement_session_keyring;
  685. current->replacement_session_keyring = NULL;
  686. write_unlock_irq(&tasklist_lock);
  687. if (!new)
  688. return;
  689. old = current_cred();
  690. new-> uid = old-> uid;
  691. new-> euid = old-> euid;
  692. new-> suid = old-> suid;
  693. new->fsuid = old->fsuid;
  694. new-> gid = old-> gid;
  695. new-> egid = old-> egid;
  696. new-> sgid = old-> sgid;
  697. new->fsgid = old->fsgid;
  698. new->user = get_uid(old->user);
  699. new->group_info = get_group_info(old->group_info);
  700. new->securebits = old->securebits;
  701. new->cap_inheritable = old->cap_inheritable;
  702. new->cap_permitted = old->cap_permitted;
  703. new->cap_effective = old->cap_effective;
  704. new->cap_bset = old->cap_bset;
  705. new->jit_keyring = old->jit_keyring;
  706. new->thread_keyring = key_get(old->thread_keyring);
  707. new->tgcred->tgid = old->tgcred->tgid;
  708. new->tgcred->process_keyring = key_get(old->tgcred->process_keyring);
  709. security_transfer_creds(new, old);
  710. commit_creds(new);
  711. }