cred.c 21 KB

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