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/sysmsg.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/spinlock2.h>
59 static MALLOC_DEFINE(M_CRED, "cred", "credentials");
62 sys_getpid(struct sysmsg *sysmsg, const struct getpid_args *uap)
64 struct proc *p = curproc;
66 sysmsg->sysmsg_fds[0] = p->p_pid;
71 sys_getppid(struct sysmsg *sysmsg, const struct getppid_args *uap)
73 struct proc *p = curproc;
75 sysmsg->sysmsg_result = p->p_ppid;
81 sys_lwp_gettid(struct sysmsg *sysmsg, const struct lwp_gettid_args *uap)
83 struct lwp *lp = curthread->td_lwp;
84 sysmsg->sysmsg_result = lp->lwp_tid;
89 * Get process group ID; note that POSIX getpgrp takes no parameter
92 sys_getpgrp(struct sysmsg *sysmsg, const struct getpgrp_args *uap)
94 struct proc *p = curproc;
96 lwkt_gettoken_shared(&p->p_token);
97 sysmsg->sysmsg_result = p->p_pgrp->pg_id;
98 lwkt_reltoken(&p->p_token);
104 * Get an arbitrary pid's process group id
107 sys_getpgid(struct sysmsg *sysmsg, const struct getpgid_args *uap)
109 struct proc *p = curproc;
119 pt = pfind(uap->pid);
124 lwkt_gettoken_shared(&pt->p_token);
125 sysmsg->sysmsg_result = pt->p_pgrp->pg_id;
126 lwkt_reltoken(&pt->p_token);
134 * Get an arbitrary pid's session id.
137 sys_getsid(struct sysmsg *sysmsg, const struct getsid_args *uap)
139 struct proc *p = curproc;
149 pt = pfind(uap->pid);
154 sysmsg->sysmsg_result = pt->p_session->s_sid;
165 sys_getuid(struct sysmsg *sysmsg, const struct getuid_args *uap)
167 struct ucred *cred = curthread->td_ucred;
169 sysmsg->sysmsg_fds[0] = cred->cr_ruid;
177 sys_geteuid(struct sysmsg *sysmsg, const struct geteuid_args *uap)
179 struct ucred *cred = curthread->td_ucred;
181 sysmsg->sysmsg_result = cred->cr_uid;
189 sys_getgid(struct sysmsg *sysmsg, const struct getgid_args *uap)
191 struct ucred *cred = curthread->td_ucred;
193 sysmsg->sysmsg_fds[0] = cred->cr_rgid;
198 * Get effective group ID. The "egid" is groups[0], and could be obtained
199 * via getgroups. This syscall exists because it is somewhat painful to do
200 * correctly in a library function.
203 sys_getegid(struct sysmsg *sysmsg, const struct getegid_args *uap)
205 struct ucred *cred = curthread->td_ucred;
207 sysmsg->sysmsg_result = cred->cr_groups[0];
212 sys_getgroups(struct sysmsg *sysmsg, const struct getgroups_args *uap)
218 cr = curthread->td_ucred;
219 if ((ngrp = uap->gidsetsize) == 0) {
220 sysmsg->sysmsg_result = cr->cr_ngroups;
223 if (ngrp < cr->cr_ngroups)
225 ngrp = cr->cr_ngroups;
226 error = copyout((caddr_t)cr->cr_groups,
227 (caddr_t)uap->gidset, ngrp * sizeof(gid_t));
229 sysmsg->sysmsg_result = ngrp;
234 * Set the per-thread title for ps
237 sys_lwp_setname(struct sysmsg *sysmsg, const struct lwp_setname_args *uap)
239 struct proc *p = curproc;
241 char buf[LPMAP_MAXTHREADTITLE];
245 if (uap->name != NULL) {
246 error = copyinstr(uap->name, buf, sizeof(buf), &len);
248 if (error != ENAMETOOLONG)
250 buf[sizeof(buf)-1] = 0;
251 len = sizeof(buf) - 1;
258 lwkt_gettoken(&p->p_token);
260 lp = lwpfind(p, uap->tid);
262 lwkt_gettoken(&lp->lwp_token);
263 if (lp->lwp_lpmap == NULL)
266 bcopy(buf, lp->lwp_lpmap->thread_title, len);
267 lwkt_reltoken(&lp->lwp_token);
274 lwkt_reltoken(&p->p_token);
280 * Retrieve the per-thread title for ps
283 sys_lwp_getname(struct sysmsg *sysmsg, const struct lwp_getname_args *uap)
285 struct proc *p = curproc;
287 char buf[LPMAP_MAXTHREADTITLE];
293 lwkt_gettoken(&p->p_token);
295 lp = lwpfind(p, uap->tid);
297 lwkt_gettoken(&lp->lwp_token);
298 if (lp->lwp_lpmap == NULL)
301 for (len = 0; len < LPMAP_MAXTHREADTITLE - 1 &&
302 len < uap->len - 1; ++len) {
303 c = lp->lwp_lpmap->thread_title[len];
309 lwkt_reltoken(&lp->lwp_token);
317 lwkt_reltoken(&p->p_token);
320 error = copyout(buf, uap->name, len);
326 sys_setsid(struct sysmsg *sysmsg, const struct setsid_args *uap)
328 struct proc *p = curproc;
329 struct pgrp *pg = NULL;
332 lwkt_gettoken(&p->p_token);
333 if (p->p_pgid == p->p_pid || (pg = pgfind(p->p_pid)) != NULL) {
338 enterpgrp(p, p->p_pid, 1);
339 sysmsg->sysmsg_result = p->p_pid;
342 lwkt_reltoken(&p->p_token);
347 * set process group (setpgid/old setpgrp)
349 * caller does setpgid(targpid, targpgid)
351 * pid must be caller or child of caller (ESRCH)
353 * pid must be in same session (EPERM)
354 * pid can't have done an exec (EACCES)
356 * there must exist some pid in same session having pgid (EPERM)
357 * pid must not be session leader (EPERM)
360 sys_setpgid(struct sysmsg *sysmsg, const struct setpgid_args *uap)
362 struct proc *curp = curproc;
363 struct proc *targp; /* target process */
364 struct pgrp *pgrp = NULL; /* target pgrp */
366 int pgid = uap->pgid;
371 if (uap->pid != 0 && uap->pid != curp->p_pid) {
372 if ((targp = pfind(uap->pid)) == NULL || !inferior(targp)) {
379 lwkt_gettoken(&targp->p_token);
380 /* targp now referenced and its token is held */
382 if (targp->p_pgrp == NULL ||
383 targp->p_session != curp->p_session) {
387 if (targp->p_flags & P_EXEC) {
394 lwkt_gettoken(&targp->p_token);
396 if (SESS_LEADER(targp)) {
402 } else if (pgid != targp->p_pid) {
403 if ((pgrp = pgfind(pgid)) == NULL ||
404 pgrp->pg_session != curp->p_session) {
409 error = enterpgrp(targp, pgid, 0);
414 lwkt_reltoken(&targp->p_token);
421 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
422 * compatible. It says that setting the uid/gid to euid/egid is a special
423 * case of "appropriate privilege". Once the rules are expanded out, this
424 * basically means that setuid(nnn) sets all three id's, in all permitted
425 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
426 * does not set the saved id - this is dangerous for traditional BSD
427 * programs. For this reason, we *really* do not want to set
428 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
430 #define POSIX_APPENDIX_B_4_2_2
433 sys_setuid(struct sysmsg *sysmsg, const struct setuid_args *uap)
435 struct proc *p = curproc;
440 lwkt_gettoken(&p->p_token);
444 * See if we have "permission" by POSIX 1003.1 rules.
446 * Note that setuid(geteuid()) is a special case of
447 * "appropriate privileges" in appendix B.4.2.2. We need
448 * to use this clause to be compatible with traditional BSD
449 * semantics. Basically, it means that "setuid(xx)" sets all
450 * three id's (assuming you have privs).
452 * Notes on the logic. We do things in three steps.
453 * 1: We determine if the euid is going to change, and do EPERM
454 * right away. We unconditionally change the euid later if this
455 * test is satisfied, simplifying that part of the logic.
456 * 2: We determine if the real and/or saved uid's are going to
457 * change. Determined by compile options.
458 * 3: Change euid last. (after tests in #2 for "appropriate privs")
461 if (uid != cr->cr_ruid && /* allow setuid(getuid()) */
462 #ifdef _POSIX_SAVED_IDS
463 uid != crc->cr_svuid && /* allow setuid(saved gid) */
465 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
466 uid != cr->cr_uid && /* allow setuid(geteuid()) */
468 (error = priv_check_cred(cr, PRIV_CRED_SETUID, 0)))
471 #ifdef _POSIX_SAVED_IDS
473 * Do we have "appropriate privileges" (are we root or uid == euid)
474 * If so, we are changing the real uid and/or saved uid.
477 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
480 priv_check_cred(cr, PRIV_CRED_SETUID, 0) == 0) /* we are using privs */
484 * Set the real uid and transfer proc count to new user.
486 if (uid != cr->cr_ruid) {
487 cr = change_ruid(uid);
493 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
494 * the security of seteuid() depends on it. B.4.2.2 says it
495 * is important that we should do this.
497 if (cr->cr_svuid != uid) {
505 * In all permitted cases, we are changing the euid.
506 * Copy credentials so other references do not see our changes.
508 if (cr->cr_uid != uid) {
514 lwkt_reltoken(&p->p_token);
519 sys_seteuid(struct sysmsg *sysmsg, const struct seteuid_args *uap)
521 struct proc *p = curproc;
526 lwkt_gettoken(&p->p_token);
529 if (euid != cr->cr_ruid && /* allow seteuid(getuid()) */
530 euid != cr->cr_svuid && /* allow seteuid(saved uid) */
531 (error = priv_check_cred(cr, PRIV_CRED_SETEUID, 0))) {
532 lwkt_reltoken(&p->p_token);
537 * Everything's okay, do it. Copy credentials so other references do
538 * not see our changes.
540 if (cr->cr_uid != euid) {
544 lwkt_reltoken(&p->p_token);
549 sys_setgid(struct sysmsg *sysmsg, const struct setgid_args *uap)
551 struct proc *p = curproc;
556 lwkt_gettoken(&p->p_token);
560 * See if we have "permission" by POSIX 1003.1 rules.
562 * Note that setgid(getegid()) is a special case of
563 * "appropriate privileges" in appendix B.4.2.2. We need
564 * to use this clause to be compatible with traditional BSD
565 * semantics. Basically, it means that "setgid(xx)" sets all
566 * three id's (assuming you have privs).
568 * For notes on the logic here, see setuid() above.
571 if (gid != cr->cr_rgid && /* allow setgid(getgid()) */
572 #ifdef _POSIX_SAVED_IDS
573 gid != cr->cr_svgid && /* allow setgid(saved gid) */
575 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
576 gid != cr->cr_groups[0] && /* allow setgid(getegid()) */
578 (error = priv_check_cred(cr, PRIV_CRED_SETGID, 0))) {
582 #ifdef _POSIX_SAVED_IDS
584 * Do we have "appropriate privileges" (are we root or gid == egid)
585 * If so, we are changing the real uid and saved gid.
588 #ifdef POSIX_APPENDIX_B_4_2_2 /* use the clause from B.4.2.2 */
589 gid == cr->cr_groups[0] ||
591 priv_check_cred(cr, PRIV_CRED_SETGID, 0) == 0) /* we are using privs */
597 if (cr->cr_rgid != gid) {
605 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
606 * the security of setegid() depends on it. B.4.2.2 says it
607 * is important that we should do this.
609 if (cr->cr_svgid != gid) {
616 * In all cases permitted cases, we are changing the egid.
617 * Copy credentials so other references do not see our changes.
619 if (cr->cr_groups[0] != gid) {
621 cr->cr_groups[0] = gid;
626 lwkt_reltoken(&p->p_token);
631 sys_setegid(struct sysmsg *sysmsg, const struct setegid_args *uap)
633 struct proc *p = curproc;
638 lwkt_gettoken(&p->p_token);
641 if (egid != cr->cr_rgid && /* allow setegid(getgid()) */
642 egid != cr->cr_svgid && /* allow setegid(saved gid) */
643 (error = priv_check_cred(cr, PRIV_CRED_SETEGID, 0))) {
646 if (cr->cr_groups[0] != egid) {
648 cr->cr_groups[0] = egid;
653 lwkt_reltoken(&p->p_token);
658 sys_setgroups(struct sysmsg *sysmsg, const struct setgroups_args *uap)
660 struct proc *p = curproc;
665 lwkt_gettoken(&p->p_token);
668 if ((error = priv_check_cred(cr, PRIV_CRED_SETGROUPS, 0)))
670 ngrp = uap->gidsetsize;
671 if (ngrp > NGROUPS) {
676 * XXX A little bit lazy here. We could test if anything has
677 * changed before cratom() and setting P_SUGID.
682 * setgroups(0, NULL) is a legitimate way of clearing the
683 * groups vector on non-BSD systems (which generally do not
684 * have the egid in the groups[0]). We risk security holes
685 * when running non-BSD software if we do not do the same.
689 error = copyin(uap->gidset, cr->cr_groups,
690 ngrp * sizeof(gid_t));
693 cr->cr_ngroups = ngrp;
698 lwkt_reltoken(&p->p_token);
703 sys_setreuid(struct sysmsg *sysmsg, const struct setreuid_args *uap)
705 struct proc *p = curproc;
710 lwkt_gettoken(&p->p_token);
715 if (((ruid != (uid_t)-1 && ruid != cr->cr_ruid &&
716 ruid != cr->cr_svuid) ||
717 (euid != (uid_t)-1 && euid != cr->cr_uid &&
718 euid != cr->cr_ruid && euid != cr->cr_svuid)) &&
719 (error = priv_check_cred(cr, PRIV_CRED_SETREUID, 0)) != 0) {
723 if (euid != (uid_t)-1 && cr->cr_uid != euid) {
724 cr = change_euid(euid);
727 if (ruid != (uid_t)-1 && cr->cr_ruid != ruid) {
728 cr = change_ruid(ruid);
731 if ((ruid != (uid_t)-1 || cr->cr_uid != cr->cr_ruid) &&
732 cr->cr_svuid != cr->cr_uid) {
734 cr->cr_svuid = cr->cr_uid;
739 lwkt_reltoken(&p->p_token);
744 sys_setregid(struct sysmsg *sysmsg, const struct setregid_args *uap)
746 struct proc *p = curproc;
751 lwkt_gettoken(&p->p_token);
756 if (((rgid != (gid_t)-1 && rgid != cr->cr_rgid &&
757 rgid != cr->cr_svgid) ||
758 (egid != (gid_t)-1 && egid != cr->cr_groups[0] &&
759 egid != cr->cr_rgid && egid != cr->cr_svgid)) &&
760 (error = priv_check_cred(cr, PRIV_CRED_SETREGID, 0)) != 0) {
764 if (egid != (gid_t)-1 && cr->cr_groups[0] != egid) {
766 cr->cr_groups[0] = egid;
769 if (rgid != (gid_t)-1 && cr->cr_rgid != rgid) {
774 if ((rgid != (gid_t)-1 || cr->cr_groups[0] != cr->cr_rgid) &&
775 cr->cr_svgid != cr->cr_groups[0]) {
777 cr->cr_svgid = cr->cr_groups[0];
782 lwkt_reltoken(&p->p_token);
787 * setresuid(ruid, euid, suid) is like setreuid except control over the
788 * saved uid is explicit.
791 sys_setresuid(struct sysmsg *sysmsg, const struct setresuid_args *uap)
793 struct proc *p = curproc;
795 uid_t ruid, euid, suid;
798 lwkt_gettoken(&p->p_token);
804 if (((ruid != (uid_t)-1 && ruid != cr->cr_ruid &&
805 ruid != cr->cr_svuid && ruid != cr->cr_uid) ||
806 (euid != (uid_t)-1 && euid != cr->cr_ruid &&
807 euid != cr->cr_svuid && euid != cr->cr_uid) ||
808 (suid != (uid_t)-1 && suid != cr->cr_ruid &&
809 suid != cr->cr_svuid && suid != cr->cr_uid)) &&
810 (error = priv_check_cred(cr, PRIV_CRED_SETRESUID, 0)) != 0) {
813 if (euid != (uid_t)-1 && cr->cr_uid != euid) {
814 cr = change_euid(euid);
817 if (ruid != (uid_t)-1 && cr->cr_ruid != ruid) {
818 cr = change_ruid(ruid);
821 if (suid != (uid_t)-1 && cr->cr_svuid != suid) {
828 lwkt_reltoken(&p->p_token);
833 * setresgid(rgid, egid, sgid) is like setregid except control over the
834 * saved gid is explicit.
837 sys_setresgid(struct sysmsg *sysmsg, const struct setresgid_args *uap)
839 struct proc *p = curproc;
841 gid_t rgid, egid, sgid;
844 lwkt_gettoken(&p->p_token);
849 if (((rgid != (gid_t)-1 && rgid != cr->cr_rgid &&
850 rgid != cr->cr_svgid && rgid != cr->cr_groups[0]) ||
851 (egid != (gid_t)-1 && egid != cr->cr_rgid &&
852 egid != cr->cr_svgid && egid != cr->cr_groups[0]) ||
853 (sgid != (gid_t)-1 && sgid != cr->cr_rgid &&
854 sgid != cr->cr_svgid && sgid != cr->cr_groups[0])) &&
855 (error = priv_check_cred(cr, PRIV_CRED_SETRESGID, 0)) != 0) {
859 if (egid != (gid_t)-1 && cr->cr_groups[0] != egid) {
861 cr->cr_groups[0] = egid;
864 if (rgid != (gid_t)-1 && cr->cr_rgid != rgid) {
869 if (sgid != (gid_t)-1 && cr->cr_svgid != sgid) {
876 lwkt_reltoken(&p->p_token);
881 sys_getresuid(struct sysmsg *sysmsg, const struct getresuid_args *uap)
884 int error1 = 0, error2 = 0, error3 = 0;
887 * copyout's can fault synchronously so we cannot use a shared
890 cr = curthread->td_ucred;
892 error1 = copyout((caddr_t)&cr->cr_ruid,
893 (caddr_t)uap->ruid, sizeof(cr->cr_ruid));
895 error2 = copyout((caddr_t)&cr->cr_uid,
896 (caddr_t)uap->euid, sizeof(cr->cr_uid));
898 error3 = copyout((caddr_t)&cr->cr_svuid,
899 (caddr_t)uap->suid, sizeof(cr->cr_svuid));
900 return error1 ? error1 : (error2 ? error2 : error3);
904 sys_getresgid(struct sysmsg *sysmsg, const struct getresgid_args *uap)
907 int error1 = 0, error2 = 0, error3 = 0;
909 cr = curthread->td_ucred;
911 error1 = copyout(&cr->cr_rgid, uap->rgid,
912 sizeof(cr->cr_rgid));
914 error2 = copyout(&cr->cr_groups[0], uap->egid,
915 sizeof(cr->cr_groups[0]));
917 error3 = copyout(&cr->cr_svgid, uap->sgid,
918 sizeof(cr->cr_svgid));
919 return error1 ? error1 : (error2 ? error2 : error3);
924 * NOTE: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
925 * we use P_SUGID because we consider changing the owners as
926 * "tainting" as well.
927 * This is significant for procs that start as root and "become"
928 * a user without an exec - programs cannot know *everything*
929 * that libc *might* have put in their data segment.
932 sys_issetugid(struct sysmsg *sysmsg, const struct issetugid_args *uap)
934 sysmsg->sysmsg_result = (curproc->p_flags & P_SUGID) ? 1 : 0;
939 * Check if gid is a member of the group set.
942 groupmember(gid_t gid, struct ucred *cred)
947 egp = &(cred->cr_groups[cred->cr_ngroups]);
948 for (gp = cred->cr_groups; gp < egp; gp++) {
956 * Test whether the specified credentials have the privilege
959 * A kernel thread without a process context is assumed to have
960 * the privilege in question. In situations where the caller always
961 * expect a cred to exist, the cred should be passed separately and
962 * priv_check_cred() should be used instead of priv_check().
964 * Returns 0 or error.
967 priv_check(struct thread *td, int priv)
969 if (td->td_lwp != NULL)
970 return priv_check_cred(td->td_ucred, priv, 0);
975 * Check a credential for privilege.
977 * A non-null credential is expected unless NULL_CRED_OKAY is set.
980 priv_check_cred(struct ucred *cred, int priv, int flags)
984 KASSERT(PRIV_VALID(priv), ("priv_check_cred: invalid privilege"));
986 KASSERT(cred != NULL || (flags & NULL_CRED_OKAY),
987 ("priv_check_cred: NULL cred!"));
990 if (flags & NULL_CRED_OKAY)
995 if (cred->cr_uid != 0)
998 error = prison_priv_check(cred, priv);
1002 /* NOTE: accounting for suser access (p_acflag/ASU) removed */
1007 * Return zero if p1 can fondle p2, return errno (EPERM/ESRCH) otherwise.
1010 p_trespass(struct ucred *cr1, struct ucred *cr2)
1014 if (!PRISON_CHECK(cr1, cr2))
1016 if (cr1->cr_ruid == cr2->cr_ruid)
1018 if (cr1->cr_uid == cr2->cr_ruid)
1020 if (cr1->cr_ruid == cr2->cr_uid)
1022 if (cr1->cr_uid == cr2->cr_uid)
1024 if (priv_check_cred(cr1, PRIV_PROC_TRESPASS, 0) == 0)
1030 * Allocate a zeroed cred structure.
1037 cr = kmalloc(sizeof(*cr), M_CRED, M_WAITOK|M_ZERO);
1044 * Claim another reference to a ucred structure. Can be used with special
1047 * It must be possible to call this routine with spinlocks held, meaning
1048 * that this routine itself cannot obtain a spinlock.
1051 crhold(struct ucred *cr)
1053 if (cr != NOCRED && cr != FSCRED)
1054 atomic_add_long(&cr->cr_ref, 1);
1059 * Drop a reference from the cred structure, free it if the reference count
1062 * NOTE: because we used atomic_add_int() above, without a spinlock, we
1063 * must also use atomic_subtract_int() below. A spinlock is required
1064 * in crfree() to handle multiple callers racing the refcount to 0.
1067 crfree(struct ucred *cr)
1069 if (cr->cr_ref <= 0)
1070 panic("Freeing already free credential! %p", cr);
1071 if (atomic_fetchadd_long(&cr->cr_ref, -1) == 1) {
1073 * Some callers of crget(), such as nfs_statfs(),
1074 * allocate a temporary credential, but don't
1075 * allocate a uidinfo structure.
1077 if (cr->cr_uidinfo != NULL) {
1078 uidrop(cr->cr_uidinfo);
1079 cr->cr_uidinfo = NULL;
1081 if (cr->cr_ruidinfo != NULL) {
1082 uidrop(cr->cr_ruidinfo);
1083 cr->cr_ruidinfo = NULL;
1087 * Destroy empty prisons
1090 prison_free(cr->cr_prison);
1091 cr->cr_prison = NULL; /* safety */
1093 kfree((caddr_t)cr, M_CRED);
1098 * Atomize a cred structure so it can be modified without polluting
1099 * other references to it.
1101 * MPSAFE (however, *pcr must be stable)
1104 cratom(struct ucred **pcr)
1106 struct ucred *oldcr;
1107 struct ucred *newcr;
1110 if (oldcr->cr_ref == 1)
1112 newcr = crget(); /* this might block */
1113 oldcr = *pcr; /* re-cache after potentially blocking */
1115 uihold(newcr->cr_uidinfo);
1116 uihold(newcr->cr_ruidinfo);
1118 prison_hold(newcr->cr_prison);
1127 * Called with a modifying token held, but must still obtain p_spin to
1128 * actually replace p_ucred to handle races against syscall entry from
1129 * other threads which cache p_ucred->td_ucred.
1131 * (the threads will only get the spin-lock, and they only need to in
1132 * the case where td_ucred != p_ucred so this is optimal).
1135 cratom_proc(struct proc *p)
1137 struct ucred *oldcr;
1138 struct ucred *newcr;
1141 if (oldcr->cr_ref == 1)
1144 newcr = crget(); /* this might block */
1145 oldcr = p->p_ucred; /* so re-cache oldcr (do not re-test) */
1147 uihold(newcr->cr_uidinfo);
1148 uihold(newcr->cr_ruidinfo);
1150 prison_hold(newcr->cr_prison);
1153 spin_lock(&p->p_spin);
1155 spin_unlock(&p->p_spin);
1162 * Dup cred struct to a new held one.
1165 crdup(struct ucred *cr)
1167 struct ucred *newcr;
1171 uihold(newcr->cr_uidinfo);
1172 uihold(newcr->cr_ruidinfo);
1174 prison_hold(newcr->cr_prison);
1180 * Fill in a struct xucred based on a struct ucred.
1183 cru2x(struct ucred *cr, struct xucred *xcr)
1186 bzero(xcr, sizeof(*xcr));
1187 xcr->cr_version = XUCRED_VERSION;
1188 xcr->cr_uid = cr->cr_uid;
1189 xcr->cr_ngroups = cr->cr_ngroups;
1190 bcopy(cr->cr_groups, xcr->cr_groups, sizeof(cr->cr_groups));
1194 * Get login name, if available.
1197 sys_getlogin(struct sysmsg *sysmsg, const struct getlogin_args *uap)
1199 struct proc *p = curproc;
1200 char buf[MAXLOGNAME];
1204 namelen = uap->namelen;
1205 if (namelen > MAXLOGNAME) /* namelen is unsigned */
1206 namelen = MAXLOGNAME;
1207 bzero(buf, sizeof(buf));
1208 lwkt_gettoken_shared(&p->p_token);
1209 bcopy(p->p_pgrp->pg_session->s_login, buf, namelen);
1210 lwkt_reltoken(&p->p_token);
1212 error = copyout(buf, uap->namebuf, namelen);
1221 sys_setlogin(struct sysmsg *sysmsg, const struct setlogin_args *uap)
1223 struct thread *td = curthread;
1226 char buf[MAXLOGNAME];
1229 cred = td->td_ucred;
1232 if ((error = priv_check_cred(cred, PRIV_PROC_SETLOGIN, 0)))
1234 bzero(buf, sizeof(buf));
1235 error = copyinstr(uap->namebuf, buf, sizeof(buf), NULL);
1236 if (error == ENAMETOOLONG)
1239 lwkt_gettoken_shared(&p->p_token);
1240 memcpy(p->p_pgrp->pg_session->s_login, buf, sizeof(buf));
1241 lwkt_reltoken(&p->p_token);
1249 struct proc *p = curproc;
1251 KKASSERT(p != NULL);
1252 lwkt_gettoken(&p->p_token);
1253 p->p_flags |= P_SUGID;
1254 if (!(p->p_pfsflags & PF_ISUGID))
1256 lwkt_reltoken(&p->p_token);
1260 * Helper function to change the effective uid of a process
1263 change_euid(uid_t euid)
1265 struct proc *p = curproc;
1268 KKASSERT(p != NULL);
1269 lf_count_adjust(p, 0);
1270 cr = cratom_proc(p);
1272 uireplace(&cr->cr_uidinfo, uifind(euid));
1273 lf_count_adjust(p, 1);
1278 * Helper function to change the real uid of a process
1280 * The per-uid process count for this process is transfered from
1281 * the old uid to the new uid.
1284 change_ruid(uid_t ruid)
1286 struct proc *p = curproc;
1289 KKASSERT(p != NULL);
1291 cr = cratom_proc(p);
1292 chgproccnt(cr->cr_ruidinfo, -1, 0);
1294 uireplace(&cr->cr_ruidinfo, uifind(ruid));
1295 chgproccnt(cr->cr_ruidinfo, 1, 0);