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