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. 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 <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/sysproto.h>
46 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #include <sys/pioctl.h>
52 #include <sys/resourcevar.h>
54 #include <sys/lockf.h>
55 #include <sys/spinlock.h>
57 #include <sys/thread2.h>
58 #include <sys/spinlock2.h>
60 static MALLOC_DEFINE(M_CRED, "cred", "credentials");
63 sys_getpid(struct getpid_args *uap)
65 struct proc *p = curproc;
67 uap->sysmsg_fds[0] = p->p_pid;
72 sys_getppid(struct getppid_args *uap)
74 struct proc *p = curproc;
76 uap->sysmsg_result = p->p_ppid;
82 sys_lwp_gettid(struct lwp_gettid_args *uap)
84 struct lwp *lp = curthread->td_lwp;
85 uap->sysmsg_result = lp->lwp_tid;
90 * Get process group ID; note that POSIX getpgrp takes no parameter
93 sys_getpgrp(struct getpgrp_args *uap)
95 struct proc *p = curproc;
97 lwkt_gettoken_shared(&p->p_token);
98 uap->sysmsg_result = p->p_pgrp->pg_id;
99 lwkt_reltoken(&p->p_token);
105 * Get an arbitrary pid's process group id
108 sys_getpgid(struct getpgid_args *uap)
110 struct proc *p = curproc;
120 pt = pfind(uap->pid);
125 lwkt_gettoken_shared(&pt->p_token);
126 uap->sysmsg_result = pt->p_pgrp->pg_id;
127 lwkt_reltoken(&pt->p_token);
135 * Get an arbitrary pid's session id.
138 sys_getsid(struct getsid_args *uap)
140 struct proc *p = curproc;
150 pt = pfind(uap->pid);
155 uap->sysmsg_result = pt->p_session->s_sid;
166 sys_getuid(struct getuid_args *uap)
168 struct ucred *cred = curthread->td_ucred;
170 uap->sysmsg_fds[0] = cred->cr_ruid;
178 sys_geteuid(struct geteuid_args *uap)
180 struct ucred *cred = curthread->td_ucred;
182 uap->sysmsg_result = cred->cr_uid;
190 sys_getgid(struct getgid_args *uap)
192 struct ucred *cred = curthread->td_ucred;
194 uap->sysmsg_fds[0] = cred->cr_rgid;
199 * Get effective group ID. The "egid" is groups[0], and could be obtained
200 * via getgroups. This syscall exists because it is somewhat painful to do
201 * correctly in a library function.
204 sys_getegid(struct getegid_args *uap)
206 struct ucred *cred = curthread->td_ucred;
208 uap->sysmsg_result = cred->cr_groups[0];
213 sys_getgroups(struct getgroups_args *uap)
219 cr = curthread->td_ucred;
220 if ((ngrp = uap->gidsetsize) == 0) {
221 uap->sysmsg_result = cr->cr_ngroups;
224 if (ngrp < cr->cr_ngroups)
226 ngrp = cr->cr_ngroups;
227 error = copyout((caddr_t)cr->cr_groups,
228 (caddr_t)uap->gidset, ngrp * sizeof(gid_t));
230 uap->sysmsg_result = ngrp;
235 sys_lwp_setname(struct lwp_setname_args *uap)
237 struct proc *p = curproc;
238 char comm0[MAXCOMLEN + 1];
239 const char *comm = NULL;
243 if (uap->name != NULL) {
244 error = copyinstr(uap->name, comm0, sizeof(comm0), NULL);
246 if (error != ENAMETOOLONG)
249 comm0[MAXCOMLEN] = '\0';
253 /* Restore to the default name, i.e. process name. */
257 lwkt_gettoken(&p->p_token);
259 lp = lwp_rb_tree_RB_LOOKUP(&p->p_lwp_tree, uap->tid);
261 strlcpy(lp->lwp_thread->td_comm, comm,
262 sizeof(lp->lwp_thread->td_comm));
268 lwkt_reltoken(&p->p_token);
273 sys_setsid(struct setsid_args *uap)
275 struct proc *p = curproc;
276 struct pgrp *pg = NULL;
279 lwkt_gettoken(&p->p_token);
280 if (p->p_pgid == p->p_pid || (pg = pgfind(p->p_pid)) != NULL) {
285 enterpgrp(p, p->p_pid, 1);
286 uap->sysmsg_result = p->p_pid;
289 lwkt_reltoken(&p->p_token);
294 * set process group (setpgid/old setpgrp)
296 * caller does setpgid(targpid, targpgid)
298 * pid must be caller or child of caller (ESRCH)
300 * pid must be in same session (EPERM)
301 * pid can't have done an exec (EACCES)
303 * there must exist some pid in same session having pgid (EPERM)
304 * pid must not be session leader (EPERM)
307 sys_setpgid(struct setpgid_args *uap)
309 struct proc *curp = curproc;
310 struct proc *targp; /* target process */
311 struct pgrp *pgrp = NULL; /* target pgrp */
317 if (uap->pid != 0 && uap->pid != curp->p_pid) {
318 if ((targp = pfind(uap->pid)) == NULL || !inferior(targp)) {
325 lwkt_gettoken(&targp->p_token);
326 /* targp now referenced and its token is held */
328 if (targp->p_pgrp == NULL ||
329 targp->p_session != curp->p_session) {
333 if (targp->p_flags & P_EXEC) {
340 lwkt_gettoken(&targp->p_token);
342 if (SESS_LEADER(targp)) {
346 if (uap->pgid == 0) {
347 uap->pgid = targp->p_pid;
348 } else if (uap->pgid != targp->p_pid) {
349 if ((pgrp = pgfind(uap->pgid)) == NULL ||
350 pgrp->pg_session != curp->p_session) {
355 error = enterpgrp(targp, uap->pgid, 0);
360 lwkt_reltoken(&targp->p_token);
367 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
368 * compatible. It says that setting the uid/gid to euid/egid is a special
369 * case of "appropriate privilege". Once the rules are expanded out, this
370 * basically means that setuid(nnn) sets all three id's, in all permitted
371 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
372 * does not set the saved id - this is dangerous for traditional BSD
373 * programs. For this reason, we *really* do not want to set
374 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
376 #define POSIX_APPENDIX_B_4_2_2
379 sys_setuid(struct setuid_args *uap)
381 struct proc *p = curproc;
386 lwkt_gettoken(&p->p_token);
390 * See if we have "permission" by POSIX 1003.1 rules.
392 * Note that setuid(geteuid()) is a special case of
393 * "appropriate privileges" in appendix B.4.2.2. We need
394 * to use this clause to be compatible with traditional BSD
395 * semantics. Basically, it means that "setuid(xx)" sets all
396 * three id's (assuming you have privs).
398 * Notes on the logic. We do things in three steps.
399 * 1: We determine if the euid is going to change, and do EPERM
400 * right away. We unconditionally change the euid later if this
401 * test is satisfied, simplifying that part of the logic.
402 * 2: We determine if the real and/or saved uid's are going to
403 * change. Determined by compile options.
404 * 3: Change euid last. (after tests in #2 for "appropriate privs")
407 if (uid != cr->cr_ruid && /* allow setuid(getuid()) */
408 #ifdef _POSIX_SAVED_IDS
409 uid != crc->cr_svuid && /* allow setuid(saved gid) */
411 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
412 uid != cr->cr_uid && /* allow setuid(geteuid()) */
414 (error = priv_check_cred(cr, PRIV_CRED_SETUID, 0)))
417 #ifdef _POSIX_SAVED_IDS
419 * Do we have "appropriate privileges" (are we root or uid == euid)
420 * If so, we are changing the real uid and/or saved uid.
423 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
426 priv_check_cred(cr, PRIV_CRED_SETUID, 0) == 0) /* we are using privs */
430 * Set the real uid and transfer proc count to new user.
432 if (uid != cr->cr_ruid) {
433 cr = change_ruid(uid);
439 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
440 * the security of seteuid() depends on it. B.4.2.2 says it
441 * is important that we should do this.
443 if (cr->cr_svuid != uid) {
451 * In all permitted cases, we are changing the euid.
452 * Copy credentials so other references do not see our changes.
454 if (cr->cr_uid != uid) {
460 lwkt_reltoken(&p->p_token);
465 sys_seteuid(struct seteuid_args *uap)
467 struct proc *p = curproc;
472 lwkt_gettoken(&p->p_token);
475 if (euid != cr->cr_ruid && /* allow seteuid(getuid()) */
476 euid != cr->cr_svuid && /* allow seteuid(saved uid) */
477 (error = priv_check_cred(cr, PRIV_CRED_SETEUID, 0))) {
478 lwkt_reltoken(&p->p_token);
483 * Everything's okay, do it. Copy credentials so other references do
484 * not see our changes.
486 if (cr->cr_uid != euid) {
490 lwkt_reltoken(&p->p_token);
495 sys_setgid(struct setgid_args *uap)
497 struct proc *p = curproc;
502 lwkt_gettoken(&p->p_token);
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) {
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) {
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) {
567 cr->cr_groups[0] = gid;
572 lwkt_reltoken(&p->p_token);
577 sys_setegid(struct setegid_args *uap)
579 struct proc *p = curproc;
584 lwkt_gettoken(&p->p_token);
587 if (egid != cr->cr_rgid && /* allow setegid(getgid()) */
588 egid != cr->cr_svgid && /* allow setegid(saved gid) */
589 (error = priv_check_cred(cr, PRIV_CRED_SETEGID, 0))) {
592 if (cr->cr_groups[0] != egid) {
594 cr->cr_groups[0] = egid;
599 lwkt_reltoken(&p->p_token);
604 sys_setgroups(struct setgroups_args *uap)
606 struct proc *p = curproc;
611 lwkt_gettoken(&p->p_token);
614 if ((error = priv_check_cred(cr, PRIV_CRED_SETGROUPS, 0)))
616 ngrp = uap->gidsetsize;
617 if (ngrp > NGROUPS) {
622 * XXX A little bit lazy here. We could test if anything has
623 * changed before cratom() and setting P_SUGID.
628 * setgroups(0, NULL) is a legitimate way of clearing the
629 * groups vector on non-BSD systems (which generally do not
630 * have the egid in the groups[0]). We risk security holes
631 * when running non-BSD software if we do not do the same.
635 error = copyin(uap->gidset, cr->cr_groups,
636 ngrp * sizeof(gid_t));
639 cr->cr_ngroups = ngrp;
644 lwkt_reltoken(&p->p_token);
649 sys_setreuid(struct setreuid_args *uap)
651 struct proc *p = curproc;
656 lwkt_gettoken(&p->p_token);
661 if (((ruid != (uid_t)-1 && ruid != cr->cr_ruid &&
662 ruid != cr->cr_svuid) ||
663 (euid != (uid_t)-1 && euid != cr->cr_uid &&
664 euid != cr->cr_ruid && euid != cr->cr_svuid)) &&
665 (error = priv_check_cred(cr, PRIV_CRED_SETREUID, 0)) != 0) {
669 if (euid != (uid_t)-1 && cr->cr_uid != euid) {
670 cr = change_euid(euid);
673 if (ruid != (uid_t)-1 && cr->cr_ruid != ruid) {
674 cr = change_ruid(ruid);
677 if ((ruid != (uid_t)-1 || cr->cr_uid != cr->cr_ruid) &&
678 cr->cr_svuid != cr->cr_uid) {
680 cr->cr_svuid = cr->cr_uid;
685 lwkt_reltoken(&p->p_token);
690 sys_setregid(struct setregid_args *uap)
692 struct proc *p = curproc;
697 lwkt_gettoken(&p->p_token);
702 if (((rgid != (gid_t)-1 && rgid != cr->cr_rgid &&
703 rgid != cr->cr_svgid) ||
704 (egid != (gid_t)-1 && egid != cr->cr_groups[0] &&
705 egid != cr->cr_rgid && egid != cr->cr_svgid)) &&
706 (error = priv_check_cred(cr, PRIV_CRED_SETREGID, 0)) != 0) {
710 if (egid != (gid_t)-1 && cr->cr_groups[0] != egid) {
712 cr->cr_groups[0] = egid;
715 if (rgid != (gid_t)-1 && cr->cr_rgid != rgid) {
720 if ((rgid != (gid_t)-1 || cr->cr_groups[0] != cr->cr_rgid) &&
721 cr->cr_svgid != cr->cr_groups[0]) {
723 cr->cr_svgid = cr->cr_groups[0];
728 lwkt_reltoken(&p->p_token);
733 * setresuid(ruid, euid, suid) is like setreuid except control over the
734 * saved uid is explicit.
737 sys_setresuid(struct setresuid_args *uap)
739 struct proc *p = curproc;
741 uid_t ruid, euid, suid;
744 lwkt_gettoken(&p->p_token);
750 if (((ruid != (uid_t)-1 && ruid != cr->cr_ruid &&
751 ruid != cr->cr_svuid && ruid != cr->cr_uid) ||
752 (euid != (uid_t)-1 && euid != cr->cr_ruid &&
753 euid != cr->cr_svuid && euid != cr->cr_uid) ||
754 (suid != (uid_t)-1 && suid != cr->cr_ruid &&
755 suid != cr->cr_svuid && suid != cr->cr_uid)) &&
756 (error = priv_check_cred(cr, PRIV_CRED_SETRESUID, 0)) != 0) {
759 if (euid != (uid_t)-1 && cr->cr_uid != euid) {
760 cr = change_euid(euid);
763 if (ruid != (uid_t)-1 && cr->cr_ruid != ruid) {
764 cr = change_ruid(ruid);
767 if (suid != (uid_t)-1 && cr->cr_svuid != suid) {
774 lwkt_reltoken(&p->p_token);
779 * setresgid(rgid, egid, sgid) is like setregid except control over the
780 * saved gid is explicit.
783 sys_setresgid(struct setresgid_args *uap)
785 struct proc *p = curproc;
787 gid_t rgid, egid, sgid;
790 lwkt_gettoken(&p->p_token);
795 if (((rgid != (gid_t)-1 && rgid != cr->cr_rgid &&
796 rgid != cr->cr_svgid && rgid != cr->cr_groups[0]) ||
797 (egid != (gid_t)-1 && egid != cr->cr_rgid &&
798 egid != cr->cr_svgid && egid != cr->cr_groups[0]) ||
799 (sgid != (gid_t)-1 && sgid != cr->cr_rgid &&
800 sgid != cr->cr_svgid && sgid != cr->cr_groups[0])) &&
801 (error = priv_check_cred(cr, PRIV_CRED_SETRESGID, 0)) != 0) {
805 if (egid != (gid_t)-1 && cr->cr_groups[0] != egid) {
807 cr->cr_groups[0] = egid;
810 if (rgid != (gid_t)-1 && cr->cr_rgid != rgid) {
815 if (sgid != (gid_t)-1 && cr->cr_svgid != sgid) {
822 lwkt_reltoken(&p->p_token);
827 sys_getresuid(struct getresuid_args *uap)
830 int error1 = 0, error2 = 0, error3 = 0;
833 * copyout's can fault synchronously so we cannot use a shared
836 cr = curthread->td_ucred;
838 error1 = copyout((caddr_t)&cr->cr_ruid,
839 (caddr_t)uap->ruid, sizeof(cr->cr_ruid));
841 error2 = copyout((caddr_t)&cr->cr_uid,
842 (caddr_t)uap->euid, sizeof(cr->cr_uid));
844 error3 = copyout((caddr_t)&cr->cr_svuid,
845 (caddr_t)uap->suid, sizeof(cr->cr_svuid));
846 return error1 ? error1 : (error2 ? error2 : error3);
850 sys_getresgid(struct getresgid_args *uap)
853 int error1 = 0, error2 = 0, error3 = 0;
855 cr = curthread->td_ucred;
857 error1 = copyout(&cr->cr_rgid, uap->rgid,
858 sizeof(cr->cr_rgid));
860 error2 = copyout(&cr->cr_groups[0], uap->egid,
861 sizeof(cr->cr_groups[0]));
863 error3 = copyout(&cr->cr_svgid, uap->sgid,
864 sizeof(cr->cr_svgid));
865 return error1 ? error1 : (error2 ? error2 : error3);
870 * NOTE: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
871 * we use P_SUGID because we consider changing the owners as
872 * "tainting" as well.
873 * This is significant for procs that start as root and "become"
874 * a user without an exec - programs cannot know *everything*
875 * that libc *might* have put in their data segment.
878 sys_issetugid(struct issetugid_args *uap)
880 uap->sysmsg_result = (curproc->p_flags & P_SUGID) ? 1 : 0;
885 * Check if gid is a member of the group set.
888 groupmember(gid_t gid, struct ucred *cred)
893 egp = &(cred->cr_groups[cred->cr_ngroups]);
894 for (gp = cred->cr_groups; gp < egp; gp++) {
902 * Test whether the specified credentials have the privilege
905 * A kernel thread without a process context is assumed to have
906 * the privilege in question. In situations where the caller always
907 * expect a cred to exist, the cred should be passed separately and
908 * priv_check_cred() should be used instead of priv_check().
910 * Returns 0 or error.
913 priv_check(struct thread *td, int priv)
915 if (td->td_lwp != NULL)
916 return priv_check_cred(td->td_ucred, priv, 0);
921 * Check a credential for privilege.
923 * A non-null credential is expected unless NULL_CRED_OKAY is set.
926 priv_check_cred(struct ucred *cred, int priv, int flags)
930 KASSERT(PRIV_VALID(priv), ("priv_check_cred: invalid privilege"));
932 KASSERT(cred != NULL || (flags & NULL_CRED_OKAY),
933 ("priv_check_cred: NULL cred!"));
936 if (flags & NULL_CRED_OKAY)
941 if (cred->cr_uid != 0)
944 error = prison_priv_check(cred, priv);
948 /* NOTE: accounting for suser access (p_acflag/ASU) removed */
953 * Return zero if p1 can fondle p2, return errno (EPERM/ESRCH) otherwise.
956 p_trespass(struct ucred *cr1, struct ucred *cr2)
960 if (!PRISON_CHECK(cr1, cr2))
962 if (cr1->cr_ruid == cr2->cr_ruid)
964 if (cr1->cr_uid == cr2->cr_ruid)
966 if (cr1->cr_ruid == cr2->cr_uid)
968 if (cr1->cr_uid == cr2->cr_uid)
970 if (priv_check_cred(cr1, PRIV_PROC_TRESPASS, 0) == 0)
976 _crinit(struct ucred *cr)
982 crinit(struct ucred *cr)
984 bzero(cr, sizeof(*cr));
989 * Allocate a zeroed cred structure.
996 cr = kmalloc(sizeof(*cr), M_CRED, M_WAITOK|M_ZERO);
1002 * Claim another reference to a ucred structure. Can be used with special
1005 * It must be possible to call this routine with spinlocks held, meaning
1006 * that this routine itself cannot obtain a spinlock.
1009 crhold(struct ucred *cr)
1011 if (cr != NOCRED && cr != FSCRED)
1012 atomic_add_long(&cr->cr_ref, 1);
1017 * Drop a reference from the cred structure, free it if the reference count
1020 * NOTE: because we used atomic_add_int() above, without a spinlock, we
1021 * must also use atomic_subtract_int() below. A spinlock is required
1022 * in crfree() to handle multiple callers racing the refcount to 0.
1025 crfree(struct ucred *cr)
1027 if (cr->cr_ref <= 0)
1028 panic("Freeing already free credential! %p", cr);
1029 if (atomic_fetchadd_long(&cr->cr_ref, -1) == 1) {
1031 * Some callers of crget(), such as nfs_statfs(),
1032 * allocate a temporary credential, but don't
1033 * allocate a uidinfo structure.
1035 if (cr->cr_uidinfo != NULL) {
1036 uidrop(cr->cr_uidinfo);
1037 cr->cr_uidinfo = NULL;
1039 if (cr->cr_ruidinfo != NULL) {
1040 uidrop(cr->cr_ruidinfo);
1041 cr->cr_ruidinfo = NULL;
1045 * Destroy empty prisons
1048 prison_free(cr->cr_prison);
1049 cr->cr_prison = NULL; /* safety */
1051 kfree((caddr_t)cr, M_CRED);
1056 * Atomize a cred structure so it can be modified without polluting
1057 * other references to it.
1059 * MPSAFE (however, *pcr must be stable)
1062 cratom(struct ucred **pcr)
1064 struct ucred *oldcr;
1065 struct ucred *newcr;
1068 if (oldcr->cr_ref == 1)
1070 newcr = crget(); /* this might block */
1071 oldcr = *pcr; /* re-cache after potentially blocking */
1073 uihold(newcr->cr_uidinfo);
1074 uihold(newcr->cr_ruidinfo);
1076 prison_hold(newcr->cr_prison);
1085 * Called with a modifying token held, but must still obtain p_spin to
1086 * actually replace p_ucred to handle races against syscall entry from
1087 * other threads which cache p_ucred->td_ucred.
1089 * (the threads will only get the spin-lock, and they only need to in
1090 * the case where td_ucred != p_ucred so this is optimal).
1093 cratom_proc(struct proc *p)
1095 struct ucred *oldcr;
1096 struct ucred *newcr;
1099 if (oldcr->cr_ref == 1)
1102 newcr = crget(); /* this might block */
1103 oldcr = p->p_ucred; /* so re-cache oldcr (do not re-test) */
1105 uihold(newcr->cr_uidinfo);
1106 uihold(newcr->cr_ruidinfo);
1108 prison_hold(newcr->cr_prison);
1111 spin_lock(&p->p_spin);
1113 spin_unlock(&p->p_spin);
1120 * Dup cred struct to a new held one.
1123 crdup(struct ucred *cr)
1125 struct ucred *newcr;
1129 uihold(newcr->cr_uidinfo);
1130 uihold(newcr->cr_ruidinfo);
1132 prison_hold(newcr->cr_prison);
1138 * Fill in a struct xucred based on a struct ucred.
1141 cru2x(struct ucred *cr, struct xucred *xcr)
1144 bzero(xcr, sizeof(*xcr));
1145 xcr->cr_version = XUCRED_VERSION;
1146 xcr->cr_uid = cr->cr_uid;
1147 xcr->cr_ngroups = cr->cr_ngroups;
1148 bcopy(cr->cr_groups, xcr->cr_groups, sizeof(cr->cr_groups));
1152 * Get login name, if available.
1155 sys_getlogin(struct getlogin_args *uap)
1157 struct proc *p = curproc;
1158 char buf[MAXLOGNAME];
1161 if (uap->namelen > MAXLOGNAME) /* namelen is unsigned */
1162 uap->namelen = MAXLOGNAME;
1163 bzero(buf, sizeof(buf));
1164 lwkt_gettoken_shared(&p->p_token);
1165 bcopy(p->p_pgrp->pg_session->s_login, buf, uap->namelen);
1166 lwkt_reltoken(&p->p_token);
1168 error = copyout(buf, uap->namebuf, uap->namelen);
1176 sys_setlogin(struct setlogin_args *uap)
1178 struct thread *td = curthread;
1181 char buf[MAXLOGNAME];
1184 cred = td->td_ucred;
1187 if ((error = priv_check_cred(cred, PRIV_PROC_SETLOGIN, 0)))
1189 bzero(buf, sizeof(buf));
1190 error = copyinstr(uap->namebuf, buf, sizeof(buf), NULL);
1191 if (error == ENAMETOOLONG)
1194 lwkt_gettoken_shared(&p->p_token);
1195 memcpy(p->p_pgrp->pg_session->s_login, buf, sizeof(buf));
1196 lwkt_reltoken(&p->p_token);
1204 struct proc *p = curproc;
1206 KKASSERT(p != NULL);
1207 lwkt_gettoken(&p->p_token);
1208 p->p_flags |= P_SUGID;
1209 if (!(p->p_pfsflags & PF_ISUGID))
1211 lwkt_reltoken(&p->p_token);
1215 * Helper function to change the effective uid of a process
1218 change_euid(uid_t euid)
1220 struct proc *p = curproc;
1223 KKASSERT(p != NULL);
1224 lf_count_adjust(p, 0);
1225 cr = cratom_proc(p);
1227 uireplace(&cr->cr_uidinfo, uifind(euid));
1228 lf_count_adjust(p, 1);
1233 * Helper function to change the real uid of a process
1235 * The per-uid process count for this process is transfered from
1236 * the old uid to the new uid.
1239 change_ruid(uid_t ruid)
1241 struct proc *p = curproc;
1244 KKASSERT(p != NULL);
1246 cr = cratom_proc(p);
1247 chgproccnt(cr->cr_ruidinfo, -1, 0);
1249 uireplace(&cr->cr_ruidinfo, uifind(ruid));
1250 chgproccnt(cr->cr_ruidinfo, 1, 0);