xref: /xnu-10002.81.5/tests/imm_pinned_control_port_crasher.c (revision 5e3eaea39dcf651e66cb99ba7d70e32cc4a99587)
1*5e3eaea3SApple OSS Distributions #include <mach/mach.h>
2*5e3eaea3SApple OSS Distributions #include <stdlib.h>
3*5e3eaea3SApple OSS Distributions #include <pthread.h>
4*5e3eaea3SApple OSS Distributions #include <unistd.h>
5*5e3eaea3SApple OSS Distributions #include <stdio.h>
6*5e3eaea3SApple OSS Distributions #include <assert.h>
7*5e3eaea3SApple OSS Distributions #include <mach/task.h>
8*5e3eaea3SApple OSS Distributions #include <mach/mk_timer.h>
9*5e3eaea3SApple OSS Distributions 
10*5e3eaea3SApple OSS Distributions /*
11*5e3eaea3SApple OSS Distributions  * DO NOT run this test file by itself.
12*5e3eaea3SApple OSS Distributions  * This test is meant to be invoked by control_port_options darwintest.
13*5e3eaea3SApple OSS Distributions  *
14*5e3eaea3SApple OSS Distributions  * If hard enforcement for pinned control port is on, pinned tests are
15*5e3eaea3SApple OSS Distributions  * expected to generate fatal EXC_GUARD.
16*5e3eaea3SApple OSS Distributions  *
17*5e3eaea3SApple OSS Distributions  * If hard enforcement for immovable control port is on, immovable tests are
18*5e3eaea3SApple OSS Distributions  * expected to generate fatal EXC_GUARD.
19*5e3eaea3SApple OSS Distributions  *
20*5e3eaea3SApple OSS Distributions  * The type of exception raised (if any) is checked on control_port_options side.
21*5e3eaea3SApple OSS Distributions  */
22*5e3eaea3SApple OSS Distributions #define MAX_TEST_NUM 21
23*5e3eaea3SApple OSS Distributions 
24*5e3eaea3SApple OSS Distributions #ifndef MACH64_SEND_ANY
25*5e3eaea3SApple OSS Distributions #define MACH64_SEND_ANY 0x0000000800000000ull
26*5e3eaea3SApple OSS Distributions #define MACH64_SEND_MQ_CALL 0x0000000400000000ull
27*5e3eaea3SApple OSS Distributions #endif
28*5e3eaea3SApple OSS Distributions 
29*5e3eaea3SApple OSS Distributions static int
attempt_send_immovable_port(mach_port_name_t port,mach_msg_type_name_t disp)30*5e3eaea3SApple OSS Distributions attempt_send_immovable_port(mach_port_name_t port, mach_msg_type_name_t disp)
31*5e3eaea3SApple OSS Distributions {
32*5e3eaea3SApple OSS Distributions 	mach_port_t server;
33*5e3eaea3SApple OSS Distributions 	kern_return_t kr;
34*5e3eaea3SApple OSS Distributions 	kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &server);
35*5e3eaea3SApple OSS Distributions 	assert(kr == 0);
36*5e3eaea3SApple OSS Distributions 
37*5e3eaea3SApple OSS Distributions 	kr = mach_port_insert_right(mach_task_self(), server, server, MACH_MSG_TYPE_MAKE_SEND);
38*5e3eaea3SApple OSS Distributions 	assert(kr == 0);
39*5e3eaea3SApple OSS Distributions 
40*5e3eaea3SApple OSS Distributions 	struct {
41*5e3eaea3SApple OSS Distributions 		mach_msg_header_t header;
42*5e3eaea3SApple OSS Distributions 		mach_msg_body_t body;
43*5e3eaea3SApple OSS Distributions 		mach_msg_port_descriptor_t desc;
44*5e3eaea3SApple OSS Distributions 	} msg;
45*5e3eaea3SApple OSS Distributions 
46*5e3eaea3SApple OSS Distributions 	msg.header.msgh_remote_port = server;
47*5e3eaea3SApple OSS Distributions 	msg.header.msgh_local_port = MACH_PORT_NULL;
48*5e3eaea3SApple OSS Distributions 	msg.header.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0) | MACH_MSGH_BITS_COMPLEX;
49*5e3eaea3SApple OSS Distributions 	msg.header.msgh_size = sizeof msg;
50*5e3eaea3SApple OSS Distributions 
51*5e3eaea3SApple OSS Distributions 	msg.body.msgh_descriptor_count = 1;
52*5e3eaea3SApple OSS Distributions 
53*5e3eaea3SApple OSS Distributions 	msg.desc.name = port;
54*5e3eaea3SApple OSS Distributions 	msg.desc.disposition = disp;
55*5e3eaea3SApple OSS Distributions 	msg.desc.type = MACH_MSG_PORT_DESCRIPTOR;
56*5e3eaea3SApple OSS Distributions 
57*5e3eaea3SApple OSS Distributions 	return mach_msg_send(&msg.header);
58*5e3eaea3SApple OSS Distributions }
59*5e3eaea3SApple OSS Distributions 
60*5e3eaea3SApple OSS Distributions static void
pinned_test_main_thread_mod_ref(void)61*5e3eaea3SApple OSS Distributions pinned_test_main_thread_mod_ref(void)
62*5e3eaea3SApple OSS Distributions {
63*5e3eaea3SApple OSS Distributions 	printf("[Crasher]: Mod refs main thread's self port to 0\n");
64*5e3eaea3SApple OSS Distributions 	mach_port_t thread_self = mach_thread_self();
65*5e3eaea3SApple OSS Distributions 	kern_return_t kr = mach_port_mod_refs(mach_task_self(), thread_self, MACH_PORT_RIGHT_SEND, -2);
66*5e3eaea3SApple OSS Distributions 
67*5e3eaea3SApple OSS Distributions 	printf("[Crasher pinned_test_main_thread_mod_ref] mach_port_mod_refs returned %s \n.", mach_error_string(kr));
68*5e3eaea3SApple OSS Distributions }
69*5e3eaea3SApple OSS Distributions 
70*5e3eaea3SApple OSS Distributions static void* _Nullable
pthread_run(void * _Nullable)71*5e3eaea3SApple OSS Distributions pthread_run(void *_Nullable)
72*5e3eaea3SApple OSS Distributions {
73*5e3eaea3SApple OSS Distributions 	printf("[Crasher]: Deallocate pthread_self\n");
74*5e3eaea3SApple OSS Distributions 	mach_port_t th_self = pthread_mach_thread_np(pthread_self());
75*5e3eaea3SApple OSS Distributions 	kern_return_t kr = mach_port_deallocate(mach_task_self(), th_self);
76*5e3eaea3SApple OSS Distributions 
77*5e3eaea3SApple OSS Distributions 	printf("[Crasher pinned_test_pthread_dealloc] mach_port_deallocate returned %s \n.", mach_error_string(kr));
78*5e3eaea3SApple OSS Distributions 	return NULL;
79*5e3eaea3SApple OSS Distributions }
80*5e3eaea3SApple OSS Distributions 
81*5e3eaea3SApple OSS Distributions static void
pinned_test_pthread_dealloc(void)82*5e3eaea3SApple OSS Distributions pinned_test_pthread_dealloc(void)
83*5e3eaea3SApple OSS Distributions {
84*5e3eaea3SApple OSS Distributions 	printf("[Crasher]: Create a pthread and deallocate its self port\n");
85*5e3eaea3SApple OSS Distributions 	pthread_t thread;
86*5e3eaea3SApple OSS Distributions 	int ret = pthread_create(&thread, NULL, pthread_run, NULL);
87*5e3eaea3SApple OSS Distributions 	assert(ret == 0);
88*5e3eaea3SApple OSS Distributions 	ret = pthread_join(thread, NULL);
89*5e3eaea3SApple OSS Distributions 	assert(ret == 0);
90*5e3eaea3SApple OSS Distributions }
91*5e3eaea3SApple OSS Distributions 
92*5e3eaea3SApple OSS Distributions static void
pinned_test_task_self_dealloc(void)93*5e3eaea3SApple OSS Distributions pinned_test_task_self_dealloc(void)
94*5e3eaea3SApple OSS Distributions {
95*5e3eaea3SApple OSS Distributions 	printf("[Crasher]: Deallocate mach_task_self twice\n");
96*5e3eaea3SApple OSS Distributions 	mach_port_t task_self = mach_task_self();
97*5e3eaea3SApple OSS Distributions 	kern_return_t kr = mach_port_deallocate(task_self, task_self);
98*5e3eaea3SApple OSS Distributions 	assert(kr == 0);
99*5e3eaea3SApple OSS Distributions 	kr = mach_port_deallocate(task_self, task_self);
100*5e3eaea3SApple OSS Distributions 
101*5e3eaea3SApple OSS Distributions 	printf("[Crasher pinned_test_task_self_dealloc] mach_port_deallocate returned %s \n.", mach_error_string(kr));
102*5e3eaea3SApple OSS Distributions }
103*5e3eaea3SApple OSS Distributions 
104*5e3eaea3SApple OSS Distributions static void
pinned_test_task_self_mod_ref(void)105*5e3eaea3SApple OSS Distributions pinned_test_task_self_mod_ref(void)
106*5e3eaea3SApple OSS Distributions {
107*5e3eaea3SApple OSS Distributions 	printf("[Crasher]: Mod refs mach_task_self() to 0\n");
108*5e3eaea3SApple OSS Distributions 	kern_return_t kr = mach_port_mod_refs(mach_task_self(), mach_task_self(), MACH_PORT_RIGHT_SEND, -2);
109*5e3eaea3SApple OSS Distributions 
110*5e3eaea3SApple OSS Distributions 	printf("[Crasher pinned_test_task_self_mod_ref] mach_port_mod_refs returned %s \n.", mach_error_string(kr));
111*5e3eaea3SApple OSS Distributions }
112*5e3eaea3SApple OSS Distributions 
113*5e3eaea3SApple OSS Distributions static void
pinned_test_task_threads_mod_ref(void)114*5e3eaea3SApple OSS Distributions pinned_test_task_threads_mod_ref(void)
115*5e3eaea3SApple OSS Distributions {
116*5e3eaea3SApple OSS Distributions 	printf("[Crasher]: task_threads should return pinned thread ports. Mod refs them to 0\n");
117*5e3eaea3SApple OSS Distributions 	thread_array_t th_list;
118*5e3eaea3SApple OSS Distributions 	mach_msg_type_number_t th_cnt;
119*5e3eaea3SApple OSS Distributions 	kern_return_t kr;
120*5e3eaea3SApple OSS Distributions 	mach_port_t th_kp = mach_thread_self();
121*5e3eaea3SApple OSS Distributions 	mach_port_deallocate(mach_task_self(), th_kp);
122*5e3eaea3SApple OSS Distributions 
123*5e3eaea3SApple OSS Distributions 	kr = task_threads(mach_task_self(), &th_list, &th_cnt);
124*5e3eaea3SApple OSS Distributions 	mach_port_deallocate(mach_task_self(), th_list[0]);
125*5e3eaea3SApple OSS Distributions 
126*5e3eaea3SApple OSS Distributions 	kr = mach_port_mod_refs(mach_task_self(), th_list[0], MACH_PORT_RIGHT_SEND, -1);
127*5e3eaea3SApple OSS Distributions 
128*5e3eaea3SApple OSS Distributions 	printf("[Crasher pinned_test_task_threads_mod_ref] mach_port_mod_refs returned %s \n.", mach_error_string(kr));
129*5e3eaea3SApple OSS Distributions }
130*5e3eaea3SApple OSS Distributions 
131*5e3eaea3SApple OSS Distributions static void
pinned_test_mach_port_destroy(void)132*5e3eaea3SApple OSS Distributions pinned_test_mach_port_destroy(void)
133*5e3eaea3SApple OSS Distributions {
134*5e3eaea3SApple OSS Distributions 	kern_return_t kr = mach_port_destroy(mach_task_self(), mach_task_self());
135*5e3eaea3SApple OSS Distributions 	printf("[Crasher pinned_test_mach_port_destroy] mach_port_destroy returned %s \n.", mach_error_string(kr));
136*5e3eaea3SApple OSS Distributions }
137*5e3eaea3SApple OSS Distributions 
138*5e3eaea3SApple OSS Distributions static void
pinned_test_move_send_as_remote_port(void)139*5e3eaea3SApple OSS Distributions pinned_test_move_send_as_remote_port(void)
140*5e3eaea3SApple OSS Distributions {
141*5e3eaea3SApple OSS Distributions 	struct {
142*5e3eaea3SApple OSS Distributions 		mach_msg_header_t header;
143*5e3eaea3SApple OSS Distributions 	} msg;
144*5e3eaea3SApple OSS Distributions 
145*5e3eaea3SApple OSS Distributions 	kern_return_t kr = mach_port_deallocate(mach_task_self(), mach_task_self());
146*5e3eaea3SApple OSS Distributions 	assert(kr == 0);
147*5e3eaea3SApple OSS Distributions 
148*5e3eaea3SApple OSS Distributions 	/*
149*5e3eaea3SApple OSS Distributions 	 * We allow move send on remote kobject port but this should trip on pinning on last ref.
150*5e3eaea3SApple OSS Distributions 	 * See: IPC_OBJECT_COPYIN_FLAGS_ALLOW_IMMOVABLE_SEND.
151*5e3eaea3SApple OSS Distributions 	 */
152*5e3eaea3SApple OSS Distributions 	msg.header.msgh_remote_port = mach_task_self();
153*5e3eaea3SApple OSS Distributions 	msg.header.msgh_local_port = MACH_PORT_NULL;
154*5e3eaea3SApple OSS Distributions 	msg.header.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_MOVE_SEND, 0);
155*5e3eaea3SApple OSS Distributions 	msg.header.msgh_id = 2000;
156*5e3eaea3SApple OSS Distributions 	msg.header.msgh_size = sizeof msg;
157*5e3eaea3SApple OSS Distributions 
158*5e3eaea3SApple OSS Distributions 	kr = mach_msg_send(&msg.header);
159*5e3eaea3SApple OSS Distributions 
160*5e3eaea3SApple OSS Distributions 	printf("[Crasher pinned_test_move_send_as_remote_port] mach_msg_send returned %s \n.", mach_error_string(kr));
161*5e3eaea3SApple OSS Distributions }
162*5e3eaea3SApple OSS Distributions 
163*5e3eaea3SApple OSS Distributions static void
immovable_test_move_send_as_remote_port(void)164*5e3eaea3SApple OSS Distributions immovable_test_move_send_as_remote_port(void)
165*5e3eaea3SApple OSS Distributions {
166*5e3eaea3SApple OSS Distributions 	struct {
167*5e3eaea3SApple OSS Distributions 		mach_msg_header_t header;
168*5e3eaea3SApple OSS Distributions 	} msg;
169*5e3eaea3SApple OSS Distributions 
170*5e3eaea3SApple OSS Distributions 	/* Local port cannot be immovable. See: ipc_right_copyin_check_reply() */
171*5e3eaea3SApple OSS Distributions 	msg.header.msgh_remote_port = mach_task_self();
172*5e3eaea3SApple OSS Distributions 	msg.header.msgh_local_port = mach_task_self();
173*5e3eaea3SApple OSS Distributions 	msg.header.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_MOVE_SEND, MACH_MSG_TYPE_MOVE_SEND);
174*5e3eaea3SApple OSS Distributions 	msg.header.msgh_id = 2000;
175*5e3eaea3SApple OSS Distributions 	msg.header.msgh_size = sizeof msg;
176*5e3eaea3SApple OSS Distributions 
177*5e3eaea3SApple OSS Distributions 	kern_return_t kr = mach_msg_send(&msg.header);
178*5e3eaea3SApple OSS Distributions 
179*5e3eaea3SApple OSS Distributions 	printf("[Crasher immovable_test_move_send_as_remote_port] mach_msg_send returned %s \n.", mach_error_string(kr));
180*5e3eaea3SApple OSS Distributions }
181*5e3eaea3SApple OSS Distributions 
182*5e3eaea3SApple OSS Distributions static void
immovable_test_move_send_task_self(void)183*5e3eaea3SApple OSS Distributions immovable_test_move_send_task_self(void)
184*5e3eaea3SApple OSS Distributions {
185*5e3eaea3SApple OSS Distributions 	kern_return_t kr;
186*5e3eaea3SApple OSS Distributions 	printf("[Crasher]: Move send mach_task_self_\n");
187*5e3eaea3SApple OSS Distributions 	kr = attempt_send_immovable_port(mach_task_self(), MACH_MSG_TYPE_MOVE_SEND);
188*5e3eaea3SApple OSS Distributions 
189*5e3eaea3SApple OSS Distributions 	printf("[Crasher immovable_test_move_send_task_self] attempt_send_immovable_port returned %s \n.", mach_error_string(kr));
190*5e3eaea3SApple OSS Distributions }
191*5e3eaea3SApple OSS Distributions 
192*5e3eaea3SApple OSS Distributions static void
immovable_test_copy_send_task_self(void)193*5e3eaea3SApple OSS Distributions immovable_test_copy_send_task_self(void)
194*5e3eaea3SApple OSS Distributions {
195*5e3eaea3SApple OSS Distributions 	kern_return_t kr;
196*5e3eaea3SApple OSS Distributions 	printf("[Crasher]: Copy send mach_task_self_\n");
197*5e3eaea3SApple OSS Distributions 	kr = attempt_send_immovable_port(mach_task_self(), MACH_MSG_TYPE_COPY_SEND);
198*5e3eaea3SApple OSS Distributions 
199*5e3eaea3SApple OSS Distributions 	printf("[Crasher immovable_test_copy_send_task_self] attempt_send_immovable_port returned %s \n.", mach_error_string(kr));
200*5e3eaea3SApple OSS Distributions }
201*5e3eaea3SApple OSS Distributions 
202*5e3eaea3SApple OSS Distributions static void
immovable_test_move_send_thread_self(void)203*5e3eaea3SApple OSS Distributions immovable_test_move_send_thread_self(void)
204*5e3eaea3SApple OSS Distributions {
205*5e3eaea3SApple OSS Distributions 	kern_return_t kr;
206*5e3eaea3SApple OSS Distributions 	printf("[Crasher]: Move send main thread's self port\n");
207*5e3eaea3SApple OSS Distributions 	kr = attempt_send_immovable_port(mach_thread_self(), MACH_MSG_TYPE_MOVE_SEND);
208*5e3eaea3SApple OSS Distributions 
209*5e3eaea3SApple OSS Distributions 	printf("[Crasher immovable_test_move_send_thread_self] attempt_send_immovable_port returned %s \n.", mach_error_string(kr));
210*5e3eaea3SApple OSS Distributions }
211*5e3eaea3SApple OSS Distributions 
212*5e3eaea3SApple OSS Distributions static void
immovable_test_copy_send_thread_self(void)213*5e3eaea3SApple OSS Distributions immovable_test_copy_send_thread_self(void)
214*5e3eaea3SApple OSS Distributions {
215*5e3eaea3SApple OSS Distributions 	kern_return_t kr;
216*5e3eaea3SApple OSS Distributions 	mach_port_t port;
217*5e3eaea3SApple OSS Distributions 	printf("[Crasher]: Copy send main thread's self port\n");
218*5e3eaea3SApple OSS Distributions 	port = mach_thread_self();
219*5e3eaea3SApple OSS Distributions 	kr = attempt_send_immovable_port(port, MACH_MSG_TYPE_COPY_SEND);
220*5e3eaea3SApple OSS Distributions 	printf("[Crasher immovable_test_copy_send_thread_self] attempt_send_immovable_port returned %s \n.", mach_error_string(kr));
221*5e3eaea3SApple OSS Distributions 
222*5e3eaea3SApple OSS Distributions 	mach_port_deallocate(mach_task_self(), port);
223*5e3eaea3SApple OSS Distributions }
224*5e3eaea3SApple OSS Distributions 
225*5e3eaea3SApple OSS Distributions static void
immovable_test_copy_send_task_read(void)226*5e3eaea3SApple OSS Distributions immovable_test_copy_send_task_read(void)
227*5e3eaea3SApple OSS Distributions {
228*5e3eaea3SApple OSS Distributions 	kern_return_t kr;
229*5e3eaea3SApple OSS Distributions 	mach_port_t port;
230*5e3eaea3SApple OSS Distributions 	printf("[Crasher]: Copy send task read port\n");
231*5e3eaea3SApple OSS Distributions 	kr = task_get_special_port(mach_task_self(), TASK_READ_PORT, &port);
232*5e3eaea3SApple OSS Distributions 	assert(kr == 0);
233*5e3eaea3SApple OSS Distributions 	kr = attempt_send_immovable_port(port, MACH_MSG_TYPE_COPY_SEND);
234*5e3eaea3SApple OSS Distributions 	printf("[Crasher immovable_test_copy_send_task_read] attempt_send_immovable_port returned %s \n.", mach_error_string(kr));
235*5e3eaea3SApple OSS Distributions 
236*5e3eaea3SApple OSS Distributions 	mach_port_deallocate(mach_task_self(), port);
237*5e3eaea3SApple OSS Distributions }
238*5e3eaea3SApple OSS Distributions 
239*5e3eaea3SApple OSS Distributions static void
immovable_test_copy_send_task_inspect(void)240*5e3eaea3SApple OSS Distributions immovable_test_copy_send_task_inspect(void)
241*5e3eaea3SApple OSS Distributions {
242*5e3eaea3SApple OSS Distributions 	kern_return_t kr;
243*5e3eaea3SApple OSS Distributions 	mach_port_t port;
244*5e3eaea3SApple OSS Distributions 	printf("[Crasher]: Move send task inspect port\n");
245*5e3eaea3SApple OSS Distributions 	kr = task_get_special_port(mach_task_self(), TASK_INSPECT_PORT, &port);
246*5e3eaea3SApple OSS Distributions 	assert(kr == 0);
247*5e3eaea3SApple OSS Distributions 	kr = attempt_send_immovable_port(port, MACH_MSG_TYPE_MOVE_SEND);
248*5e3eaea3SApple OSS Distributions 	printf("[Crasher immovable_test_copy_send_task_inspect] attempt_send_immovable_port returned %s \n.", mach_error_string(kr));
249*5e3eaea3SApple OSS Distributions }
250*5e3eaea3SApple OSS Distributions 
251*5e3eaea3SApple OSS Distributions static void
immovable_test_move_send_thread_inspect(void)252*5e3eaea3SApple OSS Distributions immovable_test_move_send_thread_inspect(void)
253*5e3eaea3SApple OSS Distributions {
254*5e3eaea3SApple OSS Distributions 	kern_return_t kr;
255*5e3eaea3SApple OSS Distributions 	mach_port_t port;
256*5e3eaea3SApple OSS Distributions 	mach_port_t th_port = mach_thread_self();
257*5e3eaea3SApple OSS Distributions 
258*5e3eaea3SApple OSS Distributions 	printf("[Crasher]: Move send thread inspect port\n");
259*5e3eaea3SApple OSS Distributions 	kr = thread_get_special_port(th_port, THREAD_INSPECT_PORT, &port);
260*5e3eaea3SApple OSS Distributions 	assert(kr == 0);
261*5e3eaea3SApple OSS Distributions 	kr = attempt_send_immovable_port(port, MACH_MSG_TYPE_MOVE_SEND);
262*5e3eaea3SApple OSS Distributions 	printf("[Crasher immovable_test_move_send_thread_inspect] attempt_send_immovable_port returned %s \n.", mach_error_string(kr));
263*5e3eaea3SApple OSS Distributions 
264*5e3eaea3SApple OSS Distributions 	mach_port_deallocate(mach_task_self(), th_port);
265*5e3eaea3SApple OSS Distributions }
266*5e3eaea3SApple OSS Distributions 
267*5e3eaea3SApple OSS Distributions static void
immovable_test_move_send_raw_thread(void)268*5e3eaea3SApple OSS Distributions immovable_test_move_send_raw_thread(void)
269*5e3eaea3SApple OSS Distributions {
270*5e3eaea3SApple OSS Distributions 	kern_return_t kr;
271*5e3eaea3SApple OSS Distributions 	mach_port_t port;
272*5e3eaea3SApple OSS Distributions 
273*5e3eaea3SApple OSS Distributions 	kr = thread_create(mach_task_self(), &port);
274*5e3eaea3SApple OSS Distributions 	assert(kr == 0);
275*5e3eaea3SApple OSS Distributions 	kr = mach_port_deallocate(mach_task_self(), port); /* not pinned, should not crash */
276*5e3eaea3SApple OSS Distributions 
277*5e3eaea3SApple OSS Distributions 	kr = thread_create(mach_task_self(), &port);
278*5e3eaea3SApple OSS Distributions 	assert(kr == 0);
279*5e3eaea3SApple OSS Distributions 	kr = attempt_send_immovable_port(port, MACH_MSG_TYPE_MOVE_SEND); /* immovable, should crash here */
280*5e3eaea3SApple OSS Distributions 	printf("[Crasher immovable_test_move_send_raw_thread] attempt_send_immovable_port returned %s \n.", mach_error_string(kr));
281*5e3eaea3SApple OSS Distributions 
282*5e3eaea3SApple OSS Distributions 	kr = thread_terminate(port);
283*5e3eaea3SApple OSS Distributions 	assert(kr == 0);
284*5e3eaea3SApple OSS Distributions }
285*5e3eaea3SApple OSS Distributions 
286*5e3eaea3SApple OSS Distributions static void
immovable_test_copy_send_thread_read(void)287*5e3eaea3SApple OSS Distributions immovable_test_copy_send_thread_read(void)
288*5e3eaea3SApple OSS Distributions {
289*5e3eaea3SApple OSS Distributions 	kern_return_t kr;
290*5e3eaea3SApple OSS Distributions 	mach_port_t port;
291*5e3eaea3SApple OSS Distributions 	mach_port_t th_port = mach_thread_self();
292*5e3eaea3SApple OSS Distributions 
293*5e3eaea3SApple OSS Distributions 	printf("[Crasher]: Copy send thread read port\n");
294*5e3eaea3SApple OSS Distributions 	kr = thread_get_special_port(th_port, THREAD_READ_PORT, &port);
295*5e3eaea3SApple OSS Distributions 	assert(kr == 0);
296*5e3eaea3SApple OSS Distributions 	kr = attempt_send_immovable_port(port, MACH_MSG_TYPE_COPY_SEND);
297*5e3eaea3SApple OSS Distributions 	printf("[Crasher immovable_test_copy_send_thread_read] attempt_send_immovable_port returned %s \n.", mach_error_string(kr));
298*5e3eaea3SApple OSS Distributions 
299*5e3eaea3SApple OSS Distributions 	mach_port_deallocate(mach_task_self(), port);
300*5e3eaea3SApple OSS Distributions 	mach_port_deallocate(mach_task_self(), th_port);
301*5e3eaea3SApple OSS Distributions }
302*5e3eaea3SApple OSS Distributions 
303*5e3eaea3SApple OSS Distributions static void
cfi_test_no_bit_set(void)304*5e3eaea3SApple OSS Distributions cfi_test_no_bit_set(void)
305*5e3eaea3SApple OSS Distributions {
306*5e3eaea3SApple OSS Distributions 	printf("[Crasher]: Try sending mach_msg2() without setting CFI bits\n");
307*5e3eaea3SApple OSS Distributions 
308*5e3eaea3SApple OSS Distributions 	mach_msg_header_t header;
309*5e3eaea3SApple OSS Distributions 	kern_return_t kr;
310*5e3eaea3SApple OSS Distributions 
311*5e3eaea3SApple OSS Distributions 	header.msgh_local_port = MACH_PORT_NULL;
312*5e3eaea3SApple OSS Distributions 	header.msgh_remote_port = mach_task_self();
313*5e3eaea3SApple OSS Distributions 	header.msgh_id = 3409;
314*5e3eaea3SApple OSS Distributions 	header.msgh_bits = MACH_MSGH_BITS_SET(MACH_MSG_TYPE_COPY_SEND, 0, 0, 0);
315*5e3eaea3SApple OSS Distributions 	header.msgh_size = sizeof(header);
316*5e3eaea3SApple OSS Distributions 
317*5e3eaea3SApple OSS Distributions 	kr = mach_msg2(&header, MACH64_SEND_MSG, header, header.msgh_size, 0, MACH_PORT_NULL,
318*5e3eaea3SApple OSS Distributions 	    0, MACH_MSG_PRIORITY_UNSPECIFIED);
319*5e3eaea3SApple OSS Distributions 	/* crash */
320*5e3eaea3SApple OSS Distributions 	printf("[Crasher cfi_test_no_bit_set]: mach_msg2() returned %d\n", kr);
321*5e3eaea3SApple OSS Distributions }
322*5e3eaea3SApple OSS Distributions 
323*5e3eaea3SApple OSS Distributions static void
cfi_test_two_bits_set(void)324*5e3eaea3SApple OSS Distributions cfi_test_two_bits_set(void)
325*5e3eaea3SApple OSS Distributions {
326*5e3eaea3SApple OSS Distributions 	printf("[Crasher]: Try sending mach_msg2() but setting 2 CFI bits\n");
327*5e3eaea3SApple OSS Distributions 
328*5e3eaea3SApple OSS Distributions 	mach_msg_header_t header;
329*5e3eaea3SApple OSS Distributions 	kern_return_t kr;
330*5e3eaea3SApple OSS Distributions 
331*5e3eaea3SApple OSS Distributions 	header.msgh_local_port = MACH_PORT_NULL;
332*5e3eaea3SApple OSS Distributions 	header.msgh_remote_port = mach_task_self();
333*5e3eaea3SApple OSS Distributions 	header.msgh_id = 3409;
334*5e3eaea3SApple OSS Distributions 	header.msgh_bits = MACH_MSGH_BITS_SET(MACH_MSG_TYPE_COPY_SEND, 0, 0, 0);
335*5e3eaea3SApple OSS Distributions 	header.msgh_size = sizeof(header);
336*5e3eaea3SApple OSS Distributions 
337*5e3eaea3SApple OSS Distributions 	kr = mach_msg2(&header, MACH64_SEND_MSG | MACH64_SEND_ANY | MACH64_SEND_KOBJECT_CALL,
338*5e3eaea3SApple OSS Distributions 	    header, header.msgh_size, 0, MACH_PORT_NULL,
339*5e3eaea3SApple OSS Distributions 	    0, MACH_MSG_PRIORITY_UNSPECIFIED);
340*5e3eaea3SApple OSS Distributions 	/* crash */
341*5e3eaea3SApple OSS Distributions 	printf("[Crasher cfi_test_two_bits_set]: mach_msg2() returned %d\n", kr);
342*5e3eaea3SApple OSS Distributions }
343*5e3eaea3SApple OSS Distributions 
344*5e3eaea3SApple OSS Distributions static void
cfi_test_msg_to_timer_port(void)345*5e3eaea3SApple OSS Distributions cfi_test_msg_to_timer_port(void)
346*5e3eaea3SApple OSS Distributions {
347*5e3eaea3SApple OSS Distributions 	printf("[Crasher]: Try sending mach_msg2() to timer port\n");
348*5e3eaea3SApple OSS Distributions 
349*5e3eaea3SApple OSS Distributions 	mach_port_t timer = MACH_PORT_NULL;
350*5e3eaea3SApple OSS Distributions 	struct oversize_msg {
351*5e3eaea3SApple OSS Distributions 		mach_msg_header_t header;
352*5e3eaea3SApple OSS Distributions 		char data[2048];
353*5e3eaea3SApple OSS Distributions 	} msg;
354*5e3eaea3SApple OSS Distributions 
355*5e3eaea3SApple OSS Distributions 	kern_return_t kr;
356*5e3eaea3SApple OSS Distributions 	natural_t kotype;
357*5e3eaea3SApple OSS Distributions 	mach_vm_address_t addr;
358*5e3eaea3SApple OSS Distributions 
359*5e3eaea3SApple OSS Distributions #define IKOT_TIMER 8
360*5e3eaea3SApple OSS Distributions 	timer = mk_timer_create();
361*5e3eaea3SApple OSS Distributions 	assert(timer != MACH_PORT_NULL);
362*5e3eaea3SApple OSS Distributions 
363*5e3eaea3SApple OSS Distributions 	/* Make sure it's a kobject port */
364*5e3eaea3SApple OSS Distributions 	kr = mach_port_kobject(mach_task_self(), timer, &kotype, &addr);
365*5e3eaea3SApple OSS Distributions 	assert(kr == KERN_SUCCESS);
366*5e3eaea3SApple OSS Distributions 	assert(kotype == IKOT_TIMER);
367*5e3eaea3SApple OSS Distributions 
368*5e3eaea3SApple OSS Distributions 	msg.header.msgh_local_port = MACH_PORT_NULL;
369*5e3eaea3SApple OSS Distributions 	msg.header.msgh_remote_port = timer;
370*5e3eaea3SApple OSS Distributions 	msg.header.msgh_bits = MACH_MSGH_BITS_SET(MACH_MSG_TYPE_MAKE_SEND, 0, 0, 0);
371*5e3eaea3SApple OSS Distributions 	msg.header.msgh_size = sizeof(msg);
372*5e3eaea3SApple OSS Distributions 
373*5e3eaea3SApple OSS Distributions 	/* Timer port must use MACH64_SEND_MQ_CALL */
374*5e3eaea3SApple OSS Distributions 	kr = mach_msg2(&msg, MACH64_SEND_MSG | MACH64_SEND_MQ_CALL,
375*5e3eaea3SApple OSS Distributions 	    msg.header, msg.header.msgh_size, 0, MACH_PORT_NULL,
376*5e3eaea3SApple OSS Distributions 	    0, MACH_MSG_PRIORITY_UNSPECIFIED);
377*5e3eaea3SApple OSS Distributions 	assert(kr == KERN_SUCCESS);
378*5e3eaea3SApple OSS Distributions 	printf("Message sent to timer port successfully\n");
379*5e3eaea3SApple OSS Distributions 
380*5e3eaea3SApple OSS Distributions 	/* Using MACH64_SEND_KOBJECT_CALL should crash */
381*5e3eaea3SApple OSS Distributions 	kr = mach_msg2(&msg, MACH64_SEND_MSG | MACH64_SEND_KOBJECT_CALL,
382*5e3eaea3SApple OSS Distributions 	    msg.header, msg.header.msgh_size, 0, MACH_PORT_NULL,
383*5e3eaea3SApple OSS Distributions 	    0, MACH_MSG_PRIORITY_UNSPECIFIED);
384*5e3eaea3SApple OSS Distributions 	/* crash */
385*5e3eaea3SApple OSS Distributions 	printf("[Crasher cfi_test_timer_port]: mach_msg2() returned %d\n", kr);
386*5e3eaea3SApple OSS Distributions }
387*5e3eaea3SApple OSS Distributions 
388*5e3eaea3SApple OSS Distributions static void
cfi_test_wrong_bit_set(void)389*5e3eaea3SApple OSS Distributions cfi_test_wrong_bit_set(void)
390*5e3eaea3SApple OSS Distributions {
391*5e3eaea3SApple OSS Distributions 	printf("[Crasher]: Try sending mach_msg2() but setting wrong CFI bits\n");
392*5e3eaea3SApple OSS Distributions 
393*5e3eaea3SApple OSS Distributions 	mach_msg_header_t header;
394*5e3eaea3SApple OSS Distributions 	kern_return_t kr;
395*5e3eaea3SApple OSS Distributions 
396*5e3eaea3SApple OSS Distributions 	header.msgh_local_port = MACH_PORT_NULL;
397*5e3eaea3SApple OSS Distributions 	header.msgh_remote_port = mach_task_self();
398*5e3eaea3SApple OSS Distributions 	header.msgh_id = 3409;
399*5e3eaea3SApple OSS Distributions 	header.msgh_bits = MACH_MSGH_BITS_SET(MACH_MSG_TYPE_COPY_SEND, 0, 0, 0);
400*5e3eaea3SApple OSS Distributions 	header.msgh_size = sizeof(header);
401*5e3eaea3SApple OSS Distributions 
402*5e3eaea3SApple OSS Distributions 	/* Using MACH64_SEND_MQ_CALL but destination is a kobject port */
403*5e3eaea3SApple OSS Distributions 	kr = mach_msg2(&header, MACH64_SEND_MSG | MACH64_SEND_MQ_CALL,
404*5e3eaea3SApple OSS Distributions 	    header, header.msgh_size, 0, MACH_PORT_NULL,
405*5e3eaea3SApple OSS Distributions 	    0, MACH_MSG_PRIORITY_UNSPECIFIED);
406*5e3eaea3SApple OSS Distributions 	/* crash */
407*5e3eaea3SApple OSS Distributions 	printf("[Crasher cfi_test_wrong_bit_set]: mach_msg2() returned %d\n", kr);
408*5e3eaea3SApple OSS Distributions }
409*5e3eaea3SApple OSS Distributions 
410*5e3eaea3SApple OSS Distributions int
main(int argc,char * argv[])411*5e3eaea3SApple OSS Distributions main(int argc, char *argv[])
412*5e3eaea3SApple OSS Distributions {
413*5e3eaea3SApple OSS Distributions 	void (*tests[MAX_TEST_NUM])(void) = {
414*5e3eaea3SApple OSS Distributions 		pinned_test_main_thread_mod_ref,
415*5e3eaea3SApple OSS Distributions 		pinned_test_pthread_dealloc,
416*5e3eaea3SApple OSS Distributions 		pinned_test_task_self_dealloc,
417*5e3eaea3SApple OSS Distributions 		pinned_test_task_self_mod_ref,
418*5e3eaea3SApple OSS Distributions 		pinned_test_task_threads_mod_ref,
419*5e3eaea3SApple OSS Distributions 		pinned_test_mach_port_destroy,
420*5e3eaea3SApple OSS Distributions 		pinned_test_move_send_as_remote_port,
421*5e3eaea3SApple OSS Distributions 
422*5e3eaea3SApple OSS Distributions 		immovable_test_move_send_task_self,
423*5e3eaea3SApple OSS Distributions 		immovable_test_copy_send_task_self,
424*5e3eaea3SApple OSS Distributions 		immovable_test_move_send_thread_self,
425*5e3eaea3SApple OSS Distributions 		immovable_test_copy_send_thread_self,
426*5e3eaea3SApple OSS Distributions 		immovable_test_copy_send_task_read,
427*5e3eaea3SApple OSS Distributions 		immovable_test_copy_send_task_inspect,
428*5e3eaea3SApple OSS Distributions 		immovable_test_move_send_thread_inspect,
429*5e3eaea3SApple OSS Distributions 		immovable_test_copy_send_thread_read,
430*5e3eaea3SApple OSS Distributions 		immovable_test_move_send_as_remote_port,
431*5e3eaea3SApple OSS Distributions 		immovable_test_move_send_raw_thread,
432*5e3eaea3SApple OSS Distributions 
433*5e3eaea3SApple OSS Distributions 		cfi_test_no_bit_set,
434*5e3eaea3SApple OSS Distributions 		cfi_test_two_bits_set,
435*5e3eaea3SApple OSS Distributions 		cfi_test_wrong_bit_set,
436*5e3eaea3SApple OSS Distributions 		cfi_test_msg_to_timer_port,
437*5e3eaea3SApple OSS Distributions 	};
438*5e3eaea3SApple OSS Distributions 	printf("[Crasher]: My Pid: %d\n", getpid());
439*5e3eaea3SApple OSS Distributions 
440*5e3eaea3SApple OSS Distributions 	if (argc < 2) {
441*5e3eaea3SApple OSS Distributions 		printf("[Crasher]: Specify a test to run.");
442*5e3eaea3SApple OSS Distributions 		exit(-1);
443*5e3eaea3SApple OSS Distributions 	}
444*5e3eaea3SApple OSS Distributions 
445*5e3eaea3SApple OSS Distributions 	int test_num = atoi(argv[1]);
446*5e3eaea3SApple OSS Distributions 
447*5e3eaea3SApple OSS Distributions 	if (test_num >= 0 && test_num < MAX_TEST_NUM) {
448*5e3eaea3SApple OSS Distributions 		(*tests[test_num])();
449*5e3eaea3SApple OSS Distributions 	} else {
450*5e3eaea3SApple OSS Distributions 		printf("[Crasher]: Invalid test num. Exiting...\n");
451*5e3eaea3SApple OSS Distributions 		exit(-1);
452*5e3eaea3SApple OSS Distributions 	}
453*5e3eaea3SApple OSS Distributions 
454*5e3eaea3SApple OSS Distributions 	exit(0);
455*5e3eaea3SApple OSS Distributions }
456