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