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 crextend(struct ucred *cr, int n);
92 static void crsetgroups_locked(struct ucred *cr, int ngrp,
95 #ifndef _SYS_SYSPROTO_H_
102 sys_getpid(struct thread *td, struct getpid_args *uap)
104 struct proc *p = td->td_proc;
106 td->td_retval[0] = p->p_pid;
107 #if defined(COMPAT_43)
109 td->td_retval[1] = p->p_pptr->p_pid;
115 #ifndef _SYS_SYSPROTO_H_
116 struct getppid_args {
122 sys_getppid(struct thread *td, struct getppid_args *uap)
124 struct proc *p = td->td_proc;
127 td->td_retval[0] = p->p_pptr->p_pid;
133 * Get process group ID; note that POSIX getpgrp takes no parameter.
135 #ifndef _SYS_SYSPROTO_H_
136 struct getpgrp_args {
141 sys_getpgrp(struct thread *td, struct getpgrp_args *uap)
143 struct proc *p = td->td_proc;
146 td->td_retval[0] = p->p_pgrp->pg_id;
151 /* Get an arbitary pid's process group id */
152 #ifndef _SYS_SYSPROTO_H_
153 struct getpgid_args {
158 sys_getpgid(struct thread *td, struct getpgid_args *uap)
170 error = p_cansee(td, p);
176 td->td_retval[0] = p->p_pgrp->pg_id;
182 * Get an arbitary pid's session id.
184 #ifndef _SYS_SYSPROTO_H_
190 sys_getsid(struct thread *td, struct getsid_args *uap)
202 error = p_cansee(td, p);
208 td->td_retval[0] = p->p_session->s_sid;
213 #ifndef _SYS_SYSPROTO_H_
220 sys_getuid(struct thread *td, struct getuid_args *uap)
223 td->td_retval[0] = td->td_ucred->cr_ruid;
224 #if defined(COMPAT_43)
225 td->td_retval[1] = td->td_ucred->cr_uid;
230 #ifndef _SYS_SYSPROTO_H_
231 struct geteuid_args {
237 sys_geteuid(struct thread *td, struct geteuid_args *uap)
240 td->td_retval[0] = td->td_ucred->cr_uid;
244 #ifndef _SYS_SYSPROTO_H_
251 sys_getgid(struct thread *td, struct getgid_args *uap)
254 td->td_retval[0] = td->td_ucred->cr_rgid;
255 #if defined(COMPAT_43)
256 td->td_retval[1] = td->td_ucred->cr_groups[0];
262 * Get effective group ID. The "egid" is groups[0], and could be obtained
263 * via getgroups. This syscall exists because it is somewhat painful to do
264 * correctly in a library function.
266 #ifndef _SYS_SYSPROTO_H_
267 struct getegid_args {
273 sys_getegid(struct thread *td, struct getegid_args *uap)
276 td->td_retval[0] = td->td_ucred->cr_groups[0];
280 #ifndef _SYS_SYSPROTO_H_
281 struct getgroups_args {
287 sys_getgroups(struct thread *td, register struct getgroups_args *uap)
293 if (uap->gidsetsize < td->td_ucred->cr_ngroups) {
294 if (uap->gidsetsize == 0)
299 ngrp = td->td_ucred->cr_ngroups;
300 groups = malloc(ngrp * sizeof(*groups), M_TEMP, M_WAITOK);
301 error = kern_getgroups(td, &ngrp, groups);
304 if (uap->gidsetsize > 0)
305 error = copyout(groups, uap->gidset, ngrp * sizeof(gid_t));
307 td->td_retval[0] = ngrp;
309 free(groups, M_TEMP);
314 kern_getgroups(struct thread *td, u_int *ngrp, gid_t *groups)
320 *ngrp = cred->cr_ngroups;
323 if (*ngrp < cred->cr_ngroups)
325 *ngrp = cred->cr_ngroups;
326 bcopy(cred->cr_groups, groups, *ngrp * sizeof(gid_t));
330 #ifndef _SYS_SYSPROTO_H_
337 sys_setsid(register struct thread *td, struct setsid_args *uap)
341 struct proc *p = td->td_proc;
342 struct pgrp *newpgrp;
343 struct session *newsess;
348 newpgrp = malloc(sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
349 newsess = malloc(sizeof(struct session), M_SESSION, M_WAITOK | M_ZERO);
351 sx_xlock(&proctree_lock);
353 if (p->p_pgid == p->p_pid || (pgrp = pgfind(p->p_pid)) != NULL) {
358 (void)enterpgrp(p, p->p_pid, newpgrp, newsess);
359 td->td_retval[0] = p->p_pid;
364 sx_xunlock(&proctree_lock);
367 free(newpgrp, M_PGRP);
369 free(newsess, M_SESSION);
375 * set process group (setpgid/old setpgrp)
377 * caller does setpgid(targpid, targpgid)
379 * pid must be caller or child of caller (ESRCH)
381 * pid must be in same session (EPERM)
382 * pid can't have done an exec (EACCES)
384 * there must exist some pid in same session having pgid (EPERM)
385 * pid must not be session leader (EPERM)
387 #ifndef _SYS_SYSPROTO_H_
388 struct setpgid_args {
389 int pid; /* target process id */
390 int pgid; /* target pgrp id */
395 sys_setpgid(struct thread *td, register struct setpgid_args *uap)
397 struct proc *curp = td->td_proc;
398 register struct proc *targp; /* target process */
399 register struct pgrp *pgrp; /* target pgrp */
401 struct pgrp *newpgrp;
408 newpgrp = malloc(sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
410 sx_xlock(&proctree_lock);
411 if (uap->pid != 0 && uap->pid != curp->p_pid) {
412 if ((targp = pfind(uap->pid)) == NULL) {
416 if (!inferior(targp)) {
421 if ((error = p_cansee(td, targp))) {
425 if (targp->p_pgrp == NULL ||
426 targp->p_session != curp->p_session) {
431 if (targp->p_flag & P_EXEC) {
439 if (SESS_LEADER(targp)) {
444 uap->pgid = targp->p_pid;
445 if ((pgrp = pgfind(uap->pgid)) == NULL) {
446 if (uap->pgid == targp->p_pid) {
447 error = enterpgrp(targp, uap->pgid, newpgrp,
454 if (pgrp == targp->p_pgrp) {
458 if (pgrp->pg_id != targp->p_pid &&
459 pgrp->pg_session != curp->p_session) {
465 error = enterthispgrp(targp, pgrp);
468 sx_xunlock(&proctree_lock);
469 KASSERT((error == 0) || (newpgrp != NULL),
470 ("setpgid failed and newpgrp is NULL"));
472 free(newpgrp, M_PGRP);
477 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
478 * compatible. It says that setting the uid/gid to euid/egid is a special
479 * case of "appropriate privilege". Once the rules are expanded out, this
480 * basically means that setuid(nnn) sets all three id's, in all permitted
481 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
482 * does not set the saved id - this is dangerous for traditional BSD
483 * programs. For this reason, we *really* do not want to set
484 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
486 #define POSIX_APPENDIX_B_4_2_2
488 #ifndef _SYS_SYSPROTO_H_
495 sys_setuid(struct thread *td, struct setuid_args *uap)
497 struct proc *p = td->td_proc;
498 struct ucred *newcred, *oldcred;
509 * Copy credentials so other references do not see our changes.
511 oldcred = crcopysafe(p, newcred);
514 error = mac_cred_check_setuid(oldcred, uid);
520 * See if we have "permission" by POSIX 1003.1 rules.
522 * Note that setuid(geteuid()) is a special case of
523 * "appropriate privileges" in appendix B.4.2.2. We need
524 * to use this clause to be compatible with traditional BSD
525 * semantics. Basically, it means that "setuid(xx)" sets all
526 * three id's (assuming you have privs).
528 * Notes on the logic. We do things in three steps.
529 * 1: We determine if the euid is going to change, and do EPERM
530 * right away. We unconditionally change the euid later if this
531 * test is satisfied, simplifying that part of the logic.
532 * 2: We determine if the real and/or saved uids are going to
533 * change. Determined by compile options.
534 * 3: Change euid last. (after tests in #2 for "appropriate privs")
536 if (uid != oldcred->cr_ruid && /* allow setuid(getuid()) */
537 #ifdef _POSIX_SAVED_IDS
538 uid != oldcred->cr_svuid && /* allow setuid(saved gid) */
540 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
541 uid != oldcred->cr_uid && /* allow setuid(geteuid()) */
543 (error = priv_check_cred(oldcred, PRIV_CRED_SETUID, 0)) != 0)
546 #ifdef _POSIX_SAVED_IDS
548 * Do we have "appropriate privileges" (are we root or uid == euid)
549 * If so, we are changing the real uid and/or saved uid.
552 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
553 uid == oldcred->cr_uid ||
555 /* We are using privs. */
556 priv_check_cred(oldcred, PRIV_CRED_SETUID, 0) == 0)
560 * Set the real uid and transfer proc count to new user.
562 if (uid != oldcred->cr_ruid) {
563 change_ruid(newcred, uip);
569 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
570 * the security of seteuid() depends on it. B.4.2.2 says it
571 * is important that we should do this.
573 if (uid != oldcred->cr_svuid) {
574 change_svuid(newcred, uid);
580 * In all permitted cases, we are changing the euid.
582 if (uid != oldcred->cr_uid) {
583 change_euid(newcred, uip);
586 p->p_ucred = newcred;
589 racct_proc_ucred_changed(p, oldcred, newcred);
602 #ifndef _SYS_SYSPROTO_H_
603 struct seteuid_args {
609 sys_seteuid(struct thread *td, struct seteuid_args *uap)
611 struct proc *p = td->td_proc;
612 struct ucred *newcred, *oldcred;
614 struct uidinfo *euip;
618 AUDIT_ARG_EUID(euid);
623 * Copy credentials so other references do not see our changes.
625 oldcred = crcopysafe(p, newcred);
628 error = mac_cred_check_seteuid(oldcred, euid);
633 if (euid != oldcred->cr_ruid && /* allow seteuid(getuid()) */
634 euid != oldcred->cr_svuid && /* allow seteuid(saved uid) */
635 (error = priv_check_cred(oldcred, PRIV_CRED_SETEUID, 0)) != 0)
639 * Everything's okay, do it.
641 if (oldcred->cr_uid != euid) {
642 change_euid(newcred, euip);
645 p->p_ucred = newcred;
658 #ifndef _SYS_SYSPROTO_H_
665 sys_setgid(struct thread *td, struct setgid_args *uap)
667 struct proc *p = td->td_proc;
668 struct ucred *newcred, *oldcred;
676 oldcred = crcopysafe(p, newcred);
679 error = mac_cred_check_setgid(oldcred, gid);
685 * See if we have "permission" by POSIX 1003.1 rules.
687 * Note that setgid(getegid()) is a special case of
688 * "appropriate privileges" in appendix B.4.2.2. We need
689 * to use this clause to be compatible with traditional BSD
690 * semantics. Basically, it means that "setgid(xx)" sets all
691 * three id's (assuming you have privs).
693 * For notes on the logic here, see setuid() above.
695 if (gid != oldcred->cr_rgid && /* allow setgid(getgid()) */
696 #ifdef _POSIX_SAVED_IDS
697 gid != oldcred->cr_svgid && /* allow setgid(saved gid) */
699 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
700 gid != oldcred->cr_groups[0] && /* allow setgid(getegid()) */
702 (error = priv_check_cred(oldcred, PRIV_CRED_SETGID, 0)) != 0)
705 #ifdef _POSIX_SAVED_IDS
707 * Do we have "appropriate privileges" (are we root or gid == egid)
708 * If so, we are changing the real uid and saved gid.
711 #ifdef POSIX_APPENDIX_B_4_2_2 /* use the clause from B.4.2.2 */
712 gid == oldcred->cr_groups[0] ||
714 /* We are using privs. */
715 priv_check_cred(oldcred, PRIV_CRED_SETGID, 0) == 0)
721 if (oldcred->cr_rgid != gid) {
722 change_rgid(newcred, gid);
728 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
729 * the security of setegid() depends on it. B.4.2.2 says it
730 * is important that we should do this.
732 if (oldcred->cr_svgid != gid) {
733 change_svgid(newcred, gid);
738 * In all cases permitted cases, we are changing the egid.
739 * Copy credentials so other references do not see our changes.
741 if (oldcred->cr_groups[0] != gid) {
742 change_egid(newcred, gid);
745 p->p_ucred = newcred;
756 #ifndef _SYS_SYSPROTO_H_
757 struct setegid_args {
763 sys_setegid(struct thread *td, struct setegid_args *uap)
765 struct proc *p = td->td_proc;
766 struct ucred *newcred, *oldcred;
771 AUDIT_ARG_EGID(egid);
774 oldcred = crcopysafe(p, newcred);
777 error = mac_cred_check_setegid(oldcred, egid);
782 if (egid != oldcred->cr_rgid && /* allow setegid(getgid()) */
783 egid != oldcred->cr_svgid && /* allow setegid(saved gid) */
784 (error = priv_check_cred(oldcred, PRIV_CRED_SETEGID, 0)) != 0)
787 if (oldcred->cr_groups[0] != egid) {
788 change_egid(newcred, egid);
791 p->p_ucred = newcred;
802 #ifndef _SYS_SYSPROTO_H_
803 struct setgroups_args {
810 sys_setgroups(struct thread *td, struct setgroups_args *uap)
812 gid_t *groups = NULL;
815 if (uap->gidsetsize > ngroups_max + 1)
817 groups = malloc(uap->gidsetsize * sizeof(gid_t), M_TEMP, M_WAITOK);
818 error = copyin(uap->gidset, groups, uap->gidsetsize * sizeof(gid_t));
821 error = kern_setgroups(td, uap->gidsetsize, groups);
823 free(groups, M_TEMP);
828 kern_setgroups(struct thread *td, u_int ngrp, gid_t *groups)
830 struct proc *p = td->td_proc;
831 struct ucred *newcred, *oldcred;
834 if (ngrp > ngroups_max + 1)
836 AUDIT_ARG_GROUPSET(groups, ngrp);
838 crextend(newcred, ngrp);
840 oldcred = crcopysafe(p, newcred);
843 error = mac_cred_check_setgroups(oldcred, ngrp, groups);
848 error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0);
854 * setgroups(0, NULL) is a legitimate way of clearing the
855 * groups vector on non-BSD systems (which generally do not
856 * have the egid in the groups[0]). We risk security holes
857 * when running non-BSD software if we do not do the same.
859 newcred->cr_ngroups = 1;
861 crsetgroups_locked(newcred, ngrp, groups);
864 p->p_ucred = newcred;
875 #ifndef _SYS_SYSPROTO_H_
876 struct setreuid_args {
883 sys_setreuid(register struct thread *td, struct setreuid_args *uap)
885 struct proc *p = td->td_proc;
886 struct ucred *newcred, *oldcred;
888 struct uidinfo *euip, *ruip;
893 AUDIT_ARG_EUID(euid);
894 AUDIT_ARG_RUID(ruid);
899 oldcred = crcopysafe(p, newcred);
902 error = mac_cred_check_setreuid(oldcred, ruid, euid);
907 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
908 ruid != oldcred->cr_svuid) ||
909 (euid != (uid_t)-1 && euid != oldcred->cr_uid &&
910 euid != oldcred->cr_ruid && euid != oldcred->cr_svuid)) &&
911 (error = priv_check_cred(oldcred, PRIV_CRED_SETREUID, 0)) != 0)
914 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
915 change_euid(newcred, euip);
918 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
919 change_ruid(newcred, ruip);
922 if ((ruid != (uid_t)-1 || newcred->cr_uid != newcred->cr_ruid) &&
923 newcred->cr_svuid != newcred->cr_uid) {
924 change_svuid(newcred, newcred->cr_uid);
927 p->p_ucred = newcred;
930 racct_proc_ucred_changed(p, oldcred, newcred);
945 #ifndef _SYS_SYSPROTO_H_
946 struct setregid_args {
953 sys_setregid(register struct thread *td, struct setregid_args *uap)
955 struct proc *p = td->td_proc;
956 struct ucred *newcred, *oldcred;
962 AUDIT_ARG_EGID(egid);
963 AUDIT_ARG_RGID(rgid);
966 oldcred = crcopysafe(p, newcred);
969 error = mac_cred_check_setregid(oldcred, rgid, egid);
974 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
975 rgid != oldcred->cr_svgid) ||
976 (egid != (gid_t)-1 && egid != oldcred->cr_groups[0] &&
977 egid != oldcred->cr_rgid && egid != oldcred->cr_svgid)) &&
978 (error = priv_check_cred(oldcred, PRIV_CRED_SETREGID, 0)) != 0)
981 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
982 change_egid(newcred, egid);
985 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
986 change_rgid(newcred, rgid);
989 if ((rgid != (gid_t)-1 || newcred->cr_groups[0] != newcred->cr_rgid) &&
990 newcred->cr_svgid != newcred->cr_groups[0]) {
991 change_svgid(newcred, newcred->cr_groups[0]);
994 p->p_ucred = newcred;
1006 * setresuid(ruid, euid, suid) is like setreuid except control over the saved
1009 #ifndef _SYS_SYSPROTO_H_
1010 struct setresuid_args {
1018 sys_setresuid(register struct thread *td, struct setresuid_args *uap)
1020 struct proc *p = td->td_proc;
1021 struct ucred *newcred, *oldcred;
1022 uid_t euid, ruid, suid;
1023 struct uidinfo *euip, *ruip;
1029 AUDIT_ARG_EUID(euid);
1030 AUDIT_ARG_RUID(ruid);
1031 AUDIT_ARG_SUID(suid);
1033 euip = uifind(euid);
1034 ruip = uifind(ruid);
1036 oldcred = crcopysafe(p, newcred);
1039 error = mac_cred_check_setresuid(oldcred, ruid, euid, suid);
1044 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
1045 ruid != oldcred->cr_svuid &&
1046 ruid != oldcred->cr_uid) ||
1047 (euid != (uid_t)-1 && euid != oldcred->cr_ruid &&
1048 euid != oldcred->cr_svuid &&
1049 euid != oldcred->cr_uid) ||
1050 (suid != (uid_t)-1 && suid != oldcred->cr_ruid &&
1051 suid != oldcred->cr_svuid &&
1052 suid != oldcred->cr_uid)) &&
1053 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESUID, 0)) != 0)
1056 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
1057 change_euid(newcred, euip);
1060 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
1061 change_ruid(newcred, ruip);
1064 if (suid != (uid_t)-1 && oldcred->cr_svuid != suid) {
1065 change_svuid(newcred, suid);
1068 p->p_ucred = newcred;
1071 racct_proc_ucred_changed(p, oldcred, newcred);
1088 * setresgid(rgid, egid, sgid) is like setregid except control over the saved
1091 #ifndef _SYS_SYSPROTO_H_
1092 struct setresgid_args {
1100 sys_setresgid(register struct thread *td, struct setresgid_args *uap)
1102 struct proc *p = td->td_proc;
1103 struct ucred *newcred, *oldcred;
1104 gid_t egid, rgid, sgid;
1110 AUDIT_ARG_EGID(egid);
1111 AUDIT_ARG_RGID(rgid);
1112 AUDIT_ARG_SGID(sgid);
1115 oldcred = crcopysafe(p, newcred);
1118 error = mac_cred_check_setresgid(oldcred, rgid, egid, sgid);
1123 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
1124 rgid != oldcred->cr_svgid &&
1125 rgid != oldcred->cr_groups[0]) ||
1126 (egid != (gid_t)-1 && egid != oldcred->cr_rgid &&
1127 egid != oldcred->cr_svgid &&
1128 egid != oldcred->cr_groups[0]) ||
1129 (sgid != (gid_t)-1 && sgid != oldcred->cr_rgid &&
1130 sgid != oldcred->cr_svgid &&
1131 sgid != oldcred->cr_groups[0])) &&
1132 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESGID, 0)) != 0)
1135 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
1136 change_egid(newcred, egid);
1139 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
1140 change_rgid(newcred, rgid);
1143 if (sgid != (gid_t)-1 && oldcred->cr_svgid != sgid) {
1144 change_svgid(newcred, sgid);
1147 p->p_ucred = newcred;
1158 #ifndef _SYS_SYSPROTO_H_
1159 struct getresuid_args {
1167 sys_getresuid(register struct thread *td, struct getresuid_args *uap)
1170 int error1 = 0, error2 = 0, error3 = 0;
1172 cred = td->td_ucred;
1174 error1 = copyout(&cred->cr_ruid,
1175 uap->ruid, sizeof(cred->cr_ruid));
1177 error2 = copyout(&cred->cr_uid,
1178 uap->euid, sizeof(cred->cr_uid));
1180 error3 = copyout(&cred->cr_svuid,
1181 uap->suid, sizeof(cred->cr_svuid));
1182 return (error1 ? error1 : error2 ? error2 : error3);
1185 #ifndef _SYS_SYSPROTO_H_
1186 struct getresgid_args {
1194 sys_getresgid(register struct thread *td, struct getresgid_args *uap)
1197 int error1 = 0, error2 = 0, error3 = 0;
1199 cred = td->td_ucred;
1201 error1 = copyout(&cred->cr_rgid,
1202 uap->rgid, sizeof(cred->cr_rgid));
1204 error2 = copyout(&cred->cr_groups[0],
1205 uap->egid, sizeof(cred->cr_groups[0]));
1207 error3 = copyout(&cred->cr_svgid,
1208 uap->sgid, sizeof(cred->cr_svgid));
1209 return (error1 ? error1 : error2 ? error2 : error3);
1212 #ifndef _SYS_SYSPROTO_H_
1213 struct issetugid_args {
1219 sys_issetugid(register struct thread *td, struct issetugid_args *uap)
1221 struct proc *p = td->td_proc;
1224 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
1225 * we use P_SUGID because we consider changing the owners as
1226 * "tainting" as well.
1227 * This is significant for procs that start as root and "become"
1228 * a user without an exec - programs cannot know *everything*
1229 * that libc *might* have put in their data segment.
1232 td->td_retval[0] = (p->p_flag & P_SUGID) ? 1 : 0;
1238 sys___setugid(struct thread *td, struct __setugid_args *uap)
1244 switch (uap->flag) {
1247 p->p_flag &= ~P_SUGID;
1252 p->p_flag |= P_SUGID;
1258 #else /* !REGRESSION */
1261 #endif /* REGRESSION */
1265 * Check if gid is a member of the group set.
1268 groupmember(gid_t gid, struct ucred *cred)
1274 if (cred->cr_groups[0] == gid)
1278 * If gid was not our primary group, perform a binary search
1279 * of the supplemental groups. This is possible because we
1280 * sort the groups in crsetgroups().
1283 h = cred->cr_ngroups;
1285 m = l + ((h - l) / 2);
1286 if (cred->cr_groups[m] < gid)
1291 if ((l < cred->cr_ngroups) && (cred->cr_groups[l] == gid))
1298 * Test the active securelevel against a given level. securelevel_gt()
1299 * implements (securelevel > level). securelevel_ge() implements
1300 * (securelevel >= level). Note that the logic is inverted -- these
1301 * functions return EPERM on "success" and 0 on "failure".
1303 * Due to care taken when setting the securelevel, we know that no jail will
1304 * be less secure that its parent (or the physical system), so it is sufficient
1305 * to test the current jail only.
1307 * XXXRW: Possibly since this has to do with privilege, it should move to
1311 securelevel_gt(struct ucred *cr, int level)
1314 return (cr->cr_prison->pr_securelevel > level ? EPERM : 0);
1318 securelevel_ge(struct ucred *cr, int level)
1321 return (cr->cr_prison->pr_securelevel >= level ? EPERM : 0);
1325 * 'see_other_uids' determines whether or not visibility of processes
1326 * and sockets with credentials holding different real uids is possible
1327 * using a variety of system MIBs.
1328 * XXX: data declarations should be together near the beginning of the file.
1330 static int see_other_uids = 1;
1331 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_uids, CTLFLAG_RW,
1333 "Unprivileged processes may see subjects/objects with different real uid");
1336 * Determine if u1 "can see" the subject specified by u2, according to the
1337 * 'see_other_uids' policy.
1338 * Returns: 0 for permitted, ESRCH otherwise
1340 * References: *u1 and *u2 must not change during the call
1341 * u1 may equal u2, in which case only one reference is required
1344 cr_seeotheruids(struct ucred *u1, struct ucred *u2)
1347 if (!see_other_uids && u1->cr_ruid != u2->cr_ruid) {
1348 if (priv_check_cred(u1, PRIV_SEEOTHERUIDS, 0) != 0)
1355 * 'see_other_gids' determines whether or not visibility of processes
1356 * and sockets with credentials holding different real gids is possible
1357 * using a variety of system MIBs.
1358 * XXX: data declarations should be together near the beginning of the file.
1360 static int see_other_gids = 1;
1361 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_gids, CTLFLAG_RW,
1363 "Unprivileged processes may see subjects/objects with different real gid");
1366 * Determine if u1 can "see" the subject specified by u2, according to the
1367 * 'see_other_gids' policy.
1368 * Returns: 0 for permitted, ESRCH otherwise
1370 * References: *u1 and *u2 must not change during the call
1371 * u1 may equal u2, in which case only one reference is required
1374 cr_seeothergids(struct ucred *u1, struct ucred *u2)
1378 if (!see_other_gids) {
1380 for (i = 0; i < u1->cr_ngroups; i++) {
1381 if (groupmember(u1->cr_groups[i], u2))
1387 if (priv_check_cred(u1, PRIV_SEEOTHERGIDS, 0) != 0)
1395 * Determine if u1 "can see" the subject specified by u2.
1396 * Returns: 0 for permitted, an errno value otherwise
1398 * References: *u1 and *u2 must not change during the call
1399 * u1 may equal u2, in which case only one reference is required
1402 cr_cansee(struct ucred *u1, struct ucred *u2)
1406 if ((error = prison_check(u1, u2)))
1409 if ((error = mac_cred_check_visible(u1, u2)))
1412 if ((error = cr_seeotheruids(u1, u2)))
1414 if ((error = cr_seeothergids(u1, u2)))
1420 * Determine if td "can see" the subject specified by p.
1421 * Returns: 0 for permitted, an errno value otherwise
1422 * Locks: Sufficient locks to protect p->p_ucred must be held. td really
1423 * should be curthread.
1424 * References: td and p must be valid for the lifetime of the call
1427 p_cansee(struct thread *td, struct proc *p)
1430 /* Wrap cr_cansee() for all functionality. */
1431 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1432 PROC_LOCK_ASSERT(p, MA_OWNED);
1433 return (cr_cansee(td->td_ucred, p->p_ucred));
1437 * 'conservative_signals' prevents the delivery of a broad class of
1438 * signals by unprivileged processes to processes that have changed their
1439 * credentials since the last invocation of execve(). This can prevent
1440 * the leakage of cached information or retained privileges as a result
1441 * of a common class of signal-related vulnerabilities. However, this
1442 * may interfere with some applications that expect to be able to
1443 * deliver these signals to peer processes after having given up
1446 static int conservative_signals = 1;
1447 SYSCTL_INT(_security_bsd, OID_AUTO, conservative_signals, CTLFLAG_RW,
1448 &conservative_signals, 0, "Unprivileged processes prevented from "
1449 "sending certain signals to processes whose credentials have changed");
1451 * Determine whether cred may deliver the specified signal to proc.
1452 * Returns: 0 for permitted, an errno value otherwise.
1453 * Locks: A lock must be held for proc.
1454 * References: cred and proc must be valid for the lifetime of the call.
1457 cr_cansignal(struct ucred *cred, struct proc *proc, int signum)
1461 PROC_LOCK_ASSERT(proc, MA_OWNED);
1463 * Jail semantics limit the scope of signalling to proc in the
1464 * same jail as cred, if cred is in jail.
1466 error = prison_check(cred, proc->p_ucred);
1470 if ((error = mac_proc_check_signal(cred, proc, signum)))
1473 if ((error = cr_seeotheruids(cred, proc->p_ucred)))
1475 if ((error = cr_seeothergids(cred, proc->p_ucred)))
1479 * UNIX signal semantics depend on the status of the P_SUGID
1480 * bit on the target process. If the bit is set, then additional
1481 * restrictions are placed on the set of available signals.
1483 if (conservative_signals && (proc->p_flag & P_SUGID)) {
1498 * Generally, permit job and terminal control
1503 /* Not permitted without privilege. */
1504 error = priv_check_cred(cred, PRIV_SIGNAL_SUGID, 0);
1511 * Generally, the target credential's ruid or svuid must match the
1512 * subject credential's ruid or euid.
1514 if (cred->cr_ruid != proc->p_ucred->cr_ruid &&
1515 cred->cr_ruid != proc->p_ucred->cr_svuid &&
1516 cred->cr_uid != proc->p_ucred->cr_ruid &&
1517 cred->cr_uid != proc->p_ucred->cr_svuid) {
1518 error = priv_check_cred(cred, PRIV_SIGNAL_DIFFCRED, 0);
1527 * Determine whether td may deliver the specified signal to p.
1528 * Returns: 0 for permitted, an errno value otherwise
1529 * Locks: Sufficient locks to protect various components of td and p
1530 * must be held. td must be curthread, and a lock must be
1532 * References: td and p must be valid for the lifetime of the call
1535 p_cansignal(struct thread *td, struct proc *p, int signum)
1538 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1539 PROC_LOCK_ASSERT(p, MA_OWNED);
1540 if (td->td_proc == p)
1544 * UNIX signalling semantics require that processes in the same
1545 * session always be able to deliver SIGCONT to one another,
1546 * overriding the remaining protections.
1548 /* XXX: This will require an additional lock of some sort. */
1549 if (signum == SIGCONT && td->td_proc->p_session == p->p_session)
1552 * Some compat layers use SIGTHR and higher signals for
1553 * communication between different kernel threads of the same
1554 * process, so that they expect that it's always possible to
1555 * deliver them, even for suid applications where cr_cansignal() can
1556 * deny such ability for security consideration. It should be
1557 * pretty safe to do since the only way to create two processes
1558 * with the same p_leader is via rfork(2).
1560 if (td->td_proc->p_leader != NULL && signum >= SIGTHR &&
1561 signum < SIGTHR + 4 && td->td_proc->p_leader == p->p_leader)
1564 return (cr_cansignal(td->td_ucred, p, signum));
1568 * Determine whether td may reschedule p.
1569 * Returns: 0 for permitted, an errno value otherwise
1570 * Locks: Sufficient locks to protect various components of td and p
1571 * must be held. td must be curthread, and a lock must
1573 * References: td and p must be valid for the lifetime of the call
1576 p_cansched(struct thread *td, struct proc *p)
1580 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1581 PROC_LOCK_ASSERT(p, MA_OWNED);
1582 if (td->td_proc == p)
1584 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1587 if ((error = mac_proc_check_sched(td->td_ucred, p)))
1590 if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1592 if ((error = cr_seeothergids(td->td_ucred, p->p_ucred)))
1594 if (td->td_ucred->cr_ruid != p->p_ucred->cr_ruid &&
1595 td->td_ucred->cr_uid != p->p_ucred->cr_ruid) {
1596 error = priv_check(td, PRIV_SCHED_DIFFCRED);
1604 * The 'unprivileged_proc_debug' flag may be used to disable a variety of
1605 * unprivileged inter-process debugging services, including some procfs
1606 * functionality, ptrace(), and ktrace(). In the past, inter-process
1607 * debugging has been involved in a variety of security problems, and sites
1608 * not requiring the service might choose to disable it when hardening
1611 * XXX: Should modifying and reading this variable require locking?
1612 * XXX: data declarations should be together near the beginning of the file.
1614 static int unprivileged_proc_debug = 1;
1615 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_proc_debug, CTLFLAG_RW,
1616 &unprivileged_proc_debug, 0,
1617 "Unprivileged processes may use process debugging facilities");
1620 * Determine whether td may debug p.
1621 * Returns: 0 for permitted, an errno value otherwise
1622 * Locks: Sufficient locks to protect various components of td and p
1623 * must be held. td must be curthread, and a lock must
1625 * References: td and p must be valid for the lifetime of the call
1628 p_candebug(struct thread *td, struct proc *p)
1630 int credentialchanged, error, grpsubset, i, uidsubset;
1632 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1633 PROC_LOCK_ASSERT(p, MA_OWNED);
1634 if (!unprivileged_proc_debug) {
1635 error = priv_check(td, PRIV_DEBUG_UNPRIV);
1639 if (td->td_proc == p)
1641 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1644 if ((error = mac_proc_check_debug(td->td_ucred, p)))
1647 if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1649 if ((error = cr_seeothergids(td->td_ucred, p->p_ucred)))
1653 * Is p's group set a subset of td's effective group set? This
1654 * includes p's egid, group access list, rgid, and svgid.
1657 for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
1658 if (!groupmember(p->p_ucred->cr_groups[i], td->td_ucred)) {
1663 grpsubset = grpsubset &&
1664 groupmember(p->p_ucred->cr_rgid, td->td_ucred) &&
1665 groupmember(p->p_ucred->cr_svgid, td->td_ucred);
1668 * Are the uids present in p's credential equal to td's
1669 * effective uid? This includes p's euid, svuid, and ruid.
1671 uidsubset = (td->td_ucred->cr_uid == p->p_ucred->cr_uid &&
1672 td->td_ucred->cr_uid == p->p_ucred->cr_svuid &&
1673 td->td_ucred->cr_uid == p->p_ucred->cr_ruid);
1676 * Has the credential of the process changed since the last exec()?
1678 credentialchanged = (p->p_flag & P_SUGID);
1681 * If p's gids aren't a subset, or the uids aren't a subset,
1682 * or the credential has changed, require appropriate privilege
1683 * for td to debug p.
1685 if (!grpsubset || !uidsubset) {
1686 error = priv_check(td, PRIV_DEBUG_DIFFCRED);
1691 if (credentialchanged) {
1692 error = priv_check(td, PRIV_DEBUG_SUGID);
1697 /* Can't trace init when securelevel > 0. */
1698 if (p == initproc) {
1699 error = securelevel_gt(td->td_ucred, 0);
1705 * Can't trace a process that's currently exec'ing.
1707 * XXX: Note, this is not a security policy decision, it's a
1708 * basic correctness/functionality decision. Therefore, this check
1709 * should be moved to the caller's of p_candebug().
1711 if ((p->p_flag & P_INEXEC) != 0)
1718 * Determine whether the subject represented by cred can "see" a socket.
1719 * Returns: 0 for permitted, ENOENT otherwise.
1722 cr_canseesocket(struct ucred *cred, struct socket *so)
1726 error = prison_check(cred, so->so_cred);
1730 error = mac_socket_check_visible(cred, so);
1734 if (cr_seeotheruids(cred, so->so_cred))
1736 if (cr_seeothergids(cred, so->so_cred))
1742 #if defined(INET) || defined(INET6)
1744 * Determine whether the subject represented by cred can "see" a socket.
1745 * Returns: 0 for permitted, ENOENT otherwise.
1748 cr_canseeinpcb(struct ucred *cred, struct inpcb *inp)
1752 error = prison_check(cred, inp->inp_cred);
1756 INP_LOCK_ASSERT(inp);
1757 error = mac_inpcb_check_visible(cred, inp);
1761 if (cr_seeotheruids(cred, inp->inp_cred))
1763 if (cr_seeothergids(cred, inp->inp_cred))
1771 * Determine whether td can wait for the exit of p.
1772 * Returns: 0 for permitted, an errno value otherwise
1773 * Locks: Sufficient locks to protect various components of td and p
1774 * must be held. td must be curthread, and a lock must
1776 * References: td and p must be valid for the lifetime of the call
1780 p_canwait(struct thread *td, struct proc *p)
1784 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1785 PROC_LOCK_ASSERT(p, MA_OWNED);
1786 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1789 if ((error = mac_proc_check_wait(td->td_ucred, p)))
1793 /* XXXMAC: This could have odd effects on some shells. */
1794 if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1802 * Allocate a zeroed cred structure.
1807 register struct ucred *cr;
1809 cr = malloc(sizeof(*cr), M_CRED, M_WAITOK | M_ZERO);
1810 refcount_init(&cr->cr_ref, 1);
1812 audit_cred_init(cr);
1817 crextend(cr, XU_NGROUPS);
1822 * Claim another reference to a ucred structure.
1825 crhold(struct ucred *cr)
1828 refcount_acquire(&cr->cr_ref);
1833 * Free a cred structure. Throws away space when ref count gets to 0.
1836 crfree(struct ucred *cr)
1839 KASSERT(cr->cr_ref > 0, ("bad ucred refcount: %d", cr->cr_ref));
1840 KASSERT(cr->cr_ref != 0xdeadc0de, ("dangling reference to ucred"));
1841 if (refcount_release(&cr->cr_ref)) {
1843 * Some callers of crget(), such as nfs_statfs(),
1844 * allocate a temporary credential, but don't
1845 * allocate a uidinfo structure.
1847 if (cr->cr_uidinfo != NULL)
1848 uifree(cr->cr_uidinfo);
1849 if (cr->cr_ruidinfo != NULL)
1850 uifree(cr->cr_ruidinfo);
1852 * Free a prison, if any.
1854 if (cr->cr_prison != NULL)
1855 prison_free(cr->cr_prison);
1856 if (cr->cr_loginclass != NULL)
1857 loginclass_free(cr->cr_loginclass);
1859 audit_cred_destroy(cr);
1862 mac_cred_destroy(cr);
1864 free(cr->cr_groups, M_CRED);
1870 * Check to see if this ucred is shared.
1873 crshared(struct ucred *cr)
1876 return (cr->cr_ref > 1);
1880 * Copy a ucred's contents from a template. Does not block.
1883 crcopy(struct ucred *dest, struct ucred *src)
1886 KASSERT(crshared(dest) == 0, ("crcopy of shared ucred"));
1887 bcopy(&src->cr_startcopy, &dest->cr_startcopy,
1888 (unsigned)((caddr_t)&src->cr_endcopy -
1889 (caddr_t)&src->cr_startcopy));
1890 crsetgroups(dest, src->cr_ngroups, src->cr_groups);
1891 uihold(dest->cr_uidinfo);
1892 uihold(dest->cr_ruidinfo);
1893 prison_hold(dest->cr_prison);
1894 loginclass_hold(dest->cr_loginclass);
1896 audit_cred_copy(src, dest);
1899 mac_cred_copy(src, dest);
1904 * Dup cred struct to a new held one.
1907 crdup(struct ucred *cr)
1909 struct ucred *newcr;
1917 * Fill in a struct xucred based on a struct ucred.
1920 cru2x(struct ucred *cr, struct xucred *xcr)
1924 bzero(xcr, sizeof(*xcr));
1925 xcr->cr_version = XUCRED_VERSION;
1926 xcr->cr_uid = cr->cr_uid;
1928 ngroups = MIN(cr->cr_ngroups, XU_NGROUPS);
1929 xcr->cr_ngroups = ngroups;
1930 bcopy(cr->cr_groups, xcr->cr_groups,
1931 ngroups * sizeof(*cr->cr_groups));
1935 * small routine to swap a thread's current ucred for the correct one taken
1939 cred_update_thread(struct thread *td)
1945 cred = td->td_ucred;
1947 td->td_ucred = crhold(p->p_ucred);
1954 crcopysafe(struct proc *p, struct ucred *cr)
1956 struct ucred *oldcred;
1959 PROC_LOCK_ASSERT(p, MA_OWNED);
1961 oldcred = p->p_ucred;
1962 while (cr->cr_agroups < oldcred->cr_agroups) {
1963 groups = oldcred->cr_agroups;
1965 crextend(cr, groups);
1967 oldcred = p->p_ucred;
1969 crcopy(cr, oldcred);
1975 * Extend the passed in credential to hold n items.
1978 crextend(struct ucred *cr, int n)
1983 if (n <= cr->cr_agroups)
1987 * We extend by 2 each time since we're using a power of two
1988 * allocator until we need enough groups to fill a page.
1989 * Once we're allocating multiple pages, only allocate as many
1990 * as we actually need. The case of processes needing a
1991 * non-power of two number of pages seems more likely than
1992 * a real world process that adds thousands of groups one at a
1995 if ( n < PAGE_SIZE / sizeof(gid_t) ) {
1996 if (cr->cr_agroups == 0)
1997 cnt = MINALLOCSIZE / sizeof(gid_t);
1999 cnt = cr->cr_agroups * 2;
2004 cnt = roundup2(n, PAGE_SIZE / sizeof(gid_t));
2006 /* Free the old array. */
2008 free(cr->cr_groups, M_CRED);
2010 cr->cr_groups = malloc(cnt * sizeof(gid_t), M_CRED, M_WAITOK | M_ZERO);
2011 cr->cr_agroups = cnt;
2015 * Copy groups in to a credential, preserving any necessary invariants.
2016 * Currently this includes the sorting of all supplemental gids.
2017 * crextend() must have been called before hand to ensure sufficient
2018 * space is available.
2021 crsetgroups_locked(struct ucred *cr, int ngrp, gid_t *groups)
2027 KASSERT(cr->cr_agroups >= ngrp, ("cr_ngroups is too small"));
2029 bcopy(groups, cr->cr_groups, ngrp * sizeof(gid_t));
2030 cr->cr_ngroups = ngrp;
2033 * Sort all groups except cr_groups[0] to allow groupmember to
2034 * perform a binary search.
2036 * XXX: If large numbers of groups become common this should
2037 * be replaced with shell sort like linux uses or possibly
2040 for (i = 2; i < ngrp; i++) {
2041 g = cr->cr_groups[i];
2042 for (j = i-1; j >= 1 && g < cr->cr_groups[j]; j--)
2043 cr->cr_groups[j + 1] = cr->cr_groups[j];
2044 cr->cr_groups[j + 1] = g;
2049 * Copy groups in to a credential after expanding it if required.
2050 * Truncate the list to (ngroups_max + 1) if it is too large.
2053 crsetgroups(struct ucred *cr, int ngrp, gid_t *groups)
2056 if (ngrp > ngroups_max + 1)
2057 ngrp = ngroups_max + 1;
2060 crsetgroups_locked(cr, ngrp, groups);
2064 * Get login name, if available.
2066 #ifndef _SYS_SYSPROTO_H_
2067 struct getlogin_args {
2074 sys_getlogin(struct thread *td, struct getlogin_args *uap)
2077 char login[MAXLOGNAME];
2078 struct proc *p = td->td_proc;
2080 if (uap->namelen > MAXLOGNAME)
2081 uap->namelen = MAXLOGNAME;
2083 SESS_LOCK(p->p_session);
2084 bcopy(p->p_session->s_login, login, uap->namelen);
2085 SESS_UNLOCK(p->p_session);
2087 if (strlen(login) + 1 > uap->namelen)
2089 error = copyout(login, uap->namebuf, uap->namelen);
2096 #ifndef _SYS_SYSPROTO_H_
2097 struct setlogin_args {
2103 sys_setlogin(struct thread *td, struct setlogin_args *uap)
2105 struct proc *p = td->td_proc;
2107 char logintmp[MAXLOGNAME];
2109 error = priv_check(td, PRIV_PROC_SETLOGIN);
2112 error = copyinstr(uap->namebuf, logintmp, sizeof(logintmp), NULL);
2113 if (error == ENAMETOOLONG)
2117 SESS_LOCK(p->p_session);
2118 (void) memcpy(p->p_session->s_login, logintmp,
2120 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;