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.10 2003/08/08 21:47:49 dillon 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>
60 static MALLOC_DEFINE(M_CRED, "cred", "credentials");
63 * NOT MP SAFE due to p_pptr access
67 getpid(struct getpid_args *uap)
69 struct proc *p = curproc;
71 uap->sysmsg_fds[0] = p->p_pid;
72 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
73 uap->sysmsg_fds[1] = p->p_pptr->p_pid;
80 getppid(struct getppid_args *uap)
82 struct proc *p = curproc;
84 uap->sysmsg_result = p->p_pptr->p_pid;
89 * Get process group ID; note that POSIX getpgrp takes no parameter
94 getpgrp(struct getpgrp_args *uap)
96 struct proc *p = curproc;
98 uap->sysmsg_result = p->p_pgrp->pg_id;
103 * Get an arbitary pid's process group id
106 getpgid(struct getpgid_args *uap)
108 struct proc *p = curproc;
115 if ((pt = pfind(uap->pid)) == 0)
118 uap->sysmsg_result = pt->p_pgrp->pg_id;
123 * Get an arbitary pid's session id.
126 getsid(struct getsid_args *uap)
128 struct proc *p = curproc;
135 if ((pt = pfind(uap->pid)) == 0)
138 uap->sysmsg_result = pt->p_session->s_sid;
148 getuid(struct getuid_args *uap)
150 struct proc *p = curproc;
152 uap->sysmsg_fds[0] = p->p_ucred->cr_ruid;
153 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
154 uap->sysmsg_fds[1] = p->p_ucred->cr_uid;
160 * geteuid() - MP SAFE
164 geteuid(struct geteuid_args *uap)
166 struct proc *p = curproc;
168 uap->sysmsg_result = p->p_ucred->cr_uid;
177 getgid(struct getgid_args *uap)
179 struct proc *p = curproc;
181 uap->sysmsg_fds[0] = p->p_ucred->cr_rgid;
182 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
183 uap->sysmsg_fds[1] = p->p_ucred->cr_groups[0];
189 * Get effective group ID. The "egid" is groups[0], and could be obtained
190 * via getgroups. This syscall exists because it is somewhat painful to do
191 * correctly in a library function.
195 getegid(struct getegid_args *uap)
197 struct proc *p = curproc;
199 uap->sysmsg_result = p->p_ucred->cr_groups[0];
204 getgroups(struct getgroups_args *uap)
206 struct proc *p = curproc;
211 if (p == NULL) /* API enforcement */
215 if ((ngrp = uap->gidsetsize) == 0) {
216 uap->sysmsg_result = cr->cr_ngroups;
219 if (ngrp < cr->cr_ngroups)
221 ngrp = cr->cr_ngroups;
222 if ((error = copyout((caddr_t)cr->cr_groups,
223 (caddr_t)uap->gidset, ngrp * sizeof(gid_t))))
225 uap->sysmsg_result = ngrp;
231 setsid(struct setsid_args *uap)
233 struct proc *p = curproc;
235 if (p->p_pgid == p->p_pid || pgfind(p->p_pid)) {
238 (void)enterpgrp(p, p->p_pid, 1);
239 uap->sysmsg_result = p->p_pid;
245 * set process group (setpgid/old setpgrp)
247 * caller does setpgid(targpid, targpgid)
249 * pid must be caller or child of caller (ESRCH)
251 * pid must be in same session (EPERM)
252 * pid can't have done an exec (EACCES)
254 * there must exist some pid in same session having pgid (EPERM)
255 * pid must not be session leader (EPERM)
259 setpgid(struct setpgid_args *uap)
261 struct proc *curp = curproc;
262 struct proc *targp; /* target process */
263 struct pgrp *pgrp; /* target pgrp */
267 if (uap->pid != 0 && uap->pid != curp->p_pid) {
268 if ((targp = pfind(uap->pid)) == 0 || !inferior(targp))
270 if (targp->p_pgrp == NULL || targp->p_session != curp->p_session)
272 if (targp->p_flag & P_EXEC)
276 if (SESS_LEADER(targp))
279 uap->pgid = targp->p_pid;
280 else if (uap->pgid != targp->p_pid)
281 if ((pgrp = pgfind(uap->pgid)) == 0 ||
282 pgrp->pg_session != curp->p_session)
284 return (enterpgrp(targp, uap->pgid, 0));
288 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
289 * compatable. It says that setting the uid/gid to euid/egid is a special
290 * case of "appropriate privilege". Once the rules are expanded out, this
291 * basically means that setuid(nnn) sets all three id's, in all permitted
292 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
293 * does not set the saved id - this is dangerous for traditional BSD
294 * programs. For this reason, we *really* do not want to set
295 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
297 #define POSIX_APPENDIX_B_4_2_2
301 setuid(struct setuid_args *uap)
303 struct proc *p = curproc;
308 if (p == NULL) /* API enforcement */
313 * See if we have "permission" by POSIX 1003.1 rules.
315 * Note that setuid(geteuid()) is a special case of
316 * "appropriate privileges" in appendix B.4.2.2. We need
317 * to use this clause to be compatable with traditional BSD
318 * semantics. Basically, it means that "setuid(xx)" sets all
319 * three id's (assuming you have privs).
321 * Notes on the logic. We do things in three steps.
322 * 1: We determine if the euid is going to change, and do EPERM
323 * right away. We unconditionally change the euid later if this
324 * test is satisfied, simplifying that part of the logic.
325 * 2: We determine if the real and/or saved uid's are going to
326 * change. Determined by compile options.
327 * 3: Change euid last. (after tests in #2 for "appropriate privs")
330 if (uid != cr->cr_ruid && /* allow setuid(getuid()) */
331 #ifdef _POSIX_SAVED_IDS
332 uid != crc->cr_svuid && /* allow setuid(saved gid) */
334 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
335 uid != cr->cr_uid && /* allow setuid(geteuid()) */
337 (error = suser_cred(cr, PRISON_ROOT)))
340 #ifdef _POSIX_SAVED_IDS
342 * Do we have "appropriate privileges" (are we root or uid == euid)
343 * If so, we are changing the real uid and/or saved uid.
346 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
349 suser_cred(cr, PRISON_ROOT) == 0) /* we are using privs */
353 * Set the real uid and transfer proc count to new user.
355 if (uid != cr->cr_ruid) {
362 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
363 * the security of seteuid() depends on it. B.4.2.2 says it
364 * is important that we should do this.
366 if (cr->cr_svuid != uid) {
367 cr = cratom(&p->p_ucred);
374 * In all permitted cases, we are changing the euid.
375 * Copy credentials so other references do not see our changes.
377 if (cr->cr_uid != uid) {
386 seteuid(struct seteuid_args *uap)
388 struct proc *p = curproc;
393 if (p == NULL) /* API enforcement */
398 if (euid != cr->cr_ruid && /* allow seteuid(getuid()) */
399 euid != cr->cr_svuid && /* allow seteuid(saved uid) */
400 (error = suser_cred(cr, PRISON_ROOT)))
403 * Everything's okay, do it. Copy credentials so other references do
404 * not see our changes.
406 if (cr->cr_uid != euid) {
415 setgid(struct setgid_args *uap)
417 struct proc *p = curproc;
422 if (p == NULL) /* API enforcement */
427 * See if we have "permission" by POSIX 1003.1 rules.
429 * Note that setgid(getegid()) is a special case of
430 * "appropriate privileges" in appendix B.4.2.2. We need
431 * to use this clause to be compatable with traditional BSD
432 * semantics. Basically, it means that "setgid(xx)" sets all
433 * three id's (assuming you have privs).
435 * For notes on the logic here, see setuid() above.
438 if (gid != cr->cr_rgid && /* allow setgid(getgid()) */
439 #ifdef _POSIX_SAVED_IDS
440 gid != cr->cr_svgid && /* allow setgid(saved gid) */
442 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
443 gid != cr->cr_groups[0] && /* allow setgid(getegid()) */
445 (error = suser_cred(cr, PRISON_ROOT)))
448 #ifdef _POSIX_SAVED_IDS
450 * Do we have "appropriate privileges" (are we root or gid == egid)
451 * If so, we are changing the real uid and saved gid.
454 #ifdef POSIX_APPENDIX_B_4_2_2 /* use the clause from B.4.2.2 */
455 gid == cr->cr_groups[0] ||
457 suser_cred(cr, PRISON_ROOT) == 0) /* we are using privs */
463 if (cr->cr_rgid != gid) {
464 cr = cratom(&p->p_ucred);
471 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
472 * the security of setegid() depends on it. B.4.2.2 says it
473 * is important that we should do this.
475 if (cr->cr_svgid != gid) {
476 cr = cratom(&p->p_ucred);
482 * In all cases permitted cases, we are changing the egid.
483 * Copy credentials so other references do not see our changes.
485 if (cr->cr_groups[0] != gid) {
486 cr = cratom(&p->p_ucred);
487 cr->cr_groups[0] = gid;
495 setegid(struct setegid_args *uap)
497 struct proc *p = curproc;
502 if (p == NULL) /* API enforcement */
507 if (egid != cr->cr_rgid && /* allow setegid(getgid()) */
508 egid != cr->cr_svgid && /* allow setegid(saved gid) */
509 (error = suser_cred(cr, PRISON_ROOT)))
511 if (cr->cr_groups[0] != egid) {
512 cr = cratom(&p->p_ucred);
513 cr->cr_groups[0] = egid;
521 setgroups(struct setgroups_args *uap)
523 struct proc *p = curproc;
528 if (p == NULL) /* API enforcement */
532 if ((error = suser_cred(cr, PRISON_ROOT)))
534 ngrp = uap->gidsetsize;
538 * XXX A little bit lazy here. We could test if anything has
539 * changed before cratom() and setting P_SUGID.
541 cr = cratom(&p->p_ucred);
544 * setgroups(0, NULL) is a legitimate way of clearing the
545 * groups vector on non-BSD systems (which generally do not
546 * have the egid in the groups[0]). We risk security holes
547 * when running non-BSD software if we do not do the same.
551 if ((error = copyin((caddr_t)uap->gidset,
552 (caddr_t)cr->cr_groups, ngrp * sizeof(gid_t))))
554 cr->cr_ngroups = ngrp;
562 setreuid(struct setreuid_args *uap)
564 struct proc *p = curproc;
569 if (p == NULL) /* API enforcement */
575 if (((ruid != (uid_t)-1 && ruid != cr->cr_ruid && ruid != cr->cr_svuid) ||
576 (euid != (uid_t)-1 && euid != cr->cr_uid &&
577 euid != cr->cr_ruid && euid != cr->cr_svuid)) &&
578 (error = suser_cred(cr, PRISON_ROOT)) != 0)
581 if (euid != (uid_t)-1 && cr->cr_uid != euid) {
585 if (ruid != (uid_t)-1 && cr->cr_ruid != ruid) {
589 if ((ruid != (uid_t)-1 || cr->cr_uid != cr->cr_ruid) &&
590 cr->cr_svuid != cr->cr_uid) {
591 cr = cratom(&p->p_ucred);
592 cr->cr_svuid = cr->cr_uid;
600 setregid(struct setregid_args *uap)
602 struct proc *p = curproc;
607 if (p == NULL) /* API enforcement */
613 if (((rgid != (gid_t)-1 && rgid != cr->cr_rgid && rgid != cr->cr_svgid) ||
614 (egid != (gid_t)-1 && egid != cr->cr_groups[0] &&
615 egid != cr->cr_rgid && egid != cr->cr_svgid)) &&
616 (error = suser_cred(cr, PRISON_ROOT)) != 0)
619 if (egid != (gid_t)-1 && cr->cr_groups[0] != egid) {
620 cr = cratom(&p->p_ucred);
621 cr->cr_groups[0] = egid;
624 if (rgid != (gid_t)-1 && cr->cr_rgid != rgid) {
625 cr = cratom(&p->p_ucred);
629 if ((rgid != (gid_t)-1 || cr->cr_groups[0] != cr->cr_rgid) &&
630 cr->cr_svgid != cr->cr_groups[0]) {
631 cr = cratom(&p->p_ucred);
632 cr->cr_svgid = cr->cr_groups[0];
639 * setresuid(ruid, euid, suid) is like setreuid except control over the
640 * saved uid is explicit.
645 setresuid(struct setresuid_args *uap)
647 struct proc *p = curproc;
649 uid_t ruid, euid, suid;
656 if (((ruid != (uid_t)-1 && ruid != cr->cr_ruid && ruid != cr->cr_svuid &&
657 ruid != cr->cr_uid) ||
658 (euid != (uid_t)-1 && euid != cr->cr_ruid && euid != cr->cr_svuid &&
659 euid != cr->cr_uid) ||
660 (suid != (uid_t)-1 && suid != cr->cr_ruid && suid != cr->cr_svuid &&
661 suid != cr->cr_uid)) &&
662 (error = suser_cred(cr, PRISON_ROOT)) != 0)
664 if (euid != (uid_t)-1 && cr->cr_uid != euid) {
668 if (ruid != (uid_t)-1 && cr->cr_ruid != ruid) {
672 if (suid != (uid_t)-1 && cr->cr_svuid != suid) {
673 cr = cratom(&p->p_ucred);
681 * setresgid(rgid, egid, sgid) is like setregid except control over the
682 * saved gid is explicit.
687 setresgid(struct setresgid_args *uap)
689 struct proc *p = curproc;
691 gid_t rgid, egid, sgid;
698 if (((rgid != (gid_t)-1 && rgid != cr->cr_rgid && rgid != cr->cr_svgid &&
699 rgid != cr->cr_groups[0]) ||
700 (egid != (gid_t)-1 && egid != cr->cr_rgid && egid != cr->cr_svgid &&
701 egid != cr->cr_groups[0]) ||
702 (sgid != (gid_t)-1 && sgid != cr->cr_rgid && sgid != cr->cr_svgid &&
703 sgid != cr->cr_groups[0])) &&
704 (error = suser_cred(cr, PRISON_ROOT)) != 0)
707 if (egid != (gid_t)-1 && cr->cr_groups[0] != egid) {
708 cr = cratom(&p->p_ucred);
709 cr->cr_groups[0] = egid;
712 if (rgid != (gid_t)-1 && cr->cr_rgid != rgid) {
713 cr = cratom(&p->p_ucred);
717 if (sgid != (gid_t)-1 && cr->cr_svgid != sgid) {
718 cr = cratom(&p->p_ucred);
727 getresuid(struct getresuid_args *uap)
729 struct proc *p = curproc;
730 struct ucred *cr = p->p_ucred;
731 int error1 = 0, error2 = 0, error3 = 0;
734 error1 = copyout((caddr_t)&cr->cr_ruid,
735 (caddr_t)uap->ruid, sizeof(cr->cr_ruid));
737 error2 = copyout((caddr_t)&cr->cr_uid,
738 (caddr_t)uap->euid, sizeof(cr->cr_uid));
740 error3 = copyout((caddr_t)&cr->cr_svuid,
741 (caddr_t)uap->suid, sizeof(cr->cr_svuid));
742 return error1 ? error1 : (error2 ? error2 : error3);
747 getresgid(struct getresgid_args *uap)
749 struct proc *p = curproc;
750 struct ucred *cr = p->p_ucred;
751 int error1 = 0, error2 = 0, error3 = 0;
754 error1 = copyout((caddr_t)&cr->cr_rgid,
755 (caddr_t)uap->rgid, sizeof(cr->cr_rgid));
757 error2 = copyout((caddr_t)&cr->cr_groups[0],
758 (caddr_t)uap->egid, sizeof(cr->cr_groups[0]));
760 error3 = copyout((caddr_t)&cr->cr_svgid,
761 (caddr_t)uap->sgid, sizeof(cr->cr_svgid));
762 return error1 ? error1 : (error2 ? error2 : error3);
768 issetugid(struct issetugid_args *uap)
770 struct proc *p = curproc;
772 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
773 * we use P_SUGID because we consider changing the owners as
774 * "tainting" as well.
775 * This is significant for procs that start as root and "become"
776 * a user without an exec - programs cannot know *everything*
777 * that libc *might* have put in their data segment.
779 uap->sysmsg_result = (p->p_flag & P_SUGID) ? 1 : 0;
784 * Check if gid is a member of the group set.
787 groupmember(gid_t gid, struct ucred *cred)
792 egp = &(cred->cr_groups[cred->cr_ngroups]);
793 for (gp = cred->cr_groups; gp < egp; gp++)
800 * Test whether the specified credentials imply "super-user"
801 * privilege; if so, and we have accounting info, set the flag
802 * indicating use of super-powers. A kernel thread without a process
803 * context is assumed to have super user capabilities. In situations
804 * where the caller always expect a cred to exist, the cred should be
805 * passed separately and suser_cred()should be used instead of suser().
807 * Returns 0 or error.
810 suser(struct thread *td)
812 struct proc *p = td->td_proc;
815 return suser_cred(p->p_ucred, 0);
822 suser_cred(struct ucred *cred, int flag)
824 KASSERT(cred != NULL, ("suser_cred: NULL cred!"));
826 if (cred->cr_uid != 0)
828 if (cred->cr_prison && !(flag & PRISON_ROOT))
830 /* NOTE: accounting for suser access (p_acflag/ASU) removed */
835 * Return zero if p1 can fondle p2, return errno (EPERM/ESRCH) otherwise.
838 p_trespass(struct ucred *cr1, struct ucred *cr2)
842 if (!PRISON_CHECK(cr1, cr2))
844 if (cr1->cr_ruid == cr2->cr_ruid)
846 if (cr1->cr_uid == cr2->cr_ruid)
848 if (cr1->cr_ruid == cr2->cr_uid)
850 if (cr1->cr_uid == cr2->cr_uid)
852 if (suser_cred(cr1, PRISON_ROOT) == 0)
858 * Allocate a zeroed cred structure.
865 MALLOC(cr, struct ucred *, sizeof(*cr), M_CRED, M_WAITOK);
866 bzero((caddr_t)cr, sizeof(*cr));
872 * Claim another reference to a ucred structure. Can be used with special
876 crhold(struct ucred *cr)
878 if (cr != NOCRED && cr != FSCRED)
884 * Free a cred structure.
885 * Throws away space when ref count gets to 0.
888 crfree(struct ucred *cr)
891 panic("Freeing already free credential! %p", cr);
893 if (--cr->cr_ref == 0) {
895 * Some callers of crget(), such as nfs_statfs(),
896 * allocate a temporary credential, but don't
897 * allocate a uidinfo structure.
899 if (cr->cr_uidinfo != NULL)
900 uifree(cr->cr_uidinfo);
901 if (cr->cr_ruidinfo != NULL)
902 uifree(cr->cr_ruidinfo);
905 * Destroy empty prisons
907 if (cr->cr_prison && !--cr->cr_prison->pr_ref) {
908 if (cr->cr_prison->pr_linux != NULL)
909 FREE(cr->cr_prison->pr_linux, M_PRISON);
910 FREE(cr->cr_prison, M_PRISON);
912 cr->cr_prison = NULL; /* safety */
914 FREE((caddr_t)cr, M_CRED);
919 * Atomize a cred structure so it can be modified without polluting
920 * other references to it.
923 cratom(struct ucred **pcr)
929 if (oldcr->cr_ref == 1)
933 if (newcr->cr_uidinfo)
934 uihold(newcr->cr_uidinfo);
935 if (newcr->cr_ruidinfo)
936 uihold(newcr->cr_ruidinfo);
937 if (newcr->cr_prison)
938 ++newcr->cr_prison->pr_ref;
945 #if 0 /* no longer used but keep around for a little while */
947 * Copy cred structure to a new one and free the old one.
950 crcopy(struct ucred *cr)
958 if (newcr->cr_uidinfo)
959 uihold(newcr->cr_uidinfo);
960 if (newcr->cr_ruidinfo)
961 uihold(newcr->cr_ruidinfo);
962 if (newcr->cr_prison)
963 ++newcr->cr_prison->pr_ref;
971 * Dup cred struct to a new held one.
981 if (newcr->cr_uidinfo)
982 uihold(newcr->cr_uidinfo);
983 if (newcr->cr_ruidinfo)
984 uihold(newcr->cr_ruidinfo);
985 if (newcr->cr_prison)
986 ++newcr->cr_prison->pr_ref;
992 * Fill in a struct xucred based on a struct ucred.
1000 bzero(xcr, sizeof(*xcr));
1001 xcr->cr_version = XUCRED_VERSION;
1002 xcr->cr_uid = cr->cr_uid;
1003 xcr->cr_ngroups = cr->cr_ngroups;
1004 bcopy(cr->cr_groups, xcr->cr_groups, sizeof(cr->cr_groups));
1008 * Get login name, if available.
1012 getlogin(struct getlogin_args *uap)
1014 struct proc *p = curproc;
1016 if (uap->namelen > MAXLOGNAME)
1017 uap->namelen = MAXLOGNAME;
1018 return (copyout((caddr_t) p->p_pgrp->pg_session->s_login,
1019 (caddr_t) uap->namebuf, uap->namelen));
1027 setlogin(struct setlogin_args *uap)
1029 struct proc *p = curproc;
1031 char logintmp[MAXLOGNAME];
1033 KKASSERT(p != NULL);
1034 if ((error = suser_cred(p->p_ucred, PRISON_ROOT)))
1036 error = copyinstr((caddr_t) uap->namebuf, (caddr_t) logintmp,
1037 sizeof(logintmp), (size_t *)0);
1038 if (error == ENAMETOOLONG)
1041 (void) memcpy(p->p_pgrp->pg_session->s_login, logintmp,
1049 struct proc *p = curproc;
1051 KKASSERT(p != NULL);
1052 p->p_flag |= P_SUGID;
1053 if (!(p->p_pfsflags & PF_ISUGID))
1058 * Helper function to change the effective uid of a process
1061 change_euid(uid_t euid)
1063 struct proc *p = curproc;
1065 struct uidinfo *uip;
1067 KKASSERT(p != NULL);
1069 cr = cratom(&p->p_ucred);
1070 uip = cr->cr_uidinfo;
1072 cr->cr_uidinfo = uifind(euid);
1077 * Helper function to change the real uid of a process
1079 * The per-uid process count for this process is transfered from
1080 * the old uid to the new uid.
1083 change_ruid(uid_t ruid)
1085 struct proc *p = curproc;
1087 struct uidinfo *uip;
1089 KKASSERT(p != NULL);
1091 cr = cratom(&p->p_ucred);
1092 (void)chgproccnt(cr->cr_ruidinfo, -1, 0);
1093 uip = cr->cr_ruidinfo;
1094 /* It is assumed that pcred is not shared between processes */
1096 cr->cr_ruidinfo = uifind(ruid);
1097 (void)chgproccnt(cr->cr_ruidinfo, 1, 0);