xref: /xnu-10063.141.1/tests/reply_port_defense_client.c (revision d8b80295118ef25ac3a784134bcf95cd8e88109f)
1*d8b80295SApple OSS Distributions #include <mach/kern_return.h>
2*d8b80295SApple OSS Distributions #include <stdlib.h>
3*d8b80295SApple OSS Distributions #include <stdio.h>
4*d8b80295SApple OSS Distributions #include <pthread.h>
5*d8b80295SApple OSS Distributions #include <unistd.h>
6*d8b80295SApple OSS Distributions #include <mach/mach.h>
7*d8b80295SApple OSS Distributions #include <mach/task.h>
8*d8b80295SApple OSS Distributions #include <mach/thread_status.h>
9*d8b80295SApple OSS Distributions #include <assert.h>
10*d8b80295SApple OSS Distributions #include <sys/codesign.h>
11*d8b80295SApple OSS Distributions #include <stdbool.h>
12*d8b80295SApple OSS Distributions #include "cs_helpers.h"
13*d8b80295SApple OSS Distributions 
14*d8b80295SApple OSS Distributions #define MAX_TEST_NUM 7
15*d8b80295SApple OSS Distributions 
16*d8b80295SApple OSS Distributions #if __arm64__
17*d8b80295SApple OSS Distributions #define machine_thread_state_t          arm_thread_state64_t
18*d8b80295SApple OSS Distributions #define EXCEPTION_THREAD_STATE          ARM_THREAD_STATE64
19*d8b80295SApple OSS Distributions #define EXCEPTION_THREAD_STATE_COUNT    ARM_THREAD_STATE64_COUNT
20*d8b80295SApple OSS Distributions #elif __x86_64__
21*d8b80295SApple OSS Distributions #define machine_thread_state_t          x86_thread_state_t
22*d8b80295SApple OSS Distributions #define EXCEPTION_THREAD_STATE          x86_THREAD_STATE
23*d8b80295SApple OSS Distributions #define EXCEPTION_THREAD_STATE_COUNT    x86_THREAD_STATE_COUNT
24*d8b80295SApple OSS Distributions #else
25*d8b80295SApple OSS Distributions #error Unsupported architecture
26*d8b80295SApple OSS Distributions #endif
27*d8b80295SApple OSS Distributions 
28*d8b80295SApple OSS Distributions static mach_port_t
alloc_server_port(void)29*d8b80295SApple OSS Distributions alloc_server_port(void)
30*d8b80295SApple OSS Distributions {
31*d8b80295SApple OSS Distributions 	mach_port_t server_port = MACH_PORT_NULL;
32*d8b80295SApple OSS Distributions 	kern_return_t kr;
33*d8b80295SApple OSS Distributions 
34*d8b80295SApple OSS Distributions 	kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &server_port);
35*d8b80295SApple OSS Distributions 	assert(kr == 0);
36*d8b80295SApple OSS Distributions 
37*d8b80295SApple OSS Distributions 	kr = mach_port_insert_right(mach_task_self(), server_port, server_port, MACH_MSG_TYPE_MAKE_SEND);
38*d8b80295SApple OSS Distributions 	assert(kr == 0);
39*d8b80295SApple OSS Distributions 
40*d8b80295SApple OSS Distributions 	return server_port;
41*d8b80295SApple OSS Distributions }
42*d8b80295SApple OSS Distributions 
43*d8b80295SApple OSS Distributions static mach_port_t
alloc_provisional_reply_port()44*d8b80295SApple OSS Distributions alloc_provisional_reply_port()
45*d8b80295SApple OSS Distributions {
46*d8b80295SApple OSS Distributions 	kern_return_t kr;
47*d8b80295SApple OSS Distributions 	mach_port_t reply_port = MACH_PORT_NULL;
48*d8b80295SApple OSS Distributions 	mach_port_t task = mach_task_self();
49*d8b80295SApple OSS Distributions 
50*d8b80295SApple OSS Distributions 	mach_port_options_t opts = {
51*d8b80295SApple OSS Distributions 		.flags = MPO_PROVISIONAL_REPLY_PORT | MPO_INSERT_SEND_RIGHT,
52*d8b80295SApple OSS Distributions 	};
53*d8b80295SApple OSS Distributions 
54*d8b80295SApple OSS Distributions 	kr = mach_port_construct(mach_task_self(), &opts, 0, &reply_port);
55*d8b80295SApple OSS Distributions 	assert(kr == 0);
56*d8b80295SApple OSS Distributions 
57*d8b80295SApple OSS Distributions 	return reply_port;
58*d8b80295SApple OSS Distributions }
59*d8b80295SApple OSS Distributions 
60*d8b80295SApple OSS Distributions static mach_port_t
alloc_reply_port()61*d8b80295SApple OSS Distributions alloc_reply_port()
62*d8b80295SApple OSS Distributions {
63*d8b80295SApple OSS Distributions 	kern_return_t kr;
64*d8b80295SApple OSS Distributions 	mach_port_t reply_port = MACH_PORT_NULL;
65*d8b80295SApple OSS Distributions 	mach_port_t task = mach_task_self();
66*d8b80295SApple OSS Distributions 
67*d8b80295SApple OSS Distributions 	mach_port_options_t opts = {
68*d8b80295SApple OSS Distributions 		.flags = MPO_REPLY_PORT | MPO_INSERT_SEND_RIGHT,
69*d8b80295SApple OSS Distributions 	};
70*d8b80295SApple OSS Distributions 
71*d8b80295SApple OSS Distributions 	kr = mach_port_construct(mach_task_self(), &opts, 0, &reply_port);
72*d8b80295SApple OSS Distributions 	assert(kr == 0);
73*d8b80295SApple OSS Distributions 
74*d8b80295SApple OSS Distributions 	return reply_port;
75*d8b80295SApple OSS Distributions }
76*d8b80295SApple OSS Distributions 
77*d8b80295SApple OSS Distributions /* The rcv right of the port would be marked immovable. */
78*d8b80295SApple OSS Distributions static void
test_immovable_receive_right(void)79*d8b80295SApple OSS Distributions test_immovable_receive_right(void)
80*d8b80295SApple OSS Distributions {
81*d8b80295SApple OSS Distributions 	kern_return_t kr;
82*d8b80295SApple OSS Distributions 	mach_port_t server_port = MACH_PORT_NULL, reply_port = MACH_PORT_NULL;
83*d8b80295SApple OSS Distributions 	struct {
84*d8b80295SApple OSS Distributions 		mach_msg_header_t header;
85*d8b80295SApple OSS Distributions 		mach_msg_body_t body;
86*d8b80295SApple OSS Distributions 		mach_msg_port_descriptor_t desc;
87*d8b80295SApple OSS Distributions 	} msg;
88*d8b80295SApple OSS Distributions 
89*d8b80295SApple OSS Distributions 	server_port = alloc_server_port();
90*d8b80295SApple OSS Distributions 	reply_port = alloc_reply_port();
91*d8b80295SApple OSS Distributions 
92*d8b80295SApple OSS Distributions 	msg.header.msgh_remote_port = server_port;
93*d8b80295SApple OSS Distributions 	msg.header.msgh_local_port = MACH_PORT_NULL;
94*d8b80295SApple OSS Distributions 	msg.header.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0) | MACH_MSGH_BITS_COMPLEX;
95*d8b80295SApple OSS Distributions 	msg.header.msgh_size = sizeof msg;
96*d8b80295SApple OSS Distributions 
97*d8b80295SApple OSS Distributions 	msg.body.msgh_descriptor_count = 1;
98*d8b80295SApple OSS Distributions 
99*d8b80295SApple OSS Distributions 	msg.desc.name = reply_port;
100*d8b80295SApple OSS Distributions 	msg.desc.disposition = MACH_MSG_TYPE_MOVE_RECEIVE;
101*d8b80295SApple OSS Distributions 	msg.desc.type = MACH_MSG_PORT_DESCRIPTOR;
102*d8b80295SApple OSS Distributions 	kr = mach_msg_send(&msg.header);
103*d8b80295SApple OSS Distributions 
104*d8b80295SApple OSS Distributions 	printf("[reply_port_defense_client test_immovable_receive_right]: mach_msg2() returned %d\n", kr);
105*d8b80295SApple OSS Distributions }
106*d8b80295SApple OSS Distributions 
107*d8b80295SApple OSS Distributions /* The only way you could create a send once right is when you send the port in local port of a mach msg with MAKE_SEND_ONCE disposition. */
108*d8b80295SApple OSS Distributions static void
test_make_send_once_right(void)109*d8b80295SApple OSS Distributions test_make_send_once_right(void)
110*d8b80295SApple OSS Distributions {
111*d8b80295SApple OSS Distributions 	kern_return_t kr;
112*d8b80295SApple OSS Distributions 	mach_port_t reply_port = alloc_reply_port();
113*d8b80295SApple OSS Distributions 	kr = mach_port_insert_right(mach_task_self(), reply_port, reply_port, MACH_MSG_TYPE_MAKE_SEND_ONCE);
114*d8b80295SApple OSS Distributions 	printf("[reply_port_defense_client test_make_send_once_right]: mach_port_insert_right() returned %d\n", kr);
115*d8b80295SApple OSS Distributions }
116*d8b80295SApple OSS Distributions 
117*d8b80295SApple OSS Distributions /* The send right of the port would only used for guarding a name in ipc space, it would not allow to send a message. */
118*d8b80295SApple OSS Distributions static void
test_using_send_right(void)119*d8b80295SApple OSS Distributions test_using_send_right(void)
120*d8b80295SApple OSS Distributions {
121*d8b80295SApple OSS Distributions 	kern_return_t kr;
122*d8b80295SApple OSS Distributions 	mach_port_t reply_port = alloc_reply_port();
123*d8b80295SApple OSS Distributions 	struct {
124*d8b80295SApple OSS Distributions 		mach_msg_header_t header;
125*d8b80295SApple OSS Distributions 		mach_msg_body_t body;
126*d8b80295SApple OSS Distributions 	} msg;
127*d8b80295SApple OSS Distributions 
128*d8b80295SApple OSS Distributions 	msg.header.msgh_remote_port = reply_port;
129*d8b80295SApple OSS Distributions 	msg.header.msgh_local_port = MACH_PORT_NULL;
130*d8b80295SApple OSS Distributions 	msg.header.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0);
131*d8b80295SApple OSS Distributions 	msg.header.msgh_size = sizeof msg;
132*d8b80295SApple OSS Distributions 
133*d8b80295SApple OSS Distributions 	kr = mach_msg_send(&msg.header);
134*d8b80295SApple OSS Distributions 	printf("[reply_port_defense_client test_using_send_right]: mach_msg2() returned %d\n", kr);
135*d8b80295SApple OSS Distributions }
136*d8b80295SApple OSS Distributions 
137*d8b80295SApple OSS Distributions /* The send right of the port would only used for guarding a name in ipc space, it would not allowed to get moved. */
138*d8b80295SApple OSS Distributions static void
test_move_send_right(void)139*d8b80295SApple OSS Distributions test_move_send_right(void)
140*d8b80295SApple OSS Distributions {
141*d8b80295SApple OSS Distributions 	kern_return_t kr;
142*d8b80295SApple OSS Distributions 	mach_port_t server_port = MACH_PORT_NULL, reply_port = MACH_PORT_NULL;
143*d8b80295SApple OSS Distributions 	struct {
144*d8b80295SApple OSS Distributions 		mach_msg_header_t header;
145*d8b80295SApple OSS Distributions 		mach_msg_body_t body;
146*d8b80295SApple OSS Distributions 		mach_msg_port_descriptor_t desc;
147*d8b80295SApple OSS Distributions 	} msg;
148*d8b80295SApple OSS Distributions 
149*d8b80295SApple OSS Distributions 	server_port = alloc_server_port();
150*d8b80295SApple OSS Distributions 	reply_port = alloc_reply_port();
151*d8b80295SApple OSS Distributions 
152*d8b80295SApple OSS Distributions 	msg.header.msgh_remote_port = server_port;
153*d8b80295SApple OSS Distributions 	msg.header.msgh_local_port = MACH_PORT_NULL;
154*d8b80295SApple OSS Distributions 	msg.header.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0) | MACH_MSGH_BITS_COMPLEX;
155*d8b80295SApple OSS Distributions 	msg.header.msgh_size = sizeof msg;
156*d8b80295SApple OSS Distributions 
157*d8b80295SApple OSS Distributions 	msg.body.msgh_descriptor_count = 1;
158*d8b80295SApple OSS Distributions 
159*d8b80295SApple OSS Distributions 	msg.desc.name = reply_port;
160*d8b80295SApple OSS Distributions 	msg.desc.disposition = MACH_MSG_TYPE_MOVE_SEND;
161*d8b80295SApple OSS Distributions 	msg.desc.type = MACH_MSG_PORT_DESCRIPTOR;
162*d8b80295SApple OSS Distributions 
163*d8b80295SApple OSS Distributions 	kr = mach_msg_send(&msg.header);
164*d8b80295SApple OSS Distributions 	printf("[reply_port_defense_client test_move_send_right]: mach_msg2() returned %d\n", kr);
165*d8b80295SApple OSS Distributions }
166*d8b80295SApple OSS Distributions 
167*d8b80295SApple OSS Distributions static void
test_move_provisional_reply_port(void)168*d8b80295SApple OSS Distributions test_move_provisional_reply_port(void)
169*d8b80295SApple OSS Distributions {
170*d8b80295SApple OSS Distributions 	kern_return_t kr;
171*d8b80295SApple OSS Distributions 	mach_port_t server_port = MACH_PORT_NULL, reply_port = MACH_PORT_NULL;
172*d8b80295SApple OSS Distributions 	struct {
173*d8b80295SApple OSS Distributions 		mach_msg_header_t header;
174*d8b80295SApple OSS Distributions 		mach_msg_body_t body;
175*d8b80295SApple OSS Distributions 		mach_msg_port_descriptor_t desc;
176*d8b80295SApple OSS Distributions 	} msg;
177*d8b80295SApple OSS Distributions 
178*d8b80295SApple OSS Distributions 	server_port = alloc_server_port();
179*d8b80295SApple OSS Distributions 	reply_port = alloc_provisional_reply_port();
180*d8b80295SApple OSS Distributions 
181*d8b80295SApple OSS Distributions 	msg.header.msgh_remote_port = server_port;
182*d8b80295SApple OSS Distributions 	msg.header.msgh_local_port = MACH_PORT_NULL;
183*d8b80295SApple OSS Distributions 	msg.header.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0) | MACH_MSGH_BITS_COMPLEX;
184*d8b80295SApple OSS Distributions 	msg.header.msgh_size = sizeof msg;
185*d8b80295SApple OSS Distributions 
186*d8b80295SApple OSS Distributions 	msg.body.msgh_descriptor_count = 1;
187*d8b80295SApple OSS Distributions 
188*d8b80295SApple OSS Distributions 	msg.desc.name = reply_port;
189*d8b80295SApple OSS Distributions 	msg.desc.disposition = MACH_MSG_TYPE_MOVE_RECEIVE;
190*d8b80295SApple OSS Distributions 	msg.desc.type = MACH_MSG_PORT_DESCRIPTOR;
191*d8b80295SApple OSS Distributions 
192*d8b80295SApple OSS Distributions 	kr = mach_msg_send(&msg.header);
193*d8b80295SApple OSS Distributions 
194*d8b80295SApple OSS Distributions 	printf("[reply_port_defense_client test_immovable_receive_right]: mach_msg2() returned %d\n", kr);
195*d8b80295SApple OSS Distributions }
196*d8b80295SApple OSS Distributions 
197*d8b80295SApple OSS Distributions static void
test_unentitled_thread_set_state(void)198*d8b80295SApple OSS Distributions test_unentitled_thread_set_state(void)
199*d8b80295SApple OSS Distributions {
200*d8b80295SApple OSS Distributions 	machine_thread_state_t ts;
201*d8b80295SApple OSS Distributions 	mach_msg_type_number_t count = MACHINE_THREAD_STATE_COUNT;
202*d8b80295SApple OSS Distributions 
203*d8b80295SApple OSS Distributions 	/* thread_set_state as a hardened binary should fail */
204*d8b80295SApple OSS Distributions 	kern_return_t kr = thread_get_state(mach_thread_self(), MACHINE_THREAD_STATE, (thread_state_t)&ts, &count);
205*d8b80295SApple OSS Distributions 
206*d8b80295SApple OSS Distributions 	kr = thread_set_state(mach_thread_self(), MACHINE_THREAD_STATE, (thread_state_t)&ts, count);
207*d8b80295SApple OSS Distributions 	assert(kr != KERN_SUCCESS);
208*d8b80295SApple OSS Distributions 	exit(-1); /* Should have crashed before here! */
209*d8b80295SApple OSS Distributions }
210*d8b80295SApple OSS Distributions 
211*d8b80295SApple OSS Distributions static void
test_unentitled_thread_set_exception_ports(void)212*d8b80295SApple OSS Distributions test_unentitled_thread_set_exception_ports(void)
213*d8b80295SApple OSS Distributions {
214*d8b80295SApple OSS Distributions 	mach_port_t exc_port = alloc_server_port();
215*d8b80295SApple OSS Distributions 
216*d8b80295SApple OSS Distributions 	/* thread_set_exception_ports as a hardened binary should fail */
217*d8b80295SApple OSS Distributions 	kern_return_t kr = thread_set_exception_ports(
218*d8b80295SApple OSS Distributions 		mach_thread_self(),
219*d8b80295SApple OSS Distributions 		EXC_MASK_ALL,
220*d8b80295SApple OSS Distributions 		exc_port,
221*d8b80295SApple OSS Distributions 		(exception_behavior_t)((unsigned int)EXCEPTION_DEFAULT | MACH_EXCEPTION_CODES),
222*d8b80295SApple OSS Distributions 		EXCEPTION_THREAD_STATE);
223*d8b80295SApple OSS Distributions 
224*d8b80295SApple OSS Distributions 	/* thread_set_exception_ports is supposed to crash, unless the policy is turned off.
225*d8b80295SApple OSS Distributions 	 * Things that disable the policy: AMFI boot-args in use, SIP disabled,
226*d8b80295SApple OSS Distributions 	 * third party plugins in a process. The caller of this client will check
227*d8b80295SApple OSS Distributions 	 * whether the test crashed and correctly adhered to these policies.
228*d8b80295SApple OSS Distributions 	 */
229*d8b80295SApple OSS Distributions 	printf("thread_set_exception_ports did not crash\n");
230*d8b80295SApple OSS Distributions }
231*d8b80295SApple OSS Distributions 
232*d8b80295SApple OSS Distributions int
main(int argc,char * argv[])233*d8b80295SApple OSS Distributions main(int argc, char *argv[])
234*d8b80295SApple OSS Distributions {
235*d8b80295SApple OSS Distributions 	uint32_t my_csflags = 0;
236*d8b80295SApple OSS Distributions 	bool thirdparty_hardened = !strcmp(argv[0], "./reply_port_defense_client_3P_hardened");
237*d8b80295SApple OSS Distributions 
238*d8b80295SApple OSS Distributions 	if (my_csflags & CS_PLATFORM_BINARY == thirdparty_hardened) {
239*d8b80295SApple OSS Distributions 		printf("platform binary does not match expected\n");
240*d8b80295SApple OSS Distributions 		return -1;
241*d8b80295SApple OSS Distributions 	}
242*d8b80295SApple OSS Distributions 
243*d8b80295SApple OSS Distributions 
244*d8b80295SApple OSS Distributions 	void (*tests[MAX_TEST_NUM])(void) = {
245*d8b80295SApple OSS Distributions 		test_immovable_receive_right, /* 0 */
246*d8b80295SApple OSS Distributions 		test_make_send_once_right,
247*d8b80295SApple OSS Distributions 		test_using_send_right,
248*d8b80295SApple OSS Distributions 		test_move_send_right,
249*d8b80295SApple OSS Distributions 		test_move_provisional_reply_port,
250*d8b80295SApple OSS Distributions 		test_unentitled_thread_set_exception_ports,
251*d8b80295SApple OSS Distributions 		test_unentitled_thread_set_state /* 6 */
252*d8b80295SApple OSS Distributions 	};
253*d8b80295SApple OSS Distributions 
254*d8b80295SApple OSS Distributions 	if (argc < 2) {
255*d8b80295SApple OSS Distributions 		printf("[reply_port_defense_client]: Specify a test to run.");
256*d8b80295SApple OSS Distributions 		exit(-1);
257*d8b80295SApple OSS Distributions 	}
258*d8b80295SApple OSS Distributions 
259*d8b80295SApple OSS Distributions 	int test_num = atoi(argv[1]);
260*d8b80295SApple OSS Distributions 	printf("[reply_port_defense_client]: My Pid: %d Test num: %d third_party_hardened: %s\n",
261*d8b80295SApple OSS Distributions 	    getpid(), test_num, thirdparty_hardened ? "yes" : "no");
262*d8b80295SApple OSS Distributions 	fflush(stdout);
263*d8b80295SApple OSS Distributions 	if (test_num >= 0 && test_num < MAX_TEST_NUM) {
264*d8b80295SApple OSS Distributions 		(*tests[test_num])();
265*d8b80295SApple OSS Distributions 	} else {
266*d8b80295SApple OSS Distributions 		printf("[reply_port_defense_client]: Invalid test num. Exiting...\n");
267*d8b80295SApple OSS Distributions 		exit(-1);
268*d8b80295SApple OSS Distributions 	}
269*d8b80295SApple OSS Distributions 	printf("Child exiting cleanly!!\n");
270*d8b80295SApple OSS Distributions 	fflush(stdout);
271*d8b80295SApple OSS Distributions 	// return 0;
272*d8b80295SApple OSS Distributions 	exit(0);
273*d8b80295SApple OSS Distributions }
274