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 extern kern_return_t
machine_thread_neon_state_initialize(thread_t thread
);
94 /* XXX should have prototypes in a shared header file */
95 extern int get_map_nentries(vm_map_t
);
97 extern kern_return_t
memory_object_signed(memory_object_control_t control
,
100 /* An empty load_result_t */
101 static load_result_t load_result_null
= {
102 .mach_header
= MACH_VM_MIN_ADDRESS
,
103 .entry_point
= MACH_VM_MIN_ADDRESS
,
104 .user_stack
= MACH_VM_MIN_ADDRESS
,
105 .user_stack_size
= 0,
106 .all_image_info_addr
= MACH_VM_MIN_ADDRESS
,
107 .all_image_info_size
= 0,
111 .needs_dynlinker
= 0,
112 .prog_allocated_stack
= 0,
113 .prog_stack_size
= 0,
119 .min_vm_addr
= MACH_VM_MAX_ADDRESS
,
120 .max_vm_addr
= MACH_VM_MIN_ADDRESS
,
125 * Prototypes of static functions.
132 struct mach_header
*header
,
138 load_result_t
*result
143 struct load_command
*lcp
,
151 load_result_t
*result
156 struct uuid_command
*uulp
,
158 load_result_t
*result
163 struct linkedit_data_command
*lcp
,
168 load_result_t
*result
);
170 #if CONFIG_CODE_DECRYPTION
173 struct encryption_info_command
*lcp
,
180 cpu_subtype_t cpusubtype
);
186 struct entry_point_command
*epc
,
189 load_result_t
*result
194 struct thread_command
*tcp
,
197 load_result_t
*result
212 mach_vm_offset_t
*user_stack
,
221 mach_vm_offset_t
*entry_point
226 struct dylinker_command
*lcp
,
232 load_result_t
*result
241 struct mach_header
*mach_header
,
244 struct macho_data
*macho_data
,
249 widen_segment_command(const struct segment_command
*scp32
,
250 struct segment_command_64
*scp
)
252 scp
->cmd
= scp32
->cmd
;
253 scp
->cmdsize
= scp32
->cmdsize
;
254 bcopy(scp32
->segname
, scp
->segname
, sizeof(scp
->segname
));
255 scp
->vmaddr
= scp32
->vmaddr
;
256 scp
->vmsize
= scp32
->vmsize
;
257 scp
->fileoff
= scp32
->fileoff
;
258 scp
->filesize
= scp32
->filesize
;
259 scp
->maxprot
= scp32
->maxprot
;
260 scp
->initprot
= scp32
->initprot
;
261 scp
->nsects
= scp32
->nsects
;
262 scp
->flags
= scp32
->flags
;
266 note_all_image_info_section(const struct segment_command_64
*scp
,
267 boolean_t is64
, size_t section_size
, const void *sections
,
268 int64_t slide
, load_result_t
*result
)
272 struct section_64 s64
;
276 if (strncmp(scp
->segname
, "__DATA", sizeof(scp
->segname
)) != 0)
278 for (i
= 0; i
< scp
->nsects
; ++i
) {
279 sectionp
= (const void *)
280 ((const char *)sections
+ section_size
* i
);
281 if (0 == strncmp(sectionp
->s64
.sectname
, "__all_image_info",
282 sizeof(sectionp
->s64
.sectname
))) {
283 result
->all_image_info_addr
=
284 is64
? sectionp
->s64
.addr
: sectionp
->s32
.addr
;
285 result
->all_image_info_addr
+= slide
;
286 result
->all_image_info_size
=
287 is64
? sectionp
->s64
.size
: sectionp
->s32
.size
;
296 struct image_params
*imgp
,
297 struct mach_header
*header
,
300 load_result_t
*result
303 struct vnode
*vp
= imgp
->ip_vp
;
304 off_t file_offset
= imgp
->ip_arch_offset
;
305 off_t macho_size
= imgp
->ip_arch_size
;
306 off_t file_size
= imgp
->ip_vattr
->va_data_size
;
308 pmap_t pmap
= 0; /* protected by create_map */
311 task_t old_task
= TASK_NULL
; /* protected by create_map */
312 load_result_t myresult
;
314 boolean_t create_map
= FALSE
;
315 boolean_t enforce_hard_pagezero
= TRUE
;
316 int spawn
= (imgp
->ip_flags
& IMGPF_SPAWN
);
317 task_t task
= current_task();
318 proc_t p
= current_proc();
319 mach_vm_offset_t aslr_offset
= 0;
320 mach_vm_offset_t dyld_aslr_offset
= 0;
323 if (macho_size
> file_size
) {
324 return(LOAD_BADMACHO
);
327 if (new_map
== VM_MAP_NULL
) {
329 old_task
= current_task();
333 * If we are spawning, we have created backing objects for the process
334 * already, which include non-lazily creating the task map. So we
335 * are going to switch out the task map with one appropriate for the
336 * bitness of the image being loaded.
340 old_task
= get_threadtask(thread
);
345 if (imgp
->ip_new_thread
) {
346 ledger_task
= get_threadtask(imgp
->ip_new_thread
);
350 pmap
= pmap_create(get_task_ledger(ledger_task
),
352 ((imgp
->ip_flags
& IMGPF_IS_64BIT
) != 0));
353 pal_switch_pmap(thread
, pmap
, imgp
->ip_flags
& IMGPF_IS_64BIT
);
354 map
= vm_map_create(pmap
,
356 vm_compute_max_offset(((imgp
->ip_flags
& IMGPF_IS_64BIT
) == IMGPF_IS_64BIT
)),
361 #if (__ARM_ARCH_7K__ >= 2) && defined(PLATFORM_WatchOS)
362 /* enforce 16KB alignment for watch targets with new ABI */
363 vm_map_set_page_shift(map
, SIXTEENK_PAGE_SHIFT
);
364 #endif /* __arm64__ */
366 #ifndef CONFIG_ENFORCE_SIGNED_CODE
367 /* This turns off faulting for executable pages, which allows
368 * to circumvent Code Signing Enforcement. The per process
369 * flag (CS_ENFORCEMENT) is not set yet, but we can use the
372 if ( !cs_enforcement(NULL
) && (header
->flags
& MH_ALLOW_STACK_EXECUTION
) )
373 vm_map_disable_NX(map
);
376 /* Forcibly disallow execution from data pages on even if the arch
377 * normally permits it. */
378 if ((header
->flags
& MH_NO_HEAP_EXECUTION
) && !(imgp
->ip_flags
& IMGPF_ALLOW_DATA_EXEC
))
379 vm_map_disallow_data_exec(map
);
382 * Compute a random offset for ASLR, and an independent random offset for dyld.
384 if (!(imgp
->ip_flags
& IMGPF_DISABLE_ASLR
)) {
385 uint64_t max_slide_pages
;
387 max_slide_pages
= vm_map_get_max_aslr_slide_pages(map
);
389 aslr_offset
= random();
390 aslr_offset
%= max_slide_pages
;
391 aslr_offset
<<= vm_map_page_shift(map
);
393 dyld_aslr_offset
= random();
394 dyld_aslr_offset
%= max_slide_pages
;
395 dyld_aslr_offset
<<= vm_map_page_shift(map
);
401 *result
= load_result_null
;
403 lret
= parse_machfile(vp
, map
, thread
, header
, file_offset
, macho_size
,
404 0, (int64_t)aslr_offset
, (int64_t)dyld_aslr_offset
, result
);
406 if (lret
!= LOAD_SUCCESS
) {
408 vm_map_deallocate(map
); /* will lose pmap reference too */
415 * On x86, for compatibility, don't enforce the hard page-zero restriction for 32-bit binaries.
417 if ((imgp
->ip_flags
& IMGPF_IS_64BIT
) == 0) {
418 enforce_hard_pagezero
= FALSE
;
422 * Check to see if the page zero is enforced by the map->min_offset.
424 if (enforce_hard_pagezero
&&
425 (vm_map_has_hard_pagezero(map
, 0x1000) == FALSE
)) {
428 vm_map_deallocate(map
); /* will lose pmap reference too */
430 return (LOAD_BADMACHO
);
437 * Swap the new map for the old, which consumes our new map
438 * reference but each leaves us responsible for the old_map reference.
439 * That lets us get off the pmap associated with it, and
440 * then we can release it.
445 * If this is an exec, then we are going to destroy the old
446 * task, and it's correct to halt it; if it's spawn, the
447 * task is not yet running, and it makes no sense.
451 * Mark the task as halting and start the other
452 * threads towards terminating themselves. Then
453 * make sure any threads waiting for a process
454 * transition get informed that we are committed to
455 * this transition, and then finally complete the
456 * task halting (wait for threads and then cleanup
459 * NOTE: task_start_halt() makes sure that no new
460 * threads are created in the task during the transition.
461 * We need to mark the workqueue as exiting before we
462 * wait for threads to terminate (at the end of which
463 * we no longer have a prohibition on thread creation).
465 * Finally, clean up any lingering workqueue data structures
466 * that may have been left behind by the workqueue threads
467 * as they exited (and then clean up the work queue itself).
469 kret
= task_start_halt(task
);
470 if (kret
!= KERN_SUCCESS
) {
471 vm_map_deallocate(map
); /* will lose pmap reference too */
472 return (LOAD_FAILURE
);
474 proc_transcommit(p
, 0);
475 workqueue_mark_exiting(p
);
476 task_complete_halt(task
);
478 kqueue_dealloc(p
->p_wqkqueue
);
479 p
->p_wqkqueue
= NULL
;
481 old_map
= swap_task_map(old_task
, thread
, map
, !spawn
);
482 vm_map_deallocate(old_map
);
484 return(LOAD_SUCCESS
);
487 int macho_printf
= 0;
488 #define MACHO_PRINTF(args) \
490 if (macho_printf) { \
496 * The file size of a mach-o file is limited to 32 bits; this is because
497 * this is the limit on the kalloc() of enough bytes for a mach_header and
498 * the contents of its sizeofcmds, which is currently constrained to 32
499 * bits in the file format itself. We read into the kernel buffer the
500 * commands section, and then parse it in order to parse the mach-o file
501 * format load_command segment(s). We are only interested in a subset of
502 * the total set of possible commands. If "map"==VM_MAP_NULL or
503 * "thread"==THREAD_NULL, do not make permament VM modifications,
504 * just preflight the parse.
512 struct mach_header
*header
,
517 int64_t dyld_aslr_offset
,
518 load_result_t
*result
522 struct load_command
*lcp
;
523 struct dylinker_command
*dlp
= 0;
524 integer_t dlarchbits
= 0;
526 load_return_t ret
= LOAD_SUCCESS
;
529 vm_size_t size
,kl_size
;
531 size_t oldoffset
; /* for overflow check */
533 proc_t p
= current_proc(); /* XXXX */
536 size_t mach_header_sz
= sizeof(struct mach_header
);
538 boolean_t got_code_signatures
= FALSE
;
541 if (header
->magic
== MH_MAGIC_64
||
542 header
->magic
== MH_CIGAM_64
) {
543 mach_header_sz
= sizeof(struct mach_header_64
);
547 * Break infinite recursion
550 return(LOAD_FAILURE
);
556 * Check to see if right machine type.
558 if (((cpu_type_t
)(header
->cputype
& ~CPU_ARCH_MASK
) != (cpu_type() & ~CPU_ARCH_MASK
)) ||
559 !grade_binary(header
->cputype
,
560 header
->cpusubtype
& ~CPU_SUBTYPE_MASK
))
561 return(LOAD_BADARCH
);
563 abi64
= ((header
->cputype
& CPU_ARCH_ABI64
) == CPU_ARCH_ABI64
);
565 switch (header
->filetype
) {
569 return (LOAD_FAILURE
);
575 return (LOAD_FAILURE
);
580 return (LOAD_FAILURE
);
584 * Get the pager for the file.
586 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
589 * Map portion that must be accessible directly into
592 if ((off_t
)(mach_header_sz
+ header
->sizeofcmds
) > macho_size
)
593 return(LOAD_BADMACHO
);
596 * Round size of Mach-O commands up to page boundry.
598 size
= round_page(mach_header_sz
+ header
->sizeofcmds
);
600 return(LOAD_BADMACHO
);
603 * Map the load commands into kernel memory.
607 kl_addr
= kalloc(size
);
608 addr
= (caddr_t
)kl_addr
;
610 return(LOAD_NOSPACE
);
612 error
= vn_rdwr(UIO_READ
, vp
, addr
, size
, file_offset
,
613 UIO_SYSSPACE
, 0, kauth_cred_get(), &resid
, p
);
616 kfree(kl_addr
, kl_size
);
617 return(LOAD_IOERROR
);
621 /* We must be able to read in as much as the mach_header indicated */
623 kfree(kl_addr
, kl_size
);
624 return(LOAD_BADMACHO
);
628 * For PIE and dyld, slide everything by the ASLR offset.
630 if ((header
->flags
& MH_PIE
) || (header
->filetype
== MH_DYLINKER
)) {
635 * Scan through the commands, processing each one as necessary.
636 * We parse in three passes through the headers:
637 * 0: determine if TEXT and DATA boundary can be page-aligned
638 * 1: thread state, uuid, code signature
640 * 3: dyld, encryption, check entry point
643 for (pass
= 0; pass
<= 3; pass
++) {
646 /* see if we need to adjust the slide to re-align... */
647 /* no re-alignment needed on X86_64 or ARM32 kernel */
649 } else if (pass
== 1) {
653 * Check that the entry point is contained in an executable segments
655 if ((pass
== 3) && (!result
->using_lcmain
&& result
->validentry
== 0)) {
656 thread_state_initialize(thread
);
662 * Loop through each of the load_commands indicated by the
663 * Mach-O header; if an absurd value is provided, we just
664 * run off the end of the reserved section by incrementing
665 * the offset too far, so we are implicitly fail-safe.
667 offset
= mach_header_sz
;
668 ncmds
= header
->ncmds
;
672 * Get a pointer to the command.
674 lcp
= (struct load_command
*)(addr
+ offset
);
676 offset
+= lcp
->cmdsize
;
679 * Perform prevalidation of the struct load_command
680 * before we attempt to use its contents. Invalid
681 * values are ones which result in an overflow, or
682 * which can not possibly be valid commands, or which
683 * straddle or exist past the reserved section at the
684 * start of the image.
686 if (oldoffset
> offset
||
687 lcp
->cmdsize
< sizeof(struct load_command
) ||
688 offset
> header
->sizeofcmds
+ mach_header_sz
) {
694 * Act on struct load_command's for which kernel
695 * intervention is required.
708 * Having an LC_SEGMENT command for the
709 * wrong ABI is invalid <rdar://problem/11021230>
715 ret
= load_segment(lcp
,
731 * Having an LC_SEGMENT_64 command for the
732 * wrong ABI is invalid <rdar://problem/11021230>
738 ret
= load_segment(lcp
,
751 ret
= load_unixthread(
752 (struct thread_command
*) lcp
,
763 (struct entry_point_command
*) lcp
,
768 case LC_LOAD_DYLINKER
:
771 if ((depth
== 1) && (dlp
== 0)) {
772 dlp
= (struct dylinker_command
*)lcp
;
773 dlarchbits
= (header
->cputype
& CPU_ARCH_MASK
);
779 if (pass
== 1 && depth
== 1) {
780 ret
= load_uuid((struct uuid_command
*) lcp
,
781 (char *)addr
+ mach_header_sz
+ header
->sizeofcmds
,
785 case LC_CODE_SIGNATURE
:
790 load signatures & store in uip
791 set VM object "signed_pages"
793 ret
= load_code_signature(
794 (struct linkedit_data_command
*) lcp
,
800 if (ret
!= LOAD_SUCCESS
) {
801 printf("proc %d: load code signature error %d "
803 p
->p_pid
, ret
, vp
->v_name
);
805 * Allow injections to be ignored on devices w/o enforcement enabled
807 if (!cs_enforcement(NULL
))
808 ret
= LOAD_SUCCESS
; /* ignore error */
811 got_code_signatures
= TRUE
;
814 if (got_code_signatures
) {
815 unsigned tainted
= CS_VALIDATE_TAINTED
;
816 boolean_t valid
= FALSE
;
817 struct cs_blob
*blobs
;
822 printf("validating initial pages of %s\n", vp
->v_name
);
823 blobs
= ubc_get_cs_blobs(vp
);
825 while (off
< size
&& ret
== LOAD_SUCCESS
) {
826 tainted
= CS_VALIDATE_TAINTED
;
828 valid
= cs_validate_page(blobs
,
833 if (!valid
|| (tainted
& CS_VALIDATE_TAINTED
)) {
835 printf("CODE SIGNING: %s[%d]: invalid initial page at offset %lld validated:%d tainted:%d csflags:0x%x\n",
836 vp
->v_name
, p
->p_pid
, (long long)(file_offset
+ off
), valid
, tainted
, result
->csflags
);
837 if (cs_enforcement(NULL
) ||
838 (result
->csflags
& (CS_HARD
|CS_KILL
|CS_ENFORCEMENT
))) {
841 result
->csflags
&= ~CS_VALID
;
848 #if CONFIG_CODE_DECRYPTION
849 case LC_ENCRYPTION_INFO
:
850 case LC_ENCRYPTION_INFO_64
:
853 ret
= set_code_unprotect(
854 (struct encryption_info_command
*) lcp
,
855 addr
, map
, slide
, vp
, file_offset
,
856 header
->cputype
, header
->cpusubtype
);
857 if (ret
!= LOAD_SUCCESS
) {
858 printf("proc %d: set_code_unprotect() error %d "
860 p
->p_pid
, ret
, vp
->v_name
);
862 * Don't let the app run if it's
863 * encrypted but we failed to set up the
864 * decrypter. If the keys are missing it will
865 * return LOAD_DECRYPTFAIL.
867 if (ret
== LOAD_DECRYPTFAIL
) {
868 /* failed to load due to missing FP keys */
870 p
->p_lflag
|= P_LTERM_DECRYPTFAIL
;
878 /* Other commands are ignored by the kernel */
882 if (ret
!= LOAD_SUCCESS
)
885 if (ret
!= LOAD_SUCCESS
)
889 if (ret
== LOAD_SUCCESS
) {
890 if (! got_code_signatures
) {
891 if (cs_enforcement(NULL
)) {
895 * No embedded signatures: look for detached by taskgated,
896 * this is only done on OSX, on embedded platforms we expect everything
897 * to be have embedded signatures.
899 struct cs_blob
*blob
;
901 blob
= ubc_cs_blob_get(vp
, -1, file_offset
);
903 unsigned int cs_flag_data
= blob
->csb_flags
;
904 if(0 != ubc_cs_generation_check(vp
)) {
905 if (0 != ubc_cs_blob_revalidate(vp
, blob
, 0)) {
906 /* clear out the flag data if revalidation fails */
908 result
->csflags
&= ~CS_VALID
;
911 /* get flags to be applied to the process */
912 result
->csflags
|= cs_flag_data
;
917 /* Make sure if we need dyld, we got it */
918 if (result
->needs_dynlinker
&& !dlp
) {
922 if ((ret
== LOAD_SUCCESS
) && (dlp
!= 0)) {
924 * load the dylinker, and slide it by the independent DYLD ASLR
925 * offset regardless of the PIE-ness of the main binary.
927 ret
= load_dylinker(dlp
, dlarchbits
, map
, thread
, depth
,
928 dyld_aslr_offset
, result
);
931 if((ret
== LOAD_SUCCESS
) && (depth
== 1)) {
932 if (result
->thread_count
== 0) {
939 kfree(kl_addr
, kl_size
);
944 #if CONFIG_CODE_DECRYPTION
946 #define APPLE_UNPROTECTED_HEADER_SIZE (3 * 4096)
949 unprotect_dsmos_segment(
955 vm_map_offset_t map_addr
,
956 vm_map_size_t map_size
)
961 * The first APPLE_UNPROTECTED_HEADER_SIZE bytes (from offset 0 of
962 * this part of a Universal binary) are not protected...
963 * The rest needs to be "transformed".
965 if (file_off
<= APPLE_UNPROTECTED_HEADER_SIZE
&&
966 file_off
+ file_size
<= APPLE_UNPROTECTED_HEADER_SIZE
) {
967 /* it's all unprotected, nothing to do... */
970 if (file_off
<= APPLE_UNPROTECTED_HEADER_SIZE
) {
972 * We start mapping in the unprotected area.
973 * Skip the unprotected part...
975 vm_map_offset_t delta
;
977 delta
= APPLE_UNPROTECTED_HEADER_SIZE
;
982 /* ... transform the rest of the mapping. */
983 struct pager_crypt_info crypt_info
;
984 crypt_info
.page_decrypt
= dsmos_page_transform
;
985 crypt_info
.crypt_ops
= NULL
;
986 crypt_info
.crypt_end
= NULL
;
987 #pragma unused(vp, macho_offset)
988 crypt_info
.crypt_ops
= (void *)0x2e69cf40;
989 vm_map_offset_t crypto_backing_offset
;
990 crypto_backing_offset
= -1; /* i.e. use map entry's offset */
991 #if DEVELOPMENT || DEBUG
994 printf("APPLE_PROTECT: %d[%s] map %p [0x%llx:0x%llx] %s(%s)\n",
995 p
->p_pid
, p
->p_comm
, map
,
996 (uint64_t) map_addr
, (uint64_t) (map_addr
+ map_size
),
997 __FUNCTION__
, vp
->v_name
);
998 #endif /* DEVELOPMENT || DEBUG */
999 kr
= vm_map_apple_protected(map
,
1001 map_addr
+ map_size
,
1002 crypto_backing_offset
,
1006 if (kr
!= KERN_SUCCESS
) {
1007 return LOAD_FAILURE
;
1009 return LOAD_SUCCESS
;
1011 #else /* CONFIG_CODE_DECRYPTION */
1012 static load_return_t
1013 unprotect_dsmos_segment(
1014 __unused
uint64_t file_off
,
1015 __unused
uint64_t file_size
,
1016 __unused
struct vnode
*vp
,
1017 __unused off_t macho_offset
,
1018 __unused vm_map_t map
,
1019 __unused vm_map_offset_t map_addr
,
1020 __unused vm_map_size_t map_size
)
1022 return LOAD_SUCCESS
;
1024 #endif /* CONFIG_CODE_DECRYPTION */
1029 * Maps a Mach-O segment, taking care of mis-alignment (wrt the system
1030 * page size) issues.
1032 * The mapping might result in 1, 2 or 3 map entries:
1033 * 1. for the first page, which could be overlap with the previous
1035 * 2. for the center (if applicable),
1036 * 3. for the last page, which could overlap with the next mapping.
1038 * For each of those map entries, we might have to interpose a
1039 * "fourk_pager" to deal with mis-alignment wrt the system page size,
1040 * either in the mapping address and/or size or the file offset and/or
1042 * The "fourk_pager" itself would be mapped with proper alignment
1043 * wrt the system page size and would then be populated with the
1044 * information about the intended mapping, with a "4KB" granularity.
1046 static kern_return_t
1049 vm_map_offset_t vm_start
,
1050 vm_map_offset_t vm_end
,
1051 memory_object_control_t control
,
1052 vm_map_offset_t file_start
,
1053 vm_map_offset_t file_end
,
1057 int extra_vm_flags
, cur_extra_vm_flags
;
1058 vm_map_offset_t cur_offset
, cur_start
, cur_end
;
1060 vm_map_offset_t effective_page_mask
;
1062 if (vm_end
< vm_start
||
1063 file_end
< file_start
) {
1064 return LOAD_BADMACHO
;
1066 if (vm_end
== vm_start
||
1067 file_end
== file_start
) {
1068 /* nothing to map... */
1069 return LOAD_SUCCESS
;
1072 effective_page_mask
= MAX(PAGE_MASK
, vm_map_page_mask(map
));
1075 if (vm_map_page_aligned(vm_start
, effective_page_mask
) &&
1076 vm_map_page_aligned(vm_end
, effective_page_mask
) &&
1077 vm_map_page_aligned(file_start
, effective_page_mask
) &&
1078 vm_map_page_aligned(file_end
, effective_page_mask
)) {
1079 /* all page-aligned and map-aligned: proceed */
1081 panic("map_segment: unexpected mis-alignment "
1082 "vm[0x%llx:0x%llx] file[0x%llx:0x%llx]\n",
1083 (uint64_t) vm_start
,
1085 (uint64_t) file_start
,
1086 (uint64_t) file_end
);
1090 cur_start
= vm_start
;
1092 if (cur_end
>= vm_start
+ (file_end
- file_start
)) {
1093 /* all mapped: done */
1096 if (vm_map_round_page(cur_end
, effective_page_mask
) >=
1097 vm_map_trunc_page(vm_start
+ (file_end
- file_start
),
1098 effective_page_mask
)) {
1101 cur_start
= cur_end
;
1102 if ((vm_start
& effective_page_mask
) !=
1103 (file_start
& effective_page_mask
)) {
1104 /* one 4K pager for the middle */
1105 cur_extra_vm_flags
= extra_vm_flags
;
1107 /* regular mapping for the middle */
1108 cur_extra_vm_flags
= 0;
1110 cur_end
= vm_map_trunc_page(vm_start
+ (file_end
-
1112 effective_page_mask
);
1113 if (control
!= MEMORY_OBJECT_CONTROL_NULL
) {
1114 ret
= vm_map_enter_mem_object_control(
1117 cur_end
- cur_start
,
1118 (mach_vm_offset_t
)0,
1119 VM_FLAGS_FIXED
| cur_extra_vm_flags
,
1121 file_start
+ cur_offset
,
1124 VM_INHERIT_DEFAULT
);
1126 ret
= vm_map_enter_mem_object(
1129 cur_end
- cur_start
,
1130 (mach_vm_offset_t
)0,
1131 VM_FLAGS_FIXED
| cur_extra_vm_flags
,
1136 VM_INHERIT_DEFAULT
);
1138 if (ret
!= KERN_SUCCESS
) {
1139 return (LOAD_NOSPACE
);
1141 cur_offset
+= cur_end
- cur_start
;
1143 if (cur_end
>= vm_start
+ (file_end
- file_start
)) {
1144 /* all mapped: done */
1147 cur_start
= cur_end
;
1149 assert(cur_end
>= vm_start
+ (file_end
- file_start
));
1150 return LOAD_SUCCESS
;
1156 struct load_command
*lcp
,
1164 load_result_t
*result
)
1166 struct segment_command_64 segment_command
, *scp
;
1168 vm_map_size_t delta_size
;
1171 size_t segment_command_size
, total_section_size
,
1172 single_section_size
;
1173 vm_map_offset_t file_offset
, file_size
;
1174 vm_map_offset_t vm_offset
, vm_size
;
1175 vm_map_offset_t vm_start
, vm_end
, vm_end_aligned
;
1176 vm_map_offset_t file_start
, file_end
;
1179 vm_map_size_t effective_page_size
;
1180 vm_map_offset_t effective_page_mask
;
1182 effective_page_size
= MAX(PAGE_SIZE
, vm_map_page_size(map
));
1183 effective_page_mask
= MAX(PAGE_MASK
, vm_map_page_mask(map
));
1186 if (LC_SEGMENT_64
== lcp
->cmd
) {
1187 segment_command_size
= sizeof(struct segment_command_64
);
1188 single_section_size
= sizeof(struct section_64
);
1190 segment_command_size
= sizeof(struct segment_command
);
1191 single_section_size
= sizeof(struct section
);
1193 if (lcp
->cmdsize
< segment_command_size
)
1194 return (LOAD_BADMACHO
);
1195 total_section_size
= lcp
->cmdsize
- segment_command_size
;
1197 if (LC_SEGMENT_64
== lcp
->cmd
) {
1198 scp
= (struct segment_command_64
*)lcp
;
1200 scp
= &segment_command
;
1201 widen_segment_command((struct segment_command
*)lcp
, scp
);
1205 MACHO_PRINTF(("+++ load_segment %s "
1206 "vm[0x%llx:0x%llx] file[0x%llx:0x%llx] "
1207 "prot %d/%d flags 0x%x\n",
1209 (uint64_t)(slide
+ scp
->vmaddr
),
1210 (uint64_t)(slide
+ scp
->vmaddr
+ scp
->vmsize
),
1211 pager_offset
+ scp
->fileoff
,
1212 pager_offset
+ scp
->fileoff
+ scp
->filesize
,
1219 * Make sure what we get from the file is really ours (as specified
1222 if (scp
->fileoff
+ scp
->filesize
< scp
->fileoff
||
1223 scp
->fileoff
+ scp
->filesize
> (uint64_t)macho_size
) {
1224 return (LOAD_BADMACHO
);
1227 * Ensure that the number of sections specified would fit
1228 * within the load command size.
1230 if (total_section_size
/ single_section_size
< scp
->nsects
) {
1231 return (LOAD_BADMACHO
);
1234 * Make sure the segment is page-aligned in the file.
1236 file_offset
= pager_offset
+ scp
->fileoff
; /* limited to 32 bits */
1237 file_size
= scp
->filesize
;
1238 if ((file_offset
& PAGE_MASK_64
) != 0 ||
1239 /* we can't mmap() it if it's not page-aligned in the file */
1240 (file_offset
& vm_map_page_mask(map
)) != 0) {
1242 * The 1st test would have failed if the system's page size
1243 * was what this process believe is the page size, so let's
1244 * fail here too for the sake of consistency.
1246 return (LOAD_BADMACHO
);
1250 * If we have a code signature attached for this slice
1251 * require that the segments are within the signed part
1254 if (result
->cs_end_offset
&&
1255 result
->cs_end_offset
< (off_t
)scp
->fileoff
&&
1256 result
->cs_end_offset
- scp
->fileoff
< scp
->filesize
)
1259 printf("section outside code signature\n");
1260 return LOAD_BADMACHO
;
1263 vm_offset
= scp
->vmaddr
+ slide
;
1264 vm_size
= scp
->vmsize
;
1267 return (LOAD_SUCCESS
);
1268 if (scp
->vmaddr
== 0 &&
1271 (scp
->initprot
& VM_PROT_ALL
) == VM_PROT_NONE
&&
1272 (scp
->maxprot
& VM_PROT_ALL
) == VM_PROT_NONE
) {
1274 * For PIE, extend page zero rather than moving it. Extending
1275 * page zero keeps early allocations from falling predictably
1276 * between the end of page zero and the beginning of the first
1280 * This is a "page zero" segment: it starts at address 0,
1281 * is not mapped from the binary file and is not accessible.
1282 * User-space should never be able to access that memory, so
1283 * make it completely off limits by raising the VM map's
1286 vm_end
= vm_offset
+ vm_size
;
1287 if (vm_end
< vm_offset
) {
1288 return (LOAD_BADMACHO
);
1291 MACHO_PRINTF(("++++++ load_segment: "
1292 "page_zero up to 0x%llx\n",
1293 (uint64_t) vm_end
));
1296 vm_end
= vm_map_round_page(vm_end
,
1298 vm_end_aligned
= vm_end
;
1300 ret
= vm_map_raise_min_offset(map
,
1303 if (ret
!= KERN_SUCCESS
) {
1304 return (LOAD_FAILURE
);
1306 return (LOAD_SUCCESS
);
1311 file_start
= vm_map_trunc_page(file_offset
,
1312 effective_page_mask
);
1313 file_end
= vm_map_round_page(file_offset
+ file_size
,
1314 effective_page_mask
);
1315 vm_start
= vm_map_trunc_page(vm_offset
,
1316 effective_page_mask
);
1317 vm_end
= vm_map_round_page(vm_offset
+ vm_size
,
1318 effective_page_mask
);
1321 if (vm_start
< result
->min_vm_addr
)
1322 result
->min_vm_addr
= vm_start
;
1323 if (vm_end
> result
->max_vm_addr
)
1324 result
->max_vm_addr
= vm_end
;
1326 if (map
== VM_MAP_NULL
)
1327 return (LOAD_SUCCESS
);
1330 initprot
= (scp
->initprot
) & VM_PROT_ALL
;
1331 maxprot
= (scp
->maxprot
) & VM_PROT_ALL
;
1333 * Map a copy of the file into the address space.
1336 MACHO_PRINTF(("++++++ load_segment: "
1337 "mapping at vm [0x%llx:0x%llx] of "
1338 "file [0x%llx:0x%llx]\n",
1339 (uint64_t) vm_start
,
1341 (uint64_t) file_start
,
1342 (uint64_t) file_end
));
1344 ret
= map_segment(map
,
1353 return LOAD_NOSPACE
;
1358 * If the file didn't end on a page boundary,
1359 * we need to zero the leftover.
1361 delta_size
= map_size
- scp
->filesize
;
1362 if (delta_size
> 0) {
1363 mach_vm_offset_t tmp
;
1365 ret
= mach_vm_allocate(kernel_map
, &tmp
, delta_size
, VM_FLAGS_ANYWHERE
| VM_MAKE_TAG(VM_KERN_MEMORY_BSD
));
1366 if (ret
!= KERN_SUCCESS
) {
1367 return(LOAD_RESOURCE
);
1370 if (copyout(tmp
, map_addr
+ scp
->filesize
,
1372 (void) mach_vm_deallocate(
1373 kernel_map
, tmp
, delta_size
);
1374 return (LOAD_FAILURE
);
1377 (void) mach_vm_deallocate(kernel_map
, tmp
, delta_size
);
1383 * If the virtual size of the segment is greater
1384 * than the size from the file, we need to allocate
1385 * zero fill memory for the rest.
1387 if ((vm_end
- vm_start
) > (file_end
- file_start
)) {
1388 delta_size
= (vm_end
- vm_start
) - (file_end
- file_start
);
1392 if (delta_size
> 0) {
1393 mach_vm_offset_t tmp
;
1395 tmp
= vm_start
+ (file_end
- file_start
);
1397 MACHO_PRINTF(("++++++ load_segment: "
1398 "delta mapping vm [0x%llx:0x%llx]\n",
1400 (uint64_t) (tmp
+ delta_size
)));
1402 kr
= map_segment(map
,
1405 MEMORY_OBJECT_CONTROL_NULL
,
1410 if (kr
!= KERN_SUCCESS
) {
1411 return(LOAD_NOSPACE
);
1415 if ( (scp
->fileoff
== 0) && (scp
->filesize
!= 0) )
1416 result
->mach_header
= vm_offset
;
1418 if (scp
->flags
& SG_PROTECTED_VERSION_1
) {
1419 ret
= unprotect_dsmos_segment(file_start
,
1420 file_end
- file_start
,
1426 if (ret
!= LOAD_SUCCESS
) {
1433 if (LOAD_SUCCESS
== ret
&&
1434 filetype
== MH_DYLINKER
&&
1435 result
->all_image_info_addr
== MACH_VM_MIN_ADDRESS
) {
1436 note_all_image_info_section(scp
,
1437 LC_SEGMENT_64
== lcp
->cmd
,
1438 single_section_size
,
1439 ((const char *)lcp
+
1440 segment_command_size
),
1445 if (result
->entry_point
!= MACH_VM_MIN_ADDRESS
) {
1446 if ((result
->entry_point
>= vm_offset
) && (result
->entry_point
< (vm_offset
+ vm_size
))) {
1447 if ((scp
->initprot
& (VM_PROT_READ
|VM_PROT_EXECUTE
)) == (VM_PROT_READ
|VM_PROT_EXECUTE
)) {
1448 result
->validentry
= 1;
1450 /* right range but wrong protections, unset if previously validated */
1451 result
->validentry
= 0;
1462 struct uuid_command
*uulp
,
1464 load_result_t
*result
1468 * We need to check the following for this command:
1469 * - The command size should be atleast the size of struct uuid_command
1470 * - The UUID part of the command should be completely within the mach-o header
1473 if ((uulp
->cmdsize
< sizeof(struct uuid_command
)) ||
1474 (((char *)uulp
+ sizeof(struct uuid_command
)) > command_end
)) {
1475 return (LOAD_BADMACHO
);
1478 memcpy(&result
->uuid
[0], &uulp
->uuid
[0], sizeof(result
->uuid
));
1479 return (LOAD_SUCCESS
);
1485 struct entry_point_command
*epc
,
1488 load_result_t
*result
1491 mach_vm_offset_t addr
;
1494 if (epc
->cmdsize
< sizeof(*epc
))
1495 return (LOAD_BADMACHO
);
1496 if (result
->thread_count
!= 0) {
1497 return (LOAD_FAILURE
);
1500 if (thread
== THREAD_NULL
)
1501 return (LOAD_SUCCESS
);
1503 /* LC_MAIN specifies stack size but not location */
1504 if (epc
->stacksize
) {
1505 result
->prog_stack_size
= 1;
1506 result
->user_stack_size
= epc
->stacksize
;
1508 result
->prog_stack_size
= 0;
1509 result
->user_stack_size
= MAXSSIZ
;
1511 result
->prog_allocated_stack
= 0;
1513 /* use default location for stack */
1514 ret
= thread_userstackdefault(thread
, &addr
);
1515 if (ret
!= KERN_SUCCESS
)
1516 return(LOAD_FAILURE
);
1518 /* The stack slides down from the default location */
1519 result
->user_stack
= addr
;
1520 result
->user_stack
-= slide
;
1522 if (result
->using_lcmain
|| result
->entry_point
!= MACH_VM_MIN_ADDRESS
) {
1523 /* Already processed LC_MAIN or LC_UNIXTHREAD */
1524 return (LOAD_FAILURE
);
1527 /* kernel does *not* use entryoff from LC_MAIN. Dyld uses it. */
1528 result
->needs_dynlinker
= TRUE
;
1529 result
->using_lcmain
= TRUE
;
1531 ret
= thread_state_initialize( thread
);
1532 if (ret
!= KERN_SUCCESS
) {
1533 return(LOAD_FAILURE
);
1537 result
->unixproc
= TRUE
;
1538 result
->thread_count
++;
1540 return(LOAD_SUCCESS
);
1547 struct thread_command
*tcp
,
1550 load_result_t
*result
1555 mach_vm_offset_t addr
;
1557 if (tcp
->cmdsize
< sizeof(*tcp
))
1558 return (LOAD_BADMACHO
);
1559 if (result
->thread_count
!= 0) {
1560 return (LOAD_FAILURE
);
1563 if (thread
== THREAD_NULL
)
1564 return (LOAD_SUCCESS
);
1566 ret
= load_threadstack(thread
,
1567 (uint32_t *)(((vm_offset_t
)tcp
) +
1568 sizeof(struct thread_command
)),
1569 tcp
->cmdsize
- sizeof(struct thread_command
),
1572 if (ret
!= LOAD_SUCCESS
)
1575 /* LC_UNIXTHREAD optionally specifies stack size and location */
1578 result
->prog_stack_size
= 0; /* unknown */
1579 result
->prog_allocated_stack
= 1;
1581 result
->prog_allocated_stack
= 0;
1582 result
->prog_stack_size
= 0;
1583 result
->user_stack_size
= MAXSSIZ
;
1586 /* The stack slides down from the default location */
1587 result
->user_stack
= addr
;
1588 result
->user_stack
-= slide
;
1590 ret
= load_threadentry(thread
,
1591 (uint32_t *)(((vm_offset_t
)tcp
) +
1592 sizeof(struct thread_command
)),
1593 tcp
->cmdsize
- sizeof(struct thread_command
),
1595 if (ret
!= LOAD_SUCCESS
)
1598 if (result
->using_lcmain
|| result
->entry_point
!= MACH_VM_MIN_ADDRESS
) {
1599 /* Already processed LC_MAIN or LC_UNIXTHREAD */
1600 return (LOAD_FAILURE
);
1603 result
->entry_point
= addr
;
1604 result
->entry_point
+= slide
;
1606 ret
= load_threadstate(thread
,
1607 (uint32_t *)(((vm_offset_t
)tcp
) +
1608 sizeof(struct thread_command
)),
1609 tcp
->cmdsize
- sizeof(struct thread_command
));
1610 if (ret
!= LOAD_SUCCESS
)
1614 result
->unixproc
= TRUE
;
1615 result
->thread_count
++;
1617 return(LOAD_SUCCESS
);
1631 uint32_t thread_size
;
1633 uint32_t local_ts_size
;
1638 ret
= thread_state_initialize( thread
);
1639 if (ret
!= KERN_SUCCESS
) {
1644 if (total_size
> 0) {
1645 local_ts_size
= total_size
;
1646 local_ts
= kalloc(local_ts_size
);
1647 if (local_ts
== NULL
) {
1651 memcpy(local_ts
, ts
, local_ts_size
);
1656 * Set the new thread state; iterate through the state flavors in
1659 while (total_size
> 0) {
1662 if (UINT32_MAX
-2 < size
||
1663 UINT32_MAX
/sizeof(uint32_t) < size
+2) {
1664 ret
= LOAD_BADMACHO
;
1667 thread_size
= (size
+2)*sizeof(uint32_t);
1668 if (thread_size
> total_size
) {
1669 ret
= LOAD_BADMACHO
;
1672 total_size
-= thread_size
;
1674 * Third argument is a kernel space pointer; it gets cast
1675 * to the appropriate type in machine_thread_set_state()
1676 * based on the value of flavor.
1678 ret
= thread_setstatus(thread
, flavor
, (thread_state_t
)ts
, size
);
1679 if (ret
!= KERN_SUCCESS
) {
1683 ts
+= size
; /* ts is a (uint32_t *) */
1688 if (local_ts
!= NULL
) {
1689 kfree(local_ts
, local_ts_size
);
1700 uint32_t total_size
,
1701 mach_vm_offset_t
*user_stack
,
1708 uint32_t stack_size
;
1710 while (total_size
> 0) {
1713 if (UINT32_MAX
-2 < size
||
1714 UINT32_MAX
/sizeof(uint32_t) < size
+2)
1715 return (LOAD_BADMACHO
);
1716 stack_size
= (size
+2)*sizeof(uint32_t);
1717 if (stack_size
> total_size
)
1718 return(LOAD_BADMACHO
);
1719 total_size
-= stack_size
;
1722 * Third argument is a kernel space pointer; it gets cast
1723 * to the appropriate type in thread_userstack() based on
1724 * the value of flavor.
1726 ret
= thread_userstack(thread
, flavor
, (thread_state_t
)ts
, size
, user_stack
, customstack
);
1727 if (ret
!= KERN_SUCCESS
) {
1728 return(LOAD_FAILURE
);
1730 ts
+= size
; /* ts is a (uint32_t *) */
1732 return(LOAD_SUCCESS
);
1740 uint32_t total_size
,
1741 mach_vm_offset_t
*entry_point
1747 uint32_t entry_size
;
1750 * Set the thread state.
1752 *entry_point
= MACH_VM_MIN_ADDRESS
;
1753 while (total_size
> 0) {
1756 if (UINT32_MAX
-2 < size
||
1757 UINT32_MAX
/sizeof(uint32_t) < size
+2)
1758 return (LOAD_BADMACHO
);
1759 entry_size
= (size
+2)*sizeof(uint32_t);
1760 if (entry_size
> total_size
)
1761 return(LOAD_BADMACHO
);
1762 total_size
-= entry_size
;
1764 * Third argument is a kernel space pointer; it gets cast
1765 * to the appropriate type in thread_entrypoint() based on
1766 * the value of flavor.
1768 ret
= thread_entrypoint(thread
, flavor
, (thread_state_t
)ts
, size
, entry_point
);
1769 if (ret
!= KERN_SUCCESS
) {
1770 return(LOAD_FAILURE
);
1772 ts
+= size
; /* ts is a (uint32_t *) */
1774 return(LOAD_SUCCESS
);
1778 struct nameidata __nid
;
1779 union macho_vnode_header
{
1780 struct mach_header mach_header
;
1781 struct fat_header fat_header
;
1786 #define DEFAULT_DYLD_PATH "/usr/lib/dyld"
1788 static load_return_t
1790 struct dylinker_command
*lcp
,
1796 load_result_t
*result
1801 struct vnode
*vp
= NULLVP
; /* set by get_macho_vnode() */
1802 struct mach_header
*header
;
1803 off_t file_offset
= 0; /* set by get_macho_vnode() */
1804 off_t macho_size
= 0; /* set by get_macho_vnode() */
1805 load_result_t
*myresult
;
1807 struct macho_data
*macho_data
;
1809 struct mach_header __header
;
1810 load_result_t __myresult
;
1811 struct macho_data __macho_data
;
1814 if (lcp
->cmdsize
< sizeof(*lcp
))
1815 return (LOAD_BADMACHO
);
1817 name
= (char *)lcp
+ lcp
->name
.offset
;
1819 * Check for a proper null terminated string.
1823 if (p
>= (char *)lcp
+ lcp
->cmdsize
)
1824 return(LOAD_BADMACHO
);
1827 #if !(DEVELOPMENT || DEBUG)
1828 if (0 != strcmp(name
, DEFAULT_DYLD_PATH
)) {
1829 return (LOAD_BADMACHO
);
1833 /* Allocate wad-of-data from heap to reduce excessively deep stacks */
1835 MALLOC(dyld_data
, void *, sizeof (*dyld_data
), M_TEMP
, M_WAITOK
);
1836 header
= &dyld_data
->__header
;
1837 myresult
= &dyld_data
->__myresult
;
1838 macho_data
= &dyld_data
->__macho_data
;
1840 ret
= get_macho_vnode(name
, archbits
, header
,
1841 &file_offset
, &macho_size
, macho_data
, &vp
);
1845 *myresult
= load_result_null
;
1848 * First try to map dyld in directly. This should work most of
1849 * the time since there shouldn't normally be something already
1850 * mapped to its address.
1853 ret
= parse_machfile(vp
, map
, thread
, header
, file_offset
,
1854 macho_size
, depth
, slide
, 0, myresult
);
1857 * If it turned out something was in the way, then we'll take
1858 * take this longer path to preflight dyld's vm ranges, then
1859 * map it at a free location in the address space.
1862 if (ret
== LOAD_NOSPACE
) {
1863 mach_vm_offset_t dyl_start
, map_addr
;
1864 mach_vm_size_t dyl_length
;
1865 int64_t slide_amount
;
1867 *myresult
= load_result_null
;
1870 * Preflight parsing the Mach-O file with a NULL
1871 * map, which will return the ranges needed for a
1872 * subsequent map attempt (with a slide) in "myresult"
1874 ret
= parse_machfile(vp
, VM_MAP_NULL
, THREAD_NULL
, header
,
1875 file_offset
, macho_size
, depth
,
1876 0 /* slide */, 0, myresult
);
1878 if (ret
!= LOAD_SUCCESS
) {
1882 dyl_start
= myresult
->min_vm_addr
;
1883 dyl_length
= myresult
->max_vm_addr
- myresult
->min_vm_addr
;
1885 dyl_length
+= slide
;
1887 /* To find an appropriate load address, do a quick allocation */
1888 map_addr
= dyl_start
;
1889 ret
= mach_vm_allocate(map
, &map_addr
, dyl_length
, VM_FLAGS_ANYWHERE
);
1890 if (ret
!= KERN_SUCCESS
) {
1895 ret
= mach_vm_deallocate(map
, map_addr
, dyl_length
);
1896 if (ret
!= KERN_SUCCESS
) {
1901 if (map_addr
< dyl_start
)
1902 slide_amount
= -(int64_t)(dyl_start
- map_addr
);
1904 slide_amount
= (int64_t)(map_addr
- dyl_start
);
1906 slide_amount
+= slide
;
1908 *myresult
= load_result_null
;
1910 ret
= parse_machfile(vp
, map
, thread
, header
,
1911 file_offset
, macho_size
, depth
,
1912 slide_amount
, 0, myresult
);
1919 if (ret
== LOAD_SUCCESS
) {
1920 result
->dynlinker
= TRUE
;
1921 result
->entry_point
= myresult
->entry_point
;
1922 result
->validentry
= myresult
->validentry
;
1923 result
->all_image_info_addr
= myresult
->all_image_info_addr
;
1924 result
->all_image_info_size
= myresult
->all_image_info_size
;
1925 if (myresult
->platform_binary
) {
1926 result
->csflags
|= CS_DYLD_PLATFORM
;
1932 FREE(dyld_data
, M_TEMP
);
1937 static load_return_t
1938 load_code_signature(
1939 struct linkedit_data_command
*lcp
,
1944 load_result_t
*result
)
1950 struct cs_blob
*blob
;
1952 vm_size_t blob_size
;
1957 if (lcp
->cmdsize
!= sizeof (struct linkedit_data_command
) ||
1958 lcp
->dataoff
+ lcp
->datasize
> macho_size
) {
1959 ret
= LOAD_BADMACHO
;
1963 blob
= ubc_cs_blob_get(vp
, cputype
, macho_offset
);
1965 /* we already have a blob for this vnode and cputype */
1966 if (blob
->csb_cpu_type
== cputype
&&
1967 blob
->csb_base_offset
== macho_offset
&&
1968 blob
->csb_mem_size
== lcp
->datasize
) {
1969 /* it matches the blob we want here, lets verify the version */
1970 if(0 != ubc_cs_generation_check(vp
)) {
1971 if (0 != ubc_cs_blob_revalidate(vp
, blob
, 0)) {
1972 ret
= LOAD_FAILURE
; /* set error same as from ubc_cs_blob_add */
1978 /* the blob has changed for this vnode: fail ! */
1979 ret
= LOAD_BADMACHO
;
1984 blob_size
= lcp
->datasize
;
1985 kr
= ubc_cs_blob_allocate(&addr
, &blob_size
);
1986 if (kr
!= KERN_SUCCESS
) {
1992 error
= vn_rdwr(UIO_READ
,
1996 macho_offset
+ lcp
->dataoff
,
2002 if (error
|| resid
!= 0) {
2007 if (ubc_cs_blob_add(vp
,
2017 /* ubc_cs_blob_add() has consumed "addr" */
2021 #if CHECK_CS_VALIDATION_BITMAP
2022 ubc_cs_validation_bitmap_allocate( vp
);
2027 if (ret
== LOAD_SUCCESS
) {
2029 panic("sucess, but no blob!");
2031 result
->csflags
|= blob
->csb_flags
;
2032 result
->platform_binary
= blob
->csb_platform_binary
;
2033 result
->cs_end_offset
= blob
->csb_end_offset
;
2036 ubc_cs_blob_deallocate(addr
, blob_size
);
2044 #if CONFIG_CODE_DECRYPTION
2046 static load_return_t
2048 struct encryption_info_command
*eip
,
2055 cpu_subtype_t cpusubtype
)
2058 pager_crypt_info_t crypt_info
;
2059 const char * cryptname
= 0;
2063 struct segment_command_64
*seg64
;
2064 struct segment_command
*seg32
;
2065 vm_map_offset_t map_offset
, map_size
;
2066 vm_object_offset_t crypto_backing_offset
;
2069 if (eip
->cmdsize
< sizeof(*eip
)) return LOAD_BADMACHO
;
2071 switch(eip
->cryptid
) {
2073 /* not encrypted, just an empty load command */
2074 return LOAD_SUCCESS
;
2076 cryptname
="com.apple.unfree";
2079 /* some random cryptid that you could manually put into
2080 * your binary if you want NULL */
2081 cryptname
="com.apple.null";
2084 return LOAD_BADMACHO
;
2087 if (map
== VM_MAP_NULL
) return (LOAD_SUCCESS
);
2088 if (NULL
== text_crypter_create
) return LOAD_FAILURE
;
2090 MALLOC_ZONE(vpath
, char *, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
2091 if(vpath
== NULL
) return LOAD_FAILURE
;
2094 error
= vn_getpath(vp
, vpath
, &len
);
2096 FREE_ZONE(vpath
, MAXPATHLEN
, M_NAMEI
);
2097 return LOAD_FAILURE
;
2100 /* set up decrypter first */
2101 crypt_file_data_t crypt_data
= {
2104 .cpusubtype
= cpusubtype
};
2105 kr
=text_crypter_create(&crypt_info
, cryptname
, (void*)&crypt_data
);
2106 #if DEVELOPMENT || DEBUG
2109 printf("APPLE_PROTECT: %d[%s] map %p %s(%s) -> 0x%x\n",
2110 p
->p_pid
, p
->p_comm
, map
, __FUNCTION__
, vpath
, kr
);
2111 #endif /* DEVELOPMENT || DEBUG */
2112 FREE_ZONE(vpath
, MAXPATHLEN
, M_NAMEI
);
2115 printf("set_code_unprotect: unable to create decrypter %s, kr=%d\n",
2117 if (kr
== kIOReturnNotPrivileged
) {
2118 /* text encryption returned decryption failure */
2119 return(LOAD_DECRYPTFAIL
);
2121 return LOAD_RESOURCE
;
2124 /* this is terrible, but we have to rescan the load commands to find the
2125 * virtual address of this encrypted stuff. This code is gonna look like
2126 * the dyld source one day... */
2127 struct mach_header
*header
= (struct mach_header
*)addr
;
2128 size_t mach_header_sz
= sizeof(struct mach_header
);
2129 if (header
->magic
== MH_MAGIC_64
||
2130 header
->magic
== MH_CIGAM_64
) {
2131 mach_header_sz
= sizeof(struct mach_header_64
);
2133 offset
= mach_header_sz
;
2134 uint32_t ncmds
= header
->ncmds
;
2137 * Get a pointer to the command.
2139 struct load_command
*lcp
= (struct load_command
*)(addr
+ offset
);
2140 offset
+= lcp
->cmdsize
;
2144 seg64
= (struct segment_command_64
*)lcp
;
2145 if ((seg64
->fileoff
<= eip
->cryptoff
) &&
2146 (seg64
->fileoff
+seg64
->filesize
>=
2147 eip
->cryptoff
+eip
->cryptsize
)) {
2148 map_offset
= seg64
->vmaddr
+ eip
->cryptoff
- seg64
->fileoff
+ slide
;
2149 map_size
= eip
->cryptsize
;
2150 crypto_backing_offset
= macho_offset
+ eip
->cryptoff
;
2154 seg32
= (struct segment_command
*)lcp
;
2155 if ((seg32
->fileoff
<= eip
->cryptoff
) &&
2156 (seg32
->fileoff
+seg32
->filesize
>=
2157 eip
->cryptoff
+eip
->cryptsize
)) {
2158 map_offset
= seg32
->vmaddr
+ eip
->cryptoff
- seg32
->fileoff
+ slide
;
2159 map_size
= eip
->cryptsize
;
2160 crypto_backing_offset
= macho_offset
+ eip
->cryptoff
;
2166 /* if we get here, did not find anything */
2167 return LOAD_BADMACHO
;
2170 /* now remap using the decrypter */
2171 MACHO_PRINTF(("+++ set_code_unprotect: vm[0x%llx:0x%llx]\n",
2172 (uint64_t) map_offset
,
2173 (uint64_t) (map_offset
+map_size
)));
2174 kr
= vm_map_apple_protected(map
,
2176 map_offset
+map_size
,
2177 crypto_backing_offset
,
2180 printf("set_code_unprotect(): mapping failed with %x\n", kr
);
2181 return LOAD_PROTECT
;
2184 return LOAD_SUCCESS
;
2190 * This routine exists to support the load_dylinker().
2192 * This routine has its own, separate, understanding of the FAT file format,
2193 * which is terrifically unfortunate.
2200 struct mach_header
*mach_header
,
2203 struct macho_data
*data
,
2208 vfs_context_t ctx
= vfs_context_current();
2209 proc_t p
= vfs_context_proc(ctx
);
2210 kauth_cred_t kerncred
;
2211 struct nameidata
*ndp
= &data
->__nid
;
2213 struct fat_arch fat_arch
;
2216 union macho_vnode_header
*header
= &data
->__header
;
2217 off_t fsize
= (off_t
)0;
2220 * Capture the kernel credential for use in the actual read of the
2221 * file, since the user doing the execution may have execute rights
2222 * but not read rights, but to exec something, we have to either map
2223 * or read it into the new process address space, which requires
2224 * read rights. This is to deal with lack of common credential
2225 * serialization code which would treat NOCRED as "serialize 'root'".
2227 kerncred
= vfs_context_ucred(vfs_context_kernel());
2229 /* init the namei data to point the file user's program name */
2230 NDINIT(ndp
, LOOKUP
, OP_OPEN
, FOLLOW
| LOCKLEAF
, UIO_SYSSPACE
, CAST_USER_ADDR_T(path
), ctx
);
2232 if ((error
= namei(ndp
)) != 0) {
2233 if (error
== ENOENT
) {
2234 error
= LOAD_ENOENT
;
2236 error
= LOAD_FAILURE
;
2243 /* check for regular file */
2244 if (vp
->v_type
!= VREG
) {
2245 error
= LOAD_PROTECT
;
2250 if ((error
= vnode_size(vp
, &fsize
, ctx
)) != 0) {
2251 error
= LOAD_FAILURE
;
2255 /* Check mount point */
2256 if (vp
->v_mount
->mnt_flag
& MNT_NOEXEC
) {
2257 error
= LOAD_PROTECT
;
2262 if ((error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_EXECUTE
| KAUTH_VNODE_READ_DATA
, ctx
)) != 0) {
2263 error
= LOAD_PROTECT
;
2267 /* try to open it */
2268 if ((error
= VNOP_OPEN(vp
, FREAD
, ctx
)) != 0) {
2269 error
= LOAD_PROTECT
;
2273 if ((error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)header
, sizeof (*header
), 0,
2274 UIO_SYSSPACE
, IO_NODELOCKED
, kerncred
, &resid
, p
)) != 0) {
2275 error
= LOAD_IOERROR
;
2280 error
= LOAD_BADMACHO
;
2284 if (header
->mach_header
.magic
== MH_MAGIC
||
2285 header
->mach_header
.magic
== MH_MAGIC_64
) {
2287 } else if (OSSwapBigToHostInt32(header
->fat_header
.magic
) == FAT_MAGIC
) {
2290 error
= LOAD_BADMACHO
;
2296 error
= fatfile_validate_fatarches((vm_offset_t
)(&header
->fat_header
),
2298 if (error
!= LOAD_SUCCESS
) {
2302 /* Look up our architecture in the fat file. */
2303 error
= fatfile_getarch_with_bits(archbits
,
2304 (vm_offset_t
)(&header
->fat_header
), sizeof(*header
), &fat_arch
);
2305 if (error
!= LOAD_SUCCESS
)
2308 /* Read the Mach-O header out of it */
2309 error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)&header
->mach_header
,
2310 sizeof (header
->mach_header
), fat_arch
.offset
,
2311 UIO_SYSSPACE
, IO_NODELOCKED
, kerncred
, &resid
, p
);
2313 error
= LOAD_IOERROR
;
2318 error
= LOAD_BADMACHO
;
2322 /* Is this really a Mach-O? */
2323 if (header
->mach_header
.magic
!= MH_MAGIC
&&
2324 header
->mach_header
.magic
!= MH_MAGIC_64
) {
2325 error
= LOAD_BADMACHO
;
2329 *file_offset
= fat_arch
.offset
;
2330 *macho_size
= fat_arch
.size
;
2333 * Force get_macho_vnode() to fail if the architecture bits
2334 * do not match the expected architecture bits. This in
2335 * turn causes load_dylinker() to fail for the same reason,
2336 * so it ensures the dynamic linker and the binary are in
2337 * lock-step. This is potentially bad, if we ever add to
2338 * the CPU_ARCH_* bits any bits that are desirable but not
2339 * required, since the dynamic linker might work, but we will
2340 * refuse to load it because of this check.
2342 if ((cpu_type_t
)(header
->mach_header
.cputype
& CPU_ARCH_MASK
) != archbits
) {
2343 error
= LOAD_BADARCH
;
2348 *macho_size
= fsize
;
2351 *mach_header
= header
->mach_header
;
2354 ubc_setsize(vp
, fsize
);
2358 (void) VNOP_CLOSE(vp
, FREAD
, ctx
);