cred.c 22 KB

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