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