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 | #include <darwintest.h> #include <darwintest_perf.h> T_GLOBAL_META(T_META_NAMESPACE("xnu.vm")); #include <machine/cpu_capabilities.h> #include <sys/mman.h> #include <errno.h> #include <fcntl.h> #include <stdint.h> #include <libkern/OSCacheControl.h> #include <unistd.h> #include <signal.h> #include <stdlib.h> #include <sys/sysctl.h> #include <mach/vm_param.h> #include <pthread.h> #include <os/thread_self_restrict.h> #include <mach/mach.h> #include <mach/mach_error.h> #include <mach/mach_init.h> #include <mach/mach_port.h> #include <mach/mach_vm.h> #include <mach/vm_map.h> #include <mach/task.h> T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true)); #if defined(__arm64__) /* PAGE_SIZE on ARM64 is an expression derived from a non-const global variable */ #define PAD_SIZE PAGE_MAX_SIZE #else #define PAD_SIZE PAGE_MIN_SIZE #endif /* Enumerations */ typedef enum _access_type { ACCESS_READ, ACCESS_WRITE, } access_type_t; typedef enum _fault_strategy { FAULT_STRAT_NONE, FAULT_STRAT_RW_TPRO, } fault_strategy_t; /* Structures */ typedef struct { uint64_t fault_count; fault_strategy_t fault_strategy; bool fault_expected; } fault_state_t; /* Globals */ static bool key_created = false; static pthread_key_t fault_state_key; /* * The pager will only map entries with TPRO if we need to perform fixups. * Otherwise it really is const. Ensure we forge a struct that will require * dynamic rebasing. */ typedef struct { void *reloc; uint32_t magic; char bytes[PAD_SIZE - 12]; } const_page_t; typedef struct { const_page_t one; const_page_t two; char ro[PAD_SIZE]; } const_state_t; #define MAGIC(state) (void *)&state->magic /* * Force known data into our __DATA_CONST segment. The pager will be responsible * for handling the mapping of this. */ __attribute__((section("__DATA_CONST,__pager"))) __attribute__((aligned(PAD_SIZE))) static const_state_t pager_state = { .one.reloc = &pager_state, .two.reloc = &pager_state, .one.magic = 0x41414141, .two.magic = 0x41414141, .ro = "CCCC" }; /* Allocate a fault_state_t, and associate it with the current thread. */ static fault_state_t * fault_state_create(void) { fault_state_t * fault_state = malloc(sizeof(fault_state_t)); if (fault_state) { fault_state->fault_count = 0; fault_state->fault_strategy = FAULT_STRAT_NONE; fault_state->fault_expected = false; if (pthread_setspecific(fault_state_key, fault_state)) { free(fault_state); fault_state = NULL; } } return fault_state; } /* Disassociate the given fault state from the current thread, and destroy it. */ static void fault_state_destroy(void * fault_state) { if (fault_state == NULL) { T_ASSERT_FAIL("Attempted to fault_state_destroy NULL"); } free(fault_state); } /* * A signal handler that attempts to resolve anticipated faults through use of * the os_thread_self_restrict_rwx functions. */ static void access_failed_handler(int signum) { fault_state_t * fault_state; /* This handler should ONLY handle SIGBUS. */ if (signum != SIGBUS) { T_ASSERT_FAIL("Unexpected signal sent to handler"); } if (!(fault_state = pthread_getspecific(fault_state_key))) { T_ASSERT_FAIL("Failed to retrieve fault state"); } if (!(fault_state->fault_expected)) { T_ASSERT_FAIL("Unexpected fault taken"); } /* We should not see a second fault. */ fault_state->fault_expected = false; switch (fault_state->fault_strategy) { case FAULT_STRAT_NONE: T_ASSERT_FAIL("No fault strategy"); /* Just in case we try to do something different. */ break; case FAULT_STRAT_RW_TPRO: os_thread_self_restrict_tpro_to_rw(); break; } fault_state->fault_count++; } /* * Attempt the specified access; if the access faults, this will return true; * otherwise, it will return false. */ static bool does_access_fault(access_type_t access_type, void * addr, uint32_t value) { uint64_t old_fault_count; uint64_t new_fault_count; fault_state_t * fault_state; struct sigaction old_action; /* Save area for any existing action. */ struct sigaction new_action; /* The action we wish to install for SIGBUS. */ bool retval = false; new_action.sa_handler = access_failed_handler; /* A handler for write failures. */ new_action.sa_mask = 0; /* Don't modify the mask. */ new_action.sa_flags = 0; /* Flags? Who needs those? */ if (addr == NULL) { T_ASSERT_FAIL("Access attempted against NULL"); } if (!(fault_state = pthread_getspecific(fault_state_key))) { T_ASSERT_FAIL("Failed to retrieve fault state"); } old_fault_count = fault_state->fault_count; /* Install a handler so that we can catch SIGBUS. */ sigaction(SIGBUS, &new_action, &old_action); /* Perform the requested operation. */ switch (access_type) { case ACCESS_READ: fault_state->fault_strategy = FAULT_STRAT_RW_TPRO; fault_state->fault_expected = true; __sync_synchronize(); #if defined(__arm64__) uint8_t a = *((volatile uint8_t *)addr); #endif __sync_synchronize(); fault_state->fault_expected = false; fault_state->fault_strategy = FAULT_STRAT_NONE; break; case ACCESS_WRITE: fault_state->fault_strategy = FAULT_STRAT_RW_TPRO; fault_state->fault_expected = true; __sync_synchronize(); *((volatile uint32_t *)addr) = value; __sync_synchronize(); fault_state->fault_expected = false; fault_state->fault_strategy = FAULT_STRAT_NONE; break; } /* Restore the old SIGBUS handler. */ sigaction(SIGBUS, &old_action, NULL); new_fault_count = fault_state->fault_count; if (new_fault_count > old_fault_count) { /* Indicate that we took a fault. */ retval = true; } return retval; } static bool does_read_fault(void * addr) { return does_access_fault(ACCESS_READ, addr, 0); } static bool does_write_fault(void * addr, uint32_t value) { return does_access_fault(ACCESS_WRITE, addr, value); } static bool has_pager_support(void) { uint32_t enabled = false; size_t output_size = sizeof(enabled); (void)sysctlbyname("vm.pmap_tpro_pagers", &enabled, &output_size, NULL, 0); return enabled; } static void cleanup(void) { fault_state_t * fault_state; if (!(fault_state = pthread_getspecific(fault_state_key))) { T_ASSERT_FAIL("Failed to retrieve fault state"); T_ASSERT_POSIX_ZERO(pthread_setspecific(fault_state_key, NULL), "Remove fault_state"); fault_state_destroy(fault_state); } if (key_created) { T_ASSERT_POSIX_ZERO(pthread_key_delete(fault_state_key), "Delete fault state key"); } return; } static void thread_self_restrict_test(void (^test)(void)) { int err = 0; T_SETUPBEGIN; T_ATEND(cleanup); /* Set up the necessary state for the test. */ err = pthread_key_create(&fault_state_key, fault_state_destroy); T_ASSERT_POSIX_ZERO(err, 0, "Create pthread key"); key_created = true; T_ASSERT_NOTNULL(fault_state_create(), "Create fault state"); T_SETUPEND; test(); } static void fork_child_test(const_page_t *state) { pid_t pid; int statloc; pid = fork(); if (pid == 0) { T_EXPECT_EQ(state->magic, 0x45454545, "Expected magic on fork"); os_thread_self_restrict_tpro_to_rw(); T_EXPECT_EQ(os_thread_self_restrict_tpro_is_writable(), true, "TPRO region configured as read-write in child"); T_EXPECT_EQ(does_write_fault((void *)&state->bytes, 0x47474747), 0, "write to pager backed memory in child (no fault)"); T_EXPECT_EQ(does_write_fault(MAGIC(state), 0x46464646), 0, "write to pager backed memory in child (no fault)"); exit(0); } if (pid < 0) { T_ASSERT_POSIX_SUCCESS(pid, "fork"); } waitpid(pid, &statloc, 0); } static void pager_test(const_page_t *state) { kern_return_t kr; uint32_t pre; vm_prot_t curprot, maxprot; mach_vm_address_t addr = 0; const_page_t *copy_state = NULL; mach_port_t cow_port = MACH_PORT_NULL; memory_object_size_t me_size = PAGE_SIZE; /* * Validate our initial status quo. TPRO permissions should be RO, * so we should be able to read from our pager backed mapping but * should fault when trying to write to it. */ T_EXPECT_EQ(os_thread_self_restrict_tpro_is_writable(), false, "TPRO region starts read-only"); T_EXPECT_EQ(does_read_fault(MAGIC(state)), 0, "read from pager backed memory"); T_EXPECT_EQ(does_write_fault(MAGIC(state), 0x43434343), 1, "write to pager backed memory (detect fault)"); /* * Toggle permissions to RW and attempt a write. We should succeed. */ os_thread_self_restrict_tpro_to_rw(); T_EXPECT_EQ(os_thread_self_restrict_tpro_is_writable(), true, "TPRO region configured as read-write"); T_EXPECT_EQ(does_write_fault(MAGIC(state), 0x44444444), 0, "write to pager backed memory (no fault)"); /* * Toggle permissions to RO and attempt a write. We should detect * the fault */ os_thread_self_restrict_tpro_to_ro(); T_EXPECT_EQ(does_write_fault(MAGIC(state), 0x45454545), 1, "write to pager backed memory (detect fault)"); /* * Fork a child process and ensure that writes into the pager backed * regions are not observed by the parent. They should now be COW. */ pre = state->magic; fork_child_test(state); T_EXPECT_EQ(pre, state->magic, "write from child should not be observed"); /* * Ensure that if we remap the target region in a shared manner that we * inherit TPRO. Remapping should be successful but we still rely on * TPRO permissions to toggle r--/rw- */ kr = mach_vm_remap(mach_task_self(), &addr, PAGE_SIZE, 0, /* mask */ VM_FLAGS_ANYWHERE, mach_task_self(), (mach_vm_address_t)state, FALSE, /* copy */ &curprot, &maxprot, VM_INHERIT_DEFAULT); T_EXPECT_POSIX_SUCCESS(kr, "mach_vm_remap(SHARED)"); copy_state = (const_page_t *)addr; os_thread_self_restrict_tpro_to_ro(); T_EXPECT_EQ(os_thread_self_restrict_tpro_is_writable(), false, "TPRO configured as read-only"); T_EXPECT_EQ(curprot, VM_PROT_READ, "TPRO region should be VM_PROT_READ"); T_EXPECT_EQ(does_write_fault(MAGIC(copy_state), 0x46464646), 1, "write to remapped region (detect fault)"); os_thread_self_restrict_tpro_to_rw(); T_EXPECT_EQ(does_write_fault(MAGIC(copy_state), 0x46464646), 0, "write to remapped region (no fault)"); T_EXPECT_EQ(0x46464646, state->magic, "write into copied region should be observed"); /* * Ensure that if we remap the region that we do not observe writes to * the new copy in __DATA_CONST itself. */ kr = mach_vm_remap(mach_task_self(), (mach_vm_address_t *)©_state, PAGE_SIZE, 0, /* mask */ VM_FLAGS_ANYWHERE, mach_task_self(), (mach_vm_address_t)state, TRUE, /* copy */ &curprot, &maxprot, VM_INHERIT_DEFAULT); T_EXPECT_POSIX_SUCCESS(kr, "mach_vm_remap(COPY)"); /* * Toggle TPRO RW and write to the new copied region */ pre = state->magic; os_thread_self_restrict_tpro_to_rw(); T_EXPECT_EQ(os_thread_self_restrict_tpro_is_writable(), true, "TPRO region configured as read-write"); T_EXPECT_EQ(does_write_fault(MAGIC(copy_state), 0x46464646), 0, "write to pager backed memory (no fault)"); T_EXPECT_EQ(pre, state->magic, "write into copied region should not be observed"); /* * Make a memory entry for our target region and attempt to map it in * in a shared fashion. We should succeed but it should transparently * copy the target VM object as extracting TPRO VM entries will fail. * Writes to the new region should therefore not be observed. */ me_size = PAGE_SIZE; kr = mach_make_memory_entry_64(mach_task_self(), &me_size, (mach_vm_address_t)state, MAP_MEM_VM_SHARE | VM_PROT_READ | VM_PROT_WRITE, &cow_port, MACH_PORT_NULL); T_EXPECT_POSIX_SUCCESS(kr, "mach_make_memory_entry_64(MAP_MEM_VM_SHARE)"); pre = state->magic; T_EXPECT_EQ(does_write_fault(MAGIC(copy_state), 0x48484849), 0, "write to mapped copy region (no fault)"); T_EXPECT_EQ(pre, state->magic, "write into copied region should not be observed"); copy_state = NULL; kr = mach_vm_map(mach_task_self(), (mach_vm_address_t *)©_state, PAGE_SIZE, 0, /* mask */ VM_FLAGS_ANYWHERE, cow_port, 0, /* offset */ TRUE, /* copy */ VM_PROT_READ | VM_PROT_WRITE, VM_PROT_READ | VM_PROT_WRITE, VM_INHERIT_DEFAULT); T_EXPECT_POSIX_SUCCESS(kr, "mach_vm_map(cow_port)"); /* * Pages of the copy will no longer be mapped in as TPRO. Both * read/writes should work even with TPRO toggled RO. */ pre = state->magic; os_thread_self_restrict_tpro_to_ro(); T_EXPECT_EQ(does_write_fault(MAGIC(copy_state), 0x48484848), 0, "write to mapped copy region (no fault)"); T_EXPECT_EQ(pre, state->magic, "write into copied region should not be observed"); /* * We've explored a number of ways to perform copies on the target * objects in __DATA_CONST. Our first target page (&pager_state.one) * should now be marked RO without TPRO permissions to handle any * incoming write faults. Write to it directly again to ensure we * fault back in with TPRO permissions. */ os_thread_self_restrict_tpro_to_ro(); T_EXPECT_EQ(does_write_fault(MAGIC(state), 0x49494949), 1, "write to pager backed memory (detect fault)"); os_thread_self_restrict_tpro_to_rw(); T_EXPECT_EQ(does_write_fault(MAGIC(state), 0x4a4a4a4a), 0, "write to pager backed memory (no fault)"); /* * Now we attempt to have the page paged out. On systems which support the * compressor, we'll get paged out/compressed. On fault we should * be pmapped back in with TPRO permissions. */ mach_vm_behavior_set(mach_task_self(), (mach_vm_address_t)state, PAGE_SIZE, VM_BEHAVIOR_PAGEOUT); /* * Can verify in debugger at this point that page(s) have been * paged out. If compressor pager is available the page should * not be resident and compressor pager should be tied to the * top level VM object. */ os_thread_self_restrict_tpro_to_ro(); T_EXPECT_EQ(does_write_fault(MAGIC(state), 0x49494949), 1, "write to pager backed memory after pageout (detect fault)"); os_thread_self_restrict_tpro_to_rw(); T_EXPECT_EQ(does_write_fault(MAGIC(state), 0x4a4a4a4a), 0, "write to pager backed memory after pageout (no fault)"); /* * Try and reprotect the region. We should fail */ kr = vm_protect(mach_task_self(), (mach_vm_address_t)state, PAGE_SIZE, FALSE, VM_PROT_DEFAULT); T_EXPECT_POSIX_ERROR(kr, KERN_PROTECTION_FAILURE, "vm_protect(RW) should fail"); os_thread_self_restrict_tpro_to_ro(); } static void mmap_test(const_page_t *state) { void *mapping; /* * Validate our initial status quo. TPRO permissions should be RO, * so we should be able to read from our pager backed mapping but * should fault when trying to write to it. */ T_EXPECT_EQ(os_thread_self_restrict_tpro_is_writable(), false, "TPRO region starts read-only"); /* * Attempt to mmap a fixed allocation over our TPRO region. * TPRO region should be permanent and should disallow being * overwritten. */ mapping = mmap(state, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0); T_ASSERT_EQ(mapping, MAP_FAILED, "Map over TPRO range should fail"); os_thread_self_restrict_tpro_to_ro(); } static void vm_allocate_test(const_page_t *state) { kern_return_t kr; mach_vm_address_t addr = (mach_vm_address_t)state; vm_region_basic_info_data_64_t info; mach_msg_type_number_t count = VM_REGION_BASIC_INFO_COUNT_64; mach_vm_size_t size = PAGE_SIZE; mach_port_t unused = MACH_PORT_NULL; /* * Validate our initial status quo. TPRO permissions should be RO, * so we should be able to read from our pager backed mapping but * should fault when trying to write to it. */ T_EXPECT_EQ(os_thread_self_restrict_tpro_is_writable(), false, "TPRO region starts read-only"); /* * Deallocate the TPRO region. This should succeed but leave the region * intact with no permissions. Further allocations should not be able to * obtain the same address. */ kr = mach_vm_deallocate(mach_task_self(), (mach_vm_address_t)state, PAGE_SIZE); T_EXPECT_POSIX_ERROR(kr, KERN_SUCCESS, "vm_deallocate should succeed"); kr = mach_vm_allocate(mach_task_self(), (mach_vm_address_t *)&addr, PAGE_SIZE, VM_FLAGS_FIXED); T_EXPECT_POSIX_ERROR(kr, KERN_NO_SPACE, "vm_allocate should fail with KERN_NO_SPACE"); /* * Lookup the target region and confirm that all permissions have been * removed. */ kr = mach_vm_region(mach_task_self(), &addr, &size, VM_REGION_BASIC_INFO_64, (vm_region_info_t)&info, &count, &unused); T_QUIET; T_EXPECT_POSIX_ERROR(kr, KERN_SUCCESS, "mach_vm_region should succeed"); T_ASSERT_EQ(info.protection, VM_PROT_NONE, "Entry should have no permissions"); T_ASSERT_EQ(info.max_protection, VM_PROT_NONE, "Entry should have no permissions"); os_thread_self_restrict_tpro_to_ro(); } T_DECL(thread_self_restrict_pagers, "Verify that the TPRO pager interfaces work correctly", T_META_TAG_VM_PREFERRED) { #if __arm64__ /* Check to see that we support the necessary hardware features. */ if (!os_thread_self_restrict_tpro_is_supported() || !has_pager_support()) { T_SKIP("no hardware TPRO support enabled on this system"); } thread_self_restrict_test(^{ pager_test(&pager_state.one); /* * Ensure that touching the second pager supported page exhibits * identical behaviour in order to validate the transitions between * VM entry & copy object chains. */ pager_test(&pager_state.two); /* * Try and write to a normal __DATA_CONST page that isn't backed by * the dyld pager. The kernel will have mapped this directly but * should still maintain TPRO protection. */ os_thread_self_restrict_tpro_to_ro(); T_EXPECT_EQ(does_write_fault(&pager_state.ro[0], 0x41414141), 1, "write to __DATA_CONST should succeed (no fault)"); os_thread_self_restrict_tpro_to_rw(); T_EXPECT_EQ(does_write_fault(&pager_state.ro[0], 0x41414141), 0, "write to __DATA_CONST should fail (detect fault)"); }); #else T_SKIP("thread_self_restrict_pagers not supported on this system"); #endif /* __arm64__ */ } T_DECL(thread_self_restrict_tpro_permanent, "Verify that TPRO VM entries are permanent") { #if __arm64__ /* Check to see that we support the necessary hardware features. */ if (!os_thread_self_restrict_tpro_is_supported() || !has_pager_support()) { T_SKIP("no hardware TPRO support enabled on this system"); } thread_self_restrict_test(^{ mmap_test(&pager_state.one); vm_allocate_test(&pager_state.two); }); #else T_SKIP("thread_self_restrict_tpro_permanent not supported on this system"); #endif /* __arm64__ */ } |