process_keys.c 21 KB

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