2 * Copyright (c) 1982, 1986, 1989, 1990, 1991, 1993
3 * The Regents of the University of California.
4 * (c) UNIX System Laboratories, Inc.
5 * Copyright (c) 2000-2001 Robert N. M. Watson.
8 * All or some portions of this file are derived from material licensed
9 * to the University of California by American Telephone and Telegraph
10 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11 * the permission of UNIX System Laboratories, Inc.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * @(#)kern_prot.c 8.6 (Berkeley) 1/21/94
41 * System calls related to processes and protection
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include "opt_compat.h"
49 #include "opt_inet6.h"
51 #include <sys/param.h>
52 #include <sys/systm.h>
55 #include <sys/kernel.h>
57 #include <sys/malloc.h>
58 #include <sys/mutex.h>
59 #include <sys/refcount.h>
63 #include <sys/sysproto.h>
65 #include <sys/pioctl.h>
66 #include <sys/resourcevar.h>
67 #include <sys/socket.h>
68 #include <sys/socketvar.h>
69 #include <sys/syscallsubr.h>
70 #include <sys/sysctl.h>
72 #if defined(INET) || defined(INET6)
73 #include <netinet/in.h>
74 #include <netinet/in_pcb.h>
77 #include <security/audit/audit.h>
78 #include <security/mac/mac_framework.h>
80 static MALLOC_DEFINE(M_CRED, "cred", "credentials");
82 SYSCTL_NODE(_security, OID_AUTO, bsd, CTLFLAG_RW, 0, "BSD security policy");
84 static void crextend(struct ucred *cr, int n);
85 static void crsetgroups_locked(struct ucred *cr, int ngrp,
88 #ifndef _SYS_SYSPROTO_H_
95 getpid(struct thread *td, struct getpid_args *uap)
97 struct proc *p = td->td_proc;
99 td->td_retval[0] = p->p_pid;
100 #if defined(COMPAT_43)
102 td->td_retval[1] = p->p_pptr->p_pid;
108 #ifndef _SYS_SYSPROTO_H_
109 struct getppid_args {
115 getppid(struct thread *td, struct getppid_args *uap)
117 struct proc *p = td->td_proc;
120 td->td_retval[0] = p->p_pptr->p_pid;
126 * Get process group ID; note that POSIX getpgrp takes no parameter.
128 #ifndef _SYS_SYSPROTO_H_
129 struct getpgrp_args {
134 getpgrp(struct thread *td, struct getpgrp_args *uap)
136 struct proc *p = td->td_proc;
139 td->td_retval[0] = p->p_pgrp->pg_id;
144 /* Get an arbitary pid's process group id */
145 #ifndef _SYS_SYSPROTO_H_
146 struct getpgid_args {
151 getpgid(struct thread *td, struct getpgid_args *uap)
163 error = p_cansee(td, p);
169 td->td_retval[0] = p->p_pgrp->pg_id;
175 * Get an arbitary pid's session id.
177 #ifndef _SYS_SYSPROTO_H_
183 getsid(struct thread *td, struct getsid_args *uap)
195 error = p_cansee(td, p);
201 td->td_retval[0] = p->p_session->s_sid;
206 #ifndef _SYS_SYSPROTO_H_
213 getuid(struct thread *td, struct getuid_args *uap)
216 td->td_retval[0] = td->td_ucred->cr_ruid;
217 #if defined(COMPAT_43)
218 td->td_retval[1] = td->td_ucred->cr_uid;
223 #ifndef _SYS_SYSPROTO_H_
224 struct geteuid_args {
230 geteuid(struct thread *td, struct geteuid_args *uap)
233 td->td_retval[0] = td->td_ucred->cr_uid;
237 #ifndef _SYS_SYSPROTO_H_
244 getgid(struct thread *td, struct getgid_args *uap)
247 td->td_retval[0] = td->td_ucred->cr_rgid;
248 #if defined(COMPAT_43)
249 td->td_retval[1] = td->td_ucred->cr_groups[0];
255 * Get effective group ID. The "egid" is groups[0], and could be obtained
256 * via getgroups. This syscall exists because it is somewhat painful to do
257 * correctly in a library function.
259 #ifndef _SYS_SYSPROTO_H_
260 struct getegid_args {
266 getegid(struct thread *td, struct getegid_args *uap)
269 td->td_retval[0] = td->td_ucred->cr_groups[0];
273 #ifndef _SYS_SYSPROTO_H_
274 struct getgroups_args {
280 getgroups(struct thread *td, register struct getgroups_args *uap)
286 ngrp = MIN(uap->gidsetsize, NGROUPS);
287 groups = malloc(ngrp * sizeof(*groups), M_TEMP, M_WAITOK);
288 error = kern_getgroups(td, &ngrp, groups);
291 if (uap->gidsetsize > 0)
292 error = copyout(groups, uap->gidset, ngrp * sizeof(gid_t));
294 td->td_retval[0] = ngrp;
296 free(groups, M_TEMP);
301 kern_getgroups(struct thread *td, u_int *ngrp, gid_t *groups)
307 *ngrp = cred->cr_ngroups;
310 if (*ngrp < cred->cr_ngroups)
312 *ngrp = cred->cr_ngroups;
313 bcopy(cred->cr_groups, groups, *ngrp * sizeof(gid_t));
317 #ifndef _SYS_SYSPROTO_H_
324 setsid(register struct thread *td, struct setsid_args *uap)
328 struct proc *p = td->td_proc;
329 struct pgrp *newpgrp;
330 struct session *newsess;
335 newpgrp = malloc(sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
336 newsess = malloc(sizeof(struct session), M_SESSION, M_WAITOK | M_ZERO);
338 sx_xlock(&proctree_lock);
340 if (p->p_pgid == p->p_pid || (pgrp = pgfind(p->p_pid)) != NULL) {
345 (void)enterpgrp(p, p->p_pid, newpgrp, newsess);
346 td->td_retval[0] = p->p_pid;
351 sx_xunlock(&proctree_lock);
354 free(newpgrp, M_PGRP);
356 free(newsess, M_SESSION);
362 * set process group (setpgid/old setpgrp)
364 * caller does setpgid(targpid, targpgid)
366 * pid must be caller or child of caller (ESRCH)
368 * pid must be in same session (EPERM)
369 * pid can't have done an exec (EACCES)
371 * there must exist some pid in same session having pgid (EPERM)
372 * pid must not be session leader (EPERM)
374 #ifndef _SYS_SYSPROTO_H_
375 struct setpgid_args {
376 int pid; /* target process id */
377 int pgid; /* target pgrp id */
382 setpgid(struct thread *td, register struct setpgid_args *uap)
384 struct proc *curp = td->td_proc;
385 register struct proc *targp; /* target process */
386 register struct pgrp *pgrp; /* target pgrp */
388 struct pgrp *newpgrp;
395 newpgrp = malloc(sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
397 sx_xlock(&proctree_lock);
398 if (uap->pid != 0 && uap->pid != curp->p_pid) {
399 if ((targp = pfind(uap->pid)) == NULL) {
403 if (!inferior(targp)) {
408 if ((error = p_cansee(td, targp))) {
412 if (targp->p_pgrp == NULL ||
413 targp->p_session != curp->p_session) {
418 if (targp->p_flag & P_EXEC) {
426 if (SESS_LEADER(targp)) {
431 uap->pgid = targp->p_pid;
432 if ((pgrp = pgfind(uap->pgid)) == NULL) {
433 if (uap->pgid == targp->p_pid) {
434 error = enterpgrp(targp, uap->pgid, newpgrp,
441 if (pgrp == targp->p_pgrp) {
445 if (pgrp->pg_id != targp->p_pid &&
446 pgrp->pg_session != curp->p_session) {
452 error = enterthispgrp(targp, pgrp);
455 sx_xunlock(&proctree_lock);
456 KASSERT((error == 0) || (newpgrp != NULL),
457 ("setpgid failed and newpgrp is NULL"));
459 free(newpgrp, M_PGRP);
464 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
465 * compatible. It says that setting the uid/gid to euid/egid is a special
466 * case of "appropriate privilege". Once the rules are expanded out, this
467 * basically means that setuid(nnn) sets all three id's, in all permitted
468 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
469 * does not set the saved id - this is dangerous for traditional BSD
470 * programs. For this reason, we *really* do not want to set
471 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
473 #define POSIX_APPENDIX_B_4_2_2
475 #ifndef _SYS_SYSPROTO_H_
482 setuid(struct thread *td, struct setuid_args *uap)
484 struct proc *p = td->td_proc;
485 struct ucred *newcred, *oldcred;
496 * Copy credentials so other references do not see our changes.
498 oldcred = crcopysafe(p, newcred);
501 error = mac_cred_check_setuid(oldcred, uid);
507 * See if we have "permission" by POSIX 1003.1 rules.
509 * Note that setuid(geteuid()) is a special case of
510 * "appropriate privileges" in appendix B.4.2.2. We need
511 * to use this clause to be compatible with traditional BSD
512 * semantics. Basically, it means that "setuid(xx)" sets all
513 * three id's (assuming you have privs).
515 * Notes on the logic. We do things in three steps.
516 * 1: We determine if the euid is going to change, and do EPERM
517 * right away. We unconditionally change the euid later if this
518 * test is satisfied, simplifying that part of the logic.
519 * 2: We determine if the real and/or saved uids are going to
520 * change. Determined by compile options.
521 * 3: Change euid last. (after tests in #2 for "appropriate privs")
523 if (uid != oldcred->cr_ruid && /* allow setuid(getuid()) */
524 #ifdef _POSIX_SAVED_IDS
525 uid != oldcred->cr_svuid && /* allow setuid(saved gid) */
527 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
528 uid != oldcred->cr_uid && /* allow setuid(geteuid()) */
530 (error = priv_check_cred(oldcred, PRIV_CRED_SETUID, 0)) != 0)
533 #ifdef _POSIX_SAVED_IDS
535 * Do we have "appropriate privileges" (are we root or uid == euid)
536 * If so, we are changing the real uid and/or saved uid.
539 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
540 uid == oldcred->cr_uid ||
542 /* We are using privs. */
543 priv_check_cred(oldcred, PRIV_CRED_SETUID, 0) == 0)
547 * Set the real uid and transfer proc count to new user.
549 if (uid != oldcred->cr_ruid) {
550 change_ruid(newcred, uip);
556 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
557 * the security of seteuid() depends on it. B.4.2.2 says it
558 * is important that we should do this.
560 if (uid != oldcred->cr_svuid) {
561 change_svuid(newcred, uid);
567 * In all permitted cases, we are changing the euid.
569 if (uid != oldcred->cr_uid) {
570 change_euid(newcred, uip);
573 p->p_ucred = newcred;
586 #ifndef _SYS_SYSPROTO_H_
587 struct seteuid_args {
593 seteuid(struct thread *td, struct seteuid_args *uap)
595 struct proc *p = td->td_proc;
596 struct ucred *newcred, *oldcred;
598 struct uidinfo *euip;
602 AUDIT_ARG_EUID(euid);
607 * Copy credentials so other references do not see our changes.
609 oldcred = crcopysafe(p, newcred);
612 error = mac_cred_check_seteuid(oldcred, euid);
617 if (euid != oldcred->cr_ruid && /* allow seteuid(getuid()) */
618 euid != oldcred->cr_svuid && /* allow seteuid(saved uid) */
619 (error = priv_check_cred(oldcred, PRIV_CRED_SETEUID, 0)) != 0)
623 * Everything's okay, do it.
625 if (oldcred->cr_uid != euid) {
626 change_euid(newcred, euip);
629 p->p_ucred = newcred;
642 #ifndef _SYS_SYSPROTO_H_
649 setgid(struct thread *td, struct setgid_args *uap)
651 struct proc *p = td->td_proc;
652 struct ucred *newcred, *oldcred;
660 oldcred = crcopysafe(p, newcred);
663 error = mac_cred_check_setgid(oldcred, gid);
669 * See if we have "permission" by POSIX 1003.1 rules.
671 * Note that setgid(getegid()) is a special case of
672 * "appropriate privileges" in appendix B.4.2.2. We need
673 * to use this clause to be compatible with traditional BSD
674 * semantics. Basically, it means that "setgid(xx)" sets all
675 * three id's (assuming you have privs).
677 * For notes on the logic here, see setuid() above.
679 if (gid != oldcred->cr_rgid && /* allow setgid(getgid()) */
680 #ifdef _POSIX_SAVED_IDS
681 gid != oldcred->cr_svgid && /* allow setgid(saved gid) */
683 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
684 gid != oldcred->cr_groups[0] && /* allow setgid(getegid()) */
686 (error = priv_check_cred(oldcred, PRIV_CRED_SETGID, 0)) != 0)
689 #ifdef _POSIX_SAVED_IDS
691 * Do we have "appropriate privileges" (are we root or gid == egid)
692 * If so, we are changing the real uid and saved gid.
695 #ifdef POSIX_APPENDIX_B_4_2_2 /* use the clause from B.4.2.2 */
696 gid == oldcred->cr_groups[0] ||
698 /* We are using privs. */
699 priv_check_cred(oldcred, PRIV_CRED_SETGID, 0) == 0)
705 if (oldcred->cr_rgid != gid) {
706 change_rgid(newcred, gid);
712 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
713 * the security of setegid() depends on it. B.4.2.2 says it
714 * is important that we should do this.
716 if (oldcred->cr_svgid != gid) {
717 change_svgid(newcred, gid);
722 * In all cases permitted cases, we are changing the egid.
723 * Copy credentials so other references do not see our changes.
725 if (oldcred->cr_groups[0] != gid) {
726 change_egid(newcred, gid);
729 p->p_ucred = newcred;
740 #ifndef _SYS_SYSPROTO_H_
741 struct setegid_args {
747 setegid(struct thread *td, struct setegid_args *uap)
749 struct proc *p = td->td_proc;
750 struct ucred *newcred, *oldcred;
755 AUDIT_ARG_EGID(egid);
758 oldcred = crcopysafe(p, newcred);
761 error = mac_cred_check_setegid(oldcred, egid);
766 if (egid != oldcred->cr_rgid && /* allow setegid(getgid()) */
767 egid != oldcred->cr_svgid && /* allow setegid(saved gid) */
768 (error = priv_check_cred(oldcred, PRIV_CRED_SETEGID, 0)) != 0)
771 if (oldcred->cr_groups[0] != egid) {
772 change_egid(newcred, egid);
775 p->p_ucred = newcred;
786 #ifndef _SYS_SYSPROTO_H_
787 struct setgroups_args {
794 setgroups(struct thread *td, struct setgroups_args *uap)
796 gid_t *groups = NULL;
799 if (uap->gidsetsize > NGROUPS)
801 groups = malloc(uap->gidsetsize * sizeof(gid_t), M_TEMP, M_WAITOK);
802 error = copyin(uap->gidset, groups, uap->gidsetsize * sizeof(gid_t));
805 error = kern_setgroups(td, uap->gidsetsize, groups);
807 free(groups, M_TEMP);
812 kern_setgroups(struct thread *td, u_int ngrp, gid_t *groups)
814 struct proc *p = td->td_proc;
815 struct ucred *newcred, *oldcred;
820 AUDIT_ARG_GROUPSET(groups, ngrp);
822 crextend(newcred, ngrp);
824 oldcred = crcopysafe(p, newcred);
827 error = mac_cred_check_setgroups(oldcred, ngrp, groups);
832 error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0);
838 * setgroups(0, NULL) is a legitimate way of clearing the
839 * groups vector on non-BSD systems (which generally do not
840 * have the egid in the groups[0]). We risk security holes
841 * when running non-BSD software if we do not do the same.
843 newcred->cr_ngroups = 1;
845 crsetgroups_locked(newcred, ngrp, groups);
848 p->p_ucred = newcred;
859 #ifndef _SYS_SYSPROTO_H_
860 struct setreuid_args {
867 setreuid(register struct thread *td, struct setreuid_args *uap)
869 struct proc *p = td->td_proc;
870 struct ucred *newcred, *oldcred;
872 struct uidinfo *euip, *ruip;
877 AUDIT_ARG_EUID(euid);
878 AUDIT_ARG_RUID(ruid);
883 oldcred = crcopysafe(p, newcred);
886 error = mac_cred_check_setreuid(oldcred, ruid, euid);
891 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
892 ruid != oldcred->cr_svuid) ||
893 (euid != (uid_t)-1 && euid != oldcred->cr_uid &&
894 euid != oldcred->cr_ruid && euid != oldcred->cr_svuid)) &&
895 (error = priv_check_cred(oldcred, PRIV_CRED_SETREUID, 0)) != 0)
898 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
899 change_euid(newcred, euip);
902 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
903 change_ruid(newcred, ruip);
906 if ((ruid != (uid_t)-1 || newcred->cr_uid != newcred->cr_ruid) &&
907 newcred->cr_svuid != newcred->cr_uid) {
908 change_svuid(newcred, newcred->cr_uid);
911 p->p_ucred = newcred;
926 #ifndef _SYS_SYSPROTO_H_
927 struct setregid_args {
934 setregid(register struct thread *td, struct setregid_args *uap)
936 struct proc *p = td->td_proc;
937 struct ucred *newcred, *oldcred;
943 AUDIT_ARG_EGID(egid);
944 AUDIT_ARG_RGID(rgid);
947 oldcred = crcopysafe(p, newcred);
950 error = mac_cred_check_setregid(oldcred, rgid, egid);
955 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
956 rgid != oldcred->cr_svgid) ||
957 (egid != (gid_t)-1 && egid != oldcred->cr_groups[0] &&
958 egid != oldcred->cr_rgid && egid != oldcred->cr_svgid)) &&
959 (error = priv_check_cred(oldcred, PRIV_CRED_SETREGID, 0)) != 0)
962 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
963 change_egid(newcred, egid);
966 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
967 change_rgid(newcred, rgid);
970 if ((rgid != (gid_t)-1 || newcred->cr_groups[0] != newcred->cr_rgid) &&
971 newcred->cr_svgid != newcred->cr_groups[0]) {
972 change_svgid(newcred, newcred->cr_groups[0]);
975 p->p_ucred = newcred;
987 * setresuid(ruid, euid, suid) is like setreuid except control over the saved
990 #ifndef _SYS_SYSPROTO_H_
991 struct setresuid_args {
999 setresuid(register struct thread *td, struct setresuid_args *uap)
1001 struct proc *p = td->td_proc;
1002 struct ucred *newcred, *oldcred;
1003 uid_t euid, ruid, suid;
1004 struct uidinfo *euip, *ruip;
1010 AUDIT_ARG_EUID(euid);
1011 AUDIT_ARG_RUID(ruid);
1012 AUDIT_ARG_SUID(suid);
1014 euip = uifind(euid);
1015 ruip = uifind(ruid);
1017 oldcred = crcopysafe(p, newcred);
1020 error = mac_cred_check_setresuid(oldcred, ruid, euid, suid);
1025 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
1026 ruid != oldcred->cr_svuid &&
1027 ruid != oldcred->cr_uid) ||
1028 (euid != (uid_t)-1 && euid != oldcred->cr_ruid &&
1029 euid != oldcred->cr_svuid &&
1030 euid != oldcred->cr_uid) ||
1031 (suid != (uid_t)-1 && suid != oldcred->cr_ruid &&
1032 suid != oldcred->cr_svuid &&
1033 suid != oldcred->cr_uid)) &&
1034 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESUID, 0)) != 0)
1037 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
1038 change_euid(newcred, euip);
1041 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
1042 change_ruid(newcred, ruip);
1045 if (suid != (uid_t)-1 && oldcred->cr_svuid != suid) {
1046 change_svuid(newcred, suid);
1049 p->p_ucred = newcred;
1066 * setresgid(rgid, egid, sgid) is like setregid except control over the saved
1069 #ifndef _SYS_SYSPROTO_H_
1070 struct setresgid_args {
1078 setresgid(register struct thread *td, struct setresgid_args *uap)
1080 struct proc *p = td->td_proc;
1081 struct ucred *newcred, *oldcred;
1082 gid_t egid, rgid, sgid;
1088 AUDIT_ARG_EGID(egid);
1089 AUDIT_ARG_RGID(rgid);
1090 AUDIT_ARG_SGID(sgid);
1093 oldcred = crcopysafe(p, newcred);
1096 error = mac_cred_check_setresgid(oldcred, rgid, egid, sgid);
1101 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
1102 rgid != oldcred->cr_svgid &&
1103 rgid != oldcred->cr_groups[0]) ||
1104 (egid != (gid_t)-1 && egid != oldcred->cr_rgid &&
1105 egid != oldcred->cr_svgid &&
1106 egid != oldcred->cr_groups[0]) ||
1107 (sgid != (gid_t)-1 && sgid != oldcred->cr_rgid &&
1108 sgid != oldcred->cr_svgid &&
1109 sgid != oldcred->cr_groups[0])) &&
1110 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESGID, 0)) != 0)
1113 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
1114 change_egid(newcred, egid);
1117 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
1118 change_rgid(newcred, rgid);
1121 if (sgid != (gid_t)-1 && oldcred->cr_svgid != sgid) {
1122 change_svgid(newcred, sgid);
1125 p->p_ucred = newcred;
1136 #ifndef _SYS_SYSPROTO_H_
1137 struct getresuid_args {
1145 getresuid(register struct thread *td, struct getresuid_args *uap)
1148 int error1 = 0, error2 = 0, error3 = 0;
1150 cred = td->td_ucred;
1152 error1 = copyout(&cred->cr_ruid,
1153 uap->ruid, sizeof(cred->cr_ruid));
1155 error2 = copyout(&cred->cr_uid,
1156 uap->euid, sizeof(cred->cr_uid));
1158 error3 = copyout(&cred->cr_svuid,
1159 uap->suid, sizeof(cred->cr_svuid));
1160 return (error1 ? error1 : error2 ? error2 : error3);
1163 #ifndef _SYS_SYSPROTO_H_
1164 struct getresgid_args {
1172 getresgid(register struct thread *td, struct getresgid_args *uap)
1175 int error1 = 0, error2 = 0, error3 = 0;
1177 cred = td->td_ucred;
1179 error1 = copyout(&cred->cr_rgid,
1180 uap->rgid, sizeof(cred->cr_rgid));
1182 error2 = copyout(&cred->cr_groups[0],
1183 uap->egid, sizeof(cred->cr_groups[0]));
1185 error3 = copyout(&cred->cr_svgid,
1186 uap->sgid, sizeof(cred->cr_svgid));
1187 return (error1 ? error1 : error2 ? error2 : error3);
1190 #ifndef _SYS_SYSPROTO_H_
1191 struct issetugid_args {
1197 issetugid(register struct thread *td, struct issetugid_args *uap)
1199 struct proc *p = td->td_proc;
1202 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
1203 * we use P_SUGID because we consider changing the owners as
1204 * "tainting" as well.
1205 * This is significant for procs that start as root and "become"
1206 * a user without an exec - programs cannot know *everything*
1207 * that libc *might* have put in their data segment.
1210 td->td_retval[0] = (p->p_flag & P_SUGID) ? 1 : 0;
1216 __setugid(struct thread *td, struct __setugid_args *uap)
1222 switch (uap->flag) {
1225 p->p_flag &= ~P_SUGID;
1230 p->p_flag |= P_SUGID;
1236 #else /* !REGRESSION */
1239 #endif /* REGRESSION */
1243 * Check if gid is a member of the group set.
1246 groupmember(gid_t gid, struct ucred *cred)
1252 if (cred->cr_groups[0] == gid)
1256 * If gid was not our primary group, perform a binary search
1257 * of the supplemental groups. This is possible because we
1258 * sort the groups in crsetgroups().
1261 h = cred->cr_ngroups;
1263 m = l + ((h - l) / 2);
1264 if (cred->cr_groups[m] < gid)
1269 if ((l < cred->cr_ngroups) && (cred->cr_groups[l] == gid))
1276 * Test the active securelevel against a given level. securelevel_gt()
1277 * implements (securelevel > level). securelevel_ge() implements
1278 * (securelevel >= level). Note that the logic is inverted -- these
1279 * functions return EPERM on "success" and 0 on "failure".
1281 * Due to care taken when setting the securelevel, we know that no jail will
1282 * be less secure that its parent (or the physical system), so it is sufficient
1283 * to test the current jail only.
1285 * XXXRW: Possibly since this has to do with privilege, it should move to
1289 securelevel_gt(struct ucred *cr, int level)
1292 return (cr->cr_prison->pr_securelevel > level ? EPERM : 0);
1296 securelevel_ge(struct ucred *cr, int level)
1299 return (cr->cr_prison->pr_securelevel >= level ? EPERM : 0);
1303 * 'see_other_uids' determines whether or not visibility of processes
1304 * and sockets with credentials holding different real uids is possible
1305 * using a variety of system MIBs.
1306 * XXX: data declarations should be together near the beginning of the file.
1308 static int see_other_uids = 1;
1309 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_uids, CTLFLAG_RW,
1311 "Unprivileged processes may see subjects/objects with different real uid");
1314 * Determine if u1 "can see" the subject specified by u2, according to the
1315 * 'see_other_uids' policy.
1316 * Returns: 0 for permitted, ESRCH otherwise
1318 * References: *u1 and *u2 must not change during the call
1319 * u1 may equal u2, in which case only one reference is required
1322 cr_seeotheruids(struct ucred *u1, struct ucred *u2)
1325 if (!see_other_uids && u1->cr_ruid != u2->cr_ruid) {
1326 if (priv_check_cred(u1, PRIV_SEEOTHERUIDS, 0) != 0)
1333 * 'see_other_gids' determines whether or not visibility of processes
1334 * and sockets with credentials holding different real gids is possible
1335 * using a variety of system MIBs.
1336 * XXX: data declarations should be together near the beginning of the file.
1338 static int see_other_gids = 1;
1339 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_gids, CTLFLAG_RW,
1341 "Unprivileged processes may see subjects/objects with different real gid");
1344 * Determine if u1 can "see" the subject specified by u2, according to the
1345 * 'see_other_gids' policy.
1346 * Returns: 0 for permitted, ESRCH otherwise
1348 * References: *u1 and *u2 must not change during the call
1349 * u1 may equal u2, in which case only one reference is required
1352 cr_seeothergids(struct ucred *u1, struct ucred *u2)
1356 if (!see_other_gids) {
1358 for (i = 0; i < u1->cr_ngroups; i++) {
1359 if (groupmember(u1->cr_groups[i], u2))
1365 if (priv_check_cred(u1, PRIV_SEEOTHERGIDS, 0) != 0)
1373 * Determine if u1 "can see" the subject specified by u2.
1374 * Returns: 0 for permitted, an errno value otherwise
1376 * References: *u1 and *u2 must not change during the call
1377 * u1 may equal u2, in which case only one reference is required
1380 cr_cansee(struct ucred *u1, struct ucred *u2)
1384 if ((error = prison_check(u1, u2)))
1387 if ((error = mac_cred_check_visible(u1, u2)))
1390 if ((error = cr_seeotheruids(u1, u2)))
1392 if ((error = cr_seeothergids(u1, u2)))
1398 * Determine if td "can see" the subject specified by p.
1399 * Returns: 0 for permitted, an errno value otherwise
1400 * Locks: Sufficient locks to protect p->p_ucred must be held. td really
1401 * should be curthread.
1402 * References: td and p must be valid for the lifetime of the call
1405 p_cansee(struct thread *td, struct proc *p)
1408 /* Wrap cr_cansee() for all functionality. */
1409 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1410 PROC_LOCK_ASSERT(p, MA_OWNED);
1411 return (cr_cansee(td->td_ucred, p->p_ucred));
1415 * 'conservative_signals' prevents the delivery of a broad class of
1416 * signals by unprivileged processes to processes that have changed their
1417 * credentials since the last invocation of execve(). This can prevent
1418 * the leakage of cached information or retained privileges as a result
1419 * of a common class of signal-related vulnerabilities. However, this
1420 * may interfere with some applications that expect to be able to
1421 * deliver these signals to peer processes after having given up
1424 static int conservative_signals = 1;
1425 SYSCTL_INT(_security_bsd, OID_AUTO, conservative_signals, CTLFLAG_RW,
1426 &conservative_signals, 0, "Unprivileged processes prevented from "
1427 "sending certain signals to processes whose credentials have changed");
1429 * Determine whether cred may deliver the specified signal to proc.
1430 * Returns: 0 for permitted, an errno value otherwise.
1431 * Locks: A lock must be held for proc.
1432 * References: cred and proc must be valid for the lifetime of the call.
1435 cr_cansignal(struct ucred *cred, struct proc *proc, int signum)
1439 PROC_LOCK_ASSERT(proc, MA_OWNED);
1441 * Jail semantics limit the scope of signalling to proc in the
1442 * same jail as cred, if cred is in jail.
1444 error = prison_check(cred, proc->p_ucred);
1448 if ((error = mac_proc_check_signal(cred, proc, signum)))
1451 if ((error = cr_seeotheruids(cred, proc->p_ucred)))
1453 if ((error = cr_seeothergids(cred, proc->p_ucred)))
1457 * UNIX signal semantics depend on the status of the P_SUGID
1458 * bit on the target process. If the bit is set, then additional
1459 * restrictions are placed on the set of available signals.
1461 if (conservative_signals && (proc->p_flag & P_SUGID)) {
1476 * Generally, permit job and terminal control
1481 /* Not permitted without privilege. */
1482 error = priv_check_cred(cred, PRIV_SIGNAL_SUGID, 0);
1489 * Generally, the target credential's ruid or svuid must match the
1490 * subject credential's ruid or euid.
1492 if (cred->cr_ruid != proc->p_ucred->cr_ruid &&
1493 cred->cr_ruid != proc->p_ucred->cr_svuid &&
1494 cred->cr_uid != proc->p_ucred->cr_ruid &&
1495 cred->cr_uid != proc->p_ucred->cr_svuid) {
1496 error = priv_check_cred(cred, PRIV_SIGNAL_DIFFCRED, 0);
1505 * Determine whether td may deliver the specified signal to p.
1506 * Returns: 0 for permitted, an errno value otherwise
1507 * Locks: Sufficient locks to protect various components of td and p
1508 * must be held. td must be curthread, and a lock must be
1510 * References: td and p must be valid for the lifetime of the call
1513 p_cansignal(struct thread *td, struct proc *p, int signum)
1516 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1517 PROC_LOCK_ASSERT(p, MA_OWNED);
1518 if (td->td_proc == p)
1522 * UNIX signalling semantics require that processes in the same
1523 * session always be able to deliver SIGCONT to one another,
1524 * overriding the remaining protections.
1526 /* XXX: This will require an additional lock of some sort. */
1527 if (signum == SIGCONT && td->td_proc->p_session == p->p_session)
1530 * Some compat layers use SIGTHR and higher signals for
1531 * communication between different kernel threads of the same
1532 * process, so that they expect that it's always possible to
1533 * deliver them, even for suid applications where cr_cansignal() can
1534 * deny such ability for security consideration. It should be
1535 * pretty safe to do since the only way to create two processes
1536 * with the same p_leader is via rfork(2).
1538 if (td->td_proc->p_leader != NULL && signum >= SIGTHR &&
1539 signum < SIGTHR + 4 && td->td_proc->p_leader == p->p_leader)
1542 return (cr_cansignal(td->td_ucred, p, signum));
1546 * Determine whether td may reschedule p.
1547 * Returns: 0 for permitted, an errno value otherwise
1548 * Locks: Sufficient locks to protect various components of td and p
1549 * must be held. td must be curthread, and a lock must
1551 * References: td and p must be valid for the lifetime of the call
1554 p_cansched(struct thread *td, struct proc *p)
1558 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1559 PROC_LOCK_ASSERT(p, MA_OWNED);
1560 if (td->td_proc == p)
1562 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1565 if ((error = mac_proc_check_sched(td->td_ucred, p)))
1568 if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1570 if ((error = cr_seeothergids(td->td_ucred, p->p_ucred)))
1572 if (td->td_ucred->cr_ruid != p->p_ucred->cr_ruid &&
1573 td->td_ucred->cr_uid != p->p_ucred->cr_ruid) {
1574 error = priv_check(td, PRIV_SCHED_DIFFCRED);
1582 * The 'unprivileged_proc_debug' flag may be used to disable a variety of
1583 * unprivileged inter-process debugging services, including some procfs
1584 * functionality, ptrace(), and ktrace(). In the past, inter-process
1585 * debugging has been involved in a variety of security problems, and sites
1586 * not requiring the service might choose to disable it when hardening
1589 * XXX: Should modifying and reading this variable require locking?
1590 * XXX: data declarations should be together near the beginning of the file.
1592 static int unprivileged_proc_debug = 1;
1593 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_proc_debug, CTLFLAG_RW,
1594 &unprivileged_proc_debug, 0,
1595 "Unprivileged processes may use process debugging facilities");
1598 * Determine whether td may debug p.
1599 * Returns: 0 for permitted, an errno value otherwise
1600 * Locks: Sufficient locks to protect various components of td and p
1601 * must be held. td must be curthread, and a lock must
1603 * References: td and p must be valid for the lifetime of the call
1606 p_candebug(struct thread *td, struct proc *p)
1608 int credentialchanged, error, grpsubset, i, uidsubset;
1610 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1611 PROC_LOCK_ASSERT(p, MA_OWNED);
1612 if (!unprivileged_proc_debug) {
1613 error = priv_check(td, PRIV_DEBUG_UNPRIV);
1617 if (td->td_proc == p)
1619 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1622 if ((error = mac_proc_check_debug(td->td_ucred, p)))
1625 if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1627 if ((error = cr_seeothergids(td->td_ucred, p->p_ucred)))
1631 * Is p's group set a subset of td's effective group set? This
1632 * includes p's egid, group access list, rgid, and svgid.
1635 for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
1636 if (!groupmember(p->p_ucred->cr_groups[i], td->td_ucred)) {
1641 grpsubset = grpsubset &&
1642 groupmember(p->p_ucred->cr_rgid, td->td_ucred) &&
1643 groupmember(p->p_ucred->cr_svgid, td->td_ucred);
1646 * Are the uids present in p's credential equal to td's
1647 * effective uid? This includes p's euid, svuid, and ruid.
1649 uidsubset = (td->td_ucred->cr_uid == p->p_ucred->cr_uid &&
1650 td->td_ucred->cr_uid == p->p_ucred->cr_svuid &&
1651 td->td_ucred->cr_uid == p->p_ucred->cr_ruid);
1654 * Has the credential of the process changed since the last exec()?
1656 credentialchanged = (p->p_flag & P_SUGID);
1659 * If p's gids aren't a subset, or the uids aren't a subset,
1660 * or the credential has changed, require appropriate privilege
1661 * for td to debug p.
1663 if (!grpsubset || !uidsubset) {
1664 error = priv_check(td, PRIV_DEBUG_DIFFCRED);
1669 if (credentialchanged) {
1670 error = priv_check(td, PRIV_DEBUG_SUGID);
1675 /* Can't trace init when securelevel > 0. */
1676 if (p == initproc) {
1677 error = securelevel_gt(td->td_ucred, 0);
1683 * Can't trace a process that's currently exec'ing.
1685 * XXX: Note, this is not a security policy decision, it's a
1686 * basic correctness/functionality decision. Therefore, this check
1687 * should be moved to the caller's of p_candebug().
1689 if ((p->p_flag & P_INEXEC) != 0)
1696 * Determine whether the subject represented by cred can "see" a socket.
1697 * Returns: 0 for permitted, ENOENT otherwise.
1700 cr_canseesocket(struct ucred *cred, struct socket *so)
1704 error = prison_check(cred, so->so_cred);
1708 error = mac_socket_check_visible(cred, so);
1712 if (cr_seeotheruids(cred, so->so_cred))
1714 if (cr_seeothergids(cred, so->so_cred))
1720 #if defined(INET) || defined(INET6)
1722 * Determine whether the subject represented by cred can "see" a socket.
1723 * Returns: 0 for permitted, ENOENT otherwise.
1726 cr_canseeinpcb(struct ucred *cred, struct inpcb *inp)
1730 error = prison_check(cred, inp->inp_cred);
1734 INP_LOCK_ASSERT(inp);
1735 error = mac_inpcb_check_visible(cred, inp);
1739 if (cr_seeotheruids(cred, inp->inp_cred))
1741 if (cr_seeothergids(cred, inp->inp_cred))
1749 * Determine whether td can wait for the exit of p.
1750 * Returns: 0 for permitted, an errno value otherwise
1751 * Locks: Sufficient locks to protect various components of td and p
1752 * must be held. td must be curthread, and a lock must
1754 * References: td and p must be valid for the lifetime of the call
1758 p_canwait(struct thread *td, struct proc *p)
1762 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1763 PROC_LOCK_ASSERT(p, MA_OWNED);
1764 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1767 if ((error = mac_proc_check_wait(td->td_ucred, p)))
1771 /* XXXMAC: This could have odd effects on some shells. */
1772 if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1780 * Allocate a zeroed cred structure.
1785 register struct ucred *cr;
1787 cr = malloc(sizeof(*cr), M_CRED, M_WAITOK | M_ZERO);
1788 refcount_init(&cr->cr_ref, 1);
1790 audit_cred_init(cr);
1795 crextend(cr, XU_NGROUPS);
1800 * Claim another reference to a ucred structure.
1803 crhold(struct ucred *cr)
1806 refcount_acquire(&cr->cr_ref);
1811 * Free a cred structure. Throws away space when ref count gets to 0.
1814 crfree(struct ucred *cr)
1817 KASSERT(cr->cr_ref > 0, ("bad ucred refcount: %d", cr->cr_ref));
1818 KASSERT(cr->cr_ref != 0xdeadc0de, ("dangling reference to ucred"));
1819 if (refcount_release(&cr->cr_ref)) {
1821 * Some callers of crget(), such as nfs_statfs(),
1822 * allocate a temporary credential, but don't
1823 * allocate a uidinfo structure.
1825 if (cr->cr_uidinfo != NULL)
1826 uifree(cr->cr_uidinfo);
1827 if (cr->cr_ruidinfo != NULL)
1828 uifree(cr->cr_ruidinfo);
1830 * Free a prison, if any.
1832 if (cr->cr_prison != NULL)
1833 prison_free(cr->cr_prison);
1835 audit_cred_destroy(cr);
1838 mac_cred_destroy(cr);
1840 free(cr->cr_groups, M_CRED);
1846 * Check to see if this ucred is shared.
1849 crshared(struct ucred *cr)
1852 return (cr->cr_ref > 1);
1856 * Copy a ucred's contents from a template. Does not block.
1859 crcopy(struct ucred *dest, struct ucred *src)
1862 KASSERT(crshared(dest) == 0, ("crcopy of shared ucred"));
1863 bcopy(&src->cr_startcopy, &dest->cr_startcopy,
1864 (unsigned)((caddr_t)&src->cr_endcopy -
1865 (caddr_t)&src->cr_startcopy));
1866 crsetgroups(dest, src->cr_ngroups, src->cr_groups);
1867 uihold(dest->cr_uidinfo);
1868 uihold(dest->cr_ruidinfo);
1869 prison_hold(dest->cr_prison);
1871 audit_cred_copy(src, dest);
1874 mac_cred_copy(src, dest);
1879 * Dup cred struct to a new held one.
1882 crdup(struct ucred *cr)
1884 struct ucred *newcr;
1892 * Fill in a struct xucred based on a struct ucred.
1895 cru2x(struct ucred *cr, struct xucred *xcr)
1899 bzero(xcr, sizeof(*xcr));
1900 xcr->cr_version = XUCRED_VERSION;
1901 xcr->cr_uid = cr->cr_uid;
1903 ngroups = MIN(cr->cr_ngroups, XU_NGROUPS);
1904 xcr->cr_ngroups = ngroups;
1905 bcopy(cr->cr_groups, xcr->cr_groups,
1906 ngroups * sizeof(*cr->cr_groups));
1910 * small routine to swap a thread's current ucred for the correct one taken
1914 cred_update_thread(struct thread *td)
1920 cred = td->td_ucred;
1922 td->td_ucred = crhold(p->p_ucred);
1929 crcopysafe(struct proc *p, struct ucred *cr)
1931 struct ucred *oldcred;
1934 PROC_LOCK_ASSERT(p, MA_OWNED);
1936 oldcred = p->p_ucred;
1937 while (cr->cr_agroups < oldcred->cr_agroups) {
1938 groups = oldcred->cr_agroups;
1940 crextend(cr, groups);
1942 oldcred = p->p_ucred;
1944 crcopy(cr, oldcred);
1950 * Extend the passed in credential to hold n items.
1953 crextend(struct ucred *cr, int n)
1958 if (n <= cr->cr_agroups)
1962 * We extend by 2 each time since we're using a power of two
1963 * allocator until we need enough groups to fill a page.
1964 * Once we're allocating multiple pages, only allocate as many
1965 * as we actually need. The case of processes needing a
1966 * non-power of two number of pages seems more likely than
1967 * a real world process that adds thousands of groups one at a
1970 if ( n < PAGE_SIZE / sizeof(gid_t) ) {
1971 if (cr->cr_agroups == 0)
1972 cnt = MINALLOCSIZE / sizeof(gid_t);
1974 cnt = cr->cr_agroups * 2;
1979 cnt = roundup2(n, PAGE_SIZE / sizeof(gid_t));
1981 /* Free the old array. */
1983 free(cr->cr_groups, M_CRED);
1985 cr->cr_groups = malloc(cnt * sizeof(gid_t), M_CRED, M_WAITOK | M_ZERO);
1986 cr->cr_agroups = cnt;
1990 * Copy groups in to a credential, preserving any necessary invariants.
1991 * Currently this includes the sorting of all supplemental gids.
1992 * crextend() must have been called before hand to ensure sufficient
1993 * space is available.
1996 crsetgroups_locked(struct ucred *cr, int ngrp, gid_t *groups)
2002 KASSERT(cr->cr_agroups >= ngrp, ("cr_ngroups is too small"));
2004 bcopy(groups, cr->cr_groups, ngrp * sizeof(gid_t));
2005 cr->cr_ngroups = ngrp;
2008 * Sort all groups except cr_groups[0] to allow groupmember to
2009 * perform a binary search.
2011 * XXX: If large numbers of groups become common this should
2012 * be replaced with shell sort like linux uses or possibly
2015 for (i = 2; i < ngrp; i++) {
2016 g = cr->cr_groups[i];
2017 for (j = i-1; j >= 1 && g < cr->cr_groups[j]; j--)
2018 cr->cr_groups[j + 1] = cr->cr_groups[j];
2019 cr->cr_groups[j + 1] = g;
2024 * Copy groups in to a credential after expanding it if required.
2025 * Truncate the list to NGROUPS if it is too large.
2028 crsetgroups(struct ucred *cr, int ngrp, gid_t *groups)
2035 crsetgroups_locked(cr, ngrp, groups);
2039 * Get login name, if available.
2041 #ifndef _SYS_SYSPROTO_H_
2042 struct getlogin_args {
2049 getlogin(struct thread *td, struct getlogin_args *uap)
2052 char login[MAXLOGNAME];
2053 struct proc *p = td->td_proc;
2055 if (uap->namelen > MAXLOGNAME)
2056 uap->namelen = MAXLOGNAME;
2058 SESS_LOCK(p->p_session);
2059 bcopy(p->p_session->s_login, login, uap->namelen);
2060 SESS_UNLOCK(p->p_session);
2062 error = copyout(login, uap->namebuf, uap->namelen);
2069 #ifndef _SYS_SYSPROTO_H_
2070 struct setlogin_args {
2076 setlogin(struct thread *td, struct setlogin_args *uap)
2078 struct proc *p = td->td_proc;
2080 char logintmp[MAXLOGNAME];
2082 error = priv_check(td, PRIV_PROC_SETLOGIN);
2085 error = copyinstr(uap->namebuf, logintmp, sizeof(logintmp), NULL);
2086 if (error == ENAMETOOLONG)
2090 SESS_LOCK(p->p_session);
2091 (void) memcpy(p->p_session->s_login, logintmp,
2093 SESS_UNLOCK(p->p_session);
2100 setsugid(struct proc *p)
2103 PROC_LOCK_ASSERT(p, MA_OWNED);
2104 p->p_flag |= P_SUGID;
2105 if (!(p->p_pfsflags & PF_ISUGID))
2110 * Change a process's effective uid.
2111 * Side effects: newcred->cr_uid and newcred->cr_uidinfo will be modified.
2112 * References: newcred must be an exclusive credential reference for the
2113 * duration of the call.
2116 change_euid(struct ucred *newcred, struct uidinfo *euip)
2119 newcred->cr_uid = euip->ui_uid;
2121 uifree(newcred->cr_uidinfo);
2122 newcred->cr_uidinfo = euip;
2126 * Change a process's effective gid.
2127 * Side effects: newcred->cr_gid will be modified.
2128 * References: newcred must be an exclusive credential reference for the
2129 * duration of the call.
2132 change_egid(struct ucred *newcred, gid_t egid)
2135 newcred->cr_groups[0] = egid;
2139 * Change a process's real uid.
2140 * Side effects: newcred->cr_ruid will be updated, newcred->cr_ruidinfo
2141 * will be updated, and the old and new cr_ruidinfo proc
2142 * counts will be updated.
2143 * References: newcred must be an exclusive credential reference for the
2144 * duration of the call.
2147 change_ruid(struct ucred *newcred, struct uidinfo *ruip)
2150 (void)chgproccnt(newcred->cr_ruidinfo, -1, 0);
2151 newcred->cr_ruid = ruip->ui_uid;
2153 uifree(newcred->cr_ruidinfo);
2154 newcred->cr_ruidinfo = ruip;
2155 (void)chgproccnt(newcred->cr_ruidinfo, 1, 0);
2159 * Change a process's real gid.
2160 * Side effects: newcred->cr_rgid will be updated.
2161 * References: newcred must be an exclusive credential reference for the
2162 * duration of the call.
2165 change_rgid(struct ucred *newcred, gid_t rgid)
2168 newcred->cr_rgid = rgid;
2172 * Change a process's saved uid.
2173 * Side effects: newcred->cr_svuid will be updated.
2174 * References: newcred must be an exclusive credential reference for the
2175 * duration of the call.
2178 change_svuid(struct ucred *newcred, uid_t svuid)
2181 newcred->cr_svuid = svuid;
2185 * Change a process's saved gid.
2186 * Side effects: newcred->cr_svgid will be updated.
2187 * References: newcred must be an exclusive credential reference for the
2188 * duration of the call.
2191 change_svgid(struct ucred *newcred, gid_t svgid)
2194 newcred->cr_svgid = svgid;