]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/kern_priv.c
Move vnode-to-file-handle translation from vfs_vptofh to vop_vptofh method.
[FreeBSD/FreeBSD.git] / sys / kern / kern_priv.c
1 /*-
2  * Copyright (c) 2006 nCircle Network Security, Inc.
3  * All rights reserved.
4  *
5  * This software was developed by Robert N. M. Watson for the TrustedBSD
6  * Project under contract to nCircle Network Security, Inc.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR, NCIRCLE NETWORK SECURITY,
21  * INC., OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
23  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31
32 #include "opt_mac.h"
33
34 #include <sys/param.h>
35 #include <sys/jail.h>
36 #include <sys/kernel.h>
37 #include <sys/priv.h>
38 #include <sys/proc.h>
39 #include <sys/sysctl.h>
40 #include <sys/systm.h>
41
42 #include <security/mac/mac_framework.h>
43
44 /*
45  * `suser_enabled' (which can be set by the security.bsd.suser_enabled
46  * sysctl) determines whether the system 'super-user' policy is in effect.  If
47  * it is nonzero, an effective uid of 0 connotes special privilege,
48  * overriding many mandatory and discretionary protections.  If it is zero,
49  * uid 0 is offered no special privilege in the kernel security policy.
50  * Setting it to zero may seriously impact the functionality of many existing
51  * userland programs, and should not be done without careful consideration of
52  * the consequences. 
53  */
54 int     suser_enabled = 1;
55 SYSCTL_INT(_security_bsd, OID_AUTO, suser_enabled, CTLFLAG_RW,
56     &suser_enabled, 0, "processes with uid 0 have privilege");
57 TUNABLE_INT("security.bsd.suser_enabled", &suser_enabled);
58
59 /*
60  * Check a credential for privilege.  Lots of good reasons to deny privilege;
61  * only a few to grant it.
62  */
63 int
64 priv_check_cred(struct ucred *cred, int priv, int flags)
65 {
66         int error;
67
68         KASSERT(PRIV_VALID(priv), ("priv_check_cred: invalid privilege %d",
69             priv));
70
71 #ifdef MAC
72         error = mac_priv_check(cred, priv);
73         if (error)
74                 return (error);
75 #endif
76
77         /*
78          * Jail policy will restrict certain privileges that may otherwise be
79          * be granted.
80          *
81          * While debugging the transition from SUSER_ALLOWJAIL to Jail being
82          * aware of specific privileges, perform run-time checking that the
83          * two versions of the policy align.  This assertion will go away
84          * once the SUSER_ALLOWJAIL flag has gone away.
85          */
86         error = prison_priv_check(cred, priv);
87 #ifdef NOTYET
88         KASSERT(!jailed(cred) || error == ((flags & SUSER_ALLOWJAIL) ? 0 :
89             EPERM), ("priv_check_cred: prison_priv_check %d but flags %s",
90             error, flags & SUSER_ALLOWJAIL ? "allowjail" : "!allowjail"));
91 #endif
92         if (error)
93                 return (error);
94
95         /*
96          * Having determined if privilege is restricted by various policies,
97          * now determine if privilege is granted.  For now, we allow
98          * short-circuit boolean evaluation, so may not call all policies.
99          * Perhaps we should.
100          *
101          * Superuser policy grants privilege based on the effective (or in
102          * certain edge cases, real) uid being 0.  We allow the policy to be
103          * globally disabled, although this is currently of limited utility.
104          */
105         if (suser_enabled) {
106                 if (flags & SUSER_RUID) {
107                         if (cred->cr_ruid == 0)
108                                 return (0);
109                 } else {
110                         if (cred->cr_uid == 0)
111                                 return (0);
112                 }
113         }
114
115         /*
116          * Now check with MAC, if enabled, to see if a policy module grants
117          * privilege.
118          */
119 #ifdef MAC
120         if (mac_priv_grant(cred, priv) == 0)
121                 return (0);
122 #endif
123         return (EPERM);
124 }
125
126 int
127 priv_check(struct thread *td, int priv)
128 {
129
130         KASSERT(td == curthread, ("priv_check: td != curthread"));
131
132         return (priv_check_cred(td->td_ucred, priv, 0));
133 }
134
135 /*
136  * Historical suser() wrapper functions, which now simply request PRIV_ROOT.
137  * These will be removed in the near future, and exist solely because
138  * the kernel and modules are not yet fully adapted to the new model.
139  */
140 int
141 suser_cred(struct ucred *cred, int flags)
142 {
143
144         return (priv_check_cred(cred, PRIV_ROOT, flags));
145 }
146
147 int
148 suser(struct thread *td)
149 {
150
151         KASSERT(td == curthread, ("suser: td != curthread"));
152
153         return (suser_cred(td->td_ucred, 0));
154 }