1 /*- 2 * Copyright (c) 1999-2002, 2006 Robert N. M. Watson 3 * Copyright (c) 2001 Ilmar S. Habibulin 4 * Copyright (c) 2001-2005 Networks Associates Technology, Inc. 5 * Copyright (c) 2005-2006 SPARTA, Inc. 6 * Copyright (c) 2008 Apple Inc. 7 * All rights reserved. 8 * 9 * This software was developed by Robert Watson and Ilmar Habibulin for the 10 * TrustedBSD Project. 11 * 12 * This software was developed for the FreeBSD Project in part by Network 13 * Associates Laboratories, the Security Research Division of Network 14 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), 15 * as part of the DARPA CHATS research program. 16 * 17 * This software was enhanced by SPARTA ISSO under SPAWAR contract 18 * N66001-04-C-6019 ("SEFOS"). 19 * 20 * Redistribution and use in source and binary forms, with or without 21 * modification, are permitted provided that the following conditions 22 * are met: 23 * 1. Redistributions of source code must retain the above copyright 24 * notice, this list of conditions and the following disclaimer. 25 * 2. Redistributions in binary form must reproduce the above copyright 26 * notice, this list of conditions and the following disclaimer in the 27 * documentation and/or other materials provided with the distribution. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39 * SUCH DAMAGE. 40 */ 41 42 /*- 43 * Framework for extensible kernel access control. This file contains core 44 * kernel infrastructure for the TrustedBSD MAC Framework, including policy 45 * registration, versioning, locking, error composition operator, and system 46 * calls. 47 * 48 * The MAC Framework implements three programming interfaces: 49 * 50 * - The kernel MAC interface, defined in mac_framework.h, and invoked 51 * throughout the kernel to request security decisions, notify of security 52 * related events, etc. 53 * 54 * - The MAC policy module interface, defined in mac_policy.h, which is 55 * implemented by MAC policy modules and invoked by the MAC Framework to 56 * forward kernel security requests and notifications to policy modules. 57 * 58 * - The user MAC API, defined in mac.h, which allows user programs to query 59 * and set label state on objects. 60 * 61 * The majority of the MAC Framework implementation may be found in 62 * src/sys/security/mac. Sample policy modules may be found in 63 * src/sys/security/mac_*. 64 */ 65 66 #include "opt_mac.h" 67 68 #include <sys/cdefs.h> 69 __FBSDID("$FreeBSD$"); 70 71 #include <sys/param.h> 72 #include <sys/condvar.h> 73 #include <sys/kernel.h> 74 #include <sys/lock.h> 75 #include <sys/mutex.h> 76 #include <sys/mac.h> 77 #include <sys/module.h> 78 #include <sys/systm.h> 79 #include <sys/sysctl.h> 80 81 #include <security/mac/mac_framework.h> 82 #include <security/mac/mac_internal.h> 83 #include <security/mac/mac_policy.h> 84 85 /* 86 * Root sysctl node for all MAC and MAC policy controls. 87 */ 88 SYSCTL_NODE(_security, OID_AUTO, mac, CTLFLAG_RW, 0, 89 "TrustedBSD MAC policy controls"); 90 91 /* 92 * Declare that the kernel provides MAC support, version 3 (FreeBSD 7.x). 93 * This permits modules to refuse to be loaded if the necessary support isn't 94 * present, even if it's pre-boot. 95 */ 96 MODULE_VERSION(kernel_mac_support, MAC_VERSION); 97 98 static unsigned int mac_version = MAC_VERSION; 99 SYSCTL_UINT(_security_mac, OID_AUTO, version, CTLFLAG_RD, &mac_version, 0, 100 ""); 101 102 /* 103 * Labels consist of a indexed set of "slots", which are allocated policies 104 * as required. The MAC Framework maintains a bitmask of slots allocated so 105 * far to prevent reuse. Slots cannot be reused, as the MAC Framework 106 * guarantees that newly allocated slots in labels will be NULL unless 107 * otherwise initialized, and because we do not have a mechanism to garbage 108 * collect slots on policy unload. As labeled policies tend to be statically 109 * loaded during boot, and not frequently unloaded and reloaded, this is not 110 * generally an issue. 111 */ 112 #if MAC_MAX_SLOTS > 32 113 #error "MAC_MAX_SLOTS too large" 114 #endif 115 116 static unsigned int mac_max_slots = MAC_MAX_SLOTS; 117 static unsigned int mac_slot_offsets_free = (1 << MAC_MAX_SLOTS) - 1; 118 SYSCTL_UINT(_security_mac, OID_AUTO, max_slots, CTLFLAG_RD, &mac_max_slots, 119 0, ""); 120 121 /* 122 * Has the kernel started generating labeled objects yet? All read/write 123 * access to this variable is serialized during the boot process. Following 124 * the end of serialization, we don't update this flag; no locking. 125 */ 126 static int mac_late = 0; 127 128 /* 129 * Each policy declares a mask of object types requiring labels to be 130 * allocated for them. For convenience, we combine and cache the bitwise or 131 * of the per-policy object flags to track whether we will allocate a label 132 * for an object type at run-time. 133 */ 134 uint64_t mac_labeled; 135 SYSCTL_QUAD(_security_mac, OID_AUTO, labeled, CTLFLAG_RD, &mac_labeled, 0, 136 "Mask of object types being labeled"); 137 138 MALLOC_DEFINE(M_MACTEMP, "mactemp", "MAC temporary label storage"); 139 140 /* 141 * mac_static_policy_list holds a list of policy modules that are not loaded 142 * while the system is "live", and cannot be unloaded. These policies can be 143 * invoked without holding the busy count. 144 * 145 * mac_policy_list stores the list of dynamic policies. A busy count is 146 * maintained for the list, stored in mac_policy_busy. The busy count is 147 * protected by mac_policy_mtx; the list may be modified only while the busy 148 * count is 0, requiring that the lock be held to prevent new references to 149 * the list from being acquired. For almost all operations, incrementing the 150 * busy count is sufficient to guarantee consistency, as the list cannot be 151 * modified while the busy count is elevated. For a few special operations 152 * involving a change to the list of active policies, the mtx itself must be 153 * held. A condition variable, mac_policy_cv, is used to signal potential 154 * exclusive consumers that they should try to acquire the lock if a first 155 * attempt at exclusive access fails. 156 * 157 * This design intentionally avoids fairness, and may starve attempts to 158 * acquire an exclusive lock on a busy system. This is required because we 159 * do not ever want acquiring a read reference to perform an unbounded length 160 * sleep. Read references are acquired in ithreads, network isrs, etc, and 161 * any unbounded blocking could lead quickly to deadlock. 162 * 163 * Another reason for never blocking on read references is that the MAC 164 * Framework may recurse: if a policy calls a VOP, for example, this might 165 * lead to vnode life cycle operations (such as init/destroy). 166 * 167 * If the kernel option MAC_STATIC has been compiled in, all locking becomes 168 * a no-op, and the global list of policies is not allowed to change after 169 * early boot. 170 * 171 * XXXRW: Currently, we signal mac_policy_cv every time the framework becomes 172 * unbusy and there is a thread waiting to enter it exclusively. Since it 173 * may take some time before the thread runs, we may issue a lot of signals. 174 * We should instead keep track of the fact that we've signalled, taking into 175 * account that the framework may be busy again by the time the thread runs, 176 * requiring us to re-signal. 177 */ 178 #ifndef MAC_STATIC 179 static struct mtx mac_policy_mtx; 180 static struct cv mac_policy_cv; 181 static int mac_policy_count; 182 static int mac_policy_wait; 183 #endif 184 struct mac_policy_list_head mac_policy_list; 185 struct mac_policy_list_head mac_static_policy_list; 186 187 /* 188 * We manually invoke WITNESS_WARN() to allow Witness to generate warnings 189 * even if we don't end up ever triggering the wait at run-time. The 190 * consumer of the exclusive interface must not hold any locks (other than 191 * potentially Giant) since we may sleep for long (potentially indefinite) 192 * periods of time waiting for the framework to become quiescent so that a 193 * policy list change may be made. 194 */ 195 void 196 mac_policy_grab_exclusive(void) 197 { 198 199 #ifndef MAC_STATIC 200 if (!mac_late) 201 return; 202 203 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 204 "mac_policy_grab_exclusive() at %s:%d", __FILE__, __LINE__); 205 mtx_lock(&mac_policy_mtx); 206 while (mac_policy_count != 0) { 207 mac_policy_wait++; 208 cv_wait(&mac_policy_cv, &mac_policy_mtx); 209 mac_policy_wait--; 210 } 211 #endif 212 } 213 214 void 215 mac_policy_assert_exclusive(void) 216 { 217 218 #ifndef MAC_STATIC 219 if (!mac_late) 220 return; 221 222 mtx_assert(&mac_policy_mtx, MA_OWNED); 223 KASSERT(mac_policy_count == 0, 224 ("mac_policy_assert_exclusive(): not exclusive")); 225 #endif 226 } 227 228 void 229 mac_policy_release_exclusive(void) 230 { 231 #ifndef MAC_STATIC 232 int dowakeup; 233 234 if (!mac_late) 235 return; 236 237 KASSERT(mac_policy_count == 0, 238 ("mac_policy_release_exclusive(): not exclusive")); 239 dowakeup = (mac_policy_wait != 0); 240 mtx_unlock(&mac_policy_mtx); 241 if (dowakeup) 242 cv_signal(&mac_policy_cv); 243 #endif 244 } 245 246 void 247 mac_policy_list_busy(void) 248 { 249 250 #ifndef MAC_STATIC 251 if (!mac_late) 252 return; 253 254 mtx_lock(&mac_policy_mtx); 255 mac_policy_count++; 256 mtx_unlock(&mac_policy_mtx); 257 #endif 258 } 259 260 int 261 mac_policy_list_conditional_busy(void) 262 { 263 #ifndef MAC_STATIC 264 int ret; 265 266 if (!mac_late) 267 return (1); 268 269 mtx_lock(&mac_policy_mtx); 270 if (!LIST_EMPTY(&mac_policy_list)) { 271 mac_policy_count++; 272 ret = 1; 273 } else 274 ret = 0; 275 mtx_unlock(&mac_policy_mtx); 276 return (ret); 277 #else 278 return (1); 279 #endif 280 } 281 282 void 283 mac_policy_list_unbusy(void) 284 { 285 #ifndef MAC_STATIC 286 int dowakeup; 287 288 if (!mac_late) 289 return; 290 291 mtx_lock(&mac_policy_mtx); 292 mac_policy_count--; 293 KASSERT(mac_policy_count >= 0, ("MAC_POLICY_LIST_LOCK")); 294 dowakeup = (mac_policy_count == 0 && mac_policy_wait != 0); 295 mtx_unlock(&mac_policy_mtx); 296 297 if (dowakeup) 298 cv_signal(&mac_policy_cv); 299 #endif 300 } 301 302 /* 303 * Initialize the MAC subsystem, including appropriate SMP locks. 304 */ 305 static void 306 mac_init(void) 307 { 308 309 LIST_INIT(&mac_static_policy_list); 310 LIST_INIT(&mac_policy_list); 311 mac_labelzone_init(); 312 313 #ifndef MAC_STATIC 314 mtx_init(&mac_policy_mtx, "mac_policy_mtx", NULL, MTX_DEF); 315 cv_init(&mac_policy_cv, "mac_policy_cv"); 316 #endif 317 } 318 319 /* 320 * For the purposes of modules that want to know if they were loaded "early", 321 * set the mac_late flag once we've processed modules either linked into the 322 * kernel, or loaded before the kernel startup. 323 */ 324 static void 325 mac_late_init(void) 326 { 327 328 mac_late = 1; 329 } 330 331 /* 332 * After the policy list has changed, walk the list to update any global 333 * flags. Currently, we support only one flag, and it's conditionally 334 * defined; as a result, the entire function is conditional. Eventually, the 335 * #else case might also iterate across the policies. 336 */ 337 static void 338 mac_policy_updateflags(void) 339 { 340 struct mac_policy_conf *mpc; 341 342 mac_policy_assert_exclusive(); 343 344 mac_labeled = 0; 345 LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) 346 mac_labeled |= mpc->mpc_labeled; 347 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) 348 mac_labeled |= mpc->mpc_labeled; 349 } 350 351 static int 352 mac_policy_register(struct mac_policy_conf *mpc) 353 { 354 struct mac_policy_conf *tmpc; 355 int error, slot, static_entry; 356 357 error = 0; 358 359 /* 360 * We don't technically need exclusive access while !mac_late, but 361 * hold it for assertion consistency. 362 */ 363 mac_policy_grab_exclusive(); 364 365 /* 366 * If the module can potentially be unloaded, or we're loading late, 367 * we have to stick it in the non-static list and pay an extra 368 * performance overhead. Otherwise, we can pay a light locking cost 369 * and stick it in the static list. 370 */ 371 static_entry = (!mac_late && 372 !(mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_UNLOADOK)); 373 374 if (static_entry) { 375 LIST_FOREACH(tmpc, &mac_static_policy_list, mpc_list) { 376 if (strcmp(tmpc->mpc_name, mpc->mpc_name) == 0) { 377 error = EEXIST; 378 goto out; 379 } 380 } 381 } else { 382 LIST_FOREACH(tmpc, &mac_policy_list, mpc_list) { 383 if (strcmp(tmpc->mpc_name, mpc->mpc_name) == 0) { 384 error = EEXIST; 385 goto out; 386 } 387 } 388 } 389 if (mpc->mpc_field_off != NULL) { 390 slot = ffs(mac_slot_offsets_free); 391 if (slot == 0) { 392 error = ENOMEM; 393 goto out; 394 } 395 slot--; 396 mac_slot_offsets_free &= ~(1 << slot); 397 *mpc->mpc_field_off = slot; 398 } 399 mpc->mpc_runtime_flags |= MPC_RUNTIME_FLAG_REGISTERED; 400 401 /* 402 * If we're loading a MAC module after the framework has initialized, 403 * it has to go into the dynamic list. If we're loading it before 404 * we've finished initializing, it can go into the static list with 405 * weaker locker requirements. 406 */ 407 if (static_entry) 408 LIST_INSERT_HEAD(&mac_static_policy_list, mpc, mpc_list); 409 else 410 LIST_INSERT_HEAD(&mac_policy_list, mpc, mpc_list); 411 412 /* 413 * Per-policy initialization. Currently, this takes place under the 414 * exclusive lock, so policies must not sleep in their init method. 415 * In the future, we may want to separate "init" from "start", with 416 * "init" occuring without the lock held. Likewise, on tear-down, 417 * breaking out "stop" from "destroy". 418 */ 419 if (mpc->mpc_ops->mpo_init != NULL) 420 (*(mpc->mpc_ops->mpo_init))(mpc); 421 mac_policy_updateflags(); 422 423 printf("Security policy loaded: %s (%s)\n", mpc->mpc_fullname, 424 mpc->mpc_name); 425 426 out: 427 mac_policy_release_exclusive(); 428 return (error); 429 } 430 431 static int 432 mac_policy_unregister(struct mac_policy_conf *mpc) 433 { 434 435 /* 436 * If we fail the load, we may get a request to unload. Check to see 437 * if we did the run-time registration, and if not, silently succeed. 438 */ 439 mac_policy_grab_exclusive(); 440 if ((mpc->mpc_runtime_flags & MPC_RUNTIME_FLAG_REGISTERED) == 0) { 441 mac_policy_release_exclusive(); 442 return (0); 443 } 444 #if 0 445 /* 446 * Don't allow unloading modules with private data. 447 */ 448 if (mpc->mpc_field_off != NULL) { 449 MAC_POLICY_LIST_UNLOCK(); 450 return (EBUSY); 451 } 452 #endif 453 /* 454 * Only allow the unload to proceed if the module is unloadable by 455 * its own definition. 456 */ 457 if ((mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_UNLOADOK) == 0) { 458 mac_policy_release_exclusive(); 459 return (EBUSY); 460 } 461 if (mpc->mpc_ops->mpo_destroy != NULL) 462 (*(mpc->mpc_ops->mpo_destroy))(mpc); 463 464 LIST_REMOVE(mpc, mpc_list); 465 mpc->mpc_runtime_flags &= ~MPC_RUNTIME_FLAG_REGISTERED; 466 mac_policy_updateflags(); 467 468 mac_policy_release_exclusive(); 469 470 printf("Security policy unload: %s (%s)\n", mpc->mpc_fullname, 471 mpc->mpc_name); 472 473 return (0); 474 } 475 476 /* 477 * Allow MAC policy modules to register during boot, etc. 478 */ 479 int 480 mac_policy_modevent(module_t mod, int type, void *data) 481 { 482 struct mac_policy_conf *mpc; 483 int error; 484 485 error = 0; 486 mpc = (struct mac_policy_conf *) data; 487 488 #ifdef MAC_STATIC 489 if (mac_late) { 490 printf("mac_policy_modevent: MAC_STATIC and late\n"); 491 return (EBUSY); 492 } 493 #endif 494 495 switch (type) { 496 case MOD_LOAD: 497 if (mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_NOTLATE && 498 mac_late) { 499 printf("mac_policy_modevent: can't load %s policy " 500 "after booting\n", mpc->mpc_name); 501 error = EBUSY; 502 break; 503 } 504 error = mac_policy_register(mpc); 505 break; 506 case MOD_UNLOAD: 507 /* Don't unregister the module if it was never registered. */ 508 if ((mpc->mpc_runtime_flags & MPC_RUNTIME_FLAG_REGISTERED) 509 != 0) 510 error = mac_policy_unregister(mpc); 511 else 512 error = 0; 513 break; 514 default: 515 error = EOPNOTSUPP; 516 break; 517 } 518 519 return (error); 520 } 521 522 /* 523 * Define an error value precedence, and given two arguments, selects the 524 * value with the higher precedence. 525 */ 526 int 527 mac_error_select(int error1, int error2) 528 { 529 530 /* Certain decision-making errors take top priority. */ 531 if (error1 == EDEADLK || error2 == EDEADLK) 532 return (EDEADLK); 533 534 /* Invalid arguments should be reported where possible. */ 535 if (error1 == EINVAL || error2 == EINVAL) 536 return (EINVAL); 537 538 /* Precedence goes to "visibility", with both process and file. */ 539 if (error1 == ESRCH || error2 == ESRCH) 540 return (ESRCH); 541 542 if (error1 == ENOENT || error2 == ENOENT) 543 return (ENOENT); 544 545 /* Precedence goes to DAC/MAC protections. */ 546 if (error1 == EACCES || error2 == EACCES) 547 return (EACCES); 548 549 /* Precedence goes to privilege. */ 550 if (error1 == EPERM || error2 == EPERM) 551 return (EPERM); 552 553 /* Precedence goes to error over success; otherwise, arbitrary. */ 554 if (error1 != 0) 555 return (error1); 556 return (error2); 557 } 558 559 int 560 mac_check_structmac_consistent(struct mac *mac) 561 { 562 563 if (mac->m_buflen < 0 || 564 mac->m_buflen > MAC_MAX_LABEL_BUF_LEN) 565 return (EINVAL); 566 567 return (0); 568 } 569 570 SYSINIT(mac, SI_SUB_MAC, SI_ORDER_FIRST, mac_init, NULL); 571 SYSINIT(mac_late, SI_SUB_MAC_LATE, SI_ORDER_FIRST, mac_late_init, NULL); 572