Home | History | Annotate | Line # | Download | only in overlay
secmodel_overlay.c revision 1.2
      1 /* $NetBSD: secmodel_overlay.c,v 1.2 2006/09/08 21:57:38 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. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *      This product includes software developed by Elad Efrat.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: secmodel_overlay.c,v 1.2 2006/09/08 21:57:38 elad Exp $");
     34 
     35 #include <sys/types.h>
     36 #include <sys/param.h>
     37 #include <sys/kauth.h>
     38 
     39 #include <sys/sysctl.h>
     40 
     41 #include <secmodel/secmodel.h>
     42 #include <secmodel/overlay/overlay.h>
     43 
     44 #include <secmodel/bsd44/bsd44.h>
     45 #include <secmodel/bsd44/suser.h>
     46 #include <secmodel/bsd44/securelevel.h>
     47 
     48 /*
     49  * Fall-back settings.
     50  */
     51 #define	OVERLAY_ISCOPE_GENERIC	"org.netbsd.kauth.overlay.generic"
     52 #define	OVERLAY_ISCOPE_SYSTEM	"org.netbsd.kauth.overlay.system"
     53 #define	OVERLAY_ISCOPE_PROCESS	"org.netbsd.kauth.overlay.process"
     54 #define	OVERLAY_ISCOPE_NETWORK	"org.netbsd.kauth.overlay.network"
     55 #define	OVERLAY_ISCOPE_MACHDEP	"org.netbsd.kauth.overlay.machdep"
     56 
     57 static kauth_scope_t secmodel_overlay_iscope_generic;
     58 static kauth_scope_t secmodel_overlay_iscope_system;
     59 static kauth_scope_t secmodel_overlay_iscope_process;
     60 static kauth_scope_t secmodel_overlay_iscope_network;
     61 static kauth_scope_t secmodel_overlay_iscope_machdep;
     62 
     63 extern int secmodel_bsd44_curtain;
     64 
     65 /*
     66  * Initialize the overlay security model.
     67  */
     68 void
     69 secmodel_overlay_init(void)
     70 {
     71 	/*
     72 	 * Register internal fall-back scopes.
     73 	 */
     74 	secmodel_overlay_iscope_generic = kauth_register_scope(
     75 	    OVERLAY_ISCOPE_GENERIC, NULL, NULL);
     76 	secmodel_overlay_iscope_system = kauth_register_scope(
     77 	    OVERLAY_ISCOPE_SYSTEM, NULL, NULL);
     78 	secmodel_overlay_iscope_process = kauth_register_scope(
     79 	    OVERLAY_ISCOPE_PROCESS, NULL, NULL);
     80 	secmodel_overlay_iscope_network = kauth_register_scope(
     81 	    OVERLAY_ISCOPE_NETWORK, NULL, NULL);
     82 	secmodel_overlay_iscope_machdep = kauth_register_scope(
     83 	    OVERLAY_ISCOPE_MACHDEP, NULL, NULL);
     84 
     85 	/*
     86 	 * Register fall-back listeners, from bsd44, to each internal
     87 	 * fall-back scope.
     88 	 */
     89 	kauth_listen_scope(OVERLAY_ISCOPE_GENERIC,
     90 	    secmodel_bsd44_suser_generic_cb, NULL);
     91 
     92 	kauth_listen_scope(OVERLAY_ISCOPE_SYSTEM,
     93 	    secmodel_bsd44_suser_system_cb, NULL);
     94 	kauth_listen_scope(OVERLAY_ISCOPE_SYSTEM,
     95 	    secmodel_bsd44_securelevel_system_cb, NULL);
     96 
     97 	kauth_listen_scope(OVERLAY_ISCOPE_PROCESS,
     98 	    secmodel_bsd44_suser_process_cb, NULL);
     99 	kauth_listen_scope(OVERLAY_ISCOPE_PROCESS,
    100 	    secmodel_bsd44_securelevel_process_cb, NULL);
    101 
    102 	kauth_listen_scope(OVERLAY_ISCOPE_NETWORK,
    103 	    secmodel_bsd44_suser_network_cb, NULL);
    104 	kauth_listen_scope(OVERLAY_ISCOPE_NETWORK,
    105 	    secmodel_bsd44_securelevel_network_cb, NULL);
    106 
    107 	kauth_listen_scope(OVERLAY_ISCOPE_MACHDEP,
    108 	    secmodel_bsd44_suser_machdep_cb, NULL);
    109 	kauth_listen_scope(OVERLAY_ISCOPE_MACHDEP,
    110 	    secmodel_bsd44_securelevel_machdep_cb, NULL);
    111 
    112 	secmodel_bsd44_init();
    113 }
    114 
    115 SYSCTL_SETUP(sysctl_security_overlay_setup,
    116     "sysctl security overlay setup")
    117 {
    118 	const struct sysctlnode *rnode;
    119 
    120 	sysctl_createv(clog, 0, NULL, &rnode,
    121 		       CTLFLAG_PERMANENT,
    122 		       CTLTYPE_NODE, "security", NULL,
    123 		       NULL, 0, NULL, 0,
    124 		       CTL_CREATE, CTL_EOL);
    125 
    126 	sysctl_createv(clog, 0, &rnode, &rnode,
    127 		       CTLFLAG_PERMANENT,
    128 		       CTLTYPE_NODE, "models", NULL,
    129 		       NULL, 0, NULL, 0,
    130 		       CTL_CREATE, CTL_EOL);
    131 
    132 	sysctl_createv(clog, 0, &rnode, &rnode,
    133 		       CTLFLAG_PERMANENT,
    134 		       CTLTYPE_NODE, "overlay",
    135 		       SYSCTL_DESCR("Overlay security model on-top of bsd44, "),
    136 		       NULL, 0, NULL, 0,
    137 		       CTL_CREATE, CTL_EOL);
    138 
    139 	sysctl_createv(clog, 0, &rnode, NULL,
    140 		       CTLFLAG_PERMANENT,
    141 		       CTLTYPE_STRING, "name", NULL,
    142 		       NULL, 0, __UNCONST("Overlay (on-top of bsd44)"), 0,
    143 		       CTL_CREATE, CTL_EOL);
    144 
    145 	sysctl_createv(clog, 0, &rnode, NULL,
    146 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    147 		       CTLTYPE_INT, "securelevel",
    148 		       SYSCTL_DESCR("System security level"),
    149 		       secmodel_bsd44_sysctl_securelevel, 0, &securelevel, 0,
    150 		       CTL_CREATE, CTL_EOL);
    151 
    152 	sysctl_createv(clog, 0, &rnode, NULL,
    153 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    154 		       CTLTYPE_INT, "curtain",
    155 		       SYSCTL_DESCR("Curtain information about objects to "
    156 				    "users not owning them."),
    157 		       NULL, 0, &secmodel_bsd44_curtain, 0,
    158 		       CTL_CREATE, CTL_EOL);
    159 }
    160 
    161 /*
    162  * Start the overlay security model.
    163  */
    164 void
    165 secmodel_start(void)
    166 {
    167 	secmodel_overlay_init();
    168 
    169 	kauth_listen_scope(KAUTH_SCOPE_GENERIC,
    170 	    secmodel_overlay_generic_cb, NULL);
    171 	kauth_listen_scope(KAUTH_SCOPE_SYSTEM,
    172 	    secmodel_overlay_system_cb, NULL);
    173 	kauth_listen_scope(KAUTH_SCOPE_PROCESS,
    174 	    secmodel_overlay_process_cb, NULL);
    175 	kauth_listen_scope(KAUTH_SCOPE_NETWORK,
    176 	    secmodel_overlay_network_cb, NULL);
    177 	kauth_listen_scope(KAUTH_SCOPE_MACHDEP,
    178 	    secmodel_overlay_machdep_cb, NULL);
    179 }
    180 
    181 /*
    182  * Overlay listener for the generic scope.
    183  */
    184 int
    185 secmodel_overlay_generic_cb(kauth_cred_t cred, kauth_action_t action,
    186     void *cookie, void *arg0, void *arg1, void *arg2, void *arg3)
    187 {
    188 	int result;
    189 
    190 	result = KAUTH_RESULT_DEFER;
    191 
    192 	switch (action) {
    193 	default:
    194 		result = KAUTH_RESULT_DEFER;
    195 		break;
    196 	}
    197 
    198 	if (result == KAUTH_RESULT_DEFER) {
    199 		result = kauth_authorize_action(
    200 		    secmodel_overlay_iscope_generic, cred, action,
    201 		    arg0, arg1, arg2, arg3);
    202 	}
    203 
    204 	return (result);
    205 }
    206 
    207 /*
    208  * Overlay listener for the system scope.
    209  */
    210 int
    211 secmodel_overlay_system_cb(kauth_cred_t cred, kauth_action_t action,
    212     void *cookie, void *arg0, void *arg1, void *arg2, void *arg3)
    213 {
    214 	int result;
    215 
    216 	result = KAUTH_RESULT_DEFER;
    217 
    218 	switch (action) {
    219 	default:
    220 		result = KAUTH_RESULT_DEFER;
    221 		break;
    222 	}
    223 
    224 	if (result == KAUTH_RESULT_DEFER) {
    225 		result = kauth_authorize_action(
    226 		    secmodel_overlay_iscope_system, cred, action,
    227 		    arg0, arg1, arg2, arg3);
    228 	}
    229 
    230 	return (result);
    231 }
    232 
    233 /*
    234  * Overlay listener for the process scope.
    235  */
    236 int
    237 secmodel_overlay_process_cb(kauth_cred_t cred, kauth_action_t action,
    238     void *cookie, void *arg0, void *arg1, void *arg2, void *arg3)
    239 {
    240 	int result;
    241 
    242 	result = KAUTH_RESULT_DEFER;
    243 
    244 	switch (action) {
    245 	default:
    246 		result = KAUTH_RESULT_DEFER;
    247 		break;
    248 	}
    249 
    250 	if (result == KAUTH_RESULT_DEFER) {
    251 		result = kauth_authorize_action(
    252 		    secmodel_overlay_iscope_process, cred, action,
    253 		    arg0, arg1, arg2, arg3);
    254 	}
    255 
    256 	return (result);
    257 }
    258 
    259 /*
    260  * Overlay listener for the network scope.
    261  */
    262 int
    263 secmodel_overlay_network_cb(kauth_cred_t cred, kauth_action_t action,
    264     void *cookie, void *arg0, void *arg1, void *arg2, void *arg3)
    265 {
    266 	int result;
    267 
    268 	result = KAUTH_RESULT_DEFER;
    269 
    270 	switch (action) {
    271 	default:
    272 		result = KAUTH_RESULT_DEFER;
    273 		break;
    274 	}
    275 
    276 	if (result == KAUTH_RESULT_DEFER) {
    277 		result = kauth_authorize_action(
    278 		    secmodel_overlay_iscope_network, cred, action,
    279 		    arg0, arg1, arg2, arg3);
    280 	}
    281 
    282 	return (result);
    283 }
    284 
    285 /*
    286  * Overlay listener for the machdep scope.
    287  */
    288 int
    289 secmodel_overlay_machdep_cb(kauth_cred_t cred, kauth_action_t action,
    290     void *cookie, void *arg0, void *arg1, void *arg2, void *arg3)
    291 {
    292 	int result;
    293 
    294 	result = KAUTH_RESULT_DEFER;
    295 
    296 	switch (action) {
    297 	default:
    298 		result = KAUTH_RESULT_DEFER;
    299 		break;
    300 	}
    301 
    302 	if (result == KAUTH_RESULT_DEFER) {
    303 		result = kauth_authorize_action(
    304 		    secmodel_overlay_iscope_machdep, cred, action,
    305 		    arg0, arg1, arg2, arg3);
    306 	}
    307 
    308 	return (result);
    309 }
    310