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>
57 #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/sysctl.h>
68 static MALLOC_DEFINE(M_CRED, "cred", "credentials");
70 SYSCTL_DECL(_security);
71 SYSCTL_NODE(_security, OID_AUTO, bsd, CTLFLAG_RW, 0,
72 "BSD security policy");
74 #ifndef _SYS_SYSPROTO_H_
84 getpid(struct thread *td, struct getpid_args *uap)
86 struct proc *p = td->td_proc;
88 td->td_retval[0] = p->p_pid;
89 #if defined(COMPAT_43)
91 td->td_retval[1] = p->p_pptr->p_pid;
97 #ifndef _SYS_SYSPROTO_H_
107 getppid(struct thread *td, struct getppid_args *uap)
109 struct proc *p = td->td_proc;
112 td->td_retval[0] = p->p_pptr->p_pid;
118 * Get process group ID; note that POSIX getpgrp takes no parameter.
120 #ifndef _SYS_SYSPROTO_H_
121 struct getpgrp_args {
129 getpgrp(struct thread *td, struct getpgrp_args *uap)
131 struct proc *p = td->td_proc;
134 td->td_retval[0] = p->p_pgrp->pg_id;
139 /* Get an arbitary pid's process group id */
140 #ifndef _SYS_SYSPROTO_H_
141 struct getpgid_args {
149 getpgid(struct thread *td, struct getpgid_args *uap)
161 error = p_cansee(td, p);
167 td->td_retval[0] = p->p_pgrp->pg_id;
173 * Get an arbitary pid's session id.
175 #ifndef _SYS_SYSPROTO_H_
184 getsid(struct thread *td, struct getsid_args *uap)
196 error = p_cansee(td, p);
202 td->td_retval[0] = p->p_session->s_sid;
207 #ifndef _SYS_SYSPROTO_H_
217 getuid(struct thread *td, struct getuid_args *uap)
220 td->td_retval[0] = td->td_ucred->cr_ruid;
221 #if defined(COMPAT_43)
222 td->td_retval[1] = td->td_ucred->cr_uid;
227 #ifndef _SYS_SYSPROTO_H_
228 struct geteuid_args {
237 geteuid(struct thread *td, struct geteuid_args *uap)
240 td->td_retval[0] = td->td_ucred->cr_uid;
244 #ifndef _SYS_SYSPROTO_H_
254 getgid(struct thread *td, struct getgid_args *uap)
257 td->td_retval[0] = td->td_ucred->cr_rgid;
258 #if defined(COMPAT_43)
259 td->td_retval[1] = td->td_ucred->cr_groups[0];
265 * Get effective group ID. The "egid" is groups[0], and could be obtained
266 * via getgroups. This syscall exists because it is somewhat painful to do
267 * correctly in a library function.
269 #ifndef _SYS_SYSPROTO_H_
270 struct getegid_args {
279 getegid(struct thread *td, struct getegid_args *uap)
282 td->td_retval[0] = td->td_ucred->cr_groups[0];
286 #ifndef _SYS_SYSPROTO_H_
287 struct getgroups_args {
296 getgroups(struct thread *td, register struct getgroups_args *uap)
303 if ((ngrp = uap->gidsetsize) == 0) {
304 td->td_retval[0] = cred->cr_ngroups;
307 if (ngrp < cred->cr_ngroups)
309 ngrp = cred->cr_ngroups;
310 error = copyout(cred->cr_groups, uap->gidset, ngrp * sizeof(gid_t));
312 td->td_retval[0] = ngrp;
316 #ifndef _SYS_SYSPROTO_H_
326 setsid(register struct thread *td, struct setsid_args *uap)
330 struct proc *p = td->td_proc;
331 struct pgrp *newpgrp;
332 struct session *newsess;
337 MALLOC(newpgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
338 MALLOC(newsess, struct session *, sizeof(struct session), M_SESSION, M_WAITOK | M_ZERO);
340 sx_xlock(&proctree_lock);
342 if (p->p_pgid == p->p_pid || (pgrp = pgfind(p->p_pid)) != NULL) {
347 (void)enterpgrp(p, p->p_pid, newpgrp, newsess);
348 td->td_retval[0] = p->p_pid;
353 sx_xunlock(&proctree_lock);
356 FREE(newpgrp, M_PGRP);
358 FREE(newsess, M_SESSION);
364 * set process group (setpgid/old setpgrp)
366 * caller does setpgid(targpid, targpgid)
368 * pid must be caller or child of caller (ESRCH)
370 * pid must be in same session (EPERM)
371 * pid can't have done an exec (EACCES)
373 * there must exist some pid in same session having pgid (EPERM)
374 * pid must not be session leader (EPERM)
376 #ifndef _SYS_SYSPROTO_H_
377 struct setpgid_args {
378 int pid; /* target process id */
379 int pgid; /* target pgrp id */
387 setpgid(struct thread *td, register struct setpgid_args *uap)
389 struct proc *curp = td->td_proc;
390 register struct proc *targp; /* target process */
391 register struct pgrp *pgrp; /* target pgrp */
393 struct pgrp *newpgrp;
400 MALLOC(newpgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
402 sx_xlock(&proctree_lock);
403 if (uap->pid != 0 && uap->pid != curp->p_pid) {
404 if ((targp = pfind(uap->pid)) == NULL) {
408 if (!inferior(targp)) {
413 if ((error = p_cansee(td, targp))) {
417 if (targp->p_pgrp == NULL ||
418 targp->p_session != curp->p_session) {
423 if (targp->p_flag & P_EXEC) {
431 if (SESS_LEADER(targp)) {
436 uap->pgid = targp->p_pid;
437 if ((pgrp = pgfind(uap->pgid)) == NULL) {
438 if (uap->pgid == targp->p_pid) {
439 error = enterpgrp(targp, uap->pgid, newpgrp,
446 if (pgrp == targp->p_pgrp) {
450 if (pgrp->pg_id != targp->p_pid &&
451 pgrp->pg_session != curp->p_session) {
457 error = enterthispgrp(targp, pgrp);
460 sx_xunlock(&proctree_lock);
461 KASSERT((error == 0) || (newpgrp != NULL),
462 ("setpgid failed and newpgrp is NULL"));
464 FREE(newpgrp, M_PGRP);
469 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
470 * compatible. It says that setting the uid/gid to euid/egid is a special
471 * case of "appropriate privilege". Once the rules are expanded out, this
472 * basically means that setuid(nnn) sets all three id's, in all permitted
473 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
474 * does not set the saved id - this is dangerous for traditional BSD
475 * programs. For this reason, we *really* do not want to set
476 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
478 #define POSIX_APPENDIX_B_4_2_2
480 #ifndef _SYS_SYSPROTO_H_
490 setuid(struct thread *td, struct setuid_args *uap)
492 struct proc *p = td->td_proc;
493 struct ucred *newcred, *oldcred;
502 oldcred = p->p_ucred;
505 error = mac_check_proc_setuid(p, oldcred, uid);
511 * See if we have "permission" by POSIX 1003.1 rules.
513 * Note that setuid(geteuid()) is a special case of
514 * "appropriate privileges" in appendix B.4.2.2. We need
515 * to use this clause to be compatible with traditional BSD
516 * semantics. Basically, it means that "setuid(xx)" sets all
517 * three id's (assuming you have privs).
519 * Notes on the logic. We do things in three steps.
520 * 1: We determine if the euid is going to change, and do EPERM
521 * right away. We unconditionally change the euid later if this
522 * test is satisfied, simplifying that part of the logic.
523 * 2: We determine if the real and/or saved uids are going to
524 * change. Determined by compile options.
525 * 3: Change euid last. (after tests in #2 for "appropriate privs")
527 if (uid != oldcred->cr_ruid && /* allow setuid(getuid()) */
528 #ifdef _POSIX_SAVED_IDS
529 uid != oldcred->cr_svuid && /* allow setuid(saved gid) */
531 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
532 uid != oldcred->cr_uid && /* allow setuid(geteuid()) */
534 (error = suser_cred(oldcred, SUSER_ALLOWJAIL)) != 0)
538 * Copy credentials so other references do not see our changes.
540 crcopy(newcred, oldcred);
541 #ifdef _POSIX_SAVED_IDS
543 * Do we have "appropriate privileges" (are we root or uid == euid)
544 * If so, we are changing the real uid and/or saved uid.
547 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
548 uid == oldcred->cr_uid ||
550 suser_cred(oldcred, SUSER_ALLOWJAIL) == 0) /* we are using privs */
554 * Set the real uid and transfer proc count to new user.
556 if (uid != oldcred->cr_ruid) {
557 change_ruid(newcred, uip);
563 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
564 * the security of seteuid() depends on it. B.4.2.2 says it
565 * is important that we should do this.
567 if (uid != oldcred->cr_svuid) {
568 change_svuid(newcred, uid);
574 * In all permitted cases, we are changing the euid.
576 if (uid != oldcred->cr_uid) {
577 change_euid(newcred, uip);
580 p->p_ucred = newcred;
593 #ifndef _SYS_SYSPROTO_H_
594 struct seteuid_args {
603 seteuid(struct thread *td, struct seteuid_args *uap)
605 struct proc *p = td->td_proc;
606 struct ucred *newcred, *oldcred;
608 struct uidinfo *euip;
615 oldcred = p->p_ucred;
618 error = mac_check_proc_seteuid(p, oldcred, euid);
623 if (euid != oldcred->cr_ruid && /* allow seteuid(getuid()) */
624 euid != oldcred->cr_svuid && /* allow seteuid(saved uid) */
625 (error = suser_cred(oldcred, SUSER_ALLOWJAIL)) != 0)
629 * Everything's okay, do it. Copy credentials so other references do
630 * not see our changes.
632 crcopy(newcred, oldcred);
633 if (oldcred->cr_uid != euid) {
634 change_euid(newcred, euip);
637 p->p_ucred = newcred;
650 #ifndef _SYS_SYSPROTO_H_
660 setgid(struct thread *td, struct setgid_args *uap)
662 struct proc *p = td->td_proc;
663 struct ucred *newcred, *oldcred;
670 oldcred = p->p_ucred;
673 error = mac_check_proc_setgid(p, oldcred, gid);
679 * See if we have "permission" by POSIX 1003.1 rules.
681 * Note that setgid(getegid()) is a special case of
682 * "appropriate privileges" in appendix B.4.2.2. We need
683 * to use this clause to be compatible with traditional BSD
684 * semantics. Basically, it means that "setgid(xx)" sets all
685 * three id's (assuming you have privs).
687 * For notes on the logic here, see setuid() above.
689 if (gid != oldcred->cr_rgid && /* allow setgid(getgid()) */
690 #ifdef _POSIX_SAVED_IDS
691 gid != oldcred->cr_svgid && /* allow setgid(saved gid) */
693 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
694 gid != oldcred->cr_groups[0] && /* allow setgid(getegid()) */
696 (error = suser_cred(oldcred, SUSER_ALLOWJAIL)) != 0)
699 crcopy(newcred, oldcred);
700 #ifdef _POSIX_SAVED_IDS
702 * Do we have "appropriate privileges" (are we root or gid == egid)
703 * If so, we are changing the real uid and saved gid.
706 #ifdef POSIX_APPENDIX_B_4_2_2 /* use the clause from B.4.2.2 */
707 gid == oldcred->cr_groups[0] ||
709 suser_cred(oldcred, SUSER_ALLOWJAIL) == 0) /* we are using privs */
715 if (oldcred->cr_rgid != gid) {
716 change_rgid(newcred, gid);
722 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
723 * the security of setegid() depends on it. B.4.2.2 says it
724 * is important that we should do this.
726 if (oldcred->cr_svgid != gid) {
727 change_svgid(newcred, gid);
732 * In all cases permitted cases, we are changing the egid.
733 * Copy credentials so other references do not see our changes.
735 if (oldcred->cr_groups[0] != gid) {
736 change_egid(newcred, gid);
739 p->p_ucred = newcred;
750 #ifndef _SYS_SYSPROTO_H_
751 struct setegid_args {
760 setegid(struct thread *td, struct setegid_args *uap)
762 struct proc *p = td->td_proc;
763 struct ucred *newcred, *oldcred;
770 oldcred = p->p_ucred;
773 error = mac_check_proc_setegid(p, oldcred, egid);
778 if (egid != oldcred->cr_rgid && /* allow setegid(getgid()) */
779 egid != oldcred->cr_svgid && /* allow setegid(saved gid) */
780 (error = suser_cred(oldcred, SUSER_ALLOWJAIL)) != 0)
783 crcopy(newcred, oldcred);
784 if (oldcred->cr_groups[0] != egid) {
785 change_egid(newcred, egid);
788 p->p_ucred = newcred;
799 #ifndef _SYS_SYSPROTO_H_
800 struct setgroups_args {
810 setgroups(struct thread *td, struct setgroups_args *uap)
812 struct proc *p = td->td_proc;
813 struct ucred *newcred, *tempcred, *oldcred;
817 ngrp = uap->gidsetsize;
821 error = copyin(uap->gidset, tempcred->cr_groups, ngrp * sizeof(gid_t));
828 oldcred = p->p_ucred;
831 error = mac_check_proc_setgroups(p, oldcred, ngrp,
832 tempcred->cr_groups);
837 error = suser_cred(oldcred, SUSER_ALLOWJAIL);
842 * XXX A little bit lazy here. We could test if anything has
843 * changed before crcopy() and setting P_SUGID.
845 crcopy(newcred, oldcred);
848 * setgroups(0, NULL) is a legitimate way of clearing the
849 * groups vector on non-BSD systems (which generally do not
850 * have the egid in the groups[0]). We risk security holes
851 * when running non-BSD software if we do not do the same.
853 newcred->cr_ngroups = 1;
855 bcopy(tempcred->cr_groups, newcred->cr_groups,
856 ngrp * sizeof(gid_t));
857 newcred->cr_ngroups = ngrp;
860 p->p_ucred = newcred;
873 #ifndef _SYS_SYSPROTO_H_
874 struct setreuid_args {
884 setreuid(register struct thread *td, struct setreuid_args *uap)
886 struct proc *p = td->td_proc;
887 struct ucred *newcred, *oldcred;
889 struct uidinfo *euip, *ruip;
898 oldcred = p->p_ucred;
901 error = mac_check_proc_setreuid(p, oldcred, ruid, euid);
906 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
907 ruid != oldcred->cr_svuid) ||
908 (euid != (uid_t)-1 && euid != oldcred->cr_uid &&
909 euid != oldcred->cr_ruid && euid != oldcred->cr_svuid)) &&
910 (error = suser_cred(oldcred, SUSER_ALLOWJAIL)) != 0)
913 crcopy(newcred, oldcred);
914 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
915 change_euid(newcred, euip);
918 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
919 change_ruid(newcred, ruip);
922 if ((ruid != (uid_t)-1 || newcred->cr_uid != newcred->cr_ruid) &&
923 newcred->cr_svuid != newcred->cr_uid) {
924 change_svuid(newcred, newcred->cr_uid);
927 p->p_ucred = newcred;
942 #ifndef _SYS_SYSPROTO_H_
943 struct setregid_args {
953 setregid(register struct thread *td, struct setregid_args *uap)
955 struct proc *p = td->td_proc;
956 struct ucred *newcred, *oldcred;
964 oldcred = p->p_ucred;
967 error = mac_check_proc_setregid(p, oldcred, rgid, egid);
972 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
973 rgid != oldcred->cr_svgid) ||
974 (egid != (gid_t)-1 && egid != oldcred->cr_groups[0] &&
975 egid != oldcred->cr_rgid && egid != oldcred->cr_svgid)) &&
976 (error = suser_cred(oldcred, SUSER_ALLOWJAIL)) != 0)
979 crcopy(newcred, oldcred);
980 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
981 change_egid(newcred, egid);
984 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
985 change_rgid(newcred, rgid);
988 if ((rgid != (gid_t)-1 || newcred->cr_groups[0] != newcred->cr_rgid) &&
989 newcred->cr_svgid != newcred->cr_groups[0]) {
990 change_svgid(newcred, newcred->cr_groups[0]);
993 p->p_ucred = newcred;
1005 * setresuid(ruid, euid, suid) is like setreuid except control over the
1006 * saved uid is explicit.
1009 #ifndef _SYS_SYSPROTO_H_
1010 struct setresuid_args {
1021 setresuid(register struct thread *td, struct setresuid_args *uap)
1023 struct proc *p = td->td_proc;
1024 struct ucred *newcred, *oldcred;
1025 uid_t euid, ruid, suid;
1026 struct uidinfo *euip, *ruip;
1033 euip = uifind(euid);
1034 ruip = uifind(ruid);
1036 oldcred = p->p_ucred;
1039 error = mac_check_proc_setresuid(p, oldcred, ruid, euid, suid);
1044 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
1045 ruid != oldcred->cr_svuid &&
1046 ruid != oldcred->cr_uid) ||
1047 (euid != (uid_t)-1 && euid != oldcred->cr_ruid &&
1048 euid != oldcred->cr_svuid &&
1049 euid != oldcred->cr_uid) ||
1050 (suid != (uid_t)-1 && suid != oldcred->cr_ruid &&
1051 suid != oldcred->cr_svuid &&
1052 suid != oldcred->cr_uid)) &&
1053 (error = suser_cred(oldcred, SUSER_ALLOWJAIL)) != 0)
1056 crcopy(newcred, oldcred);
1057 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
1058 change_euid(newcred, euip);
1061 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
1062 change_ruid(newcred, ruip);
1065 if (suid != (uid_t)-1 && oldcred->cr_svuid != suid) {
1066 change_svuid(newcred, suid);
1069 p->p_ucred = newcred;
1086 * setresgid(rgid, egid, sgid) is like setregid except control over the
1087 * saved gid is explicit.
1090 #ifndef _SYS_SYSPROTO_H_
1091 struct setresgid_args {
1102 setresgid(register struct thread *td, struct setresgid_args *uap)
1104 struct proc *p = td->td_proc;
1105 struct ucred *newcred, *oldcred;
1106 gid_t egid, rgid, sgid;
1114 oldcred = p->p_ucred;
1117 error = mac_check_proc_setresgid(p, oldcred, rgid, egid, sgid);
1122 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
1123 rgid != oldcred->cr_svgid &&
1124 rgid != oldcred->cr_groups[0]) ||
1125 (egid != (gid_t)-1 && egid != oldcred->cr_rgid &&
1126 egid != oldcred->cr_svgid &&
1127 egid != oldcred->cr_groups[0]) ||
1128 (sgid != (gid_t)-1 && sgid != oldcred->cr_rgid &&
1129 sgid != oldcred->cr_svgid &&
1130 sgid != oldcred->cr_groups[0])) &&
1131 (error = suser_cred(oldcred, SUSER_ALLOWJAIL)) != 0)
1134 crcopy(newcred, oldcred);
1135 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
1136 change_egid(newcred, egid);
1139 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
1140 change_rgid(newcred, rgid);
1143 if (sgid != (gid_t)-1 && oldcred->cr_svgid != sgid) {
1144 change_svgid(newcred, sgid);
1147 p->p_ucred = newcred;
1158 #ifndef _SYS_SYSPROTO_H_
1159 struct getresuid_args {
1170 getresuid(register struct thread *td, struct getresuid_args *uap)
1173 int error1 = 0, error2 = 0, error3 = 0;
1175 cred = td->td_ucred;
1177 error1 = copyout(&cred->cr_ruid,
1178 uap->ruid, sizeof(cred->cr_ruid));
1180 error2 = copyout(&cred->cr_uid,
1181 uap->euid, sizeof(cred->cr_uid));
1183 error3 = copyout(&cred->cr_svuid,
1184 uap->suid, sizeof(cred->cr_svuid));
1185 return (error1 ? error1 : error2 ? error2 : error3);
1188 #ifndef _SYS_SYSPROTO_H_
1189 struct getresgid_args {
1200 getresgid(register struct thread *td, struct getresgid_args *uap)
1203 int error1 = 0, error2 = 0, error3 = 0;
1205 cred = td->td_ucred;
1207 error1 = copyout(&cred->cr_rgid,
1208 uap->rgid, sizeof(cred->cr_rgid));
1210 error2 = copyout(&cred->cr_groups[0],
1211 uap->egid, sizeof(cred->cr_groups[0]));
1213 error3 = copyout(&cred->cr_svgid,
1214 uap->sgid, sizeof(cred->cr_svgid));
1215 return (error1 ? error1 : error2 ? error2 : error3);
1218 #ifndef _SYS_SYSPROTO_H_
1219 struct issetugid_args {
1228 issetugid(register struct thread *td, struct issetugid_args *uap)
1230 struct proc *p = td->td_proc;
1233 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
1234 * we use P_SUGID because we consider changing the owners as
1235 * "tainting" as well.
1236 * This is significant for procs that start as root and "become"
1237 * a user without an exec - programs cannot know *everything*
1238 * that libc *might* have put in their data segment.
1241 td->td_retval[0] = (p->p_flag & P_SUGID) ? 1 : 0;
1250 __setugid(struct thread *td, struct __setugid_args *uap)
1256 switch (uap->flag) {
1259 p->p_flag &= ~P_SUGID;
1264 p->p_flag |= P_SUGID;
1270 #else /* !REGRESSION */
1273 #endif /* REGRESSION */
1277 * Check if gid is a member of the group set.
1279 * MPSAFE (cred must be held)
1282 groupmember(gid_t gid, struct ucred *cred)
1287 egp = &(cred->cr_groups[cred->cr_ngroups]);
1288 for (gp = cred->cr_groups; gp < egp; gp++)
1295 * `suser_enabled' (which can be set by the security.suser_enabled
1296 * sysctl) determines whether the system 'super-user' policy is in effect.
1297 * If it is nonzero, an effective uid of 0 connotes special privilege,
1298 * overriding many mandatory and discretionary protections. If it is zero,
1299 * uid 0 is offered no special privilege in the kernel security policy.
1300 * Setting it to zero may seriously impact the functionality of many
1301 * existing userland programs, and should not be done without careful
1302 * consideration of the consequences.
1304 int suser_enabled = 1;
1305 SYSCTL_INT(_security_bsd, OID_AUTO, suser_enabled, CTLFLAG_RW,
1306 &suser_enabled, 0, "processes with uid 0 have privilege");
1307 TUNABLE_INT("security.bsd.suser_enabled", &suser_enabled);
1310 * Test whether the specified credentials imply "super-user" privilege.
1311 * Return 0 or EPERM.
1314 suser_cred(struct ucred *cred, int flag)
1319 if (((flag & SUSER_RUID) ? cred->cr_ruid : cred->cr_uid) != 0)
1321 if (jailed(cred) && !(flag & SUSER_ALLOWJAIL))
1327 * Shortcut to hide contents of struct td and struct proc from the
1328 * caller, promoting binary compatibility.
1331 suser(struct thread *td)
1335 if (td != curthread) {
1336 printf("suser: thread %p (%d %s) != curthread %p (%d %s)\n",
1337 td, td->td_proc->p_pid, td->td_proc->p_comm,
1338 curthread, curthread->td_proc->p_pid,
1339 curthread->td_proc->p_comm);
1345 return (suser_cred(td->td_ucred, 0));
1349 * Test the active securelevel against a given level. securelevel_gt()
1350 * implements (securelevel > level). securelevel_ge() implements
1351 * (securelevel >= level). Note that the logic is inverted -- these
1352 * functions return EPERM on "success" and 0 on "failure".
1357 securelevel_gt(struct ucred *cr, int level)
1359 int active_securelevel;
1361 active_securelevel = securelevel;
1362 KASSERT(cr != NULL, ("securelevel_gt: null cr"));
1363 if (cr->cr_prison != NULL)
1364 active_securelevel = imax(cr->cr_prison->pr_securelevel,
1365 active_securelevel);
1366 return (active_securelevel > level ? EPERM : 0);
1370 securelevel_ge(struct ucred *cr, int level)
1372 int active_securelevel;
1374 active_securelevel = securelevel;
1375 KASSERT(cr != NULL, ("securelevel_ge: null cr"));
1376 if (cr->cr_prison != NULL)
1377 active_securelevel = imax(cr->cr_prison->pr_securelevel,
1378 active_securelevel);
1379 return (active_securelevel >= level ? EPERM : 0);
1383 * 'see_other_uids' determines whether or not visibility of processes
1384 * and sockets with credentials holding different real uids is possible
1385 * using a variety of system MIBs.
1386 * XXX: data declarations should be together near the beginning of the file.
1388 static int see_other_uids = 1;
1389 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_uids, CTLFLAG_RW,
1391 "Unprivileged processes may see subjects/objects with different real uid");
1394 * Determine if u1 "can see" the subject specified by u2, according to the
1395 * 'see_other_uids' policy.
1396 * Returns: 0 for permitted, ESRCH otherwise
1398 * References: *u1 and *u2 must not change during the call
1399 * u1 may equal u2, in which case only one reference is required
1402 cr_seeotheruids(struct ucred *u1, struct ucred *u2)
1405 if (!see_other_uids && u1->cr_ruid != u2->cr_ruid) {
1406 if (suser_cred(u1, SUSER_ALLOWJAIL) != 0)
1413 * 'see_other_gids' determines whether or not visibility of processes
1414 * and sockets with credentials holding different real gids is possible
1415 * using a variety of system MIBs.
1416 * XXX: data declarations should be together near the beginning of the file.
1418 static int see_other_gids = 1;
1419 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_gids, CTLFLAG_RW,
1421 "Unprivileged processes may see subjects/objects with different real gid");
1424 * Determine if u1 can "see" the subject specified by u2, according to the
1425 * 'see_other_gids' policy.
1426 * Returns: 0 for permitted, ESRCH otherwise
1428 * References: *u1 and *u2 must not change during the call
1429 * u1 may equal u2, in which case only one reference is required
1432 cr_seeothergids(struct ucred *u1, struct ucred *u2)
1436 if (!see_other_gids) {
1438 for (i = 0; i < u1->cr_ngroups; i++) {
1439 if (groupmember(u1->cr_groups[i], u2))
1445 if (suser_cred(u1, SUSER_ALLOWJAIL) != 0)
1453 * Determine if u1 "can see" the subject specified by u2.
1454 * Returns: 0 for permitted, an errno value otherwise
1456 * References: *u1 and *u2 must not change during the call
1457 * u1 may equal u2, in which case only one reference is required
1460 cr_cansee(struct ucred *u1, struct ucred *u2)
1464 if ((error = prison_check(u1, u2)))
1467 if ((error = mac_check_cred_visible(u1, u2)))
1470 if ((error = cr_seeotheruids(u1, u2)))
1472 if ((error = cr_seeothergids(u1, u2)))
1478 * Determine if td "can see" the subject specified by p.
1479 * Returns: 0 for permitted, an errno value otherwise
1480 * Locks: Sufficient locks to protect p->p_ucred must be held. td really
1481 * should be curthread.
1482 * References: td and p must be valid for the lifetime of the call
1485 p_cansee(struct thread *td, struct proc *p)
1488 /* Wrap cr_cansee() for all functionality. */
1489 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1490 PROC_LOCK_ASSERT(p, MA_OWNED);
1491 return (cr_cansee(td->td_ucred, p->p_ucred));
1495 * 'conservative_signals' prevents the delivery of a broad class of
1496 * signals by unprivileged processes to processes that have changed their
1497 * credentials since the last invocation of execve(). This can prevent
1498 * the leakage of cached information or retained privileges as a result
1499 * of a common class of signal-related vulnerabilities. However, this
1500 * may interfere with some applications that expect to be able to
1501 * deliver these signals to peer processes after having given up
1504 static int conservative_signals = 1;
1505 SYSCTL_INT(_security_bsd, OID_AUTO, conservative_signals, CTLFLAG_RW,
1506 &conservative_signals, 0, "Unprivileged processes prevented from "
1507 "sending certain signals to processes whose credentials have changed");
1509 * Determine whether cred may deliver the specified signal to proc.
1510 * Returns: 0 for permitted, an errno value otherwise.
1511 * Locks: A lock must be held for proc.
1512 * References: cred and proc must be valid for the lifetime of the call.
1515 cr_cansignal(struct ucred *cred, struct proc *proc, int signum)
1519 PROC_LOCK_ASSERT(proc, MA_OWNED);
1521 * Jail semantics limit the scope of signalling to proc in the
1522 * same jail as cred, if cred is in jail.
1524 error = prison_check(cred, proc->p_ucred);
1528 if ((error = mac_check_proc_signal(cred, proc, signum)))
1531 if ((error = cr_seeotheruids(cred, proc->p_ucred)))
1533 if ((error = cr_seeothergids(cred, proc->p_ucred)))
1537 * UNIX signal semantics depend on the status of the P_SUGID
1538 * bit on the target process. If the bit is set, then additional
1539 * restrictions are placed on the set of available signals.
1541 if (conservative_signals && (proc->p_flag & P_SUGID)) {
1556 * Generally, permit job and terminal control
1561 /* Not permitted without privilege. */
1562 error = suser_cred(cred, SUSER_ALLOWJAIL);
1569 * Generally, the target credential's ruid or svuid must match the
1570 * subject credential's ruid or euid.
1572 if (cred->cr_ruid != proc->p_ucred->cr_ruid &&
1573 cred->cr_ruid != proc->p_ucred->cr_svuid &&
1574 cred->cr_uid != proc->p_ucred->cr_ruid &&
1575 cred->cr_uid != proc->p_ucred->cr_svuid) {
1576 /* Not permitted without privilege. */
1577 error = suser_cred(cred, SUSER_ALLOWJAIL);
1587 * Determine whether td may deliver the specified signal to p.
1588 * Returns: 0 for permitted, an errno value otherwise
1589 * Locks: Sufficient locks to protect various components of td and p
1590 * must be held. td must be curthread, and a lock must be
1592 * References: td and p must be valid for the lifetime of the call
1595 p_cansignal(struct thread *td, struct proc *p, int signum)
1598 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1599 PROC_LOCK_ASSERT(p, MA_OWNED);
1600 if (td->td_proc == p)
1604 * UNIX signalling semantics require that processes in the same
1605 * session always be able to deliver SIGCONT to one another,
1606 * overriding the remaining protections.
1608 /* XXX: This will require an additional lock of some sort. */
1609 if (signum == SIGCONT && td->td_proc->p_session == p->p_session)
1612 * Some compat layers use SIGTHR and higher signals for
1613 * communication between different kernel threads of the same
1614 * process, so that they expect that it's always possible to
1615 * deliver them, even for suid applications where cr_cansignal() can
1616 * deny such ability for security consideration. It should be
1617 * pretty safe to do since the only way to create two processes
1618 * with the same p_leader is via rfork(2).
1620 if (td->td_proc->p_leader != NULL && signum >= SIGTHR &&
1621 signum < SIGTHR + 4 && td->td_proc->p_leader == p->p_leader)
1624 return (cr_cansignal(td->td_ucred, p, signum));
1628 * Determine whether td may reschedule p.
1629 * Returns: 0 for permitted, an errno value otherwise
1630 * Locks: Sufficient locks to protect various components of td and p
1631 * must be held. td must be curthread, and a lock must
1633 * References: td and p must be valid for the lifetime of the call
1636 p_cansched(struct thread *td, struct proc *p)
1640 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1641 PROC_LOCK_ASSERT(p, MA_OWNED);
1642 if (td->td_proc == p)
1644 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1647 if ((error = mac_check_proc_sched(td->td_ucred, p)))
1650 if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1652 if ((error = cr_seeothergids(td->td_ucred, p->p_ucred)))
1654 if (td->td_ucred->cr_ruid == p->p_ucred->cr_ruid)
1656 if (td->td_ucred->cr_uid == p->p_ucred->cr_ruid)
1658 if (suser_cred(td->td_ucred, SUSER_ALLOWJAIL) == 0)
1662 if (!cap_check(NULL, td, CAP_SYS_NICE, SUSER_ALLOWJAIL))
1670 * The 'unprivileged_proc_debug' flag may be used to disable a variety of
1671 * unprivileged inter-process debugging services, including some procfs
1672 * functionality, ptrace(), and ktrace(). In the past, inter-process
1673 * debugging has been involved in a variety of security problems, and sites
1674 * not requiring the service might choose to disable it when hardening
1677 * XXX: Should modifying and reading this variable require locking?
1678 * XXX: data declarations should be together near the beginning of the file.
1680 static int unprivileged_proc_debug = 1;
1681 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_proc_debug, CTLFLAG_RW,
1682 &unprivileged_proc_debug, 0,
1683 "Unprivileged processes may use process debugging facilities");
1686 * Determine whether td may debug p.
1687 * Returns: 0 for permitted, an errno value otherwise
1688 * Locks: Sufficient locks to protect various components of td and p
1689 * must be held. td must be curthread, and a lock must
1691 * References: td and p must be valid for the lifetime of the call
1694 p_candebug(struct thread *td, struct proc *p)
1696 int credentialchanged, error, grpsubset, i, uidsubset;
1698 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1699 PROC_LOCK_ASSERT(p, MA_OWNED);
1700 if (!unprivileged_proc_debug) {
1701 error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL);
1705 if (td->td_proc == p)
1707 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1710 if ((error = mac_check_proc_debug(td->td_ucred, p)))
1713 if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1715 if ((error = cr_seeothergids(td->td_ucred, p->p_ucred)))
1719 * Is p's group set a subset of td's effective group set? This
1720 * includes p's egid, group access list, rgid, and svgid.
1723 for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
1724 if (!groupmember(p->p_ucred->cr_groups[i], td->td_ucred)) {
1729 grpsubset = grpsubset &&
1730 groupmember(p->p_ucred->cr_rgid, td->td_ucred) &&
1731 groupmember(p->p_ucred->cr_svgid, td->td_ucred);
1734 * Are the uids present in p's credential equal to td's
1735 * effective uid? This includes p's euid, svuid, and ruid.
1737 uidsubset = (td->td_ucred->cr_uid == p->p_ucred->cr_uid &&
1738 td->td_ucred->cr_uid == p->p_ucred->cr_svuid &&
1739 td->td_ucred->cr_uid == p->p_ucred->cr_ruid);
1742 * Has the credential of the process changed since the last exec()?
1744 credentialchanged = (p->p_flag & P_SUGID);
1747 * If p's gids aren't a subset, or the uids aren't a subset,
1748 * or the credential has changed, require appropriate privilege
1749 * for td to debug p. For POSIX.1e capabilities, this will
1750 * require CAP_SYS_PTRACE.
1752 if (!grpsubset || !uidsubset || credentialchanged) {
1753 error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL);
1758 /* Can't trace init when securelevel > 0. */
1759 if (p == initproc) {
1760 error = securelevel_gt(td->td_ucred, 0);
1766 * 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 = suser_cred(td->td_ucred, SUSER_ALLOWJAIL);
2031 error = copyinstr(uap->namebuf, logintmp, sizeof(logintmp), NULL);
2032 if (error == ENAMETOOLONG)
2036 SESS_LOCK(p->p_session);
2037 (void) memcpy(p->p_session->s_login, logintmp,
2039 SESS_UNLOCK(p->p_session);
2046 setsugid(struct proc *p)
2049 PROC_LOCK_ASSERT(p, MA_OWNED);
2050 p->p_flag |= P_SUGID;
2051 if (!(p->p_pfsflags & PF_ISUGID))
2056 * Change a process's effective uid.
2057 * Side effects: newcred->cr_uid and newcred->cr_uidinfo will be modified.
2058 * References: newcred must be an exclusive credential reference for the
2059 * duration of the call.
2062 change_euid(struct ucred *newcred, struct uidinfo *euip)
2065 newcred->cr_uid = euip->ui_uid;
2067 uifree(newcred->cr_uidinfo);
2068 newcred->cr_uidinfo = euip;
2072 * Change a process's effective gid.
2073 * Side effects: newcred->cr_gid will be modified.
2074 * References: newcred must be an exclusive credential reference for the
2075 * duration of the call.
2078 change_egid(struct ucred *newcred, gid_t egid)
2081 newcred->cr_groups[0] = egid;
2085 * Change a process's real uid.
2086 * Side effects: newcred->cr_ruid will be updated, newcred->cr_ruidinfo
2087 * will be updated, and the old and new cr_ruidinfo proc
2088 * counts will be updated.
2089 * References: newcred must be an exclusive credential reference for the
2090 * duration of the call.
2093 change_ruid(struct ucred *newcred, struct uidinfo *ruip)
2096 (void)chgproccnt(newcred->cr_ruidinfo, -1, 0);
2097 newcred->cr_ruid = ruip->ui_uid;
2099 uifree(newcred->cr_ruidinfo);
2100 newcred->cr_ruidinfo = ruip;
2101 (void)chgproccnt(newcred->cr_ruidinfo, 1, 0);
2105 * Change a process's real gid.
2106 * Side effects: newcred->cr_rgid will be updated.
2107 * References: newcred must be an exclusive credential reference for the
2108 * duration of the call.
2111 change_rgid(struct ucred *newcred, gid_t rgid)
2114 newcred->cr_rgid = rgid;
2118 * Change a process's saved uid.
2119 * Side effects: newcred->cr_svuid will be updated.
2120 * References: newcred must be an exclusive credential reference for the
2121 * duration of the call.
2124 change_svuid(struct ucred *newcred, uid_t svuid)
2127 newcred->cr_svuid = svuid;
2131 * Change a process's saved gid.
2132 * Side effects: newcred->cr_svgid will be updated.
2133 * References: newcred must be an exclusive credential reference for the
2134 * duration of the call.
2137 change_svgid(struct ucred *newcred, gid_t svgid)
2140 newcred->cr_svgid = svgid;