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