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