process_keys.c 20 KB

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