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