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