secmodel_securelevel.c revision 1.2 1 /* $NetBSD: secmodel_securelevel.c,v 1.2 2007/11/24 20:47:14 elad Exp $ */
2 /*-
3 * Copyright (c) 2006 Elad Efrat <elad (at) NetBSD.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * This file contains kauth(9) listeners needed to implement the traditional
31 * NetBSD securelevel.
32 *
33 * The securelevel is a system-global indication on what operations are
34 * allowed or not. It affects all users, including root.
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: secmodel_securelevel.c,v 1.2 2007/11/24 20:47:14 elad Exp $");
39
40 #ifdef _KERNEL_OPT
41 #include "opt_insecure.h"
42 #endif /* _KERNEL_OPT */
43
44 #include <sys/types.h>
45 #include <sys/param.h>
46 #include <sys/kauth.h>
47
48 #include <sys/conf.h>
49 #include <sys/mount.h>
50 #include <sys/sysctl.h>
51 #include <sys/vnode.h>
52
53 #include <miscfs/specfs/specdev.h>
54
55 #include <secmodel/securelevel/securelevel.h>
56
57 static int securelevel;
58
59 static kauth_listener_t l_system, l_process, l_network, l_machdep, l_device;
60
61 /*
62 * sysctl helper routine for securelevel. ensures that the value
63 * only rises unless the caller has pid 1 (assumed to be init).
64 */
65 int
66 secmodel_securelevel_sysctl(SYSCTLFN_ARGS)
67 {
68 int newsecurelevel, error;
69 struct sysctlnode node;
70
71 newsecurelevel = securelevel;
72 node = *rnode;
73 node.sysctl_data = &newsecurelevel;
74 error = sysctl_lookup(SYSCTLFN_CALL(&node));
75 if (error || newp == NULL)
76 return (error);
77
78 if (newsecurelevel < securelevel && l && l->l_proc->p_pid != 1)
79 return (EPERM);
80
81 securelevel = newsecurelevel;
82
83 return (error);
84 }
85
86 void
87 secmodel_securelevel_init(void)
88 {
89 #ifdef INSECURE
90 securelevel = -1;
91 #else
92 securelevel = 0;
93 #endif /* INSECURE */
94 }
95
96 SYSCTL_SETUP(sysctl_security_securelevel_setup,
97 "sysctl security securelevel setup")
98 {
99 /*
100 * For compatibility, we create a kern.securelevel variable.
101 */
102 sysctl_createv(clog, 0, NULL, NULL,
103 CTLFLAG_PERMANENT,
104 CTLTYPE_NODE, "kern", NULL,
105 NULL, 0, NULL, 0,
106 CTL_KERN, CTL_EOL);
107
108 sysctl_createv(clog, 0, NULL, NULL,
109 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
110 CTLTYPE_INT, "securelevel",
111 SYSCTL_DESCR("System security level"),
112 secmodel_securelevel_sysctl, 0, NULL, 0,
113 CTL_KERN, KERN_SECURELVL, CTL_EOL);
114 }
115
116 void
117 secmodel_securelevel_start(void)
118 {
119 l_system = kauth_listen_scope(KAUTH_SCOPE_SYSTEM,
120 secmodel_securelevel_system_cb, NULL);
121 l_process = kauth_listen_scope(KAUTH_SCOPE_PROCESS,
122 secmodel_securelevel_process_cb, NULL);
123 l_network = kauth_listen_scope(KAUTH_SCOPE_NETWORK,
124 secmodel_securelevel_network_cb, NULL);
125 l_machdep = kauth_listen_scope(KAUTH_SCOPE_MACHDEP,
126 secmodel_securelevel_machdep_cb, NULL);
127 l_device = kauth_listen_scope(KAUTH_SCOPE_DEVICE,
128 secmodel_securelevel_device_cb, NULL);
129 }
130
131 #if defined(_LKM)
132 void
133 secmodel_securelevel_stop(void)
134 {
135 kauth_unlisten_scope(l_system);
136 kauth_unlisten_scope(l_process);
137 kauth_unlisten_scope(l_network);
138 kauth_unlisten_scope(l_machdep);
139 kauth_unlisten_scope(l_device);
140 }
141 #endif /* _LKM */
142
143 /*
144 * kauth(9) listener
145 *
146 * Security model: Traditional NetBSD
147 * Scope: System
148 * Responsibility: Securelevel
149 */
150 int
151 secmodel_securelevel_system_cb(kauth_cred_t cred,
152 kauth_action_t action, void *cookie, void *arg0, void *arg1,
153 void *arg2, void *arg3)
154 {
155 int result;
156 enum kauth_system_req req;
157
158 result = KAUTH_RESULT_DEFER;
159 req = (enum kauth_system_req)arg0;
160
161 switch (action) {
162 case KAUTH_SYSTEM_CHSYSFLAGS:
163 if (securelevel > 0)
164 result = KAUTH_RESULT_DENY;
165 break;
166
167 case KAUTH_SYSTEM_TIME:
168 switch (req) {
169 case KAUTH_REQ_SYSTEM_TIME_BACKWARDS:
170 if (securelevel > 1)
171 result = KAUTH_RESULT_DENY;
172 break;
173
174 case KAUTH_REQ_SYSTEM_TIME_RTCOFFSET:
175 if (securelevel > 0)
176 result = KAUTH_RESULT_DENY;
177 break;
178
179 default:
180 break;
181 }
182 break;
183
184 case KAUTH_SYSTEM_LKM:
185 if (securelevel > 0)
186 result = KAUTH_RESULT_DENY;
187 break;
188
189 case KAUTH_SYSTEM_MOUNT:
190 switch (req) {
191 case KAUTH_REQ_SYSTEM_MOUNT_NEW:
192 if (securelevel > 1)
193 result = KAUTH_RESULT_DENY;
194
195 break;
196
197 case KAUTH_REQ_SYSTEM_MOUNT_UPDATE:
198 if (securelevel > 1) {
199 struct mount *mp = arg1;
200 u_long flags = (u_long)arg2;
201
202 /* Can only degrade from read/write to read-only. */
203 if (flags != (mp->mnt_flag | MNT_RDONLY | MNT_RELOAD |
204 MNT_FORCE | MNT_UPDATE))
205 result = KAUTH_RESULT_DENY;
206 }
207
208 break;
209
210 default:
211 break;
212 }
213
214 break;
215
216 case KAUTH_SYSTEM_SYSCTL:
217 switch (req) {
218 case KAUTH_REQ_SYSTEM_SYSCTL_ADD:
219 case KAUTH_REQ_SYSTEM_SYSCTL_DELETE:
220 case KAUTH_REQ_SYSTEM_SYSCTL_DESC:
221 if (securelevel > 0)
222 result = KAUTH_RESULT_DENY;
223 break;
224
225 default:
226 break;
227 }
228 break;
229
230 case KAUTH_SYSTEM_SETIDCORE:
231 if (securelevel > 0)
232 result = KAUTH_RESULT_DENY;
233 break;
234
235 case KAUTH_SYSTEM_DEBUG:
236 switch (req) {
237 case KAUTH_REQ_SYSTEM_DEBUG_IPKDB:
238 if (securelevel > 0)
239 result = KAUTH_RESULT_DENY;
240 break;
241
242 default:
243 break;
244 }
245 break;
246 }
247
248 return (result);
249 }
250
251 /*
252 * kauth(9) listener
253 *
254 * Security model: Traditional NetBSD
255 * Scope: Process
256 * Responsibility: Securelevel
257 */
258 int
259 secmodel_securelevel_process_cb(kauth_cred_t cred,
260 kauth_action_t action, void *cookie, void *arg0,
261 void *arg1, void *arg2, void *arg3)
262 {
263 struct proc *p;
264 int result;
265
266 result = KAUTH_RESULT_DEFER;
267 p = arg0;
268
269 switch (action) {
270 case KAUTH_PROCESS_CANPROCFS: {
271 enum kauth_process_req req;
272
273 req = (enum kauth_process_req)arg2;
274 switch (req) {
275 case KAUTH_REQ_PROCESS_CANPROCFS_READ:
276 break;
277
278 case KAUTH_REQ_PROCESS_CANPROCFS_RW:
279 case KAUTH_REQ_PROCESS_CANPROCFS_WRITE:
280 if ((p == initproc) && (securelevel > -1))
281 result = KAUTH_RESULT_DENY;
282
283 break;
284
285 default:
286 break;
287 }
288
289 break;
290 }
291
292 case KAUTH_PROCESS_CANPTRACE:
293 case KAUTH_PROCESS_CANSYSTRACE:
294 if ((p == initproc) && (securelevel >= 0))
295 result = KAUTH_RESULT_DENY;
296
297 break;
298
299 case KAUTH_PROCESS_CORENAME:
300 if (securelevel > 1)
301 result = KAUTH_RESULT_DENY;
302 break;
303 }
304
305 return (result);
306 }
307
308 /*
309 * kauth(9) listener
310 *
311 * Security model: Traditional NetBSD
312 * Scope: Network
313 * Responsibility: Securelevel
314 */
315 int
316 secmodel_securelevel_network_cb(kauth_cred_t cred,
317 kauth_action_t action, void *cookie, void *arg0,
318 void *arg1, void *arg2, void *arg3)
319 {
320 int result;
321 enum kauth_network_req req;
322
323 result = KAUTH_RESULT_DEFER;
324 req = (enum kauth_network_req)arg0;
325
326 switch (action) {
327 case KAUTH_NETWORK_FIREWALL:
328 switch (req) {
329 case KAUTH_REQ_NETWORK_FIREWALL_FW:
330 case KAUTH_REQ_NETWORK_FIREWALL_NAT:
331 if (securelevel > 1)
332 result = KAUTH_RESULT_DENY;
333 break;
334
335 default:
336 break;
337 }
338 break;
339
340 case KAUTH_NETWORK_FORWSRCRT:
341 if (securelevel > 0)
342 result = KAUTH_RESULT_DENY;
343 break;
344 }
345
346 return (result);
347 }
348
349 /*
350 * kauth(9) listener
351 *
352 * Security model: Traditional NetBSD
353 * Scope: Machdep
354 * Responsibility: Securelevel
355 */
356 int
357 secmodel_securelevel_machdep_cb(kauth_cred_t cred,
358 kauth_action_t action, void *cookie, void *arg0,
359 void *arg1, void *arg2, void *arg3)
360 {
361 int result;
362
363 result = KAUTH_RESULT_DEFER;
364
365 switch (action) {
366 case KAUTH_MACHDEP_IOPERM_SET:
367 case KAUTH_MACHDEP_IOPL:
368 if (securelevel > 0)
369 result = KAUTH_RESULT_DENY;
370 break;
371
372 case KAUTH_MACHDEP_UNMANAGEDMEM:
373 if (securelevel > 0)
374 result = KAUTH_RESULT_DENY;
375 break;
376 }
377
378 return (result);
379 }
380
381 /*
382 * kauth(9) listener
383 *
384 * Security model: Traditional NetBSD
385 * Scope: Device
386 * Responsibility: Securelevel
387 */
388 int
389 secmodel_securelevel_device_cb(kauth_cred_t cred,
390 kauth_action_t action, void *cookie, void *arg0,
391 void *arg1, void *arg2, void *arg3)
392 {
393 int result;
394
395 result = KAUTH_RESULT_DEFER;
396
397 switch (action) {
398 case KAUTH_DEVICE_RAWIO_SPEC: {
399 struct vnode *vp, *bvp;
400 enum kauth_device_req req;
401 dev_t dev;
402 int d_type;
403
404 req = (enum kauth_device_req)arg0;
405 vp = arg1;
406
407 KASSERT(vp != NULL);
408
409 dev = vp->v_un.vu_specinfo->si_rdev;
410 d_type = D_OTHER;
411 bvp = NULL;
412
413 /* Handle /dev/mem and /dev/kmem. */
414 if ((vp->v_type == VCHR) && iskmemdev(dev)) {
415 switch (req) {
416 case KAUTH_REQ_DEVICE_RAWIO_SPEC_READ:
417 break;
418
419 case KAUTH_REQ_DEVICE_RAWIO_SPEC_WRITE:
420 case KAUTH_REQ_DEVICE_RAWIO_SPEC_RW:
421 if (securelevel > 0)
422 result = KAUTH_RESULT_DENY;
423 break;
424 }
425
426 break;
427 }
428
429 switch (req) {
430 case KAUTH_REQ_DEVICE_RAWIO_SPEC_READ:
431 break;
432
433 case KAUTH_REQ_DEVICE_RAWIO_SPEC_WRITE:
434 case KAUTH_REQ_DEVICE_RAWIO_SPEC_RW:
435 switch (vp->v_type) {
436 case VCHR: {
437 const struct cdevsw *cdev;
438
439 cdev = cdevsw_lookup(dev);
440 if (cdev != NULL) {
441 dev_t blkdev;
442
443 blkdev = devsw_chr2blk(dev);
444 if (blkdev != NODEV) {
445 vfinddev(blkdev, VBLK, &bvp);
446 if (bvp != NULL)
447 d_type = (cdev->d_flag
448 & D_TYPEMASK);
449 }
450 }
451
452 break;
453 }
454 case VBLK: {
455 const struct bdevsw *bdev;
456
457 bdev = bdevsw_lookup(dev);
458 if (bdev != NULL)
459 d_type = (bdev->d_flag & D_TYPEMASK);
460
461 bvp = vp;
462
463 break;
464 }
465
466 default:
467 break;
468 }
469
470 if (d_type != D_DISK)
471 break;
472
473 /*
474 * XXX: This is bogus. We should be failing the request
475 * XXX: not only if this specific slice is mounted, but
476 * XXX: if it's on a disk with any other mounted slice.
477 */
478 if (vfs_mountedon(bvp) && (securelevel > 0))
479 break;
480
481 if (securelevel > 1)
482 result = KAUTH_RESULT_DENY;
483
484 break;
485 }
486
487 break;
488 }
489
490 case KAUTH_DEVICE_RAWIO_PASSTHRU:
491 if (securelevel > 0) {
492 u_long bits;
493
494 bits = (u_long)arg0;
495
496 KASSERT(bits != 0);
497 KASSERT((bits & ~KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_ALL) == 0);
498
499 if (bits & ~KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_READCONF)
500 result = KAUTH_RESULT_DENY;
501 }
502
503 break;
504 }
505
506 return (result);
507 }
508