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