process_keys.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. /* Management of 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/slab.h>
  15. #include <linux/keyctl.h>
  16. #include <linux/fs.h>
  17. #include <linux/err.h>
  18. #include <linux/mutex.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 = 0,
  34. .user_ns = &init_user_ns,
  35. };
  36. /*****************************************************************************/
  37. /*
  38. * install user and user session keyrings for a particular 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. char buf[20];
  46. int ret;
  47. cred = current_cred();
  48. user = cred->user;
  49. kenter("%p{%u}", user, user->uid);
  50. if (user->uid_keyring) {
  51. kleave(" = 0 [exist]");
  52. return 0;
  53. }
  54. mutex_lock(&key_user_keyring_mutex);
  55. ret = 0;
  56. if (!user->uid_keyring) {
  57. /* get the UID-specific keyring
  58. * - there may be one in existence already as it may have been
  59. * pinned by a session, but the user_struct pointing to it
  60. * may have been destroyed by setuid */
  61. sprintf(buf, "_uid.%u", user->uid);
  62. uid_keyring = find_keyring_by_name(buf, true);
  63. if (IS_ERR(uid_keyring)) {
  64. uid_keyring = keyring_alloc(buf, user->uid, (gid_t) -1,
  65. cred, KEY_ALLOC_IN_QUOTA,
  66. NULL);
  67. if (IS_ERR(uid_keyring)) {
  68. ret = PTR_ERR(uid_keyring);
  69. goto error;
  70. }
  71. }
  72. /* get a default session keyring (which might also exist
  73. * already) */
  74. sprintf(buf, "_uid_ses.%u", user->uid);
  75. session_keyring = find_keyring_by_name(buf, true);
  76. if (IS_ERR(session_keyring)) {
  77. session_keyring =
  78. keyring_alloc(buf, user->uid, (gid_t) -1,
  79. cred, KEY_ALLOC_IN_QUOTA, NULL);
  80. if (IS_ERR(session_keyring)) {
  81. ret = PTR_ERR(session_keyring);
  82. goto error_release;
  83. }
  84. /* we install a link from the user session keyring to
  85. * the user keyring */
  86. ret = key_link(session_keyring, uid_keyring);
  87. if (ret < 0)
  88. goto error_release_both;
  89. }
  90. /* install the keyrings */
  91. user->uid_keyring = uid_keyring;
  92. user->session_keyring = session_keyring;
  93. }
  94. mutex_unlock(&key_user_keyring_mutex);
  95. kleave(" = 0");
  96. return 0;
  97. error_release_both:
  98. key_put(session_keyring);
  99. error_release:
  100. key_put(uid_keyring);
  101. error:
  102. mutex_unlock(&key_user_keyring_mutex);
  103. kleave(" = %d", ret);
  104. return ret;
  105. }
  106. /*
  107. * install a fresh thread keyring directly to new credentials
  108. */
  109. int install_thread_keyring_to_cred(struct cred *new)
  110. {
  111. struct key *keyring;
  112. keyring = keyring_alloc("_tid", new->uid, new->gid, new,
  113. KEY_ALLOC_QUOTA_OVERRUN, NULL);
  114. if (IS_ERR(keyring))
  115. return PTR_ERR(keyring);
  116. new->thread_keyring = keyring;
  117. return 0;
  118. }
  119. /*
  120. * install a fresh thread keyring, discarding the old one
  121. */
  122. static int install_thread_keyring(void)
  123. {
  124. struct cred *new;
  125. int ret;
  126. new = prepare_creds();
  127. if (!new)
  128. return -ENOMEM;
  129. BUG_ON(new->thread_keyring);
  130. ret = install_thread_keyring_to_cred(new);
  131. if (ret < 0) {
  132. abort_creds(new);
  133. return ret;
  134. }
  135. return commit_creds(new);
  136. }
  137. /*
  138. * install a process keyring directly to a credentials struct
  139. * - returns -EEXIST if there was already a process keyring, 0 if one installed,
  140. * and other -ve on any other error
  141. */
  142. int install_process_keyring_to_cred(struct cred *new)
  143. {
  144. struct key *keyring;
  145. int ret;
  146. if (new->tgcred->process_keyring)
  147. return -EEXIST;
  148. keyring = keyring_alloc("_pid", new->uid, new->gid,
  149. new, KEY_ALLOC_QUOTA_OVERRUN, NULL);
  150. if (IS_ERR(keyring))
  151. return PTR_ERR(keyring);
  152. spin_lock_irq(&new->tgcred->lock);
  153. if (!new->tgcred->process_keyring) {
  154. new->tgcred->process_keyring = keyring;
  155. keyring = NULL;
  156. ret = 0;
  157. } else {
  158. ret = -EEXIST;
  159. }
  160. spin_unlock_irq(&new->tgcred->lock);
  161. key_put(keyring);
  162. return ret;
  163. }
  164. /*
  165. * make sure a process keyring is installed
  166. * - we
  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 ?: 0;
  179. }
  180. return commit_creds(new);
  181. }
  182. /*
  183. * install a session keyring directly to a credentials struct
  184. */
  185. static int install_session_keyring_to_cred(struct cred *cred,
  186. struct key *keyring)
  187. {
  188. unsigned long flags;
  189. struct key *old;
  190. might_sleep();
  191. /* create an empty session keyring */
  192. if (!keyring) {
  193. flags = KEY_ALLOC_QUOTA_OVERRUN;
  194. if (cred->tgcred->session_keyring)
  195. flags = KEY_ALLOC_IN_QUOTA;
  196. keyring = keyring_alloc("_ses", cred->uid, cred->gid,
  197. cred, flags, NULL);
  198. if (IS_ERR(keyring))
  199. return PTR_ERR(keyring);
  200. } else {
  201. atomic_inc(&keyring->usage);
  202. }
  203. /* install the keyring */
  204. spin_lock_irq(&cred->tgcred->lock);
  205. old = cred->tgcred->session_keyring;
  206. rcu_assign_pointer(cred->tgcred->session_keyring, keyring);
  207. spin_unlock_irq(&cred->tgcred->lock);
  208. /* we're using RCU on the pointer, but there's no point synchronising
  209. * on it if it didn't previously point to anything */
  210. if (old) {
  211. synchronize_rcu();
  212. key_put(old);
  213. }
  214. return 0;
  215. }
  216. /*
  217. * install a session keyring, discarding the old one
  218. * - if a keyring is not supplied, an empty one is invented
  219. */
  220. static int install_session_keyring(struct key *keyring)
  221. {
  222. struct cred *new;
  223. int ret;
  224. new = prepare_creds();
  225. if (!new)
  226. return -ENOMEM;
  227. ret = install_session_keyring_to_cred(new, NULL);
  228. if (ret < 0) {
  229. abort_creds(new);
  230. return ret;
  231. }
  232. return commit_creds(new);
  233. }
  234. /*****************************************************************************/
  235. /*
  236. * the filesystem user ID changed
  237. */
  238. void key_fsuid_changed(struct task_struct *tsk)
  239. {
  240. /* update the ownership of the thread keyring */
  241. BUG_ON(!tsk->cred);
  242. if (tsk->cred->thread_keyring) {
  243. down_write(&tsk->cred->thread_keyring->sem);
  244. tsk->cred->thread_keyring->uid = tsk->cred->fsuid;
  245. up_write(&tsk->cred->thread_keyring->sem);
  246. }
  247. } /* end key_fsuid_changed() */
  248. /*****************************************************************************/
  249. /*
  250. * the filesystem group ID changed
  251. */
  252. void key_fsgid_changed(struct task_struct *tsk)
  253. {
  254. /* update the ownership of the thread keyring */
  255. BUG_ON(!tsk->cred);
  256. if (tsk->cred->thread_keyring) {
  257. down_write(&tsk->cred->thread_keyring->sem);
  258. tsk->cred->thread_keyring->gid = tsk->cred->fsgid;
  259. up_write(&tsk->cred->thread_keyring->sem);
  260. }
  261. } /* end key_fsgid_changed() */
  262. /*****************************************************************************/
  263. /*
  264. * search the process keyrings for the first matching key
  265. * - we use the supplied match function to see if the description (or other
  266. * feature of interest) matches
  267. * - we return -EAGAIN if we didn't find any matching key
  268. * - we return -ENOKEY if we found only negative matching keys
  269. */
  270. key_ref_t search_process_keyrings(struct key_type *type,
  271. const void *description,
  272. key_match_func_t match,
  273. const struct cred *cred)
  274. {
  275. struct request_key_auth *rka;
  276. key_ref_t key_ref, ret, err;
  277. might_sleep();
  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 (cred->thread_keyring) {
  290. key_ref = keyring_search_aux(
  291. make_key_ref(cred->thread_keyring, 1),
  292. cred, type, description, match);
  293. if (!IS_ERR(key_ref))
  294. goto found;
  295. switch (PTR_ERR(key_ref)) {
  296. case -EAGAIN: /* no key */
  297. if (ret)
  298. break;
  299. case -ENOKEY: /* negative key */
  300. ret = key_ref;
  301. break;
  302. default:
  303. err = key_ref;
  304. break;
  305. }
  306. }
  307. /* search the process keyring second */
  308. if (cred->tgcred->process_keyring) {
  309. key_ref = keyring_search_aux(
  310. make_key_ref(cred->tgcred->process_keyring, 1),
  311. cred, type, description, match);
  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 (cred->tgcred->session_keyring) {
  328. rcu_read_lock();
  329. key_ref = keyring_search_aux(
  330. make_key_ref(rcu_dereference(
  331. cred->tgcred->session_keyring),
  332. 1),
  333. cred, type, description, match);
  334. rcu_read_unlock();
  335. if (!IS_ERR(key_ref))
  336. goto found;
  337. switch (PTR_ERR(key_ref)) {
  338. case -EAGAIN: /* no key */
  339. if (ret)
  340. break;
  341. case -ENOKEY: /* negative key */
  342. ret = key_ref;
  343. break;
  344. default:
  345. err = key_ref;
  346. break;
  347. }
  348. }
  349. /* or search the user-session keyring */
  350. else if (cred->user->session_keyring) {
  351. key_ref = keyring_search_aux(
  352. make_key_ref(cred->user->session_keyring, 1),
  353. cred, type, description, match);
  354. if (!IS_ERR(key_ref))
  355. goto found;
  356. switch (PTR_ERR(key_ref)) {
  357. case -EAGAIN: /* no key */
  358. if (ret)
  359. break;
  360. case -ENOKEY: /* negative key */
  361. ret = key_ref;
  362. break;
  363. default:
  364. err = key_ref;
  365. break;
  366. }
  367. }
  368. /* if this process has an instantiation authorisation key, then we also
  369. * search the keyrings of the process mentioned there
  370. * - we don't permit access to request_key auth keys via this method
  371. */
  372. if (cred->request_key_auth &&
  373. cred == current_cred() &&
  374. type != &key_type_request_key_auth
  375. ) {
  376. /* defend against the auth key being revoked */
  377. down_read(&cred->request_key_auth->sem);
  378. if (key_validate(cred->request_key_auth) == 0) {
  379. rka = cred->request_key_auth->payload.data;
  380. key_ref = search_process_keyrings(type, description,
  381. match, rka->cred);
  382. up_read(&cred->request_key_auth->sem);
  383. if (!IS_ERR(key_ref))
  384. goto found;
  385. switch (PTR_ERR(key_ref)) {
  386. case -EAGAIN: /* no key */
  387. if (ret)
  388. break;
  389. case -ENOKEY: /* negative key */
  390. ret = key_ref;
  391. break;
  392. default:
  393. err = key_ref;
  394. break;
  395. }
  396. } else {
  397. up_read(&cred->request_key_auth->sem);
  398. }
  399. }
  400. /* no key - decide on the error we're going to go for */
  401. key_ref = ret ? ret : err;
  402. found:
  403. return key_ref;
  404. } /* end search_process_keyrings() */
  405. /*****************************************************************************/
  406. /*
  407. * see if the key we're looking at is the target key
  408. */
  409. static int lookup_user_key_possessed(const struct key *key, const void *target)
  410. {
  411. return key == target;
  412. } /* end lookup_user_key_possessed() */
  413. /*****************************************************************************/
  414. /*
  415. * lookup a key given a key ID from userspace with a given permissions mask
  416. * - don't create special keyrings unless so requested
  417. * - partially constructed keys aren't found unless requested
  418. */
  419. key_ref_t lookup_user_key(key_serial_t id, int create, int partial,
  420. key_perm_t perm)
  421. {
  422. struct request_key_auth *rka;
  423. const struct cred *cred;
  424. struct key *key;
  425. key_ref_t key_ref, skey_ref;
  426. int ret;
  427. try_again:
  428. cred = get_current_cred();
  429. key_ref = ERR_PTR(-ENOKEY);
  430. switch (id) {
  431. case KEY_SPEC_THREAD_KEYRING:
  432. if (!cred->thread_keyring) {
  433. if (!create)
  434. goto error;
  435. ret = install_thread_keyring();
  436. if (ret < 0) {
  437. key = ERR_PTR(ret);
  438. goto error;
  439. }
  440. goto reget_creds;
  441. }
  442. key = cred->thread_keyring;
  443. atomic_inc(&key->usage);
  444. key_ref = make_key_ref(key, 1);
  445. break;
  446. case KEY_SPEC_PROCESS_KEYRING:
  447. if (!cred->tgcred->process_keyring) {
  448. if (!create)
  449. goto error;
  450. ret = install_process_keyring();
  451. if (ret < 0) {
  452. key = ERR_PTR(ret);
  453. goto error;
  454. }
  455. goto reget_creds;
  456. }
  457. key = cred->tgcred->process_keyring;
  458. atomic_inc(&key->usage);
  459. key_ref = make_key_ref(key, 1);
  460. break;
  461. case KEY_SPEC_SESSION_KEYRING:
  462. if (!cred->tgcred->session_keyring) {
  463. /* always install a session keyring upon access if one
  464. * doesn't exist yet */
  465. ret = install_user_keyrings();
  466. if (ret < 0)
  467. goto error;
  468. ret = install_session_keyring(
  469. cred->user->session_keyring);
  470. if (ret < 0)
  471. goto error;
  472. goto reget_creds;
  473. }
  474. rcu_read_lock();
  475. key = rcu_dereference(cred->tgcred->session_keyring);
  476. atomic_inc(&key->usage);
  477. rcu_read_unlock();
  478. key_ref = make_key_ref(key, 1);
  479. break;
  480. case KEY_SPEC_USER_KEYRING:
  481. if (!cred->user->uid_keyring) {
  482. ret = install_user_keyrings();
  483. if (ret < 0)
  484. goto error;
  485. }
  486. key = cred->user->uid_keyring;
  487. atomic_inc(&key->usage);
  488. key_ref = make_key_ref(key, 1);
  489. break;
  490. case KEY_SPEC_USER_SESSION_KEYRING:
  491. if (!cred->user->session_keyring) {
  492. ret = install_user_keyrings();
  493. if (ret < 0)
  494. goto error;
  495. }
  496. key = cred->user->session_keyring;
  497. atomic_inc(&key->usage);
  498. key_ref = make_key_ref(key, 1);
  499. break;
  500. case KEY_SPEC_GROUP_KEYRING:
  501. /* group keyrings are not yet supported */
  502. key = ERR_PTR(-EINVAL);
  503. goto error;
  504. case KEY_SPEC_REQKEY_AUTH_KEY:
  505. key = cred->request_key_auth;
  506. if (!key)
  507. goto error;
  508. atomic_inc(&key->usage);
  509. key_ref = make_key_ref(key, 1);
  510. break;
  511. case KEY_SPEC_REQUESTOR_KEYRING:
  512. if (!cred->request_key_auth)
  513. goto error;
  514. down_read(&cred->request_key_auth->sem);
  515. if (cred->request_key_auth->flags & KEY_FLAG_REVOKED) {
  516. key_ref = ERR_PTR(-EKEYREVOKED);
  517. key = NULL;
  518. } else {
  519. rka = cred->request_key_auth->payload.data;
  520. key = rka->dest_keyring;
  521. atomic_inc(&key->usage);
  522. }
  523. up_read(&cred->request_key_auth->sem);
  524. if (!key)
  525. goto error;
  526. key_ref = make_key_ref(key, 1);
  527. break;
  528. default:
  529. key_ref = ERR_PTR(-EINVAL);
  530. if (id < 1)
  531. goto error;
  532. key = key_lookup(id);
  533. if (IS_ERR(key)) {
  534. key_ref = ERR_CAST(key);
  535. goto error;
  536. }
  537. key_ref = make_key_ref(key, 0);
  538. /* check to see if we possess the key */
  539. skey_ref = search_process_keyrings(key->type, key,
  540. lookup_user_key_possessed,
  541. cred);
  542. if (!IS_ERR(skey_ref)) {
  543. key_put(key);
  544. key_ref = skey_ref;
  545. }
  546. break;
  547. }
  548. if (!partial) {
  549. ret = wait_for_key_construction(key, true);
  550. switch (ret) {
  551. case -ERESTARTSYS:
  552. goto invalid_key;
  553. default:
  554. if (perm)
  555. goto invalid_key;
  556. case 0:
  557. break;
  558. }
  559. } else if (perm) {
  560. ret = key_validate(key);
  561. if (ret < 0)
  562. goto invalid_key;
  563. }
  564. ret = -EIO;
  565. if (!partial && !test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
  566. goto invalid_key;
  567. /* check the permissions */
  568. ret = key_task_permission(key_ref, cred, perm);
  569. if (ret < 0)
  570. goto invalid_key;
  571. error:
  572. put_cred(cred);
  573. return key_ref;
  574. invalid_key:
  575. key_ref_put(key_ref);
  576. key_ref = ERR_PTR(ret);
  577. goto error;
  578. /* if we attempted to install a keyring, then it may have caused new
  579. * creds to be installed */
  580. reget_creds:
  581. put_cred(cred);
  582. goto try_again;
  583. } /* end lookup_user_key() */
  584. /*****************************************************************************/
  585. /*
  586. * join the named keyring as the session keyring if possible, or attempt to
  587. * create a new one of that name if not
  588. * - if the name is NULL, an empty anonymous keyring is installed instead
  589. * - named session keyring joining is done with a semaphore held
  590. */
  591. long join_session_keyring(const char *name)
  592. {
  593. const struct cred *old;
  594. struct cred *new;
  595. struct key *keyring;
  596. long ret, serial;
  597. /* only permit this if there's a single thread in the thread group -
  598. * this avoids us having to adjust the creds on all threads and risking
  599. * ENOMEM */
  600. if (!is_single_threaded(current))
  601. return -EMLINK;
  602. new = prepare_creds();
  603. if (!new)
  604. return -ENOMEM;
  605. old = current_cred();
  606. /* if no name is provided, install an anonymous keyring */
  607. if (!name) {
  608. ret = install_session_keyring_to_cred(new, NULL);
  609. if (ret < 0)
  610. goto error;
  611. serial = new->tgcred->session_keyring->serial;
  612. ret = commit_creds(new);
  613. if (ret == 0)
  614. ret = serial;
  615. goto okay;
  616. }
  617. /* allow the user to join or create a named keyring */
  618. mutex_lock(&key_session_mutex);
  619. /* look for an existing keyring of this name */
  620. keyring = find_keyring_by_name(name, false);
  621. if (PTR_ERR(keyring) == -ENOKEY) {
  622. /* not found - try and create a new one */
  623. keyring = keyring_alloc(name, old->uid, old->gid, old,
  624. KEY_ALLOC_IN_QUOTA, NULL);
  625. if (IS_ERR(keyring)) {
  626. ret = PTR_ERR(keyring);
  627. goto error2;
  628. }
  629. } else if (IS_ERR(keyring)) {
  630. ret = PTR_ERR(keyring);
  631. goto error2;
  632. }
  633. /* we've got a keyring - now to install it */
  634. ret = install_session_keyring_to_cred(new, keyring);
  635. if (ret < 0)
  636. goto error2;
  637. commit_creds(new);
  638. mutex_unlock(&key_session_mutex);
  639. ret = keyring->serial;
  640. key_put(keyring);
  641. okay:
  642. return ret;
  643. error2:
  644. mutex_unlock(&key_session_mutex);
  645. error:
  646. abort_creds(new);
  647. return ret;
  648. }