xref: /freebsd/sys/security/mac/mac_system.c (revision 91c878a6935c5c2e99866eb267e5bc3028bf6d2f)
1 /*-
2  * Copyright (c) 2002, 2003 Networks Associates Technology, Inc.
3  * All rights reserved.
4  *
5  * This software was developed for the FreeBSD Project in part by Network
6  * Associates Laboratories, the Security Research Division of Network
7  * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
8  * as part of the DARPA CHATS research program.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "opt_mac.h"
36 
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/lock.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/mutex.h>
43 #include <sys/mac.h>
44 #include <sys/systm.h>
45 #include <sys/vnode.h>
46 #include <sys/sysctl.h>
47 
48 #include <sys/mac_policy.h>
49 
50 #include <security/mac/mac_framework.h>
51 #include <security/mac/mac_internal.h>
52 
53 static int	mac_enforce_kld = 1;
54 SYSCTL_INT(_security_mac, OID_AUTO, enforce_kld, CTLFLAG_RW,
55     &mac_enforce_kld, 0, "Enforce MAC policy on kld operations");
56 TUNABLE_INT("security.mac.enforce_kld", &mac_enforce_kld);
57 
58 static int	mac_enforce_system = 1;
59 SYSCTL_INT(_security_mac, OID_AUTO, enforce_system, CTLFLAG_RW,
60     &mac_enforce_system, 0, "Enforce MAC policy on system operations");
61 TUNABLE_INT("security.mac.enforce_system", &mac_enforce_system);
62 
63 int
64 mac_check_kenv_dump(struct ucred *cred)
65 {
66 	int error;
67 
68 	if (!mac_enforce_system)
69 		return (0);
70 
71 	MAC_CHECK(check_kenv_dump, cred);
72 
73 	return (error);
74 }
75 
76 int
77 mac_check_kenv_get(struct ucred *cred, char *name)
78 {
79 	int error;
80 
81 	if (!mac_enforce_system)
82 		return (0);
83 
84 	MAC_CHECK(check_kenv_get, cred, name);
85 
86 	return (error);
87 }
88 
89 int
90 mac_check_kenv_set(struct ucred *cred, char *name, char *value)
91 {
92 	int error;
93 
94 	if (!mac_enforce_system)
95 		return (0);
96 
97 	MAC_CHECK(check_kenv_set, cred, name, value);
98 
99 	return (error);
100 }
101 
102 int
103 mac_check_kenv_unset(struct ucred *cred, char *name)
104 {
105 	int error;
106 
107 	if (!mac_enforce_system)
108 		return (0);
109 
110 	MAC_CHECK(check_kenv_unset, cred, name);
111 
112 	return (error);
113 }
114 
115 int
116 mac_check_kld_load(struct ucred *cred, struct vnode *vp)
117 {
118 	int error;
119 
120 	ASSERT_VOP_LOCKED(vp, "mac_check_kld_load");
121 
122 	if (!mac_enforce_kld)
123 		return (0);
124 
125 	MAC_CHECK(check_kld_load, cred, vp, vp->v_label);
126 
127 	return (error);
128 }
129 
130 int
131 mac_check_kld_stat(struct ucred *cred)
132 {
133 	int error;
134 
135 	if (!mac_enforce_kld)
136 		return (0);
137 
138 	MAC_CHECK(check_kld_stat, cred);
139 
140 	return (error);
141 }
142 
143 int
144 mac_check_kld_unload(struct ucred *cred)
145 {
146 	int error;
147 
148 	if (!mac_enforce_kld)
149 		return (0);
150 
151 	MAC_CHECK(check_kld_unload, cred);
152 
153 	return (error);
154 }
155 
156 int
157 mac_check_sysarch_ioperm(struct ucred *cred)
158 {
159 	int error;
160 
161 	if (!mac_enforce_system)
162 		return (0);
163 
164 	MAC_CHECK(check_sysarch_ioperm, cred);
165 	return (error);
166 }
167 
168 int
169 mac_check_system_acct(struct ucred *cred, struct vnode *vp)
170 {
171 	int error;
172 
173 	if (vp != NULL) {
174 		ASSERT_VOP_LOCKED(vp, "mac_check_system_acct");
175 	}
176 
177 	if (!mac_enforce_system)
178 		return (0);
179 
180 	MAC_CHECK(check_system_acct, cred, vp,
181 	    vp != NULL ? vp->v_label : NULL);
182 
183 	return (error);
184 }
185 
186 int
187 mac_check_system_nfsd(struct ucred *cred)
188 {
189 	int error;
190 
191 	if (!mac_enforce_system)
192 		return (0);
193 
194 	MAC_CHECK(check_system_nfsd, cred);
195 
196 	return (error);
197 }
198 
199 int
200 mac_check_system_reboot(struct ucred *cred, int howto)
201 {
202 	int error;
203 
204 	if (!mac_enforce_system)
205 		return (0);
206 
207 	MAC_CHECK(check_system_reboot, cred, howto);
208 
209 	return (error);
210 }
211 
212 int
213 mac_check_system_settime(struct ucred *cred)
214 {
215 	int error;
216 
217 	if (!mac_enforce_system)
218 		return (0);
219 
220 	MAC_CHECK(check_system_settime, cred);
221 
222 	return (error);
223 }
224 
225 int
226 mac_check_system_swapon(struct ucred *cred, struct vnode *vp)
227 {
228 	int error;
229 
230 	ASSERT_VOP_LOCKED(vp, "mac_check_system_swapon");
231 
232 	if (!mac_enforce_system)
233 		return (0);
234 
235 	MAC_CHECK(check_system_swapon, cred, vp, vp->v_label);
236 	return (error);
237 }
238 
239 int
240 mac_check_system_swapoff(struct ucred *cred, struct vnode *vp)
241 {
242 	int error;
243 
244 	ASSERT_VOP_LOCKED(vp, "mac_check_system_swapoff");
245 
246 	if (!mac_enforce_system)
247 		return (0);
248 
249 	MAC_CHECK(check_system_swapoff, cred, vp, vp->v_label);
250 	return (error);
251 }
252 
253 int
254 mac_check_system_sysctl(struct ucred *cred, struct sysctl_oid *oidp, void *arg1,
255     int arg2, struct sysctl_req *req)
256 {
257 	int error;
258 
259 	/*
260 	 * XXXMAC: We would very much like to assert the SYSCTL_LOCK here,
261 	 * but since it's not exported from kern_sysctl.c, we can't.
262 	 */
263 	if (!mac_enforce_system)
264 		return (0);
265 
266 	MAC_CHECK(check_system_sysctl, cred, oidp, arg1, arg2, req);
267 
268 	return (error);
269 }
270