xref: /xnu-8796.141.3/tests/reply_port_defense.c (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions #include <stdio.h>
2*1b191cb5SApple OSS Distributions #include <stdlib.h>
3*1b191cb5SApple OSS Distributions #include <unistd.h>
4*1b191cb5SApple OSS Distributions #include <darwintest.h>
5*1b191cb5SApple OSS Distributions #include <spawn.h>
6*1b191cb5SApple OSS Distributions #include <mach/mach.h>
7*1b191cb5SApple OSS Distributions #include <sys/sysctl.h>
8*1b191cb5SApple OSS Distributions #include <excserver.h>
9*1b191cb5SApple OSS Distributions #include <signal.h>
10*1b191cb5SApple OSS Distributions #include "../osfmk/ipc/ipc_init.h"
11*1b191cb5SApple OSS Distributions #include "../osfmk/mach/port.h"
12*1b191cb5SApple OSS Distributions #include "../osfmk/kern/exc_guard.h"
13*1b191cb5SApple OSS Distributions 
14*1b191cb5SApple OSS Distributions #define MAX_TEST_NUM 4
15*1b191cb5SApple OSS Distributions #define MAX_ARGV 3
16*1b191cb5SApple OSS Distributions 
17*1b191cb5SApple OSS Distributions extern char **environ;
18*1b191cb5SApple OSS Distributions static mach_exception_data_type_t received_exception_code = 0;
19*1b191cb5SApple OSS Distributions static mach_exception_data_type_t expected_exception_code = 0;
20*1b191cb5SApple OSS Distributions static exception_type_t exception_taken = 0;
21*1b191cb5SApple OSS Distributions 
22*1b191cb5SApple OSS Distributions /*
23*1b191cb5SApple OSS Distributions  * This test infrastructure is inspired from imm_pinned_control_port.c.
24*1b191cb5SApple OSS Distributions  * It verifies no reply port security semantics are violated.
25*1b191cb5SApple OSS Distributions  *
26*1b191cb5SApple OSS Distributions  * 1. The rcv right of the port would be marked immovable.
27*1b191cb5SApple OSS Distributions  */
28*1b191cb5SApple OSS Distributions T_GLOBAL_META(
29*1b191cb5SApple OSS Distributions 	T_META_NAMESPACE("xnu.ipc"),
30*1b191cb5SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
31*1b191cb5SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("IPC"),
32*1b191cb5SApple OSS Distributions 	T_META_RUN_CONCURRENTLY(TRUE));
33*1b191cb5SApple OSS Distributions 
34*1b191cb5SApple OSS Distributions static mach_port_t
alloc_exception_port(void)35*1b191cb5SApple OSS Distributions alloc_exception_port(void)
36*1b191cb5SApple OSS Distributions {
37*1b191cb5SApple OSS Distributions 	kern_return_t kret;
38*1b191cb5SApple OSS Distributions 	mach_port_t exc_port = MACH_PORT_NULL;
39*1b191cb5SApple OSS Distributions 	mach_port_t task = mach_task_self();
40*1b191cb5SApple OSS Distributions 
41*1b191cb5SApple OSS Distributions 	kret = mach_port_allocate(task, MACH_PORT_RIGHT_RECEIVE, &exc_port);
42*1b191cb5SApple OSS Distributions 	T_QUIET; T_EXPECT_MACH_SUCCESS(kret, "mach_port_allocate exc_port");
43*1b191cb5SApple OSS Distributions 
44*1b191cb5SApple OSS Distributions 	kret = mach_port_insert_right(task, exc_port, exc_port, MACH_MSG_TYPE_MAKE_SEND);
45*1b191cb5SApple OSS Distributions 	T_QUIET; T_EXPECT_MACH_SUCCESS(kret, "mach_port_insert_right exc_port");
46*1b191cb5SApple OSS Distributions 
47*1b191cb5SApple OSS Distributions 	return exc_port;
48*1b191cb5SApple OSS Distributions }
49*1b191cb5SApple OSS Distributions 
50*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)51*1b191cb5SApple OSS Distributions catch_mach_exception_raise_state(mach_port_t exception_port,
52*1b191cb5SApple OSS Distributions     exception_type_t exception,
53*1b191cb5SApple OSS Distributions     const mach_exception_data_t code,
54*1b191cb5SApple OSS Distributions     mach_msg_type_number_t code_count,
55*1b191cb5SApple OSS Distributions     int * flavor,
56*1b191cb5SApple OSS Distributions     const thread_state_t old_state,
57*1b191cb5SApple OSS Distributions     mach_msg_type_number_t old_state_count,
58*1b191cb5SApple OSS Distributions     thread_state_t new_state,
59*1b191cb5SApple OSS Distributions     mach_msg_type_number_t * new_state_count)
60*1b191cb5SApple OSS Distributions {
61*1b191cb5SApple OSS Distributions #pragma unused(exception_port, exception, code, code_count, flavor, old_state, old_state_count, new_state, new_state_count)
62*1b191cb5SApple OSS Distributions 	T_FAIL("Unsupported catch_mach_exception_raise_state");
63*1b191cb5SApple OSS Distributions 	return KERN_NOT_SUPPORTED;
64*1b191cb5SApple OSS Distributions }
65*1b191cb5SApple OSS Distributions 
66*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)67*1b191cb5SApple OSS Distributions catch_mach_exception_raise_state_identity(mach_port_t exception_port,
68*1b191cb5SApple OSS Distributions     mach_port_t thread,
69*1b191cb5SApple OSS Distributions     mach_port_t task,
70*1b191cb5SApple OSS Distributions     exception_type_t exception,
71*1b191cb5SApple OSS Distributions     mach_exception_data_t code,
72*1b191cb5SApple OSS Distributions     mach_msg_type_number_t code_count,
73*1b191cb5SApple OSS Distributions     int * flavor,
74*1b191cb5SApple OSS Distributions     thread_state_t old_state,
75*1b191cb5SApple OSS Distributions     mach_msg_type_number_t old_state_count,
76*1b191cb5SApple OSS Distributions     thread_state_t new_state,
77*1b191cb5SApple OSS Distributions     mach_msg_type_number_t * new_state_count)
78*1b191cb5SApple OSS Distributions {
79*1b191cb5SApple OSS Distributions #pragma unused(exception_port, thread, task, exception, code, code_count, flavor, old_state, old_state_count, new_state, new_state_count)
80*1b191cb5SApple OSS Distributions 	T_FAIL("Unsupported catch_mach_exception_raise_state_identity");
81*1b191cb5SApple OSS Distributions 	return KERN_NOT_SUPPORTED;
82*1b191cb5SApple OSS Distributions }
83*1b191cb5SApple OSS Distributions 
84*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)85*1b191cb5SApple OSS Distributions catch_mach_exception_raise(mach_port_t exception_port,
86*1b191cb5SApple OSS Distributions     mach_port_t thread,
87*1b191cb5SApple OSS Distributions     mach_port_t task,
88*1b191cb5SApple OSS Distributions     exception_type_t exception,
89*1b191cb5SApple OSS Distributions     mach_exception_data_t code,
90*1b191cb5SApple OSS Distributions     mach_msg_type_number_t code_count)
91*1b191cb5SApple OSS Distributions {
92*1b191cb5SApple OSS Distributions #pragma unused(exception_port, code_count)
93*1b191cb5SApple OSS Distributions 	kern_return_t kr;
94*1b191cb5SApple OSS Distributions 
95*1b191cb5SApple OSS Distributions 	kr = mach_port_deallocate(mach_task_self(), thread);
96*1b191cb5SApple OSS Distributions 	T_QUIET; T_EXPECT_MACH_SUCCESS(kr, "mach_port_deallocate");
97*1b191cb5SApple OSS Distributions 	kr = mach_port_deallocate(mach_task_self(), task);
98*1b191cb5SApple OSS Distributions 	T_QUIET; T_EXPECT_MACH_SUCCESS(kr, "mach_port_deallocate");
99*1b191cb5SApple OSS Distributions 
100*1b191cb5SApple OSS Distributions 	T_LOG("Caught exception type: %d code: 0x%llx", exception, *code);
101*1b191cb5SApple OSS Distributions 	exception_taken = exception;
102*1b191cb5SApple OSS Distributions 	if (exception == EXC_GUARD) {
103*1b191cb5SApple OSS Distributions 		received_exception_code = EXC_GUARD_DECODE_GUARD_FLAVOR( *((uint64_t *)code));
104*1b191cb5SApple OSS Distributions 	} else if (exception == EXC_CORPSE_NOTIFY) {
105*1b191cb5SApple OSS Distributions 		received_exception_code = *code;
106*1b191cb5SApple OSS Distributions 	} else {
107*1b191cb5SApple OSS Distributions 		T_FAIL("Unexpected exception");
108*1b191cb5SApple OSS Distributions 	}
109*1b191cb5SApple OSS Distributions 	return KERN_SUCCESS;
110*1b191cb5SApple OSS Distributions }
111*1b191cb5SApple OSS Distributions 
112*1b191cb5SApple OSS Distributions static void *
exception_server_thread(void * arg)113*1b191cb5SApple OSS Distributions exception_server_thread(void *arg)
114*1b191cb5SApple OSS Distributions {
115*1b191cb5SApple OSS Distributions 	kern_return_t kr;
116*1b191cb5SApple OSS Distributions 	mach_port_t exc_port = *(mach_port_t *)arg;
117*1b191cb5SApple OSS Distributions 
118*1b191cb5SApple OSS Distributions 	/* Handle exceptions on exc_port */
119*1b191cb5SApple OSS Distributions 	kr = mach_msg_server_once(mach_exc_server, 4096, exc_port, 0);
120*1b191cb5SApple OSS Distributions 	T_QUIET; T_EXPECT_MACH_SUCCESS(kr, "mach_msg_server_once");
121*1b191cb5SApple OSS Distributions 
122*1b191cb5SApple OSS Distributions 	return NULL;
123*1b191cb5SApple OSS Distributions }
124*1b191cb5SApple OSS Distributions 
125*1b191cb5SApple OSS Distributions T_DECL(reply_port_defense, "Test reply port semantics violations", T_META_IGNORECRASHES(".*reply_port_defense_client.*"), T_META_CHECK_LEAKS(false))
126*1b191cb5SApple OSS Distributions {
127*1b191cb5SApple OSS Distributions 	int ret = 0;
128*1b191cb5SApple OSS Distributions 
129*1b191cb5SApple OSS Distributions 	uint32_t task_exc_guard = 0;
130*1b191cb5SApple OSS Distributions 	size_t te_size = sizeof(&task_exc_guard);
131*1b191cb5SApple OSS Distributions 
132*1b191cb5SApple OSS Distributions 	char *test_prog_name = "./reply_port_defense_client";
133*1b191cb5SApple OSS Distributions 	char *child_args[MAX_ARGV];
134*1b191cb5SApple OSS Distributions 	pid_t client_pid = 0;
135*1b191cb5SApple OSS Distributions 	posix_spawnattr_t attrs;
136*1b191cb5SApple OSS Distributions 
137*1b191cb5SApple OSS Distributions 	pthread_t s_exc_thread;
138*1b191cb5SApple OSS Distributions 	mach_port_t exc_port;
139*1b191cb5SApple OSS Distributions 
140*1b191cb5SApple OSS Distributions 	T_LOG("Check if task_exc_guard exception has been enabled\n");
141*1b191cb5SApple OSS Distributions 	ret = sysctlbyname("kern.task_exc_guard_default", &task_exc_guard, &te_size, NULL, 0);
142*1b191cb5SApple OSS Distributions 	T_ASSERT_EQ(ret, 0, "sysctlbyname");
143*1b191cb5SApple OSS Distributions 
144*1b191cb5SApple OSS Distributions 	if (!(task_exc_guard & TASK_EXC_GUARD_MP_DELIVER)) {
145*1b191cb5SApple OSS Distributions 		T_SKIP("task_exc_guard exception is not enabled");
146*1b191cb5SApple OSS Distributions 	}
147*1b191cb5SApple OSS Distributions 
148*1b191cb5SApple OSS Distributions 	for (int i = 0; i < MAX_TEST_NUM; i++) {
149*1b191cb5SApple OSS Distributions 		exc_port = alloc_exception_port();
150*1b191cb5SApple OSS Distributions 		T_QUIET; T_ASSERT_NE(exc_port, MACH_PORT_NULL, "Create a new exception port");
151*1b191cb5SApple OSS Distributions 
152*1b191cb5SApple OSS Distributions 		/* Create exception serving thread */
153*1b191cb5SApple OSS Distributions 		ret = pthread_create(&s_exc_thread, NULL, exception_server_thread, &exc_port);
154*1b191cb5SApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "pthread_create exception_server_thread");
155*1b191cb5SApple OSS Distributions 
156*1b191cb5SApple OSS Distributions 		/* Initialize posix_spawn attributes */
157*1b191cb5SApple OSS Distributions 		posix_spawnattr_init(&attrs);
158*1b191cb5SApple OSS Distributions 
159*1b191cb5SApple OSS Distributions 		int err = posix_spawnattr_setexceptionports_np(&attrs, EXC_MASK_GUARD | EXC_MASK_CORPSE_NOTIFY, exc_port,
160*1b191cb5SApple OSS Distributions 		    (exception_behavior_t) (EXCEPTION_DEFAULT | MACH_EXCEPTION_CODES), 0);
161*1b191cb5SApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(err, "posix_spawnattr_setflags");
162*1b191cb5SApple OSS Distributions 
163*1b191cb5SApple OSS Distributions 		child_args[0] = test_prog_name;
164*1b191cb5SApple OSS Distributions 		char test_num[10];
165*1b191cb5SApple OSS Distributions 		sprintf(test_num, "%d", i);
166*1b191cb5SApple OSS Distributions 		child_args[1] = test_num;
167*1b191cb5SApple OSS Distributions 		child_args[2] = NULL;
168*1b191cb5SApple OSS Distributions 
169*1b191cb5SApple OSS Distributions 		T_LOG("========== Spawning new child ==========");
170*1b191cb5SApple OSS Distributions 		err = posix_spawn(&client_pid, child_args[0], NULL, &attrs, &child_args[0], environ);
171*1b191cb5SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(err, "posix_spawn reply_port_defense_client = %d", client_pid);
172*1b191cb5SApple OSS Distributions 
173*1b191cb5SApple OSS Distributions 		int child_status;
174*1b191cb5SApple OSS Distributions 		/* Wait for child and check for exception */
175*1b191cb5SApple OSS Distributions 		if (-1 == waitpid(-1, &child_status, 0)) {
176*1b191cb5SApple OSS Distributions 			T_FAIL("waitpid: child");
177*1b191cb5SApple OSS Distributions 		}
178*1b191cb5SApple OSS Distributions 		if (WIFEXITED(child_status) && WEXITSTATUS(child_status)) {
179*1b191cb5SApple OSS Distributions 			T_FAIL("Child exited with status = %x", child_status);
180*1b191cb5SApple OSS Distributions 			T_END;
181*1b191cb5SApple OSS Distributions 		}
182*1b191cb5SApple OSS Distributions 
183*1b191cb5SApple OSS Distributions 		sleep(1);
184*1b191cb5SApple OSS Distributions 		kill(1, SIGKILL);
185*1b191cb5SApple OSS Distributions 
186*1b191cb5SApple OSS Distributions 		ret = pthread_join(s_exc_thread, NULL);
187*1b191cb5SApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "pthread_join");
188*1b191cb5SApple OSS Distributions 
189*1b191cb5SApple OSS Distributions 		mach_port_deallocate(mach_task_self(), exc_port);
190*1b191cb5SApple OSS Distributions 
191*1b191cb5SApple OSS Distributions 		if (i == 0) { /* The first test is setup as moving immovable receive right of a reply port. */
192*1b191cb5SApple OSS Distributions 			expected_exception_code = (mach_exception_data_type_t)kGUARD_EXC_IMMOVABLE;
193*1b191cb5SApple OSS Distributions 		} else {
194*1b191cb5SApple OSS Distributions 			expected_exception_code = (mach_exception_data_type_t)kGUARD_EXC_INVALID_RIGHT;
195*1b191cb5SApple OSS Distributions 		}
196*1b191cb5SApple OSS Distributions 
197*1b191cb5SApple OSS Distributions 		T_LOG("Exception code: Received code = 0x%llx Expected code = 0x%llx", received_exception_code, expected_exception_code);
198*1b191cb5SApple OSS Distributions 		T_EXPECT_EQ(received_exception_code, expected_exception_code, "Exception code: Received == Expected");
199*1b191cb5SApple OSS Distributions 	}
200*1b191cb5SApple OSS Distributions }
201