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 proc *p = curproc;
194 uap->sysmsg_fds[0] = p->p_ucred->cr_ruid;
195 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
196 uap->sysmsg_fds[1] = p->p_ucred->cr_uid;
207 sys_geteuid(struct geteuid_args *uap)
209 struct proc *p = curproc;
211 uap->sysmsg_result = p->p_ucred->cr_uid;
221 sys_getgid(struct getgid_args *uap)
223 struct proc *p = curproc;
225 uap->sysmsg_fds[0] = p->p_ucred->cr_rgid;
226 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
227 uap->sysmsg_fds[1] = p->p_ucred->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 proc *p = curproc;
244 uap->sysmsg_result = p->p_ucred->cr_groups[0];
252 sys_getgroups(struct getgroups_args *uap)
254 struct proc *p = curproc;
260 if ((ngrp = uap->gidsetsize) == 0) {
261 uap->sysmsg_result = cr->cr_ngroups;
264 if (ngrp < cr->cr_ngroups)
266 ngrp = cr->cr_ngroups;
267 error = copyout((caddr_t)cr->cr_groups,
268 (caddr_t)uap->gidset, ngrp * sizeof(gid_t));
270 uap->sysmsg_result = ngrp;
278 sys_setsid(struct setsid_args *uap)
280 struct proc *p = curproc;
284 if (p->p_pgid == p->p_pid || pgfind(p->p_pid)) {
287 enterpgrp(p, p->p_pid, 1);
288 uap->sysmsg_result = p->p_pid;
296 * set process group (setpgid/old setpgrp)
298 * caller does setpgid(targpid, targpgid)
300 * pid must be caller or child of caller (ESRCH)
302 * pid must be in same session (EPERM)
303 * pid can't have done an exec (EACCES)
305 * there must exist some pid in same session having pgid (EPERM)
306 * pid must not be session leader (EPERM)
311 sys_setpgid(struct setpgid_args *uap)
313 struct proc *curp = curproc;
314 struct proc *targp; /* target process */
315 struct pgrp *pgrp; /* target pgrp */
322 if (uap->pid != 0 && uap->pid != curp->p_pid) {
323 if ((targp = pfind(uap->pid)) == 0 || !inferior(targp)) {
327 if (targp->p_pgrp == NULL ||
328 targp->p_session != curp->p_session) {
332 if (targp->p_flag & P_EXEC) {
339 if (SESS_LEADER(targp)) {
343 if (uap->pgid == 0) {
344 uap->pgid = targp->p_pid;
345 } else if (uap->pgid != targp->p_pid) {
346 if ((pgrp = pgfind(uap->pgid)) == 0 ||
347 pgrp->pg_session != curp->p_session) {
352 error = enterpgrp(targp, uap->pgid, 0);
359 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
360 * compatible. It says that setting the uid/gid to euid/egid is a special
361 * case of "appropriate privilege". Once the rules are expanded out, this
362 * basically means that setuid(nnn) sets all three id's, in all permitted
363 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
364 * does not set the saved id - this is dangerous for traditional BSD
365 * programs. For this reason, we *really* do not want to set
366 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
368 #define POSIX_APPENDIX_B_4_2_2
374 sys_setuid(struct setuid_args *uap)
376 struct proc *p = curproc;
385 * See if we have "permission" by POSIX 1003.1 rules.
387 * Note that setuid(geteuid()) is a special case of
388 * "appropriate privileges" in appendix B.4.2.2. We need
389 * to use this clause to be compatible with traditional BSD
390 * semantics. Basically, it means that "setuid(xx)" sets all
391 * three id's (assuming you have privs).
393 * Notes on the logic. We do things in three steps.
394 * 1: We determine if the euid is going to change, and do EPERM
395 * right away. We unconditionally change the euid later if this
396 * test is satisfied, simplifying that part of the logic.
397 * 2: We determine if the real and/or saved uid's are going to
398 * change. Determined by compile options.
399 * 3: Change euid last. (after tests in #2 for "appropriate privs")
402 if (uid != cr->cr_ruid && /* allow setuid(getuid()) */
403 #ifdef _POSIX_SAVED_IDS
404 uid != crc->cr_svuid && /* allow setuid(saved gid) */
406 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
407 uid != cr->cr_uid && /* allow setuid(geteuid()) */
409 (error = priv_check_cred(cr, PRIV_CRED_SETUID, 0)))
412 #ifdef _POSIX_SAVED_IDS
414 * Do we have "appropriate privileges" (are we root or uid == euid)
415 * If so, we are changing the real uid and/or saved uid.
418 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
421 priv_check_cred(cr, PRIV_CRED_SETUID, 0) == 0) /* we are using privs */
425 * Set the real uid and transfer proc count to new user.
427 if (uid != cr->cr_ruid) {
428 cr = change_ruid(uid);
434 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
435 * the security of seteuid() depends on it. B.4.2.2 says it
436 * is important that we should do this.
438 if (cr->cr_svuid != uid) {
439 cr = cratom(&p->p_ucred);
446 * In all permitted cases, we are changing the euid.
447 * Copy credentials so other references do not see our changes.
449 if (cr->cr_uid != uid) {
463 sys_seteuid(struct seteuid_args *uap)
465 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))) {
479 * Everything's okay, do it. Copy credentials so other references do
480 * not see our changes.
482 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)
871 struct proc *p = curproc;
873 int error1 = 0, error2 = 0, error3 = 0;
878 error1 = copyout((caddr_t)&cr->cr_rgid,
879 (caddr_t)uap->rgid, sizeof(cr->cr_rgid));
881 error2 = copyout((caddr_t)&cr->cr_groups[0],
882 (caddr_t)uap->egid, sizeof(cr->cr_groups[0]));
884 error3 = copyout((caddr_t)&cr->cr_svgid,
885 (caddr_t)uap->sgid, sizeof(cr->cr_svgid));
887 return error1 ? error1 : (error2 ? error2 : error3);
892 * NOTE: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
893 * we use P_SUGID because we consider changing the owners as
894 * "tainting" as well.
895 * This is significant for procs that start as root and "become"
896 * a user without an exec - programs cannot know *everything*
897 * that libc *might* have put in their data segment.
902 sys_issetugid(struct issetugid_args *uap)
904 uap->sysmsg_result = (curproc->p_flag & P_SUGID) ? 1 : 0;
909 * Check if gid is a member of the group set.
912 groupmember(gid_t gid, struct ucred *cred)
917 egp = &(cred->cr_groups[cred->cr_ngroups]);
918 for (gp = cred->cr_groups; gp < egp; gp++) {
926 * Test whether the specified credentials have the privilege
929 * A kernel thread without a process context is assumed to have
930 * the privilege in question. In situations where the caller always
931 * expect a cred to exist, the cred should be passed separately and
932 * priv_check_cred() should be used instead of priv_check().
934 * Returns 0 or error.
939 priv_check(struct thread *td, int priv)
941 struct proc *p = td->td_proc;
944 return priv_check_cred(p->p_ucred, priv, 0);
951 * Check a credential for privilege.
953 * A non-null credential is expected unless NULL_CRED_OKAY is set.
958 priv_check_cred(struct ucred *cred, int priv, int flags)
962 KASSERT(PRIV_VALID(priv), ("priv_check_cred: invalid privilege"));
964 KASSERT(cred != NULL || flags & NULL_CRED_OKAY,
965 ("priv_check_cred: NULL cred!"));
968 if (flags & NULL_CRED_OKAY)
973 if (cred->cr_uid != 0)
976 error = prison_priv_check(cred, priv);
980 /* NOTE: accounting for suser access (p_acflag/ASU) removed */
985 * Return zero if p1 can fondle p2, return errno (EPERM/ESRCH) otherwise.
988 p_trespass(struct ucred *cr1, struct ucred *cr2)
992 if (!PRISON_CHECK(cr1, cr2))
994 if (cr1->cr_ruid == cr2->cr_ruid)
996 if (cr1->cr_uid == cr2->cr_ruid)
998 if (cr1->cr_ruid == cr2->cr_uid)
1000 if (cr1->cr_uid == cr2->cr_uid)
1002 if (priv_check_cred(cr1, PRIV_PROC_TRESPASS, 0) == 0)
1010 static __inline void
1011 _crinit(struct ucred *cr)
1014 spin_init(&cr->cr_spin);
1021 crinit(struct ucred *cr)
1023 bzero(cr, sizeof(*cr));
1028 * Allocate a zeroed cred structure.
1037 cr = kmalloc(sizeof(*cr), M_CRED, M_WAITOK|M_ZERO);
1043 * Claim another reference to a ucred structure. Can be used with special
1046 * It must be possible to call this routine with spinlocks held, meaning
1047 * that this routine itself cannot obtain a spinlock.
1052 crhold(struct ucred *cr)
1054 if (cr != NOCRED && cr != FSCRED)
1055 atomic_add_int(&cr->cr_ref, 1);
1060 * Drop a reference from the cred structure, free it if the reference count
1063 * NOTE: because we used atomic_add_int() above, without a spinlock, we
1064 * must also use atomic_subtract_int() below. A spinlock is required
1065 * in crfree() to handle multiple callers racing the refcount to 0.
1067 * MPALMOSTSAFE - acquires mplock on 1->0 transition of ref count
1070 crfree(struct ucred *cr)
1072 if (cr->cr_ref <= 0)
1073 panic("Freeing already free credential! %p", cr);
1074 spin_lock_wr(&cr->cr_spin);
1075 atomic_subtract_int(&cr->cr_ref, 1);
1076 if (cr->cr_ref == 0) {
1077 spin_unlock_wr(&cr->cr_spin);
1079 * Some callers of crget(), such as nfs_statfs(),
1080 * allocate a temporary credential, but don't
1081 * allocate a uidinfo structure.
1084 if (cr->cr_uidinfo != NULL) {
1085 uidrop(cr->cr_uidinfo);
1086 cr->cr_uidinfo = NULL;
1088 if (cr->cr_ruidinfo != NULL) {
1089 uidrop(cr->cr_ruidinfo);
1090 cr->cr_ruidinfo = NULL;
1094 * Destroy empty prisons
1097 prison_free(cr->cr_prison);
1098 cr->cr_prison = NULL; /* safety */
1100 FREE((caddr_t)cr, M_CRED);
1103 spin_unlock_wr(&cr->cr_spin);
1108 * Atomize a cred structure so it can be modified without polluting
1109 * other references to it.
1112 cratom(struct ucred **pcr)
1114 struct ucred *oldcr;
1115 struct ucred *newcr;
1118 if (oldcr->cr_ref == 1)
1122 if (newcr->cr_uidinfo)
1123 uihold(newcr->cr_uidinfo);
1124 if (newcr->cr_ruidinfo)
1125 uihold(newcr->cr_ruidinfo);
1127 prison_hold(newcr->cr_prison);
1134 #if 0 /* no longer used but keep around for a little while */
1136 * Copy cred structure to a new one and free the old one.
1139 crcopy(struct ucred *cr)
1141 struct ucred *newcr;
1143 if (cr->cr_ref == 1)
1147 if (newcr->cr_uidinfo)
1148 uihold(newcr->cr_uidinfo);
1149 if (newcr->cr_ruidinfo)
1150 uihold(newcr->cr_ruidinfo);
1152 prison_hold(newcr->cr_prison);
1160 * Dup cred struct to a new held one.
1163 crdup(struct ucred *cr)
1165 struct ucred *newcr;
1169 if (newcr->cr_uidinfo)
1170 uihold(newcr->cr_uidinfo);
1171 if (newcr->cr_ruidinfo)
1172 uihold(newcr->cr_ruidinfo);
1174 prison_hold(newcr->cr_prison);
1180 * Fill in a struct xucred based on a struct ucred.
1183 cru2x(struct ucred *cr, struct xucred *xcr)
1186 bzero(xcr, sizeof(*xcr));
1187 xcr->cr_version = XUCRED_VERSION;
1188 xcr->cr_uid = cr->cr_uid;
1189 xcr->cr_ngroups = cr->cr_ngroups;
1190 bcopy(cr->cr_groups, xcr->cr_groups, sizeof(cr->cr_groups));
1194 * Get login name, if available.
1199 sys_getlogin(struct getlogin_args *uap)
1201 struct proc *p = curproc;
1202 char buf[MAXLOGNAME];
1205 if (uap->namelen > MAXLOGNAME) /* namelen is unsigned */
1206 uap->namelen = MAXLOGNAME;
1208 bzero(buf, sizeof(buf));
1209 bcopy(p->p_pgrp->pg_session->s_login, buf, uap->namelen);
1212 error = copyout(buf, uap->namebuf, uap->namelen);
1222 sys_setlogin(struct setlogin_args *uap)
1224 struct proc *p = curproc;
1225 char buf[MAXLOGNAME];
1228 KKASSERT(p != NULL);
1229 if ((error = priv_check_cred(p->p_ucred, PRIV_PROC_SETLOGIN, 0)))
1231 bzero(buf, sizeof(buf));
1232 error = copyinstr(uap->namebuf, buf, sizeof(buf), NULL);
1233 if (error == ENAMETOOLONG)
1237 memcpy(p->p_pgrp->pg_session->s_login, buf, sizeof(buf));
1246 struct proc *p = curproc;
1248 KKASSERT(p != NULL);
1249 p->p_flag |= P_SUGID;
1250 if (!(p->p_pfsflags & PF_ISUGID))
1255 * Helper function to change the effective uid of a process
1258 change_euid(uid_t euid)
1260 struct proc *p = curproc;
1263 KKASSERT(p != NULL);
1264 lf_count_adjust(p, 0);
1265 cr = cratom(&p->p_ucred);
1267 uireplace(&cr->cr_uidinfo, uifind(euid));
1268 lf_count_adjust(p, 1);
1273 * Helper function to change the real uid of a process
1275 * The per-uid process count for this process is transfered from
1276 * the old uid to the new uid.
1279 change_ruid(uid_t ruid)
1281 struct proc *p = curproc;
1284 KKASSERT(p != NULL);
1286 cr = cratom(&p->p_ucred);
1287 chgproccnt(cr->cr_ruidinfo, -1, 0);
1289 uireplace(&cr->cr_ruidinfo, uifind(ruid));
1290 chgproccnt(cr->cr_ruidinfo, 1, 0);