]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
f427ee49 | 2 | * Copyright (c) 2000-2020 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
0a7de745 | 5 | * |
2d21ac55 A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
0a7de745 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
0a7de745 | 17 | * |
2d21ac55 A |
18 | * The Original Code and all software distributed under the License are |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
0a7de745 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* | |
29 | * Copyright (C) 1988, 1989, NeXT, Inc. | |
30 | * | |
31 | * File: kern/mach_loader.c | |
32 | * Author: Avadis Tevanian, Jr. | |
33 | * | |
34 | * Mach object file loader (kernel version, for now). | |
35 | * | |
36 | * 21-Jul-88 Avadis Tevanian, Jr. (avie) at NeXT | |
37 | * Started. | |
38 | */ | |
91447636 | 39 | |
1c79356b | 40 | #include <sys/param.h> |
91447636 | 41 | #include <sys/vnode_internal.h> |
1c79356b A |
42 | #include <sys/uio.h> |
43 | #include <sys/namei.h> | |
91447636 A |
44 | #include <sys/proc_internal.h> |
45 | #include <sys/kauth.h> | |
1c79356b A |
46 | #include <sys/stat.h> |
47 | #include <sys/malloc.h> | |
91447636 | 48 | #include <sys/mount_internal.h> |
1c79356b | 49 | #include <sys/fcntl.h> |
f427ee49 | 50 | #include <sys/file_internal.h> |
91447636 A |
51 | #include <sys/ubc_internal.h> |
52 | #include <sys/imgact.h> | |
39236c6e | 53 | #include <sys/codesign.h> |
39037602 A |
54 | #include <sys/proc_uuid_policy.h> |
55 | #include <sys/reason.h> | |
56 | #include <sys/kdebug.h> | |
f427ee49 | 57 | #include <sys/spawn_internal.h> |
1c79356b | 58 | |
1c79356b | 59 | #include <mach/mach_types.h> |
0a7de745 A |
60 | #include <mach/vm_map.h> /* vm_allocate() */ |
61 | #include <mach/mach_vm.h> /* mach_vm_allocate() */ | |
91447636 | 62 | #include <mach/vm_statistics.h> |
91447636 A |
63 | #include <mach/task.h> |
64 | #include <mach/thread_act.h> | |
65 | ||
66 | #include <machine/vmparam.h> | |
2d21ac55 | 67 | #include <machine/exec.h> |
6d2010ae | 68 | #include <machine/pal_routines.h> |
1c79356b | 69 | |
527f9951 | 70 | #include <kern/ast.h> |
91447636 A |
71 | #include <kern/kern_types.h> |
72 | #include <kern/cpu_number.h> | |
1c79356b | 73 | #include <kern/mach_loader.h> |
b0d623f7 | 74 | #include <kern/mach_fat.h> |
91447636 | 75 | #include <kern/kalloc.h> |
55e303ae | 76 | #include <kern/task.h> |
91447636 | 77 | #include <kern/thread.h> |
593a1d5f | 78 | #include <kern/page_decrypt.h> |
1c79356b A |
79 | |
80 | #include <mach-o/fat.h> | |
81 | #include <mach-o/loader.h> | |
82 | ||
91447636 | 83 | #include <vm/pmap.h> |
1c79356b A |
84 | #include <vm/vm_map.h> |
85 | #include <vm/vm_kern.h> | |
86 | #include <vm/vm_pager.h> | |
87 | #include <vm/vnode_pager.h> | |
a39ff7e2 | 88 | #include <vm/vm_protos.h> |
f427ee49 | 89 | #include <vm/vm_shared_region.h> |
0a7de745 | 90 | #include <IOKit/IOReturn.h> /* for kIOReturnNotPrivileged */ |
f427ee49 | 91 | #include <IOKit/IOBSD.h> /* for IOVnodeHasEntitlement */ |
9bccf70c | 92 | |
490019cf A |
93 | #include <os/overflow.h> |
94 | ||
91447636 A |
95 | /* |
96 | * XXX vm/pmap.h should not treat these prototypes as MACH_KERNEL_PRIVATE | |
97 | * when KERNEL is defined. | |
98 | */ | |
cb323159 A |
99 | extern pmap_t pmap_create_options(ledger_t ledger, vm_map_size_t size, |
100 | unsigned int flags); | |
f427ee49 A |
101 | #if __has_feature(ptrauth_calls) && XNU_TARGET_OS_OSX |
102 | extern void pmap_disable_user_jop(pmap_t pmap); | |
103 | #endif /* __has_feature(ptrauth_calls) && XNU_TARGET_OS_OSX */ | |
91447636 A |
104 | |
105 | /* XXX should have prototypes in a shared header file */ | |
0a7de745 | 106 | extern int get_map_nentries(vm_map_t); |
91447636 | 107 | |
0a7de745 A |
108 | extern kern_return_t memory_object_signed(memory_object_control_t control, |
109 | boolean_t is_signed); | |
91447636 A |
110 | |
111 | /* An empty load_result_t */ | |
39037602 | 112 | static const load_result_t load_result_null = { |
2d21ac55 A |
113 | .mach_header = MACH_VM_MIN_ADDRESS, |
114 | .entry_point = MACH_VM_MIN_ADDRESS, | |
115 | .user_stack = MACH_VM_MIN_ADDRESS, | |
316670eb | 116 | .user_stack_size = 0, |
39037602 A |
117 | .user_stack_alloc = MACH_VM_MIN_ADDRESS, |
118 | .user_stack_alloc_size = 0, | |
b0d623f7 A |
119 | .all_image_info_addr = MACH_VM_MIN_ADDRESS, |
120 | .all_image_info_size = 0, | |
2d21ac55 A |
121 | .thread_count = 0, |
122 | .unixproc = 0, | |
123 | .dynlinker = 0, | |
316670eb | 124 | .needs_dynlinker = 0, |
6d2010ae | 125 | .validentry = 0, |
c18c124e | 126 | .using_lcmain = 0, |
d9a64523 A |
127 | .is_64bit_addr = 0, |
128 | .is_64bit_data = 0, | |
0a7de745 | 129 | .custom_stack = 0, |
b0d623f7 | 130 | .csflags = 0, |
3e170ce0 | 131 | .has_pagezero = 0, |
6d2010ae A |
132 | .uuid = { 0 }, |
133 | .min_vm_addr = MACH_VM_MAX_ADDRESS, | |
04b8595b | 134 | .max_vm_addr = MACH_VM_MIN_ADDRESS, |
490019cf A |
135 | .cs_end_offset = 0, |
136 | .threadstate = NULL, | |
f427ee49 A |
137 | .threadstate_sz = 0, |
138 | .is_cambria = 0, | |
139 | .dynlinker_mach_header = MACH_VM_MIN_ADDRESS, | |
140 | .dynlinker_fd = -1, | |
91447636 | 141 | }; |
9bccf70c | 142 | |
1c79356b A |
143 | /* |
144 | * Prototypes of static functions. | |
145 | */ | |
91447636 | 146 | static load_return_t |
1c79356b | 147 | parse_machfile( |
0a7de745 A |
148 | struct vnode *vp, |
149 | vm_map_t map, | |
150 | thread_t thread, | |
151 | struct mach_header *header, | |
152 | off_t file_offset, | |
153 | off_t macho_size, | |
154 | int depth, | |
155 | int64_t slide, | |
156 | int64_t dyld_slide, | |
157 | load_result_t *result, | |
158 | load_result_t *binresult, | |
159 | struct image_params *imgp | |
160 | ); | |
91447636 A |
161 | |
162 | static load_return_t | |
1c79356b | 163 | load_segment( |
0a7de745 A |
164 | struct load_command *lcp, |
165 | uint32_t filetype, | |
166 | void *control, | |
167 | off_t pager_offset, | |
168 | off_t macho_size, | |
169 | struct vnode *vp, | |
170 | vm_map_t map, | |
171 | int64_t slide, | |
f427ee49 A |
172 | load_result_t *result, |
173 | struct image_params *imgp | |
0a7de745 | 174 | ); |
91447636 | 175 | |
39236c6e A |
176 | static load_return_t |
177 | load_uuid( | |
0a7de745 A |
178 | struct uuid_command *uulp, |
179 | char *command_end, | |
180 | load_result_t *result | |
181 | ); | |
39236c6e | 182 | |
cb323159 A |
183 | static load_return_t |
184 | load_version( | |
185 | struct version_min_command *vmc, | |
186 | boolean_t *found_version_cmd, | |
f427ee49 | 187 | int ip_flags, |
cb323159 A |
188 | load_result_t *result |
189 | ); | |
190 | ||
6d2010ae A |
191 | static load_return_t |
192 | load_code_signature( | |
0a7de745 A |
193 | struct linkedit_data_command *lcp, |
194 | struct vnode *vp, | |
195 | off_t macho_offset, | |
196 | off_t macho_size, | |
197 | cpu_type_t cputype, | |
f427ee49 | 198 | cpu_subtype_t cpusubtype, |
0a7de745 A |
199 | load_result_t *result, |
200 | struct image_params *imgp); | |
201 | ||
593a1d5f A |
202 | #if CONFIG_CODE_DECRYPTION |
203 | static load_return_t | |
204 | set_code_unprotect( | |
0a7de745 A |
205 | struct encryption_info_command *lcp, |
206 | caddr_t addr, | |
207 | vm_map_t map, | |
208 | int64_t slide, | |
209 | struct vnode *vp, | |
210 | off_t macho_offset, | |
211 | cpu_type_t cputype, | |
212 | cpu_subtype_t cpusubtype); | |
593a1d5f A |
213 | #endif |
214 | ||
316670eb A |
215 | static |
216 | load_return_t | |
217 | load_main( | |
0a7de745 A |
218 | struct entry_point_command *epc, |
219 | thread_t thread, | |
220 | int64_t slide, | |
221 | load_result_t *result | |
222 | ); | |
316670eb | 223 | |
cb323159 A |
224 | static |
225 | load_return_t | |
226 | setup_driver_main( | |
227 | thread_t thread, | |
228 | int64_t slide, | |
229 | load_result_t *result | |
230 | ); | |
231 | ||
91447636 | 232 | static load_return_t |
1c79356b | 233 | load_unixthread( |
0a7de745 A |
234 | struct thread_command *tcp, |
235 | thread_t thread, | |
236 | int64_t slide, | |
f427ee49 | 237 | boolean_t is_x86_64_compat_binary, |
0a7de745 A |
238 | load_result_t *result |
239 | ); | |
91447636 A |
240 | |
241 | static load_return_t | |
1c79356b | 242 | load_threadstate( |
0a7de745 A |
243 | thread_t thread, |
244 | uint32_t *ts, | |
245 | uint32_t total_size, | |
490019cf | 246 | load_result_t * |
0a7de745 | 247 | ); |
91447636 A |
248 | |
249 | static load_return_t | |
1c79356b | 250 | load_threadstack( |
0a7de745 A |
251 | thread_t thread, |
252 | uint32_t *ts, | |
253 | uint32_t total_size, | |
254 | mach_vm_offset_t *user_stack, | |
255 | int *customstack, | |
f427ee49 | 256 | boolean_t is_x86_64_compat_binary, |
0a7de745 A |
257 | load_result_t *result |
258 | ); | |
91447636 A |
259 | |
260 | static load_return_t | |
1c79356b | 261 | load_threadentry( |
0a7de745 A |
262 | thread_t thread, |
263 | uint32_t *ts, | |
264 | uint32_t total_size, | |
265 | mach_vm_offset_t *entry_point | |
266 | ); | |
91447636 A |
267 | |
268 | static load_return_t | |
1c79356b | 269 | load_dylinker( |
0a7de745 A |
270 | struct dylinker_command *lcp, |
271 | integer_t archbits, | |
272 | vm_map_t map, | |
273 | thread_t thread, | |
274 | int depth, | |
275 | int64_t slide, | |
276 | load_result_t *result, | |
277 | struct image_params *imgp | |
278 | ); | |
91447636 | 279 | |
f427ee49 | 280 | |
cb323159 A |
281 | #if __x86_64__ |
282 | extern int bootarg_no32exec; | |
283 | static boolean_t | |
284 | check_if_simulator_binary( | |
285 | struct image_params *imgp, | |
286 | off_t file_offset, | |
287 | off_t macho_size); | |
288 | #endif | |
289 | ||
6d2010ae A |
290 | struct macho_data; |
291 | ||
91447636 | 292 | static load_return_t |
1c79356b | 293 | get_macho_vnode( |
0a7de745 A |
294 | const char *path, |
295 | integer_t archbits, | |
296 | struct mach_header *mach_header, | |
297 | off_t *file_offset, | |
298 | off_t *macho_size, | |
299 | struct macho_data *macho_data, | |
f427ee49 A |
300 | struct vnode **vpp, |
301 | struct image_params *imgp | |
0a7de745 | 302 | ); |
1c79356b | 303 | |
b0d623f7 A |
304 | static inline void |
305 | widen_segment_command(const struct segment_command *scp32, | |
306 | struct segment_command_64 *scp) | |
307 | { | |
308 | scp->cmd = scp32->cmd; | |
309 | scp->cmdsize = scp32->cmdsize; | |
310 | bcopy(scp32->segname, scp->segname, sizeof(scp->segname)); | |
311 | scp->vmaddr = scp32->vmaddr; | |
312 | scp->vmsize = scp32->vmsize; | |
313 | scp->fileoff = scp32->fileoff; | |
314 | scp->filesize = scp32->filesize; | |
315 | scp->maxprot = scp32->maxprot; | |
316 | scp->initprot = scp32->initprot; | |
317 | scp->nsects = scp32->nsects; | |
318 | scp->flags = scp32->flags; | |
319 | } | |
320 | ||
321 | static void | |
322 | note_all_image_info_section(const struct segment_command_64 *scp, | |
323 | boolean_t is64, size_t section_size, const void *sections, | |
6d2010ae | 324 | int64_t slide, load_result_t *result) |
b0d623f7 A |
325 | { |
326 | const union { | |
327 | struct section s32; | |
328 | struct section_64 s64; | |
329 | } *sectionp; | |
330 | unsigned int i; | |
331 | ||
0a7de745 | 332 | |
f427ee49 A |
333 | if (strncmp(scp->segname, "__DATA_DIRTY", sizeof(scp->segname)) != 0 && |
334 | strncmp(scp->segname, "__DATA", sizeof(scp->segname)) != 0) { | |
b0d623f7 | 335 | return; |
0a7de745 | 336 | } |
b0d623f7 A |
337 | for (i = 0; i < scp->nsects; ++i) { |
338 | sectionp = (const void *) | |
339 | ((const char *)sections + section_size * i); | |
340 | if (0 == strncmp(sectionp->s64.sectname, "__all_image_info", | |
341 | sizeof(sectionp->s64.sectname))) { | |
342 | result->all_image_info_addr = | |
343 | is64 ? sectionp->s64.addr : sectionp->s32.addr; | |
6d2010ae | 344 | result->all_image_info_addr += slide; |
b0d623f7 A |
345 | result->all_image_info_size = |
346 | is64 ? sectionp->s64.size : sectionp->s32.size; | |
347 | return; | |
348 | } | |
349 | } | |
350 | } | |
351 | ||
5ba3f43e A |
352 | #if __arm64__ |
353 | /* | |
354 | * Allow bypassing some security rules (hard pagezero, no write+execute) | |
355 | * in exchange for better binary compatibility for legacy apps built | |
356 | * before 16KB-alignment was enforced. | |
357 | */ | |
d9a64523 A |
358 | const int fourk_binary_compatibility_unsafe = TRUE; |
359 | const int fourk_binary_compatibility_allow_wx = FALSE; | |
5ba3f43e | 360 | #endif /* __arm64__ */ |
3e170ce0 | 361 | |
f427ee49 A |
362 | #if __has_feature(ptrauth_calls) && XNU_TARGET_OS_OSX |
363 | /** | |
364 | * Determines whether this is an arm64e process which may host in-process | |
365 | * plugins. | |
366 | */ | |
367 | static inline bool | |
368 | arm64e_plugin_host(struct image_params *imgp, load_result_t *result) | |
369 | { | |
370 | if (imgp->ip_flags & IMGPF_NOJOP) { | |
371 | return false; | |
372 | } | |
373 | ||
374 | if (!result->platform_binary) { | |
375 | return false; | |
376 | } | |
377 | ||
378 | struct cs_blob *csblob = csvnode_get_blob(imgp->ip_vp, imgp->ip_arch_offset); | |
379 | const char *identity = csblob_get_identity(csblob); | |
380 | if (!identity) { | |
381 | return false; | |
382 | } | |
383 | ||
384 | /* Check if override host plugin entitlement is present and posix spawn attribute to disable A keys is passed */ | |
385 | if (IOVnodeHasEntitlement(imgp->ip_vp, (int64_t)imgp->ip_arch_offset, OVERRIDE_PLUGIN_HOST_ENTITLEMENT)) { | |
386 | return imgp->ip_flags & IMGPF_PLUGIN_HOST_DISABLE_A_KEYS; | |
387 | } | |
388 | ||
389 | /* Disabling library validation is a good signal that this process plans to host plugins */ | |
390 | const char *const disable_lv_entitlements[] = { | |
391 | "com.apple.security.cs.disable-library-validation", | |
392 | "com.apple.private.cs.automator-plugins", | |
393 | CLEAR_LV_ENTITLEMENT, | |
394 | }; | |
395 | for (size_t i = 0; i < ARRAY_COUNT(disable_lv_entitlements); i++) { | |
396 | if (IOVnodeHasEntitlement(imgp->ip_vp, (int64_t)imgp->ip_arch_offset, disable_lv_entitlements[i])) { | |
397 | return true; | |
398 | } | |
399 | } | |
400 | ||
401 | /* From /System/Library/Security/HardeningExceptions.plist */ | |
402 | const char *const hardening_exceptions[] = { | |
403 | "com.apple.perl5", /* Scripting engines may load third party code and jit*/ | |
404 | "com.apple.perl", /* Scripting engines may load third party code and jit*/ | |
405 | "org.python.python", /* Scripting engines may load third party code and jit*/ | |
406 | "com.apple.expect", /* Scripting engines may load third party code and jit*/ | |
407 | "com.tcltk.wish", /* Scripting engines may load third party code and jit*/ | |
408 | "com.tcltk.tclsh", /* Scripting engines may load third party code and jit*/ | |
409 | "com.apple.ruby", /* Scripting engines may load third party code and jit*/ | |
410 | "com.apple.bash", /* Required for the 'enable' command */ | |
411 | "com.apple.zsh", /* Required for the 'zmodload' command */ | |
412 | "com.apple.ksh", /* Required for 'builtin' command */ | |
413 | }; | |
414 | for (size_t i = 0; i < ARRAY_COUNT(hardening_exceptions); i++) { | |
415 | if (strncmp(hardening_exceptions[i], identity, strlen(hardening_exceptions[i])) == 0) { | |
416 | return true; | |
417 | } | |
418 | } | |
419 | ||
420 | return false; | |
421 | } | |
422 | #endif /* __has_feature(ptrauth_calls) && XNU_TARGET_OS_OSX */ | |
423 | ||
1c79356b A |
424 | load_return_t |
425 | load_machfile( | |
0a7de745 A |
426 | struct image_params *imgp, |
427 | struct mach_header *header, | |
428 | thread_t thread, | |
429 | vm_map_t *mapp, | |
430 | load_result_t *result | |
431 | ) | |
1c79356b | 432 | { |
0a7de745 A |
433 | struct vnode *vp = imgp->ip_vp; |
434 | off_t file_offset = imgp->ip_arch_offset; | |
435 | off_t macho_size = imgp->ip_arch_size; | |
eb6b6ca3 | 436 | off_t total_size = 0; |
0a7de745 A |
437 | off_t file_size = imgp->ip_vattr->va_data_size; |
438 | pmap_t pmap = 0; /* protected by create_map */ | |
439 | vm_map_t map; | |
440 | load_result_t myresult; | |
441 | load_return_t lret; | |
fe8ab488 | 442 | boolean_t enforce_hard_pagezero = TRUE; |
743345f9 | 443 | int in_exec = (imgp->ip_flags & IMGPF_EXEC); |
b0d623f7 | 444 | task_t task = current_task(); |
0a7de745 A |
445 | int64_t aslr_page_offset = 0; |
446 | int64_t dyld_aslr_page_offset = 0; | |
447 | int64_t aslr_section_size = 0; | |
448 | int64_t aslr_section_offset = 0; | |
449 | kern_return_t kret; | |
cb323159 | 450 | unsigned int pmap_flags = 0; |
b0d623f7 | 451 | |
eb6b6ca3 A |
452 | if (os_add_overflow(file_offset, macho_size, &total_size) || |
453 | total_size > file_size) { | |
0a7de745 | 454 | return LOAD_BADMACHO; |
316670eb A |
455 | } |
456 | ||
d9a64523 A |
457 | result->is_64bit_addr = ((imgp->ip_flags & IMGPF_IS_64BIT_ADDR) == IMGPF_IS_64BIT_ADDR); |
458 | result->is_64bit_data = ((imgp->ip_flags & IMGPF_IS_64BIT_DATA) == IMGPF_IS_64BIT_DATA); | |
cb323159 A |
459 | #if defined(HAS_APPLE_PAC) |
460 | pmap_flags |= (imgp->ip_flags & IMGPF_NOJOP) ? PMAP_CREATE_DISABLE_JOP : 0; | |
461 | #endif /* defined(HAS_APPLE_PAC) */ | |
462 | pmap_flags |= result->is_64bit_addr ? PMAP_CREATE_64BIT : 0; | |
39037602 | 463 | |
743345f9 A |
464 | task_t ledger_task; |
465 | if (imgp->ip_new_thread) { | |
466 | ledger_task = get_threadtask(imgp->ip_new_thread); | |
467 | } else { | |
468 | ledger_task = task; | |
0b4e3aa0 | 469 | } |
f427ee49 A |
470 | |
471 | #if defined(PMAP_CREATE_FORCE_4K_PAGES) && (DEBUG || DEVELOPMENT) | |
472 | if (imgp->ip_px_sa != NULL) { | |
473 | struct _posix_spawnattr* psa = (struct _posix_spawnattr *) imgp->ip_px_sa; | |
474 | if (psa->psa_flags & _POSIX_SPAWN_FORCE_4K_PAGES) { | |
475 | pmap_flags |= PMAP_CREATE_FORCE_4K_PAGES; | |
476 | } | |
477 | } | |
478 | #endif /* defined(PMAP_CREATE_FORCE_4K_PAGES) && (DEBUG || DEVELOPMENT) */ | |
479 | ||
cb323159 | 480 | pmap = pmap_create_options(get_task_ledger(ledger_task), |
0a7de745 | 481 | (vm_map_size_t) 0, |
cb323159 A |
482 | pmap_flags); |
483 | if (pmap == NULL) { | |
484 | return LOAD_RESOURCE; | |
485 | } | |
743345f9 | 486 | map = vm_map_create(pmap, |
0a7de745 A |
487 | 0, |
488 | vm_compute_max_offset(result->is_64bit_addr), | |
489 | TRUE); | |
0c530ab8 | 490 | |
5ba3f43e | 491 | #if defined(__arm64__) |
d9a64523 | 492 | if (result->is_64bit_addr) { |
5ba3f43e A |
493 | /* enforce 16KB alignment of VM map entries */ |
494 | vm_map_set_page_shift(map, SIXTEENK_PAGE_SHIFT); | |
495 | } else { | |
496 | vm_map_set_page_shift(map, page_shift_user32); | |
497 | } | |
498 | #elif (__ARM_ARCH_7K__ >= 2) && defined(PLATFORM_WatchOS) | |
3e170ce0 A |
499 | /* enforce 16KB alignment for watch targets with new ABI */ |
500 | vm_map_set_page_shift(map, SIXTEENK_PAGE_SHIFT); | |
501 | #endif /* __arm64__ */ | |
fe8ab488 | 502 | |
f427ee49 A |
503 | #if PMAP_CREATE_FORCE_4K_PAGES |
504 | if (pmap_flags & PMAP_CREATE_FORCE_4K_PAGES) { | |
505 | DEBUG4K_LIFE("***** launching '%s' as 4k *****\n", vp->v_name); | |
506 | vm_map_set_page_shift(map, FOURK_PAGE_SHIFT); | |
507 | } | |
508 | #endif /* PMAP_CREATE_FORCE_4K_PAGES */ | |
509 | ||
0a7de745 | 510 | #ifndef CONFIG_ENFORCE_SIGNED_CODE |
39236c6e A |
511 | /* This turns off faulting for executable pages, which allows |
512 | * to circumvent Code Signing Enforcement. The per process | |
513 | * flag (CS_ENFORCEMENT) is not set yet, but we can use the | |
514 | * global flag. | |
515 | */ | |
0a7de745 A |
516 | if (!cs_process_global_enforcement() && (header->flags & MH_ALLOW_STACK_EXECUTION)) { |
517 | vm_map_disable_NX(map); | |
518 | // TODO: Message Trace or log that this is happening | |
d9a64523 | 519 | } |
7e4a7d39 | 520 | #endif |
6d2010ae A |
521 | |
522 | /* Forcibly disallow execution from data pages on even if the arch | |
523 | * normally permits it. */ | |
0a7de745 | 524 | if ((header->flags & MH_NO_HEAP_EXECUTION) && !(imgp->ip_flags & IMGPF_ALLOW_DATA_EXEC)) { |
6d2010ae | 525 | vm_map_disallow_data_exec(map); |
0a7de745 | 526 | } |
5ba3f43e | 527 | |
6d2010ae | 528 | /* |
39236c6e | 529 | * Compute a random offset for ASLR, and an independent random offset for dyld. |
6d2010ae A |
530 | */ |
531 | if (!(imgp->ip_flags & IMGPF_DISABLE_ASLR)) { | |
5ba3f43e A |
532 | vm_map_get_max_aslr_slide_section(map, &aslr_section_offset, &aslr_section_size); |
533 | aslr_section_offset = (random() % aslr_section_offset) * aslr_section_size; | |
39236c6e | 534 | |
5ba3f43e A |
535 | aslr_page_offset = random(); |
536 | aslr_page_offset %= vm_map_get_max_aslr_slide_pages(map); | |
537 | aslr_page_offset <<= vm_map_page_shift(map); | |
39236c6e | 538 | |
5ba3f43e A |
539 | dyld_aslr_page_offset = random(); |
540 | dyld_aslr_page_offset %= vm_map_get_max_loader_aslr_slide_pages(map); | |
541 | dyld_aslr_page_offset <<= vm_map_page_shift(map); | |
39236c6e | 542 | |
5ba3f43e | 543 | aslr_page_offset += aslr_section_offset; |
6d2010ae | 544 | } |
f427ee49 A |
545 | if (vm_map_page_shift(map) < (int)PAGE_SHIFT) { |
546 | DEBUG4K_LOAD("slide=0x%llx dyld_slide=0x%llx\n", aslr_page_offset, dyld_aslr_page_offset); | |
547 | } | |
5ba3f43e | 548 | |
0a7de745 | 549 | if (!result) { |
1c79356b | 550 | result = &myresult; |
0a7de745 | 551 | } |
1c79356b | 552 | |
91447636 | 553 | *result = load_result_null; |
1c79356b | 554 | |
39037602 A |
555 | /* |
556 | * re-set the bitness on the load result since we cleared the load result above. | |
557 | */ | |
d9a64523 A |
558 | result->is_64bit_addr = ((imgp->ip_flags & IMGPF_IS_64BIT_ADDR) == IMGPF_IS_64BIT_ADDR); |
559 | result->is_64bit_data = ((imgp->ip_flags & IMGPF_IS_64BIT_DATA) == IMGPF_IS_64BIT_DATA); | |
39037602 | 560 | |
2d21ac55 | 561 | lret = parse_machfile(vp, map, thread, header, file_offset, macho_size, |
0a7de745 A |
562 | 0, aslr_page_offset, dyld_aslr_page_offset, result, |
563 | NULL, imgp); | |
1c79356b A |
564 | |
565 | if (lret != LOAD_SUCCESS) { | |
0a7de745 A |
566 | vm_map_deallocate(map); /* will lose pmap reference too */ |
567 | return lret; | |
1c79356b | 568 | } |
55e303ae | 569 | |
fe8ab488 A |
570 | #if __x86_64__ |
571 | /* | |
572 | * On x86, for compatibility, don't enforce the hard page-zero restriction for 32-bit binaries. | |
573 | */ | |
d9a64523 | 574 | if (!result->is_64bit_addr) { |
fe8ab488 A |
575 | enforce_hard_pagezero = FALSE; |
576 | } | |
5ba3f43e A |
577 | |
578 | /* | |
579 | * For processes with IMGPF_HIGH_BITS_ASLR, add a few random high bits | |
580 | * to the start address for "anywhere" memory allocations. | |
581 | */ | |
582 | #define VM_MAP_HIGH_START_BITS_COUNT 8 | |
583 | #define VM_MAP_HIGH_START_BITS_SHIFT 27 | |
d9a64523 | 584 | if (result->is_64bit_addr && |
5ba3f43e A |
585 | (imgp->ip_flags & IMGPF_HIGH_BITS_ASLR)) { |
586 | int random_bits; | |
587 | vm_map_offset_t high_start; | |
588 | ||
589 | random_bits = random(); | |
0a7de745 | 590 | random_bits &= (1 << VM_MAP_HIGH_START_BITS_COUNT) - 1; |
5ba3f43e | 591 | high_start = (((vm_map_offset_t)random_bits) |
0a7de745 | 592 | << VM_MAP_HIGH_START_BITS_SHIFT); |
5ba3f43e A |
593 | vm_map_set_high_start(map, high_start); |
594 | } | |
595 | #endif /* __x86_64__ */ | |
596 | ||
0c530ab8 | 597 | /* |
fe8ab488 | 598 | * Check to see if the page zero is enforced by the map->min_offset. |
0a7de745 | 599 | */ |
3e170ce0 A |
600 | if (enforce_hard_pagezero && |
601 | (vm_map_has_hard_pagezero(map, 0x1000) == FALSE)) { | |
5ba3f43e | 602 | #if __arm64__ |
f427ee49 A |
603 | if ( |
604 | !result->is_64bit_addr && /* not 64-bit address space */ | |
605 | !(header->flags & MH_PIE) && /* not PIE */ | |
606 | (vm_map_page_shift(map) != FOURK_PAGE_SHIFT || | |
607 | PAGE_SHIFT != FOURK_PAGE_SHIFT) && /* page size != 4KB */ | |
608 | result->has_pagezero && /* has a "soft" page zero */ | |
609 | fourk_binary_compatibility_unsafe) { | |
5ba3f43e A |
610 | /* |
611 | * For backwards compatibility of "4K" apps on | |
612 | * a 16K system, do not enforce a hard page zero... | |
613 | */ | |
614 | } else | |
615 | #endif /* __arm64__ */ | |
3e170ce0 | 616 | { |
0a7de745 A |
617 | vm_map_deallocate(map); /* will lose pmap reference too */ |
618 | return LOAD_BADMACHO; | |
fe8ab488 | 619 | } |
316670eb | 620 | } |
fe8ab488 | 621 | |
f427ee49 A |
622 | #if __arm64__ |
623 | if (enforce_hard_pagezero && result->is_64bit_addr && (header->cputype == CPU_TYPE_ARM64)) { | |
624 | /* 64 bit ARM binary must have "hard page zero" of 4GB to cover the lower 32 bit address space */ | |
625 | if (vm_map_has_hard_pagezero(map, 0x100000000) == FALSE) { | |
626 | vm_map_deallocate(map); /* will lose pmap reference too */ | |
627 | return LOAD_BADMACHO; | |
628 | } | |
629 | } | |
630 | #endif | |
631 | ||
39037602 A |
632 | vm_commit_pagezero_status(map); |
633 | ||
743345f9 A |
634 | /* |
635 | * If this is an exec, then we are going to destroy the old | |
636 | * task, and it's correct to halt it; if it's spawn, the | |
637 | * task is not yet running, and it makes no sense. | |
638 | */ | |
639 | if (in_exec) { | |
cb323159 | 640 | proc_t p = vfs_context_proc(imgp->ip_vfs_context); |
b0d623f7 | 641 | /* |
743345f9 A |
642 | * Mark the task as halting and start the other |
643 | * threads towards terminating themselves. Then | |
644 | * make sure any threads waiting for a process | |
645 | * transition get informed that we are committed to | |
646 | * this transition, and then finally complete the | |
647 | * task halting (wait for threads and then cleanup | |
648 | * task resources). | |
649 | * | |
650 | * NOTE: task_start_halt() makes sure that no new | |
651 | * threads are created in the task during the transition. | |
652 | * We need to mark the workqueue as exiting before we | |
653 | * wait for threads to terminate (at the end of which | |
654 | * we no longer have a prohibition on thread creation). | |
655 | * | |
656 | * Finally, clean up any lingering workqueue data structures | |
657 | * that may have been left behind by the workqueue threads | |
658 | * as they exited (and then clean up the work queue itself). | |
b0d623f7 | 659 | */ |
743345f9 A |
660 | kret = task_start_halt(task); |
661 | if (kret != KERN_SUCCESS) { | |
0a7de745 A |
662 | vm_map_deallocate(map); /* will lose pmap reference too */ |
663 | return LOAD_FAILURE; | |
316670eb | 664 | } |
743345f9 | 665 | proc_transcommit(p, 0); |
d9a64523 | 666 | workq_mark_exiting(p); |
743345f9 | 667 | task_complete_halt(task); |
d9a64523 | 668 | workq_exit(p); |
5ba3f43e | 669 | |
743345f9 A |
670 | /* |
671 | * Roll up accounting info to new task. The roll up is done after | |
672 | * task_complete_halt to make sure the thread accounting info is | |
673 | * rolled up to current_task. | |
674 | */ | |
675 | task_rollup_accounting_info(get_threadtask(thread), task); | |
0b4e3aa0 | 676 | } |
743345f9 | 677 | *mapp = map; |
527f9951 | 678 | |
f427ee49 A |
679 | #if __has_feature(ptrauth_calls) && defined(XNU_TARGET_OS_OSX) |
680 | /* | |
681 | * arm64e plugin hosts currently run with JOP keys disabled, since they | |
682 | * may need to run arm64 plugins. | |
683 | */ | |
684 | if (arm64e_plugin_host(imgp, result)) { | |
685 | imgp->ip_flags |= IMGPF_NOJOP; | |
686 | pmap_disable_user_jop(pmap); | |
687 | } | |
688 | #endif /* __has_feature(ptrauth_calls) && defined(XNU_TARGET_OS_OSX) */ | |
689 | ||
527f9951 | 690 | #ifdef CONFIG_32BIT_TELEMETRY |
d9a64523 | 691 | if (!result->is_64bit_data) { |
527f9951 A |
692 | /* |
693 | * This may not need to be an AST; we merely need to ensure that | |
694 | * we gather telemetry at the point where all of the information | |
695 | * that we want has been added to the process. | |
696 | */ | |
697 | task_set_32bit_log_flag(get_threadtask(thread)); | |
698 | act_set_astbsd(thread); | |
699 | } | |
700 | #endif /* CONFIG_32BIT_TELEMETRY */ | |
701 | ||
0a7de745 | 702 | return LOAD_SUCCESS; |
1c79356b A |
703 | } |
704 | ||
3e170ce0 | 705 | int macho_printf = 0; |
0a7de745 A |
706 | #define MACHO_PRINTF(args) \ |
707 | do { \ | |
708 | if (macho_printf) { \ | |
709 | printf args; \ | |
710 | } \ | |
3e170ce0 A |
711 | } while (0) |
712 | ||
f427ee49 A |
713 | |
714 | static boolean_t | |
715 | pie_required( | |
716 | cpu_type_t exectype, | |
717 | cpu_subtype_t execsubtype) | |
718 | { | |
719 | switch (exectype) { | |
720 | case CPU_TYPE_X86_64: | |
721 | return FALSE; | |
722 | case CPU_TYPE_ARM64: | |
723 | return TRUE; | |
724 | case CPU_TYPE_ARM: | |
725 | switch (execsubtype) { | |
726 | case CPU_SUBTYPE_ARM_V7K: | |
727 | return TRUE; | |
728 | } | |
729 | break; | |
730 | } | |
731 | return FALSE; | |
732 | } | |
733 | ||
91447636 A |
734 | /* |
735 | * The file size of a mach-o file is limited to 32 bits; this is because | |
736 | * this is the limit on the kalloc() of enough bytes for a mach_header and | |
737 | * the contents of its sizeofcmds, which is currently constrained to 32 | |
738 | * bits in the file format itself. We read into the kernel buffer the | |
739 | * commands section, and then parse it in order to parse the mach-o file | |
740 | * format load_command segment(s). We are only interested in a subset of | |
6d2010ae A |
741 | * the total set of possible commands. If "map"==VM_MAP_NULL or |
742 | * "thread"==THREAD_NULL, do not make permament VM modifications, | |
743 | * just preflight the parse. | |
91447636 | 744 | */ |
1c79356b A |
745 | static |
746 | load_return_t | |
747 | parse_machfile( | |
0a7de745 A |
748 | struct vnode *vp, |
749 | vm_map_t map, | |
750 | thread_t thread, | |
751 | struct mach_header *header, | |
752 | off_t file_offset, | |
753 | off_t macho_size, | |
754 | int depth, | |
755 | int64_t aslr_offset, | |
756 | int64_t dyld_aslr_offset, | |
757 | load_result_t *result, | |
758 | load_result_t *binresult, | |
759 | struct image_params *imgp | |
760 | ) | |
1c79356b | 761 | { |
0a7de745 A |
762 | uint32_t ncmds; |
763 | struct load_command *lcp; | |
764 | struct dylinker_command *dlp = 0; | |
0a7de745 A |
765 | void * control; |
766 | load_return_t ret = LOAD_SUCCESS; | |
767 | void * addr; | |
768 | vm_size_t alloc_size, cmds_size; | |
769 | size_t offset; | |
770 | size_t oldoffset; /* for overflow check */ | |
771 | int pass; | |
cb323159 | 772 | proc_t p = vfs_context_proc(imgp->ip_vfs_context); |
0a7de745 A |
773 | int error; |
774 | int resid = 0; | |
cb323159 A |
775 | int spawn = (imgp->ip_flags & IMGPF_SPAWN); |
776 | int vfexec = (imgp->ip_flags & IMGPF_VFORK_EXEC); | |
0a7de745 A |
777 | size_t mach_header_sz = sizeof(struct mach_header); |
778 | boolean_t abi64; | |
779 | boolean_t got_code_signatures = FALSE; | |
780 | boolean_t found_header_segment = FALSE; | |
781 | boolean_t found_xhdr = FALSE; | |
cb323159 | 782 | boolean_t found_version_cmd = FALSE; |
0a7de745 A |
783 | int64_t slide = 0; |
784 | boolean_t dyld_no_load_addr = FALSE; | |
785 | boolean_t is_dyld = FALSE; | |
f427ee49 | 786 | vm_map_offset_t effective_page_mask = PAGE_MASK; |
5ba3f43e | 787 | #if __arm64__ |
f427ee49 A |
788 | uint64_t pagezero_end = 0; |
789 | uint64_t executable_end = 0; | |
790 | uint64_t writable_start = 0; | |
0a7de745 | 791 | vm_map_size_t effective_page_size; |
5ba3f43e | 792 | |
f427ee49 A |
793 | effective_page_mask = vm_map_page_mask(map); |
794 | effective_page_size = vm_map_page_size(map); | |
5ba3f43e | 795 | #endif /* __arm64__ */ |
91447636 A |
796 | |
797 | if (header->magic == MH_MAGIC_64 || | |
798 | header->magic == MH_CIGAM_64) { | |
0a7de745 | 799 | mach_header_sz = sizeof(struct mach_header_64); |
91447636 | 800 | } |
1c79356b A |
801 | |
802 | /* | |
803 | * Break infinite recursion | |
804 | */ | |
f427ee49 | 805 | if (depth > 2) { |
0a7de745 | 806 | return LOAD_FAILURE; |
2d21ac55 | 807 | } |
0b4e3aa0 | 808 | |
1c79356b A |
809 | depth++; |
810 | ||
811 | /* | |
812 | * Check to see if right machine type. | |
813 | */ | |
f427ee49 A |
814 | if (((cpu_type_t)(header->cputype & ~CPU_ARCH_MASK) != (cpu_type() & ~CPU_ARCH_MASK)) |
815 | ) { | |
816 | return LOAD_BADARCH; | |
817 | } | |
818 | ||
819 | if (!grade_binary(header->cputype, | |
820 | header->cpusubtype & ~CPU_SUBTYPE_MASK, | |
821 | header->cpusubtype & CPU_SUBTYPE_MASK, TRUE)) { | |
0a7de745 A |
822 | return LOAD_BADARCH; |
823 | } | |
a39ff7e2 | 824 | |
91447636 | 825 | abi64 = ((header->cputype & CPU_ARCH_ABI64) == CPU_ARCH_ABI64); |
a39ff7e2 | 826 | |
1c79356b | 827 | switch (header->filetype) { |
1c79356b | 828 | case MH_EXECUTE: |
f427ee49 | 829 | if (depth != 1 && depth != 3) { |
0a7de745 | 830 | return LOAD_FAILURE; |
2d21ac55 | 831 | } |
5ba3f43e A |
832 | if (header->flags & MH_DYLDLINK) { |
833 | /* Check properties of dynamic executables */ | |
834 | if (!(header->flags & MH_PIE) && pie_required(header->cputype, header->cpusubtype & ~CPU_SUBTYPE_MASK)) { | |
0a7de745 | 835 | return LOAD_FAILURE; |
5ba3f43e A |
836 | } |
837 | result->needs_dynlinker = TRUE; | |
f427ee49 A |
838 | } else if (header->cputype == CPU_TYPE_X86_64) { |
839 | /* x86_64 static binaries allowed */ | |
5ba3f43e A |
840 | } else { |
841 | /* Check properties of static executables (disallowed except for development) */ | |
842 | #if !(DEVELOPMENT || DEBUG) | |
0a7de745 | 843 | return LOAD_FAILURE; |
5ba3f43e A |
844 | #endif |
845 | } | |
c18c124e | 846 | break; |
1c79356b | 847 | case MH_DYLINKER: |
2d21ac55 | 848 | if (depth != 2) { |
0a7de745 | 849 | return LOAD_FAILURE; |
2d21ac55 | 850 | } |
39037602 | 851 | is_dyld = TRUE; |
1c79356b | 852 | break; |
0a7de745 | 853 | |
1c79356b | 854 | default: |
0a7de745 | 855 | return LOAD_FAILURE; |
1c79356b A |
856 | } |
857 | ||
f427ee49 A |
858 | /* |
859 | * For PIE and dyld, slide everything by the ASLR offset. | |
860 | */ | |
861 | if ((header->flags & MH_PIE) || is_dyld) { | |
862 | slide = aslr_offset; | |
863 | } | |
864 | ||
1c79356b A |
865 | /* |
866 | * Get the pager for the file. | |
867 | */ | |
b0d623f7 | 868 | control = ubc_getobject(vp, UBC_FLAGS_NONE); |
1c79356b | 869 | |
5ba3f43e A |
870 | /* ensure header + sizeofcmds falls within the file */ |
871 | if (os_add_overflow(mach_header_sz, header->sizeofcmds, &cmds_size) || | |
0a7de745 | 872 | (off_t)cmds_size > macho_size || |
f427ee49 A |
873 | round_page_overflow(cmds_size, &alloc_size) || |
874 | alloc_size > INT_MAX) { | |
5ba3f43e A |
875 | return LOAD_BADMACHO; |
876 | } | |
1c79356b A |
877 | |
878 | /* | |
879 | * Map the load commands into kernel memory. | |
880 | */ | |
5ba3f43e A |
881 | addr = kalloc(alloc_size); |
882 | if (addr == NULL) { | |
883 | return LOAD_NOSPACE; | |
884 | } | |
0b4e3aa0 | 885 | |
f427ee49 | 886 | error = vn_rdwr(UIO_READ, vp, addr, (int)alloc_size, file_offset, |
cb323159 | 887 | UIO_SYSSPACE, 0, vfs_context_ucred(imgp->ip_vfs_context), &resid, p); |
91447636 | 888 | if (error) { |
5ba3f43e A |
889 | kfree(addr, alloc_size); |
890 | return LOAD_IOERROR; | |
1c79356b | 891 | } |
6d2010ae | 892 | |
c18c124e | 893 | if (resid) { |
f427ee49 A |
894 | { |
895 | /* We must be able to read in as much as the mach_header indicated */ | |
896 | kfree(addr, alloc_size); | |
897 | return LOAD_BADMACHO; | |
898 | } | |
6d2010ae A |
899 | } |
900 | ||
39037602 | 901 | /* |
39236c6e A |
902 | * Scan through the commands, processing each one as necessary. |
903 | * We parse in three passes through the headers: | |
f427ee49 | 904 | * 0: determine if TEXT and DATA boundary can be page-aligned, load platform version |
39236c6e A |
905 | * 1: thread state, uuid, code signature |
906 | * 2: segments | |
907 | * 3: dyld, encryption, check entry point | |
1c79356b | 908 | */ |
39037602 A |
909 | |
910 | boolean_t slide_realign = FALSE; | |
5ba3f43e A |
911 | #if __arm64__ |
912 | if (!abi64) { | |
913 | slide_realign = TRUE; | |
914 | } | |
915 | #endif | |
39037602 | 916 | |
3e170ce0 | 917 | for (pass = 0; pass <= 3; pass++) { |
f427ee49 | 918 | if (pass == 1) { |
5ba3f43e | 919 | #if __arm64__ |
0a7de745 A |
920 | boolean_t is_pie; |
921 | int64_t adjust; | |
5ba3f43e A |
922 | |
923 | is_pie = ((header->flags & MH_PIE) != 0); | |
924 | if (pagezero_end != 0 && | |
925 | pagezero_end < effective_page_size) { | |
926 | /* need at least 1 page for PAGEZERO */ | |
927 | adjust = effective_page_size; | |
928 | MACHO_PRINTF(("pagezero boundary at " | |
0a7de745 A |
929 | "0x%llx; adjust slide from " |
930 | "0x%llx to 0x%llx%s\n", | |
931 | (uint64_t) pagezero_end, | |
932 | slide, | |
933 | slide + adjust, | |
934 | (is_pie | |
935 | ? "" | |
936 | : " BUT NO PIE ****** :-("))); | |
5ba3f43e A |
937 | if (is_pie) { |
938 | slide += adjust; | |
939 | pagezero_end += adjust; | |
940 | executable_end += adjust; | |
941 | writable_start += adjust; | |
942 | } | |
943 | } | |
944 | if (pagezero_end != 0) { | |
945 | result->has_pagezero = TRUE; | |
946 | } | |
0a7de745 | 947 | if (executable_end == writable_start && |
5ba3f43e A |
948 | (executable_end & effective_page_mask) != 0 && |
949 | (executable_end & FOURK_PAGE_MASK) == 0) { | |
5ba3f43e A |
950 | /* |
951 | * The TEXT/DATA boundary is 4K-aligned but | |
952 | * not page-aligned. Adjust the slide to make | |
953 | * it page-aligned and avoid having a page | |
954 | * with both write and execute permissions. | |
955 | */ | |
956 | adjust = | |
0a7de745 A |
957 | (effective_page_size - |
958 | (executable_end & effective_page_mask)); | |
5ba3f43e | 959 | MACHO_PRINTF(("page-unaligned X-W boundary at " |
0a7de745 A |
960 | "0x%llx; adjust slide from " |
961 | "0x%llx to 0x%llx%s\n", | |
962 | (uint64_t) executable_end, | |
963 | slide, | |
964 | slide + adjust, | |
965 | (is_pie | |
966 | ? "" | |
967 | : " BUT NO PIE ****** :-("))); | |
968 | if (is_pie) { | |
5ba3f43e | 969 | slide += adjust; |
0a7de745 | 970 | } |
5ba3f43e A |
971 | } |
972 | #endif /* __arm64__ */ | |
39037602 A |
973 | |
974 | if (dyld_no_load_addr && binresult) { | |
975 | /* | |
976 | * The dyld Mach-O does not specify a load address. Try to locate | |
977 | * it right after the main binary. If binresult == NULL, load | |
978 | * directly to the given slide. | |
979 | */ | |
f427ee49 A |
980 | mach_vm_address_t max_vm_addr = binresult->max_vm_addr; |
981 | slide = vm_map_round_page(slide + max_vm_addr, effective_page_mask); | |
39037602 | 982 | } |
3e170ce0 | 983 | } |
6d2010ae | 984 | |
6d2010ae | 985 | /* |
f427ee49 | 986 | * Check that the entry point is contained in an executable segment |
0a7de745 | 987 | */ |
f427ee49 | 988 | if ((pass == 3) && (thread != THREAD_NULL)) { |
cb323159 A |
989 | if (depth == 1 && imgp && (imgp->ip_flags & IMGPF_DRIVER)) { |
990 | /* Driver binaries must have driverkit platform */ | |
991 | if (result->ip_platform == PLATFORM_DRIVERKIT) { | |
992 | /* Driver binaries have no entry point */ | |
993 | ret = setup_driver_main(thread, slide, result); | |
994 | } else { | |
995 | ret = LOAD_FAILURE; | |
996 | } | |
997 | } else if (!result->using_lcmain && result->validentry == 0) { | |
998 | ret = LOAD_FAILURE; | |
999 | } | |
1000 | if (ret != KERN_SUCCESS) { | |
1001 | thread_state_initialize(thread); | |
1002 | break; | |
1003 | } | |
6d2010ae | 1004 | } |
6d2010ae | 1005 | |
39037602 A |
1006 | /* |
1007 | * Check that some segment maps the start of the mach-o file, which is | |
1008 | * needed by the dynamic loader to read the mach headers, etc. | |
1009 | */ | |
1010 | if ((pass == 3) && (found_header_segment == FALSE)) { | |
1011 | ret = LOAD_BADMACHO; | |
1012 | break; | |
1013 | } | |
1014 | ||
a3d08fcd A |
1015 | /* |
1016 | * Loop through each of the load_commands indicated by the | |
1017 | * Mach-O header; if an absurd value is provided, we just | |
1018 | * run off the end of the reserved section by incrementing | |
1019 | * the offset too far, so we are implicitly fail-safe. | |
1020 | */ | |
91447636 | 1021 | offset = mach_header_sz; |
1c79356b | 1022 | ncmds = header->ncmds; |
6d2010ae | 1023 | |
1c79356b | 1024 | while (ncmds--) { |
5ba3f43e A |
1025 | /* ensure enough space for a minimal load command */ |
1026 | if (offset + sizeof(struct load_command) > cmds_size) { | |
1027 | ret = LOAD_BADMACHO; | |
1028 | break; | |
1029 | } | |
1030 | ||
1c79356b A |
1031 | /* |
1032 | * Get a pointer to the command. | |
1033 | */ | |
1034 | lcp = (struct load_command *)(addr + offset); | |
a3d08fcd | 1035 | oldoffset = offset; |
1c79356b A |
1036 | |
1037 | /* | |
a3d08fcd A |
1038 | * Perform prevalidation of the struct load_command |
1039 | * before we attempt to use its contents. Invalid | |
1040 | * values are ones which result in an overflow, or | |
1041 | * which can not possibly be valid commands, or which | |
1042 | * straddle or exist past the reserved section at the | |
1043 | * start of the image. | |
1c79356b | 1044 | */ |
5ba3f43e | 1045 | if (os_add_overflow(offset, lcp->cmdsize, &offset) || |
0a7de745 A |
1046 | lcp->cmdsize < sizeof(struct load_command) || |
1047 | offset > cmds_size) { | |
91447636 | 1048 | ret = LOAD_BADMACHO; |
a3d08fcd | 1049 | break; |
1c79356b A |
1050 | } |
1051 | ||
1052 | /* | |
a3d08fcd A |
1053 | * Act on struct load_command's for which kernel |
1054 | * intervention is required. | |
cb323159 A |
1055 | * Note that each load command implementation is expected to validate |
1056 | * that lcp->cmdsize is large enough to fit its specific struct type | |
1057 | * before dereferencing fields not covered by struct load_command. | |
1c79356b | 1058 | */ |
0a7de745 | 1059 | switch (lcp->cmd) { |
39037602 A |
1060 | case LC_SEGMENT: { |
1061 | struct segment_command *scp = (struct segment_command *) lcp; | |
cb323159 A |
1062 | if (scp->cmdsize < sizeof(*scp)) { |
1063 | ret = LOAD_BADMACHO; | |
1064 | break; | |
1065 | } | |
3e170ce0 | 1066 | if (pass == 0) { |
39037602 A |
1067 | if (is_dyld && scp->vmaddr == 0 && scp->fileoff == 0) { |
1068 | dyld_no_load_addr = TRUE; | |
1069 | if (!slide_realign) { | |
1070 | /* got what we need, bail early on pass 0 */ | |
1071 | continue; | |
1072 | } | |
1073 | } | |
1074 | ||
5ba3f43e A |
1075 | #if __arm64__ |
1076 | assert(!abi64); | |
1077 | ||
1078 | if (scp->initprot == 0 && scp->maxprot == 0 && scp->vmaddr == 0) { | |
1079 | /* PAGEZERO */ | |
f427ee49 | 1080 | if (os_add3_overflow(scp->vmaddr, scp->vmsize, slide, &pagezero_end) || pagezero_end > UINT32_MAX) { |
5ba3f43e A |
1081 | ret = LOAD_BADMACHO; |
1082 | break; | |
1083 | } | |
1084 | } | |
1085 | if (scp->initprot & VM_PROT_EXECUTE) { | |
1086 | /* TEXT */ | |
f427ee49 | 1087 | if (os_add3_overflow(scp->vmaddr, scp->vmsize, slide, &executable_end) || executable_end > UINT32_MAX) { |
5ba3f43e A |
1088 | ret = LOAD_BADMACHO; |
1089 | break; | |
1090 | } | |
1091 | } | |
1092 | if (scp->initprot & VM_PROT_WRITE) { | |
1093 | /* DATA */ | |
f427ee49 | 1094 | if (os_add_overflow(scp->vmaddr, slide, &writable_start) || writable_start > UINT32_MAX) { |
5ba3f43e A |
1095 | ret = LOAD_BADMACHO; |
1096 | break; | |
1097 | } | |
1098 | } | |
1099 | #endif /* __arm64__ */ | |
3e170ce0 A |
1100 | break; |
1101 | } | |
1102 | ||
39037602 A |
1103 | if (pass == 1 && !strncmp(scp->segname, "__XHDR", sizeof(scp->segname))) { |
1104 | found_xhdr = TRUE; | |
1105 | } | |
1106 | ||
0a7de745 | 1107 | if (pass != 2) { |
39236c6e | 1108 | break; |
0a7de745 | 1109 | } |
39236c6e A |
1110 | |
1111 | if (abi64) { | |
1112 | /* | |
1113 | * Having an LC_SEGMENT command for the | |
1114 | * wrong ABI is invalid <rdar://problem/11021230> | |
1115 | */ | |
1116 | ret = LOAD_BADMACHO; | |
1117 | break; | |
1118 | } | |
1119 | ||
1120 | ret = load_segment(lcp, | |
0a7de745 A |
1121 | header->filetype, |
1122 | control, | |
1123 | file_offset, | |
1124 | macho_size, | |
1125 | vp, | |
1126 | map, | |
1127 | slide, | |
f427ee49 A |
1128 | result, |
1129 | imgp); | |
39037602 A |
1130 | if (ret == LOAD_SUCCESS && scp->fileoff == 0 && scp->filesize > 0) { |
1131 | /* Enforce a single segment mapping offset zero, with R+X | |
1132 | * protection. */ | |
1133 | if (found_header_segment || | |
0a7de745 | 1134 | ((scp->initprot & (VM_PROT_READ | VM_PROT_EXECUTE)) != (VM_PROT_READ | VM_PROT_EXECUTE))) { |
39037602 A |
1135 | ret = LOAD_BADMACHO; |
1136 | break; | |
1137 | } | |
1138 | found_header_segment = TRUE; | |
1139 | } | |
1140 | ||
39236c6e | 1141 | break; |
39037602 A |
1142 | } |
1143 | case LC_SEGMENT_64: { | |
1144 | struct segment_command_64 *scp64 = (struct segment_command_64 *) lcp; | |
cb323159 A |
1145 | if (scp64->cmdsize < sizeof(*scp64)) { |
1146 | ret = LOAD_BADMACHO; | |
1147 | break; | |
1148 | } | |
39037602 A |
1149 | if (pass == 0) { |
1150 | if (is_dyld && scp64->vmaddr == 0 && scp64->fileoff == 0) { | |
1151 | dyld_no_load_addr = TRUE; | |
39037602 | 1152 | } |
f427ee49 A |
1153 | /* got what we need, bail early on pass 0 */ |
1154 | continue; | |
39037602 A |
1155 | } |
1156 | ||
1157 | if (pass == 1 && !strncmp(scp64->segname, "__XHDR", sizeof(scp64->segname))) { | |
1158 | found_xhdr = TRUE; | |
1159 | } | |
1160 | ||
0a7de745 | 1161 | if (pass != 2) { |
1c79356b | 1162 | break; |
0a7de745 | 1163 | } |
39236c6e A |
1164 | |
1165 | if (!abi64) { | |
1166 | /* | |
1167 | * Having an LC_SEGMENT_64 command for the | |
1168 | * wrong ABI is invalid <rdar://problem/11021230> | |
1169 | */ | |
1170 | ret = LOAD_BADMACHO; | |
1171 | break; | |
1172 | } | |
1173 | ||
b0d623f7 | 1174 | ret = load_segment(lcp, |
0a7de745 A |
1175 | header->filetype, |
1176 | control, | |
1177 | file_offset, | |
1178 | macho_size, | |
1179 | vp, | |
1180 | map, | |
1181 | slide, | |
f427ee49 A |
1182 | result, |
1183 | imgp); | |
39037602 A |
1184 | |
1185 | if (ret == LOAD_SUCCESS && scp64->fileoff == 0 && scp64->filesize > 0) { | |
1186 | /* Enforce a single segment mapping offset zero, with R+X | |
1187 | * protection. */ | |
1188 | if (found_header_segment || | |
0a7de745 | 1189 | ((scp64->initprot & (VM_PROT_READ | VM_PROT_EXECUTE)) != (VM_PROT_READ | VM_PROT_EXECUTE))) { |
39037602 A |
1190 | ret = LOAD_BADMACHO; |
1191 | break; | |
1192 | } | |
1193 | found_header_segment = TRUE; | |
1194 | } | |
1195 | ||
1c79356b | 1196 | break; |
39037602 | 1197 | } |
f427ee49 A |
1198 | case LC_UNIXTHREAD: { |
1199 | boolean_t is_x86_64_compat_binary = FALSE; | |
0a7de745 | 1200 | if (pass != 1) { |
1c79356b | 1201 | break; |
0a7de745 | 1202 | } |
1c79356b | 1203 | ret = load_unixthread( |
0a7de745 A |
1204 | (struct thread_command *) lcp, |
1205 | thread, | |
1206 | slide, | |
f427ee49 | 1207 | is_x86_64_compat_binary, |
0a7de745 | 1208 | result); |
1c79356b | 1209 | break; |
f427ee49 | 1210 | } |
316670eb | 1211 | case LC_MAIN: |
0a7de745 | 1212 | if (pass != 1) { |
316670eb | 1213 | break; |
0a7de745 A |
1214 | } |
1215 | if (depth != 1) { | |
316670eb | 1216 | break; |
0a7de745 | 1217 | } |
316670eb | 1218 | ret = load_main( |
0a7de745 A |
1219 | (struct entry_point_command *) lcp, |
1220 | thread, | |
1221 | slide, | |
1222 | result); | |
316670eb | 1223 | break; |
1c79356b | 1224 | case LC_LOAD_DYLINKER: |
0a7de745 | 1225 | if (pass != 3) { |
1c79356b | 1226 | break; |
0a7de745 | 1227 | } |
91447636 | 1228 | if ((depth == 1) && (dlp == 0)) { |
1c79356b | 1229 | dlp = (struct dylinker_command *)lcp; |
91447636 | 1230 | } else { |
1c79356b | 1231 | ret = LOAD_FAILURE; |
91447636 | 1232 | } |
1c79356b | 1233 | break; |
b0d623f7 | 1234 | case LC_UUID: |
6d2010ae | 1235 | if (pass == 1 && depth == 1) { |
39236c6e | 1236 | ret = load_uuid((struct uuid_command *) lcp, |
0a7de745 A |
1237 | (char *)addr + cmds_size, |
1238 | result); | |
b0d623f7 A |
1239 | } |
1240 | break; | |
2d21ac55 A |
1241 | case LC_CODE_SIGNATURE: |
1242 | /* CODE SIGNING */ | |
0a7de745 | 1243 | if (pass != 1) { |
2d21ac55 | 1244 | break; |
0a7de745 | 1245 | } |
f427ee49 | 1246 | |
2d21ac55 | 1247 | /* pager -> uip -> |
0a7de745 A |
1248 | * load signatures & store in uip |
1249 | * set VM object "signed_pages" | |
1250 | */ | |
2d21ac55 A |
1251 | ret = load_code_signature( |
1252 | (struct linkedit_data_command *) lcp, | |
1253 | vp, | |
1254 | file_offset, | |
1255 | macho_size, | |
1256 | header->cputype, | |
f427ee49 | 1257 | header->cpusubtype, |
39037602 A |
1258 | result, |
1259 | imgp); | |
2d21ac55 A |
1260 | if (ret != LOAD_SUCCESS) { |
1261 | printf("proc %d: load code signature error %d " | |
0a7de745 A |
1262 | "for file \"%s\"\n", |
1263 | p->p_pid, ret, vp->v_name); | |
c18c124e A |
1264 | /* |
1265 | * Allow injections to be ignored on devices w/o enforcement enabled | |
1266 | */ | |
0a7de745 A |
1267 | if (!cs_process_global_enforcement()) { |
1268 | ret = LOAD_SUCCESS; /* ignore error */ | |
1269 | } | |
2d21ac55 A |
1270 | } else { |
1271 | got_code_signatures = TRUE; | |
1272 | } | |
04b8595b A |
1273 | |
1274 | if (got_code_signatures) { | |
c18c124e A |
1275 | unsigned tainted = CS_VALIDATE_TAINTED; |
1276 | boolean_t valid = FALSE; | |
04b8595b A |
1277 | vm_size_t off = 0; |
1278 | ||
1279 | ||
0a7de745 | 1280 | if (cs_debug > 10) { |
04b8595b | 1281 | printf("validating initial pages of %s\n", vp->v_name); |
0a7de745 A |
1282 | } |
1283 | ||
5ba3f43e | 1284 | while (off < alloc_size && ret == LOAD_SUCCESS) { |
0a7de745 A |
1285 | tainted = CS_VALIDATE_TAINTED; |
1286 | ||
1287 | valid = cs_validate_range(vp, | |
1288 | NULL, | |
1289 | file_offset + off, | |
1290 | addr + off, | |
f427ee49 | 1291 | MIN(PAGE_SIZE, cmds_size), |
0a7de745 A |
1292 | &tainted); |
1293 | if (!valid || (tainted & CS_VALIDATE_TAINTED)) { | |
1294 | if (cs_debug) { | |
1295 | printf("CODE SIGNING: %s[%d]: invalid initial page at offset %lld validated:%d tainted:%d csflags:0x%x\n", | |
04b8595b | 1296 | vp->v_name, p->p_pid, (long long)(file_offset + off), valid, tainted, result->csflags); |
0a7de745 A |
1297 | } |
1298 | if (cs_process_global_enforcement() || | |
1299 | (result->csflags & (CS_HARD | CS_KILL | CS_ENFORCEMENT))) { | |
1300 | ret = LOAD_FAILURE; | |
1301 | } | |
1302 | result->csflags &= ~CS_VALID; | |
1303 | } | |
1304 | off += PAGE_SIZE; | |
04b8595b A |
1305 | } |
1306 | } | |
1307 | ||
2d21ac55 | 1308 | break; |
593a1d5f A |
1309 | #if CONFIG_CODE_DECRYPTION |
1310 | case LC_ENCRYPTION_INFO: | |
39236c6e | 1311 | case LC_ENCRYPTION_INFO_64: |
0a7de745 | 1312 | if (pass != 3) { |
593a1d5f | 1313 | break; |
0a7de745 | 1314 | } |
593a1d5f A |
1315 | ret = set_code_unprotect( |
1316 | (struct encryption_info_command *) lcp, | |
3e170ce0 | 1317 | addr, map, slide, vp, file_offset, |
39236c6e | 1318 | header->cputype, header->cpusubtype); |
593a1d5f | 1319 | if (ret != LOAD_SUCCESS) { |
39037602 | 1320 | os_reason_t load_failure_reason = OS_REASON_NULL; |
c910b4d9 | 1321 | printf("proc %d: set_code_unprotect() error %d " |
0a7de745 A |
1322 | "for file \"%s\"\n", |
1323 | p->p_pid, ret, vp->v_name); | |
1324 | /* | |
1325 | * Don't let the app run if it's | |
c910b4d9 | 1326 | * encrypted but we failed to set up the |
39236c6e A |
1327 | * decrypter. If the keys are missing it will |
1328 | * return LOAD_DECRYPTFAIL. | |
1329 | */ | |
0a7de745 | 1330 | if (ret == LOAD_DECRYPTFAIL) { |
39236c6e A |
1331 | /* failed to load due to missing FP keys */ |
1332 | proc_lock(p); | |
1333 | p->p_lflag |= P_LTERM_DECRYPTFAIL; | |
1334 | proc_unlock(p); | |
39037602 A |
1335 | |
1336 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXITREASON_CREATE) | DBG_FUNC_NONE, | |
0a7de745 | 1337 | p->p_pid, OS_REASON_EXEC, EXEC_EXIT_REASON_FAIRPLAY_DECRYPT, 0, 0); |
39037602 | 1338 | load_failure_reason = os_reason_create(OS_REASON_EXEC, EXEC_EXIT_REASON_FAIRPLAY_DECRYPT); |
0a7de745 | 1339 | } else { |
39037602 | 1340 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXITREASON_CREATE) | DBG_FUNC_NONE, |
0a7de745 | 1341 | p->p_pid, OS_REASON_EXEC, EXEC_EXIT_REASON_DECRYPT, 0, 0); |
39037602 | 1342 | load_failure_reason = os_reason_create(OS_REASON_EXEC, EXEC_EXIT_REASON_DECRYPT); |
0a7de745 | 1343 | } |
39037602 | 1344 | |
cb323159 A |
1345 | /* |
1346 | * Don't signal the process if it was forked and in a partially constructed | |
1347 | * state as part of a spawn -- it will just be torn down when the exec fails. | |
1348 | */ | |
1349 | if (!spawn) { | |
1350 | assert(load_failure_reason != OS_REASON_NULL); | |
1351 | if (vfexec) { | |
1352 | psignal_vfork_with_reason(p, get_threadtask(imgp->ip_new_thread), imgp->ip_new_thread, SIGKILL, load_failure_reason); | |
1353 | load_failure_reason = OS_REASON_NULL; | |
1354 | } else { | |
1355 | psignal_with_reason(p, SIGKILL, load_failure_reason); | |
1356 | load_failure_reason = OS_REASON_NULL; | |
1357 | } | |
1358 | } else { | |
1359 | os_reason_free(load_failure_reason); | |
1360 | load_failure_reason = OS_REASON_NULL; | |
1361 | } | |
593a1d5f A |
1362 | } |
1363 | break; | |
1364 | #endif | |
cb323159 A |
1365 | case LC_VERSION_MIN_IPHONEOS: |
1366 | case LC_VERSION_MIN_MACOSX: | |
1367 | case LC_VERSION_MIN_WATCHOS: | |
1368 | case LC_VERSION_MIN_TVOS: { | |
d9a64523 A |
1369 | struct version_min_command *vmc; |
1370 | ||
f427ee49 | 1371 | if (depth != 1 || pass != 0) { |
d9a64523 A |
1372 | break; |
1373 | } | |
1374 | vmc = (struct version_min_command *) lcp; | |
f427ee49 | 1375 | ret = load_version(vmc, &found_version_cmd, imgp->ip_flags, result); |
c3c9b80d A |
1376 | #if XNU_TARGET_OS_OSX |
1377 | if (ret == LOAD_SUCCESS) { | |
1378 | if (result->ip_platform == PLATFORM_IOS) { | |
1379 | vm_map_mark_alien(map); | |
1380 | } else { | |
1381 | assert(!vm_map_is_alien(map)); | |
1382 | } | |
1383 | } | |
1384 | #endif /* XNU_TARGET_OS_OSX */ | |
cb323159 A |
1385 | break; |
1386 | } | |
1387 | case LC_BUILD_VERSION: { | |
f427ee49 | 1388 | if (depth != 1 || pass != 0) { |
cb323159 | 1389 | break; |
d9a64523 | 1390 | } |
cb323159 A |
1391 | struct build_version_command* bvc = (struct build_version_command*)lcp; |
1392 | if (bvc->cmdsize < sizeof(*bvc)) { | |
1393 | ret = LOAD_BADMACHO; | |
1394 | break; | |
1395 | } | |
1396 | if (found_version_cmd == TRUE) { | |
1397 | ret = LOAD_BADMACHO; | |
1398 | break; | |
1399 | } | |
1400 | result->ip_platform = bvc->platform; | |
f427ee49 | 1401 | result->lr_sdk = bvc->sdk; |
c3c9b80d | 1402 | result->lr_min_sdk = bvc->minos; |
cb323159 | 1403 | found_version_cmd = TRUE; |
c3c9b80d A |
1404 | #if XNU_TARGET_OS_OSX |
1405 | if (result->ip_platform == PLATFORM_IOS) { | |
1406 | vm_map_mark_alien(map); | |
1407 | } else { | |
1408 | assert(!vm_map_is_alien(map)); | |
1409 | } | |
1410 | #endif /* XNU_TARGET_OS_OSX */ | |
d9a64523 A |
1411 | break; |
1412 | } | |
1c79356b | 1413 | default: |
a3d08fcd A |
1414 | /* Other commands are ignored by the kernel */ |
1415 | ret = LOAD_SUCCESS; | |
91447636 | 1416 | break; |
1c79356b | 1417 | } |
0a7de745 | 1418 | if (ret != LOAD_SUCCESS) { |
1c79356b | 1419 | break; |
0a7de745 | 1420 | } |
1c79356b | 1421 | } |
0a7de745 | 1422 | if (ret != LOAD_SUCCESS) { |
1c79356b | 1423 | break; |
0a7de745 | 1424 | } |
1c79356b | 1425 | } |
fe8ab488 | 1426 | |
527f9951 | 1427 | if (ret == LOAD_SUCCESS) { |
0a7de745 | 1428 | if (!got_code_signatures && cs_process_global_enforcement()) { |
527f9951 | 1429 | ret = LOAD_FAILURE; |
c18c124e | 1430 | } |
91447636 | 1431 | |
316670eb | 1432 | /* Make sure if we need dyld, we got it */ |
3e170ce0 | 1433 | if (result->needs_dynlinker && !dlp) { |
316670eb A |
1434 | ret = LOAD_FAILURE; |
1435 | } | |
3e170ce0 | 1436 | |
c18c124e | 1437 | if ((ret == LOAD_SUCCESS) && (dlp != 0)) { |
fe8ab488 | 1438 | /* |
c18c124e A |
1439 | * load the dylinker, and slide it by the independent DYLD ASLR |
1440 | * offset regardless of the PIE-ness of the main binary. | |
1441 | */ | |
f427ee49 | 1442 | ret = load_dylinker(dlp, header->cputype, map, thread, depth, |
0a7de745 | 1443 | dyld_aslr_offset, result, imgp); |
fe8ab488 | 1444 | } |
39037602 | 1445 | |
f427ee49 | 1446 | |
39037602 | 1447 | if ((ret == LOAD_SUCCESS) && (depth == 1)) { |
316670eb A |
1448 | if (result->thread_count == 0) { |
1449 | ret = LOAD_FAILURE; | |
1450 | } | |
d9a64523 | 1451 | #if CONFIG_ENFORCE_SIGNED_CODE |
5ba3f43e A |
1452 | if (result->needs_dynlinker && !(result->csflags & CS_DYLD_PLATFORM)) { |
1453 | ret = LOAD_FAILURE; | |
1454 | } | |
1455 | #endif | |
0a7de745 | 1456 | } |
1c79356b A |
1457 | } |
1458 | ||
39037602 A |
1459 | if (ret == LOAD_BADMACHO && found_xhdr) { |
1460 | ret = LOAD_BADMACHO_UPX; | |
1461 | } | |
1462 | ||
5ba3f43e | 1463 | kfree(addr, alloc_size); |
0b4e3aa0 | 1464 | |
5ba3f43e | 1465 | return ret; |
1c79356b A |
1466 | } |
1467 | ||
cb323159 A |
1468 | load_return_t |
1469 | validate_potential_simulator_binary( | |
1470 | cpu_type_t exectype __unused, | |
1471 | struct image_params *imgp __unused, | |
1472 | off_t file_offset __unused, | |
1473 | off_t macho_size __unused) | |
1474 | { | |
1475 | #if __x86_64__ | |
1476 | /* Allow 32 bit exec only for simulator binaries */ | |
1477 | if (bootarg_no32exec && imgp != NULL && exectype == CPU_TYPE_X86) { | |
1478 | if (imgp->ip_simulator_binary == IMGPF_SB_DEFAULT) { | |
1479 | boolean_t simulator_binary = check_if_simulator_binary(imgp, file_offset, macho_size); | |
1480 | imgp->ip_simulator_binary = simulator_binary ? IMGPF_SB_TRUE : IMGPF_SB_FALSE; | |
1481 | } | |
1482 | ||
1483 | if (imgp->ip_simulator_binary != IMGPF_SB_TRUE) { | |
1484 | return LOAD_BADARCH; | |
1485 | } | |
1486 | } | |
1487 | #endif | |
1488 | return LOAD_SUCCESS; | |
1489 | } | |
1490 | ||
1491 | #if __x86_64__ | |
1492 | static boolean_t | |
1493 | check_if_simulator_binary( | |
1494 | struct image_params *imgp, | |
1495 | off_t file_offset, | |
1496 | off_t macho_size) | |
1497 | { | |
1498 | struct mach_header *header; | |
1499 | char *ip_vdata = NULL; | |
1500 | kauth_cred_t cred = NULL; | |
1501 | uint32_t ncmds; | |
1502 | struct load_command *lcp; | |
1503 | boolean_t simulator_binary = FALSE; | |
1504 | void * addr = NULL; | |
1505 | vm_size_t alloc_size, cmds_size; | |
1506 | size_t offset; | |
1507 | proc_t p = current_proc(); /* XXXX */ | |
1508 | int error; | |
1509 | int resid = 0; | |
1510 | size_t mach_header_sz = sizeof(struct mach_header); | |
1511 | ||
1512 | ||
1513 | cred = kauth_cred_proc_ref(p); | |
1514 | ||
1515 | /* Allocate page to copyin mach header */ | |
1516 | ip_vdata = kalloc(PAGE_SIZE); | |
f427ee49 | 1517 | bzero(ip_vdata, PAGE_SIZE); |
cb323159 A |
1518 | if (ip_vdata == NULL) { |
1519 | goto bad; | |
1520 | } | |
1521 | ||
1522 | /* Read the Mach-O header */ | |
1523 | error = vn_rdwr(UIO_READ, imgp->ip_vp, ip_vdata, | |
1524 | PAGE_SIZE, file_offset, | |
1525 | UIO_SYSSPACE, (IO_UNIT | IO_NODELOCKED), | |
1526 | cred, &resid, p); | |
1527 | if (error) { | |
1528 | goto bad; | |
1529 | } | |
1530 | ||
1531 | header = (struct mach_header *)ip_vdata; | |
1532 | ||
1533 | if (header->magic == MH_MAGIC_64 || | |
1534 | header->magic == MH_CIGAM_64) { | |
1535 | mach_header_sz = sizeof(struct mach_header_64); | |
1536 | } | |
1537 | ||
1538 | /* ensure header + sizeofcmds falls within the file */ | |
1539 | if (os_add_overflow(mach_header_sz, header->sizeofcmds, &cmds_size) || | |
1540 | (off_t)cmds_size > macho_size || | |
f427ee49 A |
1541 | round_page_overflow(cmds_size, &alloc_size) || |
1542 | alloc_size > INT_MAX) { | |
cb323159 A |
1543 | goto bad; |
1544 | } | |
1545 | ||
1546 | /* | |
1547 | * Map the load commands into kernel memory. | |
1548 | */ | |
1549 | addr = kalloc(alloc_size); | |
1550 | if (addr == NULL) { | |
1551 | goto bad; | |
1552 | } | |
1553 | ||
f427ee49 | 1554 | error = vn_rdwr(UIO_READ, imgp->ip_vp, addr, (int)alloc_size, file_offset, |
cb323159 A |
1555 | UIO_SYSSPACE, IO_NODELOCKED, cred, &resid, p); |
1556 | if (error) { | |
1557 | goto bad; | |
1558 | } | |
1559 | ||
1560 | if (resid) { | |
1561 | /* We must be able to read in as much as the mach_header indicated */ | |
1562 | goto bad; | |
1563 | } | |
1564 | ||
1565 | /* | |
1566 | * Loop through each of the load_commands indicated by the | |
1567 | * Mach-O header; if an absurd value is provided, we just | |
1568 | * run off the end of the reserved section by incrementing | |
1569 | * the offset too far, so we are implicitly fail-safe. | |
1570 | */ | |
1571 | offset = mach_header_sz; | |
1572 | ncmds = header->ncmds; | |
1573 | ||
1574 | while (ncmds--) { | |
1575 | /* ensure enough space for a minimal load command */ | |
1576 | if (offset + sizeof(struct load_command) > cmds_size) { | |
1577 | break; | |
1578 | } | |
1579 | ||
1580 | /* | |
1581 | * Get a pointer to the command. | |
1582 | */ | |
1583 | lcp = (struct load_command *)(addr + offset); | |
1584 | ||
1585 | /* | |
1586 | * Perform prevalidation of the struct load_command | |
1587 | * before we attempt to use its contents. Invalid | |
1588 | * values are ones which result in an overflow, or | |
1589 | * which can not possibly be valid commands, or which | |
1590 | * straddle or exist past the reserved section at the | |
1591 | * start of the image. | |
1592 | */ | |
1593 | if (os_add_overflow(offset, lcp->cmdsize, &offset) || | |
1594 | lcp->cmdsize < sizeof(struct load_command) || | |
1595 | offset > cmds_size) { | |
1596 | break; | |
1597 | } | |
1598 | ||
1599 | /* Check if its a simulator binary. */ | |
1600 | switch (lcp->cmd) { | |
1601 | case LC_VERSION_MIN_WATCHOS: | |
1602 | simulator_binary = TRUE; | |
1603 | break; | |
1604 | ||
1605 | case LC_BUILD_VERSION: { | |
1606 | struct build_version_command *bvc; | |
1607 | ||
1608 | bvc = (struct build_version_command *) lcp; | |
1609 | if (bvc->cmdsize < sizeof(*bvc)) { | |
1610 | /* unsafe to use this command struct if cmdsize | |
1611 | * validated above is too small for it to fit */ | |
1612 | break; | |
1613 | } | |
1614 | if (bvc->platform == PLATFORM_IOSSIMULATOR || | |
1615 | bvc->platform == PLATFORM_WATCHOSSIMULATOR) { | |
1616 | simulator_binary = TRUE; | |
1617 | } | |
1618 | ||
1619 | break; | |
1620 | } | |
1621 | ||
1622 | case LC_VERSION_MIN_IPHONEOS: { | |
1623 | simulator_binary = TRUE; | |
1624 | break; | |
1625 | } | |
1626 | ||
1627 | default: | |
1628 | /* ignore other load commands */ | |
1629 | break; | |
1630 | } | |
1631 | ||
1632 | if (simulator_binary == TRUE) { | |
1633 | break; | |
1634 | } | |
1635 | } | |
1636 | ||
1637 | bad: | |
1638 | if (ip_vdata) { | |
1639 | kfree(ip_vdata, PAGE_SIZE); | |
1640 | } | |
1641 | ||
1642 | if (cred) { | |
1643 | kauth_cred_unref(&cred); | |
1644 | } | |
1645 | ||
1646 | if (addr) { | |
1647 | kfree(addr, alloc_size); | |
1648 | } | |
1649 | ||
1650 | return simulator_binary; | |
1651 | } | |
1652 | #endif /* __x86_64__ */ | |
1653 | ||
593a1d5f | 1654 | #if CONFIG_CODE_DECRYPTION |
0c530ab8 | 1655 | |
0a7de745 | 1656 | #define APPLE_UNPROTECTED_HEADER_SIZE (3 * 4096) |
0c530ab8 A |
1657 | |
1658 | static load_return_t | |
fe8ab488 | 1659 | unprotect_dsmos_segment( |
0a7de745 A |
1660 | uint64_t file_off, |
1661 | uint64_t file_size, | |
1662 | struct vnode *vp, | |
1663 | off_t macho_offset, | |
1664 | vm_map_t map, | |
1665 | vm_map_offset_t map_addr, | |
1666 | vm_map_size_t map_size) | |
0c530ab8 | 1667 | { |
0a7de745 | 1668 | kern_return_t kr; |
f427ee49 | 1669 | uint64_t slice_off; |
0c530ab8 A |
1670 | |
1671 | /* | |
1672 | * The first APPLE_UNPROTECTED_HEADER_SIZE bytes (from offset 0 of | |
1673 | * this part of a Universal binary) are not protected... | |
1674 | * The rest needs to be "transformed". | |
1675 | */ | |
f427ee49 A |
1676 | slice_off = file_off - macho_offset; |
1677 | if (slice_off <= APPLE_UNPROTECTED_HEADER_SIZE && | |
1678 | slice_off + file_size <= APPLE_UNPROTECTED_HEADER_SIZE) { | |
0c530ab8 A |
1679 | /* it's all unprotected, nothing to do... */ |
1680 | kr = KERN_SUCCESS; | |
1681 | } else { | |
f427ee49 | 1682 | if (slice_off <= APPLE_UNPROTECTED_HEADER_SIZE) { |
0c530ab8 A |
1683 | /* |
1684 | * We start mapping in the unprotected area. | |
1685 | * Skip the unprotected part... | |
1686 | */ | |
f427ee49 A |
1687 | uint64_t delta_file; |
1688 | vm_map_offset_t delta_map; | |
0c530ab8 | 1689 | |
f427ee49 A |
1690 | delta_file = (uint64_t)APPLE_UNPROTECTED_HEADER_SIZE; |
1691 | delta_file -= slice_off; | |
1692 | if (os_convert_overflow(delta_file, &delta_map)) { | |
1693 | return LOAD_BADMACHO; | |
1694 | } | |
1695 | if (os_add_overflow(map_addr, delta_map, &map_addr)) { | |
1696 | return LOAD_BADMACHO; | |
1697 | } | |
1698 | if (os_sub_overflow(map_size, delta_map, &map_size)) { | |
1699 | return LOAD_BADMACHO; | |
1700 | } | |
0c530ab8 A |
1701 | } |
1702 | /* ... transform the rest of the mapping. */ | |
593a1d5f A |
1703 | struct pager_crypt_info crypt_info; |
1704 | crypt_info.page_decrypt = dsmos_page_transform; | |
1705 | crypt_info.crypt_ops = NULL; | |
1706 | crypt_info.crypt_end = NULL; | |
b0d623f7 A |
1707 | #pragma unused(vp, macho_offset) |
1708 | crypt_info.crypt_ops = (void *)0x2e69cf40; | |
3e170ce0 A |
1709 | vm_map_offset_t crypto_backing_offset; |
1710 | crypto_backing_offset = -1; /* i.e. use map entry's offset */ | |
39037602 A |
1711 | #if VM_MAP_DEBUG_APPLE_PROTECT |
1712 | if (vm_map_debug_apple_protect) { | |
1713 | struct proc *p; | |
1714 | p = current_proc(); | |
1715 | printf("APPLE_PROTECT: %d[%s] map %p " | |
0a7de745 A |
1716 | "[0x%llx:0x%llx] %s(%s)\n", |
1717 | p->p_pid, p->p_comm, map, | |
1718 | (uint64_t) map_addr, | |
1719 | (uint64_t) (map_addr + map_size), | |
1720 | __FUNCTION__, vp->v_name); | |
39037602 A |
1721 | } |
1722 | #endif /* VM_MAP_DEBUG_APPLE_PROTECT */ | |
7e41aa88 A |
1723 | |
1724 | /* The DSMOS pager can only be used by apple signed code */ | |
1725 | struct cs_blob * blob = csvnode_get_blob(vp, file_off); | |
0a7de745 | 1726 | if (blob == NULL || !blob->csb_platform_binary || blob->csb_platform_path) { |
7e41aa88 A |
1727 | return LOAD_FAILURE; |
1728 | } | |
1729 | ||
0c530ab8 | 1730 | kr = vm_map_apple_protected(map, |
0a7de745 A |
1731 | map_addr, |
1732 | map_addr + map_size, | |
1733 | crypto_backing_offset, | |
f427ee49 A |
1734 | &crypt_info, |
1735 | CRYPTID_APP_ENCRYPTION); | |
0c530ab8 A |
1736 | } |
1737 | ||
1738 | if (kr != KERN_SUCCESS) { | |
1739 | return LOAD_FAILURE; | |
1740 | } | |
1741 | return LOAD_SUCCESS; | |
1742 | } | |
0a7de745 | 1743 | #else /* CONFIG_CODE_DECRYPTION */ |
b0d623f7 | 1744 | static load_return_t |
fe8ab488 | 1745 | unprotect_dsmos_segment( |
0a7de745 A |
1746 | __unused uint64_t file_off, |
1747 | __unused uint64_t file_size, | |
1748 | __unused struct vnode *vp, | |
1749 | __unused off_t macho_offset, | |
1750 | __unused vm_map_t map, | |
1751 | __unused vm_map_offset_t map_addr, | |
1752 | __unused vm_map_size_t map_size) | |
1c79356b | 1753 | { |
b0d623f7 | 1754 | return LOAD_SUCCESS; |
1c79356b | 1755 | } |
0a7de745 | 1756 | #endif /* CONFIG_CODE_DECRYPTION */ |
1c79356b | 1757 | |
3e170ce0 A |
1758 | |
1759 | /* | |
1760 | * map_segment: | |
1761 | * Maps a Mach-O segment, taking care of mis-alignment (wrt the system | |
1762 | * page size) issues. | |
0a7de745 | 1763 | * |
3e170ce0 | 1764 | * The mapping might result in 1, 2 or 3 map entries: |
0a7de745 A |
1765 | * 1. for the first page, which could be overlap with the previous |
1766 | * mapping, | |
1767 | * 2. for the center (if applicable), | |
1768 | * 3. for the last page, which could overlap with the next mapping. | |
3e170ce0 A |
1769 | * |
1770 | * For each of those map entries, we might have to interpose a | |
1771 | * "fourk_pager" to deal with mis-alignment wrt the system page size, | |
1772 | * either in the mapping address and/or size or the file offset and/or | |
1773 | * size. | |
1774 | * The "fourk_pager" itself would be mapped with proper alignment | |
1775 | * wrt the system page size and would then be populated with the | |
1776 | * information about the intended mapping, with a "4KB" granularity. | |
1777 | */ | |
1778 | static kern_return_t | |
1779 | map_segment( | |
0a7de745 A |
1780 | vm_map_t map, |
1781 | vm_map_offset_t vm_start, | |
1782 | vm_map_offset_t vm_end, | |
1783 | memory_object_control_t control, | |
1784 | vm_map_offset_t file_start, | |
1785 | vm_map_offset_t file_end, | |
1786 | vm_prot_t initprot, | |
1787 | vm_prot_t maxprot, | |
1788 | load_result_t *result) | |
3e170ce0 | 1789 | { |
0a7de745 A |
1790 | vm_map_offset_t cur_offset, cur_start, cur_end; |
1791 | kern_return_t ret; | |
1792 | vm_map_offset_t effective_page_mask; | |
5ba3f43e | 1793 | vm_map_kernel_flags_t vmk_flags, cur_vmk_flags; |
0a7de745 | 1794 | |
3e170ce0 A |
1795 | if (vm_end < vm_start || |
1796 | file_end < file_start) { | |
1797 | return LOAD_BADMACHO; | |
1798 | } | |
1799 | if (vm_end == vm_start || | |
1800 | file_end == file_start) { | |
1801 | /* nothing to map... */ | |
1802 | return LOAD_SUCCESS; | |
1803 | } | |
1804 | ||
f427ee49 | 1805 | effective_page_mask = vm_map_page_mask(map); |
3e170ce0 | 1806 | |
5ba3f43e | 1807 | vmk_flags = VM_MAP_KERNEL_FLAGS_NONE; |
3e170ce0 A |
1808 | if (vm_map_page_aligned(vm_start, effective_page_mask) && |
1809 | vm_map_page_aligned(vm_end, effective_page_mask) && | |
1810 | vm_map_page_aligned(file_start, effective_page_mask) && | |
1811 | vm_map_page_aligned(file_end, effective_page_mask)) { | |
1812 | /* all page-aligned and map-aligned: proceed */ | |
1813 | } else { | |
5ba3f43e A |
1814 | #if __arm64__ |
1815 | /* use an intermediate "4K" pager */ | |
1816 | vmk_flags.vmkf_fourk = TRUE; | |
1817 | #else /* __arm64__ */ | |
3e170ce0 | 1818 | panic("map_segment: unexpected mis-alignment " |
0a7de745 A |
1819 | "vm[0x%llx:0x%llx] file[0x%llx:0x%llx]\n", |
1820 | (uint64_t) vm_start, | |
1821 | (uint64_t) vm_end, | |
1822 | (uint64_t) file_start, | |
1823 | (uint64_t) file_end); | |
5ba3f43e | 1824 | #endif /* __arm64__ */ |
3e170ce0 A |
1825 | } |
1826 | ||
1827 | cur_offset = 0; | |
1828 | cur_start = vm_start; | |
1829 | cur_end = vm_start; | |
5ba3f43e A |
1830 | #if __arm64__ |
1831 | if (!vm_map_page_aligned(vm_start, effective_page_mask)) { | |
1832 | /* one 4K pager for the 1st page */ | |
1833 | cur_end = vm_map_round_page(cur_start, effective_page_mask); | |
1834 | if (cur_end > vm_end) { | |
1835 | cur_end = vm_start + (file_end - file_start); | |
1836 | } | |
1837 | if (control != MEMORY_OBJECT_CONTROL_NULL) { | |
cb323159 A |
1838 | /* no copy-on-read for mapped binaries */ |
1839 | vmk_flags.vmkf_no_copy_on_read = 1; | |
5ba3f43e A |
1840 | ret = vm_map_enter_mem_object_control( |
1841 | map, | |
1842 | &cur_start, | |
1843 | cur_end - cur_start, | |
1844 | (mach_vm_offset_t)0, | |
1845 | VM_FLAGS_FIXED, | |
1846 | vmk_flags, | |
1847 | VM_KERN_MEMORY_NONE, | |
1848 | control, | |
1849 | file_start + cur_offset, | |
1850 | TRUE, /* copy */ | |
1851 | initprot, maxprot, | |
1852 | VM_INHERIT_DEFAULT); | |
1853 | } else { | |
1854 | ret = vm_map_enter_mem_object( | |
1855 | map, | |
1856 | &cur_start, | |
1857 | cur_end - cur_start, | |
1858 | (mach_vm_offset_t)0, | |
1859 | VM_FLAGS_FIXED, | |
1860 | vmk_flags, | |
1861 | VM_KERN_MEMORY_NONE, | |
1862 | IPC_PORT_NULL, | |
1863 | 0, /* offset */ | |
1864 | TRUE, /* copy */ | |
1865 | initprot, maxprot, | |
1866 | VM_INHERIT_DEFAULT); | |
1867 | } | |
1868 | if (ret != KERN_SUCCESS) { | |
0a7de745 | 1869 | return LOAD_NOSPACE; |
5ba3f43e A |
1870 | } |
1871 | cur_offset += cur_end - cur_start; | |
1872 | } | |
1873 | #endif /* __arm64__ */ | |
3e170ce0 A |
1874 | if (cur_end >= vm_start + (file_end - file_start)) { |
1875 | /* all mapped: done */ | |
1876 | goto done; | |
1877 | } | |
1878 | if (vm_map_round_page(cur_end, effective_page_mask) >= | |
1879 | vm_map_trunc_page(vm_start + (file_end - file_start), | |
0a7de745 | 1880 | effective_page_mask)) { |
3e170ce0 A |
1881 | /* no middle */ |
1882 | } else { | |
1883 | cur_start = cur_end; | |
1884 | if ((vm_start & effective_page_mask) != | |
1885 | (file_start & effective_page_mask)) { | |
1886 | /* one 4K pager for the middle */ | |
5ba3f43e | 1887 | cur_vmk_flags = vmk_flags; |
3e170ce0 A |
1888 | } else { |
1889 | /* regular mapping for the middle */ | |
5ba3f43e | 1890 | cur_vmk_flags = VM_MAP_KERNEL_FLAGS_NONE; |
3e170ce0 | 1891 | } |
d9a64523 | 1892 | |
f427ee49 | 1893 | #if !defined(XNU_TARGET_OS_OSX) |
d9a64523 | 1894 | (void) result; |
f427ee49 | 1895 | #else /* !defined(XNU_TARGET_OS_OSX) */ |
d9a64523 A |
1896 | /* |
1897 | * This process doesn't have its new csflags (from | |
1898 | * the image being loaded) yet, so tell VM to override the | |
1899 | * current process's CS_ENFORCEMENT for this mapping. | |
1900 | */ | |
1901 | if (result->csflags & CS_ENFORCEMENT) { | |
1902 | cur_vmk_flags.vmkf_cs_enforcement = TRUE; | |
1903 | } else { | |
1904 | cur_vmk_flags.vmkf_cs_enforcement = FALSE; | |
1905 | } | |
1906 | cur_vmk_flags.vmkf_cs_enforcement_override = TRUE; | |
f427ee49 A |
1907 | #endif /* !defined(XNU_TARGET_OS_OSX) */ |
1908 | ||
1909 | if (result->is_cambria && (initprot & VM_PROT_EXECUTE) == VM_PROT_EXECUTE) { | |
1910 | cur_vmk_flags.vmkf_translated_allow_execute = TRUE; | |
1911 | } | |
d9a64523 | 1912 | |
3e170ce0 | 1913 | cur_end = vm_map_trunc_page(vm_start + (file_end - |
0a7de745 A |
1914 | file_start), |
1915 | effective_page_mask); | |
3e170ce0 | 1916 | if (control != MEMORY_OBJECT_CONTROL_NULL) { |
cb323159 A |
1917 | /* no copy-on-read for mapped binaries */ |
1918 | cur_vmk_flags.vmkf_no_copy_on_read = 1; | |
3e170ce0 A |
1919 | ret = vm_map_enter_mem_object_control( |
1920 | map, | |
1921 | &cur_start, | |
1922 | cur_end - cur_start, | |
1923 | (mach_vm_offset_t)0, | |
5ba3f43e A |
1924 | VM_FLAGS_FIXED, |
1925 | cur_vmk_flags, | |
1926 | VM_KERN_MEMORY_NONE, | |
3e170ce0 A |
1927 | control, |
1928 | file_start + cur_offset, | |
1929 | TRUE, /* copy */ | |
1930 | initprot, maxprot, | |
1931 | VM_INHERIT_DEFAULT); | |
1932 | } else { | |
1933 | ret = vm_map_enter_mem_object( | |
1934 | map, | |
1935 | &cur_start, | |
1936 | cur_end - cur_start, | |
1937 | (mach_vm_offset_t)0, | |
5ba3f43e A |
1938 | VM_FLAGS_FIXED, |
1939 | cur_vmk_flags, | |
1940 | VM_KERN_MEMORY_NONE, | |
3e170ce0 A |
1941 | IPC_PORT_NULL, |
1942 | 0, /* offset */ | |
1943 | TRUE, /* copy */ | |
1944 | initprot, maxprot, | |
1945 | VM_INHERIT_DEFAULT); | |
1946 | } | |
1947 | if (ret != KERN_SUCCESS) { | |
0a7de745 | 1948 | return LOAD_NOSPACE; |
3e170ce0 A |
1949 | } |
1950 | cur_offset += cur_end - cur_start; | |
1951 | } | |
1952 | if (cur_end >= vm_start + (file_end - file_start)) { | |
1953 | /* all mapped: done */ | |
1954 | goto done; | |
1955 | } | |
1956 | cur_start = cur_end; | |
5ba3f43e A |
1957 | #if __arm64__ |
1958 | if (!vm_map_page_aligned(vm_start + (file_end - file_start), | |
0a7de745 | 1959 | effective_page_mask)) { |
5ba3f43e A |
1960 | /* one 4K pager for the last page */ |
1961 | cur_end = vm_start + (file_end - file_start); | |
1962 | if (control != MEMORY_OBJECT_CONTROL_NULL) { | |
cb323159 A |
1963 | /* no copy-on-read for mapped binaries */ |
1964 | vmk_flags.vmkf_no_copy_on_read = 1; | |
5ba3f43e A |
1965 | ret = vm_map_enter_mem_object_control( |
1966 | map, | |
1967 | &cur_start, | |
1968 | cur_end - cur_start, | |
1969 | (mach_vm_offset_t)0, | |
1970 | VM_FLAGS_FIXED, | |
1971 | vmk_flags, | |
1972 | VM_KERN_MEMORY_NONE, | |
1973 | control, | |
1974 | file_start + cur_offset, | |
1975 | TRUE, /* copy */ | |
1976 | initprot, maxprot, | |
1977 | VM_INHERIT_DEFAULT); | |
1978 | } else { | |
1979 | ret = vm_map_enter_mem_object( | |
1980 | map, | |
1981 | &cur_start, | |
1982 | cur_end - cur_start, | |
1983 | (mach_vm_offset_t)0, | |
1984 | VM_FLAGS_FIXED, | |
1985 | vmk_flags, | |
1986 | VM_KERN_MEMORY_NONE, | |
1987 | IPC_PORT_NULL, | |
1988 | 0, /* offset */ | |
1989 | TRUE, /* copy */ | |
1990 | initprot, maxprot, | |
1991 | VM_INHERIT_DEFAULT); | |
1992 | } | |
1993 | if (ret != KERN_SUCCESS) { | |
0a7de745 | 1994 | return LOAD_NOSPACE; |
5ba3f43e A |
1995 | } |
1996 | cur_offset += cur_end - cur_start; | |
1997 | } | |
1998 | #endif /* __arm64__ */ | |
3e170ce0 A |
1999 | done: |
2000 | assert(cur_end >= vm_start + (file_end - file_start)); | |
2001 | return LOAD_SUCCESS; | |
2002 | } | |
2003 | ||
1c79356b A |
2004 | static |
2005 | load_return_t | |
b0d623f7 | 2006 | load_segment( |
0a7de745 A |
2007 | struct load_command *lcp, |
2008 | uint32_t filetype, | |
2009 | void * control, | |
2010 | off_t pager_offset, | |
2011 | off_t macho_size, | |
2012 | struct vnode *vp, | |
2013 | vm_map_t map, | |
2014 | int64_t slide, | |
f427ee49 A |
2015 | load_result_t *result, |
2016 | struct image_params *imgp) | |
1c79356b | 2017 | { |
b0d623f7 | 2018 | struct segment_command_64 segment_command, *scp; |
0a7de745 A |
2019 | kern_return_t ret; |
2020 | vm_map_size_t delta_size; | |
2021 | vm_prot_t initprot; | |
2022 | vm_prot_t maxprot; | |
2023 | size_t segment_command_size, total_section_size, | |
2024 | single_section_size; | |
f427ee49 A |
2025 | uint64_t file_offset, file_size; |
2026 | vm_map_offset_t vm_offset; | |
2027 | size_t vm_size; | |
0a7de745 A |
2028 | vm_map_offset_t vm_start, vm_end, vm_end_aligned; |
2029 | vm_map_offset_t file_start, file_end; | |
2030 | kern_return_t kr; | |
2031 | boolean_t verbose; | |
2032 | vm_map_size_t effective_page_size; | |
2033 | vm_map_offset_t effective_page_mask; | |
5ba3f43e | 2034 | #if __arm64__ |
0a7de745 A |
2035 | vm_map_kernel_flags_t vmk_flags; |
2036 | boolean_t fourk_align; | |
5ba3f43e | 2037 | #endif /* __arm64__ */ |
3e170ce0 | 2038 | |
f427ee49 A |
2039 | (void)imgp; |
2040 | ||
2041 | effective_page_size = vm_map_page_size(map); | |
2042 | effective_page_mask = vm_map_page_mask(map); | |
3e170ce0 A |
2043 | |
2044 | verbose = FALSE; | |
b0d623f7 A |
2045 | if (LC_SEGMENT_64 == lcp->cmd) { |
2046 | segment_command_size = sizeof(struct segment_command_64); | |
2047 | single_section_size = sizeof(struct section_64); | |
5ba3f43e A |
2048 | #if __arm64__ |
2049 | /* 64-bit binary: should already be 16K-aligned */ | |
2050 | fourk_align = FALSE; | |
f427ee49 A |
2051 | |
2052 | if (vm_map_page_shift(map) == FOURK_PAGE_SHIFT && | |
2053 | PAGE_SHIFT != FOURK_PAGE_SHIFT) { | |
2054 | fourk_align = TRUE; | |
2055 | verbose = TRUE; | |
2056 | } | |
5ba3f43e | 2057 | #endif /* __arm64__ */ |
b0d623f7 A |
2058 | } else { |
2059 | segment_command_size = sizeof(struct segment_command); | |
2060 | single_section_size = sizeof(struct section); | |
5ba3f43e A |
2061 | #if __arm64__ |
2062 | /* 32-bit binary: might need 4K-alignment */ | |
2063 | if (effective_page_size != FOURK_PAGE_SIZE) { | |
2064 | /* not using 4K page size: need fourk_pager */ | |
2065 | fourk_align = TRUE; | |
2066 | verbose = TRUE; | |
2067 | } else { | |
2068 | /* using 4K page size: no need for re-alignment */ | |
2069 | fourk_align = FALSE; | |
2070 | } | |
2071 | #endif /* __arm64__ */ | |
b0d623f7 | 2072 | } |
0a7de745 | 2073 | if (lcp->cmdsize < segment_command_size) { |
f427ee49 | 2074 | DEBUG4K_ERROR("LOAD_BADMACHO cmdsize %d < %zu\n", lcp->cmdsize, segment_command_size); |
0a7de745 A |
2075 | return LOAD_BADMACHO; |
2076 | } | |
b0d623f7 A |
2077 | total_section_size = lcp->cmdsize - segment_command_size; |
2078 | ||
3e170ce0 | 2079 | if (LC_SEGMENT_64 == lcp->cmd) { |
6d2010ae | 2080 | scp = (struct segment_command_64 *)lcp; |
3e170ce0 | 2081 | } else { |
6d2010ae A |
2082 | scp = &segment_command; |
2083 | widen_segment_command((struct segment_command *)lcp, scp); | |
2084 | } | |
2085 | ||
3e170ce0 A |
2086 | if (verbose) { |
2087 | MACHO_PRINTF(("+++ load_segment %s " | |
0a7de745 A |
2088 | "vm[0x%llx:0x%llx] file[0x%llx:0x%llx] " |
2089 | "prot %d/%d flags 0x%x\n", | |
2090 | scp->segname, | |
2091 | (uint64_t)(slide + scp->vmaddr), | |
2092 | (uint64_t)(slide + scp->vmaddr + scp->vmsize), | |
2093 | pager_offset + scp->fileoff, | |
2094 | pager_offset + scp->fileoff + scp->filesize, | |
2095 | scp->initprot, | |
2096 | scp->maxprot, | |
2097 | scp->flags)); | |
3e170ce0 A |
2098 | } |
2099 | ||
91447636 A |
2100 | /* |
2101 | * Make sure what we get from the file is really ours (as specified | |
2102 | * by macho_size). | |
2103 | */ | |
b0d623f7 | 2104 | if (scp->fileoff + scp->filesize < scp->fileoff || |
3e170ce0 | 2105 | scp->fileoff + scp->filesize > (uint64_t)macho_size) { |
f427ee49 | 2106 | DEBUG4K_ERROR("LOAD_BADMACHO fileoff 0x%llx filesize 0x%llx macho_size 0x%llx\n", scp->fileoff, scp->filesize, (uint64_t)macho_size); |
0a7de745 | 2107 | return LOAD_BADMACHO; |
3e170ce0 | 2108 | } |
b0d623f7 A |
2109 | /* |
2110 | * Ensure that the number of sections specified would fit | |
2111 | * within the load command size. | |
2112 | */ | |
3e170ce0 | 2113 | if (total_section_size / single_section_size < scp->nsects) { |
f427ee49 | 2114 | DEBUG4K_ERROR("LOAD_BADMACHO 0x%zx 0x%zx %d\n", total_section_size, single_section_size, scp->nsects); |
0a7de745 | 2115 | return LOAD_BADMACHO; |
3e170ce0 | 2116 | } |
2d21ac55 A |
2117 | /* |
2118 | * Make sure the segment is page-aligned in the file. | |
2119 | */ | |
f427ee49 A |
2120 | if (os_add_overflow(pager_offset, scp->fileoff, &file_offset)) { |
2121 | DEBUG4K_ERROR("LOAD_BADMACHO file_offset: 0x%llx + 0x%llx\n", pager_offset, scp->fileoff); | |
2122 | return LOAD_BADMACHO; | |
2123 | } | |
3e170ce0 | 2124 | file_size = scp->filesize; |
5ba3f43e A |
2125 | #if __arm64__ |
2126 | if (fourk_align) { | |
2127 | if ((file_offset & FOURK_PAGE_MASK) != 0) { | |
2128 | /* | |
2129 | * we can't mmap() it if it's not at least 4KB-aligned | |
2130 | * in the file | |
2131 | */ | |
f427ee49 | 2132 | DEBUG4K_ERROR("LOAD_BADMACHO file_offset 0x%llx\n", file_offset); |
5ba3f43e A |
2133 | return LOAD_BADMACHO; |
2134 | } | |
2135 | } else | |
2136 | #endif /* __arm64__ */ | |
3e170ce0 | 2137 | if ((file_offset & PAGE_MASK_64) != 0 || |
0a7de745 | 2138 | /* we can't mmap() it if it's not page-aligned in the file */ |
3e170ce0 A |
2139 | (file_offset & vm_map_page_mask(map)) != 0) { |
2140 | /* | |
2141 | * The 1st test would have failed if the system's page size | |
2142 | * was what this process believe is the page size, so let's | |
2143 | * fail here too for the sake of consistency. | |
2144 | */ | |
f427ee49 | 2145 | DEBUG4K_ERROR("LOAD_BADMACHO file_offset 0x%llx\n", file_offset); |
0a7de745 | 2146 | return LOAD_BADMACHO; |
3e170ce0 | 2147 | } |
91447636 | 2148 | |
04b8595b A |
2149 | /* |
2150 | * If we have a code signature attached for this slice | |
2151 | * require that the segments are within the signed part | |
2152 | * of the file. | |
2153 | */ | |
2154 | if (result->cs_end_offset && | |
2155 | result->cs_end_offset < (off_t)scp->fileoff && | |
0a7de745 A |
2156 | result->cs_end_offset - scp->fileoff < scp->filesize) { |
2157 | if (cs_debug) { | |
04b8595b | 2158 | printf("section outside code signature\n"); |
0a7de745 | 2159 | } |
f427ee49 | 2160 | DEBUG4K_ERROR("LOAD_BADMACHO end_offset 0x%llx fileoff 0x%llx filesize 0x%llx\n", result->cs_end_offset, scp->fileoff, scp->filesize); |
04b8595b A |
2161 | return LOAD_BADMACHO; |
2162 | } | |
2163 | ||
cb323159 A |
2164 | if (os_add_overflow(scp->vmaddr, slide, &vm_offset)) { |
2165 | if (cs_debug) { | |
2166 | printf("vmaddr too large\n"); | |
2167 | } | |
f427ee49 | 2168 | DEBUG4K_ERROR("LOAD_BADMACHO vmaddr 0x%llx slide 0x%llx vm_offset 0x%llx\n", scp->vmaddr, slide, (uint64_t)vm_offset); |
cb323159 A |
2169 | return LOAD_BADMACHO; |
2170 | } | |
2171 | ||
f427ee49 A |
2172 | if (scp->vmsize > SIZE_MAX) { |
2173 | DEBUG4K_ERROR("LOAD_BADMACHO vmsize 0x%llx\n", scp->vmsize); | |
2174 | return LOAD_BADMACHO; | |
2175 | } | |
2176 | ||
2177 | vm_size = (size_t)scp->vmsize; | |
3e170ce0 | 2178 | |
0a7de745 A |
2179 | if (vm_size == 0) { |
2180 | return LOAD_SUCCESS; | |
2181 | } | |
3e170ce0 A |
2182 | if (scp->vmaddr == 0 && |
2183 | file_size == 0 && | |
2184 | vm_size != 0 && | |
b0d623f7 A |
2185 | (scp->initprot & VM_PROT_ALL) == VM_PROT_NONE && |
2186 | (scp->maxprot & VM_PROT_ALL) == VM_PROT_NONE) { | |
f427ee49 A |
2187 | if (map == VM_MAP_NULL) { |
2188 | return LOAD_SUCCESS; | |
2189 | } | |
2190 | ||
0c530ab8 | 2191 | /* |
6d2010ae A |
2192 | * For PIE, extend page zero rather than moving it. Extending |
2193 | * page zero keeps early allocations from falling predictably | |
2194 | * between the end of page zero and the beginning of the first | |
2195 | * slid segment. | |
0c530ab8 | 2196 | */ |
fe8ab488 A |
2197 | /* |
2198 | * This is a "page zero" segment: it starts at address 0, | |
2199 | * is not mapped from the binary file and is not accessible. | |
2200 | * User-space should never be able to access that memory, so | |
2201 | * make it completely off limits by raising the VM map's | |
2202 | * minimum offset. | |
2203 | */ | |
f427ee49 | 2204 | vm_end = (vm_map_offset_t)(vm_offset + vm_size); |
3e170ce0 | 2205 | if (vm_end < vm_offset) { |
f427ee49 | 2206 | DEBUG4K_ERROR("LOAD_BADMACHO vm_end 0x%llx vm_offset 0x%llx vm_size 0x%llx\n", (uint64_t)vm_end, (uint64_t)vm_offset, (uint64_t)vm_size); |
0a7de745 | 2207 | return LOAD_BADMACHO; |
3e170ce0 | 2208 | } |
f427ee49 | 2209 | |
3e170ce0 A |
2210 | if (verbose) { |
2211 | MACHO_PRINTF(("++++++ load_segment: " | |
0a7de745 A |
2212 | "page_zero up to 0x%llx\n", |
2213 | (uint64_t) vm_end)); | |
3e170ce0 | 2214 | } |
5ba3f43e A |
2215 | #if __arm64__ |
2216 | if (fourk_align) { | |
2217 | /* raise min_offset as much as page-alignment allows */ | |
2218 | vm_end_aligned = vm_map_trunc_page(vm_end, | |
0a7de745 | 2219 | effective_page_mask); |
5ba3f43e A |
2220 | } else |
2221 | #endif /* __arm64__ */ | |
3e170ce0 A |
2222 | { |
2223 | vm_end = vm_map_round_page(vm_end, | |
0a7de745 | 2224 | PAGE_MASK_64); |
3e170ce0 A |
2225 | vm_end_aligned = vm_end; |
2226 | } | |
2227 | ret = vm_map_raise_min_offset(map, | |
0a7de745 | 2228 | vm_end_aligned); |
5ba3f43e A |
2229 | #if __arm64__ |
2230 | if (ret == 0 && | |
2231 | vm_end > vm_end_aligned) { | |
2232 | /* use fourk_pager to map the rest of pagezero */ | |
2233 | assert(fourk_align); | |
2234 | vmk_flags = VM_MAP_KERNEL_FLAGS_NONE; | |
2235 | vmk_flags.vmkf_fourk = TRUE; | |
2236 | ret = vm_map_enter_mem_object( | |
2237 | map, | |
2238 | &vm_end_aligned, | |
2239 | vm_end - vm_end_aligned, | |
0a7de745 | 2240 | (mach_vm_offset_t) 0, /* mask */ |
5ba3f43e A |
2241 | VM_FLAGS_FIXED, |
2242 | vmk_flags, | |
2243 | VM_KERN_MEMORY_NONE, | |
2244 | IPC_PORT_NULL, | |
2245 | 0, | |
0a7de745 | 2246 | FALSE, /* copy */ |
5ba3f43e A |
2247 | (scp->initprot & VM_PROT_ALL), |
2248 | (scp->maxprot & VM_PROT_ALL), | |
2249 | VM_INHERIT_DEFAULT); | |
2250 | } | |
2251 | #endif /* __arm64__ */ | |
0a7de745 | 2252 | |
fe8ab488 | 2253 | if (ret != KERN_SUCCESS) { |
f427ee49 | 2254 | DEBUG4K_ERROR("LOAD_FAILURE ret 0x%x\n", ret); |
0a7de745 | 2255 | return LOAD_FAILURE; |
0c530ab8 | 2256 | } |
0a7de745 | 2257 | return LOAD_SUCCESS; |
3e170ce0 | 2258 | } else { |
f427ee49 | 2259 | #if !defined(XNU_TARGET_OS_OSX) |
5ba3f43e A |
2260 | /* not PAGEZERO: should not be mapped at address 0 */ |
2261 | if (filetype != MH_DYLINKER && scp->vmaddr == 0) { | |
f427ee49 | 2262 | DEBUG4K_ERROR("LOAD_BADMACHO filetype %d vmaddr 0x%llx\n", filetype, scp->vmaddr); |
5ba3f43e A |
2263 | return LOAD_BADMACHO; |
2264 | } | |
f427ee49 | 2265 | #endif /* !defined(XNU_TARGET_OS_OSX) */ |
0c530ab8 A |
2266 | } |
2267 | ||
5ba3f43e A |
2268 | #if __arm64__ |
2269 | if (fourk_align) { | |
2270 | /* 4K-align */ | |
2271 | file_start = vm_map_trunc_page(file_offset, | |
0a7de745 | 2272 | FOURK_PAGE_MASK); |
5ba3f43e | 2273 | file_end = vm_map_round_page(file_offset + file_size, |
0a7de745 | 2274 | FOURK_PAGE_MASK); |
5ba3f43e | 2275 | vm_start = vm_map_trunc_page(vm_offset, |
0a7de745 | 2276 | FOURK_PAGE_MASK); |
5ba3f43e | 2277 | vm_end = vm_map_round_page(vm_offset + vm_size, |
0a7de745 | 2278 | FOURK_PAGE_MASK); |
f427ee49 A |
2279 | |
2280 | if (file_offset - file_start > FOURK_PAGE_MASK || | |
2281 | file_end - file_offset - file_size > FOURK_PAGE_MASK) { | |
2282 | DEBUG4K_ERROR("LOAD_BADMACHO file_start / file_size wrap " | |
2283 | "[0x%llx:0x%llx] -> [0x%llx:0x%llx]\n", | |
2284 | file_offset, | |
2285 | file_offset + file_size, | |
2286 | (uint64_t) file_start, | |
2287 | (uint64_t) file_end); | |
2288 | return LOAD_BADMACHO; | |
2289 | } | |
2290 | ||
5ba3f43e A |
2291 | if (!strncmp(scp->segname, "__LINKEDIT", 11) && |
2292 | page_aligned(file_start) && | |
2293 | vm_map_page_aligned(file_start, vm_map_page_mask(map)) && | |
2294 | page_aligned(vm_start) && | |
2295 | vm_map_page_aligned(vm_start, vm_map_page_mask(map))) { | |
2296 | /* XXX last segment: ignore mis-aligned tail */ | |
2297 | file_end = vm_map_round_page(file_end, | |
0a7de745 | 2298 | effective_page_mask); |
5ba3f43e | 2299 | vm_end = vm_map_round_page(vm_end, |
0a7de745 | 2300 | effective_page_mask); |
5ba3f43e A |
2301 | } |
2302 | } else | |
2303 | #endif /* __arm64__ */ | |
3e170ce0 A |
2304 | { |
2305 | file_start = vm_map_trunc_page(file_offset, | |
0a7de745 | 2306 | effective_page_mask); |
3e170ce0 | 2307 | file_end = vm_map_round_page(file_offset + file_size, |
0a7de745 | 2308 | effective_page_mask); |
3e170ce0 | 2309 | vm_start = vm_map_trunc_page(vm_offset, |
0a7de745 | 2310 | effective_page_mask); |
3e170ce0 | 2311 | vm_end = vm_map_round_page(vm_offset + vm_size, |
0a7de745 | 2312 | effective_page_mask); |
f427ee49 A |
2313 | |
2314 | if (file_offset - file_start > effective_page_mask || | |
2315 | file_end - file_offset - file_size > effective_page_mask) { | |
2316 | DEBUG4K_ERROR("LOAD_BADMACHO file_start / file_size wrap " | |
2317 | "[0x%llx:0x%llx] -> [0x%llx:0x%llx]\n", | |
2318 | file_offset, | |
2319 | file_offset + file_size, | |
2320 | (uint64_t) file_start, | |
2321 | (uint64_t) file_end); | |
2322 | return LOAD_BADMACHO; | |
2323 | } | |
3e170ce0 | 2324 | } |
6d2010ae | 2325 | |
0a7de745 | 2326 | if (vm_start < result->min_vm_addr) { |
3e170ce0 | 2327 | result->min_vm_addr = vm_start; |
0a7de745 A |
2328 | } |
2329 | if (vm_end > result->max_vm_addr) { | |
3e170ce0 | 2330 | result->max_vm_addr = vm_end; |
0a7de745 | 2331 | } |
6d2010ae | 2332 | |
0a7de745 A |
2333 | if (map == VM_MAP_NULL) { |
2334 | return LOAD_SUCCESS; | |
2335 | } | |
6d2010ae | 2336 | |
3e170ce0 | 2337 | if (vm_size > 0) { |
b0d623f7 A |
2338 | initprot = (scp->initprot) & VM_PROT_ALL; |
2339 | maxprot = (scp->maxprot) & VM_PROT_ALL; | |
91447636 A |
2340 | /* |
2341 | * Map a copy of the file into the address space. | |
2342 | */ | |
3e170ce0 A |
2343 | if (verbose) { |
2344 | MACHO_PRINTF(("++++++ load_segment: " | |
0a7de745 A |
2345 | "mapping at vm [0x%llx:0x%llx] of " |
2346 | "file [0x%llx:0x%llx]\n", | |
2347 | (uint64_t) vm_start, | |
2348 | (uint64_t) vm_end, | |
2349 | (uint64_t) file_start, | |
2350 | (uint64_t) file_end)); | |
39236c6e | 2351 | } |
3e170ce0 | 2352 | ret = map_segment(map, |
0a7de745 A |
2353 | vm_start, |
2354 | vm_end, | |
2355 | control, | |
2356 | file_start, | |
2357 | file_end, | |
2358 | initprot, | |
2359 | maxprot, | |
2360 | result); | |
3e170ce0 | 2361 | if (ret) { |
f427ee49 | 2362 | DEBUG4K_ERROR("LOAD_NOSPACE start 0x%llx end 0x%llx ret 0x%x\n", (uint64_t)vm_start, (uint64_t)vm_end, ret); |
3e170ce0 A |
2363 | return LOAD_NOSPACE; |
2364 | } | |
2365 | ||
2366 | #if FIXME | |
91447636 A |
2367 | /* |
2368 | * If the file didn't end on a page boundary, | |
2369 | * we need to zero the leftover. | |
2370 | */ | |
b0d623f7 | 2371 | delta_size = map_size - scp->filesize; |
91447636 | 2372 | if (delta_size > 0) { |
0a7de745 A |
2373 | mach_vm_offset_t tmp; |
2374 | ||
5ba3f43e | 2375 | ret = mach_vm_allocate_kernel(kernel_map, &tmp, delta_size, VM_FLAGS_ANYWHERE, VM_KERN_MEMORY_BSD); |
3e170ce0 | 2376 | if (ret != KERN_SUCCESS) { |
f427ee49 | 2377 | DEBUG4K_ERROR("LOAD_RESOURCE delta_size 0x%llx ret 0x%x\n", delta_size, ret); |
0a7de745 | 2378 | return LOAD_RESOURCE; |
3e170ce0 | 2379 | } |
0a7de745 | 2380 | |
b0d623f7 | 2381 | if (copyout(tmp, map_addr + scp->filesize, |
0a7de745 | 2382 | delta_size)) { |
b0d623f7 | 2383 | (void) mach_vm_deallocate( |
0a7de745 | 2384 | kernel_map, tmp, delta_size); |
f427ee49 | 2385 | DEBUG4K_ERROR("LOAD_FAILURE copyout 0x%llx 0x%llx\n", map_addr + scp->filesize, delta_size); |
0a7de745 | 2386 | return LOAD_FAILURE; |
91447636 | 2387 | } |
0a7de745 | 2388 | |
b0d623f7 | 2389 | (void) mach_vm_deallocate(kernel_map, tmp, delta_size); |
91447636 A |
2390 | } |
2391 | #endif /* FIXME */ | |
2392 | } | |
1c79356b | 2393 | |
91447636 A |
2394 | /* |
2395 | * If the virtual size of the segment is greater | |
2396 | * than the size from the file, we need to allocate | |
2397 | * zero fill memory for the rest. | |
2398 | */ | |
3e170ce0 A |
2399 | if ((vm_end - vm_start) > (file_end - file_start)) { |
2400 | delta_size = (vm_end - vm_start) - (file_end - file_start); | |
2401 | } else { | |
2402 | delta_size = 0; | |
2403 | } | |
91447636 | 2404 | if (delta_size > 0) { |
f427ee49 A |
2405 | vm_map_offset_t tmp_start; |
2406 | vm_map_offset_t tmp_end; | |
2407 | ||
2408 | if (os_add_overflow(vm_start, file_end - file_start, &tmp_start)) { | |
2409 | DEBUG4K_ERROR("LOAD_NOSPACE tmp_start: 0x%llx + 0x%llx\n", (uint64_t)vm_start, (uint64_t)(file_end - file_start)); | |
2410 | return LOAD_NOSPACE; | |
2411 | } | |
2412 | ||
2413 | if (os_add_overflow(tmp_start, delta_size, &tmp_end)) { | |
2414 | DEBUG4K_ERROR("LOAD_NOSPACE tmp_end: 0x%llx + 0x%llx\n", (uint64_t)tmp_start, (uint64_t)delta_size); | |
2415 | return LOAD_NOSPACE; | |
2416 | } | |
3e170ce0 | 2417 | |
3e170ce0 A |
2418 | if (verbose) { |
2419 | MACHO_PRINTF(("++++++ load_segment: " | |
0a7de745 | 2420 | "delta mapping vm [0x%llx:0x%llx]\n", |
f427ee49 A |
2421 | (uint64_t) tmp_start, |
2422 | (uint64_t) tmp_end)); | |
3e170ce0 A |
2423 | } |
2424 | kr = map_segment(map, | |
f427ee49 A |
2425 | tmp_start, |
2426 | tmp_end, | |
0a7de745 A |
2427 | MEMORY_OBJECT_CONTROL_NULL, |
2428 | 0, | |
2429 | delta_size, | |
2430 | scp->initprot, | |
2431 | scp->maxprot, | |
2432 | result); | |
3e170ce0 | 2433 | if (kr != KERN_SUCCESS) { |
f427ee49 | 2434 | DEBUG4K_ERROR("LOAD_NOSPACE 0x%llx 0x%llx kr 0x%x\n", (unsigned long long)tmp_start, (uint64_t)delta_size, kr); |
0a7de745 | 2435 | return LOAD_NOSPACE; |
3e170ce0 | 2436 | } |
91447636 | 2437 | } |
1c79356b | 2438 | |
0a7de745 | 2439 | if ((scp->fileoff == 0) && (scp->filesize != 0)) { |
3e170ce0 | 2440 | result->mach_header = vm_offset; |
0a7de745 | 2441 | } |
0c530ab8 | 2442 | |
b0d623f7 | 2443 | if (scp->flags & SG_PROTECTED_VERSION_1) { |
3e170ce0 | 2444 | ret = unprotect_dsmos_segment(file_start, |
0a7de745 A |
2445 | file_end - file_start, |
2446 | vp, | |
2447 | pager_offset, | |
2448 | map, | |
2449 | vm_start, | |
2450 | vm_end - vm_start); | |
3e170ce0 | 2451 | if (ret != LOAD_SUCCESS) { |
f427ee49 | 2452 | DEBUG4K_ERROR("unprotect 0x%llx 0x%llx ret %d \n", (uint64_t)vm_start, (uint64_t)vm_end, ret); |
3e170ce0 A |
2453 | return ret; |
2454 | } | |
0c530ab8 A |
2455 | } else { |
2456 | ret = LOAD_SUCCESS; | |
2457 | } | |
3e170ce0 A |
2458 | |
2459 | if (LOAD_SUCCESS == ret && | |
2460 | filetype == MH_DYLINKER && | |
2461 | result->all_image_info_addr == MACH_VM_MIN_ADDRESS) { | |
b0d623f7 | 2462 | note_all_image_info_section(scp, |
0a7de745 A |
2463 | LC_SEGMENT_64 == lcp->cmd, |
2464 | single_section_size, | |
2465 | ((const char *)lcp + | |
2466 | segment_command_size), | |
2467 | slide, | |
2468 | result); | |
3e170ce0 | 2469 | } |
1c79356b | 2470 | |
c18c124e | 2471 | if (result->entry_point != MACH_VM_MIN_ADDRESS) { |
3e170ce0 | 2472 | if ((result->entry_point >= vm_offset) && (result->entry_point < (vm_offset + vm_size))) { |
0a7de745 | 2473 | if ((scp->initprot & (VM_PROT_READ | VM_PROT_EXECUTE)) == (VM_PROT_READ | VM_PROT_EXECUTE)) { |
c18c124e A |
2474 | result->validentry = 1; |
2475 | } else { | |
2476 | /* right range but wrong protections, unset if previously validated */ | |
2477 | result->validentry = 0; | |
2478 | } | |
2479 | } | |
2480 | } | |
0b4e3aa0 | 2481 | |
f427ee49 A |
2482 | if (ret != LOAD_SUCCESS && verbose) { |
2483 | DEBUG4K_ERROR("ret %d\n", ret); | |
2484 | } | |
6d2010ae | 2485 | return ret; |
1c79356b A |
2486 | } |
2487 | ||
39236c6e A |
2488 | static |
2489 | load_return_t | |
2490 | load_uuid( | |
0a7de745 A |
2491 | struct uuid_command *uulp, |
2492 | char *command_end, | |
2493 | load_result_t *result | |
2494 | ) | |
39236c6e | 2495 | { |
0a7de745 A |
2496 | /* |
2497 | * We need to check the following for this command: | |
2498 | * - The command size should be atleast the size of struct uuid_command | |
2499 | * - The UUID part of the command should be completely within the mach-o header | |
2500 | */ | |
316670eb | 2501 | |
0a7de745 A |
2502 | if ((uulp->cmdsize < sizeof(struct uuid_command)) || |
2503 | (((char *)uulp + sizeof(struct uuid_command)) > command_end)) { | |
2504 | return LOAD_BADMACHO; | |
2505 | } | |
2506 | ||
2507 | memcpy(&result->uuid[0], &uulp->uuid[0], sizeof(result->uuid)); | |
2508 | return LOAD_SUCCESS; | |
39236c6e | 2509 | } |
316670eb | 2510 | |
cb323159 A |
2511 | static |
2512 | load_return_t | |
2513 | load_version( | |
2514 | struct version_min_command *vmc, | |
2515 | boolean_t *found_version_cmd, | |
f427ee49 | 2516 | int ip_flags __unused, |
cb323159 A |
2517 | load_result_t *result |
2518 | ) | |
2519 | { | |
2520 | uint32_t platform = 0; | |
2521 | uint32_t sdk; | |
c3c9b80d | 2522 | uint32_t min_sdk; |
cb323159 A |
2523 | |
2524 | if (vmc->cmdsize < sizeof(*vmc)) { | |
2525 | return LOAD_BADMACHO; | |
2526 | } | |
2527 | if (*found_version_cmd == TRUE) { | |
2528 | return LOAD_BADMACHO; | |
2529 | } | |
2530 | *found_version_cmd = TRUE; | |
2531 | sdk = vmc->sdk; | |
c3c9b80d | 2532 | min_sdk = vmc->version; |
cb323159 A |
2533 | switch (vmc->cmd) { |
2534 | case LC_VERSION_MIN_MACOSX: | |
2535 | platform = PLATFORM_MACOS; | |
2536 | break; | |
2537 | #if __x86_64__ /* __x86_64__ */ | |
2538 | case LC_VERSION_MIN_IPHONEOS: | |
2539 | platform = PLATFORM_IOSSIMULATOR; | |
2540 | break; | |
2541 | case LC_VERSION_MIN_WATCHOS: | |
2542 | platform = PLATFORM_WATCHOSSIMULATOR; | |
2543 | break; | |
2544 | case LC_VERSION_MIN_TVOS: | |
2545 | platform = PLATFORM_TVOSSIMULATOR; | |
2546 | break; | |
2547 | #else | |
2548 | case LC_VERSION_MIN_IPHONEOS: { | |
2549 | #if __arm64__ | |
2550 | extern int legacy_footprint_entitlement_mode; | |
2551 | if (vmc->sdk < (12 << 16)) { | |
2552 | /* app built with a pre-iOS12 SDK: apply legacy footprint mitigation */ | |
2553 | result->legacy_footprint = TRUE; | |
2554 | } | |
2555 | #endif /* __arm64__ */ | |
2556 | platform = PLATFORM_IOS; | |
2557 | break; | |
2558 | } | |
2559 | case LC_VERSION_MIN_WATCHOS: | |
2560 | platform = PLATFORM_WATCHOS; | |
2561 | break; | |
2562 | case LC_VERSION_MIN_TVOS: | |
2563 | platform = PLATFORM_TVOS; | |
2564 | break; | |
2565 | #endif /* __x86_64__ */ | |
2566 | /* All LC_VERSION_MIN_* load commands are legacy and we will not be adding any more */ | |
2567 | default: | |
2568 | sdk = (uint32_t)-1; | |
c3c9b80d | 2569 | min_sdk = (uint32_t)-1; |
cb323159 A |
2570 | __builtin_unreachable(); |
2571 | } | |
2572 | result->ip_platform = platform; | |
c3c9b80d A |
2573 | result->lr_min_sdk = min_sdk; |
2574 | result->lr_sdk = sdk; | |
cb323159 A |
2575 | return LOAD_SUCCESS; |
2576 | } | |
2577 | ||
316670eb A |
2578 | static |
2579 | load_return_t | |
2580 | load_main( | |
0a7de745 A |
2581 | struct entry_point_command *epc, |
2582 | thread_t thread, | |
2583 | int64_t slide, | |
2584 | load_result_t *result | |
2585 | ) | |
316670eb A |
2586 | { |
2587 | mach_vm_offset_t addr; | |
0a7de745 A |
2588 | kern_return_t ret; |
2589 | ||
2590 | if (epc->cmdsize < sizeof(*epc)) { | |
2591 | return LOAD_BADMACHO; | |
2592 | } | |
316670eb | 2593 | if (result->thread_count != 0) { |
0a7de745 A |
2594 | return LOAD_FAILURE; |
2595 | } | |
2596 | ||
2597 | if (thread == THREAD_NULL) { | |
2598 | return LOAD_SUCCESS; | |
316670eb A |
2599 | } |
2600 | ||
39037602 A |
2601 | /* |
2602 | * LC_MAIN specifies stack size but not location. | |
2603 | * Add guard page to allocation size (MAXSSIZ includes guard page). | |
2604 | */ | |
316670eb | 2605 | if (epc->stacksize) { |
0a7de745 | 2606 | if (os_add_overflow(epc->stacksize, 4 * PAGE_SIZE, &result->user_stack_size)) { |
39037602 A |
2607 | /* |
2608 | * We are going to immediately throw away this result, but we want | |
2609 | * to make sure we aren't loading a dangerously close to | |
2610 | * overflowing value, since this will have a guard page added to it | |
2611 | * and be rounded to page boundaries | |
2612 | */ | |
2613 | return LOAD_BADMACHO; | |
2614 | } | |
316670eb | 2615 | result->user_stack_size = epc->stacksize; |
39037602 A |
2616 | if (os_add_overflow(epc->stacksize, PAGE_SIZE, &result->user_stack_alloc_size)) { |
2617 | return LOAD_BADMACHO; | |
2618 | } | |
0a7de745 | 2619 | result->custom_stack = TRUE; |
316670eb | 2620 | } else { |
39037602 | 2621 | result->user_stack_alloc_size = MAXSSIZ; |
316670eb | 2622 | } |
316670eb A |
2623 | |
2624 | /* use default location for stack */ | |
d9a64523 | 2625 | ret = thread_userstackdefault(&addr, result->is_64bit_addr); |
0a7de745 A |
2626 | if (ret != KERN_SUCCESS) { |
2627 | return LOAD_FAILURE; | |
2628 | } | |
316670eb A |
2629 | |
2630 | /* The stack slides down from the default location */ | |
f427ee49 | 2631 | result->user_stack = (user_addr_t)mach_vm_trunc_page((user_addr_t)addr - slide); |
316670eb | 2632 | |
c18c124e A |
2633 | if (result->using_lcmain || result->entry_point != MACH_VM_MIN_ADDRESS) { |
2634 | /* Already processed LC_MAIN or LC_UNIXTHREAD */ | |
0a7de745 | 2635 | return LOAD_FAILURE; |
c18c124e A |
2636 | } |
2637 | ||
316670eb A |
2638 | /* kernel does *not* use entryoff from LC_MAIN. Dyld uses it. */ |
2639 | result->needs_dynlinker = TRUE; | |
c18c124e | 2640 | result->using_lcmain = TRUE; |
39037602 A |
2641 | |
2642 | ret = thread_state_initialize( thread ); | |
2643 | if (ret != KERN_SUCCESS) { | |
0a7de745 | 2644 | return LOAD_FAILURE; |
39037602 A |
2645 | } |
2646 | ||
316670eb A |
2647 | result->unixproc = TRUE; |
2648 | result->thread_count++; | |
2649 | ||
0a7de745 | 2650 | return LOAD_SUCCESS; |
316670eb A |
2651 | } |
2652 | ||
cb323159 A |
2653 | static |
2654 | load_return_t | |
2655 | setup_driver_main( | |
2656 | thread_t thread, | |
2657 | int64_t slide, | |
2658 | load_result_t *result | |
2659 | ) | |
2660 | { | |
2661 | mach_vm_offset_t addr; | |
2662 | kern_return_t ret; | |
2663 | ||
2664 | /* Driver binaries have no LC_MAIN, use defaults */ | |
2665 | ||
2666 | if (thread == THREAD_NULL) { | |
2667 | return LOAD_SUCCESS; | |
2668 | } | |
2669 | ||
2670 | result->user_stack_alloc_size = MAXSSIZ; | |
2671 | ||
2672 | /* use default location for stack */ | |
2673 | ret = thread_userstackdefault(&addr, result->is_64bit_addr); | |
2674 | if (ret != KERN_SUCCESS) { | |
2675 | return LOAD_FAILURE; | |
2676 | } | |
2677 | ||
2678 | /* The stack slides down from the default location */ | |
f427ee49 | 2679 | result->user_stack = (user_addr_t)addr; |
cb323159 A |
2680 | result->user_stack -= slide; |
2681 | ||
2682 | if (result->using_lcmain || result->entry_point != MACH_VM_MIN_ADDRESS) { | |
2683 | /* Already processed LC_MAIN or LC_UNIXTHREAD */ | |
2684 | return LOAD_FAILURE; | |
2685 | } | |
2686 | ||
2687 | result->needs_dynlinker = TRUE; | |
2688 | ||
2689 | ret = thread_state_initialize( thread ); | |
2690 | if (ret != KERN_SUCCESS) { | |
2691 | return LOAD_FAILURE; | |
2692 | } | |
2693 | ||
2694 | result->unixproc = TRUE; | |
2695 | result->thread_count++; | |
2696 | ||
2697 | return LOAD_SUCCESS; | |
2698 | } | |
316670eb | 2699 | |
91447636 A |
2700 | static |
2701 | load_return_t | |
2702 | load_unixthread( | |
0a7de745 A |
2703 | struct thread_command *tcp, |
2704 | thread_t thread, | |
2705 | int64_t slide, | |
f427ee49 | 2706 | boolean_t is_x86_64_compat_binary, |
0a7de745 A |
2707 | load_result_t *result |
2708 | ) | |
91447636 | 2709 | { |
0a7de745 A |
2710 | load_return_t ret; |
2711 | int customstack = 0; | |
316670eb | 2712 | mach_vm_offset_t addr; |
0a7de745 A |
2713 | if (tcp->cmdsize < sizeof(*tcp)) { |
2714 | return LOAD_BADMACHO; | |
2715 | } | |
2d21ac55 | 2716 | if (result->thread_count != 0) { |
0a7de745 A |
2717 | return LOAD_FAILURE; |
2718 | } | |
2719 | ||
2720 | if (thread == THREAD_NULL) { | |
2721 | return LOAD_SUCCESS; | |
2d21ac55 | 2722 | } |
6d2010ae | 2723 | |
91447636 | 2724 | ret = load_threadstack(thread, |
0a7de745 A |
2725 | (uint32_t *)(((vm_offset_t)tcp) + |
2726 | sizeof(struct thread_command)), | |
2727 | tcp->cmdsize - sizeof(struct thread_command), | |
f427ee49 | 2728 | &addr, &customstack, is_x86_64_compat_binary, result); |
0a7de745 A |
2729 | if (ret != LOAD_SUCCESS) { |
2730 | return ret; | |
2731 | } | |
91447636 | 2732 | |
316670eb | 2733 | /* LC_UNIXTHREAD optionally specifies stack size and location */ |
d9a64523 | 2734 | |
0a7de745 A |
2735 | if (customstack) { |
2736 | result->custom_stack = TRUE; | |
2737 | } else { | |
39037602 | 2738 | result->user_stack_alloc_size = MAXSSIZ; |
316670eb | 2739 | } |
6d2010ae | 2740 | |
316670eb | 2741 | /* The stack slides down from the default location */ |
f427ee49 | 2742 | result->user_stack = (user_addr_t)mach_vm_trunc_page((user_addr_t)addr - slide); |
6d2010ae | 2743 | |
f427ee49 A |
2744 | { |
2745 | ret = load_threadentry(thread, | |
2746 | (uint32_t *)(((vm_offset_t)tcp) + | |
2747 | sizeof(struct thread_command)), | |
2748 | tcp->cmdsize - sizeof(struct thread_command), | |
2749 | &addr); | |
2750 | if (ret != LOAD_SUCCESS) { | |
2751 | return ret; | |
2752 | } | |
91447636 | 2753 | |
f427ee49 A |
2754 | if (result->using_lcmain || result->entry_point != MACH_VM_MIN_ADDRESS) { |
2755 | /* Already processed LC_MAIN or LC_UNIXTHREAD */ | |
2756 | return LOAD_FAILURE; | |
2757 | } | |
c18c124e | 2758 | |
f427ee49 A |
2759 | result->entry_point = (user_addr_t)addr; |
2760 | result->entry_point += slide; | |
6d2010ae | 2761 | |
f427ee49 A |
2762 | ret = load_threadstate(thread, |
2763 | (uint32_t *)(((vm_offset_t)tcp) + sizeof(struct thread_command)), | |
2764 | tcp->cmdsize - sizeof(struct thread_command), | |
2765 | result); | |
2766 | if (ret != LOAD_SUCCESS) { | |
2767 | return ret; | |
2768 | } | |
0a7de745 | 2769 | } |
91447636 A |
2770 | |
2771 | result->unixproc = TRUE; | |
2772 | result->thread_count++; | |
2773 | ||
0a7de745 | 2774 | return LOAD_SUCCESS; |
91447636 A |
2775 | } |
2776 | ||
1c79356b A |
2777 | static |
2778 | load_return_t | |
2779 | load_threadstate( | |
0a7de745 A |
2780 | thread_t thread, |
2781 | uint32_t *ts, | |
2782 | uint32_t total_size, | |
2783 | load_result_t *result | |
2784 | ) | |
1c79356b | 2785 | { |
0a7de745 A |
2786 | uint32_t size; |
2787 | int flavor; | |
2788 | uint32_t thread_size; | |
490019cf A |
2789 | uint32_t *local_ts = NULL; |
2790 | uint32_t local_ts_size = 0; | |
0a7de745 | 2791 | int ret; |
1c79356b | 2792 | |
490019cf | 2793 | (void)thread; |
04b8595b | 2794 | |
04b8595b A |
2795 | if (total_size > 0) { |
2796 | local_ts_size = total_size; | |
2797 | local_ts = kalloc(local_ts_size); | |
2798 | if (local_ts == NULL) { | |
490019cf | 2799 | return LOAD_FAILURE; |
04b8595b A |
2800 | } |
2801 | memcpy(local_ts, ts, local_ts_size); | |
2802 | ts = local_ts; | |
2803 | } | |
2804 | ||
1c79356b | 2805 | /* |
490019cf A |
2806 | * Validate the new thread state; iterate through the state flavors in |
2807 | * the Mach-O file. | |
2808 | * XXX: we should validate the machine state here, to avoid failing at | |
2809 | * activation time where we can't bail out cleanly. | |
1c79356b | 2810 | */ |
1c79356b | 2811 | while (total_size > 0) { |
f427ee49 A |
2812 | if (total_size < 2 * sizeof(uint32_t)) { |
2813 | return LOAD_BADMACHO; | |
2814 | } | |
2815 | ||
1c79356b A |
2816 | flavor = *ts++; |
2817 | size = *ts++; | |
490019cf | 2818 | |
39037602 | 2819 | if (os_add_and_mul_overflow(size, 2, sizeof(uint32_t), &thread_size) || |
490019cf | 2820 | os_sub_overflow(total_size, thread_size, &total_size)) { |
04b8595b | 2821 | ret = LOAD_BADMACHO; |
490019cf | 2822 | goto bad; |
2d21ac55 | 2823 | } |
490019cf | 2824 | |
0a7de745 | 2825 | ts += size; /* ts is a (uint32_t *) */ |
1c79356b | 2826 | } |
04b8595b | 2827 | |
490019cf A |
2828 | result->threadstate = local_ts; |
2829 | result->threadstate_sz = local_ts_size; | |
2830 | return LOAD_SUCCESS; | |
2831 | ||
2832 | bad: | |
2833 | if (local_ts) { | |
04b8595b | 2834 | kfree(local_ts, local_ts_size); |
04b8595b A |
2835 | } |
2836 | return ret; | |
1c79356b A |
2837 | } |
2838 | ||
f427ee49 | 2839 | |
1c79356b A |
2840 | static |
2841 | load_return_t | |
2842 | load_threadstack( | |
0a7de745 A |
2843 | thread_t thread, |
2844 | uint32_t *ts, | |
2845 | uint32_t total_size, | |
2846 | mach_vm_offset_t *user_stack, | |
2847 | int *customstack, | |
f427ee49 | 2848 | __unused boolean_t is_x86_64_compat_binary, |
0a7de745 A |
2849 | load_result_t *result |
2850 | ) | |
1c79356b | 2851 | { |
0a7de745 A |
2852 | kern_return_t ret; |
2853 | uint32_t size; | |
2854 | int flavor; | |
2855 | uint32_t stack_size; | |
1c79356b | 2856 | |
eb6b6ca3 A |
2857 | if (total_size == 0) { |
2858 | return LOAD_BADMACHO; | |
2859 | } | |
2860 | ||
1c79356b | 2861 | while (total_size > 0) { |
f427ee49 A |
2862 | if (total_size < 2 * sizeof(uint32_t)) { |
2863 | return LOAD_BADMACHO; | |
2864 | } | |
2865 | ||
1c79356b A |
2866 | flavor = *ts++; |
2867 | size = *ts++; | |
0a7de745 A |
2868 | if (UINT32_MAX - 2 < size || |
2869 | UINT32_MAX / sizeof(uint32_t) < size + 2) { | |
2870 | return LOAD_BADMACHO; | |
2871 | } | |
2872 | stack_size = (size + 2) * sizeof(uint32_t); | |
2873 | if (stack_size > total_size) { | |
2874 | return LOAD_BADMACHO; | |
2875 | } | |
91447636 A |
2876 | total_size -= stack_size; |
2877 | ||
2878 | /* | |
2879 | * Third argument is a kernel space pointer; it gets cast | |
2880 | * to the appropriate type in thread_userstack() based on | |
2881 | * the value of flavor. | |
2882 | */ | |
f427ee49 A |
2883 | { |
2884 | ret = thread_userstack(thread, flavor, (thread_state_t)ts, size, user_stack, customstack, result->is_64bit_data); | |
2885 | if (ret != KERN_SUCCESS) { | |
2886 | return LOAD_FAILURE; | |
2887 | } | |
2d21ac55 | 2888 | } |
f427ee49 | 2889 | |
0a7de745 | 2890 | ts += size; /* ts is a (uint32_t *) */ |
1c79356b | 2891 | } |
0a7de745 | 2892 | return LOAD_SUCCESS; |
1c79356b A |
2893 | } |
2894 | ||
2895 | static | |
2896 | load_return_t | |
2897 | load_threadentry( | |
0a7de745 A |
2898 | thread_t thread, |
2899 | uint32_t *ts, | |
2900 | uint32_t total_size, | |
2901 | mach_vm_offset_t *entry_point | |
2902 | ) | |
1c79356b | 2903 | { |
0a7de745 A |
2904 | kern_return_t ret; |
2905 | uint32_t size; | |
2906 | int flavor; | |
2907 | uint32_t entry_size; | |
1c79356b A |
2908 | |
2909 | /* | |
2910 | * Set the thread state. | |
2911 | */ | |
91447636 | 2912 | *entry_point = MACH_VM_MIN_ADDRESS; |
1c79356b | 2913 | while (total_size > 0) { |
f427ee49 A |
2914 | if (total_size < 2 * sizeof(uint32_t)) { |
2915 | return LOAD_BADMACHO; | |
2916 | } | |
2917 | ||
1c79356b A |
2918 | flavor = *ts++; |
2919 | size = *ts++; | |
0a7de745 A |
2920 | if (UINT32_MAX - 2 < size || |
2921 | UINT32_MAX / sizeof(uint32_t) < size + 2) { | |
2922 | return LOAD_BADMACHO; | |
2923 | } | |
2924 | entry_size = (size + 2) * sizeof(uint32_t); | |
2925 | if (entry_size > total_size) { | |
2926 | return LOAD_BADMACHO; | |
2927 | } | |
91447636 A |
2928 | total_size -= entry_size; |
2929 | /* | |
2930 | * Third argument is a kernel space pointer; it gets cast | |
2931 | * to the appropriate type in thread_entrypoint() based on | |
2932 | * the value of flavor. | |
2933 | */ | |
2934 | ret = thread_entrypoint(thread, flavor, (thread_state_t)ts, size, entry_point); | |
2d21ac55 | 2935 | if (ret != KERN_SUCCESS) { |
0a7de745 | 2936 | return LOAD_FAILURE; |
2d21ac55 | 2937 | } |
0a7de745 | 2938 | ts += size; /* ts is a (uint32_t *) */ |
1c79356b | 2939 | } |
0a7de745 | 2940 | return LOAD_SUCCESS; |
1c79356b A |
2941 | } |
2942 | ||
6d2010ae | 2943 | struct macho_data { |
0a7de745 | 2944 | struct nameidata __nid; |
6d2010ae | 2945 | union macho_vnode_header { |
0a7de745 A |
2946 | struct mach_header mach_header; |
2947 | struct fat_header fat_header; | |
2948 | char __pad[512]; | |
6d2010ae A |
2949 | } __header; |
2950 | }; | |
1c79356b | 2951 | |
c18c124e A |
2952 | #define DEFAULT_DYLD_PATH "/usr/lib/dyld" |
2953 | ||
39037602 A |
2954 | #if (DEVELOPMENT || DEBUG) |
2955 | extern char dyld_alt_path[]; | |
2956 | extern int use_alt_dyld; | |
2957 | #endif | |
2958 | ||
6d2010ae | 2959 | static load_return_t |
1c79356b | 2960 | load_dylinker( |
0a7de745 | 2961 | struct dylinker_command *lcp, |
f427ee49 | 2962 | cpu_type_t cputype, |
0a7de745 A |
2963 | vm_map_t map, |
2964 | thread_t thread, | |
2965 | int depth, | |
2966 | int64_t slide, | |
2967 | load_result_t *result, | |
2968 | struct image_params *imgp | |
2969 | ) | |
1c79356b | 2970 | { |
0a7de745 A |
2971 | const char *name; |
2972 | struct vnode *vp = NULLVP; /* set by get_macho_vnode() */ | |
2973 | struct mach_header *header; | |
2974 | off_t file_offset = 0; /* set by get_macho_vnode() */ | |
2975 | off_t macho_size = 0; /* set by get_macho_vnode() */ | |
2976 | load_result_t *myresult; | |
2977 | kern_return_t ret; | |
2978 | struct macho_data *macho_data; | |
6d2010ae | 2979 | struct { |
0a7de745 A |
2980 | struct mach_header __header; |
2981 | load_result_t __myresult; | |
2982 | struct macho_data __macho_data; | |
6d2010ae | 2983 | } *dyld_data; |
1c79356b | 2984 | |
0a7de745 | 2985 | if (lcp->cmdsize < sizeof(*lcp) || lcp->name.offset >= lcp->cmdsize) { |
5ba3f43e | 2986 | return LOAD_BADMACHO; |
0a7de745 | 2987 | } |
b0d623f7 | 2988 | |
5ba3f43e | 2989 | name = (const char *)lcp + lcp->name.offset; |
39037602 | 2990 | |
5ba3f43e A |
2991 | /* Check for a proper null terminated string. */ |
2992 | size_t maxsz = lcp->cmdsize - lcp->name.offset; | |
2993 | size_t namelen = strnlen(name, maxsz); | |
2994 | if (namelen >= maxsz) { | |
2995 | return LOAD_BADMACHO; | |
2996 | } | |
1c79356b | 2997 | |
39037602 A |
2998 | #if (DEVELOPMENT || DEBUG) |
2999 | ||
0a7de745 A |
3000 | /* |
3001 | * rdar://23680808 | |
3002 | * If an alternate dyld has been specified via boot args, check | |
3003 | * to see if PROC_UUID_ALT_DYLD_POLICY has been set on this | |
3004 | * executable and redirect the kernel to load that linker. | |
3005 | */ | |
3006 | ||
3007 | if (use_alt_dyld) { | |
3008 | int policy_error; | |
3009 | uint32_t policy_flags = 0; | |
3010 | int32_t policy_gencount = 0; | |
3011 | ||
3012 | policy_error = proc_uuid_policy_lookup(result->uuid, &policy_flags, &policy_gencount); | |
3013 | if (policy_error == 0) { | |
3014 | if (policy_flags & PROC_UUID_ALT_DYLD_POLICY) { | |
3015 | name = dyld_alt_path; | |
3016 | } | |
3017 | } | |
3018 | } | |
39037602 A |
3019 | #endif |
3020 | ||
c18c124e A |
3021 | #if !(DEVELOPMENT || DEBUG) |
3022 | if (0 != strcmp(name, DEFAULT_DYLD_PATH)) { | |
0a7de745 | 3023 | return LOAD_BADMACHO; |
c18c124e A |
3024 | } |
3025 | #endif | |
3026 | ||
6d2010ae A |
3027 | /* Allocate wad-of-data from heap to reduce excessively deep stacks */ |
3028 | ||
c3c9b80d | 3029 | dyld_data = kheap_alloc(KHEAP_TEMP, sizeof(*dyld_data), Z_WAITOK); |
6d2010ae A |
3030 | header = &dyld_data->__header; |
3031 | myresult = &dyld_data->__myresult; | |
3032 | macho_data = &dyld_data->__macho_data; | |
3033 | ||
f427ee49 A |
3034 | { |
3035 | cputype = (cputype & CPU_ARCH_MASK) | (cpu_type() & ~CPU_ARCH_MASK); | |
3036 | } | |
3037 | ||
3038 | ret = get_macho_vnode(name, cputype, header, | |
3039 | &file_offset, &macho_size, macho_data, &vp, imgp); | |
0a7de745 | 3040 | if (ret) { |
6d2010ae | 3041 | goto novp_out; |
0a7de745 | 3042 | } |
6d2010ae A |
3043 | |
3044 | *myresult = load_result_null; | |
d9a64523 A |
3045 | myresult->is_64bit_addr = result->is_64bit_addr; |
3046 | myresult->is_64bit_data = result->is_64bit_data; | |
1c79356b | 3047 | |
6d2010ae | 3048 | ret = parse_machfile(vp, map, thread, header, file_offset, |
0a7de745 | 3049 | macho_size, depth, slide, 0, myresult, result, imgp); |
6d2010ae | 3050 | |
490019cf A |
3051 | if (ret == LOAD_SUCCESS) { |
3052 | if (result->threadstate) { | |
3053 | /* don't use the app's threadstate if we have a dyld */ | |
3054 | kfree(result->threadstate, result->threadstate_sz); | |
3055 | } | |
3056 | result->threadstate = myresult->threadstate; | |
3057 | result->threadstate_sz = myresult->threadstate_sz; | |
3058 | ||
1c79356b | 3059 | result->dynlinker = TRUE; |
6d2010ae | 3060 | result->entry_point = myresult->entry_point; |
316670eb | 3061 | result->validentry = myresult->validentry; |
6d2010ae A |
3062 | result->all_image_info_addr = myresult->all_image_info_addr; |
3063 | result->all_image_info_size = myresult->all_image_info_size; | |
fe8ab488 A |
3064 | if (myresult->platform_binary) { |
3065 | result->csflags |= CS_DYLD_PLATFORM; | |
3066 | } | |
f427ee49 | 3067 | |
1c79356b | 3068 | } |
39037602 | 3069 | |
f427ee49 A |
3070 | struct vnode_attr *va; |
3071 | va = kheap_alloc(KHEAP_TEMP, sizeof(*va), Z_WAITOK | Z_ZERO); | |
3072 | VATTR_INIT(va); | |
3073 | VATTR_WANTED(va, va_fsid64); | |
3074 | VATTR_WANTED(va, va_fsid); | |
3075 | VATTR_WANTED(va, va_fileid); | |
3076 | int error = vnode_getattr(vp, va, imgp->ip_vfs_context); | |
5ba3f43e | 3077 | if (error == 0) { |
f427ee49 A |
3078 | imgp->ip_dyld_fsid = vnode_get_va_fsid(va); |
3079 | imgp->ip_dyld_fsobjid = va->va_fileid; | |
5ba3f43e A |
3080 | } |
3081 | ||
91447636 | 3082 | vnode_put(vp); |
f427ee49 | 3083 | kheap_free(KHEAP_TEMP, va, sizeof(*va)); |
6d2010ae | 3084 | novp_out: |
c3c9b80d | 3085 | kheap_free(KHEAP_TEMP, dyld_data, sizeof(*dyld_data)); |
0a7de745 | 3086 | return ret; |
1c79356b A |
3087 | } |
3088 | ||
f427ee49 | 3089 | |
6d2010ae | 3090 | static load_return_t |
2d21ac55 | 3091 | load_code_signature( |
0a7de745 A |
3092 | struct linkedit_data_command *lcp, |
3093 | struct vnode *vp, | |
3094 | off_t macho_offset, | |
3095 | off_t macho_size, | |
3096 | cpu_type_t cputype, | |
f427ee49 | 3097 | cpu_subtype_t cpusubtype, |
0a7de745 A |
3098 | load_result_t *result, |
3099 | struct image_params *imgp) | |
2d21ac55 | 3100 | { |
0a7de745 A |
3101 | int ret; |
3102 | kern_return_t kr; | |
3103 | vm_offset_t addr; | |
3104 | int resid; | |
3105 | struct cs_blob *blob; | |
3106 | int error; | |
3107 | vm_size_t blob_size; | |
cb323159 | 3108 | uint32_t sum; |
f427ee49 | 3109 | boolean_t anyCPU; |
2d21ac55 A |
3110 | |
3111 | addr = 0; | |
3112 | blob = NULL; | |
3113 | ||
f427ee49 A |
3114 | cpusubtype &= ~CPU_SUBTYPE_MASK; |
3115 | ||
cb323159 A |
3116 | if (lcp->cmdsize != sizeof(struct linkedit_data_command)) { |
3117 | ret = LOAD_BADMACHO; | |
3118 | goto out; | |
3119 | } | |
3120 | ||
3121 | sum = 0; | |
3122 | if (os_add_overflow(lcp->dataoff, lcp->datasize, &sum) || sum > macho_size) { | |
2d21ac55 A |
3123 | ret = LOAD_BADMACHO; |
3124 | goto out; | |
3125 | } | |
3126 | ||
f427ee49 | 3127 | blob = ubc_cs_blob_get(vp, cputype, cpusubtype, macho_offset); |
d9a64523 | 3128 | |
fe8ab488 | 3129 | if (blob != NULL) { |
f427ee49 A |
3130 | /* we already have a blob for this vnode and cpu(sub)type */ |
3131 | anyCPU = blob->csb_cpu_type == -1; | |
3132 | if ((blob->csb_cpu_type != cputype && | |
3133 | blob->csb_cpu_subtype != cpusubtype && !anyCPU) || | |
d9a64523 | 3134 | blob->csb_base_offset != macho_offset) { |
fe8ab488 A |
3135 | /* the blob has changed for this vnode: fail ! */ |
3136 | ret = LOAD_BADMACHO; | |
d9a64523 | 3137 | goto out; |
fe8ab488 | 3138 | } |
d9a64523 A |
3139 | |
3140 | /* It matches the blob we want here, let's verify the version */ | |
f427ee49 | 3141 | if (!anyCPU && ubc_cs_generation_check(vp) == 0) { |
d9a64523 A |
3142 | /* No need to revalidate, we're good! */ |
3143 | ret = LOAD_SUCCESS; | |
3144 | goto out; | |
3145 | } | |
3146 | ||
3147 | /* That blob may be stale, let's revalidate. */ | |
f427ee49 | 3148 | error = ubc_cs_blob_revalidate(vp, blob, imgp, 0, result->ip_platform); |
d9a64523 A |
3149 | if (error == 0) { |
3150 | /* Revalidation succeeded, we're good! */ | |
f427ee49 A |
3151 | /* If we were revaliding a CS blob with any CPU arch we adjust it */ |
3152 | if (anyCPU) { | |
3153 | vnode_lock_spin(vp); | |
3154 | blob->csb_cpu_type = cputype; | |
3155 | blob->csb_cpu_subtype = cpusubtype; | |
3156 | vnode_unlock(vp); | |
3157 | } | |
d9a64523 A |
3158 | ret = LOAD_SUCCESS; |
3159 | goto out; | |
3160 | } | |
3161 | ||
3162 | if (error != EAGAIN) { | |
3163 | printf("load_code_signature: revalidation failed: %d\n", error); | |
3164 | ret = LOAD_FAILURE; | |
3165 | goto out; | |
3166 | } | |
3167 | ||
3168 | assert(error == EAGAIN); | |
3169 | ||
3170 | /* | |
3171 | * Revalidation was not possible for this blob. We just continue as if there was no blob, | |
3172 | * rereading the signature, and ubc_cs_blob_add will do the right thing. | |
3173 | */ | |
3174 | blob = NULL; | |
2d21ac55 A |
3175 | } |
3176 | ||
593a1d5f A |
3177 | blob_size = lcp->datasize; |
3178 | kr = ubc_cs_blob_allocate(&addr, &blob_size); | |
2d21ac55 A |
3179 | if (kr != KERN_SUCCESS) { |
3180 | ret = LOAD_NOSPACE; | |
3181 | goto out; | |
3182 | } | |
0a7de745 | 3183 | |
2d21ac55 A |
3184 | resid = 0; |
3185 | error = vn_rdwr(UIO_READ, | |
0a7de745 A |
3186 | vp, |
3187 | (caddr_t) addr, | |
3188 | lcp->datasize, | |
3189 | macho_offset + lcp->dataoff, | |
3190 | UIO_SYSSPACE, | |
3191 | 0, | |
3192 | kauth_cred_get(), | |
3193 | &resid, | |
3194 | current_proc()); | |
2d21ac55 A |
3195 | if (error || resid != 0) { |
3196 | ret = LOAD_IOERROR; | |
3197 | goto out; | |
3198 | } | |
3199 | ||
3200 | if (ubc_cs_blob_add(vp, | |
f427ee49 | 3201 | result->ip_platform, |
0a7de745 | 3202 | cputype, |
f427ee49 | 3203 | cpusubtype, |
0a7de745 A |
3204 | macho_offset, |
3205 | &addr, | |
3206 | lcp->datasize, | |
3207 | imgp, | |
3208 | 0, | |
3209 | &blob)) { | |
39037602 A |
3210 | if (addr) { |
3211 | ubc_cs_blob_deallocate(addr, blob_size); | |
eb6b6ca3 | 3212 | addr = 0; |
39037602 | 3213 | } |
2d21ac55 A |
3214 | ret = LOAD_FAILURE; |
3215 | goto out; | |
3216 | } else { | |
3217 | /* ubc_cs_blob_add() has consumed "addr" */ | |
3218 | addr = 0; | |
3219 | } | |
6d2010ae A |
3220 | |
3221 | #if CHECK_CS_VALIDATION_BITMAP | |
3222 | ubc_cs_validation_bitmap_allocate( vp ); | |
3223 | #endif | |
0a7de745 | 3224 | |
2d21ac55 A |
3225 | ret = LOAD_SUCCESS; |
3226 | out: | |
fe8ab488 | 3227 | if (ret == LOAD_SUCCESS) { |
0a7de745 | 3228 | if (blob == NULL) { |
5ba3f43e | 3229 | panic("success, but no blob!"); |
0a7de745 | 3230 | } |
3e170ce0 | 3231 | |
2d21ac55 | 3232 | result->csflags |= blob->csb_flags; |
fe8ab488 | 3233 | result->platform_binary = blob->csb_platform_binary; |
04b8595b | 3234 | result->cs_end_offset = blob->csb_end_offset; |
2d21ac55 A |
3235 | } |
3236 | if (addr != 0) { | |
593a1d5f | 3237 | ubc_cs_blob_deallocate(addr, blob_size); |
2d21ac55 A |
3238 | addr = 0; |
3239 | } | |
3240 | ||
3241 | return ret; | |
3242 | } | |
3243 | ||
593a1d5f A |
3244 | |
3245 | #if CONFIG_CODE_DECRYPTION | |
3246 | ||
3247 | static load_return_t | |
3248 | set_code_unprotect( | |
3e170ce0 | 3249 | struct encryption_info_command *eip, |
0a7de745 | 3250 | caddr_t addr, |
3e170ce0 A |
3251 | vm_map_t map, |
3252 | int64_t slide, | |
3253 | struct vnode *vp, | |
3254 | off_t macho_offset, | |
3255 | cpu_type_t cputype, | |
3256 | cpu_subtype_t cpusubtype) | |
593a1d5f | 3257 | { |
3e170ce0 | 3258 | int error, len; |
593a1d5f A |
3259 | pager_crypt_info_t crypt_info; |
3260 | const char * cryptname = 0; | |
6d2010ae | 3261 | char *vpath; |
0a7de745 | 3262 | |
593a1d5f A |
3263 | size_t offset; |
3264 | struct segment_command_64 *seg64; | |
3265 | struct segment_command *seg32; | |
3266 | vm_map_offset_t map_offset, map_size; | |
3e170ce0 | 3267 | vm_object_offset_t crypto_backing_offset; |
593a1d5f | 3268 | kern_return_t kr; |
b0d623f7 | 3269 | |
0a7de745 A |
3270 | if (eip->cmdsize < sizeof(*eip)) { |
3271 | return LOAD_BADMACHO; | |
3272 | } | |
3273 | ||
3274 | switch (eip->cryptid) { | |
3275 | case 0: | |
3276 | /* not encrypted, just an empty load command */ | |
3277 | return LOAD_SUCCESS; | |
3278 | case 1: | |
3279 | cryptname = "com.apple.unfree"; | |
3280 | break; | |
3281 | case 0x10: | |
3282 | /* some random cryptid that you could manually put into | |
3283 | * your binary if you want NULL */ | |
3284 | cryptname = "com.apple.null"; | |
3285 | break; | |
3286 | default: | |
3287 | return LOAD_BADMACHO; | |
3288 | } | |
3289 | ||
3290 | if (map == VM_MAP_NULL) { | |
3291 | return LOAD_SUCCESS; | |
3292 | } | |
3293 | if (NULL == text_crypter_create) { | |
3294 | return LOAD_FAILURE; | |
593a1d5f | 3295 | } |
6d2010ae | 3296 | |
f427ee49 | 3297 | vpath = zalloc(ZV_NAMEI); |
0a7de745 | 3298 | |
593a1d5f | 3299 | len = MAXPATHLEN; |
3e170ce0 A |
3300 | error = vn_getpath(vp, vpath, &len); |
3301 | if (error) { | |
f427ee49 | 3302 | zfree(ZV_NAMEI, vpath); |
6d2010ae A |
3303 | return LOAD_FAILURE; |
3304 | } | |
0a7de745 | 3305 | |
593a1d5f | 3306 | /* set up decrypter first */ |
39236c6e A |
3307 | crypt_file_data_t crypt_data = { |
3308 | .filename = vpath, | |
3309 | .cputype = cputype, | |
0a7de745 A |
3310 | .cpusubtype = cpusubtype |
3311 | }; | |
3312 | kr = text_crypter_create(&crypt_info, cryptname, (void*)&crypt_data); | |
39037602 A |
3313 | #if VM_MAP_DEBUG_APPLE_PROTECT |
3314 | if (vm_map_debug_apple_protect) { | |
3315 | struct proc *p; | |
3316 | p = current_proc(); | |
3317 | printf("APPLE_PROTECT: %d[%s] map %p %s(%s) -> 0x%x\n", | |
0a7de745 | 3318 | p->p_pid, p->p_comm, map, __FUNCTION__, vpath, kr); |
39037602 A |
3319 | } |
3320 | #endif /* VM_MAP_DEBUG_APPLE_PROTECT */ | |
f427ee49 | 3321 | zfree(ZV_NAMEI, vpath); |
0a7de745 A |
3322 | |
3323 | if (kr) { | |
c910b4d9 | 3324 | printf("set_code_unprotect: unable to create decrypter %s, kr=%d\n", |
0a7de745 | 3325 | cryptname, kr); |
39236c6e A |
3326 | if (kr == kIOReturnNotPrivileged) { |
3327 | /* text encryption returned decryption failure */ | |
0a7de745 A |
3328 | return LOAD_DECRYPTFAIL; |
3329 | } else { | |
39236c6e | 3330 | return LOAD_RESOURCE; |
0a7de745 | 3331 | } |
593a1d5f | 3332 | } |
0a7de745 | 3333 | |
593a1d5f A |
3334 | /* this is terrible, but we have to rescan the load commands to find the |
3335 | * virtual address of this encrypted stuff. This code is gonna look like | |
3336 | * the dyld source one day... */ | |
3337 | struct mach_header *header = (struct mach_header *)addr; | |
3338 | size_t mach_header_sz = sizeof(struct mach_header); | |
3339 | if (header->magic == MH_MAGIC_64 || | |
3340 | header->magic == MH_CIGAM_64) { | |
0a7de745 | 3341 | mach_header_sz = sizeof(struct mach_header_64); |
593a1d5f A |
3342 | } |
3343 | offset = mach_header_sz; | |
3344 | uint32_t ncmds = header->ncmds; | |
3345 | while (ncmds--) { | |
3346 | /* | |
3347 | * Get a pointer to the command. | |
3348 | */ | |
3349 | struct load_command *lcp = (struct load_command *)(addr + offset); | |
3350 | offset += lcp->cmdsize; | |
0a7de745 A |
3351 | |
3352 | switch (lcp->cmd) { | |
3353 | case LC_SEGMENT_64: | |
3354 | seg64 = (struct segment_command_64 *)lcp; | |
3355 | if ((seg64->fileoff <= eip->cryptoff) && | |
3356 | (seg64->fileoff + seg64->filesize >= | |
3357 | eip->cryptoff + eip->cryptsize)) { | |
f427ee49 | 3358 | map_offset = (vm_map_offset_t)(seg64->vmaddr + eip->cryptoff - seg64->fileoff + slide); |
0a7de745 A |
3359 | map_size = eip->cryptsize; |
3360 | crypto_backing_offset = macho_offset + eip->cryptoff; | |
3361 | goto remap_now; | |
3362 | } | |
f427ee49 | 3363 | break; |
0a7de745 A |
3364 | case LC_SEGMENT: |
3365 | seg32 = (struct segment_command *)lcp; | |
3366 | if ((seg32->fileoff <= eip->cryptoff) && | |
3367 | (seg32->fileoff + seg32->filesize >= | |
3368 | eip->cryptoff + eip->cryptsize)) { | |
f427ee49 | 3369 | map_offset = (vm_map_offset_t)(seg32->vmaddr + eip->cryptoff - seg32->fileoff + slide); |
0a7de745 A |
3370 | map_size = eip->cryptsize; |
3371 | crypto_backing_offset = macho_offset + eip->cryptoff; | |
3372 | goto remap_now; | |
3373 | } | |
f427ee49 | 3374 | break; |
593a1d5f A |
3375 | } |
3376 | } | |
0a7de745 | 3377 | |
593a1d5f | 3378 | /* if we get here, did not find anything */ |
c910b4d9 | 3379 | return LOAD_BADMACHO; |
0a7de745 | 3380 | |
593a1d5f A |
3381 | remap_now: |
3382 | /* now remap using the decrypter */ | |
3e170ce0 | 3383 | MACHO_PRINTF(("+++ set_code_unprotect: vm[0x%llx:0x%llx]\n", |
0a7de745 A |
3384 | (uint64_t) map_offset, |
3385 | (uint64_t) (map_offset + map_size))); | |
3e170ce0 | 3386 | kr = vm_map_apple_protected(map, |
0a7de745 A |
3387 | map_offset, |
3388 | map_offset + map_size, | |
3389 | crypto_backing_offset, | |
f427ee49 A |
3390 | &crypt_info, |
3391 | CRYPTID_APP_ENCRYPTION); | |
3e170ce0 | 3392 | if (kr) { |
c910b4d9 | 3393 | printf("set_code_unprotect(): mapping failed with %x\n", kr); |
c910b4d9 A |
3394 | return LOAD_PROTECT; |
3395 | } | |
0a7de745 | 3396 | |
593a1d5f A |
3397 | return LOAD_SUCCESS; |
3398 | } | |
3399 | ||
3400 | #endif | |
3401 | ||
91447636 A |
3402 | /* |
3403 | * This routine exists to support the load_dylinker(). | |
3404 | * | |
3405 | * This routine has its own, separate, understanding of the FAT file format, | |
3406 | * which is terrifically unfortunate. | |
3407 | */ | |
1c79356b A |
3408 | static |
3409 | load_return_t | |
3410 | get_macho_vnode( | |
0a7de745 | 3411 | const char *path, |
f427ee49 | 3412 | cpu_type_t cputype, |
0a7de745 A |
3413 | struct mach_header *mach_header, |
3414 | off_t *file_offset, | |
3415 | off_t *macho_size, | |
3416 | struct macho_data *data, | |
f427ee49 A |
3417 | struct vnode **vpp, |
3418 | struct image_params *imgp | |
0a7de745 | 3419 | ) |
1c79356b | 3420 | { |
0a7de745 A |
3421 | struct vnode *vp; |
3422 | vfs_context_t ctx = vfs_context_current(); | |
3423 | proc_t p = vfs_context_proc(ctx); | |
3424 | kauth_cred_t kerncred; | |
3425 | struct nameidata *ndp = &data->__nid; | |
3426 | boolean_t is_fat; | |
3427 | struct fat_arch fat_arch; | |
3428 | int error; | |
1c79356b | 3429 | int resid; |
6d2010ae | 3430 | union macho_vnode_header *header = &data->__header; |
0b4e3aa0 | 3431 | off_t fsize = (off_t)0; |
6d2010ae | 3432 | |
2d21ac55 A |
3433 | /* |
3434 | * Capture the kernel credential for use in the actual read of the | |
3435 | * file, since the user doing the execution may have execute rights | |
3436 | * but not read rights, but to exec something, we have to either map | |
3437 | * or read it into the new process address space, which requires | |
3438 | * read rights. This is to deal with lack of common credential | |
3439 | * serialization code which would treat NOCRED as "serialize 'root'". | |
3440 | */ | |
3441 | kerncred = vfs_context_ucred(vfs_context_kernel()); | |
91447636 | 3442 | |
1c79356b | 3443 | /* init the namei data to point the file user's program name */ |
6d2010ae | 3444 | NDINIT(ndp, LOOKUP, OP_OPEN, FOLLOW | LOCKLEAF, UIO_SYSSPACE, CAST_USER_ADDR_T(path), ctx); |
1c79356b | 3445 | |
91447636 | 3446 | if ((error = namei(ndp)) != 0) { |
2d21ac55 | 3447 | if (error == ENOENT) { |
55e303ae | 3448 | error = LOAD_ENOENT; |
2d21ac55 | 3449 | } else { |
55e303ae | 3450 | error = LOAD_FAILURE; |
2d21ac55 | 3451 | } |
0a7de745 | 3452 | return error; |
55e303ae | 3453 | } |
91447636 | 3454 | nameidone(ndp); |
1c79356b | 3455 | vp = ndp->ni_vp; |
6d2010ae | 3456 | |
1c79356b A |
3457 | /* check for regular file */ |
3458 | if (vp->v_type != VREG) { | |
55e303ae | 3459 | error = LOAD_PROTECT; |
1c79356b A |
3460 | goto bad1; |
3461 | } | |
3462 | ||
91447636 | 3463 | /* get size */ |
2d21ac55 | 3464 | if ((error = vnode_size(vp, &fsize, ctx)) != 0) { |
55e303ae | 3465 | error = LOAD_FAILURE; |
1c79356b | 3466 | goto bad1; |
55e303ae | 3467 | } |
1c79356b A |
3468 | |
3469 | /* Check mount point */ | |
3470 | if (vp->v_mount->mnt_flag & MNT_NOEXEC) { | |
55e303ae | 3471 | error = LOAD_PROTECT; |
1c79356b A |
3472 | goto bad1; |
3473 | } | |
3474 | ||
91447636 | 3475 | /* check access */ |
39236c6e | 3476 | if ((error = vnode_authorize(vp, NULL, KAUTH_VNODE_EXECUTE | KAUTH_VNODE_READ_DATA, ctx)) != 0) { |
55e303ae | 3477 | error = LOAD_PROTECT; |
1c79356b | 3478 | goto bad1; |
55e303ae | 3479 | } |
0b4e3aa0 | 3480 | |
1c79356b | 3481 | /* try to open it */ |
2d21ac55 | 3482 | if ((error = VNOP_OPEN(vp, FREAD, ctx)) != 0) { |
55e303ae | 3483 | error = LOAD_PROTECT; |
1c79356b | 3484 | goto bad1; |
0b4e3aa0 A |
3485 | } |
3486 | ||
0a7de745 | 3487 | if ((error = vn_rdwr(UIO_READ, vp, (caddr_t)header, sizeof(*header), 0, |
b0d623f7 | 3488 | UIO_SYSSPACE, IO_NODELOCKED, kerncred, &resid, p)) != 0) { |
55e303ae | 3489 | error = LOAD_IOERROR; |
1c79356b | 3490 | goto bad2; |
55e303ae | 3491 | } |
6d2010ae | 3492 | |
c18c124e A |
3493 | if (resid) { |
3494 | error = LOAD_BADMACHO; | |
3495 | goto bad2; | |
3496 | } | |
3497 | ||
6d2010ae A |
3498 | if (header->mach_header.magic == MH_MAGIC || |
3499 | header->mach_header.magic == MH_MAGIC_64) { | |
3500 | is_fat = FALSE; | |
c18c124e | 3501 | } else if (OSSwapBigToHostInt32(header->fat_header.magic) == FAT_MAGIC) { |
0a7de745 | 3502 | is_fat = TRUE; |
6d2010ae A |
3503 | } else { |
3504 | error = LOAD_BADMACHO; | |
3505 | goto bad2; | |
1c79356b A |
3506 | } |
3507 | ||
3508 | if (is_fat) { | |
c18c124e | 3509 | error = fatfile_validate_fatarches((vm_offset_t)(&header->fat_header), |
0a7de745 | 3510 | sizeof(*header)); |
c18c124e A |
3511 | if (error != LOAD_SUCCESS) { |
3512 | goto bad2; | |
3513 | } | |
3514 | ||
0b4e3aa0 | 3515 | /* Look up our architecture in the fat file. */ |
f427ee49 A |
3516 | error = fatfile_getbestarch_for_cputype(cputype, CPU_SUBTYPE_ANY, |
3517 | (vm_offset_t)(&header->fat_header), sizeof(*header), imgp, &fat_arch); | |
0a7de745 | 3518 | if (error != LOAD_SUCCESS) { |
1c79356b | 3519 | goto bad2; |
0a7de745 | 3520 | } |
0b4e3aa0 A |
3521 | |
3522 | /* Read the Mach-O header out of it */ | |
6d2010ae | 3523 | error = vn_rdwr(UIO_READ, vp, (caddr_t)&header->mach_header, |
0a7de745 | 3524 | sizeof(header->mach_header), fat_arch.offset, |
6d2010ae | 3525 | UIO_SYSSPACE, IO_NODELOCKED, kerncred, &resid, p); |
1c79356b | 3526 | if (error) { |
55e303ae | 3527 | error = LOAD_IOERROR; |
1c79356b A |
3528 | goto bad2; |
3529 | } | |
3530 | ||
c18c124e A |
3531 | if (resid) { |
3532 | error = LOAD_BADMACHO; | |
3533 | goto bad2; | |
3534 | } | |
3535 | ||
0b4e3aa0 | 3536 | /* Is this really a Mach-O? */ |
6d2010ae A |
3537 | if (header->mach_header.magic != MH_MAGIC && |
3538 | header->mach_header.magic != MH_MAGIC_64) { | |
1c79356b A |
3539 | error = LOAD_BADMACHO; |
3540 | goto bad2; | |
3541 | } | |
0b4e3aa0 | 3542 | |
1c79356b | 3543 | *file_offset = fat_arch.offset; |
2d21ac55 | 3544 | *macho_size = fat_arch.size; |
1c79356b | 3545 | } else { |
91447636 A |
3546 | /* |
3547 | * Force get_macho_vnode() to fail if the architecture bits | |
3548 | * do not match the expected architecture bits. This in | |
3549 | * turn causes load_dylinker() to fail for the same reason, | |
3550 | * so it ensures the dynamic linker and the binary are in | |
3551 | * lock-step. This is potentially bad, if we ever add to | |
3552 | * the CPU_ARCH_* bits any bits that are desirable but not | |
3553 | * required, since the dynamic linker might work, but we will | |
3554 | * refuse to load it because of this check. | |
3555 | */ | |
f427ee49 | 3556 | if ((cpu_type_t)header->mach_header.cputype != cputype) { |
6d2010ae A |
3557 | error = LOAD_BADARCH; |
3558 | goto bad2; | |
3559 | } | |
0b4e3aa0 | 3560 | |
1c79356b | 3561 | *file_offset = 0; |
91447636 | 3562 | *macho_size = fsize; |
1c79356b A |
3563 | } |
3564 | ||
6d2010ae | 3565 | *mach_header = header->mach_header; |
0b4e3aa0 | 3566 | *vpp = vp; |
91447636 A |
3567 | |
3568 | ubc_setsize(vp, fsize); | |
0a7de745 | 3569 | return error; |
0b4e3aa0 | 3570 | |
1c79356b | 3571 | bad2: |
6d2010ae | 3572 | (void) VNOP_CLOSE(vp, FREAD, ctx); |
1c79356b | 3573 | bad1: |
91447636 | 3574 | vnode_put(vp); |
0a7de745 | 3575 | return error; |
1c79356b | 3576 | } |