2 * Copyright (c) 2000-2020 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (C) 1988, 1989, NeXT, Inc.
31 * File: kern/mach_loader.c
32 * Author: Avadis Tevanian, Jr.
34 * Mach object file loader (kernel version, for now).
36 * 21-Jul-88 Avadis Tevanian, Jr. (avie) at NeXT
40 #include <sys/param.h>
41 #include <sys/vnode_internal.h>
43 #include <sys/namei.h>
44 #include <sys/proc_internal.h>
45 #include <sys/kauth.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>
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>
66 #include <machine/vmparam.h>
67 #include <machine/exec.h>
68 #include <machine/pal_routines.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>
80 #include <mach-o/fat.h>
81 #include <mach-o/loader.h>
84 #include <vm/vm_map.h>
85 #include <vm/vm_kern.h>
86 #include <vm/vm_pager.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 */
93 #include <os/overflow.h>
96 * XXX vm/pmap.h should not treat these prototypes as MACH_KERNEL_PRIVATE
97 * when KERNEL is defined.
99 extern pmap_t
pmap_create_options(ledger_t ledger
, vm_map_size_t size
,
101 #if __has_feature(ptrauth_calls) && XNU_TARGET_OS_OSX
102 extern void pmap_disable_user_jop(pmap_t pmap
);
103 #endif /* __has_feature(ptrauth_calls) && XNU_TARGET_OS_OSX */
105 /* XXX should have prototypes in a shared header file */
106 extern int get_map_nentries(vm_map_t
);
108 extern kern_return_t
memory_object_signed(memory_object_control_t control
,
109 boolean_t is_signed
);
111 /* An empty load_result_t */
112 static const load_result_t load_result_null
= {
113 .mach_header
= MACH_VM_MIN_ADDRESS
,
114 .entry_point
= MACH_VM_MIN_ADDRESS
,
115 .user_stack
= MACH_VM_MIN_ADDRESS
,
116 .user_stack_size
= 0,
117 .user_stack_alloc
= MACH_VM_MIN_ADDRESS
,
118 .user_stack_alloc_size
= 0,
119 .all_image_info_addr
= MACH_VM_MIN_ADDRESS
,
120 .all_image_info_size
= 0,
124 .needs_dynlinker
= 0,
133 .min_vm_addr
= MACH_VM_MAX_ADDRESS
,
134 .max_vm_addr
= MACH_VM_MIN_ADDRESS
,
139 .dynlinker_mach_header
= MACH_VM_MIN_ADDRESS
,
144 * Prototypes of static functions.
151 struct mach_header
*header
,
157 load_result_t
*result
,
158 load_result_t
*binresult
,
159 struct image_params
*imgp
164 struct load_command
*lcp
,
172 load_result_t
*result
,
173 struct image_params
*imgp
178 struct uuid_command
*uulp
,
180 load_result_t
*result
185 struct version_min_command
*vmc
,
186 boolean_t
*found_version_cmd
,
188 load_result_t
*result
193 struct linkedit_data_command
*lcp
,
198 cpu_subtype_t cpusubtype
,
199 load_result_t
*result
,
200 struct image_params
*imgp
);
202 #if CONFIG_CODE_DECRYPTION
205 struct encryption_info_command
*lcp
,
212 cpu_subtype_t cpusubtype
);
218 struct entry_point_command
*epc
,
221 load_result_t
*result
229 load_result_t
*result
234 struct thread_command
*tcp
,
237 boolean_t is_x86_64_compat_binary
,
238 load_result_t
*result
254 mach_vm_offset_t
*user_stack
,
256 boolean_t is_x86_64_compat_binary
,
257 load_result_t
*result
265 mach_vm_offset_t
*entry_point
270 struct dylinker_command
*lcp
,
276 load_result_t
*result
,
277 struct image_params
*imgp
282 extern int bootarg_no32exec
;
284 check_if_simulator_binary(
285 struct image_params
*imgp
,
296 struct mach_header
*mach_header
,
299 struct macho_data
*macho_data
,
301 struct image_params
*imgp
305 widen_segment_command(const struct segment_command
*scp32
,
306 struct segment_command_64
*scp
)
308 scp
->cmd
= scp32
->cmd
;
309 scp
->cmdsize
= scp32
->cmdsize
;
310 bcopy(scp32
->segname
, scp
->segname
, sizeof(scp
->segname
));
311 scp
->vmaddr
= scp32
->vmaddr
;
312 scp
->vmsize
= scp32
->vmsize
;
313 scp
->fileoff
= scp32
->fileoff
;
314 scp
->filesize
= scp32
->filesize
;
315 scp
->maxprot
= scp32
->maxprot
;
316 scp
->initprot
= scp32
->initprot
;
317 scp
->nsects
= scp32
->nsects
;
318 scp
->flags
= scp32
->flags
;
322 note_all_image_info_section(const struct segment_command_64
*scp
,
323 boolean_t is64
, size_t section_size
, const void *sections
,
324 int64_t slide
, load_result_t
*result
)
328 struct section_64 s64
;
333 if (strncmp(scp
->segname
, "__DATA_DIRTY", sizeof(scp
->segname
)) != 0 &&
334 strncmp(scp
->segname
, "__DATA", sizeof(scp
->segname
)) != 0) {
337 for (i
= 0; i
< scp
->nsects
; ++i
) {
338 sectionp
= (const void *)
339 ((const char *)sections
+ section_size
* i
);
340 if (0 == strncmp(sectionp
->s64
.sectname
, "__all_image_info",
341 sizeof(sectionp
->s64
.sectname
))) {
342 result
->all_image_info_addr
=
343 is64
? sectionp
->s64
.addr
: sectionp
->s32
.addr
;
344 result
->all_image_info_addr
+= slide
;
345 result
->all_image_info_size
=
346 is64
? sectionp
->s64
.size
: sectionp
->s32
.size
;
354 * Allow bypassing some security rules (hard pagezero, no write+execute)
355 * in exchange for better binary compatibility for legacy apps built
356 * before 16KB-alignment was enforced.
358 const int fourk_binary_compatibility_unsafe
= TRUE
;
359 const int fourk_binary_compatibility_allow_wx
= FALSE
;
360 #endif /* __arm64__ */
362 #if __has_feature(ptrauth_calls) && XNU_TARGET_OS_OSX
364 * Determines whether this is an arm64e process which may host in-process
368 arm64e_plugin_host(struct image_params
*imgp
, load_result_t
*result
)
370 if (imgp
->ip_flags
& IMGPF_NOJOP
) {
374 if (!result
->platform_binary
) {
378 struct cs_blob
*csblob
= csvnode_get_blob(imgp
->ip_vp
, imgp
->ip_arch_offset
);
379 const char *identity
= csblob_get_identity(csblob
);
384 /* Check if override host plugin entitlement is present and posix spawn attribute to disable A keys is passed */
385 if (IOVnodeHasEntitlement(imgp
->ip_vp
, (int64_t)imgp
->ip_arch_offset
, OVERRIDE_PLUGIN_HOST_ENTITLEMENT
)) {
386 return imgp
->ip_flags
& IMGPF_PLUGIN_HOST_DISABLE_A_KEYS
;
389 /* Disabling library validation is a good signal that this process plans to host plugins */
390 const char *const disable_lv_entitlements
[] = {
391 "com.apple.security.cs.disable-library-validation",
392 "com.apple.private.cs.automator-plugins",
393 CLEAR_LV_ENTITLEMENT
,
395 for (size_t i
= 0; i
< ARRAY_COUNT(disable_lv_entitlements
); i
++) {
396 if (IOVnodeHasEntitlement(imgp
->ip_vp
, (int64_t)imgp
->ip_arch_offset
, disable_lv_entitlements
[i
])) {
401 /* From /System/Library/Security/HardeningExceptions.plist */
402 const char *const hardening_exceptions
[] = {
403 "com.apple.perl5", /* Scripting engines may load third party code and jit*/
404 "com.apple.perl", /* Scripting engines may load third party code and jit*/
405 "org.python.python", /* Scripting engines may load third party code and jit*/
406 "com.apple.expect", /* Scripting engines may load third party code and jit*/
407 "com.tcltk.wish", /* Scripting engines may load third party code and jit*/
408 "com.tcltk.tclsh", /* Scripting engines may load third party code and jit*/
409 "com.apple.ruby", /* Scripting engines may load third party code and jit*/
410 "com.apple.bash", /* Required for the 'enable' command */
411 "com.apple.zsh", /* Required for the 'zmodload' command */
412 "com.apple.ksh", /* Required for 'builtin' command */
414 for (size_t i
= 0; i
< ARRAY_COUNT(hardening_exceptions
); i
++) {
415 if (strncmp(hardening_exceptions
[i
], identity
, strlen(hardening_exceptions
[i
])) == 0) {
422 #endif /* __has_feature(ptrauth_calls) && XNU_TARGET_OS_OSX */
426 struct image_params
*imgp
,
427 struct mach_header
*header
,
430 load_result_t
*result
433 struct vnode
*vp
= imgp
->ip_vp
;
434 off_t file_offset
= imgp
->ip_arch_offset
;
435 off_t macho_size
= imgp
->ip_arch_size
;
436 off_t total_size
= 0;
437 off_t file_size
= imgp
->ip_vattr
->va_data_size
;
438 pmap_t pmap
= 0; /* protected by create_map */
440 load_result_t myresult
;
442 boolean_t enforce_hard_pagezero
= TRUE
;
443 int in_exec
= (imgp
->ip_flags
& IMGPF_EXEC
);
444 task_t task
= current_task();
445 int64_t aslr_page_offset
= 0;
446 int64_t dyld_aslr_page_offset
= 0;
447 int64_t aslr_section_size
= 0;
448 int64_t aslr_section_offset
= 0;
450 unsigned int pmap_flags
= 0;
452 if (os_add_overflow(file_offset
, macho_size
, &total_size
) ||
453 total_size
> file_size
) {
454 return LOAD_BADMACHO
;
457 result
->is_64bit_addr
= ((imgp
->ip_flags
& IMGPF_IS_64BIT_ADDR
) == IMGPF_IS_64BIT_ADDR
);
458 result
->is_64bit_data
= ((imgp
->ip_flags
& IMGPF_IS_64BIT_DATA
) == IMGPF_IS_64BIT_DATA
);
459 #if defined(HAS_APPLE_PAC)
460 pmap_flags
|= (imgp
->ip_flags
& IMGPF_NOJOP
) ? PMAP_CREATE_DISABLE_JOP
: 0;
461 #endif /* defined(HAS_APPLE_PAC) */
462 pmap_flags
|= result
->is_64bit_addr
? PMAP_CREATE_64BIT
: 0;
465 if (imgp
->ip_new_thread
) {
466 ledger_task
= get_threadtask(imgp
->ip_new_thread
);
471 #if defined(PMAP_CREATE_FORCE_4K_PAGES) && (DEBUG || DEVELOPMENT)
472 if (imgp
->ip_px_sa
!= NULL
) {
473 struct _posix_spawnattr
* psa
= (struct _posix_spawnattr
*) imgp
->ip_px_sa
;
474 if (psa
->psa_flags
& _POSIX_SPAWN_FORCE_4K_PAGES
) {
475 pmap_flags
|= PMAP_CREATE_FORCE_4K_PAGES
;
478 #endif /* defined(PMAP_CREATE_FORCE_4K_PAGES) && (DEBUG || DEVELOPMENT) */
480 pmap
= pmap_create_options(get_task_ledger(ledger_task
),
484 return LOAD_RESOURCE
;
486 map
= vm_map_create(pmap
,
488 vm_compute_max_offset(result
->is_64bit_addr
),
491 #if defined(__arm64__)
492 if (result
->is_64bit_addr
) {
493 /* enforce 16KB alignment of VM map entries */
494 vm_map_set_page_shift(map
, SIXTEENK_PAGE_SHIFT
);
496 vm_map_set_page_shift(map
, page_shift_user32
);
498 #elif (__ARM_ARCH_7K__ >= 2) && defined(PLATFORM_WatchOS)
499 /* enforce 16KB alignment for watch targets with new ABI */
500 vm_map_set_page_shift(map
, SIXTEENK_PAGE_SHIFT
);
501 #endif /* __arm64__ */
503 #if PMAP_CREATE_FORCE_4K_PAGES
504 if (pmap_flags
& PMAP_CREATE_FORCE_4K_PAGES
) {
505 DEBUG4K_LIFE("***** launching '%s' as 4k *****\n", vp
->v_name
);
506 vm_map_set_page_shift(map
, FOURK_PAGE_SHIFT
);
508 #endif /* PMAP_CREATE_FORCE_4K_PAGES */
510 #ifndef CONFIG_ENFORCE_SIGNED_CODE
511 /* This turns off faulting for executable pages, which allows
512 * to circumvent Code Signing Enforcement. The per process
513 * flag (CS_ENFORCEMENT) is not set yet, but we can use the
516 if (!cs_process_global_enforcement() && (header
->flags
& MH_ALLOW_STACK_EXECUTION
)) {
517 vm_map_disable_NX(map
);
518 // TODO: Message Trace or log that this is happening
522 /* Forcibly disallow execution from data pages on even if the arch
523 * normally permits it. */
524 if ((header
->flags
& MH_NO_HEAP_EXECUTION
) && !(imgp
->ip_flags
& IMGPF_ALLOW_DATA_EXEC
)) {
525 vm_map_disallow_data_exec(map
);
529 * Compute a random offset for ASLR, and an independent random offset for dyld.
531 if (!(imgp
->ip_flags
& IMGPF_DISABLE_ASLR
)) {
532 vm_map_get_max_aslr_slide_section(map
, &aslr_section_offset
, &aslr_section_size
);
533 aslr_section_offset
= (random() % aslr_section_offset
) * aslr_section_size
;
535 aslr_page_offset
= random();
536 aslr_page_offset
%= vm_map_get_max_aslr_slide_pages(map
);
537 aslr_page_offset
<<= vm_map_page_shift(map
);
539 dyld_aslr_page_offset
= random();
540 dyld_aslr_page_offset
%= vm_map_get_max_loader_aslr_slide_pages(map
);
541 dyld_aslr_page_offset
<<= vm_map_page_shift(map
);
543 aslr_page_offset
+= aslr_section_offset
;
545 if (vm_map_page_shift(map
) < (int)PAGE_SHIFT
) {
546 DEBUG4K_LOAD("slide=0x%llx dyld_slide=0x%llx\n", aslr_page_offset
, dyld_aslr_page_offset
);
553 *result
= load_result_null
;
556 * re-set the bitness on the load result since we cleared the load result above.
558 result
->is_64bit_addr
= ((imgp
->ip_flags
& IMGPF_IS_64BIT_ADDR
) == IMGPF_IS_64BIT_ADDR
);
559 result
->is_64bit_data
= ((imgp
->ip_flags
& IMGPF_IS_64BIT_DATA
) == IMGPF_IS_64BIT_DATA
);
561 lret
= parse_machfile(vp
, map
, thread
, header
, file_offset
, macho_size
,
562 0, aslr_page_offset
, dyld_aslr_page_offset
, result
,
565 if (lret
!= LOAD_SUCCESS
) {
566 vm_map_deallocate(map
); /* will lose pmap reference too */
572 * On x86, for compatibility, don't enforce the hard page-zero restriction for 32-bit binaries.
574 if (!result
->is_64bit_addr
) {
575 enforce_hard_pagezero
= FALSE
;
579 * For processes with IMGPF_HIGH_BITS_ASLR, add a few random high bits
580 * to the start address for "anywhere" memory allocations.
582 #define VM_MAP_HIGH_START_BITS_COUNT 8
583 #define VM_MAP_HIGH_START_BITS_SHIFT 27
584 if (result
->is_64bit_addr
&&
585 (imgp
->ip_flags
& IMGPF_HIGH_BITS_ASLR
)) {
587 vm_map_offset_t high_start
;
589 random_bits
= random();
590 random_bits
&= (1 << VM_MAP_HIGH_START_BITS_COUNT
) - 1;
591 high_start
= (((vm_map_offset_t
)random_bits
)
592 << VM_MAP_HIGH_START_BITS_SHIFT
);
593 vm_map_set_high_start(map
, high_start
);
595 #endif /* __x86_64__ */
598 * Check to see if the page zero is enforced by the map->min_offset.
600 if (enforce_hard_pagezero
&&
601 (vm_map_has_hard_pagezero(map
, 0x1000) == FALSE
)) {
604 !result
->is_64bit_addr
&& /* not 64-bit address space */
605 !(header
->flags
& MH_PIE
) && /* not PIE */
606 (vm_map_page_shift(map
) != FOURK_PAGE_SHIFT
||
607 PAGE_SHIFT
!= FOURK_PAGE_SHIFT
) && /* page size != 4KB */
608 result
->has_pagezero
&& /* has a "soft" page zero */
609 fourk_binary_compatibility_unsafe
) {
611 * For backwards compatibility of "4K" apps on
612 * a 16K system, do not enforce a hard page zero...
615 #endif /* __arm64__ */
617 vm_map_deallocate(map
); /* will lose pmap reference too */
618 return LOAD_BADMACHO
;
623 if (enforce_hard_pagezero
&& result
->is_64bit_addr
&& (header
->cputype
== CPU_TYPE_ARM64
)) {
624 /* 64 bit ARM binary must have "hard page zero" of 4GB to cover the lower 32 bit address space */
625 if (vm_map_has_hard_pagezero(map
, 0x100000000) == FALSE
) {
626 vm_map_deallocate(map
); /* will lose pmap reference too */
627 return LOAD_BADMACHO
;
632 vm_commit_pagezero_status(map
);
635 * If this is an exec, then we are going to destroy the old
636 * task, and it's correct to halt it; if it's spawn, the
637 * task is not yet running, and it makes no sense.
640 proc_t p
= vfs_context_proc(imgp
->ip_vfs_context
);
642 * Mark the task as halting and start the other
643 * threads towards terminating themselves. Then
644 * make sure any threads waiting for a process
645 * transition get informed that we are committed to
646 * this transition, and then finally complete the
647 * task halting (wait for threads and then cleanup
650 * NOTE: task_start_halt() makes sure that no new
651 * threads are created in the task during the transition.
652 * We need to mark the workqueue as exiting before we
653 * wait for threads to terminate (at the end of which
654 * we no longer have a prohibition on thread creation).
656 * Finally, clean up any lingering workqueue data structures
657 * that may have been left behind by the workqueue threads
658 * as they exited (and then clean up the work queue itself).
660 kret
= task_start_halt(task
);
661 if (kret
!= KERN_SUCCESS
) {
662 vm_map_deallocate(map
); /* will lose pmap reference too */
665 proc_transcommit(p
, 0);
666 workq_mark_exiting(p
);
667 task_complete_halt(task
);
671 * Roll up accounting info to new task. The roll up is done after
672 * task_complete_halt to make sure the thread accounting info is
673 * rolled up to current_task.
675 task_rollup_accounting_info(get_threadtask(thread
), task
);
679 #if __has_feature(ptrauth_calls) && defined(XNU_TARGET_OS_OSX)
681 * arm64e plugin hosts currently run with JOP keys disabled, since they
682 * may need to run arm64 plugins.
684 if (arm64e_plugin_host(imgp
, result
)) {
685 imgp
->ip_flags
|= IMGPF_NOJOP
;
686 pmap_disable_user_jop(pmap
);
688 #endif /* __has_feature(ptrauth_calls) && defined(XNU_TARGET_OS_OSX) */
690 #ifdef CONFIG_32BIT_TELEMETRY
691 if (!result
->is_64bit_data
) {
693 * This may not need to be an AST; we merely need to ensure that
694 * we gather telemetry at the point where all of the information
695 * that we want has been added to the process.
697 task_set_32bit_log_flag(get_threadtask(thread
));
698 act_set_astbsd(thread
);
700 #endif /* CONFIG_32BIT_TELEMETRY */
705 int macho_printf
= 0;
706 #define MACHO_PRINTF(args) \
708 if (macho_printf) { \
717 cpu_subtype_t execsubtype
)
720 case CPU_TYPE_X86_64
:
725 switch (execsubtype
) {
726 case CPU_SUBTYPE_ARM_V7K
:
735 * The file size of a mach-o file is limited to 32 bits; this is because
736 * this is the limit on the kalloc() of enough bytes for a mach_header and
737 * the contents of its sizeofcmds, which is currently constrained to 32
738 * bits in the file format itself. We read into the kernel buffer the
739 * commands section, and then parse it in order to parse the mach-o file
740 * format load_command segment(s). We are only interested in a subset of
741 * the total set of possible commands. If "map"==VM_MAP_NULL or
742 * "thread"==THREAD_NULL, do not make permament VM modifications,
743 * just preflight the parse.
751 struct mach_header
*header
,
756 int64_t dyld_aslr_offset
,
757 load_result_t
*result
,
758 load_result_t
*binresult
,
759 struct image_params
*imgp
763 struct load_command
*lcp
;
764 struct dylinker_command
*dlp
= 0;
766 load_return_t ret
= LOAD_SUCCESS
;
768 vm_size_t alloc_size
, cmds_size
;
770 size_t oldoffset
; /* for overflow check */
772 proc_t p
= vfs_context_proc(imgp
->ip_vfs_context
);
775 int spawn
= (imgp
->ip_flags
& IMGPF_SPAWN
);
776 int vfexec
= (imgp
->ip_flags
& IMGPF_VFORK_EXEC
);
777 size_t mach_header_sz
= sizeof(struct mach_header
);
779 boolean_t got_code_signatures
= FALSE
;
780 boolean_t found_header_segment
= FALSE
;
781 boolean_t found_xhdr
= FALSE
;
782 boolean_t found_version_cmd
= FALSE
;
784 boolean_t dyld_no_load_addr
= FALSE
;
785 boolean_t is_dyld
= FALSE
;
786 vm_map_offset_t effective_page_mask
= PAGE_MASK
;
788 uint64_t pagezero_end
= 0;
789 uint64_t executable_end
= 0;
790 uint64_t writable_start
= 0;
791 vm_map_size_t effective_page_size
;
793 effective_page_mask
= vm_map_page_mask(map
);
794 effective_page_size
= vm_map_page_size(map
);
795 #endif /* __arm64__ */
797 if (header
->magic
== MH_MAGIC_64
||
798 header
->magic
== MH_CIGAM_64
) {
799 mach_header_sz
= sizeof(struct mach_header_64
);
803 * Break infinite recursion
812 * Check to see if right machine type.
814 if (((cpu_type_t
)(header
->cputype
& ~CPU_ARCH_MASK
) != (cpu_type() & ~CPU_ARCH_MASK
))
819 if (!grade_binary(header
->cputype
,
820 header
->cpusubtype
& ~CPU_SUBTYPE_MASK
,
821 header
->cpusubtype
& CPU_SUBTYPE_MASK
, TRUE
)) {
825 abi64
= ((header
->cputype
& CPU_ARCH_ABI64
) == CPU_ARCH_ABI64
);
827 switch (header
->filetype
) {
829 if (depth
!= 1 && depth
!= 3) {
832 if (header
->flags
& MH_DYLDLINK
) {
833 /* Check properties of dynamic executables */
834 if (!(header
->flags
& MH_PIE
) && pie_required(header
->cputype
, header
->cpusubtype
& ~CPU_SUBTYPE_MASK
)) {
837 result
->needs_dynlinker
= TRUE
;
838 } else if (header
->cputype
== CPU_TYPE_X86_64
) {
839 /* x86_64 static binaries allowed */
841 /* Check properties of static executables (disallowed except for development) */
842 #if !(DEVELOPMENT || DEBUG)
859 * For PIE and dyld, slide everything by the ASLR offset.
861 if ((header
->flags
& MH_PIE
) || is_dyld
) {
866 * Get the pager for the file.
868 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
870 /* ensure header + sizeofcmds falls within the file */
871 if (os_add_overflow(mach_header_sz
, header
->sizeofcmds
, &cmds_size
) ||
872 (off_t
)cmds_size
> macho_size
||
873 round_page_overflow(cmds_size
, &alloc_size
) ||
874 alloc_size
> INT_MAX
) {
875 return LOAD_BADMACHO
;
879 * Map the load commands into kernel memory.
881 addr
= kalloc(alloc_size
);
886 error
= vn_rdwr(UIO_READ
, vp
, addr
, (int)alloc_size
, file_offset
,
887 UIO_SYSSPACE
, 0, vfs_context_ucred(imgp
->ip_vfs_context
), &resid
, p
);
889 kfree(addr
, alloc_size
);
895 /* We must be able to read in as much as the mach_header indicated */
896 kfree(addr
, alloc_size
);
897 return LOAD_BADMACHO
;
902 * Scan through the commands, processing each one as necessary.
903 * We parse in three passes through the headers:
904 * 0: determine if TEXT and DATA boundary can be page-aligned, load platform version
905 * 1: thread state, uuid, code signature
907 * 3: dyld, encryption, check entry point
910 boolean_t slide_realign
= FALSE
;
913 slide_realign
= TRUE
;
917 for (pass
= 0; pass
<= 3; pass
++) {
923 is_pie
= ((header
->flags
& MH_PIE
) != 0);
924 if (pagezero_end
!= 0 &&
925 pagezero_end
< effective_page_size
) {
926 /* need at least 1 page for PAGEZERO */
927 adjust
= effective_page_size
;
928 MACHO_PRINTF(("pagezero boundary at "
929 "0x%llx; adjust slide from "
930 "0x%llx to 0x%llx%s\n",
931 (uint64_t) pagezero_end
,
936 : " BUT NO PIE ****** :-(")));
939 pagezero_end
+= adjust
;
940 executable_end
+= adjust
;
941 writable_start
+= adjust
;
944 if (pagezero_end
!= 0) {
945 result
->has_pagezero
= TRUE
;
947 if (executable_end
== writable_start
&&
948 (executable_end
& effective_page_mask
) != 0 &&
949 (executable_end
& FOURK_PAGE_MASK
) == 0) {
951 * The TEXT/DATA boundary is 4K-aligned but
952 * not page-aligned. Adjust the slide to make
953 * it page-aligned and avoid having a page
954 * with both write and execute permissions.
957 (effective_page_size
-
958 (executable_end
& effective_page_mask
));
959 MACHO_PRINTF(("page-unaligned X-W boundary at "
960 "0x%llx; adjust slide from "
961 "0x%llx to 0x%llx%s\n",
962 (uint64_t) executable_end
,
967 : " BUT NO PIE ****** :-(")));
972 #endif /* __arm64__ */
974 if (dyld_no_load_addr
&& binresult
) {
976 * The dyld Mach-O does not specify a load address. Try to locate
977 * it right after the main binary. If binresult == NULL, load
978 * directly to the given slide.
980 mach_vm_address_t max_vm_addr
= binresult
->max_vm_addr
;
981 slide
= vm_map_round_page(slide
+ max_vm_addr
, effective_page_mask
);
986 * Check that the entry point is contained in an executable segment
988 if ((pass
== 3) && (thread
!= THREAD_NULL
)) {
989 if (depth
== 1 && imgp
&& (imgp
->ip_flags
& IMGPF_DRIVER
)) {
990 /* Driver binaries must have driverkit platform */
991 if (result
->ip_platform
== PLATFORM_DRIVERKIT
) {
992 /* Driver binaries have no entry point */
993 ret
= setup_driver_main(thread
, slide
, result
);
997 } else if (!result
->using_lcmain
&& result
->validentry
== 0) {
1000 if (ret
!= KERN_SUCCESS
) {
1001 thread_state_initialize(thread
);
1007 * Check that some segment maps the start of the mach-o file, which is
1008 * needed by the dynamic loader to read the mach headers, etc.
1010 if ((pass
== 3) && (found_header_segment
== FALSE
)) {
1011 ret
= LOAD_BADMACHO
;
1016 * Loop through each of the load_commands indicated by the
1017 * Mach-O header; if an absurd value is provided, we just
1018 * run off the end of the reserved section by incrementing
1019 * the offset too far, so we are implicitly fail-safe.
1021 offset
= mach_header_sz
;
1022 ncmds
= header
->ncmds
;
1025 /* ensure enough space for a minimal load command */
1026 if (offset
+ sizeof(struct load_command
) > cmds_size
) {
1027 ret
= LOAD_BADMACHO
;
1032 * Get a pointer to the command.
1034 lcp
= (struct load_command
*)(addr
+ offset
);
1038 * Perform prevalidation of the struct load_command
1039 * before we attempt to use its contents. Invalid
1040 * values are ones which result in an overflow, or
1041 * which can not possibly be valid commands, or which
1042 * straddle or exist past the reserved section at the
1043 * start of the image.
1045 if (os_add_overflow(offset
, lcp
->cmdsize
, &offset
) ||
1046 lcp
->cmdsize
< sizeof(struct load_command
) ||
1047 offset
> cmds_size
) {
1048 ret
= LOAD_BADMACHO
;
1053 * Act on struct load_command's for which kernel
1054 * intervention is required.
1055 * Note that each load command implementation is expected to validate
1056 * that lcp->cmdsize is large enough to fit its specific struct type
1057 * before dereferencing fields not covered by struct load_command.
1061 struct segment_command
*scp
= (struct segment_command
*) lcp
;
1062 if (scp
->cmdsize
< sizeof(*scp
)) {
1063 ret
= LOAD_BADMACHO
;
1067 if (is_dyld
&& scp
->vmaddr
== 0 && scp
->fileoff
== 0) {
1068 dyld_no_load_addr
= TRUE
;
1069 if (!slide_realign
) {
1070 /* got what we need, bail early on pass 0 */
1078 if (scp
->initprot
== 0 && scp
->maxprot
== 0 && scp
->vmaddr
== 0) {
1080 if (os_add3_overflow(scp
->vmaddr
, scp
->vmsize
, slide
, &pagezero_end
) || pagezero_end
> UINT32_MAX
) {
1081 ret
= LOAD_BADMACHO
;
1085 if (scp
->initprot
& VM_PROT_EXECUTE
) {
1087 if (os_add3_overflow(scp
->vmaddr
, scp
->vmsize
, slide
, &executable_end
) || executable_end
> UINT32_MAX
) {
1088 ret
= LOAD_BADMACHO
;
1092 if (scp
->initprot
& VM_PROT_WRITE
) {
1094 if (os_add_overflow(scp
->vmaddr
, slide
, &writable_start
) || writable_start
> UINT32_MAX
) {
1095 ret
= LOAD_BADMACHO
;
1099 #endif /* __arm64__ */
1103 if (pass
== 1 && !strncmp(scp
->segname
, "__XHDR", sizeof(scp
->segname
))) {
1113 * Having an LC_SEGMENT command for the
1114 * wrong ABI is invalid <rdar://problem/11021230>
1116 ret
= LOAD_BADMACHO
;
1120 ret
= load_segment(lcp
,
1130 if (ret
== LOAD_SUCCESS
&& scp
->fileoff
== 0 && scp
->filesize
> 0) {
1131 /* Enforce a single segment mapping offset zero, with R+X
1133 if (found_header_segment
||
1134 ((scp
->initprot
& (VM_PROT_READ
| VM_PROT_EXECUTE
)) != (VM_PROT_READ
| VM_PROT_EXECUTE
))) {
1135 ret
= LOAD_BADMACHO
;
1138 found_header_segment
= TRUE
;
1143 case LC_SEGMENT_64
: {
1144 struct segment_command_64
*scp64
= (struct segment_command_64
*) lcp
;
1145 if (scp64
->cmdsize
< sizeof(*scp64
)) {
1146 ret
= LOAD_BADMACHO
;
1150 if (is_dyld
&& scp64
->vmaddr
== 0 && scp64
->fileoff
== 0) {
1151 dyld_no_load_addr
= TRUE
;
1153 /* got what we need, bail early on pass 0 */
1157 if (pass
== 1 && !strncmp(scp64
->segname
, "__XHDR", sizeof(scp64
->segname
))) {
1167 * Having an LC_SEGMENT_64 command for the
1168 * wrong ABI is invalid <rdar://problem/11021230>
1170 ret
= LOAD_BADMACHO
;
1174 ret
= load_segment(lcp
,
1185 if (ret
== LOAD_SUCCESS
&& scp64
->fileoff
== 0 && scp64
->filesize
> 0) {
1186 /* Enforce a single segment mapping offset zero, with R+X
1188 if (found_header_segment
||
1189 ((scp64
->initprot
& (VM_PROT_READ
| VM_PROT_EXECUTE
)) != (VM_PROT_READ
| VM_PROT_EXECUTE
))) {
1190 ret
= LOAD_BADMACHO
;
1193 found_header_segment
= TRUE
;
1198 case LC_UNIXTHREAD
: {
1199 boolean_t is_x86_64_compat_binary
= FALSE
;
1203 ret
= load_unixthread(
1204 (struct thread_command
*) lcp
,
1207 is_x86_64_compat_binary
,
1219 (struct entry_point_command
*) lcp
,
1224 case LC_LOAD_DYLINKER
:
1228 if ((depth
== 1) && (dlp
== 0)) {
1229 dlp
= (struct dylinker_command
*)lcp
;
1235 if (pass
== 1 && depth
== 1) {
1236 ret
= load_uuid((struct uuid_command
*) lcp
,
1237 (char *)addr
+ cmds_size
,
1241 case LC_CODE_SIGNATURE
:
1248 * load signatures & store in uip
1249 * set VM object "signed_pages"
1251 ret
= load_code_signature(
1252 (struct linkedit_data_command
*) lcp
,
1260 if (ret
!= LOAD_SUCCESS
) {
1261 printf("proc %d: load code signature error %d "
1262 "for file \"%s\"\n",
1263 p
->p_pid
, ret
, vp
->v_name
);
1265 * Allow injections to be ignored on devices w/o enforcement enabled
1267 if (!cs_process_global_enforcement()) {
1268 ret
= LOAD_SUCCESS
; /* ignore error */
1271 got_code_signatures
= TRUE
;
1274 if (got_code_signatures
) {
1275 unsigned tainted
= CS_VALIDATE_TAINTED
;
1276 boolean_t valid
= FALSE
;
1280 if (cs_debug
> 10) {
1281 printf("validating initial pages of %s\n", vp
->v_name
);
1284 while (off
< alloc_size
&& ret
== LOAD_SUCCESS
) {
1285 tainted
= CS_VALIDATE_TAINTED
;
1287 valid
= cs_validate_range(vp
,
1291 MIN(PAGE_SIZE
, cmds_size
),
1293 if (!valid
|| (tainted
& CS_VALIDATE_TAINTED
)) {
1295 printf("CODE SIGNING: %s[%d]: invalid initial page at offset %lld validated:%d tainted:%d csflags:0x%x\n",
1296 vp
->v_name
, p
->p_pid
, (long long)(file_offset
+ off
), valid
, tainted
, result
->csflags
);
1298 if (cs_process_global_enforcement() ||
1299 (result
->csflags
& (CS_HARD
| CS_KILL
| CS_ENFORCEMENT
))) {
1302 result
->csflags
&= ~CS_VALID
;
1309 #if CONFIG_CODE_DECRYPTION
1310 case LC_ENCRYPTION_INFO
:
1311 case LC_ENCRYPTION_INFO_64
:
1315 ret
= set_code_unprotect(
1316 (struct encryption_info_command
*) lcp
,
1317 addr
, map
, slide
, vp
, file_offset
,
1318 header
->cputype
, header
->cpusubtype
);
1319 if (ret
!= LOAD_SUCCESS
) {
1320 os_reason_t load_failure_reason
= OS_REASON_NULL
;
1321 printf("proc %d: set_code_unprotect() error %d "
1322 "for file \"%s\"\n",
1323 p
->p_pid
, ret
, vp
->v_name
);
1325 * Don't let the app run if it's
1326 * encrypted but we failed to set up the
1327 * decrypter. If the keys are missing it will
1328 * return LOAD_DECRYPTFAIL.
1330 if (ret
== LOAD_DECRYPTFAIL
) {
1331 /* failed to load due to missing FP keys */
1333 p
->p_lflag
|= P_LTERM_DECRYPTFAIL
;
1336 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXITREASON_CREATE
) | DBG_FUNC_NONE
,
1337 p
->p_pid
, OS_REASON_EXEC
, EXEC_EXIT_REASON_FAIRPLAY_DECRYPT
, 0, 0);
1338 load_failure_reason
= os_reason_create(OS_REASON_EXEC
, EXEC_EXIT_REASON_FAIRPLAY_DECRYPT
);
1340 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXITREASON_CREATE
) | DBG_FUNC_NONE
,
1341 p
->p_pid
, OS_REASON_EXEC
, EXEC_EXIT_REASON_DECRYPT
, 0, 0);
1342 load_failure_reason
= os_reason_create(OS_REASON_EXEC
, EXEC_EXIT_REASON_DECRYPT
);
1346 * Don't signal the process if it was forked and in a partially constructed
1347 * state as part of a spawn -- it will just be torn down when the exec fails.
1350 assert(load_failure_reason
!= OS_REASON_NULL
);
1352 psignal_vfork_with_reason(p
, get_threadtask(imgp
->ip_new_thread
), imgp
->ip_new_thread
, SIGKILL
, load_failure_reason
);
1353 load_failure_reason
= OS_REASON_NULL
;
1355 psignal_with_reason(p
, SIGKILL
, load_failure_reason
);
1356 load_failure_reason
= OS_REASON_NULL
;
1359 os_reason_free(load_failure_reason
);
1360 load_failure_reason
= OS_REASON_NULL
;
1365 case LC_VERSION_MIN_IPHONEOS
:
1366 case LC_VERSION_MIN_MACOSX
:
1367 case LC_VERSION_MIN_WATCHOS
:
1368 case LC_VERSION_MIN_TVOS
: {
1369 struct version_min_command
*vmc
;
1371 if (depth
!= 1 || pass
!= 0) {
1374 vmc
= (struct version_min_command
*) lcp
;
1375 ret
= load_version(vmc
, &found_version_cmd
, imgp
->ip_flags
, result
);
1376 #if XNU_TARGET_OS_OSX
1377 if (ret
== LOAD_SUCCESS
) {
1378 if (result
->ip_platform
== PLATFORM_IOS
) {
1379 vm_map_mark_alien(map
);
1381 assert(!vm_map_is_alien(map
));
1384 #endif /* XNU_TARGET_OS_OSX */
1387 case LC_BUILD_VERSION
: {
1388 if (depth
!= 1 || pass
!= 0) {
1391 struct build_version_command
* bvc
= (struct build_version_command
*)lcp
;
1392 if (bvc
->cmdsize
< sizeof(*bvc
)) {
1393 ret
= LOAD_BADMACHO
;
1396 if (found_version_cmd
== TRUE
) {
1397 ret
= LOAD_BADMACHO
;
1400 result
->ip_platform
= bvc
->platform
;
1401 result
->lr_sdk
= bvc
->sdk
;
1402 result
->lr_min_sdk
= bvc
->minos
;
1403 found_version_cmd
= TRUE
;
1404 #if XNU_TARGET_OS_OSX
1405 if (result
->ip_platform
== PLATFORM_IOS
) {
1406 vm_map_mark_alien(map
);
1408 assert(!vm_map_is_alien(map
));
1410 #endif /* XNU_TARGET_OS_OSX */
1414 /* Other commands are ignored by the kernel */
1418 if (ret
!= LOAD_SUCCESS
) {
1422 if (ret
!= LOAD_SUCCESS
) {
1427 if (ret
== LOAD_SUCCESS
) {
1428 if (!got_code_signatures
&& cs_process_global_enforcement()) {
1432 /* Make sure if we need dyld, we got it */
1433 if (result
->needs_dynlinker
&& !dlp
) {
1437 if ((ret
== LOAD_SUCCESS
) && (dlp
!= 0)) {
1439 * load the dylinker, and slide it by the independent DYLD ASLR
1440 * offset regardless of the PIE-ness of the main binary.
1442 ret
= load_dylinker(dlp
, header
->cputype
, map
, thread
, depth
,
1443 dyld_aslr_offset
, result
, imgp
);
1447 if ((ret
== LOAD_SUCCESS
) && (depth
== 1)) {
1448 if (result
->thread_count
== 0) {
1451 #if CONFIG_ENFORCE_SIGNED_CODE
1452 if (result
->needs_dynlinker
&& !(result
->csflags
& CS_DYLD_PLATFORM
)) {
1459 if (ret
== LOAD_BADMACHO
&& found_xhdr
) {
1460 ret
= LOAD_BADMACHO_UPX
;
1463 kfree(addr
, alloc_size
);
1469 validate_potential_simulator_binary(
1470 cpu_type_t exectype __unused
,
1471 struct image_params
*imgp __unused
,
1472 off_t file_offset __unused
,
1473 off_t macho_size __unused
)
1476 /* Allow 32 bit exec only for simulator binaries */
1477 if (bootarg_no32exec
&& imgp
!= NULL
&& exectype
== CPU_TYPE_X86
) {
1478 if (imgp
->ip_simulator_binary
== IMGPF_SB_DEFAULT
) {
1479 boolean_t simulator_binary
= check_if_simulator_binary(imgp
, file_offset
, macho_size
);
1480 imgp
->ip_simulator_binary
= simulator_binary
? IMGPF_SB_TRUE
: IMGPF_SB_FALSE
;
1483 if (imgp
->ip_simulator_binary
!= IMGPF_SB_TRUE
) {
1484 return LOAD_BADARCH
;
1488 return LOAD_SUCCESS
;
1493 check_if_simulator_binary(
1494 struct image_params
*imgp
,
1498 struct mach_header
*header
;
1499 char *ip_vdata
= NULL
;
1500 kauth_cred_t cred
= NULL
;
1502 struct load_command
*lcp
;
1503 boolean_t simulator_binary
= FALSE
;
1505 vm_size_t alloc_size
, cmds_size
;
1507 proc_t p
= current_proc(); /* XXXX */
1510 size_t mach_header_sz
= sizeof(struct mach_header
);
1513 cred
= kauth_cred_proc_ref(p
);
1515 /* Allocate page to copyin mach header */
1516 ip_vdata
= kalloc(PAGE_SIZE
);
1517 bzero(ip_vdata
, PAGE_SIZE
);
1518 if (ip_vdata
== NULL
) {
1522 /* Read the Mach-O header */
1523 error
= vn_rdwr(UIO_READ
, imgp
->ip_vp
, ip_vdata
,
1524 PAGE_SIZE
, file_offset
,
1525 UIO_SYSSPACE
, (IO_UNIT
| IO_NODELOCKED
),
1531 header
= (struct mach_header
*)ip_vdata
;
1533 if (header
->magic
== MH_MAGIC_64
||
1534 header
->magic
== MH_CIGAM_64
) {
1535 mach_header_sz
= sizeof(struct mach_header_64
);
1538 /* ensure header + sizeofcmds falls within the file */
1539 if (os_add_overflow(mach_header_sz
, header
->sizeofcmds
, &cmds_size
) ||
1540 (off_t
)cmds_size
> macho_size
||
1541 round_page_overflow(cmds_size
, &alloc_size
) ||
1542 alloc_size
> INT_MAX
) {
1547 * Map the load commands into kernel memory.
1549 addr
= kalloc(alloc_size
);
1554 error
= vn_rdwr(UIO_READ
, imgp
->ip_vp
, addr
, (int)alloc_size
, file_offset
,
1555 UIO_SYSSPACE
, IO_NODELOCKED
, cred
, &resid
, p
);
1561 /* We must be able to read in as much as the mach_header indicated */
1566 * Loop through each of the load_commands indicated by the
1567 * Mach-O header; if an absurd value is provided, we just
1568 * run off the end of the reserved section by incrementing
1569 * the offset too far, so we are implicitly fail-safe.
1571 offset
= mach_header_sz
;
1572 ncmds
= header
->ncmds
;
1575 /* ensure enough space for a minimal load command */
1576 if (offset
+ sizeof(struct load_command
) > cmds_size
) {
1581 * Get a pointer to the command.
1583 lcp
= (struct load_command
*)(addr
+ offset
);
1586 * Perform prevalidation of the struct load_command
1587 * before we attempt to use its contents. Invalid
1588 * values are ones which result in an overflow, or
1589 * which can not possibly be valid commands, or which
1590 * straddle or exist past the reserved section at the
1591 * start of the image.
1593 if (os_add_overflow(offset
, lcp
->cmdsize
, &offset
) ||
1594 lcp
->cmdsize
< sizeof(struct load_command
) ||
1595 offset
> cmds_size
) {
1599 /* Check if its a simulator binary. */
1601 case LC_VERSION_MIN_WATCHOS
:
1602 simulator_binary
= TRUE
;
1605 case LC_BUILD_VERSION
: {
1606 struct build_version_command
*bvc
;
1608 bvc
= (struct build_version_command
*) lcp
;
1609 if (bvc
->cmdsize
< sizeof(*bvc
)) {
1610 /* unsafe to use this command struct if cmdsize
1611 * validated above is too small for it to fit */
1614 if (bvc
->platform
== PLATFORM_IOSSIMULATOR
||
1615 bvc
->platform
== PLATFORM_WATCHOSSIMULATOR
) {
1616 simulator_binary
= TRUE
;
1622 case LC_VERSION_MIN_IPHONEOS
: {
1623 simulator_binary
= TRUE
;
1628 /* ignore other load commands */
1632 if (simulator_binary
== TRUE
) {
1639 kfree(ip_vdata
, PAGE_SIZE
);
1643 kauth_cred_unref(&cred
);
1647 kfree(addr
, alloc_size
);
1650 return simulator_binary
;
1652 #endif /* __x86_64__ */
1654 #if CONFIG_CODE_DECRYPTION
1656 #define APPLE_UNPROTECTED_HEADER_SIZE (3 * 4096)
1658 static load_return_t
1659 unprotect_dsmos_segment(
1665 vm_map_offset_t map_addr
,
1666 vm_map_size_t map_size
)
1672 * The first APPLE_UNPROTECTED_HEADER_SIZE bytes (from offset 0 of
1673 * this part of a Universal binary) are not protected...
1674 * The rest needs to be "transformed".
1676 slice_off
= file_off
- macho_offset
;
1677 if (slice_off
<= APPLE_UNPROTECTED_HEADER_SIZE
&&
1678 slice_off
+ file_size
<= APPLE_UNPROTECTED_HEADER_SIZE
) {
1679 /* it's all unprotected, nothing to do... */
1682 if (slice_off
<= APPLE_UNPROTECTED_HEADER_SIZE
) {
1684 * We start mapping in the unprotected area.
1685 * Skip the unprotected part...
1687 uint64_t delta_file
;
1688 vm_map_offset_t delta_map
;
1690 delta_file
= (uint64_t)APPLE_UNPROTECTED_HEADER_SIZE
;
1691 delta_file
-= slice_off
;
1692 if (os_convert_overflow(delta_file
, &delta_map
)) {
1693 return LOAD_BADMACHO
;
1695 if (os_add_overflow(map_addr
, delta_map
, &map_addr
)) {
1696 return LOAD_BADMACHO
;
1698 if (os_sub_overflow(map_size
, delta_map
, &map_size
)) {
1699 return LOAD_BADMACHO
;
1702 /* ... transform the rest of the mapping. */
1703 struct pager_crypt_info crypt_info
;
1704 crypt_info
.page_decrypt
= dsmos_page_transform
;
1705 crypt_info
.crypt_ops
= NULL
;
1706 crypt_info
.crypt_end
= NULL
;
1707 #pragma unused(vp, macho_offset)
1708 crypt_info
.crypt_ops
= (void *)0x2e69cf40;
1709 vm_map_offset_t crypto_backing_offset
;
1710 crypto_backing_offset
= -1; /* i.e. use map entry's offset */
1711 #if VM_MAP_DEBUG_APPLE_PROTECT
1712 if (vm_map_debug_apple_protect
) {
1715 printf("APPLE_PROTECT: %d[%s] map %p "
1716 "[0x%llx:0x%llx] %s(%s)\n",
1717 p
->p_pid
, p
->p_comm
, map
,
1718 (uint64_t) map_addr
,
1719 (uint64_t) (map_addr
+ map_size
),
1720 __FUNCTION__
, vp
->v_name
);
1722 #endif /* VM_MAP_DEBUG_APPLE_PROTECT */
1724 /* The DSMOS pager can only be used by apple signed code */
1725 struct cs_blob
* blob
= csvnode_get_blob(vp
, file_off
);
1726 if (blob
== NULL
|| !blob
->csb_platform_binary
|| blob
->csb_platform_path
) {
1727 return LOAD_FAILURE
;
1730 kr
= vm_map_apple_protected(map
,
1732 map_addr
+ map_size
,
1733 crypto_backing_offset
,
1735 CRYPTID_APP_ENCRYPTION
);
1738 if (kr
!= KERN_SUCCESS
) {
1739 return LOAD_FAILURE
;
1741 return LOAD_SUCCESS
;
1743 #else /* CONFIG_CODE_DECRYPTION */
1744 static load_return_t
1745 unprotect_dsmos_segment(
1746 __unused
uint64_t file_off
,
1747 __unused
uint64_t file_size
,
1748 __unused
struct vnode
*vp
,
1749 __unused off_t macho_offset
,
1750 __unused vm_map_t map
,
1751 __unused vm_map_offset_t map_addr
,
1752 __unused vm_map_size_t map_size
)
1754 return LOAD_SUCCESS
;
1756 #endif /* CONFIG_CODE_DECRYPTION */
1761 * Maps a Mach-O segment, taking care of mis-alignment (wrt the system
1762 * page size) issues.
1764 * The mapping might result in 1, 2 or 3 map entries:
1765 * 1. for the first page, which could be overlap with the previous
1767 * 2. for the center (if applicable),
1768 * 3. for the last page, which could overlap with the next mapping.
1770 * For each of those map entries, we might have to interpose a
1771 * "fourk_pager" to deal with mis-alignment wrt the system page size,
1772 * either in the mapping address and/or size or the file offset and/or
1774 * The "fourk_pager" itself would be mapped with proper alignment
1775 * wrt the system page size and would then be populated with the
1776 * information about the intended mapping, with a "4KB" granularity.
1778 static kern_return_t
1781 vm_map_offset_t vm_start
,
1782 vm_map_offset_t vm_end
,
1783 memory_object_control_t control
,
1784 vm_map_offset_t file_start
,
1785 vm_map_offset_t file_end
,
1788 load_result_t
*result
)
1790 vm_map_offset_t cur_offset
, cur_start
, cur_end
;
1792 vm_map_offset_t effective_page_mask
;
1793 vm_map_kernel_flags_t vmk_flags
, cur_vmk_flags
;
1795 if (vm_end
< vm_start
||
1796 file_end
< file_start
) {
1797 return LOAD_BADMACHO
;
1799 if (vm_end
== vm_start
||
1800 file_end
== file_start
) {
1801 /* nothing to map... */
1802 return LOAD_SUCCESS
;
1805 effective_page_mask
= vm_map_page_mask(map
);
1807 vmk_flags
= VM_MAP_KERNEL_FLAGS_NONE
;
1808 if (vm_map_page_aligned(vm_start
, effective_page_mask
) &&
1809 vm_map_page_aligned(vm_end
, effective_page_mask
) &&
1810 vm_map_page_aligned(file_start
, effective_page_mask
) &&
1811 vm_map_page_aligned(file_end
, effective_page_mask
)) {
1812 /* all page-aligned and map-aligned: proceed */
1815 /* use an intermediate "4K" pager */
1816 vmk_flags
.vmkf_fourk
= TRUE
;
1817 #else /* __arm64__ */
1818 panic("map_segment: unexpected mis-alignment "
1819 "vm[0x%llx:0x%llx] file[0x%llx:0x%llx]\n",
1820 (uint64_t) vm_start
,
1822 (uint64_t) file_start
,
1823 (uint64_t) file_end
);
1824 #endif /* __arm64__ */
1828 cur_start
= vm_start
;
1831 if (!vm_map_page_aligned(vm_start
, effective_page_mask
)) {
1832 /* one 4K pager for the 1st page */
1833 cur_end
= vm_map_round_page(cur_start
, effective_page_mask
);
1834 if (cur_end
> vm_end
) {
1835 cur_end
= vm_start
+ (file_end
- file_start
);
1837 if (control
!= MEMORY_OBJECT_CONTROL_NULL
) {
1838 /* no copy-on-read for mapped binaries */
1839 vmk_flags
.vmkf_no_copy_on_read
= 1;
1840 ret
= vm_map_enter_mem_object_control(
1843 cur_end
- cur_start
,
1844 (mach_vm_offset_t
)0,
1847 VM_KERN_MEMORY_NONE
,
1849 file_start
+ cur_offset
,
1852 VM_INHERIT_DEFAULT
);
1854 ret
= vm_map_enter_mem_object(
1857 cur_end
- cur_start
,
1858 (mach_vm_offset_t
)0,
1861 VM_KERN_MEMORY_NONE
,
1866 VM_INHERIT_DEFAULT
);
1868 if (ret
!= KERN_SUCCESS
) {
1869 return LOAD_NOSPACE
;
1871 cur_offset
+= cur_end
- cur_start
;
1873 #endif /* __arm64__ */
1874 if (cur_end
>= vm_start
+ (file_end
- file_start
)) {
1875 /* all mapped: done */
1878 if (vm_map_round_page(cur_end
, effective_page_mask
) >=
1879 vm_map_trunc_page(vm_start
+ (file_end
- file_start
),
1880 effective_page_mask
)) {
1883 cur_start
= cur_end
;
1884 if ((vm_start
& effective_page_mask
) !=
1885 (file_start
& effective_page_mask
)) {
1886 /* one 4K pager for the middle */
1887 cur_vmk_flags
= vmk_flags
;
1889 /* regular mapping for the middle */
1890 cur_vmk_flags
= VM_MAP_KERNEL_FLAGS_NONE
;
1893 #if !defined(XNU_TARGET_OS_OSX)
1895 #else /* !defined(XNU_TARGET_OS_OSX) */
1897 * This process doesn't have its new csflags (from
1898 * the image being loaded) yet, so tell VM to override the
1899 * current process's CS_ENFORCEMENT for this mapping.
1901 if (result
->csflags
& CS_ENFORCEMENT
) {
1902 cur_vmk_flags
.vmkf_cs_enforcement
= TRUE
;
1904 cur_vmk_flags
.vmkf_cs_enforcement
= FALSE
;
1906 cur_vmk_flags
.vmkf_cs_enforcement_override
= TRUE
;
1907 #endif /* !defined(XNU_TARGET_OS_OSX) */
1909 if (result
->is_cambria
&& (initprot
& VM_PROT_EXECUTE
) == VM_PROT_EXECUTE
) {
1910 cur_vmk_flags
.vmkf_translated_allow_execute
= TRUE
;
1913 cur_end
= vm_map_trunc_page(vm_start
+ (file_end
-
1915 effective_page_mask
);
1916 if (control
!= MEMORY_OBJECT_CONTROL_NULL
) {
1917 /* no copy-on-read for mapped binaries */
1918 cur_vmk_flags
.vmkf_no_copy_on_read
= 1;
1919 ret
= vm_map_enter_mem_object_control(
1922 cur_end
- cur_start
,
1923 (mach_vm_offset_t
)0,
1926 VM_KERN_MEMORY_NONE
,
1928 file_start
+ cur_offset
,
1931 VM_INHERIT_DEFAULT
);
1933 ret
= vm_map_enter_mem_object(
1936 cur_end
- cur_start
,
1937 (mach_vm_offset_t
)0,
1940 VM_KERN_MEMORY_NONE
,
1945 VM_INHERIT_DEFAULT
);
1947 if (ret
!= KERN_SUCCESS
) {
1948 return LOAD_NOSPACE
;
1950 cur_offset
+= cur_end
- cur_start
;
1952 if (cur_end
>= vm_start
+ (file_end
- file_start
)) {
1953 /* all mapped: done */
1956 cur_start
= cur_end
;
1958 if (!vm_map_page_aligned(vm_start
+ (file_end
- file_start
),
1959 effective_page_mask
)) {
1960 /* one 4K pager for the last page */
1961 cur_end
= vm_start
+ (file_end
- file_start
);
1962 if (control
!= MEMORY_OBJECT_CONTROL_NULL
) {
1963 /* no copy-on-read for mapped binaries */
1964 vmk_flags
.vmkf_no_copy_on_read
= 1;
1965 ret
= vm_map_enter_mem_object_control(
1968 cur_end
- cur_start
,
1969 (mach_vm_offset_t
)0,
1972 VM_KERN_MEMORY_NONE
,
1974 file_start
+ cur_offset
,
1977 VM_INHERIT_DEFAULT
);
1979 ret
= vm_map_enter_mem_object(
1982 cur_end
- cur_start
,
1983 (mach_vm_offset_t
)0,
1986 VM_KERN_MEMORY_NONE
,
1991 VM_INHERIT_DEFAULT
);
1993 if (ret
!= KERN_SUCCESS
) {
1994 return LOAD_NOSPACE
;
1996 cur_offset
+= cur_end
- cur_start
;
1998 #endif /* __arm64__ */
2000 assert(cur_end
>= vm_start
+ (file_end
- file_start
));
2001 return LOAD_SUCCESS
;
2007 struct load_command
*lcp
,
2015 load_result_t
*result
,
2016 struct image_params
*imgp
)
2018 struct segment_command_64 segment_command
, *scp
;
2020 vm_map_size_t delta_size
;
2023 size_t segment_command_size
, total_section_size
,
2024 single_section_size
;
2025 uint64_t file_offset
, file_size
;
2026 vm_map_offset_t vm_offset
;
2028 vm_map_offset_t vm_start
, vm_end
, vm_end_aligned
;
2029 vm_map_offset_t file_start
, file_end
;
2032 vm_map_size_t effective_page_size
;
2033 vm_map_offset_t effective_page_mask
;
2035 vm_map_kernel_flags_t vmk_flags
;
2036 boolean_t fourk_align
;
2037 #endif /* __arm64__ */
2041 effective_page_size
= vm_map_page_size(map
);
2042 effective_page_mask
= vm_map_page_mask(map
);
2045 if (LC_SEGMENT_64
== lcp
->cmd
) {
2046 segment_command_size
= sizeof(struct segment_command_64
);
2047 single_section_size
= sizeof(struct section_64
);
2049 /* 64-bit binary: should already be 16K-aligned */
2050 fourk_align
= FALSE
;
2052 if (vm_map_page_shift(map
) == FOURK_PAGE_SHIFT
&&
2053 PAGE_SHIFT
!= FOURK_PAGE_SHIFT
) {
2057 #endif /* __arm64__ */
2059 segment_command_size
= sizeof(struct segment_command
);
2060 single_section_size
= sizeof(struct section
);
2062 /* 32-bit binary: might need 4K-alignment */
2063 if (effective_page_size
!= FOURK_PAGE_SIZE
) {
2064 /* not using 4K page size: need fourk_pager */
2068 /* using 4K page size: no need for re-alignment */
2069 fourk_align
= FALSE
;
2071 #endif /* __arm64__ */
2073 if (lcp
->cmdsize
< segment_command_size
) {
2074 DEBUG4K_ERROR("LOAD_BADMACHO cmdsize %d < %zu\n", lcp
->cmdsize
, segment_command_size
);
2075 return LOAD_BADMACHO
;
2077 total_section_size
= lcp
->cmdsize
- segment_command_size
;
2079 if (LC_SEGMENT_64
== lcp
->cmd
) {
2080 scp
= (struct segment_command_64
*)lcp
;
2082 scp
= &segment_command
;
2083 widen_segment_command((struct segment_command
*)lcp
, scp
);
2087 MACHO_PRINTF(("+++ load_segment %s "
2088 "vm[0x%llx:0x%llx] file[0x%llx:0x%llx] "
2089 "prot %d/%d flags 0x%x\n",
2091 (uint64_t)(slide
+ scp
->vmaddr
),
2092 (uint64_t)(slide
+ scp
->vmaddr
+ scp
->vmsize
),
2093 pager_offset
+ scp
->fileoff
,
2094 pager_offset
+ scp
->fileoff
+ scp
->filesize
,
2101 * Make sure what we get from the file is really ours (as specified
2104 if (scp
->fileoff
+ scp
->filesize
< scp
->fileoff
||
2105 scp
->fileoff
+ scp
->filesize
> (uint64_t)macho_size
) {
2106 DEBUG4K_ERROR("LOAD_BADMACHO fileoff 0x%llx filesize 0x%llx macho_size 0x%llx\n", scp
->fileoff
, scp
->filesize
, (uint64_t)macho_size
);
2107 return LOAD_BADMACHO
;
2110 * Ensure that the number of sections specified would fit
2111 * within the load command size.
2113 if (total_section_size
/ single_section_size
< scp
->nsects
) {
2114 DEBUG4K_ERROR("LOAD_BADMACHO 0x%zx 0x%zx %d\n", total_section_size
, single_section_size
, scp
->nsects
);
2115 return LOAD_BADMACHO
;
2118 * Make sure the segment is page-aligned in the file.
2120 if (os_add_overflow(pager_offset
, scp
->fileoff
, &file_offset
)) {
2121 DEBUG4K_ERROR("LOAD_BADMACHO file_offset: 0x%llx + 0x%llx\n", pager_offset
, scp
->fileoff
);
2122 return LOAD_BADMACHO
;
2124 file_size
= scp
->filesize
;
2127 if ((file_offset
& FOURK_PAGE_MASK
) != 0) {
2129 * we can't mmap() it if it's not at least 4KB-aligned
2132 DEBUG4K_ERROR("LOAD_BADMACHO file_offset 0x%llx\n", file_offset
);
2133 return LOAD_BADMACHO
;
2136 #endif /* __arm64__ */
2137 if ((file_offset
& PAGE_MASK_64
) != 0 ||
2138 /* we can't mmap() it if it's not page-aligned in the file */
2139 (file_offset
& vm_map_page_mask(map
)) != 0) {
2141 * The 1st test would have failed if the system's page size
2142 * was what this process believe is the page size, so let's
2143 * fail here too for the sake of consistency.
2145 DEBUG4K_ERROR("LOAD_BADMACHO file_offset 0x%llx\n", file_offset
);
2146 return LOAD_BADMACHO
;
2150 * If we have a code signature attached for this slice
2151 * require that the segments are within the signed part
2154 if (result
->cs_end_offset
&&
2155 result
->cs_end_offset
< (off_t
)scp
->fileoff
&&
2156 result
->cs_end_offset
- scp
->fileoff
< scp
->filesize
) {
2158 printf("section outside code signature\n");
2160 DEBUG4K_ERROR("LOAD_BADMACHO end_offset 0x%llx fileoff 0x%llx filesize 0x%llx\n", result
->cs_end_offset
, scp
->fileoff
, scp
->filesize
);
2161 return LOAD_BADMACHO
;
2164 if (os_add_overflow(scp
->vmaddr
, slide
, &vm_offset
)) {
2166 printf("vmaddr too large\n");
2168 DEBUG4K_ERROR("LOAD_BADMACHO vmaddr 0x%llx slide 0x%llx vm_offset 0x%llx\n", scp
->vmaddr
, slide
, (uint64_t)vm_offset
);
2169 return LOAD_BADMACHO
;
2172 if (scp
->vmsize
> SIZE_MAX
) {
2173 DEBUG4K_ERROR("LOAD_BADMACHO vmsize 0x%llx\n", scp
->vmsize
);
2174 return LOAD_BADMACHO
;
2177 vm_size
= (size_t)scp
->vmsize
;
2180 return LOAD_SUCCESS
;
2182 if (scp
->vmaddr
== 0 &&
2185 (scp
->initprot
& VM_PROT_ALL
) == VM_PROT_NONE
&&
2186 (scp
->maxprot
& VM_PROT_ALL
) == VM_PROT_NONE
) {
2187 if (map
== VM_MAP_NULL
) {
2188 return LOAD_SUCCESS
;
2192 * For PIE, extend page zero rather than moving it. Extending
2193 * page zero keeps early allocations from falling predictably
2194 * between the end of page zero and the beginning of the first
2198 * This is a "page zero" segment: it starts at address 0,
2199 * is not mapped from the binary file and is not accessible.
2200 * User-space should never be able to access that memory, so
2201 * make it completely off limits by raising the VM map's
2204 vm_end
= (vm_map_offset_t
)(vm_offset
+ vm_size
);
2205 if (vm_end
< vm_offset
) {
2206 DEBUG4K_ERROR("LOAD_BADMACHO vm_end 0x%llx vm_offset 0x%llx vm_size 0x%llx\n", (uint64_t)vm_end
, (uint64_t)vm_offset
, (uint64_t)vm_size
);
2207 return LOAD_BADMACHO
;
2211 MACHO_PRINTF(("++++++ load_segment: "
2212 "page_zero up to 0x%llx\n",
2213 (uint64_t) vm_end
));
2217 /* raise min_offset as much as page-alignment allows */
2218 vm_end_aligned
= vm_map_trunc_page(vm_end
,
2219 effective_page_mask
);
2221 #endif /* __arm64__ */
2223 vm_end
= vm_map_round_page(vm_end
,
2225 vm_end_aligned
= vm_end
;
2227 ret
= vm_map_raise_min_offset(map
,
2231 vm_end
> vm_end_aligned
) {
2232 /* use fourk_pager to map the rest of pagezero */
2233 assert(fourk_align
);
2234 vmk_flags
= VM_MAP_KERNEL_FLAGS_NONE
;
2235 vmk_flags
.vmkf_fourk
= TRUE
;
2236 ret
= vm_map_enter_mem_object(
2239 vm_end
- vm_end_aligned
,
2240 (mach_vm_offset_t
) 0, /* mask */
2243 VM_KERN_MEMORY_NONE
,
2247 (scp
->initprot
& VM_PROT_ALL
),
2248 (scp
->maxprot
& VM_PROT_ALL
),
2249 VM_INHERIT_DEFAULT
);
2251 #endif /* __arm64__ */
2253 if (ret
!= KERN_SUCCESS
) {
2254 DEBUG4K_ERROR("LOAD_FAILURE ret 0x%x\n", ret
);
2255 return LOAD_FAILURE
;
2257 return LOAD_SUCCESS
;
2259 #if !defined(XNU_TARGET_OS_OSX)
2260 /* not PAGEZERO: should not be mapped at address 0 */
2261 if (filetype
!= MH_DYLINKER
&& scp
->vmaddr
== 0) {
2262 DEBUG4K_ERROR("LOAD_BADMACHO filetype %d vmaddr 0x%llx\n", filetype
, scp
->vmaddr
);
2263 return LOAD_BADMACHO
;
2265 #endif /* !defined(XNU_TARGET_OS_OSX) */
2271 file_start
= vm_map_trunc_page(file_offset
,
2273 file_end
= vm_map_round_page(file_offset
+ file_size
,
2275 vm_start
= vm_map_trunc_page(vm_offset
,
2277 vm_end
= vm_map_round_page(vm_offset
+ vm_size
,
2280 if (file_offset
- file_start
> FOURK_PAGE_MASK
||
2281 file_end
- file_offset
- file_size
> FOURK_PAGE_MASK
) {
2282 DEBUG4K_ERROR("LOAD_BADMACHO file_start / file_size wrap "
2283 "[0x%llx:0x%llx] -> [0x%llx:0x%llx]\n",
2285 file_offset
+ file_size
,
2286 (uint64_t) file_start
,
2287 (uint64_t) file_end
);
2288 return LOAD_BADMACHO
;
2291 if (!strncmp(scp
->segname
, "__LINKEDIT", 11) &&
2292 page_aligned(file_start
) &&
2293 vm_map_page_aligned(file_start
, vm_map_page_mask(map
)) &&
2294 page_aligned(vm_start
) &&
2295 vm_map_page_aligned(vm_start
, vm_map_page_mask(map
))) {
2296 /* XXX last segment: ignore mis-aligned tail */
2297 file_end
= vm_map_round_page(file_end
,
2298 effective_page_mask
);
2299 vm_end
= vm_map_round_page(vm_end
,
2300 effective_page_mask
);
2303 #endif /* __arm64__ */
2305 file_start
= vm_map_trunc_page(file_offset
,
2306 effective_page_mask
);
2307 file_end
= vm_map_round_page(file_offset
+ file_size
,
2308 effective_page_mask
);
2309 vm_start
= vm_map_trunc_page(vm_offset
,
2310 effective_page_mask
);
2311 vm_end
= vm_map_round_page(vm_offset
+ vm_size
,
2312 effective_page_mask
);
2314 if (file_offset
- file_start
> effective_page_mask
||
2315 file_end
- file_offset
- file_size
> effective_page_mask
) {
2316 DEBUG4K_ERROR("LOAD_BADMACHO file_start / file_size wrap "
2317 "[0x%llx:0x%llx] -> [0x%llx:0x%llx]\n",
2319 file_offset
+ file_size
,
2320 (uint64_t) file_start
,
2321 (uint64_t) file_end
);
2322 return LOAD_BADMACHO
;
2326 if (vm_start
< result
->min_vm_addr
) {
2327 result
->min_vm_addr
= vm_start
;
2329 if (vm_end
> result
->max_vm_addr
) {
2330 result
->max_vm_addr
= vm_end
;
2333 if (map
== VM_MAP_NULL
) {
2334 return LOAD_SUCCESS
;
2338 initprot
= (scp
->initprot
) & VM_PROT_ALL
;
2339 maxprot
= (scp
->maxprot
) & VM_PROT_ALL
;
2341 * Map a copy of the file into the address space.
2344 MACHO_PRINTF(("++++++ load_segment: "
2345 "mapping at vm [0x%llx:0x%llx] of "
2346 "file [0x%llx:0x%llx]\n",
2347 (uint64_t) vm_start
,
2349 (uint64_t) file_start
,
2350 (uint64_t) file_end
));
2352 ret
= map_segment(map
,
2362 DEBUG4K_ERROR("LOAD_NOSPACE start 0x%llx end 0x%llx ret 0x%x\n", (uint64_t)vm_start
, (uint64_t)vm_end
, ret
);
2363 return LOAD_NOSPACE
;
2368 * If the file didn't end on a page boundary,
2369 * we need to zero the leftover.
2371 delta_size
= map_size
- scp
->filesize
;
2372 if (delta_size
> 0) {
2373 mach_vm_offset_t tmp
;
2375 ret
= mach_vm_allocate_kernel(kernel_map
, &tmp
, delta_size
, VM_FLAGS_ANYWHERE
, VM_KERN_MEMORY_BSD
);
2376 if (ret
!= KERN_SUCCESS
) {
2377 DEBUG4K_ERROR("LOAD_RESOURCE delta_size 0x%llx ret 0x%x\n", delta_size
, ret
);
2378 return LOAD_RESOURCE
;
2381 if (copyout(tmp
, map_addr
+ scp
->filesize
,
2383 (void) mach_vm_deallocate(
2384 kernel_map
, tmp
, delta_size
);
2385 DEBUG4K_ERROR("LOAD_FAILURE copyout 0x%llx 0x%llx\n", map_addr
+ scp
->filesize
, delta_size
);
2386 return LOAD_FAILURE
;
2389 (void) mach_vm_deallocate(kernel_map
, tmp
, delta_size
);
2395 * If the virtual size of the segment is greater
2396 * than the size from the file, we need to allocate
2397 * zero fill memory for the rest.
2399 if ((vm_end
- vm_start
) > (file_end
- file_start
)) {
2400 delta_size
= (vm_end
- vm_start
) - (file_end
- file_start
);
2404 if (delta_size
> 0) {
2405 vm_map_offset_t tmp_start
;
2406 vm_map_offset_t tmp_end
;
2408 if (os_add_overflow(vm_start
, file_end
- file_start
, &tmp_start
)) {
2409 DEBUG4K_ERROR("LOAD_NOSPACE tmp_start: 0x%llx + 0x%llx\n", (uint64_t)vm_start
, (uint64_t)(file_end
- file_start
));
2410 return LOAD_NOSPACE
;
2413 if (os_add_overflow(tmp_start
, delta_size
, &tmp_end
)) {
2414 DEBUG4K_ERROR("LOAD_NOSPACE tmp_end: 0x%llx + 0x%llx\n", (uint64_t)tmp_start
, (uint64_t)delta_size
);
2415 return LOAD_NOSPACE
;
2419 MACHO_PRINTF(("++++++ load_segment: "
2420 "delta mapping vm [0x%llx:0x%llx]\n",
2421 (uint64_t) tmp_start
,
2422 (uint64_t) tmp_end
));
2424 kr
= map_segment(map
,
2427 MEMORY_OBJECT_CONTROL_NULL
,
2433 if (kr
!= KERN_SUCCESS
) {
2434 DEBUG4K_ERROR("LOAD_NOSPACE 0x%llx 0x%llx kr 0x%x\n", (unsigned long long)tmp_start
, (uint64_t)delta_size
, kr
);
2435 return LOAD_NOSPACE
;
2439 if ((scp
->fileoff
== 0) && (scp
->filesize
!= 0)) {
2440 result
->mach_header
= vm_offset
;
2443 if (scp
->flags
& SG_PROTECTED_VERSION_1
) {
2444 ret
= unprotect_dsmos_segment(file_start
,
2445 file_end
- file_start
,
2451 if (ret
!= LOAD_SUCCESS
) {
2452 DEBUG4K_ERROR("unprotect 0x%llx 0x%llx ret %d \n", (uint64_t)vm_start
, (uint64_t)vm_end
, ret
);
2459 if (LOAD_SUCCESS
== ret
&&
2460 filetype
== MH_DYLINKER
&&
2461 result
->all_image_info_addr
== MACH_VM_MIN_ADDRESS
) {
2462 note_all_image_info_section(scp
,
2463 LC_SEGMENT_64
== lcp
->cmd
,
2464 single_section_size
,
2465 ((const char *)lcp
+
2466 segment_command_size
),
2471 if (result
->entry_point
!= MACH_VM_MIN_ADDRESS
) {
2472 if ((result
->entry_point
>= vm_offset
) && (result
->entry_point
< (vm_offset
+ vm_size
))) {
2473 if ((scp
->initprot
& (VM_PROT_READ
| VM_PROT_EXECUTE
)) == (VM_PROT_READ
| VM_PROT_EXECUTE
)) {
2474 result
->validentry
= 1;
2476 /* right range but wrong protections, unset if previously validated */
2477 result
->validentry
= 0;
2482 if (ret
!= LOAD_SUCCESS
&& verbose
) {
2483 DEBUG4K_ERROR("ret %d\n", ret
);
2491 struct uuid_command
*uulp
,
2493 load_result_t
*result
2497 * We need to check the following for this command:
2498 * - The command size should be atleast the size of struct uuid_command
2499 * - The UUID part of the command should be completely within the mach-o header
2502 if ((uulp
->cmdsize
< sizeof(struct uuid_command
)) ||
2503 (((char *)uulp
+ sizeof(struct uuid_command
)) > command_end
)) {
2504 return LOAD_BADMACHO
;
2507 memcpy(&result
->uuid
[0], &uulp
->uuid
[0], sizeof(result
->uuid
));
2508 return LOAD_SUCCESS
;
2514 struct version_min_command
*vmc
,
2515 boolean_t
*found_version_cmd
,
2516 int ip_flags __unused
,
2517 load_result_t
*result
2520 uint32_t platform
= 0;
2524 if (vmc
->cmdsize
< sizeof(*vmc
)) {
2525 return LOAD_BADMACHO
;
2527 if (*found_version_cmd
== TRUE
) {
2528 return LOAD_BADMACHO
;
2530 *found_version_cmd
= TRUE
;
2532 min_sdk
= vmc
->version
;
2534 case LC_VERSION_MIN_MACOSX
:
2535 platform
= PLATFORM_MACOS
;
2537 #if __x86_64__ /* __x86_64__ */
2538 case LC_VERSION_MIN_IPHONEOS
:
2539 platform
= PLATFORM_IOSSIMULATOR
;
2541 case LC_VERSION_MIN_WATCHOS
:
2542 platform
= PLATFORM_WATCHOSSIMULATOR
;
2544 case LC_VERSION_MIN_TVOS
:
2545 platform
= PLATFORM_TVOSSIMULATOR
;
2548 case LC_VERSION_MIN_IPHONEOS
: {
2550 extern int legacy_footprint_entitlement_mode
;
2551 if (vmc
->sdk
< (12 << 16)) {
2552 /* app built with a pre-iOS12 SDK: apply legacy footprint mitigation */
2553 result
->legacy_footprint
= TRUE
;
2555 #endif /* __arm64__ */
2556 platform
= PLATFORM_IOS
;
2559 case LC_VERSION_MIN_WATCHOS
:
2560 platform
= PLATFORM_WATCHOS
;
2562 case LC_VERSION_MIN_TVOS
:
2563 platform
= PLATFORM_TVOS
;
2565 #endif /* __x86_64__ */
2566 /* All LC_VERSION_MIN_* load commands are legacy and we will not be adding any more */
2569 min_sdk
= (uint32_t)-1;
2570 __builtin_unreachable();
2572 result
->ip_platform
= platform
;
2573 result
->lr_min_sdk
= min_sdk
;
2574 result
->lr_sdk
= sdk
;
2575 return LOAD_SUCCESS
;
2581 struct entry_point_command
*epc
,
2584 load_result_t
*result
2587 mach_vm_offset_t addr
;
2590 if (epc
->cmdsize
< sizeof(*epc
)) {
2591 return LOAD_BADMACHO
;
2593 if (result
->thread_count
!= 0) {
2594 return LOAD_FAILURE
;
2597 if (thread
== THREAD_NULL
) {
2598 return LOAD_SUCCESS
;
2602 * LC_MAIN specifies stack size but not location.
2603 * Add guard page to allocation size (MAXSSIZ includes guard page).
2605 if (epc
->stacksize
) {
2606 if (os_add_overflow(epc
->stacksize
, 4 * PAGE_SIZE
, &result
->user_stack_size
)) {
2608 * We are going to immediately throw away this result, but we want
2609 * to make sure we aren't loading a dangerously close to
2610 * overflowing value, since this will have a guard page added to it
2611 * and be rounded to page boundaries
2613 return LOAD_BADMACHO
;
2615 result
->user_stack_size
= epc
->stacksize
;
2616 if (os_add_overflow(epc
->stacksize
, PAGE_SIZE
, &result
->user_stack_alloc_size
)) {
2617 return LOAD_BADMACHO
;
2619 result
->custom_stack
= TRUE
;
2621 result
->user_stack_alloc_size
= MAXSSIZ
;
2624 /* use default location for stack */
2625 ret
= thread_userstackdefault(&addr
, result
->is_64bit_addr
);
2626 if (ret
!= KERN_SUCCESS
) {
2627 return LOAD_FAILURE
;
2630 /* The stack slides down from the default location */
2631 result
->user_stack
= (user_addr_t
)mach_vm_trunc_page((user_addr_t
)addr
- slide
);
2633 if (result
->using_lcmain
|| result
->entry_point
!= MACH_VM_MIN_ADDRESS
) {
2634 /* Already processed LC_MAIN or LC_UNIXTHREAD */
2635 return LOAD_FAILURE
;
2638 /* kernel does *not* use entryoff from LC_MAIN. Dyld uses it. */
2639 result
->needs_dynlinker
= TRUE
;
2640 result
->using_lcmain
= TRUE
;
2642 ret
= thread_state_initialize( thread
);
2643 if (ret
!= KERN_SUCCESS
) {
2644 return LOAD_FAILURE
;
2647 result
->unixproc
= TRUE
;
2648 result
->thread_count
++;
2650 return LOAD_SUCCESS
;
2658 load_result_t
*result
2661 mach_vm_offset_t addr
;
2664 /* Driver binaries have no LC_MAIN, use defaults */
2666 if (thread
== THREAD_NULL
) {
2667 return LOAD_SUCCESS
;
2670 result
->user_stack_alloc_size
= MAXSSIZ
;
2672 /* use default location for stack */
2673 ret
= thread_userstackdefault(&addr
, result
->is_64bit_addr
);
2674 if (ret
!= KERN_SUCCESS
) {
2675 return LOAD_FAILURE
;
2678 /* The stack slides down from the default location */
2679 result
->user_stack
= (user_addr_t
)addr
;
2680 result
->user_stack
-= slide
;
2682 if (result
->using_lcmain
|| result
->entry_point
!= MACH_VM_MIN_ADDRESS
) {
2683 /* Already processed LC_MAIN or LC_UNIXTHREAD */
2684 return LOAD_FAILURE
;
2687 result
->needs_dynlinker
= TRUE
;
2689 ret
= thread_state_initialize( thread
);
2690 if (ret
!= KERN_SUCCESS
) {
2691 return LOAD_FAILURE
;
2694 result
->unixproc
= TRUE
;
2695 result
->thread_count
++;
2697 return LOAD_SUCCESS
;
2703 struct thread_command
*tcp
,
2706 boolean_t is_x86_64_compat_binary
,
2707 load_result_t
*result
2711 int customstack
= 0;
2712 mach_vm_offset_t addr
;
2713 if (tcp
->cmdsize
< sizeof(*tcp
)) {
2714 return LOAD_BADMACHO
;
2716 if (result
->thread_count
!= 0) {
2717 return LOAD_FAILURE
;
2720 if (thread
== THREAD_NULL
) {
2721 return LOAD_SUCCESS
;
2724 ret
= load_threadstack(thread
,
2725 (uint32_t *)(((vm_offset_t
)tcp
) +
2726 sizeof(struct thread_command
)),
2727 tcp
->cmdsize
- sizeof(struct thread_command
),
2728 &addr
, &customstack
, is_x86_64_compat_binary
, result
);
2729 if (ret
!= LOAD_SUCCESS
) {
2733 /* LC_UNIXTHREAD optionally specifies stack size and location */
2736 result
->custom_stack
= TRUE
;
2738 result
->user_stack_alloc_size
= MAXSSIZ
;
2741 /* The stack slides down from the default location */
2742 result
->user_stack
= (user_addr_t
)mach_vm_trunc_page((user_addr_t
)addr
- slide
);
2745 ret
= load_threadentry(thread
,
2746 (uint32_t *)(((vm_offset_t
)tcp
) +
2747 sizeof(struct thread_command
)),
2748 tcp
->cmdsize
- sizeof(struct thread_command
),
2750 if (ret
!= LOAD_SUCCESS
) {
2754 if (result
->using_lcmain
|| result
->entry_point
!= MACH_VM_MIN_ADDRESS
) {
2755 /* Already processed LC_MAIN or LC_UNIXTHREAD */
2756 return LOAD_FAILURE
;
2759 result
->entry_point
= (user_addr_t
)addr
;
2760 result
->entry_point
+= slide
;
2762 ret
= load_threadstate(thread
,
2763 (uint32_t *)(((vm_offset_t
)tcp
) + sizeof(struct thread_command
)),
2764 tcp
->cmdsize
- sizeof(struct thread_command
),
2766 if (ret
!= LOAD_SUCCESS
) {
2771 result
->unixproc
= TRUE
;
2772 result
->thread_count
++;
2774 return LOAD_SUCCESS
;
2782 uint32_t total_size
,
2783 load_result_t
*result
2788 uint32_t thread_size
;
2789 uint32_t *local_ts
= NULL
;
2790 uint32_t local_ts_size
= 0;
2795 if (total_size
> 0) {
2796 local_ts_size
= total_size
;
2797 local_ts
= kalloc(local_ts_size
);
2798 if (local_ts
== NULL
) {
2799 return LOAD_FAILURE
;
2801 memcpy(local_ts
, ts
, local_ts_size
);
2806 * Validate the new thread state; iterate through the state flavors in
2808 * XXX: we should validate the machine state here, to avoid failing at
2809 * activation time where we can't bail out cleanly.
2811 while (total_size
> 0) {
2812 if (total_size
< 2 * sizeof(uint32_t)) {
2813 return LOAD_BADMACHO
;
2819 if (os_add_and_mul_overflow(size
, 2, sizeof(uint32_t), &thread_size
) ||
2820 os_sub_overflow(total_size
, thread_size
, &total_size
)) {
2821 ret
= LOAD_BADMACHO
;
2825 ts
+= size
; /* ts is a (uint32_t *) */
2828 result
->threadstate
= local_ts
;
2829 result
->threadstate_sz
= local_ts_size
;
2830 return LOAD_SUCCESS
;
2834 kfree(local_ts
, local_ts_size
);
2845 uint32_t total_size
,
2846 mach_vm_offset_t
*user_stack
,
2848 __unused boolean_t is_x86_64_compat_binary
,
2849 load_result_t
*result
2855 uint32_t stack_size
;
2857 if (total_size
== 0) {
2858 return LOAD_BADMACHO
;
2861 while (total_size
> 0) {
2862 if (total_size
< 2 * sizeof(uint32_t)) {
2863 return LOAD_BADMACHO
;
2868 if (UINT32_MAX
- 2 < size
||
2869 UINT32_MAX
/ sizeof(uint32_t) < size
+ 2) {
2870 return LOAD_BADMACHO
;
2872 stack_size
= (size
+ 2) * sizeof(uint32_t);
2873 if (stack_size
> total_size
) {
2874 return LOAD_BADMACHO
;
2876 total_size
-= stack_size
;
2879 * Third argument is a kernel space pointer; it gets cast
2880 * to the appropriate type in thread_userstack() based on
2881 * the value of flavor.
2884 ret
= thread_userstack(thread
, flavor
, (thread_state_t
)ts
, size
, user_stack
, customstack
, result
->is_64bit_data
);
2885 if (ret
!= KERN_SUCCESS
) {
2886 return LOAD_FAILURE
;
2890 ts
+= size
; /* ts is a (uint32_t *) */
2892 return LOAD_SUCCESS
;
2900 uint32_t total_size
,
2901 mach_vm_offset_t
*entry_point
2907 uint32_t entry_size
;
2910 * Set the thread state.
2912 *entry_point
= MACH_VM_MIN_ADDRESS
;
2913 while (total_size
> 0) {
2914 if (total_size
< 2 * sizeof(uint32_t)) {
2915 return LOAD_BADMACHO
;
2920 if (UINT32_MAX
- 2 < size
||
2921 UINT32_MAX
/ sizeof(uint32_t) < size
+ 2) {
2922 return LOAD_BADMACHO
;
2924 entry_size
= (size
+ 2) * sizeof(uint32_t);
2925 if (entry_size
> total_size
) {
2926 return LOAD_BADMACHO
;
2928 total_size
-= entry_size
;
2930 * Third argument is a kernel space pointer; it gets cast
2931 * to the appropriate type in thread_entrypoint() based on
2932 * the value of flavor.
2934 ret
= thread_entrypoint(thread
, flavor
, (thread_state_t
)ts
, size
, entry_point
);
2935 if (ret
!= KERN_SUCCESS
) {
2936 return LOAD_FAILURE
;
2938 ts
+= size
; /* ts is a (uint32_t *) */
2940 return LOAD_SUCCESS
;
2944 struct nameidata __nid
;
2945 union macho_vnode_header
{
2946 struct mach_header mach_header
;
2947 struct fat_header fat_header
;
2952 #define DEFAULT_DYLD_PATH "/usr/lib/dyld"
2954 #if (DEVELOPMENT || DEBUG)
2955 extern char dyld_alt_path
[];
2956 extern int use_alt_dyld
;
2959 static load_return_t
2961 struct dylinker_command
*lcp
,
2967 load_result_t
*result
,
2968 struct image_params
*imgp
2972 struct vnode
*vp
= NULLVP
; /* set by get_macho_vnode() */
2973 struct mach_header
*header
;
2974 off_t file_offset
= 0; /* set by get_macho_vnode() */
2975 off_t macho_size
= 0; /* set by get_macho_vnode() */
2976 load_result_t
*myresult
;
2978 struct macho_data
*macho_data
;
2980 struct mach_header __header
;
2981 load_result_t __myresult
;
2982 struct macho_data __macho_data
;
2985 if (lcp
->cmdsize
< sizeof(*lcp
) || lcp
->name
.offset
>= lcp
->cmdsize
) {
2986 return LOAD_BADMACHO
;
2989 name
= (const char *)lcp
+ lcp
->name
.offset
;
2991 /* Check for a proper null terminated string. */
2992 size_t maxsz
= lcp
->cmdsize
- lcp
->name
.offset
;
2993 size_t namelen
= strnlen(name
, maxsz
);
2994 if (namelen
>= maxsz
) {
2995 return LOAD_BADMACHO
;
2998 #if (DEVELOPMENT || DEBUG)
3002 * If an alternate dyld has been specified via boot args, check
3003 * to see if PROC_UUID_ALT_DYLD_POLICY has been set on this
3004 * executable and redirect the kernel to load that linker.
3009 uint32_t policy_flags
= 0;
3010 int32_t policy_gencount
= 0;
3012 policy_error
= proc_uuid_policy_lookup(result
->uuid
, &policy_flags
, &policy_gencount
);
3013 if (policy_error
== 0) {
3014 if (policy_flags
& PROC_UUID_ALT_DYLD_POLICY
) {
3015 name
= dyld_alt_path
;
3021 #if !(DEVELOPMENT || DEBUG)
3022 if (0 != strcmp(name
, DEFAULT_DYLD_PATH
)) {
3023 return LOAD_BADMACHO
;
3027 /* Allocate wad-of-data from heap to reduce excessively deep stacks */
3029 dyld_data
= kheap_alloc(KHEAP_TEMP
, sizeof(*dyld_data
), Z_WAITOK
);
3030 header
= &dyld_data
->__header
;
3031 myresult
= &dyld_data
->__myresult
;
3032 macho_data
= &dyld_data
->__macho_data
;
3035 cputype
= (cputype
& CPU_ARCH_MASK
) | (cpu_type() & ~CPU_ARCH_MASK
);
3038 ret
= get_macho_vnode(name
, cputype
, header
,
3039 &file_offset
, &macho_size
, macho_data
, &vp
, imgp
);
3044 *myresult
= load_result_null
;
3045 myresult
->is_64bit_addr
= result
->is_64bit_addr
;
3046 myresult
->is_64bit_data
= result
->is_64bit_data
;
3048 ret
= parse_machfile(vp
, map
, thread
, header
, file_offset
,
3049 macho_size
, depth
, slide
, 0, myresult
, result
, imgp
);
3051 if (ret
== LOAD_SUCCESS
) {
3052 if (result
->threadstate
) {
3053 /* don't use the app's threadstate if we have a dyld */
3054 kfree(result
->threadstate
, result
->threadstate_sz
);
3056 result
->threadstate
= myresult
->threadstate
;
3057 result
->threadstate_sz
= myresult
->threadstate_sz
;
3059 result
->dynlinker
= TRUE
;
3060 result
->entry_point
= myresult
->entry_point
;
3061 result
->validentry
= myresult
->validentry
;
3062 result
->all_image_info_addr
= myresult
->all_image_info_addr
;
3063 result
->all_image_info_size
= myresult
->all_image_info_size
;
3064 if (myresult
->platform_binary
) {
3065 result
->csflags
|= CS_DYLD_PLATFORM
;
3070 struct vnode_attr
*va
;
3071 va
= kheap_alloc(KHEAP_TEMP
, sizeof(*va
), Z_WAITOK
| Z_ZERO
);
3073 VATTR_WANTED(va
, va_fsid64
);
3074 VATTR_WANTED(va
, va_fsid
);
3075 VATTR_WANTED(va
, va_fileid
);
3076 int error
= vnode_getattr(vp
, va
, imgp
->ip_vfs_context
);
3078 imgp
->ip_dyld_fsid
= vnode_get_va_fsid(va
);
3079 imgp
->ip_dyld_fsobjid
= va
->va_fileid
;
3083 kheap_free(KHEAP_TEMP
, va
, sizeof(*va
));
3085 kheap_free(KHEAP_TEMP
, dyld_data
, sizeof(*dyld_data
));
3090 static load_return_t
3091 load_code_signature(
3092 struct linkedit_data_command
*lcp
,
3097 cpu_subtype_t cpusubtype
,
3098 load_result_t
*result
,
3099 struct image_params
*imgp
)
3105 struct cs_blob
*blob
;
3107 vm_size_t blob_size
;
3114 cpusubtype
&= ~CPU_SUBTYPE_MASK
;
3116 if (lcp
->cmdsize
!= sizeof(struct linkedit_data_command
)) {
3117 ret
= LOAD_BADMACHO
;
3122 if (os_add_overflow(lcp
->dataoff
, lcp
->datasize
, &sum
) || sum
> macho_size
) {
3123 ret
= LOAD_BADMACHO
;
3127 blob
= ubc_cs_blob_get(vp
, cputype
, cpusubtype
, macho_offset
);
3130 /* we already have a blob for this vnode and cpu(sub)type */
3131 anyCPU
= blob
->csb_cpu_type
== -1;
3132 if ((blob
->csb_cpu_type
!= cputype
&&
3133 blob
->csb_cpu_subtype
!= cpusubtype
&& !anyCPU
) ||
3134 blob
->csb_base_offset
!= macho_offset
) {
3135 /* the blob has changed for this vnode: fail ! */
3136 ret
= LOAD_BADMACHO
;
3140 /* It matches the blob we want here, let's verify the version */
3141 if (!anyCPU
&& ubc_cs_generation_check(vp
) == 0) {
3142 /* No need to revalidate, we're good! */
3147 /* That blob may be stale, let's revalidate. */
3148 error
= ubc_cs_blob_revalidate(vp
, blob
, imgp
, 0, result
->ip_platform
);
3150 /* Revalidation succeeded, we're good! */
3151 /* If we were revaliding a CS blob with any CPU arch we adjust it */
3153 vnode_lock_spin(vp
);
3154 blob
->csb_cpu_type
= cputype
;
3155 blob
->csb_cpu_subtype
= cpusubtype
;
3162 if (error
!= EAGAIN
) {
3163 printf("load_code_signature: revalidation failed: %d\n", error
);
3168 assert(error
== EAGAIN
);
3171 * Revalidation was not possible for this blob. We just continue as if there was no blob,
3172 * rereading the signature, and ubc_cs_blob_add will do the right thing.
3177 blob_size
= lcp
->datasize
;
3178 kr
= ubc_cs_blob_allocate(&addr
, &blob_size
);
3179 if (kr
!= KERN_SUCCESS
) {
3185 error
= vn_rdwr(UIO_READ
,
3189 macho_offset
+ lcp
->dataoff
,
3195 if (error
|| resid
!= 0) {
3200 if (ubc_cs_blob_add(vp
,
3201 result
->ip_platform
,
3211 ubc_cs_blob_deallocate(addr
, blob_size
);
3217 /* ubc_cs_blob_add() has consumed "addr" */
3221 #if CHECK_CS_VALIDATION_BITMAP
3222 ubc_cs_validation_bitmap_allocate( vp
);
3227 if (ret
== LOAD_SUCCESS
) {
3229 panic("success, but no blob!");
3232 result
->csflags
|= blob
->csb_flags
;
3233 result
->platform_binary
= blob
->csb_platform_binary
;
3234 result
->cs_end_offset
= blob
->csb_end_offset
;
3237 ubc_cs_blob_deallocate(addr
, blob_size
);
3245 #if CONFIG_CODE_DECRYPTION
3247 static load_return_t
3249 struct encryption_info_command
*eip
,
3256 cpu_subtype_t cpusubtype
)
3259 pager_crypt_info_t crypt_info
;
3260 const char * cryptname
= 0;
3264 struct segment_command_64
*seg64
;
3265 struct segment_command
*seg32
;
3266 vm_map_offset_t map_offset
, map_size
;
3267 vm_object_offset_t crypto_backing_offset
;
3270 if (eip
->cmdsize
< sizeof(*eip
)) {
3271 return LOAD_BADMACHO
;
3274 switch (eip
->cryptid
) {
3276 /* not encrypted, just an empty load command */
3277 return LOAD_SUCCESS
;
3279 cryptname
= "com.apple.unfree";
3282 /* some random cryptid that you could manually put into
3283 * your binary if you want NULL */
3284 cryptname
= "com.apple.null";
3287 return LOAD_BADMACHO
;
3290 if (map
== VM_MAP_NULL
) {
3291 return LOAD_SUCCESS
;
3293 if (NULL
== text_crypter_create
) {
3294 return LOAD_FAILURE
;
3297 vpath
= zalloc(ZV_NAMEI
);
3300 error
= vn_getpath(vp
, vpath
, &len
);
3302 zfree(ZV_NAMEI
, vpath
);
3303 return LOAD_FAILURE
;
3306 /* set up decrypter first */
3307 crypt_file_data_t crypt_data
= {
3310 .cpusubtype
= cpusubtype
3312 kr
= text_crypter_create(&crypt_info
, cryptname
, (void*)&crypt_data
);
3313 #if VM_MAP_DEBUG_APPLE_PROTECT
3314 if (vm_map_debug_apple_protect
) {
3317 printf("APPLE_PROTECT: %d[%s] map %p %s(%s) -> 0x%x\n",
3318 p
->p_pid
, p
->p_comm
, map
, __FUNCTION__
, vpath
, kr
);
3320 #endif /* VM_MAP_DEBUG_APPLE_PROTECT */
3321 zfree(ZV_NAMEI
, vpath
);
3324 printf("set_code_unprotect: unable to create decrypter %s, kr=%d\n",
3326 if (kr
== kIOReturnNotPrivileged
) {
3327 /* text encryption returned decryption failure */
3328 return LOAD_DECRYPTFAIL
;
3330 return LOAD_RESOURCE
;
3334 /* this is terrible, but we have to rescan the load commands to find the
3335 * virtual address of this encrypted stuff. This code is gonna look like
3336 * the dyld source one day... */
3337 struct mach_header
*header
= (struct mach_header
*)addr
;
3338 size_t mach_header_sz
= sizeof(struct mach_header
);
3339 if (header
->magic
== MH_MAGIC_64
||
3340 header
->magic
== MH_CIGAM_64
) {
3341 mach_header_sz
= sizeof(struct mach_header_64
);
3343 offset
= mach_header_sz
;
3344 uint32_t ncmds
= header
->ncmds
;
3347 * Get a pointer to the command.
3349 struct load_command
*lcp
= (struct load_command
*)(addr
+ offset
);
3350 offset
+= lcp
->cmdsize
;
3354 seg64
= (struct segment_command_64
*)lcp
;
3355 if ((seg64
->fileoff
<= eip
->cryptoff
) &&
3356 (seg64
->fileoff
+ seg64
->filesize
>=
3357 eip
->cryptoff
+ eip
->cryptsize
)) {
3358 map_offset
= (vm_map_offset_t
)(seg64
->vmaddr
+ eip
->cryptoff
- seg64
->fileoff
+ slide
);
3359 map_size
= eip
->cryptsize
;
3360 crypto_backing_offset
= macho_offset
+ eip
->cryptoff
;
3365 seg32
= (struct segment_command
*)lcp
;
3366 if ((seg32
->fileoff
<= eip
->cryptoff
) &&
3367 (seg32
->fileoff
+ seg32
->filesize
>=
3368 eip
->cryptoff
+ eip
->cryptsize
)) {
3369 map_offset
= (vm_map_offset_t
)(seg32
->vmaddr
+ eip
->cryptoff
- seg32
->fileoff
+ slide
);
3370 map_size
= eip
->cryptsize
;
3371 crypto_backing_offset
= macho_offset
+ eip
->cryptoff
;
3378 /* if we get here, did not find anything */
3379 return LOAD_BADMACHO
;
3382 /* now remap using the decrypter */
3383 MACHO_PRINTF(("+++ set_code_unprotect: vm[0x%llx:0x%llx]\n",
3384 (uint64_t) map_offset
,
3385 (uint64_t) (map_offset
+ map_size
)));
3386 kr
= vm_map_apple_protected(map
,
3388 map_offset
+ map_size
,
3389 crypto_backing_offset
,
3391 CRYPTID_APP_ENCRYPTION
);
3393 printf("set_code_unprotect(): mapping failed with %x\n", kr
);
3394 return LOAD_PROTECT
;
3397 return LOAD_SUCCESS
;
3403 * This routine exists to support the load_dylinker().
3405 * This routine has its own, separate, understanding of the FAT file format,
3406 * which is terrifically unfortunate.
3413 struct mach_header
*mach_header
,
3416 struct macho_data
*data
,
3418 struct image_params
*imgp
3422 vfs_context_t ctx
= vfs_context_current();
3423 proc_t p
= vfs_context_proc(ctx
);
3424 kauth_cred_t kerncred
;
3425 struct nameidata
*ndp
= &data
->__nid
;
3427 struct fat_arch fat_arch
;
3430 union macho_vnode_header
*header
= &data
->__header
;
3431 off_t fsize
= (off_t
)0;
3434 * Capture the kernel credential for use in the actual read of the
3435 * file, since the user doing the execution may have execute rights
3436 * but not read rights, but to exec something, we have to either map
3437 * or read it into the new process address space, which requires
3438 * read rights. This is to deal with lack of common credential
3439 * serialization code which would treat NOCRED as "serialize 'root'".
3441 kerncred
= vfs_context_ucred(vfs_context_kernel());
3443 /* init the namei data to point the file user's program name */
3444 NDINIT(ndp
, LOOKUP
, OP_OPEN
, FOLLOW
| LOCKLEAF
, UIO_SYSSPACE
, CAST_USER_ADDR_T(path
), ctx
);
3446 if ((error
= namei(ndp
)) != 0) {
3447 if (error
== ENOENT
) {
3448 error
= LOAD_ENOENT
;
3450 error
= LOAD_FAILURE
;
3457 /* check for regular file */
3458 if (vp
->v_type
!= VREG
) {
3459 error
= LOAD_PROTECT
;
3464 if ((error
= vnode_size(vp
, &fsize
, ctx
)) != 0) {
3465 error
= LOAD_FAILURE
;
3469 /* Check mount point */
3470 if (vp
->v_mount
->mnt_flag
& MNT_NOEXEC
) {
3471 error
= LOAD_PROTECT
;
3476 if ((error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_EXECUTE
| KAUTH_VNODE_READ_DATA
, ctx
)) != 0) {
3477 error
= LOAD_PROTECT
;
3481 /* try to open it */
3482 if ((error
= VNOP_OPEN(vp
, FREAD
, ctx
)) != 0) {
3483 error
= LOAD_PROTECT
;
3487 if ((error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)header
, sizeof(*header
), 0,
3488 UIO_SYSSPACE
, IO_NODELOCKED
, kerncred
, &resid
, p
)) != 0) {
3489 error
= LOAD_IOERROR
;
3494 error
= LOAD_BADMACHO
;
3498 if (header
->mach_header
.magic
== MH_MAGIC
||
3499 header
->mach_header
.magic
== MH_MAGIC_64
) {
3501 } else if (OSSwapBigToHostInt32(header
->fat_header
.magic
) == FAT_MAGIC
) {
3504 error
= LOAD_BADMACHO
;
3509 error
= fatfile_validate_fatarches((vm_offset_t
)(&header
->fat_header
),
3511 if (error
!= LOAD_SUCCESS
) {
3515 /* Look up our architecture in the fat file. */
3516 error
= fatfile_getbestarch_for_cputype(cputype
, CPU_SUBTYPE_ANY
,
3517 (vm_offset_t
)(&header
->fat_header
), sizeof(*header
), imgp
, &fat_arch
);
3518 if (error
!= LOAD_SUCCESS
) {
3522 /* Read the Mach-O header out of it */
3523 error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)&header
->mach_header
,
3524 sizeof(header
->mach_header
), fat_arch
.offset
,
3525 UIO_SYSSPACE
, IO_NODELOCKED
, kerncred
, &resid
, p
);
3527 error
= LOAD_IOERROR
;
3532 error
= LOAD_BADMACHO
;
3536 /* Is this really a Mach-O? */
3537 if (header
->mach_header
.magic
!= MH_MAGIC
&&
3538 header
->mach_header
.magic
!= MH_MAGIC_64
) {
3539 error
= LOAD_BADMACHO
;
3543 *file_offset
= fat_arch
.offset
;
3544 *macho_size
= fat_arch
.size
;
3547 * Force get_macho_vnode() to fail if the architecture bits
3548 * do not match the expected architecture bits. This in
3549 * turn causes load_dylinker() to fail for the same reason,
3550 * so it ensures the dynamic linker and the binary are in
3551 * lock-step. This is potentially bad, if we ever add to
3552 * the CPU_ARCH_* bits any bits that are desirable but not
3553 * required, since the dynamic linker might work, but we will
3554 * refuse to load it because of this check.
3556 if ((cpu_type_t
)header
->mach_header
.cputype
!= cputype
) {
3557 error
= LOAD_BADARCH
;
3562 *macho_size
= fsize
;
3565 *mach_header
= header
->mach_header
;
3568 ubc_setsize(vp
, fsize
);
3572 (void) VNOP_CLOSE(vp
, FREAD
, ctx
);