cred.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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. int ret;
  236. mutex_init(&p->cred_exec_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. set_dumpable(task->mm, suid_dumpable);
  320. task->pdeath_signal = 0;
  321. smp_wmb();
  322. }
  323. /* alter the thread keyring */
  324. if (new->fsuid != old->fsuid)
  325. key_fsuid_changed(task);
  326. if (new->fsgid != old->fsgid)
  327. key_fsgid_changed(task);
  328. /* do it
  329. * - What if a process setreuid()'s and this brings the
  330. * new uid over his NPROC rlimit? We can check this now
  331. * cheaply with the new uid cache, so if it matters
  332. * we should be checking for it. -DaveM
  333. */
  334. if (new->user != old->user)
  335. atomic_inc(&new->user->processes);
  336. rcu_assign_pointer(task->real_cred, new);
  337. rcu_assign_pointer(task->cred, new);
  338. if (new->user != old->user)
  339. atomic_dec(&old->user->processes);
  340. sched_switch_user(task);
  341. /* send notifications */
  342. if (new->uid != old->uid ||
  343. new->euid != old->euid ||
  344. new->suid != old->suid ||
  345. new->fsuid != old->fsuid)
  346. proc_id_connector(task, PROC_EVENT_UID);
  347. if (new->gid != old->gid ||
  348. new->egid != old->egid ||
  349. new->sgid != old->sgid ||
  350. new->fsgid != old->fsgid)
  351. proc_id_connector(task, PROC_EVENT_GID);
  352. /* release the old obj and subj refs both */
  353. put_cred(old);
  354. put_cred(old);
  355. return 0;
  356. }
  357. EXPORT_SYMBOL(commit_creds);
  358. /**
  359. * abort_creds - Discard a set of credentials and unlock the current task
  360. * @new: The credentials that were going to be applied
  361. *
  362. * Discard a set of credentials that were under construction and unlock the
  363. * current task.
  364. */
  365. void abort_creds(struct cred *new)
  366. {
  367. BUG_ON(atomic_read(&new->usage) < 1);
  368. put_cred(new);
  369. }
  370. EXPORT_SYMBOL(abort_creds);
  371. /**
  372. * override_creds - Override the current process's subjective credentials
  373. * @new: The credentials to be assigned
  374. *
  375. * Install a set of temporary override subjective credentials on the current
  376. * process, returning the old set for later reversion.
  377. */
  378. const struct cred *override_creds(const struct cred *new)
  379. {
  380. const struct cred *old = current->cred;
  381. rcu_assign_pointer(current->cred, get_cred(new));
  382. return old;
  383. }
  384. EXPORT_SYMBOL(override_creds);
  385. /**
  386. * revert_creds - Revert a temporary subjective credentials override
  387. * @old: The credentials to be restored
  388. *
  389. * Revert a temporary set of override subjective credentials to an old set,
  390. * discarding the override set.
  391. */
  392. void revert_creds(const struct cred *old)
  393. {
  394. const struct cred *override = current->cred;
  395. rcu_assign_pointer(current->cred, old);
  396. put_cred(override);
  397. }
  398. EXPORT_SYMBOL(revert_creds);
  399. /*
  400. * initialise the credentials stuff
  401. */
  402. void __init cred_init(void)
  403. {
  404. /* allocate a slab in which we can store credentials */
  405. cred_jar = kmem_cache_create("cred_jar", sizeof(struct cred),
  406. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  407. }
  408. /**
  409. * prepare_kernel_cred - Prepare a set of credentials for a kernel service
  410. * @daemon: A userspace daemon to be used as a reference
  411. *
  412. * Prepare a set of credentials for a kernel service. This can then be used to
  413. * override a task's own credentials so that work can be done on behalf of that
  414. * task that requires a different subjective context.
  415. *
  416. * @daemon is used to provide a base for the security record, but can be NULL.
  417. * If @daemon is supplied, then the security data will be derived from that;
  418. * otherwise they'll be set to 0 and no groups, full capabilities and no keys.
  419. *
  420. * The caller may change these controls afterwards if desired.
  421. *
  422. * Returns the new credentials or NULL if out of memory.
  423. *
  424. * Does not take, and does not return holding current->cred_replace_mutex.
  425. */
  426. struct cred *prepare_kernel_cred(struct task_struct *daemon)
  427. {
  428. const struct cred *old;
  429. struct cred *new;
  430. new = kmem_cache_alloc(cred_jar, GFP_KERNEL);
  431. if (!new)
  432. return NULL;
  433. if (daemon)
  434. old = get_task_cred(daemon);
  435. else
  436. old = get_cred(&init_cred);
  437. get_uid(new->user);
  438. get_group_info(new->group_info);
  439. #ifdef CONFIG_KEYS
  440. atomic_inc(&init_tgcred.usage);
  441. new->tgcred = &init_tgcred;
  442. new->request_key_auth = NULL;
  443. new->thread_keyring = NULL;
  444. new->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
  445. #endif
  446. #ifdef CONFIG_SECURITY
  447. new->security = NULL;
  448. #endif
  449. if (security_prepare_creds(new, old, GFP_KERNEL) < 0)
  450. goto error;
  451. atomic_set(&new->usage, 1);
  452. put_cred(old);
  453. return new;
  454. error:
  455. put_cred(new);
  456. return NULL;
  457. }
  458. EXPORT_SYMBOL(prepare_kernel_cred);
  459. /**
  460. * set_security_override - Set the security ID in a set of credentials
  461. * @new: The credentials to alter
  462. * @secid: The LSM security ID to set
  463. *
  464. * Set the LSM security ID in a set of credentials so that the subjective
  465. * security is overridden when an alternative set of credentials is used.
  466. */
  467. int set_security_override(struct cred *new, u32 secid)
  468. {
  469. return security_kernel_act_as(new, secid);
  470. }
  471. EXPORT_SYMBOL(set_security_override);
  472. /**
  473. * set_security_override_from_ctx - Set the security ID in a set of credentials
  474. * @new: The credentials to alter
  475. * @secctx: The LSM security context to generate the security ID from.
  476. *
  477. * Set the LSM security ID in a set of credentials so that the subjective
  478. * security is overridden when an alternative set of credentials is used. The
  479. * security ID is specified in string form as a security context to be
  480. * interpreted by the LSM.
  481. */
  482. int set_security_override_from_ctx(struct cred *new, const char *secctx)
  483. {
  484. u32 secid;
  485. int ret;
  486. ret = security_secctx_to_secid(secctx, strlen(secctx), &secid);
  487. if (ret < 0)
  488. return ret;
  489. return set_security_override(new, secid);
  490. }
  491. EXPORT_SYMBOL(set_security_override_from_ctx);
  492. /**
  493. * set_create_files_as - Set the LSM file create context in a set of credentials
  494. * @new: The credentials to alter
  495. * @inode: The inode to take the context from
  496. *
  497. * Change the LSM file creation context in a set of credentials to be the same
  498. * as the object context of the specified inode, so that the new inodes have
  499. * the same MAC context as that inode.
  500. */
  501. int set_create_files_as(struct cred *new, struct inode *inode)
  502. {
  503. new->fsuid = inode->i_uid;
  504. new->fsgid = inode->i_gid;
  505. return security_kernel_create_files_as(new, inode);
  506. }
  507. EXPORT_SYMBOL(set_create_files_as);