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 lwkt_gettoken_shared(&p->p_token);
77 uap->sysmsg_result = p->p_pptr->p_pid;
78 lwkt_reltoken(&p->p_token);
84 sys_lwp_gettid(struct lwp_gettid_args *uap)
86 struct lwp *lp = curthread->td_lwp;
87 uap->sysmsg_result = lp->lwp_tid;
92 * Get process group ID; note that POSIX getpgrp takes no parameter
95 sys_getpgrp(struct getpgrp_args *uap)
97 struct proc *p = curproc;
99 lwkt_gettoken_shared(&p->p_token);
100 uap->sysmsg_result = p->p_pgrp->pg_id;
101 lwkt_reltoken(&p->p_token);
107 * Get an arbitrary pid's process group id
110 sys_getpgid(struct getpgid_args *uap)
112 struct proc *p = curproc;
122 pt = pfind(uap->pid);
127 lwkt_gettoken_shared(&pt->p_token);
128 uap->sysmsg_result = pt->p_pgrp->pg_id;
129 lwkt_reltoken(&pt->p_token);
137 * Get an arbitrary pid's session id.
140 sys_getsid(struct getsid_args *uap)
142 struct proc *p = curproc;
152 pt = pfind(uap->pid);
157 uap->sysmsg_result = pt->p_session->s_sid;
168 sys_getuid(struct getuid_args *uap)
170 struct ucred *cred = curthread->td_ucred;
172 uap->sysmsg_fds[0] = cred->cr_ruid;
180 sys_geteuid(struct geteuid_args *uap)
182 struct ucred *cred = curthread->td_ucred;
184 uap->sysmsg_result = cred->cr_uid;
192 sys_getgid(struct getgid_args *uap)
194 struct ucred *cred = curthread->td_ucred;
196 uap->sysmsg_fds[0] = cred->cr_rgid;
201 * Get effective group ID. The "egid" is groups[0], and could be obtained
202 * via getgroups. This syscall exists because it is somewhat painful to do
203 * correctly in a library function.
206 sys_getegid(struct getegid_args *uap)
208 struct ucred *cred = curthread->td_ucred;
210 uap->sysmsg_result = cred->cr_groups[0];
215 sys_getgroups(struct getgroups_args *uap)
221 cr = curthread->td_ucred;
222 if ((ngrp = uap->gidsetsize) == 0) {
223 uap->sysmsg_result = cr->cr_ngroups;
226 if (ngrp < cr->cr_ngroups)
228 ngrp = cr->cr_ngroups;
229 error = copyout((caddr_t)cr->cr_groups,
230 (caddr_t)uap->gidset, ngrp * sizeof(gid_t));
232 uap->sysmsg_result = ngrp;
237 sys_lwp_setname(struct lwp_setname_args *uap)
239 struct proc *p = curproc;
240 char comm0[MAXCOMLEN + 1];
241 const char *comm = NULL;
245 if (uap->name != NULL) {
246 error = copyinstr(uap->name, comm0, sizeof(comm0), NULL);
248 if (error != ENAMETOOLONG)
251 comm0[MAXCOMLEN] = '\0';
255 /* Restore to the default name, i.e. process name. */
259 lwkt_gettoken(&p->p_token);
261 lp = lwp_rb_tree_RB_LOOKUP(&p->p_lwp_tree, uap->tid);
263 strlcpy(lp->lwp_thread->td_comm, comm,
264 sizeof(lp->lwp_thread->td_comm));
270 lwkt_reltoken(&p->p_token);
275 sys_setsid(struct setsid_args *uap)
277 struct proc *p = curproc;
278 struct pgrp *pg = NULL;
281 lwkt_gettoken(&p->p_token);
282 if (p->p_pgid == p->p_pid || (pg = pgfind(p->p_pid)) != NULL) {
287 enterpgrp(p, p->p_pid, 1);
288 uap->sysmsg_result = p->p_pid;
291 lwkt_reltoken(&p->p_token);
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)
309 sys_setpgid(struct setpgid_args *uap)
311 struct proc *curp = curproc;
312 struct proc *targp; /* target process */
313 struct pgrp *pgrp = NULL; /* target pgrp */
319 if (uap->pid != 0 && uap->pid != curp->p_pid) {
320 if ((targp = pfind(uap->pid)) == NULL || !inferior(targp)) {
327 lwkt_gettoken(&targp->p_token);
328 /* targp now referenced and its token is held */
330 if (targp->p_pgrp == NULL ||
331 targp->p_session != curp->p_session) {
335 if (targp->p_flags & P_EXEC) {
342 lwkt_gettoken(&targp->p_token);
344 if (SESS_LEADER(targp)) {
348 if (uap->pgid == 0) {
349 uap->pgid = targp->p_pid;
350 } else if (uap->pgid != targp->p_pid) {
351 if ((pgrp = pgfind(uap->pgid)) == NULL ||
352 pgrp->pg_session != curp->p_session) {
357 error = enterpgrp(targp, uap->pgid, 0);
362 lwkt_reltoken(&targp->p_token);
369 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
370 * compatible. It says that setting the uid/gid to euid/egid is a special
371 * case of "appropriate privilege". Once the rules are expanded out, this
372 * basically means that setuid(nnn) sets all three id's, in all permitted
373 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
374 * does not set the saved id - this is dangerous for traditional BSD
375 * programs. For this reason, we *really* do not want to set
376 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
378 #define POSIX_APPENDIX_B_4_2_2
381 sys_setuid(struct setuid_args *uap)
383 struct proc *p = curproc;
388 lwkt_gettoken(&p->p_token);
392 * See if we have "permission" by POSIX 1003.1 rules.
394 * Note that setuid(geteuid()) is a special case of
395 * "appropriate privileges" in appendix B.4.2.2. We need
396 * to use this clause to be compatible with traditional BSD
397 * semantics. Basically, it means that "setuid(xx)" sets all
398 * three id's (assuming you have privs).
400 * Notes on the logic. We do things in three steps.
401 * 1: We determine if the euid is going to change, and do EPERM
402 * right away. We unconditionally change the euid later if this
403 * test is satisfied, simplifying that part of the logic.
404 * 2: We determine if the real and/or saved uid's are going to
405 * change. Determined by compile options.
406 * 3: Change euid last. (after tests in #2 for "appropriate privs")
409 if (uid != cr->cr_ruid && /* allow setuid(getuid()) */
410 #ifdef _POSIX_SAVED_IDS
411 uid != crc->cr_svuid && /* allow setuid(saved gid) */
413 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
414 uid != cr->cr_uid && /* allow setuid(geteuid()) */
416 (error = priv_check_cred(cr, PRIV_CRED_SETUID, 0)))
419 #ifdef _POSIX_SAVED_IDS
421 * Do we have "appropriate privileges" (are we root or uid == euid)
422 * If so, we are changing the real uid and/or saved uid.
425 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
428 priv_check_cred(cr, PRIV_CRED_SETUID, 0) == 0) /* we are using privs */
432 * Set the real uid and transfer proc count to new user.
434 if (uid != cr->cr_ruid) {
435 cr = change_ruid(uid);
441 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
442 * the security of seteuid() depends on it. B.4.2.2 says it
443 * is important that we should do this.
445 if (cr->cr_svuid != uid) {
453 * In all permitted cases, we are changing the euid.
454 * Copy credentials so other references do not see our changes.
456 if (cr->cr_uid != uid) {
462 lwkt_reltoken(&p->p_token);
467 sys_seteuid(struct seteuid_args *uap)
469 struct proc *p = curproc;
474 lwkt_gettoken(&p->p_token);
477 if (euid != cr->cr_ruid && /* allow seteuid(getuid()) */
478 euid != cr->cr_svuid && /* allow seteuid(saved uid) */
479 (error = priv_check_cred(cr, PRIV_CRED_SETEUID, 0))) {
480 lwkt_reltoken(&p->p_token);
485 * Everything's okay, do it. Copy credentials so other references do
486 * not see our changes.
488 if (cr->cr_uid != euid) {
492 lwkt_reltoken(&p->p_token);
497 sys_setgid(struct setgid_args *uap)
499 struct proc *p = curproc;
504 lwkt_gettoken(&p->p_token);
508 * See if we have "permission" by POSIX 1003.1 rules.
510 * Note that setgid(getegid()) is a special case of
511 * "appropriate privileges" in appendix B.4.2.2. We need
512 * to use this clause to be compatible with traditional BSD
513 * semantics. Basically, it means that "setgid(xx)" sets all
514 * three id's (assuming you have privs).
516 * For notes on the logic here, see setuid() above.
519 if (gid != cr->cr_rgid && /* allow setgid(getgid()) */
520 #ifdef _POSIX_SAVED_IDS
521 gid != cr->cr_svgid && /* allow setgid(saved gid) */
523 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
524 gid != cr->cr_groups[0] && /* allow setgid(getegid()) */
526 (error = priv_check_cred(cr, PRIV_CRED_SETGID, 0))) {
530 #ifdef _POSIX_SAVED_IDS
532 * Do we have "appropriate privileges" (are we root or gid == egid)
533 * If so, we are changing the real uid and saved gid.
536 #ifdef POSIX_APPENDIX_B_4_2_2 /* use the clause from B.4.2.2 */
537 gid == cr->cr_groups[0] ||
539 priv_check_cred(cr, PRIV_CRED_SETGID, 0) == 0) /* we are using privs */
545 if (cr->cr_rgid != gid) {
553 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
554 * the security of setegid() depends on it. B.4.2.2 says it
555 * is important that we should do this.
557 if (cr->cr_svgid != gid) {
564 * In all cases permitted cases, we are changing the egid.
565 * Copy credentials so other references do not see our changes.
567 if (cr->cr_groups[0] != gid) {
569 cr->cr_groups[0] = gid;
574 lwkt_reltoken(&p->p_token);
579 sys_setegid(struct setegid_args *uap)
581 struct proc *p = curproc;
586 lwkt_gettoken(&p->p_token);
589 if (egid != cr->cr_rgid && /* allow setegid(getgid()) */
590 egid != cr->cr_svgid && /* allow setegid(saved gid) */
591 (error = priv_check_cred(cr, PRIV_CRED_SETEGID, 0))) {
594 if (cr->cr_groups[0] != egid) {
596 cr->cr_groups[0] = egid;
601 lwkt_reltoken(&p->p_token);
606 sys_setgroups(struct setgroups_args *uap)
608 struct proc *p = curproc;
613 lwkt_gettoken(&p->p_token);
616 if ((error = priv_check_cred(cr, PRIV_CRED_SETGROUPS, 0)))
618 ngrp = uap->gidsetsize;
619 if (ngrp > NGROUPS) {
624 * XXX A little bit lazy here. We could test if anything has
625 * changed before cratom() and setting P_SUGID.
630 * setgroups(0, NULL) is a legitimate way of clearing the
631 * groups vector on non-BSD systems (which generally do not
632 * have the egid in the groups[0]). We risk security holes
633 * when running non-BSD software if we do not do the same.
637 error = copyin(uap->gidset, cr->cr_groups,
638 ngrp * sizeof(gid_t));
641 cr->cr_ngroups = ngrp;
646 lwkt_reltoken(&p->p_token);
651 sys_setreuid(struct setreuid_args *uap)
653 struct proc *p = curproc;
658 lwkt_gettoken(&p->p_token);
663 if (((ruid != (uid_t)-1 && ruid != cr->cr_ruid &&
664 ruid != cr->cr_svuid) ||
665 (euid != (uid_t)-1 && euid != cr->cr_uid &&
666 euid != cr->cr_ruid && euid != cr->cr_svuid)) &&
667 (error = priv_check_cred(cr, PRIV_CRED_SETREUID, 0)) != 0) {
671 if (euid != (uid_t)-1 && cr->cr_uid != euid) {
672 cr = change_euid(euid);
675 if (ruid != (uid_t)-1 && cr->cr_ruid != ruid) {
676 cr = change_ruid(ruid);
679 if ((ruid != (uid_t)-1 || cr->cr_uid != cr->cr_ruid) &&
680 cr->cr_svuid != cr->cr_uid) {
682 cr->cr_svuid = cr->cr_uid;
687 lwkt_reltoken(&p->p_token);
692 sys_setregid(struct setregid_args *uap)
694 struct proc *p = curproc;
699 lwkt_gettoken(&p->p_token);
704 if (((rgid != (gid_t)-1 && rgid != cr->cr_rgid &&
705 rgid != cr->cr_svgid) ||
706 (egid != (gid_t)-1 && egid != cr->cr_groups[0] &&
707 egid != cr->cr_rgid && egid != cr->cr_svgid)) &&
708 (error = priv_check_cred(cr, PRIV_CRED_SETREGID, 0)) != 0) {
712 if (egid != (gid_t)-1 && cr->cr_groups[0] != egid) {
714 cr->cr_groups[0] = egid;
717 if (rgid != (gid_t)-1 && cr->cr_rgid != rgid) {
722 if ((rgid != (gid_t)-1 || cr->cr_groups[0] != cr->cr_rgid) &&
723 cr->cr_svgid != cr->cr_groups[0]) {
725 cr->cr_svgid = cr->cr_groups[0];
730 lwkt_reltoken(&p->p_token);
735 * setresuid(ruid, euid, suid) is like setreuid except control over the
736 * saved uid is explicit.
739 sys_setresuid(struct setresuid_args *uap)
741 struct proc *p = curproc;
743 uid_t ruid, euid, suid;
746 lwkt_gettoken(&p->p_token);
752 if (((ruid != (uid_t)-1 && ruid != cr->cr_ruid &&
753 ruid != cr->cr_svuid && ruid != cr->cr_uid) ||
754 (euid != (uid_t)-1 && euid != cr->cr_ruid &&
755 euid != cr->cr_svuid && euid != cr->cr_uid) ||
756 (suid != (uid_t)-1 && suid != cr->cr_ruid &&
757 suid != cr->cr_svuid && suid != cr->cr_uid)) &&
758 (error = priv_check_cred(cr, PRIV_CRED_SETRESUID, 0)) != 0) {
761 if (euid != (uid_t)-1 && cr->cr_uid != euid) {
762 cr = change_euid(euid);
765 if (ruid != (uid_t)-1 && cr->cr_ruid != ruid) {
766 cr = change_ruid(ruid);
769 if (suid != (uid_t)-1 && cr->cr_svuid != suid) {
776 lwkt_reltoken(&p->p_token);
781 * setresgid(rgid, egid, sgid) is like setregid except control over the
782 * saved gid is explicit.
785 sys_setresgid(struct setresgid_args *uap)
787 struct proc *p = curproc;
789 gid_t rgid, egid, sgid;
792 lwkt_gettoken(&p->p_token);
797 if (((rgid != (gid_t)-1 && rgid != cr->cr_rgid &&
798 rgid != cr->cr_svgid && rgid != cr->cr_groups[0]) ||
799 (egid != (gid_t)-1 && egid != cr->cr_rgid &&
800 egid != cr->cr_svgid && egid != cr->cr_groups[0]) ||
801 (sgid != (gid_t)-1 && sgid != cr->cr_rgid &&
802 sgid != cr->cr_svgid && sgid != cr->cr_groups[0])) &&
803 (error = priv_check_cred(cr, PRIV_CRED_SETRESGID, 0)) != 0) {
807 if (egid != (gid_t)-1 && cr->cr_groups[0] != egid) {
809 cr->cr_groups[0] = egid;
812 if (rgid != (gid_t)-1 && cr->cr_rgid != rgid) {
817 if (sgid != (gid_t)-1 && cr->cr_svgid != sgid) {
824 lwkt_reltoken(&p->p_token);
829 sys_getresuid(struct getresuid_args *uap)
832 int error1 = 0, error2 = 0, error3 = 0;
835 * copyout's can fault synchronously so we cannot use a shared
838 cr = curthread->td_ucred;
840 error1 = copyout((caddr_t)&cr->cr_ruid,
841 (caddr_t)uap->ruid, sizeof(cr->cr_ruid));
843 error2 = copyout((caddr_t)&cr->cr_uid,
844 (caddr_t)uap->euid, sizeof(cr->cr_uid));
846 error3 = copyout((caddr_t)&cr->cr_svuid,
847 (caddr_t)uap->suid, sizeof(cr->cr_svuid));
848 return error1 ? error1 : (error2 ? error2 : error3);
852 sys_getresgid(struct getresgid_args *uap)
855 int error1 = 0, error2 = 0, error3 = 0;
857 cr = curthread->td_ucred;
859 error1 = copyout(&cr->cr_rgid, uap->rgid,
860 sizeof(cr->cr_rgid));
862 error2 = copyout(&cr->cr_groups[0], uap->egid,
863 sizeof(cr->cr_groups[0]));
865 error3 = copyout(&cr->cr_svgid, uap->sgid,
866 sizeof(cr->cr_svgid));
867 return error1 ? error1 : (error2 ? error2 : error3);
872 * NOTE: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
873 * we use P_SUGID because we consider changing the owners as
874 * "tainting" as well.
875 * This is significant for procs that start as root and "become"
876 * a user without an exec - programs cannot know *everything*
877 * that libc *might* have put in their data segment.
880 sys_issetugid(struct issetugid_args *uap)
882 uap->sysmsg_result = (curproc->p_flags & P_SUGID) ? 1 : 0;
887 * Check if gid is a member of the group set.
890 groupmember(gid_t gid, struct ucred *cred)
895 egp = &(cred->cr_groups[cred->cr_ngroups]);
896 for (gp = cred->cr_groups; gp < egp; gp++) {
904 * Test whether the specified credentials have the privilege
907 * A kernel thread without a process context is assumed to have
908 * the privilege in question. In situations where the caller always
909 * expect a cred to exist, the cred should be passed separately and
910 * priv_check_cred() should be used instead of priv_check().
912 * Returns 0 or error.
915 priv_check(struct thread *td, int priv)
917 if (td->td_lwp != NULL)
918 return priv_check_cred(td->td_ucred, priv, 0);
923 * Check a credential for privilege.
925 * A non-null credential is expected unless NULL_CRED_OKAY is set.
928 priv_check_cred(struct ucred *cred, int priv, int flags)
932 KASSERT(PRIV_VALID(priv), ("priv_check_cred: invalid privilege"));
934 KASSERT(cred != NULL || (flags & NULL_CRED_OKAY),
935 ("priv_check_cred: NULL cred!"));
938 if (flags & NULL_CRED_OKAY)
943 if (cred->cr_uid != 0)
946 error = prison_priv_check(cred, priv);
950 /* NOTE: accounting for suser access (p_acflag/ASU) removed */
955 * Return zero if p1 can fondle p2, return errno (EPERM/ESRCH) otherwise.
958 p_trespass(struct ucred *cr1, struct ucred *cr2)
962 if (!PRISON_CHECK(cr1, cr2))
964 if (cr1->cr_ruid == cr2->cr_ruid)
966 if (cr1->cr_uid == cr2->cr_ruid)
968 if (cr1->cr_ruid == cr2->cr_uid)
970 if (cr1->cr_uid == cr2->cr_uid)
972 if (priv_check_cred(cr1, PRIV_PROC_TRESPASS, 0) == 0)
978 _crinit(struct ucred *cr)
984 crinit(struct ucred *cr)
986 bzero(cr, sizeof(*cr));
991 * Allocate a zeroed cred structure.
998 cr = kmalloc(sizeof(*cr), M_CRED, M_WAITOK|M_ZERO);
1004 * Claim another reference to a ucred structure. Can be used with special
1007 * It must be possible to call this routine with spinlocks held, meaning
1008 * that this routine itself cannot obtain a spinlock.
1011 crhold(struct ucred *cr)
1013 if (cr != NOCRED && cr != FSCRED)
1014 atomic_add_int(&cr->cr_ref, 1);
1019 * Drop a reference from the cred structure, free it if the reference count
1022 * NOTE: because we used atomic_add_int() above, without a spinlock, we
1023 * must also use atomic_subtract_int() below. A spinlock is required
1024 * in crfree() to handle multiple callers racing the refcount to 0.
1027 crfree(struct ucred *cr)
1029 if (cr->cr_ref <= 0)
1030 panic("Freeing already free credential! %p", cr);
1031 if (atomic_fetchadd_int(&cr->cr_ref, -1) == 1) {
1033 * Some callers of crget(), such as nfs_statfs(),
1034 * allocate a temporary credential, but don't
1035 * allocate a uidinfo structure.
1037 if (cr->cr_uidinfo != NULL) {
1038 uidrop(cr->cr_uidinfo);
1039 cr->cr_uidinfo = NULL;
1041 if (cr->cr_ruidinfo != NULL) {
1042 uidrop(cr->cr_ruidinfo);
1043 cr->cr_ruidinfo = NULL;
1047 * Destroy empty prisons
1050 prison_free(cr->cr_prison);
1051 cr->cr_prison = NULL; /* safety */
1053 kfree((caddr_t)cr, M_CRED);
1058 * Atomize a cred structure so it can be modified without polluting
1059 * other references to it.
1061 * MPSAFE (however, *pcr must be stable)
1064 cratom(struct ucred **pcr)
1066 struct ucred *oldcr;
1067 struct ucred *newcr;
1070 if (oldcr->cr_ref == 1)
1072 newcr = crget(); /* this might block */
1073 oldcr = *pcr; /* re-cache after potentially blocking */
1075 if (newcr->cr_uidinfo)
1076 uihold(newcr->cr_uidinfo);
1077 if (newcr->cr_ruidinfo)
1078 uihold(newcr->cr_ruidinfo);
1080 prison_hold(newcr->cr_prison);
1089 * Called with a modifying token held, but must still obtain p_spin to
1090 * actually replace p_ucred to handle races against syscall entry from
1091 * other threads which cache p_ucred->td_ucred.
1093 * (the threads will only get the spin-lock, and they only need to in
1094 * the case where td_ucred != p_ucred so this is optimal).
1097 cratom_proc(struct proc *p)
1099 struct ucred *oldcr;
1100 struct ucred *newcr;
1103 if (oldcr->cr_ref == 1)
1106 newcr = crget(); /* this might block */
1107 oldcr = p->p_ucred; /* so re-cache oldcr (do not re-test) */
1109 if (newcr->cr_uidinfo)
1110 uihold(newcr->cr_uidinfo);
1111 if (newcr->cr_ruidinfo)
1112 uihold(newcr->cr_ruidinfo);
1114 prison_hold(newcr->cr_prison);
1117 spin_lock(&p->p_spin);
1119 spin_unlock(&p->p_spin);
1126 * Dup cred struct to a new held one.
1129 crdup(struct ucred *cr)
1131 struct ucred *newcr;
1135 if (newcr->cr_uidinfo)
1136 uihold(newcr->cr_uidinfo);
1137 if (newcr->cr_ruidinfo)
1138 uihold(newcr->cr_ruidinfo);
1140 prison_hold(newcr->cr_prison);
1146 * Fill in a struct xucred based on a struct ucred.
1149 cru2x(struct ucred *cr, struct xucred *xcr)
1152 bzero(xcr, sizeof(*xcr));
1153 xcr->cr_version = XUCRED_VERSION;
1154 xcr->cr_uid = cr->cr_uid;
1155 xcr->cr_ngroups = cr->cr_ngroups;
1156 bcopy(cr->cr_groups, xcr->cr_groups, sizeof(cr->cr_groups));
1160 * Get login name, if available.
1163 sys_getlogin(struct getlogin_args *uap)
1165 struct proc *p = curproc;
1166 char buf[MAXLOGNAME];
1169 if (uap->namelen > MAXLOGNAME) /* namelen is unsigned */
1170 uap->namelen = MAXLOGNAME;
1171 bzero(buf, sizeof(buf));
1172 lwkt_gettoken_shared(&p->p_token);
1173 bcopy(p->p_pgrp->pg_session->s_login, buf, uap->namelen);
1174 lwkt_reltoken(&p->p_token);
1176 error = copyout(buf, uap->namebuf, uap->namelen);
1184 sys_setlogin(struct setlogin_args *uap)
1186 struct thread *td = curthread;
1189 char buf[MAXLOGNAME];
1192 cred = td->td_ucred;
1195 if ((error = priv_check_cred(cred, PRIV_PROC_SETLOGIN, 0)))
1197 bzero(buf, sizeof(buf));
1198 error = copyinstr(uap->namebuf, buf, sizeof(buf), NULL);
1199 if (error == ENAMETOOLONG)
1202 lwkt_gettoken_shared(&p->p_token);
1203 memcpy(p->p_pgrp->pg_session->s_login, buf, sizeof(buf));
1204 lwkt_reltoken(&p->p_token);
1212 struct proc *p = curproc;
1214 KKASSERT(p != NULL);
1215 lwkt_gettoken(&p->p_token);
1216 p->p_flags |= P_SUGID;
1217 if (!(p->p_pfsflags & PF_ISUGID))
1219 lwkt_reltoken(&p->p_token);
1223 * Helper function to change the effective uid of a process
1226 change_euid(uid_t euid)
1228 struct proc *p = curproc;
1231 KKASSERT(p != NULL);
1232 lf_count_adjust(p, 0);
1233 cr = cratom_proc(p);
1235 uireplace(&cr->cr_uidinfo, uifind(euid));
1236 lf_count_adjust(p, 1);
1241 * Helper function to change the real uid of a process
1243 * The per-uid process count for this process is transfered from
1244 * the old uid to the new uid.
1247 change_ruid(uid_t ruid)
1249 struct proc *p = curproc;
1252 KKASSERT(p != NULL);
1254 cr = cratom_proc(p);
1255 chgproccnt(cr->cr_ruidinfo, -1, 0);
1257 uireplace(&cr->cr_ruidinfo, uifind(ruid));
1258 chgproccnt(cr->cr_ruidinfo, 1, 0);