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