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