2 * Copyright (c) 1982, 1986, 1989, 1990, 1991, 1993
3 * The Regents of the University of California.
4 * (c) UNIX System Laboratories, Inc.
5 * Copyright (c) 2000-2001 Robert N. M. Watson.
8 * All or some portions of this file are derived from material licensed
9 * to the University of California by American Telephone and Telegraph
10 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11 * the permission of UNIX System Laboratories, Inc.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * @(#)kern_prot.c 8.6 (Berkeley) 1/21/94
41 * System calls related to processes and protection
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include "opt_compat.h"
49 #include "opt_inet6.h"
51 #include <sys/param.h>
52 #include <sys/systm.h>
55 #include <sys/kernel.h>
57 #include <sys/loginclass.h>
58 #include <sys/malloc.h>
59 #include <sys/mutex.h>
60 #include <sys/refcount.h>
64 #include <sys/sysproto.h>
66 #include <sys/pioctl.h>
67 #include <sys/racct.h>
68 #include <sys/resourcevar.h>
69 #include <sys/socket.h>
70 #include <sys/socketvar.h>
71 #include <sys/syscallsubr.h>
72 #include <sys/sysctl.h>
76 "Kernel support for interfaces necessary for regression testing (SECURITY RISK!)");
79 #if defined(INET) || defined(INET6)
80 #include <netinet/in.h>
81 #include <netinet/in_pcb.h>
84 #include <security/audit/audit.h>
85 #include <security/mac/mac_framework.h>
87 static MALLOC_DEFINE(M_CRED, "cred", "credentials");
89 SYSCTL_NODE(_security, OID_AUTO, bsd, CTLFLAG_RW, 0, "BSD security policy");
91 static void 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)
108 td->td_retval[1] = kern_getppid(td);
113 #ifndef _SYS_SYSPROTO_H_
114 struct getppid_args {
120 sys_getppid(struct thread *td, struct getppid_args *uap)
123 td->td_retval[0] = kern_getppid(td);
128 kern_getppid(struct thread *td)
130 struct proc *p = td->td_proc;
135 if (!(p->p_flag & P_TRACED)) {
136 ppid = p->p_pptr->p_pid;
140 sx_slock(&proctree_lock);
141 pp = proc_realparent(p);
143 sx_sunlock(&proctree_lock);
150 * Get process group ID; note that POSIX getpgrp takes no parameter.
152 #ifndef _SYS_SYSPROTO_H_
153 struct getpgrp_args {
158 sys_getpgrp(struct thread *td, struct getpgrp_args *uap)
160 struct proc *p = td->td_proc;
163 td->td_retval[0] = p->p_pgrp->pg_id;
168 /* Get an arbitary pid's process group id */
169 #ifndef _SYS_SYSPROTO_H_
170 struct getpgid_args {
175 sys_getpgid(struct thread *td, struct getpgid_args *uap)
187 error = p_cansee(td, p);
193 td->td_retval[0] = p->p_pgrp->pg_id;
199 * Get an arbitary pid's session id.
201 #ifndef _SYS_SYSPROTO_H_
207 sys_getsid(struct thread *td, struct getsid_args *uap)
219 error = p_cansee(td, p);
225 td->td_retval[0] = p->p_session->s_sid;
230 #ifndef _SYS_SYSPROTO_H_
237 sys_getuid(struct thread *td, struct getuid_args *uap)
240 td->td_retval[0] = td->td_ucred->cr_ruid;
241 #if defined(COMPAT_43)
242 td->td_retval[1] = td->td_ucred->cr_uid;
247 #ifndef _SYS_SYSPROTO_H_
248 struct geteuid_args {
254 sys_geteuid(struct thread *td, struct geteuid_args *uap)
257 td->td_retval[0] = td->td_ucred->cr_uid;
261 #ifndef _SYS_SYSPROTO_H_
268 sys_getgid(struct thread *td, struct getgid_args *uap)
271 td->td_retval[0] = td->td_ucred->cr_rgid;
272 #if defined(COMPAT_43)
273 td->td_retval[1] = td->td_ucred->cr_groups[0];
279 * Get effective group ID. The "egid" is groups[0], and could be obtained
280 * via getgroups. This syscall exists because it is somewhat painful to do
281 * correctly in a library function.
283 #ifndef _SYS_SYSPROTO_H_
284 struct getegid_args {
290 sys_getegid(struct thread *td, struct getegid_args *uap)
293 td->td_retval[0] = td->td_ucred->cr_groups[0];
297 #ifndef _SYS_SYSPROTO_H_
298 struct getgroups_args {
304 sys_getgroups(struct thread *td, register struct getgroups_args *uap)
310 if (uap->gidsetsize < td->td_ucred->cr_ngroups) {
311 if (uap->gidsetsize == 0)
316 ngrp = td->td_ucred->cr_ngroups;
317 groups = malloc(ngrp * sizeof(*groups), M_TEMP, M_WAITOK);
318 error = kern_getgroups(td, &ngrp, groups);
321 if (uap->gidsetsize > 0)
322 error = copyout(groups, uap->gidset, ngrp * sizeof(gid_t));
324 td->td_retval[0] = ngrp;
326 free(groups, M_TEMP);
331 kern_getgroups(struct thread *td, u_int *ngrp, gid_t *groups)
337 *ngrp = cred->cr_ngroups;
340 if (*ngrp < cred->cr_ngroups)
342 *ngrp = cred->cr_ngroups;
343 bcopy(cred->cr_groups, groups, *ngrp * sizeof(gid_t));
347 #ifndef _SYS_SYSPROTO_H_
354 sys_setsid(register struct thread *td, struct setsid_args *uap)
358 struct proc *p = td->td_proc;
359 struct pgrp *newpgrp;
360 struct session *newsess;
365 newpgrp = malloc(sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
366 newsess = malloc(sizeof(struct session), M_SESSION, M_WAITOK | M_ZERO);
368 sx_xlock(&proctree_lock);
370 if (p->p_pgid == p->p_pid || (pgrp = pgfind(p->p_pid)) != NULL) {
375 (void)enterpgrp(p, p->p_pid, newpgrp, newsess);
376 td->td_retval[0] = p->p_pid;
381 sx_xunlock(&proctree_lock);
384 free(newpgrp, M_PGRP);
386 free(newsess, M_SESSION);
392 * set process group (setpgid/old setpgrp)
394 * caller does setpgid(targpid, targpgid)
396 * pid must be caller or child of caller (ESRCH)
398 * pid must be in same session (EPERM)
399 * pid can't have done an exec (EACCES)
401 * there must exist some pid in same session having pgid (EPERM)
402 * pid must not be session leader (EPERM)
404 #ifndef _SYS_SYSPROTO_H_
405 struct setpgid_args {
406 int pid; /* target process id */
407 int pgid; /* target pgrp id */
412 sys_setpgid(struct thread *td, register struct setpgid_args *uap)
414 struct proc *curp = td->td_proc;
415 register struct proc *targp; /* target process */
416 register struct pgrp *pgrp; /* target pgrp */
418 struct pgrp *newpgrp;
425 newpgrp = malloc(sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
427 sx_xlock(&proctree_lock);
428 if (uap->pid != 0 && uap->pid != curp->p_pid) {
429 if ((targp = pfind(uap->pid)) == NULL) {
433 if (!inferior(targp)) {
438 if ((error = p_cansee(td, targp))) {
442 if (targp->p_pgrp == NULL ||
443 targp->p_session != curp->p_session) {
448 if (targp->p_flag & P_EXEC) {
456 if (SESS_LEADER(targp)) {
461 uap->pgid = targp->p_pid;
462 if ((pgrp = pgfind(uap->pgid)) == NULL) {
463 if (uap->pgid == targp->p_pid) {
464 error = enterpgrp(targp, uap->pgid, newpgrp,
471 if (pgrp == targp->p_pgrp) {
475 if (pgrp->pg_id != targp->p_pid &&
476 pgrp->pg_session != curp->p_session) {
482 error = enterthispgrp(targp, pgrp);
485 sx_xunlock(&proctree_lock);
486 KASSERT((error == 0) || (newpgrp != NULL),
487 ("setpgid failed and newpgrp is NULL"));
489 free(newpgrp, M_PGRP);
494 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
495 * compatible. It says that setting the uid/gid to euid/egid is a special
496 * case of "appropriate privilege". Once the rules are expanded out, this
497 * basically means that setuid(nnn) sets all three id's, in all permitted
498 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
499 * does not set the saved id - this is dangerous for traditional BSD
500 * programs. For this reason, we *really* do not want to set
501 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
503 #define POSIX_APPENDIX_B_4_2_2
505 #ifndef _SYS_SYSPROTO_H_
512 sys_setuid(struct thread *td, struct setuid_args *uap)
514 struct proc *p = td->td_proc;
515 struct ucred *newcred, *oldcred;
526 * Copy credentials so other references do not see our changes.
528 oldcred = crcopysafe(p, newcred);
531 error = mac_cred_check_setuid(oldcred, uid);
537 * See if we have "permission" by POSIX 1003.1 rules.
539 * Note that setuid(geteuid()) is a special case of
540 * "appropriate privileges" in appendix B.4.2.2. We need
541 * to use this clause to be compatible with traditional BSD
542 * semantics. Basically, it means that "setuid(xx)" sets all
543 * three id's (assuming you have privs).
545 * Notes on the logic. We do things in three steps.
546 * 1: We determine if the euid is going to change, and do EPERM
547 * right away. We unconditionally change the euid later if this
548 * test is satisfied, simplifying that part of the logic.
549 * 2: We determine if the real and/or saved uids are going to
550 * change. Determined by compile options.
551 * 3: Change euid last. (after tests in #2 for "appropriate privs")
553 if (uid != oldcred->cr_ruid && /* allow setuid(getuid()) */
554 #ifdef _POSIX_SAVED_IDS
555 uid != oldcred->cr_svuid && /* allow setuid(saved gid) */
557 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
558 uid != oldcred->cr_uid && /* allow setuid(geteuid()) */
560 (error = priv_check_cred(oldcred, PRIV_CRED_SETUID, 0)) != 0)
563 #ifdef _POSIX_SAVED_IDS
565 * Do we have "appropriate privileges" (are we root or uid == euid)
566 * If so, we are changing the real uid and/or saved uid.
569 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
570 uid == oldcred->cr_uid ||
572 /* We are using privs. */
573 priv_check_cred(oldcred, PRIV_CRED_SETUID, 0) == 0)
577 * Set the real uid and transfer proc count to new user.
579 if (uid != oldcred->cr_ruid) {
580 change_ruid(newcred, uip);
586 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
587 * the security of seteuid() depends on it. B.4.2.2 says it
588 * is important that we should do this.
590 if (uid != oldcred->cr_svuid) {
591 change_svuid(newcred, uid);
597 * In all permitted cases, we are changing the euid.
599 if (uid != oldcred->cr_uid) {
600 change_euid(newcred, uip);
603 p->p_ucred = newcred;
606 racct_proc_ucred_changed(p, oldcred, newcred);
619 #ifndef _SYS_SYSPROTO_H_
620 struct seteuid_args {
626 sys_seteuid(struct thread *td, struct seteuid_args *uap)
628 struct proc *p = td->td_proc;
629 struct ucred *newcred, *oldcred;
631 struct uidinfo *euip;
635 AUDIT_ARG_EUID(euid);
640 * Copy credentials so other references do not see our changes.
642 oldcred = crcopysafe(p, newcred);
645 error = mac_cred_check_seteuid(oldcred, euid);
650 if (euid != oldcred->cr_ruid && /* allow seteuid(getuid()) */
651 euid != oldcred->cr_svuid && /* allow seteuid(saved uid) */
652 (error = priv_check_cred(oldcred, PRIV_CRED_SETEUID, 0)) != 0)
656 * Everything's okay, do it.
658 if (oldcred->cr_uid != euid) {
659 change_euid(newcred, euip);
662 p->p_ucred = newcred;
675 #ifndef _SYS_SYSPROTO_H_
682 sys_setgid(struct thread *td, struct setgid_args *uap)
684 struct proc *p = td->td_proc;
685 struct ucred *newcred, *oldcred;
693 oldcred = crcopysafe(p, newcred);
696 error = mac_cred_check_setgid(oldcred, gid);
702 * See if we have "permission" by POSIX 1003.1 rules.
704 * Note that setgid(getegid()) is a special case of
705 * "appropriate privileges" in appendix B.4.2.2. We need
706 * to use this clause to be compatible with traditional BSD
707 * semantics. Basically, it means that "setgid(xx)" sets all
708 * three id's (assuming you have privs).
710 * For notes on the logic here, see setuid() above.
712 if (gid != oldcred->cr_rgid && /* allow setgid(getgid()) */
713 #ifdef _POSIX_SAVED_IDS
714 gid != oldcred->cr_svgid && /* allow setgid(saved gid) */
716 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
717 gid != oldcred->cr_groups[0] && /* allow setgid(getegid()) */
719 (error = priv_check_cred(oldcred, PRIV_CRED_SETGID, 0)) != 0)
722 #ifdef _POSIX_SAVED_IDS
724 * Do we have "appropriate privileges" (are we root or gid == egid)
725 * If so, we are changing the real uid and saved gid.
728 #ifdef POSIX_APPENDIX_B_4_2_2 /* use the clause from B.4.2.2 */
729 gid == oldcred->cr_groups[0] ||
731 /* We are using privs. */
732 priv_check_cred(oldcred, PRIV_CRED_SETGID, 0) == 0)
738 if (oldcred->cr_rgid != gid) {
739 change_rgid(newcred, gid);
745 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
746 * the security of setegid() depends on it. B.4.2.2 says it
747 * is important that we should do this.
749 if (oldcred->cr_svgid != gid) {
750 change_svgid(newcred, gid);
755 * In all cases permitted cases, we are changing the egid.
756 * Copy credentials so other references do not see our changes.
758 if (oldcred->cr_groups[0] != gid) {
759 change_egid(newcred, gid);
762 p->p_ucred = newcred;
773 #ifndef _SYS_SYSPROTO_H_
774 struct setegid_args {
780 sys_setegid(struct thread *td, struct setegid_args *uap)
782 struct proc *p = td->td_proc;
783 struct ucred *newcred, *oldcred;
788 AUDIT_ARG_EGID(egid);
791 oldcred = crcopysafe(p, newcred);
794 error = mac_cred_check_setegid(oldcred, egid);
799 if (egid != oldcred->cr_rgid && /* allow setegid(getgid()) */
800 egid != oldcred->cr_svgid && /* allow setegid(saved gid) */
801 (error = priv_check_cred(oldcred, PRIV_CRED_SETEGID, 0)) != 0)
804 if (oldcred->cr_groups[0] != egid) {
805 change_egid(newcred, egid);
808 p->p_ucred = newcred;
819 #ifndef _SYS_SYSPROTO_H_
820 struct setgroups_args {
827 sys_setgroups(struct thread *td, struct setgroups_args *uap)
829 gid_t *groups = NULL;
832 if (uap->gidsetsize > ngroups_max + 1)
834 groups = malloc(uap->gidsetsize * sizeof(gid_t), M_TEMP, M_WAITOK);
835 error = copyin(uap->gidset, groups, uap->gidsetsize * sizeof(gid_t));
838 error = kern_setgroups(td, uap->gidsetsize, groups);
840 free(groups, M_TEMP);
845 kern_setgroups(struct thread *td, u_int ngrp, gid_t *groups)
847 struct proc *p = td->td_proc;
848 struct ucred *newcred, *oldcred;
851 if (ngrp > ngroups_max + 1)
853 AUDIT_ARG_GROUPSET(groups, ngrp);
855 crextend(newcred, ngrp);
857 oldcred = crcopysafe(p, newcred);
860 error = mac_cred_check_setgroups(oldcred, ngrp, groups);
865 error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0);
871 * setgroups(0, NULL) is a legitimate way of clearing the
872 * groups vector on non-BSD systems (which generally do not
873 * have the egid in the groups[0]). We risk security holes
874 * when running non-BSD software if we do not do the same.
876 newcred->cr_ngroups = 1;
878 crsetgroups_locked(newcred, ngrp, groups);
881 p->p_ucred = newcred;
892 #ifndef _SYS_SYSPROTO_H_
893 struct setreuid_args {
900 sys_setreuid(register struct thread *td, struct setreuid_args *uap)
902 struct proc *p = td->td_proc;
903 struct ucred *newcred, *oldcred;
905 struct uidinfo *euip, *ruip;
910 AUDIT_ARG_EUID(euid);
911 AUDIT_ARG_RUID(ruid);
916 oldcred = crcopysafe(p, newcred);
919 error = mac_cred_check_setreuid(oldcred, ruid, euid);
924 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
925 ruid != oldcred->cr_svuid) ||
926 (euid != (uid_t)-1 && euid != oldcred->cr_uid &&
927 euid != oldcred->cr_ruid && euid != oldcred->cr_svuid)) &&
928 (error = priv_check_cred(oldcred, PRIV_CRED_SETREUID, 0)) != 0)
931 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
932 change_euid(newcred, euip);
935 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
936 change_ruid(newcred, ruip);
939 if ((ruid != (uid_t)-1 || newcred->cr_uid != newcred->cr_ruid) &&
940 newcred->cr_svuid != newcred->cr_uid) {
941 change_svuid(newcred, newcred->cr_uid);
944 p->p_ucred = newcred;
947 racct_proc_ucred_changed(p, oldcred, newcred);
962 #ifndef _SYS_SYSPROTO_H_
963 struct setregid_args {
970 sys_setregid(register struct thread *td, struct setregid_args *uap)
972 struct proc *p = td->td_proc;
973 struct ucred *newcred, *oldcred;
979 AUDIT_ARG_EGID(egid);
980 AUDIT_ARG_RGID(rgid);
983 oldcred = crcopysafe(p, newcred);
986 error = mac_cred_check_setregid(oldcred, rgid, egid);
991 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
992 rgid != oldcred->cr_svgid) ||
993 (egid != (gid_t)-1 && egid != oldcred->cr_groups[0] &&
994 egid != oldcred->cr_rgid && egid != oldcred->cr_svgid)) &&
995 (error = priv_check_cred(oldcred, PRIV_CRED_SETREGID, 0)) != 0)
998 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
999 change_egid(newcred, egid);
1002 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
1003 change_rgid(newcred, rgid);
1006 if ((rgid != (gid_t)-1 || newcred->cr_groups[0] != newcred->cr_rgid) &&
1007 newcred->cr_svgid != newcred->cr_groups[0]) {
1008 change_svgid(newcred, newcred->cr_groups[0]);
1011 p->p_ucred = newcred;
1023 * setresuid(ruid, euid, suid) is like setreuid except control over the saved
1026 #ifndef _SYS_SYSPROTO_H_
1027 struct setresuid_args {
1035 sys_setresuid(register struct thread *td, struct setresuid_args *uap)
1037 struct proc *p = td->td_proc;
1038 struct ucred *newcred, *oldcred;
1039 uid_t euid, ruid, suid;
1040 struct uidinfo *euip, *ruip;
1046 AUDIT_ARG_EUID(euid);
1047 AUDIT_ARG_RUID(ruid);
1048 AUDIT_ARG_SUID(suid);
1050 euip = uifind(euid);
1051 ruip = uifind(ruid);
1053 oldcred = crcopysafe(p, newcred);
1056 error = mac_cred_check_setresuid(oldcred, ruid, euid, suid);
1061 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
1062 ruid != oldcred->cr_svuid &&
1063 ruid != oldcred->cr_uid) ||
1064 (euid != (uid_t)-1 && euid != oldcred->cr_ruid &&
1065 euid != oldcred->cr_svuid &&
1066 euid != oldcred->cr_uid) ||
1067 (suid != (uid_t)-1 && suid != oldcred->cr_ruid &&
1068 suid != oldcred->cr_svuid &&
1069 suid != oldcred->cr_uid)) &&
1070 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESUID, 0)) != 0)
1073 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
1074 change_euid(newcred, euip);
1077 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
1078 change_ruid(newcred, ruip);
1081 if (suid != (uid_t)-1 && oldcred->cr_svuid != suid) {
1082 change_svuid(newcred, suid);
1085 p->p_ucred = newcred;
1088 racct_proc_ucred_changed(p, oldcred, newcred);
1105 * setresgid(rgid, egid, sgid) is like setregid except control over the saved
1108 #ifndef _SYS_SYSPROTO_H_
1109 struct setresgid_args {
1117 sys_setresgid(register struct thread *td, struct setresgid_args *uap)
1119 struct proc *p = td->td_proc;
1120 struct ucred *newcred, *oldcred;
1121 gid_t egid, rgid, sgid;
1127 AUDIT_ARG_EGID(egid);
1128 AUDIT_ARG_RGID(rgid);
1129 AUDIT_ARG_SGID(sgid);
1132 oldcred = crcopysafe(p, newcred);
1135 error = mac_cred_check_setresgid(oldcred, rgid, egid, sgid);
1140 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
1141 rgid != oldcred->cr_svgid &&
1142 rgid != oldcred->cr_groups[0]) ||
1143 (egid != (gid_t)-1 && egid != oldcred->cr_rgid &&
1144 egid != oldcred->cr_svgid &&
1145 egid != oldcred->cr_groups[0]) ||
1146 (sgid != (gid_t)-1 && sgid != oldcred->cr_rgid &&
1147 sgid != oldcred->cr_svgid &&
1148 sgid != oldcred->cr_groups[0])) &&
1149 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESGID, 0)) != 0)
1152 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
1153 change_egid(newcred, egid);
1156 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
1157 change_rgid(newcred, rgid);
1160 if (sgid != (gid_t)-1 && oldcred->cr_svgid != sgid) {
1161 change_svgid(newcred, sgid);
1164 p->p_ucred = newcred;
1175 #ifndef _SYS_SYSPROTO_H_
1176 struct getresuid_args {
1184 sys_getresuid(register struct thread *td, struct getresuid_args *uap)
1187 int error1 = 0, error2 = 0, error3 = 0;
1189 cred = td->td_ucred;
1191 error1 = copyout(&cred->cr_ruid,
1192 uap->ruid, sizeof(cred->cr_ruid));
1194 error2 = copyout(&cred->cr_uid,
1195 uap->euid, sizeof(cred->cr_uid));
1197 error3 = copyout(&cred->cr_svuid,
1198 uap->suid, sizeof(cred->cr_svuid));
1199 return (error1 ? error1 : error2 ? error2 : error3);
1202 #ifndef _SYS_SYSPROTO_H_
1203 struct getresgid_args {
1211 sys_getresgid(register struct thread *td, struct getresgid_args *uap)
1214 int error1 = 0, error2 = 0, error3 = 0;
1216 cred = td->td_ucred;
1218 error1 = copyout(&cred->cr_rgid,
1219 uap->rgid, sizeof(cred->cr_rgid));
1221 error2 = copyout(&cred->cr_groups[0],
1222 uap->egid, sizeof(cred->cr_groups[0]));
1224 error3 = copyout(&cred->cr_svgid,
1225 uap->sgid, sizeof(cred->cr_svgid));
1226 return (error1 ? error1 : error2 ? error2 : error3);
1229 #ifndef _SYS_SYSPROTO_H_
1230 struct issetugid_args {
1236 sys_issetugid(register struct thread *td, struct issetugid_args *uap)
1238 struct proc *p = td->td_proc;
1241 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
1242 * we use P_SUGID because we consider changing the owners as
1243 * "tainting" as well.
1244 * This is significant for procs that start as root and "become"
1245 * a user without an exec - programs cannot know *everything*
1246 * that libc *might* have put in their data segment.
1249 td->td_retval[0] = (p->p_flag & P_SUGID) ? 1 : 0;
1255 sys___setugid(struct thread *td, struct __setugid_args *uap)
1261 switch (uap->flag) {
1264 p->p_flag &= ~P_SUGID;
1269 p->p_flag |= P_SUGID;
1275 #else /* !REGRESSION */
1278 #endif /* REGRESSION */
1282 * Check if gid is a member of the group set.
1285 groupmember(gid_t gid, struct ucred *cred)
1291 if (cred->cr_groups[0] == gid)
1295 * If gid was not our primary group, perform a binary search
1296 * of the supplemental groups. This is possible because we
1297 * sort the groups in crsetgroups().
1300 h = cred->cr_ngroups;
1302 m = l + ((h - l) / 2);
1303 if (cred->cr_groups[m] < gid)
1308 if ((l < cred->cr_ngroups) && (cred->cr_groups[l] == gid))
1315 * Test the active securelevel against a given level. securelevel_gt()
1316 * implements (securelevel > level). securelevel_ge() implements
1317 * (securelevel >= level). Note that the logic is inverted -- these
1318 * functions return EPERM on "success" and 0 on "failure".
1320 * Due to care taken when setting the securelevel, we know that no jail will
1321 * be less secure that its parent (or the physical system), so it is sufficient
1322 * to test the current jail only.
1324 * XXXRW: Possibly since this has to do with privilege, it should move to
1328 securelevel_gt(struct ucred *cr, int level)
1331 return (cr->cr_prison->pr_securelevel > level ? EPERM : 0);
1335 securelevel_ge(struct ucred *cr, int level)
1338 return (cr->cr_prison->pr_securelevel >= level ? EPERM : 0);
1342 * 'see_other_uids' determines whether or not visibility of processes
1343 * and sockets with credentials holding different real uids is possible
1344 * using a variety of system MIBs.
1345 * XXX: data declarations should be together near the beginning of the file.
1347 static int see_other_uids = 1;
1348 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_uids, CTLFLAG_RW,
1350 "Unprivileged processes may see subjects/objects with different real uid");
1353 * Determine if u1 "can see" the subject specified by u2, according to the
1354 * 'see_other_uids' policy.
1355 * Returns: 0 for permitted, ESRCH otherwise
1357 * References: *u1 and *u2 must not change during the call
1358 * u1 may equal u2, in which case only one reference is required
1361 cr_seeotheruids(struct ucred *u1, struct ucred *u2)
1364 if (!see_other_uids && u1->cr_ruid != u2->cr_ruid) {
1365 if (priv_check_cred(u1, PRIV_SEEOTHERUIDS, 0) != 0)
1372 * 'see_other_gids' determines whether or not visibility of processes
1373 * and sockets with credentials holding different real gids is possible
1374 * using a variety of system MIBs.
1375 * XXX: data declarations should be together near the beginning of the file.
1377 static int see_other_gids = 1;
1378 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_gids, CTLFLAG_RW,
1380 "Unprivileged processes may see subjects/objects with different real gid");
1383 * Determine if u1 can "see" the subject specified by u2, according to the
1384 * 'see_other_gids' policy.
1385 * Returns: 0 for permitted, ESRCH otherwise
1387 * References: *u1 and *u2 must not change during the call
1388 * u1 may equal u2, in which case only one reference is required
1391 cr_seeothergids(struct ucred *u1, struct ucred *u2)
1395 if (!see_other_gids) {
1397 for (i = 0; i < u1->cr_ngroups; i++) {
1398 if (groupmember(u1->cr_groups[i], u2))
1404 if (priv_check_cred(u1, PRIV_SEEOTHERGIDS, 0) != 0)
1412 * Determine if u1 "can see" the subject specified by u2.
1413 * Returns: 0 for permitted, an errno value otherwise
1415 * References: *u1 and *u2 must not change during the call
1416 * u1 may equal u2, in which case only one reference is required
1419 cr_cansee(struct ucred *u1, struct ucred *u2)
1423 if ((error = prison_check(u1, u2)))
1426 if ((error = mac_cred_check_visible(u1, u2)))
1429 if ((error = cr_seeotheruids(u1, u2)))
1431 if ((error = cr_seeothergids(u1, u2)))
1437 * Determine if td "can see" the subject specified by p.
1438 * Returns: 0 for permitted, an errno value otherwise
1439 * Locks: Sufficient locks to protect p->p_ucred must be held. td really
1440 * should be curthread.
1441 * References: td and p must be valid for the lifetime of the call
1444 p_cansee(struct thread *td, struct proc *p)
1447 /* Wrap cr_cansee() for all functionality. */
1448 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1449 PROC_LOCK_ASSERT(p, MA_OWNED);
1450 return (cr_cansee(td->td_ucred, p->p_ucred));
1454 * 'conservative_signals' prevents the delivery of a broad class of
1455 * signals by unprivileged processes to processes that have changed their
1456 * credentials since the last invocation of execve(). This can prevent
1457 * the leakage of cached information or retained privileges as a result
1458 * of a common class of signal-related vulnerabilities. However, this
1459 * may interfere with some applications that expect to be able to
1460 * deliver these signals to peer processes after having given up
1463 static int conservative_signals = 1;
1464 SYSCTL_INT(_security_bsd, OID_AUTO, conservative_signals, CTLFLAG_RW,
1465 &conservative_signals, 0, "Unprivileged processes prevented from "
1466 "sending certain signals to processes whose credentials have changed");
1468 * Determine whether cred may deliver the specified signal to proc.
1469 * Returns: 0 for permitted, an errno value otherwise.
1470 * Locks: A lock must be held for proc.
1471 * References: cred and proc must be valid for the lifetime of the call.
1474 cr_cansignal(struct ucred *cred, struct proc *proc, int signum)
1478 PROC_LOCK_ASSERT(proc, MA_OWNED);
1480 * Jail semantics limit the scope of signalling to proc in the
1481 * same jail as cred, if cred is in jail.
1483 error = prison_check(cred, proc->p_ucred);
1487 if ((error = mac_proc_check_signal(cred, proc, signum)))
1490 if ((error = cr_seeotheruids(cred, proc->p_ucred)))
1492 if ((error = cr_seeothergids(cred, proc->p_ucred)))
1496 * UNIX signal semantics depend on the status of the P_SUGID
1497 * bit on the target process. If the bit is set, then additional
1498 * restrictions are placed on the set of available signals.
1500 if (conservative_signals && (proc->p_flag & P_SUGID)) {
1515 * Generally, permit job and terminal control
1520 /* Not permitted without privilege. */
1521 error = priv_check_cred(cred, PRIV_SIGNAL_SUGID, 0);
1528 * Generally, the target credential's ruid or svuid must match the
1529 * subject credential's ruid or euid.
1531 if (cred->cr_ruid != proc->p_ucred->cr_ruid &&
1532 cred->cr_ruid != proc->p_ucred->cr_svuid &&
1533 cred->cr_uid != proc->p_ucred->cr_ruid &&
1534 cred->cr_uid != proc->p_ucred->cr_svuid) {
1535 error = priv_check_cred(cred, PRIV_SIGNAL_DIFFCRED, 0);
1544 * Determine whether td may deliver the specified signal to p.
1545 * Returns: 0 for permitted, an errno value otherwise
1546 * Locks: Sufficient locks to protect various components of td and p
1547 * must be held. td must be curthread, and a lock must be
1549 * References: td and p must be valid for the lifetime of the call
1552 p_cansignal(struct thread *td, struct proc *p, int signum)
1555 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1556 PROC_LOCK_ASSERT(p, MA_OWNED);
1557 if (td->td_proc == p)
1561 * UNIX signalling semantics require that processes in the same
1562 * session always be able to deliver SIGCONT to one another,
1563 * overriding the remaining protections.
1565 /* XXX: This will require an additional lock of some sort. */
1566 if (signum == SIGCONT && td->td_proc->p_session == p->p_session)
1569 * Some compat layers use SIGTHR and higher signals for
1570 * communication between different kernel threads of the same
1571 * process, so that they expect that it's always possible to
1572 * deliver them, even for suid applications where cr_cansignal() can
1573 * deny such ability for security consideration. It should be
1574 * pretty safe to do since the only way to create two processes
1575 * with the same p_leader is via rfork(2).
1577 if (td->td_proc->p_leader != NULL && signum >= SIGTHR &&
1578 signum < SIGTHR + 4 && td->td_proc->p_leader == p->p_leader)
1581 return (cr_cansignal(td->td_ucred, p, signum));
1585 * Determine whether td may reschedule p.
1586 * Returns: 0 for permitted, an errno value otherwise
1587 * Locks: Sufficient locks to protect various components of td and p
1588 * must be held. td must be curthread, and a lock must
1590 * References: td and p must be valid for the lifetime of the call
1593 p_cansched(struct thread *td, struct proc *p)
1597 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1598 PROC_LOCK_ASSERT(p, MA_OWNED);
1599 if (td->td_proc == p)
1601 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1604 if ((error = mac_proc_check_sched(td->td_ucred, p)))
1607 if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1609 if ((error = cr_seeothergids(td->td_ucred, p->p_ucred)))
1611 if (td->td_ucred->cr_ruid != p->p_ucred->cr_ruid &&
1612 td->td_ucred->cr_uid != p->p_ucred->cr_ruid) {
1613 error = priv_check(td, PRIV_SCHED_DIFFCRED);
1621 * The 'unprivileged_proc_debug' flag may be used to disable a variety of
1622 * unprivileged inter-process debugging services, including some procfs
1623 * functionality, ptrace(), and ktrace(). In the past, inter-process
1624 * debugging has been involved in a variety of security problems, and sites
1625 * not requiring the service might choose to disable it when hardening
1628 * XXX: Should modifying and reading this variable require locking?
1629 * XXX: data declarations should be together near the beginning of the file.
1631 static int unprivileged_proc_debug = 1;
1632 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_proc_debug, CTLFLAG_RW,
1633 &unprivileged_proc_debug, 0,
1634 "Unprivileged processes may use process debugging facilities");
1637 * Determine whether td may debug p.
1638 * Returns: 0 for permitted, an errno value otherwise
1639 * Locks: Sufficient locks to protect various components of td and p
1640 * must be held. td must be curthread, and a lock must
1642 * References: td and p must be valid for the lifetime of the call
1645 p_candebug(struct thread *td, struct proc *p)
1647 int credentialchanged, error, grpsubset, i, uidsubset;
1649 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1650 PROC_LOCK_ASSERT(p, MA_OWNED);
1651 if (!unprivileged_proc_debug) {
1652 error = priv_check(td, PRIV_DEBUG_UNPRIV);
1656 if (td->td_proc == p)
1658 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1661 if ((error = mac_proc_check_debug(td->td_ucred, p)))
1664 if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1666 if ((error = cr_seeothergids(td->td_ucred, p->p_ucred)))
1670 * Is p's group set a subset of td's effective group set? This
1671 * includes p's egid, group access list, rgid, and svgid.
1674 for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
1675 if (!groupmember(p->p_ucred->cr_groups[i], td->td_ucred)) {
1680 grpsubset = grpsubset &&
1681 groupmember(p->p_ucred->cr_rgid, td->td_ucred) &&
1682 groupmember(p->p_ucred->cr_svgid, td->td_ucred);
1685 * Are the uids present in p's credential equal to td's
1686 * effective uid? This includes p's euid, svuid, and ruid.
1688 uidsubset = (td->td_ucred->cr_uid == p->p_ucred->cr_uid &&
1689 td->td_ucred->cr_uid == p->p_ucred->cr_svuid &&
1690 td->td_ucred->cr_uid == p->p_ucred->cr_ruid);
1693 * Has the credential of the process changed since the last exec()?
1695 credentialchanged = (p->p_flag & P_SUGID);
1698 * If p's gids aren't a subset, or the uids aren't a subset,
1699 * or the credential has changed, require appropriate privilege
1700 * for td to debug p.
1702 if (!grpsubset || !uidsubset) {
1703 error = priv_check(td, PRIV_DEBUG_DIFFCRED);
1708 if (credentialchanged) {
1709 error = priv_check(td, PRIV_DEBUG_SUGID);
1714 /* Can't trace init when securelevel > 0. */
1715 if (p == initproc) {
1716 error = securelevel_gt(td->td_ucred, 0);
1722 * Can't trace a process that's currently exec'ing.
1724 * XXX: Note, this is not a security policy decision, it's a
1725 * basic correctness/functionality decision. Therefore, this check
1726 * should be moved to the caller's of p_candebug().
1728 if ((p->p_flag & P_INEXEC) != 0)
1735 * Determine whether the subject represented by cred can "see" a socket.
1736 * Returns: 0 for permitted, ENOENT otherwise.
1739 cr_canseesocket(struct ucred *cred, struct socket *so)
1743 error = prison_check(cred, so->so_cred);
1747 error = mac_socket_check_visible(cred, so);
1751 if (cr_seeotheruids(cred, so->so_cred))
1753 if (cr_seeothergids(cred, so->so_cred))
1759 #if defined(INET) || defined(INET6)
1761 * Determine whether the subject represented by cred can "see" a socket.
1762 * Returns: 0 for permitted, ENOENT otherwise.
1765 cr_canseeinpcb(struct ucred *cred, struct inpcb *inp)
1769 error = prison_check(cred, inp->inp_cred);
1773 INP_LOCK_ASSERT(inp);
1774 error = mac_inpcb_check_visible(cred, inp);
1778 if (cr_seeotheruids(cred, inp->inp_cred))
1780 if (cr_seeothergids(cred, inp->inp_cred))
1788 * Determine whether td can wait for the exit of p.
1789 * Returns: 0 for permitted, an errno value otherwise
1790 * Locks: Sufficient locks to protect various components of td and p
1791 * must be held. td must be curthread, and a lock must
1793 * References: td and p must be valid for the lifetime of the call
1797 p_canwait(struct thread *td, struct proc *p)
1801 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1802 PROC_LOCK_ASSERT(p, MA_OWNED);
1803 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1806 if ((error = mac_proc_check_wait(td->td_ucred, p)))
1810 /* XXXMAC: This could have odd effects on some shells. */
1811 if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1819 * Allocate a zeroed cred structure.
1824 register struct ucred *cr;
1826 cr = malloc(sizeof(*cr), M_CRED, M_WAITOK | M_ZERO);
1827 refcount_init(&cr->cr_ref, 1);
1829 audit_cred_init(cr);
1834 crextend(cr, XU_NGROUPS);
1839 * Claim another reference to a ucred structure.
1842 crhold(struct ucred *cr)
1845 refcount_acquire(&cr->cr_ref);
1850 * Free a cred structure. Throws away space when ref count gets to 0.
1853 crfree(struct ucred *cr)
1856 KASSERT(cr->cr_ref > 0, ("bad ucred refcount: %d", cr->cr_ref));
1857 KASSERT(cr->cr_ref != 0xdeadc0de, ("dangling reference to ucred"));
1858 if (refcount_release(&cr->cr_ref)) {
1860 * Some callers of crget(), such as nfs_statfs(),
1861 * allocate a temporary credential, but don't
1862 * allocate a uidinfo structure.
1864 if (cr->cr_uidinfo != NULL)
1865 uifree(cr->cr_uidinfo);
1866 if (cr->cr_ruidinfo != NULL)
1867 uifree(cr->cr_ruidinfo);
1869 * Free a prison, if any.
1871 if (cr->cr_prison != NULL)
1872 prison_free(cr->cr_prison);
1873 if (cr->cr_loginclass != NULL)
1874 loginclass_free(cr->cr_loginclass);
1876 audit_cred_destroy(cr);
1879 mac_cred_destroy(cr);
1881 free(cr->cr_groups, M_CRED);
1887 * Check to see if this ucred is shared.
1890 crshared(struct ucred *cr)
1893 return (cr->cr_ref > 1);
1897 * Copy a ucred's contents from a template. Does not block.
1900 crcopy(struct ucred *dest, struct ucred *src)
1903 KASSERT(crshared(dest) == 0, ("crcopy of shared ucred"));
1904 bcopy(&src->cr_startcopy, &dest->cr_startcopy,
1905 (unsigned)((caddr_t)&src->cr_endcopy -
1906 (caddr_t)&src->cr_startcopy));
1907 crsetgroups(dest, src->cr_ngroups, src->cr_groups);
1908 uihold(dest->cr_uidinfo);
1909 uihold(dest->cr_ruidinfo);
1910 prison_hold(dest->cr_prison);
1911 loginclass_hold(dest->cr_loginclass);
1913 audit_cred_copy(src, dest);
1916 mac_cred_copy(src, dest);
1921 * Dup cred struct to a new held one.
1924 crdup(struct ucred *cr)
1926 struct ucred *newcr;
1934 * Fill in a struct xucred based on a struct ucred.
1937 cru2x(struct ucred *cr, struct xucred *xcr)
1941 bzero(xcr, sizeof(*xcr));
1942 xcr->cr_version = XUCRED_VERSION;
1943 xcr->cr_uid = cr->cr_uid;
1945 ngroups = MIN(cr->cr_ngroups, XU_NGROUPS);
1946 xcr->cr_ngroups = ngroups;
1947 bcopy(cr->cr_groups, xcr->cr_groups,
1948 ngroups * sizeof(*cr->cr_groups));
1952 * small routine to swap a thread's current ucred for the correct one taken
1956 cred_update_thread(struct thread *td)
1962 cred = td->td_ucred;
1964 td->td_ucred = crhold(p->p_ucred);
1971 crcopysafe(struct proc *p, struct ucred *cr)
1973 struct ucred *oldcred;
1976 PROC_LOCK_ASSERT(p, MA_OWNED);
1978 oldcred = p->p_ucred;
1979 while (cr->cr_agroups < oldcred->cr_agroups) {
1980 groups = oldcred->cr_agroups;
1982 crextend(cr, groups);
1984 oldcred = p->p_ucred;
1986 crcopy(cr, oldcred);
1992 * Extend the passed in credential to hold n items.
1995 crextend(struct ucred *cr, int n)
2000 if (n <= cr->cr_agroups)
2004 * We extend by 2 each time since we're using a power of two
2005 * allocator until we need enough groups to fill a page.
2006 * Once we're allocating multiple pages, only allocate as many
2007 * as we actually need. The case of processes needing a
2008 * non-power of two number of pages seems more likely than
2009 * a real world process that adds thousands of groups one at a
2012 if ( n < PAGE_SIZE / sizeof(gid_t) ) {
2013 if (cr->cr_agroups == 0)
2014 cnt = MINALLOCSIZE / sizeof(gid_t);
2016 cnt = cr->cr_agroups * 2;
2021 cnt = roundup2(n, PAGE_SIZE / sizeof(gid_t));
2023 /* Free the old array. */
2025 free(cr->cr_groups, M_CRED);
2027 cr->cr_groups = malloc(cnt * sizeof(gid_t), M_CRED, M_WAITOK | M_ZERO);
2028 cr->cr_agroups = cnt;
2032 * Copy groups in to a credential, preserving any necessary invariants.
2033 * Currently this includes the sorting of all supplemental gids.
2034 * crextend() must have been called before hand to ensure sufficient
2035 * space is available.
2038 crsetgroups_locked(struct ucred *cr, int ngrp, gid_t *groups)
2044 KASSERT(cr->cr_agroups >= ngrp, ("cr_ngroups is too small"));
2046 bcopy(groups, cr->cr_groups, ngrp * sizeof(gid_t));
2047 cr->cr_ngroups = ngrp;
2050 * Sort all groups except cr_groups[0] to allow groupmember to
2051 * perform a binary search.
2053 * XXX: If large numbers of groups become common this should
2054 * be replaced with shell sort like linux uses or possibly
2057 for (i = 2; i < ngrp; i++) {
2058 g = cr->cr_groups[i];
2059 for (j = i-1; j >= 1 && g < cr->cr_groups[j]; j--)
2060 cr->cr_groups[j + 1] = cr->cr_groups[j];
2061 cr->cr_groups[j + 1] = g;
2066 * Copy groups in to a credential after expanding it if required.
2067 * Truncate the list to (ngroups_max + 1) if it is too large.
2070 crsetgroups(struct ucred *cr, int ngrp, gid_t *groups)
2073 if (ngrp > ngroups_max + 1)
2074 ngrp = ngroups_max + 1;
2077 crsetgroups_locked(cr, ngrp, groups);
2081 * Get login name, if available.
2083 #ifndef _SYS_SYSPROTO_H_
2084 struct getlogin_args {
2091 sys_getlogin(struct thread *td, struct getlogin_args *uap)
2094 char login[MAXLOGNAME];
2095 struct proc *p = td->td_proc;
2097 if (uap->namelen > MAXLOGNAME)
2098 uap->namelen = MAXLOGNAME;
2100 SESS_LOCK(p->p_session);
2101 bcopy(p->p_session->s_login, login, uap->namelen);
2102 SESS_UNLOCK(p->p_session);
2104 if (strlen(login) + 1 > uap->namelen)
2106 error = copyout(login, uap->namebuf, uap->namelen);
2113 #ifndef _SYS_SYSPROTO_H_
2114 struct setlogin_args {
2120 sys_setlogin(struct thread *td, struct setlogin_args *uap)
2122 struct proc *p = td->td_proc;
2124 char logintmp[MAXLOGNAME];
2126 error = priv_check(td, PRIV_PROC_SETLOGIN);
2129 error = copyinstr(uap->namebuf, logintmp, sizeof(logintmp), NULL);
2130 if (error == ENAMETOOLONG)
2134 SESS_LOCK(p->p_session);
2135 (void) memcpy(p->p_session->s_login, logintmp,
2137 SESS_UNLOCK(p->p_session);
2144 setsugid(struct proc *p)
2147 PROC_LOCK_ASSERT(p, MA_OWNED);
2148 p->p_flag |= P_SUGID;
2149 if (!(p->p_pfsflags & PF_ISUGID))
2154 * Change a process's effective uid.
2155 * Side effects: newcred->cr_uid and newcred->cr_uidinfo will be modified.
2156 * References: newcred must be an exclusive credential reference for the
2157 * duration of the call.
2160 change_euid(struct ucred *newcred, struct uidinfo *euip)
2163 newcred->cr_uid = euip->ui_uid;
2165 uifree(newcred->cr_uidinfo);
2166 newcred->cr_uidinfo = euip;
2170 * Change a process's effective gid.
2171 * Side effects: newcred->cr_gid will be modified.
2172 * References: newcred must be an exclusive credential reference for the
2173 * duration of the call.
2176 change_egid(struct ucred *newcred, gid_t egid)
2179 newcred->cr_groups[0] = egid;
2183 * Change a process's real uid.
2184 * Side effects: newcred->cr_ruid will be updated, newcred->cr_ruidinfo
2185 * will be updated, and the old and new cr_ruidinfo proc
2186 * counts will be updated.
2187 * References: newcred must be an exclusive credential reference for the
2188 * duration of the call.
2191 change_ruid(struct ucred *newcred, struct uidinfo *ruip)
2194 (void)chgproccnt(newcred->cr_ruidinfo, -1, 0);
2195 newcred->cr_ruid = ruip->ui_uid;
2197 uifree(newcred->cr_ruidinfo);
2198 newcred->cr_ruidinfo = ruip;
2199 (void)chgproccnt(newcred->cr_ruidinfo, 1, 0);
2203 * Change a process's real gid.
2204 * Side effects: newcred->cr_rgid will be updated.
2205 * References: newcred must be an exclusive credential reference for the
2206 * duration of the call.
2209 change_rgid(struct ucred *newcred, gid_t rgid)
2212 newcred->cr_rgid = rgid;
2216 * Change a process's saved uid.
2217 * Side effects: newcred->cr_svuid will be updated.
2218 * References: newcred must be an exclusive credential reference for the
2219 * duration of the call.
2222 change_svuid(struct ucred *newcred, uid_t svuid)
2225 newcred->cr_svuid = svuid;
2229 * Change a process's saved gid.
2230 * Side effects: newcred->cr_svgid will be updated.
2231 * References: newcred must be an exclusive credential reference for the
2232 * duration of the call.
2235 change_svgid(struct ucred *newcred, gid_t svgid)
2238 newcred->cr_svgid = svgid;