xref: /xnu-12377.81.4/tests/unrecoverable_trap_test.c (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1*043036a2SApple OSS Distributions /*
2*043036a2SApple OSS Distributions  * Copyright (c) 2025 Apple Computer, 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 #include <darwintest.h>
30*043036a2SApple OSS Distributions #include <stdlib.h>
31*043036a2SApple OSS Distributions #include <unistd.h>
32*043036a2SApple OSS Distributions #include <mach/exception_types.h>
33*043036a2SApple OSS Distributions #include <sys/wait.h>
34*043036a2SApple OSS Distributions #include <sys/sysctl.h>
35*043036a2SApple OSS Distributions #include <sys/code_signing.h>
36*043036a2SApple OSS Distributions #include <signal.h>
37*043036a2SApple OSS Distributions #include <sys/ptrace.h>
38*043036a2SApple OSS Distributions #include <sys/mman.h>
39*043036a2SApple OSS Distributions #include <fcntl.h>
40*043036a2SApple OSS Distributions #include <os/crashlog_private.h>
41*043036a2SApple OSS Distributions 
42*043036a2SApple OSS Distributions #include "exc_helpers.h"
43*043036a2SApple OSS Distributions #include "test_utils.h"
44*043036a2SApple OSS Distributions 
45*043036a2SApple OSS Distributions T_GLOBAL_META(
46*043036a2SApple OSS Distributions 	T_META_NAMESPACE("xnu"),
47*043036a2SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
48*043036a2SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("arm"),
49*043036a2SApple OSS Distributions 	T_META_OWNER("p_tennen")
50*043036a2SApple OSS Distributions 	);
51*043036a2SApple OSS Distributions 
52*043036a2SApple 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*043036a2SApple OSS Distributions exception_handler_expect_not_called(mach_port_t task __unused, mach_port_t thread __unused,
54*043036a2SApple OSS Distributions     exception_type_t type __unused, mach_exception_data_t codes __unused)
55*043036a2SApple OSS Distributions {
56*043036a2SApple OSS Distributions 	T_ASSERT_FAIL("kernel ran exception handler instead of terminating process");
57*043036a2SApple OSS Distributions 	return 0;
58*043036a2SApple OSS Distributions }
59*043036a2SApple OSS Distributions 
60*043036a2SApple OSS Distributions static void
signal_handler_expect_not_called(int sig,siginfo_t * sip __unused,void * ucontext __unused)61*043036a2SApple OSS Distributions signal_handler_expect_not_called(int sig, siginfo_t *sip __unused, void *ucontext __unused)
62*043036a2SApple OSS Distributions {
63*043036a2SApple OSS Distributions 	T_FAIL("kernel dispatched signal handler instead of terminating process");
64*043036a2SApple OSS Distributions }
65*043036a2SApple OSS Distributions 
66*043036a2SApple OSS Distributions T_DECL(uncatchable_fatal_trap_developer_mode_disabled,
67*043036a2SApple OSS Distributions     "Ensure a maybe-unrecoverable trap label is uncatchable with !developer_mode",
68*043036a2SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("security.mac.amfi.developer_mode_status", 0),
69*043036a2SApple OSS Distributions     T_META_ENABLED(TARGET_CPU_ARM64)
70*043036a2SApple OSS Distributions     )
71*043036a2SApple OSS Distributions {
72*043036a2SApple OSS Distributions 	/* Given a child process that sets up some mechanisms to catch an exception/signal */
73*043036a2SApple OSS Distributions 	/* And developer mode is disabled and we're not being debugged */
74*043036a2SApple OSS Distributions 	pid_t pid = fork();
75*043036a2SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(pid, "fork");
76*043036a2SApple OSS Distributions 
77*043036a2SApple OSS Distributions 	if (pid == 0) {
78*043036a2SApple OSS Distributions 		/*
79*043036a2SApple OSS Distributions 		 * Try to catch the exception in two ways:
80*043036a2SApple OSS Distributions 		 * - via setting up a Mach exception handler, and
81*043036a2SApple OSS Distributions 		 * - via sigaction
82*043036a2SApple OSS Distributions 		 */
83*043036a2SApple OSS Distributions 		mach_port_t exc_port = create_exception_port(EXC_MASK_ALL);
84*043036a2SApple OSS Distributions 		run_exception_handler(exc_port, (exc_handler_callback_t)exception_handler_expect_not_called);
85*043036a2SApple OSS Distributions 
86*043036a2SApple OSS Distributions 		struct sigaction sa = {
87*043036a2SApple OSS Distributions 			.sa_sigaction = signal_handler_expect_not_called,
88*043036a2SApple OSS Distributions 			.sa_flags = SA_SIGINFO
89*043036a2SApple OSS Distributions 		};
90*043036a2SApple OSS Distributions 		sigfillset(&sa.sa_mask);
91*043036a2SApple OSS Distributions 
92*043036a2SApple OSS Distributions 		T_ASSERT_POSIX_ZERO(sigaction(SIGILL, &sa, NULL), NULL);
93*043036a2SApple OSS Distributions 
94*043036a2SApple OSS Distributions 		/* When the child issues a maybe-fatal trap label */
95*043036a2SApple OSS Distributions 		/* 0xB000 is the start of the 'runtimes-owned traps' range in xnu */
96*043036a2SApple OSS Distributions 		os_fatal_trap(0xB000);
97*043036a2SApple OSS Distributions 		/* The brk above should have been treated as unrecoverable by the kernel */
98*043036a2SApple OSS Distributions 		T_FAIL("child ran past unrecoverable brk");
99*043036a2SApple OSS Distributions 	} else {
100*043036a2SApple OSS Distributions 		int status;
101*043036a2SApple OSS Distributions 		int err = waitpid(pid, &status, 0);
102*043036a2SApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(err, "waitpid");
103*043036a2SApple OSS Distributions 
104*043036a2SApple OSS Distributions 		/* Then the child does not have an opportunity to run its exception handlers, and is immediately killed */
105*043036a2SApple OSS Distributions 		T_EXPECT_TRUE(WIFSIGNALED(status), "child terminated due to signal");
106*043036a2SApple OSS Distributions 		T_EXPECT_EQ(SIGKILL, WTERMSIG(status), "child terminated due to SIGKILL");
107*043036a2SApple OSS Distributions 	}
108*043036a2SApple OSS Distributions }
109*043036a2SApple OSS Distributions 
110*043036a2SApple OSS Distributions T_DECL(uncatchable_fatal_trap_developer_mode_enabled,
111*043036a2SApple OSS Distributions     "Ensure an maybe-unrecoverable trap label is uncatchable with developer_mode",
112*043036a2SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("security.mac.amfi.developer_mode_status", 1),
113*043036a2SApple OSS Distributions     T_META_ENABLED(TARGET_CPU_ARM64)
114*043036a2SApple OSS Distributions     )
115*043036a2SApple OSS Distributions {
116*043036a2SApple OSS Distributions 	/* Given a child process that sets up some mechanisms to catch an exception/signal */
117*043036a2SApple OSS Distributions 	/* And developer mode is enabled, but we're not being debugged */
118*043036a2SApple OSS Distributions 	pid_t pid = fork();
119*043036a2SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(pid, "fork");
120*043036a2SApple OSS Distributions 
121*043036a2SApple OSS Distributions 	if (pid == 0) {
122*043036a2SApple OSS Distributions 		/*
123*043036a2SApple OSS Distributions 		 * Try to catch the exception in two ways:
124*043036a2SApple OSS Distributions 		 * - via setting up a Mach exception handler, and
125*043036a2SApple OSS Distributions 		 * - via sigaction
126*043036a2SApple OSS Distributions 		 */
127*043036a2SApple OSS Distributions 		mach_port_t exc_port = create_exception_port(EXC_MASK_ALL);
128*043036a2SApple OSS Distributions 		run_exception_handler(exc_port, (exc_handler_callback_t)exception_handler_expect_not_called);
129*043036a2SApple OSS Distributions 
130*043036a2SApple OSS Distributions 		struct sigaction sa = {
131*043036a2SApple OSS Distributions 			.sa_sigaction = signal_handler_expect_not_called,
132*043036a2SApple OSS Distributions 			.sa_flags = SA_SIGINFO
133*043036a2SApple OSS Distributions 		};
134*043036a2SApple OSS Distributions 		sigfillset(&sa.sa_mask);
135*043036a2SApple OSS Distributions 
136*043036a2SApple OSS Distributions 		T_ASSERT_POSIX_ZERO(sigaction(SIGILL, &sa, NULL), NULL);
137*043036a2SApple OSS Distributions 
138*043036a2SApple OSS Distributions 		/* When the child issues a maybe-fatal trap label */
139*043036a2SApple OSS Distributions 		/* 0xB000 is the start of the 'runtimes-owned traps' range in xnu */
140*043036a2SApple OSS Distributions 		os_fatal_trap(0xB000);
141*043036a2SApple OSS Distributions 		/* The brk above should have been treated as unrecoverable by the kernel */
142*043036a2SApple OSS Distributions 		T_FAIL("child ran past brk");
143*043036a2SApple OSS Distributions 	} else {
144*043036a2SApple OSS Distributions 		int status;
145*043036a2SApple OSS Distributions 		int err = waitpid(pid, &status, 0);
146*043036a2SApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(err, "waitpid");
147*043036a2SApple OSS Distributions 
148*043036a2SApple OSS Distributions 		/* Then the child does not have an opportunity to run its exception handlers, and is immediately killed */
149*043036a2SApple OSS Distributions 		T_EXPECT_TRUE(WIFSIGNALED(status), "child terminated due to signal");
150*043036a2SApple OSS Distributions 		T_EXPECT_EQ(SIGKILL, WTERMSIG(status), "child terminated due to SIGKILL");
151*043036a2SApple OSS Distributions 	}
152*043036a2SApple OSS Distributions }
153*043036a2SApple OSS Distributions 
154*043036a2SApple OSS Distributions static bool* shared_was_mach_exception_handler_called = NULL;
155*043036a2SApple OSS Distributions static bool* shared_was_posix_signal_handler_called = NULL;
156*043036a2SApple OSS Distributions 
157*043036a2SApple 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*043036a2SApple OSS Distributions exception_handler_expect_called(mach_port_t task __unused, mach_port_t thread __unused,
159*043036a2SApple OSS Distributions     exception_type_t type __unused, mach_exception_data_t codes __unused)
160*043036a2SApple OSS Distributions {
161*043036a2SApple OSS Distributions 	T_PASS("Our Mach exception handler ran");
162*043036a2SApple OSS Distributions 	*shared_was_mach_exception_handler_called = true;
163*043036a2SApple OSS Distributions 	exit(0);
164*043036a2SApple OSS Distributions 	return 0;
165*043036a2SApple OSS Distributions }
166*043036a2SApple OSS Distributions 
167*043036a2SApple OSS Distributions static void
signal_handler_expect_called(int sig,siginfo_t * sip __unused,void * ucontext __unused)168*043036a2SApple OSS Distributions signal_handler_expect_called(int sig, siginfo_t *sip __unused, void *ucontext __unused)
169*043036a2SApple OSS Distributions {
170*043036a2SApple OSS Distributions 	T_PASS("Our BSD signal handler ran");
171*043036a2SApple OSS Distributions 	*shared_was_posix_signal_handler_called = true;
172*043036a2SApple OSS Distributions 	exit(0);
173*043036a2SApple OSS Distributions }
174*043036a2SApple OSS Distributions 
175*043036a2SApple OSS Distributions 
176*043036a2SApple OSS Distributions T_DECL(uncatchable_fatal_trap_debugged,
177*043036a2SApple OSS Distributions     "Ensure an maybe-unrecoverable trap label is catchable under a debugger",
178*043036a2SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("security.mac.amfi.developer_mode_status", 1),
179*043036a2SApple OSS Distributions     /* It's not straightforward to ptrace on platforms other than macOS, so don't bother */
180*043036a2SApple OSS Distributions     // T_META_ENABLED(TARGET_CPU_ARM64 && TARGET_OS_OSX)
181*043036a2SApple OSS Distributions     T_META_ENABLED(false) /* rdar://153223014 */
182*043036a2SApple OSS Distributions     )
183*043036a2SApple OSS Distributions {
184*043036a2SApple OSS Distributions 	/* Given a child process that sets up some mechanisms to catch an exception/signal */
185*043036a2SApple OSS Distributions 	/* And developer mode is enabled, and the child is being debugged */
186*043036a2SApple OSS Distributions 	int ret;
187*043036a2SApple OSS Distributions 
188*043036a2SApple OSS Distributions 	const char* memory_path = "uncatchable_fatal_trap_debugged";
189*043036a2SApple OSS Distributions 	shm_unlink(memory_path);
190*043036a2SApple OSS Distributions 	int shm_fd = shm_open(memory_path, O_RDWR | O_CREAT);
191*043036a2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(shm_fd, "Created shared memory");
192*043036a2SApple OSS Distributions 	ret = ftruncate(shm_fd, sizeof(bool) * 2);
193*043036a2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "ftruncate");
194*043036a2SApple OSS Distributions 
195*043036a2SApple OSS Distributions 	shared_was_mach_exception_handler_called = (bool*)mmap(NULL, sizeof(bool), PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
196*043036a2SApple OSS Distributions 	shared_was_posix_signal_handler_called = (bool*)mmap(NULL, sizeof(bool), PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
197*043036a2SApple OSS Distributions 	bool* has_parent_connected = (bool*)mmap(NULL, sizeof(bool), PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
198*043036a2SApple OSS Distributions 	*has_parent_connected = false;
199*043036a2SApple OSS Distributions 
200*043036a2SApple OSS Distributions 	pid_t pid = fork();
201*043036a2SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(pid, "fork");
202*043036a2SApple OSS Distributions 
203*043036a2SApple OSS Distributions 	if (pid == 0) {
204*043036a2SApple OSS Distributions 		/* Allow the parent to attach */
205*043036a2SApple OSS Distributions 		while (!*has_parent_connected) {
206*043036a2SApple OSS Distributions 			sleep(1);
207*043036a2SApple OSS Distributions 		}
208*043036a2SApple OSS Distributions 
209*043036a2SApple OSS Distributions 		/*
210*043036a2SApple OSS Distributions 		 * Try to catch the exception in two ways:
211*043036a2SApple OSS Distributions 		 * - via setting up a Mach exception handler, and
212*043036a2SApple OSS Distributions 		 * - via sigaction
213*043036a2SApple OSS Distributions 		 */
214*043036a2SApple OSS Distributions 		mach_port_t exc_port = create_exception_port(EXC_MASK_ALL);
215*043036a2SApple OSS Distributions 		run_exception_handler(exc_port, (exc_handler_callback_t)exception_handler_expect_called);
216*043036a2SApple OSS Distributions 
217*043036a2SApple OSS Distributions 		struct sigaction sa = {
218*043036a2SApple OSS Distributions 			.sa_sigaction = signal_handler_expect_called,
219*043036a2SApple OSS Distributions 			.sa_flags = SA_SIGINFO
220*043036a2SApple OSS Distributions 		};
221*043036a2SApple OSS Distributions 		sigfillset(&sa.sa_mask);
222*043036a2SApple OSS Distributions 
223*043036a2SApple OSS Distributions 		T_ASSERT_POSIX_ZERO(sigaction(SIGILL, &sa, NULL), NULL);
224*043036a2SApple OSS Distributions 
225*043036a2SApple OSS Distributions 		/* When the child issues a maybe-fatal trap label */
226*043036a2SApple OSS Distributions 		/* 0xB000 is the start of the 'runtimes-owned traps' range in xnu */
227*043036a2SApple OSS Distributions 		os_fatal_trap(0xB000);
228*043036a2SApple OSS Distributions 		/* The brk above should have terminated this thread */
229*043036a2SApple OSS Distributions 		T_FAIL("child ran past brk");
230*043036a2SApple OSS Distributions 	} else {
231*043036a2SApple OSS Distributions 		/* Attach to the child so it's marked as being debugged */
232*043036a2SApple OSS Distributions 		ret = ptrace(PT_ATTACHEXC, pid, 0, 0);
233*043036a2SApple OSS Distributions 		T_EXPECT_POSIX_SUCCESS(ret, "ptrace PT_ATTACHEXC");
234*043036a2SApple OSS Distributions 		ret = ptrace(PT_CONTINUE, pid, (caddr_t)1, 0);
235*043036a2SApple OSS Distributions 		T_EXPECT_POSIX_SUCCESS(ret, "ptrace PT_CONTINUE");
236*043036a2SApple OSS Distributions 		/* And let the child know that it can carry on */
237*043036a2SApple OSS Distributions 		*has_parent_connected = true;
238*043036a2SApple OSS Distributions 
239*043036a2SApple OSS Distributions 		int status;
240*043036a2SApple OSS Distributions 		int err = waitpid(pid, &status, 0);
241*043036a2SApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(err, "waitpid");
242*043036a2SApple OSS Distributions 
243*043036a2SApple OSS Distributions 		/*
244*043036a2SApple OSS Distributions 		 * Then the child is given an opportunity to run its exception handlers,
245*043036a2SApple OSS Distributions 		 * which we witness by its setting of a shared boolean and clean exit(0).
246*043036a2SApple OSS Distributions 		 */
247*043036a2SApple OSS Distributions 		T_EXPECT_TRUE(WIFEXITED(status), "child exited");
248*043036a2SApple OSS Distributions 		T_EXPECT_TRUE(*shared_was_mach_exception_handler_called
249*043036a2SApple OSS Distributions 		    || *shared_was_posix_signal_handler_called,
250*043036a2SApple OSS Distributions 		    "Expected one of our handlers to be dispatched");
251*043036a2SApple OSS Distributions 
252*043036a2SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(close(shm_fd), "Closed shm fd");
253*043036a2SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(shm_unlink(memory_path), "Unlinked");
254*043036a2SApple OSS Distributions 	}
255*043036a2SApple OSS Distributions }
256