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