process_keys.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. /* process_keys.c: management of a process's keyrings
  2. *
  3. * Copyright (C) 2004 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. .lock = RW_LOCK_UNLOCKED,
  38. .sem = __RWSEM_INITIALIZER(root_user_keyring.sem),
  39. .perm = KEY_USR_ALL,
  40. .flags = 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. .lock = RW_LOCK_UNLOCKED,
  53. .sem = __RWSEM_INITIALIZER(root_session_keyring.sem),
  54. .perm = KEY_USR_ALL,
  55. .flags = KEY_FLAG_INSTANTIATED,
  56. .description = "_uid_ses.0",
  57. #ifdef KEY_DEBUGGING
  58. .magic = KEY_DEBUG_MAGIC,
  59. #endif
  60. };
  61. /*****************************************************************************/
  62. /*
  63. * allocate the keyrings to be associated with a UID
  64. */
  65. int alloc_uid_keyring(struct user_struct *user)
  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, 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, 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, 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. static int install_process_keyring(struct task_struct *tsk)
  145. {
  146. unsigned long flags;
  147. struct key *keyring;
  148. char buf[20];
  149. int ret;
  150. if (!tsk->signal->process_keyring) {
  151. sprintf(buf, "_pid.%u", tsk->tgid);
  152. keyring = keyring_alloc(buf, tsk->uid, tsk->gid, 1, NULL);
  153. if (IS_ERR(keyring)) {
  154. ret = PTR_ERR(keyring);
  155. goto error;
  156. }
  157. /* attach or swap keyrings */
  158. spin_lock_irqsave(&tsk->sighand->siglock, flags);
  159. if (!tsk->signal->process_keyring) {
  160. tsk->signal->process_keyring = keyring;
  161. keyring = NULL;
  162. }
  163. spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
  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. unsigned long flags;
  179. struct key *old;
  180. char buf[20];
  181. int ret;
  182. /* create an empty session keyring */
  183. if (!keyring) {
  184. sprintf(buf, "_ses.%u", tsk->tgid);
  185. keyring = keyring_alloc(buf, tsk->uid, tsk->gid, 1, NULL);
  186. if (IS_ERR(keyring)) {
  187. ret = PTR_ERR(keyring);
  188. goto error;
  189. }
  190. }
  191. else {
  192. atomic_inc(&keyring->usage);
  193. }
  194. /* install the keyring */
  195. spin_lock_irqsave(&tsk->sighand->siglock, flags);
  196. old = tsk->signal->session_keyring;
  197. tsk->signal->session_keyring = keyring;
  198. spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
  199. ret = 0;
  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. unsigned long flags;
  211. key_check(current->thread_group->session_keyring);
  212. key_check(current->thread_group->process_keyring);
  213. /* no process keyring yet */
  214. tsk->signal->process_keyring = NULL;
  215. /* same session keyring */
  216. spin_lock_irqsave(&current->sighand->siglock, flags);
  217. tsk->signal->session_keyring =
  218. key_get(current->signal->session_keyring);
  219. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  220. return 0;
  221. } /* end copy_thread_group_keys() */
  222. /*****************************************************************************/
  223. /*
  224. * copy the keys for fork
  225. */
  226. int copy_keys(unsigned long clone_flags, struct task_struct *tsk)
  227. {
  228. key_check(tsk->thread_keyring);
  229. /* no thread keyring yet */
  230. tsk->thread_keyring = NULL;
  231. return 0;
  232. } /* end copy_keys() */
  233. /*****************************************************************************/
  234. /*
  235. * dispose of thread group keys upon thread group destruction
  236. */
  237. void exit_thread_group_keys(struct signal_struct *tg)
  238. {
  239. key_put(tg->session_keyring);
  240. key_put(tg->process_keyring);
  241. } /* end exit_thread_group_keys() */
  242. /*****************************************************************************/
  243. /*
  244. * dispose of keys upon thread exit
  245. */
  246. void exit_keys(struct task_struct *tsk)
  247. {
  248. key_put(tsk->thread_keyring);
  249. } /* end exit_keys() */
  250. /*****************************************************************************/
  251. /*
  252. * deal with execve()
  253. */
  254. int exec_keys(struct task_struct *tsk)
  255. {
  256. unsigned long flags;
  257. struct key *old;
  258. /* newly exec'd tasks don't get a thread keyring */
  259. task_lock(tsk);
  260. old = tsk->thread_keyring;
  261. tsk->thread_keyring = NULL;
  262. task_unlock(tsk);
  263. key_put(old);
  264. /* discard the process keyring from a newly exec'd task */
  265. spin_lock_irqsave(&tsk->sighand->siglock, flags);
  266. old = tsk->signal->process_keyring;
  267. tsk->signal->process_keyring = NULL;
  268. spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
  269. key_put(old);
  270. return 0;
  271. } /* end exec_keys() */
  272. /*****************************************************************************/
  273. /*
  274. * deal with SUID programs
  275. * - we might want to make this invent a new session keyring
  276. */
  277. int suid_keys(struct task_struct *tsk)
  278. {
  279. return 0;
  280. } /* end suid_keys() */
  281. /*****************************************************************************/
  282. /*
  283. * the filesystem user ID changed
  284. */
  285. void key_fsuid_changed(struct task_struct *tsk)
  286. {
  287. /* update the ownership of the thread keyring */
  288. if (tsk->thread_keyring) {
  289. down_write(&tsk->thread_keyring->sem);
  290. write_lock(&tsk->thread_keyring->lock);
  291. tsk->thread_keyring->uid = tsk->fsuid;
  292. write_unlock(&tsk->thread_keyring->lock);
  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. write_lock(&tsk->thread_keyring->lock);
  306. tsk->thread_keyring->gid = tsk->fsgid;
  307. write_unlock(&tsk->thread_keyring->lock);
  308. up_write(&tsk->thread_keyring->sem);
  309. }
  310. } /* end key_fsgid_changed() */
  311. /*****************************************************************************/
  312. /*
  313. * search the process keyrings for the first matching key
  314. * - we use the supplied match function to see if the description (or other
  315. * feature of interest) matches
  316. * - we return -EAGAIN if we didn't find any matching key
  317. * - we return -ENOKEY if we found only negative matching keys
  318. */
  319. struct key *search_process_keyrings_aux(struct key_type *type,
  320. const void *description,
  321. key_match_func_t match)
  322. {
  323. struct task_struct *tsk = current;
  324. unsigned long flags;
  325. struct key *key, *ret, *err, *tmp;
  326. /* we want to return -EAGAIN or -ENOKEY if any of the keyrings were
  327. * searchable, but we failed to find a key or we found a negative key;
  328. * otherwise we want to return a sample error (probably -EACCES) if
  329. * none of the keyrings were searchable
  330. *
  331. * in terms of priority: success > -ENOKEY > -EAGAIN > other error
  332. */
  333. key = NULL;
  334. ret = NULL;
  335. err = ERR_PTR(-EAGAIN);
  336. /* search the thread keyring first */
  337. if (tsk->thread_keyring) {
  338. key = keyring_search_aux(tsk->thread_keyring, type,
  339. description, match);
  340. if (!IS_ERR(key))
  341. goto found;
  342. switch (PTR_ERR(key)) {
  343. case -EAGAIN: /* no key */
  344. if (ret)
  345. break;
  346. case -ENOKEY: /* negative key */
  347. ret = key;
  348. break;
  349. default:
  350. err = key;
  351. break;
  352. }
  353. }
  354. /* search the process keyring second */
  355. if (tsk->signal->process_keyring) {
  356. key = keyring_search_aux(tsk->signal->process_keyring,
  357. type, description, match);
  358. if (!IS_ERR(key))
  359. goto found;
  360. switch (PTR_ERR(key)) {
  361. case -EAGAIN: /* no key */
  362. if (ret)
  363. break;
  364. case -ENOKEY: /* negative key */
  365. ret = key;
  366. break;
  367. default:
  368. err = key;
  369. break;
  370. }
  371. }
  372. /* search the session keyring last */
  373. spin_lock_irqsave(&tsk->sighand->siglock, flags);
  374. tmp = tsk->signal->session_keyring;
  375. if (!tmp)
  376. tmp = tsk->user->session_keyring;
  377. atomic_inc(&tmp->usage);
  378. spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
  379. key = keyring_search_aux(tmp, type, description, match);
  380. key_put(tmp);
  381. if (!IS_ERR(key))
  382. goto found;
  383. switch (PTR_ERR(key)) {
  384. case -EAGAIN: /* no key */
  385. if (ret)
  386. break;
  387. case -ENOKEY: /* negative key */
  388. ret = key;
  389. break;
  390. default:
  391. err = key;
  392. break;
  393. }
  394. /* no key - decide on the error we're going to go for */
  395. key = ret ? ret : err;
  396. found:
  397. return key;
  398. } /* end search_process_keyrings_aux() */
  399. /*****************************************************************************/
  400. /*
  401. * search the process keyrings for the first matching key
  402. * - we return -EAGAIN if we didn't find any matching key
  403. * - we return -ENOKEY if we found only negative matching keys
  404. */
  405. struct key *search_process_keyrings(struct key_type *type,
  406. const char *description)
  407. {
  408. return search_process_keyrings_aux(type, description, type->match);
  409. } /* end search_process_keyrings() */
  410. /*****************************************************************************/
  411. /*
  412. * lookup a key given a key ID from userspace with a given permissions mask
  413. * - don't create special keyrings unless so requested
  414. * - partially constructed keys aren't found unless requested
  415. */
  416. struct key *lookup_user_key(key_serial_t id, int create, int partial,
  417. key_perm_t perm)
  418. {
  419. struct task_struct *tsk = current;
  420. unsigned long flags;
  421. struct key *key;
  422. int ret;
  423. key = ERR_PTR(-ENOKEY);
  424. switch (id) {
  425. case KEY_SPEC_THREAD_KEYRING:
  426. if (!tsk->thread_keyring) {
  427. if (!create)
  428. goto error;
  429. ret = install_thread_keyring(tsk);
  430. if (ret < 0) {
  431. key = ERR_PTR(ret);
  432. goto error;
  433. }
  434. }
  435. key = tsk->thread_keyring;
  436. atomic_inc(&key->usage);
  437. break;
  438. case KEY_SPEC_PROCESS_KEYRING:
  439. if (!tsk->signal->process_keyring) {
  440. if (!create)
  441. goto error;
  442. ret = install_process_keyring(tsk);
  443. if (ret < 0) {
  444. key = ERR_PTR(ret);
  445. goto error;
  446. }
  447. }
  448. key = tsk->signal->process_keyring;
  449. atomic_inc(&key->usage);
  450. break;
  451. case KEY_SPEC_SESSION_KEYRING:
  452. if (!tsk->signal->session_keyring) {
  453. /* always install a session keyring upon access if one
  454. * doesn't exist yet */
  455. ret = install_session_keyring(
  456. tsk, tsk->user->session_keyring);
  457. if (ret < 0)
  458. goto error;
  459. }
  460. spin_lock_irqsave(&tsk->sighand->siglock, flags);
  461. key = tsk->signal->session_keyring;
  462. atomic_inc(&key->usage);
  463. spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
  464. break;
  465. case KEY_SPEC_USER_KEYRING:
  466. key = tsk->user->uid_keyring;
  467. atomic_inc(&key->usage);
  468. break;
  469. case KEY_SPEC_USER_SESSION_KEYRING:
  470. key = tsk->user->session_keyring;
  471. atomic_inc(&key->usage);
  472. break;
  473. case KEY_SPEC_GROUP_KEYRING:
  474. /* group keyrings are not yet supported */
  475. key = ERR_PTR(-EINVAL);
  476. goto error;
  477. default:
  478. key = ERR_PTR(-EINVAL);
  479. if (id < 1)
  480. goto error;
  481. key = key_lookup(id);
  482. if (IS_ERR(key))
  483. goto error;
  484. break;
  485. }
  486. /* check the status and permissions */
  487. if (perm) {
  488. ret = key_validate(key);
  489. if (ret < 0)
  490. goto invalid_key;
  491. }
  492. ret = -EIO;
  493. if (!partial && !(key->flags & KEY_FLAG_INSTANTIATED))
  494. goto invalid_key;
  495. ret = -EACCES;
  496. if (!key_permission(key, perm))
  497. goto invalid_key;
  498. error:
  499. return key;
  500. invalid_key:
  501. key_put(key);
  502. key = ERR_PTR(ret);
  503. goto error;
  504. } /* end lookup_user_key() */
  505. /*****************************************************************************/
  506. /*
  507. * join the named keyring as the session keyring if possible, or attempt to
  508. * create a new one of that name if not
  509. * - if the name is NULL, an empty anonymous keyring is installed instead
  510. * - named session keyring joining is done with a semaphore held
  511. */
  512. long join_session_keyring(const char *name)
  513. {
  514. struct task_struct *tsk = current;
  515. unsigned long flags;
  516. struct key *keyring;
  517. long ret;
  518. /* if no name is provided, install an anonymous keyring */
  519. if (!name) {
  520. ret = install_session_keyring(tsk, NULL);
  521. if (ret < 0)
  522. goto error;
  523. spin_lock_irqsave(&tsk->sighand->siglock, flags);
  524. ret = tsk->signal->session_keyring->serial;
  525. spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
  526. goto error;
  527. }
  528. /* allow the user to join or create a named keyring */
  529. down(&key_session_sem);
  530. /* look for an existing keyring of this name */
  531. keyring = find_keyring_by_name(name, 0);
  532. if (PTR_ERR(keyring) == -ENOKEY) {
  533. /* not found - try and create a new one */
  534. keyring = keyring_alloc(name, tsk->uid, tsk->gid, 0, NULL);
  535. if (IS_ERR(keyring)) {
  536. ret = PTR_ERR(keyring);
  537. goto error;
  538. }
  539. }
  540. else if (IS_ERR(keyring)) {
  541. ret = PTR_ERR(keyring);
  542. goto error2;
  543. }
  544. /* we've got a keyring - now to install it */
  545. ret = install_session_keyring(tsk, keyring);
  546. if (ret < 0)
  547. goto error2;
  548. ret = keyring->serial;
  549. key_put(keyring);
  550. error2:
  551. up(&key_session_sem);
  552. error:
  553. return ret;
  554. } /* end join_session_keyring() */