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>
47 __FBSDID("$FreeBSD$");
50 #include "opt_inet6.h"
52 #include <sys/param.h>
53 #include <sys/systm.h>
56 #include <sys/kernel.h>
58 #include <sys/loginclass.h>
59 #include <sys/malloc.h>
60 #include <sys/mutex.h>
61 #include <sys/ptrace.h>
62 #include <sys/refcount.h>
67 #include <sys/sysent.h>
69 #include <sys/sysproto.h>
71 #include <sys/racct.h>
73 #include <sys/resourcevar.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
76 #include <sys/syscallsubr.h>
77 #include <sys/sysctl.h>
81 "Kernel support for interfaces necessary for regression testing (SECURITY RISK!)");
84 #include <security/audit/audit.h>
85 #include <security/mac/mac_framework.h>
87 static MALLOC_DEFINE(M_CRED, "cred", "credentials");
89 SYSCTL_NODE(_security, OID_AUTO, bsd, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
90 "BSD security policy");
92 static void crfree_final(struct ucred *cr);
93 static void crsetgroups_locked(struct ucred *cr, int ngrp,
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;
338 newpgrp = uma_zalloc(pgrp_zone, M_WAITOK);
339 newsess = malloc(sizeof(struct session), M_SESSION, M_WAITOK | M_ZERO);
341 sx_xlock(&proctree_lock);
343 if (p->p_pgid == p->p_pid || (pgrp = pgfind(p->p_pid)) != NULL) {
348 (void)enterpgrp(p, p->p_pid, newpgrp, newsess);
349 td->td_retval[0] = p->p_pid;
354 sx_xunlock(&proctree_lock);
356 uma_zfree(pgrp_zone, newpgrp);
357 free(newsess, M_SESSION);
363 * set process group (setpgid/old setpgrp)
365 * caller does setpgid(targpid, targpgid)
367 * pid must be caller or child of caller (ESRCH)
369 * pid must be in same session (EPERM)
370 * pid can't have done an exec (EACCES)
372 * there must exist some pid in same session having pgid (EPERM)
373 * pid must not be session leader (EPERM)
375 #ifndef _SYS_SYSPROTO_H_
376 struct setpgid_args {
377 int pid; /* target process id */
378 int pgid; /* target pgrp id */
383 sys_setpgid(struct thread *td, struct setpgid_args *uap)
385 struct proc *curp = td->td_proc;
386 struct proc *targp; /* target process */
387 struct pgrp *pgrp; /* target pgrp */
389 struct pgrp *newpgrp;
396 newpgrp = uma_zalloc(pgrp_zone, M_WAITOK);
398 sx_xlock(&proctree_lock);
399 if (uap->pid != 0 && uap->pid != curp->p_pid) {
400 if ((targp = pfind(uap->pid)) == NULL) {
404 if (!inferior(targp)) {
409 if ((error = p_cansee(td, targp))) {
413 if (targp->p_pgrp == NULL ||
414 targp->p_session != curp->p_session) {
419 if (targp->p_flag & P_EXEC) {
427 if (SESS_LEADER(targp)) {
432 uap->pgid = targp->p_pid;
433 if ((pgrp = pgfind(uap->pgid)) == NULL) {
434 if (uap->pgid == targp->p_pid) {
435 error = enterpgrp(targp, uap->pgid, newpgrp,
442 if (pgrp == targp->p_pgrp) {
446 if (pgrp->pg_id != targp->p_pid &&
447 pgrp->pg_session != curp->p_session) {
453 error = enterthispgrp(targp, pgrp);
456 sx_xunlock(&proctree_lock);
457 KASSERT((error == 0) || (newpgrp != NULL),
458 ("setpgid failed and newpgrp is NULL"));
459 uma_zfree(pgrp_zone, newpgrp);
464 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
465 * compatible. It says that setting the uid/gid to euid/egid is a special
466 * case of "appropriate privilege". Once the rules are expanded out, this
467 * basically means that setuid(nnn) sets all three id's, in all permitted
468 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
469 * does not set the saved id - this is dangerous for traditional BSD
470 * programs. For this reason, we *really* do not want to set
471 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
473 #define POSIX_APPENDIX_B_4_2_2
475 #ifndef _SYS_SYSPROTO_H_
482 sys_setuid(struct thread *td, struct setuid_args *uap)
484 struct proc *p = td->td_proc;
485 struct ucred *newcred, *oldcred;
496 * Copy credentials so other references do not see our changes.
498 oldcred = crcopysafe(p, newcred);
501 error = mac_cred_check_setuid(oldcred, uid);
507 * See if we have "permission" by POSIX 1003.1 rules.
509 * Note that setuid(geteuid()) is a special case of
510 * "appropriate privileges" in appendix B.4.2.2. We need
511 * to use this clause to be compatible with traditional BSD
512 * semantics. Basically, it means that "setuid(xx)" sets all
513 * three id's (assuming you have privs).
515 * Notes on the logic. We do things in three steps.
516 * 1: We determine if the euid is going to change, and do EPERM
517 * right away. We unconditionally change the euid later if this
518 * test is satisfied, simplifying that part of the logic.
519 * 2: We determine if the real and/or saved uids are going to
520 * change. Determined by compile options.
521 * 3: Change euid last. (after tests in #2 for "appropriate privs")
523 if (uid != oldcred->cr_ruid && /* allow setuid(getuid()) */
524 #ifdef _POSIX_SAVED_IDS
525 uid != oldcred->cr_svuid && /* allow setuid(saved gid) */
527 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
528 uid != oldcred->cr_uid && /* allow setuid(geteuid()) */
530 (error = priv_check_cred(oldcred, PRIV_CRED_SETUID)) != 0)
533 #ifdef _POSIX_SAVED_IDS
535 * Do we have "appropriate privileges" (are we root or uid == euid)
536 * If so, we are changing the real uid and/or saved uid.
539 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
540 uid == oldcred->cr_uid ||
542 /* We are using privs. */
543 priv_check_cred(oldcred, PRIV_CRED_SETUID) == 0)
547 * Set the real uid and transfer proc count to new user.
549 if (uid != oldcred->cr_ruid) {
550 change_ruid(newcred, uip);
556 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
557 * the security of seteuid() depends on it. B.4.2.2 says it
558 * is important that we should do this.
560 if (uid != oldcred->cr_svuid) {
561 change_svuid(newcred, uid);
567 * In all permitted cases, we are changing the euid.
569 if (uid != oldcred->cr_uid) {
570 change_euid(newcred, uip);
573 proc_set_cred(p, newcred);
575 racct_proc_ucred_changed(p, oldcred, newcred);
580 rctl_proc_ucred_changed(p, newcred);
594 #ifndef _SYS_SYSPROTO_H_
595 struct seteuid_args {
601 sys_seteuid(struct thread *td, struct seteuid_args *uap)
603 struct proc *p = td->td_proc;
604 struct ucred *newcred, *oldcred;
606 struct uidinfo *euip;
610 AUDIT_ARG_EUID(euid);
615 * Copy credentials so other references do not see our changes.
617 oldcred = crcopysafe(p, newcred);
620 error = mac_cred_check_seteuid(oldcred, euid);
625 if (euid != oldcred->cr_ruid && /* allow seteuid(getuid()) */
626 euid != oldcred->cr_svuid && /* allow seteuid(saved uid) */
627 (error = priv_check_cred(oldcred, PRIV_CRED_SETEUID)) != 0)
631 * Everything's okay, do it.
633 if (oldcred->cr_uid != euid) {
634 change_euid(newcred, euip);
637 proc_set_cred(p, newcred);
650 #ifndef _SYS_SYSPROTO_H_
657 sys_setgid(struct thread *td, struct setgid_args *uap)
659 struct proc *p = td->td_proc;
660 struct ucred *newcred, *oldcred;
668 oldcred = crcopysafe(p, newcred);
671 error = mac_cred_check_setgid(oldcred, gid);
677 * See if we have "permission" by POSIX 1003.1 rules.
679 * Note that setgid(getegid()) is a special case of
680 * "appropriate privileges" in appendix B.4.2.2. We need
681 * to use this clause to be compatible with traditional BSD
682 * semantics. Basically, it means that "setgid(xx)" sets all
683 * three id's (assuming you have privs).
685 * For notes on the logic here, see setuid() above.
687 if (gid != oldcred->cr_rgid && /* allow setgid(getgid()) */
688 #ifdef _POSIX_SAVED_IDS
689 gid != oldcred->cr_svgid && /* allow setgid(saved gid) */
691 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
692 gid != oldcred->cr_groups[0] && /* allow setgid(getegid()) */
694 (error = priv_check_cred(oldcred, PRIV_CRED_SETGID)) != 0)
697 #ifdef _POSIX_SAVED_IDS
699 * Do we have "appropriate privileges" (are we root or gid == egid)
700 * If so, we are changing the real uid and saved gid.
703 #ifdef POSIX_APPENDIX_B_4_2_2 /* use the clause from B.4.2.2 */
704 gid == oldcred->cr_groups[0] ||
706 /* We are using privs. */
707 priv_check_cred(oldcred, PRIV_CRED_SETGID) == 0)
713 if (oldcred->cr_rgid != gid) {
714 change_rgid(newcred, gid);
720 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
721 * the security of setegid() depends on it. B.4.2.2 says it
722 * is important that we should do this.
724 if (oldcred->cr_svgid != gid) {
725 change_svgid(newcred, gid);
730 * In all cases permitted cases, we are changing the egid.
731 * Copy credentials so other references do not see our changes.
733 if (oldcred->cr_groups[0] != gid) {
734 change_egid(newcred, gid);
737 proc_set_cred(p, newcred);
748 #ifndef _SYS_SYSPROTO_H_
749 struct setegid_args {
755 sys_setegid(struct thread *td, struct setegid_args *uap)
757 struct proc *p = td->td_proc;
758 struct ucred *newcred, *oldcred;
763 AUDIT_ARG_EGID(egid);
766 oldcred = crcopysafe(p, newcred);
769 error = mac_cred_check_setegid(oldcred, egid);
774 if (egid != oldcred->cr_rgid && /* allow setegid(getgid()) */
775 egid != oldcred->cr_svgid && /* allow setegid(saved gid) */
776 (error = priv_check_cred(oldcred, PRIV_CRED_SETEGID)) != 0)
779 if (oldcred->cr_groups[0] != egid) {
780 change_egid(newcred, egid);
783 proc_set_cred(p, newcred);
794 #ifndef _SYS_SYSPROTO_H_
795 struct setgroups_args {
802 sys_setgroups(struct thread *td, struct setgroups_args *uap)
804 gid_t smallgroups[XU_NGROUPS];
806 int gidsetsize, error;
808 gidsetsize = uap->gidsetsize;
809 if (gidsetsize > ngroups_max + 1 || gidsetsize < 0)
812 if (gidsetsize > XU_NGROUPS)
813 groups = malloc(gidsetsize * sizeof(gid_t), M_TEMP, M_WAITOK);
815 groups = smallgroups;
817 error = copyin(uap->gidset, groups, gidsetsize * sizeof(gid_t));
819 error = kern_setgroups(td, gidsetsize, groups);
821 if (gidsetsize > XU_NGROUPS)
822 free(groups, M_TEMP);
827 kern_setgroups(struct thread *td, u_int ngrp, gid_t *groups)
829 struct proc *p = td->td_proc;
830 struct ucred *newcred, *oldcred;
833 MPASS(ngrp <= ngroups_max + 1);
834 AUDIT_ARG_GROUPSET(groups, ngrp);
836 crextend(newcred, ngrp);
838 oldcred = crcopysafe(p, newcred);
841 error = mac_cred_check_setgroups(oldcred, ngrp, groups);
846 error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS);
852 * setgroups(0, NULL) is a legitimate way of clearing the
853 * groups vector on non-BSD systems (which generally do not
854 * have the egid in the groups[0]). We risk security holes
855 * when running non-BSD software if we do not do the same.
857 newcred->cr_ngroups = 1;
859 crsetgroups_locked(newcred, ngrp, groups);
862 proc_set_cred(p, newcred);
873 #ifndef _SYS_SYSPROTO_H_
874 struct setreuid_args {
881 sys_setreuid(struct thread *td, struct setreuid_args *uap)
883 struct proc *p = td->td_proc;
884 struct ucred *newcred, *oldcred;
886 struct uidinfo *euip, *ruip;
891 AUDIT_ARG_EUID(euid);
892 AUDIT_ARG_RUID(ruid);
897 oldcred = crcopysafe(p, newcred);
900 error = mac_cred_check_setreuid(oldcred, ruid, euid);
905 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
906 ruid != oldcred->cr_svuid) ||
907 (euid != (uid_t)-1 && euid != oldcred->cr_uid &&
908 euid != oldcred->cr_ruid && euid != oldcred->cr_svuid)) &&
909 (error = priv_check_cred(oldcred, PRIV_CRED_SETREUID)) != 0)
912 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
913 change_euid(newcred, euip);
916 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
917 change_ruid(newcred, ruip);
920 if ((ruid != (uid_t)-1 || newcred->cr_uid != newcred->cr_ruid) &&
921 newcred->cr_svuid != newcred->cr_uid) {
922 change_svuid(newcred, newcred->cr_uid);
925 proc_set_cred(p, newcred);
927 racct_proc_ucred_changed(p, oldcred, newcred);
932 rctl_proc_ucred_changed(p, newcred);
948 #ifndef _SYS_SYSPROTO_H_
949 struct setregid_args {
956 sys_setregid(struct thread *td, struct setregid_args *uap)
958 struct proc *p = td->td_proc;
959 struct ucred *newcred, *oldcred;
965 AUDIT_ARG_EGID(egid);
966 AUDIT_ARG_RGID(rgid);
969 oldcred = crcopysafe(p, newcred);
972 error = mac_cred_check_setregid(oldcred, rgid, egid);
977 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
978 rgid != oldcred->cr_svgid) ||
979 (egid != (gid_t)-1 && egid != oldcred->cr_groups[0] &&
980 egid != oldcred->cr_rgid && egid != oldcred->cr_svgid)) &&
981 (error = priv_check_cred(oldcred, PRIV_CRED_SETREGID)) != 0)
984 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
985 change_egid(newcred, egid);
988 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
989 change_rgid(newcred, rgid);
992 if ((rgid != (gid_t)-1 || newcred->cr_groups[0] != newcred->cr_rgid) &&
993 newcred->cr_svgid != newcred->cr_groups[0]) {
994 change_svgid(newcred, newcred->cr_groups[0]);
997 proc_set_cred(p, newcred);
1009 * setresuid(ruid, euid, suid) is like setreuid except control over the saved
1012 #ifndef _SYS_SYSPROTO_H_
1013 struct setresuid_args {
1021 sys_setresuid(struct thread *td, struct setresuid_args *uap)
1023 struct proc *p = td->td_proc;
1024 struct ucred *newcred, *oldcred;
1025 uid_t euid, ruid, suid;
1026 struct uidinfo *euip, *ruip;
1032 AUDIT_ARG_EUID(euid);
1033 AUDIT_ARG_RUID(ruid);
1034 AUDIT_ARG_SUID(suid);
1036 euip = uifind(euid);
1037 ruip = uifind(ruid);
1039 oldcred = crcopysafe(p, newcred);
1042 error = mac_cred_check_setresuid(oldcred, ruid, euid, suid);
1047 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
1048 ruid != oldcred->cr_svuid &&
1049 ruid != oldcred->cr_uid) ||
1050 (euid != (uid_t)-1 && euid != oldcred->cr_ruid &&
1051 euid != oldcred->cr_svuid &&
1052 euid != oldcred->cr_uid) ||
1053 (suid != (uid_t)-1 && suid != oldcred->cr_ruid &&
1054 suid != oldcred->cr_svuid &&
1055 suid != oldcred->cr_uid)) &&
1056 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESUID)) != 0)
1059 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
1060 change_euid(newcred, euip);
1063 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
1064 change_ruid(newcred, ruip);
1067 if (suid != (uid_t)-1 && oldcred->cr_svuid != suid) {
1068 change_svuid(newcred, suid);
1071 proc_set_cred(p, newcred);
1073 racct_proc_ucred_changed(p, oldcred, newcred);
1078 rctl_proc_ucred_changed(p, newcred);
1096 * setresgid(rgid, egid, sgid) is like setregid except control over the saved
1099 #ifndef _SYS_SYSPROTO_H_
1100 struct setresgid_args {
1108 sys_setresgid(struct thread *td, struct setresgid_args *uap)
1110 struct proc *p = td->td_proc;
1111 struct ucred *newcred, *oldcred;
1112 gid_t egid, rgid, sgid;
1118 AUDIT_ARG_EGID(egid);
1119 AUDIT_ARG_RGID(rgid);
1120 AUDIT_ARG_SGID(sgid);
1123 oldcred = crcopysafe(p, newcred);
1126 error = mac_cred_check_setresgid(oldcred, rgid, egid, sgid);
1131 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
1132 rgid != oldcred->cr_svgid &&
1133 rgid != oldcred->cr_groups[0]) ||
1134 (egid != (gid_t)-1 && egid != oldcred->cr_rgid &&
1135 egid != oldcred->cr_svgid &&
1136 egid != oldcred->cr_groups[0]) ||
1137 (sgid != (gid_t)-1 && sgid != oldcred->cr_rgid &&
1138 sgid != oldcred->cr_svgid &&
1139 sgid != oldcred->cr_groups[0])) &&
1140 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESGID)) != 0)
1143 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
1144 change_egid(newcred, egid);
1147 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
1148 change_rgid(newcred, rgid);
1151 if (sgid != (gid_t)-1 && oldcred->cr_svgid != sgid) {
1152 change_svgid(newcred, sgid);
1155 proc_set_cred(p, newcred);
1166 #ifndef _SYS_SYSPROTO_H_
1167 struct getresuid_args {
1175 sys_getresuid(struct thread *td, struct getresuid_args *uap)
1178 int error1 = 0, error2 = 0, error3 = 0;
1180 cred = td->td_ucred;
1182 error1 = copyout(&cred->cr_ruid,
1183 uap->ruid, sizeof(cred->cr_ruid));
1185 error2 = copyout(&cred->cr_uid,
1186 uap->euid, sizeof(cred->cr_uid));
1188 error3 = copyout(&cred->cr_svuid,
1189 uap->suid, sizeof(cred->cr_svuid));
1190 return (error1 ? error1 : error2 ? error2 : error3);
1193 #ifndef _SYS_SYSPROTO_H_
1194 struct getresgid_args {
1202 sys_getresgid(struct thread *td, struct getresgid_args *uap)
1205 int error1 = 0, error2 = 0, error3 = 0;
1207 cred = td->td_ucred;
1209 error1 = copyout(&cred->cr_rgid,
1210 uap->rgid, sizeof(cred->cr_rgid));
1212 error2 = copyout(&cred->cr_groups[0],
1213 uap->egid, sizeof(cred->cr_groups[0]));
1215 error3 = copyout(&cred->cr_svgid,
1216 uap->sgid, sizeof(cred->cr_svgid));
1217 return (error1 ? error1 : error2 ? error2 : error3);
1220 #ifndef _SYS_SYSPROTO_H_
1221 struct issetugid_args {
1227 sys_issetugid(struct thread *td, struct issetugid_args *uap)
1229 struct proc *p = td->td_proc;
1232 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
1233 * we use P_SUGID because we consider changing the owners as
1234 * "tainting" as well.
1235 * This is significant for procs that start as root and "become"
1236 * a user without an exec - programs cannot know *everything*
1237 * that libc *might* have put in their data segment.
1239 td->td_retval[0] = (p->p_flag & P_SUGID) ? 1 : 0;
1244 sys___setugid(struct thread *td, struct __setugid_args *uap)
1250 switch (uap->flag) {
1253 p->p_flag &= ~P_SUGID;
1258 p->p_flag |= P_SUGID;
1264 #else /* !REGRESSION */
1267 #endif /* REGRESSION */
1271 * Check if gid is a member of the group set.
1274 groupmember(gid_t gid, struct ucred *cred)
1280 if (cred->cr_groups[0] == gid)
1284 * If gid was not our primary group, perform a binary search
1285 * of the supplemental groups. This is possible because we
1286 * sort the groups in crsetgroups().
1289 h = cred->cr_ngroups;
1291 m = l + ((h - l) / 2);
1292 if (cred->cr_groups[m] < gid)
1297 if ((l < cred->cr_ngroups) && (cred->cr_groups[l] == gid))
1304 * Test the active securelevel against a given level. securelevel_gt()
1305 * implements (securelevel > level). securelevel_ge() implements
1306 * (securelevel >= level). Note that the logic is inverted -- these
1307 * functions return EPERM on "success" and 0 on "failure".
1309 * Due to care taken when setting the securelevel, we know that no jail will
1310 * be less secure that its parent (or the physical system), so it is sufficient
1311 * to test the current jail only.
1313 * XXXRW: Possibly since this has to do with privilege, it should move to
1317 securelevel_gt(struct ucred *cr, int level)
1320 return (cr->cr_prison->pr_securelevel > level ? EPERM : 0);
1324 securelevel_ge(struct ucred *cr, int level)
1327 return (cr->cr_prison->pr_securelevel >= level ? EPERM : 0);
1331 * 'see_other_uids' determines whether or not visibility of processes
1332 * and sockets with credentials holding different real uids is possible
1333 * using a variety of system MIBs.
1334 * XXX: data declarations should be together near the beginning of the file.
1336 static int see_other_uids = 1;
1337 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_uids, CTLFLAG_RW,
1339 "Unprivileged processes may see subjects/objects with different real uid");
1342 * Determine if u1 "can see" the subject specified by u2, according to the
1343 * 'see_other_uids' policy.
1344 * Returns: 0 for permitted, ESRCH otherwise
1346 * References: *u1 and *u2 must not change during the call
1347 * u1 may equal u2, in which case only one reference is required
1350 cr_canseeotheruids(struct ucred *u1, struct ucred *u2)
1353 if (!see_other_uids && u1->cr_ruid != u2->cr_ruid) {
1354 if (priv_check_cred(u1, PRIV_SEEOTHERUIDS) != 0)
1361 * 'see_other_gids' determines whether or not visibility of processes
1362 * and sockets with credentials holding different real gids is possible
1363 * using a variety of system MIBs.
1364 * XXX: data declarations should be together near the beginning of the file.
1366 static int see_other_gids = 1;
1367 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_gids, CTLFLAG_RW,
1369 "Unprivileged processes may see subjects/objects with different real gid");
1372 * Determine if u1 can "see" the subject specified by u2, according to the
1373 * 'see_other_gids' policy.
1374 * Returns: 0 for permitted, ESRCH otherwise
1376 * References: *u1 and *u2 must not change during the call
1377 * u1 may equal u2, in which case only one reference is required
1380 cr_canseeothergids(struct ucred *u1, struct ucred *u2)
1384 if (!see_other_gids) {
1386 for (i = 0; i < u1->cr_ngroups; i++) {
1387 if (groupmember(u1->cr_groups[i], u2))
1393 if (priv_check_cred(u1, PRIV_SEEOTHERGIDS) != 0)
1401 * 'see_jail_proc' determines whether or not visibility of processes and
1402 * sockets with credentials holding different jail ids is possible using a
1403 * variety of system MIBs.
1405 * XXX: data declarations should be together near the beginning of the file.
1408 static int see_jail_proc = 1;
1409 SYSCTL_INT(_security_bsd, OID_AUTO, see_jail_proc, CTLFLAG_RW,
1411 "Unprivileged processes may see subjects/objects with different jail ids");
1414 * Determine if u1 "can see" the subject specified by u2, according to the
1415 * 'see_jail_proc' policy.
1416 * Returns: 0 for permitted, ESRCH otherwise
1418 * References: *u1 and *u2 must not change during the call
1419 * u1 may equal u2, in which case only one reference is required
1422 cr_canseejailproc(struct ucred *u1, struct ucred *u2)
1424 if (u1->cr_uid == 0)
1426 return (!see_jail_proc && u1->cr_prison != u2->cr_prison ? ESRCH : 0);
1430 * Determine if u1 "can see" the subject specified by u2.
1431 * Returns: 0 for permitted, an errno value otherwise
1433 * References: *u1 and *u2 must not change during the call
1434 * u1 may equal u2, in which case only one reference is required
1437 cr_cansee(struct ucred *u1, struct ucred *u2)
1441 if ((error = prison_check(u1, u2)))
1444 if ((error = mac_cred_check_visible(u1, u2)))
1447 if ((error = cr_canseeotheruids(u1, u2)))
1449 if ((error = cr_canseeothergids(u1, u2)))
1451 if ((error = cr_canseejailproc(u1, u2)))
1457 * Determine if td "can see" the subject specified by p.
1458 * Returns: 0 for permitted, an errno value otherwise
1459 * Locks: Sufficient locks to protect p->p_ucred must be held. td really
1460 * should be curthread.
1461 * References: td and p must be valid for the lifetime of the call
1464 p_cansee(struct thread *td, struct proc *p)
1466 /* Wrap cr_cansee() for all functionality. */
1467 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1468 PROC_LOCK_ASSERT(p, MA_OWNED);
1470 if (td->td_proc == p)
1472 return (cr_cansee(td->td_ucred, p->p_ucred));
1476 * 'conservative_signals' prevents the delivery of a broad class of
1477 * signals by unprivileged processes to processes that have changed their
1478 * credentials since the last invocation of execve(). This can prevent
1479 * the leakage of cached information or retained privileges as a result
1480 * of a common class of signal-related vulnerabilities. However, this
1481 * may interfere with some applications that expect to be able to
1482 * deliver these signals to peer processes after having given up
1485 static int conservative_signals = 1;
1486 SYSCTL_INT(_security_bsd, OID_AUTO, conservative_signals, CTLFLAG_RW,
1487 &conservative_signals, 0, "Unprivileged processes prevented from "
1488 "sending certain signals to processes whose credentials have changed");
1490 * Determine whether cred may deliver the specified signal to proc.
1491 * Returns: 0 for permitted, an errno value otherwise.
1492 * Locks: A lock must be held for proc.
1493 * References: cred and proc must be valid for the lifetime of the call.
1496 cr_cansignal(struct ucred *cred, struct proc *proc, int signum)
1500 PROC_LOCK_ASSERT(proc, MA_OWNED);
1502 * Jail semantics limit the scope of signalling to proc in the
1503 * same jail as cred, if cred is in jail.
1505 error = prison_check(cred, proc->p_ucred);
1509 if ((error = mac_proc_check_signal(cred, proc, signum)))
1512 if ((error = cr_canseeotheruids(cred, proc->p_ucred)))
1514 if ((error = cr_canseeothergids(cred, proc->p_ucred)))
1518 * UNIX signal semantics depend on the status of the P_SUGID
1519 * bit on the target process. If the bit is set, then additional
1520 * restrictions are placed on the set of available signals.
1522 if (conservative_signals && (proc->p_flag & P_SUGID)) {
1537 * Generally, permit job and terminal control
1542 /* Not permitted without privilege. */
1543 error = priv_check_cred(cred, PRIV_SIGNAL_SUGID);
1550 * Generally, the target credential's ruid or svuid must match the
1551 * subject credential's ruid or euid.
1553 if (cred->cr_ruid != proc->p_ucred->cr_ruid &&
1554 cred->cr_ruid != proc->p_ucred->cr_svuid &&
1555 cred->cr_uid != proc->p_ucred->cr_ruid &&
1556 cred->cr_uid != proc->p_ucred->cr_svuid) {
1557 error = priv_check_cred(cred, PRIV_SIGNAL_DIFFCRED);
1566 * Determine whether td may deliver the specified signal to p.
1567 * Returns: 0 for permitted, an errno value otherwise
1568 * Locks: Sufficient locks to protect various components of td and p
1569 * must be held. td must be curthread, and a lock must be
1571 * References: td and p must be valid for the lifetime of the call
1574 p_cansignal(struct thread *td, struct proc *p, int signum)
1577 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1578 PROC_LOCK_ASSERT(p, MA_OWNED);
1579 if (td->td_proc == p)
1583 * UNIX signalling semantics require that processes in the same
1584 * session always be able to deliver SIGCONT to one another,
1585 * overriding the remaining protections.
1587 /* XXX: This will require an additional lock of some sort. */
1588 if (signum == SIGCONT && td->td_proc->p_session == p->p_session)
1591 * Some compat layers use SIGTHR and higher signals for
1592 * communication between different kernel threads of the same
1593 * process, so that they expect that it's always possible to
1594 * deliver them, even for suid applications where cr_cansignal() can
1595 * deny such ability for security consideration. It should be
1596 * pretty safe to do since the only way to create two processes
1597 * with the same p_leader is via rfork(2).
1599 if (td->td_proc->p_leader != NULL && signum >= SIGTHR &&
1600 signum < SIGTHR + 4 && td->td_proc->p_leader == p->p_leader)
1603 return (cr_cansignal(td->td_ucred, p, signum));
1607 * Determine whether td may reschedule p.
1608 * Returns: 0 for permitted, an errno value otherwise
1609 * Locks: Sufficient locks to protect various components of td and p
1610 * must be held. td must be curthread, and a lock must
1612 * References: td and p must be valid for the lifetime of the call
1615 p_cansched(struct thread *td, struct proc *p)
1619 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1620 PROC_LOCK_ASSERT(p, MA_OWNED);
1621 if (td->td_proc == p)
1623 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1626 if ((error = mac_proc_check_sched(td->td_ucred, p)))
1629 if ((error = cr_canseeotheruids(td->td_ucred, p->p_ucred)))
1631 if ((error = cr_canseeothergids(td->td_ucred, p->p_ucred)))
1633 if (td->td_ucred->cr_ruid != p->p_ucred->cr_ruid &&
1634 td->td_ucred->cr_uid != p->p_ucred->cr_ruid) {
1635 error = priv_check(td, PRIV_SCHED_DIFFCRED);
1643 * Handle getting or setting the prison's unprivileged_proc_debug
1647 sysctl_unprivileged_proc_debug(SYSCTL_HANDLER_ARGS)
1651 val = prison_allow(req->td->td_ucred, PR_ALLOW_UNPRIV_DEBUG);
1652 error = sysctl_handle_int(oidp, &val, 0, req);
1653 if (error != 0 || req->newptr == NULL)
1655 if (val != 0 && val != 1)
1657 prison_set_allow(req->td->td_ucred, PR_ALLOW_UNPRIV_DEBUG, val);
1662 * The 'unprivileged_proc_debug' flag may be used to disable a variety of
1663 * unprivileged inter-process debugging services, including some procfs
1664 * functionality, ptrace(), and ktrace(). In the past, inter-process
1665 * debugging has been involved in a variety of security problems, and sites
1666 * not requiring the service might choose to disable it when hardening
1669 SYSCTL_PROC(_security_bsd, OID_AUTO, unprivileged_proc_debug,
1670 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_SECURE |
1671 CTLFLAG_MPSAFE, 0, 0, sysctl_unprivileged_proc_debug, "I",
1672 "Unprivileged processes may use process debugging facilities");
1675 * Determine whether td may debug p.
1676 * Returns: 0 for permitted, an errno value otherwise
1677 * Locks: Sufficient locks to protect various components of td and p
1678 * must be held. td must be curthread, and a lock must
1680 * References: td and p must be valid for the lifetime of the call
1683 p_candebug(struct thread *td, struct proc *p)
1685 int error, grpsubset, i, uidsubset;
1687 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1688 PROC_LOCK_ASSERT(p, MA_OWNED);
1689 if (td->td_proc == p)
1691 if ((error = priv_check(td, PRIV_DEBUG_UNPRIV)))
1693 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1696 if ((error = mac_proc_check_debug(td->td_ucred, p)))
1699 if ((error = cr_canseeotheruids(td->td_ucred, p->p_ucred)))
1701 if ((error = cr_canseeothergids(td->td_ucred, p->p_ucred)))
1705 * Is p's group set a subset of td's effective group set? This
1706 * includes p's egid, group access list, rgid, and svgid.
1709 for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
1710 if (!groupmember(p->p_ucred->cr_groups[i], td->td_ucred)) {
1715 grpsubset = grpsubset &&
1716 groupmember(p->p_ucred->cr_rgid, td->td_ucred) &&
1717 groupmember(p->p_ucred->cr_svgid, td->td_ucred);
1720 * Are the uids present in p's credential equal to td's
1721 * effective uid? This includes p's euid, svuid, and ruid.
1723 uidsubset = (td->td_ucred->cr_uid == p->p_ucred->cr_uid &&
1724 td->td_ucred->cr_uid == p->p_ucred->cr_svuid &&
1725 td->td_ucred->cr_uid == p->p_ucred->cr_ruid);
1728 * If p's gids aren't a subset, or the uids aren't a subset,
1729 * or the credential has changed, require appropriate privilege
1730 * for td to debug p.
1732 if (!grpsubset || !uidsubset) {
1733 error = priv_check(td, PRIV_DEBUG_DIFFCRED);
1739 * Has the credential of the process changed since the last exec()?
1741 if ((p->p_flag & P_SUGID) != 0) {
1742 error = priv_check(td, PRIV_DEBUG_SUGID);
1747 /* Can't trace init when securelevel > 0. */
1748 if (p == initproc) {
1749 error = securelevel_gt(td->td_ucred, 0);
1755 * Can't trace a process that's currently exec'ing.
1757 * XXX: Note, this is not a security policy decision, it's a
1758 * basic correctness/functionality decision. Therefore, this check
1759 * should be moved to the caller's of p_candebug().
1761 if ((p->p_flag & P_INEXEC) != 0)
1764 /* Denied explicitly */
1765 if ((p->p_flag2 & P2_NOTRACE) != 0) {
1766 error = priv_check(td, PRIV_DEBUG_DENIED);
1775 * Determine whether the subject represented by cred can "see" a socket.
1776 * Returns: 0 for permitted, ENOENT otherwise.
1779 cr_canseesocket(struct ucred *cred, struct socket *so)
1783 error = prison_check(cred, so->so_cred);
1787 error = mac_socket_check_visible(cred, so);
1791 if (cr_canseeotheruids(cred, so->so_cred))
1793 if (cr_canseeothergids(cred, so->so_cred))
1800 * Determine whether td can wait for the exit of p.
1801 * Returns: 0 for permitted, an errno value otherwise
1802 * Locks: Sufficient locks to protect various components of td and p
1803 * must be held. td must be curthread, and a lock must
1805 * References: td and p must be valid for the lifetime of the call
1809 p_canwait(struct thread *td, struct proc *p)
1813 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1814 PROC_LOCK_ASSERT(p, MA_OWNED);
1815 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1818 if ((error = mac_proc_check_wait(td->td_ucred, p)))
1822 /* XXXMAC: This could have odd effects on some shells. */
1823 if ((error = cr_canseeotheruids(td->td_ucred, p->p_ucred)))
1831 * Credential management.
1833 * struct ucred objects are rarely allocated but gain and lose references all
1834 * the time (e.g., on struct file alloc/dealloc) turning refcount updates into
1835 * a significant source of cache-line ping ponging. Common cases are worked
1836 * around by modifying thread-local counter instead if the cred to operate on
1837 * matches td_realucred.
1839 * The counter is split into 2 parts:
1840 * - cr_users -- total count of all struct proc and struct thread objects
1841 * which have given cred in p_ucred and td_ucred respectively
1842 * - cr_ref -- the actual ref count, only valid if cr_users == 0
1844 * If users == 0 then cr_ref behaves similarly to refcount(9), in particular if
1845 * the count reaches 0 the object is freeable.
1846 * If users > 0 and curthread->td_realucred == cred, then updates are performed
1847 * against td_ucredref.
1848 * In other cases updates are performed against cr_ref.
1850 * Changing td_realucred into something else decrements cr_users and transfers
1851 * accumulated updates.
1854 crcowget(struct ucred *cr)
1857 mtx_lock(&cr->cr_mtx);
1858 KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
1859 __func__, cr->cr_users, cr));
1862 mtx_unlock(&cr->cr_mtx);
1866 static struct ucred *
1867 crunuse(struct thread *td)
1869 struct ucred *cr, *crold;
1871 MPASS(td->td_realucred == td->td_ucred);
1872 cr = td->td_realucred;
1873 mtx_lock(&cr->cr_mtx);
1874 cr->cr_ref += td->td_ucredref;
1875 td->td_ucredref = 0;
1876 KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
1877 __func__, cr->cr_users, cr));
1879 if (cr->cr_users == 0) {
1880 KASSERT(cr->cr_ref > 0, ("%s: ref %ld not > 0 on cred %p",
1881 __func__, cr->cr_ref, cr));
1887 mtx_unlock(&cr->cr_mtx);
1888 td->td_realucred = NULL;
1893 crunusebatch(struct ucred *cr, int users, int ref)
1896 KASSERT(users > 0, ("%s: passed users %d not > 0 ; cred %p",
1897 __func__, users, cr));
1898 mtx_lock(&cr->cr_mtx);
1899 KASSERT(cr->cr_users >= users, ("%s: users %d not > %d on cred %p",
1900 __func__, cr->cr_users, users, cr));
1901 cr->cr_users -= users;
1903 cr->cr_ref -= users;
1904 if (cr->cr_users > 0) {
1905 mtx_unlock(&cr->cr_mtx);
1908 KASSERT(cr->cr_ref >= 0, ("%s: ref %ld not >= 0 on cred %p",
1909 __func__, cr->cr_ref, cr));
1910 if (cr->cr_ref > 0) {
1911 mtx_unlock(&cr->cr_mtx);
1918 crcowfree(struct thread *td)
1932 struct ucred *crnew, *crold;
1936 PROC_LOCK_ASSERT(p, MA_OWNED);
1938 MPASS(td->td_realucred == td->td_ucred);
1939 if (td->td_realucred == p->p_ucred)
1942 crnew = crcowget(p->p_ucred);
1943 crold = crunuse(td);
1944 td->td_realucred = crnew;
1945 td->td_ucred = td->td_realucred;
1953 credbatch_add(struct credbatch *crb, struct thread *td)
1957 MPASS(td->td_realucred != NULL);
1958 MPASS(td->td_realucred == td->td_ucred);
1959 MPASS(TD_GET_STATE(td) == TDS_INACTIVE);
1960 cr = td->td_realucred;
1961 KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
1962 __func__, cr->cr_users, cr));
1963 if (crb->cred != cr) {
1964 if (crb->users > 0) {
1965 MPASS(crb->cred != NULL);
1966 crunusebatch(crb->cred, crb->users, crb->ref);
1973 crb->ref += td->td_ucredref;
1974 td->td_ucredref = 0;
1975 td->td_realucred = NULL;
1979 credbatch_final(struct credbatch *crb)
1982 MPASS(crb->cred != NULL);
1983 MPASS(crb->users > 0);
1984 crunusebatch(crb->cred, crb->users, crb->ref);
1988 * Allocate a zeroed cred structure.
1995 cr = malloc(sizeof(*cr), M_CRED, M_WAITOK | M_ZERO);
1996 mtx_init(&cr->cr_mtx, "cred", NULL, MTX_DEF);
1999 audit_cred_init(cr);
2004 cr->cr_groups = cr->cr_smallgroups;
2006 sizeof(cr->cr_smallgroups) / sizeof(cr->cr_smallgroups[0]);
2011 * Claim another reference to a ucred structure.
2014 crhold(struct ucred *cr)
2019 if (__predict_true(td->td_realucred == cr)) {
2020 KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
2021 __func__, cr->cr_users, cr));
2025 mtx_lock(&cr->cr_mtx);
2027 mtx_unlock(&cr->cr_mtx);
2032 * Free a cred structure. Throws away space when ref count gets to 0.
2035 crfree(struct ucred *cr)
2040 if (__predict_true(td->td_realucred == cr)) {
2041 KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
2042 __func__, cr->cr_users, cr));
2046 mtx_lock(&cr->cr_mtx);
2047 KASSERT(cr->cr_users >= 0, ("%s: users %d not >= 0 on cred %p",
2048 __func__, cr->cr_users, cr));
2050 if (cr->cr_users > 0) {
2051 mtx_unlock(&cr->cr_mtx);
2054 KASSERT(cr->cr_ref >= 0, ("%s: ref %ld not >= 0 on cred %p",
2055 __func__, cr->cr_ref, cr));
2056 if (cr->cr_ref > 0) {
2057 mtx_unlock(&cr->cr_mtx);
2064 crfree_final(struct ucred *cr)
2067 KASSERT(cr->cr_users == 0, ("%s: users %d not == 0 on cred %p",
2068 __func__, cr->cr_users, cr));
2069 KASSERT(cr->cr_ref == 0, ("%s: ref %ld not == 0 on cred %p",
2070 __func__, cr->cr_ref, cr));
2073 * Some callers of crget(), such as nfs_statfs(), allocate a temporary
2074 * credential, but don't allocate a uidinfo structure.
2076 if (cr->cr_uidinfo != NULL)
2077 uifree(cr->cr_uidinfo);
2078 if (cr->cr_ruidinfo != NULL)
2079 uifree(cr->cr_ruidinfo);
2080 if (cr->cr_prison != NULL)
2081 prison_free(cr->cr_prison);
2082 if (cr->cr_loginclass != NULL)
2083 loginclass_free(cr->cr_loginclass);
2085 audit_cred_destroy(cr);
2088 mac_cred_destroy(cr);
2090 mtx_destroy(&cr->cr_mtx);
2091 if (cr->cr_groups != cr->cr_smallgroups)
2092 free(cr->cr_groups, M_CRED);
2097 * Copy a ucred's contents from a template. Does not block.
2100 crcopy(struct ucred *dest, struct ucred *src)
2103 KASSERT(dest->cr_ref == 1, ("crcopy of shared ucred"));
2104 bcopy(&src->cr_startcopy, &dest->cr_startcopy,
2105 (unsigned)((caddr_t)&src->cr_endcopy -
2106 (caddr_t)&src->cr_startcopy));
2107 dest->cr_flags = src->cr_flags;
2108 crsetgroups(dest, src->cr_ngroups, src->cr_groups);
2109 uihold(dest->cr_uidinfo);
2110 uihold(dest->cr_ruidinfo);
2111 prison_hold(dest->cr_prison);
2112 loginclass_hold(dest->cr_loginclass);
2114 audit_cred_copy(src, dest);
2117 mac_cred_copy(src, dest);
2122 * Dup cred struct to a new held one.
2125 crdup(struct ucred *cr)
2127 struct ucred *newcr;
2135 * Fill in a struct xucred based on a struct ucred.
2138 cru2x(struct ucred *cr, struct xucred *xcr)
2142 bzero(xcr, sizeof(*xcr));
2143 xcr->cr_version = XUCRED_VERSION;
2144 xcr->cr_uid = cr->cr_uid;
2146 ngroups = MIN(cr->cr_ngroups, XU_NGROUPS);
2147 xcr->cr_ngroups = ngroups;
2148 bcopy(cr->cr_groups, xcr->cr_groups,
2149 ngroups * sizeof(*cr->cr_groups));
2153 cru2xt(struct thread *td, struct xucred *xcr)
2156 cru2x(td->td_ucred, xcr);
2157 xcr->cr_pid = td->td_proc->p_pid;
2161 * Set initial process credentials.
2162 * Callers are responsible for providing the reference for provided credentials.
2165 proc_set_cred_init(struct proc *p, struct ucred *newcred)
2168 p->p_ucred = crcowget(newcred);
2172 * Change process credentials.
2173 * Callers are responsible for providing the reference for passed credentials
2174 * and for freeing old ones.
2176 * Process has to be locked except when it does not have credentials (as it
2177 * should not be visible just yet) or when newcred is NULL (as this can be
2178 * only used when the process is about to be freed, at which point it should
2179 * not be visible anymore).
2182 proc_set_cred(struct proc *p, struct ucred *newcred)
2188 PROC_LOCK_ASSERT(p, MA_OWNED);
2189 KASSERT(newcred->cr_users == 0, ("%s: users %d not 0 on cred %p",
2190 __func__, newcred->cr_users, newcred));
2191 mtx_lock(&cr->cr_mtx);
2192 KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
2193 __func__, cr->cr_users, cr));
2195 mtx_unlock(&cr->cr_mtx);
2196 p->p_ucred = newcred;
2197 newcred->cr_users = 1;
2202 proc_unset_cred(struct proc *p)
2206 MPASS(p->p_state == PRS_ZOMBIE || p->p_state == PRS_NEW);
2209 KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
2210 __func__, cr->cr_users, cr));
2211 mtx_lock(&cr->cr_mtx);
2213 if (cr->cr_users == 0)
2214 KASSERT(cr->cr_ref > 0, ("%s: ref %ld not > 0 on cred %p",
2215 __func__, cr->cr_ref, cr));
2216 mtx_unlock(&cr->cr_mtx);
2221 crcopysafe(struct proc *p, struct ucred *cr)
2223 struct ucred *oldcred;
2226 PROC_LOCK_ASSERT(p, MA_OWNED);
2228 oldcred = p->p_ucred;
2229 while (cr->cr_agroups < oldcred->cr_agroups) {
2230 groups = oldcred->cr_agroups;
2232 crextend(cr, groups);
2234 oldcred = p->p_ucred;
2236 crcopy(cr, oldcred);
2242 * Extend the passed in credential to hold n items.
2245 crextend(struct ucred *cr, int n)
2250 if (n <= cr->cr_agroups)
2254 * We extend by 2 each time since we're using a power of two
2255 * allocator until we need enough groups to fill a page.
2256 * Once we're allocating multiple pages, only allocate as many
2257 * as we actually need. The case of processes needing a
2258 * non-power of two number of pages seems more likely than
2259 * a real world process that adds thousands of groups one at a
2262 if ( n < PAGE_SIZE / sizeof(gid_t) ) {
2263 if (cr->cr_agroups == 0)
2264 cnt = MAX(1, MINALLOCSIZE / sizeof(gid_t));
2266 cnt = cr->cr_agroups * 2;
2271 cnt = roundup2(n, PAGE_SIZE / sizeof(gid_t));
2273 /* Free the old array. */
2274 if (cr->cr_groups != cr->cr_smallgroups)
2275 free(cr->cr_groups, M_CRED);
2277 cr->cr_groups = malloc(cnt * sizeof(gid_t), M_CRED, M_WAITOK | M_ZERO);
2278 cr->cr_agroups = cnt;
2282 * Copy groups in to a credential, preserving any necessary invariants.
2283 * Currently this includes the sorting of all supplemental gids.
2284 * crextend() must have been called before hand to ensure sufficient
2285 * space is available.
2288 crsetgroups_locked(struct ucred *cr, int ngrp, gid_t *groups)
2294 KASSERT(cr->cr_agroups >= ngrp, ("cr_ngroups is too small"));
2296 bcopy(groups, cr->cr_groups, ngrp * sizeof(gid_t));
2297 cr->cr_ngroups = ngrp;
2300 * Sort all groups except cr_groups[0] to allow groupmember to
2301 * perform a binary search.
2303 * XXX: If large numbers of groups become common this should
2304 * be replaced with shell sort like linux uses or possibly
2307 for (i = 2; i < ngrp; i++) {
2308 g = cr->cr_groups[i];
2309 for (j = i-1; j >= 1 && g < cr->cr_groups[j]; j--)
2310 cr->cr_groups[j + 1] = cr->cr_groups[j];
2311 cr->cr_groups[j + 1] = g;
2316 * Copy groups in to a credential after expanding it if required.
2317 * Truncate the list to (ngroups_max + 1) if it is too large.
2320 crsetgroups(struct ucred *cr, int ngrp, gid_t *groups)
2323 if (ngrp > ngroups_max + 1)
2324 ngrp = ngroups_max + 1;
2327 crsetgroups_locked(cr, ngrp, groups);
2331 * Get login name, if available.
2333 #ifndef _SYS_SYSPROTO_H_
2334 struct getlogin_args {
2341 sys_getlogin(struct thread *td, struct getlogin_args *uap)
2343 char login[MAXLOGNAME];
2344 struct proc *p = td->td_proc;
2347 if (uap->namelen > MAXLOGNAME)
2348 uap->namelen = MAXLOGNAME;
2350 SESS_LOCK(p->p_session);
2351 len = strlcpy(login, p->p_session->s_login, uap->namelen) + 1;
2352 SESS_UNLOCK(p->p_session);
2354 if (len > uap->namelen)
2356 return (copyout(login, uap->namebuf, len));
2362 #ifndef _SYS_SYSPROTO_H_
2363 struct setlogin_args {
2369 sys_setlogin(struct thread *td, struct setlogin_args *uap)
2371 struct proc *p = td->td_proc;
2373 char logintmp[MAXLOGNAME];
2375 CTASSERT(sizeof(p->p_session->s_login) >= sizeof(logintmp));
2377 error = priv_check(td, PRIV_PROC_SETLOGIN);
2380 error = copyinstr(uap->namebuf, logintmp, sizeof(logintmp), NULL);
2382 if (error == ENAMETOOLONG)
2386 AUDIT_ARG_LOGIN(logintmp);
2388 SESS_LOCK(p->p_session);
2389 strcpy(p->p_session->s_login, logintmp);
2390 SESS_UNLOCK(p->p_session);
2396 setsugid(struct proc *p)
2399 PROC_LOCK_ASSERT(p, MA_OWNED);
2400 p->p_flag |= P_SUGID;
2404 * Change a process's effective uid.
2405 * Side effects: newcred->cr_uid and newcred->cr_uidinfo will be modified.
2406 * References: newcred must be an exclusive credential reference for the
2407 * duration of the call.
2410 change_euid(struct ucred *newcred, struct uidinfo *euip)
2413 newcred->cr_uid = euip->ui_uid;
2415 uifree(newcred->cr_uidinfo);
2416 newcred->cr_uidinfo = euip;
2420 * Change a process's effective gid.
2421 * Side effects: newcred->cr_gid will be modified.
2422 * References: newcred must be an exclusive credential reference for the
2423 * duration of the call.
2426 change_egid(struct ucred *newcred, gid_t egid)
2429 newcred->cr_groups[0] = egid;
2433 * Change a process's real uid.
2434 * Side effects: newcred->cr_ruid will be updated, newcred->cr_ruidinfo
2435 * will be updated, and the old and new cr_ruidinfo proc
2436 * counts will be updated.
2437 * References: newcred must be an exclusive credential reference for the
2438 * duration of the call.
2441 change_ruid(struct ucred *newcred, struct uidinfo *ruip)
2444 (void)chgproccnt(newcred->cr_ruidinfo, -1, 0);
2445 newcred->cr_ruid = ruip->ui_uid;
2447 uifree(newcred->cr_ruidinfo);
2448 newcred->cr_ruidinfo = ruip;
2449 (void)chgproccnt(newcred->cr_ruidinfo, 1, 0);
2453 * Change a process's real gid.
2454 * Side effects: newcred->cr_rgid will be updated.
2455 * References: newcred must be an exclusive credential reference for the
2456 * duration of the call.
2459 change_rgid(struct ucred *newcred, gid_t rgid)
2462 newcred->cr_rgid = rgid;
2466 * Change a process's saved uid.
2467 * Side effects: newcred->cr_svuid will be updated.
2468 * References: newcred must be an exclusive credential reference for the
2469 * duration of the call.
2472 change_svuid(struct ucred *newcred, uid_t svuid)
2475 newcred->cr_svuid = svuid;
2479 * Change a process's saved gid.
2480 * Side effects: newcred->cr_svgid will be updated.
2481 * References: newcred must be an exclusive credential reference for the
2482 * duration of the call.
2485 change_svgid(struct ucred *newcred, gid_t svgid)
2488 newcred->cr_svgid = svgid;
2491 bool allow_ptrace = true;
2492 SYSCTL_BOOL(_security_bsd, OID_AUTO, allow_ptrace, CTLFLAG_RWTUN,
2494 "Deny ptrace(2) use by returning ENOSYS");