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