process_keys.c 21 KB

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