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 "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_shared(&p->p_token);
72 uap->sysmsg_fds[1] = p->p_pptr->p_pid;
73 lwkt_reltoken(&p->p_token);
79 sys_getppid(struct getppid_args *uap)
81 struct proc *p = curproc;
83 lwkt_gettoken_shared(&p->p_token);
84 uap->sysmsg_result = p->p_pptr->p_pid;
85 lwkt_reltoken(&p->p_token);
91 sys_lwp_gettid(struct lwp_gettid_args *uap)
93 struct lwp *lp = curthread->td_lwp;
94 uap->sysmsg_result = lp->lwp_tid;
99 * Get process group ID; note that POSIX getpgrp takes no parameter
102 sys_getpgrp(struct getpgrp_args *uap)
104 struct proc *p = curproc;
106 lwkt_gettoken_shared(&p->p_token);
107 uap->sysmsg_result = p->p_pgrp->pg_id;
108 lwkt_reltoken(&p->p_token);
114 * Get an arbitrary pid's process group id
117 sys_getpgid(struct getpgid_args *uap)
119 struct proc *p = curproc;
129 pt = pfind(uap->pid);
134 lwkt_gettoken_shared(&pt->p_token);
135 uap->sysmsg_result = pt->p_pgrp->pg_id;
136 lwkt_reltoken(&pt->p_token);
144 * Get an arbitrary pid's session id.
147 sys_getsid(struct getsid_args *uap)
149 struct proc *p = curproc;
159 pt = pfind(uap->pid);
164 uap->sysmsg_result = pt->p_session->s_sid;
175 sys_getuid(struct getuid_args *uap)
177 struct ucred *cred = curthread->td_ucred;
179 uap->sysmsg_fds[0] = cred->cr_ruid;
180 #if defined(COMPAT_43)
181 uap->sysmsg_fds[1] = cred->cr_uid;
190 sys_geteuid(struct geteuid_args *uap)
192 struct ucred *cred = curthread->td_ucred;
194 uap->sysmsg_result = cred->cr_uid;
202 sys_getgid(struct getgid_args *uap)
204 struct ucred *cred = curthread->td_ucred;
206 uap->sysmsg_fds[0] = cred->cr_rgid;
207 #if defined(COMPAT_43)
208 uap->sysmsg_fds[1] = cred->cr_groups[0];
214 * Get effective group ID. The "egid" is groups[0], and could be obtained
215 * via getgroups. This syscall exists because it is somewhat painful to do
216 * correctly in a library function.
219 sys_getegid(struct getegid_args *uap)
221 struct ucred *cred = curthread->td_ucred;
223 uap->sysmsg_result = cred->cr_groups[0];
228 sys_getgroups(struct getgroups_args *uap)
234 cr = curthread->td_ucred;
235 if ((ngrp = uap->gidsetsize) == 0) {
236 uap->sysmsg_result = cr->cr_ngroups;
239 if (ngrp < cr->cr_ngroups)
241 ngrp = cr->cr_ngroups;
242 error = copyout((caddr_t)cr->cr_groups,
243 (caddr_t)uap->gidset, ngrp * sizeof(gid_t));
245 uap->sysmsg_result = ngrp;
250 sys_setsid(struct setsid_args *uap)
252 struct proc *p = curproc;
253 struct pgrp *pg = NULL;
256 lwkt_gettoken(&p->p_token);
257 if (p->p_pgid == p->p_pid || (pg = pgfind(p->p_pid)) != NULL) {
262 enterpgrp(p, p->p_pid, 1);
263 uap->sysmsg_result = p->p_pid;
266 lwkt_reltoken(&p->p_token);
271 * set process group (setpgid/old setpgrp)
273 * caller does setpgid(targpid, targpgid)
275 * pid must be caller or child of caller (ESRCH)
277 * pid must be in same session (EPERM)
278 * pid can't have done an exec (EACCES)
280 * there must exist some pid in same session having pgid (EPERM)
281 * pid must not be session leader (EPERM)
284 sys_setpgid(struct setpgid_args *uap)
286 struct proc *curp = curproc;
287 struct proc *targp; /* target process */
288 struct pgrp *pgrp = NULL; /* target pgrp */
294 if (uap->pid != 0 && uap->pid != curp->p_pid) {
295 if ((targp = pfind(uap->pid)) == NULL || !inferior(targp)) {
302 lwkt_gettoken(&targp->p_token);
303 /* targp now referenced and its token is held */
305 if (targp->p_pgrp == NULL ||
306 targp->p_session != curp->p_session) {
310 if (targp->p_flags & P_EXEC) {
317 lwkt_gettoken(&targp->p_token);
319 if (SESS_LEADER(targp)) {
323 if (uap->pgid == 0) {
324 uap->pgid = targp->p_pid;
325 } else if (uap->pgid != targp->p_pid) {
326 if ((pgrp = pgfind(uap->pgid)) == NULL ||
327 pgrp->pg_session != curp->p_session) {
332 error = enterpgrp(targp, uap->pgid, 0);
337 lwkt_reltoken(&targp->p_token);
344 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
345 * compatible. It says that setting the uid/gid to euid/egid is a special
346 * case of "appropriate privilege". Once the rules are expanded out, this
347 * basically means that setuid(nnn) sets all three id's, in all permitted
348 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
349 * does not set the saved id - this is dangerous for traditional BSD
350 * programs. For this reason, we *really* do not want to set
351 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
353 #define POSIX_APPENDIX_B_4_2_2
356 sys_setuid(struct setuid_args *uap)
358 struct proc *p = curproc;
363 lwkt_gettoken(&p->p_token);
367 * See if we have "permission" by POSIX 1003.1 rules.
369 * Note that setuid(geteuid()) is a special case of
370 * "appropriate privileges" in appendix B.4.2.2. We need
371 * to use this clause to be compatible with traditional BSD
372 * semantics. Basically, it means that "setuid(xx)" sets all
373 * three id's (assuming you have privs).
375 * Notes on the logic. We do things in three steps.
376 * 1: We determine if the euid is going to change, and do EPERM
377 * right away. We unconditionally change the euid later if this
378 * test is satisfied, simplifying that part of the logic.
379 * 2: We determine if the real and/or saved uid's are going to
380 * change. Determined by compile options.
381 * 3: Change euid last. (after tests in #2 for "appropriate privs")
384 if (uid != cr->cr_ruid && /* allow setuid(getuid()) */
385 #ifdef _POSIX_SAVED_IDS
386 uid != crc->cr_svuid && /* allow setuid(saved gid) */
388 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
389 uid != cr->cr_uid && /* allow setuid(geteuid()) */
391 (error = priv_check_cred(cr, PRIV_CRED_SETUID, 0)))
394 #ifdef _POSIX_SAVED_IDS
396 * Do we have "appropriate privileges" (are we root or uid == euid)
397 * If so, we are changing the real uid and/or saved uid.
400 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
403 priv_check_cred(cr, PRIV_CRED_SETUID, 0) == 0) /* we are using privs */
407 * Set the real uid and transfer proc count to new user.
409 if (uid != cr->cr_ruid) {
410 cr = change_ruid(uid);
416 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
417 * the security of seteuid() depends on it. B.4.2.2 says it
418 * is important that we should do this.
420 if (cr->cr_svuid != uid) {
428 * In all permitted cases, we are changing the euid.
429 * Copy credentials so other references do not see our changes.
431 if (cr->cr_uid != uid) {
437 lwkt_reltoken(&p->p_token);
442 sys_seteuid(struct seteuid_args *uap)
444 struct proc *p = curproc;
449 lwkt_gettoken(&p->p_token);
452 if (euid != cr->cr_ruid && /* allow seteuid(getuid()) */
453 euid != cr->cr_svuid && /* allow seteuid(saved uid) */
454 (error = priv_check_cred(cr, PRIV_CRED_SETEUID, 0))) {
455 lwkt_reltoken(&p->p_token);
460 * Everything's okay, do it. Copy credentials so other references do
461 * not see our changes.
463 if (cr->cr_uid != euid) {
467 lwkt_reltoken(&p->p_token);
472 sys_setgid(struct setgid_args *uap)
474 struct proc *p = curproc;
479 lwkt_gettoken(&p->p_token);
483 * See if we have "permission" by POSIX 1003.1 rules.
485 * Note that setgid(getegid()) is a special case of
486 * "appropriate privileges" in appendix B.4.2.2. We need
487 * to use this clause to be compatible with traditional BSD
488 * semantics. Basically, it means that "setgid(xx)" sets all
489 * three id's (assuming you have privs).
491 * For notes on the logic here, see setuid() above.
494 if (gid != cr->cr_rgid && /* allow setgid(getgid()) */
495 #ifdef _POSIX_SAVED_IDS
496 gid != cr->cr_svgid && /* allow setgid(saved gid) */
498 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
499 gid != cr->cr_groups[0] && /* allow setgid(getegid()) */
501 (error = priv_check_cred(cr, PRIV_CRED_SETGID, 0))) {
505 #ifdef _POSIX_SAVED_IDS
507 * Do we have "appropriate privileges" (are we root or gid == egid)
508 * If so, we are changing the real uid and saved gid.
511 #ifdef POSIX_APPENDIX_B_4_2_2 /* use the clause from B.4.2.2 */
512 gid == cr->cr_groups[0] ||
514 priv_check_cred(cr, PRIV_CRED_SETGID, 0) == 0) /* we are using privs */
520 if (cr->cr_rgid != gid) {
528 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
529 * the security of setegid() depends on it. B.4.2.2 says it
530 * is important that we should do this.
532 if (cr->cr_svgid != gid) {
539 * In all cases permitted cases, we are changing the egid.
540 * Copy credentials so other references do not see our changes.
542 if (cr->cr_groups[0] != gid) {
544 cr->cr_groups[0] = gid;
549 lwkt_reltoken(&p->p_token);
554 sys_setegid(struct setegid_args *uap)
556 struct proc *p = curproc;
561 lwkt_gettoken(&p->p_token);
564 if (egid != cr->cr_rgid && /* allow setegid(getgid()) */
565 egid != cr->cr_svgid && /* allow setegid(saved gid) */
566 (error = priv_check_cred(cr, PRIV_CRED_SETEGID, 0))) {
569 if (cr->cr_groups[0] != egid) {
571 cr->cr_groups[0] = egid;
576 lwkt_reltoken(&p->p_token);
581 sys_setgroups(struct setgroups_args *uap)
583 struct proc *p = curproc;
588 lwkt_gettoken(&p->p_token);
591 if ((error = priv_check_cred(cr, PRIV_CRED_SETGROUPS, 0)))
593 ngrp = uap->gidsetsize;
594 if (ngrp > NGROUPS) {
599 * XXX A little bit lazy here. We could test if anything has
600 * changed before cratom() and setting P_SUGID.
605 * setgroups(0, NULL) is a legitimate way of clearing the
606 * groups vector on non-BSD systems (which generally do not
607 * have the egid in the groups[0]). We risk security holes
608 * when running non-BSD software if we do not do the same.
612 error = copyin(uap->gidset, cr->cr_groups,
613 ngrp * sizeof(gid_t));
616 cr->cr_ngroups = ngrp;
621 lwkt_reltoken(&p->p_token);
626 sys_setreuid(struct setreuid_args *uap)
628 struct proc *p = curproc;
633 lwkt_gettoken(&p->p_token);
638 if (((ruid != (uid_t)-1 && ruid != cr->cr_ruid &&
639 ruid != cr->cr_svuid) ||
640 (euid != (uid_t)-1 && euid != cr->cr_uid &&
641 euid != cr->cr_ruid && euid != cr->cr_svuid)) &&
642 (error = priv_check_cred(cr, PRIV_CRED_SETREUID, 0)) != 0) {
646 if (euid != (uid_t)-1 && cr->cr_uid != euid) {
647 cr = change_euid(euid);
650 if (ruid != (uid_t)-1 && cr->cr_ruid != ruid) {
651 cr = change_ruid(ruid);
654 if ((ruid != (uid_t)-1 || cr->cr_uid != cr->cr_ruid) &&
655 cr->cr_svuid != cr->cr_uid) {
657 cr->cr_svuid = cr->cr_uid;
662 lwkt_reltoken(&p->p_token);
667 sys_setregid(struct setregid_args *uap)
669 struct proc *p = curproc;
674 lwkt_gettoken(&p->p_token);
679 if (((rgid != (gid_t)-1 && rgid != cr->cr_rgid &&
680 rgid != cr->cr_svgid) ||
681 (egid != (gid_t)-1 && egid != cr->cr_groups[0] &&
682 egid != cr->cr_rgid && egid != cr->cr_svgid)) &&
683 (error = priv_check_cred(cr, PRIV_CRED_SETREGID, 0)) != 0) {
687 if (egid != (gid_t)-1 && cr->cr_groups[0] != egid) {
689 cr->cr_groups[0] = egid;
692 if (rgid != (gid_t)-1 && cr->cr_rgid != rgid) {
697 if ((rgid != (gid_t)-1 || cr->cr_groups[0] != cr->cr_rgid) &&
698 cr->cr_svgid != cr->cr_groups[0]) {
700 cr->cr_svgid = cr->cr_groups[0];
705 lwkt_reltoken(&p->p_token);
710 * setresuid(ruid, euid, suid) is like setreuid except control over the
711 * saved uid is explicit.
714 sys_setresuid(struct setresuid_args *uap)
716 struct proc *p = curproc;
718 uid_t ruid, euid, suid;
721 lwkt_gettoken(&p->p_token);
727 if (((ruid != (uid_t)-1 && ruid != cr->cr_ruid &&
728 ruid != cr->cr_svuid && ruid != cr->cr_uid) ||
729 (euid != (uid_t)-1 && euid != cr->cr_ruid &&
730 euid != cr->cr_svuid && euid != cr->cr_uid) ||
731 (suid != (uid_t)-1 && suid != cr->cr_ruid &&
732 suid != cr->cr_svuid && suid != cr->cr_uid)) &&
733 (error = priv_check_cred(cr, PRIV_CRED_SETRESUID, 0)) != 0) {
736 if (euid != (uid_t)-1 && cr->cr_uid != euid) {
737 cr = change_euid(euid);
740 if (ruid != (uid_t)-1 && cr->cr_ruid != ruid) {
741 cr = change_ruid(ruid);
744 if (suid != (uid_t)-1 && cr->cr_svuid != suid) {
751 lwkt_reltoken(&p->p_token);
756 * setresgid(rgid, egid, sgid) is like setregid except control over the
757 * saved gid is explicit.
760 sys_setresgid(struct setresgid_args *uap)
762 struct proc *p = curproc;
764 gid_t rgid, egid, sgid;
767 lwkt_gettoken(&p->p_token);
772 if (((rgid != (gid_t)-1 && rgid != cr->cr_rgid &&
773 rgid != cr->cr_svgid && rgid != cr->cr_groups[0]) ||
774 (egid != (gid_t)-1 && egid != cr->cr_rgid &&
775 egid != cr->cr_svgid && egid != cr->cr_groups[0]) ||
776 (sgid != (gid_t)-1 && sgid != cr->cr_rgid &&
777 sgid != cr->cr_svgid && sgid != cr->cr_groups[0])) &&
778 (error = priv_check_cred(cr, PRIV_CRED_SETRESGID, 0)) != 0) {
782 if (egid != (gid_t)-1 && cr->cr_groups[0] != egid) {
784 cr->cr_groups[0] = egid;
787 if (rgid != (gid_t)-1 && cr->cr_rgid != rgid) {
792 if (sgid != (gid_t)-1 && cr->cr_svgid != sgid) {
799 lwkt_reltoken(&p->p_token);
804 sys_getresuid(struct getresuid_args *uap)
807 int error1 = 0, error2 = 0, error3 = 0;
810 * copyout's can fault synchronously so we cannot use a shared
813 cr = curthread->td_ucred;
815 error1 = copyout((caddr_t)&cr->cr_ruid,
816 (caddr_t)uap->ruid, sizeof(cr->cr_ruid));
818 error2 = copyout((caddr_t)&cr->cr_uid,
819 (caddr_t)uap->euid, sizeof(cr->cr_uid));
821 error3 = copyout((caddr_t)&cr->cr_svuid,
822 (caddr_t)uap->suid, sizeof(cr->cr_svuid));
823 return error1 ? error1 : (error2 ? error2 : error3);
827 sys_getresgid(struct getresgid_args *uap)
830 int error1 = 0, error2 = 0, error3 = 0;
832 cr = curthread->td_ucred;
834 error1 = copyout(&cr->cr_rgid, uap->rgid,
835 sizeof(cr->cr_rgid));
837 error2 = copyout(&cr->cr_groups[0], uap->egid,
838 sizeof(cr->cr_groups[0]));
840 error3 = copyout(&cr->cr_svgid, uap->sgid,
841 sizeof(cr->cr_svgid));
842 return error1 ? error1 : (error2 ? error2 : error3);
847 * NOTE: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
848 * we use P_SUGID because we consider changing the owners as
849 * "tainting" as well.
850 * This is significant for procs that start as root and "become"
851 * a user without an exec - programs cannot know *everything*
852 * that libc *might* have put in their data segment.
855 sys_issetugid(struct issetugid_args *uap)
857 uap->sysmsg_result = (curproc->p_flags & P_SUGID) ? 1 : 0;
862 * Check if gid is a member of the group set.
865 groupmember(gid_t gid, struct ucred *cred)
870 egp = &(cred->cr_groups[cred->cr_ngroups]);
871 for (gp = cred->cr_groups; gp < egp; gp++) {
879 * Test whether the specified credentials have the privilege
882 * A kernel thread without a process context is assumed to have
883 * the privilege in question. In situations where the caller always
884 * expect a cred to exist, the cred should be passed separately and
885 * priv_check_cred() should be used instead of priv_check().
887 * Returns 0 or error.
890 priv_check(struct thread *td, int priv)
892 if (td->td_lwp != NULL)
893 return priv_check_cred(td->td_ucred, priv, 0);
898 * Check a credential for privilege.
900 * A non-null credential is expected unless NULL_CRED_OKAY is set.
903 priv_check_cred(struct ucred *cred, int priv, int flags)
907 KASSERT(PRIV_VALID(priv), ("priv_check_cred: invalid privilege"));
909 KASSERT(cred != NULL || (flags & NULL_CRED_OKAY),
910 ("priv_check_cred: NULL cred!"));
913 if (flags & NULL_CRED_OKAY)
918 if (cred->cr_uid != 0)
921 error = prison_priv_check(cred, priv);
925 /* NOTE: accounting for suser access (p_acflag/ASU) removed */
930 * Return zero if p1 can fondle p2, return errno (EPERM/ESRCH) otherwise.
933 p_trespass(struct ucred *cr1, struct ucred *cr2)
937 if (!PRISON_CHECK(cr1, cr2))
939 if (cr1->cr_ruid == cr2->cr_ruid)
941 if (cr1->cr_uid == cr2->cr_ruid)
943 if (cr1->cr_ruid == cr2->cr_uid)
945 if (cr1->cr_uid == cr2->cr_uid)
947 if (priv_check_cred(cr1, PRIV_PROC_TRESPASS, 0) == 0)
953 _crinit(struct ucred *cr)
959 crinit(struct ucred *cr)
961 bzero(cr, sizeof(*cr));
966 * Allocate a zeroed cred structure.
973 cr = kmalloc(sizeof(*cr), M_CRED, M_WAITOK|M_ZERO);
979 * Claim another reference to a ucred structure. Can be used with special
982 * It must be possible to call this routine with spinlocks held, meaning
983 * that this routine itself cannot obtain a spinlock.
986 crhold(struct ucred *cr)
988 if (cr != NOCRED && cr != FSCRED)
989 atomic_add_int(&cr->cr_ref, 1);
994 * Drop a reference from the cred structure, free it if the reference count
997 * NOTE: because we used atomic_add_int() above, without a spinlock, we
998 * must also use atomic_subtract_int() below. A spinlock is required
999 * in crfree() to handle multiple callers racing the refcount to 0.
1002 crfree(struct ucred *cr)
1004 if (cr->cr_ref <= 0)
1005 panic("Freeing already free credential! %p", cr);
1006 if (atomic_fetchadd_int(&cr->cr_ref, -1) == 1) {
1008 * Some callers of crget(), such as nfs_statfs(),
1009 * allocate a temporary credential, but don't
1010 * allocate a uidinfo structure.
1012 if (cr->cr_uidinfo != NULL) {
1013 uidrop(cr->cr_uidinfo);
1014 cr->cr_uidinfo = NULL;
1016 if (cr->cr_ruidinfo != NULL) {
1017 uidrop(cr->cr_ruidinfo);
1018 cr->cr_ruidinfo = NULL;
1022 * Destroy empty prisons
1025 prison_free(cr->cr_prison);
1026 cr->cr_prison = NULL; /* safety */
1028 kfree((caddr_t)cr, M_CRED);
1033 * Atomize a cred structure so it can be modified without polluting
1034 * other references to it.
1036 * MPSAFE (however, *pcr must be stable)
1039 cratom(struct ucred **pcr)
1041 struct ucred *oldcr;
1042 struct ucred *newcr;
1045 if (oldcr->cr_ref == 1)
1047 newcr = crget(); /* this might block */
1048 oldcr = *pcr; /* re-cache after potentially blocking */
1050 if (newcr->cr_uidinfo)
1051 uihold(newcr->cr_uidinfo);
1052 if (newcr->cr_ruidinfo)
1053 uihold(newcr->cr_ruidinfo);
1055 prison_hold(newcr->cr_prison);
1064 * Called with a modifying token held, but must still obtain p_spin to
1065 * actually replace p_ucred to handle races against syscall entry from
1066 * other threads which cache p_ucred->td_ucred.
1068 * (the threads will only get the spin-lock, and they only need to in
1069 * the case where td_ucred != p_ucred so this is optimal).
1072 cratom_proc(struct proc *p)
1074 struct ucred *oldcr;
1075 struct ucred *newcr;
1078 if (oldcr->cr_ref == 1)
1081 newcr = crget(); /* this might block */
1082 oldcr = p->p_ucred; /* so re-cache oldcr (do not re-test) */
1084 if (newcr->cr_uidinfo)
1085 uihold(newcr->cr_uidinfo);
1086 if (newcr->cr_ruidinfo)
1087 uihold(newcr->cr_ruidinfo);
1089 prison_hold(newcr->cr_prison);
1092 spin_lock(&p->p_spin);
1094 spin_unlock(&p->p_spin);
1101 * Dup cred struct to a new held one.
1104 crdup(struct ucred *cr)
1106 struct ucred *newcr;
1110 if (newcr->cr_uidinfo)
1111 uihold(newcr->cr_uidinfo);
1112 if (newcr->cr_ruidinfo)
1113 uihold(newcr->cr_ruidinfo);
1115 prison_hold(newcr->cr_prison);
1121 * Fill in a struct xucred based on a struct ucred.
1124 cru2x(struct ucred *cr, struct xucred *xcr)
1127 bzero(xcr, sizeof(*xcr));
1128 xcr->cr_version = XUCRED_VERSION;
1129 xcr->cr_uid = cr->cr_uid;
1130 xcr->cr_ngroups = cr->cr_ngroups;
1131 bcopy(cr->cr_groups, xcr->cr_groups, sizeof(cr->cr_groups));
1135 * Get login name, if available.
1138 sys_getlogin(struct getlogin_args *uap)
1140 struct proc *p = curproc;
1141 char buf[MAXLOGNAME];
1144 if (uap->namelen > MAXLOGNAME) /* namelen is unsigned */
1145 uap->namelen = MAXLOGNAME;
1146 bzero(buf, sizeof(buf));
1147 lwkt_gettoken_shared(&p->p_token);
1148 bcopy(p->p_pgrp->pg_session->s_login, buf, uap->namelen);
1149 lwkt_reltoken(&p->p_token);
1151 error = copyout(buf, uap->namebuf, uap->namelen);
1159 sys_setlogin(struct setlogin_args *uap)
1161 struct thread *td = curthread;
1164 char buf[MAXLOGNAME];
1167 cred = td->td_ucred;
1170 if ((error = priv_check_cred(cred, PRIV_PROC_SETLOGIN, 0)))
1172 bzero(buf, sizeof(buf));
1173 error = copyinstr(uap->namebuf, buf, sizeof(buf), NULL);
1174 if (error == ENAMETOOLONG)
1177 lwkt_gettoken_shared(&p->p_token);
1178 memcpy(p->p_pgrp->pg_session->s_login, buf, sizeof(buf));
1179 lwkt_reltoken(&p->p_token);
1187 struct proc *p = curproc;
1189 KKASSERT(p != NULL);
1190 lwkt_gettoken(&p->p_token);
1191 p->p_flags |= P_SUGID;
1192 if (!(p->p_pfsflags & PF_ISUGID))
1194 lwkt_reltoken(&p->p_token);
1198 * Helper function to change the effective uid of a process
1201 change_euid(uid_t euid)
1203 struct proc *p = curproc;
1206 KKASSERT(p != NULL);
1207 lf_count_adjust(p, 0);
1208 cr = cratom_proc(p);
1210 uireplace(&cr->cr_uidinfo, uifind(euid));
1211 lf_count_adjust(p, 1);
1216 * Helper function to change the real uid of a process
1218 * The per-uid process count for this process is transfered from
1219 * the old uid to the new uid.
1222 change_ruid(uid_t ruid)
1224 struct proc *p = curproc;
1227 KKASSERT(p != NULL);
1229 cr = cratom_proc(p);
1230 chgproccnt(cr->cr_ruidinfo, -1, 0);
1232 uireplace(&cr->cr_ruidinfo, uifind(ruid));
1233 chgproccnt(cr->cr_ruidinfo, 1, 0);