1 /*- 2 * Copyright (c) 1999-2002, 2006, 2009 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-2009 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 * This software was developed at the University of Cambridge Computer 21 * Laboratory with support from a grant from Google, Inc. 22 * 23 * Redistribution and use in source and binary forms, with or without 24 * modification, are permitted provided that the following conditions 25 * are met: 26 * 1. Redistributions of source code must retain the above copyright 27 * notice, this list of conditions and the following disclaimer. 28 * 2. Redistributions in binary form must reproduce the above copyright 29 * notice, this list of conditions and the following disclaimer in the 30 * documentation and/or other materials provided with the distribution. 31 * 32 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 35 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 42 * SUCH DAMAGE. 43 */ 44 45 /*- 46 * Framework for extensible kernel access control. This file contains core 47 * kernel infrastructure for the TrustedBSD MAC Framework, including policy 48 * registration, versioning, locking, error composition operator, and system 49 * calls. 50 * 51 * The MAC Framework implements three programming interfaces: 52 * 53 * - The kernel MAC interface, defined in mac_framework.h, and invoked 54 * throughout the kernel to request security decisions, notify of security 55 * related events, etc. 56 * 57 * - The MAC policy module interface, defined in mac_policy.h, which is 58 * implemented by MAC policy modules and invoked by the MAC Framework to 59 * forward kernel security requests and notifications to policy modules. 60 * 61 * - The user MAC API, defined in mac.h, which allows user programs to query 62 * and set label state on objects. 63 * 64 * The majority of the MAC Framework implementation may be found in 65 * src/sys/security/mac. Sample policy modules may be found in 66 * src/sys/security/mac_*. 67 */ 68 69 #include "opt_mac.h" 70 71 #include <sys/cdefs.h> 72 __FBSDID("$FreeBSD$"); 73 74 #include <sys/param.h> 75 #include <sys/systm.h> 76 #include <sys/condvar.h> 77 #include <sys/kernel.h> 78 #include <sys/lock.h> 79 #include <sys/mac.h> 80 #include <sys/module.h> 81 #include <sys/rmlock.h> 82 #include <sys/sdt.h> 83 #include <sys/sx.h> 84 #include <sys/sysctl.h> 85 86 #include <security/mac/mac_framework.h> 87 #include <security/mac/mac_internal.h> 88 #include <security/mac/mac_policy.h> 89 90 /* 91 * DTrace SDT providers for MAC. 92 */ 93 SDT_PROVIDER_DEFINE(mac); 94 SDT_PROVIDER_DEFINE(mac_framework); 95 96 SDT_PROBE_DEFINE2(mac, , policy, modevent, "int", 97 "struct mac_policy_conf *"); 98 SDT_PROBE_DEFINE1(mac, , policy, register, 99 "struct mac_policy_conf *"); 100 SDT_PROBE_DEFINE1(mac, , policy, unregister, 101 "struct mac_policy_conf *"); 102 103 /* 104 * Root sysctl node for all MAC and MAC policy controls. 105 */ 106 SYSCTL_NODE(_security, OID_AUTO, mac, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 107 "TrustedBSD MAC policy controls"); 108 109 /* 110 * Declare that the kernel provides MAC support, version 3 (FreeBSD 7.x). 111 * This permits modules to refuse to be loaded if the necessary support isn't 112 * present, even if it's pre-boot. 113 */ 114 MODULE_VERSION(kernel_mac_support, MAC_VERSION); 115 116 static unsigned int mac_version = MAC_VERSION; 117 SYSCTL_UINT(_security_mac, OID_AUTO, version, CTLFLAG_RD, &mac_version, 0, 118 ""); 119 120 /* 121 * Flags for inlined checks. Note this would be best hotpatched at runtime. 122 * The following is a band-aid. 123 * 124 * Use FPFLAG for hooks running in commonly executed paths and FPFLAG_RARE 125 * for the rest. 126 */ 127 #define FPFLAG(f) \ 128 bool __read_frequently mac_##f##_fp_flag 129 130 #define FPFLAG_RARE(f) \ 131 bool __read_mostly mac_##f##_fp_flag 132 133 FPFLAG(priv_check); 134 FPFLAG(priv_grant); 135 FPFLAG(vnode_check_lookup); 136 FPFLAG(vnode_check_open); 137 FPFLAG(vnode_check_stat); 138 FPFLAG(vnode_check_read); 139 FPFLAG(vnode_check_write); 140 FPFLAG(vnode_check_mmap); 141 FPFLAG_RARE(vnode_check_poll); 142 FPFLAG_RARE(vnode_check_rename_from); 143 FPFLAG_RARE(vnode_check_access); 144 FPFLAG_RARE(pipe_check_stat); 145 FPFLAG_RARE(pipe_check_poll); 146 147 #undef FPFLAG 148 #undef FPFLAG_RARE 149 150 /* 151 * Labels consist of a indexed set of "slots", which are allocated policies 152 * as required. The MAC Framework maintains a bitmask of slots allocated so 153 * far to prevent reuse. Slots cannot be reused, as the MAC Framework 154 * guarantees that newly allocated slots in labels will be NULL unless 155 * otherwise initialized, and because we do not have a mechanism to garbage 156 * collect slots on policy unload. As labeled policies tend to be statically 157 * loaded during boot, and not frequently unloaded and reloaded, this is not 158 * generally an issue. 159 */ 160 #if MAC_MAX_SLOTS > 32 161 #error "MAC_MAX_SLOTS too large" 162 #endif 163 164 static unsigned int mac_max_slots = MAC_MAX_SLOTS; 165 static unsigned int mac_slot_offsets_free = (1 << MAC_MAX_SLOTS) - 1; 166 SYSCTL_UINT(_security_mac, OID_AUTO, max_slots, CTLFLAG_RD, &mac_max_slots, 167 0, ""); 168 169 /* 170 * Has the kernel started generating labeled objects yet? All read/write 171 * access to this variable is serialized during the boot process. Following 172 * the end of serialization, we don't update this flag; no locking. 173 */ 174 static int mac_late = 0; 175 176 /* 177 * Each policy declares a mask of object types requiring labels to be 178 * allocated for them. For convenience, we combine and cache the bitwise or 179 * of the per-policy object flags to track whether we will allocate a label 180 * for an object type at run-time. 181 */ 182 uint64_t mac_labeled; 183 SYSCTL_UQUAD(_security_mac, OID_AUTO, labeled, CTLFLAG_RD, &mac_labeled, 0, 184 "Mask of object types being labeled"); 185 186 MALLOC_DEFINE(M_MACTEMP, "mactemp", "MAC temporary label storage"); 187 188 /* 189 * MAC policy modules are placed in one of two lists: mac_static_policy_list, 190 * for policies that are loaded early and cannot be unloaded, and 191 * mac_policy_list, which holds policies either loaded later in the boot 192 * cycle or that may be unloaded. The static policy list does not require 193 * locks to iterate over, but the dynamic list requires synchronization. 194 * Support for dynamic policy loading can be compiled out using the 195 * MAC_STATIC kernel option. 196 * 197 * The dynamic policy list is protected by two locks: modifying the list 198 * requires both locks to be held exclusively. One of the locks, 199 * mac_policy_rm, is acquired over policy entry points that will never sleep; 200 * the other, mac_policy_rms, is acquired over policy entry points that may 201 * sleep. The former category will be used when kernel locks may be held 202 * over calls to the MAC Framework, during network processing in ithreads, 203 * etc. The latter will tend to involve potentially blocking memory 204 * allocations, extended attribute I/O, etc. 205 */ 206 #ifndef MAC_STATIC 207 static struct rmlock mac_policy_rm; /* Non-sleeping entry points. */ 208 static struct rmslock mac_policy_rms; /* Sleeping entry points. */ 209 #endif 210 211 struct mac_policy_list_head mac_policy_list; 212 struct mac_policy_list_head mac_static_policy_list; 213 u_int mac_policy_count; /* Registered policy count. */ 214 215 static void mac_policy_xlock(void); 216 static void mac_policy_xlock_assert(void); 217 static void mac_policy_xunlock(void); 218 219 void 220 mac_policy_slock_nosleep(struct rm_priotracker *tracker) 221 { 222 223 #ifndef MAC_STATIC 224 if (!mac_late) 225 return; 226 227 rm_rlock(&mac_policy_rm, tracker); 228 #endif 229 } 230 231 void 232 mac_policy_slock_sleep(void) 233 { 234 235 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 236 "mac_policy_slock_sleep"); 237 238 #ifndef MAC_STATIC 239 if (!mac_late) 240 return; 241 242 rms_rlock(&mac_policy_rms); 243 #endif 244 } 245 246 void 247 mac_policy_sunlock_nosleep(struct rm_priotracker *tracker) 248 { 249 250 #ifndef MAC_STATIC 251 if (!mac_late) 252 return; 253 254 rm_runlock(&mac_policy_rm, tracker); 255 #endif 256 } 257 258 void 259 mac_policy_sunlock_sleep(void) 260 { 261 262 #ifndef MAC_STATIC 263 if (!mac_late) 264 return; 265 266 rms_runlock(&mac_policy_rms); 267 #endif 268 } 269 270 static void 271 mac_policy_xlock(void) 272 { 273 274 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 275 "mac_policy_xlock()"); 276 277 #ifndef MAC_STATIC 278 if (!mac_late) 279 return; 280 281 rms_wlock(&mac_policy_rms); 282 rm_wlock(&mac_policy_rm); 283 #endif 284 } 285 286 static void 287 mac_policy_xunlock(void) 288 { 289 290 #ifndef MAC_STATIC 291 if (!mac_late) 292 return; 293 294 rm_wunlock(&mac_policy_rm); 295 rms_wunlock(&mac_policy_rms); 296 #endif 297 } 298 299 static void 300 mac_policy_xlock_assert(void) 301 { 302 303 #ifndef MAC_STATIC 304 if (!mac_late) 305 return; 306 307 rm_assert(&mac_policy_rm, RA_WLOCKED); 308 #endif 309 } 310 311 /* 312 * Initialize the MAC subsystem, including appropriate SMP locks. 313 */ 314 static void 315 mac_init(void) 316 { 317 318 LIST_INIT(&mac_static_policy_list); 319 LIST_INIT(&mac_policy_list); 320 mac_labelzone_init(); 321 322 #ifndef MAC_STATIC 323 rm_init_flags(&mac_policy_rm, "mac_policy_rm", RM_NOWITNESS | 324 RM_RECURSE); 325 rms_init(&mac_policy_rms, "mac_policy_rms"); 326 #endif 327 } 328 329 /* 330 * For the purposes of modules that want to know if they were loaded "early", 331 * set the mac_late flag once we've processed modules either linked into the 332 * kernel, or loaded before the kernel startup. 333 */ 334 static void 335 mac_late_init(void) 336 { 337 338 mac_late = 1; 339 } 340 341 /* 342 * Given a policy, derive from its set of non-NULL label init methods what 343 * object types the policy is interested in. 344 */ 345 static uint64_t 346 mac_policy_getlabeled(struct mac_policy_conf *mpc) 347 { 348 uint64_t labeled; 349 350 #define MPC_FLAG(method, flag) \ 351 if (mpc->mpc_ops->mpo_ ## method != NULL) \ 352 labeled |= (flag); \ 353 354 labeled = 0; 355 MPC_FLAG(cred_init_label, MPC_OBJECT_CRED); 356 MPC_FLAG(proc_init_label, MPC_OBJECT_PROC); 357 MPC_FLAG(vnode_init_label, MPC_OBJECT_VNODE); 358 MPC_FLAG(inpcb_init_label, MPC_OBJECT_INPCB); 359 MPC_FLAG(socket_init_label, MPC_OBJECT_SOCKET); 360 MPC_FLAG(devfs_init_label, MPC_OBJECT_DEVFS); 361 MPC_FLAG(mbuf_init_label, MPC_OBJECT_MBUF); 362 MPC_FLAG(ipq_init_label, MPC_OBJECT_IPQ); 363 MPC_FLAG(ifnet_init_label, MPC_OBJECT_IFNET); 364 MPC_FLAG(bpfdesc_init_label, MPC_OBJECT_BPFDESC); 365 MPC_FLAG(pipe_init_label, MPC_OBJECT_PIPE); 366 MPC_FLAG(mount_init_label, MPC_OBJECT_MOUNT); 367 MPC_FLAG(posixsem_init_label, MPC_OBJECT_POSIXSEM); 368 MPC_FLAG(posixshm_init_label, MPC_OBJECT_POSIXSHM); 369 MPC_FLAG(sysvmsg_init_label, MPC_OBJECT_SYSVMSG); 370 MPC_FLAG(sysvmsq_init_label, MPC_OBJECT_SYSVMSQ); 371 MPC_FLAG(sysvsem_init_label, MPC_OBJECT_SYSVSEM); 372 MPC_FLAG(sysvshm_init_label, MPC_OBJECT_SYSVSHM); 373 MPC_FLAG(syncache_init_label, MPC_OBJECT_SYNCACHE); 374 MPC_FLAG(ip6q_init_label, MPC_OBJECT_IP6Q); 375 376 #undef MPC_FLAG 377 return (labeled); 378 } 379 380 /* 381 * When policies are loaded or unloaded, walk the list of registered policies 382 * and built mac_labeled, a bitmask representing the union of all objects 383 * requiring labels across all policies. 384 */ 385 static void 386 mac_policy_update(void) 387 { 388 struct mac_policy_conf *mpc; 389 390 mac_policy_xlock_assert(); 391 392 mac_labeled = 0; 393 mac_policy_count = 0; 394 LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { 395 mac_labeled |= mac_policy_getlabeled(mpc); 396 mac_policy_count++; 397 } 398 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { 399 mac_labeled |= mac_policy_getlabeled(mpc); 400 mac_policy_count++; 401 } 402 } 403 404 /* 405 * There are frequently used code paths which check for rarely installed 406 * policies. Gross hack below enables doing it in a cheap manner. 407 */ 408 409 #define FPO(f) (offsetof(struct mac_policy_ops, mpo_##f) / sizeof(uintptr_t)) 410 411 struct mac_policy_fastpath_elem { 412 int count; 413 bool *flag; 414 size_t offset; 415 }; 416 417 struct mac_policy_fastpath_elem mac_policy_fastpath_array[] = { 418 { .offset = FPO(priv_check), .flag = &mac_priv_check_fp_flag }, 419 { .offset = FPO(priv_grant), .flag = &mac_priv_grant_fp_flag }, 420 { .offset = FPO(vnode_check_lookup), 421 .flag = &mac_vnode_check_lookup_fp_flag }, 422 { .offset = FPO(vnode_check_open), 423 .flag = &mac_vnode_check_open_fp_flag }, 424 { .offset = FPO(vnode_check_stat), 425 .flag = &mac_vnode_check_stat_fp_flag }, 426 { .offset = FPO(vnode_check_read), 427 .flag = &mac_vnode_check_read_fp_flag }, 428 { .offset = FPO(vnode_check_write), 429 .flag = &mac_vnode_check_write_fp_flag }, 430 { .offset = FPO(vnode_check_mmap), 431 .flag = &mac_vnode_check_mmap_fp_flag }, 432 { .offset = FPO(vnode_check_poll), 433 .flag = &mac_vnode_check_poll_fp_flag }, 434 { .offset = FPO(vnode_check_rename_from), 435 .flag = &mac_vnode_check_rename_from_fp_flag }, 436 { .offset = FPO(vnode_check_access), 437 .flag = &mac_vnode_check_access_fp_flag }, 438 { .offset = FPO(pipe_check_stat), 439 .flag = &mac_pipe_check_stat_fp_flag }, 440 { .offset = FPO(pipe_check_poll), 441 .flag = &mac_pipe_check_poll_fp_flag }, 442 }; 443 444 static void 445 mac_policy_fastpath_enable(struct mac_policy_fastpath_elem *mpfe) 446 { 447 448 MPASS(mpfe->count >= 0); 449 mpfe->count++; 450 if (mpfe->count == 1) { 451 MPASS(*mpfe->flag == false); 452 *mpfe->flag = true; 453 } 454 } 455 456 static void 457 mac_policy_fastpath_disable(struct mac_policy_fastpath_elem *mpfe) 458 { 459 460 MPASS(mpfe->count >= 1); 461 mpfe->count--; 462 if (mpfe->count == 0) { 463 MPASS(*mpfe->flag == true); 464 *mpfe->flag = false; 465 } 466 } 467 468 static void 469 mac_policy_fastpath_register(struct mac_policy_conf *mpc) 470 { 471 struct mac_policy_fastpath_elem *mpfe; 472 uintptr_t **ops; 473 int i; 474 475 mac_policy_xlock_assert(); 476 477 ops = (uintptr_t **)mpc->mpc_ops; 478 for (i = 0; i < nitems(mac_policy_fastpath_array); i++) { 479 mpfe = &mac_policy_fastpath_array[i]; 480 if (ops[mpfe->offset] != NULL) 481 mac_policy_fastpath_enable(mpfe); 482 } 483 } 484 485 static void 486 mac_policy_fastpath_unregister(struct mac_policy_conf *mpc) 487 { 488 struct mac_policy_fastpath_elem *mpfe; 489 uintptr_t **ops; 490 int i; 491 492 mac_policy_xlock_assert(); 493 494 ops = (uintptr_t **)mpc->mpc_ops; 495 for (i = 0; i < nitems(mac_policy_fastpath_array); i++) { 496 mpfe = &mac_policy_fastpath_array[i]; 497 if (ops[mpfe->offset] != NULL) 498 mac_policy_fastpath_disable(mpfe); 499 } 500 } 501 502 #undef FPO 503 504 static int 505 mac_policy_register(struct mac_policy_conf *mpc) 506 { 507 struct mac_policy_conf *tmpc; 508 int error, slot, static_entry; 509 510 error = 0; 511 512 /* 513 * We don't technically need exclusive access while !mac_late, but 514 * hold it for assertion consistency. 515 */ 516 mac_policy_xlock(); 517 518 /* 519 * If the module can potentially be unloaded, or we're loading late, 520 * we have to stick it in the non-static list and pay an extra 521 * performance overhead. Otherwise, we can pay a light locking cost 522 * and stick it in the static list. 523 */ 524 static_entry = (!mac_late && 525 !(mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_UNLOADOK)); 526 527 if (static_entry) { 528 LIST_FOREACH(tmpc, &mac_static_policy_list, mpc_list) { 529 if (strcmp(tmpc->mpc_name, mpc->mpc_name) == 0) { 530 error = EEXIST; 531 goto out; 532 } 533 } 534 } else { 535 LIST_FOREACH(tmpc, &mac_policy_list, mpc_list) { 536 if (strcmp(tmpc->mpc_name, mpc->mpc_name) == 0) { 537 error = EEXIST; 538 goto out; 539 } 540 } 541 } 542 if (mpc->mpc_field_off != NULL) { 543 slot = ffs(mac_slot_offsets_free); 544 if (slot == 0) { 545 error = ENOMEM; 546 goto out; 547 } 548 slot--; 549 mac_slot_offsets_free &= ~(1 << slot); 550 *mpc->mpc_field_off = slot; 551 } 552 mpc->mpc_runtime_flags |= MPC_RUNTIME_FLAG_REGISTERED; 553 554 /* 555 * If we're loading a MAC module after the framework has initialized, 556 * it has to go into the dynamic list. If we're loading it before 557 * we've finished initializing, it can go into the static list with 558 * weaker locker requirements. 559 */ 560 if (static_entry) 561 LIST_INSERT_HEAD(&mac_static_policy_list, mpc, mpc_list); 562 else 563 LIST_INSERT_HEAD(&mac_policy_list, mpc, mpc_list); 564 565 /* 566 * Per-policy initialization. Currently, this takes place under the 567 * exclusive lock, so policies must not sleep in their init method. 568 * In the future, we may want to separate "init" from "start", with 569 * "init" occurring without the lock held. Likewise, on tear-down, 570 * breaking out "stop" from "destroy". 571 */ 572 if (mpc->mpc_ops->mpo_init != NULL) 573 (*(mpc->mpc_ops->mpo_init))(mpc); 574 575 mac_policy_fastpath_register(mpc); 576 577 mac_policy_update(); 578 579 SDT_PROBE1(mac, , policy, register, mpc); 580 printf("Security policy loaded: %s (%s)\n", mpc->mpc_fullname, 581 mpc->mpc_name); 582 583 out: 584 mac_policy_xunlock(); 585 return (error); 586 } 587 588 static int 589 mac_policy_unregister(struct mac_policy_conf *mpc) 590 { 591 592 /* 593 * If we fail the load, we may get a request to unload. Check to see 594 * if we did the run-time registration, and if not, silently succeed. 595 */ 596 mac_policy_xlock(); 597 if ((mpc->mpc_runtime_flags & MPC_RUNTIME_FLAG_REGISTERED) == 0) { 598 mac_policy_xunlock(); 599 return (0); 600 } 601 #if 0 602 /* 603 * Don't allow unloading modules with private data. 604 */ 605 if (mpc->mpc_field_off != NULL) { 606 mac_policy_xunlock(); 607 return (EBUSY); 608 } 609 #endif 610 /* 611 * Only allow the unload to proceed if the module is unloadable by 612 * its own definition. 613 */ 614 if ((mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_UNLOADOK) == 0) { 615 mac_policy_xunlock(); 616 return (EBUSY); 617 } 618 619 mac_policy_fastpath_unregister(mpc); 620 621 if (mpc->mpc_ops->mpo_destroy != NULL) 622 (*(mpc->mpc_ops->mpo_destroy))(mpc); 623 624 LIST_REMOVE(mpc, mpc_list); 625 mpc->mpc_runtime_flags &= ~MPC_RUNTIME_FLAG_REGISTERED; 626 mac_policy_update(); 627 mac_policy_xunlock(); 628 629 SDT_PROBE1(mac, , policy, unregister, mpc); 630 printf("Security policy unload: %s (%s)\n", mpc->mpc_fullname, 631 mpc->mpc_name); 632 633 return (0); 634 } 635 636 /* 637 * Allow MAC policy modules to register during boot, etc. 638 */ 639 int 640 mac_policy_modevent(module_t mod, int type, void *data) 641 { 642 struct mac_policy_conf *mpc; 643 int error; 644 645 error = 0; 646 mpc = (struct mac_policy_conf *) data; 647 648 #ifdef MAC_STATIC 649 if (mac_late) { 650 printf("mac_policy_modevent: MAC_STATIC and late\n"); 651 return (EBUSY); 652 } 653 #endif 654 655 SDT_PROBE2(mac, , policy, modevent, type, mpc); 656 switch (type) { 657 case MOD_LOAD: 658 if (mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_NOTLATE && 659 mac_late) { 660 printf("mac_policy_modevent: can't load %s policy " 661 "after booting\n", mpc->mpc_name); 662 error = EBUSY; 663 break; 664 } 665 error = mac_policy_register(mpc); 666 break; 667 case MOD_UNLOAD: 668 /* Don't unregister the module if it was never registered. */ 669 if ((mpc->mpc_runtime_flags & MPC_RUNTIME_FLAG_REGISTERED) 670 != 0) 671 error = mac_policy_unregister(mpc); 672 else 673 error = 0; 674 break; 675 default: 676 error = EOPNOTSUPP; 677 break; 678 } 679 680 return (error); 681 } 682 683 /* 684 * Define an error value precedence, and given two arguments, selects the 685 * value with the higher precedence. 686 */ 687 int 688 mac_error_select(int error1, int error2) 689 { 690 691 /* Certain decision-making errors take top priority. */ 692 if (error1 == EDEADLK || error2 == EDEADLK) 693 return (EDEADLK); 694 695 /* Invalid arguments should be reported where possible. */ 696 if (error1 == EINVAL || error2 == EINVAL) 697 return (EINVAL); 698 699 /* Precedence goes to "visibility", with both process and file. */ 700 if (error1 == ESRCH || error2 == ESRCH) 701 return (ESRCH); 702 703 if (error1 == ENOENT || error2 == ENOENT) 704 return (ENOENT); 705 706 /* Precedence goes to DAC/MAC protections. */ 707 if (error1 == EACCES || error2 == EACCES) 708 return (EACCES); 709 710 /* Precedence goes to privilege. */ 711 if (error1 == EPERM || error2 == EPERM) 712 return (EPERM); 713 714 /* Precedence goes to error over success; otherwise, arbitrary. */ 715 if (error1 != 0) 716 return (error1); 717 return (error2); 718 } 719 720 int 721 mac_check_structmac_consistent(struct mac *mac) 722 { 723 724 /* Require that labels have a non-zero length. */ 725 if (mac->m_buflen > MAC_MAX_LABEL_BUF_LEN || 726 mac->m_buflen <= sizeof("")) 727 return (EINVAL); 728 729 return (0); 730 } 731 732 SYSINIT(mac, SI_SUB_MAC, SI_ORDER_FIRST, mac_init, NULL); 733 SYSINIT(mac_late, SI_SUB_MAC_LATE, SI_ORDER_FIRST, mac_late_init, NULL); 734