2 * Copyright (c) 2000-2010 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (C) 1988, 1989, NeXT, Inc.
31 * File: kern/mach_loader.c
32 * Author: Avadis Tevanian, Jr.
34 * Mach object file loader (kernel version, for now).
36 * 21-Jul-88 Avadis Tevanian, Jr. (avie) at NeXT
40 #include <sys/param.h>
41 #include <sys/vnode_internal.h>
43 #include <sys/namei.h>
44 #include <sys/proc_internal.h>
45 #include <sys/kauth.h>
47 #include <sys/malloc.h>
48 #include <sys/mount_internal.h>
49 #include <sys/fcntl.h>
50 #include <sys/ubc_internal.h>
51 #include <sys/imgact.h>
52 #include <sys/codesign.h>
53 #include <sys/proc_uuid_policy.h>
54 #include <sys/reason.h>
55 #include <sys/kdebug.h>
57 #include <mach/mach_types.h>
58 #include <mach/vm_map.h> /* vm_allocate() */
59 #include <mach/mach_vm.h> /* mach_vm_allocate() */
60 #include <mach/vm_statistics.h>
61 #include <mach/task.h>
62 #include <mach/thread_act.h>
64 #include <machine/vmparam.h>
65 #include <machine/exec.h>
66 #include <machine/pal_routines.h>
68 #include <kern/kern_types.h>
69 #include <kern/cpu_number.h>
70 #include <kern/mach_loader.h>
71 #include <kern/mach_fat.h>
72 #include <kern/kalloc.h>
73 #include <kern/task.h>
74 #include <kern/thread.h>
75 #include <kern/page_decrypt.h>
77 #include <mach-o/fat.h>
78 #include <mach-o/loader.h>
81 #include <vm/vm_map.h>
82 #include <vm/vm_kern.h>
83 #include <vm/vm_pager.h>
84 #include <vm/vnode_pager.h>
85 #include <vm/vm_protos.h>
86 #include <IOKit/IOReturn.h> /* for kIOReturnNotPrivileged */
88 #include <os/overflow.h>
91 * XXX vm/pmap.h should not treat these prototypes as MACH_KERNEL_PRIVATE
92 * when KERNEL is defined.
94 extern pmap_t
pmap_create(ledger_t ledger
, vm_map_size_t size
,
97 /* XXX should have prototypes in a shared header file */
98 extern int get_map_nentries(vm_map_t
);
100 extern kern_return_t
memory_object_signed(memory_object_control_t control
,
101 boolean_t is_signed
);
103 /* An empty load_result_t */
104 static const load_result_t load_result_null
= {
105 .mach_header
= MACH_VM_MIN_ADDRESS
,
106 .entry_point
= MACH_VM_MIN_ADDRESS
,
107 .user_stack
= MACH_VM_MIN_ADDRESS
,
108 .user_stack_size
= 0,
109 .user_stack_alloc
= MACH_VM_MIN_ADDRESS
,
110 .user_stack_alloc_size
= 0,
111 .all_image_info_addr
= MACH_VM_MIN_ADDRESS
,
112 .all_image_info_size
= 0,
116 .needs_dynlinker
= 0,
123 .min_vm_addr
= MACH_VM_MAX_ADDRESS
,
124 .max_vm_addr
= MACH_VM_MIN_ADDRESS
,
131 * Prototypes of static functions.
138 struct mach_header
*header
,
144 load_result_t
*result
,
145 load_result_t
*binresult
,
146 struct image_params
*imgp
151 struct load_command
*lcp
,
159 load_result_t
*result
164 struct uuid_command
*uulp
,
166 load_result_t
*result
171 struct linkedit_data_command
*lcp
,
176 load_result_t
*result
,
177 struct image_params
*imgp
);
179 #if CONFIG_CODE_DECRYPTION
182 struct encryption_info_command
*lcp
,
189 cpu_subtype_t cpusubtype
);
195 struct entry_point_command
*epc
,
198 load_result_t
*result
203 struct thread_command
*tcp
,
206 load_result_t
*result
222 mach_vm_offset_t
*user_stack
,
224 load_result_t
*result
232 mach_vm_offset_t
*entry_point
237 struct dylinker_command
*lcp
,
243 load_result_t
*result
,
244 struct image_params
*imgp
253 struct mach_header
*mach_header
,
256 struct macho_data
*macho_data
,
261 widen_segment_command(const struct segment_command
*scp32
,
262 struct segment_command_64
*scp
)
264 scp
->cmd
= scp32
->cmd
;
265 scp
->cmdsize
= scp32
->cmdsize
;
266 bcopy(scp32
->segname
, scp
->segname
, sizeof(scp
->segname
));
267 scp
->vmaddr
= scp32
->vmaddr
;
268 scp
->vmsize
= scp32
->vmsize
;
269 scp
->fileoff
= scp32
->fileoff
;
270 scp
->filesize
= scp32
->filesize
;
271 scp
->maxprot
= scp32
->maxprot
;
272 scp
->initprot
= scp32
->initprot
;
273 scp
->nsects
= scp32
->nsects
;
274 scp
->flags
= scp32
->flags
;
278 note_all_image_info_section(const struct segment_command_64
*scp
,
279 boolean_t is64
, size_t section_size
, const void *sections
,
280 int64_t slide
, load_result_t
*result
)
284 struct section_64 s64
;
288 if (strncmp(scp
->segname
, "__DATA", sizeof(scp
->segname
)) != 0)
290 for (i
= 0; i
< scp
->nsects
; ++i
) {
291 sectionp
= (const void *)
292 ((const char *)sections
+ section_size
* i
);
293 if (0 == strncmp(sectionp
->s64
.sectname
, "__all_image_info",
294 sizeof(sectionp
->s64
.sectname
))) {
295 result
->all_image_info_addr
=
296 is64
? sectionp
->s64
.addr
: sectionp
->s32
.addr
;
297 result
->all_image_info_addr
+= slide
;
298 result
->all_image_info_size
=
299 is64
? sectionp
->s64
.size
: sectionp
->s32
.size
;
308 struct image_params
*imgp
,
309 struct mach_header
*header
,
312 load_result_t
*result
315 struct vnode
*vp
= imgp
->ip_vp
;
316 off_t file_offset
= imgp
->ip_arch_offset
;
317 off_t macho_size
= imgp
->ip_arch_size
;
318 off_t file_size
= imgp
->ip_vattr
->va_data_size
;
319 vm_map_t new_map
= *mapp
;
320 pmap_t pmap
= 0; /* protected by create_map */
322 load_result_t myresult
;
324 boolean_t create_map
= FALSE
;
325 boolean_t enforce_hard_pagezero
= TRUE
;
326 int spawn
= (imgp
->ip_flags
& IMGPF_SPAWN
);
327 task_t task
= current_task();
328 proc_t p
= current_proc();
329 mach_vm_offset_t aslr_offset
= 0;
330 mach_vm_offset_t dyld_aslr_offset
= 0;
333 if (macho_size
> file_size
) {
334 return(LOAD_BADMACHO
);
337 if (new_map
== VM_MAP_NULL
) {
341 result
->is64bit
= ((imgp
->ip_flags
& IMGPF_IS_64BIT
) == IMGPF_IS_64BIT
);
344 * If we are spawning, we have created backing objects for the process
345 * already, which include non-lazily creating the task map. So we
346 * are going to switch out the task map with one appropriate for the
347 * bitness of the image being loaded.
355 if (imgp
->ip_new_thread
) {
356 ledger_task
= get_threadtask(imgp
->ip_new_thread
);
360 pmap
= pmap_create(get_task_ledger(ledger_task
),
363 map
= vm_map_create(pmap
,
365 vm_compute_max_offset(result
->is64bit
),
370 #if (__ARM_ARCH_7K__ >= 2) && defined(PLATFORM_WatchOS)
371 /* enforce 16KB alignment for watch targets with new ABI */
372 vm_map_set_page_shift(map
, SIXTEENK_PAGE_SHIFT
);
373 #endif /* __arm64__ */
375 #ifndef CONFIG_ENFORCE_SIGNED_CODE
376 /* This turns off faulting for executable pages, which allows
377 * to circumvent Code Signing Enforcement. The per process
378 * flag (CS_ENFORCEMENT) is not set yet, but we can use the
381 if ( !cs_enforcement(NULL
) && (header
->flags
& MH_ALLOW_STACK_EXECUTION
) )
382 vm_map_disable_NX(map
);
385 /* Forcibly disallow execution from data pages on even if the arch
386 * normally permits it. */
387 if ((header
->flags
& MH_NO_HEAP_EXECUTION
) && !(imgp
->ip_flags
& IMGPF_ALLOW_DATA_EXEC
))
388 vm_map_disallow_data_exec(map
);
391 * Compute a random offset for ASLR, and an independent random offset for dyld.
393 if (!(imgp
->ip_flags
& IMGPF_DISABLE_ASLR
)) {
394 uint64_t max_slide_pages
;
396 max_slide_pages
= vm_map_get_max_aslr_slide_pages(map
);
398 aslr_offset
= random();
399 aslr_offset
%= max_slide_pages
;
400 aslr_offset
<<= vm_map_page_shift(map
);
402 dyld_aslr_offset
= random();
403 dyld_aslr_offset
%= max_slide_pages
;
404 dyld_aslr_offset
<<= vm_map_page_shift(map
);
410 *result
= load_result_null
;
413 * re-set the bitness on the load result since we cleared the load result above.
415 result
->is64bit
= ((imgp
->ip_flags
& IMGPF_IS_64BIT
) == IMGPF_IS_64BIT
);
417 lret
= parse_machfile(vp
, map
, thread
, header
, file_offset
, macho_size
,
418 0, (int64_t)aslr_offset
, (int64_t)dyld_aslr_offset
, result
,
421 if (lret
!= LOAD_SUCCESS
) {
423 vm_map_deallocate(map
); /* will lose pmap reference too */
430 * On x86, for compatibility, don't enforce the hard page-zero restriction for 32-bit binaries.
432 if (!result
->is64bit
) {
433 enforce_hard_pagezero
= FALSE
;
437 * Check to see if the page zero is enforced by the map->min_offset.
439 if (enforce_hard_pagezero
&&
440 (vm_map_has_hard_pagezero(map
, 0x1000) == FALSE
)) {
443 vm_map_deallocate(map
); /* will lose pmap reference too */
445 return (LOAD_BADMACHO
);
449 vm_commit_pagezero_status(map
);
453 * If this is an exec, then we are going to destroy the old
454 * task, and it's correct to halt it; if it's spawn, the
455 * task is not yet running, and it makes no sense.
459 * Mark the task as halting and start the other
460 * threads towards terminating themselves. Then
461 * make sure any threads waiting for a process
462 * transition get informed that we are committed to
463 * this transition, and then finally complete the
464 * task halting (wait for threads and then cleanup
467 * NOTE: task_start_halt() makes sure that no new
468 * threads are created in the task during the transition.
469 * We need to mark the workqueue as exiting before we
470 * wait for threads to terminate (at the end of which
471 * we no longer have a prohibition on thread creation).
473 * Finally, clean up any lingering workqueue data structures
474 * that may have been left behind by the workqueue threads
475 * as they exited (and then clean up the work queue itself).
477 kret
= task_start_halt(task
);
478 if (kret
!= KERN_SUCCESS
) {
479 vm_map_deallocate(map
); /* will lose pmap reference too */
480 return (LOAD_FAILURE
);
482 proc_transcommit(p
, 0);
483 workqueue_mark_exiting(p
);
484 task_complete_halt(task
);
486 kqueue_dealloc(p
->p_wqkqueue
);
487 p
->p_wqkqueue
= NULL
;
491 return(LOAD_SUCCESS
);
494 int macho_printf
= 0;
495 #define MACHO_PRINTF(args) \
497 if (macho_printf) { \
503 * The file size of a mach-o file is limited to 32 bits; this is because
504 * this is the limit on the kalloc() of enough bytes for a mach_header and
505 * the contents of its sizeofcmds, which is currently constrained to 32
506 * bits in the file format itself. We read into the kernel buffer the
507 * commands section, and then parse it in order to parse the mach-o file
508 * format load_command segment(s). We are only interested in a subset of
509 * the total set of possible commands. If "map"==VM_MAP_NULL or
510 * "thread"==THREAD_NULL, do not make permament VM modifications,
511 * just preflight the parse.
519 struct mach_header
*header
,
524 int64_t dyld_aslr_offset
,
525 load_result_t
*result
,
526 load_result_t
*binresult
,
527 struct image_params
*imgp
531 struct load_command
*lcp
;
532 struct dylinker_command
*dlp
= 0;
533 integer_t dlarchbits
= 0;
535 load_return_t ret
= LOAD_SUCCESS
;
538 vm_size_t size
,kl_size
;
540 size_t oldoffset
; /* for overflow check */
542 proc_t p
= current_proc(); /* XXXX */
545 size_t mach_header_sz
= sizeof(struct mach_header
);
547 boolean_t got_code_signatures
= FALSE
;
548 boolean_t found_header_segment
= FALSE
;
549 boolean_t found_xhdr
= FALSE
;
551 boolean_t dyld_no_load_addr
= FALSE
;
552 boolean_t is_dyld
= FALSE
;
553 vm_map_offset_t effective_page_mask
= MAX(PAGE_MASK
, vm_map_page_mask(map
));
555 if (header
->magic
== MH_MAGIC_64
||
556 header
->magic
== MH_CIGAM_64
) {
557 mach_header_sz
= sizeof(struct mach_header_64
);
561 * Break infinite recursion
564 return(LOAD_FAILURE
);
570 * Check to see if right machine type.
572 if (((cpu_type_t
)(header
->cputype
& ~CPU_ARCH_MASK
) != (cpu_type() & ~CPU_ARCH_MASK
)) ||
573 !grade_binary(header
->cputype
,
574 header
->cpusubtype
& ~CPU_SUBTYPE_MASK
))
575 return(LOAD_BADARCH
);
577 abi64
= ((header
->cputype
& CPU_ARCH_ABI64
) == CPU_ARCH_ABI64
);
579 switch (header
->filetype
) {
583 return (LOAD_FAILURE
);
589 return (LOAD_FAILURE
);
595 return (LOAD_FAILURE
);
599 * Get the pager for the file.
601 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
604 * Map portion that must be accessible directly into
607 if ((off_t
)(mach_header_sz
+ header
->sizeofcmds
) > macho_size
)
608 return(LOAD_BADMACHO
);
611 * Round size of Mach-O commands up to page boundry.
613 size
= round_page(mach_header_sz
+ header
->sizeofcmds
);
615 return(LOAD_BADMACHO
);
618 * Map the load commands into kernel memory.
622 kl_addr
= kalloc(size
);
623 addr
= (caddr_t
)kl_addr
;
625 return(LOAD_NOSPACE
);
627 error
= vn_rdwr(UIO_READ
, vp
, addr
, size
, file_offset
,
628 UIO_SYSSPACE
, 0, kauth_cred_get(), &resid
, p
);
631 kfree(kl_addr
, kl_size
);
632 return(LOAD_IOERROR
);
636 /* We must be able to read in as much as the mach_header indicated */
638 kfree(kl_addr
, kl_size
);
639 return(LOAD_BADMACHO
);
643 * For PIE and dyld, slide everything by the ASLR offset.
645 if ((header
->flags
& MH_PIE
) || is_dyld
) {
650 * Scan through the commands, processing each one as necessary.
651 * We parse in three passes through the headers:
652 * 0: determine if TEXT and DATA boundary can be page-aligned
653 * 1: thread state, uuid, code signature
655 * 3: dyld, encryption, check entry point
658 boolean_t slide_realign
= FALSE
;
660 for (pass
= 0; pass
<= 3; pass
++) {
662 if (pass
== 0 && !slide_realign
&& !is_dyld
) {
663 /* if we dont need to realign the slide or determine dyld's load
664 * address, pass 0 can be skipped */
666 } else if (pass
== 1) {
668 if (dyld_no_load_addr
&& binresult
) {
670 * The dyld Mach-O does not specify a load address. Try to locate
671 * it right after the main binary. If binresult == NULL, load
672 * directly to the given slide.
674 slide
= vm_map_round_page(slide
+ binresult
->max_vm_addr
, effective_page_mask
);
679 * Check that the entry point is contained in an executable segments
681 if ((pass
== 3) && (!result
->using_lcmain
&& result
->validentry
== 0)) {
682 thread_state_initialize(thread
);
688 * Check that some segment maps the start of the mach-o file, which is
689 * needed by the dynamic loader to read the mach headers, etc.
691 if ((pass
== 3) && (found_header_segment
== FALSE
)) {
697 * Loop through each of the load_commands indicated by the
698 * Mach-O header; if an absurd value is provided, we just
699 * run off the end of the reserved section by incrementing
700 * the offset too far, so we are implicitly fail-safe.
702 offset
= mach_header_sz
;
703 ncmds
= header
->ncmds
;
707 * Get a pointer to the command.
709 lcp
= (struct load_command
*)(addr
+ offset
);
711 offset
+= lcp
->cmdsize
;
714 * Perform prevalidation of the struct load_command
715 * before we attempt to use its contents. Invalid
716 * values are ones which result in an overflow, or
717 * which can not possibly be valid commands, or which
718 * straddle or exist past the reserved section at the
719 * start of the image.
721 if (oldoffset
> offset
||
722 lcp
->cmdsize
< sizeof(struct load_command
) ||
723 offset
> header
->sizeofcmds
+ mach_header_sz
) {
729 * Act on struct load_command's for which kernel
730 * intervention is required.
734 struct segment_command
*scp
= (struct segment_command
*) lcp
;
737 if (is_dyld
&& scp
->vmaddr
== 0 && scp
->fileoff
== 0) {
738 dyld_no_load_addr
= TRUE
;
739 if (!slide_realign
) {
740 /* got what we need, bail early on pass 0 */
748 if (pass
== 1 && !strncmp(scp
->segname
, "__XHDR", sizeof(scp
->segname
))) {
757 * Having an LC_SEGMENT command for the
758 * wrong ABI is invalid <rdar://problem/11021230>
764 ret
= load_segment(lcp
,
774 if (ret
== LOAD_SUCCESS
&& scp
->fileoff
== 0 && scp
->filesize
> 0) {
775 /* Enforce a single segment mapping offset zero, with R+X
777 if (found_header_segment
||
778 ((scp
->initprot
& (VM_PROT_READ
|VM_PROT_EXECUTE
)) != (VM_PROT_READ
|VM_PROT_EXECUTE
))) {
782 found_header_segment
= TRUE
;
787 case LC_SEGMENT_64
: {
788 struct segment_command_64
*scp64
= (struct segment_command_64
*) lcp
;
791 if (is_dyld
&& scp64
->vmaddr
== 0 && scp64
->fileoff
== 0) {
792 dyld_no_load_addr
= TRUE
;
793 if (!slide_realign
) {
794 /* got what we need, bail early on pass 0 */
800 if (pass
== 1 && !strncmp(scp64
->segname
, "__XHDR", sizeof(scp64
->segname
))) {
809 * Having an LC_SEGMENT_64 command for the
810 * wrong ABI is invalid <rdar://problem/11021230>
816 ret
= load_segment(lcp
,
826 if (ret
== LOAD_SUCCESS
&& scp64
->fileoff
== 0 && scp64
->filesize
> 0) {
827 /* Enforce a single segment mapping offset zero, with R+X
829 if (found_header_segment
||
830 ((scp64
->initprot
& (VM_PROT_READ
|VM_PROT_EXECUTE
)) != (VM_PROT_READ
|VM_PROT_EXECUTE
))) {
834 found_header_segment
= TRUE
;
842 ret
= load_unixthread(
843 (struct thread_command
*) lcp
,
854 (struct entry_point_command
*) lcp
,
859 case LC_LOAD_DYLINKER
:
862 if ((depth
== 1) && (dlp
== 0)) {
863 dlp
= (struct dylinker_command
*)lcp
;
864 dlarchbits
= (header
->cputype
& CPU_ARCH_MASK
);
870 if (pass
== 1 && depth
== 1) {
871 ret
= load_uuid((struct uuid_command
*) lcp
,
872 (char *)addr
+ mach_header_sz
+ header
->sizeofcmds
,
876 case LC_CODE_SIGNATURE
:
881 load signatures & store in uip
882 set VM object "signed_pages"
884 ret
= load_code_signature(
885 (struct linkedit_data_command
*) lcp
,
892 if (ret
!= LOAD_SUCCESS
) {
893 printf("proc %d: load code signature error %d "
895 p
->p_pid
, ret
, vp
->v_name
);
897 * Allow injections to be ignored on devices w/o enforcement enabled
899 if (!cs_enforcement(NULL
))
900 ret
= LOAD_SUCCESS
; /* ignore error */
903 got_code_signatures
= TRUE
;
906 if (got_code_signatures
) {
907 unsigned tainted
= CS_VALIDATE_TAINTED
;
908 boolean_t valid
= FALSE
;
913 printf("validating initial pages of %s\n", vp
->v_name
);
915 while (off
< size
&& ret
== LOAD_SUCCESS
) {
916 tainted
= CS_VALIDATE_TAINTED
;
918 valid
= cs_validate_range(vp
,
924 if (!valid
|| (tainted
& CS_VALIDATE_TAINTED
)) {
926 printf("CODE SIGNING: %s[%d]: invalid initial page at offset %lld validated:%d tainted:%d csflags:0x%x\n",
927 vp
->v_name
, p
->p_pid
, (long long)(file_offset
+ off
), valid
, tainted
, result
->csflags
);
928 if (cs_enforcement(NULL
) ||
929 (result
->csflags
& (CS_HARD
|CS_KILL
|CS_ENFORCEMENT
))) {
932 result
->csflags
&= ~CS_VALID
;
939 #if CONFIG_CODE_DECRYPTION
940 case LC_ENCRYPTION_INFO
:
941 case LC_ENCRYPTION_INFO_64
:
944 ret
= set_code_unprotect(
945 (struct encryption_info_command
*) lcp
,
946 addr
, map
, slide
, vp
, file_offset
,
947 header
->cputype
, header
->cpusubtype
);
948 if (ret
!= LOAD_SUCCESS
) {
949 os_reason_t load_failure_reason
= OS_REASON_NULL
;
950 printf("proc %d: set_code_unprotect() error %d "
952 p
->p_pid
, ret
, vp
->v_name
);
954 * Don't let the app run if it's
955 * encrypted but we failed to set up the
956 * decrypter. If the keys are missing it will
957 * return LOAD_DECRYPTFAIL.
959 if (ret
== LOAD_DECRYPTFAIL
) {
960 /* failed to load due to missing FP keys */
962 p
->p_lflag
|= P_LTERM_DECRYPTFAIL
;
965 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXITREASON_CREATE
) | DBG_FUNC_NONE
,
966 p
->p_pid
, OS_REASON_EXEC
, EXEC_EXIT_REASON_FAIRPLAY_DECRYPT
, 0, 0);
967 load_failure_reason
= os_reason_create(OS_REASON_EXEC
, EXEC_EXIT_REASON_FAIRPLAY_DECRYPT
);
970 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXITREASON_CREATE
) | DBG_FUNC_NONE
,
971 p
->p_pid
, OS_REASON_EXEC
, EXEC_EXIT_REASON_DECRYPT
, 0, 0);
972 load_failure_reason
= os_reason_create(OS_REASON_EXEC
, EXEC_EXIT_REASON_DECRYPT
);
975 assert(load_failure_reason
!= OS_REASON_NULL
);
976 psignal_with_reason(p
, SIGKILL
, load_failure_reason
);
981 /* Other commands are ignored by the kernel */
985 if (ret
!= LOAD_SUCCESS
)
988 if (ret
!= LOAD_SUCCESS
)
992 if (ret
== LOAD_SUCCESS
) {
993 if (! got_code_signatures
) {
994 if (cs_enforcement(NULL
)) {
998 * No embedded signatures: look for detached by taskgated,
999 * this is only done on OSX, on embedded platforms we expect everything
1000 * to be have embedded signatures.
1002 struct cs_blob
*blob
;
1004 blob
= ubc_cs_blob_get(vp
, -1, file_offset
);
1006 unsigned int cs_flag_data
= blob
->csb_flags
;
1007 if(0 != ubc_cs_generation_check(vp
)) {
1008 if (0 != ubc_cs_blob_revalidate(vp
, blob
, imgp
, 0)) {
1009 /* clear out the flag data if revalidation fails */
1011 result
->csflags
&= ~CS_VALID
;
1014 /* get flags to be applied to the process */
1015 result
->csflags
|= cs_flag_data
;
1020 /* Make sure if we need dyld, we got it */
1021 if (result
->needs_dynlinker
&& !dlp
) {
1025 if ((ret
== LOAD_SUCCESS
) && (dlp
!= 0)) {
1027 * load the dylinker, and slide it by the independent DYLD ASLR
1028 * offset regardless of the PIE-ness of the main binary.
1030 ret
= load_dylinker(dlp
, dlarchbits
, map
, thread
, depth
,
1031 dyld_aslr_offset
, result
, imgp
);
1034 if ((ret
== LOAD_SUCCESS
) && (depth
== 1)) {
1035 if (result
->thread_count
== 0) {
1041 if (ret
== LOAD_BADMACHO
&& found_xhdr
) {
1042 ret
= LOAD_BADMACHO_UPX
;
1046 kfree(kl_addr
, kl_size
);
1052 #if CONFIG_CODE_DECRYPTION
1054 #define APPLE_UNPROTECTED_HEADER_SIZE (3 * 4096)
1056 static load_return_t
1057 unprotect_dsmos_segment(
1063 vm_map_offset_t map_addr
,
1064 vm_map_size_t map_size
)
1069 * The first APPLE_UNPROTECTED_HEADER_SIZE bytes (from offset 0 of
1070 * this part of a Universal binary) are not protected...
1071 * The rest needs to be "transformed".
1073 if (file_off
<= APPLE_UNPROTECTED_HEADER_SIZE
&&
1074 file_off
+ file_size
<= APPLE_UNPROTECTED_HEADER_SIZE
) {
1075 /* it's all unprotected, nothing to do... */
1078 if (file_off
<= APPLE_UNPROTECTED_HEADER_SIZE
) {
1080 * We start mapping in the unprotected area.
1081 * Skip the unprotected part...
1083 vm_map_offset_t delta
;
1085 delta
= APPLE_UNPROTECTED_HEADER_SIZE
;
1090 /* ... transform the rest of the mapping. */
1091 struct pager_crypt_info crypt_info
;
1092 crypt_info
.page_decrypt
= dsmos_page_transform
;
1093 crypt_info
.crypt_ops
= NULL
;
1094 crypt_info
.crypt_end
= NULL
;
1095 #pragma unused(vp, macho_offset)
1096 crypt_info
.crypt_ops
= (void *)0x2e69cf40;
1097 vm_map_offset_t crypto_backing_offset
;
1098 crypto_backing_offset
= -1; /* i.e. use map entry's offset */
1099 #if VM_MAP_DEBUG_APPLE_PROTECT
1100 if (vm_map_debug_apple_protect
) {
1103 printf("APPLE_PROTECT: %d[%s] map %p "
1104 "[0x%llx:0x%llx] %s(%s)\n",
1105 p
->p_pid
, p
->p_comm
, map
,
1106 (uint64_t) map_addr
,
1107 (uint64_t) (map_addr
+ map_size
),
1108 __FUNCTION__
, vp
->v_name
);
1110 #endif /* VM_MAP_DEBUG_APPLE_PROTECT */
1112 /* The DSMOS pager can only be used by apple signed code */
1113 struct cs_blob
* blob
= csvnode_get_blob(vp
, file_off
);
1114 if( blob
== NULL
|| !blob
->csb_platform_binary
|| blob
->csb_platform_path
)
1116 return LOAD_FAILURE
;
1119 kr
= vm_map_apple_protected(map
,
1121 map_addr
+ map_size
,
1122 crypto_backing_offset
,
1126 if (kr
!= KERN_SUCCESS
) {
1127 return LOAD_FAILURE
;
1129 return LOAD_SUCCESS
;
1131 #else /* CONFIG_CODE_DECRYPTION */
1132 static load_return_t
1133 unprotect_dsmos_segment(
1134 __unused
uint64_t file_off
,
1135 __unused
uint64_t file_size
,
1136 __unused
struct vnode
*vp
,
1137 __unused off_t macho_offset
,
1138 __unused vm_map_t map
,
1139 __unused vm_map_offset_t map_addr
,
1140 __unused vm_map_size_t map_size
)
1142 return LOAD_SUCCESS
;
1144 #endif /* CONFIG_CODE_DECRYPTION */
1149 * Maps a Mach-O segment, taking care of mis-alignment (wrt the system
1150 * page size) issues.
1152 * The mapping might result in 1, 2 or 3 map entries:
1153 * 1. for the first page, which could be overlap with the previous
1155 * 2. for the center (if applicable),
1156 * 3. for the last page, which could overlap with the next mapping.
1158 * For each of those map entries, we might have to interpose a
1159 * "fourk_pager" to deal with mis-alignment wrt the system page size,
1160 * either in the mapping address and/or size or the file offset and/or
1162 * The "fourk_pager" itself would be mapped with proper alignment
1163 * wrt the system page size and would then be populated with the
1164 * information about the intended mapping, with a "4KB" granularity.
1166 static kern_return_t
1169 vm_map_offset_t vm_start
,
1170 vm_map_offset_t vm_end
,
1171 memory_object_control_t control
,
1172 vm_map_offset_t file_start
,
1173 vm_map_offset_t file_end
,
1177 int extra_vm_flags
, cur_extra_vm_flags
;
1178 vm_map_offset_t cur_offset
, cur_start
, cur_end
;
1180 vm_map_offset_t effective_page_mask
;
1182 if (vm_end
< vm_start
||
1183 file_end
< file_start
) {
1184 return LOAD_BADMACHO
;
1186 if (vm_end
== vm_start
||
1187 file_end
== file_start
) {
1188 /* nothing to map... */
1189 return LOAD_SUCCESS
;
1192 effective_page_mask
= MAX(PAGE_MASK
, vm_map_page_mask(map
));
1195 if (vm_map_page_aligned(vm_start
, effective_page_mask
) &&
1196 vm_map_page_aligned(vm_end
, effective_page_mask
) &&
1197 vm_map_page_aligned(file_start
, effective_page_mask
) &&
1198 vm_map_page_aligned(file_end
, effective_page_mask
)) {
1199 /* all page-aligned and map-aligned: proceed */
1201 panic("map_segment: unexpected mis-alignment "
1202 "vm[0x%llx:0x%llx] file[0x%llx:0x%llx]\n",
1203 (uint64_t) vm_start
,
1205 (uint64_t) file_start
,
1206 (uint64_t) file_end
);
1210 cur_start
= vm_start
;
1212 if (cur_end
>= vm_start
+ (file_end
- file_start
)) {
1213 /* all mapped: done */
1216 if (vm_map_round_page(cur_end
, effective_page_mask
) >=
1217 vm_map_trunc_page(vm_start
+ (file_end
- file_start
),
1218 effective_page_mask
)) {
1221 cur_start
= cur_end
;
1222 if ((vm_start
& effective_page_mask
) !=
1223 (file_start
& effective_page_mask
)) {
1224 /* one 4K pager for the middle */
1225 cur_extra_vm_flags
= extra_vm_flags
;
1227 /* regular mapping for the middle */
1228 cur_extra_vm_flags
= 0;
1230 cur_end
= vm_map_trunc_page(vm_start
+ (file_end
-
1232 effective_page_mask
);
1233 if (control
!= MEMORY_OBJECT_CONTROL_NULL
) {
1234 ret
= vm_map_enter_mem_object_control(
1237 cur_end
- cur_start
,
1238 (mach_vm_offset_t
)0,
1239 VM_FLAGS_FIXED
| cur_extra_vm_flags
,
1241 file_start
+ cur_offset
,
1244 VM_INHERIT_DEFAULT
);
1246 ret
= vm_map_enter_mem_object(
1249 cur_end
- cur_start
,
1250 (mach_vm_offset_t
)0,
1251 VM_FLAGS_FIXED
| cur_extra_vm_flags
,
1256 VM_INHERIT_DEFAULT
);
1258 if (ret
!= KERN_SUCCESS
) {
1259 return (LOAD_NOSPACE
);
1261 cur_offset
+= cur_end
- cur_start
;
1263 if (cur_end
>= vm_start
+ (file_end
- file_start
)) {
1264 /* all mapped: done */
1267 cur_start
= cur_end
;
1269 assert(cur_end
>= vm_start
+ (file_end
- file_start
));
1270 return LOAD_SUCCESS
;
1276 struct load_command
*lcp
,
1284 load_result_t
*result
)
1286 struct segment_command_64 segment_command
, *scp
;
1288 vm_map_size_t delta_size
;
1291 size_t segment_command_size
, total_section_size
,
1292 single_section_size
;
1293 vm_map_offset_t file_offset
, file_size
;
1294 vm_map_offset_t vm_offset
, vm_size
;
1295 vm_map_offset_t vm_start
, vm_end
, vm_end_aligned
;
1296 vm_map_offset_t file_start
, file_end
;
1299 vm_map_size_t effective_page_size
;
1300 vm_map_offset_t effective_page_mask
;
1302 effective_page_size
= MAX(PAGE_SIZE
, vm_map_page_size(map
));
1303 effective_page_mask
= MAX(PAGE_MASK
, vm_map_page_mask(map
));
1306 if (LC_SEGMENT_64
== lcp
->cmd
) {
1307 segment_command_size
= sizeof(struct segment_command_64
);
1308 single_section_size
= sizeof(struct section_64
);
1310 segment_command_size
= sizeof(struct segment_command
);
1311 single_section_size
= sizeof(struct section
);
1313 if (lcp
->cmdsize
< segment_command_size
)
1314 return (LOAD_BADMACHO
);
1315 total_section_size
= lcp
->cmdsize
- segment_command_size
;
1317 if (LC_SEGMENT_64
== lcp
->cmd
) {
1318 scp
= (struct segment_command_64
*)lcp
;
1320 scp
= &segment_command
;
1321 widen_segment_command((struct segment_command
*)lcp
, scp
);
1325 MACHO_PRINTF(("+++ load_segment %s "
1326 "vm[0x%llx:0x%llx] file[0x%llx:0x%llx] "
1327 "prot %d/%d flags 0x%x\n",
1329 (uint64_t)(slide
+ scp
->vmaddr
),
1330 (uint64_t)(slide
+ scp
->vmaddr
+ scp
->vmsize
),
1331 pager_offset
+ scp
->fileoff
,
1332 pager_offset
+ scp
->fileoff
+ scp
->filesize
,
1339 * Make sure what we get from the file is really ours (as specified
1342 if (scp
->fileoff
+ scp
->filesize
< scp
->fileoff
||
1343 scp
->fileoff
+ scp
->filesize
> (uint64_t)macho_size
) {
1344 return (LOAD_BADMACHO
);
1347 * Ensure that the number of sections specified would fit
1348 * within the load command size.
1350 if (total_section_size
/ single_section_size
< scp
->nsects
) {
1351 return (LOAD_BADMACHO
);
1354 * Make sure the segment is page-aligned in the file.
1356 file_offset
= pager_offset
+ scp
->fileoff
; /* limited to 32 bits */
1357 file_size
= scp
->filesize
;
1358 if ((file_offset
& PAGE_MASK_64
) != 0 ||
1359 /* we can't mmap() it if it's not page-aligned in the file */
1360 (file_offset
& vm_map_page_mask(map
)) != 0) {
1362 * The 1st test would have failed if the system's page size
1363 * was what this process believe is the page size, so let's
1364 * fail here too for the sake of consistency.
1366 return (LOAD_BADMACHO
);
1370 * If we have a code signature attached for this slice
1371 * require that the segments are within the signed part
1374 if (result
->cs_end_offset
&&
1375 result
->cs_end_offset
< (off_t
)scp
->fileoff
&&
1376 result
->cs_end_offset
- scp
->fileoff
< scp
->filesize
)
1379 printf("section outside code signature\n");
1380 return LOAD_BADMACHO
;
1383 vm_offset
= scp
->vmaddr
+ slide
;
1384 vm_size
= scp
->vmsize
;
1387 return (LOAD_SUCCESS
);
1388 if (scp
->vmaddr
== 0 &&
1391 (scp
->initprot
& VM_PROT_ALL
) == VM_PROT_NONE
&&
1392 (scp
->maxprot
& VM_PROT_ALL
) == VM_PROT_NONE
) {
1394 * For PIE, extend page zero rather than moving it. Extending
1395 * page zero keeps early allocations from falling predictably
1396 * between the end of page zero and the beginning of the first
1400 * This is a "page zero" segment: it starts at address 0,
1401 * is not mapped from the binary file and is not accessible.
1402 * User-space should never be able to access that memory, so
1403 * make it completely off limits by raising the VM map's
1406 vm_end
= vm_offset
+ vm_size
;
1407 if (vm_end
< vm_offset
) {
1408 return (LOAD_BADMACHO
);
1411 MACHO_PRINTF(("++++++ load_segment: "
1412 "page_zero up to 0x%llx\n",
1413 (uint64_t) vm_end
));
1416 vm_end
= vm_map_round_page(vm_end
,
1418 vm_end_aligned
= vm_end
;
1420 ret
= vm_map_raise_min_offset(map
,
1423 if (ret
!= KERN_SUCCESS
) {
1424 return (LOAD_FAILURE
);
1426 return (LOAD_SUCCESS
);
1431 file_start
= vm_map_trunc_page(file_offset
,
1432 effective_page_mask
);
1433 file_end
= vm_map_round_page(file_offset
+ file_size
,
1434 effective_page_mask
);
1435 vm_start
= vm_map_trunc_page(vm_offset
,
1436 effective_page_mask
);
1437 vm_end
= vm_map_round_page(vm_offset
+ vm_size
,
1438 effective_page_mask
);
1441 if (vm_start
< result
->min_vm_addr
)
1442 result
->min_vm_addr
= vm_start
;
1443 if (vm_end
> result
->max_vm_addr
)
1444 result
->max_vm_addr
= vm_end
;
1446 if (map
== VM_MAP_NULL
)
1447 return (LOAD_SUCCESS
);
1450 initprot
= (scp
->initprot
) & VM_PROT_ALL
;
1451 maxprot
= (scp
->maxprot
) & VM_PROT_ALL
;
1453 * Map a copy of the file into the address space.
1456 MACHO_PRINTF(("++++++ load_segment: "
1457 "mapping at vm [0x%llx:0x%llx] of "
1458 "file [0x%llx:0x%llx]\n",
1459 (uint64_t) vm_start
,
1461 (uint64_t) file_start
,
1462 (uint64_t) file_end
));
1464 ret
= map_segment(map
,
1473 return LOAD_NOSPACE
;
1478 * If the file didn't end on a page boundary,
1479 * we need to zero the leftover.
1481 delta_size
= map_size
- scp
->filesize
;
1482 if (delta_size
> 0) {
1483 mach_vm_offset_t tmp
;
1485 ret
= mach_vm_allocate(kernel_map
, &tmp
, delta_size
, VM_FLAGS_ANYWHERE
| VM_MAKE_TAG(VM_KERN_MEMORY_BSD
));
1486 if (ret
!= KERN_SUCCESS
) {
1487 return(LOAD_RESOURCE
);
1490 if (copyout(tmp
, map_addr
+ scp
->filesize
,
1492 (void) mach_vm_deallocate(
1493 kernel_map
, tmp
, delta_size
);
1494 return (LOAD_FAILURE
);
1497 (void) mach_vm_deallocate(kernel_map
, tmp
, delta_size
);
1503 * If the virtual size of the segment is greater
1504 * than the size from the file, we need to allocate
1505 * zero fill memory for the rest.
1507 if ((vm_end
- vm_start
) > (file_end
- file_start
)) {
1508 delta_size
= (vm_end
- vm_start
) - (file_end
- file_start
);
1512 if (delta_size
> 0) {
1513 mach_vm_offset_t tmp
;
1515 tmp
= vm_start
+ (file_end
- file_start
);
1517 MACHO_PRINTF(("++++++ load_segment: "
1518 "delta mapping vm [0x%llx:0x%llx]\n",
1520 (uint64_t) (tmp
+ delta_size
)));
1522 kr
= map_segment(map
,
1525 MEMORY_OBJECT_CONTROL_NULL
,
1530 if (kr
!= KERN_SUCCESS
) {
1531 return(LOAD_NOSPACE
);
1535 if ( (scp
->fileoff
== 0) && (scp
->filesize
!= 0) )
1536 result
->mach_header
= vm_offset
;
1538 if (scp
->flags
& SG_PROTECTED_VERSION_1
) {
1539 ret
= unprotect_dsmos_segment(file_start
,
1540 file_end
- file_start
,
1546 if (ret
!= LOAD_SUCCESS
) {
1553 if (LOAD_SUCCESS
== ret
&&
1554 filetype
== MH_DYLINKER
&&
1555 result
->all_image_info_addr
== MACH_VM_MIN_ADDRESS
) {
1556 note_all_image_info_section(scp
,
1557 LC_SEGMENT_64
== lcp
->cmd
,
1558 single_section_size
,
1559 ((const char *)lcp
+
1560 segment_command_size
),
1565 if (result
->entry_point
!= MACH_VM_MIN_ADDRESS
) {
1566 if ((result
->entry_point
>= vm_offset
) && (result
->entry_point
< (vm_offset
+ vm_size
))) {
1567 if ((scp
->initprot
& (VM_PROT_READ
|VM_PROT_EXECUTE
)) == (VM_PROT_READ
|VM_PROT_EXECUTE
)) {
1568 result
->validentry
= 1;
1570 /* right range but wrong protections, unset if previously validated */
1571 result
->validentry
= 0;
1582 struct uuid_command
*uulp
,
1584 load_result_t
*result
1588 * We need to check the following for this command:
1589 * - The command size should be atleast the size of struct uuid_command
1590 * - The UUID part of the command should be completely within the mach-o header
1593 if ((uulp
->cmdsize
< sizeof(struct uuid_command
)) ||
1594 (((char *)uulp
+ sizeof(struct uuid_command
)) > command_end
)) {
1595 return (LOAD_BADMACHO
);
1598 memcpy(&result
->uuid
[0], &uulp
->uuid
[0], sizeof(result
->uuid
));
1599 return (LOAD_SUCCESS
);
1605 struct entry_point_command
*epc
,
1608 load_result_t
*result
1611 mach_vm_offset_t addr
;
1614 if (epc
->cmdsize
< sizeof(*epc
))
1615 return (LOAD_BADMACHO
);
1616 if (result
->thread_count
!= 0) {
1617 return (LOAD_FAILURE
);
1620 if (thread
== THREAD_NULL
)
1621 return (LOAD_SUCCESS
);
1624 * LC_MAIN specifies stack size but not location.
1625 * Add guard page to allocation size (MAXSSIZ includes guard page).
1627 if (epc
->stacksize
) {
1628 if (os_add_overflow(epc
->stacksize
, 4*PAGE_SIZE
, &result
->user_stack_size
)) {
1630 * We are going to immediately throw away this result, but we want
1631 * to make sure we aren't loading a dangerously close to
1632 * overflowing value, since this will have a guard page added to it
1633 * and be rounded to page boundaries
1635 return LOAD_BADMACHO
;
1637 result
->user_stack_size
= epc
->stacksize
;
1638 if (os_add_overflow(epc
->stacksize
, PAGE_SIZE
, &result
->user_stack_alloc_size
)) {
1639 return LOAD_BADMACHO
;
1642 result
->user_stack_alloc_size
= MAXSSIZ
;
1645 /* use default location for stack */
1646 ret
= thread_userstackdefault(&addr
, result
->is64bit
);
1647 if (ret
!= KERN_SUCCESS
)
1648 return(LOAD_FAILURE
);
1650 /* The stack slides down from the default location */
1651 result
->user_stack
= addr
;
1652 result
->user_stack
-= slide
;
1654 if (result
->using_lcmain
|| result
->entry_point
!= MACH_VM_MIN_ADDRESS
) {
1655 /* Already processed LC_MAIN or LC_UNIXTHREAD */
1656 return (LOAD_FAILURE
);
1659 /* kernel does *not* use entryoff from LC_MAIN. Dyld uses it. */
1660 result
->needs_dynlinker
= TRUE
;
1661 result
->using_lcmain
= TRUE
;
1663 ret
= thread_state_initialize( thread
);
1664 if (ret
!= KERN_SUCCESS
) {
1665 return(LOAD_FAILURE
);
1668 result
->unixproc
= TRUE
;
1669 result
->thread_count
++;
1671 return(LOAD_SUCCESS
);
1678 struct thread_command
*tcp
,
1681 load_result_t
*result
1686 mach_vm_offset_t addr
;
1688 if (tcp
->cmdsize
< sizeof(*tcp
))
1689 return (LOAD_BADMACHO
);
1690 if (result
->thread_count
!= 0) {
1691 return (LOAD_FAILURE
);
1694 if (thread
== THREAD_NULL
)
1695 return (LOAD_SUCCESS
);
1697 ret
= load_threadstack(thread
,
1698 (uint32_t *)(((vm_offset_t
)tcp
) +
1699 sizeof(struct thread_command
)),
1700 tcp
->cmdsize
- sizeof(struct thread_command
),
1701 &addr
, &customstack
, result
);
1702 if (ret
!= LOAD_SUCCESS
)
1705 /* LC_UNIXTHREAD optionally specifies stack size and location */
1708 result
->user_stack_alloc_size
= MAXSSIZ
;
1711 /* The stack slides down from the default location */
1712 result
->user_stack
= addr
;
1713 result
->user_stack
-= slide
;
1715 ret
= load_threadentry(thread
,
1716 (uint32_t *)(((vm_offset_t
)tcp
) +
1717 sizeof(struct thread_command
)),
1718 tcp
->cmdsize
- sizeof(struct thread_command
),
1720 if (ret
!= LOAD_SUCCESS
)
1723 if (result
->using_lcmain
|| result
->entry_point
!= MACH_VM_MIN_ADDRESS
) {
1724 /* Already processed LC_MAIN or LC_UNIXTHREAD */
1725 return (LOAD_FAILURE
);
1728 result
->entry_point
= addr
;
1729 result
->entry_point
+= slide
;
1731 ret
= load_threadstate(thread
,
1732 (uint32_t *)(((vm_offset_t
)tcp
) + sizeof(struct thread_command
)),
1733 tcp
->cmdsize
- sizeof(struct thread_command
),
1735 if (ret
!= LOAD_SUCCESS
)
1738 result
->unixproc
= TRUE
;
1739 result
->thread_count
++;
1741 return(LOAD_SUCCESS
);
1749 uint32_t total_size
,
1750 load_result_t
*result
1755 uint32_t thread_size
;
1756 uint32_t *local_ts
= NULL
;
1757 uint32_t local_ts_size
= 0;
1762 if (total_size
> 0) {
1763 local_ts_size
= total_size
;
1764 local_ts
= kalloc(local_ts_size
);
1765 if (local_ts
== NULL
) {
1766 return LOAD_FAILURE
;
1768 memcpy(local_ts
, ts
, local_ts_size
);
1773 * Validate the new thread state; iterate through the state flavors in
1775 * XXX: we should validate the machine state here, to avoid failing at
1776 * activation time where we can't bail out cleanly.
1778 while (total_size
> 0) {
1782 if (os_add_and_mul_overflow(size
, 2, sizeof(uint32_t), &thread_size
) ||
1783 os_sub_overflow(total_size
, thread_size
, &total_size
)) {
1784 ret
= LOAD_BADMACHO
;
1788 ts
+= size
; /* ts is a (uint32_t *) */
1791 result
->threadstate
= local_ts
;
1792 result
->threadstate_sz
= local_ts_size
;
1793 return LOAD_SUCCESS
;
1797 kfree(local_ts
, local_ts_size
);
1807 uint32_t total_size
,
1808 mach_vm_offset_t
*user_stack
,
1810 load_result_t
*result
1816 uint32_t stack_size
;
1818 while (total_size
> 0) {
1821 if (UINT32_MAX
-2 < size
||
1822 UINT32_MAX
/sizeof(uint32_t) < size
+2)
1823 return (LOAD_BADMACHO
);
1824 stack_size
= (size
+2)*sizeof(uint32_t);
1825 if (stack_size
> total_size
)
1826 return(LOAD_BADMACHO
);
1827 total_size
-= stack_size
;
1830 * Third argument is a kernel space pointer; it gets cast
1831 * to the appropriate type in thread_userstack() based on
1832 * the value of flavor.
1834 ret
= thread_userstack(thread
, flavor
, (thread_state_t
)ts
, size
, user_stack
, customstack
, result
->is64bit
);
1835 if (ret
!= KERN_SUCCESS
) {
1836 return(LOAD_FAILURE
);
1838 ts
+= size
; /* ts is a (uint32_t *) */
1840 return(LOAD_SUCCESS
);
1848 uint32_t total_size
,
1849 mach_vm_offset_t
*entry_point
1855 uint32_t entry_size
;
1858 * Set the thread state.
1860 *entry_point
= MACH_VM_MIN_ADDRESS
;
1861 while (total_size
> 0) {
1864 if (UINT32_MAX
-2 < size
||
1865 UINT32_MAX
/sizeof(uint32_t) < size
+2)
1866 return (LOAD_BADMACHO
);
1867 entry_size
= (size
+2)*sizeof(uint32_t);
1868 if (entry_size
> total_size
)
1869 return(LOAD_BADMACHO
);
1870 total_size
-= entry_size
;
1872 * Third argument is a kernel space pointer; it gets cast
1873 * to the appropriate type in thread_entrypoint() based on
1874 * the value of flavor.
1876 ret
= thread_entrypoint(thread
, flavor
, (thread_state_t
)ts
, size
, entry_point
);
1877 if (ret
!= KERN_SUCCESS
) {
1878 return(LOAD_FAILURE
);
1880 ts
+= size
; /* ts is a (uint32_t *) */
1882 return(LOAD_SUCCESS
);
1886 struct nameidata __nid
;
1887 union macho_vnode_header
{
1888 struct mach_header mach_header
;
1889 struct fat_header fat_header
;
1894 #define DEFAULT_DYLD_PATH "/usr/lib/dyld"
1896 #if (DEVELOPMENT || DEBUG)
1897 extern char dyld_alt_path
[];
1898 extern int use_alt_dyld
;
1901 static load_return_t
1903 struct dylinker_command
*lcp
,
1909 load_result_t
*result
,
1910 struct image_params
*imgp
1915 struct vnode
*vp
= NULLVP
; /* set by get_macho_vnode() */
1916 struct mach_header
*header
;
1917 off_t file_offset
= 0; /* set by get_macho_vnode() */
1918 off_t macho_size
= 0; /* set by get_macho_vnode() */
1919 load_result_t
*myresult
;
1921 struct macho_data
*macho_data
;
1923 struct mach_header __header
;
1924 load_result_t __myresult
;
1925 struct macho_data __macho_data
;
1928 if (lcp
->cmdsize
< sizeof(*lcp
))
1929 return (LOAD_BADMACHO
);
1931 name
= (char *)lcp
+ lcp
->name
.offset
;
1934 * Check for a proper null terminated string.
1938 if (p
>= (char *)lcp
+ lcp
->cmdsize
)
1939 return(LOAD_BADMACHO
);
1942 #if (DEVELOPMENT || DEBUG)
1946 * If an alternate dyld has been specified via boot args, check
1947 * to see if PROC_UUID_ALT_DYLD_POLICY has been set on this
1948 * executable and redirect the kernel to load that linker.
1953 uint32_t policy_flags
= 0;
1954 int32_t policy_gencount
= 0;
1956 policy_error
= proc_uuid_policy_lookup(result
->uuid
, &policy_flags
, &policy_gencount
);
1957 if (policy_error
== 0) {
1958 if (policy_flags
& PROC_UUID_ALT_DYLD_POLICY
) {
1959 name
= dyld_alt_path
;
1965 #if !(DEVELOPMENT || DEBUG)
1966 if (0 != strcmp(name
, DEFAULT_DYLD_PATH
)) {
1967 return (LOAD_BADMACHO
);
1971 /* Allocate wad-of-data from heap to reduce excessively deep stacks */
1973 MALLOC(dyld_data
, void *, sizeof (*dyld_data
), M_TEMP
, M_WAITOK
);
1974 header
= &dyld_data
->__header
;
1975 myresult
= &dyld_data
->__myresult
;
1976 macho_data
= &dyld_data
->__macho_data
;
1978 ret
= get_macho_vnode(name
, archbits
, header
,
1979 &file_offset
, &macho_size
, macho_data
, &vp
);
1983 *myresult
= load_result_null
;
1984 myresult
->is64bit
= result
->is64bit
;
1986 ret
= parse_machfile(vp
, map
, thread
, header
, file_offset
,
1987 macho_size
, depth
, slide
, 0, myresult
, result
, imgp
);
1989 if (ret
== LOAD_SUCCESS
) {
1990 if (result
->threadstate
) {
1991 /* don't use the app's threadstate if we have a dyld */
1992 kfree(result
->threadstate
, result
->threadstate_sz
);
1994 result
->threadstate
= myresult
->threadstate
;
1995 result
->threadstate_sz
= myresult
->threadstate_sz
;
1997 result
->dynlinker
= TRUE
;
1998 result
->entry_point
= myresult
->entry_point
;
1999 result
->validentry
= myresult
->validentry
;
2000 result
->all_image_info_addr
= myresult
->all_image_info_addr
;
2001 result
->all_image_info_size
= myresult
->all_image_info_size
;
2002 if (myresult
->platform_binary
) {
2003 result
->csflags
|= CS_DYLD_PLATFORM
;
2009 FREE(dyld_data
, M_TEMP
);
2014 static load_return_t
2015 load_code_signature(
2016 struct linkedit_data_command
*lcp
,
2021 load_result_t
*result
,
2022 struct image_params
*imgp
)
2028 struct cs_blob
*blob
;
2030 vm_size_t blob_size
;
2035 if (lcp
->cmdsize
!= sizeof (struct linkedit_data_command
) ||
2036 lcp
->dataoff
+ lcp
->datasize
> macho_size
) {
2037 ret
= LOAD_BADMACHO
;
2041 blob
= ubc_cs_blob_get(vp
, cputype
, macho_offset
);
2043 /* we already have a blob for this vnode and cputype */
2044 if (blob
->csb_cpu_type
== cputype
&&
2045 blob
->csb_base_offset
== macho_offset
&&
2046 blob
->csb_mem_size
== lcp
->datasize
) {
2047 /* it matches the blob we want here, lets verify the version */
2048 if(0 != ubc_cs_generation_check(vp
)) {
2049 if (0 != ubc_cs_blob_revalidate(vp
, blob
, imgp
, 0)) {
2050 ret
= LOAD_FAILURE
; /* set error same as from ubc_cs_blob_add */
2056 /* the blob has changed for this vnode: fail ! */
2057 ret
= LOAD_BADMACHO
;
2062 blob_size
= lcp
->datasize
;
2063 kr
= ubc_cs_blob_allocate(&addr
, &blob_size
);
2064 if (kr
!= KERN_SUCCESS
) {
2070 error
= vn_rdwr(UIO_READ
,
2074 macho_offset
+ lcp
->dataoff
,
2080 if (error
|| resid
!= 0) {
2085 if (ubc_cs_blob_add(vp
,
2094 ubc_cs_blob_deallocate(addr
, blob_size
);
2099 /* ubc_cs_blob_add() has consumed "addr" */
2103 #if CHECK_CS_VALIDATION_BITMAP
2104 ubc_cs_validation_bitmap_allocate( vp
);
2109 if (ret
== LOAD_SUCCESS
) {
2111 panic("sucess, but no blob!");
2113 result
->csflags
|= blob
->csb_flags
;
2114 result
->platform_binary
= blob
->csb_platform_binary
;
2115 result
->cs_end_offset
= blob
->csb_end_offset
;
2118 ubc_cs_blob_deallocate(addr
, blob_size
);
2126 #if CONFIG_CODE_DECRYPTION
2128 static load_return_t
2130 struct encryption_info_command
*eip
,
2137 cpu_subtype_t cpusubtype
)
2140 pager_crypt_info_t crypt_info
;
2141 const char * cryptname
= 0;
2145 struct segment_command_64
*seg64
;
2146 struct segment_command
*seg32
;
2147 vm_map_offset_t map_offset
, map_size
;
2148 vm_object_offset_t crypto_backing_offset
;
2151 if (eip
->cmdsize
< sizeof(*eip
)) return LOAD_BADMACHO
;
2153 switch(eip
->cryptid
) {
2155 /* not encrypted, just an empty load command */
2156 return LOAD_SUCCESS
;
2158 cryptname
="com.apple.unfree";
2161 /* some random cryptid that you could manually put into
2162 * your binary if you want NULL */
2163 cryptname
="com.apple.null";
2166 return LOAD_BADMACHO
;
2169 if (map
== VM_MAP_NULL
) return (LOAD_SUCCESS
);
2170 if (NULL
== text_crypter_create
) return LOAD_FAILURE
;
2172 MALLOC_ZONE(vpath
, char *, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
2173 if(vpath
== NULL
) return LOAD_FAILURE
;
2176 error
= vn_getpath(vp
, vpath
, &len
);
2178 FREE_ZONE(vpath
, MAXPATHLEN
, M_NAMEI
);
2179 return LOAD_FAILURE
;
2182 /* set up decrypter first */
2183 crypt_file_data_t crypt_data
= {
2186 .cpusubtype
= cpusubtype
};
2187 kr
=text_crypter_create(&crypt_info
, cryptname
, (void*)&crypt_data
);
2188 #if VM_MAP_DEBUG_APPLE_PROTECT
2189 if (vm_map_debug_apple_protect
) {
2192 printf("APPLE_PROTECT: %d[%s] map %p %s(%s) -> 0x%x\n",
2193 p
->p_pid
, p
->p_comm
, map
, __FUNCTION__
, vpath
, kr
);
2195 #endif /* VM_MAP_DEBUG_APPLE_PROTECT */
2196 FREE_ZONE(vpath
, MAXPATHLEN
, M_NAMEI
);
2199 printf("set_code_unprotect: unable to create decrypter %s, kr=%d\n",
2201 if (kr
== kIOReturnNotPrivileged
) {
2202 /* text encryption returned decryption failure */
2203 return(LOAD_DECRYPTFAIL
);
2205 return LOAD_RESOURCE
;
2208 /* this is terrible, but we have to rescan the load commands to find the
2209 * virtual address of this encrypted stuff. This code is gonna look like
2210 * the dyld source one day... */
2211 struct mach_header
*header
= (struct mach_header
*)addr
;
2212 size_t mach_header_sz
= sizeof(struct mach_header
);
2213 if (header
->magic
== MH_MAGIC_64
||
2214 header
->magic
== MH_CIGAM_64
) {
2215 mach_header_sz
= sizeof(struct mach_header_64
);
2217 offset
= mach_header_sz
;
2218 uint32_t ncmds
= header
->ncmds
;
2221 * Get a pointer to the command.
2223 struct load_command
*lcp
= (struct load_command
*)(addr
+ offset
);
2224 offset
+= lcp
->cmdsize
;
2228 seg64
= (struct segment_command_64
*)lcp
;
2229 if ((seg64
->fileoff
<= eip
->cryptoff
) &&
2230 (seg64
->fileoff
+seg64
->filesize
>=
2231 eip
->cryptoff
+eip
->cryptsize
)) {
2232 map_offset
= seg64
->vmaddr
+ eip
->cryptoff
- seg64
->fileoff
+ slide
;
2233 map_size
= eip
->cryptsize
;
2234 crypto_backing_offset
= macho_offset
+ eip
->cryptoff
;
2238 seg32
= (struct segment_command
*)lcp
;
2239 if ((seg32
->fileoff
<= eip
->cryptoff
) &&
2240 (seg32
->fileoff
+seg32
->filesize
>=
2241 eip
->cryptoff
+eip
->cryptsize
)) {
2242 map_offset
= seg32
->vmaddr
+ eip
->cryptoff
- seg32
->fileoff
+ slide
;
2243 map_size
= eip
->cryptsize
;
2244 crypto_backing_offset
= macho_offset
+ eip
->cryptoff
;
2250 /* if we get here, did not find anything */
2251 return LOAD_BADMACHO
;
2254 /* now remap using the decrypter */
2255 MACHO_PRINTF(("+++ set_code_unprotect: vm[0x%llx:0x%llx]\n",
2256 (uint64_t) map_offset
,
2257 (uint64_t) (map_offset
+map_size
)));
2258 kr
= vm_map_apple_protected(map
,
2260 map_offset
+map_size
,
2261 crypto_backing_offset
,
2264 printf("set_code_unprotect(): mapping failed with %x\n", kr
);
2265 return LOAD_PROTECT
;
2268 return LOAD_SUCCESS
;
2274 * This routine exists to support the load_dylinker().
2276 * This routine has its own, separate, understanding of the FAT file format,
2277 * which is terrifically unfortunate.
2284 struct mach_header
*mach_header
,
2287 struct macho_data
*data
,
2292 vfs_context_t ctx
= vfs_context_current();
2293 proc_t p
= vfs_context_proc(ctx
);
2294 kauth_cred_t kerncred
;
2295 struct nameidata
*ndp
= &data
->__nid
;
2297 struct fat_arch fat_arch
;
2300 union macho_vnode_header
*header
= &data
->__header
;
2301 off_t fsize
= (off_t
)0;
2304 * Capture the kernel credential for use in the actual read of the
2305 * file, since the user doing the execution may have execute rights
2306 * but not read rights, but to exec something, we have to either map
2307 * or read it into the new process address space, which requires
2308 * read rights. This is to deal with lack of common credential
2309 * serialization code which would treat NOCRED as "serialize 'root'".
2311 kerncred
= vfs_context_ucred(vfs_context_kernel());
2313 /* init the namei data to point the file user's program name */
2314 NDINIT(ndp
, LOOKUP
, OP_OPEN
, FOLLOW
| LOCKLEAF
, UIO_SYSSPACE
, CAST_USER_ADDR_T(path
), ctx
);
2316 if ((error
= namei(ndp
)) != 0) {
2317 if (error
== ENOENT
) {
2318 error
= LOAD_ENOENT
;
2320 error
= LOAD_FAILURE
;
2327 /* check for regular file */
2328 if (vp
->v_type
!= VREG
) {
2329 error
= LOAD_PROTECT
;
2334 if ((error
= vnode_size(vp
, &fsize
, ctx
)) != 0) {
2335 error
= LOAD_FAILURE
;
2339 /* Check mount point */
2340 if (vp
->v_mount
->mnt_flag
& MNT_NOEXEC
) {
2341 error
= LOAD_PROTECT
;
2346 if ((error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_EXECUTE
| KAUTH_VNODE_READ_DATA
, ctx
)) != 0) {
2347 error
= LOAD_PROTECT
;
2351 /* try to open it */
2352 if ((error
= VNOP_OPEN(vp
, FREAD
, ctx
)) != 0) {
2353 error
= LOAD_PROTECT
;
2357 if ((error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)header
, sizeof (*header
), 0,
2358 UIO_SYSSPACE
, IO_NODELOCKED
, kerncred
, &resid
, p
)) != 0) {
2359 error
= LOAD_IOERROR
;
2364 error
= LOAD_BADMACHO
;
2368 if (header
->mach_header
.magic
== MH_MAGIC
||
2369 header
->mach_header
.magic
== MH_MAGIC_64
) {
2371 } else if (OSSwapBigToHostInt32(header
->fat_header
.magic
) == FAT_MAGIC
) {
2374 error
= LOAD_BADMACHO
;
2380 error
= fatfile_validate_fatarches((vm_offset_t
)(&header
->fat_header
),
2382 if (error
!= LOAD_SUCCESS
) {
2386 /* Look up our architecture in the fat file. */
2387 error
= fatfile_getarch_with_bits(archbits
,
2388 (vm_offset_t
)(&header
->fat_header
), sizeof(*header
), &fat_arch
);
2389 if (error
!= LOAD_SUCCESS
)
2392 /* Read the Mach-O header out of it */
2393 error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)&header
->mach_header
,
2394 sizeof (header
->mach_header
), fat_arch
.offset
,
2395 UIO_SYSSPACE
, IO_NODELOCKED
, kerncred
, &resid
, p
);
2397 error
= LOAD_IOERROR
;
2402 error
= LOAD_BADMACHO
;
2406 /* Is this really a Mach-O? */
2407 if (header
->mach_header
.magic
!= MH_MAGIC
&&
2408 header
->mach_header
.magic
!= MH_MAGIC_64
) {
2409 error
= LOAD_BADMACHO
;
2413 *file_offset
= fat_arch
.offset
;
2414 *macho_size
= fat_arch
.size
;
2417 * Force get_macho_vnode() to fail if the architecture bits
2418 * do not match the expected architecture bits. This in
2419 * turn causes load_dylinker() to fail for the same reason,
2420 * so it ensures the dynamic linker and the binary are in
2421 * lock-step. This is potentially bad, if we ever add to
2422 * the CPU_ARCH_* bits any bits that are desirable but not
2423 * required, since the dynamic linker might work, but we will
2424 * refuse to load it because of this check.
2426 if ((cpu_type_t
)(header
->mach_header
.cputype
& CPU_ARCH_MASK
) != archbits
) {
2427 error
= LOAD_BADARCH
;
2432 *macho_size
= fsize
;
2435 *mach_header
= header
->mach_header
;
2438 ubc_setsize(vp
, fsize
);
2442 (void) VNOP_CLOSE(vp
, FREAD
, ctx
);