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