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 if (uap->gidsetsize < td->td_ucred->cr_ngroups) {
287 if (uap->gidsetsize == 0)
292 ngrp = td->td_ucred->cr_ngroups;
293 groups = malloc(ngrp * sizeof(*groups), M_TEMP, M_WAITOK);
294 error = kern_getgroups(td, &ngrp, groups);
297 if (uap->gidsetsize > 0)
298 error = copyout(groups, uap->gidset, ngrp * sizeof(gid_t));
300 td->td_retval[0] = ngrp;
302 free(groups, M_TEMP);
307 kern_getgroups(struct thread *td, u_int *ngrp, gid_t *groups)
313 *ngrp = cred->cr_ngroups;
316 if (*ngrp < cred->cr_ngroups)
318 *ngrp = cred->cr_ngroups;
319 bcopy(cred->cr_groups, groups, *ngrp * sizeof(gid_t));
323 #ifndef _SYS_SYSPROTO_H_
330 setsid(register struct thread *td, struct setsid_args *uap)
334 struct proc *p = td->td_proc;
335 struct pgrp *newpgrp;
336 struct session *newsess;
341 newpgrp = malloc(sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
342 newsess = malloc(sizeof(struct session), M_SESSION, M_WAITOK | M_ZERO);
344 sx_xlock(&proctree_lock);
346 if (p->p_pgid == p->p_pid || (pgrp = pgfind(p->p_pid)) != NULL) {
351 (void)enterpgrp(p, p->p_pid, newpgrp, newsess);
352 td->td_retval[0] = p->p_pid;
357 sx_xunlock(&proctree_lock);
360 free(newpgrp, M_PGRP);
362 free(newsess, M_SESSION);
368 * set process group (setpgid/old setpgrp)
370 * caller does setpgid(targpid, targpgid)
372 * pid must be caller or child of caller (ESRCH)
374 * pid must be in same session (EPERM)
375 * pid can't have done an exec (EACCES)
377 * there must exist some pid in same session having pgid (EPERM)
378 * pid must not be session leader (EPERM)
380 #ifndef _SYS_SYSPROTO_H_
381 struct setpgid_args {
382 int pid; /* target process id */
383 int pgid; /* target pgrp id */
388 setpgid(struct thread *td, register struct setpgid_args *uap)
390 struct proc *curp = td->td_proc;
391 register struct proc *targp; /* target process */
392 register struct pgrp *pgrp; /* target pgrp */
394 struct pgrp *newpgrp;
401 newpgrp = malloc(sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
403 sx_xlock(&proctree_lock);
404 if (uap->pid != 0 && uap->pid != curp->p_pid) {
405 if ((targp = pfind(uap->pid)) == NULL) {
409 if (!inferior(targp)) {
414 if ((error = p_cansee(td, targp))) {
418 if (targp->p_pgrp == NULL ||
419 targp->p_session != curp->p_session) {
424 if (targp->p_flag & P_EXEC) {
432 if (SESS_LEADER(targp)) {
437 uap->pgid = targp->p_pid;
438 if ((pgrp = pgfind(uap->pgid)) == NULL) {
439 if (uap->pgid == targp->p_pid) {
440 error = enterpgrp(targp, uap->pgid, newpgrp,
447 if (pgrp == targp->p_pgrp) {
451 if (pgrp->pg_id != targp->p_pid &&
452 pgrp->pg_session != curp->p_session) {
458 error = enterthispgrp(targp, pgrp);
461 sx_xunlock(&proctree_lock);
462 KASSERT((error == 0) || (newpgrp != NULL),
463 ("setpgid failed and newpgrp is NULL"));
465 free(newpgrp, M_PGRP);
470 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
471 * compatible. It says that setting the uid/gid to euid/egid is a special
472 * case of "appropriate privilege". Once the rules are expanded out, this
473 * basically means that setuid(nnn) sets all three id's, in all permitted
474 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
475 * does not set the saved id - this is dangerous for traditional BSD
476 * programs. For this reason, we *really* do not want to set
477 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
479 #define POSIX_APPENDIX_B_4_2_2
481 #ifndef _SYS_SYSPROTO_H_
488 setuid(struct thread *td, struct setuid_args *uap)
490 struct proc *p = td->td_proc;
491 struct ucred *newcred, *oldcred;
502 * Copy credentials so other references do not see our changes.
504 oldcred = crcopysafe(p, newcred);
507 error = mac_cred_check_setuid(oldcred, uid);
513 * See if we have "permission" by POSIX 1003.1 rules.
515 * Note that setuid(geteuid()) is a special case of
516 * "appropriate privileges" in appendix B.4.2.2. We need
517 * to use this clause to be compatible with traditional BSD
518 * semantics. Basically, it means that "setuid(xx)" sets all
519 * three id's (assuming you have privs).
521 * Notes on the logic. We do things in three steps.
522 * 1: We determine if the euid is going to change, and do EPERM
523 * right away. We unconditionally change the euid later if this
524 * test is satisfied, simplifying that part of the logic.
525 * 2: We determine if the real and/or saved uids are going to
526 * change. Determined by compile options.
527 * 3: Change euid last. (after tests in #2 for "appropriate privs")
529 if (uid != oldcred->cr_ruid && /* allow setuid(getuid()) */
530 #ifdef _POSIX_SAVED_IDS
531 uid != oldcred->cr_svuid && /* allow setuid(saved gid) */
533 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
534 uid != oldcred->cr_uid && /* allow setuid(geteuid()) */
536 (error = priv_check_cred(oldcred, PRIV_CRED_SETUID, 0)) != 0)
539 #ifdef _POSIX_SAVED_IDS
541 * Do we have "appropriate privileges" (are we root or uid == euid)
542 * If so, we are changing the real uid and/or saved uid.
545 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
546 uid == oldcred->cr_uid ||
548 /* We are using privs. */
549 priv_check_cred(oldcred, PRIV_CRED_SETUID, 0) == 0)
553 * Set the real uid and transfer proc count to new user.
555 if (uid != oldcred->cr_ruid) {
556 change_ruid(newcred, uip);
562 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
563 * the security of seteuid() depends on it. B.4.2.2 says it
564 * is important that we should do this.
566 if (uid != oldcred->cr_svuid) {
567 change_svuid(newcred, uid);
573 * In all permitted cases, we are changing the euid.
575 if (uid != oldcred->cr_uid) {
576 change_euid(newcred, uip);
579 p->p_ucred = newcred;
592 #ifndef _SYS_SYSPROTO_H_
593 struct seteuid_args {
599 seteuid(struct thread *td, struct seteuid_args *uap)
601 struct proc *p = td->td_proc;
602 struct ucred *newcred, *oldcred;
604 struct uidinfo *euip;
608 AUDIT_ARG_EUID(euid);
613 * Copy credentials so other references do not see our changes.
615 oldcred = crcopysafe(p, newcred);
618 error = mac_cred_check_seteuid(oldcred, euid);
623 if (euid != oldcred->cr_ruid && /* allow seteuid(getuid()) */
624 euid != oldcred->cr_svuid && /* allow seteuid(saved uid) */
625 (error = priv_check_cred(oldcred, PRIV_CRED_SETEUID, 0)) != 0)
629 * Everything's okay, do it.
631 if (oldcred->cr_uid != euid) {
632 change_euid(newcred, euip);
635 p->p_ucred = newcred;
648 #ifndef _SYS_SYSPROTO_H_
655 setgid(struct thread *td, struct setgid_args *uap)
657 struct proc *p = td->td_proc;
658 struct ucred *newcred, *oldcred;
666 oldcred = crcopysafe(p, newcred);
669 error = mac_cred_check_setgid(oldcred, gid);
675 * See if we have "permission" by POSIX 1003.1 rules.
677 * Note that setgid(getegid()) is a special case of
678 * "appropriate privileges" in appendix B.4.2.2. We need
679 * to use this clause to be compatible with traditional BSD
680 * semantics. Basically, it means that "setgid(xx)" sets all
681 * three id's (assuming you have privs).
683 * For notes on the logic here, see setuid() above.
685 if (gid != oldcred->cr_rgid && /* allow setgid(getgid()) */
686 #ifdef _POSIX_SAVED_IDS
687 gid != oldcred->cr_svgid && /* allow setgid(saved gid) */
689 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
690 gid != oldcred->cr_groups[0] && /* allow setgid(getegid()) */
692 (error = priv_check_cred(oldcred, PRIV_CRED_SETGID, 0)) != 0)
695 #ifdef _POSIX_SAVED_IDS
697 * Do we have "appropriate privileges" (are we root or gid == egid)
698 * If so, we are changing the real uid and saved gid.
701 #ifdef POSIX_APPENDIX_B_4_2_2 /* use the clause from B.4.2.2 */
702 gid == oldcred->cr_groups[0] ||
704 /* We are using privs. */
705 priv_check_cred(oldcred, PRIV_CRED_SETGID, 0) == 0)
711 if (oldcred->cr_rgid != gid) {
712 change_rgid(newcred, gid);
718 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
719 * the security of setegid() depends on it. B.4.2.2 says it
720 * is important that we should do this.
722 if (oldcred->cr_svgid != gid) {
723 change_svgid(newcred, gid);
728 * In all cases permitted cases, we are changing the egid.
729 * Copy credentials so other references do not see our changes.
731 if (oldcred->cr_groups[0] != gid) {
732 change_egid(newcred, gid);
735 p->p_ucred = newcred;
746 #ifndef _SYS_SYSPROTO_H_
747 struct setegid_args {
753 setegid(struct thread *td, struct setegid_args *uap)
755 struct proc *p = td->td_proc;
756 struct ucred *newcred, *oldcred;
761 AUDIT_ARG_EGID(egid);
764 oldcred = crcopysafe(p, newcred);
767 error = mac_cred_check_setegid(oldcred, egid);
772 if (egid != oldcred->cr_rgid && /* allow setegid(getgid()) */
773 egid != oldcred->cr_svgid && /* allow setegid(saved gid) */
774 (error = priv_check_cred(oldcred, PRIV_CRED_SETEGID, 0)) != 0)
777 if (oldcred->cr_groups[0] != egid) {
778 change_egid(newcred, egid);
781 p->p_ucred = newcred;
792 #ifndef _SYS_SYSPROTO_H_
793 struct setgroups_args {
800 setgroups(struct thread *td, struct setgroups_args *uap)
802 gid_t *groups = NULL;
805 if (uap->gidsetsize > ngroups_max + 1)
807 groups = malloc(uap->gidsetsize * sizeof(gid_t), M_TEMP, M_WAITOK);
808 error = copyin(uap->gidset, groups, uap->gidsetsize * sizeof(gid_t));
811 error = kern_setgroups(td, uap->gidsetsize, groups);
813 free(groups, M_TEMP);
818 kern_setgroups(struct thread *td, u_int ngrp, gid_t *groups)
820 struct proc *p = td->td_proc;
821 struct ucred *newcred, *oldcred;
824 if (ngrp > ngroups_max + 1)
826 AUDIT_ARG_GROUPSET(groups, ngrp);
828 crextend(newcred, ngrp);
830 oldcred = crcopysafe(p, newcred);
833 error = mac_cred_check_setgroups(oldcred, ngrp, groups);
838 error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0);
844 * setgroups(0, NULL) is a legitimate way of clearing the
845 * groups vector on non-BSD systems (which generally do not
846 * have the egid in the groups[0]). We risk security holes
847 * when running non-BSD software if we do not do the same.
849 newcred->cr_ngroups = 1;
851 crsetgroups_locked(newcred, ngrp, groups);
854 p->p_ucred = newcred;
865 #ifndef _SYS_SYSPROTO_H_
866 struct setreuid_args {
873 setreuid(register struct thread *td, struct setreuid_args *uap)
875 struct proc *p = td->td_proc;
876 struct ucred *newcred, *oldcred;
878 struct uidinfo *euip, *ruip;
883 AUDIT_ARG_EUID(euid);
884 AUDIT_ARG_RUID(ruid);
889 oldcred = crcopysafe(p, newcred);
892 error = mac_cred_check_setreuid(oldcred, ruid, euid);
897 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
898 ruid != oldcred->cr_svuid) ||
899 (euid != (uid_t)-1 && euid != oldcred->cr_uid &&
900 euid != oldcred->cr_ruid && euid != oldcred->cr_svuid)) &&
901 (error = priv_check_cred(oldcred, PRIV_CRED_SETREUID, 0)) != 0)
904 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
905 change_euid(newcred, euip);
908 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
909 change_ruid(newcred, ruip);
912 if ((ruid != (uid_t)-1 || newcred->cr_uid != newcred->cr_ruid) &&
913 newcred->cr_svuid != newcred->cr_uid) {
914 change_svuid(newcred, newcred->cr_uid);
917 p->p_ucred = newcred;
932 #ifndef _SYS_SYSPROTO_H_
933 struct setregid_args {
940 setregid(register struct thread *td, struct setregid_args *uap)
942 struct proc *p = td->td_proc;
943 struct ucred *newcred, *oldcred;
949 AUDIT_ARG_EGID(egid);
950 AUDIT_ARG_RGID(rgid);
953 oldcred = crcopysafe(p, newcred);
956 error = mac_cred_check_setregid(oldcred, rgid, egid);
961 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
962 rgid != oldcred->cr_svgid) ||
963 (egid != (gid_t)-1 && egid != oldcred->cr_groups[0] &&
964 egid != oldcred->cr_rgid && egid != oldcred->cr_svgid)) &&
965 (error = priv_check_cred(oldcred, PRIV_CRED_SETREGID, 0)) != 0)
968 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
969 change_egid(newcred, egid);
972 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
973 change_rgid(newcred, rgid);
976 if ((rgid != (gid_t)-1 || newcred->cr_groups[0] != newcred->cr_rgid) &&
977 newcred->cr_svgid != newcred->cr_groups[0]) {
978 change_svgid(newcred, newcred->cr_groups[0]);
981 p->p_ucred = newcred;
993 * setresuid(ruid, euid, suid) is like setreuid except control over the saved
996 #ifndef _SYS_SYSPROTO_H_
997 struct setresuid_args {
1005 setresuid(register struct thread *td, struct setresuid_args *uap)
1007 struct proc *p = td->td_proc;
1008 struct ucred *newcred, *oldcred;
1009 uid_t euid, ruid, suid;
1010 struct uidinfo *euip, *ruip;
1016 AUDIT_ARG_EUID(euid);
1017 AUDIT_ARG_RUID(ruid);
1018 AUDIT_ARG_SUID(suid);
1020 euip = uifind(euid);
1021 ruip = uifind(ruid);
1023 oldcred = crcopysafe(p, newcred);
1026 error = mac_cred_check_setresuid(oldcred, ruid, euid, suid);
1031 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
1032 ruid != oldcred->cr_svuid &&
1033 ruid != oldcred->cr_uid) ||
1034 (euid != (uid_t)-1 && euid != oldcred->cr_ruid &&
1035 euid != oldcred->cr_svuid &&
1036 euid != oldcred->cr_uid) ||
1037 (suid != (uid_t)-1 && suid != oldcred->cr_ruid &&
1038 suid != oldcred->cr_svuid &&
1039 suid != oldcred->cr_uid)) &&
1040 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESUID, 0)) != 0)
1043 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
1044 change_euid(newcred, euip);
1047 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
1048 change_ruid(newcred, ruip);
1051 if (suid != (uid_t)-1 && oldcred->cr_svuid != suid) {
1052 change_svuid(newcred, suid);
1055 p->p_ucred = newcred;
1072 * setresgid(rgid, egid, sgid) is like setregid except control over the saved
1075 #ifndef _SYS_SYSPROTO_H_
1076 struct setresgid_args {
1084 setresgid(register struct thread *td, struct setresgid_args *uap)
1086 struct proc *p = td->td_proc;
1087 struct ucred *newcred, *oldcred;
1088 gid_t egid, rgid, sgid;
1094 AUDIT_ARG_EGID(egid);
1095 AUDIT_ARG_RGID(rgid);
1096 AUDIT_ARG_SGID(sgid);
1099 oldcred = crcopysafe(p, newcred);
1102 error = mac_cred_check_setresgid(oldcred, rgid, egid, sgid);
1107 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
1108 rgid != oldcred->cr_svgid &&
1109 rgid != oldcred->cr_groups[0]) ||
1110 (egid != (gid_t)-1 && egid != oldcred->cr_rgid &&
1111 egid != oldcred->cr_svgid &&
1112 egid != oldcred->cr_groups[0]) ||
1113 (sgid != (gid_t)-1 && sgid != oldcred->cr_rgid &&
1114 sgid != oldcred->cr_svgid &&
1115 sgid != oldcred->cr_groups[0])) &&
1116 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESGID, 0)) != 0)
1119 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
1120 change_egid(newcred, egid);
1123 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
1124 change_rgid(newcred, rgid);
1127 if (sgid != (gid_t)-1 && oldcred->cr_svgid != sgid) {
1128 change_svgid(newcred, sgid);
1131 p->p_ucred = newcred;
1142 #ifndef _SYS_SYSPROTO_H_
1143 struct getresuid_args {
1151 getresuid(register struct thread *td, struct getresuid_args *uap)
1154 int error1 = 0, error2 = 0, error3 = 0;
1156 cred = td->td_ucred;
1158 error1 = copyout(&cred->cr_ruid,
1159 uap->ruid, sizeof(cred->cr_ruid));
1161 error2 = copyout(&cred->cr_uid,
1162 uap->euid, sizeof(cred->cr_uid));
1164 error3 = copyout(&cred->cr_svuid,
1165 uap->suid, sizeof(cred->cr_svuid));
1166 return (error1 ? error1 : error2 ? error2 : error3);
1169 #ifndef _SYS_SYSPROTO_H_
1170 struct getresgid_args {
1178 getresgid(register struct thread *td, struct getresgid_args *uap)
1181 int error1 = 0, error2 = 0, error3 = 0;
1183 cred = td->td_ucred;
1185 error1 = copyout(&cred->cr_rgid,
1186 uap->rgid, sizeof(cred->cr_rgid));
1188 error2 = copyout(&cred->cr_groups[0],
1189 uap->egid, sizeof(cred->cr_groups[0]));
1191 error3 = copyout(&cred->cr_svgid,
1192 uap->sgid, sizeof(cred->cr_svgid));
1193 return (error1 ? error1 : error2 ? error2 : error3);
1196 #ifndef _SYS_SYSPROTO_H_
1197 struct issetugid_args {
1203 issetugid(register struct thread *td, struct issetugid_args *uap)
1205 struct proc *p = td->td_proc;
1208 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
1209 * we use P_SUGID because we consider changing the owners as
1210 * "tainting" as well.
1211 * This is significant for procs that start as root and "become"
1212 * a user without an exec - programs cannot know *everything*
1213 * that libc *might* have put in their data segment.
1216 td->td_retval[0] = (p->p_flag & P_SUGID) ? 1 : 0;
1222 __setugid(struct thread *td, struct __setugid_args *uap)
1228 switch (uap->flag) {
1231 p->p_flag &= ~P_SUGID;
1236 p->p_flag |= P_SUGID;
1242 #else /* !REGRESSION */
1245 #endif /* REGRESSION */
1249 * Check if gid is a member of the group set.
1252 groupmember(gid_t gid, struct ucred *cred)
1258 if (cred->cr_groups[0] == gid)
1262 * If gid was not our primary group, perform a binary search
1263 * of the supplemental groups. This is possible because we
1264 * sort the groups in crsetgroups().
1267 h = cred->cr_ngroups;
1269 m = l + ((h - l) / 2);
1270 if (cred->cr_groups[m] < gid)
1275 if ((l < cred->cr_ngroups) && (cred->cr_groups[l] == gid))
1282 * Test the active securelevel against a given level. securelevel_gt()
1283 * implements (securelevel > level). securelevel_ge() implements
1284 * (securelevel >= level). Note that the logic is inverted -- these
1285 * functions return EPERM on "success" and 0 on "failure".
1287 * Due to care taken when setting the securelevel, we know that no jail will
1288 * be less secure that its parent (or the physical system), so it is sufficient
1289 * to test the current jail only.
1291 * XXXRW: Possibly since this has to do with privilege, it should move to
1295 securelevel_gt(struct ucred *cr, int level)
1298 return (cr->cr_prison->pr_securelevel > level ? EPERM : 0);
1302 securelevel_ge(struct ucred *cr, int level)
1305 return (cr->cr_prison->pr_securelevel >= level ? EPERM : 0);
1309 * 'see_other_uids' determines whether or not visibility of processes
1310 * and sockets with credentials holding different real uids is possible
1311 * using a variety of system MIBs.
1312 * XXX: data declarations should be together near the beginning of the file.
1314 static int see_other_uids = 1;
1315 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_uids, CTLFLAG_RW,
1317 "Unprivileged processes may see subjects/objects with different real uid");
1320 * Determine if u1 "can see" the subject specified by u2, according to the
1321 * 'see_other_uids' policy.
1322 * Returns: 0 for permitted, ESRCH otherwise
1324 * References: *u1 and *u2 must not change during the call
1325 * u1 may equal u2, in which case only one reference is required
1328 cr_seeotheruids(struct ucred *u1, struct ucred *u2)
1331 if (!see_other_uids && u1->cr_ruid != u2->cr_ruid) {
1332 if (priv_check_cred(u1, PRIV_SEEOTHERUIDS, 0) != 0)
1339 * 'see_other_gids' determines whether or not visibility of processes
1340 * and sockets with credentials holding different real gids is possible
1341 * using a variety of system MIBs.
1342 * XXX: data declarations should be together near the beginning of the file.
1344 static int see_other_gids = 1;
1345 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_gids, CTLFLAG_RW,
1347 "Unprivileged processes may see subjects/objects with different real gid");
1350 * Determine if u1 can "see" the subject specified by u2, according to the
1351 * 'see_other_gids' policy.
1352 * Returns: 0 for permitted, ESRCH otherwise
1354 * References: *u1 and *u2 must not change during the call
1355 * u1 may equal u2, in which case only one reference is required
1358 cr_seeothergids(struct ucred *u1, struct ucred *u2)
1362 if (!see_other_gids) {
1364 for (i = 0; i < u1->cr_ngroups; i++) {
1365 if (groupmember(u1->cr_groups[i], u2))
1371 if (priv_check_cred(u1, PRIV_SEEOTHERGIDS, 0) != 0)
1379 * Determine if u1 "can see" the subject specified by u2.
1380 * Returns: 0 for permitted, an errno value otherwise
1382 * References: *u1 and *u2 must not change during the call
1383 * u1 may equal u2, in which case only one reference is required
1386 cr_cansee(struct ucred *u1, struct ucred *u2)
1390 if ((error = prison_check(u1, u2)))
1393 if ((error = mac_cred_check_visible(u1, u2)))
1396 if ((error = cr_seeotheruids(u1, u2)))
1398 if ((error = cr_seeothergids(u1, u2)))
1404 * Determine if td "can see" the subject specified by p.
1405 * Returns: 0 for permitted, an errno value otherwise
1406 * Locks: Sufficient locks to protect p->p_ucred must be held. td really
1407 * should be curthread.
1408 * References: td and p must be valid for the lifetime of the call
1411 p_cansee(struct thread *td, struct proc *p)
1414 /* Wrap cr_cansee() for all functionality. */
1415 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1416 PROC_LOCK_ASSERT(p, MA_OWNED);
1417 return (cr_cansee(td->td_ucred, p->p_ucred));
1421 * 'conservative_signals' prevents the delivery of a broad class of
1422 * signals by unprivileged processes to processes that have changed their
1423 * credentials since the last invocation of execve(). This can prevent
1424 * the leakage of cached information or retained privileges as a result
1425 * of a common class of signal-related vulnerabilities. However, this
1426 * may interfere with some applications that expect to be able to
1427 * deliver these signals to peer processes after having given up
1430 static int conservative_signals = 1;
1431 SYSCTL_INT(_security_bsd, OID_AUTO, conservative_signals, CTLFLAG_RW,
1432 &conservative_signals, 0, "Unprivileged processes prevented from "
1433 "sending certain signals to processes whose credentials have changed");
1435 * Determine whether cred may deliver the specified signal to proc.
1436 * Returns: 0 for permitted, an errno value otherwise.
1437 * Locks: A lock must be held for proc.
1438 * References: cred and proc must be valid for the lifetime of the call.
1441 cr_cansignal(struct ucred *cred, struct proc *proc, int signum)
1445 PROC_LOCK_ASSERT(proc, MA_OWNED);
1447 * Jail semantics limit the scope of signalling to proc in the
1448 * same jail as cred, if cred is in jail.
1450 error = prison_check(cred, proc->p_ucred);
1454 if ((error = mac_proc_check_signal(cred, proc, signum)))
1457 if ((error = cr_seeotheruids(cred, proc->p_ucred)))
1459 if ((error = cr_seeothergids(cred, proc->p_ucred)))
1463 * UNIX signal semantics depend on the status of the P_SUGID
1464 * bit on the target process. If the bit is set, then additional
1465 * restrictions are placed on the set of available signals.
1467 if (conservative_signals && (proc->p_flag & P_SUGID)) {
1482 * Generally, permit job and terminal control
1487 /* Not permitted without privilege. */
1488 error = priv_check_cred(cred, PRIV_SIGNAL_SUGID, 0);
1495 * Generally, the target credential's ruid or svuid must match the
1496 * subject credential's ruid or euid.
1498 if (cred->cr_ruid != proc->p_ucred->cr_ruid &&
1499 cred->cr_ruid != proc->p_ucred->cr_svuid &&
1500 cred->cr_uid != proc->p_ucred->cr_ruid &&
1501 cred->cr_uid != proc->p_ucred->cr_svuid) {
1502 error = priv_check_cred(cred, PRIV_SIGNAL_DIFFCRED, 0);
1511 * Determine whether td may deliver the specified signal to p.
1512 * Returns: 0 for permitted, an errno value otherwise
1513 * Locks: Sufficient locks to protect various components of td and p
1514 * must be held. td must be curthread, and a lock must be
1516 * References: td and p must be valid for the lifetime of the call
1519 p_cansignal(struct thread *td, struct proc *p, int signum)
1522 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1523 PROC_LOCK_ASSERT(p, MA_OWNED);
1524 if (td->td_proc == p)
1528 * UNIX signalling semantics require that processes in the same
1529 * session always be able to deliver SIGCONT to one another,
1530 * overriding the remaining protections.
1532 /* XXX: This will require an additional lock of some sort. */
1533 if (signum == SIGCONT && td->td_proc->p_session == p->p_session)
1536 * Some compat layers use SIGTHR and higher signals for
1537 * communication between different kernel threads of the same
1538 * process, so that they expect that it's always possible to
1539 * deliver them, even for suid applications where cr_cansignal() can
1540 * deny such ability for security consideration. It should be
1541 * pretty safe to do since the only way to create two processes
1542 * with the same p_leader is via rfork(2).
1544 if (td->td_proc->p_leader != NULL && signum >= SIGTHR &&
1545 signum < SIGTHR + 4 && td->td_proc->p_leader == p->p_leader)
1548 return (cr_cansignal(td->td_ucred, p, signum));
1552 * Determine whether td may reschedule p.
1553 * Returns: 0 for permitted, an errno value otherwise
1554 * Locks: Sufficient locks to protect various components of td and p
1555 * must be held. td must be curthread, and a lock must
1557 * References: td and p must be valid for the lifetime of the call
1560 p_cansched(struct thread *td, struct proc *p)
1564 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1565 PROC_LOCK_ASSERT(p, MA_OWNED);
1566 if (td->td_proc == p)
1568 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1571 if ((error = mac_proc_check_sched(td->td_ucred, p)))
1574 if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1576 if ((error = cr_seeothergids(td->td_ucred, p->p_ucred)))
1578 if (td->td_ucred->cr_ruid != p->p_ucred->cr_ruid &&
1579 td->td_ucred->cr_uid != p->p_ucred->cr_ruid) {
1580 error = priv_check(td, PRIV_SCHED_DIFFCRED);
1588 * The 'unprivileged_proc_debug' flag may be used to disable a variety of
1589 * unprivileged inter-process debugging services, including some procfs
1590 * functionality, ptrace(), and ktrace(). In the past, inter-process
1591 * debugging has been involved in a variety of security problems, and sites
1592 * not requiring the service might choose to disable it when hardening
1595 * XXX: Should modifying and reading this variable require locking?
1596 * XXX: data declarations should be together near the beginning of the file.
1598 static int unprivileged_proc_debug = 1;
1599 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_proc_debug, CTLFLAG_RW,
1600 &unprivileged_proc_debug, 0,
1601 "Unprivileged processes may use process debugging facilities");
1604 * Determine whether td may debug p.
1605 * Returns: 0 for permitted, an errno value otherwise
1606 * Locks: Sufficient locks to protect various components of td and p
1607 * must be held. td must be curthread, and a lock must
1609 * References: td and p must be valid for the lifetime of the call
1612 p_candebug(struct thread *td, struct proc *p)
1614 int credentialchanged, error, grpsubset, i, uidsubset;
1616 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1617 PROC_LOCK_ASSERT(p, MA_OWNED);
1618 if (!unprivileged_proc_debug) {
1619 error = priv_check(td, PRIV_DEBUG_UNPRIV);
1623 if (td->td_proc == p)
1625 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1628 if ((error = mac_proc_check_debug(td->td_ucred, p)))
1631 if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1633 if ((error = cr_seeothergids(td->td_ucred, p->p_ucred)))
1637 * Is p's group set a subset of td's effective group set? This
1638 * includes p's egid, group access list, rgid, and svgid.
1641 for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
1642 if (!groupmember(p->p_ucred->cr_groups[i], td->td_ucred)) {
1647 grpsubset = grpsubset &&
1648 groupmember(p->p_ucred->cr_rgid, td->td_ucred) &&
1649 groupmember(p->p_ucred->cr_svgid, td->td_ucred);
1652 * Are the uids present in p's credential equal to td's
1653 * effective uid? This includes p's euid, svuid, and ruid.
1655 uidsubset = (td->td_ucred->cr_uid == p->p_ucred->cr_uid &&
1656 td->td_ucred->cr_uid == p->p_ucred->cr_svuid &&
1657 td->td_ucred->cr_uid == p->p_ucred->cr_ruid);
1660 * Has the credential of the process changed since the last exec()?
1662 credentialchanged = (p->p_flag & P_SUGID);
1665 * If p's gids aren't a subset, or the uids aren't a subset,
1666 * or the credential has changed, require appropriate privilege
1667 * for td to debug p.
1669 if (!grpsubset || !uidsubset) {
1670 error = priv_check(td, PRIV_DEBUG_DIFFCRED);
1675 if (credentialchanged) {
1676 error = priv_check(td, PRIV_DEBUG_SUGID);
1681 /* Can't trace init when securelevel > 0. */
1682 if (p == initproc) {
1683 error = securelevel_gt(td->td_ucred, 0);
1689 * Can't trace a process that's currently exec'ing.
1691 * XXX: Note, this is not a security policy decision, it's a
1692 * basic correctness/functionality decision. Therefore, this check
1693 * should be moved to the caller's of p_candebug().
1695 if ((p->p_flag & P_INEXEC) != 0)
1702 * Determine whether the subject represented by cred can "see" a socket.
1703 * Returns: 0 for permitted, ENOENT otherwise.
1706 cr_canseesocket(struct ucred *cred, struct socket *so)
1710 error = prison_check(cred, so->so_cred);
1714 error = mac_socket_check_visible(cred, so);
1718 if (cr_seeotheruids(cred, so->so_cred))
1720 if (cr_seeothergids(cred, so->so_cred))
1726 #if defined(INET) || defined(INET6)
1728 * Determine whether the subject represented by cred can "see" a socket.
1729 * Returns: 0 for permitted, ENOENT otherwise.
1732 cr_canseeinpcb(struct ucred *cred, struct inpcb *inp)
1736 error = prison_check(cred, inp->inp_cred);
1740 INP_LOCK_ASSERT(inp);
1741 error = mac_inpcb_check_visible(cred, inp);
1745 if (cr_seeotheruids(cred, inp->inp_cred))
1747 if (cr_seeothergids(cred, inp->inp_cred))
1755 * Determine whether td can wait for the exit of p.
1756 * Returns: 0 for permitted, an errno value otherwise
1757 * Locks: Sufficient locks to protect various components of td and p
1758 * must be held. td must be curthread, and a lock must
1760 * References: td and p must be valid for the lifetime of the call
1764 p_canwait(struct thread *td, struct proc *p)
1768 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1769 PROC_LOCK_ASSERT(p, MA_OWNED);
1770 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1773 if ((error = mac_proc_check_wait(td->td_ucred, p)))
1777 /* XXXMAC: This could have odd effects on some shells. */
1778 if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1786 * Allocate a zeroed cred structure.
1791 register struct ucred *cr;
1793 cr = malloc(sizeof(*cr), M_CRED, M_WAITOK | M_ZERO);
1794 refcount_init(&cr->cr_ref, 1);
1796 audit_cred_init(cr);
1801 crextend(cr, XU_NGROUPS);
1806 * Claim another reference to a ucred structure.
1809 crhold(struct ucred *cr)
1812 refcount_acquire(&cr->cr_ref);
1817 * Free a cred structure. Throws away space when ref count gets to 0.
1820 crfree(struct ucred *cr)
1823 KASSERT(cr->cr_ref > 0, ("bad ucred refcount: %d", cr->cr_ref));
1824 KASSERT(cr->cr_ref != 0xdeadc0de, ("dangling reference to ucred"));
1825 if (refcount_release(&cr->cr_ref)) {
1827 * Some callers of crget(), such as nfs_statfs(),
1828 * allocate a temporary credential, but don't
1829 * allocate a uidinfo structure.
1831 if (cr->cr_uidinfo != NULL)
1832 uifree(cr->cr_uidinfo);
1833 if (cr->cr_ruidinfo != NULL)
1834 uifree(cr->cr_ruidinfo);
1836 * Free a prison, if any.
1838 if (cr->cr_prison != NULL)
1839 prison_free(cr->cr_prison);
1841 audit_cred_destroy(cr);
1844 mac_cred_destroy(cr);
1846 free(cr->cr_groups, M_CRED);
1852 * Check to see if this ucred is shared.
1855 crshared(struct ucred *cr)
1858 return (cr->cr_ref > 1);
1862 * Copy a ucred's contents from a template. Does not block.
1865 crcopy(struct ucred *dest, struct ucred *src)
1868 KASSERT(crshared(dest) == 0, ("crcopy of shared ucred"));
1869 bcopy(&src->cr_startcopy, &dest->cr_startcopy,
1870 (unsigned)((caddr_t)&src->cr_endcopy -
1871 (caddr_t)&src->cr_startcopy));
1872 crsetgroups(dest, src->cr_ngroups, src->cr_groups);
1873 uihold(dest->cr_uidinfo);
1874 uihold(dest->cr_ruidinfo);
1875 prison_hold(dest->cr_prison);
1877 audit_cred_copy(src, dest);
1880 mac_cred_copy(src, dest);
1885 * Dup cred struct to a new held one.
1888 crdup(struct ucred *cr)
1890 struct ucred *newcr;
1898 * Fill in a struct xucred based on a struct ucred.
1901 cru2x(struct ucred *cr, struct xucred *xcr)
1905 bzero(xcr, sizeof(*xcr));
1906 xcr->cr_version = XUCRED_VERSION;
1907 xcr->cr_uid = cr->cr_uid;
1909 ngroups = MIN(cr->cr_ngroups, XU_NGROUPS);
1910 xcr->cr_ngroups = ngroups;
1911 bcopy(cr->cr_groups, xcr->cr_groups,
1912 ngroups * sizeof(*cr->cr_groups));
1916 * small routine to swap a thread's current ucred for the correct one taken
1920 cred_update_thread(struct thread *td)
1926 cred = td->td_ucred;
1928 td->td_ucred = crhold(p->p_ucred);
1935 crcopysafe(struct proc *p, struct ucred *cr)
1937 struct ucred *oldcred;
1940 PROC_LOCK_ASSERT(p, MA_OWNED);
1942 oldcred = p->p_ucred;
1943 while (cr->cr_agroups < oldcred->cr_agroups) {
1944 groups = oldcred->cr_agroups;
1946 crextend(cr, groups);
1948 oldcred = p->p_ucred;
1950 crcopy(cr, oldcred);
1956 * Extend the passed in credential to hold n items.
1959 crextend(struct ucred *cr, int n)
1964 if (n <= cr->cr_agroups)
1968 * We extend by 2 each time since we're using a power of two
1969 * allocator until we need enough groups to fill a page.
1970 * Once we're allocating multiple pages, only allocate as many
1971 * as we actually need. The case of processes needing a
1972 * non-power of two number of pages seems more likely than
1973 * a real world process that adds thousands of groups one at a
1976 if ( n < PAGE_SIZE / sizeof(gid_t) ) {
1977 if (cr->cr_agroups == 0)
1978 cnt = MINALLOCSIZE / sizeof(gid_t);
1980 cnt = cr->cr_agroups * 2;
1985 cnt = roundup2(n, PAGE_SIZE / sizeof(gid_t));
1987 /* Free the old array. */
1989 free(cr->cr_groups, M_CRED);
1991 cr->cr_groups = malloc(cnt * sizeof(gid_t), M_CRED, M_WAITOK | M_ZERO);
1992 cr->cr_agroups = cnt;
1996 * Copy groups in to a credential, preserving any necessary invariants.
1997 * Currently this includes the sorting of all supplemental gids.
1998 * crextend() must have been called before hand to ensure sufficient
1999 * space is available.
2002 crsetgroups_locked(struct ucred *cr, int ngrp, gid_t *groups)
2008 KASSERT(cr->cr_agroups >= ngrp, ("cr_ngroups is too small"));
2010 bcopy(groups, cr->cr_groups, ngrp * sizeof(gid_t));
2011 cr->cr_ngroups = ngrp;
2014 * Sort all groups except cr_groups[0] to allow groupmember to
2015 * perform a binary search.
2017 * XXX: If large numbers of groups become common this should
2018 * be replaced with shell sort like linux uses or possibly
2021 for (i = 2; i < ngrp; i++) {
2022 g = cr->cr_groups[i];
2023 for (j = i-1; j >= 1 && g < cr->cr_groups[j]; j--)
2024 cr->cr_groups[j + 1] = cr->cr_groups[j];
2025 cr->cr_groups[j + 1] = g;
2030 * Copy groups in to a credential after expanding it if required.
2031 * Truncate the list to (ngroups_max + 1) if it is too large.
2034 crsetgroups(struct ucred *cr, int ngrp, gid_t *groups)
2037 if (ngrp > ngroups_max + 1)
2038 ngrp = ngroups_max + 1;
2041 crsetgroups_locked(cr, ngrp, groups);
2045 * Get login name, if available.
2047 #ifndef _SYS_SYSPROTO_H_
2048 struct getlogin_args {
2055 getlogin(struct thread *td, struct getlogin_args *uap)
2058 char login[MAXLOGNAME];
2059 struct proc *p = td->td_proc;
2061 if (uap->namelen > MAXLOGNAME)
2062 uap->namelen = MAXLOGNAME;
2064 SESS_LOCK(p->p_session);
2065 bcopy(p->p_session->s_login, login, uap->namelen);
2066 SESS_UNLOCK(p->p_session);
2068 error = copyout(login, uap->namebuf, uap->namelen);
2075 #ifndef _SYS_SYSPROTO_H_
2076 struct setlogin_args {
2082 setlogin(struct thread *td, struct setlogin_args *uap)
2084 struct proc *p = td->td_proc;
2086 char logintmp[MAXLOGNAME];
2088 error = priv_check(td, PRIV_PROC_SETLOGIN);
2091 error = copyinstr(uap->namebuf, logintmp, sizeof(logintmp), NULL);
2092 if (error == ENAMETOOLONG)
2096 SESS_LOCK(p->p_session);
2097 (void) memcpy(p->p_session->s_login, logintmp,
2099 SESS_UNLOCK(p->p_session);
2106 setsugid(struct proc *p)
2109 PROC_LOCK_ASSERT(p, MA_OWNED);
2110 p->p_flag |= P_SUGID;
2111 if (!(p->p_pfsflags & PF_ISUGID))
2116 * Change a process's effective uid.
2117 * Side effects: newcred->cr_uid and newcred->cr_uidinfo will be modified.
2118 * References: newcred must be an exclusive credential reference for the
2119 * duration of the call.
2122 change_euid(struct ucred *newcred, struct uidinfo *euip)
2125 newcred->cr_uid = euip->ui_uid;
2127 uifree(newcred->cr_uidinfo);
2128 newcred->cr_uidinfo = euip;
2132 * Change a process's effective gid.
2133 * Side effects: newcred->cr_gid will be modified.
2134 * References: newcred must be an exclusive credential reference for the
2135 * duration of the call.
2138 change_egid(struct ucred *newcred, gid_t egid)
2141 newcred->cr_groups[0] = egid;
2145 * Change a process's real uid.
2146 * Side effects: newcred->cr_ruid will be updated, newcred->cr_ruidinfo
2147 * will be updated, and the old and new cr_ruidinfo proc
2148 * counts will be updated.
2149 * References: newcred must be an exclusive credential reference for the
2150 * duration of the call.
2153 change_ruid(struct ucred *newcred, struct uidinfo *ruip)
2156 (void)chgproccnt(newcred->cr_ruidinfo, -1, 0);
2157 newcred->cr_ruid = ruip->ui_uid;
2159 uifree(newcred->cr_ruidinfo);
2160 newcred->cr_ruidinfo = ruip;
2161 (void)chgproccnt(newcred->cr_ruidinfo, 1, 0);
2165 * Change a process's real gid.
2166 * Side effects: newcred->cr_rgid will be updated.
2167 * References: newcred must be an exclusive credential reference for the
2168 * duration of the call.
2171 change_rgid(struct ucred *newcred, gid_t rgid)
2174 newcred->cr_rgid = rgid;
2178 * Change a process's saved uid.
2179 * Side effects: newcred->cr_svuid will be updated.
2180 * References: newcred must be an exclusive credential reference for the
2181 * duration of the call.
2184 change_svuid(struct ucred *newcred, uid_t svuid)
2187 newcred->cr_svuid = svuid;
2191 * Change a process's saved gid.
2192 * Side effects: newcred->cr_svgid will be updated.
2193 * References: newcred must be an exclusive credential reference for the
2194 * duration of the call.
2197 change_svgid(struct ucred *newcred, gid_t svgid)
2200 newcred->cr_svgid = svgid;