2 * Copyright (c) 1982, 1986, 1989, 1990, 1991, 1993
3 * The Regents of the University of California.
4 * (c) UNIX System Laboratories, Inc.
5 * Copyright (c) 2000-2001 Robert N. M. Watson.
8 * All or some portions of this file are derived from material licensed
9 * to the University of California by American Telephone and Telegraph
10 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11 * the permission of UNIX System Laboratories, Inc.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * @(#)kern_prot.c 8.6 (Berkeley) 1/21/94
41 * System calls related to processes and protection
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include "opt_compat.h"
49 #include "opt_inet6.h"
51 #include <sys/param.h>
52 #include <sys/systm.h>
55 #include <sys/kernel.h>
57 #include <sys/loginclass.h>
58 #include <sys/malloc.h>
59 #include <sys/mutex.h>
60 #include <sys/refcount.h>
64 #include <sys/sysproto.h>
66 #include <sys/pioctl.h>
67 #include <sys/racct.h>
68 #include <sys/resourcevar.h>
69 #include <sys/socket.h>
70 #include <sys/socketvar.h>
71 #include <sys/syscallsubr.h>
72 #include <sys/sysctl.h>
76 "Kernel support for interfaces necessary for regression testing (SECURITY RISK!)");
79 #if defined(INET) || defined(INET6)
80 #include <netinet/in.h>
81 #include <netinet/in_pcb.h>
84 #include <security/audit/audit.h>
85 #include <security/mac/mac_framework.h>
87 static MALLOC_DEFINE(M_CRED, "cred", "credentials");
89 SYSCTL_NODE(_security, OID_AUTO, bsd, CTLFLAG_RW, 0, "BSD security policy");
91 static void crsetgroups_locked(struct ucred *cr, int ngrp,
94 #ifndef _SYS_SYSPROTO_H_
101 sys_getpid(struct thread *td, struct getpid_args *uap)
103 struct proc *p = td->td_proc;
105 td->td_retval[0] = p->p_pid;
106 #if defined(COMPAT_43)
107 td->td_retval[1] = kern_getppid(td);
112 #ifndef _SYS_SYSPROTO_H_
113 struct getppid_args {
119 sys_getppid(struct thread *td, struct getppid_args *uap)
122 td->td_retval[0] = kern_getppid(td);
127 kern_getppid(struct thread *td)
129 struct proc *p = td->td_proc;
134 if (!(p->p_flag & P_TRACED)) {
135 ppid = p->p_pptr->p_pid;
139 sx_slock(&proctree_lock);
140 pp = proc_realparent(p);
142 sx_sunlock(&proctree_lock);
149 * Get process group ID; note that POSIX getpgrp takes no parameter.
151 #ifndef _SYS_SYSPROTO_H_
152 struct getpgrp_args {
157 sys_getpgrp(struct thread *td, struct getpgrp_args *uap)
159 struct proc *p = td->td_proc;
162 td->td_retval[0] = p->p_pgrp->pg_id;
167 /* Get an arbitary pid's process group id */
168 #ifndef _SYS_SYSPROTO_H_
169 struct getpgid_args {
174 sys_getpgid(struct thread *td, struct getpgid_args *uap)
186 error = p_cansee(td, p);
192 td->td_retval[0] = p->p_pgrp->pg_id;
198 * Get an arbitary pid's session id.
200 #ifndef _SYS_SYSPROTO_H_
206 sys_getsid(struct thread *td, struct getsid_args *uap)
218 error = p_cansee(td, p);
224 td->td_retval[0] = p->p_session->s_sid;
229 #ifndef _SYS_SYSPROTO_H_
236 sys_getuid(struct thread *td, struct getuid_args *uap)
239 td->td_retval[0] = td->td_ucred->cr_ruid;
240 #if defined(COMPAT_43)
241 td->td_retval[1] = td->td_ucred->cr_uid;
246 #ifndef _SYS_SYSPROTO_H_
247 struct geteuid_args {
253 sys_geteuid(struct thread *td, struct geteuid_args *uap)
256 td->td_retval[0] = td->td_ucred->cr_uid;
260 #ifndef _SYS_SYSPROTO_H_
267 sys_getgid(struct thread *td, struct getgid_args *uap)
270 td->td_retval[0] = td->td_ucred->cr_rgid;
271 #if defined(COMPAT_43)
272 td->td_retval[1] = td->td_ucred->cr_groups[0];
278 * Get effective group ID. The "egid" is groups[0], and could be obtained
279 * via getgroups. This syscall exists because it is somewhat painful to do
280 * correctly in a library function.
282 #ifndef _SYS_SYSPROTO_H_
283 struct getegid_args {
289 sys_getegid(struct thread *td, struct getegid_args *uap)
292 td->td_retval[0] = td->td_ucred->cr_groups[0];
296 #ifndef _SYS_SYSPROTO_H_
297 struct getgroups_args {
303 sys_getgroups(struct thread *td, register struct getgroups_args *uap)
310 ngrp = cred->cr_ngroups;
312 if (uap->gidsetsize == 0) {
316 if (uap->gidsetsize < ngrp)
319 error = copyout(cred->cr_groups, uap->gidset, ngrp * sizeof(gid_t));
321 td->td_retval[0] = ngrp;
325 #ifndef _SYS_SYSPROTO_H_
332 sys_setsid(register struct thread *td, struct setsid_args *uap)
336 struct proc *p = td->td_proc;
337 struct pgrp *newpgrp;
338 struct session *newsess;
343 newpgrp = malloc(sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
344 newsess = malloc(sizeof(struct session), M_SESSION, M_WAITOK | M_ZERO);
346 sx_xlock(&proctree_lock);
348 if (p->p_pgid == p->p_pid || (pgrp = pgfind(p->p_pid)) != NULL) {
353 (void)enterpgrp(p, p->p_pid, newpgrp, newsess);
354 td->td_retval[0] = p->p_pid;
359 sx_xunlock(&proctree_lock);
362 free(newpgrp, M_PGRP);
364 free(newsess, M_SESSION);
370 * set process group (setpgid/old setpgrp)
372 * caller does setpgid(targpid, targpgid)
374 * pid must be caller or child of caller (ESRCH)
376 * pid must be in same session (EPERM)
377 * pid can't have done an exec (EACCES)
379 * there must exist some pid in same session having pgid (EPERM)
380 * pid must not be session leader (EPERM)
382 #ifndef _SYS_SYSPROTO_H_
383 struct setpgid_args {
384 int pid; /* target process id */
385 int pgid; /* target pgrp id */
390 sys_setpgid(struct thread *td, register struct setpgid_args *uap)
392 struct proc *curp = td->td_proc;
393 register struct proc *targp; /* target process */
394 register struct pgrp *pgrp; /* target pgrp */
396 struct pgrp *newpgrp;
403 newpgrp = malloc(sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
405 sx_xlock(&proctree_lock);
406 if (uap->pid != 0 && uap->pid != curp->p_pid) {
407 if ((targp = pfind(uap->pid)) == NULL) {
411 if (!inferior(targp)) {
416 if ((error = p_cansee(td, targp))) {
420 if (targp->p_pgrp == NULL ||
421 targp->p_session != curp->p_session) {
426 if (targp->p_flag & P_EXEC) {
434 if (SESS_LEADER(targp)) {
439 uap->pgid = targp->p_pid;
440 if ((pgrp = pgfind(uap->pgid)) == NULL) {
441 if (uap->pgid == targp->p_pid) {
442 error = enterpgrp(targp, uap->pgid, newpgrp,
449 if (pgrp == targp->p_pgrp) {
453 if (pgrp->pg_id != targp->p_pid &&
454 pgrp->pg_session != curp->p_session) {
460 error = enterthispgrp(targp, pgrp);
463 sx_xunlock(&proctree_lock);
464 KASSERT((error == 0) || (newpgrp != NULL),
465 ("setpgid failed and newpgrp is NULL"));
467 free(newpgrp, M_PGRP);
472 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
473 * compatible. It says that setting the uid/gid to euid/egid is a special
474 * case of "appropriate privilege". Once the rules are expanded out, this
475 * basically means that setuid(nnn) sets all three id's, in all permitted
476 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
477 * does not set the saved id - this is dangerous for traditional BSD
478 * programs. For this reason, we *really* do not want to set
479 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
481 #define POSIX_APPENDIX_B_4_2_2
483 #ifndef _SYS_SYSPROTO_H_
490 sys_setuid(struct thread *td, struct setuid_args *uap)
492 struct proc *p = td->td_proc;
493 struct ucred *newcred, *oldcred;
504 * Copy credentials so other references do not see our changes.
506 oldcred = crcopysafe(p, newcred);
509 error = mac_cred_check_setuid(oldcred, uid);
515 * See if we have "permission" by POSIX 1003.1 rules.
517 * Note that setuid(geteuid()) is a special case of
518 * "appropriate privileges" in appendix B.4.2.2. We need
519 * to use this clause to be compatible with traditional BSD
520 * semantics. Basically, it means that "setuid(xx)" sets all
521 * three id's (assuming you have privs).
523 * Notes on the logic. We do things in three steps.
524 * 1: We determine if the euid is going to change, and do EPERM
525 * right away. We unconditionally change the euid later if this
526 * test is satisfied, simplifying that part of the logic.
527 * 2: We determine if the real and/or saved uids are going to
528 * change. Determined by compile options.
529 * 3: Change euid last. (after tests in #2 for "appropriate privs")
531 if (uid != oldcred->cr_ruid && /* allow setuid(getuid()) */
532 #ifdef _POSIX_SAVED_IDS
533 uid != oldcred->cr_svuid && /* allow setuid(saved gid) */
535 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
536 uid != oldcred->cr_uid && /* allow setuid(geteuid()) */
538 (error = priv_check_cred(oldcred, PRIV_CRED_SETUID, 0)) != 0)
541 #ifdef _POSIX_SAVED_IDS
543 * Do we have "appropriate privileges" (are we root or uid == euid)
544 * If so, we are changing the real uid and/or saved uid.
547 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
548 uid == oldcred->cr_uid ||
550 /* We are using privs. */
551 priv_check_cred(oldcred, PRIV_CRED_SETUID, 0) == 0)
555 * Set the real uid and transfer proc count to new user.
557 if (uid != oldcred->cr_ruid) {
558 change_ruid(newcred, uip);
564 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
565 * the security of seteuid() depends on it. B.4.2.2 says it
566 * is important that we should do this.
568 if (uid != oldcred->cr_svuid) {
569 change_svuid(newcred, uid);
575 * In all permitted cases, we are changing the euid.
577 if (uid != oldcred->cr_uid) {
578 change_euid(newcred, uip);
581 proc_set_cred(p, newcred);
584 racct_proc_ucred_changed(p, oldcred, newcred);
597 #ifndef _SYS_SYSPROTO_H_
598 struct seteuid_args {
604 sys_seteuid(struct thread *td, struct seteuid_args *uap)
606 struct proc *p = td->td_proc;
607 struct ucred *newcred, *oldcred;
609 struct uidinfo *euip;
613 AUDIT_ARG_EUID(euid);
618 * Copy credentials so other references do not see our changes.
620 oldcred = crcopysafe(p, newcred);
623 error = mac_cred_check_seteuid(oldcred, euid);
628 if (euid != oldcred->cr_ruid && /* allow seteuid(getuid()) */
629 euid != oldcred->cr_svuid && /* allow seteuid(saved uid) */
630 (error = priv_check_cred(oldcred, PRIV_CRED_SETEUID, 0)) != 0)
634 * Everything's okay, do it.
636 if (oldcred->cr_uid != euid) {
637 change_euid(newcred, euip);
640 proc_set_cred(p, newcred);
653 #ifndef _SYS_SYSPROTO_H_
660 sys_setgid(struct thread *td, struct setgid_args *uap)
662 struct proc *p = td->td_proc;
663 struct ucred *newcred, *oldcred;
671 oldcred = crcopysafe(p, newcred);
674 error = mac_cred_check_setgid(oldcred, gid);
680 * See if we have "permission" by POSIX 1003.1 rules.
682 * Note that setgid(getegid()) is a special case of
683 * "appropriate privileges" in appendix B.4.2.2. We need
684 * to use this clause to be compatible with traditional BSD
685 * semantics. Basically, it means that "setgid(xx)" sets all
686 * three id's (assuming you have privs).
688 * For notes on the logic here, see setuid() above.
690 if (gid != oldcred->cr_rgid && /* allow setgid(getgid()) */
691 #ifdef _POSIX_SAVED_IDS
692 gid != oldcred->cr_svgid && /* allow setgid(saved gid) */
694 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
695 gid != oldcred->cr_groups[0] && /* allow setgid(getegid()) */
697 (error = priv_check_cred(oldcred, PRIV_CRED_SETGID, 0)) != 0)
700 #ifdef _POSIX_SAVED_IDS
702 * Do we have "appropriate privileges" (are we root or gid == egid)
703 * If so, we are changing the real uid and saved gid.
706 #ifdef POSIX_APPENDIX_B_4_2_2 /* use the clause from B.4.2.2 */
707 gid == oldcred->cr_groups[0] ||
709 /* We are using privs. */
710 priv_check_cred(oldcred, PRIV_CRED_SETGID, 0) == 0)
716 if (oldcred->cr_rgid != gid) {
717 change_rgid(newcred, gid);
723 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
724 * the security of setegid() depends on it. B.4.2.2 says it
725 * is important that we should do this.
727 if (oldcred->cr_svgid != gid) {
728 change_svgid(newcred, gid);
733 * In all cases permitted cases, we are changing the egid.
734 * Copy credentials so other references do not see our changes.
736 if (oldcred->cr_groups[0] != gid) {
737 change_egid(newcred, gid);
740 proc_set_cred(p, newcred);
751 #ifndef _SYS_SYSPROTO_H_
752 struct setegid_args {
758 sys_setegid(struct thread *td, struct setegid_args *uap)
760 struct proc *p = td->td_proc;
761 struct ucred *newcred, *oldcred;
766 AUDIT_ARG_EGID(egid);
769 oldcred = crcopysafe(p, newcred);
772 error = mac_cred_check_setegid(oldcred, egid);
777 if (egid != oldcred->cr_rgid && /* allow setegid(getgid()) */
778 egid != oldcred->cr_svgid && /* allow setegid(saved gid) */
779 (error = priv_check_cred(oldcred, PRIV_CRED_SETEGID, 0)) != 0)
782 if (oldcred->cr_groups[0] != egid) {
783 change_egid(newcred, egid);
786 proc_set_cred(p, newcred);
797 #ifndef _SYS_SYSPROTO_H_
798 struct setgroups_args {
805 sys_setgroups(struct thread *td, struct setgroups_args *uap)
807 gid_t smallgroups[XU_NGROUPS];
812 gidsetsize = uap->gidsetsize;
813 if (gidsetsize > ngroups_max + 1)
816 if (gidsetsize > XU_NGROUPS)
817 groups = malloc(gidsetsize * sizeof(gid_t), M_TEMP, M_WAITOK);
819 groups = smallgroups;
821 error = copyin(uap->gidset, groups, gidsetsize * sizeof(gid_t));
823 error = kern_setgroups(td, gidsetsize, groups);
825 if (gidsetsize > XU_NGROUPS)
826 free(groups, M_TEMP);
831 kern_setgroups(struct thread *td, u_int ngrp, gid_t *groups)
833 struct proc *p = td->td_proc;
834 struct ucred *newcred, *oldcred;
837 MPASS(ngrp <= ngroups_max + 1);
838 AUDIT_ARG_GROUPSET(groups, ngrp);
840 crextend(newcred, ngrp);
842 oldcred = crcopysafe(p, newcred);
845 error = mac_cred_check_setgroups(oldcred, ngrp, groups);
850 error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0);
856 * setgroups(0, NULL) is a legitimate way of clearing the
857 * groups vector on non-BSD systems (which generally do not
858 * have the egid in the groups[0]). We risk security holes
859 * when running non-BSD software if we do not do the same.
861 newcred->cr_ngroups = 1;
863 crsetgroups_locked(newcred, ngrp, groups);
866 proc_set_cred(p, newcred);
877 #ifndef _SYS_SYSPROTO_H_
878 struct setreuid_args {
885 sys_setreuid(register struct thread *td, struct setreuid_args *uap)
887 struct proc *p = td->td_proc;
888 struct ucred *newcred, *oldcred;
890 struct uidinfo *euip, *ruip;
895 AUDIT_ARG_EUID(euid);
896 AUDIT_ARG_RUID(ruid);
901 oldcred = crcopysafe(p, newcred);
904 error = mac_cred_check_setreuid(oldcred, ruid, euid);
909 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
910 ruid != oldcred->cr_svuid) ||
911 (euid != (uid_t)-1 && euid != oldcred->cr_uid &&
912 euid != oldcred->cr_ruid && euid != oldcred->cr_svuid)) &&
913 (error = priv_check_cred(oldcred, PRIV_CRED_SETREUID, 0)) != 0)
916 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
917 change_euid(newcred, euip);
920 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
921 change_ruid(newcred, ruip);
924 if ((ruid != (uid_t)-1 || newcred->cr_uid != newcred->cr_ruid) &&
925 newcred->cr_svuid != newcred->cr_uid) {
926 change_svuid(newcred, newcred->cr_uid);
929 proc_set_cred(p, newcred);
932 racct_proc_ucred_changed(p, oldcred, newcred);
947 #ifndef _SYS_SYSPROTO_H_
948 struct setregid_args {
955 sys_setregid(register struct thread *td, struct setregid_args *uap)
957 struct proc *p = td->td_proc;
958 struct ucred *newcred, *oldcred;
964 AUDIT_ARG_EGID(egid);
965 AUDIT_ARG_RGID(rgid);
968 oldcred = crcopysafe(p, newcred);
971 error = mac_cred_check_setregid(oldcred, rgid, egid);
976 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
977 rgid != oldcred->cr_svgid) ||
978 (egid != (gid_t)-1 && egid != oldcred->cr_groups[0] &&
979 egid != oldcred->cr_rgid && egid != oldcred->cr_svgid)) &&
980 (error = priv_check_cred(oldcred, PRIV_CRED_SETREGID, 0)) != 0)
983 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
984 change_egid(newcred, egid);
987 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
988 change_rgid(newcred, rgid);
991 if ((rgid != (gid_t)-1 || newcred->cr_groups[0] != newcred->cr_rgid) &&
992 newcred->cr_svgid != newcred->cr_groups[0]) {
993 change_svgid(newcred, newcred->cr_groups[0]);
996 proc_set_cred(p, newcred);
1008 * setresuid(ruid, euid, suid) is like setreuid except control over the saved
1011 #ifndef _SYS_SYSPROTO_H_
1012 struct setresuid_args {
1020 sys_setresuid(register struct thread *td, struct setresuid_args *uap)
1022 struct proc *p = td->td_proc;
1023 struct ucred *newcred, *oldcred;
1024 uid_t euid, ruid, suid;
1025 struct uidinfo *euip, *ruip;
1031 AUDIT_ARG_EUID(euid);
1032 AUDIT_ARG_RUID(ruid);
1033 AUDIT_ARG_SUID(suid);
1035 euip = uifind(euid);
1036 ruip = uifind(ruid);
1038 oldcred = crcopysafe(p, newcred);
1041 error = mac_cred_check_setresuid(oldcred, ruid, euid, suid);
1046 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
1047 ruid != oldcred->cr_svuid &&
1048 ruid != oldcred->cr_uid) ||
1049 (euid != (uid_t)-1 && euid != oldcred->cr_ruid &&
1050 euid != oldcred->cr_svuid &&
1051 euid != oldcred->cr_uid) ||
1052 (suid != (uid_t)-1 && suid != oldcred->cr_ruid &&
1053 suid != oldcred->cr_svuid &&
1054 suid != oldcred->cr_uid)) &&
1055 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESUID, 0)) != 0)
1058 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
1059 change_euid(newcred, euip);
1062 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
1063 change_ruid(newcred, ruip);
1066 if (suid != (uid_t)-1 && oldcred->cr_svuid != suid) {
1067 change_svuid(newcred, suid);
1070 proc_set_cred(p, newcred);
1073 racct_proc_ucred_changed(p, oldcred, newcred);
1090 * setresgid(rgid, egid, sgid) is like setregid except control over the saved
1093 #ifndef _SYS_SYSPROTO_H_
1094 struct setresgid_args {
1102 sys_setresgid(register struct thread *td, struct setresgid_args *uap)
1104 struct proc *p = td->td_proc;
1105 struct ucred *newcred, *oldcred;
1106 gid_t egid, rgid, sgid;
1112 AUDIT_ARG_EGID(egid);
1113 AUDIT_ARG_RGID(rgid);
1114 AUDIT_ARG_SGID(sgid);
1117 oldcred = crcopysafe(p, newcred);
1120 error = mac_cred_check_setresgid(oldcred, rgid, egid, sgid);
1125 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
1126 rgid != oldcred->cr_svgid &&
1127 rgid != oldcred->cr_groups[0]) ||
1128 (egid != (gid_t)-1 && egid != oldcred->cr_rgid &&
1129 egid != oldcred->cr_svgid &&
1130 egid != oldcred->cr_groups[0]) ||
1131 (sgid != (gid_t)-1 && sgid != oldcred->cr_rgid &&
1132 sgid != oldcred->cr_svgid &&
1133 sgid != oldcred->cr_groups[0])) &&
1134 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESGID, 0)) != 0)
1137 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
1138 change_egid(newcred, egid);
1141 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
1142 change_rgid(newcred, rgid);
1145 if (sgid != (gid_t)-1 && oldcred->cr_svgid != sgid) {
1146 change_svgid(newcred, sgid);
1149 proc_set_cred(p, newcred);
1160 #ifndef _SYS_SYSPROTO_H_
1161 struct getresuid_args {
1169 sys_getresuid(register struct thread *td, struct getresuid_args *uap)
1172 int error1 = 0, error2 = 0, error3 = 0;
1174 cred = td->td_ucred;
1176 error1 = copyout(&cred->cr_ruid,
1177 uap->ruid, sizeof(cred->cr_ruid));
1179 error2 = copyout(&cred->cr_uid,
1180 uap->euid, sizeof(cred->cr_uid));
1182 error3 = copyout(&cred->cr_svuid,
1183 uap->suid, sizeof(cred->cr_svuid));
1184 return (error1 ? error1 : error2 ? error2 : error3);
1187 #ifndef _SYS_SYSPROTO_H_
1188 struct getresgid_args {
1196 sys_getresgid(register struct thread *td, struct getresgid_args *uap)
1199 int error1 = 0, error2 = 0, error3 = 0;
1201 cred = td->td_ucred;
1203 error1 = copyout(&cred->cr_rgid,
1204 uap->rgid, sizeof(cred->cr_rgid));
1206 error2 = copyout(&cred->cr_groups[0],
1207 uap->egid, sizeof(cred->cr_groups[0]));
1209 error3 = copyout(&cred->cr_svgid,
1210 uap->sgid, sizeof(cred->cr_svgid));
1211 return (error1 ? error1 : error2 ? error2 : error3);
1214 #ifndef _SYS_SYSPROTO_H_
1215 struct issetugid_args {
1221 sys_issetugid(register struct thread *td, struct issetugid_args *uap)
1223 struct proc *p = td->td_proc;
1226 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
1227 * we use P_SUGID because we consider changing the owners as
1228 * "tainting" as well.
1229 * This is significant for procs that start as root and "become"
1230 * a user without an exec - programs cannot know *everything*
1231 * that libc *might* have put in their data segment.
1234 td->td_retval[0] = (p->p_flag & P_SUGID) ? 1 : 0;
1240 sys___setugid(struct thread *td, struct __setugid_args *uap)
1246 switch (uap->flag) {
1249 p->p_flag &= ~P_SUGID;
1254 p->p_flag |= P_SUGID;
1260 #else /* !REGRESSION */
1263 #endif /* REGRESSION */
1267 * Check if gid is a member of the group set.
1270 groupmember(gid_t gid, struct ucred *cred)
1276 if (cred->cr_groups[0] == gid)
1280 * If gid was not our primary group, perform a binary search
1281 * of the supplemental groups. This is possible because we
1282 * sort the groups in crsetgroups().
1285 h = cred->cr_ngroups;
1287 m = l + ((h - l) / 2);
1288 if (cred->cr_groups[m] < gid)
1293 if ((l < cred->cr_ngroups) && (cred->cr_groups[l] == gid))
1300 * Test the active securelevel against a given level. securelevel_gt()
1301 * implements (securelevel > level). securelevel_ge() implements
1302 * (securelevel >= level). Note that the logic is inverted -- these
1303 * functions return EPERM on "success" and 0 on "failure".
1305 * Due to care taken when setting the securelevel, we know that no jail will
1306 * be less secure that its parent (or the physical system), so it is sufficient
1307 * to test the current jail only.
1309 * XXXRW: Possibly since this has to do with privilege, it should move to
1313 securelevel_gt(struct ucred *cr, int level)
1316 return (cr->cr_prison->pr_securelevel > level ? EPERM : 0);
1320 securelevel_ge(struct ucred *cr, int level)
1323 return (cr->cr_prison->pr_securelevel >= level ? EPERM : 0);
1327 * 'see_other_uids' determines whether or not visibility of processes
1328 * and sockets with credentials holding different real uids is possible
1329 * using a variety of system MIBs.
1330 * XXX: data declarations should be together near the beginning of the file.
1332 static int see_other_uids = 1;
1333 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_uids, CTLFLAG_RW,
1335 "Unprivileged processes may see subjects/objects with different real uid");
1338 * Determine if u1 "can see" the subject specified by u2, according to the
1339 * 'see_other_uids' policy.
1340 * Returns: 0 for permitted, ESRCH otherwise
1342 * References: *u1 and *u2 must not change during the call
1343 * u1 may equal u2, in which case only one reference is required
1346 cr_seeotheruids(struct ucred *u1, struct ucred *u2)
1349 if (!see_other_uids && u1->cr_ruid != u2->cr_ruid) {
1350 if (priv_check_cred(u1, PRIV_SEEOTHERUIDS, 0) != 0)
1357 * 'see_other_gids' determines whether or not visibility of processes
1358 * and sockets with credentials holding different real gids is possible
1359 * using a variety of system MIBs.
1360 * XXX: data declarations should be together near the beginning of the file.
1362 static int see_other_gids = 1;
1363 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_gids, CTLFLAG_RW,
1365 "Unprivileged processes may see subjects/objects with different real gid");
1368 * Determine if u1 can "see" the subject specified by u2, according to the
1369 * 'see_other_gids' policy.
1370 * Returns: 0 for permitted, ESRCH otherwise
1372 * References: *u1 and *u2 must not change during the call
1373 * u1 may equal u2, in which case only one reference is required
1376 cr_seeothergids(struct ucred *u1, struct ucred *u2)
1380 if (!see_other_gids) {
1382 for (i = 0; i < u1->cr_ngroups; i++) {
1383 if (groupmember(u1->cr_groups[i], u2))
1389 if (priv_check_cred(u1, PRIV_SEEOTHERGIDS, 0) != 0)
1397 * Determine if u1 "can see" the subject specified by u2.
1398 * Returns: 0 for permitted, an errno value otherwise
1400 * References: *u1 and *u2 must not change during the call
1401 * u1 may equal u2, in which case only one reference is required
1404 cr_cansee(struct ucred *u1, struct ucred *u2)
1408 if ((error = prison_check(u1, u2)))
1411 if ((error = mac_cred_check_visible(u1, u2)))
1414 if ((error = cr_seeotheruids(u1, u2)))
1416 if ((error = cr_seeothergids(u1, u2)))
1422 * Determine if td "can see" the subject specified by p.
1423 * Returns: 0 for permitted, an errno value otherwise
1424 * Locks: Sufficient locks to protect p->p_ucred must be held. td really
1425 * should be curthread.
1426 * References: td and p must be valid for the lifetime of the call
1429 p_cansee(struct thread *td, struct proc *p)
1432 /* Wrap cr_cansee() for all functionality. */
1433 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1434 PROC_LOCK_ASSERT(p, MA_OWNED);
1435 return (cr_cansee(td->td_ucred, p->p_ucred));
1439 * 'conservative_signals' prevents the delivery of a broad class of
1440 * signals by unprivileged processes to processes that have changed their
1441 * credentials since the last invocation of execve(). This can prevent
1442 * the leakage of cached information or retained privileges as a result
1443 * of a common class of signal-related vulnerabilities. However, this
1444 * may interfere with some applications that expect to be able to
1445 * deliver these signals to peer processes after having given up
1448 static int conservative_signals = 1;
1449 SYSCTL_INT(_security_bsd, OID_AUTO, conservative_signals, CTLFLAG_RW,
1450 &conservative_signals, 0, "Unprivileged processes prevented from "
1451 "sending certain signals to processes whose credentials have changed");
1453 * Determine whether cred may deliver the specified signal to proc.
1454 * Returns: 0 for permitted, an errno value otherwise.
1455 * Locks: A lock must be held for proc.
1456 * References: cred and proc must be valid for the lifetime of the call.
1459 cr_cansignal(struct ucred *cred, struct proc *proc, int signum)
1463 PROC_LOCK_ASSERT(proc, MA_OWNED);
1465 * Jail semantics limit the scope of signalling to proc in the
1466 * same jail as cred, if cred is in jail.
1468 error = prison_check(cred, proc->p_ucred);
1472 if ((error = mac_proc_check_signal(cred, proc, signum)))
1475 if ((error = cr_seeotheruids(cred, proc->p_ucred)))
1477 if ((error = cr_seeothergids(cred, proc->p_ucred)))
1481 * UNIX signal semantics depend on the status of the P_SUGID
1482 * bit on the target process. If the bit is set, then additional
1483 * restrictions are placed on the set of available signals.
1485 if (conservative_signals && (proc->p_flag & P_SUGID)) {
1500 * Generally, permit job and terminal control
1505 /* Not permitted without privilege. */
1506 error = priv_check_cred(cred, PRIV_SIGNAL_SUGID, 0);
1513 * Generally, the target credential's ruid or svuid must match the
1514 * subject credential's ruid or euid.
1516 if (cred->cr_ruid != proc->p_ucred->cr_ruid &&
1517 cred->cr_ruid != proc->p_ucred->cr_svuid &&
1518 cred->cr_uid != proc->p_ucred->cr_ruid &&
1519 cred->cr_uid != proc->p_ucred->cr_svuid) {
1520 error = priv_check_cred(cred, PRIV_SIGNAL_DIFFCRED, 0);
1529 * Determine whether td may deliver the specified signal to p.
1530 * Returns: 0 for permitted, an errno value otherwise
1531 * Locks: Sufficient locks to protect various components of td and p
1532 * must be held. td must be curthread, and a lock must be
1534 * References: td and p must be valid for the lifetime of the call
1537 p_cansignal(struct thread *td, struct proc *p, int signum)
1540 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1541 PROC_LOCK_ASSERT(p, MA_OWNED);
1542 if (td->td_proc == p)
1546 * UNIX signalling semantics require that processes in the same
1547 * session always be able to deliver SIGCONT to one another,
1548 * overriding the remaining protections.
1550 /* XXX: This will require an additional lock of some sort. */
1551 if (signum == SIGCONT && td->td_proc->p_session == p->p_session)
1554 * Some compat layers use SIGTHR and higher signals for
1555 * communication between different kernel threads of the same
1556 * process, so that they expect that it's always possible to
1557 * deliver them, even for suid applications where cr_cansignal() can
1558 * deny such ability for security consideration. It should be
1559 * pretty safe to do since the only way to create two processes
1560 * with the same p_leader is via rfork(2).
1562 if (td->td_proc->p_leader != NULL && signum >= SIGTHR &&
1563 signum < SIGTHR + 4 && td->td_proc->p_leader == p->p_leader)
1566 return (cr_cansignal(td->td_ucred, p, signum));
1570 * Determine whether td may reschedule p.
1571 * Returns: 0 for permitted, an errno value otherwise
1572 * Locks: Sufficient locks to protect various components of td and p
1573 * must be held. td must be curthread, and a lock must
1575 * References: td and p must be valid for the lifetime of the call
1578 p_cansched(struct thread *td, struct proc *p)
1582 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1583 PROC_LOCK_ASSERT(p, MA_OWNED);
1584 if (td->td_proc == p)
1586 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1589 if ((error = mac_proc_check_sched(td->td_ucred, p)))
1592 if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1594 if ((error = cr_seeothergids(td->td_ucred, p->p_ucred)))
1596 if (td->td_ucred->cr_ruid != p->p_ucred->cr_ruid &&
1597 td->td_ucred->cr_uid != p->p_ucred->cr_ruid) {
1598 error = priv_check(td, PRIV_SCHED_DIFFCRED);
1606 * The 'unprivileged_proc_debug' flag may be used to disable a variety of
1607 * unprivileged inter-process debugging services, including some procfs
1608 * functionality, ptrace(), and ktrace(). In the past, inter-process
1609 * debugging has been involved in a variety of security problems, and sites
1610 * not requiring the service might choose to disable it when hardening
1613 * XXX: Should modifying and reading this variable require locking?
1614 * XXX: data declarations should be together near the beginning of the file.
1616 static int unprivileged_proc_debug = 1;
1617 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_proc_debug, CTLFLAG_RW,
1618 &unprivileged_proc_debug, 0,
1619 "Unprivileged processes may use process debugging facilities");
1622 * Determine whether td may debug p.
1623 * Returns: 0 for permitted, an errno value otherwise
1624 * Locks: Sufficient locks to protect various components of td and p
1625 * must be held. td must be curthread, and a lock must
1627 * References: td and p must be valid for the lifetime of the call
1630 p_candebug(struct thread *td, struct proc *p)
1632 int credentialchanged, error, grpsubset, i, uidsubset;
1634 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1635 PROC_LOCK_ASSERT(p, MA_OWNED);
1636 if (!unprivileged_proc_debug) {
1637 error = priv_check(td, PRIV_DEBUG_UNPRIV);
1641 if (td->td_proc == p)
1643 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1646 if ((error = mac_proc_check_debug(td->td_ucred, p)))
1649 if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1651 if ((error = cr_seeothergids(td->td_ucred, p->p_ucred)))
1655 * Is p's group set a subset of td's effective group set? This
1656 * includes p's egid, group access list, rgid, and svgid.
1659 for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
1660 if (!groupmember(p->p_ucred->cr_groups[i], td->td_ucred)) {
1665 grpsubset = grpsubset &&
1666 groupmember(p->p_ucred->cr_rgid, td->td_ucred) &&
1667 groupmember(p->p_ucred->cr_svgid, td->td_ucred);
1670 * Are the uids present in p's credential equal to td's
1671 * effective uid? This includes p's euid, svuid, and ruid.
1673 uidsubset = (td->td_ucred->cr_uid == p->p_ucred->cr_uid &&
1674 td->td_ucred->cr_uid == p->p_ucred->cr_svuid &&
1675 td->td_ucred->cr_uid == p->p_ucred->cr_ruid);
1678 * Has the credential of the process changed since the last exec()?
1680 credentialchanged = (p->p_flag & P_SUGID);
1683 * If p's gids aren't a subset, or the uids aren't a subset,
1684 * or the credential has changed, require appropriate privilege
1685 * for td to debug p.
1687 if (!grpsubset || !uidsubset) {
1688 error = priv_check(td, PRIV_DEBUG_DIFFCRED);
1693 if (credentialchanged) {
1694 error = priv_check(td, PRIV_DEBUG_SUGID);
1699 /* Can't trace init when securelevel > 0. */
1700 if (p == initproc) {
1701 error = securelevel_gt(td->td_ucred, 0);
1707 * Can't trace a process that's currently exec'ing.
1709 * XXX: Note, this is not a security policy decision, it's a
1710 * basic correctness/functionality decision. Therefore, this check
1711 * should be moved to the caller's of p_candebug().
1713 if ((p->p_flag & P_INEXEC) != 0)
1716 /* Denied explicitely */
1717 if ((p->p_flag2 & P2_NOTRACE) != 0) {
1718 error = priv_check(td, PRIV_DEBUG_DENIED);
1727 * Determine whether the subject represented by cred can "see" a socket.
1728 * Returns: 0 for permitted, ENOENT otherwise.
1731 cr_canseesocket(struct ucred *cred, struct socket *so)
1735 error = prison_check(cred, so->so_cred);
1739 error = mac_socket_check_visible(cred, so);
1743 if (cr_seeotheruids(cred, so->so_cred))
1745 if (cr_seeothergids(cred, so->so_cred))
1751 #if defined(INET) || defined(INET6)
1753 * Determine whether the subject represented by cred can "see" a socket.
1754 * Returns: 0 for permitted, ENOENT otherwise.
1757 cr_canseeinpcb(struct ucred *cred, struct inpcb *inp)
1761 error = prison_check(cred, inp->inp_cred);
1765 INP_LOCK_ASSERT(inp);
1766 error = mac_inpcb_check_visible(cred, inp);
1770 if (cr_seeotheruids(cred, inp->inp_cred))
1772 if (cr_seeothergids(cred, inp->inp_cred))
1780 * Determine whether td can wait for the exit of p.
1781 * Returns: 0 for permitted, an errno value otherwise
1782 * Locks: Sufficient locks to protect various components of td and p
1783 * must be held. td must be curthread, and a lock must
1785 * References: td and p must be valid for the lifetime of the call
1789 p_canwait(struct thread *td, struct proc *p)
1793 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1794 PROC_LOCK_ASSERT(p, MA_OWNED);
1795 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1798 if ((error = mac_proc_check_wait(td->td_ucred, p)))
1802 /* XXXMAC: This could have odd effects on some shells. */
1803 if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1811 * Allocate a zeroed cred structure.
1816 register struct ucred *cr;
1818 cr = malloc(sizeof(*cr), M_CRED, M_WAITOK | M_ZERO);
1819 refcount_init(&cr->cr_ref, 1);
1821 audit_cred_init(cr);
1826 cr->cr_groups = cr->cr_smallgroups;
1828 sizeof(cr->cr_smallgroups) / sizeof(cr->cr_smallgroups[0]);
1833 * Claim another reference to a ucred structure.
1836 crhold(struct ucred *cr)
1839 refcount_acquire(&cr->cr_ref);
1844 * Free a cred structure. Throws away space when ref count gets to 0.
1847 crfree(struct ucred *cr)
1850 KASSERT(cr->cr_ref > 0, ("bad ucred refcount: %d", cr->cr_ref));
1851 KASSERT(cr->cr_ref != 0xdeadc0de, ("dangling reference to ucred"));
1852 if (refcount_release(&cr->cr_ref)) {
1854 * Some callers of crget(), such as nfs_statfs(),
1855 * allocate a temporary credential, but don't
1856 * allocate a uidinfo structure.
1858 if (cr->cr_uidinfo != NULL)
1859 uifree(cr->cr_uidinfo);
1860 if (cr->cr_ruidinfo != NULL)
1861 uifree(cr->cr_ruidinfo);
1863 * Free a prison, if any.
1865 if (cr->cr_prison != NULL)
1866 prison_free(cr->cr_prison);
1867 if (cr->cr_loginclass != NULL)
1868 loginclass_free(cr->cr_loginclass);
1870 audit_cred_destroy(cr);
1873 mac_cred_destroy(cr);
1875 if (cr->cr_groups != cr->cr_smallgroups)
1876 free(cr->cr_groups, M_CRED);
1882 * Copy a ucred's contents from a template. Does not block.
1885 crcopy(struct ucred *dest, struct ucred *src)
1888 KASSERT(dest->cr_ref == 1, ("crcopy of shared ucred"));
1889 bcopy(&src->cr_startcopy, &dest->cr_startcopy,
1890 (unsigned)((caddr_t)&src->cr_endcopy -
1891 (caddr_t)&src->cr_startcopy));
1892 crsetgroups(dest, src->cr_ngroups, src->cr_groups);
1893 uihold(dest->cr_uidinfo);
1894 uihold(dest->cr_ruidinfo);
1895 prison_hold(dest->cr_prison);
1896 loginclass_hold(dest->cr_loginclass);
1898 audit_cred_copy(src, dest);
1901 mac_cred_copy(src, dest);
1906 * Dup cred struct to a new held one.
1909 crdup(struct ucred *cr)
1911 struct ucred *newcr;
1919 * Fill in a struct xucred based on a struct ucred.
1922 cru2x(struct ucred *cr, struct xucred *xcr)
1926 bzero(xcr, sizeof(*xcr));
1927 xcr->cr_version = XUCRED_VERSION;
1928 xcr->cr_uid = cr->cr_uid;
1930 ngroups = MIN(cr->cr_ngroups, XU_NGROUPS);
1931 xcr->cr_ngroups = ngroups;
1932 bcopy(cr->cr_groups, xcr->cr_groups,
1933 ngroups * sizeof(*cr->cr_groups));
1937 * Set initial process credentials.
1938 * Callers are responsible for providing the reference for provided credentials.
1941 proc_set_cred_init(struct proc *p, struct ucred *newcred)
1944 p->p_ucred = newcred;
1948 * Change process credentials.
1949 * Callers are responsible for providing the reference for passed credentials
1950 * and for freeing old ones.
1952 * Process has to be locked except when it does not have credentials (as it
1953 * should not be visible just yet) or when newcred is NULL (as this can be
1954 * only used when the process is about to be freed, at which point it should
1955 * not be visible anymore).
1958 proc_set_cred(struct proc *p, struct ucred *newcred)
1960 struct ucred *oldcred;
1962 MPASS(p->p_ucred != NULL);
1963 if (newcred == NULL)
1964 MPASS(p->p_state == PRS_ZOMBIE);
1966 PROC_LOCK_ASSERT(p, MA_OWNED);
1968 oldcred = p->p_ucred;
1969 p->p_ucred = newcred;
1970 if (newcred != NULL)
1976 crcopysafe(struct proc *p, struct ucred *cr)
1978 struct ucred *oldcred;
1981 PROC_LOCK_ASSERT(p, MA_OWNED);
1983 oldcred = p->p_ucred;
1984 while (cr->cr_agroups < oldcred->cr_agroups) {
1985 groups = oldcred->cr_agroups;
1987 crextend(cr, groups);
1989 oldcred = p->p_ucred;
1991 crcopy(cr, oldcred);
1997 * Extend the passed in credential to hold n items.
2000 crextend(struct ucred *cr, int n)
2005 if (n <= cr->cr_agroups)
2009 * We extend by 2 each time since we're using a power of two
2010 * allocator until we need enough groups to fill a page.
2011 * Once we're allocating multiple pages, only allocate as many
2012 * as we actually need. The case of processes needing a
2013 * non-power of two number of pages seems more likely than
2014 * a real world process that adds thousands of groups one at a
2017 if ( n < PAGE_SIZE / sizeof(gid_t) ) {
2018 if (cr->cr_agroups == 0)
2019 cnt = MINALLOCSIZE / sizeof(gid_t);
2021 cnt = cr->cr_agroups * 2;
2026 cnt = roundup2(n, PAGE_SIZE / sizeof(gid_t));
2028 /* Free the old array. */
2029 if (cr->cr_groups != cr->cr_smallgroups)
2030 free(cr->cr_groups, M_CRED);
2032 cr->cr_groups = malloc(cnt * sizeof(gid_t), M_CRED, M_WAITOK | M_ZERO);
2033 cr->cr_agroups = cnt;
2037 * Copy groups in to a credential, preserving any necessary invariants.
2038 * Currently this includes the sorting of all supplemental gids.
2039 * crextend() must have been called before hand to ensure sufficient
2040 * space is available.
2043 crsetgroups_locked(struct ucred *cr, int ngrp, gid_t *groups)
2049 KASSERT(cr->cr_agroups >= ngrp, ("cr_ngroups is too small"));
2051 bcopy(groups, cr->cr_groups, ngrp * sizeof(gid_t));
2052 cr->cr_ngroups = ngrp;
2055 * Sort all groups except cr_groups[0] to allow groupmember to
2056 * perform a binary search.
2058 * XXX: If large numbers of groups become common this should
2059 * be replaced with shell sort like linux uses or possibly
2062 for (i = 2; i < ngrp; i++) {
2063 g = cr->cr_groups[i];
2064 for (j = i-1; j >= 1 && g < cr->cr_groups[j]; j--)
2065 cr->cr_groups[j + 1] = cr->cr_groups[j];
2066 cr->cr_groups[j + 1] = g;
2071 * Copy groups in to a credential after expanding it if required.
2072 * Truncate the list to (ngroups_max + 1) if it is too large.
2075 crsetgroups(struct ucred *cr, int ngrp, gid_t *groups)
2078 if (ngrp > ngroups_max + 1)
2079 ngrp = ngroups_max + 1;
2082 crsetgroups_locked(cr, ngrp, groups);
2086 * Get login name, if available.
2088 #ifndef _SYS_SYSPROTO_H_
2089 struct getlogin_args {
2096 sys_getlogin(struct thread *td, struct getlogin_args *uap)
2098 char login[MAXLOGNAME];
2099 struct proc *p = td->td_proc;
2102 if (uap->namelen > MAXLOGNAME)
2103 uap->namelen = MAXLOGNAME;
2105 SESS_LOCK(p->p_session);
2106 len = strlcpy(login, p->p_session->s_login, uap->namelen) + 1;
2107 SESS_UNLOCK(p->p_session);
2109 if (len > uap->namelen)
2111 return (copyout(login, uap->namebuf, len));
2117 #ifndef _SYS_SYSPROTO_H_
2118 struct setlogin_args {
2124 sys_setlogin(struct thread *td, struct setlogin_args *uap)
2126 struct proc *p = td->td_proc;
2128 char logintmp[MAXLOGNAME];
2130 CTASSERT(sizeof(p->p_session->s_login) >= sizeof(logintmp));
2132 error = priv_check(td, PRIV_PROC_SETLOGIN);
2135 error = copyinstr(uap->namebuf, logintmp, sizeof(logintmp), NULL);
2137 if (error == ENAMETOOLONG)
2142 SESS_LOCK(p->p_session);
2143 strcpy(p->p_session->s_login, logintmp);
2144 SESS_UNLOCK(p->p_session);
2150 setsugid(struct proc *p)
2153 PROC_LOCK_ASSERT(p, MA_OWNED);
2154 p->p_flag |= P_SUGID;
2155 if (!(p->p_pfsflags & PF_ISUGID))
2160 * Change a process's effective uid.
2161 * Side effects: newcred->cr_uid and newcred->cr_uidinfo will be modified.
2162 * References: newcred must be an exclusive credential reference for the
2163 * duration of the call.
2166 change_euid(struct ucred *newcred, struct uidinfo *euip)
2169 newcred->cr_uid = euip->ui_uid;
2171 uifree(newcred->cr_uidinfo);
2172 newcred->cr_uidinfo = euip;
2176 * Change a process's effective gid.
2177 * Side effects: newcred->cr_gid will be modified.
2178 * References: newcred must be an exclusive credential reference for the
2179 * duration of the call.
2182 change_egid(struct ucred *newcred, gid_t egid)
2185 newcred->cr_groups[0] = egid;
2189 * Change a process's real uid.
2190 * Side effects: newcred->cr_ruid will be updated, newcred->cr_ruidinfo
2191 * will be updated, and the old and new cr_ruidinfo proc
2192 * counts will be updated.
2193 * References: newcred must be an exclusive credential reference for the
2194 * duration of the call.
2197 change_ruid(struct ucred *newcred, struct uidinfo *ruip)
2200 (void)chgproccnt(newcred->cr_ruidinfo, -1, 0);
2201 newcred->cr_ruid = ruip->ui_uid;
2203 uifree(newcred->cr_ruidinfo);
2204 newcred->cr_ruidinfo = ruip;
2205 (void)chgproccnt(newcred->cr_ruidinfo, 1, 0);
2209 * Change a process's real gid.
2210 * Side effects: newcred->cr_rgid will be updated.
2211 * References: newcred must be an exclusive credential reference for the
2212 * duration of the call.
2215 change_rgid(struct ucred *newcred, gid_t rgid)
2218 newcred->cr_rgid = rgid;
2222 * Change a process's saved uid.
2223 * Side effects: newcred->cr_svuid will be updated.
2224 * References: newcred must be an exclusive credential reference for the
2225 * duration of the call.
2228 change_svuid(struct ucred *newcred, uid_t svuid)
2231 newcred->cr_svuid = svuid;
2235 * Change a process's saved gid.
2236 * Side effects: newcred->cr_svgid will be updated.
2237 * References: newcred must be an exclusive credential reference for the
2238 * duration of the call.
2241 change_svgid(struct ucred *newcred, gid_t svgid)
2244 newcred->cr_svgid = svgid;