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"
49 #include "opt_inet6.h"
52 #include <sys/param.h>
53 #include <sys/systm.h>
56 #include <sys/kernel.h>
58 #include <sys/malloc.h>
59 #include <sys/mutex.h>
60 #include <sys/refcount.h>
64 #include <sys/sysproto.h>
66 #include <sys/pioctl.h>
67 #include <sys/resourcevar.h>
68 #include <sys/socket.h>
69 #include <sys/socketvar.h>
70 #include <sys/syscallsubr.h>
71 #include <sys/sysctl.h>
73 #if defined(INET) || defined(INET6)
74 #include <netinet/in.h>
75 #include <netinet/in_pcb.h>
78 #include <security/audit/audit.h>
79 #include <security/mac/mac_framework.h>
81 static MALLOC_DEFINE(M_CRED, "cred", "credentials");
83 SYSCTL_NODE(_security, OID_AUTO, bsd, CTLFLAG_RW, 0, "BSD security policy");
85 #ifndef _SYS_SYSPROTO_H_
92 getpid(struct thread *td, struct getpid_args *uap)
94 struct proc *p = td->td_proc;
96 td->td_retval[0] = p->p_pid;
97 #if defined(COMPAT_43)
99 td->td_retval[1] = p->p_pptr->p_pid;
105 #ifndef _SYS_SYSPROTO_H_
106 struct getppid_args {
112 getppid(struct thread *td, struct getppid_args *uap)
114 struct proc *p = td->td_proc;
117 td->td_retval[0] = p->p_pptr->p_pid;
123 * Get process group ID; note that POSIX getpgrp takes no parameter.
125 #ifndef _SYS_SYSPROTO_H_
126 struct getpgrp_args {
131 getpgrp(struct thread *td, struct getpgrp_args *uap)
133 struct proc *p = td->td_proc;
136 td->td_retval[0] = p->p_pgrp->pg_id;
141 /* Get an arbitary pid's process group id */
142 #ifndef _SYS_SYSPROTO_H_
143 struct getpgid_args {
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_
180 getsid(struct thread *td, struct getsid_args *uap)
192 error = p_cansee(td, p);
198 td->td_retval[0] = p->p_session->s_sid;
203 #ifndef _SYS_SYSPROTO_H_
210 getuid(struct thread *td, struct getuid_args *uap)
213 td->td_retval[0] = td->td_ucred->cr_ruid;
214 #if defined(COMPAT_43)
215 td->td_retval[1] = td->td_ucred->cr_uid;
220 #ifndef _SYS_SYSPROTO_H_
221 struct geteuid_args {
227 geteuid(struct thread *td, struct geteuid_args *uap)
230 td->td_retval[0] = td->td_ucred->cr_uid;
234 #ifndef _SYS_SYSPROTO_H_
241 getgid(struct thread *td, struct getgid_args *uap)
244 td->td_retval[0] = td->td_ucred->cr_rgid;
245 #if defined(COMPAT_43)
246 td->td_retval[1] = td->td_ucred->cr_groups[0];
252 * Get effective group ID. The "egid" is groups[0], and could be obtained
253 * via getgroups. This syscall exists because it is somewhat painful to do
254 * correctly in a library function.
256 #ifndef _SYS_SYSPROTO_H_
257 struct getegid_args {
263 getegid(struct thread *td, struct getegid_args *uap)
266 td->td_retval[0] = td->td_ucred->cr_groups[0];
270 #ifndef _SYS_SYSPROTO_H_
271 struct getgroups_args {
277 getgroups(struct thread *td, register struct getgroups_args *uap)
279 gid_t groups[NGROUPS];
283 ngrp = MIN(uap->gidsetsize, NGROUPS);
284 error = kern_getgroups(td, &ngrp, groups);
287 if (uap->gidsetsize > 0)
288 error = copyout(groups, uap->gidset, ngrp * sizeof(gid_t));
290 td->td_retval[0] = ngrp;
295 kern_getgroups(struct thread *td, u_int *ngrp, gid_t *groups)
301 *ngrp = cred->cr_ngroups;
304 if (*ngrp < cred->cr_ngroups)
306 *ngrp = cred->cr_ngroups;
307 bcopy(cred->cr_groups, groups, *ngrp * sizeof(gid_t));
311 #ifndef _SYS_SYSPROTO_H_
318 setsid(register struct thread *td, struct setsid_args *uap)
322 struct proc *p = td->td_proc;
323 struct pgrp *newpgrp;
324 struct session *newsess;
329 MALLOC(newpgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
330 MALLOC(newsess, struct session *, sizeof(struct session), M_SESSION, M_WAITOK | M_ZERO);
332 sx_xlock(&proctree_lock);
334 if (p->p_pgid == p->p_pid || (pgrp = pgfind(p->p_pid)) != NULL) {
339 (void)enterpgrp(p, p->p_pid, newpgrp, newsess);
340 td->td_retval[0] = p->p_pid;
345 sx_xunlock(&proctree_lock);
348 FREE(newpgrp, M_PGRP);
350 FREE(newsess, M_SESSION);
356 * set process group (setpgid/old setpgrp)
358 * caller does setpgid(targpid, targpgid)
360 * pid must be caller or child of caller (ESRCH)
362 * pid must be in same session (EPERM)
363 * pid can't have done an exec (EACCES)
365 * there must exist some pid in same session having pgid (EPERM)
366 * pid must not be session leader (EPERM)
368 #ifndef _SYS_SYSPROTO_H_
369 struct setpgid_args {
370 int pid; /* target process id */
371 int pgid; /* target pgrp id */
376 setpgid(struct thread *td, register struct setpgid_args *uap)
378 struct proc *curp = td->td_proc;
379 register struct proc *targp; /* target process */
380 register struct pgrp *pgrp; /* target pgrp */
382 struct pgrp *newpgrp;
389 MALLOC(newpgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
391 sx_xlock(&proctree_lock);
392 if (uap->pid != 0 && uap->pid != curp->p_pid) {
393 if ((targp = pfind(uap->pid)) == NULL) {
397 if (!inferior(targp)) {
402 if ((error = p_cansee(td, targp))) {
406 if (targp->p_pgrp == NULL ||
407 targp->p_session != curp->p_session) {
412 if (targp->p_flag & P_EXEC) {
420 if (SESS_LEADER(targp)) {
425 uap->pgid = targp->p_pid;
426 if ((pgrp = pgfind(uap->pgid)) == NULL) {
427 if (uap->pgid == targp->p_pid) {
428 error = enterpgrp(targp, uap->pgid, newpgrp,
435 if (pgrp == targp->p_pgrp) {
439 if (pgrp->pg_id != targp->p_pid &&
440 pgrp->pg_session != curp->p_session) {
446 error = enterthispgrp(targp, pgrp);
449 sx_xunlock(&proctree_lock);
450 KASSERT((error == 0) || (newpgrp != NULL),
451 ("setpgid failed and newpgrp is NULL"));
453 FREE(newpgrp, M_PGRP);
458 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
459 * compatible. It says that setting the uid/gid to euid/egid is a special
460 * case of "appropriate privilege". Once the rules are expanded out, this
461 * basically means that setuid(nnn) sets all three id's, in all permitted
462 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
463 * does not set the saved id - this is dangerous for traditional BSD
464 * programs. For this reason, we *really* do not want to set
465 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
467 #define POSIX_APPENDIX_B_4_2_2
469 #ifndef _SYS_SYSPROTO_H_
476 setuid(struct thread *td, struct setuid_args *uap)
478 struct proc *p = td->td_proc;
479 struct ucred *newcred, *oldcred;
489 oldcred = p->p_ucred;
492 error = mac_check_proc_setuid(p, oldcred, uid);
498 * See if we have "permission" by POSIX 1003.1 rules.
500 * Note that setuid(geteuid()) is a special case of
501 * "appropriate privileges" in appendix B.4.2.2. We need
502 * to use this clause to be compatible with traditional BSD
503 * semantics. Basically, it means that "setuid(xx)" sets all
504 * three id's (assuming you have privs).
506 * Notes on the logic. We do things in three steps.
507 * 1: We determine if the euid is going to change, and do EPERM
508 * right away. We unconditionally change the euid later if this
509 * test is satisfied, simplifying that part of the logic.
510 * 2: We determine if the real and/or saved uids are going to
511 * change. Determined by compile options.
512 * 3: Change euid last. (after tests in #2 for "appropriate privs")
514 if (uid != oldcred->cr_ruid && /* allow setuid(getuid()) */
515 #ifdef _POSIX_SAVED_IDS
516 uid != oldcred->cr_svuid && /* allow setuid(saved gid) */
518 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
519 uid != oldcred->cr_uid && /* allow setuid(geteuid()) */
521 (error = priv_check_cred(oldcred, PRIV_CRED_SETUID, 0)) != 0)
525 * Copy credentials so other references do not see our changes.
527 crcopy(newcred, oldcred);
528 #ifdef _POSIX_SAVED_IDS
530 * Do we have "appropriate privileges" (are we root or uid == euid)
531 * If so, we are changing the real uid and/or saved uid.
534 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
535 uid == oldcred->cr_uid ||
537 /* We are using privs. */
538 priv_check_cred(oldcred, PRIV_CRED_SETUID, 0) == 0)
542 * Set the real uid and transfer proc count to new user.
544 if (uid != oldcred->cr_ruid) {
545 change_ruid(newcred, uip);
551 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
552 * the security of seteuid() depends on it. B.4.2.2 says it
553 * is important that we should do this.
555 if (uid != oldcred->cr_svuid) {
556 change_svuid(newcred, uid);
562 * In all permitted cases, we are changing the euid.
564 if (uid != oldcred->cr_uid) {
565 change_euid(newcred, uip);
568 p->p_ucred = newcred;
581 #ifndef _SYS_SYSPROTO_H_
582 struct seteuid_args {
588 seteuid(struct thread *td, struct seteuid_args *uap)
590 struct proc *p = td->td_proc;
591 struct ucred *newcred, *oldcred;
593 struct uidinfo *euip;
597 AUDIT_ARG(euid, euid);
601 oldcred = p->p_ucred;
604 error = mac_check_proc_seteuid(p, oldcred, euid);
609 if (euid != oldcred->cr_ruid && /* allow seteuid(getuid()) */
610 euid != oldcred->cr_svuid && /* allow seteuid(saved uid) */
611 (error = priv_check_cred(oldcred, PRIV_CRED_SETEUID, 0)) != 0)
615 * Everything's okay, do it. Copy credentials so other references do
616 * not see our changes.
618 crcopy(newcred, oldcred);
619 if (oldcred->cr_uid != euid) {
620 change_euid(newcred, euip);
623 p->p_ucred = newcred;
636 #ifndef _SYS_SYSPROTO_H_
643 setgid(struct thread *td, struct setgid_args *uap)
645 struct proc *p = td->td_proc;
646 struct ucred *newcred, *oldcred;
654 oldcred = p->p_ucred;
657 error = mac_check_proc_setgid(p, oldcred, gid);
663 * See if we have "permission" by POSIX 1003.1 rules.
665 * Note that setgid(getegid()) is a special case of
666 * "appropriate privileges" in appendix B.4.2.2. We need
667 * to use this clause to be compatible with traditional BSD
668 * semantics. Basically, it means that "setgid(xx)" sets all
669 * three id's (assuming you have privs).
671 * For notes on the logic here, see setuid() above.
673 if (gid != oldcred->cr_rgid && /* allow setgid(getgid()) */
674 #ifdef _POSIX_SAVED_IDS
675 gid != oldcred->cr_svgid && /* allow setgid(saved gid) */
677 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
678 gid != oldcred->cr_groups[0] && /* allow setgid(getegid()) */
680 (error = priv_check_cred(oldcred, PRIV_CRED_SETGID, 0)) != 0)
683 crcopy(newcred, oldcred);
684 #ifdef _POSIX_SAVED_IDS
686 * Do we have "appropriate privileges" (are we root or gid == egid)
687 * If so, we are changing the real uid and saved gid.
690 #ifdef POSIX_APPENDIX_B_4_2_2 /* use the clause from B.4.2.2 */
691 gid == oldcred->cr_groups[0] ||
693 /* We are using privs. */
694 priv_check_cred(oldcred, PRIV_CRED_SETGID, 0) == 0)
700 if (oldcred->cr_rgid != gid) {
701 change_rgid(newcred, gid);
707 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
708 * the security of setegid() depends on it. B.4.2.2 says it
709 * is important that we should do this.
711 if (oldcred->cr_svgid != gid) {
712 change_svgid(newcred, gid);
717 * In all cases permitted cases, we are changing the egid.
718 * Copy credentials so other references do not see our changes.
720 if (oldcred->cr_groups[0] != gid) {
721 change_egid(newcred, gid);
724 p->p_ucred = newcred;
735 #ifndef _SYS_SYSPROTO_H_
736 struct setegid_args {
742 setegid(struct thread *td, struct setegid_args *uap)
744 struct proc *p = td->td_proc;
745 struct ucred *newcred, *oldcred;
750 AUDIT_ARG(egid, egid);
753 oldcred = p->p_ucred;
756 error = mac_check_proc_setegid(p, oldcred, egid);
761 if (egid != oldcred->cr_rgid && /* allow setegid(getgid()) */
762 egid != oldcred->cr_svgid && /* allow setegid(saved gid) */
763 (error = priv_check_cred(oldcred, PRIV_CRED_SETEGID, 0)) != 0)
766 crcopy(newcred, oldcred);
767 if (oldcred->cr_groups[0] != egid) {
768 change_egid(newcred, egid);
771 p->p_ucred = newcred;
782 #ifndef _SYS_SYSPROTO_H_
783 struct setgroups_args {
790 setgroups(struct thread *td, struct setgroups_args *uap)
792 gid_t groups[NGROUPS];
795 if (uap->gidsetsize > NGROUPS)
797 error = copyin(uap->gidset, groups, uap->gidsetsize * sizeof(gid_t));
800 return (kern_setgroups(td, uap->gidsetsize, groups));
804 kern_setgroups(struct thread *td, u_int ngrp, gid_t *groups)
806 struct proc *p = td->td_proc;
807 struct ucred *newcred, *oldcred;
812 AUDIT_ARG(groupset, groups, ngrp);
815 oldcred = p->p_ucred;
818 error = mac_check_proc_setgroups(p, oldcred, ngrp, groups);
823 error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0);
828 * XXX A little bit lazy here. We could test if anything has
829 * changed before crcopy() and setting P_SUGID.
831 crcopy(newcred, oldcred);
834 * setgroups(0, NULL) is a legitimate way of clearing the
835 * groups vector on non-BSD systems (which generally do not
836 * have the egid in the groups[0]). We risk security holes
837 * when running non-BSD software if we do not do the same.
839 newcred->cr_ngroups = 1;
841 bcopy(groups, newcred->cr_groups, ngrp * sizeof(gid_t));
842 newcred->cr_ngroups = ngrp;
845 p->p_ucred = newcred;
856 #ifndef _SYS_SYSPROTO_H_
857 struct setreuid_args {
864 setreuid(register struct thread *td, struct setreuid_args *uap)
866 struct proc *p = td->td_proc;
867 struct ucred *newcred, *oldcred;
869 struct uidinfo *euip, *ruip;
874 AUDIT_ARG(euid, euid);
875 AUDIT_ARG(ruid, ruid);
880 oldcred = p->p_ucred;
883 error = mac_check_proc_setreuid(p, oldcred, ruid, euid);
888 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
889 ruid != oldcred->cr_svuid) ||
890 (euid != (uid_t)-1 && euid != oldcred->cr_uid &&
891 euid != oldcred->cr_ruid && euid != oldcred->cr_svuid)) &&
892 (error = priv_check_cred(oldcred, PRIV_CRED_SETREUID, 0)) != 0)
895 crcopy(newcred, oldcred);
896 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
897 change_euid(newcred, euip);
900 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
901 change_ruid(newcred, ruip);
904 if ((ruid != (uid_t)-1 || newcred->cr_uid != newcred->cr_ruid) &&
905 newcred->cr_svuid != newcred->cr_uid) {
906 change_svuid(newcred, newcred->cr_uid);
909 p->p_ucred = newcred;
924 #ifndef _SYS_SYSPROTO_H_
925 struct setregid_args {
932 setregid(register struct thread *td, struct setregid_args *uap)
934 struct proc *p = td->td_proc;
935 struct ucred *newcred, *oldcred;
941 AUDIT_ARG(egid, egid);
942 AUDIT_ARG(rgid, rgid);
945 oldcred = p->p_ucred;
948 error = mac_check_proc_setregid(p, oldcred, rgid, egid);
953 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
954 rgid != oldcred->cr_svgid) ||
955 (egid != (gid_t)-1 && egid != oldcred->cr_groups[0] &&
956 egid != oldcred->cr_rgid && egid != oldcred->cr_svgid)) &&
957 (error = priv_check_cred(oldcred, PRIV_CRED_SETREGID, 0)) != 0)
960 crcopy(newcred, oldcred);
961 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
962 change_egid(newcred, egid);
965 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
966 change_rgid(newcred, rgid);
969 if ((rgid != (gid_t)-1 || newcred->cr_groups[0] != newcred->cr_rgid) &&
970 newcred->cr_svgid != newcred->cr_groups[0]) {
971 change_svgid(newcred, newcred->cr_groups[0]);
974 p->p_ucred = newcred;
986 * setresuid(ruid, euid, suid) is like setreuid except control over the saved
989 #ifndef _SYS_SYSPROTO_H_
990 struct setresuid_args {
998 setresuid(register struct thread *td, struct setresuid_args *uap)
1000 struct proc *p = td->td_proc;
1001 struct ucred *newcred, *oldcred;
1002 uid_t euid, ruid, suid;
1003 struct uidinfo *euip, *ruip;
1009 AUDIT_ARG(euid, euid);
1010 AUDIT_ARG(ruid, ruid);
1011 AUDIT_ARG(suid, suid);
1013 euip = uifind(euid);
1014 ruip = uifind(ruid);
1016 oldcred = p->p_ucred;
1019 error = mac_check_proc_setresuid(p, oldcred, ruid, euid, suid);
1024 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
1025 ruid != oldcred->cr_svuid &&
1026 ruid != oldcred->cr_uid) ||
1027 (euid != (uid_t)-1 && euid != oldcred->cr_ruid &&
1028 euid != oldcred->cr_svuid &&
1029 euid != oldcred->cr_uid) ||
1030 (suid != (uid_t)-1 && suid != oldcred->cr_ruid &&
1031 suid != oldcred->cr_svuid &&
1032 suid != oldcred->cr_uid)) &&
1033 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESUID, 0)) != 0)
1036 crcopy(newcred, oldcred);
1037 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
1038 change_euid(newcred, euip);
1041 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
1042 change_ruid(newcred, ruip);
1045 if (suid != (uid_t)-1 && oldcred->cr_svuid != suid) {
1046 change_svuid(newcred, suid);
1049 p->p_ucred = newcred;
1066 * setresgid(rgid, egid, sgid) is like setregid except control over the saved
1069 #ifndef _SYS_SYSPROTO_H_
1070 struct setresgid_args {
1078 setresgid(register struct thread *td, struct setresgid_args *uap)
1080 struct proc *p = td->td_proc;
1081 struct ucred *newcred, *oldcred;
1082 gid_t egid, rgid, sgid;
1088 AUDIT_ARG(egid, egid);
1089 AUDIT_ARG(rgid, rgid);
1090 AUDIT_ARG(sgid, sgid);
1093 oldcred = p->p_ucred;
1096 error = mac_check_proc_setresgid(p, oldcred, rgid, egid, sgid);
1101 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
1102 rgid != oldcred->cr_svgid &&
1103 rgid != oldcred->cr_groups[0]) ||
1104 (egid != (gid_t)-1 && egid != oldcred->cr_rgid &&
1105 egid != oldcred->cr_svgid &&
1106 egid != oldcred->cr_groups[0]) ||
1107 (sgid != (gid_t)-1 && sgid != oldcred->cr_rgid &&
1108 sgid != oldcred->cr_svgid &&
1109 sgid != oldcred->cr_groups[0])) &&
1110 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESGID, 0)) != 0)
1113 crcopy(newcred, oldcred);
1114 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
1115 change_egid(newcred, egid);
1118 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
1119 change_rgid(newcred, rgid);
1122 if (sgid != (gid_t)-1 && oldcred->cr_svgid != sgid) {
1123 change_svgid(newcred, sgid);
1126 p->p_ucred = newcred;
1137 #ifndef _SYS_SYSPROTO_H_
1138 struct getresuid_args {
1146 getresuid(register struct thread *td, struct getresuid_args *uap)
1149 int error1 = 0, error2 = 0, error3 = 0;
1151 cred = td->td_ucred;
1153 error1 = copyout(&cred->cr_ruid,
1154 uap->ruid, sizeof(cred->cr_ruid));
1156 error2 = copyout(&cred->cr_uid,
1157 uap->euid, sizeof(cred->cr_uid));
1159 error3 = copyout(&cred->cr_svuid,
1160 uap->suid, sizeof(cred->cr_svuid));
1161 return (error1 ? error1 : error2 ? error2 : error3);
1164 #ifndef _SYS_SYSPROTO_H_
1165 struct getresgid_args {
1173 getresgid(register struct thread *td, struct getresgid_args *uap)
1176 int error1 = 0, error2 = 0, error3 = 0;
1178 cred = td->td_ucred;
1180 error1 = copyout(&cred->cr_rgid,
1181 uap->rgid, sizeof(cred->cr_rgid));
1183 error2 = copyout(&cred->cr_groups[0],
1184 uap->egid, sizeof(cred->cr_groups[0]));
1186 error3 = copyout(&cred->cr_svgid,
1187 uap->sgid, sizeof(cred->cr_svgid));
1188 return (error1 ? error1 : error2 ? error2 : error3);
1191 #ifndef _SYS_SYSPROTO_H_
1192 struct issetugid_args {
1198 issetugid(register struct thread *td, struct issetugid_args *uap)
1200 struct proc *p = td->td_proc;
1203 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
1204 * we use P_SUGID because we consider changing the owners as
1205 * "tainting" as well.
1206 * This is significant for procs that start as root and "become"
1207 * a user without an exec - programs cannot know *everything*
1208 * that libc *might* have put in their data segment.
1211 td->td_retval[0] = (p->p_flag & P_SUGID) ? 1 : 0;
1217 __setugid(struct thread *td, struct __setugid_args *uap)
1223 switch (uap->flag) {
1226 p->p_flag &= ~P_SUGID;
1231 p->p_flag |= P_SUGID;
1237 #else /* !REGRESSION */
1240 #endif /* REGRESSION */
1244 * Check if gid is a member of the group set.
1247 groupmember(gid_t gid, struct ucred *cred)
1252 egp = &(cred->cr_groups[cred->cr_ngroups]);
1253 for (gp = cred->cr_groups; gp < egp; gp++)
1260 * Test the active securelevel against a given level. securelevel_gt()
1261 * implements (securelevel > level). securelevel_ge() implements
1262 * (securelevel >= level). Note that the logic is inverted -- these
1263 * functions return EPERM on "success" and 0 on "failure".
1265 * XXXRW: Possibly since this has to do with privilege, it should move to
1269 securelevel_gt(struct ucred *cr, int level)
1271 int active_securelevel;
1273 active_securelevel = securelevel;
1274 KASSERT(cr != NULL, ("securelevel_gt: null cr"));
1275 if (cr->cr_prison != NULL)
1276 active_securelevel = imax(cr->cr_prison->pr_securelevel,
1277 active_securelevel);
1278 return (active_securelevel > level ? EPERM : 0);
1282 securelevel_ge(struct ucred *cr, int level)
1284 int active_securelevel;
1286 active_securelevel = securelevel;
1287 KASSERT(cr != NULL, ("securelevel_ge: null cr"));
1288 if (cr->cr_prison != NULL)
1289 active_securelevel = imax(cr->cr_prison->pr_securelevel,
1290 active_securelevel);
1291 return (active_securelevel >= level ? EPERM : 0);
1295 * 'see_other_uids' determines whether or not visibility of processes
1296 * and sockets with credentials holding different real uids is possible
1297 * using a variety of system MIBs.
1298 * XXX: data declarations should be together near the beginning of the file.
1300 static int see_other_uids = 1;
1301 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_uids, CTLFLAG_RW,
1303 "Unprivileged processes may see subjects/objects with different real uid");
1306 * Determine if u1 "can see" the subject specified by u2, according to the
1307 * 'see_other_uids' policy.
1308 * Returns: 0 for permitted, ESRCH otherwise
1310 * References: *u1 and *u2 must not change during the call
1311 * u1 may equal u2, in which case only one reference is required
1314 cr_seeotheruids(struct ucred *u1, struct ucred *u2)
1317 if (!see_other_uids && u1->cr_ruid != u2->cr_ruid) {
1318 if (priv_check_cred(u1, PRIV_SEEOTHERUIDS, 0) != 0)
1325 * 'see_other_gids' determines whether or not visibility of processes
1326 * and sockets with credentials holding different real gids is possible
1327 * using a variety of system MIBs.
1328 * XXX: data declarations should be together near the beginning of the file.
1330 static int see_other_gids = 1;
1331 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_gids, CTLFLAG_RW,
1333 "Unprivileged processes may see subjects/objects with different real gid");
1336 * Determine if u1 can "see" the subject specified by u2, according to the
1337 * 'see_other_gids' policy.
1338 * Returns: 0 for permitted, ESRCH otherwise
1340 * References: *u1 and *u2 must not change during the call
1341 * u1 may equal u2, in which case only one reference is required
1344 cr_seeothergids(struct ucred *u1, struct ucred *u2)
1348 if (!see_other_gids) {
1350 for (i = 0; i < u1->cr_ngroups; i++) {
1351 if (groupmember(u1->cr_groups[i], u2))
1357 if (priv_check_cred(u1, PRIV_SEEOTHERGIDS, 0) != 0)
1365 * Determine if u1 "can see" the subject specified by u2.
1366 * Returns: 0 for permitted, an errno value otherwise
1368 * References: *u1 and *u2 must not change during the call
1369 * u1 may equal u2, in which case only one reference is required
1372 cr_cansee(struct ucred *u1, struct ucred *u2)
1376 if ((error = prison_check(u1, u2)))
1379 if ((error = mac_check_cred_visible(u1, u2)))
1382 if ((error = cr_seeotheruids(u1, u2)))
1384 if ((error = cr_seeothergids(u1, u2)))
1390 * Determine if td "can see" the subject specified by p.
1391 * Returns: 0 for permitted, an errno value otherwise
1392 * Locks: Sufficient locks to protect p->p_ucred must be held. td really
1393 * should be curthread.
1394 * References: td and p must be valid for the lifetime of the call
1397 p_cansee(struct thread *td, struct proc *p)
1400 /* Wrap cr_cansee() for all functionality. */
1401 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1402 PROC_LOCK_ASSERT(p, MA_OWNED);
1403 return (cr_cansee(td->td_ucred, p->p_ucred));
1407 * 'conservative_signals' prevents the delivery of a broad class of
1408 * signals by unprivileged processes to processes that have changed their
1409 * credentials since the last invocation of execve(). This can prevent
1410 * the leakage of cached information or retained privileges as a result
1411 * of a common class of signal-related vulnerabilities. However, this
1412 * may interfere with some applications that expect to be able to
1413 * deliver these signals to peer processes after having given up
1416 static int conservative_signals = 1;
1417 SYSCTL_INT(_security_bsd, OID_AUTO, conservative_signals, CTLFLAG_RW,
1418 &conservative_signals, 0, "Unprivileged processes prevented from "
1419 "sending certain signals to processes whose credentials have changed");
1421 * Determine whether cred may deliver the specified signal to proc.
1422 * Returns: 0 for permitted, an errno value otherwise.
1423 * Locks: A lock must be held for proc.
1424 * References: cred and proc must be valid for the lifetime of the call.
1427 cr_cansignal(struct ucred *cred, struct proc *proc, int signum)
1431 PROC_LOCK_ASSERT(proc, MA_OWNED);
1433 * Jail semantics limit the scope of signalling to proc in the
1434 * same jail as cred, if cred is in jail.
1436 error = prison_check(cred, proc->p_ucred);
1440 if ((error = mac_check_proc_signal(cred, proc, signum)))
1443 if ((error = cr_seeotheruids(cred, proc->p_ucred)))
1445 if ((error = cr_seeothergids(cred, proc->p_ucred)))
1449 * UNIX signal semantics depend on the status of the P_SUGID
1450 * bit on the target process. If the bit is set, then additional
1451 * restrictions are placed on the set of available signals.
1453 if (conservative_signals && (proc->p_flag & P_SUGID)) {
1468 * Generally, permit job and terminal control
1473 /* Not permitted without privilege. */
1474 error = priv_check_cred(cred, PRIV_SIGNAL_SUGID, 0);
1481 * Generally, the target credential's ruid or svuid must match the
1482 * subject credential's ruid or euid.
1484 if (cred->cr_ruid != proc->p_ucred->cr_ruid &&
1485 cred->cr_ruid != proc->p_ucred->cr_svuid &&
1486 cred->cr_uid != proc->p_ucred->cr_ruid &&
1487 cred->cr_uid != proc->p_ucred->cr_svuid) {
1488 error = priv_check_cred(cred, PRIV_SIGNAL_DIFFCRED, 0);
1497 * Determine whether td may deliver the specified signal to p.
1498 * Returns: 0 for permitted, an errno value otherwise
1499 * Locks: Sufficient locks to protect various components of td and p
1500 * must be held. td must be curthread, and a lock must be
1502 * References: td and p must be valid for the lifetime of the call
1505 p_cansignal(struct thread *td, struct proc *p, int signum)
1508 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1509 PROC_LOCK_ASSERT(p, MA_OWNED);
1510 if (td->td_proc == p)
1514 * UNIX signalling semantics require that processes in the same
1515 * session always be able to deliver SIGCONT to one another,
1516 * overriding the remaining protections.
1518 /* XXX: This will require an additional lock of some sort. */
1519 if (signum == SIGCONT && td->td_proc->p_session == p->p_session)
1522 * Some compat layers use SIGTHR and higher signals for
1523 * communication between different kernel threads of the same
1524 * process, so that they expect that it's always possible to
1525 * deliver them, even for suid applications where cr_cansignal() can
1526 * deny such ability for security consideration. It should be
1527 * pretty safe to do since the only way to create two processes
1528 * with the same p_leader is via rfork(2).
1530 if (td->td_proc->p_leader != NULL && signum >= SIGTHR &&
1531 signum < SIGTHR + 4 && td->td_proc->p_leader == p->p_leader)
1534 return (cr_cansignal(td->td_ucred, p, signum));
1538 * Determine whether td may reschedule p.
1539 * Returns: 0 for permitted, an errno value otherwise
1540 * Locks: Sufficient locks to protect various components of td and p
1541 * must be held. td must be curthread, and a lock must
1543 * References: td and p must be valid for the lifetime of the call
1546 p_cansched(struct thread *td, struct proc *p)
1550 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1551 PROC_LOCK_ASSERT(p, MA_OWNED);
1552 if (td->td_proc == p)
1554 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1557 if ((error = mac_check_proc_sched(td->td_ucred, p)))
1560 if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1562 if ((error = cr_seeothergids(td->td_ucred, p->p_ucred)))
1564 if (td->td_ucred->cr_ruid != p->p_ucred->cr_ruid &&
1565 td->td_ucred->cr_uid != p->p_ucred->cr_ruid) {
1566 error = priv_check(td, PRIV_SCHED_DIFFCRED);
1574 * The 'unprivileged_proc_debug' flag may be used to disable a variety of
1575 * unprivileged inter-process debugging services, including some procfs
1576 * functionality, ptrace(), and ktrace(). In the past, inter-process
1577 * debugging has been involved in a variety of security problems, and sites
1578 * not requiring the service might choose to disable it when hardening
1581 * XXX: Should modifying and reading this variable require locking?
1582 * XXX: data declarations should be together near the beginning of the file.
1584 static int unprivileged_proc_debug = 1;
1585 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_proc_debug, CTLFLAG_RW,
1586 &unprivileged_proc_debug, 0,
1587 "Unprivileged processes may use process debugging facilities");
1590 * Determine whether td may debug p.
1591 * Returns: 0 for permitted, an errno value otherwise
1592 * Locks: Sufficient locks to protect various components of td and p
1593 * must be held. td must be curthread, and a lock must
1595 * References: td and p must be valid for the lifetime of the call
1598 p_candebug(struct thread *td, struct proc *p)
1600 int credentialchanged, error, grpsubset, i, uidsubset;
1602 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1603 PROC_LOCK_ASSERT(p, MA_OWNED);
1604 if (!unprivileged_proc_debug) {
1605 error = priv_check(td, PRIV_DEBUG_UNPRIV);
1609 if (td->td_proc == p)
1611 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1614 if ((error = mac_check_proc_debug(td->td_ucred, p)))
1617 if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1619 if ((error = cr_seeothergids(td->td_ucred, p->p_ucred)))
1623 * Is p's group set a subset of td's effective group set? This
1624 * includes p's egid, group access list, rgid, and svgid.
1627 for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
1628 if (!groupmember(p->p_ucred->cr_groups[i], td->td_ucred)) {
1633 grpsubset = grpsubset &&
1634 groupmember(p->p_ucred->cr_rgid, td->td_ucred) &&
1635 groupmember(p->p_ucred->cr_svgid, td->td_ucred);
1638 * Are the uids present in p's credential equal to td's
1639 * effective uid? This includes p's euid, svuid, and ruid.
1641 uidsubset = (td->td_ucred->cr_uid == p->p_ucred->cr_uid &&
1642 td->td_ucred->cr_uid == p->p_ucred->cr_svuid &&
1643 td->td_ucred->cr_uid == p->p_ucred->cr_ruid);
1646 * Has the credential of the process changed since the last exec()?
1648 credentialchanged = (p->p_flag & P_SUGID);
1651 * If p's gids aren't a subset, or the uids aren't a subset,
1652 * or the credential has changed, require appropriate privilege
1653 * for td to debug p.
1655 if (!grpsubset || !uidsubset) {
1656 error = priv_check(td, PRIV_DEBUG_DIFFCRED);
1661 if (credentialchanged) {
1662 error = priv_check(td, PRIV_DEBUG_SUGID);
1667 /* Can't trace init when securelevel > 0. */
1668 if (p == initproc) {
1669 error = securelevel_gt(td->td_ucred, 0);
1675 * Can't trace a process that's currently exec'ing.
1677 * XXX: Note, this is not a security policy decision, it's a
1678 * basic correctness/functionality decision. Therefore, this check
1679 * should be moved to the caller's of p_candebug().
1681 if ((p->p_flag & P_INEXEC) != 0)
1688 * Determine whether the subject represented by cred can "see" a socket.
1689 * Returns: 0 for permitted, ENOENT otherwise.
1692 cr_canseesocket(struct ucred *cred, struct socket *so)
1696 error = prison_check(cred, so->so_cred);
1701 error = mac_check_socket_visible(cred, so);
1706 if (cr_seeotheruids(cred, so->so_cred))
1708 if (cr_seeothergids(cred, so->so_cred))
1714 #if defined(INET) || defined(INET6)
1716 * Determine whether the subject represented by cred can "see" a socket.
1717 * Returns: 0 for permitted, ENOENT otherwise.
1720 cr_canseeinpcb(struct ucred *cred, struct inpcb *inp)
1724 error = prison_check(cred, inp->inp_cred);
1728 INP_LOCK_ASSERT(inp);
1729 error = mac_check_inpcb_visible(cred, inp);
1733 if (cr_seeotheruids(cred, inp->inp_cred))
1735 if (cr_seeothergids(cred, inp->inp_cred))
1743 * Determine whether td can wait for the exit of p.
1744 * Returns: 0 for permitted, an errno value otherwise
1745 * Locks: Sufficient locks to protect various components of td and p
1746 * must be held. td must be curthread, and a lock must
1748 * References: td and p must be valid for the lifetime of the call
1752 p_canwait(struct thread *td, struct proc *p)
1756 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1757 PROC_LOCK_ASSERT(p, MA_OWNED);
1758 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1761 if ((error = mac_check_proc_wait(td->td_ucred, p)))
1765 /* XXXMAC: This could have odd effects on some shells. */
1766 if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1774 * Allocate a zeroed cred structure.
1779 register struct ucred *cr;
1781 MALLOC(cr, struct ucred *, sizeof(*cr), M_CRED, M_WAITOK | M_ZERO);
1782 refcount_init(&cr->cr_ref, 1);
1784 audit_cred_init(cr);
1793 * Claim another reference to a ucred structure.
1796 crhold(struct ucred *cr)
1799 refcount_acquire(&cr->cr_ref);
1804 * Free a cred structure. Throws away space when ref count gets to 0.
1807 crfree(struct ucred *cr)
1810 KASSERT(cr->cr_ref > 0, ("bad ucred refcount: %d", cr->cr_ref));
1811 KASSERT(cr->cr_ref != 0xdeadc0de, ("dangling reference to ucred"));
1812 if (refcount_release(&cr->cr_ref)) {
1814 * Some callers of crget(), such as nfs_statfs(),
1815 * allocate a temporary credential, but don't
1816 * allocate a uidinfo structure.
1818 if (cr->cr_uidinfo != NULL)
1819 uifree(cr->cr_uidinfo);
1820 if (cr->cr_ruidinfo != NULL)
1821 uifree(cr->cr_ruidinfo);
1823 * Free a prison, if any.
1826 prison_free(cr->cr_prison);
1828 audit_cred_destroy(cr);
1831 mac_destroy_cred(cr);
1838 * Check to see if this ucred is shared.
1841 crshared(struct ucred *cr)
1844 return (cr->cr_ref > 1);
1848 * Copy a ucred's contents from a template. Does not block.
1851 crcopy(struct ucred *dest, struct ucred *src)
1854 KASSERT(crshared(dest) == 0, ("crcopy of shared ucred"));
1855 bcopy(&src->cr_startcopy, &dest->cr_startcopy,
1856 (unsigned)((caddr_t)&src->cr_endcopy -
1857 (caddr_t)&src->cr_startcopy));
1858 uihold(dest->cr_uidinfo);
1859 uihold(dest->cr_ruidinfo);
1861 prison_hold(dest->cr_prison);
1863 audit_cred_copy(src, dest);
1866 mac_copy_cred(src, dest);
1871 * Dup cred struct to a new held one.
1874 crdup(struct ucred *cr)
1876 struct ucred *newcr;
1884 * Fill in a struct xucred based on a struct ucred.
1887 cru2x(struct ucred *cr, struct xucred *xcr)
1890 bzero(xcr, sizeof(*xcr));
1891 xcr->cr_version = XUCRED_VERSION;
1892 xcr->cr_uid = cr->cr_uid;
1893 xcr->cr_ngroups = cr->cr_ngroups;
1894 bcopy(cr->cr_groups, xcr->cr_groups, sizeof(cr->cr_groups));
1898 * small routine to swap a thread's current ucred for the correct one taken
1902 cred_update_thread(struct thread *td)
1908 cred = td->td_ucred;
1910 td->td_ucred = crhold(p->p_ucred);
1917 * Get login name, if available.
1919 #ifndef _SYS_SYSPROTO_H_
1920 struct getlogin_args {
1927 getlogin(struct thread *td, struct getlogin_args *uap)
1930 char login[MAXLOGNAME];
1931 struct proc *p = td->td_proc;
1933 if (uap->namelen > MAXLOGNAME)
1934 uap->namelen = MAXLOGNAME;
1936 SESS_LOCK(p->p_session);
1937 bcopy(p->p_session->s_login, login, uap->namelen);
1938 SESS_UNLOCK(p->p_session);
1940 error = copyout(login, uap->namebuf, uap->namelen);
1947 #ifndef _SYS_SYSPROTO_H_
1948 struct setlogin_args {
1954 setlogin(struct thread *td, struct setlogin_args *uap)
1956 struct proc *p = td->td_proc;
1958 char logintmp[MAXLOGNAME];
1960 error = priv_check(td, PRIV_PROC_SETLOGIN);
1963 error = copyinstr(uap->namebuf, logintmp, sizeof(logintmp), NULL);
1964 if (error == ENAMETOOLONG)
1968 SESS_LOCK(p->p_session);
1969 (void) memcpy(p->p_session->s_login, logintmp,
1971 SESS_UNLOCK(p->p_session);
1978 setsugid(struct proc *p)
1981 PROC_LOCK_ASSERT(p, MA_OWNED);
1982 p->p_flag |= P_SUGID;
1983 if (!(p->p_pfsflags & PF_ISUGID))
1988 * Change a process's effective uid.
1989 * Side effects: newcred->cr_uid and newcred->cr_uidinfo will be modified.
1990 * References: newcred must be an exclusive credential reference for the
1991 * duration of the call.
1994 change_euid(struct ucred *newcred, struct uidinfo *euip)
1997 newcred->cr_uid = euip->ui_uid;
1999 uifree(newcred->cr_uidinfo);
2000 newcred->cr_uidinfo = euip;
2004 * Change a process's effective gid.
2005 * Side effects: newcred->cr_gid will be modified.
2006 * References: newcred must be an exclusive credential reference for the
2007 * duration of the call.
2010 change_egid(struct ucred *newcred, gid_t egid)
2013 newcred->cr_groups[0] = egid;
2017 * Change a process's real uid.
2018 * Side effects: newcred->cr_ruid will be updated, newcred->cr_ruidinfo
2019 * will be updated, and the old and new cr_ruidinfo proc
2020 * counts will be updated.
2021 * References: newcred must be an exclusive credential reference for the
2022 * duration of the call.
2025 change_ruid(struct ucred *newcred, struct uidinfo *ruip)
2028 (void)chgproccnt(newcred->cr_ruidinfo, -1, 0);
2029 newcred->cr_ruid = ruip->ui_uid;
2031 uifree(newcred->cr_ruidinfo);
2032 newcred->cr_ruidinfo = ruip;
2033 (void)chgproccnt(newcred->cr_ruidinfo, 1, 0);
2037 * Change a process's real gid.
2038 * Side effects: newcred->cr_rgid will be updated.
2039 * References: newcred must be an exclusive credential reference for the
2040 * duration of the call.
2043 change_rgid(struct ucred *newcred, gid_t rgid)
2046 newcred->cr_rgid = rgid;
2050 * Change a process's saved uid.
2051 * Side effects: newcred->cr_svuid will be updated.
2052 * References: newcred must be an exclusive credential reference for the
2053 * duration of the call.
2056 change_svuid(struct ucred *newcred, uid_t svuid)
2059 newcred->cr_svuid = svuid;
2063 * Change a process's saved gid.
2064 * Side effects: newcred->cr_svgid will be updated.
2065 * References: newcred must be an exclusive credential reference for the
2066 * duration of the call.
2069 change_svgid(struct ucred *newcred, gid_t svgid)
2072 newcred->cr_svgid = svgid;