process_keys.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  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. bool lookup_user_key_possessed(const struct key *key,
  422. const struct key_match_data *match_data)
  423. {
  424. return key == match_data->raw_data;
  425. }
  426. /*
  427. * Look up a key ID given us by userspace with a given permissions mask to get
  428. * the key it refers to.
  429. *
  430. * Flags can be passed to request that special keyrings be created if referred
  431. * to directly, to permit partially constructed keys to be found and to skip
  432. * validity and permission checks on the found key.
  433. *
  434. * Returns a pointer to the key with an incremented usage count if successful;
  435. * -EINVAL if the key ID is invalid; -ENOKEY if the key ID does not correspond
  436. * to a key or the best found key was a negative key; -EKEYREVOKED or
  437. * -EKEYEXPIRED if the best found key was revoked or expired; -EACCES if the
  438. * found key doesn't grant the requested permit or the LSM denied access to it;
  439. * or -ENOMEM if a special keyring couldn't be created.
  440. *
  441. * In the case of a successful return, the possession attribute is set on the
  442. * returned key reference.
  443. */
  444. key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
  445. key_perm_t perm)
  446. {
  447. struct keyring_search_context ctx = {
  448. .match_data.cmp = lookup_user_key_possessed,
  449. .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
  450. .flags = KEYRING_SEARCH_NO_STATE_CHECK,
  451. };
  452. struct request_key_auth *rka;
  453. struct key *key;
  454. key_ref_t key_ref, skey_ref;
  455. int ret;
  456. try_again:
  457. ctx.cred = get_current_cred();
  458. key_ref = ERR_PTR(-ENOKEY);
  459. switch (id) {
  460. case KEY_SPEC_THREAD_KEYRING:
  461. if (!ctx.cred->thread_keyring) {
  462. if (!(lflags & KEY_LOOKUP_CREATE))
  463. goto error;
  464. ret = install_thread_keyring();
  465. if (ret < 0) {
  466. key_ref = ERR_PTR(ret);
  467. goto error;
  468. }
  469. goto reget_creds;
  470. }
  471. key = ctx.cred->thread_keyring;
  472. __key_get(key);
  473. key_ref = make_key_ref(key, 1);
  474. break;
  475. case KEY_SPEC_PROCESS_KEYRING:
  476. if (!ctx.cred->process_keyring) {
  477. if (!(lflags & KEY_LOOKUP_CREATE))
  478. goto error;
  479. ret = install_process_keyring();
  480. if (ret < 0) {
  481. key_ref = ERR_PTR(ret);
  482. goto error;
  483. }
  484. goto reget_creds;
  485. }
  486. key = ctx.cred->process_keyring;
  487. __key_get(key);
  488. key_ref = make_key_ref(key, 1);
  489. break;
  490. case KEY_SPEC_SESSION_KEYRING:
  491. if (!ctx.cred->session_keyring) {
  492. /* always install a session keyring upon access if one
  493. * doesn't exist yet */
  494. ret = install_user_keyrings();
  495. if (ret < 0)
  496. goto error;
  497. if (lflags & KEY_LOOKUP_CREATE)
  498. ret = join_session_keyring(NULL);
  499. else
  500. ret = install_session_keyring(
  501. ctx.cred->user->session_keyring);
  502. if (ret < 0)
  503. goto error;
  504. goto reget_creds;
  505. } else if (ctx.cred->session_keyring ==
  506. ctx.cred->user->session_keyring &&
  507. lflags & KEY_LOOKUP_CREATE) {
  508. ret = join_session_keyring(NULL);
  509. if (ret < 0)
  510. goto error;
  511. goto reget_creds;
  512. }
  513. rcu_read_lock();
  514. key = rcu_dereference(ctx.cred->session_keyring);
  515. __key_get(key);
  516. rcu_read_unlock();
  517. key_ref = make_key_ref(key, 1);
  518. break;
  519. case KEY_SPEC_USER_KEYRING:
  520. if (!ctx.cred->user->uid_keyring) {
  521. ret = install_user_keyrings();
  522. if (ret < 0)
  523. goto error;
  524. }
  525. key = ctx.cred->user->uid_keyring;
  526. __key_get(key);
  527. key_ref = make_key_ref(key, 1);
  528. break;
  529. case KEY_SPEC_USER_SESSION_KEYRING:
  530. if (!ctx.cred->user->session_keyring) {
  531. ret = install_user_keyrings();
  532. if (ret < 0)
  533. goto error;
  534. }
  535. key = ctx.cred->user->session_keyring;
  536. __key_get(key);
  537. key_ref = make_key_ref(key, 1);
  538. break;
  539. case KEY_SPEC_GROUP_KEYRING:
  540. /* group keyrings are not yet supported */
  541. key_ref = ERR_PTR(-EINVAL);
  542. goto error;
  543. case KEY_SPEC_REQKEY_AUTH_KEY:
  544. key = ctx.cred->request_key_auth;
  545. if (!key)
  546. goto error;
  547. __key_get(key);
  548. key_ref = make_key_ref(key, 1);
  549. break;
  550. case KEY_SPEC_REQUESTOR_KEYRING:
  551. if (!ctx.cred->request_key_auth)
  552. goto error;
  553. down_read(&ctx.cred->request_key_auth->sem);
  554. if (test_bit(KEY_FLAG_REVOKED,
  555. &ctx.cred->request_key_auth->flags)) {
  556. key_ref = ERR_PTR(-EKEYREVOKED);
  557. key = NULL;
  558. } else {
  559. rka = ctx.cred->request_key_auth->payload.data;
  560. key = rka->dest_keyring;
  561. __key_get(key);
  562. }
  563. up_read(&ctx.cred->request_key_auth->sem);
  564. if (!key)
  565. goto error;
  566. key_ref = make_key_ref(key, 1);
  567. break;
  568. default:
  569. key_ref = ERR_PTR(-EINVAL);
  570. if (id < 1)
  571. goto error;
  572. key = key_lookup(id);
  573. if (IS_ERR(key)) {
  574. key_ref = ERR_CAST(key);
  575. goto error;
  576. }
  577. key_ref = make_key_ref(key, 0);
  578. /* check to see if we possess the key */
  579. ctx.index_key.type = key->type;
  580. ctx.index_key.description = key->description;
  581. ctx.index_key.desc_len = strlen(key->description);
  582. ctx.match_data.raw_data = key;
  583. kdebug("check possessed");
  584. skey_ref = search_process_keyrings(&ctx);
  585. kdebug("possessed=%p", skey_ref);
  586. if (!IS_ERR(skey_ref)) {
  587. key_put(key);
  588. key_ref = skey_ref;
  589. }
  590. break;
  591. }
  592. /* unlink does not use the nominated key in any way, so can skip all
  593. * the permission checks as it is only concerned with the keyring */
  594. if (lflags & KEY_LOOKUP_FOR_UNLINK) {
  595. ret = 0;
  596. goto error;
  597. }
  598. if (!(lflags & KEY_LOOKUP_PARTIAL)) {
  599. ret = wait_for_key_construction(key, true);
  600. switch (ret) {
  601. case -ERESTARTSYS:
  602. goto invalid_key;
  603. default:
  604. if (perm)
  605. goto invalid_key;
  606. case 0:
  607. break;
  608. }
  609. } else if (perm) {
  610. ret = key_validate(key);
  611. if (ret < 0)
  612. goto invalid_key;
  613. }
  614. ret = -EIO;
  615. if (!(lflags & KEY_LOOKUP_PARTIAL) &&
  616. !test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
  617. goto invalid_key;
  618. /* check the permissions */
  619. ret = key_task_permission(key_ref, ctx.cred, perm);
  620. if (ret < 0)
  621. goto invalid_key;
  622. key->last_used_at = current_kernel_time().tv_sec;
  623. error:
  624. put_cred(ctx.cred);
  625. return key_ref;
  626. invalid_key:
  627. key_ref_put(key_ref);
  628. key_ref = ERR_PTR(ret);
  629. goto error;
  630. /* if we attempted to install a keyring, then it may have caused new
  631. * creds to be installed */
  632. reget_creds:
  633. put_cred(ctx.cred);
  634. goto try_again;
  635. }
  636. /*
  637. * Join the named keyring as the session keyring if possible else attempt to
  638. * create a new one of that name and join that.
  639. *
  640. * If the name is NULL, an empty anonymous keyring will be installed as the
  641. * session keyring.
  642. *
  643. * Named session keyrings are joined with a semaphore held to prevent the
  644. * keyrings from going away whilst the attempt is made to going them and also
  645. * to prevent a race in creating compatible session keyrings.
  646. */
  647. long join_session_keyring(const char *name)
  648. {
  649. const struct cred *old;
  650. struct cred *new;
  651. struct key *keyring;
  652. long ret, serial;
  653. new = prepare_creds();
  654. if (!new)
  655. return -ENOMEM;
  656. old = current_cred();
  657. /* if no name is provided, install an anonymous keyring */
  658. if (!name) {
  659. ret = install_session_keyring_to_cred(new, NULL);
  660. if (ret < 0)
  661. goto error;
  662. serial = new->session_keyring->serial;
  663. ret = commit_creds(new);
  664. if (ret == 0)
  665. ret = serial;
  666. goto okay;
  667. }
  668. /* allow the user to join or create a named keyring */
  669. mutex_lock(&key_session_mutex);
  670. /* look for an existing keyring of this name */
  671. keyring = find_keyring_by_name(name, false);
  672. if (PTR_ERR(keyring) == -ENOKEY) {
  673. /* not found - try and create a new one */
  674. keyring = keyring_alloc(
  675. name, old->uid, old->gid, old,
  676. KEY_POS_ALL | KEY_USR_VIEW | KEY_USR_READ | KEY_USR_LINK,
  677. KEY_ALLOC_IN_QUOTA, NULL);
  678. if (IS_ERR(keyring)) {
  679. ret = PTR_ERR(keyring);
  680. goto error2;
  681. }
  682. } else if (IS_ERR(keyring)) {
  683. ret = PTR_ERR(keyring);
  684. goto error2;
  685. } else if (keyring == new->session_keyring) {
  686. ret = 0;
  687. goto error2;
  688. }
  689. /* we've got a keyring - now to install it */
  690. ret = install_session_keyring_to_cred(new, keyring);
  691. if (ret < 0)
  692. goto error2;
  693. commit_creds(new);
  694. mutex_unlock(&key_session_mutex);
  695. ret = keyring->serial;
  696. key_put(keyring);
  697. okay:
  698. return ret;
  699. error2:
  700. mutex_unlock(&key_session_mutex);
  701. error:
  702. abort_creds(new);
  703. return ret;
  704. }
  705. /*
  706. * Replace a process's session keyring on behalf of one of its children when
  707. * the target process is about to resume userspace execution.
  708. */
  709. void key_change_session_keyring(struct callback_head *twork)
  710. {
  711. const struct cred *old = current_cred();
  712. struct cred *new = container_of(twork, struct cred, rcu);
  713. if (unlikely(current->flags & PF_EXITING)) {
  714. put_cred(new);
  715. return;
  716. }
  717. new-> uid = old-> uid;
  718. new-> euid = old-> euid;
  719. new-> suid = old-> suid;
  720. new->fsuid = old->fsuid;
  721. new-> gid = old-> gid;
  722. new-> egid = old-> egid;
  723. new-> sgid = old-> sgid;
  724. new->fsgid = old->fsgid;
  725. new->user = get_uid(old->user);
  726. new->user_ns = get_user_ns(old->user_ns);
  727. new->group_info = get_group_info(old->group_info);
  728. new->securebits = old->securebits;
  729. new->cap_inheritable = old->cap_inheritable;
  730. new->cap_permitted = old->cap_permitted;
  731. new->cap_effective = old->cap_effective;
  732. new->cap_bset = old->cap_bset;
  733. new->jit_keyring = old->jit_keyring;
  734. new->thread_keyring = key_get(old->thread_keyring);
  735. new->process_keyring = key_get(old->process_keyring);
  736. security_transfer_creds(new, old);
  737. commit_creds(new);
  738. }
  739. /*
  740. * Make sure that root's user and user-session keyrings exist.
  741. */
  742. static int __init init_root_keyring(void)
  743. {
  744. return install_user_keyrings();
  745. }
  746. late_initcall(init_root_keyring);