1*43a90889SApple OSS Distributions #include <stdio.h>
2*43a90889SApple OSS Distributions #include <stdlib.h>
3*43a90889SApple OSS Distributions #include <unistd.h>
4*43a90889SApple OSS Distributions #include <darwintest.h>
5*43a90889SApple OSS Distributions #include <mach/mach.h>
6*43a90889SApple OSS Distributions #include <mach/mach_vm.h>
7*43a90889SApple OSS Distributions #include <sys/sysctl.h>
8*43a90889SApple OSS Distributions #include <spawn.h>
9*43a90889SApple OSS Distributions #include <signal.h>
10*43a90889SApple OSS Distributions #include <TargetConditionals.h>
11*43a90889SApple OSS Distributions #include "excserver_protect.h"
12*43a90889SApple OSS Distributions
13*43a90889SApple OSS Distributions #define MAX_ARGV 3
14*43a90889SApple OSS Distributions #define EXC_CODE_SHIFT 32
15*43a90889SApple OSS Distributions #define EXC_GUARD_TYPE_SHIFT 29
16*43a90889SApple OSS Distributions #define MAX_TEST_NUM 21
17*43a90889SApple OSS Distributions
18*43a90889SApple OSS Distributions #define TASK_EXC_GUARD_MP_DELIVER 0x10
19*43a90889SApple OSS Distributions
20*43a90889SApple OSS Distributions extern char **environ;
21*43a90889SApple OSS Distributions static uint64_t exception_code = 0;
22*43a90889SApple OSS Distributions static exception_type_t exception_taken = 0;
23*43a90889SApple OSS Distributions
24*43a90889SApple OSS Distributions #define IKOT_TASK_CONTROL 2
25*43a90889SApple OSS Distributions
26*43a90889SApple OSS Distributions #ifndef kGUARD_EXC_INVALID_OPTIONS
27*43a90889SApple OSS Distributions #define kGUARD_EXC_INVALID_OPTIONS 3
28*43a90889SApple OSS Distributions #endif
29*43a90889SApple OSS Distributions
30*43a90889SApple OSS Distributions /*
31*43a90889SApple OSS Distributions * This test verifies behaviors of immovable/pinned task/thread ports.
32*43a90889SApple OSS Distributions *
33*43a90889SApple OSS Distributions * 1. Compare and verifies port names of mach_{task, thread}_self(),
34*43a90889SApple OSS Distributions * {TASK, THREAD}_KERNEL_PORT, and ports returned from task_threads()
35*43a90889SApple OSS Distributions * and processor_set_tasks().
36*43a90889SApple OSS Distributions * 2. Make sure correct exceptions are raised resulting from moving immovable
37*43a90889SApple OSS Distributions * task/thread control, read and inspect ports.
38*43a90889SApple OSS Distributions * 3. Make sure correct exceptions are raised resulting from deallocating pinned
39*43a90889SApple OSS Distributions * task/thread control ports.
40*43a90889SApple OSS Distributions * 4. Make sure immovable ports cannot be stashed:
41*43a90889SApple OSS Distributions * rdar://70585367 (Disallow immovable port stashing with *_set_special_port() and mach_port_register())
42*43a90889SApple OSS Distributions */
43*43a90889SApple OSS Distributions T_GLOBAL_META(
44*43a90889SApple OSS Distributions T_META_NAMESPACE("xnu.ipc"),
45*43a90889SApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"),
46*43a90889SApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("IPC"),
47*43a90889SApple OSS Distributions T_META_RUN_CONCURRENTLY(TRUE));
48*43a90889SApple OSS Distributions
49*43a90889SApple OSS Distributions static uint64_t soft_exception_code[] = {
50*43a90889SApple OSS Distributions EXC_GUARD, // Soft crash delivered as EXC_CORPSE_NOTIFY
51*43a90889SApple OSS Distributions EXC_GUARD,
52*43a90889SApple OSS Distributions EXC_GUARD,
53*43a90889SApple OSS Distributions EXC_GUARD,
54*43a90889SApple OSS Distributions EXC_GUARD,
55*43a90889SApple OSS Distributions EXC_GUARD,
56*43a90889SApple OSS Distributions EXC_GUARD,
57*43a90889SApple OSS Distributions
58*43a90889SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_IMMOVABLE,
59*43a90889SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_IMMOVABLE,
60*43a90889SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_IMMOVABLE,
61*43a90889SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_IMMOVABLE,
62*43a90889SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_IMMOVABLE,
63*43a90889SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_IMMOVABLE,
64*43a90889SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_IMMOVABLE,
65*43a90889SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_IMMOVABLE,
66*43a90889SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_IMMOVABLE,
67*43a90889SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_IMMOVABLE,
68*43a90889SApple OSS Distributions
69*43a90889SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_INVALID_OPTIONS,
70*43a90889SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_INVALID_OPTIONS,
71*43a90889SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_INVALID_OPTIONS,
72*43a90889SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_INVALID_OPTIONS,
73*43a90889SApple OSS Distributions };
74*43a90889SApple OSS Distributions
75*43a90889SApple OSS Distributions static uint64_t hard_exception_code[] = {
76*43a90889SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_MOD_REFS,
77*43a90889SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_MOD_REFS,
78*43a90889SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_MOD_REFS,
79*43a90889SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_MOD_REFS,
80*43a90889SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_MOD_REFS,
81*43a90889SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_MOD_REFS,
82*43a90889SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_MOD_REFS,
83*43a90889SApple OSS Distributions
84*43a90889SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_IMMOVABLE,
85*43a90889SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_IMMOVABLE,
86*43a90889SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_IMMOVABLE,
87*43a90889SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_IMMOVABLE,
88*43a90889SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_IMMOVABLE,
89*43a90889SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_IMMOVABLE,
90*43a90889SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_IMMOVABLE,
91*43a90889SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_IMMOVABLE,
92*43a90889SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_IMMOVABLE,
93*43a90889SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_IMMOVABLE,
94*43a90889SApple OSS Distributions
95*43a90889SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_INVALID_OPTIONS,
96*43a90889SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_INVALID_OPTIONS,
97*43a90889SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_INVALID_OPTIONS,
98*43a90889SApple OSS Distributions (GUARD_TYPE_MACH_PORT << EXC_GUARD_TYPE_SHIFT) | kGUARD_EXC_INVALID_OPTIONS,
99*43a90889SApple OSS Distributions };
100*43a90889SApple OSS Distributions
101*43a90889SApple 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)102*43a90889SApple OSS Distributions catch_mach_exception_raise_state(mach_port_t exception_port,
103*43a90889SApple OSS Distributions exception_type_t exception,
104*43a90889SApple OSS Distributions const mach_exception_data_t code,
105*43a90889SApple OSS Distributions mach_msg_type_number_t code_count,
106*43a90889SApple OSS Distributions int * flavor,
107*43a90889SApple OSS Distributions const thread_state_t old_state,
108*43a90889SApple OSS Distributions mach_msg_type_number_t old_state_count,
109*43a90889SApple OSS Distributions thread_state_t new_state,
110*43a90889SApple OSS Distributions mach_msg_type_number_t * new_state_count)
111*43a90889SApple OSS Distributions {
112*43a90889SApple OSS Distributions #pragma unused(exception_port, exception, code, code_count, flavor, old_state, old_state_count, new_state, new_state_count)
113*43a90889SApple OSS Distributions T_FAIL("Unsupported catch_mach_exception_raise_state");
114*43a90889SApple OSS Distributions return KERN_NOT_SUPPORTED;
115*43a90889SApple OSS Distributions }
116*43a90889SApple OSS Distributions
117*43a90889SApple OSS Distributions kern_return_t
catch_mach_exception_raise_identity_protected(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)118*43a90889SApple OSS Distributions catch_mach_exception_raise_identity_protected(
119*43a90889SApple OSS Distributions mach_port_t exception_port,
120*43a90889SApple OSS Distributions uint64_t thread_id,
121*43a90889SApple OSS Distributions mach_port_t task_id_token,
122*43a90889SApple OSS Distributions exception_type_t exception,
123*43a90889SApple OSS Distributions mach_exception_data_t codes,
124*43a90889SApple OSS Distributions mach_msg_type_number_t codeCnt)
125*43a90889SApple OSS Distributions {
126*43a90889SApple OSS Distributions #pragma unused(exception_port, codeCnt)
127*43a90889SApple OSS Distributions task_t task;
128*43a90889SApple OSS Distributions pid_t pid;
129*43a90889SApple OSS Distributions kern_return_t kr = task_identity_token_get_task_port(task_id_token, TASK_FLAVOR_READ, &task);
130*43a90889SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "task_identity_token_get_task_port");
131*43a90889SApple OSS Distributions kr = pid_for_task(task, &pid);
132*43a90889SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "pid_for_task");
133*43a90889SApple OSS Distributions T_LOG("Crashing child pid: %d, continuing...\n", pid);
134*43a90889SApple OSS Distributions
135*43a90889SApple OSS Distributions kr = mach_port_deallocate(mach_task_self(), task);
136*43a90889SApple OSS Distributions T_QUIET; T_EXPECT_MACH_SUCCESS(kr, "mach_port_deallocate");
137*43a90889SApple OSS Distributions
138*43a90889SApple OSS Distributions T_ASSERT_GT_UINT(codeCnt, 0, "CodeCnt");
139*43a90889SApple OSS Distributions T_LOG("Caught exception type: %d code: 0x%llx", exception, (uint64_t)codes[0]);
140*43a90889SApple OSS Distributions if (exception == EXC_GUARD || exception == EXC_CORPSE_NOTIFY) {
141*43a90889SApple OSS Distributions exception_taken = exception;
142*43a90889SApple OSS Distributions exception_code = (uint64_t)codes[0];
143*43a90889SApple OSS Distributions } else {
144*43a90889SApple OSS Distributions T_FAIL("Unexpected exception");
145*43a90889SApple OSS Distributions }
146*43a90889SApple OSS Distributions return KERN_SUCCESS;
147*43a90889SApple OSS Distributions }
148*43a90889SApple OSS Distributions
149*43a90889SApple 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)150*43a90889SApple OSS Distributions catch_mach_exception_raise_state_identity(mach_port_t exception_port,
151*43a90889SApple OSS Distributions mach_port_t thread,
152*43a90889SApple OSS Distributions mach_port_t task,
153*43a90889SApple OSS Distributions exception_type_t exception,
154*43a90889SApple OSS Distributions mach_exception_data_t code,
155*43a90889SApple OSS Distributions mach_msg_type_number_t code_count,
156*43a90889SApple OSS Distributions int * flavor,
157*43a90889SApple OSS Distributions thread_state_t old_state,
158*43a90889SApple OSS Distributions mach_msg_type_number_t old_state_count,
159*43a90889SApple OSS Distributions thread_state_t new_state,
160*43a90889SApple OSS Distributions mach_msg_type_number_t * new_state_count)
161*43a90889SApple OSS Distributions {
162*43a90889SApple OSS Distributions #pragma unused(exception_port, thread, task, exception, code, code_count, flavor, old_state, old_state_count, new_state, new_state_count)
163*43a90889SApple OSS Distributions T_FAIL("Unsupported catch_mach_exception_raise_state_identity");
164*43a90889SApple OSS Distributions return KERN_NOT_SUPPORTED;
165*43a90889SApple OSS Distributions }
166*43a90889SApple OSS Distributions
167*43a90889SApple 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)168*43a90889SApple OSS Distributions catch_mach_exception_raise(mach_port_t exception_port,
169*43a90889SApple OSS Distributions mach_port_t thread,
170*43a90889SApple OSS Distributions mach_port_t task,
171*43a90889SApple OSS Distributions exception_type_t exception,
172*43a90889SApple OSS Distributions mach_exception_data_t code,
173*43a90889SApple OSS Distributions mach_msg_type_number_t code_count)
174*43a90889SApple OSS Distributions {
175*43a90889SApple OSS Distributions #pragma unused(exception_port, thread, task, exception, code, code_count, flavor, old_state, old_state_count, new_state, new_state_count)
176*43a90889SApple OSS Distributions T_FAIL("Unsupported catch_mach_exception_raise_state_identity");
177*43a90889SApple OSS Distributions return KERN_NOT_SUPPORTED;
178*43a90889SApple OSS Distributions }
179*43a90889SApple OSS Distributions
180*43a90889SApple OSS Distributions static void *
exception_server_thread(void * arg)181*43a90889SApple OSS Distributions exception_server_thread(void *arg)
182*43a90889SApple OSS Distributions {
183*43a90889SApple OSS Distributions kern_return_t kr;
184*43a90889SApple OSS Distributions mach_port_t exc_port = *(mach_port_t *)arg;
185*43a90889SApple OSS Distributions
186*43a90889SApple OSS Distributions /* Handle exceptions on exc_port */
187*43a90889SApple OSS Distributions kr = mach_msg_server_once(mach_exc_server, 4096, exc_port, 0);
188*43a90889SApple OSS Distributions T_QUIET; T_EXPECT_MACH_SUCCESS(kr, "mach_msg_server_once");
189*43a90889SApple OSS Distributions
190*43a90889SApple OSS Distributions return NULL;
191*43a90889SApple OSS Distributions }
192*43a90889SApple OSS Distributions
193*43a90889SApple OSS Distributions static mach_port_t
alloc_exception_port(void)194*43a90889SApple OSS Distributions alloc_exception_port(void)
195*43a90889SApple OSS Distributions {
196*43a90889SApple OSS Distributions kern_return_t kret;
197*43a90889SApple OSS Distributions mach_port_t exc_port = MACH_PORT_NULL;
198*43a90889SApple OSS Distributions mach_port_t task = mach_task_self();
199*43a90889SApple OSS Distributions
200*43a90889SApple OSS Distributions kret = mach_port_allocate(task, MACH_PORT_RIGHT_RECEIVE, &exc_port);
201*43a90889SApple OSS Distributions T_QUIET; T_EXPECT_MACH_SUCCESS(kret, "mach_port_allocate exc_port");
202*43a90889SApple OSS Distributions
203*43a90889SApple OSS Distributions kret = mach_port_insert_right(task, exc_port, exc_port, MACH_MSG_TYPE_MAKE_SEND);
204*43a90889SApple OSS Distributions T_QUIET; T_EXPECT_MACH_SUCCESS(kret, "mach_port_insert_right exc_port");
205*43a90889SApple OSS Distributions
206*43a90889SApple OSS Distributions return exc_port;
207*43a90889SApple OSS Distributions }
208*43a90889SApple OSS Distributions
209*43a90889SApple OSS Distributions static void
test_immovable_port_stashing(void)210*43a90889SApple OSS Distributions test_immovable_port_stashing(void)
211*43a90889SApple OSS Distributions {
212*43a90889SApple OSS Distributions kern_return_t kr;
213*43a90889SApple OSS Distributions mach_port_t port;
214*43a90889SApple OSS Distributions
215*43a90889SApple OSS Distributions kr = task_set_special_port(mach_task_self(), TASK_BOOTSTRAP_PORT, mach_task_self());
216*43a90889SApple OSS Distributions T_EXPECT_EQ(kr, KERN_INVALID_RIGHT, "should disallow task_set_special_port() with immovable port");
217*43a90889SApple OSS Distributions
218*43a90889SApple OSS Distributions kr = thread_set_special_port(mach_thread_self(), THREAD_KERNEL_PORT, mach_thread_self());
219*43a90889SApple OSS Distributions T_EXPECT_EQ(kr, KERN_INVALID_RIGHT, "should disallow task_set_special_port() with immovable port");
220*43a90889SApple OSS Distributions
221*43a90889SApple OSS Distributions mach_port_t stash[1] = {mach_task_self()};
222*43a90889SApple OSS Distributions kr = mach_ports_register(mach_task_self(), stash, 1);
223*43a90889SApple OSS Distributions T_EXPECT_EQ(kr, KERN_INVALID_RIGHT, "should disallow mach_ports_register() with immovable port");
224*43a90889SApple OSS Distributions
225*43a90889SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &port), "mach_port_allocate");
226*43a90889SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(mach_port_insert_right(mach_task_self(), port, port, MACH_MSG_TYPE_MAKE_SEND), "mach_port_insert_right");
227*43a90889SApple OSS Distributions
228*43a90889SApple OSS Distributions stash[0] = port;
229*43a90889SApple OSS Distributions kr = mach_ports_register(mach_task_self(), stash, 1);
230*43a90889SApple OSS Distributions T_EXPECT_MACH_SUCCESS(kr, "mach_ports_register() should succeed with movable port");
231*43a90889SApple OSS Distributions }
232*43a90889SApple OSS Distributions
233*43a90889SApple OSS Distributions static void
test_task_thread_port_values(void)234*43a90889SApple OSS Distributions test_task_thread_port_values(void)
235*43a90889SApple OSS Distributions {
236*43a90889SApple OSS Distributions T_LOG("Compare various task/thread control port values\n");
237*43a90889SApple OSS Distributions kern_return_t kr;
238*43a90889SApple OSS Distributions mach_port_t port, th_self;
239*43a90889SApple OSS Distributions thread_array_t threadList;
240*43a90889SApple OSS Distributions mach_msg_type_number_t threadCount = 0;
241*43a90889SApple OSS Distributions boolean_t found_self = false;
242*43a90889SApple OSS Distributions processor_set_name_array_t psets;
243*43a90889SApple OSS Distributions processor_set_t pset_priv;
244*43a90889SApple OSS Distributions task_array_t taskList;
245*43a90889SApple OSS Distributions mach_msg_type_number_t pcnt = 0, tcnt = 0;
246*43a90889SApple OSS Distributions mach_port_t host = mach_host_self();
247*43a90889SApple OSS Distributions
248*43a90889SApple OSS Distributions /* Compare with task/thread_get_special_port() */
249*43a90889SApple OSS Distributions kr = task_get_special_port(mach_task_self(), TASK_KERNEL_PORT, &port);
250*43a90889SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "task_get_special_port() - TASK_KERNEL_PORT");
251*43a90889SApple OSS Distributions T_EXPECT_NE(port, mach_task_self(), "TASK_KERNEL_PORT should not match mach_task_self()");
252*43a90889SApple OSS Distributions mach_port_deallocate(mach_task_self(), port);
253*43a90889SApple OSS Distributions
254*43a90889SApple OSS Distributions kr = task_for_pid(mach_task_self(), getpid(), &port);
255*43a90889SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "task_for_pid()");
256*43a90889SApple OSS Distributions T_EXPECT_EQ(port, mach_task_self(), "task_for_pid(self) should match mach_task_self()");
257*43a90889SApple OSS Distributions mach_port_deallocate(mach_task_self(), port);
258*43a90889SApple OSS Distributions
259*43a90889SApple OSS Distributions th_self = mach_thread_self();
260*43a90889SApple OSS Distributions kr = thread_get_special_port(th_self, THREAD_KERNEL_PORT, &port);
261*43a90889SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "thread_get_special_port() - THREAD_KERNEL_PORT");
262*43a90889SApple OSS Distributions T_EXPECT_NE(port, th_self, "THREAD_KERNEL_PORT should not match mach_thread_self()");
263*43a90889SApple OSS Distributions mach_port_deallocate(mach_task_self(), port);
264*43a90889SApple OSS Distributions
265*43a90889SApple OSS Distributions /* Make sure task_threads() return immovable thread ports */
266*43a90889SApple OSS Distributions kr = task_threads(mach_task_self(), &threadList, &threadCount);
267*43a90889SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "task_threads()");
268*43a90889SApple OSS Distributions T_QUIET; T_ASSERT_GE(threadCount, 1, "should have at least 1 thread");
269*43a90889SApple OSS Distributions
270*43a90889SApple OSS Distributions for (size_t i = 0; i < threadCount; i++) {
271*43a90889SApple OSS Distributions if (th_self == threadList[i]) { /* th_self is immovable */
272*43a90889SApple OSS Distributions found_self = true;
273*43a90889SApple OSS Distributions break;
274*43a90889SApple OSS Distributions }
275*43a90889SApple OSS Distributions }
276*43a90889SApple OSS Distributions
277*43a90889SApple OSS Distributions T_EXPECT_TRUE(found_self, "task_threads() should return immovable thread self");
278*43a90889SApple OSS Distributions
279*43a90889SApple OSS Distributions for (size_t i = 0; i < threadCount; i++) {
280*43a90889SApple OSS Distributions mach_port_deallocate(mach_task_self(), threadList[i]);
281*43a90889SApple OSS Distributions }
282*43a90889SApple OSS Distributions
283*43a90889SApple OSS Distributions if (threadCount > 0) {
284*43a90889SApple OSS Distributions mach_vm_deallocate(mach_task_self(),
285*43a90889SApple OSS Distributions (mach_vm_address_t)threadList,
286*43a90889SApple OSS Distributions threadCount * sizeof(mach_port_t));
287*43a90889SApple OSS Distributions }
288*43a90889SApple OSS Distributions
289*43a90889SApple OSS Distributions mach_port_deallocate(mach_task_self(), th_self);
290*43a90889SApple OSS Distributions
291*43a90889SApple OSS Distributions /* Make sure processor_set_tasks() return immovable task self */
292*43a90889SApple OSS Distributions kr = host_processor_sets(host, &psets, &pcnt);
293*43a90889SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "host_processor_sets");
294*43a90889SApple OSS Distributions T_QUIET; T_ASSERT_GE(pcnt, 1, "should have at least 1 processor set");
295*43a90889SApple OSS Distributions
296*43a90889SApple OSS Distributions kr = host_processor_set_priv(host, psets[0], &pset_priv);
297*43a90889SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "host_processor_set_priv");
298*43a90889SApple OSS Distributions for (size_t i = 0; i < pcnt; i++) {
299*43a90889SApple OSS Distributions mach_port_deallocate(mach_task_self(), psets[i]);
300*43a90889SApple OSS Distributions }
301*43a90889SApple OSS Distributions mach_port_deallocate(mach_task_self(), host);
302*43a90889SApple OSS Distributions vm_deallocate(mach_task_self(), (vm_address_t)psets, (vm_size_t)pcnt * sizeof(mach_port_t));
303*43a90889SApple OSS Distributions
304*43a90889SApple OSS Distributions kr = processor_set_tasks_with_flavor(pset_priv, TASK_FLAVOR_CONTROL, &taskList, &tcnt);
305*43a90889SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "processor_set_tasks_with_flavor");
306*43a90889SApple OSS Distributions T_QUIET; T_ASSERT_GE(tcnt, 1, "should have at least 1 task");
307*43a90889SApple OSS Distributions mach_port_deallocate(mach_task_self(), pset_priv);
308*43a90889SApple OSS Distributions
309*43a90889SApple OSS Distributions found_self = false;
310*43a90889SApple OSS Distributions for (size_t i = 0; i < tcnt; i++) {
311*43a90889SApple OSS Distributions if (taskList[i] == mach_task_self()) {
312*43a90889SApple OSS Distributions found_self = true;
313*43a90889SApple OSS Distributions break;
314*43a90889SApple OSS Distributions }
315*43a90889SApple OSS Distributions }
316*43a90889SApple OSS Distributions
317*43a90889SApple OSS Distributions T_EXPECT_TRUE(found_self, " processor_set_tasks() should return immovable task self");
318*43a90889SApple OSS Distributions
319*43a90889SApple OSS Distributions for (size_t i = 0; i < tcnt; i++) {
320*43a90889SApple OSS Distributions mach_port_deallocate(mach_task_self(), taskList[i]);
321*43a90889SApple OSS Distributions }
322*43a90889SApple OSS Distributions
323*43a90889SApple OSS Distributions if (tcnt > 0) {
324*43a90889SApple OSS Distributions mach_vm_deallocate(mach_task_self(),
325*43a90889SApple OSS Distributions (mach_vm_address_t)taskList,
326*43a90889SApple OSS Distributions tcnt * sizeof(mach_port_t));
327*43a90889SApple OSS Distributions }
328*43a90889SApple OSS Distributions }
329*43a90889SApple OSS Distributions
330*43a90889SApple OSS Distributions static void
test_imm_pinned_control_port(const char * test_prog_name)331*43a90889SApple OSS Distributions test_imm_pinned_control_port(const char *test_prog_name)
332*43a90889SApple OSS Distributions {
333*43a90889SApple OSS Distributions uint32_t task_exc_guard = 0;
334*43a90889SApple OSS Distributions size_t te_size = sizeof(&task_exc_guard);
335*43a90889SApple OSS Distributions posix_spawnattr_t attrs;
336*43a90889SApple OSS Distributions char *child_args[MAX_ARGV];
337*43a90889SApple OSS Distributions pid_t client_pid = 0;
338*43a90889SApple OSS Distributions uint32_t opts = 0;
339*43a90889SApple OSS Distributions uint64_t *test_exception_code;
340*43a90889SApple OSS Distributions size_t size = sizeof(&opts);
341*43a90889SApple OSS Distributions mach_port_t exc_port;
342*43a90889SApple OSS Distributions pthread_t s_exc_thread;
343*43a90889SApple OSS Distributions uint64_t exc_id;
344*43a90889SApple OSS Distributions
345*43a90889SApple OSS Distributions T_LOG("Check if task_exc_guard exception has been enabled\n");
346*43a90889SApple OSS Distributions int ret = sysctlbyname("kern.task_exc_guard_default", &task_exc_guard, &te_size, NULL, 0);
347*43a90889SApple OSS Distributions T_ASSERT_EQ(ret, 0, "sysctlbyname");
348*43a90889SApple OSS Distributions
349*43a90889SApple OSS Distributions if (!(task_exc_guard & TASK_EXC_GUARD_MP_DELIVER)) {
350*43a90889SApple OSS Distributions T_SKIP("task_exc_guard exception is not enabled");
351*43a90889SApple OSS Distributions }
352*43a90889SApple OSS Distributions
353*43a90889SApple OSS Distributions T_LOG("Check if immovable control port has been enabled\n");
354*43a90889SApple OSS Distributions ret = sysctlbyname("kern.ipc_control_port_options", &opts, &size, NULL, 0);
355*43a90889SApple OSS Distributions
356*43a90889SApple OSS Distributions if (!ret && (opts & 0x8) != 0x8) {
357*43a90889SApple OSS Distributions T_SKIP("hard immovable control port isn't enabled");
358*43a90889SApple OSS Distributions }
359*43a90889SApple OSS Distributions
360*43a90889SApple OSS Distributions if (!ret && (opts & 0x2)) {
361*43a90889SApple OSS Distributions T_LOG("Hard pinning enforcement is on.");
362*43a90889SApple OSS Distributions test_exception_code = hard_exception_code;
363*43a90889SApple OSS Distributions } else {
364*43a90889SApple OSS Distributions T_LOG("Hard pinning enforcement is off.");
365*43a90889SApple OSS Distributions test_exception_code = soft_exception_code;
366*43a90889SApple OSS Distributions }
367*43a90889SApple OSS Distributions
368*43a90889SApple OSS Distributions
369*43a90889SApple OSS Distributions /* first, try out comparing various task/thread ports */
370*43a90889SApple OSS Distributions test_task_thread_port_values();
371*43a90889SApple OSS Distributions
372*43a90889SApple OSS Distributions /* try stashing immovable ports: rdar://70585367 */
373*43a90889SApple OSS Distributions test_immovable_port_stashing();
374*43a90889SApple OSS Distributions
375*43a90889SApple OSS Distributions /* spawn a child and see if EXC_GUARD are correctly generated */
376*43a90889SApple OSS Distributions for (int i = 0; i < MAX_TEST_NUM; i++) {
377*43a90889SApple OSS Distributions /* Create the exception port for the child */
378*43a90889SApple OSS Distributions exc_port = alloc_exception_port();
379*43a90889SApple OSS Distributions T_QUIET; T_ASSERT_NE(exc_port, MACH_PORT_NULL, "Create a new exception port");
380*43a90889SApple OSS Distributions
381*43a90889SApple OSS Distributions /* Create exception serving thread */
382*43a90889SApple OSS Distributions ret = pthread_create(&s_exc_thread, NULL, exception_server_thread, &exc_port);
383*43a90889SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "pthread_create exception_server_thread");
384*43a90889SApple OSS Distributions
385*43a90889SApple OSS Distributions /* Initialize posix_spawn attributes */
386*43a90889SApple OSS Distributions posix_spawnattr_init(&attrs);
387*43a90889SApple OSS Distributions
388*43a90889SApple OSS Distributions int err = posix_spawnattr_setexceptionports_np(&attrs, EXC_MASK_GUARD | EXC_MASK_CORPSE_NOTIFY, exc_port,
389*43a90889SApple OSS Distributions (exception_behavior_t) (EXCEPTION_IDENTITY_PROTECTED | MACH_EXCEPTION_CODES), 0);
390*43a90889SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(err, "posix_spawnattr_setflags");
391*43a90889SApple OSS Distributions
392*43a90889SApple OSS Distributions child_args[0] = test_prog_name;
393*43a90889SApple OSS Distributions char test_num[10];
394*43a90889SApple OSS Distributions sprintf(test_num, "%d", i);
395*43a90889SApple OSS Distributions child_args[1] = test_num;
396*43a90889SApple OSS Distributions child_args[2] = NULL;
397*43a90889SApple OSS Distributions
398*43a90889SApple OSS Distributions T_LOG("========== Spawning new child ==========");
399*43a90889SApple OSS Distributions err = posix_spawn(&client_pid, child_args[0], NULL, &attrs, &child_args[0], environ);
400*43a90889SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(err, "posix_spawn control_port_options_client = %d test_num = %d", client_pid, i);
401*43a90889SApple OSS Distributions
402*43a90889SApple OSS Distributions /* try extracting child task port: rdar://71744817
403*43a90889SApple OSS Distributions * Moved to tests/extract_right_soft_fail.c
404*43a90889SApple OSS Distributions */
405*43a90889SApple OSS Distributions // test_extract_immovable_task_port(client_pid);
406*43a90889SApple OSS Distributions
407*43a90889SApple OSS Distributions int child_status;
408*43a90889SApple OSS Distributions /* Wait for child and check for exception */
409*43a90889SApple OSS Distributions if (-1 == waitpid(-1, &child_status, 0)) {
410*43a90889SApple OSS Distributions T_FAIL("waitpid: child mia");
411*43a90889SApple OSS Distributions }
412*43a90889SApple OSS Distributions
413*43a90889SApple OSS Distributions if (WIFEXITED(child_status)) {
414*43a90889SApple OSS Distributions if (WEXITSTATUS(child_status)) {
415*43a90889SApple OSS Distributions T_FAIL("Child exited with status = %x", child_status); T_END;
416*43a90889SApple OSS Distributions } else {
417*43a90889SApple OSS Distributions /* Skipping test because CS_OPS_CLEARPLATFORM is not supported in the current BATS container */
418*43a90889SApple OSS Distributions T_SKIP("Failed to remove platform binary");
419*43a90889SApple OSS Distributions }
420*43a90889SApple OSS Distributions }
421*43a90889SApple OSS Distributions
422*43a90889SApple OSS Distributions sleep(1);
423*43a90889SApple OSS Distributions kill(1, SIGKILL);
424*43a90889SApple OSS Distributions
425*43a90889SApple OSS Distributions ret = pthread_join(s_exc_thread, NULL);
426*43a90889SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "pthread_join");
427*43a90889SApple OSS Distributions
428*43a90889SApple OSS Distributions if (exception_taken == EXC_GUARD) {
429*43a90889SApple OSS Distributions exc_id = exception_code >> EXC_CODE_SHIFT;
430*43a90889SApple OSS Distributions } else {
431*43a90889SApple OSS Distributions exc_id = exception_code;
432*43a90889SApple OSS Distributions }
433*43a90889SApple OSS Distributions
434*43a90889SApple OSS Distributions T_LOG("Exception code: Received code = 0x%llx Expected code = 0x%llx", exc_id, test_exception_code[i]);
435*43a90889SApple OSS Distributions T_EXPECT_EQ(exc_id, test_exception_code[i], "Exception code: Received == Expected");
436*43a90889SApple OSS Distributions }
437*43a90889SApple OSS Distributions }
438*43a90889SApple OSS Distributions
439*43a90889SApple OSS Distributions T_DECL(imm_pinned_control_port_hardened, "Test pinned & immovable task and thread control ports for hardened runtime binary",
440*43a90889SApple OSS Distributions T_META_IGNORECRASHES(".*pinned_rights_child.*"),
441*43a90889SApple OSS Distributions T_META_CHECK_LEAKS(false))
442*43a90889SApple OSS Distributions {
443*43a90889SApple OSS Distributions test_imm_pinned_control_port("imm_pinned_control_port_crasher_3P_hardened");
444*43a90889SApple OSS Distributions }
445*43a90889SApple OSS Distributions
446*43a90889SApple OSS Distributions T_DECL(imm_pinned_control_port, "Test pinned & immovable task and thread control ports for first party binary",
447*43a90889SApple OSS Distributions T_META_IGNORECRASHES(".*pinned_rights_child.*"),
448*43a90889SApple OSS Distributions T_META_CHECK_LEAKS(false),
449*43a90889SApple OSS Distributions T_META_TAG_VM_PREFERRED)
450*43a90889SApple OSS Distributions {
451*43a90889SApple OSS Distributions test_imm_pinned_control_port("imm_pinned_control_port_crasher");
452*43a90889SApple OSS Distributions }
453