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