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