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");
70 sys_getpid(struct getpid_args *uap)
72 struct proc *p = curproc;
74 uap->sysmsg_fds[0] = p->p_pid;
75 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
76 lwkt_gettoken(&proc_token);
77 uap->sysmsg_fds[1] = p->p_pptr->p_pid;
78 lwkt_reltoken(&proc_token);
84 sys_getppid(struct getppid_args *uap)
86 struct proc *p = curproc;
88 lwkt_gettoken(&proc_token);
89 uap->sysmsg_result = p->p_pptr->p_pid;
90 lwkt_reltoken(&proc_token);
99 sys_lwp_gettid(struct lwp_gettid_args *uap)
101 struct lwp *lp = curthread->td_lwp;
103 uap->sysmsg_result = lp->lwp_tid;
108 * Get process group ID; note that POSIX getpgrp takes no parameter
113 sys_getpgrp(struct getpgrp_args *uap)
115 struct proc *p = curproc;
117 uap->sysmsg_result = p->p_pgrp->pg_id;
122 * Get an arbitrary pid's process group id
125 sys_getpgid(struct getpgid_args *uap)
127 struct proc *p = curproc;
131 lwkt_gettoken(&proc_token);
137 pt = pfind(uap->pid);
142 uap->sysmsg_result = pt->p_pgrp->pg_id;
143 lwkt_reltoken(&proc_token);
148 * Get an arbitrary pid's session id.
151 sys_getsid(struct getsid_args *uap)
153 struct proc *p = curproc;
157 lwkt_gettoken(&proc_token);
163 pt = pfind(uap->pid);
168 uap->sysmsg_result = pt->p_session->s_sid;
169 lwkt_reltoken(&proc_token);
180 sys_getuid(struct getuid_args *uap)
182 struct ucred *cred = curthread->td_ucred;
184 uap->sysmsg_fds[0] = cred->cr_ruid;
185 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
186 uap->sysmsg_fds[1] = cred->cr_uid;
197 sys_geteuid(struct geteuid_args *uap)
199 struct ucred *cred = curthread->td_ucred;
201 uap->sysmsg_result = cred->cr_uid;
211 sys_getgid(struct getgid_args *uap)
213 struct ucred *cred = curthread->td_ucred;
215 uap->sysmsg_fds[0] = cred->cr_rgid;
216 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
217 uap->sysmsg_fds[1] = cred->cr_groups[0];
223 * Get effective group ID. The "egid" is groups[0], and could be obtained
224 * via getgroups. This syscall exists because it is somewhat painful to do
225 * correctly in a library function.
230 sys_getegid(struct getegid_args *uap)
232 struct ucred *cred = curthread->td_ucred;
234 uap->sysmsg_result = cred->cr_groups[0];
242 sys_getgroups(struct getgroups_args *uap)
248 cr = curthread->td_ucred;
249 if ((ngrp = uap->gidsetsize) == 0) {
250 uap->sysmsg_result = cr->cr_ngroups;
253 if (ngrp < cr->cr_ngroups)
255 ngrp = cr->cr_ngroups;
256 error = copyout((caddr_t)cr->cr_groups,
257 (caddr_t)uap->gidset, ngrp * sizeof(gid_t));
259 uap->sysmsg_result = ngrp;
264 sys_setsid(struct setsid_args *uap)
266 struct proc *p = curproc;
269 lwkt_gettoken(&proc_token);
270 if (p->p_pgid == p->p_pid || pgfind(p->p_pid)) {
273 enterpgrp(p, p->p_pid, 1);
274 uap->sysmsg_result = p->p_pid;
277 lwkt_reltoken(&proc_token);
282 * set process group (setpgid/old setpgrp)
284 * caller does setpgid(targpid, targpgid)
286 * pid must be caller or child of caller (ESRCH)
288 * pid must be in same session (EPERM)
289 * pid can't have done an exec (EACCES)
291 * there must exist some pid in same session having pgid (EPERM)
292 * pid must not be session leader (EPERM)
295 sys_setpgid(struct setpgid_args *uap)
297 struct proc *curp = curproc;
298 struct proc *targp; /* target process */
299 struct pgrp *pgrp; /* target pgrp */
305 lwkt_gettoken(&proc_token);
306 if (uap->pid != 0 && uap->pid != curp->p_pid) {
307 if ((targp = pfind(uap->pid)) == 0 || !inferior(targp)) {
311 if (targp->p_pgrp == NULL ||
312 targp->p_session != curp->p_session) {
316 if (targp->p_flag & P_EXEC) {
323 if (SESS_LEADER(targp)) {
327 if (uap->pgid == 0) {
328 uap->pgid = targp->p_pid;
329 } else if (uap->pgid != targp->p_pid) {
330 if ((pgrp = pgfind(uap->pgid)) == 0 ||
331 pgrp->pg_session != curp->p_session) {
336 error = enterpgrp(targp, uap->pgid, 0);
338 lwkt_reltoken(&proc_token);
343 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
344 * compatible. It says that setting the uid/gid to euid/egid is a special
345 * case of "appropriate privilege". Once the rules are expanded out, this
346 * basically means that setuid(nnn) sets all three id's, in all permitted
347 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
348 * does not set the saved id - this is dangerous for traditional BSD
349 * programs. For this reason, we *really* do not want to set
350 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
352 #define POSIX_APPENDIX_B_4_2_2
355 sys_setuid(struct setuid_args *uap)
357 struct proc *p = curproc;
362 lwkt_gettoken(&proc_token);
366 * See if we have "permission" by POSIX 1003.1 rules.
368 * Note that setuid(geteuid()) is a special case of
369 * "appropriate privileges" in appendix B.4.2.2. We need
370 * to use this clause to be compatible with traditional BSD
371 * semantics. Basically, it means that "setuid(xx)" sets all
372 * three id's (assuming you have privs).
374 * Notes on the logic. We do things in three steps.
375 * 1: We determine if the euid is going to change, and do EPERM
376 * right away. We unconditionally change the euid later if this
377 * test is satisfied, simplifying that part of the logic.
378 * 2: We determine if the real and/or saved uid's are going to
379 * change. Determined by compile options.
380 * 3: Change euid last. (after tests in #2 for "appropriate privs")
383 if (uid != cr->cr_ruid && /* allow setuid(getuid()) */
384 #ifdef _POSIX_SAVED_IDS
385 uid != crc->cr_svuid && /* allow setuid(saved gid) */
387 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
388 uid != cr->cr_uid && /* allow setuid(geteuid()) */
390 (error = priv_check_cred(cr, PRIV_CRED_SETUID, 0)))
393 #ifdef _POSIX_SAVED_IDS
395 * Do we have "appropriate privileges" (are we root or uid == euid)
396 * If so, we are changing the real uid and/or saved uid.
399 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
402 priv_check_cred(cr, PRIV_CRED_SETUID, 0) == 0) /* we are using privs */
406 * Set the real uid and transfer proc count to new user.
408 if (uid != cr->cr_ruid) {
409 cr = change_ruid(uid);
415 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
416 * the security of seteuid() depends on it. B.4.2.2 says it
417 * is important that we should do this.
419 if (cr->cr_svuid != uid) {
420 cr = cratom(&p->p_ucred);
427 * In all permitted cases, we are changing the euid.
428 * Copy credentials so other references do not see our changes.
430 if (cr->cr_uid != uid) {
436 lwkt_reltoken(&proc_token);
441 sys_seteuid(struct seteuid_args *uap)
443 struct proc *p = curproc;
448 lwkt_gettoken(&proc_token);
451 if (euid != cr->cr_ruid && /* allow seteuid(getuid()) */
452 euid != cr->cr_svuid && /* allow seteuid(saved uid) */
453 (error = priv_check_cred(cr, PRIV_CRED_SETEUID, 0))) {
454 lwkt_reltoken(&proc_token);
459 * Everything's okay, do it. Copy credentials so other references do
460 * not see our changes.
462 if (cr->cr_uid != euid) {
466 lwkt_reltoken(&proc_token);
471 sys_setgid(struct setgid_args *uap)
473 struct proc *p = curproc;
478 lwkt_gettoken(&proc_token);
482 * See if we have "permission" by POSIX 1003.1 rules.
484 * Note that setgid(getegid()) is a special case of
485 * "appropriate privileges" in appendix B.4.2.2. We need
486 * to use this clause to be compatible with traditional BSD
487 * semantics. Basically, it means that "setgid(xx)" sets all
488 * three id's (assuming you have privs).
490 * For notes on the logic here, see setuid() above.
493 if (gid != cr->cr_rgid && /* allow setgid(getgid()) */
494 #ifdef _POSIX_SAVED_IDS
495 gid != cr->cr_svgid && /* allow setgid(saved gid) */
497 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
498 gid != cr->cr_groups[0] && /* allow setgid(getegid()) */
500 (error = priv_check_cred(cr, PRIV_CRED_SETGID, 0))) {
504 #ifdef _POSIX_SAVED_IDS
506 * Do we have "appropriate privileges" (are we root or gid == egid)
507 * If so, we are changing the real uid and saved gid.
510 #ifdef POSIX_APPENDIX_B_4_2_2 /* use the clause from B.4.2.2 */
511 gid == cr->cr_groups[0] ||
513 priv_check_cred(cr, PRIV_CRED_SETGID, 0) == 0) /* we are using privs */
519 if (cr->cr_rgid != gid) {
520 cr = cratom(&p->p_ucred);
527 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
528 * the security of setegid() depends on it. B.4.2.2 says it
529 * is important that we should do this.
531 if (cr->cr_svgid != gid) {
532 cr = cratom(&p->p_ucred);
538 * In all cases permitted cases, we are changing the egid.
539 * Copy credentials so other references do not see our changes.
541 if (cr->cr_groups[0] != gid) {
542 cr = cratom(&p->p_ucred);
543 cr->cr_groups[0] = gid;
548 lwkt_reltoken(&proc_token);
553 sys_setegid(struct setegid_args *uap)
555 struct proc *p = curproc;
560 lwkt_gettoken(&proc_token);
563 if (egid != cr->cr_rgid && /* allow setegid(getgid()) */
564 egid != cr->cr_svgid && /* allow setegid(saved gid) */
565 (error = priv_check_cred(cr, PRIV_CRED_SETEGID, 0))) {
568 if (cr->cr_groups[0] != egid) {
569 cr = cratom(&p->p_ucred);
570 cr->cr_groups[0] = egid;
575 lwkt_reltoken(&proc_token);
580 sys_setgroups(struct setgroups_args *uap)
582 struct proc *p = curproc;
587 lwkt_gettoken(&proc_token);
590 if ((error = priv_check_cred(cr, PRIV_CRED_SETGROUPS, 0)))
592 ngrp = uap->gidsetsize;
593 if (ngrp > NGROUPS) {
598 * XXX A little bit lazy here. We could test if anything has
599 * changed before cratom() and setting P_SUGID.
601 cr = cratom(&p->p_ucred);
604 * setgroups(0, NULL) is a legitimate way of clearing the
605 * groups vector on non-BSD systems (which generally do not
606 * have the egid in the groups[0]). We risk security holes
607 * when running non-BSD software if we do not do the same.
611 error = copyin(uap->gidset, cr->cr_groups,
612 ngrp * sizeof(gid_t));
615 cr->cr_ngroups = ngrp;
620 lwkt_reltoken(&proc_token);
625 sys_setreuid(struct setreuid_args *uap)
627 struct proc *p = curproc;
632 lwkt_gettoken(&proc_token);
637 if (((ruid != (uid_t)-1 && ruid != cr->cr_ruid && ruid != cr->cr_svuid) ||
638 (euid != (uid_t)-1 && euid != cr->cr_uid &&
639 euid != cr->cr_ruid && euid != cr->cr_svuid)) &&
640 (error = priv_check_cred(cr, PRIV_CRED_SETREUID, 0)) != 0) {
644 if (euid != (uid_t)-1 && cr->cr_uid != euid) {
645 cr = change_euid(euid);
648 if (ruid != (uid_t)-1 && cr->cr_ruid != ruid) {
649 cr = change_ruid(ruid);
652 if ((ruid != (uid_t)-1 || cr->cr_uid != cr->cr_ruid) &&
653 cr->cr_svuid != cr->cr_uid) {
654 cr = cratom(&p->p_ucred);
655 cr->cr_svuid = cr->cr_uid;
660 lwkt_reltoken(&proc_token);
665 sys_setregid(struct setregid_args *uap)
667 struct proc *p = curproc;
672 lwkt_gettoken(&proc_token);
677 if (((rgid != (gid_t)-1 && rgid != cr->cr_rgid && rgid != cr->cr_svgid) ||
678 (egid != (gid_t)-1 && egid != cr->cr_groups[0] &&
679 egid != cr->cr_rgid && egid != cr->cr_svgid)) &&
680 (error = priv_check_cred(cr, PRIV_CRED_SETREGID, 0)) != 0) {
684 if (egid != (gid_t)-1 && cr->cr_groups[0] != egid) {
685 cr = cratom(&p->p_ucred);
686 cr->cr_groups[0] = egid;
689 if (rgid != (gid_t)-1 && cr->cr_rgid != rgid) {
690 cr = cratom(&p->p_ucred);
694 if ((rgid != (gid_t)-1 || cr->cr_groups[0] != cr->cr_rgid) &&
695 cr->cr_svgid != cr->cr_groups[0]) {
696 cr = cratom(&p->p_ucred);
697 cr->cr_svgid = cr->cr_groups[0];
702 lwkt_reltoken(&proc_token);
707 * setresuid(ruid, euid, suid) is like setreuid except control over the
708 * saved uid is explicit.
711 sys_setresuid(struct setresuid_args *uap)
713 struct proc *p = curproc;
715 uid_t ruid, euid, suid;
718 lwkt_gettoken(&proc_token);
724 if (((ruid != (uid_t)-1 && ruid != cr->cr_ruid && ruid != cr->cr_svuid &&
725 ruid != cr->cr_uid) ||
726 (euid != (uid_t)-1 && euid != cr->cr_ruid && euid != cr->cr_svuid &&
727 euid != cr->cr_uid) ||
728 (suid != (uid_t)-1 && suid != cr->cr_ruid && suid != cr->cr_svuid &&
729 suid != cr->cr_uid)) &&
730 (error = priv_check_cred(cr, PRIV_CRED_SETRESUID, 0)) != 0) {
733 if (euid != (uid_t)-1 && cr->cr_uid != euid) {
734 cr = change_euid(euid);
737 if (ruid != (uid_t)-1 && cr->cr_ruid != ruid) {
738 cr = change_ruid(ruid);
741 if (suid != (uid_t)-1 && cr->cr_svuid != suid) {
742 cr = cratom(&p->p_ucred);
748 lwkt_reltoken(&proc_token);
753 * setresgid(rgid, egid, sgid) is like setregid except control over the
754 * saved gid is explicit.
757 sys_setresgid(struct setresgid_args *uap)
759 struct proc *p = curproc;
761 gid_t rgid, egid, sgid;
764 lwkt_gettoken(&proc_token);
769 if (((rgid != (gid_t)-1 && rgid != cr->cr_rgid && rgid != cr->cr_svgid &&
770 rgid != cr->cr_groups[0]) ||
771 (egid != (gid_t)-1 && egid != cr->cr_rgid && egid != cr->cr_svgid &&
772 egid != cr->cr_groups[0]) ||
773 (sgid != (gid_t)-1 && sgid != cr->cr_rgid && sgid != cr->cr_svgid &&
774 sgid != cr->cr_groups[0])) &&
775 (error = priv_check_cred(cr, PRIV_CRED_SETRESGID, 0)) != 0) {
779 if (egid != (gid_t)-1 && cr->cr_groups[0] != egid) {
780 cr = cratom(&p->p_ucred);
781 cr->cr_groups[0] = egid;
784 if (rgid != (gid_t)-1 && cr->cr_rgid != rgid) {
785 cr = cratom(&p->p_ucred);
789 if (sgid != (gid_t)-1 && cr->cr_svgid != sgid) {
790 cr = cratom(&p->p_ucred);
796 lwkt_reltoken(&proc_token);
801 sys_getresuid(struct getresuid_args *uap)
803 struct proc *p = curproc;
805 int error1 = 0, error2 = 0, error3 = 0;
807 lwkt_gettoken(&proc_token);
810 error1 = copyout((caddr_t)&cr->cr_ruid,
811 (caddr_t)uap->ruid, sizeof(cr->cr_ruid));
813 error2 = copyout((caddr_t)&cr->cr_uid,
814 (caddr_t)uap->euid, sizeof(cr->cr_uid));
816 error3 = copyout((caddr_t)&cr->cr_svuid,
817 (caddr_t)uap->suid, sizeof(cr->cr_svuid));
818 lwkt_reltoken(&proc_token);
819 return error1 ? error1 : (error2 ? error2 : error3);
826 sys_getresgid(struct getresgid_args *uap)
829 int error1 = 0, error2 = 0, error3 = 0;
831 cr = curthread->td_ucred;
833 error1 = copyout(&cr->cr_rgid, uap->rgid,
834 sizeof(cr->cr_rgid));
836 error2 = copyout(&cr->cr_groups[0], uap->egid,
837 sizeof(cr->cr_groups[0]));
839 error3 = copyout(&cr->cr_svgid, uap->sgid,
840 sizeof(cr->cr_svgid));
841 return error1 ? error1 : (error2 ? error2 : error3);
846 * NOTE: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
847 * we use P_SUGID because we consider changing the owners as
848 * "tainting" as well.
849 * This is significant for procs that start as root and "become"
850 * a user without an exec - programs cannot know *everything*
851 * that libc *might* have put in their data segment.
856 sys_issetugid(struct issetugid_args *uap)
858 uap->sysmsg_result = (curproc->p_flag & P_SUGID) ? 1 : 0;
863 * Check if gid is a member of the group set.
866 groupmember(gid_t gid, struct ucred *cred)
871 egp = &(cred->cr_groups[cred->cr_ngroups]);
872 for (gp = cred->cr_groups; gp < egp; gp++) {
880 * Test whether the specified credentials have the privilege
883 * A kernel thread without a process context is assumed to have
884 * the privilege in question. In situations where the caller always
885 * expect a cred to exist, the cred should be passed separately and
886 * priv_check_cred() should be used instead of priv_check().
888 * Returns 0 or error.
893 priv_check(struct thread *td, int priv)
895 if (td->td_lwp != NULL)
896 return priv_check_cred(td->td_ucred, priv, 0);
901 * Check a credential for privilege.
903 * A non-null credential is expected unless NULL_CRED_OKAY is set.
908 priv_check_cred(struct ucred *cred, int priv, int flags)
912 KASSERT(PRIV_VALID(priv), ("priv_check_cred: invalid privilege"));
914 KASSERT(cred != NULL || flags & NULL_CRED_OKAY,
915 ("priv_check_cred: NULL cred!"));
918 if (flags & NULL_CRED_OKAY)
923 if (cred->cr_uid != 0)
926 error = prison_priv_check(cred, priv);
930 /* NOTE: accounting for suser access (p_acflag/ASU) removed */
935 * Return zero if p1 can fondle p2, return errno (EPERM/ESRCH) otherwise.
938 p_trespass(struct ucred *cr1, struct ucred *cr2)
942 if (!PRISON_CHECK(cr1, cr2))
944 if (cr1->cr_ruid == cr2->cr_ruid)
946 if (cr1->cr_uid == cr2->cr_ruid)
948 if (cr1->cr_ruid == cr2->cr_uid)
950 if (cr1->cr_uid == cr2->cr_uid)
952 if (priv_check_cred(cr1, PRIV_PROC_TRESPASS, 0) == 0)
961 _crinit(struct ucred *cr)
970 crinit(struct ucred *cr)
972 bzero(cr, sizeof(*cr));
977 * Allocate a zeroed cred structure.
986 cr = kmalloc(sizeof(*cr), M_CRED, M_WAITOK|M_ZERO);
992 * Claim another reference to a ucred structure. Can be used with special
995 * It must be possible to call this routine with spinlocks held, meaning
996 * that this routine itself cannot obtain a spinlock.
1001 crhold(struct ucred *cr)
1003 if (cr != NOCRED && cr != FSCRED)
1004 atomic_add_int(&cr->cr_ref, 1);
1009 * Drop a reference from the cred structure, free it if the reference count
1012 * NOTE: because we used atomic_add_int() above, without a spinlock, we
1013 * must also use atomic_subtract_int() below. A spinlock is required
1014 * in crfree() to handle multiple callers racing the refcount to 0.
1019 crfree(struct ucred *cr)
1021 if (cr->cr_ref <= 0)
1022 panic("Freeing already free credential! %p", cr);
1023 if (atomic_fetchadd_int(&cr->cr_ref, -1) == 1) {
1025 * Some callers of crget(), such as nfs_statfs(),
1026 * allocate a temporary credential, but don't
1027 * allocate a uidinfo structure.
1029 if (cr->cr_uidinfo != NULL) {
1030 uidrop(cr->cr_uidinfo);
1031 cr->cr_uidinfo = NULL;
1033 if (cr->cr_ruidinfo != NULL) {
1034 uidrop(cr->cr_ruidinfo);
1035 cr->cr_ruidinfo = NULL;
1039 * Destroy empty prisons
1042 prison_free(cr->cr_prison);
1043 cr->cr_prison = NULL; /* safety */
1045 FREE((caddr_t)cr, M_CRED);
1050 * Atomize a cred structure so it can be modified without polluting
1051 * other references to it.
1053 * MPSAFE (however, *pcr must be stable)
1056 cratom(struct ucred **pcr)
1058 struct ucred *oldcr;
1059 struct ucred *newcr;
1062 if (oldcr->cr_ref == 1)
1066 if (newcr->cr_uidinfo)
1067 uihold(newcr->cr_uidinfo);
1068 if (newcr->cr_ruidinfo)
1069 uihold(newcr->cr_ruidinfo);
1071 prison_hold(newcr->cr_prison);
1078 #if 0 /* no longer used but keep around for a little while */
1080 * Copy cred structure to a new one and free the old one.
1082 * MPSAFE (*cr must be stable)
1085 crcopy(struct ucred *cr)
1087 struct ucred *newcr;
1089 if (cr->cr_ref == 1)
1093 if (newcr->cr_uidinfo)
1094 uihold(newcr->cr_uidinfo);
1095 if (newcr->cr_ruidinfo)
1096 uihold(newcr->cr_ruidinfo);
1098 prison_hold(newcr->cr_prison);
1106 * Dup cred struct to a new held one.
1109 crdup(struct ucred *cr)
1111 struct ucred *newcr;
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);
1126 * Fill in a struct xucred based on a struct ucred.
1129 cru2x(struct ucred *cr, struct xucred *xcr)
1132 bzero(xcr, sizeof(*xcr));
1133 xcr->cr_version = XUCRED_VERSION;
1134 xcr->cr_uid = cr->cr_uid;
1135 xcr->cr_ngroups = cr->cr_ngroups;
1136 bcopy(cr->cr_groups, xcr->cr_groups, sizeof(cr->cr_groups));
1140 * Get login name, if available.
1143 sys_getlogin(struct getlogin_args *uap)
1145 struct proc *p = curproc;
1146 char buf[MAXLOGNAME];
1149 if (uap->namelen > MAXLOGNAME) /* namelen is unsigned */
1150 uap->namelen = MAXLOGNAME;
1151 bzero(buf, sizeof(buf));
1152 lwkt_gettoken(&proc_token);
1153 bcopy(p->p_pgrp->pg_session->s_login, buf, uap->namelen);
1154 lwkt_reltoken(&proc_token);
1156 error = copyout(buf, uap->namebuf, uap->namelen);
1164 sys_setlogin(struct setlogin_args *uap)
1166 struct thread *td = curthread;
1169 char buf[MAXLOGNAME];
1172 cred = td->td_ucred;
1175 if ((error = priv_check_cred(cred, PRIV_PROC_SETLOGIN, 0)))
1177 bzero(buf, sizeof(buf));
1178 error = copyinstr(uap->namebuf, buf, sizeof(buf), NULL);
1179 if (error == ENAMETOOLONG)
1182 lwkt_gettoken(&proc_token);
1183 memcpy(p->p_pgrp->pg_session->s_login, buf, sizeof(buf));
1184 lwkt_reltoken(&proc_token);
1192 struct proc *p = curproc;
1194 KKASSERT(p != NULL);
1195 p->p_flag |= P_SUGID;
1196 if (!(p->p_pfsflags & PF_ISUGID))
1201 * Helper function to change the effective uid of a process
1204 change_euid(uid_t euid)
1206 struct proc *p = curproc;
1209 KKASSERT(p != NULL);
1210 lf_count_adjust(p, 0);
1211 cr = cratom(&p->p_ucred);
1213 uireplace(&cr->cr_uidinfo, uifind(euid));
1214 lf_count_adjust(p, 1);
1219 * Helper function to change the real uid of a process
1221 * The per-uid process count for this process is transfered from
1222 * the old uid to the new uid.
1225 change_ruid(uid_t ruid)
1227 struct proc *p = curproc;
1230 KKASSERT(p != NULL);
1232 cr = cratom(&p->p_ucred);
1233 chgproccnt(cr->cr_ruidinfo, -1, 0);
1235 uireplace(&cr->cr_ruidinfo, uifind(ruid));
1236 chgproccnt(cr->cr_ruidinfo, 1, 0);