xref: /xnu-11215.81.4/tests/immovable_send.c (revision d4514f0bc1d3f944c22d92e68b646ac3fb40d452)
1*d4514f0bSApple OSS Distributions #include <darwintest.h>
2*d4514f0bSApple OSS Distributions #include <servers/bootstrap.h>
3*d4514f0bSApple OSS Distributions #include <mach/mach.h>
4*d4514f0bSApple OSS Distributions #include <mach/message.h>
5*d4514f0bSApple OSS Distributions #include <stdlib.h>
6*d4514f0bSApple OSS Distributions #include <sys/sysctl.h>
7*d4514f0bSApple OSS Distributions #include <unistd.h>
8*d4514f0bSApple OSS Distributions #include <darwintest_multiprocess.h>
9*d4514f0bSApple OSS Distributions #include <excserver.h>
10*d4514f0bSApple OSS Distributions #include <spawn.h>
11*d4514f0bSApple OSS Distributions #include <spawn_private.h>
12*d4514f0bSApple OSS Distributions #include <libproc_internal.h>
13*d4514f0bSApple OSS Distributions #include <signal.h>
14*d4514f0bSApple OSS Distributions 
15*d4514f0bSApple OSS Distributions #include <IOKit/IOKitLib.h>
16*d4514f0bSApple OSS Distributions 
17*d4514f0bSApple OSS Distributions T_GLOBAL_META(
18*d4514f0bSApple OSS Distributions 	T_META_NAMESPACE("xnu.ipc"),
19*d4514f0bSApple OSS Distributions 	T_META_RUN_CONCURRENTLY(TRUE),
20*d4514f0bSApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
21*d4514f0bSApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("IPC"),
22*d4514f0bSApple OSS Distributions 	T_META_TAG_VM_PREFERRED);
23*d4514f0bSApple OSS Distributions 
24*d4514f0bSApple OSS Distributions #define TASK_EXC_GUARD_MP_DELIVER 0x10
25*d4514f0bSApple OSS Distributions #define MAX_ARGV 2
26*d4514f0bSApple OSS Distributions 
27*d4514f0bSApple OSS Distributions extern char **environ;
28*d4514f0bSApple OSS Distributions 
29*d4514f0bSApple OSS Distributions kern_return_t
catch_mach_exception_raise_state(mach_port_t exception_port,exception_type_t exception,const mach_exception_data_t code,mach_msg_type_number_t code_count,int * flavor,const thread_state_t old_state,mach_msg_type_number_t old_state_count,thread_state_t new_state,mach_msg_type_number_t * new_state_count)30*d4514f0bSApple OSS Distributions catch_mach_exception_raise_state(mach_port_t exception_port,
31*d4514f0bSApple OSS Distributions     exception_type_t exception,
32*d4514f0bSApple OSS Distributions     const mach_exception_data_t code,
33*d4514f0bSApple OSS Distributions     mach_msg_type_number_t code_count,
34*d4514f0bSApple OSS Distributions     int * flavor,
35*d4514f0bSApple OSS Distributions     const thread_state_t old_state,
36*d4514f0bSApple OSS Distributions     mach_msg_type_number_t old_state_count,
37*d4514f0bSApple OSS Distributions     thread_state_t new_state,
38*d4514f0bSApple OSS Distributions     mach_msg_type_number_t * new_state_count)
39*d4514f0bSApple OSS Distributions {
40*d4514f0bSApple OSS Distributions #pragma unused(exception_port, exception, code, code_count, flavor, old_state, old_state_count, new_state, new_state_count)
41*d4514f0bSApple OSS Distributions 	T_FAIL("Unsupported catch_mach_exception_raise_state");
42*d4514f0bSApple OSS Distributions 	return KERN_NOT_SUPPORTED;
43*d4514f0bSApple OSS Distributions }
44*d4514f0bSApple OSS Distributions 
45*d4514f0bSApple OSS Distributions kern_return_t
catch_mach_exception_raise_state_identity(mach_port_t exception_port,mach_port_t thread,mach_port_t task,exception_type_t exception,mach_exception_data_t code,mach_msg_type_number_t code_count,int * flavor,thread_state_t old_state,mach_msg_type_number_t old_state_count,thread_state_t new_state,mach_msg_type_number_t * new_state_count)46*d4514f0bSApple OSS Distributions catch_mach_exception_raise_state_identity(mach_port_t exception_port,
47*d4514f0bSApple OSS Distributions     mach_port_t thread,
48*d4514f0bSApple OSS Distributions     mach_port_t task,
49*d4514f0bSApple OSS Distributions     exception_type_t exception,
50*d4514f0bSApple OSS Distributions     mach_exception_data_t code,
51*d4514f0bSApple OSS Distributions     mach_msg_type_number_t code_count,
52*d4514f0bSApple OSS Distributions     int * flavor,
53*d4514f0bSApple OSS Distributions     thread_state_t old_state,
54*d4514f0bSApple OSS Distributions     mach_msg_type_number_t old_state_count,
55*d4514f0bSApple OSS Distributions     thread_state_t new_state,
56*d4514f0bSApple OSS Distributions     mach_msg_type_number_t * new_state_count)
57*d4514f0bSApple OSS Distributions {
58*d4514f0bSApple OSS Distributions #pragma unused(exception_port, thread, task, exception, code, code_count, flavor, old_state, old_state_count, new_state, new_state_count)
59*d4514f0bSApple OSS Distributions 	T_FAIL("Unsupported catch_mach_exception_raise_state_identity");
60*d4514f0bSApple OSS Distributions 	return KERN_NOT_SUPPORTED;
61*d4514f0bSApple OSS Distributions }
62*d4514f0bSApple OSS Distributions 
63*d4514f0bSApple OSS Distributions kern_return_t
catch_mach_exception_raise(mach_port_t exception_port,mach_port_t thread,mach_port_t task,exception_type_t exception,mach_exception_data_t code,mach_msg_type_number_t code_count)64*d4514f0bSApple OSS Distributions catch_mach_exception_raise(mach_port_t exception_port,
65*d4514f0bSApple OSS Distributions     mach_port_t thread,
66*d4514f0bSApple OSS Distributions     mach_port_t task,
67*d4514f0bSApple OSS Distributions     exception_type_t exception,
68*d4514f0bSApple OSS Distributions     mach_exception_data_t code,
69*d4514f0bSApple OSS Distributions     mach_msg_type_number_t code_count)
70*d4514f0bSApple OSS Distributions {
71*d4514f0bSApple OSS Distributions #pragma unused(exception_port, task, thread, code_count)
72*d4514f0bSApple OSS Distributions 	T_ASSERT_EQ(exception, EXC_GUARD, "exception type");
73*d4514f0bSApple OSS Distributions 	T_LOG("Exception raised with exception code : %llx\n", *code);
74*d4514f0bSApple OSS Distributions 	T_END;
75*d4514f0bSApple OSS Distributions 	return KERN_SUCCESS;
76*d4514f0bSApple OSS Distributions }
77*d4514f0bSApple OSS Distributions 
78*d4514f0bSApple OSS Distributions typedef struct {
79*d4514f0bSApple OSS Distributions 	mach_msg_header_t   header;
80*d4514f0bSApple OSS Distributions 	mach_msg_body_t     body;
81*d4514f0bSApple OSS Distributions 	mach_msg_port_descriptor_t port_descriptor;
82*d4514f0bSApple OSS Distributions 	mach_msg_trailer_t  trailer;            // subtract this when sending
83*d4514f0bSApple OSS Distributions } ipc_complex_message;
84*d4514f0bSApple OSS Distributions 
85*d4514f0bSApple OSS Distributions struct args {
86*d4514f0bSApple OSS Distributions 	char *server_port_name;
87*d4514f0bSApple OSS Distributions 	mach_port_t server_port;
88*d4514f0bSApple OSS Distributions };
89*d4514f0bSApple OSS Distributions 
90*d4514f0bSApple OSS Distributions void parse_args(struct args *args);
91*d4514f0bSApple OSS Distributions void server_setup(struct args* args);
92*d4514f0bSApple OSS Distributions void* exception_server_thread(void *arg);
93*d4514f0bSApple OSS Distributions mach_port_t create_exception_port(void);
94*d4514f0bSApple OSS Distributions 
95*d4514f0bSApple OSS Distributions #define TEST_TIMEOUT    10
96*d4514f0bSApple OSS Distributions 
97*d4514f0bSApple OSS Distributions void
parse_args(struct args * args)98*d4514f0bSApple OSS Distributions parse_args(struct args *args)
99*d4514f0bSApple OSS Distributions {
100*d4514f0bSApple OSS Distributions 	args->server_port_name = "TEST_IMMOVABLE_SEND";
101*d4514f0bSApple OSS Distributions 	args->server_port = MACH_PORT_NULL;
102*d4514f0bSApple OSS Distributions }
103*d4514f0bSApple OSS Distributions 
104*d4514f0bSApple OSS Distributions /* Create a mach IPC listener which will respond to the client's message */
105*d4514f0bSApple OSS Distributions void
server_setup(struct args * args)106*d4514f0bSApple OSS Distributions server_setup(struct args *args)
107*d4514f0bSApple OSS Distributions {
108*d4514f0bSApple OSS Distributions 	kern_return_t ret;
109*d4514f0bSApple OSS Distributions 	mach_port_t bsport;
110*d4514f0bSApple OSS Distributions 
111*d4514f0bSApple OSS Distributions 	ret = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE,
112*d4514f0bSApple OSS Distributions 	    &args->server_port);
113*d4514f0bSApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(ret, "server: mach_port_allocate()");
114*d4514f0bSApple OSS Distributions 
115*d4514f0bSApple OSS Distributions 	ret = mach_port_insert_right(mach_task_self(), args->server_port, args->server_port,
116*d4514f0bSApple OSS Distributions 	    MACH_MSG_TYPE_MAKE_SEND);
117*d4514f0bSApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(ret, "server: mach_port_insert_right()");
118*d4514f0bSApple OSS Distributions 
119*d4514f0bSApple OSS Distributions 	ret = task_get_bootstrap_port(mach_task_self(), &bsport);
120*d4514f0bSApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(ret, "server: task_get_bootstrap_port()");
121*d4514f0bSApple OSS Distributions 
122*d4514f0bSApple OSS Distributions 	ret = bootstrap_register(bsport, args->server_port_name, args->server_port);
123*d4514f0bSApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(ret, "server: bootstrap_register()");
124*d4514f0bSApple OSS Distributions 
125*d4514f0bSApple OSS Distributions 	T_LOG("server: waiting for IPC messages from client on port '%s'.\n",
126*d4514f0bSApple OSS Distributions 	    args->server_port_name);
127*d4514f0bSApple OSS Distributions }
128*d4514f0bSApple OSS Distributions 
129*d4514f0bSApple OSS Distributions mach_port_t
create_exception_port()130*d4514f0bSApple OSS Distributions create_exception_port()
131*d4514f0bSApple OSS Distributions {
132*d4514f0bSApple OSS Distributions 	kern_return_t kret;
133*d4514f0bSApple OSS Distributions 	mach_port_t exc_port = MACH_PORT_NULL;
134*d4514f0bSApple OSS Distributions 	mach_port_t task = mach_task_self();
135*d4514f0bSApple OSS Distributions 
136*d4514f0bSApple OSS Distributions 	kret = mach_port_allocate(task, MACH_PORT_RIGHT_RECEIVE, &exc_port);
137*d4514f0bSApple OSS Distributions 	T_EXPECT_MACH_SUCCESS(kret, "mach_port_allocate exc_port");
138*d4514f0bSApple OSS Distributions 
139*d4514f0bSApple OSS Distributions 	kret = mach_port_insert_right(task, exc_port, exc_port, MACH_MSG_TYPE_MAKE_SEND);
140*d4514f0bSApple OSS Distributions 	T_EXPECT_MACH_SUCCESS(kret, "mach_port_insert_right exc_port");
141*d4514f0bSApple OSS Distributions 
142*d4514f0bSApple OSS Distributions 	return exc_port;
143*d4514f0bSApple OSS Distributions }
144*d4514f0bSApple OSS Distributions 
145*d4514f0bSApple OSS Distributions void *
exception_server_thread(void * arg)146*d4514f0bSApple OSS Distributions exception_server_thread(void *arg)
147*d4514f0bSApple OSS Distributions {
148*d4514f0bSApple OSS Distributions 	kern_return_t kr;
149*d4514f0bSApple OSS Distributions 	mach_port_t exc_port = *(mach_port_t *)arg;
150*d4514f0bSApple OSS Distributions 	T_EXPECT_NE(exc_port, MACH_PORT_NULL, "exception port is not null");
151*d4514f0bSApple OSS Distributions 
152*d4514f0bSApple OSS Distributions 	/* Handle exceptions on exc_port */
153*d4514f0bSApple OSS Distributions 	kr = mach_msg_server(mach_exc_server, 4096, exc_port, 0);
154*d4514f0bSApple OSS Distributions 	T_EXPECT_MACH_SUCCESS(kr, "mach_msg_server");
155*d4514f0bSApple OSS Distributions 
156*d4514f0bSApple OSS Distributions 	return NULL;
157*d4514f0bSApple OSS Distributions }
158*d4514f0bSApple OSS Distributions 
159*d4514f0bSApple OSS Distributions T_DECL(catch_immovable_send_exception, "Send guard port descriptor to another process", T_META_IGNORECRASHES(".*immovable_send_client.*"))
160*d4514f0bSApple OSS Distributions {
161*d4514f0bSApple OSS Distributions 	uint32_t task_exc_guard = 0;
162*d4514f0bSApple OSS Distributions 	size_t te_size = sizeof(&task_exc_guard);
163*d4514f0bSApple OSS Distributions 	kern_return_t kr;
164*d4514f0bSApple OSS Distributions 	mach_msg_type_number_t  maskCount = 1;
165*d4514f0bSApple OSS Distributions 	exception_mask_t        mask;
166*d4514f0bSApple OSS Distributions 	exception_handler_t     handler;
167*d4514f0bSApple OSS Distributions 	exception_behavior_t    behavior;
168*d4514f0bSApple OSS Distributions 	thread_state_flavor_t   flavor;
169*d4514f0bSApple OSS Distributions 	mach_port_t             task = mach_task_self();
170*d4514f0bSApple OSS Distributions 	struct args*            server_args = (struct args*)malloc(sizeof(struct args));
171*d4514f0bSApple OSS Distributions 	posix_spawnattr_t       attrs;
172*d4514f0bSApple OSS Distributions 	char *test_prog_name = "./immovable_send_client";
173*d4514f0bSApple OSS Distributions 	char *child_args[MAX_ARGV];
174*d4514f0bSApple OSS Distributions 
175*d4514f0bSApple OSS Distributions 	T_LOG("Check if task_exc_guard exception has been enabled\n");
176*d4514f0bSApple OSS Distributions 	sysctlbyname("kern.task_exc_guard_default", &task_exc_guard, &te_size, NULL, 0);
177*d4514f0bSApple OSS Distributions 	//TODO: check if sysctlbyname is successful
178*d4514f0bSApple OSS Distributions 
179*d4514f0bSApple OSS Distributions 	/* Create the bootstrap port */
180*d4514f0bSApple OSS Distributions 	parse_args(server_args);
181*d4514f0bSApple OSS Distributions 	server_setup(server_args);
182*d4514f0bSApple OSS Distributions 
183*d4514f0bSApple OSS Distributions 	/* Create the exception port for the server */
184*d4514f0bSApple OSS Distributions 	mach_port_t exc_port = create_exception_port();
185*d4514f0bSApple OSS Distributions 	T_EXPECT_NOTNULL(exc_port, "Create a new exception port");
186*d4514f0bSApple OSS Distributions 
187*d4514f0bSApple OSS Distributions 	pthread_t s_exc_thread;
188*d4514f0bSApple OSS Distributions 
189*d4514f0bSApple OSS Distributions 	/* Create exception serving thread */
190*d4514f0bSApple OSS Distributions 	int ret = pthread_create(&s_exc_thread, NULL, exception_server_thread, &exc_port);
191*d4514f0bSApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(ret, "pthread_create exception_server_thread");
192*d4514f0bSApple OSS Distributions 
193*d4514f0bSApple OSS Distributions 	/* Get current exception ports */
194*d4514f0bSApple OSS Distributions 	kr = task_get_exception_ports(task, EXC_MASK_GUARD, &mask,
195*d4514f0bSApple OSS Distributions 	    &maskCount, &handler, &behavior, &flavor);
196*d4514f0bSApple OSS Distributions 	T_EXPECT_MACH_SUCCESS(kr, "task_get_exception_ports");
197*d4514f0bSApple OSS Distributions 
198*d4514f0bSApple OSS Distributions 	/* Initialize posix_spawn attributes */
199*d4514f0bSApple OSS Distributions 	posix_spawnattr_init(&attrs);
200*d4514f0bSApple OSS Distributions 
201*d4514f0bSApple OSS Distributions 	int err = posix_spawnattr_setexceptionports_np(&attrs, EXC_MASK_GUARD, exc_port,
202*d4514f0bSApple OSS Distributions 	    (exception_behavior_t) (EXCEPTION_DEFAULT | MACH_EXCEPTION_CODES), 0);
203*d4514f0bSApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(err, "posix_spawnattr_setflags");
204*d4514f0bSApple OSS Distributions 
205*d4514f0bSApple OSS Distributions 	child_args[0] = test_prog_name;
206*d4514f0bSApple OSS Distributions 	child_args[1] = NULL;
207*d4514f0bSApple OSS Distributions 
208*d4514f0bSApple OSS Distributions 	err = posix_spawn(NULL, child_args[0], NULL, &attrs, &child_args[0], environ);
209*d4514f0bSApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(err, "posix_spawn immovable_send_client");
210*d4514f0bSApple OSS Distributions 
211*d4514f0bSApple OSS Distributions 	int child_status;
212*d4514f0bSApple OSS Distributions 	/* Wait for child and check for exception */
213*d4514f0bSApple OSS Distributions 	if (-1 == wait4(-1, &child_status, 0, NULL)) {
214*d4514f0bSApple OSS Distributions 		T_FAIL("wait4: child mia");
215*d4514f0bSApple OSS Distributions 	}
216*d4514f0bSApple OSS Distributions 
217*d4514f0bSApple OSS Distributions 	if (WIFEXITED(child_status) && WEXITSTATUS(child_status)) {
218*d4514f0bSApple OSS Distributions 		T_LOG("Child exited with status = %x", child_status);
219*d4514f0bSApple OSS Distributions 	}
220*d4514f0bSApple OSS Distributions 
221*d4514f0bSApple OSS Distributions 	sigsuspend(0);
222*d4514f0bSApple OSS Distributions }
223