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
);
1378 case LC_BUILD_VERSION
: {
1379 if (depth
!= 1 || pass
!= 0) {
1382 struct build_version_command
* bvc
= (struct build_version_command
*)lcp
;
1383 if (bvc
->cmdsize
< sizeof(*bvc
)) {
1384 ret
= LOAD_BADMACHO
;
1387 if (found_version_cmd
== TRUE
) {
1388 ret
= LOAD_BADMACHO
;
1391 result
->ip_platform
= bvc
->platform
;
1392 result
->lr_sdk
= bvc
->sdk
;
1393 found_version_cmd
= TRUE
;
1397 /* Other commands are ignored by the kernel */
1401 if (ret
!= LOAD_SUCCESS
) {
1405 if (ret
!= LOAD_SUCCESS
) {
1410 if (ret
== LOAD_SUCCESS
) {
1411 if (!got_code_signatures
&& cs_process_global_enforcement()) {
1415 /* Make sure if we need dyld, we got it */
1416 if (result
->needs_dynlinker
&& !dlp
) {
1420 if ((ret
== LOAD_SUCCESS
) && (dlp
!= 0)) {
1422 * load the dylinker, and slide it by the independent DYLD ASLR
1423 * offset regardless of the PIE-ness of the main binary.
1425 ret
= load_dylinker(dlp
, header
->cputype
, map
, thread
, depth
,
1426 dyld_aslr_offset
, result
, imgp
);
1430 if ((ret
== LOAD_SUCCESS
) && (depth
== 1)) {
1431 if (result
->thread_count
== 0) {
1434 #if CONFIG_ENFORCE_SIGNED_CODE
1435 if (result
->needs_dynlinker
&& !(result
->csflags
& CS_DYLD_PLATFORM
)) {
1442 if (ret
== LOAD_BADMACHO
&& found_xhdr
) {
1443 ret
= LOAD_BADMACHO_UPX
;
1446 kfree(addr
, alloc_size
);
1452 validate_potential_simulator_binary(
1453 cpu_type_t exectype __unused
,
1454 struct image_params
*imgp __unused
,
1455 off_t file_offset __unused
,
1456 off_t macho_size __unused
)
1459 /* Allow 32 bit exec only for simulator binaries */
1460 if (bootarg_no32exec
&& imgp
!= NULL
&& exectype
== CPU_TYPE_X86
) {
1461 if (imgp
->ip_simulator_binary
== IMGPF_SB_DEFAULT
) {
1462 boolean_t simulator_binary
= check_if_simulator_binary(imgp
, file_offset
, macho_size
);
1463 imgp
->ip_simulator_binary
= simulator_binary
? IMGPF_SB_TRUE
: IMGPF_SB_FALSE
;
1466 if (imgp
->ip_simulator_binary
!= IMGPF_SB_TRUE
) {
1467 return LOAD_BADARCH
;
1471 return LOAD_SUCCESS
;
1476 check_if_simulator_binary(
1477 struct image_params
*imgp
,
1481 struct mach_header
*header
;
1482 char *ip_vdata
= NULL
;
1483 kauth_cred_t cred
= NULL
;
1485 struct load_command
*lcp
;
1486 boolean_t simulator_binary
= FALSE
;
1488 vm_size_t alloc_size
, cmds_size
;
1490 proc_t p
= current_proc(); /* XXXX */
1493 size_t mach_header_sz
= sizeof(struct mach_header
);
1496 cred
= kauth_cred_proc_ref(p
);
1498 /* Allocate page to copyin mach header */
1499 ip_vdata
= kalloc(PAGE_SIZE
);
1500 bzero(ip_vdata
, PAGE_SIZE
);
1501 if (ip_vdata
== NULL
) {
1505 /* Read the Mach-O header */
1506 error
= vn_rdwr(UIO_READ
, imgp
->ip_vp
, ip_vdata
,
1507 PAGE_SIZE
, file_offset
,
1508 UIO_SYSSPACE
, (IO_UNIT
| IO_NODELOCKED
),
1514 header
= (struct mach_header
*)ip_vdata
;
1516 if (header
->magic
== MH_MAGIC_64
||
1517 header
->magic
== MH_CIGAM_64
) {
1518 mach_header_sz
= sizeof(struct mach_header_64
);
1521 /* ensure header + sizeofcmds falls within the file */
1522 if (os_add_overflow(mach_header_sz
, header
->sizeofcmds
, &cmds_size
) ||
1523 (off_t
)cmds_size
> macho_size
||
1524 round_page_overflow(cmds_size
, &alloc_size
) ||
1525 alloc_size
> INT_MAX
) {
1530 * Map the load commands into kernel memory.
1532 addr
= kalloc(alloc_size
);
1537 error
= vn_rdwr(UIO_READ
, imgp
->ip_vp
, addr
, (int)alloc_size
, file_offset
,
1538 UIO_SYSSPACE
, IO_NODELOCKED
, cred
, &resid
, p
);
1544 /* We must be able to read in as much as the mach_header indicated */
1549 * Loop through each of the load_commands indicated by the
1550 * Mach-O header; if an absurd value is provided, we just
1551 * run off the end of the reserved section by incrementing
1552 * the offset too far, so we are implicitly fail-safe.
1554 offset
= mach_header_sz
;
1555 ncmds
= header
->ncmds
;
1558 /* ensure enough space for a minimal load command */
1559 if (offset
+ sizeof(struct load_command
) > cmds_size
) {
1564 * Get a pointer to the command.
1566 lcp
= (struct load_command
*)(addr
+ offset
);
1569 * Perform prevalidation of the struct load_command
1570 * before we attempt to use its contents. Invalid
1571 * values are ones which result in an overflow, or
1572 * which can not possibly be valid commands, or which
1573 * straddle or exist past the reserved section at the
1574 * start of the image.
1576 if (os_add_overflow(offset
, lcp
->cmdsize
, &offset
) ||
1577 lcp
->cmdsize
< sizeof(struct load_command
) ||
1578 offset
> cmds_size
) {
1582 /* Check if its a simulator binary. */
1584 case LC_VERSION_MIN_WATCHOS
:
1585 simulator_binary
= TRUE
;
1588 case LC_BUILD_VERSION
: {
1589 struct build_version_command
*bvc
;
1591 bvc
= (struct build_version_command
*) lcp
;
1592 if (bvc
->cmdsize
< sizeof(*bvc
)) {
1593 /* unsafe to use this command struct if cmdsize
1594 * validated above is too small for it to fit */
1597 if (bvc
->platform
== PLATFORM_IOSSIMULATOR
||
1598 bvc
->platform
== PLATFORM_WATCHOSSIMULATOR
) {
1599 simulator_binary
= TRUE
;
1605 case LC_VERSION_MIN_IPHONEOS
: {
1606 simulator_binary
= TRUE
;
1611 /* ignore other load commands */
1615 if (simulator_binary
== TRUE
) {
1622 kfree(ip_vdata
, PAGE_SIZE
);
1626 kauth_cred_unref(&cred
);
1630 kfree(addr
, alloc_size
);
1633 return simulator_binary
;
1635 #endif /* __x86_64__ */
1637 #if CONFIG_CODE_DECRYPTION
1639 #define APPLE_UNPROTECTED_HEADER_SIZE (3 * 4096)
1641 static load_return_t
1642 unprotect_dsmos_segment(
1648 vm_map_offset_t map_addr
,
1649 vm_map_size_t map_size
)
1655 * The first APPLE_UNPROTECTED_HEADER_SIZE bytes (from offset 0 of
1656 * this part of a Universal binary) are not protected...
1657 * The rest needs to be "transformed".
1659 slice_off
= file_off
- macho_offset
;
1660 if (slice_off
<= APPLE_UNPROTECTED_HEADER_SIZE
&&
1661 slice_off
+ file_size
<= APPLE_UNPROTECTED_HEADER_SIZE
) {
1662 /* it's all unprotected, nothing to do... */
1665 if (slice_off
<= APPLE_UNPROTECTED_HEADER_SIZE
) {
1667 * We start mapping in the unprotected area.
1668 * Skip the unprotected part...
1670 uint64_t delta_file
;
1671 vm_map_offset_t delta_map
;
1673 delta_file
= (uint64_t)APPLE_UNPROTECTED_HEADER_SIZE
;
1674 delta_file
-= slice_off
;
1675 if (os_convert_overflow(delta_file
, &delta_map
)) {
1676 return LOAD_BADMACHO
;
1678 if (os_add_overflow(map_addr
, delta_map
, &map_addr
)) {
1679 return LOAD_BADMACHO
;
1681 if (os_sub_overflow(map_size
, delta_map
, &map_size
)) {
1682 return LOAD_BADMACHO
;
1685 /* ... transform the rest of the mapping. */
1686 struct pager_crypt_info crypt_info
;
1687 crypt_info
.page_decrypt
= dsmos_page_transform
;
1688 crypt_info
.crypt_ops
= NULL
;
1689 crypt_info
.crypt_end
= NULL
;
1690 #pragma unused(vp, macho_offset)
1691 crypt_info
.crypt_ops
= (void *)0x2e69cf40;
1692 vm_map_offset_t crypto_backing_offset
;
1693 crypto_backing_offset
= -1; /* i.e. use map entry's offset */
1694 #if VM_MAP_DEBUG_APPLE_PROTECT
1695 if (vm_map_debug_apple_protect
) {
1698 printf("APPLE_PROTECT: %d[%s] map %p "
1699 "[0x%llx:0x%llx] %s(%s)\n",
1700 p
->p_pid
, p
->p_comm
, map
,
1701 (uint64_t) map_addr
,
1702 (uint64_t) (map_addr
+ map_size
),
1703 __FUNCTION__
, vp
->v_name
);
1705 #endif /* VM_MAP_DEBUG_APPLE_PROTECT */
1707 /* The DSMOS pager can only be used by apple signed code */
1708 struct cs_blob
* blob
= csvnode_get_blob(vp
, file_off
);
1709 if (blob
== NULL
|| !blob
->csb_platform_binary
|| blob
->csb_platform_path
) {
1710 return LOAD_FAILURE
;
1713 kr
= vm_map_apple_protected(map
,
1715 map_addr
+ map_size
,
1716 crypto_backing_offset
,
1718 CRYPTID_APP_ENCRYPTION
);
1721 if (kr
!= KERN_SUCCESS
) {
1722 return LOAD_FAILURE
;
1724 return LOAD_SUCCESS
;
1726 #else /* CONFIG_CODE_DECRYPTION */
1727 static load_return_t
1728 unprotect_dsmos_segment(
1729 __unused
uint64_t file_off
,
1730 __unused
uint64_t file_size
,
1731 __unused
struct vnode
*vp
,
1732 __unused off_t macho_offset
,
1733 __unused vm_map_t map
,
1734 __unused vm_map_offset_t map_addr
,
1735 __unused vm_map_size_t map_size
)
1737 return LOAD_SUCCESS
;
1739 #endif /* CONFIG_CODE_DECRYPTION */
1744 * Maps a Mach-O segment, taking care of mis-alignment (wrt the system
1745 * page size) issues.
1747 * The mapping might result in 1, 2 or 3 map entries:
1748 * 1. for the first page, which could be overlap with the previous
1750 * 2. for the center (if applicable),
1751 * 3. for the last page, which could overlap with the next mapping.
1753 * For each of those map entries, we might have to interpose a
1754 * "fourk_pager" to deal with mis-alignment wrt the system page size,
1755 * either in the mapping address and/or size or the file offset and/or
1757 * The "fourk_pager" itself would be mapped with proper alignment
1758 * wrt the system page size and would then be populated with the
1759 * information about the intended mapping, with a "4KB" granularity.
1761 static kern_return_t
1764 vm_map_offset_t vm_start
,
1765 vm_map_offset_t vm_end
,
1766 memory_object_control_t control
,
1767 vm_map_offset_t file_start
,
1768 vm_map_offset_t file_end
,
1771 load_result_t
*result
)
1773 vm_map_offset_t cur_offset
, cur_start
, cur_end
;
1775 vm_map_offset_t effective_page_mask
;
1776 vm_map_kernel_flags_t vmk_flags
, cur_vmk_flags
;
1778 if (vm_end
< vm_start
||
1779 file_end
< file_start
) {
1780 return LOAD_BADMACHO
;
1782 if (vm_end
== vm_start
||
1783 file_end
== file_start
) {
1784 /* nothing to map... */
1785 return LOAD_SUCCESS
;
1788 effective_page_mask
= vm_map_page_mask(map
);
1790 vmk_flags
= VM_MAP_KERNEL_FLAGS_NONE
;
1791 if (vm_map_page_aligned(vm_start
, effective_page_mask
) &&
1792 vm_map_page_aligned(vm_end
, effective_page_mask
) &&
1793 vm_map_page_aligned(file_start
, effective_page_mask
) &&
1794 vm_map_page_aligned(file_end
, effective_page_mask
)) {
1795 /* all page-aligned and map-aligned: proceed */
1798 /* use an intermediate "4K" pager */
1799 vmk_flags
.vmkf_fourk
= TRUE
;
1800 #else /* __arm64__ */
1801 panic("map_segment: unexpected mis-alignment "
1802 "vm[0x%llx:0x%llx] file[0x%llx:0x%llx]\n",
1803 (uint64_t) vm_start
,
1805 (uint64_t) file_start
,
1806 (uint64_t) file_end
);
1807 #endif /* __arm64__ */
1811 cur_start
= vm_start
;
1814 if (!vm_map_page_aligned(vm_start
, effective_page_mask
)) {
1815 /* one 4K pager for the 1st page */
1816 cur_end
= vm_map_round_page(cur_start
, effective_page_mask
);
1817 if (cur_end
> vm_end
) {
1818 cur_end
= vm_start
+ (file_end
- file_start
);
1820 if (control
!= MEMORY_OBJECT_CONTROL_NULL
) {
1821 /* no copy-on-read for mapped binaries */
1822 vmk_flags
.vmkf_no_copy_on_read
= 1;
1823 ret
= vm_map_enter_mem_object_control(
1826 cur_end
- cur_start
,
1827 (mach_vm_offset_t
)0,
1830 VM_KERN_MEMORY_NONE
,
1832 file_start
+ cur_offset
,
1835 VM_INHERIT_DEFAULT
);
1837 ret
= vm_map_enter_mem_object(
1840 cur_end
- cur_start
,
1841 (mach_vm_offset_t
)0,
1844 VM_KERN_MEMORY_NONE
,
1849 VM_INHERIT_DEFAULT
);
1851 if (ret
!= KERN_SUCCESS
) {
1852 return LOAD_NOSPACE
;
1854 cur_offset
+= cur_end
- cur_start
;
1856 #endif /* __arm64__ */
1857 if (cur_end
>= vm_start
+ (file_end
- file_start
)) {
1858 /* all mapped: done */
1861 if (vm_map_round_page(cur_end
, effective_page_mask
) >=
1862 vm_map_trunc_page(vm_start
+ (file_end
- file_start
),
1863 effective_page_mask
)) {
1866 cur_start
= cur_end
;
1867 if ((vm_start
& effective_page_mask
) !=
1868 (file_start
& effective_page_mask
)) {
1869 /* one 4K pager for the middle */
1870 cur_vmk_flags
= vmk_flags
;
1872 /* regular mapping for the middle */
1873 cur_vmk_flags
= VM_MAP_KERNEL_FLAGS_NONE
;
1876 #if !defined(XNU_TARGET_OS_OSX)
1878 #else /* !defined(XNU_TARGET_OS_OSX) */
1880 * This process doesn't have its new csflags (from
1881 * the image being loaded) yet, so tell VM to override the
1882 * current process's CS_ENFORCEMENT for this mapping.
1884 if (result
->csflags
& CS_ENFORCEMENT
) {
1885 cur_vmk_flags
.vmkf_cs_enforcement
= TRUE
;
1887 cur_vmk_flags
.vmkf_cs_enforcement
= FALSE
;
1889 cur_vmk_flags
.vmkf_cs_enforcement_override
= TRUE
;
1890 #endif /* !defined(XNU_TARGET_OS_OSX) */
1892 if (result
->is_cambria
&& (initprot
& VM_PROT_EXECUTE
) == VM_PROT_EXECUTE
) {
1893 cur_vmk_flags
.vmkf_translated_allow_execute
= TRUE
;
1896 cur_end
= vm_map_trunc_page(vm_start
+ (file_end
-
1898 effective_page_mask
);
1899 if (control
!= MEMORY_OBJECT_CONTROL_NULL
) {
1900 /* no copy-on-read for mapped binaries */
1901 cur_vmk_flags
.vmkf_no_copy_on_read
= 1;
1902 ret
= vm_map_enter_mem_object_control(
1905 cur_end
- cur_start
,
1906 (mach_vm_offset_t
)0,
1909 VM_KERN_MEMORY_NONE
,
1911 file_start
+ cur_offset
,
1914 VM_INHERIT_DEFAULT
);
1916 ret
= vm_map_enter_mem_object(
1919 cur_end
- cur_start
,
1920 (mach_vm_offset_t
)0,
1923 VM_KERN_MEMORY_NONE
,
1928 VM_INHERIT_DEFAULT
);
1930 if (ret
!= KERN_SUCCESS
) {
1931 return LOAD_NOSPACE
;
1933 cur_offset
+= cur_end
- cur_start
;
1935 if (cur_end
>= vm_start
+ (file_end
- file_start
)) {
1936 /* all mapped: done */
1939 cur_start
= cur_end
;
1941 if (!vm_map_page_aligned(vm_start
+ (file_end
- file_start
),
1942 effective_page_mask
)) {
1943 /* one 4K pager for the last page */
1944 cur_end
= vm_start
+ (file_end
- file_start
);
1945 if (control
!= MEMORY_OBJECT_CONTROL_NULL
) {
1946 /* no copy-on-read for mapped binaries */
1947 vmk_flags
.vmkf_no_copy_on_read
= 1;
1948 ret
= vm_map_enter_mem_object_control(
1951 cur_end
- cur_start
,
1952 (mach_vm_offset_t
)0,
1955 VM_KERN_MEMORY_NONE
,
1957 file_start
+ cur_offset
,
1960 VM_INHERIT_DEFAULT
);
1962 ret
= vm_map_enter_mem_object(
1965 cur_end
- cur_start
,
1966 (mach_vm_offset_t
)0,
1969 VM_KERN_MEMORY_NONE
,
1974 VM_INHERIT_DEFAULT
);
1976 if (ret
!= KERN_SUCCESS
) {
1977 return LOAD_NOSPACE
;
1979 cur_offset
+= cur_end
- cur_start
;
1981 #endif /* __arm64__ */
1983 assert(cur_end
>= vm_start
+ (file_end
- file_start
));
1984 return LOAD_SUCCESS
;
1990 struct load_command
*lcp
,
1998 load_result_t
*result
,
1999 struct image_params
*imgp
)
2001 struct segment_command_64 segment_command
, *scp
;
2003 vm_map_size_t delta_size
;
2006 size_t segment_command_size
, total_section_size
,
2007 single_section_size
;
2008 uint64_t file_offset
, file_size
;
2009 vm_map_offset_t vm_offset
;
2011 vm_map_offset_t vm_start
, vm_end
, vm_end_aligned
;
2012 vm_map_offset_t file_start
, file_end
;
2015 vm_map_size_t effective_page_size
;
2016 vm_map_offset_t effective_page_mask
;
2018 vm_map_kernel_flags_t vmk_flags
;
2019 boolean_t fourk_align
;
2020 #endif /* __arm64__ */
2024 effective_page_size
= vm_map_page_size(map
);
2025 effective_page_mask
= vm_map_page_mask(map
);
2028 if (LC_SEGMENT_64
== lcp
->cmd
) {
2029 segment_command_size
= sizeof(struct segment_command_64
);
2030 single_section_size
= sizeof(struct section_64
);
2032 /* 64-bit binary: should already be 16K-aligned */
2033 fourk_align
= FALSE
;
2035 if (vm_map_page_shift(map
) == FOURK_PAGE_SHIFT
&&
2036 PAGE_SHIFT
!= FOURK_PAGE_SHIFT
) {
2040 #endif /* __arm64__ */
2042 segment_command_size
= sizeof(struct segment_command
);
2043 single_section_size
= sizeof(struct section
);
2045 /* 32-bit binary: might need 4K-alignment */
2046 if (effective_page_size
!= FOURK_PAGE_SIZE
) {
2047 /* not using 4K page size: need fourk_pager */
2051 /* using 4K page size: no need for re-alignment */
2052 fourk_align
= FALSE
;
2054 #endif /* __arm64__ */
2056 if (lcp
->cmdsize
< segment_command_size
) {
2057 DEBUG4K_ERROR("LOAD_BADMACHO cmdsize %d < %zu\n", lcp
->cmdsize
, segment_command_size
);
2058 return LOAD_BADMACHO
;
2060 total_section_size
= lcp
->cmdsize
- segment_command_size
;
2062 if (LC_SEGMENT_64
== lcp
->cmd
) {
2063 scp
= (struct segment_command_64
*)lcp
;
2065 scp
= &segment_command
;
2066 widen_segment_command((struct segment_command
*)lcp
, scp
);
2070 MACHO_PRINTF(("+++ load_segment %s "
2071 "vm[0x%llx:0x%llx] file[0x%llx:0x%llx] "
2072 "prot %d/%d flags 0x%x\n",
2074 (uint64_t)(slide
+ scp
->vmaddr
),
2075 (uint64_t)(slide
+ scp
->vmaddr
+ scp
->vmsize
),
2076 pager_offset
+ scp
->fileoff
,
2077 pager_offset
+ scp
->fileoff
+ scp
->filesize
,
2084 * Make sure what we get from the file is really ours (as specified
2087 if (scp
->fileoff
+ scp
->filesize
< scp
->fileoff
||
2088 scp
->fileoff
+ scp
->filesize
> (uint64_t)macho_size
) {
2089 DEBUG4K_ERROR("LOAD_BADMACHO fileoff 0x%llx filesize 0x%llx macho_size 0x%llx\n", scp
->fileoff
, scp
->filesize
, (uint64_t)macho_size
);
2090 return LOAD_BADMACHO
;
2093 * Ensure that the number of sections specified would fit
2094 * within the load command size.
2096 if (total_section_size
/ single_section_size
< scp
->nsects
) {
2097 DEBUG4K_ERROR("LOAD_BADMACHO 0x%zx 0x%zx %d\n", total_section_size
, single_section_size
, scp
->nsects
);
2098 return LOAD_BADMACHO
;
2101 * Make sure the segment is page-aligned in the file.
2103 if (os_add_overflow(pager_offset
, scp
->fileoff
, &file_offset
)) {
2104 DEBUG4K_ERROR("LOAD_BADMACHO file_offset: 0x%llx + 0x%llx\n", pager_offset
, scp
->fileoff
);
2105 return LOAD_BADMACHO
;
2107 file_size
= scp
->filesize
;
2110 if ((file_offset
& FOURK_PAGE_MASK
) != 0) {
2112 * we can't mmap() it if it's not at least 4KB-aligned
2115 DEBUG4K_ERROR("LOAD_BADMACHO file_offset 0x%llx\n", file_offset
);
2116 return LOAD_BADMACHO
;
2119 #endif /* __arm64__ */
2120 if ((file_offset
& PAGE_MASK_64
) != 0 ||
2121 /* we can't mmap() it if it's not page-aligned in the file */
2122 (file_offset
& vm_map_page_mask(map
)) != 0) {
2124 * The 1st test would have failed if the system's page size
2125 * was what this process believe is the page size, so let's
2126 * fail here too for the sake of consistency.
2128 DEBUG4K_ERROR("LOAD_BADMACHO file_offset 0x%llx\n", file_offset
);
2129 return LOAD_BADMACHO
;
2133 * If we have a code signature attached for this slice
2134 * require that the segments are within the signed part
2137 if (result
->cs_end_offset
&&
2138 result
->cs_end_offset
< (off_t
)scp
->fileoff
&&
2139 result
->cs_end_offset
- scp
->fileoff
< scp
->filesize
) {
2141 printf("section outside code signature\n");
2143 DEBUG4K_ERROR("LOAD_BADMACHO end_offset 0x%llx fileoff 0x%llx filesize 0x%llx\n", result
->cs_end_offset
, scp
->fileoff
, scp
->filesize
);
2144 return LOAD_BADMACHO
;
2147 if (os_add_overflow(scp
->vmaddr
, slide
, &vm_offset
)) {
2149 printf("vmaddr too large\n");
2151 DEBUG4K_ERROR("LOAD_BADMACHO vmaddr 0x%llx slide 0x%llx vm_offset 0x%llx\n", scp
->vmaddr
, slide
, (uint64_t)vm_offset
);
2152 return LOAD_BADMACHO
;
2155 if (scp
->vmsize
> SIZE_MAX
) {
2156 DEBUG4K_ERROR("LOAD_BADMACHO vmsize 0x%llx\n", scp
->vmsize
);
2157 return LOAD_BADMACHO
;
2160 vm_size
= (size_t)scp
->vmsize
;
2163 return LOAD_SUCCESS
;
2165 if (scp
->vmaddr
== 0 &&
2168 (scp
->initprot
& VM_PROT_ALL
) == VM_PROT_NONE
&&
2169 (scp
->maxprot
& VM_PROT_ALL
) == VM_PROT_NONE
) {
2170 if (map
== VM_MAP_NULL
) {
2171 return LOAD_SUCCESS
;
2175 * For PIE, extend page zero rather than moving it. Extending
2176 * page zero keeps early allocations from falling predictably
2177 * between the end of page zero and the beginning of the first
2181 * This is a "page zero" segment: it starts at address 0,
2182 * is not mapped from the binary file and is not accessible.
2183 * User-space should never be able to access that memory, so
2184 * make it completely off limits by raising the VM map's
2187 vm_end
= (vm_map_offset_t
)(vm_offset
+ vm_size
);
2188 if (vm_end
< vm_offset
) {
2189 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
);
2190 return LOAD_BADMACHO
;
2194 MACHO_PRINTF(("++++++ load_segment: "
2195 "page_zero up to 0x%llx\n",
2196 (uint64_t) vm_end
));
2200 /* raise min_offset as much as page-alignment allows */
2201 vm_end_aligned
= vm_map_trunc_page(vm_end
,
2202 effective_page_mask
);
2204 #endif /* __arm64__ */
2206 vm_end
= vm_map_round_page(vm_end
,
2208 vm_end_aligned
= vm_end
;
2210 ret
= vm_map_raise_min_offset(map
,
2214 vm_end
> vm_end_aligned
) {
2215 /* use fourk_pager to map the rest of pagezero */
2216 assert(fourk_align
);
2217 vmk_flags
= VM_MAP_KERNEL_FLAGS_NONE
;
2218 vmk_flags
.vmkf_fourk
= TRUE
;
2219 ret
= vm_map_enter_mem_object(
2222 vm_end
- vm_end_aligned
,
2223 (mach_vm_offset_t
) 0, /* mask */
2226 VM_KERN_MEMORY_NONE
,
2230 (scp
->initprot
& VM_PROT_ALL
),
2231 (scp
->maxprot
& VM_PROT_ALL
),
2232 VM_INHERIT_DEFAULT
);
2234 #endif /* __arm64__ */
2236 if (ret
!= KERN_SUCCESS
) {
2237 DEBUG4K_ERROR("LOAD_FAILURE ret 0x%x\n", ret
);
2238 return LOAD_FAILURE
;
2240 return LOAD_SUCCESS
;
2242 #if !defined(XNU_TARGET_OS_OSX)
2243 /* not PAGEZERO: should not be mapped at address 0 */
2244 if (filetype
!= MH_DYLINKER
&& scp
->vmaddr
== 0) {
2245 DEBUG4K_ERROR("LOAD_BADMACHO filetype %d vmaddr 0x%llx\n", filetype
, scp
->vmaddr
);
2246 return LOAD_BADMACHO
;
2248 #endif /* !defined(XNU_TARGET_OS_OSX) */
2254 file_start
= vm_map_trunc_page(file_offset
,
2256 file_end
= vm_map_round_page(file_offset
+ file_size
,
2258 vm_start
= vm_map_trunc_page(vm_offset
,
2260 vm_end
= vm_map_round_page(vm_offset
+ vm_size
,
2263 if (file_offset
- file_start
> FOURK_PAGE_MASK
||
2264 file_end
- file_offset
- file_size
> FOURK_PAGE_MASK
) {
2265 DEBUG4K_ERROR("LOAD_BADMACHO file_start / file_size wrap "
2266 "[0x%llx:0x%llx] -> [0x%llx:0x%llx]\n",
2268 file_offset
+ file_size
,
2269 (uint64_t) file_start
,
2270 (uint64_t) file_end
);
2271 return LOAD_BADMACHO
;
2274 if (!strncmp(scp
->segname
, "__LINKEDIT", 11) &&
2275 page_aligned(file_start
) &&
2276 vm_map_page_aligned(file_start
, vm_map_page_mask(map
)) &&
2277 page_aligned(vm_start
) &&
2278 vm_map_page_aligned(vm_start
, vm_map_page_mask(map
))) {
2279 /* XXX last segment: ignore mis-aligned tail */
2280 file_end
= vm_map_round_page(file_end
,
2281 effective_page_mask
);
2282 vm_end
= vm_map_round_page(vm_end
,
2283 effective_page_mask
);
2286 #endif /* __arm64__ */
2288 file_start
= vm_map_trunc_page(file_offset
,
2289 effective_page_mask
);
2290 file_end
= vm_map_round_page(file_offset
+ file_size
,
2291 effective_page_mask
);
2292 vm_start
= vm_map_trunc_page(vm_offset
,
2293 effective_page_mask
);
2294 vm_end
= vm_map_round_page(vm_offset
+ vm_size
,
2295 effective_page_mask
);
2297 if (file_offset
- file_start
> effective_page_mask
||
2298 file_end
- file_offset
- file_size
> effective_page_mask
) {
2299 DEBUG4K_ERROR("LOAD_BADMACHO file_start / file_size wrap "
2300 "[0x%llx:0x%llx] -> [0x%llx:0x%llx]\n",
2302 file_offset
+ file_size
,
2303 (uint64_t) file_start
,
2304 (uint64_t) file_end
);
2305 return LOAD_BADMACHO
;
2309 if (vm_start
< result
->min_vm_addr
) {
2310 result
->min_vm_addr
= vm_start
;
2312 if (vm_end
> result
->max_vm_addr
) {
2313 result
->max_vm_addr
= vm_end
;
2316 if (map
== VM_MAP_NULL
) {
2317 return LOAD_SUCCESS
;
2321 initprot
= (scp
->initprot
) & VM_PROT_ALL
;
2322 maxprot
= (scp
->maxprot
) & VM_PROT_ALL
;
2324 * Map a copy of the file into the address space.
2327 MACHO_PRINTF(("++++++ load_segment: "
2328 "mapping at vm [0x%llx:0x%llx] of "
2329 "file [0x%llx:0x%llx]\n",
2330 (uint64_t) vm_start
,
2332 (uint64_t) file_start
,
2333 (uint64_t) file_end
));
2335 ret
= map_segment(map
,
2345 DEBUG4K_ERROR("LOAD_NOSPACE start 0x%llx end 0x%llx ret 0x%x\n", (uint64_t)vm_start
, (uint64_t)vm_end
, ret
);
2346 return LOAD_NOSPACE
;
2351 * If the file didn't end on a page boundary,
2352 * we need to zero the leftover.
2354 delta_size
= map_size
- scp
->filesize
;
2355 if (delta_size
> 0) {
2356 mach_vm_offset_t tmp
;
2358 ret
= mach_vm_allocate_kernel(kernel_map
, &tmp
, delta_size
, VM_FLAGS_ANYWHERE
, VM_KERN_MEMORY_BSD
);
2359 if (ret
!= KERN_SUCCESS
) {
2360 DEBUG4K_ERROR("LOAD_RESOURCE delta_size 0x%llx ret 0x%x\n", delta_size
, ret
);
2361 return LOAD_RESOURCE
;
2364 if (copyout(tmp
, map_addr
+ scp
->filesize
,
2366 (void) mach_vm_deallocate(
2367 kernel_map
, tmp
, delta_size
);
2368 DEBUG4K_ERROR("LOAD_FAILURE copyout 0x%llx 0x%llx\n", map_addr
+ scp
->filesize
, delta_size
);
2369 return LOAD_FAILURE
;
2372 (void) mach_vm_deallocate(kernel_map
, tmp
, delta_size
);
2378 * If the virtual size of the segment is greater
2379 * than the size from the file, we need to allocate
2380 * zero fill memory for the rest.
2382 if ((vm_end
- vm_start
) > (file_end
- file_start
)) {
2383 delta_size
= (vm_end
- vm_start
) - (file_end
- file_start
);
2387 if (delta_size
> 0) {
2388 vm_map_offset_t tmp_start
;
2389 vm_map_offset_t tmp_end
;
2391 if (os_add_overflow(vm_start
, file_end
- file_start
, &tmp_start
)) {
2392 DEBUG4K_ERROR("LOAD_NOSPACE tmp_start: 0x%llx + 0x%llx\n", (uint64_t)vm_start
, (uint64_t)(file_end
- file_start
));
2393 return LOAD_NOSPACE
;
2396 if (os_add_overflow(tmp_start
, delta_size
, &tmp_end
)) {
2397 DEBUG4K_ERROR("LOAD_NOSPACE tmp_end: 0x%llx + 0x%llx\n", (uint64_t)tmp_start
, (uint64_t)delta_size
);
2398 return LOAD_NOSPACE
;
2402 MACHO_PRINTF(("++++++ load_segment: "
2403 "delta mapping vm [0x%llx:0x%llx]\n",
2404 (uint64_t) tmp_start
,
2405 (uint64_t) tmp_end
));
2407 kr
= map_segment(map
,
2410 MEMORY_OBJECT_CONTROL_NULL
,
2416 if (kr
!= KERN_SUCCESS
) {
2417 DEBUG4K_ERROR("LOAD_NOSPACE 0x%llx 0x%llx kr 0x%x\n", (unsigned long long)tmp_start
, (uint64_t)delta_size
, kr
);
2418 return LOAD_NOSPACE
;
2422 if ((scp
->fileoff
== 0) && (scp
->filesize
!= 0)) {
2423 result
->mach_header
= vm_offset
;
2426 if (scp
->flags
& SG_PROTECTED_VERSION_1
) {
2427 ret
= unprotect_dsmos_segment(file_start
,
2428 file_end
- file_start
,
2434 if (ret
!= LOAD_SUCCESS
) {
2435 DEBUG4K_ERROR("unprotect 0x%llx 0x%llx ret %d \n", (uint64_t)vm_start
, (uint64_t)vm_end
, ret
);
2442 if (LOAD_SUCCESS
== ret
&&
2443 filetype
== MH_DYLINKER
&&
2444 result
->all_image_info_addr
== MACH_VM_MIN_ADDRESS
) {
2445 note_all_image_info_section(scp
,
2446 LC_SEGMENT_64
== lcp
->cmd
,
2447 single_section_size
,
2448 ((const char *)lcp
+
2449 segment_command_size
),
2454 if (result
->entry_point
!= MACH_VM_MIN_ADDRESS
) {
2455 if ((result
->entry_point
>= vm_offset
) && (result
->entry_point
< (vm_offset
+ vm_size
))) {
2456 if ((scp
->initprot
& (VM_PROT_READ
| VM_PROT_EXECUTE
)) == (VM_PROT_READ
| VM_PROT_EXECUTE
)) {
2457 result
->validentry
= 1;
2459 /* right range but wrong protections, unset if previously validated */
2460 result
->validentry
= 0;
2465 if (ret
!= LOAD_SUCCESS
&& verbose
) {
2466 DEBUG4K_ERROR("ret %d\n", ret
);
2474 struct uuid_command
*uulp
,
2476 load_result_t
*result
2480 * We need to check the following for this command:
2481 * - The command size should be atleast the size of struct uuid_command
2482 * - The UUID part of the command should be completely within the mach-o header
2485 if ((uulp
->cmdsize
< sizeof(struct uuid_command
)) ||
2486 (((char *)uulp
+ sizeof(struct uuid_command
)) > command_end
)) {
2487 return LOAD_BADMACHO
;
2490 memcpy(&result
->uuid
[0], &uulp
->uuid
[0], sizeof(result
->uuid
));
2491 return LOAD_SUCCESS
;
2497 struct version_min_command
*vmc
,
2498 boolean_t
*found_version_cmd
,
2499 int ip_flags __unused
,
2500 load_result_t
*result
2503 uint32_t platform
= 0;
2506 if (vmc
->cmdsize
< sizeof(*vmc
)) {
2507 return LOAD_BADMACHO
;
2509 if (*found_version_cmd
== TRUE
) {
2510 return LOAD_BADMACHO
;
2512 *found_version_cmd
= TRUE
;
2515 case LC_VERSION_MIN_MACOSX
:
2516 platform
= PLATFORM_MACOS
;
2518 #if __x86_64__ /* __x86_64__ */
2519 case LC_VERSION_MIN_IPHONEOS
:
2520 platform
= PLATFORM_IOSSIMULATOR
;
2522 case LC_VERSION_MIN_WATCHOS
:
2523 platform
= PLATFORM_WATCHOSSIMULATOR
;
2525 case LC_VERSION_MIN_TVOS
:
2526 platform
= PLATFORM_TVOSSIMULATOR
;
2529 case LC_VERSION_MIN_IPHONEOS
: {
2531 extern int legacy_footprint_entitlement_mode
;
2532 if (vmc
->sdk
< (12 << 16)) {
2533 /* app built with a pre-iOS12 SDK: apply legacy footprint mitigation */
2534 result
->legacy_footprint
= TRUE
;
2536 #endif /* __arm64__ */
2537 platform
= PLATFORM_IOS
;
2540 case LC_VERSION_MIN_WATCHOS
:
2541 platform
= PLATFORM_WATCHOS
;
2543 case LC_VERSION_MIN_TVOS
:
2544 platform
= PLATFORM_TVOS
;
2546 #endif /* __x86_64__ */
2547 /* All LC_VERSION_MIN_* load commands are legacy and we will not be adding any more */
2550 __builtin_unreachable();
2552 result
->ip_platform
= platform
;
2553 result
->lr_min_sdk
= sdk
;
2554 return LOAD_SUCCESS
;
2560 struct entry_point_command
*epc
,
2563 load_result_t
*result
2566 mach_vm_offset_t addr
;
2569 if (epc
->cmdsize
< sizeof(*epc
)) {
2570 return LOAD_BADMACHO
;
2572 if (result
->thread_count
!= 0) {
2573 return LOAD_FAILURE
;
2576 if (thread
== THREAD_NULL
) {
2577 return LOAD_SUCCESS
;
2581 * LC_MAIN specifies stack size but not location.
2582 * Add guard page to allocation size (MAXSSIZ includes guard page).
2584 if (epc
->stacksize
) {
2585 if (os_add_overflow(epc
->stacksize
, 4 * PAGE_SIZE
, &result
->user_stack_size
)) {
2587 * We are going to immediately throw away this result, but we want
2588 * to make sure we aren't loading a dangerously close to
2589 * overflowing value, since this will have a guard page added to it
2590 * and be rounded to page boundaries
2592 return LOAD_BADMACHO
;
2594 result
->user_stack_size
= epc
->stacksize
;
2595 if (os_add_overflow(epc
->stacksize
, PAGE_SIZE
, &result
->user_stack_alloc_size
)) {
2596 return LOAD_BADMACHO
;
2598 result
->custom_stack
= TRUE
;
2600 result
->user_stack_alloc_size
= MAXSSIZ
;
2603 /* use default location for stack */
2604 ret
= thread_userstackdefault(&addr
, result
->is_64bit_addr
);
2605 if (ret
!= KERN_SUCCESS
) {
2606 return LOAD_FAILURE
;
2609 /* The stack slides down from the default location */
2610 result
->user_stack
= (user_addr_t
)mach_vm_trunc_page((user_addr_t
)addr
- slide
);
2612 if (result
->using_lcmain
|| result
->entry_point
!= MACH_VM_MIN_ADDRESS
) {
2613 /* Already processed LC_MAIN or LC_UNIXTHREAD */
2614 return LOAD_FAILURE
;
2617 /* kernel does *not* use entryoff from LC_MAIN. Dyld uses it. */
2618 result
->needs_dynlinker
= TRUE
;
2619 result
->using_lcmain
= TRUE
;
2621 ret
= thread_state_initialize( thread
);
2622 if (ret
!= KERN_SUCCESS
) {
2623 return LOAD_FAILURE
;
2626 result
->unixproc
= TRUE
;
2627 result
->thread_count
++;
2629 return LOAD_SUCCESS
;
2637 load_result_t
*result
2640 mach_vm_offset_t addr
;
2643 /* Driver binaries have no LC_MAIN, use defaults */
2645 if (thread
== THREAD_NULL
) {
2646 return LOAD_SUCCESS
;
2649 result
->user_stack_alloc_size
= MAXSSIZ
;
2651 /* use default location for stack */
2652 ret
= thread_userstackdefault(&addr
, result
->is_64bit_addr
);
2653 if (ret
!= KERN_SUCCESS
) {
2654 return LOAD_FAILURE
;
2657 /* The stack slides down from the default location */
2658 result
->user_stack
= (user_addr_t
)addr
;
2659 result
->user_stack
-= slide
;
2661 if (result
->using_lcmain
|| result
->entry_point
!= MACH_VM_MIN_ADDRESS
) {
2662 /* Already processed LC_MAIN or LC_UNIXTHREAD */
2663 return LOAD_FAILURE
;
2666 result
->needs_dynlinker
= TRUE
;
2668 ret
= thread_state_initialize( thread
);
2669 if (ret
!= KERN_SUCCESS
) {
2670 return LOAD_FAILURE
;
2673 result
->unixproc
= TRUE
;
2674 result
->thread_count
++;
2676 return LOAD_SUCCESS
;
2682 struct thread_command
*tcp
,
2685 boolean_t is_x86_64_compat_binary
,
2686 load_result_t
*result
2690 int customstack
= 0;
2691 mach_vm_offset_t addr
;
2692 if (tcp
->cmdsize
< sizeof(*tcp
)) {
2693 return LOAD_BADMACHO
;
2695 if (result
->thread_count
!= 0) {
2696 return LOAD_FAILURE
;
2699 if (thread
== THREAD_NULL
) {
2700 return LOAD_SUCCESS
;
2703 ret
= load_threadstack(thread
,
2704 (uint32_t *)(((vm_offset_t
)tcp
) +
2705 sizeof(struct thread_command
)),
2706 tcp
->cmdsize
- sizeof(struct thread_command
),
2707 &addr
, &customstack
, is_x86_64_compat_binary
, result
);
2708 if (ret
!= LOAD_SUCCESS
) {
2712 /* LC_UNIXTHREAD optionally specifies stack size and location */
2715 result
->custom_stack
= TRUE
;
2717 result
->user_stack_alloc_size
= MAXSSIZ
;
2720 /* The stack slides down from the default location */
2721 result
->user_stack
= (user_addr_t
)mach_vm_trunc_page((user_addr_t
)addr
- slide
);
2724 ret
= load_threadentry(thread
,
2725 (uint32_t *)(((vm_offset_t
)tcp
) +
2726 sizeof(struct thread_command
)),
2727 tcp
->cmdsize
- sizeof(struct thread_command
),
2729 if (ret
!= LOAD_SUCCESS
) {
2733 if (result
->using_lcmain
|| result
->entry_point
!= MACH_VM_MIN_ADDRESS
) {
2734 /* Already processed LC_MAIN or LC_UNIXTHREAD */
2735 return LOAD_FAILURE
;
2738 result
->entry_point
= (user_addr_t
)addr
;
2739 result
->entry_point
+= slide
;
2741 ret
= load_threadstate(thread
,
2742 (uint32_t *)(((vm_offset_t
)tcp
) + sizeof(struct thread_command
)),
2743 tcp
->cmdsize
- sizeof(struct thread_command
),
2745 if (ret
!= LOAD_SUCCESS
) {
2750 result
->unixproc
= TRUE
;
2751 result
->thread_count
++;
2753 return LOAD_SUCCESS
;
2761 uint32_t total_size
,
2762 load_result_t
*result
2767 uint32_t thread_size
;
2768 uint32_t *local_ts
= NULL
;
2769 uint32_t local_ts_size
= 0;
2774 if (total_size
> 0) {
2775 local_ts_size
= total_size
;
2776 local_ts
= kalloc(local_ts_size
);
2777 if (local_ts
== NULL
) {
2778 return LOAD_FAILURE
;
2780 memcpy(local_ts
, ts
, local_ts_size
);
2785 * Validate the new thread state; iterate through the state flavors in
2787 * XXX: we should validate the machine state here, to avoid failing at
2788 * activation time where we can't bail out cleanly.
2790 while (total_size
> 0) {
2791 if (total_size
< 2 * sizeof(uint32_t)) {
2792 return LOAD_BADMACHO
;
2798 if (os_add_and_mul_overflow(size
, 2, sizeof(uint32_t), &thread_size
) ||
2799 os_sub_overflow(total_size
, thread_size
, &total_size
)) {
2800 ret
= LOAD_BADMACHO
;
2804 ts
+= size
; /* ts is a (uint32_t *) */
2807 result
->threadstate
= local_ts
;
2808 result
->threadstate_sz
= local_ts_size
;
2809 return LOAD_SUCCESS
;
2813 kfree(local_ts
, local_ts_size
);
2824 uint32_t total_size
,
2825 mach_vm_offset_t
*user_stack
,
2827 __unused boolean_t is_x86_64_compat_binary
,
2828 load_result_t
*result
2834 uint32_t stack_size
;
2836 if (total_size
== 0) {
2837 return LOAD_BADMACHO
;
2840 while (total_size
> 0) {
2841 if (total_size
< 2 * sizeof(uint32_t)) {
2842 return LOAD_BADMACHO
;
2847 if (UINT32_MAX
- 2 < size
||
2848 UINT32_MAX
/ sizeof(uint32_t) < size
+ 2) {
2849 return LOAD_BADMACHO
;
2851 stack_size
= (size
+ 2) * sizeof(uint32_t);
2852 if (stack_size
> total_size
) {
2853 return LOAD_BADMACHO
;
2855 total_size
-= stack_size
;
2858 * Third argument is a kernel space pointer; it gets cast
2859 * to the appropriate type in thread_userstack() based on
2860 * the value of flavor.
2863 ret
= thread_userstack(thread
, flavor
, (thread_state_t
)ts
, size
, user_stack
, customstack
, result
->is_64bit_data
);
2864 if (ret
!= KERN_SUCCESS
) {
2865 return LOAD_FAILURE
;
2869 ts
+= size
; /* ts is a (uint32_t *) */
2871 return LOAD_SUCCESS
;
2879 uint32_t total_size
,
2880 mach_vm_offset_t
*entry_point
2886 uint32_t entry_size
;
2889 * Set the thread state.
2891 *entry_point
= MACH_VM_MIN_ADDRESS
;
2892 while (total_size
> 0) {
2893 if (total_size
< 2 * sizeof(uint32_t)) {
2894 return LOAD_BADMACHO
;
2899 if (UINT32_MAX
- 2 < size
||
2900 UINT32_MAX
/ sizeof(uint32_t) < size
+ 2) {
2901 return LOAD_BADMACHO
;
2903 entry_size
= (size
+ 2) * sizeof(uint32_t);
2904 if (entry_size
> total_size
) {
2905 return LOAD_BADMACHO
;
2907 total_size
-= entry_size
;
2909 * Third argument is a kernel space pointer; it gets cast
2910 * to the appropriate type in thread_entrypoint() based on
2911 * the value of flavor.
2913 ret
= thread_entrypoint(thread
, flavor
, (thread_state_t
)ts
, size
, entry_point
);
2914 if (ret
!= KERN_SUCCESS
) {
2915 return LOAD_FAILURE
;
2917 ts
+= size
; /* ts is a (uint32_t *) */
2919 return LOAD_SUCCESS
;
2923 struct nameidata __nid
;
2924 union macho_vnode_header
{
2925 struct mach_header mach_header
;
2926 struct fat_header fat_header
;
2931 #define DEFAULT_DYLD_PATH "/usr/lib/dyld"
2933 #if (DEVELOPMENT || DEBUG)
2934 extern char dyld_alt_path
[];
2935 extern int use_alt_dyld
;
2938 static load_return_t
2940 struct dylinker_command
*lcp
,
2946 load_result_t
*result
,
2947 struct image_params
*imgp
2951 struct vnode
*vp
= NULLVP
; /* set by get_macho_vnode() */
2952 struct mach_header
*header
;
2953 off_t file_offset
= 0; /* set by get_macho_vnode() */
2954 off_t macho_size
= 0; /* set by get_macho_vnode() */
2955 load_result_t
*myresult
;
2957 struct macho_data
*macho_data
;
2959 struct mach_header __header
;
2960 load_result_t __myresult
;
2961 struct macho_data __macho_data
;
2964 if (lcp
->cmdsize
< sizeof(*lcp
) || lcp
->name
.offset
>= lcp
->cmdsize
) {
2965 return LOAD_BADMACHO
;
2968 name
= (const char *)lcp
+ lcp
->name
.offset
;
2970 /* Check for a proper null terminated string. */
2971 size_t maxsz
= lcp
->cmdsize
- lcp
->name
.offset
;
2972 size_t namelen
= strnlen(name
, maxsz
);
2973 if (namelen
>= maxsz
) {
2974 return LOAD_BADMACHO
;
2977 #if (DEVELOPMENT || DEBUG)
2981 * If an alternate dyld has been specified via boot args, check
2982 * to see if PROC_UUID_ALT_DYLD_POLICY has been set on this
2983 * executable and redirect the kernel to load that linker.
2988 uint32_t policy_flags
= 0;
2989 int32_t policy_gencount
= 0;
2991 policy_error
= proc_uuid_policy_lookup(result
->uuid
, &policy_flags
, &policy_gencount
);
2992 if (policy_error
== 0) {
2993 if (policy_flags
& PROC_UUID_ALT_DYLD_POLICY
) {
2994 name
= dyld_alt_path
;
3000 #if !(DEVELOPMENT || DEBUG)
3001 if (0 != strcmp(name
, DEFAULT_DYLD_PATH
)) {
3002 return LOAD_BADMACHO
;
3006 /* Allocate wad-of-data from heap to reduce excessively deep stacks */
3008 MALLOC(dyld_data
, void *, sizeof(*dyld_data
), M_TEMP
, M_WAITOK
);
3009 header
= &dyld_data
->__header
;
3010 myresult
= &dyld_data
->__myresult
;
3011 macho_data
= &dyld_data
->__macho_data
;
3014 cputype
= (cputype
& CPU_ARCH_MASK
) | (cpu_type() & ~CPU_ARCH_MASK
);
3017 ret
= get_macho_vnode(name
, cputype
, header
,
3018 &file_offset
, &macho_size
, macho_data
, &vp
, imgp
);
3023 *myresult
= load_result_null
;
3024 myresult
->is_64bit_addr
= result
->is_64bit_addr
;
3025 myresult
->is_64bit_data
= result
->is_64bit_data
;
3027 ret
= parse_machfile(vp
, map
, thread
, header
, file_offset
,
3028 macho_size
, depth
, slide
, 0, myresult
, result
, imgp
);
3030 if (ret
== LOAD_SUCCESS
) {
3031 if (result
->threadstate
) {
3032 /* don't use the app's threadstate if we have a dyld */
3033 kfree(result
->threadstate
, result
->threadstate_sz
);
3035 result
->threadstate
= myresult
->threadstate
;
3036 result
->threadstate_sz
= myresult
->threadstate_sz
;
3038 result
->dynlinker
= TRUE
;
3039 result
->entry_point
= myresult
->entry_point
;
3040 result
->validentry
= myresult
->validentry
;
3041 result
->all_image_info_addr
= myresult
->all_image_info_addr
;
3042 result
->all_image_info_size
= myresult
->all_image_info_size
;
3043 if (myresult
->platform_binary
) {
3044 result
->csflags
|= CS_DYLD_PLATFORM
;
3049 struct vnode_attr
*va
;
3050 va
= kheap_alloc(KHEAP_TEMP
, sizeof(*va
), Z_WAITOK
| Z_ZERO
);
3052 VATTR_WANTED(va
, va_fsid64
);
3053 VATTR_WANTED(va
, va_fsid
);
3054 VATTR_WANTED(va
, va_fileid
);
3055 int error
= vnode_getattr(vp
, va
, imgp
->ip_vfs_context
);
3057 imgp
->ip_dyld_fsid
= vnode_get_va_fsid(va
);
3058 imgp
->ip_dyld_fsobjid
= va
->va_fileid
;
3062 kheap_free(KHEAP_TEMP
, va
, sizeof(*va
));
3064 FREE(dyld_data
, M_TEMP
);
3069 static load_return_t
3070 load_code_signature(
3071 struct linkedit_data_command
*lcp
,
3076 cpu_subtype_t cpusubtype
,
3077 load_result_t
*result
,
3078 struct image_params
*imgp
)
3084 struct cs_blob
*blob
;
3086 vm_size_t blob_size
;
3093 cpusubtype
&= ~CPU_SUBTYPE_MASK
;
3095 if (lcp
->cmdsize
!= sizeof(struct linkedit_data_command
)) {
3096 ret
= LOAD_BADMACHO
;
3101 if (os_add_overflow(lcp
->dataoff
, lcp
->datasize
, &sum
) || sum
> macho_size
) {
3102 ret
= LOAD_BADMACHO
;
3106 blob
= ubc_cs_blob_get(vp
, cputype
, cpusubtype
, macho_offset
);
3109 /* we already have a blob for this vnode and cpu(sub)type */
3110 anyCPU
= blob
->csb_cpu_type
== -1;
3111 if ((blob
->csb_cpu_type
!= cputype
&&
3112 blob
->csb_cpu_subtype
!= cpusubtype
&& !anyCPU
) ||
3113 blob
->csb_base_offset
!= macho_offset
) {
3114 /* the blob has changed for this vnode: fail ! */
3115 ret
= LOAD_BADMACHO
;
3119 /* It matches the blob we want here, let's verify the version */
3120 if (!anyCPU
&& ubc_cs_generation_check(vp
) == 0) {
3121 /* No need to revalidate, we're good! */
3126 /* That blob may be stale, let's revalidate. */
3127 error
= ubc_cs_blob_revalidate(vp
, blob
, imgp
, 0, result
->ip_platform
);
3129 /* Revalidation succeeded, we're good! */
3130 /* If we were revaliding a CS blob with any CPU arch we adjust it */
3132 vnode_lock_spin(vp
);
3133 blob
->csb_cpu_type
= cputype
;
3134 blob
->csb_cpu_subtype
= cpusubtype
;
3141 if (error
!= EAGAIN
) {
3142 printf("load_code_signature: revalidation failed: %d\n", error
);
3147 assert(error
== EAGAIN
);
3150 * Revalidation was not possible for this blob. We just continue as if there was no blob,
3151 * rereading the signature, and ubc_cs_blob_add will do the right thing.
3156 blob_size
= lcp
->datasize
;
3157 kr
= ubc_cs_blob_allocate(&addr
, &blob_size
);
3158 if (kr
!= KERN_SUCCESS
) {
3164 error
= vn_rdwr(UIO_READ
,
3168 macho_offset
+ lcp
->dataoff
,
3174 if (error
|| resid
!= 0) {
3179 if (ubc_cs_blob_add(vp
,
3180 result
->ip_platform
,
3190 ubc_cs_blob_deallocate(addr
, blob_size
);
3196 /* ubc_cs_blob_add() has consumed "addr" */
3200 #if CHECK_CS_VALIDATION_BITMAP
3201 ubc_cs_validation_bitmap_allocate( vp
);
3206 if (ret
== LOAD_SUCCESS
) {
3208 panic("success, but no blob!");
3211 result
->csflags
|= blob
->csb_flags
;
3212 result
->platform_binary
= blob
->csb_platform_binary
;
3213 result
->cs_end_offset
= blob
->csb_end_offset
;
3216 ubc_cs_blob_deallocate(addr
, blob_size
);
3224 #if CONFIG_CODE_DECRYPTION
3226 static load_return_t
3228 struct encryption_info_command
*eip
,
3235 cpu_subtype_t cpusubtype
)
3238 pager_crypt_info_t crypt_info
;
3239 const char * cryptname
= 0;
3243 struct segment_command_64
*seg64
;
3244 struct segment_command
*seg32
;
3245 vm_map_offset_t map_offset
, map_size
;
3246 vm_object_offset_t crypto_backing_offset
;
3249 if (eip
->cmdsize
< sizeof(*eip
)) {
3250 return LOAD_BADMACHO
;
3253 switch (eip
->cryptid
) {
3255 /* not encrypted, just an empty load command */
3256 return LOAD_SUCCESS
;
3258 cryptname
= "com.apple.unfree";
3261 /* some random cryptid that you could manually put into
3262 * your binary if you want NULL */
3263 cryptname
= "com.apple.null";
3266 return LOAD_BADMACHO
;
3269 if (map
== VM_MAP_NULL
) {
3270 return LOAD_SUCCESS
;
3272 if (NULL
== text_crypter_create
) {
3273 return LOAD_FAILURE
;
3276 vpath
= zalloc(ZV_NAMEI
);
3279 error
= vn_getpath(vp
, vpath
, &len
);
3281 zfree(ZV_NAMEI
, vpath
);
3282 return LOAD_FAILURE
;
3285 /* set up decrypter first */
3286 crypt_file_data_t crypt_data
= {
3289 .cpusubtype
= cpusubtype
3291 kr
= text_crypter_create(&crypt_info
, cryptname
, (void*)&crypt_data
);
3292 #if VM_MAP_DEBUG_APPLE_PROTECT
3293 if (vm_map_debug_apple_protect
) {
3296 printf("APPLE_PROTECT: %d[%s] map %p %s(%s) -> 0x%x\n",
3297 p
->p_pid
, p
->p_comm
, map
, __FUNCTION__
, vpath
, kr
);
3299 #endif /* VM_MAP_DEBUG_APPLE_PROTECT */
3300 zfree(ZV_NAMEI
, vpath
);
3303 printf("set_code_unprotect: unable to create decrypter %s, kr=%d\n",
3305 if (kr
== kIOReturnNotPrivileged
) {
3306 /* text encryption returned decryption failure */
3307 return LOAD_DECRYPTFAIL
;
3309 return LOAD_RESOURCE
;
3313 /* this is terrible, but we have to rescan the load commands to find the
3314 * virtual address of this encrypted stuff. This code is gonna look like
3315 * the dyld source one day... */
3316 struct mach_header
*header
= (struct mach_header
*)addr
;
3317 size_t mach_header_sz
= sizeof(struct mach_header
);
3318 if (header
->magic
== MH_MAGIC_64
||
3319 header
->magic
== MH_CIGAM_64
) {
3320 mach_header_sz
= sizeof(struct mach_header_64
);
3322 offset
= mach_header_sz
;
3323 uint32_t ncmds
= header
->ncmds
;
3326 * Get a pointer to the command.
3328 struct load_command
*lcp
= (struct load_command
*)(addr
+ offset
);
3329 offset
+= lcp
->cmdsize
;
3333 seg64
= (struct segment_command_64
*)lcp
;
3334 if ((seg64
->fileoff
<= eip
->cryptoff
) &&
3335 (seg64
->fileoff
+ seg64
->filesize
>=
3336 eip
->cryptoff
+ eip
->cryptsize
)) {
3337 map_offset
= (vm_map_offset_t
)(seg64
->vmaddr
+ eip
->cryptoff
- seg64
->fileoff
+ slide
);
3338 map_size
= eip
->cryptsize
;
3339 crypto_backing_offset
= macho_offset
+ eip
->cryptoff
;
3344 seg32
= (struct segment_command
*)lcp
;
3345 if ((seg32
->fileoff
<= eip
->cryptoff
) &&
3346 (seg32
->fileoff
+ seg32
->filesize
>=
3347 eip
->cryptoff
+ eip
->cryptsize
)) {
3348 map_offset
= (vm_map_offset_t
)(seg32
->vmaddr
+ eip
->cryptoff
- seg32
->fileoff
+ slide
);
3349 map_size
= eip
->cryptsize
;
3350 crypto_backing_offset
= macho_offset
+ eip
->cryptoff
;
3357 /* if we get here, did not find anything */
3358 return LOAD_BADMACHO
;
3361 /* now remap using the decrypter */
3362 MACHO_PRINTF(("+++ set_code_unprotect: vm[0x%llx:0x%llx]\n",
3363 (uint64_t) map_offset
,
3364 (uint64_t) (map_offset
+ map_size
)));
3365 kr
= vm_map_apple_protected(map
,
3367 map_offset
+ map_size
,
3368 crypto_backing_offset
,
3370 CRYPTID_APP_ENCRYPTION
);
3372 printf("set_code_unprotect(): mapping failed with %x\n", kr
);
3373 return LOAD_PROTECT
;
3376 return LOAD_SUCCESS
;
3382 * This routine exists to support the load_dylinker().
3384 * This routine has its own, separate, understanding of the FAT file format,
3385 * which is terrifically unfortunate.
3392 struct mach_header
*mach_header
,
3395 struct macho_data
*data
,
3397 struct image_params
*imgp
3401 vfs_context_t ctx
= vfs_context_current();
3402 proc_t p
= vfs_context_proc(ctx
);
3403 kauth_cred_t kerncred
;
3404 struct nameidata
*ndp
= &data
->__nid
;
3406 struct fat_arch fat_arch
;
3409 union macho_vnode_header
*header
= &data
->__header
;
3410 off_t fsize
= (off_t
)0;
3413 * Capture the kernel credential for use in the actual read of the
3414 * file, since the user doing the execution may have execute rights
3415 * but not read rights, but to exec something, we have to either map
3416 * or read it into the new process address space, which requires
3417 * read rights. This is to deal with lack of common credential
3418 * serialization code which would treat NOCRED as "serialize 'root'".
3420 kerncred
= vfs_context_ucred(vfs_context_kernel());
3422 /* init the namei data to point the file user's program name */
3423 NDINIT(ndp
, LOOKUP
, OP_OPEN
, FOLLOW
| LOCKLEAF
, UIO_SYSSPACE
, CAST_USER_ADDR_T(path
), ctx
);
3425 if ((error
= namei(ndp
)) != 0) {
3426 if (error
== ENOENT
) {
3427 error
= LOAD_ENOENT
;
3429 error
= LOAD_FAILURE
;
3436 /* check for regular file */
3437 if (vp
->v_type
!= VREG
) {
3438 error
= LOAD_PROTECT
;
3443 if ((error
= vnode_size(vp
, &fsize
, ctx
)) != 0) {
3444 error
= LOAD_FAILURE
;
3448 /* Check mount point */
3449 if (vp
->v_mount
->mnt_flag
& MNT_NOEXEC
) {
3450 error
= LOAD_PROTECT
;
3455 if ((error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_EXECUTE
| KAUTH_VNODE_READ_DATA
, ctx
)) != 0) {
3456 error
= LOAD_PROTECT
;
3460 /* try to open it */
3461 if ((error
= VNOP_OPEN(vp
, FREAD
, ctx
)) != 0) {
3462 error
= LOAD_PROTECT
;
3466 if ((error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)header
, sizeof(*header
), 0,
3467 UIO_SYSSPACE
, IO_NODELOCKED
, kerncred
, &resid
, p
)) != 0) {
3468 error
= LOAD_IOERROR
;
3473 error
= LOAD_BADMACHO
;
3477 if (header
->mach_header
.magic
== MH_MAGIC
||
3478 header
->mach_header
.magic
== MH_MAGIC_64
) {
3480 } else if (OSSwapBigToHostInt32(header
->fat_header
.magic
) == FAT_MAGIC
) {
3483 error
= LOAD_BADMACHO
;
3488 error
= fatfile_validate_fatarches((vm_offset_t
)(&header
->fat_header
),
3490 if (error
!= LOAD_SUCCESS
) {
3494 /* Look up our architecture in the fat file. */
3495 error
= fatfile_getbestarch_for_cputype(cputype
, CPU_SUBTYPE_ANY
,
3496 (vm_offset_t
)(&header
->fat_header
), sizeof(*header
), imgp
, &fat_arch
);
3497 if (error
!= LOAD_SUCCESS
) {
3501 /* Read the Mach-O header out of it */
3502 error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)&header
->mach_header
,
3503 sizeof(header
->mach_header
), fat_arch
.offset
,
3504 UIO_SYSSPACE
, IO_NODELOCKED
, kerncred
, &resid
, p
);
3506 error
= LOAD_IOERROR
;
3511 error
= LOAD_BADMACHO
;
3515 /* Is this really a Mach-O? */
3516 if (header
->mach_header
.magic
!= MH_MAGIC
&&
3517 header
->mach_header
.magic
!= MH_MAGIC_64
) {
3518 error
= LOAD_BADMACHO
;
3522 *file_offset
= fat_arch
.offset
;
3523 *macho_size
= fat_arch
.size
;
3526 * Force get_macho_vnode() to fail if the architecture bits
3527 * do not match the expected architecture bits. This in
3528 * turn causes load_dylinker() to fail for the same reason,
3529 * so it ensures the dynamic linker and the binary are in
3530 * lock-step. This is potentially bad, if we ever add to
3531 * the CPU_ARCH_* bits any bits that are desirable but not
3532 * required, since the dynamic linker might work, but we will
3533 * refuse to load it because of this check.
3535 if ((cpu_type_t
)header
->mach_header
.cputype
!= cputype
) {
3536 error
= LOAD_BADARCH
;
3541 *macho_size
= fsize
;
3544 *mach_header
= header
->mach_header
;
3547 ubc_setsize(vp
, fsize
);
3551 (void) VNOP_CLOSE(vp
, FREAD
, ctx
);