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