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
39 * @(#)kern_prot.c 8.6 (Berkeley) 1/21/94
43 * System calls related to processes and protection
46 #include <sys/cdefs.h>
48 #include "opt_inet6.h"
50 #include <sys/param.h>
51 #include <sys/systm.h>
54 #include <sys/kernel.h>
56 #include <sys/loginclass.h>
57 #include <sys/malloc.h>
58 #include <sys/mutex.h>
59 #include <sys/ptrace.h>
60 #include <sys/refcount.h>
65 #include <sys/sysent.h>
67 #include <sys/sysproto.h>
69 #include <sys/racct.h>
71 #include <sys/resourcevar.h>
72 #include <sys/socket.h>
73 #include <sys/socketvar.h>
74 #include <sys/syscallsubr.h>
75 #include <sys/sysctl.h>
79 "Kernel support for interfaces necessary for regression testing (SECURITY RISK!)");
82 #include <security/audit/audit.h>
83 #include <security/mac/mac_framework.h>
85 static MALLOC_DEFINE(M_CRED, "cred", "credentials");
87 SYSCTL_NODE(_security, OID_AUTO, bsd, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
88 "BSD security policy");
90 static void crfree_final(struct ucred *cr);
91 static void crsetgroups_locked(struct ucred *cr, int ngrp,
94 static int cr_canseeotheruids(struct ucred *u1, struct ucred *u2);
95 static int cr_canseeothergids(struct ucred *u1, struct ucred *u2);
96 static int cr_canseejailproc(struct ucred *u1, struct ucred *u2);
98 #ifndef _SYS_SYSPROTO_H_
105 sys_getpid(struct thread *td, struct getpid_args *uap)
107 struct proc *p = td->td_proc;
109 td->td_retval[0] = p->p_pid;
110 #if defined(COMPAT_43)
111 if (SV_PROC_FLAG(p, SV_AOUT))
112 td->td_retval[1] = kern_getppid(td);
117 #ifndef _SYS_SYSPROTO_H_
118 struct getppid_args {
124 sys_getppid(struct thread *td, struct getppid_args *uap)
127 td->td_retval[0] = kern_getppid(td);
132 kern_getppid(struct thread *td)
134 struct proc *p = td->td_proc;
140 * Get process group ID; note that POSIX getpgrp takes no parameter.
142 #ifndef _SYS_SYSPROTO_H_
143 struct getpgrp_args {
148 sys_getpgrp(struct thread *td, struct getpgrp_args *uap)
150 struct proc *p = td->td_proc;
153 td->td_retval[0] = p->p_pgrp->pg_id;
158 /* Get an arbitrary pid's process group id */
159 #ifndef _SYS_SYSPROTO_H_
160 struct getpgid_args {
165 sys_getpgid(struct thread *td, struct getpgid_args *uap)
177 error = p_cansee(td, p);
183 td->td_retval[0] = p->p_pgrp->pg_id;
189 * Get an arbitrary pid's session id.
191 #ifndef _SYS_SYSPROTO_H_
197 sys_getsid(struct thread *td, struct getsid_args *uap)
200 return (kern_getsid(td, uap->pid));
204 kern_getsid(struct thread *td, pid_t pid)
216 error = p_cansee(td, p);
222 td->td_retval[0] = p->p_session->s_sid;
227 #ifndef _SYS_SYSPROTO_H_
234 sys_getuid(struct thread *td, struct getuid_args *uap)
237 td->td_retval[0] = td->td_ucred->cr_ruid;
238 #if defined(COMPAT_43)
239 td->td_retval[1] = td->td_ucred->cr_uid;
244 #ifndef _SYS_SYSPROTO_H_
245 struct geteuid_args {
251 sys_geteuid(struct thread *td, struct geteuid_args *uap)
254 td->td_retval[0] = td->td_ucred->cr_uid;
258 #ifndef _SYS_SYSPROTO_H_
265 sys_getgid(struct thread *td, struct getgid_args *uap)
268 td->td_retval[0] = td->td_ucred->cr_rgid;
269 #if defined(COMPAT_43)
270 td->td_retval[1] = td->td_ucred->cr_groups[0];
276 * Get effective group ID. The "egid" is groups[0], and could be obtained
277 * via getgroups. This syscall exists because it is somewhat painful to do
278 * correctly in a library function.
280 #ifndef _SYS_SYSPROTO_H_
281 struct getegid_args {
287 sys_getegid(struct thread *td, struct getegid_args *uap)
290 td->td_retval[0] = td->td_ucred->cr_groups[0];
294 #ifndef _SYS_SYSPROTO_H_
295 struct getgroups_args {
301 sys_getgroups(struct thread *td, struct getgroups_args *uap)
307 ngrp = cred->cr_ngroups;
309 if (uap->gidsetsize == 0) {
313 if (uap->gidsetsize < ngrp)
316 error = copyout(cred->cr_groups, uap->gidset, ngrp * sizeof(gid_t));
318 td->td_retval[0] = ngrp;
322 #ifndef _SYS_SYSPROTO_H_
329 sys_setsid(struct thread *td, struct setsid_args *uap)
333 struct proc *p = td->td_proc;
334 struct pgrp *newpgrp;
335 struct session *newsess;
339 newpgrp = uma_zalloc(pgrp_zone, M_WAITOK);
340 newsess = malloc(sizeof(struct session), M_SESSION, M_WAITOK | M_ZERO);
344 sx_xlock(&proctree_lock);
346 if (p->p_pgid == p->p_pid || (pgrp = pgfind(p->p_pid)) != NULL) {
351 error = enterpgrp(p, p->p_pid, newpgrp, newsess);
352 if (error == ERESTART)
355 td->td_retval[0] = p->p_pid;
360 sx_xunlock(&proctree_lock);
362 uma_zfree(pgrp_zone, newpgrp);
363 free(newsess, M_SESSION);
369 * set process group (setpgid/old setpgrp)
371 * caller does setpgid(targpid, targpgid)
373 * pid must be caller or child of caller (ESRCH)
375 * pid must be in same session (EPERM)
376 * pid can't have done an exec (EACCES)
378 * there must exist some pid in same session having pgid (EPERM)
379 * pid must not be session leader (EPERM)
381 #ifndef _SYS_SYSPROTO_H_
382 struct setpgid_args {
383 int pid; /* target process id */
384 int pgid; /* target pgrp id */
389 sys_setpgid(struct thread *td, struct setpgid_args *uap)
391 struct proc *curp = td->td_proc;
392 struct proc *targp; /* target process */
393 struct pgrp *pgrp; /* target pgrp */
395 struct pgrp *newpgrp;
400 newpgrp = uma_zalloc(pgrp_zone, M_WAITOK);
405 sx_xlock(&proctree_lock);
406 if (uap->pid != 0 && uap->pid != curp->p_pid) {
407 if ((targp = pfind(uap->pid)) == NULL) {
411 if (!inferior(targp)) {
416 if ((error = p_cansee(td, targp))) {
420 if (targp->p_pgrp == NULL ||
421 targp->p_session != curp->p_session) {
426 if (targp->p_flag & P_EXEC) {
434 if (SESS_LEADER(targp)) {
439 uap->pgid = targp->p_pid;
440 if ((pgrp = pgfind(uap->pgid)) == NULL) {
441 if (uap->pgid == targp->p_pid) {
442 error = enterpgrp(targp, uap->pgid, newpgrp,
449 if (pgrp == targp->p_pgrp) {
453 if (pgrp->pg_id != targp->p_pid &&
454 pgrp->pg_session != curp->p_session) {
460 error = enterthispgrp(targp, pgrp);
463 KASSERT(error == 0 || newpgrp != NULL,
464 ("setpgid failed and newpgrp is NULL"));
465 if (error == ERESTART)
467 sx_xunlock(&proctree_lock);
468 uma_zfree(pgrp_zone, newpgrp);
473 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
474 * compatible. It says that setting the uid/gid to euid/egid is a special
475 * case of "appropriate privilege". Once the rules are expanded out, this
476 * basically means that setuid(nnn) sets all three id's, in all permitted
477 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
478 * does not set the saved id - this is dangerous for traditional BSD
479 * programs. For this reason, we *really* do not want to set
480 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
482 #define POSIX_APPENDIX_B_4_2_2
484 #ifndef _SYS_SYSPROTO_H_
491 sys_setuid(struct thread *td, struct setuid_args *uap)
493 struct proc *p = td->td_proc;
494 struct ucred *newcred, *oldcred;
505 * Copy credentials so other references do not see our changes.
507 oldcred = crcopysafe(p, newcred);
510 error = mac_cred_check_setuid(oldcred, uid);
516 * See if we have "permission" by POSIX 1003.1 rules.
518 * Note that setuid(geteuid()) is a special case of
519 * "appropriate privileges" in appendix B.4.2.2. We need
520 * to use this clause to be compatible with traditional BSD
521 * semantics. Basically, it means that "setuid(xx)" sets all
522 * three id's (assuming you have privs).
524 * Notes on the logic. We do things in three steps.
525 * 1: We determine if the euid is going to change, and do EPERM
526 * right away. We unconditionally change the euid later if this
527 * test is satisfied, simplifying that part of the logic.
528 * 2: We determine if the real and/or saved uids are going to
529 * change. Determined by compile options.
530 * 3: Change euid last. (after tests in #2 for "appropriate privs")
532 if (uid != oldcred->cr_ruid && /* allow setuid(getuid()) */
533 #ifdef _POSIX_SAVED_IDS
534 uid != oldcred->cr_svuid && /* allow setuid(saved gid) */
536 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
537 uid != oldcred->cr_uid && /* allow setuid(geteuid()) */
539 (error = priv_check_cred(oldcred, PRIV_CRED_SETUID)) != 0)
542 #ifdef _POSIX_SAVED_IDS
544 * Do we have "appropriate privileges" (are we root or uid == euid)
545 * If so, we are changing the real uid and/or saved uid.
548 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
549 uid == oldcred->cr_uid ||
551 /* We are using privs. */
552 priv_check_cred(oldcred, PRIV_CRED_SETUID) == 0)
556 * Set the real uid and transfer proc count to new user.
558 if (uid != oldcred->cr_ruid) {
559 change_ruid(newcred, uip);
565 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
566 * the security of seteuid() depends on it. B.4.2.2 says it
567 * is important that we should do this.
569 if (uid != oldcred->cr_svuid) {
570 change_svuid(newcred, uid);
576 * In all permitted cases, we are changing the euid.
578 if (uid != oldcred->cr_uid) {
579 change_euid(newcred, uip);
582 proc_set_cred(p, newcred);
584 racct_proc_ucred_changed(p, oldcred, newcred);
589 rctl_proc_ucred_changed(p, newcred);
603 #ifndef _SYS_SYSPROTO_H_
604 struct seteuid_args {
610 sys_seteuid(struct thread *td, struct seteuid_args *uap)
612 struct proc *p = td->td_proc;
613 struct ucred *newcred, *oldcred;
615 struct uidinfo *euip;
619 AUDIT_ARG_EUID(euid);
624 * Copy credentials so other references do not see our changes.
626 oldcred = crcopysafe(p, newcred);
629 error = mac_cred_check_seteuid(oldcred, euid);
634 if (euid != oldcred->cr_ruid && /* allow seteuid(getuid()) */
635 euid != oldcred->cr_svuid && /* allow seteuid(saved uid) */
636 (error = priv_check_cred(oldcred, PRIV_CRED_SETEUID)) != 0)
640 * Everything's okay, do it.
642 if (oldcred->cr_uid != euid) {
643 change_euid(newcred, euip);
646 proc_set_cred(p, newcred);
659 #ifndef _SYS_SYSPROTO_H_
666 sys_setgid(struct thread *td, struct setgid_args *uap)
668 struct proc *p = td->td_proc;
669 struct ucred *newcred, *oldcred;
677 oldcred = crcopysafe(p, newcred);
680 error = mac_cred_check_setgid(oldcred, gid);
686 * See if we have "permission" by POSIX 1003.1 rules.
688 * Note that setgid(getegid()) is a special case of
689 * "appropriate privileges" in appendix B.4.2.2. We need
690 * to use this clause to be compatible with traditional BSD
691 * semantics. Basically, it means that "setgid(xx)" sets all
692 * three id's (assuming you have privs).
694 * For notes on the logic here, see setuid() above.
696 if (gid != oldcred->cr_rgid && /* allow setgid(getgid()) */
697 #ifdef _POSIX_SAVED_IDS
698 gid != oldcred->cr_svgid && /* allow setgid(saved gid) */
700 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
701 gid != oldcred->cr_groups[0] && /* allow setgid(getegid()) */
703 (error = priv_check_cred(oldcred, PRIV_CRED_SETGID)) != 0)
706 #ifdef _POSIX_SAVED_IDS
708 * Do we have "appropriate privileges" (are we root or gid == egid)
709 * If so, we are changing the real uid and saved gid.
712 #ifdef POSIX_APPENDIX_B_4_2_2 /* use the clause from B.4.2.2 */
713 gid == oldcred->cr_groups[0] ||
715 /* We are using privs. */
716 priv_check_cred(oldcred, PRIV_CRED_SETGID) == 0)
722 if (oldcred->cr_rgid != gid) {
723 change_rgid(newcred, gid);
729 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
730 * the security of setegid() depends on it. B.4.2.2 says it
731 * is important that we should do this.
733 if (oldcred->cr_svgid != gid) {
734 change_svgid(newcred, gid);
739 * In all cases permitted cases, we are changing the egid.
740 * Copy credentials so other references do not see our changes.
742 if (oldcred->cr_groups[0] != gid) {
743 change_egid(newcred, gid);
746 proc_set_cred(p, newcred);
757 #ifndef _SYS_SYSPROTO_H_
758 struct setegid_args {
764 sys_setegid(struct thread *td, struct setegid_args *uap)
766 struct proc *p = td->td_proc;
767 struct ucred *newcred, *oldcred;
772 AUDIT_ARG_EGID(egid);
775 oldcred = crcopysafe(p, newcred);
778 error = mac_cred_check_setegid(oldcred, egid);
783 if (egid != oldcred->cr_rgid && /* allow setegid(getgid()) */
784 egid != oldcred->cr_svgid && /* allow setegid(saved gid) */
785 (error = priv_check_cred(oldcred, PRIV_CRED_SETEGID)) != 0)
788 if (oldcred->cr_groups[0] != egid) {
789 change_egid(newcred, egid);
792 proc_set_cred(p, newcred);
803 #ifndef _SYS_SYSPROTO_H_
804 struct setgroups_args {
811 sys_setgroups(struct thread *td, struct setgroups_args *uap)
813 gid_t smallgroups[XU_NGROUPS];
815 int gidsetsize, error;
817 gidsetsize = uap->gidsetsize;
818 if (gidsetsize > ngroups_max + 1 || gidsetsize < 0)
821 if (gidsetsize > XU_NGROUPS)
822 groups = malloc(gidsetsize * sizeof(gid_t), M_TEMP, M_WAITOK);
824 groups = smallgroups;
826 error = copyin(uap->gidset, groups, gidsetsize * sizeof(gid_t));
828 error = kern_setgroups(td, gidsetsize, groups);
830 if (gidsetsize > XU_NGROUPS)
831 free(groups, M_TEMP);
836 kern_setgroups(struct thread *td, u_int ngrp, gid_t *groups)
838 struct proc *p = td->td_proc;
839 struct ucred *newcred, *oldcred;
842 MPASS(ngrp <= ngroups_max + 1);
843 AUDIT_ARG_GROUPSET(groups, ngrp);
845 crextend(newcred, ngrp);
847 oldcred = crcopysafe(p, newcred);
850 error = mac_cred_check_setgroups(oldcred, ngrp, groups);
855 error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS);
861 * setgroups(0, NULL) is a legitimate way of clearing the
862 * groups vector on non-BSD systems (which generally do not
863 * have the egid in the groups[0]). We risk security holes
864 * when running non-BSD software if we do not do the same.
866 newcred->cr_ngroups = 1;
868 crsetgroups_locked(newcred, ngrp, groups);
871 proc_set_cred(p, newcred);
882 #ifndef _SYS_SYSPROTO_H_
883 struct setreuid_args {
890 sys_setreuid(struct thread *td, struct setreuid_args *uap)
892 struct proc *p = td->td_proc;
893 struct ucred *newcred, *oldcred;
895 struct uidinfo *euip, *ruip;
900 AUDIT_ARG_EUID(euid);
901 AUDIT_ARG_RUID(ruid);
906 oldcred = crcopysafe(p, newcred);
909 error = mac_cred_check_setreuid(oldcred, ruid, euid);
914 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
915 ruid != oldcred->cr_svuid) ||
916 (euid != (uid_t)-1 && euid != oldcred->cr_uid &&
917 euid != oldcred->cr_ruid && euid != oldcred->cr_svuid)) &&
918 (error = priv_check_cred(oldcred, PRIV_CRED_SETREUID)) != 0)
921 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
922 change_euid(newcred, euip);
925 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
926 change_ruid(newcred, ruip);
929 if ((ruid != (uid_t)-1 || newcred->cr_uid != newcred->cr_ruid) &&
930 newcred->cr_svuid != newcred->cr_uid) {
931 change_svuid(newcred, newcred->cr_uid);
934 proc_set_cred(p, newcred);
936 racct_proc_ucred_changed(p, oldcred, newcred);
941 rctl_proc_ucred_changed(p, newcred);
957 #ifndef _SYS_SYSPROTO_H_
958 struct setregid_args {
965 sys_setregid(struct thread *td, struct setregid_args *uap)
967 struct proc *p = td->td_proc;
968 struct ucred *newcred, *oldcred;
974 AUDIT_ARG_EGID(egid);
975 AUDIT_ARG_RGID(rgid);
978 oldcred = crcopysafe(p, newcred);
981 error = mac_cred_check_setregid(oldcred, rgid, egid);
986 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
987 rgid != oldcred->cr_svgid) ||
988 (egid != (gid_t)-1 && egid != oldcred->cr_groups[0] &&
989 egid != oldcred->cr_rgid && egid != oldcred->cr_svgid)) &&
990 (error = priv_check_cred(oldcred, PRIV_CRED_SETREGID)) != 0)
993 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
994 change_egid(newcred, egid);
997 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
998 change_rgid(newcred, rgid);
1001 if ((rgid != (gid_t)-1 || newcred->cr_groups[0] != newcred->cr_rgid) &&
1002 newcred->cr_svgid != newcred->cr_groups[0]) {
1003 change_svgid(newcred, newcred->cr_groups[0]);
1006 proc_set_cred(p, newcred);
1018 * setresuid(ruid, euid, suid) is like setreuid except control over the saved
1021 #ifndef _SYS_SYSPROTO_H_
1022 struct setresuid_args {
1030 sys_setresuid(struct thread *td, struct setresuid_args *uap)
1032 struct proc *p = td->td_proc;
1033 struct ucred *newcred, *oldcred;
1034 uid_t euid, ruid, suid;
1035 struct uidinfo *euip, *ruip;
1041 AUDIT_ARG_EUID(euid);
1042 AUDIT_ARG_RUID(ruid);
1043 AUDIT_ARG_SUID(suid);
1045 euip = uifind(euid);
1046 ruip = uifind(ruid);
1048 oldcred = crcopysafe(p, newcred);
1051 error = mac_cred_check_setresuid(oldcred, ruid, euid, suid);
1056 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
1057 ruid != oldcred->cr_svuid &&
1058 ruid != oldcred->cr_uid) ||
1059 (euid != (uid_t)-1 && euid != oldcred->cr_ruid &&
1060 euid != oldcred->cr_svuid &&
1061 euid != oldcred->cr_uid) ||
1062 (suid != (uid_t)-1 && suid != oldcred->cr_ruid &&
1063 suid != oldcred->cr_svuid &&
1064 suid != oldcred->cr_uid)) &&
1065 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESUID)) != 0)
1068 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
1069 change_euid(newcred, euip);
1072 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
1073 change_ruid(newcred, ruip);
1076 if (suid != (uid_t)-1 && oldcred->cr_svuid != suid) {
1077 change_svuid(newcred, suid);
1080 proc_set_cred(p, newcred);
1082 racct_proc_ucred_changed(p, oldcred, newcred);
1087 rctl_proc_ucred_changed(p, newcred);
1105 * setresgid(rgid, egid, sgid) is like setregid except control over the saved
1108 #ifndef _SYS_SYSPROTO_H_
1109 struct setresgid_args {
1117 sys_setresgid(struct thread *td, struct setresgid_args *uap)
1119 struct proc *p = td->td_proc;
1120 struct ucred *newcred, *oldcred;
1121 gid_t egid, rgid, sgid;
1127 AUDIT_ARG_EGID(egid);
1128 AUDIT_ARG_RGID(rgid);
1129 AUDIT_ARG_SGID(sgid);
1132 oldcred = crcopysafe(p, newcred);
1135 error = mac_cred_check_setresgid(oldcred, rgid, egid, sgid);
1140 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
1141 rgid != oldcred->cr_svgid &&
1142 rgid != oldcred->cr_groups[0]) ||
1143 (egid != (gid_t)-1 && egid != oldcred->cr_rgid &&
1144 egid != oldcred->cr_svgid &&
1145 egid != oldcred->cr_groups[0]) ||
1146 (sgid != (gid_t)-1 && sgid != oldcred->cr_rgid &&
1147 sgid != oldcred->cr_svgid &&
1148 sgid != oldcred->cr_groups[0])) &&
1149 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESGID)) != 0)
1152 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
1153 change_egid(newcred, egid);
1156 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
1157 change_rgid(newcred, rgid);
1160 if (sgid != (gid_t)-1 && oldcred->cr_svgid != sgid) {
1161 change_svgid(newcred, sgid);
1164 proc_set_cred(p, newcred);
1175 #ifndef _SYS_SYSPROTO_H_
1176 struct getresuid_args {
1184 sys_getresuid(struct thread *td, struct getresuid_args *uap)
1187 int error1 = 0, error2 = 0, error3 = 0;
1189 cred = td->td_ucred;
1191 error1 = copyout(&cred->cr_ruid,
1192 uap->ruid, sizeof(cred->cr_ruid));
1194 error2 = copyout(&cred->cr_uid,
1195 uap->euid, sizeof(cred->cr_uid));
1197 error3 = copyout(&cred->cr_svuid,
1198 uap->suid, sizeof(cred->cr_svuid));
1199 return (error1 ? error1 : error2 ? error2 : error3);
1202 #ifndef _SYS_SYSPROTO_H_
1203 struct getresgid_args {
1211 sys_getresgid(struct thread *td, struct getresgid_args *uap)
1214 int error1 = 0, error2 = 0, error3 = 0;
1216 cred = td->td_ucred;
1218 error1 = copyout(&cred->cr_rgid,
1219 uap->rgid, sizeof(cred->cr_rgid));
1221 error2 = copyout(&cred->cr_groups[0],
1222 uap->egid, sizeof(cred->cr_groups[0]));
1224 error3 = copyout(&cred->cr_svgid,
1225 uap->sgid, sizeof(cred->cr_svgid));
1226 return (error1 ? error1 : error2 ? error2 : error3);
1229 #ifndef _SYS_SYSPROTO_H_
1230 struct issetugid_args {
1236 sys_issetugid(struct thread *td, struct issetugid_args *uap)
1238 struct proc *p = td->td_proc;
1241 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
1242 * we use P_SUGID because we consider changing the owners as
1243 * "tainting" as well.
1244 * This is significant for procs that start as root and "become"
1245 * a user without an exec - programs cannot know *everything*
1246 * that libc *might* have put in their data segment.
1248 td->td_retval[0] = (p->p_flag & P_SUGID) ? 1 : 0;
1253 sys___setugid(struct thread *td, struct __setugid_args *uap)
1259 switch (uap->flag) {
1262 p->p_flag &= ~P_SUGID;
1267 p->p_flag |= P_SUGID;
1273 #else /* !REGRESSION */
1276 #endif /* REGRESSION */
1280 * Returns whether gid designates a supplementary group in cred.
1283 supplementary_group_member(gid_t gid, struct ucred *cred)
1288 * Perform a binary search of the supplemental groups. This is possible
1289 * because we sort the groups in crsetgroups().
1292 h = cred->cr_ngroups;
1295 m = l + (h - l) / 2;
1296 if (cred->cr_groups[m] < gid)
1302 return (l < cred->cr_ngroups && cred->cr_groups[l] == gid);
1306 * Check if gid is a member of the (effective) group set (i.e., effective and
1307 * supplementary groups).
1310 groupmember(gid_t gid, struct ucred *cred)
1313 if (gid == cred->cr_groups[0])
1316 return (supplementary_group_member(gid, cred));
1320 * Check if gid is a member of the real group set (i.e., real and supplementary
1324 realgroupmember(gid_t gid, struct ucred *cred)
1326 if (gid == cred->cr_rgid)
1329 return (supplementary_group_member(gid, cred));
1333 * Test the active securelevel against a given level. securelevel_gt()
1334 * implements (securelevel > level). securelevel_ge() implements
1335 * (securelevel >= level). Note that the logic is inverted -- these
1336 * functions return EPERM on "success" and 0 on "failure".
1338 * Due to care taken when setting the securelevel, we know that no jail will
1339 * be less secure that its parent (or the physical system), so it is sufficient
1340 * to test the current jail only.
1342 * XXXRW: Possibly since this has to do with privilege, it should move to
1346 securelevel_gt(struct ucred *cr, int level)
1349 return (cr->cr_prison->pr_securelevel > level ? EPERM : 0);
1353 securelevel_ge(struct ucred *cr, int level)
1356 return (cr->cr_prison->pr_securelevel >= level ? EPERM : 0);
1360 * 'see_other_uids' determines whether or not visibility of processes
1361 * and sockets with credentials holding different real uids is possible
1362 * using a variety of system MIBs.
1363 * XXX: data declarations should be together near the beginning of the file.
1365 static int see_other_uids = 1;
1366 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_uids, CTLFLAG_RW,
1368 "Unprivileged processes may see subjects/objects with different real uid");
1371 * Determine if u1 "can see" the subject specified by u2, according to the
1372 * 'see_other_uids' policy.
1373 * Returns: 0 for permitted, ESRCH otherwise
1375 * References: *u1 and *u2 must not change during the call
1376 * u1 may equal u2, in which case only one reference is required
1379 cr_canseeotheruids(struct ucred *u1, struct ucred *u2)
1382 if (!see_other_uids && u1->cr_ruid != u2->cr_ruid) {
1383 if (priv_check_cred(u1, PRIV_SEEOTHERUIDS) != 0)
1390 * 'see_other_gids' determines whether or not visibility of processes
1391 * and sockets with credentials holding different real gids is possible
1392 * using a variety of system MIBs.
1393 * XXX: data declarations should be together near the beginning of the file.
1395 static int see_other_gids = 1;
1396 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_gids, CTLFLAG_RW,
1398 "Unprivileged processes may see subjects/objects with different real gid");
1401 * Determine if u1 can "see" the subject specified by u2, according to the
1402 * 'see_other_gids' policy.
1403 * Returns: 0 for permitted, ESRCH otherwise
1405 * References: *u1 and *u2 must not change during the call
1406 * u1 may equal u2, in which case only one reference is required
1409 cr_canseeothergids(struct ucred *u1, struct ucred *u2)
1411 if (!see_other_gids) {
1412 if (realgroupmember(u1->cr_rgid, u2))
1415 for (int i = 1; i < u1->cr_ngroups; i++)
1416 if (realgroupmember(u1->cr_groups[i], u2))
1419 if (priv_check_cred(u1, PRIV_SEEOTHERGIDS) != 0)
1427 * 'see_jail_proc' determines whether or not visibility of processes and
1428 * sockets with credentials holding different jail ids is possible using a
1429 * variety of system MIBs.
1431 * XXX: data declarations should be together near the beginning of the file.
1434 static int see_jail_proc = 1;
1435 SYSCTL_INT(_security_bsd, OID_AUTO, see_jail_proc, CTLFLAG_RW,
1437 "Unprivileged processes may see subjects/objects with different jail ids");
1440 * Determine if u1 "can see" the subject specified by u2, according to the
1441 * 'see_jail_proc' policy.
1442 * Returns: 0 for permitted, ESRCH otherwise
1444 * References: *u1 and *u2 must not change during the call
1445 * u1 may equal u2, in which case only one reference is required
1448 cr_canseejailproc(struct ucred *u1, struct ucred *u2)
1450 if (see_jail_proc || /* Policy deactivated. */
1451 u1->cr_prison == u2->cr_prison || /* Same jail. */
1452 priv_check_cred(u1, PRIV_SEEJAILPROC) == 0) /* Privileged. */
1459 * Helper for cr_cansee*() functions to abide by system-wide security.bsd.see_*
1460 * policies. Determines if u1 "can see" u2 according to these policies.
1461 * Returns: 0 for permitted, ESRCH otherwise
1464 cr_bsd_visible(struct ucred *u1, struct ucred *u2)
1468 if ((error = cr_canseeotheruids(u1, u2)))
1470 if ((error = cr_canseeothergids(u1, u2)))
1472 if ((error = cr_canseejailproc(u1, u2)))
1478 * Determine if u1 "can see" the subject specified by u2.
1479 * Returns: 0 for permitted, an errno value otherwise
1481 * References: *u1 and *u2 must not change during the call
1482 * u1 may equal u2, in which case only one reference is required
1485 cr_cansee(struct ucred *u1, struct ucred *u2)
1489 if ((error = prison_check(u1, u2)))
1492 if ((error = mac_cred_check_visible(u1, u2)))
1495 if ((error = cr_bsd_visible(u1, u2)))
1501 * Determine if td "can see" the subject specified by p.
1502 * Returns: 0 for permitted, an errno value otherwise
1503 * Locks: Sufficient locks to protect p->p_ucred must be held. td really
1504 * should be curthread.
1505 * References: td and p must be valid for the lifetime of the call
1508 p_cansee(struct thread *td, struct proc *p)
1510 /* Wrap cr_cansee() for all functionality. */
1511 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1512 PROC_LOCK_ASSERT(p, MA_OWNED);
1514 if (td->td_proc == p)
1516 return (cr_cansee(td->td_ucred, p->p_ucred));
1520 * 'conservative_signals' prevents the delivery of a broad class of
1521 * signals by unprivileged processes to processes that have changed their
1522 * credentials since the last invocation of execve(). This can prevent
1523 * the leakage of cached information or retained privileges as a result
1524 * of a common class of signal-related vulnerabilities. However, this
1525 * may interfere with some applications that expect to be able to
1526 * deliver these signals to peer processes after having given up
1529 static int conservative_signals = 1;
1530 SYSCTL_INT(_security_bsd, OID_AUTO, conservative_signals, CTLFLAG_RW,
1531 &conservative_signals, 0, "Unprivileged processes prevented from "
1532 "sending certain signals to processes whose credentials have changed");
1534 * Determine whether cred may deliver the specified signal to proc.
1535 * Returns: 0 for permitted, an errno value otherwise.
1536 * Locks: A lock must be held for proc.
1537 * References: cred and proc must be valid for the lifetime of the call.
1540 cr_cansignal(struct ucred *cred, struct proc *proc, int signum)
1544 PROC_LOCK_ASSERT(proc, MA_OWNED);
1546 * Jail semantics limit the scope of signalling to proc in the
1547 * same jail as cred, if cred is in jail.
1549 error = prison_check(cred, proc->p_ucred);
1553 if ((error = mac_proc_check_signal(cred, proc, signum)))
1556 if ((error = cr_bsd_visible(cred, proc->p_ucred)))
1560 * UNIX signal semantics depend on the status of the P_SUGID
1561 * bit on the target process. If the bit is set, then additional
1562 * restrictions are placed on the set of available signals.
1564 if (conservative_signals && (proc->p_flag & P_SUGID)) {
1579 * Generally, permit job and terminal control
1584 /* Not permitted without privilege. */
1585 error = priv_check_cred(cred, PRIV_SIGNAL_SUGID);
1592 * Generally, the target credential's ruid or svuid must match the
1593 * subject credential's ruid or euid.
1595 if (cred->cr_ruid != proc->p_ucred->cr_ruid &&
1596 cred->cr_ruid != proc->p_ucred->cr_svuid &&
1597 cred->cr_uid != proc->p_ucred->cr_ruid &&
1598 cred->cr_uid != proc->p_ucred->cr_svuid) {
1599 error = priv_check_cred(cred, PRIV_SIGNAL_DIFFCRED);
1608 * Determine whether td may deliver the specified signal to p.
1609 * Returns: 0 for permitted, an errno value otherwise
1610 * Locks: Sufficient locks to protect various components of td and p
1611 * must be held. td must be curthread, and a lock must be
1613 * References: td and p must be valid for the lifetime of the call
1616 p_cansignal(struct thread *td, struct proc *p, int signum)
1619 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1620 PROC_LOCK_ASSERT(p, MA_OWNED);
1621 if (td->td_proc == p)
1625 * UNIX signalling semantics require that processes in the same
1626 * session always be able to deliver SIGCONT to one another,
1627 * overriding the remaining protections.
1629 /* XXX: This will require an additional lock of some sort. */
1630 if (signum == SIGCONT && td->td_proc->p_session == p->p_session)
1633 * Some compat layers use SIGTHR and higher signals for
1634 * communication between different kernel threads of the same
1635 * process, so that they expect that it's always possible to
1636 * deliver them, even for suid applications where cr_cansignal() can
1637 * deny such ability for security consideration. It should be
1638 * pretty safe to do since the only way to create two processes
1639 * with the same p_leader is via rfork(2).
1641 if (td->td_proc->p_leader != NULL && signum >= SIGTHR &&
1642 signum < SIGTHR + 4 && td->td_proc->p_leader == p->p_leader)
1645 return (cr_cansignal(td->td_ucred, p, signum));
1649 * Determine whether td may reschedule p.
1650 * Returns: 0 for permitted, an errno value otherwise
1651 * Locks: Sufficient locks to protect various components of td and p
1652 * must be held. td must be curthread, and a lock must
1654 * References: td and p must be valid for the lifetime of the call
1657 p_cansched(struct thread *td, struct proc *p)
1661 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1662 PROC_LOCK_ASSERT(p, MA_OWNED);
1663 if (td->td_proc == p)
1665 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1668 if ((error = mac_proc_check_sched(td->td_ucred, p)))
1671 if ((error = cr_bsd_visible(td->td_ucred, p->p_ucred)))
1674 if (td->td_ucred->cr_ruid != p->p_ucred->cr_ruid &&
1675 td->td_ucred->cr_uid != p->p_ucred->cr_ruid) {
1676 error = priv_check(td, PRIV_SCHED_DIFFCRED);
1684 * Handle getting or setting the prison's unprivileged_proc_debug
1688 sysctl_unprivileged_proc_debug(SYSCTL_HANDLER_ARGS)
1692 val = prison_allow(req->td->td_ucred, PR_ALLOW_UNPRIV_DEBUG);
1693 error = sysctl_handle_int(oidp, &val, 0, req);
1694 if (error != 0 || req->newptr == NULL)
1696 if (val != 0 && val != 1)
1698 prison_set_allow(req->td->td_ucred, PR_ALLOW_UNPRIV_DEBUG, val);
1703 * The 'unprivileged_proc_debug' flag may be used to disable a variety of
1704 * unprivileged inter-process debugging services, including some procfs
1705 * functionality, ptrace(), and ktrace(). In the past, inter-process
1706 * debugging has been involved in a variety of security problems, and sites
1707 * not requiring the service might choose to disable it when hardening
1710 SYSCTL_PROC(_security_bsd, OID_AUTO, unprivileged_proc_debug,
1711 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_SECURE |
1712 CTLFLAG_MPSAFE, 0, 0, sysctl_unprivileged_proc_debug, "I",
1713 "Unprivileged processes may use process debugging facilities");
1716 * Determine whether td may debug p.
1717 * Returns: 0 for permitted, an errno value otherwise
1718 * Locks: Sufficient locks to protect various components of td and p
1719 * must be held. td must be curthread, and a lock must
1721 * References: td and p must be valid for the lifetime of the call
1724 p_candebug(struct thread *td, struct proc *p)
1726 int error, grpsubset, i, uidsubset;
1728 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1729 PROC_LOCK_ASSERT(p, MA_OWNED);
1730 if (td->td_proc == p)
1732 if ((error = priv_check(td, PRIV_DEBUG_UNPRIV)))
1734 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1737 if ((error = mac_proc_check_debug(td->td_ucred, p)))
1740 if ((error = cr_bsd_visible(td->td_ucred, p->p_ucred)))
1744 * Is p's group set a subset of td's effective group set? This
1745 * includes p's egid, group access list, rgid, and svgid.
1748 for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
1749 if (!groupmember(p->p_ucred->cr_groups[i], td->td_ucred)) {
1754 grpsubset = grpsubset &&
1755 groupmember(p->p_ucred->cr_rgid, td->td_ucred) &&
1756 groupmember(p->p_ucred->cr_svgid, td->td_ucred);
1759 * Are the uids present in p's credential equal to td's
1760 * effective uid? This includes p's euid, svuid, and ruid.
1762 uidsubset = (td->td_ucred->cr_uid == p->p_ucred->cr_uid &&
1763 td->td_ucred->cr_uid == p->p_ucred->cr_svuid &&
1764 td->td_ucred->cr_uid == p->p_ucred->cr_ruid);
1767 * If p's gids aren't a subset, or the uids aren't a subset,
1768 * or the credential has changed, require appropriate privilege
1769 * for td to debug p.
1771 if (!grpsubset || !uidsubset) {
1772 error = priv_check(td, PRIV_DEBUG_DIFFCRED);
1778 * Has the credential of the process changed since the last exec()?
1780 if ((p->p_flag & P_SUGID) != 0) {
1781 error = priv_check(td, PRIV_DEBUG_SUGID);
1786 /* Can't trace init when securelevel > 0. */
1787 if (p == initproc) {
1788 error = securelevel_gt(td->td_ucred, 0);
1794 * Can't trace a process that's currently exec'ing.
1796 * XXX: Note, this is not a security policy decision, it's a
1797 * basic correctness/functionality decision. Therefore, this check
1798 * should be moved to the caller's of p_candebug().
1800 if ((p->p_flag & P_INEXEC) != 0)
1803 /* Denied explicitly */
1804 if ((p->p_flag2 & P2_NOTRACE) != 0) {
1805 error = priv_check(td, PRIV_DEBUG_DENIED);
1814 * Determine whether the subject represented by cred can "see" a socket.
1815 * Returns: 0 for permitted, ENOENT otherwise.
1818 cr_canseesocket(struct ucred *cred, struct socket *so)
1822 error = prison_check(cred, so->so_cred);
1826 error = mac_socket_check_visible(cred, so);
1830 if (cr_bsd_visible(cred, so->so_cred))
1837 * Determine whether td can wait for the exit of p.
1838 * Returns: 0 for permitted, an errno value otherwise
1839 * Locks: Sufficient locks to protect various components of td and p
1840 * must be held. td must be curthread, and a lock must
1842 * References: td and p must be valid for the lifetime of the call
1846 p_canwait(struct thread *td, struct proc *p)
1850 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1851 PROC_LOCK_ASSERT(p, MA_OWNED);
1852 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1855 if ((error = mac_proc_check_wait(td->td_ucred, p)))
1859 /* XXXMAC: This could have odd effects on some shells. */
1860 if ((error = cr_bsd_visible(td->td_ucred, p->p_ucred)))
1868 * Credential management.
1870 * struct ucred objects are rarely allocated but gain and lose references all
1871 * the time (e.g., on struct file alloc/dealloc) turning refcount updates into
1872 * a significant source of cache-line ping ponging. Common cases are worked
1873 * around by modifying thread-local counter instead if the cred to operate on
1874 * matches td_realucred.
1876 * The counter is split into 2 parts:
1877 * - cr_users -- total count of all struct proc and struct thread objects
1878 * which have given cred in p_ucred and td_ucred respectively
1879 * - cr_ref -- the actual ref count, only valid if cr_users == 0
1881 * If users == 0 then cr_ref behaves similarly to refcount(9), in particular if
1882 * the count reaches 0 the object is freeable.
1883 * If users > 0 and curthread->td_realucred == cred, then updates are performed
1884 * against td_ucredref.
1885 * In other cases updates are performed against cr_ref.
1887 * Changing td_realucred into something else decrements cr_users and transfers
1888 * accumulated updates.
1891 crcowget(struct ucred *cr)
1894 mtx_lock(&cr->cr_mtx);
1895 KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
1896 __func__, cr->cr_users, cr));
1899 mtx_unlock(&cr->cr_mtx);
1903 static struct ucred *
1904 crunuse(struct thread *td)
1906 struct ucred *cr, *crold;
1908 MPASS(td->td_realucred == td->td_ucred);
1909 cr = td->td_realucred;
1910 mtx_lock(&cr->cr_mtx);
1911 cr->cr_ref += td->td_ucredref;
1912 td->td_ucredref = 0;
1913 KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
1914 __func__, cr->cr_users, cr));
1916 if (cr->cr_users == 0) {
1917 KASSERT(cr->cr_ref > 0, ("%s: ref %ld not > 0 on cred %p",
1918 __func__, cr->cr_ref, cr));
1924 mtx_unlock(&cr->cr_mtx);
1925 td->td_realucred = NULL;
1930 crunusebatch(struct ucred *cr, int users, int ref)
1933 KASSERT(users > 0, ("%s: passed users %d not > 0 ; cred %p",
1934 __func__, users, cr));
1935 mtx_lock(&cr->cr_mtx);
1936 KASSERT(cr->cr_users >= users, ("%s: users %d not > %d on cred %p",
1937 __func__, cr->cr_users, users, cr));
1938 cr->cr_users -= users;
1940 cr->cr_ref -= users;
1941 if (cr->cr_users > 0) {
1942 mtx_unlock(&cr->cr_mtx);
1945 KASSERT(cr->cr_ref >= 0, ("%s: ref %ld not >= 0 on cred %p",
1946 __func__, cr->cr_ref, cr));
1947 if (cr->cr_ref > 0) {
1948 mtx_unlock(&cr->cr_mtx);
1955 crcowfree(struct thread *td)
1969 struct ucred *crnew, *crold;
1973 PROC_LOCK_ASSERT(p, MA_OWNED);
1975 MPASS(td->td_realucred == td->td_ucred);
1976 if (td->td_realucred == p->p_ucred)
1979 crnew = crcowget(p->p_ucred);
1980 crold = crunuse(td);
1981 td->td_realucred = crnew;
1982 td->td_ucred = td->td_realucred;
1990 credbatch_add(struct credbatch *crb, struct thread *td)
1994 MPASS(td->td_realucred != NULL);
1995 MPASS(td->td_realucred == td->td_ucred);
1996 MPASS(TD_GET_STATE(td) == TDS_INACTIVE);
1997 cr = td->td_realucred;
1998 KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
1999 __func__, cr->cr_users, cr));
2000 if (crb->cred != cr) {
2001 if (crb->users > 0) {
2002 MPASS(crb->cred != NULL);
2003 crunusebatch(crb->cred, crb->users, crb->ref);
2010 crb->ref += td->td_ucredref;
2011 td->td_ucredref = 0;
2012 td->td_realucred = NULL;
2016 credbatch_final(struct credbatch *crb)
2019 MPASS(crb->cred != NULL);
2020 MPASS(crb->users > 0);
2021 crunusebatch(crb->cred, crb->users, crb->ref);
2025 * Allocate a zeroed cred structure.
2032 cr = malloc(sizeof(*cr), M_CRED, M_WAITOK | M_ZERO);
2033 mtx_init(&cr->cr_mtx, "cred", NULL, MTX_DEF);
2036 audit_cred_init(cr);
2041 cr->cr_groups = cr->cr_smallgroups;
2043 sizeof(cr->cr_smallgroups) / sizeof(cr->cr_smallgroups[0]);
2048 * Claim another reference to a ucred structure.
2051 crhold(struct ucred *cr)
2056 if (__predict_true(td->td_realucred == cr)) {
2057 KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
2058 __func__, cr->cr_users, cr));
2062 mtx_lock(&cr->cr_mtx);
2064 mtx_unlock(&cr->cr_mtx);
2069 * Free a cred structure. Throws away space when ref count gets to 0.
2072 crfree(struct ucred *cr)
2077 if (__predict_true(td->td_realucred == cr)) {
2078 KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
2079 __func__, cr->cr_users, cr));
2083 mtx_lock(&cr->cr_mtx);
2084 KASSERT(cr->cr_users >= 0, ("%s: users %d not >= 0 on cred %p",
2085 __func__, cr->cr_users, cr));
2087 if (cr->cr_users > 0) {
2088 mtx_unlock(&cr->cr_mtx);
2091 KASSERT(cr->cr_ref >= 0, ("%s: ref %ld not >= 0 on cred %p",
2092 __func__, cr->cr_ref, cr));
2093 if (cr->cr_ref > 0) {
2094 mtx_unlock(&cr->cr_mtx);
2101 crfree_final(struct ucred *cr)
2104 KASSERT(cr->cr_users == 0, ("%s: users %d not == 0 on cred %p",
2105 __func__, cr->cr_users, cr));
2106 KASSERT(cr->cr_ref == 0, ("%s: ref %ld not == 0 on cred %p",
2107 __func__, cr->cr_ref, cr));
2110 * Some callers of crget(), such as nfs_statfs(), allocate a temporary
2111 * credential, but don't allocate a uidinfo structure.
2113 if (cr->cr_uidinfo != NULL)
2114 uifree(cr->cr_uidinfo);
2115 if (cr->cr_ruidinfo != NULL)
2116 uifree(cr->cr_ruidinfo);
2117 if (cr->cr_prison != NULL)
2118 prison_free(cr->cr_prison);
2119 if (cr->cr_loginclass != NULL)
2120 loginclass_free(cr->cr_loginclass);
2122 audit_cred_destroy(cr);
2125 mac_cred_destroy(cr);
2127 mtx_destroy(&cr->cr_mtx);
2128 if (cr->cr_groups != cr->cr_smallgroups)
2129 free(cr->cr_groups, M_CRED);
2134 * Copy a ucred's contents from a template. Does not block.
2137 crcopy(struct ucred *dest, struct ucred *src)
2140 KASSERT(dest->cr_ref == 1, ("crcopy of shared ucred"));
2141 bcopy(&src->cr_startcopy, &dest->cr_startcopy,
2142 (unsigned)((caddr_t)&src->cr_endcopy -
2143 (caddr_t)&src->cr_startcopy));
2144 dest->cr_flags = src->cr_flags;
2145 crsetgroups(dest, src->cr_ngroups, src->cr_groups);
2146 uihold(dest->cr_uidinfo);
2147 uihold(dest->cr_ruidinfo);
2148 prison_hold(dest->cr_prison);
2149 loginclass_hold(dest->cr_loginclass);
2151 audit_cred_copy(src, dest);
2154 mac_cred_copy(src, dest);
2159 * Dup cred struct to a new held one.
2162 crdup(struct ucred *cr)
2164 struct ucred *newcr;
2172 * Fill in a struct xucred based on a struct ucred.
2175 cru2x(struct ucred *cr, struct xucred *xcr)
2179 bzero(xcr, sizeof(*xcr));
2180 xcr->cr_version = XUCRED_VERSION;
2181 xcr->cr_uid = cr->cr_uid;
2183 ngroups = MIN(cr->cr_ngroups, XU_NGROUPS);
2184 xcr->cr_ngroups = ngroups;
2185 bcopy(cr->cr_groups, xcr->cr_groups,
2186 ngroups * sizeof(*cr->cr_groups));
2190 cru2xt(struct thread *td, struct xucred *xcr)
2193 cru2x(td->td_ucred, xcr);
2194 xcr->cr_pid = td->td_proc->p_pid;
2198 * Set initial process credentials.
2199 * Callers are responsible for providing the reference for provided credentials.
2202 proc_set_cred_init(struct proc *p, struct ucred *newcred)
2205 p->p_ucred = crcowget(newcred);
2209 * Change process credentials.
2210 * Callers are responsible for providing the reference for passed credentials
2211 * and for freeing old ones.
2213 * Process has to be locked except when it does not have credentials (as it
2214 * should not be visible just yet) or when newcred is NULL (as this can be
2215 * only used when the process is about to be freed, at which point it should
2216 * not be visible anymore).
2219 proc_set_cred(struct proc *p, struct ucred *newcred)
2225 PROC_LOCK_ASSERT(p, MA_OWNED);
2226 KASSERT(newcred->cr_users == 0, ("%s: users %d not 0 on cred %p",
2227 __func__, newcred->cr_users, newcred));
2228 mtx_lock(&cr->cr_mtx);
2229 KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
2230 __func__, cr->cr_users, cr));
2232 mtx_unlock(&cr->cr_mtx);
2233 p->p_ucred = newcred;
2234 newcred->cr_users = 1;
2239 proc_unset_cred(struct proc *p)
2243 MPASS(p->p_state == PRS_ZOMBIE || p->p_state == PRS_NEW);
2246 KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
2247 __func__, cr->cr_users, cr));
2248 mtx_lock(&cr->cr_mtx);
2250 if (cr->cr_users == 0)
2251 KASSERT(cr->cr_ref > 0, ("%s: ref %ld not > 0 on cred %p",
2252 __func__, cr->cr_ref, cr));
2253 mtx_unlock(&cr->cr_mtx);
2258 crcopysafe(struct proc *p, struct ucred *cr)
2260 struct ucred *oldcred;
2263 PROC_LOCK_ASSERT(p, MA_OWNED);
2265 oldcred = p->p_ucred;
2266 while (cr->cr_agroups < oldcred->cr_agroups) {
2267 groups = oldcred->cr_agroups;
2269 crextend(cr, groups);
2271 oldcred = p->p_ucred;
2273 crcopy(cr, oldcred);
2279 * Extend the passed in credential to hold n items.
2282 crextend(struct ucred *cr, int n)
2287 if (n <= cr->cr_agroups)
2291 * We extend by 2 each time since we're using a power of two
2292 * allocator until we need enough groups to fill a page.
2293 * Once we're allocating multiple pages, only allocate as many
2294 * as we actually need. The case of processes needing a
2295 * non-power of two number of pages seems more likely than
2296 * a real world process that adds thousands of groups one at a
2299 if ( n < PAGE_SIZE / sizeof(gid_t) ) {
2300 if (cr->cr_agroups == 0)
2301 cnt = MAX(1, MINALLOCSIZE / sizeof(gid_t));
2303 cnt = cr->cr_agroups * 2;
2308 cnt = roundup2(n, PAGE_SIZE / sizeof(gid_t));
2310 /* Free the old array. */
2311 if (cr->cr_groups != cr->cr_smallgroups)
2312 free(cr->cr_groups, M_CRED);
2314 cr->cr_groups = malloc(cnt * sizeof(gid_t), M_CRED, M_WAITOK | M_ZERO);
2315 cr->cr_agroups = cnt;
2319 * Copy groups in to a credential, preserving any necessary invariants.
2320 * Currently this includes the sorting of all supplemental gids.
2321 * crextend() must have been called before hand to ensure sufficient
2322 * space is available.
2325 crsetgroups_locked(struct ucred *cr, int ngrp, gid_t *groups)
2331 KASSERT(cr->cr_agroups >= ngrp, ("cr_ngroups is too small"));
2333 bcopy(groups, cr->cr_groups, ngrp * sizeof(gid_t));
2334 cr->cr_ngroups = ngrp;
2337 * Sort all groups except cr_groups[0] to allow groupmember to
2338 * perform a binary search.
2340 * XXX: If large numbers of groups become common this should
2341 * be replaced with shell sort like linux uses or possibly
2344 for (i = 2; i < ngrp; i++) {
2345 g = cr->cr_groups[i];
2346 for (j = i-1; j >= 1 && g < cr->cr_groups[j]; j--)
2347 cr->cr_groups[j + 1] = cr->cr_groups[j];
2348 cr->cr_groups[j + 1] = g;
2353 * Copy groups in to a credential after expanding it if required.
2354 * Truncate the list to (ngroups_max + 1) if it is too large.
2357 crsetgroups(struct ucred *cr, int ngrp, gid_t *groups)
2360 if (ngrp > ngroups_max + 1)
2361 ngrp = ngroups_max + 1;
2364 crsetgroups_locked(cr, ngrp, groups);
2368 * Get login name, if available.
2370 #ifndef _SYS_SYSPROTO_H_
2371 struct getlogin_args {
2378 sys_getlogin(struct thread *td, struct getlogin_args *uap)
2380 char login[MAXLOGNAME];
2381 struct proc *p = td->td_proc;
2384 if (uap->namelen > MAXLOGNAME)
2385 uap->namelen = MAXLOGNAME;
2387 SESS_LOCK(p->p_session);
2388 len = strlcpy(login, p->p_session->s_login, uap->namelen) + 1;
2389 SESS_UNLOCK(p->p_session);
2391 if (len > uap->namelen)
2393 return (copyout(login, uap->namebuf, len));
2399 #ifndef _SYS_SYSPROTO_H_
2400 struct setlogin_args {
2406 sys_setlogin(struct thread *td, struct setlogin_args *uap)
2408 struct proc *p = td->td_proc;
2410 char logintmp[MAXLOGNAME];
2412 CTASSERT(sizeof(p->p_session->s_login) >= sizeof(logintmp));
2414 error = priv_check(td, PRIV_PROC_SETLOGIN);
2417 error = copyinstr(uap->namebuf, logintmp, sizeof(logintmp), NULL);
2419 if (error == ENAMETOOLONG)
2423 AUDIT_ARG_LOGIN(logintmp);
2425 SESS_LOCK(p->p_session);
2426 strcpy(p->p_session->s_login, logintmp);
2427 SESS_UNLOCK(p->p_session);
2433 setsugid(struct proc *p)
2436 PROC_LOCK_ASSERT(p, MA_OWNED);
2437 p->p_flag |= P_SUGID;
2441 * Change a process's effective uid.
2442 * Side effects: newcred->cr_uid and newcred->cr_uidinfo will be modified.
2443 * References: newcred must be an exclusive credential reference for the
2444 * duration of the call.
2447 change_euid(struct ucred *newcred, struct uidinfo *euip)
2450 newcred->cr_uid = euip->ui_uid;
2452 uifree(newcred->cr_uidinfo);
2453 newcred->cr_uidinfo = euip;
2457 * Change a process's effective gid.
2458 * Side effects: newcred->cr_gid will be modified.
2459 * References: newcred must be an exclusive credential reference for the
2460 * duration of the call.
2463 change_egid(struct ucred *newcred, gid_t egid)
2466 newcred->cr_groups[0] = egid;
2470 * Change a process's real uid.
2471 * Side effects: newcred->cr_ruid will be updated, newcred->cr_ruidinfo
2472 * will be updated, and the old and new cr_ruidinfo proc
2473 * counts will be updated.
2474 * References: newcred must be an exclusive credential reference for the
2475 * duration of the call.
2478 change_ruid(struct ucred *newcred, struct uidinfo *ruip)
2481 (void)chgproccnt(newcred->cr_ruidinfo, -1, 0);
2482 newcred->cr_ruid = ruip->ui_uid;
2484 uifree(newcred->cr_ruidinfo);
2485 newcred->cr_ruidinfo = ruip;
2486 (void)chgproccnt(newcred->cr_ruidinfo, 1, 0);
2490 * Change a process's real gid.
2491 * Side effects: newcred->cr_rgid will be updated.
2492 * References: newcred must be an exclusive credential reference for the
2493 * duration of the call.
2496 change_rgid(struct ucred *newcred, gid_t rgid)
2499 newcred->cr_rgid = rgid;
2503 * Change a process's saved uid.
2504 * Side effects: newcred->cr_svuid will be updated.
2505 * References: newcred must be an exclusive credential reference for the
2506 * duration of the call.
2509 change_svuid(struct ucred *newcred, uid_t svuid)
2512 newcred->cr_svuid = svuid;
2516 * Change a process's saved gid.
2517 * Side effects: newcred->cr_svgid will be updated.
2518 * References: newcred must be an exclusive credential reference for the
2519 * duration of the call.
2522 change_svgid(struct ucred *newcred, gid_t svgid)
2525 newcred->cr_svgid = svgid;
2528 bool allow_ptrace = true;
2529 SYSCTL_BOOL(_security_bsd, OID_AUTO, allow_ptrace, CTLFLAG_RWTUN,
2531 "Deny ptrace(2) use by returning ENOSYS");