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