Home | History | Annotate | Line # | Download | only in overlay
secmodel_overlay.c revision 1.5
      1 /* $NetBSD: secmodel_overlay.c,v 1.5 2007/01/09 12:57:56 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.5 2007/01/09 12:57:56 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, &securelevel, 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_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 /*
    190  * Overlay listener for the generic scope.
    191  */
    192 int
    193 secmodel_overlay_generic_cb(kauth_cred_t cred, kauth_action_t action,
    194     void *cookie, void *arg0, void *arg1, void *arg2, void *arg3)
    195 {
    196 	int result;
    197 
    198 	result = KAUTH_RESULT_DEFER;
    199 
    200 	switch (action) {
    201 	default:
    202 		result = KAUTH_RESULT_DEFER;
    203 		break;
    204 	}
    205 
    206 	if (result == KAUTH_RESULT_DEFER) {
    207 		result = kauth_authorize_action(
    208 		    secmodel_overlay_iscope_generic, cred, action,
    209 		    arg0, arg1, arg2, arg3);
    210 	}
    211 
    212 	return (result);
    213 }
    214 
    215 /*
    216  * Overlay listener for the system scope.
    217  */
    218 int
    219 secmodel_overlay_system_cb(kauth_cred_t cred, kauth_action_t action,
    220     void *cookie, void *arg0, void *arg1, void *arg2, void *arg3)
    221 {
    222 	int result;
    223 
    224 	result = KAUTH_RESULT_DEFER;
    225 
    226 	switch (action) {
    227 	default:
    228 		result = KAUTH_RESULT_DEFER;
    229 		break;
    230 	}
    231 
    232 	if (result == KAUTH_RESULT_DEFER) {
    233 		result = kauth_authorize_action(
    234 		    secmodel_overlay_iscope_system, cred, action,
    235 		    arg0, arg1, arg2, arg3);
    236 	}
    237 
    238 	return (result);
    239 }
    240 
    241 /*
    242  * Overlay listener for the process scope.
    243  */
    244 int
    245 secmodel_overlay_process_cb(kauth_cred_t cred, kauth_action_t action,
    246     void *cookie, void *arg0, void *arg1, void *arg2, void *arg3)
    247 {
    248 	int result;
    249 
    250 	result = KAUTH_RESULT_DEFER;
    251 
    252 	switch (action) {
    253 	default:
    254 		result = KAUTH_RESULT_DEFER;
    255 		break;
    256 	}
    257 
    258 	if (result == KAUTH_RESULT_DEFER) {
    259 		result = kauth_authorize_action(
    260 		    secmodel_overlay_iscope_process, cred, action,
    261 		    arg0, arg1, arg2, arg3);
    262 	}
    263 
    264 	return (result);
    265 }
    266 
    267 /*
    268  * Overlay listener for the network scope.
    269  */
    270 int
    271 secmodel_overlay_network_cb(kauth_cred_t cred, kauth_action_t action,
    272     void *cookie, void *arg0, void *arg1, void *arg2, void *arg3)
    273 {
    274 	int result;
    275 
    276 	result = KAUTH_RESULT_DEFER;
    277 
    278 	switch (action) {
    279 	default:
    280 		result = KAUTH_RESULT_DEFER;
    281 		break;
    282 	}
    283 
    284 	if (result == KAUTH_RESULT_DEFER) {
    285 		result = kauth_authorize_action(
    286 		    secmodel_overlay_iscope_network, cred, action,
    287 		    arg0, arg1, arg2, arg3);
    288 	}
    289 
    290 	return (result);
    291 }
    292 
    293 /*
    294  * Overlay listener for the machdep scope.
    295  */
    296 int
    297 secmodel_overlay_machdep_cb(kauth_cred_t cred, kauth_action_t action,
    298     void *cookie, void *arg0, void *arg1, void *arg2, void *arg3)
    299 {
    300 	int result;
    301 
    302 	result = KAUTH_RESULT_DEFER;
    303 
    304 	switch (action) {
    305 	default:
    306 		result = KAUTH_RESULT_DEFER;
    307 		break;
    308 	}
    309 
    310 	if (result == KAUTH_RESULT_DEFER) {
    311 		result = kauth_authorize_action(
    312 		    secmodel_overlay_iscope_machdep, cred, action,
    313 		    arg0, arg1, arg2, arg3);
    314 	}
    315 
    316 	return (result);
    317 }
    318 
    319 /*
    320  * Overlay listener for the device scope.
    321  */
    322 int
    323 secmodel_overlay_device_cb(kauth_cred_t cred, kauth_action_t action,
    324     void *cookie, void *arg0, void *arg1, void *arg2, void *arg3)
    325 {
    326 	int result;
    327 
    328 	result = KAUTH_RESULT_DEFER;
    329 
    330 	switch (action) {
    331 	default:
    332 		result = KAUTH_RESULT_DEFER;
    333 		break;
    334 	}
    335 
    336 	if (result == KAUTH_RESULT_DEFER) {
    337 		result = kauth_authorize_action(
    338 		    secmodel_overlay_iscope_device, cred, action,
    339 		    arg0, arg1, arg2, arg3);
    340 	}
    341 
    342 	return (result);
    343 }
    344