process_keys.c 17 KB

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