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_lwp_setname(struct lwp_setname_args
*uap
)
252 struct proc
*p
= curproc
;
253 char comm0
[MAXCOMLEN
+ 1];
254 const char *comm
= NULL
;
258 if (uap
->name
!= NULL
) {
259 error
= copyinstr(uap
->name
, comm0
, sizeof(comm0
), NULL
);
261 if (error
!= ENAMETOOLONG
)
264 comm0
[MAXCOMLEN
] = '\0';
268 /* Restore to the default name, i.e. process name. */
272 lwkt_gettoken(&p
->p_token
);
274 lp
= lwp_rb_tree_RB_LOOKUP(&p
->p_lwp_tree
, uap
->tid
);
276 strlcpy(lp
->lwp_thread
->td_comm
, comm
,
277 sizeof(lp
->lwp_thread
->td_comm
));
283 lwkt_reltoken(&p
->p_token
);
288 sys_setsid(struct setsid_args
*uap
)
290 struct proc
*p
= curproc
;
291 struct pgrp
*pg
= NULL
;
294 lwkt_gettoken(&p
->p_token
);
295 if (p
->p_pgid
== p
->p_pid
|| (pg
= pgfind(p
->p_pid
)) != NULL
) {
300 enterpgrp(p
, p
->p_pid
, 1);
301 uap
->sysmsg_result
= p
->p_pid
;
304 lwkt_reltoken(&p
->p_token
);
309 * set process group (setpgid/old setpgrp)
311 * caller does setpgid(targpid, targpgid)
313 * pid must be caller or child of caller (ESRCH)
315 * pid must be in same session (EPERM)
316 * pid can't have done an exec (EACCES)
318 * there must exist some pid in same session having pgid (EPERM)
319 * pid must not be session leader (EPERM)
322 sys_setpgid(struct setpgid_args
*uap
)
324 struct proc
*curp
= curproc
;
325 struct proc
*targp
; /* target process */
326 struct pgrp
*pgrp
= NULL
; /* target pgrp */
332 if (uap
->pid
!= 0 && uap
->pid
!= curp
->p_pid
) {
333 if ((targp
= pfind(uap
->pid
)) == NULL
|| !inferior(targp
)) {
340 lwkt_gettoken(&targp
->p_token
);
341 /* targp now referenced and its token is held */
343 if (targp
->p_pgrp
== NULL
||
344 targp
->p_session
!= curp
->p_session
) {
348 if (targp
->p_flags
& P_EXEC
) {
355 lwkt_gettoken(&targp
->p_token
);
357 if (SESS_LEADER(targp
)) {
361 if (uap
->pgid
== 0) {
362 uap
->pgid
= targp
->p_pid
;
363 } else if (uap
->pgid
!= targp
->p_pid
) {
364 if ((pgrp
= pgfind(uap
->pgid
)) == NULL
||
365 pgrp
->pg_session
!= curp
->p_session
) {
370 error
= enterpgrp(targp
, uap
->pgid
, 0);
375 lwkt_reltoken(&targp
->p_token
);
382 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
383 * compatible. It says that setting the uid/gid to euid/egid is a special
384 * case of "appropriate privilege". Once the rules are expanded out, this
385 * basically means that setuid(nnn) sets all three id's, in all permitted
386 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
387 * does not set the saved id - this is dangerous for traditional BSD
388 * programs. For this reason, we *really* do not want to set
389 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
391 #define POSIX_APPENDIX_B_4_2_2
394 sys_setuid(struct setuid_args
*uap
)
396 struct proc
*p
= curproc
;
401 lwkt_gettoken(&p
->p_token
);
405 * See if we have "permission" by POSIX 1003.1 rules.
407 * Note that setuid(geteuid()) is a special case of
408 * "appropriate privileges" in appendix B.4.2.2. We need
409 * to use this clause to be compatible with traditional BSD
410 * semantics. Basically, it means that "setuid(xx)" sets all
411 * three id's (assuming you have privs).
413 * Notes on the logic. We do things in three steps.
414 * 1: We determine if the euid is going to change, and do EPERM
415 * right away. We unconditionally change the euid later if this
416 * test is satisfied, simplifying that part of the logic.
417 * 2: We determine if the real and/or saved uid's are going to
418 * change. Determined by compile options.
419 * 3: Change euid last. (after tests in #2 for "appropriate privs")
422 if (uid
!= cr
->cr_ruid
&& /* allow setuid(getuid()) */
423 #ifdef _POSIX_SAVED_IDS
424 uid
!= crc
->cr_svuid
&& /* allow setuid(saved gid) */
426 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
427 uid
!= cr
->cr_uid
&& /* allow setuid(geteuid()) */
429 (error
= priv_check_cred(cr
, PRIV_CRED_SETUID
, 0)))
432 #ifdef _POSIX_SAVED_IDS
434 * Do we have "appropriate privileges" (are we root or uid == euid)
435 * If so, we are changing the real uid and/or saved uid.
438 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
441 priv_check_cred(cr
, PRIV_CRED_SETUID
, 0) == 0) /* we are using privs */
445 * Set the real uid and transfer proc count to new user.
447 if (uid
!= cr
->cr_ruid
) {
448 cr
= change_ruid(uid
);
454 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
455 * the security of seteuid() depends on it. B.4.2.2 says it
456 * is important that we should do this.
458 if (cr
->cr_svuid
!= uid
) {
466 * In all permitted cases, we are changing the euid.
467 * Copy credentials so other references do not see our changes.
469 if (cr
->cr_uid
!= uid
) {
475 lwkt_reltoken(&p
->p_token
);
480 sys_seteuid(struct seteuid_args
*uap
)
482 struct proc
*p
= curproc
;
487 lwkt_gettoken(&p
->p_token
);
490 if (euid
!= cr
->cr_ruid
&& /* allow seteuid(getuid()) */
491 euid
!= cr
->cr_svuid
&& /* allow seteuid(saved uid) */
492 (error
= priv_check_cred(cr
, PRIV_CRED_SETEUID
, 0))) {
493 lwkt_reltoken(&p
->p_token
);
498 * Everything's okay, do it. Copy credentials so other references do
499 * not see our changes.
501 if (cr
->cr_uid
!= euid
) {
505 lwkt_reltoken(&p
->p_token
);
510 sys_setgid(struct setgid_args
*uap
)
512 struct proc
*p
= curproc
;
517 lwkt_gettoken(&p
->p_token
);
521 * See if we have "permission" by POSIX 1003.1 rules.
523 * Note that setgid(getegid()) is a special case of
524 * "appropriate privileges" in appendix B.4.2.2. We need
525 * to use this clause to be compatible with traditional BSD
526 * semantics. Basically, it means that "setgid(xx)" sets all
527 * three id's (assuming you have privs).
529 * For notes on the logic here, see setuid() above.
532 if (gid
!= cr
->cr_rgid
&& /* allow setgid(getgid()) */
533 #ifdef _POSIX_SAVED_IDS
534 gid
!= cr
->cr_svgid
&& /* allow setgid(saved gid) */
536 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
537 gid
!= cr
->cr_groups
[0] && /* allow setgid(getegid()) */
539 (error
= priv_check_cred(cr
, PRIV_CRED_SETGID
, 0))) {
543 #ifdef _POSIX_SAVED_IDS
545 * Do we have "appropriate privileges" (are we root or gid == egid)
546 * If so, we are changing the real uid and saved gid.
549 #ifdef POSIX_APPENDIX_B_4_2_2 /* use the clause from B.4.2.2 */
550 gid
== cr
->cr_groups
[0] ||
552 priv_check_cred(cr
, PRIV_CRED_SETGID
, 0) == 0) /* we are using privs */
558 if (cr
->cr_rgid
!= gid
) {
566 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
567 * the security of setegid() depends on it. B.4.2.2 says it
568 * is important that we should do this.
570 if (cr
->cr_svgid
!= gid
) {
577 * In all cases permitted cases, we are changing the egid.
578 * Copy credentials so other references do not see our changes.
580 if (cr
->cr_groups
[0] != gid
) {
582 cr
->cr_groups
[0] = gid
;
587 lwkt_reltoken(&p
->p_token
);
592 sys_setegid(struct setegid_args
*uap
)
594 struct proc
*p
= curproc
;
599 lwkt_gettoken(&p
->p_token
);
602 if (egid
!= cr
->cr_rgid
&& /* allow setegid(getgid()) */
603 egid
!= cr
->cr_svgid
&& /* allow setegid(saved gid) */
604 (error
= priv_check_cred(cr
, PRIV_CRED_SETEGID
, 0))) {
607 if (cr
->cr_groups
[0] != egid
) {
609 cr
->cr_groups
[0] = egid
;
614 lwkt_reltoken(&p
->p_token
);
619 sys_setgroups(struct setgroups_args
*uap
)
621 struct proc
*p
= curproc
;
626 lwkt_gettoken(&p
->p_token
);
629 if ((error
= priv_check_cred(cr
, PRIV_CRED_SETGROUPS
, 0)))
631 ngrp
= uap
->gidsetsize
;
632 if (ngrp
> NGROUPS
) {
637 * XXX A little bit lazy here. We could test if anything has
638 * changed before cratom() and setting P_SUGID.
643 * setgroups(0, NULL) is a legitimate way of clearing the
644 * groups vector on non-BSD systems (which generally do not
645 * have the egid in the groups[0]). We risk security holes
646 * when running non-BSD software if we do not do the same.
650 error
= copyin(uap
->gidset
, cr
->cr_groups
,
651 ngrp
* sizeof(gid_t
));
654 cr
->cr_ngroups
= ngrp
;
659 lwkt_reltoken(&p
->p_token
);
664 sys_setreuid(struct setreuid_args
*uap
)
666 struct proc
*p
= curproc
;
671 lwkt_gettoken(&p
->p_token
);
676 if (((ruid
!= (uid_t
)-1 && ruid
!= cr
->cr_ruid
&&
677 ruid
!= cr
->cr_svuid
) ||
678 (euid
!= (uid_t
)-1 && euid
!= cr
->cr_uid
&&
679 euid
!= cr
->cr_ruid
&& euid
!= cr
->cr_svuid
)) &&
680 (error
= priv_check_cred(cr
, PRIV_CRED_SETREUID
, 0)) != 0) {
684 if (euid
!= (uid_t
)-1 && cr
->cr_uid
!= euid
) {
685 cr
= change_euid(euid
);
688 if (ruid
!= (uid_t
)-1 && cr
->cr_ruid
!= ruid
) {
689 cr
= change_ruid(ruid
);
692 if ((ruid
!= (uid_t
)-1 || cr
->cr_uid
!= cr
->cr_ruid
) &&
693 cr
->cr_svuid
!= cr
->cr_uid
) {
695 cr
->cr_svuid
= cr
->cr_uid
;
700 lwkt_reltoken(&p
->p_token
);
705 sys_setregid(struct setregid_args
*uap
)
707 struct proc
*p
= curproc
;
712 lwkt_gettoken(&p
->p_token
);
717 if (((rgid
!= (gid_t
)-1 && rgid
!= cr
->cr_rgid
&&
718 rgid
!= cr
->cr_svgid
) ||
719 (egid
!= (gid_t
)-1 && egid
!= cr
->cr_groups
[0] &&
720 egid
!= cr
->cr_rgid
&& egid
!= cr
->cr_svgid
)) &&
721 (error
= priv_check_cred(cr
, PRIV_CRED_SETREGID
, 0)) != 0) {
725 if (egid
!= (gid_t
)-1 && cr
->cr_groups
[0] != egid
) {
727 cr
->cr_groups
[0] = egid
;
730 if (rgid
!= (gid_t
)-1 && cr
->cr_rgid
!= rgid
) {
735 if ((rgid
!= (gid_t
)-1 || cr
->cr_groups
[0] != cr
->cr_rgid
) &&
736 cr
->cr_svgid
!= cr
->cr_groups
[0]) {
738 cr
->cr_svgid
= cr
->cr_groups
[0];
743 lwkt_reltoken(&p
->p_token
);
748 * setresuid(ruid, euid, suid) is like setreuid except control over the
749 * saved uid is explicit.
752 sys_setresuid(struct setresuid_args
*uap
)
754 struct proc
*p
= curproc
;
756 uid_t ruid
, euid
, suid
;
759 lwkt_gettoken(&p
->p_token
);
765 if (((ruid
!= (uid_t
)-1 && ruid
!= cr
->cr_ruid
&&
766 ruid
!= cr
->cr_svuid
&& ruid
!= cr
->cr_uid
) ||
767 (euid
!= (uid_t
)-1 && euid
!= cr
->cr_ruid
&&
768 euid
!= cr
->cr_svuid
&& euid
!= cr
->cr_uid
) ||
769 (suid
!= (uid_t
)-1 && suid
!= cr
->cr_ruid
&&
770 suid
!= cr
->cr_svuid
&& suid
!= cr
->cr_uid
)) &&
771 (error
= priv_check_cred(cr
, PRIV_CRED_SETRESUID
, 0)) != 0) {
774 if (euid
!= (uid_t
)-1 && cr
->cr_uid
!= euid
) {
775 cr
= change_euid(euid
);
778 if (ruid
!= (uid_t
)-1 && cr
->cr_ruid
!= ruid
) {
779 cr
= change_ruid(ruid
);
782 if (suid
!= (uid_t
)-1 && cr
->cr_svuid
!= suid
) {
789 lwkt_reltoken(&p
->p_token
);
794 * setresgid(rgid, egid, sgid) is like setregid except control over the
795 * saved gid is explicit.
798 sys_setresgid(struct setresgid_args
*uap
)
800 struct proc
*p
= curproc
;
802 gid_t rgid
, egid
, sgid
;
805 lwkt_gettoken(&p
->p_token
);
810 if (((rgid
!= (gid_t
)-1 && rgid
!= cr
->cr_rgid
&&
811 rgid
!= cr
->cr_svgid
&& rgid
!= cr
->cr_groups
[0]) ||
812 (egid
!= (gid_t
)-1 && egid
!= cr
->cr_rgid
&&
813 egid
!= cr
->cr_svgid
&& egid
!= cr
->cr_groups
[0]) ||
814 (sgid
!= (gid_t
)-1 && sgid
!= cr
->cr_rgid
&&
815 sgid
!= cr
->cr_svgid
&& sgid
!= cr
->cr_groups
[0])) &&
816 (error
= priv_check_cred(cr
, PRIV_CRED_SETRESGID
, 0)) != 0) {
820 if (egid
!= (gid_t
)-1 && cr
->cr_groups
[0] != egid
) {
822 cr
->cr_groups
[0] = egid
;
825 if (rgid
!= (gid_t
)-1 && cr
->cr_rgid
!= rgid
) {
830 if (sgid
!= (gid_t
)-1 && cr
->cr_svgid
!= sgid
) {
837 lwkt_reltoken(&p
->p_token
);
842 sys_getresuid(struct getresuid_args
*uap
)
845 int error1
= 0, error2
= 0, error3
= 0;
848 * copyout's can fault synchronously so we cannot use a shared
851 cr
= curthread
->td_ucred
;
853 error1
= copyout((caddr_t
)&cr
->cr_ruid
,
854 (caddr_t
)uap
->ruid
, sizeof(cr
->cr_ruid
));
856 error2
= copyout((caddr_t
)&cr
->cr_uid
,
857 (caddr_t
)uap
->euid
, sizeof(cr
->cr_uid
));
859 error3
= copyout((caddr_t
)&cr
->cr_svuid
,
860 (caddr_t
)uap
->suid
, sizeof(cr
->cr_svuid
));
861 return error1
? error1
: (error2
? error2
: error3
);
865 sys_getresgid(struct getresgid_args
*uap
)
868 int error1
= 0, error2
= 0, error3
= 0;
870 cr
= curthread
->td_ucred
;
872 error1
= copyout(&cr
->cr_rgid
, uap
->rgid
,
873 sizeof(cr
->cr_rgid
));
875 error2
= copyout(&cr
->cr_groups
[0], uap
->egid
,
876 sizeof(cr
->cr_groups
[0]));
878 error3
= copyout(&cr
->cr_svgid
, uap
->sgid
,
879 sizeof(cr
->cr_svgid
));
880 return error1
? error1
: (error2
? error2
: error3
);
885 * NOTE: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
886 * we use P_SUGID because we consider changing the owners as
887 * "tainting" as well.
888 * This is significant for procs that start as root and "become"
889 * a user without an exec - programs cannot know *everything*
890 * that libc *might* have put in their data segment.
893 sys_issetugid(struct issetugid_args
*uap
)
895 uap
->sysmsg_result
= (curproc
->p_flags
& P_SUGID
) ? 1 : 0;
900 * Check if gid is a member of the group set.
903 groupmember(gid_t gid
, struct ucred
*cred
)
908 egp
= &(cred
->cr_groups
[cred
->cr_ngroups
]);
909 for (gp
= cred
->cr_groups
; gp
< egp
; gp
++) {
917 * Test whether the specified credentials have the privilege
920 * A kernel thread without a process context is assumed to have
921 * the privilege in question. In situations where the caller always
922 * expect a cred to exist, the cred should be passed separately and
923 * priv_check_cred() should be used instead of priv_check().
925 * Returns 0 or error.
928 priv_check(struct thread
*td
, int priv
)
930 if (td
->td_lwp
!= NULL
)
931 return priv_check_cred(td
->td_ucred
, priv
, 0);
936 * Check a credential for privilege.
938 * A non-null credential is expected unless NULL_CRED_OKAY is set.
941 priv_check_cred(struct ucred
*cred
, int priv
, int flags
)
945 KASSERT(PRIV_VALID(priv
), ("priv_check_cred: invalid privilege"));
947 KASSERT(cred
!= NULL
|| (flags
& NULL_CRED_OKAY
),
948 ("priv_check_cred: NULL cred!"));
951 if (flags
& NULL_CRED_OKAY
)
956 if (cred
->cr_uid
!= 0)
959 error
= prison_priv_check(cred
, priv
);
963 /* NOTE: accounting for suser access (p_acflag/ASU) removed */
968 * Return zero if p1 can fondle p2, return errno (EPERM/ESRCH) otherwise.
971 p_trespass(struct ucred
*cr1
, struct ucred
*cr2
)
975 if (!PRISON_CHECK(cr1
, cr2
))
977 if (cr1
->cr_ruid
== cr2
->cr_ruid
)
979 if (cr1
->cr_uid
== cr2
->cr_ruid
)
981 if (cr1
->cr_ruid
== cr2
->cr_uid
)
983 if (cr1
->cr_uid
== cr2
->cr_uid
)
985 if (priv_check_cred(cr1
, PRIV_PROC_TRESPASS
, 0) == 0)
991 _crinit(struct ucred
*cr
)
997 crinit(struct ucred
*cr
)
999 bzero(cr
, sizeof(*cr
));
1004 * Allocate a zeroed cred structure.
1011 cr
= kmalloc(sizeof(*cr
), M_CRED
, M_WAITOK
|M_ZERO
);
1017 * Claim another reference to a ucred structure. Can be used with special
1020 * It must be possible to call this routine with spinlocks held, meaning
1021 * that this routine itself cannot obtain a spinlock.
1024 crhold(struct ucred
*cr
)
1026 if (cr
!= NOCRED
&& cr
!= FSCRED
)
1027 atomic_add_int(&cr
->cr_ref
, 1);
1032 * Drop a reference from the cred structure, free it if the reference count
1035 * NOTE: because we used atomic_add_int() above, without a spinlock, we
1036 * must also use atomic_subtract_int() below. A spinlock is required
1037 * in crfree() to handle multiple callers racing the refcount to 0.
1040 crfree(struct ucred
*cr
)
1042 if (cr
->cr_ref
<= 0)
1043 panic("Freeing already free credential! %p", cr
);
1044 if (atomic_fetchadd_int(&cr
->cr_ref
, -1) == 1) {
1046 * Some callers of crget(), such as nfs_statfs(),
1047 * allocate a temporary credential, but don't
1048 * allocate a uidinfo structure.
1050 if (cr
->cr_uidinfo
!= NULL
) {
1051 uidrop(cr
->cr_uidinfo
);
1052 cr
->cr_uidinfo
= NULL
;
1054 if (cr
->cr_ruidinfo
!= NULL
) {
1055 uidrop(cr
->cr_ruidinfo
);
1056 cr
->cr_ruidinfo
= NULL
;
1060 * Destroy empty prisons
1063 prison_free(cr
->cr_prison
);
1064 cr
->cr_prison
= NULL
; /* safety */
1066 kfree((caddr_t
)cr
, M_CRED
);
1071 * Atomize a cred structure so it can be modified without polluting
1072 * other references to it.
1074 * MPSAFE (however, *pcr must be stable)
1077 cratom(struct ucred
**pcr
)
1079 struct ucred
*oldcr
;
1080 struct ucred
*newcr
;
1083 if (oldcr
->cr_ref
== 1)
1085 newcr
= crget(); /* this might block */
1086 oldcr
= *pcr
; /* re-cache after potentially blocking */
1088 if (newcr
->cr_uidinfo
)
1089 uihold(newcr
->cr_uidinfo
);
1090 if (newcr
->cr_ruidinfo
)
1091 uihold(newcr
->cr_ruidinfo
);
1093 prison_hold(newcr
->cr_prison
);
1102 * Called with a modifying token held, but must still obtain p_spin to
1103 * actually replace p_ucred to handle races against syscall entry from
1104 * other threads which cache p_ucred->td_ucred.
1106 * (the threads will only get the spin-lock, and they only need to in
1107 * the case where td_ucred != p_ucred so this is optimal).
1110 cratom_proc(struct proc
*p
)
1112 struct ucred
*oldcr
;
1113 struct ucred
*newcr
;
1116 if (oldcr
->cr_ref
== 1)
1119 newcr
= crget(); /* this might block */
1120 oldcr
= p
->p_ucred
; /* so re-cache oldcr (do not re-test) */
1122 if (newcr
->cr_uidinfo
)
1123 uihold(newcr
->cr_uidinfo
);
1124 if (newcr
->cr_ruidinfo
)
1125 uihold(newcr
->cr_ruidinfo
);
1127 prison_hold(newcr
->cr_prison
);
1130 spin_lock(&p
->p_spin
);
1132 spin_unlock(&p
->p_spin
);
1139 * Dup cred struct to a new held one.
1142 crdup(struct ucred
*cr
)
1144 struct ucred
*newcr
;
1148 if (newcr
->cr_uidinfo
)
1149 uihold(newcr
->cr_uidinfo
);
1150 if (newcr
->cr_ruidinfo
)
1151 uihold(newcr
->cr_ruidinfo
);
1153 prison_hold(newcr
->cr_prison
);
1159 * Fill in a struct xucred based on a struct ucred.
1162 cru2x(struct ucred
*cr
, struct xucred
*xcr
)
1165 bzero(xcr
, sizeof(*xcr
));
1166 xcr
->cr_version
= XUCRED_VERSION
;
1167 xcr
->cr_uid
= cr
->cr_uid
;
1168 xcr
->cr_ngroups
= cr
->cr_ngroups
;
1169 bcopy(cr
->cr_groups
, xcr
->cr_groups
, sizeof(cr
->cr_groups
));
1173 * Get login name, if available.
1176 sys_getlogin(struct getlogin_args
*uap
)
1178 struct proc
*p
= curproc
;
1179 char buf
[MAXLOGNAME
];
1182 if (uap
->namelen
> MAXLOGNAME
) /* namelen is unsigned */
1183 uap
->namelen
= MAXLOGNAME
;
1184 bzero(buf
, sizeof(buf
));
1185 lwkt_gettoken_shared(&p
->p_token
);
1186 bcopy(p
->p_pgrp
->pg_session
->s_login
, buf
, uap
->namelen
);
1187 lwkt_reltoken(&p
->p_token
);
1189 error
= copyout(buf
, uap
->namebuf
, uap
->namelen
);
1197 sys_setlogin(struct setlogin_args
*uap
)
1199 struct thread
*td
= curthread
;
1202 char buf
[MAXLOGNAME
];
1205 cred
= td
->td_ucred
;
1208 if ((error
= priv_check_cred(cred
, PRIV_PROC_SETLOGIN
, 0)))
1210 bzero(buf
, sizeof(buf
));
1211 error
= copyinstr(uap
->namebuf
, buf
, sizeof(buf
), NULL
);
1212 if (error
== ENAMETOOLONG
)
1215 lwkt_gettoken_shared(&p
->p_token
);
1216 memcpy(p
->p_pgrp
->pg_session
->s_login
, buf
, sizeof(buf
));
1217 lwkt_reltoken(&p
->p_token
);
1225 struct proc
*p
= curproc
;
1227 KKASSERT(p
!= NULL
);
1228 lwkt_gettoken(&p
->p_token
);
1229 p
->p_flags
|= P_SUGID
;
1230 if (!(p
->p_pfsflags
& PF_ISUGID
))
1232 lwkt_reltoken(&p
->p_token
);
1236 * Helper function to change the effective uid of a process
1239 change_euid(uid_t euid
)
1241 struct proc
*p
= curproc
;
1244 KKASSERT(p
!= NULL
);
1245 lf_count_adjust(p
, 0);
1246 cr
= cratom_proc(p
);
1248 uireplace(&cr
->cr_uidinfo
, uifind(euid
));
1249 lf_count_adjust(p
, 1);
1254 * Helper function to change the real uid of a process
1256 * The per-uid process count for this process is transfered from
1257 * the old uid to the new uid.
1260 change_ruid(uid_t ruid
)
1262 struct proc
*p
= curproc
;
1265 KKASSERT(p
!= NULL
);
1267 cr
= cratom_proc(p
);
1268 chgproccnt(cr
->cr_ruidinfo
, -1, 0);
1270 uireplace(&cr
->cr_ruidinfo
, uifind(ruid
));
1271 chgproccnt(cr
->cr_ruidinfo
, 1, 0);