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