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.24 2006/05/26 00:33:09 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>
59 #include <sys/lockf.h>
60 #include <sys/spinlock.h>
62 #include <sys/thread2.h>
63 #include <sys/spinlock2.h>
65 static MALLOC_DEFINE(M_CRED, "cred", "credentials");
68 * NOT MP SAFE due to p_pptr access
72 getpid(struct getpid_args *uap)
74 struct proc *p = curproc;
76 uap->sysmsg_fds[0] = p->p_pid;
77 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
78 uap->sysmsg_fds[1] = p->p_pptr->p_pid;
85 getppid(struct getppid_args *uap)
87 struct proc *p = curproc;
89 uap->sysmsg_result = p->p_pptr->p_pid;
94 * Get process group ID; note that POSIX getpgrp takes no parameter
99 getpgrp(struct getpgrp_args *uap)
101 struct proc *p = curproc;
103 uap->sysmsg_result = p->p_pgrp->pg_id;
108 * Get an arbitary pid's process group id
111 getpgid(struct getpgid_args *uap)
113 struct proc *p = curproc;
120 if ((pt = pfind(uap->pid)) == 0)
123 uap->sysmsg_result = pt->p_pgrp->pg_id;
128 * Get an arbitary pid's session id.
131 getsid(struct getsid_args *uap)
133 struct proc *p = curproc;
140 if ((pt = pfind(uap->pid)) == 0)
143 uap->sysmsg_result = pt->p_session->s_sid;
153 getuid(struct getuid_args *uap)
155 struct proc *p = curproc;
157 uap->sysmsg_fds[0] = p->p_ucred->cr_ruid;
158 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
159 uap->sysmsg_fds[1] = p->p_ucred->cr_uid;
165 * geteuid() - MP SAFE
169 geteuid(struct geteuid_args *uap)
171 struct proc *p = curproc;
173 uap->sysmsg_result = p->p_ucred->cr_uid;
182 getgid(struct getgid_args *uap)
184 struct proc *p = curproc;
186 uap->sysmsg_fds[0] = p->p_ucred->cr_rgid;
187 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
188 uap->sysmsg_fds[1] = p->p_ucred->cr_groups[0];
194 * Get effective group ID. The "egid" is groups[0], and could be obtained
195 * via getgroups. This syscall exists because it is somewhat painful to do
196 * correctly in a library function.
202 getegid(struct getegid_args *uap)
204 struct proc *p = curproc;
206 uap->sysmsg_result = p->p_ucred->cr_groups[0];
211 getgroups(struct getgroups_args *uap)
213 struct proc *p = curproc;
218 if (p == NULL) /* API enforcement */
222 if ((ngrp = uap->gidsetsize) == 0) {
223 uap->sysmsg_result = cr->cr_ngroups;
226 if (ngrp < cr->cr_ngroups)
228 ngrp = cr->cr_ngroups;
229 if ((error = copyout((caddr_t)cr->cr_groups,
230 (caddr_t)uap->gidset, ngrp * sizeof(gid_t))))
232 uap->sysmsg_result = ngrp;
238 setsid(struct setsid_args *uap)
240 struct proc *p = curproc;
242 if (p->p_pgid == p->p_pid || pgfind(p->p_pid)) {
245 (void)enterpgrp(p, p->p_pid, 1);
246 uap->sysmsg_result = p->p_pid;
252 * set process group (setpgid/old setpgrp)
254 * caller does setpgid(targpid, targpgid)
256 * pid must be caller or child of caller (ESRCH)
258 * pid must be in same session (EPERM)
259 * pid can't have done an exec (EACCES)
261 * there must exist some pid in same session having pgid (EPERM)
262 * pid must not be session leader (EPERM)
266 setpgid(struct setpgid_args *uap)
268 struct proc *curp = curproc;
269 struct proc *targp; /* target process */
270 struct pgrp *pgrp; /* target pgrp */
274 if (uap->pid != 0 && uap->pid != curp->p_pid) {
275 if ((targp = pfind(uap->pid)) == 0 || !inferior(targp))
277 if (targp->p_pgrp == NULL || targp->p_session != curp->p_session)
279 if (targp->p_flag & P_EXEC)
283 if (SESS_LEADER(targp))
286 uap->pgid = targp->p_pid;
287 else if (uap->pgid != targp->p_pid)
288 if ((pgrp = pgfind(uap->pgid)) == 0 ||
289 pgrp->pg_session != curp->p_session)
291 return (enterpgrp(targp, uap->pgid, 0));
295 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
296 * compatible. It says that setting the uid/gid to euid/egid is a special
297 * case of "appropriate privilege". Once the rules are expanded out, this
298 * basically means that setuid(nnn) sets all three id's, in all permitted
299 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
300 * does not set the saved id - this is dangerous for traditional BSD
301 * programs. For this reason, we *really* do not want to set
302 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
304 #define POSIX_APPENDIX_B_4_2_2
308 setuid(struct setuid_args *uap)
310 struct proc *p = curproc;
315 if (p == NULL) /* API enforcement */
320 * See if we have "permission" by POSIX 1003.1 rules.
322 * Note that setuid(geteuid()) is a special case of
323 * "appropriate privileges" in appendix B.4.2.2. We need
324 * to use this clause to be compatible with traditional BSD
325 * semantics. Basically, it means that "setuid(xx)" sets all
326 * three id's (assuming you have privs).
328 * Notes on the logic. We do things in three steps.
329 * 1: We determine if the euid is going to change, and do EPERM
330 * right away. We unconditionally change the euid later if this
331 * test is satisfied, simplifying that part of the logic.
332 * 2: We determine if the real and/or saved uid's are going to
333 * change. Determined by compile options.
334 * 3: Change euid last. (after tests in #2 for "appropriate privs")
337 if (uid != cr->cr_ruid && /* allow setuid(getuid()) */
338 #ifdef _POSIX_SAVED_IDS
339 uid != crc->cr_svuid && /* allow setuid(saved gid) */
341 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
342 uid != cr->cr_uid && /* allow setuid(geteuid()) */
344 (error = suser_cred(cr, PRISON_ROOT)))
347 #ifdef _POSIX_SAVED_IDS
349 * Do we have "appropriate privileges" (are we root or uid == euid)
350 * If so, we are changing the real uid and/or saved uid.
353 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
356 suser_cred(cr, PRISON_ROOT) == 0) /* we are using privs */
360 * Set the real uid and transfer proc count to new user.
362 if (uid != cr->cr_ruid) {
369 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
370 * the security of seteuid() depends on it. B.4.2.2 says it
371 * is important that we should do this.
373 if (cr->cr_svuid != uid) {
374 cr = cratom(&p->p_ucred);
381 * In all permitted cases, we are changing the euid.
382 * Copy credentials so other references do not see our changes.
384 if (cr->cr_uid != uid) {
393 seteuid(struct seteuid_args *uap)
395 struct proc *p = curproc;
400 if (p == NULL) /* API enforcement */
405 if (euid != cr->cr_ruid && /* allow seteuid(getuid()) */
406 euid != cr->cr_svuid && /* allow seteuid(saved uid) */
407 (error = suser_cred(cr, PRISON_ROOT)))
410 * Everything's okay, do it. Copy credentials so other references do
411 * not see our changes.
413 if (cr->cr_uid != euid) {
422 setgid(struct setgid_args *uap)
424 struct proc *p = curproc;
429 if (p == NULL) /* API enforcement */
434 * See if we have "permission" by POSIX 1003.1 rules.
436 * Note that setgid(getegid()) is a special case of
437 * "appropriate privileges" in appendix B.4.2.2. We need
438 * to use this clause to be compatible with traditional BSD
439 * semantics. Basically, it means that "setgid(xx)" sets all
440 * three id's (assuming you have privs).
442 * For notes on the logic here, see setuid() above.
445 if (gid != cr->cr_rgid && /* allow setgid(getgid()) */
446 #ifdef _POSIX_SAVED_IDS
447 gid != cr->cr_svgid && /* allow setgid(saved gid) */
449 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
450 gid != cr->cr_groups[0] && /* allow setgid(getegid()) */
452 (error = suser_cred(cr, PRISON_ROOT)))
455 #ifdef _POSIX_SAVED_IDS
457 * Do we have "appropriate privileges" (are we root or gid == egid)
458 * If so, we are changing the real uid and saved gid.
461 #ifdef POSIX_APPENDIX_B_4_2_2 /* use the clause from B.4.2.2 */
462 gid == cr->cr_groups[0] ||
464 suser_cred(cr, PRISON_ROOT) == 0) /* we are using privs */
470 if (cr->cr_rgid != gid) {
471 cr = cratom(&p->p_ucred);
478 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
479 * the security of setegid() depends on it. B.4.2.2 says it
480 * is important that we should do this.
482 if (cr->cr_svgid != gid) {
483 cr = cratom(&p->p_ucred);
489 * In all cases permitted cases, we are changing the egid.
490 * Copy credentials so other references do not see our changes.
492 if (cr->cr_groups[0] != gid) {
493 cr = cratom(&p->p_ucred);
494 cr->cr_groups[0] = gid;
502 setegid(struct setegid_args *uap)
504 struct proc *p = curproc;
509 if (p == NULL) /* API enforcement */
514 if (egid != cr->cr_rgid && /* allow setegid(getgid()) */
515 egid != cr->cr_svgid && /* allow setegid(saved gid) */
516 (error = suser_cred(cr, PRISON_ROOT)))
518 if (cr->cr_groups[0] != egid) {
519 cr = cratom(&p->p_ucred);
520 cr->cr_groups[0] = egid;
528 setgroups(struct setgroups_args *uap)
530 struct proc *p = curproc;
535 if (p == NULL) /* API enforcement */
539 if ((error = suser_cred(cr, PRISON_ROOT)))
541 ngrp = uap->gidsetsize;
545 * XXX A little bit lazy here. We could test if anything has
546 * changed before cratom() and setting P_SUGID.
548 cr = cratom(&p->p_ucred);
551 * setgroups(0, NULL) is a legitimate way of clearing the
552 * groups vector on non-BSD systems (which generally do not
553 * have the egid in the groups[0]). We risk security holes
554 * when running non-BSD software if we do not do the same.
558 if ((error = copyin((caddr_t)uap->gidset,
559 (caddr_t)cr->cr_groups, ngrp * sizeof(gid_t))))
561 cr->cr_ngroups = ngrp;
569 setreuid(struct setreuid_args *uap)
571 struct proc *p = curproc;
576 if (p == NULL) /* API enforcement */
582 if (((ruid != (uid_t)-1 && ruid != cr->cr_ruid && ruid != cr->cr_svuid) ||
583 (euid != (uid_t)-1 && euid != cr->cr_uid &&
584 euid != cr->cr_ruid && euid != cr->cr_svuid)) &&
585 (error = suser_cred(cr, PRISON_ROOT)) != 0)
588 if (euid != (uid_t)-1 && cr->cr_uid != euid) {
592 if (ruid != (uid_t)-1 && cr->cr_ruid != ruid) {
596 if ((ruid != (uid_t)-1 || cr->cr_uid != cr->cr_ruid) &&
597 cr->cr_svuid != cr->cr_uid) {
598 cr = cratom(&p->p_ucred);
599 cr->cr_svuid = cr->cr_uid;
607 setregid(struct setregid_args *uap)
609 struct proc *p = curproc;
614 if (p == NULL) /* API enforcement */
620 if (((rgid != (gid_t)-1 && rgid != cr->cr_rgid && rgid != cr->cr_svgid) ||
621 (egid != (gid_t)-1 && egid != cr->cr_groups[0] &&
622 egid != cr->cr_rgid && egid != cr->cr_svgid)) &&
623 (error = suser_cred(cr, PRISON_ROOT)) != 0)
626 if (egid != (gid_t)-1 && cr->cr_groups[0] != egid) {
627 cr = cratom(&p->p_ucred);
628 cr->cr_groups[0] = egid;
631 if (rgid != (gid_t)-1 && cr->cr_rgid != rgid) {
632 cr = cratom(&p->p_ucred);
636 if ((rgid != (gid_t)-1 || cr->cr_groups[0] != cr->cr_rgid) &&
637 cr->cr_svgid != cr->cr_groups[0]) {
638 cr = cratom(&p->p_ucred);
639 cr->cr_svgid = cr->cr_groups[0];
646 * setresuid(ruid, euid, suid) is like setreuid except control over the
647 * saved uid is explicit.
652 setresuid(struct setresuid_args *uap)
654 struct proc *p = curproc;
656 uid_t ruid, euid, suid;
663 if (((ruid != (uid_t)-1 && ruid != cr->cr_ruid && ruid != cr->cr_svuid &&
664 ruid != cr->cr_uid) ||
665 (euid != (uid_t)-1 && euid != cr->cr_ruid && euid != cr->cr_svuid &&
666 euid != cr->cr_uid) ||
667 (suid != (uid_t)-1 && suid != cr->cr_ruid && suid != cr->cr_svuid &&
668 suid != cr->cr_uid)) &&
669 (error = suser_cred(cr, PRISON_ROOT)) != 0)
671 if (euid != (uid_t)-1 && cr->cr_uid != euid) {
675 if (ruid != (uid_t)-1 && cr->cr_ruid != ruid) {
679 if (suid != (uid_t)-1 && cr->cr_svuid != suid) {
680 cr = cratom(&p->p_ucred);
688 * setresgid(rgid, egid, sgid) is like setregid except control over the
689 * saved gid is explicit.
694 setresgid(struct setresgid_args *uap)
696 struct proc *p = curproc;
698 gid_t rgid, egid, sgid;
705 if (((rgid != (gid_t)-1 && rgid != cr->cr_rgid && rgid != cr->cr_svgid &&
706 rgid != cr->cr_groups[0]) ||
707 (egid != (gid_t)-1 && egid != cr->cr_rgid && egid != cr->cr_svgid &&
708 egid != cr->cr_groups[0]) ||
709 (sgid != (gid_t)-1 && sgid != cr->cr_rgid && sgid != cr->cr_svgid &&
710 sgid != cr->cr_groups[0])) &&
711 (error = suser_cred(cr, PRISON_ROOT)) != 0)
714 if (egid != (gid_t)-1 && cr->cr_groups[0] != egid) {
715 cr = cratom(&p->p_ucred);
716 cr->cr_groups[0] = egid;
719 if (rgid != (gid_t)-1 && cr->cr_rgid != rgid) {
720 cr = cratom(&p->p_ucred);
724 if (sgid != (gid_t)-1 && cr->cr_svgid != sgid) {
725 cr = cratom(&p->p_ucred);
734 getresuid(struct getresuid_args *uap)
736 struct proc *p = curproc;
737 struct ucred *cr = p->p_ucred;
738 int error1 = 0, error2 = 0, error3 = 0;
741 error1 = copyout((caddr_t)&cr->cr_ruid,
742 (caddr_t)uap->ruid, sizeof(cr->cr_ruid));
744 error2 = copyout((caddr_t)&cr->cr_uid,
745 (caddr_t)uap->euid, sizeof(cr->cr_uid));
747 error3 = copyout((caddr_t)&cr->cr_svuid,
748 (caddr_t)uap->suid, sizeof(cr->cr_svuid));
749 return error1 ? error1 : (error2 ? error2 : error3);
754 getresgid(struct getresgid_args *uap)
756 struct proc *p = curproc;
757 struct ucred *cr = p->p_ucred;
758 int error1 = 0, error2 = 0, error3 = 0;
761 error1 = copyout((caddr_t)&cr->cr_rgid,
762 (caddr_t)uap->rgid, sizeof(cr->cr_rgid));
764 error2 = copyout((caddr_t)&cr->cr_groups[0],
765 (caddr_t)uap->egid, sizeof(cr->cr_groups[0]));
767 error3 = copyout((caddr_t)&cr->cr_svgid,
768 (caddr_t)uap->sgid, sizeof(cr->cr_svgid));
769 return error1 ? error1 : (error2 ? error2 : error3);
775 issetugid(struct issetugid_args *uap)
777 struct proc *p = curproc;
779 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
780 * we use P_SUGID because we consider changing the owners as
781 * "tainting" as well.
782 * This is significant for procs that start as root and "become"
783 * a user without an exec - programs cannot know *everything*
784 * that libc *might* have put in their data segment.
786 uap->sysmsg_result = (p->p_flag & P_SUGID) ? 1 : 0;
791 * Check if gid is a member of the group set.
794 groupmember(gid_t gid, struct ucred *cred)
799 egp = &(cred->cr_groups[cred->cr_ngroups]);
800 for (gp = cred->cr_groups; gp < egp; gp++) {
808 * Test whether the specified credentials imply "super-user"
809 * privilege; if so, and we have accounting info, set the flag
810 * indicating use of super-powers. A kernel thread without a process
811 * context is assumed to have super user capabilities. In situations
812 * where the caller always expect a cred to exist, the cred should be
813 * passed separately and suser_cred()should be used instead of suser().
815 * Returns 0 or error.
818 suser(struct thread *td)
820 struct proc *p = td->td_proc;
823 return suser_cred(p->p_ucred, 0);
830 * A non-null credential is expected unless NULL_CRED_OKAY is set.
833 suser_cred(struct ucred *cred, int flag)
835 KASSERT(cred != NULL || flag & NULL_CRED_OKAY,
836 ("suser_cred: NULL cred!"));
839 if (flag & NULL_CRED_OKAY)
844 if (cred->cr_uid != 0)
846 if (cred->cr_prison && !(flag & PRISON_ROOT))
848 /* NOTE: accounting for suser access (p_acflag/ASU) removed */
853 * Return zero if p1 can fondle p2, return errno (EPERM/ESRCH) otherwise.
856 p_trespass(struct ucred *cr1, struct ucred *cr2)
860 if (!PRISON_CHECK(cr1, cr2))
862 if (cr1->cr_ruid == cr2->cr_ruid)
864 if (cr1->cr_uid == cr2->cr_ruid)
866 if (cr1->cr_ruid == cr2->cr_uid)
868 if (cr1->cr_uid == cr2->cr_uid)
870 if (suser_cred(cr1, PRISON_ROOT) == 0)
879 _crinit(struct ucred *cr)
881 bzero(cr, sizeof(*cr));
883 spin_init(&cr->cr_spin);
890 crinit(struct ucred *cr)
896 * Allocate a zeroed cred structure.
905 MALLOC(cr, struct ucred *, sizeof(*cr), M_CRED, M_WAITOK);
911 * Claim another reference to a ucred structure. Can be used with special
914 * It must be possible to call this routine with spinlocks held, meaning
915 * that this routine itself cannot obtain a spinlock.
920 crhold(struct ucred *cr)
922 if (cr != NOCRED && cr != FSCRED)
923 atomic_add_int(&cr->cr_ref, 1);
928 * Drop a reference from the cred structure, free it if the reference count
931 * NOTE: because we used atomic_add_int() above, without a spinlock, we
932 * must also use atomic_subtract_int() below. A spinlock is required
933 * in crfree() to handle multiple callers racing the refcount to 0.
935 * MPALMOSTSAFE - acquires mplock on 1->0 transition of ref count
938 crfree(struct ucred *cr)
941 panic("Freeing already free credential! %p", cr);
942 spin_lock_wr(&cr->cr_spin);
943 atomic_subtract_int(&cr->cr_ref, 1);
944 if (cr->cr_ref == 0) {
945 spin_unlock_wr(&cr->cr_spin);
947 * Some callers of crget(), such as nfs_statfs(),
948 * allocate a temporary credential, but don't
949 * allocate a uidinfo structure.
952 if (cr->cr_uidinfo != NULL) {
953 uidrop(cr->cr_uidinfo);
954 cr->cr_uidinfo = NULL;
956 if (cr->cr_ruidinfo != NULL) {
957 uidrop(cr->cr_ruidinfo);
958 cr->cr_ruidinfo = NULL;
962 * Destroy empty prisons
965 prison_free(cr->cr_prison);
966 cr->cr_prison = NULL; /* safety */
968 FREE((caddr_t)cr, M_CRED);
971 spin_unlock_wr(&cr->cr_spin);
976 * Atomize a cred structure so it can be modified without polluting
977 * other references to it.
980 cratom(struct ucred **pcr)
986 if (oldcr->cr_ref == 1)
990 if (newcr->cr_uidinfo)
991 uihold(newcr->cr_uidinfo);
992 if (newcr->cr_ruidinfo)
993 uihold(newcr->cr_ruidinfo);
995 prison_hold(newcr->cr_prison);
1002 #if 0 /* no longer used but keep around for a little while */
1004 * Copy cred structure to a new one and free the old one.
1007 crcopy(struct ucred *cr)
1009 struct ucred *newcr;
1011 if (cr->cr_ref == 1)
1015 if (newcr->cr_uidinfo)
1016 uihold(newcr->cr_uidinfo);
1017 if (newcr->cr_ruidinfo)
1018 uihold(newcr->cr_ruidinfo);
1020 prison_hold(newcr->cr_prison);
1028 * Dup cred struct to a new held one.
1031 crdup(struct ucred *cr)
1033 struct ucred *newcr;
1037 if (newcr->cr_uidinfo)
1038 uihold(newcr->cr_uidinfo);
1039 if (newcr->cr_ruidinfo)
1040 uihold(newcr->cr_ruidinfo);
1042 prison_hold(newcr->cr_prison);
1048 * Fill in a struct xucred based on a struct ucred.
1051 cru2x(struct ucred *cr, struct xucred *xcr)
1054 bzero(xcr, sizeof(*xcr));
1055 xcr->cr_version = XUCRED_VERSION;
1056 xcr->cr_uid = cr->cr_uid;
1057 xcr->cr_ngroups = cr->cr_ngroups;
1058 bcopy(cr->cr_groups, xcr->cr_groups, sizeof(cr->cr_groups));
1062 * Get login name, if available.
1066 getlogin(struct getlogin_args *uap)
1068 struct proc *p = curproc;
1070 if (uap->namelen > MAXLOGNAME)
1071 uap->namelen = MAXLOGNAME;
1072 return (copyout((caddr_t) p->p_pgrp->pg_session->s_login,
1073 (caddr_t) uap->namebuf, uap->namelen));
1081 setlogin(struct setlogin_args *uap)
1083 struct proc *p = curproc;
1085 char logintmp[MAXLOGNAME];
1087 KKASSERT(p != NULL);
1088 if ((error = suser_cred(p->p_ucred, PRISON_ROOT)))
1090 error = copyinstr((caddr_t) uap->namebuf, (caddr_t) logintmp,
1091 sizeof(logintmp), (size_t *)0);
1092 if (error == ENAMETOOLONG)
1095 (void) memcpy(p->p_pgrp->pg_session->s_login, logintmp,
1103 struct proc *p = curproc;
1105 KKASSERT(p != NULL);
1106 p->p_flag |= P_SUGID;
1107 if (!(p->p_pfsflags & PF_ISUGID))
1112 * Helper function to change the effective uid of a process
1115 change_euid(uid_t euid)
1117 struct proc *p = curproc;
1120 KKASSERT(p != NULL);
1121 lf_count_adjust(p, 0);
1122 cr = cratom(&p->p_ucred);
1124 uireplace(&cr->cr_uidinfo, uifind(euid));
1125 lf_count_adjust(p, 1);
1129 * Helper function to change the real uid of a process
1131 * The per-uid process count for this process is transfered from
1132 * the old uid to the new uid.
1135 change_ruid(uid_t ruid)
1137 struct proc *p = curproc;
1140 KKASSERT(p != NULL);
1142 cr = cratom(&p->p_ucred);
1143 (void)chgproccnt(cr->cr_ruidinfo, -1, 0);
1144 /* It is assumed that pcred is not shared between processes */
1146 uireplace(&cr->cr_ruidinfo, uifind(ruid));
1147 (void)chgproccnt(cr->cr_ruidinfo, 1, 0);