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