2 * Copyright (c) 1982, 1986, 1989, 1990, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 * Copyright (c) 2000-2001 Robert N. M. Watson. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * @(#)kern_prot.c 8.6 (Berkeley) 1/21/94
39 * System calls related to processes and protection
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
45 #include "opt_compat.h"
48 #include <sys/param.h>
49 #include <sys/systm.h>
52 #include <sys/kernel.h>
54 #include <sys/malloc.h>
55 #include <sys/mutex.h>
56 #include <sys/refcount.h>
60 #include <sys/sysproto.h>
62 #include <sys/pioctl.h>
63 #include <sys/resourcevar.h>
64 #include <sys/socket.h>
65 #include <sys/socketvar.h>
66 #include <sys/syscallsubr.h>
67 #include <sys/sysctl.h>
69 #include <security/audit/audit.h>
70 #include <security/mac/mac_framework.h>
72 static MALLOC_DEFINE(M_CRED, "cred", "credentials");
74 SYSCTL_NODE(_security, OID_AUTO, bsd, CTLFLAG_RW, 0, "BSD security policy");
76 #ifndef _SYS_SYSPROTO_H_
86 getpid(struct thread *td, struct getpid_args *uap)
88 struct proc *p = td->td_proc;
90 td->td_retval[0] = p->p_pid;
91 #if defined(COMPAT_43)
93 td->td_retval[1] = p->p_pptr->p_pid;
99 #ifndef _SYS_SYSPROTO_H_
100 struct getppid_args {
109 getppid(struct thread *td, struct getppid_args *uap)
111 struct proc *p = td->td_proc;
114 td->td_retval[0] = p->p_pptr->p_pid;
120 * Get process group ID; note that POSIX getpgrp takes no parameter.
122 #ifndef _SYS_SYSPROTO_H_
123 struct getpgrp_args {
131 getpgrp(struct thread *td, struct getpgrp_args *uap)
133 struct proc *p = td->td_proc;
136 td->td_retval[0] = p->p_pgrp->pg_id;
141 /* Get an arbitary pid's process group id */
142 #ifndef _SYS_SYSPROTO_H_
143 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_
186 getsid(struct thread *td, struct getsid_args *uap)
198 error = p_cansee(td, p);
204 td->td_retval[0] = p->p_session->s_sid;
209 #ifndef _SYS_SYSPROTO_H_
219 getuid(struct thread *td, struct getuid_args *uap)
222 td->td_retval[0] = td->td_ucred->cr_ruid;
223 #if defined(COMPAT_43)
224 td->td_retval[1] = td->td_ucred->cr_uid;
229 #ifndef _SYS_SYSPROTO_H_
230 struct geteuid_args {
239 geteuid(struct thread *td, struct geteuid_args *uap)
242 td->td_retval[0] = td->td_ucred->cr_uid;
246 #ifndef _SYS_SYSPROTO_H_
256 getgid(struct thread *td, struct getgid_args *uap)
259 td->td_retval[0] = td->td_ucred->cr_rgid;
260 #if defined(COMPAT_43)
261 td->td_retval[1] = td->td_ucred->cr_groups[0];
267 * Get effective group ID. The "egid" is groups[0], and could be obtained
268 * via getgroups. This syscall exists because it is somewhat painful to do
269 * correctly in a library function.
271 #ifndef _SYS_SYSPROTO_H_
272 struct getegid_args {
281 getegid(struct thread *td, struct getegid_args *uap)
284 td->td_retval[0] = td->td_ucred->cr_groups[0];
288 #ifndef _SYS_SYSPROTO_H_
289 struct getgroups_args {
298 getgroups(struct thread *td, register struct getgroups_args *uap)
300 gid_t groups[NGROUPS];
304 ngrp = MIN(uap->gidsetsize, NGROUPS);
305 error = kern_getgroups(td, &ngrp, groups);
308 if (uap->gidsetsize > 0)
309 error = copyout(groups, uap->gidset, ngrp * sizeof(gid_t));
311 td->td_retval[0] = ngrp;
316 kern_getgroups(struct thread *td, u_int *ngrp, gid_t *groups)
322 *ngrp = cred->cr_ngroups;
325 if (*ngrp < cred->cr_ngroups)
327 *ngrp = cred->cr_ngroups;
328 bcopy(cred->cr_groups, groups, *ngrp * sizeof(gid_t));
332 #ifndef _SYS_SYSPROTO_H_
342 setsid(register struct thread *td, struct setsid_args *uap)
346 struct proc *p = td->td_proc;
347 struct pgrp *newpgrp;
348 struct session *newsess;
353 MALLOC(newpgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
354 MALLOC(newsess, struct session *, sizeof(struct session), M_SESSION, M_WAITOK | M_ZERO);
356 sx_xlock(&proctree_lock);
358 if (p->p_pgid == p->p_pid || (pgrp = pgfind(p->p_pid)) != NULL) {
363 (void)enterpgrp(p, p->p_pid, newpgrp, newsess);
364 td->td_retval[0] = p->p_pid;
369 sx_xunlock(&proctree_lock);
372 FREE(newpgrp, M_PGRP);
374 FREE(newsess, M_SESSION);
380 * set process group (setpgid/old setpgrp)
382 * caller does setpgid(targpid, targpgid)
384 * pid must be caller or child of caller (ESRCH)
386 * pid must be in same session (EPERM)
387 * pid can't have done an exec (EACCES)
389 * there must exist some pid in same session having pgid (EPERM)
390 * pid must not be session leader (EPERM)
392 #ifndef _SYS_SYSPROTO_H_
393 struct setpgid_args {
394 int pid; /* target process id */
395 int pgid; /* target pgrp id */
403 setpgid(struct thread *td, register struct setpgid_args *uap)
405 struct proc *curp = td->td_proc;
406 register struct proc *targp; /* target process */
407 register struct pgrp *pgrp; /* target pgrp */
409 struct pgrp *newpgrp;
416 MALLOC(newpgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
418 sx_xlock(&proctree_lock);
419 if (uap->pid != 0 && uap->pid != curp->p_pid) {
420 if ((targp = pfind(uap->pid)) == NULL) {
424 if (!inferior(targp)) {
429 if ((error = p_cansee(td, targp))) {
433 if (targp->p_pgrp == NULL ||
434 targp->p_session != curp->p_session) {
439 if (targp->p_flag & P_EXEC) {
447 if (SESS_LEADER(targp)) {
452 uap->pgid = targp->p_pid;
453 if ((pgrp = pgfind(uap->pgid)) == NULL) {
454 if (uap->pgid == targp->p_pid) {
455 error = enterpgrp(targp, uap->pgid, newpgrp,
462 if (pgrp == targp->p_pgrp) {
466 if (pgrp->pg_id != targp->p_pid &&
467 pgrp->pg_session != curp->p_session) {
473 error = enterthispgrp(targp, pgrp);
476 sx_xunlock(&proctree_lock);
477 KASSERT((error == 0) || (newpgrp != NULL),
478 ("setpgid failed and newpgrp is NULL"));
480 FREE(newpgrp, M_PGRP);
485 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
486 * compatible. It says that setting the uid/gid to euid/egid is a special
487 * case of "appropriate privilege". Once the rules are expanded out, this
488 * basically means that setuid(nnn) sets all three id's, in all permitted
489 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
490 * does not set the saved id - this is dangerous for traditional BSD
491 * programs. For this reason, we *really* do not want to set
492 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
494 #define POSIX_APPENDIX_B_4_2_2
496 #ifndef _SYS_SYSPROTO_H_
506 setuid(struct thread *td, struct setuid_args *uap)
508 struct proc *p = td->td_proc;
509 struct ucred *newcred, *oldcred;
519 oldcred = p->p_ucred;
522 error = mac_check_proc_setuid(p, oldcred, uid);
528 * See if we have "permission" by POSIX 1003.1 rules.
530 * Note that setuid(geteuid()) is a special case of
531 * "appropriate privileges" in appendix B.4.2.2. We need
532 * to use this clause to be compatible with traditional BSD
533 * semantics. Basically, it means that "setuid(xx)" sets all
534 * three id's (assuming you have privs).
536 * Notes on the logic. We do things in three steps.
537 * 1: We determine if the euid is going to change, and do EPERM
538 * right away. We unconditionally change the euid later if this
539 * test is satisfied, simplifying that part of the logic.
540 * 2: We determine if the real and/or saved uids are going to
541 * change. Determined by compile options.
542 * 3: Change euid last. (after tests in #2 for "appropriate privs")
544 if (uid != oldcred->cr_ruid && /* allow setuid(getuid()) */
545 #ifdef _POSIX_SAVED_IDS
546 uid != oldcred->cr_svuid && /* allow setuid(saved gid) */
548 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
549 uid != oldcred->cr_uid && /* allow setuid(geteuid()) */
551 (error = priv_check_cred(oldcred, PRIV_CRED_SETUID,
552 SUSER_ALLOWJAIL)) != 0)
556 * Copy credentials so other references do not see our changes.
558 crcopy(newcred, oldcred);
559 #ifdef _POSIX_SAVED_IDS
561 * Do we have "appropriate privileges" (are we root or uid == euid)
562 * If so, we are changing the real uid and/or saved uid.
565 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
566 uid == oldcred->cr_uid ||
568 /* We are using privs. */
569 priv_check_cred(oldcred, PRIV_CRED_SETUID, SUSER_ALLOWJAIL) == 0)
573 * Set the real uid and transfer proc count to new user.
575 if (uid != oldcred->cr_ruid) {
576 change_ruid(newcred, uip);
582 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
583 * the security of seteuid() depends on it. B.4.2.2 says it
584 * is important that we should do this.
586 if (uid != oldcred->cr_svuid) {
587 change_svuid(newcred, uid);
593 * In all permitted cases, we are changing the euid.
595 if (uid != oldcred->cr_uid) {
596 change_euid(newcred, uip);
599 p->p_ucred = newcred;
612 #ifndef _SYS_SYSPROTO_H_
613 struct seteuid_args {
622 seteuid(struct thread *td, struct seteuid_args *uap)
624 struct proc *p = td->td_proc;
625 struct ucred *newcred, *oldcred;
627 struct uidinfo *euip;
631 AUDIT_ARG(euid, euid);
635 oldcred = p->p_ucred;
638 error = mac_check_proc_seteuid(p, oldcred, euid);
643 if (euid != oldcred->cr_ruid && /* allow seteuid(getuid()) */
644 euid != oldcred->cr_svuid && /* allow seteuid(saved uid) */
645 (error = priv_check_cred(oldcred, PRIV_CRED_SETEUID,
646 SUSER_ALLOWJAIL)) != 0)
650 * Everything's okay, do it. Copy credentials so other references do
651 * not see our changes.
653 crcopy(newcred, oldcred);
654 if (oldcred->cr_uid != euid) {
655 change_euid(newcred, euip);
658 p->p_ucred = newcred;
671 #ifndef _SYS_SYSPROTO_H_
681 setgid(struct thread *td, struct setgid_args *uap)
683 struct proc *p = td->td_proc;
684 struct ucred *newcred, *oldcred;
692 oldcred = p->p_ucred;
695 error = mac_check_proc_setgid(p, oldcred, gid);
701 * See if we have "permission" by POSIX 1003.1 rules.
703 * Note that setgid(getegid()) is a special case of
704 * "appropriate privileges" in appendix B.4.2.2. We need
705 * to use this clause to be compatible with traditional BSD
706 * semantics. Basically, it means that "setgid(xx)" sets all
707 * three id's (assuming you have privs).
709 * For notes on the logic here, see setuid() above.
711 if (gid != oldcred->cr_rgid && /* allow setgid(getgid()) */
712 #ifdef _POSIX_SAVED_IDS
713 gid != oldcred->cr_svgid && /* allow setgid(saved gid) */
715 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
716 gid != oldcred->cr_groups[0] && /* allow setgid(getegid()) */
718 (error = priv_check_cred(oldcred, PRIV_CRED_SETGID,
719 SUSER_ALLOWJAIL)) != 0)
722 crcopy(newcred, oldcred);
723 #ifdef _POSIX_SAVED_IDS
725 * Do we have "appropriate privileges" (are we root or gid == egid)
726 * If so, we are changing the real uid and saved gid.
729 #ifdef POSIX_APPENDIX_B_4_2_2 /* use the clause from B.4.2.2 */
730 gid == oldcred->cr_groups[0] ||
732 /* We are using privs. */
733 priv_check_cred(oldcred, PRIV_CRED_SETGID, SUSER_ALLOWJAIL) == 0)
739 if (oldcred->cr_rgid != gid) {
740 change_rgid(newcred, gid);
746 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
747 * the security of setegid() depends on it. B.4.2.2 says it
748 * is important that we should do this.
750 if (oldcred->cr_svgid != gid) {
751 change_svgid(newcred, gid);
756 * In all cases permitted cases, we are changing the egid.
757 * Copy credentials so other references do not see our changes.
759 if (oldcred->cr_groups[0] != gid) {
760 change_egid(newcred, gid);
763 p->p_ucred = newcred;
774 #ifndef _SYS_SYSPROTO_H_
775 struct setegid_args {
784 setegid(struct thread *td, struct setegid_args *uap)
786 struct proc *p = td->td_proc;
787 struct ucred *newcred, *oldcred;
792 AUDIT_ARG(egid, egid);
795 oldcred = p->p_ucred;
798 error = mac_check_proc_setegid(p, oldcred, egid);
803 if (egid != oldcred->cr_rgid && /* allow setegid(getgid()) */
804 egid != oldcred->cr_svgid && /* allow setegid(saved gid) */
805 (error = priv_check_cred(oldcred, PRIV_CRED_SETEGID,
806 SUSER_ALLOWJAIL)) != 0)
809 crcopy(newcred, oldcred);
810 if (oldcred->cr_groups[0] != egid) {
811 change_egid(newcred, egid);
814 p->p_ucred = newcred;
825 #ifndef _SYS_SYSPROTO_H_
826 struct setgroups_args {
836 setgroups(struct thread *td, struct setgroups_args *uap)
838 gid_t groups[NGROUPS];
841 if (uap->gidsetsize > NGROUPS)
843 error = copyin(uap->gidset, groups, uap->gidsetsize * sizeof(gid_t));
846 return (kern_setgroups(td, uap->gidsetsize, groups));
850 kern_setgroups(struct thread *td, u_int ngrp, gid_t *groups)
852 struct proc *p = td->td_proc;
853 struct ucred *newcred, *oldcred;
858 AUDIT_ARG(groupset, groups, ngrp);
861 oldcred = p->p_ucred;
864 error = mac_check_proc_setgroups(p, oldcred, ngrp, groups);
869 error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS,
875 * XXX A little bit lazy here. We could test if anything has
876 * changed before crcopy() and setting P_SUGID.
878 crcopy(newcred, oldcred);
881 * setgroups(0, NULL) is a legitimate way of clearing the
882 * groups vector on non-BSD systems (which generally do not
883 * have the egid in the groups[0]). We risk security holes
884 * when running non-BSD software if we do not do the same.
886 newcred->cr_ngroups = 1;
888 bcopy(groups, newcred->cr_groups, ngrp * sizeof(gid_t));
889 newcred->cr_ngroups = ngrp;
892 p->p_ucred = newcred;
903 #ifndef _SYS_SYSPROTO_H_
904 struct setreuid_args {
914 setreuid(register struct thread *td, struct setreuid_args *uap)
916 struct proc *p = td->td_proc;
917 struct ucred *newcred, *oldcred;
919 struct uidinfo *euip, *ruip;
924 AUDIT_ARG(euid, euid);
925 AUDIT_ARG(ruid, ruid);
930 oldcred = p->p_ucred;
933 error = mac_check_proc_setreuid(p, oldcred, ruid, euid);
938 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
939 ruid != oldcred->cr_svuid) ||
940 (euid != (uid_t)-1 && euid != oldcred->cr_uid &&
941 euid != oldcred->cr_ruid && euid != oldcred->cr_svuid)) &&
942 (error = priv_check_cred(oldcred, PRIV_CRED_SETREUID,
943 SUSER_ALLOWJAIL)) != 0)
946 crcopy(newcred, oldcred);
947 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
948 change_euid(newcred, euip);
951 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
952 change_ruid(newcred, ruip);
955 if ((ruid != (uid_t)-1 || newcred->cr_uid != newcred->cr_ruid) &&
956 newcred->cr_svuid != newcred->cr_uid) {
957 change_svuid(newcred, newcred->cr_uid);
960 p->p_ucred = newcred;
975 #ifndef _SYS_SYSPROTO_H_
976 struct setregid_args {
986 setregid(register struct thread *td, struct setregid_args *uap)
988 struct proc *p = td->td_proc;
989 struct ucred *newcred, *oldcred;
995 AUDIT_ARG(egid, egid);
996 AUDIT_ARG(rgid, rgid);
999 oldcred = p->p_ucred;
1002 error = mac_check_proc_setregid(p, oldcred, rgid, egid);
1007 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
1008 rgid != oldcred->cr_svgid) ||
1009 (egid != (gid_t)-1 && egid != oldcred->cr_groups[0] &&
1010 egid != oldcred->cr_rgid && egid != oldcred->cr_svgid)) &&
1011 (error = priv_check_cred(oldcred, PRIV_CRED_SETREGID,
1012 SUSER_ALLOWJAIL)) != 0)
1015 crcopy(newcred, oldcred);
1016 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
1017 change_egid(newcred, egid);
1020 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
1021 change_rgid(newcred, rgid);
1024 if ((rgid != (gid_t)-1 || newcred->cr_groups[0] != newcred->cr_rgid) &&
1025 newcred->cr_svgid != newcred->cr_groups[0]) {
1026 change_svgid(newcred, newcred->cr_groups[0]);
1029 p->p_ucred = newcred;
1041 * setresuid(ruid, euid, suid) is like setreuid except control over the
1042 * saved uid is explicit.
1045 #ifndef _SYS_SYSPROTO_H_
1046 struct setresuid_args {
1057 setresuid(register struct thread *td, struct setresuid_args *uap)
1059 struct proc *p = td->td_proc;
1060 struct ucred *newcred, *oldcred;
1061 uid_t euid, ruid, suid;
1062 struct uidinfo *euip, *ruip;
1068 AUDIT_ARG(euid, euid);
1069 AUDIT_ARG(ruid, ruid);
1070 AUDIT_ARG(suid, suid);
1072 euip = uifind(euid);
1073 ruip = uifind(ruid);
1075 oldcred = p->p_ucred;
1078 error = mac_check_proc_setresuid(p, oldcred, ruid, euid, suid);
1083 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
1084 ruid != oldcred->cr_svuid &&
1085 ruid != oldcred->cr_uid) ||
1086 (euid != (uid_t)-1 && euid != oldcred->cr_ruid &&
1087 euid != oldcred->cr_svuid &&
1088 euid != oldcred->cr_uid) ||
1089 (suid != (uid_t)-1 && suid != oldcred->cr_ruid &&
1090 suid != oldcred->cr_svuid &&
1091 suid != oldcred->cr_uid)) &&
1092 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESUID,
1093 SUSER_ALLOWJAIL)) != 0)
1096 crcopy(newcred, oldcred);
1097 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
1098 change_euid(newcred, euip);
1101 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
1102 change_ruid(newcred, ruip);
1105 if (suid != (uid_t)-1 && oldcred->cr_svuid != suid) {
1106 change_svuid(newcred, suid);
1109 p->p_ucred = newcred;
1126 * setresgid(rgid, egid, sgid) is like setregid except control over the
1127 * saved gid is explicit.
1130 #ifndef _SYS_SYSPROTO_H_
1131 struct setresgid_args {
1142 setresgid(register struct thread *td, struct setresgid_args *uap)
1144 struct proc *p = td->td_proc;
1145 struct ucred *newcred, *oldcred;
1146 gid_t egid, rgid, sgid;
1152 AUDIT_ARG(egid, egid);
1153 AUDIT_ARG(rgid, rgid);
1154 AUDIT_ARG(sgid, sgid);
1157 oldcred = p->p_ucred;
1160 error = mac_check_proc_setresgid(p, oldcred, rgid, egid, sgid);
1165 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
1166 rgid != oldcred->cr_svgid &&
1167 rgid != oldcred->cr_groups[0]) ||
1168 (egid != (gid_t)-1 && egid != oldcred->cr_rgid &&
1169 egid != oldcred->cr_svgid &&
1170 egid != oldcred->cr_groups[0]) ||
1171 (sgid != (gid_t)-1 && sgid != oldcred->cr_rgid &&
1172 sgid != oldcred->cr_svgid &&
1173 sgid != oldcred->cr_groups[0])) &&
1174 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESGID,
1175 SUSER_ALLOWJAIL)) != 0)
1178 crcopy(newcred, oldcred);
1179 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
1180 change_egid(newcred, egid);
1183 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
1184 change_rgid(newcred, rgid);
1187 if (sgid != (gid_t)-1 && oldcred->cr_svgid != sgid) {
1188 change_svgid(newcred, sgid);
1191 p->p_ucred = newcred;
1202 #ifndef _SYS_SYSPROTO_H_
1203 struct getresuid_args {
1214 getresuid(register struct thread *td, struct getresuid_args *uap)
1217 int error1 = 0, error2 = 0, error3 = 0;
1219 cred = td->td_ucred;
1221 error1 = copyout(&cred->cr_ruid,
1222 uap->ruid, sizeof(cred->cr_ruid));
1224 error2 = copyout(&cred->cr_uid,
1225 uap->euid, sizeof(cred->cr_uid));
1227 error3 = copyout(&cred->cr_svuid,
1228 uap->suid, sizeof(cred->cr_svuid));
1229 return (error1 ? error1 : error2 ? error2 : error3);
1232 #ifndef _SYS_SYSPROTO_H_
1233 struct getresgid_args {
1244 getresgid(register struct thread *td, struct getresgid_args *uap)
1247 int error1 = 0, error2 = 0, error3 = 0;
1249 cred = td->td_ucred;
1251 error1 = copyout(&cred->cr_rgid,
1252 uap->rgid, sizeof(cred->cr_rgid));
1254 error2 = copyout(&cred->cr_groups[0],
1255 uap->egid, sizeof(cred->cr_groups[0]));
1257 error3 = copyout(&cred->cr_svgid,
1258 uap->sgid, sizeof(cred->cr_svgid));
1259 return (error1 ? error1 : error2 ? error2 : error3);
1262 #ifndef _SYS_SYSPROTO_H_
1263 struct issetugid_args {
1272 issetugid(register struct thread *td, struct issetugid_args *uap)
1274 struct proc *p = td->td_proc;
1277 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
1278 * we use P_SUGID because we consider changing the owners as
1279 * "tainting" as well.
1280 * This is significant for procs that start as root and "become"
1281 * a user without an exec - programs cannot know *everything*
1282 * that libc *might* have put in their data segment.
1285 td->td_retval[0] = (p->p_flag & P_SUGID) ? 1 : 0;
1294 __setugid(struct thread *td, struct __setugid_args *uap)
1300 switch (uap->flag) {
1303 p->p_flag &= ~P_SUGID;
1308 p->p_flag |= P_SUGID;
1314 #else /* !REGRESSION */
1317 #endif /* REGRESSION */
1321 * Check if gid is a member of the group set.
1323 * MPSAFE (cred must be held)
1326 groupmember(gid_t gid, struct ucred *cred)
1331 egp = &(cred->cr_groups[cred->cr_ngroups]);
1332 for (gp = cred->cr_groups; gp < egp; gp++)
1339 * Test the active securelevel against a given level. securelevel_gt()
1340 * implements (securelevel > level). securelevel_ge() implements
1341 * (securelevel >= level). Note that the logic is inverted -- these
1342 * functions return EPERM on "success" and 0 on "failure".
1344 * XXXRW: Possibly since this has to do with privilege, it should move to
1350 securelevel_gt(struct ucred *cr, int level)
1352 int active_securelevel;
1354 active_securelevel = securelevel;
1355 KASSERT(cr != NULL, ("securelevel_gt: null cr"));
1356 if (cr->cr_prison != NULL)
1357 active_securelevel = imax(cr->cr_prison->pr_securelevel,
1358 active_securelevel);
1359 return (active_securelevel > level ? EPERM : 0);
1363 securelevel_ge(struct ucred *cr, int level)
1365 int active_securelevel;
1367 active_securelevel = securelevel;
1368 KASSERT(cr != NULL, ("securelevel_ge: null cr"));
1369 if (cr->cr_prison != NULL)
1370 active_securelevel = imax(cr->cr_prison->pr_securelevel,
1371 active_securelevel);
1372 return (active_securelevel >= level ? EPERM : 0);
1376 * 'see_other_uids' determines whether or not visibility of processes
1377 * and sockets with credentials holding different real uids is possible
1378 * using a variety of system MIBs.
1379 * XXX: data declarations should be together near the beginning of the file.
1381 static int see_other_uids = 1;
1382 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_uids, CTLFLAG_RW,
1384 "Unprivileged processes may see subjects/objects with different real uid");
1387 * Determine if u1 "can see" the subject specified by u2, according to the
1388 * 'see_other_uids' policy.
1389 * Returns: 0 for permitted, ESRCH otherwise
1391 * References: *u1 and *u2 must not change during the call
1392 * u1 may equal u2, in which case only one reference is required
1395 cr_seeotheruids(struct ucred *u1, struct ucred *u2)
1398 if (!see_other_uids && u1->cr_ruid != u2->cr_ruid) {
1399 if (priv_check_cred(u1, PRIV_SEEOTHERUIDS, SUSER_ALLOWJAIL)
1407 * 'see_other_gids' determines whether or not visibility of processes
1408 * and sockets with credentials holding different real gids is possible
1409 * using a variety of system MIBs.
1410 * XXX: data declarations should be together near the beginning of the file.
1412 static int see_other_gids = 1;
1413 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_gids, CTLFLAG_RW,
1415 "Unprivileged processes may see subjects/objects with different real gid");
1418 * Determine if u1 can "see" the subject specified by u2, according to the
1419 * 'see_other_gids' policy.
1420 * Returns: 0 for permitted, ESRCH otherwise
1422 * References: *u1 and *u2 must not change during the call
1423 * u1 may equal u2, in which case only one reference is required
1426 cr_seeothergids(struct ucred *u1, struct ucred *u2)
1430 if (!see_other_gids) {
1432 for (i = 0; i < u1->cr_ngroups; i++) {
1433 if (groupmember(u1->cr_groups[i], u2))
1439 if (priv_check_cred(u1, PRIV_SEEOTHERGIDS,
1440 SUSER_ALLOWJAIL) != 0)
1448 * Determine if u1 "can see" the subject specified by u2.
1449 * Returns: 0 for permitted, an errno value otherwise
1451 * References: *u1 and *u2 must not change during the call
1452 * u1 may equal u2, in which case only one reference is required
1455 cr_cansee(struct ucred *u1, struct ucred *u2)
1459 if ((error = prison_check(u1, u2)))
1462 if ((error = mac_check_cred_visible(u1, u2)))
1465 if ((error = cr_seeotheruids(u1, u2)))
1467 if ((error = cr_seeothergids(u1, u2)))
1473 * Determine if td "can see" the subject specified by p.
1474 * Returns: 0 for permitted, an errno value otherwise
1475 * Locks: Sufficient locks to protect p->p_ucred must be held. td really
1476 * should be curthread.
1477 * References: td and p must be valid for the lifetime of the call
1480 p_cansee(struct thread *td, struct proc *p)
1483 /* Wrap cr_cansee() for all functionality. */
1484 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1485 PROC_LOCK_ASSERT(p, MA_OWNED);
1486 return (cr_cansee(td->td_ucred, p->p_ucred));
1490 * 'conservative_signals' prevents the delivery of a broad class of
1491 * signals by unprivileged processes to processes that have changed their
1492 * credentials since the last invocation of execve(). This can prevent
1493 * the leakage of cached information or retained privileges as a result
1494 * of a common class of signal-related vulnerabilities. However, this
1495 * may interfere with some applications that expect to be able to
1496 * deliver these signals to peer processes after having given up
1499 static int conservative_signals = 1;
1500 SYSCTL_INT(_security_bsd, OID_AUTO, conservative_signals, CTLFLAG_RW,
1501 &conservative_signals, 0, "Unprivileged processes prevented from "
1502 "sending certain signals to processes whose credentials have changed");
1504 * Determine whether cred may deliver the specified signal to proc.
1505 * Returns: 0 for permitted, an errno value otherwise.
1506 * Locks: A lock must be held for proc.
1507 * References: cred and proc must be valid for the lifetime of the call.
1510 cr_cansignal(struct ucred *cred, struct proc *proc, int signum)
1514 PROC_LOCK_ASSERT(proc, MA_OWNED);
1516 * Jail semantics limit the scope of signalling to proc in the
1517 * same jail as cred, if cred is in jail.
1519 error = prison_check(cred, proc->p_ucred);
1523 if ((error = mac_check_proc_signal(cred, proc, signum)))
1526 if ((error = cr_seeotheruids(cred, proc->p_ucred)))
1528 if ((error = cr_seeothergids(cred, proc->p_ucred)))
1532 * UNIX signal semantics depend on the status of the P_SUGID
1533 * bit on the target process. If the bit is set, then additional
1534 * restrictions are placed on the set of available signals.
1536 if (conservative_signals && (proc->p_flag & P_SUGID)) {
1551 * Generally, permit job and terminal control
1556 /* Not permitted without privilege. */
1557 error = priv_check_cred(cred, PRIV_SIGNAL_SUGID,
1565 * Generally, the target credential's ruid or svuid must match the
1566 * subject credential's ruid or euid.
1568 if (cred->cr_ruid != proc->p_ucred->cr_ruid &&
1569 cred->cr_ruid != proc->p_ucred->cr_svuid &&
1570 cred->cr_uid != proc->p_ucred->cr_ruid &&
1571 cred->cr_uid != proc->p_ucred->cr_svuid) {
1572 /* Not permitted without privilege. */
1573 error = priv_check_cred(cred, PRIV_SIGNAL_DIFFCRED,
1583 * Determine whether td may deliver the specified signal to p.
1584 * Returns: 0 for permitted, an errno value otherwise
1585 * Locks: Sufficient locks to protect various components of td and p
1586 * must be held. td must be curthread, and a lock must be
1588 * References: td and p must be valid for the lifetime of the call
1591 p_cansignal(struct thread *td, struct proc *p, int signum)
1594 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1595 PROC_LOCK_ASSERT(p, MA_OWNED);
1596 if (td->td_proc == p)
1600 * UNIX signalling semantics require that processes in the same
1601 * session always be able to deliver SIGCONT to one another,
1602 * overriding the remaining protections.
1604 /* XXX: This will require an additional lock of some sort. */
1605 if (signum == SIGCONT && td->td_proc->p_session == p->p_session)
1608 * Some compat layers use SIGTHR and higher signals for
1609 * communication between different kernel threads of the same
1610 * process, so that they expect that it's always possible to
1611 * deliver them, even for suid applications where cr_cansignal() can
1612 * deny such ability for security consideration. It should be
1613 * pretty safe to do since the only way to create two processes
1614 * with the same p_leader is via rfork(2).
1616 if (td->td_proc->p_leader != NULL && signum >= SIGTHR &&
1617 signum < SIGTHR + 4 && td->td_proc->p_leader == p->p_leader)
1620 return (cr_cansignal(td->td_ucred, p, signum));
1624 * Determine whether td may reschedule p.
1625 * Returns: 0 for permitted, an errno value otherwise
1626 * Locks: Sufficient locks to protect various components of td and p
1627 * must be held. td must be curthread, and a lock must
1629 * References: td and p must be valid for the lifetime of the call
1632 p_cansched(struct thread *td, struct proc *p)
1636 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1637 PROC_LOCK_ASSERT(p, MA_OWNED);
1638 if (td->td_proc == p)
1640 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1643 if ((error = mac_check_proc_sched(td->td_ucred, p)))
1646 if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1648 if ((error = cr_seeothergids(td->td_ucred, p->p_ucred)))
1650 if (td->td_ucred->cr_ruid != p->p_ucred->cr_ruid &&
1651 td->td_ucred->cr_uid != p->p_ucred->cr_ruid) {
1652 error = priv_check_cred(td->td_ucred, PRIV_SCHED_DIFFCRED,
1661 * The 'unprivileged_proc_debug' flag may be used to disable a variety of
1662 * unprivileged inter-process debugging services, including some procfs
1663 * functionality, ptrace(), and ktrace(). In the past, inter-process
1664 * debugging has been involved in a variety of security problems, and sites
1665 * not requiring the service might choose to disable it when hardening
1668 * XXX: Should modifying and reading this variable require locking?
1669 * XXX: data declarations should be together near the beginning of the file.
1671 static int unprivileged_proc_debug = 1;
1672 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_proc_debug, CTLFLAG_RW,
1673 &unprivileged_proc_debug, 0,
1674 "Unprivileged processes may use process debugging facilities");
1677 * Determine whether td may debug p.
1678 * Returns: 0 for permitted, an errno value otherwise
1679 * Locks: Sufficient locks to protect various components of td and p
1680 * must be held. td must be curthread, and a lock must
1682 * References: td and p must be valid for the lifetime of the call
1685 p_candebug(struct thread *td, struct proc *p)
1687 int credentialchanged, error, grpsubset, i, uidsubset;
1689 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1690 PROC_LOCK_ASSERT(p, MA_OWNED);
1691 if (!unprivileged_proc_debug) {
1692 error = priv_check_cred(td->td_ucred, PRIV_DEBUG_UNPRIV,
1697 if (td->td_proc == p)
1699 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1702 if ((error = mac_check_proc_debug(td->td_ucred, p)))
1705 if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1707 if ((error = cr_seeothergids(td->td_ucred, p->p_ucred)))
1711 * Is p's group set a subset of td's effective group set? This
1712 * includes p's egid, group access list, rgid, and svgid.
1715 for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
1716 if (!groupmember(p->p_ucred->cr_groups[i], td->td_ucred)) {
1721 grpsubset = grpsubset &&
1722 groupmember(p->p_ucred->cr_rgid, td->td_ucred) &&
1723 groupmember(p->p_ucred->cr_svgid, td->td_ucred);
1726 * Are the uids present in p's credential equal to td's
1727 * effective uid? This includes p's euid, svuid, and ruid.
1729 uidsubset = (td->td_ucred->cr_uid == p->p_ucred->cr_uid &&
1730 td->td_ucred->cr_uid == p->p_ucred->cr_svuid &&
1731 td->td_ucred->cr_uid == p->p_ucred->cr_ruid);
1734 * Has the credential of the process changed since the last exec()?
1736 credentialchanged = (p->p_flag & P_SUGID);
1739 * If p's gids aren't a subset, or the uids aren't a subset,
1740 * or the credential has changed, require appropriate privilege
1741 * for td to debug p.
1743 if (!grpsubset || !uidsubset) {
1744 error = priv_check_cred(td->td_ucred, PRIV_DEBUG_DIFFCRED,
1750 if (credentialchanged) {
1751 error = priv_check_cred(td->td_ucred, PRIV_DEBUG_SUGID,
1757 /* Can't trace init when securelevel > 0. */
1758 if (p == initproc) {
1759 error = securelevel_gt(td->td_ucred, 0);
1765 * Can't trace a process that's currently exec'ing.
1767 * XXX: Note, this is not a security policy decision, it's a
1768 * basic correctness/functionality decision. Therefore, this check
1769 * should be moved to the caller's of p_candebug().
1771 if ((p->p_flag & P_INEXEC) != 0)
1778 * Determine whether the subject represented by cred can "see" a socket.
1779 * Returns: 0 for permitted, ENOENT otherwise.
1782 cr_canseesocket(struct ucred *cred, struct socket *so)
1786 error = prison_check(cred, so->so_cred);
1791 error = mac_check_socket_visible(cred, so);
1796 if (cr_seeotheruids(cred, so->so_cred))
1798 if (cr_seeothergids(cred, so->so_cred))
1805 * Determine whether td can wait for the exit of p.
1806 * Returns: 0 for permitted, an errno value otherwise
1807 * Locks: Sufficient locks to protect various components of td and p
1808 * must be held. td must be curthread, and a lock must
1810 * References: td and p must be valid for the lifetime of the call
1814 p_canwait(struct thread *td, struct proc *p)
1818 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1819 PROC_LOCK_ASSERT(p, MA_OWNED);
1820 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1823 if ((error = mac_check_proc_wait(td->td_ucred, p)))
1827 /* XXXMAC: This could have odd effects on some shells. */
1828 if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1836 * Allocate a zeroed cred structure.
1842 register struct ucred *cr;
1844 MALLOC(cr, struct ucred *, sizeof(*cr), M_CRED, M_WAITOK | M_ZERO);
1845 refcount_init(&cr->cr_ref, 1);
1853 * Claim another reference to a ucred structure.
1857 crhold(struct ucred *cr)
1860 refcount_acquire(&cr->cr_ref);
1865 * Free a cred structure.
1866 * Throws away space when ref count gets to 0.
1870 crfree(struct ucred *cr)
1873 KASSERT(cr->cr_ref > 0, ("bad ucred refcount: %d", cr->cr_ref));
1874 KASSERT(cr->cr_ref != 0xdeadc0de, ("dangling reference to ucred"));
1875 if (refcount_release(&cr->cr_ref)) {
1877 * Some callers of crget(), such as nfs_statfs(),
1878 * allocate a temporary credential, but don't
1879 * allocate a uidinfo structure.
1881 if (cr->cr_uidinfo != NULL)
1882 uifree(cr->cr_uidinfo);
1883 if (cr->cr_ruidinfo != NULL)
1884 uifree(cr->cr_ruidinfo);
1886 * Free a prison, if any.
1889 prison_free(cr->cr_prison);
1891 mac_destroy_cred(cr);
1898 * Check to see if this ucred is shared.
1902 crshared(struct ucred *cr)
1905 return (cr->cr_ref > 1);
1909 * Copy a ucred's contents from a template. Does not block.
1913 crcopy(struct ucred *dest, struct ucred *src)
1916 KASSERT(crshared(dest) == 0, ("crcopy of shared ucred"));
1917 bcopy(&src->cr_startcopy, &dest->cr_startcopy,
1918 (unsigned)((caddr_t)&src->cr_endcopy -
1919 (caddr_t)&src->cr_startcopy));
1920 uihold(dest->cr_uidinfo);
1921 uihold(dest->cr_ruidinfo);
1923 prison_hold(dest->cr_prison);
1925 mac_copy_cred(src, dest);
1930 * Dup cred struct to a new held one.
1934 crdup(struct ucred *cr)
1936 struct ucred *newcr;
1944 * Fill in a struct xucred based on a struct ucred.
1948 cru2x(struct ucred *cr, struct xucred *xcr)
1951 bzero(xcr, sizeof(*xcr));
1952 xcr->cr_version = XUCRED_VERSION;
1953 xcr->cr_uid = cr->cr_uid;
1954 xcr->cr_ngroups = cr->cr_ngroups;
1955 bcopy(cr->cr_groups, xcr->cr_groups, sizeof(cr->cr_groups));
1959 * small routine to swap a thread's current ucred for the correct one
1960 * taken from the process.
1964 cred_update_thread(struct thread *td)
1970 cred = td->td_ucred;
1972 td->td_ucred = crhold(p->p_ucred);
1979 * Get login name, if available.
1981 #ifndef _SYS_SYSPROTO_H_
1982 struct getlogin_args {
1992 getlogin(struct thread *td, struct getlogin_args *uap)
1995 char login[MAXLOGNAME];
1996 struct proc *p = td->td_proc;
1998 if (uap->namelen > MAXLOGNAME)
1999 uap->namelen = MAXLOGNAME;
2001 SESS_LOCK(p->p_session);
2002 bcopy(p->p_session->s_login, login, uap->namelen);
2003 SESS_UNLOCK(p->p_session);
2005 error = copyout(login, uap->namebuf, uap->namelen);
2012 #ifndef _SYS_SYSPROTO_H_
2013 struct setlogin_args {
2022 setlogin(struct thread *td, struct setlogin_args *uap)
2024 struct proc *p = td->td_proc;
2026 char logintmp[MAXLOGNAME];
2028 error = priv_check_cred(td->td_ucred, PRIV_PROC_SETLOGIN,
2032 error = copyinstr(uap->namebuf, logintmp, sizeof(logintmp), NULL);
2033 if (error == ENAMETOOLONG)
2037 SESS_LOCK(p->p_session);
2038 (void) memcpy(p->p_session->s_login, logintmp,
2040 SESS_UNLOCK(p->p_session);
2047 setsugid(struct proc *p)
2050 PROC_LOCK_ASSERT(p, MA_OWNED);
2051 p->p_flag |= P_SUGID;
2052 if (!(p->p_pfsflags & PF_ISUGID))
2057 * Change a process's effective uid.
2058 * Side effects: newcred->cr_uid and newcred->cr_uidinfo will be modified.
2059 * References: newcred must be an exclusive credential reference for the
2060 * duration of the call.
2063 change_euid(struct ucred *newcred, struct uidinfo *euip)
2066 newcred->cr_uid = euip->ui_uid;
2068 uifree(newcred->cr_uidinfo);
2069 newcred->cr_uidinfo = euip;
2073 * Change a process's effective gid.
2074 * Side effects: newcred->cr_gid will be modified.
2075 * References: newcred must be an exclusive credential reference for the
2076 * duration of the call.
2079 change_egid(struct ucred *newcred, gid_t egid)
2082 newcred->cr_groups[0] = egid;
2086 * Change a process's real uid.
2087 * Side effects: newcred->cr_ruid will be updated, newcred->cr_ruidinfo
2088 * will be updated, and the old and new cr_ruidinfo proc
2089 * counts will be updated.
2090 * References: newcred must be an exclusive credential reference for the
2091 * duration of the call.
2094 change_ruid(struct ucred *newcred, struct uidinfo *ruip)
2097 (void)chgproccnt(newcred->cr_ruidinfo, -1, 0);
2098 newcred->cr_ruid = ruip->ui_uid;
2100 uifree(newcred->cr_ruidinfo);
2101 newcred->cr_ruidinfo = ruip;
2102 (void)chgproccnt(newcred->cr_ruidinfo, 1, 0);
2106 * Change a process's real gid.
2107 * Side effects: newcred->cr_rgid will be updated.
2108 * References: newcred must be an exclusive credential reference for the
2109 * duration of the call.
2112 change_rgid(struct ucred *newcred, gid_t rgid)
2115 newcred->cr_rgid = rgid;
2119 * Change a process's saved uid.
2120 * Side effects: newcred->cr_svuid will be updated.
2121 * References: newcred must be an exclusive credential reference for the
2122 * duration of the call.
2125 change_svuid(struct ucred *newcred, uid_t svuid)
2128 newcred->cr_svuid = svuid;
2132 * Change a process's saved gid.
2133 * Side effects: newcred->cr_svgid will be updated.
2134 * References: newcred must be an exclusive credential reference for the
2135 * duration of the call.
2138 change_svgid(struct ucred *newcred, gid_t svgid)
2141 newcred->cr_svgid = svgid;