cred.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. /* Task credentials management - see Documentation/security/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/export.h>
  12. #include <linux/cred.h>
  13. #include <linux/slab.h>
  14. #include <linux/sched.h>
  15. #include <linux/key.h>
  16. #include <linux/keyctl.h>
  17. #include <linux/init_task.h>
  18. #include <linux/security.h>
  19. #include <linux/binfmts.h>
  20. #include <linux/cn_proc.h>
  21. #if 0
  22. #define kdebug(FMT, ...) \
  23. printk("[%-5.5s%5u] "FMT"\n", current->comm, current->pid ,##__VA_ARGS__)
  24. #else
  25. #define kdebug(FMT, ...) \
  26. no_printk("[%-5.5s%5u] "FMT"\n", current->comm, current->pid ,##__VA_ARGS__)
  27. #endif
  28. static struct kmem_cache *cred_jar;
  29. /*
  30. * The initial credentials for the initial task
  31. */
  32. struct cred init_cred = {
  33. .usage = ATOMIC_INIT(4),
  34. #ifdef CONFIG_DEBUG_CREDENTIALS
  35. .subscribers = ATOMIC_INIT(2),
  36. .magic = CRED_MAGIC,
  37. #endif
  38. .uid = GLOBAL_ROOT_UID,
  39. .gid = GLOBAL_ROOT_GID,
  40. .suid = GLOBAL_ROOT_UID,
  41. .sgid = GLOBAL_ROOT_GID,
  42. .euid = GLOBAL_ROOT_UID,
  43. .egid = GLOBAL_ROOT_GID,
  44. .fsuid = GLOBAL_ROOT_UID,
  45. .fsgid = GLOBAL_ROOT_GID,
  46. .securebits = SECUREBITS_DEFAULT,
  47. .cap_inheritable = CAP_EMPTY_SET,
  48. .cap_permitted = CAP_FULL_SET,
  49. .cap_effective = CAP_FULL_SET,
  50. .cap_bset = CAP_FULL_SET,
  51. .user = INIT_USER,
  52. .user_ns = &init_user_ns,
  53. .group_info = &init_groups,
  54. };
  55. static inline void set_cred_subscribers(struct cred *cred, int n)
  56. {
  57. #ifdef CONFIG_DEBUG_CREDENTIALS
  58. atomic_set(&cred->subscribers, n);
  59. #endif
  60. }
  61. static inline int read_cred_subscribers(const struct cred *cred)
  62. {
  63. #ifdef CONFIG_DEBUG_CREDENTIALS
  64. return atomic_read(&cred->subscribers);
  65. #else
  66. return 0;
  67. #endif
  68. }
  69. static inline void alter_cred_subscribers(const struct cred *_cred, int n)
  70. {
  71. #ifdef CONFIG_DEBUG_CREDENTIALS
  72. struct cred *cred = (struct cred *) _cred;
  73. atomic_add(n, &cred->subscribers);
  74. #endif
  75. }
  76. /*
  77. * The RCU callback to actually dispose of a set of credentials
  78. */
  79. static void put_cred_rcu(struct rcu_head *rcu)
  80. {
  81. struct cred *cred = container_of(rcu, struct cred, rcu);
  82. kdebug("put_cred_rcu(%p)", cred);
  83. #ifdef CONFIG_DEBUG_CREDENTIALS
  84. if (cred->magic != CRED_MAGIC_DEAD ||
  85. atomic_read(&cred->usage) != 0 ||
  86. read_cred_subscribers(cred) != 0)
  87. panic("CRED: put_cred_rcu() sees %p with"
  88. " mag %x, put %p, usage %d, subscr %d\n",
  89. cred, cred->magic, cred->put_addr,
  90. atomic_read(&cred->usage),
  91. read_cred_subscribers(cred));
  92. #else
  93. if (atomic_read(&cred->usage) != 0)
  94. panic("CRED: put_cred_rcu() sees %p with usage %d\n",
  95. cred, atomic_read(&cred->usage));
  96. #endif
  97. security_cred_free(cred);
  98. key_put(cred->session_keyring);
  99. key_put(cred->process_keyring);
  100. key_put(cred->thread_keyring);
  101. key_put(cred->request_key_auth);
  102. if (cred->group_info)
  103. put_group_info(cred->group_info);
  104. free_uid(cred->user);
  105. put_user_ns(cred->user_ns);
  106. kmem_cache_free(cred_jar, cred);
  107. }
  108. /**
  109. * __put_cred - Destroy a set of credentials
  110. * @cred: The record to release
  111. *
  112. * Destroy a set of credentials on which no references remain.
  113. */
  114. void __put_cred(struct cred *cred)
  115. {
  116. kdebug("__put_cred(%p{%d,%d})", cred,
  117. atomic_read(&cred->usage),
  118. read_cred_subscribers(cred));
  119. BUG_ON(atomic_read(&cred->usage) != 0);
  120. #ifdef CONFIG_DEBUG_CREDENTIALS
  121. BUG_ON(read_cred_subscribers(cred) != 0);
  122. cred->magic = CRED_MAGIC_DEAD;
  123. cred->put_addr = __builtin_return_address(0);
  124. #endif
  125. BUG_ON(cred == current->cred);
  126. BUG_ON(cred == current->real_cred);
  127. call_rcu(&cred->rcu, put_cred_rcu);
  128. }
  129. EXPORT_SYMBOL(__put_cred);
  130. /*
  131. * Clean up a task's credentials when it exits
  132. */
  133. void exit_creds(struct task_struct *tsk)
  134. {
  135. struct cred *cred;
  136. kdebug("exit_creds(%u,%p,%p,{%d,%d})", tsk->pid, tsk->real_cred, tsk->cred,
  137. atomic_read(&tsk->cred->usage),
  138. read_cred_subscribers(tsk->cred));
  139. cred = (struct cred *) tsk->real_cred;
  140. tsk->real_cred = NULL;
  141. validate_creds(cred);
  142. alter_cred_subscribers(cred, -1);
  143. put_cred(cred);
  144. cred = (struct cred *) tsk->cred;
  145. tsk->cred = NULL;
  146. validate_creds(cred);
  147. alter_cred_subscribers(cred, -1);
  148. put_cred(cred);
  149. }
  150. /**
  151. * get_task_cred - Get another task's objective credentials
  152. * @task: The task to query
  153. *
  154. * Get the objective credentials of a task, pinning them so that they can't go
  155. * away. Accessing a task's credentials directly is not permitted.
  156. *
  157. * The caller must also make sure task doesn't get deleted, either by holding a
  158. * ref on task or by holding tasklist_lock to prevent it from being unlinked.
  159. */
  160. const struct cred *get_task_cred(struct task_struct *task)
  161. {
  162. const struct cred *cred;
  163. rcu_read_lock();
  164. do {
  165. cred = __task_cred((task));
  166. BUG_ON(!cred);
  167. } while (!atomic_inc_not_zero(&((struct cred *)cred)->usage));
  168. rcu_read_unlock();
  169. return cred;
  170. }
  171. /*
  172. * Allocate blank credentials, such that the credentials can be filled in at a
  173. * later date without risk of ENOMEM.
  174. */
  175. struct cred *cred_alloc_blank(void)
  176. {
  177. struct cred *new;
  178. new = kmem_cache_zalloc(cred_jar, GFP_KERNEL);
  179. if (!new)
  180. return NULL;
  181. atomic_set(&new->usage, 1);
  182. #ifdef CONFIG_DEBUG_CREDENTIALS
  183. new->magic = CRED_MAGIC;
  184. #endif
  185. if (security_cred_alloc_blank(new, GFP_KERNEL) < 0)
  186. goto error;
  187. return new;
  188. error:
  189. abort_creds(new);
  190. return NULL;
  191. }
  192. /**
  193. * prepare_creds - Prepare a new set of credentials for modification
  194. *
  195. * Prepare a new set of task credentials for modification. A task's creds
  196. * shouldn't generally be modified directly, therefore this function is used to
  197. * prepare a new copy, which the caller then modifies and then commits by
  198. * calling commit_creds().
  199. *
  200. * Preparation involves making a copy of the objective creds for modification.
  201. *
  202. * Returns a pointer to the new creds-to-be if successful, NULL otherwise.
  203. *
  204. * Call commit_creds() or abort_creds() to clean up.
  205. */
  206. struct cred *prepare_creds(void)
  207. {
  208. struct task_struct *task = current;
  209. const struct cred *old;
  210. struct cred *new;
  211. validate_process_creds();
  212. new = kmem_cache_alloc(cred_jar, GFP_KERNEL);
  213. if (!new)
  214. return NULL;
  215. kdebug("prepare_creds() alloc %p", new);
  216. old = task->cred;
  217. memcpy(new, old, sizeof(struct cred));
  218. atomic_set(&new->usage, 1);
  219. set_cred_subscribers(new, 0);
  220. get_group_info(new->group_info);
  221. get_uid(new->user);
  222. get_user_ns(new->user_ns);
  223. #ifdef CONFIG_KEYS
  224. key_get(new->session_keyring);
  225. key_get(new->process_keyring);
  226. key_get(new->thread_keyring);
  227. key_get(new->request_key_auth);
  228. #endif
  229. #ifdef CONFIG_SECURITY
  230. new->security = NULL;
  231. #endif
  232. if (security_prepare_creds(new, old, GFP_KERNEL) < 0)
  233. goto error;
  234. validate_creds(new);
  235. return new;
  236. error:
  237. abort_creds(new);
  238. return NULL;
  239. }
  240. EXPORT_SYMBOL(prepare_creds);
  241. /*
  242. * Prepare credentials for current to perform an execve()
  243. * - The caller must hold ->cred_guard_mutex
  244. */
  245. struct cred *prepare_exec_creds(void)
  246. {
  247. struct cred *new;
  248. new = prepare_creds();
  249. if (!new)
  250. return new;
  251. #ifdef CONFIG_KEYS
  252. /* newly exec'd tasks don't get a thread keyring */
  253. key_put(new->thread_keyring);
  254. new->thread_keyring = NULL;
  255. /* inherit the session keyring; new process keyring */
  256. key_put(new->process_keyring);
  257. new->process_keyring = NULL;
  258. #endif
  259. return new;
  260. }
  261. /*
  262. * Copy credentials for the new process created by fork()
  263. *
  264. * We share if we can, but under some circumstances we have to generate a new
  265. * set.
  266. *
  267. * The new process gets the current process's subjective credentials as its
  268. * objective and subjective credentials
  269. */
  270. int copy_creds(struct task_struct *p, unsigned long clone_flags)
  271. {
  272. struct cred *new;
  273. int ret;
  274. if (
  275. #ifdef CONFIG_KEYS
  276. !p->cred->thread_keyring &&
  277. #endif
  278. clone_flags & CLONE_THREAD
  279. ) {
  280. p->real_cred = get_cred(p->cred);
  281. get_cred(p->cred);
  282. alter_cred_subscribers(p->cred, 2);
  283. kdebug("share_creds(%p{%d,%d})",
  284. p->cred, atomic_read(&p->cred->usage),
  285. read_cred_subscribers(p->cred));
  286. atomic_inc(&p->cred->user->processes);
  287. return 0;
  288. }
  289. new = prepare_creds();
  290. if (!new)
  291. return -ENOMEM;
  292. if (clone_flags & CLONE_NEWUSER) {
  293. ret = create_user_ns(new);
  294. if (ret < 0)
  295. goto error_put;
  296. }
  297. #ifdef CONFIG_KEYS
  298. /* new threads get their own thread keyrings if their parent already
  299. * had one */
  300. if (new->thread_keyring) {
  301. key_put(new->thread_keyring);
  302. new->thread_keyring = NULL;
  303. if (clone_flags & CLONE_THREAD)
  304. install_thread_keyring_to_cred(new);
  305. }
  306. /* The process keyring is only shared between the threads in a process;
  307. * anything outside of those threads doesn't inherit.
  308. */
  309. if (!(clone_flags & CLONE_THREAD)) {
  310. key_put(new->process_keyring);
  311. new->process_keyring = NULL;
  312. }
  313. #endif
  314. atomic_inc(&new->user->processes);
  315. p->cred = p->real_cred = get_cred(new);
  316. alter_cred_subscribers(new, 2);
  317. validate_creds(new);
  318. return 0;
  319. error_put:
  320. put_cred(new);
  321. return ret;
  322. }
  323. static bool cred_cap_issubset(const struct cred *set, const struct cred *subset)
  324. {
  325. const struct user_namespace *set_ns = set->user_ns;
  326. const struct user_namespace *subset_ns = subset->user_ns;
  327. /* If the two credentials are in the same user namespace see if
  328. * the capabilities of subset are a subset of set.
  329. */
  330. if (set_ns == subset_ns)
  331. return cap_issubset(subset->cap_permitted, set->cap_permitted);
  332. /* The credentials are in a different user namespaces
  333. * therefore one is a subset of the other only if a set is an
  334. * ancestor of subset and set->euid is owner of subset or one
  335. * of subsets ancestors.
  336. */
  337. for (;subset_ns != &init_user_ns; subset_ns = subset_ns->parent) {
  338. if ((set_ns == subset_ns->parent) &&
  339. uid_eq(subset_ns->owner, set->euid))
  340. return true;
  341. }
  342. return false;
  343. }
  344. /**
  345. * commit_creds - Install new credentials upon the current task
  346. * @new: The credentials to be assigned
  347. *
  348. * Install a new set of credentials to the current task, using RCU to replace
  349. * the old set. Both the objective and the subjective credentials pointers are
  350. * updated. This function may not be called if the subjective credentials are
  351. * in an overridden state.
  352. *
  353. * This function eats the caller's reference to the new credentials.
  354. *
  355. * Always returns 0 thus allowing this function to be tail-called at the end
  356. * of, say, sys_setgid().
  357. */
  358. int commit_creds(struct cred *new)
  359. {
  360. struct task_struct *task = current;
  361. const struct cred *old = task->real_cred;
  362. kdebug("commit_creds(%p{%d,%d})", new,
  363. atomic_read(&new->usage),
  364. read_cred_subscribers(new));
  365. BUG_ON(task->cred != old);
  366. #ifdef CONFIG_DEBUG_CREDENTIALS
  367. BUG_ON(read_cred_subscribers(old) < 2);
  368. validate_creds(old);
  369. validate_creds(new);
  370. #endif
  371. BUG_ON(atomic_read(&new->usage) < 1);
  372. get_cred(new); /* we will require a ref for the subj creds too */
  373. /* dumpability changes */
  374. if (!uid_eq(old->euid, new->euid) ||
  375. !gid_eq(old->egid, new->egid) ||
  376. !uid_eq(old->fsuid, new->fsuid) ||
  377. !gid_eq(old->fsgid, new->fsgid) ||
  378. !cred_cap_issubset(old, new)) {
  379. if (task->mm)
  380. set_dumpable(task->mm, suid_dumpable);
  381. task->pdeath_signal = 0;
  382. smp_wmb();
  383. }
  384. /* alter the thread keyring */
  385. if (!uid_eq(new->fsuid, old->fsuid))
  386. key_fsuid_changed(task);
  387. if (!gid_eq(new->fsgid, old->fsgid))
  388. key_fsgid_changed(task);
  389. /* do it
  390. * RLIMIT_NPROC limits on user->processes have already been checked
  391. * in set_user().
  392. */
  393. alter_cred_subscribers(new, 2);
  394. if (new->user != old->user)
  395. atomic_inc(&new->user->processes);
  396. rcu_assign_pointer(task->real_cred, new);
  397. rcu_assign_pointer(task->cred, new);
  398. if (new->user != old->user)
  399. atomic_dec(&old->user->processes);
  400. alter_cred_subscribers(old, -2);
  401. /* send notifications */
  402. if (!uid_eq(new->uid, old->uid) ||
  403. !uid_eq(new->euid, old->euid) ||
  404. !uid_eq(new->suid, old->suid) ||
  405. !uid_eq(new->fsuid, old->fsuid))
  406. proc_id_connector(task, PROC_EVENT_UID);
  407. if (!gid_eq(new->gid, old->gid) ||
  408. !gid_eq(new->egid, old->egid) ||
  409. !gid_eq(new->sgid, old->sgid) ||
  410. !gid_eq(new->fsgid, old->fsgid))
  411. proc_id_connector(task, PROC_EVENT_GID);
  412. /* release the old obj and subj refs both */
  413. put_cred(old);
  414. put_cred(old);
  415. return 0;
  416. }
  417. EXPORT_SYMBOL(commit_creds);
  418. /**
  419. * abort_creds - Discard a set of credentials and unlock the current task
  420. * @new: The credentials that were going to be applied
  421. *
  422. * Discard a set of credentials that were under construction and unlock the
  423. * current task.
  424. */
  425. void abort_creds(struct cred *new)
  426. {
  427. kdebug("abort_creds(%p{%d,%d})", new,
  428. atomic_read(&new->usage),
  429. read_cred_subscribers(new));
  430. #ifdef CONFIG_DEBUG_CREDENTIALS
  431. BUG_ON(read_cred_subscribers(new) != 0);
  432. #endif
  433. BUG_ON(atomic_read(&new->usage) < 1);
  434. put_cred(new);
  435. }
  436. EXPORT_SYMBOL(abort_creds);
  437. /**
  438. * override_creds - Override the current process's subjective credentials
  439. * @new: The credentials to be assigned
  440. *
  441. * Install a set of temporary override subjective credentials on the current
  442. * process, returning the old set for later reversion.
  443. */
  444. const struct cred *override_creds(const struct cred *new)
  445. {
  446. const struct cred *old = current->cred;
  447. kdebug("override_creds(%p{%d,%d})", new,
  448. atomic_read(&new->usage),
  449. read_cred_subscribers(new));
  450. validate_creds(old);
  451. validate_creds(new);
  452. get_cred(new);
  453. alter_cred_subscribers(new, 1);
  454. rcu_assign_pointer(current->cred, new);
  455. alter_cred_subscribers(old, -1);
  456. kdebug("override_creds() = %p{%d,%d}", old,
  457. atomic_read(&old->usage),
  458. read_cred_subscribers(old));
  459. return old;
  460. }
  461. EXPORT_SYMBOL(override_creds);
  462. /**
  463. * revert_creds - Revert a temporary subjective credentials override
  464. * @old: The credentials to be restored
  465. *
  466. * Revert a temporary set of override subjective credentials to an old set,
  467. * discarding the override set.
  468. */
  469. void revert_creds(const struct cred *old)
  470. {
  471. const struct cred *override = current->cred;
  472. kdebug("revert_creds(%p{%d,%d})", old,
  473. atomic_read(&old->usage),
  474. read_cred_subscribers(old));
  475. validate_creds(old);
  476. validate_creds(override);
  477. alter_cred_subscribers(old, 1);
  478. rcu_assign_pointer(current->cred, old);
  479. alter_cred_subscribers(override, -1);
  480. put_cred(override);
  481. }
  482. EXPORT_SYMBOL(revert_creds);
  483. /*
  484. * initialise the credentials stuff
  485. */
  486. void __init cred_init(void)
  487. {
  488. /* allocate a slab in which we can store credentials */
  489. cred_jar = kmem_cache_create("cred_jar", sizeof(struct cred),
  490. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  491. }
  492. /**
  493. * prepare_kernel_cred - Prepare a set of credentials for a kernel service
  494. * @daemon: A userspace daemon to be used as a reference
  495. *
  496. * Prepare a set of credentials for a kernel service. This can then be used to
  497. * override a task's own credentials so that work can be done on behalf of that
  498. * task that requires a different subjective context.
  499. *
  500. * @daemon is used to provide a base for the security record, but can be NULL.
  501. * If @daemon is supplied, then the security data will be derived from that;
  502. * otherwise they'll be set to 0 and no groups, full capabilities and no keys.
  503. *
  504. * The caller may change these controls afterwards if desired.
  505. *
  506. * Returns the new credentials or NULL if out of memory.
  507. *
  508. * Does not take, and does not return holding current->cred_replace_mutex.
  509. */
  510. struct cred *prepare_kernel_cred(struct task_struct *daemon)
  511. {
  512. const struct cred *old;
  513. struct cred *new;
  514. new = kmem_cache_alloc(cred_jar, GFP_KERNEL);
  515. if (!new)
  516. return NULL;
  517. kdebug("prepare_kernel_cred() alloc %p", new);
  518. if (daemon)
  519. old = get_task_cred(daemon);
  520. else
  521. old = get_cred(&init_cred);
  522. validate_creds(old);
  523. *new = *old;
  524. atomic_set(&new->usage, 1);
  525. set_cred_subscribers(new, 0);
  526. get_uid(new->user);
  527. get_user_ns(new->user_ns);
  528. get_group_info(new->group_info);
  529. #ifdef CONFIG_KEYS
  530. new->session_keyring = NULL;
  531. new->process_keyring = NULL;
  532. new->thread_keyring = NULL;
  533. new->request_key_auth = NULL;
  534. new->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
  535. #endif
  536. #ifdef CONFIG_SECURITY
  537. new->security = NULL;
  538. #endif
  539. if (security_prepare_creds(new, old, GFP_KERNEL) < 0)
  540. goto error;
  541. put_cred(old);
  542. validate_creds(new);
  543. return new;
  544. error:
  545. put_cred(new);
  546. put_cred(old);
  547. return NULL;
  548. }
  549. EXPORT_SYMBOL(prepare_kernel_cred);
  550. /**
  551. * set_security_override - Set the security ID in a set of credentials
  552. * @new: The credentials to alter
  553. * @secid: The LSM security ID to set
  554. *
  555. * Set the LSM security ID in a set of credentials so that the subjective
  556. * security is overridden when an alternative set of credentials is used.
  557. */
  558. int set_security_override(struct cred *new, u32 secid)
  559. {
  560. return security_kernel_act_as(new, secid);
  561. }
  562. EXPORT_SYMBOL(set_security_override);
  563. /**
  564. * set_security_override_from_ctx - Set the security ID in a set of credentials
  565. * @new: The credentials to alter
  566. * @secctx: The LSM security context to generate the security ID from.
  567. *
  568. * Set the LSM security ID in a set of credentials so that the subjective
  569. * security is overridden when an alternative set of credentials is used. The
  570. * security ID is specified in string form as a security context to be
  571. * interpreted by the LSM.
  572. */
  573. int set_security_override_from_ctx(struct cred *new, const char *secctx)
  574. {
  575. u32 secid;
  576. int ret;
  577. ret = security_secctx_to_secid(secctx, strlen(secctx), &secid);
  578. if (ret < 0)
  579. return ret;
  580. return set_security_override(new, secid);
  581. }
  582. EXPORT_SYMBOL(set_security_override_from_ctx);
  583. /**
  584. * set_create_files_as - Set the LSM file create context in a set of credentials
  585. * @new: The credentials to alter
  586. * @inode: The inode to take the context from
  587. *
  588. * Change the LSM file creation context in a set of credentials to be the same
  589. * as the object context of the specified inode, so that the new inodes have
  590. * the same MAC context as that inode.
  591. */
  592. int set_create_files_as(struct cred *new, struct inode *inode)
  593. {
  594. new->fsuid = inode->i_uid;
  595. new->fsgid = inode->i_gid;
  596. return security_kernel_create_files_as(new, inode);
  597. }
  598. EXPORT_SYMBOL(set_create_files_as);
  599. #ifdef CONFIG_DEBUG_CREDENTIALS
  600. bool creds_are_invalid(const struct cred *cred)
  601. {
  602. if (cred->magic != CRED_MAGIC)
  603. return true;
  604. #ifdef CONFIG_SECURITY_SELINUX
  605. /*
  606. * cred->security == NULL if security_cred_alloc_blank() or
  607. * security_prepare_creds() returned an error.
  608. */
  609. if (selinux_is_enabled() && cred->security) {
  610. if ((unsigned long) cred->security < PAGE_SIZE)
  611. return true;
  612. if ((*(u32 *)cred->security & 0xffffff00) ==
  613. (POISON_FREE << 24 | POISON_FREE << 16 | POISON_FREE << 8))
  614. return true;
  615. }
  616. #endif
  617. return false;
  618. }
  619. EXPORT_SYMBOL(creds_are_invalid);
  620. /*
  621. * dump invalid credentials
  622. */
  623. static void dump_invalid_creds(const struct cred *cred, const char *label,
  624. const struct task_struct *tsk)
  625. {
  626. printk(KERN_ERR "CRED: %s credentials: %p %s%s%s\n",
  627. label, cred,
  628. cred == &init_cred ? "[init]" : "",
  629. cred == tsk->real_cred ? "[real]" : "",
  630. cred == tsk->cred ? "[eff]" : "");
  631. printk(KERN_ERR "CRED: ->magic=%x, put_addr=%p\n",
  632. cred->magic, cred->put_addr);
  633. printk(KERN_ERR "CRED: ->usage=%d, subscr=%d\n",
  634. atomic_read(&cred->usage),
  635. read_cred_subscribers(cred));
  636. printk(KERN_ERR "CRED: ->*uid = { %d,%d,%d,%d }\n",
  637. from_kuid_munged(&init_user_ns, cred->uid),
  638. from_kuid_munged(&init_user_ns, cred->euid),
  639. from_kuid_munged(&init_user_ns, cred->suid),
  640. from_kuid_munged(&init_user_ns, cred->fsuid));
  641. printk(KERN_ERR "CRED: ->*gid = { %d,%d,%d,%d }\n",
  642. from_kgid_munged(&init_user_ns, cred->gid),
  643. from_kgid_munged(&init_user_ns, cred->egid),
  644. from_kgid_munged(&init_user_ns, cred->sgid),
  645. from_kgid_munged(&init_user_ns, cred->fsgid));
  646. #ifdef CONFIG_SECURITY
  647. printk(KERN_ERR "CRED: ->security is %p\n", cred->security);
  648. if ((unsigned long) cred->security >= PAGE_SIZE &&
  649. (((unsigned long) cred->security & 0xffffff00) !=
  650. (POISON_FREE << 24 | POISON_FREE << 16 | POISON_FREE << 8)))
  651. printk(KERN_ERR "CRED: ->security {%x, %x}\n",
  652. ((u32*)cred->security)[0],
  653. ((u32*)cred->security)[1]);
  654. #endif
  655. }
  656. /*
  657. * report use of invalid credentials
  658. */
  659. void __invalid_creds(const struct cred *cred, const char *file, unsigned line)
  660. {
  661. printk(KERN_ERR "CRED: Invalid credentials\n");
  662. printk(KERN_ERR "CRED: At %s:%u\n", file, line);
  663. dump_invalid_creds(cred, "Specified", current);
  664. BUG();
  665. }
  666. EXPORT_SYMBOL(__invalid_creds);
  667. /*
  668. * check the credentials on a process
  669. */
  670. void __validate_process_creds(struct task_struct *tsk,
  671. const char *file, unsigned line)
  672. {
  673. if (tsk->cred == tsk->real_cred) {
  674. if (unlikely(read_cred_subscribers(tsk->cred) < 2 ||
  675. creds_are_invalid(tsk->cred)))
  676. goto invalid_creds;
  677. } else {
  678. if (unlikely(read_cred_subscribers(tsk->real_cred) < 1 ||
  679. read_cred_subscribers(tsk->cred) < 1 ||
  680. creds_are_invalid(tsk->real_cred) ||
  681. creds_are_invalid(tsk->cred)))
  682. goto invalid_creds;
  683. }
  684. return;
  685. invalid_creds:
  686. printk(KERN_ERR "CRED: Invalid process credentials\n");
  687. printk(KERN_ERR "CRED: At %s:%u\n", file, line);
  688. dump_invalid_creds(tsk->real_cred, "Real", tsk);
  689. if (tsk->cred != tsk->real_cred)
  690. dump_invalid_creds(tsk->cred, "Effective", tsk);
  691. else
  692. printk(KERN_ERR "CRED: Effective creds == Real creds\n");
  693. BUG();
  694. }
  695. EXPORT_SYMBOL(__validate_process_creds);
  696. /*
  697. * check creds for do_exit()
  698. */
  699. void validate_creds_for_do_exit(struct task_struct *tsk)
  700. {
  701. kdebug("validate_creds_for_do_exit(%p,%p{%d,%d})",
  702. tsk->real_cred, tsk->cred,
  703. atomic_read(&tsk->cred->usage),
  704. read_cred_subscribers(tsk->cred));
  705. __validate_process_creds(tsk, __FILE__, __LINE__);
  706. }
  707. #endif /* CONFIG_DEBUG_CREDENTIALS */