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_kdtrace.h" 70 #include "opt_mac.h" 71 72 #include <sys/cdefs.h> 73 __FBSDID("$FreeBSD$"); 74 75 #include <sys/param.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/systm.h> 85 #include <sys/sysctl.h> 86 87 #include <security/mac/mac_framework.h> 88 #include <security/mac/mac_internal.h> 89 #include <security/mac/mac_policy.h> 90 91 /* 92 * DTrace SDT providers for MAC. 93 */ 94 SDT_PROVIDER_DEFINE(mac); 95 SDT_PROVIDER_DEFINE(mac_framework); 96 97 SDT_PROBE_DEFINE2(mac, kernel, policy, modevent, "int", 98 "struct mac_policy_conf *mpc"); 99 SDT_PROBE_DEFINE1(mac, kernel, policy, register, "struct mac_policy_conf *"); 100 SDT_PROBE_DEFINE1(mac, kernel, policy, unregister, "struct mac_policy_conf *"); 101 102 /* 103 * Root sysctl node for all MAC and MAC policy controls. 104 */ 105 SYSCTL_NODE(_security, OID_AUTO, mac, CTLFLAG_RW, 0, 106 "TrustedBSD MAC policy controls"); 107 108 /* 109 * Declare that the kernel provides MAC support, version 3 (FreeBSD 7.x). 110 * This permits modules to refuse to be loaded if the necessary support isn't 111 * present, even if it's pre-boot. 112 */ 113 MODULE_VERSION(kernel_mac_support, MAC_VERSION); 114 115 static unsigned int mac_version = MAC_VERSION; 116 SYSCTL_UINT(_security_mac, OID_AUTO, version, CTLFLAG_RD, &mac_version, 0, 117 ""); 118 119 /* 120 * Labels consist of a indexed set of "slots", which are allocated policies 121 * as required. The MAC Framework maintains a bitmask of slots allocated so 122 * far to prevent reuse. Slots cannot be reused, as the MAC Framework 123 * guarantees that newly allocated slots in labels will be NULL unless 124 * otherwise initialized, and because we do not have a mechanism to garbage 125 * collect slots on policy unload. As labeled policies tend to be statically 126 * loaded during boot, and not frequently unloaded and reloaded, this is not 127 * generally an issue. 128 */ 129 #if MAC_MAX_SLOTS > 32 130 #error "MAC_MAX_SLOTS too large" 131 #endif 132 133 static unsigned int mac_max_slots = MAC_MAX_SLOTS; 134 static unsigned int mac_slot_offsets_free = (1 << MAC_MAX_SLOTS) - 1; 135 SYSCTL_UINT(_security_mac, OID_AUTO, max_slots, CTLFLAG_RD, &mac_max_slots, 136 0, ""); 137 138 /* 139 * Has the kernel started generating labeled objects yet? All read/write 140 * access to this variable is serialized during the boot process. Following 141 * the end of serialization, we don't update this flag; no locking. 142 */ 143 static int mac_late = 0; 144 145 /* 146 * Each policy declares a mask of object types requiring labels to be 147 * allocated for them. For convenience, we combine and cache the bitwise or 148 * of the per-policy object flags to track whether we will allocate a label 149 * for an object type at run-time. 150 */ 151 uint64_t mac_labeled; 152 SYSCTL_QUAD(_security_mac, OID_AUTO, labeled, CTLFLAG_RD, &mac_labeled, 0, 153 "Mask of object types being labeled"); 154 155 MALLOC_DEFINE(M_MACTEMP, "mactemp", "MAC temporary label storage"); 156 157 /* 158 * MAC policy modules are placed in one of two lists: mac_static_policy_list, 159 * for policies that are loaded early and cannot be unloaded, and 160 * mac_policy_list, which holds policies either loaded later in the boot 161 * cycle or that may be unloaded. The static policy list does not require 162 * locks to iterate over, but the dynamic list requires synchronization. 163 * Support for dynamic policy loading can be compiled out using the 164 * MAC_STATIC kernel option. 165 * 166 * The dynamic policy list is protected by two locks: modifying the list 167 * requires both locks to be held exclusively. One of the locks, 168 * mac_policy_rm, is acquired over policy entry points that will never sleep; 169 * the other, mac_policy_sx, is acquire over policy entry points that may 170 * sleep. The former category will be used when kernel locks may be held 171 * over calls to the MAC Framework, during network processing in ithreads, 172 * etc. The latter will tend to involve potentially blocking memory 173 * allocations, extended attribute I/O, etc. 174 */ 175 #ifndef MAC_STATIC 176 static struct rmlock mac_policy_rm; /* Non-sleeping entry points. */ 177 static struct sx mac_policy_sx; /* Sleeping entry points. */ 178 #endif 179 180 struct mac_policy_list_head mac_policy_list; 181 struct mac_policy_list_head mac_static_policy_list; 182 183 static void mac_policy_xlock(void); 184 static void mac_policy_xlock_assert(void); 185 static void mac_policy_xunlock(void); 186 187 void 188 mac_policy_slock_nosleep(struct rm_priotracker *tracker) 189 { 190 191 #ifndef MAC_STATIC 192 if (!mac_late) 193 return; 194 195 rm_rlock(&mac_policy_rm, tracker); 196 #endif 197 } 198 199 void 200 mac_policy_slock_sleep(void) 201 { 202 203 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 204 "mac_policy_slock_sleep"); 205 206 #ifndef MAC_STATIC 207 if (!mac_late) 208 return; 209 210 sx_slock(&mac_policy_sx); 211 #endif 212 } 213 214 void 215 mac_policy_sunlock_nosleep(struct rm_priotracker *tracker) 216 { 217 218 #ifndef MAC_STATIC 219 if (!mac_late) 220 return; 221 222 rm_runlock(&mac_policy_rm, tracker); 223 #endif 224 } 225 226 void 227 mac_policy_sunlock_sleep(void) 228 { 229 230 #ifndef MAC_STATIC 231 if (!mac_late) 232 return; 233 234 sx_sunlock(&mac_policy_sx); 235 #endif 236 } 237 238 static void 239 mac_policy_xlock(void) 240 { 241 242 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 243 "mac_policy_xlock()"); 244 245 #ifndef MAC_STATIC 246 if (!mac_late) 247 return; 248 249 sx_xlock(&mac_policy_sx); 250 rm_wlock(&mac_policy_rm); 251 #endif 252 } 253 254 static void 255 mac_policy_xunlock(void) 256 { 257 258 #ifndef MAC_STATIC 259 if (!mac_late) 260 return; 261 262 rm_wunlock(&mac_policy_rm); 263 sx_xunlock(&mac_policy_sx); 264 #endif 265 } 266 267 static void 268 mac_policy_xlock_assert(void) 269 { 270 271 #ifndef MAC_STATIC 272 if (!mac_late) 273 return; 274 275 /* XXXRW: rm_assert(&mac_policy_rm, RA_WLOCKED); */ 276 sx_assert(&mac_policy_sx, SA_XLOCKED); 277 #endif 278 } 279 280 /* 281 * Initialize the MAC subsystem, including appropriate SMP locks. 282 */ 283 static void 284 mac_init(void) 285 { 286 287 LIST_INIT(&mac_static_policy_list); 288 LIST_INIT(&mac_policy_list); 289 mac_labelzone_init(); 290 291 #ifndef MAC_STATIC 292 rm_init(&mac_policy_rm, "mac_policy_rm"); 293 sx_init(&mac_policy_sx, "mac_policy_sx"); 294 #endif 295 } 296 297 /* 298 * For the purposes of modules that want to know if they were loaded "early", 299 * set the mac_late flag once we've processed modules either linked into the 300 * kernel, or loaded before the kernel startup. 301 */ 302 static void 303 mac_late_init(void) 304 { 305 306 mac_late = 1; 307 } 308 309 /* 310 * Given a policy, derive from its set of non-NULL label init methods what 311 * object types the policy is interested in. 312 */ 313 static uint64_t 314 mac_policy_getlabeled(struct mac_policy_conf *mpc) 315 { 316 uint64_t labeled; 317 318 #define MPC_FLAG(method, flag) \ 319 if (mpc->mpc_ops->mpo_ ## method != NULL) \ 320 labeled |= (flag); \ 321 322 labeled = 0; 323 MPC_FLAG(cred_init_label, MPC_OBJECT_CRED); 324 MPC_FLAG(proc_init_label, MPC_OBJECT_PROC); 325 MPC_FLAG(vnode_init_label, MPC_OBJECT_VNODE); 326 MPC_FLAG(inpcb_init_label, MPC_OBJECT_INPCB); 327 MPC_FLAG(socket_init_label, MPC_OBJECT_SOCKET); 328 MPC_FLAG(devfs_init_label, MPC_OBJECT_DEVFS); 329 MPC_FLAG(mbuf_init_label, MPC_OBJECT_MBUF); 330 MPC_FLAG(ipq_init_label, MPC_OBJECT_IPQ); 331 MPC_FLAG(ifnet_init_label, MPC_OBJECT_IFNET); 332 MPC_FLAG(bpfdesc_init_label, MPC_OBJECT_BPFDESC); 333 MPC_FLAG(pipe_init_label, MPC_OBJECT_PIPE); 334 MPC_FLAG(mount_init_label, MPC_OBJECT_MOUNT); 335 MPC_FLAG(posixsem_init_label, MPC_OBJECT_POSIXSEM); 336 MPC_FLAG(posixshm_init_label, MPC_OBJECT_POSIXSHM); 337 MPC_FLAG(sysvmsg_init_label, MPC_OBJECT_SYSVMSG); 338 MPC_FLAG(sysvmsq_init_label, MPC_OBJECT_SYSVMSQ); 339 MPC_FLAG(sysvsem_init_label, MPC_OBJECT_SYSVSEM); 340 MPC_FLAG(sysvshm_init_label, MPC_OBJECT_SYSVSHM); 341 MPC_FLAG(syncache_init_label, MPC_OBJECT_SYNCACHE); 342 MPC_FLAG(ip6q_init_label, MPC_OBJECT_IP6Q); 343 344 #undef MPC_FLAG 345 return (labeled); 346 } 347 348 /* 349 * When policies are loaded or unloaded, walk the list of registered policies 350 * and built mac_labeled, a bitmask representing the union of all objects 351 * requiring labels across all policies. 352 */ 353 static void 354 mac_policy_updateflags(void) 355 { 356 struct mac_policy_conf *mpc; 357 358 mac_policy_xlock_assert(); 359 360 mac_labeled = 0; 361 LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) 362 mac_labeled |= mac_policy_getlabeled(mpc); 363 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) 364 mac_labeled |= mac_policy_getlabeled(mpc); 365 } 366 367 static int 368 mac_policy_register(struct mac_policy_conf *mpc) 369 { 370 struct mac_policy_conf *tmpc; 371 int error, slot, static_entry; 372 373 error = 0; 374 375 /* 376 * We don't technically need exclusive access while !mac_late, but 377 * hold it for assertion consistency. 378 */ 379 mac_policy_xlock(); 380 381 /* 382 * If the module can potentially be unloaded, or we're loading late, 383 * we have to stick it in the non-static list and pay an extra 384 * performance overhead. Otherwise, we can pay a light locking cost 385 * and stick it in the static list. 386 */ 387 static_entry = (!mac_late && 388 !(mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_UNLOADOK)); 389 390 if (static_entry) { 391 LIST_FOREACH(tmpc, &mac_static_policy_list, mpc_list) { 392 if (strcmp(tmpc->mpc_name, mpc->mpc_name) == 0) { 393 error = EEXIST; 394 goto out; 395 } 396 } 397 } else { 398 LIST_FOREACH(tmpc, &mac_policy_list, mpc_list) { 399 if (strcmp(tmpc->mpc_name, mpc->mpc_name) == 0) { 400 error = EEXIST; 401 goto out; 402 } 403 } 404 } 405 if (mpc->mpc_field_off != NULL) { 406 slot = ffs(mac_slot_offsets_free); 407 if (slot == 0) { 408 error = ENOMEM; 409 goto out; 410 } 411 slot--; 412 mac_slot_offsets_free &= ~(1 << slot); 413 *mpc->mpc_field_off = slot; 414 } 415 mpc->mpc_runtime_flags |= MPC_RUNTIME_FLAG_REGISTERED; 416 417 /* 418 * If we're loading a MAC module after the framework has initialized, 419 * it has to go into the dynamic list. If we're loading it before 420 * we've finished initializing, it can go into the static list with 421 * weaker locker requirements. 422 */ 423 if (static_entry) 424 LIST_INSERT_HEAD(&mac_static_policy_list, mpc, mpc_list); 425 else 426 LIST_INSERT_HEAD(&mac_policy_list, mpc, mpc_list); 427 428 /* 429 * Per-policy initialization. Currently, this takes place under the 430 * exclusive lock, so policies must not sleep in their init method. 431 * In the future, we may want to separate "init" from "start", with 432 * "init" occuring without the lock held. Likewise, on tear-down, 433 * breaking out "stop" from "destroy". 434 */ 435 if (mpc->mpc_ops->mpo_init != NULL) 436 (*(mpc->mpc_ops->mpo_init))(mpc); 437 mac_policy_updateflags(); 438 439 SDT_PROBE(mac, kernel, policy, register, mpc, 0, 0, 0, 0); 440 printf("Security policy loaded: %s (%s)\n", mpc->mpc_fullname, 441 mpc->mpc_name); 442 443 out: 444 mac_policy_xunlock(); 445 return (error); 446 } 447 448 static int 449 mac_policy_unregister(struct mac_policy_conf *mpc) 450 { 451 452 /* 453 * If we fail the load, we may get a request to unload. Check to see 454 * if we did the run-time registration, and if not, silently succeed. 455 */ 456 mac_policy_xlock(); 457 if ((mpc->mpc_runtime_flags & MPC_RUNTIME_FLAG_REGISTERED) == 0) { 458 mac_policy_xunlock(); 459 return (0); 460 } 461 #if 0 462 /* 463 * Don't allow unloading modules with private data. 464 */ 465 if (mpc->mpc_field_off != NULL) { 466 mac_policy_xunlock(); 467 return (EBUSY); 468 } 469 #endif 470 /* 471 * Only allow the unload to proceed if the module is unloadable by 472 * its own definition. 473 */ 474 if ((mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_UNLOADOK) == 0) { 475 mac_policy_xunlock(); 476 return (EBUSY); 477 } 478 if (mpc->mpc_ops->mpo_destroy != NULL) 479 (*(mpc->mpc_ops->mpo_destroy))(mpc); 480 481 LIST_REMOVE(mpc, mpc_list); 482 mpc->mpc_runtime_flags &= ~MPC_RUNTIME_FLAG_REGISTERED; 483 mac_policy_updateflags(); 484 mac_policy_xunlock(); 485 486 SDT_PROBE(mac, kernel, policy, unregister, mpc, 0, 0, 0, 0); 487 printf("Security policy unload: %s (%s)\n", mpc->mpc_fullname, 488 mpc->mpc_name); 489 490 return (0); 491 } 492 493 /* 494 * Allow MAC policy modules to register during boot, etc. 495 */ 496 int 497 mac_policy_modevent(module_t mod, int type, void *data) 498 { 499 struct mac_policy_conf *mpc; 500 int error; 501 502 error = 0; 503 mpc = (struct mac_policy_conf *) data; 504 505 #ifdef MAC_STATIC 506 if (mac_late) { 507 printf("mac_policy_modevent: MAC_STATIC and late\n"); 508 return (EBUSY); 509 } 510 #endif 511 512 SDT_PROBE(mac, kernel, policy, modevent, type, mpc, 0, 0, 0); 513 switch (type) { 514 case MOD_LOAD: 515 if (mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_NOTLATE && 516 mac_late) { 517 printf("mac_policy_modevent: can't load %s policy " 518 "after booting\n", mpc->mpc_name); 519 error = EBUSY; 520 break; 521 } 522 error = mac_policy_register(mpc); 523 break; 524 case MOD_UNLOAD: 525 /* Don't unregister the module if it was never registered. */ 526 if ((mpc->mpc_runtime_flags & MPC_RUNTIME_FLAG_REGISTERED) 527 != 0) 528 error = mac_policy_unregister(mpc); 529 else 530 error = 0; 531 break; 532 default: 533 error = EOPNOTSUPP; 534 break; 535 } 536 537 return (error); 538 } 539 540 /* 541 * Define an error value precedence, and given two arguments, selects the 542 * value with the higher precedence. 543 */ 544 int 545 mac_error_select(int error1, int error2) 546 { 547 548 /* Certain decision-making errors take top priority. */ 549 if (error1 == EDEADLK || error2 == EDEADLK) 550 return (EDEADLK); 551 552 /* Invalid arguments should be reported where possible. */ 553 if (error1 == EINVAL || error2 == EINVAL) 554 return (EINVAL); 555 556 /* Precedence goes to "visibility", with both process and file. */ 557 if (error1 == ESRCH || error2 == ESRCH) 558 return (ESRCH); 559 560 if (error1 == ENOENT || error2 == ENOENT) 561 return (ENOENT); 562 563 /* Precedence goes to DAC/MAC protections. */ 564 if (error1 == EACCES || error2 == EACCES) 565 return (EACCES); 566 567 /* Precedence goes to privilege. */ 568 if (error1 == EPERM || error2 == EPERM) 569 return (EPERM); 570 571 /* Precedence goes to error over success; otherwise, arbitrary. */ 572 if (error1 != 0) 573 return (error1); 574 return (error2); 575 } 576 577 int 578 mac_check_structmac_consistent(struct mac *mac) 579 { 580 581 if (mac->m_buflen < 0 || 582 mac->m_buflen > MAC_MAX_LABEL_BUF_LEN) 583 return (EINVAL); 584 585 return (0); 586 } 587 588 SYSINIT(mac, SI_SUB_MAC, SI_ORDER_FIRST, mac_init, NULL); 589 SYSINIT(mac_late, SI_SUB_MAC_LATE, SI_ORDER_FIRST, mac_late_init, NULL); 590