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 crsetgroups_locked(struct ucred *cr, int ngrp,
92 #ifndef _SYS_SYSPROTO_H_
99 sys_getpid(struct thread *td, struct getpid_args *uap)
101 struct proc *p = td->td_proc;
103 td->td_retval[0] = p->p_pid;
104 #if defined(COMPAT_43)
105 if (SV_PROC_FLAG(p, SV_AOUT))
106 td->td_retval[1] = kern_getppid(td);
111 #ifndef _SYS_SYSPROTO_H_
112 struct getppid_args {
118 sys_getppid(struct thread *td, struct getppid_args *uap)
121 td->td_retval[0] = kern_getppid(td);
126 kern_getppid(struct thread *td)
128 struct proc *p = td->td_proc;
134 * Get process group ID; note that POSIX getpgrp takes no parameter.
136 #ifndef _SYS_SYSPROTO_H_
137 struct getpgrp_args {
142 sys_getpgrp(struct thread *td, struct getpgrp_args *uap)
144 struct proc *p = td->td_proc;
147 td->td_retval[0] = p->p_pgrp->pg_id;
152 /* Get an arbitrary pid's process group id */
153 #ifndef _SYS_SYSPROTO_H_
154 struct getpgid_args {
159 sys_getpgid(struct thread *td, struct getpgid_args *uap)
171 error = p_cansee(td, p);
177 td->td_retval[0] = p->p_pgrp->pg_id;
183 * Get an arbitrary pid's session id.
185 #ifndef _SYS_SYSPROTO_H_
191 sys_getsid(struct thread *td, struct getsid_args *uap)
194 return (kern_getsid(td, uap->pid));
198 kern_getsid(struct thread *td, pid_t pid)
210 error = p_cansee(td, p);
216 td->td_retval[0] = p->p_session->s_sid;
221 #ifndef _SYS_SYSPROTO_H_
228 sys_getuid(struct thread *td, struct getuid_args *uap)
231 td->td_retval[0] = td->td_ucred->cr_ruid;
232 #if defined(COMPAT_43)
233 td->td_retval[1] = td->td_ucred->cr_uid;
238 #ifndef _SYS_SYSPROTO_H_
239 struct geteuid_args {
245 sys_geteuid(struct thread *td, struct geteuid_args *uap)
248 td->td_retval[0] = td->td_ucred->cr_uid;
252 #ifndef _SYS_SYSPROTO_H_
259 sys_getgid(struct thread *td, struct getgid_args *uap)
262 td->td_retval[0] = td->td_ucred->cr_rgid;
263 #if defined(COMPAT_43)
264 td->td_retval[1] = td->td_ucred->cr_groups[0];
270 * Get effective group ID. The "egid" is groups[0], and could be obtained
271 * via getgroups. This syscall exists because it is somewhat painful to do
272 * correctly in a library function.
274 #ifndef _SYS_SYSPROTO_H_
275 struct getegid_args {
281 sys_getegid(struct thread *td, struct getegid_args *uap)
284 td->td_retval[0] = td->td_ucred->cr_groups[0];
288 #ifndef _SYS_SYSPROTO_H_
289 struct getgroups_args {
295 sys_getgroups(struct thread *td, struct getgroups_args *uap)
302 ngrp = cred->cr_ngroups;
304 if (uap->gidsetsize == 0) {
308 if (uap->gidsetsize < ngrp)
311 error = copyout(cred->cr_groups, uap->gidset, ngrp * sizeof(gid_t));
313 td->td_retval[0] = ngrp;
317 #ifndef _SYS_SYSPROTO_H_
324 sys_setsid(struct thread *td, struct setsid_args *uap)
328 struct proc *p = td->td_proc;
329 struct pgrp *newpgrp;
330 struct session *newsess;
335 newpgrp = malloc(sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
336 newsess = malloc(sizeof(struct session), M_SESSION, M_WAITOK | M_ZERO);
338 sx_xlock(&proctree_lock);
340 if (p->p_pgid == p->p_pid || (pgrp = pgfind(p->p_pid)) != NULL) {
345 (void)enterpgrp(p, p->p_pid, newpgrp, newsess);
346 td->td_retval[0] = p->p_pid;
351 sx_xunlock(&proctree_lock);
354 free(newpgrp, M_PGRP);
356 free(newsess, M_SESSION);
362 * set process group (setpgid/old setpgrp)
364 * caller does setpgid(targpid, targpgid)
366 * pid must be caller or child of caller (ESRCH)
368 * pid must be in same session (EPERM)
369 * pid can't have done an exec (EACCES)
371 * there must exist some pid in same session having pgid (EPERM)
372 * pid must not be session leader (EPERM)
374 #ifndef _SYS_SYSPROTO_H_
375 struct setpgid_args {
376 int pid; /* target process id */
377 int pgid; /* target pgrp id */
382 sys_setpgid(struct thread *td, struct setpgid_args *uap)
384 struct proc *curp = td->td_proc;
385 struct proc *targp; /* target process */
386 struct pgrp *pgrp; /* target pgrp */
388 struct pgrp *newpgrp;
395 newpgrp = malloc(sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
397 sx_xlock(&proctree_lock);
398 if (uap->pid != 0 && uap->pid != curp->p_pid) {
399 if ((targp = pfind(uap->pid)) == NULL) {
403 if (!inferior(targp)) {
408 if ((error = p_cansee(td, targp))) {
412 if (targp->p_pgrp == NULL ||
413 targp->p_session != curp->p_session) {
418 if (targp->p_flag & P_EXEC) {
426 if (SESS_LEADER(targp)) {
431 uap->pgid = targp->p_pid;
432 if ((pgrp = pgfind(uap->pgid)) == NULL) {
433 if (uap->pgid == targp->p_pid) {
434 error = enterpgrp(targp, uap->pgid, newpgrp,
441 if (pgrp == targp->p_pgrp) {
445 if (pgrp->pg_id != targp->p_pid &&
446 pgrp->pg_session != curp->p_session) {
452 error = enterthispgrp(targp, pgrp);
455 sx_xunlock(&proctree_lock);
456 KASSERT((error == 0) || (newpgrp != NULL),
457 ("setpgid failed and newpgrp is NULL"));
459 free(newpgrp, M_PGRP);
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];
809 gidsetsize = uap->gidsetsize;
810 if (gidsetsize > ngroups_max + 1)
813 if (gidsetsize > XU_NGROUPS)
814 groups = malloc(gidsetsize * sizeof(gid_t), M_TEMP, M_WAITOK);
816 groups = smallgroups;
818 error = copyin(uap->gidset, groups, gidsetsize * sizeof(gid_t));
820 error = kern_setgroups(td, gidsetsize, groups);
822 if (gidsetsize > XU_NGROUPS)
823 free(groups, M_TEMP);
828 kern_setgroups(struct thread *td, u_int ngrp, gid_t *groups)
830 struct proc *p = td->td_proc;
831 struct ucred *newcred, *oldcred;
834 MPASS(ngrp <= ngroups_max + 1);
835 AUDIT_ARG_GROUPSET(groups, ngrp);
837 crextend(newcred, ngrp);
839 oldcred = crcopysafe(p, newcred);
842 error = mac_cred_check_setgroups(oldcred, ngrp, groups);
847 error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS);
853 * setgroups(0, NULL) is a legitimate way of clearing the
854 * groups vector on non-BSD systems (which generally do not
855 * have the egid in the groups[0]). We risk security holes
856 * when running non-BSD software if we do not do the same.
858 newcred->cr_ngroups = 1;
860 crsetgroups_locked(newcred, ngrp, groups);
863 proc_set_cred(p, newcred);
874 #ifndef _SYS_SYSPROTO_H_
875 struct setreuid_args {
882 sys_setreuid(struct thread *td, struct setreuid_args *uap)
884 struct proc *p = td->td_proc;
885 struct ucred *newcred, *oldcred;
887 struct uidinfo *euip, *ruip;
892 AUDIT_ARG_EUID(euid);
893 AUDIT_ARG_RUID(ruid);
898 oldcred = crcopysafe(p, newcred);
901 error = mac_cred_check_setreuid(oldcred, ruid, euid);
906 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
907 ruid != oldcred->cr_svuid) ||
908 (euid != (uid_t)-1 && euid != oldcred->cr_uid &&
909 euid != oldcred->cr_ruid && euid != oldcred->cr_svuid)) &&
910 (error = priv_check_cred(oldcred, PRIV_CRED_SETREUID)) != 0)
913 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
914 change_euid(newcred, euip);
917 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
918 change_ruid(newcred, ruip);
921 if ((ruid != (uid_t)-1 || newcred->cr_uid != newcred->cr_ruid) &&
922 newcred->cr_svuid != newcred->cr_uid) {
923 change_svuid(newcred, newcred->cr_uid);
926 proc_set_cred(p, newcred);
928 racct_proc_ucred_changed(p, oldcred, newcred);
933 rctl_proc_ucred_changed(p, newcred);
949 #ifndef _SYS_SYSPROTO_H_
950 struct setregid_args {
957 sys_setregid(struct thread *td, struct setregid_args *uap)
959 struct proc *p = td->td_proc;
960 struct ucred *newcred, *oldcred;
966 AUDIT_ARG_EGID(egid);
967 AUDIT_ARG_RGID(rgid);
970 oldcred = crcopysafe(p, newcred);
973 error = mac_cred_check_setregid(oldcred, rgid, egid);
978 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
979 rgid != oldcred->cr_svgid) ||
980 (egid != (gid_t)-1 && egid != oldcred->cr_groups[0] &&
981 egid != oldcred->cr_rgid && egid != oldcred->cr_svgid)) &&
982 (error = priv_check_cred(oldcred, PRIV_CRED_SETREGID)) != 0)
985 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
986 change_egid(newcred, egid);
989 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
990 change_rgid(newcred, rgid);
993 if ((rgid != (gid_t)-1 || newcred->cr_groups[0] != newcred->cr_rgid) &&
994 newcred->cr_svgid != newcred->cr_groups[0]) {
995 change_svgid(newcred, newcred->cr_groups[0]);
998 proc_set_cred(p, newcred);
1010 * setresuid(ruid, euid, suid) is like setreuid except control over the saved
1013 #ifndef _SYS_SYSPROTO_H_
1014 struct setresuid_args {
1022 sys_setresuid(struct thread *td, struct setresuid_args *uap)
1024 struct proc *p = td->td_proc;
1025 struct ucred *newcred, *oldcred;
1026 uid_t euid, ruid, suid;
1027 struct uidinfo *euip, *ruip;
1033 AUDIT_ARG_EUID(euid);
1034 AUDIT_ARG_RUID(ruid);
1035 AUDIT_ARG_SUID(suid);
1037 euip = uifind(euid);
1038 ruip = uifind(ruid);
1040 oldcred = crcopysafe(p, newcred);
1043 error = mac_cred_check_setresuid(oldcred, ruid, euid, suid);
1048 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
1049 ruid != oldcred->cr_svuid &&
1050 ruid != oldcred->cr_uid) ||
1051 (euid != (uid_t)-1 && euid != oldcred->cr_ruid &&
1052 euid != oldcred->cr_svuid &&
1053 euid != oldcred->cr_uid) ||
1054 (suid != (uid_t)-1 && suid != oldcred->cr_ruid &&
1055 suid != oldcred->cr_svuid &&
1056 suid != oldcred->cr_uid)) &&
1057 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESUID)) != 0)
1060 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
1061 change_euid(newcred, euip);
1064 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
1065 change_ruid(newcred, ruip);
1068 if (suid != (uid_t)-1 && oldcred->cr_svuid != suid) {
1069 change_svuid(newcred, suid);
1072 proc_set_cred(p, newcred);
1074 racct_proc_ucred_changed(p, oldcred, newcred);
1079 rctl_proc_ucred_changed(p, newcred);
1097 * setresgid(rgid, egid, sgid) is like setregid except control over the saved
1100 #ifndef _SYS_SYSPROTO_H_
1101 struct setresgid_args {
1109 sys_setresgid(struct thread *td, struct setresgid_args *uap)
1111 struct proc *p = td->td_proc;
1112 struct ucred *newcred, *oldcred;
1113 gid_t egid, rgid, sgid;
1119 AUDIT_ARG_EGID(egid);
1120 AUDIT_ARG_RGID(rgid);
1121 AUDIT_ARG_SGID(sgid);
1124 oldcred = crcopysafe(p, newcred);
1127 error = mac_cred_check_setresgid(oldcred, rgid, egid, sgid);
1132 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
1133 rgid != oldcred->cr_svgid &&
1134 rgid != oldcred->cr_groups[0]) ||
1135 (egid != (gid_t)-1 && egid != oldcred->cr_rgid &&
1136 egid != oldcred->cr_svgid &&
1137 egid != oldcred->cr_groups[0]) ||
1138 (sgid != (gid_t)-1 && sgid != oldcred->cr_rgid &&
1139 sgid != oldcred->cr_svgid &&
1140 sgid != oldcred->cr_groups[0])) &&
1141 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESGID)) != 0)
1144 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
1145 change_egid(newcred, egid);
1148 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
1149 change_rgid(newcred, rgid);
1152 if (sgid != (gid_t)-1 && oldcred->cr_svgid != sgid) {
1153 change_svgid(newcred, sgid);
1156 proc_set_cred(p, newcred);
1167 #ifndef _SYS_SYSPROTO_H_
1168 struct getresuid_args {
1176 sys_getresuid(struct thread *td, struct getresuid_args *uap)
1179 int error1 = 0, error2 = 0, error3 = 0;
1181 cred = td->td_ucred;
1183 error1 = copyout(&cred->cr_ruid,
1184 uap->ruid, sizeof(cred->cr_ruid));
1186 error2 = copyout(&cred->cr_uid,
1187 uap->euid, sizeof(cred->cr_uid));
1189 error3 = copyout(&cred->cr_svuid,
1190 uap->suid, sizeof(cred->cr_svuid));
1191 return (error1 ? error1 : error2 ? error2 : error3);
1194 #ifndef _SYS_SYSPROTO_H_
1195 struct getresgid_args {
1203 sys_getresgid(struct thread *td, struct getresgid_args *uap)
1206 int error1 = 0, error2 = 0, error3 = 0;
1208 cred = td->td_ucred;
1210 error1 = copyout(&cred->cr_rgid,
1211 uap->rgid, sizeof(cred->cr_rgid));
1213 error2 = copyout(&cred->cr_groups[0],
1214 uap->egid, sizeof(cred->cr_groups[0]));
1216 error3 = copyout(&cred->cr_svgid,
1217 uap->sgid, sizeof(cred->cr_svgid));
1218 return (error1 ? error1 : error2 ? error2 : error3);
1221 #ifndef _SYS_SYSPROTO_H_
1222 struct issetugid_args {
1228 sys_issetugid(struct thread *td, struct issetugid_args *uap)
1230 struct proc *p = td->td_proc;
1233 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
1234 * we use P_SUGID because we consider changing the owners as
1235 * "tainting" as well.
1236 * This is significant for procs that start as root and "become"
1237 * a user without an exec - programs cannot know *everything*
1238 * that libc *might* have put in their data segment.
1240 td->td_retval[0] = (p->p_flag & P_SUGID) ? 1 : 0;
1245 sys___setugid(struct thread *td, struct __setugid_args *uap)
1251 switch (uap->flag) {
1254 p->p_flag &= ~P_SUGID;
1259 p->p_flag |= P_SUGID;
1265 #else /* !REGRESSION */
1268 #endif /* REGRESSION */
1272 * Check if gid is a member of the group set.
1275 groupmember(gid_t gid, struct ucred *cred)
1281 if (cred->cr_groups[0] == gid)
1285 * If gid was not our primary group, perform a binary search
1286 * of the supplemental groups. This is possible because we
1287 * sort the groups in crsetgroups().
1290 h = cred->cr_ngroups;
1292 m = l + ((h - l) / 2);
1293 if (cred->cr_groups[m] < gid)
1298 if ((l < cred->cr_ngroups) && (cred->cr_groups[l] == gid))
1305 * Test the active securelevel against a given level. securelevel_gt()
1306 * implements (securelevel > level). securelevel_ge() implements
1307 * (securelevel >= level). Note that the logic is inverted -- these
1308 * functions return EPERM on "success" and 0 on "failure".
1310 * Due to care taken when setting the securelevel, we know that no jail will
1311 * be less secure that its parent (or the physical system), so it is sufficient
1312 * to test the current jail only.
1314 * XXXRW: Possibly since this has to do with privilege, it should move to
1318 securelevel_gt(struct ucred *cr, int level)
1321 return (cr->cr_prison->pr_securelevel > level ? EPERM : 0);
1325 securelevel_ge(struct ucred *cr, int level)
1328 return (cr->cr_prison->pr_securelevel >= level ? EPERM : 0);
1332 * 'see_other_uids' determines whether or not visibility of processes
1333 * and sockets with credentials holding different real uids is possible
1334 * using a variety of system MIBs.
1335 * XXX: data declarations should be together near the beginning of the file.
1337 static int see_other_uids = 1;
1338 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_uids, CTLFLAG_RW,
1340 "Unprivileged processes may see subjects/objects with different real uid");
1343 * Determine if u1 "can see" the subject specified by u2, according to the
1344 * 'see_other_uids' policy.
1345 * Returns: 0 for permitted, ESRCH otherwise
1347 * References: *u1 and *u2 must not change during the call
1348 * u1 may equal u2, in which case only one reference is required
1351 cr_canseeotheruids(struct ucred *u1, struct ucred *u2)
1354 if (!see_other_uids && u1->cr_ruid != u2->cr_ruid) {
1355 if (priv_check_cred(u1, PRIV_SEEOTHERUIDS) != 0)
1362 * 'see_other_gids' determines whether or not visibility of processes
1363 * and sockets with credentials holding different real gids is possible
1364 * using a variety of system MIBs.
1365 * XXX: data declarations should be together near the beginning of the file.
1367 static int see_other_gids = 1;
1368 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_gids, CTLFLAG_RW,
1370 "Unprivileged processes may see subjects/objects with different real gid");
1373 * Determine if u1 can "see" the subject specified by u2, according to the
1374 * 'see_other_gids' policy.
1375 * Returns: 0 for permitted, ESRCH otherwise
1377 * References: *u1 and *u2 must not change during the call
1378 * u1 may equal u2, in which case only one reference is required
1381 cr_canseeothergids(struct ucred *u1, struct ucred *u2)
1385 if (!see_other_gids) {
1387 for (i = 0; i < u1->cr_ngroups; i++) {
1388 if (groupmember(u1->cr_groups[i], u2))
1394 if (priv_check_cred(u1, PRIV_SEEOTHERGIDS) != 0)
1402 * 'see_jail_proc' determines whether or not visibility of processes and
1403 * sockets with credentials holding different jail ids is possible using a
1404 * variety of system MIBs.
1406 * XXX: data declarations should be together near the beginning of the file.
1409 static int see_jail_proc = 1;
1410 SYSCTL_INT(_security_bsd, OID_AUTO, see_jail_proc, CTLFLAG_RW,
1412 "Unprivileged processes may see subjects/objects with different jail ids");
1415 * Determine if u1 "can see" the subject specified by u2, according to the
1416 * 'see_jail_proc' policy.
1417 * Returns: 0 for permitted, ESRCH otherwise
1419 * References: *u1 and *u2 must not change during the call
1420 * u1 may equal u2, in which case only one reference is required
1423 cr_canseejailproc(struct ucred *u1, struct ucred *u2)
1425 if (u1->cr_uid == 0)
1427 return (!see_jail_proc && u1->cr_prison != u2->cr_prison ? ESRCH : 0);
1431 * Determine if u1 "can see" the subject specified by u2.
1432 * Returns: 0 for permitted, an errno value otherwise
1434 * References: *u1 and *u2 must not change during the call
1435 * u1 may equal u2, in which case only one reference is required
1438 cr_cansee(struct ucred *u1, struct ucred *u2)
1442 if ((error = prison_check(u1, u2)))
1445 if ((error = mac_cred_check_visible(u1, u2)))
1448 if ((error = cr_canseeotheruids(u1, u2)))
1450 if ((error = cr_canseeothergids(u1, u2)))
1452 if ((error = cr_canseejailproc(u1, u2)))
1458 * Determine if td "can see" the subject specified by p.
1459 * Returns: 0 for permitted, an errno value otherwise
1460 * Locks: Sufficient locks to protect p->p_ucred must be held. td really
1461 * should be curthread.
1462 * References: td and p must be valid for the lifetime of the call
1465 p_cansee(struct thread *td, struct proc *p)
1468 /* Wrap cr_cansee() for all functionality. */
1469 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1470 PROC_LOCK_ASSERT(p, MA_OWNED);
1471 return (cr_cansee(td->td_ucred, p->p_ucred));
1475 * 'conservative_signals' prevents the delivery of a broad class of
1476 * signals by unprivileged processes to processes that have changed their
1477 * credentials since the last invocation of execve(). This can prevent
1478 * the leakage of cached information or retained privileges as a result
1479 * of a common class of signal-related vulnerabilities. However, this
1480 * may interfere with some applications that expect to be able to
1481 * deliver these signals to peer processes after having given up
1484 static int conservative_signals = 1;
1485 SYSCTL_INT(_security_bsd, OID_AUTO, conservative_signals, CTLFLAG_RW,
1486 &conservative_signals, 0, "Unprivileged processes prevented from "
1487 "sending certain signals to processes whose credentials have changed");
1489 * Determine whether cred may deliver the specified signal to proc.
1490 * Returns: 0 for permitted, an errno value otherwise.
1491 * Locks: A lock must be held for proc.
1492 * References: cred and proc must be valid for the lifetime of the call.
1495 cr_cansignal(struct ucred *cred, struct proc *proc, int signum)
1499 PROC_LOCK_ASSERT(proc, MA_OWNED);
1501 * Jail semantics limit the scope of signalling to proc in the
1502 * same jail as cred, if cred is in jail.
1504 error = prison_check(cred, proc->p_ucred);
1508 if ((error = mac_proc_check_signal(cred, proc, signum)))
1511 if ((error = cr_canseeotheruids(cred, proc->p_ucred)))
1513 if ((error = cr_canseeothergids(cred, proc->p_ucred)))
1517 * UNIX signal semantics depend on the status of the P_SUGID
1518 * bit on the target process. If the bit is set, then additional
1519 * restrictions are placed on the set of available signals.
1521 if (conservative_signals && (proc->p_flag & P_SUGID)) {
1536 * Generally, permit job and terminal control
1541 /* Not permitted without privilege. */
1542 error = priv_check_cred(cred, PRIV_SIGNAL_SUGID);
1549 * Generally, the target credential's ruid or svuid must match the
1550 * subject credential's ruid or euid.
1552 if (cred->cr_ruid != proc->p_ucred->cr_ruid &&
1553 cred->cr_ruid != proc->p_ucred->cr_svuid &&
1554 cred->cr_uid != proc->p_ucred->cr_ruid &&
1555 cred->cr_uid != proc->p_ucred->cr_svuid) {
1556 error = priv_check_cred(cred, PRIV_SIGNAL_DIFFCRED);
1565 * Determine whether td may deliver the specified signal to p.
1566 * Returns: 0 for permitted, an errno value otherwise
1567 * Locks: Sufficient locks to protect various components of td and p
1568 * must be held. td must be curthread, and a lock must be
1570 * References: td and p must be valid for the lifetime of the call
1573 p_cansignal(struct thread *td, struct proc *p, int signum)
1576 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1577 PROC_LOCK_ASSERT(p, MA_OWNED);
1578 if (td->td_proc == p)
1582 * UNIX signalling semantics require that processes in the same
1583 * session always be able to deliver SIGCONT to one another,
1584 * overriding the remaining protections.
1586 /* XXX: This will require an additional lock of some sort. */
1587 if (signum == SIGCONT && td->td_proc->p_session == p->p_session)
1590 * Some compat layers use SIGTHR and higher signals for
1591 * communication between different kernel threads of the same
1592 * process, so that they expect that it's always possible to
1593 * deliver them, even for suid applications where cr_cansignal() can
1594 * deny such ability for security consideration. It should be
1595 * pretty safe to do since the only way to create two processes
1596 * with the same p_leader is via rfork(2).
1598 if (td->td_proc->p_leader != NULL && signum >= SIGTHR &&
1599 signum < SIGTHR + 4 && td->td_proc->p_leader == p->p_leader)
1602 return (cr_cansignal(td->td_ucred, p, signum));
1606 * Determine whether td may reschedule p.
1607 * Returns: 0 for permitted, an errno value otherwise
1608 * Locks: Sufficient locks to protect various components of td and p
1609 * must be held. td must be curthread, and a lock must
1611 * References: td and p must be valid for the lifetime of the call
1614 p_cansched(struct thread *td, struct proc *p)
1618 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1619 PROC_LOCK_ASSERT(p, MA_OWNED);
1620 if (td->td_proc == p)
1622 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1625 if ((error = mac_proc_check_sched(td->td_ucred, p)))
1628 if ((error = cr_canseeotheruids(td->td_ucred, p->p_ucred)))
1630 if ((error = cr_canseeothergids(td->td_ucred, p->p_ucred)))
1632 if (td->td_ucred->cr_ruid != p->p_ucred->cr_ruid &&
1633 td->td_ucred->cr_uid != p->p_ucred->cr_ruid) {
1634 error = priv_check(td, PRIV_SCHED_DIFFCRED);
1642 * Handle getting or setting the prison's unprivileged_proc_debug
1646 sysctl_unprivileged_proc_debug(SYSCTL_HANDLER_ARGS)
1651 val = prison_allow(req->td->td_ucred, PR_ALLOW_UNPRIV_DEBUG) != 0;
1652 error = sysctl_handle_int(oidp, &val, 0, req);
1653 if (error != 0 || req->newptr == NULL)
1655 pr = req->td->td_ucred->cr_prison;
1656 mtx_lock(&pr->pr_mtx);
1659 pr->pr_allow &= ~(PR_ALLOW_UNPRIV_DEBUG);
1662 pr->pr_allow |= PR_ALLOW_UNPRIV_DEBUG;
1667 mtx_unlock(&pr->pr_mtx);
1673 * The 'unprivileged_proc_debug' flag may be used to disable a variety of
1674 * unprivileged inter-process debugging services, including some procfs
1675 * functionality, ptrace(), and ktrace(). In the past, inter-process
1676 * debugging has been involved in a variety of security problems, and sites
1677 * not requiring the service might choose to disable it when hardening
1680 SYSCTL_PROC(_security_bsd, OID_AUTO, unprivileged_proc_debug,
1681 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_SECURE |
1682 CTLFLAG_MPSAFE, 0, 0, sysctl_unprivileged_proc_debug, "I",
1683 "Unprivileged processes may use process debugging facilities");
1686 * Determine whether td may debug p.
1687 * Returns: 0 for permitted, an errno value otherwise
1688 * Locks: Sufficient locks to protect various components of td and p
1689 * must be held. td must be curthread, and a lock must
1691 * References: td and p must be valid for the lifetime of the call
1694 p_candebug(struct thread *td, struct proc *p)
1696 int credentialchanged, error, grpsubset, i, uidsubset;
1698 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1699 PROC_LOCK_ASSERT(p, MA_OWNED);
1700 if ((error = priv_check(td, PRIV_DEBUG_UNPRIV)))
1702 if (td->td_proc == p)
1704 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1707 if ((error = mac_proc_check_debug(td->td_ucred, p)))
1710 if ((error = cr_canseeotheruids(td->td_ucred, p->p_ucred)))
1712 if ((error = cr_canseeothergids(td->td_ucred, p->p_ucred)))
1716 * Is p's group set a subset of td's effective group set? This
1717 * includes p's egid, group access list, rgid, and svgid.
1720 for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
1721 if (!groupmember(p->p_ucred->cr_groups[i], td->td_ucred)) {
1726 grpsubset = grpsubset &&
1727 groupmember(p->p_ucred->cr_rgid, td->td_ucred) &&
1728 groupmember(p->p_ucred->cr_svgid, td->td_ucred);
1731 * Are the uids present in p's credential equal to td's
1732 * effective uid? This includes p's euid, svuid, and ruid.
1734 uidsubset = (td->td_ucred->cr_uid == p->p_ucred->cr_uid &&
1735 td->td_ucred->cr_uid == p->p_ucred->cr_svuid &&
1736 td->td_ucred->cr_uid == p->p_ucred->cr_ruid);
1739 * Has the credential of the process changed since the last exec()?
1741 credentialchanged = (p->p_flag & P_SUGID);
1744 * If p's gids aren't a subset, or the uids aren't a subset,
1745 * or the credential has changed, require appropriate privilege
1746 * for td to debug p.
1748 if (!grpsubset || !uidsubset) {
1749 error = priv_check(td, PRIV_DEBUG_DIFFCRED);
1754 if (credentialchanged) {
1755 error = priv_check(td, PRIV_DEBUG_SUGID);
1760 /* Can't trace init when securelevel > 0. */
1761 if (p == initproc) {
1762 error = securelevel_gt(td->td_ucred, 0);
1768 * Can't trace a process that's currently exec'ing.
1770 * XXX: Note, this is not a security policy decision, it's a
1771 * basic correctness/functionality decision. Therefore, this check
1772 * should be moved to the caller's of p_candebug().
1774 if ((p->p_flag & P_INEXEC) != 0)
1777 /* Denied explicitely */
1778 if ((p->p_flag2 & P2_NOTRACE) != 0) {
1779 error = priv_check(td, PRIV_DEBUG_DENIED);
1788 * Determine whether the subject represented by cred can "see" a socket.
1789 * Returns: 0 for permitted, ENOENT otherwise.
1792 cr_canseesocket(struct ucred *cred, struct socket *so)
1796 error = prison_check(cred, so->so_cred);
1800 error = mac_socket_check_visible(cred, so);
1804 if (cr_canseeotheruids(cred, so->so_cred))
1806 if (cr_canseeothergids(cred, so->so_cred))
1813 * Determine whether td can wait for the exit of p.
1814 * Returns: 0 for permitted, an errno value otherwise
1815 * Locks: Sufficient locks to protect various components of td and p
1816 * must be held. td must be curthread, and a lock must
1818 * References: td and p must be valid for the lifetime of the call
1822 p_canwait(struct thread *td, struct proc *p)
1826 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1827 PROC_LOCK_ASSERT(p, MA_OWNED);
1828 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1831 if ((error = mac_proc_check_wait(td->td_ucred, p)))
1835 /* XXXMAC: This could have odd effects on some shells. */
1836 if ((error = cr_canseeotheruids(td->td_ucred, p->p_ucred)))
1844 * Credential management.
1846 * struct ucred objects are rarely allocated but gain and lose references all
1847 * the time (e.g., on struct file alloc/dealloc) turning refcount updates into
1848 * a significant source of cache-line ping ponging. Common cases are worked
1849 * around by modifying thread-local counter instead if the cred to operate on
1850 * matches td_realucred.
1852 * The counter is split into 2 parts:
1853 * - cr_users -- total count of all struct proc and struct thread objects
1854 * which have given cred in p_ucred and td_ucred respectively
1855 * - cr_ref -- the actual ref count, only valid if cr_users == 0
1857 * If users == 0 then cr_ref behaves similarly to refcount(9), in particular if
1858 * the count reaches 0 the object is freeable.
1859 * If users > 0 and curthread->td_realucred == cred, then updates are performed
1860 * against td_ucredref.
1861 * In other cases updates are performed against cr_ref.
1863 * Changing td_realucred into something else decrements cr_users and transfers
1864 * accumulated updates.
1867 crcowget(struct ucred *cr)
1870 mtx_lock(&cr->cr_mtx);
1871 KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
1872 __func__, cr->cr_users, cr));
1875 mtx_unlock(&cr->cr_mtx);
1879 static struct ucred *
1880 crunuse(struct thread *td)
1882 struct ucred *cr, *crold;
1885 mtx_lock(&cr->cr_mtx);
1886 cr->cr_ref += td->td_ucredref;
1887 td->td_ucredref = 0;
1888 KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
1889 __func__, cr->cr_users, cr));
1891 if (cr->cr_users == 0) {
1892 KASSERT(cr->cr_ref > 0, ("%s: ref %d not > 0 on cred %p",
1893 __func__, cr->cr_ref, cr));
1899 mtx_unlock(&cr->cr_mtx);
1904 crcowfree(struct thread *td)
1918 struct ucred *crnew, *crold;
1922 PROC_LOCK_ASSERT(p, MA_OWNED);
1924 MPASS(td->td_realucred == td->td_ucred);
1925 if (td->td_realucred == p->p_ucred)
1928 crnew = crcowget(p->p_ucred);
1929 crold = crunuse(td);
1930 td->td_realucred = crnew;
1931 td->td_ucred = td->td_realucred;
1936 * Allocate a zeroed cred structure.
1943 cr = malloc(sizeof(*cr), M_CRED, M_WAITOK | M_ZERO);
1944 mtx_init(&cr->cr_mtx, "cred", NULL, MTX_DEF);
1947 audit_cred_init(cr);
1952 cr->cr_groups = cr->cr_smallgroups;
1954 sizeof(cr->cr_smallgroups) / sizeof(cr->cr_smallgroups[0]);
1959 * Claim another reference to a ucred structure.
1962 crhold(struct ucred *cr)
1967 if (__predict_true(td->td_realucred == cr)) {
1968 KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
1969 __func__, cr->cr_users, cr));
1973 mtx_lock(&cr->cr_mtx);
1975 mtx_unlock(&cr->cr_mtx);
1980 * Free a cred structure. Throws away space when ref count gets to 0.
1983 crfree(struct ucred *cr)
1988 if (td->td_realucred == cr) {
1989 KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
1990 __func__, cr->cr_users, cr));
1994 mtx_lock(&cr->cr_mtx);
1995 KASSERT(cr->cr_users >= 0, ("%s: users %d not >= 0 on cred %p",
1996 __func__, cr->cr_users, cr));
1998 if (cr->cr_users > 0) {
1999 mtx_unlock(&cr->cr_mtx);
2002 KASSERT(cr->cr_ref >= 0, ("%s: ref %d not >= 0 on cred %p",
2003 __func__, cr->cr_ref, cr));
2004 if (cr->cr_ref > 0) {
2005 mtx_unlock(&cr->cr_mtx);
2009 * Some callers of crget(), such as nfs_statfs(), allocate a temporary
2010 * credential, but don't allocate a uidinfo structure.
2012 if (cr->cr_uidinfo != NULL)
2013 uifree(cr->cr_uidinfo);
2014 if (cr->cr_ruidinfo != NULL)
2015 uifree(cr->cr_ruidinfo);
2016 if (cr->cr_prison != NULL)
2017 prison_free(cr->cr_prison);
2018 if (cr->cr_loginclass != NULL)
2019 loginclass_free(cr->cr_loginclass);
2021 audit_cred_destroy(cr);
2024 mac_cred_destroy(cr);
2026 mtx_destroy(&cr->cr_mtx);
2027 if (cr->cr_groups != cr->cr_smallgroups)
2028 free(cr->cr_groups, M_CRED);
2033 * Copy a ucred's contents from a template. Does not block.
2036 crcopy(struct ucred *dest, struct ucred *src)
2039 KASSERT(dest->cr_ref == 1, ("crcopy of shared ucred"));
2040 bcopy(&src->cr_startcopy, &dest->cr_startcopy,
2041 (unsigned)((caddr_t)&src->cr_endcopy -
2042 (caddr_t)&src->cr_startcopy));
2043 crsetgroups(dest, src->cr_ngroups, src->cr_groups);
2044 uihold(dest->cr_uidinfo);
2045 uihold(dest->cr_ruidinfo);
2046 prison_hold(dest->cr_prison);
2047 loginclass_hold(dest->cr_loginclass);
2049 audit_cred_copy(src, dest);
2052 mac_cred_copy(src, dest);
2057 * Dup cred struct to a new held one.
2060 crdup(struct ucred *cr)
2062 struct ucred *newcr;
2070 * Fill in a struct xucred based on a struct ucred.
2073 cru2x(struct ucred *cr, struct xucred *xcr)
2077 bzero(xcr, sizeof(*xcr));
2078 xcr->cr_version = XUCRED_VERSION;
2079 xcr->cr_uid = cr->cr_uid;
2081 ngroups = MIN(cr->cr_ngroups, XU_NGROUPS);
2082 xcr->cr_ngroups = ngroups;
2083 bcopy(cr->cr_groups, xcr->cr_groups,
2084 ngroups * sizeof(*cr->cr_groups));
2088 cru2xt(struct thread *td, struct xucred *xcr)
2091 cru2x(td->td_ucred, xcr);
2092 xcr->cr_pid = td->td_proc->p_pid;
2096 * Set initial process credentials.
2097 * Callers are responsible for providing the reference for provided credentials.
2100 proc_set_cred_init(struct proc *p, struct ucred *newcred)
2103 p->p_ucred = crcowget(newcred);
2107 * Change process credentials.
2108 * Callers are responsible for providing the reference for passed credentials
2109 * and for freeing old ones.
2111 * Process has to be locked except when it does not have credentials (as it
2112 * should not be visible just yet) or when newcred is NULL (as this can be
2113 * only used when the process is about to be freed, at which point it should
2114 * not be visible anymore).
2117 proc_set_cred(struct proc *p, struct ucred *newcred)
2123 PROC_LOCK_ASSERT(p, MA_OWNED);
2124 KASSERT(newcred->cr_users == 0, ("%s: users %d not 0 on cred %p",
2125 __func__, newcred->cr_users, newcred));
2126 mtx_lock(&cr->cr_mtx);
2127 KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
2128 __func__, cr->cr_users, cr));
2130 mtx_unlock(&cr->cr_mtx);
2131 p->p_ucred = newcred;
2132 newcred->cr_users = 1;
2137 proc_unset_cred(struct proc *p)
2141 MPASS(p->p_state == PRS_ZOMBIE || p->p_state == PRS_NEW);
2144 KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
2145 __func__, cr->cr_users, cr));
2146 mtx_lock(&cr->cr_mtx);
2148 if (cr->cr_users == 0)
2149 KASSERT(cr->cr_ref > 0, ("%s: ref %d not > 0 on cred %p",
2150 __func__, cr->cr_ref, cr));
2151 mtx_unlock(&cr->cr_mtx);
2156 crcopysafe(struct proc *p, struct ucred *cr)
2158 struct ucred *oldcred;
2161 PROC_LOCK_ASSERT(p, MA_OWNED);
2163 oldcred = p->p_ucred;
2164 while (cr->cr_agroups < oldcred->cr_agroups) {
2165 groups = oldcred->cr_agroups;
2167 crextend(cr, groups);
2169 oldcred = p->p_ucred;
2171 crcopy(cr, oldcred);
2177 * Extend the passed in credential to hold n items.
2180 crextend(struct ucred *cr, int n)
2185 if (n <= cr->cr_agroups)
2189 * We extend by 2 each time since we're using a power of two
2190 * allocator until we need enough groups to fill a page.
2191 * Once we're allocating multiple pages, only allocate as many
2192 * as we actually need. The case of processes needing a
2193 * non-power of two number of pages seems more likely than
2194 * a real world process that adds thousands of groups one at a
2197 if ( n < PAGE_SIZE / sizeof(gid_t) ) {
2198 if (cr->cr_agroups == 0)
2199 cnt = MAX(1, MINALLOCSIZE / sizeof(gid_t));
2201 cnt = cr->cr_agroups * 2;
2206 cnt = roundup2(n, PAGE_SIZE / sizeof(gid_t));
2208 /* Free the old array. */
2209 if (cr->cr_groups != cr->cr_smallgroups)
2210 free(cr->cr_groups, M_CRED);
2212 cr->cr_groups = malloc(cnt * sizeof(gid_t), M_CRED, M_WAITOK | M_ZERO);
2213 cr->cr_agroups = cnt;
2217 * Copy groups in to a credential, preserving any necessary invariants.
2218 * Currently this includes the sorting of all supplemental gids.
2219 * crextend() must have been called before hand to ensure sufficient
2220 * space is available.
2223 crsetgroups_locked(struct ucred *cr, int ngrp, gid_t *groups)
2229 KASSERT(cr->cr_agroups >= ngrp, ("cr_ngroups is too small"));
2231 bcopy(groups, cr->cr_groups, ngrp * sizeof(gid_t));
2232 cr->cr_ngroups = ngrp;
2235 * Sort all groups except cr_groups[0] to allow groupmember to
2236 * perform a binary search.
2238 * XXX: If large numbers of groups become common this should
2239 * be replaced with shell sort like linux uses or possibly
2242 for (i = 2; i < ngrp; i++) {
2243 g = cr->cr_groups[i];
2244 for (j = i-1; j >= 1 && g < cr->cr_groups[j]; j--)
2245 cr->cr_groups[j + 1] = cr->cr_groups[j];
2246 cr->cr_groups[j + 1] = g;
2251 * Copy groups in to a credential after expanding it if required.
2252 * Truncate the list to (ngroups_max + 1) if it is too large.
2255 crsetgroups(struct ucred *cr, int ngrp, gid_t *groups)
2258 if (ngrp > ngroups_max + 1)
2259 ngrp = ngroups_max + 1;
2262 crsetgroups_locked(cr, ngrp, groups);
2266 * Get login name, if available.
2268 #ifndef _SYS_SYSPROTO_H_
2269 struct getlogin_args {
2276 sys_getlogin(struct thread *td, struct getlogin_args *uap)
2278 char login[MAXLOGNAME];
2279 struct proc *p = td->td_proc;
2282 if (uap->namelen > MAXLOGNAME)
2283 uap->namelen = MAXLOGNAME;
2285 SESS_LOCK(p->p_session);
2286 len = strlcpy(login, p->p_session->s_login, uap->namelen) + 1;
2287 SESS_UNLOCK(p->p_session);
2289 if (len > uap->namelen)
2291 return (copyout(login, uap->namebuf, len));
2297 #ifndef _SYS_SYSPROTO_H_
2298 struct setlogin_args {
2304 sys_setlogin(struct thread *td, struct setlogin_args *uap)
2306 struct proc *p = td->td_proc;
2308 char logintmp[MAXLOGNAME];
2310 CTASSERT(sizeof(p->p_session->s_login) >= sizeof(logintmp));
2312 error = priv_check(td, PRIV_PROC_SETLOGIN);
2315 error = copyinstr(uap->namebuf, logintmp, sizeof(logintmp), NULL);
2317 if (error == ENAMETOOLONG)
2321 AUDIT_ARG_LOGIN(logintmp);
2323 SESS_LOCK(p->p_session);
2324 strcpy(p->p_session->s_login, logintmp);
2325 SESS_UNLOCK(p->p_session);
2331 setsugid(struct proc *p)
2334 PROC_LOCK_ASSERT(p, MA_OWNED);
2335 p->p_flag |= P_SUGID;
2339 * Change a process's effective uid.
2340 * Side effects: newcred->cr_uid and newcred->cr_uidinfo will be modified.
2341 * References: newcred must be an exclusive credential reference for the
2342 * duration of the call.
2345 change_euid(struct ucred *newcred, struct uidinfo *euip)
2348 newcred->cr_uid = euip->ui_uid;
2350 uifree(newcred->cr_uidinfo);
2351 newcred->cr_uidinfo = euip;
2355 * Change a process's effective gid.
2356 * Side effects: newcred->cr_gid will be modified.
2357 * References: newcred must be an exclusive credential reference for the
2358 * duration of the call.
2361 change_egid(struct ucred *newcred, gid_t egid)
2364 newcred->cr_groups[0] = egid;
2368 * Change a process's real uid.
2369 * Side effects: newcred->cr_ruid will be updated, newcred->cr_ruidinfo
2370 * will be updated, and the old and new cr_ruidinfo proc
2371 * counts will be updated.
2372 * References: newcred must be an exclusive credential reference for the
2373 * duration of the call.
2376 change_ruid(struct ucred *newcred, struct uidinfo *ruip)
2379 (void)chgproccnt(newcred->cr_ruidinfo, -1, 0);
2380 newcred->cr_ruid = ruip->ui_uid;
2382 uifree(newcred->cr_ruidinfo);
2383 newcred->cr_ruidinfo = ruip;
2384 (void)chgproccnt(newcred->cr_ruidinfo, 1, 0);
2388 * Change a process's real gid.
2389 * Side effects: newcred->cr_rgid will be updated.
2390 * References: newcred must be an exclusive credential reference for the
2391 * duration of the call.
2394 change_rgid(struct ucred *newcred, gid_t rgid)
2397 newcred->cr_rgid = rgid;
2401 * Change a process's saved uid.
2402 * Side effects: newcred->cr_svuid will be updated.
2403 * References: newcred must be an exclusive credential reference for the
2404 * duration of the call.
2407 change_svuid(struct ucred *newcred, uid_t svuid)
2410 newcred->cr_svuid = svuid;
2414 * Change a process's saved gid.
2415 * Side effects: newcred->cr_svgid will be updated.
2416 * References: newcred must be an exclusive credential reference for the
2417 * duration of the call.
2420 change_svgid(struct ucred *newcred, gid_t svgid)
2423 newcred->cr_svgid = svgid;