xref: /freebsd/sys/kern/kern_prot.c (revision 68d4b311270ecca80bbb887a5e502e2caacebc98)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1989, 1990, 1991, 1993
5  *	The Regents of the University of California.
6  * (c) UNIX System Laboratories, Inc.
7  * Copyright (c) 2000-2001 Robert N. M. Watson.
8  * All rights reserved.
9  * Copyright (c) 2024-2025 The FreeBSD Foundation
10  *
11  * Portions of this software were developed by Olivier Certner
12  * <olce@FreeBSD.org> at Kumacom SARL under sponsorship from the FreeBSD
13  * Foundation.
14  *
15  * All or some portions of this file are derived from material licensed
16  * to the University of California by American Telephone and Telegraph
17  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
18  * the permission of UNIX System Laboratories, Inc.
19  *
20  * Redistribution and use in source and binary forms, with or without
21  * modification, are permitted provided that the following conditions
22  * are met:
23  * 1. Redistributions of source code must retain the above copyright
24  *    notice, this list of conditions and the following disclaimer.
25  * 2. Redistributions in binary form must reproduce the above copyright
26  *    notice, this list of conditions and the following disclaimer in the
27  *    documentation and/or other materials provided with the distribution.
28  * 3. Neither the name of the University nor the names of its contributors
29  *    may be used to endorse or promote products derived from this software
30  *    without specific prior written permission.
31  *
32  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42  * SUCH DAMAGE.
43  */
44 
45 /*
46  * System calls related to processes and protection
47  */
48 
49 #include "opt_inet.h"
50 #include "opt_inet6.h"
51 
52 #include <sys/systm.h>
53 #include <sys/abi_compat.h>
54 #include <sys/acct.h>
55 #include <sys/capsicum.h>
56 #include <sys/imgact.h>
57 #include <sys/kdb.h>
58 #include <sys/kernel.h>
59 #include <sys/libkern.h>
60 #include <sys/lock.h>
61 #include <sys/loginclass.h>
62 #include <sys/malloc.h>
63 #include <sys/mutex.h>
64 #include <sys/ptrace.h>
65 #include <sys/refcount.h>
66 #include <sys/sx.h>
67 #include <sys/priv.h>
68 #include <sys/proc.h>
69 #ifdef COMPAT_43
70 #include <sys/sysent.h>
71 #endif
72 #include <sys/sysproto.h>
73 #include <sys/jail.h>
74 #include <sys/racct.h>
75 #include <sys/rctl.h>
76 #include <sys/resourcevar.h>
77 #include <sys/socket.h>
78 #include <sys/socketvar.h>
79 #include <sys/syscallsubr.h>
80 #include <sys/sysctl.h>
81 
82 #ifdef MAC
83 #include <security/mac/mac_syscalls.h>
84 #endif
85 
86 #include <vm/uma.h>
87 
88 #ifdef REGRESSION
89 FEATURE(regression,
90     "Kernel support for interfaces necessary for regression testing (SECURITY RISK!)");
91 #endif
92 
93 #include <security/audit/audit.h>
94 #include <security/mac/mac_framework.h>
95 
96 static MALLOC_DEFINE(M_CRED, "cred", "credentials");
97 
98 SYSCTL_NODE(_security, OID_AUTO, bsd, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
99     "BSD security policy");
100 
101 static void crfree_final(struct ucred *cr);
102 
103 static inline void
groups_check_positive_len(int ngrp)104 groups_check_positive_len(int ngrp)
105 {
106 	MPASS2(ngrp >= 0, "negative number of groups");
107 }
108 static inline void
groups_check_max_len(int ngrp)109 groups_check_max_len(int ngrp)
110 {
111 	MPASS2(ngrp <= ngroups_max, "too many supplementary groups");
112 }
113 
114 static void groups_normalize(int *ngrp, gid_t *groups);
115 static void crsetgroups_internal(struct ucred *cr, int ngrp,
116     const gid_t *groups);
117 
118 static int cr_canseeotheruids(struct ucred *u1, struct ucred *u2);
119 static int cr_canseeothergids(struct ucred *u1, struct ucred *u2);
120 static int cr_canseejailproc(struct ucred *u1, struct ucred *u2);
121 
122 #ifndef _SYS_SYSPROTO_H_
123 struct getpid_args {
124 	int	dummy;
125 };
126 #endif
127 /* ARGSUSED */
128 int
sys_getpid(struct thread * td,struct getpid_args * uap)129 sys_getpid(struct thread *td, struct getpid_args *uap)
130 {
131 	struct proc *p = td->td_proc;
132 
133 	td->td_retval[0] = p->p_pid;
134 #if defined(COMPAT_43)
135 	if (SV_PROC_FLAG(p, SV_AOUT))
136 		td->td_retval[1] = kern_getppid(td);
137 #endif
138 	return (0);
139 }
140 
141 #ifndef _SYS_SYSPROTO_H_
142 struct getppid_args {
143         int     dummy;
144 };
145 #endif
146 /* ARGSUSED */
147 int
sys_getppid(struct thread * td,struct getppid_args * uap)148 sys_getppid(struct thread *td, struct getppid_args *uap)
149 {
150 
151 	td->td_retval[0] = kern_getppid(td);
152 	return (0);
153 }
154 
155 int
kern_getppid(struct thread * td)156 kern_getppid(struct thread *td)
157 {
158 	struct proc *p = td->td_proc;
159 
160 	return (p->p_oppid);
161 }
162 
163 /*
164  * Get process group ID; note that POSIX getpgrp takes no parameter.
165  */
166 #ifndef _SYS_SYSPROTO_H_
167 struct getpgrp_args {
168         int     dummy;
169 };
170 #endif
171 int
sys_getpgrp(struct thread * td,struct getpgrp_args * uap)172 sys_getpgrp(struct thread *td, struct getpgrp_args *uap)
173 {
174 	struct proc *p = td->td_proc;
175 
176 	PROC_LOCK(p);
177 	td->td_retval[0] = p->p_pgrp->pg_id;
178 	PROC_UNLOCK(p);
179 	return (0);
180 }
181 
182 /* Get an arbitrary pid's process group id */
183 #ifndef _SYS_SYSPROTO_H_
184 struct getpgid_args {
185 	pid_t	pid;
186 };
187 #endif
188 int
sys_getpgid(struct thread * td,struct getpgid_args * uap)189 sys_getpgid(struct thread *td, struct getpgid_args *uap)
190 {
191 	struct proc *p;
192 	int error;
193 
194 	if (uap->pid == 0) {
195 		p = td->td_proc;
196 		PROC_LOCK(p);
197 	} else {
198 		p = pfind_any(uap->pid);
199 		if (p == NULL)
200 			return (ESRCH);
201 		error = p_cansee(td, p);
202 		if (error) {
203 			PROC_UNLOCK(p);
204 			return (error);
205 		}
206 	}
207 	td->td_retval[0] = p->p_pgrp->pg_id;
208 	PROC_UNLOCK(p);
209 	return (0);
210 }
211 
212 /*
213  * Get an arbitrary pid's session id.
214  */
215 #ifndef _SYS_SYSPROTO_H_
216 struct getsid_args {
217 	pid_t	pid;
218 };
219 #endif
220 int
sys_getsid(struct thread * td,struct getsid_args * uap)221 sys_getsid(struct thread *td, struct getsid_args *uap)
222 {
223 
224 	return (kern_getsid(td, uap->pid));
225 }
226 
227 int
kern_getsid(struct thread * td,pid_t pid)228 kern_getsid(struct thread *td, pid_t pid)
229 {
230 	struct proc *p;
231 	int error;
232 
233 	error = 0;
234 	if (pid == 0) {
235 		p = td->td_proc;
236 		PROC_LOCK(p);
237 	} else {
238 		p = pfind_any(pid);
239 		if (p == NULL)
240 			return (ESRCH);
241 		error = p_cansee(td, p);
242 		if (error) {
243 			PROC_UNLOCK(p);
244 			return (error);
245 		}
246 	}
247 	if (p->p_session != NULL)
248 		td->td_retval[0] = p->p_session->s_sid;
249 	else
250 		error = EINVAL;
251 	PROC_UNLOCK(p);
252 	return (error);
253 }
254 
255 #ifndef _SYS_SYSPROTO_H_
256 struct getuid_args {
257         int     dummy;
258 };
259 #endif
260 /* ARGSUSED */
261 int
sys_getuid(struct thread * td,struct getuid_args * uap)262 sys_getuid(struct thread *td, struct getuid_args *uap)
263 {
264 
265 	td->td_retval[0] = td->td_ucred->cr_ruid;
266 #if defined(COMPAT_43)
267 	td->td_retval[1] = td->td_ucred->cr_uid;
268 #endif
269 	return (0);
270 }
271 
272 #ifndef _SYS_SYSPROTO_H_
273 struct geteuid_args {
274         int     dummy;
275 };
276 #endif
277 /* ARGSUSED */
278 int
sys_geteuid(struct thread * td,struct geteuid_args * uap)279 sys_geteuid(struct thread *td, struct geteuid_args *uap)
280 {
281 
282 	td->td_retval[0] = td->td_ucred->cr_uid;
283 	return (0);
284 }
285 
286 #ifndef _SYS_SYSPROTO_H_
287 struct getgid_args {
288         int     dummy;
289 };
290 #endif
291 /* ARGSUSED */
292 int
sys_getgid(struct thread * td,struct getgid_args * uap)293 sys_getgid(struct thread *td, struct getgid_args *uap)
294 {
295 
296 	td->td_retval[0] = td->td_ucred->cr_rgid;
297 #if defined(COMPAT_43)
298 	td->td_retval[1] = td->td_ucred->cr_gid;
299 #endif
300 	return (0);
301 }
302 
303 #ifndef _SYS_SYSPROTO_H_
304 struct getegid_args {
305         int     dummy;
306 };
307 #endif
308 /* ARGSUSED */
309 int
sys_getegid(struct thread * td,struct getegid_args * uap)310 sys_getegid(struct thread *td, struct getegid_args *uap)
311 {
312 
313 	td->td_retval[0] = td->td_ucred->cr_gid;
314 	return (0);
315 }
316 
317 #ifdef COMPAT_FREEBSD14
318 int
freebsd14_getgroups(struct thread * td,struct freebsd14_getgroups_args * uap)319 freebsd14_getgroups(struct thread *td, struct freebsd14_getgroups_args *uap)
320 {
321 	struct ucred *cred;
322 	int ngrp, error;
323 
324 	cred = td->td_ucred;
325 
326 	/*
327 	 * For FreeBSD < 15.0, we account for the egid being placed at the
328 	 * beginning of the group list prior to all supplementary groups.
329 	 */
330 	ngrp = cred->cr_ngroups + 1;
331 	if (uap->gidsetsize == 0) {
332 		error = 0;
333 		goto out;
334 	} else if (uap->gidsetsize < ngrp) {
335 		return (EINVAL);
336 	}
337 
338 	error = copyout(&cred->cr_gid, uap->gidset, sizeof(gid_t));
339 	if (error == 0)
340 		error = copyout(cred->cr_groups, uap->gidset + 1,
341 		    (ngrp - 1) * sizeof(gid_t));
342 
343 out:
344 	td->td_retval[0] = ngrp;
345 	return (error);
346 
347 }
348 #endif	/* COMPAT_FREEBSD14 */
349 
350 #ifndef _SYS_SYSPROTO_H_
351 struct getgroups_args {
352 	int	gidsetsize;
353 	gid_t	*gidset;
354 };
355 #endif
356 int
sys_getgroups(struct thread * td,struct getgroups_args * uap)357 sys_getgroups(struct thread *td, struct getgroups_args *uap)
358 {
359 	struct ucred *cred;
360 	int ngrp, error;
361 
362 	cred = td->td_ucred;
363 
364 	ngrp = cred->cr_ngroups;
365 	if (uap->gidsetsize == 0) {
366 		error = 0;
367 		goto out;
368 	}
369 	if (uap->gidsetsize < ngrp)
370 		return (EINVAL);
371 
372 	error = copyout(cred->cr_groups, uap->gidset, ngrp * sizeof(gid_t));
373 out:
374 	td->td_retval[0] = ngrp;
375 	return (error);
376 }
377 
378 #ifndef _SYS_SYSPROTO_H_
379 struct setsid_args {
380         int     dummy;
381 };
382 #endif
383 /* ARGSUSED */
384 int
sys_setsid(struct thread * td,struct setsid_args * uap)385 sys_setsid(struct thread *td, struct setsid_args *uap)
386 {
387 	struct pgrp *pgrp;
388 	int error;
389 	struct proc *p = td->td_proc;
390 	struct pgrp *newpgrp;
391 	struct session *newsess;
392 
393 	pgrp = NULL;
394 
395 	newpgrp = uma_zalloc(pgrp_zone, M_WAITOK);
396 	newsess = malloc(sizeof(struct session), M_SESSION, M_WAITOK | M_ZERO);
397 
398 again:
399 	error = 0;
400 	sx_xlock(&proctree_lock);
401 
402 	if (p->p_pgid == p->p_pid || (pgrp = pgfind(p->p_pid)) != NULL) {
403 		if (pgrp != NULL)
404 			PGRP_UNLOCK(pgrp);
405 		error = EPERM;
406 	} else {
407 		error = enterpgrp(p, p->p_pid, newpgrp, newsess);
408 		if (error == ERESTART)
409 			goto again;
410 		MPASS(error == 0);
411 		td->td_retval[0] = p->p_pid;
412 		newpgrp = NULL;
413 		newsess = NULL;
414 	}
415 
416 	sx_xunlock(&proctree_lock);
417 
418 	uma_zfree(pgrp_zone, newpgrp);
419 	free(newsess, M_SESSION);
420 
421 	return (error);
422 }
423 
424 /*
425  * set process group (setpgid/old setpgrp)
426  *
427  * caller does setpgid(targpid, targpgid)
428  *
429  * pid must be caller or child of caller (ESRCH)
430  * if a child
431  *	pid must be in same session (EPERM)
432  *	pid can't have done an exec (EACCES)
433  * if pgid != pid
434  *	there must exist some pid in same session having pgid (EPERM)
435  * pid must not be session leader (EPERM)
436  */
437 #ifndef _SYS_SYSPROTO_H_
438 struct setpgid_args {
439 	int	pid;		/* target process id */
440 	int	pgid;		/* target pgrp id */
441 };
442 #endif
443 /* ARGSUSED */
444 int
sys_setpgid(struct thread * td,struct setpgid_args * uap)445 sys_setpgid(struct thread *td, struct setpgid_args *uap)
446 {
447 	struct proc *curp = td->td_proc;
448 	struct proc *targp;	/* target process */
449 	struct pgrp *pgrp;	/* target pgrp */
450 	int error;
451 	struct pgrp *newpgrp;
452 
453 	if (uap->pgid < 0)
454 		return (EINVAL);
455 
456 	newpgrp = uma_zalloc(pgrp_zone, M_WAITOK);
457 
458 again:
459 	error = 0;
460 
461 	sx_xlock(&proctree_lock);
462 	if (uap->pid != 0 && uap->pid != curp->p_pid) {
463 		if ((targp = pfind(uap->pid)) == NULL) {
464 			error = ESRCH;
465 			goto done;
466 		}
467 		if (!inferior(targp)) {
468 			PROC_UNLOCK(targp);
469 			error = ESRCH;
470 			goto done;
471 		}
472 		if ((error = p_cansee(td, targp))) {
473 			PROC_UNLOCK(targp);
474 			goto done;
475 		}
476 		if (targp->p_pgrp == NULL ||
477 		    targp->p_session != curp->p_session) {
478 			PROC_UNLOCK(targp);
479 			error = EPERM;
480 			goto done;
481 		}
482 		if (targp->p_flag & P_EXEC) {
483 			PROC_UNLOCK(targp);
484 			error = EACCES;
485 			goto done;
486 		}
487 		PROC_UNLOCK(targp);
488 	} else
489 		targp = curp;
490 	if (SESS_LEADER(targp)) {
491 		error = EPERM;
492 		goto done;
493 	}
494 	if (uap->pgid == 0)
495 		uap->pgid = targp->p_pid;
496 	if ((pgrp = pgfind(uap->pgid)) == NULL) {
497 		if (uap->pgid == targp->p_pid) {
498 			error = enterpgrp(targp, uap->pgid, newpgrp,
499 			    NULL);
500 			if (error == 0)
501 				newpgrp = NULL;
502 		} else
503 			error = EPERM;
504 	} else {
505 		if (pgrp == targp->p_pgrp) {
506 			PGRP_UNLOCK(pgrp);
507 			goto done;
508 		}
509 		if (pgrp->pg_id != targp->p_pid &&
510 		    pgrp->pg_session != curp->p_session) {
511 			PGRP_UNLOCK(pgrp);
512 			error = EPERM;
513 			goto done;
514 		}
515 		PGRP_UNLOCK(pgrp);
516 		error = enterthispgrp(targp, pgrp);
517 	}
518 done:
519 	KASSERT(error == 0 || newpgrp != NULL,
520 	    ("setpgid failed and newpgrp is NULL"));
521 	if (error == ERESTART)
522 		goto again;
523 	sx_xunlock(&proctree_lock);
524 	uma_zfree(pgrp_zone, newpgrp);
525 	return (error);
526 }
527 
528 static int
gidp_cmp(const void * p1,const void * p2)529 gidp_cmp(const void *p1, const void *p2)
530 {
531 	const gid_t g1 = *(const gid_t *)p1;
532 	const gid_t g2 = *(const gid_t *)p2;
533 
534 	return ((g1 > g2) - (g1 < g2));
535 }
536 
537 /*
538  * 'smallgroups' must be an (uninitialized) array of length CRED_SMALLGROUPS_NB.
539  * Always sets 'sc_supp_groups', either to a valid kernel-space groups array
540  * (which may or may not be 'smallgroups'), or NULL if SETCREDF_SUPP_GROUPS was
541  * not specified or there are too many groups, or a buffer containing garbage on
542  * copyin() failure.  In the last two cases, 'sc_supp_groups_nb' is additionally
543  * set to 0 as a security measure.  'sc_supp_groups' must be freed (M_TEMP) if
544  * not equal to 'smallgroups' even on failure.
545  */
546 static int
user_setcred_copyin_supp_groups(struct setcred * const wcred,const u_int flags,gid_t * const smallgroups)547 user_setcred_copyin_supp_groups(struct setcred *const wcred,
548     const u_int flags, gid_t *const smallgroups)
549 {
550 	gid_t *groups;
551 	int error;
552 
553 	if ((flags & SETCREDF_SUPP_GROUPS) == 0) {
554 		error = 0;
555 		goto reset_groups_exit;
556 	}
557 
558 	/*
559 	 * Check the number of groups' limit right now in order to limit the
560 	 * amount of bytes to copy.
561 	 */
562 	if (wcred->sc_supp_groups_nb > ngroups_max) {
563 		error = EINVAL;
564 		goto reset_groups_exit;
565 	}
566 
567 	groups = wcred->sc_supp_groups_nb <= CRED_SMALLGROUPS_NB ?
568 	    smallgroups : malloc(wcred->sc_supp_groups_nb * sizeof(gid_t),
569 	    M_TEMP, M_WAITOK);
570 	error = copyin(wcred->sc_supp_groups, groups,
571 	    wcred->sc_supp_groups_nb * sizeof(gid_t));
572 	wcred->sc_supp_groups = groups;
573 
574 	if (error != 0) {
575 		wcred->sc_supp_groups_nb = 0;
576 		/*
577 		 * 'sc_supp_groups' must be freed by caller if not
578 		 * 'smallgroups'.
579 		 */
580 		return (error);
581 	}
582 
583 	return (0);
584 
585 reset_groups_exit:
586 	wcred->sc_supp_groups_nb = 0;
587 	wcred->sc_supp_groups = NULL;
588 	return (error);
589 }
590 
591 int
user_setcred(struct thread * td,const u_int flags,struct setcred * const wcred)592 user_setcred(struct thread *td, const u_int flags, struct setcred *const wcred)
593 {
594 #ifdef MAC
595 	struct mac mac;
596 	/* Pointer to 'struct mac' or 'struct mac32'. */
597 	void *umac;
598 #endif
599 	gid_t smallgroups[CRED_SMALLGROUPS_NB];
600 	int error;
601 
602 	/*
603 	 * As the only point of this wrapper function is to copyin() from
604 	 * userland, we only interpret the data pieces we need to perform this
605 	 * operation and defer further sanity checks to kern_setcred(), except
606 	 * that we redundantly check here that no unknown flags have been
607 	 * passed.
608 	 */
609 	if ((flags & ~SETCREDF_MASK) != 0)
610 		return (EINVAL);
611 
612 #ifdef MAC
613 	umac = wcred->sc_label;
614 #endif
615 	/* Also done on !MAC as a defensive measure. */
616 	wcred->sc_label = NULL;
617 
618 	/*
619 	 * Copy supplementary groups as needed.  There is no specific
620 	 * alternative for 32-bit compatibility as 'gid_t' has the same size
621 	 * everywhere.
622 	 */
623 	error = user_setcred_copyin_supp_groups(wcred, flags, smallgroups);
624 	if (error != 0)
625 		goto free_groups;
626 
627 #ifdef MAC
628 	if ((flags & SETCREDF_MAC_LABEL) != 0) {
629 		error = mac_label_copyin(umac, &mac, NULL);
630 		if (error != 0)
631 			goto free_groups;
632 		wcred->sc_label = &mac;
633 	}
634 #endif
635 
636 	error = kern_setcred(td, flags, wcred);
637 
638 #ifdef MAC
639 	if (wcred->sc_label != NULL)
640 		free_copied_label(wcred->sc_label);
641 #endif
642 
643 free_groups:
644 	if (wcred->sc_supp_groups != smallgroups)
645 		free(wcred->sc_supp_groups, M_TEMP);
646 
647 	return (error);
648 }
649 
650 #ifndef _SYS_SYSPROTO_H_
651 struct setcred_args {
652 	u_int			 flags;	/* Flags. */
653 	const struct setcred	*wcred;
654 	size_t			 size;	/* Passed 'setcred' structure length. */
655 };
656 #endif
657 /* ARGSUSED */
658 int
sys_setcred(struct thread * td,struct setcred_args * uap)659 sys_setcred(struct thread *td, struct setcred_args *uap)
660 {
661 	struct setcred wcred;
662 	int error;
663 
664 	if (uap->size != sizeof(wcred))
665 		return (EINVAL);
666 	error = copyin(uap->wcred, &wcred, sizeof(wcred));
667 	if (error != 0)
668 		return (error);
669 	return (user_setcred(td, uap->flags, &wcred));
670 }
671 
672 /*
673  * CAUTION: This function normalizes groups in 'wcred'.
674  */
675 int
kern_setcred(struct thread * const td,const u_int flags,struct setcred * const wcred)676 kern_setcred(struct thread *const td, const u_int flags,
677     struct setcred *const wcred)
678 {
679 	struct proc *const p = td->td_proc;
680 	struct ucred *new_cred, *old_cred, *to_free_cred = NULL;
681 	struct uidinfo *uip = NULL, *ruip = NULL;
682 #ifdef MAC
683 	void *mac_set_proc_data = NULL;
684 	bool proc_label_set = false;
685 #endif
686 	int error;
687 	bool cred_set = false;
688 
689 	/* Bail out on unrecognized flags. */
690 	if (flags & ~SETCREDF_MASK)
691 		return (EINVAL);
692 
693 	/*
694 	 * Part 1: We allocate and perform preparatory operations with no locks.
695 	 */
696 
697 	if ((flags & SETCREDF_SUPP_GROUPS) != 0 &&
698 	    wcred->sc_supp_groups_nb > ngroups_max)
699 			return (EINVAL);
700 
701 	if (flags & SETCREDF_MAC_LABEL) {
702 #ifdef MAC
703 		error = mac_set_proc_prepare(td, wcred->sc_label,
704 		    &mac_set_proc_data);
705 		if (error != 0)
706 			return (error);
707 #else
708 		return (ENOTSUP);
709 #endif
710 	}
711 
712 	if (flags & SETCREDF_UID) {
713 		AUDIT_ARG_EUID(wcred->sc_uid);
714 		uip = uifind(wcred->sc_uid);
715 	}
716 	if (flags & SETCREDF_RUID) {
717 		AUDIT_ARG_RUID(wcred->sc_ruid);
718 		ruip = uifind(wcred->sc_ruid);
719 	}
720 	if (flags & SETCREDF_SVUID)
721 		AUDIT_ARG_SUID(wcred->sc_svuid);
722 
723 	if (flags & SETCREDF_GID)
724 		AUDIT_ARG_EGID(wcred->sc_gid);
725 	if (flags & SETCREDF_RGID)
726 		AUDIT_ARG_RGID(wcred->sc_rgid);
727 	if (flags & SETCREDF_SVGID)
728 		AUDIT_ARG_SGID(wcred->sc_svgid);
729 	if (flags & SETCREDF_SUPP_GROUPS) {
730 		/*
731 		 * Output the raw supplementary groups array for better
732 		 * traceability.
733 		 */
734 		AUDIT_ARG_GROUPSET(wcred->sc_supp_groups,
735 		    wcred->sc_supp_groups_nb);
736 		groups_normalize(&wcred->sc_supp_groups_nb,
737 		    wcred->sc_supp_groups);
738 	}
739 
740 	/*
741 	 * We first completely build the new credentials and only then pass them
742 	 * to MAC along with the old ones so that modules can check whether the
743 	 * requested transition is allowed.
744 	 */
745 	new_cred = crget();
746 	to_free_cred = new_cred;
747 	if (flags & SETCREDF_SUPP_GROUPS)
748 		crextend(new_cred, wcred->sc_supp_groups_nb);
749 
750 #ifdef MAC
751 	mac_cred_setcred_enter();
752 #endif
753 
754 	/*
755 	 * Part 2: We grab the process lock as to have a stable view of its
756 	 * current credentials, and prepare a copy of them with the requested
757 	 * changes applied under that lock.
758 	 */
759 
760 	PROC_LOCK(p);
761 	old_cred = crcopysafe(p, new_cred);
762 
763 	/*
764 	 * Change user IDs.
765 	 */
766 	if (flags & SETCREDF_UID)
767 		change_euid(new_cred, uip);
768 	if (flags & SETCREDF_RUID)
769 		change_ruid(new_cred, ruip);
770 	if (flags & SETCREDF_SVUID)
771 		change_svuid(new_cred, wcred->sc_svuid);
772 
773 	/*
774 	 * Change groups.
775 	 */
776 	if (flags & SETCREDF_SUPP_GROUPS)
777 		crsetgroups_internal(new_cred, wcred->sc_supp_groups_nb,
778 		    wcred->sc_supp_groups);
779 	if (flags & SETCREDF_GID)
780 		change_egid(new_cred, wcred->sc_gid);
781 	if (flags & SETCREDF_RGID)
782 		change_rgid(new_cred, wcred->sc_rgid);
783 	if (flags & SETCREDF_SVGID)
784 		change_svgid(new_cred, wcred->sc_svgid);
785 
786 #ifdef MAC
787 	/*
788 	 * Change the MAC label.
789 	 */
790 	if (flags & SETCREDF_MAC_LABEL) {
791 		error = mac_set_proc_core(td, new_cred, mac_set_proc_data);
792 		if (error != 0)
793 			goto unlock_finish;
794 		proc_label_set = true;
795 	}
796 
797 	/*
798 	 * MAC security modules checks.
799 	 */
800 	error = mac_cred_check_setcred(flags, old_cred, new_cred);
801 	if (error != 0)
802 		goto unlock_finish;
803 #endif
804 	/*
805 	 * Privilege check.
806 	 */
807 	error = priv_check_cred(old_cred, PRIV_CRED_SETCRED);
808 	if (error != 0)
809 		goto unlock_finish;
810 
811 #ifdef RACCT
812 	/*
813 	 * Hold a reference to 'new_cred', as we need to call some functions on
814 	 * it after proc_set_cred_enforce_proc_lim().
815 	 */
816 	crhold(new_cred);
817 #endif
818 
819 	/* Set the new credentials. */
820 	cred_set = proc_set_cred_enforce_proc_lim(p, new_cred);
821 	if (cred_set) {
822 		setsugid(p);
823 #ifdef RACCT
824 		/* Adjust RACCT counters. */
825 		racct_proc_ucred_changed(p, old_cred, new_cred);
826 #endif
827 		to_free_cred = old_cred;
828 		MPASS(error == 0);
829 	} else {
830 #ifdef RACCT
831 		/* Matches the crhold() just before the containing 'if'. */
832 		crfree(new_cred);
833 #endif
834 		error = EAGAIN;
835 	}
836 
837 unlock_finish:
838 	PROC_UNLOCK(p);
839 
840 	/*
841 	 * Part 3: After releasing the process lock, we perform cleanups and
842 	 * finishing operations.
843 	 */
844 
845 #ifdef RACCT
846 	if (cred_set) {
847 #ifdef RCTL
848 		rctl_proc_ucred_changed(p, new_cred);
849 #endif
850 		/* Paired with the crhold() above. */
851 		crfree(new_cred);
852 	}
853 #endif
854 
855 #ifdef MAC
856 	if (mac_set_proc_data != NULL)
857 		mac_set_proc_finish(td, proc_label_set, mac_set_proc_data);
858 	mac_cred_setcred_exit();
859 #endif
860 	crfree(to_free_cred);
861 	if (uip != NULL)
862 		uifree(uip);
863 	if (ruip != NULL)
864 		uifree(ruip);
865 
866 	return (error);
867 }
868 
869 /*
870  * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
871  * compatible.  It says that setting the uid/gid to euid/egid is a special
872  * case of "appropriate privilege".  Once the rules are expanded out, this
873  * basically means that setuid(nnn) sets all three id's, in all permitted
874  * cases unless _POSIX_SAVED_IDS is enabled.  In that case, setuid(getuid())
875  * does not set the saved id - this is dangerous for traditional BSD
876  * programs.  For this reason, we *really* do not want to set
877  * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
878  */
879 #define POSIX_APPENDIX_B_4_2_2
880 
881 #ifndef _SYS_SYSPROTO_H_
882 struct setuid_args {
883 	uid_t	uid;
884 };
885 #endif
886 /* ARGSUSED */
887 int
sys_setuid(struct thread * td,struct setuid_args * uap)888 sys_setuid(struct thread *td, struct setuid_args *uap)
889 {
890 	struct proc *p = td->td_proc;
891 	struct ucred *newcred, *oldcred;
892 	uid_t uid;
893 	struct uidinfo *uip;
894 	int error;
895 
896 	uid = uap->uid;
897 	AUDIT_ARG_UID(uid);
898 	newcred = crget();
899 	uip = uifind(uid);
900 	PROC_LOCK(p);
901 	/*
902 	 * Copy credentials so other references do not see our changes.
903 	 */
904 	oldcred = crcopysafe(p, newcred);
905 
906 #ifdef MAC
907 	error = mac_cred_check_setuid(oldcred, uid);
908 	if (error)
909 		goto fail;
910 #endif
911 
912 	/*
913 	 * See if we have "permission" by POSIX 1003.1 rules.
914 	 *
915 	 * Note that setuid(geteuid()) is a special case of
916 	 * "appropriate privileges" in appendix B.4.2.2.  We need
917 	 * to use this clause to be compatible with traditional BSD
918 	 * semantics.  Basically, it means that "setuid(xx)" sets all
919 	 * three id's (assuming you have privs).
920 	 *
921 	 * Notes on the logic.  We do things in three steps.
922 	 * 1: We determine if the euid is going to change, and do EPERM
923 	 *    right away.  We unconditionally change the euid later if this
924 	 *    test is satisfied, simplifying that part of the logic.
925 	 * 2: We determine if the real and/or saved uids are going to
926 	 *    change.  Determined by compile options.
927 	 * 3: Change euid last. (after tests in #2 for "appropriate privs")
928 	 */
929 	if (uid != oldcred->cr_ruid &&		/* allow setuid(getuid()) */
930 #ifdef _POSIX_SAVED_IDS
931 	    uid != oldcred->cr_svuid &&		/* allow setuid(saved gid) */
932 #endif
933 #ifdef POSIX_APPENDIX_B_4_2_2	/* Use BSD-compat clause from B.4.2.2 */
934 	    uid != oldcred->cr_uid &&		/* allow setuid(geteuid()) */
935 #endif
936 	    (error = priv_check_cred(oldcred, PRIV_CRED_SETUID)) != 0)
937 		goto fail;
938 
939 #ifdef _POSIX_SAVED_IDS
940 	/*
941 	 * Do we have "appropriate privileges" (are we root or uid == euid)
942 	 * If so, we are changing the real uid and/or saved uid.
943 	 */
944 	if (
945 #ifdef POSIX_APPENDIX_B_4_2_2	/* Use the clause from B.4.2.2 */
946 	    uid == oldcred->cr_uid ||
947 #endif
948 	    /* We are using privs. */
949 	    priv_check_cred(oldcred, PRIV_CRED_SETUID) == 0)
950 #endif
951 	{
952 		/*
953 		 * Set the real uid.
954 		 */
955 		if (uid != oldcred->cr_ruid) {
956 			change_ruid(newcred, uip);
957 			setsugid(p);
958 		}
959 		/*
960 		 * Set saved uid
961 		 *
962 		 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
963 		 * the security of seteuid() depends on it.  B.4.2.2 says it
964 		 * is important that we should do this.
965 		 */
966 		if (uid != oldcred->cr_svuid) {
967 			change_svuid(newcred, uid);
968 			setsugid(p);
969 		}
970 	}
971 
972 	/*
973 	 * In all permitted cases, we are changing the euid.
974 	 */
975 	if (uid != oldcred->cr_uid) {
976 		change_euid(newcred, uip);
977 		setsugid(p);
978 	}
979 
980 #ifdef RACCT
981 	racct_proc_ucred_changed(p, oldcred, newcred);
982 #endif
983 #ifdef RCTL
984 	crhold(newcred);
985 #endif
986 	/*
987 	 * Takes over 'newcred''s reference, so 'newcred' must not be used
988 	 * besides this point except on RCTL where we took an additional
989 	 * reference above.
990 	 */
991 	proc_set_cred(p, newcred);
992 	PROC_UNLOCK(p);
993 #ifdef RCTL
994 	rctl_proc_ucred_changed(p, newcred);
995 	crfree(newcred);
996 #endif
997 	uifree(uip);
998 	crfree(oldcred);
999 	return (0);
1000 
1001 fail:
1002 	PROC_UNLOCK(p);
1003 	uifree(uip);
1004 	crfree(newcred);
1005 	return (error);
1006 }
1007 
1008 #ifndef _SYS_SYSPROTO_H_
1009 struct seteuid_args {
1010 	uid_t	euid;
1011 };
1012 #endif
1013 /* ARGSUSED */
1014 int
sys_seteuid(struct thread * td,struct seteuid_args * uap)1015 sys_seteuid(struct thread *td, struct seteuid_args *uap)
1016 {
1017 	struct proc *p = td->td_proc;
1018 	struct ucred *newcred, *oldcred;
1019 	uid_t euid;
1020 	struct uidinfo *euip;
1021 	int error;
1022 
1023 	euid = uap->euid;
1024 	AUDIT_ARG_EUID(euid);
1025 	newcred = crget();
1026 	euip = uifind(euid);
1027 	PROC_LOCK(p);
1028 	execve_block_pass(td);
1029 
1030 	/*
1031 	 * Copy credentials so other references do not see our changes.
1032 	 */
1033 	oldcred = crcopysafe(p, newcred);
1034 
1035 #ifdef MAC
1036 	error = mac_cred_check_seteuid(oldcred, euid);
1037 	if (error)
1038 		goto fail;
1039 #endif
1040 
1041 	if (euid != oldcred->cr_ruid &&		/* allow seteuid(getuid()) */
1042 	    euid != oldcred->cr_svuid &&	/* allow seteuid(saved uid) */
1043 	    (error = priv_check_cred(oldcred, PRIV_CRED_SETEUID)) != 0)
1044 		goto fail;
1045 
1046 	/*
1047 	 * Everything's okay, do it.
1048 	 */
1049 	if (oldcred->cr_uid != euid) {
1050 		change_euid(newcred, euip);
1051 		setsugid(p);
1052 	}
1053 	proc_set_cred(p, newcred);
1054 	PROC_UNLOCK(p);
1055 	uifree(euip);
1056 	crfree(oldcred);
1057 	return (0);
1058 
1059 fail:
1060 	PROC_UNLOCK(p);
1061 	uifree(euip);
1062 	crfree(newcred);
1063 	return (error);
1064 }
1065 
1066 #ifndef _SYS_SYSPROTO_H_
1067 struct setgid_args {
1068 	gid_t	gid;
1069 };
1070 #endif
1071 /* ARGSUSED */
1072 int
sys_setgid(struct thread * td,struct setgid_args * uap)1073 sys_setgid(struct thread *td, struct setgid_args *uap)
1074 {
1075 	struct proc *p = td->td_proc;
1076 	struct ucred *newcred, *oldcred;
1077 	gid_t gid;
1078 	int error;
1079 
1080 	gid = uap->gid;
1081 	AUDIT_ARG_GID(gid);
1082 	newcred = crget();
1083 	PROC_LOCK(p);
1084 	execve_block_pass(td);
1085 	oldcred = crcopysafe(p, newcred);
1086 
1087 #ifdef MAC
1088 	error = mac_cred_check_setgid(oldcred, gid);
1089 	if (error)
1090 		goto fail;
1091 #endif
1092 
1093 	/*
1094 	 * See if we have "permission" by POSIX 1003.1 rules.
1095 	 *
1096 	 * Note that setgid(getegid()) is a special case of
1097 	 * "appropriate privileges" in appendix B.4.2.2.  We need
1098 	 * to use this clause to be compatible with traditional BSD
1099 	 * semantics.  Basically, it means that "setgid(xx)" sets all
1100 	 * three id's (assuming you have privs).
1101 	 *
1102 	 * For notes on the logic here, see setuid() above.
1103 	 */
1104 	if (gid != oldcred->cr_rgid &&		/* allow setgid(getgid()) */
1105 #ifdef _POSIX_SAVED_IDS
1106 	    gid != oldcred->cr_svgid &&		/* allow setgid(saved gid) */
1107 #endif
1108 #ifdef POSIX_APPENDIX_B_4_2_2	/* Use BSD-compat clause from B.4.2.2 */
1109 	    gid != oldcred->cr_gid && /* allow setgid(getegid()) */
1110 #endif
1111 	    (error = priv_check_cred(oldcred, PRIV_CRED_SETGID)) != 0)
1112 		goto fail;
1113 
1114 #ifdef _POSIX_SAVED_IDS
1115 	/*
1116 	 * Do we have "appropriate privileges" (are we root or gid == egid)
1117 	 * If so, we are changing the real uid and saved gid.
1118 	 */
1119 	if (
1120 #ifdef POSIX_APPENDIX_B_4_2_2	/* use the clause from B.4.2.2 */
1121 	    gid == oldcred->cr_gid ||
1122 #endif
1123 	    /* We are using privs. */
1124 	    priv_check_cred(oldcred, PRIV_CRED_SETGID) == 0)
1125 #endif
1126 	{
1127 		/*
1128 		 * Set real gid
1129 		 */
1130 		if (oldcred->cr_rgid != gid) {
1131 			change_rgid(newcred, gid);
1132 			setsugid(p);
1133 		}
1134 		/*
1135 		 * Set saved gid
1136 		 *
1137 		 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
1138 		 * the security of setegid() depends on it.  B.4.2.2 says it
1139 		 * is important that we should do this.
1140 		 */
1141 		if (oldcred->cr_svgid != gid) {
1142 			change_svgid(newcred, gid);
1143 			setsugid(p);
1144 		}
1145 	}
1146 	/*
1147 	 * In all cases permitted cases, we are changing the egid.
1148 	 * Copy credentials so other references do not see our changes.
1149 	 */
1150 	if (oldcred->cr_gid != gid) {
1151 		change_egid(newcred, gid);
1152 		setsugid(p);
1153 	}
1154 	proc_set_cred(p, newcred);
1155 	PROC_UNLOCK(p);
1156 	crfree(oldcred);
1157 	return (0);
1158 
1159 fail:
1160 	PROC_UNLOCK(p);
1161 	crfree(newcred);
1162 	return (error);
1163 }
1164 
1165 #ifndef _SYS_SYSPROTO_H_
1166 struct setegid_args {
1167 	gid_t	egid;
1168 };
1169 #endif
1170 /* ARGSUSED */
1171 int
sys_setegid(struct thread * td,struct setegid_args * uap)1172 sys_setegid(struct thread *td, struct setegid_args *uap)
1173 {
1174 	struct proc *p = td->td_proc;
1175 	struct ucred *newcred, *oldcred;
1176 	gid_t egid;
1177 	int error;
1178 
1179 	egid = uap->egid;
1180 	AUDIT_ARG_EGID(egid);
1181 	newcred = crget();
1182 	PROC_LOCK(p);
1183 	execve_block_pass(td);
1184 	oldcred = crcopysafe(p, newcred);
1185 
1186 #ifdef MAC
1187 	error = mac_cred_check_setegid(oldcred, egid);
1188 	if (error)
1189 		goto fail;
1190 #endif
1191 
1192 	if (egid != oldcred->cr_rgid &&		/* allow setegid(getgid()) */
1193 	    egid != oldcred->cr_svgid &&	/* allow setegid(saved gid) */
1194 	    (error = priv_check_cred(oldcred, PRIV_CRED_SETEGID)) != 0)
1195 		goto fail;
1196 
1197 	if (oldcred->cr_gid != egid) {
1198 		change_egid(newcred, egid);
1199 		setsugid(p);
1200 	}
1201 	proc_set_cred(p, newcred);
1202 	PROC_UNLOCK(p);
1203 	crfree(oldcred);
1204 	return (0);
1205 
1206 fail:
1207 	PROC_UNLOCK(p);
1208 	crfree(newcred);
1209 	return (error);
1210 }
1211 
1212 #ifdef COMPAT_FREEBSD14
1213 int
freebsd14_setgroups(struct thread * td,struct freebsd14_setgroups_args * uap)1214 freebsd14_setgroups(struct thread *td, struct freebsd14_setgroups_args *uap)
1215 {
1216 	gid_t smallgroups[CRED_SMALLGROUPS_NB];
1217 	gid_t *groups;
1218 	int gidsetsize, error;
1219 
1220 	/*
1221 	 * Before FreeBSD 15.0, we allow one more group to be supplied to
1222 	 * account for the egid appearing before the supplementary groups.  This
1223 	 * may technically allow one more supplementary group for systems that
1224 	 * did use the default NGROUPS_MAX if we round it back up to 1024.
1225 	 */
1226 	gidsetsize = uap->gidsetsize;
1227 	if (gidsetsize > ngroups_max + 1 || gidsetsize < 0)
1228 		return (EINVAL);
1229 
1230 	if (gidsetsize > CRED_SMALLGROUPS_NB)
1231 		groups = malloc(gidsetsize * sizeof(gid_t), M_TEMP, M_WAITOK);
1232 	else
1233 		groups = smallgroups;
1234 
1235 	error = copyin(uap->gidset, groups, gidsetsize * sizeof(gid_t));
1236 	if (error == 0) {
1237 		int ngroups = gidsetsize > 0 ? gidsetsize - 1 /* egid */ : 0;
1238 
1239 		error = kern_setgroups(td, &ngroups, groups + 1);
1240 		if (error == 0 && gidsetsize > 0)
1241 			td->td_proc->p_ucred->cr_gid = groups[0];
1242 	}
1243 
1244 	if (groups != smallgroups)
1245 		free(groups, M_TEMP);
1246 	return (error);
1247 }
1248 #endif	/* COMPAT_FREEBSD14 */
1249 
1250 #ifndef _SYS_SYSPROTO_H_
1251 struct setgroups_args {
1252 	int	gidsetsize;
1253 	gid_t	*gidset;
1254 };
1255 #endif
1256 /* ARGSUSED */
1257 int
sys_setgroups(struct thread * td,struct setgroups_args * uap)1258 sys_setgroups(struct thread *td, struct setgroups_args *uap)
1259 {
1260 	gid_t smallgroups[CRED_SMALLGROUPS_NB];
1261 	gid_t *groups;
1262 	int gidsetsize, error;
1263 
1264 	/*
1265 	 * Sanity check size now to avoid passing too big a value to copyin(),
1266 	 * even if kern_setgroups() will do it again.
1267 	 *
1268 	 * Ideally, the 'gidsetsize' argument should have been a 'u_int' (and it
1269 	 * was, in this implementation, for a long time), but POSIX standardized
1270 	 * getgroups() to take an 'int' and it would be quite entrapping to have
1271 	 * setgroups() differ.
1272 	 */
1273 	gidsetsize = uap->gidsetsize;
1274 	if (gidsetsize > ngroups_max || gidsetsize < 0)
1275 		return (EINVAL);
1276 
1277 	if (gidsetsize > CRED_SMALLGROUPS_NB)
1278 		groups = malloc(gidsetsize * sizeof(gid_t), M_TEMP, M_WAITOK);
1279 	else
1280 		groups = smallgroups;
1281 
1282 	error = copyin(uap->gidset, groups, gidsetsize * sizeof(gid_t));
1283 	if (error == 0)
1284 		error = kern_setgroups(td, &gidsetsize, groups);
1285 
1286 	if (groups != smallgroups)
1287 		free(groups, M_TEMP);
1288 	return (error);
1289 }
1290 
1291 /*
1292  * CAUTION: This function normalizes 'groups', possibly also changing the value
1293  * of '*ngrpp' as a consequence.
1294  */
1295 int
kern_setgroups(struct thread * td,int * ngrpp,gid_t * groups)1296 kern_setgroups(struct thread *td, int *ngrpp, gid_t *groups)
1297 {
1298 	struct proc *p = td->td_proc;
1299 	struct ucred *newcred, *oldcred;
1300 	int ngrp, error;
1301 
1302 	ngrp = *ngrpp;
1303 	/* Sanity check size. */
1304 	if (ngrp < 0 || ngrp > ngroups_max)
1305 		return (EINVAL);
1306 
1307 	AUDIT_ARG_GROUPSET(groups, ngrp);
1308 
1309 	groups_normalize(&ngrp, groups);
1310 	*ngrpp = ngrp;
1311 
1312 	newcred = crget();
1313 	crextend(newcred, ngrp);
1314 	PROC_LOCK(p);
1315 	execve_block_pass(td);
1316 	oldcred = crcopysafe(p, newcred);
1317 
1318 #ifdef MAC
1319 	/*
1320 	 * We pass NULL here explicitly if we don't have any supplementary
1321 	 * groups mostly for the sake of normalization, but also to avoid/detect
1322 	 * a situation where a MAC module has some assumption about the layout
1323 	 * of `groups` matching historical behavior.
1324 	 */
1325 	error = mac_cred_check_setgroups(oldcred, ngrp,
1326 	    ngrp == 0 ? NULL : groups);
1327 	if (error)
1328 		goto fail;
1329 #endif
1330 
1331 	error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS);
1332 	if (error)
1333 		goto fail;
1334 
1335 	crsetgroups_internal(newcred, ngrp, groups);
1336 	setsugid(p);
1337 	proc_set_cred(p, newcred);
1338 	PROC_UNLOCK(p);
1339 	crfree(oldcred);
1340 	return (0);
1341 
1342 fail:
1343 	PROC_UNLOCK(p);
1344 	crfree(newcred);
1345 	return (error);
1346 }
1347 
1348 #ifndef _SYS_SYSPROTO_H_
1349 struct setreuid_args {
1350 	uid_t	ruid;
1351 	uid_t	euid;
1352 };
1353 #endif
1354 /* ARGSUSED */
1355 int
sys_setreuid(struct thread * td,struct setreuid_args * uap)1356 sys_setreuid(struct thread *td, struct setreuid_args *uap)
1357 {
1358 	struct proc *p = td->td_proc;
1359 	struct ucred *newcred, *oldcred;
1360 	uid_t euid, ruid;
1361 	struct uidinfo *euip, *ruip;
1362 	int error;
1363 
1364 	euid = uap->euid;
1365 	ruid = uap->ruid;
1366 	AUDIT_ARG_EUID(euid);
1367 	AUDIT_ARG_RUID(ruid);
1368 	newcred = crget();
1369 	euip = uifind(euid);
1370 	ruip = uifind(ruid);
1371 	PROC_LOCK(p);
1372 	execve_block_pass(td);
1373 	oldcred = crcopysafe(p, newcred);
1374 
1375 #ifdef MAC
1376 	error = mac_cred_check_setreuid(oldcred, ruid, euid);
1377 	if (error)
1378 		goto fail;
1379 #endif
1380 
1381 	if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
1382 	      ruid != oldcred->cr_svuid) ||
1383 	     (euid != (uid_t)-1 && euid != oldcred->cr_uid &&
1384 	      euid != oldcred->cr_ruid && euid != oldcred->cr_svuid)) &&
1385 	    (error = priv_check_cred(oldcred, PRIV_CRED_SETREUID)) != 0)
1386 		goto fail;
1387 
1388 	if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
1389 		change_euid(newcred, euip);
1390 		setsugid(p);
1391 	}
1392 	if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
1393 		change_ruid(newcred, ruip);
1394 		setsugid(p);
1395 	}
1396 	if ((ruid != (uid_t)-1 || newcred->cr_uid != newcred->cr_ruid) &&
1397 	    newcred->cr_svuid != newcred->cr_uid) {
1398 		change_svuid(newcred, newcred->cr_uid);
1399 		setsugid(p);
1400 	}
1401 #ifdef RACCT
1402 	racct_proc_ucred_changed(p, oldcred, newcred);
1403 #endif
1404 #ifdef RCTL
1405 	crhold(newcred);
1406 #endif
1407 	/*
1408 	 * Takes over 'newcred''s reference, so 'newcred' must not be used
1409 	 * besides this point except on RCTL where we took an additional
1410 	 * reference above.
1411 	 */
1412 	proc_set_cred(p, newcred);
1413 	PROC_UNLOCK(p);
1414 #ifdef RCTL
1415 	rctl_proc_ucred_changed(p, newcred);
1416 	crfree(newcred);
1417 #endif
1418 	uifree(ruip);
1419 	uifree(euip);
1420 	crfree(oldcred);
1421 	return (0);
1422 
1423 fail:
1424 	PROC_UNLOCK(p);
1425 	uifree(ruip);
1426 	uifree(euip);
1427 	crfree(newcred);
1428 	return (error);
1429 }
1430 
1431 #ifndef _SYS_SYSPROTO_H_
1432 struct setregid_args {
1433 	gid_t	rgid;
1434 	gid_t	egid;
1435 };
1436 #endif
1437 /* ARGSUSED */
1438 int
sys_setregid(struct thread * td,struct setregid_args * uap)1439 sys_setregid(struct thread *td, struct setregid_args *uap)
1440 {
1441 	struct proc *p = td->td_proc;
1442 	struct ucred *newcred, *oldcred;
1443 	gid_t egid, rgid;
1444 	int error;
1445 
1446 	egid = uap->egid;
1447 	rgid = uap->rgid;
1448 	AUDIT_ARG_EGID(egid);
1449 	AUDIT_ARG_RGID(rgid);
1450 	newcred = crget();
1451 	PROC_LOCK(p);
1452 	execve_block_pass(td);
1453 	oldcred = crcopysafe(p, newcred);
1454 
1455 #ifdef MAC
1456 	error = mac_cred_check_setregid(oldcred, rgid, egid);
1457 	if (error)
1458 		goto fail;
1459 #endif
1460 
1461 	if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
1462 	    rgid != oldcred->cr_svgid) ||
1463 	     (egid != (gid_t)-1 && egid != oldcred->cr_gid &&
1464 	     egid != oldcred->cr_rgid && egid != oldcred->cr_svgid)) &&
1465 	    (error = priv_check_cred(oldcred, PRIV_CRED_SETREGID)) != 0)
1466 		goto fail;
1467 
1468 	if (egid != (gid_t)-1 && oldcred->cr_gid != egid) {
1469 		change_egid(newcred, egid);
1470 		setsugid(p);
1471 	}
1472 	if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
1473 		change_rgid(newcred, rgid);
1474 		setsugid(p);
1475 	}
1476 	if ((rgid != (gid_t)-1 || newcred->cr_gid != newcred->cr_rgid) &&
1477 	    newcred->cr_svgid != newcred->cr_gid) {
1478 		change_svgid(newcred, newcred->cr_gid);
1479 		setsugid(p);
1480 	}
1481 	proc_set_cred(p, newcred);
1482 	PROC_UNLOCK(p);
1483 	crfree(oldcred);
1484 	return (0);
1485 
1486 fail:
1487 	PROC_UNLOCK(p);
1488 	crfree(newcred);
1489 	return (error);
1490 }
1491 
1492 /*
1493  * setresuid(ruid, euid, suid) is like setreuid except control over the saved
1494  * uid is explicit.
1495  */
1496 #ifndef _SYS_SYSPROTO_H_
1497 struct setresuid_args {
1498 	uid_t	ruid;
1499 	uid_t	euid;
1500 	uid_t	suid;
1501 };
1502 #endif
1503 /* ARGSUSED */
1504 int
sys_setresuid(struct thread * td,struct setresuid_args * uap)1505 sys_setresuid(struct thread *td, struct setresuid_args *uap)
1506 {
1507 	struct proc *p = td->td_proc;
1508 	struct ucred *newcred, *oldcred;
1509 	uid_t euid, ruid, suid;
1510 	struct uidinfo *euip, *ruip;
1511 	int error;
1512 
1513 	euid = uap->euid;
1514 	ruid = uap->ruid;
1515 	suid = uap->suid;
1516 	AUDIT_ARG_EUID(euid);
1517 	AUDIT_ARG_RUID(ruid);
1518 	AUDIT_ARG_SUID(suid);
1519 	newcred = crget();
1520 	euip = uifind(euid);
1521 	ruip = uifind(ruid);
1522 	PROC_LOCK(p);
1523 	execve_block_pass(td);
1524 	oldcred = crcopysafe(p, newcred);
1525 
1526 #ifdef MAC
1527 	error = mac_cred_check_setresuid(oldcred, ruid, euid, suid);
1528 	if (error)
1529 		goto fail;
1530 #endif
1531 
1532 	if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
1533 	     ruid != oldcred->cr_svuid &&
1534 	      ruid != oldcred->cr_uid) ||
1535 	     (euid != (uid_t)-1 && euid != oldcred->cr_ruid &&
1536 	    euid != oldcred->cr_svuid &&
1537 	      euid != oldcred->cr_uid) ||
1538 	     (suid != (uid_t)-1 && suid != oldcred->cr_ruid &&
1539 	    suid != oldcred->cr_svuid &&
1540 	      suid != oldcred->cr_uid)) &&
1541 	    (error = priv_check_cred(oldcred, PRIV_CRED_SETRESUID)) != 0)
1542 		goto fail;
1543 
1544 	if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
1545 		change_euid(newcred, euip);
1546 		setsugid(p);
1547 	}
1548 	if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
1549 		change_ruid(newcred, ruip);
1550 		setsugid(p);
1551 	}
1552 	if (suid != (uid_t)-1 && oldcred->cr_svuid != suid) {
1553 		change_svuid(newcred, suid);
1554 		setsugid(p);
1555 	}
1556 #ifdef RACCT
1557 	racct_proc_ucred_changed(p, oldcred, newcred);
1558 #endif
1559 #ifdef RCTL
1560 	crhold(newcred);
1561 #endif
1562 	/*
1563 	 * Takes over 'newcred''s reference, so 'newcred' must not be used
1564 	 * besides this point except on RCTL where we took an additional
1565 	 * reference above.
1566 	 */
1567 	proc_set_cred(p, newcred);
1568 	PROC_UNLOCK(p);
1569 #ifdef RCTL
1570 	rctl_proc_ucred_changed(p, newcred);
1571 	crfree(newcred);
1572 #endif
1573 	uifree(ruip);
1574 	uifree(euip);
1575 	crfree(oldcred);
1576 	return (0);
1577 
1578 fail:
1579 	PROC_UNLOCK(p);
1580 	uifree(ruip);
1581 	uifree(euip);
1582 	crfree(newcred);
1583 	return (error);
1584 
1585 }
1586 
1587 /*
1588  * setresgid(rgid, egid, sgid) is like setregid except control over the saved
1589  * gid is explicit.
1590  */
1591 #ifndef _SYS_SYSPROTO_H_
1592 struct setresgid_args {
1593 	gid_t	rgid;
1594 	gid_t	egid;
1595 	gid_t	sgid;
1596 };
1597 #endif
1598 /* ARGSUSED */
1599 int
sys_setresgid(struct thread * td,struct setresgid_args * uap)1600 sys_setresgid(struct thread *td, struct setresgid_args *uap)
1601 {
1602 	struct proc *p = td->td_proc;
1603 	struct ucred *newcred, *oldcred;
1604 	gid_t egid, rgid, sgid;
1605 	int error;
1606 
1607 	egid = uap->egid;
1608 	rgid = uap->rgid;
1609 	sgid = uap->sgid;
1610 	AUDIT_ARG_EGID(egid);
1611 	AUDIT_ARG_RGID(rgid);
1612 	AUDIT_ARG_SGID(sgid);
1613 	newcred = crget();
1614 	PROC_LOCK(p);
1615 	execve_block_pass(td);
1616 	oldcred = crcopysafe(p, newcred);
1617 
1618 #ifdef MAC
1619 	error = mac_cred_check_setresgid(oldcred, rgid, egid, sgid);
1620 	if (error)
1621 		goto fail;
1622 #endif
1623 
1624 	if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
1625 	      rgid != oldcred->cr_svgid &&
1626 	      rgid != oldcred->cr_gid) ||
1627 	     (egid != (gid_t)-1 && egid != oldcred->cr_rgid &&
1628 	      egid != oldcred->cr_svgid &&
1629 	      egid != oldcred->cr_gid) ||
1630 	     (sgid != (gid_t)-1 && sgid != oldcred->cr_rgid &&
1631 	      sgid != oldcred->cr_svgid &&
1632 	      sgid != oldcred->cr_gid)) &&
1633 	    (error = priv_check_cred(oldcred, PRIV_CRED_SETRESGID)) != 0)
1634 		goto fail;
1635 
1636 	if (egid != (gid_t)-1 && oldcred->cr_gid != egid) {
1637 		change_egid(newcred, egid);
1638 		setsugid(p);
1639 	}
1640 	if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
1641 		change_rgid(newcred, rgid);
1642 		setsugid(p);
1643 	}
1644 	if (sgid != (gid_t)-1 && oldcred->cr_svgid != sgid) {
1645 		change_svgid(newcred, sgid);
1646 		setsugid(p);
1647 	}
1648 	proc_set_cred(p, newcred);
1649 	PROC_UNLOCK(p);
1650 	crfree(oldcred);
1651 	return (0);
1652 
1653 fail:
1654 	PROC_UNLOCK(p);
1655 	crfree(newcred);
1656 	return (error);
1657 }
1658 
1659 #ifndef _SYS_SYSPROTO_H_
1660 struct getresuid_args {
1661 	uid_t	*ruid;
1662 	uid_t	*euid;
1663 	uid_t	*suid;
1664 };
1665 #endif
1666 /* ARGSUSED */
1667 int
sys_getresuid(struct thread * td,struct getresuid_args * uap)1668 sys_getresuid(struct thread *td, struct getresuid_args *uap)
1669 {
1670 	struct ucred *cred;
1671 	int error1 = 0, error2 = 0, error3 = 0;
1672 
1673 	cred = td->td_ucred;
1674 	if (uap->ruid)
1675 		error1 = copyout(&cred->cr_ruid,
1676 		    uap->ruid, sizeof(cred->cr_ruid));
1677 	if (uap->euid)
1678 		error2 = copyout(&cred->cr_uid,
1679 		    uap->euid, sizeof(cred->cr_uid));
1680 	if (uap->suid)
1681 		error3 = copyout(&cred->cr_svuid,
1682 		    uap->suid, sizeof(cred->cr_svuid));
1683 	return (error1 ? error1 : error2 ? error2 : error3);
1684 }
1685 
1686 #ifndef _SYS_SYSPROTO_H_
1687 struct getresgid_args {
1688 	gid_t	*rgid;
1689 	gid_t	*egid;
1690 	gid_t	*sgid;
1691 };
1692 #endif
1693 /* ARGSUSED */
1694 int
sys_getresgid(struct thread * td,struct getresgid_args * uap)1695 sys_getresgid(struct thread *td, struct getresgid_args *uap)
1696 {
1697 	struct ucred *cred;
1698 	int error1 = 0, error2 = 0, error3 = 0;
1699 
1700 	cred = td->td_ucred;
1701 	if (uap->rgid)
1702 		error1 = copyout(&cred->cr_rgid,
1703 		    uap->rgid, sizeof(cred->cr_rgid));
1704 	if (uap->egid)
1705 		error2 = copyout(&cred->cr_gid,
1706 		    uap->egid, sizeof(cred->cr_gid));
1707 	if (uap->sgid)
1708 		error3 = copyout(&cred->cr_svgid,
1709 		    uap->sgid, sizeof(cred->cr_svgid));
1710 	return (error1 ? error1 : error2 ? error2 : error3);
1711 }
1712 
1713 #ifndef _SYS_SYSPROTO_H_
1714 struct issetugid_args {
1715 	int dummy;
1716 };
1717 #endif
1718 /* ARGSUSED */
1719 int
sys_issetugid(struct thread * td,struct issetugid_args * uap)1720 sys_issetugid(struct thread *td, struct issetugid_args *uap)
1721 {
1722 	struct proc *p = td->td_proc;
1723 
1724 	/*
1725 	 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
1726 	 * we use P_SUGID because we consider changing the owners as
1727 	 * "tainting" as well.
1728 	 * This is significant for procs that start as root and "become"
1729 	 * a user without an exec - programs cannot know *everything*
1730 	 * that libc *might* have put in their data segment.
1731 	 */
1732 	td->td_retval[0] = (p->p_flag & P_SUGID) ? 1 : 0;
1733 	return (0);
1734 }
1735 
1736 int
sys___setugid(struct thread * td,struct __setugid_args * uap)1737 sys___setugid(struct thread *td, struct __setugid_args *uap)
1738 {
1739 #ifdef REGRESSION
1740 	struct proc *p;
1741 
1742 	p = td->td_proc;
1743 	switch (uap->flag) {
1744 	case 0:
1745 		PROC_LOCK(p);
1746 		p->p_flag &= ~P_SUGID;
1747 		PROC_UNLOCK(p);
1748 		return (0);
1749 	case 1:
1750 		PROC_LOCK(p);
1751 		p->p_flag |= P_SUGID;
1752 		PROC_UNLOCK(p);
1753 		return (0);
1754 	default:
1755 		return (EINVAL);
1756 	}
1757 #else /* !REGRESSION */
1758 
1759 	return (ENOSYS);
1760 #endif /* REGRESSION */
1761 }
1762 
1763 #ifdef INVARIANTS
1764 static void
groups_check_normalized(int ngrp,const gid_t * groups)1765 groups_check_normalized(int ngrp, const gid_t *groups)
1766 {
1767 	gid_t prev_g;
1768 
1769 	groups_check_positive_len(ngrp);
1770 	groups_check_max_len(ngrp);
1771 
1772 	if (ngrp <= 1)
1773 		return;
1774 
1775 	prev_g = groups[0];
1776 	for (int i = 1; i < ngrp; ++i) {
1777 		const gid_t g = groups[i];
1778 
1779 		if (prev_g >= g)
1780 			panic("%s: groups[%d] (%u) >= groups[%d] (%u)",
1781 			    __func__, i - 1, prev_g, i, g);
1782 		prev_g = g;
1783 	}
1784 }
1785 #else
1786 #define groups_check_normalized(...)
1787 #endif
1788 
1789 /*
1790  * Returns whether gid designates a supplementary group in cred.
1791  */
1792 bool
group_is_supplementary(const gid_t gid,const struct ucred * const cred)1793 group_is_supplementary(const gid_t gid, const struct ucred *const cred)
1794 {
1795 
1796 	groups_check_normalized(cred->cr_ngroups, cred->cr_groups);
1797 
1798 	/*
1799 	 * Perform a binary search of the supplementary groups.  This is
1800 	 * possible because we sort the groups in crsetgroups().
1801 	 */
1802 	return (bsearch(&gid, cred->cr_groups, cred->cr_ngroups,
1803 	    sizeof(gid), gidp_cmp) != NULL);
1804 }
1805 
1806 /*
1807  * Check if gid is a member of the (effective) group set (i.e., effective and
1808  * supplementary groups).
1809  */
1810 bool
groupmember(gid_t gid,const struct ucred * cred)1811 groupmember(gid_t gid, const struct ucred *cred)
1812 {
1813 
1814 	groups_check_positive_len(cred->cr_ngroups);
1815 
1816 	if (gid == cred->cr_gid)
1817 		return (true);
1818 
1819 	return (group_is_supplementary(gid, cred));
1820 }
1821 
1822 /*
1823  * Check if gid is a member of the real group set (i.e., real and supplementary
1824  * groups).
1825  */
1826 bool
realgroupmember(gid_t gid,const struct ucred * cred)1827 realgroupmember(gid_t gid, const struct ucred *cred)
1828 {
1829 	groups_check_positive_len(cred->cr_ngroups);
1830 
1831 	if (gid == cred->cr_rgid)
1832 		return (true);
1833 
1834 	return (group_is_supplementary(gid, cred));
1835 }
1836 
1837 /*
1838  * Test the active securelevel against a given level.  securelevel_gt()
1839  * implements (securelevel > level).  securelevel_ge() implements
1840  * (securelevel >= level).  Note that the logic is inverted -- these
1841  * functions return EPERM on "success" and 0 on "failure".
1842  *
1843  * Due to care taken when setting the securelevel, we know that no jail will
1844  * be less secure that its parent (or the physical system), so it is sufficient
1845  * to test the current jail only.
1846  *
1847  * XXXRW: Possibly since this has to do with privilege, it should move to
1848  * kern_priv.c.
1849  */
1850 int
securelevel_gt(struct ucred * cr,int level)1851 securelevel_gt(struct ucred *cr, int level)
1852 {
1853 
1854 	return (cr->cr_prison->pr_securelevel > level ? EPERM : 0);
1855 }
1856 
1857 int
securelevel_ge(struct ucred * cr,int level)1858 securelevel_ge(struct ucred *cr, int level)
1859 {
1860 
1861 	return (cr->cr_prison->pr_securelevel >= level ? EPERM : 0);
1862 }
1863 
1864 /*
1865  * 'see_other_uids' determines whether or not visibility of processes
1866  * and sockets with credentials holding different real uids is possible
1867  * using a variety of system MIBs.
1868  * XXX: data declarations should be together near the beginning of the file.
1869  */
1870 static int	see_other_uids = 1;
1871 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_uids, CTLFLAG_RW,
1872     &see_other_uids, 0,
1873     "Unprivileged processes may see subjects/objects with different real uid");
1874 
1875 /*-
1876  * Determine if u1 "can see" the subject specified by u2, according to the
1877  * 'see_other_uids' policy.
1878  * Returns: 0 for permitted, ESRCH otherwise
1879  * Locks: none
1880  * References: *u1 and *u2 must not change during the call
1881  *             u1 may equal u2, in which case only one reference is required
1882  */
1883 static int
cr_canseeotheruids(struct ucred * u1,struct ucred * u2)1884 cr_canseeotheruids(struct ucred *u1, struct ucred *u2)
1885 {
1886 
1887 	if (!see_other_uids && u1->cr_ruid != u2->cr_ruid) {
1888 		if (priv_check_cred(u1, PRIV_SEEOTHERUIDS) != 0)
1889 			return (ESRCH);
1890 	}
1891 	return (0);
1892 }
1893 
1894 /*
1895  * 'see_other_gids' determines whether or not visibility of processes
1896  * and sockets with credentials holding different real gids is possible
1897  * using a variety of system MIBs.
1898  * XXX: data declarations should be together near the beginning of the file.
1899  */
1900 static int	see_other_gids = 1;
1901 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_gids, CTLFLAG_RW,
1902     &see_other_gids, 0,
1903     "Unprivileged processes may see subjects/objects with different real gid");
1904 
1905 /*
1906  * Determine if u1 can "see" the subject specified by u2, according to the
1907  * 'see_other_gids' policy.
1908  * Returns: 0 for permitted, ESRCH otherwise
1909  * Locks: none
1910  * References: *u1 and *u2 must not change during the call
1911  *             u1 may equal u2, in which case only one reference is required
1912  */
1913 static int
cr_canseeothergids(struct ucred * u1,struct ucred * u2)1914 cr_canseeothergids(struct ucred *u1, struct ucred *u2)
1915 {
1916 	if (see_other_gids)
1917 		return (0);
1918 
1919 	/* Restriction in force. */
1920 
1921 	if (realgroupmember(u1->cr_rgid, u2))
1922 		return (0);
1923 
1924 	for (int i = 0; i < u1->cr_ngroups; i++)
1925 		if (realgroupmember(u1->cr_groups[i], u2))
1926 			return (0);
1927 
1928 	if (priv_check_cred(u1, PRIV_SEEOTHERGIDS) == 0)
1929 		return (0);
1930 
1931 	return (ESRCH);
1932 }
1933 
1934 /*
1935  * 'see_jail_proc' determines whether or not visibility of processes and
1936  * sockets with credentials holding different jail ids is possible using a
1937  * variety of system MIBs.
1938  *
1939  * XXX: data declarations should be together near the beginning of the file.
1940  */
1941 
1942 static int	see_jail_proc = 1;
1943 SYSCTL_INT(_security_bsd, OID_AUTO, see_jail_proc, CTLFLAG_RW,
1944     &see_jail_proc, 0,
1945     "Unprivileged processes may see subjects/objects with different jail ids");
1946 
1947 /*-
1948  * Determine if u1 "can see" the subject specified by u2, according to the
1949  * 'see_jail_proc' policy.
1950  * Returns: 0 for permitted, ESRCH otherwise
1951  * Locks: none
1952  * References: *u1 and *u2 must not change during the call
1953  *             u1 may equal u2, in which case only one reference is required
1954  */
1955 static int
cr_canseejailproc(struct ucred * u1,struct ucred * u2)1956 cr_canseejailproc(struct ucred *u1, struct ucred *u2)
1957 {
1958 	if (see_jail_proc || /* Policy deactivated. */
1959 	    u1->cr_prison == u2->cr_prison || /* Same jail. */
1960 	    priv_check_cred(u1, PRIV_SEEJAILPROC) == 0) /* Privileged. */
1961 		return (0);
1962 
1963 	return (ESRCH);
1964 }
1965 
1966 /*
1967  * Determine if u1 can tamper with the subject specified by u2, if they are in
1968  * different jails and 'unprivileged_parent_tampering' jail policy allows it.
1969  *
1970  * May be called if u1 and u2 are in the same jail, but it is expected that the
1971  * caller has already done a prison_check() prior to calling it.
1972  *
1973  * Returns: 0 for permitted, EPERM otherwise
1974  */
1975 static int
cr_can_tamper_with_subjail(struct ucred * u1,struct ucred * u2,int priv)1976 cr_can_tamper_with_subjail(struct ucred *u1, struct ucred *u2, int priv)
1977 {
1978 
1979 	MPASS(prison_check(u1, u2) == 0);
1980 	if (u1->cr_prison == u2->cr_prison)
1981 		return (0);
1982 
1983 	if (priv_check_cred(u1, priv) == 0)
1984 		return (0);
1985 
1986 	/*
1987 	 * Jails do not maintain a distinct UID space, so process visibility is
1988 	 * all that would control an unprivileged process' ability to tamper
1989 	 * with a process in a subjail by default if we did not have the
1990 	 * allow.unprivileged_parent_tampering knob to restrict it by default.
1991 	 */
1992 	if (prison_allow(u2, PR_ALLOW_UNPRIV_PARENT_TAMPER))
1993 		return (0);
1994 
1995 	return (EPERM);
1996 }
1997 
1998 /*
1999  * Helper for cr_cansee*() functions to abide by system-wide security.bsd.see_*
2000  * policies.  Determines if u1 "can see" u2 according to these policies.
2001  * Returns: 0 for permitted, ESRCH otherwise
2002  */
2003 int
cr_bsd_visible(struct ucred * u1,struct ucred * u2)2004 cr_bsd_visible(struct ucred *u1, struct ucred *u2)
2005 {
2006 	int error;
2007 
2008 	error = cr_canseeotheruids(u1, u2);
2009 	if (error != 0)
2010 		return (error);
2011 	error = cr_canseeothergids(u1, u2);
2012 	if (error != 0)
2013 		return (error);
2014 	error = cr_canseejailproc(u1, u2);
2015 	if (error != 0)
2016 		return (error);
2017 	return (0);
2018 }
2019 
2020 /*-
2021  * Determine if u1 "can see" the subject specified by u2.
2022  * Returns: 0 for permitted, an errno value otherwise
2023  * Locks: none
2024  * References: *u1 and *u2 must not change during the call
2025  *             u1 may equal u2, in which case only one reference is required
2026  */
2027 int
cr_cansee(struct ucred * u1,struct ucred * u2)2028 cr_cansee(struct ucred *u1, struct ucred *u2)
2029 {
2030 	int error;
2031 
2032 	if ((error = prison_check(u1, u2)))
2033 		return (error);
2034 #ifdef MAC
2035 	if ((error = mac_cred_check_visible(u1, u2)))
2036 		return (error);
2037 #endif
2038 	if ((error = cr_bsd_visible(u1, u2)))
2039 		return (error);
2040 	return (0);
2041 }
2042 
2043 /*-
2044  * Determine if td "can see" the subject specified by p.
2045  * Returns: 0 for permitted, an errno value otherwise
2046  * Locks: Sufficient locks to protect p->p_ucred must be held.  td really
2047  *        should be curthread.
2048  * References: td and p must be valid for the lifetime of the call
2049  */
2050 int
p_cansee(struct thread * td,struct proc * p)2051 p_cansee(struct thread *td, struct proc *p)
2052 {
2053 	/* Wrap cr_cansee() for all functionality. */
2054 	KASSERT(td == curthread, ("%s: td not curthread", __func__));
2055 	PROC_LOCK_ASSERT(p, MA_OWNED);
2056 
2057 	if (td->td_proc == p)
2058 		return (0);
2059 	return (cr_cansee(td->td_ucred, p->p_ucred));
2060 }
2061 
2062 /*
2063  * 'conservative_signals' prevents the delivery of a broad class of
2064  * signals by unprivileged processes to processes that have changed their
2065  * credentials since the last invocation of execve().  This can prevent
2066  * the leakage of cached information or retained privileges as a result
2067  * of a common class of signal-related vulnerabilities.  However, this
2068  * may interfere with some applications that expect to be able to
2069  * deliver these signals to peer processes after having given up
2070  * privilege.
2071  */
2072 static int	conservative_signals = 1;
2073 SYSCTL_INT(_security_bsd, OID_AUTO, conservative_signals, CTLFLAG_RW,
2074     &conservative_signals, 0, "Unprivileged processes prevented from "
2075     "sending certain signals to processes whose credentials have changed");
2076 /*-
2077  * Determine whether cred may deliver the specified signal to proc.
2078  * Returns: 0 for permitted, an errno value otherwise.
2079  * Locks: A lock must be held for proc.
2080  * References: cred and proc must be valid for the lifetime of the call.
2081  */
2082 int
cr_cansignal(struct ucred * cred,struct proc * proc,int signum)2083 cr_cansignal(struct ucred *cred, struct proc *proc, int signum)
2084 {
2085 	int error;
2086 
2087 	PROC_LOCK_ASSERT(proc, MA_OWNED);
2088 	/*
2089 	 * Jail semantics limit the scope of signalling to proc in the
2090 	 * same jail as cred, if cred is in jail.
2091 	 */
2092 	error = prison_check(cred, proc->p_ucred);
2093 	if (error)
2094 		return (error);
2095 #ifdef MAC
2096 	if ((error = mac_proc_check_signal(cred, proc, signum)))
2097 		return (error);
2098 #endif
2099 	if ((error = cr_bsd_visible(cred, proc->p_ucred)))
2100 		return (error);
2101 
2102 	/*
2103 	 * UNIX signal semantics depend on the status of the P_SUGID
2104 	 * bit on the target process.  If the bit is set, then additional
2105 	 * restrictions are placed on the set of available signals.
2106 	 */
2107 	if (conservative_signals && (proc->p_flag & P_SUGID)) {
2108 		switch (signum) {
2109 		case 0:
2110 		case SIGKILL:
2111 		case SIGINT:
2112 		case SIGTERM:
2113 		case SIGALRM:
2114 		case SIGSTOP:
2115 		case SIGTTIN:
2116 		case SIGTTOU:
2117 		case SIGTSTP:
2118 		case SIGHUP:
2119 		case SIGUSR1:
2120 		case SIGUSR2:
2121 			/*
2122 			 * Generally, permit job and terminal control
2123 			 * signals.
2124 			 */
2125 			break;
2126 		default:
2127 			/* Not permitted without privilege. */
2128 			error = priv_check_cred(cred, PRIV_SIGNAL_SUGID);
2129 			if (error)
2130 				return (error);
2131 		}
2132 	}
2133 
2134 	/*
2135 	 * Generally, the target credential's ruid or svuid must match the
2136 	 * subject credential's ruid or euid.
2137 	 */
2138 	if (cred->cr_ruid != proc->p_ucred->cr_ruid &&
2139 	    cred->cr_ruid != proc->p_ucred->cr_svuid &&
2140 	    cred->cr_uid != proc->p_ucred->cr_ruid &&
2141 	    cred->cr_uid != proc->p_ucred->cr_svuid) {
2142 		error = priv_check_cred(cred, PRIV_SIGNAL_DIFFCRED);
2143 		if (error)
2144 			return (error);
2145 	}
2146 
2147 	/*
2148 	 * At this point, the target may be in a different jail than the
2149 	 * subject -- the subject must be in a parent jail to the target,
2150 	 * whether it is prison0 or a subordinate of prison0 that has
2151 	 * children.  Additional privileges are required to allow this, as
2152 	 * whether the creds are truly equivalent or not must be determined on
2153 	 * a case-by-case basis.
2154 	 */
2155 	error = cr_can_tamper_with_subjail(cred, proc->p_ucred,
2156 	    PRIV_SIGNAL_DIFFJAIL);
2157 	if (error)
2158 		return (error);
2159 
2160 	return (0);
2161 }
2162 
2163 /*-
2164  * Determine whether td may deliver the specified signal to p.
2165  * Returns: 0 for permitted, an errno value otherwise
2166  * Locks: Sufficient locks to protect various components of td and p
2167  *        must be held.  td must be curthread, and a lock must be
2168  *        held for p.
2169  * References: td and p must be valid for the lifetime of the call
2170  */
2171 int
p_cansignal(struct thread * td,struct proc * p,int signum)2172 p_cansignal(struct thread *td, struct proc *p, int signum)
2173 {
2174 
2175 	KASSERT(td == curthread, ("%s: td not curthread", __func__));
2176 	PROC_LOCK_ASSERT(p, MA_OWNED);
2177 	if (td->td_proc == p)
2178 		return (0);
2179 
2180 	/*
2181 	 * UNIX signalling semantics require that processes in the same
2182 	 * session always be able to deliver SIGCONT to one another,
2183 	 * overriding the remaining protections.
2184 	 */
2185 	/* XXX: This will require an additional lock of some sort. */
2186 	if (signum == SIGCONT && td->td_proc->p_session == p->p_session)
2187 		return (0);
2188 	/*
2189 	 * Some compat layers use SIGTHR and higher signals for
2190 	 * communication between different kernel threads of the same
2191 	 * process, so that they expect that it's always possible to
2192 	 * deliver them, even for suid applications where cr_cansignal() can
2193 	 * deny such ability for security consideration.  It should be
2194 	 * pretty safe to do since the only way to create two processes
2195 	 * with the same p_leader is via rfork(2).
2196 	 */
2197 	if (td->td_proc->p_leader != NULL && signum >= SIGTHR &&
2198 	    signum < SIGTHR + 4 && td->td_proc->p_leader == p->p_leader)
2199 		return (0);
2200 
2201 	return (cr_cansignal(td->td_ucred, p, signum));
2202 }
2203 
2204 /*-
2205  * Determine whether td may reschedule p.
2206  * Returns: 0 for permitted, an errno value otherwise
2207  * Locks: Sufficient locks to protect various components of td and p
2208  *        must be held.  td must be curthread, and a lock must
2209  *        be held for p.
2210  * References: td and p must be valid for the lifetime of the call
2211  */
2212 int
p_cansched(struct thread * td,struct proc * p)2213 p_cansched(struct thread *td, struct proc *p)
2214 {
2215 	int error;
2216 
2217 	KASSERT(td == curthread, ("%s: td not curthread", __func__));
2218 	PROC_LOCK_ASSERT(p, MA_OWNED);
2219 	if (td->td_proc == p)
2220 		return (0);
2221 	if ((error = prison_check(td->td_ucred, p->p_ucred)))
2222 		return (error);
2223 #ifdef MAC
2224 	if ((error = mac_proc_check_sched(td->td_ucred, p)))
2225 		return (error);
2226 #endif
2227 	if ((error = cr_bsd_visible(td->td_ucred, p->p_ucred)))
2228 		return (error);
2229 
2230 	if (td->td_ucred->cr_ruid != p->p_ucred->cr_ruid &&
2231 	    td->td_ucred->cr_uid != p->p_ucred->cr_ruid) {
2232 		error = priv_check(td, PRIV_SCHED_DIFFCRED);
2233 		if (error)
2234 			return (error);
2235 	}
2236 
2237 	error = cr_can_tamper_with_subjail(td->td_ucred, p->p_ucred,
2238 	    PRIV_SCHED_DIFFJAIL);
2239 	if (error)
2240 		return (error);
2241 
2242 	return (0);
2243 }
2244 
2245 /*
2246  * Handle getting or setting the prison's unprivileged_proc_debug
2247  * value.
2248  */
2249 static int
sysctl_unprivileged_proc_debug(SYSCTL_HANDLER_ARGS)2250 sysctl_unprivileged_proc_debug(SYSCTL_HANDLER_ARGS)
2251 {
2252 	int error, val;
2253 
2254 	val = prison_allow(req->td->td_ucred, PR_ALLOW_UNPRIV_DEBUG);
2255 	error = sysctl_handle_int(oidp, &val, 0, req);
2256 	if (error != 0 || req->newptr == NULL)
2257 		return (error);
2258 	if (val != 0 && val != 1)
2259 		return (EINVAL);
2260 	prison_set_allow(req->td->td_ucred, PR_ALLOW_UNPRIV_DEBUG, val);
2261 	return (0);
2262 }
2263 
2264 /*
2265  * The 'unprivileged_proc_debug' flag may be used to disable a variety of
2266  * unprivileged inter-process debugging services, including some procfs
2267  * functionality, ptrace(), and ktrace().  In the past, inter-process
2268  * debugging has been involved in a variety of security problems, and sites
2269  * not requiring the service might choose to disable it when hardening
2270  * systems.
2271  */
2272 SYSCTL_PROC(_security_bsd, OID_AUTO, unprivileged_proc_debug,
2273     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_SECURE |
2274     CTLFLAG_MPSAFE, 0, 0, sysctl_unprivileged_proc_debug, "I",
2275     "Unprivileged processes may use process debugging facilities");
2276 
2277 /*
2278  * Return true if the object owner/group ids are subset of the active
2279  * credentials.
2280  */
2281 bool
cr_xids_subset(struct ucred * active_cred,struct ucred * obj_cred)2282 cr_xids_subset(struct ucred *active_cred, struct ucred *obj_cred)
2283 {
2284 	int i;
2285 	bool grpsubset, uidsubset;
2286 
2287 	/*
2288 	 * Is p's group set a subset of td's effective group set?  This
2289 	 * includes p's egid, group access list, rgid, and svgid.
2290 	 */
2291 	grpsubset = true;
2292 	for (i = 0; i < obj_cred->cr_ngroups; i++) {
2293 		if (!groupmember(obj_cred->cr_groups[i], active_cred)) {
2294 			grpsubset = false;
2295 			break;
2296 		}
2297 	}
2298 	grpsubset = grpsubset &&
2299 	    groupmember(obj_cred->cr_gid, active_cred) &&
2300 	    groupmember(obj_cred->cr_rgid, active_cred) &&
2301 	    groupmember(obj_cred->cr_svgid, active_cred);
2302 
2303 	/*
2304 	 * Are the uids present in obj_cred's credential equal to
2305 	 * active_cred's effective uid?  This includes obj_cred's
2306 	 * euid, svuid, and ruid.
2307 	 */
2308 	uidsubset = (active_cred->cr_uid == obj_cred->cr_uid &&
2309 	    active_cred->cr_uid == obj_cred->cr_svuid &&
2310 	    active_cred->cr_uid == obj_cred->cr_ruid);
2311 
2312 	return (uidsubset && grpsubset);
2313 }
2314 
2315 /*
2316  * Determine whether the td thread allowed to do pdopenpid(2) on the
2317  * process p.  The permissions are scoped to the PIDs namespace and
2318  * processes hierarchy, and do not imply permissions to perform
2319  * operations on the resulting process descriptor, e.g. pdkill(2) and
2320  * other.
2321  */
2322 int
p_canopen(struct thread * td,struct proc * p)2323 p_canopen(struct thread *td, struct proc *p)
2324 {
2325 #ifdef INVARIANTS
2326 	if (IN_CAPABILITY_MODE(td))
2327 		sx_assert(&proctree_lock, SX_LOCKED);
2328 #endif
2329 
2330 	/*
2331 	 * Allow implicit parent in cap mode: either real parent or
2332 	 * debugger can open pid.
2333 	 */
2334 	if (!IN_CAPABILITY_MODE(td) || (allow_ptrace_in_cap_mode &&
2335 	    (td->td_proc == p->p_pptr || p->p_oppid == td->td_proc->p_pid)))
2336 		return (0);
2337 	return (ECAPMODE);
2338 }
2339 
2340 /*-
2341  * Determine whether td may debug p.
2342  * Returns: 0 for permitted, an errno value otherwise
2343  * Locks: Sufficient locks to protect various components of td and p
2344  *        must be held.  td must be curthread, and a lock must
2345  *        be held for p.
2346  * References: td and p must be valid for the lifetime of the call
2347  */
2348 int
p_candebug(struct thread * td,struct proc * p)2349 p_candebug(struct thread *td, struct proc *p)
2350 {
2351 	int error;
2352 
2353 	KASSERT(td == curthread, ("%s: td not curthread", __func__));
2354 	PROC_LOCK_ASSERT(p, MA_OWNED);
2355 	if (td->td_proc == p)
2356 		return (0);
2357 	if ((error = priv_check(td, PRIV_DEBUG_UNPRIV)))
2358 		return (error);
2359 	if ((error = prison_check(td->td_ucred, p->p_ucred)))
2360 		return (error);
2361 #ifdef MAC
2362 	if ((error = mac_proc_check_debug(td->td_ucred, p)))
2363 		return (error);
2364 #endif
2365 	if ((error = cr_bsd_visible(td->td_ucred, p->p_ucred)))
2366 		return (error);
2367 
2368 	/*
2369 	 * If p's gids aren't a subset, or the uids aren't a subset,
2370 	 * or the credential has changed, require appropriate privilege
2371 	 * for td to debug p.
2372 	 */
2373 	if (!cr_xids_subset(td->td_ucred, p->p_ucred)) {
2374 		error = priv_check(td, PRIV_DEBUG_DIFFCRED);
2375 		if (error)
2376 			return (error);
2377 	}
2378 
2379 	/*
2380 	 * Has the credential of the process changed since the last exec()?
2381 	 */
2382 	if ((p->p_flag & P_SUGID) != 0) {
2383 		error = priv_check(td, PRIV_DEBUG_SUGID);
2384 		if (error)
2385 			return (error);
2386 	}
2387 
2388 	error = cr_can_tamper_with_subjail(td->td_ucred, p->p_ucred,
2389 	    PRIV_DEBUG_DIFFJAIL);
2390 	if (error)
2391 		return (error);
2392 
2393 	/* Can't trace init when securelevel > 0. */
2394 	if (p == initproc) {
2395 		error = securelevel_gt(td->td_ucred, 0);
2396 		if (error)
2397 			return (error);
2398 	}
2399 
2400 	/*
2401 	 * Can't trace a process that's currently exec'ing.  Otherwise
2402 	 * the process vmspace might change, and the target might be
2403 	 * loading a setugid image.  The execve_block(9) and
2404 	 * proc_vmspace_ref(9) allow to get the stable credentials and
2405 	 * vmspace reference.
2406 	 */
2407 	if ((p->p_flag & P_INEXEC) != 0)
2408 		return (EBUSY);
2409 
2410 	/* Denied explicitly */
2411 	if ((p->p_flag2 & P2_NOTRACE) != 0) {
2412 		error = priv_check(td, PRIV_DEBUG_DENIED);
2413 		if (error != 0)
2414 			return (error);
2415 	}
2416 
2417 	return (0);
2418 }
2419 
2420 /*-
2421  * Determine whether the subject represented by cred can "see" a socket.
2422  * Returns: 0 for permitted, ENOENT otherwise.
2423  */
2424 int
cr_canseesocket(struct ucred * cred,struct socket * so)2425 cr_canseesocket(struct ucred *cred, struct socket *so)
2426 {
2427 	int error;
2428 
2429 	error = prison_check(cred, so->so_cred);
2430 	if (error)
2431 		return (ENOENT);
2432 #ifdef MAC
2433 	error = mac_socket_check_visible(cred, so);
2434 	if (error)
2435 		return (error);
2436 #endif
2437 	if (cr_bsd_visible(cred, so->so_cred))
2438 		return (ENOENT);
2439 
2440 	return (0);
2441 }
2442 
2443 /*-
2444  * Determine whether td can wait for the exit of p.
2445  * Returns: 0 for permitted, an errno value otherwise
2446  * Locks: Sufficient locks to protect various components of td and p
2447  *        must be held.  td must be curthread, and a lock must
2448  *        be held for p.
2449  * References: td and p must be valid for the lifetime of the call
2450 
2451  */
2452 int
p_canwait(struct thread * td,struct proc * p)2453 p_canwait(struct thread *td, struct proc *p)
2454 {
2455 	int error;
2456 
2457 	KASSERT(td == curthread, ("%s: td not curthread", __func__));
2458 	PROC_LOCK_ASSERT(p, MA_OWNED);
2459 	if ((error = prison_check(td->td_ucred, p->p_ucred)))
2460 		return (error);
2461 #ifdef MAC
2462 	if ((error = mac_proc_check_wait(td->td_ucred, p)))
2463 		return (error);
2464 #endif
2465 #if 0
2466 	/* XXXMAC: This could have odd effects on some shells. */
2467 	if ((error = cr_bsd_visible(td->td_ucred, p->p_ucred)))
2468 		return (error);
2469 #endif
2470 
2471 	return (0);
2472 }
2473 
2474 /*
2475  * Credential management.
2476  *
2477  * struct ucred objects are rarely allocated but gain and lose references all
2478  * the time (e.g., on struct file alloc/dealloc) turning refcount updates into
2479  * a significant source of cache-line ping ponging. Common cases are worked
2480  * around by modifying thread-local counter instead if the cred to operate on
2481  * matches td_realucred.
2482  *
2483  * The counter is split into 2 parts:
2484  * - cr_users -- total count of all struct proc and struct thread objects
2485  *   which have given cred in p_ucred and td_ucred respectively
2486  * - cr_ref -- the actual ref count, only valid if cr_users == 0
2487  *
2488  * If users == 0 then cr_ref behaves similarly to refcount(9), in particular if
2489  * the count reaches 0 the object is freeable.
2490  * If users > 0 and curthread->td_realucred == cred, then updates are performed
2491  * against td_ucredref.
2492  * In other cases updates are performed against cr_ref.
2493  *
2494  * Changing td_realucred into something else decrements cr_users and transfers
2495  * accumulated updates.
2496  */
2497 struct ucred *
crcowget(struct ucred * cr)2498 crcowget(struct ucred *cr)
2499 {
2500 
2501 	mtx_lock(&cr->cr_mtx);
2502 	KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
2503 	    __func__, cr->cr_users, cr));
2504 	cr->cr_users++;
2505 	cr->cr_ref++;
2506 	mtx_unlock(&cr->cr_mtx);
2507 	return (cr);
2508 }
2509 
2510 static struct ucred *
crunuse(struct thread * td)2511 crunuse(struct thread *td)
2512 {
2513 	struct ucred *cr, *crold;
2514 
2515 	MPASS(td->td_realucred == td->td_ucred);
2516 	cr = td->td_realucred;
2517 	mtx_lock(&cr->cr_mtx);
2518 	cr->cr_ref += td->td_ucredref;
2519 	td->td_ucredref = 0;
2520 	KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
2521 	    __func__, cr->cr_users, cr));
2522 	cr->cr_users--;
2523 	if (cr->cr_users == 0) {
2524 		KASSERT(cr->cr_ref > 0, ("%s: ref %ld not > 0 on cred %p",
2525 		    __func__, cr->cr_ref, cr));
2526 		crold = cr;
2527 	} else {
2528 		cr->cr_ref--;
2529 		crold = NULL;
2530 	}
2531 	mtx_unlock(&cr->cr_mtx);
2532 	td->td_realucred = NULL;
2533 	return (crold);
2534 }
2535 
2536 static void
crunusebatch(struct ucred * cr,u_int users,long ref)2537 crunusebatch(struct ucred *cr, u_int users, long ref)
2538 {
2539 
2540 	KASSERT(users > 0, ("%s: passed users %d not > 0 ; cred %p",
2541 	    __func__, users, cr));
2542 	mtx_lock(&cr->cr_mtx);
2543 	KASSERT(cr->cr_users >= users, ("%s: users %d not > %d on cred %p",
2544 	    __func__, cr->cr_users, users, cr));
2545 	cr->cr_users -= users;
2546 	cr->cr_ref += ref;
2547 	cr->cr_ref -= users;
2548 	if (cr->cr_users > 0) {
2549 		mtx_unlock(&cr->cr_mtx);
2550 		return;
2551 	}
2552 	KASSERT(cr->cr_ref >= 0, ("%s: ref %ld not >= 0 on cred %p",
2553 	    __func__, cr->cr_ref, cr));
2554 	if (cr->cr_ref > 0) {
2555 		mtx_unlock(&cr->cr_mtx);
2556 		return;
2557 	}
2558 	crfree_final(cr);
2559 }
2560 
2561 void
crcowfree(struct thread * td)2562 crcowfree(struct thread *td)
2563 {
2564 	struct ucred *cr;
2565 
2566 	cr = crunuse(td);
2567 	if (cr != NULL)
2568 		crfree(cr);
2569 }
2570 
2571 struct ucred *
crcowsync(void)2572 crcowsync(void)
2573 {
2574 	struct thread *td;
2575 	struct proc *p;
2576 	struct ucred *crnew, *crold;
2577 
2578 	td = curthread;
2579 	p = td->td_proc;
2580 	PROC_LOCK_ASSERT(p, MA_OWNED);
2581 
2582 	MPASS(td->td_realucred == td->td_ucred);
2583 	if (td->td_realucred == p->p_ucred)
2584 		return (NULL);
2585 
2586 	crnew = crcowget(p->p_ucred);
2587 	crold = crunuse(td);
2588 	td->td_realucred = crnew;
2589 	td->td_ucred = td->td_realucred;
2590 	return (crold);
2591 }
2592 
2593 /*
2594  * Batching.
2595  */
2596 void
credbatch_add(struct credbatch * crb,struct thread * td)2597 credbatch_add(struct credbatch *crb, struct thread *td)
2598 {
2599 	struct ucred *cr;
2600 
2601 	MPASS(td->td_realucred != NULL);
2602 	MPASS(td->td_realucred == td->td_ucred);
2603 	MPASS(TD_GET_STATE(td) == TDS_INACTIVE);
2604 	cr = td->td_realucred;
2605 	KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
2606 	    __func__, cr->cr_users, cr));
2607 	if (crb->cred != cr) {
2608 		if (crb->users > 0) {
2609 			MPASS(crb->cred != NULL);
2610 			crunusebatch(crb->cred, crb->users, crb->ref);
2611 			crb->users = 0;
2612 			crb->ref = 0;
2613 		}
2614 	}
2615 	crb->cred = cr;
2616 	crb->users++;
2617 	crb->ref += td->td_ucredref;
2618 	td->td_ucredref = 0;
2619 	td->td_realucred = NULL;
2620 }
2621 
2622 void
credbatch_final(struct credbatch * crb)2623 credbatch_final(struct credbatch *crb)
2624 {
2625 
2626 	MPASS(crb->cred != NULL);
2627 	MPASS(crb->users > 0);
2628 	crunusebatch(crb->cred, crb->users, crb->ref);
2629 }
2630 
2631 /*
2632  * Allocate a zeroed cred structure.
2633  */
2634 struct ucred *
crget(void)2635 crget(void)
2636 {
2637 	struct ucred *cr;
2638 
2639 	cr = malloc(sizeof(*cr), M_CRED, M_WAITOK | M_ZERO);
2640 	mtx_init(&cr->cr_mtx, "cred", NULL, MTX_DEF);
2641 	cr->cr_ref = 1;
2642 #ifdef AUDIT
2643 	audit_cred_init(cr);
2644 #endif
2645 #ifdef MAC
2646 	mac_cred_init(cr);
2647 #endif
2648 	cr->cr_groups = cr->cr_smallgroups;
2649 	cr->cr_agroups = nitems(cr->cr_smallgroups);
2650 	return (cr);
2651 }
2652 
2653 /*
2654  * Claim another reference to a ucred structure.
2655  */
2656 struct ucred *
crhold(struct ucred * cr)2657 crhold(struct ucred *cr)
2658 {
2659 	struct thread *td;
2660 
2661 	td = curthread;
2662 	if (__predict_true(td->td_realucred == cr)) {
2663 		KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
2664 		    __func__, cr->cr_users, cr));
2665 		td->td_ucredref++;
2666 		return (cr);
2667 	}
2668 	mtx_lock(&cr->cr_mtx);
2669 	cr->cr_ref++;
2670 	mtx_unlock(&cr->cr_mtx);
2671 	return (cr);
2672 }
2673 
2674 /*
2675  * Free a cred structure.  Throws away space when ref count gets to 0.
2676  */
2677 void
crfree(struct ucred * cr)2678 crfree(struct ucred *cr)
2679 {
2680 	struct thread *td;
2681 
2682 	td = curthread;
2683 	if (__predict_true(td->td_realucred == cr)) {
2684 		KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
2685 		    __func__, cr->cr_users, cr));
2686 		td->td_ucredref--;
2687 		return;
2688 	}
2689 	mtx_lock(&cr->cr_mtx);
2690 	KASSERT(cr->cr_users >= 0, ("%s: users %d not >= 0 on cred %p",
2691 	    __func__, cr->cr_users, cr));
2692 	cr->cr_ref--;
2693 	if (cr->cr_users > 0) {
2694 		mtx_unlock(&cr->cr_mtx);
2695 		return;
2696 	}
2697 	KASSERT(cr->cr_ref >= 0, ("%s: ref %ld not >= 0 on cred %p",
2698 	    __func__, cr->cr_ref, cr));
2699 	if (cr->cr_ref > 0) {
2700 		mtx_unlock(&cr->cr_mtx);
2701 		return;
2702 	}
2703 	crfree_final(cr);
2704 }
2705 
2706 static void
crfree_final(struct ucred * cr)2707 crfree_final(struct ucred *cr)
2708 {
2709 
2710 	KASSERT(cr->cr_users == 0, ("%s: users %d not == 0 on cred %p",
2711 	    __func__, cr->cr_users, cr));
2712 	KASSERT(cr->cr_ref == 0, ("%s: ref %ld not == 0 on cred %p",
2713 	    __func__, cr->cr_ref, cr));
2714 
2715 	/*
2716 	 * Some callers of crget(), such as nfs_statfs(), allocate a temporary
2717 	 * credential, but don't allocate a uidinfo structure.
2718 	 */
2719 	if (cr->cr_uidinfo != NULL)
2720 		uifree(cr->cr_uidinfo);
2721 	if (cr->cr_ruidinfo != NULL)
2722 		uifree(cr->cr_ruidinfo);
2723 	if (cr->cr_prison != NULL)
2724 		prison_free(cr->cr_prison);
2725 	if (cr->cr_loginclass != NULL)
2726 		loginclass_free(cr->cr_loginclass);
2727 #ifdef AUDIT
2728 	audit_cred_destroy(cr);
2729 #endif
2730 #ifdef MAC
2731 	mac_cred_destroy(cr);
2732 #endif
2733 	mtx_destroy(&cr->cr_mtx);
2734 	if (cr->cr_groups != cr->cr_smallgroups)
2735 		free(cr->cr_groups, M_CRED);
2736 	free(cr, M_CRED);
2737 }
2738 
2739 /*
2740  * Copy a ucred's contents from a template.  Does not block.
2741  */
2742 void
crcopy(struct ucred * dest,struct ucred * src)2743 crcopy(struct ucred *dest, struct ucred *src)
2744 {
2745 
2746 	bcopy(&src->cr_startcopy, &dest->cr_startcopy,
2747 	    (unsigned)((caddr_t)&src->cr_endcopy -
2748 		(caddr_t)&src->cr_startcopy));
2749 	dest->cr_flags = src->cr_flags;
2750 	crsetgroups(dest, src->cr_ngroups, src->cr_groups);
2751 	uihold(dest->cr_uidinfo);
2752 	uihold(dest->cr_ruidinfo);
2753 	prison_hold(dest->cr_prison);
2754 	loginclass_hold(dest->cr_loginclass);
2755 #ifdef AUDIT
2756 	audit_cred_copy(src, dest);
2757 #endif
2758 #ifdef MAC
2759 	mac_cred_copy(src, dest);
2760 #endif
2761 }
2762 
2763 /*
2764  * Dup cred struct to a new held one.
2765  */
2766 struct ucred *
crdup(struct ucred * cr)2767 crdup(struct ucred *cr)
2768 {
2769 	struct ucred *newcr;
2770 
2771 	newcr = crget();
2772 	crcopy(newcr, cr);
2773 	return (newcr);
2774 }
2775 
2776 /*
2777  * Fill in a struct xucred based on a struct ucred.
2778  */
2779 void
cru2x(struct ucred * cr,struct xucred * xcr)2780 cru2x(struct ucred *cr, struct xucred *xcr)
2781 {
2782 	int ngroups;
2783 
2784 	bzero(xcr, sizeof(*xcr));
2785 	xcr->cr_version = XUCRED_VERSION;
2786 	xcr->cr_uid = cr->cr_uid;
2787 	xcr->cr_gid = cr->cr_gid;
2788 
2789 	/*
2790 	 * We use a union to alias cr_gid to cr_groups[0] in the xucred, so
2791 	 * this is kind of ugly; cr_ngroups still includes the egid for our
2792 	 * purposes to avoid bumping the xucred version.
2793 	 */
2794 	ngroups = MIN(cr->cr_ngroups + 1, nitems(xcr->cr_groups));
2795 	xcr->cr_ngroups = ngroups;
2796 	bcopy(cr->cr_groups, xcr->cr_sgroups,
2797 	    (ngroups - 1) * sizeof(*cr->cr_groups));
2798 }
2799 
2800 void
cru2xt(struct thread * td,struct xucred * xcr)2801 cru2xt(struct thread *td, struct xucred *xcr)
2802 {
2803 
2804 	cru2x(td->td_ucred, xcr);
2805 	xcr->cr_pid = td->td_proc->p_pid;
2806 }
2807 
2808 /*
2809  * Change process credentials.
2810  *
2811  * Callers are responsible for providing the reference for passed credentials
2812  * and for freeing old ones.  Calls chgproccnt() to correctly account the
2813  * current process to the proper real UID, if the latter has changed.  Returns
2814  * whether the operation was successful.  Failure can happen only on
2815  * 'enforce_proc_lim' being true and if no new process can be accounted to the
2816  * new real UID because of the current limit (see the inner comment for more
2817  * details) and the caller does not have privilege (PRIV_PROC_LIMIT) to override
2818  * that.  In this case, the reference to 'newcred' is not taken over.
2819  */
2820 static bool
_proc_set_cred(struct proc * p,struct ucred * newcred,bool enforce_proc_lim)2821 _proc_set_cred(struct proc *p, struct ucred *newcred, bool enforce_proc_lim)
2822 {
2823 	struct ucred *const oldcred = p->p_ucred;
2824 
2825 	MPASS(oldcred != NULL);
2826 	PROC_LOCK_ASSERT(p, MA_OWNED);
2827 
2828 	if (newcred->cr_ruidinfo != oldcred->cr_ruidinfo) {
2829 		/*
2830 		 * XXXOC: This check is flawed but nonetheless the best we can
2831 		 * currently do as we don't really track limits per UID contrary
2832 		 * to what we pretend in setrlimit(2).  Until this is reworked,
2833 		 * we just check here that the number of processes for our new
2834 		 * real UID doesn't exceed this process' process number limit
2835 		 * (which is meant to be associated with the current real UID).
2836 		 */
2837 		const int proccnt_changed = chgproccnt(newcred->cr_ruidinfo, 1,
2838 		    enforce_proc_lim ? lim_cur_proc(p, RLIMIT_NPROC) : 0);
2839 
2840 		if (!proccnt_changed) {
2841 			if (priv_check_cred(oldcred, PRIV_PROC_LIMIT) != 0)
2842 				return (false);
2843 			(void)chgproccnt(newcred->cr_ruidinfo, 1, 0);
2844 		}
2845 	}
2846 
2847 	mtx_lock(&oldcred->cr_mtx);
2848 	KASSERT(oldcred->cr_users > 0, ("%s: users %d not > 0 on cred %p",
2849 	    __func__, oldcred->cr_users, oldcred));
2850 	oldcred->cr_users--;
2851 	mtx_unlock(&oldcred->cr_mtx);
2852 	mtx_lock(&newcred->cr_mtx);
2853 	newcred->cr_users++;
2854 	mtx_unlock(&newcred->cr_mtx);
2855 	p->p_ucred = newcred;
2856 	PROC_UPDATE_COW(p);
2857 	if (newcred->cr_ruidinfo != oldcred->cr_ruidinfo)
2858 		(void)chgproccnt(oldcred->cr_ruidinfo, -1, 0);
2859 	return (true);
2860 }
2861 
2862 void
proc_set_cred(struct proc * p,struct ucred * newcred)2863 proc_set_cred(struct proc *p, struct ucred *newcred)
2864 {
2865 	bool success __diagused = _proc_set_cred(p, newcred, false);
2866 
2867 	MPASS(success);
2868 }
2869 
2870 bool
proc_set_cred_enforce_proc_lim(struct proc * p,struct ucred * newcred)2871 proc_set_cred_enforce_proc_lim(struct proc *p, struct ucred *newcred)
2872 {
2873 	return (_proc_set_cred(p, newcred, true));
2874 }
2875 
2876 void
proc_unset_cred(struct proc * p,bool decrement_proc_count)2877 proc_unset_cred(struct proc *p, bool decrement_proc_count)
2878 {
2879 	struct ucred *cr;
2880 
2881 	MPASS(p->p_state == PRS_ZOMBIE || p->p_state == PRS_NEW);
2882 	cr = p->p_ucred;
2883 	p->p_ucred = NULL;
2884 	KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
2885 	    __func__, cr->cr_users, cr));
2886 	mtx_lock(&cr->cr_mtx);
2887 	cr->cr_users--;
2888 	if (cr->cr_users == 0)
2889 		KASSERT(cr->cr_ref > 0, ("%s: ref %ld not > 0 on cred %p",
2890 		    __func__, cr->cr_ref, cr));
2891 	mtx_unlock(&cr->cr_mtx);
2892 	if (decrement_proc_count)
2893 		(void)chgproccnt(cr->cr_ruidinfo, -1, 0);
2894 	crfree(cr);
2895 }
2896 
2897 struct ucred *
crcopysafe(struct proc * p,struct ucred * cr)2898 crcopysafe(struct proc *p, struct ucred *cr)
2899 {
2900 	struct ucred *oldcred;
2901 	int groups;
2902 
2903 	PROC_LOCK_ASSERT(p, MA_OWNED);
2904 
2905 	oldcred = p->p_ucred;
2906 	while (cr->cr_agroups < oldcred->cr_ngroups) {
2907 		groups = oldcred->cr_ngroups;
2908 		PROC_UNLOCK(p);
2909 		crextend(cr, groups);
2910 		PROC_LOCK(p);
2911 		oldcred = p->p_ucred;
2912 	}
2913 	crcopy(cr, oldcred);
2914 
2915 	return (oldcred);
2916 }
2917 
2918 /*
2919  * Extend the passed-in credentials to hold n groups.
2920  *
2921  * Must not be called after groups have been set.
2922  */
2923 void
crextend(struct ucred * cr,int n)2924 crextend(struct ucred *cr, int n)
2925 {
2926 	size_t nbytes;
2927 
2928 	MPASS2(cr->cr_ref == 1, "'cr_ref' must be 1 (referenced, unshared)");
2929 	MPASS2((cr->cr_flags & CRED_FLAG_GROUPSET) == 0,
2930 	    "groups on 'cr' already set!");
2931 	groups_check_positive_len(n);
2932 	groups_check_max_len(n);
2933 
2934 	if (n <= cr->cr_agroups)
2935 		return;
2936 
2937 	nbytes = n * sizeof(gid_t);
2938 	if (nbytes < n)
2939 		panic("Too many groups (memory size overflow)! "
2940 		    "Computation of 'kern.ngroups' should have prevented this, "
2941 		    "please fix it. In the meantime, reduce 'kern.ngroups'.");
2942 
2943 	/*
2944 	 * We allocate a power of 2 larger than 'nbytes', except when that
2945 	 * exceeds PAGE_SIZE, in which case we allocate the right multiple of
2946 	 * pages.  We assume PAGE_SIZE is a power of 2 (the call to roundup2()
2947 	 * below) but do not need to for sizeof(gid_t).
2948 	 */
2949 	if (nbytes < PAGE_SIZE) {
2950 		if (!powerof2(nbytes))
2951 			/* fls*() return a bit index starting at 1. */
2952 			nbytes = 1 << flsl(nbytes);
2953 	} else
2954 		nbytes = roundup2(nbytes, PAGE_SIZE);
2955 
2956 	/* Free the old array. */
2957 	if (cr->cr_groups != cr->cr_smallgroups)
2958 		free(cr->cr_groups, M_CRED);
2959 
2960 	cr->cr_groups = malloc(nbytes, M_CRED, M_WAITOK | M_ZERO);
2961 	cr->cr_agroups = nbytes / sizeof(gid_t);
2962 }
2963 
2964 /*
2965  * Normalizes a set of groups to be applied to a 'struct ucred'.
2966  *
2967  * Normalization ensures that the supplementary groups are sorted in ascending
2968  * order and do not contain duplicates.  This allows group_is_supplementary() to
2969  * do a binary search.
2970  */
2971 static void
groups_normalize(int * ngrp,gid_t * groups)2972 groups_normalize(int *ngrp, gid_t *groups)
2973 {
2974 	gid_t prev_g;
2975 	int ins_idx;
2976 
2977 	groups_check_positive_len(*ngrp);
2978 	groups_check_max_len(*ngrp);
2979 
2980 	if (*ngrp <= 1)
2981 		return;
2982 
2983 	qsort(groups, *ngrp, sizeof(*groups), gidp_cmp);
2984 
2985 	/* Remove duplicates. */
2986 	prev_g = groups[0];
2987 	ins_idx = 1;
2988 	for (int i = ins_idx; i < *ngrp; ++i) {
2989 		const gid_t g = groups[i];
2990 
2991 		if (g != prev_g) {
2992 			if (i != ins_idx)
2993 				groups[ins_idx] = g;
2994 			++ins_idx;
2995 			prev_g = g;
2996 		}
2997 	}
2998 	*ngrp = ins_idx;
2999 
3000 	groups_check_normalized(*ngrp, groups);
3001 }
3002 
3003 /*
3004  * Internal function copying groups into a credential.
3005  *
3006  * 'ngrp' must be strictly positive.  Either the passed 'groups' array must have
3007  * been normalized in advance (see groups_normalize()), else it must be so
3008  * before the structure is to be used again.
3009  *
3010  * This function is suitable to be used under any lock (it doesn't take any lock
3011  * itself nor sleep, and in particular doesn't allocate memory).  crextend()
3012  * must have been called beforehand to ensure sufficient space is available.
3013  * See also crsetgroups(), which handles that.
3014  */
3015 static void
crsetgroups_internal(struct ucred * cr,int ngrp,const gid_t * groups)3016 crsetgroups_internal(struct ucred *cr, int ngrp, const gid_t *groups)
3017 {
3018 
3019 	MPASS2(cr->cr_ref == 1, "'cr_ref' must be 1 (referenced, unshared)");
3020 	MPASS2(cr->cr_agroups >= ngrp, "'cr_agroups' too small");
3021 	groups_check_positive_len(ngrp);
3022 
3023 	bcopy(groups, cr->cr_groups, ngrp * sizeof(gid_t));
3024 	cr->cr_ngroups = ngrp;
3025 	cr->cr_flags |= CRED_FLAG_GROUPSET;
3026 }
3027 
3028 /*
3029  * Copy groups in to a credential after expanding it if required.
3030  *
3031  * May sleep in order to allocate memory (except if, e.g., crextend() was called
3032  * before with 'ngrp' or greater).  Truncates the list to 'ngroups_max' if
3033  * it is too large.  Array 'groups' doesn't need to be sorted.  'ngrp' must be
3034  * positive.
3035  */
3036 void
crsetgroups(struct ucred * cr,int ngrp,const gid_t * groups)3037 crsetgroups(struct ucred *cr, int ngrp, const gid_t *groups)
3038 {
3039 
3040 	if (ngrp > ngroups_max)
3041 		ngrp = ngroups_max;
3042 	cr->cr_ngroups = 0;
3043 	if (ngrp == 0) {
3044 		cr->cr_flags |= CRED_FLAG_GROUPSET;
3045 		return;
3046 	}
3047 
3048 	/*
3049 	 * crextend() asserts that groups are not set, as it may allocate a new
3050 	 * backing storage without copying the content of the old one.  Since we
3051 	 * are going to install a completely new set anyway, signal that we
3052 	 * consider the old ones thrown away.
3053 	 */
3054 	cr->cr_flags &= ~CRED_FLAG_GROUPSET;
3055 
3056 	crextend(cr, ngrp);
3057 	crsetgroups_internal(cr, ngrp, groups);
3058 	groups_normalize(&cr->cr_ngroups, cr->cr_groups);
3059 }
3060 
3061 /*
3062  * Same as crsetgroups() but sets the effective GID as well.
3063  *
3064  * This function ensures that an effective GID is always present in credentials.
3065  * An empty array will only set the effective GID to 'default_egid', while
3066  * a non-empty array will peel off groups[0] to set as the effective GID and use
3067  * the remainder, if any, as supplementary groups.
3068  */
3069 void
crsetgroups_and_egid(struct ucred * cr,int ngrp,const gid_t * groups,const gid_t default_egid)3070 crsetgroups_and_egid(struct ucred *cr, int ngrp, const gid_t *groups,
3071     const gid_t default_egid)
3072 {
3073 	if (ngrp == 0) {
3074 		cr->cr_gid = default_egid;
3075 		cr->cr_ngroups = 0;
3076 		cr->cr_flags |= CRED_FLAG_GROUPSET;
3077 		return;
3078 	}
3079 
3080 	crsetgroups(cr, ngrp - 1, groups + 1);
3081 	cr->cr_gid = groups[0];
3082 }
3083 
3084 /*
3085  * Get login name, if available.
3086  */
3087 #ifndef _SYS_SYSPROTO_H_
3088 struct getlogin_args {
3089 	char	*namebuf;
3090 	u_int	namelen;
3091 };
3092 #endif
3093 /* ARGSUSED */
3094 int
sys_getlogin(struct thread * td,struct getlogin_args * uap)3095 sys_getlogin(struct thread *td, struct getlogin_args *uap)
3096 {
3097 	char login[MAXLOGNAME];
3098 	struct proc *p = td->td_proc;
3099 	size_t len;
3100 
3101 	if (uap->namelen > MAXLOGNAME)
3102 		uap->namelen = MAXLOGNAME;
3103 	PROC_LOCK(p);
3104 	SESS_LOCK(p->p_session);
3105 	len = strlcpy(login, p->p_session->s_login, uap->namelen) + 1;
3106 	SESS_UNLOCK(p->p_session);
3107 	PROC_UNLOCK(p);
3108 	if (len > uap->namelen)
3109 		return (ERANGE);
3110 	return (copyout(login, uap->namebuf, len));
3111 }
3112 
3113 /*
3114  * Set login name.
3115  */
3116 #ifndef _SYS_SYSPROTO_H_
3117 struct setlogin_args {
3118 	char	*namebuf;
3119 };
3120 #endif
3121 /* ARGSUSED */
3122 int
sys_setlogin(struct thread * td,struct setlogin_args * uap)3123 sys_setlogin(struct thread *td, struct setlogin_args *uap)
3124 {
3125 	struct proc *p = td->td_proc;
3126 	int error;
3127 	char logintmp[MAXLOGNAME];
3128 
3129 	CTASSERT(sizeof(p->p_session->s_login) >= sizeof(logintmp));
3130 
3131 	error = priv_check(td, PRIV_PROC_SETLOGIN);
3132 	if (error)
3133 		return (error);
3134 	error = copyinstr(uap->namebuf, logintmp, sizeof(logintmp), NULL);
3135 	if (error != 0) {
3136 		if (error == ENAMETOOLONG)
3137 			error = EINVAL;
3138 		return (error);
3139 	}
3140 	AUDIT_ARG_LOGIN(logintmp);
3141 	PROC_LOCK(p);
3142 	SESS_LOCK(p->p_session);
3143 	strcpy(p->p_session->s_login, logintmp);
3144 	SESS_UNLOCK(p->p_session);
3145 	PROC_UNLOCK(p);
3146 	return (0);
3147 }
3148 
3149 void
setsugid(struct proc * p)3150 setsugid(struct proc *p)
3151 {
3152 
3153 	PROC_LOCK_ASSERT(p, MA_OWNED);
3154 	p->p_flag |= P_SUGID;
3155 }
3156 
3157 /*-
3158  * Change a process's effective uid.
3159  * Side effects: newcred->cr_uid and newcred->cr_uidinfo will be modified.
3160  * References: newcred must be an exclusive credential reference for the
3161  *             duration of the call.
3162  */
3163 void
change_euid(struct ucred * newcred,struct uidinfo * euip)3164 change_euid(struct ucred *newcred, struct uidinfo *euip)
3165 {
3166 
3167 	newcred->cr_uid = euip->ui_uid;
3168 	uihold(euip);
3169 	uifree(newcred->cr_uidinfo);
3170 	newcred->cr_uidinfo = euip;
3171 }
3172 
3173 /*-
3174  * Change a process's effective gid.
3175  * Side effects: newcred->cr_gid will be modified.
3176  * References: newcred must be an exclusive credential reference for the
3177  *             duration of the call.
3178  */
3179 void
change_egid(struct ucred * newcred,gid_t egid)3180 change_egid(struct ucred *newcred, gid_t egid)
3181 {
3182 
3183 	newcred->cr_gid = egid;
3184 }
3185 
3186 /*-
3187  * Change a process's real uid.
3188  * Side effects: newcred->cr_ruid will be updated, newcred->cr_ruidinfo
3189  *               will be updated.
3190  * References: newcred must be an exclusive credential reference for the
3191  *             duration of the call.
3192  */
3193 void
change_ruid(struct ucred * newcred,struct uidinfo * ruip)3194 change_ruid(struct ucred *newcred, struct uidinfo *ruip)
3195 {
3196 
3197 	newcred->cr_ruid = ruip->ui_uid;
3198 	uihold(ruip);
3199 	uifree(newcred->cr_ruidinfo);
3200 	newcred->cr_ruidinfo = ruip;
3201 }
3202 
3203 /*-
3204  * Change a process's real gid.
3205  * Side effects: newcred->cr_rgid will be updated.
3206  * References: newcred must be an exclusive credential reference for the
3207  *             duration of the call.
3208  */
3209 void
change_rgid(struct ucred * newcred,gid_t rgid)3210 change_rgid(struct ucred *newcred, gid_t rgid)
3211 {
3212 
3213 	newcred->cr_rgid = rgid;
3214 }
3215 
3216 /*-
3217  * Change a process's saved uid.
3218  * Side effects: newcred->cr_svuid will be updated.
3219  * References: newcred must be an exclusive credential reference for the
3220  *             duration of the call.
3221  */
3222 void
change_svuid(struct ucred * newcred,uid_t svuid)3223 change_svuid(struct ucred *newcred, uid_t svuid)
3224 {
3225 
3226 	newcred->cr_svuid = svuid;
3227 }
3228 
3229 /*-
3230  * Change a process's saved gid.
3231  * Side effects: newcred->cr_svgid will be updated.
3232  * References: newcred must be an exclusive credential reference for the
3233  *             duration of the call.
3234  */
3235 void
change_svgid(struct ucred * newcred,gid_t svgid)3236 change_svgid(struct ucred *newcred, gid_t svgid)
3237 {
3238 
3239 	newcred->cr_svgid = svgid;
3240 }
3241 
3242 bool allow_ptrace = true;
3243 SYSCTL_BOOL(_security_bsd, OID_AUTO, allow_ptrace, CTLFLAG_RWTUN,
3244     &allow_ptrace, 0,
3245     "Deny ptrace(2) use by returning ENOSYS");
3246