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 * 3. 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 #include <security/audit/audit.h>
80 #include <security/mac/mac_framework.h>
82 static MALLOC_DEFINE(M_CRED, "cred", "credentials");
84 SYSCTL_NODE(_security, OID_AUTO, bsd, CTLFLAG_RW, 0, "BSD security policy");
86 static void crsetgroups_locked(struct ucred *cr, int ngrp,
89 #ifndef _SYS_SYSPROTO_H_
96 sys_getpid(struct thread *td, struct getpid_args *uap)
98 struct proc *p = td->td_proc;
100 td->td_retval[0] = p->p_pid;
101 #if defined(COMPAT_43)
102 td->td_retval[1] = kern_getppid(td);
107 #ifndef _SYS_SYSPROTO_H_
108 struct getppid_args {
114 sys_getppid(struct thread *td, struct getppid_args *uap)
117 td->td_retval[0] = kern_getppid(td);
122 kern_getppid(struct thread *td)
124 struct proc *p = td->td_proc;
129 if (!(p->p_flag & P_TRACED)) {
130 ppid = p->p_pptr->p_pid;
134 sx_slock(&proctree_lock);
135 pp = proc_realparent(p);
137 sx_sunlock(&proctree_lock);
144 * Get process group ID; note that POSIX getpgrp takes no parameter.
146 #ifndef _SYS_SYSPROTO_H_
147 struct getpgrp_args {
152 sys_getpgrp(struct thread *td, struct getpgrp_args *uap)
154 struct proc *p = td->td_proc;
157 td->td_retval[0] = p->p_pgrp->pg_id;
162 /* Get an arbitrary pid's process group id */
163 #ifndef _SYS_SYSPROTO_H_
164 struct getpgid_args {
169 sys_getpgid(struct thread *td, struct getpgid_args *uap)
181 error = p_cansee(td, p);
187 td->td_retval[0] = p->p_pgrp->pg_id;
193 * Get an arbitrary pid's session id.
195 #ifndef _SYS_SYSPROTO_H_
201 sys_getsid(struct thread *td, struct getsid_args *uap)
213 error = p_cansee(td, p);
219 td->td_retval[0] = p->p_session->s_sid;
224 #ifndef _SYS_SYSPROTO_H_
231 sys_getuid(struct thread *td, struct getuid_args *uap)
234 td->td_retval[0] = td->td_ucred->cr_ruid;
235 #if defined(COMPAT_43)
236 td->td_retval[1] = td->td_ucred->cr_uid;
241 #ifndef _SYS_SYSPROTO_H_
242 struct geteuid_args {
248 sys_geteuid(struct thread *td, struct geteuid_args *uap)
251 td->td_retval[0] = td->td_ucred->cr_uid;
255 #ifndef _SYS_SYSPROTO_H_
262 sys_getgid(struct thread *td, struct getgid_args *uap)
265 td->td_retval[0] = td->td_ucred->cr_rgid;
266 #if defined(COMPAT_43)
267 td->td_retval[1] = td->td_ucred->cr_groups[0];
273 * Get effective group ID. The "egid" is groups[0], and could be obtained
274 * via getgroups. This syscall exists because it is somewhat painful to do
275 * correctly in a library function.
277 #ifndef _SYS_SYSPROTO_H_
278 struct getegid_args {
284 sys_getegid(struct thread *td, struct getegid_args *uap)
287 td->td_retval[0] = td->td_ucred->cr_groups[0];
291 #ifndef _SYS_SYSPROTO_H_
292 struct getgroups_args {
298 sys_getgroups(struct thread *td, register struct getgroups_args *uap)
305 ngrp = cred->cr_ngroups;
307 if (uap->gidsetsize == 0) {
311 if (uap->gidsetsize < ngrp)
314 error = copyout(cred->cr_groups, uap->gidset, ngrp * sizeof(gid_t));
316 td->td_retval[0] = ngrp;
320 #ifndef _SYS_SYSPROTO_H_
327 sys_setsid(register struct thread *td, struct setsid_args *uap)
331 struct proc *p = td->td_proc;
332 struct pgrp *newpgrp;
333 struct session *newsess;
338 newpgrp = malloc(sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
339 newsess = malloc(sizeof(struct session), M_SESSION, M_WAITOK | M_ZERO);
341 sx_xlock(&proctree_lock);
343 if (p->p_pgid == p->p_pid || (pgrp = pgfind(p->p_pid)) != NULL) {
348 (void)enterpgrp(p, p->p_pid, newpgrp, newsess);
349 td->td_retval[0] = p->p_pid;
354 sx_xunlock(&proctree_lock);
357 free(newpgrp, M_PGRP);
359 free(newsess, M_SESSION);
365 * set process group (setpgid/old setpgrp)
367 * caller does setpgid(targpid, targpgid)
369 * pid must be caller or child of caller (ESRCH)
371 * pid must be in same session (EPERM)
372 * pid can't have done an exec (EACCES)
374 * there must exist some pid in same session having pgid (EPERM)
375 * pid must not be session leader (EPERM)
377 #ifndef _SYS_SYSPROTO_H_
378 struct setpgid_args {
379 int pid; /* target process id */
380 int pgid; /* target pgrp id */
385 sys_setpgid(struct thread *td, register struct setpgid_args *uap)
387 struct proc *curp = td->td_proc;
388 register struct proc *targp; /* target process */
389 register struct pgrp *pgrp; /* target pgrp */
391 struct pgrp *newpgrp;
398 newpgrp = malloc(sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
400 sx_xlock(&proctree_lock);
401 if (uap->pid != 0 && uap->pid != curp->p_pid) {
402 if ((targp = pfind(uap->pid)) == NULL) {
406 if (!inferior(targp)) {
411 if ((error = p_cansee(td, targp))) {
415 if (targp->p_pgrp == NULL ||
416 targp->p_session != curp->p_session) {
421 if (targp->p_flag & P_EXEC) {
429 if (SESS_LEADER(targp)) {
434 uap->pgid = targp->p_pid;
435 if ((pgrp = pgfind(uap->pgid)) == NULL) {
436 if (uap->pgid == targp->p_pid) {
437 error = enterpgrp(targp, uap->pgid, newpgrp,
444 if (pgrp == targp->p_pgrp) {
448 if (pgrp->pg_id != targp->p_pid &&
449 pgrp->pg_session != curp->p_session) {
455 error = enterthispgrp(targp, pgrp);
458 sx_xunlock(&proctree_lock);
459 KASSERT((error == 0) || (newpgrp != NULL),
460 ("setpgid failed and newpgrp is NULL"));
462 free(newpgrp, M_PGRP);
467 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
468 * compatible. It says that setting the uid/gid to euid/egid is a special
469 * case of "appropriate privilege". Once the rules are expanded out, this
470 * basically means that setuid(nnn) sets all three id's, in all permitted
471 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
472 * does not set the saved id - this is dangerous for traditional BSD
473 * programs. For this reason, we *really* do not want to set
474 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
476 #define POSIX_APPENDIX_B_4_2_2
478 #ifndef _SYS_SYSPROTO_H_
485 sys_setuid(struct thread *td, struct setuid_args *uap)
487 struct proc *p = td->td_proc;
488 struct ucred *newcred, *oldcred;
499 * Copy credentials so other references do not see our changes.
501 oldcred = crcopysafe(p, newcred);
504 error = mac_cred_check_setuid(oldcred, uid);
510 * See if we have "permission" by POSIX 1003.1 rules.
512 * Note that setuid(geteuid()) is a special case of
513 * "appropriate privileges" in appendix B.4.2.2. We need
514 * to use this clause to be compatible with traditional BSD
515 * semantics. Basically, it means that "setuid(xx)" sets all
516 * three id's (assuming you have privs).
518 * Notes on the logic. We do things in three steps.
519 * 1: We determine if the euid is going to change, and do EPERM
520 * right away. We unconditionally change the euid later if this
521 * test is satisfied, simplifying that part of the logic.
522 * 2: We determine if the real and/or saved uids are going to
523 * change. Determined by compile options.
524 * 3: Change euid last. (after tests in #2 for "appropriate privs")
526 if (uid != oldcred->cr_ruid && /* allow setuid(getuid()) */
527 #ifdef _POSIX_SAVED_IDS
528 uid != oldcred->cr_svuid && /* allow setuid(saved gid) */
530 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
531 uid != oldcred->cr_uid && /* allow setuid(geteuid()) */
533 (error = priv_check_cred(oldcred, PRIV_CRED_SETUID, 0)) != 0)
536 #ifdef _POSIX_SAVED_IDS
538 * Do we have "appropriate privileges" (are we root or uid == euid)
539 * If so, we are changing the real uid and/or saved uid.
542 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
543 uid == oldcred->cr_uid ||
545 /* We are using privs. */
546 priv_check_cred(oldcred, PRIV_CRED_SETUID, 0) == 0)
550 * Set the real uid and transfer proc count to new user.
552 if (uid != oldcred->cr_ruid) {
553 change_ruid(newcred, uip);
559 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
560 * the security of seteuid() depends on it. B.4.2.2 says it
561 * is important that we should do this.
563 if (uid != oldcred->cr_svuid) {
564 change_svuid(newcred, uid);
570 * In all permitted cases, we are changing the euid.
572 if (uid != oldcred->cr_uid) {
573 change_euid(newcred, uip);
576 proc_set_cred(p, newcred);
579 racct_proc_ucred_changed(p, oldcred, newcred);
592 #ifndef _SYS_SYSPROTO_H_
593 struct seteuid_args {
599 sys_seteuid(struct thread *td, struct seteuid_args *uap)
601 struct proc *p = td->td_proc;
602 struct ucred *newcred, *oldcred;
604 struct uidinfo *euip;
608 AUDIT_ARG_EUID(euid);
613 * Copy credentials so other references do not see our changes.
615 oldcred = crcopysafe(p, newcred);
618 error = mac_cred_check_seteuid(oldcred, euid);
623 if (euid != oldcred->cr_ruid && /* allow seteuid(getuid()) */
624 euid != oldcred->cr_svuid && /* allow seteuid(saved uid) */
625 (error = priv_check_cred(oldcred, PRIV_CRED_SETEUID, 0)) != 0)
629 * Everything's okay, do it.
631 if (oldcred->cr_uid != euid) {
632 change_euid(newcred, euip);
635 proc_set_cred(p, newcred);
648 #ifndef _SYS_SYSPROTO_H_
655 sys_setgid(struct thread *td, struct setgid_args *uap)
657 struct proc *p = td->td_proc;
658 struct ucred *newcred, *oldcred;
666 oldcred = crcopysafe(p, newcred);
669 error = mac_cred_check_setgid(oldcred, gid);
675 * See if we have "permission" by POSIX 1003.1 rules.
677 * Note that setgid(getegid()) is a special case of
678 * "appropriate privileges" in appendix B.4.2.2. We need
679 * to use this clause to be compatible with traditional BSD
680 * semantics. Basically, it means that "setgid(xx)" sets all
681 * three id's (assuming you have privs).
683 * For notes on the logic here, see setuid() above.
685 if (gid != oldcred->cr_rgid && /* allow setgid(getgid()) */
686 #ifdef _POSIX_SAVED_IDS
687 gid != oldcred->cr_svgid && /* allow setgid(saved gid) */
689 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
690 gid != oldcred->cr_groups[0] && /* allow setgid(getegid()) */
692 (error = priv_check_cred(oldcred, PRIV_CRED_SETGID, 0)) != 0)
695 #ifdef _POSIX_SAVED_IDS
697 * Do we have "appropriate privileges" (are we root or gid == egid)
698 * If so, we are changing the real uid and saved gid.
701 #ifdef POSIX_APPENDIX_B_4_2_2 /* use the clause from B.4.2.2 */
702 gid == oldcred->cr_groups[0] ||
704 /* We are using privs. */
705 priv_check_cred(oldcred, PRIV_CRED_SETGID, 0) == 0)
711 if (oldcred->cr_rgid != gid) {
712 change_rgid(newcred, gid);
718 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
719 * the security of setegid() depends on it. B.4.2.2 says it
720 * is important that we should do this.
722 if (oldcred->cr_svgid != gid) {
723 change_svgid(newcred, gid);
728 * In all cases permitted cases, we are changing the egid.
729 * Copy credentials so other references do not see our changes.
731 if (oldcred->cr_groups[0] != gid) {
732 change_egid(newcred, gid);
735 proc_set_cred(p, newcred);
746 #ifndef _SYS_SYSPROTO_H_
747 struct setegid_args {
753 sys_setegid(struct thread *td, struct setegid_args *uap)
755 struct proc *p = td->td_proc;
756 struct ucred *newcred, *oldcred;
761 AUDIT_ARG_EGID(egid);
764 oldcred = crcopysafe(p, newcred);
767 error = mac_cred_check_setegid(oldcred, egid);
772 if (egid != oldcred->cr_rgid && /* allow setegid(getgid()) */
773 egid != oldcred->cr_svgid && /* allow setegid(saved gid) */
774 (error = priv_check_cred(oldcred, PRIV_CRED_SETEGID, 0)) != 0)
777 if (oldcred->cr_groups[0] != egid) {
778 change_egid(newcred, egid);
781 proc_set_cred(p, newcred);
792 #ifndef _SYS_SYSPROTO_H_
793 struct setgroups_args {
800 sys_setgroups(struct thread *td, struct setgroups_args *uap)
802 gid_t smallgroups[XU_NGROUPS];
807 gidsetsize = uap->gidsetsize;
808 if (gidsetsize > ngroups_max + 1)
811 if (gidsetsize > XU_NGROUPS)
812 groups = malloc(gidsetsize * sizeof(gid_t), M_TEMP, M_WAITOK);
814 groups = smallgroups;
816 error = copyin(uap->gidset, groups, gidsetsize * sizeof(gid_t));
818 error = kern_setgroups(td, gidsetsize, groups);
820 if (gidsetsize > XU_NGROUPS)
821 free(groups, M_TEMP);
826 kern_setgroups(struct thread *td, u_int ngrp, gid_t *groups)
828 struct proc *p = td->td_proc;
829 struct ucred *newcred, *oldcred;
832 MPASS(ngrp <= ngroups_max + 1);
833 AUDIT_ARG_GROUPSET(groups, ngrp);
835 crextend(newcred, ngrp);
837 oldcred = crcopysafe(p, newcred);
840 error = mac_cred_check_setgroups(oldcred, ngrp, groups);
845 error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0);
851 * setgroups(0, NULL) is a legitimate way of clearing the
852 * groups vector on non-BSD systems (which generally do not
853 * have the egid in the groups[0]). We risk security holes
854 * when running non-BSD software if we do not do the same.
856 newcred->cr_ngroups = 1;
858 crsetgroups_locked(newcred, ngrp, groups);
861 proc_set_cred(p, newcred);
872 #ifndef _SYS_SYSPROTO_H_
873 struct setreuid_args {
880 sys_setreuid(register struct thread *td, struct setreuid_args *uap)
882 struct proc *p = td->td_proc;
883 struct ucred *newcred, *oldcred;
885 struct uidinfo *euip, *ruip;
890 AUDIT_ARG_EUID(euid);
891 AUDIT_ARG_RUID(ruid);
896 oldcred = crcopysafe(p, newcred);
899 error = mac_cred_check_setreuid(oldcred, ruid, euid);
904 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
905 ruid != oldcred->cr_svuid) ||
906 (euid != (uid_t)-1 && euid != oldcred->cr_uid &&
907 euid != oldcred->cr_ruid && euid != oldcred->cr_svuid)) &&
908 (error = priv_check_cred(oldcred, PRIV_CRED_SETREUID, 0)) != 0)
911 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
912 change_euid(newcred, euip);
915 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
916 change_ruid(newcred, ruip);
919 if ((ruid != (uid_t)-1 || newcred->cr_uid != newcred->cr_ruid) &&
920 newcred->cr_svuid != newcred->cr_uid) {
921 change_svuid(newcred, newcred->cr_uid);
924 proc_set_cred(p, newcred);
927 racct_proc_ucred_changed(p, oldcred, newcred);
942 #ifndef _SYS_SYSPROTO_H_
943 struct setregid_args {
950 sys_setregid(register struct thread *td, struct setregid_args *uap)
952 struct proc *p = td->td_proc;
953 struct ucred *newcred, *oldcred;
959 AUDIT_ARG_EGID(egid);
960 AUDIT_ARG_RGID(rgid);
963 oldcred = crcopysafe(p, newcred);
966 error = mac_cred_check_setregid(oldcred, rgid, egid);
971 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
972 rgid != oldcred->cr_svgid) ||
973 (egid != (gid_t)-1 && egid != oldcred->cr_groups[0] &&
974 egid != oldcred->cr_rgid && egid != oldcred->cr_svgid)) &&
975 (error = priv_check_cred(oldcred, PRIV_CRED_SETREGID, 0)) != 0)
978 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
979 change_egid(newcred, egid);
982 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
983 change_rgid(newcred, rgid);
986 if ((rgid != (gid_t)-1 || newcred->cr_groups[0] != newcred->cr_rgid) &&
987 newcred->cr_svgid != newcred->cr_groups[0]) {
988 change_svgid(newcred, newcred->cr_groups[0]);
991 proc_set_cred(p, newcred);
1003 * setresuid(ruid, euid, suid) is like setreuid except control over the saved
1006 #ifndef _SYS_SYSPROTO_H_
1007 struct setresuid_args {
1015 sys_setresuid(register struct thread *td, struct setresuid_args *uap)
1017 struct proc *p = td->td_proc;
1018 struct ucred *newcred, *oldcred;
1019 uid_t euid, ruid, suid;
1020 struct uidinfo *euip, *ruip;
1026 AUDIT_ARG_EUID(euid);
1027 AUDIT_ARG_RUID(ruid);
1028 AUDIT_ARG_SUID(suid);
1030 euip = uifind(euid);
1031 ruip = uifind(ruid);
1033 oldcred = crcopysafe(p, newcred);
1036 error = mac_cred_check_setresuid(oldcred, ruid, euid, suid);
1041 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
1042 ruid != oldcred->cr_svuid &&
1043 ruid != oldcred->cr_uid) ||
1044 (euid != (uid_t)-1 && euid != oldcred->cr_ruid &&
1045 euid != oldcred->cr_svuid &&
1046 euid != oldcred->cr_uid) ||
1047 (suid != (uid_t)-1 && suid != oldcred->cr_ruid &&
1048 suid != oldcred->cr_svuid &&
1049 suid != oldcred->cr_uid)) &&
1050 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESUID, 0)) != 0)
1053 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
1054 change_euid(newcred, euip);
1057 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
1058 change_ruid(newcred, ruip);
1061 if (suid != (uid_t)-1 && oldcred->cr_svuid != suid) {
1062 change_svuid(newcred, suid);
1065 proc_set_cred(p, newcred);
1068 racct_proc_ucred_changed(p, oldcred, newcred);
1085 * setresgid(rgid, egid, sgid) is like setregid except control over the saved
1088 #ifndef _SYS_SYSPROTO_H_
1089 struct setresgid_args {
1097 sys_setresgid(register struct thread *td, struct setresgid_args *uap)
1099 struct proc *p = td->td_proc;
1100 struct ucred *newcred, *oldcred;
1101 gid_t egid, rgid, sgid;
1107 AUDIT_ARG_EGID(egid);
1108 AUDIT_ARG_RGID(rgid);
1109 AUDIT_ARG_SGID(sgid);
1112 oldcred = crcopysafe(p, newcred);
1115 error = mac_cred_check_setresgid(oldcred, rgid, egid, sgid);
1120 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
1121 rgid != oldcred->cr_svgid &&
1122 rgid != oldcred->cr_groups[0]) ||
1123 (egid != (gid_t)-1 && egid != oldcred->cr_rgid &&
1124 egid != oldcred->cr_svgid &&
1125 egid != oldcred->cr_groups[0]) ||
1126 (sgid != (gid_t)-1 && sgid != oldcred->cr_rgid &&
1127 sgid != oldcred->cr_svgid &&
1128 sgid != oldcred->cr_groups[0])) &&
1129 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESGID, 0)) != 0)
1132 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
1133 change_egid(newcred, egid);
1136 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
1137 change_rgid(newcred, rgid);
1140 if (sgid != (gid_t)-1 && oldcred->cr_svgid != sgid) {
1141 change_svgid(newcred, sgid);
1144 proc_set_cred(p, newcred);
1155 #ifndef _SYS_SYSPROTO_H_
1156 struct getresuid_args {
1164 sys_getresuid(register struct thread *td, struct getresuid_args *uap)
1167 int error1 = 0, error2 = 0, error3 = 0;
1169 cred = td->td_ucred;
1171 error1 = copyout(&cred->cr_ruid,
1172 uap->ruid, sizeof(cred->cr_ruid));
1174 error2 = copyout(&cred->cr_uid,
1175 uap->euid, sizeof(cred->cr_uid));
1177 error3 = copyout(&cred->cr_svuid,
1178 uap->suid, sizeof(cred->cr_svuid));
1179 return (error1 ? error1 : error2 ? error2 : error3);
1182 #ifndef _SYS_SYSPROTO_H_
1183 struct getresgid_args {
1191 sys_getresgid(register struct thread *td, struct getresgid_args *uap)
1194 int error1 = 0, error2 = 0, error3 = 0;
1196 cred = td->td_ucred;
1198 error1 = copyout(&cred->cr_rgid,
1199 uap->rgid, sizeof(cred->cr_rgid));
1201 error2 = copyout(&cred->cr_groups[0],
1202 uap->egid, sizeof(cred->cr_groups[0]));
1204 error3 = copyout(&cred->cr_svgid,
1205 uap->sgid, sizeof(cred->cr_svgid));
1206 return (error1 ? error1 : error2 ? error2 : error3);
1209 #ifndef _SYS_SYSPROTO_H_
1210 struct issetugid_args {
1216 sys_issetugid(register struct thread *td, struct issetugid_args *uap)
1218 struct proc *p = td->td_proc;
1221 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
1222 * we use P_SUGID because we consider changing the owners as
1223 * "tainting" as well.
1224 * This is significant for procs that start as root and "become"
1225 * a user without an exec - programs cannot know *everything*
1226 * that libc *might* have put in their data segment.
1228 td->td_retval[0] = (p->p_flag & P_SUGID) ? 1 : 0;
1233 sys___setugid(struct thread *td, struct __setugid_args *uap)
1239 switch (uap->flag) {
1242 p->p_flag &= ~P_SUGID;
1247 p->p_flag |= P_SUGID;
1253 #else /* !REGRESSION */
1256 #endif /* REGRESSION */
1260 * Check if gid is a member of the group set.
1263 groupmember(gid_t gid, struct ucred *cred)
1269 if (cred->cr_groups[0] == gid)
1273 * If gid was not our primary group, perform a binary search
1274 * of the supplemental groups. This is possible because we
1275 * sort the groups in crsetgroups().
1278 h = cred->cr_ngroups;
1280 m = l + ((h - l) / 2);
1281 if (cred->cr_groups[m] < gid)
1286 if ((l < cred->cr_ngroups) && (cred->cr_groups[l] == gid))
1293 * Test the active securelevel against a given level. securelevel_gt()
1294 * implements (securelevel > level). securelevel_ge() implements
1295 * (securelevel >= level). Note that the logic is inverted -- these
1296 * functions return EPERM on "success" and 0 on "failure".
1298 * Due to care taken when setting the securelevel, we know that no jail will
1299 * be less secure that its parent (or the physical system), so it is sufficient
1300 * to test the current jail only.
1302 * XXXRW: Possibly since this has to do with privilege, it should move to
1306 securelevel_gt(struct ucred *cr, int level)
1309 return (cr->cr_prison->pr_securelevel > level ? EPERM : 0);
1313 securelevel_ge(struct ucred *cr, int level)
1316 return (cr->cr_prison->pr_securelevel >= level ? EPERM : 0);
1320 * 'see_other_uids' determines whether or not visibility of processes
1321 * and sockets with credentials holding different real uids is possible
1322 * using a variety of system MIBs.
1323 * XXX: data declarations should be together near the beginning of the file.
1325 static int see_other_uids = 1;
1326 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_uids, CTLFLAG_RW,
1328 "Unprivileged processes may see subjects/objects with different real uid");
1331 * Determine if u1 "can see" the subject specified by u2, according to the
1332 * 'see_other_uids' policy.
1333 * Returns: 0 for permitted, ESRCH otherwise
1335 * References: *u1 and *u2 must not change during the call
1336 * u1 may equal u2, in which case only one reference is required
1339 cr_canseeotheruids(struct ucred *u1, struct ucred *u2)
1342 if (!see_other_uids && u1->cr_ruid != u2->cr_ruid) {
1343 if (priv_check_cred(u1, PRIV_SEEOTHERUIDS, 0) != 0)
1350 * 'see_other_gids' determines whether or not visibility of processes
1351 * and sockets with credentials holding different real gids is possible
1352 * using a variety of system MIBs.
1353 * XXX: data declarations should be together near the beginning of the file.
1355 static int see_other_gids = 1;
1356 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_gids, CTLFLAG_RW,
1358 "Unprivileged processes may see subjects/objects with different real gid");
1361 * Determine if u1 can "see" the subject specified by u2, according to the
1362 * 'see_other_gids' policy.
1363 * Returns: 0 for permitted, ESRCH otherwise
1365 * References: *u1 and *u2 must not change during the call
1366 * u1 may equal u2, in which case only one reference is required
1369 cr_canseeothergids(struct ucred *u1, struct ucred *u2)
1373 if (!see_other_gids) {
1375 for (i = 0; i < u1->cr_ngroups; i++) {
1376 if (groupmember(u1->cr_groups[i], u2))
1382 if (priv_check_cred(u1, PRIV_SEEOTHERGIDS, 0) != 0)
1390 * Determine if u1 "can see" the subject specified by u2.
1391 * Returns: 0 for permitted, an errno value otherwise
1393 * References: *u1 and *u2 must not change during the call
1394 * u1 may equal u2, in which case only one reference is required
1397 cr_cansee(struct ucred *u1, struct ucred *u2)
1401 if ((error = prison_check(u1, u2)))
1404 if ((error = mac_cred_check_visible(u1, u2)))
1407 if ((error = cr_canseeotheruids(u1, u2)))
1409 if ((error = cr_canseeothergids(u1, u2)))
1415 * Determine if td "can see" the subject specified by p.
1416 * Returns: 0 for permitted, an errno value otherwise
1417 * Locks: Sufficient locks to protect p->p_ucred must be held. td really
1418 * should be curthread.
1419 * References: td and p must be valid for the lifetime of the call
1422 p_cansee(struct thread *td, struct proc *p)
1425 /* Wrap cr_cansee() for all functionality. */
1426 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1427 PROC_LOCK_ASSERT(p, MA_OWNED);
1428 return (cr_cansee(td->td_ucred, p->p_ucred));
1432 * 'conservative_signals' prevents the delivery of a broad class of
1433 * signals by unprivileged processes to processes that have changed their
1434 * credentials since the last invocation of execve(). This can prevent
1435 * the leakage of cached information or retained privileges as a result
1436 * of a common class of signal-related vulnerabilities. However, this
1437 * may interfere with some applications that expect to be able to
1438 * deliver these signals to peer processes after having given up
1441 static int conservative_signals = 1;
1442 SYSCTL_INT(_security_bsd, OID_AUTO, conservative_signals, CTLFLAG_RW,
1443 &conservative_signals, 0, "Unprivileged processes prevented from "
1444 "sending certain signals to processes whose credentials have changed");
1446 * Determine whether cred may deliver the specified signal to proc.
1447 * Returns: 0 for permitted, an errno value otherwise.
1448 * Locks: A lock must be held for proc.
1449 * References: cred and proc must be valid for the lifetime of the call.
1452 cr_cansignal(struct ucred *cred, struct proc *proc, int signum)
1456 PROC_LOCK_ASSERT(proc, MA_OWNED);
1458 * Jail semantics limit the scope of signalling to proc in the
1459 * same jail as cred, if cred is in jail.
1461 error = prison_check(cred, proc->p_ucred);
1465 if ((error = mac_proc_check_signal(cred, proc, signum)))
1468 if ((error = cr_canseeotheruids(cred, proc->p_ucred)))
1470 if ((error = cr_canseeothergids(cred, proc->p_ucred)))
1474 * UNIX signal semantics depend on the status of the P_SUGID
1475 * bit on the target process. If the bit is set, then additional
1476 * restrictions are placed on the set of available signals.
1478 if (conservative_signals && (proc->p_flag & P_SUGID)) {
1493 * Generally, permit job and terminal control
1498 /* Not permitted without privilege. */
1499 error = priv_check_cred(cred, PRIV_SIGNAL_SUGID, 0);
1506 * Generally, the target credential's ruid or svuid must match the
1507 * subject credential's ruid or euid.
1509 if (cred->cr_ruid != proc->p_ucred->cr_ruid &&
1510 cred->cr_ruid != proc->p_ucred->cr_svuid &&
1511 cred->cr_uid != proc->p_ucred->cr_ruid &&
1512 cred->cr_uid != proc->p_ucred->cr_svuid) {
1513 error = priv_check_cred(cred, PRIV_SIGNAL_DIFFCRED, 0);
1522 * Determine whether td may deliver the specified signal to p.
1523 * Returns: 0 for permitted, an errno value otherwise
1524 * Locks: Sufficient locks to protect various components of td and p
1525 * must be held. td must be curthread, and a lock must be
1527 * References: td and p must be valid for the lifetime of the call
1530 p_cansignal(struct thread *td, struct proc *p, int signum)
1533 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1534 PROC_LOCK_ASSERT(p, MA_OWNED);
1535 if (td->td_proc == p)
1539 * UNIX signalling semantics require that processes in the same
1540 * session always be able to deliver SIGCONT to one another,
1541 * overriding the remaining protections.
1543 /* XXX: This will require an additional lock of some sort. */
1544 if (signum == SIGCONT && td->td_proc->p_session == p->p_session)
1547 * Some compat layers use SIGTHR and higher signals for
1548 * communication between different kernel threads of the same
1549 * process, so that they expect that it's always possible to
1550 * deliver them, even for suid applications where cr_cansignal() can
1551 * deny such ability for security consideration. It should be
1552 * pretty safe to do since the only way to create two processes
1553 * with the same p_leader is via rfork(2).
1555 if (td->td_proc->p_leader != NULL && signum >= SIGTHR &&
1556 signum < SIGTHR + 4 && td->td_proc->p_leader == p->p_leader)
1559 return (cr_cansignal(td->td_ucred, p, signum));
1563 * Determine whether td may reschedule p.
1564 * Returns: 0 for permitted, an errno value otherwise
1565 * Locks: Sufficient locks to protect various components of td and p
1566 * must be held. td must be curthread, and a lock must
1568 * References: td and p must be valid for the lifetime of the call
1571 p_cansched(struct thread *td, struct proc *p)
1575 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1576 PROC_LOCK_ASSERT(p, MA_OWNED);
1577 if (td->td_proc == p)
1579 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1582 if ((error = mac_proc_check_sched(td->td_ucred, p)))
1585 if ((error = cr_canseeotheruids(td->td_ucred, p->p_ucred)))
1587 if ((error = cr_canseeothergids(td->td_ucred, p->p_ucred)))
1589 if (td->td_ucred->cr_ruid != p->p_ucred->cr_ruid &&
1590 td->td_ucred->cr_uid != p->p_ucred->cr_ruid) {
1591 error = priv_check(td, PRIV_SCHED_DIFFCRED);
1599 * The 'unprivileged_proc_debug' flag may be used to disable a variety of
1600 * unprivileged inter-process debugging services, including some procfs
1601 * functionality, ptrace(), and ktrace(). In the past, inter-process
1602 * debugging has been involved in a variety of security problems, and sites
1603 * not requiring the service might choose to disable it when hardening
1606 * XXX: Should modifying and reading this variable require locking?
1607 * XXX: data declarations should be together near the beginning of the file.
1609 static int unprivileged_proc_debug = 1;
1610 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_proc_debug, CTLFLAG_RW,
1611 &unprivileged_proc_debug, 0,
1612 "Unprivileged processes may use process debugging facilities");
1615 * Determine whether td may debug p.
1616 * Returns: 0 for permitted, an errno value otherwise
1617 * Locks: Sufficient locks to protect various components of td and p
1618 * must be held. td must be curthread, and a lock must
1620 * References: td and p must be valid for the lifetime of the call
1623 p_candebug(struct thread *td, struct proc *p)
1625 int credentialchanged, error, grpsubset, i, uidsubset;
1627 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1628 PROC_LOCK_ASSERT(p, MA_OWNED);
1629 if (!unprivileged_proc_debug) {
1630 error = priv_check(td, PRIV_DEBUG_UNPRIV);
1634 if (td->td_proc == p)
1636 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1639 if ((error = mac_proc_check_debug(td->td_ucred, p)))
1642 if ((error = cr_canseeotheruids(td->td_ucred, p->p_ucred)))
1644 if ((error = cr_canseeothergids(td->td_ucred, p->p_ucred)))
1648 * Is p's group set a subset of td's effective group set? This
1649 * includes p's egid, group access list, rgid, and svgid.
1652 for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
1653 if (!groupmember(p->p_ucred->cr_groups[i], td->td_ucred)) {
1658 grpsubset = grpsubset &&
1659 groupmember(p->p_ucred->cr_rgid, td->td_ucred) &&
1660 groupmember(p->p_ucred->cr_svgid, td->td_ucred);
1663 * Are the uids present in p's credential equal to td's
1664 * effective uid? This includes p's euid, svuid, and ruid.
1666 uidsubset = (td->td_ucred->cr_uid == p->p_ucred->cr_uid &&
1667 td->td_ucred->cr_uid == p->p_ucred->cr_svuid &&
1668 td->td_ucred->cr_uid == p->p_ucred->cr_ruid);
1671 * Has the credential of the process changed since the last exec()?
1673 credentialchanged = (p->p_flag & P_SUGID);
1676 * If p's gids aren't a subset, or the uids aren't a subset,
1677 * or the credential has changed, require appropriate privilege
1678 * for td to debug p.
1680 if (!grpsubset || !uidsubset) {
1681 error = priv_check(td, PRIV_DEBUG_DIFFCRED);
1686 if (credentialchanged) {
1687 error = priv_check(td, PRIV_DEBUG_SUGID);
1692 /* Can't trace init when securelevel > 0. */
1693 if (p == initproc) {
1694 error = securelevel_gt(td->td_ucred, 0);
1700 * Can't trace a process that's currently exec'ing.
1702 * XXX: Note, this is not a security policy decision, it's a
1703 * basic correctness/functionality decision. Therefore, this check
1704 * should be moved to the caller's of p_candebug().
1706 if ((p->p_flag & P_INEXEC) != 0)
1709 /* Denied explicitely */
1710 if ((p->p_flag2 & P2_NOTRACE) != 0) {
1711 error = priv_check(td, PRIV_DEBUG_DENIED);
1720 * Determine whether the subject represented by cred can "see" a socket.
1721 * Returns: 0 for permitted, ENOENT otherwise.
1724 cr_canseesocket(struct ucred *cred, struct socket *so)
1728 error = prison_check(cred, so->so_cred);
1732 error = mac_socket_check_visible(cred, so);
1736 if (cr_canseeotheruids(cred, so->so_cred))
1738 if (cr_canseeothergids(cred, so->so_cred))
1745 * Determine whether td can wait for the exit of p.
1746 * Returns: 0 for permitted, an errno value otherwise
1747 * Locks: Sufficient locks to protect various components of td and p
1748 * must be held. td must be curthread, and a lock must
1750 * References: td and p must be valid for the lifetime of the call
1754 p_canwait(struct thread *td, struct proc *p)
1758 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1759 PROC_LOCK_ASSERT(p, MA_OWNED);
1760 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1763 if ((error = mac_proc_check_wait(td->td_ucred, p)))
1767 /* XXXMAC: This could have odd effects on some shells. */
1768 if ((error = cr_canseeotheruids(td->td_ucred, p->p_ucred)))
1776 * Allocate a zeroed cred structure.
1781 register struct ucred *cr;
1783 cr = malloc(sizeof(*cr), M_CRED, M_WAITOK | M_ZERO);
1784 refcount_init(&cr->cr_ref, 1);
1786 audit_cred_init(cr);
1791 cr->cr_groups = cr->cr_smallgroups;
1793 sizeof(cr->cr_smallgroups) / sizeof(cr->cr_smallgroups[0]);
1798 * Claim another reference to a ucred structure.
1801 crhold(struct ucred *cr)
1804 refcount_acquire(&cr->cr_ref);
1809 * Free a cred structure. Throws away space when ref count gets to 0.
1812 crfree(struct ucred *cr)
1815 KASSERT(cr->cr_ref > 0, ("bad ucred refcount: %d", cr->cr_ref));
1816 KASSERT(cr->cr_ref != 0xdeadc0de, ("dangling reference to ucred"));
1817 if (refcount_release(&cr->cr_ref)) {
1819 * Some callers of crget(), such as nfs_statfs(),
1820 * allocate a temporary credential, but don't
1821 * allocate a uidinfo structure.
1823 if (cr->cr_uidinfo != NULL)
1824 uifree(cr->cr_uidinfo);
1825 if (cr->cr_ruidinfo != NULL)
1826 uifree(cr->cr_ruidinfo);
1828 * Free a prison, if any.
1830 if (cr->cr_prison != NULL)
1831 prison_free(cr->cr_prison);
1832 if (cr->cr_loginclass != NULL)
1833 loginclass_free(cr->cr_loginclass);
1835 audit_cred_destroy(cr);
1838 mac_cred_destroy(cr);
1840 if (cr->cr_groups != cr->cr_smallgroups)
1841 free(cr->cr_groups, M_CRED);
1847 * Copy a ucred's contents from a template. Does not block.
1850 crcopy(struct ucred *dest, struct ucred *src)
1853 KASSERT(dest->cr_ref == 1, ("crcopy of shared ucred"));
1854 bcopy(&src->cr_startcopy, &dest->cr_startcopy,
1855 (unsigned)((caddr_t)&src->cr_endcopy -
1856 (caddr_t)&src->cr_startcopy));
1857 crsetgroups(dest, src->cr_ngroups, src->cr_groups);
1858 uihold(dest->cr_uidinfo);
1859 uihold(dest->cr_ruidinfo);
1860 prison_hold(dest->cr_prison);
1861 loginclass_hold(dest->cr_loginclass);
1863 audit_cred_copy(src, dest);
1866 mac_cred_copy(src, dest);
1871 * Dup cred struct to a new held one.
1874 crdup(struct ucred *cr)
1876 struct ucred *newcr;
1884 * Fill in a struct xucred based on a struct ucred.
1887 cru2x(struct ucred *cr, struct xucred *xcr)
1891 bzero(xcr, sizeof(*xcr));
1892 xcr->cr_version = XUCRED_VERSION;
1893 xcr->cr_uid = cr->cr_uid;
1895 ngroups = MIN(cr->cr_ngroups, XU_NGROUPS);
1896 xcr->cr_ngroups = ngroups;
1897 bcopy(cr->cr_groups, xcr->cr_groups,
1898 ngroups * sizeof(*cr->cr_groups));
1902 * Set initial process credentials.
1903 * Callers are responsible for providing the reference for provided credentials.
1906 proc_set_cred_init(struct proc *p, struct ucred *newcred)
1909 p->p_ucred = newcred;
1913 * Change process credentials.
1914 * Callers are responsible for providing the reference for passed credentials
1915 * and for freeing old ones.
1917 * Process has to be locked except when it does not have credentials (as it
1918 * should not be visible just yet) or when newcred is NULL (as this can be
1919 * only used when the process is about to be freed, at which point it should
1920 * not be visible anymore).
1923 proc_set_cred(struct proc *p, struct ucred *newcred)
1925 struct ucred *oldcred;
1927 MPASS(p->p_ucred != NULL);
1928 if (newcred == NULL)
1929 MPASS(p->p_state == PRS_ZOMBIE);
1931 PROC_LOCK_ASSERT(p, MA_OWNED);
1933 oldcred = p->p_ucred;
1934 p->p_ucred = newcred;
1935 if (newcred != NULL)
1941 crcopysafe(struct proc *p, struct ucred *cr)
1943 struct ucred *oldcred;
1946 PROC_LOCK_ASSERT(p, MA_OWNED);
1948 oldcred = p->p_ucred;
1949 while (cr->cr_agroups < oldcred->cr_agroups) {
1950 groups = oldcred->cr_agroups;
1952 crextend(cr, groups);
1954 oldcred = p->p_ucred;
1956 crcopy(cr, oldcred);
1962 * Extend the passed in credential to hold n items.
1965 crextend(struct ucred *cr, int n)
1970 if (n <= cr->cr_agroups)
1974 * We extend by 2 each time since we're using a power of two
1975 * allocator until we need enough groups to fill a page.
1976 * Once we're allocating multiple pages, only allocate as many
1977 * as we actually need. The case of processes needing a
1978 * non-power of two number of pages seems more likely than
1979 * a real world process that adds thousands of groups one at a
1982 if ( n < PAGE_SIZE / sizeof(gid_t) ) {
1983 if (cr->cr_agroups == 0)
1984 cnt = MINALLOCSIZE / sizeof(gid_t);
1986 cnt = cr->cr_agroups * 2;
1991 cnt = roundup2(n, PAGE_SIZE / sizeof(gid_t));
1993 /* Free the old array. */
1994 if (cr->cr_groups != cr->cr_smallgroups)
1995 free(cr->cr_groups, M_CRED);
1997 cr->cr_groups = malloc(cnt * sizeof(gid_t), M_CRED, M_WAITOK | M_ZERO);
1998 cr->cr_agroups = cnt;
2002 * Copy groups in to a credential, preserving any necessary invariants.
2003 * Currently this includes the sorting of all supplemental gids.
2004 * crextend() must have been called before hand to ensure sufficient
2005 * space is available.
2008 crsetgroups_locked(struct ucred *cr, int ngrp, gid_t *groups)
2014 KASSERT(cr->cr_agroups >= ngrp, ("cr_ngroups is too small"));
2016 bcopy(groups, cr->cr_groups, ngrp * sizeof(gid_t));
2017 cr->cr_ngroups = ngrp;
2020 * Sort all groups except cr_groups[0] to allow groupmember to
2021 * perform a binary search.
2023 * XXX: If large numbers of groups become common this should
2024 * be replaced with shell sort like linux uses or possibly
2027 for (i = 2; i < ngrp; i++) {
2028 g = cr->cr_groups[i];
2029 for (j = i-1; j >= 1 && g < cr->cr_groups[j]; j--)
2030 cr->cr_groups[j + 1] = cr->cr_groups[j];
2031 cr->cr_groups[j + 1] = g;
2036 * Copy groups in to a credential after expanding it if required.
2037 * Truncate the list to (ngroups_max + 1) if it is too large.
2040 crsetgroups(struct ucred *cr, int ngrp, gid_t *groups)
2043 if (ngrp > ngroups_max + 1)
2044 ngrp = ngroups_max + 1;
2047 crsetgroups_locked(cr, ngrp, groups);
2051 * Get login name, if available.
2053 #ifndef _SYS_SYSPROTO_H_
2054 struct getlogin_args {
2061 sys_getlogin(struct thread *td, struct getlogin_args *uap)
2063 char login[MAXLOGNAME];
2064 struct proc *p = td->td_proc;
2067 if (uap->namelen > MAXLOGNAME)
2068 uap->namelen = MAXLOGNAME;
2070 SESS_LOCK(p->p_session);
2071 len = strlcpy(login, p->p_session->s_login, uap->namelen) + 1;
2072 SESS_UNLOCK(p->p_session);
2074 if (len > uap->namelen)
2076 return (copyout(login, uap->namebuf, len));
2082 #ifndef _SYS_SYSPROTO_H_
2083 struct setlogin_args {
2089 sys_setlogin(struct thread *td, struct setlogin_args *uap)
2091 struct proc *p = td->td_proc;
2093 char logintmp[MAXLOGNAME];
2095 CTASSERT(sizeof(p->p_session->s_login) >= sizeof(logintmp));
2097 error = priv_check(td, PRIV_PROC_SETLOGIN);
2100 error = copyinstr(uap->namebuf, logintmp, sizeof(logintmp), NULL);
2102 if (error == ENAMETOOLONG)
2106 AUDIT_ARG_LOGIN(logintmp);
2108 SESS_LOCK(p->p_session);
2109 strcpy(p->p_session->s_login, logintmp);
2110 SESS_UNLOCK(p->p_session);
2116 setsugid(struct proc *p)
2119 PROC_LOCK_ASSERT(p, MA_OWNED);
2120 p->p_flag |= P_SUGID;
2121 if (!(p->p_pfsflags & PF_ISUGID))
2126 * Change a process's effective uid.
2127 * Side effects: newcred->cr_uid and newcred->cr_uidinfo will be modified.
2128 * References: newcred must be an exclusive credential reference for the
2129 * duration of the call.
2132 change_euid(struct ucred *newcred, struct uidinfo *euip)
2135 newcred->cr_uid = euip->ui_uid;
2137 uifree(newcred->cr_uidinfo);
2138 newcred->cr_uidinfo = euip;
2142 * Change a process's effective gid.
2143 * Side effects: newcred->cr_gid will be modified.
2144 * References: newcred must be an exclusive credential reference for the
2145 * duration of the call.
2148 change_egid(struct ucred *newcred, gid_t egid)
2151 newcred->cr_groups[0] = egid;
2155 * Change a process's real uid.
2156 * Side effects: newcred->cr_ruid will be updated, newcred->cr_ruidinfo
2157 * will be updated, and the old and new cr_ruidinfo proc
2158 * counts will be updated.
2159 * References: newcred must be an exclusive credential reference for the
2160 * duration of the call.
2163 change_ruid(struct ucred *newcred, struct uidinfo *ruip)
2166 (void)chgproccnt(newcred->cr_ruidinfo, -1, 0);
2167 newcred->cr_ruid = ruip->ui_uid;
2169 uifree(newcred->cr_ruidinfo);
2170 newcred->cr_ruidinfo = ruip;
2171 (void)chgproccnt(newcred->cr_ruidinfo, 1, 0);
2175 * Change a process's real gid.
2176 * Side effects: newcred->cr_rgid will be updated.
2177 * References: newcred must be an exclusive credential reference for the
2178 * duration of the call.
2181 change_rgid(struct ucred *newcred, gid_t rgid)
2184 newcred->cr_rgid = rgid;
2188 * Change a process's saved uid.
2189 * Side effects: newcred->cr_svuid will be updated.
2190 * References: newcred must be an exclusive credential reference for the
2191 * duration of the call.
2194 change_svuid(struct ucred *newcred, uid_t svuid)
2197 newcred->cr_svuid = svuid;
2201 * Change a process's saved gid.
2202 * Side effects: newcred->cr_svgid will be updated.
2203 * References: newcred must be an exclusive credential reference for the
2204 * duration of the call.
2207 change_svgid(struct ucred *newcred, gid_t svgid)
2210 newcred->cr_svgid = svgid;