process_keys.c 21 KB

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