2 * Copyright (c) 1982, 1986, 1989, 1990, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)kern_prot.c 8.6 (Berkeley) 1/21/94
39 * $FreeBSD: src/sys/kern/kern_prot.c,v 1.53.2.9 2002/03/09 05:20:26 dd Exp $
43 * System calls related to processes and protection
46 #include "opt_compat.h"
48 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/sysproto.h>
52 #include <sys/kernel.h>
54 #include <sys/malloc.h>
55 #include <sys/pioctl.h>
56 #include <sys/resourcevar.h>
58 static MALLOC_DEFINE(M_CRED, "cred", "credentials");
60 #ifndef _SYS_SYSPROTO_H_
67 * NOT MP SAFE due to p_pptr access
73 struct getpid_args *uap;
76 p->p_retval[0] = p->p_pid;
77 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
78 p->p_retval[1] = p->p_pptr->p_pid;
83 #ifndef _SYS_SYSPROTO_H_
92 struct getppid_args *uap;
95 p->p_retval[0] = p->p_pptr->p_pid;
100 * Get process group ID; note that POSIX getpgrp takes no parameter
104 #ifndef _SYS_SYSPROTO_H_
105 struct getpgrp_args {
113 struct getpgrp_args *uap;
116 p->p_retval[0] = p->p_pgrp->pg_id;
120 /* Get an arbitary pid's process group id */
121 #ifndef _SYS_SYSPROTO_H_
122 struct getpgid_args {
130 struct getpgid_args *uap;
138 if ((pt = pfind(uap->pid)) == 0)
141 p->p_retval[0] = pt->p_pgrp->pg_id;
146 * Get an arbitary pid's session id.
148 #ifndef _SYS_SYSPROTO_H_
157 struct getsid_args *uap;
165 if ((pt = pfind(uap->pid)) == 0)
168 p->p_retval[0] = pt->p_session->s_sid;
176 #ifndef _SYS_SYSPROTO_H_
186 struct getuid_args *uap;
189 p->p_retval[0] = p->p_cred->p_ruid;
190 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
191 p->p_retval[1] = p->p_ucred->cr_uid;
197 * geteuid() - MP SAFE
199 #ifndef _SYS_SYSPROTO_H_
200 struct geteuid_args {
209 struct geteuid_args *uap;
212 p->p_retval[0] = p->p_ucred->cr_uid;
219 #ifndef _SYS_SYSPROTO_H_
229 struct getgid_args *uap;
232 p->p_retval[0] = p->p_cred->p_rgid;
233 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
234 p->p_retval[1] = p->p_ucred->cr_groups[0];
240 * Get effective group ID. The "egid" is groups[0], and could be obtained
241 * via getgroups. This syscall exists because it is somewhat painful to do
242 * correctly in a library function.
244 #ifndef _SYS_SYSPROTO_H_
245 struct getegid_args {
254 struct getegid_args *uap;
257 p->p_retval[0] = p->p_ucred->cr_groups[0];
261 #ifndef _SYS_SYSPROTO_H_
262 struct getgroups_args {
270 register struct getgroups_args *uap;
272 register struct pcred *pc = p->p_cred;
276 if ((ngrp = uap->gidsetsize) == 0) {
277 p->p_retval[0] = pc->pc_ucred->cr_ngroups;
280 if (ngrp < pc->pc_ucred->cr_ngroups)
282 ngrp = pc->pc_ucred->cr_ngroups;
283 if ((error = copyout((caddr_t)pc->pc_ucred->cr_groups,
284 (caddr_t)uap->gidset, ngrp * sizeof(gid_t))))
286 p->p_retval[0] = ngrp;
290 #ifndef _SYS_SYSPROTO_H_
299 register struct proc *p;
300 struct setsid_args *uap;
303 if (p->p_pgid == p->p_pid || pgfind(p->p_pid)) {
306 (void)enterpgrp(p, p->p_pid, 1);
307 p->p_retval[0] = p->p_pid;
313 * set process group (setpgid/old setpgrp)
315 * caller does setpgid(targpid, targpgid)
317 * pid must be caller or child of caller (ESRCH)
319 * pid must be in same session (EPERM)
320 * pid can't have done an exec (EACCES)
322 * there must exist some pid in same session having pgid (EPERM)
323 * pid must not be session leader (EPERM)
325 #ifndef _SYS_SYSPROTO_H_
326 struct setpgid_args {
327 int pid; /* target process id */
328 int pgid; /* target pgrp id */
335 register struct setpgid_args *uap;
337 register struct proc *targp; /* target process */
338 register struct pgrp *pgrp; /* target pgrp */
342 if (uap->pid != 0 && uap->pid != curp->p_pid) {
343 if ((targp = pfind(uap->pid)) == 0 || !inferior(targp))
345 if (targp->p_pgrp == NULL || targp->p_session != curp->p_session)
347 if (targp->p_flag & P_EXEC)
351 if (SESS_LEADER(targp))
354 uap->pgid = targp->p_pid;
355 else if (uap->pgid != targp->p_pid)
356 if ((pgrp = pgfind(uap->pgid)) == 0 ||
357 pgrp->pg_session != curp->p_session)
359 return (enterpgrp(targp, uap->pgid, 0));
363 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
364 * compatable. It says that setting the uid/gid to euid/egid is a special
365 * case of "appropriate privilege". Once the rules are expanded out, this
366 * basically means that setuid(nnn) sets all three id's, in all permitted
367 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
368 * does not set the saved id - this is dangerous for traditional BSD
369 * programs. For this reason, we *really* do not want to set
370 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
372 #define POSIX_APPENDIX_B_4_2_2
374 #ifndef _SYS_SYSPROTO_H_
383 struct setuid_args *uap;
385 register struct pcred *pc = p->p_cred;
390 * See if we have "permission" by POSIX 1003.1 rules.
392 * Note that setuid(geteuid()) is a special case of
393 * "appropriate privileges" in appendix B.4.2.2. We need
394 * to use this clause to be compatable with traditional BSD
395 * semantics. Basically, it means that "setuid(xx)" sets all
396 * three id's (assuming you have privs).
398 * Notes on the logic. We do things in three steps.
399 * 1: We determine if the euid is going to change, and do EPERM
400 * right away. We unconditionally change the euid later if this
401 * test is satisfied, simplifying that part of the logic.
402 * 2: We determine if the real and/or saved uid's are going to
403 * change. Determined by compile options.
404 * 3: Change euid last. (after tests in #2 for "appropriate privs")
407 if (uid != pc->p_ruid && /* allow setuid(getuid()) */
408 #ifdef _POSIX_SAVED_IDS
409 uid != pc->p_svuid && /* allow setuid(saved gid) */
411 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
412 uid != pc->pc_ucred->cr_uid && /* allow setuid(geteuid()) */
414 (error = suser_xxx(0, p, PRISON_ROOT)))
417 #ifdef _POSIX_SAVED_IDS
419 * Do we have "appropriate privileges" (are we root or uid == euid)
420 * If so, we are changing the real uid and/or saved uid.
423 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
424 uid == pc->pc_ucred->cr_uid ||
426 suser_xxx(0, p, PRISON_ROOT) == 0) /* we are using privs */
430 * Set the real uid and transfer proc count to new user.
432 if (uid != pc->p_ruid) {
439 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
440 * the security of seteuid() depends on it. B.4.2.2 says it
441 * is important that we should do this.
443 if (pc->p_svuid != uid) {
450 * In all permitted cases, we are changing the euid.
451 * Copy credentials so other references do not see our changes.
453 if (pc->pc_ucred->cr_uid != uid) {
460 #ifndef _SYS_SYSPROTO_H_
461 struct seteuid_args {
469 struct seteuid_args *uap;
471 register struct pcred *pc = p->p_cred;
476 if (euid != pc->p_ruid && /* allow seteuid(getuid()) */
477 euid != pc->p_svuid && /* allow seteuid(saved uid) */
478 (error = suser_xxx(0, p, PRISON_ROOT)))
481 * Everything's okay, do it. Copy credentials so other references do
482 * not see our changes.
484 if (pc->pc_ucred->cr_uid != euid) {
485 change_euid(p, euid);
491 #ifndef _SYS_SYSPROTO_H_
500 struct setgid_args *uap;
502 register struct pcred *pc = p->p_cred;
507 * See if we have "permission" by POSIX 1003.1 rules.
509 * Note that setgid(getegid()) is a special case of
510 * "appropriate privileges" in appendix B.4.2.2. We need
511 * to use this clause to be compatable with traditional BSD
512 * semantics. Basically, it means that "setgid(xx)" sets all
513 * three id's (assuming you have privs).
515 * For notes on the logic here, see setuid() above.
518 if (gid != pc->p_rgid && /* allow setgid(getgid()) */
519 #ifdef _POSIX_SAVED_IDS
520 gid != pc->p_svgid && /* allow setgid(saved gid) */
522 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
523 gid != pc->pc_ucred->cr_groups[0] && /* allow setgid(getegid()) */
525 (error = suser_xxx(0, p, PRISON_ROOT)))
528 #ifdef _POSIX_SAVED_IDS
530 * Do we have "appropriate privileges" (are we root or gid == egid)
531 * If so, we are changing the real uid and saved gid.
534 #ifdef POSIX_APPENDIX_B_4_2_2 /* use the clause from B.4.2.2 */
535 gid == pc->pc_ucred->cr_groups[0] ||
537 suser_xxx(0, p, PRISON_ROOT) == 0) /* we are using privs */
543 if (pc->p_rgid != gid) {
550 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
551 * the security of setegid() depends on it. B.4.2.2 says it
552 * is important that we should do this.
554 if (pc->p_svgid != gid) {
560 * In all cases permitted cases, we are changing the egid.
561 * Copy credentials so other references do not see our changes.
563 if (pc->pc_ucred->cr_groups[0] != gid) {
564 pc->pc_ucred = crcopy(pc->pc_ucred);
565 pc->pc_ucred->cr_groups[0] = gid;
571 #ifndef _SYS_SYSPROTO_H_
572 struct setegid_args {
580 struct setegid_args *uap;
582 register struct pcred *pc = p->p_cred;
587 if (egid != pc->p_rgid && /* allow setegid(getgid()) */
588 egid != pc->p_svgid && /* allow setegid(saved gid) */
589 (error = suser_xxx(0, p, PRISON_ROOT)))
591 if (pc->pc_ucred->cr_groups[0] != egid) {
592 pc->pc_ucred = crcopy(pc->pc_ucred);
593 pc->pc_ucred->cr_groups[0] = egid;
599 #ifndef _SYS_SYSPROTO_H_
600 struct setgroups_args {
609 struct setgroups_args *uap;
611 register struct pcred *pc = p->p_cred;
615 if ((error = suser_xxx(0, p, PRISON_ROOT)))
617 ngrp = uap->gidsetsize;
621 * XXX A little bit lazy here. We could test if anything has
622 * changed before crcopy() and setting P_SUGID.
624 pc->pc_ucred = crcopy(pc->pc_ucred);
627 * setgroups(0, NULL) is a legitimate way of clearing the
628 * groups vector on non-BSD systems (which generally do not
629 * have the egid in the groups[0]). We risk security holes
630 * when running non-BSD software if we do not do the same.
632 pc->pc_ucred->cr_ngroups = 1;
634 if ((error = copyin((caddr_t)uap->gidset,
635 (caddr_t)pc->pc_ucred->cr_groups, ngrp * sizeof(gid_t))))
637 pc->pc_ucred->cr_ngroups = ngrp;
643 #ifndef _SYS_SYSPROTO_H_
644 struct setreuid_args {
652 register struct proc *p;
653 struct setreuid_args *uap;
655 register struct pcred *pc = p->p_cred;
656 register uid_t ruid, euid;
661 if (((ruid != (uid_t)-1 && ruid != pc->p_ruid && ruid != pc->p_svuid) ||
662 (euid != (uid_t)-1 && euid != pc->pc_ucred->cr_uid &&
663 euid != pc->p_ruid && euid != pc->p_svuid)) &&
664 (error = suser_xxx(0, p, PRISON_ROOT)) != 0)
667 if (euid != (uid_t)-1 && pc->pc_ucred->cr_uid != euid) {
668 change_euid(p, euid);
671 if (ruid != (uid_t)-1 && pc->p_ruid != ruid) {
672 change_ruid(p, ruid);
675 if ((ruid != (uid_t)-1 || pc->pc_ucred->cr_uid != pc->p_ruid) &&
676 pc->p_svuid != pc->pc_ucred->cr_uid) {
677 pc->p_svuid = pc->pc_ucred->cr_uid;
683 #ifndef _SYS_SYSPROTO_H_
684 struct setregid_args {
692 register struct proc *p;
693 struct setregid_args *uap;
695 register struct pcred *pc = p->p_cred;
696 register gid_t rgid, egid;
701 if (((rgid != (gid_t)-1 && rgid != pc->p_rgid && rgid != pc->p_svgid) ||
702 (egid != (gid_t)-1 && egid != pc->pc_ucred->cr_groups[0] &&
703 egid != pc->p_rgid && egid != pc->p_svgid)) &&
704 (error = suser_xxx(0, p, PRISON_ROOT)) != 0)
707 if (egid != (gid_t)-1 && pc->pc_ucred->cr_groups[0] != egid) {
708 pc->pc_ucred = crcopy(pc->pc_ucred);
709 pc->pc_ucred->cr_groups[0] = egid;
712 if (rgid != (gid_t)-1 && pc->p_rgid != rgid) {
716 if ((rgid != (gid_t)-1 || pc->pc_ucred->cr_groups[0] != pc->p_rgid) &&
717 pc->p_svgid != pc->pc_ucred->cr_groups[0]) {
718 pc->p_svgid = pc->pc_ucred->cr_groups[0];
725 * setresuid(ruid, euid, suid) is like setreuid except control over the
726 * saved uid is explicit.
729 #ifndef _SYS_SYSPROTO_H_
730 struct setresuid_args {
739 register struct proc *p;
740 struct setresuid_args *uap;
742 register struct pcred *pc = p->p_cred;
743 register uid_t ruid, euid, suid;
749 if (((ruid != (uid_t)-1 && ruid != pc->p_ruid && ruid != pc->p_svuid &&
750 ruid != pc->pc_ucred->cr_uid) ||
751 (euid != (uid_t)-1 && euid != pc->p_ruid && euid != pc->p_svuid &&
752 euid != pc->pc_ucred->cr_uid) ||
753 (suid != (uid_t)-1 && suid != pc->p_ruid && suid != pc->p_svuid &&
754 suid != pc->pc_ucred->cr_uid)) &&
755 (error = suser_xxx(0, p, PRISON_ROOT)) != 0)
757 if (euid != (uid_t)-1 && pc->pc_ucred->cr_uid != euid) {
758 change_euid(p, euid);
761 if (ruid != (uid_t)-1 && pc->p_ruid != ruid) {
762 change_ruid(p, ruid);
765 if (suid != (uid_t)-1 && pc->p_svuid != suid) {
773 * setresgid(rgid, egid, sgid) is like setregid except control over the
774 * saved gid is explicit.
777 #ifndef _SYS_SYSPROTO_H_
778 struct setresgid_args {
787 register struct proc *p;
788 struct setresgid_args *uap;
790 register struct pcred *pc = p->p_cred;
791 register gid_t rgid, egid, sgid;
797 if (((rgid != (gid_t)-1 && rgid != pc->p_rgid && rgid != pc->p_svgid &&
798 rgid != pc->pc_ucred->cr_groups[0]) ||
799 (egid != (gid_t)-1 && egid != pc->p_rgid && egid != pc->p_svgid &&
800 egid != pc->pc_ucred->cr_groups[0]) ||
801 (sgid != (gid_t)-1 && sgid != pc->p_rgid && sgid != pc->p_svgid &&
802 sgid != pc->pc_ucred->cr_groups[0])) &&
803 (error = suser_xxx(0, p, PRISON_ROOT)) != 0)
806 if (egid != (gid_t)-1 && pc->pc_ucred->cr_groups[0] != egid) {
807 pc->pc_ucred = crcopy(pc->pc_ucred);
808 pc->pc_ucred->cr_groups[0] = egid;
811 if (rgid != (gid_t)-1 && pc->p_rgid != rgid) {
815 if (sgid != (gid_t)-1 && pc->p_svgid != sgid) {
822 #ifndef _SYS_SYSPROTO_H_
823 struct getresuid_args {
832 register struct proc *p;
833 struct getresuid_args *uap;
835 struct pcred *pc = p->p_cred;
836 int error1 = 0, error2 = 0, error3 = 0;
839 error1 = copyout((caddr_t)&pc->p_ruid,
840 (caddr_t)uap->ruid, sizeof(pc->p_ruid));
842 error2 = copyout((caddr_t)&pc->pc_ucred->cr_uid,
843 (caddr_t)uap->euid, sizeof(pc->pc_ucred->cr_uid));
845 error3 = copyout((caddr_t)&pc->p_svuid,
846 (caddr_t)uap->suid, sizeof(pc->p_svuid));
847 return error1 ? error1 : (error2 ? error2 : error3);
850 #ifndef _SYS_SYSPROTO_H_
851 struct getresgid_args {
860 register struct proc *p;
861 struct getresgid_args *uap;
863 struct pcred *pc = p->p_cred;
864 int error1 = 0, error2 = 0, error3 = 0;
867 error1 = copyout((caddr_t)&pc->p_rgid,
868 (caddr_t)uap->rgid, sizeof(pc->p_rgid));
870 error2 = copyout((caddr_t)&pc->pc_ucred->cr_groups[0],
871 (caddr_t)uap->egid, sizeof(pc->pc_ucred->cr_groups[0]));
873 error3 = copyout((caddr_t)&pc->p_svgid,
874 (caddr_t)uap->sgid, sizeof(pc->p_svgid));
875 return error1 ? error1 : (error2 ? error2 : error3);
879 #ifndef _SYS_SYSPROTO_H_
880 struct issetugid_args {
887 register struct proc *p;
888 struct issetugid_args *uap;
891 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
892 * we use P_SUGID because we consider changing the owners as
893 * "tainting" as well.
894 * This is significant for procs that start as root and "become"
895 * a user without an exec - programs cannot know *everything*
896 * that libc *might* have put in their data segment.
898 p->p_retval[0] = (p->p_flag & P_SUGID) ? 1 : 0;
903 * Check if gid is a member of the group set.
906 groupmember(gid, cred)
908 register struct ucred *cred;
913 egp = &(cred->cr_groups[cred->cr_ngroups]);
914 for (gp = cred->cr_groups; gp < egp; gp++)
921 * Test whether the specified credentials imply "super-user"
922 * privilege; if so, and we have accounting info, set the flag
923 * indicating use of super-powers.
924 * Returns 0 or error.
930 return suser_xxx(0, p, 0);
934 suser_xxx(cred, proc, flag)
939 if (!cred && !proc) {
940 printf("suser_xxx(): THINK!\n");
944 cred = proc->p_ucred;
945 if (cred->cr_uid != 0)
947 if (proc && proc->p_prison && !(flag & PRISON_ROOT))
950 proc->p_acflag |= ASU;
955 * Return zero if p1 can fondle p2, return errno (EPERM/ESRCH) otherwise.
959 p_trespass(struct proc *p1, struct proc *p2)
964 if (!PRISON_CHECK(p1, p2))
966 if (p1->p_cred->p_ruid == p2->p_cred->p_ruid)
968 if (p1->p_ucred->cr_uid == p2->p_cred->p_ruid)
970 if (p1->p_cred->p_ruid == p2->p_ucred->cr_uid)
972 if (p1->p_ucred->cr_uid == p2->p_ucred->cr_uid)
974 if (!suser_xxx(0, p1, PRISON_ROOT))
980 * Allocate a zeroed cred structure.
985 register struct ucred *cr;
987 MALLOC(cr, struct ucred *, sizeof(*cr), M_CRED, M_WAITOK);
988 bzero((caddr_t)cr, sizeof(*cr));
994 * Claim another reference to a ucred structure
1004 * Free a cred structure.
1005 * Throws away space when ref count gets to 0.
1011 if (cr->cr_ref == 0)
1012 panic("Freeing already free credential! %p", cr);
1014 if (--cr->cr_ref == 0) {
1016 * Some callers of crget(), such as nfs_statfs(),
1017 * allocate a temporary credential, but don't
1018 * allocate a uidinfo structure.
1020 if (cr->cr_uidinfo != NULL)
1021 uifree(cr->cr_uidinfo);
1022 FREE((caddr_t)cr, M_CRED);
1027 * Copy cred structure to a new one and free the old one.
1033 struct ucred *newcr;
1035 if (cr->cr_ref == 1)
1039 uihold(newcr->cr_uidinfo);
1046 * Dup cred struct to a new held one.
1052 struct ucred *newcr;
1056 uihold(newcr->cr_uidinfo);
1062 * Fill in a struct xucred based on a struct ucred.
1070 bzero(xcr, sizeof(*xcr));
1071 xcr->cr_version = XUCRED_VERSION;
1072 xcr->cr_uid = cr->cr_uid;
1073 xcr->cr_ngroups = cr->cr_ngroups;
1074 bcopy(cr->cr_groups, xcr->cr_groups, sizeof(cr->cr_groups));
1078 * Get login name, if available.
1080 #ifndef _SYS_SYSPROTO_H_
1081 struct getlogin_args {
1090 struct getlogin_args *uap;
1093 if (uap->namelen > MAXLOGNAME)
1094 uap->namelen = MAXLOGNAME;
1095 return (copyout((caddr_t) p->p_pgrp->pg_session->s_login,
1096 (caddr_t) uap->namebuf, uap->namelen));
1102 #ifndef _SYS_SYSPROTO_H_
1103 struct setlogin_args {
1111 struct setlogin_args *uap;
1114 char logintmp[MAXLOGNAME];
1116 if ((error = suser_xxx(0, p, PRISON_ROOT)))
1118 error = copyinstr((caddr_t) uap->namebuf, (caddr_t) logintmp,
1119 sizeof(logintmp), (size_t *)0);
1120 if (error == ENAMETOOLONG)
1123 (void) memcpy(p->p_pgrp->pg_session->s_login, logintmp,
1132 p->p_flag |= P_SUGID;
1133 if (!(p->p_pfsflags & PF_ISUGID))
1138 * Helper function to change the effective uid of a process
1141 change_euid(p, euid)
1146 struct uidinfo *uip;
1150 * crcopy is essentially a NOP if ucred has a reference count
1151 * of 1, which is true if it has already been copied.
1153 pc->pc_ucred = crcopy(pc->pc_ucred);
1154 uip = pc->pc_ucred->cr_uidinfo;
1155 pc->pc_ucred->cr_uid = euid;
1156 pc->pc_ucred->cr_uidinfo = uifind(euid);
1161 * Helper function to change the real uid of a process
1163 * The per-uid process count for this process is transfered from
1164 * the old uid to the new uid.
1167 change_ruid(p, ruid)
1172 struct uidinfo *uip;
1175 (void)chgproccnt(pc->p_uidinfo, -1, 0);
1176 uip = pc->p_uidinfo;
1177 /* It is assumed that pcred is not shared between processes */
1179 pc->p_uidinfo = uifind(ruid);
1180 (void)chgproccnt(pc->p_uidinfo, 1, 0);