cred.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Task credentials management - see Documentation/security/credentials.rst
  3. *
  4. * Copyright (C) 2008 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #define pr_fmt(fmt) "CRED: " fmt
  8. #include <linux/export.h>
  9. #include <linux/cred.h>
  10. #include <linux/slab.h>
  11. #include <linux/sched.h>
  12. #include <linux/sched/coredump.h>
  13. #include <linux/key.h>
  14. #include <linux/keyctl.h>
  15. #include <linux/init_task.h>
  16. #include <linux/security.h>
  17. #include <linux/binfmts.h>
  18. #include <linux/cn_proc.h>
  19. #include <linux/uidgid.h>
  20. #if 0
  21. #define kdebug(FMT, ...) \
  22. printk("[%-5.5s%5u] " FMT "\n", \
  23. current->comm, current->pid, ##__VA_ARGS__)
  24. #else
  25. #define kdebug(FMT, ...) \
  26. do { \
  27. if (0) \
  28. no_printk("[%-5.5s%5u] " FMT "\n", \
  29. current->comm, current->pid, ##__VA_ARGS__); \
  30. } while (0)
  31. #endif
  32. static struct kmem_cache *cred_jar;
  33. /* init to 2 - one for init_task, one to ensure it is never freed */
  34. static struct group_info init_groups = { .usage = REFCOUNT_INIT(2) };
  35. /*
  36. * The initial credentials for the initial task
  37. */
  38. struct cred init_cred = {
  39. .usage = ATOMIC_INIT(4),
  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. .ucounts = &init_ucounts,
  57. };
  58. /*
  59. * The RCU callback to actually dispose of a set of credentials
  60. */
  61. static void put_cred_rcu(struct rcu_head *rcu)
  62. {
  63. struct cred *cred = container_of(rcu, struct cred, rcu);
  64. kdebug("put_cred_rcu(%p)", cred);
  65. if (atomic_long_read(&cred->usage) != 0)
  66. panic("CRED: put_cred_rcu() sees %p with usage %ld\n",
  67. cred, atomic_long_read(&cred->usage));
  68. security_cred_free(cred);
  69. key_put(cred->session_keyring);
  70. key_put(cred->process_keyring);
  71. key_put(cred->thread_keyring);
  72. key_put(cred->request_key_auth);
  73. if (cred->group_info)
  74. put_group_info(cred->group_info);
  75. free_uid(cred->user);
  76. if (cred->ucounts)
  77. put_ucounts(cred->ucounts);
  78. put_user_ns(cred->user_ns);
  79. kmem_cache_free(cred_jar, cred);
  80. }
  81. /**
  82. * __put_cred - Destroy a set of credentials
  83. * @cred: The record to release
  84. *
  85. * Destroy a set of credentials on which no references remain.
  86. */
  87. void __put_cred(struct cred *cred)
  88. {
  89. kdebug("__put_cred(%p{%ld})", cred,
  90. atomic_long_read(&cred->usage));
  91. BUG_ON(atomic_long_read(&cred->usage) != 0);
  92. BUG_ON(cred == current->cred);
  93. BUG_ON(cred == current->real_cred);
  94. if (cred->non_rcu)
  95. put_cred_rcu(&cred->rcu);
  96. else
  97. call_rcu(&cred->rcu, put_cred_rcu);
  98. }
  99. EXPORT_SYMBOL(__put_cred);
  100. /*
  101. * Clean up a task's credentials when it exits
  102. */
  103. void exit_creds(struct task_struct *tsk)
  104. {
  105. struct cred *real_cred, *cred;
  106. kdebug("exit_creds(%u,%p,%p,{%ld})", tsk->pid, tsk->real_cred, tsk->cred,
  107. atomic_long_read(&tsk->cred->usage));
  108. real_cred = (struct cred *) tsk->real_cred;
  109. tsk->real_cred = NULL;
  110. cred = (struct cred *) tsk->cred;
  111. tsk->cred = NULL;
  112. if (real_cred == cred) {
  113. put_cred_many(cred, 2);
  114. } else {
  115. put_cred(real_cred);
  116. put_cred(cred);
  117. }
  118. #ifdef CONFIG_KEYS_REQUEST_CACHE
  119. key_put(tsk->cached_requested_key);
  120. tsk->cached_requested_key = NULL;
  121. #endif
  122. }
  123. /**
  124. * get_task_cred - Get another task's objective credentials
  125. * @task: The task to query
  126. *
  127. * Get the objective credentials of a task, pinning them so that they can't go
  128. * away. Accessing a task's credentials directly is not permitted.
  129. *
  130. * The caller must also make sure task doesn't get deleted, either by holding a
  131. * ref on task or by holding tasklist_lock to prevent it from being unlinked.
  132. */
  133. const struct cred *get_task_cred(struct task_struct *task)
  134. {
  135. const struct cred *cred;
  136. rcu_read_lock();
  137. do {
  138. cred = __task_cred((task));
  139. BUG_ON(!cred);
  140. } while (!get_cred_rcu(cred));
  141. rcu_read_unlock();
  142. return cred;
  143. }
  144. EXPORT_SYMBOL(get_task_cred);
  145. /*
  146. * Allocate blank credentials, such that the credentials can be filled in at a
  147. * later date without risk of ENOMEM.
  148. */
  149. struct cred *cred_alloc_blank(void)
  150. {
  151. struct cred *new;
  152. new = kmem_cache_zalloc(cred_jar, GFP_KERNEL);
  153. if (!new)
  154. return NULL;
  155. atomic_long_set(&new->usage, 1);
  156. if (security_cred_alloc_blank(new, GFP_KERNEL_ACCOUNT) < 0)
  157. goto error;
  158. return new;
  159. error:
  160. abort_creds(new);
  161. return NULL;
  162. }
  163. /**
  164. * prepare_creds - Prepare a new set of credentials for modification
  165. *
  166. * Prepare a new set of task credentials for modification. A task's creds
  167. * shouldn't generally be modified directly, therefore this function is used to
  168. * prepare a new copy, which the caller then modifies and then commits by
  169. * calling commit_creds().
  170. *
  171. * Preparation involves making a copy of the objective creds for modification.
  172. *
  173. * Returns a pointer to the new creds-to-be if successful, NULL otherwise.
  174. *
  175. * Call commit_creds() or abort_creds() to clean up.
  176. */
  177. struct cred *prepare_creds(void)
  178. {
  179. struct task_struct *task = current;
  180. const struct cred *old;
  181. struct cred *new;
  182. new = kmem_cache_alloc(cred_jar, GFP_KERNEL);
  183. if (!new)
  184. return NULL;
  185. kdebug("prepare_creds() alloc %p", new);
  186. old = task->cred;
  187. memcpy(new, old, sizeof(struct cred));
  188. new->non_rcu = 0;
  189. atomic_long_set(&new->usage, 1);
  190. get_group_info(new->group_info);
  191. get_uid(new->user);
  192. get_user_ns(new->user_ns);
  193. #ifdef CONFIG_KEYS
  194. key_get(new->session_keyring);
  195. key_get(new->process_keyring);
  196. key_get(new->thread_keyring);
  197. key_get(new->request_key_auth);
  198. #endif
  199. #ifdef CONFIG_SECURITY
  200. new->security = NULL;
  201. #endif
  202. new->ucounts = get_ucounts(new->ucounts);
  203. if (!new->ucounts)
  204. goto error;
  205. if (security_prepare_creds(new, old, GFP_KERNEL_ACCOUNT) < 0)
  206. goto error;
  207. return new;
  208. error:
  209. abort_creds(new);
  210. return NULL;
  211. }
  212. EXPORT_SYMBOL(prepare_creds);
  213. /*
  214. * Prepare credentials for current to perform an execve()
  215. * - The caller must hold ->cred_guard_mutex
  216. */
  217. struct cred *prepare_exec_creds(void)
  218. {
  219. struct cred *new;
  220. new = prepare_creds();
  221. if (!new)
  222. return new;
  223. #ifdef CONFIG_KEYS
  224. /* newly exec'd tasks don't get a thread keyring */
  225. key_put(new->thread_keyring);
  226. new->thread_keyring = NULL;
  227. /* inherit the session keyring; new process keyring */
  228. key_put(new->process_keyring);
  229. new->process_keyring = NULL;
  230. #endif
  231. new->suid = new->fsuid = new->euid;
  232. new->sgid = new->fsgid = new->egid;
  233. return new;
  234. }
  235. /*
  236. * Copy credentials for the new process created by fork()
  237. *
  238. * We share if we can, but under some circumstances we have to generate a new
  239. * set.
  240. *
  241. * The new process gets the current process's subjective credentials as its
  242. * objective and subjective credentials
  243. */
  244. int copy_creds(struct task_struct *p, unsigned long clone_flags)
  245. {
  246. struct cred *new;
  247. int ret;
  248. #ifdef CONFIG_KEYS_REQUEST_CACHE
  249. p->cached_requested_key = NULL;
  250. #endif
  251. if (
  252. #ifdef CONFIG_KEYS
  253. !p->cred->thread_keyring &&
  254. #endif
  255. clone_flags & CLONE_THREAD
  256. ) {
  257. p->real_cred = get_cred_many(p->cred, 2);
  258. kdebug("share_creds(%p{%ld})",
  259. p->cred, atomic_long_read(&p->cred->usage));
  260. inc_rlimit_ucounts(task_ucounts(p), UCOUNT_RLIMIT_NPROC, 1);
  261. return 0;
  262. }
  263. new = prepare_creds();
  264. if (!new)
  265. return -ENOMEM;
  266. if (clone_flags & CLONE_NEWUSER) {
  267. ret = create_user_ns(new);
  268. if (ret < 0)
  269. goto error_put;
  270. ret = set_cred_ucounts(new);
  271. if (ret < 0)
  272. goto error_put;
  273. }
  274. #ifdef CONFIG_KEYS
  275. /* new threads get their own thread keyrings if their parent already
  276. * had one */
  277. if (new->thread_keyring) {
  278. key_put(new->thread_keyring);
  279. new->thread_keyring = NULL;
  280. if (clone_flags & CLONE_THREAD)
  281. install_thread_keyring_to_cred(new);
  282. }
  283. /* The process keyring is only shared between the threads in a process;
  284. * anything outside of those threads doesn't inherit.
  285. */
  286. if (!(clone_flags & CLONE_THREAD)) {
  287. key_put(new->process_keyring);
  288. new->process_keyring = NULL;
  289. }
  290. #endif
  291. p->cred = p->real_cred = get_cred(new);
  292. inc_rlimit_ucounts(task_ucounts(p), UCOUNT_RLIMIT_NPROC, 1);
  293. return 0;
  294. error_put:
  295. put_cred(new);
  296. return ret;
  297. }
  298. static bool cred_cap_issubset(const struct cred *set, const struct cred *subset)
  299. {
  300. const struct user_namespace *set_ns = set->user_ns;
  301. const struct user_namespace *subset_ns = subset->user_ns;
  302. /* If the two credentials are in the same user namespace see if
  303. * the capabilities of subset are a subset of set.
  304. */
  305. if (set_ns == subset_ns)
  306. return cap_issubset(subset->cap_permitted, set->cap_permitted);
  307. /* The credentials are in a different user namespaces
  308. * therefore one is a subset of the other only if a set is an
  309. * ancestor of subset and set->euid is owner of subset or one
  310. * of subsets ancestors.
  311. */
  312. for (;subset_ns != &init_user_ns; subset_ns = subset_ns->parent) {
  313. if ((set_ns == subset_ns->parent) &&
  314. uid_eq(subset_ns->owner, set->euid))
  315. return true;
  316. }
  317. return false;
  318. }
  319. /**
  320. * commit_creds - Install new credentials upon the current task
  321. * @new: The credentials to be assigned
  322. *
  323. * Install a new set of credentials to the current task, using RCU to replace
  324. * the old set. Both the objective and the subjective credentials pointers are
  325. * updated. This function may not be called if the subjective credentials are
  326. * in an overridden state.
  327. *
  328. * This function eats the caller's reference to the new credentials.
  329. *
  330. * Always returns 0 thus allowing this function to be tail-called at the end
  331. * of, say, sys_setgid().
  332. */
  333. int commit_creds(struct cred *new)
  334. {
  335. struct task_struct *task = current;
  336. const struct cred *old = task->real_cred;
  337. kdebug("commit_creds(%p{%ld})", new,
  338. atomic_long_read(&new->usage));
  339. BUG_ON(task->cred != old);
  340. BUG_ON(atomic_long_read(&new->usage) < 1);
  341. get_cred(new); /* we will require a ref for the subj creds too */
  342. /* dumpability changes */
  343. if (!uid_eq(old->euid, new->euid) ||
  344. !gid_eq(old->egid, new->egid) ||
  345. !uid_eq(old->fsuid, new->fsuid) ||
  346. !gid_eq(old->fsgid, new->fsgid) ||
  347. !cred_cap_issubset(old, new)) {
  348. if (task->mm)
  349. set_dumpable(task->mm, suid_dumpable);
  350. task->pdeath_signal = 0;
  351. /*
  352. * If a task drops privileges and becomes nondumpable,
  353. * the dumpability change must become visible before
  354. * the credential change; otherwise, a __ptrace_may_access()
  355. * racing with this change may be able to attach to a task it
  356. * shouldn't be able to attach to (as if the task had dropped
  357. * privileges without becoming nondumpable).
  358. * Pairs with a read barrier in __ptrace_may_access().
  359. */
  360. smp_wmb();
  361. }
  362. /* alter the thread keyring */
  363. if (!uid_eq(new->fsuid, old->fsuid))
  364. key_fsuid_changed(new);
  365. if (!gid_eq(new->fsgid, old->fsgid))
  366. key_fsgid_changed(new);
  367. /* do it
  368. * RLIMIT_NPROC limits on user->processes have already been checked
  369. * in set_user().
  370. */
  371. if (new->user != old->user || new->user_ns != old->user_ns)
  372. inc_rlimit_ucounts(new->ucounts, UCOUNT_RLIMIT_NPROC, 1);
  373. rcu_assign_pointer(task->real_cred, new);
  374. rcu_assign_pointer(task->cred, new);
  375. if (new->user != old->user || new->user_ns != old->user_ns)
  376. dec_rlimit_ucounts(old->ucounts, UCOUNT_RLIMIT_NPROC, 1);
  377. /* send notifications */
  378. if (!uid_eq(new->uid, old->uid) ||
  379. !uid_eq(new->euid, old->euid) ||
  380. !uid_eq(new->suid, old->suid) ||
  381. !uid_eq(new->fsuid, old->fsuid))
  382. proc_id_connector(task, PROC_EVENT_UID);
  383. if (!gid_eq(new->gid, old->gid) ||
  384. !gid_eq(new->egid, old->egid) ||
  385. !gid_eq(new->sgid, old->sgid) ||
  386. !gid_eq(new->fsgid, old->fsgid))
  387. proc_id_connector(task, PROC_EVENT_GID);
  388. /* release the old obj and subj refs both */
  389. put_cred_many(old, 2);
  390. return 0;
  391. }
  392. EXPORT_SYMBOL(commit_creds);
  393. /**
  394. * abort_creds - Discard a set of credentials and unlock the current task
  395. * @new: The credentials that were going to be applied
  396. *
  397. * Discard a set of credentials that were under construction and unlock the
  398. * current task.
  399. */
  400. void abort_creds(struct cred *new)
  401. {
  402. kdebug("abort_creds(%p{%ld})", new,
  403. atomic_long_read(&new->usage));
  404. BUG_ON(atomic_long_read(&new->usage) < 1);
  405. put_cred(new);
  406. }
  407. EXPORT_SYMBOL(abort_creds);
  408. /**
  409. * override_creds - Override the current process's subjective credentials
  410. * @new: The credentials to be assigned
  411. *
  412. * Install a set of temporary override subjective credentials on the current
  413. * process, returning the old set for later reversion.
  414. */
  415. const struct cred *override_creds(const struct cred *new)
  416. {
  417. const struct cred *old = current->cred;
  418. kdebug("override_creds(%p{%ld})", new,
  419. atomic_long_read(&new->usage));
  420. /*
  421. * NOTE! This uses 'get_new_cred()' rather than 'get_cred()'.
  422. *
  423. * That means that we do not clear the 'non_rcu' flag, since
  424. * we are only installing the cred into the thread-synchronous
  425. * '->cred' pointer, not the '->real_cred' pointer that is
  426. * visible to other threads under RCU.
  427. */
  428. get_new_cred((struct cred *)new);
  429. rcu_assign_pointer(current->cred, new);
  430. kdebug("override_creds() = %p{%ld}", old,
  431. atomic_long_read(&old->usage));
  432. return old;
  433. }
  434. EXPORT_SYMBOL(override_creds);
  435. /**
  436. * revert_creds - Revert a temporary subjective credentials override
  437. * @old: The credentials to be restored
  438. *
  439. * Revert a temporary set of override subjective credentials to an old set,
  440. * discarding the override set.
  441. */
  442. void revert_creds(const struct cred *old)
  443. {
  444. const struct cred *override = current->cred;
  445. kdebug("revert_creds(%p{%ld})", old,
  446. atomic_long_read(&old->usage));
  447. rcu_assign_pointer(current->cred, old);
  448. put_cred(override);
  449. }
  450. EXPORT_SYMBOL(revert_creds);
  451. /**
  452. * cred_fscmp - Compare two credentials with respect to filesystem access.
  453. * @a: The first credential
  454. * @b: The second credential
  455. *
  456. * cred_cmp() will return zero if both credentials have the same
  457. * fsuid, fsgid, and supplementary groups. That is, if they will both
  458. * provide the same access to files based on mode/uid/gid.
  459. * If the credentials are different, then either -1 or 1 will
  460. * be returned depending on whether @a comes before or after @b
  461. * respectively in an arbitrary, but stable, ordering of credentials.
  462. *
  463. * Return: -1, 0, or 1 depending on comparison
  464. */
  465. int cred_fscmp(const struct cred *a, const struct cred *b)
  466. {
  467. struct group_info *ga, *gb;
  468. int g;
  469. if (a == b)
  470. return 0;
  471. if (uid_lt(a->fsuid, b->fsuid))
  472. return -1;
  473. if (uid_gt(a->fsuid, b->fsuid))
  474. return 1;
  475. if (gid_lt(a->fsgid, b->fsgid))
  476. return -1;
  477. if (gid_gt(a->fsgid, b->fsgid))
  478. return 1;
  479. ga = a->group_info;
  480. gb = b->group_info;
  481. if (ga == gb)
  482. return 0;
  483. if (ga == NULL)
  484. return -1;
  485. if (gb == NULL)
  486. return 1;
  487. if (ga->ngroups < gb->ngroups)
  488. return -1;
  489. if (ga->ngroups > gb->ngroups)
  490. return 1;
  491. for (g = 0; g < ga->ngroups; g++) {
  492. if (gid_lt(ga->gid[g], gb->gid[g]))
  493. return -1;
  494. if (gid_gt(ga->gid[g], gb->gid[g]))
  495. return 1;
  496. }
  497. return 0;
  498. }
  499. EXPORT_SYMBOL(cred_fscmp);
  500. int set_cred_ucounts(struct cred *new)
  501. {
  502. struct ucounts *new_ucounts, *old_ucounts = new->ucounts;
  503. /*
  504. * This optimization is needed because alloc_ucounts() uses locks
  505. * for table lookups.
  506. */
  507. if (old_ucounts->ns == new->user_ns && uid_eq(old_ucounts->uid, new->uid))
  508. return 0;
  509. if (!(new_ucounts = alloc_ucounts(new->user_ns, new->uid)))
  510. return -EAGAIN;
  511. new->ucounts = new_ucounts;
  512. put_ucounts(old_ucounts);
  513. return 0;
  514. }
  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(cred,
  522. SLAB_HWCACHE_ALIGN | SLAB_PANIC | SLAB_ACCOUNT);
  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 cred, with the security data derived from
  533. * that; if this is "&init_task", they'll be set to 0, no groups, full
  534. * 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. struct cred *prepare_kernel_cred(struct task_struct *daemon)
  541. {
  542. const struct cred *old;
  543. struct cred *new;
  544. if (WARN_ON_ONCE(!daemon))
  545. return NULL;
  546. new = kmem_cache_alloc(cred_jar, GFP_KERNEL);
  547. if (!new)
  548. return NULL;
  549. kdebug("prepare_kernel_cred() alloc %p", new);
  550. old = get_task_cred(daemon);
  551. *new = *old;
  552. new->non_rcu = 0;
  553. atomic_long_set(&new->usage, 1);
  554. get_uid(new->user);
  555. get_user_ns(new->user_ns);
  556. get_group_info(new->group_info);
  557. #ifdef CONFIG_KEYS
  558. new->session_keyring = NULL;
  559. new->process_keyring = NULL;
  560. new->thread_keyring = NULL;
  561. new->request_key_auth = NULL;
  562. new->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
  563. #endif
  564. #ifdef CONFIG_SECURITY
  565. new->security = NULL;
  566. #endif
  567. new->ucounts = get_ucounts(new->ucounts);
  568. if (!new->ucounts)
  569. goto error;
  570. if (security_prepare_creds(new, old, GFP_KERNEL_ACCOUNT) < 0)
  571. goto error;
  572. put_cred(old);
  573. return new;
  574. error:
  575. put_cred(new);
  576. put_cred(old);
  577. return NULL;
  578. }
  579. EXPORT_SYMBOL(prepare_kernel_cred);
  580. /**
  581. * set_security_override - Set the security ID in a set of credentials
  582. * @new: The credentials to alter
  583. * @secid: The LSM security ID to set
  584. *
  585. * Set the LSM security ID in a set of credentials so that the subjective
  586. * security is overridden when an alternative set of credentials is used.
  587. */
  588. int set_security_override(struct cred *new, u32 secid)
  589. {
  590. return security_kernel_act_as(new, secid);
  591. }
  592. EXPORT_SYMBOL(set_security_override);
  593. /**
  594. * set_security_override_from_ctx - Set the security ID in a set of credentials
  595. * @new: The credentials to alter
  596. * @secctx: The LSM security context to generate the security ID from.
  597. *
  598. * Set the LSM security ID in a set of credentials so that the subjective
  599. * security is overridden when an alternative set of credentials is used. The
  600. * security ID is specified in string form as a security context to be
  601. * interpreted by the LSM.
  602. */
  603. int set_security_override_from_ctx(struct cred *new, const char *secctx)
  604. {
  605. u32 secid;
  606. int ret;
  607. ret = security_secctx_to_secid(secctx, strlen(secctx), &secid);
  608. if (ret < 0)
  609. return ret;
  610. return set_security_override(new, secid);
  611. }
  612. EXPORT_SYMBOL(set_security_override_from_ctx);
  613. /**
  614. * set_create_files_as - Set the LSM file create context in a set of credentials
  615. * @new: The credentials to alter
  616. * @inode: The inode to take the context from
  617. *
  618. * Change the LSM file creation context in a set of credentials to be the same
  619. * as the object context of the specified inode, so that the new inodes have
  620. * the same MAC context as that inode.
  621. */
  622. int set_create_files_as(struct cred *new, struct inode *inode)
  623. {
  624. if (!uid_valid(inode->i_uid) || !gid_valid(inode->i_gid))
  625. return -EINVAL;
  626. new->fsuid = inode->i_uid;
  627. new->fsgid = inode->i_gid;
  628. return security_kernel_create_files_as(new, inode);
  629. }
  630. EXPORT_SYMBOL(set_create_files_as);