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 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)kern_prot.c 8.6 (Berkeley) 1/21/94
35 * $FreeBSD: src/sys/kern/kern_prot.c,v 1.53.2.9 2002/03/09 05:20:26 dd Exp $
39 * System calls related to processes and protection
42 #include "opt_compat.h"
44 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/sysproto.h>
48 #include <sys/kernel.h>
52 #include <sys/malloc.h>
53 #include <sys/pioctl.h>
54 #include <sys/resourcevar.h>
56 #include <sys/lockf.h>
57 #include <sys/spinlock.h>
59 #include <sys/thread2.h>
60 #include <sys/spinlock2.h>
62 static MALLOC_DEFINE(M_CRED, "cred", "credentials");
65 sys_getpid(struct getpid_args *uap)
67 struct proc *p = curproc;
69 uap->sysmsg_fds[0] = p->p_pid;
70 #if defined(COMPAT_43)
71 lwkt_gettoken(&proc_token);
72 uap->sysmsg_fds[1] = p->p_pptr->p_pid;
73 lwkt_reltoken(&proc_token);
79 sys_getppid(struct getppid_args *uap)
81 struct proc *p = curproc;
83 lwkt_gettoken(&proc_token);
84 uap->sysmsg_result = p->p_pptr->p_pid;
85 lwkt_reltoken(&proc_token);
94 sys_lwp_gettid(struct lwp_gettid_args *uap)
96 struct lwp *lp = curthread->td_lwp;
98 uap->sysmsg_result = lp->lwp_tid;
103 * Get process group ID; note that POSIX getpgrp takes no parameter
108 sys_getpgrp(struct getpgrp_args *uap)
110 struct proc *p = curproc;
112 uap->sysmsg_result = p->p_pgrp->pg_id;
117 * Get an arbitrary pid's process group id
120 sys_getpgid(struct getpgid_args *uap)
122 struct proc *p = curproc;
132 pt = pfind(uap->pid);
136 /* XXX MPSAFE on pgrp? */
138 uap->sysmsg_result = pt->p_pgrp->pg_id;
145 * Get an arbitrary pid's session id.
148 sys_getsid(struct getsid_args *uap)
150 struct proc *p = curproc;
160 pt = pfind(uap->pid);
165 uap->sysmsg_result = pt->p_session->s_sid;
178 sys_getuid(struct getuid_args *uap)
180 struct ucred *cred = curthread->td_ucred;
182 uap->sysmsg_fds[0] = cred->cr_ruid;
183 #if defined(COMPAT_43)
184 uap->sysmsg_fds[1] = cred->cr_uid;
195 sys_geteuid(struct geteuid_args *uap)
197 struct ucred *cred = curthread->td_ucred;
199 uap->sysmsg_result = cred->cr_uid;
209 sys_getgid(struct getgid_args *uap)
211 struct ucred *cred = curthread->td_ucred;
213 uap->sysmsg_fds[0] = cred->cr_rgid;
214 #if defined(COMPAT_43)
215 uap->sysmsg_fds[1] = cred->cr_groups[0];
221 * Get effective group ID. The "egid" is groups[0], and could be obtained
222 * via getgroups. This syscall exists because it is somewhat painful to do
223 * correctly in a library function.
228 sys_getegid(struct getegid_args *uap)
230 struct ucred *cred = curthread->td_ucred;
232 uap->sysmsg_result = cred->cr_groups[0];
240 sys_getgroups(struct getgroups_args *uap)
246 cr = curthread->td_ucred;
247 if ((ngrp = uap->gidsetsize) == 0) {
248 uap->sysmsg_result = cr->cr_ngroups;
251 if (ngrp < cr->cr_ngroups)
253 ngrp = cr->cr_ngroups;
254 error = copyout((caddr_t)cr->cr_groups,
255 (caddr_t)uap->gidset, ngrp * sizeof(gid_t));
257 uap->sysmsg_result = ngrp;
262 sys_setsid(struct setsid_args *uap)
264 struct proc *p = curproc;
265 struct pgrp *pg = NULL;
268 lwkt_gettoken(&p->p_token);
269 if (p->p_pgid == p->p_pid || (pg = pgfind(p->p_pid)) != NULL) {
274 enterpgrp(p, p->p_pid, 1);
275 uap->sysmsg_result = p->p_pid;
278 lwkt_reltoken(&p->p_token);
283 * set process group (setpgid/old setpgrp)
285 * caller does setpgid(targpid, targpgid)
287 * pid must be caller or child of caller (ESRCH)
289 * pid must be in same session (EPERM)
290 * pid can't have done an exec (EACCES)
292 * there must exist some pid in same session having pgid (EPERM)
293 * pid must not be session leader (EPERM)
296 sys_setpgid(struct setpgid_args *uap)
298 struct proc *curp = curproc;
299 struct proc *targp; /* target process */
300 struct pgrp *pgrp = NULL; /* target pgrp */
306 if (uap->pid != 0 && uap->pid != curp->p_pid) {
307 if ((targp = pfind(uap->pid)) == NULL || !inferior(targp)) {
314 lwkt_gettoken(&targp->p_token);
315 /* targp now referenced and its token is held */
317 if (targp->p_pgrp == NULL ||
318 targp->p_session != curp->p_session) {
322 if (targp->p_flags & P_EXEC) {
329 lwkt_gettoken(&targp->p_token);
331 if (SESS_LEADER(targp)) {
335 if (uap->pgid == 0) {
336 uap->pgid = targp->p_pid;
337 } else if (uap->pgid != targp->p_pid) {
338 if ((pgrp = pgfind(uap->pgid)) == NULL ||
339 pgrp->pg_session != curp->p_session) {
344 error = enterpgrp(targp, uap->pgid, 0);
349 lwkt_reltoken(&targp->p_token);
356 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
357 * compatible. It says that setting the uid/gid to euid/egid is a special
358 * case of "appropriate privilege". Once the rules are expanded out, this
359 * basically means that setuid(nnn) sets all three id's, in all permitted
360 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
361 * does not set the saved id - this is dangerous for traditional BSD
362 * programs. For this reason, we *really* do not want to set
363 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
365 #define POSIX_APPENDIX_B_4_2_2
368 sys_setuid(struct setuid_args *uap)
370 struct proc *p = curproc;
375 lwkt_gettoken(&proc_token);
379 * See if we have "permission" by POSIX 1003.1 rules.
381 * Note that setuid(geteuid()) is a special case of
382 * "appropriate privileges" in appendix B.4.2.2. We need
383 * to use this clause to be compatible with traditional BSD
384 * semantics. Basically, it means that "setuid(xx)" sets all
385 * three id's (assuming you have privs).
387 * Notes on the logic. We do things in three steps.
388 * 1: We determine if the euid is going to change, and do EPERM
389 * right away. We unconditionally change the euid later if this
390 * test is satisfied, simplifying that part of the logic.
391 * 2: We determine if the real and/or saved uid's are going to
392 * change. Determined by compile options.
393 * 3: Change euid last. (after tests in #2 for "appropriate privs")
396 if (uid != cr->cr_ruid && /* allow setuid(getuid()) */
397 #ifdef _POSIX_SAVED_IDS
398 uid != crc->cr_svuid && /* allow setuid(saved gid) */
400 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
401 uid != cr->cr_uid && /* allow setuid(geteuid()) */
403 (error = priv_check_cred(cr, PRIV_CRED_SETUID, 0)))
406 #ifdef _POSIX_SAVED_IDS
408 * Do we have "appropriate privileges" (are we root or uid == euid)
409 * If so, we are changing the real uid and/or saved uid.
412 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
415 priv_check_cred(cr, PRIV_CRED_SETUID, 0) == 0) /* we are using privs */
419 * Set the real uid and transfer proc count to new user.
421 if (uid != cr->cr_ruid) {
422 cr = change_ruid(uid);
428 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
429 * the security of seteuid() depends on it. B.4.2.2 says it
430 * is important that we should do this.
432 if (cr->cr_svuid != uid) {
433 cr = cratom(&p->p_ucred);
440 * In all permitted cases, we are changing the euid.
441 * Copy credentials so other references do not see our changes.
443 if (cr->cr_uid != uid) {
449 lwkt_reltoken(&proc_token);
454 sys_seteuid(struct seteuid_args *uap)
456 struct proc *p = curproc;
461 lwkt_gettoken(&proc_token);
464 if (euid != cr->cr_ruid && /* allow seteuid(getuid()) */
465 euid != cr->cr_svuid && /* allow seteuid(saved uid) */
466 (error = priv_check_cred(cr, PRIV_CRED_SETEUID, 0))) {
467 lwkt_reltoken(&proc_token);
472 * Everything's okay, do it. Copy credentials so other references do
473 * not see our changes.
475 if (cr->cr_uid != euid) {
479 lwkt_reltoken(&proc_token);
484 sys_setgid(struct setgid_args *uap)
486 struct proc *p = curproc;
491 lwkt_gettoken(&proc_token);
495 * See if we have "permission" by POSIX 1003.1 rules.
497 * Note that setgid(getegid()) is a special case of
498 * "appropriate privileges" in appendix B.4.2.2. We need
499 * to use this clause to be compatible with traditional BSD
500 * semantics. Basically, it means that "setgid(xx)" sets all
501 * three id's (assuming you have privs).
503 * For notes on the logic here, see setuid() above.
506 if (gid != cr->cr_rgid && /* allow setgid(getgid()) */
507 #ifdef _POSIX_SAVED_IDS
508 gid != cr->cr_svgid && /* allow setgid(saved gid) */
510 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
511 gid != cr->cr_groups[0] && /* allow setgid(getegid()) */
513 (error = priv_check_cred(cr, PRIV_CRED_SETGID, 0))) {
517 #ifdef _POSIX_SAVED_IDS
519 * Do we have "appropriate privileges" (are we root or gid == egid)
520 * If so, we are changing the real uid and saved gid.
523 #ifdef POSIX_APPENDIX_B_4_2_2 /* use the clause from B.4.2.2 */
524 gid == cr->cr_groups[0] ||
526 priv_check_cred(cr, PRIV_CRED_SETGID, 0) == 0) /* we are using privs */
532 if (cr->cr_rgid != gid) {
533 cr = cratom(&p->p_ucred);
540 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
541 * the security of setegid() depends on it. B.4.2.2 says it
542 * is important that we should do this.
544 if (cr->cr_svgid != gid) {
545 cr = cratom(&p->p_ucred);
551 * In all cases permitted cases, we are changing the egid.
552 * Copy credentials so other references do not see our changes.
554 if (cr->cr_groups[0] != gid) {
555 cr = cratom(&p->p_ucred);
556 cr->cr_groups[0] = gid;
561 lwkt_reltoken(&proc_token);
566 sys_setegid(struct setegid_args *uap)
568 struct proc *p = curproc;
573 lwkt_gettoken(&proc_token);
576 if (egid != cr->cr_rgid && /* allow setegid(getgid()) */
577 egid != cr->cr_svgid && /* allow setegid(saved gid) */
578 (error = priv_check_cred(cr, PRIV_CRED_SETEGID, 0))) {
581 if (cr->cr_groups[0] != egid) {
582 cr = cratom(&p->p_ucred);
583 cr->cr_groups[0] = egid;
588 lwkt_reltoken(&proc_token);
593 sys_setgroups(struct setgroups_args *uap)
595 struct proc *p = curproc;
600 lwkt_gettoken(&proc_token);
603 if ((error = priv_check_cred(cr, PRIV_CRED_SETGROUPS, 0)))
605 ngrp = uap->gidsetsize;
606 if (ngrp > NGROUPS) {
611 * XXX A little bit lazy here. We could test if anything has
612 * changed before cratom() and setting P_SUGID.
614 cr = cratom(&p->p_ucred);
617 * setgroups(0, NULL) is a legitimate way of clearing the
618 * groups vector on non-BSD systems (which generally do not
619 * have the egid in the groups[0]). We risk security holes
620 * when running non-BSD software if we do not do the same.
624 error = copyin(uap->gidset, cr->cr_groups,
625 ngrp * sizeof(gid_t));
628 cr->cr_ngroups = ngrp;
633 lwkt_reltoken(&proc_token);
638 sys_setreuid(struct setreuid_args *uap)
640 struct proc *p = curproc;
645 lwkt_gettoken(&proc_token);
650 if (((ruid != (uid_t)-1 && ruid != cr->cr_ruid && ruid != cr->cr_svuid) ||
651 (euid != (uid_t)-1 && euid != cr->cr_uid &&
652 euid != cr->cr_ruid && euid != cr->cr_svuid)) &&
653 (error = priv_check_cred(cr, PRIV_CRED_SETREUID, 0)) != 0) {
657 if (euid != (uid_t)-1 && cr->cr_uid != euid) {
658 cr = change_euid(euid);
661 if (ruid != (uid_t)-1 && cr->cr_ruid != ruid) {
662 cr = change_ruid(ruid);
665 if ((ruid != (uid_t)-1 || cr->cr_uid != cr->cr_ruid) &&
666 cr->cr_svuid != cr->cr_uid) {
667 cr = cratom(&p->p_ucred);
668 cr->cr_svuid = cr->cr_uid;
673 lwkt_reltoken(&proc_token);
678 sys_setregid(struct setregid_args *uap)
680 struct proc *p = curproc;
685 lwkt_gettoken(&proc_token);
690 if (((rgid != (gid_t)-1 && rgid != cr->cr_rgid && rgid != cr->cr_svgid) ||
691 (egid != (gid_t)-1 && egid != cr->cr_groups[0] &&
692 egid != cr->cr_rgid && egid != cr->cr_svgid)) &&
693 (error = priv_check_cred(cr, PRIV_CRED_SETREGID, 0)) != 0) {
697 if (egid != (gid_t)-1 && cr->cr_groups[0] != egid) {
698 cr = cratom(&p->p_ucred);
699 cr->cr_groups[0] = egid;
702 if (rgid != (gid_t)-1 && cr->cr_rgid != rgid) {
703 cr = cratom(&p->p_ucred);
707 if ((rgid != (gid_t)-1 || cr->cr_groups[0] != cr->cr_rgid) &&
708 cr->cr_svgid != cr->cr_groups[0]) {
709 cr = cratom(&p->p_ucred);
710 cr->cr_svgid = cr->cr_groups[0];
715 lwkt_reltoken(&proc_token);
720 * setresuid(ruid, euid, suid) is like setreuid except control over the
721 * saved uid is explicit.
724 sys_setresuid(struct setresuid_args *uap)
726 struct proc *p = curproc;
728 uid_t ruid, euid, suid;
731 lwkt_gettoken(&proc_token);
737 if (((ruid != (uid_t)-1 && ruid != cr->cr_ruid && ruid != cr->cr_svuid &&
738 ruid != cr->cr_uid) ||
739 (euid != (uid_t)-1 && euid != cr->cr_ruid && euid != cr->cr_svuid &&
740 euid != cr->cr_uid) ||
741 (suid != (uid_t)-1 && suid != cr->cr_ruid && suid != cr->cr_svuid &&
742 suid != cr->cr_uid)) &&
743 (error = priv_check_cred(cr, PRIV_CRED_SETRESUID, 0)) != 0) {
746 if (euid != (uid_t)-1 && cr->cr_uid != euid) {
747 cr = change_euid(euid);
750 if (ruid != (uid_t)-1 && cr->cr_ruid != ruid) {
751 cr = change_ruid(ruid);
754 if (suid != (uid_t)-1 && cr->cr_svuid != suid) {
755 cr = cratom(&p->p_ucred);
761 lwkt_reltoken(&proc_token);
766 * setresgid(rgid, egid, sgid) is like setregid except control over the
767 * saved gid is explicit.
770 sys_setresgid(struct setresgid_args *uap)
772 struct proc *p = curproc;
774 gid_t rgid, egid, sgid;
777 lwkt_gettoken(&proc_token);
782 if (((rgid != (gid_t)-1 && rgid != cr->cr_rgid && rgid != cr->cr_svgid &&
783 rgid != cr->cr_groups[0]) ||
784 (egid != (gid_t)-1 && egid != cr->cr_rgid && egid != cr->cr_svgid &&
785 egid != cr->cr_groups[0]) ||
786 (sgid != (gid_t)-1 && sgid != cr->cr_rgid && sgid != cr->cr_svgid &&
787 sgid != cr->cr_groups[0])) &&
788 (error = priv_check_cred(cr, PRIV_CRED_SETRESGID, 0)) != 0) {
792 if (egid != (gid_t)-1 && cr->cr_groups[0] != egid) {
793 cr = cratom(&p->p_ucred);
794 cr->cr_groups[0] = egid;
797 if (rgid != (gid_t)-1 && cr->cr_rgid != rgid) {
798 cr = cratom(&p->p_ucred);
802 if (sgid != (gid_t)-1 && cr->cr_svgid != sgid) {
803 cr = cratom(&p->p_ucred);
809 lwkt_reltoken(&proc_token);
814 sys_getresuid(struct getresuid_args *uap)
816 struct proc *p = curproc;
818 int error1 = 0, error2 = 0, error3 = 0;
820 lwkt_gettoken(&proc_token);
823 error1 = copyout((caddr_t)&cr->cr_ruid,
824 (caddr_t)uap->ruid, sizeof(cr->cr_ruid));
826 error2 = copyout((caddr_t)&cr->cr_uid,
827 (caddr_t)uap->euid, sizeof(cr->cr_uid));
829 error3 = copyout((caddr_t)&cr->cr_svuid,
830 (caddr_t)uap->suid, sizeof(cr->cr_svuid));
831 lwkt_reltoken(&proc_token);
832 return error1 ? error1 : (error2 ? error2 : error3);
839 sys_getresgid(struct getresgid_args *uap)
842 int error1 = 0, error2 = 0, error3 = 0;
844 cr = curthread->td_ucred;
846 error1 = copyout(&cr->cr_rgid, uap->rgid,
847 sizeof(cr->cr_rgid));
849 error2 = copyout(&cr->cr_groups[0], uap->egid,
850 sizeof(cr->cr_groups[0]));
852 error3 = copyout(&cr->cr_svgid, uap->sgid,
853 sizeof(cr->cr_svgid));
854 return error1 ? error1 : (error2 ? error2 : error3);
859 * NOTE: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
860 * we use P_SUGID because we consider changing the owners as
861 * "tainting" as well.
862 * This is significant for procs that start as root and "become"
863 * a user without an exec - programs cannot know *everything*
864 * that libc *might* have put in their data segment.
869 sys_issetugid(struct issetugid_args *uap)
871 uap->sysmsg_result = (curproc->p_flags & P_SUGID) ? 1 : 0;
876 * Check if gid is a member of the group set.
879 groupmember(gid_t gid, struct ucred *cred)
884 egp = &(cred->cr_groups[cred->cr_ngroups]);
885 for (gp = cred->cr_groups; gp < egp; gp++) {
893 * Test whether the specified credentials have the privilege
896 * A kernel thread without a process context is assumed to have
897 * the privilege in question. In situations where the caller always
898 * expect a cred to exist, the cred should be passed separately and
899 * priv_check_cred() should be used instead of priv_check().
901 * Returns 0 or error.
906 priv_check(struct thread *td, int priv)
908 if (td->td_lwp != NULL)
909 return priv_check_cred(td->td_ucred, priv, 0);
914 * Check a credential for privilege.
916 * A non-null credential is expected unless NULL_CRED_OKAY is set.
921 priv_check_cred(struct ucred *cred, int priv, int flags)
925 KASSERT(PRIV_VALID(priv), ("priv_check_cred: invalid privilege"));
927 KASSERT(cred != NULL || flags & NULL_CRED_OKAY,
928 ("priv_check_cred: NULL cred!"));
931 if (flags & NULL_CRED_OKAY)
936 if (cred->cr_uid != 0)
939 error = prison_priv_check(cred, priv);
943 /* NOTE: accounting for suser access (p_acflag/ASU) removed */
948 * Return zero if p1 can fondle p2, return errno (EPERM/ESRCH) otherwise.
951 p_trespass(struct ucred *cr1, struct ucred *cr2)
955 if (!PRISON_CHECK(cr1, cr2))
957 if (cr1->cr_ruid == cr2->cr_ruid)
959 if (cr1->cr_uid == cr2->cr_ruid)
961 if (cr1->cr_ruid == cr2->cr_uid)
963 if (cr1->cr_uid == cr2->cr_uid)
965 if (priv_check_cred(cr1, PRIV_PROC_TRESPASS, 0) == 0)
974 _crinit(struct ucred *cr)
983 crinit(struct ucred *cr)
985 bzero(cr, sizeof(*cr));
990 * Allocate a zeroed cred structure.
999 cr = kmalloc(sizeof(*cr), M_CRED, M_WAITOK|M_ZERO);
1005 * Claim another reference to a ucred structure. Can be used with special
1008 * It must be possible to call this routine with spinlocks held, meaning
1009 * that this routine itself cannot obtain a spinlock.
1014 crhold(struct ucred *cr)
1016 if (cr != NOCRED && cr != FSCRED)
1017 atomic_add_int(&cr->cr_ref, 1);
1022 * Drop a reference from the cred structure, free it if the reference count
1025 * NOTE: because we used atomic_add_int() above, without a spinlock, we
1026 * must also use atomic_subtract_int() below. A spinlock is required
1027 * in crfree() to handle multiple callers racing the refcount to 0.
1032 crfree(struct ucred *cr)
1034 if (cr->cr_ref <= 0)
1035 panic("Freeing already free credential! %p", cr);
1036 if (atomic_fetchadd_int(&cr->cr_ref, -1) == 1) {
1038 * Some callers of crget(), such as nfs_statfs(),
1039 * allocate a temporary credential, but don't
1040 * allocate a uidinfo structure.
1042 if (cr->cr_uidinfo != NULL) {
1043 uidrop(cr->cr_uidinfo);
1044 cr->cr_uidinfo = NULL;
1046 if (cr->cr_ruidinfo != NULL) {
1047 uidrop(cr->cr_ruidinfo);
1048 cr->cr_ruidinfo = NULL;
1052 * Destroy empty prisons
1055 prison_free(cr->cr_prison);
1056 cr->cr_prison = NULL; /* safety */
1058 kfree((caddr_t)cr, M_CRED);
1063 * Atomize a cred structure so it can be modified without polluting
1064 * other references to it.
1066 * MPSAFE (however, *pcr must be stable)
1069 cratom(struct ucred **pcr)
1071 struct ucred *oldcr;
1072 struct ucred *newcr;
1075 if (oldcr->cr_ref == 1)
1079 if (newcr->cr_uidinfo)
1080 uihold(newcr->cr_uidinfo);
1081 if (newcr->cr_ruidinfo)
1082 uihold(newcr->cr_ruidinfo);
1084 prison_hold(newcr->cr_prison);
1091 #if 0 /* no longer used but keep around for a little while */
1093 * Copy cred structure to a new one and free the old one.
1095 * MPSAFE (*cr must be stable)
1098 crcopy(struct ucred *cr)
1100 struct ucred *newcr;
1102 if (cr->cr_ref == 1)
1106 if (newcr->cr_uidinfo)
1107 uihold(newcr->cr_uidinfo);
1108 if (newcr->cr_ruidinfo)
1109 uihold(newcr->cr_ruidinfo);
1111 prison_hold(newcr->cr_prison);
1119 * Dup cred struct to a new held one.
1122 crdup(struct ucred *cr)
1124 struct ucred *newcr;
1128 if (newcr->cr_uidinfo)
1129 uihold(newcr->cr_uidinfo);
1130 if (newcr->cr_ruidinfo)
1131 uihold(newcr->cr_ruidinfo);
1133 prison_hold(newcr->cr_prison);
1139 * Fill in a struct xucred based on a struct ucred.
1142 cru2x(struct ucred *cr, struct xucred *xcr)
1145 bzero(xcr, sizeof(*xcr));
1146 xcr->cr_version = XUCRED_VERSION;
1147 xcr->cr_uid = cr->cr_uid;
1148 xcr->cr_ngroups = cr->cr_ngroups;
1149 bcopy(cr->cr_groups, xcr->cr_groups, sizeof(cr->cr_groups));
1153 * Get login name, if available.
1156 sys_getlogin(struct getlogin_args *uap)
1158 struct proc *p = curproc;
1159 char buf[MAXLOGNAME];
1162 if (uap->namelen > MAXLOGNAME) /* namelen is unsigned */
1163 uap->namelen = MAXLOGNAME;
1164 bzero(buf, sizeof(buf));
1165 lwkt_gettoken(&proc_token);
1166 bcopy(p->p_pgrp->pg_session->s_login, buf, uap->namelen);
1167 lwkt_reltoken(&proc_token);
1169 error = copyout(buf, uap->namebuf, uap->namelen);
1177 sys_setlogin(struct setlogin_args *uap)
1179 struct thread *td = curthread;
1182 char buf[MAXLOGNAME];
1185 cred = td->td_ucred;
1188 if ((error = priv_check_cred(cred, PRIV_PROC_SETLOGIN, 0)))
1190 bzero(buf, sizeof(buf));
1191 error = copyinstr(uap->namebuf, buf, sizeof(buf), NULL);
1192 if (error == ENAMETOOLONG)
1195 lwkt_gettoken(&proc_token);
1196 memcpy(p->p_pgrp->pg_session->s_login, buf, sizeof(buf));
1197 lwkt_reltoken(&proc_token);
1205 struct proc *p = curproc;
1207 KKASSERT(p != NULL);
1208 lwkt_gettoken(&p->p_token);
1209 p->p_flags |= P_SUGID;
1210 if (!(p->p_pfsflags & PF_ISUGID))
1212 lwkt_reltoken(&p->p_token);
1216 * Helper function to change the effective uid of a process
1219 change_euid(uid_t euid)
1221 struct proc *p = curproc;
1224 KKASSERT(p != NULL);
1225 lf_count_adjust(p, 0);
1226 cr = cratom(&p->p_ucred);
1228 uireplace(&cr->cr_uidinfo, uifind(euid));
1229 lf_count_adjust(p, 1);
1234 * Helper function to change the real uid of a process
1236 * The per-uid process count for this process is transfered from
1237 * the old uid to the new uid.
1240 change_ruid(uid_t ruid)
1242 struct proc *p = curproc;
1245 KKASSERT(p != NULL);
1247 cr = cratom(&p->p_ucred);
1248 chgproccnt(cr->cr_ruidinfo, -1, 0);
1250 uireplace(&cr->cr_ruidinfo, uifind(ruid));
1251 chgproccnt(cr->cr_ruidinfo, 1, 0);