1*d8b80295SApple OSS Distributions #include <darwintest.h>
2*d8b80295SApple OSS Distributions
3*d8b80295SApple OSS Distributions #include <errno.h>
4*d8b80295SApple OSS Distributions #include <fcntl.h>
5*d8b80295SApple OSS Distributions #include <signal.h>
6*d8b80295SApple OSS Distributions #include <spawn.h>
7*d8b80295SApple OSS Distributions #include <spawn_private.h>
8*d8b80295SApple OSS Distributions #include <stdbool.h>
9*d8b80295SApple OSS Distributions #include <stdint.h>
10*d8b80295SApple OSS Distributions #include <stdio.h>
11*d8b80295SApple OSS Distributions #include <stdlib.h>
12*d8b80295SApple OSS Distributions #include <string.h>
13*d8b80295SApple OSS Distributions #include <sys/spawn_internal.h>
14*d8b80295SApple OSS Distributions #include <sys/sysctl.h>
15*d8b80295SApple OSS Distributions #include <sys/syslimits.h>
16*d8b80295SApple OSS Distributions #include <sys/reason.h>
17*d8b80295SApple OSS Distributions #include <sysexits.h>
18*d8b80295SApple OSS Distributions #include <unistd.h>
19*d8b80295SApple OSS Distributions #include <signal.h>
20*d8b80295SApple OSS Distributions #include <libproc.h>
21*d8b80295SApple OSS Distributions
22*d8b80295SApple OSS Distributions #include <mach-o/dyld.h>
23*d8b80295SApple OSS Distributions #include <mach-o/dyld_priv.h>
24*d8b80295SApple OSS Distributions #include <dlfcn.h>
25*d8b80295SApple OSS Distributions
26*d8b80295SApple OSS Distributions #define SHARED_CACHE_HELPER "get_shared_cache_address"
27*d8b80295SApple OSS Distributions #define DO_RUSAGE_CHECK "check_rusage_flag"
28*d8b80295SApple OSS Distributions #define DO_DUMMY "dummy"
29*d8b80295SApple OSS Distributions #define ADDRESS_OUTPUT_SIZE 12L
30*d8b80295SApple OSS Distributions
31*d8b80295SApple OSS Distributions #ifndef _POSIX_SPAWN_RESLIDE
32*d8b80295SApple OSS Distributions #define _POSIX_SPAWN_RESLIDE 0x0800
33*d8b80295SApple OSS Distributions #endif
34*d8b80295SApple OSS Distributions
35*d8b80295SApple OSS Distributions #ifndef OS_REASON_FLAG_SHAREDREGION_FAULT
36*d8b80295SApple OSS Distributions #define OS_REASON_FLAG_SHAREDREGION_FAULT 0x400
37*d8b80295SApple OSS Distributions #endif
38*d8b80295SApple OSS Distributions
39*d8b80295SApple OSS Distributions T_GLOBAL_META(
40*d8b80295SApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"),
41*d8b80295SApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("VM"),
42*d8b80295SApple OSS Distributions T_META_OWNER("eperla"),
43*d8b80295SApple OSS Distributions T_META_RUN_CONCURRENTLY(true));
44*d8b80295SApple OSS Distributions
45*d8b80295SApple OSS Distributions #if (__arm64e__) && (TARGET_OS_IOS || TARGET_OS_OSX)
46*d8b80295SApple OSS Distributions static void *
get_current_slide_address(bool reslide)47*d8b80295SApple OSS Distributions get_current_slide_address(bool reslide)
48*d8b80295SApple OSS Distributions {
49*d8b80295SApple OSS Distributions pid_t pid;
50*d8b80295SApple OSS Distributions int pipefd[2];
51*d8b80295SApple OSS Distributions posix_spawnattr_t attr;
52*d8b80295SApple OSS Distributions posix_spawn_file_actions_t action;
53*d8b80295SApple OSS Distributions uintptr_t addr;
54*d8b80295SApple OSS Distributions
55*d8b80295SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(posix_spawnattr_init(&attr), "posix_spawnattr_init");
56*d8b80295SApple OSS Distributions /* spawn the helper requesting a reslide */
57*d8b80295SApple OSS Distributions if (reslide) {
58*d8b80295SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(posix_spawnattr_setflags(&attr, _POSIX_SPAWN_RESLIDE), "posix_spawnattr_setflags");
59*d8b80295SApple OSS Distributions }
60*d8b80295SApple OSS Distributions
61*d8b80295SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(pipe(pipefd), "pipe");
62*d8b80295SApple OSS Distributions T_ASSERT_POSIX_ZERO(posix_spawn_file_actions_init(&action), "posix_spawn_fileactions_init");
63*d8b80295SApple OSS Distributions T_ASSERT_POSIX_ZERO(posix_spawn_file_actions_addclose(&action, pipefd[0]), "posix_spawn_file_actions_addclose");
64*d8b80295SApple OSS Distributions T_ASSERT_POSIX_ZERO(posix_spawn_file_actions_adddup2(&action, pipefd[1], 1), "posix_spawn_file_actions_addup2");
65*d8b80295SApple OSS Distributions T_ASSERT_POSIX_ZERO(posix_spawn_file_actions_addclose(&action, pipefd[1]), "posix_spawn_file_actions_addclose");
66*d8b80295SApple OSS Distributions
67*d8b80295SApple OSS Distributions char *argvs[3];
68*d8b80295SApple OSS Distributions argvs[0] = SHARED_CACHE_HELPER;
69*d8b80295SApple OSS Distributions argvs[1] = reslide ? DO_RUSAGE_CHECK : DO_DUMMY;
70*d8b80295SApple OSS Distributions argvs[2] = NULL;
71*d8b80295SApple OSS Distributions char *const envps[] = {NULL};
72*d8b80295SApple OSS Distributions
73*d8b80295SApple OSS Distributions T_ASSERT_POSIX_ZERO(posix_spawn(&pid, SHARED_CACHE_HELPER, &action, &attr, argvs, envps), "helper posix_spawn");
74*d8b80295SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(close(pipefd[1]), "close child end of the pipe");
75*d8b80295SApple OSS Distributions
76*d8b80295SApple OSS Distributions char buf[ADDRESS_OUTPUT_SIZE] = {0};
77*d8b80295SApple OSS Distributions
78*d8b80295SApple OSS Distributions ssize_t read_bytes = 0;
79*d8b80295SApple OSS Distributions do {
80*d8b80295SApple OSS Distributions if (read_bytes == -1) {
81*d8b80295SApple OSS Distributions T_LOG("reading off get_shared_cache_address got interrupted");
82*d8b80295SApple OSS Distributions }
83*d8b80295SApple OSS Distributions read_bytes = read(pipefd[0], buf, sizeof(buf));
84*d8b80295SApple OSS Distributions } while (read_bytes == -1 && errno == EINTR);
85*d8b80295SApple OSS Distributions
86*d8b80295SApple OSS Distributions T_ASSERT_EQ_LONG(ADDRESS_OUTPUT_SIZE, read_bytes, "read helper output");
87*d8b80295SApple OSS Distributions
88*d8b80295SApple OSS Distributions int status = 0;
89*d8b80295SApple OSS Distributions int waitpid_result = waitpid(pid, &status, 0);
90*d8b80295SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(waitpid_result, "waitpid");
91*d8b80295SApple OSS Distributions T_ASSERT_EQ(waitpid_result, pid, "waitpid should return child we spawned");
92*d8b80295SApple OSS Distributions T_ASSERT_EQ(WIFEXITED(status), 1, "child should have exited normally");
93*d8b80295SApple OSS Distributions T_ASSERT_EQ(WEXITSTATUS(status), EX_OK, "child should have exited with success");
94*d8b80295SApple OSS Distributions
95*d8b80295SApple OSS Distributions addr = strtoul(buf, NULL, 16);
96*d8b80295SApple OSS Distributions T_ASSERT_GE_LONG(addr, 0L, "convert address to uintptr_t");
97*d8b80295SApple OSS Distributions
98*d8b80295SApple OSS Distributions return (void *)addr;
99*d8b80295SApple OSS Distributions }
100*d8b80295SApple OSS Distributions
101*d8b80295SApple OSS Distributions #define TEST_FAULT_BASE (0x00)
102*d8b80295SApple OSS Distributions #define TEST_FAULT_TBI (0x01)
103*d8b80295SApple OSS Distributions #define TEST_FAULT_WRITE (0x02)
104*d8b80295SApple OSS Distributions
105*d8b80295SApple OSS Distributions /*
106*d8b80295SApple OSS Distributions * build_faulting_shared_cache_address creates a pointer to an address that is
107*d8b80295SApple OSS Distributions * within the shared_cache range but that is guaranteed to not be mapped.
108*d8b80295SApple OSS Distributions */
109*d8b80295SApple OSS Distributions static char *
build_faulting_shared_cache_address(uint8_t flags)110*d8b80295SApple OSS Distributions build_faulting_shared_cache_address(uint8_t flags)
111*d8b80295SApple OSS Distributions {
112*d8b80295SApple OSS Distributions uintptr_t fault_address;
113*d8b80295SApple OSS Distributions
114*d8b80295SApple OSS Distributions // Grab currently mapped shared cache location and size
115*d8b80295SApple OSS Distributions size_t shared_cache_len = 0;
116*d8b80295SApple OSS Distributions const void *shared_cache_location = _dyld_get_shared_cache_range(&shared_cache_len);
117*d8b80295SApple OSS Distributions if (shared_cache_location == NULL || shared_cache_len == 0) {
118*d8b80295SApple OSS Distributions return NULL;
119*d8b80295SApple OSS Distributions }
120*d8b80295SApple OSS Distributions
121*d8b80295SApple OSS Distributions // Locate a mach_header in the shared cache
122*d8b80295SApple OSS Distributions Dl_info info;
123*d8b80295SApple OSS Distributions if (dladdr((const void *)fork, &info) == 0) {
124*d8b80295SApple OSS Distributions return NULL;
125*d8b80295SApple OSS Distributions }
126*d8b80295SApple OSS Distributions
127*d8b80295SApple OSS Distributions const struct mach_header *mh = info.dli_fbase;
128*d8b80295SApple OSS Distributions uintptr_t slide = (uintptr_t)_dyld_get_image_slide(mh);
129*d8b80295SApple OSS Distributions
130*d8b80295SApple OSS Distributions if (flags & TEST_FAULT_WRITE) {
131*d8b80295SApple OSS Distributions fault_address = (uintptr_t)shared_cache_location;
132*d8b80295SApple OSS Distributions } else if (slide == 0) {
133*d8b80295SApple OSS Distributions fault_address = (uintptr_t)shared_cache_location + shared_cache_len + PAGE_SIZE;
134*d8b80295SApple OSS Distributions } else {
135*d8b80295SApple OSS Distributions fault_address = (uintptr_t)shared_cache_location - PAGE_SIZE;
136*d8b80295SApple OSS Distributions }
137*d8b80295SApple OSS Distributions
138*d8b80295SApple OSS Distributions if (flags & TEST_FAULT_TBI) {
139*d8b80295SApple OSS Distributions fault_address |= 0x2000000000000000;
140*d8b80295SApple OSS Distributions }
141*d8b80295SApple OSS Distributions
142*d8b80295SApple OSS Distributions return (char *)fault_address;
143*d8b80295SApple OSS Distributions }
144*d8b80295SApple OSS Distributions
145*d8b80295SApple OSS Distributions #define INDUCE_CRASH_READ (0x01)
146*d8b80295SApple OSS Distributions #define INDUCE_CRASH_WRITE (0x02)
147*d8b80295SApple OSS Distributions
148*d8b80295SApple OSS Distributions static void
induce_crash(volatile char * ptr,uint8_t how_to_crash)149*d8b80295SApple OSS Distributions induce_crash(volatile char *ptr, uint8_t how_to_crash)
150*d8b80295SApple OSS Distributions {
151*d8b80295SApple OSS Distributions pid_t child = fork();
152*d8b80295SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(child, "fork");
153*d8b80295SApple OSS Distributions
154*d8b80295SApple OSS Distributions if (child == 0) {
155*d8b80295SApple OSS Distributions if (how_to_crash == INDUCE_CRASH_READ) {
156*d8b80295SApple OSS Distributions ptr[1];
157*d8b80295SApple OSS Distributions } else if (how_to_crash == INDUCE_CRASH_WRITE) {
158*d8b80295SApple OSS Distributions ptr[1] = 'a';
159*d8b80295SApple OSS Distributions } else {
160*d8b80295SApple OSS Distributions exit(1);
161*d8b80295SApple OSS Distributions }
162*d8b80295SApple OSS Distributions } else {
163*d8b80295SApple OSS Distributions sleep(1);
164*d8b80295SApple OSS Distributions struct proc_exitreasonbasicinfo exit_reason = {0};
165*d8b80295SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(proc_pidinfo(child, PROC_PIDEXITREASONBASICINFO, 1, &exit_reason, sizeof(exit_reason)), "basic exit reason");
166*d8b80295SApple OSS Distributions
167*d8b80295SApple OSS Distributions int status = 0;
168*d8b80295SApple OSS Distributions int waitpid_result;
169*d8b80295SApple OSS Distributions do {
170*d8b80295SApple OSS Distributions waitpid_result = waitpid(child, &status, 0);
171*d8b80295SApple OSS Distributions } while (waitpid_result < 0 && errno == EINTR);
172*d8b80295SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(waitpid_result, "waitpid");
173*d8b80295SApple OSS Distributions T_ASSERT_EQ(waitpid_result, child, "waitpid should return forked child");
174*d8b80295SApple OSS Distributions T_ASSERT_EQ(exit_reason.beri_namespace, OS_REASON_SIGNAL, "child should have exited with a signal");
175*d8b80295SApple OSS Distributions
176*d8b80295SApple OSS Distributions if (ptr) {
177*d8b80295SApple OSS Distributions if (how_to_crash == INDUCE_CRASH_READ) {
178*d8b80295SApple OSS Distributions T_ASSERT_EQ_ULLONG(exit_reason.beri_code, (unsigned long long)SIGSEGV, "child should have received SIGSEGV");
179*d8b80295SApple OSS Distributions }
180*d8b80295SApple OSS Distributions
181*d8b80295SApple OSS Distributions if (how_to_crash == INDUCE_CRASH_WRITE) {
182*d8b80295SApple OSS Distributions T_ASSERT_EQ_ULLONG(exit_reason.beri_code, (unsigned long long)SIGBUS, "child should have received SIGBUS");
183*d8b80295SApple OSS Distributions }
184*d8b80295SApple OSS Distributions
185*d8b80295SApple OSS Distributions T_ASSERT_NE((int)(exit_reason.beri_flags & OS_REASON_FLAG_SHAREDREGION_FAULT), 0, "should detect shared cache fault");
186*d8b80295SApple OSS Distributions } else {
187*d8b80295SApple OSS Distributions T_ASSERT_EQ((int)(exit_reason.beri_flags & OS_REASON_FLAG_SHAREDREGION_FAULT), 0, "should not detect shared cache fault");
188*d8b80295SApple OSS Distributions }
189*d8b80295SApple OSS Distributions }
190*d8b80295SApple OSS Distributions }
191*d8b80295SApple OSS Distributions
192*d8b80295SApple OSS Distributions static int saved_status;
193*d8b80295SApple OSS Distributions static void
cleanup_sysctl(void)194*d8b80295SApple OSS Distributions cleanup_sysctl(void)
195*d8b80295SApple OSS Distributions {
196*d8b80295SApple OSS Distributions int ret;
197*d8b80295SApple OSS Distributions
198*d8b80295SApple OSS Distributions if (saved_status == 0) {
199*d8b80295SApple OSS Distributions ret = sysctlbyname("vm.vm_shared_region_reslide_aslr", NULL, NULL, &saved_status, sizeof(saved_status));
200*d8b80295SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_SUCCESS(ret, "set shared region resliding back off");
201*d8b80295SApple OSS Distributions }
202*d8b80295SApple OSS Distributions }
203*d8b80295SApple OSS Distributions #endif /* arm64e && (TARGET_OS_IOS || TARGET_OS_OSX) */
204*d8b80295SApple OSS Distributions
205*d8b80295SApple OSS Distributions T_DECL(reslide_sharedcache, "crash induced reslide of the shared cache",
206*d8b80295SApple OSS Distributions T_META_CHECK_LEAKS(false), T_META_IGNORECRASHES(".*shared_cache_reslide_test.*"),
207*d8b80295SApple OSS Distributions T_META_ASROOT(true))
208*d8b80295SApple OSS Distributions {
209*d8b80295SApple OSS Distributions #if (__arm64e__) && (TARGET_OS_IOS || TARGET_OS_OSX)
210*d8b80295SApple OSS Distributions void *system_address;
211*d8b80295SApple OSS Distributions void *reslide_address;
212*d8b80295SApple OSS Distributions void *confirm_address;
213*d8b80295SApple OSS Distributions char *ptr;
214*d8b80295SApple OSS Distributions int on = 1;
215*d8b80295SApple OSS Distributions size_t size = sizeof(saved_status);
216*d8b80295SApple OSS Distributions
217*d8b80295SApple OSS Distributions /* Force resliding on */
218*d8b80295SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("vm.vm_shared_region_reslide_aslr", &saved_status, &size, &on, sizeof(on)), "force enable reslide");
219*d8b80295SApple OSS Distributions T_ATEND(cleanup_sysctl);
220*d8b80295SApple OSS Distributions
221*d8b80295SApple OSS Distributions system_address = get_current_slide_address(false);
222*d8b80295SApple OSS Distributions confirm_address = get_current_slide_address(false);
223*d8b80295SApple OSS Distributions T_ASSERT_EQ_PTR(system_address, confirm_address, "system and current addresses should not diverge %p %p", system_address, confirm_address);
224*d8b80295SApple OSS Distributions
225*d8b80295SApple OSS Distributions reslide_address = get_current_slide_address(true);
226*d8b80295SApple OSS Distributions confirm_address = get_current_slide_address(true);
227*d8b80295SApple OSS Distributions T_ASSERT_NE_PTR(system_address, reslide_address, "system and reslide addresses should diverge %p %p", system_address, reslide_address);
228*d8b80295SApple OSS Distributions T_ASSERT_EQ_PTR(reslide_address, confirm_address, "reslide and another reslide (no crash) shouldn't diverge %p %p", reslide_address, confirm_address);
229*d8b80295SApple OSS Distributions
230*d8b80295SApple OSS Distributions /* Crash into the shared cache area */
231*d8b80295SApple OSS Distributions ptr = build_faulting_shared_cache_address(TEST_FAULT_BASE);
232*d8b80295SApple OSS Distributions T_ASSERT_NOTNULL(ptr, "faulting on %p in the shared region", (void *)ptr);
233*d8b80295SApple OSS Distributions induce_crash(ptr, INDUCE_CRASH_READ);
234*d8b80295SApple OSS Distributions reslide_address = get_current_slide_address(true);
235*d8b80295SApple OSS Distributions T_ASSERT_NE_PTR(system_address, reslide_address, "system and reslide should diverge (after crash) %p %p", system_address, reslide_address);
236*d8b80295SApple OSS Distributions T_ASSERT_NE_PTR(confirm_address, reslide_address, "reslide and another reslide should diverge (after crash) %p %p", confirm_address, reslide_address);
237*d8b80295SApple OSS Distributions
238*d8b80295SApple OSS Distributions confirm_address = get_current_slide_address(true);
239*d8b80295SApple OSS Distributions T_ASSERT_EQ_PTR(reslide_address, confirm_address, "reslide and another reslide shouldn't diverge (no crash) %p %p", reslide_address, confirm_address);
240*d8b80295SApple OSS Distributions
241*d8b80295SApple OSS Distributions /* Crash somewhere else */
242*d8b80295SApple OSS Distributions ptr = NULL;
243*d8b80295SApple OSS Distributions induce_crash(ptr, INDUCE_CRASH_READ);
244*d8b80295SApple OSS Distributions confirm_address = get_current_slide_address(true);
245*d8b80295SApple OSS Distributions T_ASSERT_EQ_PTR(reslide_address, confirm_address, "reslide and another reslide after a non-tracked crash shouldn't diverge %p %p", reslide_address, confirm_address);
246*d8b80295SApple OSS Distributions
247*d8b80295SApple OSS Distributions /* Ensure we still get the system address */
248*d8b80295SApple OSS Distributions confirm_address = get_current_slide_address(false);
249*d8b80295SApple OSS Distributions T_ASSERT_EQ_PTR(system_address, confirm_address, "system address and new process without resliding shouldn't diverge %p %p", system_address, confirm_address);
250*d8b80295SApple OSS Distributions
251*d8b80295SApple OSS Distributions /* Ensure we detect a crash into the shared area with a TBI tagged address */
252*d8b80295SApple OSS Distributions ptr = build_faulting_shared_cache_address(TEST_FAULT_TBI);
253*d8b80295SApple OSS Distributions T_ASSERT_NOTNULL(ptr, "faulting on %p in the shared region", (void *)ptr);
254*d8b80295SApple OSS Distributions confirm_address = get_current_slide_address(true);
255*d8b80295SApple OSS Distributions induce_crash(ptr, INDUCE_CRASH_READ);
256*d8b80295SApple OSS Distributions reslide_address = get_current_slide_address(true);
257*d8b80295SApple OSS Distributions T_ASSERT_NE_PTR(system_address, reslide_address, "system and reslide should diverge (after crash, TBI test) %p %p", system_address, reslide_address);
258*d8b80295SApple OSS Distributions T_ASSERT_NE_PTR(confirm_address, reslide_address, "reslide and another reslide should diverge (after crash, TBI test) %p %p", confirm_address, reslide_address);
259*d8b80295SApple OSS Distributions
260*d8b80295SApple OSS Distributions /* Ensure we detect a crash into the shared area with a WRITE access */
261*d8b80295SApple OSS Distributions ptr = build_faulting_shared_cache_address(TEST_FAULT_WRITE);
262*d8b80295SApple OSS Distributions T_ASSERT_NOTNULL(ptr, "faulting on write on %p in the shared region", (void *)ptr);
263*d8b80295SApple OSS Distributions confirm_address = get_current_slide_address(true);
264*d8b80295SApple OSS Distributions induce_crash(ptr, INDUCE_CRASH_WRITE);
265*d8b80295SApple OSS Distributions reslide_address = get_current_slide_address(true);
266*d8b80295SApple OSS Distributions T_ASSERT_NE_PTR(system_address, reslide_address, "system and reslide should diverge (after crash, WRITE test) %p %p", system_address, reslide_address);
267*d8b80295SApple OSS Distributions T_ASSERT_NE_PTR(confirm_address, reslide_address, "reslide and another reslide should diverge (after crash, WRITE_TEST) %p %p", confirm_address, reslide_address);
268*d8b80295SApple OSS Distributions
269*d8b80295SApple OSS Distributions #else /* __arm64e__ && (TARGET_OS_IOS || TARGET_OS_OSX) */
270*d8b80295SApple OSS Distributions T_SKIP("shared cache reslide is currently only supported on arm64e iPhones and Apple Silicon Macs");
271*d8b80295SApple OSS Distributions #endif /* __arm64e__ && (TARGET_OS_IOS || TARGET_OS_OSX) */
272*d8b80295SApple OSS Distributions }
273