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