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