xref: /xnu-11215.81.4/tests/reply_port_defense.c (revision d4514f0bc1d3f944c22d92e68b646ac3fb40d452)
1*d4514f0bSApple OSS Distributions #include <stdio.h>
2*d4514f0bSApple OSS Distributions #include <stdlib.h>
3*d4514f0bSApple OSS Distributions #include <unistd.h>
4*d4514f0bSApple OSS Distributions #include <darwintest.h>
5*d4514f0bSApple OSS Distributions #include <spawn.h>
6*d4514f0bSApple OSS Distributions #include <mach/mach.h>
7*d4514f0bSApple OSS Distributions #include <sys/sysctl.h>
8*d4514f0bSApple OSS Distributions #include <signal.h>
9*d4514f0bSApple OSS Distributions #include "excserver_protect_state.h"
10*d4514f0bSApple OSS Distributions #include "../osfmk/ipc/ipc_init.h"
11*d4514f0bSApple OSS Distributions #include "../osfmk/mach/port.h"
12*d4514f0bSApple OSS Distributions #include "../osfmk/kern/exc_guard.h"
13*d4514f0bSApple OSS Distributions #include "exc_helpers.h"
14*d4514f0bSApple OSS Distributions #include <sys/code_signing.h>
15*d4514f0bSApple OSS Distributions #include "cs_helpers.h"
16*d4514f0bSApple OSS Distributions #include <TargetConditionals.h>
17*d4514f0bSApple OSS Distributions 
18*d4514f0bSApple OSS Distributions #define MAX_TEST_NUM 10
19*d4514f0bSApple OSS Distributions #define MAX_ARGV 3
20*d4514f0bSApple OSS Distributions 
21*d4514f0bSApple OSS Distributions extern char **environ;
22*d4514f0bSApple OSS Distributions static mach_exception_data_type_t received_exception_code = 0;
23*d4514f0bSApple OSS Distributions static exception_type_t exception_taken = 0;
24*d4514f0bSApple OSS Distributions 
25*d4514f0bSApple OSS Distributions /*
26*d4514f0bSApple OSS Distributions  * This test infrastructure is inspired from imm_pinned_control_port.c.
27*d4514f0bSApple OSS Distributions  * It verifies no reply port security semantics are violated.
28*d4514f0bSApple OSS Distributions  *
29*d4514f0bSApple OSS Distributions  * 1. The rcv right of the port would be marked immovable.
30*d4514f0bSApple OSS Distributions  */
31*d4514f0bSApple OSS Distributions T_GLOBAL_META(
32*d4514f0bSApple OSS Distributions 	T_META_NAMESPACE("xnu.ipc"),
33*d4514f0bSApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
34*d4514f0bSApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("IPC"),
35*d4514f0bSApple OSS Distributions 	T_META_TIMEOUT(10),
36*d4514f0bSApple OSS Distributions 	T_META_RUN_CONCURRENTLY(TRUE));
37*d4514f0bSApple OSS Distributions 
38*d4514f0bSApple OSS Distributions static mach_port_t
alloc_exception_port(void)39*d4514f0bSApple OSS Distributions alloc_exception_port(void)
40*d4514f0bSApple OSS Distributions {
41*d4514f0bSApple OSS Distributions 	kern_return_t kret;
42*d4514f0bSApple OSS Distributions 	mach_port_t exc_port = MACH_PORT_NULL;
43*d4514f0bSApple OSS Distributions 	mach_port_t task = mach_task_self();
44*d4514f0bSApple OSS Distributions 
45*d4514f0bSApple OSS Distributions 	kret = mach_port_allocate(task, MACH_PORT_RIGHT_RECEIVE, &exc_port);
46*d4514f0bSApple OSS Distributions 	T_QUIET; T_EXPECT_MACH_SUCCESS(kret, "mach_port_allocate exc_port");
47*d4514f0bSApple OSS Distributions 
48*d4514f0bSApple OSS Distributions 	kret = mach_port_insert_right(task, exc_port, exc_port, MACH_MSG_TYPE_MAKE_SEND);
49*d4514f0bSApple OSS Distributions 	T_QUIET; T_EXPECT_MACH_SUCCESS(kret, "mach_port_insert_right exc_port");
50*d4514f0bSApple OSS Distributions 
51*d4514f0bSApple OSS Distributions 	return exc_port;
52*d4514f0bSApple OSS Distributions }
53*d4514f0bSApple OSS Distributions 
54*d4514f0bSApple OSS Distributions kern_return_t
catch_mach_exception_raise_state(mach_port_t exception_port,exception_type_t exception,const mach_exception_data_t code,mach_msg_type_number_t code_count,int * flavor,const thread_state_t old_state,mach_msg_type_number_t old_state_count,thread_state_t new_state,mach_msg_type_number_t * new_state_count)55*d4514f0bSApple OSS Distributions catch_mach_exception_raise_state(mach_port_t exception_port,
56*d4514f0bSApple OSS Distributions     exception_type_t exception,
57*d4514f0bSApple OSS Distributions     const mach_exception_data_t code,
58*d4514f0bSApple OSS Distributions     mach_msg_type_number_t code_count,
59*d4514f0bSApple OSS Distributions     int * flavor,
60*d4514f0bSApple OSS Distributions     const thread_state_t old_state,
61*d4514f0bSApple OSS Distributions     mach_msg_type_number_t old_state_count,
62*d4514f0bSApple OSS Distributions     thread_state_t new_state,
63*d4514f0bSApple OSS Distributions     mach_msg_type_number_t * new_state_count)
64*d4514f0bSApple OSS Distributions {
65*d4514f0bSApple OSS Distributions #pragma unused(exception_port, exception, code, code_count, flavor, old_state, old_state_count, new_state, new_state_count)
66*d4514f0bSApple OSS Distributions 	T_FAIL("Unsupported catch_mach_exception_raise_state");
67*d4514f0bSApple OSS Distributions 	return KERN_NOT_SUPPORTED;
68*d4514f0bSApple OSS Distributions }
69*d4514f0bSApple OSS Distributions 
70*d4514f0bSApple OSS Distributions kern_return_t
catch_mach_exception_raise_state_identity(mach_port_t exception_port,mach_port_t thread,mach_port_t task,exception_type_t exception,mach_exception_data_t code,mach_msg_type_number_t code_count,int * flavor,thread_state_t old_state,mach_msg_type_number_t old_state_count,thread_state_t new_state,mach_msg_type_number_t * new_state_count)71*d4514f0bSApple OSS Distributions catch_mach_exception_raise_state_identity(mach_port_t exception_port,
72*d4514f0bSApple OSS Distributions     mach_port_t thread,
73*d4514f0bSApple OSS Distributions     mach_port_t task,
74*d4514f0bSApple OSS Distributions     exception_type_t exception,
75*d4514f0bSApple OSS Distributions     mach_exception_data_t code,
76*d4514f0bSApple OSS Distributions     mach_msg_type_number_t code_count,
77*d4514f0bSApple OSS Distributions     int * flavor,
78*d4514f0bSApple OSS Distributions     thread_state_t old_state,
79*d4514f0bSApple OSS Distributions     mach_msg_type_number_t old_state_count,
80*d4514f0bSApple OSS Distributions     thread_state_t new_state,
81*d4514f0bSApple OSS Distributions     mach_msg_type_number_t * new_state_count)
82*d4514f0bSApple OSS Distributions {
83*d4514f0bSApple OSS Distributions #pragma unused(exception_port, thread, task, exception, code, code_count, flavor, old_state, old_state_count, new_state, new_state_count)
84*d4514f0bSApple OSS Distributions 	T_FAIL("Unsupported catch_mach_exception_raise_state_identity");
85*d4514f0bSApple OSS Distributions 	return KERN_NOT_SUPPORTED;
86*d4514f0bSApple OSS Distributions }
87*d4514f0bSApple OSS Distributions 
88*d4514f0bSApple OSS Distributions kern_return_t
catch_mach_exception_raise_state_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,__unused int * flavor,__unused thread_state_t old_state,__unused mach_msg_type_number_t old_state_count,__unused thread_state_t new_state,__unused mach_msg_type_number_t * new_state_count)89*d4514f0bSApple OSS Distributions catch_mach_exception_raise_state_identity_protected(
90*d4514f0bSApple OSS Distributions 	__unused mach_port_t      exception_port,
91*d4514f0bSApple OSS Distributions 	uint64_t                  thread_id,
92*d4514f0bSApple OSS Distributions 	mach_port_t               task_id_token,
93*d4514f0bSApple OSS Distributions 	exception_type_t          exception,
94*d4514f0bSApple OSS Distributions 	mach_exception_data_t     codes,
95*d4514f0bSApple OSS Distributions 	mach_msg_type_number_t    codeCnt,
96*d4514f0bSApple OSS Distributions 	__unused int * flavor,
97*d4514f0bSApple OSS Distributions 	__unused thread_state_t old_state,
98*d4514f0bSApple OSS Distributions 	__unused mach_msg_type_number_t old_state_count,
99*d4514f0bSApple OSS Distributions 	__unused thread_state_t new_state,
100*d4514f0bSApple OSS Distributions 	__unused mach_msg_type_number_t * new_state_count)
101*d4514f0bSApple OSS Distributions {
102*d4514f0bSApple OSS Distributions #pragma unused(exception_port, thread_id, task_id_token, exception, codes, codeCnt)
103*d4514f0bSApple OSS Distributions 
104*d4514f0bSApple OSS Distributions 	T_FAIL("Unsupported catch_mach_exception_raise_state_identity_protected");
105*d4514f0bSApple OSS Distributions 	return KERN_NOT_SUPPORTED;
106*d4514f0bSApple OSS Distributions }
107*d4514f0bSApple OSS Distributions 
108*d4514f0bSApple 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)109*d4514f0bSApple OSS Distributions catch_mach_exception_raise_identity_protected(
110*d4514f0bSApple OSS Distributions 	__unused mach_port_t      exception_port,
111*d4514f0bSApple OSS Distributions 	uint64_t                  thread_id,
112*d4514f0bSApple OSS Distributions 	mach_port_t               task_id_token,
113*d4514f0bSApple OSS Distributions 	exception_type_t          exception,
114*d4514f0bSApple OSS Distributions 	mach_exception_data_t     codes,
115*d4514f0bSApple OSS Distributions 	mach_msg_type_number_t    codeCnt)
116*d4514f0bSApple OSS Distributions {
117*d4514f0bSApple OSS Distributions #pragma unused(exception_port, thread_id, task_id_token)
118*d4514f0bSApple OSS Distributions 
119*d4514f0bSApple OSS Distributions 	T_ASSERT_GT_UINT(codeCnt, 0, "CodeCnt");
120*d4514f0bSApple OSS Distributions 
121*d4514f0bSApple OSS Distributions 	T_LOG("Caught exception type: %d code: 0x%llx", exception, codes[0]);
122*d4514f0bSApple OSS Distributions 	exception_taken = exception;
123*d4514f0bSApple OSS Distributions 	if (exception == EXC_GUARD) {
124*d4514f0bSApple OSS Distributions 		received_exception_code = EXC_GUARD_DECODE_GUARD_FLAVOR((uint64_t)codes[0]);
125*d4514f0bSApple OSS Distributions 	} else {
126*d4514f0bSApple OSS Distributions 		T_FAIL("Unexpected exception");
127*d4514f0bSApple OSS Distributions 	}
128*d4514f0bSApple OSS Distributions 	return KERN_SUCCESS;
129*d4514f0bSApple OSS Distributions }
130*d4514f0bSApple OSS Distributions 
131*d4514f0bSApple 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 exception,mach_exception_data_t code,mach_msg_type_number_t code_count)132*d4514f0bSApple OSS Distributions catch_mach_exception_raise(mach_port_t exception_port,
133*d4514f0bSApple OSS Distributions     mach_port_t thread,
134*d4514f0bSApple OSS Distributions     mach_port_t task,
135*d4514f0bSApple OSS Distributions     exception_type_t exception,
136*d4514f0bSApple OSS Distributions     mach_exception_data_t code,
137*d4514f0bSApple OSS Distributions     mach_msg_type_number_t code_count)
138*d4514f0bSApple OSS Distributions {
139*d4514f0bSApple OSS Distributions #pragma unused(exception_port, thread, task, exception, code, code_count)
140*d4514f0bSApple OSS Distributions 	T_FAIL("Unsupported catch_mach_exception_raise_state_identity");
141*d4514f0bSApple OSS Distributions 	return KERN_NOT_SUPPORTED;
142*d4514f0bSApple OSS Distributions }
143*d4514f0bSApple OSS Distributions 
144*d4514f0bSApple OSS Distributions static void *
exception_server_thread(void * arg)145*d4514f0bSApple OSS Distributions exception_server_thread(void *arg)
146*d4514f0bSApple OSS Distributions {
147*d4514f0bSApple OSS Distributions 	kern_return_t kr;
148*d4514f0bSApple OSS Distributions 	mach_port_t exc_port = *(mach_port_t *)arg;
149*d4514f0bSApple OSS Distributions 
150*d4514f0bSApple OSS Distributions 	/* Handle exceptions on exc_port */
151*d4514f0bSApple OSS Distributions 	kr = mach_msg_server_once(mach_exc_server, 4096, exc_port, 0);
152*d4514f0bSApple OSS Distributions 	T_QUIET; T_EXPECT_MACH_SUCCESS(kr, "mach_msg_server_once");
153*d4514f0bSApple OSS Distributions 
154*d4514f0bSApple OSS Distributions 	return NULL;
155*d4514f0bSApple OSS Distributions }
156*d4514f0bSApple OSS Distributions 
157*d4514f0bSApple OSS Distributions static void
reply_port_defense(const bool thirdparty_hardened,int test_index,mach_exception_data_type_t expected_exception_code,bool triggers_exception)158*d4514f0bSApple OSS Distributions reply_port_defense(const bool thirdparty_hardened, int test_index, mach_exception_data_type_t expected_exception_code, bool triggers_exception)
159*d4514f0bSApple OSS Distributions {
160*d4514f0bSApple OSS Distributions 	int ret = 0;
161*d4514f0bSApple OSS Distributions 
162*d4514f0bSApple OSS Distributions 	uint32_t task_exc_guard = 0;
163*d4514f0bSApple OSS Distributions 	size_t te_size = sizeof(&task_exc_guard);
164*d4514f0bSApple OSS Distributions 
165*d4514f0bSApple OSS Distributions 	/* Test that the behavior is the same between these two */
166*d4514f0bSApple OSS Distributions 	char *test_prog_name = thirdparty_hardened ?
167*d4514f0bSApple OSS Distributions 	    "./reply_port_defense_client_3P_hardened" : "./reply_port_defense_client";
168*d4514f0bSApple OSS Distributions 	char *child_args[MAX_ARGV];
169*d4514f0bSApple OSS Distributions 	pid_t client_pid = 0;
170*d4514f0bSApple OSS Distributions 	posix_spawnattr_t attrs;
171*d4514f0bSApple OSS Distributions 
172*d4514f0bSApple OSS Distributions 	pthread_t s_exc_thread;
173*d4514f0bSApple OSS Distributions 	mach_port_t exc_port;
174*d4514f0bSApple OSS Distributions 
175*d4514f0bSApple OSS Distributions 	T_LOG("Check if task_exc_guard exception has been enabled\n");
176*d4514f0bSApple OSS Distributions 	ret = sysctlbyname("kern.task_exc_guard_default", &task_exc_guard, &te_size, NULL, 0);
177*d4514f0bSApple OSS Distributions 	T_ASSERT_EQ(ret, 0, "sysctlbyname");
178*d4514f0bSApple OSS Distributions 
179*d4514f0bSApple OSS Distributions 	if (!(task_exc_guard & TASK_EXC_GUARD_MP_DELIVER)) {
180*d4514f0bSApple OSS Distributions 		T_SKIP("task_exc_guard exception is not enabled");
181*d4514f0bSApple OSS Distributions 	}
182*d4514f0bSApple OSS Distributions 
183*d4514f0bSApple OSS Distributions 	exc_port = alloc_exception_port();
184*d4514f0bSApple OSS Distributions 	T_QUIET; T_ASSERT_NE(exc_port, MACH_PORT_NULL, "Create a new exception port");
185*d4514f0bSApple OSS Distributions 
186*d4514f0bSApple OSS Distributions 	/* Create exception serving thread */
187*d4514f0bSApple OSS Distributions 	ret = pthread_create(&s_exc_thread, NULL, exception_server_thread, &exc_port);
188*d4514f0bSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "pthread_create exception_server_thread");
189*d4514f0bSApple OSS Distributions 
190*d4514f0bSApple OSS Distributions 	/* Initialize posix_spawn attributes */
191*d4514f0bSApple OSS Distributions 	posix_spawnattr_init(&attrs);
192*d4514f0bSApple OSS Distributions 
193*d4514f0bSApple OSS Distributions 	/*
194*d4514f0bSApple OSS Distributions 	 * It is allowed for us to set an exception port because we are entitled test process.
195*d4514f0bSApple OSS Distributions 	 * If you are using this code as an example for platform binaries,
196*d4514f0bSApple OSS Distributions 	 * use `EXCEPTION_IDENTITY_PROTECTED` instead of `EXCEPTION_DEFAULT`
197*d4514f0bSApple OSS Distributions 	 */
198*d4514f0bSApple OSS Distributions 	int err = posix_spawnattr_setexceptionports_np(&attrs, EXC_MASK_GUARD, exc_port,
199*d4514f0bSApple OSS Distributions 	    (exception_behavior_t) (EXCEPTION_IDENTITY_PROTECTED | MACH_EXCEPTION_CODES), 0);
200*d4514f0bSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(err, "posix_spawnattr_setflags");
201*d4514f0bSApple OSS Distributions 
202*d4514f0bSApple OSS Distributions 	child_args[0] = test_prog_name;
203*d4514f0bSApple OSS Distributions 	char test_num[10];
204*d4514f0bSApple OSS Distributions 	sprintf(test_num, "%d", test_index);
205*d4514f0bSApple OSS Distributions 	child_args[1] = test_num;
206*d4514f0bSApple OSS Distributions 	child_args[2] = NULL;
207*d4514f0bSApple OSS Distributions 
208*d4514f0bSApple OSS Distributions 	T_LOG("========== Spawning new child ==========");
209*d4514f0bSApple OSS Distributions 	err = posix_spawn(&client_pid, child_args[0], NULL, &attrs, &child_args[0], environ);
210*d4514f0bSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(err, "posix_spawn reply_port_defense_client = %d", client_pid);
211*d4514f0bSApple OSS Distributions 
212*d4514f0bSApple OSS Distributions 	int child_status;
213*d4514f0bSApple OSS Distributions 	/* Wait for child and check for exception */
214*d4514f0bSApple OSS Distributions 	if (-1 == waitpid(-1, &child_status, 0)) {
215*d4514f0bSApple OSS Distributions 		T_FAIL("%s waitpid: child", strerror(errno));
216*d4514f0bSApple OSS Distributions 	}
217*d4514f0bSApple OSS Distributions 	if (WIFEXITED(child_status) && WEXITSTATUS(child_status)) {
218*d4514f0bSApple OSS Distributions 		T_FAIL("Child exited with status = 0x%x", child_status);
219*d4514f0bSApple OSS Distributions 	}
220*d4514f0bSApple OSS Distributions 	sleep(1);
221*d4514f0bSApple OSS Distributions 	kill(1, SIGKILL);
222*d4514f0bSApple OSS Distributions 	if (triggers_exception) {
223*d4514f0bSApple OSS Distributions 		ret = pthread_join(s_exc_thread, NULL);
224*d4514f0bSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "pthread_join");
225*d4514f0bSApple OSS Distributions 	}
226*d4514f0bSApple OSS Distributions 
227*d4514f0bSApple OSS Distributions 	mach_port_deallocate(mach_task_self(), exc_port);
228*d4514f0bSApple OSS Distributions 
229*d4514f0bSApple OSS Distributions 	T_LOG("Exception code: Received code = 0x%llx Expected code = 0x%llx", received_exception_code, expected_exception_code);
230*d4514f0bSApple OSS Distributions 	T_EXPECT_EQ(received_exception_code, expected_exception_code, "Exception code: Received == Expected");
231*d4514f0bSApple OSS Distributions }
232*d4514f0bSApple OSS Distributions 
233*d4514f0bSApple OSS Distributions T_DECL(reply_port_defense,
234*d4514f0bSApple OSS Distributions     "Test reply port semantics violations",
235*d4514f0bSApple OSS Distributions     T_META_IGNORECRASHES(".*reply_port_defense_client.*"),
236*d4514f0bSApple OSS Distributions     T_META_CHECK_LEAKS(false),
237*d4514f0bSApple OSS Distributions     T_META_TAG_VM_NOT_PREFERRED) {
238*d4514f0bSApple OSS Distributions 	bool triggers_exception = true;
239*d4514f0bSApple OSS Distributions 	/* The first test is setup as moving immovable receive right of a reply port. */
240*d4514f0bSApple OSS Distributions 	reply_port_defense(true, 0, kGUARD_EXC_IMMOVABLE, triggers_exception);
241*d4514f0bSApple OSS Distributions 	reply_port_defense(false, 0, kGUARD_EXC_IMMOVABLE, triggers_exception);
242*d4514f0bSApple OSS Distributions 
243*d4514f0bSApple OSS Distributions 	int rp_defense_max_test_idx = 3;
244*d4514f0bSApple OSS Distributions 	/* Run the reply_port_defense tests 1, 2, and 3 */
245*d4514f0bSApple OSS Distributions 	mach_exception_data_type_t expected_exception_code = kGUARD_EXC_INVALID_RIGHT;
246*d4514f0bSApple OSS Distributions 	for (int i = 1; i <= rp_defense_max_test_idx; i++) {
247*d4514f0bSApple OSS Distributions 		reply_port_defense(true, i, expected_exception_code, triggers_exception);
248*d4514f0bSApple OSS Distributions 		reply_port_defense(false, i, expected_exception_code, triggers_exception);
249*d4514f0bSApple OSS Distributions 	}
250*d4514f0bSApple OSS Distributions }
251*d4514f0bSApple OSS Distributions 
252*d4514f0bSApple OSS Distributions 
253*d4514f0bSApple OSS Distributions T_DECL(test_move_provisional_reply_port,
254*d4514f0bSApple OSS Distributions     "provisional reply ports are immovable",
255*d4514f0bSApple OSS Distributions     T_META_IGNORECRASHES(".*reply_port_defense_client.*"),
256*d4514f0bSApple OSS Distributions     T_META_CHECK_LEAKS(false)) {
257*d4514f0bSApple OSS Distributions 	int test_num = 4;
258*d4514f0bSApple OSS Distributions 	mach_exception_data_type_t expected_exception_code = 0;
259*d4514f0bSApple OSS Distributions 	bool triggers_exception = false;
260*d4514f0bSApple OSS Distributions 	reply_port_defense(true, test_num, expected_exception_code, triggers_exception);
261*d4514f0bSApple OSS Distributions 	reply_port_defense(false, test_num, expected_exception_code, triggers_exception);
262*d4514f0bSApple OSS Distributions }
263*d4514f0bSApple OSS Distributions 
264*d4514f0bSApple OSS Distributions T_DECL(test_unentitled_thread_set_exception_ports,
265*d4514f0bSApple OSS Distributions     "thread_set_exception_ports should fail without an entitlement",
266*d4514f0bSApple OSS Distributions     T_META_IGNORECRASHES(".*reply_port_defense_client.*"),
267*d4514f0bSApple OSS Distributions     T_META_CHECK_LEAKS(false)) {
268*d4514f0bSApple OSS Distributions 	int test_num = 5;
269*d4514f0bSApple OSS Distributions 	mach_exception_data_type_t expected_exception_code = kGUARD_EXC_EXCEPTION_BEHAVIOR_ENFORCE;
270*d4514f0bSApple OSS Distributions 	bool triggers_exception = true;
271*d4514f0bSApple OSS Distributions 
272*d4514f0bSApple OSS Distributions #if TARGET_OS_OSX
273*d4514f0bSApple OSS Distributions 	T_SKIP("Test disabled on macOS due to SIP disabled and AMFI boot args usage on BATS");
274*d4514f0bSApple OSS Distributions 	/*
275*d4514f0bSApple OSS Distributions 	 * CS_CONFIG_GET_OUT_OF_MY_WAY (enabled via AMFI boot-args)
276*d4514f0bSApple OSS Distributions 	 * disables this security feature. This boot-arg previously
277*d4514f0bSApple OSS Distributions 	 * caused a headache for developers on macos, who frequently use it for
278*d4514f0bSApple OSS Distributions 	 * testing purposes, because all of their 3rd party apps will
279*d4514f0bSApple OSS Distributions 	 * crash due to being treated as platform code. Unfortunately
280*d4514f0bSApple OSS Distributions 	 * BATS runs with this boot-arg enabled.
281*d4514f0bSApple OSS Distributions 	 */
282*d4514f0bSApple OSS Distributions 	code_signing_config_t cs_config = 0;
283*d4514f0bSApple OSS Distributions 	size_t cs_config_size = sizeof(cs_config);
284*d4514f0bSApple OSS Distributions 	sysctlbyname("security.codesigning.config", &cs_config, &cs_config_size, NULL, 0);
285*d4514f0bSApple OSS Distributions 	if (cs_config & CS_CONFIG_GET_OUT_OF_MY_WAY) {
286*d4514f0bSApple OSS Distributions 		expected_exception_code = 0;
287*d4514f0bSApple OSS Distributions 		triggers_exception = false;
288*d4514f0bSApple OSS Distributions 		T_LOG("task identity security policy for thread_set_exception_ports"
289*d4514f0bSApple OSS Distributions 		    " disabled due to AMFI boot-args.");
290*d4514f0bSApple OSS Distributions 	} else
291*d4514f0bSApple OSS Distributions #endif /* TARGET_OS_OSX */
292*d4514f0bSApple OSS Distributions 	{
293*d4514f0bSApple OSS Distributions 		T_LOG("task identity security policy for thread_set_exception_ports enabled");
294*d4514f0bSApple OSS Distributions 	}
295*d4514f0bSApple OSS Distributions 
296*d4514f0bSApple OSS Distributions 	reply_port_defense(true, test_num, expected_exception_code, triggers_exception);
297*d4514f0bSApple OSS Distributions 	reply_port_defense(false, test_num, expected_exception_code, triggers_exception);
298*d4514f0bSApple OSS Distributions }
299*d4514f0bSApple OSS Distributions 
300*d4514f0bSApple OSS Distributions T_DECL(test_unentitled_thread_set_state,
301*d4514f0bSApple OSS Distributions     "thread_set_state should fail without an entitlement",
302*d4514f0bSApple OSS Distributions     T_META_IGNORECRASHES(".*reply_port_defense_client.*"),
303*d4514f0bSApple OSS Distributions     T_META_CHECK_LEAKS(false),
304*d4514f0bSApple OSS Distributions     T_META_ENABLED(false /* rdar://133955889 */))
305*d4514f0bSApple OSS Distributions {
306*d4514f0bSApple OSS Distributions 	int test_num = 6;
307*d4514f0bSApple OSS Distributions 	mach_exception_data_type_t expected_exception_code = (mach_exception_data_type_t)kGUARD_EXC_THREAD_SET_STATE;
308*d4514f0bSApple OSS Distributions 	bool triggers_exception = true;
309*d4514f0bSApple OSS Distributions 
310*d4514f0bSApple OSS Distributions #if TARGET_OS_OSX
311*d4514f0bSApple OSS Distributions 	T_SKIP("Test disabled on macOS due to mach hardening opt out");
312*d4514f0bSApple OSS Distributions #endif /* TARGET_OS_OSX */
313*d4514f0bSApple OSS Distributions 
314*d4514f0bSApple OSS Distributions 	reply_port_defense(true, test_num, expected_exception_code, triggers_exception);
315*d4514f0bSApple OSS Distributions 	reply_port_defense(false, test_num, expected_exception_code, triggers_exception);
316*d4514f0bSApple OSS Distributions }
317*d4514f0bSApple OSS Distributions 
318*d4514f0bSApple OSS Distributions T_DECL(unentitled_set_exception_ports_pass,
319*d4514f0bSApple OSS Distributions     "set_exception_ports should succeed",
320*d4514f0bSApple OSS Distributions     T_META_IGNORECRASHES(".*reply_port_defense_client.*"),
321*d4514f0bSApple OSS Distributions     T_META_CHECK_LEAKS(false)) {
322*d4514f0bSApple OSS Distributions 	int test_num = 7;
323*d4514f0bSApple OSS Distributions 	mach_exception_data_type_t expected_exception_code = (mach_exception_data_type_t)0;
324*d4514f0bSApple OSS Distributions 	bool triggers_exception = false;
325*d4514f0bSApple OSS Distributions 	reply_port_defense(true, test_num, expected_exception_code, triggers_exception);
326*d4514f0bSApple OSS Distributions 	reply_port_defense(false, test_num, expected_exception_code, triggers_exception);
327*d4514f0bSApple OSS Distributions }
328*d4514f0bSApple OSS Distributions 
329*d4514f0bSApple OSS Distributions 
330*d4514f0bSApple OSS Distributions T_DECL(kobject_reply_port_defense,
331*d4514f0bSApple OSS Distributions     "sending messages to kobjects without a proper reply port should crash",
332*d4514f0bSApple OSS Distributions     T_META_IGNORECRASHES(".*reply_port_defense_client.*"),
333*d4514f0bSApple OSS Distributions     T_META_CHECK_LEAKS(false),
334*d4514f0bSApple OSS Distributions     T_META_ENABLED(!TARGET_OS_OSX)) {     /* disable on macOS due to BATS boot-args */
335*d4514f0bSApple OSS Distributions 	int test_num = 9;
336*d4514f0bSApple OSS Distributions #if __x86_64__
337*d4514f0bSApple OSS Distributions 	mach_exception_data_type_t expected_exception_code = (mach_exception_data_type_t)kGUARD_EXC_REQUIRE_REPLY_PORT_SEMANTICS;
338*d4514f0bSApple OSS Distributions #else
339*d4514f0bSApple OSS Distributions 	mach_exception_data_type_t expected_exception_code = (mach_exception_data_type_t)kGUARD_EXC_SEND_INVALID_REPLY;
340*d4514f0bSApple OSS Distributions #endif
341*d4514f0bSApple OSS Distributions 	bool triggers_exception = true;
342*d4514f0bSApple OSS Distributions 	reply_port_defense(true, test_num, expected_exception_code, triggers_exception);
343*d4514f0bSApple OSS Distributions 	reply_port_defense(false, test_num, expected_exception_code, triggers_exception);
344*d4514f0bSApple OSS Distributions }
345