process_keys.c 23 KB

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