1*c54f35caSApple OSS Distributions /*
2*c54f35caSApple OSS Distributions * Copyright (c) 2021 Apple Computer, Inc. All rights reserved.
3*c54f35caSApple OSS Distributions *
4*c54f35caSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*c54f35caSApple OSS Distributions *
6*c54f35caSApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*c54f35caSApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*c54f35caSApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*c54f35caSApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*c54f35caSApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*c54f35caSApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*c54f35caSApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*c54f35caSApple OSS Distributions * terms of an Apple operating system software license agreement.
14*c54f35caSApple OSS Distributions *
15*c54f35caSApple OSS Distributions * Please obtain a copy of the License at
16*c54f35caSApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*c54f35caSApple OSS Distributions *
18*c54f35caSApple OSS Distributions * The Original Code and all software distributed under the License are
19*c54f35caSApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*c54f35caSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*c54f35caSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*c54f35caSApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*c54f35caSApple OSS Distributions * Please see the License for the specific language governing rights and
24*c54f35caSApple OSS Distributions * limitations under the License.
25*c54f35caSApple OSS Distributions *
26*c54f35caSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*c54f35caSApple OSS Distributions */
28*c54f35caSApple OSS Distributions
29*c54f35caSApple OSS Distributions #include <darwintest.h>
30*c54f35caSApple OSS Distributions #include <ptrauth.h>
31*c54f35caSApple OSS Distributions #include <stdbool.h>
32*c54f35caSApple OSS Distributions #include <stdlib.h>
33*c54f35caSApple OSS Distributions #include <unistd.h>
34*c54f35caSApple OSS Distributions #include <mach/mach.h>
35*c54f35caSApple OSS Distributions #include <mach/exception.h>
36*c54f35caSApple OSS Distributions #include <mach/thread_status.h>
37*c54f35caSApple OSS Distributions #include <sys/types.h>
38*c54f35caSApple OSS Distributions #include <TargetConditionals.h>
39*c54f35caSApple OSS Distributions #include <mach/semaphore.h>
40*c54f35caSApple OSS Distributions
41*c54f35caSApple OSS Distributions #if __arm64__
42*c54f35caSApple OSS Distributions #define EXCEPTION_THREAD_STATE ARM_THREAD_STATE64
43*c54f35caSApple OSS Distributions #define EXCEPTION_THREAD_STATE_COUNT ARM_THREAD_STATE64_COUNT
44*c54f35caSApple OSS Distributions #elif __arm__
45*c54f35caSApple OSS Distributions #define EXCEPTION_THREAD_STATE ARM_THREAD_STATE
46*c54f35caSApple OSS Distributions #define EXCEPTION_THREAD_STATE_COUNT ARM_THREAD_STATE_COUNT
47*c54f35caSApple OSS Distributions #elif __x86_64__
48*c54f35caSApple OSS Distributions #define EXCEPTION_THREAD_STATE x86_THREAD_STATE
49*c54f35caSApple OSS Distributions #define EXCEPTION_THREAD_STATE_COUNT x86_THREAD_STATE_COUNT
50*c54f35caSApple OSS Distributions #else
51*c54f35caSApple OSS Distributions #error Unsupported architecture
52*c54f35caSApple OSS Distributions #endif
53*c54f35caSApple OSS Distributions
54*c54f35caSApple OSS Distributions T_GLOBAL_META(
55*c54f35caSApple OSS Distributions T_META_NAMESPACE("xnu.ipc"),
56*c54f35caSApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"),
57*c54f35caSApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("IPC"),
58*c54f35caSApple OSS Distributions T_META_RUN_CONCURRENTLY(true));
59*c54f35caSApple OSS Distributions
60*c54f35caSApple OSS Distributions /**
61*c54f35caSApple OSS Distributions * mach_exc_server() is a MIG-generated function that verifies the message
62*c54f35caSApple OSS Distributions * that was received is indeed a mach exception and then calls
63*c54f35caSApple OSS Distributions * catch_mach_exception_raise_state() to handle the exception.
64*c54f35caSApple OSS Distributions */
65*c54f35caSApple OSS Distributions extern boolean_t mach_exc_server(mach_msg_header_t *, mach_msg_header_t *);
66*c54f35caSApple OSS Distributions
67*c54f35caSApple OSS Distributions extern kern_return_t
68*c54f35caSApple OSS Distributions catch_mach_exception_raise(
69*c54f35caSApple OSS Distributions mach_port_t exception_port,
70*c54f35caSApple OSS Distributions mach_port_t thread,
71*c54f35caSApple OSS Distributions mach_port_t task,
72*c54f35caSApple OSS Distributions exception_type_t type,
73*c54f35caSApple OSS Distributions exception_data_t codes,
74*c54f35caSApple OSS Distributions mach_msg_type_number_t code_count);
75*c54f35caSApple OSS Distributions
76*c54f35caSApple OSS Distributions extern kern_return_t
77*c54f35caSApple OSS Distributions catch_mach_exception_raise_state(
78*c54f35caSApple OSS Distributions mach_port_t exception_port,
79*c54f35caSApple OSS Distributions exception_type_t type,
80*c54f35caSApple OSS Distributions exception_data_t codes,
81*c54f35caSApple OSS Distributions mach_msg_type_number_t code_count,
82*c54f35caSApple OSS Distributions int *flavor,
83*c54f35caSApple OSS Distributions thread_state_t in_state,
84*c54f35caSApple OSS Distributions mach_msg_type_number_t in_state_count,
85*c54f35caSApple OSS Distributions thread_state_t out_state,
86*c54f35caSApple OSS Distributions mach_msg_type_number_t *out_state_count);
87*c54f35caSApple OSS Distributions
88*c54f35caSApple OSS Distributions extern kern_return_t
89*c54f35caSApple OSS Distributions catch_mach_exception_raise_state_identity(
90*c54f35caSApple OSS Distributions mach_port_t exception_port,
91*c54f35caSApple OSS Distributions mach_port_t thread,
92*c54f35caSApple OSS Distributions mach_port_t task,
93*c54f35caSApple OSS Distributions exception_type_t type,
94*c54f35caSApple OSS Distributions exception_data_t codes,
95*c54f35caSApple OSS Distributions mach_msg_type_number_t code_count,
96*c54f35caSApple OSS Distributions int *flavor,
97*c54f35caSApple OSS Distributions thread_state_t in_state,
98*c54f35caSApple OSS Distributions mach_msg_type_number_t in_state_count,
99*c54f35caSApple OSS Distributions thread_state_t out_state,
100*c54f35caSApple OSS Distributions mach_msg_type_number_t *out_state_count);
101*c54f35caSApple OSS Distributions
102*c54f35caSApple OSS Distributions extern kern_return_t
103*c54f35caSApple OSS Distributions catch_mach_exception_raise_identity_protected(
104*c54f35caSApple OSS Distributions __unused mach_port_t exception_port,
105*c54f35caSApple OSS Distributions uint64_t thread_id,
106*c54f35caSApple OSS Distributions mach_port_t task_id_token,
107*c54f35caSApple OSS Distributions exception_type_t exception,
108*c54f35caSApple OSS Distributions mach_exception_data_t codes,
109*c54f35caSApple OSS Distributions mach_msg_type_number_t codeCnt);
110*c54f35caSApple OSS Distributions
111*c54f35caSApple OSS Distributions /**
112*c54f35caSApple OSS Distributions * This has to be defined for linking purposes, but it's unused.
113*c54f35caSApple OSS Distributions */
114*c54f35caSApple OSS Distributions kern_return_t
catch_mach_exception_raise(mach_port_t exception_port,mach_port_t thread,mach_port_t task,exception_type_t type,exception_data_t codes,mach_msg_type_number_t code_count)115*c54f35caSApple OSS Distributions catch_mach_exception_raise(
116*c54f35caSApple OSS Distributions mach_port_t exception_port,
117*c54f35caSApple OSS Distributions mach_port_t thread,
118*c54f35caSApple OSS Distributions mach_port_t task,
119*c54f35caSApple OSS Distributions exception_type_t type,
120*c54f35caSApple OSS Distributions exception_data_t codes,
121*c54f35caSApple OSS Distributions mach_msg_type_number_t code_count)
122*c54f35caSApple OSS Distributions {
123*c54f35caSApple OSS Distributions #pragma unused(exception_port, thread, task, type, codes, code_count)
124*c54f35caSApple OSS Distributions T_FAIL("Triggered catch_mach_exception_raise() which shouldn't happen...");
125*c54f35caSApple OSS Distributions __builtin_unreachable();
126*c54f35caSApple OSS Distributions }
127*c54f35caSApple OSS Distributions
128*c54f35caSApple OSS Distributions kern_return_t
catch_mach_exception_raise_identity_protected(__unused mach_port_t exception_port,uint64_t thread_id,mach_port_t task_id_token,exception_type_t exception,mach_exception_data_t codes,mach_msg_type_number_t codeCnt)129*c54f35caSApple OSS Distributions catch_mach_exception_raise_identity_protected(
130*c54f35caSApple OSS Distributions __unused mach_port_t exception_port,
131*c54f35caSApple OSS Distributions uint64_t thread_id,
132*c54f35caSApple OSS Distributions mach_port_t task_id_token,
133*c54f35caSApple OSS Distributions exception_type_t exception,
134*c54f35caSApple OSS Distributions mach_exception_data_t codes,
135*c54f35caSApple OSS Distributions mach_msg_type_number_t codeCnt)
136*c54f35caSApple OSS Distributions {
137*c54f35caSApple OSS Distributions #pragma unused(exception_port, thread_id, task_id_token, exception, codes, codeCnt)
138*c54f35caSApple OSS Distributions T_FAIL("Triggered catch_mach_exception_raise_identity_protected() which shouldn't happen...");
139*c54f35caSApple OSS Distributions __builtin_unreachable();
140*c54f35caSApple OSS Distributions }
141*c54f35caSApple OSS Distributions
142*c54f35caSApple OSS Distributions /**
143*c54f35caSApple OSS Distributions * This has to be defined for linking purposes, but it's unused.
144*c54f35caSApple OSS Distributions */
145*c54f35caSApple OSS Distributions kern_return_t
catch_mach_exception_raise_state(mach_port_t exception_port,exception_type_t type,exception_data_t codes,mach_msg_type_number_t code_count,int * flavor,thread_state_t in_state,mach_msg_type_number_t in_state_count,thread_state_t out_state,mach_msg_type_number_t * out_state_count)146*c54f35caSApple OSS Distributions catch_mach_exception_raise_state(
147*c54f35caSApple OSS Distributions mach_port_t exception_port,
148*c54f35caSApple OSS Distributions exception_type_t type,
149*c54f35caSApple OSS Distributions exception_data_t codes,
150*c54f35caSApple OSS Distributions mach_msg_type_number_t code_count,
151*c54f35caSApple OSS Distributions int *flavor,
152*c54f35caSApple OSS Distributions thread_state_t in_state,
153*c54f35caSApple OSS Distributions mach_msg_type_number_t in_state_count,
154*c54f35caSApple OSS Distributions thread_state_t out_state,
155*c54f35caSApple OSS Distributions mach_msg_type_number_t *out_state_count)
156*c54f35caSApple OSS Distributions {
157*c54f35caSApple OSS Distributions #pragma unused(exception_port, type, codes, code_count, flavor, in_state, in_state_count, out_state, out_state_count)
158*c54f35caSApple OSS Distributions T_FAIL("Triggered catch_mach_exception_raise_state() which shouldn't happen...");
159*c54f35caSApple OSS Distributions __builtin_unreachable();
160*c54f35caSApple OSS Distributions }
161*c54f35caSApple OSS Distributions
162*c54f35caSApple OSS Distributions static int exception_count = 0;
163*c54f35caSApple OSS Distributions static int reset_diversifier = 0;
164*c54f35caSApple OSS Distributions static semaphore_t semaphore;
165*c54f35caSApple OSS Distributions
166*c54f35caSApple OSS Distributions /*
167*c54f35caSApple OSS Distributions * Since the test needs to change the opaque field in
168*c54f35caSApple OSS Distributions * thread struct, the test redefines the thread struct
169*c54f35caSApple OSS Distributions * here. This is just for test purposes, this should not
170*c54f35caSApple OSS Distributions * be done anywhere else.
171*c54f35caSApple OSS Distributions */
172*c54f35caSApple OSS Distributions struct test_user_thread_state_64 {
173*c54f35caSApple OSS Distributions __uint64_t __x[29]; /* General purpose registers x0-x28 */
174*c54f35caSApple OSS Distributions void* __opaque_fp; /* Frame pointer x29 */
175*c54f35caSApple OSS Distributions void* __opaque_lr; /* Link register x30 */
176*c54f35caSApple OSS Distributions void* __opaque_sp; /* Stack pointer x31 */
177*c54f35caSApple OSS Distributions void* __opaque_pc; /* Program counter */
178*c54f35caSApple OSS Distributions __uint32_t __cpsr; /* Current program status register */
179*c54f35caSApple OSS Distributions __uint32_t __opaque_flags; /* Flags describing structure format */
180*c54f35caSApple OSS Distributions };
181*c54f35caSApple OSS Distributions #define __TEST_USER_THREAD_STATE64_FLAGS_KERNEL_SIGNED_PC 0x4
182*c54f35caSApple OSS Distributions
183*c54f35caSApple OSS Distributions /**
184*c54f35caSApple OSS Distributions * Called by mach_exc_server() to handle the exception.
185*c54f35caSApple OSS Distributions * The first time this is called, it will modify the pc
186*c54f35caSApple OSS Distributions * but keep the kernel signed bit. Next time this is called
187*c54f35caSApple OSS Distributions * it will modify the pc and remove the kernel signed bit.
188*c54f35caSApple OSS Distributions */
189*c54f35caSApple OSS Distributions kern_return_t
catch_mach_exception_raise_state_identity(mach_port_t exception_port __unused,mach_port_t thread __unused,mach_port_t task __unused,exception_type_t type __unused,exception_data_t codes __unused,mach_msg_type_number_t code_count __unused,int * flavor,thread_state_t in_state,mach_msg_type_number_t in_state_count,thread_state_t out_state,mach_msg_type_number_t * out_state_count)190*c54f35caSApple OSS Distributions catch_mach_exception_raise_state_identity(
191*c54f35caSApple OSS Distributions mach_port_t exception_port __unused,
192*c54f35caSApple OSS Distributions mach_port_t thread __unused,
193*c54f35caSApple OSS Distributions mach_port_t task __unused,
194*c54f35caSApple OSS Distributions exception_type_t type __unused,
195*c54f35caSApple OSS Distributions exception_data_t codes __unused,
196*c54f35caSApple OSS Distributions mach_msg_type_number_t code_count __unused,
197*c54f35caSApple OSS Distributions int *flavor,
198*c54f35caSApple OSS Distributions thread_state_t in_state,
199*c54f35caSApple OSS Distributions mach_msg_type_number_t in_state_count,
200*c54f35caSApple OSS Distributions thread_state_t out_state,
201*c54f35caSApple OSS Distributions mach_msg_type_number_t *out_state_count)
202*c54f35caSApple OSS Distributions {
203*c54f35caSApple OSS Distributions T_LOG("Caught a mach exception %d!\n", type);
204*c54f35caSApple OSS Distributions exception_count++;
205*c54f35caSApple OSS Distributions
206*c54f35caSApple OSS Distributions /* There should only be two code values. */
207*c54f35caSApple OSS Distributions T_QUIET; T_ASSERT_EQ(code_count, 2, "Two code values were provided with the mach exception");
208*c54f35caSApple OSS Distributions
209*c54f35caSApple OSS Distributions /**
210*c54f35caSApple OSS Distributions * The code values should be 64-bit since MACH_EXCEPTION_CODES was specified
211*c54f35caSApple OSS Distributions * when setting the exception port.
212*c54f35caSApple OSS Distributions */
213*c54f35caSApple OSS Distributions mach_exception_data_t codes_64 = (mach_exception_data_t)(void *)codes;
214*c54f35caSApple OSS Distributions T_LOG("Mach exception codes[0]: %#llx, codes[1]: %#llx\n", codes_64[0], codes_64[1]);
215*c54f35caSApple OSS Distributions
216*c54f35caSApple OSS Distributions if (type == EXC_CRASH) {
217*c54f35caSApple OSS Distributions T_LOG("Received a crash notification, signaling main thread and returning\n");
218*c54f35caSApple OSS Distributions T_ASSERT_MACH_SUCCESS(semaphore_signal(semaphore), "semaphore_signal");
219*c54f35caSApple OSS Distributions return KERN_SUCCESS;
220*c54f35caSApple OSS Distributions }
221*c54f35caSApple OSS Distributions
222*c54f35caSApple OSS Distributions /* Verify that we're receiving the expected thread state flavor. */
223*c54f35caSApple OSS Distributions T_QUIET; T_ASSERT_EQ(*flavor, EXCEPTION_THREAD_STATE, "The thread state flavor is EXCEPTION_THREAD_STATE");
224*c54f35caSApple OSS Distributions T_QUIET; T_ASSERT_EQ(in_state_count, EXCEPTION_THREAD_STATE_COUNT, "The thread state count is EXCEPTION_THREAD_STATE_COUNT");
225*c54f35caSApple OSS Distributions
226*c54f35caSApple OSS Distributions /**
227*c54f35caSApple OSS Distributions * Increment the PC by the 4 so the thread doesn't cause
228*c54f35caSApple OSS Distributions * another exception when it resumes.
229*c54f35caSApple OSS Distributions */
230*c54f35caSApple OSS Distributions *out_state_count = in_state_count; /* size of state object in 32-bit words */
231*c54f35caSApple OSS Distributions memcpy((void*)out_state, (void*)in_state, in_state_count * 4);
232*c54f35caSApple OSS Distributions
233*c54f35caSApple OSS Distributions #if __arm64__
234*c54f35caSApple OSS Distributions arm_thread_state64_t *state = (arm_thread_state64_t*)(void *)out_state;
235*c54f35caSApple OSS Distributions struct test_user_thread_state_64 *test_state = (struct test_user_thread_state_64 *)(void *)out_state;
236*c54f35caSApple OSS Distributions uint32_t userland_diversifier = test_state->__opaque_flags & 0xff000000;
237*c54f35caSApple OSS Distributions
238*c54f35caSApple OSS Distributions void *pc = (void*)(arm_thread_state64_get_pc(*state) + 4);
239*c54f35caSApple OSS Distributions /* Have to sign the new PC value when pointer authentication is enabled. */
240*c54f35caSApple OSS Distributions T_LOG("Userland diversifier for thread state is 0x%x\n", userland_diversifier);
241*c54f35caSApple OSS Distributions T_QUIET; T_ASSERT_NE(userland_diversifier, 0, "Userland diversifier is non zero");
242*c54f35caSApple OSS Distributions
243*c54f35caSApple OSS Distributions pc = ptrauth_sign_unauthenticated(pc, ptrauth_key_function_pointer, 0);
244*c54f35caSApple OSS Distributions arm_thread_state64_set_pc_fptr(*state, pc);
245*c54f35caSApple OSS Distributions
246*c54f35caSApple OSS Distributions /* Use the set and get lr, fp and sp function to make sure it compiles */
247*c54f35caSApple OSS Distributions arm_thread_state64_set_lr_fptr(*state, arm_thread_state64_get_lr_fptr(*state));
248*c54f35caSApple OSS Distributions arm_thread_state64_set_sp(*state, arm_thread_state64_get_sp(*state));
249*c54f35caSApple OSS Distributions arm_thread_state64_set_fp(*state, arm_thread_state64_get_fp(*state));
250*c54f35caSApple OSS Distributions #endif
251*c54f35caSApple OSS Distributions
252*c54f35caSApple OSS Distributions if (reset_diversifier == 0) {
253*c54f35caSApple OSS Distributions if (exception_count == 1) {
254*c54f35caSApple OSS Distributions #if __arm64__
255*c54f35caSApple OSS Distributions /* Set the kernel signed bit, so kernel ignores the new PC */
256*c54f35caSApple OSS Distributions test_state->__opaque_flags |= __TEST_USER_THREAD_STATE64_FLAGS_KERNEL_SIGNED_PC;
257*c54f35caSApple OSS Distributions T_LOG("Set the kernel signed flag on the thread state");
258*c54f35caSApple OSS Distributions #else
259*c54f35caSApple OSS Distributions T_LOG("Not on arm64, Not doing anything");
260*c54f35caSApple OSS Distributions #endif
261*c54f35caSApple OSS Distributions } else if (exception_count == 2) {
262*c54f35caSApple OSS Distributions T_LOG("Not clearing the kernel signed bit, this should be the last exception");
263*c54f35caSApple OSS Distributions } else {
264*c54f35caSApple OSS Distributions T_FAIL("Received more than 2 exceptions, failing the test");
265*c54f35caSApple OSS Distributions }
266*c54f35caSApple OSS Distributions } else {
267*c54f35caSApple OSS Distributions if (exception_count == 1) {
268*c54f35caSApple OSS Distributions #if __arm64__
269*c54f35caSApple OSS Distributions /* Set the user diversifier to zero and resign the pc */
270*c54f35caSApple OSS Distributions test_state->__opaque_flags &= 0x00ffffff;
271*c54f35caSApple OSS Distributions arm_thread_state64_set_pc_fptr(*state, pc);
272*c54f35caSApple OSS Distributions T_LOG("Set the diversifier to zero and signed the pc, this should crash on return");
273*c54f35caSApple OSS Distributions #else
274*c54f35caSApple OSS Distributions T_LOG("Not on arm64, Not doing anything");
275*c54f35caSApple OSS Distributions #endif
276*c54f35caSApple OSS Distributions } else {
277*c54f35caSApple OSS Distributions T_FAIL("Received more than 2 exceptions, failing the test");
278*c54f35caSApple OSS Distributions }
279*c54f35caSApple OSS Distributions }
280*c54f35caSApple OSS Distributions
281*c54f35caSApple OSS Distributions /* Return KERN_SUCCESS to tell the kernel to keep running the victim thread. */
282*c54f35caSApple OSS Distributions return KERN_SUCCESS;
283*c54f35caSApple OSS Distributions }
284*c54f35caSApple OSS Distributions
285*c54f35caSApple OSS Distributions static mach_port_t
create_exception_port_behavior64(exception_mask_t exception_mask,exception_behavior_t behavior)286*c54f35caSApple OSS Distributions create_exception_port_behavior64(exception_mask_t exception_mask, exception_behavior_t behavior)
287*c54f35caSApple OSS Distributions {
288*c54f35caSApple OSS Distributions mach_port_t exc_port = MACH_PORT_NULL;
289*c54f35caSApple OSS Distributions mach_port_t task = mach_task_self();
290*c54f35caSApple OSS Distributions kern_return_t kr = KERN_SUCCESS;
291*c54f35caSApple OSS Distributions
292*c54f35caSApple OSS Distributions if (behavior != EXCEPTION_STATE_IDENTITY && behavior != EXCEPTION_IDENTITY_PROTECTED) {
293*c54f35caSApple OSS Distributions T_FAIL("Currently only EXCEPTION_STATE_IDENTITY and EXCEPTION_IDENTITY_PROTECTED are implemented");
294*c54f35caSApple OSS Distributions }
295*c54f35caSApple OSS Distributions
296*c54f35caSApple OSS Distributions /* Create the mach port the exception messages will be sent to. */
297*c54f35caSApple OSS Distributions kr = mach_port_allocate(task, MACH_PORT_RIGHT_RECEIVE, &exc_port);
298*c54f35caSApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "Allocated mach exception port");
299*c54f35caSApple OSS Distributions
300*c54f35caSApple OSS Distributions /**
301*c54f35caSApple OSS Distributions * Insert a send right into the exception port that the kernel will use to
302*c54f35caSApple OSS Distributions * send the exception thread the exception messages.
303*c54f35caSApple OSS Distributions */
304*c54f35caSApple OSS Distributions kr = mach_port_insert_right(task, exc_port, exc_port, MACH_MSG_TYPE_MAKE_SEND);
305*c54f35caSApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "Inserted a SEND right into the exception port");
306*c54f35caSApple OSS Distributions
307*c54f35caSApple OSS Distributions /* Tell the kernel what port to send exceptions to. */
308*c54f35caSApple OSS Distributions kr = task_set_exception_ports(
309*c54f35caSApple OSS Distributions task,
310*c54f35caSApple OSS Distributions exception_mask,
311*c54f35caSApple OSS Distributions exc_port,
312*c54f35caSApple OSS Distributions (exception_behavior_t)(behavior | (exception_behavior_t)MACH_EXCEPTION_CODES),
313*c54f35caSApple OSS Distributions EXCEPTION_THREAD_STATE);
314*c54f35caSApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "Set the exception port to my custom handler");
315*c54f35caSApple OSS Distributions
316*c54f35caSApple OSS Distributions return exc_port;
317*c54f35caSApple OSS Distributions }
318*c54f35caSApple OSS Distributions
319*c54f35caSApple OSS Distributions static mach_port_t __unused
create_exception_port(exception_mask_t exception_mask)320*c54f35caSApple OSS Distributions create_exception_port(exception_mask_t exception_mask)
321*c54f35caSApple OSS Distributions {
322*c54f35caSApple OSS Distributions return create_exception_port_behavior64(exception_mask, EXCEPTION_STATE_IDENTITY);
323*c54f35caSApple OSS Distributions }
324*c54f35caSApple OSS Distributions
325*c54f35caSApple OSS Distributions /**
326*c54f35caSApple OSS Distributions * Thread to handle the mach exception.
327*c54f35caSApple OSS Distributions *
328*c54f35caSApple OSS Distributions * @param arg The exception port to wait for a message on.
329*c54f35caSApple OSS Distributions */
330*c54f35caSApple OSS Distributions static void *
exc_server_thread(void * arg)331*c54f35caSApple OSS Distributions exc_server_thread(void *arg)
332*c54f35caSApple OSS Distributions {
333*c54f35caSApple OSS Distributions mach_port_t exc_port = (mach_port_t)arg;
334*c54f35caSApple OSS Distributions kern_return_t kr;
335*c54f35caSApple OSS Distributions
336*c54f35caSApple OSS Distributions /**
337*c54f35caSApple OSS Distributions * mach_msg_server_once is a helper function provided by libsyscall that
338*c54f35caSApple OSS Distributions * handles creating mach messages, blocks waiting for a message on the
339*c54f35caSApple OSS Distributions * exception port, calls mach_exc_server() to handle the exception, and
340*c54f35caSApple OSS Distributions * sends a reply based on the return value of mach_exc_server().
341*c54f35caSApple OSS Distributions */
342*c54f35caSApple OSS Distributions #define MACH_MSG_REPLY_SIZE 4096
343*c54f35caSApple OSS Distributions kr = mach_msg_server(mach_exc_server, MACH_MSG_REPLY_SIZE, exc_port, 0);
344*c54f35caSApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "Received mach exception message");
345*c54f35caSApple OSS Distributions
346*c54f35caSApple OSS Distributions pthread_exit((void*)0);
347*c54f35caSApple OSS Distributions __builtin_unreachable();
348*c54f35caSApple OSS Distributions }
349*c54f35caSApple OSS Distributions
350*c54f35caSApple OSS Distributions static void __unused
run_exception_handler(mach_port_t exc_port)351*c54f35caSApple OSS Distributions run_exception_handler(mach_port_t exc_port)
352*c54f35caSApple OSS Distributions {
353*c54f35caSApple OSS Distributions pthread_t exc_thread;
354*c54f35caSApple OSS Distributions
355*c54f35caSApple OSS Distributions /* Spawn the exception server's thread. */
356*c54f35caSApple OSS Distributions int err = pthread_create(&exc_thread, (pthread_attr_t*)0, exc_server_thread, (void *)(unsigned long long)exc_port);
357*c54f35caSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(err, "Spawned exception server thread");
358*c54f35caSApple OSS Distributions
359*c54f35caSApple OSS Distributions /* No need to wait for the exception server to be joined when it exits. */
360*c54f35caSApple OSS Distributions pthread_detach(exc_thread);
361*c54f35caSApple OSS Distributions }
362*c54f35caSApple OSS Distributions
363*c54f35caSApple OSS Distributions T_DECL(kernel_signed_pac_thread_state, "Test that kernel signed thread state given to exception ignores the pc")
364*c54f35caSApple OSS Distributions {
365*c54f35caSApple OSS Distributions #if !__arm64e__
366*c54f35caSApple OSS Distributions T_SKIP("Running on non-arm64e target, skipping...");
367*c54f35caSApple OSS Distributions #else
368*c54f35caSApple OSS Distributions mach_port_t exc_port = create_exception_port(EXC_MASK_BAD_ACCESS);
369*c54f35caSApple OSS Distributions
370*c54f35caSApple OSS Distributions int expected_exception = 2;
371*c54f35caSApple OSS Distributions
372*c54f35caSApple OSS Distributions run_exception_handler(exc_port);
373*c54f35caSApple OSS Distributions *(void *volatile*)0 = 0;
374*c54f35caSApple OSS Distributions
375*c54f35caSApple OSS Distributions if (exception_count != expected_exception) {
376*c54f35caSApple OSS Distributions T_FAIL("Expected %d exceptions, received %d", expected_exception, exception_count);
377*c54f35caSApple OSS Distributions } else {
378*c54f35caSApple OSS Distributions T_LOG("TEST PASSED");
379*c54f35caSApple OSS Distributions }
380*c54f35caSApple OSS Distributions T_END;
381*c54f35caSApple OSS Distributions #endif
382*c54f35caSApple OSS Distributions }
383*c54f35caSApple OSS Distributions
384*c54f35caSApple OSS Distributions T_DECL(user_signed_pac_thread_state, "Test that user signed thread state given to exception works with correct diversifier")
385*c54f35caSApple OSS Distributions {
386*c54f35caSApple OSS Distributions #if !__arm64e__
387*c54f35caSApple OSS Distributions T_SKIP("Running on non-arm64e target, skipping...");
388*c54f35caSApple OSS Distributions #else
389*c54f35caSApple OSS Distributions mach_port_t exc_port = create_exception_port(EXC_MASK_BAD_ACCESS | EXC_MASK_CRASH);
390*c54f35caSApple OSS Distributions T_ASSERT_MACH_SUCCESS(semaphore_create(mach_task_self(), &semaphore,
391*c54f35caSApple OSS Distributions SYNC_POLICY_FIFO, 0), "semaphore_create");
392*c54f35caSApple OSS Distributions
393*c54f35caSApple OSS Distributions exception_count = 0;
394*c54f35caSApple OSS Distributions int expected_exception = 2;
395*c54f35caSApple OSS Distributions
396*c54f35caSApple OSS Distributions run_exception_handler(exc_port);
397*c54f35caSApple OSS Distributions
398*c54f35caSApple OSS Distributions /* Set the reset diversifier variable */
399*c54f35caSApple OSS Distributions reset_diversifier = 1;
400*c54f35caSApple OSS Distributions pid_t child_pid = fork();
401*c54f35caSApple OSS Distributions
402*c54f35caSApple OSS Distributions if (child_pid == 0) {
403*c54f35caSApple OSS Distributions *(void *volatile*)0 = 0;
404*c54f35caSApple OSS Distributions T_FAIL("Child should have been terminated, but it did not");
405*c54f35caSApple OSS Distributions }
406*c54f35caSApple OSS Distributions
407*c54f35caSApple OSS Distributions T_ASSERT_MACH_SUCCESS(semaphore_wait(semaphore), "semaphore_wait");
408*c54f35caSApple OSS Distributions
409*c54f35caSApple OSS Distributions if (exception_count != expected_exception) {
410*c54f35caSApple OSS Distributions T_FAIL("Expected %d exceptions, received %d", expected_exception, exception_count);
411*c54f35caSApple OSS Distributions } else {
412*c54f35caSApple OSS Distributions T_LOG("TEST PASSED");
413*c54f35caSApple OSS Distributions }
414*c54f35caSApple OSS Distributions T_END;
415*c54f35caSApple OSS Distributions #endif
416*c54f35caSApple OSS Distributions }
417