2 * Copyright (c) 2000-2010 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/ubc_internal.h>
51 #include <sys/imgact.h>
52 #include <sys/codesign.h>
53 #include <sys/proc_uuid_policy.h>
54 #include <sys/reason.h>
55 #include <sys/kdebug.h>
57 #include <mach/mach_types.h>
58 #include <mach/vm_map.h> /* vm_allocate() */
59 #include <mach/mach_vm.h> /* mach_vm_allocate() */
60 #include <mach/vm_statistics.h>
61 #include <mach/task.h>
62 #include <mach/thread_act.h>
64 #include <machine/vmparam.h>
65 #include <machine/exec.h>
66 #include <machine/pal_routines.h>
69 #include <kern/kern_types.h>
70 #include <kern/cpu_number.h>
71 #include <kern/mach_loader.h>
72 #include <kern/mach_fat.h>
73 #include <kern/kalloc.h>
74 #include <kern/task.h>
75 #include <kern/thread.h>
76 #include <kern/page_decrypt.h>
78 #include <mach-o/fat.h>
79 #include <mach-o/loader.h>
82 #include <vm/vm_map.h>
83 #include <vm/vm_kern.h>
84 #include <vm/vm_pager.h>
85 #include <vm/vnode_pager.h>
86 #include <vm/vm_protos.h>
87 #include <IOKit/IOReturn.h> /* for kIOReturnNotPrivileged */
89 #include <os/overflow.h>
92 extern int bootarg_no32exec
; /* bsd_init.c */
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(ledger_t ledger
, vm_map_size_t size
,
102 /* XXX should have prototypes in a shared header file */
103 extern int get_map_nentries(vm_map_t
);
105 extern kern_return_t
memory_object_signed(memory_object_control_t control
,
106 boolean_t is_signed
);
108 /* An empty load_result_t */
109 static const load_result_t load_result_null
= {
110 .mach_header
= MACH_VM_MIN_ADDRESS
,
111 .entry_point
= MACH_VM_MIN_ADDRESS
,
112 .user_stack
= MACH_VM_MIN_ADDRESS
,
113 .user_stack_size
= 0,
114 .user_stack_alloc
= MACH_VM_MIN_ADDRESS
,
115 .user_stack_alloc_size
= 0,
116 .all_image_info_addr
= MACH_VM_MIN_ADDRESS
,
117 .all_image_info_size
= 0,
121 .needs_dynlinker
= 0,
128 .min_vm_addr
= MACH_VM_MAX_ADDRESS
,
129 .max_vm_addr
= MACH_VM_MIN_ADDRESS
,
136 * Prototypes of static functions.
143 struct mach_header
*header
,
149 load_result_t
*result
,
150 load_result_t
*binresult
,
151 struct image_params
*imgp
156 struct load_command
*lcp
,
164 load_result_t
*result
169 struct uuid_command
*uulp
,
171 load_result_t
*result
176 struct linkedit_data_command
*lcp
,
181 load_result_t
*result
,
182 struct image_params
*imgp
);
184 #if CONFIG_CODE_DECRYPTION
187 struct encryption_info_command
*lcp
,
194 cpu_subtype_t cpusubtype
);
200 struct entry_point_command
*epc
,
203 load_result_t
*result
208 struct thread_command
*tcp
,
211 load_result_t
*result
227 mach_vm_offset_t
*user_stack
,
229 load_result_t
*result
237 mach_vm_offset_t
*entry_point
242 struct dylinker_command
*lcp
,
248 load_result_t
*result
,
249 struct image_params
*imgp
258 struct mach_header
*mach_header
,
261 struct macho_data
*macho_data
,
266 widen_segment_command(const struct segment_command
*scp32
,
267 struct segment_command_64
*scp
)
269 scp
->cmd
= scp32
->cmd
;
270 scp
->cmdsize
= scp32
->cmdsize
;
271 bcopy(scp32
->segname
, scp
->segname
, sizeof(scp
->segname
));
272 scp
->vmaddr
= scp32
->vmaddr
;
273 scp
->vmsize
= scp32
->vmsize
;
274 scp
->fileoff
= scp32
->fileoff
;
275 scp
->filesize
= scp32
->filesize
;
276 scp
->maxprot
= scp32
->maxprot
;
277 scp
->initprot
= scp32
->initprot
;
278 scp
->nsects
= scp32
->nsects
;
279 scp
->flags
= scp32
->flags
;
283 note_all_image_info_section(const struct segment_command_64
*scp
,
284 boolean_t is64
, size_t section_size
, const void *sections
,
285 int64_t slide
, load_result_t
*result
)
289 struct section_64 s64
;
294 if (strncmp(scp
->segname
, "__DATA", sizeof(scp
->segname
)) != 0)
296 for (i
= 0; i
< scp
->nsects
; ++i
) {
297 sectionp
= (const void *)
298 ((const char *)sections
+ section_size
* i
);
299 if (0 == strncmp(sectionp
->s64
.sectname
, "__all_image_info",
300 sizeof(sectionp
->s64
.sectname
))) {
301 result
->all_image_info_addr
=
302 is64
? sectionp
->s64
.addr
: sectionp
->s32
.addr
;
303 result
->all_image_info_addr
+= slide
;
304 result
->all_image_info_size
=
305 is64
? sectionp
->s64
.size
: sectionp
->s32
.size
;
313 * Allow bypassing some security rules (hard pagezero, no write+execute)
314 * in exchange for better binary compatibility for legacy apps built
315 * before 16KB-alignment was enforced.
317 int fourk_binary_compatibility_unsafe
= TRUE
;
318 int fourk_binary_compatibility_allow_wx
= FALSE
;
319 #endif /* __arm64__ */
323 struct image_params
*imgp
,
324 struct mach_header
*header
,
327 load_result_t
*result
330 struct vnode
*vp
= imgp
->ip_vp
;
331 off_t file_offset
= imgp
->ip_arch_offset
;
332 off_t macho_size
= imgp
->ip_arch_size
;
333 off_t file_size
= imgp
->ip_vattr
->va_data_size
;
334 pmap_t pmap
= 0; /* protected by create_map */
336 load_result_t myresult
;
338 boolean_t enforce_hard_pagezero
= TRUE
;
339 int in_exec
= (imgp
->ip_flags
& IMGPF_EXEC
);
340 task_t task
= current_task();
341 proc_t p
= current_proc();
342 int64_t aslr_page_offset
= 0;
343 int64_t dyld_aslr_page_offset
= 0;
344 int64_t aslr_section_size
= 0;
345 int64_t aslr_section_offset
= 0;
348 if (macho_size
> file_size
) {
349 return(LOAD_BADMACHO
);
352 result
->is64bit
= ((imgp
->ip_flags
& IMGPF_IS_64BIT
) == IMGPF_IS_64BIT
);
355 if (imgp
->ip_new_thread
) {
356 ledger_task
= get_threadtask(imgp
->ip_new_thread
);
360 pmap
= pmap_create(get_task_ledger(ledger_task
),
363 map
= vm_map_create(pmap
,
365 vm_compute_max_offset(result
->is64bit
),
368 #if defined(__arm64__)
369 if (result
->is64bit
) {
370 /* enforce 16KB alignment of VM map entries */
371 vm_map_set_page_shift(map
, SIXTEENK_PAGE_SHIFT
);
373 vm_map_set_page_shift(map
, page_shift_user32
);
375 #elif (__ARM_ARCH_7K__ >= 2) && defined(PLATFORM_WatchOS)
376 /* enforce 16KB alignment for watch targets with new ABI */
377 vm_map_set_page_shift(map
, SIXTEENK_PAGE_SHIFT
);
378 #endif /* __arm64__ */
380 #ifndef CONFIG_ENFORCE_SIGNED_CODE
381 /* This turns off faulting for executable pages, which allows
382 * to circumvent Code Signing Enforcement. The per process
383 * flag (CS_ENFORCEMENT) is not set yet, but we can use the
386 if ( !cs_enforcement(NULL
) && (header
->flags
& MH_ALLOW_STACK_EXECUTION
) )
387 vm_map_disable_NX(map
);
390 /* Forcibly disallow execution from data pages on even if the arch
391 * normally permits it. */
392 if ((header
->flags
& MH_NO_HEAP_EXECUTION
) && !(imgp
->ip_flags
& IMGPF_ALLOW_DATA_EXEC
))
393 vm_map_disallow_data_exec(map
);
396 * Compute a random offset for ASLR, and an independent random offset for dyld.
398 if (!(imgp
->ip_flags
& IMGPF_DISABLE_ASLR
)) {
399 vm_map_get_max_aslr_slide_section(map
, &aslr_section_offset
, &aslr_section_size
);
400 aslr_section_offset
= (random() % aslr_section_offset
) * aslr_section_size
;
402 aslr_page_offset
= random();
403 aslr_page_offset
%= vm_map_get_max_aslr_slide_pages(map
);
404 aslr_page_offset
<<= vm_map_page_shift(map
);
406 dyld_aslr_page_offset
= random();
407 dyld_aslr_page_offset
%= vm_map_get_max_loader_aslr_slide_pages(map
);
408 dyld_aslr_page_offset
<<= vm_map_page_shift(map
);
410 aslr_page_offset
+= aslr_section_offset
;
416 *result
= load_result_null
;
419 * re-set the bitness on the load result since we cleared the load result above.
421 result
->is64bit
= ((imgp
->ip_flags
& IMGPF_IS_64BIT
) == IMGPF_IS_64BIT
);
423 lret
= parse_machfile(vp
, map
, thread
, header
, file_offset
, macho_size
,
424 0, aslr_page_offset
, dyld_aslr_page_offset
, result
,
427 if (lret
!= LOAD_SUCCESS
) {
428 vm_map_deallocate(map
); /* will lose pmap reference too */
434 * On x86, for compatibility, don't enforce the hard page-zero restriction for 32-bit binaries.
436 if (!result
->is64bit
) {
437 enforce_hard_pagezero
= FALSE
;
441 * For processes with IMGPF_HIGH_BITS_ASLR, add a few random high bits
442 * to the start address for "anywhere" memory allocations.
444 #define VM_MAP_HIGH_START_BITS_COUNT 8
445 #define VM_MAP_HIGH_START_BITS_SHIFT 27
446 if (result
->is64bit
&&
447 (imgp
->ip_flags
& IMGPF_HIGH_BITS_ASLR
)) {
449 vm_map_offset_t high_start
;
451 random_bits
= random();
452 random_bits
&= (1 << VM_MAP_HIGH_START_BITS_COUNT
)-1;
453 high_start
= (((vm_map_offset_t
)random_bits
)
454 << VM_MAP_HIGH_START_BITS_SHIFT
);
455 vm_map_set_high_start(map
, high_start
);
457 #endif /* __x86_64__ */
460 * Check to see if the page zero is enforced by the map->min_offset.
462 if (enforce_hard_pagezero
&&
463 (vm_map_has_hard_pagezero(map
, 0x1000) == FALSE
)) {
465 if (!result
->is64bit
&& /* not 64-bit */
466 !(header
->flags
& MH_PIE
) && /* not PIE */
467 (vm_map_page_shift(map
) != FOURK_PAGE_SHIFT
||
468 PAGE_SHIFT
!= FOURK_PAGE_SHIFT
) && /* page size != 4KB */
469 result
->has_pagezero
&& /* has a "soft" page zero */
470 fourk_binary_compatibility_unsafe
) {
472 * For backwards compatibility of "4K" apps on
473 * a 16K system, do not enforce a hard page zero...
476 #endif /* __arm64__ */
478 vm_map_deallocate(map
); /* will lose pmap reference too */
479 return (LOAD_BADMACHO
);
483 vm_commit_pagezero_status(map
);
486 * If this is an exec, then we are going to destroy the old
487 * task, and it's correct to halt it; if it's spawn, the
488 * task is not yet running, and it makes no sense.
492 * Mark the task as halting and start the other
493 * threads towards terminating themselves. Then
494 * make sure any threads waiting for a process
495 * transition get informed that we are committed to
496 * this transition, and then finally complete the
497 * task halting (wait for threads and then cleanup
500 * NOTE: task_start_halt() makes sure that no new
501 * threads are created in the task during the transition.
502 * We need to mark the workqueue as exiting before we
503 * wait for threads to terminate (at the end of which
504 * we no longer have a prohibition on thread creation).
506 * Finally, clean up any lingering workqueue data structures
507 * that may have been left behind by the workqueue threads
508 * as they exited (and then clean up the work queue itself).
510 kret
= task_start_halt(task
);
511 if (kret
!= KERN_SUCCESS
) {
512 vm_map_deallocate(map
); /* will lose pmap reference too */
513 return (LOAD_FAILURE
);
515 proc_transcommit(p
, 0);
516 workqueue_mark_exiting(p
);
517 task_complete_halt(task
);
521 * Roll up accounting info to new task. The roll up is done after
522 * task_complete_halt to make sure the thread accounting info is
523 * rolled up to current_task.
525 task_rollup_accounting_info(get_threadtask(thread
), task
);
529 #ifdef CONFIG_32BIT_TELEMETRY
530 if (!result
->is64bit
) {
532 * This may not need to be an AST; we merely need to ensure that
533 * we gather telemetry at the point where all of the information
534 * that we want has been added to the process.
536 task_set_32bit_log_flag(get_threadtask(thread
));
537 act_set_astbsd(thread
);
539 #endif /* CONFIG_32BIT_TELEMETRY */
541 return(LOAD_SUCCESS
);
544 int macho_printf
= 0;
545 #define MACHO_PRINTF(args) \
547 if (macho_printf) { \
553 * The file size of a mach-o file is limited to 32 bits; this is because
554 * this is the limit on the kalloc() of enough bytes for a mach_header and
555 * the contents of its sizeofcmds, which is currently constrained to 32
556 * bits in the file format itself. We read into the kernel buffer the
557 * commands section, and then parse it in order to parse the mach-o file
558 * format load_command segment(s). We are only interested in a subset of
559 * the total set of possible commands. If "map"==VM_MAP_NULL or
560 * "thread"==THREAD_NULL, do not make permament VM modifications,
561 * just preflight the parse.
569 struct mach_header
*header
,
574 int64_t dyld_aslr_offset
,
575 load_result_t
*result
,
576 load_result_t
*binresult
,
577 struct image_params
*imgp
581 struct load_command
*lcp
;
582 struct dylinker_command
*dlp
= 0;
583 integer_t dlarchbits
= 0;
585 load_return_t ret
= LOAD_SUCCESS
;
587 vm_size_t alloc_size
, cmds_size
;
589 size_t oldoffset
; /* for overflow check */
591 proc_t p
= current_proc(); /* XXXX */
594 size_t mach_header_sz
= sizeof(struct mach_header
);
596 boolean_t got_code_signatures
= FALSE
;
597 boolean_t found_header_segment
= FALSE
;
598 boolean_t found_xhdr
= FALSE
;
600 boolean_t dyld_no_load_addr
= FALSE
;
601 boolean_t is_dyld
= FALSE
;
602 vm_map_offset_t effective_page_mask
= MAX(PAGE_MASK
, vm_map_page_mask(map
));
604 uint32_t pagezero_end
= 0;
605 uint32_t executable_end
= 0;
606 uint32_t writable_start
= 0;
607 vm_map_size_t effective_page_size
;
609 effective_page_size
= MAX(PAGE_SIZE
, vm_map_page_size(map
));
610 #endif /* __arm64__ */
612 if (header
->magic
== MH_MAGIC_64
||
613 header
->magic
== MH_CIGAM_64
) {
614 mach_header_sz
= sizeof(struct mach_header_64
);
618 * Break infinite recursion
621 return(LOAD_FAILURE
);
627 * Check to see if right machine type.
629 if (((cpu_type_t
)(header
->cputype
& ~CPU_ARCH_MASK
) != (cpu_type() & ~CPU_ARCH_MASK
)) ||
630 !grade_binary(header
->cputype
,
631 header
->cpusubtype
& ~CPU_SUBTYPE_MASK
))
632 return(LOAD_BADARCH
);
635 if (bootarg_no32exec
&& (header
->cputype
== CPU_TYPE_X86
)) {
636 return(LOAD_BADARCH_X86
);
640 abi64
= ((header
->cputype
& CPU_ARCH_ABI64
) == CPU_ARCH_ABI64
);
642 switch (header
->filetype
) {
646 return (LOAD_FAILURE
);
649 if (header
->flags
& MH_DYLDLINK
) {
650 /* Check properties of dynamic executables */
651 if (!(header
->flags
& MH_PIE
) && pie_required(header
->cputype
, header
->cpusubtype
& ~CPU_SUBTYPE_MASK
)) {
652 return (LOAD_FAILURE
);
654 result
->needs_dynlinker
= TRUE
;
656 /* Check properties of static executables (disallowed except for development) */
657 #if !(DEVELOPMENT || DEBUG)
658 return (LOAD_FAILURE
);
661 #endif /* CONFIG_EMBEDDED */
666 return (LOAD_FAILURE
);
672 return (LOAD_FAILURE
);
676 * Get the pager for the file.
678 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
680 /* ensure header + sizeofcmds falls within the file */
681 if (os_add_overflow(mach_header_sz
, header
->sizeofcmds
, &cmds_size
) ||
682 (off_t
)cmds_size
> macho_size
||
683 round_page_overflow(cmds_size
, &alloc_size
)) {
684 return LOAD_BADMACHO
;
688 * Map the load commands into kernel memory.
690 addr
= kalloc(alloc_size
);
695 error
= vn_rdwr(UIO_READ
, vp
, addr
, alloc_size
, file_offset
,
696 UIO_SYSSPACE
, 0, kauth_cred_get(), &resid
, p
);
698 kfree(addr
, alloc_size
);
703 /* We must be able to read in as much as the mach_header indicated */
704 kfree(addr
, alloc_size
);
705 return LOAD_BADMACHO
;
709 * For PIE and dyld, slide everything by the ASLR offset.
711 if ((header
->flags
& MH_PIE
) || is_dyld
) {
716 * Scan through the commands, processing each one as necessary.
717 * We parse in three passes through the headers:
718 * 0: determine if TEXT and DATA boundary can be page-aligned
719 * 1: thread state, uuid, code signature
721 * 3: dyld, encryption, check entry point
724 boolean_t slide_realign
= FALSE
;
727 slide_realign
= TRUE
;
731 for (pass
= 0; pass
<= 3; pass
++) {
733 if (pass
== 0 && !slide_realign
&& !is_dyld
) {
734 /* if we dont need to realign the slide or determine dyld's load
735 * address, pass 0 can be skipped */
737 } else if (pass
== 1) {
742 is_pie
= ((header
->flags
& MH_PIE
) != 0);
743 if (pagezero_end
!= 0 &&
744 pagezero_end
< effective_page_size
) {
745 /* need at least 1 page for PAGEZERO */
746 adjust
= effective_page_size
;
747 MACHO_PRINTF(("pagezero boundary at "
748 "0x%llx; adjust slide from "
749 "0x%llx to 0x%llx%s\n",
750 (uint64_t) pagezero_end
,
755 : " BUT NO PIE ****** :-(")));
758 pagezero_end
+= adjust
;
759 executable_end
+= adjust
;
760 writable_start
+= adjust
;
763 if (pagezero_end
!= 0) {
764 result
->has_pagezero
= TRUE
;
766 if (executable_end
== writable_start
&&
767 (executable_end
& effective_page_mask
) != 0 &&
768 (executable_end
& FOURK_PAGE_MASK
) == 0) {
771 * The TEXT/DATA boundary is 4K-aligned but
772 * not page-aligned. Adjust the slide to make
773 * it page-aligned and avoid having a page
774 * with both write and execute permissions.
777 (effective_page_size
-
778 (executable_end
& effective_page_mask
));
779 MACHO_PRINTF(("page-unaligned X-W boundary at "
780 "0x%llx; adjust slide from "
781 "0x%llx to 0x%llx%s\n",
782 (uint64_t) executable_end
,
787 : " BUT NO PIE ****** :-(")));
791 #endif /* __arm64__ */
793 if (dyld_no_load_addr
&& binresult
) {
795 * The dyld Mach-O does not specify a load address. Try to locate
796 * it right after the main binary. If binresult == NULL, load
797 * directly to the given slide.
799 slide
= vm_map_round_page(slide
+ binresult
->max_vm_addr
, effective_page_mask
);
804 * Check that the entry point is contained in an executable segments
806 if ((pass
== 3) && (!result
->using_lcmain
&& result
->validentry
== 0)) {
807 thread_state_initialize(thread
);
813 * Check that some segment maps the start of the mach-o file, which is
814 * needed by the dynamic loader to read the mach headers, etc.
816 if ((pass
== 3) && (found_header_segment
== FALSE
)) {
822 * Loop through each of the load_commands indicated by the
823 * Mach-O header; if an absurd value is provided, we just
824 * run off the end of the reserved section by incrementing
825 * the offset too far, so we are implicitly fail-safe.
827 offset
= mach_header_sz
;
828 ncmds
= header
->ncmds
;
832 /* ensure enough space for a minimal load command */
833 if (offset
+ sizeof(struct load_command
) > cmds_size
) {
839 * Get a pointer to the command.
841 lcp
= (struct load_command
*)(addr
+ offset
);
845 * Perform prevalidation of the struct load_command
846 * before we attempt to use its contents. Invalid
847 * values are ones which result in an overflow, or
848 * which can not possibly be valid commands, or which
849 * straddle or exist past the reserved section at the
850 * start of the image.
852 if (os_add_overflow(offset
, lcp
->cmdsize
, &offset
) ||
853 lcp
->cmdsize
< sizeof(struct load_command
) ||
854 offset
> cmds_size
) {
860 * Act on struct load_command's for which kernel
861 * intervention is required.
865 struct segment_command
*scp
= (struct segment_command
*) lcp
;
868 if (is_dyld
&& scp
->vmaddr
== 0 && scp
->fileoff
== 0) {
869 dyld_no_load_addr
= TRUE
;
870 if (!slide_realign
) {
871 /* got what we need, bail early on pass 0 */
879 if (scp
->initprot
== 0 && scp
->maxprot
== 0 && scp
->vmaddr
== 0) {
881 if (os_add3_overflow(scp
->vmaddr
, scp
->vmsize
, slide
, &pagezero_end
)) {
886 if (scp
->initprot
& VM_PROT_EXECUTE
) {
888 if (os_add3_overflow(scp
->vmaddr
, scp
->vmsize
, slide
, &executable_end
)) {
893 if (scp
->initprot
& VM_PROT_WRITE
) {
895 if (os_add_overflow(scp
->vmaddr
, slide
, &writable_start
)) {
900 #endif /* __arm64__ */
904 if (pass
== 1 && !strncmp(scp
->segname
, "__XHDR", sizeof(scp
->segname
))) {
913 * Having an LC_SEGMENT command for the
914 * wrong ABI is invalid <rdar://problem/11021230>
920 ret
= load_segment(lcp
,
930 if (ret
== LOAD_SUCCESS
&& scp
->fileoff
== 0 && scp
->filesize
> 0) {
931 /* Enforce a single segment mapping offset zero, with R+X
933 if (found_header_segment
||
934 ((scp
->initprot
& (VM_PROT_READ
|VM_PROT_EXECUTE
)) != (VM_PROT_READ
|VM_PROT_EXECUTE
))) {
938 found_header_segment
= TRUE
;
943 case LC_SEGMENT_64
: {
944 struct segment_command_64
*scp64
= (struct segment_command_64
*) lcp
;
947 if (is_dyld
&& scp64
->vmaddr
== 0 && scp64
->fileoff
== 0) {
948 dyld_no_load_addr
= TRUE
;
949 if (!slide_realign
) {
950 /* got what we need, bail early on pass 0 */
956 if (pass
== 1 && !strncmp(scp64
->segname
, "__XHDR", sizeof(scp64
->segname
))) {
965 * Having an LC_SEGMENT_64 command for the
966 * wrong ABI is invalid <rdar://problem/11021230>
972 ret
= load_segment(lcp
,
982 if (ret
== LOAD_SUCCESS
&& scp64
->fileoff
== 0 && scp64
->filesize
> 0) {
983 /* Enforce a single segment mapping offset zero, with R+X
985 if (found_header_segment
||
986 ((scp64
->initprot
& (VM_PROT_READ
|VM_PROT_EXECUTE
)) != (VM_PROT_READ
|VM_PROT_EXECUTE
))) {
990 found_header_segment
= TRUE
;
998 ret
= load_unixthread(
999 (struct thread_command
*) lcp
,
1010 (struct entry_point_command
*) lcp
,
1015 case LC_LOAD_DYLINKER
:
1018 if ((depth
== 1) && (dlp
== 0)) {
1019 dlp
= (struct dylinker_command
*)lcp
;
1020 dlarchbits
= (header
->cputype
& CPU_ARCH_MASK
);
1026 if (pass
== 1 && depth
== 1) {
1027 ret
= load_uuid((struct uuid_command
*) lcp
,
1028 (char *)addr
+ cmds_size
,
1032 case LC_CODE_SIGNATURE
:
1037 load signatures & store in uip
1038 set VM object "signed_pages"
1040 ret
= load_code_signature(
1041 (struct linkedit_data_command
*) lcp
,
1048 if (ret
!= LOAD_SUCCESS
) {
1049 printf("proc %d: load code signature error %d "
1050 "for file \"%s\"\n",
1051 p
->p_pid
, ret
, vp
->v_name
);
1053 * Allow injections to be ignored on devices w/o enforcement enabled
1055 if (!cs_enforcement(NULL
))
1056 ret
= LOAD_SUCCESS
; /* ignore error */
1059 got_code_signatures
= TRUE
;
1062 if (got_code_signatures
) {
1063 unsigned tainted
= CS_VALIDATE_TAINTED
;
1064 boolean_t valid
= FALSE
;
1069 printf("validating initial pages of %s\n", vp
->v_name
);
1071 while (off
< alloc_size
&& ret
== LOAD_SUCCESS
) {
1072 tainted
= CS_VALIDATE_TAINTED
;
1074 valid
= cs_validate_range(vp
,
1080 if (!valid
|| (tainted
& CS_VALIDATE_TAINTED
)) {
1082 printf("CODE SIGNING: %s[%d]: invalid initial page at offset %lld validated:%d tainted:%d csflags:0x%x\n",
1083 vp
->v_name
, p
->p_pid
, (long long)(file_offset
+ off
), valid
, tainted
, result
->csflags
);
1084 if (cs_enforcement(NULL
) ||
1085 (result
->csflags
& (CS_HARD
|CS_KILL
|CS_ENFORCEMENT
))) {
1088 result
->csflags
&= ~CS_VALID
;
1095 #if CONFIG_CODE_DECRYPTION
1096 case LC_ENCRYPTION_INFO
:
1097 case LC_ENCRYPTION_INFO_64
:
1100 ret
= set_code_unprotect(
1101 (struct encryption_info_command
*) lcp
,
1102 addr
, map
, slide
, vp
, file_offset
,
1103 header
->cputype
, header
->cpusubtype
);
1104 if (ret
!= LOAD_SUCCESS
) {
1105 os_reason_t load_failure_reason
= OS_REASON_NULL
;
1106 printf("proc %d: set_code_unprotect() error %d "
1107 "for file \"%s\"\n",
1108 p
->p_pid
, ret
, vp
->v_name
);
1110 * Don't let the app run if it's
1111 * encrypted but we failed to set up the
1112 * decrypter. If the keys are missing it will
1113 * return LOAD_DECRYPTFAIL.
1115 if (ret
== LOAD_DECRYPTFAIL
) {
1116 /* failed to load due to missing FP keys */
1118 p
->p_lflag
|= P_LTERM_DECRYPTFAIL
;
1121 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXITREASON_CREATE
) | DBG_FUNC_NONE
,
1122 p
->p_pid
, OS_REASON_EXEC
, EXEC_EXIT_REASON_FAIRPLAY_DECRYPT
, 0, 0);
1123 load_failure_reason
= os_reason_create(OS_REASON_EXEC
, EXEC_EXIT_REASON_FAIRPLAY_DECRYPT
);
1126 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXITREASON_CREATE
) | DBG_FUNC_NONE
,
1127 p
->p_pid
, OS_REASON_EXEC
, EXEC_EXIT_REASON_DECRYPT
, 0, 0);
1128 load_failure_reason
= os_reason_create(OS_REASON_EXEC
, EXEC_EXIT_REASON_DECRYPT
);
1131 assert(load_failure_reason
!= OS_REASON_NULL
);
1132 psignal_with_reason(p
, SIGKILL
, load_failure_reason
);
1137 /* Other commands are ignored by the kernel */
1141 if (ret
!= LOAD_SUCCESS
)
1144 if (ret
!= LOAD_SUCCESS
)
1148 if (ret
== LOAD_SUCCESS
) {
1149 if(!got_code_signatures
&& cs_enforcement(NULL
)) {
1153 /* Make sure if we need dyld, we got it */
1154 if (result
->needs_dynlinker
&& !dlp
) {
1158 if ((ret
== LOAD_SUCCESS
) && (dlp
!= 0)) {
1160 * load the dylinker, and slide it by the independent DYLD ASLR
1161 * offset regardless of the PIE-ness of the main binary.
1163 ret
= load_dylinker(dlp
, dlarchbits
, map
, thread
, depth
,
1164 dyld_aslr_offset
, result
, imgp
);
1167 if ((ret
== LOAD_SUCCESS
) && (depth
== 1)) {
1168 if (result
->thread_count
== 0) {
1172 if (result
->needs_dynlinker
&& !(result
->csflags
& CS_DYLD_PLATFORM
)) {
1179 if (ret
== LOAD_BADMACHO
&& found_xhdr
) {
1180 ret
= LOAD_BADMACHO_UPX
;
1183 kfree(addr
, alloc_size
);
1188 #if CONFIG_CODE_DECRYPTION
1190 #define APPLE_UNPROTECTED_HEADER_SIZE (3 * 4096)
1192 static load_return_t
1193 unprotect_dsmos_segment(
1199 vm_map_offset_t map_addr
,
1200 vm_map_size_t map_size
)
1205 * The first APPLE_UNPROTECTED_HEADER_SIZE bytes (from offset 0 of
1206 * this part of a Universal binary) are not protected...
1207 * The rest needs to be "transformed".
1209 if (file_off
<= APPLE_UNPROTECTED_HEADER_SIZE
&&
1210 file_off
+ file_size
<= APPLE_UNPROTECTED_HEADER_SIZE
) {
1211 /* it's all unprotected, nothing to do... */
1214 if (file_off
<= APPLE_UNPROTECTED_HEADER_SIZE
) {
1216 * We start mapping in the unprotected area.
1217 * Skip the unprotected part...
1219 vm_map_offset_t delta
;
1221 delta
= APPLE_UNPROTECTED_HEADER_SIZE
;
1226 /* ... transform the rest of the mapping. */
1227 struct pager_crypt_info crypt_info
;
1228 crypt_info
.page_decrypt
= dsmos_page_transform
;
1229 crypt_info
.crypt_ops
= NULL
;
1230 crypt_info
.crypt_end
= NULL
;
1231 #pragma unused(vp, macho_offset)
1232 crypt_info
.crypt_ops
= (void *)0x2e69cf40;
1233 vm_map_offset_t crypto_backing_offset
;
1234 crypto_backing_offset
= -1; /* i.e. use map entry's offset */
1235 #if VM_MAP_DEBUG_APPLE_PROTECT
1236 if (vm_map_debug_apple_protect
) {
1239 printf("APPLE_PROTECT: %d[%s] map %p "
1240 "[0x%llx:0x%llx] %s(%s)\n",
1241 p
->p_pid
, p
->p_comm
, map
,
1242 (uint64_t) map_addr
,
1243 (uint64_t) (map_addr
+ map_size
),
1244 __FUNCTION__
, vp
->v_name
);
1246 #endif /* VM_MAP_DEBUG_APPLE_PROTECT */
1248 /* The DSMOS pager can only be used by apple signed code */
1249 struct cs_blob
* blob
= csvnode_get_blob(vp
, file_off
);
1250 if( blob
== NULL
|| !blob
->csb_platform_binary
|| blob
->csb_platform_path
)
1252 return LOAD_FAILURE
;
1255 kr
= vm_map_apple_protected(map
,
1257 map_addr
+ map_size
,
1258 crypto_backing_offset
,
1262 if (kr
!= KERN_SUCCESS
) {
1263 return LOAD_FAILURE
;
1265 return LOAD_SUCCESS
;
1267 #else /* CONFIG_CODE_DECRYPTION */
1268 static load_return_t
1269 unprotect_dsmos_segment(
1270 __unused
uint64_t file_off
,
1271 __unused
uint64_t file_size
,
1272 __unused
struct vnode
*vp
,
1273 __unused off_t macho_offset
,
1274 __unused vm_map_t map
,
1275 __unused vm_map_offset_t map_addr
,
1276 __unused vm_map_size_t map_size
)
1278 return LOAD_SUCCESS
;
1280 #endif /* CONFIG_CODE_DECRYPTION */
1285 * Maps a Mach-O segment, taking care of mis-alignment (wrt the system
1286 * page size) issues.
1288 * The mapping might result in 1, 2 or 3 map entries:
1289 * 1. for the first page, which could be overlap with the previous
1291 * 2. for the center (if applicable),
1292 * 3. for the last page, which could overlap with the next mapping.
1294 * For each of those map entries, we might have to interpose a
1295 * "fourk_pager" to deal with mis-alignment wrt the system page size,
1296 * either in the mapping address and/or size or the file offset and/or
1298 * The "fourk_pager" itself would be mapped with proper alignment
1299 * wrt the system page size and would then be populated with the
1300 * information about the intended mapping, with a "4KB" granularity.
1302 static kern_return_t
1305 vm_map_offset_t vm_start
,
1306 vm_map_offset_t vm_end
,
1307 memory_object_control_t control
,
1308 vm_map_offset_t file_start
,
1309 vm_map_offset_t file_end
,
1313 vm_map_offset_t cur_offset
, cur_start
, cur_end
;
1315 vm_map_offset_t effective_page_mask
;
1316 vm_map_kernel_flags_t vmk_flags
, cur_vmk_flags
;
1318 if (vm_end
< vm_start
||
1319 file_end
< file_start
) {
1320 return LOAD_BADMACHO
;
1322 if (vm_end
== vm_start
||
1323 file_end
== file_start
) {
1324 /* nothing to map... */
1325 return LOAD_SUCCESS
;
1328 effective_page_mask
= MAX(PAGE_MASK
, vm_map_page_mask(map
));
1330 vmk_flags
= VM_MAP_KERNEL_FLAGS_NONE
;
1331 if (vm_map_page_aligned(vm_start
, effective_page_mask
) &&
1332 vm_map_page_aligned(vm_end
, effective_page_mask
) &&
1333 vm_map_page_aligned(file_start
, effective_page_mask
) &&
1334 vm_map_page_aligned(file_end
, effective_page_mask
)) {
1335 /* all page-aligned and map-aligned: proceed */
1338 /* use an intermediate "4K" pager */
1339 vmk_flags
.vmkf_fourk
= TRUE
;
1340 #else /* __arm64__ */
1341 panic("map_segment: unexpected mis-alignment "
1342 "vm[0x%llx:0x%llx] file[0x%llx:0x%llx]\n",
1343 (uint64_t) vm_start
,
1345 (uint64_t) file_start
,
1346 (uint64_t) file_end
);
1347 #endif /* __arm64__ */
1351 cur_start
= vm_start
;
1354 if (!vm_map_page_aligned(vm_start
, effective_page_mask
)) {
1355 /* one 4K pager for the 1st page */
1356 cur_end
= vm_map_round_page(cur_start
, effective_page_mask
);
1357 if (cur_end
> vm_end
) {
1358 cur_end
= vm_start
+ (file_end
- file_start
);
1360 if (control
!= MEMORY_OBJECT_CONTROL_NULL
) {
1361 ret
= vm_map_enter_mem_object_control(
1364 cur_end
- cur_start
,
1365 (mach_vm_offset_t
)0,
1368 VM_KERN_MEMORY_NONE
,
1370 file_start
+ cur_offset
,
1373 VM_INHERIT_DEFAULT
);
1375 ret
= vm_map_enter_mem_object(
1378 cur_end
- cur_start
,
1379 (mach_vm_offset_t
)0,
1382 VM_KERN_MEMORY_NONE
,
1387 VM_INHERIT_DEFAULT
);
1389 if (ret
!= KERN_SUCCESS
) {
1390 return (LOAD_NOSPACE
);
1392 cur_offset
+= cur_end
- cur_start
;
1394 #endif /* __arm64__ */
1395 if (cur_end
>= vm_start
+ (file_end
- file_start
)) {
1396 /* all mapped: done */
1399 if (vm_map_round_page(cur_end
, effective_page_mask
) >=
1400 vm_map_trunc_page(vm_start
+ (file_end
- file_start
),
1401 effective_page_mask
)) {
1404 cur_start
= cur_end
;
1405 if ((vm_start
& effective_page_mask
) !=
1406 (file_start
& effective_page_mask
)) {
1407 /* one 4K pager for the middle */
1408 cur_vmk_flags
= vmk_flags
;
1410 /* regular mapping for the middle */
1411 cur_vmk_flags
= VM_MAP_KERNEL_FLAGS_NONE
;
1413 cur_end
= vm_map_trunc_page(vm_start
+ (file_end
-
1415 effective_page_mask
);
1416 if (control
!= MEMORY_OBJECT_CONTROL_NULL
) {
1417 ret
= vm_map_enter_mem_object_control(
1420 cur_end
- cur_start
,
1421 (mach_vm_offset_t
)0,
1424 VM_KERN_MEMORY_NONE
,
1426 file_start
+ cur_offset
,
1429 VM_INHERIT_DEFAULT
);
1431 ret
= vm_map_enter_mem_object(
1434 cur_end
- cur_start
,
1435 (mach_vm_offset_t
)0,
1438 VM_KERN_MEMORY_NONE
,
1443 VM_INHERIT_DEFAULT
);
1445 if (ret
!= KERN_SUCCESS
) {
1446 return (LOAD_NOSPACE
);
1448 cur_offset
+= cur_end
- cur_start
;
1450 if (cur_end
>= vm_start
+ (file_end
- file_start
)) {
1451 /* all mapped: done */
1454 cur_start
= cur_end
;
1456 if (!vm_map_page_aligned(vm_start
+ (file_end
- file_start
),
1457 effective_page_mask
)) {
1458 /* one 4K pager for the last page */
1459 cur_end
= vm_start
+ (file_end
- file_start
);
1460 if (control
!= MEMORY_OBJECT_CONTROL_NULL
) {
1461 ret
= vm_map_enter_mem_object_control(
1464 cur_end
- cur_start
,
1465 (mach_vm_offset_t
)0,
1468 VM_KERN_MEMORY_NONE
,
1470 file_start
+ cur_offset
,
1473 VM_INHERIT_DEFAULT
);
1475 ret
= vm_map_enter_mem_object(
1478 cur_end
- cur_start
,
1479 (mach_vm_offset_t
)0,
1482 VM_KERN_MEMORY_NONE
,
1487 VM_INHERIT_DEFAULT
);
1489 if (ret
!= KERN_SUCCESS
) {
1490 return (LOAD_NOSPACE
);
1492 cur_offset
+= cur_end
- cur_start
;
1494 #endif /* __arm64__ */
1496 assert(cur_end
>= vm_start
+ (file_end
- file_start
));
1497 return LOAD_SUCCESS
;
1503 struct load_command
*lcp
,
1511 load_result_t
*result
)
1513 struct segment_command_64 segment_command
, *scp
;
1515 vm_map_size_t delta_size
;
1518 size_t segment_command_size
, total_section_size
,
1519 single_section_size
;
1520 vm_map_offset_t file_offset
, file_size
;
1521 vm_map_offset_t vm_offset
, vm_size
;
1522 vm_map_offset_t vm_start
, vm_end
, vm_end_aligned
;
1523 vm_map_offset_t file_start
, file_end
;
1526 vm_map_size_t effective_page_size
;
1527 vm_map_offset_t effective_page_mask
;
1529 vm_map_kernel_flags_t vmk_flags
;
1530 boolean_t fourk_align
;
1531 #endif /* __arm64__ */
1533 effective_page_size
= MAX(PAGE_SIZE
, vm_map_page_size(map
));
1534 effective_page_mask
= MAX(PAGE_MASK
, vm_map_page_mask(map
));
1537 if (LC_SEGMENT_64
== lcp
->cmd
) {
1538 segment_command_size
= sizeof(struct segment_command_64
);
1539 single_section_size
= sizeof(struct section_64
);
1541 /* 64-bit binary: should already be 16K-aligned */
1542 fourk_align
= FALSE
;
1543 #endif /* __arm64__ */
1545 segment_command_size
= sizeof(struct segment_command
);
1546 single_section_size
= sizeof(struct section
);
1548 /* 32-bit binary: might need 4K-alignment */
1549 if (effective_page_size
!= FOURK_PAGE_SIZE
) {
1550 /* not using 4K page size: need fourk_pager */
1554 /* using 4K page size: no need for re-alignment */
1555 fourk_align
= FALSE
;
1557 #endif /* __arm64__ */
1559 if (lcp
->cmdsize
< segment_command_size
)
1560 return (LOAD_BADMACHO
);
1561 total_section_size
= lcp
->cmdsize
- segment_command_size
;
1563 if (LC_SEGMENT_64
== lcp
->cmd
) {
1564 scp
= (struct segment_command_64
*)lcp
;
1566 scp
= &segment_command
;
1567 widen_segment_command((struct segment_command
*)lcp
, scp
);
1571 MACHO_PRINTF(("+++ load_segment %s "
1572 "vm[0x%llx:0x%llx] file[0x%llx:0x%llx] "
1573 "prot %d/%d flags 0x%x\n",
1575 (uint64_t)(slide
+ scp
->vmaddr
),
1576 (uint64_t)(slide
+ scp
->vmaddr
+ scp
->vmsize
),
1577 pager_offset
+ scp
->fileoff
,
1578 pager_offset
+ scp
->fileoff
+ scp
->filesize
,
1585 * Make sure what we get from the file is really ours (as specified
1588 if (scp
->fileoff
+ scp
->filesize
< scp
->fileoff
||
1589 scp
->fileoff
+ scp
->filesize
> (uint64_t)macho_size
) {
1590 return (LOAD_BADMACHO
);
1593 * Ensure that the number of sections specified would fit
1594 * within the load command size.
1596 if (total_section_size
/ single_section_size
< scp
->nsects
) {
1597 return (LOAD_BADMACHO
);
1600 * Make sure the segment is page-aligned in the file.
1602 file_offset
= pager_offset
+ scp
->fileoff
; /* limited to 32 bits */
1603 file_size
= scp
->filesize
;
1606 if ((file_offset
& FOURK_PAGE_MASK
) != 0) {
1608 * we can't mmap() it if it's not at least 4KB-aligned
1611 return LOAD_BADMACHO
;
1614 #endif /* __arm64__ */
1615 if ((file_offset
& PAGE_MASK_64
) != 0 ||
1616 /* we can't mmap() it if it's not page-aligned in the file */
1617 (file_offset
& vm_map_page_mask(map
)) != 0) {
1619 * The 1st test would have failed if the system's page size
1620 * was what this process believe is the page size, so let's
1621 * fail here too for the sake of consistency.
1623 return (LOAD_BADMACHO
);
1627 * If we have a code signature attached for this slice
1628 * require that the segments are within the signed part
1631 if (result
->cs_end_offset
&&
1632 result
->cs_end_offset
< (off_t
)scp
->fileoff
&&
1633 result
->cs_end_offset
- scp
->fileoff
< scp
->filesize
)
1636 printf("section outside code signature\n");
1637 return LOAD_BADMACHO
;
1640 vm_offset
= scp
->vmaddr
+ slide
;
1641 vm_size
= scp
->vmsize
;
1644 return (LOAD_SUCCESS
);
1645 if (scp
->vmaddr
== 0 &&
1648 (scp
->initprot
& VM_PROT_ALL
) == VM_PROT_NONE
&&
1649 (scp
->maxprot
& VM_PROT_ALL
) == VM_PROT_NONE
) {
1651 * For PIE, extend page zero rather than moving it. Extending
1652 * page zero keeps early allocations from falling predictably
1653 * between the end of page zero and the beginning of the first
1657 * This is a "page zero" segment: it starts at address 0,
1658 * is not mapped from the binary file and is not accessible.
1659 * User-space should never be able to access that memory, so
1660 * make it completely off limits by raising the VM map's
1663 vm_end
= vm_offset
+ vm_size
;
1664 if (vm_end
< vm_offset
) {
1665 return (LOAD_BADMACHO
);
1668 MACHO_PRINTF(("++++++ load_segment: "
1669 "page_zero up to 0x%llx\n",
1670 (uint64_t) vm_end
));
1674 /* raise min_offset as much as page-alignment allows */
1675 vm_end_aligned
= vm_map_trunc_page(vm_end
,
1676 effective_page_mask
);
1678 #endif /* __arm64__ */
1680 vm_end
= vm_map_round_page(vm_end
,
1682 vm_end_aligned
= vm_end
;
1684 ret
= vm_map_raise_min_offset(map
,
1688 vm_end
> vm_end_aligned
) {
1689 /* use fourk_pager to map the rest of pagezero */
1690 assert(fourk_align
);
1691 vmk_flags
= VM_MAP_KERNEL_FLAGS_NONE
;
1692 vmk_flags
.vmkf_fourk
= TRUE
;
1693 ret
= vm_map_enter_mem_object(
1696 vm_end
- vm_end_aligned
,
1697 (mach_vm_offset_t
) 0, /* mask */
1700 VM_KERN_MEMORY_NONE
,
1704 (scp
->initprot
& VM_PROT_ALL
),
1705 (scp
->maxprot
& VM_PROT_ALL
),
1706 VM_INHERIT_DEFAULT
);
1708 #endif /* __arm64__ */
1710 if (ret
!= KERN_SUCCESS
) {
1711 return (LOAD_FAILURE
);
1713 return (LOAD_SUCCESS
);
1716 /* not PAGEZERO: should not be mapped at address 0 */
1717 if (filetype
!= MH_DYLINKER
&& scp
->vmaddr
== 0) {
1718 return LOAD_BADMACHO
;
1720 #endif /* CONFIG_EMBEDDED */
1726 file_start
= vm_map_trunc_page(file_offset
,
1728 file_end
= vm_map_round_page(file_offset
+ file_size
,
1730 vm_start
= vm_map_trunc_page(vm_offset
,
1732 vm_end
= vm_map_round_page(vm_offset
+ vm_size
,
1734 if (!strncmp(scp
->segname
, "__LINKEDIT", 11) &&
1735 page_aligned(file_start
) &&
1736 vm_map_page_aligned(file_start
, vm_map_page_mask(map
)) &&
1737 page_aligned(vm_start
) &&
1738 vm_map_page_aligned(vm_start
, vm_map_page_mask(map
))) {
1739 /* XXX last segment: ignore mis-aligned tail */
1740 file_end
= vm_map_round_page(file_end
,
1741 effective_page_mask
);
1742 vm_end
= vm_map_round_page(vm_end
,
1743 effective_page_mask
);
1746 #endif /* __arm64__ */
1748 file_start
= vm_map_trunc_page(file_offset
,
1749 effective_page_mask
);
1750 file_end
= vm_map_round_page(file_offset
+ file_size
,
1751 effective_page_mask
);
1752 vm_start
= vm_map_trunc_page(vm_offset
,
1753 effective_page_mask
);
1754 vm_end
= vm_map_round_page(vm_offset
+ vm_size
,
1755 effective_page_mask
);
1758 if (vm_start
< result
->min_vm_addr
)
1759 result
->min_vm_addr
= vm_start
;
1760 if (vm_end
> result
->max_vm_addr
)
1761 result
->max_vm_addr
= vm_end
;
1763 if (map
== VM_MAP_NULL
)
1764 return (LOAD_SUCCESS
);
1767 initprot
= (scp
->initprot
) & VM_PROT_ALL
;
1768 maxprot
= (scp
->maxprot
) & VM_PROT_ALL
;
1770 * Map a copy of the file into the address space.
1773 MACHO_PRINTF(("++++++ load_segment: "
1774 "mapping at vm [0x%llx:0x%llx] of "
1775 "file [0x%llx:0x%llx]\n",
1776 (uint64_t) vm_start
,
1778 (uint64_t) file_start
,
1779 (uint64_t) file_end
));
1781 ret
= map_segment(map
,
1790 return LOAD_NOSPACE
;
1795 * If the file didn't end on a page boundary,
1796 * we need to zero the leftover.
1798 delta_size
= map_size
- scp
->filesize
;
1799 if (delta_size
> 0) {
1800 mach_vm_offset_t tmp
;
1802 ret
= mach_vm_allocate_kernel(kernel_map
, &tmp
, delta_size
, VM_FLAGS_ANYWHERE
, VM_KERN_MEMORY_BSD
);
1803 if (ret
!= KERN_SUCCESS
) {
1804 return(LOAD_RESOURCE
);
1807 if (copyout(tmp
, map_addr
+ scp
->filesize
,
1809 (void) mach_vm_deallocate(
1810 kernel_map
, tmp
, delta_size
);
1811 return (LOAD_FAILURE
);
1814 (void) mach_vm_deallocate(kernel_map
, tmp
, delta_size
);
1820 * If the virtual size of the segment is greater
1821 * than the size from the file, we need to allocate
1822 * zero fill memory for the rest.
1824 if ((vm_end
- vm_start
) > (file_end
- file_start
)) {
1825 delta_size
= (vm_end
- vm_start
) - (file_end
- file_start
);
1829 if (delta_size
> 0) {
1830 mach_vm_offset_t tmp
;
1832 tmp
= vm_start
+ (file_end
- file_start
);
1834 MACHO_PRINTF(("++++++ load_segment: "
1835 "delta mapping vm [0x%llx:0x%llx]\n",
1837 (uint64_t) (tmp
+ delta_size
)));
1839 kr
= map_segment(map
,
1842 MEMORY_OBJECT_CONTROL_NULL
,
1847 if (kr
!= KERN_SUCCESS
) {
1848 return(LOAD_NOSPACE
);
1852 if ( (scp
->fileoff
== 0) && (scp
->filesize
!= 0) )
1853 result
->mach_header
= vm_offset
;
1855 if (scp
->flags
& SG_PROTECTED_VERSION_1
) {
1856 ret
= unprotect_dsmos_segment(file_start
,
1857 file_end
- file_start
,
1863 if (ret
!= LOAD_SUCCESS
) {
1870 if (LOAD_SUCCESS
== ret
&&
1871 filetype
== MH_DYLINKER
&&
1872 result
->all_image_info_addr
== MACH_VM_MIN_ADDRESS
) {
1873 note_all_image_info_section(scp
,
1874 LC_SEGMENT_64
== lcp
->cmd
,
1875 single_section_size
,
1876 ((const char *)lcp
+
1877 segment_command_size
),
1882 if (result
->entry_point
!= MACH_VM_MIN_ADDRESS
) {
1883 if ((result
->entry_point
>= vm_offset
) && (result
->entry_point
< (vm_offset
+ vm_size
))) {
1884 if ((scp
->initprot
& (VM_PROT_READ
|VM_PROT_EXECUTE
)) == (VM_PROT_READ
|VM_PROT_EXECUTE
)) {
1885 result
->validentry
= 1;
1887 /* right range but wrong protections, unset if previously validated */
1888 result
->validentry
= 0;
1899 struct uuid_command
*uulp
,
1901 load_result_t
*result
1905 * We need to check the following for this command:
1906 * - The command size should be atleast the size of struct uuid_command
1907 * - The UUID part of the command should be completely within the mach-o header
1910 if ((uulp
->cmdsize
< sizeof(struct uuid_command
)) ||
1911 (((char *)uulp
+ sizeof(struct uuid_command
)) > command_end
)) {
1912 return (LOAD_BADMACHO
);
1915 memcpy(&result
->uuid
[0], &uulp
->uuid
[0], sizeof(result
->uuid
));
1916 return (LOAD_SUCCESS
);
1922 struct entry_point_command
*epc
,
1925 load_result_t
*result
1928 mach_vm_offset_t addr
;
1931 if (epc
->cmdsize
< sizeof(*epc
))
1932 return (LOAD_BADMACHO
);
1933 if (result
->thread_count
!= 0) {
1934 return (LOAD_FAILURE
);
1937 if (thread
== THREAD_NULL
)
1938 return (LOAD_SUCCESS
);
1941 * LC_MAIN specifies stack size but not location.
1942 * Add guard page to allocation size (MAXSSIZ includes guard page).
1944 if (epc
->stacksize
) {
1945 if (os_add_overflow(epc
->stacksize
, 4*PAGE_SIZE
, &result
->user_stack_size
)) {
1947 * We are going to immediately throw away this result, but we want
1948 * to make sure we aren't loading a dangerously close to
1949 * overflowing value, since this will have a guard page added to it
1950 * and be rounded to page boundaries
1952 return LOAD_BADMACHO
;
1954 result
->user_stack_size
= epc
->stacksize
;
1955 if (os_add_overflow(epc
->stacksize
, PAGE_SIZE
, &result
->user_stack_alloc_size
)) {
1956 return LOAD_BADMACHO
;
1959 result
->user_stack_alloc_size
= MAXSSIZ
;
1962 /* use default location for stack */
1963 ret
= thread_userstackdefault(&addr
, result
->is64bit
);
1964 if (ret
!= KERN_SUCCESS
)
1965 return(LOAD_FAILURE
);
1967 /* The stack slides down from the default location */
1968 result
->user_stack
= addr
;
1969 result
->user_stack
-= slide
;
1971 if (result
->using_lcmain
|| result
->entry_point
!= MACH_VM_MIN_ADDRESS
) {
1972 /* Already processed LC_MAIN or LC_UNIXTHREAD */
1973 return (LOAD_FAILURE
);
1976 /* kernel does *not* use entryoff from LC_MAIN. Dyld uses it. */
1977 result
->needs_dynlinker
= TRUE
;
1978 result
->using_lcmain
= TRUE
;
1980 ret
= thread_state_initialize( thread
);
1981 if (ret
!= KERN_SUCCESS
) {
1982 return(LOAD_FAILURE
);
1985 result
->unixproc
= TRUE
;
1986 result
->thread_count
++;
1988 return(LOAD_SUCCESS
);
1995 struct thread_command
*tcp
,
1998 load_result_t
*result
2003 mach_vm_offset_t addr
;
2005 if (tcp
->cmdsize
< sizeof(*tcp
))
2006 return (LOAD_BADMACHO
);
2007 if (result
->thread_count
!= 0) {
2008 return (LOAD_FAILURE
);
2011 if (thread
== THREAD_NULL
)
2012 return (LOAD_SUCCESS
);
2014 ret
= load_threadstack(thread
,
2015 (uint32_t *)(((vm_offset_t
)tcp
) +
2016 sizeof(struct thread_command
)),
2017 tcp
->cmdsize
- sizeof(struct thread_command
),
2018 &addr
, &customstack
, result
);
2019 if (ret
!= LOAD_SUCCESS
)
2022 /* LC_UNIXTHREAD optionally specifies stack size and location */
2025 result
->user_stack_alloc_size
= MAXSSIZ
;
2028 /* The stack slides down from the default location */
2029 result
->user_stack
= addr
;
2030 result
->user_stack
-= slide
;
2032 ret
= load_threadentry(thread
,
2033 (uint32_t *)(((vm_offset_t
)tcp
) +
2034 sizeof(struct thread_command
)),
2035 tcp
->cmdsize
- sizeof(struct thread_command
),
2037 if (ret
!= LOAD_SUCCESS
)
2040 if (result
->using_lcmain
|| result
->entry_point
!= MACH_VM_MIN_ADDRESS
) {
2041 /* Already processed LC_MAIN or LC_UNIXTHREAD */
2042 return (LOAD_FAILURE
);
2045 result
->entry_point
= addr
;
2046 result
->entry_point
+= slide
;
2048 ret
= load_threadstate(thread
,
2049 (uint32_t *)(((vm_offset_t
)tcp
) + sizeof(struct thread_command
)),
2050 tcp
->cmdsize
- sizeof(struct thread_command
),
2052 if (ret
!= LOAD_SUCCESS
)
2055 result
->unixproc
= TRUE
;
2056 result
->thread_count
++;
2058 return(LOAD_SUCCESS
);
2066 uint32_t total_size
,
2067 load_result_t
*result
2072 uint32_t thread_size
;
2073 uint32_t *local_ts
= NULL
;
2074 uint32_t local_ts_size
= 0;
2079 if (total_size
> 0) {
2080 local_ts_size
= total_size
;
2081 local_ts
= kalloc(local_ts_size
);
2082 if (local_ts
== NULL
) {
2083 return LOAD_FAILURE
;
2085 memcpy(local_ts
, ts
, local_ts_size
);
2090 * Validate the new thread state; iterate through the state flavors in
2092 * XXX: we should validate the machine state here, to avoid failing at
2093 * activation time where we can't bail out cleanly.
2095 while (total_size
> 0) {
2099 if (os_add_and_mul_overflow(size
, 2, sizeof(uint32_t), &thread_size
) ||
2100 os_sub_overflow(total_size
, thread_size
, &total_size
)) {
2101 ret
= LOAD_BADMACHO
;
2105 ts
+= size
; /* ts is a (uint32_t *) */
2108 result
->threadstate
= local_ts
;
2109 result
->threadstate_sz
= local_ts_size
;
2110 return LOAD_SUCCESS
;
2114 kfree(local_ts
, local_ts_size
);
2124 uint32_t total_size
,
2125 mach_vm_offset_t
*user_stack
,
2127 load_result_t
*result
2133 uint32_t stack_size
;
2135 while (total_size
> 0) {
2138 if (UINT32_MAX
-2 < size
||
2139 UINT32_MAX
/sizeof(uint32_t) < size
+2)
2140 return (LOAD_BADMACHO
);
2141 stack_size
= (size
+2)*sizeof(uint32_t);
2142 if (stack_size
> total_size
)
2143 return(LOAD_BADMACHO
);
2144 total_size
-= stack_size
;
2147 * Third argument is a kernel space pointer; it gets cast
2148 * to the appropriate type in thread_userstack() based on
2149 * the value of flavor.
2151 ret
= thread_userstack(thread
, flavor
, (thread_state_t
)ts
, size
, user_stack
, customstack
, result
->is64bit
);
2152 if (ret
!= KERN_SUCCESS
) {
2153 return(LOAD_FAILURE
);
2155 ts
+= size
; /* ts is a (uint32_t *) */
2157 return(LOAD_SUCCESS
);
2165 uint32_t total_size
,
2166 mach_vm_offset_t
*entry_point
2172 uint32_t entry_size
;
2175 * Set the thread state.
2177 *entry_point
= MACH_VM_MIN_ADDRESS
;
2178 while (total_size
> 0) {
2181 if (UINT32_MAX
-2 < size
||
2182 UINT32_MAX
/sizeof(uint32_t) < size
+2)
2183 return (LOAD_BADMACHO
);
2184 entry_size
= (size
+2)*sizeof(uint32_t);
2185 if (entry_size
> total_size
)
2186 return(LOAD_BADMACHO
);
2187 total_size
-= entry_size
;
2189 * Third argument is a kernel space pointer; it gets cast
2190 * to the appropriate type in thread_entrypoint() based on
2191 * the value of flavor.
2193 ret
= thread_entrypoint(thread
, flavor
, (thread_state_t
)ts
, size
, entry_point
);
2194 if (ret
!= KERN_SUCCESS
) {
2195 return(LOAD_FAILURE
);
2197 ts
+= size
; /* ts is a (uint32_t *) */
2199 return(LOAD_SUCCESS
);
2203 struct nameidata __nid
;
2204 union macho_vnode_header
{
2205 struct mach_header mach_header
;
2206 struct fat_header fat_header
;
2211 #define DEFAULT_DYLD_PATH "/usr/lib/dyld"
2213 #if (DEVELOPMENT || DEBUG)
2214 extern char dyld_alt_path
[];
2215 extern int use_alt_dyld
;
2218 static uint64_t get_va_fsid(struct vnode_attr
*vap
)
2220 if (VATTR_IS_SUPPORTED(vap
, va_fsid64
)) {
2221 return *(uint64_t *)&vap
->va_fsid64
;
2223 return vap
->va_fsid
;
2227 static load_return_t
2229 struct dylinker_command
*lcp
,
2235 load_result_t
*result
,
2236 struct image_params
*imgp
2240 struct vnode
*vp
= NULLVP
; /* set by get_macho_vnode() */
2241 struct mach_header
*header
;
2242 off_t file_offset
= 0; /* set by get_macho_vnode() */
2243 off_t macho_size
= 0; /* set by get_macho_vnode() */
2244 load_result_t
*myresult
;
2246 struct macho_data
*macho_data
;
2248 struct mach_header __header
;
2249 load_result_t __myresult
;
2250 struct macho_data __macho_data
;
2253 if (lcp
->cmdsize
< sizeof(*lcp
) || lcp
->name
.offset
>= lcp
->cmdsize
)
2254 return LOAD_BADMACHO
;
2256 name
= (const char *)lcp
+ lcp
->name
.offset
;
2258 /* Check for a proper null terminated string. */
2259 size_t maxsz
= lcp
->cmdsize
- lcp
->name
.offset
;
2260 size_t namelen
= strnlen(name
, maxsz
);
2261 if (namelen
>= maxsz
) {
2262 return LOAD_BADMACHO
;
2265 #if (DEVELOPMENT || DEBUG)
2269 * If an alternate dyld has been specified via boot args, check
2270 * to see if PROC_UUID_ALT_DYLD_POLICY has been set on this
2271 * executable and redirect the kernel to load that linker.
2276 uint32_t policy_flags
= 0;
2277 int32_t policy_gencount
= 0;
2279 policy_error
= proc_uuid_policy_lookup(result
->uuid
, &policy_flags
, &policy_gencount
);
2280 if (policy_error
== 0) {
2281 if (policy_flags
& PROC_UUID_ALT_DYLD_POLICY
) {
2282 name
= dyld_alt_path
;
2288 #if !(DEVELOPMENT || DEBUG)
2289 if (0 != strcmp(name
, DEFAULT_DYLD_PATH
)) {
2290 return (LOAD_BADMACHO
);
2294 /* Allocate wad-of-data from heap to reduce excessively deep stacks */
2296 MALLOC(dyld_data
, void *, sizeof (*dyld_data
), M_TEMP
, M_WAITOK
);
2297 header
= &dyld_data
->__header
;
2298 myresult
= &dyld_data
->__myresult
;
2299 macho_data
= &dyld_data
->__macho_data
;
2301 ret
= get_macho_vnode(name
, archbits
, header
,
2302 &file_offset
, &macho_size
, macho_data
, &vp
);
2306 *myresult
= load_result_null
;
2307 myresult
->is64bit
= result
->is64bit
;
2309 ret
= parse_machfile(vp
, map
, thread
, header
, file_offset
,
2310 macho_size
, depth
, slide
, 0, myresult
, result
, imgp
);
2312 if (ret
== LOAD_SUCCESS
) {
2313 if (result
->threadstate
) {
2314 /* don't use the app's threadstate if we have a dyld */
2315 kfree(result
->threadstate
, result
->threadstate_sz
);
2317 result
->threadstate
= myresult
->threadstate
;
2318 result
->threadstate_sz
= myresult
->threadstate_sz
;
2320 result
->dynlinker
= TRUE
;
2321 result
->entry_point
= myresult
->entry_point
;
2322 result
->validentry
= myresult
->validentry
;
2323 result
->all_image_info_addr
= myresult
->all_image_info_addr
;
2324 result
->all_image_info_size
= myresult
->all_image_info_size
;
2325 if (myresult
->platform_binary
) {
2326 result
->csflags
|= CS_DYLD_PLATFORM
;
2330 struct vnode_attr va
;
2332 VATTR_WANTED(&va
, va_fsid64
);
2333 VATTR_WANTED(&va
, va_fsid
);
2334 VATTR_WANTED(&va
, va_fileid
);
2335 int error
= vnode_getattr(vp
, &va
, imgp
->ip_vfs_context
);
2337 imgp
->ip_dyld_fsid
= get_va_fsid(&va
);
2338 imgp
->ip_dyld_fsobjid
= va
.va_fileid
;
2343 FREE(dyld_data
, M_TEMP
);
2348 static load_return_t
2349 load_code_signature(
2350 struct linkedit_data_command
*lcp
,
2355 load_result_t
*result
,
2356 struct image_params
*imgp
)
2362 struct cs_blob
*blob
;
2364 vm_size_t blob_size
;
2369 if (lcp
->cmdsize
!= sizeof (struct linkedit_data_command
) ||
2370 lcp
->dataoff
+ lcp
->datasize
> macho_size
) {
2371 ret
= LOAD_BADMACHO
;
2375 blob
= ubc_cs_blob_get(vp
, cputype
, macho_offset
);
2377 /* we already have a blob for this vnode and cputype */
2378 if (blob
->csb_cpu_type
== cputype
&&
2379 blob
->csb_base_offset
== macho_offset
) {
2380 /* it matches the blob we want here, lets verify the version */
2381 if(0 != ubc_cs_generation_check(vp
)) {
2382 if (0 != ubc_cs_blob_revalidate(vp
, blob
, imgp
, 0)) {
2383 ret
= LOAD_FAILURE
; /* set error same as from ubc_cs_blob_add */
2389 /* the blob has changed for this vnode: fail ! */
2390 ret
= LOAD_BADMACHO
;
2395 blob_size
= lcp
->datasize
;
2396 kr
= ubc_cs_blob_allocate(&addr
, &blob_size
);
2397 if (kr
!= KERN_SUCCESS
) {
2403 error
= vn_rdwr(UIO_READ
,
2407 macho_offset
+ lcp
->dataoff
,
2413 if (error
|| resid
!= 0) {
2418 if (ubc_cs_blob_add(vp
,
2427 ubc_cs_blob_deallocate(addr
, blob_size
);
2432 /* ubc_cs_blob_add() has consumed "addr" */
2436 #if CHECK_CS_VALIDATION_BITMAP
2437 ubc_cs_validation_bitmap_allocate( vp
);
2442 if (ret
== LOAD_SUCCESS
) {
2444 panic("success, but no blob!");
2446 result
->csflags
|= blob
->csb_flags
;
2447 result
->platform_binary
= blob
->csb_platform_binary
;
2448 result
->cs_end_offset
= blob
->csb_end_offset
;
2451 ubc_cs_blob_deallocate(addr
, blob_size
);
2459 #if CONFIG_CODE_DECRYPTION
2461 static load_return_t
2463 struct encryption_info_command
*eip
,
2470 cpu_subtype_t cpusubtype
)
2473 pager_crypt_info_t crypt_info
;
2474 const char * cryptname
= 0;
2478 struct segment_command_64
*seg64
;
2479 struct segment_command
*seg32
;
2480 vm_map_offset_t map_offset
, map_size
;
2481 vm_object_offset_t crypto_backing_offset
;
2484 if (eip
->cmdsize
< sizeof(*eip
)) return LOAD_BADMACHO
;
2486 switch(eip
->cryptid
) {
2488 /* not encrypted, just an empty load command */
2489 return LOAD_SUCCESS
;
2491 cryptname
="com.apple.unfree";
2494 /* some random cryptid that you could manually put into
2495 * your binary if you want NULL */
2496 cryptname
="com.apple.null";
2499 return LOAD_BADMACHO
;
2502 if (map
== VM_MAP_NULL
) return (LOAD_SUCCESS
);
2503 if (NULL
== text_crypter_create
) return LOAD_FAILURE
;
2505 MALLOC_ZONE(vpath
, char *, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
2506 if(vpath
== NULL
) return LOAD_FAILURE
;
2509 error
= vn_getpath(vp
, vpath
, &len
);
2511 FREE_ZONE(vpath
, MAXPATHLEN
, M_NAMEI
);
2512 return LOAD_FAILURE
;
2515 /* set up decrypter first */
2516 crypt_file_data_t crypt_data
= {
2519 .cpusubtype
= cpusubtype
};
2520 kr
=text_crypter_create(&crypt_info
, cryptname
, (void*)&crypt_data
);
2521 #if VM_MAP_DEBUG_APPLE_PROTECT
2522 if (vm_map_debug_apple_protect
) {
2525 printf("APPLE_PROTECT: %d[%s] map %p %s(%s) -> 0x%x\n",
2526 p
->p_pid
, p
->p_comm
, map
, __FUNCTION__
, vpath
, kr
);
2528 #endif /* VM_MAP_DEBUG_APPLE_PROTECT */
2529 FREE_ZONE(vpath
, MAXPATHLEN
, M_NAMEI
);
2532 printf("set_code_unprotect: unable to create decrypter %s, kr=%d\n",
2534 if (kr
== kIOReturnNotPrivileged
) {
2535 /* text encryption returned decryption failure */
2536 return(LOAD_DECRYPTFAIL
);
2538 return LOAD_RESOURCE
;
2541 /* this is terrible, but we have to rescan the load commands to find the
2542 * virtual address of this encrypted stuff. This code is gonna look like
2543 * the dyld source one day... */
2544 struct mach_header
*header
= (struct mach_header
*)addr
;
2545 size_t mach_header_sz
= sizeof(struct mach_header
);
2546 if (header
->magic
== MH_MAGIC_64
||
2547 header
->magic
== MH_CIGAM_64
) {
2548 mach_header_sz
= sizeof(struct mach_header_64
);
2550 offset
= mach_header_sz
;
2551 uint32_t ncmds
= header
->ncmds
;
2554 * Get a pointer to the command.
2556 struct load_command
*lcp
= (struct load_command
*)(addr
+ offset
);
2557 offset
+= lcp
->cmdsize
;
2561 seg64
= (struct segment_command_64
*)lcp
;
2562 if ((seg64
->fileoff
<= eip
->cryptoff
) &&
2563 (seg64
->fileoff
+seg64
->filesize
>=
2564 eip
->cryptoff
+eip
->cryptsize
)) {
2565 map_offset
= seg64
->vmaddr
+ eip
->cryptoff
- seg64
->fileoff
+ slide
;
2566 map_size
= eip
->cryptsize
;
2567 crypto_backing_offset
= macho_offset
+ eip
->cryptoff
;
2571 seg32
= (struct segment_command
*)lcp
;
2572 if ((seg32
->fileoff
<= eip
->cryptoff
) &&
2573 (seg32
->fileoff
+seg32
->filesize
>=
2574 eip
->cryptoff
+eip
->cryptsize
)) {
2575 map_offset
= seg32
->vmaddr
+ eip
->cryptoff
- seg32
->fileoff
+ slide
;
2576 map_size
= eip
->cryptsize
;
2577 crypto_backing_offset
= macho_offset
+ eip
->cryptoff
;
2583 /* if we get here, did not find anything */
2584 return LOAD_BADMACHO
;
2587 /* now remap using the decrypter */
2588 MACHO_PRINTF(("+++ set_code_unprotect: vm[0x%llx:0x%llx]\n",
2589 (uint64_t) map_offset
,
2590 (uint64_t) (map_offset
+map_size
)));
2591 kr
= vm_map_apple_protected(map
,
2593 map_offset
+map_size
,
2594 crypto_backing_offset
,
2597 printf("set_code_unprotect(): mapping failed with %x\n", kr
);
2598 return LOAD_PROTECT
;
2601 return LOAD_SUCCESS
;
2607 * This routine exists to support the load_dylinker().
2609 * This routine has its own, separate, understanding of the FAT file format,
2610 * which is terrifically unfortunate.
2617 struct mach_header
*mach_header
,
2620 struct macho_data
*data
,
2625 vfs_context_t ctx
= vfs_context_current();
2626 proc_t p
= vfs_context_proc(ctx
);
2627 kauth_cred_t kerncred
;
2628 struct nameidata
*ndp
= &data
->__nid
;
2630 struct fat_arch fat_arch
;
2633 union macho_vnode_header
*header
= &data
->__header
;
2634 off_t fsize
= (off_t
)0;
2637 * Capture the kernel credential for use in the actual read of the
2638 * file, since the user doing the execution may have execute rights
2639 * but not read rights, but to exec something, we have to either map
2640 * or read it into the new process address space, which requires
2641 * read rights. This is to deal with lack of common credential
2642 * serialization code which would treat NOCRED as "serialize 'root'".
2644 kerncred
= vfs_context_ucred(vfs_context_kernel());
2646 /* init the namei data to point the file user's program name */
2647 NDINIT(ndp
, LOOKUP
, OP_OPEN
, FOLLOW
| LOCKLEAF
, UIO_SYSSPACE
, CAST_USER_ADDR_T(path
), ctx
);
2649 if ((error
= namei(ndp
)) != 0) {
2650 if (error
== ENOENT
) {
2651 error
= LOAD_ENOENT
;
2653 error
= LOAD_FAILURE
;
2660 /* check for regular file */
2661 if (vp
->v_type
!= VREG
) {
2662 error
= LOAD_PROTECT
;
2667 if ((error
= vnode_size(vp
, &fsize
, ctx
)) != 0) {
2668 error
= LOAD_FAILURE
;
2672 /* Check mount point */
2673 if (vp
->v_mount
->mnt_flag
& MNT_NOEXEC
) {
2674 error
= LOAD_PROTECT
;
2679 if ((error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_EXECUTE
| KAUTH_VNODE_READ_DATA
, ctx
)) != 0) {
2680 error
= LOAD_PROTECT
;
2684 /* try to open it */
2685 if ((error
= VNOP_OPEN(vp
, FREAD
, ctx
)) != 0) {
2686 error
= LOAD_PROTECT
;
2690 if ((error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)header
, sizeof (*header
), 0,
2691 UIO_SYSSPACE
, IO_NODELOCKED
, kerncred
, &resid
, p
)) != 0) {
2692 error
= LOAD_IOERROR
;
2697 error
= LOAD_BADMACHO
;
2701 if (header
->mach_header
.magic
== MH_MAGIC
||
2702 header
->mach_header
.magic
== MH_MAGIC_64
) {
2704 } else if (OSSwapBigToHostInt32(header
->fat_header
.magic
) == FAT_MAGIC
) {
2707 error
= LOAD_BADMACHO
;
2713 error
= fatfile_validate_fatarches((vm_offset_t
)(&header
->fat_header
),
2715 if (error
!= LOAD_SUCCESS
) {
2719 /* Look up our architecture in the fat file. */
2720 error
= fatfile_getarch_with_bits(archbits
,
2721 (vm_offset_t
)(&header
->fat_header
), sizeof(*header
), &fat_arch
);
2722 if (error
!= LOAD_SUCCESS
)
2725 /* Read the Mach-O header out of it */
2726 error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)&header
->mach_header
,
2727 sizeof (header
->mach_header
), fat_arch
.offset
,
2728 UIO_SYSSPACE
, IO_NODELOCKED
, kerncred
, &resid
, p
);
2730 error
= LOAD_IOERROR
;
2735 error
= LOAD_BADMACHO
;
2739 /* Is this really a Mach-O? */
2740 if (header
->mach_header
.magic
!= MH_MAGIC
&&
2741 header
->mach_header
.magic
!= MH_MAGIC_64
) {
2742 error
= LOAD_BADMACHO
;
2746 *file_offset
= fat_arch
.offset
;
2747 *macho_size
= fat_arch
.size
;
2750 * Force get_macho_vnode() to fail if the architecture bits
2751 * do not match the expected architecture bits. This in
2752 * turn causes load_dylinker() to fail for the same reason,
2753 * so it ensures the dynamic linker and the binary are in
2754 * lock-step. This is potentially bad, if we ever add to
2755 * the CPU_ARCH_* bits any bits that are desirable but not
2756 * required, since the dynamic linker might work, but we will
2757 * refuse to load it because of this check.
2759 if ((cpu_type_t
)(header
->mach_header
.cputype
& CPU_ARCH_MASK
) != archbits
) {
2760 error
= LOAD_BADARCH
;
2765 *macho_size
= fsize
;
2768 *mach_header
= header
->mach_header
;
2771 ubc_setsize(vp
, fsize
);
2775 (void) VNOP_CLOSE(vp
, FREAD
, ctx
);