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