mac_biba.c revision 173102
1/*- 2 * Copyright (c) 1999-2002, 2007 Robert N. M. Watson 3 * Copyright (c) 2001-2005 McAfee, Inc. 4 * Copyright (c) 2006 SPARTA, Inc. 5 * All rights reserved. 6 * 7 * This software was developed by Robert Watson for the TrustedBSD Project. 8 * 9 * This software was developed for the FreeBSD Project in part by McAfee 10 * Research, the Security Research Division of McAfee, Inc. under 11 * DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA 12 * CHATS research program. 13 * 14 * This software was enhanced by SPARTA ISSO under SPAWAR contract 15 * N66001-04-C-6019 ("SEFOS"). 16 * 17 * Redistribution and use in source and binary forms, with or without 18 * modification, are permitted provided that the following conditions 19 * are met: 20 * 1. Redistributions of source code must retain the above copyright 21 * notice, this list of conditions and the following disclaimer. 22 * 2. Redistributions in binary form must reproduce the above copyright 23 * notice, this list of conditions and the following disclaimer in the 24 * documentation and/or other materials provided with the distribution. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * $FreeBSD: head/sys/security/mac_biba/mac_biba.c 173102 2007-10-28 17:12:48Z rwatson $ 39 */ 40 41/* 42 * Developed by the TrustedBSD Project. 43 * 44 * Biba fixed label mandatory integrity policy. 45 */ 46 47#include <sys/param.h> 48#include <sys/conf.h> 49#include <sys/extattr.h> 50#include <sys/kernel.h> 51#include <sys/ksem.h> 52#include <sys/malloc.h> 53#include <sys/mman.h> 54#include <sys/mount.h> 55#include <sys/priv.h> 56#include <sys/proc.h> 57#include <sys/sbuf.h> 58#include <sys/systm.h> 59#include <sys/sysproto.h> 60#include <sys/sysent.h> 61#include <sys/systm.h> 62#include <sys/vnode.h> 63#include <sys/file.h> 64#include <sys/socket.h> 65#include <sys/socketvar.h> 66#include <sys/pipe.h> 67#include <sys/sx.h> 68#include <sys/sysctl.h> 69#include <sys/msg.h> 70#include <sys/sem.h> 71#include <sys/shm.h> 72 73#include <fs/devfs/devfs.h> 74 75#include <net/bpfdesc.h> 76#include <net/if.h> 77#include <net/if_types.h> 78#include <net/if_var.h> 79 80#include <netinet/in.h> 81#include <netinet/in_pcb.h> 82#include <netinet/ip_var.h> 83 84#include <vm/uma.h> 85#include <vm/vm.h> 86 87#include <security/mac/mac_policy.h> 88#include <security/mac_biba/mac_biba.h> 89 90SYSCTL_DECL(_security_mac); 91 92SYSCTL_NODE(_security_mac, OID_AUTO, biba, CTLFLAG_RW, 0, 93 "TrustedBSD mac_biba policy controls"); 94 95static int biba_label_size = sizeof(struct mac_biba); 96SYSCTL_INT(_security_mac_biba, OID_AUTO, label_size, CTLFLAG_RD, 97 &biba_label_size, 0, "Size of struct mac_biba"); 98 99static int biba_enabled = 1; 100SYSCTL_INT(_security_mac_biba, OID_AUTO, enabled, CTLFLAG_RW, &biba_enabled, 101 0, "Enforce MAC/Biba policy"); 102TUNABLE_INT("security.mac.biba.enabled", &biba_enabled); 103 104static int destroyed_not_inited; 105SYSCTL_INT(_security_mac_biba, OID_AUTO, destroyed_not_inited, CTLFLAG_RD, 106 &destroyed_not_inited, 0, "Count of labels destroyed but not inited"); 107 108static int trust_all_interfaces = 0; 109SYSCTL_INT(_security_mac_biba, OID_AUTO, trust_all_interfaces, CTLFLAG_RD, 110 &trust_all_interfaces, 0, "Consider all interfaces 'trusted' by MAC/Biba"); 111TUNABLE_INT("security.mac.biba.trust_all_interfaces", &trust_all_interfaces); 112 113static char trusted_interfaces[128]; 114SYSCTL_STRING(_security_mac_biba, OID_AUTO, trusted_interfaces, CTLFLAG_RD, 115 trusted_interfaces, 0, "Interfaces considered 'trusted' by MAC/Biba"); 116TUNABLE_STR("security.mac.biba.trusted_interfaces", trusted_interfaces, 117 sizeof(trusted_interfaces)); 118 119static int max_compartments = MAC_BIBA_MAX_COMPARTMENTS; 120SYSCTL_INT(_security_mac_biba, OID_AUTO, max_compartments, CTLFLAG_RD, 121 &max_compartments, 0, "Maximum supported compartments"); 122 123static int ptys_equal = 0; 124SYSCTL_INT(_security_mac_biba, OID_AUTO, ptys_equal, CTLFLAG_RW, 125 &ptys_equal, 0, "Label pty devices as biba/equal on create"); 126TUNABLE_INT("security.mac.biba.ptys_equal", &ptys_equal); 127 128static int interfaces_equal; 129SYSCTL_INT(_security_mac_biba, OID_AUTO, interfaces_equal, CTLFLAG_RW, 130 &interfaces_equal, 0, "Label network interfaces as biba/equal on create"); 131TUNABLE_INT("security.mac.biba.interfaces_equal", &interfaces_equal); 132 133static int revocation_enabled = 0; 134SYSCTL_INT(_security_mac_biba, OID_AUTO, revocation_enabled, CTLFLAG_RW, 135 &revocation_enabled, 0, "Revoke access to objects on relabel"); 136TUNABLE_INT("security.mac.biba.revocation_enabled", &revocation_enabled); 137 138static int biba_slot; 139#define SLOT(l) ((struct mac_biba *)mac_label_get((l), biba_slot)) 140#define SLOT_SET(l, val) mac_label_set((l), biba_slot, (uintptr_t)(val)) 141 142static uma_zone_t zone_biba; 143 144static __inline int 145biba_bit_set_empty(u_char *set) { 146 int i; 147 148 for (i = 0; i < MAC_BIBA_MAX_COMPARTMENTS >> 3; i++) 149 if (set[i] != 0) 150 return (0); 151 return (1); 152} 153 154static struct mac_biba * 155biba_alloc(int flag) 156{ 157 158 return (uma_zalloc(zone_biba, flag | M_ZERO)); 159} 160 161static void 162biba_free(struct mac_biba *mb) 163{ 164 165 if (mb != NULL) 166 uma_zfree(zone_biba, mb); 167 else 168 atomic_add_int(&destroyed_not_inited, 1); 169} 170 171static int 172biba_atmostflags(struct mac_biba *mb, int flags) 173{ 174 175 if ((mb->mb_flags & flags) != mb->mb_flags) 176 return (EINVAL); 177 return (0); 178} 179 180static int 181biba_dominate_element(struct mac_biba_element *a, struct mac_biba_element *b) 182{ 183 int bit; 184 185 switch (a->mbe_type) { 186 case MAC_BIBA_TYPE_EQUAL: 187 case MAC_BIBA_TYPE_HIGH: 188 return (1); 189 190 case MAC_BIBA_TYPE_LOW: 191 switch (b->mbe_type) { 192 case MAC_BIBA_TYPE_GRADE: 193 case MAC_BIBA_TYPE_HIGH: 194 return (0); 195 196 case MAC_BIBA_TYPE_EQUAL: 197 case MAC_BIBA_TYPE_LOW: 198 return (1); 199 200 default: 201 panic("biba_dominate_element: b->mbe_type invalid"); 202 } 203 204 case MAC_BIBA_TYPE_GRADE: 205 switch (b->mbe_type) { 206 case MAC_BIBA_TYPE_EQUAL: 207 case MAC_BIBA_TYPE_LOW: 208 return (1); 209 210 case MAC_BIBA_TYPE_HIGH: 211 return (0); 212 213 case MAC_BIBA_TYPE_GRADE: 214 for (bit = 1; bit <= MAC_BIBA_MAX_COMPARTMENTS; bit++) 215 if (!MAC_BIBA_BIT_TEST(bit, 216 a->mbe_compartments) && 217 MAC_BIBA_BIT_TEST(bit, b->mbe_compartments)) 218 return (0); 219 return (a->mbe_grade >= b->mbe_grade); 220 221 default: 222 panic("biba_dominate_element: b->mbe_type invalid"); 223 } 224 225 default: 226 panic("biba_dominate_element: a->mbe_type invalid"); 227 } 228 229 return (0); 230} 231 232static int 233biba_subject_dominate_high(struct mac_biba *mb) 234{ 235 struct mac_biba_element *element; 236 237 KASSERT((mb->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) != 0, 238 ("biba_effective_in_range: mb not effective")); 239 element = &mb->mb_effective; 240 241 return (element->mbe_type == MAC_BIBA_TYPE_EQUAL || 242 element->mbe_type == MAC_BIBA_TYPE_HIGH); 243} 244 245static int 246biba_range_in_range(struct mac_biba *rangea, struct mac_biba *rangeb) 247{ 248 249 return (biba_dominate_element(&rangeb->mb_rangehigh, 250 &rangea->mb_rangehigh) && 251 biba_dominate_element(&rangea->mb_rangelow, 252 &rangeb->mb_rangelow)); 253} 254 255static int 256biba_effective_in_range(struct mac_biba *effective, struct mac_biba *range) 257{ 258 259 KASSERT((effective->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) != 0, 260 ("biba_effective_in_range: a not effective")); 261 KASSERT((range->mb_flags & MAC_BIBA_FLAG_RANGE) != 0, 262 ("biba_effective_in_range: b not range")); 263 264 return (biba_dominate_element(&range->mb_rangehigh, 265 &effective->mb_effective) && 266 biba_dominate_element(&effective->mb_effective, 267 &range->mb_rangelow)); 268 269 return (1); 270} 271 272static int 273biba_dominate_effective(struct mac_biba *a, struct mac_biba *b) 274{ 275 KASSERT((a->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) != 0, 276 ("biba_dominate_effective: a not effective")); 277 KASSERT((b->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) != 0, 278 ("biba_dominate_effective: b not effective")); 279 280 return (biba_dominate_element(&a->mb_effective, &b->mb_effective)); 281} 282 283static int 284biba_equal_element(struct mac_biba_element *a, struct mac_biba_element *b) 285{ 286 287 if (a->mbe_type == MAC_BIBA_TYPE_EQUAL || 288 b->mbe_type == MAC_BIBA_TYPE_EQUAL) 289 return (1); 290 291 return (a->mbe_type == b->mbe_type && a->mbe_grade == b->mbe_grade); 292} 293 294static int 295biba_equal_effective(struct mac_biba *a, struct mac_biba *b) 296{ 297 298 KASSERT((a->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) != 0, 299 ("biba_equal_effective: a not effective")); 300 KASSERT((b->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) != 0, 301 ("biba_equal_effective: b not effective")); 302 303 return (biba_equal_element(&a->mb_effective, &b->mb_effective)); 304} 305 306static int 307biba_contains_equal(struct mac_biba *mb) 308{ 309 310 if (mb->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) { 311 if (mb->mb_effective.mbe_type == MAC_BIBA_TYPE_EQUAL) 312 return (1); 313 } 314 315 if (mb->mb_flags & MAC_BIBA_FLAG_RANGE) { 316 if (mb->mb_rangelow.mbe_type == MAC_BIBA_TYPE_EQUAL) 317 return (1); 318 if (mb->mb_rangehigh.mbe_type == MAC_BIBA_TYPE_EQUAL) 319 return (1); 320 } 321 322 return (0); 323} 324 325static int 326biba_subject_privileged(struct mac_biba *mb) 327{ 328 329 KASSERT((mb->mb_flags & MAC_BIBA_FLAGS_BOTH) == MAC_BIBA_FLAGS_BOTH, 330 ("biba_subject_privileged: subject doesn't have both labels")); 331 332 /* If the effective is EQUAL, it's ok. */ 333 if (mb->mb_effective.mbe_type == MAC_BIBA_TYPE_EQUAL) 334 return (0); 335 336 /* If either range endpoint is EQUAL, it's ok. */ 337 if (mb->mb_rangelow.mbe_type == MAC_BIBA_TYPE_EQUAL || 338 mb->mb_rangehigh.mbe_type == MAC_BIBA_TYPE_EQUAL) 339 return (0); 340 341 /* If the range is low-high, it's ok. */ 342 if (mb->mb_rangelow.mbe_type == MAC_BIBA_TYPE_LOW && 343 mb->mb_rangehigh.mbe_type == MAC_BIBA_TYPE_HIGH) 344 return (0); 345 346 /* It's not ok. */ 347 return (EPERM); 348} 349 350static int 351biba_high_effective(struct mac_biba *mb) 352{ 353 354 KASSERT((mb->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) != 0, 355 ("biba_equal_effective: mb not effective")); 356 357 return (mb->mb_effective.mbe_type == MAC_BIBA_TYPE_HIGH); 358} 359 360static int 361biba_valid(struct mac_biba *mb) 362{ 363 364 if (mb->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) { 365 switch (mb->mb_effective.mbe_type) { 366 case MAC_BIBA_TYPE_GRADE: 367 break; 368 369 case MAC_BIBA_TYPE_EQUAL: 370 case MAC_BIBA_TYPE_HIGH: 371 case MAC_BIBA_TYPE_LOW: 372 if (mb->mb_effective.mbe_grade != 0 || 373 !MAC_BIBA_BIT_SET_EMPTY( 374 mb->mb_effective.mbe_compartments)) 375 return (EINVAL); 376 break; 377 378 default: 379 return (EINVAL); 380 } 381 } else { 382 if (mb->mb_effective.mbe_type != MAC_BIBA_TYPE_UNDEF) 383 return (EINVAL); 384 } 385 386 if (mb->mb_flags & MAC_BIBA_FLAG_RANGE) { 387 switch (mb->mb_rangelow.mbe_type) { 388 case MAC_BIBA_TYPE_GRADE: 389 break; 390 391 case MAC_BIBA_TYPE_EQUAL: 392 case MAC_BIBA_TYPE_HIGH: 393 case MAC_BIBA_TYPE_LOW: 394 if (mb->mb_rangelow.mbe_grade != 0 || 395 !MAC_BIBA_BIT_SET_EMPTY( 396 mb->mb_rangelow.mbe_compartments)) 397 return (EINVAL); 398 break; 399 400 default: 401 return (EINVAL); 402 } 403 404 switch (mb->mb_rangehigh.mbe_type) { 405 case MAC_BIBA_TYPE_GRADE: 406 break; 407 408 case MAC_BIBA_TYPE_EQUAL: 409 case MAC_BIBA_TYPE_HIGH: 410 case MAC_BIBA_TYPE_LOW: 411 if (mb->mb_rangehigh.mbe_grade != 0 || 412 !MAC_BIBA_BIT_SET_EMPTY( 413 mb->mb_rangehigh.mbe_compartments)) 414 return (EINVAL); 415 break; 416 417 default: 418 return (EINVAL); 419 } 420 if (!biba_dominate_element(&mb->mb_rangehigh, 421 &mb->mb_rangelow)) 422 return (EINVAL); 423 } else { 424 if (mb->mb_rangelow.mbe_type != MAC_BIBA_TYPE_UNDEF || 425 mb->mb_rangehigh.mbe_type != MAC_BIBA_TYPE_UNDEF) 426 return (EINVAL); 427 } 428 429 return (0); 430} 431 432static void 433biba_set_range(struct mac_biba *mb, u_short typelow, u_short gradelow, 434 u_char *compartmentslow, u_short typehigh, u_short gradehigh, 435 u_char *compartmentshigh) 436{ 437 438 mb->mb_rangelow.mbe_type = typelow; 439 mb->mb_rangelow.mbe_grade = gradelow; 440 if (compartmentslow != NULL) 441 memcpy(mb->mb_rangelow.mbe_compartments, compartmentslow, 442 sizeof(mb->mb_rangelow.mbe_compartments)); 443 mb->mb_rangehigh.mbe_type = typehigh; 444 mb->mb_rangehigh.mbe_grade = gradehigh; 445 if (compartmentshigh != NULL) 446 memcpy(mb->mb_rangehigh.mbe_compartments, compartmentshigh, 447 sizeof(mb->mb_rangehigh.mbe_compartments)); 448 mb->mb_flags |= MAC_BIBA_FLAG_RANGE; 449} 450 451static void 452biba_set_effective(struct mac_biba *mb, u_short type, u_short grade, 453 u_char *compartments) 454{ 455 456 mb->mb_effective.mbe_type = type; 457 mb->mb_effective.mbe_grade = grade; 458 if (compartments != NULL) 459 memcpy(mb->mb_effective.mbe_compartments, compartments, 460 sizeof(mb->mb_effective.mbe_compartments)); 461 mb->mb_flags |= MAC_BIBA_FLAG_EFFECTIVE; 462} 463 464static void 465biba_copy_range(struct mac_biba *labelfrom, struct mac_biba *labelto) 466{ 467 468 KASSERT((labelfrom->mb_flags & MAC_BIBA_FLAG_RANGE) != 0, 469 ("biba_copy_range: labelfrom not range")); 470 471 labelto->mb_rangelow = labelfrom->mb_rangelow; 472 labelto->mb_rangehigh = labelfrom->mb_rangehigh; 473 labelto->mb_flags |= MAC_BIBA_FLAG_RANGE; 474} 475 476static void 477biba_copy_effective(struct mac_biba *labelfrom, struct mac_biba *labelto) 478{ 479 480 KASSERT((labelfrom->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) != 0, 481 ("biba_copy_effective: labelfrom not effective")); 482 483 labelto->mb_effective = labelfrom->mb_effective; 484 labelto->mb_flags |= MAC_BIBA_FLAG_EFFECTIVE; 485} 486 487static void 488biba_copy(struct mac_biba *source, struct mac_biba *dest) 489{ 490 491 if (source->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) 492 biba_copy_effective(source, dest); 493 if (source->mb_flags & MAC_BIBA_FLAG_RANGE) 494 biba_copy_range(source, dest); 495} 496 497/* 498 * Policy module operations. 499 */ 500static void 501biba_init(struct mac_policy_conf *conf) 502{ 503 504 zone_biba = uma_zcreate("mac_biba", sizeof(struct mac_biba), NULL, 505 NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 506} 507 508/* 509 * Label operations. 510 */ 511static void 512biba_init_label(struct label *label) 513{ 514 515 SLOT_SET(label, biba_alloc(M_WAITOK)); 516} 517 518static int 519biba_init_label_waitcheck(struct label *label, int flag) 520{ 521 522 SLOT_SET(label, biba_alloc(flag)); 523 if (SLOT(label) == NULL) 524 return (ENOMEM); 525 526 return (0); 527} 528 529static void 530biba_destroy_label(struct label *label) 531{ 532 533 biba_free(SLOT(label)); 534 SLOT_SET(label, NULL); 535} 536 537/* 538 * biba_element_to_string() accepts an sbuf and Biba element. It converts 539 * the Biba element to a string and stores the result in the sbuf; if there 540 * isn't space in the sbuf, -1 is returned. 541 */ 542static int 543biba_element_to_string(struct sbuf *sb, struct mac_biba_element *element) 544{ 545 int i, first; 546 547 switch (element->mbe_type) { 548 case MAC_BIBA_TYPE_HIGH: 549 return (sbuf_printf(sb, "high")); 550 551 case MAC_BIBA_TYPE_LOW: 552 return (sbuf_printf(sb, "low")); 553 554 case MAC_BIBA_TYPE_EQUAL: 555 return (sbuf_printf(sb, "equal")); 556 557 case MAC_BIBA_TYPE_GRADE: 558 if (sbuf_printf(sb, "%d", element->mbe_grade) == -1) 559 return (-1); 560 561 first = 1; 562 for (i = 1; i <= MAC_BIBA_MAX_COMPARTMENTS; i++) { 563 if (MAC_BIBA_BIT_TEST(i, element->mbe_compartments)) { 564 if (first) { 565 if (sbuf_putc(sb, ':') == -1) 566 return (-1); 567 if (sbuf_printf(sb, "%d", i) == -1) 568 return (-1); 569 first = 0; 570 } else { 571 if (sbuf_printf(sb, "+%d", i) == -1) 572 return (-1); 573 } 574 } 575 } 576 return (0); 577 578 default: 579 panic("biba_element_to_string: invalid type (%d)", 580 element->mbe_type); 581 } 582} 583 584/* 585 * biba_to_string() converts a Biba label to a string, and places the results 586 * in the passed sbuf. It returns 0 on success, or EINVAL if there isn't 587 * room in the sbuf. Note: the sbuf will be modified even in a failure case, 588 * so the caller may need to revert the sbuf by restoring the offset if 589 * that's undesired. 590 */ 591static int 592biba_to_string(struct sbuf *sb, struct mac_biba *mb) 593{ 594 595 if (mb->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) { 596 if (biba_element_to_string(sb, &mb->mb_effective) == -1) 597 return (EINVAL); 598 } 599 600 if (mb->mb_flags & MAC_BIBA_FLAG_RANGE) { 601 if (sbuf_putc(sb, '(') == -1) 602 return (EINVAL); 603 604 if (biba_element_to_string(sb, &mb->mb_rangelow) == -1) 605 return (EINVAL); 606 607 if (sbuf_putc(sb, '-') == -1) 608 return (EINVAL); 609 610 if (biba_element_to_string(sb, &mb->mb_rangehigh) == -1) 611 return (EINVAL); 612 613 if (sbuf_putc(sb, ')') == -1) 614 return (EINVAL); 615 } 616 617 return (0); 618} 619 620static int 621biba_externalize_label(struct label *label, char *element_name, 622 struct sbuf *sb, int *claimed) 623{ 624 struct mac_biba *mb; 625 626 if (strcmp(MAC_BIBA_LABEL_NAME, element_name) != 0) 627 return (0); 628 629 (*claimed)++; 630 631 mb = SLOT(label); 632 return (biba_to_string(sb, mb)); 633} 634 635static int 636biba_parse_element(struct mac_biba_element *element, char *string) 637{ 638 char *compartment, *end, *grade; 639 int value; 640 641 if (strcmp(string, "high") == 0 || 642 strcmp(string, "hi") == 0) { 643 element->mbe_type = MAC_BIBA_TYPE_HIGH; 644 element->mbe_grade = MAC_BIBA_TYPE_UNDEF; 645 } else if (strcmp(string, "low") == 0 || 646 strcmp(string, "lo") == 0) { 647 element->mbe_type = MAC_BIBA_TYPE_LOW; 648 element->mbe_grade = MAC_BIBA_TYPE_UNDEF; 649 } else if (strcmp(string, "equal") == 0 || 650 strcmp(string, "eq") == 0) { 651 element->mbe_type = MAC_BIBA_TYPE_EQUAL; 652 element->mbe_grade = MAC_BIBA_TYPE_UNDEF; 653 } else { 654 element->mbe_type = MAC_BIBA_TYPE_GRADE; 655 656 /* 657 * Numeric grade piece of the element. 658 */ 659 grade = strsep(&string, ":"); 660 value = strtol(grade, &end, 10); 661 if (end == grade || *end != '\0') 662 return (EINVAL); 663 if (value < 0 || value > 65535) 664 return (EINVAL); 665 element->mbe_grade = value; 666 667 /* 668 * Optional compartment piece of the element. If none 669 * are included, we assume that the label has no 670 * compartments. 671 */ 672 if (string == NULL) 673 return (0); 674 if (*string == '\0') 675 return (0); 676 677 while ((compartment = strsep(&string, "+")) != NULL) { 678 value = strtol(compartment, &end, 10); 679 if (compartment == end || *end != '\0') 680 return (EINVAL); 681 if (value < 1 || value > MAC_BIBA_MAX_COMPARTMENTS) 682 return (EINVAL); 683 MAC_BIBA_BIT_SET(value, element->mbe_compartments); 684 } 685 } 686 687 return (0); 688} 689 690/* 691 * Note: destructively consumes the string, make a local copy before 692 * calling if that's a problem. 693 */ 694static int 695biba_parse(struct mac_biba *mb, char *string) 696{ 697 char *rangehigh, *rangelow, *effective; 698 int error; 699 700 effective = strsep(&string, "("); 701 if (*effective == '\0') 702 effective = NULL; 703 704 if (string != NULL) { 705 rangelow = strsep(&string, "-"); 706 if (string == NULL) 707 return (EINVAL); 708 rangehigh = strsep(&string, ")"); 709 if (string == NULL) 710 return (EINVAL); 711 if (*string != '\0') 712 return (EINVAL); 713 } else { 714 rangelow = NULL; 715 rangehigh = NULL; 716 } 717 718 KASSERT((rangelow != NULL && rangehigh != NULL) || 719 (rangelow == NULL && rangehigh == NULL), 720 ("biba_parse: range mismatch")); 721 722 bzero(mb, sizeof(*mb)); 723 if (effective != NULL) { 724 error = biba_parse_element(&mb->mb_effective, effective); 725 if (error) 726 return (error); 727 mb->mb_flags |= MAC_BIBA_FLAG_EFFECTIVE; 728 } 729 730 if (rangelow != NULL) { 731 error = biba_parse_element(&mb->mb_rangelow, rangelow); 732 if (error) 733 return (error); 734 error = biba_parse_element(&mb->mb_rangehigh, rangehigh); 735 if (error) 736 return (error); 737 mb->mb_flags |= MAC_BIBA_FLAG_RANGE; 738 } 739 740 error = biba_valid(mb); 741 if (error) 742 return (error); 743 744 return (0); 745} 746 747static int 748biba_internalize_label(struct label *label, char *element_name, 749 char *element_data, int *claimed) 750{ 751 struct mac_biba *mb, mb_temp; 752 int error; 753 754 if (strcmp(MAC_BIBA_LABEL_NAME, element_name) != 0) 755 return (0); 756 757 (*claimed)++; 758 759 error = biba_parse(&mb_temp, element_data); 760 if (error) 761 return (error); 762 763 mb = SLOT(label); 764 *mb = mb_temp; 765 766 return (0); 767} 768 769static void 770biba_copy_label(struct label *src, struct label *dest) 771{ 772 773 *SLOT(dest) = *SLOT(src); 774} 775 776/* 777 * Labeling event operations: file system objects, and things that look a lot 778 * like file system objects. 779 */ 780static void 781biba_devfs_create_device(struct ucred *cred, struct mount *mp, 782 struct cdev *dev, struct devfs_dirent *de, struct label *delabel) 783{ 784 struct mac_biba *mb; 785 int biba_type; 786 787 mb = SLOT(delabel); 788 if (strcmp(dev->si_name, "null") == 0 || 789 strcmp(dev->si_name, "zero") == 0 || 790 strcmp(dev->si_name, "random") == 0 || 791 strncmp(dev->si_name, "fd/", strlen("fd/")) == 0) 792 biba_type = MAC_BIBA_TYPE_EQUAL; 793 else if (ptys_equal && 794 (strncmp(dev->si_name, "ttyp", strlen("ttyp")) == 0 || 795 strncmp(dev->si_name, "ptyp", strlen("ptyp")) == 0)) 796 biba_type = MAC_BIBA_TYPE_EQUAL; 797 else 798 biba_type = MAC_BIBA_TYPE_HIGH; 799 biba_set_effective(mb, biba_type, 0, NULL); 800} 801 802static void 803biba_devfs_create_directory(struct mount *mp, char *dirname, int dirnamelen, 804 struct devfs_dirent *de, struct label *delabel) 805{ 806 struct mac_biba *mb; 807 808 mb = SLOT(delabel); 809 810 biba_set_effective(mb, MAC_BIBA_TYPE_HIGH, 0, NULL); 811} 812 813static void 814biba_devfs_create_symlink(struct ucred *cred, struct mount *mp, 815 struct devfs_dirent *dd, struct label *ddlabel, struct devfs_dirent *de, 816 struct label *delabel) 817{ 818 struct mac_biba *source, *dest; 819 820 source = SLOT(cred->cr_label); 821 dest = SLOT(delabel); 822 823 biba_copy_effective(source, dest); 824} 825 826static void 827biba_mount_create(struct ucred *cred, struct mount *mp, 828 struct label *mplabel) 829{ 830 struct mac_biba *source, *dest; 831 832 source = SLOT(cred->cr_label); 833 dest = SLOT(mplabel); 834 835 biba_copy_effective(source, dest); 836} 837 838static void 839biba_vnode_relabel(struct ucred *cred, struct vnode *vp, 840 struct label *vplabel, struct label *newlabel) 841{ 842 struct mac_biba *source, *dest; 843 844 source = SLOT(newlabel); 845 dest = SLOT(vplabel); 846 847 biba_copy(source, dest); 848} 849 850static void 851biba_devfs_update(struct mount *mp, struct devfs_dirent *de, 852 struct label *delabel, struct vnode *vp, struct label *vplabel) 853{ 854 struct mac_biba *source, *dest; 855 856 source = SLOT(vplabel); 857 dest = SLOT(delabel); 858 859 biba_copy(source, dest); 860} 861 862static void 863biba_devfs_vnode_associate(struct mount *mp, struct label *mntlabel, 864 struct devfs_dirent *de, struct label *delabel, struct vnode *vp, 865 struct label *vplabel) 866{ 867 struct mac_biba *source, *dest; 868 869 source = SLOT(delabel); 870 dest = SLOT(vplabel); 871 872 biba_copy_effective(source, dest); 873} 874 875static int 876biba_vnode_associate_extattr(struct mount *mp, struct label *mplabel, 877 struct vnode *vp, struct label *vplabel) 878{ 879 struct mac_biba mb_temp, *source, *dest; 880 int buflen, error; 881 882 source = SLOT(mplabel); 883 dest = SLOT(vplabel); 884 885 buflen = sizeof(mb_temp); 886 bzero(&mb_temp, buflen); 887 888 error = vn_extattr_get(vp, IO_NODELOCKED, MAC_BIBA_EXTATTR_NAMESPACE, 889 MAC_BIBA_EXTATTR_NAME, &buflen, (char *) &mb_temp, curthread); 890 if (error == ENOATTR || error == EOPNOTSUPP) { 891 /* Fall back to the mntlabel. */ 892 biba_copy_effective(source, dest); 893 return (0); 894 } else if (error) 895 return (error); 896 897 if (buflen != sizeof(mb_temp)) { 898 printf("biba_vnode_associate_extattr: bad size %d\n", 899 buflen); 900 return (EPERM); 901 } 902 if (biba_valid(&mb_temp) != 0) { 903 printf("biba_vnode_associate_extattr: invalid\n"); 904 return (EPERM); 905 } 906 if ((mb_temp.mb_flags & MAC_BIBA_FLAGS_BOTH) != 907 MAC_BIBA_FLAG_EFFECTIVE) { 908 printf("biba_vnode_associate_extattr: not effective\n"); 909 return (EPERM); 910 } 911 912 biba_copy_effective(&mb_temp, dest); 913 return (0); 914} 915 916static void 917biba_vnode_associate_singlelabel(struct mount *mp, struct label *mplabel, 918 struct vnode *vp, struct label *vplabel) 919{ 920 struct mac_biba *source, *dest; 921 922 source = SLOT(mplabel); 923 dest = SLOT(vplabel); 924 925 biba_copy_effective(source, dest); 926} 927 928static int 929biba_vnode_create_extattr(struct ucred *cred, struct mount *mp, 930 struct label *mplabel, struct vnode *dvp, struct label *dvplabel, 931 struct vnode *vp, struct label *vplabel, struct componentname *cnp) 932{ 933 struct mac_biba *source, *dest, mb_temp; 934 size_t buflen; 935 int error; 936 937 buflen = sizeof(mb_temp); 938 bzero(&mb_temp, buflen); 939 940 source = SLOT(cred->cr_label); 941 dest = SLOT(vplabel); 942 biba_copy_effective(source, &mb_temp); 943 944 error = vn_extattr_set(vp, IO_NODELOCKED, MAC_BIBA_EXTATTR_NAMESPACE, 945 MAC_BIBA_EXTATTR_NAME, buflen, (char *) &mb_temp, curthread); 946 if (error == 0) 947 biba_copy_effective(source, dest); 948 return (error); 949} 950 951static int 952biba_vnode_setlabel_extattr(struct ucred *cred, struct vnode *vp, 953 struct label *vplabel, struct label *intlabel) 954{ 955 struct mac_biba *source, mb_temp; 956 size_t buflen; 957 int error; 958 959 buflen = sizeof(mb_temp); 960 bzero(&mb_temp, buflen); 961 962 source = SLOT(intlabel); 963 if ((source->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) == 0) 964 return (0); 965 966 biba_copy_effective(source, &mb_temp); 967 968 error = vn_extattr_set(vp, IO_NODELOCKED, MAC_BIBA_EXTATTR_NAMESPACE, 969 MAC_BIBA_EXTATTR_NAME, buflen, (char *) &mb_temp, curthread); 970 return (error); 971} 972 973/* 974 * Labeling event operations: IPC object. 975 */ 976static void 977biba_inpcb_create(struct socket *so, struct label *solabel, 978 struct inpcb *inp, struct label *inplabel) 979{ 980 struct mac_biba *source, *dest; 981 982 source = SLOT(solabel); 983 dest = SLOT(inplabel); 984 985 biba_copy_effective(source, dest); 986} 987 988static void 989biba_socket_create_mbuf(struct socket *so, struct label *solabel, 990 struct mbuf *m, struct label *mlabel) 991{ 992 struct mac_biba *source, *dest; 993 994 source = SLOT(solabel); 995 dest = SLOT(mlabel); 996 997 biba_copy_effective(source, dest); 998} 999 1000static void 1001biba_socket_create(struct ucred *cred, struct socket *so, 1002 struct label *solabel) 1003{ 1004 struct mac_biba *source, *dest; 1005 1006 source = SLOT(cred->cr_label); 1007 dest = SLOT(solabel); 1008 1009 biba_copy_effective(source, dest); 1010} 1011 1012static void 1013biba_pipe_create(struct ucred *cred, struct pipepair *pp, 1014 struct label *pplabel) 1015{ 1016 struct mac_biba *source, *dest; 1017 1018 source = SLOT(cred->cr_label); 1019 dest = SLOT(pplabel); 1020 1021 biba_copy_effective(source, dest); 1022} 1023 1024static void 1025biba_posixsem_create(struct ucred *cred, struct ksem *ks, 1026 struct label *kslabel) 1027{ 1028 struct mac_biba *source, *dest; 1029 1030 source = SLOT(cred->cr_label); 1031 dest = SLOT(kslabel); 1032 1033 biba_copy_effective(source, dest); 1034} 1035 1036static void 1037biba_socket_newconn(struct socket *oldso, struct label *oldsolabel, 1038 struct socket *newso, struct label *newsolabel) 1039{ 1040 struct mac_biba *source, *dest; 1041 1042 source = SLOT(oldsolabel); 1043 dest = SLOT(newsolabel); 1044 1045 biba_copy_effective(source, dest); 1046} 1047 1048static void 1049biba_socket_relabel(struct ucred *cred, struct socket *so, 1050 struct label *solabel, struct label *newlabel) 1051{ 1052 struct mac_biba *source, *dest; 1053 1054 source = SLOT(newlabel); 1055 dest = SLOT(solabel); 1056 1057 biba_copy(source, dest); 1058} 1059 1060static void 1061biba_pipe_relabel(struct ucred *cred, struct pipepair *pp, 1062 struct label *pplabel, struct label *newlabel) 1063{ 1064 struct mac_biba *source, *dest; 1065 1066 source = SLOT(newlabel); 1067 dest = SLOT(pplabel); 1068 1069 biba_copy(source, dest); 1070} 1071 1072static void 1073biba_socketpeer_set_from_mbuf(struct mbuf *m, struct label *mlabel, 1074 struct socket *so, struct label *sopeerlabel) 1075{ 1076 struct mac_biba *source, *dest; 1077 1078 source = SLOT(mlabel); 1079 dest = SLOT(sopeerlabel); 1080 1081 biba_copy_effective(source, dest); 1082} 1083 1084/* 1085 * Labeling event operations: System V IPC objects. 1086 */ 1087static void 1088biba_sysvmsg_create(struct ucred *cred, struct msqid_kernel *msqkptr, 1089 struct label *msqlabel, struct msg *msgptr, struct label *msglabel) 1090{ 1091 struct mac_biba *source, *dest; 1092 1093 /* Ignore the msgq label */ 1094 source = SLOT(cred->cr_label); 1095 dest = SLOT(msglabel); 1096 1097 biba_copy_effective(source, dest); 1098} 1099 1100static void 1101biba_sysvmsq_create(struct ucred *cred, struct msqid_kernel *msqkptr, 1102 struct label *msqlabel) 1103{ 1104 struct mac_biba *source, *dest; 1105 1106 source = SLOT(cred->cr_label); 1107 dest = SLOT(msqlabel); 1108 1109 biba_copy_effective(source, dest); 1110} 1111 1112static void 1113biba_sysvsem_create(struct ucred *cred, struct semid_kernel *semakptr, 1114 struct label *semalabel) 1115{ 1116 struct mac_biba *source, *dest; 1117 1118 source = SLOT(cred->cr_label); 1119 dest = SLOT(semalabel); 1120 1121 biba_copy_effective(source, dest); 1122} 1123 1124static void 1125biba_sysvshm_create(struct ucred *cred, struct shmid_kernel *shmsegptr, 1126 struct label *shmlabel) 1127{ 1128 struct mac_biba *source, *dest; 1129 1130 source = SLOT(cred->cr_label); 1131 dest = SLOT(shmlabel); 1132 1133 biba_copy_effective(source, dest); 1134} 1135 1136/* 1137 * Labeling event operations: network objects. 1138 */ 1139static void 1140biba_socketpeer_set_from_socket(struct socket *oldso, 1141 struct label *oldsolabel, struct socket *newso, 1142 struct label *newsopeerlabel) 1143{ 1144 struct mac_biba *source, *dest; 1145 1146 source = SLOT(oldsolabel); 1147 dest = SLOT(newsopeerlabel); 1148 1149 biba_copy_effective(source, dest); 1150} 1151 1152static void 1153biba_bpfdesc_create(struct ucred *cred, struct bpf_d *d, 1154 struct label *dlabel) 1155{ 1156 struct mac_biba *source, *dest; 1157 1158 source = SLOT(cred->cr_label); 1159 dest = SLOT(dlabel); 1160 1161 biba_copy_effective(source, dest); 1162} 1163 1164static void 1165biba_ifnet_create(struct ifnet *ifp, struct label *ifplabel) 1166{ 1167 char tifname[IFNAMSIZ], *p, *q; 1168 char tiflist[sizeof(trusted_interfaces)]; 1169 struct mac_biba *dest; 1170 int len, type; 1171 1172 dest = SLOT(ifplabel); 1173 1174 if (ifp->if_type == IFT_LOOP || interfaces_equal != 0) { 1175 type = MAC_BIBA_TYPE_EQUAL; 1176 goto set; 1177 } 1178 1179 if (trust_all_interfaces) { 1180 type = MAC_BIBA_TYPE_HIGH; 1181 goto set; 1182 } 1183 1184 type = MAC_BIBA_TYPE_LOW; 1185 1186 if (trusted_interfaces[0] == '\0' || 1187 !strvalid(trusted_interfaces, sizeof(trusted_interfaces))) 1188 goto set; 1189 1190 bzero(tiflist, sizeof(tiflist)); 1191 for (p = trusted_interfaces, q = tiflist; *p != '\0'; p++, q++) 1192 if(*p != ' ' && *p != '\t') 1193 *q = *p; 1194 1195 for (p = q = tiflist;; p++) { 1196 if (*p == ',' || *p == '\0') { 1197 len = p - q; 1198 if (len < IFNAMSIZ) { 1199 bzero(tifname, sizeof(tifname)); 1200 bcopy(q, tifname, len); 1201 if (strcmp(tifname, ifp->if_xname) == 0) { 1202 type = MAC_BIBA_TYPE_HIGH; 1203 break; 1204 } 1205 } else { 1206 *p = '\0'; 1207 printf("mac_biba warning: interface name " 1208 "\"%s\" is too long (must be < %d)\n", 1209 q, IFNAMSIZ); 1210 } 1211 if (*p == '\0') 1212 break; 1213 q = p + 1; 1214 } 1215 } 1216set: 1217 biba_set_effective(dest, type, 0, NULL); 1218 biba_set_range(dest, type, 0, NULL, type, 0, NULL); 1219} 1220 1221static void 1222biba_ipq_create(struct mbuf *m, struct label *mlabel, struct ipq *ipq, 1223 struct label *ipqlabel) 1224{ 1225 struct mac_biba *source, *dest; 1226 1227 source = SLOT(mlabel); 1228 dest = SLOT(ipqlabel); 1229 1230 biba_copy_effective(source, dest); 1231} 1232 1233static void 1234biba_ipq_reassemble(struct ipq *ipq, struct label *ipqlabel, struct mbuf *m, 1235 struct label *mlabel) 1236{ 1237 struct mac_biba *source, *dest; 1238 1239 source = SLOT(ipqlabel); 1240 dest = SLOT(mlabel); 1241 1242 /* Just use the head, since we require them all to match. */ 1243 biba_copy_effective(source, dest); 1244} 1245 1246static void 1247biba_netinet_fragment(struct mbuf *m, struct label *mlabel, 1248 struct mbuf *frag, struct label *fraglabel) 1249{ 1250 struct mac_biba *source, *dest; 1251 1252 source = SLOT(mlabel); 1253 dest = SLOT(fraglabel); 1254 1255 biba_copy_effective(source, dest); 1256} 1257 1258static void 1259biba_inpcb_create_mbuf(struct inpcb *inp, struct label *inplabel, 1260 struct mbuf *m, struct label *mlabel) 1261{ 1262 struct mac_biba *source, *dest; 1263 1264 source = SLOT(inplabel); 1265 dest = SLOT(mlabel); 1266 1267 biba_copy_effective(source, dest); 1268} 1269 1270static void 1271biba_bpfdesc_create_mbuf(struct bpf_d *d, struct label *dlabel, 1272 struct mbuf *m, struct label *mlabel) 1273{ 1274 struct mac_biba *source, *dest; 1275 1276 source = SLOT(dlabel); 1277 dest = SLOT(mlabel); 1278 1279 biba_copy_effective(source, dest); 1280} 1281 1282static void 1283biba_ifnet_create_mbuf(struct ifnet *ifp, struct label *ifplabel, 1284 struct mbuf *m, struct label *mlabel) 1285{ 1286 struct mac_biba *source, *dest; 1287 1288 source = SLOT(ifplabel); 1289 dest = SLOT(mlabel); 1290 1291 biba_copy_effective(source, dest); 1292} 1293 1294static void 1295biba_mbuf_create_multicast_encap(struct mbuf *m, struct label *mlabel, 1296 struct ifnet *ifp, struct label *ifplabel, struct mbuf *mnew, 1297 struct label *mnewlabel) 1298{ 1299 struct mac_biba *source, *dest; 1300 1301 source = SLOT(mlabel); 1302 dest = SLOT(mnewlabel); 1303 1304 biba_copy_effective(source, dest); 1305} 1306 1307static int 1308biba_ipq_match(struct mbuf *m, struct label *mlabel, struct ipq *ipq, 1309 struct label *ipqlabel) 1310{ 1311 struct mac_biba *a, *b; 1312 1313 a = SLOT(ipqlabel); 1314 b = SLOT(mlabel); 1315 1316 return (biba_equal_effective(a, b)); 1317} 1318 1319static void 1320biba_ifnet_relabel(struct ucred *cred, struct ifnet *ifp, 1321 struct label *ifplabel, struct label *newlabel) 1322{ 1323 struct mac_biba *source, *dest; 1324 1325 source = SLOT(newlabel); 1326 dest = SLOT(ifplabel); 1327 1328 biba_copy(source, dest); 1329} 1330 1331static void 1332biba_ipq_update(struct mbuf *m, struct label *mlabel, struct ipq *ipq, 1333 struct label *ipqlabel) 1334{ 1335 1336 /* NOOP: we only accept matching labels, so no need to update */ 1337} 1338 1339static void 1340biba_inpcb_sosetlabel(struct socket *so, struct label *solabel, 1341 struct inpcb *inp, struct label *inplabel) 1342{ 1343 struct mac_biba *source, *dest; 1344 1345 source = SLOT(solabel); 1346 dest = SLOT(inplabel); 1347 1348 biba_copy(source, dest); 1349} 1350 1351static void 1352biba_netatalk_aarp_send(struct ifnet *ifp, struct label *ifplabel, 1353 struct mbuf *m, struct label *mlabel) 1354{ 1355 struct mac_biba *dest; 1356 1357 dest = SLOT(mlabel); 1358 1359 biba_set_effective(dest, MAC_BIBA_TYPE_EQUAL, 0, NULL); 1360} 1361 1362static void 1363biba_netinet_arp_send(struct ifnet *ifp, struct label *ifplabel, 1364 struct mbuf *m, struct label *mlabel) 1365{ 1366 struct mac_biba *dest; 1367 1368 dest = SLOT(mlabel); 1369 1370 biba_set_effective(dest, MAC_BIBA_TYPE_EQUAL, 0, NULL); 1371} 1372 1373static void 1374biba_netinet_firewall_reply(struct mbuf *mrecv, struct label *mrecvlabel, 1375 struct mbuf *msend, struct label *msendlabel) 1376{ 1377 struct mac_biba *source, *dest; 1378 1379 source = SLOT(mrecvlabel); 1380 dest = SLOT(msendlabel); 1381 1382 biba_copy_effective(source, dest); 1383} 1384 1385static void 1386biba_netinet_firewall_send(struct mbuf *m, struct label *mlabel) 1387{ 1388 struct mac_biba *dest; 1389 1390 dest = SLOT(mlabel); 1391 1392 /* XXX: where is the label for the firewall really coming from? */ 1393 biba_set_effective(dest, MAC_BIBA_TYPE_EQUAL, 0, NULL); 1394} 1395 1396static void 1397biba_netinet_icmp_reply(struct mbuf *mrecv, struct label *mrecvlabel, 1398 struct mbuf *msend, struct label *msendlabel) 1399{ 1400 struct mac_biba *source, *dest; 1401 1402 source = SLOT(mrecvlabel); 1403 dest = SLOT(msendlabel); 1404 1405 biba_copy_effective(source, dest); 1406} 1407 1408static void 1409biba_netinet_igmp_send(struct ifnet *ifp, struct label *ifplabel, 1410 struct mbuf *m, struct label *mlabel) 1411{ 1412 struct mac_biba *dest; 1413 1414 dest = SLOT(mlabel); 1415 1416 biba_set_effective(dest, MAC_BIBA_TYPE_EQUAL, 0, NULL); 1417} 1418 1419static void 1420biba_netinet6_nd6_send(struct ifnet *ifp, struct label *ifplabel, 1421 struct mbuf *m, struct label *mlabel) 1422{ 1423 struct mac_biba *dest; 1424 1425 dest = SLOT(mlabel); 1426 1427 biba_set_effective(dest, MAC_BIBA_TYPE_EQUAL, 0, NULL); 1428} 1429 1430/* 1431 * Labeling event operations: processes. 1432 */ 1433static void 1434biba_proc_create_swapper(struct ucred *cred) 1435{ 1436 struct mac_biba *dest; 1437 1438 dest = SLOT(cred->cr_label); 1439 1440 biba_set_effective(dest, MAC_BIBA_TYPE_EQUAL, 0, NULL); 1441 biba_set_range(dest, MAC_BIBA_TYPE_LOW, 0, NULL, MAC_BIBA_TYPE_HIGH, 1442 0, NULL); 1443} 1444 1445static void 1446biba_proc_create_init(struct ucred *cred) 1447{ 1448 struct mac_biba *dest; 1449 1450 dest = SLOT(cred->cr_label); 1451 1452 biba_set_effective(dest, MAC_BIBA_TYPE_HIGH, 0, NULL); 1453 biba_set_range(dest, MAC_BIBA_TYPE_LOW, 0, NULL, MAC_BIBA_TYPE_HIGH, 1454 0, NULL); 1455} 1456 1457static void 1458biba_proc_associate_nfsd(struct ucred *cred) 1459{ 1460 struct mac_biba *label; 1461 1462 label = SLOT(cred->cr_label); 1463 biba_set_effective(label, MAC_BIBA_TYPE_LOW, 0, NULL); 1464 biba_set_range(label, MAC_BIBA_TYPE_LOW, 0, NULL, MAC_BIBA_TYPE_HIGH, 1465 0, NULL); 1466} 1467 1468static void 1469biba_cred_relabel(struct ucred *cred, struct label *newlabel) 1470{ 1471 struct mac_biba *source, *dest; 1472 1473 source = SLOT(newlabel); 1474 dest = SLOT(cred->cr_label); 1475 1476 biba_copy(source, dest); 1477} 1478 1479/* 1480 * Label cleanup/flush operations 1481 */ 1482static void 1483biba_sysvmsg_cleanup(struct label *msglabel) 1484{ 1485 1486 bzero(SLOT(msglabel), sizeof(struct mac_biba)); 1487} 1488 1489static void 1490biba_sysvmsq_cleanup(struct label *msqlabel) 1491{ 1492 1493 bzero(SLOT(msqlabel), sizeof(struct mac_biba)); 1494} 1495 1496static void 1497biba_sysvsem_cleanup(struct label *semalabel) 1498{ 1499 1500 bzero(SLOT(semalabel), sizeof(struct mac_biba)); 1501} 1502 1503static void 1504biba_sysvshm_cleanup(struct label *shmlabel) 1505{ 1506 bzero(SLOT(shmlabel), sizeof(struct mac_biba)); 1507} 1508 1509/* 1510 * Access control checks. 1511 */ 1512static int 1513biba_bpfdesc_check_receive(struct bpf_d *d, struct label *dlabel, 1514 struct ifnet *ifp, struct label *ifplabel) 1515{ 1516 struct mac_biba *a, *b; 1517 1518 if (!biba_enabled) 1519 return (0); 1520 1521 a = SLOT(dlabel); 1522 b = SLOT(ifplabel); 1523 1524 if (biba_equal_effective(a, b)) 1525 return (0); 1526 return (EACCES); 1527} 1528 1529static int 1530biba_cred_check_relabel(struct ucred *cred, struct label *newlabel) 1531{ 1532 struct mac_biba *subj, *new; 1533 int error; 1534 1535 subj = SLOT(cred->cr_label); 1536 new = SLOT(newlabel); 1537 1538 /* 1539 * If there is a Biba label update for the credential, it may 1540 * be an update of the effective, range, or both. 1541 */ 1542 error = biba_atmostflags(new, MAC_BIBA_FLAGS_BOTH); 1543 if (error) 1544 return (error); 1545 1546 /* 1547 * If the Biba label is to be changed, authorize as appropriate. 1548 */ 1549 if (new->mb_flags & MAC_BIBA_FLAGS_BOTH) { 1550 /* 1551 * If the change request modifies both the Biba label 1552 * effective and range, check that the new effective will be 1553 * in the new range. 1554 */ 1555 if ((new->mb_flags & MAC_BIBA_FLAGS_BOTH) == 1556 MAC_BIBA_FLAGS_BOTH && 1557 !biba_effective_in_range(new, new)) 1558 return (EINVAL); 1559 1560 /* 1561 * To change the Biba effective label on a credential, the 1562 * new effective label must be in the current range. 1563 */ 1564 if (new->mb_flags & MAC_BIBA_FLAG_EFFECTIVE && 1565 !biba_effective_in_range(new, subj)) 1566 return (EPERM); 1567 1568 /* 1569 * To change the Biba range on a credential, the new range 1570 * label must be in the current range. 1571 */ 1572 if (new->mb_flags & MAC_BIBA_FLAG_RANGE && 1573 !biba_range_in_range(new, subj)) 1574 return (EPERM); 1575 1576 /* 1577 * To have EQUAL in any component of the new credential Biba 1578 * label, the subject must already have EQUAL in their label. 1579 */ 1580 if (biba_contains_equal(new)) { 1581 error = biba_subject_privileged(subj); 1582 if (error) 1583 return (error); 1584 } 1585 } 1586 1587 return (0); 1588} 1589 1590static int 1591biba_cred_check_visible(struct ucred *u1, struct ucred *u2) 1592{ 1593 struct mac_biba *subj, *obj; 1594 1595 if (!biba_enabled) 1596 return (0); 1597 1598 subj = SLOT(u1->cr_label); 1599 obj = SLOT(u2->cr_label); 1600 1601 /* XXX: range */ 1602 if (!biba_dominate_effective(obj, subj)) 1603 return (ESRCH); 1604 1605 return (0); 1606} 1607 1608static int 1609biba_ifnet_check_relabel(struct ucred *cred, struct ifnet *ifp, 1610 struct label *ifplabel, struct label *newlabel) 1611{ 1612 struct mac_biba *subj, *new; 1613 int error; 1614 1615 subj = SLOT(cred->cr_label); 1616 new = SLOT(newlabel); 1617 1618 /* 1619 * If there is a Biba label update for the interface, it may be an 1620 * update of the effective, range, or both. 1621 */ 1622 error = biba_atmostflags(new, MAC_BIBA_FLAGS_BOTH); 1623 if (error) 1624 return (error); 1625 1626 /* 1627 * Relabling network interfaces requires Biba privilege. 1628 */ 1629 error = biba_subject_privileged(subj); 1630 if (error) 1631 return (error); 1632 1633 return (0); 1634} 1635 1636static int 1637biba_ifnet_check_transmit(struct ifnet *ifp, struct label *ifplabel, 1638 struct mbuf *m, struct label *mlabel) 1639{ 1640 struct mac_biba *p, *i; 1641 1642 if (!biba_enabled) 1643 return (0); 1644 1645 p = SLOT(mlabel); 1646 i = SLOT(ifplabel); 1647 1648 return (biba_effective_in_range(p, i) ? 0 : EACCES); 1649} 1650 1651static int 1652biba_inpcb_check_deliver(struct inpcb *inp, struct label *inplabel, 1653 struct mbuf *m, struct label *mlabel) 1654{ 1655 struct mac_biba *p, *i; 1656 1657 if (!biba_enabled) 1658 return (0); 1659 1660 p = SLOT(mlabel); 1661 i = SLOT(inplabel); 1662 1663 return (biba_equal_effective(p, i) ? 0 : EACCES); 1664} 1665 1666static int 1667biba_sysvmsq_check_msgrcv(struct ucred *cred, struct msg *msgptr, 1668 struct label *msglabel) 1669{ 1670 struct mac_biba *subj, *obj; 1671 1672 if (!biba_enabled) 1673 return (0); 1674 1675 subj = SLOT(cred->cr_label); 1676 obj = SLOT(msglabel); 1677 1678 if (!biba_dominate_effective(obj, subj)) 1679 return (EACCES); 1680 1681 return (0); 1682} 1683 1684static int 1685biba_sysvmsq_check_msgrmid(struct ucred *cred, struct msg *msgptr, 1686 struct label *msglabel) 1687{ 1688 struct mac_biba *subj, *obj; 1689 1690 if (!biba_enabled) 1691 return (0); 1692 1693 subj = SLOT(cred->cr_label); 1694 obj = SLOT(msglabel); 1695 1696 if (!biba_dominate_effective(subj, obj)) 1697 return (EACCES); 1698 1699 return (0); 1700} 1701 1702static int 1703biba_sysvmsq_check_msqget(struct ucred *cred, struct msqid_kernel *msqkptr, 1704 struct label *msqklabel) 1705{ 1706 struct mac_biba *subj, *obj; 1707 1708 if (!biba_enabled) 1709 return (0); 1710 1711 subj = SLOT(cred->cr_label); 1712 obj = SLOT(msqklabel); 1713 1714 if (!biba_dominate_effective(obj, subj)) 1715 return (EACCES); 1716 1717 return (0); 1718} 1719 1720static int 1721biba_sysvmsq_check_msqsnd(struct ucred *cred, struct msqid_kernel *msqkptr, 1722 struct label *msqklabel) 1723{ 1724 struct mac_biba *subj, *obj; 1725 1726 if (!biba_enabled) 1727 return (0); 1728 1729 subj = SLOT(cred->cr_label); 1730 obj = SLOT(msqklabel); 1731 1732 if (!biba_dominate_effective(subj, obj)) 1733 return (EACCES); 1734 1735 return (0); 1736} 1737 1738static int 1739biba_sysvmsq_check_msqrcv(struct ucred *cred, struct msqid_kernel *msqkptr, 1740 struct label *msqklabel) 1741{ 1742 struct mac_biba *subj, *obj; 1743 1744 if (!biba_enabled) 1745 return (0); 1746 1747 subj = SLOT(cred->cr_label); 1748 obj = SLOT(msqklabel); 1749 1750 if (!biba_dominate_effective(obj, subj)) 1751 return (EACCES); 1752 1753 return (0); 1754} 1755 1756static int 1757biba_sysvmsq_check_msqctl(struct ucred *cred, struct msqid_kernel *msqkptr, 1758 struct label *msqklabel, int cmd) 1759{ 1760 struct mac_biba *subj, *obj; 1761 1762 if (!biba_enabled) 1763 return (0); 1764 1765 subj = SLOT(cred->cr_label); 1766 obj = SLOT(msqklabel); 1767 1768 switch(cmd) { 1769 case IPC_RMID: 1770 case IPC_SET: 1771 if (!biba_dominate_effective(subj, obj)) 1772 return (EACCES); 1773 break; 1774 1775 case IPC_STAT: 1776 if (!biba_dominate_effective(obj, subj)) 1777 return (EACCES); 1778 break; 1779 1780 default: 1781 return (EACCES); 1782 } 1783 1784 return (0); 1785} 1786 1787static int 1788biba_sysvsem_check_semctl(struct ucred *cred, struct semid_kernel *semakptr, 1789 struct label *semaklabel, int cmd) 1790{ 1791 struct mac_biba *subj, *obj; 1792 1793 if (!biba_enabled) 1794 return (0); 1795 1796 subj = SLOT(cred->cr_label); 1797 obj = SLOT(semaklabel); 1798 1799 switch(cmd) { 1800 case IPC_RMID: 1801 case IPC_SET: 1802 case SETVAL: 1803 case SETALL: 1804 if (!biba_dominate_effective(subj, obj)) 1805 return (EACCES); 1806 break; 1807 1808 case IPC_STAT: 1809 case GETVAL: 1810 case GETPID: 1811 case GETNCNT: 1812 case GETZCNT: 1813 case GETALL: 1814 if (!biba_dominate_effective(obj, subj)) 1815 return (EACCES); 1816 break; 1817 1818 default: 1819 return (EACCES); 1820 } 1821 1822 return (0); 1823} 1824 1825static int 1826biba_sysvsem_check_semget(struct ucred *cred, struct semid_kernel *semakptr, 1827 struct label *semaklabel) 1828{ 1829 struct mac_biba *subj, *obj; 1830 1831 if (!biba_enabled) 1832 return (0); 1833 1834 subj = SLOT(cred->cr_label); 1835 obj = SLOT(semaklabel); 1836 1837 if (!biba_dominate_effective(obj, subj)) 1838 return (EACCES); 1839 1840 return (0); 1841} 1842 1843static int 1844biba_sysvsem_check_semop(struct ucred *cred, struct semid_kernel *semakptr, 1845 struct label *semaklabel, size_t accesstype) 1846{ 1847 struct mac_biba *subj, *obj; 1848 1849 if (!biba_enabled) 1850 return (0); 1851 1852 subj = SLOT(cred->cr_label); 1853 obj = SLOT(semaklabel); 1854 1855 if (accesstype & SEM_R) 1856 if (!biba_dominate_effective(obj, subj)) 1857 return (EACCES); 1858 1859 if (accesstype & SEM_A) 1860 if (!biba_dominate_effective(subj, obj)) 1861 return (EACCES); 1862 1863 return (0); 1864} 1865 1866static int 1867biba_sysvshm_check_shmat(struct ucred *cred, struct shmid_kernel *shmsegptr, 1868 struct label *shmseglabel, int shmflg) 1869{ 1870 struct mac_biba *subj, *obj; 1871 1872 if (!biba_enabled) 1873 return (0); 1874 1875 subj = SLOT(cred->cr_label); 1876 obj = SLOT(shmseglabel); 1877 1878 if (!biba_dominate_effective(obj, subj)) 1879 return (EACCES); 1880 if ((shmflg & SHM_RDONLY) == 0) { 1881 if (!biba_dominate_effective(subj, obj)) 1882 return (EACCES); 1883 } 1884 1885 return (0); 1886} 1887 1888static int 1889biba_sysvshm_check_shmctl(struct ucred *cred, struct shmid_kernel *shmsegptr, 1890 struct label *shmseglabel, int cmd) 1891{ 1892 struct mac_biba *subj, *obj; 1893 1894 if (!biba_enabled) 1895 return (0); 1896 1897 subj = SLOT(cred->cr_label); 1898 obj = SLOT(shmseglabel); 1899 1900 switch(cmd) { 1901 case IPC_RMID: 1902 case IPC_SET: 1903 if (!biba_dominate_effective(subj, obj)) 1904 return (EACCES); 1905 break; 1906 1907 case IPC_STAT: 1908 case SHM_STAT: 1909 if (!biba_dominate_effective(obj, subj)) 1910 return (EACCES); 1911 break; 1912 1913 default: 1914 return (EACCES); 1915 } 1916 1917 return (0); 1918} 1919 1920static int 1921biba_sysvshm_check_shmget(struct ucred *cred, struct shmid_kernel *shmsegptr, 1922 struct label *shmseglabel, int shmflg) 1923{ 1924 struct mac_biba *subj, *obj; 1925 1926 if (!biba_enabled) 1927 return (0); 1928 1929 subj = SLOT(cred->cr_label); 1930 obj = SLOT(shmseglabel); 1931 1932 if (!biba_dominate_effective(obj, subj)) 1933 return (EACCES); 1934 1935 return (0); 1936} 1937 1938static int 1939biba_kld_check_load(struct ucred *cred, struct vnode *vp, 1940 struct label *vplabel) 1941{ 1942 struct mac_biba *subj, *obj; 1943 int error; 1944 1945 if (!biba_enabled) 1946 return (0); 1947 1948 subj = SLOT(cred->cr_label); 1949 1950 error = biba_subject_privileged(subj); 1951 if (error) 1952 return (error); 1953 1954 obj = SLOT(vplabel); 1955 if (!biba_high_effective(obj)) 1956 return (EACCES); 1957 1958 return (0); 1959} 1960 1961static int 1962biba_mount_check_stat(struct ucred *cred, struct mount *mp, 1963 struct label *mplabel) 1964{ 1965 struct mac_biba *subj, *obj; 1966 1967 if (!biba_enabled) 1968 return (0); 1969 1970 subj = SLOT(cred->cr_label); 1971 obj = SLOT(mplabel); 1972 1973 if (!biba_dominate_effective(obj, subj)) 1974 return (EACCES); 1975 1976 return (0); 1977} 1978 1979static int 1980biba_pipe_check_ioctl(struct ucred *cred, struct pipepair *pp, 1981 struct label *pplabel, unsigned long cmd, void /* caddr_t */ *data) 1982{ 1983 1984 if(!biba_enabled) 1985 return (0); 1986 1987 /* XXX: This will be implemented soon... */ 1988 1989 return (0); 1990} 1991 1992static int 1993biba_pipe_check_poll(struct ucred *cred, struct pipepair *pp, 1994 struct label *pplabel) 1995{ 1996 struct mac_biba *subj, *obj; 1997 1998 if (!biba_enabled) 1999 return (0); 2000 2001 subj = SLOT(cred->cr_label); 2002 obj = SLOT(pplabel); 2003 2004 if (!biba_dominate_effective(obj, subj)) 2005 return (EACCES); 2006 2007 return (0); 2008} 2009 2010static int 2011biba_pipe_check_read(struct ucred *cred, struct pipepair *pp, 2012 struct label *pplabel) 2013{ 2014 struct mac_biba *subj, *obj; 2015 2016 if (!biba_enabled) 2017 return (0); 2018 2019 subj = SLOT(cred->cr_label); 2020 obj = SLOT(pplabel); 2021 2022 if (!biba_dominate_effective(obj, subj)) 2023 return (EACCES); 2024 2025 return (0); 2026} 2027 2028static int 2029biba_pipe_check_relabel(struct ucred *cred, struct pipepair *pp, 2030 struct label *pplabel, struct label *newlabel) 2031{ 2032 struct mac_biba *subj, *obj, *new; 2033 int error; 2034 2035 new = SLOT(newlabel); 2036 subj = SLOT(cred->cr_label); 2037 obj = SLOT(pplabel); 2038 2039 /* 2040 * If there is a Biba label update for a pipe, it must be a effective 2041 * update. 2042 */ 2043 error = biba_atmostflags(new, MAC_BIBA_FLAG_EFFECTIVE); 2044 if (error) 2045 return (error); 2046 2047 /* 2048 * To perform a relabel of a pipe (Biba label or not), Biba must 2049 * authorize the relabel. 2050 */ 2051 if (!biba_effective_in_range(obj, subj)) 2052 return (EPERM); 2053 2054 /* 2055 * If the Biba label is to be changed, authorize as appropriate. 2056 */ 2057 if (new->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) { 2058 /* 2059 * To change the Biba label on a pipe, the new pipe label 2060 * must be in the subject range. 2061 */ 2062 if (!biba_effective_in_range(new, subj)) 2063 return (EPERM); 2064 2065 /* 2066 * To change the Biba label on a pipe to be EQUAL, the 2067 * subject must have appropriate privilege. 2068 */ 2069 if (biba_contains_equal(new)) { 2070 error = biba_subject_privileged(subj); 2071 if (error) 2072 return (error); 2073 } 2074 } 2075 2076 return (0); 2077} 2078 2079static int 2080biba_pipe_check_stat(struct ucred *cred, struct pipepair *pp, 2081 struct label *pplabel) 2082{ 2083 struct mac_biba *subj, *obj; 2084 2085 if (!biba_enabled) 2086 return (0); 2087 2088 subj = SLOT(cred->cr_label); 2089 obj = SLOT(pplabel); 2090 2091 if (!biba_dominate_effective(obj, subj)) 2092 return (EACCES); 2093 2094 return (0); 2095} 2096 2097static int 2098biba_pipe_check_write(struct ucred *cred, struct pipepair *pp, 2099 struct label *pplabel) 2100{ 2101 struct mac_biba *subj, *obj; 2102 2103 if (!biba_enabled) 2104 return (0); 2105 2106 subj = SLOT(cred->cr_label); 2107 obj = SLOT(pplabel); 2108 2109 if (!biba_dominate_effective(subj, obj)) 2110 return (EACCES); 2111 2112 return (0); 2113} 2114 2115static int 2116biba_posixsem_check_write(struct ucred *cred, struct ksem *ks, 2117 struct label *kslabel) 2118{ 2119 struct mac_biba *subj, *obj; 2120 2121 if (!biba_enabled) 2122 return (0); 2123 2124 subj = SLOT(cred->cr_label); 2125 obj = SLOT(kslabel); 2126 2127 if (!biba_dominate_effective(subj, obj)) 2128 return (EACCES); 2129 2130 return (0); 2131} 2132 2133static int 2134biba_posixsem_check_rdonly(struct ucred *cred, struct ksem *ks, 2135 struct label *kslabel) 2136{ 2137 struct mac_biba *subj, *obj; 2138 2139 if (!biba_enabled) 2140 return (0); 2141 2142 subj = SLOT(cred->cr_label); 2143 obj = SLOT(kslabel); 2144 2145 if (!biba_dominate_effective(obj, subj)) 2146 return (EACCES); 2147 2148 return (0); 2149} 2150 2151static int 2152biba_proc_check_debug(struct ucred *cred, struct proc *p) 2153{ 2154 struct mac_biba *subj, *obj; 2155 2156 if (!biba_enabled) 2157 return (0); 2158 2159 subj = SLOT(cred->cr_label); 2160 obj = SLOT(p->p_ucred->cr_label); 2161 2162 /* XXX: range checks */ 2163 if (!biba_dominate_effective(obj, subj)) 2164 return (ESRCH); 2165 if (!biba_dominate_effective(subj, obj)) 2166 return (EACCES); 2167 2168 return (0); 2169} 2170 2171static int 2172biba_proc_check_sched(struct ucred *cred, struct proc *p) 2173{ 2174 struct mac_biba *subj, *obj; 2175 2176 if (!biba_enabled) 2177 return (0); 2178 2179 subj = SLOT(cred->cr_label); 2180 obj = SLOT(p->p_ucred->cr_label); 2181 2182 /* XXX: range checks */ 2183 if (!biba_dominate_effective(obj, subj)) 2184 return (ESRCH); 2185 if (!biba_dominate_effective(subj, obj)) 2186 return (EACCES); 2187 2188 return (0); 2189} 2190 2191static int 2192biba_proc_check_signal(struct ucred *cred, struct proc *p, int signum) 2193{ 2194 struct mac_biba *subj, *obj; 2195 2196 if (!biba_enabled) 2197 return (0); 2198 2199 subj = SLOT(cred->cr_label); 2200 obj = SLOT(p->p_ucred->cr_label); 2201 2202 /* XXX: range checks */ 2203 if (!biba_dominate_effective(obj, subj)) 2204 return (ESRCH); 2205 if (!biba_dominate_effective(subj, obj)) 2206 return (EACCES); 2207 2208 return (0); 2209} 2210 2211static int 2212biba_socket_check_deliver(struct socket *so, struct label *solabel, 2213 struct mbuf *m, struct label *mlabel) 2214{ 2215 struct mac_biba *p, *s; 2216 2217 if (!biba_enabled) 2218 return (0); 2219 2220 p = SLOT(mlabel); 2221 s = SLOT(solabel); 2222 2223 return (biba_equal_effective(p, s) ? 0 : EACCES); 2224} 2225 2226static int 2227biba_socket_check_relabel(struct ucred *cred, struct socket *so, 2228 struct label *solabel, struct label *newlabel) 2229{ 2230 struct mac_biba *subj, *obj, *new; 2231 int error; 2232 2233 new = SLOT(newlabel); 2234 subj = SLOT(cred->cr_label); 2235 obj = SLOT(solabel); 2236 2237 /* 2238 * If there is a Biba label update for the socket, it may be an 2239 * update of effective. 2240 */ 2241 error = biba_atmostflags(new, MAC_BIBA_FLAG_EFFECTIVE); 2242 if (error) 2243 return (error); 2244 2245 /* 2246 * To relabel a socket, the old socket effective must be in the 2247 * subject range. 2248 */ 2249 if (!biba_effective_in_range(obj, subj)) 2250 return (EPERM); 2251 2252 /* 2253 * If the Biba label is to be changed, authorize as appropriate. 2254 */ 2255 if (new->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) { 2256 /* 2257 * To relabel a socket, the new socket effective must be in 2258 * the subject range. 2259 */ 2260 if (!biba_effective_in_range(new, subj)) 2261 return (EPERM); 2262 2263 /* 2264 * To change the Biba label on the socket to contain EQUAL, 2265 * the subject must have appropriate privilege. 2266 */ 2267 if (biba_contains_equal(new)) { 2268 error = biba_subject_privileged(subj); 2269 if (error) 2270 return (error); 2271 } 2272 } 2273 2274 return (0); 2275} 2276 2277static int 2278biba_socket_check_visible(struct ucred *cred, struct socket *so, 2279 struct label *solabel) 2280{ 2281 struct mac_biba *subj, *obj; 2282 2283 if (!biba_enabled) 2284 return (0); 2285 2286 subj = SLOT(cred->cr_label); 2287 obj = SLOT(solabel); 2288 2289 if (!biba_dominate_effective(obj, subj)) 2290 return (ENOENT); 2291 2292 return (0); 2293} 2294 2295/* 2296 * Some system privileges are allowed regardless of integrity grade; others 2297 * are allowed only when running with privilege with respect to the Biba 2298 * policy as they might otherwise allow bypassing of the integrity policy. 2299 */ 2300static int 2301biba_priv_check(struct ucred *cred, int priv) 2302{ 2303 struct mac_biba *subj; 2304 int error; 2305 2306 if (!biba_enabled) 2307 return (0); 2308 2309 /* 2310 * Exempt only specific privileges from the Biba integrity policy. 2311 */ 2312 switch (priv) { 2313 case PRIV_KTRACE: 2314 case PRIV_MSGBUF: 2315 2316 /* 2317 * Allow processes to manipulate basic process audit properties, and 2318 * to submit audit records. 2319 */ 2320 case PRIV_AUDIT_GETAUDIT: 2321 case PRIV_AUDIT_SETAUDIT: 2322 case PRIV_AUDIT_SUBMIT: 2323 2324 /* 2325 * Allow processes to manipulate their regular UNIX credentials. 2326 */ 2327 case PRIV_CRED_SETUID: 2328 case PRIV_CRED_SETEUID: 2329 case PRIV_CRED_SETGID: 2330 case PRIV_CRED_SETEGID: 2331 case PRIV_CRED_SETGROUPS: 2332 case PRIV_CRED_SETREUID: 2333 case PRIV_CRED_SETREGID: 2334 case PRIV_CRED_SETRESUID: 2335 case PRIV_CRED_SETRESGID: 2336 2337 /* 2338 * Allow processes to perform system monitoring. 2339 */ 2340 case PRIV_SEEOTHERGIDS: 2341 case PRIV_SEEOTHERUIDS: 2342 break; 2343 2344 /* 2345 * Allow access to general process debugging facilities. We 2346 * separately control debugging based on MAC label. 2347 */ 2348 case PRIV_DEBUG_DIFFCRED: 2349 case PRIV_DEBUG_SUGID: 2350 case PRIV_DEBUG_UNPRIV: 2351 2352 /* 2353 * Allow manipulating jails. 2354 */ 2355 case PRIV_JAIL_ATTACH: 2356 2357 /* 2358 * Allow privilege with respect to the Partition policy, but not the 2359 * Privs policy. 2360 */ 2361 case PRIV_MAC_PARTITION: 2362 2363 /* 2364 * Allow privilege with respect to process resource limits and login 2365 * context. 2366 */ 2367 case PRIV_PROC_LIMIT: 2368 case PRIV_PROC_SETLOGIN: 2369 case PRIV_PROC_SETRLIMIT: 2370 2371 /* 2372 * Allow System V and POSIX IPC privileges. 2373 */ 2374 case PRIV_IPC_READ: 2375 case PRIV_IPC_WRITE: 2376 case PRIV_IPC_ADMIN: 2377 case PRIV_IPC_MSGSIZE: 2378 case PRIV_MQ_ADMIN: 2379 2380 /* 2381 * Allow certain scheduler manipulations -- possibly this should be 2382 * controlled by more fine-grained policy, as potentially low 2383 * integrity processes can deny CPU to higher integrity ones. 2384 */ 2385 case PRIV_SCHED_DIFFCRED: 2386 case PRIV_SCHED_SETPRIORITY: 2387 case PRIV_SCHED_RTPRIO: 2388 case PRIV_SCHED_SETPOLICY: 2389 case PRIV_SCHED_SET: 2390 case PRIV_SCHED_SETPARAM: 2391 2392 /* 2393 * More IPC privileges. 2394 */ 2395 case PRIV_SEM_WRITE: 2396 2397 /* 2398 * Allow signaling privileges subject to integrity policy. 2399 */ 2400 case PRIV_SIGNAL_DIFFCRED: 2401 case PRIV_SIGNAL_SUGID: 2402 2403 /* 2404 * Allow access to only limited sysctls from lower integrity levels; 2405 * piggy-back on the Jail definition. 2406 */ 2407 case PRIV_SYSCTL_WRITEJAIL: 2408 2409 /* 2410 * Allow TTY-based privileges, subject to general device access using 2411 * labels on TTY device nodes, but not console privilege. 2412 */ 2413 case PRIV_TTY_DRAINWAIT: 2414 case PRIV_TTY_DTRWAIT: 2415 case PRIV_TTY_EXCLUSIVE: 2416 case PRIV_TTY_PRISON: 2417 case PRIV_TTY_STI: 2418 case PRIV_TTY_SETA: 2419 2420 /* 2421 * Grant most VFS privileges, as almost all are in practice bounded 2422 * by more specific checks using labels. 2423 */ 2424 case PRIV_VFS_READ: 2425 case PRIV_VFS_WRITE: 2426 case PRIV_VFS_ADMIN: 2427 case PRIV_VFS_EXEC: 2428 case PRIV_VFS_LOOKUP: 2429 case PRIV_VFS_CHFLAGS_DEV: 2430 case PRIV_VFS_CHOWN: 2431 case PRIV_VFS_CHROOT: 2432 case PRIV_VFS_RETAINSUGID: 2433 case PRIV_VFS_EXCEEDQUOTA: 2434 case PRIV_VFS_FCHROOT: 2435 case PRIV_VFS_FHOPEN: 2436 case PRIV_VFS_FHSTATFS: 2437 case PRIV_VFS_GENERATION: 2438 case PRIV_VFS_GETFH: 2439 case PRIV_VFS_GETQUOTA: 2440 case PRIV_VFS_LINK: 2441 case PRIV_VFS_MOUNT: 2442 case PRIV_VFS_MOUNT_OWNER: 2443 case PRIV_VFS_MOUNT_PERM: 2444 case PRIV_VFS_MOUNT_SUIDDIR: 2445 case PRIV_VFS_MOUNT_NONUSER: 2446 case PRIV_VFS_SETGID: 2447 case PRIV_VFS_STICKYFILE: 2448 case PRIV_VFS_SYSFLAGS: 2449 case PRIV_VFS_UNMOUNT: 2450 2451 /* 2452 * Allow VM privileges; it would be nice if these were subject to 2453 * resource limits. 2454 */ 2455 case PRIV_VM_MADV_PROTECT: 2456 case PRIV_VM_MLOCK: 2457 case PRIV_VM_MUNLOCK: 2458 2459 /* 2460 * Allow some but not all network privileges. In general, dont allow 2461 * reconfiguring the network stack, just normal use. 2462 */ 2463 case PRIV_NETATALK_RESERVEDPORT: 2464 case PRIV_NETINET_RESERVEDPORT: 2465 case PRIV_NETINET_RAW: 2466 case PRIV_NETINET_REUSEPORT: 2467 case PRIV_NETIPX_RESERVEDPORT: 2468 case PRIV_NETIPX_RAW: 2469 break; 2470 2471 /* 2472 * All remaining system privileges are allow only if the process 2473 * holds privilege with respect to the Biba policy. 2474 */ 2475 default: 2476 subj = SLOT(cred->cr_label); 2477 error = biba_subject_privileged(subj); 2478 if (error) 2479 return (error); 2480 } 2481 return (0); 2482} 2483 2484static int 2485biba_system_check_acct(struct ucred *cred, struct vnode *vp, 2486 struct label *vplabel) 2487{ 2488 struct mac_biba *subj, *obj; 2489 int error; 2490 2491 if (!biba_enabled) 2492 return (0); 2493 2494 subj = SLOT(cred->cr_label); 2495 2496 error = biba_subject_privileged(subj); 2497 if (error) 2498 return (error); 2499 2500 if (vplabel == NULL) 2501 return (0); 2502 2503 obj = SLOT(vplabel); 2504 if (!biba_high_effective(obj)) 2505 return (EACCES); 2506 2507 return (0); 2508} 2509 2510static int 2511biba_system_check_auditctl(struct ucred *cred, struct vnode *vp, 2512 struct label *vplabel) 2513{ 2514 struct mac_biba *subj, *obj; 2515 int error; 2516 2517 if (!biba_enabled) 2518 return (0); 2519 2520 subj = SLOT(cred->cr_label); 2521 2522 error = biba_subject_privileged(subj); 2523 if (error) 2524 return (error); 2525 2526 if (vplabel == NULL) 2527 return (0); 2528 2529 obj = SLOT(vplabel); 2530 if (!biba_high_effective(obj)) 2531 return (EACCES); 2532 2533 return (0); 2534} 2535 2536static int 2537biba_system_check_auditon(struct ucred *cred, int cmd) 2538{ 2539 struct mac_biba *subj; 2540 int error; 2541 2542 if (!biba_enabled) 2543 return (0); 2544 2545 subj = SLOT(cred->cr_label); 2546 2547 error = biba_subject_privileged(subj); 2548 if (error) 2549 return (error); 2550 2551 return (0); 2552} 2553 2554static int 2555biba_system_check_swapon(struct ucred *cred, struct vnode *vp, 2556 struct label *vplabel) 2557{ 2558 struct mac_biba *subj, *obj; 2559 int error; 2560 2561 if (!biba_enabled) 2562 return (0); 2563 2564 subj = SLOT(cred->cr_label); 2565 obj = SLOT(vplabel); 2566 2567 error = biba_subject_privileged(subj); 2568 if (error) 2569 return (error); 2570 2571 if (!biba_high_effective(obj)) 2572 return (EACCES); 2573 2574 return (0); 2575} 2576 2577static int 2578biba_system_check_swapoff(struct ucred *cred, struct vnode *vp, 2579 struct label *label) 2580{ 2581 struct mac_biba *subj; 2582 int error; 2583 2584 if (!biba_enabled) 2585 return (0); 2586 2587 subj = SLOT(cred->cr_label); 2588 2589 error = biba_subject_privileged(subj); 2590 if (error) 2591 return (error); 2592 2593 return (0); 2594} 2595 2596static int 2597biba_system_check_sysctl(struct ucred *cred, struct sysctl_oid *oidp, 2598 void *arg1, int arg2, struct sysctl_req *req) 2599{ 2600 struct mac_biba *subj; 2601 int error; 2602 2603 if (!biba_enabled) 2604 return (0); 2605 2606 subj = SLOT(cred->cr_label); 2607 2608 /* 2609 * Treat sysctl variables without CTLFLAG_ANYBODY flag as biba/high, 2610 * but also require privilege to change them. 2611 */ 2612 if (req->newptr != NULL && (oidp->oid_kind & CTLFLAG_ANYBODY) == 0) { 2613 if (!biba_subject_dominate_high(subj)) 2614 return (EACCES); 2615 2616 error = biba_subject_privileged(subj); 2617 if (error) 2618 return (error); 2619 } 2620 2621 return (0); 2622} 2623 2624static int 2625biba_vnode_check_chdir(struct ucred *cred, struct vnode *dvp, 2626 struct label *dvplabel) 2627{ 2628 struct mac_biba *subj, *obj; 2629 2630 if (!biba_enabled) 2631 return (0); 2632 2633 subj = SLOT(cred->cr_label); 2634 obj = SLOT(dvplabel); 2635 2636 if (!biba_dominate_effective(obj, subj)) 2637 return (EACCES); 2638 2639 return (0); 2640} 2641 2642static int 2643biba_vnode_check_chroot(struct ucred *cred, struct vnode *dvp, 2644 struct label *dvplabel) 2645{ 2646 struct mac_biba *subj, *obj; 2647 2648 if (!biba_enabled) 2649 return (0); 2650 2651 subj = SLOT(cred->cr_label); 2652 obj = SLOT(dvplabel); 2653 2654 if (!biba_dominate_effective(obj, subj)) 2655 return (EACCES); 2656 2657 return (0); 2658} 2659 2660static int 2661biba_vnode_check_create(struct ucred *cred, struct vnode *dvp, 2662 struct label *dvplabel, struct componentname *cnp, struct vattr *vap) 2663{ 2664 struct mac_biba *subj, *obj; 2665 2666 if (!biba_enabled) 2667 return (0); 2668 2669 subj = SLOT(cred->cr_label); 2670 obj = SLOT(dvplabel); 2671 2672 if (!biba_dominate_effective(subj, obj)) 2673 return (EACCES); 2674 2675 return (0); 2676} 2677 2678static int 2679biba_vnode_check_deleteacl(struct ucred *cred, struct vnode *vp, 2680 struct label *vplabel, acl_type_t type) 2681{ 2682 struct mac_biba *subj, *obj; 2683 2684 if (!biba_enabled) 2685 return (0); 2686 2687 subj = SLOT(cred->cr_label); 2688 obj = SLOT(vplabel); 2689 2690 if (!biba_dominate_effective(subj, obj)) 2691 return (EACCES); 2692 2693 return (0); 2694} 2695 2696static int 2697biba_vnode_check_deleteextattr(struct ucred *cred, struct vnode *vp, 2698 struct label *vplabel, int attrnamespace, const char *name) 2699{ 2700 struct mac_biba *subj, *obj; 2701 2702 if (!biba_enabled) 2703 return (0); 2704 2705 subj = SLOT(cred->cr_label); 2706 obj = SLOT(vplabel); 2707 2708 if (!biba_dominate_effective(subj, obj)) 2709 return (EACCES); 2710 2711 return (0); 2712} 2713 2714static int 2715biba_vnode_check_exec(struct ucred *cred, struct vnode *vp, 2716 struct label *vplabel, struct image_params *imgp, 2717 struct label *execlabel) 2718{ 2719 struct mac_biba *subj, *obj, *exec; 2720 int error; 2721 2722 if (execlabel != NULL) { 2723 /* 2724 * We currently don't permit labels to be changed at 2725 * exec-time as part of Biba, so disallow non-NULL Biba label 2726 * elements in the execlabel. 2727 */ 2728 exec = SLOT(execlabel); 2729 error = biba_atmostflags(exec, 0); 2730 if (error) 2731 return (error); 2732 } 2733 2734 if (!biba_enabled) 2735 return (0); 2736 2737 subj = SLOT(cred->cr_label); 2738 obj = SLOT(vplabel); 2739 2740 if (!biba_dominate_effective(obj, subj)) 2741 return (EACCES); 2742 2743 return (0); 2744} 2745 2746static int 2747biba_vnode_check_getacl(struct ucred *cred, struct vnode *vp, 2748 struct label *vplabel, acl_type_t type) 2749{ 2750 struct mac_biba *subj, *obj; 2751 2752 if (!biba_enabled) 2753 return (0); 2754 2755 subj = SLOT(cred->cr_label); 2756 obj = SLOT(vplabel); 2757 2758 if (!biba_dominate_effective(obj, subj)) 2759 return (EACCES); 2760 2761 return (0); 2762} 2763 2764static int 2765biba_vnode_check_getextattr(struct ucred *cred, struct vnode *vp, 2766 struct label *vplabel, int attrnamespace, const char *name, 2767 struct uio *uio) 2768{ 2769 struct mac_biba *subj, *obj; 2770 2771 if (!biba_enabled) 2772 return (0); 2773 2774 subj = SLOT(cred->cr_label); 2775 obj = SLOT(vplabel); 2776 2777 if (!biba_dominate_effective(obj, subj)) 2778 return (EACCES); 2779 2780 return (0); 2781} 2782 2783static int 2784biba_vnode_check_link(struct ucred *cred, struct vnode *dvp, 2785 struct label *dvplabel, struct vnode *vp, struct label *vplabel, 2786 struct componentname *cnp) 2787{ 2788 struct mac_biba *subj, *obj; 2789 2790 if (!biba_enabled) 2791 return (0); 2792 2793 subj = SLOT(cred->cr_label); 2794 obj = SLOT(dvplabel); 2795 2796 if (!biba_dominate_effective(subj, obj)) 2797 return (EACCES); 2798 2799 obj = SLOT(vplabel); 2800 2801 if (!biba_dominate_effective(subj, obj)) 2802 return (EACCES); 2803 2804 return (0); 2805} 2806 2807static int 2808biba_vnode_check_listextattr(struct ucred *cred, struct vnode *vp, 2809 struct label *vplabel, int attrnamespace) 2810{ 2811 struct mac_biba *subj, *obj; 2812 2813 if (!biba_enabled) 2814 return (0); 2815 2816 subj = SLOT(cred->cr_label); 2817 obj = SLOT(vplabel); 2818 2819 if (!biba_dominate_effective(obj, subj)) 2820 return (EACCES); 2821 2822 return (0); 2823} 2824 2825static int 2826biba_vnode_check_lookup(struct ucred *cred, struct vnode *dvp, 2827 struct label *dvplabel, struct componentname *cnp) 2828{ 2829 struct mac_biba *subj, *obj; 2830 2831 if (!biba_enabled) 2832 return (0); 2833 2834 subj = SLOT(cred->cr_label); 2835 obj = SLOT(dvplabel); 2836 2837 if (!biba_dominate_effective(obj, subj)) 2838 return (EACCES); 2839 2840 return (0); 2841} 2842 2843static int 2844biba_vnode_check_mmap(struct ucred *cred, struct vnode *vp, 2845 struct label *vplabel, int prot, int flags) 2846{ 2847 struct mac_biba *subj, *obj; 2848 2849 /* 2850 * Rely on the use of open()-time protections to handle 2851 * non-revocation cases. 2852 */ 2853 if (!biba_enabled || !revocation_enabled) 2854 return (0); 2855 2856 subj = SLOT(cred->cr_label); 2857 obj = SLOT(vplabel); 2858 2859 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) { 2860 if (!biba_dominate_effective(obj, subj)) 2861 return (EACCES); 2862 } 2863 if (((prot & VM_PROT_WRITE) != 0) && ((flags & MAP_SHARED) != 0)) { 2864 if (!biba_dominate_effective(subj, obj)) 2865 return (EACCES); 2866 } 2867 2868 return (0); 2869} 2870 2871static int 2872biba_vnode_check_open(struct ucred *cred, struct vnode *vp, 2873 struct label *vplabel, int acc_mode) 2874{ 2875 struct mac_biba *subj, *obj; 2876 2877 if (!biba_enabled) 2878 return (0); 2879 2880 subj = SLOT(cred->cr_label); 2881 obj = SLOT(vplabel); 2882 2883 /* XXX privilege override for admin? */ 2884 if (acc_mode & (VREAD | VEXEC | VSTAT)) { 2885 if (!biba_dominate_effective(obj, subj)) 2886 return (EACCES); 2887 } 2888 if (acc_mode & (VWRITE | VAPPEND | VADMIN)) { 2889 if (!biba_dominate_effective(subj, obj)) 2890 return (EACCES); 2891 } 2892 2893 return (0); 2894} 2895 2896static int 2897biba_vnode_check_poll(struct ucred *active_cred, struct ucred *file_cred, 2898 struct vnode *vp, struct label *vplabel) 2899{ 2900 struct mac_biba *subj, *obj; 2901 2902 if (!biba_enabled || !revocation_enabled) 2903 return (0); 2904 2905 subj = SLOT(active_cred->cr_label); 2906 obj = SLOT(vplabel); 2907 2908 if (!biba_dominate_effective(obj, subj)) 2909 return (EACCES); 2910 2911 return (0); 2912} 2913 2914static int 2915biba_vnode_check_read(struct ucred *active_cred, struct ucred *file_cred, 2916 struct vnode *vp, struct label *vplabel) 2917{ 2918 struct mac_biba *subj, *obj; 2919 2920 if (!biba_enabled || !revocation_enabled) 2921 return (0); 2922 2923 subj = SLOT(active_cred->cr_label); 2924 obj = SLOT(vplabel); 2925 2926 if (!biba_dominate_effective(obj, subj)) 2927 return (EACCES); 2928 2929 return (0); 2930} 2931 2932static int 2933biba_vnode_check_readdir(struct ucred *cred, struct vnode *dvp, 2934 struct label *dvplabel) 2935{ 2936 struct mac_biba *subj, *obj; 2937 2938 if (!biba_enabled) 2939 return (0); 2940 2941 subj = SLOT(cred->cr_label); 2942 obj = SLOT(dvplabel); 2943 2944 if (!biba_dominate_effective(obj, subj)) 2945 return (EACCES); 2946 2947 return (0); 2948} 2949 2950static int 2951biba_vnode_check_readlink(struct ucred *cred, struct vnode *vp, 2952 struct label *vplabel) 2953{ 2954 struct mac_biba *subj, *obj; 2955 2956 if (!biba_enabled) 2957 return (0); 2958 2959 subj = SLOT(cred->cr_label); 2960 obj = SLOT(vplabel); 2961 2962 if (!biba_dominate_effective(obj, subj)) 2963 return (EACCES); 2964 2965 return (0); 2966} 2967 2968static int 2969biba_vnode_check_relabel(struct ucred *cred, struct vnode *vp, 2970 struct label *vplabel, struct label *newlabel) 2971{ 2972 struct mac_biba *old, *new, *subj; 2973 int error; 2974 2975 old = SLOT(vplabel); 2976 new = SLOT(newlabel); 2977 subj = SLOT(cred->cr_label); 2978 2979 /* 2980 * If there is a Biba label update for the vnode, it must be a 2981 * effective label. 2982 */ 2983 error = biba_atmostflags(new, MAC_BIBA_FLAG_EFFECTIVE); 2984 if (error) 2985 return (error); 2986 2987 /* 2988 * To perform a relabel of the vnode (Biba label or not), Biba must 2989 * authorize the relabel. 2990 */ 2991 if (!biba_effective_in_range(old, subj)) 2992 return (EPERM); 2993 2994 /* 2995 * If the Biba label is to be changed, authorize as appropriate. 2996 */ 2997 if (new->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) { 2998 /* 2999 * To change the Biba label on a vnode, the new vnode label 3000 * must be in the subject range. 3001 */ 3002 if (!biba_effective_in_range(new, subj)) 3003 return (EPERM); 3004 3005 /* 3006 * To change the Biba label on the vnode to be EQUAL, the 3007 * subject must have appropriate privilege. 3008 */ 3009 if (biba_contains_equal(new)) { 3010 error = biba_subject_privileged(subj); 3011 if (error) 3012 return (error); 3013 } 3014 } 3015 3016 return (0); 3017} 3018 3019static int 3020biba_vnode_check_rename_from(struct ucred *cred, struct vnode *dvp, 3021 struct label *dvplabel, struct vnode *vp, struct label *vplabel, 3022 struct componentname *cnp) 3023{ 3024 struct mac_biba *subj, *obj; 3025 3026 if (!biba_enabled) 3027 return (0); 3028 3029 subj = SLOT(cred->cr_label); 3030 obj = SLOT(dvplabel); 3031 3032 if (!biba_dominate_effective(subj, obj)) 3033 return (EACCES); 3034 3035 obj = SLOT(vplabel); 3036 3037 if (!biba_dominate_effective(subj, obj)) 3038 return (EACCES); 3039 3040 return (0); 3041} 3042 3043static int 3044biba_vnode_check_rename_to(struct ucred *cred, struct vnode *dvp, 3045 struct label *dvplabel, struct vnode *vp, struct label *vplabel, 3046 int samedir, struct componentname *cnp) 3047{ 3048 struct mac_biba *subj, *obj; 3049 3050 if (!biba_enabled) 3051 return (0); 3052 3053 subj = SLOT(cred->cr_label); 3054 obj = SLOT(dvplabel); 3055 3056 if (!biba_dominate_effective(subj, obj)) 3057 return (EACCES); 3058 3059 if (vp != NULL) { 3060 obj = SLOT(vplabel); 3061 3062 if (!biba_dominate_effective(subj, obj)) 3063 return (EACCES); 3064 } 3065 3066 return (0); 3067} 3068 3069static int 3070biba_vnode_check_revoke(struct ucred *cred, struct vnode *vp, 3071 struct label *vplabel) 3072{ 3073 struct mac_biba *subj, *obj; 3074 3075 if (!biba_enabled) 3076 return (0); 3077 3078 subj = SLOT(cred->cr_label); 3079 obj = SLOT(vplabel); 3080 3081 if (!biba_dominate_effective(subj, obj)) 3082 return (EACCES); 3083 3084 return (0); 3085} 3086 3087static int 3088biba_vnode_check_setacl(struct ucred *cred, struct vnode *vp, 3089 struct label *vplabel, acl_type_t type, struct acl *acl) 3090{ 3091 struct mac_biba *subj, *obj; 3092 3093 if (!biba_enabled) 3094 return (0); 3095 3096 subj = SLOT(cred->cr_label); 3097 obj = SLOT(vplabel); 3098 3099 if (!biba_dominate_effective(subj, obj)) 3100 return (EACCES); 3101 3102 return (0); 3103} 3104 3105static int 3106biba_vnode_check_setextattr(struct ucred *cred, struct vnode *vp, 3107 struct label *vplabel, int attrnamespace, const char *name, 3108 struct uio *uio) 3109{ 3110 struct mac_biba *subj, *obj; 3111 3112 if (!biba_enabled) 3113 return (0); 3114 3115 subj = SLOT(cred->cr_label); 3116 obj = SLOT(vplabel); 3117 3118 if (!biba_dominate_effective(subj, obj)) 3119 return (EACCES); 3120 3121 /* XXX: protect the MAC EA in a special way? */ 3122 3123 return (0); 3124} 3125 3126static int 3127biba_vnode_check_setflags(struct ucred *cred, struct vnode *vp, 3128 struct label *vplabel, u_long flags) 3129{ 3130 struct mac_biba *subj, *obj; 3131 3132 if (!biba_enabled) 3133 return (0); 3134 3135 subj = SLOT(cred->cr_label); 3136 obj = SLOT(vplabel); 3137 3138 if (!biba_dominate_effective(subj, obj)) 3139 return (EACCES); 3140 3141 return (0); 3142} 3143 3144static int 3145biba_vnode_check_setmode(struct ucred *cred, struct vnode *vp, 3146 struct label *vplabel, mode_t mode) 3147{ 3148 struct mac_biba *subj, *obj; 3149 3150 if (!biba_enabled) 3151 return (0); 3152 3153 subj = SLOT(cred->cr_label); 3154 obj = SLOT(vplabel); 3155 3156 if (!biba_dominate_effective(subj, obj)) 3157 return (EACCES); 3158 3159 return (0); 3160} 3161 3162static int 3163biba_vnode_check_setowner(struct ucred *cred, struct vnode *vp, 3164 struct label *vplabel, uid_t uid, gid_t gid) 3165{ 3166 struct mac_biba *subj, *obj; 3167 3168 if (!biba_enabled) 3169 return (0); 3170 3171 subj = SLOT(cred->cr_label); 3172 obj = SLOT(vplabel); 3173 3174 if (!biba_dominate_effective(subj, obj)) 3175 return (EACCES); 3176 3177 return (0); 3178} 3179 3180static int 3181biba_vnode_check_setutimes(struct ucred *cred, struct vnode *vp, 3182 struct label *vplabel, struct timespec atime, struct timespec mtime) 3183{ 3184 struct mac_biba *subj, *obj; 3185 3186 if (!biba_enabled) 3187 return (0); 3188 3189 subj = SLOT(cred->cr_label); 3190 obj = SLOT(vplabel); 3191 3192 if (!biba_dominate_effective(subj, obj)) 3193 return (EACCES); 3194 3195 return (0); 3196} 3197 3198static int 3199biba_vnode_check_stat(struct ucred *active_cred, struct ucred *file_cred, 3200 struct vnode *vp, struct label *vplabel) 3201{ 3202 struct mac_biba *subj, *obj; 3203 3204 if (!biba_enabled) 3205 return (0); 3206 3207 subj = SLOT(active_cred->cr_label); 3208 obj = SLOT(vplabel); 3209 3210 if (!biba_dominate_effective(obj, subj)) 3211 return (EACCES); 3212 3213 return (0); 3214} 3215 3216static int 3217biba_vnode_check_unlink(struct ucred *cred, struct vnode *dvp, 3218 struct label *dvplabel, struct vnode *vp, struct label *vplabel, 3219 struct componentname *cnp) 3220{ 3221 struct mac_biba *subj, *obj; 3222 3223 if (!biba_enabled) 3224 return (0); 3225 3226 subj = SLOT(cred->cr_label); 3227 obj = SLOT(dvplabel); 3228 3229 if (!biba_dominate_effective(subj, obj)) 3230 return (EACCES); 3231 3232 obj = SLOT(vplabel); 3233 3234 if (!biba_dominate_effective(subj, obj)) 3235 return (EACCES); 3236 3237 return (0); 3238} 3239 3240static int 3241biba_vnode_check_write(struct ucred *active_cred, 3242 struct ucred *file_cred, struct vnode *vp, struct label *vplabel) 3243{ 3244 struct mac_biba *subj, *obj; 3245 3246 if (!biba_enabled || !revocation_enabled) 3247 return (0); 3248 3249 subj = SLOT(active_cred->cr_label); 3250 obj = SLOT(vplabel); 3251 3252 if (!biba_dominate_effective(subj, obj)) 3253 return (EACCES); 3254 3255 return (0); 3256} 3257 3258static void 3259biba_syncache_create(struct label *label, struct inpcb *inp) 3260{ 3261 struct mac_biba *source, *dest; 3262 3263 source = SLOT(inp->inp_label); 3264 dest = SLOT(label); 3265 biba_copy_effective(source, dest); 3266} 3267 3268static void 3269biba_syncache_create_mbuf(struct label *sc_label, struct mbuf *m, 3270 struct label *mlabel) 3271{ 3272 struct mac_biba *source, *dest; 3273 3274 source = SLOT(sc_label); 3275 dest = SLOT(mlabel); 3276 biba_copy_effective(source, dest); 3277} 3278 3279static struct mac_policy_ops mac_biba_ops = 3280{ 3281 .mpo_init = biba_init, 3282 .mpo_bpfdesc_init_label = biba_init_label, 3283 .mpo_cred_init_label = biba_init_label, 3284 .mpo_devfs_init_label = biba_init_label, 3285 .mpo_ifnet_init_label = biba_init_label, 3286 .mpo_inpcb_init_label = biba_init_label_waitcheck, 3287 .mpo_syncache_init_label = biba_init_label_waitcheck, 3288 .mpo_sysvmsg_init_label = biba_init_label, 3289 .mpo_sysvmsq_init_label = biba_init_label, 3290 .mpo_sysvsem_init_label = biba_init_label, 3291 .mpo_sysvshm_init_label = biba_init_label, 3292 .mpo_ipq_init_label = biba_init_label_waitcheck, 3293 .mpo_mbuf_init_label = biba_init_label_waitcheck, 3294 .mpo_mount_init_label = biba_init_label, 3295 .mpo_pipe_init_label = biba_init_label, 3296 .mpo_posixsem_init_label = biba_init_label, 3297 .mpo_socket_init_label = biba_init_label_waitcheck, 3298 .mpo_socketpeer_init_label = biba_init_label_waitcheck, 3299 .mpo_syncache_create = biba_syncache_create, 3300 .mpo_vnode_init_label = biba_init_label, 3301 .mpo_bpfdesc_destroy_label = biba_destroy_label, 3302 .mpo_cred_destroy_label = biba_destroy_label, 3303 .mpo_devfs_destroy_label = biba_destroy_label, 3304 .mpo_ifnet_destroy_label = biba_destroy_label, 3305 .mpo_inpcb_destroy_label = biba_destroy_label, 3306 .mpo_syncache_destroy_label = biba_destroy_label, 3307 .mpo_sysvmsg_destroy_label = biba_destroy_label, 3308 .mpo_sysvmsq_destroy_label = biba_destroy_label, 3309 .mpo_sysvsem_destroy_label = biba_destroy_label, 3310 .mpo_sysvshm_destroy_label = biba_destroy_label, 3311 .mpo_ipq_destroy_label = biba_destroy_label, 3312 .mpo_mbuf_destroy_label = biba_destroy_label, 3313 .mpo_mount_destroy_label = biba_destroy_label, 3314 .mpo_pipe_destroy_label = biba_destroy_label, 3315 .mpo_posixsem_destroy_label = biba_destroy_label, 3316 .mpo_socket_destroy_label = biba_destroy_label, 3317 .mpo_socketpeer_destroy_label = biba_destroy_label, 3318 .mpo_vnode_destroy_label = biba_destroy_label, 3319 .mpo_cred_copy_label = biba_copy_label, 3320 .mpo_ifnet_copy_label = biba_copy_label, 3321 .mpo_mbuf_copy_label = biba_copy_label, 3322 .mpo_pipe_copy_label = biba_copy_label, 3323 .mpo_socket_copy_label = biba_copy_label, 3324 .mpo_vnode_copy_label = biba_copy_label, 3325 .mpo_cred_externalize_label = biba_externalize_label, 3326 .mpo_ifnet_externalize_label = biba_externalize_label, 3327 .mpo_pipe_externalize_label = biba_externalize_label, 3328 .mpo_socket_externalize_label = biba_externalize_label, 3329 .mpo_socketpeer_externalize_label = biba_externalize_label, 3330 .mpo_vnode_externalize_label = biba_externalize_label, 3331 .mpo_cred_internalize_label = biba_internalize_label, 3332 .mpo_ifnet_internalize_label = biba_internalize_label, 3333 .mpo_pipe_internalize_label = biba_internalize_label, 3334 .mpo_socket_internalize_label = biba_internalize_label, 3335 .mpo_vnode_internalize_label = biba_internalize_label, 3336 .mpo_devfs_create_device = biba_devfs_create_device, 3337 .mpo_devfs_create_directory = biba_devfs_create_directory, 3338 .mpo_devfs_create_symlink = biba_devfs_create_symlink, 3339 .mpo_mount_create = biba_mount_create, 3340 .mpo_vnode_relabel = biba_vnode_relabel, 3341 .mpo_devfs_update = biba_devfs_update, 3342 .mpo_devfs_vnode_associate = biba_devfs_vnode_associate, 3343 .mpo_vnode_associate_extattr = biba_vnode_associate_extattr, 3344 .mpo_vnode_associate_singlelabel = biba_vnode_associate_singlelabel, 3345 .mpo_vnode_create_extattr = biba_vnode_create_extattr, 3346 .mpo_vnode_setlabel_extattr = biba_vnode_setlabel_extattr, 3347 .mpo_socket_create_mbuf = biba_socket_create_mbuf, 3348 .mpo_syncache_create_mbuf = biba_syncache_create_mbuf, 3349 .mpo_pipe_create = biba_pipe_create, 3350 .mpo_posixsem_create = biba_posixsem_create, 3351 .mpo_socket_create = biba_socket_create, 3352 .mpo_socket_newconn = biba_socket_newconn, 3353 .mpo_pipe_relabel = biba_pipe_relabel, 3354 .mpo_socket_relabel = biba_socket_relabel, 3355 .mpo_socketpeer_set_from_mbuf = biba_socketpeer_set_from_mbuf, 3356 .mpo_socketpeer_set_from_socket = biba_socketpeer_set_from_socket, 3357 .mpo_bpfdesc_create = biba_bpfdesc_create, 3358 .mpo_ipq_reassemble = biba_ipq_reassemble, 3359 .mpo_netinet_fragment = biba_netinet_fragment, 3360 .mpo_ifnet_create = biba_ifnet_create, 3361 .mpo_inpcb_create = biba_inpcb_create, 3362 .mpo_sysvmsg_create = biba_sysvmsg_create, 3363 .mpo_sysvmsq_create = biba_sysvmsq_create, 3364 .mpo_sysvsem_create = biba_sysvsem_create, 3365 .mpo_sysvshm_create = biba_sysvshm_create, 3366 .mpo_ipq_create = biba_ipq_create, 3367 .mpo_inpcb_create_mbuf = biba_inpcb_create_mbuf, 3368 .mpo_bpfdesc_create_mbuf = biba_bpfdesc_create_mbuf, 3369 .mpo_ifnet_create_mbuf = biba_ifnet_create_mbuf, 3370 .mpo_mbuf_create_multicast_encap = biba_mbuf_create_multicast_encap, 3371 .mpo_ipq_match = biba_ipq_match, 3372 .mpo_ifnet_relabel = biba_ifnet_relabel, 3373 .mpo_ipq_update = biba_ipq_update, 3374 .mpo_inpcb_sosetlabel = biba_inpcb_sosetlabel, 3375 .mpo_proc_create_swapper = biba_proc_create_swapper, 3376 .mpo_proc_create_init = biba_proc_create_init, 3377 .mpo_proc_associate_nfsd = biba_proc_associate_nfsd, 3378 .mpo_cred_relabel = biba_cred_relabel, 3379 .mpo_sysvmsg_cleanup = biba_sysvmsg_cleanup, 3380 .mpo_sysvmsq_cleanup = biba_sysvmsq_cleanup, 3381 .mpo_sysvsem_cleanup = biba_sysvsem_cleanup, 3382 .mpo_sysvshm_cleanup = biba_sysvshm_cleanup, 3383 .mpo_bpfdesc_check_receive = biba_bpfdesc_check_receive, 3384 .mpo_cred_check_relabel = biba_cred_check_relabel, 3385 .mpo_cred_check_visible = biba_cred_check_visible, 3386 .mpo_ifnet_check_relabel = biba_ifnet_check_relabel, 3387 .mpo_ifnet_check_transmit = biba_ifnet_check_transmit, 3388 .mpo_inpcb_check_deliver = biba_inpcb_check_deliver, 3389 .mpo_sysvmsq_check_msgrcv = biba_sysvmsq_check_msgrcv, 3390 .mpo_sysvmsq_check_msgrmid = biba_sysvmsq_check_msgrmid, 3391 .mpo_sysvmsq_check_msqget = biba_sysvmsq_check_msqget, 3392 .mpo_sysvmsq_check_msqsnd = biba_sysvmsq_check_msqsnd, 3393 .mpo_sysvmsq_check_msqrcv = biba_sysvmsq_check_msqrcv, 3394 .mpo_sysvmsq_check_msqctl = biba_sysvmsq_check_msqctl, 3395 .mpo_sysvsem_check_semctl = biba_sysvsem_check_semctl, 3396 .mpo_sysvsem_check_semget = biba_sysvsem_check_semget, 3397 .mpo_sysvsem_check_semop = biba_sysvsem_check_semop, 3398 .mpo_sysvshm_check_shmat = biba_sysvshm_check_shmat, 3399 .mpo_sysvshm_check_shmctl = biba_sysvshm_check_shmctl, 3400 .mpo_sysvshm_check_shmget = biba_sysvshm_check_shmget, 3401 .mpo_kld_check_load = biba_kld_check_load, 3402 .mpo_mount_check_stat = biba_mount_check_stat, 3403 .mpo_pipe_check_ioctl = biba_pipe_check_ioctl, 3404 .mpo_pipe_check_poll = biba_pipe_check_poll, 3405 .mpo_pipe_check_read = biba_pipe_check_read, 3406 .mpo_pipe_check_relabel = biba_pipe_check_relabel, 3407 .mpo_pipe_check_stat = biba_pipe_check_stat, 3408 .mpo_pipe_check_write = biba_pipe_check_write, 3409 .mpo_posixsem_check_destroy = biba_posixsem_check_write, 3410 .mpo_posixsem_check_getvalue = biba_posixsem_check_rdonly, 3411 .mpo_posixsem_check_open = biba_posixsem_check_write, 3412 .mpo_posixsem_check_post = biba_posixsem_check_write, 3413 .mpo_posixsem_check_unlink = biba_posixsem_check_write, 3414 .mpo_posixsem_check_wait = biba_posixsem_check_write, 3415 .mpo_proc_check_debug = biba_proc_check_debug, 3416 .mpo_proc_check_sched = biba_proc_check_sched, 3417 .mpo_proc_check_signal = biba_proc_check_signal, 3418 .mpo_socket_check_deliver = biba_socket_check_deliver, 3419 .mpo_socket_check_relabel = biba_socket_check_relabel, 3420 .mpo_socket_check_visible = biba_socket_check_visible, 3421 .mpo_system_check_acct = biba_system_check_acct, 3422 .mpo_system_check_auditctl = biba_system_check_auditctl, 3423 .mpo_system_check_auditon = biba_system_check_auditon, 3424 .mpo_system_check_swapon = biba_system_check_swapon, 3425 .mpo_system_check_swapoff = biba_system_check_swapoff, 3426 .mpo_system_check_sysctl = biba_system_check_sysctl, 3427 .mpo_vnode_check_access = biba_vnode_check_open, 3428 .mpo_vnode_check_chdir = biba_vnode_check_chdir, 3429 .mpo_vnode_check_chroot = biba_vnode_check_chroot, 3430 .mpo_vnode_check_create = biba_vnode_check_create, 3431 .mpo_vnode_check_deleteacl = biba_vnode_check_deleteacl, 3432 .mpo_vnode_check_deleteextattr = biba_vnode_check_deleteextattr, 3433 .mpo_vnode_check_exec = biba_vnode_check_exec, 3434 .mpo_vnode_check_getacl = biba_vnode_check_getacl, 3435 .mpo_vnode_check_getextattr = biba_vnode_check_getextattr, 3436 .mpo_vnode_check_link = biba_vnode_check_link, 3437 .mpo_vnode_check_listextattr = biba_vnode_check_listextattr, 3438 .mpo_vnode_check_lookup = biba_vnode_check_lookup, 3439 .mpo_vnode_check_mmap = biba_vnode_check_mmap, 3440 .mpo_vnode_check_open = biba_vnode_check_open, 3441 .mpo_vnode_check_poll = biba_vnode_check_poll, 3442 .mpo_vnode_check_read = biba_vnode_check_read, 3443 .mpo_vnode_check_readdir = biba_vnode_check_readdir, 3444 .mpo_vnode_check_readlink = biba_vnode_check_readlink, 3445 .mpo_vnode_check_relabel = biba_vnode_check_relabel, 3446 .mpo_vnode_check_rename_from = biba_vnode_check_rename_from, 3447 .mpo_vnode_check_rename_to = biba_vnode_check_rename_to, 3448 .mpo_vnode_check_revoke = biba_vnode_check_revoke, 3449 .mpo_vnode_check_setacl = biba_vnode_check_setacl, 3450 .mpo_vnode_check_setextattr = biba_vnode_check_setextattr, 3451 .mpo_vnode_check_setflags = biba_vnode_check_setflags, 3452 .mpo_vnode_check_setmode = biba_vnode_check_setmode, 3453 .mpo_vnode_check_setowner = biba_vnode_check_setowner, 3454 .mpo_vnode_check_setutimes = biba_vnode_check_setutimes, 3455 .mpo_vnode_check_stat = biba_vnode_check_stat, 3456 .mpo_vnode_check_unlink = biba_vnode_check_unlink, 3457 .mpo_vnode_check_write = biba_vnode_check_write, 3458 .mpo_netatalk_aarp_send = biba_netatalk_aarp_send, 3459 .mpo_netinet_arp_send = biba_netinet_arp_send, 3460 .mpo_netinet_firewall_reply = biba_netinet_firewall_reply, 3461 .mpo_netinet_firewall_send = biba_netinet_firewall_send, 3462 .mpo_netinet_icmp_reply = biba_netinet_icmp_reply, 3463 .mpo_netinet_igmp_send = biba_netinet_igmp_send, 3464 .mpo_netinet6_nd6_send = biba_netinet6_nd6_send, 3465 .mpo_priv_check = biba_priv_check, 3466}; 3467 3468MAC_POLICY_SET(&mac_biba_ops, mac_biba, "TrustedBSD MAC/Biba", 3469 MPC_LOADTIME_FLAG_NOTLATE | MPC_LOADTIME_FLAG_LABELMBUFS, &biba_slot); 3470