process_keys.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. /* process_keys.c: management of a process's keyrings
  2. *
  3. * Copyright (C) 2004-5 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 <asm/uaccess.h>
  20. #include "internal.h"
  21. /* session keyring create vs join semaphore */
  22. static DEFINE_MUTEX(key_session_mutex);
  23. /* the root user's tracking struct */
  24. struct key_user root_key_user = {
  25. .usage = ATOMIC_INIT(3),
  26. .consq = LIST_HEAD_INIT(root_key_user.consq),
  27. .lock = SPIN_LOCK_UNLOCKED,
  28. .nkeys = ATOMIC_INIT(2),
  29. .nikeys = ATOMIC_INIT(2),
  30. .uid = 0,
  31. };
  32. /* the root user's UID keyring */
  33. struct key root_user_keyring = {
  34. .usage = ATOMIC_INIT(1),
  35. .serial = 2,
  36. .type = &key_type_keyring,
  37. .user = &root_key_user,
  38. .sem = __RWSEM_INITIALIZER(root_user_keyring.sem),
  39. .perm = (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
  40. .flags = 1 << KEY_FLAG_INSTANTIATED,
  41. .description = "_uid.0",
  42. #ifdef KEY_DEBUGGING
  43. .magic = KEY_DEBUG_MAGIC,
  44. #endif
  45. };
  46. /* the root user's default session keyring */
  47. struct key root_session_keyring = {
  48. .usage = ATOMIC_INIT(1),
  49. .serial = 1,
  50. .type = &key_type_keyring,
  51. .user = &root_key_user,
  52. .sem = __RWSEM_INITIALIZER(root_session_keyring.sem),
  53. .perm = (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
  54. .flags = 1 << KEY_FLAG_INSTANTIATED,
  55. .description = "_uid_ses.0",
  56. #ifdef KEY_DEBUGGING
  57. .magic = KEY_DEBUG_MAGIC,
  58. #endif
  59. };
  60. /*****************************************************************************/
  61. /*
  62. * allocate the keyrings to be associated with a UID
  63. */
  64. int alloc_uid_keyring(struct user_struct *user)
  65. {
  66. struct key *uid_keyring, *session_keyring;
  67. char buf[20];
  68. int ret;
  69. /* concoct a default session keyring */
  70. sprintf(buf, "_uid_ses.%u", user->uid);
  71. session_keyring = keyring_alloc(buf, user->uid, (gid_t) -1, 0, NULL);
  72. if (IS_ERR(session_keyring)) {
  73. ret = PTR_ERR(session_keyring);
  74. goto error;
  75. }
  76. /* and a UID specific keyring, pointed to by the default session
  77. * keyring */
  78. sprintf(buf, "_uid.%u", user->uid);
  79. uid_keyring = keyring_alloc(buf, user->uid, (gid_t) -1, 0,
  80. session_keyring);
  81. if (IS_ERR(uid_keyring)) {
  82. key_put(session_keyring);
  83. ret = PTR_ERR(uid_keyring);
  84. goto error;
  85. }
  86. /* install the keyrings */
  87. user->uid_keyring = uid_keyring;
  88. user->session_keyring = session_keyring;
  89. ret = 0;
  90. error:
  91. return ret;
  92. } /* end alloc_uid_keyring() */
  93. /*****************************************************************************/
  94. /*
  95. * deal with the UID changing
  96. */
  97. void switch_uid_keyring(struct user_struct *new_user)
  98. {
  99. #if 0 /* do nothing for now */
  100. struct key *old;
  101. /* switch to the new user's session keyring if we were running under
  102. * root's default session keyring */
  103. if (new_user->uid != 0 &&
  104. current->session_keyring == &root_session_keyring
  105. ) {
  106. atomic_inc(&new_user->session_keyring->usage);
  107. task_lock(current);
  108. old = current->session_keyring;
  109. current->session_keyring = new_user->session_keyring;
  110. task_unlock(current);
  111. key_put(old);
  112. }
  113. #endif
  114. } /* end switch_uid_keyring() */
  115. /*****************************************************************************/
  116. /*
  117. * install a fresh thread keyring, discarding the old one
  118. */
  119. int install_thread_keyring(struct task_struct *tsk)
  120. {
  121. struct key *keyring, *old;
  122. char buf[20];
  123. int ret;
  124. sprintf(buf, "_tid.%u", tsk->pid);
  125. keyring = keyring_alloc(buf, tsk->uid, tsk->gid, 1, NULL);
  126. if (IS_ERR(keyring)) {
  127. ret = PTR_ERR(keyring);
  128. goto error;
  129. }
  130. task_lock(tsk);
  131. old = tsk->thread_keyring;
  132. tsk->thread_keyring = keyring;
  133. task_unlock(tsk);
  134. ret = 0;
  135. key_put(old);
  136. error:
  137. return ret;
  138. } /* end install_thread_keyring() */
  139. /*****************************************************************************/
  140. /*
  141. * make sure a process keyring is installed
  142. */
  143. int install_process_keyring(struct task_struct *tsk)
  144. {
  145. struct key *keyring;
  146. char buf[20];
  147. int ret;
  148. might_sleep();
  149. if (!tsk->signal->process_keyring) {
  150. sprintf(buf, "_pid.%u", tsk->tgid);
  151. keyring = keyring_alloc(buf, tsk->uid, tsk->gid, 1, NULL);
  152. if (IS_ERR(keyring)) {
  153. ret = PTR_ERR(keyring);
  154. goto error;
  155. }
  156. /* attach keyring */
  157. spin_lock_irq(&tsk->sighand->siglock);
  158. if (!tsk->signal->process_keyring) {
  159. tsk->signal->process_keyring = keyring;
  160. keyring = NULL;
  161. }
  162. spin_unlock_irq(&tsk->sighand->siglock);
  163. key_put(keyring);
  164. }
  165. ret = 0;
  166. error:
  167. return ret;
  168. } /* end install_process_keyring() */
  169. /*****************************************************************************/
  170. /*
  171. * install a session keyring, discarding the old one
  172. * - if a keyring is not supplied, an empty one is invented
  173. */
  174. static int install_session_keyring(struct task_struct *tsk,
  175. struct key *keyring)
  176. {
  177. struct key *old;
  178. char buf[20];
  179. might_sleep();
  180. /* create an empty session keyring */
  181. if (!keyring) {
  182. sprintf(buf, "_ses.%u", tsk->tgid);
  183. keyring = keyring_alloc(buf, tsk->uid, tsk->gid, 1, NULL);
  184. if (IS_ERR(keyring))
  185. return PTR_ERR(keyring);
  186. }
  187. else {
  188. atomic_inc(&keyring->usage);
  189. }
  190. /* install the keyring */
  191. spin_lock_irq(&tsk->sighand->siglock);
  192. old = tsk->signal->session_keyring;
  193. rcu_assign_pointer(tsk->signal->session_keyring, keyring);
  194. spin_unlock_irq(&tsk->sighand->siglock);
  195. /* we're using RCU on the pointer, but there's no point synchronising
  196. * on it if it didn't previously point to anything */
  197. if (old) {
  198. synchronize_rcu();
  199. key_put(old);
  200. }
  201. return 0;
  202. } /* end install_session_keyring() */
  203. /*****************************************************************************/
  204. /*
  205. * copy the keys in a thread group for fork without CLONE_THREAD
  206. */
  207. int copy_thread_group_keys(struct task_struct *tsk)
  208. {
  209. key_check(current->thread_group->session_keyring);
  210. key_check(current->thread_group->process_keyring);
  211. /* no process keyring yet */
  212. tsk->signal->process_keyring = NULL;
  213. /* same session keyring */
  214. rcu_read_lock();
  215. tsk->signal->session_keyring =
  216. key_get(rcu_dereference(current->signal->session_keyring));
  217. rcu_read_unlock();
  218. return 0;
  219. } /* end copy_thread_group_keys() */
  220. /*****************************************************************************/
  221. /*
  222. * copy the keys for fork
  223. */
  224. int copy_keys(unsigned long clone_flags, struct task_struct *tsk)
  225. {
  226. key_check(tsk->thread_keyring);
  227. key_check(tsk->request_key_auth);
  228. /* no thread keyring yet */
  229. tsk->thread_keyring = NULL;
  230. /* copy the request_key() authorisation for this thread */
  231. key_get(tsk->request_key_auth);
  232. return 0;
  233. } /* end copy_keys() */
  234. /*****************************************************************************/
  235. /*
  236. * dispose of thread group keys upon thread group destruction
  237. */
  238. void exit_thread_group_keys(struct signal_struct *tg)
  239. {
  240. key_put(tg->session_keyring);
  241. key_put(tg->process_keyring);
  242. } /* end exit_thread_group_keys() */
  243. /*****************************************************************************/
  244. /*
  245. * dispose of per-thread keys upon thread exit
  246. */
  247. void exit_keys(struct task_struct *tsk)
  248. {
  249. key_put(tsk->thread_keyring);
  250. key_put(tsk->request_key_auth);
  251. } /* end exit_keys() */
  252. /*****************************************************************************/
  253. /*
  254. * deal with execve()
  255. */
  256. int exec_keys(struct task_struct *tsk)
  257. {
  258. struct key *old;
  259. /* newly exec'd tasks don't get a thread keyring */
  260. task_lock(tsk);
  261. old = tsk->thread_keyring;
  262. tsk->thread_keyring = NULL;
  263. task_unlock(tsk);
  264. key_put(old);
  265. /* discard the process keyring from a newly exec'd task */
  266. spin_lock_irq(&tsk->sighand->siglock);
  267. old = tsk->signal->process_keyring;
  268. tsk->signal->process_keyring = NULL;
  269. spin_unlock_irq(&tsk->sighand->siglock);
  270. key_put(old);
  271. return 0;
  272. } /* end exec_keys() */
  273. /*****************************************************************************/
  274. /*
  275. * deal with SUID programs
  276. * - we might want to make this invent a new session keyring
  277. */
  278. int suid_keys(struct task_struct *tsk)
  279. {
  280. return 0;
  281. } /* end suid_keys() */
  282. /*****************************************************************************/
  283. /*
  284. * the filesystem user ID changed
  285. */
  286. void key_fsuid_changed(struct task_struct *tsk)
  287. {
  288. /* update the ownership of the thread keyring */
  289. if (tsk->thread_keyring) {
  290. down_write(&tsk->thread_keyring->sem);
  291. tsk->thread_keyring->uid = tsk->fsuid;
  292. up_write(&tsk->thread_keyring->sem);
  293. }
  294. } /* end key_fsuid_changed() */
  295. /*****************************************************************************/
  296. /*
  297. * the filesystem group ID changed
  298. */
  299. void key_fsgid_changed(struct task_struct *tsk)
  300. {
  301. /* update the ownership of the thread keyring */
  302. if (tsk->thread_keyring) {
  303. down_write(&tsk->thread_keyring->sem);
  304. tsk->thread_keyring->gid = tsk->fsgid;
  305. up_write(&tsk->thread_keyring->sem);
  306. }
  307. } /* end key_fsgid_changed() */
  308. /*****************************************************************************/
  309. /*
  310. * search the process keyrings for the first matching key
  311. * - we use the supplied match function to see if the description (or other
  312. * feature of interest) matches
  313. * - we return -EAGAIN if we didn't find any matching key
  314. * - we return -ENOKEY if we found only negative matching keys
  315. */
  316. key_ref_t search_process_keyrings(struct key_type *type,
  317. const void *description,
  318. key_match_func_t match,
  319. struct task_struct *context)
  320. {
  321. struct request_key_auth *rka;
  322. key_ref_t key_ref, ret, err;
  323. /* we want to return -EAGAIN or -ENOKEY if any of the keyrings were
  324. * searchable, but we failed to find a key or we found a negative key;
  325. * otherwise we want to return a sample error (probably -EACCES) if
  326. * none of the keyrings were searchable
  327. *
  328. * in terms of priority: success > -ENOKEY > -EAGAIN > other error
  329. */
  330. key_ref = NULL;
  331. ret = NULL;
  332. err = ERR_PTR(-EAGAIN);
  333. /* search the thread keyring first */
  334. if (context->thread_keyring) {
  335. key_ref = keyring_search_aux(
  336. make_key_ref(context->thread_keyring, 1),
  337. context, type, description, match);
  338. if (!IS_ERR(key_ref))
  339. goto found;
  340. switch (PTR_ERR(key_ref)) {
  341. case -EAGAIN: /* no key */
  342. if (ret)
  343. break;
  344. case -ENOKEY: /* negative key */
  345. ret = key_ref;
  346. break;
  347. default:
  348. err = key_ref;
  349. break;
  350. }
  351. }
  352. /* search the process keyring second */
  353. if (context->signal->process_keyring) {
  354. key_ref = keyring_search_aux(
  355. make_key_ref(context->signal->process_keyring, 1),
  356. context, type, description, match);
  357. if (!IS_ERR(key_ref))
  358. goto found;
  359. switch (PTR_ERR(key_ref)) {
  360. case -EAGAIN: /* no key */
  361. if (ret)
  362. break;
  363. case -ENOKEY: /* negative key */
  364. ret = key_ref;
  365. break;
  366. default:
  367. err = key_ref;
  368. break;
  369. }
  370. }
  371. /* search the session keyring */
  372. if (context->signal->session_keyring) {
  373. rcu_read_lock();
  374. key_ref = keyring_search_aux(
  375. make_key_ref(rcu_dereference(
  376. context->signal->session_keyring),
  377. 1),
  378. context, type, description, match);
  379. rcu_read_unlock();
  380. if (!IS_ERR(key_ref))
  381. goto found;
  382. switch (PTR_ERR(key_ref)) {
  383. case -EAGAIN: /* no key */
  384. if (ret)
  385. break;
  386. case -ENOKEY: /* negative key */
  387. ret = key_ref;
  388. break;
  389. default:
  390. err = key_ref;
  391. break;
  392. }
  393. }
  394. /* or search the user-session keyring */
  395. else {
  396. key_ref = keyring_search_aux(
  397. make_key_ref(context->user->session_keyring, 1),
  398. context, type, description, match);
  399. if (!IS_ERR(key_ref))
  400. goto found;
  401. switch (PTR_ERR(key_ref)) {
  402. case -EAGAIN: /* no key */
  403. if (ret)
  404. break;
  405. case -ENOKEY: /* negative key */
  406. ret = key_ref;
  407. break;
  408. default:
  409. err = key_ref;
  410. break;
  411. }
  412. }
  413. /* if this process has an instantiation authorisation key, then we also
  414. * search the keyrings of the process mentioned there
  415. * - we don't permit access to request_key auth keys via this method
  416. */
  417. if (context->request_key_auth &&
  418. context == current &&
  419. type != &key_type_request_key_auth &&
  420. key_validate(context->request_key_auth) == 0
  421. ) {
  422. rka = context->request_key_auth->payload.data;
  423. key_ref = search_process_keyrings(type, description, match,
  424. rka->context);
  425. if (!IS_ERR(key_ref))
  426. goto found;
  427. switch (PTR_ERR(key_ref)) {
  428. case -EAGAIN: /* no key */
  429. if (ret)
  430. break;
  431. case -ENOKEY: /* negative key */
  432. ret = key_ref;
  433. break;
  434. default:
  435. err = key_ref;
  436. break;
  437. }
  438. }
  439. /* no key - decide on the error we're going to go for */
  440. key_ref = ret ? ret : err;
  441. found:
  442. return key_ref;
  443. } /* end search_process_keyrings() */
  444. /*****************************************************************************/
  445. /*
  446. * see if the key we're looking at is the target key
  447. */
  448. static int lookup_user_key_possessed(const struct key *key, const void *target)
  449. {
  450. return key == target;
  451. } /* end lookup_user_key_possessed() */
  452. /*****************************************************************************/
  453. /*
  454. * lookup a key given a key ID from userspace with a given permissions mask
  455. * - don't create special keyrings unless so requested
  456. * - partially constructed keys aren't found unless requested
  457. */
  458. key_ref_t lookup_user_key(struct task_struct *context, key_serial_t id,
  459. int create, int partial, key_perm_t perm)
  460. {
  461. key_ref_t key_ref, skey_ref;
  462. struct key *key;
  463. int ret;
  464. if (!context)
  465. context = current;
  466. key_ref = ERR_PTR(-ENOKEY);
  467. switch (id) {
  468. case KEY_SPEC_THREAD_KEYRING:
  469. if (!context->thread_keyring) {
  470. if (!create)
  471. goto error;
  472. ret = install_thread_keyring(context);
  473. if (ret < 0) {
  474. key = ERR_PTR(ret);
  475. goto error;
  476. }
  477. }
  478. key = context->thread_keyring;
  479. atomic_inc(&key->usage);
  480. key_ref = make_key_ref(key, 1);
  481. break;
  482. case KEY_SPEC_PROCESS_KEYRING:
  483. if (!context->signal->process_keyring) {
  484. if (!create)
  485. goto error;
  486. ret = install_process_keyring(context);
  487. if (ret < 0) {
  488. key = ERR_PTR(ret);
  489. goto error;
  490. }
  491. }
  492. key = context->signal->process_keyring;
  493. atomic_inc(&key->usage);
  494. key_ref = make_key_ref(key, 1);
  495. break;
  496. case KEY_SPEC_SESSION_KEYRING:
  497. if (!context->signal->session_keyring) {
  498. /* always install a session keyring upon access if one
  499. * doesn't exist yet */
  500. ret = install_session_keyring(
  501. context, context->user->session_keyring);
  502. if (ret < 0)
  503. goto error;
  504. }
  505. rcu_read_lock();
  506. key = rcu_dereference(context->signal->session_keyring);
  507. atomic_inc(&key->usage);
  508. rcu_read_unlock();
  509. key_ref = make_key_ref(key, 1);
  510. break;
  511. case KEY_SPEC_USER_KEYRING:
  512. key = context->user->uid_keyring;
  513. atomic_inc(&key->usage);
  514. key_ref = make_key_ref(key, 1);
  515. break;
  516. case KEY_SPEC_USER_SESSION_KEYRING:
  517. key = context->user->session_keyring;
  518. atomic_inc(&key->usage);
  519. key_ref = make_key_ref(key, 1);
  520. break;
  521. case KEY_SPEC_GROUP_KEYRING:
  522. /* group keyrings are not yet supported */
  523. key = ERR_PTR(-EINVAL);
  524. goto error;
  525. case KEY_SPEC_REQKEY_AUTH_KEY:
  526. key = context->request_key_auth;
  527. if (!key)
  528. goto error;
  529. atomic_inc(&key->usage);
  530. key_ref = make_key_ref(key, 1);
  531. break;
  532. default:
  533. key_ref = ERR_PTR(-EINVAL);
  534. if (id < 1)
  535. goto error;
  536. key = key_lookup(id);
  537. if (IS_ERR(key)) {
  538. key_ref = ERR_PTR(PTR_ERR(key));
  539. goto error;
  540. }
  541. key_ref = make_key_ref(key, 0);
  542. /* check to see if we possess the key */
  543. skey_ref = search_process_keyrings(key->type, key,
  544. lookup_user_key_possessed,
  545. current);
  546. if (!IS_ERR(skey_ref)) {
  547. key_put(key);
  548. key_ref = skey_ref;
  549. }
  550. break;
  551. }
  552. /* check the status */
  553. if (perm) {
  554. ret = key_validate(key);
  555. if (ret < 0)
  556. goto invalid_key;
  557. }
  558. ret = -EIO;
  559. if (!partial && !test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
  560. goto invalid_key;
  561. /* check the permissions */
  562. ret = key_task_permission(key_ref, context, perm);
  563. if (ret < 0)
  564. goto invalid_key;
  565. error:
  566. return key_ref;
  567. invalid_key:
  568. key_ref_put(key_ref);
  569. key_ref = ERR_PTR(ret);
  570. goto error;
  571. } /* end lookup_user_key() */
  572. /*****************************************************************************/
  573. /*
  574. * join the named keyring as the session keyring if possible, or attempt to
  575. * create a new one of that name if not
  576. * - if the name is NULL, an empty anonymous keyring is installed instead
  577. * - named session keyring joining is done with a semaphore held
  578. */
  579. long join_session_keyring(const char *name)
  580. {
  581. struct task_struct *tsk = current;
  582. struct key *keyring;
  583. long ret;
  584. /* if no name is provided, install an anonymous keyring */
  585. if (!name) {
  586. ret = install_session_keyring(tsk, NULL);
  587. if (ret < 0)
  588. goto error;
  589. rcu_read_lock();
  590. ret = rcu_dereference(tsk->signal->session_keyring)->serial;
  591. rcu_read_unlock();
  592. goto error;
  593. }
  594. /* allow the user to join or create a named keyring */
  595. mutex_lock(&key_session_mutex);
  596. /* look for an existing keyring of this name */
  597. keyring = find_keyring_by_name(name, 0);
  598. if (PTR_ERR(keyring) == -ENOKEY) {
  599. /* not found - try and create a new one */
  600. keyring = keyring_alloc(name, tsk->uid, tsk->gid, 0, NULL);
  601. if (IS_ERR(keyring)) {
  602. ret = PTR_ERR(keyring);
  603. goto error2;
  604. }
  605. }
  606. else if (IS_ERR(keyring)) {
  607. ret = PTR_ERR(keyring);
  608. goto error2;
  609. }
  610. /* we've got a keyring - now to install it */
  611. ret = install_session_keyring(tsk, keyring);
  612. if (ret < 0)
  613. goto error2;
  614. ret = keyring->serial;
  615. key_put(keyring);
  616. error2:
  617. mutex_unlock(&key_session_mutex);
  618. error:
  619. return ret;
  620. } /* end join_session_keyring() */