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