xref: /freebsd/sys/kern/kern_prot.c (revision 99e8005137088aafb1350e23b113d69b01b0820f)
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1990, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)kern_prot.c	8.6 (Berkeley) 1/21/94
39  * $FreeBSD$
40  */
41 
42 /*
43  * System calls related to processes and protection
44  */
45 
46 #include "opt_compat.h"
47 #include "opt_global.h"
48 
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/acct.h>
52 #include <sys/kernel.h>
53 #include <sys/lock.h>
54 #include <sys/mutex.h>
55 #include <sys/proc.h>
56 #include <sys/sysproto.h>
57 #include <sys/malloc.h>
58 #include <sys/pioctl.h>
59 #include <sys/resourcevar.h>
60 #include <sys/sysctl.h>
61 #include <sys/jail.h>
62 
63 static MALLOC_DEFINE(M_CRED, "cred", "credentials");
64 
65 #ifndef _SYS_SYSPROTO_H_
66 struct getpid_args {
67 	int	dummy;
68 };
69 #endif
70 
71 /*
72  * getpid - MP SAFE
73  */
74 
75 /* ARGSUSED */
76 int
77 getpid(p, uap)
78 	struct proc *p;
79 	struct getpid_args *uap;
80 {
81 
82 	p->p_retval[0] = p->p_pid;
83 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
84 	PROC_LOCK(p);
85 	p->p_retval[1] = p->p_pptr->p_pid;
86 	PROC_UNLOCK(p);
87 #endif
88 	return (0);
89 }
90 
91 /*
92  * getppid - MP SAFE
93  */
94 
95 #ifndef _SYS_SYSPROTO_H_
96 struct getppid_args {
97         int     dummy;
98 };
99 #endif
100 /* ARGSUSED */
101 int
102 getppid(p, uap)
103 	struct proc *p;
104 	struct getppid_args *uap;
105 {
106 
107 	PROC_LOCK(p);
108 	p->p_retval[0] = p->p_pptr->p_pid;
109 	PROC_UNLOCK(p);
110 	return (0);
111 }
112 
113 /*
114  * Get process group ID; note that POSIX getpgrp takes no parameter
115  *
116  * MP SAFE
117  */
118 #ifndef _SYS_SYSPROTO_H_
119 struct getpgrp_args {
120         int     dummy;
121 };
122 #endif
123 
124 int
125 getpgrp(p, uap)
126 	struct proc *p;
127 	struct getpgrp_args *uap;
128 {
129 
130 	p->p_retval[0] = p->p_pgrp->pg_id;
131 	return (0);
132 }
133 
134 /* Get an arbitary pid's process group id */
135 #ifndef _SYS_SYSPROTO_H_
136 struct getpgid_args {
137 	pid_t	pid;
138 };
139 #endif
140 
141 int
142 getpgid(p, uap)
143 	struct proc *p;
144 	struct getpgid_args *uap;
145 {
146 	struct proc *pt;
147 	int error;
148 
149 	if (uap->pid == 0)
150 		p->p_retval[0] = p->p_pgrp->pg_id;
151 	else {
152 		if ((pt = pfind(uap->pid)) == NULL)
153 			return ESRCH;
154 		if ((error = p_can(p, pt, P_CAN_SEE, NULL))) {
155 			PROC_UNLOCK(pt);
156 			return (error);
157 		}
158 		p->p_retval[0] = pt->p_pgrp->pg_id;
159 		PROC_UNLOCK(pt);
160 	}
161 	return 0;
162 }
163 
164 /*
165  * Get an arbitary pid's session id.
166  */
167 #ifndef _SYS_SYSPROTO_H_
168 struct getsid_args {
169 	pid_t	pid;
170 };
171 #endif
172 
173 int
174 getsid(p, uap)
175 	struct proc *p;
176 	struct getsid_args *uap;
177 {
178 	struct proc *pt;
179 	int error;
180 
181 	if (uap->pid == 0)
182 		p->p_retval[0] = p->p_session->s_sid;
183 	else {
184 		if ((pt = pfind(uap->pid)) == NULL)
185 			return ESRCH;
186 		if ((error = p_can(p, pt, P_CAN_SEE, NULL))) {
187 			PROC_UNLOCK(pt);
188 			return (error);
189 		}
190 		p->p_retval[0] = pt->p_session->s_sid;
191 		PROC_UNLOCK(pt);
192 	}
193 	return 0;
194 }
195 
196 
197 /*
198  * getuid() - MP SAFE
199  */
200 #ifndef _SYS_SYSPROTO_H_
201 struct getuid_args {
202         int     dummy;
203 };
204 #endif
205 
206 /* ARGSUSED */
207 int
208 getuid(p, uap)
209 	struct proc *p;
210 	struct getuid_args *uap;
211 {
212 
213 	p->p_retval[0] = p->p_ucred->cr_ruid;
214 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
215 	p->p_retval[1] = p->p_ucred->cr_uid;
216 #endif
217 	return (0);
218 }
219 
220 /*
221  * geteuid() - MP SAFE
222  */
223 #ifndef _SYS_SYSPROTO_H_
224 struct geteuid_args {
225         int     dummy;
226 };
227 #endif
228 
229 /* ARGSUSED */
230 int
231 geteuid(p, uap)
232 	struct proc *p;
233 	struct geteuid_args *uap;
234 {
235 
236 	p->p_retval[0] = p->p_ucred->cr_uid;
237 	return (0);
238 }
239 
240 /*
241  * getgid() - MP SAFE
242  */
243 #ifndef _SYS_SYSPROTO_H_
244 struct getgid_args {
245         int     dummy;
246 };
247 #endif
248 
249 /* ARGSUSED */
250 int
251 getgid(p, uap)
252 	struct proc *p;
253 	struct getgid_args *uap;
254 {
255 
256 	p->p_retval[0] = p->p_ucred->cr_rgid;
257 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
258 	p->p_retval[1] = p->p_ucred->cr_groups[0];
259 #endif
260 	return (0);
261 }
262 
263 /*
264  * Get effective group ID.  The "egid" is groups[0], and could be obtained
265  * via getgroups.  This syscall exists because it is somewhat painful to do
266  * correctly in a library function.
267  */
268 #ifndef _SYS_SYSPROTO_H_
269 struct getegid_args {
270         int     dummy;
271 };
272 #endif
273 
274 /* ARGSUSED */
275 int
276 getegid(p, uap)
277 	struct proc *p;
278 	struct getegid_args *uap;
279 {
280 
281 	p->p_retval[0] = p->p_ucred->cr_groups[0];
282 	return (0);
283 }
284 
285 #ifndef _SYS_SYSPROTO_H_
286 struct getgroups_args {
287 	u_int	gidsetsize;
288 	gid_t	*gidset;
289 };
290 #endif
291 int
292 getgroups(p, uap)
293 	struct proc *p;
294 	register struct	getgroups_args *uap;
295 {
296 	struct ucred *cred = p->p_ucred;
297 	u_int ngrp;
298 	int error;
299 
300 	if ((ngrp = uap->gidsetsize) == 0) {
301 		p->p_retval[0] = cred->cr_ngroups;
302 		return (0);
303 	}
304 	if (ngrp < cred->cr_ngroups)
305 		return (EINVAL);
306 	ngrp = cred->cr_ngroups;
307 	if ((error = copyout((caddr_t)cred->cr_groups,
308 	    (caddr_t)uap->gidset, ngrp * sizeof(gid_t))))
309 		return (error);
310 	p->p_retval[0] = ngrp;
311 	return (0);
312 }
313 
314 #ifndef _SYS_SYSPROTO_H_
315 struct setsid_args {
316         int     dummy;
317 };
318 #endif
319 
320 /* ARGSUSED */
321 int
322 setsid(p, uap)
323 	register struct proc *p;
324 	struct setsid_args *uap;
325 {
326 
327 	if (p->p_pgid == p->p_pid || pgfind(p->p_pid)) {
328 		return (EPERM);
329 	} else {
330 		(void)enterpgrp(p, p->p_pid, 1);
331 		p->p_retval[0] = p->p_pid;
332 		return (0);
333 	}
334 }
335 
336 /*
337  * set process group (setpgid/old setpgrp)
338  *
339  * caller does setpgid(targpid, targpgid)
340  *
341  * pid must be caller or child of caller (ESRCH)
342  * if a child
343  *	pid must be in same session (EPERM)
344  *	pid can't have done an exec (EACCES)
345  * if pgid != pid
346  * 	there must exist some pid in same session having pgid (EPERM)
347  * pid must not be session leader (EPERM)
348  */
349 #ifndef _SYS_SYSPROTO_H_
350 struct setpgid_args {
351 	int	pid;	/* target process id */
352 	int	pgid;	/* target pgrp id */
353 };
354 #endif
355 /* ARGSUSED */
356 int
357 setpgid(curp, uap)
358 	struct proc *curp;
359 	register struct setpgid_args *uap;
360 {
361 	register struct proc *targp;		/* target process */
362 	register struct pgrp *pgrp;		/* target pgrp */
363 	int error;
364 
365 	if (uap->pgid < 0)
366 		return (EINVAL);
367 	if (uap->pid != 0 && uap->pid != curp->p_pid) {
368 		if ((targp = pfind(uap->pid)) == NULL || !inferior(targp)) {
369 			if (targp)
370 				PROC_UNLOCK(targp);
371 			return (ESRCH);
372 		}
373 		if ((error = p_can(curproc, targp, P_CAN_SEE, NULL))) {
374 			PROC_UNLOCK(targp);
375 			return (error);
376 		}
377 		if (targp->p_pgrp == NULL ||
378 		    targp->p_session != curp->p_session) {
379 			PROC_UNLOCK(targp);
380 			return (EPERM);
381 		}
382 		if (targp->p_flag & P_EXEC) {
383 			PROC_UNLOCK(targp);
384 			return (EACCES);
385 		}
386 	} else {
387 		targp = curp;
388 		PROC_LOCK(curp);	/* XXX: not needed */
389 	}
390 	if (SESS_LEADER(targp)) {
391 		PROC_UNLOCK(targp);
392 		return (EPERM);
393 	}
394 	if (uap->pgid == 0)
395 		uap->pgid = targp->p_pid;
396 	else if (uap->pgid != targp->p_pid)
397 		if ((pgrp = pgfind(uap->pgid)) == 0 ||
398 	            pgrp->pg_session != curp->p_session) {
399 			PROC_UNLOCK(targp);
400 			return (EPERM);
401 		}
402 	/* XXX: We should probably hold the lock across enterpgrp. */
403 	PROC_UNLOCK(targp);
404 	return (enterpgrp(targp, uap->pgid, 0));
405 }
406 
407 /*
408  * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
409  * compatible.  It says that setting the uid/gid to euid/egid is a special
410  * case of "appropriate privilege".  Once the rules are expanded out, this
411  * basically means that setuid(nnn) sets all three id's, in all permitted
412  * cases unless _POSIX_SAVED_IDS is enabled.  In that case, setuid(getuid())
413  * does not set the saved id - this is dangerous for traditional BSD
414  * programs.  For this reason, we *really* do not want to set
415  * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
416  */
417 #define POSIX_APPENDIX_B_4_2_2
418 
419 #ifndef _SYS_SYSPROTO_H_
420 struct setuid_args {
421 	uid_t	uid;
422 };
423 #endif
424 /* ARGSUSED */
425 int
426 setuid(p, uap)
427 	struct proc *p;
428 	struct setuid_args *uap;
429 {
430 	struct ucred *newcred, *oldcred;
431 	uid_t uid;
432 	int error;
433 
434 	uid = uap->uid;
435 	oldcred = p->p_ucred;
436 	/*
437 	 * See if we have "permission" by POSIX 1003.1 rules.
438 	 *
439 	 * Note that setuid(geteuid()) is a special case of
440 	 * "appropriate privileges" in appendix B.4.2.2.  We need
441 	 * to use this clause to be compatible with traditional BSD
442 	 * semantics.  Basically, it means that "setuid(xx)" sets all
443 	 * three id's (assuming you have privs).
444 	 *
445 	 * Notes on the logic.  We do things in three steps.
446 	 * 1: We determine if the euid is going to change, and do EPERM
447 	 *    right away.  We unconditionally change the euid later if this
448 	 *    test is satisfied, simplifying that part of the logic.
449 	 * 2: We determine if the real and/or saved uid's are going to
450 	 *    change.  Determined by compile options.
451 	 * 3: Change euid last. (after tests in #2 for "appropriate privs")
452 	 */
453 	if (uid != oldcred->cr_ruid &&		/* allow setuid(getuid()) */
454 #ifdef _POSIX_SAVED_IDS
455 	    uid != oldcred->cr_svuid &&		/* allow setuid(saved gid) */
456 #endif
457 #ifdef POSIX_APPENDIX_B_4_2_2	/* Use BSD-compat clause from B.4.2.2 */
458 	    uid != oldcred->cr_uid &&		/* allow setuid(geteuid()) */
459 #endif
460 	    (error = suser_xxx(oldcred, NULL, PRISON_ROOT)))
461 		return (error);
462 
463 	newcred = crdup(oldcred);
464 #ifdef _POSIX_SAVED_IDS
465 	/*
466 	 * Do we have "appropriate privileges" (are we root or uid == euid)
467 	 * If so, we are changing the real uid and/or saved uid.
468 	 */
469 	if (
470 #ifdef POSIX_APPENDIX_B_4_2_2	/* Use the clause from B.4.2.2 */
471 	    uid == oldcred->cr_uid ||
472 #endif
473 	    suser_xxx(oldcred, NULL, PRISON_ROOT) == 0) /* we are using privs */
474 #endif
475 	{
476 		/*
477 		 * Set the real uid and transfer proc count to new user.
478 		 */
479 		if (uid != oldcred->cr_ruid) {
480 			change_ruid(newcred, uid);
481 			setsugid(p);
482 		}
483 		/*
484 		 * Set saved uid
485 		 *
486 		 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
487 		 * the security of seteuid() depends on it.  B.4.2.2 says it
488 		 * is important that we should do this.
489 		 */
490 		if (uid != oldcred->cr_svuid) {
491 			change_svuid(newcred, uid);
492 			setsugid(p);
493 		}
494 	}
495 
496 	/*
497 	 * In all permitted cases, we are changing the euid.
498 	 * Copy credentials so other references do not see our changes.
499 	 */
500 	if (uid != oldcred->cr_uid) {
501 		change_euid(newcred, uid);
502 		setsugid(p);
503 	}
504 	p->p_ucred = newcred;
505 	crfree(oldcred);
506 	return (0);
507 }
508 
509 #ifndef _SYS_SYSPROTO_H_
510 struct seteuid_args {
511 	uid_t	euid;
512 };
513 #endif
514 /* ARGSUSED */
515 int
516 seteuid(p, uap)
517 	struct proc *p;
518 	struct seteuid_args *uap;
519 {
520 	struct ucred *newcred, *oldcred;
521 	uid_t euid;
522 	int error;
523 
524 	euid = uap->euid;
525 	oldcred = p->p_ucred;
526 	if (euid != oldcred->cr_ruid &&		/* allow seteuid(getuid()) */
527 	    euid != oldcred->cr_svuid &&	/* allow seteuid(saved uid) */
528 	    (error = suser_xxx(oldcred, NULL, PRISON_ROOT)))
529 		return (error);
530 	/*
531 	 * Everything's okay, do it.  Copy credentials so other references do
532 	 * not see our changes.
533 	 */
534 	newcred = crdup(oldcred);
535 	if (oldcred->cr_uid != euid) {
536 		change_euid(newcred, euid);
537 		setsugid(p);
538 	}
539 	p->p_ucred = newcred;
540 	crfree(oldcred);
541 	return (0);
542 }
543 
544 #ifndef _SYS_SYSPROTO_H_
545 struct setgid_args {
546 	gid_t	gid;
547 };
548 #endif
549 /* ARGSUSED */
550 int
551 setgid(p, uap)
552 	struct proc *p;
553 	struct setgid_args *uap;
554 {
555 	struct ucred *newcred, *oldcred;
556 	gid_t gid;
557 	int error;
558 
559 	gid = uap->gid;
560 	oldcred = p->p_ucred;
561 	/*
562 	 * See if we have "permission" by POSIX 1003.1 rules.
563 	 *
564 	 * Note that setgid(getegid()) is a special case of
565 	 * "appropriate privileges" in appendix B.4.2.2.  We need
566 	 * to use this clause to be compatible with traditional BSD
567 	 * semantics.  Basically, it means that "setgid(xx)" sets all
568 	 * three id's (assuming you have privs).
569 	 *
570 	 * For notes on the logic here, see setuid() above.
571 	 */
572 	if (gid != oldcred->cr_rgid &&		/* allow setgid(getgid()) */
573 #ifdef _POSIX_SAVED_IDS
574 	    gid != oldcred->cr_svgid &&		/* allow setgid(saved gid) */
575 #endif
576 #ifdef POSIX_APPENDIX_B_4_2_2	/* Use BSD-compat clause from B.4.2.2 */
577 	    gid != oldcred->cr_groups[0] && /* allow setgid(getegid()) */
578 #endif
579 	    (error = suser_xxx(oldcred, NULL, PRISON_ROOT)))
580 		return (error);
581 
582 	newcred = crdup(oldcred);
583 #ifdef _POSIX_SAVED_IDS
584 	/*
585 	 * Do we have "appropriate privileges" (are we root or gid == egid)
586 	 * If so, we are changing the real uid and saved gid.
587 	 */
588 	if (
589 #ifdef POSIX_APPENDIX_B_4_2_2	/* use the clause from B.4.2.2 */
590 	    gid == oldcred->cr_groups[0] ||
591 #endif
592 	    suser_xxx(oldcred, NULL, PRISON_ROOT) == 0) /* we are using privs */
593 #endif
594 	{
595 		/*
596 		 * Set real gid
597 		 */
598 		if (oldcred->cr_rgid != gid) {
599 			change_rgid(newcred, gid);
600 			setsugid(p);
601 		}
602 		/*
603 		 * Set saved gid
604 		 *
605 		 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
606 		 * the security of setegid() depends on it.  B.4.2.2 says it
607 		 * is important that we should do this.
608 		 */
609 		if (oldcred->cr_svgid != gid) {
610 			change_svgid(newcred, gid);
611 			setsugid(p);
612 		}
613 	}
614 	/*
615 	 * In all cases permitted cases, we are changing the egid.
616 	 * Copy credentials so other references do not see our changes.
617 	 */
618 	if (oldcred->cr_groups[0] != gid) {
619 		change_egid(newcred, gid);
620 		setsugid(p);
621 	}
622 	p->p_ucred = newcred;
623 	crfree(oldcred);
624 	return (0);
625 }
626 
627 #ifndef _SYS_SYSPROTO_H_
628 struct setegid_args {
629 	gid_t	egid;
630 };
631 #endif
632 /* ARGSUSED */
633 int
634 setegid(p, uap)
635 	struct proc *p;
636 	struct setegid_args *uap;
637 {
638 	struct ucred *newcred, *oldcred;
639 	gid_t egid;
640 	int error;
641 
642 	egid = uap->egid;
643 	oldcred = p->p_ucred;
644 	if (egid != oldcred->cr_rgid &&		/* allow setegid(getgid()) */
645 	    egid != oldcred->cr_svgid &&	/* allow setegid(saved gid) */
646 	    (error = suser_xxx(oldcred, NULL, PRISON_ROOT)))
647 		return (error);
648 	newcred = crdup(oldcred);
649 	if (oldcred->cr_groups[0] != egid) {
650 		change_egid(newcred, egid);
651 		setsugid(p);
652 	}
653 	p->p_ucred = newcred;
654 	crfree(oldcred);
655 	return (0);
656 }
657 
658 #ifndef _SYS_SYSPROTO_H_
659 struct setgroups_args {
660 	u_int	gidsetsize;
661 	gid_t	*gidset;
662 };
663 #endif
664 /* ARGSUSED */
665 int
666 setgroups(p, uap)
667 	struct proc *p;
668 	struct setgroups_args *uap;
669 {
670 	struct ucred *newcred, *oldcred;
671 	u_int ngrp;
672 	int error;
673 
674 	ngrp = uap->gidsetsize;
675 	oldcred = p->p_ucred;
676 	if ((error = suser_xxx(oldcred, NULL, PRISON_ROOT)))
677 		return (error);
678 	if (ngrp > NGROUPS)
679 		return (EINVAL);
680 	/*
681 	 * XXX A little bit lazy here.  We could test if anything has
682 	 * changed before crcopy() and setting P_SUGID.
683 	 */
684 	newcred = crdup(oldcred);
685 	if (ngrp < 1) {
686 		/*
687 		 * setgroups(0, NULL) is a legitimate way of clearing the
688 		 * groups vector on non-BSD systems (which generally do not
689 		 * have the egid in the groups[0]).  We risk security holes
690 		 * when running non-BSD software if we do not do the same.
691 		 */
692 		newcred->cr_ngroups = 1;
693 	} else {
694 		if ((error = copyin((caddr_t)uap->gidset,
695 		    (caddr_t)newcred->cr_groups, ngrp * sizeof(gid_t)))) {
696 			crfree(newcred);
697 			return (error);
698 		}
699 		newcred->cr_ngroups = ngrp;
700 	}
701 	setsugid(p);
702 	p->p_ucred = newcred;
703 	crfree(oldcred);
704 	return (0);
705 }
706 
707 #ifndef _SYS_SYSPROTO_H_
708 struct setreuid_args {
709 	uid_t	ruid;
710 	uid_t	euid;
711 };
712 #endif
713 /* ARGSUSED */
714 int
715 setreuid(p, uap)
716 	register struct proc *p;
717 	struct setreuid_args *uap;
718 {
719 	struct ucred *newcred, *oldcred;
720 	uid_t ruid, euid;
721 	int error;
722 
723 	ruid = uap->ruid;
724 	euid = uap->euid;
725 	oldcred = p->p_ucred;
726 	if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
727 	      ruid != oldcred->cr_svuid) ||
728 	     (euid != (uid_t)-1 && euid != oldcred->cr_uid &&
729 	      euid != oldcred->cr_ruid && euid != oldcred->cr_svuid)) &&
730 	    (error = suser_xxx(oldcred, NULL, PRISON_ROOT)) != 0)
731 		return (error);
732 	newcred = crdup(oldcred);
733 	if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
734 		change_euid(newcred, euid);
735 		setsugid(p);
736 	}
737 	if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
738 		change_ruid(newcred, ruid);
739 		setsugid(p);
740 	}
741 	if ((ruid != (uid_t)-1 || newcred->cr_uid != newcred->cr_ruid) &&
742 	    newcred->cr_svuid != newcred->cr_uid) {
743 		change_svuid(newcred, newcred->cr_uid);
744 		setsugid(p);
745 	}
746 	p->p_ucred = newcred;
747 	crfree(oldcred);
748 	return (0);
749 }
750 
751 #ifndef _SYS_SYSPROTO_H_
752 struct setregid_args {
753 	gid_t	rgid;
754 	gid_t	egid;
755 };
756 #endif
757 /* ARGSUSED */
758 int
759 setregid(p, uap)
760 	register struct proc *p;
761 	struct setregid_args *uap;
762 {
763 	struct ucred *newcred, *oldcred;
764 	gid_t rgid, egid;
765 	int error;
766 
767 	rgid = uap->rgid;
768 	egid = uap->egid;
769 	oldcred = p->p_ucred;
770 	if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
771 	    rgid != oldcred->cr_svgid) ||
772 	     (egid != (gid_t)-1 && egid != oldcred->cr_groups[0] &&
773 	     egid != oldcred->cr_rgid && egid != oldcred->cr_svgid)) &&
774 	    (error = suser_xxx(oldcred, NULL, PRISON_ROOT)) != 0)
775 		return (error);
776 
777 	newcred = crdup(oldcred);
778 	if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
779 		change_egid(newcred, egid);
780 		setsugid(p);
781 	}
782 	if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
783 		change_rgid(newcred, rgid);
784 		setsugid(p);
785 	}
786 	if ((rgid != (gid_t)-1 || newcred->cr_groups[0] != newcred->cr_rgid) &&
787 	    newcred->cr_svgid != newcred->cr_groups[0]) {
788 		change_svgid(newcred, newcred->cr_groups[0]);
789 		setsugid(p);
790 	}
791 	return (0);
792 }
793 
794 /*
795  * setresuid(ruid, euid, suid) is like setreuid except control over the
796  * saved uid is explicit.
797  */
798 
799 #ifndef _SYS_SYSPROTO_H_
800 struct setresuid_args {
801 	uid_t	ruid;
802 	uid_t	euid;
803 	uid_t	suid;
804 };
805 #endif
806 /* ARGSUSED */
807 int
808 setresuid(p, uap)
809 	register struct proc *p;
810 	struct setresuid_args *uap;
811 {
812 	struct ucred *newcred, *oldcred;
813 	uid_t ruid, euid, suid;
814 	int error;
815 
816 	ruid = uap->ruid;
817 	euid = uap->euid;
818 	suid = uap->suid;
819 	oldcred = p->p_ucred;
820 	if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
821 	     ruid != oldcred->cr_svuid &&
822 	      ruid != oldcred->cr_uid) ||
823 	     (euid != (uid_t)-1 && euid != oldcred->cr_ruid &&
824 	    euid != oldcred->cr_svuid &&
825 	      euid != oldcred->cr_uid) ||
826 	     (suid != (uid_t)-1 && suid != oldcred->cr_ruid &&
827 	    suid != oldcred->cr_svuid &&
828 	      suid != oldcred->cr_uid)) &&
829 	    (error = suser_xxx(oldcred, NULL, PRISON_ROOT)) != 0)
830 		return (error);
831 
832 	newcred = crdup(oldcred);
833 	if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
834 		change_euid(newcred, euid);
835 		setsugid(p);
836 	}
837 	if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
838 		change_ruid(newcred, ruid);
839 		setsugid(p);
840 	}
841 	if (suid != (uid_t)-1 && oldcred->cr_svuid != suid) {
842 		change_svuid(newcred, suid);
843 		setsugid(p);
844 	}
845 	p->p_ucred = newcred;
846 	crfree(oldcred);
847 	return (0);
848 }
849 
850 /*
851  * setresgid(rgid, egid, sgid) is like setregid except control over the
852  * saved gid is explicit.
853  */
854 
855 #ifndef _SYS_SYSPROTO_H_
856 struct setresgid_args {
857 	gid_t	rgid;
858 	gid_t	egid;
859 	gid_t	sgid;
860 };
861 #endif
862 /* ARGSUSED */
863 int
864 setresgid(p, uap)
865 	register struct proc *p;
866 	struct setresgid_args *uap;
867 {
868 	struct ucred *newcred, *oldcred;
869 	gid_t rgid, egid, sgid;
870 	int error;
871 
872 	rgid = uap->rgid;
873 	egid = uap->egid;
874 	sgid = uap->sgid;
875 	oldcred = p->p_ucred;
876 	if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
877 	      rgid != oldcred->cr_svgid &&
878 	      rgid != oldcred->cr_groups[0]) ||
879 	     (egid != (gid_t)-1 && egid != oldcred->cr_rgid &&
880 	      egid != oldcred->cr_svgid &&
881 	      egid != oldcred->cr_groups[0]) ||
882 	     (sgid != (gid_t)-1 && sgid != oldcred->cr_rgid &&
883 	      sgid != oldcred->cr_svgid &&
884 	      sgid != oldcred->cr_groups[0])) &&
885 	    (error = suser_xxx(oldcred, NULL, PRISON_ROOT)) != 0)
886 		return (error);
887 
888 	newcred = crdup(oldcred);
889 	if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
890 		change_egid(newcred, egid);
891 		setsugid(p);
892 	}
893 	if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
894 		change_rgid(newcred, rgid);
895 		setsugid(p);
896 	}
897 	if (sgid != (gid_t)-1 && oldcred->cr_svgid != sgid) {
898 		change_svgid(newcred, sgid);
899 		setsugid(p);
900 	}
901 	p->p_ucred = newcred;
902 	crfree(oldcred);
903 	return (0);
904 }
905 
906 #ifndef _SYS_SYSPROTO_H_
907 struct getresuid_args {
908 	uid_t	*ruid;
909 	uid_t	*euid;
910 	uid_t	*suid;
911 };
912 #endif
913 /* ARGSUSED */
914 int
915 getresuid(p, uap)
916 	register struct proc *p;
917 	struct getresuid_args *uap;
918 {
919 	struct ucred *cred = p->p_ucred;
920 	int error1 = 0, error2 = 0, error3 = 0;
921 
922 	if (uap->ruid)
923 		error1 = copyout((caddr_t)&cred->cr_ruid,
924 		    (caddr_t)uap->ruid, sizeof(cred->cr_ruid));
925 	if (uap->euid)
926 		error2 = copyout((caddr_t)&cred->cr_uid,
927 		    (caddr_t)uap->euid, sizeof(cred->cr_uid));
928 	if (uap->suid)
929 		error3 = copyout((caddr_t)&cred->cr_svuid,
930 		    (caddr_t)uap->suid, sizeof(cred->cr_svuid));
931 	return error1 ? error1 : (error2 ? error2 : error3);
932 }
933 
934 #ifndef _SYS_SYSPROTO_H_
935 struct getresgid_args {
936 	gid_t	*rgid;
937 	gid_t	*egid;
938 	gid_t	*sgid;
939 };
940 #endif
941 /* ARGSUSED */
942 int
943 getresgid(p, uap)
944 	register struct proc *p;
945 	struct getresgid_args *uap;
946 {
947 	struct ucred *cred = p->p_ucred;
948 	int error1 = 0, error2 = 0, error3 = 0;
949 
950 	if (uap->rgid)
951 		error1 = copyout((caddr_t)&cred->cr_rgid,
952 		    (caddr_t)uap->rgid, sizeof(cred->cr_rgid));
953 	if (uap->egid)
954 		error2 = copyout((caddr_t)&cred->cr_groups[0],
955 		    (caddr_t)uap->egid, sizeof(cred->cr_groups[0]));
956 	if (uap->sgid)
957 		error3 = copyout((caddr_t)&cred->cr_svgid,
958 		    (caddr_t)uap->sgid, sizeof(cred->cr_svgid));
959 	return error1 ? error1 : (error2 ? error2 : error3);
960 }
961 
962 
963 #ifndef _SYS_SYSPROTO_H_
964 struct issetugid_args {
965 	int dummy;
966 };
967 #endif
968 /* ARGSUSED */
969 int
970 issetugid(p, uap)
971 	register struct proc *p;
972 	struct issetugid_args *uap;
973 {
974 	/*
975 	 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
976 	 * we use P_SUGID because we consider changing the owners as
977 	 * "tainting" as well.
978 	 * This is significant for procs that start as root and "become"
979 	 * a user without an exec - programs cannot know *everything*
980 	 * that libc *might* have put in their data segment.
981 	 */
982 	p->p_retval[0] = (p->p_flag & P_SUGID) ? 1 : 0;
983 	return (0);
984 }
985 
986 int
987 __setugid(p, uap)
988 	struct proc *p;
989 	struct __setugid_args *uap;
990 {
991 
992 #ifdef REGRESSION
993 	switch (uap->flag) {
994 	case 0:
995 		p->p_flag &= ~P_SUGID;
996 		return (0);
997 	case 1:
998 		p->p_flag |= P_SUGID;
999 		return (0);
1000 	default:
1001 		return (EINVAL);
1002 	}
1003 #else /* !REGRESSION */
1004 	return (ENOSYS);
1005 #endif /* !REGRESSION */
1006 }
1007 
1008 /*
1009  * Check if gid is a member of the group set.
1010  */
1011 int
1012 groupmember(gid, cred)
1013 	gid_t gid;
1014 	struct ucred *cred;
1015 {
1016 	register gid_t *gp;
1017 	gid_t *egp;
1018 
1019 	egp = &(cred->cr_groups[cred->cr_ngroups]);
1020 	for (gp = cred->cr_groups; gp < egp; gp++)
1021 		if (*gp == gid)
1022 			return (1);
1023 	return (0);
1024 }
1025 
1026 static int suser_permitted = 1;
1027 
1028 SYSCTL_INT(_kern, OID_AUTO, suser_permitted, CTLFLAG_RW, &suser_permitted, 0,
1029     "processes with uid 0 have privilege");
1030 
1031 /*
1032  * Test whether the specified credentials imply "super-user"
1033  * privilege; if so, and we have accounting info, set the flag
1034  * indicating use of super-powers.
1035  * Returns 0 or error.
1036  */
1037 int
1038 suser(p)
1039 	struct proc *p;
1040 {
1041 	return suser_xxx(0, p, 0);
1042 }
1043 
1044 int
1045 suser_xxx(cred, proc, flag)
1046 	struct ucred *cred;
1047 	struct proc *proc;
1048 	int flag;
1049 {
1050 	if (!suser_permitted)
1051 		return (EPERM);
1052 	if (!cred && !proc) {
1053 		printf("suser_xxx(): THINK!\n");
1054 		return (EPERM);
1055 	}
1056 	if (!cred)
1057 		cred = proc->p_ucred;
1058 	if (cred->cr_uid != 0)
1059 		return (EPERM);
1060 	if (jailed(cred) && !(flag & PRISON_ROOT))
1061 		return (EPERM);
1062 	return (0);
1063 }
1064 
1065 /*
1066  * u_cansee(u1, u2): determine if u1 "can see" the subject specified by u2
1067  * Arguments: imutable credentials u1, u2
1068  * Returns: 0 for permitted, an errno value otherwise
1069  * Locks: none
1070  * References: u1 and u2 must be valid for the lifetime of the call
1071  *             u1 may equal u2, in which case only one reference is required
1072  */
1073 int
1074 u_cansee(struct ucred *u1, struct ucred *u2)
1075 {
1076 	int error;
1077 
1078 	if ((error = prison_check(u1, u2)))
1079 		return (error);
1080 	if (!ps_showallprocs && u1->cr_ruid != u2->cr_ruid) {
1081 		if (suser_xxx(u1, NULL, PRISON_ROOT) != 0)
1082 			return (ESRCH);
1083 	}
1084 	return (0);
1085 }
1086 
1087 static int
1088 p_cansee(struct proc *p1, struct proc *p2, int *privused)
1089 {
1090 
1091 	/* XXX: privused is going away, so don't do that here. */
1092 	if (privused != NULL)
1093 		*privused = 0;
1094 	/* Wrap u_cansee() for all functionality. */
1095 	return (u_cansee(p1->p_ucred, p2->p_ucred));
1096 }
1097 
1098 /*
1099  * Can process p1 send the signal signum to process p2?
1100  */
1101 int
1102 p_cansignal(struct proc *p1, struct proc *p2, int signum)
1103 {
1104 	int	error;
1105 
1106 	if (p1 == p2)
1107 		return (0);
1108 
1109 	/*
1110 	 * Jail semantics limit the scope of signalling to p2 in the same
1111 	 * jail as p1, if p1 is in jail.
1112 	 */
1113 	if ((error = prison_check(p1->p_ucred, p2->p_ucred)))
1114 		return (error);
1115 
1116 	/*
1117 	 * UNIX signalling semantics require that processes in the same
1118 	 * session always be able to deliver SIGCONT to one another,
1119 	 * overriding the remaining protections.
1120 	 */
1121 	if (signum == SIGCONT && p1->p_session == p2->p_session)
1122 		return (0);
1123 
1124 	/*
1125 	 * UNIX uid semantics depend on the status of the P_SUGID
1126 	 * bit on the target process.  If the bit is set, then more
1127 	 * restricted signal sets are permitted.
1128 	 */
1129 	if (p2->p_flag & P_SUGID) {
1130 		switch (signum) {
1131 		case 0:
1132 		case SIGKILL:
1133 		case SIGINT:
1134 		case SIGTERM:
1135 		case SIGSTOP:
1136 		case SIGTTIN:
1137 		case SIGTTOU:
1138 		case SIGTSTP:
1139 		case SIGHUP:
1140 		case SIGUSR1:
1141 		case SIGUSR2:
1142 			break;
1143 		default:
1144 			/* Not permitted, try privilege. */
1145 			error = suser_xxx(NULL, p1, PRISON_ROOT);
1146 			if (error)
1147 				return (error);
1148 		}
1149 	}
1150 
1151 	/*
1152 	 * Generally, the object credential's ruid or svuid must match the
1153 	 * subject credential's ruid or euid.
1154 	 */
1155 	if (p1->p_ucred->cr_ruid != p2->p_ucred->cr_ruid &&
1156 	    p1->p_ucred->cr_ruid != p2->p_ucred->cr_svuid &&
1157 	    p1->p_ucred->cr_uid != p2->p_ucred->cr_ruid &&
1158 	    p1->p_ucred->cr_uid != p2->p_ucred->cr_svuid) {
1159 		/* Not permitted, try privilege. */
1160 		error = suser_xxx(NULL, p1, PRISON_ROOT);
1161 		if (error)
1162 			return (error);
1163 	}
1164 
1165         return (0);
1166 }
1167 
1168 static int
1169 p_cansched(struct proc *p1, struct proc *p2, int *privused)
1170 {
1171 	int error;
1172 
1173 	if (privused != NULL)
1174 		*privused = 0;
1175 
1176 	if (p1 == p2)
1177 		return (0);
1178 
1179 	if ((error = prison_check(p1->p_ucred, p2->p_ucred)))
1180 		return (error);
1181 
1182 	if (p1->p_ucred->cr_ruid == p2->p_ucred->cr_ruid)
1183 		return (0);
1184 	if (p1->p_ucred->cr_uid == p2->p_ucred->cr_ruid)
1185 		return (0);
1186 
1187 	if (!suser_xxx(0, p1, PRISON_ROOT)) {
1188 		if (privused != NULL)
1189 			*privused = 1;
1190 		return (0);
1191 	}
1192 
1193 #ifdef CAPABILITIES
1194 	if (!cap_check_xxx(0, p1, CAP_SYS_NICE, PRISON_ROOT)) {
1195 		if (privused != NULL)
1196 			*privused = 1;
1197 		return (0);
1198 	}
1199 #endif
1200 
1201 	return (EPERM);
1202 }
1203 
1204 static int
1205 p_candebug(struct proc *p1, struct proc *p2, int *privused)
1206 {
1207 	int error;
1208 
1209 	if (privused != NULL)
1210 		*privused = 0;
1211 
1212 	if (p1 == p2)
1213 		return (0);
1214 
1215 	if ((error = prison_check(p1->p_ucred, p2->p_ucred)))
1216 		return (error);
1217 
1218 	/* not owned by you, has done setuid (unless you're root) */
1219 	/* add a CAP_SYS_PTRACE here? */
1220 	if (p1->p_ucred->cr_uid != p2->p_ucred->cr_uid ||
1221 	    p1->p_ucred->cr_uid != p2->p_ucred->cr_svuid ||
1222 	    p1->p_ucred->cr_uid != p2->p_ucred->cr_ruid ||
1223 	    p2->p_flag & P_SUGID) {
1224 		if ((error = suser_xxx(0, p1, PRISON_ROOT)))
1225 			return (error);
1226 		if (privused != NULL)
1227 			*privused = 1;
1228 	}
1229 
1230 	/* can't trace init when securelevel > 0 */
1231 	if (securelevel > 0 && p2->p_pid == 1)
1232 		return (EPERM);
1233 
1234 	return (0);
1235 }
1236 
1237 int
1238 p_can(struct proc *p1, struct proc *p2, int operation,
1239     int *privused)
1240 {
1241 
1242 	switch(operation) {
1243 	case P_CAN_SEE:
1244 		return (p_cansee(p1, p2, privused));
1245 
1246 	case P_CAN_SCHED:
1247 		return (p_cansched(p1, p2, privused));
1248 
1249 	case P_CAN_DEBUG:
1250 		return (p_candebug(p1, p2, privused));
1251 
1252 	default:
1253 		panic("p_can: invalid operation");
1254 	}
1255 }
1256 
1257 
1258 /*
1259  * Allocate a zeroed cred structure.
1260  */
1261 struct ucred *
1262 crget()
1263 {
1264 	register struct ucred *cr;
1265 
1266 	MALLOC(cr, struct ucred *, sizeof(*cr), M_CRED, M_WAITOK|M_ZERO);
1267 	cr->cr_ref = 1;
1268 	mtx_init(&cr->cr_mtx, "ucred", MTX_DEF);
1269 	return (cr);
1270 }
1271 
1272 /*
1273  * Claim another reference to a ucred structure
1274  */
1275 void
1276 crhold(cr)
1277 	struct ucred *cr;
1278 {
1279 
1280 	mtx_lock(&cr->cr_mtx);
1281 	cr->cr_ref++;
1282 	mtx_unlock(&(cr)->cr_mtx);
1283 }
1284 
1285 
1286 /*
1287  * Free a cred structure.
1288  * Throws away space when ref count gets to 0.
1289  */
1290 void
1291 crfree(cr)
1292 	struct ucred *cr;
1293 {
1294 
1295 	mtx_lock(&cr->cr_mtx);
1296 	KASSERT(cr->cr_ref > 0, ("bad ucred refcount: %d", cr->cr_ref));
1297 	if (--cr->cr_ref == 0) {
1298 		mtx_destroy(&cr->cr_mtx);
1299 		/*
1300 		 * Some callers of crget(), such as nfs_statfs(),
1301 		 * allocate a temporary credential, but don't
1302 		 * allocate a uidinfo structure.
1303 		 */
1304 		if (cr->cr_uidinfo != NULL)
1305 			uifree(cr->cr_uidinfo);
1306 		if (cr->cr_ruidinfo != NULL)
1307 			uifree(cr->cr_ruidinfo);
1308 		/*
1309 		 * Free a prison, if any.
1310 		 */
1311 		if (jailed(cr))
1312 			prison_free(cr->cr_prison);
1313 		FREE((caddr_t)cr, M_CRED);
1314 	} else {
1315 		mtx_unlock(&cr->cr_mtx);
1316 	}
1317 }
1318 
1319 /*
1320  * Copy cred structure to a new one and free the old one.
1321  */
1322 struct ucred *
1323 crcopy(cr)
1324 	struct ucred *cr;
1325 {
1326 	struct ucred *newcr;
1327 
1328 	mtx_lock(&cr->cr_mtx);
1329 	if (cr->cr_ref == 1) {
1330 		mtx_unlock(&cr->cr_mtx);
1331 		return (cr);
1332 	}
1333 	mtx_unlock(&cr->cr_mtx);
1334 	newcr = crdup(cr);
1335 	crfree(cr);
1336 	return (newcr);
1337 }
1338 
1339 /*
1340  * Dup cred struct to a new held one.
1341  */
1342 struct ucred *
1343 crdup(cr)
1344 	struct ucred *cr;
1345 {
1346 	struct ucred *newcr;
1347 
1348 	MALLOC(newcr, struct ucred *, sizeof(*cr), M_CRED, M_WAITOK);
1349 	*newcr = *cr;
1350 	mtx_init(&newcr->cr_mtx, "ucred", MTX_DEF);
1351 	uihold(newcr->cr_uidinfo);
1352 	uihold(newcr->cr_ruidinfo);
1353 	if (jailed(newcr))
1354 		prison_hold(newcr->cr_prison);
1355 	newcr->cr_ref = 1;
1356 	return (newcr);
1357 }
1358 
1359 /*
1360  * Get login name, if available.
1361  */
1362 #ifndef _SYS_SYSPROTO_H_
1363 struct getlogin_args {
1364 	char	*namebuf;
1365 	u_int	namelen;
1366 };
1367 #endif
1368 /* ARGSUSED */
1369 int
1370 getlogin(p, uap)
1371 	struct proc *p;
1372 	struct getlogin_args *uap;
1373 {
1374 
1375 	if (uap->namelen > MAXLOGNAME)
1376 		uap->namelen = MAXLOGNAME;
1377 	return (copyout((caddr_t) p->p_pgrp->pg_session->s_login,
1378 	    (caddr_t) uap->namebuf, uap->namelen));
1379 }
1380 
1381 /*
1382  * Set login name.
1383  */
1384 #ifndef _SYS_SYSPROTO_H_
1385 struct setlogin_args {
1386 	char	*namebuf;
1387 };
1388 #endif
1389 /* ARGSUSED */
1390 int
1391 setlogin(p, uap)
1392 	struct proc *p;
1393 	struct setlogin_args *uap;
1394 {
1395 	int error;
1396 	char logintmp[MAXLOGNAME];
1397 
1398 	if ((error = suser_xxx(0, p, PRISON_ROOT)))
1399 		return (error);
1400 	error = copyinstr((caddr_t) uap->namebuf, (caddr_t) logintmp,
1401 	    sizeof(logintmp), (size_t *)0);
1402 	if (error == ENAMETOOLONG)
1403 		error = EINVAL;
1404 	else if (!error)
1405 		(void) memcpy(p->p_pgrp->pg_session->s_login, logintmp,
1406 		    sizeof(logintmp));
1407 	return (error);
1408 }
1409 
1410 void
1411 setsugid(p)
1412 	struct proc *p;
1413 {
1414 	p->p_flag |= P_SUGID;
1415 	if (!(p->p_pfsflags & PF_ISUGID))
1416 		p->p_stops = 0;
1417 }
1418 
1419 /*
1420  * change_euid(): Change a process's effective uid.
1421  * Side effects: newcred->cr_uid and newcred->cr_uidinfo will be modified.
1422  * References: newcred must be an exclusive credential reference for the
1423  *             duration of the call.
1424  */
1425 void
1426 change_euid(newcred, euid)
1427 	struct ucred *newcred;
1428 	uid_t euid;
1429 {
1430 
1431 	newcred->cr_uid = euid;
1432 	uifree(newcred->cr_uidinfo);
1433 	newcred->cr_uidinfo = uifind(euid);
1434 }
1435 
1436 /*
1437  * change_egid(): Change a process's effective gid.
1438  * Side effects: newcred->cr_gid will be modified.
1439  * References: newcred must be an exclusive credential reference for the
1440  *             duration of the call.
1441  */
1442 void
1443 change_egid(newcred, egid)
1444 	struct ucred *newcred;
1445 	gid_t egid;
1446 {
1447 
1448 	newcred->cr_groups[0] = egid;
1449 }
1450 
1451 /*
1452  * change_ruid(): Change a process's real uid.
1453  * Side effects: newcred->cr_ruid will be updated, newcred->cr_ruidinfo
1454  *               will be updated, and the old and new cr_ruidinfo proc
1455  *               counts will be updated.
1456  * References: newcred must be an exclusive credential reference for the
1457  *             duration of the call.
1458  */
1459 void
1460 change_ruid(newcred, ruid)
1461 	struct ucred *newcred;
1462 	uid_t ruid;
1463 {
1464 
1465 	(void)chgproccnt(newcred->cr_ruidinfo, -1, 0);
1466 	newcred->cr_ruid = ruid;
1467 	uifree(newcred->cr_ruidinfo);
1468 	newcred->cr_ruidinfo = uifind(ruid);
1469 	(void)chgproccnt(newcred->cr_ruidinfo, 1, 0);
1470 }
1471 
1472 /*
1473  * change_rgid(): Change a process's real gid.
1474  * Side effects: newcred->cr_rgid will be updated.
1475  * References: newcred must be an exclusive credential reference for the
1476  *             duration of the call.
1477  */
1478 void
1479 change_rgid(newcred, rgid)
1480 	struct ucred *newcred;
1481 	gid_t rgid;
1482 {
1483 
1484 	newcred->cr_rgid = rgid;
1485 }
1486 
1487 /*
1488  * change_svuid(): Change a process's saved uid.
1489  * Side effects: newcred->cr_svuid will be updated.
1490  * References: newcred must be an exclusive credential reference for the
1491  *             duration of the call.
1492  */
1493 void
1494 change_svuid(newcred, svuid)
1495 	struct ucred *newcred;
1496 	uid_t svuid;
1497 {
1498 
1499 	newcred->cr_svuid = svuid;
1500 }
1501 
1502 /*
1503  * change_svgid(): Change a process's saved gid.
1504  * Side effects: newcred->cr_svgid will be updated.
1505  * References: newcred must be an exclusive credential reference for the
1506  *             duration of the call.
1507  */
1508 void
1509 change_svgid(newcred, svgid)
1510 	struct ucred *newcred;
1511 	gid_t svgid;
1512 {
1513 
1514 	newcred->cr_svgid = svgid;
1515 }
1516