cred.c 21 KB

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