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 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 | /* * 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@ */ /* avoid includes here; we want these pragmas to also affect included inline functions */ #include <mach/machine/vm_param.h> /* to get PAGE_SHIFT without the inline functions from mach/vm_param.h */ /* * On 4k-hardware-page arm64 systems, the PAGE_SHIFT macro does not resolve to * a constant, but instead a variable whose value is determined on boot depending * on the amount of RAM installed. * * In these cases, actual instructions need to be emitted to compute values like * PAGE_SIZE = (1 << PAGE_SHIFT), which means UBSan checks will be generated * as well since the values cannot be computed at compile time. * * Therefore, we disable arithmetic UBSan checks on these configurations. We * detect them with PAGE_SHIFT == 0, since (during the preprocessing phase) * symbols will resolve to 0, whereas PAGE_SHIFT will resolve to its actual * nonzero value if it is defined as a macro. */ #if PAGE_SHIFT == 0 #pragma clang attribute push (__attribute__((no_sanitize("signed-integer-overflow", \ "unsigned-integer-overflow", "shift", "unsigned-shift-base"))), apply_to=function) #endif /* Disabling optimizations makes it impossible to optimize out UBSan checks */ #if !__OPTIMIZE__ #pragma clang attribute push (__attribute__((no_sanitize("undefined", \ "integer", "unsigned-shift-base", "nullability", "bounds"))), apply_to=function) #endif #include <vm/vm_map_xnu.h> #include <vm/vm_sanitize_internal.h> #include <vm/vm_object_internal.h> #define VM_SANITIZE_PROT_ALLOWED (VM_PROT_ALL | VM_PROT_ALLEXEC) // TODO: enable telemetry and ktriage separately? /* Also send telemetry output to kernel serial console? */ static TUNABLE(bool, vm_sanitize_telemeter_to_serial, "vm_sanitize_telemeter_to_serial", false); /* * Arithmetic macros that suppress UBSan. os_xyz_overflow does not generate a * UBSan overflow check, since it indicates to the compiler that overflow is * (potentially) intentional and well-defined. * * These macros ignore the value that indicates whether overflow actually, * occurred, so a comment should be left explaining why it is unlikely to * happen or is otherwise not a concern. */ #define vm_add_no_ubsan(a, b) ({ typeof(a+b) TMP; (void) os_add_overflow(a, b, &TMP); TMP; }) #define vm_sub_no_ubsan(a, b) ({ typeof(a+b) TMP; (void) os_sub_overflow(a, b, &TMP); TMP; }) static inline kern_return_t vm_sanitize_apply_err_rewrite_policy(kern_return_t initial_kr, vm_sanitize_compat_rewrite_t rewrite) { return rewrite.should_rewrite ? rewrite.compat_kr : initial_kr; } __attribute__((always_inline, warn_unused_result)) vm_addr_struct_t vm_sanitize_wrap_addr(vm_address_t val) { return (vm_addr_struct_t) { .UNSAFE = val }; } __attribute__((always_inline, warn_unused_result)) vm_size_struct_t vm_sanitize_wrap_size(vm_size_t val) { return (vm_size_struct_t) { .UNSAFE = val }; } __attribute__((always_inline, warn_unused_result)) vm32_size_struct_t vm32_sanitize_wrap_size(vm32_size_t val) { return (vm32_size_struct_t) { .UNSAFE = val }; } __attribute__((always_inline, warn_unused_result)) vm_prot_ut vm_sanitize_wrap_prot(vm_prot_t val) { return (vm_prot_ut) { .UNSAFE = val }; } __attribute__((always_inline, warn_unused_result)) vm_inherit_ut vm_sanitize_wrap_inherit(vm_inherit_t val) { return (vm_inherit_ut) { .UNSAFE = val }; } __attribute__((always_inline, warn_unused_result)) vm_behavior_ut vm_sanitize_wrap_behavior(vm_behavior_t val) { return (vm_behavior_ut) { .UNSAFE = val }; } #ifdef MACH_KERNEL_PRIVATE __attribute__((always_inline, warn_unused_result)) vm_addr_struct_t vm_sanitize_expand_addr_to_64(vm32_address_ut val) { return (vm_addr_struct_t) { .UNSAFE = val.UNSAFE }; } __attribute__((always_inline, warn_unused_result)) vm_size_struct_t vm_sanitize_expand_size_to_64(vm32_size_ut val) { return (vm_size_struct_t) { .UNSAFE = val.UNSAFE }; } __attribute__((always_inline, warn_unused_result)) vm32_address_ut vm_sanitize_trunc_addr_to_32(vm_addr_struct_t val) { vm32_address_ut ret; ret.UNSAFE = CAST_DOWN_EXPLICIT(vm32_address_t, val.UNSAFE); return ret; } __attribute__((always_inline, warn_unused_result)) vm32_size_ut vm_sanitize_trunc_size_to_32(vm_size_struct_t val) { vm32_size_ut ret; ret.UNSAFE = CAST_DOWN_EXPLICIT(vm32_size_t, val.UNSAFE); return ret; } __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) { vm32_address_t addr = VM_SANITIZE_UNSAFE_UNWRAP(addr_u); vm32_size_t size = VM_SANITIZE_UNSAFE_UNWRAP(size_u); return os_add_overflow(addr, size, &addr_out_u->UNSAFE); } #endif /* MACH_KERNEL_PRIVATE */ __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) { mach_vm_address_t addr = VM_SANITIZE_UNSAFE_UNWRAP(addr_u); mach_vm_size_t size = VM_SANITIZE_UNSAFE_UNWRAP(size_u); return os_add_overflow(addr, size, &addr_out_u->UNSAFE); } __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) { mach_vm_address_t size1 = VM_SANITIZE_UNSAFE_UNWRAP(size1_u); mach_vm_size_t size2 = VM_SANITIZE_UNSAFE_UNWRAP(size2_u); return os_add_overflow(size1, size2, &size_out_u->UNSAFE); } /* * vm_*_no_ubsan is acceptable in these functions since they operate on unsafe * types. The return value is also an unsafe type and must be sanitized before * it can be used in other functions. */ __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) { vm_addr_struct_t end_u = { 0 }; vm_address_t addr_local = VM_SANITIZE_UNSAFE_UNWRAP(addr_u); vm_size_t size_local = VM_SANITIZE_UNSAFE_UNWRAP(size_u); VM_SANITIZE_UT_SET(end_u, vm_add_no_ubsan(addr_local, size_local)); return end_u; } __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) { vm_size_struct_t size_u = { 0 }; vm_address_t addr_local = VM_SANITIZE_UNSAFE_UNWRAP(addr_u); vm_address_t end_local = VM_SANITIZE_UNSAFE_UNWRAP(end_u); VM_SANITIZE_UT_SET(size_u, vm_sub_no_ubsan(end_local, addr_local)); return size_u; } __attribute__((always_inline, warn_unused_result)) mach_vm_address_t vm_sanitize_addr( vm_map_t map, vm_addr_struct_t addr_u) { mach_vm_address_t addr = VM_SANITIZE_UNSAFE_UNWRAP(addr_u); vm_map_offset_t pgmask = vm_map_page_mask(map); return vm_map_trunc_page_mask(addr, pgmask); } __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) { return VM_SANITIZE_UNSAFE_UNWRAP(addr_u) & mask; } __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 __unused, vm_map_address_t addr, vm_map_address_t end, vm_map_offset_t *offset) { *offset = VM_SANITIZE_UNSAFE_UNWRAP(offset_u); if ((*offset < addr) || (*offset > end)) { *offset = 0; return KERN_INVALID_ARGUMENT; } return KERN_SUCCESS; } __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 __unused, vm_map_offset_t *mask) { *mask = VM_SANITIZE_UNSAFE_UNWRAP(mask_u); /* * Adding validation to mask has high ABI risk and low security value. * The only internal function that deals with mask is vm_map_locate_space * and it currently ensures that addresses are aligned to page boundary * even for weird alignment requests. * * rdar://120445665 */ return KERN_SUCCESS; } __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 __unused, vm_sanitize_flags_t flags, vm_object_offset_t *size) { mach_vm_size_t size_aligned; *size = VM_SANITIZE_UNSAFE_UNWRAP(size_u); /* * Handle size zero as requested by the caller */ if (*size == 0) { if (flags & VM_SANITIZE_FLAGS_SIZE_ZERO_SUCCEEDS) { return VM_ERR_RETURN_NOW; } else if (flags & VM_SANITIZE_FLAGS_SIZE_ZERO_FAILS) { return KERN_INVALID_ARGUMENT; } else { /* VM_SANITIZE_FLAGS_SIZE_ZERO_FALLTHROUGH - nothing to do */ return KERN_SUCCESS; } } size_aligned = vm_map_round_page_mask(*size, PAGE_MASK); if (size_aligned == 0) { *size = 0; return KERN_INVALID_ARGUMENT; } if (!(flags & VM_SANITIZE_FLAGS_GET_UNALIGNED_VALUES)) { *size = size_aligned; } return KERN_SUCCESS; } __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 __unused, vm_map_t map, vm_sanitize_flags_t flags, mach_vm_size_t *size) { mach_vm_size_t offset = VM_SANITIZE_UNSAFE_UNWRAP(offset_u); vm_map_offset_t pgmask = vm_map_page_mask(map); mach_vm_size_t size_aligned; *size = VM_SANITIZE_UNSAFE_UNWRAP(size_u); /* * Handle size zero as requested by the caller */ if (*size == 0) { if (flags & VM_SANITIZE_FLAGS_SIZE_ZERO_SUCCEEDS) { return VM_ERR_RETURN_NOW; } else if (flags & VM_SANITIZE_FLAGS_SIZE_ZERO_FAILS) { return KERN_INVALID_ARGUMENT; } else { /* VM_SANITIZE_FLAGS_SIZE_ZERO_FALLTHROUGH - nothing to do */ return KERN_SUCCESS; } } /* * Ensure that offset and size don't overflow when refering to the * vm_object */ if (os_add_overflow(*size, offset, &size_aligned)) { *size = 0; return KERN_INVALID_ARGUMENT; } /* * This rounding is a check on the vm_object and thus uses the kernel's PAGE_MASK */ if (vm_map_round_page_mask(size_aligned, PAGE_MASK) == 0) { *size = 0; return KERN_INVALID_ARGUMENT; } /* * Check that a non zero size being mapped doesn't round to 0 * * vm_sub_no_ubsan is acceptable here since the subtraction is guaranteed to * not overflow. We know size_aligned = *size + offset, and since that * addition did not overflow and offset >= offset & ~pgmask, this * subtraction also cannot overflow. */ size_aligned = vm_sub_no_ubsan(size_aligned, offset & ~pgmask); /* * This rounding is a check on the specified map and thus uses its pgmask */ size_aligned = vm_map_round_page_mask(size_aligned, pgmask); if (size_aligned == 0) { *size = 0; return KERN_INVALID_ARGUMENT; } if (!(flags & VM_SANITIZE_FLAGS_GET_UNALIGNED_VALUES)) { *size = size_aligned; } return KERN_SUCCESS; } static __attribute__((warn_unused_result)) kern_return_t vm_sanitize_err_compat_addr_size( kern_return_t initial_kr, vm_sanitize_caller_t vm_sanitize_caller, vm_addr_struct_t addr_u, vm_size_struct_t size_u, mach_vm_offset_t pgmask, vm_map_t map_or_null) { vm_sanitize_compat_rewrite_t compat = {initial_kr, false, false}; if (vm_sanitize_caller->err_compat_addr_size) { compat = (vm_sanitize_caller->err_compat_addr_size) (initial_kr, VM_SANITIZE_UNSAFE_UNWRAP(addr_u), VM_SANITIZE_UNSAFE_UNWRAP(size_u), pgmask, map_or_null); } if (compat.should_telemeter) { #if DEVELOPMENT || DEBUG if (vm_sanitize_telemeter_to_serial) { printf("VM API - [%s] unsanitary addr 0x%llx size 0x%llx pgmask " "0x%llx passed to %s; error code %d may become %d\n", proc_best_name(current_proc()), VM_SANITIZE_UNSAFE_UNWRAP(addr_u), VM_SANITIZE_UNSAFE_UNWRAP(size_u), pgmask, vm_sanitize_caller->vmsc_caller_name, initial_kr, compat.compat_kr); } #endif /* DEVELOPMENT || DEBUG */ vm_sanitize_send_telemetry( vm_sanitize_caller->vmsc_telemetry_id, VM_SANITIZE_CHECKER_ADDR_SIZE, VM_SANITIZE_CHECKER_COUNT_1 /* fixme */, vm_sanitize_caller->vmsc_ktriage_id, VM_SANITIZE_UNSAFE_UNWRAP(addr_u), VM_SANITIZE_UNSAFE_UNWRAP(size_u), pgmask, 0 /* arg4 */, initial_kr, compat.compat_kr); } return vm_sanitize_apply_err_rewrite_policy(initial_kr, compat); } __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 pgmask, 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) { /* * map_or_null is not available from all call sites. * Use pgmask instead of vm_map_page_mask(map) for alignment. */ vm_map_offset_t addr_aligned = 0; vm_map_offset_t end_aligned = 0, end_unaligned = 0; kern_return_t kr; *addr = VM_SANITIZE_UNSAFE_UNWRAP(addr_u); *size = VM_SANITIZE_UNSAFE_UNWRAP(size_u); if (flags & VM_SANITIZE_FLAGS_REALIGN_START) { assert(!(flags & VM_SANITIZE_FLAGS_GET_UNALIGNED_VALUES)); } #if CONFIG_KERNEL_TAGGING if (flags & VM_SANITIZE_FLAGS_CANONICALIZE) { *addr = vm_memtag_canonicalize_address(*addr); } #endif /* CONFIG_KERNEL_TAGGING */ addr_aligned = vm_map_trunc_page_mask(*addr, pgmask); /* * Ensure that the address is aligned */ if (__improbable((flags & VM_SANITIZE_FLAGS_CHECK_ALIGNED_START) && (*addr & pgmask))) { kr = KERN_INVALID_ARGUMENT; goto unsanitary; } /* * Ensure that the size is aligned */ if (__improbable((flags & VM_SANITIZE_FLAGS_CHECK_ALIGNED_SIZE) && (*size & pgmask))) { kr = KERN_INVALID_ARGUMENT; goto unsanitary; } /* * Handle size zero as requested by the caller */ if (*size == 0) { if (flags & VM_SANITIZE_FLAGS_SIZE_ZERO_SUCCEEDS) { *addr = 0; *end = 0; /* size is already 0 */ return VM_ERR_RETURN_NOW; } else if (flags & VM_SANITIZE_FLAGS_SIZE_ZERO_FAILS) { kr = KERN_INVALID_ARGUMENT; goto unsanitary; } else { /* VM_SANITIZE_FLAGS_SIZE_ZERO_FALLTHROUGH - nothing to do */ if (flags & VM_SANITIZE_FLAGS_GET_UNALIGNED_VALUES) { /* addr is already set */ *end = *addr; /* size is already 0 */ return KERN_SUCCESS; } else { *addr = addr_aligned; *end = addr_aligned; /* size is already 0 */ return KERN_SUCCESS; } } } /* * Compute the aligned end now */ if (flags & VM_SANITIZE_FLAGS_REALIGN_START) { *addr = addr_aligned; } if (__improbable(os_add_overflow(*addr, *size, &end_unaligned))) { kr = KERN_INVALID_ARGUMENT; goto unsanitary; } end_aligned = vm_map_round_page_mask(end_unaligned, pgmask); if (__improbable(end_aligned <= addr_aligned)) { kr = KERN_INVALID_ARGUMENT; goto unsanitary; } if (flags & VM_SANITIZE_FLAGS_GET_UNALIGNED_VALUES) { /* addr and size are already set */ *end = end_unaligned; } else { *addr = addr_aligned; *end = end_aligned; /* * vm_sub_no_ubsan is acceptable since the subtraction is guaranteed to * not overflow, as we have already verified end_aligned > addr_aligned. */ *size = vm_sub_no_ubsan(end_aligned, addr_aligned); } return KERN_SUCCESS; unsanitary: *addr = 0; *end = 0; *size = 0; return vm_sanitize_err_compat_addr_size(kr, vm_sanitize_caller, addr_u, size_u, pgmask, map_or_null); } __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_size_struct_t size_u = vm_sanitize_compute_ut_size(addr_u, end_u); return vm_sanitize_addr_size(addr_u, size_u, vm_sanitize_caller, mask, map_or_null, flags, start, end, size); } __attribute__((always_inline, warn_unused_result)) kern_return_t vm_sanitize_prot( vm_prot_ut prot_u, vm_sanitize_caller_t vm_sanitize_caller __unused, vm_map_t map __unused, vm_prot_t extra_mask, vm_prot_t *prot) { *prot = VM_SANITIZE_UNSAFE_UNWRAP(prot_u); if (__improbable(*prot & ~(VM_SANITIZE_PROT_ALLOWED | extra_mask))) { *prot = VM_PROT_NONE; return KERN_INVALID_ARGUMENT; } #if defined(__x86_64__) if ((*prot & VM_PROT_UEXEC) && !pmap_supported_feature(map->pmap, PMAP_FEAT_UEXEC)) { *prot = VM_PROT_NONE; return KERN_INVALID_ARGUMENT; } #endif return KERN_SUCCESS; } /* * *out_cur and *out_max are modified when there is an err compat rewrite * otherwise they are left unchanged */ static __attribute__((warn_unused_result)) kern_return_t vm_sanitize_err_compat_cur_and_max_prots( kern_return_t initial_kr, vm_sanitize_caller_t vm_sanitize_caller, vm_prot_ut cur_prot_u, vm_prot_ut max_prot_u, vm_prot_t extra_mask, vm_prot_t *out_cur, vm_prot_t *out_max) { vm_prot_t initial_cur_prot = VM_SANITIZE_UNSAFE_UNWRAP(cur_prot_u); vm_prot_t initial_max_prot = VM_SANITIZE_UNSAFE_UNWRAP(max_prot_u); vm_sanitize_compat_rewrite_t compat = {initial_kr, false, false}; vm_prot_t compat_cur_prot = initial_cur_prot; vm_prot_t compat_max_prot = initial_max_prot; if (vm_sanitize_caller->err_compat_prot_cur_max) { compat = (vm_sanitize_caller->err_compat_prot_cur_max) (initial_kr, &compat_cur_prot, &compat_max_prot, extra_mask); } if (compat.should_telemeter) { #if DEVELOPMENT || DEBUG if (vm_sanitize_telemeter_to_serial) { printf("VM API - [%s] unsanitary vm_prot cur %d max %d " "passed to %s; error code %d may become %d\n", proc_best_name(current_proc()), initial_cur_prot, initial_max_prot, vm_sanitize_caller->vmsc_caller_name, initial_kr, compat.compat_kr); } #endif /* DEVELOPMENT || DEBUG */ vm_sanitize_send_telemetry( vm_sanitize_caller->vmsc_telemetry_id, VM_SANITIZE_CHECKER_PROT_CUR_MAX, VM_SANITIZE_CHECKER_COUNT_1 /* fixme */, vm_sanitize_caller->vmsc_ktriage_id, initial_cur_prot, initial_max_prot, extra_mask, 0 /* arg4 */, initial_kr, compat.compat_kr); } if (compat.should_rewrite) { *out_cur = compat_cur_prot; *out_max = compat_max_prot; return compat.compat_kr; } else { /* out_cur and out_max unchanged */ return initial_kr; } } __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) { kern_return_t kr; kr = vm_sanitize_prot(cur_prot_u, vm_sanitize_caller, map, extra_mask, cur_prot); if (__improbable(kr != KERN_SUCCESS)) { *cur_prot = VM_PROT_NONE; *max_prot = VM_PROT_NONE; return kr; } kr = vm_sanitize_prot(max_prot_u, vm_sanitize_caller, map, extra_mask, max_prot); if (__improbable(kr != KERN_SUCCESS)) { *cur_prot = VM_PROT_NONE; *max_prot = VM_PROT_NONE; return kr; } /* * This check needs to be performed on the actual protection bits. * vm_sanitize_prot restricts cur and max prot to * (VM_PROT_ALL | VM_PROT_ALLEXEC | extra_mask), but we don't enforce * ordering on the extra_mask bits. */ if (__improbable((*cur_prot & *max_prot & VM_SANITIZE_PROT_ALLOWED) != (*cur_prot & VM_SANITIZE_PROT_ALLOWED))) { /* cur is more permissive than max */ kr = KERN_INVALID_ARGUMENT; goto unsanitary; } return KERN_SUCCESS; unsanitary: *cur_prot = VM_PROT_NONE; *max_prot = VM_PROT_NONE; /* error compat may set cur/max to something other than 0/0 */ return vm_sanitize_err_compat_cur_and_max_prots(kr, vm_sanitize_caller, cur_prot_u, max_prot_u, extra_mask, cur_prot, max_prot); } __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 __unused) { vm_prot_t prot = VM_SANITIZE_UNSAFE_UNWRAP(prot_u); /* * Strip all protections that are not allowed */ prot &= (VM_PROT_ALL | VM_PROT_TRUSTED | VM_PROT_STRIP_READ); return prot; } __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 __unused, vm_sanitize_flags_t flags, vm_prot_t extra_mask, vm_prot_t *perm) { vm_prot_t prot; vm_prot_t map_mem_flags; vm_prot_t access; *perm = VM_SANITIZE_UNSAFE_UNWRAP(perm_u); prot = *perm & MAP_MEM_PROT_MASK; map_mem_flags = *perm & MAP_MEM_FLAGS_MASK; access = GET_MAP_MEM(*perm); if ((flags & VM_SANITIZE_FLAGS_CHECK_USER_MEM_MAP_FLAGS) && (map_mem_flags & ~MAP_MEM_FLAGS_USER)) { /* * Unknown flag: reject for forward compatibility. */ *perm = VM_PROT_NONE; return KERN_INVALID_VALUE; } /* * Clear prot bits in perm and set them to only allowed values */ *perm &= ~MAP_MEM_PROT_MASK; *perm |= (prot & (VM_PROT_ALL | extra_mask)); /* * No checks on access */ (void) access; return KERN_SUCCESS; } __attribute__((always_inline, warn_unused_result)) kern_return_t vm_sanitize_inherit( vm_inherit_ut inherit_u, vm_sanitize_caller_t vm_sanitize_caller __unused, vm_inherit_t *inherit) { *inherit = VM_SANITIZE_UNSAFE_UNWRAP(inherit_u); if (__improbable(*inherit > VM_INHERIT_LAST_VALID)) { *inherit = VM_INHERIT_NONE; return KERN_INVALID_ARGUMENT; } return KERN_SUCCESS; } __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) { *behavior = VM_SANITIZE_UNSAFE_UNWRAP(behavior_u); if (__improbable((*behavior > VM_BEHAVIOR_LAST_VALID) || (*behavior < 0))) { *behavior = VM_BEHAVIOR_DEFAULT; return KERN_INVALID_ARGUMENT; } return KERN_SUCCESS; } #if DEBUG || DEVELOPMENT static bool vm_sanitize_offset_test(void) { kern_return_t kr = KERN_SUCCESS; vm_map_offset_t offset; vm_map_address_t addr, end; vm_addr_struct_t offset_u; /* * Offset that is less than lower bound */ offset_u = vm_sanitize_wrap_addr(0); addr = 5; end = 10; kr = vm_sanitize_offset(offset_u, VM_SANITIZE_CALLER_TEST, addr, end, &offset); if (vm_sanitize_get_kr(kr) == KERN_SUCCESS) { printf("%s: failed for addr %p end %p offset %p\n", __func__, (void *)addr, (void *)end, (void *)VM_SANITIZE_UNSAFE_UNWRAP(offset_u)); return false; } /* * Offset that is less than lower bound */ offset_u = vm_sanitize_wrap_addr(11); addr = 5; end = 10; kr = KERN_SUCCESS; kr = vm_sanitize_offset(offset_u, VM_SANITIZE_CALLER_TEST, addr, end, &offset); if (vm_sanitize_get_kr(kr) == KERN_SUCCESS) { printf("%s: failed for addr %p end %p offset %p\n", __func__, (void *)addr, (void *)end, (void *)VM_SANITIZE_UNSAFE_UNWRAP(offset_u)); return false; } printf("%s: passed\n", __func__); return true; } static bool vm_sanitize_size_test(void) { kern_return_t kr = KERN_SUCCESS; vm_map_size_t size; vm_addr_struct_t offset_u; vm_size_struct_t size_u; /* * VM_SANITIZE_FLAGS_SIZE_ZERO_SUCCEEDS should return VM_ERR_RETURN_NOW for size = 0 * for callers that need to return success early */ offset_u = vm_sanitize_wrap_addr(PAGE_SIZE + 1); size_u = vm_sanitize_wrap_size(0); kr = vm_sanitize_size(offset_u, size_u, VM_SANITIZE_CALLER_TEST, current_map(), VM_SANITIZE_FLAGS_SIZE_ZERO_SUCCEEDS, &size); if (vm_sanitize_get_kr(kr) != KERN_SUCCESS || kr != VM_ERR_RETURN_NOW) { printf("%s: VM_SANITIZE_FLAGS_SIZE_ZERO_SUCCEEDS failed for offset %p size %p\n", __func__, (void *)VM_SANITIZE_UNSAFE_UNWRAP(offset_u), (void *)VM_SANITIZE_UNSAFE_UNWRAP(size_u)); return false; } /* * VM_SANITIZE_FLAGS_SIZE_ZERO_FAILS should return failure for size = 0 */ offset_u = vm_sanitize_wrap_addr(PAGE_SIZE + 1); size_u = vm_sanitize_wrap_size(0); kr = KERN_SUCCESS; kr = vm_sanitize_size(offset_u, size_u, VM_SANITIZE_CALLER_TEST, current_map(), VM_SANITIZE_FLAGS_SIZE_ZERO_FAILS, &size); if (vm_sanitize_get_kr(kr) == KERN_SUCCESS) { printf("%s: VM_SANITIZE_FLAGS_SIZE_ZERO_FAILS failed for offset %p size %p\n", __func__, (void *)VM_SANITIZE_UNSAFE_UNWRAP(offset_u), (void *)VM_SANITIZE_UNSAFE_UNWRAP(size_u)); return false; } /* * VM_SANITIZE_FLAGS_SIZE_ZERO_FALLTHROUGH should return success for size = 0 */ offset_u = vm_sanitize_wrap_addr(PAGE_SIZE + 1); size_u = vm_sanitize_wrap_size(0); kr = KERN_SUCCESS; kr = vm_sanitize_size(offset_u, size_u, VM_SANITIZE_CALLER_TEST, current_map(), VM_SANITIZE_FLAGS_SIZE_ZERO_FALLTHROUGH, &size); if (vm_sanitize_get_kr(kr) != KERN_SUCCESS) { printf("%s: VM_SANITIZE_FLAGS_SIZE_ZERO_FALLTHROUGH failed for offset %p " "size %p\n", __func__, (void *)VM_SANITIZE_UNSAFE_UNWRAP(offset_u), (void *)VM_SANITIZE_UNSAFE_UNWRAP(size_u)); return false; } /* * VM_SANITIZE_FLAGS_GET_UNALIGNED_VALUES should return unaligned values */ offset_u = vm_sanitize_wrap_addr(PAGE_SIZE + 1); size_u = vm_sanitize_wrap_size(PAGE_SIZE + 1); kr = KERN_SUCCESS; kr = vm_sanitize_size(offset_u, size_u, VM_SANITIZE_CALLER_TEST, current_map(), VM_SANITIZE_FLAGS_GET_UNALIGNED_VALUES | VM_SANITIZE_FLAGS_SIZE_ZERO_FALLTHROUGH, &size); if ((vm_sanitize_get_kr(kr) != KERN_SUCCESS) || (size != PAGE_SIZE + 1)) { printf("%s: VM_SANITIZE_FLAGS_GET_UNALIGNED_VALUES failed for offset %p size %p\n", __func__, (void *)VM_SANITIZE_UNSAFE_UNWRAP(offset_u), (void *)VM_SANITIZE_UNSAFE_UNWRAP(size_u)); return false; } /* * Values that overflow */ offset_u = vm_sanitize_wrap_addr(2 * PAGE_SIZE); size_u = vm_sanitize_wrap_size(-PAGE_SIZE - 1); kr = KERN_SUCCESS; kr = vm_sanitize_size(offset_u, size_u, VM_SANITIZE_CALLER_TEST, current_map(), VM_SANITIZE_FLAGS_SIZE_ZERO_FALLTHROUGH, &size); if (vm_sanitize_get_kr(kr) == KERN_SUCCESS) { printf("%s: failed for offset %p size %p\n", __func__, (void *)VM_SANITIZE_UNSAFE_UNWRAP(offset_u), (void *)VM_SANITIZE_UNSAFE_UNWRAP(size_u)); return false; } /* * Values that overflow when rounding */ offset_u = vm_sanitize_wrap_addr(0); size_u = vm_sanitize_wrap_size(-1); kr = KERN_SUCCESS; kr = vm_sanitize_size(offset_u, size_u, VM_SANITIZE_CALLER_TEST, current_map(), VM_SANITIZE_FLAGS_SIZE_ZERO_FALLTHROUGH, &size); if (vm_sanitize_get_kr(kr) == KERN_SUCCESS) { printf("%s: failed for offset %p size %p\n", __func__, (void *)VM_SANITIZE_UNSAFE_UNWRAP(offset_u), (void *)VM_SANITIZE_UNSAFE_UNWRAP(size_u)); return false; } /* * Values that overflow when rounding */ offset_u = vm_sanitize_wrap_addr(-2); size_u = vm_sanitize_wrap_size(1); kr = KERN_SUCCESS; kr = vm_sanitize_size(offset_u, size_u, VM_SANITIZE_CALLER_TEST, current_map(), VM_SANITIZE_FLAGS_SIZE_ZERO_FALLTHROUGH, &size); if (vm_sanitize_get_kr(kr) == KERN_SUCCESS) { printf("%s: failed for offset %p size %p\n", __func__, (void *)VM_SANITIZE_UNSAFE_UNWRAP(offset_u), (void *)VM_SANITIZE_UNSAFE_UNWRAP(size_u)); return false; } printf("%s: passed\n", __func__); return true; } static bool vm_sanitize_addr_size_test(void) { kern_return_t kr = KERN_SUCCESS; vm_map_address_t start, end; vm_map_size_t size; vm_addr_struct_t offset_u; vm_size_struct_t size_u; /* * VM_SANITIZE_FLAGS_CHECK_ALIGNED_START should fail on passing unaligned offset */ offset_u = vm_sanitize_wrap_addr(PAGE_SIZE + 1); size_u = vm_sanitize_wrap_size(PAGE_SIZE); kr = vm_sanitize_addr_size(offset_u, size_u, VM_SANITIZE_CALLER_TEST, PAGE_MASK, VM_SANITIZE_FLAGS_CHECK_ALIGNED_START | VM_SANITIZE_FLAGS_SIZE_ZERO_FALLTHROUGH, &start, &end, &size); if (vm_sanitize_get_kr(kr) == KERN_SUCCESS) { printf("%s: VM_SANITIZE_FLAGS_CHECK_ALIGNED_START failed for offset %p size %p\n", __func__, (void *)VM_SANITIZE_UNSAFE_UNWRAP(offset_u), (void *)VM_SANITIZE_UNSAFE_UNWRAP(size_u)); return false; } /* * VM_SANITIZE_FLAGS_SIZE_ZERO_SUCCEEDS should return VM_ERR_RETURN_NOW for size = 0 * for callers that need to return success early */ offset_u = vm_sanitize_wrap_addr(PAGE_SIZE + 1); size_u = vm_sanitize_wrap_size(0); kr = KERN_SUCCESS; kr = vm_sanitize_addr_size(offset_u, size_u, VM_SANITIZE_CALLER_TEST, PAGE_MASK, VM_SANITIZE_FLAGS_SIZE_ZERO_SUCCEEDS, &start, &end, &size); if (vm_sanitize_get_kr(kr) != KERN_SUCCESS || kr != VM_ERR_RETURN_NOW) { printf("%s: VM_SANITIZE_FLAGS_SIZE_ZERO_SUCCEEDS failed for offset %p size %p\n", __func__, (void *)VM_SANITIZE_UNSAFE_UNWRAP(offset_u), (void *)VM_SANITIZE_UNSAFE_UNWRAP(size_u)); return false; } /* * VM_SANITIZE_FLAGS_SIZE_ZERO_FAILS should return failure for size = 0 */ offset_u = vm_sanitize_wrap_addr(PAGE_SIZE + 1); size_u = vm_sanitize_wrap_size(0); kr = KERN_SUCCESS; kr = vm_sanitize_addr_size(offset_u, size_u, VM_SANITIZE_CALLER_TEST, PAGE_MASK, VM_SANITIZE_FLAGS_SIZE_ZERO_FAILS, &start, &end, &size); if (vm_sanitize_get_kr(kr) == KERN_SUCCESS) { printf("%s: VM_SANITIZE_FLAGS_SIZE_ZERO_FAILS failed for offset %p size %p\n", __func__, (void *)VM_SANITIZE_UNSAFE_UNWRAP(offset_u), (void *)VM_SANITIZE_UNSAFE_UNWRAP(size_u)); return false; } /* * VM_SANITIZE_FLAGS_SIZE_ZERO_FALLTHROUGH should return success for size = 0 */ offset_u = vm_sanitize_wrap_addr(PAGE_SIZE + 1); size_u = vm_sanitize_wrap_size(0); kr = KERN_SUCCESS; kr = vm_sanitize_addr_size(offset_u, size_u, VM_SANITIZE_CALLER_TEST, PAGE_MASK, VM_SANITIZE_FLAGS_SIZE_ZERO_FALLTHROUGH, &start, &end, &size); if ((vm_sanitize_get_kr(kr) != KERN_SUCCESS) || (start != PAGE_SIZE) || (end != PAGE_SIZE)) { printf("%s: VM_SANITIZE_FLAGS_SIZE_ZERO_FALLTHROUGH failed for offset %p " "size %p\n", __func__, (void *)VM_SANITIZE_UNSAFE_UNWRAP(offset_u), (void *)VM_SANITIZE_UNSAFE_UNWRAP(size_u)); return false; } /* * VM_SANITIZE_FLAGS_GET_UNALIGNED_VALUES should return unaligned values */ offset_u = vm_sanitize_wrap_addr(PAGE_SIZE + 1); size_u = vm_sanitize_wrap_size(PAGE_SIZE); kr = KERN_SUCCESS; kr = vm_sanitize_addr_size(offset_u, size_u, VM_SANITIZE_CALLER_TEST, PAGE_MASK, VM_SANITIZE_FLAGS_GET_UNALIGNED_VALUES | VM_SANITIZE_FLAGS_SIZE_ZERO_FALLTHROUGH, &start, &end, &size); if ((vm_sanitize_get_kr(kr) != KERN_SUCCESS) || (start != PAGE_SIZE + 1) || (end != 2 * PAGE_SIZE + 1)) { printf("%s: VM_SANITIZE_FLAGS_GET_UNALIGNED_VALUES failed for offset %p size %p\n", __func__, (void *)VM_SANITIZE_UNSAFE_UNWRAP(offset_u), (void *)VM_SANITIZE_UNSAFE_UNWRAP(size_u)); return false; } /* * VM_SANITIZE_FLAGS_REALIGN_START should not use unaligned values for sanitization */ offset_u = vm_sanitize_wrap_addr(PAGE_SIZE + 1); size_u = vm_sanitize_wrap_size(PAGE_SIZE); kr = KERN_SUCCESS; kr = vm_sanitize_addr_size(offset_u, size_u, VM_SANITIZE_CALLER_TEST, PAGE_MASK, VM_SANITIZE_FLAGS_REALIGN_START | VM_SANITIZE_FLAGS_SIZE_ZERO_FALLTHROUGH, &start, &end, &size); if ((vm_sanitize_get_kr(kr) != KERN_SUCCESS) || (start != PAGE_SIZE) || (end != 2 * PAGE_SIZE)) { printf("%s: VM_SANITIZE_FLAGS_REALIGN_START failed for offset %p size %p\n", __func__, (void *)VM_SANITIZE_UNSAFE_UNWRAP(offset_u), (void *)VM_SANITIZE_UNSAFE_UNWRAP(size_u)); return false; } /* * Values that overflow */ offset_u = vm_sanitize_wrap_addr(2 * PAGE_SIZE); size_u = vm_sanitize_wrap_size(-PAGE_SIZE - 1); kr = KERN_SUCCESS; kr = vm_sanitize_addr_size(offset_u, size_u, VM_SANITIZE_CALLER_TEST, PAGE_MASK, VM_SANITIZE_FLAGS_SIZE_ZERO_FALLTHROUGH, &start, &end, &size); if (vm_sanitize_get_kr(kr) == KERN_SUCCESS) { printf("%s: failed for offset %p size %p\n", __func__, (void *)VM_SANITIZE_UNSAFE_UNWRAP(offset_u), (void *)VM_SANITIZE_UNSAFE_UNWRAP(size_u)); return false; } printf("%s: passed\n", __func__); return true; } static bool vm_sanitize_prot_test(void) { kern_return_t kr = KERN_SUCCESS; vm_prot_ut prot_u; vm_prot_t prot; prot_u = vm_sanitize_wrap_prot(VM_PROT_NO_CHANGE_LEGACY | VM_PROT_NO_CHANGE | VM_PROT_COPY | VM_PROT_WANTS_COPY | VM_PROT_TRUSTED | VM_PROT_IS_MASK | VM_PROT_STRIP_READ | VM_PROT_EXECUTE_ONLY | VM_PROT_COPY_FAIL_IF_EXECUTABLE | VM_PROT_TPRO); kr = vm_sanitize_prot(prot_u, VM_SANITIZE_CALLER_TEST, current_map(), VM_PROT_NONE, &prot); if (kr == KERN_SUCCESS) { printf("%s: failed for invalid set of permissions\n", __func__); return false; } printf("%s: passed\n", __func__); return true; } static bool vm_sanitize_cur_and_max_prots_test(void) { kern_return_t kr = KERN_SUCCESS; vm_prot_ut cur_prot_u, max_prot_u; vm_prot_t cur_prot, max_prot; /* * Validate that incompatible prots are rejected */ cur_prot_u = vm_sanitize_wrap_prot(VM_PROT_ALL); max_prot_u = vm_sanitize_wrap_prot(VM_PROT_READ); kr = vm_sanitize_cur_and_max_prots(cur_prot_u, max_prot_u, VM_SANITIZE_CALLER_TEST, current_map(), VM_PROT_NONE, &cur_prot, &max_prot); if (kr == KERN_SUCCESS) { printf("%s: failed for invalid set of permissions\n", __func__); return false; } printf("%s: passed\n", __func__); return true; } static bool vm_sanitize_prot_bsd_test(void) { vm_prot_ut prot_u; vm_prot_t prot; prot_u = vm_sanitize_wrap_prot(VM_PROT_NO_CHANGE_LEGACY | VM_PROT_NO_CHANGE | VM_PROT_COPY | VM_PROT_WANTS_COPY | VM_PROT_IS_MASK | VM_PROT_COPY_FAIL_IF_EXECUTABLE | VM_PROT_TPRO); prot = vm_sanitize_prot_bsd(prot_u, VM_SANITIZE_CALLER_TEST); if (prot != VM_PROT_NONE) { printf("%s: failed to strip invalid permissions\n", __func__); return false; } printf("%s: passed\n", __func__); return true; } static bool vm_sanitize_memory_entry_perm_test(void) { kern_return_t kr = KERN_SUCCESS; vm_prot_ut perm_u; vm_prot_t perm; /* * Ensure invalid map_mem_flags is rejected */ perm_u = vm_sanitize_wrap_prot(0x001000); kr = vm_sanitize_memory_entry_perm(perm_u, VM_SANITIZE_CALLER_TEST, VM_SANITIZE_FLAGS_CHECK_USER_MEM_MAP_FLAGS, VM_PROT_IS_MASK, &perm); if (kr == KERN_SUCCESS) { printf("%s: failed to reject invalid map_mem_flags\n", __func__); return false; } /* * Ensure invalid prot bits are cleared */ kr = KERN_SUCCESS; perm_u = vm_sanitize_wrap_prot(VM_PROT_NO_CHANGE_LEGACY | VM_PROT_NO_CHANGE | VM_PROT_COPY | VM_PROT_WANTS_COPY | VM_PROT_EXECUTE_ONLY | VM_PROT_COPY_FAIL_IF_EXECUTABLE | VM_PROT_TPRO); kr = vm_sanitize_memory_entry_perm(perm_u, VM_SANITIZE_CALLER_TEST, VM_SANITIZE_FLAGS_CHECK_USER_MEM_MAP_FLAGS, VM_PROT_IS_MASK, &perm); if (perm != VM_PROT_NONE) { printf("%s: failed to clear invalid prot bits\n", __func__); return false; } printf("%s: passed\n", __func__); return true; } static bool vm_sanitize_inherit_test(void) { kern_return_t kr = KERN_SUCCESS; vm_inherit_ut inherit_u; vm_inherit_t inherit; /* * Ensure invalid values are rejected */ inherit_u = vm_sanitize_wrap_inherit(VM_INHERIT_DONATE_COPY); kr = vm_sanitize_inherit(inherit_u, VM_SANITIZE_CALLER_TEST, &inherit); if (kr == KERN_SUCCESS) { printf("%s: failed to reject invalid inherit values\n", __func__); return false; } printf("%s: passed\n", __func__); return true; } static bool vm_sanitize_behavior_test(void) { kern_return_t kr = KERN_SUCCESS; vm_behavior_ut behavior_u; vm_behavior_t behavior; /* * Ensure invalid values are rejected */ behavior_u = vm_sanitize_wrap_behavior(VM_BEHAVIOR_LAST_VALID + 1); kr = vm_sanitize_behavior(behavior_u, VM_SANITIZE_CALLER_TEST, &behavior); if (kr == KERN_SUCCESS) { printf("%s: failed to reject invalid behavior value\n", __func__); return false; } printf("%s: passed\n", __func__); return true; } /* * Verify that u0..u15 == t_arg[0]..t_arg[15], then return ret. * If there are ABI problems then the parameters or return * value may be passed incorrectly. We use a large number * of parameters in order to fill the ABI's parameter * registers and spill onto the stack. */ #define TEST_UT_TYPE_ABI_UT_CALLEE(type_t, type_ut, equal, t_arg) \ static_assert(sizeof(type_ut) == sizeof(type_t)); \ static_assert(__alignof__(type_ut) == __alignof__(type_t)); \ \ __attribute__((used, noinline)) \ static type_ut \ vm_sanitize_test_##type_ut##_callee( \ type_ut u0, type_ut u1, type_ut u2, type_ut u3, \ type_ut u4, type_ut u5, type_ut u6, type_ut u7, \ type_ut u8, type_ut u9, type_ut u10, type_ut u11, \ type_ut u12, type_ut u13, type_ut u14, type_ut u15, \ type_ut ret, \ bool *out_params_ok) { \ asm(""); \ \ *out_params_ok = ( \ equal(u0.UNSAFE, t_arg[0]) && equal(u1.UNSAFE, t_arg[1]) && \ equal(u2.UNSAFE, t_arg[2]) && equal(u3.UNSAFE, t_arg[3]) && \ equal(u4.UNSAFE, t_arg[4]) && equal(u5.UNSAFE, t_arg[5]) && \ equal(u6.UNSAFE, t_arg[6]) && equal(u7.UNSAFE, t_arg[7]) && \ equal(u8.UNSAFE, t_arg[8]) && equal(u9.UNSAFE, t_arg[9]) && \ equal(u10.UNSAFE, t_arg[10]) && equal(u11.UNSAFE, t_arg[11]) && \ equal(u12.UNSAFE, t_arg[12]) && equal(u13.UNSAFE, t_arg[13]) && \ equal(u14.UNSAFE, t_arg[14]) && equal(u15.UNSAFE, t_arg[15]) \ ); \ \ /* return value is checked by the caller */ \ return ret; \ } /* * Make a function pointer that points to the function above, * but with a function type that has type_t parameters * instead of type_ut. * * This is the same type mismatch that occurs when * the call site is outside the trust boundary and * the callee is inside it. */ #define TEST_UT_TYPE_ABI_T_CALLEE(type_t, type_ut) \ typedef type_t \ (*vm_sanitize_test_##type_t##_callee_t)( \ type_t u0, type_t u1, type_t u2, type_t u3, \ type_t u4, type_t u5, type_t u6, type_t u7, \ type_t u8, type_t u9, type_t u10, type_t u11, \ type_t u12, type_t u13, type_t u14, type_t u15, \ type_t ret, \ bool *out_params_ok); \ _Pragma("clang diagnostic push") \ _Pragma("clang diagnostic ignored \"-Wcast-align\"") \ _Pragma("clang diagnostic ignored \"-Wcast-function-type\"") \ static vm_sanitize_test_##type_t##_callee_t \ volatile vm_sanitize_test_##type_t##_callee = \ (vm_sanitize_test_##type_t##_callee_t) \ vm_sanitize_test_##type_ut##_callee; \ _Pragma("clang diagnostic pop") /* Verify type_t actual parameters passed to type_ut formal parameters */ #define TEST_UT_TYPE_ABI_T_CALLER(type_t, type_ut, equal, t_arg) \ static void \ vm_sanitize_test_##type_t##_abi( \ bool *out_params_ok, \ bool *out_ret_ok) { \ \ type_t t_ret = vm_sanitize_test_##type_t##_callee( \ t_arg[0], t_arg[1], t_arg[2], t_arg[3], \ t_arg[4], t_arg[5], t_arg[6], t_arg[7], \ t_arg[8], t_arg[9], t_arg[10], t_arg[11], \ t_arg[12], t_arg[13], t_arg[14], t_arg[15], \ t_arg[16], \ out_params_ok); \ *out_ret_ok = equal(t_ret, t_arg[16]); \ } /* Verify type_ut actual parameters passed to type_ut formal parameters */ #define TEST_UT_TYPE_ABI_UT_CALLER(type_t, type_ut, equal, t_arg) \ static void \ vm_sanitize_test_##type_ut##_abi( \ bool *out_params_ok, \ bool *out_ret_ok) { \ \ type_ut ut_ret = vm_sanitize_test_##type_ut##_callee( \ (type_ut){t_arg[0]}, (type_ut){t_arg[1]}, \ (type_ut){t_arg[2]}, (type_ut){t_arg[3]}, \ (type_ut){t_arg[4]}, (type_ut){t_arg[5]}, \ (type_ut){t_arg[6]}, (type_ut){t_arg[7]}, \ (type_ut){t_arg[8]}, (type_ut){t_arg[9]}, \ (type_ut){t_arg[10]}, (type_ut){t_arg[11]}, \ (type_ut){t_arg[12]}, (type_ut){t_arg[13]}, \ (type_ut){t_arg[14]}, (type_ut){t_arg[15]}, \ (type_ut){t_arg[16]}, \ out_params_ok); \ *out_ret_ok = equal(ut_ret.UNSAFE, t_arg[16]); \ } /* * Generate ABI testing functions for one type `type_t` * and its unsafe type `type_ut`. * * `equal(a, b)` is a function or macro that compares two `type_t`. */ #define TEST_UT_TYPE_ABI(type_t, type_ut, equal) \ TEST_UT_TYPE_ABI_UT_CALLEE(type_t, type_ut, equal, vm_sanitize_test_##type_t##_args) \ TEST_UT_TYPE_ABI_T_CALLEE(type_t, type_ut) \ TEST_UT_TYPE_ABI_T_CALLER(type_t, type_ut, equal, vm_sanitize_test_##type_t##_args) \ TEST_UT_TYPE_ABI_UT_CALLER(type_t, type_ut, equal, vm_sanitize_test_##type_t##_args) \ /* our test macro requires a single-token type name, not `struct mach_vm_range` */ typedef struct mach_vm_range mach_vm_range_s; /* mach_vm_range_us does not exist, but we'll create it for this test */ VM_GENERATE_UNSAFE_TYPE(mach_vm_range_s, mach_vm_range_us); /* * Parameter values to test. 16 input values plus 1 return value. * These arrays are used inside the macros above under the name `t_arg`. */ #define V 0xF89aFb00 static vm_behavior_t vm_sanitize_test_vm_behavior_t_args[17] = { V + 0, V + 1, V + 2, V + 3, V + 4, V + 5, V + 6, V + 7, V + 8, V + 9, V + 10, V + 11, V + 12, V + 13, V + 14, V + 15, V + 16 }; #undef V #define V 0xF89aFb00u static const vm_inherit_t vm_sanitize_test_vm_inherit_t_args[17] = { V + 0, V + 1, V + 2, V + 3, V + 4, V + 5, V + 6, V + 7, V + 8, V + 9, V + 10, V + 11, V + 12, V + 13, V + 14, V + 15, V + 16 }; #undef V #define V 0xF1234567F89aFb00ull static const mach_vm_address_t vm_sanitize_test_mach_vm_address_t_args[17] = { V + 0, V + 1, V + 2, V + 3, V + 4, V + 5, V + 6, V + 7, V + 8, V + 9, V + 10, V + 11, V + 12, V + 13, V + 14, V + 15, V + 16 }; #undef V #define V 0xF1234567F89aFb00ull static const mach_vm_range_s vm_sanitize_test_mach_vm_range_s_args[17] = { {V, V + 0x10000000100 * 0}, {V, V + 0x10000000100 * 1}, {V, V + 0x10000000100 * 2}, {V, V + 0x10000000100 * 3}, {V, V + 0x10000000100 * 4}, {V, V + 0x10000000100 * 5}, {V, V + 0x10000000100 * 6}, {V, V + 0x10000000100 * 7}, {V, V + 0x10000000100 * 8}, {V, V + 0x10000000100 * 9}, {V, V + 0x10000000100 * 10}, {V, V + 0x10000000100 * 11}, {V, V + 0x10000000100 * 12}, {V, V + 0x10000000100 * 13}, {V, V + 0x10000000100 * 14}, {V, V + 0x10000000100 * 15}, {V, V + 0x10000000100 * 16}, }; #undef V #define equal_scalar(a, b) (a) == (b) #define equal_range(a, b) \ ((a).min_address == (b).min_address && (a).max_address == (b).max_address) /* signed int */ TEST_UT_TYPE_ABI(vm_behavior_t, vm_behavior_ut, equal_scalar); /* unsigned int */ TEST_UT_TYPE_ABI(vm_inherit_t, vm_inherit_ut, equal_scalar); /* pointer-size int */ TEST_UT_TYPE_ABI(mach_vm_address_t, mach_vm_address_ut, equal_scalar); /* struct of two pointer-sized ints */ TEST_UT_TYPE_ABI(mach_vm_range_s, mach_vm_range_us, equal_range); #undef equal_scalar #undef equal_struct /* Call the ABI test function for one type and complain if it failed. */ static bool vm_sanitize_test_one_abi(void (*test_t)(bool*, bool*), const char *t_name) { bool params_ok, ret_ok; test_t(¶ms_ok, &ret_ok); if (!params_ok) { printf("vm_sanitize_ut_type_abi_test: mismatched %s parameter\n", t_name); return false; } if (!ret_ok) { printf("vm_sanitize_ut_type_abi_test: mismatched %s return\n", t_name); return false; } return true; } /* * Verify that the ABI conventions of a _ut type and its _t counterpart match. * * Sanitized types means that external call sites use type_t parameters and * internal definitions use type_ut parameters. Any mismatch between * type_t and type_ut at the function call ABI level may cause misinterpreted * parameter values or data type layouts. * * - same sizeof * - same alignof * - type_t actual parameter passed to type_ut formal parameter * - type_ut actual return value returned to type_t formal return */ static bool vm_sanitize_ut_type_abi_test(void) { bool passed = vm_sanitize_test_one_abi(vm_sanitize_test_vm_behavior_t_abi, "vm_behavior_t") && vm_sanitize_test_one_abi(vm_sanitize_test_vm_behavior_ut_abi, "vm_behavior_ut") && vm_sanitize_test_one_abi(vm_sanitize_test_vm_inherit_t_abi, "vm_inherit_t") && vm_sanitize_test_one_abi(vm_sanitize_test_vm_inherit_ut_abi, "vm_inherit_ut") && vm_sanitize_test_one_abi(vm_sanitize_test_mach_vm_address_t_abi, "mach_vm_address_t") && vm_sanitize_test_one_abi(vm_sanitize_test_mach_vm_address_ut_abi, "mach_vm_address_ut") && vm_sanitize_test_one_abi(vm_sanitize_test_mach_vm_range_s_abi, "mach_vm_range_s") && vm_sanitize_test_one_abi(vm_sanitize_test_mach_vm_range_us_abi, "mach_vm_range_us"); if (passed) { printf("%s: passed\n", __func__); } return passed; } static int vm_sanitize_run_test(int64_t in __unused, int64_t *out) { *out = 0; if (!vm_sanitize_offset_test() || !vm_sanitize_size_test() || !vm_sanitize_addr_size_test() || !vm_sanitize_prot_test() || !vm_sanitize_cur_and_max_prots_test() || !vm_sanitize_prot_bsd_test() || !vm_sanitize_memory_entry_perm_test() || !vm_sanitize_inherit_test() || !vm_sanitize_behavior_test() || !vm_sanitize_ut_type_abi_test()) { return 0; } printf("%s: All tests passed\n", __func__); *out = 1; return 0; } SYSCTL_TEST_REGISTER(vm_sanitize_test, vm_sanitize_run_test); #endif /* DEBUG || DEVELOPMENT */ #if PAGE_SHIFT == 0 #pragma clang attribute pop #endif #if !__OPTIMIZE__ #pragma clang attribute pop #endif |