xref: /xnu-11417.140.69/tests/unrecoverable_trap_test.c (revision 43a90889846e00bfb5cf1d255cdc0a701a1e05a4)
1*43a90889SApple OSS Distributions /*
2*43a90889SApple OSS Distributions  * Copyright (c) 2025 Apple Computer, Inc. All rights reserved.
3*43a90889SApple OSS Distributions  *
4*43a90889SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*43a90889SApple OSS Distributions  *
6*43a90889SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*43a90889SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*43a90889SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*43a90889SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*43a90889SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*43a90889SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*43a90889SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*43a90889SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*43a90889SApple OSS Distributions  *
15*43a90889SApple OSS Distributions  * Please obtain a copy of the License at
16*43a90889SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*43a90889SApple OSS Distributions  *
18*43a90889SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*43a90889SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*43a90889SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*43a90889SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*43a90889SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*43a90889SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*43a90889SApple OSS Distributions  * limitations under the License.
25*43a90889SApple OSS Distributions  *
26*43a90889SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*43a90889SApple OSS Distributions  */
28*43a90889SApple OSS Distributions 
29*43a90889SApple OSS Distributions #include <darwintest.h>
30*43a90889SApple OSS Distributions #include <stdlib.h>
31*43a90889SApple OSS Distributions #include <unistd.h>
32*43a90889SApple OSS Distributions #include <mach/exception_types.h>
33*43a90889SApple OSS Distributions #include <sys/wait.h>
34*43a90889SApple OSS Distributions #include <sys/sysctl.h>
35*43a90889SApple OSS Distributions #include <sys/code_signing.h>
36*43a90889SApple OSS Distributions #include <signal.h>
37*43a90889SApple OSS Distributions #include <sys/ptrace.h>
38*43a90889SApple OSS Distributions #include <sys/mman.h>
39*43a90889SApple OSS Distributions #include <fcntl.h>
40*43a90889SApple OSS Distributions #include <os/crashlog_private.h>
41*43a90889SApple OSS Distributions 
42*43a90889SApple OSS Distributions #include "exc_helpers.h"
43*43a90889SApple OSS Distributions #include "test_utils.h"
44*43a90889SApple OSS Distributions 
45*43a90889SApple OSS Distributions T_GLOBAL_META(
46*43a90889SApple OSS Distributions 	T_META_NAMESPACE("xnu"),
47*43a90889SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
48*43a90889SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("arm"),
49*43a90889SApple OSS Distributions 	T_META_OWNER("p_tennen")
50*43a90889SApple OSS Distributions 	);
51*43a90889SApple OSS Distributions 
52*43a90889SApple OSS Distributions static size_t
exception_handler_expect_not_called(mach_port_t task __unused,mach_port_t thread __unused,exception_type_t type __unused,mach_exception_data_t codes __unused)53*43a90889SApple OSS Distributions exception_handler_expect_not_called(mach_port_t task __unused, mach_port_t thread __unused,
54*43a90889SApple OSS Distributions     exception_type_t type __unused, mach_exception_data_t codes __unused)
55*43a90889SApple OSS Distributions {
56*43a90889SApple OSS Distributions 	T_ASSERT_FAIL("kernel ran exception handler instead of terminating process");
57*43a90889SApple OSS Distributions 	return 0;
58*43a90889SApple OSS Distributions }
59*43a90889SApple OSS Distributions 
60*43a90889SApple OSS Distributions static void
signal_handler_expect_not_called(int sig,siginfo_t * sip __unused,void * ucontext __unused)61*43a90889SApple OSS Distributions signal_handler_expect_not_called(int sig, siginfo_t *sip __unused, void *ucontext __unused)
62*43a90889SApple OSS Distributions {
63*43a90889SApple OSS Distributions 	T_FAIL("kernel dispatched signal handler instead of terminating process");
64*43a90889SApple OSS Distributions }
65*43a90889SApple OSS Distributions 
66*43a90889SApple OSS Distributions T_DECL(uncatchable_fatal_trap_developer_mode_disabled,
67*43a90889SApple OSS Distributions     "Ensure a maybe-unrecoverable trap label is uncatchable with !developer_mode",
68*43a90889SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("security.mac.amfi.developer_mode_status", 0),
69*43a90889SApple OSS Distributions     T_META_ENABLED(TARGET_CPU_ARM64)
70*43a90889SApple OSS Distributions     )
71*43a90889SApple OSS Distributions {
72*43a90889SApple OSS Distributions 	/* Given a child process that sets up some mechanisms to catch an exception/signal */
73*43a90889SApple OSS Distributions 	/* And developer mode is disabled and we're not being debugged */
74*43a90889SApple OSS Distributions 	pid_t pid = fork();
75*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(pid, "fork");
76*43a90889SApple OSS Distributions 
77*43a90889SApple OSS Distributions 	if (pid == 0) {
78*43a90889SApple OSS Distributions 		/*
79*43a90889SApple OSS Distributions 		 * Try to catch the exception in two ways:
80*43a90889SApple OSS Distributions 		 * - via setting up a Mach exception handler, and
81*43a90889SApple OSS Distributions 		 * - via sigaction
82*43a90889SApple OSS Distributions 		 */
83*43a90889SApple OSS Distributions 		mach_port_t exc_port = create_exception_port(EXC_MASK_ALL);
84*43a90889SApple OSS Distributions 		run_exception_handler(exc_port, (exc_handler_callback_t)exception_handler_expect_not_called);
85*43a90889SApple OSS Distributions 
86*43a90889SApple OSS Distributions 		struct sigaction sa = {
87*43a90889SApple OSS Distributions 			.sa_sigaction = signal_handler_expect_not_called,
88*43a90889SApple OSS Distributions 			.sa_flags = SA_SIGINFO
89*43a90889SApple OSS Distributions 		};
90*43a90889SApple OSS Distributions 		sigfillset(&sa.sa_mask);
91*43a90889SApple OSS Distributions 
92*43a90889SApple OSS Distributions 		T_ASSERT_POSIX_ZERO(sigaction(SIGILL, &sa, NULL), NULL);
93*43a90889SApple OSS Distributions 
94*43a90889SApple OSS Distributions 		/* When the child issues a maybe-fatal trap label */
95*43a90889SApple OSS Distributions 		/* 0xB000 is the start of the 'runtimes-owned traps' range in xnu */
96*43a90889SApple OSS Distributions 		os_fatal_trap(0xB000);
97*43a90889SApple OSS Distributions 		/* The brk above should have been treated as unrecoverable by the kernel */
98*43a90889SApple OSS Distributions 		T_FAIL("child ran past unrecoverable brk");
99*43a90889SApple OSS Distributions 	} else {
100*43a90889SApple OSS Distributions 		int status;
101*43a90889SApple OSS Distributions 		int err = waitpid(pid, &status, 0);
102*43a90889SApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(err, "waitpid");
103*43a90889SApple OSS Distributions 
104*43a90889SApple OSS Distributions 		/* Then the child does not have an opportunity to run its exception handlers, and is immediately killed */
105*43a90889SApple OSS Distributions 		T_EXPECT_TRUE(WIFSIGNALED(status), "child terminated due to signal");
106*43a90889SApple OSS Distributions 		T_EXPECT_EQ(SIGKILL, WTERMSIG(status), "child terminated due to SIGKILL");
107*43a90889SApple OSS Distributions 	}
108*43a90889SApple OSS Distributions }
109*43a90889SApple OSS Distributions 
110*43a90889SApple OSS Distributions T_DECL(uncatchable_fatal_trap_developer_mode_enabled,
111*43a90889SApple OSS Distributions     "Ensure an maybe-unrecoverable trap label is uncatchable with developer_mode",
112*43a90889SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("security.mac.amfi.developer_mode_status", 1),
113*43a90889SApple OSS Distributions     T_META_ENABLED(TARGET_CPU_ARM64)
114*43a90889SApple OSS Distributions     )
115*43a90889SApple OSS Distributions {
116*43a90889SApple OSS Distributions 	/* Given a child process that sets up some mechanisms to catch an exception/signal */
117*43a90889SApple OSS Distributions 	/* And developer mode is enabled, but we're not being debugged */
118*43a90889SApple OSS Distributions 	pid_t pid = fork();
119*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(pid, "fork");
120*43a90889SApple OSS Distributions 
121*43a90889SApple OSS Distributions 	if (pid == 0) {
122*43a90889SApple OSS Distributions 		/*
123*43a90889SApple OSS Distributions 		 * Try to catch the exception in two ways:
124*43a90889SApple OSS Distributions 		 * - via setting up a Mach exception handler, and
125*43a90889SApple OSS Distributions 		 * - via sigaction
126*43a90889SApple OSS Distributions 		 */
127*43a90889SApple OSS Distributions 		mach_port_t exc_port = create_exception_port(EXC_MASK_ALL);
128*43a90889SApple OSS Distributions 		run_exception_handler(exc_port, (exc_handler_callback_t)exception_handler_expect_not_called);
129*43a90889SApple OSS Distributions 
130*43a90889SApple OSS Distributions 		struct sigaction sa = {
131*43a90889SApple OSS Distributions 			.sa_sigaction = signal_handler_expect_not_called,
132*43a90889SApple OSS Distributions 			.sa_flags = SA_SIGINFO
133*43a90889SApple OSS Distributions 		};
134*43a90889SApple OSS Distributions 		sigfillset(&sa.sa_mask);
135*43a90889SApple OSS Distributions 
136*43a90889SApple OSS Distributions 		T_ASSERT_POSIX_ZERO(sigaction(SIGILL, &sa, NULL), NULL);
137*43a90889SApple OSS Distributions 
138*43a90889SApple OSS Distributions 		/* When the child issues a maybe-fatal trap label */
139*43a90889SApple OSS Distributions 		/* 0xB000 is the start of the 'runtimes-owned traps' range in xnu */
140*43a90889SApple OSS Distributions 		os_fatal_trap(0xB000);
141*43a90889SApple OSS Distributions 		/* The brk above should have been treated as unrecoverable by the kernel */
142*43a90889SApple OSS Distributions 		T_FAIL("child ran past brk");
143*43a90889SApple OSS Distributions 	} else {
144*43a90889SApple OSS Distributions 		int status;
145*43a90889SApple OSS Distributions 		int err = waitpid(pid, &status, 0);
146*43a90889SApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(err, "waitpid");
147*43a90889SApple OSS Distributions 
148*43a90889SApple OSS Distributions 		/* Then the child does not have an opportunity to run its exception handlers, and is immediately killed */
149*43a90889SApple OSS Distributions 		T_EXPECT_TRUE(WIFSIGNALED(status), "child terminated due to signal");
150*43a90889SApple OSS Distributions 		T_EXPECT_EQ(SIGKILL, WTERMSIG(status), "child terminated due to SIGKILL");
151*43a90889SApple OSS Distributions 	}
152*43a90889SApple OSS Distributions }
153*43a90889SApple OSS Distributions 
154*43a90889SApple OSS Distributions static bool* shared_was_mach_exception_handler_called = NULL;
155*43a90889SApple OSS Distributions static bool* shared_was_posix_signal_handler_called = NULL;
156*43a90889SApple OSS Distributions 
157*43a90889SApple OSS Distributions static size_t
exception_handler_expect_called(mach_port_t task __unused,mach_port_t thread __unused,exception_type_t type __unused,mach_exception_data_t codes __unused)158*43a90889SApple OSS Distributions exception_handler_expect_called(mach_port_t task __unused, mach_port_t thread __unused,
159*43a90889SApple OSS Distributions     exception_type_t type __unused, mach_exception_data_t codes __unused)
160*43a90889SApple OSS Distributions {
161*43a90889SApple OSS Distributions 	T_PASS("Our Mach exception handler ran");
162*43a90889SApple OSS Distributions 	*shared_was_mach_exception_handler_called = true;
163*43a90889SApple OSS Distributions 	exit(0);
164*43a90889SApple OSS Distributions 	return 0;
165*43a90889SApple OSS Distributions }
166*43a90889SApple OSS Distributions 
167*43a90889SApple OSS Distributions static void
signal_handler_expect_called(int sig,siginfo_t * sip __unused,void * ucontext __unused)168*43a90889SApple OSS Distributions signal_handler_expect_called(int sig, siginfo_t *sip __unused, void *ucontext __unused)
169*43a90889SApple OSS Distributions {
170*43a90889SApple OSS Distributions 	T_PASS("Our BSD signal handler ran");
171*43a90889SApple OSS Distributions 	*shared_was_posix_signal_handler_called = true;
172*43a90889SApple OSS Distributions 	exit(0);
173*43a90889SApple OSS Distributions }
174*43a90889SApple OSS Distributions 
175*43a90889SApple OSS Distributions 
176*43a90889SApple OSS Distributions T_DECL(uncatchable_fatal_trap_debugged,
177*43a90889SApple OSS Distributions     "Ensure an maybe-unrecoverable trap label is catchable under a debugger",
178*43a90889SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("security.mac.amfi.developer_mode_status", 1),
179*43a90889SApple OSS Distributions     /* It's not straightforward to ptrace on platforms other than macOS, so don't bother */
180*43a90889SApple OSS Distributions     T_META_ENABLED(TARGET_CPU_ARM64 && TARGET_OS_OSX)
181*43a90889SApple OSS Distributions     )
182*43a90889SApple OSS Distributions {
183*43a90889SApple OSS Distributions 	/* Given a child process that sets up some mechanisms to catch an exception/signal */
184*43a90889SApple OSS Distributions 	/* And developer mode is enabled, and the child is being debugged */
185*43a90889SApple OSS Distributions 	int ret;
186*43a90889SApple OSS Distributions 
187*43a90889SApple OSS Distributions 	const char* memory_path = "uncatchable_fatal_trap_debugged";
188*43a90889SApple OSS Distributions 	shm_unlink(memory_path);
189*43a90889SApple OSS Distributions 	int shm_fd = shm_open(memory_path, O_RDWR | O_CREAT);
190*43a90889SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(shm_fd, "Created shared memory");
191*43a90889SApple OSS Distributions 	ret = ftruncate(shm_fd, sizeof(bool) * 2);
192*43a90889SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "ftruncate");
193*43a90889SApple OSS Distributions 
194*43a90889SApple OSS Distributions 	shared_was_mach_exception_handler_called = (bool*)mmap(NULL, sizeof(bool), PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
195*43a90889SApple OSS Distributions 	shared_was_posix_signal_handler_called = (bool*)mmap(NULL, sizeof(bool), PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
196*43a90889SApple OSS Distributions 	bool* has_parent_connected = (bool*)mmap(NULL, sizeof(bool), PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
197*43a90889SApple OSS Distributions 	*has_parent_connected = false;
198*43a90889SApple OSS Distributions 
199*43a90889SApple OSS Distributions 	pid_t pid = fork();
200*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(pid, "fork");
201*43a90889SApple OSS Distributions 
202*43a90889SApple OSS Distributions 	if (pid == 0) {
203*43a90889SApple OSS Distributions 		/* Allow the parent to attach */
204*43a90889SApple OSS Distributions 		while (!*has_parent_connected) {
205*43a90889SApple OSS Distributions 			sleep(1);
206*43a90889SApple OSS Distributions 		}
207*43a90889SApple OSS Distributions 
208*43a90889SApple OSS Distributions 		/*
209*43a90889SApple OSS Distributions 		 * Try to catch the exception in two ways:
210*43a90889SApple OSS Distributions 		 * - via setting up a Mach exception handler, and
211*43a90889SApple OSS Distributions 		 * - via sigaction
212*43a90889SApple OSS Distributions 		 */
213*43a90889SApple OSS Distributions 		mach_port_t exc_port = create_exception_port(EXC_MASK_ALL);
214*43a90889SApple OSS Distributions 		run_exception_handler(exc_port, (exc_handler_callback_t)exception_handler_expect_called);
215*43a90889SApple OSS Distributions 
216*43a90889SApple OSS Distributions 		struct sigaction sa = {
217*43a90889SApple OSS Distributions 			.sa_sigaction = signal_handler_expect_called,
218*43a90889SApple OSS Distributions 			.sa_flags = SA_SIGINFO
219*43a90889SApple OSS Distributions 		};
220*43a90889SApple OSS Distributions 		sigfillset(&sa.sa_mask);
221*43a90889SApple OSS Distributions 
222*43a90889SApple OSS Distributions 		T_ASSERT_POSIX_ZERO(sigaction(SIGILL, &sa, NULL), NULL);
223*43a90889SApple OSS Distributions 
224*43a90889SApple OSS Distributions 		/* When the child issues a maybe-fatal trap label */
225*43a90889SApple OSS Distributions 		/* 0xB000 is the start of the 'runtimes-owned traps' range in xnu */
226*43a90889SApple OSS Distributions 		os_fatal_trap(0xB000);
227*43a90889SApple OSS Distributions 		/* The brk above should have terminated this thread */
228*43a90889SApple OSS Distributions 		T_FAIL("child ran past brk");
229*43a90889SApple OSS Distributions 	} else {
230*43a90889SApple OSS Distributions 		/* Attach to the child so it's marked as being debugged */
231*43a90889SApple OSS Distributions 		ret = ptrace(PT_ATTACHEXC, pid, 0, 0);
232*43a90889SApple OSS Distributions 		T_EXPECT_POSIX_SUCCESS(ret, "ptrace PT_ATTACHEXC");
233*43a90889SApple OSS Distributions 		ret = ptrace(PT_CONTINUE, pid, (caddr_t)1, 0);
234*43a90889SApple OSS Distributions 		T_EXPECT_POSIX_SUCCESS(ret, "ptrace PT_CONTINUE");
235*43a90889SApple OSS Distributions 		/* And let the child know that it can carry on */
236*43a90889SApple OSS Distributions 		*has_parent_connected = true;
237*43a90889SApple OSS Distributions 
238*43a90889SApple OSS Distributions 		int status;
239*43a90889SApple OSS Distributions 		int err = waitpid(pid, &status, 0);
240*43a90889SApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(err, "waitpid");
241*43a90889SApple OSS Distributions 
242*43a90889SApple OSS Distributions 		/*
243*43a90889SApple OSS Distributions 		 * Then the child is given an opportunity to run its exception handlers,
244*43a90889SApple OSS Distributions 		 * which we witness by its setting of a shared boolean and clean exit(0).
245*43a90889SApple OSS Distributions 		 */
246*43a90889SApple OSS Distributions 		T_EXPECT_TRUE(WIFEXITED(status), "child exited");
247*43a90889SApple OSS Distributions 		T_EXPECT_TRUE(*shared_was_mach_exception_handler_called
248*43a90889SApple OSS Distributions 		    || *shared_was_posix_signal_handler_called,
249*43a90889SApple OSS Distributions 		    "Expected one of our handlers to be dispatched");
250*43a90889SApple OSS Distributions 
251*43a90889SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(close(shm_fd), "Closed shm fd");
252*43a90889SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(shm_unlink(memory_path), "Unlinked");
253*43a90889SApple OSS Distributions 	}
254*43a90889SApple OSS Distributions }
255