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 */
85 #include <os/overflow.h>
88 * XXX vm/pmap.h should not treat these prototypes as MACH_KERNEL_PRIVATE
89 * when KERNEL is defined.
91 extern pmap_t
pmap_create(ledger_t ledger
, vm_map_size_t size
,
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
,
127 * Prototypes of static functions.
134 struct mach_header
*header
,
140 load_result_t
*result
145 struct load_command
*lcp
,
153 load_result_t
*result
158 struct uuid_command
*uulp
,
160 load_result_t
*result
165 struct linkedit_data_command
*lcp
,
170 load_result_t
*result
);
172 #if CONFIG_CODE_DECRYPTION
175 struct encryption_info_command
*lcp
,
182 cpu_subtype_t cpusubtype
);
188 struct entry_point_command
*epc
,
191 load_result_t
*result
196 struct thread_command
*tcp
,
199 load_result_t
*result
215 mach_vm_offset_t
*user_stack
,
224 mach_vm_offset_t
*entry_point
229 struct dylinker_command
*lcp
,
235 load_result_t
*result
244 struct mach_header
*mach_header
,
247 struct macho_data
*macho_data
,
252 widen_segment_command(const struct segment_command
*scp32
,
253 struct segment_command_64
*scp
)
255 scp
->cmd
= scp32
->cmd
;
256 scp
->cmdsize
= scp32
->cmdsize
;
257 bcopy(scp32
->segname
, scp
->segname
, sizeof(scp
->segname
));
258 scp
->vmaddr
= scp32
->vmaddr
;
259 scp
->vmsize
= scp32
->vmsize
;
260 scp
->fileoff
= scp32
->fileoff
;
261 scp
->filesize
= scp32
->filesize
;
262 scp
->maxprot
= scp32
->maxprot
;
263 scp
->initprot
= scp32
->initprot
;
264 scp
->nsects
= scp32
->nsects
;
265 scp
->flags
= scp32
->flags
;
269 note_all_image_info_section(const struct segment_command_64
*scp
,
270 boolean_t is64
, size_t section_size
, const void *sections
,
271 int64_t slide
, load_result_t
*result
)
275 struct section_64 s64
;
279 if (strncmp(scp
->segname
, "__DATA", sizeof(scp
->segname
)) != 0)
281 for (i
= 0; i
< scp
->nsects
; ++i
) {
282 sectionp
= (const void *)
283 ((const char *)sections
+ section_size
* i
);
284 if (0 == strncmp(sectionp
->s64
.sectname
, "__all_image_info",
285 sizeof(sectionp
->s64
.sectname
))) {
286 result
->all_image_info_addr
=
287 is64
? sectionp
->s64
.addr
: sectionp
->s32
.addr
;
288 result
->all_image_info_addr
+= slide
;
289 result
->all_image_info_size
=
290 is64
? sectionp
->s64
.size
: sectionp
->s32
.size
;
299 struct image_params
*imgp
,
300 struct mach_header
*header
,
303 load_result_t
*result
306 struct vnode
*vp
= imgp
->ip_vp
;
307 off_t file_offset
= imgp
->ip_arch_offset
;
308 off_t macho_size
= imgp
->ip_arch_size
;
309 off_t file_size
= imgp
->ip_vattr
->va_data_size
;
310 vm_map_t new_map
= *mapp
;
311 pmap_t pmap
= 0; /* protected by create_map */
313 load_result_t myresult
;
315 boolean_t create_map
= FALSE
;
316 boolean_t enforce_hard_pagezero
= TRUE
;
317 int spawn
= (imgp
->ip_flags
& IMGPF_SPAWN
);
318 task_t task
= current_task();
319 proc_t p
= current_proc();
320 mach_vm_offset_t aslr_offset
= 0;
321 mach_vm_offset_t dyld_aslr_offset
= 0;
324 if (macho_size
> file_size
) {
325 return(LOAD_BADMACHO
);
328 if (new_map
== VM_MAP_NULL
) {
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.
344 if (imgp
->ip_new_thread
) {
345 ledger_task
= get_threadtask(imgp
->ip_new_thread
);
349 pmap
= pmap_create(get_task_ledger(ledger_task
),
351 ((imgp
->ip_flags
& IMGPF_IS_64BIT
) != 0));
352 pal_switch_pmap(thread
, pmap
, imgp
->ip_flags
& IMGPF_IS_64BIT
);
353 map
= vm_map_create(pmap
,
355 vm_compute_max_offset(((imgp
->ip_flags
& IMGPF_IS_64BIT
) == IMGPF_IS_64BIT
)),
360 #if (__ARM_ARCH_7K__ >= 2) && defined(PLATFORM_WatchOS)
361 /* enforce 16KB alignment for watch targets with new ABI */
362 vm_map_set_page_shift(map
, SIXTEENK_PAGE_SHIFT
);
363 #endif /* __arm64__ */
365 #ifndef CONFIG_ENFORCE_SIGNED_CODE
366 /* This turns off faulting for executable pages, which allows
367 * to circumvent Code Signing Enforcement. The per process
368 * flag (CS_ENFORCEMENT) is not set yet, but we can use the
371 if ( !cs_enforcement(NULL
) && (header
->flags
& MH_ALLOW_STACK_EXECUTION
) )
372 vm_map_disable_NX(map
);
375 /* Forcibly disallow execution from data pages on even if the arch
376 * normally permits it. */
377 if ((header
->flags
& MH_NO_HEAP_EXECUTION
) && !(imgp
->ip_flags
& IMGPF_ALLOW_DATA_EXEC
))
378 vm_map_disallow_data_exec(map
);
381 * Compute a random offset for ASLR, and an independent random offset for dyld.
383 if (!(imgp
->ip_flags
& IMGPF_DISABLE_ASLR
)) {
384 uint64_t max_slide_pages
;
386 max_slide_pages
= vm_map_get_max_aslr_slide_pages(map
);
388 aslr_offset
= random();
389 aslr_offset
%= max_slide_pages
;
390 aslr_offset
<<= vm_map_page_shift(map
);
392 dyld_aslr_offset
= random();
393 dyld_aslr_offset
%= max_slide_pages
;
394 dyld_aslr_offset
<<= vm_map_page_shift(map
);
400 *result
= load_result_null
;
402 lret
= parse_machfile(vp
, map
, thread
, header
, file_offset
, macho_size
,
403 0, (int64_t)aslr_offset
, (int64_t)dyld_aslr_offset
, result
);
405 if (lret
!= LOAD_SUCCESS
) {
407 vm_map_deallocate(map
); /* will lose pmap reference too */
414 * On x86, for compatibility, don't enforce the hard page-zero restriction for 32-bit binaries.
416 if ((imgp
->ip_flags
& IMGPF_IS_64BIT
) == 0) {
417 enforce_hard_pagezero
= FALSE
;
421 * Check to see if the page zero is enforced by the map->min_offset.
423 if (enforce_hard_pagezero
&&
424 (vm_map_has_hard_pagezero(map
, 0x1000) == FALSE
)) {
427 vm_map_deallocate(map
); /* will lose pmap reference too */
429 return (LOAD_BADMACHO
);
435 * If this is an exec, then we are going to destroy the old
436 * task, and it's correct to halt it; if it's spawn, the
437 * task is not yet running, and it makes no sense.
441 * Mark the task as halting and start the other
442 * threads towards terminating themselves. Then
443 * make sure any threads waiting for a process
444 * transition get informed that we are committed to
445 * this transition, and then finally complete the
446 * task halting (wait for threads and then cleanup
449 * NOTE: task_start_halt() makes sure that no new
450 * threads are created in the task during the transition.
451 * We need to mark the workqueue as exiting before we
452 * wait for threads to terminate (at the end of which
453 * we no longer have a prohibition on thread creation).
455 * Finally, clean up any lingering workqueue data structures
456 * that may have been left behind by the workqueue threads
457 * as they exited (and then clean up the work queue itself).
459 kret
= task_start_halt(task
);
460 if (kret
!= KERN_SUCCESS
) {
461 vm_map_deallocate(map
); /* will lose pmap reference too */
462 return (LOAD_FAILURE
);
464 proc_transcommit(p
, 0);
465 workqueue_mark_exiting(p
);
466 task_complete_halt(task
);
468 kqueue_dealloc(p
->p_wqkqueue
);
469 p
->p_wqkqueue
= NULL
;
473 return(LOAD_SUCCESS
);
476 int macho_printf
= 0;
477 #define MACHO_PRINTF(args) \
479 if (macho_printf) { \
485 * The file size of a mach-o file is limited to 32 bits; this is because
486 * this is the limit on the kalloc() of enough bytes for a mach_header and
487 * the contents of its sizeofcmds, which is currently constrained to 32
488 * bits in the file format itself. We read into the kernel buffer the
489 * commands section, and then parse it in order to parse the mach-o file
490 * format load_command segment(s). We are only interested in a subset of
491 * the total set of possible commands. If "map"==VM_MAP_NULL or
492 * "thread"==THREAD_NULL, do not make permament VM modifications,
493 * just preflight the parse.
501 struct mach_header
*header
,
506 int64_t dyld_aslr_offset
,
507 load_result_t
*result
511 struct load_command
*lcp
;
512 struct dylinker_command
*dlp
= 0;
513 integer_t dlarchbits
= 0;
515 load_return_t ret
= LOAD_SUCCESS
;
518 vm_size_t size
,kl_size
;
520 size_t oldoffset
; /* for overflow check */
522 proc_t p
= current_proc(); /* XXXX */
525 size_t mach_header_sz
= sizeof(struct mach_header
);
527 boolean_t got_code_signatures
= FALSE
;
530 if (header
->magic
== MH_MAGIC_64
||
531 header
->magic
== MH_CIGAM_64
) {
532 mach_header_sz
= sizeof(struct mach_header_64
);
536 * Break infinite recursion
539 return(LOAD_FAILURE
);
545 * Check to see if right machine type.
547 if (((cpu_type_t
)(header
->cputype
& ~CPU_ARCH_MASK
) != (cpu_type() & ~CPU_ARCH_MASK
)) ||
548 !grade_binary(header
->cputype
,
549 header
->cpusubtype
& ~CPU_SUBTYPE_MASK
))
550 return(LOAD_BADARCH
);
552 abi64
= ((header
->cputype
& CPU_ARCH_ABI64
) == CPU_ARCH_ABI64
);
554 switch (header
->filetype
) {
558 return (LOAD_FAILURE
);
564 return (LOAD_FAILURE
);
569 return (LOAD_FAILURE
);
573 * Get the pager for the file.
575 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
578 * Map portion that must be accessible directly into
581 if ((off_t
)(mach_header_sz
+ header
->sizeofcmds
) > macho_size
)
582 return(LOAD_BADMACHO
);
585 * Round size of Mach-O commands up to page boundry.
587 size
= round_page(mach_header_sz
+ header
->sizeofcmds
);
589 return(LOAD_BADMACHO
);
592 * Map the load commands into kernel memory.
596 kl_addr
= kalloc(size
);
597 addr
= (caddr_t
)kl_addr
;
599 return(LOAD_NOSPACE
);
601 error
= vn_rdwr(UIO_READ
, vp
, addr
, size
, file_offset
,
602 UIO_SYSSPACE
, 0, kauth_cred_get(), &resid
, p
);
605 kfree(kl_addr
, kl_size
);
606 return(LOAD_IOERROR
);
610 /* We must be able to read in as much as the mach_header indicated */
612 kfree(kl_addr
, kl_size
);
613 return(LOAD_BADMACHO
);
617 * For PIE and dyld, slide everything by the ASLR offset.
619 if ((header
->flags
& MH_PIE
) || (header
->filetype
== MH_DYLINKER
)) {
624 * Scan through the commands, processing each one as necessary.
625 * We parse in three passes through the headers:
626 * 0: determine if TEXT and DATA boundary can be page-aligned
627 * 1: thread state, uuid, code signature
629 * 3: dyld, encryption, check entry point
632 for (pass
= 0; pass
<= 3; pass
++) {
635 /* see if we need to adjust the slide to re-align... */
636 /* no re-alignment needed on X86_64 or ARM32 kernel */
638 } else if (pass
== 1) {
642 * Check that the entry point is contained in an executable segments
644 if ((pass
== 3) && (!result
->using_lcmain
&& result
->validentry
== 0)) {
645 thread_state_initialize(thread
);
651 * Loop through each of the load_commands indicated by the
652 * Mach-O header; if an absurd value is provided, we just
653 * run off the end of the reserved section by incrementing
654 * the offset too far, so we are implicitly fail-safe.
656 offset
= mach_header_sz
;
657 ncmds
= header
->ncmds
;
661 * Get a pointer to the command.
663 lcp
= (struct load_command
*)(addr
+ offset
);
665 offset
+= lcp
->cmdsize
;
668 * Perform prevalidation of the struct load_command
669 * before we attempt to use its contents. Invalid
670 * values are ones which result in an overflow, or
671 * which can not possibly be valid commands, or which
672 * straddle or exist past the reserved section at the
673 * start of the image.
675 if (oldoffset
> offset
||
676 lcp
->cmdsize
< sizeof(struct load_command
) ||
677 offset
> header
->sizeofcmds
+ mach_header_sz
) {
683 * Act on struct load_command's for which kernel
684 * intervention is required.
697 * Having an LC_SEGMENT command for the
698 * wrong ABI is invalid <rdar://problem/11021230>
704 ret
= load_segment(lcp
,
720 * Having an LC_SEGMENT_64 command for the
721 * wrong ABI is invalid <rdar://problem/11021230>
727 ret
= load_segment(lcp
,
740 ret
= load_unixthread(
741 (struct thread_command
*) lcp
,
752 (struct entry_point_command
*) lcp
,
757 case LC_LOAD_DYLINKER
:
760 if ((depth
== 1) && (dlp
== 0)) {
761 dlp
= (struct dylinker_command
*)lcp
;
762 dlarchbits
= (header
->cputype
& CPU_ARCH_MASK
);
768 if (pass
== 1 && depth
== 1) {
769 ret
= load_uuid((struct uuid_command
*) lcp
,
770 (char *)addr
+ mach_header_sz
+ header
->sizeofcmds
,
774 case LC_CODE_SIGNATURE
:
779 load signatures & store in uip
780 set VM object "signed_pages"
782 ret
= load_code_signature(
783 (struct linkedit_data_command
*) lcp
,
789 if (ret
!= LOAD_SUCCESS
) {
790 printf("proc %d: load code signature error %d "
792 p
->p_pid
, ret
, vp
->v_name
);
794 * Allow injections to be ignored on devices w/o enforcement enabled
796 if (!cs_enforcement(NULL
))
797 ret
= LOAD_SUCCESS
; /* ignore error */
800 got_code_signatures
= TRUE
;
803 if (got_code_signatures
) {
804 unsigned tainted
= CS_VALIDATE_TAINTED
;
805 boolean_t valid
= FALSE
;
806 struct cs_blob
*blobs
;
811 printf("validating initial pages of %s\n", vp
->v_name
);
812 blobs
= ubc_get_cs_blobs(vp
);
814 while (off
< size
&& ret
== LOAD_SUCCESS
) {
815 tainted
= CS_VALIDATE_TAINTED
;
817 valid
= cs_validate_page(blobs
,
822 if (!valid
|| (tainted
& CS_VALIDATE_TAINTED
)) {
824 printf("CODE SIGNING: %s[%d]: invalid initial page at offset %lld validated:%d tainted:%d csflags:0x%x\n",
825 vp
->v_name
, p
->p_pid
, (long long)(file_offset
+ off
), valid
, tainted
, result
->csflags
);
826 if (cs_enforcement(NULL
) ||
827 (result
->csflags
& (CS_HARD
|CS_KILL
|CS_ENFORCEMENT
))) {
830 result
->csflags
&= ~CS_VALID
;
837 #if CONFIG_CODE_DECRYPTION
838 case LC_ENCRYPTION_INFO
:
839 case LC_ENCRYPTION_INFO_64
:
842 ret
= set_code_unprotect(
843 (struct encryption_info_command
*) lcp
,
844 addr
, map
, slide
, vp
, file_offset
,
845 header
->cputype
, header
->cpusubtype
);
846 if (ret
!= LOAD_SUCCESS
) {
847 printf("proc %d: set_code_unprotect() error %d "
849 p
->p_pid
, ret
, vp
->v_name
);
851 * Don't let the app run if it's
852 * encrypted but we failed to set up the
853 * decrypter. If the keys are missing it will
854 * return LOAD_DECRYPTFAIL.
856 if (ret
== LOAD_DECRYPTFAIL
) {
857 /* failed to load due to missing FP keys */
859 p
->p_lflag
|= P_LTERM_DECRYPTFAIL
;
867 /* Other commands are ignored by the kernel */
871 if (ret
!= LOAD_SUCCESS
)
874 if (ret
!= LOAD_SUCCESS
)
878 if (ret
== LOAD_SUCCESS
) {
879 if (! got_code_signatures
) {
880 if (cs_enforcement(NULL
)) {
884 * No embedded signatures: look for detached by taskgated,
885 * this is only done on OSX, on embedded platforms we expect everything
886 * to be have embedded signatures.
888 struct cs_blob
*blob
;
890 blob
= ubc_cs_blob_get(vp
, -1, file_offset
);
892 unsigned int cs_flag_data
= blob
->csb_flags
;
893 if(0 != ubc_cs_generation_check(vp
)) {
894 if (0 != ubc_cs_blob_revalidate(vp
, blob
, 0)) {
895 /* clear out the flag data if revalidation fails */
897 result
->csflags
&= ~CS_VALID
;
900 /* get flags to be applied to the process */
901 result
->csflags
|= cs_flag_data
;
906 /* Make sure if we need dyld, we got it */
907 if (result
->needs_dynlinker
&& !dlp
) {
911 if ((ret
== LOAD_SUCCESS
) && (dlp
!= 0)) {
913 * load the dylinker, and slide it by the independent DYLD ASLR
914 * offset regardless of the PIE-ness of the main binary.
916 ret
= load_dylinker(dlp
, dlarchbits
, map
, thread
, depth
,
917 dyld_aslr_offset
, result
);
920 if((ret
== LOAD_SUCCESS
) && (depth
== 1)) {
921 if (result
->thread_count
== 0) {
928 kfree(kl_addr
, kl_size
);
933 #if CONFIG_CODE_DECRYPTION
935 #define APPLE_UNPROTECTED_HEADER_SIZE (3 * 4096)
938 unprotect_dsmos_segment(
944 vm_map_offset_t map_addr
,
945 vm_map_size_t map_size
)
950 * The first APPLE_UNPROTECTED_HEADER_SIZE bytes (from offset 0 of
951 * this part of a Universal binary) are not protected...
952 * The rest needs to be "transformed".
954 if (file_off
<= APPLE_UNPROTECTED_HEADER_SIZE
&&
955 file_off
+ file_size
<= APPLE_UNPROTECTED_HEADER_SIZE
) {
956 /* it's all unprotected, nothing to do... */
959 if (file_off
<= APPLE_UNPROTECTED_HEADER_SIZE
) {
961 * We start mapping in the unprotected area.
962 * Skip the unprotected part...
964 vm_map_offset_t delta
;
966 delta
= APPLE_UNPROTECTED_HEADER_SIZE
;
971 /* ... transform the rest of the mapping. */
972 struct pager_crypt_info crypt_info
;
973 crypt_info
.page_decrypt
= dsmos_page_transform
;
974 crypt_info
.crypt_ops
= NULL
;
975 crypt_info
.crypt_end
= NULL
;
976 #pragma unused(vp, macho_offset)
977 crypt_info
.crypt_ops
= (void *)0x2e69cf40;
978 vm_map_offset_t crypto_backing_offset
;
979 crypto_backing_offset
= -1; /* i.e. use map entry's offset */
980 #if DEVELOPMENT || DEBUG
983 printf("APPLE_PROTECT: %d[%s] map %p [0x%llx:0x%llx] %s(%s)\n",
984 p
->p_pid
, p
->p_comm
, map
,
985 (uint64_t) map_addr
, (uint64_t) (map_addr
+ map_size
),
986 __FUNCTION__
, vp
->v_name
);
987 #endif /* DEVELOPMENT || DEBUG */
989 /* The DSMOS pager can only be used by apple signed code */
990 struct cs_blob
* blob
= csvnode_get_blob(vp
, file_off
);
991 if( blob
== NULL
|| !blob
->csb_platform_binary
|| blob
->csb_platform_path
)
996 kr
= vm_map_apple_protected(map
,
999 crypto_backing_offset
,
1003 if (kr
!= KERN_SUCCESS
) {
1004 return LOAD_FAILURE
;
1006 return LOAD_SUCCESS
;
1008 #else /* CONFIG_CODE_DECRYPTION */
1009 static load_return_t
1010 unprotect_dsmos_segment(
1011 __unused
uint64_t file_off
,
1012 __unused
uint64_t file_size
,
1013 __unused
struct vnode
*vp
,
1014 __unused off_t macho_offset
,
1015 __unused vm_map_t map
,
1016 __unused vm_map_offset_t map_addr
,
1017 __unused vm_map_size_t map_size
)
1019 return LOAD_SUCCESS
;
1021 #endif /* CONFIG_CODE_DECRYPTION */
1026 * Maps a Mach-O segment, taking care of mis-alignment (wrt the system
1027 * page size) issues.
1029 * The mapping might result in 1, 2 or 3 map entries:
1030 * 1. for the first page, which could be overlap with the previous
1032 * 2. for the center (if applicable),
1033 * 3. for the last page, which could overlap with the next mapping.
1035 * For each of those map entries, we might have to interpose a
1036 * "fourk_pager" to deal with mis-alignment wrt the system page size,
1037 * either in the mapping address and/or size or the file offset and/or
1039 * The "fourk_pager" itself would be mapped with proper alignment
1040 * wrt the system page size and would then be populated with the
1041 * information about the intended mapping, with a "4KB" granularity.
1043 static kern_return_t
1046 vm_map_offset_t vm_start
,
1047 vm_map_offset_t vm_end
,
1048 memory_object_control_t control
,
1049 vm_map_offset_t file_start
,
1050 vm_map_offset_t file_end
,
1054 int extra_vm_flags
, cur_extra_vm_flags
;
1055 vm_map_offset_t cur_offset
, cur_start
, cur_end
;
1057 vm_map_offset_t effective_page_mask
;
1059 if (vm_end
< vm_start
||
1060 file_end
< file_start
) {
1061 return LOAD_BADMACHO
;
1063 if (vm_end
== vm_start
||
1064 file_end
== file_start
) {
1065 /* nothing to map... */
1066 return LOAD_SUCCESS
;
1069 effective_page_mask
= MAX(PAGE_MASK
, vm_map_page_mask(map
));
1072 if (vm_map_page_aligned(vm_start
, effective_page_mask
) &&
1073 vm_map_page_aligned(vm_end
, effective_page_mask
) &&
1074 vm_map_page_aligned(file_start
, effective_page_mask
) &&
1075 vm_map_page_aligned(file_end
, effective_page_mask
)) {
1076 /* all page-aligned and map-aligned: proceed */
1078 panic("map_segment: unexpected mis-alignment "
1079 "vm[0x%llx:0x%llx] file[0x%llx:0x%llx]\n",
1080 (uint64_t) vm_start
,
1082 (uint64_t) file_start
,
1083 (uint64_t) file_end
);
1087 cur_start
= vm_start
;
1089 if (cur_end
>= vm_start
+ (file_end
- file_start
)) {
1090 /* all mapped: done */
1093 if (vm_map_round_page(cur_end
, effective_page_mask
) >=
1094 vm_map_trunc_page(vm_start
+ (file_end
- file_start
),
1095 effective_page_mask
)) {
1098 cur_start
= cur_end
;
1099 if ((vm_start
& effective_page_mask
) !=
1100 (file_start
& effective_page_mask
)) {
1101 /* one 4K pager for the middle */
1102 cur_extra_vm_flags
= extra_vm_flags
;
1104 /* regular mapping for the middle */
1105 cur_extra_vm_flags
= 0;
1107 cur_end
= vm_map_trunc_page(vm_start
+ (file_end
-
1109 effective_page_mask
);
1110 if (control
!= MEMORY_OBJECT_CONTROL_NULL
) {
1111 ret
= vm_map_enter_mem_object_control(
1114 cur_end
- cur_start
,
1115 (mach_vm_offset_t
)0,
1116 VM_FLAGS_FIXED
| cur_extra_vm_flags
,
1118 file_start
+ cur_offset
,
1121 VM_INHERIT_DEFAULT
);
1123 ret
= vm_map_enter_mem_object(
1126 cur_end
- cur_start
,
1127 (mach_vm_offset_t
)0,
1128 VM_FLAGS_FIXED
| cur_extra_vm_flags
,
1133 VM_INHERIT_DEFAULT
);
1135 if (ret
!= KERN_SUCCESS
) {
1136 return (LOAD_NOSPACE
);
1138 cur_offset
+= cur_end
- cur_start
;
1140 if (cur_end
>= vm_start
+ (file_end
- file_start
)) {
1141 /* all mapped: done */
1144 cur_start
= cur_end
;
1146 assert(cur_end
>= vm_start
+ (file_end
- file_start
));
1147 return LOAD_SUCCESS
;
1153 struct load_command
*lcp
,
1161 load_result_t
*result
)
1163 struct segment_command_64 segment_command
, *scp
;
1165 vm_map_size_t delta_size
;
1168 size_t segment_command_size
, total_section_size
,
1169 single_section_size
;
1170 vm_map_offset_t file_offset
, file_size
;
1171 vm_map_offset_t vm_offset
, vm_size
;
1172 vm_map_offset_t vm_start
, vm_end
, vm_end_aligned
;
1173 vm_map_offset_t file_start
, file_end
;
1176 vm_map_size_t effective_page_size
;
1177 vm_map_offset_t effective_page_mask
;
1179 effective_page_size
= MAX(PAGE_SIZE
, vm_map_page_size(map
));
1180 effective_page_mask
= MAX(PAGE_MASK
, vm_map_page_mask(map
));
1183 if (LC_SEGMENT_64
== lcp
->cmd
) {
1184 segment_command_size
= sizeof(struct segment_command_64
);
1185 single_section_size
= sizeof(struct section_64
);
1187 segment_command_size
= sizeof(struct segment_command
);
1188 single_section_size
= sizeof(struct section
);
1190 if (lcp
->cmdsize
< segment_command_size
)
1191 return (LOAD_BADMACHO
);
1192 total_section_size
= lcp
->cmdsize
- segment_command_size
;
1194 if (LC_SEGMENT_64
== lcp
->cmd
) {
1195 scp
= (struct segment_command_64
*)lcp
;
1197 scp
= &segment_command
;
1198 widen_segment_command((struct segment_command
*)lcp
, scp
);
1202 MACHO_PRINTF(("+++ load_segment %s "
1203 "vm[0x%llx:0x%llx] file[0x%llx:0x%llx] "
1204 "prot %d/%d flags 0x%x\n",
1206 (uint64_t)(slide
+ scp
->vmaddr
),
1207 (uint64_t)(slide
+ scp
->vmaddr
+ scp
->vmsize
),
1208 pager_offset
+ scp
->fileoff
,
1209 pager_offset
+ scp
->fileoff
+ scp
->filesize
,
1216 * Make sure what we get from the file is really ours (as specified
1219 if (scp
->fileoff
+ scp
->filesize
< scp
->fileoff
||
1220 scp
->fileoff
+ scp
->filesize
> (uint64_t)macho_size
) {
1221 return (LOAD_BADMACHO
);
1224 * Ensure that the number of sections specified would fit
1225 * within the load command size.
1227 if (total_section_size
/ single_section_size
< scp
->nsects
) {
1228 return (LOAD_BADMACHO
);
1231 * Make sure the segment is page-aligned in the file.
1233 file_offset
= pager_offset
+ scp
->fileoff
; /* limited to 32 bits */
1234 file_size
= scp
->filesize
;
1235 if ((file_offset
& PAGE_MASK_64
) != 0 ||
1236 /* we can't mmap() it if it's not page-aligned in the file */
1237 (file_offset
& vm_map_page_mask(map
)) != 0) {
1239 * The 1st test would have failed if the system's page size
1240 * was what this process believe is the page size, so let's
1241 * fail here too for the sake of consistency.
1243 return (LOAD_BADMACHO
);
1247 * If we have a code signature attached for this slice
1248 * require that the segments are within the signed part
1251 if (result
->cs_end_offset
&&
1252 result
->cs_end_offset
< (off_t
)scp
->fileoff
&&
1253 result
->cs_end_offset
- scp
->fileoff
< scp
->filesize
)
1256 printf("section outside code signature\n");
1257 return LOAD_BADMACHO
;
1260 vm_offset
= scp
->vmaddr
+ slide
;
1261 vm_size
= scp
->vmsize
;
1264 return (LOAD_SUCCESS
);
1265 if (scp
->vmaddr
== 0 &&
1268 (scp
->initprot
& VM_PROT_ALL
) == VM_PROT_NONE
&&
1269 (scp
->maxprot
& VM_PROT_ALL
) == VM_PROT_NONE
) {
1271 * For PIE, extend page zero rather than moving it. Extending
1272 * page zero keeps early allocations from falling predictably
1273 * between the end of page zero and the beginning of the first
1277 * This is a "page zero" segment: it starts at address 0,
1278 * is not mapped from the binary file and is not accessible.
1279 * User-space should never be able to access that memory, so
1280 * make it completely off limits by raising the VM map's
1283 vm_end
= vm_offset
+ vm_size
;
1284 if (vm_end
< vm_offset
) {
1285 return (LOAD_BADMACHO
);
1288 MACHO_PRINTF(("++++++ load_segment: "
1289 "page_zero up to 0x%llx\n",
1290 (uint64_t) vm_end
));
1293 vm_end
= vm_map_round_page(vm_end
,
1295 vm_end_aligned
= vm_end
;
1297 ret
= vm_map_raise_min_offset(map
,
1300 if (ret
!= KERN_SUCCESS
) {
1301 return (LOAD_FAILURE
);
1303 return (LOAD_SUCCESS
);
1308 file_start
= vm_map_trunc_page(file_offset
,
1309 effective_page_mask
);
1310 file_end
= vm_map_round_page(file_offset
+ file_size
,
1311 effective_page_mask
);
1312 vm_start
= vm_map_trunc_page(vm_offset
,
1313 effective_page_mask
);
1314 vm_end
= vm_map_round_page(vm_offset
+ vm_size
,
1315 effective_page_mask
);
1318 if (vm_start
< result
->min_vm_addr
)
1319 result
->min_vm_addr
= vm_start
;
1320 if (vm_end
> result
->max_vm_addr
)
1321 result
->max_vm_addr
= vm_end
;
1323 if (map
== VM_MAP_NULL
)
1324 return (LOAD_SUCCESS
);
1327 initprot
= (scp
->initprot
) & VM_PROT_ALL
;
1328 maxprot
= (scp
->maxprot
) & VM_PROT_ALL
;
1330 * Map a copy of the file into the address space.
1333 MACHO_PRINTF(("++++++ load_segment: "
1334 "mapping at vm [0x%llx:0x%llx] of "
1335 "file [0x%llx:0x%llx]\n",
1336 (uint64_t) vm_start
,
1338 (uint64_t) file_start
,
1339 (uint64_t) file_end
));
1341 ret
= map_segment(map
,
1350 return LOAD_NOSPACE
;
1355 * If the file didn't end on a page boundary,
1356 * we need to zero the leftover.
1358 delta_size
= map_size
- scp
->filesize
;
1359 if (delta_size
> 0) {
1360 mach_vm_offset_t tmp
;
1362 ret
= mach_vm_allocate(kernel_map
, &tmp
, delta_size
, VM_FLAGS_ANYWHERE
| VM_MAKE_TAG(VM_KERN_MEMORY_BSD
));
1363 if (ret
!= KERN_SUCCESS
) {
1364 return(LOAD_RESOURCE
);
1367 if (copyout(tmp
, map_addr
+ scp
->filesize
,
1369 (void) mach_vm_deallocate(
1370 kernel_map
, tmp
, delta_size
);
1371 return (LOAD_FAILURE
);
1374 (void) mach_vm_deallocate(kernel_map
, tmp
, delta_size
);
1380 * If the virtual size of the segment is greater
1381 * than the size from the file, we need to allocate
1382 * zero fill memory for the rest.
1384 if ((vm_end
- vm_start
) > (file_end
- file_start
)) {
1385 delta_size
= (vm_end
- vm_start
) - (file_end
- file_start
);
1389 if (delta_size
> 0) {
1390 mach_vm_offset_t tmp
;
1392 tmp
= vm_start
+ (file_end
- file_start
);
1394 MACHO_PRINTF(("++++++ load_segment: "
1395 "delta mapping vm [0x%llx:0x%llx]\n",
1397 (uint64_t) (tmp
+ delta_size
)));
1399 kr
= map_segment(map
,
1402 MEMORY_OBJECT_CONTROL_NULL
,
1407 if (kr
!= KERN_SUCCESS
) {
1408 return(LOAD_NOSPACE
);
1412 if ( (scp
->fileoff
== 0) && (scp
->filesize
!= 0) )
1413 result
->mach_header
= vm_offset
;
1415 if (scp
->flags
& SG_PROTECTED_VERSION_1
) {
1416 ret
= unprotect_dsmos_segment(file_start
,
1417 file_end
- file_start
,
1423 if (ret
!= LOAD_SUCCESS
) {
1430 if (LOAD_SUCCESS
== ret
&&
1431 filetype
== MH_DYLINKER
&&
1432 result
->all_image_info_addr
== MACH_VM_MIN_ADDRESS
) {
1433 note_all_image_info_section(scp
,
1434 LC_SEGMENT_64
== lcp
->cmd
,
1435 single_section_size
,
1436 ((const char *)lcp
+
1437 segment_command_size
),
1442 if (result
->entry_point
!= MACH_VM_MIN_ADDRESS
) {
1443 if ((result
->entry_point
>= vm_offset
) && (result
->entry_point
< (vm_offset
+ vm_size
))) {
1444 if ((scp
->initprot
& (VM_PROT_READ
|VM_PROT_EXECUTE
)) == (VM_PROT_READ
|VM_PROT_EXECUTE
)) {
1445 result
->validentry
= 1;
1447 /* right range but wrong protections, unset if previously validated */
1448 result
->validentry
= 0;
1459 struct uuid_command
*uulp
,
1461 load_result_t
*result
1465 * We need to check the following for this command:
1466 * - The command size should be atleast the size of struct uuid_command
1467 * - The UUID part of the command should be completely within the mach-o header
1470 if ((uulp
->cmdsize
< sizeof(struct uuid_command
)) ||
1471 (((char *)uulp
+ sizeof(struct uuid_command
)) > command_end
)) {
1472 return (LOAD_BADMACHO
);
1475 memcpy(&result
->uuid
[0], &uulp
->uuid
[0], sizeof(result
->uuid
));
1476 return (LOAD_SUCCESS
);
1482 struct entry_point_command
*epc
,
1485 load_result_t
*result
1488 mach_vm_offset_t addr
;
1491 if (epc
->cmdsize
< sizeof(*epc
))
1492 return (LOAD_BADMACHO
);
1493 if (result
->thread_count
!= 0) {
1494 return (LOAD_FAILURE
);
1497 if (thread
== THREAD_NULL
)
1498 return (LOAD_SUCCESS
);
1500 /* LC_MAIN specifies stack size but not location */
1501 if (epc
->stacksize
) {
1502 result
->prog_stack_size
= 1;
1503 result
->user_stack_size
= epc
->stacksize
;
1505 result
->prog_stack_size
= 0;
1506 result
->user_stack_size
= MAXSSIZ
;
1508 result
->prog_allocated_stack
= 0;
1510 /* use default location for stack */
1511 ret
= thread_userstackdefault(thread
, &addr
);
1512 if (ret
!= KERN_SUCCESS
)
1513 return(LOAD_FAILURE
);
1515 /* The stack slides down from the default location */
1516 result
->user_stack
= addr
;
1517 result
->user_stack
-= slide
;
1519 if (result
->using_lcmain
|| result
->entry_point
!= MACH_VM_MIN_ADDRESS
) {
1520 /* Already processed LC_MAIN or LC_UNIXTHREAD */
1521 return (LOAD_FAILURE
);
1524 /* kernel does *not* use entryoff from LC_MAIN. Dyld uses it. */
1525 result
->needs_dynlinker
= TRUE
;
1526 result
->using_lcmain
= TRUE
;
1527 result
->unixproc
= TRUE
;
1528 result
->thread_count
++;
1530 return(LOAD_SUCCESS
);
1537 struct thread_command
*tcp
,
1540 load_result_t
*result
1545 mach_vm_offset_t addr
;
1547 if (tcp
->cmdsize
< sizeof(*tcp
))
1548 return (LOAD_BADMACHO
);
1549 if (result
->thread_count
!= 0) {
1550 return (LOAD_FAILURE
);
1553 if (thread
== THREAD_NULL
)
1554 return (LOAD_SUCCESS
);
1556 ret
= load_threadstack(thread
,
1557 (uint32_t *)(((vm_offset_t
)tcp
) +
1558 sizeof(struct thread_command
)),
1559 tcp
->cmdsize
- sizeof(struct thread_command
),
1562 if (ret
!= LOAD_SUCCESS
)
1565 /* LC_UNIXTHREAD optionally specifies stack size and location */
1568 result
->prog_stack_size
= 0; /* unknown */
1569 result
->prog_allocated_stack
= 1;
1571 result
->prog_allocated_stack
= 0;
1572 result
->prog_stack_size
= 0;
1573 result
->user_stack_size
= MAXSSIZ
;
1576 /* The stack slides down from the default location */
1577 result
->user_stack
= addr
;
1578 result
->user_stack
-= slide
;
1580 ret
= load_threadentry(thread
,
1581 (uint32_t *)(((vm_offset_t
)tcp
) +
1582 sizeof(struct thread_command
)),
1583 tcp
->cmdsize
- sizeof(struct thread_command
),
1585 if (ret
!= LOAD_SUCCESS
)
1588 if (result
->using_lcmain
|| result
->entry_point
!= MACH_VM_MIN_ADDRESS
) {
1589 /* Already processed LC_MAIN or LC_UNIXTHREAD */
1590 return (LOAD_FAILURE
);
1593 result
->entry_point
= addr
;
1594 result
->entry_point
+= slide
;
1596 ret
= load_threadstate(thread
,
1597 (uint32_t *)(((vm_offset_t
)tcp
) + sizeof(struct thread_command
)),
1598 tcp
->cmdsize
- sizeof(struct thread_command
),
1600 if (ret
!= LOAD_SUCCESS
)
1603 result
->unixproc
= TRUE
;
1604 result
->thread_count
++;
1606 return(LOAD_SUCCESS
);
1614 uint32_t total_size
,
1615 load_result_t
*result
1620 uint32_t thread_size
;
1621 uint32_t *local_ts
= NULL
;
1622 uint32_t local_ts_size
= 0;
1627 if (total_size
> 0) {
1628 local_ts_size
= total_size
;
1629 local_ts
= kalloc(local_ts_size
);
1630 if (local_ts
== NULL
) {
1631 return LOAD_FAILURE
;
1633 memcpy(local_ts
, ts
, local_ts_size
);
1638 * Validate the new thread state; iterate through the state flavors in
1640 * XXX: we should validate the machine state here, to avoid failing at
1641 * activation time where we can't bail out cleanly.
1643 while (total_size
> 0) {
1647 if (os_add_overflow(size
, UINT32_C(2), &thread_size
) ||
1648 os_mul_overflow(thread_size
, (uint32_t)sizeof(uint32_t), &thread_size
) ||
1649 os_sub_overflow(total_size
, thread_size
, &total_size
)) {
1650 ret
= LOAD_BADMACHO
;
1654 ts
+= size
; /* ts is a (uint32_t *) */
1657 result
->threadstate
= local_ts
;
1658 result
->threadstate_sz
= local_ts_size
;
1659 return LOAD_SUCCESS
;
1663 kfree(local_ts
, local_ts_size
);
1673 uint32_t total_size
,
1674 mach_vm_offset_t
*user_stack
,
1681 uint32_t stack_size
;
1683 while (total_size
> 0) {
1686 if (UINT32_MAX
-2 < size
||
1687 UINT32_MAX
/sizeof(uint32_t) < size
+2)
1688 return (LOAD_BADMACHO
);
1689 stack_size
= (size
+2)*sizeof(uint32_t);
1690 if (stack_size
> total_size
)
1691 return(LOAD_BADMACHO
);
1692 total_size
-= stack_size
;
1695 * Third argument is a kernel space pointer; it gets cast
1696 * to the appropriate type in thread_userstack() based on
1697 * the value of flavor.
1699 ret
= thread_userstack(thread
, flavor
, (thread_state_t
)ts
, size
, user_stack
, customstack
);
1700 if (ret
!= KERN_SUCCESS
) {
1701 return(LOAD_FAILURE
);
1703 ts
+= size
; /* ts is a (uint32_t *) */
1705 return(LOAD_SUCCESS
);
1713 uint32_t total_size
,
1714 mach_vm_offset_t
*entry_point
1720 uint32_t entry_size
;
1723 * Set the thread state.
1725 *entry_point
= MACH_VM_MIN_ADDRESS
;
1726 while (total_size
> 0) {
1729 if (UINT32_MAX
-2 < size
||
1730 UINT32_MAX
/sizeof(uint32_t) < size
+2)
1731 return (LOAD_BADMACHO
);
1732 entry_size
= (size
+2)*sizeof(uint32_t);
1733 if (entry_size
> total_size
)
1734 return(LOAD_BADMACHO
);
1735 total_size
-= entry_size
;
1737 * Third argument is a kernel space pointer; it gets cast
1738 * to the appropriate type in thread_entrypoint() based on
1739 * the value of flavor.
1741 ret
= thread_entrypoint(thread
, flavor
, (thread_state_t
)ts
, size
, entry_point
);
1742 if (ret
!= KERN_SUCCESS
) {
1743 return(LOAD_FAILURE
);
1745 ts
+= size
; /* ts is a (uint32_t *) */
1747 return(LOAD_SUCCESS
);
1751 struct nameidata __nid
;
1752 union macho_vnode_header
{
1753 struct mach_header mach_header
;
1754 struct fat_header fat_header
;
1759 #define DEFAULT_DYLD_PATH "/usr/lib/dyld"
1761 static load_return_t
1763 struct dylinker_command
*lcp
,
1769 load_result_t
*result
1774 struct vnode
*vp
= NULLVP
; /* set by get_macho_vnode() */
1775 struct mach_header
*header
;
1776 off_t file_offset
= 0; /* set by get_macho_vnode() */
1777 off_t macho_size
= 0; /* set by get_macho_vnode() */
1778 load_result_t
*myresult
;
1780 struct macho_data
*macho_data
;
1782 struct mach_header __header
;
1783 load_result_t __myresult
;
1784 struct macho_data __macho_data
;
1787 if (lcp
->cmdsize
< sizeof(*lcp
))
1788 return (LOAD_BADMACHO
);
1790 name
= (char *)lcp
+ lcp
->name
.offset
;
1792 * Check for a proper null terminated string.
1796 if (p
>= (char *)lcp
+ lcp
->cmdsize
)
1797 return(LOAD_BADMACHO
);
1800 #if !(DEVELOPMENT || DEBUG)
1801 if (0 != strcmp(name
, DEFAULT_DYLD_PATH
)) {
1802 return (LOAD_BADMACHO
);
1806 /* Allocate wad-of-data from heap to reduce excessively deep stacks */
1808 MALLOC(dyld_data
, void *, sizeof (*dyld_data
), M_TEMP
, M_WAITOK
);
1809 header
= &dyld_data
->__header
;
1810 myresult
= &dyld_data
->__myresult
;
1811 macho_data
= &dyld_data
->__macho_data
;
1813 ret
= get_macho_vnode(name
, archbits
, header
,
1814 &file_offset
, &macho_size
, macho_data
, &vp
);
1818 *myresult
= load_result_null
;
1821 * First try to map dyld in directly. This should work most of
1822 * the time since there shouldn't normally be something already
1823 * mapped to its address.
1826 ret
= parse_machfile(vp
, map
, thread
, header
, file_offset
,
1827 macho_size
, depth
, slide
, 0, myresult
);
1830 * If it turned out something was in the way, then we'll take
1831 * take this longer path to preflight dyld's vm ranges, then
1832 * map it at a free location in the address space.
1835 if (ret
== LOAD_NOSPACE
) {
1836 mach_vm_offset_t dyl_start
, map_addr
;
1837 mach_vm_size_t dyl_length
;
1838 int64_t slide_amount
;
1840 *myresult
= load_result_null
;
1843 * Preflight parsing the Mach-O file with a NULL
1844 * map, which will return the ranges needed for a
1845 * subsequent map attempt (with a slide) in "myresult"
1847 ret
= parse_machfile(vp
, VM_MAP_NULL
, THREAD_NULL
, header
,
1848 file_offset
, macho_size
, depth
,
1849 0 /* slide */, 0, myresult
);
1851 if (ret
!= LOAD_SUCCESS
) {
1855 dyl_start
= myresult
->min_vm_addr
;
1856 dyl_length
= myresult
->max_vm_addr
- myresult
->min_vm_addr
;
1858 dyl_length
+= slide
;
1860 /* To find an appropriate load address, do a quick allocation */
1861 map_addr
= dyl_start
;
1862 ret
= mach_vm_allocate(map
, &map_addr
, dyl_length
, VM_FLAGS_ANYWHERE
);
1863 if (ret
!= KERN_SUCCESS
) {
1868 ret
= mach_vm_deallocate(map
, map_addr
, dyl_length
);
1869 if (ret
!= KERN_SUCCESS
) {
1874 if (map_addr
< dyl_start
)
1875 slide_amount
= -(int64_t)(dyl_start
- map_addr
);
1877 slide_amount
= (int64_t)(map_addr
- dyl_start
);
1879 slide_amount
+= slide
;
1881 *myresult
= load_result_null
;
1883 ret
= parse_machfile(vp
, map
, thread
, header
,
1884 file_offset
, macho_size
, depth
,
1885 slide_amount
, 0, myresult
);
1892 if (ret
== LOAD_SUCCESS
) {
1893 if (result
->threadstate
) {
1894 /* don't use the app's threadstate if we have a dyld */
1895 kfree(result
->threadstate
, result
->threadstate_sz
);
1897 result
->threadstate
= myresult
->threadstate
;
1898 result
->threadstate_sz
= myresult
->threadstate_sz
;
1900 result
->dynlinker
= TRUE
;
1901 result
->entry_point
= myresult
->entry_point
;
1902 result
->validentry
= myresult
->validentry
;
1903 result
->all_image_info_addr
= myresult
->all_image_info_addr
;
1904 result
->all_image_info_size
= myresult
->all_image_info_size
;
1905 if (myresult
->platform_binary
) {
1906 result
->csflags
|= CS_DYLD_PLATFORM
;
1912 FREE(dyld_data
, M_TEMP
);
1917 static load_return_t
1918 load_code_signature(
1919 struct linkedit_data_command
*lcp
,
1924 load_result_t
*result
)
1930 struct cs_blob
*blob
;
1932 vm_size_t blob_size
;
1937 if (lcp
->cmdsize
!= sizeof (struct linkedit_data_command
) ||
1938 lcp
->dataoff
+ lcp
->datasize
> macho_size
) {
1939 ret
= LOAD_BADMACHO
;
1943 blob
= ubc_cs_blob_get(vp
, cputype
, macho_offset
);
1945 /* we already have a blob for this vnode and cputype */
1946 if (blob
->csb_cpu_type
== cputype
&&
1947 blob
->csb_base_offset
== macho_offset
&&
1948 blob
->csb_mem_size
== lcp
->datasize
) {
1949 /* it matches the blob we want here, lets verify the version */
1950 if(0 != ubc_cs_generation_check(vp
)) {
1951 if (0 != ubc_cs_blob_revalidate(vp
, blob
, 0)) {
1952 ret
= LOAD_FAILURE
; /* set error same as from ubc_cs_blob_add */
1958 /* the blob has changed for this vnode: fail ! */
1959 ret
= LOAD_BADMACHO
;
1964 blob_size
= lcp
->datasize
;
1965 kr
= ubc_cs_blob_allocate(&addr
, &blob_size
);
1966 if (kr
!= KERN_SUCCESS
) {
1972 error
= vn_rdwr(UIO_READ
,
1976 macho_offset
+ lcp
->dataoff
,
1982 if (error
|| resid
!= 0) {
1987 if (ubc_cs_blob_add(vp
,
1997 /* ubc_cs_blob_add() has consumed "addr" */
2001 #if CHECK_CS_VALIDATION_BITMAP
2002 ubc_cs_validation_bitmap_allocate( vp
);
2007 if (ret
== LOAD_SUCCESS
) {
2009 panic("sucess, but no blob!");
2011 result
->csflags
|= blob
->csb_flags
;
2012 result
->platform_binary
= blob
->csb_platform_binary
;
2013 result
->cs_end_offset
= blob
->csb_end_offset
;
2016 ubc_cs_blob_deallocate(addr
, blob_size
);
2024 #if CONFIG_CODE_DECRYPTION
2026 static load_return_t
2028 struct encryption_info_command
*eip
,
2035 cpu_subtype_t cpusubtype
)
2038 pager_crypt_info_t crypt_info
;
2039 const char * cryptname
= 0;
2043 struct segment_command_64
*seg64
;
2044 struct segment_command
*seg32
;
2045 vm_map_offset_t map_offset
, map_size
;
2046 vm_object_offset_t crypto_backing_offset
;
2049 if (eip
->cmdsize
< sizeof(*eip
)) return LOAD_BADMACHO
;
2051 switch(eip
->cryptid
) {
2053 /* not encrypted, just an empty load command */
2054 return LOAD_SUCCESS
;
2056 cryptname
="com.apple.unfree";
2059 /* some random cryptid that you could manually put into
2060 * your binary if you want NULL */
2061 cryptname
="com.apple.null";
2064 return LOAD_BADMACHO
;
2067 if (map
== VM_MAP_NULL
) return (LOAD_SUCCESS
);
2068 if (NULL
== text_crypter_create
) return LOAD_FAILURE
;
2070 MALLOC_ZONE(vpath
, char *, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
2071 if(vpath
== NULL
) return LOAD_FAILURE
;
2074 error
= vn_getpath(vp
, vpath
, &len
);
2076 FREE_ZONE(vpath
, MAXPATHLEN
, M_NAMEI
);
2077 return LOAD_FAILURE
;
2080 /* set up decrypter first */
2081 crypt_file_data_t crypt_data
= {
2084 .cpusubtype
= cpusubtype
};
2085 kr
=text_crypter_create(&crypt_info
, cryptname
, (void*)&crypt_data
);
2086 #if DEVELOPMENT || DEBUG
2089 printf("APPLE_PROTECT: %d[%s] map %p %s(%s) -> 0x%x\n",
2090 p
->p_pid
, p
->p_comm
, map
, __FUNCTION__
, vpath
, kr
);
2091 #endif /* DEVELOPMENT || DEBUG */
2092 FREE_ZONE(vpath
, MAXPATHLEN
, M_NAMEI
);
2095 printf("set_code_unprotect: unable to create decrypter %s, kr=%d\n",
2097 if (kr
== kIOReturnNotPrivileged
) {
2098 /* text encryption returned decryption failure */
2099 return(LOAD_DECRYPTFAIL
);
2101 return LOAD_RESOURCE
;
2104 /* this is terrible, but we have to rescan the load commands to find the
2105 * virtual address of this encrypted stuff. This code is gonna look like
2106 * the dyld source one day... */
2107 struct mach_header
*header
= (struct mach_header
*)addr
;
2108 size_t mach_header_sz
= sizeof(struct mach_header
);
2109 if (header
->magic
== MH_MAGIC_64
||
2110 header
->magic
== MH_CIGAM_64
) {
2111 mach_header_sz
= sizeof(struct mach_header_64
);
2113 offset
= mach_header_sz
;
2114 uint32_t ncmds
= header
->ncmds
;
2117 * Get a pointer to the command.
2119 struct load_command
*lcp
= (struct load_command
*)(addr
+ offset
);
2120 offset
+= lcp
->cmdsize
;
2124 seg64
= (struct segment_command_64
*)lcp
;
2125 if ((seg64
->fileoff
<= eip
->cryptoff
) &&
2126 (seg64
->fileoff
+seg64
->filesize
>=
2127 eip
->cryptoff
+eip
->cryptsize
)) {
2128 map_offset
= seg64
->vmaddr
+ eip
->cryptoff
- seg64
->fileoff
+ slide
;
2129 map_size
= eip
->cryptsize
;
2130 crypto_backing_offset
= macho_offset
+ eip
->cryptoff
;
2134 seg32
= (struct segment_command
*)lcp
;
2135 if ((seg32
->fileoff
<= eip
->cryptoff
) &&
2136 (seg32
->fileoff
+seg32
->filesize
>=
2137 eip
->cryptoff
+eip
->cryptsize
)) {
2138 map_offset
= seg32
->vmaddr
+ eip
->cryptoff
- seg32
->fileoff
+ slide
;
2139 map_size
= eip
->cryptsize
;
2140 crypto_backing_offset
= macho_offset
+ eip
->cryptoff
;
2146 /* if we get here, did not find anything */
2147 return LOAD_BADMACHO
;
2150 /* now remap using the decrypter */
2151 MACHO_PRINTF(("+++ set_code_unprotect: vm[0x%llx:0x%llx]\n",
2152 (uint64_t) map_offset
,
2153 (uint64_t) (map_offset
+map_size
)));
2154 kr
= vm_map_apple_protected(map
,
2156 map_offset
+map_size
,
2157 crypto_backing_offset
,
2160 printf("set_code_unprotect(): mapping failed with %x\n", kr
);
2161 return LOAD_PROTECT
;
2164 return LOAD_SUCCESS
;
2170 * This routine exists to support the load_dylinker().
2172 * This routine has its own, separate, understanding of the FAT file format,
2173 * which is terrifically unfortunate.
2180 struct mach_header
*mach_header
,
2183 struct macho_data
*data
,
2188 vfs_context_t ctx
= vfs_context_current();
2189 proc_t p
= vfs_context_proc(ctx
);
2190 kauth_cred_t kerncred
;
2191 struct nameidata
*ndp
= &data
->__nid
;
2193 struct fat_arch fat_arch
;
2196 union macho_vnode_header
*header
= &data
->__header
;
2197 off_t fsize
= (off_t
)0;
2200 * Capture the kernel credential for use in the actual read of the
2201 * file, since the user doing the execution may have execute rights
2202 * but not read rights, but to exec something, we have to either map
2203 * or read it into the new process address space, which requires
2204 * read rights. This is to deal with lack of common credential
2205 * serialization code which would treat NOCRED as "serialize 'root'".
2207 kerncred
= vfs_context_ucred(vfs_context_kernel());
2209 /* init the namei data to point the file user's program name */
2210 NDINIT(ndp
, LOOKUP
, OP_OPEN
, FOLLOW
| LOCKLEAF
, UIO_SYSSPACE
, CAST_USER_ADDR_T(path
), ctx
);
2212 if ((error
= namei(ndp
)) != 0) {
2213 if (error
== ENOENT
) {
2214 error
= LOAD_ENOENT
;
2216 error
= LOAD_FAILURE
;
2223 /* check for regular file */
2224 if (vp
->v_type
!= VREG
) {
2225 error
= LOAD_PROTECT
;
2230 if ((error
= vnode_size(vp
, &fsize
, ctx
)) != 0) {
2231 error
= LOAD_FAILURE
;
2235 /* Check mount point */
2236 if (vp
->v_mount
->mnt_flag
& MNT_NOEXEC
) {
2237 error
= LOAD_PROTECT
;
2242 if ((error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_EXECUTE
| KAUTH_VNODE_READ_DATA
, ctx
)) != 0) {
2243 error
= LOAD_PROTECT
;
2247 /* try to open it */
2248 if ((error
= VNOP_OPEN(vp
, FREAD
, ctx
)) != 0) {
2249 error
= LOAD_PROTECT
;
2253 if ((error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)header
, sizeof (*header
), 0,
2254 UIO_SYSSPACE
, IO_NODELOCKED
, kerncred
, &resid
, p
)) != 0) {
2255 error
= LOAD_IOERROR
;
2260 error
= LOAD_BADMACHO
;
2264 if (header
->mach_header
.magic
== MH_MAGIC
||
2265 header
->mach_header
.magic
== MH_MAGIC_64
) {
2267 } else if (OSSwapBigToHostInt32(header
->fat_header
.magic
) == FAT_MAGIC
) {
2270 error
= LOAD_BADMACHO
;
2276 error
= fatfile_validate_fatarches((vm_offset_t
)(&header
->fat_header
),
2278 if (error
!= LOAD_SUCCESS
) {
2282 /* Look up our architecture in the fat file. */
2283 error
= fatfile_getarch_with_bits(archbits
,
2284 (vm_offset_t
)(&header
->fat_header
), sizeof(*header
), &fat_arch
);
2285 if (error
!= LOAD_SUCCESS
)
2288 /* Read the Mach-O header out of it */
2289 error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)&header
->mach_header
,
2290 sizeof (header
->mach_header
), fat_arch
.offset
,
2291 UIO_SYSSPACE
, IO_NODELOCKED
, kerncred
, &resid
, p
);
2293 error
= LOAD_IOERROR
;
2298 error
= LOAD_BADMACHO
;
2302 /* Is this really a Mach-O? */
2303 if (header
->mach_header
.magic
!= MH_MAGIC
&&
2304 header
->mach_header
.magic
!= MH_MAGIC_64
) {
2305 error
= LOAD_BADMACHO
;
2309 *file_offset
= fat_arch
.offset
;
2310 *macho_size
= fat_arch
.size
;
2313 * Force get_macho_vnode() to fail if the architecture bits
2314 * do not match the expected architecture bits. This in
2315 * turn causes load_dylinker() to fail for the same reason,
2316 * so it ensures the dynamic linker and the binary are in
2317 * lock-step. This is potentially bad, if we ever add to
2318 * the CPU_ARCH_* bits any bits that are desirable but not
2319 * required, since the dynamic linker might work, but we will
2320 * refuse to load it because of this check.
2322 if ((cpu_type_t
)(header
->mach_header
.cputype
& CPU_ARCH_MASK
) != archbits
) {
2323 error
= LOAD_BADARCH
;
2328 *macho_size
= fsize
;
2331 *mach_header
= header
->mach_header
;
2334 ubc_setsize(vp
, fsize
);
2338 (void) VNOP_CLOSE(vp
, FREAD
, ctx
);