2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1982, 1986, 1989, 1990, 1991, 1993
5 * The Regents of the University of California.
6 * (c) UNIX System Laboratories, Inc.
7 * Copyright (c) 2000-2001 Robert N. M. Watson.
10 * All or some portions of this file are derived from material licensed
11 * to the University of California by American Telephone and Telegraph
12 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
13 * the permission of UNIX System Laboratories, Inc.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * System calls related to processes and protection
44 #include <sys/cdefs.h>
46 #include "opt_inet6.h"
48 #include <sys/param.h>
49 #include <sys/systm.h>
52 #include <sys/kernel.h>
54 #include <sys/loginclass.h>
55 #include <sys/malloc.h>
56 #include <sys/mutex.h>
57 #include <sys/ptrace.h>
58 #include <sys/refcount.h>
63 #include <sys/sysent.h>
65 #include <sys/sysproto.h>
67 #include <sys/racct.h>
69 #include <sys/resourcevar.h>
70 #include <sys/socket.h>
71 #include <sys/socketvar.h>
72 #include <sys/syscallsubr.h>
73 #include <sys/sysctl.h>
77 "Kernel support for interfaces necessary for regression testing (SECURITY RISK!)");
80 #include <security/audit/audit.h>
81 #include <security/mac/mac_framework.h>
83 static MALLOC_DEFINE(M_CRED, "cred", "credentials");
85 SYSCTL_NODE(_security, OID_AUTO, bsd, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
86 "BSD security policy");
88 static void crfree_final(struct ucred *cr);
89 static void crsetgroups_locked(struct ucred *cr, int ngrp,
92 static int cr_canseeotheruids(struct ucred *u1, struct ucred *u2);
93 static int cr_canseeothergids(struct ucred *u1, struct ucred *u2);
94 static int cr_canseejailproc(struct ucred *u1, struct ucred *u2);
96 #ifndef _SYS_SYSPROTO_H_
103 sys_getpid(struct thread *td, struct getpid_args *uap)
105 struct proc *p = td->td_proc;
107 td->td_retval[0] = p->p_pid;
108 #if defined(COMPAT_43)
109 if (SV_PROC_FLAG(p, SV_AOUT))
110 td->td_retval[1] = kern_getppid(td);
115 #ifndef _SYS_SYSPROTO_H_
116 struct getppid_args {
122 sys_getppid(struct thread *td, struct getppid_args *uap)
125 td->td_retval[0] = kern_getppid(td);
130 kern_getppid(struct thread *td)
132 struct proc *p = td->td_proc;
138 * Get process group ID; note that POSIX getpgrp takes no parameter.
140 #ifndef _SYS_SYSPROTO_H_
141 struct getpgrp_args {
146 sys_getpgrp(struct thread *td, struct getpgrp_args *uap)
148 struct proc *p = td->td_proc;
151 td->td_retval[0] = p->p_pgrp->pg_id;
156 /* Get an arbitrary pid's process group id */
157 #ifndef _SYS_SYSPROTO_H_
158 struct getpgid_args {
163 sys_getpgid(struct thread *td, struct getpgid_args *uap)
175 error = p_cansee(td, p);
181 td->td_retval[0] = p->p_pgrp->pg_id;
187 * Get an arbitrary pid's session id.
189 #ifndef _SYS_SYSPROTO_H_
195 sys_getsid(struct thread *td, struct getsid_args *uap)
198 return (kern_getsid(td, uap->pid));
202 kern_getsid(struct thread *td, pid_t pid)
214 error = p_cansee(td, p);
220 td->td_retval[0] = p->p_session->s_sid;
225 #ifndef _SYS_SYSPROTO_H_
232 sys_getuid(struct thread *td, struct getuid_args *uap)
235 td->td_retval[0] = td->td_ucred->cr_ruid;
236 #if defined(COMPAT_43)
237 td->td_retval[1] = td->td_ucred->cr_uid;
242 #ifndef _SYS_SYSPROTO_H_
243 struct geteuid_args {
249 sys_geteuid(struct thread *td, struct geteuid_args *uap)
252 td->td_retval[0] = td->td_ucred->cr_uid;
256 #ifndef _SYS_SYSPROTO_H_
263 sys_getgid(struct thread *td, struct getgid_args *uap)
266 td->td_retval[0] = td->td_ucred->cr_rgid;
267 #if defined(COMPAT_43)
268 td->td_retval[1] = td->td_ucred->cr_groups[0];
274 * Get effective group ID. The "egid" is groups[0], and could be obtained
275 * via getgroups. This syscall exists because it is somewhat painful to do
276 * correctly in a library function.
278 #ifndef _SYS_SYSPROTO_H_
279 struct getegid_args {
285 sys_getegid(struct thread *td, struct getegid_args *uap)
288 td->td_retval[0] = td->td_ucred->cr_groups[0];
292 #ifndef _SYS_SYSPROTO_H_
293 struct getgroups_args {
299 sys_getgroups(struct thread *td, struct getgroups_args *uap)
305 ngrp = cred->cr_ngroups;
307 if (uap->gidsetsize == 0) {
311 if (uap->gidsetsize < ngrp)
314 error = copyout(cred->cr_groups, uap->gidset, ngrp * sizeof(gid_t));
316 td->td_retval[0] = ngrp;
320 #ifndef _SYS_SYSPROTO_H_
327 sys_setsid(struct thread *td, struct setsid_args *uap)
331 struct proc *p = td->td_proc;
332 struct pgrp *newpgrp;
333 struct session *newsess;
337 newpgrp = uma_zalloc(pgrp_zone, M_WAITOK);
338 newsess = malloc(sizeof(struct session), M_SESSION, M_WAITOK | M_ZERO);
342 sx_xlock(&proctree_lock);
344 if (p->p_pgid == p->p_pid || (pgrp = pgfind(p->p_pid)) != NULL) {
349 error = enterpgrp(p, p->p_pid, newpgrp, newsess);
350 if (error == ERESTART)
353 td->td_retval[0] = p->p_pid;
358 sx_xunlock(&proctree_lock);
360 uma_zfree(pgrp_zone, newpgrp);
361 free(newsess, M_SESSION);
367 * set process group (setpgid/old setpgrp)
369 * caller does setpgid(targpid, targpgid)
371 * pid must be caller or child of caller (ESRCH)
373 * pid must be in same session (EPERM)
374 * pid can't have done an exec (EACCES)
376 * there must exist some pid in same session having pgid (EPERM)
377 * pid must not be session leader (EPERM)
379 #ifndef _SYS_SYSPROTO_H_
380 struct setpgid_args {
381 int pid; /* target process id */
382 int pgid; /* target pgrp id */
387 sys_setpgid(struct thread *td, struct setpgid_args *uap)
389 struct proc *curp = td->td_proc;
390 struct proc *targp; /* target process */
391 struct pgrp *pgrp; /* target pgrp */
393 struct pgrp *newpgrp;
398 newpgrp = uma_zalloc(pgrp_zone, M_WAITOK);
403 sx_xlock(&proctree_lock);
404 if (uap->pid != 0 && uap->pid != curp->p_pid) {
405 if ((targp = pfind(uap->pid)) == NULL) {
409 if (!inferior(targp)) {
414 if ((error = p_cansee(td, targp))) {
418 if (targp->p_pgrp == NULL ||
419 targp->p_session != curp->p_session) {
424 if (targp->p_flag & P_EXEC) {
432 if (SESS_LEADER(targp)) {
437 uap->pgid = targp->p_pid;
438 if ((pgrp = pgfind(uap->pgid)) == NULL) {
439 if (uap->pgid == targp->p_pid) {
440 error = enterpgrp(targp, uap->pgid, newpgrp,
447 if (pgrp == targp->p_pgrp) {
451 if (pgrp->pg_id != targp->p_pid &&
452 pgrp->pg_session != curp->p_session) {
458 error = enterthispgrp(targp, pgrp);
461 KASSERT(error == 0 || newpgrp != NULL,
462 ("setpgid failed and newpgrp is NULL"));
463 if (error == ERESTART)
465 sx_xunlock(&proctree_lock);
466 uma_zfree(pgrp_zone, newpgrp);
471 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
472 * compatible. It says that setting the uid/gid to euid/egid is a special
473 * case of "appropriate privilege". Once the rules are expanded out, this
474 * basically means that setuid(nnn) sets all three id's, in all permitted
475 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
476 * does not set the saved id - this is dangerous for traditional BSD
477 * programs. For this reason, we *really* do not want to set
478 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
480 #define POSIX_APPENDIX_B_4_2_2
482 #ifndef _SYS_SYSPROTO_H_
489 sys_setuid(struct thread *td, struct setuid_args *uap)
491 struct proc *p = td->td_proc;
492 struct ucred *newcred, *oldcred;
503 * Copy credentials so other references do not see our changes.
505 oldcred = crcopysafe(p, newcred);
508 error = mac_cred_check_setuid(oldcred, uid);
514 * See if we have "permission" by POSIX 1003.1 rules.
516 * Note that setuid(geteuid()) is a special case of
517 * "appropriate privileges" in appendix B.4.2.2. We need
518 * to use this clause to be compatible with traditional BSD
519 * semantics. Basically, it means that "setuid(xx)" sets all
520 * three id's (assuming you have privs).
522 * Notes on the logic. We do things in three steps.
523 * 1: We determine if the euid is going to change, and do EPERM
524 * right away. We unconditionally change the euid later if this
525 * test is satisfied, simplifying that part of the logic.
526 * 2: We determine if the real and/or saved uids are going to
527 * change. Determined by compile options.
528 * 3: Change euid last. (after tests in #2 for "appropriate privs")
530 if (uid != oldcred->cr_ruid && /* allow setuid(getuid()) */
531 #ifdef _POSIX_SAVED_IDS
532 uid != oldcred->cr_svuid && /* allow setuid(saved gid) */
534 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
535 uid != oldcred->cr_uid && /* allow setuid(geteuid()) */
537 (error = priv_check_cred(oldcred, PRIV_CRED_SETUID)) != 0)
540 #ifdef _POSIX_SAVED_IDS
542 * Do we have "appropriate privileges" (are we root or uid == euid)
543 * If so, we are changing the real uid and/or saved uid.
546 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
547 uid == oldcred->cr_uid ||
549 /* We are using privs. */
550 priv_check_cred(oldcred, PRIV_CRED_SETUID) == 0)
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 proc_set_cred(p, newcred);
582 racct_proc_ucred_changed(p, oldcred, newcred);
587 rctl_proc_ucred_changed(p, newcred);
601 #ifndef _SYS_SYSPROTO_H_
602 struct seteuid_args {
608 sys_seteuid(struct thread *td, struct seteuid_args *uap)
610 struct proc *p = td->td_proc;
611 struct ucred *newcred, *oldcred;
613 struct uidinfo *euip;
617 AUDIT_ARG_EUID(euid);
622 * Copy credentials so other references do not see our changes.
624 oldcred = crcopysafe(p, newcred);
627 error = mac_cred_check_seteuid(oldcred, euid);
632 if (euid != oldcred->cr_ruid && /* allow seteuid(getuid()) */
633 euid != oldcred->cr_svuid && /* allow seteuid(saved uid) */
634 (error = priv_check_cred(oldcred, PRIV_CRED_SETEUID)) != 0)
638 * Everything's okay, do it.
640 if (oldcred->cr_uid != euid) {
641 change_euid(newcred, euip);
644 proc_set_cred(p, newcred);
657 #ifndef _SYS_SYSPROTO_H_
664 sys_setgid(struct thread *td, struct setgid_args *uap)
666 struct proc *p = td->td_proc;
667 struct ucred *newcred, *oldcred;
675 oldcred = crcopysafe(p, newcred);
678 error = mac_cred_check_setgid(oldcred, gid);
684 * See if we have "permission" by POSIX 1003.1 rules.
686 * Note that setgid(getegid()) is a special case of
687 * "appropriate privileges" in appendix B.4.2.2. We need
688 * to use this clause to be compatible with traditional BSD
689 * semantics. Basically, it means that "setgid(xx)" sets all
690 * three id's (assuming you have privs).
692 * For notes on the logic here, see setuid() above.
694 if (gid != oldcred->cr_rgid && /* allow setgid(getgid()) */
695 #ifdef _POSIX_SAVED_IDS
696 gid != oldcred->cr_svgid && /* allow setgid(saved gid) */
698 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
699 gid != oldcred->cr_groups[0] && /* allow setgid(getegid()) */
701 (error = priv_check_cred(oldcred, PRIV_CRED_SETGID)) != 0)
704 #ifdef _POSIX_SAVED_IDS
706 * Do we have "appropriate privileges" (are we root or gid == egid)
707 * If so, we are changing the real uid and saved gid.
710 #ifdef POSIX_APPENDIX_B_4_2_2 /* use the clause from B.4.2.2 */
711 gid == oldcred->cr_groups[0] ||
713 /* We are using privs. */
714 priv_check_cred(oldcred, PRIV_CRED_SETGID) == 0)
720 if (oldcred->cr_rgid != gid) {
721 change_rgid(newcred, gid);
727 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
728 * the security of setegid() depends on it. B.4.2.2 says it
729 * is important that we should do this.
731 if (oldcred->cr_svgid != gid) {
732 change_svgid(newcred, gid);
737 * In all cases permitted cases, we are changing the egid.
738 * Copy credentials so other references do not see our changes.
740 if (oldcred->cr_groups[0] != gid) {
741 change_egid(newcred, gid);
744 proc_set_cred(p, newcred);
755 #ifndef _SYS_SYSPROTO_H_
756 struct setegid_args {
762 sys_setegid(struct thread *td, struct setegid_args *uap)
764 struct proc *p = td->td_proc;
765 struct ucred *newcred, *oldcred;
770 AUDIT_ARG_EGID(egid);
773 oldcred = crcopysafe(p, newcred);
776 error = mac_cred_check_setegid(oldcred, egid);
781 if (egid != oldcred->cr_rgid && /* allow setegid(getgid()) */
782 egid != oldcred->cr_svgid && /* allow setegid(saved gid) */
783 (error = priv_check_cred(oldcred, PRIV_CRED_SETEGID)) != 0)
786 if (oldcred->cr_groups[0] != egid) {
787 change_egid(newcred, egid);
790 proc_set_cred(p, newcred);
801 #ifndef _SYS_SYSPROTO_H_
802 struct setgroups_args {
809 sys_setgroups(struct thread *td, struct setgroups_args *uap)
811 gid_t smallgroups[XU_NGROUPS];
813 int gidsetsize, error;
815 gidsetsize = uap->gidsetsize;
816 if (gidsetsize > ngroups_max + 1 || gidsetsize < 0)
819 if (gidsetsize > XU_NGROUPS)
820 groups = malloc(gidsetsize * sizeof(gid_t), M_TEMP, M_WAITOK);
822 groups = smallgroups;
824 error = copyin(uap->gidset, groups, gidsetsize * sizeof(gid_t));
826 error = kern_setgroups(td, gidsetsize, groups);
828 if (gidsetsize > XU_NGROUPS)
829 free(groups, M_TEMP);
834 kern_setgroups(struct thread *td, u_int ngrp, gid_t *groups)
836 struct proc *p = td->td_proc;
837 struct ucred *newcred, *oldcred;
840 MPASS(ngrp <= ngroups_max + 1);
841 AUDIT_ARG_GROUPSET(groups, ngrp);
843 crextend(newcred, ngrp);
845 oldcred = crcopysafe(p, newcred);
848 error = mac_cred_check_setgroups(oldcred, ngrp, groups);
853 error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS);
859 * setgroups(0, NULL) is a legitimate way of clearing the
860 * groups vector on non-BSD systems (which generally do not
861 * have the egid in the groups[0]). We risk security holes
862 * when running non-BSD software if we do not do the same.
864 newcred->cr_ngroups = 1;
866 crsetgroups_locked(newcred, ngrp, groups);
869 proc_set_cred(p, newcred);
880 #ifndef _SYS_SYSPROTO_H_
881 struct setreuid_args {
888 sys_setreuid(struct thread *td, struct setreuid_args *uap)
890 struct proc *p = td->td_proc;
891 struct ucred *newcred, *oldcred;
893 struct uidinfo *euip, *ruip;
898 AUDIT_ARG_EUID(euid);
899 AUDIT_ARG_RUID(ruid);
904 oldcred = crcopysafe(p, newcred);
907 error = mac_cred_check_setreuid(oldcred, ruid, euid);
912 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
913 ruid != oldcred->cr_svuid) ||
914 (euid != (uid_t)-1 && euid != oldcred->cr_uid &&
915 euid != oldcred->cr_ruid && euid != oldcred->cr_svuid)) &&
916 (error = priv_check_cred(oldcred, PRIV_CRED_SETREUID)) != 0)
919 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
920 change_euid(newcred, euip);
923 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
924 change_ruid(newcred, ruip);
927 if ((ruid != (uid_t)-1 || newcred->cr_uid != newcred->cr_ruid) &&
928 newcred->cr_svuid != newcred->cr_uid) {
929 change_svuid(newcred, newcred->cr_uid);
932 proc_set_cred(p, newcred);
934 racct_proc_ucred_changed(p, oldcred, newcred);
939 rctl_proc_ucred_changed(p, newcred);
955 #ifndef _SYS_SYSPROTO_H_
956 struct setregid_args {
963 sys_setregid(struct thread *td, struct setregid_args *uap)
965 struct proc *p = td->td_proc;
966 struct ucred *newcred, *oldcred;
972 AUDIT_ARG_EGID(egid);
973 AUDIT_ARG_RGID(rgid);
976 oldcred = crcopysafe(p, newcred);
979 error = mac_cred_check_setregid(oldcred, rgid, egid);
984 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
985 rgid != oldcred->cr_svgid) ||
986 (egid != (gid_t)-1 && egid != oldcred->cr_groups[0] &&
987 egid != oldcred->cr_rgid && egid != oldcred->cr_svgid)) &&
988 (error = priv_check_cred(oldcred, PRIV_CRED_SETREGID)) != 0)
991 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
992 change_egid(newcred, egid);
995 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
996 change_rgid(newcred, rgid);
999 if ((rgid != (gid_t)-1 || newcred->cr_groups[0] != newcred->cr_rgid) &&
1000 newcred->cr_svgid != newcred->cr_groups[0]) {
1001 change_svgid(newcred, newcred->cr_groups[0]);
1004 proc_set_cred(p, newcred);
1016 * setresuid(ruid, euid, suid) is like setreuid except control over the saved
1019 #ifndef _SYS_SYSPROTO_H_
1020 struct setresuid_args {
1028 sys_setresuid(struct thread *td, struct setresuid_args *uap)
1030 struct proc *p = td->td_proc;
1031 struct ucred *newcred, *oldcred;
1032 uid_t euid, ruid, suid;
1033 struct uidinfo *euip, *ruip;
1039 AUDIT_ARG_EUID(euid);
1040 AUDIT_ARG_RUID(ruid);
1041 AUDIT_ARG_SUID(suid);
1043 euip = uifind(euid);
1044 ruip = uifind(ruid);
1046 oldcred = crcopysafe(p, newcred);
1049 error = mac_cred_check_setresuid(oldcred, ruid, euid, suid);
1054 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
1055 ruid != oldcred->cr_svuid &&
1056 ruid != oldcred->cr_uid) ||
1057 (euid != (uid_t)-1 && euid != oldcred->cr_ruid &&
1058 euid != oldcred->cr_svuid &&
1059 euid != oldcred->cr_uid) ||
1060 (suid != (uid_t)-1 && suid != oldcred->cr_ruid &&
1061 suid != oldcred->cr_svuid &&
1062 suid != oldcred->cr_uid)) &&
1063 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESUID)) != 0)
1066 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
1067 change_euid(newcred, euip);
1070 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
1071 change_ruid(newcred, ruip);
1074 if (suid != (uid_t)-1 && oldcred->cr_svuid != suid) {
1075 change_svuid(newcred, suid);
1078 proc_set_cred(p, newcred);
1080 racct_proc_ucred_changed(p, oldcred, newcred);
1085 rctl_proc_ucred_changed(p, newcred);
1103 * setresgid(rgid, egid, sgid) is like setregid except control over the saved
1106 #ifndef _SYS_SYSPROTO_H_
1107 struct setresgid_args {
1115 sys_setresgid(struct thread *td, struct setresgid_args *uap)
1117 struct proc *p = td->td_proc;
1118 struct ucred *newcred, *oldcred;
1119 gid_t egid, rgid, sgid;
1125 AUDIT_ARG_EGID(egid);
1126 AUDIT_ARG_RGID(rgid);
1127 AUDIT_ARG_SGID(sgid);
1130 oldcred = crcopysafe(p, newcred);
1133 error = mac_cred_check_setresgid(oldcred, rgid, egid, sgid);
1138 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
1139 rgid != oldcred->cr_svgid &&
1140 rgid != oldcred->cr_groups[0]) ||
1141 (egid != (gid_t)-1 && egid != oldcred->cr_rgid &&
1142 egid != oldcred->cr_svgid &&
1143 egid != oldcred->cr_groups[0]) ||
1144 (sgid != (gid_t)-1 && sgid != oldcred->cr_rgid &&
1145 sgid != oldcred->cr_svgid &&
1146 sgid != oldcred->cr_groups[0])) &&
1147 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESGID)) != 0)
1150 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
1151 change_egid(newcred, egid);
1154 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
1155 change_rgid(newcred, rgid);
1158 if (sgid != (gid_t)-1 && oldcred->cr_svgid != sgid) {
1159 change_svgid(newcred, sgid);
1162 proc_set_cred(p, newcred);
1173 #ifndef _SYS_SYSPROTO_H_
1174 struct getresuid_args {
1182 sys_getresuid(struct thread *td, struct getresuid_args *uap)
1185 int error1 = 0, error2 = 0, error3 = 0;
1187 cred = td->td_ucred;
1189 error1 = copyout(&cred->cr_ruid,
1190 uap->ruid, sizeof(cred->cr_ruid));
1192 error2 = copyout(&cred->cr_uid,
1193 uap->euid, sizeof(cred->cr_uid));
1195 error3 = copyout(&cred->cr_svuid,
1196 uap->suid, sizeof(cred->cr_svuid));
1197 return (error1 ? error1 : error2 ? error2 : error3);
1200 #ifndef _SYS_SYSPROTO_H_
1201 struct getresgid_args {
1209 sys_getresgid(struct thread *td, struct getresgid_args *uap)
1212 int error1 = 0, error2 = 0, error3 = 0;
1214 cred = td->td_ucred;
1216 error1 = copyout(&cred->cr_rgid,
1217 uap->rgid, sizeof(cred->cr_rgid));
1219 error2 = copyout(&cred->cr_groups[0],
1220 uap->egid, sizeof(cred->cr_groups[0]));
1222 error3 = copyout(&cred->cr_svgid,
1223 uap->sgid, sizeof(cred->cr_svgid));
1224 return (error1 ? error1 : error2 ? error2 : error3);
1227 #ifndef _SYS_SYSPROTO_H_
1228 struct issetugid_args {
1234 sys_issetugid(struct thread *td, struct issetugid_args *uap)
1236 struct proc *p = td->td_proc;
1239 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
1240 * we use P_SUGID because we consider changing the owners as
1241 * "tainting" as well.
1242 * This is significant for procs that start as root and "become"
1243 * a user without an exec - programs cannot know *everything*
1244 * that libc *might* have put in their data segment.
1246 td->td_retval[0] = (p->p_flag & P_SUGID) ? 1 : 0;
1251 sys___setugid(struct thread *td, struct __setugid_args *uap)
1257 switch (uap->flag) {
1260 p->p_flag &= ~P_SUGID;
1265 p->p_flag |= P_SUGID;
1271 #else /* !REGRESSION */
1274 #endif /* REGRESSION */
1278 * Returns whether gid designates a supplementary group in cred.
1281 supplementary_group_member(gid_t gid, struct ucred *cred)
1286 * Perform a binary search of the supplemental groups. This is possible
1287 * because we sort the groups in crsetgroups().
1290 h = cred->cr_ngroups;
1293 m = l + (h - l) / 2;
1294 if (cred->cr_groups[m] < gid)
1300 return (l < cred->cr_ngroups && cred->cr_groups[l] == gid);
1304 * Check if gid is a member of the (effective) group set (i.e., effective and
1305 * supplementary groups).
1308 groupmember(gid_t gid, struct ucred *cred)
1311 if (gid == cred->cr_groups[0])
1314 return (supplementary_group_member(gid, cred));
1318 * Check if gid is a member of the real group set (i.e., real and supplementary
1322 realgroupmember(gid_t gid, struct ucred *cred)
1324 if (gid == cred->cr_rgid)
1327 return (supplementary_group_member(gid, cred));
1331 * Test the active securelevel against a given level. securelevel_gt()
1332 * implements (securelevel > level). securelevel_ge() implements
1333 * (securelevel >= level). Note that the logic is inverted -- these
1334 * functions return EPERM on "success" and 0 on "failure".
1336 * Due to care taken when setting the securelevel, we know that no jail will
1337 * be less secure that its parent (or the physical system), so it is sufficient
1338 * to test the current jail only.
1340 * XXXRW: Possibly since this has to do with privilege, it should move to
1344 securelevel_gt(struct ucred *cr, int level)
1347 return (cr->cr_prison->pr_securelevel > level ? EPERM : 0);
1351 securelevel_ge(struct ucred *cr, int level)
1354 return (cr->cr_prison->pr_securelevel >= level ? EPERM : 0);
1358 * 'see_other_uids' determines whether or not visibility of processes
1359 * and sockets with credentials holding different real uids is possible
1360 * using a variety of system MIBs.
1361 * XXX: data declarations should be together near the beginning of the file.
1363 static int see_other_uids = 1;
1364 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_uids, CTLFLAG_RW,
1366 "Unprivileged processes may see subjects/objects with different real uid");
1369 * Determine if u1 "can see" the subject specified by u2, according to the
1370 * 'see_other_uids' policy.
1371 * Returns: 0 for permitted, ESRCH otherwise
1373 * References: *u1 and *u2 must not change during the call
1374 * u1 may equal u2, in which case only one reference is required
1377 cr_canseeotheruids(struct ucred *u1, struct ucred *u2)
1380 if (!see_other_uids && u1->cr_ruid != u2->cr_ruid) {
1381 if (priv_check_cred(u1, PRIV_SEEOTHERUIDS) != 0)
1388 * 'see_other_gids' determines whether or not visibility of processes
1389 * and sockets with credentials holding different real gids is possible
1390 * using a variety of system MIBs.
1391 * XXX: data declarations should be together near the beginning of the file.
1393 static int see_other_gids = 1;
1394 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_gids, CTLFLAG_RW,
1396 "Unprivileged processes may see subjects/objects with different real gid");
1399 * Determine if u1 can "see" the subject specified by u2, according to the
1400 * 'see_other_gids' policy.
1401 * Returns: 0 for permitted, ESRCH otherwise
1403 * References: *u1 and *u2 must not change during the call
1404 * u1 may equal u2, in which case only one reference is required
1407 cr_canseeothergids(struct ucred *u1, struct ucred *u2)
1409 if (!see_other_gids) {
1410 if (realgroupmember(u1->cr_rgid, u2))
1413 for (int i = 1; i < u1->cr_ngroups; i++)
1414 if (realgroupmember(u1->cr_groups[i], u2))
1417 if (priv_check_cred(u1, PRIV_SEEOTHERGIDS) != 0)
1425 * 'see_jail_proc' determines whether or not visibility of processes and
1426 * sockets with credentials holding different jail ids is possible using a
1427 * variety of system MIBs.
1429 * XXX: data declarations should be together near the beginning of the file.
1432 static int see_jail_proc = 1;
1433 SYSCTL_INT(_security_bsd, OID_AUTO, see_jail_proc, CTLFLAG_RW,
1435 "Unprivileged processes may see subjects/objects with different jail ids");
1438 * Determine if u1 "can see" the subject specified by u2, according to the
1439 * 'see_jail_proc' policy.
1440 * Returns: 0 for permitted, ESRCH otherwise
1442 * References: *u1 and *u2 must not change during the call
1443 * u1 may equal u2, in which case only one reference is required
1446 cr_canseejailproc(struct ucred *u1, struct ucred *u2)
1448 if (see_jail_proc || /* Policy deactivated. */
1449 u1->cr_prison == u2->cr_prison || /* Same jail. */
1450 priv_check_cred(u1, PRIV_SEEJAILPROC) == 0) /* Privileged. */
1457 * Helper for cr_cansee*() functions to abide by system-wide security.bsd.see_*
1458 * policies. Determines if u1 "can see" u2 according to these policies.
1459 * Returns: 0 for permitted, ESRCH otherwise
1462 cr_bsd_visible(struct ucred *u1, struct ucred *u2)
1466 if ((error = cr_canseeotheruids(u1, u2)))
1468 if ((error = cr_canseeothergids(u1, u2)))
1470 if ((error = cr_canseejailproc(u1, u2)))
1476 * Determine if u1 "can see" the subject specified by u2.
1477 * Returns: 0 for permitted, an errno value otherwise
1479 * References: *u1 and *u2 must not change during the call
1480 * u1 may equal u2, in which case only one reference is required
1483 cr_cansee(struct ucred *u1, struct ucred *u2)
1487 if ((error = prison_check(u1, u2)))
1490 if ((error = mac_cred_check_visible(u1, u2)))
1493 if ((error = cr_bsd_visible(u1, u2)))
1499 * Determine if td "can see" the subject specified by p.
1500 * Returns: 0 for permitted, an errno value otherwise
1501 * Locks: Sufficient locks to protect p->p_ucred must be held. td really
1502 * should be curthread.
1503 * References: td and p must be valid for the lifetime of the call
1506 p_cansee(struct thread *td, struct proc *p)
1508 /* Wrap cr_cansee() for all functionality. */
1509 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1510 PROC_LOCK_ASSERT(p, MA_OWNED);
1512 if (td->td_proc == p)
1514 return (cr_cansee(td->td_ucred, p->p_ucred));
1518 * 'conservative_signals' prevents the delivery of a broad class of
1519 * signals by unprivileged processes to processes that have changed their
1520 * credentials since the last invocation of execve(). This can prevent
1521 * the leakage of cached information or retained privileges as a result
1522 * of a common class of signal-related vulnerabilities. However, this
1523 * may interfere with some applications that expect to be able to
1524 * deliver these signals to peer processes after having given up
1527 static int conservative_signals = 1;
1528 SYSCTL_INT(_security_bsd, OID_AUTO, conservative_signals, CTLFLAG_RW,
1529 &conservative_signals, 0, "Unprivileged processes prevented from "
1530 "sending certain signals to processes whose credentials have changed");
1532 * Determine whether cred may deliver the specified signal to proc.
1533 * Returns: 0 for permitted, an errno value otherwise.
1534 * Locks: A lock must be held for proc.
1535 * References: cred and proc must be valid for the lifetime of the call.
1538 cr_cansignal(struct ucred *cred, struct proc *proc, int signum)
1542 PROC_LOCK_ASSERT(proc, MA_OWNED);
1544 * Jail semantics limit the scope of signalling to proc in the
1545 * same jail as cred, if cred is in jail.
1547 error = prison_check(cred, proc->p_ucred);
1551 if ((error = mac_proc_check_signal(cred, proc, signum)))
1554 if ((error = cr_bsd_visible(cred, proc->p_ucred)))
1558 * UNIX signal semantics depend on the status of the P_SUGID
1559 * bit on the target process. If the bit is set, then additional
1560 * restrictions are placed on the set of available signals.
1562 if (conservative_signals && (proc->p_flag & P_SUGID)) {
1577 * Generally, permit job and terminal control
1582 /* Not permitted without privilege. */
1583 error = priv_check_cred(cred, PRIV_SIGNAL_SUGID);
1590 * Generally, the target credential's ruid or svuid must match the
1591 * subject credential's ruid or euid.
1593 if (cred->cr_ruid != proc->p_ucred->cr_ruid &&
1594 cred->cr_ruid != proc->p_ucred->cr_svuid &&
1595 cred->cr_uid != proc->p_ucred->cr_ruid &&
1596 cred->cr_uid != proc->p_ucred->cr_svuid) {
1597 error = priv_check_cred(cred, PRIV_SIGNAL_DIFFCRED);
1606 * Determine whether td may deliver the specified signal to p.
1607 * Returns: 0 for permitted, an errno value otherwise
1608 * Locks: Sufficient locks to protect various components of td and p
1609 * must be held. td must be curthread, and a lock must be
1611 * References: td and p must be valid for the lifetime of the call
1614 p_cansignal(struct thread *td, struct proc *p, int signum)
1617 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1618 PROC_LOCK_ASSERT(p, MA_OWNED);
1619 if (td->td_proc == p)
1623 * UNIX signalling semantics require that processes in the same
1624 * session always be able to deliver SIGCONT to one another,
1625 * overriding the remaining protections.
1627 /* XXX: This will require an additional lock of some sort. */
1628 if (signum == SIGCONT && td->td_proc->p_session == p->p_session)
1631 * Some compat layers use SIGTHR and higher signals for
1632 * communication between different kernel threads of the same
1633 * process, so that they expect that it's always possible to
1634 * deliver them, even for suid applications where cr_cansignal() can
1635 * deny such ability for security consideration. It should be
1636 * pretty safe to do since the only way to create two processes
1637 * with the same p_leader is via rfork(2).
1639 if (td->td_proc->p_leader != NULL && signum >= SIGTHR &&
1640 signum < SIGTHR + 4 && td->td_proc->p_leader == p->p_leader)
1643 return (cr_cansignal(td->td_ucred, p, signum));
1647 * Determine whether td may reschedule p.
1648 * Returns: 0 for permitted, an errno value otherwise
1649 * Locks: Sufficient locks to protect various components of td and p
1650 * must be held. td must be curthread, and a lock must
1652 * References: td and p must be valid for the lifetime of the call
1655 p_cansched(struct thread *td, struct proc *p)
1659 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1660 PROC_LOCK_ASSERT(p, MA_OWNED);
1661 if (td->td_proc == p)
1663 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1666 if ((error = mac_proc_check_sched(td->td_ucred, p)))
1669 if ((error = cr_bsd_visible(td->td_ucred, p->p_ucred)))
1672 if (td->td_ucred->cr_ruid != p->p_ucred->cr_ruid &&
1673 td->td_ucred->cr_uid != p->p_ucred->cr_ruid) {
1674 error = priv_check(td, PRIV_SCHED_DIFFCRED);
1682 * Handle getting or setting the prison's unprivileged_proc_debug
1686 sysctl_unprivileged_proc_debug(SYSCTL_HANDLER_ARGS)
1690 val = prison_allow(req->td->td_ucred, PR_ALLOW_UNPRIV_DEBUG);
1691 error = sysctl_handle_int(oidp, &val, 0, req);
1692 if (error != 0 || req->newptr == NULL)
1694 if (val != 0 && val != 1)
1696 prison_set_allow(req->td->td_ucred, PR_ALLOW_UNPRIV_DEBUG, val);
1701 * The 'unprivileged_proc_debug' flag may be used to disable a variety of
1702 * unprivileged inter-process debugging services, including some procfs
1703 * functionality, ptrace(), and ktrace(). In the past, inter-process
1704 * debugging has been involved in a variety of security problems, and sites
1705 * not requiring the service might choose to disable it when hardening
1708 SYSCTL_PROC(_security_bsd, OID_AUTO, unprivileged_proc_debug,
1709 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_SECURE |
1710 CTLFLAG_MPSAFE, 0, 0, sysctl_unprivileged_proc_debug, "I",
1711 "Unprivileged processes may use process debugging facilities");
1714 * Determine whether td may debug p.
1715 * Returns: 0 for permitted, an errno value otherwise
1716 * Locks: Sufficient locks to protect various components of td and p
1717 * must be held. td must be curthread, and a lock must
1719 * References: td and p must be valid for the lifetime of the call
1722 p_candebug(struct thread *td, struct proc *p)
1724 int error, grpsubset, i, uidsubset;
1726 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1727 PROC_LOCK_ASSERT(p, MA_OWNED);
1728 if (td->td_proc == p)
1730 if ((error = priv_check(td, PRIV_DEBUG_UNPRIV)))
1732 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1735 if ((error = mac_proc_check_debug(td->td_ucred, p)))
1738 if ((error = cr_bsd_visible(td->td_ucred, p->p_ucred)))
1742 * Is p's group set a subset of td's effective group set? This
1743 * includes p's egid, group access list, rgid, and svgid.
1746 for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
1747 if (!groupmember(p->p_ucred->cr_groups[i], td->td_ucred)) {
1752 grpsubset = grpsubset &&
1753 groupmember(p->p_ucred->cr_rgid, td->td_ucred) &&
1754 groupmember(p->p_ucred->cr_svgid, td->td_ucred);
1757 * Are the uids present in p's credential equal to td's
1758 * effective uid? This includes p's euid, svuid, and ruid.
1760 uidsubset = (td->td_ucred->cr_uid == p->p_ucred->cr_uid &&
1761 td->td_ucred->cr_uid == p->p_ucred->cr_svuid &&
1762 td->td_ucred->cr_uid == p->p_ucred->cr_ruid);
1765 * If p's gids aren't a subset, or the uids aren't a subset,
1766 * or the credential has changed, require appropriate privilege
1767 * for td to debug p.
1769 if (!grpsubset || !uidsubset) {
1770 error = priv_check(td, PRIV_DEBUG_DIFFCRED);
1776 * Has the credential of the process changed since the last exec()?
1778 if ((p->p_flag & P_SUGID) != 0) {
1779 error = priv_check(td, PRIV_DEBUG_SUGID);
1784 /* Can't trace init when securelevel > 0. */
1785 if (p == initproc) {
1786 error = securelevel_gt(td->td_ucred, 0);
1792 * Can't trace a process that's currently exec'ing.
1794 * XXX: Note, this is not a security policy decision, it's a
1795 * basic correctness/functionality decision. Therefore, this check
1796 * should be moved to the caller's of p_candebug().
1798 if ((p->p_flag & P_INEXEC) != 0)
1801 /* Denied explicitly */
1802 if ((p->p_flag2 & P2_NOTRACE) != 0) {
1803 error = priv_check(td, PRIV_DEBUG_DENIED);
1812 * Determine whether the subject represented by cred can "see" a socket.
1813 * Returns: 0 for permitted, ENOENT otherwise.
1816 cr_canseesocket(struct ucred *cred, struct socket *so)
1820 error = prison_check(cred, so->so_cred);
1824 error = mac_socket_check_visible(cred, so);
1828 if (cr_bsd_visible(cred, so->so_cred))
1835 * Determine whether td can wait for the exit of p.
1836 * Returns: 0 for permitted, an errno value otherwise
1837 * Locks: Sufficient locks to protect various components of td and p
1838 * must be held. td must be curthread, and a lock must
1840 * References: td and p must be valid for the lifetime of the call
1844 p_canwait(struct thread *td, struct proc *p)
1848 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1849 PROC_LOCK_ASSERT(p, MA_OWNED);
1850 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1853 if ((error = mac_proc_check_wait(td->td_ucred, p)))
1857 /* XXXMAC: This could have odd effects on some shells. */
1858 if ((error = cr_bsd_visible(td->td_ucred, p->p_ucred)))
1866 * Credential management.
1868 * struct ucred objects are rarely allocated but gain and lose references all
1869 * the time (e.g., on struct file alloc/dealloc) turning refcount updates into
1870 * a significant source of cache-line ping ponging. Common cases are worked
1871 * around by modifying thread-local counter instead if the cred to operate on
1872 * matches td_realucred.
1874 * The counter is split into 2 parts:
1875 * - cr_users -- total count of all struct proc and struct thread objects
1876 * which have given cred in p_ucred and td_ucred respectively
1877 * - cr_ref -- the actual ref count, only valid if cr_users == 0
1879 * If users == 0 then cr_ref behaves similarly to refcount(9), in particular if
1880 * the count reaches 0 the object is freeable.
1881 * If users > 0 and curthread->td_realucred == cred, then updates are performed
1882 * against td_ucredref.
1883 * In other cases updates are performed against cr_ref.
1885 * Changing td_realucred into something else decrements cr_users and transfers
1886 * accumulated updates.
1889 crcowget(struct ucred *cr)
1892 mtx_lock(&cr->cr_mtx);
1893 KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
1894 __func__, cr->cr_users, cr));
1897 mtx_unlock(&cr->cr_mtx);
1901 static struct ucred *
1902 crunuse(struct thread *td)
1904 struct ucred *cr, *crold;
1906 MPASS(td->td_realucred == td->td_ucred);
1907 cr = td->td_realucred;
1908 mtx_lock(&cr->cr_mtx);
1909 cr->cr_ref += td->td_ucredref;
1910 td->td_ucredref = 0;
1911 KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
1912 __func__, cr->cr_users, cr));
1914 if (cr->cr_users == 0) {
1915 KASSERT(cr->cr_ref > 0, ("%s: ref %ld not > 0 on cred %p",
1916 __func__, cr->cr_ref, cr));
1922 mtx_unlock(&cr->cr_mtx);
1923 td->td_realucred = NULL;
1928 crunusebatch(struct ucred *cr, int users, int ref)
1931 KASSERT(users > 0, ("%s: passed users %d not > 0 ; cred %p",
1932 __func__, users, cr));
1933 mtx_lock(&cr->cr_mtx);
1934 KASSERT(cr->cr_users >= users, ("%s: users %d not > %d on cred %p",
1935 __func__, cr->cr_users, users, cr));
1936 cr->cr_users -= users;
1938 cr->cr_ref -= users;
1939 if (cr->cr_users > 0) {
1940 mtx_unlock(&cr->cr_mtx);
1943 KASSERT(cr->cr_ref >= 0, ("%s: ref %ld not >= 0 on cred %p",
1944 __func__, cr->cr_ref, cr));
1945 if (cr->cr_ref > 0) {
1946 mtx_unlock(&cr->cr_mtx);
1953 crcowfree(struct thread *td)
1967 struct ucred *crnew, *crold;
1971 PROC_LOCK_ASSERT(p, MA_OWNED);
1973 MPASS(td->td_realucred == td->td_ucred);
1974 if (td->td_realucred == p->p_ucred)
1977 crnew = crcowget(p->p_ucred);
1978 crold = crunuse(td);
1979 td->td_realucred = crnew;
1980 td->td_ucred = td->td_realucred;
1988 credbatch_add(struct credbatch *crb, struct thread *td)
1992 MPASS(td->td_realucred != NULL);
1993 MPASS(td->td_realucred == td->td_ucred);
1994 MPASS(TD_GET_STATE(td) == TDS_INACTIVE);
1995 cr = td->td_realucred;
1996 KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
1997 __func__, cr->cr_users, cr));
1998 if (crb->cred != cr) {
1999 if (crb->users > 0) {
2000 MPASS(crb->cred != NULL);
2001 crunusebatch(crb->cred, crb->users, crb->ref);
2008 crb->ref += td->td_ucredref;
2009 td->td_ucredref = 0;
2010 td->td_realucred = NULL;
2014 credbatch_final(struct credbatch *crb)
2017 MPASS(crb->cred != NULL);
2018 MPASS(crb->users > 0);
2019 crunusebatch(crb->cred, crb->users, crb->ref);
2023 * Allocate a zeroed cred structure.
2030 cr = malloc(sizeof(*cr), M_CRED, M_WAITOK | M_ZERO);
2031 mtx_init(&cr->cr_mtx, "cred", NULL, MTX_DEF);
2034 audit_cred_init(cr);
2039 cr->cr_groups = cr->cr_smallgroups;
2041 sizeof(cr->cr_smallgroups) / sizeof(cr->cr_smallgroups[0]);
2046 * Claim another reference to a ucred structure.
2049 crhold(struct ucred *cr)
2054 if (__predict_true(td->td_realucred == cr)) {
2055 KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
2056 __func__, cr->cr_users, cr));
2060 mtx_lock(&cr->cr_mtx);
2062 mtx_unlock(&cr->cr_mtx);
2067 * Free a cred structure. Throws away space when ref count gets to 0.
2070 crfree(struct ucred *cr)
2075 if (__predict_true(td->td_realucred == cr)) {
2076 KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
2077 __func__, cr->cr_users, cr));
2081 mtx_lock(&cr->cr_mtx);
2082 KASSERT(cr->cr_users >= 0, ("%s: users %d not >= 0 on cred %p",
2083 __func__, cr->cr_users, cr));
2085 if (cr->cr_users > 0) {
2086 mtx_unlock(&cr->cr_mtx);
2089 KASSERT(cr->cr_ref >= 0, ("%s: ref %ld not >= 0 on cred %p",
2090 __func__, cr->cr_ref, cr));
2091 if (cr->cr_ref > 0) {
2092 mtx_unlock(&cr->cr_mtx);
2099 crfree_final(struct ucred *cr)
2102 KASSERT(cr->cr_users == 0, ("%s: users %d not == 0 on cred %p",
2103 __func__, cr->cr_users, cr));
2104 KASSERT(cr->cr_ref == 0, ("%s: ref %ld not == 0 on cred %p",
2105 __func__, cr->cr_ref, cr));
2108 * Some callers of crget(), such as nfs_statfs(), allocate a temporary
2109 * credential, but don't allocate a uidinfo structure.
2111 if (cr->cr_uidinfo != NULL)
2112 uifree(cr->cr_uidinfo);
2113 if (cr->cr_ruidinfo != NULL)
2114 uifree(cr->cr_ruidinfo);
2115 if (cr->cr_prison != NULL)
2116 prison_free(cr->cr_prison);
2117 if (cr->cr_loginclass != NULL)
2118 loginclass_free(cr->cr_loginclass);
2120 audit_cred_destroy(cr);
2123 mac_cred_destroy(cr);
2125 mtx_destroy(&cr->cr_mtx);
2126 if (cr->cr_groups != cr->cr_smallgroups)
2127 free(cr->cr_groups, M_CRED);
2132 * Copy a ucred's contents from a template. Does not block.
2135 crcopy(struct ucred *dest, struct ucred *src)
2138 KASSERT(dest->cr_ref == 1, ("crcopy of shared ucred"));
2139 bcopy(&src->cr_startcopy, &dest->cr_startcopy,
2140 (unsigned)((caddr_t)&src->cr_endcopy -
2141 (caddr_t)&src->cr_startcopy));
2142 dest->cr_flags = src->cr_flags;
2143 crsetgroups(dest, src->cr_ngroups, src->cr_groups);
2144 uihold(dest->cr_uidinfo);
2145 uihold(dest->cr_ruidinfo);
2146 prison_hold(dest->cr_prison);
2147 loginclass_hold(dest->cr_loginclass);
2149 audit_cred_copy(src, dest);
2152 mac_cred_copy(src, dest);
2157 * Dup cred struct to a new held one.
2160 crdup(struct ucred *cr)
2162 struct ucred *newcr;
2170 * Fill in a struct xucred based on a struct ucred.
2173 cru2x(struct ucred *cr, struct xucred *xcr)
2177 bzero(xcr, sizeof(*xcr));
2178 xcr->cr_version = XUCRED_VERSION;
2179 xcr->cr_uid = cr->cr_uid;
2181 ngroups = MIN(cr->cr_ngroups, XU_NGROUPS);
2182 xcr->cr_ngroups = ngroups;
2183 bcopy(cr->cr_groups, xcr->cr_groups,
2184 ngroups * sizeof(*cr->cr_groups));
2188 cru2xt(struct thread *td, struct xucred *xcr)
2191 cru2x(td->td_ucred, xcr);
2192 xcr->cr_pid = td->td_proc->p_pid;
2196 * Change process credentials.
2197 * Callers are responsible for providing the reference for passed credentials
2198 * and for freeing old ones.
2200 * Process has to be locked except when it does not have credentials (as it
2201 * should not be visible just yet) or when newcred is NULL (as this can be
2202 * only used when the process is about to be freed, at which point it should
2203 * not be visible anymore).
2206 proc_set_cred(struct proc *p, struct ucred *newcred)
2212 PROC_LOCK_ASSERT(p, MA_OWNED);
2213 KASSERT(newcred->cr_users == 0, ("%s: users %d not 0 on cred %p",
2214 __func__, newcred->cr_users, newcred));
2215 mtx_lock(&cr->cr_mtx);
2216 KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
2217 __func__, cr->cr_users, cr));
2219 mtx_unlock(&cr->cr_mtx);
2220 p->p_ucred = newcred;
2221 newcred->cr_users = 1;
2226 proc_unset_cred(struct proc *p)
2230 MPASS(p->p_state == PRS_ZOMBIE || p->p_state == PRS_NEW);
2233 KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
2234 __func__, cr->cr_users, cr));
2235 mtx_lock(&cr->cr_mtx);
2237 if (cr->cr_users == 0)
2238 KASSERT(cr->cr_ref > 0, ("%s: ref %ld not > 0 on cred %p",
2239 __func__, cr->cr_ref, cr));
2240 mtx_unlock(&cr->cr_mtx);
2245 crcopysafe(struct proc *p, struct ucred *cr)
2247 struct ucred *oldcred;
2250 PROC_LOCK_ASSERT(p, MA_OWNED);
2252 oldcred = p->p_ucred;
2253 while (cr->cr_agroups < oldcred->cr_agroups) {
2254 groups = oldcred->cr_agroups;
2256 crextend(cr, groups);
2258 oldcred = p->p_ucred;
2260 crcopy(cr, oldcred);
2266 * Extend the passed in credential to hold n items.
2269 crextend(struct ucred *cr, int n)
2274 if (n <= cr->cr_agroups)
2278 * We extend by 2 each time since we're using a power of two
2279 * allocator until we need enough groups to fill a page.
2280 * Once we're allocating multiple pages, only allocate as many
2281 * as we actually need. The case of processes needing a
2282 * non-power of two number of pages seems more likely than
2283 * a real world process that adds thousands of groups one at a
2286 if ( n < PAGE_SIZE / sizeof(gid_t) ) {
2287 if (cr->cr_agroups == 0)
2288 cnt = MAX(1, MINALLOCSIZE / sizeof(gid_t));
2290 cnt = cr->cr_agroups * 2;
2295 cnt = roundup2(n, PAGE_SIZE / sizeof(gid_t));
2297 /* Free the old array. */
2298 if (cr->cr_groups != cr->cr_smallgroups)
2299 free(cr->cr_groups, M_CRED);
2301 cr->cr_groups = malloc(cnt * sizeof(gid_t), M_CRED, M_WAITOK | M_ZERO);
2302 cr->cr_agroups = cnt;
2306 * Copy groups in to a credential, preserving any necessary invariants.
2307 * Currently this includes the sorting of all supplemental gids.
2308 * crextend() must have been called before hand to ensure sufficient
2309 * space is available.
2312 crsetgroups_locked(struct ucred *cr, int ngrp, gid_t *groups)
2318 KASSERT(cr->cr_agroups >= ngrp, ("cr_ngroups is too small"));
2320 bcopy(groups, cr->cr_groups, ngrp * sizeof(gid_t));
2321 cr->cr_ngroups = ngrp;
2324 * Sort all groups except cr_groups[0] to allow groupmember to
2325 * perform a binary search.
2327 * XXX: If large numbers of groups become common this should
2328 * be replaced with shell sort like linux uses or possibly
2331 for (i = 2; i < ngrp; i++) {
2332 g = cr->cr_groups[i];
2333 for (j = i-1; j >= 1 && g < cr->cr_groups[j]; j--)
2334 cr->cr_groups[j + 1] = cr->cr_groups[j];
2335 cr->cr_groups[j + 1] = g;
2340 * Copy groups in to a credential after expanding it if required.
2341 * Truncate the list to (ngroups_max + 1) if it is too large.
2344 crsetgroups(struct ucred *cr, int ngrp, gid_t *groups)
2347 if (ngrp > ngroups_max + 1)
2348 ngrp = ngroups_max + 1;
2351 crsetgroups_locked(cr, ngrp, groups);
2355 * Get login name, if available.
2357 #ifndef _SYS_SYSPROTO_H_
2358 struct getlogin_args {
2365 sys_getlogin(struct thread *td, struct getlogin_args *uap)
2367 char login[MAXLOGNAME];
2368 struct proc *p = td->td_proc;
2371 if (uap->namelen > MAXLOGNAME)
2372 uap->namelen = MAXLOGNAME;
2374 SESS_LOCK(p->p_session);
2375 len = strlcpy(login, p->p_session->s_login, uap->namelen) + 1;
2376 SESS_UNLOCK(p->p_session);
2378 if (len > uap->namelen)
2380 return (copyout(login, uap->namebuf, len));
2386 #ifndef _SYS_SYSPROTO_H_
2387 struct setlogin_args {
2393 sys_setlogin(struct thread *td, struct setlogin_args *uap)
2395 struct proc *p = td->td_proc;
2397 char logintmp[MAXLOGNAME];
2399 CTASSERT(sizeof(p->p_session->s_login) >= sizeof(logintmp));
2401 error = priv_check(td, PRIV_PROC_SETLOGIN);
2404 error = copyinstr(uap->namebuf, logintmp, sizeof(logintmp), NULL);
2406 if (error == ENAMETOOLONG)
2410 AUDIT_ARG_LOGIN(logintmp);
2412 SESS_LOCK(p->p_session);
2413 strcpy(p->p_session->s_login, logintmp);
2414 SESS_UNLOCK(p->p_session);
2420 setsugid(struct proc *p)
2423 PROC_LOCK_ASSERT(p, MA_OWNED);
2424 p->p_flag |= P_SUGID;
2428 * Change a process's effective uid.
2429 * Side effects: newcred->cr_uid and newcred->cr_uidinfo will be modified.
2430 * References: newcred must be an exclusive credential reference for the
2431 * duration of the call.
2434 change_euid(struct ucred *newcred, struct uidinfo *euip)
2437 newcred->cr_uid = euip->ui_uid;
2439 uifree(newcred->cr_uidinfo);
2440 newcred->cr_uidinfo = euip;
2444 * Change a process's effective gid.
2445 * Side effects: newcred->cr_gid will be modified.
2446 * References: newcred must be an exclusive credential reference for the
2447 * duration of the call.
2450 change_egid(struct ucred *newcred, gid_t egid)
2453 newcred->cr_groups[0] = egid;
2457 * Change a process's real uid.
2458 * Side effects: newcred->cr_ruid will be updated, newcred->cr_ruidinfo
2459 * will be updated, and the old and new cr_ruidinfo proc
2460 * counts will be updated.
2461 * References: newcred must be an exclusive credential reference for the
2462 * duration of the call.
2465 change_ruid(struct ucred *newcred, struct uidinfo *ruip)
2468 (void)chgproccnt(newcred->cr_ruidinfo, -1, 0);
2469 newcred->cr_ruid = ruip->ui_uid;
2471 uifree(newcred->cr_ruidinfo);
2472 newcred->cr_ruidinfo = ruip;
2473 (void)chgproccnt(newcred->cr_ruidinfo, 1, 0);
2477 * Change a process's real gid.
2478 * Side effects: newcred->cr_rgid will be updated.
2479 * References: newcred must be an exclusive credential reference for the
2480 * duration of the call.
2483 change_rgid(struct ucred *newcred, gid_t rgid)
2486 newcred->cr_rgid = rgid;
2490 * Change a process's saved uid.
2491 * Side effects: newcred->cr_svuid will be updated.
2492 * References: newcred must be an exclusive credential reference for the
2493 * duration of the call.
2496 change_svuid(struct ucred *newcred, uid_t svuid)
2499 newcred->cr_svuid = svuid;
2503 * Change a process's saved gid.
2504 * Side effects: newcred->cr_svgid will be updated.
2505 * References: newcred must be an exclusive credential reference for the
2506 * duration of the call.
2509 change_svgid(struct ucred *newcred, gid_t svgid)
2512 newcred->cr_svgid = svgid;
2515 bool allow_ptrace = true;
2516 SYSCTL_BOOL(_security_bsd, OID_AUTO, allow_ptrace, CTLFLAG_RWTUN,
2518 "Deny ptrace(2) use by returning ENOSYS");