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 $
40 * $DragonFly: src/sys/kern/kern_prot.c,v 1.23 2006/03/23 20:55:07 drhodus Exp $
44 * System calls related to processes and protection
47 #include "opt_compat.h"
49 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/sysproto.h>
53 #include <sys/kernel.h>
55 #include <sys/malloc.h>
56 #include <sys/pioctl.h>
57 #include <sys/resourcevar.h>
58 #include <sys/thread2.h>
60 #include <sys/lockf.h>
62 static MALLOC_DEFINE(M_CRED, "cred", "credentials");
65 * NOT MP SAFE due to p_pptr access
69 getpid(struct getpid_args *uap)
71 struct proc *p = curproc;
73 uap->sysmsg_fds[0] = p->p_pid;
74 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
75 uap->sysmsg_fds[1] = p->p_pptr->p_pid;
82 getppid(struct getppid_args *uap)
84 struct proc *p = curproc;
86 uap->sysmsg_result = p->p_pptr->p_pid;
91 * Get process group ID; note that POSIX getpgrp takes no parameter
96 getpgrp(struct getpgrp_args *uap)
98 struct proc *p = curproc;
100 uap->sysmsg_result = p->p_pgrp->pg_id;
105 * Get an arbitary pid's process group id
108 getpgid(struct getpgid_args *uap)
110 struct proc *p = curproc;
117 if ((pt = pfind(uap->pid)) == 0)
120 uap->sysmsg_result = pt->p_pgrp->pg_id;
125 * Get an arbitary pid's session id.
128 getsid(struct getsid_args *uap)
130 struct proc *p = curproc;
137 if ((pt = pfind(uap->pid)) == 0)
140 uap->sysmsg_result = pt->p_session->s_sid;
150 getuid(struct getuid_args *uap)
152 struct proc *p = curproc;
154 uap->sysmsg_fds[0] = p->p_ucred->cr_ruid;
155 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
156 uap->sysmsg_fds[1] = p->p_ucred->cr_uid;
162 * geteuid() - MP SAFE
166 geteuid(struct geteuid_args *uap)
168 struct proc *p = curproc;
170 uap->sysmsg_result = p->p_ucred->cr_uid;
179 getgid(struct getgid_args *uap)
181 struct proc *p = curproc;
183 uap->sysmsg_fds[0] = p->p_ucred->cr_rgid;
184 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
185 uap->sysmsg_fds[1] = p->p_ucred->cr_groups[0];
191 * Get effective group ID. The "egid" is groups[0], and could be obtained
192 * via getgroups. This syscall exists because it is somewhat painful to do
193 * correctly in a library function.
199 getegid(struct getegid_args *uap)
201 struct proc *p = curproc;
203 uap->sysmsg_result = p->p_ucred->cr_groups[0];
208 getgroups(struct getgroups_args *uap)
210 struct proc *p = curproc;
215 if (p == NULL) /* API enforcement */
219 if ((ngrp = uap->gidsetsize) == 0) {
220 uap->sysmsg_result = cr->cr_ngroups;
223 if (ngrp < cr->cr_ngroups)
225 ngrp = cr->cr_ngroups;
226 if ((error = copyout((caddr_t)cr->cr_groups,
227 (caddr_t)uap->gidset, ngrp * sizeof(gid_t))))
229 uap->sysmsg_result = ngrp;
235 setsid(struct setsid_args *uap)
237 struct proc *p = curproc;
239 if (p->p_pgid == p->p_pid || pgfind(p->p_pid)) {
242 (void)enterpgrp(p, p->p_pid, 1);
243 uap->sysmsg_result = p->p_pid;
249 * set process group (setpgid/old setpgrp)
251 * caller does setpgid(targpid, targpgid)
253 * pid must be caller or child of caller (ESRCH)
255 * pid must be in same session (EPERM)
256 * pid can't have done an exec (EACCES)
258 * there must exist some pid in same session having pgid (EPERM)
259 * pid must not be session leader (EPERM)
263 setpgid(struct setpgid_args *uap)
265 struct proc *curp = curproc;
266 struct proc *targp; /* target process */
267 struct pgrp *pgrp; /* target pgrp */
271 if (uap->pid != 0 && uap->pid != curp->p_pid) {
272 if ((targp = pfind(uap->pid)) == 0 || !inferior(targp))
274 if (targp->p_pgrp == NULL || targp->p_session != curp->p_session)
276 if (targp->p_flag & P_EXEC)
280 if (SESS_LEADER(targp))
283 uap->pgid = targp->p_pid;
284 else if (uap->pgid != targp->p_pid)
285 if ((pgrp = pgfind(uap->pgid)) == 0 ||
286 pgrp->pg_session != curp->p_session)
288 return (enterpgrp(targp, uap->pgid, 0));
292 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
293 * compatible. It says that setting the uid/gid to euid/egid is a special
294 * case of "appropriate privilege". Once the rules are expanded out, this
295 * basically means that setuid(nnn) sets all three id's, in all permitted
296 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
297 * does not set the saved id - this is dangerous for traditional BSD
298 * programs. For this reason, we *really* do not want to set
299 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
301 #define POSIX_APPENDIX_B_4_2_2
305 setuid(struct setuid_args *uap)
307 struct proc *p = curproc;
312 if (p == NULL) /* API enforcement */
317 * See if we have "permission" by POSIX 1003.1 rules.
319 * Note that setuid(geteuid()) is a special case of
320 * "appropriate privileges" in appendix B.4.2.2. We need
321 * to use this clause to be compatible with traditional BSD
322 * semantics. Basically, it means that "setuid(xx)" sets all
323 * three id's (assuming you have privs).
325 * Notes on the logic. We do things in three steps.
326 * 1: We determine if the euid is going to change, and do EPERM
327 * right away. We unconditionally change the euid later if this
328 * test is satisfied, simplifying that part of the logic.
329 * 2: We determine if the real and/or saved uid's are going to
330 * change. Determined by compile options.
331 * 3: Change euid last. (after tests in #2 for "appropriate privs")
334 if (uid != cr->cr_ruid && /* allow setuid(getuid()) */
335 #ifdef _POSIX_SAVED_IDS
336 uid != crc->cr_svuid && /* allow setuid(saved gid) */
338 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
339 uid != cr->cr_uid && /* allow setuid(geteuid()) */
341 (error = suser_cred(cr, PRISON_ROOT)))
344 #ifdef _POSIX_SAVED_IDS
346 * Do we have "appropriate privileges" (are we root or uid == euid)
347 * If so, we are changing the real uid and/or saved uid.
350 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
353 suser_cred(cr, PRISON_ROOT) == 0) /* we are using privs */
357 * Set the real uid and transfer proc count to new user.
359 if (uid != cr->cr_ruid) {
366 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
367 * the security of seteuid() depends on it. B.4.2.2 says it
368 * is important that we should do this.
370 if (cr->cr_svuid != uid) {
371 cr = cratom(&p->p_ucred);
378 * In all permitted cases, we are changing the euid.
379 * Copy credentials so other references do not see our changes.
381 if (cr->cr_uid != uid) {
390 seteuid(struct seteuid_args *uap)
392 struct proc *p = curproc;
397 if (p == NULL) /* API enforcement */
402 if (euid != cr->cr_ruid && /* allow seteuid(getuid()) */
403 euid != cr->cr_svuid && /* allow seteuid(saved uid) */
404 (error = suser_cred(cr, PRISON_ROOT)))
407 * Everything's okay, do it. Copy credentials so other references do
408 * not see our changes.
410 if (cr->cr_uid != euid) {
419 setgid(struct setgid_args *uap)
421 struct proc *p = curproc;
426 if (p == NULL) /* API enforcement */
431 * See if we have "permission" by POSIX 1003.1 rules.
433 * Note that setgid(getegid()) is a special case of
434 * "appropriate privileges" in appendix B.4.2.2. We need
435 * to use this clause to be compatible with traditional BSD
436 * semantics. Basically, it means that "setgid(xx)" sets all
437 * three id's (assuming you have privs).
439 * For notes on the logic here, see setuid() above.
442 if (gid != cr->cr_rgid && /* allow setgid(getgid()) */
443 #ifdef _POSIX_SAVED_IDS
444 gid != cr->cr_svgid && /* allow setgid(saved gid) */
446 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
447 gid != cr->cr_groups[0] && /* allow setgid(getegid()) */
449 (error = suser_cred(cr, PRISON_ROOT)))
452 #ifdef _POSIX_SAVED_IDS
454 * Do we have "appropriate privileges" (are we root or gid == egid)
455 * If so, we are changing the real uid and saved gid.
458 #ifdef POSIX_APPENDIX_B_4_2_2 /* use the clause from B.4.2.2 */
459 gid == cr->cr_groups[0] ||
461 suser_cred(cr, PRISON_ROOT) == 0) /* we are using privs */
467 if (cr->cr_rgid != gid) {
468 cr = cratom(&p->p_ucred);
475 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
476 * the security of setegid() depends on it. B.4.2.2 says it
477 * is important that we should do this.
479 if (cr->cr_svgid != gid) {
480 cr = cratom(&p->p_ucred);
486 * In all cases permitted cases, we are changing the egid.
487 * Copy credentials so other references do not see our changes.
489 if (cr->cr_groups[0] != gid) {
490 cr = cratom(&p->p_ucred);
491 cr->cr_groups[0] = gid;
499 setegid(struct setegid_args *uap)
501 struct proc *p = curproc;
506 if (p == NULL) /* API enforcement */
511 if (egid != cr->cr_rgid && /* allow setegid(getgid()) */
512 egid != cr->cr_svgid && /* allow setegid(saved gid) */
513 (error = suser_cred(cr, PRISON_ROOT)))
515 if (cr->cr_groups[0] != egid) {
516 cr = cratom(&p->p_ucred);
517 cr->cr_groups[0] = egid;
525 setgroups(struct setgroups_args *uap)
527 struct proc *p = curproc;
532 if (p == NULL) /* API enforcement */
536 if ((error = suser_cred(cr, PRISON_ROOT)))
538 ngrp = uap->gidsetsize;
542 * XXX A little bit lazy here. We could test if anything has
543 * changed before cratom() and setting P_SUGID.
545 cr = cratom(&p->p_ucred);
548 * setgroups(0, NULL) is a legitimate way of clearing the
549 * groups vector on non-BSD systems (which generally do not
550 * have the egid in the groups[0]). We risk security holes
551 * when running non-BSD software if we do not do the same.
555 if ((error = copyin((caddr_t)uap->gidset,
556 (caddr_t)cr->cr_groups, ngrp * sizeof(gid_t))))
558 cr->cr_ngroups = ngrp;
566 setreuid(struct setreuid_args *uap)
568 struct proc *p = curproc;
573 if (p == NULL) /* API enforcement */
579 if (((ruid != (uid_t)-1 && ruid != cr->cr_ruid && ruid != cr->cr_svuid) ||
580 (euid != (uid_t)-1 && euid != cr->cr_uid &&
581 euid != cr->cr_ruid && euid != cr->cr_svuid)) &&
582 (error = suser_cred(cr, PRISON_ROOT)) != 0)
585 if (euid != (uid_t)-1 && cr->cr_uid != euid) {
589 if (ruid != (uid_t)-1 && cr->cr_ruid != ruid) {
593 if ((ruid != (uid_t)-1 || cr->cr_uid != cr->cr_ruid) &&
594 cr->cr_svuid != cr->cr_uid) {
595 cr = cratom(&p->p_ucred);
596 cr->cr_svuid = cr->cr_uid;
604 setregid(struct setregid_args *uap)
606 struct proc *p = curproc;
611 if (p == NULL) /* API enforcement */
617 if (((rgid != (gid_t)-1 && rgid != cr->cr_rgid && rgid != cr->cr_svgid) ||
618 (egid != (gid_t)-1 && egid != cr->cr_groups[0] &&
619 egid != cr->cr_rgid && egid != cr->cr_svgid)) &&
620 (error = suser_cred(cr, PRISON_ROOT)) != 0)
623 if (egid != (gid_t)-1 && cr->cr_groups[0] != egid) {
624 cr = cratom(&p->p_ucred);
625 cr->cr_groups[0] = egid;
628 if (rgid != (gid_t)-1 && cr->cr_rgid != rgid) {
629 cr = cratom(&p->p_ucred);
633 if ((rgid != (gid_t)-1 || cr->cr_groups[0] != cr->cr_rgid) &&
634 cr->cr_svgid != cr->cr_groups[0]) {
635 cr = cratom(&p->p_ucred);
636 cr->cr_svgid = cr->cr_groups[0];
643 * setresuid(ruid, euid, suid) is like setreuid except control over the
644 * saved uid is explicit.
649 setresuid(struct setresuid_args *uap)
651 struct proc *p = curproc;
653 uid_t ruid, euid, suid;
660 if (((ruid != (uid_t)-1 && ruid != cr->cr_ruid && ruid != cr->cr_svuid &&
661 ruid != cr->cr_uid) ||
662 (euid != (uid_t)-1 && euid != cr->cr_ruid && euid != cr->cr_svuid &&
663 euid != cr->cr_uid) ||
664 (suid != (uid_t)-1 && suid != cr->cr_ruid && suid != cr->cr_svuid &&
665 suid != cr->cr_uid)) &&
666 (error = suser_cred(cr, PRISON_ROOT)) != 0)
668 if (euid != (uid_t)-1 && cr->cr_uid != euid) {
672 if (ruid != (uid_t)-1 && cr->cr_ruid != ruid) {
676 if (suid != (uid_t)-1 && cr->cr_svuid != suid) {
677 cr = cratom(&p->p_ucred);
685 * setresgid(rgid, egid, sgid) is like setregid except control over the
686 * saved gid is explicit.
691 setresgid(struct setresgid_args *uap)
693 struct proc *p = curproc;
695 gid_t rgid, egid, sgid;
702 if (((rgid != (gid_t)-1 && rgid != cr->cr_rgid && rgid != cr->cr_svgid &&
703 rgid != cr->cr_groups[0]) ||
704 (egid != (gid_t)-1 && egid != cr->cr_rgid && egid != cr->cr_svgid &&
705 egid != cr->cr_groups[0]) ||
706 (sgid != (gid_t)-1 && sgid != cr->cr_rgid && sgid != cr->cr_svgid &&
707 sgid != cr->cr_groups[0])) &&
708 (error = suser_cred(cr, PRISON_ROOT)) != 0)
711 if (egid != (gid_t)-1 && cr->cr_groups[0] != egid) {
712 cr = cratom(&p->p_ucred);
713 cr->cr_groups[0] = egid;
716 if (rgid != (gid_t)-1 && cr->cr_rgid != rgid) {
717 cr = cratom(&p->p_ucred);
721 if (sgid != (gid_t)-1 && cr->cr_svgid != sgid) {
722 cr = cratom(&p->p_ucred);
731 getresuid(struct getresuid_args *uap)
733 struct proc *p = curproc;
734 struct ucred *cr = p->p_ucred;
735 int error1 = 0, error2 = 0, error3 = 0;
738 error1 = copyout((caddr_t)&cr->cr_ruid,
739 (caddr_t)uap->ruid, sizeof(cr->cr_ruid));
741 error2 = copyout((caddr_t)&cr->cr_uid,
742 (caddr_t)uap->euid, sizeof(cr->cr_uid));
744 error3 = copyout((caddr_t)&cr->cr_svuid,
745 (caddr_t)uap->suid, sizeof(cr->cr_svuid));
746 return error1 ? error1 : (error2 ? error2 : error3);
751 getresgid(struct getresgid_args *uap)
753 struct proc *p = curproc;
754 struct ucred *cr = p->p_ucred;
755 int error1 = 0, error2 = 0, error3 = 0;
758 error1 = copyout((caddr_t)&cr->cr_rgid,
759 (caddr_t)uap->rgid, sizeof(cr->cr_rgid));
761 error2 = copyout((caddr_t)&cr->cr_groups[0],
762 (caddr_t)uap->egid, sizeof(cr->cr_groups[0]));
764 error3 = copyout((caddr_t)&cr->cr_svgid,
765 (caddr_t)uap->sgid, sizeof(cr->cr_svgid));
766 return error1 ? error1 : (error2 ? error2 : error3);
772 issetugid(struct issetugid_args *uap)
774 struct proc *p = curproc;
776 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
777 * we use P_SUGID because we consider changing the owners as
778 * "tainting" as well.
779 * This is significant for procs that start as root and "become"
780 * a user without an exec - programs cannot know *everything*
781 * that libc *might* have put in their data segment.
783 uap->sysmsg_result = (p->p_flag & P_SUGID) ? 1 : 0;
788 * Check if gid is a member of the group set.
791 groupmember(gid_t gid, struct ucred *cred)
796 egp = &(cred->cr_groups[cred->cr_ngroups]);
797 for (gp = cred->cr_groups; gp < egp; gp++) {
805 * Test whether the specified credentials imply "super-user"
806 * privilege; if so, and we have accounting info, set the flag
807 * indicating use of super-powers. A kernel thread without a process
808 * context is assumed to have super user capabilities. In situations
809 * where the caller always expect a cred to exist, the cred should be
810 * passed separately and suser_cred()should be used instead of suser().
812 * Returns 0 or error.
815 suser(struct thread *td)
817 struct proc *p = td->td_proc;
820 return suser_cred(p->p_ucred, 0);
827 * A non-null credential is expected unless NULL_CRED_OKAY is set.
830 suser_cred(struct ucred *cred, int flag)
832 KASSERT(cred != NULL || flag & NULL_CRED_OKAY,
833 ("suser_cred: NULL cred!"));
836 if (flag & NULL_CRED_OKAY)
841 if (cred->cr_uid != 0)
843 if (cred->cr_prison && !(flag & PRISON_ROOT))
845 /* NOTE: accounting for suser access (p_acflag/ASU) removed */
850 * Return zero if p1 can fondle p2, return errno (EPERM/ESRCH) otherwise.
853 p_trespass(struct ucred *cr1, struct ucred *cr2)
857 if (!PRISON_CHECK(cr1, cr2))
859 if (cr1->cr_ruid == cr2->cr_ruid)
861 if (cr1->cr_uid == cr2->cr_ruid)
863 if (cr1->cr_ruid == cr2->cr_uid)
865 if (cr1->cr_uid == cr2->cr_uid)
867 if (suser_cred(cr1, PRISON_ROOT) == 0)
873 * Allocate a zeroed cred structure.
880 MALLOC(cr, struct ucred *, sizeof(*cr), M_CRED, M_WAITOK);
881 bzero((caddr_t)cr, sizeof(*cr));
887 * Claim another reference to a ucred structure. Can be used with special
891 crhold(struct ucred *cr)
893 if (cr != NOCRED && cr != FSCRED)
899 * Free a cred structure.
900 * Throws away space when ref count gets to 0.
904 crfree(struct ucred *cr)
906 /* Protect crfree() as a critical section as there
907 * appears to be a crfree race which can occur on
912 panic("Freeing already free credential! %p", cr);
914 if (--cr->cr_ref == 0) {
916 * Some callers of crget(), such as nfs_statfs(),
917 * allocate a temporary credential, but don't
918 * allocate a uidinfo structure.
920 if (cr->cr_uidinfo != NULL) {
921 uidrop(cr->cr_uidinfo);
922 cr->cr_uidinfo = NULL;
924 if (cr->cr_ruidinfo != NULL) {
925 uidrop(cr->cr_ruidinfo);
926 cr->cr_ruidinfo = NULL;
930 * Destroy empty prisons
933 prison_free(cr->cr_prison);
934 cr->cr_prison = NULL; /* safety */
936 FREE((caddr_t)cr, M_CRED);
942 * Atomize a cred structure so it can be modified without polluting
943 * other references to it.
946 cratom(struct ucred **pcr)
952 if (oldcr->cr_ref == 1)
956 if (newcr->cr_uidinfo)
957 uihold(newcr->cr_uidinfo);
958 if (newcr->cr_ruidinfo)
959 uihold(newcr->cr_ruidinfo);
961 prison_hold(newcr->cr_prison);
968 #if 0 /* no longer used but keep around for a little while */
970 * Copy cred structure to a new one and free the old one.
973 crcopy(struct ucred *cr)
981 if (newcr->cr_uidinfo)
982 uihold(newcr->cr_uidinfo);
983 if (newcr->cr_ruidinfo)
984 uihold(newcr->cr_ruidinfo);
986 prison_hold(newcr->cr_prison);
994 * Dup cred struct to a new held one.
997 crdup(struct ucred *cr)
1003 if (newcr->cr_uidinfo)
1004 uihold(newcr->cr_uidinfo);
1005 if (newcr->cr_ruidinfo)
1006 uihold(newcr->cr_ruidinfo);
1008 prison_hold(newcr->cr_prison);
1014 * Fill in a struct xucred based on a struct ucred.
1017 cru2x(struct ucred *cr, struct xucred *xcr)
1020 bzero(xcr, sizeof(*xcr));
1021 xcr->cr_version = XUCRED_VERSION;
1022 xcr->cr_uid = cr->cr_uid;
1023 xcr->cr_ngroups = cr->cr_ngroups;
1024 bcopy(cr->cr_groups, xcr->cr_groups, sizeof(cr->cr_groups));
1028 * Get login name, if available.
1032 getlogin(struct getlogin_args *uap)
1034 struct proc *p = curproc;
1036 if (uap->namelen > MAXLOGNAME)
1037 uap->namelen = MAXLOGNAME;
1038 return (copyout((caddr_t) p->p_pgrp->pg_session->s_login,
1039 (caddr_t) uap->namebuf, uap->namelen));
1047 setlogin(struct setlogin_args *uap)
1049 struct proc *p = curproc;
1051 char logintmp[MAXLOGNAME];
1053 KKASSERT(p != NULL);
1054 if ((error = suser_cred(p->p_ucred, PRISON_ROOT)))
1056 error = copyinstr((caddr_t) uap->namebuf, (caddr_t) logintmp,
1057 sizeof(logintmp), (size_t *)0);
1058 if (error == ENAMETOOLONG)
1061 (void) memcpy(p->p_pgrp->pg_session->s_login, logintmp,
1069 struct proc *p = curproc;
1071 KKASSERT(p != NULL);
1072 p->p_flag |= P_SUGID;
1073 if (!(p->p_pfsflags & PF_ISUGID))
1078 * Helper function to change the effective uid of a process
1081 change_euid(uid_t euid)
1083 struct proc *p = curproc;
1086 KKASSERT(p != NULL);
1087 lf_count_adjust(p, 0);
1088 cr = cratom(&p->p_ucred);
1090 uireplace(&cr->cr_uidinfo, uifind(euid));
1091 lf_count_adjust(p, 1);
1095 * Helper function to change the real uid of a process
1097 * The per-uid process count for this process is transfered from
1098 * the old uid to the new uid.
1101 change_ruid(uid_t ruid)
1103 struct proc *p = curproc;
1106 KKASSERT(p != NULL);
1108 cr = cratom(&p->p_ucred);
1109 (void)chgproccnt(cr->cr_ruidinfo, -1, 0);
1110 /* It is assumed that pcred is not shared between processes */
1112 uireplace(&cr->cr_ruidinfo, uifind(ruid));
1113 (void)chgproccnt(cr->cr_ruidinfo, 1, 0);