process_keys.c 21 KB

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