process_keys.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  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, keyring);
  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. bool no_state_check,
  288. const struct cred *cred)
  289. {
  290. key_ref_t key_ref, ret, err;
  291. /* we want to return -EAGAIN or -ENOKEY if any of the keyrings were
  292. * searchable, but we failed to find a key or we found a negative key;
  293. * otherwise we want to return a sample error (probably -EACCES) if
  294. * none of the keyrings were searchable
  295. *
  296. * in terms of priority: success > -ENOKEY > -EAGAIN > other error
  297. */
  298. key_ref = NULL;
  299. ret = NULL;
  300. err = ERR_PTR(-EAGAIN);
  301. /* search the thread keyring first */
  302. if (cred->thread_keyring) {
  303. key_ref = keyring_search_aux(
  304. make_key_ref(cred->thread_keyring, 1),
  305. cred, type, description, match, no_state_check);
  306. if (!IS_ERR(key_ref))
  307. goto found;
  308. switch (PTR_ERR(key_ref)) {
  309. case -EAGAIN: /* no key */
  310. case -ENOKEY: /* negative key */
  311. ret = key_ref;
  312. break;
  313. default:
  314. err = key_ref;
  315. break;
  316. }
  317. }
  318. /* search the process keyring second */
  319. if (cred->tgcred->process_keyring) {
  320. key_ref = keyring_search_aux(
  321. make_key_ref(cred->tgcred->process_keyring, 1),
  322. cred, type, description, match, no_state_check);
  323. if (!IS_ERR(key_ref))
  324. goto found;
  325. switch (PTR_ERR(key_ref)) {
  326. case -EAGAIN: /* no key */
  327. if (ret)
  328. break;
  329. case -ENOKEY: /* negative key */
  330. ret = key_ref;
  331. break;
  332. default:
  333. err = key_ref;
  334. break;
  335. }
  336. }
  337. /* search the session keyring */
  338. if (cred->tgcred->session_keyring) {
  339. rcu_read_lock();
  340. key_ref = keyring_search_aux(
  341. make_key_ref(rcu_dereference(
  342. cred->tgcred->session_keyring),
  343. 1),
  344. cred, type, description, match, no_state_check);
  345. rcu_read_unlock();
  346. if (!IS_ERR(key_ref))
  347. goto found;
  348. switch (PTR_ERR(key_ref)) {
  349. case -EAGAIN: /* no key */
  350. if (ret)
  351. break;
  352. case -ENOKEY: /* negative key */
  353. ret = key_ref;
  354. break;
  355. default:
  356. err = key_ref;
  357. break;
  358. }
  359. }
  360. /* or search the user-session keyring */
  361. else if (cred->user->session_keyring) {
  362. key_ref = keyring_search_aux(
  363. make_key_ref(cred->user->session_keyring, 1),
  364. cred, type, description, match, no_state_check);
  365. if (!IS_ERR(key_ref))
  366. goto found;
  367. switch (PTR_ERR(key_ref)) {
  368. case -EAGAIN: /* no key */
  369. if (ret)
  370. break;
  371. case -ENOKEY: /* negative key */
  372. ret = key_ref;
  373. break;
  374. default:
  375. err = key_ref;
  376. break;
  377. }
  378. }
  379. /* no key - decide on the error we're going to go for */
  380. key_ref = ret ? ret : err;
  381. found:
  382. return key_ref;
  383. }
  384. /*
  385. * Search the process keyrings attached to the supplied cred for the first
  386. * matching key in the manner of search_my_process_keyrings(), but also search
  387. * the keys attached to the assumed authorisation key using its credentials if
  388. * one is available.
  389. *
  390. * Return same as search_my_process_keyrings().
  391. */
  392. key_ref_t search_process_keyrings(struct key_type *type,
  393. const void *description,
  394. key_match_func_t match,
  395. const struct cred *cred)
  396. {
  397. struct request_key_auth *rka;
  398. key_ref_t key_ref, ret = ERR_PTR(-EACCES), err;
  399. might_sleep();
  400. key_ref = search_my_process_keyrings(type, description, match,
  401. false, 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. if (lflags & KEY_LOOKUP_CREATE)
  512. ret = join_session_keyring(NULL);
  513. else
  514. ret = install_session_keyring(
  515. cred->user->session_keyring);
  516. if (ret < 0)
  517. goto error;
  518. goto reget_creds;
  519. } else if (cred->tgcred->session_keyring ==
  520. cred->user->session_keyring &&
  521. lflags & KEY_LOOKUP_CREATE) {
  522. ret = join_session_keyring(NULL);
  523. if (ret < 0)
  524. goto error;
  525. goto reget_creds;
  526. }
  527. rcu_read_lock();
  528. key = rcu_dereference(cred->tgcred->session_keyring);
  529. atomic_inc(&key->usage);
  530. rcu_read_unlock();
  531. key_ref = make_key_ref(key, 1);
  532. break;
  533. case KEY_SPEC_USER_KEYRING:
  534. if (!cred->user->uid_keyring) {
  535. ret = install_user_keyrings();
  536. if (ret < 0)
  537. goto error;
  538. }
  539. key = cred->user->uid_keyring;
  540. atomic_inc(&key->usage);
  541. key_ref = make_key_ref(key, 1);
  542. break;
  543. case KEY_SPEC_USER_SESSION_KEYRING:
  544. if (!cred->user->session_keyring) {
  545. ret = install_user_keyrings();
  546. if (ret < 0)
  547. goto error;
  548. }
  549. key = cred->user->session_keyring;
  550. atomic_inc(&key->usage);
  551. key_ref = make_key_ref(key, 1);
  552. break;
  553. case KEY_SPEC_GROUP_KEYRING:
  554. /* group keyrings are not yet supported */
  555. key_ref = ERR_PTR(-EINVAL);
  556. goto error;
  557. case KEY_SPEC_REQKEY_AUTH_KEY:
  558. key = cred->request_key_auth;
  559. if (!key)
  560. goto error;
  561. atomic_inc(&key->usage);
  562. key_ref = make_key_ref(key, 1);
  563. break;
  564. case KEY_SPEC_REQUESTOR_KEYRING:
  565. if (!cred->request_key_auth)
  566. goto error;
  567. down_read(&cred->request_key_auth->sem);
  568. if (test_bit(KEY_FLAG_REVOKED,
  569. &cred->request_key_auth->flags)) {
  570. key_ref = ERR_PTR(-EKEYREVOKED);
  571. key = NULL;
  572. } else {
  573. rka = cred->request_key_auth->payload.data;
  574. key = rka->dest_keyring;
  575. atomic_inc(&key->usage);
  576. }
  577. up_read(&cred->request_key_auth->sem);
  578. if (!key)
  579. goto error;
  580. key_ref = make_key_ref(key, 1);
  581. break;
  582. default:
  583. key_ref = ERR_PTR(-EINVAL);
  584. if (id < 1)
  585. goto error;
  586. key = key_lookup(id);
  587. if (IS_ERR(key)) {
  588. key_ref = ERR_CAST(key);
  589. goto error;
  590. }
  591. key_ref = make_key_ref(key, 0);
  592. /* check to see if we possess the key */
  593. skey_ref = search_process_keyrings(key->type, key,
  594. lookup_user_key_possessed,
  595. cred);
  596. if (!IS_ERR(skey_ref)) {
  597. key_put(key);
  598. key_ref = skey_ref;
  599. }
  600. break;
  601. }
  602. /* unlink does not use the nominated key in any way, so can skip all
  603. * the permission checks as it is only concerned with the keyring */
  604. if (lflags & KEY_LOOKUP_FOR_UNLINK) {
  605. ret = 0;
  606. goto error;
  607. }
  608. if (!(lflags & KEY_LOOKUP_PARTIAL)) {
  609. ret = wait_for_key_construction(key, true);
  610. switch (ret) {
  611. case -ERESTARTSYS:
  612. goto invalid_key;
  613. default:
  614. if (perm)
  615. goto invalid_key;
  616. case 0:
  617. break;
  618. }
  619. } else if (perm) {
  620. ret = key_validate(key);
  621. if (ret < 0)
  622. goto invalid_key;
  623. }
  624. ret = -EIO;
  625. if (!(lflags & KEY_LOOKUP_PARTIAL) &&
  626. !test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
  627. goto invalid_key;
  628. /* check the permissions */
  629. ret = key_task_permission(key_ref, cred, perm);
  630. if (ret < 0)
  631. goto invalid_key;
  632. key->last_used_at = current_kernel_time().tv_sec;
  633. error:
  634. put_cred(cred);
  635. return key_ref;
  636. invalid_key:
  637. key_ref_put(key_ref);
  638. key_ref = ERR_PTR(ret);
  639. goto error;
  640. /* if we attempted to install a keyring, then it may have caused new
  641. * creds to be installed */
  642. reget_creds:
  643. put_cred(cred);
  644. goto try_again;
  645. }
  646. /*
  647. * Join the named keyring as the session keyring if possible else attempt to
  648. * create a new one of that name and join that.
  649. *
  650. * If the name is NULL, an empty anonymous keyring will be installed as the
  651. * session keyring.
  652. *
  653. * Named session keyrings are joined with a semaphore held to prevent the
  654. * keyrings from going away whilst the attempt is made to going them and also
  655. * to prevent a race in creating compatible session keyrings.
  656. */
  657. long join_session_keyring(const char *name)
  658. {
  659. const struct cred *old;
  660. struct cred *new;
  661. struct key *keyring;
  662. long ret, serial;
  663. /* only permit this if there's a single thread in the thread group -
  664. * this avoids us having to adjust the creds on all threads and risking
  665. * ENOMEM */
  666. if (!current_is_single_threaded())
  667. return -EMLINK;
  668. new = prepare_creds();
  669. if (!new)
  670. return -ENOMEM;
  671. old = current_cred();
  672. /* if no name is provided, install an anonymous keyring */
  673. if (!name) {
  674. ret = install_session_keyring_to_cred(new, NULL);
  675. if (ret < 0)
  676. goto error;
  677. serial = new->tgcred->session_keyring->serial;
  678. ret = commit_creds(new);
  679. if (ret == 0)
  680. ret = serial;
  681. goto okay;
  682. }
  683. /* allow the user to join or create a named keyring */
  684. mutex_lock(&key_session_mutex);
  685. /* look for an existing keyring of this name */
  686. keyring = find_keyring_by_name(name, false);
  687. if (PTR_ERR(keyring) == -ENOKEY) {
  688. /* not found - try and create a new one */
  689. keyring = keyring_alloc(name, old->uid, old->gid, old,
  690. KEY_ALLOC_IN_QUOTA, NULL);
  691. if (IS_ERR(keyring)) {
  692. ret = PTR_ERR(keyring);
  693. goto error2;
  694. }
  695. } else if (IS_ERR(keyring)) {
  696. ret = PTR_ERR(keyring);
  697. goto error2;
  698. }
  699. /* we've got a keyring - now to install it */
  700. ret = install_session_keyring_to_cred(new, keyring);
  701. if (ret < 0)
  702. goto error2;
  703. commit_creds(new);
  704. mutex_unlock(&key_session_mutex);
  705. ret = keyring->serial;
  706. key_put(keyring);
  707. okay:
  708. return ret;
  709. error2:
  710. mutex_unlock(&key_session_mutex);
  711. error:
  712. abort_creds(new);
  713. return ret;
  714. }
  715. /*
  716. * Replace a process's session keyring on behalf of one of its children when
  717. * the target process is about to resume userspace execution.
  718. */
  719. void key_change_session_keyring(struct callback_head *twork)
  720. {
  721. const struct cred *old = current_cred();
  722. struct cred *new = container_of(twork, struct cred, rcu);
  723. if (unlikely(current->flags & PF_EXITING)) {
  724. put_cred(new);
  725. return;
  726. }
  727. new-> uid = old-> uid;
  728. new-> euid = old-> euid;
  729. new-> suid = old-> suid;
  730. new->fsuid = old->fsuid;
  731. new-> gid = old-> gid;
  732. new-> egid = old-> egid;
  733. new-> sgid = old-> sgid;
  734. new->fsgid = old->fsgid;
  735. new->user = get_uid(old->user);
  736. new->user_ns = get_user_ns(new->user_ns);
  737. new->group_info = get_group_info(old->group_info);
  738. new->securebits = old->securebits;
  739. new->cap_inheritable = old->cap_inheritable;
  740. new->cap_permitted = old->cap_permitted;
  741. new->cap_effective = old->cap_effective;
  742. new->cap_bset = old->cap_bset;
  743. new->jit_keyring = old->jit_keyring;
  744. new->thread_keyring = key_get(old->thread_keyring);
  745. new->tgcred->tgid = old->tgcred->tgid;
  746. new->tgcred->process_keyring = key_get(old->tgcred->process_keyring);
  747. security_transfer_creds(new, old);
  748. commit_creds(new);
  749. }