1 /* $NetBSD: secmodel_example.c,v 1.24 2008/02/28 16:09:18 elad Exp $ */
4 * This file is placed in the public domain.
8 * Skeleton file for building a NetBSD security model from scratch, containing
9 * every kauth(9) scope, action, and request, as well as some coding hints.
11 * This file will be kept in-sync with the official NetBSD kernel, so *always*
12 * use the latest revision.
15 #include <sys/cdefs.h>
16 __KERNEL_RCSID(0, "$NetBSD: secmodel_example.c,v 1.24 2008/02/28 16:09:18 elad Exp $");
18 #include <sys/types.h>
19 #include <sys/param.h>
20 #include <sys/kauth.h>
22 #include <sys/sysctl.h>
24 #include <secmodel/secmodel.h>
26 #include <secmodel/example/example.h>
29 * Initialize the security model.
32 secmodel_example_init(void)
38 * If the security model is to be used as an LKM, this routine should be
39 * changed, because otherwise creating permanent sysctl(9) nodes will fail.
41 * To make it work, the prototype should be changed to something like:
43 * void secmodel_example_sysctl(void)
45 * and it should be called from secmodel_start().
47 * In addition, the CTLFLAG_PERMANENT flag must be removed from all the
50 SYSCTL_SETUP(sysctl_security_example_setup
,
51 "sysctl security example setup")
53 const struct sysctlnode
*rnode
;
55 sysctl_createv(clog
, 0, NULL
, &rnode
,
57 CTLTYPE_NODE
, "security", NULL
,
61 sysctl_createv(clog
, 0, &rnode
, &rnode
,
63 CTLTYPE_NODE
, "models", NULL
,
67 sysctl_createv(clog
, 0, &rnode
, &rnode
,
69 CTLTYPE_NODE
, "example",
70 SYSCTL_DESCR("example security model"),
74 sysctl_createv(clog
, 0, &rnode
, NULL
,
76 CTLTYPE_STRING
, "name", NULL
,
77 NULL
, 0, __UNCONST("Example"), 0
83 * Start the security model.
88 secmodel_example_init();
90 kauth_listen_scope(KAUTH_SCOPE_GENERIC
,
91 secmodel_example_generic_cb
, NULL
);
92 kauth_listen_scope(KAUTH_SCOPE_SYSTEM
,
93 secmodel_example_system_cb
, NULL
);
94 kauth_listen_scope(KAUTH_SCOPE_PROCESS
,
95 secmodel_example_process_cb
, NULL
);
96 kauth_listen_scope(KAUTH_SCOPE_NETWORK
,
97 secmodel_example_network_cb
, NULL
);
98 kauth_listen_scope(KAUTH_SCOPE_MACHDEP
,
99 secmodel_example_machdep_cb
, NULL
);
103 * Security model: example
107 secmodel_example_generic_cb(kauth_cred_t
, kauth_action_t action
,
108 void *cookie
, void *arg0
, void *arg1
, void *arg2
, void *arg3
)
112 result
= KAUTH_RESULT_DENY
;
115 case KAUTH_GENERIC_ISSUSER
:
116 case KAUTH_GENERIC_CANSEE
:
118 result
= KAUTH_RESULT_DEFER
;
126 * Security model: example
130 secmodel_example_system_cb(kauth_cred_t cred
, kauth_action_t action
,
131 void *cookie
, void *arg0
, void *arg1
, void *arg2
, void *arg3
)
134 enum kauth_system_req req
;
136 result
= KAUTH_RESULT_DENY
;
138 req
= (enum kauth_system_req
)arg0
;
141 case KAUTH_SYSTEM_MOUNT
:
143 case KAUTH_REQ_SYSTEM_MOUNT_GET
:
144 case KAUTH_REQ_SYSTEM_MOUNT_NEW
:
145 case KAUTH_REQ_SYSTEM_MOUNT_UNMOUNT
:
146 case KAUTH_REQ_SYSTEM_MOUNT_UPDATE
:
148 result
= KAUTH_RESULT_DEFER
;
153 case KAUTH_SYSTEM_TIME
:
155 case KAUTH_REQ_SYSTEM_TIME_ADJTIME
:
156 case KAUTH_REQ_SYSTEM_TIME_BACKWARDS
:
157 case KAUTH_REQ_SYSTEM_TIME_NTPADJTIME
:
158 case KAUTH_REQ_SYSTEM_TIME_RTCOFFSET
:
159 case KAUTH_REQ_SYSTEM_TIME_SYSTEM
:
160 case KAUTH_REQ_SYSTEM_TIME_TIMECOUNTERS
:
162 result
= KAUTH_RESULT_DEFER
;
167 case KAUTH_SYSTEM_SYSCTL
:
169 case KAUTH_REQ_SYSTEM_SYSCTL_ADD
:
170 case KAUTH_REQ_SYSTEM_SYSCTL_DELETE
:
171 case KAUTH_REQ_SYSTEM_SYSCTL_DESC
:
173 result
= KAUTH_RESULT_DEFER
;
178 case KAUTH_SYSTEM_CHROOT
:
180 case KAUTH_REQ_SYSTEM_CHROOT_CHROOT
:
181 case KAUTH_REQ_SYSTEM_CHROOT_FCHROOT
:
183 result
= KAUTH_RESULT_DEFER
;
188 case KAUTH_SYSTEM_CPU
:
190 case KAUTH_REQ_SYSTEM_CPU_SETSTATE
:
192 result
= KAUTH_RESULT_DEFER
;
197 case KAUTH_SYSTEM_DEBUG
:
199 case KAUTH_REQ_SYSTEM_DEBUG_IPKDB
:
201 result
= KAUTH_RESULT_DEFER
;
206 case KAUTH_SYSTEM_PSET
:
208 case KAUTH_REQ_SYSTEM_PSET_ASSIGN
:
209 case KAUTH_REQ_SYSTEM_PSET_BIND
:
210 case KAUTH_REQ_SYSTEM_PSET_CREATE
:
211 case KAUTH_REQ_SYSTEM_PSET_DESTROY
:
213 result
= KAUTH_RESULT_DEFER
;
218 case KAUTH_SYSTEM_LKM
:
219 case KAUTH_SYSTEM_FILEHANDLE
:
220 case KAUTH_SYSTEM_MKNOD
:
221 case KAUTH_SYSTEM_MODULE
:
222 case KAUTH_SYSTEM_SETIDCORE
:
223 case KAUTH_SYSTEM_SWAPCTL
:
224 case KAUTH_SYSTEM_ACCOUNTING
:
225 case KAUTH_SYSTEM_REBOOT
:
227 result
= KAUTH_RESULT_DEFER
;
237 * Security model: example
241 secmodel_example_process_cb(kauth_cred_t cred
, kauth_action_t action
,
242 void *cookie
, void *arg0
, void *arg1
, void *arg2
, void *arg3
)
246 result
= KAUTH_RESULT_DENY
;
249 case KAUTH_PROCESS_KTRACE
:
250 switch ((u_long
)arg1
) {
251 case KAUTH_REQ_PROCESS_KTRACE_PERSISTENT
:
253 result
= KAUTH_RESULT_DEFER
;
258 case KAUTH_PROCESS_PROCFS
:
259 case KAUTH_PROCESS_CANSEE
:
260 case KAUTH_PROCESS_SIGNAL
:
261 case KAUTH_PROCESS_PTRACE
:
262 case KAUTH_PROCESS_CORENAME
:
263 case KAUTH_PROCESS_FORK
:
264 case KAUTH_PROCESS_KEVENT_FILTER
:
265 case KAUTH_PROCESS_NICE
:
266 case KAUTH_PROCESS_RLIMIT
:
267 case KAUTH_PROCESS_SCHEDULER_GETAFFINITY
:
268 case KAUTH_PROCESS_SCHEDULER_SETAFFINITY
:
269 case KAUTH_PROCESS_SCHEDULER_GETPARAM
:
270 case KAUTH_PROCESS_SCHEDULER_SETPARAM
:
271 case KAUTH_PROCESS_SETID
:
272 case KAUTH_PROCESS_STOPFLAG
:
274 result
= KAUTH_RESULT_DEFER
;
284 * Security model: example
288 secmodel_example_network_cb(kauth_cred_t cred
, kauth_action_t action
,
289 void *cookie
, void *arg0
, void *arg1
, void *arg2
, void *arg3
)
293 result
= KAUTH_RESULT_DENY
;
296 case KAUTH_NETWORK_ALTQ
:
297 switch((u_long
)arg0
) {
298 case KAUTH_REQ_NETWORK_ALTQ_AFMAP
:
299 case KAUTH_REQ_NETWORK_ALTQ_BLUE
:
300 case KAUTH_REQ_NETWORK_ALTQ_CBQ
:
301 case KAUTH_REQ_NETWORK_ALTQ_CDNR
:
302 case KAUTH_REQ_NETWORK_ALTQ_CONF
:
303 case KAUTH_REQ_NETWORK_ALTQ_FIFOQ
:
304 case KAUTH_REQ_NETWORK_ALTQ_HFSC
:
305 case KAUTH_REQ_NETWORK_ALTQ_JOBS
:
306 case KAUTH_REQ_NETWORK_ALTQ_PRIQ
:
307 case KAUTH_REQ_NETWORK_ALTQ_RED
:
308 case KAUTH_REQ_NETWORK_ALTQ_RIO
:
309 case KAUTH_REQ_NETWORK_ALTQ_WFQ
:
311 result
= KAUTH_RESULT_DEFER
;
316 case KAUTH_NETWORK_BIND
:
317 switch((u_long
)arg0
) {
318 case KAUTH_REQ_NETWORK_BIND_PORT
:
319 case KAUTH_REQ_NETWORK_BIND_PRIVPORT
:
321 result
= KAUTH_RESULT_DEFER
;
326 case KAUTH_NETWORK_FIREWALL
:
327 switch ((u_long
)arg0
) {
328 case KAUTH_REQ_NETWORK_FIREWALL_FW
:
329 case KAUTH_REQ_NETWORK_FIREWALL_NAT
:
331 result
= KAUTH_RESULT_DEFER
;
336 case KAUTH_NETWORK_FORWSRCRT
:
339 case KAUTH_NETWORK_INTERFACE
:
340 switch ((u_long
)arg0
) {
341 case KAUTH_REQ_NETWORK_INTERFACE_GET
:
342 case KAUTH_REQ_NETWORK_INTERFACE_SET
:
343 case KAUTH_REQ_NETWORK_INTERFACE_GETPRIV
:
344 case KAUTH_REQ_NETWORK_INTERFACE_SETPRIV
:
346 result
= KAUTH_RESULT_DEFER
;
351 case KAUTH_NETWORK_NFS
:
352 switch ((u_long
)arg0
) {
353 case KAUTH_REQ_NETWORK_NFS_EXPORT
:
354 case KAUTH_REQ_NETWORK_NFS_SVC
:
356 result
= KAUTH_RESULT_DEFER
;
361 case KAUTH_NETWORK_ROUTE
:
364 case KAUTH_NETWORK_SOCKET
:
365 switch((u_long
)arg0
) {
366 case KAUTH_REQ_NETWORK_SOCKET_OPEN
:
367 case KAUTH_REQ_NETWORK_SOCKET_RAWSOCK
:
368 case KAUTH_REQ_NETWORK_SOCKET_CANSEE
:
370 result
= KAUTH_RESULT_DEFER
;
376 result
= KAUTH_RESULT_DEFER
;
386 * Security model: example
390 secmodel_example_machdep_cb(kauth_cred_t cred
, kauth_action_t action
,
391 void *cookie
, void *arg0
, void *arg1
, void *arg2
, void *arg3
)
395 result
= KAUTH_RESULT_DENY
;
398 case KAUTH_MACHDEP_IOPERM_GET
:
399 case KAUTH_MACHDEP_IOPERM_SET
:
400 case KAUTH_MACHDEP_IOPL
:
401 case KAUTH_MACHDEP_LDT_GET
:
402 case KAUTH_MACHDEP_LDT_SET
:
403 case KAUTH_MACHDEP_MTRR_GET
:
404 case KAUTH_MACHDEP_MTRR_SET
:
405 case KAUTH_MACHDEP_UNMANAGEDMEM
:
407 result
= KAUTH_RESULT_DEFER
;
417 * Security model: example
421 secmodel_example_device_cb(kauth_cred_t cred
, kauth_action_t action
,
422 void *cookie
, void *arg0
, void *arg1
, void *arg2
, void *arg3
)
426 result
= KAUTH_RESULT_DENY
;
429 case KAUTH_DEVICE_TTY_OPEN
:
430 case KAUTH_DEVICE_TTY_PRIVSET
:
431 case KAUTH_DEVICE_TTY_STI
:
434 case KAUTH_DEVICE_RAWIO_SPEC
:
435 switch ((u_long
)arg0
) {
436 case KAUTH_REQ_DEVICE_RAWIO_SPEC_READ
:
437 case KAUTH_REQ_DEVICE_RAWIO_SPEC_WRITE
:
438 case KAUTH_REQ_DEVICE_RAWIO_SPEC_RW
:
442 result
= KAUTH_RESULT_DEFER
;
448 case KAUTH_DEVICE_RAWIO_PASSTHRU
:
450 result
= KAUTH_RESULT_DEFER
;