xref: /xnu-11417.140.69/tests/debug_enable_syscall_rejection.c (revision 43a90889846e00bfb5cf1d255cdc0a701a1e05a4)
1*43a90889SApple OSS Distributions /*
2*43a90889SApple OSS Distributions  * Copyright (c) 2019-2021 Apple Inc. All rights reserved.
3*43a90889SApple OSS Distributions  *
4*43a90889SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*43a90889SApple OSS Distributions  *
6*43a90889SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*43a90889SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*43a90889SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*43a90889SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*43a90889SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*43a90889SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*43a90889SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*43a90889SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*43a90889SApple OSS Distributions  *
15*43a90889SApple OSS Distributions  * Please obtain a copy of the License at
16*43a90889SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*43a90889SApple OSS Distributions  *
18*43a90889SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*43a90889SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*43a90889SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*43a90889SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*43a90889SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*43a90889SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*43a90889SApple OSS Distributions  * limitations under the License.
25*43a90889SApple OSS Distributions  *
26*43a90889SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*43a90889SApple OSS Distributions  */
28*43a90889SApple OSS Distributions 
29*43a90889SApple OSS Distributions /*
30*43a90889SApple OSS Distributions  * tests for scheduler hygiene system call rejection feature
31*43a90889SApple OSS Distributions  */
32*43a90889SApple OSS Distributions 
33*43a90889SApple OSS Distributions #include <darwintest.h>
34*43a90889SApple OSS Distributions #include <darwintest_posix.h>
35*43a90889SApple OSS Distributions #include <darwintest_utils.h>
36*43a90889SApple OSS Distributions 
37*43a90889SApple OSS Distributions #include <errno.h>
38*43a90889SApple OSS Distributions #include <libgen.h>
39*43a90889SApple OSS Distributions #include <pthread.h>
40*43a90889SApple OSS Distributions #include <signal.h>
41*43a90889SApple OSS Distributions #include <stdarg.h>
42*43a90889SApple OSS Distributions #include <stdio.h>
43*43a90889SApple OSS Distributions #include <string.h>
44*43a90889SApple OSS Distributions #include <sys/sysctl.h>
45*43a90889SApple OSS Distributions #include <unistd.h>
46*43a90889SApple OSS Distributions 
47*43a90889SApple OSS Distributions #include <mach/mach.h>
48*43a90889SApple OSS Distributions #include <mach/mach_traps.h>
49*43a90889SApple OSS Distributions #include <mach/mach_types.h>
50*43a90889SApple OSS Distributions 
51*43a90889SApple OSS Distributions #include <darwintest.h>
52*43a90889SApple OSS Distributions #include <darwintest_utils.h>
53*43a90889SApple OSS Distributions 
54*43a90889SApple OSS Distributions #include <sys/kern_debug.h>
55*43a90889SApple OSS Distributions #include <sys/stat.h>
56*43a90889SApple OSS Distributions 
57*43a90889SApple OSS Distributions #include <mach-o/dyld.h>
58*43a90889SApple OSS Distributions 
59*43a90889SApple OSS Distributions T_GLOBAL_META(
60*43a90889SApple OSS Distributions 	T_META_NAMESPACE("xnu.debug"),
61*43a90889SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
62*43a90889SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("arm"),
63*43a90889SApple OSS Distributions 	T_META_OWNER("julien_oster")
64*43a90889SApple OSS Distributions 	);
65*43a90889SApple OSS Distributions 
66*43a90889SApple OSS Distributions static int const ALIVE = 0;
67*43a90889SApple OSS Distributions static int const DEAD = 1;
68*43a90889SApple OSS Distributions 
69*43a90889SApple OSS Distributions static int
helper(char ** args)70*43a90889SApple OSS Distributions helper(char **args)
71*43a90889SApple OSS Distributions {
72*43a90889SApple OSS Distributions 	int ret = 0;
73*43a90889SApple OSS Distributions 	int status = 0;
74*43a90889SApple OSS Distributions 	int signal = 0;
75*43a90889SApple OSS Distributions 	int timeout = 30;
76*43a90889SApple OSS Distributions 
77*43a90889SApple OSS Distributions 	pid_t child_pid = 0;
78*43a90889SApple OSS Distributions 	bool wait_ret = true;
79*43a90889SApple OSS Distributions 
80*43a90889SApple OSS Distributions 	char binary_path[MAXPATHLEN], *binary_dir = NULL;
81*43a90889SApple OSS Distributions 	uint32_t path_size = sizeof(binary_path);
82*43a90889SApple OSS Distributions 
83*43a90889SApple OSS Distributions 	ret = _NSGetExecutablePath(binary_path, &path_size);
84*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(ret, 0, "_NSGetExecutablePath: %s, size: %d", binary_path, path_size);
85*43a90889SApple OSS Distributions 	binary_dir = dirname(binary_path);
86*43a90889SApple OSS Distributions 	T_QUIET; T_WITH_ERRNO; T_ASSERT_NOTNULL(binary_dir, "get binary directory: %s", binary_dir);
87*43a90889SApple OSS Distributions 
88*43a90889SApple OSS Distributions 	char const *helper_binary = "debug_syscall_rejection_helper";
89*43a90889SApple OSS Distributions 	snprintf(binary_path, MAXPATHLEN, "%s/%s", binary_dir, helper_binary);
90*43a90889SApple OSS Distributions 	args[0] = binary_path;
91*43a90889SApple OSS Distributions 
92*43a90889SApple OSS Distributions 	ret = dt_launch_tool(&child_pid, args, false, NULL, NULL);
93*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(ret, 0, "launch helper: %s", helper_binary);
94*43a90889SApple OSS Distributions 
95*43a90889SApple OSS Distributions 	wait_ret = dt_waitpid(child_pid, &status, &signal, timeout);
96*43a90889SApple OSS Distributions 
97*43a90889SApple OSS Distributions 	if (wait_ret) {
98*43a90889SApple OSS Distributions 		// T_LOG("helper returned: %d", status);
99*43a90889SApple OSS Distributions 
100*43a90889SApple OSS Distributions 		T_EXPECT_EQ(status, 0, "helper returned exit code %i", status);
101*43a90889SApple OSS Distributions 
102*43a90889SApple OSS Distributions 		return 0;
103*43a90889SApple OSS Distributions 	}
104*43a90889SApple OSS Distributions 
105*43a90889SApple OSS Distributions 	// helper crashed (possibly expectedly)
106*43a90889SApple OSS Distributions 
107*43a90889SApple OSS Distributions 	if (signal != 0) {
108*43a90889SApple OSS Distributions 		// T_LOG("signal terminated helper: %d", signal);
109*43a90889SApple OSS Distributions 
110*43a90889SApple OSS Distributions 		T_EXPECT_EQ(signal, SIGKILL, "helper terminated with signal %i", signal);
111*43a90889SApple OSS Distributions 		return 1;
112*43a90889SApple OSS Distributions 	}
113*43a90889SApple OSS Distributions 
114*43a90889SApple OSS Distributions 	T_FAIL("helper terminated with unexpected condition (status: %i, signal: %i)", status, signal);
115*43a90889SApple OSS Distributions 	return 2;
116*43a90889SApple OSS Distributions }
117*43a90889SApple OSS Distributions 
118*43a90889SApple OSS Distributions static void
do_test(char const * msg,int expectation,...)119*43a90889SApple OSS Distributions do_test(char const *msg, int expectation, ...)
120*43a90889SApple OSS Distributions {
121*43a90889SApple OSS Distributions 	size_t const arg_max_count = 128;
122*43a90889SApple OSS Distributions 	char *args[arg_max_count] = {0};
123*43a90889SApple OSS Distributions 
124*43a90889SApple OSS Distributions 	va_list ap;
125*43a90889SApple OSS Distributions 	va_start(ap, expectation);
126*43a90889SApple OSS Distributions 
127*43a90889SApple OSS Distributions 	char *arg;
128*43a90889SApple OSS Distributions 	int i = 1;
129*43a90889SApple OSS Distributions 	while ((arg = va_arg(ap, char *)) != NULL) {
130*43a90889SApple OSS Distributions 		T_QUIET; T_ASSERT_LT(i, (int)arg_max_count, "no args overflow");
131*43a90889SApple OSS Distributions 		args[i++] = arg;
132*43a90889SApple OSS Distributions 	}
133*43a90889SApple OSS Distributions 
134*43a90889SApple OSS Distributions 	va_end(ap);
135*43a90889SApple OSS Distributions 
136*43a90889SApple OSS Distributions 	T_EXPECT_EQ(helper(args), expectation, "%s", msg);
137*43a90889SApple OSS Distributions }
138*43a90889SApple OSS Distributions 
139*43a90889SApple OSS Distributions #define ALLOW(pos, mask) "-a", "-s", #mask, "-i", #pos
140*43a90889SApple OSS Distributions #define DENY(pos, mask) "-d", "-s", #mask, "-i", #pos
141*43a90889SApple OSS Distributions 
142*43a90889SApple OSS Distributions T_DECL(debug_syscall_rejection_tests,
143*43a90889SApple OSS Distributions     "Verify that syscall rejection works",
144*43a90889SApple OSS Distributions     T_META_SYSCTL_INT("kern.debug_syscall_rejection_mode=1"),
145*43a90889SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("kern.debug_syscall_rejection_mode", 1), T_META_TAG_VM_PREFERRED)
146*43a90889SApple OSS Distributions {
147*43a90889SApple OSS Distributions 	int old_mode;
148*43a90889SApple OSS Distributions 	size_t old_mode_size = sizeof(old_mode);
149*43a90889SApple OSS Distributions 	int new_mode = 2;
150*43a90889SApple OSS Distributions 
151*43a90889SApple OSS Distributions 	// test syscall mode switching (even if already done by T_META_SYSCTL_INT)
152*43a90889SApple OSS Distributions 	int ret = sysctlbyname("kern.debug_syscall_rejection_mode", &old_mode, &old_mode_size, &new_mode, sizeof(new_mode));
153*43a90889SApple OSS Distributions 	if (ret != 0) {
154*43a90889SApple OSS Distributions 		T_ASSERT_FAIL("Syscall rejection mode switching failed: ret %d, errno %d", ret, errno);
155*43a90889SApple OSS Distributions 	}
156*43a90889SApple OSS Distributions 
157*43a90889SApple OSS Distributions 	T_ASSERT_EQ(old_mode_size, sizeof(old_mode), "sysctl returns correct size");
158*43a90889SApple OSS Distributions 
159*43a90889SApple OSS Distributions 	char * const set_mask_12 = "12: chdir debug_syscall_reject_config";
160*43a90889SApple OSS Distributions 	size_t const set_mask_12_size = strlen(set_mask_12);
161*43a90889SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(sysctlbyname("kern.syscall_rejection_masks", NULL, NULL, set_mask_12, set_mask_12_size),
162*43a90889SApple OSS Distributions 	    "set syscall rej test mask");
163*43a90889SApple OSS Distributions 
164*43a90889SApple OSS Distributions 	do_test("default is disallow all", DEAD, NULL);
165*43a90889SApple OSS Distributions 	do_test("disallow all dies", DEAD, DENY(0, 1), NULL);
166*43a90889SApple OSS Distributions 	do_test("allow all lives", ALIVE, ALLOW(0, 1), NULL);
167*43a90889SApple OSS Distributions 	do_test("allow testmask lives", ALIVE, ALLOW(0, 12), NULL);
168*43a90889SApple OSS Distributions 	do_test("disallow testmask overrides dies", DEAD, ALLOW(0, 1), DENY(1, 12), NULL);
169*43a90889SApple OSS Distributions 
170*43a90889SApple OSS Distributions 	// check that all slots work as expected
171*43a90889SApple OSS Distributions 	int const slots = 16;
172*43a90889SApple OSS Distributions 	for (int i = 0; i < slots - 1; i++) {
173*43a90889SApple OSS Distributions 		size_t const max_pos_len = 16;
174*43a90889SApple OSS Distributions 		char pos1[max_pos_len], pos2[max_pos_len];
175*43a90889SApple OSS Distributions 		T_QUIET; T_ASSERT_GE_INT(snprintf(pos1, max_pos_len, "%u", i), 0, "creating pos1");
176*43a90889SApple OSS Distributions 		T_QUIET; T_ASSERT_GE_INT(snprintf(pos2, max_pos_len, "%u", i + 1), 0, "creating pos2");
177*43a90889SApple OSS Distributions 
178*43a90889SApple OSS Distributions 		char *args[] = {DENY(pos1, ALL), ALLOW(pos2, 12), NULL};
179*43a90889SApple OSS Distributions 		T_EXPECT_EQ(helper(args), ALIVE, "pos %s/%s works (alive)", pos1, pos2);
180*43a90889SApple OSS Distributions 		char *args_d[] = {ALLOW(pos1, ALL), DENY(pos2, 12), NULL};
181*43a90889SApple OSS Distributions 		T_EXPECT_EQ(helper(args_d), DEAD, "pos %s/%s works (dead)", pos1, pos2);
182*43a90889SApple OSS Distributions 	}
183*43a90889SApple OSS Distributions 
184*43a90889SApple OSS Distributions 	// check non-fatal mode
185*43a90889SApple OSS Distributions 	new_mode = 1;
186*43a90889SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(sysctlbyname("kern.debug_syscall_rejection_mode", NULL, NULL, &new_mode, sizeof(new_mode)),
187*43a90889SApple OSS Distributions 	    "set syscall rej mode to non-fatal");
188*43a90889SApple OSS Distributions 
189*43a90889SApple OSS Distributions 	do_test("non-fatal: default is disallow all", ALIVE, NULL);
190*43a90889SApple OSS Distributions 	do_test("non-fatal: disallow all", ALIVE, DENY(0, 1), NULL);
191*43a90889SApple OSS Distributions 	do_test("non-fatal: allow all", ALIVE, ALLOW(0, 1), NULL);
192*43a90889SApple OSS Distributions 	do_test("non-fatal: allow testmask", ALIVE, ALLOW(0, 12), NULL);
193*43a90889SApple OSS Distributions 	do_test("non-fatal: disallow testmask overrides", ALIVE, ALLOW(0, 1), DENY(1, 12), NULL);
194*43a90889SApple OSS Distributions 
195*43a90889SApple OSS Distributions 	// check force-fatal override
196*43a90889SApple OSS Distributions 	new_mode = 1;
197*43a90889SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(sysctlbyname("kern.debug_syscall_rejection_mode", NULL, NULL, &new_mode, sizeof(new_mode)),
198*43a90889SApple OSS Distributions 	    "set syscall rej mode to non-fatal");
199*43a90889SApple OSS Distributions 
200*43a90889SApple OSS Distributions 	do_test("force-fatal: default is disallow all", DEAD, "-F", NULL);
201*43a90889SApple OSS Distributions 	do_test("force-fatal: disallow all", DEAD, DENY(0, 1), "-F", NULL);
202*43a90889SApple OSS Distributions 	do_test("force-fatal: allow all", ALIVE, ALLOW(0, 1), "-F", NULL);
203*43a90889SApple OSS Distributions 	do_test("force-fatal: allow testmask", ALIVE, ALLOW(0, 12), "-F", NULL);
204*43a90889SApple OSS Distributions 	do_test("force-fatal: disallow testmask overrides", DEAD, ALLOW(0, 1), DENY(1, 12), "-F", NULL);
205*43a90889SApple OSS Distributions }
206*43a90889SApple OSS Distributions 
207*43a90889SApple OSS Distributions T_DECL(debug_enable_syscall_rejection_crash_count,
208*43a90889SApple OSS Distributions     "count syscall rejection crash reports",
209*43a90889SApple OSS Distributions     T_META_ENABLED(FALSE), /* currently a manual test */
210*43a90889SApple OSS Distributions     T_META_TAG_VM_PREFERRED
211*43a90889SApple OSS Distributions     )
212*43a90889SApple OSS Distributions {
213*43a90889SApple OSS Distributions 	syscall_rejection_selector_t masks[] = {
214*43a90889SApple OSS Distributions 		SYSCALL_REJECTION_ALLOW(SYSCALL_REJECTION_ALL),
215*43a90889SApple OSS Distributions 		SYSCALL_REJECTION_DENY(SYSCALL_REJECTION_NULL),
216*43a90889SApple OSS Distributions 		SYSCALL_REJECTION_DENY(SYSCALL_REJECTION_NULL),
217*43a90889SApple OSS Distributions 		SYSCALL_REJECTION_DENY(SYSCALL_REJECTION_NULL),
218*43a90889SApple OSS Distributions 		SYSCALL_REJECTION_DENY(SYSCALL_REJECTION_NULL),
219*43a90889SApple OSS Distributions 		SYSCALL_REJECTION_DENY(SYSCALL_REJECTION_NULL),
220*43a90889SApple OSS Distributions 		SYSCALL_REJECTION_DENY(SYSCALL_REJECTION_NULL),
221*43a90889SApple OSS Distributions 		SYSCALL_REJECTION_DENY(SYSCALL_REJECTION_NULL),
222*43a90889SApple OSS Distributions 		SYSCALL_REJECTION_DENY(SYSCALL_REJECTION_NULL),
223*43a90889SApple OSS Distributions 		SYSCALL_REJECTION_DENY(2),
224*43a90889SApple OSS Distributions 		SYSCALL_REJECTION_DENY(2),
225*43a90889SApple OSS Distributions 		SYSCALL_REJECTION_DENY(2),
226*43a90889SApple OSS Distributions 		SYSCALL_REJECTION_DENY(2),
227*43a90889SApple OSS Distributions 		SYSCALL_REJECTION_DENY(2),
228*43a90889SApple OSS Distributions 		SYSCALL_REJECTION_DENY(2),
229*43a90889SApple OSS Distributions 		SYSCALL_REJECTION_DENY(2),
230*43a90889SApple OSS Distributions 	};
231*43a90889SApple OSS Distributions 
232*43a90889SApple OSS Distributions 	int ret = debug_syscall_reject_config(masks, sizeof(masks) / sizeof(masks[0]), SYSCALL_REJECTION_FLAGS_DEFAULT);
233*43a90889SApple OSS Distributions 
234*43a90889SApple OSS Distributions 	T_WITH_ERRNO; T_ASSERT_POSIX_SUCCESS(ret, "debug_syscall_reject_config");
235*43a90889SApple OSS Distributions 
236*43a90889SApple OSS Distributions 	ret = chdir("/tmp");
237*43a90889SApple OSS Distributions 	ret = chdir("/tmp");
238*43a90889SApple OSS Distributions 	ret = chdir("/tmp");
239*43a90889SApple OSS Distributions 
240*43a90889SApple OSS Distributions 	printf("chdir: %i\n", ret);
241*43a90889SApple OSS Distributions 
242*43a90889SApple OSS Distributions 	ret = debug_syscall_reject_config(masks, sizeof(masks) / sizeof(masks[0]), SYSCALL_REJECTION_FLAGS_ONCE);
243*43a90889SApple OSS Distributions 
244*43a90889SApple OSS Distributions 	T_WITH_ERRNO; T_ASSERT_POSIX_SUCCESS(ret, "debug_syscall_reject_config once");
245*43a90889SApple OSS Distributions 
246*43a90889SApple OSS Distributions 	ret = chdir("/tmp");
247*43a90889SApple OSS Distributions 	ret = chdir("/tmp");
248*43a90889SApple OSS Distributions 	ret = chdir("/tmp");
249*43a90889SApple OSS Distributions 
250*43a90889SApple OSS Distributions 	printf("chdir once: %i\n", ret);
251*43a90889SApple OSS Distributions 
252*43a90889SApple OSS Distributions 	ret = debug_syscall_reject_config(masks, sizeof(masks) / sizeof(masks[0]), SYSCALL_REJECTION_FLAGS_ONCE);
253*43a90889SApple OSS Distributions 
254*43a90889SApple OSS Distributions 	T_WITH_ERRNO; T_ASSERT_POSIX_SUCCESS(ret, "debug_syscall_reject_config once ignore");
255*43a90889SApple OSS Distributions 
256*43a90889SApple OSS Distributions 	ret = chdir("/tmp");
257*43a90889SApple OSS Distributions 	ret = chdir("/tmp");
258*43a90889SApple OSS Distributions 	ret = chdir("/tmp");
259*43a90889SApple OSS Distributions 
260*43a90889SApple OSS Distributions 	printf("chdir once ignore: %i\n", ret);
261*43a90889SApple OSS Distributions 
262*43a90889SApple OSS Distributions 	ret = debug_syscall_reject_config(masks, sizeof(masks) / sizeof(masks[0]), SYSCALL_REJECTION_FLAGS_FORCE_FATAL);
263*43a90889SApple OSS Distributions 
264*43a90889SApple OSS Distributions 	T_WITH_ERRNO; T_ASSERT_POSIX_SUCCESS(ret, "debug_syscall_reject_config fatal");
265*43a90889SApple OSS Distributions 
266*43a90889SApple OSS Distributions 	ret = chdir("/tmp");
267*43a90889SApple OSS Distributions 	ret = chdir("/tmp");
268*43a90889SApple OSS Distributions 	ret = chdir("/tmp");
269*43a90889SApple OSS Distributions 
270*43a90889SApple OSS Distributions 	printf("chdir fatal: %i\n", ret); // unreached, but this is currently manual testing only
271*43a90889SApple OSS Distributions 
272*43a90889SApple OSS Distributions 	/*
273*43a90889SApple OSS Distributions 	 * Expected number of crash reports:
274*43a90889SApple OSS Distributions 	 * Mode				Crashes		Reason
275*43a90889SApple OSS Distributions 	 * Ignore (0)		1			force fatal causes final crash
276*43a90889SApple OSS Distributions 	 * Guard (1)		5			3 without ONCE flag, 1 with ONCE flag, final forced fatal crash
277*43a90889SApple OSS Distributions 	 * Fatal (2)		1			first crash is fatal
278*43a90889SApple OSS Distributions 	 */
279*43a90889SApple OSS Distributions }
280