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>
54 #include <mach/mach_types.h>
55 #include <mach/vm_map.h> /* vm_allocate() */
56 #include <mach/mach_vm.h> /* mach_vm_allocate() */
57 #include <mach/vm_statistics.h>
58 #include <mach/task.h>
59 #include <mach/thread_act.h>
61 #include <machine/vmparam.h>
62 #include <machine/exec.h>
63 #include <machine/pal_routines.h>
65 #include <kern/kern_types.h>
66 #include <kern/cpu_number.h>
67 #include <kern/mach_loader.h>
68 #include <kern/mach_fat.h>
69 #include <kern/kalloc.h>
70 #include <kern/task.h>
71 #include <kern/thread.h>
72 #include <kern/page_decrypt.h>
74 #include <mach-o/fat.h>
75 #include <mach-o/loader.h>
78 #include <vm/vm_map.h>
79 #include <vm/vm_kern.h>
80 #include <vm/vm_pager.h>
81 #include <vm/vnode_pager.h>
82 #include <vm/vm_protos.h>
83 #include <IOKit/IOReturn.h> /* for kIOReturnNotPrivileged */
86 * XXX vm/pmap.h should not treat these prototypes as MACH_KERNEL_PRIVATE
87 * when KERNEL is defined.
89 extern pmap_t
pmap_create(ledger_t ledger
, vm_map_size_t size
,
92 /* XXX should have prototypes in a shared header file */
93 extern int get_map_nentries(vm_map_t
);
95 extern kern_return_t
memory_object_signed(memory_object_control_t control
,
98 /* An empty load_result_t */
99 static load_result_t load_result_null
= {
100 .mach_header
= MACH_VM_MIN_ADDRESS
,
101 .entry_point
= MACH_VM_MIN_ADDRESS
,
102 .user_stack
= MACH_VM_MIN_ADDRESS
,
103 .user_stack_size
= 0,
104 .all_image_info_addr
= MACH_VM_MIN_ADDRESS
,
105 .all_image_info_size
= 0,
109 .needs_dynlinker
= 0,
110 .prog_allocated_stack
= 0,
111 .prog_stack_size
= 0,
115 .min_vm_addr
= MACH_VM_MAX_ADDRESS
,
116 .max_vm_addr
= MACH_VM_MIN_ADDRESS
120 * Prototypes of static functions.
127 struct mach_header
*header
,
133 load_result_t
*result
138 struct load_command
*lcp
,
146 load_result_t
*result
151 struct uuid_command
*uulp
,
153 load_result_t
*result
158 struct linkedit_data_command
*lcp
,
163 load_result_t
*result
);
165 #if CONFIG_CODE_DECRYPTION
168 struct encryption_info_command
*lcp
,
174 cpu_subtype_t cpusubtype
);
180 struct entry_point_command
*epc
,
183 load_result_t
*result
188 struct thread_command
*tcp
,
191 load_result_t
*result
206 mach_vm_offset_t
*user_stack
,
215 mach_vm_offset_t
*entry_point
220 struct dylinker_command
*lcp
,
226 load_result_t
*result
235 struct mach_header
*mach_header
,
238 struct macho_data
*macho_data
,
243 widen_segment_command(const struct segment_command
*scp32
,
244 struct segment_command_64
*scp
)
246 scp
->cmd
= scp32
->cmd
;
247 scp
->cmdsize
= scp32
->cmdsize
;
248 bcopy(scp32
->segname
, scp
->segname
, sizeof(scp
->segname
));
249 scp
->vmaddr
= scp32
->vmaddr
;
250 scp
->vmsize
= scp32
->vmsize
;
251 scp
->fileoff
= scp32
->fileoff
;
252 scp
->filesize
= scp32
->filesize
;
253 scp
->maxprot
= scp32
->maxprot
;
254 scp
->initprot
= scp32
->initprot
;
255 scp
->nsects
= scp32
->nsects
;
256 scp
->flags
= scp32
->flags
;
260 note_all_image_info_section(const struct segment_command_64
*scp
,
261 boolean_t is64
, size_t section_size
, const void *sections
,
262 int64_t slide
, load_result_t
*result
)
266 struct section_64 s64
;
270 if (strncmp(scp
->segname
, "__DATA", sizeof(scp
->segname
)) != 0)
272 for (i
= 0; i
< scp
->nsects
; ++i
) {
273 sectionp
= (const void *)
274 ((const char *)sections
+ section_size
* i
);
275 if (0 == strncmp(sectionp
->s64
.sectname
, "__all_image_info",
276 sizeof(sectionp
->s64
.sectname
))) {
277 result
->all_image_info_addr
=
278 is64
? sectionp
->s64
.addr
: sectionp
->s32
.addr
;
279 result
->all_image_info_addr
+= slide
;
280 result
->all_image_info_size
=
281 is64
? sectionp
->s64
.size
: sectionp
->s32
.size
;
289 struct image_params
*imgp
,
290 struct mach_header
*header
,
293 load_result_t
*result
296 struct vnode
*vp
= imgp
->ip_vp
;
297 off_t file_offset
= imgp
->ip_arch_offset
;
298 off_t macho_size
= imgp
->ip_arch_size
;
299 off_t file_size
= imgp
->ip_vattr
->va_data_size
;
301 pmap_t pmap
= 0; /* protected by create_map */
304 task_t old_task
= TASK_NULL
; /* protected by create_map */
305 load_result_t myresult
;
307 boolean_t create_map
= FALSE
;
308 boolean_t enforce_hard_pagezero
= TRUE
;
309 int spawn
= (imgp
->ip_flags
& IMGPF_SPAWN
);
310 task_t task
= current_task();
311 proc_t p
= current_proc();
312 mach_vm_offset_t aslr_offset
= 0;
313 mach_vm_offset_t dyld_aslr_offset
= 0;
316 if (macho_size
> file_size
) {
317 return(LOAD_BADMACHO
);
320 if (new_map
== VM_MAP_NULL
) {
322 old_task
= current_task();
326 * If we are spawning, we have created backing objects for the process
327 * already, which include non-lazily creating the task map. So we
328 * are going to switch out the task map with one appropriate for the
329 * bitness of the image being loaded.
333 old_task
= get_threadtask(thread
);
338 if (imgp
->ip_new_thread
) {
339 ledger_task
= get_threadtask(imgp
->ip_new_thread
);
343 pmap
= pmap_create(get_task_ledger(ledger_task
),
345 (imgp
->ip_flags
& IMGPF_IS_64BIT
));
346 pal_switch_pmap(thread
, pmap
, imgp
->ip_flags
& IMGPF_IS_64BIT
);
347 map
= vm_map_create(pmap
,
349 vm_compute_max_offset((imgp
->ip_flags
& IMGPF_IS_64BIT
)),
355 #ifndef CONFIG_ENFORCE_SIGNED_CODE
356 /* This turns off faulting for executable pages, which allows
357 * to circumvent Code Signing Enforcement. The per process
358 * flag (CS_ENFORCEMENT) is not set yet, but we can use the
361 if ( !cs_enforcement(NULL
) && (header
->flags
& MH_ALLOW_STACK_EXECUTION
) )
362 vm_map_disable_NX(map
);
365 /* Forcibly disallow execution from data pages on even if the arch
366 * normally permits it. */
367 if ((header
->flags
& MH_NO_HEAP_EXECUTION
) && !(imgp
->ip_flags
& IMGPF_ALLOW_DATA_EXEC
))
368 vm_map_disallow_data_exec(map
);
371 * Compute a random offset for ASLR, and an independent random offset for dyld.
373 if (!(imgp
->ip_flags
& IMGPF_DISABLE_ASLR
)) {
374 uint64_t max_slide_pages
;
376 max_slide_pages
= vm_map_get_max_aslr_slide_pages(map
);
378 aslr_offset
= random();
379 aslr_offset
%= max_slide_pages
;
380 aslr_offset
<<= vm_map_page_shift(map
);
382 dyld_aslr_offset
= random();
383 dyld_aslr_offset
%= max_slide_pages
;
384 dyld_aslr_offset
<<= vm_map_page_shift(map
);
390 *result
= load_result_null
;
392 lret
= parse_machfile(vp
, map
, thread
, header
, file_offset
, macho_size
,
393 0, (int64_t)aslr_offset
, (int64_t)dyld_aslr_offset
, result
);
395 if (lret
!= LOAD_SUCCESS
) {
397 vm_map_deallocate(map
); /* will lose pmap reference too */
404 * On x86, for compatibility, don't enforce the hard page-zero restriction for 32-bit binaries.
406 if ((imgp
->ip_flags
& IMGPF_IS_64BIT
) == 0) {
407 enforce_hard_pagezero
= FALSE
;
411 * Check to see if the page zero is enforced by the map->min_offset.
413 if (enforce_hard_pagezero
&& (vm_map_has_hard_pagezero(map
, 0x1000) == FALSE
)) {
415 vm_map_deallocate(map
); /* will lose pmap reference too */
417 printf("Cannot enforce a hard page-zero for %s\n", imgp
->ip_strings
);
418 return (LOAD_BADMACHO
);
424 * Swap the new map for the old, which consumes our new map
425 * reference but each leaves us responsible for the old_map reference.
426 * That lets us get off the pmap associated with it, and
427 * then we can release it.
432 * If this is an exec, then we are going to destroy the old
433 * task, and it's correct to halt it; if it's spawn, the
434 * task is not yet running, and it makes no sense.
438 * Mark the task as halting and start the other
439 * threads towards terminating themselves. Then
440 * make sure any threads waiting for a process
441 * transition get informed that we are committed to
442 * this transition, and then finally complete the
443 * task halting (wait for threads and then cleanup
446 * NOTE: task_start_halt() makes sure that no new
447 * threads are created in the task during the transition.
448 * We need to mark the workqueue as exiting before we
449 * wait for threads to terminate (at the end of which
450 * we no longer have a prohibition on thread creation).
452 * Finally, clean up any lingering workqueue data structures
453 * that may have been left behind by the workqueue threads
454 * as they exited (and then clean up the work queue itself).
456 kret
= task_start_halt(task
);
457 if (kret
!= KERN_SUCCESS
) {
458 vm_map_deallocate(map
); /* will lose pmap reference too */
459 return (LOAD_FAILURE
);
461 proc_transcommit(p
, 0);
462 workqueue_mark_exiting(p
);
463 task_complete_halt(task
);
466 old_map
= swap_task_map(old_task
, thread
, map
, !spawn
);
467 vm_map_deallocate(old_map
);
469 return(LOAD_SUCCESS
);
473 * The file size of a mach-o file is limited to 32 bits; this is because
474 * this is the limit on the kalloc() of enough bytes for a mach_header and
475 * the contents of its sizeofcmds, which is currently constrained to 32
476 * bits in the file format itself. We read into the kernel buffer the
477 * commands section, and then parse it in order to parse the mach-o file
478 * format load_command segment(s). We are only interested in a subset of
479 * the total set of possible commands. If "map"==VM_MAP_NULL or
480 * "thread"==THREAD_NULL, do not make permament VM modifications,
481 * just preflight the parse.
489 struct mach_header
*header
,
494 int64_t dyld_aslr_offset
,
495 load_result_t
*result
499 struct load_command
*lcp
;
500 struct dylinker_command
*dlp
= 0;
501 integer_t dlarchbits
= 0;
503 load_return_t ret
= LOAD_SUCCESS
;
506 vm_size_t size
,kl_size
;
508 size_t oldoffset
; /* for overflow check */
510 proc_t p
= current_proc(); /* XXXX */
513 size_t mach_header_sz
= sizeof(struct mach_header
);
515 boolean_t got_code_signatures
= FALSE
;
518 if (header
->magic
== MH_MAGIC_64
||
519 header
->magic
== MH_CIGAM_64
) {
520 mach_header_sz
= sizeof(struct mach_header_64
);
524 * Break infinite recursion
527 return(LOAD_FAILURE
);
533 * Check to see if right machine type.
535 if (((cpu_type_t
)(header
->cputype
& ~CPU_ARCH_MASK
) != (cpu_type() & ~CPU_ARCH_MASK
)) ||
536 !grade_binary(header
->cputype
,
537 header
->cpusubtype
& ~CPU_SUBTYPE_MASK
))
538 return(LOAD_BADARCH
);
540 abi64
= ((header
->cputype
& CPU_ARCH_ABI64
) == CPU_ARCH_ABI64
);
542 switch (header
->filetype
) {
548 return (LOAD_FAILURE
);
555 return (LOAD_FAILURE
);
561 return (LOAD_FAILURE
);
566 return (LOAD_FAILURE
);
570 * Get the pager for the file.
572 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
575 * Map portion that must be accessible directly into
578 if ((off_t
)(mach_header_sz
+ header
->sizeofcmds
) > macho_size
)
579 return(LOAD_BADMACHO
);
582 * Round size of Mach-O commands up to page boundry.
584 size
= round_page(mach_header_sz
+ header
->sizeofcmds
);
586 return(LOAD_BADMACHO
);
589 * Map the load commands into kernel memory.
593 kl_addr
= kalloc(size
);
594 addr
= (caddr_t
)kl_addr
;
596 return(LOAD_NOSPACE
);
598 error
= vn_rdwr(UIO_READ
, vp
, addr
, size
, file_offset
,
599 UIO_SYSSPACE
, 0, kauth_cred_get(), &resid
, p
);
602 kfree(kl_addr
, kl_size
);
603 return(LOAD_IOERROR
);
607 * For PIE and dyld, slide everything by the ASLR offset.
609 if ((header
->flags
& MH_PIE
) || (header
->filetype
== MH_DYLINKER
)) {
614 * Scan through the commands, processing each one as necessary.
615 * We parse in three passes through the headers:
616 * 1: thread state, uuid, code signature
618 * 3: dyld, encryption, check entry point
621 for (pass
= 1; pass
<= 3; pass
++) {
624 * Check that the entry point is contained in an executable segments
626 if ((pass
== 3) && (result
->validentry
== 0)) {
627 thread_state_initialize(thread
);
633 * Loop through each of the load_commands indicated by the
634 * Mach-O header; if an absurd value is provided, we just
635 * run off the end of the reserved section by incrementing
636 * the offset too far, so we are implicitly fail-safe.
638 offset
= mach_header_sz
;
639 ncmds
= header
->ncmds
;
643 * Get a pointer to the command.
645 lcp
= (struct load_command
*)(addr
+ offset
);
647 offset
+= lcp
->cmdsize
;
650 * Perform prevalidation of the struct load_command
651 * before we attempt to use its contents. Invalid
652 * values are ones which result in an overflow, or
653 * which can not possibly be valid commands, or which
654 * straddle or exist past the reserved section at the
655 * start of the image.
657 if (oldoffset
> offset
||
658 lcp
->cmdsize
< sizeof(struct load_command
) ||
659 offset
> header
->sizeofcmds
+ mach_header_sz
) {
665 * Act on struct load_command's for which kernel
666 * intervention is required.
675 * Having an LC_SEGMENT command for the
676 * wrong ABI is invalid <rdar://problem/11021230>
682 ret
= load_segment(lcp
,
698 * Having an LC_SEGMENT_64 command for the
699 * wrong ABI is invalid <rdar://problem/11021230>
705 ret
= load_segment(lcp
,
718 ret
= load_unixthread(
719 (struct thread_command
*) lcp
,
730 (struct entry_point_command
*) lcp
,
735 case LC_LOAD_DYLINKER
:
738 if ((depth
== 1) && (dlp
== 0)) {
739 dlp
= (struct dylinker_command
*)lcp
;
740 dlarchbits
= (header
->cputype
& CPU_ARCH_MASK
);
746 if (pass
== 1 && depth
== 1) {
747 ret
= load_uuid((struct uuid_command
*) lcp
,
748 (char *)addr
+ mach_header_sz
+ header
->sizeofcmds
,
752 case LC_CODE_SIGNATURE
:
757 load signatures & store in uip
758 set VM object "signed_pages"
760 ret
= load_code_signature(
761 (struct linkedit_data_command
*) lcp
,
767 if (ret
!= LOAD_SUCCESS
) {
768 printf("proc %d: load code signature error %d "
770 p
->p_pid
, ret
, vp
->v_name
);
771 ret
= LOAD_SUCCESS
; /* ignore error */
773 got_code_signatures
= TRUE
;
776 #if CONFIG_CODE_DECRYPTION
777 case LC_ENCRYPTION_INFO
:
778 case LC_ENCRYPTION_INFO_64
:
781 ret
= set_code_unprotect(
782 (struct encryption_info_command
*) lcp
,
783 addr
, map
, slide
, vp
,
784 header
->cputype
, header
->cpusubtype
);
785 if (ret
!= LOAD_SUCCESS
) {
786 printf("proc %d: set_code_unprotect() error %d "
788 p
->p_pid
, ret
, vp
->v_name
);
790 * Don't let the app run if it's
791 * encrypted but we failed to set up the
792 * decrypter. If the keys are missing it will
793 * return LOAD_DECRYPTFAIL.
795 if (ret
== LOAD_DECRYPTFAIL
) {
796 /* failed to load due to missing FP keys */
798 p
->p_lflag
|= P_LTERM_DECRYPTFAIL
;
806 /* Other commands are ignored by the kernel */
810 if (ret
!= LOAD_SUCCESS
)
813 if (ret
!= LOAD_SUCCESS
)
817 if (ret
== LOAD_SUCCESS
) {
818 if (! got_code_signatures
) {
819 struct cs_blob
*blob
;
820 /* no embedded signatures: look for detached ones */
821 blob
= ubc_cs_blob_get(vp
, -1, file_offset
);
823 unsigned int cs_flag_data
= blob
->csb_flags
;
824 if(0 != ubc_cs_generation_check(vp
)) {
825 if (0 != ubc_cs_blob_revalidate(vp
, blob
)) {
826 /* clear out the flag data if revalidation fails */
828 result
->csflags
&= ~CS_VALID
;
831 /* get flags to be applied to the process */
832 result
->csflags
|= cs_flag_data
;
836 /* Make sure if we need dyld, we got it */
837 if (result
->needs_dynlinker
&& !dlp
) {
841 if ((ret
== LOAD_SUCCESS
) && (dlp
!= 0)) {
843 * load the dylinker, and slide it by the independent DYLD ASLR
844 * offset regardless of the PIE-ness of the main binary.
846 ret
= load_dylinker(dlp
, dlarchbits
, map
, thread
, depth
,
847 dyld_aslr_offset
, result
);
850 if((ret
== LOAD_SUCCESS
) && (depth
== 1)) {
851 if (result
->thread_count
== 0) {
858 kfree(kl_addr
, kl_size
);
863 #if CONFIG_CODE_DECRYPTION
865 #define APPLE_UNPROTECTED_HEADER_SIZE (3 * PAGE_SIZE_64)
868 unprotect_dsmos_segment(
874 vm_map_offset_t map_addr
,
875 vm_map_size_t map_size
)
880 * The first APPLE_UNPROTECTED_HEADER_SIZE bytes (from offset 0 of
881 * this part of a Universal binary) are not protected...
882 * The rest needs to be "transformed".
884 if (file_off
<= APPLE_UNPROTECTED_HEADER_SIZE
&&
885 file_off
+ file_size
<= APPLE_UNPROTECTED_HEADER_SIZE
) {
886 /* it's all unprotected, nothing to do... */
889 if (file_off
<= APPLE_UNPROTECTED_HEADER_SIZE
) {
891 * We start mapping in the unprotected area.
892 * Skip the unprotected part...
894 vm_map_offset_t delta
;
896 delta
= APPLE_UNPROTECTED_HEADER_SIZE
;
901 /* ... transform the rest of the mapping. */
902 struct pager_crypt_info crypt_info
;
903 crypt_info
.page_decrypt
= dsmos_page_transform
;
904 crypt_info
.crypt_ops
= NULL
;
905 crypt_info
.crypt_end
= NULL
;
906 #pragma unused(vp, macho_offset)
907 crypt_info
.crypt_ops
= (void *)0x2e69cf40;
908 kr
= vm_map_apple_protected(map
,
914 if (kr
!= KERN_SUCCESS
) {
919 #else /* CONFIG_CODE_DECRYPTION */
921 unprotect_dsmos_segment(
922 __unused
uint64_t file_off
,
923 __unused
uint64_t file_size
,
924 __unused
struct vnode
*vp
,
925 __unused off_t macho_offset
,
926 __unused vm_map_t map
,
927 __unused vm_map_offset_t map_addr
,
928 __unused vm_map_size_t map_size
)
932 #endif /* CONFIG_CODE_DECRYPTION */
937 struct load_command
*lcp
,
945 load_result_t
*result
948 struct segment_command_64 segment_command
, *scp
;
950 vm_map_offset_t map_addr
, map_offset
;
951 vm_map_size_t map_size
, seg_size
, delta_size
;
954 size_t segment_command_size
, total_section_size
,
957 if (LC_SEGMENT_64
== lcp
->cmd
) {
958 segment_command_size
= sizeof(struct segment_command_64
);
959 single_section_size
= sizeof(struct section_64
);
961 segment_command_size
= sizeof(struct segment_command
);
962 single_section_size
= sizeof(struct section
);
964 if (lcp
->cmdsize
< segment_command_size
)
965 return (LOAD_BADMACHO
);
966 total_section_size
= lcp
->cmdsize
- segment_command_size
;
968 if (LC_SEGMENT_64
== lcp
->cmd
)
969 scp
= (struct segment_command_64
*)lcp
;
971 scp
= &segment_command
;
972 widen_segment_command((struct segment_command
*)lcp
, scp
);
976 * Make sure what we get from the file is really ours (as specified
979 if (scp
->fileoff
+ scp
->filesize
< scp
->fileoff
||
980 scp
->fileoff
+ scp
->filesize
> (uint64_t)macho_size
)
981 return (LOAD_BADMACHO
);
983 * Ensure that the number of sections specified would fit
984 * within the load command size.
986 if (total_section_size
/ single_section_size
< scp
->nsects
)
987 return (LOAD_BADMACHO
);
989 * Make sure the segment is page-aligned in the file.
991 if ((scp
->fileoff
& PAGE_MASK_64
) != 0)
992 return (LOAD_BADMACHO
);
995 * Round sizes to page size.
997 seg_size
= round_page_64(scp
->vmsize
);
998 map_size
= round_page_64(scp
->filesize
);
999 map_addr
= trunc_page_64(scp
->vmaddr
); /* JVXXX note that in XNU TOT this is round instead of trunc for 64 bits */
1001 seg_size
= vm_map_round_page(seg_size
, vm_map_page_mask(map
));
1002 map_size
= vm_map_round_page(map_size
, vm_map_page_mask(map
));
1005 return (KERN_SUCCESS
);
1006 if (map_addr
== 0 &&
1009 (scp
->initprot
& VM_PROT_ALL
) == VM_PROT_NONE
&&
1010 (scp
->maxprot
& VM_PROT_ALL
) == VM_PROT_NONE
) {
1012 * For PIE, extend page zero rather than moving it. Extending
1013 * page zero keeps early allocations from falling predictably
1014 * between the end of page zero and the beginning of the first
1021 * This is a "page zero" segment: it starts at address 0,
1022 * is not mapped from the binary file and is not accessible.
1023 * User-space should never be able to access that memory, so
1024 * make it completely off limits by raising the VM map's
1027 ret
= vm_map_raise_min_offset(map
, seg_size
);
1028 if (ret
!= KERN_SUCCESS
) {
1029 return (LOAD_FAILURE
);
1031 return (LOAD_SUCCESS
);
1034 /* If a non-zero slide was specified by the caller, apply now */
1037 if (map_addr
< result
->min_vm_addr
)
1038 result
->min_vm_addr
= map_addr
;
1039 if (map_addr
+seg_size
> result
->max_vm_addr
)
1040 result
->max_vm_addr
= map_addr
+seg_size
;
1042 if (map
== VM_MAP_NULL
)
1043 return (LOAD_SUCCESS
);
1045 map_offset
= pager_offset
+ scp
->fileoff
; /* limited to 32 bits */
1048 initprot
= (scp
->initprot
) & VM_PROT_ALL
;
1049 maxprot
= (scp
->maxprot
) & VM_PROT_ALL
;
1051 * Map a copy of the file into the address space.
1053 ret
= vm_map_enter_mem_object_control(map
,
1054 &map_addr
, map_size
, (mach_vm_offset_t
)0,
1055 VM_FLAGS_FIXED
, control
, map_offset
, TRUE
,
1057 VM_INHERIT_DEFAULT
);
1058 if (ret
!= KERN_SUCCESS
) {
1059 return (LOAD_NOSPACE
);
1063 * If the file didn't end on a page boundary,
1064 * we need to zero the leftover.
1066 delta_size
= map_size
- scp
->filesize
;
1068 if (delta_size
> 0) {
1069 mach_vm_offset_t tmp
;
1071 ret
= mach_vm_allocate(kernel_map
, &tmp
, delta_size
, VM_FLAGS_ANYWHERE
);
1072 if (ret
!= KERN_SUCCESS
)
1073 return(LOAD_RESOURCE
);
1075 if (copyout(tmp
, map_addr
+ scp
->filesize
,
1077 (void) mach_vm_deallocate(
1078 kernel_map
, tmp
, delta_size
);
1079 return (LOAD_FAILURE
);
1082 (void) mach_vm_deallocate(kernel_map
, tmp
, delta_size
);
1088 * If the virtual size of the segment is greater
1089 * than the size from the file, we need to allocate
1090 * zero fill memory for the rest.
1092 delta_size
= seg_size
- map_size
;
1093 if (delta_size
> 0) {
1094 mach_vm_offset_t tmp
= map_addr
+ map_size
;
1096 ret
= mach_vm_map(map
, &tmp
, delta_size
, 0, VM_FLAGS_FIXED
,
1098 scp
->initprot
, scp
->maxprot
,
1099 VM_INHERIT_DEFAULT
);
1100 if (ret
!= KERN_SUCCESS
)
1101 return(LOAD_NOSPACE
);
1104 if ( (scp
->fileoff
== 0) && (scp
->filesize
!= 0) )
1105 result
->mach_header
= map_addr
;
1107 if (scp
->flags
& SG_PROTECTED_VERSION_1
) {
1108 ret
= unprotect_dsmos_segment(scp
->fileoff
,
1118 if (LOAD_SUCCESS
== ret
&& filetype
== MH_DYLINKER
&&
1119 result
->all_image_info_addr
== MACH_VM_MIN_ADDRESS
)
1120 note_all_image_info_section(scp
,
1121 LC_SEGMENT_64
== lcp
->cmd
, single_section_size
,
1122 (const char *)lcp
+ segment_command_size
, slide
, result
);
1124 if ((result
->entry_point
>= map_addr
) && (result
->entry_point
< (map_addr
+ map_size
)))
1125 result
->validentry
= 1;
1133 struct uuid_command
*uulp
,
1135 load_result_t
*result
1139 * We need to check the following for this command:
1140 * - The command size should be atleast the size of struct uuid_command
1141 * - The UUID part of the command should be completely within the mach-o header
1144 if ((uulp
->cmdsize
< sizeof(struct uuid_command
)) ||
1145 (((char *)uulp
+ sizeof(struct uuid_command
)) > command_end
)) {
1146 return (LOAD_BADMACHO
);
1149 memcpy(&result
->uuid
[0], &uulp
->uuid
[0], sizeof(result
->uuid
));
1150 return (LOAD_SUCCESS
);
1156 struct entry_point_command
*epc
,
1159 load_result_t
*result
1162 mach_vm_offset_t addr
;
1165 if (epc
->cmdsize
< sizeof(*epc
))
1166 return (LOAD_BADMACHO
);
1167 if (result
->thread_count
!= 0) {
1168 printf("load_main: already have a thread!");
1169 return (LOAD_FAILURE
);
1172 if (thread
== THREAD_NULL
)
1173 return (LOAD_SUCCESS
);
1175 /* LC_MAIN specifies stack size but not location */
1176 if (epc
->stacksize
) {
1177 result
->prog_stack_size
= 1;
1178 result
->user_stack_size
= epc
->stacksize
;
1180 result
->prog_stack_size
= 0;
1181 result
->user_stack_size
= MAXSSIZ
;
1183 result
->prog_allocated_stack
= 0;
1185 /* use default location for stack */
1186 ret
= thread_userstackdefault(thread
, &addr
);
1187 if (ret
!= KERN_SUCCESS
)
1188 return(LOAD_FAILURE
);
1190 /* The stack slides down from the default location */
1191 result
->user_stack
= addr
;
1192 result
->user_stack
-= slide
;
1194 /* kernel does *not* use entryoff from LC_MAIN. Dyld uses it. */
1195 result
->needs_dynlinker
= TRUE
;
1196 result
->validentry
= TRUE
;
1198 ret
= thread_state_initialize( thread
);
1199 if (ret
!= KERN_SUCCESS
) {
1200 return(LOAD_FAILURE
);
1203 result
->unixproc
= TRUE
;
1204 result
->thread_count
++;
1206 return(LOAD_SUCCESS
);
1213 struct thread_command
*tcp
,
1216 load_result_t
*result
1221 mach_vm_offset_t addr
;
1223 if (tcp
->cmdsize
< sizeof(*tcp
))
1224 return (LOAD_BADMACHO
);
1225 if (result
->thread_count
!= 0) {
1226 printf("load_unixthread: already have a thread!");
1227 return (LOAD_FAILURE
);
1230 if (thread
== THREAD_NULL
)
1231 return (LOAD_SUCCESS
);
1233 ret
= load_threadstack(thread
,
1234 (uint32_t *)(((vm_offset_t
)tcp
) +
1235 sizeof(struct thread_command
)),
1236 tcp
->cmdsize
- sizeof(struct thread_command
),
1239 if (ret
!= LOAD_SUCCESS
)
1242 /* LC_UNIXTHREAD optionally specifies stack size and location */
1245 result
->prog_stack_size
= 0; /* unknown */
1246 result
->prog_allocated_stack
= 1;
1248 result
->prog_allocated_stack
= 0;
1249 result
->prog_stack_size
= 0;
1250 result
->user_stack_size
= MAXSSIZ
;
1253 /* The stack slides down from the default location */
1254 result
->user_stack
= addr
;
1255 result
->user_stack
-= slide
;
1257 ret
= load_threadentry(thread
,
1258 (uint32_t *)(((vm_offset_t
)tcp
) +
1259 sizeof(struct thread_command
)),
1260 tcp
->cmdsize
- sizeof(struct thread_command
),
1262 if (ret
!= LOAD_SUCCESS
)
1265 result
->entry_point
= addr
;
1266 result
->entry_point
+= slide
;
1268 ret
= load_threadstate(thread
,
1269 (uint32_t *)(((vm_offset_t
)tcp
) +
1270 sizeof(struct thread_command
)),
1271 tcp
->cmdsize
- sizeof(struct thread_command
));
1272 if (ret
!= LOAD_SUCCESS
)
1275 result
->unixproc
= TRUE
;
1276 result
->thread_count
++;
1278 return(LOAD_SUCCESS
);
1292 uint32_t thread_size
;
1294 ret
= thread_state_initialize( thread
);
1295 if (ret
!= KERN_SUCCESS
) {
1296 return(LOAD_FAILURE
);
1300 * Set the new thread state; iterate through the state flavors in
1303 while (total_size
> 0) {
1306 if (UINT32_MAX
-2 < size
||
1307 UINT32_MAX
/sizeof(uint32_t) < size
+2)
1308 return (LOAD_BADMACHO
);
1309 thread_size
= (size
+2)*sizeof(uint32_t);
1310 if (thread_size
> total_size
)
1311 return(LOAD_BADMACHO
);
1312 total_size
-= thread_size
;
1314 * Third argument is a kernel space pointer; it gets cast
1315 * to the appropriate type in machine_thread_set_state()
1316 * based on the value of flavor.
1318 ret
= thread_setstatus(thread
, flavor
, (thread_state_t
)ts
, size
);
1319 if (ret
!= KERN_SUCCESS
) {
1320 return(LOAD_FAILURE
);
1322 ts
+= size
; /* ts is a (uint32_t *) */
1324 return(LOAD_SUCCESS
);
1332 uint32_t total_size
,
1333 mach_vm_offset_t
*user_stack
,
1340 uint32_t stack_size
;
1342 while (total_size
> 0) {
1345 if (UINT32_MAX
-2 < size
||
1346 UINT32_MAX
/sizeof(uint32_t) < size
+2)
1347 return (LOAD_BADMACHO
);
1348 stack_size
= (size
+2)*sizeof(uint32_t);
1349 if (stack_size
> total_size
)
1350 return(LOAD_BADMACHO
);
1351 total_size
-= stack_size
;
1354 * Third argument is a kernel space pointer; it gets cast
1355 * to the appropriate type in thread_userstack() based on
1356 * the value of flavor.
1358 ret
= thread_userstack(thread
, flavor
, (thread_state_t
)ts
, size
, user_stack
, customstack
);
1359 if (ret
!= KERN_SUCCESS
) {
1360 return(LOAD_FAILURE
);
1362 ts
+= size
; /* ts is a (uint32_t *) */
1364 return(LOAD_SUCCESS
);
1372 uint32_t total_size
,
1373 mach_vm_offset_t
*entry_point
1379 uint32_t entry_size
;
1382 * Set the thread state.
1384 *entry_point
= MACH_VM_MIN_ADDRESS
;
1385 while (total_size
> 0) {
1388 if (UINT32_MAX
-2 < size
||
1389 UINT32_MAX
/sizeof(uint32_t) < size
+2)
1390 return (LOAD_BADMACHO
);
1391 entry_size
= (size
+2)*sizeof(uint32_t);
1392 if (entry_size
> total_size
)
1393 return(LOAD_BADMACHO
);
1394 total_size
-= entry_size
;
1396 * Third argument is a kernel space pointer; it gets cast
1397 * to the appropriate type in thread_entrypoint() based on
1398 * the value of flavor.
1400 ret
= thread_entrypoint(thread
, flavor
, (thread_state_t
)ts
, size
, entry_point
);
1401 if (ret
!= KERN_SUCCESS
) {
1402 return(LOAD_FAILURE
);
1404 ts
+= size
; /* ts is a (uint32_t *) */
1406 return(LOAD_SUCCESS
);
1410 struct nameidata __nid
;
1411 union macho_vnode_header
{
1412 struct mach_header mach_header
;
1413 struct fat_header fat_header
;
1418 static load_return_t
1420 struct dylinker_command
*lcp
,
1426 load_result_t
*result
1431 struct vnode
*vp
= NULLVP
; /* set by get_macho_vnode() */
1432 struct mach_header
*header
;
1433 off_t file_offset
= 0; /* set by get_macho_vnode() */
1434 off_t macho_size
= 0; /* set by get_macho_vnode() */
1435 load_result_t
*myresult
;
1437 struct macho_data
*macho_data
;
1439 struct mach_header __header
;
1440 load_result_t __myresult
;
1441 struct macho_data __macho_data
;
1444 if (lcp
->cmdsize
< sizeof(*lcp
))
1445 return (LOAD_BADMACHO
);
1447 name
= (char *)lcp
+ lcp
->name
.offset
;
1449 * Check for a proper null terminated string.
1453 if (p
>= (char *)lcp
+ lcp
->cmdsize
)
1454 return(LOAD_BADMACHO
);
1457 /* Allocate wad-of-data from heap to reduce excessively deep stacks */
1459 MALLOC(dyld_data
, void *, sizeof (*dyld_data
), M_TEMP
, M_WAITOK
);
1460 header
= &dyld_data
->__header
;
1461 myresult
= &dyld_data
->__myresult
;
1462 macho_data
= &dyld_data
->__macho_data
;
1464 ret
= get_macho_vnode(name
, archbits
, header
,
1465 &file_offset
, &macho_size
, macho_data
, &vp
);
1469 *myresult
= load_result_null
;
1472 * First try to map dyld in directly. This should work most of
1473 * the time since there shouldn't normally be something already
1474 * mapped to its address.
1477 ret
= parse_machfile(vp
, map
, thread
, header
, file_offset
,
1478 macho_size
, depth
, slide
, 0, myresult
);
1481 * If it turned out something was in the way, then we'll take
1482 * take this longer path to preflight dyld's vm ranges, then
1483 * map it at a free location in the address space.
1486 if (ret
== LOAD_NOSPACE
) {
1487 mach_vm_offset_t dyl_start
, map_addr
;
1488 mach_vm_size_t dyl_length
;
1489 int64_t slide_amount
;
1491 *myresult
= load_result_null
;
1494 * Preflight parsing the Mach-O file with a NULL
1495 * map, which will return the ranges needed for a
1496 * subsequent map attempt (with a slide) in "myresult"
1498 ret
= parse_machfile(vp
, VM_MAP_NULL
, THREAD_NULL
, header
,
1499 file_offset
, macho_size
, depth
,
1500 0 /* slide */, 0, myresult
);
1502 if (ret
!= LOAD_SUCCESS
) {
1506 dyl_start
= myresult
->min_vm_addr
;
1507 dyl_length
= myresult
->max_vm_addr
- myresult
->min_vm_addr
;
1509 dyl_length
+= slide
;
1511 /* To find an appropriate load address, do a quick allocation */
1512 map_addr
= dyl_start
;
1513 ret
= mach_vm_allocate(map
, &map_addr
, dyl_length
, VM_FLAGS_ANYWHERE
);
1514 if (ret
!= KERN_SUCCESS
) {
1519 ret
= mach_vm_deallocate(map
, map_addr
, dyl_length
);
1520 if (ret
!= KERN_SUCCESS
) {
1525 if (map_addr
< dyl_start
)
1526 slide_amount
= -(int64_t)(dyl_start
- map_addr
);
1528 slide_amount
= (int64_t)(map_addr
- dyl_start
);
1530 slide_amount
+= slide
;
1532 *myresult
= load_result_null
;
1534 ret
= parse_machfile(vp
, map
, thread
, header
,
1535 file_offset
, macho_size
, depth
,
1536 slide_amount
, 0, myresult
);
1543 if (ret
== LOAD_SUCCESS
) {
1544 result
->dynlinker
= TRUE
;
1545 result
->entry_point
= myresult
->entry_point
;
1546 result
->validentry
= myresult
->validentry
;
1547 result
->all_image_info_addr
= myresult
->all_image_info_addr
;
1548 result
->all_image_info_size
= myresult
->all_image_info_size
;
1549 if (myresult
->platform_binary
) {
1550 result
->csflags
|= CS_DYLD_PLATFORM
;
1556 FREE(dyld_data
, M_TEMP
);
1561 static load_return_t
1562 load_code_signature(
1563 struct linkedit_data_command
*lcp
,
1568 load_result_t
*result
)
1574 struct cs_blob
*blob
;
1576 vm_size_t blob_size
;
1581 if (lcp
->cmdsize
!= sizeof (struct linkedit_data_command
) ||
1582 lcp
->dataoff
+ lcp
->datasize
> macho_size
) {
1583 ret
= LOAD_BADMACHO
;
1587 blob
= ubc_cs_blob_get(vp
, cputype
, -1);
1589 /* we already have a blob for this vnode and cputype */
1590 if (blob
->csb_cpu_type
== cputype
&&
1591 blob
->csb_base_offset
== macho_offset
&&
1592 blob
->csb_mem_size
== lcp
->datasize
) {
1593 /* it matches the blob we want here, lets verify the version */
1594 if(0 != ubc_cs_generation_check(vp
)) {
1595 if (0 != ubc_cs_blob_revalidate(vp
, blob
)) {
1596 ret
= LOAD_FAILURE
; /* set error same as from ubc_cs_blob_add */
1602 /* the blob has changed for this vnode: fail ! */
1603 ret
= LOAD_BADMACHO
;
1608 blob_size
= lcp
->datasize
;
1609 kr
= ubc_cs_blob_allocate(&addr
, &blob_size
);
1610 if (kr
!= KERN_SUCCESS
) {
1616 error
= vn_rdwr(UIO_READ
,
1620 macho_offset
+ lcp
->dataoff
,
1626 if (error
|| resid
!= 0) {
1631 if (ubc_cs_blob_add(vp
,
1639 /* ubc_cs_blob_add() has consumed "addr" */
1643 #if CHECK_CS_VALIDATION_BITMAP
1644 ubc_cs_validation_bitmap_allocate( vp
);
1647 blob
= ubc_cs_blob_get(vp
, cputype
, -1);
1651 if (ret
== LOAD_SUCCESS
) {
1652 result
->csflags
|= blob
->csb_flags
;
1653 result
->platform_binary
= blob
->csb_platform_binary
;
1656 ubc_cs_blob_deallocate(addr
, blob_size
);
1664 #if CONFIG_CODE_DECRYPTION
1666 static load_return_t
1668 struct encryption_info_command
*eip
,
1674 cpu_subtype_t cpusubtype
)
1677 pager_crypt_info_t crypt_info
;
1678 const char * cryptname
= 0;
1682 struct segment_command_64
*seg64
;
1683 struct segment_command
*seg32
;
1684 vm_map_offset_t map_offset
, map_size
;
1687 if (eip
->cmdsize
< sizeof(*eip
)) return LOAD_BADMACHO
;
1689 switch(eip
->cryptid
) {
1691 /* not encrypted, just an empty load command */
1692 return LOAD_SUCCESS
;
1694 cryptname
="com.apple.unfree";
1697 /* some random cryptid that you could manually put into
1698 * your binary if you want NULL */
1699 cryptname
="com.apple.null";
1702 return LOAD_BADMACHO
;
1705 if (map
== VM_MAP_NULL
) return (LOAD_SUCCESS
);
1706 if (NULL
== text_crypter_create
) return LOAD_FAILURE
;
1708 MALLOC_ZONE(vpath
, char *, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
1709 if(vpath
== NULL
) return LOAD_FAILURE
;
1712 result
= vn_getpath(vp
, vpath
, &len
);
1714 FREE_ZONE(vpath
, MAXPATHLEN
, M_NAMEI
);
1715 return LOAD_FAILURE
;
1718 /* set up decrypter first */
1719 crypt_file_data_t crypt_data
= {
1722 .cpusubtype
= cpusubtype
};
1723 kr
=text_crypter_create(&crypt_info
, cryptname
, (void*)&crypt_data
);
1724 FREE_ZONE(vpath
, MAXPATHLEN
, M_NAMEI
);
1727 printf("set_code_unprotect: unable to create decrypter %s, kr=%d\n",
1729 if (kr
== kIOReturnNotPrivileged
) {
1730 /* text encryption returned decryption failure */
1731 return(LOAD_DECRYPTFAIL
);
1733 return LOAD_RESOURCE
;
1736 /* this is terrible, but we have to rescan the load commands to find the
1737 * virtual address of this encrypted stuff. This code is gonna look like
1738 * the dyld source one day... */
1739 struct mach_header
*header
= (struct mach_header
*)addr
;
1740 size_t mach_header_sz
= sizeof(struct mach_header
);
1741 if (header
->magic
== MH_MAGIC_64
||
1742 header
->magic
== MH_CIGAM_64
) {
1743 mach_header_sz
= sizeof(struct mach_header_64
);
1745 offset
= mach_header_sz
;
1746 uint32_t ncmds
= header
->ncmds
;
1749 * Get a pointer to the command.
1751 struct load_command
*lcp
= (struct load_command
*)(addr
+ offset
);
1752 offset
+= lcp
->cmdsize
;
1756 seg64
= (struct segment_command_64
*)lcp
;
1757 if ((seg64
->fileoff
<= eip
->cryptoff
) &&
1758 (seg64
->fileoff
+seg64
->filesize
>=
1759 eip
->cryptoff
+eip
->cryptsize
)) {
1760 map_offset
= seg64
->vmaddr
+ eip
->cryptoff
- seg64
->fileoff
+ slide
;
1761 map_size
= eip
->cryptsize
;
1765 seg32
= (struct segment_command
*)lcp
;
1766 if ((seg32
->fileoff
<= eip
->cryptoff
) &&
1767 (seg32
->fileoff
+seg32
->filesize
>=
1768 eip
->cryptoff
+eip
->cryptsize
)) {
1769 map_offset
= seg32
->vmaddr
+ eip
->cryptoff
- seg32
->fileoff
+ slide
;
1770 map_size
= eip
->cryptsize
;
1776 /* if we get here, did not find anything */
1777 return LOAD_BADMACHO
;
1780 /* now remap using the decrypter */
1781 kr
= vm_map_apple_protected(map
, map_offset
, map_offset
+map_size
, &crypt_info
);
1783 printf("set_code_unprotect(): mapping failed with %x\n", kr
);
1784 crypt_info
.crypt_end(crypt_info
.crypt_ops
);
1785 return LOAD_PROTECT
;
1788 return LOAD_SUCCESS
;
1794 * This routine exists to support the load_dylinker().
1796 * This routine has its own, separate, understanding of the FAT file format,
1797 * which is terrifically unfortunate.
1804 struct mach_header
*mach_header
,
1807 struct macho_data
*data
,
1812 vfs_context_t ctx
= vfs_context_current();
1813 proc_t p
= vfs_context_proc(ctx
);
1814 kauth_cred_t kerncred
;
1815 struct nameidata
*ndp
= &data
->__nid
;
1817 struct fat_arch fat_arch
;
1820 union macho_vnode_header
*header
= &data
->__header
;
1821 off_t fsize
= (off_t
)0;
1824 * Capture the kernel credential for use in the actual read of the
1825 * file, since the user doing the execution may have execute rights
1826 * but not read rights, but to exec something, we have to either map
1827 * or read it into the new process address space, which requires
1828 * read rights. This is to deal with lack of common credential
1829 * serialization code which would treat NOCRED as "serialize 'root'".
1831 kerncred
= vfs_context_ucred(vfs_context_kernel());
1833 /* init the namei data to point the file user's program name */
1834 NDINIT(ndp
, LOOKUP
, OP_OPEN
, FOLLOW
| LOCKLEAF
, UIO_SYSSPACE
, CAST_USER_ADDR_T(path
), ctx
);
1836 if ((error
= namei(ndp
)) != 0) {
1837 if (error
== ENOENT
) {
1838 error
= LOAD_ENOENT
;
1840 error
= LOAD_FAILURE
;
1847 /* check for regular file */
1848 if (vp
->v_type
!= VREG
) {
1849 error
= LOAD_PROTECT
;
1854 if ((error
= vnode_size(vp
, &fsize
, ctx
)) != 0) {
1855 error
= LOAD_FAILURE
;
1859 /* Check mount point */
1860 if (vp
->v_mount
->mnt_flag
& MNT_NOEXEC
) {
1861 error
= LOAD_PROTECT
;
1866 if ((error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_EXECUTE
| KAUTH_VNODE_READ_DATA
, ctx
)) != 0) {
1867 error
= LOAD_PROTECT
;
1871 /* try to open it */
1872 if ((error
= VNOP_OPEN(vp
, FREAD
, ctx
)) != 0) {
1873 error
= LOAD_PROTECT
;
1877 if ((error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)header
, sizeof (*header
), 0,
1878 UIO_SYSSPACE
, IO_NODELOCKED
, kerncred
, &resid
, p
)) != 0) {
1879 error
= LOAD_IOERROR
;
1883 if (header
->mach_header
.magic
== MH_MAGIC
||
1884 header
->mach_header
.magic
== MH_MAGIC_64
) {
1886 } else if (header
->fat_header
.magic
== FAT_MAGIC
||
1887 header
->fat_header
.magic
== FAT_CIGAM
) {
1890 error
= LOAD_BADMACHO
;
1895 /* Look up our architecture in the fat file. */
1896 error
= fatfile_getarch_with_bits(vp
, archbits
,
1897 (vm_offset_t
)(&header
->fat_header
), &fat_arch
);
1898 if (error
!= LOAD_SUCCESS
)
1901 /* Read the Mach-O header out of it */
1902 error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)&header
->mach_header
,
1903 sizeof (header
->mach_header
), fat_arch
.offset
,
1904 UIO_SYSSPACE
, IO_NODELOCKED
, kerncred
, &resid
, p
);
1906 error
= LOAD_IOERROR
;
1910 /* Is this really a Mach-O? */
1911 if (header
->mach_header
.magic
!= MH_MAGIC
&&
1912 header
->mach_header
.magic
!= MH_MAGIC_64
) {
1913 error
= LOAD_BADMACHO
;
1917 *file_offset
= fat_arch
.offset
;
1918 *macho_size
= fat_arch
.size
;
1921 * Force get_macho_vnode() to fail if the architecture bits
1922 * do not match the expected architecture bits. This in
1923 * turn causes load_dylinker() to fail for the same reason,
1924 * so it ensures the dynamic linker and the binary are in
1925 * lock-step. This is potentially bad, if we ever add to
1926 * the CPU_ARCH_* bits any bits that are desirable but not
1927 * required, since the dynamic linker might work, but we will
1928 * refuse to load it because of this check.
1930 if ((cpu_type_t
)(header
->mach_header
.cputype
& CPU_ARCH_MASK
) != archbits
) {
1931 error
= LOAD_BADARCH
;
1936 *macho_size
= fsize
;
1939 *mach_header
= header
->mach_header
;
1942 ubc_setsize(vp
, fsize
);
1946 (void) VNOP_CLOSE(vp
, FREAD
, ctx
);