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>
55 #include <sys/malloc.h>
56 #include <sys/mutex.h>
59 #include <sys/sysproto.h>
61 #include <sys/pioctl.h>
62 #include <sys/resourcevar.h>
63 #include <sys/socket.h>
64 #include <sys/socketvar.h>
65 #include <sys/sysctl.h>
67 static MALLOC_DEFINE(M_CRED, "cred", "credentials");
69 SYSCTL_DECL(_security);
70 SYSCTL_NODE(_security, OID_AUTO, bsd, CTLFLAG_RW, 0,
71 "BSD security policy");
73 #ifndef _SYS_SYSPROTO_H_
83 getpid(struct thread *td, struct getpid_args *uap)
85 struct proc *p = td->td_proc;
87 td->td_retval[0] = p->p_pid;
88 #if defined(COMPAT_43)
90 td->td_retval[1] = p->p_pptr->p_pid;
96 #ifndef _SYS_SYSPROTO_H_
106 getppid(struct thread *td, struct getppid_args *uap)
108 struct proc *p = td->td_proc;
111 td->td_retval[0] = p->p_pptr->p_pid;
117 * Get process group ID; note that POSIX getpgrp takes no parameter.
119 #ifndef _SYS_SYSPROTO_H_
120 struct getpgrp_args {
128 getpgrp(struct thread *td, struct getpgrp_args *uap)
130 struct proc *p = td->td_proc;
133 td->td_retval[0] = p->p_pgrp->pg_id;
138 /* Get an arbitary pid's process group id */
139 #ifndef _SYS_SYSPROTO_H_
140 struct getpgid_args {
148 getpgid(struct thread *td, struct getpgid_args *uap)
160 error = p_cansee(td, p);
166 td->td_retval[0] = p->p_pgrp->pg_id;
172 * Get an arbitary pid's session id.
174 #ifndef _SYS_SYSPROTO_H_
183 getsid(struct thread *td, struct getsid_args *uap)
195 error = p_cansee(td, p);
201 td->td_retval[0] = p->p_session->s_sid;
206 #ifndef _SYS_SYSPROTO_H_
216 getuid(struct thread *td, struct getuid_args *uap)
219 td->td_retval[0] = td->td_ucred->cr_ruid;
220 #if defined(COMPAT_43)
221 td->td_retval[1] = td->td_ucred->cr_uid;
226 #ifndef _SYS_SYSPROTO_H_
227 struct geteuid_args {
236 geteuid(struct thread *td, struct geteuid_args *uap)
239 td->td_retval[0] = td->td_ucred->cr_uid;
243 #ifndef _SYS_SYSPROTO_H_
253 getgid(struct thread *td, struct getgid_args *uap)
256 td->td_retval[0] = td->td_ucred->cr_rgid;
257 #if defined(COMPAT_43)
258 td->td_retval[1] = td->td_ucred->cr_groups[0];
264 * Get effective group ID. The "egid" is groups[0], and could be obtained
265 * via getgroups. This syscall exists because it is somewhat painful to do
266 * correctly in a library function.
268 #ifndef _SYS_SYSPROTO_H_
269 struct getegid_args {
278 getegid(struct thread *td, struct getegid_args *uap)
281 td->td_retval[0] = td->td_ucred->cr_groups[0];
285 #ifndef _SYS_SYSPROTO_H_
286 struct getgroups_args {
295 getgroups(struct thread *td, register struct getgroups_args *uap)
302 if ((ngrp = uap->gidsetsize) == 0) {
303 td->td_retval[0] = cred->cr_ngroups;
306 if (ngrp < cred->cr_ngroups)
308 ngrp = cred->cr_ngroups;
309 error = copyout(cred->cr_groups, uap->gidset, ngrp * sizeof(gid_t));
311 td->td_retval[0] = ngrp;
315 #ifndef _SYS_SYSPROTO_H_
325 setsid(register struct thread *td, struct setsid_args *uap)
329 struct proc *p = td->td_proc;
330 struct pgrp *newpgrp;
331 struct session *newsess;
336 MALLOC(newpgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
337 MALLOC(newsess, struct session *, sizeof(struct session), M_SESSION, M_WAITOK | M_ZERO);
339 sx_xlock(&proctree_lock);
341 if (p->p_pgid == p->p_pid || (pgrp = pgfind(p->p_pid)) != NULL) {
346 (void)enterpgrp(p, p->p_pid, newpgrp, newsess);
347 td->td_retval[0] = p->p_pid;
352 sx_xunlock(&proctree_lock);
355 FREE(newpgrp, M_PGRP);
357 FREE(newsess, M_SESSION);
363 * set process group (setpgid/old setpgrp)
365 * caller does setpgid(targpid, targpgid)
367 * pid must be caller or child of caller (ESRCH)
369 * pid must be in same session (EPERM)
370 * pid can't have done an exec (EACCES)
372 * there must exist some pid in same session having pgid (EPERM)
373 * pid must not be session leader (EPERM)
375 #ifndef _SYS_SYSPROTO_H_
376 struct setpgid_args {
377 int pid; /* target process id */
378 int pgid; /* target pgrp id */
386 setpgid(struct thread *td, register struct setpgid_args *uap)
388 struct proc *curp = td->td_proc;
389 register struct proc *targp; /* target process */
390 register struct pgrp *pgrp; /* target pgrp */
392 struct pgrp *newpgrp;
399 MALLOC(newpgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
401 sx_xlock(&proctree_lock);
402 if (uap->pid != 0 && uap->pid != curp->p_pid) {
403 if ((targp = pfind(uap->pid)) == NULL) {
407 if (!inferior(targp)) {
412 if ((error = p_cansee(td, targp))) {
416 if (targp->p_pgrp == NULL ||
417 targp->p_session != curp->p_session) {
422 if (targp->p_flag & P_EXEC) {
430 if (SESS_LEADER(targp)) {
435 uap->pgid = targp->p_pid;
436 if ((pgrp = pgfind(uap->pgid)) == NULL) {
437 if (uap->pgid == targp->p_pid) {
438 error = enterpgrp(targp, uap->pgid, newpgrp,
445 if (pgrp == targp->p_pgrp) {
449 if (pgrp->pg_id != targp->p_pid &&
450 pgrp->pg_session != curp->p_session) {
456 error = enterthispgrp(targp, pgrp);
459 sx_xunlock(&proctree_lock);
460 KASSERT((error == 0) || (newpgrp != NULL),
461 ("setpgid failed and newpgrp is NULL"));
463 FREE(newpgrp, M_PGRP);
468 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
469 * compatible. It says that setting the uid/gid to euid/egid is a special
470 * case of "appropriate privilege". Once the rules are expanded out, this
471 * basically means that setuid(nnn) sets all three id's, in all permitted
472 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
473 * does not set the saved id - this is dangerous for traditional BSD
474 * programs. For this reason, we *really* do not want to set
475 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
477 #define POSIX_APPENDIX_B_4_2_2
479 #ifndef _SYS_SYSPROTO_H_
489 setuid(struct thread *td, struct setuid_args *uap)
491 struct proc *p = td->td_proc;
492 struct ucred *newcred, *oldcred;
501 oldcred = p->p_ucred;
504 * See if we have "permission" by POSIX 1003.1 rules.
506 * Note that setuid(geteuid()) is a special case of
507 * "appropriate privileges" in appendix B.4.2.2. We need
508 * to use this clause to be compatible with traditional BSD
509 * semantics. Basically, it means that "setuid(xx)" sets all
510 * three id's (assuming you have privs).
512 * Notes on the logic. We do things in three steps.
513 * 1: We determine if the euid is going to change, and do EPERM
514 * right away. We unconditionally change the euid later if this
515 * test is satisfied, simplifying that part of the logic.
516 * 2: We determine if the real and/or saved uids are going to
517 * change. Determined by compile options.
518 * 3: Change euid last. (after tests in #2 for "appropriate privs")
520 if (uid != oldcred->cr_ruid && /* allow setuid(getuid()) */
521 #ifdef _POSIX_SAVED_IDS
522 uid != oldcred->cr_svuid && /* allow setuid(saved gid) */
524 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
525 uid != oldcred->cr_uid && /* allow setuid(geteuid()) */
527 (error = suser_cred(oldcred, SUSER_ALLOWJAIL)) != 0) {
535 * Copy credentials so other references do not see our changes.
537 crcopy(newcred, oldcred);
538 #ifdef _POSIX_SAVED_IDS
540 * Do we have "appropriate privileges" (are we root or uid == euid)
541 * If so, we are changing the real uid and/or saved uid.
544 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
545 uid == oldcred->cr_uid ||
547 suser_cred(oldcred, SUSER_ALLOWJAIL) == 0) /* we are using privs */
551 * Set the real uid and transfer proc count to new user.
553 if (uid != oldcred->cr_ruid) {
554 change_ruid(newcred, uip);
560 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
561 * the security of seteuid() depends on it. B.4.2.2 says it
562 * is important that we should do this.
564 if (uid != oldcred->cr_svuid) {
565 change_svuid(newcred, uid);
571 * In all permitted cases, we are changing the euid.
573 if (uid != oldcred->cr_uid) {
574 change_euid(newcred, uip);
577 p->p_ucred = newcred;
584 #ifndef _SYS_SYSPROTO_H_
585 struct seteuid_args {
594 seteuid(struct thread *td, struct seteuid_args *uap)
596 struct proc *p = td->td_proc;
597 struct ucred *newcred, *oldcred;
599 struct uidinfo *euip;
606 oldcred = p->p_ucred;
607 if (euid != oldcred->cr_ruid && /* allow seteuid(getuid()) */
608 euid != oldcred->cr_svuid && /* allow seteuid(saved uid) */
609 (error = suser_cred(oldcred, SUSER_ALLOWJAIL)) != 0) {
616 * Everything's okay, do it. Copy credentials so other references do
617 * not see our changes.
619 crcopy(newcred, oldcred);
620 if (oldcred->cr_uid != euid) {
621 change_euid(newcred, euip);
624 p->p_ucred = newcred;
631 #ifndef _SYS_SYSPROTO_H_
641 setgid(struct thread *td, struct setgid_args *uap)
643 struct proc *p = td->td_proc;
644 struct ucred *newcred, *oldcred;
651 oldcred = p->p_ucred;
654 * See if we have "permission" by POSIX 1003.1 rules.
656 * Note that setgid(getegid()) is a special case of
657 * "appropriate privileges" in appendix B.4.2.2. We need
658 * to use this clause to be compatible with traditional BSD
659 * semantics. Basically, it means that "setgid(xx)" sets all
660 * three id's (assuming you have privs).
662 * For notes on the logic here, see setuid() above.
664 if (gid != oldcred->cr_rgid && /* allow setgid(getgid()) */
665 #ifdef _POSIX_SAVED_IDS
666 gid != oldcred->cr_svgid && /* allow setgid(saved gid) */
668 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
669 gid != oldcred->cr_groups[0] && /* allow setgid(getegid()) */
671 (error = suser_cred(oldcred, SUSER_ALLOWJAIL)) != 0) {
677 crcopy(newcred, oldcred);
678 #ifdef _POSIX_SAVED_IDS
680 * Do we have "appropriate privileges" (are we root or gid == egid)
681 * If so, we are changing the real uid and saved gid.
684 #ifdef POSIX_APPENDIX_B_4_2_2 /* use the clause from B.4.2.2 */
685 gid == oldcred->cr_groups[0] ||
687 suser_cred(oldcred, SUSER_ALLOWJAIL) == 0) /* we are using privs */
693 if (oldcred->cr_rgid != gid) {
694 change_rgid(newcred, gid);
700 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
701 * the security of setegid() depends on it. B.4.2.2 says it
702 * is important that we should do this.
704 if (oldcred->cr_svgid != gid) {
705 change_svgid(newcred, gid);
710 * In all cases permitted cases, we are changing the egid.
711 * Copy credentials so other references do not see our changes.
713 if (oldcred->cr_groups[0] != gid) {
714 change_egid(newcred, gid);
717 p->p_ucred = newcred;
723 #ifndef _SYS_SYSPROTO_H_
724 struct setegid_args {
733 setegid(struct thread *td, struct setegid_args *uap)
735 struct proc *p = td->td_proc;
736 struct ucred *newcred, *oldcred;
743 oldcred = p->p_ucred;
744 if (egid != oldcred->cr_rgid && /* allow setegid(getgid()) */
745 egid != oldcred->cr_svgid && /* allow setegid(saved gid) */
746 (error = suser_cred(oldcred, SUSER_ALLOWJAIL)) != 0) {
751 crcopy(newcred, oldcred);
752 if (oldcred->cr_groups[0] != egid) {
753 change_egid(newcred, egid);
756 p->p_ucred = newcred;
762 #ifndef _SYS_SYSPROTO_H_
763 struct setgroups_args {
773 setgroups(struct thread *td, struct setgroups_args *uap)
775 struct proc *p = td->td_proc;
776 struct ucred *newcred, *tempcred, *oldcred;
780 ngrp = uap->gidsetsize;
784 error = copyin(uap->gidset, tempcred->cr_groups, ngrp * sizeof(gid_t));
791 oldcred = p->p_ucred;
792 error = suser_cred(oldcred, SUSER_ALLOWJAIL);
801 * XXX A little bit lazy here. We could test if anything has
802 * changed before crcopy() and setting P_SUGID.
804 crcopy(newcred, oldcred);
807 * setgroups(0, NULL) is a legitimate way of clearing the
808 * groups vector on non-BSD systems (which generally do not
809 * have the egid in the groups[0]). We risk security holes
810 * when running non-BSD software if we do not do the same.
812 newcred->cr_ngroups = 1;
814 bcopy(tempcred->cr_groups, newcred->cr_groups,
815 ngrp * sizeof(gid_t));
816 newcred->cr_ngroups = ngrp;
819 p->p_ucred = newcred;
826 #ifndef _SYS_SYSPROTO_H_
827 struct setreuid_args {
837 setreuid(register struct thread *td, struct setreuid_args *uap)
839 struct proc *p = td->td_proc;
840 struct ucred *newcred, *oldcred;
842 struct uidinfo *euip, *ruip;
851 oldcred = p->p_ucred;
852 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
853 ruid != oldcred->cr_svuid) ||
854 (euid != (uid_t)-1 && euid != oldcred->cr_uid &&
855 euid != oldcred->cr_ruid && euid != oldcred->cr_svuid)) &&
856 (error = suser_cred(oldcred, SUSER_ALLOWJAIL)) != 0) {
863 crcopy(newcred, oldcred);
864 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
865 change_euid(newcred, euip);
868 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
869 change_ruid(newcred, ruip);
872 if ((ruid != (uid_t)-1 || newcred->cr_uid != newcred->cr_ruid) &&
873 newcred->cr_svuid != newcred->cr_uid) {
874 change_svuid(newcred, newcred->cr_uid);
877 p->p_ucred = newcred;
885 #ifndef _SYS_SYSPROTO_H_
886 struct setregid_args {
896 setregid(register struct thread *td, struct setregid_args *uap)
898 struct proc *p = td->td_proc;
899 struct ucred *newcred, *oldcred;
907 oldcred = p->p_ucred;
908 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
909 rgid != oldcred->cr_svgid) ||
910 (egid != (gid_t)-1 && egid != oldcred->cr_groups[0] &&
911 egid != oldcred->cr_rgid && egid != oldcred->cr_svgid)) &&
912 (error = suser_cred(oldcred, SUSER_ALLOWJAIL)) != 0) {
918 crcopy(newcred, oldcred);
919 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
920 change_egid(newcred, egid);
923 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
924 change_rgid(newcred, rgid);
927 if ((rgid != (gid_t)-1 || newcred->cr_groups[0] != newcred->cr_rgid) &&
928 newcred->cr_svgid != newcred->cr_groups[0]) {
929 change_svgid(newcred, newcred->cr_groups[0]);
932 p->p_ucred = newcred;
939 * setresuid(ruid, euid, suid) is like setreuid except control over the
940 * saved uid is explicit.
943 #ifndef _SYS_SYSPROTO_H_
944 struct setresuid_args {
955 setresuid(register struct thread *td, struct setresuid_args *uap)
957 struct proc *p = td->td_proc;
958 struct ucred *newcred, *oldcred;
959 uid_t euid, ruid, suid;
960 struct uidinfo *euip, *ruip;
970 oldcred = p->p_ucred;
971 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
972 ruid != oldcred->cr_svuid &&
973 ruid != oldcred->cr_uid) ||
974 (euid != (uid_t)-1 && euid != oldcred->cr_ruid &&
975 euid != oldcred->cr_svuid &&
976 euid != oldcred->cr_uid) ||
977 (suid != (uid_t)-1 && suid != oldcred->cr_ruid &&
978 suid != oldcred->cr_svuid &&
979 suid != oldcred->cr_uid)) &&
980 (error = suser_cred(oldcred, SUSER_ALLOWJAIL)) != 0) {
988 crcopy(newcred, oldcred);
989 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
990 change_euid(newcred, euip);
993 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
994 change_ruid(newcred, ruip);
997 if (suid != (uid_t)-1 && oldcred->cr_svuid != suid) {
998 change_svuid(newcred, suid);
1001 p->p_ucred = newcred;
1010 * setresgid(rgid, egid, sgid) is like setregid except control over the
1011 * saved gid is explicit.
1014 #ifndef _SYS_SYSPROTO_H_
1015 struct setresgid_args {
1026 setresgid(register struct thread *td, struct setresgid_args *uap)
1028 struct proc *p = td->td_proc;
1029 struct ucred *newcred, *oldcred;
1030 gid_t egid, rgid, sgid;
1038 oldcred = p->p_ucred;
1039 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
1040 rgid != oldcred->cr_svgid &&
1041 rgid != oldcred->cr_groups[0]) ||
1042 (egid != (gid_t)-1 && egid != oldcred->cr_rgid &&
1043 egid != oldcred->cr_svgid &&
1044 egid != oldcred->cr_groups[0]) ||
1045 (sgid != (gid_t)-1 && sgid != oldcred->cr_rgid &&
1046 sgid != oldcred->cr_svgid &&
1047 sgid != oldcred->cr_groups[0])) &&
1048 (error = suser_cred(oldcred, SUSER_ALLOWJAIL)) != 0) {
1054 crcopy(newcred, oldcred);
1055 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
1056 change_egid(newcred, egid);
1059 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
1060 change_rgid(newcred, rgid);
1063 if (sgid != (gid_t)-1 && oldcred->cr_svgid != sgid) {
1064 change_svgid(newcred, sgid);
1067 p->p_ucred = newcred;
1073 #ifndef _SYS_SYSPROTO_H_
1074 struct getresuid_args {
1085 getresuid(register struct thread *td, struct getresuid_args *uap)
1088 int error1 = 0, error2 = 0, error3 = 0;
1090 cred = td->td_ucred;
1092 error1 = copyout(&cred->cr_ruid,
1093 uap->ruid, sizeof(cred->cr_ruid));
1095 error2 = copyout(&cred->cr_uid,
1096 uap->euid, sizeof(cred->cr_uid));
1098 error3 = copyout(&cred->cr_svuid,
1099 uap->suid, sizeof(cred->cr_svuid));
1100 return (error1 ? error1 : error2 ? error2 : error3);
1103 #ifndef _SYS_SYSPROTO_H_
1104 struct getresgid_args {
1115 getresgid(register struct thread *td, struct getresgid_args *uap)
1118 int error1 = 0, error2 = 0, error3 = 0;
1120 cred = td->td_ucred;
1122 error1 = copyout(&cred->cr_rgid,
1123 uap->rgid, sizeof(cred->cr_rgid));
1125 error2 = copyout(&cred->cr_groups[0],
1126 uap->egid, sizeof(cred->cr_groups[0]));
1128 error3 = copyout(&cred->cr_svgid,
1129 uap->sgid, sizeof(cred->cr_svgid));
1130 return (error1 ? error1 : error2 ? error2 : error3);
1133 #ifndef _SYS_SYSPROTO_H_
1134 struct issetugid_args {
1143 issetugid(register struct thread *td, struct issetugid_args *uap)
1145 struct proc *p = td->td_proc;
1148 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
1149 * we use P_SUGID because we consider changing the owners as
1150 * "tainting" as well.
1151 * This is significant for procs that start as root and "become"
1152 * a user without an exec - programs cannot know *everything*
1153 * that libc *might* have put in their data segment.
1156 td->td_retval[0] = (p->p_flag & P_SUGID) ? 1 : 0;
1165 __setugid(struct thread *td, struct __setugid_args *uap)
1171 switch (uap->flag) {
1174 p->p_flag &= ~P_SUGID;
1179 p->p_flag |= P_SUGID;
1185 #else /* !REGRESSION */
1188 #endif /* REGRESSION */
1192 * Check if gid is a member of the group set.
1194 * MPSAFE (cred must be held)
1197 groupmember(gid_t gid, struct ucred *cred)
1202 egp = &(cred->cr_groups[cred->cr_ngroups]);
1203 for (gp = cred->cr_groups; gp < egp; gp++)
1210 * `suser_enabled' (which can be set by the security.suser_enabled
1211 * sysctl) determines whether the system 'super-user' policy is in effect.
1212 * If it is nonzero, an effective uid of 0 connotes special privilege,
1213 * overriding many mandatory and discretionary protections. If it is zero,
1214 * uid 0 is offered no special privilege in the kernel security policy.
1215 * Setting it to zero may seriously impact the functionality of many
1216 * existing userland programs, and should not be done without careful
1217 * consideration of the consequences.
1219 int suser_enabled = 1;
1220 SYSCTL_INT(_security_bsd, OID_AUTO, suser_enabled, CTLFLAG_RW,
1221 &suser_enabled, 0, "processes with uid 0 have privilege");
1222 TUNABLE_INT("security.bsd.suser_enabled", &suser_enabled);
1225 * Test whether the specified credentials imply "super-user" privilege.
1226 * Return 0 or EPERM.
1229 suser_cred(struct ucred *cred, int flag)
1234 if (((flag & SUSER_RUID) ? cred->cr_ruid : cred->cr_uid) != 0)
1236 if (jailed(cred) && !(flag & SUSER_ALLOWJAIL))
1242 * Shortcut to hide contents of struct td and struct proc from the
1243 * caller, promoting binary compatibility.
1246 suser(struct thread *td)
1250 if (td != curthread) {
1251 printf("suser: thread %p (%d %s) != curthread %p (%d %s)\n",
1252 td, td->td_proc->p_pid, td->td_proc->p_comm,
1253 curthread, curthread->td_proc->p_pid,
1254 curthread->td_proc->p_comm);
1260 return (suser_cred(td->td_ucred, 0));
1264 * Test the active securelevel against a given level. securelevel_gt()
1265 * implements (securelevel > level). securelevel_ge() implements
1266 * (securelevel >= level). Note that the logic is inverted -- these
1267 * functions return EPERM on "success" and 0 on "failure".
1272 securelevel_gt(struct ucred *cr, int level)
1274 int active_securelevel;
1276 active_securelevel = securelevel;
1277 KASSERT(cr != NULL, ("securelevel_gt: null cr"));
1278 if (cr->cr_prison != NULL)
1279 active_securelevel = imax(cr->cr_prison->pr_securelevel,
1280 active_securelevel);
1281 return (active_securelevel > level ? EPERM : 0);
1285 securelevel_ge(struct ucred *cr, int level)
1287 int active_securelevel;
1289 active_securelevel = securelevel;
1290 KASSERT(cr != NULL, ("securelevel_ge: null cr"));
1291 if (cr->cr_prison != NULL)
1292 active_securelevel = imax(cr->cr_prison->pr_securelevel,
1293 active_securelevel);
1294 return (active_securelevel >= level ? EPERM : 0);
1298 * 'see_other_uids' determines whether or not visibility of processes
1299 * and sockets with credentials holding different real uids is possible
1300 * using a variety of system MIBs.
1301 * XXX: data declarations should be together near the beginning of the file.
1303 static int see_other_uids = 1;
1304 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_uids, CTLFLAG_RW,
1306 "Unprivileged processes may see subjects/objects with different real uid");
1309 * Determine if u1 "can see" the subject specified by u2, according to the
1310 * 'see_other_uids' policy.
1311 * Returns: 0 for permitted, ESRCH otherwise
1313 * References: *u1 and *u2 must not change during the call
1314 * u1 may equal u2, in which case only one reference is required
1317 cr_seeotheruids(struct ucred *u1, struct ucred *u2)
1320 if (!see_other_uids && u1->cr_ruid != u2->cr_ruid) {
1321 if (suser_cred(u1, SUSER_ALLOWJAIL) != 0)
1328 * 'see_other_gids' determines whether or not visibility of processes
1329 * and sockets with credentials holding different real gids is possible
1330 * using a variety of system MIBs.
1331 * XXX: data declarations should be together near the beginning of the file.
1333 static int see_other_gids = 1;
1334 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_gids, CTLFLAG_RW,
1336 "Unprivileged processes may see subjects/objects with different real gid");
1339 * Determine if u1 can "see" the subject specified by u2, according to the
1340 * 'see_other_gids' policy.
1341 * Returns: 0 for permitted, ESRCH otherwise
1343 * References: *u1 and *u2 must not change during the call
1344 * u1 may equal u2, in which case only one reference is required
1347 cr_seeothergids(struct ucred *u1, struct ucred *u2)
1351 if (!see_other_gids) {
1353 for (i = 0; i < u1->cr_ngroups; i++) {
1354 if (groupmember(u1->cr_groups[i], u2))
1360 if (suser_cred(u1, SUSER_ALLOWJAIL) != 0)
1368 * Determine if u1 "can see" the subject specified by u2.
1369 * Returns: 0 for permitted, an errno value otherwise
1371 * References: *u1 and *u2 must not change during the call
1372 * u1 may equal u2, in which case only one reference is required
1375 cr_cansee(struct ucred *u1, struct ucred *u2)
1379 if ((error = prison_check(u1, u2)))
1382 if ((error = mac_check_cred_visible(u1, u2)))
1385 if ((error = cr_seeotheruids(u1, u2)))
1387 if ((error = cr_seeothergids(u1, u2)))
1393 * Determine if td "can see" the subject specified by p.
1394 * Returns: 0 for permitted, an errno value otherwise
1395 * Locks: Sufficient locks to protect p->p_ucred must be held. td really
1396 * should be curthread.
1397 * References: td and p must be valid for the lifetime of the call
1400 p_cansee(struct thread *td, struct proc *p)
1403 /* Wrap cr_cansee() for all functionality. */
1404 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1405 PROC_LOCK_ASSERT(p, MA_OWNED);
1406 return (cr_cansee(td->td_ucred, p->p_ucred));
1410 * 'conservative_signals' prevents the delivery of a broad class of
1411 * signals by unprivileged processes to processes that have changed their
1412 * credentials since the last invocation of execve(). This can prevent
1413 * the leakage of cached information or retained privileges as a result
1414 * of a common class of signal-related vulnerabilities. However, this
1415 * may interfere with some applications that expect to be able to
1416 * deliver these signals to peer processes after having given up
1419 static int conservative_signals = 1;
1420 SYSCTL_INT(_security_bsd, OID_AUTO, conservative_signals, CTLFLAG_RW,
1421 &conservative_signals, 0, "Unprivileged processes prevented from "
1422 "sending certain signals to processes whose credentials have changed");
1424 * Determine whether cred may deliver the specified signal to proc.
1425 * Returns: 0 for permitted, an errno value otherwise.
1426 * Locks: A lock must be held for proc.
1427 * References: cred and proc must be valid for the lifetime of the call.
1430 cr_cansignal(struct ucred *cred, struct proc *proc, int signum)
1434 PROC_LOCK_ASSERT(proc, MA_OWNED);
1436 * Jail semantics limit the scope of signalling to proc in the
1437 * same jail as cred, if cred is in jail.
1439 error = prison_check(cred, proc->p_ucred);
1443 if ((error = mac_check_proc_signal(cred, proc, signum)))
1446 if ((error = cr_seeotheruids(cred, proc->p_ucred)))
1448 if ((error = cr_seeothergids(cred, proc->p_ucred)))
1452 * UNIX signal semantics depend on the status of the P_SUGID
1453 * bit on the target process. If the bit is set, then additional
1454 * restrictions are placed on the set of available signals.
1456 if (conservative_signals && (proc->p_flag & P_SUGID)) {
1471 * Generally, permit job and terminal control
1476 /* Not permitted without privilege. */
1477 error = suser_cred(cred, SUSER_ALLOWJAIL);
1484 * Generally, the target credential's ruid or svuid must match the
1485 * subject credential's ruid or euid.
1487 if (cred->cr_ruid != proc->p_ucred->cr_ruid &&
1488 cred->cr_ruid != proc->p_ucred->cr_svuid &&
1489 cred->cr_uid != proc->p_ucred->cr_ruid &&
1490 cred->cr_uid != proc->p_ucred->cr_svuid) {
1491 /* Not permitted without privilege. */
1492 error = suser_cred(cred, SUSER_ALLOWJAIL);
1502 * Determine whether td may deliver the specified signal to p.
1503 * Returns: 0 for permitted, an errno value otherwise
1504 * Locks: Sufficient locks to protect various components of td and p
1505 * must be held. td must be curthread, and a lock must be
1507 * References: td and p must be valid for the lifetime of the call
1510 p_cansignal(struct thread *td, struct proc *p, int signum)
1513 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1514 PROC_LOCK_ASSERT(p, MA_OWNED);
1515 if (td->td_proc == p)
1519 * UNIX signalling semantics require that processes in the same
1520 * session always be able to deliver SIGCONT to one another,
1521 * overriding the remaining protections.
1523 /* XXX: This will require an additional lock of some sort. */
1524 if (signum == SIGCONT && td->td_proc->p_session == p->p_session)
1527 * Some compat layers use SIGTHR for communications between
1528 * different kernel threads of the same process, so that
1529 * they are expecting that it's always possible to deliver
1530 * it, even for suid applications where cr_cansignal() can
1531 * deny such ability for security consideration. It should be
1532 * pretty safe to do since the only way to create two processes
1533 * with the same p_leader is via rfork(2).
1535 if (signum == SIGTHR && td->td_proc->p_leader != NULL &&
1536 td->td_proc->p_leader == p->p_leader)
1539 return (cr_cansignal(td->td_ucred, p, signum));
1543 * Determine whether td may reschedule 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
1548 * References: td and p must be valid for the lifetime of the call
1551 p_cansched(struct thread *td, struct proc *p)
1555 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1556 PROC_LOCK_ASSERT(p, MA_OWNED);
1557 if (td->td_proc == p)
1559 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1562 if ((error = mac_check_proc_sched(td->td_ucred, p)))
1565 if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1567 if ((error = cr_seeothergids(td->td_ucred, p->p_ucred)))
1569 if (td->td_ucred->cr_ruid == p->p_ucred->cr_ruid)
1571 if (td->td_ucred->cr_uid == p->p_ucred->cr_ruid)
1573 if (suser_cred(td->td_ucred, SUSER_ALLOWJAIL) == 0)
1577 if (!cap_check(NULL, td, CAP_SYS_NICE, SUSER_ALLOWJAIL))
1585 * The 'unprivileged_proc_debug' flag may be used to disable a variety of
1586 * unprivileged inter-process debugging services, including some procfs
1587 * functionality, ptrace(), and ktrace(). In the past, inter-process
1588 * debugging has been involved in a variety of security problems, and sites
1589 * not requiring the service might choose to disable it when hardening
1592 * XXX: Should modifying and reading this variable require locking?
1593 * XXX: data declarations should be together near the beginning of the file.
1595 static int unprivileged_proc_debug = 1;
1596 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_proc_debug, CTLFLAG_RW,
1597 &unprivileged_proc_debug, 0,
1598 "Unprivileged processes may use process debugging facilities");
1601 * Determine whether td may debug p.
1602 * Returns: 0 for permitted, an errno value otherwise
1603 * Locks: Sufficient locks to protect various components of td and p
1604 * must be held. td must be curthread, and a lock must
1606 * References: td and p must be valid for the lifetime of the call
1609 p_candebug(struct thread *td, struct proc *p)
1611 int credentialchanged, error, grpsubset, i, uidsubset;
1613 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1614 PROC_LOCK_ASSERT(p, MA_OWNED);
1615 if (!unprivileged_proc_debug) {
1616 error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL);
1620 if (td->td_proc == p)
1622 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1625 if ((error = mac_check_proc_debug(td->td_ucred, p)))
1628 if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1630 if ((error = cr_seeothergids(td->td_ucred, p->p_ucred)))
1634 * Is p's group set a subset of td's effective group set? This
1635 * includes p's egid, group access list, rgid, and svgid.
1638 for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
1639 if (!groupmember(p->p_ucred->cr_groups[i], td->td_ucred)) {
1644 grpsubset = grpsubset &&
1645 groupmember(p->p_ucred->cr_rgid, td->td_ucred) &&
1646 groupmember(p->p_ucred->cr_svgid, td->td_ucred);
1649 * Are the uids present in p's credential equal to td's
1650 * effective uid? This includes p's euid, svuid, and ruid.
1652 uidsubset = (td->td_ucred->cr_uid == p->p_ucred->cr_uid &&
1653 td->td_ucred->cr_uid == p->p_ucred->cr_svuid &&
1654 td->td_ucred->cr_uid == p->p_ucred->cr_ruid);
1657 * Has the credential of the process changed since the last exec()?
1659 credentialchanged = (p->p_flag & P_SUGID);
1662 * If p's gids aren't a subset, or the uids aren't a subset,
1663 * or the credential has changed, require appropriate privilege
1664 * for td to debug p. For POSIX.1e capabilities, this will
1665 * require CAP_SYS_PTRACE.
1667 if (!grpsubset || !uidsubset || credentialchanged) {
1668 error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL);
1673 /* Can't trace init when securelevel > 0. */
1674 if (p == initproc) {
1675 error = securelevel_gt(td->td_ucred, 0);
1681 * Can't trace a process that's currently exec'ing.
1682 * XXX: Note, this is not a security policy decision, it's a
1683 * basic correctness/functionality decision. Therefore, this check
1684 * should be moved to the caller's of p_candebug().
1686 if ((p->p_flag & P_INEXEC) != 0)
1693 * Determine whether the subject represented by cred can "see" a socket.
1694 * Returns: 0 for permitted, ENOENT otherwise.
1697 cr_canseesocket(struct ucred *cred, struct socket *so)
1701 error = prison_check(cred, so->so_cred);
1706 error = mac_check_socket_visible(cred, so);
1711 if (cr_seeotheruids(cred, so->so_cred))
1713 if (cr_seeothergids(cred, so->so_cred))
1720 * Allocate a zeroed cred structure.
1726 register struct ucred *cr;
1728 MALLOC(cr, struct ucred *, sizeof(*cr), M_CRED, M_WAITOK | M_ZERO);
1730 cr->cr_mtxp = mtx_pool_find(mtxpool_sleep, cr);
1738 * Claim another reference to a ucred structure.
1742 crhold(struct ucred *cr)
1745 mtx_lock(cr->cr_mtxp);
1747 mtx_unlock(cr->cr_mtxp);
1752 * Free a cred structure.
1753 * Throws away space when ref count gets to 0.
1757 crfree(struct ucred *cr)
1759 struct mtx *mtxp = cr->cr_mtxp;
1762 KASSERT(cr->cr_ref > 0, ("bad ucred refcount: %d", cr->cr_ref));
1763 if (--cr->cr_ref == 0) {
1766 * Some callers of crget(), such as nfs_statfs(),
1767 * allocate a temporary credential, but don't
1768 * allocate a uidinfo structure.
1770 if (cr->cr_uidinfo != NULL)
1771 uifree(cr->cr_uidinfo);
1772 if (cr->cr_ruidinfo != NULL)
1773 uifree(cr->cr_ruidinfo);
1775 * Free a prison, if any.
1778 prison_free(cr->cr_prison);
1780 mac_destroy_cred(cr);
1789 * Check to see if this ucred is shared.
1793 crshared(struct ucred *cr)
1797 mtx_lock(cr->cr_mtxp);
1798 shared = (cr->cr_ref > 1);
1799 mtx_unlock(cr->cr_mtxp);
1804 * Copy a ucred's contents from a template. Does not block.
1808 crcopy(struct ucred *dest, struct ucred *src)
1811 KASSERT(crshared(dest) == 0, ("crcopy of shared ucred"));
1812 bcopy(&src->cr_startcopy, &dest->cr_startcopy,
1813 (unsigned)((caddr_t)&src->cr_endcopy -
1814 (caddr_t)&src->cr_startcopy));
1815 uihold(dest->cr_uidinfo);
1816 uihold(dest->cr_ruidinfo);
1818 prison_hold(dest->cr_prison);
1820 mac_copy_cred(src, dest);
1825 * Dup cred struct to a new held one.
1829 crdup(struct ucred *cr)
1831 struct ucred *newcr;
1839 * Fill in a struct xucred based on a struct ucred.
1843 cru2x(struct ucred *cr, struct xucred *xcr)
1846 bzero(xcr, sizeof(*xcr));
1847 xcr->cr_version = XUCRED_VERSION;
1848 xcr->cr_uid = cr->cr_uid;
1849 xcr->cr_ngroups = cr->cr_ngroups;
1850 bcopy(cr->cr_groups, xcr->cr_groups, sizeof(cr->cr_groups));
1854 * small routine to swap a thread's current ucred for the correct one
1855 * taken from the process.
1859 cred_update_thread(struct thread *td)
1865 cred = td->td_ucred;
1867 td->td_ucred = crhold(p->p_ucred);
1874 * Get login name, if available.
1876 #ifndef _SYS_SYSPROTO_H_
1877 struct getlogin_args {
1887 getlogin(struct thread *td, struct getlogin_args *uap)
1890 char login[MAXLOGNAME];
1891 struct proc *p = td->td_proc;
1893 if (uap->namelen > MAXLOGNAME)
1894 uap->namelen = MAXLOGNAME;
1896 SESS_LOCK(p->p_session);
1897 bcopy(p->p_session->s_login, login, uap->namelen);
1898 SESS_UNLOCK(p->p_session);
1900 error = copyout(login, uap->namebuf, uap->namelen);
1907 #ifndef _SYS_SYSPROTO_H_
1908 struct setlogin_args {
1917 setlogin(struct thread *td, struct setlogin_args *uap)
1919 struct proc *p = td->td_proc;
1921 char logintmp[MAXLOGNAME];
1923 error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL);
1926 error = copyinstr(uap->namebuf, logintmp, sizeof(logintmp), NULL);
1927 if (error == ENAMETOOLONG)
1931 SESS_LOCK(p->p_session);
1932 (void) memcpy(p->p_session->s_login, logintmp,
1934 SESS_UNLOCK(p->p_session);
1941 setsugid(struct proc *p)
1944 PROC_LOCK_ASSERT(p, MA_OWNED);
1945 p->p_flag |= P_SUGID;
1946 if (!(p->p_pfsflags & PF_ISUGID))
1951 * Change a process's effective uid.
1952 * Side effects: newcred->cr_uid and newcred->cr_uidinfo will be modified.
1953 * References: newcred must be an exclusive credential reference for the
1954 * duration of the call.
1957 change_euid(struct ucred *newcred, struct uidinfo *euip)
1960 newcred->cr_uid = euip->ui_uid;
1962 uifree(newcred->cr_uidinfo);
1963 newcred->cr_uidinfo = euip;
1967 * Change a process's effective gid.
1968 * Side effects: newcred->cr_gid will be modified.
1969 * References: newcred must be an exclusive credential reference for the
1970 * duration of the call.
1973 change_egid(struct ucred *newcred, gid_t egid)
1976 newcred->cr_groups[0] = egid;
1980 * Change a process's real uid.
1981 * Side effects: newcred->cr_ruid will be updated, newcred->cr_ruidinfo
1982 * will be updated, and the old and new cr_ruidinfo proc
1983 * counts will be updated.
1984 * References: newcred must be an exclusive credential reference for the
1985 * duration of the call.
1988 change_ruid(struct ucred *newcred, struct uidinfo *ruip)
1991 (void)chgproccnt(newcred->cr_ruidinfo, -1, 0);
1992 newcred->cr_ruid = ruip->ui_uid;
1994 uifree(newcred->cr_ruidinfo);
1995 newcred->cr_ruidinfo = ruip;
1996 (void)chgproccnt(newcred->cr_ruidinfo, 1, 0);
2000 * Change a process's real gid.
2001 * Side effects: newcred->cr_rgid will be updated.
2002 * References: newcred must be an exclusive credential reference for the
2003 * duration of the call.
2006 change_rgid(struct ucred *newcred, gid_t rgid)
2009 newcred->cr_rgid = rgid;
2013 * Change a process's saved uid.
2014 * Side effects: newcred->cr_svuid will be updated.
2015 * References: newcred must be an exclusive credential reference for the
2016 * duration of the call.
2019 change_svuid(struct ucred *newcred, uid_t svuid)
2022 newcred->cr_svuid = svuid;
2026 * Change a process's saved gid.
2027 * Side effects: newcred->cr_svgid will be updated.
2028 * References: newcred must be an exclusive credential reference for the
2029 * duration of the call.
2032 change_svgid(struct ucred *newcred, gid_t svgid)
2035 newcred->cr_svgid = svgid;