Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 | /* * Copyright (c) 2024 Apple Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. The rights granted to you under the License * may not be used to create, or enable the creation or redistribution of, * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ #ifndef _VM_SANITIZE_INTERNAL_H_ #define _VM_SANITIZE_INTERNAL_H_ #include <mach/vm_types_unsafe.h> #include <mach/error.h> #include <stdbool.h> #include <vm/vm_sanitize_telemetry.h> __BEGIN_DECLS #pragma GCC visibility push(hidden) /* * kern_return_t errors used internally by VM */ /*! * @define VM_ERR_RETURN_NOW * @abstract Communicate to a caller that they should * return @c KERN_SUCCESS immediately after completing sanitization checks. */ #define VM_ERR_RETURN_NOW (err_vm | err_sub(0) | 1) /*! * @function vm_sanitize_get_kr * @abstract When a VM sanitizer returns an error, use this to extract * the real value that the sanitizers request that you return. * * @discussion errno-returning callers may need to transform this result further * * @param kr error code set by the sanitizers * @returns a (possibly different) error code */ static inline kern_return_t vm_sanitize_get_kr(kern_return_t kr) { if (kr == VM_ERR_RETURN_NOW) { return KERN_SUCCESS; } return kr; } /*! * @enum vm_sanitize_caller_id_t * * @brief * IDs for callers of sanitization functions that have different * set of return values. */ __enum_closed_decl(vm_sanitize_caller_id_t, uint32_t, { VM_SANITIZE_CALLER_ID_NONE, /* memory entry */ VM_SANITIZE_CALLER_ID_MACH_MAKE_MEMORY_ENTRY, VM_SANITIZE_CALLER_ID_MACH_MEMORY_ENTRY_PAGE_OP, VM_SANITIZE_CALLER_ID_MACH_MEMORY_ENTRY_RANGE_OP, VM_SANITIZE_CALLER_ID_MACH_MEMORY_ENTRY_MAP_SIZE, VM_SANITIZE_CALLER_ID_MACH_MEMORY_OBJECT_MEMORY_ENTRY, /* alloc/dealloc */ VM_SANITIZE_CALLER_ID_VM_ALLOCATE_FIXED, VM_SANITIZE_CALLER_ID_VM_ALLOCATE_ANYWHERE, VM_SANITIZE_CALLER_ID_VM_DEALLOCATE, VM_SANITIZE_CALLER_ID_MUNMAP, /* map/remap */ VM_SANITIZE_CALLER_ID_VM_MAP_REMAP, VM_SANITIZE_CALLER_ID_VM_MAP_REALLOCATE, VM_SANITIZE_CALLER_ID_MMAP, VM_SANITIZE_CALLER_ID_MREMAP_ENCRYPTED, VM_SANITIZE_CALLER_ID_MAP_WITH_LINKING_NP, VM_SANITIZE_CALLER_ID_ENTER_MEM_OBJ, VM_SANITIZE_CALLER_ID_ENTER_MEM_OBJ_CTL, /* wire/unwire */ VM_SANITIZE_CALLER_ID_VM_WIRE_USER, VM_SANITIZE_CALLER_ID_VM_UNWIRE_USER, VM_SANITIZE_CALLER_ID_VM_MAP_WIRE, VM_SANITIZE_CALLER_ID_VM_MAP_UNWIRE, VM_SANITIZE_CALLER_ID_VSLOCK, VM_SANITIZE_CALLER_ID_VSUNLOCK, /* copyin/copyout */ VM_SANITIZE_CALLER_ID_VM_MAP_COPY_OVERWRITE, VM_SANITIZE_CALLER_ID_VM_MAP_COPYIN, VM_SANITIZE_CALLER_ID_VM_MAP_READ_USER, VM_SANITIZE_CALLER_ID_VM_MAP_WRITE_USER, /* inherit */ VM_SANITIZE_CALLER_ID_VM_MAP_INHERIT, VM_SANITIZE_CALLER_ID_MINHERIT, /* protect */ VM_SANITIZE_CALLER_ID_VM_MAP_PROTECT, VM_SANITIZE_CALLER_ID_MPROTECT, VM_SANITIZE_CALLER_ID_USERACC, /* behavior */ VM_SANITIZE_CALLER_ID_VM_BEHAVIOR_SET, VM_SANITIZE_CALLER_ID_MADVISE, /* msync */ VM_SANITIZE_CALLER_ID_VM_MAP_MSYNC, VM_SANITIZE_CALLER_ID_MSYNC, /* machine attribute */ VM_SANITIZE_CALLER_ID_VM_MAP_MACHINE_ATTRIBUTE, /* page info */ VM_SANITIZE_CALLER_ID_VM_MAP_PAGE_RANGE_INFO, VM_SANITIZE_CALLER_ID_VM_MAP_PAGE_RANGE_QUERY, VM_SANITIZE_CALLER_ID_MINCORE, /* single */ VM_SANITIZE_CALLER_ID_MACH_VM_DEFERRED_RECLAMATION_BUFFER_INIT, VM_SANITIZE_CALLER_ID_MACH_VM_RANGE_CREATE, VM_SANITIZE_CALLER_ID_SHARED_REGION_MAP_AND_SLIDE_2_NP, /* test */ VM_SANITIZE_CALLER_ID_TEST }); /*! * @enum vm_sanitize_flags_t * * @brief * Flags that influence the sanitization being performed. * * @const VM_SANITIZE_FLAGS_NONE * Default value. * * @const VM_SANITIZE_FLAGS_CHECK_ALIGNED_START * Checks that the start address is aligned to map page size. * * @const VM_SANITIZE_FLAGS_SIZE_ZERO_SUCCEEDS * In sanitizers that have a @c size parameter, the sanitizer will ask * the caller to return @c KERN_SUCCESS when @c size @c == @c 0. * * Exactly one of @c VM_SANITIZE_FLAGS_SIZE_ZERO_* must be passed to sanitizers * that return a sanitized size. * * @const VM_SANITIZE_FLAGS_SIZE_ZERO_FAILS * In sanitizers that have a @c size parameter, the sanitizer will ask * the caller to return @c KERN_INVALID_ARGUMENT when @c size @c == @c 0. * * Exactly one of @c VM_SANITIZE_FLAGS_SIZE_ZERO_* must be passed to sanitizers * that return a sanitized size. * * @const VM_SANITIZE_FLAGS_SIZE_ZERO_FALLTHROUGH * In sanitizers that have a @c size parameter, the sanitizer will not ask * the caller to return when @c size @c == @c 0, thus falling through into * the caller's implementation. * * Exactly one of @c VM_SANITIZE_FLAGS_SIZE_ZERO_* must be passed to sanitizers * that return a sanitized size. * * @const VM_SANITIZE_FLAGS_GET_UNALIGNED_VALUES * Return unaligned start/end/size rather than realigned values. * * @const VM_SANITIZE_FLAGS_REALIGN_START * Ignore the misaligned bits of the start address when sanitizing an address. * * @const VM_SANITIZE_FLAGS_CHECK_USER_MEM_MAP_FLAGS * Reject non user allowed mem map flags for memory entry. * * @const VM_SANITIZE_FLAGS_CHECK_ALIGNED_SIZE * Checks that the size is aligned to map page size. * * @const VM_SANITIZE_FLAGS_CHECK_ADDR_RANGE * Checks that computing values within the range [start, end) does not overflow * into bits above the supported VA bits for the system. These bits may be used * by the kernel or hardware to store additional values. */ __options_closed_decl(vm_sanitize_flags_t, uint32_t, { VM_SANITIZE_FLAGS_NONE = 0x00000000, VM_SANITIZE_FLAGS_CHECK_ALIGNED_START = 0x00000001, VM_SANITIZE_FLAGS_SIZE_ZERO_SUCCEEDS = 0x00000002, VM_SANITIZE_FLAGS_SIZE_ZERO_FAILS = 0x00000004, VM_SANITIZE_FLAGS_SIZE_ZERO_FALLTHROUGH = 0x00000008, VM_SANITIZE_FLAGS_GET_UNALIGNED_VALUES = 0x00000010, VM_SANITIZE_FLAGS_REALIGN_START = 0x00000020, VM_SANITIZE_FLAGS_CHECK_USER_MEM_MAP_FLAGS = 0x00000040, /* 0x00000080 */ VM_SANITIZE_FLAGS_CHECK_ALIGNED_SIZE = 0x00000100, VM_SANITIZE_FLAGS_CHECK_ADDR_RANGE = 0x00000200, }); #define __vm_sanitize_bits_one_of(flags) \ ((flags) != 0 && ((flags) & ((flags) - 1)) == 0) #define __vm_sanitize_assert_one_of(arg, mask) \ __attribute__((diagnose_if(!__vm_sanitize_bits_one_of((arg) & (mask)), \ "`" #arg "` must have one of these flags `" #mask "`", "error"))) #define __vm_sanitize_require_size_zero_flag(arg) \ __vm_sanitize_assert_one_of(arg, \ VM_SANITIZE_FLAGS_SIZE_ZERO_SUCCEEDS | VM_SANITIZE_FLAGS_SIZE_ZERO_FAILS | VM_SANITIZE_FLAGS_SIZE_ZERO_FALLTHROUGH) /* * Error compat rewrite result: * compat_kr: the more-compatible return value * should_rewrite: true if compat_kr should be returned * should_telemeter: true if compat_kr should be telemetered */ typedef struct { kern_return_t compat_kr; bool should_rewrite; bool should_telemeter; } vm_sanitize_compat_rewrite_t; typedef vm_sanitize_compat_rewrite_t (*vm_sanitize_err_compat_addr_size_fn)(kern_return_t kr, vm_address_t addr, vm_size_t size, vm_offset_t pgmask, vm_map_t map_or_null); typedef vm_sanitize_compat_rewrite_t (*vm_sanitize_err_compat_cur_and_max_prots_fn)(kern_return_t kr, vm_prot_t *cur_inout, vm_prot_t *max_inout, vm_prot_t extra_mask); typedef const struct vm_sanitize_caller { vm_sanitize_caller_id_t vmsc_caller_id; const char *vmsc_caller_name; vm_sanitize_method_t vmsc_telemetry_id; enum vm_sanitize_subsys_error_codes vmsc_ktriage_id; vm_sanitize_err_compat_addr_size_fn err_compat_addr_size; vm_sanitize_err_compat_cur_and_max_prots_fn err_compat_prot_cur_max; } *vm_sanitize_caller_t; /* * Macros to declare and define callers of sanitization functions */ #define VM_SANITIZE_DECL_CALLER(name) \ extern vm_sanitize_caller_t const VM_SANITIZE_CALLER_ ## name; #define VM_SANITIZE_DEFINE_CALLER(name, ... /* error compat functions */) \ static const struct vm_sanitize_caller vm_sanitize_caller_storage_ ## name = { \ .vmsc_caller_id = VM_SANITIZE_CALLER_ID_ ## name, \ .vmsc_caller_name = #name, \ .vmsc_telemetry_id = VM_SANITIZE_METHOD_ ## name, \ .vmsc_ktriage_id = KDBG_TRIAGE_VM_SANITIZE_ ## name, \ __VA_ARGS__ \ }; \ vm_sanitize_caller_t const VM_SANITIZE_CALLER_ ## name = &vm_sanitize_caller_storage_ ## name /* * Declaration of callers of VM sanitization functions */ /* memory entry */ VM_SANITIZE_DECL_CALLER(MACH_MAKE_MEMORY_ENTRY); VM_SANITIZE_DECL_CALLER(MACH_MEMORY_ENTRY_PAGE_OP); VM_SANITIZE_DECL_CALLER(MACH_MEMORY_ENTRY_RANGE_OP); VM_SANITIZE_DECL_CALLER(MACH_MEMORY_ENTRY_MAP_SIZE); VM_SANITIZE_DECL_CALLER(MACH_MEMORY_OBJECT_MEMORY_ENTRY); /* alloc/dealloc */ VM_SANITIZE_DECL_CALLER(VM_ALLOCATE_FIXED); VM_SANITIZE_DECL_CALLER(VM_ALLOCATE_ANYWHERE); VM_SANITIZE_DECL_CALLER(VM_DEALLOCATE); VM_SANITIZE_DECL_CALLER(MUNMAP); /* map/remap */ VM_SANITIZE_DECL_CALLER(VM_MAP_REMAP); VM_SANITIZE_DECL_CALLER(VM_MAP_REALLOCATE); VM_SANITIZE_DECL_CALLER(MMAP); VM_SANITIZE_DECL_CALLER(MREMAP_ENCRYPTED); VM_SANITIZE_DECL_CALLER(MAP_WITH_LINKING_NP); VM_SANITIZE_DECL_CALLER(ENTER_MEM_OBJ); VM_SANITIZE_DECL_CALLER(ENTER_MEM_OBJ_CTL); /* wire/unwire */ VM_SANITIZE_DECL_CALLER(VM_WIRE_USER); VM_SANITIZE_DECL_CALLER(VM_UNWIRE_USER); VM_SANITIZE_DECL_CALLER(VM_MAP_WIRE); VM_SANITIZE_DECL_CALLER(VM_MAP_UNWIRE); VM_SANITIZE_DECL_CALLER(VSLOCK); VM_SANITIZE_DECL_CALLER(VSUNLOCK); /* copyin/copyout */ VM_SANITIZE_DECL_CALLER(VM_MAP_COPY_OVERWRITE); VM_SANITIZE_DECL_CALLER(VM_MAP_COPYIN); VM_SANITIZE_DECL_CALLER(VM_MAP_READ_USER); VM_SANITIZE_DECL_CALLER(VM_MAP_WRITE_USER); /* inherit */ VM_SANITIZE_DECL_CALLER(MACH_VM_INHERIT); VM_SANITIZE_DECL_CALLER(VM_INHERIT); VM_SANITIZE_DECL_CALLER(VM32_INHERIT); VM_SANITIZE_DECL_CALLER(VM_MAP_INHERIT); VM_SANITIZE_DECL_CALLER(MINHERIT); /* protect */ VM_SANITIZE_DECL_CALLER(MACH_VM_PROTECT); VM_SANITIZE_DECL_CALLER(VM_PROTECT); VM_SANITIZE_DECL_CALLER(VM32_PROTECT); VM_SANITIZE_DECL_CALLER(VM_MAP_PROTECT); VM_SANITIZE_DECL_CALLER(MPROTECT); VM_SANITIZE_DECL_CALLER(USERACC); /* behavior */ VM_SANITIZE_DECL_CALLER(VM_BEHAVIOR_SET); VM_SANITIZE_DECL_CALLER(MADVISE); /* msync */ VM_SANITIZE_DECL_CALLER(VM_MAP_MSYNC); VM_SANITIZE_DECL_CALLER(MSYNC); /* machine attribute */ VM_SANITIZE_DECL_CALLER(VM_MAP_MACHINE_ATTRIBUTE); /* page info */ VM_SANITIZE_DECL_CALLER(VM_MAP_PAGE_RANGE_INFO); VM_SANITIZE_DECL_CALLER(VM_MAP_PAGE_RANGE_QUERY); VM_SANITIZE_DECL_CALLER(MINCORE); /* single */ VM_SANITIZE_DECL_CALLER(MACH_VM_DEFERRED_RECLAMATION_BUFFER_INIT); VM_SANITIZE_DECL_CALLER(MACH_VM_RANGE_CREATE); VM_SANITIZE_DECL_CALLER(SHARED_REGION_MAP_AND_SLIDE_2_NP); /* test */ VM_SANITIZE_DECL_CALLER(TEST); /* * Macro that extracts the inner struct member from a wrapped type. Should be * used in all cases, including validation functions, when accessing the * inner struct member. */ #define VM_SANITIZE_UNSAFE_UNWRAP(_val) (_val).UNSAFE /* * Macro to check if unsafe value is a specific safe value */ #define VM_SANITIZE_UNSAFE_IS_EQUAL(_var, _val) ((_var).UNSAFE == (_val)) /* * Macro to check if unsafe value is zero */ #define VM_SANITIZE_UNSAFE_IS_ZERO(_var) VM_SANITIZE_UNSAFE_IS_EQUAL(_var, 0) /* * returns whether a given unsafe value fits a given type */ #define VM_SANITIZE_UNSAFE_FITS(_var, type_t) ({ \ __auto_type __tmp = (_var).UNSAFE; \ __tmp == (type_t)__tmp; \ }) /* * Macro that sets a value of unsafe type to a value of safe type. * This is safe to do because we are only forcing ourselves to perform * checks on a value we already have direct access to. */ #define VM_SANITIZE_UT_SET(_var, _val) ((_var).UNSAFE) = (_val) /*! * @function vm_sanitize_wrap_addr * * @abstract * Function that wraps unsanitized safe address into unsafe address * * @param val safe address * @returns unsafe address */ __attribute__((always_inline, warn_unused_result)) vm_addr_struct_t vm_sanitize_wrap_addr(vm_address_t val); /*! * @function vm_sanitize_wrap_addr_ref * * @abstract * Function that wraps a safe address pointer, * into unsafe address pointer. * * @param val safe address ref * @returns unsafe address reference */ #define vm_sanitize_wrap_addr_ref(var) _Generic(var, \ mach_vm_address_t *: (vm_addr_struct_t *)(var), \ vm_address_t *: (vm_addr_struct_t *)(var), \ default: (var)) /*! * @function vm_sanitize_wrap_size * * @abstract * Function that wraps unsanitized safe size into unsafe size * * @param val safe size * @returns unsafe size */ __attribute__((always_inline, warn_unused_result)) vm_size_struct_t vm_sanitize_wrap_size(vm_size_t val); /* * bsd doesn't use 32bit interfaces and the types aren't even defined for them, * so we just expose this to MACH. */ #ifdef MACH_KERNEL_PRIVATE /*! * @function vm32_sanitize_wrap_size * * @abstract * Function that wraps unsanitized 32bit safe size into 32bit unsafe size * * @param val safe size * @returns unsafe size */ __attribute__((always_inline, warn_unused_result)) vm32_size_struct_t vm32_sanitize_wrap_size(vm32_size_t val); #endif /* MACH_KERNEL_PRIVATE */ /*! * @function vm_sanitize_wrap_prot * * @abstract * Function that wraps unsanitized safe protection into unsafe protection * * @param val safe protection * @returns unsafe protection */ __attribute__((always_inline, warn_unused_result)) vm_prot_ut vm_sanitize_wrap_prot(vm_prot_t val); /*! * @function vm_sanitize_wrap_prot_ref * * @abstract * Function that wraps a safe protection pointer into unsafe protection pointer. * * @param val safe protection pointer * @returns unsafe protection pointer */ __attribute__((always_inline, warn_unused_result)) static inline vm_prot_ut * vm_sanitize_wrap_prot_ref(vm_prot_t *val) { return (vm_prot_ut *)val; } /*! * @function vm_sanitize_wrap_inherit * * @abstract * Function that wraps unsanitized safe vm_inherit into unsafe vm_inherit * * @param val safe vm_inherit * @returns unsafe vm_inherit */ __attribute__((always_inline, warn_unused_result)) vm_inherit_ut vm_sanitize_wrap_inherit(vm_inherit_t val); /*! * @function vm_sanitize_wrap_behavior * * @abstract * Function that wraps a safe vm_behavior into an unsafe vm_behavior * * @param val safe vm_behavior * @returns unsafe vm_behavior */ __attribute__((always_inline, warn_unused_result)) vm_behavior_ut vm_sanitize_wrap_behavior(vm_behavior_t val); #ifdef MACH_KERNEL_PRIVATE /*! * @function vm_sanitize_expand_addr_to_64 * * @abstract * Function used by the vm32 functions to cast 32bit unsafe address * to 64bit unsafe address * * @param val 32bit unsafe address * @returns 64bit unsafe address */ __attribute__((always_inline, warn_unused_result)) vm_addr_struct_t vm_sanitize_expand_addr_to_64(vm32_address_ut val); /*! * @function vm_sanitize_expand_size_to_64 * * @abstract * Function used by the vm32 functions to cast 32bit unsafe size * to 64bit unsafe size * * @param val 32bit unsafe size * @returns 64bit unsafe size */ __attribute__((always_inline, warn_unused_result)) vm_size_struct_t vm_sanitize_expand_size_to_64(vm32_size_ut val); /*! * @function vm_sanitize_trunc_addr_to_32 * * @abstract * Function used by the vm32 functions to cast 64bit unsafe address * to 32bit unsafe address * * @param val 64bit unsafe address * @returns 32bit unsafe address */ __attribute__((always_inline, warn_unused_result)) vm32_address_ut vm_sanitize_trunc_addr_to_32(vm_addr_struct_t val); /*! * @function vm_sanitize_trunc_size_to_32 * * @abstract * Function used by the vm32 functions to cast 64bit unsafe size * to 32bit unsafe size * * @param val 64bit unsafe size * @returns 32bit unsafe size */ __attribute__((always_inline, warn_unused_result)) vm32_size_ut vm_sanitize_trunc_size_to_32(vm_size_struct_t val); /*! * @function vm_sanitize_add_overflow() * * @abstract * Computes the sum of an address and a size checking for overflow, * staying in the unsafe world. * * @param addr_u unsafe address * @param size_u unsafe size * @param addr_out_u unsafe result * @returns whether the operation overflowed */ __attribute__((always_inline, warn_unused_result, overloadable)) bool vm_sanitize_add_overflow( vm32_address_ut addr_u, vm32_size_ut size_u, vm32_address_ut *addr_out_u); #endif /* MACH_KERNEL_PRIVATE */ /*! * @function vm_sanitize_add_overflow() * * @abstract * Computes the sum of an address and a size checking for overflow, * staying in the unsafe world. * * @param addr_u unsafe address * @param size_u unsafe size * @param addr_out_u unsafe result * @returns whether the operation overflowed */ __attribute__((always_inline, warn_unused_result, overloadable)) bool vm_sanitize_add_overflow( vm_addr_struct_t addr_u, vm_size_struct_t size_u, vm_addr_struct_t *addr_out_u); /*! * @function vm_sanitize_add_overflow() * * @abstract * Computes the sum of two sizes checking for overflow, * staying in the unsafe world. * * @param size1_u unsafe size 1 * @param size2_u unsafe size 2 * @param size_out_u unsafe result * @returns whether the operation overflowed */ __attribute__((always_inline, warn_unused_result, overloadable)) bool vm_sanitize_add_overflow( vm_size_struct_t size1_u, vm_size_struct_t size2_u, vm_size_struct_t *size_out_u); /*! * @function vm_sanitize_compute_ut_end * * @abstract * Computes and returns unsafe end from unsafe start and size * * @param addr_u unsafe start * @param size_u unsafe size * @returns unsafe end */ __attribute__((always_inline, warn_unused_result)) vm_addr_struct_t vm_sanitize_compute_ut_end( vm_addr_struct_t addr_u, vm_size_struct_t size_u); /*! * @function vm_sanitize_compute_ut_size * * @abstract * Computes and returns unsafe size from unsafe start and end * * @param addr_u unsafe start * @param end_u unsafe end * @returns unsafe size */ __attribute__((always_inline, warn_unused_result)) vm_size_struct_t vm_sanitize_compute_ut_size( vm_addr_struct_t addr_u, vm_addr_struct_t end_u); #if CONFIG_KERNEL_TAGGING || HAS_MTE_EMULATION_SHIMS /*! * @function vm_sanitize_canonicalize_ut_addr * * @abstract * Canonicalizes and returns unsafe address from unsafe address. * Intended to be used prior to address/offset sanitization. * * @param map map containing the unsafe address * @param addr_u unsafe address * @returns unsafe address, canonicalized */ __attribute__((always_inline, warn_unused_result)) vm_addr_struct_t vm_sanitize_canonicalize_ut_addr( vm_map_t map, vm_addr_struct_t addr_u); /*! * @function vm_sanitize_canonicalize_ut_addr_end * * @abstract * Returns canonicalized unsafe start and end addresses. * Verifies that @c addr_u and @c end_u have the same tag. * Intended to be used prior to address-end sanitization. * * @param map map containing the unsafe address * @param addr_u unsafe address, IN/OUT * @param end_u unsafe end, IN/OUT * @returns KERN_SUCCESS if addr_u and end_u have the same tag, * KERN_INVALID_ARGUMENT if not */ __attribute__((always_inline, warn_unused_result)) kern_return_t vm_sanitize_canonicalize_ut_addr_end( vm_map_t map, vm_addr_struct_t *const addr_u, vm_addr_struct_t *const end_u); /*! * @function vm_sanitize_validate_non_canonical_ut_addr * * @abstract * Check whether an address is in its canonical form. Reject * (KERN_INVALID_ARGUMENT) addresses that are not. * Intended to be used prior to address/offset sanitization. * * @param map map containing the unsafe address * @param addr_u unsafe address * @returns KERN_SUCCESS if the address is canonical, * kERN_INVALID_ARGUMENT if not. */ __attribute__((always_inline, warn_unused_result)) kern_return_t vm_sanitize_validate_non_canonical_ut_addr( vm_map_t map, vm_addr_struct_t addr_u); #endif /* CONFIG_KERNEL_TAGGING || HAS_MTE_EMULATION_SHIMS */ /*! * @function vm_sanitize_addr * * @abstract * Sanitization function that takes unsafe address, and returns a truncated * address. * * @param map map the address belongs to * @param addr_u unsafe address to sanitize * @returns a sanitized address */ __attribute__((always_inline, warn_unused_result)) mach_vm_address_t vm_sanitize_addr( vm_map_t map, vm_addr_struct_t addr_u); /*! * @function vm_sanitize_offset_in_page * * @abstract * Sanitization function that takes unsafe address, * and returns the offset in the page for this address. * * @param mask page mask to use * @param addr_u unsafe address to sanitize * @returns a sanitized offset in page */ __attribute__((always_inline, warn_unused_result)) mach_vm_offset_t vm_sanitize_offset_in_page( vm_map_offset_t mask, vm_addr_struct_t addr_u); /* * @function vm_sanitize_offset_in_page * * @abstract * Sanitization function that takes unsafe address, * and returns the offset in the page for this address. * * @param map map the address belongs to * @param addr_u unsafe address to sanitize * @returns a sanitized offset in page */ __attribute__((always_inline, warn_unused_result, overloadable)) static inline mach_vm_offset_t vm_sanitize_offset_in_page( vm_map_t map, vm_addr_struct_t addr_u) { return vm_sanitize_offset_in_page(vm_map_page_mask(map), addr_u); } /*! * @function vm_sanitize_offset * * @abstract * Sanitization function that takes unsafe offset and validates * that it is within addr and end provided. * * @param offset_u unsafe offset to sanitize * @param vm_sanitize_caller caller of the sanitization function * @param addr sanitized start address * @param end sanitized end address * @param offset sanitized offset * @returns return code indicating success/failure of sanitization */ __attribute__((always_inline, warn_unused_result)) kern_return_t vm_sanitize_offset( vm_addr_struct_t offset_u, vm_sanitize_caller_t vm_sanitize_caller, vm_map_address_t addr, vm_map_address_t end, vm_map_offset_t *offset); /*! * @function vm_sanitize_mask * * @abstract * Sanitization function that takes unsafe mask and sanitizes it. * * @param mask_u unsafe mask to sanitize * @param vm_sanitize_caller caller of the sanitization function * @param mask sanitized mask * @returns return code indicating success/failure of sanitization */ __attribute__((always_inline, warn_unused_result)) kern_return_t vm_sanitize_mask( vm_addr_struct_t mask_u, vm_sanitize_caller_t vm_sanitize_caller, vm_map_offset_t *mask); /*! * @function vm_sanitize_object_size * * @abstract * Sanitization function that takes unsafe VM object size and safely rounds it * up wrt a VM object. * * @param size_u unsafe size to sanitize * @param vm_sanitize_caller caller of the sanitization function * @param flags flags that influence sanitization performed * @param size sanitized object size * @returns return code indicating success/failure of sanitization */ __attribute__((always_inline, warn_unused_result)) kern_return_t vm_sanitize_object_size( vm_size_struct_t size_u, vm_sanitize_caller_t vm_sanitize_caller, vm_sanitize_flags_t flags, vm_object_offset_t *size) __vm_sanitize_require_size_zero_flag(flags); /*! * @function vm_sanitize_size * * @abstract * Sanitization function that takes unsafe size and safely rounds it up. * * @param offset_u an offset/address which marks the beginning of the * memory region of size @c size_u. Overflow checks * will be performed on @c size_u+offset_u, and the * low bits of @c offset_u may influence the rounding * of @c size_u to ensure the returned size covers all * pages that intersect with the region that starts at * @c offset_u and has size @c size_u. * @param size_u unsafe size to sanitize * @param vm_sanitize_caller caller of the sanitization function * @param map map the address belongs to * @param flags flags that influence sanitization performed * @param size sanitized size * @returns return code indicating success/failure of sanitization */ __attribute__((always_inline, warn_unused_result)) kern_return_t vm_sanitize_size( vm_addr_struct_t offset_u, vm_size_struct_t size_u, vm_sanitize_caller_t vm_sanitize_caller, vm_map_t map, vm_sanitize_flags_t flags, mach_vm_size_t *size) __vm_sanitize_require_size_zero_flag(flags); /*! * @function vm_sanitize_addr_size * * @abstract * Sanitization function that takes unsafe address and size and returns * sanitized start, end and size via out parameters. * * @param addr_u unsafe address to sanitize * @param size_u unsafe size to sanitize * @param vm_sanitize_caller caller of the sanitization function * @param mask page mask to use * @param map_or_null optional map, used for error compat for some callers * @param flags flags that influence sanitization performed * @param addr sanitized start * @param end sanitized end * @param size sanitized size * @returns return code indicating success/failure of sanitization */ __attribute__((always_inline, warn_unused_result)) kern_return_t vm_sanitize_addr_size( vm_addr_struct_t addr_u, vm_size_struct_t size_u, vm_sanitize_caller_t vm_sanitize_caller, mach_vm_offset_t mask, vm_map_t map_or_null, vm_sanitize_flags_t flags, vm_map_offset_t *addr, vm_map_offset_t *end, vm_map_size_t *size) __vm_sanitize_require_size_zero_flag(flags); /*! * @function vm_sanitize_addr_size * * @abstract * Sanitization function that takes unsafe address and size and returns * sanitized start, end and size via out parameters. * * @param addr_u unsafe address to sanitize * @param size_u unsafe size to sanitize * @param vm_sanitize_caller caller of the sanitization function * @param mask page mask to use * @param flags flags that influence sanitization performed * @param addr sanitized start * @param end sanitized end * @param size sanitized size * @returns return code indicating success/failure of sanitization */ __attribute__((always_inline, warn_unused_result, overloadable)) static inline kern_return_t vm_sanitize_addr_size( vm_addr_struct_t addr_u, vm_size_struct_t size_u, vm_sanitize_caller_t vm_sanitize_caller, mach_vm_offset_t mask, vm_sanitize_flags_t flags, vm_map_offset_t *addr, vm_map_offset_t *end, vm_map_size_t *size) __vm_sanitize_require_size_zero_flag(flags) { return vm_sanitize_addr_size(addr_u, size_u, vm_sanitize_caller, mask, VM_MAP_NULL, flags, addr, end, size); } /*! * @function vm_sanitize_addr_size * * @abstract * Sanitization function that takes unsafe address and size and returns * sanitized start, end and size via out parameters. * * @param addr_u unsafe address to sanitize * @param size_u unsafe size to sanitize * @param vm_sanitize_caller caller of the sanitization function * @param map map the address belongs to * @param flags flags that influence sanitization performed * @param addr sanitized start * @param end sanitized end * @param size sanitized size * @returns return code indicating success/failure of sanitization */ __attribute__((always_inline, warn_unused_result, overloadable)) static inline kern_return_t vm_sanitize_addr_size( vm_addr_struct_t addr_u, vm_size_struct_t size_u, vm_sanitize_caller_t vm_sanitize_caller, vm_map_t map, vm_sanitize_flags_t flags, vm_map_offset_t *addr, vm_map_offset_t *end, vm_map_size_t *size) __vm_sanitize_require_size_zero_flag(flags) { mach_vm_offset_t mask = vm_map_page_mask(map); return vm_sanitize_addr_size(addr_u, size_u, vm_sanitize_caller, mask, map, flags, addr, end, size); } /*! * @function vm_sanitize_addr_end * * @abstract * Sanitization function that takes unsafe address and end and returns * sanitized start, end and size via out parameters. * * @param addr_u unsafe address to sanitize * @param end_u unsafe end to sanitize * @param vm_sanitize_caller caller of the sanitization function * @param mask page mask to use * @param map_or_null optional map, used for error compat for some callers * @param flags flags that influence sanitization performed * @param start sanitized start * @param end sanitized end * @param size sanitized size * @returns return code indicating success/failure of sanitization */ __attribute__((always_inline, warn_unused_result)) kern_return_t vm_sanitize_addr_end( vm_addr_struct_t addr_u, vm_addr_struct_t end_u, vm_sanitize_caller_t vm_sanitize_caller, mach_vm_offset_t mask, vm_map_t map_or_null, vm_sanitize_flags_t flags, vm_map_offset_t *start, vm_map_offset_t *end, vm_map_size_t *size) __vm_sanitize_require_size_zero_flag(flags); /*! * @function vm_sanitize_addr_end * * @abstract * Sanitization function that takes unsafe address and end and returns * sanitized start, end and size via out parameters. * * @param addr_u unsafe address to sanitize * @param end_u unsafe end to sanitize * @param vm_sanitize_caller caller of the sanitization function * @param mask page mask to use * @param flags flags that influence sanitization performed * @param start sanitized start * @param end sanitized end * @param size sanitized size * @returns return code indicating success/failure of sanitization */ __attribute__((always_inline, warn_unused_result, overloadable)) static inline kern_return_t vm_sanitize_addr_end( vm_addr_struct_t addr_u, vm_addr_struct_t end_u, vm_sanitize_caller_t vm_sanitize_caller, mach_vm_offset_t mask, vm_sanitize_flags_t flags, vm_map_offset_t *start, vm_map_offset_t *end, vm_map_size_t *size) __vm_sanitize_require_size_zero_flag(flags) { return vm_sanitize_addr_end(addr_u, end_u, vm_sanitize_caller, mask, VM_MAP_NULL, flags, start, end, size); } /*! * @function vm_sanitize_addr_end * * @abstract * Sanitization function that takes unsafe address and end and returns * sanitized start, end and size via out parameters. * * @param addr_u unsafe address to sanitize * @param end_u unsafe end to sanitize * @param vm_sanitize_caller caller of the sanitization function * @param map map the address belongs to * @param flags flags that influence sanitization performed * @param start sanitized start * @param end sanitized end * @param size sanitized size * @returns return code indicating success/failure of sanitization */ __attribute__((always_inline, warn_unused_result, overloadable)) static inline kern_return_t vm_sanitize_addr_end( vm_addr_struct_t addr_u, vm_addr_struct_t end_u, vm_sanitize_caller_t vm_sanitize_caller, vm_map_t map, vm_sanitize_flags_t flags, vm_map_offset_t *start, vm_map_offset_t *end, vm_map_size_t *size) __vm_sanitize_require_size_zero_flag(flags) { mach_vm_offset_t mask = vm_map_page_mask(map); return vm_sanitize_addr_end(addr_u, end_u, vm_sanitize_caller, mask, map, flags, start, end, size); } /*! * @function vm_sanitize_prot * * @abstract * Sanitization function that takes unsafe protections and sanitizes it. * * @param prot_u unsafe protections * @param vm_sanitize_caller caller of the sanitization function * @param map map in which protections are going to be changed * @param extra_mask extra mask to allow on top of (VM_PROT_ALL | VM_PROT_ALLEXEC) * @param prot sanitized protections * @returns return code indicating success/failure of sanitization */ __attribute__((always_inline, warn_unused_result)) kern_return_t vm_sanitize_prot( vm_prot_ut prot_u, vm_sanitize_caller_t vm_sanitize_caller, vm_map_t map, vm_prot_t extra_mask, vm_prot_t *prot); __attribute__((always_inline, warn_unused_result, overloadable)) static inline kern_return_t vm_sanitize_prot( vm_prot_ut prot_u, vm_sanitize_caller_t vm_sanitize_caller, vm_map_t map, vm_prot_t *prot) { return vm_sanitize_prot(prot_u, vm_sanitize_caller, map, VM_PROT_NONE, prot); } /*! * @function vm_sanitize_cur_and_max_prots * * @abstract * Sanitization function that takes a pair of unsafe current and max protections * and sanitizes it. * * @param cur_prot_u unsafe current protections * @param max_prot_u unsafe max protections * @param vm_sanitize_caller caller of the sanitization function * @param map map in which protections are going to be changed * @param extra_mask extra mask to allow on top of (VM_PROT_ALL | VM_PROT_ALLEXEC) * @param cur_prot sanitized current protections * @param max_prot sanitized max protections * @returns return code indicating success/failure of sanitization */ __attribute__((always_inline, warn_unused_result)) kern_return_t vm_sanitize_cur_and_max_prots( vm_prot_ut cur_prot_u, vm_prot_ut max_prot_u, vm_sanitize_caller_t vm_sanitize_caller, vm_map_t map, vm_prot_t extra_mask, vm_prot_t *cur_prot, vm_prot_t *max_prot); __attribute__((always_inline, warn_unused_result, overloadable)) static inline kern_return_t vm_sanitize_cur_and_max_prots( vm_prot_ut cur_prot_u, vm_prot_ut max_prot_u, vm_sanitize_caller_t vm_sanitize_caller, vm_map_t map, vm_prot_t *cur_prot, vm_prot_t *max_prot) { return vm_sanitize_cur_and_max_prots(cur_prot_u, max_prot_u, vm_sanitize_caller, map, VM_PROT_NONE, cur_prot, max_prot); } /*! * @function vm_sanitize_memory_entry_perm * * @abstract * Sanitization function that takes unsafe memory entry permissions and * sanitizes it. * * @param perm_u unsafe permissions to sanitize * @param vm_sanitize_caller caller of the sanitization function * @param flags flags that influence sanitization performed * @param extra_mask extra mask to allow on top of VM_PROT_ALL * @param perm sanitized memory entry permissions * @returns return code indicating success/failure of sanitization */ __attribute__((always_inline, warn_unused_result)) kern_return_t vm_sanitize_memory_entry_perm( vm_prot_ut perm_u, vm_sanitize_caller_t vm_sanitize_caller, vm_sanitize_flags_t flags, vm_prot_t extra_mask, vm_prot_t *perm); /*! * @function vm_sanitize_prot_bsd * * @abstract * Sanitization function that takes unsafe protections and sanitizes it. * * @discussion * Use this function for BSD callers as it strips invalid protections instead * of returning an error. * * @param prot_u unsafe protections * @param vm_sanitize_caller caller of the sanitization function * @returns sanitized protections */ __attribute__((always_inline, warn_unused_result)) vm_prot_t vm_sanitize_prot_bsd( vm_prot_ut prot_u, vm_sanitize_caller_t vm_sanitize_caller); /*! * @function vm_sanitize_inherit * * @abstract * Sanitization function that takes unsafe vm_inherit and sanitizes it. * * @param inherit_u unsafe vm_inherit * @param vm_sanitize_caller caller of the sanitization function * @param inherit sanitized vm_inherit * @returns return code indicating success/failure of sanitization */ __attribute__((always_inline, warn_unused_result)) kern_return_t vm_sanitize_inherit( vm_inherit_ut inherit_u, vm_sanitize_caller_t vm_sanitize_caller, vm_inherit_t *inherit); /*! * @function vm_sanitize_behavior * * @abstract * Sanitization function that takes an unsafe vm_behavior and sanitizes it. * * @param behavior_u unsafe vm_behavior * @param vm_sanitize_caller caller of the sanitization function * @param behavior sanitized vm_behavior * @returns return code indicating success/failure of sanitization */ __attribute__((always_inline, warn_unused_result)) kern_return_t vm_sanitize_behavior( vm_behavior_ut behavior_u, vm_sanitize_caller_t vm_sanitize_caller __unused, vm_behavior_t *behavior); #pragma GCC visibility pop __END_DECLS #endif /* _VM_SANITIZE_INTERNAL_H_ */ |