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. {
  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. unsigned long flags;
  146. struct key *keyring;
  147. char buf[20];
  148. int ret;
  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_irqsave(&tsk->sighand->siglock, flags);
  158. if (!tsk->signal->process_keyring) {
  159. tsk->signal->process_keyring = keyring;
  160. keyring = NULL;
  161. }
  162. spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
  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. unsigned long flags;
  178. struct key *old;
  179. char buf[20];
  180. int ret;
  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, 1, NULL);
  185. if (IS_ERR(keyring)) {
  186. ret = PTR_ERR(keyring);
  187. goto error;
  188. }
  189. }
  190. else {
  191. atomic_inc(&keyring->usage);
  192. }
  193. /* install the keyring */
  194. spin_lock_irqsave(&tsk->sighand->siglock, flags);
  195. old = rcu_dereference(tsk->signal->session_keyring);
  196. rcu_assign_pointer(tsk->signal->session_keyring, keyring);
  197. spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
  198. ret = 0;
  199. /* we're using RCU on the pointer */
  200. synchronize_rcu();
  201. key_put(old);
  202. error:
  203. return ret;
  204. } /* end install_session_keyring() */
  205. /*****************************************************************************/
  206. /*
  207. * copy the keys in a thread group for fork without CLONE_THREAD
  208. */
  209. int copy_thread_group_keys(struct task_struct *tsk)
  210. {
  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. rcu_read_lock();
  217. tsk->signal->session_keyring =
  218. key_get(rcu_dereference(current->signal->session_keyring));
  219. rcu_read_unlock();
  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. key_check(tsk->request_key_auth);
  230. /* no thread keyring yet */
  231. tsk->thread_keyring = NULL;
  232. /* copy the request_key() authorisation for this thread */
  233. key_get(tsk->request_key_auth);
  234. return 0;
  235. } /* end copy_keys() */
  236. /*****************************************************************************/
  237. /*
  238. * dispose of thread group keys upon thread group destruction
  239. */
  240. void exit_thread_group_keys(struct signal_struct *tg)
  241. {
  242. key_put(tg->session_keyring);
  243. key_put(tg->process_keyring);
  244. } /* end exit_thread_group_keys() */
  245. /*****************************************************************************/
  246. /*
  247. * dispose of per-thread keys upon thread exit
  248. */
  249. void exit_keys(struct task_struct *tsk)
  250. {
  251. key_put(tsk->thread_keyring);
  252. key_put(tsk->request_key_auth);
  253. } /* end exit_keys() */
  254. /*****************************************************************************/
  255. /*
  256. * deal with execve()
  257. */
  258. int exec_keys(struct task_struct *tsk)
  259. {
  260. unsigned long flags;
  261. struct key *old;
  262. /* newly exec'd tasks don't get a thread keyring */
  263. task_lock(tsk);
  264. old = tsk->thread_keyring;
  265. tsk->thread_keyring = NULL;
  266. task_unlock(tsk);
  267. key_put(old);
  268. /* discard the process keyring from a newly exec'd task */
  269. spin_lock_irqsave(&tsk->sighand->siglock, flags);
  270. old = tsk->signal->process_keyring;
  271. tsk->signal->process_keyring = NULL;
  272. spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
  273. key_put(old);
  274. return 0;
  275. } /* end exec_keys() */
  276. /*****************************************************************************/
  277. /*
  278. * deal with SUID programs
  279. * - we might want to make this invent a new session keyring
  280. */
  281. int suid_keys(struct task_struct *tsk)
  282. {
  283. return 0;
  284. } /* end suid_keys() */
  285. /*****************************************************************************/
  286. /*
  287. * the filesystem user ID changed
  288. */
  289. void key_fsuid_changed(struct task_struct *tsk)
  290. {
  291. /* update the ownership of the thread keyring */
  292. if (tsk->thread_keyring) {
  293. down_write(&tsk->thread_keyring->sem);
  294. tsk->thread_keyring->uid = tsk->fsuid;
  295. up_write(&tsk->thread_keyring->sem);
  296. }
  297. } /* end key_fsuid_changed() */
  298. /*****************************************************************************/
  299. /*
  300. * the filesystem group ID changed
  301. */
  302. void key_fsgid_changed(struct task_struct *tsk)
  303. {
  304. /* update the ownership of the thread keyring */
  305. if (tsk->thread_keyring) {
  306. down_write(&tsk->thread_keyring->sem);
  307. tsk->thread_keyring->gid = tsk->fsgid;
  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. key_ref_t search_process_keyrings(struct key_type *type,
  320. const void *description,
  321. key_match_func_t match,
  322. struct task_struct *context)
  323. {
  324. struct request_key_auth *rka;
  325. key_ref_t key_ref, ret, err;
  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_ref = NULL;
  334. ret = NULL;
  335. err = ERR_PTR(-EAGAIN);
  336. /* search the thread keyring first */
  337. if (context->thread_keyring) {
  338. key_ref = keyring_search_aux(
  339. make_key_ref(context->thread_keyring, 1),
  340. context, type, description, match);
  341. if (!IS_ERR(key_ref))
  342. goto found;
  343. switch (PTR_ERR(key_ref)) {
  344. case -EAGAIN: /* no key */
  345. if (ret)
  346. break;
  347. case -ENOKEY: /* negative key */
  348. ret = key_ref;
  349. break;
  350. default:
  351. err = key_ref;
  352. break;
  353. }
  354. }
  355. /* search the process keyring second */
  356. if (context->signal->process_keyring) {
  357. key_ref = keyring_search_aux(
  358. make_key_ref(context->signal->process_keyring, 1),
  359. context, type, description, match);
  360. if (!IS_ERR(key_ref))
  361. goto found;
  362. switch (PTR_ERR(key_ref)) {
  363. case -EAGAIN: /* no key */
  364. if (ret)
  365. break;
  366. case -ENOKEY: /* negative key */
  367. ret = key_ref;
  368. break;
  369. default:
  370. err = key_ref;
  371. break;
  372. }
  373. }
  374. /* search the session keyring */
  375. if (context->signal->session_keyring) {
  376. rcu_read_lock();
  377. key_ref = keyring_search_aux(
  378. make_key_ref(rcu_dereference(
  379. context->signal->session_keyring),
  380. 1),
  381. context, type, description, match);
  382. rcu_read_unlock();
  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. }
  397. /* or search the user-session keyring */
  398. else {
  399. key_ref = keyring_search_aux(
  400. make_key_ref(context->user->session_keyring, 1),
  401. context, type, description, match);
  402. if (!IS_ERR(key_ref))
  403. goto found;
  404. switch (PTR_ERR(key_ref)) {
  405. case -EAGAIN: /* no key */
  406. if (ret)
  407. break;
  408. case -ENOKEY: /* negative key */
  409. ret = key_ref;
  410. break;
  411. default:
  412. err = key_ref;
  413. break;
  414. }
  415. }
  416. /* if this process has an instantiation authorisation key, then we also
  417. * search the keyrings of the process mentioned there
  418. * - we don't permit access to request_key auth keys via this method
  419. */
  420. if (context->request_key_auth &&
  421. context == current &&
  422. type != &key_type_request_key_auth &&
  423. key_validate(context->request_key_auth) == 0
  424. ) {
  425. rka = context->request_key_auth->payload.data;
  426. key_ref = search_process_keyrings(type, description, match,
  427. rka->context);
  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 - decide on the error we're going to go for */
  443. key_ref = ret ? ret : err;
  444. found:
  445. return key_ref;
  446. } /* end search_process_keyrings() */
  447. /*****************************************************************************/
  448. /*
  449. * see if the key we're looking at is the target key
  450. */
  451. static int lookup_user_key_possessed(const struct key *key, const void *target)
  452. {
  453. return key == target;
  454. } /* end lookup_user_key_possessed() */
  455. /*****************************************************************************/
  456. /*
  457. * lookup a key given a key ID from userspace with a given permissions mask
  458. * - don't create special keyrings unless so requested
  459. * - partially constructed keys aren't found unless requested
  460. */
  461. key_ref_t lookup_user_key(struct task_struct *context, key_serial_t id,
  462. int create, int partial, key_perm_t perm)
  463. {
  464. key_ref_t key_ref, skey_ref;
  465. struct key *key;
  466. int ret;
  467. if (!context)
  468. context = current;
  469. key_ref = ERR_PTR(-ENOKEY);
  470. switch (id) {
  471. case KEY_SPEC_THREAD_KEYRING:
  472. if (!context->thread_keyring) {
  473. if (!create)
  474. goto error;
  475. ret = install_thread_keyring(context);
  476. if (ret < 0) {
  477. key = ERR_PTR(ret);
  478. goto error;
  479. }
  480. }
  481. key = context->thread_keyring;
  482. atomic_inc(&key->usage);
  483. key_ref = make_key_ref(key, 1);
  484. break;
  485. case KEY_SPEC_PROCESS_KEYRING:
  486. if (!context->signal->process_keyring) {
  487. if (!create)
  488. goto error;
  489. ret = install_process_keyring(context);
  490. if (ret < 0) {
  491. key = ERR_PTR(ret);
  492. goto error;
  493. }
  494. }
  495. key = context->signal->process_keyring;
  496. atomic_inc(&key->usage);
  497. key_ref = make_key_ref(key, 1);
  498. break;
  499. case KEY_SPEC_SESSION_KEYRING:
  500. if (!context->signal->session_keyring) {
  501. /* always install a session keyring upon access if one
  502. * doesn't exist yet */
  503. ret = install_session_keyring(
  504. context, context->user->session_keyring);
  505. if (ret < 0)
  506. goto error;
  507. }
  508. rcu_read_lock();
  509. key = rcu_dereference(context->signal->session_keyring);
  510. atomic_inc(&key->usage);
  511. rcu_read_unlock();
  512. key_ref = make_key_ref(key, 1);
  513. break;
  514. case KEY_SPEC_USER_KEYRING:
  515. key = context->user->uid_keyring;
  516. atomic_inc(&key->usage);
  517. key_ref = make_key_ref(key, 1);
  518. break;
  519. case KEY_SPEC_USER_SESSION_KEYRING:
  520. key = context->user->session_keyring;
  521. atomic_inc(&key->usage);
  522. key_ref = make_key_ref(key, 1);
  523. break;
  524. case KEY_SPEC_GROUP_KEYRING:
  525. /* group keyrings are not yet supported */
  526. key = ERR_PTR(-EINVAL);
  527. goto error;
  528. case KEY_SPEC_REQKEY_AUTH_KEY:
  529. key = context->request_key_auth;
  530. if (!key)
  531. goto error;
  532. atomic_inc(&key->usage);
  533. key_ref = make_key_ref(key, 1);
  534. break;
  535. default:
  536. key_ref = ERR_PTR(-EINVAL);
  537. if (id < 1)
  538. goto error;
  539. key = key_lookup(id);
  540. if (IS_ERR(key)) {
  541. key_ref = ERR_PTR(PTR_ERR(key));
  542. goto error;
  543. }
  544. key_ref = make_key_ref(key, 0);
  545. /* check to see if we possess the key */
  546. skey_ref = search_process_keyrings(key->type, key,
  547. lookup_user_key_possessed,
  548. current);
  549. if (!IS_ERR(skey_ref)) {
  550. key_put(key);
  551. key_ref = skey_ref;
  552. }
  553. break;
  554. }
  555. /* check the status */
  556. if (perm) {
  557. ret = key_validate(key);
  558. if (ret < 0)
  559. goto invalid_key;
  560. }
  561. ret = -EIO;
  562. if (!partial && !test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
  563. goto invalid_key;
  564. /* check the permissions */
  565. ret = key_task_permission(key_ref, context, perm);
  566. if (ret < 0)
  567. goto invalid_key;
  568. error:
  569. return key_ref;
  570. invalid_key:
  571. key_ref_put(key_ref);
  572. key_ref = ERR_PTR(ret);
  573. goto error;
  574. } /* end lookup_user_key() */
  575. /*****************************************************************************/
  576. /*
  577. * join the named keyring as the session keyring if possible, or attempt to
  578. * create a new one of that name if not
  579. * - if the name is NULL, an empty anonymous keyring is installed instead
  580. * - named session keyring joining is done with a semaphore held
  581. */
  582. long join_session_keyring(const char *name)
  583. {
  584. struct task_struct *tsk = current;
  585. struct key *keyring;
  586. long ret;
  587. /* if no name is provided, install an anonymous keyring */
  588. if (!name) {
  589. ret = install_session_keyring(tsk, NULL);
  590. if (ret < 0)
  591. goto error;
  592. rcu_read_lock();
  593. ret = rcu_dereference(tsk->signal->session_keyring)->serial;
  594. rcu_read_unlock();
  595. goto error;
  596. }
  597. /* allow the user to join or create a named keyring */
  598. mutex_lock(&key_session_mutex);
  599. /* look for an existing keyring of this name */
  600. keyring = find_keyring_by_name(name, 0);
  601. if (PTR_ERR(keyring) == -ENOKEY) {
  602. /* not found - try and create a new one */
  603. keyring = keyring_alloc(name, tsk->uid, tsk->gid, 0, NULL);
  604. if (IS_ERR(keyring)) {
  605. ret = PTR_ERR(keyring);
  606. goto error2;
  607. }
  608. }
  609. else if (IS_ERR(keyring)) {
  610. ret = PTR_ERR(keyring);
  611. goto error2;
  612. }
  613. /* we've got a keyring - now to install it */
  614. ret = install_session_keyring(tsk, keyring);
  615. if (ret < 0)
  616. goto error2;
  617. ret = keyring->serial;
  618. key_put(keyring);
  619. error2:
  620. mutex_unlock(&key_session_mutex);
  621. error:
  622. return ret;
  623. } /* end join_session_keyring() */