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