cred.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /* Task credentials management - see Documentation/credentials.txt
  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(4),
  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. * Preparation involves making a copy of the objective creds for modification.
  110. *
  111. * Returns a pointer to the new creds-to-be if successful, NULL otherwise.
  112. *
  113. * Call commit_creds() or abort_creds() to clean up.
  114. */
  115. struct cred *prepare_creds(void)
  116. {
  117. struct task_struct *task = current;
  118. const struct cred *old;
  119. struct cred *new;
  120. BUG_ON(atomic_read(&task->real_cred->usage) < 1);
  121. new = kmem_cache_alloc(cred_jar, GFP_KERNEL);
  122. if (!new)
  123. return NULL;
  124. old = task->cred;
  125. memcpy(new, old, sizeof(struct cred));
  126. atomic_set(&new->usage, 1);
  127. get_group_info(new->group_info);
  128. get_uid(new->user);
  129. #ifdef CONFIG_KEYS
  130. key_get(new->thread_keyring);
  131. key_get(new->request_key_auth);
  132. atomic_inc(&new->tgcred->usage);
  133. #endif
  134. #ifdef CONFIG_SECURITY
  135. new->security = NULL;
  136. #endif
  137. if (security_prepare_creds(new, old, GFP_KERNEL) < 0)
  138. goto error;
  139. return new;
  140. error:
  141. abort_creds(new);
  142. return NULL;
  143. }
  144. EXPORT_SYMBOL(prepare_creds);
  145. /*
  146. * Prepare credentials for current to perform an execve()
  147. * - The caller must hold current->cred_exec_mutex
  148. */
  149. struct cred *prepare_exec_creds(void)
  150. {
  151. struct thread_group_cred *tgcred = NULL;
  152. struct cred *new;
  153. #ifdef CONFIG_KEYS
  154. tgcred = kmalloc(sizeof(*tgcred), GFP_KERNEL);
  155. if (!tgcred)
  156. return NULL;
  157. #endif
  158. new = prepare_creds();
  159. if (!new) {
  160. kfree(tgcred);
  161. return new;
  162. }
  163. #ifdef CONFIG_KEYS
  164. /* newly exec'd tasks don't get a thread keyring */
  165. key_put(new->thread_keyring);
  166. new->thread_keyring = NULL;
  167. /* create a new per-thread-group creds for all this set of threads to
  168. * share */
  169. memcpy(tgcred, new->tgcred, sizeof(struct thread_group_cred));
  170. atomic_set(&tgcred->usage, 1);
  171. spin_lock_init(&tgcred->lock);
  172. /* inherit the session keyring; new process keyring */
  173. key_get(tgcred->session_keyring);
  174. tgcred->process_keyring = NULL;
  175. release_tgcred(new);
  176. new->tgcred = tgcred;
  177. #endif
  178. return new;
  179. }
  180. /*
  181. * prepare new credentials for the usermode helper dispatcher
  182. */
  183. struct cred *prepare_usermodehelper_creds(void)
  184. {
  185. #ifdef CONFIG_KEYS
  186. struct thread_group_cred *tgcred = NULL;
  187. #endif
  188. struct cred *new;
  189. #ifdef CONFIG_KEYS
  190. tgcred = kzalloc(sizeof(*new->tgcred), GFP_ATOMIC);
  191. if (!tgcred)
  192. return NULL;
  193. #endif
  194. new = kmem_cache_alloc(cred_jar, GFP_ATOMIC);
  195. if (!new)
  196. return NULL;
  197. memcpy(new, &init_cred, sizeof(struct cred));
  198. atomic_set(&new->usage, 1);
  199. get_group_info(new->group_info);
  200. get_uid(new->user);
  201. #ifdef CONFIG_KEYS
  202. new->thread_keyring = NULL;
  203. new->request_key_auth = NULL;
  204. new->jit_keyring = KEY_REQKEY_DEFL_DEFAULT;
  205. atomic_set(&tgcred->usage, 1);
  206. spin_lock_init(&tgcred->lock);
  207. new->tgcred = tgcred;
  208. #endif
  209. #ifdef CONFIG_SECURITY
  210. new->security = NULL;
  211. #endif
  212. if (security_prepare_creds(new, &init_cred, GFP_ATOMIC) < 0)
  213. goto error;
  214. BUG_ON(atomic_read(&new->usage) != 1);
  215. return new;
  216. error:
  217. put_cred(new);
  218. return NULL;
  219. }
  220. /*
  221. * Copy credentials for the new process created by fork()
  222. *
  223. * We share if we can, but under some circumstances we have to generate a new
  224. * set.
  225. *
  226. * The new process gets the current process's subjective credentials as its
  227. * objective and subjective credentials
  228. */
  229. int copy_creds(struct task_struct *p, unsigned long clone_flags)
  230. {
  231. #ifdef CONFIG_KEYS
  232. struct thread_group_cred *tgcred;
  233. #endif
  234. struct cred *new;
  235. mutex_init(&p->cred_exec_mutex);
  236. if (
  237. #ifdef CONFIG_KEYS
  238. !p->cred->thread_keyring &&
  239. #endif
  240. clone_flags & CLONE_THREAD
  241. ) {
  242. p->real_cred = get_cred(p->cred);
  243. get_cred(p->cred);
  244. atomic_inc(&p->cred->user->processes);
  245. return 0;
  246. }
  247. new = prepare_creds();
  248. if (!new)
  249. return -ENOMEM;
  250. #ifdef CONFIG_KEYS
  251. /* new threads get their own thread keyrings if their parent already
  252. * had one */
  253. if (new->thread_keyring) {
  254. key_put(new->thread_keyring);
  255. new->thread_keyring = NULL;
  256. if (clone_flags & CLONE_THREAD)
  257. install_thread_keyring_to_cred(new);
  258. }
  259. /* we share the process and session keyrings between all the threads in
  260. * a process - this is slightly icky as we violate COW credentials a
  261. * bit */
  262. if (!(clone_flags & CLONE_THREAD)) {
  263. tgcred = kmalloc(sizeof(*tgcred), GFP_KERNEL);
  264. if (!tgcred) {
  265. put_cred(new);
  266. return -ENOMEM;
  267. }
  268. atomic_set(&tgcred->usage, 1);
  269. spin_lock_init(&tgcred->lock);
  270. tgcred->process_keyring = NULL;
  271. tgcred->session_keyring = key_get(new->tgcred->session_keyring);
  272. release_tgcred(new);
  273. new->tgcred = tgcred;
  274. }
  275. #endif
  276. atomic_inc(&new->user->processes);
  277. p->cred = p->real_cred = get_cred(new);
  278. return 0;
  279. }
  280. /**
  281. * commit_creds - Install new credentials upon the current task
  282. * @new: The credentials to be assigned
  283. *
  284. * Install a new set of credentials to the current task, using RCU to replace
  285. * the old set. Both the objective and the subjective credentials pointers are
  286. * updated. This function may not be called if the subjective credentials are
  287. * in an overridden state.
  288. *
  289. * This function eats the caller's reference to the new credentials.
  290. *
  291. * Always returns 0 thus allowing this function to be tail-called at the end
  292. * of, say, sys_setgid().
  293. */
  294. int commit_creds(struct cred *new)
  295. {
  296. struct task_struct *task = current;
  297. const struct cred *old;
  298. BUG_ON(task->cred != task->real_cred);
  299. BUG_ON(atomic_read(&task->real_cred->usage) < 2);
  300. BUG_ON(atomic_read(&new->usage) < 1);
  301. old = task->real_cred;
  302. security_commit_creds(new, old);
  303. get_cred(new); /* we will require a ref for the subj creds too */
  304. /* dumpability changes */
  305. if (old->euid != new->euid ||
  306. old->egid != new->egid ||
  307. old->fsuid != new->fsuid ||
  308. old->fsgid != new->fsgid ||
  309. !cap_issubset(new->cap_permitted, old->cap_permitted)) {
  310. set_dumpable(task->mm, suid_dumpable);
  311. task->pdeath_signal = 0;
  312. smp_wmb();
  313. }
  314. /* alter the thread keyring */
  315. if (new->fsuid != old->fsuid)
  316. key_fsuid_changed(task);
  317. if (new->fsgid != old->fsgid)
  318. key_fsgid_changed(task);
  319. /* do it
  320. * - What if a process setreuid()'s and this brings the
  321. * new uid over his NPROC rlimit? We can check this now
  322. * cheaply with the new uid cache, so if it matters
  323. * we should be checking for it. -DaveM
  324. */
  325. if (new->user != old->user)
  326. atomic_inc(&new->user->processes);
  327. rcu_assign_pointer(task->real_cred, new);
  328. rcu_assign_pointer(task->cred, new);
  329. if (new->user != old->user)
  330. atomic_dec(&old->user->processes);
  331. sched_switch_user(task);
  332. /* send notifications */
  333. if (new->uid != old->uid ||
  334. new->euid != old->euid ||
  335. new->suid != old->suid ||
  336. new->fsuid != old->fsuid)
  337. proc_id_connector(task, PROC_EVENT_UID);
  338. if (new->gid != old->gid ||
  339. new->egid != old->egid ||
  340. new->sgid != old->sgid ||
  341. new->fsgid != old->fsgid)
  342. proc_id_connector(task, PROC_EVENT_GID);
  343. /* release the old obj and subj refs both */
  344. put_cred(old);
  345. put_cred(old);
  346. return 0;
  347. }
  348. EXPORT_SYMBOL(commit_creds);
  349. /**
  350. * abort_creds - Discard a set of credentials and unlock the current task
  351. * @new: The credentials that were going to be applied
  352. *
  353. * Discard a set of credentials that were under construction and unlock the
  354. * current task.
  355. */
  356. void abort_creds(struct cred *new)
  357. {
  358. BUG_ON(atomic_read(&new->usage) < 1);
  359. put_cred(new);
  360. }
  361. EXPORT_SYMBOL(abort_creds);
  362. /**
  363. * override_creds - Override the current process's subjective credentials
  364. * @new: The credentials to be assigned
  365. *
  366. * Install a set of temporary override subjective credentials on the current
  367. * process, returning the old set for later reversion.
  368. */
  369. const struct cred *override_creds(const struct cred *new)
  370. {
  371. const struct cred *old = current->cred;
  372. rcu_assign_pointer(current->cred, get_cred(new));
  373. return old;
  374. }
  375. EXPORT_SYMBOL(override_creds);
  376. /**
  377. * revert_creds - Revert a temporary subjective credentials override
  378. * @old: The credentials to be restored
  379. *
  380. * Revert a temporary set of override subjective credentials to an old set,
  381. * discarding the override set.
  382. */
  383. void revert_creds(const struct cred *old)
  384. {
  385. const struct cred *override = current->cred;
  386. rcu_assign_pointer(current->cred, old);
  387. put_cred(override);
  388. }
  389. EXPORT_SYMBOL(revert_creds);
  390. /*
  391. * initialise the credentials stuff
  392. */
  393. void __init cred_init(void)
  394. {
  395. /* allocate a slab in which we can store credentials */
  396. cred_jar = kmem_cache_create("cred_jar", sizeof(struct cred),
  397. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  398. }