1 /* 2 * Copyright (c) 2024 Apple Inc. All rights reserved. 3 * 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5 * 6 * This file contains Original Code and/or Modifications of Original Code 7 * as defined in and that are subject to the Apple Public Source License 8 * Version 2.0 (the 'License'). You may not use this file except in 9 * compliance with the License. The rights granted to you under the License 10 * may not be used to create, or enable the creation or redistribution of, 11 * unlawful or unlicensed copies of an Apple operating system, or to 12 * circumvent, violate, or enable the circumvention or violation of, any 13 * terms of an Apple operating system software license agreement. 14 * 15 * Please obtain a copy of the License at 16 * http://www.opensource.apple.com/apsl/ and read it before using this file. 17 * 18 * The Original Code and all software distributed under the License are 19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23 * Please see the License for the specific language governing rights and 24 * limitations under the License. 25 * 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27 */ 28 29 /* 30 * exc_guard_helper_test_unexpected.c 31 * 32 * Test the testing helper functions in exc_guard_helper.h. 33 * The exception handler used by block_raise_exc_guard_of_type() 34 * should allow other exceptions to continue to a crash. 35 */ 36 37 #include "test_utils.h" 38 #include "exc_guard_helper.h" 39 40 #include <darwintest.h> 41 #include <sys/wait.h> 42 #include <sys/types.h> 43 #include <sys/sysctl.h> 44 #include <mach/mach.h> 45 #include <mach/mach_vm.h> 46 #include <mach/task_info.h> 47 48 T_GLOBAL_META( 49 T_META_NAMESPACE("xnu"), 50 T_META_RADAR_COMPONENT_NAME("xnu"), 51 T_META_RADAR_COMPONENT_VERSION("vm"), 52 T_META_RUN_CONCURRENTLY(true), 53 T_META_ALL_VALID_ARCHS(true), 54 55 T_META_IGNORECRASHES(".*exc_guard_helper_test_unexpected.*") 56 ); 57 58 T_DECL(exc_guard_helper_test_unexpected_exc_guard, 59 "provoke one guard exception type while exc_guard_helper is expecting another") 60 { 61 if (process_is_translated()) { 62 T_SKIP("VM guard exceptions not supported on Rosetta (rdar://142438840)"); 63 } 64 65 pid_t child_pid; 66 67 if ((child_pid = fork())) { 68 /* parent */ 69 T_QUIET; T_ASSERT_POSIX_SUCCESS(child_pid, "fork"); 70 71 int status; 72 pid_t waited_pid; 73 74 waited_pid = waitpid(child_pid, &status, 0); 75 T_QUIET; T_ASSERT_POSIX_SUCCESS(waited_pid, "waitpid"); 76 T_QUIET; T_ASSERT_EQ(waited_pid, child_pid, "waitpid"); 77 78 T_ASSERT_TRUE(WIFSIGNALED(status), "child should have crashed"); 79 T_ASSERT_EQ(WTERMSIG(status), SIGKILL, "child should have crashed with SIGKILL"); 80 } else { 81 /* child */ 82 kern_return_t kr; 83 task_exc_guard_behavior_t behavior; 84 exc_guard_helper_info_t exc_info; 85 mach_port_t port; 86 87 exc_guard_helper_init(); 88 89 /* 90 * set GUARD_TYPE_MACH_PORT to be enabled and fatal. 91 * This child process is expected to crash. 92 */ 93 kr = task_get_exc_guard_behavior(mach_task_self(), &behavior); 94 T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "get old behavior"); 95 behavior &= ~TASK_EXC_GUARD_MP_ALL; 96 behavior |= TASK_EXC_GUARD_MP_DELIVER | TASK_EXC_GUARD_MP_FATAL; 97 kr = task_set_exc_guard_behavior(mach_task_self(), behavior); 98 T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "set fatal mach port behavior"); 99 100 kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &port); 101 T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "new port"); 102 kr = mach_port_insert_right(mach_task_self(), port, port, MACH_MSG_TYPE_MAKE_SEND); 103 T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "make send"); 104 105 /* provoke GUARD_TYPE_MACH_PORT while listening for GUARD_TYPE_VIRT_MEMORY */ 106 107 if (block_raised_exc_guard_of_type(GUARD_TYPE_VIRT_MEMORY, &exc_info, ^{ 108 kern_return_t kr; 109 T_LOG("CHILD EXPECTED TO CRASH after this guard exception"); 110 kr = mach_port_mod_refs(mach_task_self(), port, MACH_PORT_RIGHT_SEND, INT32_MAX); 111 T_QUIET; T_ASSERT_MACH_ERROR(kr, KERN_INVALID_VALUE, "add too many send rights"); 112 })) { 113 T_FAIL("Mach port guard exception unexpectedly caught by VM guard exception handler"); 114 } 115 116 T_FAIL("expected Mach port guard exception to kill the process"); 117 } 118 } 119