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/refcount.h>
65 #include <sys/sysent.h>
66 #include <sys/sysproto.h>
68 #include <sys/racct.h>
70 #include <sys/resourcevar.h>
71 #include <sys/socket.h>
72 #include <sys/socketvar.h>
73 #include <sys/syscallsubr.h>
74 #include <sys/sysctl.h>
78 "Kernel support for interfaces necessary for regression testing (SECURITY RISK!)");
81 #include <security/audit/audit.h>
82 #include <security/mac/mac_framework.h>
84 static MALLOC_DEFINE(M_CRED, "cred", "credentials");
86 SYSCTL_NODE(_security, OID_AUTO, bsd, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
87 "BSD security policy");
89 static void crfree_final(struct ucred *cr);
90 static void crsetgroups_locked(struct ucred *cr, int ngrp,
93 #ifndef _SYS_SYSPROTO_H_
100 sys_getpid(struct thread *td, struct getpid_args *uap)
102 struct proc *p = td->td_proc;
104 td->td_retval[0] = p->p_pid;
105 #if defined(COMPAT_43)
106 if (SV_PROC_FLAG(p, SV_AOUT))
107 td->td_retval[1] = kern_getppid(td);
112 #ifndef _SYS_SYSPROTO_H_
113 struct getppid_args {
119 sys_getppid(struct thread *td, struct getppid_args *uap)
122 td->td_retval[0] = kern_getppid(td);
127 kern_getppid(struct thread *td)
129 struct proc *p = td->td_proc;
135 * Get process group ID; note that POSIX getpgrp takes no parameter.
137 #ifndef _SYS_SYSPROTO_H_
138 struct getpgrp_args {
143 sys_getpgrp(struct thread *td, struct getpgrp_args *uap)
145 struct proc *p = td->td_proc;
148 td->td_retval[0] = p->p_pgrp->pg_id;
153 /* Get an arbitrary pid's process group id */
154 #ifndef _SYS_SYSPROTO_H_
155 struct getpgid_args {
160 sys_getpgid(struct thread *td, struct getpgid_args *uap)
172 error = p_cansee(td, p);
178 td->td_retval[0] = p->p_pgrp->pg_id;
184 * Get an arbitrary pid's session id.
186 #ifndef _SYS_SYSPROTO_H_
192 sys_getsid(struct thread *td, struct getsid_args *uap)
195 return (kern_getsid(td, uap->pid));
199 kern_getsid(struct thread *td, pid_t pid)
211 error = p_cansee(td, p);
217 td->td_retval[0] = p->p_session->s_sid;
222 #ifndef _SYS_SYSPROTO_H_
229 sys_getuid(struct thread *td, struct getuid_args *uap)
232 td->td_retval[0] = td->td_ucred->cr_ruid;
233 #if defined(COMPAT_43)
234 td->td_retval[1] = td->td_ucred->cr_uid;
239 #ifndef _SYS_SYSPROTO_H_
240 struct geteuid_args {
246 sys_geteuid(struct thread *td, struct geteuid_args *uap)
249 td->td_retval[0] = td->td_ucred->cr_uid;
253 #ifndef _SYS_SYSPROTO_H_
260 sys_getgid(struct thread *td, struct getgid_args *uap)
263 td->td_retval[0] = td->td_ucred->cr_rgid;
264 #if defined(COMPAT_43)
265 td->td_retval[1] = td->td_ucred->cr_groups[0];
271 * Get effective group ID. The "egid" is groups[0], and could be obtained
272 * via getgroups. This syscall exists because it is somewhat painful to do
273 * correctly in a library function.
275 #ifndef _SYS_SYSPROTO_H_
276 struct getegid_args {
282 sys_getegid(struct thread *td, struct getegid_args *uap)
285 td->td_retval[0] = td->td_ucred->cr_groups[0];
289 #ifndef _SYS_SYSPROTO_H_
290 struct getgroups_args {
296 sys_getgroups(struct thread *td, struct getgroups_args *uap)
303 ngrp = cred->cr_ngroups;
305 if (uap->gidsetsize == 0) {
309 if (uap->gidsetsize < ngrp)
312 error = copyout(cred->cr_groups, uap->gidset, ngrp * sizeof(gid_t));
314 td->td_retval[0] = ngrp;
318 #ifndef _SYS_SYSPROTO_H_
325 sys_setsid(struct thread *td, struct setsid_args *uap)
329 struct proc *p = td->td_proc;
330 struct pgrp *newpgrp;
331 struct session *newsess;
336 newpgrp = malloc(sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
337 newsess = malloc(sizeof(struct session), M_SESSION, M_WAITOK | M_ZERO);
339 sx_xlock(&proctree_lock);
341 if (p->p_pgid == p->p_pid || (pgrp = pgfind(p->p_pid)) != NULL) {
346 (void)enterpgrp(p, p->p_pid, newpgrp, newsess);
347 td->td_retval[0] = p->p_pid;
352 sx_xunlock(&proctree_lock);
355 free(newpgrp, M_PGRP);
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 = malloc(sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
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"));
460 free(newpgrp, M_PGRP);
465 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
466 * compatible. It says that setting the uid/gid to euid/egid is a special
467 * case of "appropriate privilege". Once the rules are expanded out, this
468 * basically means that setuid(nnn) sets all three id's, in all permitted
469 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
470 * does not set the saved id - this is dangerous for traditional BSD
471 * programs. For this reason, we *really* do not want to set
472 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
474 #define POSIX_APPENDIX_B_4_2_2
476 #ifndef _SYS_SYSPROTO_H_
483 sys_setuid(struct thread *td, struct setuid_args *uap)
485 struct proc *p = td->td_proc;
486 struct ucred *newcred, *oldcred;
497 * Copy credentials so other references do not see our changes.
499 oldcred = crcopysafe(p, newcred);
502 error = mac_cred_check_setuid(oldcred, uid);
508 * See if we have "permission" by POSIX 1003.1 rules.
510 * Note that setuid(geteuid()) is a special case of
511 * "appropriate privileges" in appendix B.4.2.2. We need
512 * to use this clause to be compatible with traditional BSD
513 * semantics. Basically, it means that "setuid(xx)" sets all
514 * three id's (assuming you have privs).
516 * Notes on the logic. We do things in three steps.
517 * 1: We determine if the euid is going to change, and do EPERM
518 * right away. We unconditionally change the euid later if this
519 * test is satisfied, simplifying that part of the logic.
520 * 2: We determine if the real and/or saved uids are going to
521 * change. Determined by compile options.
522 * 3: Change euid last. (after tests in #2 for "appropriate privs")
524 if (uid != oldcred->cr_ruid && /* allow setuid(getuid()) */
525 #ifdef _POSIX_SAVED_IDS
526 uid != oldcred->cr_svuid && /* allow setuid(saved gid) */
528 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
529 uid != oldcred->cr_uid && /* allow setuid(geteuid()) */
531 (error = priv_check_cred(oldcred, PRIV_CRED_SETUID)) != 0)
534 #ifdef _POSIX_SAVED_IDS
536 * Do we have "appropriate privileges" (are we root or uid == euid)
537 * If so, we are changing the real uid and/or saved uid.
540 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
541 uid == oldcred->cr_uid ||
543 /* We are using privs. */
544 priv_check_cred(oldcred, PRIV_CRED_SETUID) == 0)
548 * Set the real uid and transfer proc count to new user.
550 if (uid != oldcred->cr_ruid) {
551 change_ruid(newcred, uip);
557 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
558 * the security of seteuid() depends on it. B.4.2.2 says it
559 * is important that we should do this.
561 if (uid != oldcred->cr_svuid) {
562 change_svuid(newcred, uid);
568 * In all permitted cases, we are changing the euid.
570 if (uid != oldcred->cr_uid) {
571 change_euid(newcred, uip);
574 proc_set_cred(p, newcred);
576 racct_proc_ucred_changed(p, oldcred, newcred);
581 rctl_proc_ucred_changed(p, newcred);
595 #ifndef _SYS_SYSPROTO_H_
596 struct seteuid_args {
602 sys_seteuid(struct thread *td, struct seteuid_args *uap)
604 struct proc *p = td->td_proc;
605 struct ucred *newcred, *oldcred;
607 struct uidinfo *euip;
611 AUDIT_ARG_EUID(euid);
616 * Copy credentials so other references do not see our changes.
618 oldcred = crcopysafe(p, newcred);
621 error = mac_cred_check_seteuid(oldcred, euid);
626 if (euid != oldcred->cr_ruid && /* allow seteuid(getuid()) */
627 euid != oldcred->cr_svuid && /* allow seteuid(saved uid) */
628 (error = priv_check_cred(oldcred, PRIV_CRED_SETEUID)) != 0)
632 * Everything's okay, do it.
634 if (oldcred->cr_uid != euid) {
635 change_euid(newcred, euip);
638 proc_set_cred(p, newcred);
651 #ifndef _SYS_SYSPROTO_H_
658 sys_setgid(struct thread *td, struct setgid_args *uap)
660 struct proc *p = td->td_proc;
661 struct ucred *newcred, *oldcred;
669 oldcred = crcopysafe(p, newcred);
672 error = mac_cred_check_setgid(oldcred, gid);
678 * See if we have "permission" by POSIX 1003.1 rules.
680 * Note that setgid(getegid()) is a special case of
681 * "appropriate privileges" in appendix B.4.2.2. We need
682 * to use this clause to be compatible with traditional BSD
683 * semantics. Basically, it means that "setgid(xx)" sets all
684 * three id's (assuming you have privs).
686 * For notes on the logic here, see setuid() above.
688 if (gid != oldcred->cr_rgid && /* allow setgid(getgid()) */
689 #ifdef _POSIX_SAVED_IDS
690 gid != oldcred->cr_svgid && /* allow setgid(saved gid) */
692 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
693 gid != oldcred->cr_groups[0] && /* allow setgid(getegid()) */
695 (error = priv_check_cred(oldcred, PRIV_CRED_SETGID)) != 0)
698 #ifdef _POSIX_SAVED_IDS
700 * Do we have "appropriate privileges" (are we root or gid == egid)
701 * If so, we are changing the real uid and saved gid.
704 #ifdef POSIX_APPENDIX_B_4_2_2 /* use the clause from B.4.2.2 */
705 gid == oldcred->cr_groups[0] ||
707 /* We are using privs. */
708 priv_check_cred(oldcred, PRIV_CRED_SETGID) == 0)
714 if (oldcred->cr_rgid != gid) {
715 change_rgid(newcred, gid);
721 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
722 * the security of setegid() depends on it. B.4.2.2 says it
723 * is important that we should do this.
725 if (oldcred->cr_svgid != gid) {
726 change_svgid(newcred, gid);
731 * In all cases permitted cases, we are changing the egid.
732 * Copy credentials so other references do not see our changes.
734 if (oldcred->cr_groups[0] != gid) {
735 change_egid(newcred, gid);
738 proc_set_cred(p, newcred);
749 #ifndef _SYS_SYSPROTO_H_
750 struct setegid_args {
756 sys_setegid(struct thread *td, struct setegid_args *uap)
758 struct proc *p = td->td_proc;
759 struct ucred *newcred, *oldcred;
764 AUDIT_ARG_EGID(egid);
767 oldcred = crcopysafe(p, newcred);
770 error = mac_cred_check_setegid(oldcred, egid);
775 if (egid != oldcred->cr_rgid && /* allow setegid(getgid()) */
776 egid != oldcred->cr_svgid && /* allow setegid(saved gid) */
777 (error = priv_check_cred(oldcred, PRIV_CRED_SETEGID)) != 0)
780 if (oldcred->cr_groups[0] != egid) {
781 change_egid(newcred, egid);
784 proc_set_cred(p, newcred);
795 #ifndef _SYS_SYSPROTO_H_
796 struct setgroups_args {
803 sys_setgroups(struct thread *td, struct setgroups_args *uap)
805 gid_t smallgroups[XU_NGROUPS];
810 gidsetsize = uap->gidsetsize;
811 if (gidsetsize > ngroups_max + 1)
814 if (gidsetsize > XU_NGROUPS)
815 groups = malloc(gidsetsize * sizeof(gid_t), M_TEMP, M_WAITOK);
817 groups = smallgroups;
819 error = copyin(uap->gidset, groups, gidsetsize * sizeof(gid_t));
821 error = kern_setgroups(td, gidsetsize, groups);
823 if (gidsetsize > XU_NGROUPS)
824 free(groups, M_TEMP);
829 kern_setgroups(struct thread *td, u_int ngrp, gid_t *groups)
831 struct proc *p = td->td_proc;
832 struct ucred *newcred, *oldcred;
835 MPASS(ngrp <= ngroups_max + 1);
836 AUDIT_ARG_GROUPSET(groups, ngrp);
838 crextend(newcred, ngrp);
840 oldcred = crcopysafe(p, newcred);
843 error = mac_cred_check_setgroups(oldcred, ngrp, groups);
848 error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS);
854 * setgroups(0, NULL) is a legitimate way of clearing the
855 * groups vector on non-BSD systems (which generally do not
856 * have the egid in the groups[0]). We risk security holes
857 * when running non-BSD software if we do not do the same.
859 newcred->cr_ngroups = 1;
861 crsetgroups_locked(newcred, ngrp, groups);
864 proc_set_cred(p, newcred);
875 #ifndef _SYS_SYSPROTO_H_
876 struct setreuid_args {
883 sys_setreuid(struct thread *td, struct setreuid_args *uap)
885 struct proc *p = td->td_proc;
886 struct ucred *newcred, *oldcred;
888 struct uidinfo *euip, *ruip;
893 AUDIT_ARG_EUID(euid);
894 AUDIT_ARG_RUID(ruid);
899 oldcred = crcopysafe(p, newcred);
902 error = mac_cred_check_setreuid(oldcred, ruid, euid);
907 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
908 ruid != oldcred->cr_svuid) ||
909 (euid != (uid_t)-1 && euid != oldcred->cr_uid &&
910 euid != oldcred->cr_ruid && euid != oldcred->cr_svuid)) &&
911 (error = priv_check_cred(oldcred, PRIV_CRED_SETREUID)) != 0)
914 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
915 change_euid(newcred, euip);
918 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
919 change_ruid(newcred, ruip);
922 if ((ruid != (uid_t)-1 || newcred->cr_uid != newcred->cr_ruid) &&
923 newcred->cr_svuid != newcred->cr_uid) {
924 change_svuid(newcred, newcred->cr_uid);
927 proc_set_cred(p, newcred);
929 racct_proc_ucred_changed(p, oldcred, newcred);
934 rctl_proc_ucred_changed(p, newcred);
950 #ifndef _SYS_SYSPROTO_H_
951 struct setregid_args {
958 sys_setregid(struct thread *td, struct setregid_args *uap)
960 struct proc *p = td->td_proc;
961 struct ucred *newcred, *oldcred;
967 AUDIT_ARG_EGID(egid);
968 AUDIT_ARG_RGID(rgid);
971 oldcred = crcopysafe(p, newcred);
974 error = mac_cred_check_setregid(oldcred, rgid, egid);
979 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
980 rgid != oldcred->cr_svgid) ||
981 (egid != (gid_t)-1 && egid != oldcred->cr_groups[0] &&
982 egid != oldcred->cr_rgid && egid != oldcred->cr_svgid)) &&
983 (error = priv_check_cred(oldcred, PRIV_CRED_SETREGID)) != 0)
986 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
987 change_egid(newcred, egid);
990 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
991 change_rgid(newcred, rgid);
994 if ((rgid != (gid_t)-1 || newcred->cr_groups[0] != newcred->cr_rgid) &&
995 newcred->cr_svgid != newcred->cr_groups[0]) {
996 change_svgid(newcred, newcred->cr_groups[0]);
999 proc_set_cred(p, newcred);
1011 * setresuid(ruid, euid, suid) is like setreuid except control over the saved
1014 #ifndef _SYS_SYSPROTO_H_
1015 struct setresuid_args {
1023 sys_setresuid(struct thread *td, struct setresuid_args *uap)
1025 struct proc *p = td->td_proc;
1026 struct ucred *newcred, *oldcred;
1027 uid_t euid, ruid, suid;
1028 struct uidinfo *euip, *ruip;
1034 AUDIT_ARG_EUID(euid);
1035 AUDIT_ARG_RUID(ruid);
1036 AUDIT_ARG_SUID(suid);
1038 euip = uifind(euid);
1039 ruip = uifind(ruid);
1041 oldcred = crcopysafe(p, newcred);
1044 error = mac_cred_check_setresuid(oldcred, ruid, euid, suid);
1049 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
1050 ruid != oldcred->cr_svuid &&
1051 ruid != oldcred->cr_uid) ||
1052 (euid != (uid_t)-1 && euid != oldcred->cr_ruid &&
1053 euid != oldcred->cr_svuid &&
1054 euid != oldcred->cr_uid) ||
1055 (suid != (uid_t)-1 && suid != oldcred->cr_ruid &&
1056 suid != oldcred->cr_svuid &&
1057 suid != oldcred->cr_uid)) &&
1058 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESUID)) != 0)
1061 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
1062 change_euid(newcred, euip);
1065 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
1066 change_ruid(newcred, ruip);
1069 if (suid != (uid_t)-1 && oldcred->cr_svuid != suid) {
1070 change_svuid(newcred, suid);
1073 proc_set_cred(p, newcred);
1075 racct_proc_ucred_changed(p, oldcred, newcred);
1080 rctl_proc_ucred_changed(p, newcred);
1098 * setresgid(rgid, egid, sgid) is like setregid except control over the saved
1101 #ifndef _SYS_SYSPROTO_H_
1102 struct setresgid_args {
1110 sys_setresgid(struct thread *td, struct setresgid_args *uap)
1112 struct proc *p = td->td_proc;
1113 struct ucred *newcred, *oldcred;
1114 gid_t egid, rgid, sgid;
1120 AUDIT_ARG_EGID(egid);
1121 AUDIT_ARG_RGID(rgid);
1122 AUDIT_ARG_SGID(sgid);
1125 oldcred = crcopysafe(p, newcred);
1128 error = mac_cred_check_setresgid(oldcred, rgid, egid, sgid);
1133 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
1134 rgid != oldcred->cr_svgid &&
1135 rgid != oldcred->cr_groups[0]) ||
1136 (egid != (gid_t)-1 && egid != oldcred->cr_rgid &&
1137 egid != oldcred->cr_svgid &&
1138 egid != oldcred->cr_groups[0]) ||
1139 (sgid != (gid_t)-1 && sgid != oldcred->cr_rgid &&
1140 sgid != oldcred->cr_svgid &&
1141 sgid != oldcred->cr_groups[0])) &&
1142 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESGID)) != 0)
1145 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
1146 change_egid(newcred, egid);
1149 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
1150 change_rgid(newcred, rgid);
1153 if (sgid != (gid_t)-1 && oldcred->cr_svgid != sgid) {
1154 change_svgid(newcred, sgid);
1157 proc_set_cred(p, newcred);
1168 #ifndef _SYS_SYSPROTO_H_
1169 struct getresuid_args {
1177 sys_getresuid(struct thread *td, struct getresuid_args *uap)
1180 int error1 = 0, error2 = 0, error3 = 0;
1182 cred = td->td_ucred;
1184 error1 = copyout(&cred->cr_ruid,
1185 uap->ruid, sizeof(cred->cr_ruid));
1187 error2 = copyout(&cred->cr_uid,
1188 uap->euid, sizeof(cred->cr_uid));
1190 error3 = copyout(&cred->cr_svuid,
1191 uap->suid, sizeof(cred->cr_svuid));
1192 return (error1 ? error1 : error2 ? error2 : error3);
1195 #ifndef _SYS_SYSPROTO_H_
1196 struct getresgid_args {
1204 sys_getresgid(struct thread *td, struct getresgid_args *uap)
1207 int error1 = 0, error2 = 0, error3 = 0;
1209 cred = td->td_ucred;
1211 error1 = copyout(&cred->cr_rgid,
1212 uap->rgid, sizeof(cred->cr_rgid));
1214 error2 = copyout(&cred->cr_groups[0],
1215 uap->egid, sizeof(cred->cr_groups[0]));
1217 error3 = copyout(&cred->cr_svgid,
1218 uap->sgid, sizeof(cred->cr_svgid));
1219 return (error1 ? error1 : error2 ? error2 : error3);
1222 #ifndef _SYS_SYSPROTO_H_
1223 struct issetugid_args {
1229 sys_issetugid(struct thread *td, struct issetugid_args *uap)
1231 struct proc *p = td->td_proc;
1234 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
1235 * we use P_SUGID because we consider changing the owners as
1236 * "tainting" as well.
1237 * This is significant for procs that start as root and "become"
1238 * a user without an exec - programs cannot know *everything*
1239 * that libc *might* have put in their data segment.
1241 td->td_retval[0] = (p->p_flag & P_SUGID) ? 1 : 0;
1246 sys___setugid(struct thread *td, struct __setugid_args *uap)
1252 switch (uap->flag) {
1255 p->p_flag &= ~P_SUGID;
1260 p->p_flag |= P_SUGID;
1266 #else /* !REGRESSION */
1269 #endif /* REGRESSION */
1273 * Check if gid is a member of the group set.
1276 groupmember(gid_t gid, struct ucred *cred)
1282 if (cred->cr_groups[0] == gid)
1286 * If gid was not our primary group, perform a binary search
1287 * of the supplemental groups. This is possible because we
1288 * sort the groups in crsetgroups().
1291 h = cred->cr_ngroups;
1293 m = l + ((h - l) / 2);
1294 if (cred->cr_groups[m] < gid)
1299 if ((l < cred->cr_ngroups) && (cred->cr_groups[l] == gid))
1306 * Test the active securelevel against a given level. securelevel_gt()
1307 * implements (securelevel > level). securelevel_ge() implements
1308 * (securelevel >= level). Note that the logic is inverted -- these
1309 * functions return EPERM on "success" and 0 on "failure".
1311 * Due to care taken when setting the securelevel, we know that no jail will
1312 * be less secure that its parent (or the physical system), so it is sufficient
1313 * to test the current jail only.
1315 * XXXRW: Possibly since this has to do with privilege, it should move to
1319 securelevel_gt(struct ucred *cr, int level)
1322 return (cr->cr_prison->pr_securelevel > level ? EPERM : 0);
1326 securelevel_ge(struct ucred *cr, int level)
1329 return (cr->cr_prison->pr_securelevel >= level ? EPERM : 0);
1333 * 'see_other_uids' determines whether or not visibility of processes
1334 * and sockets with credentials holding different real uids is possible
1335 * using a variety of system MIBs.
1336 * XXX: data declarations should be together near the beginning of the file.
1338 static int see_other_uids = 1;
1339 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_uids, CTLFLAG_RW,
1341 "Unprivileged processes may see subjects/objects with different real uid");
1344 * Determine if u1 "can see" the subject specified by u2, according to the
1345 * 'see_other_uids' policy.
1346 * Returns: 0 for permitted, ESRCH otherwise
1348 * References: *u1 and *u2 must not change during the call
1349 * u1 may equal u2, in which case only one reference is required
1352 cr_canseeotheruids(struct ucred *u1, struct ucred *u2)
1355 if (!see_other_uids && u1->cr_ruid != u2->cr_ruid) {
1356 if (priv_check_cred(u1, PRIV_SEEOTHERUIDS) != 0)
1363 * 'see_other_gids' determines whether or not visibility of processes
1364 * and sockets with credentials holding different real gids is possible
1365 * using a variety of system MIBs.
1366 * XXX: data declarations should be together near the beginning of the file.
1368 static int see_other_gids = 1;
1369 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_gids, CTLFLAG_RW,
1371 "Unprivileged processes may see subjects/objects with different real gid");
1374 * Determine if u1 can "see" the subject specified by u2, according to the
1375 * 'see_other_gids' policy.
1376 * Returns: 0 for permitted, ESRCH otherwise
1378 * References: *u1 and *u2 must not change during the call
1379 * u1 may equal u2, in which case only one reference is required
1382 cr_canseeothergids(struct ucred *u1, struct ucred *u2)
1386 if (!see_other_gids) {
1388 for (i = 0; i < u1->cr_ngroups; i++) {
1389 if (groupmember(u1->cr_groups[i], u2))
1395 if (priv_check_cred(u1, PRIV_SEEOTHERGIDS) != 0)
1403 * 'see_jail_proc' determines whether or not visibility of processes and
1404 * sockets with credentials holding different jail ids is possible using a
1405 * variety of system MIBs.
1407 * XXX: data declarations should be together near the beginning of the file.
1410 static int see_jail_proc = 1;
1411 SYSCTL_INT(_security_bsd, OID_AUTO, see_jail_proc, CTLFLAG_RW,
1413 "Unprivileged processes may see subjects/objects with different jail ids");
1416 * Determine if u1 "can see" the subject specified by u2, according to the
1417 * 'see_jail_proc' policy.
1418 * Returns: 0 for permitted, ESRCH otherwise
1420 * References: *u1 and *u2 must not change during the call
1421 * u1 may equal u2, in which case only one reference is required
1424 cr_canseejailproc(struct ucred *u1, struct ucred *u2)
1426 if (u1->cr_uid == 0)
1428 return (!see_jail_proc && u1->cr_prison != u2->cr_prison ? ESRCH : 0);
1432 * Determine if u1 "can see" the subject specified by u2.
1433 * Returns: 0 for permitted, an errno value otherwise
1435 * References: *u1 and *u2 must not change during the call
1436 * u1 may equal u2, in which case only one reference is required
1439 cr_cansee(struct ucred *u1, struct ucred *u2)
1443 if ((error = prison_check(u1, u2)))
1446 if ((error = mac_cred_check_visible(u1, u2)))
1449 if ((error = cr_canseeotheruids(u1, u2)))
1451 if ((error = cr_canseeothergids(u1, u2)))
1453 if ((error = cr_canseejailproc(u1, u2)))
1459 * Determine if td "can see" the subject specified by p.
1460 * Returns: 0 for permitted, an errno value otherwise
1461 * Locks: Sufficient locks to protect p->p_ucred must be held. td really
1462 * should be curthread.
1463 * References: td and p must be valid for the lifetime of the call
1466 p_cansee(struct thread *td, struct proc *p)
1469 /* Wrap cr_cansee() for all functionality. */
1470 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1471 PROC_LOCK_ASSERT(p, MA_OWNED);
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)
1652 val = prison_allow(req->td->td_ucred, PR_ALLOW_UNPRIV_DEBUG) != 0;
1653 error = sysctl_handle_int(oidp, &val, 0, req);
1654 if (error != 0 || req->newptr == NULL)
1656 pr = req->td->td_ucred->cr_prison;
1657 mtx_lock(&pr->pr_mtx);
1660 pr->pr_allow &= ~(PR_ALLOW_UNPRIV_DEBUG);
1663 pr->pr_allow |= PR_ALLOW_UNPRIV_DEBUG;
1668 mtx_unlock(&pr->pr_mtx);
1674 * The 'unprivileged_proc_debug' flag may be used to disable a variety of
1675 * unprivileged inter-process debugging services, including some procfs
1676 * functionality, ptrace(), and ktrace(). In the past, inter-process
1677 * debugging has been involved in a variety of security problems, and sites
1678 * not requiring the service might choose to disable it when hardening
1681 SYSCTL_PROC(_security_bsd, OID_AUTO, unprivileged_proc_debug,
1682 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_SECURE |
1683 CTLFLAG_MPSAFE, 0, 0, sysctl_unprivileged_proc_debug, "I",
1684 "Unprivileged processes may use process debugging facilities");
1687 * Determine whether td may debug p.
1688 * Returns: 0 for permitted, an errno value otherwise
1689 * Locks: Sufficient locks to protect various components of td and p
1690 * must be held. td must be curthread, and a lock must
1692 * References: td and p must be valid for the lifetime of the call
1695 p_candebug(struct thread *td, struct proc *p)
1697 int credentialchanged, error, grpsubset, i, uidsubset;
1699 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1700 PROC_LOCK_ASSERT(p, MA_OWNED);
1701 if ((error = priv_check(td, PRIV_DEBUG_UNPRIV)))
1703 if (td->td_proc == p)
1705 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1708 if ((error = mac_proc_check_debug(td->td_ucred, p)))
1711 if ((error = cr_canseeotheruids(td->td_ucred, p->p_ucred)))
1713 if ((error = cr_canseeothergids(td->td_ucred, p->p_ucred)))
1717 * Is p's group set a subset of td's effective group set? This
1718 * includes p's egid, group access list, rgid, and svgid.
1721 for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
1722 if (!groupmember(p->p_ucred->cr_groups[i], td->td_ucred)) {
1727 grpsubset = grpsubset &&
1728 groupmember(p->p_ucred->cr_rgid, td->td_ucred) &&
1729 groupmember(p->p_ucred->cr_svgid, td->td_ucred);
1732 * Are the uids present in p's credential equal to td's
1733 * effective uid? This includes p's euid, svuid, and ruid.
1735 uidsubset = (td->td_ucred->cr_uid == p->p_ucred->cr_uid &&
1736 td->td_ucred->cr_uid == p->p_ucred->cr_svuid &&
1737 td->td_ucred->cr_uid == p->p_ucred->cr_ruid);
1740 * Has the credential of the process changed since the last exec()?
1742 credentialchanged = (p->p_flag & P_SUGID);
1745 * If p's gids aren't a subset, or the uids aren't a subset,
1746 * or the credential has changed, require appropriate privilege
1747 * for td to debug p.
1749 if (!grpsubset || !uidsubset) {
1750 error = priv_check(td, PRIV_DEBUG_DIFFCRED);
1755 if (credentialchanged) {
1756 error = priv_check(td, PRIV_DEBUG_SUGID);
1761 /* Can't trace init when securelevel > 0. */
1762 if (p == initproc) {
1763 error = securelevel_gt(td->td_ucred, 0);
1769 * Can't trace a process that's currently exec'ing.
1771 * XXX: Note, this is not a security policy decision, it's a
1772 * basic correctness/functionality decision. Therefore, this check
1773 * should be moved to the caller's of p_candebug().
1775 if ((p->p_flag & P_INEXEC) != 0)
1778 /* Denied explicitely */
1779 if ((p->p_flag2 & P2_NOTRACE) != 0) {
1780 error = priv_check(td, PRIV_DEBUG_DENIED);
1789 * Determine whether the subject represented by cred can "see" a socket.
1790 * Returns: 0 for permitted, ENOENT otherwise.
1793 cr_canseesocket(struct ucred *cred, struct socket *so)
1797 error = prison_check(cred, so->so_cred);
1801 error = mac_socket_check_visible(cred, so);
1805 if (cr_canseeotheruids(cred, so->so_cred))
1807 if (cr_canseeothergids(cred, so->so_cred))
1814 * Determine whether td can wait for the exit of p.
1815 * Returns: 0 for permitted, an errno value otherwise
1816 * Locks: Sufficient locks to protect various components of td and p
1817 * must be held. td must be curthread, and a lock must
1819 * References: td and p must be valid for the lifetime of the call
1823 p_canwait(struct thread *td, struct proc *p)
1827 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1828 PROC_LOCK_ASSERT(p, MA_OWNED);
1829 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1832 if ((error = mac_proc_check_wait(td->td_ucred, p)))
1836 /* XXXMAC: This could have odd effects on some shells. */
1837 if ((error = cr_canseeotheruids(td->td_ucred, p->p_ucred)))
1845 * Credential management.
1847 * struct ucred objects are rarely allocated but gain and lose references all
1848 * the time (e.g., on struct file alloc/dealloc) turning refcount updates into
1849 * a significant source of cache-line ping ponging. Common cases are worked
1850 * around by modifying thread-local counter instead if the cred to operate on
1851 * matches td_realucred.
1853 * The counter is split into 2 parts:
1854 * - cr_users -- total count of all struct proc and struct thread objects
1855 * which have given cred in p_ucred and td_ucred respectively
1856 * - cr_ref -- the actual ref count, only valid if cr_users == 0
1858 * If users == 0 then cr_ref behaves similarly to refcount(9), in particular if
1859 * the count reaches 0 the object is freeable.
1860 * If users > 0 and curthread->td_realucred == cred, then updates are performed
1861 * against td_ucredref.
1862 * In other cases updates are performed against cr_ref.
1864 * Changing td_realucred into something else decrements cr_users and transfers
1865 * accumulated updates.
1868 crcowget(struct ucred *cr)
1871 mtx_lock(&cr->cr_mtx);
1872 KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
1873 __func__, cr->cr_users, cr));
1876 mtx_unlock(&cr->cr_mtx);
1880 static struct ucred *
1881 crunuse(struct thread *td)
1883 struct ucred *cr, *crold;
1885 MPASS(td->td_realucred == td->td_ucred);
1886 cr = td->td_realucred;
1887 mtx_lock(&cr->cr_mtx);
1888 cr->cr_ref += td->td_ucredref;
1889 td->td_ucredref = 0;
1890 KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
1891 __func__, cr->cr_users, cr));
1893 if (cr->cr_users == 0) {
1894 KASSERT(cr->cr_ref > 0, ("%s: ref %d not > 0 on cred %p",
1895 __func__, cr->cr_ref, cr));
1901 mtx_unlock(&cr->cr_mtx);
1902 td->td_realucred = NULL;
1907 crunusebatch(struct ucred *cr, int users, int ref)
1910 KASSERT(users > 0, ("%s: passed users %d not > 0 ; cred %p",
1911 __func__, users, cr));
1912 mtx_lock(&cr->cr_mtx);
1913 KASSERT(cr->cr_users >= users, ("%s: users %d not > %d on cred %p",
1914 __func__, cr->cr_users, users, cr));
1915 cr->cr_users -= users;
1917 cr->cr_ref -= users;
1918 if (cr->cr_users > 0) {
1919 mtx_unlock(&cr->cr_mtx);
1922 KASSERT(cr->cr_ref >= 0, ("%s: ref %d not >= 0 on cred %p",
1923 __func__, cr->cr_ref, cr));
1924 if (cr->cr_ref > 0) {
1925 mtx_unlock(&cr->cr_mtx);
1932 crcowfree(struct thread *td)
1946 struct ucred *crnew, *crold;
1950 PROC_LOCK_ASSERT(p, MA_OWNED);
1952 MPASS(td->td_realucred == td->td_ucred);
1953 if (td->td_realucred == p->p_ucred)
1956 crnew = crcowget(p->p_ucred);
1957 crold = crunuse(td);
1958 td->td_realucred = crnew;
1959 td->td_ucred = td->td_realucred;
1967 credbatch_add(struct credbatch *crb, struct thread *td)
1971 MPASS(td->td_realucred != NULL);
1972 MPASS(td->td_realucred == td->td_ucred);
1973 MPASS(td->td_state == TDS_INACTIVE);
1974 cr = td->td_realucred;
1975 KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
1976 __func__, cr->cr_users, cr));
1977 if (crb->cred != cr) {
1978 if (crb->users > 0) {
1979 MPASS(crb->cred != NULL);
1980 crunusebatch(crb->cred, crb->users, crb->ref);
1987 crb->ref += td->td_ucredref;
1988 td->td_ucredref = 0;
1989 td->td_realucred = NULL;
1993 credbatch_final(struct credbatch *crb)
1996 MPASS(crb->cred != NULL);
1997 MPASS(crb->users > 0);
1998 crunusebatch(crb->cred, crb->users, crb->ref);
2002 * Allocate a zeroed cred structure.
2009 cr = malloc(sizeof(*cr), M_CRED, M_WAITOK | M_ZERO);
2010 mtx_init(&cr->cr_mtx, "cred", NULL, MTX_DEF);
2013 audit_cred_init(cr);
2018 cr->cr_groups = cr->cr_smallgroups;
2020 sizeof(cr->cr_smallgroups) / sizeof(cr->cr_smallgroups[0]);
2025 * Claim another reference to a ucred structure.
2028 crhold(struct ucred *cr)
2033 if (__predict_true(td->td_realucred == cr)) {
2034 KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
2035 __func__, cr->cr_users, cr));
2039 mtx_lock(&cr->cr_mtx);
2041 mtx_unlock(&cr->cr_mtx);
2046 * Free a cred structure. Throws away space when ref count gets to 0.
2049 crfree(struct ucred *cr)
2054 if (__predict_true(td->td_realucred == cr)) {
2055 KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
2056 __func__, cr->cr_users, cr));
2060 mtx_lock(&cr->cr_mtx);
2061 KASSERT(cr->cr_users >= 0, ("%s: users %d not >= 0 on cred %p",
2062 __func__, cr->cr_users, cr));
2064 if (cr->cr_users > 0) {
2065 mtx_unlock(&cr->cr_mtx);
2068 KASSERT(cr->cr_ref >= 0, ("%s: ref %d not >= 0 on cred %p",
2069 __func__, cr->cr_ref, cr));
2070 if (cr->cr_ref > 0) {
2071 mtx_unlock(&cr->cr_mtx);
2078 crfree_final(struct ucred *cr)
2081 KASSERT(cr->cr_users == 0, ("%s: users %d not == 0 on cred %p",
2082 __func__, cr->cr_users, cr));
2083 KASSERT(cr->cr_ref == 0, ("%s: ref %d not == 0 on cred %p",
2084 __func__, cr->cr_ref, cr));
2087 * Some callers of crget(), such as nfs_statfs(), allocate a temporary
2088 * credential, but don't allocate a uidinfo structure.
2090 if (cr->cr_uidinfo != NULL)
2091 uifree(cr->cr_uidinfo);
2092 if (cr->cr_ruidinfo != NULL)
2093 uifree(cr->cr_ruidinfo);
2094 if (cr->cr_prison != NULL)
2095 prison_free(cr->cr_prison);
2096 if (cr->cr_loginclass != NULL)
2097 loginclass_free(cr->cr_loginclass);
2099 audit_cred_destroy(cr);
2102 mac_cred_destroy(cr);
2104 mtx_destroy(&cr->cr_mtx);
2105 if (cr->cr_groups != cr->cr_smallgroups)
2106 free(cr->cr_groups, M_CRED);
2111 * Copy a ucred's contents from a template. Does not block.
2114 crcopy(struct ucred *dest, struct ucred *src)
2117 KASSERT(dest->cr_ref == 1, ("crcopy of shared ucred"));
2118 bcopy(&src->cr_startcopy, &dest->cr_startcopy,
2119 (unsigned)((caddr_t)&src->cr_endcopy -
2120 (caddr_t)&src->cr_startcopy));
2121 crsetgroups(dest, src->cr_ngroups, src->cr_groups);
2122 uihold(dest->cr_uidinfo);
2123 uihold(dest->cr_ruidinfo);
2124 prison_hold(dest->cr_prison);
2125 loginclass_hold(dest->cr_loginclass);
2127 audit_cred_copy(src, dest);
2130 mac_cred_copy(src, dest);
2135 * Dup cred struct to a new held one.
2138 crdup(struct ucred *cr)
2140 struct ucred *newcr;
2148 * Fill in a struct xucred based on a struct ucred.
2151 cru2x(struct ucred *cr, struct xucred *xcr)
2155 bzero(xcr, sizeof(*xcr));
2156 xcr->cr_version = XUCRED_VERSION;
2157 xcr->cr_uid = cr->cr_uid;
2159 ngroups = MIN(cr->cr_ngroups, XU_NGROUPS);
2160 xcr->cr_ngroups = ngroups;
2161 bcopy(cr->cr_groups, xcr->cr_groups,
2162 ngroups * sizeof(*cr->cr_groups));
2166 cru2xt(struct thread *td, struct xucred *xcr)
2169 cru2x(td->td_ucred, xcr);
2170 xcr->cr_pid = td->td_proc->p_pid;
2174 * Set initial process credentials.
2175 * Callers are responsible for providing the reference for provided credentials.
2178 proc_set_cred_init(struct proc *p, struct ucred *newcred)
2181 p->p_ucred = crcowget(newcred);
2185 * Change process credentials.
2186 * Callers are responsible for providing the reference for passed credentials
2187 * and for freeing old ones.
2189 * Process has to be locked except when it does not have credentials (as it
2190 * should not be visible just yet) or when newcred is NULL (as this can be
2191 * only used when the process is about to be freed, at which point it should
2192 * not be visible anymore).
2195 proc_set_cred(struct proc *p, struct ucred *newcred)
2201 PROC_LOCK_ASSERT(p, MA_OWNED);
2202 KASSERT(newcred->cr_users == 0, ("%s: users %d not 0 on cred %p",
2203 __func__, newcred->cr_users, newcred));
2204 mtx_lock(&cr->cr_mtx);
2205 KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
2206 __func__, cr->cr_users, cr));
2208 mtx_unlock(&cr->cr_mtx);
2209 p->p_ucred = newcred;
2210 newcred->cr_users = 1;
2215 proc_unset_cred(struct proc *p)
2219 MPASS(p->p_state == PRS_ZOMBIE || p->p_state == PRS_NEW);
2222 KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
2223 __func__, cr->cr_users, cr));
2224 mtx_lock(&cr->cr_mtx);
2226 if (cr->cr_users == 0)
2227 KASSERT(cr->cr_ref > 0, ("%s: ref %d not > 0 on cred %p",
2228 __func__, cr->cr_ref, cr));
2229 mtx_unlock(&cr->cr_mtx);
2234 crcopysafe(struct proc *p, struct ucred *cr)
2236 struct ucred *oldcred;
2239 PROC_LOCK_ASSERT(p, MA_OWNED);
2241 oldcred = p->p_ucred;
2242 while (cr->cr_agroups < oldcred->cr_agroups) {
2243 groups = oldcred->cr_agroups;
2245 crextend(cr, groups);
2247 oldcred = p->p_ucred;
2249 crcopy(cr, oldcred);
2255 * Extend the passed in credential to hold n items.
2258 crextend(struct ucred *cr, int n)
2263 if (n <= cr->cr_agroups)
2267 * We extend by 2 each time since we're using a power of two
2268 * allocator until we need enough groups to fill a page.
2269 * Once we're allocating multiple pages, only allocate as many
2270 * as we actually need. The case of processes needing a
2271 * non-power of two number of pages seems more likely than
2272 * a real world process that adds thousands of groups one at a
2275 if ( n < PAGE_SIZE / sizeof(gid_t) ) {
2276 if (cr->cr_agroups == 0)
2277 cnt = MAX(1, MINALLOCSIZE / sizeof(gid_t));
2279 cnt = cr->cr_agroups * 2;
2284 cnt = roundup2(n, PAGE_SIZE / sizeof(gid_t));
2286 /* Free the old array. */
2287 if (cr->cr_groups != cr->cr_smallgroups)
2288 free(cr->cr_groups, M_CRED);
2290 cr->cr_groups = malloc(cnt * sizeof(gid_t), M_CRED, M_WAITOK | M_ZERO);
2291 cr->cr_agroups = cnt;
2295 * Copy groups in to a credential, preserving any necessary invariants.
2296 * Currently this includes the sorting of all supplemental gids.
2297 * crextend() must have been called before hand to ensure sufficient
2298 * space is available.
2301 crsetgroups_locked(struct ucred *cr, int ngrp, gid_t *groups)
2307 KASSERT(cr->cr_agroups >= ngrp, ("cr_ngroups is too small"));
2309 bcopy(groups, cr->cr_groups, ngrp * sizeof(gid_t));
2310 cr->cr_ngroups = ngrp;
2313 * Sort all groups except cr_groups[0] to allow groupmember to
2314 * perform a binary search.
2316 * XXX: If large numbers of groups become common this should
2317 * be replaced with shell sort like linux uses or possibly
2320 for (i = 2; i < ngrp; i++) {
2321 g = cr->cr_groups[i];
2322 for (j = i-1; j >= 1 && g < cr->cr_groups[j]; j--)
2323 cr->cr_groups[j + 1] = cr->cr_groups[j];
2324 cr->cr_groups[j + 1] = g;
2329 * Copy groups in to a credential after expanding it if required.
2330 * Truncate the list to (ngroups_max + 1) if it is too large.
2333 crsetgroups(struct ucred *cr, int ngrp, gid_t *groups)
2336 if (ngrp > ngroups_max + 1)
2337 ngrp = ngroups_max + 1;
2340 crsetgroups_locked(cr, ngrp, groups);
2344 * Get login name, if available.
2346 #ifndef _SYS_SYSPROTO_H_
2347 struct getlogin_args {
2354 sys_getlogin(struct thread *td, struct getlogin_args *uap)
2356 char login[MAXLOGNAME];
2357 struct proc *p = td->td_proc;
2360 if (uap->namelen > MAXLOGNAME)
2361 uap->namelen = MAXLOGNAME;
2363 SESS_LOCK(p->p_session);
2364 len = strlcpy(login, p->p_session->s_login, uap->namelen) + 1;
2365 SESS_UNLOCK(p->p_session);
2367 if (len > uap->namelen)
2369 return (copyout(login, uap->namebuf, len));
2375 #ifndef _SYS_SYSPROTO_H_
2376 struct setlogin_args {
2382 sys_setlogin(struct thread *td, struct setlogin_args *uap)
2384 struct proc *p = td->td_proc;
2386 char logintmp[MAXLOGNAME];
2388 CTASSERT(sizeof(p->p_session->s_login) >= sizeof(logintmp));
2390 error = priv_check(td, PRIV_PROC_SETLOGIN);
2393 error = copyinstr(uap->namebuf, logintmp, sizeof(logintmp), NULL);
2395 if (error == ENAMETOOLONG)
2399 AUDIT_ARG_LOGIN(logintmp);
2401 SESS_LOCK(p->p_session);
2402 strcpy(p->p_session->s_login, logintmp);
2403 SESS_UNLOCK(p->p_session);
2409 setsugid(struct proc *p)
2412 PROC_LOCK_ASSERT(p, MA_OWNED);
2413 p->p_flag |= P_SUGID;
2417 * Change a process's effective uid.
2418 * Side effects: newcred->cr_uid and newcred->cr_uidinfo will be modified.
2419 * References: newcred must be an exclusive credential reference for the
2420 * duration of the call.
2423 change_euid(struct ucred *newcred, struct uidinfo *euip)
2426 newcred->cr_uid = euip->ui_uid;
2428 uifree(newcred->cr_uidinfo);
2429 newcred->cr_uidinfo = euip;
2433 * Change a process's effective gid.
2434 * Side effects: newcred->cr_gid will be modified.
2435 * References: newcred must be an exclusive credential reference for the
2436 * duration of the call.
2439 change_egid(struct ucred *newcred, gid_t egid)
2442 newcred->cr_groups[0] = egid;
2446 * Change a process's real uid.
2447 * Side effects: newcred->cr_ruid will be updated, newcred->cr_ruidinfo
2448 * will be updated, and the old and new cr_ruidinfo proc
2449 * counts will be updated.
2450 * References: newcred must be an exclusive credential reference for the
2451 * duration of the call.
2454 change_ruid(struct ucred *newcred, struct uidinfo *ruip)
2457 (void)chgproccnt(newcred->cr_ruidinfo, -1, 0);
2458 newcred->cr_ruid = ruip->ui_uid;
2460 uifree(newcred->cr_ruidinfo);
2461 newcred->cr_ruidinfo = ruip;
2462 (void)chgproccnt(newcred->cr_ruidinfo, 1, 0);
2466 * Change a process's real gid.
2467 * Side effects: newcred->cr_rgid will be updated.
2468 * References: newcred must be an exclusive credential reference for the
2469 * duration of the call.
2472 change_rgid(struct ucred *newcred, gid_t rgid)
2475 newcred->cr_rgid = rgid;
2479 * Change a process's saved uid.
2480 * Side effects: newcred->cr_svuid will be updated.
2481 * References: newcred must be an exclusive credential reference for the
2482 * duration of the call.
2485 change_svuid(struct ucred *newcred, uid_t svuid)
2488 newcred->cr_svuid = svuid;
2492 * Change a process's saved gid.
2493 * Side effects: newcred->cr_svgid will be updated.
2494 * References: newcred must be an exclusive credential reference for the
2495 * duration of the call.
2498 change_svgid(struct ucred *newcred, gid_t svgid)
2501 newcred->cr_svgid = svgid;