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.29 2008/02/16 15:53:39 matthias 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>
57 #include <sys/malloc.h>
58 #include <sys/pioctl.h>
59 #include <sys/resourcevar.h>
61 #include <sys/lockf.h>
62 #include <sys/spinlock.h>
64 #include <sys/thread2.h>
65 #include <sys/spinlock2.h>
67 static MALLOC_DEFINE(M_CRED, "cred", "credentials");
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)
80 uap->sysmsg_fds[1] = p->p_pptr->p_pid;
90 sys_getppid(struct getppid_args *uap)
92 struct proc *p = curproc;
95 uap->sysmsg_result = p->p_pptr->p_pid;
105 sys_lwp_gettid(struct lwp_gettid_args *uap)
107 struct lwp *lp = curthread->td_lwp;
109 uap->sysmsg_result = lp->lwp_tid;
114 * Get process group ID; note that POSIX getpgrp takes no parameter
119 sys_getpgrp(struct getpgrp_args *uap)
121 struct proc *p = curproc;
123 uap->sysmsg_result = p->p_pgrp->pg_id;
128 * Get an arbitrary pid's process group id
133 sys_getpgid(struct getpgid_args *uap)
135 struct proc *p = curproc;
145 pt = pfind(uap->pid);
150 uap->sysmsg_result = pt->p_pgrp->pg_id;
156 * Get an arbitrary pid's session id.
161 sys_getsid(struct getsid_args *uap)
163 struct proc *p = curproc;
173 pt = pfind(uap->pid);
178 uap->sysmsg_result = pt->p_session->s_sid;
190 sys_getuid(struct getuid_args *uap)
192 struct ucred *cred = curthread->td_ucred;
194 uap->sysmsg_fds[0] = cred->cr_ruid;
195 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
196 uap->sysmsg_fds[1] = cred->cr_uid;
207 sys_geteuid(struct geteuid_args *uap)
209 struct ucred *cred = curthread->td_ucred;
211 uap->sysmsg_result = cred->cr_uid;
221 sys_getgid(struct getgid_args *uap)
223 struct ucred *cred = curthread->td_ucred;
225 uap->sysmsg_fds[0] = cred->cr_rgid;
226 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
227 uap->sysmsg_fds[1] = cred->cr_groups[0];
233 * Get effective group ID. The "egid" is groups[0], and could be obtained
234 * via getgroups. This syscall exists because it is somewhat painful to do
235 * correctly in a library function.
240 sys_getegid(struct getegid_args *uap)
242 struct ucred *cred = curthread->td_ucred;
244 uap->sysmsg_result = cred->cr_groups[0];
252 sys_getgroups(struct getgroups_args *uap)
258 cr = curthread->td_ucred;
259 if ((ngrp = uap->gidsetsize) == 0) {
260 uap->sysmsg_result = cr->cr_ngroups;
263 if (ngrp < cr->cr_ngroups)
265 ngrp = cr->cr_ngroups;
266 error = copyout((caddr_t)cr->cr_groups,
267 (caddr_t)uap->gidset, ngrp * sizeof(gid_t));
269 uap->sysmsg_result = ngrp;
277 sys_setsid(struct setsid_args *uap)
279 struct proc *p = curproc;
283 if (p->p_pgid == p->p_pid || pgfind(p->p_pid)) {
286 enterpgrp(p, p->p_pid, 1);
287 uap->sysmsg_result = p->p_pid;
295 * set process group (setpgid/old setpgrp)
297 * caller does setpgid(targpid, targpgid)
299 * pid must be caller or child of caller (ESRCH)
301 * pid must be in same session (EPERM)
302 * pid can't have done an exec (EACCES)
304 * there must exist some pid in same session having pgid (EPERM)
305 * pid must not be session leader (EPERM)
310 sys_setpgid(struct setpgid_args *uap)
312 struct proc *curp = curproc;
313 struct proc *targp; /* target process */
314 struct pgrp *pgrp; /* target pgrp */
321 if (uap->pid != 0 && uap->pid != curp->p_pid) {
322 if ((targp = pfind(uap->pid)) == 0 || !inferior(targp)) {
326 if (targp->p_pgrp == NULL ||
327 targp->p_session != curp->p_session) {
331 if (targp->p_flag & P_EXEC) {
338 if (SESS_LEADER(targp)) {
342 if (uap->pgid == 0) {
343 uap->pgid = targp->p_pid;
344 } else if (uap->pgid != targp->p_pid) {
345 if ((pgrp = pgfind(uap->pgid)) == 0 ||
346 pgrp->pg_session != curp->p_session) {
351 error = enterpgrp(targp, uap->pgid, 0);
358 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
359 * compatible. It says that setting the uid/gid to euid/egid is a special
360 * case of "appropriate privilege". Once the rules are expanded out, this
361 * basically means that setuid(nnn) sets all three id's, in all permitted
362 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
363 * does not set the saved id - this is dangerous for traditional BSD
364 * programs. For this reason, we *really* do not want to set
365 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
367 #define POSIX_APPENDIX_B_4_2_2
373 sys_setuid(struct setuid_args *uap)
375 struct proc *p = curproc;
384 * See if we have "permission" by POSIX 1003.1 rules.
386 * Note that setuid(geteuid()) is a special case of
387 * "appropriate privileges" in appendix B.4.2.2. We need
388 * to use this clause to be compatible with traditional BSD
389 * semantics. Basically, it means that "setuid(xx)" sets all
390 * three id's (assuming you have privs).
392 * Notes on the logic. We do things in three steps.
393 * 1: We determine if the euid is going to change, and do EPERM
394 * right away. We unconditionally change the euid later if this
395 * test is satisfied, simplifying that part of the logic.
396 * 2: We determine if the real and/or saved uid's are going to
397 * change. Determined by compile options.
398 * 3: Change euid last. (after tests in #2 for "appropriate privs")
401 if (uid != cr->cr_ruid && /* allow setuid(getuid()) */
402 #ifdef _POSIX_SAVED_IDS
403 uid != crc->cr_svuid && /* allow setuid(saved gid) */
405 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
406 uid != cr->cr_uid && /* allow setuid(geteuid()) */
408 (error = priv_check_cred(cr, PRIV_CRED_SETUID, 0)))
411 #ifdef _POSIX_SAVED_IDS
413 * Do we have "appropriate privileges" (are we root or uid == euid)
414 * If so, we are changing the real uid and/or saved uid.
417 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
420 priv_check_cred(cr, PRIV_CRED_SETUID, 0) == 0) /* we are using privs */
424 * Set the real uid and transfer proc count to new user.
426 if (uid != cr->cr_ruid) {
427 cr = change_ruid(uid);
433 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
434 * the security of seteuid() depends on it. B.4.2.2 says it
435 * is important that we should do this.
437 if (cr->cr_svuid != uid) {
438 cr = cratom(&p->p_ucred);
445 * In all permitted cases, we are changing the euid.
446 * Copy credentials so other references do not see our changes.
448 if (cr->cr_uid != uid) {
462 sys_seteuid(struct seteuid_args *uap)
464 struct proc *p = curproc;
472 if (euid != cr->cr_ruid && /* allow seteuid(getuid()) */
473 euid != cr->cr_svuid && /* allow seteuid(saved uid) */
474 (error = priv_check_cred(cr, PRIV_CRED_SETEUID, 0))) {
480 * Everything's okay, do it. Copy credentials so other references do
481 * not see our changes.
483 if (cr->cr_uid != euid) {
495 sys_setgid(struct setgid_args *uap)
497 struct proc *p = curproc;
506 * See if we have "permission" by POSIX 1003.1 rules.
508 * Note that setgid(getegid()) is a special case of
509 * "appropriate privileges" in appendix B.4.2.2. We need
510 * to use this clause to be compatible with traditional BSD
511 * semantics. Basically, it means that "setgid(xx)" sets all
512 * three id's (assuming you have privs).
514 * For notes on the logic here, see setuid() above.
517 if (gid != cr->cr_rgid && /* allow setgid(getgid()) */
518 #ifdef _POSIX_SAVED_IDS
519 gid != cr->cr_svgid && /* allow setgid(saved gid) */
521 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
522 gid != cr->cr_groups[0] && /* allow setgid(getegid()) */
524 (error = priv_check_cred(cr, PRIV_CRED_SETGID, 0))) {
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 == cr->cr_groups[0] ||
537 priv_check_cred(cr, PRIV_CRED_SETGID, 0) == 0) /* we are using privs */
543 if (cr->cr_rgid != gid) {
544 cr = cratom(&p->p_ucred);
551 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
552 * the security of setegid() depends on it. B.4.2.2 says it
553 * is important that we should do this.
555 if (cr->cr_svgid != gid) {
556 cr = cratom(&p->p_ucred);
562 * In all cases permitted cases, we are changing the egid.
563 * Copy credentials so other references do not see our changes.
565 if (cr->cr_groups[0] != gid) {
566 cr = cratom(&p->p_ucred);
567 cr->cr_groups[0] = gid;
580 sys_setegid(struct setegid_args *uap)
582 struct proc *p = curproc;
590 if (egid != cr->cr_rgid && /* allow setegid(getgid()) */
591 egid != cr->cr_svgid && /* allow setegid(saved gid) */
592 (error = priv_check_cred(cr, PRIV_CRED_SETEGID, 0))) {
595 if (cr->cr_groups[0] != egid) {
596 cr = cratom(&p->p_ucred);
597 cr->cr_groups[0] = egid;
610 sys_setgroups(struct setgroups_args *uap)
612 struct proc *p = curproc;
620 if ((error = priv_check_cred(cr, PRIV_CRED_SETGROUPS, 0)))
622 ngrp = uap->gidsetsize;
623 if (ngrp > NGROUPS) {
628 * XXX A little bit lazy here. We could test if anything has
629 * changed before cratom() and setting P_SUGID.
631 cr = cratom(&p->p_ucred);
634 * setgroups(0, NULL) is a legitimate way of clearing the
635 * groups vector on non-BSD systems (which generally do not
636 * have the egid in the groups[0]). We risk security holes
637 * when running non-BSD software if we do not do the same.
641 error = copyin(uap->gidset, cr->cr_groups,
642 ngrp * sizeof(gid_t));
645 cr->cr_ngroups = ngrp;
658 sys_setreuid(struct setreuid_args *uap)
660 struct proc *p = curproc;
670 if (((ruid != (uid_t)-1 && ruid != cr->cr_ruid && ruid != cr->cr_svuid) ||
671 (euid != (uid_t)-1 && euid != cr->cr_uid &&
672 euid != cr->cr_ruid && euid != cr->cr_svuid)) &&
673 (error = priv_check_cred(cr, PRIV_CRED_SETREUID, 0)) != 0) {
677 if (euid != (uid_t)-1 && cr->cr_uid != euid) {
678 cr = change_euid(euid);
681 if (ruid != (uid_t)-1 && cr->cr_ruid != ruid) {
682 cr = change_ruid(ruid);
685 if ((ruid != (uid_t)-1 || cr->cr_uid != cr->cr_ruid) &&
686 cr->cr_svuid != cr->cr_uid) {
687 cr = cratom(&p->p_ucred);
688 cr->cr_svuid = cr->cr_uid;
701 sys_setregid(struct setregid_args *uap)
703 struct proc *p = curproc;
713 if (((rgid != (gid_t)-1 && rgid != cr->cr_rgid && rgid != cr->cr_svgid) ||
714 (egid != (gid_t)-1 && egid != cr->cr_groups[0] &&
715 egid != cr->cr_rgid && egid != cr->cr_svgid)) &&
716 (error = priv_check_cred(cr, PRIV_CRED_SETREGID, 0)) != 0) {
720 if (egid != (gid_t)-1 && cr->cr_groups[0] != egid) {
721 cr = cratom(&p->p_ucred);
722 cr->cr_groups[0] = egid;
725 if (rgid != (gid_t)-1 && cr->cr_rgid != rgid) {
726 cr = cratom(&p->p_ucred);
730 if ((rgid != (gid_t)-1 || cr->cr_groups[0] != cr->cr_rgid) &&
731 cr->cr_svgid != cr->cr_groups[0]) {
732 cr = cratom(&p->p_ucred);
733 cr->cr_svgid = cr->cr_groups[0];
743 * setresuid(ruid, euid, suid) is like setreuid except control over the
744 * saved uid is explicit.
749 sys_setresuid(struct setresuid_args *uap)
751 struct proc *p = curproc;
753 uid_t ruid, euid, suid;
762 if (((ruid != (uid_t)-1 && ruid != cr->cr_ruid && ruid != cr->cr_svuid &&
763 ruid != cr->cr_uid) ||
764 (euid != (uid_t)-1 && euid != cr->cr_ruid && euid != cr->cr_svuid &&
765 euid != cr->cr_uid) ||
766 (suid != (uid_t)-1 && suid != cr->cr_ruid && suid != cr->cr_svuid &&
767 suid != cr->cr_uid)) &&
768 (error = priv_check_cred(cr, PRIV_CRED_SETRESUID, 0)) != 0) {
771 if (euid != (uid_t)-1 && cr->cr_uid != euid) {
772 cr = change_euid(euid);
775 if (ruid != (uid_t)-1 && cr->cr_ruid != ruid) {
776 cr = change_ruid(ruid);
779 if (suid != (uid_t)-1 && cr->cr_svuid != suid) {
780 cr = cratom(&p->p_ucred);
791 * setresgid(rgid, egid, sgid) is like setregid except control over the
792 * saved gid is explicit.
797 sys_setresgid(struct setresgid_args *uap)
799 struct proc *p = curproc;
801 gid_t rgid, egid, sgid;
809 if (((rgid != (gid_t)-1 && rgid != cr->cr_rgid && rgid != cr->cr_svgid &&
810 rgid != cr->cr_groups[0]) ||
811 (egid != (gid_t)-1 && egid != cr->cr_rgid && egid != cr->cr_svgid &&
812 egid != cr->cr_groups[0]) ||
813 (sgid != (gid_t)-1 && sgid != cr->cr_rgid && sgid != cr->cr_svgid &&
814 sgid != cr->cr_groups[0])) &&
815 (error = priv_check_cred(cr, PRIV_CRED_SETRESGID, 0)) != 0) {
819 if (egid != (gid_t)-1 && cr->cr_groups[0] != egid) {
820 cr = cratom(&p->p_ucred);
821 cr->cr_groups[0] = egid;
824 if (rgid != (gid_t)-1 && cr->cr_rgid != rgid) {
825 cr = cratom(&p->p_ucred);
829 if (sgid != (gid_t)-1 && cr->cr_svgid != sgid) {
830 cr = cratom(&p->p_ucred);
844 sys_getresuid(struct getresuid_args *uap)
846 struct proc *p = curproc;
848 int error1 = 0, error2 = 0, error3 = 0;
853 error1 = copyout((caddr_t)&cr->cr_ruid,
854 (caddr_t)uap->ruid, sizeof(cr->cr_ruid));
856 error2 = copyout((caddr_t)&cr->cr_uid,
857 (caddr_t)uap->euid, sizeof(cr->cr_uid));
859 error3 = copyout((caddr_t)&cr->cr_svuid,
860 (caddr_t)uap->suid, sizeof(cr->cr_svuid));
862 return error1 ? error1 : (error2 ? error2 : error3);
869 sys_getresgid(struct getresgid_args *uap)
872 int error1 = 0, error2 = 0, error3 = 0;
874 cr = curthread->td_ucred;
876 error1 = copyout(&cr->cr_rgid, uap->rgid,
877 sizeof(cr->cr_rgid));
879 error2 = copyout(&cr->cr_groups[0], uap->egid,
880 sizeof(cr->cr_groups[0]));
882 error3 = copyout(&cr->cr_svgid, uap->sgid,
883 sizeof(cr->cr_svgid));
884 return error1 ? error1 : (error2 ? error2 : error3);
889 * NOTE: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
890 * we use P_SUGID because we consider changing the owners as
891 * "tainting" as well.
892 * This is significant for procs that start as root and "become"
893 * a user without an exec - programs cannot know *everything*
894 * that libc *might* have put in their data segment.
899 sys_issetugid(struct issetugid_args *uap)
901 uap->sysmsg_result = (curproc->p_flag & P_SUGID) ? 1 : 0;
906 * Check if gid is a member of the group set.
909 groupmember(gid_t gid, struct ucred *cred)
914 egp = &(cred->cr_groups[cred->cr_ngroups]);
915 for (gp = cred->cr_groups; gp < egp; gp++) {
923 * Test whether the specified credentials have the privilege
926 * A kernel thread without a process context is assumed to have
927 * the privilege in question. In situations where the caller always
928 * expect a cred to exist, the cred should be passed separately and
929 * priv_check_cred() should be used instead of priv_check().
931 * Returns 0 or error.
936 priv_check(struct thread *td, int priv)
938 if (td->td_lwp != NULL)
939 return priv_check_cred(td->td_ucred, priv, 0);
944 * Check a credential for privilege.
946 * A non-null credential is expected unless NULL_CRED_OKAY is set.
951 priv_check_cred(struct ucred *cred, int priv, int flags)
955 KASSERT(PRIV_VALID(priv), ("priv_check_cred: invalid privilege"));
957 KASSERT(cred != NULL || flags & NULL_CRED_OKAY,
958 ("priv_check_cred: NULL cred!"));
961 if (flags & NULL_CRED_OKAY)
966 if (cred->cr_uid != 0)
969 error = prison_priv_check(cred, priv);
973 /* NOTE: accounting for suser access (p_acflag/ASU) removed */
978 * Return zero if p1 can fondle p2, return errno (EPERM/ESRCH) otherwise.
981 p_trespass(struct ucred *cr1, struct ucred *cr2)
985 if (!PRISON_CHECK(cr1, cr2))
987 if (cr1->cr_ruid == cr2->cr_ruid)
989 if (cr1->cr_uid == cr2->cr_ruid)
991 if (cr1->cr_ruid == cr2->cr_uid)
993 if (cr1->cr_uid == cr2->cr_uid)
995 if (priv_check_cred(cr1, PRIV_PROC_TRESPASS, 0) == 0)
1003 static __inline void
1004 _crinit(struct ucred *cr)
1007 spin_init(&cr->cr_spin);
1014 crinit(struct ucred *cr)
1016 bzero(cr, sizeof(*cr));
1021 * Allocate a zeroed cred structure.
1030 cr = kmalloc(sizeof(*cr), M_CRED, M_WAITOK|M_ZERO);
1036 * Claim another reference to a ucred structure. Can be used with special
1039 * It must be possible to call this routine with spinlocks held, meaning
1040 * that this routine itself cannot obtain a spinlock.
1045 crhold(struct ucred *cr)
1047 if (cr != NOCRED && cr != FSCRED)
1048 atomic_add_int(&cr->cr_ref, 1);
1053 * Drop a reference from the cred structure, free it if the reference count
1056 * NOTE: because we used atomic_add_int() above, without a spinlock, we
1057 * must also use atomic_subtract_int() below. A spinlock is required
1058 * in crfree() to handle multiple callers racing the refcount to 0.
1060 * MPALMOSTSAFE - acquires mplock on 1->0 transition of ref count
1063 crfree(struct ucred *cr)
1065 if (cr->cr_ref <= 0)
1066 panic("Freeing already free credential! %p", cr);
1067 spin_lock_wr(&cr->cr_spin);
1068 atomic_subtract_int(&cr->cr_ref, 1);
1069 if (cr->cr_ref == 0) {
1070 spin_unlock_wr(&cr->cr_spin);
1072 * Some callers of crget(), such as nfs_statfs(),
1073 * allocate a temporary credential, but don't
1074 * allocate a uidinfo structure.
1077 if (cr->cr_uidinfo != NULL) {
1078 uidrop(cr->cr_uidinfo);
1079 cr->cr_uidinfo = NULL;
1081 if (cr->cr_ruidinfo != NULL) {
1082 uidrop(cr->cr_ruidinfo);
1083 cr->cr_ruidinfo = NULL;
1087 * Destroy empty prisons
1090 prison_free(cr->cr_prison);
1091 cr->cr_prison = NULL; /* safety */
1093 FREE((caddr_t)cr, M_CRED);
1096 spin_unlock_wr(&cr->cr_spin);
1101 * Atomize a cred structure so it can be modified without polluting
1102 * other references to it.
1105 cratom(struct ucred **pcr)
1107 struct ucred *oldcr;
1108 struct ucred *newcr;
1111 if (oldcr->cr_ref == 1)
1115 if (newcr->cr_uidinfo)
1116 uihold(newcr->cr_uidinfo);
1117 if (newcr->cr_ruidinfo)
1118 uihold(newcr->cr_ruidinfo);
1120 prison_hold(newcr->cr_prison);
1127 #if 0 /* no longer used but keep around for a little while */
1129 * Copy cred structure to a new one and free the old one.
1132 crcopy(struct ucred *cr)
1134 struct ucred *newcr;
1136 if (cr->cr_ref == 1)
1140 if (newcr->cr_uidinfo)
1141 uihold(newcr->cr_uidinfo);
1142 if (newcr->cr_ruidinfo)
1143 uihold(newcr->cr_ruidinfo);
1145 prison_hold(newcr->cr_prison);
1153 * Dup cred struct to a new held one.
1156 crdup(struct ucred *cr)
1158 struct ucred *newcr;
1162 if (newcr->cr_uidinfo)
1163 uihold(newcr->cr_uidinfo);
1164 if (newcr->cr_ruidinfo)
1165 uihold(newcr->cr_ruidinfo);
1167 prison_hold(newcr->cr_prison);
1173 * Fill in a struct xucred based on a struct ucred.
1176 cru2x(struct ucred *cr, struct xucred *xcr)
1179 bzero(xcr, sizeof(*xcr));
1180 xcr->cr_version = XUCRED_VERSION;
1181 xcr->cr_uid = cr->cr_uid;
1182 xcr->cr_ngroups = cr->cr_ngroups;
1183 bcopy(cr->cr_groups, xcr->cr_groups, sizeof(cr->cr_groups));
1187 * Get login name, if available.
1192 sys_getlogin(struct getlogin_args *uap)
1194 struct proc *p = curproc;
1195 char buf[MAXLOGNAME];
1198 if (uap->namelen > MAXLOGNAME) /* namelen is unsigned */
1199 uap->namelen = MAXLOGNAME;
1201 bzero(buf, sizeof(buf));
1202 bcopy(p->p_pgrp->pg_session->s_login, buf, uap->namelen);
1205 error = copyout(buf, uap->namebuf, uap->namelen);
1215 sys_setlogin(struct setlogin_args *uap)
1217 struct thread *td = curthread;
1220 char buf[MAXLOGNAME];
1223 cred = td->td_ucred;
1226 if ((error = priv_check_cred(cred, PRIV_PROC_SETLOGIN, 0)))
1228 bzero(buf, sizeof(buf));
1229 error = copyinstr(uap->namebuf, buf, sizeof(buf), NULL);
1230 if (error == ENAMETOOLONG)
1234 memcpy(p->p_pgrp->pg_session->s_login, buf, sizeof(buf));
1243 struct proc *p = curproc;
1245 KKASSERT(p != NULL);
1246 p->p_flag |= P_SUGID;
1247 if (!(p->p_pfsflags & PF_ISUGID))
1252 * Helper function to change the effective uid of a process
1255 change_euid(uid_t euid)
1257 struct proc *p = curproc;
1260 KKASSERT(p != NULL);
1261 lf_count_adjust(p, 0);
1262 cr = cratom(&p->p_ucred);
1264 uireplace(&cr->cr_uidinfo, uifind(euid));
1265 lf_count_adjust(p, 1);
1270 * Helper function to change the real uid of a process
1272 * The per-uid process count for this process is transfered from
1273 * the old uid to the new uid.
1276 change_ruid(uid_t ruid)
1278 struct proc *p = curproc;
1281 KKASSERT(p != NULL);
1283 cr = cratom(&p->p_ucred);
1284 chgproccnt(cr->cr_ruidinfo, -1, 0);
1286 uireplace(&cr->cr_ruidinfo, uifind(ruid));
1287 chgproccnt(cr->cr_ruidinfo, 1, 0);