cred.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  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. /**
  324. * commit_creds - Install new credentials upon the current task
  325. * @new: The credentials to be assigned
  326. *
  327. * Install a new set of credentials to the current task, using RCU to replace
  328. * the old set. Both the objective and the subjective credentials pointers are
  329. * updated. This function may not be called if the subjective credentials are
  330. * in an overridden state.
  331. *
  332. * This function eats the caller's reference to the new credentials.
  333. *
  334. * Always returns 0 thus allowing this function to be tail-called at the end
  335. * of, say, sys_setgid().
  336. */
  337. int commit_creds(struct cred *new)
  338. {
  339. struct task_struct *task = current;
  340. const struct cred *old = task->real_cred;
  341. kdebug("commit_creds(%p{%d,%d})", new,
  342. atomic_read(&new->usage),
  343. read_cred_subscribers(new));
  344. BUG_ON(task->cred != old);
  345. #ifdef CONFIG_DEBUG_CREDENTIALS
  346. BUG_ON(read_cred_subscribers(old) < 2);
  347. validate_creds(old);
  348. validate_creds(new);
  349. #endif
  350. BUG_ON(atomic_read(&new->usage) < 1);
  351. get_cred(new); /* we will require a ref for the subj creds too */
  352. /* dumpability changes */
  353. if (!uid_eq(old->euid, new->euid) ||
  354. !gid_eq(old->egid, new->egid) ||
  355. !uid_eq(old->fsuid, new->fsuid) ||
  356. !gid_eq(old->fsgid, new->fsgid) ||
  357. !cap_issubset(new->cap_permitted, old->cap_permitted)) {
  358. if (task->mm)
  359. set_dumpable(task->mm, suid_dumpable);
  360. task->pdeath_signal = 0;
  361. smp_wmb();
  362. }
  363. /* alter the thread keyring */
  364. if (!uid_eq(new->fsuid, old->fsuid))
  365. key_fsuid_changed(task);
  366. if (!gid_eq(new->fsgid, old->fsgid))
  367. key_fsgid_changed(task);
  368. /* do it
  369. * RLIMIT_NPROC limits on user->processes have already been checked
  370. * in set_user().
  371. */
  372. alter_cred_subscribers(new, 2);
  373. if (new->user != old->user)
  374. atomic_inc(&new->user->processes);
  375. rcu_assign_pointer(task->real_cred, new);
  376. rcu_assign_pointer(task->cred, new);
  377. if (new->user != old->user)
  378. atomic_dec(&old->user->processes);
  379. alter_cred_subscribers(old, -2);
  380. /* send notifications */
  381. if (!uid_eq(new->uid, old->uid) ||
  382. !uid_eq(new->euid, old->euid) ||
  383. !uid_eq(new->suid, old->suid) ||
  384. !uid_eq(new->fsuid, old->fsuid))
  385. proc_id_connector(task, PROC_EVENT_UID);
  386. if (!gid_eq(new->gid, old->gid) ||
  387. !gid_eq(new->egid, old->egid) ||
  388. !gid_eq(new->sgid, old->sgid) ||
  389. !gid_eq(new->fsgid, old->fsgid))
  390. proc_id_connector(task, PROC_EVENT_GID);
  391. /* release the old obj and subj refs both */
  392. put_cred(old);
  393. put_cred(old);
  394. return 0;
  395. }
  396. EXPORT_SYMBOL(commit_creds);
  397. /**
  398. * abort_creds - Discard a set of credentials and unlock the current task
  399. * @new: The credentials that were going to be applied
  400. *
  401. * Discard a set of credentials that were under construction and unlock the
  402. * current task.
  403. */
  404. void abort_creds(struct cred *new)
  405. {
  406. kdebug("abort_creds(%p{%d,%d})", new,
  407. atomic_read(&new->usage),
  408. read_cred_subscribers(new));
  409. #ifdef CONFIG_DEBUG_CREDENTIALS
  410. BUG_ON(read_cred_subscribers(new) != 0);
  411. #endif
  412. BUG_ON(atomic_read(&new->usage) < 1);
  413. put_cred(new);
  414. }
  415. EXPORT_SYMBOL(abort_creds);
  416. /**
  417. * override_creds - Override the current process's subjective credentials
  418. * @new: The credentials to be assigned
  419. *
  420. * Install a set of temporary override subjective credentials on the current
  421. * process, returning the old set for later reversion.
  422. */
  423. const struct cred *override_creds(const struct cred *new)
  424. {
  425. const struct cred *old = current->cred;
  426. kdebug("override_creds(%p{%d,%d})", new,
  427. atomic_read(&new->usage),
  428. read_cred_subscribers(new));
  429. validate_creds(old);
  430. validate_creds(new);
  431. get_cred(new);
  432. alter_cred_subscribers(new, 1);
  433. rcu_assign_pointer(current->cred, new);
  434. alter_cred_subscribers(old, -1);
  435. kdebug("override_creds() = %p{%d,%d}", old,
  436. atomic_read(&old->usage),
  437. read_cred_subscribers(old));
  438. return old;
  439. }
  440. EXPORT_SYMBOL(override_creds);
  441. /**
  442. * revert_creds - Revert a temporary subjective credentials override
  443. * @old: The credentials to be restored
  444. *
  445. * Revert a temporary set of override subjective credentials to an old set,
  446. * discarding the override set.
  447. */
  448. void revert_creds(const struct cred *old)
  449. {
  450. const struct cred *override = current->cred;
  451. kdebug("revert_creds(%p{%d,%d})", old,
  452. atomic_read(&old->usage),
  453. read_cred_subscribers(old));
  454. validate_creds(old);
  455. validate_creds(override);
  456. alter_cred_subscribers(old, 1);
  457. rcu_assign_pointer(current->cred, old);
  458. alter_cred_subscribers(override, -1);
  459. put_cred(override);
  460. }
  461. EXPORT_SYMBOL(revert_creds);
  462. /*
  463. * initialise the credentials stuff
  464. */
  465. void __init cred_init(void)
  466. {
  467. /* allocate a slab in which we can store credentials */
  468. cred_jar = kmem_cache_create("cred_jar", sizeof(struct cred),
  469. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  470. }
  471. /**
  472. * prepare_kernel_cred - Prepare a set of credentials for a kernel service
  473. * @daemon: A userspace daemon to be used as a reference
  474. *
  475. * Prepare a set of credentials for a kernel service. This can then be used to
  476. * override a task's own credentials so that work can be done on behalf of that
  477. * task that requires a different subjective context.
  478. *
  479. * @daemon is used to provide a base for the security record, but can be NULL.
  480. * If @daemon is supplied, then the security data will be derived from that;
  481. * otherwise they'll be set to 0 and no groups, full capabilities and no keys.
  482. *
  483. * The caller may change these controls afterwards if desired.
  484. *
  485. * Returns the new credentials or NULL if out of memory.
  486. *
  487. * Does not take, and does not return holding current->cred_replace_mutex.
  488. */
  489. struct cred *prepare_kernel_cred(struct task_struct *daemon)
  490. {
  491. const struct cred *old;
  492. struct cred *new;
  493. new = kmem_cache_alloc(cred_jar, GFP_KERNEL);
  494. if (!new)
  495. return NULL;
  496. kdebug("prepare_kernel_cred() alloc %p", new);
  497. if (daemon)
  498. old = get_task_cred(daemon);
  499. else
  500. old = get_cred(&init_cred);
  501. validate_creds(old);
  502. *new = *old;
  503. atomic_set(&new->usage, 1);
  504. set_cred_subscribers(new, 0);
  505. get_uid(new->user);
  506. get_user_ns(new->user_ns);
  507. get_group_info(new->group_info);
  508. #ifdef CONFIG_KEYS
  509. new->session_keyring = NULL;
  510. new->process_keyring = NULL;
  511. new->thread_keyring = NULL;
  512. new->request_key_auth = NULL;
  513. new->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
  514. #endif
  515. #ifdef CONFIG_SECURITY
  516. new->security = NULL;
  517. #endif
  518. if (security_prepare_creds(new, old, GFP_KERNEL) < 0)
  519. goto error;
  520. put_cred(old);
  521. validate_creds(new);
  522. return new;
  523. error:
  524. put_cred(new);
  525. put_cred(old);
  526. return NULL;
  527. }
  528. EXPORT_SYMBOL(prepare_kernel_cred);
  529. /**
  530. * set_security_override - Set the security ID in a set of credentials
  531. * @new: The credentials to alter
  532. * @secid: The LSM security ID to set
  533. *
  534. * Set the LSM security ID in a set of credentials so that the subjective
  535. * security is overridden when an alternative set of credentials is used.
  536. */
  537. int set_security_override(struct cred *new, u32 secid)
  538. {
  539. return security_kernel_act_as(new, secid);
  540. }
  541. EXPORT_SYMBOL(set_security_override);
  542. /**
  543. * set_security_override_from_ctx - Set the security ID in a set of credentials
  544. * @new: The credentials to alter
  545. * @secctx: The LSM security context to generate the security ID from.
  546. *
  547. * Set the LSM security ID in a set of credentials so that the subjective
  548. * security is overridden when an alternative set of credentials is used. The
  549. * security ID is specified in string form as a security context to be
  550. * interpreted by the LSM.
  551. */
  552. int set_security_override_from_ctx(struct cred *new, const char *secctx)
  553. {
  554. u32 secid;
  555. int ret;
  556. ret = security_secctx_to_secid(secctx, strlen(secctx), &secid);
  557. if (ret < 0)
  558. return ret;
  559. return set_security_override(new, secid);
  560. }
  561. EXPORT_SYMBOL(set_security_override_from_ctx);
  562. /**
  563. * set_create_files_as - Set the LSM file create context in a set of credentials
  564. * @new: The credentials to alter
  565. * @inode: The inode to take the context from
  566. *
  567. * Change the LSM file creation context in a set of credentials to be the same
  568. * as the object context of the specified inode, so that the new inodes have
  569. * the same MAC context as that inode.
  570. */
  571. int set_create_files_as(struct cred *new, struct inode *inode)
  572. {
  573. new->fsuid = inode->i_uid;
  574. new->fsgid = inode->i_gid;
  575. return security_kernel_create_files_as(new, inode);
  576. }
  577. EXPORT_SYMBOL(set_create_files_as);
  578. #ifdef CONFIG_DEBUG_CREDENTIALS
  579. bool creds_are_invalid(const struct cred *cred)
  580. {
  581. if (cred->magic != CRED_MAGIC)
  582. return true;
  583. #ifdef CONFIG_SECURITY_SELINUX
  584. /*
  585. * cred->security == NULL if security_cred_alloc_blank() or
  586. * security_prepare_creds() returned an error.
  587. */
  588. if (selinux_is_enabled() && cred->security) {
  589. if ((unsigned long) cred->security < PAGE_SIZE)
  590. return true;
  591. if ((*(u32 *)cred->security & 0xffffff00) ==
  592. (POISON_FREE << 24 | POISON_FREE << 16 | POISON_FREE << 8))
  593. return true;
  594. }
  595. #endif
  596. return false;
  597. }
  598. EXPORT_SYMBOL(creds_are_invalid);
  599. /*
  600. * dump invalid credentials
  601. */
  602. static void dump_invalid_creds(const struct cred *cred, const char *label,
  603. const struct task_struct *tsk)
  604. {
  605. printk(KERN_ERR "CRED: %s credentials: %p %s%s%s\n",
  606. label, cred,
  607. cred == &init_cred ? "[init]" : "",
  608. cred == tsk->real_cred ? "[real]" : "",
  609. cred == tsk->cred ? "[eff]" : "");
  610. printk(KERN_ERR "CRED: ->magic=%x, put_addr=%p\n",
  611. cred->magic, cred->put_addr);
  612. printk(KERN_ERR "CRED: ->usage=%d, subscr=%d\n",
  613. atomic_read(&cred->usage),
  614. read_cred_subscribers(cred));
  615. printk(KERN_ERR "CRED: ->*uid = { %d,%d,%d,%d }\n",
  616. from_kuid_munged(&init_user_ns, cred->uid),
  617. from_kuid_munged(&init_user_ns, cred->euid),
  618. from_kuid_munged(&init_user_ns, cred->suid),
  619. from_kuid_munged(&init_user_ns, cred->fsuid));
  620. printk(KERN_ERR "CRED: ->*gid = { %d,%d,%d,%d }\n",
  621. from_kgid_munged(&init_user_ns, cred->gid),
  622. from_kgid_munged(&init_user_ns, cred->egid),
  623. from_kgid_munged(&init_user_ns, cred->sgid),
  624. from_kgid_munged(&init_user_ns, cred->fsgid));
  625. #ifdef CONFIG_SECURITY
  626. printk(KERN_ERR "CRED: ->security is %p\n", cred->security);
  627. if ((unsigned long) cred->security >= PAGE_SIZE &&
  628. (((unsigned long) cred->security & 0xffffff00) !=
  629. (POISON_FREE << 24 | POISON_FREE << 16 | POISON_FREE << 8)))
  630. printk(KERN_ERR "CRED: ->security {%x, %x}\n",
  631. ((u32*)cred->security)[0],
  632. ((u32*)cred->security)[1]);
  633. #endif
  634. }
  635. /*
  636. * report use of invalid credentials
  637. */
  638. void __invalid_creds(const struct cred *cred, const char *file, unsigned line)
  639. {
  640. printk(KERN_ERR "CRED: Invalid credentials\n");
  641. printk(KERN_ERR "CRED: At %s:%u\n", file, line);
  642. dump_invalid_creds(cred, "Specified", current);
  643. BUG();
  644. }
  645. EXPORT_SYMBOL(__invalid_creds);
  646. /*
  647. * check the credentials on a process
  648. */
  649. void __validate_process_creds(struct task_struct *tsk,
  650. const char *file, unsigned line)
  651. {
  652. if (tsk->cred == tsk->real_cred) {
  653. if (unlikely(read_cred_subscribers(tsk->cred) < 2 ||
  654. creds_are_invalid(tsk->cred)))
  655. goto invalid_creds;
  656. } else {
  657. if (unlikely(read_cred_subscribers(tsk->real_cred) < 1 ||
  658. read_cred_subscribers(tsk->cred) < 1 ||
  659. creds_are_invalid(tsk->real_cred) ||
  660. creds_are_invalid(tsk->cred)))
  661. goto invalid_creds;
  662. }
  663. return;
  664. invalid_creds:
  665. printk(KERN_ERR "CRED: Invalid process credentials\n");
  666. printk(KERN_ERR "CRED: At %s:%u\n", file, line);
  667. dump_invalid_creds(tsk->real_cred, "Real", tsk);
  668. if (tsk->cred != tsk->real_cred)
  669. dump_invalid_creds(tsk->cred, "Effective", tsk);
  670. else
  671. printk(KERN_ERR "CRED: Effective creds == Real creds\n");
  672. BUG();
  673. }
  674. EXPORT_SYMBOL(__validate_process_creds);
  675. /*
  676. * check creds for do_exit()
  677. */
  678. void validate_creds_for_do_exit(struct task_struct *tsk)
  679. {
  680. kdebug("validate_creds_for_do_exit(%p,%p{%d,%d})",
  681. tsk->real_cred, tsk->cred,
  682. atomic_read(&tsk->cred->usage),
  683. read_cred_subscribers(tsk->cred));
  684. __validate_process_creds(tsk, __FILE__, __LINE__);
  685. }
  686. #endif /* CONFIG_DEBUG_CREDENTIALS */