1 /*- 2 * Copyright (c) 2002, 2003 Networks Associates Technology, Inc. 3 * All rights reserved. 4 * 5 * This software was developed for the FreeBSD Project in part by Network 6 * Associates Laboratories, the Security Research Division of Network 7 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), 8 * as part of the DARPA CHATS research program. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include "opt_mac.h" 36 37 #include <sys/param.h> 38 #include <sys/kernel.h> 39 #include <sys/lock.h> 40 #include <sys/malloc.h> 41 #include <sys/mutex.h> 42 #include <sys/mac.h> 43 #include <sys/systm.h> 44 #include <sys/vnode.h> 45 #include <sys/sysctl.h> 46 47 #include <sys/mac_policy.h> 48 49 #include <security/mac/mac_internal.h> 50 51 static int mac_enforce_kld = 1; 52 SYSCTL_INT(_security_mac, OID_AUTO, enforce_kld, CTLFLAG_RW, 53 &mac_enforce_kld, 0, "Enforce MAC policy on kld operations"); 54 TUNABLE_INT("security.mac.enforce_kld", &mac_enforce_kld); 55 56 static int mac_enforce_system = 1; 57 SYSCTL_INT(_security_mac, OID_AUTO, enforce_system, CTLFLAG_RW, 58 &mac_enforce_system, 0, "Enforce MAC policy on system operations"); 59 TUNABLE_INT("security.mac.enforce_system", &mac_enforce_system); 60 61 int 62 mac_check_kenv_dump(struct ucred *cred) 63 { 64 int error; 65 66 if (!mac_enforce_system) 67 return (0); 68 69 MAC_CHECK(check_kenv_dump, cred); 70 71 return (error); 72 } 73 74 int 75 mac_check_kenv_get(struct ucred *cred, char *name) 76 { 77 int error; 78 79 if (!mac_enforce_system) 80 return (0); 81 82 MAC_CHECK(check_kenv_get, cred, name); 83 84 return (error); 85 } 86 87 int 88 mac_check_kenv_set(struct ucred *cred, char *name, char *value) 89 { 90 int error; 91 92 if (!mac_enforce_system) 93 return (0); 94 95 MAC_CHECK(check_kenv_set, cred, name, value); 96 97 return (error); 98 } 99 100 int 101 mac_check_kenv_unset(struct ucred *cred, char *name) 102 { 103 int error; 104 105 if (!mac_enforce_system) 106 return (0); 107 108 MAC_CHECK(check_kenv_unset, cred, name); 109 110 return (error); 111 } 112 113 int 114 mac_check_kld_load(struct ucred *cred, struct vnode *vp) 115 { 116 int error; 117 118 ASSERT_VOP_LOCKED(vp, "mac_check_kld_load"); 119 120 if (!mac_enforce_kld) 121 return (0); 122 123 MAC_CHECK(check_kld_load, cred, vp, vp->v_label); 124 125 return (error); 126 } 127 128 int 129 mac_check_kld_stat(struct ucred *cred) 130 { 131 int error; 132 133 if (!mac_enforce_kld) 134 return (0); 135 136 MAC_CHECK(check_kld_stat, cred); 137 138 return (error); 139 } 140 141 int 142 mac_check_kld_unload(struct ucred *cred) 143 { 144 int error; 145 146 if (!mac_enforce_kld) 147 return (0); 148 149 MAC_CHECK(check_kld_unload, cred); 150 151 return (error); 152 } 153 154 int 155 mac_check_sysarch_ioperm(struct ucred *cred) 156 { 157 int error; 158 159 if (!mac_enforce_system) 160 return (0); 161 162 MAC_CHECK(check_sysarch_ioperm, cred); 163 return (error); 164 } 165 166 int 167 mac_check_system_acct(struct ucred *cred, struct vnode *vp) 168 { 169 int error; 170 171 if (vp != NULL) { 172 ASSERT_VOP_LOCKED(vp, "mac_check_system_acct"); 173 } 174 175 if (!mac_enforce_system) 176 return (0); 177 178 MAC_CHECK(check_system_acct, cred, vp, 179 vp != NULL ? vp->v_label : NULL); 180 181 return (error); 182 } 183 184 int 185 mac_check_system_nfsd(struct ucred *cred) 186 { 187 int error; 188 189 if (!mac_enforce_system) 190 return (0); 191 192 MAC_CHECK(check_system_nfsd, cred); 193 194 return (error); 195 } 196 197 int 198 mac_check_system_reboot(struct ucred *cred, int howto) 199 { 200 int error; 201 202 if (!mac_enforce_system) 203 return (0); 204 205 MAC_CHECK(check_system_reboot, cred, howto); 206 207 return (error); 208 } 209 210 int 211 mac_check_system_settime(struct ucred *cred) 212 { 213 int error; 214 215 if (!mac_enforce_system) 216 return (0); 217 218 MAC_CHECK(check_system_settime, cred); 219 220 return (error); 221 } 222 223 int 224 mac_check_system_swapon(struct ucred *cred, struct vnode *vp) 225 { 226 int error; 227 228 ASSERT_VOP_LOCKED(vp, "mac_check_system_swapon"); 229 230 if (!mac_enforce_system) 231 return (0); 232 233 MAC_CHECK(check_system_swapon, cred, vp, vp->v_label); 234 return (error); 235 } 236 237 int 238 mac_check_system_swapoff(struct ucred *cred, struct vnode *vp) 239 { 240 int error; 241 242 ASSERT_VOP_LOCKED(vp, "mac_check_system_swapoff"); 243 244 if (!mac_enforce_system) 245 return (0); 246 247 MAC_CHECK(check_system_swapoff, cred, vp, vp->v_label); 248 return (error); 249 } 250 251 int 252 mac_check_system_sysctl(struct ucred *cred, struct sysctl_oid *oidp, void *arg1, 253 int arg2, struct sysctl_req *req) 254 { 255 int error; 256 257 /* 258 * XXXMAC: We're very much like to assert the SYSCTL_LOCK here, 259 * but since it's not exported from kern_sysctl.c, we can't. 260 */ 261 if (!mac_enforce_system) 262 return (0); 263 264 MAC_CHECK(check_system_sysctl, cred, oidp, arg1, arg2, req); 265 266 return (error); 267 } 268