xref: /freebsd/sys/kern/kern_prot.c (revision 1a996ed1d8c8a87fa6b1be7b2a10b4dd4955336a)
19454b2d8SWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1990, 1991, 1993
3ef08c420SRobert Watson  *	The Regents of the University of California.
4df8bae1dSRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
5ef08c420SRobert Watson  * Copyright (c) 2000-2001 Robert N. M. Watson.
6ef08c420SRobert Watson  * All rights reserved.
7ef08c420SRobert Watson  *
8df8bae1dSRodney W. Grimes  * All or some portions of this file are derived from material licensed
9df8bae1dSRodney W. Grimes  * to the University of California by American Telephone and Telegraph
10df8bae1dSRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11df8bae1dSRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
12df8bae1dSRodney W. Grimes  *
13df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
14df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
15df8bae1dSRodney W. Grimes  * are met:
16df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
17df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
18df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
19df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
20df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
21df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
22df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
23df8bae1dSRodney W. Grimes  *    without specific prior written permission.
24df8bae1dSRodney W. Grimes  *
25df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
36df8bae1dSRodney W. Grimes  *
37df8bae1dSRodney W. Grimes  *	@(#)kern_prot.c	8.6 (Berkeley) 1/21/94
38df8bae1dSRodney W. Grimes  */
39df8bae1dSRodney W. Grimes 
40df8bae1dSRodney W. Grimes /*
41df8bae1dSRodney W. Grimes  * System calls related to processes and protection
42df8bae1dSRodney W. Grimes  */
43df8bae1dSRodney W. Grimes 
44677b542eSDavid E. O'Brien #include <sys/cdefs.h>
45677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
46677b542eSDavid E. O'Brien 
475591b823SEivind Eklund #include "opt_compat.h"
48f08ef6c5SBjoern A. Zeeb #include "opt_inet.h"
49f08ef6c5SBjoern A. Zeeb #include "opt_inet6.h"
505591b823SEivind Eklund 
51df8bae1dSRodney W. Grimes #include <sys/param.h>
52df8bae1dSRodney W. Grimes #include <sys/systm.h>
53fb919e4dSMark Murray #include <sys/acct.h>
54df04411aSRobert Watson #include <sys/kdb.h>
551c5bb3eaSPeter Wemm #include <sys/kernel.h>
5698f03f90SJake Burkholder #include <sys/lock.h>
57f9d0d524SRobert Watson #include <sys/malloc.h>
58fb919e4dSMark Murray #include <sys/mutex.h>
597e9e371fSJohn Baldwin #include <sys/refcount.h>
605b29d6e9SJohn Baldwin #include <sys/sx.h>
61800c9408SRobert Watson #include <sys/priv.h>
62f591779bSSeigo Tanimura #include <sys/proc.h>
63fb919e4dSMark Murray #include <sys/sysproto.h>
64eb725b4eSRobert Watson #include <sys/jail.h>
65d5f81602SSean Eric Fagan #include <sys/pioctl.h>
66f535380cSDon Lewis #include <sys/resourcevar.h>
6729dc1288SRobert Watson #include <sys/socket.h>
6829dc1288SRobert Watson #include <sys/socketvar.h>
693cb83e71SJohn Baldwin #include <sys/syscallsubr.h>
70579f4eb4SRobert Watson #include <sys/sysctl.h>
71df8bae1dSRodney W. Grimes 
72f08ef6c5SBjoern A. Zeeb #if defined(INET) || defined(INET6)
73f08ef6c5SBjoern A. Zeeb #include <netinet/in.h>
74f08ef6c5SBjoern A. Zeeb #include <netinet/in_pcb.h>
75f08ef6c5SBjoern A. Zeeb #endif
76f08ef6c5SBjoern A. Zeeb 
772f8a46d5SWayne Salamon #include <security/audit/audit.h>
78aed55708SRobert Watson #include <security/mac/mac_framework.h>
792f8a46d5SWayne Salamon 
80a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_CRED, "cred", "credentials");
81a1c995b6SPoul-Henning Kamp 
825702e096SRobert Watson SYSCTL_NODE(_security, OID_AUTO, bsd, CTLFLAG_RW, 0, "BSD security policy");
8348713bdcSRobert Watson 
84838d9858SBrooks Davis static void crextend(struct ucred *cr, int n);
85838d9858SBrooks Davis static void crsetgroups_locked(struct ucred *cr, int ngrp,
86838d9858SBrooks Davis     gid_t *groups);
87838d9858SBrooks Davis 
88d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
89ad7507e2SSteven Wallace struct getpid_args {
90df8bae1dSRodney W. Grimes 	int	dummy;
91df8bae1dSRodney W. Grimes };
92d2d3e875SBruce Evans #endif
93df8bae1dSRodney W. Grimes /* ARGSUSED */
9426f9a767SRodney W. Grimes int
954c44ad8eSJohn Baldwin getpid(struct thread *td, struct getpid_args *uap)
96df8bae1dSRodney W. Grimes {
97b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
98df8bae1dSRodney W. Grimes 
99b40ce416SJulian Elischer 	td->td_retval[0] = p->p_pid;
1001930e303SPoul-Henning Kamp #if defined(COMPAT_43)
101bae3a80bSJohn Baldwin 	PROC_LOCK(p);
102b40ce416SJulian Elischer 	td->td_retval[1] = p->p_pptr->p_pid;
103bae3a80bSJohn Baldwin 	PROC_UNLOCK(p);
104df8bae1dSRodney W. Grimes #endif
105df8bae1dSRodney W. Grimes 	return (0);
106df8bae1dSRodney W. Grimes }
107df8bae1dSRodney W. Grimes 
108d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
109ad7507e2SSteven Wallace struct getppid_args {
110ad7507e2SSteven Wallace         int     dummy;
111ad7507e2SSteven Wallace };
112d2d3e875SBruce Evans #endif
113df8bae1dSRodney W. Grimes /* ARGSUSED */
11426f9a767SRodney W. Grimes int
1154c44ad8eSJohn Baldwin getppid(struct thread *td, struct getppid_args *uap)
116df8bae1dSRodney W. Grimes {
117b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
118df8bae1dSRodney W. Grimes 
119bae3a80bSJohn Baldwin 	PROC_LOCK(p);
120b40ce416SJulian Elischer 	td->td_retval[0] = p->p_pptr->p_pid;
121bae3a80bSJohn Baldwin 	PROC_UNLOCK(p);
122df8bae1dSRodney W. Grimes 	return (0);
123df8bae1dSRodney W. Grimes }
124df8bae1dSRodney W. Grimes 
12536e9f877SMatthew Dillon /*
126eb725b4eSRobert Watson  * Get process group ID; note that POSIX getpgrp takes no parameter.
12736e9f877SMatthew Dillon  */
128d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
129ad7507e2SSteven Wallace struct getpgrp_args {
130ad7507e2SSteven Wallace         int     dummy;
131ad7507e2SSteven Wallace };
132d2d3e875SBruce Evans #endif
13326f9a767SRodney W. Grimes int
1344c44ad8eSJohn Baldwin getpgrp(struct thread *td, struct getpgrp_args *uap)
135df8bae1dSRodney W. Grimes {
136b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
137df8bae1dSRodney W. Grimes 
138f591779bSSeigo Tanimura 	PROC_LOCK(p);
139b40ce416SJulian Elischer 	td->td_retval[0] = p->p_pgrp->pg_id;
140f591779bSSeigo Tanimura 	PROC_UNLOCK(p);
141df8bae1dSRodney W. Grimes 	return (0);
142df8bae1dSRodney W. Grimes }
143df8bae1dSRodney W. Grimes 
1441a5018a0SPeter Wemm /* Get an arbitary pid's process group id */
1451a5018a0SPeter Wemm #ifndef _SYS_SYSPROTO_H_
1461a5018a0SPeter Wemm struct getpgid_args {
1471a5018a0SPeter Wemm 	pid_t	pid;
1481a5018a0SPeter Wemm };
1491a5018a0SPeter Wemm #endif
1501a5018a0SPeter Wemm int
1514c44ad8eSJohn Baldwin getpgid(struct thread *td, struct getpgid_args *uap)
1521a5018a0SPeter Wemm {
153a70a2b74SJohn Baldwin 	struct proc *p;
154f2ae7368SJohn Baldwin 	int error;
15565de0c7aSDon Lewis 
156f591779bSSeigo Tanimura 	if (uap->pid == 0) {
157a70a2b74SJohn Baldwin 		p = td->td_proc;
158f591779bSSeigo Tanimura 		PROC_LOCK(p);
159a70a2b74SJohn Baldwin 	} else {
160a70a2b74SJohn Baldwin 		p = pfind(uap->pid);
161a70a2b74SJohn Baldwin 		if (p == NULL)
162a70a2b74SJohn Baldwin 			return (ESRCH);
163a70a2b74SJohn Baldwin 		error = p_cansee(td, p);
164a70a2b74SJohn Baldwin 		if (error) {
165a70a2b74SJohn Baldwin 			PROC_UNLOCK(p);
166a70a2b74SJohn Baldwin 			return (error);
167a70a2b74SJohn Baldwin 		}
168a70a2b74SJohn Baldwin 	}
169b40ce416SJulian Elischer 	td->td_retval[0] = p->p_pgrp->pg_id;
170f591779bSSeigo Tanimura 	PROC_UNLOCK(p);
171a70a2b74SJohn Baldwin 	return (0);
1721a5018a0SPeter Wemm }
1731a5018a0SPeter Wemm 
1741a5018a0SPeter Wemm /*
1751a5018a0SPeter Wemm  * Get an arbitary pid's session id.
1761a5018a0SPeter Wemm  */
1771a5018a0SPeter Wemm #ifndef _SYS_SYSPROTO_H_
1781a5018a0SPeter Wemm struct getsid_args {
1791a5018a0SPeter Wemm 	pid_t	pid;
1801a5018a0SPeter Wemm };
1811a5018a0SPeter Wemm #endif
1821a5018a0SPeter Wemm int
1834c44ad8eSJohn Baldwin getsid(struct thread *td, struct getsid_args *uap)
1841a5018a0SPeter Wemm {
185a70a2b74SJohn Baldwin 	struct proc *p;
186eb725b4eSRobert Watson 	int error;
18765de0c7aSDon Lewis 
188f591779bSSeigo Tanimura 	if (uap->pid == 0) {
189a70a2b74SJohn Baldwin 		p = td->td_proc;
190f591779bSSeigo Tanimura 		PROC_LOCK(p);
191a70a2b74SJohn Baldwin 	} else {
192a70a2b74SJohn Baldwin 		p = pfind(uap->pid);
193a70a2b74SJohn Baldwin 		if (p == NULL)
194a70a2b74SJohn Baldwin 			return (ESRCH);
195a70a2b74SJohn Baldwin 		error = p_cansee(td, p);
196a70a2b74SJohn Baldwin 		if (error) {
197a70a2b74SJohn Baldwin 			PROC_UNLOCK(p);
198a70a2b74SJohn Baldwin 			return (error);
199a70a2b74SJohn Baldwin 		}
200a70a2b74SJohn Baldwin 	}
201b40ce416SJulian Elischer 	td->td_retval[0] = p->p_session->s_sid;
202f591779bSSeigo Tanimura 	PROC_UNLOCK(p);
203a70a2b74SJohn Baldwin 	return (0);
2041a5018a0SPeter Wemm }
2051a5018a0SPeter Wemm 
206d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
207ad7507e2SSteven Wallace struct getuid_args {
208ad7507e2SSteven Wallace         int     dummy;
209ad7507e2SSteven Wallace };
210d2d3e875SBruce Evans #endif
211df8bae1dSRodney W. Grimes /* ARGSUSED */
21226f9a767SRodney W. Grimes int
2134c44ad8eSJohn Baldwin getuid(struct thread *td, struct getuid_args *uap)
214df8bae1dSRodney W. Grimes {
215df8bae1dSRodney W. Grimes 
216d846883bSJohn Baldwin 	td->td_retval[0] = td->td_ucred->cr_ruid;
2171930e303SPoul-Henning Kamp #if defined(COMPAT_43)
218d846883bSJohn Baldwin 	td->td_retval[1] = td->td_ucred->cr_uid;
219df8bae1dSRodney W. Grimes #endif
220df8bae1dSRodney W. Grimes 	return (0);
221df8bae1dSRodney W. Grimes }
222df8bae1dSRodney W. Grimes 
223d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
224ad7507e2SSteven Wallace struct geteuid_args {
225ad7507e2SSteven Wallace         int     dummy;
226ad7507e2SSteven Wallace };
227d2d3e875SBruce Evans #endif
228df8bae1dSRodney W. Grimes /* ARGSUSED */
22926f9a767SRodney W. Grimes int
2304c44ad8eSJohn Baldwin geteuid(struct thread *td, struct geteuid_args *uap)
231df8bae1dSRodney W. Grimes {
232d846883bSJohn Baldwin 
233d846883bSJohn Baldwin 	td->td_retval[0] = td->td_ucred->cr_uid;
234df8bae1dSRodney W. Grimes 	return (0);
235df8bae1dSRodney W. Grimes }
236df8bae1dSRodney W. Grimes 
237d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
238ad7507e2SSteven Wallace struct getgid_args {
239ad7507e2SSteven Wallace         int     dummy;
240ad7507e2SSteven Wallace };
241d2d3e875SBruce Evans #endif
242df8bae1dSRodney W. Grimes /* ARGSUSED */
24326f9a767SRodney W. Grimes int
2444c44ad8eSJohn Baldwin getgid(struct thread *td, struct getgid_args *uap)
245df8bae1dSRodney W. Grimes {
246df8bae1dSRodney W. Grimes 
247d846883bSJohn Baldwin 	td->td_retval[0] = td->td_ucred->cr_rgid;
2481930e303SPoul-Henning Kamp #if defined(COMPAT_43)
249d846883bSJohn Baldwin 	td->td_retval[1] = td->td_ucred->cr_groups[0];
250df8bae1dSRodney W. Grimes #endif
251df8bae1dSRodney W. Grimes 	return (0);
252df8bae1dSRodney W. Grimes }
253df8bae1dSRodney W. Grimes 
254df8bae1dSRodney W. Grimes /*
255df8bae1dSRodney W. Grimes  * Get effective group ID.  The "egid" is groups[0], and could be obtained
256df8bae1dSRodney W. Grimes  * via getgroups.  This syscall exists because it is somewhat painful to do
257df8bae1dSRodney W. Grimes  * correctly in a library function.
258df8bae1dSRodney W. Grimes  */
259d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
260ad7507e2SSteven Wallace struct getegid_args {
261ad7507e2SSteven Wallace         int     dummy;
262ad7507e2SSteven Wallace };
263d2d3e875SBruce Evans #endif
264df8bae1dSRodney W. Grimes /* ARGSUSED */
26526f9a767SRodney W. Grimes int
2664c44ad8eSJohn Baldwin getegid(struct thread *td, struct getegid_args *uap)
267df8bae1dSRodney W. Grimes {
268df8bae1dSRodney W. Grimes 
269d846883bSJohn Baldwin 	td->td_retval[0] = td->td_ucred->cr_groups[0];
270df8bae1dSRodney W. Grimes 	return (0);
271df8bae1dSRodney W. Grimes }
272df8bae1dSRodney W. Grimes 
273d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
274df8bae1dSRodney W. Grimes struct getgroups_args {
275df8bae1dSRodney W. Grimes 	u_int	gidsetsize;
276df8bae1dSRodney W. Grimes 	gid_t	*gidset;
277df8bae1dSRodney W. Grimes };
278d2d3e875SBruce Evans #endif
27926f9a767SRodney W. Grimes int
2804c44ad8eSJohn Baldwin getgroups(struct thread *td, register struct getgroups_args *uap)
281df8bae1dSRodney W. Grimes {
282838d9858SBrooks Davis 	gid_t *groups;
283b1fc0ec1SRobert Watson 	u_int ngrp;
284eb725b4eSRobert Watson 	int error;
285df8bae1dSRodney W. Grimes 
2869126964cSBrooks Davis 	if (uap->gidsetsize < td->td_ucred->cr_ngroups) {
2879126964cSBrooks Davis 		if (uap->gidsetsize == 0)
2889126964cSBrooks Davis 			ngrp = 0;
2899126964cSBrooks Davis 		else
2909126964cSBrooks Davis 			return (EINVAL);
2919126964cSBrooks Davis 	} else
2929126964cSBrooks Davis 		ngrp = td->td_ucred->cr_ngroups;
293838d9858SBrooks Davis 	groups = malloc(ngrp * sizeof(*groups), M_TEMP, M_WAITOK);
2943cb83e71SJohn Baldwin 	error = kern_getgroups(td, &ngrp, groups);
2953cb83e71SJohn Baldwin 	if (error)
296838d9858SBrooks Davis 		goto out;
2973cb83e71SJohn Baldwin 	if (uap->gidsetsize > 0)
2983cb83e71SJohn Baldwin 		error = copyout(groups, uap->gidset, ngrp * sizeof(gid_t));
299d74ac681SMatthew Dillon 	if (error == 0)
300d846883bSJohn Baldwin 		td->td_retval[0] = ngrp;
301838d9858SBrooks Davis out:
302838d9858SBrooks Davis 	free(groups, M_TEMP);
303d74ac681SMatthew Dillon 	return (error);
304df8bae1dSRodney W. Grimes }
305df8bae1dSRodney W. Grimes 
3063cb83e71SJohn Baldwin int
3073cb83e71SJohn Baldwin kern_getgroups(struct thread *td, u_int *ngrp, gid_t *groups)
3083cb83e71SJohn Baldwin {
3093cb83e71SJohn Baldwin 	struct ucred *cred;
3103cb83e71SJohn Baldwin 
3113cb83e71SJohn Baldwin 	cred = td->td_ucred;
3123cb83e71SJohn Baldwin 	if (*ngrp == 0) {
3133cb83e71SJohn Baldwin 		*ngrp = cred->cr_ngroups;
3143cb83e71SJohn Baldwin 		return (0);
3153cb83e71SJohn Baldwin 	}
3163cb83e71SJohn Baldwin 	if (*ngrp < cred->cr_ngroups)
3173cb83e71SJohn Baldwin 		return (EINVAL);
3183cb83e71SJohn Baldwin 	*ngrp = cred->cr_ngroups;
3193cb83e71SJohn Baldwin 	bcopy(cred->cr_groups, groups, *ngrp * sizeof(gid_t));
3203cb83e71SJohn Baldwin 	return (0);
3213cb83e71SJohn Baldwin }
3223cb83e71SJohn Baldwin 
323d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
32482970b81SBruce Evans struct setsid_args {
325ad7507e2SSteven Wallace         int     dummy;
326ad7507e2SSteven Wallace };
327d2d3e875SBruce Evans #endif
328df8bae1dSRodney W. Grimes /* ARGSUSED */
32926f9a767SRodney W. Grimes int
3304c44ad8eSJohn Baldwin setsid(register struct thread *td, struct setsid_args *uap)
331df8bae1dSRodney W. Grimes {
332f591779bSSeigo Tanimura 	struct pgrp *pgrp;
333835a82eeSMatthew Dillon 	int error;
334b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
335f591779bSSeigo Tanimura 	struct pgrp *newpgrp;
336f591779bSSeigo Tanimura 	struct session *newsess;
337f591779bSSeigo Tanimura 
338f591779bSSeigo Tanimura 	error = 0;
339f591779bSSeigo Tanimura 	pgrp = NULL;
340df8bae1dSRodney W. Grimes 
3411ede983cSDag-Erling Smørgrav 	newpgrp = malloc(sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
3421ede983cSDag-Erling Smørgrav 	newsess = malloc(sizeof(struct session), M_SESSION, M_WAITOK | M_ZERO);
343f591779bSSeigo Tanimura 
344c8b1829dSJohn Baldwin 	sx_xlock(&proctree_lock);
345f591779bSSeigo Tanimura 
346f591779bSSeigo Tanimura 	if (p->p_pgid == p->p_pid || (pgrp = pgfind(p->p_pid)) != NULL) {
347f591779bSSeigo Tanimura 		if (pgrp != NULL)
348f591779bSSeigo Tanimura 			PGRP_UNLOCK(pgrp);
349835a82eeSMatthew Dillon 		error = EPERM;
350f591779bSSeigo Tanimura 	} else {
351f591779bSSeigo Tanimura 		(void)enterpgrp(p, p->p_pid, newpgrp, newsess);
352b40ce416SJulian Elischer 		td->td_retval[0] = p->p_pid;
353c8b1829dSJohn Baldwin 		newpgrp = NULL;
354c8b1829dSJohn Baldwin 		newsess = NULL;
355df8bae1dSRodney W. Grimes 	}
356f591779bSSeigo Tanimura 
357c8b1829dSJohn Baldwin 	sx_xunlock(&proctree_lock);
358f591779bSSeigo Tanimura 
359c8b1829dSJohn Baldwin 	if (newpgrp != NULL)
3601ede983cSDag-Erling Smørgrav 		free(newpgrp, M_PGRP);
361c8b1829dSJohn Baldwin 	if (newsess != NULL)
3621ede983cSDag-Erling Smørgrav 		free(newsess, M_SESSION);
3631c2451c2SSeigo Tanimura 
364c8b1829dSJohn Baldwin 	return (error);
365df8bae1dSRodney W. Grimes }
366df8bae1dSRodney W. Grimes 
367df8bae1dSRodney W. Grimes /*
368df8bae1dSRodney W. Grimes  * set process group (setpgid/old setpgrp)
369df8bae1dSRodney W. Grimes  *
370df8bae1dSRodney W. Grimes  * caller does setpgid(targpid, targpgid)
371df8bae1dSRodney W. Grimes  *
372df8bae1dSRodney W. Grimes  * pid must be caller or child of caller (ESRCH)
373df8bae1dSRodney W. Grimes  * if a child
374df8bae1dSRodney W. Grimes  *	pid must be in same session (EPERM)
375df8bae1dSRodney W. Grimes  *	pid can't have done an exec (EACCES)
376df8bae1dSRodney W. Grimes  * if pgid != pid
377df8bae1dSRodney W. Grimes  * 	there must exist some pid in same session having pgid (EPERM)
378df8bae1dSRodney W. Grimes  * pid must not be session leader (EPERM)
379df8bae1dSRodney W. Grimes  */
380d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
381df8bae1dSRodney W. Grimes struct setpgid_args {
382df8bae1dSRodney W. Grimes 	int	pid;		/* target process id */
383df8bae1dSRodney W. Grimes 	int	pgid;		/* target pgrp id */
384df8bae1dSRodney W. Grimes };
385d2d3e875SBruce Evans #endif
386df8bae1dSRodney W. Grimes /* ARGSUSED */
38726f9a767SRodney W. Grimes int
3884c44ad8eSJohn Baldwin setpgid(struct thread *td, register struct setpgid_args *uap)
389df8bae1dSRodney W. Grimes {
390b40ce416SJulian Elischer 	struct proc *curp = td->td_proc;
391df8bae1dSRodney W. Grimes 	register struct proc *targp;	/* target process */
392df8bae1dSRodney W. Grimes 	register struct pgrp *pgrp;	/* target pgrp */
393eb9e5c1dSRobert Watson 	int error;
394f591779bSSeigo Tanimura 	struct pgrp *newpgrp;
395df8bae1dSRodney W. Grimes 
39678f64bccSBruce Evans 	if (uap->pgid < 0)
39778f64bccSBruce Evans 		return (EINVAL);
398f591779bSSeigo Tanimura 
399f591779bSSeigo Tanimura 	error = 0;
400f591779bSSeigo Tanimura 
4011ede983cSDag-Erling Smørgrav 	newpgrp = malloc(sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
402f591779bSSeigo Tanimura 
403c8b1829dSJohn Baldwin 	sx_xlock(&proctree_lock);
404df8bae1dSRodney W. Grimes 	if (uap->pid != 0 && uap->pid != curp->p_pid) {
405f591779bSSeigo Tanimura 		if ((targp = pfind(uap->pid)) == NULL) {
406835a82eeSMatthew Dillon 			error = ESRCH;
407c8b1829dSJohn Baldwin 			goto done;
40833a9ed9dSJohn Baldwin 		}
409f591779bSSeigo Tanimura 		if (!inferior(targp)) {
410f591779bSSeigo Tanimura 			PROC_UNLOCK(targp);
4112f932587SSeigo Tanimura 			error = ESRCH;
412c8b1829dSJohn Baldwin 			goto done;
413f591779bSSeigo Tanimura 		}
41471a057bcSRobert Watson 		if ((error = p_cansee(td, targp))) {
41533a9ed9dSJohn Baldwin 			PROC_UNLOCK(targp);
416c8b1829dSJohn Baldwin 			goto done;
41733a9ed9dSJohn Baldwin 		}
41833a9ed9dSJohn Baldwin 		if (targp->p_pgrp == NULL ||
41933a9ed9dSJohn Baldwin 		    targp->p_session != curp->p_session) {
42033a9ed9dSJohn Baldwin 			PROC_UNLOCK(targp);
421835a82eeSMatthew Dillon 			error = EPERM;
422c8b1829dSJohn Baldwin 			goto done;
42333a9ed9dSJohn Baldwin 		}
42433a9ed9dSJohn Baldwin 		if (targp->p_flag & P_EXEC) {
42533a9ed9dSJohn Baldwin 			PROC_UNLOCK(targp);
426835a82eeSMatthew Dillon 			error = EACCES;
427c8b1829dSJohn Baldwin 			goto done;
42833a9ed9dSJohn Baldwin 		}
42933a9ed9dSJohn Baldwin 		PROC_UNLOCK(targp);
430f591779bSSeigo Tanimura 	} else
431f591779bSSeigo Tanimura 		targp = curp;
432f591779bSSeigo Tanimura 	if (SESS_LEADER(targp)) {
433835a82eeSMatthew Dillon 		error = EPERM;
434c8b1829dSJohn Baldwin 		goto done;
43533a9ed9dSJohn Baldwin 	}
436eb725b4eSRobert Watson 	if (uap->pgid == 0)
437df8bae1dSRodney W. Grimes 		uap->pgid = targp->p_pid;
438a10d5f02SOlivier Houchard 	if ((pgrp = pgfind(uap->pgid)) == NULL) {
439f591779bSSeigo Tanimura 		if (uap->pgid == targp->p_pid) {
440a10d5f02SOlivier Houchard 			error = enterpgrp(targp, uap->pgid, newpgrp,
441a10d5f02SOlivier Houchard 			    NULL);
442f591779bSSeigo Tanimura 			if (error == 0)
443f591779bSSeigo Tanimura 				newpgrp = NULL;
444a10d5f02SOlivier Houchard 		} else
445835a82eeSMatthew Dillon 			error = EPERM;
446a10d5f02SOlivier Houchard 	} else {
447f591779bSSeigo Tanimura 		if (pgrp == targp->p_pgrp) {
448f591779bSSeigo Tanimura 			PGRP_UNLOCK(pgrp);
449f591779bSSeigo Tanimura 			goto done;
45033a9ed9dSJohn Baldwin 		}
451a10d5f02SOlivier Houchard 		if (pgrp->pg_id != targp->p_pid &&
452a10d5f02SOlivier Houchard 		    pgrp->pg_session != curp->p_session) {
453a10d5f02SOlivier Houchard 			PGRP_UNLOCK(pgrp);
454a10d5f02SOlivier Houchard 			error = EPERM;
455a10d5f02SOlivier Houchard 			goto done;
456a10d5f02SOlivier Houchard 		}
457f591779bSSeigo Tanimura 		PGRP_UNLOCK(pgrp);
458f591779bSSeigo Tanimura 		error = enterthispgrp(targp, pgrp);
459f591779bSSeigo Tanimura 	}
460f591779bSSeigo Tanimura done:
461c8b1829dSJohn Baldwin 	sx_xunlock(&proctree_lock);
462c8b1829dSJohn Baldwin 	KASSERT((error == 0) || (newpgrp != NULL),
463c8b1829dSJohn Baldwin 	    ("setpgid failed and newpgrp is NULL"));
4646041fa0aSSeigo Tanimura 	if (newpgrp != NULL)
4651ede983cSDag-Erling Smørgrav 		free(newpgrp, M_PGRP);
466835a82eeSMatthew Dillon 	return (error);
467df8bae1dSRodney W. Grimes }
468df8bae1dSRodney W. Grimes 
469a08f4bf6SPeter Wemm /*
470a08f4bf6SPeter Wemm  * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
4712fa72ea7SJeroen Ruigrok van der Werven  * compatible.  It says that setting the uid/gid to euid/egid is a special
472a08f4bf6SPeter Wemm  * case of "appropriate privilege".  Once the rules are expanded out, this
473a08f4bf6SPeter Wemm  * basically means that setuid(nnn) sets all three id's, in all permitted
474a08f4bf6SPeter Wemm  * cases unless _POSIX_SAVED_IDS is enabled.  In that case, setuid(getuid())
475a08f4bf6SPeter Wemm  * does not set the saved id - this is dangerous for traditional BSD
476a08f4bf6SPeter Wemm  * programs.  For this reason, we *really* do not want to set
477a08f4bf6SPeter Wemm  * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
478a08f4bf6SPeter Wemm  */
479a08f4bf6SPeter Wemm #define POSIX_APPENDIX_B_4_2_2
480a08f4bf6SPeter Wemm 
481d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
482df8bae1dSRodney W. Grimes struct setuid_args {
483df8bae1dSRodney W. Grimes 	uid_t	uid;
484df8bae1dSRodney W. Grimes };
485d2d3e875SBruce Evans #endif
486df8bae1dSRodney W. Grimes /* ARGSUSED */
48726f9a767SRodney W. Grimes int
4884c44ad8eSJohn Baldwin setuid(struct thread *td, struct setuid_args *uap)
489df8bae1dSRodney W. Grimes {
490b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
491b1fc0ec1SRobert Watson 	struct ucred *newcred, *oldcred;
492b1fc0ec1SRobert Watson 	uid_t uid;
4931419eacbSAlfred Perlstein 	struct uidinfo *uip;
494eb725b4eSRobert Watson 	int error;
495df8bae1dSRodney W. Grimes 
49607f3485dSJohn Baldwin 	uid = uap->uid;
49714961ba7SRobert Watson 	AUDIT_ARG_UID(uid);
49807f3485dSJohn Baldwin 	newcred = crget();
4991419eacbSAlfred Perlstein 	uip = uifind(uid);
50007f3485dSJohn Baldwin 	PROC_LOCK(p);
501838d9858SBrooks Davis 	/*
502838d9858SBrooks Davis 	 * Copy credentials so other references do not see our changes.
503838d9858SBrooks Davis 	 */
504838d9858SBrooks Davis 	oldcred = crcopysafe(p, newcred);
5055a92ee3cSRobert Watson 
506030a28b3SRobert Watson #ifdef MAC
5076f6174a7SRobert Watson 	error = mac_cred_check_setuid(oldcred, uid);
508030a28b3SRobert Watson 	if (error)
509030a28b3SRobert Watson 		goto fail;
510030a28b3SRobert Watson #endif
511030a28b3SRobert Watson 
512a08f4bf6SPeter Wemm 	/*
513a08f4bf6SPeter Wemm 	 * See if we have "permission" by POSIX 1003.1 rules.
514a08f4bf6SPeter Wemm 	 *
515a08f4bf6SPeter Wemm 	 * Note that setuid(geteuid()) is a special case of
516a08f4bf6SPeter Wemm 	 * "appropriate privileges" in appendix B.4.2.2.  We need
5172fa72ea7SJeroen Ruigrok van der Werven 	 * to use this clause to be compatible with traditional BSD
518a08f4bf6SPeter Wemm 	 * semantics.  Basically, it means that "setuid(xx)" sets all
519a08f4bf6SPeter Wemm 	 * three id's (assuming you have privs).
520a08f4bf6SPeter Wemm 	 *
521a08f4bf6SPeter Wemm 	 * Notes on the logic.  We do things in three steps.
522a08f4bf6SPeter Wemm 	 * 1: We determine if the euid is going to change, and do EPERM
523a08f4bf6SPeter Wemm 	 *    right away.  We unconditionally change the euid later if this
524a08f4bf6SPeter Wemm 	 *    test is satisfied, simplifying that part of the logic.
525eb725b4eSRobert Watson 	 * 2: We determine if the real and/or saved uids are going to
526a08f4bf6SPeter Wemm 	 *    change.  Determined by compile options.
527a08f4bf6SPeter Wemm 	 * 3: Change euid last. (after tests in #2 for "appropriate privs")
528a08f4bf6SPeter Wemm 	 */
529b1fc0ec1SRobert Watson 	if (uid != oldcred->cr_ruid &&		/* allow setuid(getuid()) */
5303f246666SAndrey A. Chernov #ifdef _POSIX_SAVED_IDS
531b1fc0ec1SRobert Watson 	    uid != oldcred->cr_svuid &&		/* allow setuid(saved gid) */
532a08f4bf6SPeter Wemm #endif
533a08f4bf6SPeter Wemm #ifdef POSIX_APPENDIX_B_4_2_2	/* Use BSD-compat clause from B.4.2.2 */
534b1fc0ec1SRobert Watson 	    uid != oldcred->cr_uid &&		/* allow setuid(geteuid()) */
5353f246666SAndrey A. Chernov #endif
53632f9753cSRobert Watson 	    (error = priv_check_cred(oldcred, PRIV_CRED_SETUID, 0)) != 0)
537030a28b3SRobert Watson 		goto fail;
538a08f4bf6SPeter Wemm 
539a08f4bf6SPeter Wemm #ifdef _POSIX_SAVED_IDS
540df8bae1dSRodney W. Grimes 	/*
541a08f4bf6SPeter Wemm 	 * Do we have "appropriate privileges" (are we root or uid == euid)
542a08f4bf6SPeter Wemm 	 * If so, we are changing the real uid and/or saved uid.
543df8bae1dSRodney W. Grimes 	 */
5443f246666SAndrey A. Chernov 	if (
545a08f4bf6SPeter Wemm #ifdef POSIX_APPENDIX_B_4_2_2	/* Use the clause from B.4.2.2 */
546b1fc0ec1SRobert Watson 	    uid == oldcred->cr_uid ||
5473f246666SAndrey A. Chernov #endif
548800c9408SRobert Watson 	    /* We are using privs. */
54932f9753cSRobert Watson 	    priv_check_cred(oldcred, PRIV_CRED_SETUID, 0) == 0)
550a08f4bf6SPeter Wemm #endif
551a08f4bf6SPeter Wemm 	{
552a08f4bf6SPeter Wemm 		/*
553f535380cSDon Lewis 		 * Set the real uid and transfer proc count to new user.
554a08f4bf6SPeter Wemm 		 */
555b1fc0ec1SRobert Watson 		if (uid != oldcred->cr_ruid) {
5561419eacbSAlfred Perlstein 			change_ruid(newcred, uip);
557f535380cSDon Lewis 			setsugid(p);
558d3cdb93dSAndrey A. Chernov 		}
559a08f4bf6SPeter Wemm 		/*
560a08f4bf6SPeter Wemm 		 * Set saved uid
561a08f4bf6SPeter Wemm 		 *
562a08f4bf6SPeter Wemm 		 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
563a08f4bf6SPeter Wemm 		 * the security of seteuid() depends on it.  B.4.2.2 says it
564a08f4bf6SPeter Wemm 		 * is important that we should do this.
565a08f4bf6SPeter Wemm 		 */
566b1fc0ec1SRobert Watson 		if (uid != oldcred->cr_svuid) {
567b1fc0ec1SRobert Watson 			change_svuid(newcred, uid);
568d5f81602SSean Eric Fagan 			setsugid(p);
569a08f4bf6SPeter Wemm 		}
570a08f4bf6SPeter Wemm 	}
571a08f4bf6SPeter Wemm 
572a08f4bf6SPeter Wemm 	/*
573a08f4bf6SPeter Wemm 	 * In all permitted cases, we are changing the euid.
574a08f4bf6SPeter Wemm 	 */
575b1fc0ec1SRobert Watson 	if (uid != oldcred->cr_uid) {
5761419eacbSAlfred Perlstein 		change_euid(newcred, uip);
577d5f81602SSean Eric Fagan 		setsugid(p);
578a08f4bf6SPeter Wemm 	}
579b1fc0ec1SRobert Watson 	p->p_ucred = newcred;
58007f3485dSJohn Baldwin 	PROC_UNLOCK(p);
5811419eacbSAlfred Perlstein 	uifree(uip);
582b1fc0ec1SRobert Watson 	crfree(oldcred);
58307f3485dSJohn Baldwin 	return (0);
584030a28b3SRobert Watson 
585030a28b3SRobert Watson fail:
586030a28b3SRobert Watson 	PROC_UNLOCK(p);
587030a28b3SRobert Watson 	uifree(uip);
588030a28b3SRobert Watson 	crfree(newcred);
589030a28b3SRobert Watson 	return (error);
590df8bae1dSRodney W. Grimes }
591df8bae1dSRodney W. Grimes 
592d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
593df8bae1dSRodney W. Grimes struct seteuid_args {
594df8bae1dSRodney W. Grimes 	uid_t	euid;
595df8bae1dSRodney W. Grimes };
596d2d3e875SBruce Evans #endif
597df8bae1dSRodney W. Grimes /* ARGSUSED */
59826f9a767SRodney W. Grimes int
5994c44ad8eSJohn Baldwin seteuid(struct thread *td, struct seteuid_args *uap)
600df8bae1dSRodney W. Grimes {
601b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
602b1fc0ec1SRobert Watson 	struct ucred *newcred, *oldcred;
603b1fc0ec1SRobert Watson 	uid_t euid;
6041419eacbSAlfred Perlstein 	struct uidinfo *euip;
605eb725b4eSRobert Watson 	int error;
606df8bae1dSRodney W. Grimes 
607df8bae1dSRodney W. Grimes 	euid = uap->euid;
60814961ba7SRobert Watson 	AUDIT_ARG_EUID(euid);
60907f3485dSJohn Baldwin 	newcred = crget();
6101419eacbSAlfred Perlstein 	euip = uifind(euid);
61107f3485dSJohn Baldwin 	PROC_LOCK(p);
612838d9858SBrooks Davis 	/*
613838d9858SBrooks Davis 	 * Copy credentials so other references do not see our changes.
614838d9858SBrooks Davis 	 */
615838d9858SBrooks Davis 	oldcred = crcopysafe(p, newcred);
616030a28b3SRobert Watson 
617030a28b3SRobert Watson #ifdef MAC
6186f6174a7SRobert Watson 	error = mac_cred_check_seteuid(oldcred, euid);
619030a28b3SRobert Watson 	if (error)
620030a28b3SRobert Watson 		goto fail;
621030a28b3SRobert Watson #endif
622030a28b3SRobert Watson 
623b1fc0ec1SRobert Watson 	if (euid != oldcred->cr_ruid &&		/* allow seteuid(getuid()) */
624b1fc0ec1SRobert Watson 	    euid != oldcred->cr_svuid &&	/* allow seteuid(saved uid) */
62532f9753cSRobert Watson 	    (error = priv_check_cred(oldcred, PRIV_CRED_SETEUID, 0)) != 0)
626030a28b3SRobert Watson 		goto fail;
627030a28b3SRobert Watson 
628df8bae1dSRodney W. Grimes 	/*
629838d9858SBrooks Davis 	 * Everything's okay, do it.
630df8bae1dSRodney W. Grimes 	 */
631b1fc0ec1SRobert Watson 	if (oldcred->cr_uid != euid) {
6321419eacbSAlfred Perlstein 		change_euid(newcred, euip);
633d5f81602SSean Eric Fagan 		setsugid(p);
634229a15f0SPeter Wemm 	}
635b1fc0ec1SRobert Watson 	p->p_ucred = newcred;
63607f3485dSJohn Baldwin 	PROC_UNLOCK(p);
6371419eacbSAlfred Perlstein 	uifree(euip);
638b1fc0ec1SRobert Watson 	crfree(oldcred);
63907f3485dSJohn Baldwin 	return (0);
640030a28b3SRobert Watson 
641030a28b3SRobert Watson fail:
642030a28b3SRobert Watson 	PROC_UNLOCK(p);
643030a28b3SRobert Watson 	uifree(euip);
644030a28b3SRobert Watson 	crfree(newcred);
645030a28b3SRobert Watson 	return (error);
646df8bae1dSRodney W. Grimes }
647df8bae1dSRodney W. Grimes 
648d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
649df8bae1dSRodney W. Grimes struct setgid_args {
650df8bae1dSRodney W. Grimes 	gid_t	gid;
651df8bae1dSRodney W. Grimes };
652d2d3e875SBruce Evans #endif
653df8bae1dSRodney W. Grimes /* ARGSUSED */
65426f9a767SRodney W. Grimes int
6554c44ad8eSJohn Baldwin setgid(struct thread *td, struct setgid_args *uap)
656df8bae1dSRodney W. Grimes {
657b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
658b1fc0ec1SRobert Watson 	struct ucred *newcred, *oldcred;
659b1fc0ec1SRobert Watson 	gid_t gid;
660eb725b4eSRobert Watson 	int error;
661df8bae1dSRodney W. Grimes 
662b1fc0ec1SRobert Watson 	gid = uap->gid;
66314961ba7SRobert Watson 	AUDIT_ARG_GID(gid);
66407f3485dSJohn Baldwin 	newcred = crget();
66507f3485dSJohn Baldwin 	PROC_LOCK(p);
666838d9858SBrooks Davis 	oldcred = crcopysafe(p, newcred);
6675a92ee3cSRobert Watson 
668030a28b3SRobert Watson #ifdef MAC
6696f6174a7SRobert Watson 	error = mac_cred_check_setgid(oldcred, gid);
670030a28b3SRobert Watson 	if (error)
671030a28b3SRobert Watson 		goto fail;
672030a28b3SRobert Watson #endif
673030a28b3SRobert Watson 
674a08f4bf6SPeter Wemm 	/*
675a08f4bf6SPeter Wemm 	 * See if we have "permission" by POSIX 1003.1 rules.
676a08f4bf6SPeter Wemm 	 *
677a08f4bf6SPeter Wemm 	 * Note that setgid(getegid()) is a special case of
678a08f4bf6SPeter Wemm 	 * "appropriate privileges" in appendix B.4.2.2.  We need
6792fa72ea7SJeroen Ruigrok van der Werven 	 * to use this clause to be compatible with traditional BSD
680a08f4bf6SPeter Wemm 	 * semantics.  Basically, it means that "setgid(xx)" sets all
681a08f4bf6SPeter Wemm 	 * three id's (assuming you have privs).
682a08f4bf6SPeter Wemm 	 *
683a08f4bf6SPeter Wemm 	 * For notes on the logic here, see setuid() above.
684a08f4bf6SPeter Wemm 	 */
685b1fc0ec1SRobert Watson 	if (gid != oldcred->cr_rgid &&		/* allow setgid(getgid()) */
6863f246666SAndrey A. Chernov #ifdef _POSIX_SAVED_IDS
687b1fc0ec1SRobert Watson 	    gid != oldcred->cr_svgid &&		/* allow setgid(saved gid) */
688a08f4bf6SPeter Wemm #endif
689a08f4bf6SPeter Wemm #ifdef POSIX_APPENDIX_B_4_2_2	/* Use BSD-compat clause from B.4.2.2 */
690b1fc0ec1SRobert Watson 	    gid != oldcred->cr_groups[0] && /* allow setgid(getegid()) */
6913f246666SAndrey A. Chernov #endif
69232f9753cSRobert Watson 	    (error = priv_check_cred(oldcred, PRIV_CRED_SETGID, 0)) != 0)
693030a28b3SRobert Watson 		goto fail;
694a08f4bf6SPeter Wemm 
695a08f4bf6SPeter Wemm #ifdef _POSIX_SAVED_IDS
696a08f4bf6SPeter Wemm 	/*
697a08f4bf6SPeter Wemm 	 * Do we have "appropriate privileges" (are we root or gid == egid)
698a08f4bf6SPeter Wemm 	 * If so, we are changing the real uid and saved gid.
699a08f4bf6SPeter Wemm 	 */
700a08f4bf6SPeter Wemm 	if (
701a08f4bf6SPeter Wemm #ifdef POSIX_APPENDIX_B_4_2_2	/* use the clause from B.4.2.2 */
702b1fc0ec1SRobert Watson 	    gid == oldcred->cr_groups[0] ||
703a08f4bf6SPeter Wemm #endif
704800c9408SRobert Watson 	    /* We are using privs. */
70532f9753cSRobert Watson 	    priv_check_cred(oldcred, PRIV_CRED_SETGID, 0) == 0)
706a08f4bf6SPeter Wemm #endif
707a08f4bf6SPeter Wemm 	{
708a08f4bf6SPeter Wemm 		/*
709a08f4bf6SPeter Wemm 		 * Set real gid
710a08f4bf6SPeter Wemm 		 */
711b1fc0ec1SRobert Watson 		if (oldcred->cr_rgid != gid) {
712b1fc0ec1SRobert Watson 			change_rgid(newcred, gid);
713d5f81602SSean Eric Fagan 			setsugid(p);
714a08f4bf6SPeter Wemm 		}
715a08f4bf6SPeter Wemm 		/*
716a08f4bf6SPeter Wemm 		 * Set saved gid
717a08f4bf6SPeter Wemm 		 *
718a08f4bf6SPeter Wemm 		 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
719a08f4bf6SPeter Wemm 		 * the security of setegid() depends on it.  B.4.2.2 says it
720a08f4bf6SPeter Wemm 		 * is important that we should do this.
721a08f4bf6SPeter Wemm 		 */
722b1fc0ec1SRobert Watson 		if (oldcred->cr_svgid != gid) {
723b1fc0ec1SRobert Watson 			change_svgid(newcred, gid);
724d5f81602SSean Eric Fagan 			setsugid(p);
725a08f4bf6SPeter Wemm 		}
726a08f4bf6SPeter Wemm 	}
727a08f4bf6SPeter Wemm 	/*
728a08f4bf6SPeter Wemm 	 * In all cases permitted cases, we are changing the egid.
729a08f4bf6SPeter Wemm 	 * Copy credentials so other references do not see our changes.
730a08f4bf6SPeter Wemm 	 */
731b1fc0ec1SRobert Watson 	if (oldcred->cr_groups[0] != gid) {
732b1fc0ec1SRobert Watson 		change_egid(newcred, gid);
733d5f81602SSean Eric Fagan 		setsugid(p);
734a08f4bf6SPeter Wemm 	}
735b1fc0ec1SRobert Watson 	p->p_ucred = newcred;
73607f3485dSJohn Baldwin 	PROC_UNLOCK(p);
737b1fc0ec1SRobert Watson 	crfree(oldcred);
73807f3485dSJohn Baldwin 	return (0);
739030a28b3SRobert Watson 
740030a28b3SRobert Watson fail:
741030a28b3SRobert Watson 	PROC_UNLOCK(p);
742030a28b3SRobert Watson 	crfree(newcred);
743030a28b3SRobert Watson 	return (error);
744df8bae1dSRodney W. Grimes }
745df8bae1dSRodney W. Grimes 
746d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
747df8bae1dSRodney W. Grimes struct setegid_args {
748df8bae1dSRodney W. Grimes 	gid_t	egid;
749df8bae1dSRodney W. Grimes };
750d2d3e875SBruce Evans #endif
751df8bae1dSRodney W. Grimes /* ARGSUSED */
75226f9a767SRodney W. Grimes int
7534c44ad8eSJohn Baldwin setegid(struct thread *td, struct setegid_args *uap)
754df8bae1dSRodney W. Grimes {
755b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
756b1fc0ec1SRobert Watson 	struct ucred *newcred, *oldcred;
757b1fc0ec1SRobert Watson 	gid_t egid;
758eb725b4eSRobert Watson 	int error;
759df8bae1dSRodney W. Grimes 
760df8bae1dSRodney W. Grimes 	egid = uap->egid;
76114961ba7SRobert Watson 	AUDIT_ARG_EGID(egid);
76207f3485dSJohn Baldwin 	newcred = crget();
76307f3485dSJohn Baldwin 	PROC_LOCK(p);
764838d9858SBrooks Davis 	oldcred = crcopysafe(p, newcred);
765030a28b3SRobert Watson 
766030a28b3SRobert Watson #ifdef MAC
7676f6174a7SRobert Watson 	error = mac_cred_check_setegid(oldcred, egid);
768030a28b3SRobert Watson 	if (error)
769030a28b3SRobert Watson 		goto fail;
770030a28b3SRobert Watson #endif
771030a28b3SRobert Watson 
772b1fc0ec1SRobert Watson 	if (egid != oldcred->cr_rgid &&		/* allow setegid(getgid()) */
773b1fc0ec1SRobert Watson 	    egid != oldcred->cr_svgid &&	/* allow setegid(saved gid) */
77432f9753cSRobert Watson 	    (error = priv_check_cred(oldcred, PRIV_CRED_SETEGID, 0)) != 0)
775030a28b3SRobert Watson 		goto fail;
776030a28b3SRobert Watson 
777b1fc0ec1SRobert Watson 	if (oldcred->cr_groups[0] != egid) {
778b1fc0ec1SRobert Watson 		change_egid(newcred, egid);
779d5f81602SSean Eric Fagan 		setsugid(p);
780229a15f0SPeter Wemm 	}
781b1fc0ec1SRobert Watson 	p->p_ucred = newcred;
78207f3485dSJohn Baldwin 	PROC_UNLOCK(p);
783b1fc0ec1SRobert Watson 	crfree(oldcred);
78407f3485dSJohn Baldwin 	return (0);
785030a28b3SRobert Watson 
786030a28b3SRobert Watson fail:
787030a28b3SRobert Watson 	PROC_UNLOCK(p);
788030a28b3SRobert Watson 	crfree(newcred);
789030a28b3SRobert Watson 	return (error);
790df8bae1dSRodney W. Grimes }
791df8bae1dSRodney W. Grimes 
792d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
793df8bae1dSRodney W. Grimes struct setgroups_args {
794df8bae1dSRodney W. Grimes 	u_int	gidsetsize;
795df8bae1dSRodney W. Grimes 	gid_t	*gidset;
796df8bae1dSRodney W. Grimes };
797d2d3e875SBruce Evans #endif
798df8bae1dSRodney W. Grimes /* ARGSUSED */
79926f9a767SRodney W. Grimes int
8004c44ad8eSJohn Baldwin setgroups(struct thread *td, struct setgroups_args *uap)
801df8bae1dSRodney W. Grimes {
802838d9858SBrooks Davis 	gid_t *groups = NULL;
803df8bae1dSRodney W. Grimes 	int error;
804df8bae1dSRodney W. Grimes 
805412f9500SBrooks Davis 	if (uap->gidsetsize > ngroups_max + 1)
8063cb83e71SJohn Baldwin 		return (EINVAL);
807838d9858SBrooks Davis 	groups = malloc(uap->gidsetsize * sizeof(gid_t), M_TEMP, M_WAITOK);
8083cb83e71SJohn Baldwin 	error = copyin(uap->gidset, groups, uap->gidsetsize * sizeof(gid_t));
8093cb83e71SJohn Baldwin 	if (error)
810838d9858SBrooks Davis 		goto out;
811838d9858SBrooks Davis 	error = kern_setgroups(td, uap->gidsetsize, groups);
812838d9858SBrooks Davis out:
813838d9858SBrooks Davis 	free(groups, M_TEMP);
8143cb83e71SJohn Baldwin 	return (error);
8153cb83e71SJohn Baldwin }
8163cb83e71SJohn Baldwin 
8173cb83e71SJohn Baldwin int
8183cb83e71SJohn Baldwin kern_setgroups(struct thread *td, u_int ngrp, gid_t *groups)
8193cb83e71SJohn Baldwin {
8203cb83e71SJohn Baldwin 	struct proc *p = td->td_proc;
8213cb83e71SJohn Baldwin 	struct ucred *newcred, *oldcred;
8223cb83e71SJohn Baldwin 	int error;
8233cb83e71SJohn Baldwin 
824412f9500SBrooks Davis 	if (ngrp > ngroups_max + 1)
82507f3485dSJohn Baldwin 		return (EINVAL);
82614961ba7SRobert Watson 	AUDIT_ARG_GROUPSET(groups, ngrp);
82707f3485dSJohn Baldwin 	newcred = crget();
828838d9858SBrooks Davis 	crextend(newcred, ngrp);
82907f3485dSJohn Baldwin 	PROC_LOCK(p);
830838d9858SBrooks Davis 	oldcred = crcopysafe(p, newcred);
831030a28b3SRobert Watson 
832030a28b3SRobert Watson #ifdef MAC
8336f6174a7SRobert Watson 	error = mac_cred_check_setgroups(oldcred, ngrp, groups);
834030a28b3SRobert Watson 	if (error)
835030a28b3SRobert Watson 		goto fail;
836030a28b3SRobert Watson #endif
837030a28b3SRobert Watson 
83832f9753cSRobert Watson 	error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0);
839030a28b3SRobert Watson 	if (error)
840030a28b3SRobert Watson 		goto fail;
84107f3485dSJohn Baldwin 
8428a5d815aSPeter Wemm 	if (ngrp < 1) {
8438a5d815aSPeter Wemm 		/*
8448a5d815aSPeter Wemm 		 * setgroups(0, NULL) is a legitimate way of clearing the
8458a5d815aSPeter Wemm 		 * groups vector on non-BSD systems (which generally do not
8468a5d815aSPeter Wemm 		 * have the egid in the groups[0]).  We risk security holes
8478a5d815aSPeter Wemm 		 * when running non-BSD software if we do not do the same.
8488a5d815aSPeter Wemm 		 */
849b1fc0ec1SRobert Watson 		newcred->cr_ngroups = 1;
8508a5d815aSPeter Wemm 	} else {
851838d9858SBrooks Davis 		crsetgroups_locked(newcred, ngrp, groups);
8528a5d815aSPeter Wemm 	}
853d5f81602SSean Eric Fagan 	setsugid(p);
854b1fc0ec1SRobert Watson 	p->p_ucred = newcred;
85507f3485dSJohn Baldwin 	PROC_UNLOCK(p);
856b1fc0ec1SRobert Watson 	crfree(oldcred);
85707f3485dSJohn Baldwin 	return (0);
858030a28b3SRobert Watson 
859030a28b3SRobert Watson fail:
860030a28b3SRobert Watson 	PROC_UNLOCK(p);
861030a28b3SRobert Watson 	crfree(newcred);
862030a28b3SRobert Watson 	return (error);
863df8bae1dSRodney W. Grimes }
864df8bae1dSRodney W. Grimes 
865d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
866df8bae1dSRodney W. Grimes struct setreuid_args {
86700999cd6SAndrey A. Chernov 	uid_t	ruid;
86800999cd6SAndrey A. Chernov 	uid_t	euid;
869df8bae1dSRodney W. Grimes };
870d2d3e875SBruce Evans #endif
871df8bae1dSRodney W. Grimes /* ARGSUSED */
87226f9a767SRodney W. Grimes int
8734c44ad8eSJohn Baldwin setreuid(register struct thread *td, struct setreuid_args *uap)
874df8bae1dSRodney W. Grimes {
875b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
876b1fc0ec1SRobert Watson 	struct ucred *newcred, *oldcred;
877eb725b4eSRobert Watson 	uid_t euid, ruid;
8781419eacbSAlfred Perlstein 	struct uidinfo *euip, *ruip;
879eb725b4eSRobert Watson 	int error;
880df8bae1dSRodney W. Grimes 
88100999cd6SAndrey A. Chernov 	euid = uap->euid;
882eb725b4eSRobert Watson 	ruid = uap->ruid;
88314961ba7SRobert Watson 	AUDIT_ARG_EUID(euid);
88414961ba7SRobert Watson 	AUDIT_ARG_RUID(ruid);
88507f3485dSJohn Baldwin 	newcred = crget();
8861419eacbSAlfred Perlstein 	euip = uifind(euid);
8871419eacbSAlfred Perlstein 	ruip = uifind(ruid);
88807f3485dSJohn Baldwin 	PROC_LOCK(p);
889838d9858SBrooks Davis 	oldcred = crcopysafe(p, newcred);
890030a28b3SRobert Watson 
891030a28b3SRobert Watson #ifdef MAC
8926f6174a7SRobert Watson 	error = mac_cred_check_setreuid(oldcred, ruid, euid);
893030a28b3SRobert Watson 	if (error)
894030a28b3SRobert Watson 		goto fail;
895030a28b3SRobert Watson #endif
896030a28b3SRobert Watson 
897b1fc0ec1SRobert Watson 	if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
898b1fc0ec1SRobert Watson 	      ruid != oldcred->cr_svuid) ||
899b1fc0ec1SRobert Watson 	     (euid != (uid_t)-1 && euid != oldcred->cr_uid &&
900b1fc0ec1SRobert Watson 	      euid != oldcred->cr_ruid && euid != oldcred->cr_svuid)) &&
90132f9753cSRobert Watson 	    (error = priv_check_cred(oldcred, PRIV_CRED_SETREUID, 0)) != 0)
902030a28b3SRobert Watson 		goto fail;
903030a28b3SRobert Watson 
904b1fc0ec1SRobert Watson 	if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
9051419eacbSAlfred Perlstein 		change_euid(newcred, euip);
906d5f81602SSean Eric Fagan 		setsugid(p);
907a89a5370SPeter Wemm 	}
908b1fc0ec1SRobert Watson 	if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
9091419eacbSAlfred Perlstein 		change_ruid(newcred, ruip);
910d5f81602SSean Eric Fagan 		setsugid(p);
91100999cd6SAndrey A. Chernov 	}
912b1fc0ec1SRobert Watson 	if ((ruid != (uid_t)-1 || newcred->cr_uid != newcred->cr_ruid) &&
913b1fc0ec1SRobert Watson 	    newcred->cr_svuid != newcred->cr_uid) {
914b1fc0ec1SRobert Watson 		change_svuid(newcred, newcred->cr_uid);
915d5f81602SSean Eric Fagan 		setsugid(p);
916a89a5370SPeter Wemm 	}
917b1fc0ec1SRobert Watson 	p->p_ucred = newcred;
91807f3485dSJohn Baldwin 	PROC_UNLOCK(p);
9191419eacbSAlfred Perlstein 	uifree(ruip);
9201419eacbSAlfred Perlstein 	uifree(euip);
921b1fc0ec1SRobert Watson 	crfree(oldcred);
92207f3485dSJohn Baldwin 	return (0);
923030a28b3SRobert Watson 
924030a28b3SRobert Watson fail:
925030a28b3SRobert Watson 	PROC_UNLOCK(p);
926030a28b3SRobert Watson 	uifree(ruip);
927030a28b3SRobert Watson 	uifree(euip);
928030a28b3SRobert Watson 	crfree(newcred);
929030a28b3SRobert Watson 	return (error);
930df8bae1dSRodney W. Grimes }
931df8bae1dSRodney W. Grimes 
932d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
933df8bae1dSRodney W. Grimes struct setregid_args {
93400999cd6SAndrey A. Chernov 	gid_t	rgid;
93500999cd6SAndrey A. Chernov 	gid_t	egid;
936df8bae1dSRodney W. Grimes };
937d2d3e875SBruce Evans #endif
938df8bae1dSRodney W. Grimes /* ARGSUSED */
93926f9a767SRodney W. Grimes int
9404c44ad8eSJohn Baldwin setregid(register struct thread *td, struct setregid_args *uap)
941df8bae1dSRodney W. Grimes {
942b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
943b1fc0ec1SRobert Watson 	struct ucred *newcred, *oldcred;
944eb725b4eSRobert Watson 	gid_t egid, rgid;
945eb725b4eSRobert Watson 	int error;
946df8bae1dSRodney W. Grimes 
94700999cd6SAndrey A. Chernov 	egid = uap->egid;
948eb725b4eSRobert Watson 	rgid = uap->rgid;
94914961ba7SRobert Watson 	AUDIT_ARG_EGID(egid);
95014961ba7SRobert Watson 	AUDIT_ARG_RGID(rgid);
95107f3485dSJohn Baldwin 	newcred = crget();
95207f3485dSJohn Baldwin 	PROC_LOCK(p);
953838d9858SBrooks Davis 	oldcred = crcopysafe(p, newcred);
954030a28b3SRobert Watson 
955030a28b3SRobert Watson #ifdef MAC
9566f6174a7SRobert Watson 	error = mac_cred_check_setregid(oldcred, rgid, egid);
957030a28b3SRobert Watson 	if (error)
958030a28b3SRobert Watson 		goto fail;
959030a28b3SRobert Watson #endif
960030a28b3SRobert Watson 
961b1fc0ec1SRobert Watson 	if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
962b1fc0ec1SRobert Watson 	    rgid != oldcred->cr_svgid) ||
963b1fc0ec1SRobert Watson 	     (egid != (gid_t)-1 && egid != oldcred->cr_groups[0] &&
964b1fc0ec1SRobert Watson 	     egid != oldcred->cr_rgid && egid != oldcred->cr_svgid)) &&
96532f9753cSRobert Watson 	    (error = priv_check_cred(oldcred, PRIV_CRED_SETREGID, 0)) != 0)
966030a28b3SRobert Watson 		goto fail;
96707f3485dSJohn Baldwin 
968b1fc0ec1SRobert Watson 	if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
969b1fc0ec1SRobert Watson 		change_egid(newcred, egid);
970d5f81602SSean Eric Fagan 		setsugid(p);
971a89a5370SPeter Wemm 	}
972b1fc0ec1SRobert Watson 	if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
973b1fc0ec1SRobert Watson 		change_rgid(newcred, rgid);
974d5f81602SSean Eric Fagan 		setsugid(p);
975a89a5370SPeter Wemm 	}
976b1fc0ec1SRobert Watson 	if ((rgid != (gid_t)-1 || newcred->cr_groups[0] != newcred->cr_rgid) &&
977b1fc0ec1SRobert Watson 	    newcred->cr_svgid != newcred->cr_groups[0]) {
978b1fc0ec1SRobert Watson 		change_svgid(newcred, newcred->cr_groups[0]);
979d5f81602SSean Eric Fagan 		setsugid(p);
980a89a5370SPeter Wemm 	}
9814589be70SRuslan Ermilov 	p->p_ucred = newcred;
98207f3485dSJohn Baldwin 	PROC_UNLOCK(p);
9834589be70SRuslan Ermilov 	crfree(oldcred);
98407f3485dSJohn Baldwin 	return (0);
985030a28b3SRobert Watson 
986030a28b3SRobert Watson fail:
987030a28b3SRobert Watson 	PROC_UNLOCK(p);
988030a28b3SRobert Watson 	crfree(newcred);
989030a28b3SRobert Watson 	return (error);
990df8bae1dSRodney W. Grimes }
991df8bae1dSRodney W. Grimes 
9928ccd6334SPeter Wemm /*
993873fbcd7SRobert Watson  * setresuid(ruid, euid, suid) is like setreuid except control over the saved
994873fbcd7SRobert Watson  * uid is explicit.
9958ccd6334SPeter Wemm  */
9968ccd6334SPeter Wemm #ifndef _SYS_SYSPROTO_H_
9978ccd6334SPeter Wemm struct setresuid_args {
9988ccd6334SPeter Wemm 	uid_t	ruid;
9998ccd6334SPeter Wemm 	uid_t	euid;
10008ccd6334SPeter Wemm 	uid_t	suid;
10018ccd6334SPeter Wemm };
10028ccd6334SPeter Wemm #endif
10038ccd6334SPeter Wemm /* ARGSUSED */
10048ccd6334SPeter Wemm int
10054c44ad8eSJohn Baldwin setresuid(register struct thread *td, struct setresuid_args *uap)
10068ccd6334SPeter Wemm {
1007b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1008b1fc0ec1SRobert Watson 	struct ucred *newcred, *oldcred;
1009eb725b4eSRobert Watson 	uid_t euid, ruid, suid;
10101419eacbSAlfred Perlstein 	struct uidinfo *euip, *ruip;
10118ccd6334SPeter Wemm 	int error;
10128ccd6334SPeter Wemm 
10138ccd6334SPeter Wemm 	euid = uap->euid;
1014eb725b4eSRobert Watson 	ruid = uap->ruid;
10158ccd6334SPeter Wemm 	suid = uap->suid;
101614961ba7SRobert Watson 	AUDIT_ARG_EUID(euid);
101714961ba7SRobert Watson 	AUDIT_ARG_RUID(ruid);
101814961ba7SRobert Watson 	AUDIT_ARG_SUID(suid);
101907f3485dSJohn Baldwin 	newcred = crget();
10201419eacbSAlfred Perlstein 	euip = uifind(euid);
10211419eacbSAlfred Perlstein 	ruip = uifind(ruid);
102207f3485dSJohn Baldwin 	PROC_LOCK(p);
1023838d9858SBrooks Davis 	oldcred = crcopysafe(p, newcred);
1024030a28b3SRobert Watson 
1025030a28b3SRobert Watson #ifdef MAC
10266f6174a7SRobert Watson 	error = mac_cred_check_setresuid(oldcred, ruid, euid, suid);
1027030a28b3SRobert Watson 	if (error)
1028030a28b3SRobert Watson 		goto fail;
1029030a28b3SRobert Watson #endif
1030030a28b3SRobert Watson 
1031b1fc0ec1SRobert Watson 	if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
1032b1fc0ec1SRobert Watson 	     ruid != oldcred->cr_svuid &&
1033b1fc0ec1SRobert Watson 	      ruid != oldcred->cr_uid) ||
1034b1fc0ec1SRobert Watson 	     (euid != (uid_t)-1 && euid != oldcred->cr_ruid &&
1035b1fc0ec1SRobert Watson 	    euid != oldcred->cr_svuid &&
1036b1fc0ec1SRobert Watson 	      euid != oldcred->cr_uid) ||
1037b1fc0ec1SRobert Watson 	     (suid != (uid_t)-1 && suid != oldcred->cr_ruid &&
1038b1fc0ec1SRobert Watson 	    suid != oldcred->cr_svuid &&
1039b1fc0ec1SRobert Watson 	      suid != oldcred->cr_uid)) &&
104032f9753cSRobert Watson 	    (error = priv_check_cred(oldcred, PRIV_CRED_SETRESUID, 0)) != 0)
1041030a28b3SRobert Watson 		goto fail;
104207f3485dSJohn Baldwin 
1043b1fc0ec1SRobert Watson 	if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
10441419eacbSAlfred Perlstein 		change_euid(newcred, euip);
10458ccd6334SPeter Wemm 		setsugid(p);
10468ccd6334SPeter Wemm 	}
1047b1fc0ec1SRobert Watson 	if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
10481419eacbSAlfred Perlstein 		change_ruid(newcred, ruip);
10498ccd6334SPeter Wemm 		setsugid(p);
10508ccd6334SPeter Wemm 	}
1051b1fc0ec1SRobert Watson 	if (suid != (uid_t)-1 && oldcred->cr_svuid != suid) {
1052b1fc0ec1SRobert Watson 		change_svuid(newcred, suid);
10538ccd6334SPeter Wemm 		setsugid(p);
10548ccd6334SPeter Wemm 	}
1055b1fc0ec1SRobert Watson 	p->p_ucred = newcred;
105607f3485dSJohn Baldwin 	PROC_UNLOCK(p);
10571419eacbSAlfred Perlstein 	uifree(ruip);
10581419eacbSAlfred Perlstein 	uifree(euip);
1059b1fc0ec1SRobert Watson 	crfree(oldcred);
106007f3485dSJohn Baldwin 	return (0);
1061030a28b3SRobert Watson 
1062030a28b3SRobert Watson fail:
1063030a28b3SRobert Watson 	PROC_UNLOCK(p);
1064030a28b3SRobert Watson 	uifree(ruip);
1065030a28b3SRobert Watson 	uifree(euip);
1066030a28b3SRobert Watson 	crfree(newcred);
1067030a28b3SRobert Watson 	return (error);
1068030a28b3SRobert Watson 
10698ccd6334SPeter Wemm }
10708ccd6334SPeter Wemm 
10718ccd6334SPeter Wemm /*
1072873fbcd7SRobert Watson  * setresgid(rgid, egid, sgid) is like setregid except control over the saved
1073873fbcd7SRobert Watson  * gid is explicit.
10748ccd6334SPeter Wemm  */
10758ccd6334SPeter Wemm #ifndef _SYS_SYSPROTO_H_
10768ccd6334SPeter Wemm struct setresgid_args {
10778ccd6334SPeter Wemm 	gid_t	rgid;
10788ccd6334SPeter Wemm 	gid_t	egid;
10798ccd6334SPeter Wemm 	gid_t	sgid;
10808ccd6334SPeter Wemm };
10818ccd6334SPeter Wemm #endif
10828ccd6334SPeter Wemm /* ARGSUSED */
10838ccd6334SPeter Wemm int
10844c44ad8eSJohn Baldwin setresgid(register struct thread *td, struct setresgid_args *uap)
10858ccd6334SPeter Wemm {
1086b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1087b1fc0ec1SRobert Watson 	struct ucred *newcred, *oldcred;
1088eb725b4eSRobert Watson 	gid_t egid, rgid, sgid;
10898ccd6334SPeter Wemm 	int error;
10908ccd6334SPeter Wemm 
10918ccd6334SPeter Wemm 	egid = uap->egid;
1092eb725b4eSRobert Watson 	rgid = uap->rgid;
10938ccd6334SPeter Wemm 	sgid = uap->sgid;
109414961ba7SRobert Watson 	AUDIT_ARG_EGID(egid);
109514961ba7SRobert Watson 	AUDIT_ARG_RGID(rgid);
109614961ba7SRobert Watson 	AUDIT_ARG_SGID(sgid);
109707f3485dSJohn Baldwin 	newcred = crget();
109807f3485dSJohn Baldwin 	PROC_LOCK(p);
1099838d9858SBrooks Davis 	oldcred = crcopysafe(p, newcred);
1100030a28b3SRobert Watson 
1101030a28b3SRobert Watson #ifdef MAC
11026f6174a7SRobert Watson 	error = mac_cred_check_setresgid(oldcred, rgid, egid, sgid);
1103030a28b3SRobert Watson 	if (error)
1104030a28b3SRobert Watson 		goto fail;
1105030a28b3SRobert Watson #endif
1106030a28b3SRobert Watson 
1107b1fc0ec1SRobert Watson 	if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
1108b1fc0ec1SRobert Watson 	      rgid != oldcred->cr_svgid &&
1109b1fc0ec1SRobert Watson 	      rgid != oldcred->cr_groups[0]) ||
1110b1fc0ec1SRobert Watson 	     (egid != (gid_t)-1 && egid != oldcred->cr_rgid &&
1111b1fc0ec1SRobert Watson 	      egid != oldcred->cr_svgid &&
1112b1fc0ec1SRobert Watson 	      egid != oldcred->cr_groups[0]) ||
1113b1fc0ec1SRobert Watson 	     (sgid != (gid_t)-1 && sgid != oldcred->cr_rgid &&
1114b1fc0ec1SRobert Watson 	      sgid != oldcred->cr_svgid &&
1115b1fc0ec1SRobert Watson 	      sgid != oldcred->cr_groups[0])) &&
111632f9753cSRobert Watson 	    (error = priv_check_cred(oldcred, PRIV_CRED_SETRESGID, 0)) != 0)
1117030a28b3SRobert Watson 		goto fail;
111807f3485dSJohn Baldwin 
1119b1fc0ec1SRobert Watson 	if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
1120b1fc0ec1SRobert Watson 		change_egid(newcred, egid);
11218ccd6334SPeter Wemm 		setsugid(p);
11228ccd6334SPeter Wemm 	}
1123b1fc0ec1SRobert Watson 	if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
1124b1fc0ec1SRobert Watson 		change_rgid(newcred, rgid);
11258ccd6334SPeter Wemm 		setsugid(p);
11268ccd6334SPeter Wemm 	}
1127b1fc0ec1SRobert Watson 	if (sgid != (gid_t)-1 && oldcred->cr_svgid != sgid) {
1128b1fc0ec1SRobert Watson 		change_svgid(newcred, sgid);
11298ccd6334SPeter Wemm 		setsugid(p);
11308ccd6334SPeter Wemm 	}
1131b1fc0ec1SRobert Watson 	p->p_ucred = newcred;
113207f3485dSJohn Baldwin 	PROC_UNLOCK(p);
1133b1fc0ec1SRobert Watson 	crfree(oldcred);
113407f3485dSJohn Baldwin 	return (0);
1135030a28b3SRobert Watson 
1136030a28b3SRobert Watson fail:
1137030a28b3SRobert Watson 	PROC_UNLOCK(p);
1138030a28b3SRobert Watson 	crfree(newcred);
1139030a28b3SRobert Watson 	return (error);
11408ccd6334SPeter Wemm }
11418ccd6334SPeter Wemm 
11428ccd6334SPeter Wemm #ifndef _SYS_SYSPROTO_H_
11438ccd6334SPeter Wemm struct getresuid_args {
11448ccd6334SPeter Wemm 	uid_t	*ruid;
11458ccd6334SPeter Wemm 	uid_t	*euid;
11468ccd6334SPeter Wemm 	uid_t	*suid;
11478ccd6334SPeter Wemm };
11488ccd6334SPeter Wemm #endif
11498ccd6334SPeter Wemm /* ARGSUSED */
11508ccd6334SPeter Wemm int
11514c44ad8eSJohn Baldwin getresuid(register struct thread *td, struct getresuid_args *uap)
11528ccd6334SPeter Wemm {
1153835a82eeSMatthew Dillon 	struct ucred *cred;
11548ccd6334SPeter Wemm 	int error1 = 0, error2 = 0, error3 = 0;
11558ccd6334SPeter Wemm 
1156d74ac681SMatthew Dillon 	cred = td->td_ucred;
11578ccd6334SPeter Wemm 	if (uap->ruid)
11587f05b035SAlfred Perlstein 		error1 = copyout(&cred->cr_ruid,
11597f05b035SAlfred Perlstein 		    uap->ruid, sizeof(cred->cr_ruid));
11608ccd6334SPeter Wemm 	if (uap->euid)
11617f05b035SAlfred Perlstein 		error2 = copyout(&cred->cr_uid,
11627f05b035SAlfred Perlstein 		    uap->euid, sizeof(cred->cr_uid));
11638ccd6334SPeter Wemm 	if (uap->suid)
11647f05b035SAlfred Perlstein 		error3 = copyout(&cred->cr_svuid,
11657f05b035SAlfred Perlstein 		    uap->suid, sizeof(cred->cr_svuid));
1166eb725b4eSRobert Watson 	return (error1 ? error1 : error2 ? error2 : error3);
11678ccd6334SPeter Wemm }
11688ccd6334SPeter Wemm 
11698ccd6334SPeter Wemm #ifndef _SYS_SYSPROTO_H_
11708ccd6334SPeter Wemm struct getresgid_args {
11718ccd6334SPeter Wemm 	gid_t	*rgid;
11728ccd6334SPeter Wemm 	gid_t	*egid;
11738ccd6334SPeter Wemm 	gid_t	*sgid;
11748ccd6334SPeter Wemm };
11758ccd6334SPeter Wemm #endif
11768ccd6334SPeter Wemm /* ARGSUSED */
11778ccd6334SPeter Wemm int
11784c44ad8eSJohn Baldwin getresgid(register struct thread *td, struct getresgid_args *uap)
11798ccd6334SPeter Wemm {
1180835a82eeSMatthew Dillon 	struct ucred *cred;
11818ccd6334SPeter Wemm 	int error1 = 0, error2 = 0, error3 = 0;
11828ccd6334SPeter Wemm 
1183d74ac681SMatthew Dillon 	cred = td->td_ucred;
11848ccd6334SPeter Wemm 	if (uap->rgid)
11857f05b035SAlfred Perlstein 		error1 = copyout(&cred->cr_rgid,
11867f05b035SAlfred Perlstein 		    uap->rgid, sizeof(cred->cr_rgid));
11878ccd6334SPeter Wemm 	if (uap->egid)
11887f05b035SAlfred Perlstein 		error2 = copyout(&cred->cr_groups[0],
11897f05b035SAlfred Perlstein 		    uap->egid, sizeof(cred->cr_groups[0]));
11908ccd6334SPeter Wemm 	if (uap->sgid)
11917f05b035SAlfred Perlstein 		error3 = copyout(&cred->cr_svgid,
11927f05b035SAlfred Perlstein 		    uap->sgid, sizeof(cred->cr_svgid));
1193eb725b4eSRobert Watson 	return (error1 ? error1 : error2 ? error2 : error3);
11948ccd6334SPeter Wemm }
11958ccd6334SPeter Wemm 
1196b67cbc65SPeter Wemm #ifndef _SYS_SYSPROTO_H_
1197b67cbc65SPeter Wemm struct issetugid_args {
1198b67cbc65SPeter Wemm 	int dummy;
1199b67cbc65SPeter Wemm };
1200b67cbc65SPeter Wemm #endif
1201b67cbc65SPeter Wemm /* ARGSUSED */
1202b67cbc65SPeter Wemm int
12034c44ad8eSJohn Baldwin issetugid(register struct thread *td, struct issetugid_args *uap)
1204b67cbc65SPeter Wemm {
1205b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1206b40ce416SJulian Elischer 
1207b67cbc65SPeter Wemm 	/*
1208b67cbc65SPeter Wemm 	 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
1209b67cbc65SPeter Wemm 	 * we use P_SUGID because we consider changing the owners as
1210b67cbc65SPeter Wemm 	 * "tainting" as well.
1211b67cbc65SPeter Wemm 	 * This is significant for procs that start as root and "become"
1212b67cbc65SPeter Wemm 	 * a user without an exec - programs cannot know *everything*
1213b67cbc65SPeter Wemm 	 * that libc *might* have put in their data segment.
1214b67cbc65SPeter Wemm 	 */
1215f591779bSSeigo Tanimura 	PROC_LOCK(p);
1216b40ce416SJulian Elischer 	td->td_retval[0] = (p->p_flag & P_SUGID) ? 1 : 0;
1217f591779bSSeigo Tanimura 	PROC_UNLOCK(p);
1218b67cbc65SPeter Wemm 	return (0);
1219b67cbc65SPeter Wemm }
1220b67cbc65SPeter Wemm 
1221130d0157SRobert Watson int
12224c44ad8eSJohn Baldwin __setugid(struct thread *td, struct __setugid_args *uap)
1223130d0157SRobert Watson {
1224130d0157SRobert Watson #ifdef REGRESSION
122507f3485dSJohn Baldwin 	struct proc *p;
1226835a82eeSMatthew Dillon 
122707f3485dSJohn Baldwin 	p = td->td_proc;
1228130d0157SRobert Watson 	switch (uap->flag) {
1229130d0157SRobert Watson 	case 0:
123007f3485dSJohn Baldwin 		PROC_LOCK(p);
123107f3485dSJohn Baldwin 		p->p_flag &= ~P_SUGID;
123207f3485dSJohn Baldwin 		PROC_UNLOCK(p);
123307f3485dSJohn Baldwin 		return (0);
123407f3485dSJohn Baldwin 	case 1:
123507f3485dSJohn Baldwin 		PROC_LOCK(p);
123607f3485dSJohn Baldwin 		p->p_flag |= P_SUGID;
123707f3485dSJohn Baldwin 		PROC_UNLOCK(p);
123807f3485dSJohn Baldwin 		return (0);
123907f3485dSJohn Baldwin 	default:
124007f3485dSJohn Baldwin 		return (EINVAL);
124107f3485dSJohn Baldwin 	}
1242130d0157SRobert Watson #else /* !REGRESSION */
1243eb725b4eSRobert Watson 
1244130d0157SRobert Watson 	return (ENOSYS);
1245eb725b4eSRobert Watson #endif /* REGRESSION */
1246130d0157SRobert Watson }
1247130d0157SRobert Watson 
1248df8bae1dSRodney W. Grimes /*
1249df8bae1dSRodney W. Grimes  * Check if gid is a member of the group set.
1250df8bae1dSRodney W. Grimes  */
125126f9a767SRodney W. Grimes int
12524c44ad8eSJohn Baldwin groupmember(gid_t gid, struct ucred *cred)
1253df8bae1dSRodney W. Grimes {
12547f92e578SBrooks Davis 	int l;
12557f92e578SBrooks Davis 	int h;
12567f92e578SBrooks Davis 	int m;
1257df8bae1dSRodney W. Grimes 
12587f92e578SBrooks Davis 	if (cred->cr_groups[0] == gid)
1259df8bae1dSRodney W. Grimes 		return(1);
12607f92e578SBrooks Davis 
12617f92e578SBrooks Davis 	/*
12627f92e578SBrooks Davis 	 * If gid was not our primary group, perform a binary search
12637f92e578SBrooks Davis 	 * of the supplemental groups.  This is possible because we
12647f92e578SBrooks Davis 	 * sort the groups in crsetgroups().
12657f92e578SBrooks Davis 	 */
12667f92e578SBrooks Davis 	l = 1;
12677f92e578SBrooks Davis 	h = cred->cr_ngroups;
12687f92e578SBrooks Davis 	while (l < h) {
12697f92e578SBrooks Davis 		m = l + ((h - l) / 2);
12707f92e578SBrooks Davis 		if (cred->cr_groups[m] < gid)
12717f92e578SBrooks Davis 			l = m + 1;
12727f92e578SBrooks Davis 		else
12737f92e578SBrooks Davis 			h = m;
12747f92e578SBrooks Davis 	}
12757f92e578SBrooks Davis 	if ((l < cred->cr_ngroups) && (cred->cr_groups[l] == gid))
12767f92e578SBrooks Davis 		return (1);
12777f92e578SBrooks Davis 
1278df8bae1dSRodney W. Grimes 	return (0);
1279df8bae1dSRodney W. Grimes }
1280df8bae1dSRodney W. Grimes 
12813b243b72SRobert Watson /*
1282eb725b4eSRobert Watson  * Test the active securelevel against a given level.  securelevel_gt()
1283eb725b4eSRobert Watson  * implements (securelevel > level).  securelevel_ge() implements
1284eb725b4eSRobert Watson  * (securelevel >= level).  Note that the logic is inverted -- these
1285eb725b4eSRobert Watson  * functions return EPERM on "success" and 0 on "failure".
12863ca719f1SRobert Watson  *
12870304c731SJamie Gritton  * Due to care taken when setting the securelevel, we know that no jail will
12880304c731SJamie Gritton  * be less secure that its parent (or the physical system), so it is sufficient
12890304c731SJamie Gritton  * to test the current jail only.
12900304c731SJamie Gritton  *
1291800c9408SRobert Watson  * XXXRW: Possibly since this has to do with privilege, it should move to
1292800c9408SRobert Watson  * kern_priv.c.
12933ca719f1SRobert Watson  */
12943ca719f1SRobert Watson int
12953ca719f1SRobert Watson securelevel_gt(struct ucred *cr, int level)
12963ca719f1SRobert Watson {
12973ca719f1SRobert Watson 
12980304c731SJamie Gritton 	return (cr->cr_prison->pr_securelevel > level ? EPERM : 0);
12993ca719f1SRobert Watson }
13003ca719f1SRobert Watson 
13013ca719f1SRobert Watson int
13023ca719f1SRobert Watson securelevel_ge(struct ucred *cr, int level)
13033ca719f1SRobert Watson {
13043ca719f1SRobert Watson 
13050304c731SJamie Gritton 	return (cr->cr_prison->pr_securelevel >= level ? EPERM : 0);
13063ca719f1SRobert Watson }
13073ca719f1SRobert Watson 
13088a7d8cc6SRobert Watson /*
1309e409590dSRobert Watson  * 'see_other_uids' determines whether or not visibility of processes
1310eb725b4eSRobert Watson  * and sockets with credentials holding different real uids is possible
131148713bdcSRobert Watson  * using a variety of system MIBs.
1312eb725b4eSRobert Watson  * XXX: data declarations should be together near the beginning of the file.
13138a7d8cc6SRobert Watson  */
1314e409590dSRobert Watson static int	see_other_uids = 1;
1315d0615c64SAndrew R. Reiter SYSCTL_INT(_security_bsd, OID_AUTO, see_other_uids, CTLFLAG_RW,
1316eb725b4eSRobert Watson     &see_other_uids, 0,
13178a7d8cc6SRobert Watson     "Unprivileged processes may see subjects/objects with different real uid");
13188a7d8cc6SRobert Watson 
1319*1a996ed1SEdward Tomasz Napierala /*-
13201b350b45SRobert Watson  * Determine if u1 "can see" the subject specified by u2, according to the
13211b350b45SRobert Watson  * 'see_other_uids' policy.
13221b350b45SRobert Watson  * Returns: 0 for permitted, ESRCH otherwise
13231b350b45SRobert Watson  * Locks: none
13241b350b45SRobert Watson  * References: *u1 and *u2 must not change during the call
13251b350b45SRobert Watson  *             u1 may equal u2, in which case only one reference is required
13261b350b45SRobert Watson  */
13271b350b45SRobert Watson static int
13281b350b45SRobert Watson cr_seeotheruids(struct ucred *u1, struct ucred *u2)
13291b350b45SRobert Watson {
13301b350b45SRobert Watson 
13311b350b45SRobert Watson 	if (!see_other_uids && u1->cr_ruid != u2->cr_ruid) {
133232f9753cSRobert Watson 		if (priv_check_cred(u1, PRIV_SEEOTHERUIDS, 0) != 0)
13331b350b45SRobert Watson 			return (ESRCH);
13341b350b45SRobert Watson 	}
13351b350b45SRobert Watson 	return (0);
13361b350b45SRobert Watson }
13371b350b45SRobert Watson 
133864d19c2eSRobert Watson /*
133964d19c2eSRobert Watson  * 'see_other_gids' determines whether or not visibility of processes
134064d19c2eSRobert Watson  * and sockets with credentials holding different real gids is possible
134164d19c2eSRobert Watson  * using a variety of system MIBs.
134264d19c2eSRobert Watson  * XXX: data declarations should be together near the beginning of the file.
134364d19c2eSRobert Watson  */
134464d19c2eSRobert Watson static int	see_other_gids = 1;
134564d19c2eSRobert Watson SYSCTL_INT(_security_bsd, OID_AUTO, see_other_gids, CTLFLAG_RW,
134664d19c2eSRobert Watson     &see_other_gids, 0,
134764d19c2eSRobert Watson     "Unprivileged processes may see subjects/objects with different real gid");
134864d19c2eSRobert Watson 
134964d19c2eSRobert Watson /*
135064d19c2eSRobert Watson  * Determine if u1 can "see" the subject specified by u2, according to the
135164d19c2eSRobert Watson  * 'see_other_gids' policy.
135264d19c2eSRobert Watson  * Returns: 0 for permitted, ESRCH otherwise
135364d19c2eSRobert Watson  * Locks: none
135464d19c2eSRobert Watson  * References: *u1 and *u2 must not change during the call
135564d19c2eSRobert Watson  *             u1 may equal u2, in which case only one reference is required
135664d19c2eSRobert Watson  */
135764d19c2eSRobert Watson static int
135864d19c2eSRobert Watson cr_seeothergids(struct ucred *u1, struct ucred *u2)
135964d19c2eSRobert Watson {
136064d19c2eSRobert Watson 	int i, match;
136164d19c2eSRobert Watson 
136264d19c2eSRobert Watson 	if (!see_other_gids) {
136364d19c2eSRobert Watson 		match = 0;
136464d19c2eSRobert Watson 		for (i = 0; i < u1->cr_ngroups; i++) {
136564d19c2eSRobert Watson 			if (groupmember(u1->cr_groups[i], u2))
136664d19c2eSRobert Watson 				match = 1;
136764d19c2eSRobert Watson 			if (match)
136864d19c2eSRobert Watson 				break;
136964d19c2eSRobert Watson 		}
137064d19c2eSRobert Watson 		if (!match) {
137132f9753cSRobert Watson 			if (priv_check_cred(u1, PRIV_SEEOTHERGIDS, 0) != 0)
137264d19c2eSRobert Watson 				return (ESRCH);
137364d19c2eSRobert Watson 		}
137464d19c2eSRobert Watson 	}
137564d19c2eSRobert Watson 	return (0);
137664d19c2eSRobert Watson }
137764d19c2eSRobert Watson 
1378*1a996ed1SEdward Tomasz Napierala /*-
13797fd6a959SRobert Watson  * Determine if u1 "can see" the subject specified by u2.
1380ed639720SRobert Watson  * Returns: 0 for permitted, an errno value otherwise
1381ed639720SRobert Watson  * Locks: none
1382eb725b4eSRobert Watson  * References: *u1 and *u2 must not change during the call
1383ed639720SRobert Watson  *             u1 may equal u2, in which case only one reference is required
1384ed639720SRobert Watson  */
1385ed639720SRobert Watson int
138694088977SRobert Watson cr_cansee(struct ucred *u1, struct ucred *u2)
1387a9e0361bSPoul-Henning Kamp {
138891421ba2SRobert Watson 	int error;
1389a9e0361bSPoul-Henning Kamp 
1390ed639720SRobert Watson 	if ((error = prison_check(u1, u2)))
139191421ba2SRobert Watson 		return (error);
13928a1d977dSRobert Watson #ifdef MAC
139330d239bcSRobert Watson 	if ((error = mac_cred_check_visible(u1, u2)))
13948a1d977dSRobert Watson 		return (error);
13958a1d977dSRobert Watson #endif
13961b350b45SRobert Watson 	if ((error = cr_seeotheruids(u1, u2)))
13971b350b45SRobert Watson 		return (error);
139864d19c2eSRobert Watson 	if ((error = cr_seeothergids(u1, u2)))
139964d19c2eSRobert Watson 		return (error);
1400387d2c03SRobert Watson 	return (0);
1401387d2c03SRobert Watson }
1402387d2c03SRobert Watson 
1403*1a996ed1SEdward Tomasz Napierala /*-
1404f44d9e24SJohn Baldwin  * Determine if td "can see" the subject specified by p.
14053b243b72SRobert Watson  * Returns: 0 for permitted, an errno value otherwise
1406f44d9e24SJohn Baldwin  * Locks: Sufficient locks to protect p->p_ucred must be held.  td really
1407f44d9e24SJohn Baldwin  *        should be curthread.
1408f44d9e24SJohn Baldwin  * References: td and p must be valid for the lifetime of the call
14093b243b72SRobert Watson  */
1410a0f75161SRobert Watson int
1411f44d9e24SJohn Baldwin p_cansee(struct thread *td, struct proc *p)
1412ed639720SRobert Watson {
1413ed639720SRobert Watson 
141494088977SRobert Watson 	/* Wrap cr_cansee() for all functionality. */
1415f44d9e24SJohn Baldwin 	KASSERT(td == curthread, ("%s: td not curthread", __func__));
1416f44d9e24SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1417f44d9e24SJohn Baldwin 	return (cr_cansee(td->td_ucred, p->p_ucred));
1418ed639720SRobert Watson }
1419ed639720SRobert Watson 
142062c45ef4SRobert Watson /*
142162c45ef4SRobert Watson  * 'conservative_signals' prevents the delivery of a broad class of
142262c45ef4SRobert Watson  * signals by unprivileged processes to processes that have changed their
142362c45ef4SRobert Watson  * credentials since the last invocation of execve().  This can prevent
142462c45ef4SRobert Watson  * the leakage of cached information or retained privileges as a result
142562c45ef4SRobert Watson  * of a common class of signal-related vulnerabilities.  However, this
142662c45ef4SRobert Watson  * may interfere with some applications that expect to be able to
142762c45ef4SRobert Watson  * deliver these signals to peer processes after having given up
142862c45ef4SRobert Watson  * privilege.
142962c45ef4SRobert Watson  */
143062c45ef4SRobert Watson static int	conservative_signals = 1;
143162c45ef4SRobert Watson SYSCTL_INT(_security_bsd, OID_AUTO, conservative_signals, CTLFLAG_RW,
143262c45ef4SRobert Watson     &conservative_signals, 0, "Unprivileged processes prevented from "
143362c45ef4SRobert Watson     "sending certain signals to processes whose credentials have changed");
1434*1a996ed1SEdward Tomasz Napierala /*-
1435c83f8015SRobert Watson  * Determine whether cred may deliver the specified signal to proc.
1436c83f8015SRobert Watson  * Returns: 0 for permitted, an errno value otherwise.
1437c83f8015SRobert Watson  * Locks: A lock must be held for proc.
1438c83f8015SRobert Watson  * References: cred and proc must be valid for the lifetime of the call.
14394c5eb9c3SRobert Watson  */
14404c5eb9c3SRobert Watson int
14411a88a252SMaxim Sobolev cr_cansignal(struct ucred *cred, struct proc *proc, int signum)
1442387d2c03SRobert Watson {
144391421ba2SRobert Watson 	int error;
1444387d2c03SRobert Watson 
1445f44d9e24SJohn Baldwin 	PROC_LOCK_ASSERT(proc, MA_OWNED);
14464c5eb9c3SRobert Watson 	/*
1447c83f8015SRobert Watson 	 * Jail semantics limit the scope of signalling to proc in the
1448c83f8015SRobert Watson 	 * same jail as cred, if cred is in jail.
14494c5eb9c3SRobert Watson 	 */
1450c83f8015SRobert Watson 	error = prison_check(cred, proc->p_ucred);
1451c83f8015SRobert Watson 	if (error)
145291421ba2SRobert Watson 		return (error);
14538a1d977dSRobert Watson #ifdef MAC
145430d239bcSRobert Watson 	if ((error = mac_proc_check_signal(cred, proc, signum)))
14558a1d977dSRobert Watson 		return (error);
14568a1d977dSRobert Watson #endif
145764d19c2eSRobert Watson 	if ((error = cr_seeotheruids(cred, proc->p_ucred)))
145864d19c2eSRobert Watson 		return (error);
145964d19c2eSRobert Watson 	if ((error = cr_seeothergids(cred, proc->p_ucred)))
14601b350b45SRobert Watson 		return (error);
1461387d2c03SRobert Watson 
1462387d2c03SRobert Watson 	/*
14633b243b72SRobert Watson 	 * UNIX signal semantics depend on the status of the P_SUGID
14643b243b72SRobert Watson 	 * bit on the target process.  If the bit is set, then additional
14653b243b72SRobert Watson 	 * restrictions are placed on the set of available signals.
14664c5eb9c3SRobert Watson 	 */
14671a88a252SMaxim Sobolev 	if (conservative_signals && (proc->p_flag & P_SUGID)) {
14684c5eb9c3SRobert Watson 		switch (signum) {
14694c5eb9c3SRobert Watson 		case 0:
14704c5eb9c3SRobert Watson 		case SIGKILL:
14714c5eb9c3SRobert Watson 		case SIGINT:
14724c5eb9c3SRobert Watson 		case SIGTERM:
147362c45ef4SRobert Watson 		case SIGALRM:
14744c5eb9c3SRobert Watson 		case SIGSTOP:
14754c5eb9c3SRobert Watson 		case SIGTTIN:
14764c5eb9c3SRobert Watson 		case SIGTTOU:
14774c5eb9c3SRobert Watson 		case SIGTSTP:
14784c5eb9c3SRobert Watson 		case SIGHUP:
14794c5eb9c3SRobert Watson 		case SIGUSR1:
14804c5eb9c3SRobert Watson 		case SIGUSR2:
14817fd6a959SRobert Watson 			/*
14827fd6a959SRobert Watson 			 * Generally, permit job and terminal control
14837fd6a959SRobert Watson 			 * signals.
14847fd6a959SRobert Watson 			 */
14854c5eb9c3SRobert Watson 			break;
14864c5eb9c3SRobert Watson 		default:
1487c83f8015SRobert Watson 			/* Not permitted without privilege. */
148832f9753cSRobert Watson 			error = priv_check_cred(cred, PRIV_SIGNAL_SUGID, 0);
14894c5eb9c3SRobert Watson 			if (error)
14904c5eb9c3SRobert Watson 				return (error);
14914c5eb9c3SRobert Watson 		}
1492e9e7ff5bSRobert Watson 	}
1493e9e7ff5bSRobert Watson 
14944c5eb9c3SRobert Watson 	/*
14953b243b72SRobert Watson 	 * Generally, the target credential's ruid or svuid must match the
1496e9e7ff5bSRobert Watson 	 * subject credential's ruid or euid.
14974c5eb9c3SRobert Watson 	 */
1498c83f8015SRobert Watson 	if (cred->cr_ruid != proc->p_ucred->cr_ruid &&
1499c83f8015SRobert Watson 	    cred->cr_ruid != proc->p_ucred->cr_svuid &&
1500c83f8015SRobert Watson 	    cred->cr_uid != proc->p_ucred->cr_ruid &&
1501c83f8015SRobert Watson 	    cred->cr_uid != proc->p_ucred->cr_svuid) {
150232f9753cSRobert Watson 		error = priv_check_cred(cred, PRIV_SIGNAL_DIFFCRED, 0);
15034c5eb9c3SRobert Watson 		if (error)
15044c5eb9c3SRobert Watson 			return (error);
15054c5eb9c3SRobert Watson 	}
1506387d2c03SRobert Watson 
1507387d2c03SRobert Watson 	return (0);
1508387d2c03SRobert Watson }
1509a9e0361bSPoul-Henning Kamp 
1510*1a996ed1SEdward Tomasz Napierala /*-
1511f44d9e24SJohn Baldwin  * Determine whether td may deliver the specified signal to p.
1512c83f8015SRobert Watson  * Returns: 0 for permitted, an errno value otherwise
1513f44d9e24SJohn Baldwin  * Locks: Sufficient locks to protect various components of td and p
1514f44d9e24SJohn Baldwin  *        must be held.  td must be curthread, and a lock must be
1515f44d9e24SJohn Baldwin  *        held for p.
1516f44d9e24SJohn Baldwin  * References: td and p must be valid for the lifetime of the call
1517c83f8015SRobert Watson  */
1518c83f8015SRobert Watson int
15191a88a252SMaxim Sobolev p_cansignal(struct thread *td, struct proc *p, int signum)
1520c83f8015SRobert Watson {
1521c83f8015SRobert Watson 
1522f44d9e24SJohn Baldwin 	KASSERT(td == curthread, ("%s: td not curthread", __func__));
1523f44d9e24SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1524f44d9e24SJohn Baldwin 	if (td->td_proc == p)
1525c83f8015SRobert Watson 		return (0);
1526c83f8015SRobert Watson 
1527c83f8015SRobert Watson 	/*
1528c83f8015SRobert Watson 	 * UNIX signalling semantics require that processes in the same
1529c83f8015SRobert Watson 	 * session always be able to deliver SIGCONT to one another,
1530c83f8015SRobert Watson 	 * overriding the remaining protections.
1531c83f8015SRobert Watson 	 */
1532f44d9e24SJohn Baldwin 	/* XXX: This will require an additional lock of some sort. */
1533f44d9e24SJohn Baldwin 	if (signum == SIGCONT && td->td_proc->p_session == p->p_session)
1534c83f8015SRobert Watson 		return (0);
15354b178336SMaxim Sobolev 	/*
1536f9cd63d4SMaxim Sobolev 	 * Some compat layers use SIGTHR and higher signals for
1537f9cd63d4SMaxim Sobolev 	 * communication between different kernel threads of the same
1538f9cd63d4SMaxim Sobolev 	 * process, so that they expect that it's always possible to
1539f9cd63d4SMaxim Sobolev 	 * deliver them, even for suid applications where cr_cansignal() can
15404b178336SMaxim Sobolev 	 * deny such ability for security consideration.  It should be
15414b178336SMaxim Sobolev 	 * pretty safe to do since the only way to create two processes
15424b178336SMaxim Sobolev 	 * with the same p_leader is via rfork(2).
15434b178336SMaxim Sobolev 	 */
15442322a0a7SMaxim Sobolev 	if (td->td_proc->p_leader != NULL && signum >= SIGTHR &&
15452322a0a7SMaxim Sobolev 	    signum < SIGTHR + 4 && td->td_proc->p_leader == p->p_leader)
15464b178336SMaxim Sobolev 		return (0);
1547c83f8015SRobert Watson 
15481a88a252SMaxim Sobolev 	return (cr_cansignal(td->td_ucred, p, signum));
1549c83f8015SRobert Watson }
1550c83f8015SRobert Watson 
1551*1a996ed1SEdward Tomasz Napierala /*-
1552f44d9e24SJohn Baldwin  * Determine whether td may reschedule p.
15537fd6a959SRobert Watson  * Returns: 0 for permitted, an errno value otherwise
1554f44d9e24SJohn Baldwin  * Locks: Sufficient locks to protect various components of td and p
1555f44d9e24SJohn Baldwin  *        must be held.  td must be curthread, and a lock must
1556f44d9e24SJohn Baldwin  *        be held for p.
1557f44d9e24SJohn Baldwin  * References: td and p must be valid for the lifetime of the call
15583b243b72SRobert Watson  */
1559a0f75161SRobert Watson int
1560f44d9e24SJohn Baldwin p_cansched(struct thread *td, struct proc *p)
1561387d2c03SRobert Watson {
156291421ba2SRobert Watson 	int error;
1563387d2c03SRobert Watson 
1564f44d9e24SJohn Baldwin 	KASSERT(td == curthread, ("%s: td not curthread", __func__));
1565f44d9e24SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1566f44d9e24SJohn Baldwin 	if (td->td_proc == p)
1567387d2c03SRobert Watson 		return (0);
1568f44d9e24SJohn Baldwin 	if ((error = prison_check(td->td_ucred, p->p_ucred)))
156991421ba2SRobert Watson 		return (error);
15708a1d977dSRobert Watson #ifdef MAC
157130d239bcSRobert Watson 	if ((error = mac_proc_check_sched(td->td_ucred, p)))
15728a1d977dSRobert Watson 		return (error);
15738a1d977dSRobert Watson #endif
1574f44d9e24SJohn Baldwin 	if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
15751b350b45SRobert Watson 		return (error);
157664d19c2eSRobert Watson 	if ((error = cr_seeothergids(td->td_ucred, p->p_ucred)))
157764d19c2eSRobert Watson 		return (error);
1578800c9408SRobert Watson 	if (td->td_ucred->cr_ruid != p->p_ucred->cr_ruid &&
1579800c9408SRobert Watson 	    td->td_ucred->cr_uid != p->p_ucred->cr_ruid) {
158032f9753cSRobert Watson 		error = priv_check(td, PRIV_SCHED_DIFFCRED);
1581800c9408SRobert Watson 		if (error)
1582800c9408SRobert Watson 			return (error);
1583800c9408SRobert Watson 	}
1584387d2c03SRobert Watson 	return (0);
1585387d2c03SRobert Watson }
1586387d2c03SRobert Watson 
15873b243b72SRobert Watson /*
15885d476e73SRobert Watson  * The 'unprivileged_proc_debug' flag may be used to disable a variety of
15895d476e73SRobert Watson  * unprivileged inter-process debugging services, including some procfs
15905d476e73SRobert Watson  * functionality, ptrace(), and ktrace().  In the past, inter-process
15915d476e73SRobert Watson  * debugging has been involved in a variety of security problems, and sites
15925d476e73SRobert Watson  * not requiring the service might choose to disable it when hardening
15935d476e73SRobert Watson  * systems.
15943b243b72SRobert Watson  *
15953b243b72SRobert Watson  * XXX: Should modifying and reading this variable require locking?
1596eb725b4eSRobert Watson  * XXX: data declarations should be together near the beginning of the file.
15973b243b72SRobert Watson  */
1598e409590dSRobert Watson static int	unprivileged_proc_debug = 1;
1599d0615c64SAndrew R. Reiter SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_proc_debug, CTLFLAG_RW,
1600eb725b4eSRobert Watson     &unprivileged_proc_debug, 0,
16010ef5652eSRobert Watson     "Unprivileged processes may use process debugging facilities");
16020ef5652eSRobert Watson 
1603*1a996ed1SEdward Tomasz Napierala /*-
1604f44d9e24SJohn Baldwin  * Determine whether td may debug p.
16057fd6a959SRobert Watson  * Returns: 0 for permitted, an errno value otherwise
1606f44d9e24SJohn Baldwin  * Locks: Sufficient locks to protect various components of td and p
1607f44d9e24SJohn Baldwin  *        must be held.  td must be curthread, and a lock must
1608f44d9e24SJohn Baldwin  *        be held for p.
1609f44d9e24SJohn Baldwin  * References: td and p must be valid for the lifetime of the call
16103b243b72SRobert Watson  */
1611a0f75161SRobert Watson int
1612f44d9e24SJohn Baldwin p_candebug(struct thread *td, struct proc *p)
1613387d2c03SRobert Watson {
1614eb725b4eSRobert Watson 	int credentialchanged, error, grpsubset, i, uidsubset;
1615387d2c03SRobert Watson 
1616f44d9e24SJohn Baldwin 	KASSERT(td == curthread, ("%s: td not curthread", __func__));
1617f44d9e24SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1618e409590dSRobert Watson 	if (!unprivileged_proc_debug) {
161932f9753cSRobert Watson 		error = priv_check(td, PRIV_DEBUG_UNPRIV);
162032d18604SRobert Watson 		if (error)
162132d18604SRobert Watson 			return (error);
162232d18604SRobert Watson 	}
1623f44d9e24SJohn Baldwin 	if (td->td_proc == p)
162423fad5b6SDag-Erling Smørgrav 		return (0);
1625f44d9e24SJohn Baldwin 	if ((error = prison_check(td->td_ucred, p->p_ucred)))
162691421ba2SRobert Watson 		return (error);
16278a1d977dSRobert Watson #ifdef MAC
162830d239bcSRobert Watson 	if ((error = mac_proc_check_debug(td->td_ucred, p)))
16298a1d977dSRobert Watson 		return (error);
16308a1d977dSRobert Watson #endif
1631f44d9e24SJohn Baldwin 	if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
16321b350b45SRobert Watson 		return (error);
163364d19c2eSRobert Watson 	if ((error = cr_seeothergids(td->td_ucred, p->p_ucred)))
163464d19c2eSRobert Watson 		return (error);
1635387d2c03SRobert Watson 
16367fd6a959SRobert Watson 	/*
1637f44d9e24SJohn Baldwin 	 * Is p's group set a subset of td's effective group set?  This
1638f44d9e24SJohn Baldwin 	 * includes p's egid, group access list, rgid, and svgid.
16397fd6a959SRobert Watson 	 */
1640db42a33dSRobert Watson 	grpsubset = 1;
1641f44d9e24SJohn Baldwin 	for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
1642f44d9e24SJohn Baldwin 		if (!groupmember(p->p_ucred->cr_groups[i], td->td_ucred)) {
1643db42a33dSRobert Watson 			grpsubset = 0;
1644db42a33dSRobert Watson 			break;
1645db42a33dSRobert Watson 		}
1646db42a33dSRobert Watson 	}
1647db42a33dSRobert Watson 	grpsubset = grpsubset &&
1648f44d9e24SJohn Baldwin 	    groupmember(p->p_ucred->cr_rgid, td->td_ucred) &&
1649f44d9e24SJohn Baldwin 	    groupmember(p->p_ucred->cr_svgid, td->td_ucred);
1650db42a33dSRobert Watson 
1651db42a33dSRobert Watson 	/*
1652f44d9e24SJohn Baldwin 	 * Are the uids present in p's credential equal to td's
1653f44d9e24SJohn Baldwin 	 * effective uid?  This includes p's euid, svuid, and ruid.
1654db42a33dSRobert Watson 	 */
1655f44d9e24SJohn Baldwin 	uidsubset = (td->td_ucred->cr_uid == p->p_ucred->cr_uid &&
1656f44d9e24SJohn Baldwin 	    td->td_ucred->cr_uid == p->p_ucred->cr_svuid &&
1657f44d9e24SJohn Baldwin 	    td->td_ucred->cr_uid == p->p_ucred->cr_ruid);
1658db42a33dSRobert Watson 
1659db42a33dSRobert Watson 	/*
1660db42a33dSRobert Watson 	 * Has the credential of the process changed since the last exec()?
1661db42a33dSRobert Watson 	 */
1662f44d9e24SJohn Baldwin 	credentialchanged = (p->p_flag & P_SUGID);
1663db42a33dSRobert Watson 
1664db42a33dSRobert Watson 	/*
1665f44d9e24SJohn Baldwin 	 * If p's gids aren't a subset, or the uids aren't a subset,
1666db42a33dSRobert Watson 	 * or the credential has changed, require appropriate privilege
1667800c9408SRobert Watson 	 * for td to debug p.
1668db42a33dSRobert Watson 	 */
1669800c9408SRobert Watson 	if (!grpsubset || !uidsubset) {
167032f9753cSRobert Watson 		error = priv_check(td, PRIV_DEBUG_DIFFCRED);
1671800c9408SRobert Watson 		if (error)
1672800c9408SRobert Watson 			return (error);
1673800c9408SRobert Watson 	}
1674800c9408SRobert Watson 
1675800c9408SRobert Watson 	if (credentialchanged) {
167632f9753cSRobert Watson 		error = priv_check(td, PRIV_DEBUG_SUGID);
167732d18604SRobert Watson 		if (error)
1678387d2c03SRobert Watson 			return (error);
16797fd6a959SRobert Watson 	}
1680387d2c03SRobert Watson 
1681eb725b4eSRobert Watson 	/* Can't trace init when securelevel > 0. */
1682f44d9e24SJohn Baldwin 	if (p == initproc) {
1683f44d9e24SJohn Baldwin 		error = securelevel_gt(td->td_ucred, 0);
16843ca719f1SRobert Watson 		if (error)
16853ca719f1SRobert Watson 			return (error);
16863ca719f1SRobert Watson 	}
1687387d2c03SRobert Watson 
16885fab7614SRobert Watson 	/*
16895fab7614SRobert Watson 	 * Can't trace a process that's currently exec'ing.
1690800c9408SRobert Watson 	 *
16915fab7614SRobert Watson 	 * XXX: Note, this is not a security policy decision, it's a
16925fab7614SRobert Watson 	 * basic correctness/functionality decision.  Therefore, this check
16935fab7614SRobert Watson 	 * should be moved to the caller's of p_candebug().
16945fab7614SRobert Watson 	 */
1695f44d9e24SJohn Baldwin 	if ((p->p_flag & P_INEXEC) != 0)
1696af80b2c9SKonstantin Belousov 		return (EBUSY);
16979ca45e81SDag-Erling Smørgrav 
1698387d2c03SRobert Watson 	return (0);
1699387d2c03SRobert Watson }
1700387d2c03SRobert Watson 
1701*1a996ed1SEdward Tomasz Napierala /*-
170229dc1288SRobert Watson  * Determine whether the subject represented by cred can "see" a socket.
170329dc1288SRobert Watson  * Returns: 0 for permitted, ENOENT otherwise.
170429dc1288SRobert Watson  */
170529dc1288SRobert Watson int
170629dc1288SRobert Watson cr_canseesocket(struct ucred *cred, struct socket *so)
170729dc1288SRobert Watson {
170829dc1288SRobert Watson 	int error;
170929dc1288SRobert Watson 
171029dc1288SRobert Watson 	error = prison_check(cred, so->so_cred);
171129dc1288SRobert Watson 	if (error)
171229dc1288SRobert Watson 		return (ENOENT);
17138a1d977dSRobert Watson #ifdef MAC
171430d239bcSRobert Watson 	error = mac_socket_check_visible(cred, so);
17158a1d977dSRobert Watson 	if (error)
17168a1d977dSRobert Watson 		return (error);
17178a1d977dSRobert Watson #endif
171829dc1288SRobert Watson 	if (cr_seeotheruids(cred, so->so_cred))
171929dc1288SRobert Watson 		return (ENOENT);
172064d19c2eSRobert Watson 	if (cr_seeothergids(cred, so->so_cred))
172164d19c2eSRobert Watson 		return (ENOENT);
172229dc1288SRobert Watson 
172329dc1288SRobert Watson 	return (0);
172429dc1288SRobert Watson }
172529dc1288SRobert Watson 
1726f08ef6c5SBjoern A. Zeeb #if defined(INET) || defined(INET6)
1727*1a996ed1SEdward Tomasz Napierala /*-
1728f08ef6c5SBjoern A. Zeeb  * Determine whether the subject represented by cred can "see" a socket.
1729f08ef6c5SBjoern A. Zeeb  * Returns: 0 for permitted, ENOENT otherwise.
1730f08ef6c5SBjoern A. Zeeb  */
1731f08ef6c5SBjoern A. Zeeb int
1732f08ef6c5SBjoern A. Zeeb cr_canseeinpcb(struct ucred *cred, struct inpcb *inp)
1733f08ef6c5SBjoern A. Zeeb {
1734f08ef6c5SBjoern A. Zeeb 	int error;
1735f08ef6c5SBjoern A. Zeeb 
1736f08ef6c5SBjoern A. Zeeb 	error = prison_check(cred, inp->inp_cred);
1737f08ef6c5SBjoern A. Zeeb 	if (error)
1738f08ef6c5SBjoern A. Zeeb 		return (ENOENT);
1739f08ef6c5SBjoern A. Zeeb #ifdef MAC
1740f08ef6c5SBjoern A. Zeeb 	INP_LOCK_ASSERT(inp);
1741f08ef6c5SBjoern A. Zeeb 	error = mac_inpcb_check_visible(cred, inp);
1742f08ef6c5SBjoern A. Zeeb 	if (error)
1743f08ef6c5SBjoern A. Zeeb 		return (error);
1744f08ef6c5SBjoern A. Zeeb #endif
1745f08ef6c5SBjoern A. Zeeb 	if (cr_seeotheruids(cred, inp->inp_cred))
1746f08ef6c5SBjoern A. Zeeb 		return (ENOENT);
1747f08ef6c5SBjoern A. Zeeb 	if (cr_seeothergids(cred, inp->inp_cred))
1748f08ef6c5SBjoern A. Zeeb 		return (ENOENT);
1749f08ef6c5SBjoern A. Zeeb 
1750f08ef6c5SBjoern A. Zeeb 	return (0);
1751f08ef6c5SBjoern A. Zeeb }
1752f08ef6c5SBjoern A. Zeeb #endif
1753f08ef6c5SBjoern A. Zeeb 
1754*1a996ed1SEdward Tomasz Napierala /*-
1755babe9a2bSRobert Watson  * Determine whether td can wait for the exit of p.
1756babe9a2bSRobert Watson  * Returns: 0 for permitted, an errno value otherwise
1757babe9a2bSRobert Watson  * Locks: Sufficient locks to protect various components of td and p
1758babe9a2bSRobert Watson  *        must be held.  td must be curthread, and a lock must
1759babe9a2bSRobert Watson  *        be held for p.
1760babe9a2bSRobert Watson  * References: td and p must be valid for the lifetime of the call
1761babe9a2bSRobert Watson 
1762babe9a2bSRobert Watson  */
1763babe9a2bSRobert Watson int
1764babe9a2bSRobert Watson p_canwait(struct thread *td, struct proc *p)
1765babe9a2bSRobert Watson {
1766babe9a2bSRobert Watson 	int error;
1767babe9a2bSRobert Watson 
1768babe9a2bSRobert Watson 	KASSERT(td == curthread, ("%s: td not curthread", __func__));
1769babe9a2bSRobert Watson 	PROC_LOCK_ASSERT(p, MA_OWNED);
17707afcbc18SJamie Gritton 	if ((error = prison_check(td->td_ucred, p->p_ucred)))
1771babe9a2bSRobert Watson 		return (error);
1772babe9a2bSRobert Watson #ifdef MAC
177330d239bcSRobert Watson 	if ((error = mac_proc_check_wait(td->td_ucred, p)))
1774babe9a2bSRobert Watson 		return (error);
1775babe9a2bSRobert Watson #endif
1776babe9a2bSRobert Watson #if 0
1777babe9a2bSRobert Watson 	/* XXXMAC: This could have odd effects on some shells. */
1778babe9a2bSRobert Watson 	if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1779babe9a2bSRobert Watson 		return (error);
1780babe9a2bSRobert Watson #endif
1781babe9a2bSRobert Watson 
1782babe9a2bSRobert Watson 	return (0);
1783babe9a2bSRobert Watson }
1784babe9a2bSRobert Watson 
1785a9e0361bSPoul-Henning Kamp /*
1786df8bae1dSRodney W. Grimes  * Allocate a zeroed cred structure.
1787df8bae1dSRodney W. Grimes  */
1788df8bae1dSRodney W. Grimes struct ucred *
17894c44ad8eSJohn Baldwin crget(void)
1790df8bae1dSRodney W. Grimes {
1791df8bae1dSRodney W. Grimes 	register struct ucred *cr;
1792df8bae1dSRodney W. Grimes 
17931ede983cSDag-Erling Smørgrav 	cr = malloc(sizeof(*cr), M_CRED, M_WAITOK | M_ZERO);
17947e9e371fSJohn Baldwin 	refcount_init(&cr->cr_ref, 1);
1795faef5371SRobert Watson #ifdef AUDIT
1796faef5371SRobert Watson 	audit_cred_init(cr);
1797faef5371SRobert Watson #endif
179840244964SRobert Watson #ifdef MAC
179930d239bcSRobert Watson 	mac_cred_init(cr);
180040244964SRobert Watson #endif
1801838d9858SBrooks Davis 	crextend(cr, XU_NGROUPS);
1802df8bae1dSRodney W. Grimes 	return (cr);
1803df8bae1dSRodney W. Grimes }
1804df8bae1dSRodney W. Grimes 
1805df8bae1dSRodney W. Grimes /*
18067fd6a959SRobert Watson  * Claim another reference to a ucred structure.
18075c3f70d7SAlfred Perlstein  */
1808bd78ceceSJohn Baldwin struct ucred *
18094c44ad8eSJohn Baldwin crhold(struct ucred *cr)
18105c3f70d7SAlfred Perlstein {
18115c3f70d7SAlfred Perlstein 
18127e9e371fSJohn Baldwin 	refcount_acquire(&cr->cr_ref);
1813bd78ceceSJohn Baldwin 	return (cr);
18145c3f70d7SAlfred Perlstein }
18155c3f70d7SAlfred Perlstein 
18165c3f70d7SAlfred Perlstein /*
18170c14ff0eSRobert Watson  * Free a cred structure.  Throws away space when ref count gets to 0.
1818df8bae1dSRodney W. Grimes  */
181926f9a767SRodney W. Grimes void
18204c44ad8eSJohn Baldwin crfree(struct ucred *cr)
1821df8bae1dSRodney W. Grimes {
18221e5d626aSAlfred Perlstein 
1823e04670b7SAlfred Perlstein 	KASSERT(cr->cr_ref > 0, ("bad ucred refcount: %d", cr->cr_ref));
18247e9e371fSJohn Baldwin 	KASSERT(cr->cr_ref != 0xdeadc0de, ("dangling reference to ucred"));
18257e9e371fSJohn Baldwin 	if (refcount_release(&cr->cr_ref)) {
1826f535380cSDon Lewis 		/*
1827f535380cSDon Lewis 		 * Some callers of crget(), such as nfs_statfs(),
1828f535380cSDon Lewis 		 * allocate a temporary credential, but don't
1829f535380cSDon Lewis 		 * allocate a uidinfo structure.
1830f535380cSDon Lewis 		 */
1831f535380cSDon Lewis 		if (cr->cr_uidinfo != NULL)
1832f535380cSDon Lewis 			uifree(cr->cr_uidinfo);
1833823c224eSRobert Watson 		if (cr->cr_ruidinfo != NULL)
1834823c224eSRobert Watson 			uifree(cr->cr_ruidinfo);
183591421ba2SRobert Watson 		/*
183691421ba2SRobert Watson 		 * Free a prison, if any.
183791421ba2SRobert Watson 		 */
18380304c731SJamie Gritton 		if (cr->cr_prison != NULL)
183991421ba2SRobert Watson 			prison_free(cr->cr_prison);
1840faef5371SRobert Watson #ifdef AUDIT
1841faef5371SRobert Watson 		audit_cred_destroy(cr);
1842faef5371SRobert Watson #endif
184340244964SRobert Watson #ifdef MAC
184430d239bcSRobert Watson 		mac_cred_destroy(cr);
184540244964SRobert Watson #endif
1846838d9858SBrooks Davis 		free(cr->cr_groups, M_CRED);
18471ede983cSDag-Erling Smørgrav 		free(cr, M_CRED);
1848e1bca29fSMatthew Dillon 	}
1849df8bae1dSRodney W. Grimes }
1850df8bae1dSRodney W. Grimes 
1851df8bae1dSRodney W. Grimes /*
1852bd78ceceSJohn Baldwin  * Check to see if this ucred is shared.
1853df8bae1dSRodney W. Grimes  */
1854bd78ceceSJohn Baldwin int
18554c44ad8eSJohn Baldwin crshared(struct ucred *cr)
1856df8bae1dSRodney W. Grimes {
1857df8bae1dSRodney W. Grimes 
18587e9e371fSJohn Baldwin 	return (cr->cr_ref > 1);
18591e5d626aSAlfred Perlstein }
1860bd78ceceSJohn Baldwin 
1861bd78ceceSJohn Baldwin /*
1862bd78ceceSJohn Baldwin  * Copy a ucred's contents from a template.  Does not block.
1863bd78ceceSJohn Baldwin  */
1864bd78ceceSJohn Baldwin void
18654c44ad8eSJohn Baldwin crcopy(struct ucred *dest, struct ucred *src)
1866bd78ceceSJohn Baldwin {
1867bd78ceceSJohn Baldwin 
1868bd78ceceSJohn Baldwin 	KASSERT(crshared(dest) == 0, ("crcopy of shared ucred"));
1869bd78ceceSJohn Baldwin 	bcopy(&src->cr_startcopy, &dest->cr_startcopy,
1870bd78ceceSJohn Baldwin 	    (unsigned)((caddr_t)&src->cr_endcopy -
1871bd78ceceSJohn Baldwin 		(caddr_t)&src->cr_startcopy));
1872838d9858SBrooks Davis 	crsetgroups(dest, src->cr_ngroups, src->cr_groups);
1873bd78ceceSJohn Baldwin 	uihold(dest->cr_uidinfo);
1874bd78ceceSJohn Baldwin 	uihold(dest->cr_ruidinfo);
1875bd78ceceSJohn Baldwin 	prison_hold(dest->cr_prison);
1876faef5371SRobert Watson #ifdef AUDIT
1877faef5371SRobert Watson 	audit_cred_copy(src, dest);
1878faef5371SRobert Watson #endif
187940244964SRobert Watson #ifdef MAC
188030d239bcSRobert Watson 	mac_cred_copy(src, dest);
188140244964SRobert Watson #endif
1882df8bae1dSRodney W. Grimes }
1883df8bae1dSRodney W. Grimes 
1884df8bae1dSRodney W. Grimes /*
1885df8bae1dSRodney W. Grimes  * Dup cred struct to a new held one.
1886df8bae1dSRodney W. Grimes  */
1887df8bae1dSRodney W. Grimes struct ucred *
18884c44ad8eSJohn Baldwin crdup(struct ucred *cr)
1889df8bae1dSRodney W. Grimes {
1890df8bae1dSRodney W. Grimes 	struct ucred *newcr;
1891df8bae1dSRodney W. Grimes 
1892bd78ceceSJohn Baldwin 	newcr = crget();
1893bd78ceceSJohn Baldwin 	crcopy(newcr, cr);
1894df8bae1dSRodney W. Grimes 	return (newcr);
1895df8bae1dSRodney W. Grimes }
1896df8bae1dSRodney W. Grimes 
1897df8bae1dSRodney W. Grimes /*
189876183f34SDima Dorfman  * Fill in a struct xucred based on a struct ucred.
189976183f34SDima Dorfman  */
190076183f34SDima Dorfman void
19014c44ad8eSJohn Baldwin cru2x(struct ucred *cr, struct xucred *xcr)
190276183f34SDima Dorfman {
1903838d9858SBrooks Davis 	int ngroups;
190476183f34SDima Dorfman 
190576183f34SDima Dorfman 	bzero(xcr, sizeof(*xcr));
190676183f34SDima Dorfman 	xcr->cr_version = XUCRED_VERSION;
190776183f34SDima Dorfman 	xcr->cr_uid = cr->cr_uid;
1908838d9858SBrooks Davis 
1909838d9858SBrooks Davis 	ngroups = MIN(cr->cr_ngroups, XU_NGROUPS);
1910838d9858SBrooks Davis 	xcr->cr_ngroups = ngroups;
1911838d9858SBrooks Davis 	bcopy(cr->cr_groups, xcr->cr_groups,
1912838d9858SBrooks Davis 	    ngroups * sizeof(*cr->cr_groups));
191376183f34SDima Dorfman }
191476183f34SDima Dorfman 
191576183f34SDima Dorfman /*
19160c14ff0eSRobert Watson  * small routine to swap a thread's current ucred for the correct one taken
19170c14ff0eSRobert Watson  * from the process.
19182eb927e2SJulian Elischer  */
19192eb927e2SJulian Elischer void
19202eb927e2SJulian Elischer cred_update_thread(struct thread *td)
19212eb927e2SJulian Elischer {
19222eb927e2SJulian Elischer 	struct proc *p;
192365e3406dSJohn Baldwin 	struct ucred *cred;
19242eb927e2SJulian Elischer 
19252eb927e2SJulian Elischer 	p = td->td_proc;
192665e3406dSJohn Baldwin 	cred = td->td_ucred;
19272eb927e2SJulian Elischer 	PROC_LOCK(p);
19282eb927e2SJulian Elischer 	td->td_ucred = crhold(p->p_ucred);
19292eb927e2SJulian Elischer 	PROC_UNLOCK(p);
193065e3406dSJohn Baldwin 	if (cred != NULL)
193165e3406dSJohn Baldwin 		crfree(cred);
19322eb927e2SJulian Elischer }
19332eb927e2SJulian Elischer 
1934838d9858SBrooks Davis struct ucred *
1935838d9858SBrooks Davis crcopysafe(struct proc *p, struct ucred *cr)
1936838d9858SBrooks Davis {
1937838d9858SBrooks Davis 	struct ucred *oldcred;
1938838d9858SBrooks Davis 	int groups;
1939838d9858SBrooks Davis 
1940838d9858SBrooks Davis 	PROC_LOCK_ASSERT(p, MA_OWNED);
1941838d9858SBrooks Davis 
1942838d9858SBrooks Davis 	oldcred = p->p_ucred;
1943838d9858SBrooks Davis 	while (cr->cr_agroups < oldcred->cr_agroups) {
1944838d9858SBrooks Davis 		groups = oldcred->cr_agroups;
1945838d9858SBrooks Davis 		PROC_UNLOCK(p);
1946838d9858SBrooks Davis 		crextend(cr, groups);
1947838d9858SBrooks Davis 		PROC_LOCK(p);
1948838d9858SBrooks Davis 		oldcred = p->p_ucred;
1949838d9858SBrooks Davis 	}
1950838d9858SBrooks Davis 	crcopy(cr, oldcred);
1951838d9858SBrooks Davis 
1952838d9858SBrooks Davis 	return (oldcred);
1953838d9858SBrooks Davis }
1954838d9858SBrooks Davis 
1955838d9858SBrooks Davis /*
1956838d9858SBrooks Davis  * Extend the passed in credential to hold n items.
1957838d9858SBrooks Davis  */
1958838d9858SBrooks Davis static void
1959838d9858SBrooks Davis crextend(struct ucred *cr, int n)
1960838d9858SBrooks Davis {
1961838d9858SBrooks Davis 	int cnt;
1962838d9858SBrooks Davis 
1963838d9858SBrooks Davis 	/* Truncate? */
1964838d9858SBrooks Davis 	if (n <= cr->cr_agroups)
1965838d9858SBrooks Davis 		return;
1966838d9858SBrooks Davis 
1967838d9858SBrooks Davis 	/*
1968838d9858SBrooks Davis 	 * We extend by 2 each time since we're using a power of two
1969838d9858SBrooks Davis 	 * allocator until we need enough groups to fill a page.
1970838d9858SBrooks Davis 	 * Once we're allocating multiple pages, only allocate as many
1971838d9858SBrooks Davis 	 * as we actually need.  The case of processes needing a
1972838d9858SBrooks Davis 	 * non-power of two number of pages seems more likely than
1973838d9858SBrooks Davis 	 * a real world process that adds thousands of groups one at a
1974838d9858SBrooks Davis 	 * time.
1975838d9858SBrooks Davis 	 */
1976838d9858SBrooks Davis 	if ( n < PAGE_SIZE / sizeof(gid_t) ) {
1977838d9858SBrooks Davis 		if (cr->cr_agroups == 0)
1978838d9858SBrooks Davis 			cnt = MINALLOCSIZE / sizeof(gid_t);
1979838d9858SBrooks Davis 		else
1980838d9858SBrooks Davis 			cnt = cr->cr_agroups * 2;
1981838d9858SBrooks Davis 
1982838d9858SBrooks Davis 		while (cnt < n)
1983838d9858SBrooks Davis 			cnt *= 2;
1984838d9858SBrooks Davis 	} else
1985838d9858SBrooks Davis 		cnt = roundup2(n, PAGE_SIZE / sizeof(gid_t));
1986838d9858SBrooks Davis 
1987838d9858SBrooks Davis 	/* Free the old array. */
1988838d9858SBrooks Davis 	if (cr->cr_groups)
1989838d9858SBrooks Davis 		free(cr->cr_groups, M_CRED);
1990838d9858SBrooks Davis 
1991838d9858SBrooks Davis 	cr->cr_groups = malloc(cnt * sizeof(gid_t), M_CRED, M_WAITOK | M_ZERO);
1992838d9858SBrooks Davis 	cr->cr_agroups = cnt;
1993838d9858SBrooks Davis }
1994838d9858SBrooks Davis 
1995838d9858SBrooks Davis /*
19967f92e578SBrooks Davis  * Copy groups in to a credential, preserving any necessary invariants.
19977f92e578SBrooks Davis  * Currently this includes the sorting of all supplemental gids.
19987f92e578SBrooks Davis  * crextend() must have been called before hand to ensure sufficient
19997f92e578SBrooks Davis  * space is available.
2000838d9858SBrooks Davis  */
2001838d9858SBrooks Davis static void
2002838d9858SBrooks Davis crsetgroups_locked(struct ucred *cr, int ngrp, gid_t *groups)
2003838d9858SBrooks Davis {
20047f92e578SBrooks Davis 	int i;
20057f92e578SBrooks Davis 	int j;
20067f92e578SBrooks Davis 	gid_t g;
2007838d9858SBrooks Davis 
2008838d9858SBrooks Davis 	KASSERT(cr->cr_agroups >= ngrp, ("cr_ngroups is too small"));
2009838d9858SBrooks Davis 
2010838d9858SBrooks Davis 	bcopy(groups, cr->cr_groups, ngrp * sizeof(gid_t));
2011838d9858SBrooks Davis 	cr->cr_ngroups = ngrp;
20127f92e578SBrooks Davis 
20137f92e578SBrooks Davis 	/*
20147f92e578SBrooks Davis 	 * Sort all groups except cr_groups[0] to allow groupmember to
20157f92e578SBrooks Davis 	 * perform a binary search.
20167f92e578SBrooks Davis 	 *
20177f92e578SBrooks Davis 	 * XXX: If large numbers of groups become common this should
20187f92e578SBrooks Davis 	 * be replaced with shell sort like linux uses or possibly
20197f92e578SBrooks Davis 	 * heap sort.
20207f92e578SBrooks Davis 	 */
20217f92e578SBrooks Davis 	for (i = 2; i < ngrp; i++) {
20227f92e578SBrooks Davis 		g = cr->cr_groups[i];
20237f92e578SBrooks Davis 		for (j = i-1; j >= 1 && g < cr->cr_groups[j]; j--)
20247f92e578SBrooks Davis 			cr->cr_groups[j + 1] = cr->cr_groups[j];
20257f92e578SBrooks Davis 		cr->cr_groups[j + 1] = g;
20267f92e578SBrooks Davis 	}
2027838d9858SBrooks Davis }
2028838d9858SBrooks Davis 
2029838d9858SBrooks Davis /*
2030838d9858SBrooks Davis  * Copy groups in to a credential after expanding it if required.
2031412f9500SBrooks Davis  * Truncate the list to (ngroups_max + 1) if it is too large.
2032838d9858SBrooks Davis  */
2033838d9858SBrooks Davis void
2034838d9858SBrooks Davis crsetgroups(struct ucred *cr, int ngrp, gid_t *groups)
2035838d9858SBrooks Davis {
2036838d9858SBrooks Davis 
2037412f9500SBrooks Davis 	if (ngrp > ngroups_max + 1)
2038412f9500SBrooks Davis 		ngrp = ngroups_max + 1;
2039838d9858SBrooks Davis 
2040838d9858SBrooks Davis 	crextend(cr, ngrp);
2041838d9858SBrooks Davis 	crsetgroups_locked(cr, ngrp, groups);
2042838d9858SBrooks Davis }
2043838d9858SBrooks Davis 
20442eb927e2SJulian Elischer /*
2045df8bae1dSRodney W. Grimes  * Get login name, if available.
2046df8bae1dSRodney W. Grimes  */
2047d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
2048df8bae1dSRodney W. Grimes struct getlogin_args {
2049df8bae1dSRodney W. Grimes 	char	*namebuf;
2050df8bae1dSRodney W. Grimes 	u_int	namelen;
2051df8bae1dSRodney W. Grimes };
2052d2d3e875SBruce Evans #endif
2053df8bae1dSRodney W. Grimes /* ARGSUSED */
205426f9a767SRodney W. Grimes int
20554c44ad8eSJohn Baldwin getlogin(struct thread *td, struct getlogin_args *uap)
2056df8bae1dSRodney W. Grimes {
2057835a82eeSMatthew Dillon 	int error;
2058f591779bSSeigo Tanimura 	char login[MAXLOGNAME];
2059b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2060df8bae1dSRodney W. Grimes 
206130cf3ac4SAndrey A. Chernov 	if (uap->namelen > MAXLOGNAME)
206253490b76SAndrey A. Chernov 		uap->namelen = MAXLOGNAME;
2063f591779bSSeigo Tanimura 	PROC_LOCK(p);
2064f591779bSSeigo Tanimura 	SESS_LOCK(p->p_session);
2065f591779bSSeigo Tanimura 	bcopy(p->p_session->s_login, login, uap->namelen);
2066f591779bSSeigo Tanimura 	SESS_UNLOCK(p->p_session);
2067f591779bSSeigo Tanimura 	PROC_UNLOCK(p);
20687f05b035SAlfred Perlstein 	error = copyout(login, uap->namebuf, uap->namelen);
2069835a82eeSMatthew Dillon 	return(error);
2070df8bae1dSRodney W. Grimes }
2071df8bae1dSRodney W. Grimes 
2072df8bae1dSRodney W. Grimes /*
2073df8bae1dSRodney W. Grimes  * Set login name.
2074df8bae1dSRodney W. Grimes  */
2075d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
2076df8bae1dSRodney W. Grimes struct setlogin_args {
2077df8bae1dSRodney W. Grimes 	char	*namebuf;
2078df8bae1dSRodney W. Grimes };
2079d2d3e875SBruce Evans #endif
2080df8bae1dSRodney W. Grimes /* ARGSUSED */
208126f9a767SRodney W. Grimes int
20824c44ad8eSJohn Baldwin setlogin(struct thread *td, struct setlogin_args *uap)
2083df8bae1dSRodney W. Grimes {
2084b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
2085df8bae1dSRodney W. Grimes 	int error;
2086964ca0caSAndrey A. Chernov 	char logintmp[MAXLOGNAME];
2087df8bae1dSRodney W. Grimes 
208832f9753cSRobert Watson 	error = priv_check(td, PRIV_PROC_SETLOGIN);
208907f3485dSJohn Baldwin 	if (error)
209007f3485dSJohn Baldwin 		return (error);
20917f05b035SAlfred Perlstein 	error = copyinstr(uap->namebuf, logintmp, sizeof(logintmp), NULL);
2092eb725b4eSRobert Watson 	if (error == ENAMETOOLONG)
2093df8bae1dSRodney W. Grimes 		error = EINVAL;
2094f591779bSSeigo Tanimura 	else if (!error) {
2095f591779bSSeigo Tanimura 		PROC_LOCK(p);
2096f591779bSSeigo Tanimura 		SESS_LOCK(p->p_session);
2097f591779bSSeigo Tanimura 		(void) memcpy(p->p_session->s_login, logintmp,
2098964ca0caSAndrey A. Chernov 		    sizeof(logintmp));
2099f591779bSSeigo Tanimura 		SESS_UNLOCK(p->p_session);
2100f591779bSSeigo Tanimura 		PROC_UNLOCK(p);
2101f591779bSSeigo Tanimura 	}
2102df8bae1dSRodney W. Grimes 	return (error);
2103df8bae1dSRodney W. Grimes }
2104d5f81602SSean Eric Fagan 
2105d5f81602SSean Eric Fagan void
21064c44ad8eSJohn Baldwin setsugid(struct proc *p)
2107d5f81602SSean Eric Fagan {
2108f2102dadSAlfred Perlstein 
2109f2102dadSAlfred Perlstein 	PROC_LOCK_ASSERT(p, MA_OWNED);
2110d5f81602SSean Eric Fagan 	p->p_flag |= P_SUGID;
211189361835SSean Eric Fagan 	if (!(p->p_pfsflags & PF_ISUGID))
2112d5f81602SSean Eric Fagan 		p->p_stops = 0;
2113d5f81602SSean Eric Fagan }
2114f535380cSDon Lewis 
2115*1a996ed1SEdward Tomasz Napierala /*-
21167fd6a959SRobert Watson  * Change a process's effective uid.
2117b1fc0ec1SRobert Watson  * Side effects: newcred->cr_uid and newcred->cr_uidinfo will be modified.
2118b1fc0ec1SRobert Watson  * References: newcred must be an exclusive credential reference for the
2119b1fc0ec1SRobert Watson  *             duration of the call.
2120f535380cSDon Lewis  */
2121f535380cSDon Lewis void
21221419eacbSAlfred Perlstein change_euid(struct ucred *newcred, struct uidinfo *euip)
2123f535380cSDon Lewis {
2124f535380cSDon Lewis 
21251419eacbSAlfred Perlstein 	newcred->cr_uid = euip->ui_uid;
21261419eacbSAlfred Perlstein 	uihold(euip);
2127b1fc0ec1SRobert Watson 	uifree(newcred->cr_uidinfo);
21281419eacbSAlfred Perlstein 	newcred->cr_uidinfo = euip;
2129f535380cSDon Lewis }
2130f535380cSDon Lewis 
2131*1a996ed1SEdward Tomasz Napierala /*-
21327fd6a959SRobert Watson  * Change a process's effective gid.
2133b1fc0ec1SRobert Watson  * Side effects: newcred->cr_gid will be modified.
2134b1fc0ec1SRobert Watson  * References: newcred must be an exclusive credential reference for the
2135b1fc0ec1SRobert Watson  *             duration of the call.
2136f535380cSDon Lewis  */
2137810bfc8eSAndrew Gallatin void
21384c44ad8eSJohn Baldwin change_egid(struct ucred *newcred, gid_t egid)
2139b1fc0ec1SRobert Watson {
2140b1fc0ec1SRobert Watson 
2141b1fc0ec1SRobert Watson 	newcred->cr_groups[0] = egid;
2142b1fc0ec1SRobert Watson }
2143b1fc0ec1SRobert Watson 
2144*1a996ed1SEdward Tomasz Napierala /*-
21457fd6a959SRobert Watson  * Change a process's real uid.
2146b1fc0ec1SRobert Watson  * Side effects: newcred->cr_ruid will be updated, newcred->cr_ruidinfo
2147b1fc0ec1SRobert Watson  *               will be updated, and the old and new cr_ruidinfo proc
2148b1fc0ec1SRobert Watson  *               counts will be updated.
2149b1fc0ec1SRobert Watson  * References: newcred must be an exclusive credential reference for the
2150b1fc0ec1SRobert Watson  *             duration of the call.
2151b1fc0ec1SRobert Watson  */
2152b1fc0ec1SRobert Watson void
21531419eacbSAlfred Perlstein change_ruid(struct ucred *newcred, struct uidinfo *ruip)
2154f535380cSDon Lewis {
2155f535380cSDon Lewis 
2156b1fc0ec1SRobert Watson 	(void)chgproccnt(newcred->cr_ruidinfo, -1, 0);
21571419eacbSAlfred Perlstein 	newcred->cr_ruid = ruip->ui_uid;
21581419eacbSAlfred Perlstein 	uihold(ruip);
2159b1fc0ec1SRobert Watson 	uifree(newcred->cr_ruidinfo);
21601419eacbSAlfred Perlstein 	newcred->cr_ruidinfo = ruip;
2161b1fc0ec1SRobert Watson 	(void)chgproccnt(newcred->cr_ruidinfo, 1, 0);
2162b1fc0ec1SRobert Watson }
2163b1fc0ec1SRobert Watson 
2164*1a996ed1SEdward Tomasz Napierala /*-
21657fd6a959SRobert Watson  * Change a process's real gid.
2166b1fc0ec1SRobert Watson  * Side effects: newcred->cr_rgid will be updated.
2167b1fc0ec1SRobert Watson  * References: newcred must be an exclusive credential reference for the
2168b1fc0ec1SRobert Watson  *             duration of the call.
2169b1fc0ec1SRobert Watson  */
2170b1fc0ec1SRobert Watson void
21714c44ad8eSJohn Baldwin change_rgid(struct ucred *newcred, gid_t rgid)
2172b1fc0ec1SRobert Watson {
2173b1fc0ec1SRobert Watson 
2174b1fc0ec1SRobert Watson 	newcred->cr_rgid = rgid;
2175b1fc0ec1SRobert Watson }
2176b1fc0ec1SRobert Watson 
2177*1a996ed1SEdward Tomasz Napierala /*-
21787fd6a959SRobert Watson  * Change a process's saved uid.
2179b1fc0ec1SRobert Watson  * Side effects: newcred->cr_svuid will be updated.
2180b1fc0ec1SRobert Watson  * References: newcred must be an exclusive credential reference for the
2181b1fc0ec1SRobert Watson  *             duration of the call.
2182b1fc0ec1SRobert Watson  */
2183b1fc0ec1SRobert Watson void
21844c44ad8eSJohn Baldwin change_svuid(struct ucred *newcred, uid_t svuid)
2185b1fc0ec1SRobert Watson {
2186b1fc0ec1SRobert Watson 
2187b1fc0ec1SRobert Watson 	newcred->cr_svuid = svuid;
2188b1fc0ec1SRobert Watson }
2189b1fc0ec1SRobert Watson 
2190*1a996ed1SEdward Tomasz Napierala /*-
21917fd6a959SRobert Watson  * Change a process's saved gid.
2192b1fc0ec1SRobert Watson  * Side effects: newcred->cr_svgid will be updated.
2193b1fc0ec1SRobert Watson  * References: newcred must be an exclusive credential reference for the
2194b1fc0ec1SRobert Watson  *             duration of the call.
2195b1fc0ec1SRobert Watson  */
2196b1fc0ec1SRobert Watson void
21974c44ad8eSJohn Baldwin change_svgid(struct ucred *newcred, gid_t svgid)
2198b1fc0ec1SRobert Watson {
2199b1fc0ec1SRobert Watson 
2200b1fc0ec1SRobert Watson 	newcred->cr_svgid = svgid;
2201f535380cSDon Lewis }
2202