mac_mls.c (105656) | mac_mls.c (105696) |
---|---|
1/*- 2 * Copyright (c) 1999, 2000, 2001, 2002 Robert N. M. Watson 3 * Copyright (c) 2001, 2002 Networks Associates Technology, Inc. 4 * All rights reserved. 5 * 6 * This software was developed by Robert Watson for the TrustedBSD Project. 7 * 8 * This software was developed for the FreeBSD Project in part by NAI Labs, --- 20 unchanged lines hidden (view full) --- 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * | 1/*- 2 * Copyright (c) 1999, 2000, 2001, 2002 Robert N. M. Watson 3 * Copyright (c) 2001, 2002 Networks Associates Technology, Inc. 4 * All rights reserved. 5 * 6 * This software was developed by Robert Watson for the TrustedBSD Project. 7 * 8 * This software was developed for the FreeBSD Project in part by NAI Labs, --- 20 unchanged lines hidden (view full) --- 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * |
37 * $FreeBSD: head/sys/security/mac_mls/mac_mls.c 105656 2002-10-21 20:55:39Z rwatson $ | 37 * $FreeBSD: head/sys/security/mac_mls/mac_mls.c 105696 2002-10-22 14:31:34Z rwatson $ |
38 */ 39 40/* 41 * Developed by the TrustedBSD Project. 42 * MLS fixed label mandatory confidentiality policy. 43 */ 44 45#include <sys/types.h> 46#include <sys/param.h> 47#include <sys/acl.h> 48#include <sys/conf.h> 49#include <sys/kernel.h> 50#include <sys/mac.h> 51#include <sys/malloc.h> 52#include <sys/mount.h> 53#include <sys/proc.h> 54#include <sys/systm.h> 55#include <sys/sysproto.h> 56#include <sys/sysent.h> | 38 */ 39 40/* 41 * Developed by the TrustedBSD Project. 42 * MLS fixed label mandatory confidentiality policy. 43 */ 44 45#include <sys/types.h> 46#include <sys/param.h> 47#include <sys/acl.h> 48#include <sys/conf.h> 49#include <sys/kernel.h> 50#include <sys/mac.h> 51#include <sys/malloc.h> 52#include <sys/mount.h> 53#include <sys/proc.h> 54#include <sys/systm.h> 55#include <sys/sysproto.h> 56#include <sys/sysent.h> |
57#include <sys/systm.h> |
|
57#include <sys/vnode.h> 58#include <sys/file.h> 59#include <sys/socket.h> 60#include <sys/socketvar.h> 61#include <sys/pipe.h> 62#include <sys/sysctl.h> 63 64#include <fs/devfs/devfs.h> --- 419 unchanged lines hidden (view full) --- 484static void 485mac_mls_destroy_label(struct label *label) 486{ 487 488 mls_free(SLOT(label)); 489 SLOT(label) = NULL; 490} 491 | 58#include <sys/vnode.h> 59#include <sys/file.h> 60#include <sys/socket.h> 61#include <sys/socketvar.h> 62#include <sys/pipe.h> 63#include <sys/sysctl.h> 64 65#include <fs/devfs/devfs.h> --- 419 unchanged lines hidden (view full) --- 485static void 486mac_mls_destroy_label(struct label *label) 487{ 488 489 mls_free(SLOT(label)); 490 SLOT(label) = NULL; 491} 492 |
493/* 494 * mac_mls_element_to_string() is basically an snprintf wrapper with 495 * the same properties as snprintf(). It returns the length it would 496 * have added to the string in the event the string is too short. 497 */ 498static size_t 499mac_mls_element_to_string(char *string, size_t size, 500 struct mac_mls_element *element) 501{ 502 int pos, bit = 1; 503 504 switch (element->mme_type) { 505 case MAC_MLS_TYPE_HIGH: 506 return (snprintf(string, size, "high")); 507 508 case MAC_MLS_TYPE_LOW: 509 return (snprintf(string, size, "low")); 510 511 case MAC_MLS_TYPE_EQUAL: 512 return (snprintf(string, size, "equal")); 513 514 case MAC_MLS_TYPE_LEVEL: 515 pos = snprintf(string, size, "%d:", element->mme_level); 516 for (bit = 1; bit <= MAC_MLS_MAX_COMPARTMENTS; bit++) { 517 if (MAC_MLS_BIT_TEST(bit, element->mme_compartments)) 518 pos += snprintf(string + pos, size - pos, 519 "%d+", bit); 520 } 521 if (string[pos - 1] == '+' || string[pos - 1] == ':') 522 string[--pos] = NULL; 523 return (pos); 524 525 default: 526 panic("mac_mls_element_to_string: invalid type (%d)", 527 element->mme_type); 528 } 529} 530 531static size_t 532mac_mls_to_string(char *string, size_t size, size_t *caller_len, 533 struct mac_mls *mac_mls) 534{ 535 size_t left, len; 536 char *curptr; 537 538 bzero(string, size); 539 curptr = string; 540 left = size; 541 542 if (mac_mls->mm_flags & MAC_MLS_FLAG_SINGLE) { 543 len = mac_mls_element_to_string(curptr, left, 544 &mac_mls->mm_single); 545 if (len >= left) 546 return (EINVAL); 547 left -= len; 548 curptr += len; 549 } 550 551 if (mac_mls->mm_flags & MAC_MLS_FLAG_RANGE) { 552 len = snprintf(curptr, left, "("); 553 if (len >= left) 554 return (EINVAL); 555 left -= len; 556 curptr += len; 557 558 len = mac_mls_element_to_string(curptr, left, 559 &mac_mls->mm_rangelow); 560 if (len >= left) 561 return (EINVAL); 562 left -= len; 563 curptr += len; 564 565 len = snprintf(curptr, left, "-"); 566 if (len >= left) 567 return (EINVAL); 568 left -= len; 569 curptr += len; 570 571 len = mac_mls_element_to_string(curptr, left, 572 &mac_mls->mm_rangehigh); 573 if (len >= left) 574 return (EINVAL); 575 left -= len; 576 curptr += len; 577 578 len = snprintf(curptr, left, ")"); 579 if (len >= left) 580 return (EINVAL); 581 left -= len; 582 curptr += len; 583 } 584 585 *caller_len = strlen(string); 586 return (0); 587} 588 |
|
492static int | 589static int |
493mac_mls_externalize(struct label *label, struct mac *extmac) | 590mac_mls_externalize_label(struct label *label, char *element_name, 591 char *element_data, size_t size, size_t *len, int *claimed) |
494{ 495 struct mac_mls *mac_mls; | 592{ 593 struct mac_mls *mac_mls; |
594 int error; |
|
496 | 595 |
596 if (strcmp(MAC_MLS_LABEL_NAME, element_name) != 0) 597 return (0); 598 599 (*claimed)++; 600 |
|
497 mac_mls = SLOT(label); 498 | 601 mac_mls = SLOT(label); 602 |
603 error = mac_mls_to_string(element_data, size, len, mac_mls); 604 if (error) 605 return (error); 606 607 *len = strlen(element_data); 608 return (0); 609} 610 611static int 612mac_mls_externalize_vnode_oldmac(struct label *label, struct oldmac *extmac) 613{ 614 struct mac_mls *mac_mls; 615 616 mac_mls = SLOT(label); 617 |
|
499 if (mac_mls == NULL) { 500 printf("mac_mls_externalize: NULL pointer\n"); 501 return (0); 502 } 503 504 extmac->m_mls = *mac_mls; 505 506 return (0); 507} 508 509static int | 618 if (mac_mls == NULL) { 619 printf("mac_mls_externalize: NULL pointer\n"); 620 return (0); 621 } 622 623 extmac->m_mls = *mac_mls; 624 625 return (0); 626} 627 628static int |
510mac_mls_internalize(struct label *label, struct mac *extmac) | 629mac_mls_parse_element(struct mac_mls_element *element, char *string) |
511{ | 630{ |
512 struct mac_mls *mac_mls; | 631 632 if (strcmp(string, "high") == 0 || 633 strcmp(string, "hi") == 0) { 634 element->mme_type = MAC_MLS_TYPE_HIGH; 635 element->mme_level = MAC_MLS_TYPE_UNDEF; 636 } else if (strcmp(string, "low") == 0 || 637 strcmp(string, "lo") == 0) { 638 element->mme_type = MAC_MLS_TYPE_LOW; 639 element->mme_level = MAC_MLS_TYPE_UNDEF; 640 } else if (strcmp(string, "equal") == 0 || 641 strcmp(string, "eq") == 0) { 642 element->mme_type = MAC_MLS_TYPE_EQUAL; 643 element->mme_level = MAC_MLS_TYPE_UNDEF; 644 } else { 645 char *p0, *p1; 646 int d; 647 648 p0 = string; 649 d = strtol(p0, &p1, 10); 650 651 if (d < 0 || d > 65535) 652 return (EINVAL); 653 element->mme_type = MAC_MLS_TYPE_LEVEL; 654 element->mme_level = d; 655 656 if (*p1 != ':') { 657 if (p1 == p0 || *p1 != '\0') 658 return (EINVAL); 659 else 660 return (0); 661 } 662 else 663 if (*(p1 + 1) == '\0') 664 return (0); 665 666 while ((p0 = ++p1)) { 667 d = strtol(p0, &p1, 10); 668 if (d < 1 || d > MAC_MLS_MAX_COMPARTMENTS) 669 return (EINVAL); 670 671 MAC_MLS_BIT_SET(d, element->mme_compartments); 672 673 if (*p1 == '\0') 674 break; 675 if (p1 == p0 || *p1 != '+') 676 return (EINVAL); 677 } 678 } 679 680 return (0); 681} 682 683/* 684 * Note: destructively consumes the string, make a local copy before 685 * calling if that's a problem. 686 */ 687static int 688mac_mls_parse(struct mac_mls *mac_mls, char *string) 689{ 690 char *range, *rangeend, *rangehigh, *rangelow, *single; |
513 int error; 514 | 691 int error; 692 |
515 mac_mls = SLOT(label); | 693 /* Do we have a range? */ 694 single = string; 695 range = index(string, '('); 696 if (range == single) 697 single = NULL; 698 rangelow = rangehigh = NULL; 699 if (range != NULL) { 700 /* Nul terminate the end of the single string. */ 701 *range = '\0'; 702 range++; 703 rangelow = range; 704 rangehigh = index(rangelow, '-'); 705 if (rangehigh == NULL) 706 return (EINVAL); 707 rangehigh++; 708 if (*rangelow == '\0' || *rangehigh == '\0') 709 return (EINVAL); 710 rangeend = index(rangehigh, ')'); 711 if (rangeend == NULL) 712 return (EINVAL); 713 if (*(rangeend + 1) != '\0') 714 return (EINVAL); 715 /* Nul terminate the ends of the ranges. */ 716 *(rangehigh - 1) = '\0'; 717 *rangeend = '\0'; 718 } 719 KASSERT((rangelow != NULL && rangehigh != NULL) || 720 (rangelow == NULL && rangehigh == NULL), 721 ("mac_biba_internalize_label: range mismatch")); |
516 | 722 |
723 bzero(mac_mls, sizeof(*mac_mls)); 724 if (single != NULL) { 725 error = mac_mls_parse_element(&mac_mls->mm_single, single); 726 if (error) 727 return (error); 728 mac_mls->mm_flags |= MAC_MLS_FLAG_SINGLE; 729 } 730 731 if (rangelow != NULL) { 732 error = mac_mls_parse_element(&mac_mls->mm_rangelow, 733 rangelow); 734 if (error) 735 return (error); 736 error = mac_mls_parse_element(&mac_mls->mm_rangehigh, 737 rangehigh); 738 if (error) 739 return (error); 740 mac_mls->mm_flags |= MAC_MLS_FLAG_RANGE; 741 } 742 |
|
517 error = mac_mls_valid(mac_mls); 518 if (error) 519 return (error); 520 | 743 error = mac_mls_valid(mac_mls); 744 if (error) 745 return (error); 746 |
521 *mac_mls = extmac->m_mls; | 747 return (0); 748} |
522 | 749 |
750static int 751mac_mls_internalize_label(struct label *label, char *element_name, 752 char *element_data, int *claimed) 753{ 754 struct mac_mls *mac_mls, mac_mls_temp; 755 int error; 756 757 if (strcmp(MAC_MLS_LABEL_NAME, element_name) != 0) 758 return (0); 759 760 (*claimed)++; 761 762 error = mac_mls_parse(&mac_mls_temp, element_data); 763 if (error) 764 return (error); 765 766 mac_mls = SLOT(label); 767 *mac_mls = mac_mls_temp; 768 |
|
523 return (0); 524} 525 | 769 return (0); 770} 771 |
772static void 773mac_mls_copy_label(struct label *src, struct label *dest) 774{ 775 776 *SLOT(dest) = *SLOT(src); 777} 778 |
|
526/* 527 * Labeling event operations: file system objects, and things that look 528 * a lot like file system objects. 529 */ 530static void 531mac_mls_create_devfs_device(dev_t dev, struct devfs_dirent *devfs_dirent, 532 struct label *label) 533{ --- 126 unchanged lines hidden (view full) --- 660 * Only copy the single, not the range, since vnodes only have 661 * a single. 662 */ 663 mac_mls_copy_single(source, dest); 664} 665 666static int 667mac_mls_update_vnode_from_externalized(struct vnode *vp, | 779/* 780 * Labeling event operations: file system objects, and things that look 781 * a lot like file system objects. 782 */ 783static void 784mac_mls_create_devfs_device(dev_t dev, struct devfs_dirent *devfs_dirent, 785 struct label *label) 786{ --- 126 unchanged lines hidden (view full) --- 913 * Only copy the single, not the range, since vnodes only have 914 * a single. 915 */ 916 mac_mls_copy_single(source, dest); 917} 918 919static int 920mac_mls_update_vnode_from_externalized(struct vnode *vp, |
668 struct label *vnodelabel, struct mac *extmac) | 921 struct label *vnodelabel, struct oldmac *extmac) |
669{ 670 struct mac_mls *source, *dest; 671 int error; 672 673 source = &extmac->m_mls; 674 dest = SLOT(vnodelabel); 675 676 error = mac_mls_valid(source); --- 315 unchanged lines hidden (view full) --- 992 dest = SLOT(&cred_child->cr_label); 993 994 mac_mls_copy_single(source, dest); 995 mac_mls_copy_range(source, dest); 996} 997 998static void 999mac_mls_execve_transition(struct ucred *old, struct ucred *new, | 922{ 923 struct mac_mls *source, *dest; 924 int error; 925 926 source = &extmac->m_mls; 927 dest = SLOT(vnodelabel); 928 929 error = mac_mls_valid(source); --- 315 unchanged lines hidden (view full) --- 1245 dest = SLOT(&cred_child->cr_label); 1246 1247 mac_mls_copy_single(source, dest); 1248 mac_mls_copy_range(source, dest); 1249} 1250 1251static void 1252mac_mls_execve_transition(struct ucred *old, struct ucred *new, |
1000 struct vnode *vp, struct mac *vnodelabel) | 1253 struct vnode *vp, struct label *vnodelabel) |
1001{ 1002 struct mac_mls *source, *dest; 1003 1004 source = SLOT(&old->cr_label); 1005 dest = SLOT(&new->cr_label); 1006 1007 mac_mls_copy_single(source, dest); 1008 mac_mls_copy_range(source, dest); 1009} 1010 1011static int 1012mac_mls_execve_will_transition(struct ucred *old, struct vnode *vp, | 1254{ 1255 struct mac_mls *source, *dest; 1256 1257 source = SLOT(&old->cr_label); 1258 dest = SLOT(&new->cr_label); 1259 1260 mac_mls_copy_single(source, dest); 1261 mac_mls_copy_range(source, dest); 1262} 1263 1264static int 1265mac_mls_execve_will_transition(struct ucred *old, struct vnode *vp, |
1013 struct mac *vnodelabel) | 1266 struct label *vnodelabel) |
1014{ 1015 1016 return (0); 1017} 1018 1019static void 1020mac_mls_create_proc0(struct ucred *cred) 1021{ --- 1083 unchanged lines hidden (view full) --- 2105 { MAC_INIT_MOUNT_FS_LABEL, 2106 (macop_t)mac_mls_init_label }, 2107 { MAC_INIT_PIPE_LABEL, 2108 (macop_t)mac_mls_init_label }, 2109 { MAC_INIT_SOCKET_LABEL, 2110 (macop_t)mac_mls_init_label_waitcheck }, 2111 { MAC_INIT_SOCKET_PEER_LABEL, 2112 (macop_t)mac_mls_init_label_waitcheck }, | 1267{ 1268 1269 return (0); 1270} 1271 1272static void 1273mac_mls_create_proc0(struct ucred *cred) 1274{ --- 1083 unchanged lines hidden (view full) --- 2358 { MAC_INIT_MOUNT_FS_LABEL, 2359 (macop_t)mac_mls_init_label }, 2360 { MAC_INIT_PIPE_LABEL, 2361 (macop_t)mac_mls_init_label }, 2362 { MAC_INIT_SOCKET_LABEL, 2363 (macop_t)mac_mls_init_label_waitcheck }, 2364 { MAC_INIT_SOCKET_PEER_LABEL, 2365 (macop_t)mac_mls_init_label_waitcheck }, |
2113 { MAC_INIT_TEMP_LABEL, 2114 (macop_t)mac_mls_init_label }, | |
2115 { MAC_INIT_VNODE_LABEL, 2116 (macop_t)mac_mls_init_label }, 2117 { MAC_DESTROY_BPFDESC_LABEL, 2118 (macop_t)mac_mls_destroy_label }, 2119 { MAC_DESTROY_CRED_LABEL, 2120 (macop_t)mac_mls_destroy_label }, 2121 { MAC_DESTROY_DEVFSDIRENT_LABEL, 2122 (macop_t)mac_mls_destroy_label }, --- 8 unchanged lines hidden (view full) --- 2131 { MAC_DESTROY_MOUNT_FS_LABEL, 2132 (macop_t)mac_mls_destroy_label }, 2133 { MAC_DESTROY_PIPE_LABEL, 2134 (macop_t)mac_mls_destroy_label }, 2135 { MAC_DESTROY_SOCKET_LABEL, 2136 (macop_t)mac_mls_destroy_label }, 2137 { MAC_DESTROY_SOCKET_PEER_LABEL, 2138 (macop_t)mac_mls_destroy_label }, | 2366 { MAC_INIT_VNODE_LABEL, 2367 (macop_t)mac_mls_init_label }, 2368 { MAC_DESTROY_BPFDESC_LABEL, 2369 (macop_t)mac_mls_destroy_label }, 2370 { MAC_DESTROY_CRED_LABEL, 2371 (macop_t)mac_mls_destroy_label }, 2372 { MAC_DESTROY_DEVFSDIRENT_LABEL, 2373 (macop_t)mac_mls_destroy_label }, --- 8 unchanged lines hidden (view full) --- 2382 { MAC_DESTROY_MOUNT_FS_LABEL, 2383 (macop_t)mac_mls_destroy_label }, 2384 { MAC_DESTROY_PIPE_LABEL, 2385 (macop_t)mac_mls_destroy_label }, 2386 { MAC_DESTROY_SOCKET_LABEL, 2387 (macop_t)mac_mls_destroy_label }, 2388 { MAC_DESTROY_SOCKET_PEER_LABEL, 2389 (macop_t)mac_mls_destroy_label }, |
2139 { MAC_DESTROY_TEMP_LABEL, 2140 (macop_t)mac_mls_destroy_label }, | |
2141 { MAC_DESTROY_VNODE_LABEL, 2142 (macop_t)mac_mls_destroy_label }, | 2390 { MAC_DESTROY_VNODE_LABEL, 2391 (macop_t)mac_mls_destroy_label }, |
2143 { MAC_EXTERNALIZE, 2144 (macop_t)mac_mls_externalize }, 2145 { MAC_INTERNALIZE, 2146 (macop_t)mac_mls_internalize }, | 2392 { MAC_COPY_PIPE_LABEL, 2393 (macop_t)mac_mls_copy_label }, 2394 { MAC_COPY_VNODE_LABEL, 2395 (macop_t)mac_mls_copy_label }, 2396 { MAC_EXTERNALIZE_CRED_LABEL, 2397 (macop_t)mac_mls_externalize_label }, 2398 { MAC_EXTERNALIZE_IFNET_LABEL, 2399 (macop_t)mac_mls_externalize_label }, 2400 { MAC_EXTERNALIZE_PIPE_LABEL, 2401 (macop_t)mac_mls_externalize_label }, 2402 { MAC_EXTERNALIZE_SOCKET_LABEL, 2403 (macop_t)mac_mls_externalize_label }, 2404 { MAC_EXTERNALIZE_SOCKET_PEER_LABEL, 2405 (macop_t)mac_mls_externalize_label }, 2406 { MAC_EXTERNALIZE_VNODE_LABEL, 2407 (macop_t)mac_mls_externalize_label }, 2408 { MAC_EXTERNALIZE_VNODE_OLDMAC, 2409 (macop_t)mac_mls_externalize_vnode_oldmac }, 2410 { MAC_INTERNALIZE_CRED_LABEL, 2411 (macop_t)mac_mls_internalize_label }, 2412 { MAC_INTERNALIZE_IFNET_LABEL, 2413 (macop_t)mac_mls_internalize_label }, 2414 { MAC_INTERNALIZE_PIPE_LABEL, 2415 (macop_t)mac_mls_internalize_label }, 2416 { MAC_INTERNALIZE_SOCKET_LABEL, 2417 (macop_t)mac_mls_internalize_label }, 2418 { MAC_INTERNALIZE_VNODE_LABEL, 2419 (macop_t)mac_mls_internalize_label }, |
2147 { MAC_CREATE_DEVFS_DEVICE, 2148 (macop_t)mac_mls_create_devfs_device }, 2149 { MAC_CREATE_DEVFS_DIRECTORY, 2150 (macop_t)mac_mls_create_devfs_directory }, 2151 { MAC_CREATE_DEVFS_SYMLINK, 2152 (macop_t)mac_mls_create_devfs_symlink }, 2153 { MAC_CREATE_DEVFS_VNODE, 2154 (macop_t)mac_mls_create_devfs_vnode }, --- 173 unchanged lines hidden --- | 2420 { MAC_CREATE_DEVFS_DEVICE, 2421 (macop_t)mac_mls_create_devfs_device }, 2422 { MAC_CREATE_DEVFS_DIRECTORY, 2423 (macop_t)mac_mls_create_devfs_directory }, 2424 { MAC_CREATE_DEVFS_SYMLINK, 2425 (macop_t)mac_mls_create_devfs_symlink }, 2426 { MAC_CREATE_DEVFS_VNODE, 2427 (macop_t)mac_mls_create_devfs_vnode }, --- 173 unchanged lines hidden --- |