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 nessesary 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)
108 td->td_retval[1] = p->p_pptr->p_pid;
114 #ifndef _SYS_SYSPROTO_H_
115 struct getppid_args {
121 sys_getppid(struct thread *td, struct getppid_args *uap)
123 struct proc *p = td->td_proc;
126 td->td_retval[0] = p->p_pptr->p_pid;
132 * Get process group ID; note that POSIX getpgrp takes no parameter.
134 #ifndef _SYS_SYSPROTO_H_
135 struct getpgrp_args {
140 sys_getpgrp(struct thread *td, struct getpgrp_args *uap)
142 struct proc *p = td->td_proc;
145 td->td_retval[0] = p->p_pgrp->pg_id;
150 /* Get an arbitary pid's process group id */
151 #ifndef _SYS_SYSPROTO_H_
152 struct getpgid_args {
157 sys_getpgid(struct thread *td, struct getpgid_args *uap)
169 error = p_cansee(td, p);
175 td->td_retval[0] = p->p_pgrp->pg_id;
181 * Get an arbitary pid's session id.
183 #ifndef _SYS_SYSPROTO_H_
189 sys_getsid(struct thread *td, struct getsid_args *uap)
201 error = p_cansee(td, p);
207 td->td_retval[0] = p->p_session->s_sid;
212 #ifndef _SYS_SYSPROTO_H_
219 sys_getuid(struct thread *td, struct getuid_args *uap)
222 td->td_retval[0] = td->td_ucred->cr_ruid;
223 #if defined(COMPAT_43)
224 td->td_retval[1] = td->td_ucred->cr_uid;
229 #ifndef _SYS_SYSPROTO_H_
230 struct geteuid_args {
236 sys_geteuid(struct thread *td, struct geteuid_args *uap)
239 td->td_retval[0] = td->td_ucred->cr_uid;
243 #ifndef _SYS_SYSPROTO_H_
250 sys_getgid(struct thread *td, struct getgid_args *uap)
253 td->td_retval[0] = td->td_ucred->cr_rgid;
254 #if defined(COMPAT_43)
255 td->td_retval[1] = td->td_ucred->cr_groups[0];
261 * Get effective group ID. The "egid" is groups[0], and could be obtained
262 * via getgroups. This syscall exists because it is somewhat painful to do
263 * correctly in a library function.
265 #ifndef _SYS_SYSPROTO_H_
266 struct getegid_args {
272 sys_getegid(struct thread *td, struct getegid_args *uap)
275 td->td_retval[0] = td->td_ucred->cr_groups[0];
279 #ifndef _SYS_SYSPROTO_H_
280 struct getgroups_args {
286 sys_getgroups(struct thread *td, register struct getgroups_args *uap)
292 if (uap->gidsetsize < td->td_ucred->cr_ngroups) {
293 if (uap->gidsetsize == 0)
298 ngrp = td->td_ucred->cr_ngroups;
299 groups = malloc(ngrp * sizeof(*groups), M_TEMP, M_WAITOK);
300 error = kern_getgroups(td, &ngrp, groups);
303 if (uap->gidsetsize > 0)
304 error = copyout(groups, uap->gidset, ngrp * sizeof(gid_t));
306 td->td_retval[0] = ngrp;
308 free(groups, M_TEMP);
313 kern_getgroups(struct thread *td, u_int *ngrp, gid_t *groups)
319 *ngrp = cred->cr_ngroups;
322 if (*ngrp < cred->cr_ngroups)
324 *ngrp = cred->cr_ngroups;
325 bcopy(cred->cr_groups, groups, *ngrp * sizeof(gid_t));
329 #ifndef _SYS_SYSPROTO_H_
336 sys_setsid(register struct thread *td, struct setsid_args *uap)
340 struct proc *p = td->td_proc;
341 struct pgrp *newpgrp;
342 struct session *newsess;
347 newpgrp = malloc(sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
348 newsess = malloc(sizeof(struct session), M_SESSION, M_WAITOK | M_ZERO);
350 sx_xlock(&proctree_lock);
352 if (p->p_pgid == p->p_pid || (pgrp = pgfind(p->p_pid)) != NULL) {
357 (void)enterpgrp(p, p->p_pid, newpgrp, newsess);
358 td->td_retval[0] = p->p_pid;
363 sx_xunlock(&proctree_lock);
366 free(newpgrp, M_PGRP);
368 free(newsess, M_SESSION);
374 * set process group (setpgid/old setpgrp)
376 * caller does setpgid(targpid, targpgid)
378 * pid must be caller or child of caller (ESRCH)
380 * pid must be in same session (EPERM)
381 * pid can't have done an exec (EACCES)
383 * there must exist some pid in same session having pgid (EPERM)
384 * pid must not be session leader (EPERM)
386 #ifndef _SYS_SYSPROTO_H_
387 struct setpgid_args {
388 int pid; /* target process id */
389 int pgid; /* target pgrp id */
394 sys_setpgid(struct thread *td, register struct setpgid_args *uap)
396 struct proc *curp = td->td_proc;
397 register struct proc *targp; /* target process */
398 register struct pgrp *pgrp; /* target pgrp */
400 struct pgrp *newpgrp;
407 newpgrp = malloc(sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
409 sx_xlock(&proctree_lock);
410 if (uap->pid != 0 && uap->pid != curp->p_pid) {
411 if ((targp = pfind(uap->pid)) == NULL) {
415 if (!inferior(targp)) {
420 if ((error = p_cansee(td, targp))) {
424 if (targp->p_pgrp == NULL ||
425 targp->p_session != curp->p_session) {
430 if (targp->p_flag & P_EXEC) {
438 if (SESS_LEADER(targp)) {
443 uap->pgid = targp->p_pid;
444 if ((pgrp = pgfind(uap->pgid)) == NULL) {
445 if (uap->pgid == targp->p_pid) {
446 error = enterpgrp(targp, uap->pgid, newpgrp,
453 if (pgrp == targp->p_pgrp) {
457 if (pgrp->pg_id != targp->p_pid &&
458 pgrp->pg_session != curp->p_session) {
464 error = enterthispgrp(targp, pgrp);
467 sx_xunlock(&proctree_lock);
468 KASSERT((error == 0) || (newpgrp != NULL),
469 ("setpgid failed and newpgrp is NULL"));
471 free(newpgrp, M_PGRP);
476 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
477 * compatible. It says that setting the uid/gid to euid/egid is a special
478 * case of "appropriate privilege". Once the rules are expanded out, this
479 * basically means that setuid(nnn) sets all three id's, in all permitted
480 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
481 * does not set the saved id - this is dangerous for traditional BSD
482 * programs. For this reason, we *really* do not want to set
483 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
485 #define POSIX_APPENDIX_B_4_2_2
487 #ifndef _SYS_SYSPROTO_H_
494 sys_setuid(struct thread *td, struct setuid_args *uap)
496 struct proc *p = td->td_proc;
497 struct ucred *newcred, *oldcred;
508 * Copy credentials so other references do not see our changes.
510 oldcred = crcopysafe(p, newcred);
513 error = mac_cred_check_setuid(oldcred, uid);
519 * See if we have "permission" by POSIX 1003.1 rules.
521 * Note that setuid(geteuid()) is a special case of
522 * "appropriate privileges" in appendix B.4.2.2. We need
523 * to use this clause to be compatible with traditional BSD
524 * semantics. Basically, it means that "setuid(xx)" sets all
525 * three id's (assuming you have privs).
527 * Notes on the logic. We do things in three steps.
528 * 1: We determine if the euid is going to change, and do EPERM
529 * right away. We unconditionally change the euid later if this
530 * test is satisfied, simplifying that part of the logic.
531 * 2: We determine if the real and/or saved uids are going to
532 * change. Determined by compile options.
533 * 3: Change euid last. (after tests in #2 for "appropriate privs")
535 if (uid != oldcred->cr_ruid && /* allow setuid(getuid()) */
536 #ifdef _POSIX_SAVED_IDS
537 uid != oldcred->cr_svuid && /* allow setuid(saved gid) */
539 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
540 uid != oldcred->cr_uid && /* allow setuid(geteuid()) */
542 (error = priv_check_cred(oldcred, PRIV_CRED_SETUID, 0)) != 0)
545 #ifdef _POSIX_SAVED_IDS
547 * Do we have "appropriate privileges" (are we root or uid == euid)
548 * If so, we are changing the real uid and/or saved uid.
551 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
552 uid == oldcred->cr_uid ||
554 /* We are using privs. */
555 priv_check_cred(oldcred, PRIV_CRED_SETUID, 0) == 0)
559 * Set the real uid and transfer proc count to new user.
561 if (uid != oldcred->cr_ruid) {
562 change_ruid(newcred, uip);
568 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
569 * the security of seteuid() depends on it. B.4.2.2 says it
570 * is important that we should do this.
572 if (uid != oldcred->cr_svuid) {
573 change_svuid(newcred, uid);
579 * In all permitted cases, we are changing the euid.
581 if (uid != oldcred->cr_uid) {
582 change_euid(newcred, uip);
585 p->p_ucred = newcred;
588 racct_proc_ucred_changed(p, oldcred, newcred);
601 #ifndef _SYS_SYSPROTO_H_
602 struct seteuid_args {
608 sys_seteuid(struct thread *td, struct seteuid_args *uap)
610 struct proc *p = td->td_proc;
611 struct ucred *newcred, *oldcred;
613 struct uidinfo *euip;
617 AUDIT_ARG_EUID(euid);
622 * Copy credentials so other references do not see our changes.
624 oldcred = crcopysafe(p, newcred);
627 error = mac_cred_check_seteuid(oldcred, euid);
632 if (euid != oldcred->cr_ruid && /* allow seteuid(getuid()) */
633 euid != oldcred->cr_svuid && /* allow seteuid(saved uid) */
634 (error = priv_check_cred(oldcred, PRIV_CRED_SETEUID, 0)) != 0)
638 * Everything's okay, do it.
640 if (oldcred->cr_uid != euid) {
641 change_euid(newcred, euip);
644 p->p_ucred = newcred;
657 #ifndef _SYS_SYSPROTO_H_
664 sys_setgid(struct thread *td, struct setgid_args *uap)
666 struct proc *p = td->td_proc;
667 struct ucred *newcred, *oldcred;
675 oldcred = crcopysafe(p, newcred);
678 error = mac_cred_check_setgid(oldcred, gid);
684 * See if we have "permission" by POSIX 1003.1 rules.
686 * Note that setgid(getegid()) is a special case of
687 * "appropriate privileges" in appendix B.4.2.2. We need
688 * to use this clause to be compatible with traditional BSD
689 * semantics. Basically, it means that "setgid(xx)" sets all
690 * three id's (assuming you have privs).
692 * For notes on the logic here, see setuid() above.
694 if (gid != oldcred->cr_rgid && /* allow setgid(getgid()) */
695 #ifdef _POSIX_SAVED_IDS
696 gid != oldcred->cr_svgid && /* allow setgid(saved gid) */
698 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
699 gid != oldcred->cr_groups[0] && /* allow setgid(getegid()) */
701 (error = priv_check_cred(oldcred, PRIV_CRED_SETGID, 0)) != 0)
704 #ifdef _POSIX_SAVED_IDS
706 * Do we have "appropriate privileges" (are we root or gid == egid)
707 * If so, we are changing the real uid and saved gid.
710 #ifdef POSIX_APPENDIX_B_4_2_2 /* use the clause from B.4.2.2 */
711 gid == oldcred->cr_groups[0] ||
713 /* We are using privs. */
714 priv_check_cred(oldcred, PRIV_CRED_SETGID, 0) == 0)
720 if (oldcred->cr_rgid != gid) {
721 change_rgid(newcred, gid);
727 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
728 * the security of setegid() depends on it. B.4.2.2 says it
729 * is important that we should do this.
731 if (oldcred->cr_svgid != gid) {
732 change_svgid(newcred, gid);
737 * In all cases permitted cases, we are changing the egid.
738 * Copy credentials so other references do not see our changes.
740 if (oldcred->cr_groups[0] != gid) {
741 change_egid(newcred, gid);
744 p->p_ucred = newcred;
755 #ifndef _SYS_SYSPROTO_H_
756 struct setegid_args {
762 sys_setegid(struct thread *td, struct setegid_args *uap)
764 struct proc *p = td->td_proc;
765 struct ucred *newcred, *oldcred;
770 AUDIT_ARG_EGID(egid);
773 oldcred = crcopysafe(p, newcred);
776 error = mac_cred_check_setegid(oldcred, egid);
781 if (egid != oldcred->cr_rgid && /* allow setegid(getgid()) */
782 egid != oldcred->cr_svgid && /* allow setegid(saved gid) */
783 (error = priv_check_cred(oldcred, PRIV_CRED_SETEGID, 0)) != 0)
786 if (oldcred->cr_groups[0] != egid) {
787 change_egid(newcred, egid);
790 p->p_ucred = newcred;
801 #ifndef _SYS_SYSPROTO_H_
802 struct setgroups_args {
809 sys_setgroups(struct thread *td, struct setgroups_args *uap)
811 gid_t *groups = NULL;
814 if (uap->gidsetsize > ngroups_max + 1)
816 groups = malloc(uap->gidsetsize * sizeof(gid_t), M_TEMP, M_WAITOK);
817 error = copyin(uap->gidset, groups, uap->gidsetsize * sizeof(gid_t));
820 error = kern_setgroups(td, uap->gidsetsize, groups);
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 if (ngrp > ngroups_max + 1)
835 AUDIT_ARG_GROUPSET(groups, ngrp);
837 crextend(newcred, ngrp);
839 oldcred = crcopysafe(p, newcred);
842 error = mac_cred_check_setgroups(oldcred, ngrp, groups);
847 error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0);
853 * setgroups(0, NULL) is a legitimate way of clearing the
854 * groups vector on non-BSD systems (which generally do not
855 * have the egid in the groups[0]). We risk security holes
856 * when running non-BSD software if we do not do the same.
858 newcred->cr_ngroups = 1;
860 crsetgroups_locked(newcred, ngrp, groups);
863 p->p_ucred = newcred;
874 #ifndef _SYS_SYSPROTO_H_
875 struct setreuid_args {
882 sys_setreuid(register struct thread *td, struct setreuid_args *uap)
884 struct proc *p = td->td_proc;
885 struct ucred *newcred, *oldcred;
887 struct uidinfo *euip, *ruip;
892 AUDIT_ARG_EUID(euid);
893 AUDIT_ARG_RUID(ruid);
898 oldcred = crcopysafe(p, newcred);
901 error = mac_cred_check_setreuid(oldcred, ruid, euid);
906 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
907 ruid != oldcred->cr_svuid) ||
908 (euid != (uid_t)-1 && euid != oldcred->cr_uid &&
909 euid != oldcred->cr_ruid && euid != oldcred->cr_svuid)) &&
910 (error = priv_check_cred(oldcred, PRIV_CRED_SETREUID, 0)) != 0)
913 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
914 change_euid(newcred, euip);
917 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
918 change_ruid(newcred, ruip);
921 if ((ruid != (uid_t)-1 || newcred->cr_uid != newcred->cr_ruid) &&
922 newcred->cr_svuid != newcred->cr_uid) {
923 change_svuid(newcred, newcred->cr_uid);
926 p->p_ucred = newcred;
929 racct_proc_ucred_changed(p, oldcred, newcred);
944 #ifndef _SYS_SYSPROTO_H_
945 struct setregid_args {
952 sys_setregid(register struct thread *td, struct setregid_args *uap)
954 struct proc *p = td->td_proc;
955 struct ucred *newcred, *oldcred;
961 AUDIT_ARG_EGID(egid);
962 AUDIT_ARG_RGID(rgid);
965 oldcred = crcopysafe(p, newcred);
968 error = mac_cred_check_setregid(oldcred, rgid, egid);
973 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
974 rgid != oldcred->cr_svgid) ||
975 (egid != (gid_t)-1 && egid != oldcred->cr_groups[0] &&
976 egid != oldcred->cr_rgid && egid != oldcred->cr_svgid)) &&
977 (error = priv_check_cred(oldcred, PRIV_CRED_SETREGID, 0)) != 0)
980 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
981 change_egid(newcred, egid);
984 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
985 change_rgid(newcred, rgid);
988 if ((rgid != (gid_t)-1 || newcred->cr_groups[0] != newcred->cr_rgid) &&
989 newcred->cr_svgid != newcred->cr_groups[0]) {
990 change_svgid(newcred, newcred->cr_groups[0]);
993 p->p_ucred = newcred;
1005 * setresuid(ruid, euid, suid) is like setreuid except control over the saved
1008 #ifndef _SYS_SYSPROTO_H_
1009 struct setresuid_args {
1017 sys_setresuid(register struct thread *td, struct setresuid_args *uap)
1019 struct proc *p = td->td_proc;
1020 struct ucred *newcred, *oldcred;
1021 uid_t euid, ruid, suid;
1022 struct uidinfo *euip, *ruip;
1028 AUDIT_ARG_EUID(euid);
1029 AUDIT_ARG_RUID(ruid);
1030 AUDIT_ARG_SUID(suid);
1032 euip = uifind(euid);
1033 ruip = uifind(ruid);
1035 oldcred = crcopysafe(p, newcred);
1038 error = mac_cred_check_setresuid(oldcred, ruid, euid, suid);
1043 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
1044 ruid != oldcred->cr_svuid &&
1045 ruid != oldcred->cr_uid) ||
1046 (euid != (uid_t)-1 && euid != oldcred->cr_ruid &&
1047 euid != oldcred->cr_svuid &&
1048 euid != oldcred->cr_uid) ||
1049 (suid != (uid_t)-1 && suid != oldcred->cr_ruid &&
1050 suid != oldcred->cr_svuid &&
1051 suid != oldcred->cr_uid)) &&
1052 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESUID, 0)) != 0)
1055 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
1056 change_euid(newcred, euip);
1059 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
1060 change_ruid(newcred, ruip);
1063 if (suid != (uid_t)-1 && oldcred->cr_svuid != suid) {
1064 change_svuid(newcred, suid);
1067 p->p_ucred = newcred;
1070 racct_proc_ucred_changed(p, oldcred, newcred);
1087 * setresgid(rgid, egid, sgid) is like setregid except control over the saved
1090 #ifndef _SYS_SYSPROTO_H_
1091 struct setresgid_args {
1099 sys_setresgid(register struct thread *td, struct setresgid_args *uap)
1101 struct proc *p = td->td_proc;
1102 struct ucred *newcred, *oldcred;
1103 gid_t egid, rgid, sgid;
1109 AUDIT_ARG_EGID(egid);
1110 AUDIT_ARG_RGID(rgid);
1111 AUDIT_ARG_SGID(sgid);
1114 oldcred = crcopysafe(p, newcred);
1117 error = mac_cred_check_setresgid(oldcred, rgid, egid, sgid);
1122 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
1123 rgid != oldcred->cr_svgid &&
1124 rgid != oldcred->cr_groups[0]) ||
1125 (egid != (gid_t)-1 && egid != oldcred->cr_rgid &&
1126 egid != oldcred->cr_svgid &&
1127 egid != oldcred->cr_groups[0]) ||
1128 (sgid != (gid_t)-1 && sgid != oldcred->cr_rgid &&
1129 sgid != oldcred->cr_svgid &&
1130 sgid != oldcred->cr_groups[0])) &&
1131 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESGID, 0)) != 0)
1134 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
1135 change_egid(newcred, egid);
1138 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
1139 change_rgid(newcred, rgid);
1142 if (sgid != (gid_t)-1 && oldcred->cr_svgid != sgid) {
1143 change_svgid(newcred, sgid);
1146 p->p_ucred = newcred;
1157 #ifndef _SYS_SYSPROTO_H_
1158 struct getresuid_args {
1166 sys_getresuid(register struct thread *td, struct getresuid_args *uap)
1169 int error1 = 0, error2 = 0, error3 = 0;
1171 cred = td->td_ucred;
1173 error1 = copyout(&cred->cr_ruid,
1174 uap->ruid, sizeof(cred->cr_ruid));
1176 error2 = copyout(&cred->cr_uid,
1177 uap->euid, sizeof(cred->cr_uid));
1179 error3 = copyout(&cred->cr_svuid,
1180 uap->suid, sizeof(cred->cr_svuid));
1181 return (error1 ? error1 : error2 ? error2 : error3);
1184 #ifndef _SYS_SYSPROTO_H_
1185 struct getresgid_args {
1193 sys_getresgid(register struct thread *td, struct getresgid_args *uap)
1196 int error1 = 0, error2 = 0, error3 = 0;
1198 cred = td->td_ucred;
1200 error1 = copyout(&cred->cr_rgid,
1201 uap->rgid, sizeof(cred->cr_rgid));
1203 error2 = copyout(&cred->cr_groups[0],
1204 uap->egid, sizeof(cred->cr_groups[0]));
1206 error3 = copyout(&cred->cr_svgid,
1207 uap->sgid, sizeof(cred->cr_svgid));
1208 return (error1 ? error1 : error2 ? error2 : error3);
1211 #ifndef _SYS_SYSPROTO_H_
1212 struct issetugid_args {
1218 sys_issetugid(register struct thread *td, struct issetugid_args *uap)
1220 struct proc *p = td->td_proc;
1223 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
1224 * we use P_SUGID because we consider changing the owners as
1225 * "tainting" as well.
1226 * This is significant for procs that start as root and "become"
1227 * a user without an exec - programs cannot know *everything*
1228 * that libc *might* have put in their data segment.
1231 td->td_retval[0] = (p->p_flag & P_SUGID) ? 1 : 0;
1237 sys___setugid(struct thread *td, struct __setugid_args *uap)
1243 switch (uap->flag) {
1246 p->p_flag &= ~P_SUGID;
1251 p->p_flag |= P_SUGID;
1257 #else /* !REGRESSION */
1260 #endif /* REGRESSION */
1264 * Check if gid is a member of the group set.
1267 groupmember(gid_t gid, struct ucred *cred)
1273 if (cred->cr_groups[0] == gid)
1277 * If gid was not our primary group, perform a binary search
1278 * of the supplemental groups. This is possible because we
1279 * sort the groups in crsetgroups().
1282 h = cred->cr_ngroups;
1284 m = l + ((h - l) / 2);
1285 if (cred->cr_groups[m] < gid)
1290 if ((l < cred->cr_ngroups) && (cred->cr_groups[l] == gid))
1297 * Test the active securelevel against a given level. securelevel_gt()
1298 * implements (securelevel > level). securelevel_ge() implements
1299 * (securelevel >= level). Note that the logic is inverted -- these
1300 * functions return EPERM on "success" and 0 on "failure".
1302 * Due to care taken when setting the securelevel, we know that no jail will
1303 * be less secure that its parent (or the physical system), so it is sufficient
1304 * to test the current jail only.
1306 * XXXRW: Possibly since this has to do with privilege, it should move to
1310 securelevel_gt(struct ucred *cr, int level)
1313 return (cr->cr_prison->pr_securelevel > level ? EPERM : 0);
1317 securelevel_ge(struct ucred *cr, int level)
1320 return (cr->cr_prison->pr_securelevel >= level ? EPERM : 0);
1324 * 'see_other_uids' determines whether or not visibility of processes
1325 * and sockets with credentials holding different real uids is possible
1326 * using a variety of system MIBs.
1327 * XXX: data declarations should be together near the beginning of the file.
1329 static int see_other_uids = 1;
1330 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_uids, CTLFLAG_RW,
1332 "Unprivileged processes may see subjects/objects with different real uid");
1335 * Determine if u1 "can see" the subject specified by u2, according to the
1336 * 'see_other_uids' policy.
1337 * Returns: 0 for permitted, ESRCH otherwise
1339 * References: *u1 and *u2 must not change during the call
1340 * u1 may equal u2, in which case only one reference is required
1343 cr_seeotheruids(struct ucred *u1, struct ucred *u2)
1346 if (!see_other_uids && u1->cr_ruid != u2->cr_ruid) {
1347 if (priv_check_cred(u1, PRIV_SEEOTHERUIDS, 0) != 0)
1354 * 'see_other_gids' determines whether or not visibility of processes
1355 * and sockets with credentials holding different real gids is possible
1356 * using a variety of system MIBs.
1357 * XXX: data declarations should be together near the beginning of the file.
1359 static int see_other_gids = 1;
1360 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_gids, CTLFLAG_RW,
1362 "Unprivileged processes may see subjects/objects with different real gid");
1365 * Determine if u1 can "see" the subject specified by u2, according to the
1366 * 'see_other_gids' policy.
1367 * Returns: 0 for permitted, ESRCH otherwise
1369 * References: *u1 and *u2 must not change during the call
1370 * u1 may equal u2, in which case only one reference is required
1373 cr_seeothergids(struct ucred *u1, struct ucred *u2)
1377 if (!see_other_gids) {
1379 for (i = 0; i < u1->cr_ngroups; i++) {
1380 if (groupmember(u1->cr_groups[i], u2))
1386 if (priv_check_cred(u1, PRIV_SEEOTHERGIDS, 0) != 0)
1394 * Determine if u1 "can see" the subject specified by u2.
1395 * Returns: 0 for permitted, an errno value otherwise
1397 * References: *u1 and *u2 must not change during the call
1398 * u1 may equal u2, in which case only one reference is required
1401 cr_cansee(struct ucred *u1, struct ucred *u2)
1405 if ((error = prison_check(u1, u2)))
1408 if ((error = mac_cred_check_visible(u1, u2)))
1411 if ((error = cr_seeotheruids(u1, u2)))
1413 if ((error = cr_seeothergids(u1, u2)))
1419 * Determine if td "can see" the subject specified by p.
1420 * Returns: 0 for permitted, an errno value otherwise
1421 * Locks: Sufficient locks to protect p->p_ucred must be held. td really
1422 * should be curthread.
1423 * References: td and p must be valid for the lifetime of the call
1426 p_cansee(struct thread *td, struct proc *p)
1429 /* Wrap cr_cansee() for all functionality. */
1430 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1431 PROC_LOCK_ASSERT(p, MA_OWNED);
1432 return (cr_cansee(td->td_ucred, p->p_ucred));
1436 * 'conservative_signals' prevents the delivery of a broad class of
1437 * signals by unprivileged processes to processes that have changed their
1438 * credentials since the last invocation of execve(). This can prevent
1439 * the leakage of cached information or retained privileges as a result
1440 * of a common class of signal-related vulnerabilities. However, this
1441 * may interfere with some applications that expect to be able to
1442 * deliver these signals to peer processes after having given up
1445 static int conservative_signals = 1;
1446 SYSCTL_INT(_security_bsd, OID_AUTO, conservative_signals, CTLFLAG_RW,
1447 &conservative_signals, 0, "Unprivileged processes prevented from "
1448 "sending certain signals to processes whose credentials have changed");
1450 * Determine whether cred may deliver the specified signal to proc.
1451 * Returns: 0 for permitted, an errno value otherwise.
1452 * Locks: A lock must be held for proc.
1453 * References: cred and proc must be valid for the lifetime of the call.
1456 cr_cansignal(struct ucred *cred, struct proc *proc, int signum)
1460 PROC_LOCK_ASSERT(proc, MA_OWNED);
1462 * Jail semantics limit the scope of signalling to proc in the
1463 * same jail as cred, if cred is in jail.
1465 error = prison_check(cred, proc->p_ucred);
1469 if ((error = mac_proc_check_signal(cred, proc, signum)))
1472 if ((error = cr_seeotheruids(cred, proc->p_ucred)))
1474 if ((error = cr_seeothergids(cred, proc->p_ucred)))
1478 * UNIX signal semantics depend on the status of the P_SUGID
1479 * bit on the target process. If the bit is set, then additional
1480 * restrictions are placed on the set of available signals.
1482 if (conservative_signals && (proc->p_flag & P_SUGID)) {
1497 * Generally, permit job and terminal control
1502 /* Not permitted without privilege. */
1503 error = priv_check_cred(cred, PRIV_SIGNAL_SUGID, 0);
1510 * Generally, the target credential's ruid or svuid must match the
1511 * subject credential's ruid or euid.
1513 if (cred->cr_ruid != proc->p_ucred->cr_ruid &&
1514 cred->cr_ruid != proc->p_ucred->cr_svuid &&
1515 cred->cr_uid != proc->p_ucred->cr_ruid &&
1516 cred->cr_uid != proc->p_ucred->cr_svuid) {
1517 error = priv_check_cred(cred, PRIV_SIGNAL_DIFFCRED, 0);
1526 * Determine whether td may deliver the specified signal to p.
1527 * Returns: 0 for permitted, an errno value otherwise
1528 * Locks: Sufficient locks to protect various components of td and p
1529 * must be held. td must be curthread, and a lock must be
1531 * References: td and p must be valid for the lifetime of the call
1534 p_cansignal(struct thread *td, struct proc *p, int signum)
1537 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1538 PROC_LOCK_ASSERT(p, MA_OWNED);
1539 if (td->td_proc == p)
1543 * UNIX signalling semantics require that processes in the same
1544 * session always be able to deliver SIGCONT to one another,
1545 * overriding the remaining protections.
1547 /* XXX: This will require an additional lock of some sort. */
1548 if (signum == SIGCONT && td->td_proc->p_session == p->p_session)
1551 * Some compat layers use SIGTHR and higher signals for
1552 * communication between different kernel threads of the same
1553 * process, so that they expect that it's always possible to
1554 * deliver them, even for suid applications where cr_cansignal() can
1555 * deny such ability for security consideration. It should be
1556 * pretty safe to do since the only way to create two processes
1557 * with the same p_leader is via rfork(2).
1559 if (td->td_proc->p_leader != NULL && signum >= SIGTHR &&
1560 signum < SIGTHR + 4 && td->td_proc->p_leader == p->p_leader)
1563 return (cr_cansignal(td->td_ucred, p, signum));
1567 * Determine whether td may reschedule p.
1568 * Returns: 0 for permitted, an errno value otherwise
1569 * Locks: Sufficient locks to protect various components of td and p
1570 * must be held. td must be curthread, and a lock must
1572 * References: td and p must be valid for the lifetime of the call
1575 p_cansched(struct thread *td, struct proc *p)
1579 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1580 PROC_LOCK_ASSERT(p, MA_OWNED);
1581 if (td->td_proc == p)
1583 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1586 if ((error = mac_proc_check_sched(td->td_ucred, p)))
1589 if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1591 if ((error = cr_seeothergids(td->td_ucred, p->p_ucred)))
1593 if (td->td_ucred->cr_ruid != p->p_ucred->cr_ruid &&
1594 td->td_ucred->cr_uid != p->p_ucred->cr_ruid) {
1595 error = priv_check(td, PRIV_SCHED_DIFFCRED);
1603 * The 'unprivileged_proc_debug' flag may be used to disable a variety of
1604 * unprivileged inter-process debugging services, including some procfs
1605 * functionality, ptrace(), and ktrace(). In the past, inter-process
1606 * debugging has been involved in a variety of security problems, and sites
1607 * not requiring the service might choose to disable it when hardening
1610 * XXX: Should modifying and reading this variable require locking?
1611 * XXX: data declarations should be together near the beginning of the file.
1613 static int unprivileged_proc_debug = 1;
1614 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_proc_debug, CTLFLAG_RW,
1615 &unprivileged_proc_debug, 0,
1616 "Unprivileged processes may use process debugging facilities");
1619 * Determine whether td may debug p.
1620 * Returns: 0 for permitted, an errno value otherwise
1621 * Locks: Sufficient locks to protect various components of td and p
1622 * must be held. td must be curthread, and a lock must
1624 * References: td and p must be valid for the lifetime of the call
1627 p_candebug(struct thread *td, struct proc *p)
1629 int credentialchanged, error, grpsubset, i, uidsubset;
1631 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1632 PROC_LOCK_ASSERT(p, MA_OWNED);
1633 if (!unprivileged_proc_debug) {
1634 error = priv_check(td, PRIV_DEBUG_UNPRIV);
1638 if (td->td_proc == p)
1640 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1643 if ((error = mac_proc_check_debug(td->td_ucred, p)))
1646 if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1648 if ((error = cr_seeothergids(td->td_ucred, p->p_ucred)))
1652 * Is p's group set a subset of td's effective group set? This
1653 * includes p's egid, group access list, rgid, and svgid.
1656 for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
1657 if (!groupmember(p->p_ucred->cr_groups[i], td->td_ucred)) {
1662 grpsubset = grpsubset &&
1663 groupmember(p->p_ucred->cr_rgid, td->td_ucred) &&
1664 groupmember(p->p_ucred->cr_svgid, td->td_ucred);
1667 * Are the uids present in p's credential equal to td's
1668 * effective uid? This includes p's euid, svuid, and ruid.
1670 uidsubset = (td->td_ucred->cr_uid == p->p_ucred->cr_uid &&
1671 td->td_ucred->cr_uid == p->p_ucred->cr_svuid &&
1672 td->td_ucred->cr_uid == p->p_ucred->cr_ruid);
1675 * Has the credential of the process changed since the last exec()?
1677 credentialchanged = (p->p_flag & P_SUGID);
1680 * If p's gids aren't a subset, or the uids aren't a subset,
1681 * or the credential has changed, require appropriate privilege
1682 * for td to debug p.
1684 if (!grpsubset || !uidsubset) {
1685 error = priv_check(td, PRIV_DEBUG_DIFFCRED);
1690 if (credentialchanged) {
1691 error = priv_check(td, PRIV_DEBUG_SUGID);
1696 /* Can't trace init when securelevel > 0. */
1697 if (p == initproc) {
1698 error = securelevel_gt(td->td_ucred, 0);
1704 * Can't trace a process that's currently exec'ing.
1706 * XXX: Note, this is not a security policy decision, it's a
1707 * basic correctness/functionality decision. Therefore, this check
1708 * should be moved to the caller's of p_candebug().
1710 if ((p->p_flag & P_INEXEC) != 0)
1717 * Determine whether the subject represented by cred can "see" a socket.
1718 * Returns: 0 for permitted, ENOENT otherwise.
1721 cr_canseesocket(struct ucred *cred, struct socket *so)
1725 error = prison_check(cred, so->so_cred);
1729 error = mac_socket_check_visible(cred, so);
1733 if (cr_seeotheruids(cred, so->so_cred))
1735 if (cr_seeothergids(cred, so->so_cred))
1741 #if defined(INET) || defined(INET6)
1743 * Determine whether the subject represented by cred can "see" a socket.
1744 * Returns: 0 for permitted, ENOENT otherwise.
1747 cr_canseeinpcb(struct ucred *cred, struct inpcb *inp)
1751 error = prison_check(cred, inp->inp_cred);
1755 INP_LOCK_ASSERT(inp);
1756 error = mac_inpcb_check_visible(cred, inp);
1760 if (cr_seeotheruids(cred, inp->inp_cred))
1762 if (cr_seeothergids(cred, inp->inp_cred))
1770 * Determine whether td can wait for the exit of p.
1771 * Returns: 0 for permitted, an errno value otherwise
1772 * Locks: Sufficient locks to protect various components of td and p
1773 * must be held. td must be curthread, and a lock must
1775 * References: td and p must be valid for the lifetime of the call
1779 p_canwait(struct thread *td, struct proc *p)
1783 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1784 PROC_LOCK_ASSERT(p, MA_OWNED);
1785 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1788 if ((error = mac_proc_check_wait(td->td_ucred, p)))
1792 /* XXXMAC: This could have odd effects on some shells. */
1793 if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1801 * Allocate a zeroed cred structure.
1806 register struct ucred *cr;
1808 cr = malloc(sizeof(*cr), M_CRED, M_WAITOK | M_ZERO);
1809 refcount_init(&cr->cr_ref, 1);
1811 audit_cred_init(cr);
1816 crextend(cr, XU_NGROUPS);
1821 * Claim another reference to a ucred structure.
1824 crhold(struct ucred *cr)
1827 refcount_acquire(&cr->cr_ref);
1832 * Free a cred structure. Throws away space when ref count gets to 0.
1835 crfree(struct ucred *cr)
1838 KASSERT(cr->cr_ref > 0, ("bad ucred refcount: %d", cr->cr_ref));
1839 KASSERT(cr->cr_ref != 0xdeadc0de, ("dangling reference to ucred"));
1840 if (refcount_release(&cr->cr_ref)) {
1842 * Some callers of crget(), such as nfs_statfs(),
1843 * allocate a temporary credential, but don't
1844 * allocate a uidinfo structure.
1846 if (cr->cr_uidinfo != NULL)
1847 uifree(cr->cr_uidinfo);
1848 if (cr->cr_ruidinfo != NULL)
1849 uifree(cr->cr_ruidinfo);
1851 * Free a prison, if any.
1853 if (cr->cr_prison != NULL)
1854 prison_free(cr->cr_prison);
1855 if (cr->cr_loginclass != NULL)
1856 loginclass_free(cr->cr_loginclass);
1858 audit_cred_destroy(cr);
1861 mac_cred_destroy(cr);
1863 free(cr->cr_groups, M_CRED);
1869 * Check to see if this ucred is shared.
1872 crshared(struct ucred *cr)
1875 return (cr->cr_ref > 1);
1879 * Copy a ucred's contents from a template. Does not block.
1882 crcopy(struct ucred *dest, struct ucred *src)
1885 KASSERT(crshared(dest) == 0, ("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 * small routine to swap a thread's current ucred for the correct one taken
1938 cred_update_thread(struct thread *td)
1944 cred = td->td_ucred;
1946 td->td_ucred = crhold(p->p_ucred);
1953 crcopysafe(struct proc *p, struct ucred *cr)
1955 struct ucred *oldcred;
1958 PROC_LOCK_ASSERT(p, MA_OWNED);
1960 oldcred = p->p_ucred;
1961 while (cr->cr_agroups < oldcred->cr_agroups) {
1962 groups = oldcred->cr_agroups;
1964 crextend(cr, groups);
1966 oldcred = p->p_ucred;
1968 crcopy(cr, oldcred);
1974 * Extend the passed in credential to hold n items.
1977 crextend(struct ucred *cr, int n)
1982 if (n <= cr->cr_agroups)
1986 * We extend by 2 each time since we're using a power of two
1987 * allocator until we need enough groups to fill a page.
1988 * Once we're allocating multiple pages, only allocate as many
1989 * as we actually need. The case of processes needing a
1990 * non-power of two number of pages seems more likely than
1991 * a real world process that adds thousands of groups one at a
1994 if ( n < PAGE_SIZE / sizeof(gid_t) ) {
1995 if (cr->cr_agroups == 0)
1996 cnt = MINALLOCSIZE / sizeof(gid_t);
1998 cnt = cr->cr_agroups * 2;
2003 cnt = roundup2(n, PAGE_SIZE / sizeof(gid_t));
2005 /* Free the old array. */
2007 free(cr->cr_groups, M_CRED);
2009 cr->cr_groups = malloc(cnt * sizeof(gid_t), M_CRED, M_WAITOK | M_ZERO);
2010 cr->cr_agroups = cnt;
2014 * Copy groups in to a credential, preserving any necessary invariants.
2015 * Currently this includes the sorting of all supplemental gids.
2016 * crextend() must have been called before hand to ensure sufficient
2017 * space is available.
2020 crsetgroups_locked(struct ucred *cr, int ngrp, gid_t *groups)
2026 KASSERT(cr->cr_agroups >= ngrp, ("cr_ngroups is too small"));
2028 bcopy(groups, cr->cr_groups, ngrp * sizeof(gid_t));
2029 cr->cr_ngroups = ngrp;
2032 * Sort all groups except cr_groups[0] to allow groupmember to
2033 * perform a binary search.
2035 * XXX: If large numbers of groups become common this should
2036 * be replaced with shell sort like linux uses or possibly
2039 for (i = 2; i < ngrp; i++) {
2040 g = cr->cr_groups[i];
2041 for (j = i-1; j >= 1 && g < cr->cr_groups[j]; j--)
2042 cr->cr_groups[j + 1] = cr->cr_groups[j];
2043 cr->cr_groups[j + 1] = g;
2048 * Copy groups in to a credential after expanding it if required.
2049 * Truncate the list to (ngroups_max + 1) if it is too large.
2052 crsetgroups(struct ucred *cr, int ngrp, gid_t *groups)
2055 if (ngrp > ngroups_max + 1)
2056 ngrp = ngroups_max + 1;
2059 crsetgroups_locked(cr, ngrp, groups);
2063 * Get login name, if available.
2065 #ifndef _SYS_SYSPROTO_H_
2066 struct getlogin_args {
2073 sys_getlogin(struct thread *td, struct getlogin_args *uap)
2075 char login[MAXLOGNAME];
2076 struct proc *p = td->td_proc;
2079 if (uap->namelen > MAXLOGNAME)
2080 uap->namelen = MAXLOGNAME;
2082 SESS_LOCK(p->p_session);
2083 len = strlcpy(login, p->p_session->s_login, uap->namelen) + 1;
2084 SESS_UNLOCK(p->p_session);
2086 if (len > uap->namelen)
2088 return (copyout(login, uap->namebuf, len));
2094 #ifndef _SYS_SYSPROTO_H_
2095 struct setlogin_args {
2101 sys_setlogin(struct thread *td, struct setlogin_args *uap)
2103 struct proc *p = td->td_proc;
2105 char logintmp[MAXLOGNAME];
2107 CTASSERT(sizeof(p->p_session->s_login) >= sizeof(logintmp));
2109 error = priv_check(td, PRIV_PROC_SETLOGIN);
2112 error = copyinstr(uap->namebuf, logintmp, sizeof(logintmp), NULL);
2114 if (error == ENAMETOOLONG)
2119 SESS_LOCK(p->p_session);
2120 strcpy(p->p_session->s_login, logintmp);
2121 SESS_UNLOCK(p->p_session);
2127 setsugid(struct proc *p)
2130 PROC_LOCK_ASSERT(p, MA_OWNED);
2131 p->p_flag |= P_SUGID;
2132 if (!(p->p_pfsflags & PF_ISUGID))
2137 * Change a process's effective uid.
2138 * Side effects: newcred->cr_uid and newcred->cr_uidinfo will be modified.
2139 * References: newcred must be an exclusive credential reference for the
2140 * duration of the call.
2143 change_euid(struct ucred *newcred, struct uidinfo *euip)
2146 newcred->cr_uid = euip->ui_uid;
2148 uifree(newcred->cr_uidinfo);
2149 newcred->cr_uidinfo = euip;
2153 * Change a process's effective gid.
2154 * Side effects: newcred->cr_gid will be modified.
2155 * References: newcred must be an exclusive credential reference for the
2156 * duration of the call.
2159 change_egid(struct ucred *newcred, gid_t egid)
2162 newcred->cr_groups[0] = egid;
2166 * Change a process's real uid.
2167 * Side effects: newcred->cr_ruid will be updated, newcred->cr_ruidinfo
2168 * will be updated, and the old and new cr_ruidinfo proc
2169 * counts will be updated.
2170 * References: newcred must be an exclusive credential reference for the
2171 * duration of the call.
2174 change_ruid(struct ucred *newcred, struct uidinfo *ruip)
2177 (void)chgproccnt(newcred->cr_ruidinfo, -1, 0);
2178 newcred->cr_ruid = ruip->ui_uid;
2180 uifree(newcred->cr_ruidinfo);
2181 newcred->cr_ruidinfo = ruip;
2182 (void)chgproccnt(newcred->cr_ruidinfo, 1, 0);
2186 * Change a process's real gid.
2187 * Side effects: newcred->cr_rgid will be updated.
2188 * References: newcred must be an exclusive credential reference for the
2189 * duration of the call.
2192 change_rgid(struct ucred *newcred, gid_t rgid)
2195 newcred->cr_rgid = rgid;
2199 * Change a process's saved uid.
2200 * Side effects: newcred->cr_svuid will be updated.
2201 * References: newcred must be an exclusive credential reference for the
2202 * duration of the call.
2205 change_svuid(struct ucred *newcred, uid_t svuid)
2208 newcred->cr_svuid = svuid;
2212 * Change a process's saved gid.
2213 * Side effects: newcred->cr_svgid will be updated.
2214 * References: newcred must be an exclusive credential reference for the
2215 * duration of the call.
2218 change_svgid(struct ucred *newcred, gid_t svgid)
2221 newcred->cr_svgid = svgid;