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/loginclass.h>
58 #include <sys/malloc.h>
59 #include <sys/mutex.h>
60 #include <sys/refcount.h>
64 #include <sys/sysproto.h>
66 #include <sys/pioctl.h>
67 #include <sys/racct.h>
69 #include <sys/resourcevar.h>
70 #include <sys/socket.h>
71 #include <sys/socketvar.h>
72 #include <sys/syscallsubr.h>
73 #include <sys/sysctl.h>
77 "Kernel support for interfaces necessary for regression testing (SECURITY RISK!)");
80 #if defined(INET) || defined(INET6)
81 #include <netinet/in.h>
82 #include <netinet/in_pcb.h>
85 #include <security/audit/audit.h>
86 #include <security/mac/mac_framework.h>
88 static MALLOC_DEFINE(M_CRED, "cred", "credentials");
90 SYSCTL_NODE(_security, OID_AUTO, bsd, CTLFLAG_RW, 0, "BSD security policy");
92 static void crsetgroups_locked(struct ucred *cr, int ngrp,
95 #ifndef _SYS_SYSPROTO_H_
102 sys_getpid(struct thread *td, struct getpid_args *uap)
104 struct proc *p = td->td_proc;
106 td->td_retval[0] = p->p_pid;
107 #if defined(COMPAT_43)
108 td->td_retval[1] = kern_getppid(td);
113 #ifndef _SYS_SYSPROTO_H_
114 struct getppid_args {
120 sys_getppid(struct thread *td, struct getppid_args *uap)
123 td->td_retval[0] = kern_getppid(td);
128 kern_getppid(struct thread *td)
130 struct proc *p = td->td_proc;
135 if (!(p->p_flag & P_TRACED)) {
136 ppid = p->p_pptr->p_pid;
140 sx_slock(&proctree_lock);
141 pp = proc_realparent(p);
143 sx_sunlock(&proctree_lock);
150 * Get process group ID; note that POSIX getpgrp takes no parameter.
152 #ifndef _SYS_SYSPROTO_H_
153 struct getpgrp_args {
158 sys_getpgrp(struct thread *td, struct getpgrp_args *uap)
160 struct proc *p = td->td_proc;
163 td->td_retval[0] = p->p_pgrp->pg_id;
168 /* Get an arbitrary pid's process group id */
169 #ifndef _SYS_SYSPROTO_H_
170 struct getpgid_args {
175 sys_getpgid(struct thread *td, struct getpgid_args *uap)
187 error = p_cansee(td, p);
193 td->td_retval[0] = p->p_pgrp->pg_id;
199 * Get an arbitrary pid's session id.
201 #ifndef _SYS_SYSPROTO_H_
207 sys_getsid(struct thread *td, struct getsid_args *uap)
219 error = p_cansee(td, p);
225 td->td_retval[0] = p->p_session->s_sid;
230 #ifndef _SYS_SYSPROTO_H_
237 sys_getuid(struct thread *td, struct getuid_args *uap)
240 td->td_retval[0] = td->td_ucred->cr_ruid;
241 #if defined(COMPAT_43)
242 td->td_retval[1] = td->td_ucred->cr_uid;
247 #ifndef _SYS_SYSPROTO_H_
248 struct geteuid_args {
254 sys_geteuid(struct thread *td, struct geteuid_args *uap)
257 td->td_retval[0] = td->td_ucred->cr_uid;
261 #ifndef _SYS_SYSPROTO_H_
268 sys_getgid(struct thread *td, struct getgid_args *uap)
271 td->td_retval[0] = td->td_ucred->cr_rgid;
272 #if defined(COMPAT_43)
273 td->td_retval[1] = td->td_ucred->cr_groups[0];
279 * Get effective group ID. The "egid" is groups[0], and could be obtained
280 * via getgroups. This syscall exists because it is somewhat painful to do
281 * correctly in a library function.
283 #ifndef _SYS_SYSPROTO_H_
284 struct getegid_args {
290 sys_getegid(struct thread *td, struct getegid_args *uap)
293 td->td_retval[0] = td->td_ucred->cr_groups[0];
297 #ifndef _SYS_SYSPROTO_H_
298 struct getgroups_args {
304 sys_getgroups(struct thread *td, struct getgroups_args *uap)
311 ngrp = cred->cr_ngroups;
313 if (uap->gidsetsize == 0) {
317 if (uap->gidsetsize < ngrp)
320 error = copyout(cred->cr_groups, uap->gidset, ngrp * sizeof(gid_t));
322 td->td_retval[0] = ngrp;
326 #ifndef _SYS_SYSPROTO_H_
333 sys_setsid(struct thread *td, struct setsid_args *uap)
337 struct proc *p = td->td_proc;
338 struct pgrp *newpgrp;
339 struct session *newsess;
344 newpgrp = malloc(sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
345 newsess = malloc(sizeof(struct session), M_SESSION, M_WAITOK | M_ZERO);
347 sx_xlock(&proctree_lock);
349 if (p->p_pgid == p->p_pid || (pgrp = pgfind(p->p_pid)) != NULL) {
354 (void)enterpgrp(p, p->p_pid, newpgrp, newsess);
355 td->td_retval[0] = p->p_pid;
360 sx_xunlock(&proctree_lock);
363 free(newpgrp, M_PGRP);
365 free(newsess, M_SESSION);
371 * set process group (setpgid/old setpgrp)
373 * caller does setpgid(targpid, targpgid)
375 * pid must be caller or child of caller (ESRCH)
377 * pid must be in same session (EPERM)
378 * pid can't have done an exec (EACCES)
380 * there must exist some pid in same session having pgid (EPERM)
381 * pid must not be session leader (EPERM)
383 #ifndef _SYS_SYSPROTO_H_
384 struct setpgid_args {
385 int pid; /* target process id */
386 int pgid; /* target pgrp id */
391 sys_setpgid(struct thread *td, struct setpgid_args *uap)
393 struct proc *curp = td->td_proc;
394 struct proc *targp; /* target process */
395 struct pgrp *pgrp; /* target pgrp */
397 struct pgrp *newpgrp;
404 newpgrp = malloc(sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
406 sx_xlock(&proctree_lock);
407 if (uap->pid != 0 && uap->pid != curp->p_pid) {
408 if ((targp = pfind(uap->pid)) == NULL) {
412 if (!inferior(targp)) {
417 if ((error = p_cansee(td, targp))) {
421 if (targp->p_pgrp == NULL ||
422 targp->p_session != curp->p_session) {
427 if (targp->p_flag & P_EXEC) {
435 if (SESS_LEADER(targp)) {
440 uap->pgid = targp->p_pid;
441 if ((pgrp = pgfind(uap->pgid)) == NULL) {
442 if (uap->pgid == targp->p_pid) {
443 error = enterpgrp(targp, uap->pgid, newpgrp,
450 if (pgrp == targp->p_pgrp) {
454 if (pgrp->pg_id != targp->p_pid &&
455 pgrp->pg_session != curp->p_session) {
461 error = enterthispgrp(targp, pgrp);
464 sx_xunlock(&proctree_lock);
465 KASSERT((error == 0) || (newpgrp != NULL),
466 ("setpgid failed and newpgrp is NULL"));
468 free(newpgrp, M_PGRP);
473 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
474 * compatible. It says that setting the uid/gid to euid/egid is a special
475 * case of "appropriate privilege". Once the rules are expanded out, this
476 * basically means that setuid(nnn) sets all three id's, in all permitted
477 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
478 * does not set the saved id - this is dangerous for traditional BSD
479 * programs. For this reason, we *really* do not want to set
480 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
482 #define POSIX_APPENDIX_B_4_2_2
484 #ifndef _SYS_SYSPROTO_H_
491 sys_setuid(struct thread *td, struct setuid_args *uap)
493 struct proc *p = td->td_proc;
494 struct ucred *newcred, *oldcred;
505 * Copy credentials so other references do not see our changes.
507 oldcred = crcopysafe(p, newcred);
510 error = mac_cred_check_setuid(oldcred, uid);
516 * See if we have "permission" by POSIX 1003.1 rules.
518 * Note that setuid(geteuid()) is a special case of
519 * "appropriate privileges" in appendix B.4.2.2. We need
520 * to use this clause to be compatible with traditional BSD
521 * semantics. Basically, it means that "setuid(xx)" sets all
522 * three id's (assuming you have privs).
524 * Notes on the logic. We do things in three steps.
525 * 1: We determine if the euid is going to change, and do EPERM
526 * right away. We unconditionally change the euid later if this
527 * test is satisfied, simplifying that part of the logic.
528 * 2: We determine if the real and/or saved uids are going to
529 * change. Determined by compile options.
530 * 3: Change euid last. (after tests in #2 for "appropriate privs")
532 if (uid != oldcred->cr_ruid && /* allow setuid(getuid()) */
533 #ifdef _POSIX_SAVED_IDS
534 uid != oldcred->cr_svuid && /* allow setuid(saved gid) */
536 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
537 uid != oldcred->cr_uid && /* allow setuid(geteuid()) */
539 (error = priv_check_cred(oldcred, PRIV_CRED_SETUID, 0)) != 0)
542 #ifdef _POSIX_SAVED_IDS
544 * Do we have "appropriate privileges" (are we root or uid == euid)
545 * If so, we are changing the real uid and/or saved uid.
548 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
549 uid == oldcred->cr_uid ||
551 /* We are using privs. */
552 priv_check_cred(oldcred, PRIV_CRED_SETUID, 0) == 0)
556 * Set the real uid and transfer proc count to new user.
558 if (uid != oldcred->cr_ruid) {
559 change_ruid(newcred, uip);
565 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
566 * the security of seteuid() depends on it. B.4.2.2 says it
567 * is important that we should do this.
569 if (uid != oldcred->cr_svuid) {
570 change_svuid(newcred, uid);
576 * In all permitted cases, we are changing the euid.
578 if (uid != oldcred->cr_uid) {
579 change_euid(newcred, uip);
582 proc_set_cred(p, newcred);
584 racct_proc_ucred_changed(p, oldcred, newcred);
589 rctl_proc_ucred_changed(p, newcred);
603 #ifndef _SYS_SYSPROTO_H_
604 struct seteuid_args {
610 sys_seteuid(struct thread *td, struct seteuid_args *uap)
612 struct proc *p = td->td_proc;
613 struct ucred *newcred, *oldcred;
615 struct uidinfo *euip;
619 AUDIT_ARG_EUID(euid);
624 * Copy credentials so other references do not see our changes.
626 oldcred = crcopysafe(p, newcred);
629 error = mac_cred_check_seteuid(oldcred, euid);
634 if (euid != oldcred->cr_ruid && /* allow seteuid(getuid()) */
635 euid != oldcred->cr_svuid && /* allow seteuid(saved uid) */
636 (error = priv_check_cred(oldcred, PRIV_CRED_SETEUID, 0)) != 0)
640 * Everything's okay, do it.
642 if (oldcred->cr_uid != euid) {
643 change_euid(newcred, euip);
646 proc_set_cred(p, newcred);
659 #ifndef _SYS_SYSPROTO_H_
666 sys_setgid(struct thread *td, struct setgid_args *uap)
668 struct proc *p = td->td_proc;
669 struct ucred *newcred, *oldcred;
677 oldcred = crcopysafe(p, newcred);
680 error = mac_cred_check_setgid(oldcred, gid);
686 * See if we have "permission" by POSIX 1003.1 rules.
688 * Note that setgid(getegid()) is a special case of
689 * "appropriate privileges" in appendix B.4.2.2. We need
690 * to use this clause to be compatible with traditional BSD
691 * semantics. Basically, it means that "setgid(xx)" sets all
692 * three id's (assuming you have privs).
694 * For notes on the logic here, see setuid() above.
696 if (gid != oldcred->cr_rgid && /* allow setgid(getgid()) */
697 #ifdef _POSIX_SAVED_IDS
698 gid != oldcred->cr_svgid && /* allow setgid(saved gid) */
700 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
701 gid != oldcred->cr_groups[0] && /* allow setgid(getegid()) */
703 (error = priv_check_cred(oldcred, PRIV_CRED_SETGID, 0)) != 0)
706 #ifdef _POSIX_SAVED_IDS
708 * Do we have "appropriate privileges" (are we root or gid == egid)
709 * If so, we are changing the real uid and saved gid.
712 #ifdef POSIX_APPENDIX_B_4_2_2 /* use the clause from B.4.2.2 */
713 gid == oldcred->cr_groups[0] ||
715 /* We are using privs. */
716 priv_check_cred(oldcred, PRIV_CRED_SETGID, 0) == 0)
722 if (oldcred->cr_rgid != gid) {
723 change_rgid(newcred, gid);
729 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
730 * the security of setegid() depends on it. B.4.2.2 says it
731 * is important that we should do this.
733 if (oldcred->cr_svgid != gid) {
734 change_svgid(newcred, gid);
739 * In all cases permitted cases, we are changing the egid.
740 * Copy credentials so other references do not see our changes.
742 if (oldcred->cr_groups[0] != gid) {
743 change_egid(newcred, gid);
746 proc_set_cred(p, newcred);
757 #ifndef _SYS_SYSPROTO_H_
758 struct setegid_args {
764 sys_setegid(struct thread *td, struct setegid_args *uap)
766 struct proc *p = td->td_proc;
767 struct ucred *newcred, *oldcred;
772 AUDIT_ARG_EGID(egid);
775 oldcred = crcopysafe(p, newcred);
778 error = mac_cred_check_setegid(oldcred, egid);
783 if (egid != oldcred->cr_rgid && /* allow setegid(getgid()) */
784 egid != oldcred->cr_svgid && /* allow setegid(saved gid) */
785 (error = priv_check_cred(oldcred, PRIV_CRED_SETEGID, 0)) != 0)
788 if (oldcred->cr_groups[0] != egid) {
789 change_egid(newcred, egid);
792 proc_set_cred(p, newcred);
803 #ifndef _SYS_SYSPROTO_H_
804 struct setgroups_args {
811 sys_setgroups(struct thread *td, struct setgroups_args *uap)
813 gid_t smallgroups[XU_NGROUPS];
818 gidsetsize = uap->gidsetsize;
819 if (gidsetsize > ngroups_max + 1)
822 if (gidsetsize > XU_NGROUPS)
823 groups = malloc(gidsetsize * sizeof(gid_t), M_TEMP, M_WAITOK);
825 groups = smallgroups;
827 error = copyin(uap->gidset, groups, gidsetsize * sizeof(gid_t));
829 error = kern_setgroups(td, gidsetsize, groups);
831 if (gidsetsize > XU_NGROUPS)
832 free(groups, M_TEMP);
837 kern_setgroups(struct thread *td, u_int ngrp, gid_t *groups)
839 struct proc *p = td->td_proc;
840 struct ucred *newcred, *oldcred;
843 MPASS(ngrp <= ngroups_max + 1);
844 AUDIT_ARG_GROUPSET(groups, ngrp);
846 crextend(newcred, ngrp);
848 oldcred = crcopysafe(p, newcred);
851 error = mac_cred_check_setgroups(oldcred, ngrp, groups);
856 error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0);
862 * setgroups(0, NULL) is a legitimate way of clearing the
863 * groups vector on non-BSD systems (which generally do not
864 * have the egid in the groups[0]). We risk security holes
865 * when running non-BSD software if we do not do the same.
867 newcred->cr_ngroups = 1;
869 crsetgroups_locked(newcred, ngrp, groups);
872 proc_set_cred(p, newcred);
883 #ifndef _SYS_SYSPROTO_H_
884 struct setreuid_args {
891 sys_setreuid(struct thread *td, struct setreuid_args *uap)
893 struct proc *p = td->td_proc;
894 struct ucred *newcred, *oldcred;
896 struct uidinfo *euip, *ruip;
901 AUDIT_ARG_EUID(euid);
902 AUDIT_ARG_RUID(ruid);
907 oldcred = crcopysafe(p, newcred);
910 error = mac_cred_check_setreuid(oldcred, ruid, euid);
915 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
916 ruid != oldcred->cr_svuid) ||
917 (euid != (uid_t)-1 && euid != oldcred->cr_uid &&
918 euid != oldcred->cr_ruid && euid != oldcred->cr_svuid)) &&
919 (error = priv_check_cred(oldcred, PRIV_CRED_SETREUID, 0)) != 0)
922 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
923 change_euid(newcred, euip);
926 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
927 change_ruid(newcred, ruip);
930 if ((ruid != (uid_t)-1 || newcred->cr_uid != newcred->cr_ruid) &&
931 newcred->cr_svuid != newcred->cr_uid) {
932 change_svuid(newcred, newcred->cr_uid);
935 proc_set_cred(p, newcred);
937 racct_proc_ucred_changed(p, oldcred, newcred);
942 rctl_proc_ucred_changed(p, newcred);
958 #ifndef _SYS_SYSPROTO_H_
959 struct setregid_args {
966 sys_setregid(struct thread *td, struct setregid_args *uap)
968 struct proc *p = td->td_proc;
969 struct ucred *newcred, *oldcred;
975 AUDIT_ARG_EGID(egid);
976 AUDIT_ARG_RGID(rgid);
979 oldcred = crcopysafe(p, newcred);
982 error = mac_cred_check_setregid(oldcred, rgid, egid);
987 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
988 rgid != oldcred->cr_svgid) ||
989 (egid != (gid_t)-1 && egid != oldcred->cr_groups[0] &&
990 egid != oldcred->cr_rgid && egid != oldcred->cr_svgid)) &&
991 (error = priv_check_cred(oldcred, PRIV_CRED_SETREGID, 0)) != 0)
994 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
995 change_egid(newcred, egid);
998 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
999 change_rgid(newcred, rgid);
1002 if ((rgid != (gid_t)-1 || newcred->cr_groups[0] != newcred->cr_rgid) &&
1003 newcred->cr_svgid != newcred->cr_groups[0]) {
1004 change_svgid(newcred, newcred->cr_groups[0]);
1007 proc_set_cred(p, newcred);
1019 * setresuid(ruid, euid, suid) is like setreuid except control over the saved
1022 #ifndef _SYS_SYSPROTO_H_
1023 struct setresuid_args {
1031 sys_setresuid(struct thread *td, struct setresuid_args *uap)
1033 struct proc *p = td->td_proc;
1034 struct ucred *newcred, *oldcred;
1035 uid_t euid, ruid, suid;
1036 struct uidinfo *euip, *ruip;
1042 AUDIT_ARG_EUID(euid);
1043 AUDIT_ARG_RUID(ruid);
1044 AUDIT_ARG_SUID(suid);
1046 euip = uifind(euid);
1047 ruip = uifind(ruid);
1049 oldcred = crcopysafe(p, newcred);
1052 error = mac_cred_check_setresuid(oldcred, ruid, euid, suid);
1057 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
1058 ruid != oldcred->cr_svuid &&
1059 ruid != oldcred->cr_uid) ||
1060 (euid != (uid_t)-1 && euid != oldcred->cr_ruid &&
1061 euid != oldcred->cr_svuid &&
1062 euid != oldcred->cr_uid) ||
1063 (suid != (uid_t)-1 && suid != oldcred->cr_ruid &&
1064 suid != oldcred->cr_svuid &&
1065 suid != oldcred->cr_uid)) &&
1066 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESUID, 0)) != 0)
1069 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
1070 change_euid(newcred, euip);
1073 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
1074 change_ruid(newcred, ruip);
1077 if (suid != (uid_t)-1 && oldcred->cr_svuid != suid) {
1078 change_svuid(newcred, suid);
1081 proc_set_cred(p, newcred);
1083 racct_proc_ucred_changed(p, oldcred, newcred);
1088 rctl_proc_ucred_changed(p, newcred);
1106 * setresgid(rgid, egid, sgid) is like setregid except control over the saved
1109 #ifndef _SYS_SYSPROTO_H_
1110 struct setresgid_args {
1118 sys_setresgid(struct thread *td, struct setresgid_args *uap)
1120 struct proc *p = td->td_proc;
1121 struct ucred *newcred, *oldcred;
1122 gid_t egid, rgid, sgid;
1128 AUDIT_ARG_EGID(egid);
1129 AUDIT_ARG_RGID(rgid);
1130 AUDIT_ARG_SGID(sgid);
1133 oldcred = crcopysafe(p, newcred);
1136 error = mac_cred_check_setresgid(oldcred, rgid, egid, sgid);
1141 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
1142 rgid != oldcred->cr_svgid &&
1143 rgid != oldcred->cr_groups[0]) ||
1144 (egid != (gid_t)-1 && egid != oldcred->cr_rgid &&
1145 egid != oldcred->cr_svgid &&
1146 egid != oldcred->cr_groups[0]) ||
1147 (sgid != (gid_t)-1 && sgid != oldcred->cr_rgid &&
1148 sgid != oldcred->cr_svgid &&
1149 sgid != oldcred->cr_groups[0])) &&
1150 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESGID, 0)) != 0)
1153 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
1154 change_egid(newcred, egid);
1157 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
1158 change_rgid(newcred, rgid);
1161 if (sgid != (gid_t)-1 && oldcred->cr_svgid != sgid) {
1162 change_svgid(newcred, sgid);
1165 proc_set_cred(p, newcred);
1176 #ifndef _SYS_SYSPROTO_H_
1177 struct getresuid_args {
1185 sys_getresuid(struct thread *td, struct getresuid_args *uap)
1188 int error1 = 0, error2 = 0, error3 = 0;
1190 cred = td->td_ucred;
1192 error1 = copyout(&cred->cr_ruid,
1193 uap->ruid, sizeof(cred->cr_ruid));
1195 error2 = copyout(&cred->cr_uid,
1196 uap->euid, sizeof(cred->cr_uid));
1198 error3 = copyout(&cred->cr_svuid,
1199 uap->suid, sizeof(cred->cr_svuid));
1200 return (error1 ? error1 : error2 ? error2 : error3);
1203 #ifndef _SYS_SYSPROTO_H_
1204 struct getresgid_args {
1212 sys_getresgid(struct thread *td, struct getresgid_args *uap)
1215 int error1 = 0, error2 = 0, error3 = 0;
1217 cred = td->td_ucred;
1219 error1 = copyout(&cred->cr_rgid,
1220 uap->rgid, sizeof(cred->cr_rgid));
1222 error2 = copyout(&cred->cr_groups[0],
1223 uap->egid, sizeof(cred->cr_groups[0]));
1225 error3 = copyout(&cred->cr_svgid,
1226 uap->sgid, sizeof(cred->cr_svgid));
1227 return (error1 ? error1 : error2 ? error2 : error3);
1230 #ifndef _SYS_SYSPROTO_H_
1231 struct issetugid_args {
1237 sys_issetugid(struct thread *td, struct issetugid_args *uap)
1239 struct proc *p = td->td_proc;
1242 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
1243 * we use P_SUGID because we consider changing the owners as
1244 * "tainting" as well.
1245 * This is significant for procs that start as root and "become"
1246 * a user without an exec - programs cannot know *everything*
1247 * that libc *might* have put in their data segment.
1249 td->td_retval[0] = (p->p_flag & P_SUGID) ? 1 : 0;
1254 sys___setugid(struct thread *td, struct __setugid_args *uap)
1260 switch (uap->flag) {
1263 p->p_flag &= ~P_SUGID;
1268 p->p_flag |= P_SUGID;
1274 #else /* !REGRESSION */
1277 #endif /* REGRESSION */
1281 * Check if gid is a member of the group set.
1284 groupmember(gid_t gid, struct ucred *cred)
1290 if (cred->cr_groups[0] == gid)
1294 * If gid was not our primary group, perform a binary search
1295 * of the supplemental groups. This is possible because we
1296 * sort the groups in crsetgroups().
1299 h = cred->cr_ngroups;
1301 m = l + ((h - l) / 2);
1302 if (cred->cr_groups[m] < gid)
1307 if ((l < cred->cr_ngroups) && (cred->cr_groups[l] == gid))
1314 * Test the active securelevel against a given level. securelevel_gt()
1315 * implements (securelevel > level). securelevel_ge() implements
1316 * (securelevel >= level). Note that the logic is inverted -- these
1317 * functions return EPERM on "success" and 0 on "failure".
1319 * Due to care taken when setting the securelevel, we know that no jail will
1320 * be less secure that its parent (or the physical system), so it is sufficient
1321 * to test the current jail only.
1323 * XXXRW: Possibly since this has to do with privilege, it should move to
1327 securelevel_gt(struct ucred *cr, int level)
1330 return (cr->cr_prison->pr_securelevel > level ? EPERM : 0);
1334 securelevel_ge(struct ucred *cr, int level)
1337 return (cr->cr_prison->pr_securelevel >= level ? EPERM : 0);
1341 * 'see_other_uids' determines whether or not visibility of processes
1342 * and sockets with credentials holding different real uids is possible
1343 * using a variety of system MIBs.
1344 * XXX: data declarations should be together near the beginning of the file.
1346 static int see_other_uids = 1;
1347 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_uids, CTLFLAG_RW,
1349 "Unprivileged processes may see subjects/objects with different real uid");
1352 * Determine if u1 "can see" the subject specified by u2, according to the
1353 * 'see_other_uids' policy.
1354 * Returns: 0 for permitted, ESRCH otherwise
1356 * References: *u1 and *u2 must not change during the call
1357 * u1 may equal u2, in which case only one reference is required
1360 cr_seeotheruids(struct ucred *u1, struct ucred *u2)
1363 if (!see_other_uids && u1->cr_ruid != u2->cr_ruid) {
1364 if (priv_check_cred(u1, PRIV_SEEOTHERUIDS, 0) != 0)
1371 * 'see_other_gids' determines whether or not visibility of processes
1372 * and sockets with credentials holding different real gids is possible
1373 * using a variety of system MIBs.
1374 * XXX: data declarations should be together near the beginning of the file.
1376 static int see_other_gids = 1;
1377 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_gids, CTLFLAG_RW,
1379 "Unprivileged processes may see subjects/objects with different real gid");
1382 * Determine if u1 can "see" the subject specified by u2, according to the
1383 * 'see_other_gids' policy.
1384 * Returns: 0 for permitted, ESRCH otherwise
1386 * References: *u1 and *u2 must not change during the call
1387 * u1 may equal u2, in which case only one reference is required
1390 cr_seeothergids(struct ucred *u1, struct ucred *u2)
1394 if (!see_other_gids) {
1396 for (i = 0; i < u1->cr_ngroups; i++) {
1397 if (groupmember(u1->cr_groups[i], u2))
1403 if (priv_check_cred(u1, PRIV_SEEOTHERGIDS, 0) != 0)
1411 * Determine if u1 "can see" the subject specified by u2.
1412 * Returns: 0 for permitted, an errno value otherwise
1414 * References: *u1 and *u2 must not change during the call
1415 * u1 may equal u2, in which case only one reference is required
1418 cr_cansee(struct ucred *u1, struct ucred *u2)
1422 if ((error = prison_check(u1, u2)))
1425 if ((error = mac_cred_check_visible(u1, u2)))
1428 if ((error = cr_seeotheruids(u1, u2)))
1430 if ((error = cr_seeothergids(u1, u2)))
1436 * Determine if td "can see" the subject specified by p.
1437 * Returns: 0 for permitted, an errno value otherwise
1438 * Locks: Sufficient locks to protect p->p_ucred must be held. td really
1439 * should be curthread.
1440 * References: td and p must be valid for the lifetime of the call
1443 p_cansee(struct thread *td, struct proc *p)
1446 /* Wrap cr_cansee() for all functionality. */
1447 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1448 PROC_LOCK_ASSERT(p, MA_OWNED);
1449 return (cr_cansee(td->td_ucred, p->p_ucred));
1453 * 'conservative_signals' prevents the delivery of a broad class of
1454 * signals by unprivileged processes to processes that have changed their
1455 * credentials since the last invocation of execve(). This can prevent
1456 * the leakage of cached information or retained privileges as a result
1457 * of a common class of signal-related vulnerabilities. However, this
1458 * may interfere with some applications that expect to be able to
1459 * deliver these signals to peer processes after having given up
1462 static int conservative_signals = 1;
1463 SYSCTL_INT(_security_bsd, OID_AUTO, conservative_signals, CTLFLAG_RW,
1464 &conservative_signals, 0, "Unprivileged processes prevented from "
1465 "sending certain signals to processes whose credentials have changed");
1467 * Determine whether cred may deliver the specified signal to proc.
1468 * Returns: 0 for permitted, an errno value otherwise.
1469 * Locks: A lock must be held for proc.
1470 * References: cred and proc must be valid for the lifetime of the call.
1473 cr_cansignal(struct ucred *cred, struct proc *proc, int signum)
1477 PROC_LOCK_ASSERT(proc, MA_OWNED);
1479 * Jail semantics limit the scope of signalling to proc in the
1480 * same jail as cred, if cred is in jail.
1482 error = prison_check(cred, proc->p_ucred);
1486 if ((error = mac_proc_check_signal(cred, proc, signum)))
1489 if ((error = cr_seeotheruids(cred, proc->p_ucred)))
1491 if ((error = cr_seeothergids(cred, proc->p_ucred)))
1495 * UNIX signal semantics depend on the status of the P_SUGID
1496 * bit on the target process. If the bit is set, then additional
1497 * restrictions are placed on the set of available signals.
1499 if (conservative_signals && (proc->p_flag & P_SUGID)) {
1514 * Generally, permit job and terminal control
1519 /* Not permitted without privilege. */
1520 error = priv_check_cred(cred, PRIV_SIGNAL_SUGID, 0);
1527 * Generally, the target credential's ruid or svuid must match the
1528 * subject credential's ruid or euid.
1530 if (cred->cr_ruid != proc->p_ucred->cr_ruid &&
1531 cred->cr_ruid != proc->p_ucred->cr_svuid &&
1532 cred->cr_uid != proc->p_ucred->cr_ruid &&
1533 cred->cr_uid != proc->p_ucred->cr_svuid) {
1534 error = priv_check_cred(cred, PRIV_SIGNAL_DIFFCRED, 0);
1543 * Determine whether td may deliver the specified signal to p.
1544 * Returns: 0 for permitted, an errno value otherwise
1545 * Locks: Sufficient locks to protect various components of td and p
1546 * must be held. td must be curthread, and a lock must be
1548 * References: td and p must be valid for the lifetime of the call
1551 p_cansignal(struct thread *td, struct proc *p, int signum)
1554 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1555 PROC_LOCK_ASSERT(p, MA_OWNED);
1556 if (td->td_proc == p)
1560 * UNIX signalling semantics require that processes in the same
1561 * session always be able to deliver SIGCONT to one another,
1562 * overriding the remaining protections.
1564 /* XXX: This will require an additional lock of some sort. */
1565 if (signum == SIGCONT && td->td_proc->p_session == p->p_session)
1568 * Some compat layers use SIGTHR and higher signals for
1569 * communication between different kernel threads of the same
1570 * process, so that they expect that it's always possible to
1571 * deliver them, even for suid applications where cr_cansignal() can
1572 * deny such ability for security consideration. It should be
1573 * pretty safe to do since the only way to create two processes
1574 * with the same p_leader is via rfork(2).
1576 if (td->td_proc->p_leader != NULL && signum >= SIGTHR &&
1577 signum < SIGTHR + 4 && td->td_proc->p_leader == p->p_leader)
1580 return (cr_cansignal(td->td_ucred, p, signum));
1584 * Determine whether td may reschedule p.
1585 * Returns: 0 for permitted, an errno value otherwise
1586 * Locks: Sufficient locks to protect various components of td and p
1587 * must be held. td must be curthread, and a lock must
1589 * References: td and p must be valid for the lifetime of the call
1592 p_cansched(struct thread *td, struct proc *p)
1596 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1597 PROC_LOCK_ASSERT(p, MA_OWNED);
1598 if (td->td_proc == p)
1600 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1603 if ((error = mac_proc_check_sched(td->td_ucred, p)))
1606 if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1608 if ((error = cr_seeothergids(td->td_ucred, p->p_ucred)))
1610 if (td->td_ucred->cr_ruid != p->p_ucred->cr_ruid &&
1611 td->td_ucred->cr_uid != p->p_ucred->cr_ruid) {
1612 error = priv_check(td, PRIV_SCHED_DIFFCRED);
1620 * The 'unprivileged_proc_debug' flag may be used to disable a variety of
1621 * unprivileged inter-process debugging services, including some procfs
1622 * functionality, ptrace(), and ktrace(). In the past, inter-process
1623 * debugging has been involved in a variety of security problems, and sites
1624 * not requiring the service might choose to disable it when hardening
1627 * XXX: Should modifying and reading this variable require locking?
1628 * XXX: data declarations should be together near the beginning of the file.
1630 static int unprivileged_proc_debug = 1;
1631 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_proc_debug, CTLFLAG_RW,
1632 &unprivileged_proc_debug, 0,
1633 "Unprivileged processes may use process debugging facilities");
1636 * Determine whether td may debug p.
1637 * Returns: 0 for permitted, an errno value otherwise
1638 * Locks: Sufficient locks to protect various components of td and p
1639 * must be held. td must be curthread, and a lock must
1641 * References: td and p must be valid for the lifetime of the call
1644 p_candebug(struct thread *td, struct proc *p)
1646 int credentialchanged, error, grpsubset, i, uidsubset;
1648 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1649 PROC_LOCK_ASSERT(p, MA_OWNED);
1650 if (!unprivileged_proc_debug) {
1651 error = priv_check(td, PRIV_DEBUG_UNPRIV);
1655 if (td->td_proc == p)
1657 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1660 if ((error = mac_proc_check_debug(td->td_ucred, p)))
1663 if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1665 if ((error = cr_seeothergids(td->td_ucred, p->p_ucred)))
1669 * Is p's group set a subset of td's effective group set? This
1670 * includes p's egid, group access list, rgid, and svgid.
1673 for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
1674 if (!groupmember(p->p_ucred->cr_groups[i], td->td_ucred)) {
1679 grpsubset = grpsubset &&
1680 groupmember(p->p_ucred->cr_rgid, td->td_ucred) &&
1681 groupmember(p->p_ucred->cr_svgid, td->td_ucred);
1684 * Are the uids present in p's credential equal to td's
1685 * effective uid? This includes p's euid, svuid, and ruid.
1687 uidsubset = (td->td_ucred->cr_uid == p->p_ucred->cr_uid &&
1688 td->td_ucred->cr_uid == p->p_ucred->cr_svuid &&
1689 td->td_ucred->cr_uid == p->p_ucred->cr_ruid);
1692 * Has the credential of the process changed since the last exec()?
1694 credentialchanged = (p->p_flag & P_SUGID);
1697 * If p's gids aren't a subset, or the uids aren't a subset,
1698 * or the credential has changed, require appropriate privilege
1699 * for td to debug p.
1701 if (!grpsubset || !uidsubset) {
1702 error = priv_check(td, PRIV_DEBUG_DIFFCRED);
1707 if (credentialchanged) {
1708 error = priv_check(td, PRIV_DEBUG_SUGID);
1713 /* Can't trace init when securelevel > 0. */
1714 if (p == initproc) {
1715 error = securelevel_gt(td->td_ucred, 0);
1721 * Can't trace a process that's currently exec'ing.
1723 * XXX: Note, this is not a security policy decision, it's a
1724 * basic correctness/functionality decision. Therefore, this check
1725 * should be moved to the caller's of p_candebug().
1727 if ((p->p_flag & P_INEXEC) != 0)
1730 /* Denied explicitely */
1731 if ((p->p_flag2 & P2_NOTRACE) != 0) {
1732 error = priv_check(td, PRIV_DEBUG_DENIED);
1741 * Determine whether the subject represented by cred can "see" a socket.
1742 * Returns: 0 for permitted, ENOENT otherwise.
1745 cr_canseesocket(struct ucred *cred, struct socket *so)
1749 error = prison_check(cred, so->so_cred);
1753 error = mac_socket_check_visible(cred, so);
1757 if (cr_seeotheruids(cred, so->so_cred))
1759 if (cr_seeothergids(cred, so->so_cred))
1765 #if defined(INET) || defined(INET6)
1767 * Determine whether the subject represented by cred can "see" a socket.
1768 * Returns: 0 for permitted, ENOENT otherwise.
1771 cr_canseeinpcb(struct ucred *cred, struct inpcb *inp)
1775 error = prison_check(cred, inp->inp_cred);
1779 INP_LOCK_ASSERT(inp);
1780 error = mac_inpcb_check_visible(cred, inp);
1784 if (cr_seeotheruids(cred, inp->inp_cred))
1786 if (cr_seeothergids(cred, inp->inp_cred))
1794 * Determine whether td can wait for the exit of p.
1795 * Returns: 0 for permitted, an errno value otherwise
1796 * Locks: Sufficient locks to protect various components of td and p
1797 * must be held. td must be curthread, and a lock must
1799 * References: td and p must be valid for the lifetime of the call
1803 p_canwait(struct thread *td, struct proc *p)
1807 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1808 PROC_LOCK_ASSERT(p, MA_OWNED);
1809 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1812 if ((error = mac_proc_check_wait(td->td_ucred, p)))
1816 /* XXXMAC: This could have odd effects on some shells. */
1817 if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1825 * Allocate a zeroed cred structure.
1832 cr = malloc(sizeof(*cr), M_CRED, M_WAITOK | M_ZERO);
1833 refcount_init(&cr->cr_ref, 1);
1835 audit_cred_init(cr);
1840 cr->cr_groups = cr->cr_smallgroups;
1842 sizeof(cr->cr_smallgroups) / sizeof(cr->cr_smallgroups[0]);
1847 * Claim another reference to a ucred structure.
1850 crhold(struct ucred *cr)
1853 refcount_acquire(&cr->cr_ref);
1858 * Free a cred structure. Throws away space when ref count gets to 0.
1861 crfree(struct ucred *cr)
1864 KASSERT(cr->cr_ref > 0, ("bad ucred refcount: %d", cr->cr_ref));
1865 KASSERT(cr->cr_ref != 0xdeadc0de, ("dangling reference to ucred"));
1866 if (refcount_release(&cr->cr_ref)) {
1868 * Some callers of crget(), such as nfs_statfs(),
1869 * allocate a temporary credential, but don't
1870 * allocate a uidinfo structure.
1872 if (cr->cr_uidinfo != NULL)
1873 uifree(cr->cr_uidinfo);
1874 if (cr->cr_ruidinfo != NULL)
1875 uifree(cr->cr_ruidinfo);
1877 * Free a prison, if any.
1879 if (cr->cr_prison != NULL)
1880 prison_free(cr->cr_prison);
1881 if (cr->cr_loginclass != NULL)
1882 loginclass_free(cr->cr_loginclass);
1884 audit_cred_destroy(cr);
1887 mac_cred_destroy(cr);
1889 if (cr->cr_groups != cr->cr_smallgroups)
1890 free(cr->cr_groups, M_CRED);
1896 * Copy a ucred's contents from a template. Does not block.
1899 crcopy(struct ucred *dest, struct ucred *src)
1902 KASSERT(dest->cr_ref == 1, ("crcopy of shared ucred"));
1903 bcopy(&src->cr_startcopy, &dest->cr_startcopy,
1904 (unsigned)((caddr_t)&src->cr_endcopy -
1905 (caddr_t)&src->cr_startcopy));
1906 crsetgroups(dest, src->cr_ngroups, src->cr_groups);
1907 uihold(dest->cr_uidinfo);
1908 uihold(dest->cr_ruidinfo);
1909 prison_hold(dest->cr_prison);
1910 loginclass_hold(dest->cr_loginclass);
1912 audit_cred_copy(src, dest);
1915 mac_cred_copy(src, dest);
1920 * Dup cred struct to a new held one.
1923 crdup(struct ucred *cr)
1925 struct ucred *newcr;
1933 * Fill in a struct xucred based on a struct ucred.
1936 cru2x(struct ucred *cr, struct xucred *xcr)
1940 bzero(xcr, sizeof(*xcr));
1941 xcr->cr_version = XUCRED_VERSION;
1942 xcr->cr_uid = cr->cr_uid;
1944 ngroups = MIN(cr->cr_ngroups, XU_NGROUPS);
1945 xcr->cr_ngroups = ngroups;
1946 bcopy(cr->cr_groups, xcr->cr_groups,
1947 ngroups * sizeof(*cr->cr_groups));
1951 * Set initial process credentials.
1952 * Callers are responsible for providing the reference for provided credentials.
1955 proc_set_cred_init(struct proc *p, struct ucred *newcred)
1958 p->p_ucred = newcred;
1962 * Change process credentials.
1963 * Callers are responsible for providing the reference for passed credentials
1964 * and for freeing old ones.
1966 * Process has to be locked except when it does not have credentials (as it
1967 * should not be visible just yet) or when newcred is NULL (as this can be
1968 * only used when the process is about to be freed, at which point it should
1969 * not be visible anymore).
1972 proc_set_cred(struct proc *p, struct ucred *newcred)
1974 struct ucred *oldcred;
1976 MPASS(p->p_ucred != NULL);
1977 if (newcred == NULL)
1978 MPASS(p->p_state == PRS_ZOMBIE);
1980 PROC_LOCK_ASSERT(p, MA_OWNED);
1982 oldcred = p->p_ucred;
1983 p->p_ucred = newcred;
1984 if (newcred != NULL)
1990 crcopysafe(struct proc *p, struct ucred *cr)
1992 struct ucred *oldcred;
1995 PROC_LOCK_ASSERT(p, MA_OWNED);
1997 oldcred = p->p_ucred;
1998 while (cr->cr_agroups < oldcred->cr_agroups) {
1999 groups = oldcred->cr_agroups;
2001 crextend(cr, groups);
2003 oldcred = p->p_ucred;
2005 crcopy(cr, oldcred);
2011 * Extend the passed in credential to hold n items.
2014 crextend(struct ucred *cr, int n)
2019 if (n <= cr->cr_agroups)
2023 * We extend by 2 each time since we're using a power of two
2024 * allocator until we need enough groups to fill a page.
2025 * Once we're allocating multiple pages, only allocate as many
2026 * as we actually need. The case of processes needing a
2027 * non-power of two number of pages seems more likely than
2028 * a real world process that adds thousands of groups one at a
2031 if ( n < PAGE_SIZE / sizeof(gid_t) ) {
2032 if (cr->cr_agroups == 0)
2033 cnt = MINALLOCSIZE / sizeof(gid_t);
2035 cnt = cr->cr_agroups * 2;
2040 cnt = roundup2(n, PAGE_SIZE / sizeof(gid_t));
2042 /* Free the old array. */
2043 if (cr->cr_groups != cr->cr_smallgroups)
2044 free(cr->cr_groups, M_CRED);
2046 cr->cr_groups = malloc(cnt * sizeof(gid_t), M_CRED, M_WAITOK | M_ZERO);
2047 cr->cr_agroups = cnt;
2051 * Copy groups in to a credential, preserving any necessary invariants.
2052 * Currently this includes the sorting of all supplemental gids.
2053 * crextend() must have been called before hand to ensure sufficient
2054 * space is available.
2057 crsetgroups_locked(struct ucred *cr, int ngrp, gid_t *groups)
2063 KASSERT(cr->cr_agroups >= ngrp, ("cr_ngroups is too small"));
2065 bcopy(groups, cr->cr_groups, ngrp * sizeof(gid_t));
2066 cr->cr_ngroups = ngrp;
2069 * Sort all groups except cr_groups[0] to allow groupmember to
2070 * perform a binary search.
2072 * XXX: If large numbers of groups become common this should
2073 * be replaced with shell sort like linux uses or possibly
2076 for (i = 2; i < ngrp; i++) {
2077 g = cr->cr_groups[i];
2078 for (j = i-1; j >= 1 && g < cr->cr_groups[j]; j--)
2079 cr->cr_groups[j + 1] = cr->cr_groups[j];
2080 cr->cr_groups[j + 1] = g;
2085 * Copy groups in to a credential after expanding it if required.
2086 * Truncate the list to (ngroups_max + 1) if it is too large.
2089 crsetgroups(struct ucred *cr, int ngrp, gid_t *groups)
2092 if (ngrp > ngroups_max + 1)
2093 ngrp = ngroups_max + 1;
2096 crsetgroups_locked(cr, ngrp, groups);
2100 * Get login name, if available.
2102 #ifndef _SYS_SYSPROTO_H_
2103 struct getlogin_args {
2110 sys_getlogin(struct thread *td, struct getlogin_args *uap)
2112 char login[MAXLOGNAME];
2113 struct proc *p = td->td_proc;
2116 if (uap->namelen > MAXLOGNAME)
2117 uap->namelen = MAXLOGNAME;
2119 SESS_LOCK(p->p_session);
2120 len = strlcpy(login, p->p_session->s_login, uap->namelen) + 1;
2121 SESS_UNLOCK(p->p_session);
2123 if (len > uap->namelen)
2125 return (copyout(login, uap->namebuf, len));
2131 #ifndef _SYS_SYSPROTO_H_
2132 struct setlogin_args {
2138 sys_setlogin(struct thread *td, struct setlogin_args *uap)
2140 struct proc *p = td->td_proc;
2142 char logintmp[MAXLOGNAME];
2144 CTASSERT(sizeof(p->p_session->s_login) >= sizeof(logintmp));
2146 error = priv_check(td, PRIV_PROC_SETLOGIN);
2149 error = copyinstr(uap->namebuf, logintmp, sizeof(logintmp), NULL);
2151 if (error == ENAMETOOLONG)
2156 SESS_LOCK(p->p_session);
2157 strcpy(p->p_session->s_login, logintmp);
2158 SESS_UNLOCK(p->p_session);
2164 setsugid(struct proc *p)
2167 PROC_LOCK_ASSERT(p, MA_OWNED);
2168 p->p_flag |= P_SUGID;
2169 if (!(p->p_pfsflags & PF_ISUGID))
2174 * Change a process's effective uid.
2175 * Side effects: newcred->cr_uid and newcred->cr_uidinfo will be modified.
2176 * References: newcred must be an exclusive credential reference for the
2177 * duration of the call.
2180 change_euid(struct ucred *newcred, struct uidinfo *euip)
2183 newcred->cr_uid = euip->ui_uid;
2185 uifree(newcred->cr_uidinfo);
2186 newcred->cr_uidinfo = euip;
2190 * Change a process's effective gid.
2191 * Side effects: newcred->cr_gid will be modified.
2192 * References: newcred must be an exclusive credential reference for the
2193 * duration of the call.
2196 change_egid(struct ucred *newcred, gid_t egid)
2199 newcred->cr_groups[0] = egid;
2203 * Change a process's real uid.
2204 * Side effects: newcred->cr_ruid will be updated, newcred->cr_ruidinfo
2205 * will be updated, and the old and new cr_ruidinfo proc
2206 * counts will be updated.
2207 * References: newcred must be an exclusive credential reference for the
2208 * duration of the call.
2211 change_ruid(struct ucred *newcred, struct uidinfo *ruip)
2214 (void)chgproccnt(newcred->cr_ruidinfo, -1, 0);
2215 newcred->cr_ruid = ruip->ui_uid;
2217 uifree(newcred->cr_ruidinfo);
2218 newcred->cr_ruidinfo = ruip;
2219 (void)chgproccnt(newcred->cr_ruidinfo, 1, 0);
2223 * Change a process's real gid.
2224 * Side effects: newcred->cr_rgid will be updated.
2225 * References: newcred must be an exclusive credential reference for the
2226 * duration of the call.
2229 change_rgid(struct ucred *newcred, gid_t rgid)
2232 newcred->cr_rgid = rgid;
2236 * Change a process's saved uid.
2237 * Side effects: newcred->cr_svuid will be updated.
2238 * References: newcred must be an exclusive credential reference for the
2239 * duration of the call.
2242 change_svuid(struct ucred *newcred, uid_t svuid)
2245 newcred->cr_svuid = svuid;
2249 * Change a process's saved gid.
2250 * Side effects: newcred->cr_svgid will be updated.
2251 * References: newcred must be an exclusive credential reference for the
2252 * duration of the call.
2255 change_svgid(struct ucred *newcred, gid_t svgid)
2258 newcred->cr_svgid = svgid;