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/sysproto.h>
67 #include <sys/pioctl.h>
68 #include <sys/racct.h>
69 #include <sys/resourcevar.h>
70 #include <sys/socket.h>
71 #include <sys/socketvar.h>
72 #include <sys/syscallsubr.h>
73 #include <sys/sysctl.h>
77 "Kernel support for interfaces necessary for regression testing (SECURITY RISK!)");
80 #include <security/audit/audit.h>
81 #include <security/mac/mac_framework.h>
83 static MALLOC_DEFINE(M_CRED, "cred", "credentials");
85 SYSCTL_NODE(_security, OID_AUTO, bsd, CTLFLAG_RW, 0, "BSD security policy");
87 static void crsetgroups_locked(struct ucred *cr, int ngrp,
90 #ifndef _SYS_SYSPROTO_H_
97 sys_getpid(struct thread *td, struct getpid_args *uap)
99 struct proc *p = td->td_proc;
101 td->td_retval[0] = p->p_pid;
102 #if defined(COMPAT_43)
103 td->td_retval[1] = kern_getppid(td);
108 #ifndef _SYS_SYSPROTO_H_
109 struct getppid_args {
115 sys_getppid(struct thread *td, struct getppid_args *uap)
118 td->td_retval[0] = kern_getppid(td);
123 kern_getppid(struct thread *td)
125 struct proc *p = td->td_proc;
130 if (!(p->p_flag & P_TRACED)) {
131 ppid = p->p_pptr->p_pid;
135 sx_slock(&proctree_lock);
136 pp = proc_realparent(p);
138 sx_sunlock(&proctree_lock);
145 * Get process group ID; note that POSIX getpgrp takes no parameter.
147 #ifndef _SYS_SYSPROTO_H_
148 struct getpgrp_args {
153 sys_getpgrp(struct thread *td, struct getpgrp_args *uap)
155 struct proc *p = td->td_proc;
158 td->td_retval[0] = p->p_pgrp->pg_id;
163 /* Get an arbitrary pid's process group id */
164 #ifndef _SYS_SYSPROTO_H_
165 struct getpgid_args {
170 sys_getpgid(struct thread *td, struct getpgid_args *uap)
182 error = p_cansee(td, p);
188 td->td_retval[0] = p->p_pgrp->pg_id;
194 * Get an arbitrary pid's session id.
196 #ifndef _SYS_SYSPROTO_H_
202 sys_getsid(struct thread *td, struct getsid_args *uap)
214 error = p_cansee(td, p);
220 td->td_retval[0] = p->p_session->s_sid;
225 #ifndef _SYS_SYSPROTO_H_
232 sys_getuid(struct thread *td, struct getuid_args *uap)
235 td->td_retval[0] = td->td_ucred->cr_ruid;
236 #if defined(COMPAT_43)
237 td->td_retval[1] = td->td_ucred->cr_uid;
242 #ifndef _SYS_SYSPROTO_H_
243 struct geteuid_args {
249 sys_geteuid(struct thread *td, struct geteuid_args *uap)
252 td->td_retval[0] = td->td_ucred->cr_uid;
256 #ifndef _SYS_SYSPROTO_H_
263 sys_getgid(struct thread *td, struct getgid_args *uap)
266 td->td_retval[0] = td->td_ucred->cr_rgid;
267 #if defined(COMPAT_43)
268 td->td_retval[1] = td->td_ucred->cr_groups[0];
274 * Get effective group ID. The "egid" is groups[0], and could be obtained
275 * via getgroups. This syscall exists because it is somewhat painful to do
276 * correctly in a library function.
278 #ifndef _SYS_SYSPROTO_H_
279 struct getegid_args {
285 sys_getegid(struct thread *td, struct getegid_args *uap)
288 td->td_retval[0] = td->td_ucred->cr_groups[0];
292 #ifndef _SYS_SYSPROTO_H_
293 struct getgroups_args {
299 sys_getgroups(struct thread *td, struct getgroups_args *uap)
306 ngrp = cred->cr_ngroups;
308 if (uap->gidsetsize == 0) {
312 if (uap->gidsetsize < ngrp)
315 error = copyout(cred->cr_groups, uap->gidset, ngrp * sizeof(gid_t));
317 td->td_retval[0] = ngrp;
321 #ifndef _SYS_SYSPROTO_H_
328 sys_setsid(struct thread *td, struct setsid_args *uap)
332 struct proc *p = td->td_proc;
333 struct pgrp *newpgrp;
334 struct session *newsess;
339 newpgrp = malloc(sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
340 newsess = malloc(sizeof(struct session), M_SESSION, M_WAITOK | M_ZERO);
342 sx_xlock(&proctree_lock);
344 if (p->p_pgid == p->p_pid || (pgrp = pgfind(p->p_pid)) != NULL) {
349 (void)enterpgrp(p, p->p_pid, newpgrp, newsess);
350 td->td_retval[0] = p->p_pid;
355 sx_xunlock(&proctree_lock);
358 free(newpgrp, M_PGRP);
360 free(newsess, M_SESSION);
366 * set process group (setpgid/old setpgrp)
368 * caller does setpgid(targpid, targpgid)
370 * pid must be caller or child of caller (ESRCH)
372 * pid must be in same session (EPERM)
373 * pid can't have done an exec (EACCES)
375 * there must exist some pid in same session having pgid (EPERM)
376 * pid must not be session leader (EPERM)
378 #ifndef _SYS_SYSPROTO_H_
379 struct setpgid_args {
380 int pid; /* target process id */
381 int pgid; /* target pgrp id */
386 sys_setpgid(struct thread *td, struct setpgid_args *uap)
388 struct proc *curp = td->td_proc;
389 struct proc *targp; /* target process */
390 struct pgrp *pgrp; /* target pgrp */
392 struct pgrp *newpgrp;
399 newpgrp = malloc(sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
401 sx_xlock(&proctree_lock);
402 if (uap->pid != 0 && uap->pid != curp->p_pid) {
403 if ((targp = pfind(uap->pid)) == NULL) {
407 if (!inferior(targp)) {
412 if ((error = p_cansee(td, targp))) {
416 if (targp->p_pgrp == NULL ||
417 targp->p_session != curp->p_session) {
422 if (targp->p_flag & P_EXEC) {
430 if (SESS_LEADER(targp)) {
435 uap->pgid = targp->p_pid;
436 if ((pgrp = pgfind(uap->pgid)) == NULL) {
437 if (uap->pgid == targp->p_pid) {
438 error = enterpgrp(targp, uap->pgid, newpgrp,
445 if (pgrp == targp->p_pgrp) {
449 if (pgrp->pg_id != targp->p_pid &&
450 pgrp->pg_session != curp->p_session) {
456 error = enterthispgrp(targp, pgrp);
459 sx_xunlock(&proctree_lock);
460 KASSERT((error == 0) || (newpgrp != NULL),
461 ("setpgid failed and newpgrp is NULL"));
463 free(newpgrp, M_PGRP);
468 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
469 * compatible. It says that setting the uid/gid to euid/egid is a special
470 * case of "appropriate privilege". Once the rules are expanded out, this
471 * basically means that setuid(nnn) sets all three id's, in all permitted
472 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
473 * does not set the saved id - this is dangerous for traditional BSD
474 * programs. For this reason, we *really* do not want to set
475 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
477 #define POSIX_APPENDIX_B_4_2_2
479 #ifndef _SYS_SYSPROTO_H_
486 sys_setuid(struct thread *td, struct setuid_args *uap)
488 struct proc *p = td->td_proc;
489 struct ucred *newcred, *oldcred;
500 * Copy credentials so other references do not see our changes.
502 oldcred = crcopysafe(p, newcred);
505 error = mac_cred_check_setuid(oldcred, uid);
511 * See if we have "permission" by POSIX 1003.1 rules.
513 * Note that setuid(geteuid()) is a special case of
514 * "appropriate privileges" in appendix B.4.2.2. We need
515 * to use this clause to be compatible with traditional BSD
516 * semantics. Basically, it means that "setuid(xx)" sets all
517 * three id's (assuming you have privs).
519 * Notes on the logic. We do things in three steps.
520 * 1: We determine if the euid is going to change, and do EPERM
521 * right away. We unconditionally change the euid later if this
522 * test is satisfied, simplifying that part of the logic.
523 * 2: We determine if the real and/or saved uids are going to
524 * change. Determined by compile options.
525 * 3: Change euid last. (after tests in #2 for "appropriate privs")
527 if (uid != oldcred->cr_ruid && /* allow setuid(getuid()) */
528 #ifdef _POSIX_SAVED_IDS
529 uid != oldcred->cr_svuid && /* allow setuid(saved gid) */
531 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
532 uid != oldcred->cr_uid && /* allow setuid(geteuid()) */
534 (error = priv_check_cred(oldcred, PRIV_CRED_SETUID, 0)) != 0)
537 #ifdef _POSIX_SAVED_IDS
539 * Do we have "appropriate privileges" (are we root or uid == euid)
540 * If so, we are changing the real uid and/or saved uid.
543 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
544 uid == oldcred->cr_uid ||
546 /* We are using privs. */
547 priv_check_cred(oldcred, PRIV_CRED_SETUID, 0) == 0)
551 * Set the real uid and transfer proc count to new user.
553 if (uid != oldcred->cr_ruid) {
554 change_ruid(newcred, uip);
560 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
561 * the security of seteuid() depends on it. B.4.2.2 says it
562 * is important that we should do this.
564 if (uid != oldcred->cr_svuid) {
565 change_svuid(newcred, uid);
571 * In all permitted cases, we are changing the euid.
573 if (uid != oldcred->cr_uid) {
574 change_euid(newcred, uip);
577 proc_set_cred(p, newcred);
580 racct_proc_ucred_changed(p, oldcred, newcred);
593 #ifndef _SYS_SYSPROTO_H_
594 struct seteuid_args {
600 sys_seteuid(struct thread *td, struct seteuid_args *uap)
602 struct proc *p = td->td_proc;
603 struct ucred *newcred, *oldcred;
605 struct uidinfo *euip;
609 AUDIT_ARG_EUID(euid);
614 * Copy credentials so other references do not see our changes.
616 oldcred = crcopysafe(p, newcred);
619 error = mac_cred_check_seteuid(oldcred, euid);
624 if (euid != oldcred->cr_ruid && /* allow seteuid(getuid()) */
625 euid != oldcred->cr_svuid && /* allow seteuid(saved uid) */
626 (error = priv_check_cred(oldcred, PRIV_CRED_SETEUID, 0)) != 0)
630 * Everything's okay, do it.
632 if (oldcred->cr_uid != euid) {
633 change_euid(newcred, euip);
636 proc_set_cred(p, newcred);
649 #ifndef _SYS_SYSPROTO_H_
656 sys_setgid(struct thread *td, struct setgid_args *uap)
658 struct proc *p = td->td_proc;
659 struct ucred *newcred, *oldcred;
667 oldcred = crcopysafe(p, newcred);
670 error = mac_cred_check_setgid(oldcred, gid);
676 * See if we have "permission" by POSIX 1003.1 rules.
678 * Note that setgid(getegid()) is a special case of
679 * "appropriate privileges" in appendix B.4.2.2. We need
680 * to use this clause to be compatible with traditional BSD
681 * semantics. Basically, it means that "setgid(xx)" sets all
682 * three id's (assuming you have privs).
684 * For notes on the logic here, see setuid() above.
686 if (gid != oldcred->cr_rgid && /* allow setgid(getgid()) */
687 #ifdef _POSIX_SAVED_IDS
688 gid != oldcred->cr_svgid && /* allow setgid(saved gid) */
690 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
691 gid != oldcred->cr_groups[0] && /* allow setgid(getegid()) */
693 (error = priv_check_cred(oldcred, PRIV_CRED_SETGID, 0)) != 0)
696 #ifdef _POSIX_SAVED_IDS
698 * Do we have "appropriate privileges" (are we root or gid == egid)
699 * If so, we are changing the real uid and saved gid.
702 #ifdef POSIX_APPENDIX_B_4_2_2 /* use the clause from B.4.2.2 */
703 gid == oldcred->cr_groups[0] ||
705 /* We are using privs. */
706 priv_check_cred(oldcred, PRIV_CRED_SETGID, 0) == 0)
712 if (oldcred->cr_rgid != gid) {
713 change_rgid(newcred, gid);
719 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
720 * the security of setegid() depends on it. B.4.2.2 says it
721 * is important that we should do this.
723 if (oldcred->cr_svgid != gid) {
724 change_svgid(newcred, gid);
729 * In all cases permitted cases, we are changing the egid.
730 * Copy credentials so other references do not see our changes.
732 if (oldcred->cr_groups[0] != gid) {
733 change_egid(newcred, gid);
736 proc_set_cred(p, newcred);
747 #ifndef _SYS_SYSPROTO_H_
748 struct setegid_args {
754 sys_setegid(struct thread *td, struct setegid_args *uap)
756 struct proc *p = td->td_proc;
757 struct ucred *newcred, *oldcred;
762 AUDIT_ARG_EGID(egid);
765 oldcred = crcopysafe(p, newcred);
768 error = mac_cred_check_setegid(oldcred, egid);
773 if (egid != oldcred->cr_rgid && /* allow setegid(getgid()) */
774 egid != oldcred->cr_svgid && /* allow setegid(saved gid) */
775 (error = priv_check_cred(oldcred, PRIV_CRED_SETEGID, 0)) != 0)
778 if (oldcred->cr_groups[0] != egid) {
779 change_egid(newcred, egid);
782 proc_set_cred(p, newcred);
793 #ifndef _SYS_SYSPROTO_H_
794 struct setgroups_args {
801 sys_setgroups(struct thread *td, struct setgroups_args *uap)
803 gid_t smallgroups[XU_NGROUPS];
808 gidsetsize = uap->gidsetsize;
809 if (gidsetsize > ngroups_max + 1)
812 if (gidsetsize > XU_NGROUPS)
813 groups = malloc(gidsetsize * sizeof(gid_t), M_TEMP, M_WAITOK);
815 groups = smallgroups;
817 error = copyin(uap->gidset, groups, gidsetsize * sizeof(gid_t));
819 error = kern_setgroups(td, gidsetsize, groups);
821 if (gidsetsize > XU_NGROUPS)
822 free(groups, M_TEMP);
827 kern_setgroups(struct thread *td, u_int ngrp, gid_t *groups)
829 struct proc *p = td->td_proc;
830 struct ucred *newcred, *oldcred;
833 MPASS(ngrp <= ngroups_max + 1);
834 AUDIT_ARG_GROUPSET(groups, ngrp);
836 crextend(newcred, ngrp);
838 oldcred = crcopysafe(p, newcred);
841 error = mac_cred_check_setgroups(oldcred, ngrp, groups);
846 error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0);
852 * setgroups(0, NULL) is a legitimate way of clearing the
853 * groups vector on non-BSD systems (which generally do not
854 * have the egid in the groups[0]). We risk security holes
855 * when running non-BSD software if we do not do the same.
857 newcred->cr_ngroups = 1;
859 crsetgroups_locked(newcred, ngrp, groups);
862 proc_set_cred(p, newcred);
873 #ifndef _SYS_SYSPROTO_H_
874 struct setreuid_args {
881 sys_setreuid(struct thread *td, struct setreuid_args *uap)
883 struct proc *p = td->td_proc;
884 struct ucred *newcred, *oldcred;
886 struct uidinfo *euip, *ruip;
891 AUDIT_ARG_EUID(euid);
892 AUDIT_ARG_RUID(ruid);
897 oldcred = crcopysafe(p, newcred);
900 error = mac_cred_check_setreuid(oldcred, ruid, euid);
905 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
906 ruid != oldcred->cr_svuid) ||
907 (euid != (uid_t)-1 && euid != oldcred->cr_uid &&
908 euid != oldcred->cr_ruid && euid != oldcred->cr_svuid)) &&
909 (error = priv_check_cred(oldcred, PRIV_CRED_SETREUID, 0)) != 0)
912 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
913 change_euid(newcred, euip);
916 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
917 change_ruid(newcred, ruip);
920 if ((ruid != (uid_t)-1 || newcred->cr_uid != newcred->cr_ruid) &&
921 newcred->cr_svuid != newcred->cr_uid) {
922 change_svuid(newcred, newcred->cr_uid);
925 proc_set_cred(p, newcred);
928 racct_proc_ucred_changed(p, oldcred, newcred);
943 #ifndef _SYS_SYSPROTO_H_
944 struct setregid_args {
951 sys_setregid(struct thread *td, struct setregid_args *uap)
953 struct proc *p = td->td_proc;
954 struct ucred *newcred, *oldcred;
960 AUDIT_ARG_EGID(egid);
961 AUDIT_ARG_RGID(rgid);
964 oldcred = crcopysafe(p, newcred);
967 error = mac_cred_check_setregid(oldcred, rgid, egid);
972 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
973 rgid != oldcred->cr_svgid) ||
974 (egid != (gid_t)-1 && egid != oldcred->cr_groups[0] &&
975 egid != oldcred->cr_rgid && egid != oldcred->cr_svgid)) &&
976 (error = priv_check_cred(oldcred, PRIV_CRED_SETREGID, 0)) != 0)
979 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
980 change_egid(newcred, egid);
983 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
984 change_rgid(newcred, rgid);
987 if ((rgid != (gid_t)-1 || newcred->cr_groups[0] != newcred->cr_rgid) &&
988 newcred->cr_svgid != newcred->cr_groups[0]) {
989 change_svgid(newcred, newcred->cr_groups[0]);
992 proc_set_cred(p, newcred);
1004 * setresuid(ruid, euid, suid) is like setreuid except control over the saved
1007 #ifndef _SYS_SYSPROTO_H_
1008 struct setresuid_args {
1016 sys_setresuid(struct thread *td, struct setresuid_args *uap)
1018 struct proc *p = td->td_proc;
1019 struct ucred *newcred, *oldcred;
1020 uid_t euid, ruid, suid;
1021 struct uidinfo *euip, *ruip;
1027 AUDIT_ARG_EUID(euid);
1028 AUDIT_ARG_RUID(ruid);
1029 AUDIT_ARG_SUID(suid);
1031 euip = uifind(euid);
1032 ruip = uifind(ruid);
1034 oldcred = crcopysafe(p, newcred);
1037 error = mac_cred_check_setresuid(oldcred, ruid, euid, suid);
1042 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
1043 ruid != oldcred->cr_svuid &&
1044 ruid != oldcred->cr_uid) ||
1045 (euid != (uid_t)-1 && euid != oldcred->cr_ruid &&
1046 euid != oldcred->cr_svuid &&
1047 euid != oldcred->cr_uid) ||
1048 (suid != (uid_t)-1 && suid != oldcred->cr_ruid &&
1049 suid != oldcred->cr_svuid &&
1050 suid != oldcred->cr_uid)) &&
1051 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESUID, 0)) != 0)
1054 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
1055 change_euid(newcred, euip);
1058 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
1059 change_ruid(newcred, ruip);
1062 if (suid != (uid_t)-1 && oldcred->cr_svuid != suid) {
1063 change_svuid(newcred, suid);
1066 proc_set_cred(p, newcred);
1069 racct_proc_ucred_changed(p, oldcred, newcred);
1086 * setresgid(rgid, egid, sgid) is like setregid except control over the saved
1089 #ifndef _SYS_SYSPROTO_H_
1090 struct setresgid_args {
1098 sys_setresgid(struct thread *td, struct setresgid_args *uap)
1100 struct proc *p = td->td_proc;
1101 struct ucred *newcred, *oldcred;
1102 gid_t egid, rgid, sgid;
1108 AUDIT_ARG_EGID(egid);
1109 AUDIT_ARG_RGID(rgid);
1110 AUDIT_ARG_SGID(sgid);
1113 oldcred = crcopysafe(p, newcred);
1116 error = mac_cred_check_setresgid(oldcred, rgid, egid, sgid);
1121 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
1122 rgid != oldcred->cr_svgid &&
1123 rgid != oldcred->cr_groups[0]) ||
1124 (egid != (gid_t)-1 && egid != oldcred->cr_rgid &&
1125 egid != oldcred->cr_svgid &&
1126 egid != oldcred->cr_groups[0]) ||
1127 (sgid != (gid_t)-1 && sgid != oldcred->cr_rgid &&
1128 sgid != oldcred->cr_svgid &&
1129 sgid != oldcred->cr_groups[0])) &&
1130 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESGID, 0)) != 0)
1133 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
1134 change_egid(newcred, egid);
1137 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
1138 change_rgid(newcred, rgid);
1141 if (sgid != (gid_t)-1 && oldcred->cr_svgid != sgid) {
1142 change_svgid(newcred, sgid);
1145 proc_set_cred(p, newcred);
1156 #ifndef _SYS_SYSPROTO_H_
1157 struct getresuid_args {
1165 sys_getresuid(struct thread *td, struct getresuid_args *uap)
1168 int error1 = 0, error2 = 0, error3 = 0;
1170 cred = td->td_ucred;
1172 error1 = copyout(&cred->cr_ruid,
1173 uap->ruid, sizeof(cred->cr_ruid));
1175 error2 = copyout(&cred->cr_uid,
1176 uap->euid, sizeof(cred->cr_uid));
1178 error3 = copyout(&cred->cr_svuid,
1179 uap->suid, sizeof(cred->cr_svuid));
1180 return (error1 ? error1 : error2 ? error2 : error3);
1183 #ifndef _SYS_SYSPROTO_H_
1184 struct getresgid_args {
1192 sys_getresgid(struct thread *td, struct getresgid_args *uap)
1195 int error1 = 0, error2 = 0, error3 = 0;
1197 cred = td->td_ucred;
1199 error1 = copyout(&cred->cr_rgid,
1200 uap->rgid, sizeof(cred->cr_rgid));
1202 error2 = copyout(&cred->cr_groups[0],
1203 uap->egid, sizeof(cred->cr_groups[0]));
1205 error3 = copyout(&cred->cr_svgid,
1206 uap->sgid, sizeof(cred->cr_svgid));
1207 return (error1 ? error1 : error2 ? error2 : error3);
1210 #ifndef _SYS_SYSPROTO_H_
1211 struct issetugid_args {
1217 sys_issetugid(struct thread *td, struct issetugid_args *uap)
1219 struct proc *p = td->td_proc;
1222 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
1223 * we use P_SUGID because we consider changing the owners as
1224 * "tainting" as well.
1225 * This is significant for procs that start as root and "become"
1226 * a user without an exec - programs cannot know *everything*
1227 * that libc *might* have put in their data segment.
1229 td->td_retval[0] = (p->p_flag & P_SUGID) ? 1 : 0;
1234 sys___setugid(struct thread *td, struct __setugid_args *uap)
1240 switch (uap->flag) {
1243 p->p_flag &= ~P_SUGID;
1248 p->p_flag |= P_SUGID;
1254 #else /* !REGRESSION */
1257 #endif /* REGRESSION */
1261 * Check if gid is a member of the group set.
1264 groupmember(gid_t gid, struct ucred *cred)
1270 if (cred->cr_groups[0] == gid)
1274 * If gid was not our primary group, perform a binary search
1275 * of the supplemental groups. This is possible because we
1276 * sort the groups in crsetgroups().
1279 h = cred->cr_ngroups;
1281 m = l + ((h - l) / 2);
1282 if (cred->cr_groups[m] < gid)
1287 if ((l < cred->cr_ngroups) && (cred->cr_groups[l] == gid))
1294 * Test the active securelevel against a given level. securelevel_gt()
1295 * implements (securelevel > level). securelevel_ge() implements
1296 * (securelevel >= level). Note that the logic is inverted -- these
1297 * functions return EPERM on "success" and 0 on "failure".
1299 * Due to care taken when setting the securelevel, we know that no jail will
1300 * be less secure that its parent (or the physical system), so it is sufficient
1301 * to test the current jail only.
1303 * XXXRW: Possibly since this has to do with privilege, it should move to
1307 securelevel_gt(struct ucred *cr, int level)
1310 return (cr->cr_prison->pr_securelevel > level ? EPERM : 0);
1314 securelevel_ge(struct ucred *cr, int level)
1317 return (cr->cr_prison->pr_securelevel >= level ? EPERM : 0);
1321 * 'see_other_uids' determines whether or not visibility of processes
1322 * and sockets with credentials holding different real uids is possible
1323 * using a variety of system MIBs.
1324 * XXX: data declarations should be together near the beginning of the file.
1326 static int see_other_uids = 1;
1327 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_uids, CTLFLAG_RW,
1329 "Unprivileged processes may see subjects/objects with different real uid");
1332 * Determine if u1 "can see" the subject specified by u2, according to the
1333 * 'see_other_uids' policy.
1334 * Returns: 0 for permitted, ESRCH otherwise
1336 * References: *u1 and *u2 must not change during the call
1337 * u1 may equal u2, in which case only one reference is required
1340 cr_canseeotheruids(struct ucred *u1, struct ucred *u2)
1343 if (!see_other_uids && u1->cr_ruid != u2->cr_ruid) {
1344 if (priv_check_cred(u1, PRIV_SEEOTHERUIDS, 0) != 0)
1351 * 'see_other_gids' determines whether or not visibility of processes
1352 * and sockets with credentials holding different real gids is possible
1353 * using a variety of system MIBs.
1354 * XXX: data declarations should be together near the beginning of the file.
1356 static int see_other_gids = 1;
1357 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_gids, CTLFLAG_RW,
1359 "Unprivileged processes may see subjects/objects with different real gid");
1362 * Determine if u1 can "see" the subject specified by u2, according to the
1363 * 'see_other_gids' policy.
1364 * Returns: 0 for permitted, ESRCH otherwise
1366 * References: *u1 and *u2 must not change during the call
1367 * u1 may equal u2, in which case only one reference is required
1370 cr_canseeothergids(struct ucred *u1, struct ucred *u2)
1374 if (!see_other_gids) {
1376 for (i = 0; i < u1->cr_ngroups; i++) {
1377 if (groupmember(u1->cr_groups[i], u2))
1383 if (priv_check_cred(u1, PRIV_SEEOTHERGIDS, 0) != 0)
1391 * 'see_jail_proc' determines whether or not visibility of processes and
1392 * sockets with credentials holding different jail ids is possible using a
1393 * variety of system MIBs.
1395 * XXX: data declarations should be together near the beginning of the file.
1398 static int see_jail_proc = 1;
1399 SYSCTL_INT(_security_bsd, OID_AUTO, see_jail_proc, CTLFLAG_RW,
1401 "Unprivileged processes may see subjects/objects with different jail ids");
1404 * Determine if u1 "can see" the subject specified by u2, according to the
1405 * 'see_jail_proc' policy.
1406 * Returns: 0 for permitted, ESRCH otherwise
1408 * References: *u1 and *u2 must not change during the call
1409 * u1 may equal u2, in which case only one reference is required
1412 cr_canseejailproc(struct ucred *u1, struct ucred *u2)
1414 if (u1->cr_uid == 0)
1416 return (!see_jail_proc && u1->cr_prison != u2->cr_prison ? ESRCH : 0);
1420 * Determine if u1 "can see" the subject specified by u2.
1421 * Returns: 0 for permitted, an errno value otherwise
1423 * References: *u1 and *u2 must not change during the call
1424 * u1 may equal u2, in which case only one reference is required
1427 cr_cansee(struct ucred *u1, struct ucred *u2)
1431 if ((error = prison_check(u1, u2)))
1434 if ((error = mac_cred_check_visible(u1, u2)))
1437 if ((error = cr_canseeotheruids(u1, u2)))
1439 if ((error = cr_canseeothergids(u1, u2)))
1441 if ((error = cr_canseejailproc(u1, u2)))
1447 * Determine if td "can see" the subject specified by p.
1448 * Returns: 0 for permitted, an errno value otherwise
1449 * Locks: Sufficient locks to protect p->p_ucred must be held. td really
1450 * should be curthread.
1451 * References: td and p must be valid for the lifetime of the call
1454 p_cansee(struct thread *td, struct proc *p)
1457 /* Wrap cr_cansee() for all functionality. */
1458 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1459 PROC_LOCK_ASSERT(p, MA_OWNED);
1460 return (cr_cansee(td->td_ucred, p->p_ucred));
1464 * 'conservative_signals' prevents the delivery of a broad class of
1465 * signals by unprivileged processes to processes that have changed their
1466 * credentials since the last invocation of execve(). This can prevent
1467 * the leakage of cached information or retained privileges as a result
1468 * of a common class of signal-related vulnerabilities. However, this
1469 * may interfere with some applications that expect to be able to
1470 * deliver these signals to peer processes after having given up
1473 static int conservative_signals = 1;
1474 SYSCTL_INT(_security_bsd, OID_AUTO, conservative_signals, CTLFLAG_RW,
1475 &conservative_signals, 0, "Unprivileged processes prevented from "
1476 "sending certain signals to processes whose credentials have changed");
1478 * Determine whether cred may deliver the specified signal to proc.
1479 * Returns: 0 for permitted, an errno value otherwise.
1480 * Locks: A lock must be held for proc.
1481 * References: cred and proc must be valid for the lifetime of the call.
1484 cr_cansignal(struct ucred *cred, struct proc *proc, int signum)
1488 PROC_LOCK_ASSERT(proc, MA_OWNED);
1490 * Jail semantics limit the scope of signalling to proc in the
1491 * same jail as cred, if cred is in jail.
1493 error = prison_check(cred, proc->p_ucred);
1497 if ((error = mac_proc_check_signal(cred, proc, signum)))
1500 if ((error = cr_canseeotheruids(cred, proc->p_ucred)))
1502 if ((error = cr_canseeothergids(cred, proc->p_ucred)))
1506 * UNIX signal semantics depend on the status of the P_SUGID
1507 * bit on the target process. If the bit is set, then additional
1508 * restrictions are placed on the set of available signals.
1510 if (conservative_signals && (proc->p_flag & P_SUGID)) {
1525 * Generally, permit job and terminal control
1530 /* Not permitted without privilege. */
1531 error = priv_check_cred(cred, PRIV_SIGNAL_SUGID, 0);
1538 * Generally, the target credential's ruid or svuid must match the
1539 * subject credential's ruid or euid.
1541 if (cred->cr_ruid != proc->p_ucred->cr_ruid &&
1542 cred->cr_ruid != proc->p_ucred->cr_svuid &&
1543 cred->cr_uid != proc->p_ucred->cr_ruid &&
1544 cred->cr_uid != proc->p_ucred->cr_svuid) {
1545 error = priv_check_cred(cred, PRIV_SIGNAL_DIFFCRED, 0);
1554 * Determine whether td may deliver the specified signal to p.
1555 * Returns: 0 for permitted, an errno value otherwise
1556 * Locks: Sufficient locks to protect various components of td and p
1557 * must be held. td must be curthread, and a lock must be
1559 * References: td and p must be valid for the lifetime of the call
1562 p_cansignal(struct thread *td, struct proc *p, int signum)
1565 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1566 PROC_LOCK_ASSERT(p, MA_OWNED);
1567 if (td->td_proc == p)
1571 * UNIX signalling semantics require that processes in the same
1572 * session always be able to deliver SIGCONT to one another,
1573 * overriding the remaining protections.
1575 /* XXX: This will require an additional lock of some sort. */
1576 if (signum == SIGCONT && td->td_proc->p_session == p->p_session)
1579 * Some compat layers use SIGTHR and higher signals for
1580 * communication between different kernel threads of the same
1581 * process, so that they expect that it's always possible to
1582 * deliver them, even for suid applications where cr_cansignal() can
1583 * deny such ability for security consideration. It should be
1584 * pretty safe to do since the only way to create two processes
1585 * with the same p_leader is via rfork(2).
1587 if (td->td_proc->p_leader != NULL && signum >= SIGTHR &&
1588 signum < SIGTHR + 4 && td->td_proc->p_leader == p->p_leader)
1591 return (cr_cansignal(td->td_ucred, p, signum));
1595 * Determine whether td may reschedule p.
1596 * Returns: 0 for permitted, an errno value otherwise
1597 * Locks: Sufficient locks to protect various components of td and p
1598 * must be held. td must be curthread, and a lock must
1600 * References: td and p must be valid for the lifetime of the call
1603 p_cansched(struct thread *td, struct proc *p)
1607 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1608 PROC_LOCK_ASSERT(p, MA_OWNED);
1609 if (td->td_proc == p)
1611 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1614 if ((error = mac_proc_check_sched(td->td_ucred, p)))
1617 if ((error = cr_canseeotheruids(td->td_ucred, p->p_ucred)))
1619 if ((error = cr_canseeothergids(td->td_ucred, p->p_ucred)))
1621 if (td->td_ucred->cr_ruid != p->p_ucred->cr_ruid &&
1622 td->td_ucred->cr_uid != p->p_ucred->cr_ruid) {
1623 error = priv_check(td, PRIV_SCHED_DIFFCRED);
1631 * The 'unprivileged_proc_debug' flag may be used to disable a variety of
1632 * unprivileged inter-process debugging services, including some procfs
1633 * functionality, ptrace(), and ktrace(). In the past, inter-process
1634 * debugging has been involved in a variety of security problems, and sites
1635 * not requiring the service might choose to disable it when hardening
1638 * XXX: Should modifying and reading this variable require locking?
1639 * XXX: data declarations should be together near the beginning of the file.
1641 static int unprivileged_proc_debug = 1;
1642 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_proc_debug, CTLFLAG_RW,
1643 &unprivileged_proc_debug, 0,
1644 "Unprivileged processes may use process debugging facilities");
1647 * Determine whether td may debug p.
1648 * Returns: 0 for permitted, an errno value otherwise
1649 * Locks: Sufficient locks to protect various components of td and p
1650 * must be held. td must be curthread, and a lock must
1652 * References: td and p must be valid for the lifetime of the call
1655 p_candebug(struct thread *td, struct proc *p)
1657 int credentialchanged, error, grpsubset, i, uidsubset;
1659 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1660 PROC_LOCK_ASSERT(p, MA_OWNED);
1661 if (!unprivileged_proc_debug) {
1662 error = priv_check(td, PRIV_DEBUG_UNPRIV);
1666 if (td->td_proc == p)
1668 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1671 if ((error = mac_proc_check_debug(td->td_ucred, p)))
1674 if ((error = cr_canseeotheruids(td->td_ucred, p->p_ucred)))
1676 if ((error = cr_canseeothergids(td->td_ucred, p->p_ucred)))
1680 * Is p's group set a subset of td's effective group set? This
1681 * includes p's egid, group access list, rgid, and svgid.
1684 for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
1685 if (!groupmember(p->p_ucred->cr_groups[i], td->td_ucred)) {
1690 grpsubset = grpsubset &&
1691 groupmember(p->p_ucred->cr_rgid, td->td_ucred) &&
1692 groupmember(p->p_ucred->cr_svgid, td->td_ucred);
1695 * Are the uids present in p's credential equal to td's
1696 * effective uid? This includes p's euid, svuid, and ruid.
1698 uidsubset = (td->td_ucred->cr_uid == p->p_ucred->cr_uid &&
1699 td->td_ucred->cr_uid == p->p_ucred->cr_svuid &&
1700 td->td_ucred->cr_uid == p->p_ucred->cr_ruid);
1703 * Has the credential of the process changed since the last exec()?
1705 credentialchanged = (p->p_flag & P_SUGID);
1708 * If p's gids aren't a subset, or the uids aren't a subset,
1709 * or the credential has changed, require appropriate privilege
1710 * for td to debug p.
1712 if (!grpsubset || !uidsubset) {
1713 error = priv_check(td, PRIV_DEBUG_DIFFCRED);
1718 if (credentialchanged) {
1719 error = priv_check(td, PRIV_DEBUG_SUGID);
1724 /* Can't trace init when securelevel > 0. */
1725 if (p == initproc) {
1726 error = securelevel_gt(td->td_ucred, 0);
1732 * Can't trace a process that's currently exec'ing.
1734 * XXX: Note, this is not a security policy decision, it's a
1735 * basic correctness/functionality decision. Therefore, this check
1736 * should be moved to the caller's of p_candebug().
1738 if ((p->p_flag & P_INEXEC) != 0)
1741 /* Denied explicitely */
1742 if ((p->p_flag2 & P2_NOTRACE) != 0) {
1743 error = priv_check(td, PRIV_DEBUG_DENIED);
1752 * Determine whether the subject represented by cred can "see" a socket.
1753 * Returns: 0 for permitted, ENOENT otherwise.
1756 cr_canseesocket(struct ucred *cred, struct socket *so)
1760 error = prison_check(cred, so->so_cred);
1764 error = mac_socket_check_visible(cred, so);
1768 if (cr_canseeotheruids(cred, so->so_cred))
1770 if (cr_canseeothergids(cred, so->so_cred))
1777 * Determine whether td can wait for the exit of p.
1778 * Returns: 0 for permitted, an errno value otherwise
1779 * Locks: Sufficient locks to protect various components of td and p
1780 * must be held. td must be curthread, and a lock must
1782 * References: td and p must be valid for the lifetime of the call
1786 p_canwait(struct thread *td, struct proc *p)
1790 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1791 PROC_LOCK_ASSERT(p, MA_OWNED);
1792 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1795 if ((error = mac_proc_check_wait(td->td_ucred, p)))
1799 /* XXXMAC: This could have odd effects on some shells. */
1800 if ((error = cr_canseeotheruids(td->td_ucred, p->p_ucred)))
1808 * Allocate a zeroed cred structure.
1815 cr = malloc(sizeof(*cr), M_CRED, M_WAITOK | M_ZERO);
1816 refcount_init(&cr->cr_ref, 1);
1818 audit_cred_init(cr);
1823 cr->cr_groups = cr->cr_smallgroups;
1825 sizeof(cr->cr_smallgroups) / sizeof(cr->cr_smallgroups[0]);
1830 * Claim another reference to a ucred structure.
1833 crhold(struct ucred *cr)
1836 refcount_acquire(&cr->cr_ref);
1841 * Free a cred structure. Throws away space when ref count gets to 0.
1844 crfree(struct ucred *cr)
1847 KASSERT(cr->cr_ref > 0, ("bad ucred refcount: %d", cr->cr_ref));
1848 KASSERT(cr->cr_ref != 0xdeadc0de, ("dangling reference to ucred"));
1849 if (refcount_release(&cr->cr_ref)) {
1851 * Some callers of crget(), such as nfs_statfs(),
1852 * allocate a temporary credential, but don't
1853 * allocate a uidinfo structure.
1855 if (cr->cr_uidinfo != NULL)
1856 uifree(cr->cr_uidinfo);
1857 if (cr->cr_ruidinfo != NULL)
1858 uifree(cr->cr_ruidinfo);
1860 * Free a prison, if any.
1862 if (cr->cr_prison != NULL)
1863 prison_free(cr->cr_prison);
1864 if (cr->cr_loginclass != NULL)
1865 loginclass_free(cr->cr_loginclass);
1867 audit_cred_destroy(cr);
1870 mac_cred_destroy(cr);
1872 if (cr->cr_groups != cr->cr_smallgroups)
1873 free(cr->cr_groups, M_CRED);
1879 * Copy a ucred's contents from a template. Does not block.
1882 crcopy(struct ucred *dest, struct ucred *src)
1885 KASSERT(dest->cr_ref == 1, ("crcopy of shared ucred"));
1886 bcopy(&src->cr_startcopy, &dest->cr_startcopy,
1887 (unsigned)((caddr_t)&src->cr_endcopy -
1888 (caddr_t)&src->cr_startcopy));
1889 crsetgroups(dest, src->cr_ngroups, src->cr_groups);
1890 uihold(dest->cr_uidinfo);
1891 uihold(dest->cr_ruidinfo);
1892 prison_hold(dest->cr_prison);
1893 loginclass_hold(dest->cr_loginclass);
1895 audit_cred_copy(src, dest);
1898 mac_cred_copy(src, dest);
1903 * Dup cred struct to a new held one.
1906 crdup(struct ucred *cr)
1908 struct ucred *newcr;
1916 * Fill in a struct xucred based on a struct ucred.
1919 cru2x(struct ucred *cr, struct xucred *xcr)
1923 bzero(xcr, sizeof(*xcr));
1924 xcr->cr_version = XUCRED_VERSION;
1925 xcr->cr_uid = cr->cr_uid;
1927 ngroups = MIN(cr->cr_ngroups, XU_NGROUPS);
1928 xcr->cr_ngroups = ngroups;
1929 bcopy(cr->cr_groups, xcr->cr_groups,
1930 ngroups * sizeof(*cr->cr_groups));
1934 * Set initial process credentials.
1935 * Callers are responsible for providing the reference for provided credentials.
1938 proc_set_cred_init(struct proc *p, struct ucred *newcred)
1941 p->p_ucred = newcred;
1945 * Change process credentials.
1946 * Callers are responsible for providing the reference for passed credentials
1947 * and for freeing old ones.
1949 * Process has to be locked except when it does not have credentials (as it
1950 * should not be visible just yet) or when newcred is NULL (as this can be
1951 * only used when the process is about to be freed, at which point it should
1952 * not be visible anymore).
1955 proc_set_cred(struct proc *p, struct ucred *newcred)
1957 struct ucred *oldcred;
1959 MPASS(p->p_ucred != NULL);
1960 if (newcred == NULL)
1961 MPASS(p->p_state == PRS_ZOMBIE);
1963 PROC_LOCK_ASSERT(p, MA_OWNED);
1965 oldcred = p->p_ucred;
1966 p->p_ucred = newcred;
1967 if (newcred != NULL)
1973 crcopysafe(struct proc *p, struct ucred *cr)
1975 struct ucred *oldcred;
1978 PROC_LOCK_ASSERT(p, MA_OWNED);
1980 oldcred = p->p_ucred;
1981 while (cr->cr_agroups < oldcred->cr_agroups) {
1982 groups = oldcred->cr_agroups;
1984 crextend(cr, groups);
1986 oldcred = p->p_ucred;
1988 crcopy(cr, oldcred);
1994 * Extend the passed in credential to hold n items.
1997 crextend(struct ucred *cr, int n)
2002 if (n <= cr->cr_agroups)
2006 * We extend by 2 each time since we're using a power of two
2007 * allocator until we need enough groups to fill a page.
2008 * Once we're allocating multiple pages, only allocate as many
2009 * as we actually need. The case of processes needing a
2010 * non-power of two number of pages seems more likely than
2011 * a real world process that adds thousands of groups one at a
2014 if ( n < PAGE_SIZE / sizeof(gid_t) ) {
2015 if (cr->cr_agroups == 0)
2016 cnt = MINALLOCSIZE / sizeof(gid_t);
2018 cnt = cr->cr_agroups * 2;
2023 cnt = roundup2(n, PAGE_SIZE / sizeof(gid_t));
2025 /* Free the old array. */
2026 if (cr->cr_groups != cr->cr_smallgroups)
2027 free(cr->cr_groups, M_CRED);
2029 cr->cr_groups = malloc(cnt * sizeof(gid_t), M_CRED, M_WAITOK | M_ZERO);
2030 cr->cr_agroups = cnt;
2034 * Copy groups in to a credential, preserving any necessary invariants.
2035 * Currently this includes the sorting of all supplemental gids.
2036 * crextend() must have been called before hand to ensure sufficient
2037 * space is available.
2040 crsetgroups_locked(struct ucred *cr, int ngrp, gid_t *groups)
2046 KASSERT(cr->cr_agroups >= ngrp, ("cr_ngroups is too small"));
2048 bcopy(groups, cr->cr_groups, ngrp * sizeof(gid_t));
2049 cr->cr_ngroups = ngrp;
2052 * Sort all groups except cr_groups[0] to allow groupmember to
2053 * perform a binary search.
2055 * XXX: If large numbers of groups become common this should
2056 * be replaced with shell sort like linux uses or possibly
2059 for (i = 2; i < ngrp; i++) {
2060 g = cr->cr_groups[i];
2061 for (j = i-1; j >= 1 && g < cr->cr_groups[j]; j--)
2062 cr->cr_groups[j + 1] = cr->cr_groups[j];
2063 cr->cr_groups[j + 1] = g;
2068 * Copy groups in to a credential after expanding it if required.
2069 * Truncate the list to (ngroups_max + 1) if it is too large.
2072 crsetgroups(struct ucred *cr, int ngrp, gid_t *groups)
2075 if (ngrp > ngroups_max + 1)
2076 ngrp = ngroups_max + 1;
2079 crsetgroups_locked(cr, ngrp, groups);
2083 * Get login name, if available.
2085 #ifndef _SYS_SYSPROTO_H_
2086 struct getlogin_args {
2093 sys_getlogin(struct thread *td, struct getlogin_args *uap)
2095 char login[MAXLOGNAME];
2096 struct proc *p = td->td_proc;
2099 if (uap->namelen > MAXLOGNAME)
2100 uap->namelen = MAXLOGNAME;
2102 SESS_LOCK(p->p_session);
2103 len = strlcpy(login, p->p_session->s_login, uap->namelen) + 1;
2104 SESS_UNLOCK(p->p_session);
2106 if (len > uap->namelen)
2108 return (copyout(login, uap->namebuf, len));
2114 #ifndef _SYS_SYSPROTO_H_
2115 struct setlogin_args {
2121 sys_setlogin(struct thread *td, struct setlogin_args *uap)
2123 struct proc *p = td->td_proc;
2125 char logintmp[MAXLOGNAME];
2127 CTASSERT(sizeof(p->p_session->s_login) >= sizeof(logintmp));
2129 error = priv_check(td, PRIV_PROC_SETLOGIN);
2132 error = copyinstr(uap->namebuf, logintmp, sizeof(logintmp), NULL);
2134 if (error == ENAMETOOLONG)
2138 AUDIT_ARG_LOGIN(logintmp);
2140 SESS_LOCK(p->p_session);
2141 strcpy(p->p_session->s_login, logintmp);
2142 SESS_UNLOCK(p->p_session);
2148 setsugid(struct proc *p)
2151 PROC_LOCK_ASSERT(p, MA_OWNED);
2152 p->p_flag |= P_SUGID;
2153 if (!(p->p_pfsflags & PF_ISUGID))
2158 * Change a process's effective uid.
2159 * Side effects: newcred->cr_uid and newcred->cr_uidinfo will be modified.
2160 * References: newcred must be an exclusive credential reference for the
2161 * duration of the call.
2164 change_euid(struct ucred *newcred, struct uidinfo *euip)
2167 newcred->cr_uid = euip->ui_uid;
2169 uifree(newcred->cr_uidinfo);
2170 newcred->cr_uidinfo = euip;
2174 * Change a process's effective gid.
2175 * Side effects: newcred->cr_gid will be modified.
2176 * References: newcred must be an exclusive credential reference for the
2177 * duration of the call.
2180 change_egid(struct ucred *newcred, gid_t egid)
2183 newcred->cr_groups[0] = egid;
2187 * Change a process's real uid.
2188 * Side effects: newcred->cr_ruid will be updated, newcred->cr_ruidinfo
2189 * will be updated, and the old and new cr_ruidinfo proc
2190 * counts will be updated.
2191 * References: newcred must be an exclusive credential reference for the
2192 * duration of the call.
2195 change_ruid(struct ucred *newcred, struct uidinfo *ruip)
2198 (void)chgproccnt(newcred->cr_ruidinfo, -1, 0);
2199 newcred->cr_ruid = ruip->ui_uid;
2201 uifree(newcred->cr_ruidinfo);
2202 newcred->cr_ruidinfo = ruip;
2203 (void)chgproccnt(newcred->cr_ruidinfo, 1, 0);
2207 * Change a process's real gid.
2208 * Side effects: newcred->cr_rgid will be updated.
2209 * References: newcred must be an exclusive credential reference for the
2210 * duration of the call.
2213 change_rgid(struct ucred *newcred, gid_t rgid)
2216 newcred->cr_rgid = rgid;
2220 * Change a process's saved uid.
2221 * Side effects: newcred->cr_svuid will be updated.
2222 * References: newcred must be an exclusive credential reference for the
2223 * duration of the call.
2226 change_svuid(struct ucred *newcred, uid_t svuid)
2229 newcred->cr_svuid = svuid;
2233 * Change a process's saved gid.
2234 * Side effects: newcred->cr_svgid will be updated.
2235 * References: newcred must be an exclusive credential reference for the
2236 * duration of the call.
2239 change_svgid(struct ucred *newcred, gid_t svgid)
2242 newcred->cr_svgid = svgid;