process_keys.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. /* Manage a process's keyrings
  2. *
  3. * Copyright (C) 2004-2005, 2008 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/sched.h>
  14. #include <linux/keyctl.h>
  15. #include <linux/fs.h>
  16. #include <linux/err.h>
  17. #include <linux/mutex.h>
  18. #include <linux/security.h>
  19. #include <linux/user_namespace.h>
  20. #include <asm/uaccess.h>
  21. #include "internal.h"
  22. /* Session keyring create vs join semaphore */
  23. static DEFINE_MUTEX(key_session_mutex);
  24. /* User keyring creation semaphore */
  25. static DEFINE_MUTEX(key_user_keyring_mutex);
  26. /* The root user's tracking struct */
  27. struct key_user root_key_user = {
  28. .usage = ATOMIC_INIT(3),
  29. .cons_lock = __MUTEX_INITIALIZER(root_key_user.cons_lock),
  30. .lock = __SPIN_LOCK_UNLOCKED(root_key_user.lock),
  31. .nkeys = ATOMIC_INIT(2),
  32. .nikeys = ATOMIC_INIT(2),
  33. .uid = 0,
  34. .user_ns = &init_user_ns,
  35. };
  36. /*
  37. * Install the user and user session keyrings for the current process's UID.
  38. */
  39. int install_user_keyrings(void)
  40. {
  41. struct user_struct *user;
  42. const struct cred *cred;
  43. struct key *uid_keyring, *session_keyring;
  44. char buf[20];
  45. int ret;
  46. cred = current_cred();
  47. user = cred->user;
  48. kenter("%p{%u}", user, user->uid);
  49. if (user->uid_keyring) {
  50. kleave(" = 0 [exist]");
  51. return 0;
  52. }
  53. mutex_lock(&key_user_keyring_mutex);
  54. ret = 0;
  55. if (!user->uid_keyring) {
  56. /* get the UID-specific keyring
  57. * - there may be one in existence already as it may have been
  58. * pinned by a session, but the user_struct pointing to it
  59. * may have been destroyed by setuid */
  60. sprintf(buf, "_uid.%u", user->uid);
  61. uid_keyring = find_keyring_by_name(buf, true);
  62. if (IS_ERR(uid_keyring)) {
  63. uid_keyring = keyring_alloc(buf, user->uid, (gid_t) -1,
  64. cred, KEY_ALLOC_IN_QUOTA,
  65. NULL);
  66. if (IS_ERR(uid_keyring)) {
  67. ret = PTR_ERR(uid_keyring);
  68. goto error;
  69. }
  70. }
  71. /* get a default session keyring (which might also exist
  72. * already) */
  73. sprintf(buf, "_uid_ses.%u", user->uid);
  74. session_keyring = find_keyring_by_name(buf, true);
  75. if (IS_ERR(session_keyring)) {
  76. session_keyring =
  77. keyring_alloc(buf, user->uid, (gid_t) -1,
  78. cred, KEY_ALLOC_IN_QUOTA, NULL);
  79. if (IS_ERR(session_keyring)) {
  80. ret = PTR_ERR(session_keyring);
  81. goto error_release;
  82. }
  83. /* we install a link from the user session keyring to
  84. * the user keyring */
  85. ret = key_link(session_keyring, uid_keyring);
  86. if (ret < 0)
  87. goto error_release_both;
  88. }
  89. /* install the keyrings */
  90. user->uid_keyring = uid_keyring;
  91. user->session_keyring = session_keyring;
  92. }
  93. mutex_unlock(&key_user_keyring_mutex);
  94. kleave(" = 0");
  95. return 0;
  96. error_release_both:
  97. key_put(session_keyring);
  98. error_release:
  99. key_put(uid_keyring);
  100. error:
  101. mutex_unlock(&key_user_keyring_mutex);
  102. kleave(" = %d", ret);
  103. return ret;
  104. }
  105. /*
  106. * Install a fresh thread keyring directly to new credentials. This keyring is
  107. * allowed to overrun the quota.
  108. */
  109. int install_thread_keyring_to_cred(struct cred *new)
  110. {
  111. struct key *keyring;
  112. keyring = keyring_alloc("_tid", new->uid, new->gid, new,
  113. KEY_ALLOC_QUOTA_OVERRUN, NULL);
  114. if (IS_ERR(keyring))
  115. return PTR_ERR(keyring);
  116. new->thread_keyring = keyring;
  117. return 0;
  118. }
  119. /*
  120. * Install a fresh thread keyring, discarding the old one.
  121. */
  122. static int install_thread_keyring(void)
  123. {
  124. struct cred *new;
  125. int ret;
  126. new = prepare_creds();
  127. if (!new)
  128. return -ENOMEM;
  129. BUG_ON(new->thread_keyring);
  130. ret = install_thread_keyring_to_cred(new);
  131. if (ret < 0) {
  132. abort_creds(new);
  133. return ret;
  134. }
  135. return commit_creds(new);
  136. }
  137. /*
  138. * Install a process keyring directly to a credentials struct.
  139. *
  140. * Returns -EEXIST if there was already a process keyring, 0 if one installed,
  141. * and other value on any other error
  142. */
  143. int install_process_keyring_to_cred(struct cred *new)
  144. {
  145. struct key *keyring;
  146. int ret;
  147. if (new->tgcred->process_keyring)
  148. return -EEXIST;
  149. keyring = keyring_alloc("_pid", new->uid, new->gid,
  150. new, KEY_ALLOC_QUOTA_OVERRUN, NULL);
  151. if (IS_ERR(keyring))
  152. return PTR_ERR(keyring);
  153. spin_lock_irq(&new->tgcred->lock);
  154. if (!new->tgcred->process_keyring) {
  155. new->tgcred->process_keyring = keyring;
  156. keyring = NULL;
  157. ret = 0;
  158. } else {
  159. ret = -EEXIST;
  160. }
  161. spin_unlock_irq(&new->tgcred->lock);
  162. key_put(keyring);
  163. return ret;
  164. }
  165. /*
  166. * Make sure a process keyring is installed for the current process. The
  167. * existing process keyring is not replaced.
  168. *
  169. * Returns 0 if there is a process keyring by the end of this function, some
  170. * error otherwise.
  171. */
  172. static int install_process_keyring(void)
  173. {
  174. struct cred *new;
  175. int ret;
  176. new = prepare_creds();
  177. if (!new)
  178. return -ENOMEM;
  179. ret = install_process_keyring_to_cred(new);
  180. if (ret < 0) {
  181. abort_creds(new);
  182. return ret != -EEXIST ? ret : 0;
  183. }
  184. return commit_creds(new);
  185. }
  186. /*
  187. * Install a session keyring directly to a credentials struct.
  188. */
  189. int install_session_keyring_to_cred(struct cred *cred, struct key *keyring)
  190. {
  191. unsigned long flags;
  192. struct key *old;
  193. might_sleep();
  194. /* create an empty session keyring */
  195. if (!keyring) {
  196. flags = KEY_ALLOC_QUOTA_OVERRUN;
  197. if (cred->tgcred->session_keyring)
  198. flags = KEY_ALLOC_IN_QUOTA;
  199. keyring = keyring_alloc("_ses", cred->uid, cred->gid,
  200. cred, flags, NULL);
  201. if (IS_ERR(keyring))
  202. return PTR_ERR(keyring);
  203. } else {
  204. atomic_inc(&keyring->usage);
  205. }
  206. /* install the keyring */
  207. spin_lock_irq(&cred->tgcred->lock);
  208. old = cred->tgcred->session_keyring;
  209. rcu_assign_pointer(cred->tgcred->session_keyring, keyring);
  210. spin_unlock_irq(&cred->tgcred->lock);
  211. /* we're using RCU on the pointer, but there's no point synchronising
  212. * on it if it didn't previously point to anything */
  213. if (old) {
  214. synchronize_rcu();
  215. key_put(old);
  216. }
  217. return 0;
  218. }
  219. /*
  220. * Install a session keyring, discarding the old one. If a keyring is not
  221. * supplied, an empty one is invented.
  222. */
  223. static int install_session_keyring(struct key *keyring)
  224. {
  225. struct cred *new;
  226. int ret;
  227. new = prepare_creds();
  228. if (!new)
  229. return -ENOMEM;
  230. ret = install_session_keyring_to_cred(new, NULL);
  231. if (ret < 0) {
  232. abort_creds(new);
  233. return ret;
  234. }
  235. return commit_creds(new);
  236. }
  237. /*
  238. * Handle the fsuid changing.
  239. */
  240. void key_fsuid_changed(struct task_struct *tsk)
  241. {
  242. /* update the ownership of the thread keyring */
  243. BUG_ON(!tsk->cred);
  244. if (tsk->cred->thread_keyring) {
  245. down_write(&tsk->cred->thread_keyring->sem);
  246. tsk->cred->thread_keyring->uid = tsk->cred->fsuid;
  247. up_write(&tsk->cred->thread_keyring->sem);
  248. }
  249. }
  250. /*
  251. * Handle the fsgid changing.
  252. */
  253. void key_fsgid_changed(struct task_struct *tsk)
  254. {
  255. /* update the ownership of the thread keyring */
  256. BUG_ON(!tsk->cred);
  257. if (tsk->cred->thread_keyring) {
  258. down_write(&tsk->cred->thread_keyring->sem);
  259. tsk->cred->thread_keyring->gid = tsk->cred->fsgid;
  260. up_write(&tsk->cred->thread_keyring->sem);
  261. }
  262. }
  263. /*
  264. * Search the process keyrings attached to the supplied cred for the first
  265. * matching key.
  266. *
  267. * The search criteria are the type and the match function. The description is
  268. * given to the match function as a parameter, but doesn't otherwise influence
  269. * the search. Typically the match function will compare the description
  270. * parameter to the key's description.
  271. *
  272. * This can only search keyrings that grant Search permission to the supplied
  273. * credentials. Keyrings linked to searched keyrings will also be searched if
  274. * they grant Search permission too. Keys can only be found if they grant
  275. * Search permission to the credentials.
  276. *
  277. * Returns a pointer to the key with the key usage count incremented if
  278. * successful, -EAGAIN if we didn't find any matching key or -ENOKEY if we only
  279. * matched negative keys.
  280. *
  281. * In the case of a successful return, the possession attribute is set on the
  282. * returned key reference.
  283. */
  284. key_ref_t search_my_process_keyrings(struct key_type *type,
  285. const void *description,
  286. key_match_func_t match,
  287. const struct cred *cred)
  288. {
  289. key_ref_t key_ref, ret, err;
  290. /* we want to return -EAGAIN or -ENOKEY if any of the keyrings were
  291. * searchable, but we failed to find a key or we found a negative key;
  292. * otherwise we want to return a sample error (probably -EACCES) if
  293. * none of the keyrings were searchable
  294. *
  295. * in terms of priority: success > -ENOKEY > -EAGAIN > other error
  296. */
  297. key_ref = NULL;
  298. ret = NULL;
  299. err = ERR_PTR(-EAGAIN);
  300. /* search the thread keyring first */
  301. if (cred->thread_keyring) {
  302. key_ref = keyring_search_aux(
  303. make_key_ref(cred->thread_keyring, 1),
  304. cred, type, description, match);
  305. if (!IS_ERR(key_ref))
  306. goto found;
  307. switch (PTR_ERR(key_ref)) {
  308. case -EAGAIN: /* no key */
  309. if (ret)
  310. break;
  311. case -ENOKEY: /* negative key */
  312. ret = key_ref;
  313. break;
  314. default:
  315. err = key_ref;
  316. break;
  317. }
  318. }
  319. /* search the process keyring second */
  320. if (cred->tgcred->process_keyring) {
  321. key_ref = keyring_search_aux(
  322. make_key_ref(cred->tgcred->process_keyring, 1),
  323. cred, type, description, match);
  324. if (!IS_ERR(key_ref))
  325. goto found;
  326. switch (PTR_ERR(key_ref)) {
  327. case -EAGAIN: /* no key */
  328. if (ret)
  329. break;
  330. case -ENOKEY: /* negative key */
  331. ret = key_ref;
  332. break;
  333. default:
  334. err = key_ref;
  335. break;
  336. }
  337. }
  338. /* search the session keyring */
  339. if (cred->tgcred->session_keyring) {
  340. rcu_read_lock();
  341. key_ref = keyring_search_aux(
  342. make_key_ref(rcu_dereference(
  343. cred->tgcred->session_keyring),
  344. 1),
  345. cred, type, description, match);
  346. rcu_read_unlock();
  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. /* or search the user-session keyring */
  362. else if (cred->user->session_keyring) {
  363. key_ref = keyring_search_aux(
  364. make_key_ref(cred->user->session_keyring, 1),
  365. cred, type, description, match);
  366. if (!IS_ERR(key_ref))
  367. goto found;
  368. switch (PTR_ERR(key_ref)) {
  369. case -EAGAIN: /* no key */
  370. if (ret)
  371. break;
  372. case -ENOKEY: /* negative key */
  373. ret = key_ref;
  374. break;
  375. default:
  376. err = key_ref;
  377. break;
  378. }
  379. }
  380. /* no key - decide on the error we're going to go for */
  381. key_ref = ret ? ret : err;
  382. found:
  383. return key_ref;
  384. }
  385. /*
  386. * Search the process keyrings attached to the supplied cred for the first
  387. * matching key in the manner of search_my_process_keyrings(), but also search
  388. * the keys attached to the assumed authorisation key using its credentials if
  389. * one is available.
  390. *
  391. * Return same as search_my_process_keyrings().
  392. */
  393. key_ref_t search_process_keyrings(struct key_type *type,
  394. const void *description,
  395. key_match_func_t match,
  396. const struct cred *cred)
  397. {
  398. struct request_key_auth *rka;
  399. key_ref_t key_ref, ret = ERR_PTR(-EACCES), err;
  400. might_sleep();
  401. key_ref = search_my_process_keyrings(type, description, match, cred);
  402. if (!IS_ERR(key_ref))
  403. goto found;
  404. err = key_ref;
  405. /* if this process has an instantiation authorisation key, then we also
  406. * search the keyrings of the process mentioned there
  407. * - we don't permit access to request_key auth keys via this method
  408. */
  409. if (cred->request_key_auth &&
  410. cred == current_cred() &&
  411. type != &key_type_request_key_auth
  412. ) {
  413. /* defend against the auth key being revoked */
  414. down_read(&cred->request_key_auth->sem);
  415. if (key_validate(cred->request_key_auth) == 0) {
  416. rka = cred->request_key_auth->payload.data;
  417. key_ref = search_process_keyrings(type, description,
  418. match, rka->cred);
  419. up_read(&cred->request_key_auth->sem);
  420. if (!IS_ERR(key_ref))
  421. goto found;
  422. ret = key_ref;
  423. } else {
  424. up_read(&cred->request_key_auth->sem);
  425. }
  426. }
  427. /* no key - decide on the error we're going to go for */
  428. if (err == ERR_PTR(-ENOKEY) || ret == ERR_PTR(-ENOKEY))
  429. key_ref = ERR_PTR(-ENOKEY);
  430. else if (err == ERR_PTR(-EACCES))
  431. key_ref = ret;
  432. else
  433. key_ref = err;
  434. found:
  435. return key_ref;
  436. }
  437. /*
  438. * See if the key we're looking at is the target key.
  439. */
  440. int lookup_user_key_possessed(const struct key *key, const void *target)
  441. {
  442. return key == target;
  443. }
  444. /*
  445. * Look up a key ID given us by userspace with a given permissions mask to get
  446. * the key it refers to.
  447. *
  448. * Flags can be passed to request that special keyrings be created if referred
  449. * to directly, to permit partially constructed keys to be found and to skip
  450. * validity and permission checks on the found key.
  451. *
  452. * Returns a pointer to the key with an incremented usage count if successful;
  453. * -EINVAL if the key ID is invalid; -ENOKEY if the key ID does not correspond
  454. * to a key or the best found key was a negative key; -EKEYREVOKED or
  455. * -EKEYEXPIRED if the best found key was revoked or expired; -EACCES if the
  456. * found key doesn't grant the requested permit or the LSM denied access to it;
  457. * or -ENOMEM if a special keyring couldn't be created.
  458. *
  459. * In the case of a successful return, the possession attribute is set on the
  460. * returned key reference.
  461. */
  462. key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
  463. key_perm_t perm)
  464. {
  465. struct request_key_auth *rka;
  466. const struct cred *cred;
  467. struct key *key;
  468. key_ref_t key_ref, skey_ref;
  469. int ret;
  470. try_again:
  471. cred = get_current_cred();
  472. key_ref = ERR_PTR(-ENOKEY);
  473. switch (id) {
  474. case KEY_SPEC_THREAD_KEYRING:
  475. if (!cred->thread_keyring) {
  476. if (!(lflags & KEY_LOOKUP_CREATE))
  477. goto error;
  478. ret = install_thread_keyring();
  479. if (ret < 0) {
  480. key_ref = ERR_PTR(ret);
  481. goto error;
  482. }
  483. goto reget_creds;
  484. }
  485. key = cred->thread_keyring;
  486. atomic_inc(&key->usage);
  487. key_ref = make_key_ref(key, 1);
  488. break;
  489. case KEY_SPEC_PROCESS_KEYRING:
  490. if (!cred->tgcred->process_keyring) {
  491. if (!(lflags & KEY_LOOKUP_CREATE))
  492. goto error;
  493. ret = install_process_keyring();
  494. if (ret < 0) {
  495. key_ref = ERR_PTR(ret);
  496. goto error;
  497. }
  498. goto reget_creds;
  499. }
  500. key = cred->tgcred->process_keyring;
  501. atomic_inc(&key->usage);
  502. key_ref = make_key_ref(key, 1);
  503. break;
  504. case KEY_SPEC_SESSION_KEYRING:
  505. if (!cred->tgcred->session_keyring) {
  506. /* always install a session keyring upon access if one
  507. * doesn't exist yet */
  508. ret = install_user_keyrings();
  509. if (ret < 0)
  510. goto error;
  511. ret = install_session_keyring(
  512. cred->user->session_keyring);
  513. if (ret < 0)
  514. goto error;
  515. goto reget_creds;
  516. }
  517. rcu_read_lock();
  518. key = rcu_dereference(cred->tgcred->session_keyring);
  519. atomic_inc(&key->usage);
  520. rcu_read_unlock();
  521. key_ref = make_key_ref(key, 1);
  522. break;
  523. case KEY_SPEC_USER_KEYRING:
  524. if (!cred->user->uid_keyring) {
  525. ret = install_user_keyrings();
  526. if (ret < 0)
  527. goto error;
  528. }
  529. key = cred->user->uid_keyring;
  530. atomic_inc(&key->usage);
  531. key_ref = make_key_ref(key, 1);
  532. break;
  533. case KEY_SPEC_USER_SESSION_KEYRING:
  534. if (!cred->user->session_keyring) {
  535. ret = install_user_keyrings();
  536. if (ret < 0)
  537. goto error;
  538. }
  539. key = cred->user->session_keyring;
  540. atomic_inc(&key->usage);
  541. key_ref = make_key_ref(key, 1);
  542. break;
  543. case KEY_SPEC_GROUP_KEYRING:
  544. /* group keyrings are not yet supported */
  545. key_ref = ERR_PTR(-EINVAL);
  546. goto error;
  547. case KEY_SPEC_REQKEY_AUTH_KEY:
  548. key = cred->request_key_auth;
  549. if (!key)
  550. goto error;
  551. atomic_inc(&key->usage);
  552. key_ref = make_key_ref(key, 1);
  553. break;
  554. case KEY_SPEC_REQUESTOR_KEYRING:
  555. if (!cred->request_key_auth)
  556. goto error;
  557. down_read(&cred->request_key_auth->sem);
  558. if (cred->request_key_auth->flags & KEY_FLAG_REVOKED) {
  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. error:
  622. put_cred(cred);
  623. return key_ref;
  624. invalid_key:
  625. key_ref_put(key_ref);
  626. key_ref = ERR_PTR(ret);
  627. goto error;
  628. /* if we attempted to install a keyring, then it may have caused new
  629. * creds to be installed */
  630. reget_creds:
  631. put_cred(cred);
  632. goto try_again;
  633. }
  634. /*
  635. * Join the named keyring as the session keyring if possible else attempt to
  636. * create a new one of that name and join that.
  637. *
  638. * If the name is NULL, an empty anonymous keyring will be installed as the
  639. * session keyring.
  640. *
  641. * Named session keyrings are joined with a semaphore held to prevent the
  642. * keyrings from going away whilst the attempt is made to going them and also
  643. * to prevent a race in creating compatible session keyrings.
  644. */
  645. long join_session_keyring(const char *name)
  646. {
  647. const struct cred *old;
  648. struct cred *new;
  649. struct key *keyring;
  650. long ret, serial;
  651. /* only permit this if there's a single thread in the thread group -
  652. * this avoids us having to adjust the creds on all threads and risking
  653. * ENOMEM */
  654. if (!current_is_single_threaded())
  655. return -EMLINK;
  656. new = prepare_creds();
  657. if (!new)
  658. return -ENOMEM;
  659. old = current_cred();
  660. /* if no name is provided, install an anonymous keyring */
  661. if (!name) {
  662. ret = install_session_keyring_to_cred(new, NULL);
  663. if (ret < 0)
  664. goto error;
  665. serial = new->tgcred->session_keyring->serial;
  666. ret = commit_creds(new);
  667. if (ret == 0)
  668. ret = serial;
  669. goto okay;
  670. }
  671. /* allow the user to join or create a named keyring */
  672. mutex_lock(&key_session_mutex);
  673. /* look for an existing keyring of this name */
  674. keyring = find_keyring_by_name(name, false);
  675. if (PTR_ERR(keyring) == -ENOKEY) {
  676. /* not found - try and create a new one */
  677. keyring = keyring_alloc(name, old->uid, old->gid, old,
  678. KEY_ALLOC_IN_QUOTA, NULL);
  679. if (IS_ERR(keyring)) {
  680. ret = PTR_ERR(keyring);
  681. goto error2;
  682. }
  683. } else if (IS_ERR(keyring)) {
  684. ret = PTR_ERR(keyring);
  685. goto error2;
  686. }
  687. /* we've got a keyring - now to install it */
  688. ret = install_session_keyring_to_cred(new, keyring);
  689. if (ret < 0)
  690. goto error2;
  691. commit_creds(new);
  692. mutex_unlock(&key_session_mutex);
  693. ret = keyring->serial;
  694. key_put(keyring);
  695. okay:
  696. return ret;
  697. error2:
  698. mutex_unlock(&key_session_mutex);
  699. error:
  700. abort_creds(new);
  701. return ret;
  702. }
  703. /*
  704. * Replace a process's session keyring on behalf of one of its children when
  705. * the target process is about to resume userspace execution.
  706. */
  707. void key_replace_session_keyring(void)
  708. {
  709. const struct cred *old;
  710. struct cred *new;
  711. if (!current->replacement_session_keyring)
  712. return;
  713. write_lock_irq(&tasklist_lock);
  714. new = current->replacement_session_keyring;
  715. current->replacement_session_keyring = NULL;
  716. write_unlock_irq(&tasklist_lock);
  717. if (!new)
  718. return;
  719. old = current_cred();
  720. new-> uid = old-> uid;
  721. new-> euid = old-> euid;
  722. new-> suid = old-> suid;
  723. new->fsuid = old->fsuid;
  724. new-> gid = old-> gid;
  725. new-> egid = old-> egid;
  726. new-> sgid = old-> sgid;
  727. new->fsgid = old->fsgid;
  728. new->user = get_uid(old->user);
  729. new->group_info = get_group_info(old->group_info);
  730. new->securebits = old->securebits;
  731. new->cap_inheritable = old->cap_inheritable;
  732. new->cap_permitted = old->cap_permitted;
  733. new->cap_effective = old->cap_effective;
  734. new->cap_bset = old->cap_bset;
  735. new->jit_keyring = old->jit_keyring;
  736. new->thread_keyring = key_get(old->thread_keyring);
  737. new->tgcred->tgid = old->tgcred->tgid;
  738. new->tgcred->process_keyring = key_get(old->tgcred->process_keyring);
  739. security_transfer_creds(new, old);
  740. commit_creds(new);
  741. }