process_keys.c 17 KB

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