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/pioctl.h>
69 #include <sys/racct.h>
71 #include <sys/resourcevar.h>
72 #include <sys/socket.h>
73 #include <sys/socketvar.h>
74 #include <sys/syscallsubr.h>
75 #include <sys/sysctl.h>
79 "Kernel support for interfaces necessary for regression testing (SECURITY RISK!)");
82 #include <security/audit/audit.h>
83 #include <security/mac/mac_framework.h>
85 static MALLOC_DEFINE(M_CRED, "cred", "credentials");
87 SYSCTL_NODE(_security, OID_AUTO, bsd, CTLFLAG_RW, 0, "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)
203 error = p_cansee(td, p);
209 td->td_retval[0] = p->p_session->s_sid;
214 #ifndef _SYS_SYSPROTO_H_
221 sys_getuid(struct thread *td, struct getuid_args *uap)
224 td->td_retval[0] = td->td_ucred->cr_ruid;
225 #if defined(COMPAT_43)
226 td->td_retval[1] = td->td_ucred->cr_uid;
231 #ifndef _SYS_SYSPROTO_H_
232 struct geteuid_args {
238 sys_geteuid(struct thread *td, struct geteuid_args *uap)
241 td->td_retval[0] = td->td_ucred->cr_uid;
245 #ifndef _SYS_SYSPROTO_H_
252 sys_getgid(struct thread *td, struct getgid_args *uap)
255 td->td_retval[0] = td->td_ucred->cr_rgid;
256 #if defined(COMPAT_43)
257 td->td_retval[1] = td->td_ucred->cr_groups[0];
263 * Get effective group ID. The "egid" is groups[0], and could be obtained
264 * via getgroups. This syscall exists because it is somewhat painful to do
265 * correctly in a library function.
267 #ifndef _SYS_SYSPROTO_H_
268 struct getegid_args {
274 sys_getegid(struct thread *td, struct getegid_args *uap)
277 td->td_retval[0] = td->td_ucred->cr_groups[0];
281 #ifndef _SYS_SYSPROTO_H_
282 struct getgroups_args {
288 sys_getgroups(struct thread *td, struct getgroups_args *uap)
295 ngrp = cred->cr_ngroups;
297 if (uap->gidsetsize == 0) {
301 if (uap->gidsetsize < ngrp)
304 error = copyout(cred->cr_groups, uap->gidset, ngrp * sizeof(gid_t));
306 td->td_retval[0] = ngrp;
310 #ifndef _SYS_SYSPROTO_H_
317 sys_setsid(struct thread *td, struct setsid_args *uap)
321 struct proc *p = td->td_proc;
322 struct pgrp *newpgrp;
323 struct session *newsess;
328 newpgrp = uma_zalloc(pgrp_zone, M_WAITOK);
329 newsess = malloc(sizeof(struct session), M_SESSION, M_WAITOK | M_ZERO);
331 sx_xlock(&proctree_lock);
333 if (p->p_pgid == p->p_pid || (pgrp = pgfind(p->p_pid)) != NULL) {
338 (void)enterpgrp(p, p->p_pid, newpgrp, newsess);
339 td->td_retval[0] = p->p_pid;
344 sx_xunlock(&proctree_lock);
346 uma_zfree(pgrp_zone, newpgrp);
347 free(newsess, M_SESSION);
353 * set process group (setpgid/old setpgrp)
355 * caller does setpgid(targpid, targpgid)
357 * pid must be caller or child of caller (ESRCH)
359 * pid must be in same session (EPERM)
360 * pid can't have done an exec (EACCES)
362 * there must exist some pid in same session having pgid (EPERM)
363 * pid must not be session leader (EPERM)
365 #ifndef _SYS_SYSPROTO_H_
366 struct setpgid_args {
367 int pid; /* target process id */
368 int pgid; /* target pgrp id */
373 sys_setpgid(struct thread *td, struct setpgid_args *uap)
375 struct proc *curp = td->td_proc;
376 struct proc *targp; /* target process */
377 struct pgrp *pgrp; /* target pgrp */
379 struct pgrp *newpgrp;
386 newpgrp = uma_zalloc(pgrp_zone, M_WAITOK);
388 sx_xlock(&proctree_lock);
389 if (uap->pid != 0 && uap->pid != curp->p_pid) {
390 if ((targp = pfind(uap->pid)) == NULL) {
394 if (!inferior(targp)) {
399 if ((error = p_cansee(td, targp))) {
403 if (targp->p_pgrp == NULL ||
404 targp->p_session != curp->p_session) {
409 if (targp->p_flag & P_EXEC) {
417 if (SESS_LEADER(targp)) {
422 uap->pgid = targp->p_pid;
423 if ((pgrp = pgfind(uap->pgid)) == NULL) {
424 if (uap->pgid == targp->p_pid) {
425 error = enterpgrp(targp, uap->pgid, newpgrp,
432 if (pgrp == targp->p_pgrp) {
436 if (pgrp->pg_id != targp->p_pid &&
437 pgrp->pg_session != curp->p_session) {
443 error = enterthispgrp(targp, pgrp);
446 sx_xunlock(&proctree_lock);
447 KASSERT((error == 0) || (newpgrp != NULL),
448 ("setpgid failed and newpgrp is NULL"));
449 uma_zfree(pgrp_zone, newpgrp);
454 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
455 * compatible. It says that setting the uid/gid to euid/egid is a special
456 * case of "appropriate privilege". Once the rules are expanded out, this
457 * basically means that setuid(nnn) sets all three id's, in all permitted
458 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
459 * does not set the saved id - this is dangerous for traditional BSD
460 * programs. For this reason, we *really* do not want to set
461 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
463 #define POSIX_APPENDIX_B_4_2_2
465 #ifndef _SYS_SYSPROTO_H_
472 sys_setuid(struct thread *td, struct setuid_args *uap)
474 struct proc *p = td->td_proc;
475 struct ucred *newcred, *oldcred;
486 * Copy credentials so other references do not see our changes.
488 oldcred = crcopysafe(p, newcred);
491 error = mac_cred_check_setuid(oldcred, uid);
497 * See if we have "permission" by POSIX 1003.1 rules.
499 * Note that setuid(geteuid()) is a special case of
500 * "appropriate privileges" in appendix B.4.2.2. We need
501 * to use this clause to be compatible with traditional BSD
502 * semantics. Basically, it means that "setuid(xx)" sets all
503 * three id's (assuming you have privs).
505 * Notes on the logic. We do things in three steps.
506 * 1: We determine if the euid is going to change, and do EPERM
507 * right away. We unconditionally change the euid later if this
508 * test is satisfied, simplifying that part of the logic.
509 * 2: We determine if the real and/or saved uids are going to
510 * change. Determined by compile options.
511 * 3: Change euid last. (after tests in #2 for "appropriate privs")
513 if (uid != oldcred->cr_ruid && /* allow setuid(getuid()) */
514 #ifdef _POSIX_SAVED_IDS
515 uid != oldcred->cr_svuid && /* allow setuid(saved gid) */
517 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
518 uid != oldcred->cr_uid && /* allow setuid(geteuid()) */
520 (error = priv_check_cred(oldcred, PRIV_CRED_SETUID, 0)) != 0)
523 #ifdef _POSIX_SAVED_IDS
525 * Do we have "appropriate privileges" (are we root or uid == euid)
526 * If so, we are changing the real uid and/or saved uid.
529 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
530 uid == oldcred->cr_uid ||
532 /* We are using privs. */
533 priv_check_cred(oldcred, PRIV_CRED_SETUID, 0) == 0)
537 * Set the real uid and transfer proc count to new user.
539 if (uid != oldcred->cr_ruid) {
540 change_ruid(newcred, uip);
546 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
547 * the security of seteuid() depends on it. B.4.2.2 says it
548 * is important that we should do this.
550 if (uid != oldcred->cr_svuid) {
551 change_svuid(newcred, uid);
557 * In all permitted cases, we are changing the euid.
559 if (uid != oldcred->cr_uid) {
560 change_euid(newcred, uip);
563 proc_set_cred(p, newcred);
565 racct_proc_ucred_changed(p, oldcred, newcred);
570 rctl_proc_ucred_changed(p, newcred);
584 #ifndef _SYS_SYSPROTO_H_
585 struct seteuid_args {
591 sys_seteuid(struct thread *td, struct seteuid_args *uap)
593 struct proc *p = td->td_proc;
594 struct ucred *newcred, *oldcred;
596 struct uidinfo *euip;
600 AUDIT_ARG_EUID(euid);
605 * Copy credentials so other references do not see our changes.
607 oldcred = crcopysafe(p, newcred);
610 error = mac_cred_check_seteuid(oldcred, euid);
615 if (euid != oldcred->cr_ruid && /* allow seteuid(getuid()) */
616 euid != oldcred->cr_svuid && /* allow seteuid(saved uid) */
617 (error = priv_check_cred(oldcred, PRIV_CRED_SETEUID, 0)) != 0)
621 * Everything's okay, do it.
623 if (oldcred->cr_uid != euid) {
624 change_euid(newcred, euip);
627 proc_set_cred(p, newcred);
640 #ifndef _SYS_SYSPROTO_H_
647 sys_setgid(struct thread *td, struct setgid_args *uap)
649 struct proc *p = td->td_proc;
650 struct ucred *newcred, *oldcred;
658 oldcred = crcopysafe(p, newcred);
661 error = mac_cred_check_setgid(oldcred, gid);
667 * See if we have "permission" by POSIX 1003.1 rules.
669 * Note that setgid(getegid()) is a special case of
670 * "appropriate privileges" in appendix B.4.2.2. We need
671 * to use this clause to be compatible with traditional BSD
672 * semantics. Basically, it means that "setgid(xx)" sets all
673 * three id's (assuming you have privs).
675 * For notes on the logic here, see setuid() above.
677 if (gid != oldcred->cr_rgid && /* allow setgid(getgid()) */
678 #ifdef _POSIX_SAVED_IDS
679 gid != oldcred->cr_svgid && /* allow setgid(saved gid) */
681 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
682 gid != oldcred->cr_groups[0] && /* allow setgid(getegid()) */
684 (error = priv_check_cred(oldcred, PRIV_CRED_SETGID, 0)) != 0)
687 #ifdef _POSIX_SAVED_IDS
689 * Do we have "appropriate privileges" (are we root or gid == egid)
690 * If so, we are changing the real uid and saved gid.
693 #ifdef POSIX_APPENDIX_B_4_2_2 /* use the clause from B.4.2.2 */
694 gid == oldcred->cr_groups[0] ||
696 /* We are using privs. */
697 priv_check_cred(oldcred, PRIV_CRED_SETGID, 0) == 0)
703 if (oldcred->cr_rgid != gid) {
704 change_rgid(newcred, gid);
710 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
711 * the security of setegid() depends on it. B.4.2.2 says it
712 * is important that we should do this.
714 if (oldcred->cr_svgid != gid) {
715 change_svgid(newcred, gid);
720 * In all cases permitted cases, we are changing the egid.
721 * Copy credentials so other references do not see our changes.
723 if (oldcred->cr_groups[0] != gid) {
724 change_egid(newcred, gid);
727 proc_set_cred(p, newcred);
738 #ifndef _SYS_SYSPROTO_H_
739 struct setegid_args {
745 sys_setegid(struct thread *td, struct setegid_args *uap)
747 struct proc *p = td->td_proc;
748 struct ucred *newcred, *oldcred;
753 AUDIT_ARG_EGID(egid);
756 oldcred = crcopysafe(p, newcred);
759 error = mac_cred_check_setegid(oldcred, egid);
764 if (egid != oldcred->cr_rgid && /* allow setegid(getgid()) */
765 egid != oldcred->cr_svgid && /* allow setegid(saved gid) */
766 (error = priv_check_cred(oldcred, PRIV_CRED_SETEGID, 0)) != 0)
769 if (oldcred->cr_groups[0] != egid) {
770 change_egid(newcred, egid);
773 proc_set_cred(p, newcred);
784 #ifndef _SYS_SYSPROTO_H_
785 struct setgroups_args {
792 sys_setgroups(struct thread *td, struct setgroups_args *uap)
794 gid_t smallgroups[XU_NGROUPS];
799 gidsetsize = uap->gidsetsize;
800 if (gidsetsize > ngroups_max + 1)
803 if (gidsetsize > XU_NGROUPS)
804 groups = malloc(gidsetsize * sizeof(gid_t), M_TEMP, M_WAITOK);
806 groups = smallgroups;
808 error = copyin(uap->gidset, groups, gidsetsize * sizeof(gid_t));
810 error = kern_setgroups(td, gidsetsize, groups);
812 if (gidsetsize > XU_NGROUPS)
813 free(groups, M_TEMP);
818 kern_setgroups(struct thread *td, u_int ngrp, gid_t *groups)
820 struct proc *p = td->td_proc;
821 struct ucred *newcred, *oldcred;
824 MPASS(ngrp <= ngroups_max + 1);
825 AUDIT_ARG_GROUPSET(groups, ngrp);
827 crextend(newcred, ngrp);
829 oldcred = crcopysafe(p, newcred);
832 error = mac_cred_check_setgroups(oldcred, ngrp, groups);
837 error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0);
843 * setgroups(0, NULL) is a legitimate way of clearing the
844 * groups vector on non-BSD systems (which generally do not
845 * have the egid in the groups[0]). We risk security holes
846 * when running non-BSD software if we do not do the same.
848 newcred->cr_ngroups = 1;
850 crsetgroups_locked(newcred, ngrp, groups);
853 proc_set_cred(p, newcred);
864 #ifndef _SYS_SYSPROTO_H_
865 struct setreuid_args {
872 sys_setreuid(struct thread *td, struct setreuid_args *uap)
874 struct proc *p = td->td_proc;
875 struct ucred *newcred, *oldcred;
877 struct uidinfo *euip, *ruip;
882 AUDIT_ARG_EUID(euid);
883 AUDIT_ARG_RUID(ruid);
888 oldcred = crcopysafe(p, newcred);
891 error = mac_cred_check_setreuid(oldcred, ruid, euid);
896 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
897 ruid != oldcred->cr_svuid) ||
898 (euid != (uid_t)-1 && euid != oldcred->cr_uid &&
899 euid != oldcred->cr_ruid && euid != oldcred->cr_svuid)) &&
900 (error = priv_check_cred(oldcred, PRIV_CRED_SETREUID, 0)) != 0)
903 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
904 change_euid(newcred, euip);
907 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
908 change_ruid(newcred, ruip);
911 if ((ruid != (uid_t)-1 || newcred->cr_uid != newcred->cr_ruid) &&
912 newcred->cr_svuid != newcred->cr_uid) {
913 change_svuid(newcred, newcred->cr_uid);
916 proc_set_cred(p, newcred);
918 racct_proc_ucred_changed(p, oldcred, newcred);
923 rctl_proc_ucred_changed(p, newcred);
939 #ifndef _SYS_SYSPROTO_H_
940 struct setregid_args {
947 sys_setregid(struct thread *td, struct setregid_args *uap)
949 struct proc *p = td->td_proc;
950 struct ucred *newcred, *oldcred;
956 AUDIT_ARG_EGID(egid);
957 AUDIT_ARG_RGID(rgid);
960 oldcred = crcopysafe(p, newcred);
963 error = mac_cred_check_setregid(oldcred, rgid, egid);
968 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
969 rgid != oldcred->cr_svgid) ||
970 (egid != (gid_t)-1 && egid != oldcred->cr_groups[0] &&
971 egid != oldcred->cr_rgid && egid != oldcred->cr_svgid)) &&
972 (error = priv_check_cred(oldcred, PRIV_CRED_SETREGID, 0)) != 0)
975 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
976 change_egid(newcred, egid);
979 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
980 change_rgid(newcred, rgid);
983 if ((rgid != (gid_t)-1 || newcred->cr_groups[0] != newcred->cr_rgid) &&
984 newcred->cr_svgid != newcred->cr_groups[0]) {
985 change_svgid(newcred, newcred->cr_groups[0]);
988 proc_set_cred(p, newcred);
1000 * setresuid(ruid, euid, suid) is like setreuid except control over the saved
1003 #ifndef _SYS_SYSPROTO_H_
1004 struct setresuid_args {
1012 sys_setresuid(struct thread *td, struct setresuid_args *uap)
1014 struct proc *p = td->td_proc;
1015 struct ucred *newcred, *oldcred;
1016 uid_t euid, ruid, suid;
1017 struct uidinfo *euip, *ruip;
1023 AUDIT_ARG_EUID(euid);
1024 AUDIT_ARG_RUID(ruid);
1025 AUDIT_ARG_SUID(suid);
1027 euip = uifind(euid);
1028 ruip = uifind(ruid);
1030 oldcred = crcopysafe(p, newcred);
1033 error = mac_cred_check_setresuid(oldcred, ruid, euid, suid);
1038 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
1039 ruid != oldcred->cr_svuid &&
1040 ruid != oldcred->cr_uid) ||
1041 (euid != (uid_t)-1 && euid != oldcred->cr_ruid &&
1042 euid != oldcred->cr_svuid &&
1043 euid != oldcred->cr_uid) ||
1044 (suid != (uid_t)-1 && suid != oldcred->cr_ruid &&
1045 suid != oldcred->cr_svuid &&
1046 suid != oldcred->cr_uid)) &&
1047 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESUID, 0)) != 0)
1050 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
1051 change_euid(newcred, euip);
1054 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
1055 change_ruid(newcred, ruip);
1058 if (suid != (uid_t)-1 && oldcred->cr_svuid != suid) {
1059 change_svuid(newcred, suid);
1062 proc_set_cred(p, newcred);
1064 racct_proc_ucred_changed(p, oldcred, newcred);
1069 rctl_proc_ucred_changed(p, newcred);
1087 * setresgid(rgid, egid, sgid) is like setregid except control over the saved
1090 #ifndef _SYS_SYSPROTO_H_
1091 struct setresgid_args {
1099 sys_setresgid(struct thread *td, struct setresgid_args *uap)
1101 struct proc *p = td->td_proc;
1102 struct ucred *newcred, *oldcred;
1103 gid_t egid, rgid, sgid;
1109 AUDIT_ARG_EGID(egid);
1110 AUDIT_ARG_RGID(rgid);
1111 AUDIT_ARG_SGID(sgid);
1114 oldcred = crcopysafe(p, newcred);
1117 error = mac_cred_check_setresgid(oldcred, rgid, egid, sgid);
1122 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
1123 rgid != oldcred->cr_svgid &&
1124 rgid != oldcred->cr_groups[0]) ||
1125 (egid != (gid_t)-1 && egid != oldcred->cr_rgid &&
1126 egid != oldcred->cr_svgid &&
1127 egid != oldcred->cr_groups[0]) ||
1128 (sgid != (gid_t)-1 && sgid != oldcred->cr_rgid &&
1129 sgid != oldcred->cr_svgid &&
1130 sgid != oldcred->cr_groups[0])) &&
1131 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESGID, 0)) != 0)
1134 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
1135 change_egid(newcred, egid);
1138 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
1139 change_rgid(newcred, rgid);
1142 if (sgid != (gid_t)-1 && oldcred->cr_svgid != sgid) {
1143 change_svgid(newcred, sgid);
1146 proc_set_cred(p, newcred);
1157 #ifndef _SYS_SYSPROTO_H_
1158 struct getresuid_args {
1166 sys_getresuid(struct thread *td, struct getresuid_args *uap)
1169 int error1 = 0, error2 = 0, error3 = 0;
1171 cred = td->td_ucred;
1173 error1 = copyout(&cred->cr_ruid,
1174 uap->ruid, sizeof(cred->cr_ruid));
1176 error2 = copyout(&cred->cr_uid,
1177 uap->euid, sizeof(cred->cr_uid));
1179 error3 = copyout(&cred->cr_svuid,
1180 uap->suid, sizeof(cred->cr_svuid));
1181 return (error1 ? error1 : error2 ? error2 : error3);
1184 #ifndef _SYS_SYSPROTO_H_
1185 struct getresgid_args {
1193 sys_getresgid(struct thread *td, struct getresgid_args *uap)
1196 int error1 = 0, error2 = 0, error3 = 0;
1198 cred = td->td_ucred;
1200 error1 = copyout(&cred->cr_rgid,
1201 uap->rgid, sizeof(cred->cr_rgid));
1203 error2 = copyout(&cred->cr_groups[0],
1204 uap->egid, sizeof(cred->cr_groups[0]));
1206 error3 = copyout(&cred->cr_svgid,
1207 uap->sgid, sizeof(cred->cr_svgid));
1208 return (error1 ? error1 : error2 ? error2 : error3);
1211 #ifndef _SYS_SYSPROTO_H_
1212 struct issetugid_args {
1218 sys_issetugid(struct thread *td, struct issetugid_args *uap)
1220 struct proc *p = td->td_proc;
1223 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
1224 * we use P_SUGID because we consider changing the owners as
1225 * "tainting" as well.
1226 * This is significant for procs that start as root and "become"
1227 * a user without an exec - programs cannot know *everything*
1228 * that libc *might* have put in their data segment.
1230 td->td_retval[0] = (p->p_flag & P_SUGID) ? 1 : 0;
1235 sys___setugid(struct thread *td, struct __setugid_args *uap)
1241 switch (uap->flag) {
1244 p->p_flag &= ~P_SUGID;
1249 p->p_flag |= P_SUGID;
1255 #else /* !REGRESSION */
1258 #endif /* REGRESSION */
1262 * Check if gid is a member of the group set.
1265 groupmember(gid_t gid, struct ucred *cred)
1271 if (cred->cr_groups[0] == gid)
1275 * If gid was not our primary group, perform a binary search
1276 * of the supplemental groups. This is possible because we
1277 * sort the groups in crsetgroups().
1280 h = cred->cr_ngroups;
1282 m = l + ((h - l) / 2);
1283 if (cred->cr_groups[m] < gid)
1288 if ((l < cred->cr_ngroups) && (cred->cr_groups[l] == gid))
1295 * Test the active securelevel against a given level. securelevel_gt()
1296 * implements (securelevel > level). securelevel_ge() implements
1297 * (securelevel >= level). Note that the logic is inverted -- these
1298 * functions return EPERM on "success" and 0 on "failure".
1300 * Due to care taken when setting the securelevel, we know that no jail will
1301 * be less secure that its parent (or the physical system), so it is sufficient
1302 * to test the current jail only.
1304 * XXXRW: Possibly since this has to do with privilege, it should move to
1308 securelevel_gt(struct ucred *cr, int level)
1311 return (cr->cr_prison->pr_securelevel > level ? EPERM : 0);
1315 securelevel_ge(struct ucred *cr, int level)
1318 return (cr->cr_prison->pr_securelevel >= level ? EPERM : 0);
1322 * 'see_other_uids' determines whether or not visibility of processes
1323 * and sockets with credentials holding different real uids is possible
1324 * using a variety of system MIBs.
1325 * XXX: data declarations should be together near the beginning of the file.
1327 static int see_other_uids = 1;
1328 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_uids, CTLFLAG_RW,
1330 "Unprivileged processes may see subjects/objects with different real uid");
1333 * Determine if u1 "can see" the subject specified by u2, according to the
1334 * 'see_other_uids' policy.
1335 * Returns: 0 for permitted, ESRCH otherwise
1337 * References: *u1 and *u2 must not change during the call
1338 * u1 may equal u2, in which case only one reference is required
1341 cr_canseeotheruids(struct ucred *u1, struct ucred *u2)
1344 if (!see_other_uids && u1->cr_ruid != u2->cr_ruid) {
1345 if (priv_check_cred(u1, PRIV_SEEOTHERUIDS, 0) != 0)
1352 * 'see_other_gids' determines whether or not visibility of processes
1353 * and sockets with credentials holding different real gids is possible
1354 * using a variety of system MIBs.
1355 * XXX: data declarations should be together near the beginning of the file.
1357 static int see_other_gids = 1;
1358 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_gids, CTLFLAG_RW,
1360 "Unprivileged processes may see subjects/objects with different real gid");
1363 * Determine if u1 can "see" the subject specified by u2, according to the
1364 * 'see_other_gids' policy.
1365 * Returns: 0 for permitted, ESRCH otherwise
1367 * References: *u1 and *u2 must not change during the call
1368 * u1 may equal u2, in which case only one reference is required
1371 cr_canseeothergids(struct ucred *u1, struct ucred *u2)
1375 if (!see_other_gids) {
1377 for (i = 0; i < u1->cr_ngroups; i++) {
1378 if (groupmember(u1->cr_groups[i], u2))
1384 if (priv_check_cred(u1, PRIV_SEEOTHERGIDS, 0) != 0)
1392 * 'see_jail_proc' determines whether or not visibility of processes and
1393 * sockets with credentials holding different jail ids is possible using a
1394 * variety of system MIBs.
1396 * XXX: data declarations should be together near the beginning of the file.
1399 static int see_jail_proc = 1;
1400 SYSCTL_INT(_security_bsd, OID_AUTO, see_jail_proc, CTLFLAG_RW,
1402 "Unprivileged processes may see subjects/objects with different jail ids");
1405 * Determine if u1 "can see" the subject specified by u2, according to the
1406 * 'see_jail_proc' policy.
1407 * Returns: 0 for permitted, ESRCH otherwise
1409 * References: *u1 and *u2 must not change during the call
1410 * u1 may equal u2, in which case only one reference is required
1413 cr_canseejailproc(struct ucred *u1, struct ucred *u2)
1415 if (u1->cr_uid == 0)
1417 return (!see_jail_proc && u1->cr_prison != u2->cr_prison ? ESRCH : 0);
1421 * Determine if u1 "can see" the subject specified by u2.
1422 * Returns: 0 for permitted, an errno value otherwise
1424 * References: *u1 and *u2 must not change during the call
1425 * u1 may equal u2, in which case only one reference is required
1428 cr_cansee(struct ucred *u1, struct ucred *u2)
1432 if ((error = prison_check(u1, u2)))
1435 if ((error = mac_cred_check_visible(u1, u2)))
1438 if ((error = cr_canseeotheruids(u1, u2)))
1440 if ((error = cr_canseeothergids(u1, u2)))
1442 if ((error = cr_canseejailproc(u1, u2)))
1448 * Determine if td "can see" the subject specified by p.
1449 * Returns: 0 for permitted, an errno value otherwise
1450 * Locks: Sufficient locks to protect p->p_ucred must be held. td really
1451 * should be curthread.
1452 * References: td and p must be valid for the lifetime of the call
1455 p_cansee(struct thread *td, struct proc *p)
1458 /* Wrap cr_cansee() for all functionality. */
1459 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1460 PROC_LOCK_ASSERT(p, MA_OWNED);
1461 return (cr_cansee(td->td_ucred, p->p_ucred));
1465 * 'conservative_signals' prevents the delivery of a broad class of
1466 * signals by unprivileged processes to processes that have changed their
1467 * credentials since the last invocation of execve(). This can prevent
1468 * the leakage of cached information or retained privileges as a result
1469 * of a common class of signal-related vulnerabilities. However, this
1470 * may interfere with some applications that expect to be able to
1471 * deliver these signals to peer processes after having given up
1474 static int conservative_signals = 1;
1475 SYSCTL_INT(_security_bsd, OID_AUTO, conservative_signals, CTLFLAG_RW,
1476 &conservative_signals, 0, "Unprivileged processes prevented from "
1477 "sending certain signals to processes whose credentials have changed");
1479 * Determine whether cred may deliver the specified signal to proc.
1480 * Returns: 0 for permitted, an errno value otherwise.
1481 * Locks: A lock must be held for proc.
1482 * References: cred and proc must be valid for the lifetime of the call.
1485 cr_cansignal(struct ucred *cred, struct proc *proc, int signum)
1489 PROC_LOCK_ASSERT(proc, MA_OWNED);
1491 * Jail semantics limit the scope of signalling to proc in the
1492 * same jail as cred, if cred is in jail.
1494 error = prison_check(cred, proc->p_ucred);
1498 if ((error = mac_proc_check_signal(cred, proc, signum)))
1501 if ((error = cr_canseeotheruids(cred, proc->p_ucred)))
1503 if ((error = cr_canseeothergids(cred, proc->p_ucred)))
1507 * UNIX signal semantics depend on the status of the P_SUGID
1508 * bit on the target process. If the bit is set, then additional
1509 * restrictions are placed on the set of available signals.
1511 if (conservative_signals && (proc->p_flag & P_SUGID)) {
1526 * Generally, permit job and terminal control
1531 /* Not permitted without privilege. */
1532 error = priv_check_cred(cred, PRIV_SIGNAL_SUGID, 0);
1539 * Generally, the target credential's ruid or svuid must match the
1540 * subject credential's ruid or euid.
1542 if (cred->cr_ruid != proc->p_ucred->cr_ruid &&
1543 cred->cr_ruid != proc->p_ucred->cr_svuid &&
1544 cred->cr_uid != proc->p_ucred->cr_ruid &&
1545 cred->cr_uid != proc->p_ucred->cr_svuid) {
1546 error = priv_check_cred(cred, PRIV_SIGNAL_DIFFCRED, 0);
1555 * Determine whether td may deliver the specified signal to p.
1556 * Returns: 0 for permitted, an errno value otherwise
1557 * Locks: Sufficient locks to protect various components of td and p
1558 * must be held. td must be curthread, and a lock must be
1560 * References: td and p must be valid for the lifetime of the call
1563 p_cansignal(struct thread *td, struct proc *p, int signum)
1566 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1567 PROC_LOCK_ASSERT(p, MA_OWNED);
1568 if (td->td_proc == p)
1572 * UNIX signalling semantics require that processes in the same
1573 * session always be able to deliver SIGCONT to one another,
1574 * overriding the remaining protections.
1576 /* XXX: This will require an additional lock of some sort. */
1577 if (signum == SIGCONT && td->td_proc->p_session == p->p_session)
1580 * Some compat layers use SIGTHR and higher signals for
1581 * communication between different kernel threads of the same
1582 * process, so that they expect that it's always possible to
1583 * deliver them, even for suid applications where cr_cansignal() can
1584 * deny such ability for security consideration. It should be
1585 * pretty safe to do since the only way to create two processes
1586 * with the same p_leader is via rfork(2).
1588 if (td->td_proc->p_leader != NULL && signum >= SIGTHR &&
1589 signum < SIGTHR + 4 && td->td_proc->p_leader == p->p_leader)
1592 return (cr_cansignal(td->td_ucred, p, signum));
1596 * Determine whether td may reschedule p.
1597 * Returns: 0 for permitted, an errno value otherwise
1598 * Locks: Sufficient locks to protect various components of td and p
1599 * must be held. td must be curthread, and a lock must
1601 * References: td and p must be valid for the lifetime of the call
1604 p_cansched(struct thread *td, struct proc *p)
1608 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1609 PROC_LOCK_ASSERT(p, MA_OWNED);
1610 if (td->td_proc == p)
1612 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1615 if ((error = mac_proc_check_sched(td->td_ucred, p)))
1618 if ((error = cr_canseeotheruids(td->td_ucred, p->p_ucred)))
1620 if ((error = cr_canseeothergids(td->td_ucred, p->p_ucred)))
1622 if (td->td_ucred->cr_ruid != p->p_ucred->cr_ruid &&
1623 td->td_ucred->cr_uid != p->p_ucred->cr_ruid) {
1624 error = priv_check(td, PRIV_SCHED_DIFFCRED);
1632 * The 'unprivileged_proc_debug' flag may be used to disable a variety of
1633 * unprivileged inter-process debugging services, including some procfs
1634 * functionality, ptrace(), and ktrace(). In the past, inter-process
1635 * debugging has been involved in a variety of security problems, and sites
1636 * not requiring the service might choose to disable it when hardening
1639 * XXX: Should modifying and reading this variable require locking?
1640 * XXX: data declarations should be together near the beginning of the file.
1642 static int unprivileged_proc_debug = 1;
1643 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_proc_debug, CTLFLAG_RW,
1644 &unprivileged_proc_debug, 0,
1645 "Unprivileged processes may use process debugging facilities");
1648 * Determine whether td may debug p.
1649 * Returns: 0 for permitted, an errno value otherwise
1650 * Locks: Sufficient locks to protect various components of td and p
1651 * must be held. td must be curthread, and a lock must
1653 * References: td and p must be valid for the lifetime of the call
1656 p_candebug(struct thread *td, struct proc *p)
1658 int credentialchanged, error, grpsubset, i, uidsubset;
1660 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1661 PROC_LOCK_ASSERT(p, MA_OWNED);
1662 if (!unprivileged_proc_debug) {
1663 error = priv_check(td, PRIV_DEBUG_UNPRIV);
1667 if (td->td_proc == p)
1669 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1672 if ((error = mac_proc_check_debug(td->td_ucred, p)))
1675 if ((error = cr_canseeotheruids(td->td_ucred, p->p_ucred)))
1677 if ((error = cr_canseeothergids(td->td_ucred, p->p_ucred)))
1681 * Is p's group set a subset of td's effective group set? This
1682 * includes p's egid, group access list, rgid, and svgid.
1685 for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
1686 if (!groupmember(p->p_ucred->cr_groups[i], td->td_ucred)) {
1691 grpsubset = grpsubset &&
1692 groupmember(p->p_ucred->cr_rgid, td->td_ucred) &&
1693 groupmember(p->p_ucred->cr_svgid, td->td_ucred);
1696 * Are the uids present in p's credential equal to td's
1697 * effective uid? This includes p's euid, svuid, and ruid.
1699 uidsubset = (td->td_ucred->cr_uid == p->p_ucred->cr_uid &&
1700 td->td_ucred->cr_uid == p->p_ucred->cr_svuid &&
1701 td->td_ucred->cr_uid == p->p_ucred->cr_ruid);
1704 * Has the credential of the process changed since the last exec()?
1706 credentialchanged = (p->p_flag & P_SUGID);
1709 * If p's gids aren't a subset, or the uids aren't a subset,
1710 * or the credential has changed, require appropriate privilege
1711 * for td to debug p.
1713 if (!grpsubset || !uidsubset) {
1714 error = priv_check(td, PRIV_DEBUG_DIFFCRED);
1719 if (credentialchanged) {
1720 error = priv_check(td, PRIV_DEBUG_SUGID);
1725 /* Can't trace init when securelevel > 0. */
1726 if (p == initproc) {
1727 error = securelevel_gt(td->td_ucred, 0);
1733 * Can't trace a process that's currently exec'ing.
1735 * XXX: Note, this is not a security policy decision, it's a
1736 * basic correctness/functionality decision. Therefore, this check
1737 * should be moved to the caller's of p_candebug().
1739 if ((p->p_flag & P_INEXEC) != 0)
1742 /* Denied explicitely */
1743 if ((p->p_flag2 & P2_NOTRACE) != 0) {
1744 error = priv_check(td, PRIV_DEBUG_DENIED);
1753 * Determine whether the subject represented by cred can "see" a socket.
1754 * Returns: 0 for permitted, ENOENT otherwise.
1757 cr_canseesocket(struct ucred *cred, struct socket *so)
1761 error = prison_check(cred, so->so_cred);
1765 error = mac_socket_check_visible(cred, so);
1769 if (cr_canseeotheruids(cred, so->so_cred))
1771 if (cr_canseeothergids(cred, so->so_cred))
1778 * Determine whether td can wait for the exit of p.
1779 * Returns: 0 for permitted, an errno value otherwise
1780 * Locks: Sufficient locks to protect various components of td and p
1781 * must be held. td must be curthread, and a lock must
1783 * References: td and p must be valid for the lifetime of the call
1787 p_canwait(struct thread *td, struct proc *p)
1791 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1792 PROC_LOCK_ASSERT(p, MA_OWNED);
1793 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1796 if ((error = mac_proc_check_wait(td->td_ucred, p)))
1800 /* XXXMAC: This could have odd effects on some shells. */
1801 if ((error = cr_canseeotheruids(td->td_ucred, p->p_ucred)))
1809 * Allocate a zeroed cred structure.
1816 cr = malloc(sizeof(*cr), M_CRED, M_WAITOK | M_ZERO);
1817 refcount_init(&cr->cr_ref, 1);
1819 audit_cred_init(cr);
1824 cr->cr_groups = cr->cr_smallgroups;
1826 sizeof(cr->cr_smallgroups) / sizeof(cr->cr_smallgroups[0]);
1831 * Claim another reference to a ucred structure.
1834 crhold(struct ucred *cr)
1837 refcount_acquire(&cr->cr_ref);
1842 * Free a cred structure. Throws away space when ref count gets to 0.
1845 crfree(struct ucred *cr)
1848 KASSERT(cr->cr_ref > 0, ("bad ucred refcount: %d", cr->cr_ref));
1849 KASSERT(cr->cr_ref != 0xdeadc0de, ("dangling reference to ucred"));
1850 if (refcount_release(&cr->cr_ref)) {
1852 * Some callers of crget(), such as nfs_statfs(),
1853 * allocate a temporary credential, but don't
1854 * allocate a uidinfo structure.
1856 if (cr->cr_uidinfo != NULL)
1857 uifree(cr->cr_uidinfo);
1858 if (cr->cr_ruidinfo != NULL)
1859 uifree(cr->cr_ruidinfo);
1861 * Free a prison, if any.
1863 if (cr->cr_prison != NULL)
1864 prison_free(cr->cr_prison);
1865 if (cr->cr_loginclass != NULL)
1866 loginclass_free(cr->cr_loginclass);
1868 audit_cred_destroy(cr);
1871 mac_cred_destroy(cr);
1873 if (cr->cr_groups != cr->cr_smallgroups)
1874 free(cr->cr_groups, M_CRED);
1880 * Copy a ucred's contents from a template. Does not block.
1883 crcopy(struct ucred *dest, struct ucred *src)
1886 KASSERT(dest->cr_ref == 1, ("crcopy of shared ucred"));
1887 bcopy(&src->cr_startcopy, &dest->cr_startcopy,
1888 (unsigned)((caddr_t)&src->cr_endcopy -
1889 (caddr_t)&src->cr_startcopy));
1890 crsetgroups(dest, src->cr_ngroups, src->cr_groups);
1891 uihold(dest->cr_uidinfo);
1892 uihold(dest->cr_ruidinfo);
1893 prison_hold(dest->cr_prison);
1894 loginclass_hold(dest->cr_loginclass);
1896 audit_cred_copy(src, dest);
1899 mac_cred_copy(src, dest);
1904 * Dup cred struct to a new held one.
1907 crdup(struct ucred *cr)
1909 struct ucred *newcr;
1917 * Fill in a struct xucred based on a struct ucred.
1920 cru2x(struct ucred *cr, struct xucred *xcr)
1924 bzero(xcr, sizeof(*xcr));
1925 xcr->cr_version = XUCRED_VERSION;
1926 xcr->cr_uid = cr->cr_uid;
1928 ngroups = MIN(cr->cr_ngroups, XU_NGROUPS);
1929 xcr->cr_ngroups = ngroups;
1930 bcopy(cr->cr_groups, xcr->cr_groups,
1931 ngroups * sizeof(*cr->cr_groups));
1935 cru2xt(struct thread *td, struct xucred *xcr)
1938 cru2x(td->td_ucred, xcr);
1939 xcr->cr_pid = td->td_proc->p_pid;
1943 * Set initial process credentials.
1944 * Callers are responsible for providing the reference for provided credentials.
1947 proc_set_cred_init(struct proc *p, struct ucred *newcred)
1950 p->p_ucred = newcred;
1954 * Change process credentials.
1955 * Callers are responsible for providing the reference for passed credentials
1956 * and for freeing old ones.
1958 * Process has to be locked except when it does not have credentials (as it
1959 * should not be visible just yet) or when newcred is NULL (as this can be
1960 * only used when the process is about to be freed, at which point it should
1961 * not be visible anymore).
1964 proc_set_cred(struct proc *p, struct ucred *newcred)
1966 struct ucred *oldcred;
1968 MPASS(p->p_ucred != NULL);
1969 if (newcred == NULL)
1970 MPASS(p->p_state == PRS_ZOMBIE);
1972 PROC_LOCK_ASSERT(p, MA_OWNED);
1974 oldcred = p->p_ucred;
1975 p->p_ucred = newcred;
1976 if (newcred != NULL)
1982 crcopysafe(struct proc *p, struct ucred *cr)
1984 struct ucred *oldcred;
1987 PROC_LOCK_ASSERT(p, MA_OWNED);
1989 oldcred = p->p_ucred;
1990 while (cr->cr_agroups < oldcred->cr_agroups) {
1991 groups = oldcred->cr_agroups;
1993 crextend(cr, groups);
1995 oldcred = p->p_ucred;
1997 crcopy(cr, oldcred);
2003 * Extend the passed in credential to hold n items.
2006 crextend(struct ucred *cr, int n)
2011 if (n <= cr->cr_agroups)
2015 * We extend by 2 each time since we're using a power of two
2016 * allocator until we need enough groups to fill a page.
2017 * Once we're allocating multiple pages, only allocate as many
2018 * as we actually need. The case of processes needing a
2019 * non-power of two number of pages seems more likely than
2020 * a real world process that adds thousands of groups one at a
2023 if ( n < PAGE_SIZE / sizeof(gid_t) ) {
2024 if (cr->cr_agroups == 0)
2025 cnt = MINALLOCSIZE / sizeof(gid_t);
2027 cnt = cr->cr_agroups * 2;
2032 cnt = roundup2(n, PAGE_SIZE / sizeof(gid_t));
2034 /* Free the old array. */
2035 if (cr->cr_groups != cr->cr_smallgroups)
2036 free(cr->cr_groups, M_CRED);
2038 cr->cr_groups = malloc(cnt * sizeof(gid_t), M_CRED, M_WAITOK | M_ZERO);
2039 cr->cr_agroups = cnt;
2043 * Copy groups in to a credential, preserving any necessary invariants.
2044 * Currently this includes the sorting of all supplemental gids.
2045 * crextend() must have been called before hand to ensure sufficient
2046 * space is available.
2049 crsetgroups_locked(struct ucred *cr, int ngrp, gid_t *groups)
2055 KASSERT(cr->cr_agroups >= ngrp, ("cr_ngroups is too small"));
2057 bcopy(groups, cr->cr_groups, ngrp * sizeof(gid_t));
2058 cr->cr_ngroups = ngrp;
2061 * Sort all groups except cr_groups[0] to allow groupmember to
2062 * perform a binary search.
2064 * XXX: If large numbers of groups become common this should
2065 * be replaced with shell sort like linux uses or possibly
2068 for (i = 2; i < ngrp; i++) {
2069 g = cr->cr_groups[i];
2070 for (j = i-1; j >= 1 && g < cr->cr_groups[j]; j--)
2071 cr->cr_groups[j + 1] = cr->cr_groups[j];
2072 cr->cr_groups[j + 1] = g;
2077 * Copy groups in to a credential after expanding it if required.
2078 * Truncate the list to (ngroups_max + 1) if it is too large.
2081 crsetgroups(struct ucred *cr, int ngrp, gid_t *groups)
2084 if (ngrp > ngroups_max + 1)
2085 ngrp = ngroups_max + 1;
2088 crsetgroups_locked(cr, ngrp, groups);
2092 * Get login name, if available.
2094 #ifndef _SYS_SYSPROTO_H_
2095 struct getlogin_args {
2102 sys_getlogin(struct thread *td, struct getlogin_args *uap)
2104 char login[MAXLOGNAME];
2105 struct proc *p = td->td_proc;
2108 if (uap->namelen > MAXLOGNAME)
2109 uap->namelen = MAXLOGNAME;
2111 SESS_LOCK(p->p_session);
2112 len = strlcpy(login, p->p_session->s_login, uap->namelen) + 1;
2113 SESS_UNLOCK(p->p_session);
2115 if (len > uap->namelen)
2117 return (copyout(login, uap->namebuf, len));
2123 #ifndef _SYS_SYSPROTO_H_
2124 struct setlogin_args {
2130 sys_setlogin(struct thread *td, struct setlogin_args *uap)
2132 struct proc *p = td->td_proc;
2134 char logintmp[MAXLOGNAME];
2136 CTASSERT(sizeof(p->p_session->s_login) >= sizeof(logintmp));
2138 error = priv_check(td, PRIV_PROC_SETLOGIN);
2141 error = copyinstr(uap->namebuf, logintmp, sizeof(logintmp), NULL);
2143 if (error == ENAMETOOLONG)
2147 AUDIT_ARG_LOGIN(logintmp);
2149 SESS_LOCK(p->p_session);
2150 strcpy(p->p_session->s_login, logintmp);
2151 SESS_UNLOCK(p->p_session);
2157 setsugid(struct proc *p)
2160 PROC_LOCK_ASSERT(p, MA_OWNED);
2161 p->p_flag |= P_SUGID;
2162 if (!(p->p_pfsflags & PF_ISUGID))
2167 * Change a process's effective uid.
2168 * Side effects: newcred->cr_uid and newcred->cr_uidinfo will be modified.
2169 * References: newcred must be an exclusive credential reference for the
2170 * duration of the call.
2173 change_euid(struct ucred *newcred, struct uidinfo *euip)
2176 newcred->cr_uid = euip->ui_uid;
2178 uifree(newcred->cr_uidinfo);
2179 newcred->cr_uidinfo = euip;
2183 * Change a process's effective gid.
2184 * Side effects: newcred->cr_gid will be modified.
2185 * References: newcred must be an exclusive credential reference for the
2186 * duration of the call.
2189 change_egid(struct ucred *newcred, gid_t egid)
2192 newcred->cr_groups[0] = egid;
2196 * Change a process's real uid.
2197 * Side effects: newcred->cr_ruid will be updated, newcred->cr_ruidinfo
2198 * will be updated, and the old and new cr_ruidinfo proc
2199 * counts will be updated.
2200 * References: newcred must be an exclusive credential reference for the
2201 * duration of the call.
2204 change_ruid(struct ucred *newcred, struct uidinfo *ruip)
2207 (void)chgproccnt(newcred->cr_ruidinfo, -1, 0);
2208 newcred->cr_ruid = ruip->ui_uid;
2210 uifree(newcred->cr_ruidinfo);
2211 newcred->cr_ruidinfo = ruip;
2212 (void)chgproccnt(newcred->cr_ruidinfo, 1, 0);
2216 * Change a process's real gid.
2217 * Side effects: newcred->cr_rgid will be updated.
2218 * References: newcred must be an exclusive credential reference for the
2219 * duration of the call.
2222 change_rgid(struct ucred *newcred, gid_t rgid)
2225 newcred->cr_rgid = rgid;
2229 * Change a process's saved uid.
2230 * Side effects: newcred->cr_svuid will be updated.
2231 * References: newcred must be an exclusive credential reference for the
2232 * duration of the call.
2235 change_svuid(struct ucred *newcred, uid_t svuid)
2238 newcred->cr_svuid = svuid;
2242 * Change a process's saved gid.
2243 * Side effects: newcred->cr_svgid will be updated.
2244 * References: newcred must be an exclusive credential reference for the
2245 * duration of the call.
2248 change_svgid(struct ucred *newcred, gid_t svgid)
2251 newcred->cr_svgid = svgid;