process_keys.c 21 KB

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