Home | History | Annotate | Line # | Download | only in overlay
secmodel_overlay.c revision 1.7
      1 /* $NetBSD: secmodel_overlay.c,v 1.7 2007/01/16 00:11:39 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 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: secmodel_overlay.c,v 1.7 2007/01/16 00:11:39 elad Exp $");
     31 
     32 #include <sys/types.h>
     33 #include <sys/param.h>
     34 #include <sys/kauth.h>
     35 
     36 #include <sys/sysctl.h>
     37 
     38 #include <secmodel/secmodel.h>
     39 #include <secmodel/overlay/overlay.h>
     40 
     41 #include <secmodel/bsd44/bsd44.h>
     42 #include <secmodel/bsd44/suser.h>
     43 #include <secmodel/bsd44/securelevel.h>
     44 
     45 /*
     46  * Fall-back settings.
     47  */
     48 #define	OVERLAY_ISCOPE_GENERIC	"org.netbsd.kauth.overlay.generic"
     49 #define	OVERLAY_ISCOPE_SYSTEM	"org.netbsd.kauth.overlay.system"
     50 #define	OVERLAY_ISCOPE_PROCESS	"org.netbsd.kauth.overlay.process"
     51 #define	OVERLAY_ISCOPE_NETWORK	"org.netbsd.kauth.overlay.network"
     52 #define	OVERLAY_ISCOPE_MACHDEP	"org.netbsd.kauth.overlay.machdep"
     53 #define	OVERLAY_ISCOPE_DEVICE	"org.netbsd.kauth.overlay.device"
     54 
     55 static kauth_scope_t secmodel_overlay_iscope_generic;
     56 static kauth_scope_t secmodel_overlay_iscope_system;
     57 static kauth_scope_t secmodel_overlay_iscope_process;
     58 static kauth_scope_t secmodel_overlay_iscope_network;
     59 static kauth_scope_t secmodel_overlay_iscope_machdep;
     60 static kauth_scope_t secmodel_overlay_iscope_device;
     61 
     62 extern int secmodel_bsd44_curtain;
     63 
     64 /*
     65  * Initialize the overlay security model.
     66  */
     67 void
     68 secmodel_overlay_init(void)
     69 {
     70 	/*
     71 	 * Register internal fall-back scopes.
     72 	 */
     73 	secmodel_overlay_iscope_generic = kauth_register_scope(
     74 	    OVERLAY_ISCOPE_GENERIC, NULL, NULL);
     75 	secmodel_overlay_iscope_system = kauth_register_scope(
     76 	    OVERLAY_ISCOPE_SYSTEM, NULL, NULL);
     77 	secmodel_overlay_iscope_process = kauth_register_scope(
     78 	    OVERLAY_ISCOPE_PROCESS, NULL, NULL);
     79 	secmodel_overlay_iscope_network = kauth_register_scope(
     80 	    OVERLAY_ISCOPE_NETWORK, NULL, NULL);
     81 	secmodel_overlay_iscope_machdep = kauth_register_scope(
     82 	    OVERLAY_ISCOPE_MACHDEP, NULL, NULL);
     83 	secmodel_overlay_iscope_device = kauth_register_scope(
     84 	    OVERLAY_ISCOPE_DEVICE, NULL, NULL);
     85 
     86 	/*
     87 	 * Register fall-back listeners, from bsd44, to each internal
     88 	 * fall-back scope.
     89 	 */
     90 	kauth_listen_scope(OVERLAY_ISCOPE_GENERIC,
     91 	    secmodel_bsd44_suser_generic_cb, NULL);
     92 
     93 	kauth_listen_scope(OVERLAY_ISCOPE_SYSTEM,
     94 	    secmodel_bsd44_suser_system_cb, NULL);
     95 	kauth_listen_scope(OVERLAY_ISCOPE_SYSTEM,
     96 	    secmodel_bsd44_securelevel_system_cb, NULL);
     97 
     98 	kauth_listen_scope(OVERLAY_ISCOPE_PROCESS,
     99 	    secmodel_bsd44_suser_process_cb, NULL);
    100 	kauth_listen_scope(OVERLAY_ISCOPE_PROCESS,
    101 	    secmodel_bsd44_securelevel_process_cb, NULL);
    102 
    103 	kauth_listen_scope(OVERLAY_ISCOPE_NETWORK,
    104 	    secmodel_bsd44_suser_network_cb, NULL);
    105 	kauth_listen_scope(OVERLAY_ISCOPE_NETWORK,
    106 	    secmodel_bsd44_securelevel_network_cb, NULL);
    107 
    108 	kauth_listen_scope(OVERLAY_ISCOPE_MACHDEP,
    109 	    secmodel_bsd44_suser_machdep_cb, NULL);
    110 	kauth_listen_scope(OVERLAY_ISCOPE_MACHDEP,
    111 	    secmodel_bsd44_securelevel_machdep_cb, NULL);
    112 
    113 	kauth_listen_scope(OVERLAY_ISCOPE_DEVICE,
    114 	    secmodel_bsd44_suser_device_cb, NULL);
    115 	kauth_listen_scope(OVERLAY_ISCOPE_DEVICE,
    116 	    secmodel_bsd44_securelevel_device_cb, NULL);
    117 
    118 	secmodel_bsd44_init();
    119 }
    120 
    121 SYSCTL_SETUP(sysctl_security_overlay_setup,
    122     "sysctl security overlay setup")
    123 {
    124 	const struct sysctlnode *rnode;
    125 
    126 	sysctl_createv(clog, 0, NULL, &rnode,
    127 		       CTLFLAG_PERMANENT,
    128 		       CTLTYPE_NODE, "security", NULL,
    129 		       NULL, 0, NULL, 0,
    130 		       CTL_SECURITY, CTL_EOL);
    131 
    132 	sysctl_createv(clog, 0, &rnode, &rnode,
    133 		       CTLFLAG_PERMANENT,
    134 		       CTLTYPE_NODE, "models", NULL,
    135 		       NULL, 0, NULL, 0,
    136 		       CTL_CREATE, CTL_EOL);
    137 
    138 	sysctl_createv(clog, 0, &rnode, &rnode,
    139 		       CTLFLAG_PERMANENT,
    140 		       CTLTYPE_NODE, "overlay",
    141 		       SYSCTL_DESCR("Overlay security model on-top of bsd44, "),
    142 		       NULL, 0, NULL, 0,
    143 		       CTL_CREATE, CTL_EOL);
    144 
    145 	sysctl_createv(clog, 0, &rnode, NULL,
    146 		       CTLFLAG_PERMANENT,
    147 		       CTLTYPE_STRING, "name", NULL,
    148 		       NULL, 0, __UNCONST("Overlay (on-top of bsd44)"), 0,
    149 		       CTL_CREATE, CTL_EOL);
    150 
    151 	sysctl_createv(clog, 0, &rnode, NULL,
    152 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    153 		       CTLTYPE_INT, "securelevel",
    154 		       SYSCTL_DESCR("System security level"),
    155 		       secmodel_bsd44_sysctl_securelevel, 0, NULL, 0,
    156 		       CTL_CREATE, CTL_EOL);
    157 
    158 	sysctl_createv(clog, 0, &rnode, NULL,
    159 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    160 		       CTLTYPE_INT, "curtain",
    161 		       SYSCTL_DESCR("Curtain information about objects to "
    162 				    "users not owning them."),
    163 		       NULL, 0, &secmodel_bsd44_curtain, 0,
    164 		       CTL_CREATE, CTL_EOL);
    165 }
    166 
    167 /*
    168  * Start the overlay security model.
    169  */
    170 void
    171 secmodel_overlay_start(void)
    172 {
    173 	secmodel_overlay_init();
    174 
    175 	kauth_listen_scope(KAUTH_SCOPE_GENERIC,
    176 	    secmodel_overlay_generic_cb, NULL);
    177 	kauth_listen_scope(KAUTH_SCOPE_SYSTEM,
    178 	    secmodel_overlay_system_cb, NULL);
    179 	kauth_listen_scope(KAUTH_SCOPE_PROCESS,
    180 	    secmodel_overlay_process_cb, NULL);
    181 	kauth_listen_scope(KAUTH_SCOPE_NETWORK,
    182 	    secmodel_overlay_network_cb, NULL);
    183 	kauth_listen_scope(KAUTH_SCOPE_MACHDEP,
    184 	    secmodel_overlay_machdep_cb, NULL);
    185 	kauth_listen_scope(KAUTH_SCOPE_DEVICE,
    186 	    secmodel_overlay_device_cb, NULL);
    187 }
    188 
    189 void
    190 secmodel_start(void)
    191 {
    192 	secmodel_overlay_start();
    193 }
    194 
    195 /*
    196  * Overlay listener for the generic scope.
    197  */
    198 int
    199 secmodel_overlay_generic_cb(kauth_cred_t cred, kauth_action_t action,
    200     void *cookie, void *arg0, void *arg1, void *arg2, void *arg3)
    201 {
    202 	int result;
    203 
    204 	result = KAUTH_RESULT_DEFER;
    205 
    206 	switch (action) {
    207 	default:
    208 		result = KAUTH_RESULT_DEFER;
    209 		break;
    210 	}
    211 
    212 	if (result == KAUTH_RESULT_DEFER) {
    213 		result = kauth_authorize_action(
    214 		    secmodel_overlay_iscope_generic, cred, action,
    215 		    arg0, arg1, arg2, arg3);
    216 	}
    217 
    218 	return (result);
    219 }
    220 
    221 /*
    222  * Overlay listener for the system scope.
    223  */
    224 int
    225 secmodel_overlay_system_cb(kauth_cred_t cred, kauth_action_t action,
    226     void *cookie, void *arg0, void *arg1, void *arg2, void *arg3)
    227 {
    228 	int result;
    229 
    230 	result = KAUTH_RESULT_DEFER;
    231 
    232 	switch (action) {
    233 	default:
    234 		result = KAUTH_RESULT_DEFER;
    235 		break;
    236 	}
    237 
    238 	if (result == KAUTH_RESULT_DEFER) {
    239 		result = kauth_authorize_action(
    240 		    secmodel_overlay_iscope_system, cred, action,
    241 		    arg0, arg1, arg2, arg3);
    242 	}
    243 
    244 	return (result);
    245 }
    246 
    247 /*
    248  * Overlay listener for the process scope.
    249  */
    250 int
    251 secmodel_overlay_process_cb(kauth_cred_t cred, kauth_action_t action,
    252     void *cookie, void *arg0, void *arg1, void *arg2, void *arg3)
    253 {
    254 	int result;
    255 
    256 	result = KAUTH_RESULT_DEFER;
    257 
    258 	switch (action) {
    259 	default:
    260 		result = KAUTH_RESULT_DEFER;
    261 		break;
    262 	}
    263 
    264 	if (result == KAUTH_RESULT_DEFER) {
    265 		result = kauth_authorize_action(
    266 		    secmodel_overlay_iscope_process, cred, action,
    267 		    arg0, arg1, arg2, arg3);
    268 	}
    269 
    270 	return (result);
    271 }
    272 
    273 /*
    274  * Overlay listener for the network scope.
    275  */
    276 int
    277 secmodel_overlay_network_cb(kauth_cred_t cred, kauth_action_t action,
    278     void *cookie, void *arg0, void *arg1, void *arg2, void *arg3)
    279 {
    280 	int result;
    281 
    282 	result = KAUTH_RESULT_DEFER;
    283 
    284 	switch (action) {
    285 	default:
    286 		result = KAUTH_RESULT_DEFER;
    287 		break;
    288 	}
    289 
    290 	if (result == KAUTH_RESULT_DEFER) {
    291 		result = kauth_authorize_action(
    292 		    secmodel_overlay_iscope_network, cred, action,
    293 		    arg0, arg1, arg2, arg3);
    294 	}
    295 
    296 	return (result);
    297 }
    298 
    299 /*
    300  * Overlay listener for the machdep scope.
    301  */
    302 int
    303 secmodel_overlay_machdep_cb(kauth_cred_t cred, kauth_action_t action,
    304     void *cookie, void *arg0, void *arg1, void *arg2, void *arg3)
    305 {
    306 	int result;
    307 
    308 	result = KAUTH_RESULT_DEFER;
    309 
    310 	switch (action) {
    311 	default:
    312 		result = KAUTH_RESULT_DEFER;
    313 		break;
    314 	}
    315 
    316 	if (result == KAUTH_RESULT_DEFER) {
    317 		result = kauth_authorize_action(
    318 		    secmodel_overlay_iscope_machdep, cred, action,
    319 		    arg0, arg1, arg2, arg3);
    320 	}
    321 
    322 	return (result);
    323 }
    324 
    325 /*
    326  * Overlay listener for the device scope.
    327  */
    328 int
    329 secmodel_overlay_device_cb(kauth_cred_t cred, kauth_action_t action,
    330     void *cookie, void *arg0, void *arg1, void *arg2, void *arg3)
    331 {
    332 	int result;
    333 
    334 	result = KAUTH_RESULT_DEFER;
    335 
    336 	switch (action) {
    337 	default:
    338 		result = KAUTH_RESULT_DEFER;
    339 		break;
    340 	}
    341 
    342 	if (result == KAUTH_RESULT_DEFER) {
    343 		result = kauth_authorize_action(
    344 		    secmodel_overlay_iscope_device, cred, action,
    345 		    arg0, arg1, arg2, arg3);
    346 	}
    347 
    348 	return (result);
    349 }
    350