process_keys.c 21 KB

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