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 145 #undef FPFLAG 146 #undef FPFLAG_RARE 147 148 /* 149 * Labels consist of a indexed set of "slots", which are allocated policies 150 * as required. The MAC Framework maintains a bitmask of slots allocated so 151 * far to prevent reuse. Slots cannot be reused, as the MAC Framework 152 * guarantees that newly allocated slots in labels will be NULL unless 153 * otherwise initialized, and because we do not have a mechanism to garbage 154 * collect slots on policy unload. As labeled policies tend to be statically 155 * loaded during boot, and not frequently unloaded and reloaded, this is not 156 * generally an issue. 157 */ 158 #if MAC_MAX_SLOTS > 32 159 #error "MAC_MAX_SLOTS too large" 160 #endif 161 162 static unsigned int mac_max_slots = MAC_MAX_SLOTS; 163 static unsigned int mac_slot_offsets_free = (1 << MAC_MAX_SLOTS) - 1; 164 SYSCTL_UINT(_security_mac, OID_AUTO, max_slots, CTLFLAG_RD, &mac_max_slots, 165 0, ""); 166 167 /* 168 * Has the kernel started generating labeled objects yet? All read/write 169 * access to this variable is serialized during the boot process. Following 170 * the end of serialization, we don't update this flag; no locking. 171 */ 172 static int mac_late = 0; 173 174 /* 175 * Each policy declares a mask of object types requiring labels to be 176 * allocated for them. For convenience, we combine and cache the bitwise or 177 * of the per-policy object flags to track whether we will allocate a label 178 * for an object type at run-time. 179 */ 180 uint64_t mac_labeled; 181 SYSCTL_UQUAD(_security_mac, OID_AUTO, labeled, CTLFLAG_RD, &mac_labeled, 0, 182 "Mask of object types being labeled"); 183 184 MALLOC_DEFINE(M_MACTEMP, "mactemp", "MAC temporary label storage"); 185 186 /* 187 * MAC policy modules are placed in one of two lists: mac_static_policy_list, 188 * for policies that are loaded early and cannot be unloaded, and 189 * mac_policy_list, which holds policies either loaded later in the boot 190 * cycle or that may be unloaded. The static policy list does not require 191 * locks to iterate over, but the dynamic list requires synchronization. 192 * Support for dynamic policy loading can be compiled out using the 193 * MAC_STATIC kernel option. 194 * 195 * The dynamic policy list is protected by two locks: modifying the list 196 * requires both locks to be held exclusively. One of the locks, 197 * mac_policy_rm, is acquired over policy entry points that will never sleep; 198 * the other, mac_policy_rms, is acquired over policy entry points that may 199 * sleep. The former category will be used when kernel locks may be held 200 * over calls to the MAC Framework, during network processing in ithreads, 201 * etc. The latter will tend to involve potentially blocking memory 202 * allocations, extended attribute I/O, etc. 203 */ 204 #ifndef MAC_STATIC 205 static struct rmlock mac_policy_rm; /* Non-sleeping entry points. */ 206 static struct rmslock mac_policy_rms; /* Sleeping entry points. */ 207 #endif 208 209 struct mac_policy_list_head mac_policy_list; 210 struct mac_policy_list_head mac_static_policy_list; 211 u_int mac_policy_count; /* Registered policy count. */ 212 213 static void mac_policy_xlock(void); 214 static void mac_policy_xlock_assert(void); 215 static void mac_policy_xunlock(void); 216 217 void 218 mac_policy_slock_nosleep(struct rm_priotracker *tracker) 219 { 220 221 #ifndef MAC_STATIC 222 if (!mac_late) 223 return; 224 225 rm_rlock(&mac_policy_rm, tracker); 226 #endif 227 } 228 229 void 230 mac_policy_slock_sleep(void) 231 { 232 233 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 234 "mac_policy_slock_sleep"); 235 236 #ifndef MAC_STATIC 237 if (!mac_late) 238 return; 239 240 rms_rlock(&mac_policy_rms); 241 #endif 242 } 243 244 void 245 mac_policy_sunlock_nosleep(struct rm_priotracker *tracker) 246 { 247 248 #ifndef MAC_STATIC 249 if (!mac_late) 250 return; 251 252 rm_runlock(&mac_policy_rm, tracker); 253 #endif 254 } 255 256 void 257 mac_policy_sunlock_sleep(void) 258 { 259 260 #ifndef MAC_STATIC 261 if (!mac_late) 262 return; 263 264 rms_runlock(&mac_policy_rms); 265 #endif 266 } 267 268 static void 269 mac_policy_xlock(void) 270 { 271 272 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 273 "mac_policy_xlock()"); 274 275 #ifndef MAC_STATIC 276 if (!mac_late) 277 return; 278 279 rms_wlock(&mac_policy_rms); 280 rm_wlock(&mac_policy_rm); 281 #endif 282 } 283 284 static void 285 mac_policy_xunlock(void) 286 { 287 288 #ifndef MAC_STATIC 289 if (!mac_late) 290 return; 291 292 rm_wunlock(&mac_policy_rm); 293 rms_wunlock(&mac_policy_rms); 294 #endif 295 } 296 297 static void 298 mac_policy_xlock_assert(void) 299 { 300 301 #ifndef MAC_STATIC 302 if (!mac_late) 303 return; 304 305 rm_assert(&mac_policy_rm, RA_WLOCKED); 306 #endif 307 } 308 309 /* 310 * Initialize the MAC subsystem, including appropriate SMP locks. 311 */ 312 static void 313 mac_init(void) 314 { 315 316 LIST_INIT(&mac_static_policy_list); 317 LIST_INIT(&mac_policy_list); 318 mac_labelzone_init(); 319 320 #ifndef MAC_STATIC 321 rm_init_flags(&mac_policy_rm, "mac_policy_rm", RM_NOWITNESS | 322 RM_RECURSE); 323 rms_init(&mac_policy_rms, "mac_policy_rms"); 324 #endif 325 } 326 327 /* 328 * For the purposes of modules that want to know if they were loaded "early", 329 * set the mac_late flag once we've processed modules either linked into the 330 * kernel, or loaded before the kernel startup. 331 */ 332 static void 333 mac_late_init(void) 334 { 335 336 mac_late = 1; 337 } 338 339 /* 340 * Given a policy, derive from its set of non-NULL label init methods what 341 * object types the policy is interested in. 342 */ 343 static uint64_t 344 mac_policy_getlabeled(struct mac_policy_conf *mpc) 345 { 346 uint64_t labeled; 347 348 #define MPC_FLAG(method, flag) \ 349 if (mpc->mpc_ops->mpo_ ## method != NULL) \ 350 labeled |= (flag); \ 351 352 labeled = 0; 353 MPC_FLAG(cred_init_label, MPC_OBJECT_CRED); 354 MPC_FLAG(proc_init_label, MPC_OBJECT_PROC); 355 MPC_FLAG(vnode_init_label, MPC_OBJECT_VNODE); 356 MPC_FLAG(inpcb_init_label, MPC_OBJECT_INPCB); 357 MPC_FLAG(socket_init_label, MPC_OBJECT_SOCKET); 358 MPC_FLAG(devfs_init_label, MPC_OBJECT_DEVFS); 359 MPC_FLAG(mbuf_init_label, MPC_OBJECT_MBUF); 360 MPC_FLAG(ipq_init_label, MPC_OBJECT_IPQ); 361 MPC_FLAG(ifnet_init_label, MPC_OBJECT_IFNET); 362 MPC_FLAG(bpfdesc_init_label, MPC_OBJECT_BPFDESC); 363 MPC_FLAG(pipe_init_label, MPC_OBJECT_PIPE); 364 MPC_FLAG(mount_init_label, MPC_OBJECT_MOUNT); 365 MPC_FLAG(posixsem_init_label, MPC_OBJECT_POSIXSEM); 366 MPC_FLAG(posixshm_init_label, MPC_OBJECT_POSIXSHM); 367 MPC_FLAG(sysvmsg_init_label, MPC_OBJECT_SYSVMSG); 368 MPC_FLAG(sysvmsq_init_label, MPC_OBJECT_SYSVMSQ); 369 MPC_FLAG(sysvsem_init_label, MPC_OBJECT_SYSVSEM); 370 MPC_FLAG(sysvshm_init_label, MPC_OBJECT_SYSVSHM); 371 MPC_FLAG(syncache_init_label, MPC_OBJECT_SYNCACHE); 372 MPC_FLAG(ip6q_init_label, MPC_OBJECT_IP6Q); 373 374 #undef MPC_FLAG 375 return (labeled); 376 } 377 378 /* 379 * When policies are loaded or unloaded, walk the list of registered policies 380 * and built mac_labeled, a bitmask representing the union of all objects 381 * requiring labels across all policies. 382 */ 383 static void 384 mac_policy_update(void) 385 { 386 struct mac_policy_conf *mpc; 387 388 mac_policy_xlock_assert(); 389 390 mac_labeled = 0; 391 mac_policy_count = 0; 392 LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { 393 mac_labeled |= mac_policy_getlabeled(mpc); 394 mac_policy_count++; 395 } 396 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { 397 mac_labeled |= mac_policy_getlabeled(mpc); 398 mac_policy_count++; 399 } 400 } 401 402 /* 403 * There are frequently used code paths which check for rarely installed 404 * policies. Gross hack below enables doing it in a cheap manner. 405 */ 406 407 #define FPO(f) (offsetof(struct mac_policy_ops, mpo_##f) / sizeof(uintptr_t)) 408 409 struct mac_policy_fastpath_elem { 410 int count; 411 bool *flag; 412 size_t offset; 413 }; 414 415 struct mac_policy_fastpath_elem mac_policy_fastpath_array[] = { 416 { .offset = FPO(priv_check), .flag = &mac_priv_check_fp_flag }, 417 { .offset = FPO(priv_grant), .flag = &mac_priv_grant_fp_flag }, 418 { .offset = FPO(vnode_check_lookup), 419 .flag = &mac_vnode_check_lookup_fp_flag }, 420 { .offset = FPO(vnode_check_open), 421 .flag = &mac_vnode_check_open_fp_flag }, 422 { .offset = FPO(vnode_check_stat), 423 .flag = &mac_vnode_check_stat_fp_flag }, 424 { .offset = FPO(vnode_check_read), 425 .flag = &mac_vnode_check_read_fp_flag }, 426 { .offset = FPO(vnode_check_write), 427 .flag = &mac_vnode_check_write_fp_flag }, 428 { .offset = FPO(vnode_check_mmap), 429 .flag = &mac_vnode_check_mmap_fp_flag }, 430 { .offset = FPO(vnode_check_poll), 431 .flag = &mac_vnode_check_poll_fp_flag }, 432 { .offset = FPO(vnode_check_rename_from), 433 .flag = &mac_vnode_check_rename_from_fp_flag }, 434 { .offset = FPO(vnode_check_access), 435 .flag = &mac_vnode_check_access_fp_flag }, 436 }; 437 438 static void 439 mac_policy_fastpath_enable(struct mac_policy_fastpath_elem *mpfe) 440 { 441 442 MPASS(mpfe->count >= 0); 443 mpfe->count++; 444 if (mpfe->count == 1) { 445 MPASS(*mpfe->flag == false); 446 *mpfe->flag = true; 447 } 448 } 449 450 static void 451 mac_policy_fastpath_disable(struct mac_policy_fastpath_elem *mpfe) 452 { 453 454 MPASS(mpfe->count >= 1); 455 mpfe->count--; 456 if (mpfe->count == 0) { 457 MPASS(*mpfe->flag == true); 458 *mpfe->flag = false; 459 } 460 } 461 462 static void 463 mac_policy_fastpath_register(struct mac_policy_conf *mpc) 464 { 465 struct mac_policy_fastpath_elem *mpfe; 466 uintptr_t **ops; 467 int i; 468 469 mac_policy_xlock_assert(); 470 471 ops = (uintptr_t **)mpc->mpc_ops; 472 for (i = 0; i < nitems(mac_policy_fastpath_array); i++) { 473 mpfe = &mac_policy_fastpath_array[i]; 474 if (ops[mpfe->offset] != NULL) 475 mac_policy_fastpath_enable(mpfe); 476 } 477 } 478 479 static void 480 mac_policy_fastpath_unregister(struct mac_policy_conf *mpc) 481 { 482 struct mac_policy_fastpath_elem *mpfe; 483 uintptr_t **ops; 484 int i; 485 486 mac_policy_xlock_assert(); 487 488 ops = (uintptr_t **)mpc->mpc_ops; 489 for (i = 0; i < nitems(mac_policy_fastpath_array); i++) { 490 mpfe = &mac_policy_fastpath_array[i]; 491 if (ops[mpfe->offset] != NULL) 492 mac_policy_fastpath_disable(mpfe); 493 } 494 } 495 496 #undef FPO 497 498 static int 499 mac_policy_register(struct mac_policy_conf *mpc) 500 { 501 struct mac_policy_conf *tmpc; 502 int error, slot, static_entry; 503 504 error = 0; 505 506 /* 507 * We don't technically need exclusive access while !mac_late, but 508 * hold it for assertion consistency. 509 */ 510 mac_policy_xlock(); 511 512 /* 513 * If the module can potentially be unloaded, or we're loading late, 514 * we have to stick it in the non-static list and pay an extra 515 * performance overhead. Otherwise, we can pay a light locking cost 516 * and stick it in the static list. 517 */ 518 static_entry = (!mac_late && 519 !(mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_UNLOADOK)); 520 521 if (static_entry) { 522 LIST_FOREACH(tmpc, &mac_static_policy_list, mpc_list) { 523 if (strcmp(tmpc->mpc_name, mpc->mpc_name) == 0) { 524 error = EEXIST; 525 goto out; 526 } 527 } 528 } else { 529 LIST_FOREACH(tmpc, &mac_policy_list, mpc_list) { 530 if (strcmp(tmpc->mpc_name, mpc->mpc_name) == 0) { 531 error = EEXIST; 532 goto out; 533 } 534 } 535 } 536 if (mpc->mpc_field_off != NULL) { 537 slot = ffs(mac_slot_offsets_free); 538 if (slot == 0) { 539 error = ENOMEM; 540 goto out; 541 } 542 slot--; 543 mac_slot_offsets_free &= ~(1 << slot); 544 *mpc->mpc_field_off = slot; 545 } 546 mpc->mpc_runtime_flags |= MPC_RUNTIME_FLAG_REGISTERED; 547 548 /* 549 * If we're loading a MAC module after the framework has initialized, 550 * it has to go into the dynamic list. If we're loading it before 551 * we've finished initializing, it can go into the static list with 552 * weaker locker requirements. 553 */ 554 if (static_entry) 555 LIST_INSERT_HEAD(&mac_static_policy_list, mpc, mpc_list); 556 else 557 LIST_INSERT_HEAD(&mac_policy_list, mpc, mpc_list); 558 559 /* 560 * Per-policy initialization. Currently, this takes place under the 561 * exclusive lock, so policies must not sleep in their init method. 562 * In the future, we may want to separate "init" from "start", with 563 * "init" occurring without the lock held. Likewise, on tear-down, 564 * breaking out "stop" from "destroy". 565 */ 566 if (mpc->mpc_ops->mpo_init != NULL) 567 (*(mpc->mpc_ops->mpo_init))(mpc); 568 569 mac_policy_fastpath_register(mpc); 570 571 mac_policy_update(); 572 573 SDT_PROBE1(mac, , policy, register, mpc); 574 printf("Security policy loaded: %s (%s)\n", mpc->mpc_fullname, 575 mpc->mpc_name); 576 577 out: 578 mac_policy_xunlock(); 579 return (error); 580 } 581 582 static int 583 mac_policy_unregister(struct mac_policy_conf *mpc) 584 { 585 586 /* 587 * If we fail the load, we may get a request to unload. Check to see 588 * if we did the run-time registration, and if not, silently succeed. 589 */ 590 mac_policy_xlock(); 591 if ((mpc->mpc_runtime_flags & MPC_RUNTIME_FLAG_REGISTERED) == 0) { 592 mac_policy_xunlock(); 593 return (0); 594 } 595 #if 0 596 /* 597 * Don't allow unloading modules with private data. 598 */ 599 if (mpc->mpc_field_off != NULL) { 600 mac_policy_xunlock(); 601 return (EBUSY); 602 } 603 #endif 604 /* 605 * Only allow the unload to proceed if the module is unloadable by 606 * its own definition. 607 */ 608 if ((mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_UNLOADOK) == 0) { 609 mac_policy_xunlock(); 610 return (EBUSY); 611 } 612 613 mac_policy_fastpath_unregister(mpc); 614 615 if (mpc->mpc_ops->mpo_destroy != NULL) 616 (*(mpc->mpc_ops->mpo_destroy))(mpc); 617 618 LIST_REMOVE(mpc, mpc_list); 619 mpc->mpc_runtime_flags &= ~MPC_RUNTIME_FLAG_REGISTERED; 620 mac_policy_update(); 621 mac_policy_xunlock(); 622 623 SDT_PROBE1(mac, , policy, unregister, mpc); 624 printf("Security policy unload: %s (%s)\n", mpc->mpc_fullname, 625 mpc->mpc_name); 626 627 return (0); 628 } 629 630 /* 631 * Allow MAC policy modules to register during boot, etc. 632 */ 633 int 634 mac_policy_modevent(module_t mod, int type, void *data) 635 { 636 struct mac_policy_conf *mpc; 637 int error; 638 639 error = 0; 640 mpc = (struct mac_policy_conf *) data; 641 642 #ifdef MAC_STATIC 643 if (mac_late) { 644 printf("mac_policy_modevent: MAC_STATIC and late\n"); 645 return (EBUSY); 646 } 647 #endif 648 649 SDT_PROBE2(mac, , policy, modevent, type, mpc); 650 switch (type) { 651 case MOD_LOAD: 652 if (mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_NOTLATE && 653 mac_late) { 654 printf("mac_policy_modevent: can't load %s policy " 655 "after booting\n", mpc->mpc_name); 656 error = EBUSY; 657 break; 658 } 659 error = mac_policy_register(mpc); 660 break; 661 case MOD_UNLOAD: 662 /* Don't unregister the module if it was never registered. */ 663 if ((mpc->mpc_runtime_flags & MPC_RUNTIME_FLAG_REGISTERED) 664 != 0) 665 error = mac_policy_unregister(mpc); 666 else 667 error = 0; 668 break; 669 default: 670 error = EOPNOTSUPP; 671 break; 672 } 673 674 return (error); 675 } 676 677 /* 678 * Define an error value precedence, and given two arguments, selects the 679 * value with the higher precedence. 680 */ 681 int 682 mac_error_select(int error1, int error2) 683 { 684 685 /* Certain decision-making errors take top priority. */ 686 if (error1 == EDEADLK || error2 == EDEADLK) 687 return (EDEADLK); 688 689 /* Invalid arguments should be reported where possible. */ 690 if (error1 == EINVAL || error2 == EINVAL) 691 return (EINVAL); 692 693 /* Precedence goes to "visibility", with both process and file. */ 694 if (error1 == ESRCH || error2 == ESRCH) 695 return (ESRCH); 696 697 if (error1 == ENOENT || error2 == ENOENT) 698 return (ENOENT); 699 700 /* Precedence goes to DAC/MAC protections. */ 701 if (error1 == EACCES || error2 == EACCES) 702 return (EACCES); 703 704 /* Precedence goes to privilege. */ 705 if (error1 == EPERM || error2 == EPERM) 706 return (EPERM); 707 708 /* Precedence goes to error over success; otherwise, arbitrary. */ 709 if (error1 != 0) 710 return (error1); 711 return (error2); 712 } 713 714 int 715 mac_check_structmac_consistent(struct mac *mac) 716 { 717 718 /* Require that labels have a non-zero length. */ 719 if (mac->m_buflen > MAC_MAX_LABEL_BUF_LEN || 720 mac->m_buflen <= sizeof("")) 721 return (EINVAL); 722 723 return (0); 724 } 725 726 SYSINIT(mac, SI_SUB_MAC, SI_ORDER_FIRST, mac_init, NULL); 727 SYSINIT(mac_late, SI_SUB_MAC_LATE, SI_ORDER_FIRST, mac_late_init, NULL); 728