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