cred.c 22 KB

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