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