cred.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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_guard_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. int ret;
  236. mutex_init(&p->cred_guard_mutex);
  237. if (
  238. #ifdef CONFIG_KEYS
  239. !p->cred->thread_keyring &&
  240. #endif
  241. clone_flags & CLONE_THREAD
  242. ) {
  243. p->real_cred = get_cred(p->cred);
  244. get_cred(p->cred);
  245. atomic_inc(&p->cred->user->processes);
  246. return 0;
  247. }
  248. new = prepare_creds();
  249. if (!new)
  250. return -ENOMEM;
  251. if (clone_flags & CLONE_NEWUSER) {
  252. ret = create_user_ns(new);
  253. if (ret < 0)
  254. goto error_put;
  255. }
  256. #ifdef CONFIG_KEYS
  257. /* new threads get their own thread keyrings if their parent already
  258. * had one */
  259. if (new->thread_keyring) {
  260. key_put(new->thread_keyring);
  261. new->thread_keyring = NULL;
  262. if (clone_flags & CLONE_THREAD)
  263. install_thread_keyring_to_cred(new);
  264. }
  265. /* we share the process and session keyrings between all the threads in
  266. * a process - this is slightly icky as we violate COW credentials a
  267. * bit */
  268. if (!(clone_flags & CLONE_THREAD)) {
  269. tgcred = kmalloc(sizeof(*tgcred), GFP_KERNEL);
  270. if (!tgcred) {
  271. ret = -ENOMEM;
  272. goto error_put;
  273. }
  274. atomic_set(&tgcred->usage, 1);
  275. spin_lock_init(&tgcred->lock);
  276. tgcred->process_keyring = NULL;
  277. tgcred->session_keyring = key_get(new->tgcred->session_keyring);
  278. release_tgcred(new);
  279. new->tgcred = tgcred;
  280. }
  281. #endif
  282. atomic_inc(&new->user->processes);
  283. p->cred = p->real_cred = get_cred(new);
  284. return 0;
  285. error_put:
  286. put_cred(new);
  287. return ret;
  288. }
  289. /**
  290. * commit_creds - Install new credentials upon the current task
  291. * @new: The credentials to be assigned
  292. *
  293. * Install a new set of credentials to the current task, using RCU to replace
  294. * the old set. Both the objective and the subjective credentials pointers are
  295. * updated. This function may not be called if the subjective credentials are
  296. * in an overridden state.
  297. *
  298. * This function eats the caller's reference to the new credentials.
  299. *
  300. * Always returns 0 thus allowing this function to be tail-called at the end
  301. * of, say, sys_setgid().
  302. */
  303. int commit_creds(struct cred *new)
  304. {
  305. struct task_struct *task = current;
  306. const struct cred *old;
  307. BUG_ON(task->cred != task->real_cred);
  308. BUG_ON(atomic_read(&task->real_cred->usage) < 2);
  309. BUG_ON(atomic_read(&new->usage) < 1);
  310. old = task->real_cred;
  311. security_commit_creds(new, old);
  312. get_cred(new); /* we will require a ref for the subj creds too */
  313. /* dumpability changes */
  314. if (old->euid != new->euid ||
  315. old->egid != new->egid ||
  316. old->fsuid != new->fsuid ||
  317. old->fsgid != new->fsgid ||
  318. !cap_issubset(new->cap_permitted, old->cap_permitted)) {
  319. if (task->mm)
  320. set_dumpable(task->mm, suid_dumpable);
  321. task->pdeath_signal = 0;
  322. smp_wmb();
  323. }
  324. /* alter the thread keyring */
  325. if (new->fsuid != old->fsuid)
  326. key_fsuid_changed(task);
  327. if (new->fsgid != old->fsgid)
  328. key_fsgid_changed(task);
  329. /* do it
  330. * - What if a process setreuid()'s and this brings the
  331. * new uid over his NPROC rlimit? We can check this now
  332. * cheaply with the new uid cache, so if it matters
  333. * we should be checking for it. -DaveM
  334. */
  335. if (new->user != old->user)
  336. atomic_inc(&new->user->processes);
  337. rcu_assign_pointer(task->real_cred, new);
  338. rcu_assign_pointer(task->cred, new);
  339. if (new->user != old->user)
  340. atomic_dec(&old->user->processes);
  341. sched_switch_user(task);
  342. /* send notifications */
  343. if (new->uid != old->uid ||
  344. new->euid != old->euid ||
  345. new->suid != old->suid ||
  346. new->fsuid != old->fsuid)
  347. proc_id_connector(task, PROC_EVENT_UID);
  348. if (new->gid != old->gid ||
  349. new->egid != old->egid ||
  350. new->sgid != old->sgid ||
  351. new->fsgid != old->fsgid)
  352. proc_id_connector(task, PROC_EVENT_GID);
  353. /* release the old obj and subj refs both */
  354. put_cred(old);
  355. put_cred(old);
  356. return 0;
  357. }
  358. EXPORT_SYMBOL(commit_creds);
  359. /**
  360. * abort_creds - Discard a set of credentials and unlock the current task
  361. * @new: The credentials that were going to be applied
  362. *
  363. * Discard a set of credentials that were under construction and unlock the
  364. * current task.
  365. */
  366. void abort_creds(struct cred *new)
  367. {
  368. BUG_ON(atomic_read(&new->usage) < 1);
  369. put_cred(new);
  370. }
  371. EXPORT_SYMBOL(abort_creds);
  372. /**
  373. * override_creds - Override the current process's subjective credentials
  374. * @new: The credentials to be assigned
  375. *
  376. * Install a set of temporary override subjective credentials on the current
  377. * process, returning the old set for later reversion.
  378. */
  379. const struct cred *override_creds(const struct cred *new)
  380. {
  381. const struct cred *old = current->cred;
  382. rcu_assign_pointer(current->cred, get_cred(new));
  383. return old;
  384. }
  385. EXPORT_SYMBOL(override_creds);
  386. /**
  387. * revert_creds - Revert a temporary subjective credentials override
  388. * @old: The credentials to be restored
  389. *
  390. * Revert a temporary set of override subjective credentials to an old set,
  391. * discarding the override set.
  392. */
  393. void revert_creds(const struct cred *old)
  394. {
  395. const struct cred *override = current->cred;
  396. rcu_assign_pointer(current->cred, old);
  397. put_cred(override);
  398. }
  399. EXPORT_SYMBOL(revert_creds);
  400. /*
  401. * initialise the credentials stuff
  402. */
  403. void __init cred_init(void)
  404. {
  405. /* allocate a slab in which we can store credentials */
  406. cred_jar = kmem_cache_create("cred_jar", sizeof(struct cred),
  407. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  408. }
  409. /**
  410. * prepare_kernel_cred - Prepare a set of credentials for a kernel service
  411. * @daemon: A userspace daemon to be used as a reference
  412. *
  413. * Prepare a set of credentials for a kernel service. This can then be used to
  414. * override a task's own credentials so that work can be done on behalf of that
  415. * task that requires a different subjective context.
  416. *
  417. * @daemon is used to provide a base for the security record, but can be NULL.
  418. * If @daemon is supplied, then the security data will be derived from that;
  419. * otherwise they'll be set to 0 and no groups, full capabilities and no keys.
  420. *
  421. * The caller may change these controls afterwards if desired.
  422. *
  423. * Returns the new credentials or NULL if out of memory.
  424. *
  425. * Does not take, and does not return holding current->cred_replace_mutex.
  426. */
  427. struct cred *prepare_kernel_cred(struct task_struct *daemon)
  428. {
  429. const struct cred *old;
  430. struct cred *new;
  431. new = kmem_cache_alloc(cred_jar, GFP_KERNEL);
  432. if (!new)
  433. return NULL;
  434. if (daemon)
  435. old = get_task_cred(daemon);
  436. else
  437. old = get_cred(&init_cred);
  438. *new = *old;
  439. get_uid(new->user);
  440. get_group_info(new->group_info);
  441. #ifdef CONFIG_KEYS
  442. atomic_inc(&init_tgcred.usage);
  443. new->tgcred = &init_tgcred;
  444. new->request_key_auth = NULL;
  445. new->thread_keyring = NULL;
  446. new->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
  447. #endif
  448. #ifdef CONFIG_SECURITY
  449. new->security = NULL;
  450. #endif
  451. if (security_prepare_creds(new, old, GFP_KERNEL) < 0)
  452. goto error;
  453. atomic_set(&new->usage, 1);
  454. put_cred(old);
  455. return new;
  456. error:
  457. put_cred(new);
  458. put_cred(old);
  459. return NULL;
  460. }
  461. EXPORT_SYMBOL(prepare_kernel_cred);
  462. /**
  463. * set_security_override - Set the security ID in a set of credentials
  464. * @new: The credentials to alter
  465. * @secid: The LSM security ID to set
  466. *
  467. * Set the LSM security ID in a set of credentials so that the subjective
  468. * security is overridden when an alternative set of credentials is used.
  469. */
  470. int set_security_override(struct cred *new, u32 secid)
  471. {
  472. return security_kernel_act_as(new, secid);
  473. }
  474. EXPORT_SYMBOL(set_security_override);
  475. /**
  476. * set_security_override_from_ctx - Set the security ID in a set of credentials
  477. * @new: The credentials to alter
  478. * @secctx: The LSM security context to generate the security ID from.
  479. *
  480. * Set the LSM security ID in a set of credentials so that the subjective
  481. * security is overridden when an alternative set of credentials is used. The
  482. * security ID is specified in string form as a security context to be
  483. * interpreted by the LSM.
  484. */
  485. int set_security_override_from_ctx(struct cred *new, const char *secctx)
  486. {
  487. u32 secid;
  488. int ret;
  489. ret = security_secctx_to_secid(secctx, strlen(secctx), &secid);
  490. if (ret < 0)
  491. return ret;
  492. return set_security_override(new, secid);
  493. }
  494. EXPORT_SYMBOL(set_security_override_from_ctx);
  495. /**
  496. * set_create_files_as - Set the LSM file create context in a set of credentials
  497. * @new: The credentials to alter
  498. * @inode: The inode to take the context from
  499. *
  500. * Change the LSM file creation context in a set of credentials to be the same
  501. * as the object context of the specified inode, so that the new inodes have
  502. * the same MAC context as that inode.
  503. */
  504. int set_create_files_as(struct cred *new, struct inode *inode)
  505. {
  506. new->fsuid = inode->i_uid;
  507. new->fsgid = inode->i_gid;
  508. return security_kernel_create_files_as(new, inode);
  509. }
  510. EXPORT_SYMBOL(set_create_files_as);