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