cred.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /* Task credentials management
  2. *
  3. * Copyright (C) 2008 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 Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/cred.h>
  13. #include <linux/sched.h>
  14. #include <linux/key.h>
  15. #include <linux/keyctl.h>
  16. #include <linux/init_task.h>
  17. #include <linux/security.h>
  18. #include <linux/cn_proc.h>
  19. #include "cred-internals.h"
  20. static struct kmem_cache *cred_jar;
  21. /*
  22. * The common credentials for the initial task's thread group
  23. */
  24. #ifdef CONFIG_KEYS
  25. static struct thread_group_cred init_tgcred = {
  26. .usage = ATOMIC_INIT(2),
  27. .tgid = 0,
  28. .lock = SPIN_LOCK_UNLOCKED,
  29. };
  30. #endif
  31. /*
  32. * The initial credentials for the initial task
  33. */
  34. struct cred init_cred = {
  35. .usage = ATOMIC_INIT(3),
  36. .securebits = SECUREBITS_DEFAULT,
  37. .cap_inheritable = CAP_INIT_INH_SET,
  38. .cap_permitted = CAP_FULL_SET,
  39. .cap_effective = CAP_INIT_EFF_SET,
  40. .cap_bset = CAP_INIT_BSET,
  41. .user = INIT_USER,
  42. .group_info = &init_groups,
  43. #ifdef CONFIG_KEYS
  44. .tgcred = &init_tgcred,
  45. #endif
  46. };
  47. /*
  48. * Dispose of the shared task group credentials
  49. */
  50. #ifdef CONFIG_KEYS
  51. static void release_tgcred_rcu(struct rcu_head *rcu)
  52. {
  53. struct thread_group_cred *tgcred =
  54. container_of(rcu, struct thread_group_cred, rcu);
  55. BUG_ON(atomic_read(&tgcred->usage) != 0);
  56. key_put(tgcred->session_keyring);
  57. key_put(tgcred->process_keyring);
  58. kfree(tgcred);
  59. }
  60. #endif
  61. /*
  62. * Release a set of thread group credentials.
  63. */
  64. static void release_tgcred(struct cred *cred)
  65. {
  66. #ifdef CONFIG_KEYS
  67. struct thread_group_cred *tgcred = cred->tgcred;
  68. if (atomic_dec_and_test(&tgcred->usage))
  69. call_rcu(&tgcred->rcu, release_tgcred_rcu);
  70. #endif
  71. }
  72. /*
  73. * The RCU callback to actually dispose of a set of credentials
  74. */
  75. static void put_cred_rcu(struct rcu_head *rcu)
  76. {
  77. struct cred *cred = container_of(rcu, struct cred, rcu);
  78. if (atomic_read(&cred->usage) != 0)
  79. panic("CRED: put_cred_rcu() sees %p with usage %d\n",
  80. cred, atomic_read(&cred->usage));
  81. security_cred_free(cred);
  82. key_put(cred->thread_keyring);
  83. key_put(cred->request_key_auth);
  84. release_tgcred(cred);
  85. put_group_info(cred->group_info);
  86. free_uid(cred->user);
  87. kmem_cache_free(cred_jar, cred);
  88. }
  89. /**
  90. * __put_cred - Destroy a set of credentials
  91. * @cred: The record to release
  92. *
  93. * Destroy a set of credentials on which no references remain.
  94. */
  95. void __put_cred(struct cred *cred)
  96. {
  97. BUG_ON(atomic_read(&cred->usage) != 0);
  98. call_rcu(&cred->rcu, put_cred_rcu);
  99. }
  100. EXPORT_SYMBOL(__put_cred);
  101. /**
  102. * prepare_creds - Prepare a new set of credentials for modification
  103. *
  104. * Prepare a new set of task credentials for modification. A task's creds
  105. * shouldn't generally be modified directly, therefore this function is used to
  106. * prepare a new copy, which the caller then modifies and then commits by
  107. * calling commit_creds().
  108. *
  109. * Returns a pointer to the new creds-to-be if successful, NULL otherwise.
  110. *
  111. * Call commit_creds() or abort_creds() to clean up.
  112. */
  113. struct cred *prepare_creds(void)
  114. {
  115. struct task_struct *task = current;
  116. const struct cred *old;
  117. struct cred *new;
  118. BUG_ON(atomic_read(&task->cred->usage) < 1);
  119. new = kmem_cache_alloc(cred_jar, GFP_KERNEL);
  120. if (!new)
  121. return NULL;
  122. old = task->cred;
  123. memcpy(new, old, sizeof(struct cred));
  124. atomic_set(&new->usage, 1);
  125. get_group_info(new->group_info);
  126. get_uid(new->user);
  127. #ifdef CONFIG_KEYS
  128. key_get(new->thread_keyring);
  129. key_get(new->request_key_auth);
  130. atomic_inc(&new->tgcred->usage);
  131. #endif
  132. #ifdef CONFIG_SECURITY
  133. new->security = NULL;
  134. #endif
  135. if (security_prepare_creds(new, old, GFP_KERNEL) < 0)
  136. goto error;
  137. return new;
  138. error:
  139. abort_creds(new);
  140. return NULL;
  141. }
  142. EXPORT_SYMBOL(prepare_creds);
  143. /*
  144. * Prepare credentials for current to perform an execve()
  145. * - The caller must hold current->cred_exec_mutex
  146. */
  147. struct cred *prepare_exec_creds(void)
  148. {
  149. struct thread_group_cred *tgcred = NULL;
  150. struct cred *new;
  151. #ifdef CONFIG_KEYS
  152. tgcred = kmalloc(sizeof(*tgcred), GFP_KERNEL);
  153. if (!tgcred)
  154. return NULL;
  155. #endif
  156. new = prepare_creds();
  157. if (!new) {
  158. kfree(tgcred);
  159. return new;
  160. }
  161. #ifdef CONFIG_KEYS
  162. /* newly exec'd tasks don't get a thread keyring */
  163. key_put(new->thread_keyring);
  164. new->thread_keyring = NULL;
  165. /* create a new per-thread-group creds for all this set of threads to
  166. * share */
  167. memcpy(tgcred, new->tgcred, sizeof(struct thread_group_cred));
  168. atomic_set(&tgcred->usage, 1);
  169. spin_lock_init(&tgcred->lock);
  170. /* inherit the session keyring; new process keyring */
  171. key_get(tgcred->session_keyring);
  172. tgcred->process_keyring = NULL;
  173. release_tgcred(new);
  174. new->tgcred = tgcred;
  175. #endif
  176. return new;
  177. }
  178. /*
  179. * prepare new credentials for the usermode helper dispatcher
  180. */
  181. struct cred *prepare_usermodehelper_creds(void)
  182. {
  183. #ifdef CONFIG_KEYS
  184. struct thread_group_cred *tgcred = NULL;
  185. #endif
  186. struct cred *new;
  187. #ifdef CONFIG_KEYS
  188. tgcred = kzalloc(sizeof(*new->tgcred), GFP_ATOMIC);
  189. if (!tgcred)
  190. return NULL;
  191. #endif
  192. new = kmem_cache_alloc(cred_jar, GFP_ATOMIC);
  193. if (!new)
  194. return NULL;
  195. memcpy(new, &init_cred, sizeof(struct cred));
  196. atomic_set(&new->usage, 1);
  197. get_group_info(new->group_info);
  198. get_uid(new->user);
  199. #ifdef CONFIG_KEYS
  200. new->thread_keyring = NULL;
  201. new->request_key_auth = NULL;
  202. new->jit_keyring = KEY_REQKEY_DEFL_DEFAULT;
  203. atomic_set(&tgcred->usage, 1);
  204. spin_lock_init(&tgcred->lock);
  205. new->tgcred = tgcred;
  206. #endif
  207. #ifdef CONFIG_SECURITY
  208. new->security = NULL;
  209. #endif
  210. if (security_prepare_creds(new, &init_cred, GFP_ATOMIC) < 0)
  211. goto error;
  212. BUG_ON(atomic_read(&new->usage) != 1);
  213. return new;
  214. error:
  215. put_cred(new);
  216. return NULL;
  217. }
  218. /*
  219. * Copy credentials for the new process created by fork()
  220. *
  221. * We share if we can, but under some circumstances we have to generate a new
  222. * set.
  223. */
  224. int copy_creds(struct task_struct *p, unsigned long clone_flags)
  225. {
  226. #ifdef CONFIG_KEYS
  227. struct thread_group_cred *tgcred;
  228. #endif
  229. struct cred *new;
  230. mutex_init(&p->cred_exec_mutex);
  231. if (
  232. #ifdef CONFIG_KEYS
  233. !p->cred->thread_keyring &&
  234. #endif
  235. clone_flags & CLONE_THREAD
  236. ) {
  237. get_cred(p->cred);
  238. atomic_inc(&p->cred->user->processes);
  239. return 0;
  240. }
  241. new = prepare_creds();
  242. if (!new)
  243. return -ENOMEM;
  244. #ifdef CONFIG_KEYS
  245. /* new threads get their own thread keyrings if their parent already
  246. * had one */
  247. if (new->thread_keyring) {
  248. key_put(new->thread_keyring);
  249. new->thread_keyring = NULL;
  250. if (clone_flags & CLONE_THREAD)
  251. install_thread_keyring_to_cred(new);
  252. }
  253. /* we share the process and session keyrings between all the threads in
  254. * a process - this is slightly icky as we violate COW credentials a
  255. * bit */
  256. if (!(clone_flags & CLONE_THREAD)) {
  257. tgcred = kmalloc(sizeof(*tgcred), GFP_KERNEL);
  258. if (!tgcred) {
  259. put_cred(new);
  260. return -ENOMEM;
  261. }
  262. atomic_set(&tgcred->usage, 1);
  263. spin_lock_init(&tgcred->lock);
  264. tgcred->process_keyring = NULL;
  265. tgcred->session_keyring = key_get(new->tgcred->session_keyring);
  266. release_tgcred(new);
  267. new->tgcred = tgcred;
  268. }
  269. #endif
  270. atomic_inc(&new->user->processes);
  271. p->cred = new;
  272. return 0;
  273. }
  274. /**
  275. * commit_creds - Install new credentials upon the current task
  276. * @new: The credentials to be assigned
  277. *
  278. * Install a new set of credentials to the current task, using RCU to replace
  279. * the old set.
  280. *
  281. * This function eats the caller's reference to the new credentials.
  282. *
  283. * Always returns 0 thus allowing this function to be tail-called at the end
  284. * of, say, sys_setgid().
  285. */
  286. int commit_creds(struct cred *new)
  287. {
  288. struct task_struct *task = current;
  289. const struct cred *old;
  290. BUG_ON(atomic_read(&new->usage) < 1);
  291. BUG_ON(atomic_read(&task->cred->usage) < 1);
  292. old = task->cred;
  293. security_commit_creds(new, old);
  294. /* dumpability changes */
  295. if (old->euid != new->euid ||
  296. old->egid != new->egid ||
  297. old->fsuid != new->fsuid ||
  298. old->fsgid != new->fsgid ||
  299. !cap_issubset(new->cap_permitted, old->cap_permitted)) {
  300. set_dumpable(task->mm, suid_dumpable);
  301. task->pdeath_signal = 0;
  302. smp_wmb();
  303. }
  304. /* alter the thread keyring */
  305. if (new->fsuid != old->fsuid)
  306. key_fsuid_changed(task);
  307. if (new->fsgid != old->fsgid)
  308. key_fsgid_changed(task);
  309. /* do it
  310. * - What if a process setreuid()'s and this brings the
  311. * new uid over his NPROC rlimit? We can check this now
  312. * cheaply with the new uid cache, so if it matters
  313. * we should be checking for it. -DaveM
  314. */
  315. if (new->user != old->user)
  316. atomic_inc(&new->user->processes);
  317. rcu_assign_pointer(task->cred, new);
  318. if (new->user != old->user)
  319. atomic_dec(&old->user->processes);
  320. sched_switch_user(task);
  321. /* send notifications */
  322. if (new->uid != old->uid ||
  323. new->euid != old->euid ||
  324. new->suid != old->suid ||
  325. new->fsuid != old->fsuid)
  326. proc_id_connector(task, PROC_EVENT_UID);
  327. if (new->gid != old->gid ||
  328. new->egid != old->egid ||
  329. new->sgid != old->sgid ||
  330. new->fsgid != old->fsgid)
  331. proc_id_connector(task, PROC_EVENT_GID);
  332. put_cred(old);
  333. return 0;
  334. }
  335. EXPORT_SYMBOL(commit_creds);
  336. /**
  337. * abort_creds - Discard a set of credentials and unlock the current task
  338. * @new: The credentials that were going to be applied
  339. *
  340. * Discard a set of credentials that were under construction and unlock the
  341. * current task.
  342. */
  343. void abort_creds(struct cred *new)
  344. {
  345. BUG_ON(atomic_read(&new->usage) < 1);
  346. put_cred(new);
  347. }
  348. EXPORT_SYMBOL(abort_creds);
  349. /**
  350. * override_creds - Temporarily override the current process's credentials
  351. * @new: The credentials to be assigned
  352. *
  353. * Install a set of temporary override credentials on the current process,
  354. * returning the old set for later reversion.
  355. */
  356. const struct cred *override_creds(const struct cred *new)
  357. {
  358. const struct cred *old = current->cred;
  359. rcu_assign_pointer(current->cred, get_cred(new));
  360. return old;
  361. }
  362. EXPORT_SYMBOL(override_creds);
  363. /**
  364. * revert_creds - Revert a temporary credentials override
  365. * @old: The credentials to be restored
  366. *
  367. * Revert a temporary set of override credentials to an old set, discarding the
  368. * override set.
  369. */
  370. void revert_creds(const struct cred *old)
  371. {
  372. const struct cred *override = current->cred;
  373. rcu_assign_pointer(current->cred, old);
  374. put_cred(override);
  375. }
  376. EXPORT_SYMBOL(revert_creds);
  377. /*
  378. * initialise the credentials stuff
  379. */
  380. void __init cred_init(void)
  381. {
  382. /* allocate a slab in which we can store credentials */
  383. cred_jar = kmem_cache_create("cred_jar", sizeof(struct cred),
  384. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  385. }