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.26 2006/06/05 07:26:10 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>
56 #include <sys/malloc.h>
57 #include <sys/pioctl.h>
58 #include <sys/resourcevar.h>
60 #include <sys/lockf.h>
61 #include <sys/spinlock.h>
63 #include <sys/thread2.h>
64 #include <sys/spinlock2.h>
66 static MALLOC_DEFINE(M_CRED, "cred", "credentials");
69 * NOT MP SAFE due to p_pptr access
73 sys_getpid(struct getpid_args *uap)
75 struct proc *p = curproc;
77 uap->sysmsg_fds[0] = p->p_pid;
78 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
79 uap->sysmsg_fds[1] = p->p_pptr->p_pid;
86 sys_getppid(struct getppid_args *uap)
88 struct proc *p = curproc;
90 uap->sysmsg_result = p->p_pptr->p_pid;
95 * Get process group ID; note that POSIX getpgrp takes no parameter
100 sys_getpgrp(struct getpgrp_args *uap)
102 struct proc *p = curproc;
104 uap->sysmsg_result = p->p_pgrp->pg_id;
109 * Get an arbitary pid's process group id
112 sys_getpgid(struct getpgid_args *uap)
114 struct proc *p = curproc;
121 if ((pt = pfind(uap->pid)) == 0)
124 uap->sysmsg_result = pt->p_pgrp->pg_id;
129 * Get an arbitary pid's session id.
132 sys_getsid(struct getsid_args *uap)
134 struct proc *p = curproc;
141 if ((pt = pfind(uap->pid)) == 0)
144 uap->sysmsg_result = pt->p_session->s_sid;
154 sys_getuid(struct getuid_args *uap)
156 struct proc *p = curproc;
158 uap->sysmsg_fds[0] = p->p_ucred->cr_ruid;
159 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
160 uap->sysmsg_fds[1] = p->p_ucred->cr_uid;
166 * geteuid() - MP SAFE
170 sys_geteuid(struct geteuid_args *uap)
172 struct proc *p = curproc;
174 uap->sysmsg_result = p->p_ucred->cr_uid;
183 sys_getgid(struct getgid_args *uap)
185 struct proc *p = curproc;
187 uap->sysmsg_fds[0] = p->p_ucred->cr_rgid;
188 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
189 uap->sysmsg_fds[1] = p->p_ucred->cr_groups[0];
195 * Get effective group ID. The "egid" is groups[0], and could be obtained
196 * via getgroups. This syscall exists because it is somewhat painful to do
197 * correctly in a library function.
203 sys_getegid(struct getegid_args *uap)
205 struct proc *p = curproc;
207 uap->sysmsg_result = p->p_ucred->cr_groups[0];
212 sys_getgroups(struct getgroups_args *uap)
214 struct proc *p = curproc;
219 if (p == NULL) /* API enforcement */
223 if ((ngrp = uap->gidsetsize) == 0) {
224 uap->sysmsg_result = cr->cr_ngroups;
227 if (ngrp < cr->cr_ngroups)
229 ngrp = cr->cr_ngroups;
230 if ((error = copyout((caddr_t)cr->cr_groups,
231 (caddr_t)uap->gidset, ngrp * sizeof(gid_t))))
233 uap->sysmsg_result = ngrp;
239 sys_setsid(struct setsid_args *uap)
241 struct proc *p = curproc;
243 if (p->p_pgid == p->p_pid || pgfind(p->p_pid)) {
246 (void)enterpgrp(p, p->p_pid, 1);
247 uap->sysmsg_result = p->p_pid;
253 * set process group (setpgid/old setpgrp)
255 * caller does setpgid(targpid, targpgid)
257 * pid must be caller or child of caller (ESRCH)
259 * pid must be in same session (EPERM)
260 * pid can't have done an exec (EACCES)
262 * there must exist some pid in same session having pgid (EPERM)
263 * pid must not be session leader (EPERM)
267 sys_setpgid(struct setpgid_args *uap)
269 struct proc *curp = curproc;
270 struct proc *targp; /* target process */
271 struct pgrp *pgrp; /* target pgrp */
275 if (uap->pid != 0 && uap->pid != curp->p_pid) {
276 if ((targp = pfind(uap->pid)) == 0 || !inferior(targp))
278 if (targp->p_pgrp == NULL || targp->p_session != curp->p_session)
280 if (targp->p_flag & P_EXEC)
284 if (SESS_LEADER(targp))
287 uap->pgid = targp->p_pid;
288 else if (uap->pgid != targp->p_pid)
289 if ((pgrp = pgfind(uap->pgid)) == 0 ||
290 pgrp->pg_session != curp->p_session)
292 return (enterpgrp(targp, uap->pgid, 0));
296 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
297 * compatible. It says that setting the uid/gid to euid/egid is a special
298 * case of "appropriate privilege". Once the rules are expanded out, this
299 * basically means that setuid(nnn) sets all three id's, in all permitted
300 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
301 * does not set the saved id - this is dangerous for traditional BSD
302 * programs. For this reason, we *really* do not want to set
303 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
305 #define POSIX_APPENDIX_B_4_2_2
309 sys_setuid(struct setuid_args *uap)
311 struct proc *p = curproc;
316 if (p == NULL) /* API enforcement */
321 * See if we have "permission" by POSIX 1003.1 rules.
323 * Note that setuid(geteuid()) is a special case of
324 * "appropriate privileges" in appendix B.4.2.2. We need
325 * to use this clause to be compatible with traditional BSD
326 * semantics. Basically, it means that "setuid(xx)" sets all
327 * three id's (assuming you have privs).
329 * Notes on the logic. We do things in three steps.
330 * 1: We determine if the euid is going to change, and do EPERM
331 * right away. We unconditionally change the euid later if this
332 * test is satisfied, simplifying that part of the logic.
333 * 2: We determine if the real and/or saved uid's are going to
334 * change. Determined by compile options.
335 * 3: Change euid last. (after tests in #2 for "appropriate privs")
338 if (uid != cr->cr_ruid && /* allow setuid(getuid()) */
339 #ifdef _POSIX_SAVED_IDS
340 uid != crc->cr_svuid && /* allow setuid(saved gid) */
342 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
343 uid != cr->cr_uid && /* allow setuid(geteuid()) */
345 (error = suser_cred(cr, PRISON_ROOT)))
348 #ifdef _POSIX_SAVED_IDS
350 * Do we have "appropriate privileges" (are we root or uid == euid)
351 * If so, we are changing the real uid and/or saved uid.
354 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
357 suser_cred(cr, PRISON_ROOT) == 0) /* we are using privs */
361 * Set the real uid and transfer proc count to new user.
363 if (uid != cr->cr_ruid) {
370 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
371 * the security of seteuid() depends on it. B.4.2.2 says it
372 * is important that we should do this.
374 if (cr->cr_svuid != uid) {
375 cr = cratom(&p->p_ucred);
382 * In all permitted cases, we are changing the euid.
383 * Copy credentials so other references do not see our changes.
385 if (cr->cr_uid != uid) {
394 sys_seteuid(struct seteuid_args *uap)
396 struct proc *p = curproc;
401 if (p == NULL) /* API enforcement */
406 if (euid != cr->cr_ruid && /* allow seteuid(getuid()) */
407 euid != cr->cr_svuid && /* allow seteuid(saved uid) */
408 (error = suser_cred(cr, PRISON_ROOT)))
411 * Everything's okay, do it. Copy credentials so other references do
412 * not see our changes.
414 if (cr->cr_uid != euid) {
423 sys_setgid(struct setgid_args *uap)
425 struct proc *p = curproc;
430 if (p == NULL) /* API enforcement */
435 * See if we have "permission" by POSIX 1003.1 rules.
437 * Note that setgid(getegid()) is a special case of
438 * "appropriate privileges" in appendix B.4.2.2. We need
439 * to use this clause to be compatible with traditional BSD
440 * semantics. Basically, it means that "setgid(xx)" sets all
441 * three id's (assuming you have privs).
443 * For notes on the logic here, see setuid() above.
446 if (gid != cr->cr_rgid && /* allow setgid(getgid()) */
447 #ifdef _POSIX_SAVED_IDS
448 gid != cr->cr_svgid && /* allow setgid(saved gid) */
450 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
451 gid != cr->cr_groups[0] && /* allow setgid(getegid()) */
453 (error = suser_cred(cr, PRISON_ROOT)))
456 #ifdef _POSIX_SAVED_IDS
458 * Do we have "appropriate privileges" (are we root or gid == egid)
459 * If so, we are changing the real uid and saved gid.
462 #ifdef POSIX_APPENDIX_B_4_2_2 /* use the clause from B.4.2.2 */
463 gid == cr->cr_groups[0] ||
465 suser_cred(cr, PRISON_ROOT) == 0) /* we are using privs */
471 if (cr->cr_rgid != gid) {
472 cr = cratom(&p->p_ucred);
479 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
480 * the security of setegid() depends on it. B.4.2.2 says it
481 * is important that we should do this.
483 if (cr->cr_svgid != gid) {
484 cr = cratom(&p->p_ucred);
490 * In all cases permitted cases, we are changing the egid.
491 * Copy credentials so other references do not see our changes.
493 if (cr->cr_groups[0] != gid) {
494 cr = cratom(&p->p_ucred);
495 cr->cr_groups[0] = gid;
503 sys_setegid(struct setegid_args *uap)
505 struct proc *p = curproc;
510 if (p == NULL) /* API enforcement */
515 if (egid != cr->cr_rgid && /* allow setegid(getgid()) */
516 egid != cr->cr_svgid && /* allow setegid(saved gid) */
517 (error = suser_cred(cr, PRISON_ROOT)))
519 if (cr->cr_groups[0] != egid) {
520 cr = cratom(&p->p_ucred);
521 cr->cr_groups[0] = egid;
529 sys_setgroups(struct setgroups_args *uap)
531 struct proc *p = curproc;
536 if (p == NULL) /* API enforcement */
540 if ((error = suser_cred(cr, PRISON_ROOT)))
542 ngrp = uap->gidsetsize;
546 * XXX A little bit lazy here. We could test if anything has
547 * changed before cratom() and setting P_SUGID.
549 cr = cratom(&p->p_ucred);
552 * setgroups(0, NULL) is a legitimate way of clearing the
553 * groups vector on non-BSD systems (which generally do not
554 * have the egid in the groups[0]). We risk security holes
555 * when running non-BSD software if we do not do the same.
559 if ((error = copyin((caddr_t)uap->gidset,
560 (caddr_t)cr->cr_groups, ngrp * sizeof(gid_t))))
562 cr->cr_ngroups = ngrp;
570 sys_setreuid(struct setreuid_args *uap)
572 struct proc *p = curproc;
577 if (p == NULL) /* API enforcement */
583 if (((ruid != (uid_t)-1 && ruid != cr->cr_ruid && ruid != cr->cr_svuid) ||
584 (euid != (uid_t)-1 && euid != cr->cr_uid &&
585 euid != cr->cr_ruid && euid != cr->cr_svuid)) &&
586 (error = suser_cred(cr, PRISON_ROOT)) != 0)
589 if (euid != (uid_t)-1 && cr->cr_uid != euid) {
593 if (ruid != (uid_t)-1 && cr->cr_ruid != ruid) {
597 if ((ruid != (uid_t)-1 || cr->cr_uid != cr->cr_ruid) &&
598 cr->cr_svuid != cr->cr_uid) {
599 cr = cratom(&p->p_ucred);
600 cr->cr_svuid = cr->cr_uid;
608 sys_setregid(struct setregid_args *uap)
610 struct proc *p = curproc;
615 if (p == NULL) /* API enforcement */
621 if (((rgid != (gid_t)-1 && rgid != cr->cr_rgid && rgid != cr->cr_svgid) ||
622 (egid != (gid_t)-1 && egid != cr->cr_groups[0] &&
623 egid != cr->cr_rgid && egid != cr->cr_svgid)) &&
624 (error = suser_cred(cr, PRISON_ROOT)) != 0)
627 if (egid != (gid_t)-1 && cr->cr_groups[0] != egid) {
628 cr = cratom(&p->p_ucred);
629 cr->cr_groups[0] = egid;
632 if (rgid != (gid_t)-1 && cr->cr_rgid != rgid) {
633 cr = cratom(&p->p_ucred);
637 if ((rgid != (gid_t)-1 || cr->cr_groups[0] != cr->cr_rgid) &&
638 cr->cr_svgid != cr->cr_groups[0]) {
639 cr = cratom(&p->p_ucred);
640 cr->cr_svgid = cr->cr_groups[0];
647 * setresuid(ruid, euid, suid) is like setreuid except control over the
648 * saved uid is explicit.
653 sys_setresuid(struct setresuid_args *uap)
655 struct proc *p = curproc;
657 uid_t ruid, euid, suid;
664 if (((ruid != (uid_t)-1 && ruid != cr->cr_ruid && ruid != cr->cr_svuid &&
665 ruid != cr->cr_uid) ||
666 (euid != (uid_t)-1 && euid != cr->cr_ruid && euid != cr->cr_svuid &&
667 euid != cr->cr_uid) ||
668 (suid != (uid_t)-1 && suid != cr->cr_ruid && suid != cr->cr_svuid &&
669 suid != cr->cr_uid)) &&
670 (error = suser_cred(cr, PRISON_ROOT)) != 0)
672 if (euid != (uid_t)-1 && cr->cr_uid != euid) {
676 if (ruid != (uid_t)-1 && cr->cr_ruid != ruid) {
680 if (suid != (uid_t)-1 && cr->cr_svuid != suid) {
681 cr = cratom(&p->p_ucred);
689 * setresgid(rgid, egid, sgid) is like setregid except control over the
690 * saved gid is explicit.
695 sys_setresgid(struct setresgid_args *uap)
697 struct proc *p = curproc;
699 gid_t rgid, egid, sgid;
706 if (((rgid != (gid_t)-1 && rgid != cr->cr_rgid && rgid != cr->cr_svgid &&
707 rgid != cr->cr_groups[0]) ||
708 (egid != (gid_t)-1 && egid != cr->cr_rgid && egid != cr->cr_svgid &&
709 egid != cr->cr_groups[0]) ||
710 (sgid != (gid_t)-1 && sgid != cr->cr_rgid && sgid != cr->cr_svgid &&
711 sgid != cr->cr_groups[0])) &&
712 (error = suser_cred(cr, PRISON_ROOT)) != 0)
715 if (egid != (gid_t)-1 && cr->cr_groups[0] != egid) {
716 cr = cratom(&p->p_ucred);
717 cr->cr_groups[0] = egid;
720 if (rgid != (gid_t)-1 && cr->cr_rgid != rgid) {
721 cr = cratom(&p->p_ucred);
725 if (sgid != (gid_t)-1 && cr->cr_svgid != sgid) {
726 cr = cratom(&p->p_ucred);
735 sys_getresuid(struct getresuid_args *uap)
737 struct proc *p = curproc;
738 struct ucred *cr = p->p_ucred;
739 int error1 = 0, error2 = 0, error3 = 0;
742 error1 = copyout((caddr_t)&cr->cr_ruid,
743 (caddr_t)uap->ruid, sizeof(cr->cr_ruid));
745 error2 = copyout((caddr_t)&cr->cr_uid,
746 (caddr_t)uap->euid, sizeof(cr->cr_uid));
748 error3 = copyout((caddr_t)&cr->cr_svuid,
749 (caddr_t)uap->suid, sizeof(cr->cr_svuid));
750 return error1 ? error1 : (error2 ? error2 : error3);
755 sys_getresgid(struct getresgid_args *uap)
757 struct proc *p = curproc;
758 struct ucred *cr = p->p_ucred;
759 int error1 = 0, error2 = 0, error3 = 0;
762 error1 = copyout((caddr_t)&cr->cr_rgid,
763 (caddr_t)uap->rgid, sizeof(cr->cr_rgid));
765 error2 = copyout((caddr_t)&cr->cr_groups[0],
766 (caddr_t)uap->egid, sizeof(cr->cr_groups[0]));
768 error3 = copyout((caddr_t)&cr->cr_svgid,
769 (caddr_t)uap->sgid, sizeof(cr->cr_svgid));
770 return error1 ? error1 : (error2 ? error2 : error3);
776 sys_issetugid(struct issetugid_args *uap)
778 struct proc *p = curproc;
780 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
781 * we use P_SUGID because we consider changing the owners as
782 * "tainting" as well.
783 * This is significant for procs that start as root and "become"
784 * a user without an exec - programs cannot know *everything*
785 * that libc *might* have put in their data segment.
787 uap->sysmsg_result = (p->p_flag & P_SUGID) ? 1 : 0;
792 * Check if gid is a member of the group set.
795 groupmember(gid_t gid, struct ucred *cred)
800 egp = &(cred->cr_groups[cred->cr_ngroups]);
801 for (gp = cred->cr_groups; gp < egp; gp++) {
809 * Test whether the specified credentials imply "super-user"
810 * privilege; if so, and we have accounting info, set the flag
811 * indicating use of super-powers. A kernel thread without a process
812 * context is assumed to have super user capabilities. In situations
813 * where the caller always expect a cred to exist, the cred should be
814 * passed separately and suser_cred()should be used instead of suser().
816 * Returns 0 or error.
819 suser(struct thread *td)
821 struct proc *p = td->td_proc;
824 return suser_cred(p->p_ucred, 0);
831 * A non-null credential is expected unless NULL_CRED_OKAY is set.
834 suser_cred(struct ucred *cred, int flag)
836 KASSERT(cred != NULL || flag & NULL_CRED_OKAY,
837 ("suser_cred: NULL cred!"));
840 if (flag & NULL_CRED_OKAY)
845 if (cred->cr_uid != 0)
847 if (cred->cr_prison && !(flag & PRISON_ROOT))
849 /* NOTE: accounting for suser access (p_acflag/ASU) removed */
854 * Return zero if p1 can fondle p2, return errno (EPERM/ESRCH) otherwise.
857 p_trespass(struct ucred *cr1, struct ucred *cr2)
861 if (!PRISON_CHECK(cr1, cr2))
863 if (cr1->cr_ruid == cr2->cr_ruid)
865 if (cr1->cr_uid == cr2->cr_ruid)
867 if (cr1->cr_ruid == cr2->cr_uid)
869 if (cr1->cr_uid == cr2->cr_uid)
871 if (suser_cred(cr1, PRISON_ROOT) == 0)
880 _crinit(struct ucred *cr)
882 bzero(cr, sizeof(*cr));
884 spin_init(&cr->cr_spin);
891 crinit(struct ucred *cr)
897 * Allocate a zeroed cred structure.
906 MALLOC(cr, struct ucred *, sizeof(*cr), M_CRED, M_WAITOK);
912 * Claim another reference to a ucred structure. Can be used with special
915 * It must be possible to call this routine with spinlocks held, meaning
916 * that this routine itself cannot obtain a spinlock.
921 crhold(struct ucred *cr)
923 if (cr != NOCRED && cr != FSCRED)
924 atomic_add_int(&cr->cr_ref, 1);
929 * Drop a reference from the cred structure, free it if the reference count
932 * NOTE: because we used atomic_add_int() above, without a spinlock, we
933 * must also use atomic_subtract_int() below. A spinlock is required
934 * in crfree() to handle multiple callers racing the refcount to 0.
936 * MPALMOSTSAFE - acquires mplock on 1->0 transition of ref count
939 crfree(struct ucred *cr)
942 panic("Freeing already free credential! %p", cr);
943 spin_lock_wr(&cr->cr_spin);
944 atomic_subtract_int(&cr->cr_ref, 1);
945 if (cr->cr_ref == 0) {
946 spin_unlock_wr(&cr->cr_spin);
948 * Some callers of crget(), such as nfs_statfs(),
949 * allocate a temporary credential, but don't
950 * allocate a uidinfo structure.
953 if (cr->cr_uidinfo != NULL) {
954 uidrop(cr->cr_uidinfo);
955 cr->cr_uidinfo = NULL;
957 if (cr->cr_ruidinfo != NULL) {
958 uidrop(cr->cr_ruidinfo);
959 cr->cr_ruidinfo = NULL;
963 * Destroy empty prisons
966 prison_free(cr->cr_prison);
967 cr->cr_prison = NULL; /* safety */
969 FREE((caddr_t)cr, M_CRED);
972 spin_unlock_wr(&cr->cr_spin);
977 * Atomize a cred structure so it can be modified without polluting
978 * other references to it.
981 cratom(struct ucred **pcr)
987 if (oldcr->cr_ref == 1)
991 if (newcr->cr_uidinfo)
992 uihold(newcr->cr_uidinfo);
993 if (newcr->cr_ruidinfo)
994 uihold(newcr->cr_ruidinfo);
996 prison_hold(newcr->cr_prison);
1003 #if 0 /* no longer used but keep around for a little while */
1005 * Copy cred structure to a new one and free the old one.
1008 crcopy(struct ucred *cr)
1010 struct ucred *newcr;
1012 if (cr->cr_ref == 1)
1016 if (newcr->cr_uidinfo)
1017 uihold(newcr->cr_uidinfo);
1018 if (newcr->cr_ruidinfo)
1019 uihold(newcr->cr_ruidinfo);
1021 prison_hold(newcr->cr_prison);
1029 * Dup cred struct to a new held one.
1032 crdup(struct ucred *cr)
1034 struct ucred *newcr;
1038 if (newcr->cr_uidinfo)
1039 uihold(newcr->cr_uidinfo);
1040 if (newcr->cr_ruidinfo)
1041 uihold(newcr->cr_ruidinfo);
1043 prison_hold(newcr->cr_prison);
1049 * Fill in a struct xucred based on a struct ucred.
1052 cru2x(struct ucred *cr, struct xucred *xcr)
1055 bzero(xcr, sizeof(*xcr));
1056 xcr->cr_version = XUCRED_VERSION;
1057 xcr->cr_uid = cr->cr_uid;
1058 xcr->cr_ngroups = cr->cr_ngroups;
1059 bcopy(cr->cr_groups, xcr->cr_groups, sizeof(cr->cr_groups));
1063 * Get login name, if available.
1067 sys_getlogin(struct getlogin_args *uap)
1069 struct proc *p = curproc;
1071 if (uap->namelen > MAXLOGNAME)
1072 uap->namelen = MAXLOGNAME;
1073 return (copyout((caddr_t) p->p_pgrp->pg_session->s_login,
1074 (caddr_t) uap->namebuf, uap->namelen));
1082 sys_setlogin(struct setlogin_args *uap)
1084 struct proc *p = curproc;
1086 char logintmp[MAXLOGNAME];
1088 KKASSERT(p != NULL);
1089 if ((error = suser_cred(p->p_ucred, PRISON_ROOT)))
1091 error = copyinstr((caddr_t) uap->namebuf, (caddr_t) logintmp,
1092 sizeof(logintmp), (size_t *)0);
1093 if (error == ENAMETOOLONG)
1096 (void) memcpy(p->p_pgrp->pg_session->s_login, logintmp,
1104 struct proc *p = curproc;
1106 KKASSERT(p != NULL);
1107 p->p_flag |= P_SUGID;
1108 if (!(p->p_pfsflags & PF_ISUGID))
1113 * Helper function to change the effective uid of a process
1116 change_euid(uid_t euid)
1118 struct proc *p = curproc;
1121 KKASSERT(p != NULL);
1122 lf_count_adjust(p, 0);
1123 cr = cratom(&p->p_ucred);
1125 uireplace(&cr->cr_uidinfo, uifind(euid));
1126 lf_count_adjust(p, 1);
1130 * Helper function to change the real uid of a process
1132 * The per-uid process count for this process is transfered from
1133 * the old uid to the new uid.
1136 change_ruid(uid_t ruid)
1138 struct proc *p = curproc;
1141 KKASSERT(p != NULL);
1143 cr = cratom(&p->p_ucred);
1144 (void)chgproccnt(cr->cr_ruidinfo, -1, 0);
1145 /* It is assumed that pcred is not shared between processes */
1147 uireplace(&cr->cr_ruidinfo, uifind(ruid));
1148 (void)chgproccnt(cr->cr_ruidinfo, 1, 0);