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_
85 getpid(struct thread *td, struct getpid_args *uap)
87 struct proc *p = td->td_proc;
89 td->td_retval[0] = p->p_pid;
90 #if defined(COMPAT_43)
92 td->td_retval[1] = p->p_pptr->p_pid;
98 #ifndef _SYS_SYSPROTO_H_
105 getppid(struct thread *td, struct getppid_args *uap)
107 struct proc *p = td->td_proc;
110 td->td_retval[0] = p->p_pptr->p_pid;
116 * Get process group ID; note that POSIX getpgrp takes no parameter.
118 #ifndef _SYS_SYSPROTO_H_
119 struct getpgrp_args {
124 getpgrp(struct thread *td, struct getpgrp_args *uap)
126 struct proc *p = td->td_proc;
129 td->td_retval[0] = p->p_pgrp->pg_id;
134 /* Get an arbitary pid's process group id */
135 #ifndef _SYS_SYSPROTO_H_
136 struct getpgid_args {
141 getpgid(struct thread *td, struct getpgid_args *uap)
153 error = p_cansee(td, p);
159 td->td_retval[0] = p->p_pgrp->pg_id;
165 * Get an arbitary pid's session id.
167 #ifndef _SYS_SYSPROTO_H_
173 getsid(struct thread *td, struct getsid_args *uap)
185 error = p_cansee(td, p);
191 td->td_retval[0] = p->p_session->s_sid;
196 #ifndef _SYS_SYSPROTO_H_
203 getuid(struct thread *td, struct getuid_args *uap)
206 td->td_retval[0] = td->td_ucred->cr_ruid;
207 #if defined(COMPAT_43)
208 td->td_retval[1] = td->td_ucred->cr_uid;
213 #ifndef _SYS_SYSPROTO_H_
214 struct geteuid_args {
220 geteuid(struct thread *td, struct geteuid_args *uap)
223 td->td_retval[0] = td->td_ucred->cr_uid;
227 #ifndef _SYS_SYSPROTO_H_
234 getgid(struct thread *td, struct getgid_args *uap)
237 td->td_retval[0] = td->td_ucred->cr_rgid;
238 #if defined(COMPAT_43)
239 td->td_retval[1] = td->td_ucred->cr_groups[0];
245 * Get effective group ID. The "egid" is groups[0], and could be obtained
246 * via getgroups. This syscall exists because it is somewhat painful to do
247 * correctly in a library function.
249 #ifndef _SYS_SYSPROTO_H_
250 struct getegid_args {
256 getegid(struct thread *td, struct getegid_args *uap)
259 td->td_retval[0] = td->td_ucred->cr_groups[0];
263 #ifndef _SYS_SYSPROTO_H_
264 struct getgroups_args {
270 getgroups(struct thread *td, register struct getgroups_args *uap)
272 gid_t groups[NGROUPS];
276 ngrp = MIN(uap->gidsetsize, NGROUPS);
277 error = kern_getgroups(td, &ngrp, groups);
280 if (uap->gidsetsize > 0)
281 error = copyout(groups, uap->gidset, ngrp * sizeof(gid_t));
283 td->td_retval[0] = ngrp;
288 kern_getgroups(struct thread *td, u_int *ngrp, gid_t *groups)
294 *ngrp = cred->cr_ngroups;
297 if (*ngrp < cred->cr_ngroups)
299 *ngrp = cred->cr_ngroups;
300 bcopy(cred->cr_groups, groups, *ngrp * sizeof(gid_t));
304 #ifndef _SYS_SYSPROTO_H_
311 setsid(register struct thread *td, struct setsid_args *uap)
315 struct proc *p = td->td_proc;
316 struct pgrp *newpgrp;
317 struct session *newsess;
322 MALLOC(newpgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
323 MALLOC(newsess, struct session *, sizeof(struct session), M_SESSION, M_WAITOK | M_ZERO);
325 sx_xlock(&proctree_lock);
327 if (p->p_pgid == p->p_pid || (pgrp = pgfind(p->p_pid)) != NULL) {
332 (void)enterpgrp(p, p->p_pid, newpgrp, newsess);
333 td->td_retval[0] = p->p_pid;
338 sx_xunlock(&proctree_lock);
341 FREE(newpgrp, M_PGRP);
343 FREE(newsess, M_SESSION);
349 * set process group (setpgid/old setpgrp)
351 * caller does setpgid(targpid, targpgid)
353 * pid must be caller or child of caller (ESRCH)
355 * pid must be in same session (EPERM)
356 * pid can't have done an exec (EACCES)
358 * there must exist some pid in same session having pgid (EPERM)
359 * pid must not be session leader (EPERM)
361 #ifndef _SYS_SYSPROTO_H_
362 struct setpgid_args {
363 int pid; /* target process id */
364 int pgid; /* target pgrp id */
369 setpgid(struct thread *td, register struct setpgid_args *uap)
371 struct proc *curp = td->td_proc;
372 register struct proc *targp; /* target process */
373 register struct pgrp *pgrp; /* target pgrp */
375 struct pgrp *newpgrp;
382 MALLOC(newpgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
384 sx_xlock(&proctree_lock);
385 if (uap->pid != 0 && uap->pid != curp->p_pid) {
386 if ((targp = pfind(uap->pid)) == NULL) {
390 if (!inferior(targp)) {
395 if ((error = p_cansee(td, targp))) {
399 if (targp->p_pgrp == NULL ||
400 targp->p_session != curp->p_session) {
405 if (targp->p_flag & P_EXEC) {
413 if (SESS_LEADER(targp)) {
418 uap->pgid = targp->p_pid;
419 if ((pgrp = pgfind(uap->pgid)) == NULL) {
420 if (uap->pgid == targp->p_pid) {
421 error = enterpgrp(targp, uap->pgid, newpgrp,
428 if (pgrp == targp->p_pgrp) {
432 if (pgrp->pg_id != targp->p_pid &&
433 pgrp->pg_session != curp->p_session) {
439 error = enterthispgrp(targp, pgrp);
442 sx_xunlock(&proctree_lock);
443 KASSERT((error == 0) || (newpgrp != NULL),
444 ("setpgid failed and newpgrp is NULL"));
446 FREE(newpgrp, M_PGRP);
451 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
452 * compatible. It says that setting the uid/gid to euid/egid is a special
453 * case of "appropriate privilege". Once the rules are expanded out, this
454 * basically means that setuid(nnn) sets all three id's, in all permitted
455 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
456 * does not set the saved id - this is dangerous for traditional BSD
457 * programs. For this reason, we *really* do not want to set
458 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
460 #define POSIX_APPENDIX_B_4_2_2
462 #ifndef _SYS_SYSPROTO_H_
469 setuid(struct thread *td, struct setuid_args *uap)
471 struct proc *p = td->td_proc;
472 struct ucred *newcred, *oldcred;
482 oldcred = p->p_ucred;
485 error = mac_proc_check_setuid(p, oldcred, uid);
491 * See if we have "permission" by POSIX 1003.1 rules.
493 * Note that setuid(geteuid()) is a special case of
494 * "appropriate privileges" in appendix B.4.2.2. We need
495 * to use this clause to be compatible with traditional BSD
496 * semantics. Basically, it means that "setuid(xx)" sets all
497 * three id's (assuming you have privs).
499 * Notes on the logic. We do things in three steps.
500 * 1: We determine if the euid is going to change, and do EPERM
501 * right away. We unconditionally change the euid later if this
502 * test is satisfied, simplifying that part of the logic.
503 * 2: We determine if the real and/or saved uids are going to
504 * change. Determined by compile options.
505 * 3: Change euid last. (after tests in #2 for "appropriate privs")
507 if (uid != oldcred->cr_ruid && /* allow setuid(getuid()) */
508 #ifdef _POSIX_SAVED_IDS
509 uid != oldcred->cr_svuid && /* allow setuid(saved gid) */
511 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
512 uid != oldcred->cr_uid && /* allow setuid(geteuid()) */
514 (error = priv_check_cred(oldcred, PRIV_CRED_SETUID, 0)) != 0)
518 * Copy credentials so other references do not see our changes.
520 crcopy(newcred, oldcred);
521 #ifdef _POSIX_SAVED_IDS
523 * Do we have "appropriate privileges" (are we root or uid == euid)
524 * If so, we are changing the real uid and/or saved uid.
527 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
528 uid == oldcred->cr_uid ||
530 /* We are using privs. */
531 priv_check_cred(oldcred, PRIV_CRED_SETUID, 0) == 0)
535 * Set the real uid and transfer proc count to new user.
537 if (uid != oldcred->cr_ruid) {
538 change_ruid(newcred, uip);
544 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
545 * the security of seteuid() depends on it. B.4.2.2 says it
546 * is important that we should do this.
548 if (uid != oldcred->cr_svuid) {
549 change_svuid(newcred, uid);
555 * In all permitted cases, we are changing the euid.
557 if (uid != oldcred->cr_uid) {
558 change_euid(newcred, uip);
561 p->p_ucred = newcred;
574 #ifndef _SYS_SYSPROTO_H_
575 struct seteuid_args {
581 seteuid(struct thread *td, struct seteuid_args *uap)
583 struct proc *p = td->td_proc;
584 struct ucred *newcred, *oldcred;
586 struct uidinfo *euip;
590 AUDIT_ARG(euid, euid);
594 oldcred = p->p_ucred;
597 error = mac_proc_check_seteuid(p, oldcred, euid);
602 if (euid != oldcred->cr_ruid && /* allow seteuid(getuid()) */
603 euid != oldcred->cr_svuid && /* allow seteuid(saved uid) */
604 (error = priv_check_cred(oldcred, PRIV_CRED_SETEUID, 0)) != 0)
608 * Everything's okay, do it. Copy credentials so other references do
609 * not see our changes.
611 crcopy(newcred, oldcred);
612 if (oldcred->cr_uid != euid) {
613 change_euid(newcred, euip);
616 p->p_ucred = newcred;
629 #ifndef _SYS_SYSPROTO_H_
636 setgid(struct thread *td, struct setgid_args *uap)
638 struct proc *p = td->td_proc;
639 struct ucred *newcred, *oldcred;
647 oldcred = p->p_ucred;
650 error = mac_proc_check_setgid(p, oldcred, gid);
656 * See if we have "permission" by POSIX 1003.1 rules.
658 * Note that setgid(getegid()) is a special case of
659 * "appropriate privileges" in appendix B.4.2.2. We need
660 * to use this clause to be compatible with traditional BSD
661 * semantics. Basically, it means that "setgid(xx)" sets all
662 * three id's (assuming you have privs).
664 * For notes on the logic here, see setuid() above.
666 if (gid != oldcred->cr_rgid && /* allow setgid(getgid()) */
667 #ifdef _POSIX_SAVED_IDS
668 gid != oldcred->cr_svgid && /* allow setgid(saved gid) */
670 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
671 gid != oldcred->cr_groups[0] && /* allow setgid(getegid()) */
673 (error = priv_check_cred(oldcred, PRIV_CRED_SETGID, 0)) != 0)
676 crcopy(newcred, oldcred);
677 #ifdef _POSIX_SAVED_IDS
679 * Do we have "appropriate privileges" (are we root or gid == egid)
680 * If so, we are changing the real uid and saved gid.
683 #ifdef POSIX_APPENDIX_B_4_2_2 /* use the clause from B.4.2.2 */
684 gid == oldcred->cr_groups[0] ||
686 /* We are using privs. */
687 priv_check_cred(oldcred, PRIV_CRED_SETGID, 0) == 0)
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;
728 #ifndef _SYS_SYSPROTO_H_
729 struct setegid_args {
735 setegid(struct thread *td, struct setegid_args *uap)
737 struct proc *p = td->td_proc;
738 struct ucred *newcred, *oldcred;
743 AUDIT_ARG(egid, egid);
746 oldcred = p->p_ucred;
749 error = mac_proc_check_setegid(p, oldcred, egid);
754 if (egid != oldcred->cr_rgid && /* allow setegid(getgid()) */
755 egid != oldcred->cr_svgid && /* allow setegid(saved gid) */
756 (error = priv_check_cred(oldcred, PRIV_CRED_SETEGID, 0)) != 0)
759 crcopy(newcred, oldcred);
760 if (oldcred->cr_groups[0] != egid) {
761 change_egid(newcred, egid);
764 p->p_ucred = newcred;
775 #ifndef _SYS_SYSPROTO_H_
776 struct setgroups_args {
783 setgroups(struct thread *td, struct setgroups_args *uap)
785 gid_t groups[NGROUPS];
788 if (uap->gidsetsize > NGROUPS)
790 error = copyin(uap->gidset, groups, uap->gidsetsize * sizeof(gid_t));
793 return (kern_setgroups(td, uap->gidsetsize, groups));
797 kern_setgroups(struct thread *td, u_int ngrp, gid_t *groups)
799 struct proc *p = td->td_proc;
800 struct ucred *newcred, *oldcred;
805 AUDIT_ARG(groupset, groups, ngrp);
808 oldcred = p->p_ucred;
811 error = mac_proc_check_setgroups(p, oldcred, ngrp, groups);
816 error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0);
821 * XXX A little bit lazy here. We could test if anything has
822 * changed before crcopy() and setting P_SUGID.
824 crcopy(newcred, oldcred);
827 * setgroups(0, NULL) is a legitimate way of clearing the
828 * groups vector on non-BSD systems (which generally do not
829 * have the egid in the groups[0]). We risk security holes
830 * when running non-BSD software if we do not do the same.
832 newcred->cr_ngroups = 1;
834 bcopy(groups, newcred->cr_groups, ngrp * sizeof(gid_t));
835 newcred->cr_ngroups = ngrp;
838 p->p_ucred = newcred;
849 #ifndef _SYS_SYSPROTO_H_
850 struct setreuid_args {
857 setreuid(register struct thread *td, struct setreuid_args *uap)
859 struct proc *p = td->td_proc;
860 struct ucred *newcred, *oldcred;
862 struct uidinfo *euip, *ruip;
867 AUDIT_ARG(euid, euid);
868 AUDIT_ARG(ruid, ruid);
873 oldcred = p->p_ucred;
876 error = mac_proc_check_setreuid(p, oldcred, ruid, euid);
881 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
882 ruid != oldcred->cr_svuid) ||
883 (euid != (uid_t)-1 && euid != oldcred->cr_uid &&
884 euid != oldcred->cr_ruid && euid != oldcred->cr_svuid)) &&
885 (error = priv_check_cred(oldcred, PRIV_CRED_SETREUID, 0)) != 0)
888 crcopy(newcred, oldcred);
889 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
890 change_euid(newcred, euip);
893 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
894 change_ruid(newcred, ruip);
897 if ((ruid != (uid_t)-1 || newcred->cr_uid != newcred->cr_ruid) &&
898 newcred->cr_svuid != newcred->cr_uid) {
899 change_svuid(newcred, newcred->cr_uid);
902 p->p_ucred = newcred;
917 #ifndef _SYS_SYSPROTO_H_
918 struct setregid_args {
925 setregid(register struct thread *td, struct setregid_args *uap)
927 struct proc *p = td->td_proc;
928 struct ucred *newcred, *oldcred;
934 AUDIT_ARG(egid, egid);
935 AUDIT_ARG(rgid, rgid);
938 oldcred = p->p_ucred;
941 error = mac_proc_check_setregid(p, oldcred, rgid, egid);
946 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
947 rgid != oldcred->cr_svgid) ||
948 (egid != (gid_t)-1 && egid != oldcred->cr_groups[0] &&
949 egid != oldcred->cr_rgid && egid != oldcred->cr_svgid)) &&
950 (error = priv_check_cred(oldcred, PRIV_CRED_SETREGID, 0)) != 0)
953 crcopy(newcred, oldcred);
954 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
955 change_egid(newcred, egid);
958 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
959 change_rgid(newcred, rgid);
962 if ((rgid != (gid_t)-1 || newcred->cr_groups[0] != newcred->cr_rgid) &&
963 newcred->cr_svgid != newcred->cr_groups[0]) {
964 change_svgid(newcred, newcred->cr_groups[0]);
967 p->p_ucred = newcred;
979 * setresuid(ruid, euid, suid) is like setreuid except control over the saved
982 #ifndef _SYS_SYSPROTO_H_
983 struct setresuid_args {
991 setresuid(register struct thread *td, struct setresuid_args *uap)
993 struct proc *p = td->td_proc;
994 struct ucred *newcred, *oldcred;
995 uid_t euid, ruid, suid;
996 struct uidinfo *euip, *ruip;
1002 AUDIT_ARG(euid, euid);
1003 AUDIT_ARG(ruid, ruid);
1004 AUDIT_ARG(suid, suid);
1006 euip = uifind(euid);
1007 ruip = uifind(ruid);
1009 oldcred = p->p_ucred;
1012 error = mac_proc_check_setresuid(p, oldcred, ruid, euid, suid);
1017 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
1018 ruid != oldcred->cr_svuid &&
1019 ruid != oldcred->cr_uid) ||
1020 (euid != (uid_t)-1 && euid != oldcred->cr_ruid &&
1021 euid != oldcred->cr_svuid &&
1022 euid != oldcred->cr_uid) ||
1023 (suid != (uid_t)-1 && suid != oldcred->cr_ruid &&
1024 suid != oldcred->cr_svuid &&
1025 suid != oldcred->cr_uid)) &&
1026 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESUID, 0)) != 0)
1029 crcopy(newcred, oldcred);
1030 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
1031 change_euid(newcred, euip);
1034 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
1035 change_ruid(newcred, ruip);
1038 if (suid != (uid_t)-1 && oldcred->cr_svuid != suid) {
1039 change_svuid(newcred, suid);
1042 p->p_ucred = newcred;
1059 * setresgid(rgid, egid, sgid) is like setregid except control over the saved
1062 #ifndef _SYS_SYSPROTO_H_
1063 struct setresgid_args {
1071 setresgid(register struct thread *td, struct setresgid_args *uap)
1073 struct proc *p = td->td_proc;
1074 struct ucred *newcred, *oldcred;
1075 gid_t egid, rgid, sgid;
1081 AUDIT_ARG(egid, egid);
1082 AUDIT_ARG(rgid, rgid);
1083 AUDIT_ARG(sgid, sgid);
1086 oldcred = p->p_ucred;
1089 error = mac_proc_check_setresgid(p, oldcred, rgid, egid, sgid);
1094 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
1095 rgid != oldcred->cr_svgid &&
1096 rgid != oldcred->cr_groups[0]) ||
1097 (egid != (gid_t)-1 && egid != oldcred->cr_rgid &&
1098 egid != oldcred->cr_svgid &&
1099 egid != oldcred->cr_groups[0]) ||
1100 (sgid != (gid_t)-1 && sgid != oldcred->cr_rgid &&
1101 sgid != oldcred->cr_svgid &&
1102 sgid != oldcred->cr_groups[0])) &&
1103 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESGID, 0)) != 0)
1106 crcopy(newcred, oldcred);
1107 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
1108 change_egid(newcred, egid);
1111 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
1112 change_rgid(newcred, rgid);
1115 if (sgid != (gid_t)-1 && oldcred->cr_svgid != sgid) {
1116 change_svgid(newcred, sgid);
1119 p->p_ucred = newcred;
1130 #ifndef _SYS_SYSPROTO_H_
1131 struct getresuid_args {
1139 getresuid(register struct thread *td, struct getresuid_args *uap)
1142 int error1 = 0, error2 = 0, error3 = 0;
1144 cred = td->td_ucred;
1146 error1 = copyout(&cred->cr_ruid,
1147 uap->ruid, sizeof(cred->cr_ruid));
1149 error2 = copyout(&cred->cr_uid,
1150 uap->euid, sizeof(cred->cr_uid));
1152 error3 = copyout(&cred->cr_svuid,
1153 uap->suid, sizeof(cred->cr_svuid));
1154 return (error1 ? error1 : error2 ? error2 : error3);
1157 #ifndef _SYS_SYSPROTO_H_
1158 struct getresgid_args {
1166 getresgid(register struct thread *td, struct getresgid_args *uap)
1169 int error1 = 0, error2 = 0, error3 = 0;
1171 cred = td->td_ucred;
1173 error1 = copyout(&cred->cr_rgid,
1174 uap->rgid, sizeof(cred->cr_rgid));
1176 error2 = copyout(&cred->cr_groups[0],
1177 uap->egid, sizeof(cred->cr_groups[0]));
1179 error3 = copyout(&cred->cr_svgid,
1180 uap->sgid, sizeof(cred->cr_svgid));
1181 return (error1 ? error1 : error2 ? error2 : error3);
1184 #ifndef _SYS_SYSPROTO_H_
1185 struct issetugid_args {
1191 issetugid(register struct thread *td, struct issetugid_args *uap)
1193 struct proc *p = td->td_proc;
1196 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
1197 * we use P_SUGID because we consider changing the owners as
1198 * "tainting" as well.
1199 * This is significant for procs that start as root and "become"
1200 * a user without an exec - programs cannot know *everything*
1201 * that libc *might* have put in their data segment.
1204 td->td_retval[0] = (p->p_flag & P_SUGID) ? 1 : 0;
1210 __setugid(struct thread *td, struct __setugid_args *uap)
1216 switch (uap->flag) {
1219 p->p_flag &= ~P_SUGID;
1224 p->p_flag |= P_SUGID;
1230 #else /* !REGRESSION */
1233 #endif /* REGRESSION */
1237 * Check if gid is a member of the group set.
1240 groupmember(gid_t gid, struct ucred *cred)
1245 egp = &(cred->cr_groups[cred->cr_ngroups]);
1246 for (gp = cred->cr_groups; gp < egp; gp++)
1253 * Test the active securelevel against a given level. securelevel_gt()
1254 * implements (securelevel > level). securelevel_ge() implements
1255 * (securelevel >= level). Note that the logic is inverted -- these
1256 * functions return EPERM on "success" and 0 on "failure".
1258 * XXXRW: Possibly since this has to do with privilege, it should move to
1262 securelevel_gt(struct ucred *cr, int level)
1264 int active_securelevel;
1266 active_securelevel = securelevel;
1267 KASSERT(cr != NULL, ("securelevel_gt: null cr"));
1268 if (cr->cr_prison != NULL)
1269 active_securelevel = imax(cr->cr_prison->pr_securelevel,
1270 active_securelevel);
1271 return (active_securelevel > level ? EPERM : 0);
1275 securelevel_ge(struct ucred *cr, int level)
1277 int active_securelevel;
1279 active_securelevel = securelevel;
1280 KASSERT(cr != NULL, ("securelevel_ge: null cr"));
1281 if (cr->cr_prison != NULL)
1282 active_securelevel = imax(cr->cr_prison->pr_securelevel,
1283 active_securelevel);
1284 return (active_securelevel >= level ? EPERM : 0);
1288 * 'see_other_uids' determines whether or not visibility of processes
1289 * and sockets with credentials holding different real uids is possible
1290 * using a variety of system MIBs.
1291 * XXX: data declarations should be together near the beginning of the file.
1293 static int see_other_uids = 1;
1294 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_uids, CTLFLAG_RW,
1296 "Unprivileged processes may see subjects/objects with different real uid");
1299 * Determine if u1 "can see" the subject specified by u2, according to the
1300 * 'see_other_uids' policy.
1301 * Returns: 0 for permitted, ESRCH otherwise
1303 * References: *u1 and *u2 must not change during the call
1304 * u1 may equal u2, in which case only one reference is required
1307 cr_seeotheruids(struct ucred *u1, struct ucred *u2)
1310 if (!see_other_uids && u1->cr_ruid != u2->cr_ruid) {
1311 if (priv_check_cred(u1, PRIV_SEEOTHERUIDS, 0) != 0)
1318 * 'see_other_gids' determines whether or not visibility of processes
1319 * and sockets with credentials holding different real gids is possible
1320 * using a variety of system MIBs.
1321 * XXX: data declarations should be together near the beginning of the file.
1323 static int see_other_gids = 1;
1324 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_gids, CTLFLAG_RW,
1326 "Unprivileged processes may see subjects/objects with different real gid");
1329 * Determine if u1 can "see" the subject specified by u2, according to the
1330 * 'see_other_gids' policy.
1331 * Returns: 0 for permitted, ESRCH otherwise
1333 * References: *u1 and *u2 must not change during the call
1334 * u1 may equal u2, in which case only one reference is required
1337 cr_seeothergids(struct ucred *u1, struct ucred *u2)
1341 if (!see_other_gids) {
1343 for (i = 0; i < u1->cr_ngroups; i++) {
1344 if (groupmember(u1->cr_groups[i], u2))
1350 if (priv_check_cred(u1, PRIV_SEEOTHERGIDS, 0) != 0)
1358 * Determine if u1 "can see" the subject specified by u2.
1359 * Returns: 0 for permitted, an errno value otherwise
1361 * References: *u1 and *u2 must not change during the call
1362 * u1 may equal u2, in which case only one reference is required
1365 cr_cansee(struct ucred *u1, struct ucred *u2)
1369 if ((error = prison_check(u1, u2)))
1372 if ((error = mac_cred_check_visible(u1, u2)))
1375 if ((error = cr_seeotheruids(u1, u2)))
1377 if ((error = cr_seeothergids(u1, u2)))
1383 * Determine if td "can see" the subject specified by p.
1384 * Returns: 0 for permitted, an errno value otherwise
1385 * Locks: Sufficient locks to protect p->p_ucred must be held. td really
1386 * should be curthread.
1387 * References: td and p must be valid for the lifetime of the call
1390 p_cansee(struct thread *td, struct proc *p)
1393 /* Wrap cr_cansee() for all functionality. */
1394 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1395 PROC_LOCK_ASSERT(p, MA_OWNED);
1396 return (cr_cansee(td->td_ucred, p->p_ucred));
1400 * 'conservative_signals' prevents the delivery of a broad class of
1401 * signals by unprivileged processes to processes that have changed their
1402 * credentials since the last invocation of execve(). This can prevent
1403 * the leakage of cached information or retained privileges as a result
1404 * of a common class of signal-related vulnerabilities. However, this
1405 * may interfere with some applications that expect to be able to
1406 * deliver these signals to peer processes after having given up
1409 static int conservative_signals = 1;
1410 SYSCTL_INT(_security_bsd, OID_AUTO, conservative_signals, CTLFLAG_RW,
1411 &conservative_signals, 0, "Unprivileged processes prevented from "
1412 "sending certain signals to processes whose credentials have changed");
1414 * Determine whether cred may deliver the specified signal to proc.
1415 * Returns: 0 for permitted, an errno value otherwise.
1416 * Locks: A lock must be held for proc.
1417 * References: cred and proc must be valid for the lifetime of the call.
1420 cr_cansignal(struct ucred *cred, struct proc *proc, int signum)
1424 PROC_LOCK_ASSERT(proc, MA_OWNED);
1426 * Jail semantics limit the scope of signalling to proc in the
1427 * same jail as cred, if cred is in jail.
1429 error = prison_check(cred, proc->p_ucred);
1433 if ((error = mac_proc_check_signal(cred, proc, signum)))
1436 if ((error = cr_seeotheruids(cred, proc->p_ucred)))
1438 if ((error = cr_seeothergids(cred, proc->p_ucred)))
1442 * UNIX signal semantics depend on the status of the P_SUGID
1443 * bit on the target process. If the bit is set, then additional
1444 * restrictions are placed on the set of available signals.
1446 if (conservative_signals && (proc->p_flag & P_SUGID)) {
1461 * Generally, permit job and terminal control
1466 /* Not permitted without privilege. */
1467 error = priv_check_cred(cred, PRIV_SIGNAL_SUGID, 0);
1474 * Generally, the target credential's ruid or svuid must match the
1475 * subject credential's ruid or euid.
1477 if (cred->cr_ruid != proc->p_ucred->cr_ruid &&
1478 cred->cr_ruid != proc->p_ucred->cr_svuid &&
1479 cred->cr_uid != proc->p_ucred->cr_ruid &&
1480 cred->cr_uid != proc->p_ucred->cr_svuid) {
1481 error = priv_check_cred(cred, PRIV_SIGNAL_DIFFCRED, 0);
1490 * Determine whether td may deliver the specified signal to p.
1491 * Returns: 0 for permitted, an errno value otherwise
1492 * Locks: Sufficient locks to protect various components of td and p
1493 * must be held. td must be curthread, and a lock must be
1495 * References: td and p must be valid for the lifetime of the call
1498 p_cansignal(struct thread *td, struct proc *p, int signum)
1501 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1502 PROC_LOCK_ASSERT(p, MA_OWNED);
1503 if (td->td_proc == p)
1507 * UNIX signalling semantics require that processes in the same
1508 * session always be able to deliver SIGCONT to one another,
1509 * overriding the remaining protections.
1511 /* XXX: This will require an additional lock of some sort. */
1512 if (signum == SIGCONT && td->td_proc->p_session == p->p_session)
1515 * Some compat layers use SIGTHR and higher signals for
1516 * communication between different kernel threads of the same
1517 * process, so that they expect that it's always possible to
1518 * deliver them, even for suid applications where cr_cansignal() can
1519 * deny such ability for security consideration. It should be
1520 * pretty safe to do since the only way to create two processes
1521 * with the same p_leader is via rfork(2).
1523 if (td->td_proc->p_leader != NULL && signum >= SIGTHR &&
1524 signum < SIGTHR + 4 && td->td_proc->p_leader == p->p_leader)
1527 return (cr_cansignal(td->td_ucred, p, signum));
1531 * Determine whether td may reschedule p.
1532 * Returns: 0 for permitted, an errno value otherwise
1533 * Locks: Sufficient locks to protect various components of td and p
1534 * must be held. td must be curthread, and a lock must
1536 * References: td and p must be valid for the lifetime of the call
1539 p_cansched(struct thread *td, struct proc *p)
1543 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1544 PROC_LOCK_ASSERT(p, MA_OWNED);
1545 if (td->td_proc == p)
1547 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1550 if ((error = mac_proc_check_sched(td->td_ucred, p)))
1553 if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1555 if ((error = cr_seeothergids(td->td_ucred, p->p_ucred)))
1557 if (td->td_ucred->cr_ruid != p->p_ucred->cr_ruid &&
1558 td->td_ucred->cr_uid != p->p_ucred->cr_ruid) {
1559 error = priv_check(td, PRIV_SCHED_DIFFCRED);
1567 * The 'unprivileged_proc_debug' flag may be used to disable a variety of
1568 * unprivileged inter-process debugging services, including some procfs
1569 * functionality, ptrace(), and ktrace(). In the past, inter-process
1570 * debugging has been involved in a variety of security problems, and sites
1571 * not requiring the service might choose to disable it when hardening
1574 * XXX: Should modifying and reading this variable require locking?
1575 * XXX: data declarations should be together near the beginning of the file.
1577 static int unprivileged_proc_debug = 1;
1578 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_proc_debug, CTLFLAG_RW,
1579 &unprivileged_proc_debug, 0,
1580 "Unprivileged processes may use process debugging facilities");
1583 * Determine whether td may debug p.
1584 * Returns: 0 for permitted, an errno value otherwise
1585 * Locks: Sufficient locks to protect various components of td and p
1586 * must be held. td must be curthread, and a lock must
1588 * References: td and p must be valid for the lifetime of the call
1591 p_candebug(struct thread *td, struct proc *p)
1593 int credentialchanged, error, grpsubset, i, uidsubset;
1595 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1596 PROC_LOCK_ASSERT(p, MA_OWNED);
1597 if (!unprivileged_proc_debug) {
1598 error = priv_check(td, PRIV_DEBUG_UNPRIV);
1602 if (td->td_proc == p)
1604 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1607 if ((error = mac_proc_check_debug(td->td_ucred, p)))
1610 if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1612 if ((error = cr_seeothergids(td->td_ucred, p->p_ucred)))
1616 * Is p's group set a subset of td's effective group set? This
1617 * includes p's egid, group access list, rgid, and svgid.
1620 for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
1621 if (!groupmember(p->p_ucred->cr_groups[i], td->td_ucred)) {
1626 grpsubset = grpsubset &&
1627 groupmember(p->p_ucred->cr_rgid, td->td_ucred) &&
1628 groupmember(p->p_ucred->cr_svgid, td->td_ucred);
1631 * Are the uids present in p's credential equal to td's
1632 * effective uid? This includes p's euid, svuid, and ruid.
1634 uidsubset = (td->td_ucred->cr_uid == p->p_ucred->cr_uid &&
1635 td->td_ucred->cr_uid == p->p_ucred->cr_svuid &&
1636 td->td_ucred->cr_uid == p->p_ucred->cr_ruid);
1639 * Has the credential of the process changed since the last exec()?
1641 credentialchanged = (p->p_flag & P_SUGID);
1644 * If p's gids aren't a subset, or the uids aren't a subset,
1645 * or the credential has changed, require appropriate privilege
1646 * for td to debug p.
1648 if (!grpsubset || !uidsubset) {
1649 error = priv_check(td, PRIV_DEBUG_DIFFCRED);
1654 if (credentialchanged) {
1655 error = priv_check(td, PRIV_DEBUG_SUGID);
1660 /* Can't trace init when securelevel > 0. */
1661 if (p == initproc) {
1662 error = securelevel_gt(td->td_ucred, 0);
1668 * Can't trace a process that's currently exec'ing.
1670 * XXX: Note, this is not a security policy decision, it's a
1671 * basic correctness/functionality decision. Therefore, this check
1672 * should be moved to the caller's of p_candebug().
1674 if ((p->p_flag & P_INEXEC) != 0)
1681 * Determine whether the subject represented by cred can "see" a socket.
1682 * Returns: 0 for permitted, ENOENT otherwise.
1685 cr_canseesocket(struct ucred *cred, struct socket *so)
1689 error = prison_check(cred, so->so_cred);
1694 error = mac_socket_check_visible(cred, so);
1699 if (cr_seeotheruids(cred, so->so_cred))
1701 if (cr_seeothergids(cred, so->so_cred))
1708 * Determine whether td can wait for the exit of p.
1709 * Returns: 0 for permitted, an errno value otherwise
1710 * Locks: Sufficient locks to protect various components of td and p
1711 * must be held. td must be curthread, and a lock must
1713 * References: td and p must be valid for the lifetime of the call
1717 p_canwait(struct thread *td, struct proc *p)
1721 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1722 PROC_LOCK_ASSERT(p, MA_OWNED);
1723 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1726 if ((error = mac_proc_check_wait(td->td_ucred, p)))
1730 /* XXXMAC: This could have odd effects on some shells. */
1731 if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1739 * Allocate a zeroed cred structure.
1744 register struct ucred *cr;
1746 MALLOC(cr, struct ucred *, sizeof(*cr), M_CRED, M_WAITOK | M_ZERO);
1747 refcount_init(&cr->cr_ref, 1);
1749 audit_cred_init(cr);
1758 * Claim another reference to a ucred structure.
1761 crhold(struct ucred *cr)
1764 refcount_acquire(&cr->cr_ref);
1769 * Free a cred structure. Throws away space when ref count gets to 0.
1772 crfree(struct ucred *cr)
1775 KASSERT(cr->cr_ref > 0, ("bad ucred refcount: %d", cr->cr_ref));
1776 KASSERT(cr->cr_ref != 0xdeadc0de, ("dangling reference to ucred"));
1777 if (refcount_release(&cr->cr_ref)) {
1779 * Some callers of crget(), such as nfs_statfs(),
1780 * allocate a temporary credential, but don't
1781 * allocate a uidinfo structure.
1783 if (cr->cr_uidinfo != NULL)
1784 uifree(cr->cr_uidinfo);
1785 if (cr->cr_ruidinfo != NULL)
1786 uifree(cr->cr_ruidinfo);
1788 * Free a prison, if any.
1791 prison_free(cr->cr_prison);
1793 audit_cred_destroy(cr);
1796 mac_cred_destroy(cr);
1803 * Check to see if this ucred is shared.
1806 crshared(struct ucred *cr)
1809 return (cr->cr_ref > 1);
1813 * Copy a ucred's contents from a template. Does not block.
1816 crcopy(struct ucred *dest, struct ucred *src)
1819 KASSERT(crshared(dest) == 0, ("crcopy of shared ucred"));
1820 bcopy(&src->cr_startcopy, &dest->cr_startcopy,
1821 (unsigned)((caddr_t)&src->cr_endcopy -
1822 (caddr_t)&src->cr_startcopy));
1823 uihold(dest->cr_uidinfo);
1824 uihold(dest->cr_ruidinfo);
1826 prison_hold(dest->cr_prison);
1828 audit_cred_copy(src, dest);
1831 mac_cred_copy(src, dest);
1836 * Dup cred struct to a new held one.
1839 crdup(struct ucred *cr)
1841 struct ucred *newcr;
1849 * Fill in a struct xucred based on a struct ucred.
1852 cru2x(struct ucred *cr, struct xucred *xcr)
1855 bzero(xcr, sizeof(*xcr));
1856 xcr->cr_version = XUCRED_VERSION;
1857 xcr->cr_uid = cr->cr_uid;
1858 xcr->cr_ngroups = cr->cr_ngroups;
1859 bcopy(cr->cr_groups, xcr->cr_groups, sizeof(cr->cr_groups));
1863 * small routine to swap a thread's current ucred for the correct one taken
1867 cred_update_thread(struct thread *td)
1873 cred = td->td_ucred;
1875 td->td_ucred = crhold(p->p_ucred);
1882 * Get login name, if available.
1884 #ifndef _SYS_SYSPROTO_H_
1885 struct getlogin_args {
1892 getlogin(struct thread *td, struct getlogin_args *uap)
1895 char login[MAXLOGNAME];
1896 struct proc *p = td->td_proc;
1898 if (uap->namelen > MAXLOGNAME)
1899 uap->namelen = MAXLOGNAME;
1901 SESS_LOCK(p->p_session);
1902 bcopy(p->p_session->s_login, login, uap->namelen);
1903 SESS_UNLOCK(p->p_session);
1905 error = copyout(login, uap->namebuf, uap->namelen);
1912 #ifndef _SYS_SYSPROTO_H_
1913 struct setlogin_args {
1919 setlogin(struct thread *td, struct setlogin_args *uap)
1921 struct proc *p = td->td_proc;
1923 char logintmp[MAXLOGNAME];
1925 error = priv_check(td, PRIV_PROC_SETLOGIN);
1928 error = copyinstr(uap->namebuf, logintmp, sizeof(logintmp), NULL);
1929 if (error == ENAMETOOLONG)
1933 SESS_LOCK(p->p_session);
1934 (void) memcpy(p->p_session->s_login, logintmp,
1936 SESS_UNLOCK(p->p_session);
1943 setsugid(struct proc *p)
1946 PROC_LOCK_ASSERT(p, MA_OWNED);
1947 p->p_flag |= P_SUGID;
1948 if (!(p->p_pfsflags & PF_ISUGID))
1953 * Change a process's effective uid.
1954 * Side effects: newcred->cr_uid and newcred->cr_uidinfo will be modified.
1955 * References: newcred must be an exclusive credential reference for the
1956 * duration of the call.
1959 change_euid(struct ucred *newcred, struct uidinfo *euip)
1962 newcred->cr_uid = euip->ui_uid;
1964 uifree(newcred->cr_uidinfo);
1965 newcred->cr_uidinfo = euip;
1969 * Change a process's effective gid.
1970 * Side effects: newcred->cr_gid will be modified.
1971 * References: newcred must be an exclusive credential reference for the
1972 * duration of the call.
1975 change_egid(struct ucred *newcred, gid_t egid)
1978 newcred->cr_groups[0] = egid;
1982 * Change a process's real uid.
1983 * Side effects: newcred->cr_ruid will be updated, newcred->cr_ruidinfo
1984 * will be updated, and the old and new cr_ruidinfo proc
1985 * counts will be updated.
1986 * References: newcred must be an exclusive credential reference for the
1987 * duration of the call.
1990 change_ruid(struct ucred *newcred, struct uidinfo *ruip)
1993 (void)chgproccnt(newcred->cr_ruidinfo, -1, 0);
1994 newcred->cr_ruid = ruip->ui_uid;
1996 uifree(newcred->cr_ruidinfo);
1997 newcred->cr_ruidinfo = ruip;
1998 (void)chgproccnt(newcred->cr_ruidinfo, 1, 0);
2002 * Change a process's real gid.
2003 * Side effects: newcred->cr_rgid will be updated.
2004 * References: newcred must be an exclusive credential reference for the
2005 * duration of the call.
2008 change_rgid(struct ucred *newcred, gid_t rgid)
2011 newcred->cr_rgid = rgid;
2015 * Change a process's saved uid.
2016 * Side effects: newcred->cr_svuid will be updated.
2017 * References: newcred must be an exclusive credential reference for the
2018 * duration of the call.
2021 change_svuid(struct ucred *newcred, uid_t svuid)
2024 newcred->cr_svuid = svuid;
2028 * Change a process's saved gid.
2029 * Side effects: newcred->cr_svgid will be updated.
2030 * References: newcred must be an exclusive credential reference for the
2031 * duration of the call.
2034 change_svgid(struct ucred *newcred, gid_t svgid)
2037 newcred->cr_svgid = svgid;