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"
50 #include <sys/param.h>
51 #include <sys/systm.h>
54 #include <sys/kernel.h>
56 #include <sys/malloc.h>
57 #include <sys/mutex.h>
58 #include <sys/refcount.h>
62 #include <sys/sysproto.h>
64 #include <sys/pioctl.h>
65 #include <sys/resourcevar.h>
66 #include <sys/socket.h>
67 #include <sys/socketvar.h>
68 #include <sys/syscallsubr.h>
69 #include <sys/sysctl.h>
71 #include <security/audit/audit.h>
72 #include <security/mac/mac_framework.h>
74 static MALLOC_DEFINE(M_CRED, "cred", "credentials");
76 SYSCTL_NODE(_security, OID_AUTO, bsd, CTLFLAG_RW, 0, "BSD security policy");
78 #ifndef _SYS_SYSPROTO_H_
88 getpid(struct thread *td, struct getpid_args *uap)
90 struct proc *p = td->td_proc;
92 td->td_retval[0] = p->p_pid;
93 #if defined(COMPAT_43)
95 td->td_retval[1] = p->p_pptr->p_pid;
101 #ifndef _SYS_SYSPROTO_H_
102 struct getppid_args {
111 getppid(struct thread *td, struct getppid_args *uap)
113 struct proc *p = td->td_proc;
116 td->td_retval[0] = p->p_pptr->p_pid;
122 * Get process group ID; note that POSIX getpgrp takes no parameter.
124 #ifndef _SYS_SYSPROTO_H_
125 struct getpgrp_args {
133 getpgrp(struct thread *td, struct getpgrp_args *uap)
135 struct proc *p = td->td_proc;
138 td->td_retval[0] = p->p_pgrp->pg_id;
143 /* Get an arbitary pid's process group id */
144 #ifndef _SYS_SYSPROTO_H_
145 struct getpgid_args {
153 getpgid(struct thread *td, struct getpgid_args *uap)
165 error = p_cansee(td, p);
171 td->td_retval[0] = p->p_pgrp->pg_id;
177 * Get an arbitary pid's session id.
179 #ifndef _SYS_SYSPROTO_H_
188 getsid(struct thread *td, struct getsid_args *uap)
200 error = p_cansee(td, p);
206 td->td_retval[0] = p->p_session->s_sid;
211 #ifndef _SYS_SYSPROTO_H_
221 getuid(struct thread *td, struct getuid_args *uap)
224 td->td_retval[0] = td->td_ucred->cr_ruid;
225 #if defined(COMPAT_43)
226 td->td_retval[1] = td->td_ucred->cr_uid;
231 #ifndef _SYS_SYSPROTO_H_
232 struct geteuid_args {
241 geteuid(struct thread *td, struct geteuid_args *uap)
244 td->td_retval[0] = td->td_ucred->cr_uid;
248 #ifndef _SYS_SYSPROTO_H_
258 getgid(struct thread *td, struct getgid_args *uap)
261 td->td_retval[0] = td->td_ucred->cr_rgid;
262 #if defined(COMPAT_43)
263 td->td_retval[1] = td->td_ucred->cr_groups[0];
269 * Get effective group ID. The "egid" is groups[0], and could be obtained
270 * via getgroups. This syscall exists because it is somewhat painful to do
271 * correctly in a library function.
273 #ifndef _SYS_SYSPROTO_H_
274 struct getegid_args {
283 getegid(struct thread *td, struct getegid_args *uap)
286 td->td_retval[0] = td->td_ucred->cr_groups[0];
290 #ifndef _SYS_SYSPROTO_H_
291 struct getgroups_args {
300 getgroups(struct thread *td, register struct getgroups_args *uap)
302 gid_t groups[NGROUPS];
306 ngrp = MIN(uap->gidsetsize, NGROUPS);
307 error = kern_getgroups(td, &ngrp, groups);
310 if (uap->gidsetsize > 0)
311 error = copyout(groups, uap->gidset, ngrp * sizeof(gid_t));
313 td->td_retval[0] = ngrp;
318 kern_getgroups(struct thread *td, u_int *ngrp, gid_t *groups)
324 *ngrp = cred->cr_ngroups;
327 if (*ngrp < cred->cr_ngroups)
329 *ngrp = cred->cr_ngroups;
330 bcopy(cred->cr_groups, groups, *ngrp * sizeof(gid_t));
334 #ifndef _SYS_SYSPROTO_H_
344 setsid(register struct thread *td, struct setsid_args *uap)
348 struct proc *p = td->td_proc;
349 struct pgrp *newpgrp;
350 struct session *newsess;
355 MALLOC(newpgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
356 MALLOC(newsess, struct session *, sizeof(struct session), M_SESSION, M_WAITOK | M_ZERO);
358 sx_xlock(&proctree_lock);
360 if (p->p_pgid == p->p_pid || (pgrp = pgfind(p->p_pid)) != NULL) {
365 (void)enterpgrp(p, p->p_pid, newpgrp, newsess);
366 td->td_retval[0] = p->p_pid;
371 sx_xunlock(&proctree_lock);
374 FREE(newpgrp, M_PGRP);
376 FREE(newsess, M_SESSION);
382 * set process group (setpgid/old setpgrp)
384 * caller does setpgid(targpid, targpgid)
386 * pid must be caller or child of caller (ESRCH)
388 * pid must be in same session (EPERM)
389 * pid can't have done an exec (EACCES)
391 * there must exist some pid in same session having pgid (EPERM)
392 * pid must not be session leader (EPERM)
394 #ifndef _SYS_SYSPROTO_H_
395 struct setpgid_args {
396 int pid; /* target process id */
397 int pgid; /* target pgrp id */
405 setpgid(struct thread *td, register struct setpgid_args *uap)
407 struct proc *curp = td->td_proc;
408 register struct proc *targp; /* target process */
409 register struct pgrp *pgrp; /* target pgrp */
411 struct pgrp *newpgrp;
418 MALLOC(newpgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
420 sx_xlock(&proctree_lock);
421 if (uap->pid != 0 && uap->pid != curp->p_pid) {
422 if ((targp = pfind(uap->pid)) == NULL) {
426 if (!inferior(targp)) {
431 if ((error = p_cansee(td, targp))) {
435 if (targp->p_pgrp == NULL ||
436 targp->p_session != curp->p_session) {
441 if (targp->p_flag & P_EXEC) {
449 if (SESS_LEADER(targp)) {
454 uap->pgid = targp->p_pid;
455 if ((pgrp = pgfind(uap->pgid)) == NULL) {
456 if (uap->pgid == targp->p_pid) {
457 error = enterpgrp(targp, uap->pgid, newpgrp,
464 if (pgrp == targp->p_pgrp) {
468 if (pgrp->pg_id != targp->p_pid &&
469 pgrp->pg_session != curp->p_session) {
475 error = enterthispgrp(targp, pgrp);
478 sx_xunlock(&proctree_lock);
479 KASSERT((error == 0) || (newpgrp != NULL),
480 ("setpgid failed and newpgrp is NULL"));
482 FREE(newpgrp, M_PGRP);
487 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
488 * compatible. It says that setting the uid/gid to euid/egid is a special
489 * case of "appropriate privilege". Once the rules are expanded out, this
490 * basically means that setuid(nnn) sets all three id's, in all permitted
491 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
492 * does not set the saved id - this is dangerous for traditional BSD
493 * programs. For this reason, we *really* do not want to set
494 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
496 #define POSIX_APPENDIX_B_4_2_2
498 #ifndef _SYS_SYSPROTO_H_
508 setuid(struct thread *td, struct setuid_args *uap)
510 struct proc *p = td->td_proc;
511 struct ucred *newcred, *oldcred;
521 oldcred = p->p_ucred;
524 error = mac_check_proc_setuid(p, oldcred, uid);
530 * See if we have "permission" by POSIX 1003.1 rules.
532 * Note that setuid(geteuid()) is a special case of
533 * "appropriate privileges" in appendix B.4.2.2. We need
534 * to use this clause to be compatible with traditional BSD
535 * semantics. Basically, it means that "setuid(xx)" sets all
536 * three id's (assuming you have privs).
538 * Notes on the logic. We do things in three steps.
539 * 1: We determine if the euid is going to change, and do EPERM
540 * right away. We unconditionally change the euid later if this
541 * test is satisfied, simplifying that part of the logic.
542 * 2: We determine if the real and/or saved uids are going to
543 * change. Determined by compile options.
544 * 3: Change euid last. (after tests in #2 for "appropriate privs")
546 if (uid != oldcred->cr_ruid && /* allow setuid(getuid()) */
547 #ifdef _POSIX_SAVED_IDS
548 uid != oldcred->cr_svuid && /* allow setuid(saved gid) */
550 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
551 uid != oldcred->cr_uid && /* allow setuid(geteuid()) */
553 (error = priv_check_cred(oldcred, PRIV_CRED_SETUID,
554 SUSER_ALLOWJAIL)) != 0)
558 * Copy credentials so other references do not see our changes.
560 crcopy(newcred, oldcred);
561 #ifdef _POSIX_SAVED_IDS
563 * Do we have "appropriate privileges" (are we root or uid == euid)
564 * If so, we are changing the real uid and/or saved uid.
567 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
568 uid == oldcred->cr_uid ||
570 /* We are using privs. */
571 priv_check_cred(oldcred, PRIV_CRED_SETUID, SUSER_ALLOWJAIL) == 0)
575 * Set the real uid and transfer proc count to new user.
577 if (uid != oldcred->cr_ruid) {
578 change_ruid(newcred, uip);
584 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
585 * the security of seteuid() depends on it. B.4.2.2 says it
586 * is important that we should do this.
588 if (uid != oldcred->cr_svuid) {
589 change_svuid(newcred, uid);
595 * In all permitted cases, we are changing the euid.
597 if (uid != oldcred->cr_uid) {
598 change_euid(newcred, uip);
601 p->p_ucred = newcred;
614 #ifndef _SYS_SYSPROTO_H_
615 struct seteuid_args {
624 seteuid(struct thread *td, struct seteuid_args *uap)
626 struct proc *p = td->td_proc;
627 struct ucred *newcred, *oldcred;
629 struct uidinfo *euip;
633 AUDIT_ARG(euid, euid);
637 oldcred = p->p_ucred;
640 error = mac_check_proc_seteuid(p, oldcred, euid);
645 if (euid != oldcred->cr_ruid && /* allow seteuid(getuid()) */
646 euid != oldcred->cr_svuid && /* allow seteuid(saved uid) */
647 (error = priv_check_cred(oldcred, PRIV_CRED_SETEUID,
648 SUSER_ALLOWJAIL)) != 0)
652 * Everything's okay, do it. Copy credentials so other references do
653 * not see our changes.
655 crcopy(newcred, oldcred);
656 if (oldcred->cr_uid != euid) {
657 change_euid(newcred, euip);
660 p->p_ucred = newcred;
673 #ifndef _SYS_SYSPROTO_H_
683 setgid(struct thread *td, struct setgid_args *uap)
685 struct proc *p = td->td_proc;
686 struct ucred *newcred, *oldcred;
694 oldcred = p->p_ucred;
697 error = mac_check_proc_setgid(p, oldcred, gid);
703 * See if we have "permission" by POSIX 1003.1 rules.
705 * Note that setgid(getegid()) is a special case of
706 * "appropriate privileges" in appendix B.4.2.2. We need
707 * to use this clause to be compatible with traditional BSD
708 * semantics. Basically, it means that "setgid(xx)" sets all
709 * three id's (assuming you have privs).
711 * For notes on the logic here, see setuid() above.
713 if (gid != oldcred->cr_rgid && /* allow setgid(getgid()) */
714 #ifdef _POSIX_SAVED_IDS
715 gid != oldcred->cr_svgid && /* allow setgid(saved gid) */
717 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
718 gid != oldcred->cr_groups[0] && /* allow setgid(getegid()) */
720 (error = priv_check_cred(oldcred, PRIV_CRED_SETGID,
721 SUSER_ALLOWJAIL)) != 0)
724 crcopy(newcred, oldcred);
725 #ifdef _POSIX_SAVED_IDS
727 * Do we have "appropriate privileges" (are we root or gid == egid)
728 * If so, we are changing the real uid and saved gid.
731 #ifdef POSIX_APPENDIX_B_4_2_2 /* use the clause from B.4.2.2 */
732 gid == oldcred->cr_groups[0] ||
734 /* We are using privs. */
735 priv_check_cred(oldcred, PRIV_CRED_SETGID, SUSER_ALLOWJAIL) == 0)
741 if (oldcred->cr_rgid != gid) {
742 change_rgid(newcred, gid);
748 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
749 * the security of setegid() depends on it. B.4.2.2 says it
750 * is important that we should do this.
752 if (oldcred->cr_svgid != gid) {
753 change_svgid(newcred, gid);
758 * In all cases permitted cases, we are changing the egid.
759 * Copy credentials so other references do not see our changes.
761 if (oldcred->cr_groups[0] != gid) {
762 change_egid(newcred, gid);
765 p->p_ucred = newcred;
776 #ifndef _SYS_SYSPROTO_H_
777 struct setegid_args {
786 setegid(struct thread *td, struct setegid_args *uap)
788 struct proc *p = td->td_proc;
789 struct ucred *newcred, *oldcred;
794 AUDIT_ARG(egid, egid);
797 oldcred = p->p_ucred;
800 error = mac_check_proc_setegid(p, oldcred, egid);
805 if (egid != oldcred->cr_rgid && /* allow setegid(getgid()) */
806 egid != oldcred->cr_svgid && /* allow setegid(saved gid) */
807 (error = priv_check_cred(oldcred, PRIV_CRED_SETEGID,
808 SUSER_ALLOWJAIL)) != 0)
811 crcopy(newcred, oldcred);
812 if (oldcred->cr_groups[0] != egid) {
813 change_egid(newcred, egid);
816 p->p_ucred = newcred;
827 #ifndef _SYS_SYSPROTO_H_
828 struct setgroups_args {
838 setgroups(struct thread *td, struct setgroups_args *uap)
840 gid_t groups[NGROUPS];
843 if (uap->gidsetsize > NGROUPS)
845 error = copyin(uap->gidset, groups, uap->gidsetsize * sizeof(gid_t));
848 return (kern_setgroups(td, uap->gidsetsize, groups));
852 kern_setgroups(struct thread *td, u_int ngrp, gid_t *groups)
854 struct proc *p = td->td_proc;
855 struct ucred *newcred, *oldcred;
860 AUDIT_ARG(groupset, groups, ngrp);
863 oldcred = p->p_ucred;
866 error = mac_check_proc_setgroups(p, oldcred, ngrp, groups);
871 error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS,
877 * XXX A little bit lazy here. We could test if anything has
878 * changed before crcopy() and setting P_SUGID.
880 crcopy(newcred, oldcred);
883 * setgroups(0, NULL) is a legitimate way of clearing the
884 * groups vector on non-BSD systems (which generally do not
885 * have the egid in the groups[0]). We risk security holes
886 * when running non-BSD software if we do not do the same.
888 newcred->cr_ngroups = 1;
890 bcopy(groups, newcred->cr_groups, ngrp * sizeof(gid_t));
891 newcred->cr_ngroups = ngrp;
894 p->p_ucred = newcred;
905 #ifndef _SYS_SYSPROTO_H_
906 struct setreuid_args {
916 setreuid(register struct thread *td, struct setreuid_args *uap)
918 struct proc *p = td->td_proc;
919 struct ucred *newcred, *oldcred;
921 struct uidinfo *euip, *ruip;
926 AUDIT_ARG(euid, euid);
927 AUDIT_ARG(ruid, ruid);
932 oldcred = p->p_ucred;
935 error = mac_check_proc_setreuid(p, oldcred, ruid, euid);
940 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
941 ruid != oldcred->cr_svuid) ||
942 (euid != (uid_t)-1 && euid != oldcred->cr_uid &&
943 euid != oldcred->cr_ruid && euid != oldcred->cr_svuid)) &&
944 (error = priv_check_cred(oldcred, PRIV_CRED_SETREUID,
945 SUSER_ALLOWJAIL)) != 0)
948 crcopy(newcred, oldcred);
949 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
950 change_euid(newcred, euip);
953 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
954 change_ruid(newcred, ruip);
957 if ((ruid != (uid_t)-1 || newcred->cr_uid != newcred->cr_ruid) &&
958 newcred->cr_svuid != newcred->cr_uid) {
959 change_svuid(newcred, newcred->cr_uid);
962 p->p_ucred = newcred;
977 #ifndef _SYS_SYSPROTO_H_
978 struct setregid_args {
988 setregid(register struct thread *td, struct setregid_args *uap)
990 struct proc *p = td->td_proc;
991 struct ucred *newcred, *oldcred;
997 AUDIT_ARG(egid, egid);
998 AUDIT_ARG(rgid, rgid);
1001 oldcred = p->p_ucred;
1004 error = mac_check_proc_setregid(p, oldcred, rgid, egid);
1009 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
1010 rgid != oldcred->cr_svgid) ||
1011 (egid != (gid_t)-1 && egid != oldcred->cr_groups[0] &&
1012 egid != oldcred->cr_rgid && egid != oldcred->cr_svgid)) &&
1013 (error = priv_check_cred(oldcred, PRIV_CRED_SETREGID,
1014 SUSER_ALLOWJAIL)) != 0)
1017 crcopy(newcred, oldcred);
1018 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
1019 change_egid(newcred, egid);
1022 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
1023 change_rgid(newcred, rgid);
1026 if ((rgid != (gid_t)-1 || newcred->cr_groups[0] != newcred->cr_rgid) &&
1027 newcred->cr_svgid != newcred->cr_groups[0]) {
1028 change_svgid(newcred, newcred->cr_groups[0]);
1031 p->p_ucred = newcred;
1043 * setresuid(ruid, euid, suid) is like setreuid except control over the
1044 * saved uid is explicit.
1047 #ifndef _SYS_SYSPROTO_H_
1048 struct setresuid_args {
1059 setresuid(register struct thread *td, struct setresuid_args *uap)
1061 struct proc *p = td->td_proc;
1062 struct ucred *newcred, *oldcred;
1063 uid_t euid, ruid, suid;
1064 struct uidinfo *euip, *ruip;
1070 AUDIT_ARG(euid, euid);
1071 AUDIT_ARG(ruid, ruid);
1072 AUDIT_ARG(suid, suid);
1074 euip = uifind(euid);
1075 ruip = uifind(ruid);
1077 oldcred = p->p_ucred;
1080 error = mac_check_proc_setresuid(p, oldcred, ruid, euid, suid);
1085 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
1086 ruid != oldcred->cr_svuid &&
1087 ruid != oldcred->cr_uid) ||
1088 (euid != (uid_t)-1 && euid != oldcred->cr_ruid &&
1089 euid != oldcred->cr_svuid &&
1090 euid != oldcred->cr_uid) ||
1091 (suid != (uid_t)-1 && suid != oldcred->cr_ruid &&
1092 suid != oldcred->cr_svuid &&
1093 suid != oldcred->cr_uid)) &&
1094 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESUID,
1095 SUSER_ALLOWJAIL)) != 0)
1098 crcopy(newcred, oldcred);
1099 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
1100 change_euid(newcred, euip);
1103 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
1104 change_ruid(newcred, ruip);
1107 if (suid != (uid_t)-1 && oldcred->cr_svuid != suid) {
1108 change_svuid(newcred, suid);
1111 p->p_ucred = newcred;
1128 * setresgid(rgid, egid, sgid) is like setregid except control over the
1129 * saved gid is explicit.
1132 #ifndef _SYS_SYSPROTO_H_
1133 struct setresgid_args {
1144 setresgid(register struct thread *td, struct setresgid_args *uap)
1146 struct proc *p = td->td_proc;
1147 struct ucred *newcred, *oldcred;
1148 gid_t egid, rgid, sgid;
1154 AUDIT_ARG(egid, egid);
1155 AUDIT_ARG(rgid, rgid);
1156 AUDIT_ARG(sgid, sgid);
1159 oldcred = p->p_ucred;
1162 error = mac_check_proc_setresgid(p, oldcred, rgid, egid, sgid);
1167 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
1168 rgid != oldcred->cr_svgid &&
1169 rgid != oldcred->cr_groups[0]) ||
1170 (egid != (gid_t)-1 && egid != oldcred->cr_rgid &&
1171 egid != oldcred->cr_svgid &&
1172 egid != oldcred->cr_groups[0]) ||
1173 (sgid != (gid_t)-1 && sgid != oldcred->cr_rgid &&
1174 sgid != oldcred->cr_svgid &&
1175 sgid != oldcred->cr_groups[0])) &&
1176 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESGID,
1177 SUSER_ALLOWJAIL)) != 0)
1180 crcopy(newcred, oldcred);
1181 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
1182 change_egid(newcred, egid);
1185 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
1186 change_rgid(newcred, rgid);
1189 if (sgid != (gid_t)-1 && oldcred->cr_svgid != sgid) {
1190 change_svgid(newcred, sgid);
1193 p->p_ucred = newcred;
1204 #ifndef _SYS_SYSPROTO_H_
1205 struct getresuid_args {
1216 getresuid(register struct thread *td, struct getresuid_args *uap)
1219 int error1 = 0, error2 = 0, error3 = 0;
1221 cred = td->td_ucred;
1223 error1 = copyout(&cred->cr_ruid,
1224 uap->ruid, sizeof(cred->cr_ruid));
1226 error2 = copyout(&cred->cr_uid,
1227 uap->euid, sizeof(cred->cr_uid));
1229 error3 = copyout(&cred->cr_svuid,
1230 uap->suid, sizeof(cred->cr_svuid));
1231 return (error1 ? error1 : error2 ? error2 : error3);
1234 #ifndef _SYS_SYSPROTO_H_
1235 struct getresgid_args {
1246 getresgid(register struct thread *td, struct getresgid_args *uap)
1249 int error1 = 0, error2 = 0, error3 = 0;
1251 cred = td->td_ucred;
1253 error1 = copyout(&cred->cr_rgid,
1254 uap->rgid, sizeof(cred->cr_rgid));
1256 error2 = copyout(&cred->cr_groups[0],
1257 uap->egid, sizeof(cred->cr_groups[0]));
1259 error3 = copyout(&cred->cr_svgid,
1260 uap->sgid, sizeof(cred->cr_svgid));
1261 return (error1 ? error1 : error2 ? error2 : error3);
1264 #ifndef _SYS_SYSPROTO_H_
1265 struct issetugid_args {
1274 issetugid(register struct thread *td, struct issetugid_args *uap)
1276 struct proc *p = td->td_proc;
1279 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
1280 * we use P_SUGID because we consider changing the owners as
1281 * "tainting" as well.
1282 * This is significant for procs that start as root and "become"
1283 * a user without an exec - programs cannot know *everything*
1284 * that libc *might* have put in their data segment.
1287 td->td_retval[0] = (p->p_flag & P_SUGID) ? 1 : 0;
1296 __setugid(struct thread *td, struct __setugid_args *uap)
1302 switch (uap->flag) {
1305 p->p_flag &= ~P_SUGID;
1310 p->p_flag |= P_SUGID;
1316 #else /* !REGRESSION */
1319 #endif /* REGRESSION */
1323 * Check if gid is a member of the group set.
1325 * MPSAFE (cred must be held)
1328 groupmember(gid_t gid, struct ucred *cred)
1333 egp = &(cred->cr_groups[cred->cr_ngroups]);
1334 for (gp = cred->cr_groups; gp < egp; gp++)
1341 * Test the active securelevel against a given level. securelevel_gt()
1342 * implements (securelevel > level). securelevel_ge() implements
1343 * (securelevel >= level). Note that the logic is inverted -- these
1344 * functions return EPERM on "success" and 0 on "failure".
1346 * XXXRW: Possibly since this has to do with privilege, it should move to
1352 securelevel_gt(struct ucred *cr, int level)
1354 int active_securelevel;
1356 active_securelevel = securelevel;
1357 KASSERT(cr != NULL, ("securelevel_gt: null cr"));
1358 if (cr->cr_prison != NULL)
1359 active_securelevel = imax(cr->cr_prison->pr_securelevel,
1360 active_securelevel);
1361 return (active_securelevel > level ? EPERM : 0);
1365 securelevel_ge(struct ucred *cr, int level)
1367 int active_securelevel;
1369 active_securelevel = securelevel;
1370 KASSERT(cr != NULL, ("securelevel_ge: null cr"));
1371 if (cr->cr_prison != NULL)
1372 active_securelevel = imax(cr->cr_prison->pr_securelevel,
1373 active_securelevel);
1374 return (active_securelevel >= level ? EPERM : 0);
1378 * 'see_other_uids' determines whether or not visibility of processes
1379 * and sockets with credentials holding different real uids is possible
1380 * using a variety of system MIBs.
1381 * XXX: data declarations should be together near the beginning of the file.
1383 static int see_other_uids = 1;
1384 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_uids, CTLFLAG_RW,
1386 "Unprivileged processes may see subjects/objects with different real uid");
1389 * Determine if u1 "can see" the subject specified by u2, according to the
1390 * 'see_other_uids' policy.
1391 * Returns: 0 for permitted, ESRCH otherwise
1393 * References: *u1 and *u2 must not change during the call
1394 * u1 may equal u2, in which case only one reference is required
1397 cr_seeotheruids(struct ucred *u1, struct ucred *u2)
1400 if (!see_other_uids && u1->cr_ruid != u2->cr_ruid) {
1401 if (priv_check_cred(u1, PRIV_SEEOTHERUIDS, SUSER_ALLOWJAIL)
1409 * 'see_other_gids' determines whether or not visibility of processes
1410 * and sockets with credentials holding different real gids is possible
1411 * using a variety of system MIBs.
1412 * XXX: data declarations should be together near the beginning of the file.
1414 static int see_other_gids = 1;
1415 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_gids, CTLFLAG_RW,
1417 "Unprivileged processes may see subjects/objects with different real gid");
1420 * Determine if u1 can "see" the subject specified by u2, according to the
1421 * 'see_other_gids' policy.
1422 * Returns: 0 for permitted, ESRCH otherwise
1424 * References: *u1 and *u2 must not change during the call
1425 * u1 may equal u2, in which case only one reference is required
1428 cr_seeothergids(struct ucred *u1, struct ucred *u2)
1432 if (!see_other_gids) {
1434 for (i = 0; i < u1->cr_ngroups; i++) {
1435 if (groupmember(u1->cr_groups[i], u2))
1441 if (priv_check_cred(u1, PRIV_SEEOTHERGIDS,
1442 SUSER_ALLOWJAIL) != 0)
1450 * Determine if u1 "can see" the subject specified by u2.
1451 * Returns: 0 for permitted, an errno value otherwise
1453 * References: *u1 and *u2 must not change during the call
1454 * u1 may equal u2, in which case only one reference is required
1457 cr_cansee(struct ucred *u1, struct ucred *u2)
1461 if ((error = prison_check(u1, u2)))
1464 if ((error = mac_check_cred_visible(u1, u2)))
1467 if ((error = cr_seeotheruids(u1, u2)))
1469 if ((error = cr_seeothergids(u1, u2)))
1475 * Determine if td "can see" the subject specified by p.
1476 * Returns: 0 for permitted, an errno value otherwise
1477 * Locks: Sufficient locks to protect p->p_ucred must be held. td really
1478 * should be curthread.
1479 * References: td and p must be valid for the lifetime of the call
1482 p_cansee(struct thread *td, struct proc *p)
1485 /* Wrap cr_cansee() for all functionality. */
1486 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1487 PROC_LOCK_ASSERT(p, MA_OWNED);
1488 return (cr_cansee(td->td_ucred, p->p_ucred));
1492 * 'conservative_signals' prevents the delivery of a broad class of
1493 * signals by unprivileged processes to processes that have changed their
1494 * credentials since the last invocation of execve(). This can prevent
1495 * the leakage of cached information or retained privileges as a result
1496 * of a common class of signal-related vulnerabilities. However, this
1497 * may interfere with some applications that expect to be able to
1498 * deliver these signals to peer processes after having given up
1501 static int conservative_signals = 1;
1502 SYSCTL_INT(_security_bsd, OID_AUTO, conservative_signals, CTLFLAG_RW,
1503 &conservative_signals, 0, "Unprivileged processes prevented from "
1504 "sending certain signals to processes whose credentials have changed");
1506 * Determine whether cred may deliver the specified signal to proc.
1507 * Returns: 0 for permitted, an errno value otherwise.
1508 * Locks: A lock must be held for proc.
1509 * References: cred and proc must be valid for the lifetime of the call.
1512 cr_cansignal(struct ucred *cred, struct proc *proc, int signum)
1516 PROC_LOCK_ASSERT(proc, MA_OWNED);
1518 * Jail semantics limit the scope of signalling to proc in the
1519 * same jail as cred, if cred is in jail.
1521 error = prison_check(cred, proc->p_ucred);
1525 if ((error = mac_check_proc_signal(cred, proc, signum)))
1528 if ((error = cr_seeotheruids(cred, proc->p_ucred)))
1530 if ((error = cr_seeothergids(cred, proc->p_ucred)))
1534 * UNIX signal semantics depend on the status of the P_SUGID
1535 * bit on the target process. If the bit is set, then additional
1536 * restrictions are placed on the set of available signals.
1538 if (conservative_signals && (proc->p_flag & P_SUGID)) {
1553 * Generally, permit job and terminal control
1558 /* Not permitted without privilege. */
1559 error = priv_check_cred(cred, PRIV_SIGNAL_SUGID,
1567 * Generally, the target credential's ruid or svuid must match the
1568 * subject credential's ruid or euid.
1570 if (cred->cr_ruid != proc->p_ucred->cr_ruid &&
1571 cred->cr_ruid != proc->p_ucred->cr_svuid &&
1572 cred->cr_uid != proc->p_ucred->cr_ruid &&
1573 cred->cr_uid != proc->p_ucred->cr_svuid) {
1574 /* Not permitted without privilege. */
1575 error = priv_check_cred(cred, PRIV_SIGNAL_DIFFCRED,
1585 * Determine whether td may deliver the specified signal to p.
1586 * Returns: 0 for permitted, an errno value otherwise
1587 * Locks: Sufficient locks to protect various components of td and p
1588 * must be held. td must be curthread, and a lock must be
1590 * References: td and p must be valid for the lifetime of the call
1593 p_cansignal(struct thread *td, struct proc *p, int signum)
1596 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1597 PROC_LOCK_ASSERT(p, MA_OWNED);
1598 if (td->td_proc == p)
1602 * UNIX signalling semantics require that processes in the same
1603 * session always be able to deliver SIGCONT to one another,
1604 * overriding the remaining protections.
1606 /* XXX: This will require an additional lock of some sort. */
1607 if (signum == SIGCONT && td->td_proc->p_session == p->p_session)
1610 * Some compat layers use SIGTHR and higher signals for
1611 * communication between different kernel threads of the same
1612 * process, so that they expect that it's always possible to
1613 * deliver them, even for suid applications where cr_cansignal() can
1614 * deny such ability for security consideration. It should be
1615 * pretty safe to do since the only way to create two processes
1616 * with the same p_leader is via rfork(2).
1618 if (td->td_proc->p_leader != NULL && signum >= SIGTHR &&
1619 signum < SIGTHR + 4 && td->td_proc->p_leader == p->p_leader)
1622 return (cr_cansignal(td->td_ucred, p, signum));
1626 * Determine whether td may reschedule p.
1627 * Returns: 0 for permitted, an errno value otherwise
1628 * Locks: Sufficient locks to protect various components of td and p
1629 * must be held. td must be curthread, and a lock must
1631 * References: td and p must be valid for the lifetime of the call
1634 p_cansched(struct thread *td, struct proc *p)
1638 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1639 PROC_LOCK_ASSERT(p, MA_OWNED);
1640 if (td->td_proc == p)
1642 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1645 if ((error = mac_check_proc_sched(td->td_ucred, p)))
1648 if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1650 if ((error = cr_seeothergids(td->td_ucred, p->p_ucred)))
1652 if (td->td_ucred->cr_ruid != p->p_ucred->cr_ruid &&
1653 td->td_ucred->cr_uid != p->p_ucred->cr_ruid) {
1654 error = priv_check_cred(td->td_ucred, PRIV_SCHED_DIFFCRED,
1663 * The 'unprivileged_proc_debug' flag may be used to disable a variety of
1664 * unprivileged inter-process debugging services, including some procfs
1665 * functionality, ptrace(), and ktrace(). In the past, inter-process
1666 * debugging has been involved in a variety of security problems, and sites
1667 * not requiring the service might choose to disable it when hardening
1670 * XXX: Should modifying and reading this variable require locking?
1671 * XXX: data declarations should be together near the beginning of the file.
1673 static int unprivileged_proc_debug = 1;
1674 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_proc_debug, CTLFLAG_RW,
1675 &unprivileged_proc_debug, 0,
1676 "Unprivileged processes may use process debugging facilities");
1679 * Determine whether td may debug p.
1680 * Returns: 0 for permitted, an errno value otherwise
1681 * Locks: Sufficient locks to protect various components of td and p
1682 * must be held. td must be curthread, and a lock must
1684 * References: td and p must be valid for the lifetime of the call
1687 p_candebug(struct thread *td, struct proc *p)
1689 int credentialchanged, error, grpsubset, i, uidsubset;
1691 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1692 PROC_LOCK_ASSERT(p, MA_OWNED);
1693 if (!unprivileged_proc_debug) {
1694 error = priv_check_cred(td->td_ucred, PRIV_DEBUG_UNPRIV,
1699 if (td->td_proc == p)
1701 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1704 if ((error = mac_check_proc_debug(td->td_ucred, p)))
1707 if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1709 if ((error = cr_seeothergids(td->td_ucred, p->p_ucred)))
1713 * Is p's group set a subset of td's effective group set? This
1714 * includes p's egid, group access list, rgid, and svgid.
1717 for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
1718 if (!groupmember(p->p_ucred->cr_groups[i], td->td_ucred)) {
1723 grpsubset = grpsubset &&
1724 groupmember(p->p_ucred->cr_rgid, td->td_ucred) &&
1725 groupmember(p->p_ucred->cr_svgid, td->td_ucred);
1728 * Are the uids present in p's credential equal to td's
1729 * effective uid? This includes p's euid, svuid, and ruid.
1731 uidsubset = (td->td_ucred->cr_uid == p->p_ucred->cr_uid &&
1732 td->td_ucred->cr_uid == p->p_ucred->cr_svuid &&
1733 td->td_ucred->cr_uid == p->p_ucred->cr_ruid);
1736 * Has the credential of the process changed since the last exec()?
1738 credentialchanged = (p->p_flag & P_SUGID);
1741 * If p's gids aren't a subset, or the uids aren't a subset,
1742 * or the credential has changed, require appropriate privilege
1743 * for td to debug p.
1745 if (!grpsubset || !uidsubset) {
1746 error = priv_check_cred(td->td_ucred, PRIV_DEBUG_DIFFCRED,
1752 if (credentialchanged) {
1753 error = priv_check_cred(td->td_ucred, PRIV_DEBUG_SUGID,
1759 /* Can't trace init when securelevel > 0. */
1760 if (p == initproc) {
1761 error = securelevel_gt(td->td_ucred, 0);
1767 * Can't trace a process that's currently exec'ing.
1769 * XXX: Note, this is not a security policy decision, it's a
1770 * basic correctness/functionality decision. Therefore, this check
1771 * should be moved to the caller's of p_candebug().
1773 if ((p->p_flag & P_INEXEC) != 0)
1780 * Determine whether the subject represented by cred can "see" a socket.
1781 * Returns: 0 for permitted, ENOENT otherwise.
1784 cr_canseesocket(struct ucred *cred, struct socket *so)
1788 error = prison_check(cred, so->so_cred);
1793 error = mac_check_socket_visible(cred, so);
1798 if (cr_seeotheruids(cred, so->so_cred))
1800 if (cr_seeothergids(cred, so->so_cred))
1807 * Determine whether td can wait for the exit of p.
1808 * Returns: 0 for permitted, an errno value otherwise
1809 * Locks: Sufficient locks to protect various components of td and p
1810 * must be held. td must be curthread, and a lock must
1812 * References: td and p must be valid for the lifetime of the call
1816 p_canwait(struct thread *td, struct proc *p)
1820 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1821 PROC_LOCK_ASSERT(p, MA_OWNED);
1822 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1825 if ((error = mac_check_proc_wait(td->td_ucred, p)))
1829 /* XXXMAC: This could have odd effects on some shells. */
1830 if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1838 * Allocate a zeroed cred structure.
1844 register struct ucred *cr;
1846 MALLOC(cr, struct ucred *, sizeof(*cr), M_CRED, M_WAITOK | M_ZERO);
1847 refcount_init(&cr->cr_ref, 1);
1855 * Claim another reference to a ucred structure.
1859 crhold(struct ucred *cr)
1862 refcount_acquire(&cr->cr_ref);
1867 * Free a cred structure.
1868 * Throws away space when ref count gets to 0.
1872 crfree(struct ucred *cr)
1875 KASSERT(cr->cr_ref > 0, ("bad ucred refcount: %d", cr->cr_ref));
1876 KASSERT(cr->cr_ref != 0xdeadc0de, ("dangling reference to ucred"));
1877 if (refcount_release(&cr->cr_ref)) {
1879 * Some callers of crget(), such as nfs_statfs(),
1880 * allocate a temporary credential, but don't
1881 * allocate a uidinfo structure.
1883 if (cr->cr_uidinfo != NULL)
1884 uifree(cr->cr_uidinfo);
1885 if (cr->cr_ruidinfo != NULL)
1886 uifree(cr->cr_ruidinfo);
1888 * Free a prison, if any.
1891 prison_free(cr->cr_prison);
1893 mac_destroy_cred(cr);
1900 * Check to see if this ucred is shared.
1904 crshared(struct ucred *cr)
1907 return (cr->cr_ref > 1);
1911 * Copy a ucred's contents from a template. Does not block.
1915 crcopy(struct ucred *dest, struct ucred *src)
1918 KASSERT(crshared(dest) == 0, ("crcopy of shared ucred"));
1919 bcopy(&src->cr_startcopy, &dest->cr_startcopy,
1920 (unsigned)((caddr_t)&src->cr_endcopy -
1921 (caddr_t)&src->cr_startcopy));
1922 uihold(dest->cr_uidinfo);
1923 uihold(dest->cr_ruidinfo);
1925 prison_hold(dest->cr_prison);
1927 mac_copy_cred(src, dest);
1932 * Dup cred struct to a new held one.
1936 crdup(struct ucred *cr)
1938 struct ucred *newcr;
1946 * Fill in a struct xucred based on a struct ucred.
1950 cru2x(struct ucred *cr, struct xucred *xcr)
1953 bzero(xcr, sizeof(*xcr));
1954 xcr->cr_version = XUCRED_VERSION;
1955 xcr->cr_uid = cr->cr_uid;
1956 xcr->cr_ngroups = cr->cr_ngroups;
1957 bcopy(cr->cr_groups, xcr->cr_groups, sizeof(cr->cr_groups));
1961 * small routine to swap a thread's current ucred for the correct one
1962 * taken from the process.
1966 cred_update_thread(struct thread *td)
1972 cred = td->td_ucred;
1974 td->td_ucred = crhold(p->p_ucred);
1981 * Get login name, if available.
1983 #ifndef _SYS_SYSPROTO_H_
1984 struct getlogin_args {
1994 getlogin(struct thread *td, struct getlogin_args *uap)
1997 char login[MAXLOGNAME];
1998 struct proc *p = td->td_proc;
2000 if (uap->namelen > MAXLOGNAME)
2001 uap->namelen = MAXLOGNAME;
2003 SESS_LOCK(p->p_session);
2004 bcopy(p->p_session->s_login, login, uap->namelen);
2005 SESS_UNLOCK(p->p_session);
2007 error = copyout(login, uap->namebuf, uap->namelen);
2014 #ifndef _SYS_SYSPROTO_H_
2015 struct setlogin_args {
2024 setlogin(struct thread *td, struct setlogin_args *uap)
2026 struct proc *p = td->td_proc;
2028 char logintmp[MAXLOGNAME];
2030 error = priv_check_cred(td->td_ucred, PRIV_PROC_SETLOGIN,
2034 error = copyinstr(uap->namebuf, logintmp, sizeof(logintmp), NULL);
2035 if (error == ENAMETOOLONG)
2039 SESS_LOCK(p->p_session);
2040 (void) memcpy(p->p_session->s_login, logintmp,
2042 SESS_UNLOCK(p->p_session);
2049 setsugid(struct proc *p)
2052 PROC_LOCK_ASSERT(p, MA_OWNED);
2053 p->p_flag |= P_SUGID;
2054 if (!(p->p_pfsflags & PF_ISUGID))
2059 * Change a process's effective uid.
2060 * Side effects: newcred->cr_uid and newcred->cr_uidinfo will be modified.
2061 * References: newcred must be an exclusive credential reference for the
2062 * duration of the call.
2065 change_euid(struct ucred *newcred, struct uidinfo *euip)
2068 newcred->cr_uid = euip->ui_uid;
2070 uifree(newcred->cr_uidinfo);
2071 newcred->cr_uidinfo = euip;
2075 * Change a process's effective gid.
2076 * Side effects: newcred->cr_gid will be modified.
2077 * References: newcred must be an exclusive credential reference for the
2078 * duration of the call.
2081 change_egid(struct ucred *newcred, gid_t egid)
2084 newcred->cr_groups[0] = egid;
2088 * Change a process's real uid.
2089 * Side effects: newcred->cr_ruid will be updated, newcred->cr_ruidinfo
2090 * will be updated, and the old and new cr_ruidinfo proc
2091 * counts will be updated.
2092 * References: newcred must be an exclusive credential reference for the
2093 * duration of the call.
2096 change_ruid(struct ucred *newcred, struct uidinfo *ruip)
2099 (void)chgproccnt(newcred->cr_ruidinfo, -1, 0);
2100 newcred->cr_ruid = ruip->ui_uid;
2102 uifree(newcred->cr_ruidinfo);
2103 newcred->cr_ruidinfo = ruip;
2104 (void)chgproccnt(newcred->cr_ruidinfo, 1, 0);
2108 * Change a process's real gid.
2109 * Side effects: newcred->cr_rgid will be updated.
2110 * References: newcred must be an exclusive credential reference for the
2111 * duration of the call.
2114 change_rgid(struct ucred *newcred, gid_t rgid)
2117 newcred->cr_rgid = rgid;
2121 * Change a process's saved uid.
2122 * Side effects: newcred->cr_svuid will be updated.
2123 * References: newcred must be an exclusive credential reference for the
2124 * duration of the call.
2127 change_svuid(struct ucred *newcred, uid_t svuid)
2130 newcred->cr_svuid = svuid;
2134 * Change a process's saved gid.
2135 * Side effects: newcred->cr_svgid will be updated.
2136 * References: newcred must be an exclusive credential reference for the
2137 * duration of the call.
2140 change_svgid(struct ucred *newcred, gid_t svgid)
2143 newcred->cr_svgid = svgid;