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 pmap_t pmap
= 0; /* protected by create_map */
321 load_result_t myresult
;
323 boolean_t enforce_hard_pagezero
= TRUE
;
324 int in_exec
= (imgp
->ip_flags
& IMGPF_EXEC
);
325 task_t task
= current_task();
326 proc_t p
= current_proc();
327 mach_vm_offset_t aslr_offset
= 0;
328 mach_vm_offset_t dyld_aslr_offset
= 0;
331 if (macho_size
> file_size
) {
332 return(LOAD_BADMACHO
);
335 result
->is64bit
= ((imgp
->ip_flags
& IMGPF_IS_64BIT
) == IMGPF_IS_64BIT
);
338 if (imgp
->ip_new_thread
) {
339 ledger_task
= get_threadtask(imgp
->ip_new_thread
);
343 pmap
= pmap_create(get_task_ledger(ledger_task
),
346 map
= vm_map_create(pmap
,
348 vm_compute_max_offset(result
->is64bit
),
351 #if (__ARM_ARCH_7K__ >= 2) && defined(PLATFORM_WatchOS)
352 /* enforce 16KB alignment for watch targets with new ABI */
353 vm_map_set_page_shift(map
, SIXTEENK_PAGE_SHIFT
);
354 #endif /* __arm64__ */
356 #ifndef CONFIG_ENFORCE_SIGNED_CODE
357 /* This turns off faulting for executable pages, which allows
358 * to circumvent Code Signing Enforcement. The per process
359 * flag (CS_ENFORCEMENT) is not set yet, but we can use the
362 if ( !cs_enforcement(NULL
) && (header
->flags
& MH_ALLOW_STACK_EXECUTION
) )
363 vm_map_disable_NX(map
);
366 /* Forcibly disallow execution from data pages on even if the arch
367 * normally permits it. */
368 if ((header
->flags
& MH_NO_HEAP_EXECUTION
) && !(imgp
->ip_flags
& IMGPF_ALLOW_DATA_EXEC
))
369 vm_map_disallow_data_exec(map
);
372 * Compute a random offset for ASLR, and an independent random offset for dyld.
374 if (!(imgp
->ip_flags
& IMGPF_DISABLE_ASLR
)) {
375 uint64_t max_slide_pages
;
377 max_slide_pages
= vm_map_get_max_aslr_slide_pages(map
);
379 aslr_offset
= random();
380 aslr_offset
%= max_slide_pages
;
381 aslr_offset
<<= vm_map_page_shift(map
);
383 dyld_aslr_offset
= random();
384 dyld_aslr_offset
%= max_slide_pages
;
385 dyld_aslr_offset
<<= vm_map_page_shift(map
);
391 *result
= load_result_null
;
394 * re-set the bitness on the load result since we cleared the load result above.
396 result
->is64bit
= ((imgp
->ip_flags
& IMGPF_IS_64BIT
) == IMGPF_IS_64BIT
);
398 lret
= parse_machfile(vp
, map
, thread
, header
, file_offset
, macho_size
,
399 0, (int64_t)aslr_offset
, (int64_t)dyld_aslr_offset
, result
,
402 if (lret
!= LOAD_SUCCESS
) {
403 vm_map_deallocate(map
); /* will lose pmap reference too */
409 * On x86, for compatibility, don't enforce the hard page-zero restriction for 32-bit binaries.
411 if (!result
->is64bit
) {
412 enforce_hard_pagezero
= FALSE
;
416 * Check to see if the page zero is enforced by the map->min_offset.
418 if (enforce_hard_pagezero
&&
419 (vm_map_has_hard_pagezero(map
, 0x1000) == FALSE
)) {
421 vm_map_deallocate(map
); /* will lose pmap reference too */
422 return (LOAD_BADMACHO
);
426 vm_commit_pagezero_status(map
);
429 * If this is an exec, then we are going to destroy the old
430 * task, and it's correct to halt it; if it's spawn, the
431 * task is not yet running, and it makes no sense.
435 * Mark the task as halting and start the other
436 * threads towards terminating themselves. Then
437 * make sure any threads waiting for a process
438 * transition get informed that we are committed to
439 * this transition, and then finally complete the
440 * task halting (wait for threads and then cleanup
443 * NOTE: task_start_halt() makes sure that no new
444 * threads are created in the task during the transition.
445 * We need to mark the workqueue as exiting before we
446 * wait for threads to terminate (at the end of which
447 * we no longer have a prohibition on thread creation).
449 * Finally, clean up any lingering workqueue data structures
450 * that may have been left behind by the workqueue threads
451 * as they exited (and then clean up the work queue itself).
453 kret
= task_start_halt(task
);
454 if (kret
!= KERN_SUCCESS
) {
455 vm_map_deallocate(map
); /* will lose pmap reference too */
456 return (LOAD_FAILURE
);
458 proc_transcommit(p
, 0);
459 workqueue_mark_exiting(p
);
460 task_complete_halt(task
);
462 kqueue_dealloc(p
->p_wqkqueue
);
463 p
->p_wqkqueue
= NULL
;
465 * Roll up accounting info to new task. The roll up is done after
466 * task_complete_halt to make sure the thread accounting info is
467 * rolled up to current_task.
469 task_rollup_accounting_info(get_threadtask(thread
), task
);
472 return(LOAD_SUCCESS
);
475 int macho_printf
= 0;
476 #define MACHO_PRINTF(args) \
478 if (macho_printf) { \
484 * The file size of a mach-o file is limited to 32 bits; this is because
485 * this is the limit on the kalloc() of enough bytes for a mach_header and
486 * the contents of its sizeofcmds, which is currently constrained to 32
487 * bits in the file format itself. We read into the kernel buffer the
488 * commands section, and then parse it in order to parse the mach-o file
489 * format load_command segment(s). We are only interested in a subset of
490 * the total set of possible commands. If "map"==VM_MAP_NULL or
491 * "thread"==THREAD_NULL, do not make permament VM modifications,
492 * just preflight the parse.
500 struct mach_header
*header
,
505 int64_t dyld_aslr_offset
,
506 load_result_t
*result
,
507 load_result_t
*binresult
,
508 struct image_params
*imgp
512 struct load_command
*lcp
;
513 struct dylinker_command
*dlp
= 0;
514 integer_t dlarchbits
= 0;
516 load_return_t ret
= LOAD_SUCCESS
;
519 vm_size_t size
,kl_size
;
521 size_t oldoffset
; /* for overflow check */
523 proc_t p
= current_proc(); /* XXXX */
526 size_t mach_header_sz
= sizeof(struct mach_header
);
528 boolean_t got_code_signatures
= FALSE
;
529 boolean_t found_header_segment
= FALSE
;
530 boolean_t found_xhdr
= FALSE
;
532 boolean_t dyld_no_load_addr
= FALSE
;
533 boolean_t is_dyld
= FALSE
;
534 vm_map_offset_t effective_page_mask
= MAX(PAGE_MASK
, vm_map_page_mask(map
));
536 if (header
->magic
== MH_MAGIC_64
||
537 header
->magic
== MH_CIGAM_64
) {
538 mach_header_sz
= sizeof(struct mach_header_64
);
542 * Break infinite recursion
545 return(LOAD_FAILURE
);
551 * Check to see if right machine type.
553 if (((cpu_type_t
)(header
->cputype
& ~CPU_ARCH_MASK
) != (cpu_type() & ~CPU_ARCH_MASK
)) ||
554 !grade_binary(header
->cputype
,
555 header
->cpusubtype
& ~CPU_SUBTYPE_MASK
))
556 return(LOAD_BADARCH
);
558 abi64
= ((header
->cputype
& CPU_ARCH_ABI64
) == CPU_ARCH_ABI64
);
560 switch (header
->filetype
) {
564 return (LOAD_FAILURE
);
570 return (LOAD_FAILURE
);
576 return (LOAD_FAILURE
);
580 * Get the pager for the file.
582 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
585 * Map portion that must be accessible directly into
588 if ((off_t
)(mach_header_sz
+ header
->sizeofcmds
) > macho_size
)
589 return(LOAD_BADMACHO
);
592 * Round size of Mach-O commands up to page boundry.
594 size
= round_page(mach_header_sz
+ header
->sizeofcmds
);
596 return(LOAD_BADMACHO
);
599 * Map the load commands into kernel memory.
603 kl_addr
= kalloc(size
);
604 addr
= (caddr_t
)kl_addr
;
606 return(LOAD_NOSPACE
);
608 error
= vn_rdwr(UIO_READ
, vp
, addr
, size
, file_offset
,
609 UIO_SYSSPACE
, 0, kauth_cred_get(), &resid
, p
);
612 kfree(kl_addr
, kl_size
);
613 return(LOAD_IOERROR
);
617 /* We must be able to read in as much as the mach_header indicated */
619 kfree(kl_addr
, kl_size
);
620 return(LOAD_BADMACHO
);
624 * For PIE and dyld, slide everything by the ASLR offset.
626 if ((header
->flags
& MH_PIE
) || is_dyld
) {
631 * Scan through the commands, processing each one as necessary.
632 * We parse in three passes through the headers:
633 * 0: determine if TEXT and DATA boundary can be page-aligned
634 * 1: thread state, uuid, code signature
636 * 3: dyld, encryption, check entry point
639 boolean_t slide_realign
= FALSE
;
641 for (pass
= 0; pass
<= 3; pass
++) {
643 if (pass
== 0 && !slide_realign
&& !is_dyld
) {
644 /* if we dont need to realign the slide or determine dyld's load
645 * address, pass 0 can be skipped */
647 } else if (pass
== 1) {
649 if (dyld_no_load_addr
&& binresult
) {
651 * The dyld Mach-O does not specify a load address. Try to locate
652 * it right after the main binary. If binresult == NULL, load
653 * directly to the given slide.
655 slide
= vm_map_round_page(slide
+ binresult
->max_vm_addr
, effective_page_mask
);
660 * Check that the entry point is contained in an executable segments
662 if ((pass
== 3) && (!result
->using_lcmain
&& result
->validentry
== 0)) {
663 thread_state_initialize(thread
);
669 * Check that some segment maps the start of the mach-o file, which is
670 * needed by the dynamic loader to read the mach headers, etc.
672 if ((pass
== 3) && (found_header_segment
== FALSE
)) {
678 * Loop through each of the load_commands indicated by the
679 * Mach-O header; if an absurd value is provided, we just
680 * run off the end of the reserved section by incrementing
681 * the offset too far, so we are implicitly fail-safe.
683 offset
= mach_header_sz
;
684 ncmds
= header
->ncmds
;
688 * Get a pointer to the command.
690 lcp
= (struct load_command
*)(addr
+ offset
);
692 offset
+= lcp
->cmdsize
;
695 * Perform prevalidation of the struct load_command
696 * before we attempt to use its contents. Invalid
697 * values are ones which result in an overflow, or
698 * which can not possibly be valid commands, or which
699 * straddle or exist past the reserved section at the
700 * start of the image.
702 if (oldoffset
> offset
||
703 lcp
->cmdsize
< sizeof(struct load_command
) ||
704 offset
> header
->sizeofcmds
+ mach_header_sz
) {
710 * Act on struct load_command's for which kernel
711 * intervention is required.
715 struct segment_command
*scp
= (struct segment_command
*) lcp
;
718 if (is_dyld
&& scp
->vmaddr
== 0 && scp
->fileoff
== 0) {
719 dyld_no_load_addr
= TRUE
;
720 if (!slide_realign
) {
721 /* got what we need, bail early on pass 0 */
729 if (pass
== 1 && !strncmp(scp
->segname
, "__XHDR", sizeof(scp
->segname
))) {
738 * Having an LC_SEGMENT command for the
739 * wrong ABI is invalid <rdar://problem/11021230>
745 ret
= load_segment(lcp
,
755 if (ret
== LOAD_SUCCESS
&& scp
->fileoff
== 0 && scp
->filesize
> 0) {
756 /* Enforce a single segment mapping offset zero, with R+X
758 if (found_header_segment
||
759 ((scp
->initprot
& (VM_PROT_READ
|VM_PROT_EXECUTE
)) != (VM_PROT_READ
|VM_PROT_EXECUTE
))) {
763 found_header_segment
= TRUE
;
768 case LC_SEGMENT_64
: {
769 struct segment_command_64
*scp64
= (struct segment_command_64
*) lcp
;
772 if (is_dyld
&& scp64
->vmaddr
== 0 && scp64
->fileoff
== 0) {
773 dyld_no_load_addr
= TRUE
;
774 if (!slide_realign
) {
775 /* got what we need, bail early on pass 0 */
781 if (pass
== 1 && !strncmp(scp64
->segname
, "__XHDR", sizeof(scp64
->segname
))) {
790 * Having an LC_SEGMENT_64 command for the
791 * wrong ABI is invalid <rdar://problem/11021230>
797 ret
= load_segment(lcp
,
807 if (ret
== LOAD_SUCCESS
&& scp64
->fileoff
== 0 && scp64
->filesize
> 0) {
808 /* Enforce a single segment mapping offset zero, with R+X
810 if (found_header_segment
||
811 ((scp64
->initprot
& (VM_PROT_READ
|VM_PROT_EXECUTE
)) != (VM_PROT_READ
|VM_PROT_EXECUTE
))) {
815 found_header_segment
= TRUE
;
823 ret
= load_unixthread(
824 (struct thread_command
*) lcp
,
835 (struct entry_point_command
*) lcp
,
840 case LC_LOAD_DYLINKER
:
843 if ((depth
== 1) && (dlp
== 0)) {
844 dlp
= (struct dylinker_command
*)lcp
;
845 dlarchbits
= (header
->cputype
& CPU_ARCH_MASK
);
851 if (pass
== 1 && depth
== 1) {
852 ret
= load_uuid((struct uuid_command
*) lcp
,
853 (char *)addr
+ mach_header_sz
+ header
->sizeofcmds
,
857 case LC_CODE_SIGNATURE
:
862 load signatures & store in uip
863 set VM object "signed_pages"
865 ret
= load_code_signature(
866 (struct linkedit_data_command
*) lcp
,
873 if (ret
!= LOAD_SUCCESS
) {
874 printf("proc %d: load code signature error %d "
876 p
->p_pid
, ret
, vp
->v_name
);
878 * Allow injections to be ignored on devices w/o enforcement enabled
880 if (!cs_enforcement(NULL
))
881 ret
= LOAD_SUCCESS
; /* ignore error */
884 got_code_signatures
= TRUE
;
887 if (got_code_signatures
) {
888 unsigned tainted
= CS_VALIDATE_TAINTED
;
889 boolean_t valid
= FALSE
;
894 printf("validating initial pages of %s\n", vp
->v_name
);
896 while (off
< size
&& ret
== LOAD_SUCCESS
) {
897 tainted
= CS_VALIDATE_TAINTED
;
899 valid
= cs_validate_range(vp
,
905 if (!valid
|| (tainted
& CS_VALIDATE_TAINTED
)) {
907 printf("CODE SIGNING: %s[%d]: invalid initial page at offset %lld validated:%d tainted:%d csflags:0x%x\n",
908 vp
->v_name
, p
->p_pid
, (long long)(file_offset
+ off
), valid
, tainted
, result
->csflags
);
909 if (cs_enforcement(NULL
) ||
910 (result
->csflags
& (CS_HARD
|CS_KILL
|CS_ENFORCEMENT
))) {
913 result
->csflags
&= ~CS_VALID
;
920 #if CONFIG_CODE_DECRYPTION
921 case LC_ENCRYPTION_INFO
:
922 case LC_ENCRYPTION_INFO_64
:
925 ret
= set_code_unprotect(
926 (struct encryption_info_command
*) lcp
,
927 addr
, map
, slide
, vp
, file_offset
,
928 header
->cputype
, header
->cpusubtype
);
929 if (ret
!= LOAD_SUCCESS
) {
930 os_reason_t load_failure_reason
= OS_REASON_NULL
;
931 printf("proc %d: set_code_unprotect() error %d "
933 p
->p_pid
, ret
, vp
->v_name
);
935 * Don't let the app run if it's
936 * encrypted but we failed to set up the
937 * decrypter. If the keys are missing it will
938 * return LOAD_DECRYPTFAIL.
940 if (ret
== LOAD_DECRYPTFAIL
) {
941 /* failed to load due to missing FP keys */
943 p
->p_lflag
|= P_LTERM_DECRYPTFAIL
;
946 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXITREASON_CREATE
) | DBG_FUNC_NONE
,
947 p
->p_pid
, OS_REASON_EXEC
, EXEC_EXIT_REASON_FAIRPLAY_DECRYPT
, 0, 0);
948 load_failure_reason
= os_reason_create(OS_REASON_EXEC
, EXEC_EXIT_REASON_FAIRPLAY_DECRYPT
);
951 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXITREASON_CREATE
) | DBG_FUNC_NONE
,
952 p
->p_pid
, OS_REASON_EXEC
, EXEC_EXIT_REASON_DECRYPT
, 0, 0);
953 load_failure_reason
= os_reason_create(OS_REASON_EXEC
, EXEC_EXIT_REASON_DECRYPT
);
956 assert(load_failure_reason
!= OS_REASON_NULL
);
957 psignal_with_reason(p
, SIGKILL
, load_failure_reason
);
962 /* Other commands are ignored by the kernel */
966 if (ret
!= LOAD_SUCCESS
)
969 if (ret
!= LOAD_SUCCESS
)
973 if (ret
== LOAD_SUCCESS
) {
974 if (! got_code_signatures
) {
975 if (cs_enforcement(NULL
)) {
979 * No embedded signatures: look for detached by taskgated,
980 * this is only done on OSX, on embedded platforms we expect everything
981 * to be have embedded signatures.
983 struct cs_blob
*blob
;
985 blob
= ubc_cs_blob_get(vp
, -1, file_offset
);
987 unsigned int cs_flag_data
= blob
->csb_flags
;
988 if(0 != ubc_cs_generation_check(vp
)) {
989 if (0 != ubc_cs_blob_revalidate(vp
, blob
, imgp
, 0)) {
990 /* clear out the flag data if revalidation fails */
992 result
->csflags
&= ~CS_VALID
;
995 /* get flags to be applied to the process */
996 result
->csflags
|= cs_flag_data
;
1001 /* Make sure if we need dyld, we got it */
1002 if (result
->needs_dynlinker
&& !dlp
) {
1006 if ((ret
== LOAD_SUCCESS
) && (dlp
!= 0)) {
1008 * load the dylinker, and slide it by the independent DYLD ASLR
1009 * offset regardless of the PIE-ness of the main binary.
1011 ret
= load_dylinker(dlp
, dlarchbits
, map
, thread
, depth
,
1012 dyld_aslr_offset
, result
, imgp
);
1015 if ((ret
== LOAD_SUCCESS
) && (depth
== 1)) {
1016 if (result
->thread_count
== 0) {
1022 if (ret
== LOAD_BADMACHO
&& found_xhdr
) {
1023 ret
= LOAD_BADMACHO_UPX
;
1027 kfree(kl_addr
, kl_size
);
1033 #if CONFIG_CODE_DECRYPTION
1035 #define APPLE_UNPROTECTED_HEADER_SIZE (3 * 4096)
1037 static load_return_t
1038 unprotect_dsmos_segment(
1044 vm_map_offset_t map_addr
,
1045 vm_map_size_t map_size
)
1050 * The first APPLE_UNPROTECTED_HEADER_SIZE bytes (from offset 0 of
1051 * this part of a Universal binary) are not protected...
1052 * The rest needs to be "transformed".
1054 if (file_off
<= APPLE_UNPROTECTED_HEADER_SIZE
&&
1055 file_off
+ file_size
<= APPLE_UNPROTECTED_HEADER_SIZE
) {
1056 /* it's all unprotected, nothing to do... */
1059 if (file_off
<= APPLE_UNPROTECTED_HEADER_SIZE
) {
1061 * We start mapping in the unprotected area.
1062 * Skip the unprotected part...
1064 vm_map_offset_t delta
;
1066 delta
= APPLE_UNPROTECTED_HEADER_SIZE
;
1071 /* ... transform the rest of the mapping. */
1072 struct pager_crypt_info crypt_info
;
1073 crypt_info
.page_decrypt
= dsmos_page_transform
;
1074 crypt_info
.crypt_ops
= NULL
;
1075 crypt_info
.crypt_end
= NULL
;
1076 #pragma unused(vp, macho_offset)
1077 crypt_info
.crypt_ops
= (void *)0x2e69cf40;
1078 vm_map_offset_t crypto_backing_offset
;
1079 crypto_backing_offset
= -1; /* i.e. use map entry's offset */
1080 #if VM_MAP_DEBUG_APPLE_PROTECT
1081 if (vm_map_debug_apple_protect
) {
1084 printf("APPLE_PROTECT: %d[%s] map %p "
1085 "[0x%llx:0x%llx] %s(%s)\n",
1086 p
->p_pid
, p
->p_comm
, map
,
1087 (uint64_t) map_addr
,
1088 (uint64_t) (map_addr
+ map_size
),
1089 __FUNCTION__
, vp
->v_name
);
1091 #endif /* VM_MAP_DEBUG_APPLE_PROTECT */
1093 /* The DSMOS pager can only be used by apple signed code */
1094 struct cs_blob
* blob
= csvnode_get_blob(vp
, file_off
);
1095 if( blob
== NULL
|| !blob
->csb_platform_binary
|| blob
->csb_platform_path
)
1097 return LOAD_FAILURE
;
1100 kr
= vm_map_apple_protected(map
,
1102 map_addr
+ map_size
,
1103 crypto_backing_offset
,
1107 if (kr
!= KERN_SUCCESS
) {
1108 return LOAD_FAILURE
;
1110 return LOAD_SUCCESS
;
1112 #else /* CONFIG_CODE_DECRYPTION */
1113 static load_return_t
1114 unprotect_dsmos_segment(
1115 __unused
uint64_t file_off
,
1116 __unused
uint64_t file_size
,
1117 __unused
struct vnode
*vp
,
1118 __unused off_t macho_offset
,
1119 __unused vm_map_t map
,
1120 __unused vm_map_offset_t map_addr
,
1121 __unused vm_map_size_t map_size
)
1123 return LOAD_SUCCESS
;
1125 #endif /* CONFIG_CODE_DECRYPTION */
1130 * Maps a Mach-O segment, taking care of mis-alignment (wrt the system
1131 * page size) issues.
1133 * The mapping might result in 1, 2 or 3 map entries:
1134 * 1. for the first page, which could be overlap with the previous
1136 * 2. for the center (if applicable),
1137 * 3. for the last page, which could overlap with the next mapping.
1139 * For each of those map entries, we might have to interpose a
1140 * "fourk_pager" to deal with mis-alignment wrt the system page size,
1141 * either in the mapping address and/or size or the file offset and/or
1143 * The "fourk_pager" itself would be mapped with proper alignment
1144 * wrt the system page size and would then be populated with the
1145 * information about the intended mapping, with a "4KB" granularity.
1147 static kern_return_t
1150 vm_map_offset_t vm_start
,
1151 vm_map_offset_t vm_end
,
1152 memory_object_control_t control
,
1153 vm_map_offset_t file_start
,
1154 vm_map_offset_t file_end
,
1158 int extra_vm_flags
, cur_extra_vm_flags
;
1159 vm_map_offset_t cur_offset
, cur_start
, cur_end
;
1161 vm_map_offset_t effective_page_mask
;
1163 if (vm_end
< vm_start
||
1164 file_end
< file_start
) {
1165 return LOAD_BADMACHO
;
1167 if (vm_end
== vm_start
||
1168 file_end
== file_start
) {
1169 /* nothing to map... */
1170 return LOAD_SUCCESS
;
1173 effective_page_mask
= MAX(PAGE_MASK
, vm_map_page_mask(map
));
1176 if (vm_map_page_aligned(vm_start
, effective_page_mask
) &&
1177 vm_map_page_aligned(vm_end
, effective_page_mask
) &&
1178 vm_map_page_aligned(file_start
, effective_page_mask
) &&
1179 vm_map_page_aligned(file_end
, effective_page_mask
)) {
1180 /* all page-aligned and map-aligned: proceed */
1182 panic("map_segment: unexpected mis-alignment "
1183 "vm[0x%llx:0x%llx] file[0x%llx:0x%llx]\n",
1184 (uint64_t) vm_start
,
1186 (uint64_t) file_start
,
1187 (uint64_t) file_end
);
1191 cur_start
= vm_start
;
1193 if (cur_end
>= vm_start
+ (file_end
- file_start
)) {
1194 /* all mapped: done */
1197 if (vm_map_round_page(cur_end
, effective_page_mask
) >=
1198 vm_map_trunc_page(vm_start
+ (file_end
- file_start
),
1199 effective_page_mask
)) {
1202 cur_start
= cur_end
;
1203 if ((vm_start
& effective_page_mask
) !=
1204 (file_start
& effective_page_mask
)) {
1205 /* one 4K pager for the middle */
1206 cur_extra_vm_flags
= extra_vm_flags
;
1208 /* regular mapping for the middle */
1209 cur_extra_vm_flags
= 0;
1211 cur_end
= vm_map_trunc_page(vm_start
+ (file_end
-
1213 effective_page_mask
);
1214 if (control
!= MEMORY_OBJECT_CONTROL_NULL
) {
1215 ret
= vm_map_enter_mem_object_control(
1218 cur_end
- cur_start
,
1219 (mach_vm_offset_t
)0,
1220 VM_FLAGS_FIXED
| cur_extra_vm_flags
,
1222 file_start
+ cur_offset
,
1225 VM_INHERIT_DEFAULT
);
1227 ret
= vm_map_enter_mem_object(
1230 cur_end
- cur_start
,
1231 (mach_vm_offset_t
)0,
1232 VM_FLAGS_FIXED
| cur_extra_vm_flags
,
1237 VM_INHERIT_DEFAULT
);
1239 if (ret
!= KERN_SUCCESS
) {
1240 return (LOAD_NOSPACE
);
1242 cur_offset
+= cur_end
- cur_start
;
1244 if (cur_end
>= vm_start
+ (file_end
- file_start
)) {
1245 /* all mapped: done */
1248 cur_start
= cur_end
;
1250 assert(cur_end
>= vm_start
+ (file_end
- file_start
));
1251 return LOAD_SUCCESS
;
1257 struct load_command
*lcp
,
1265 load_result_t
*result
)
1267 struct segment_command_64 segment_command
, *scp
;
1269 vm_map_size_t delta_size
;
1272 size_t segment_command_size
, total_section_size
,
1273 single_section_size
;
1274 vm_map_offset_t file_offset
, file_size
;
1275 vm_map_offset_t vm_offset
, vm_size
;
1276 vm_map_offset_t vm_start
, vm_end
, vm_end_aligned
;
1277 vm_map_offset_t file_start
, file_end
;
1280 vm_map_size_t effective_page_size
;
1281 vm_map_offset_t effective_page_mask
;
1283 effective_page_size
= MAX(PAGE_SIZE
, vm_map_page_size(map
));
1284 effective_page_mask
= MAX(PAGE_MASK
, vm_map_page_mask(map
));
1287 if (LC_SEGMENT_64
== lcp
->cmd
) {
1288 segment_command_size
= sizeof(struct segment_command_64
);
1289 single_section_size
= sizeof(struct section_64
);
1291 segment_command_size
= sizeof(struct segment_command
);
1292 single_section_size
= sizeof(struct section
);
1294 if (lcp
->cmdsize
< segment_command_size
)
1295 return (LOAD_BADMACHO
);
1296 total_section_size
= lcp
->cmdsize
- segment_command_size
;
1298 if (LC_SEGMENT_64
== lcp
->cmd
) {
1299 scp
= (struct segment_command_64
*)lcp
;
1301 scp
= &segment_command
;
1302 widen_segment_command((struct segment_command
*)lcp
, scp
);
1306 MACHO_PRINTF(("+++ load_segment %s "
1307 "vm[0x%llx:0x%llx] file[0x%llx:0x%llx] "
1308 "prot %d/%d flags 0x%x\n",
1310 (uint64_t)(slide
+ scp
->vmaddr
),
1311 (uint64_t)(slide
+ scp
->vmaddr
+ scp
->vmsize
),
1312 pager_offset
+ scp
->fileoff
,
1313 pager_offset
+ scp
->fileoff
+ scp
->filesize
,
1320 * Make sure what we get from the file is really ours (as specified
1323 if (scp
->fileoff
+ scp
->filesize
< scp
->fileoff
||
1324 scp
->fileoff
+ scp
->filesize
> (uint64_t)macho_size
) {
1325 return (LOAD_BADMACHO
);
1328 * Ensure that the number of sections specified would fit
1329 * within the load command size.
1331 if (total_section_size
/ single_section_size
< scp
->nsects
) {
1332 return (LOAD_BADMACHO
);
1335 * Make sure the segment is page-aligned in the file.
1337 file_offset
= pager_offset
+ scp
->fileoff
; /* limited to 32 bits */
1338 file_size
= scp
->filesize
;
1339 if ((file_offset
& PAGE_MASK_64
) != 0 ||
1340 /* we can't mmap() it if it's not page-aligned in the file */
1341 (file_offset
& vm_map_page_mask(map
)) != 0) {
1343 * The 1st test would have failed if the system's page size
1344 * was what this process believe is the page size, so let's
1345 * fail here too for the sake of consistency.
1347 return (LOAD_BADMACHO
);
1351 * If we have a code signature attached for this slice
1352 * require that the segments are within the signed part
1355 if (result
->cs_end_offset
&&
1356 result
->cs_end_offset
< (off_t
)scp
->fileoff
&&
1357 result
->cs_end_offset
- scp
->fileoff
< scp
->filesize
)
1360 printf("section outside code signature\n");
1361 return LOAD_BADMACHO
;
1364 vm_offset
= scp
->vmaddr
+ slide
;
1365 vm_size
= scp
->vmsize
;
1368 return (LOAD_SUCCESS
);
1369 if (scp
->vmaddr
== 0 &&
1372 (scp
->initprot
& VM_PROT_ALL
) == VM_PROT_NONE
&&
1373 (scp
->maxprot
& VM_PROT_ALL
) == VM_PROT_NONE
) {
1375 * For PIE, extend page zero rather than moving it. Extending
1376 * page zero keeps early allocations from falling predictably
1377 * between the end of page zero and the beginning of the first
1381 * This is a "page zero" segment: it starts at address 0,
1382 * is not mapped from the binary file and is not accessible.
1383 * User-space should never be able to access that memory, so
1384 * make it completely off limits by raising the VM map's
1387 vm_end
= vm_offset
+ vm_size
;
1388 if (vm_end
< vm_offset
) {
1389 return (LOAD_BADMACHO
);
1392 MACHO_PRINTF(("++++++ load_segment: "
1393 "page_zero up to 0x%llx\n",
1394 (uint64_t) vm_end
));
1397 vm_end
= vm_map_round_page(vm_end
,
1399 vm_end_aligned
= vm_end
;
1401 ret
= vm_map_raise_min_offset(map
,
1404 if (ret
!= KERN_SUCCESS
) {
1405 return (LOAD_FAILURE
);
1407 return (LOAD_SUCCESS
);
1412 file_start
= vm_map_trunc_page(file_offset
,
1413 effective_page_mask
);
1414 file_end
= vm_map_round_page(file_offset
+ file_size
,
1415 effective_page_mask
);
1416 vm_start
= vm_map_trunc_page(vm_offset
,
1417 effective_page_mask
);
1418 vm_end
= vm_map_round_page(vm_offset
+ vm_size
,
1419 effective_page_mask
);
1422 if (vm_start
< result
->min_vm_addr
)
1423 result
->min_vm_addr
= vm_start
;
1424 if (vm_end
> result
->max_vm_addr
)
1425 result
->max_vm_addr
= vm_end
;
1427 if (map
== VM_MAP_NULL
)
1428 return (LOAD_SUCCESS
);
1431 initprot
= (scp
->initprot
) & VM_PROT_ALL
;
1432 maxprot
= (scp
->maxprot
) & VM_PROT_ALL
;
1434 * Map a copy of the file into the address space.
1437 MACHO_PRINTF(("++++++ load_segment: "
1438 "mapping at vm [0x%llx:0x%llx] of "
1439 "file [0x%llx:0x%llx]\n",
1440 (uint64_t) vm_start
,
1442 (uint64_t) file_start
,
1443 (uint64_t) file_end
));
1445 ret
= map_segment(map
,
1454 return LOAD_NOSPACE
;
1459 * If the file didn't end on a page boundary,
1460 * we need to zero the leftover.
1462 delta_size
= map_size
- scp
->filesize
;
1463 if (delta_size
> 0) {
1464 mach_vm_offset_t tmp
;
1466 ret
= mach_vm_allocate(kernel_map
, &tmp
, delta_size
, VM_FLAGS_ANYWHERE
| VM_MAKE_TAG(VM_KERN_MEMORY_BSD
));
1467 if (ret
!= KERN_SUCCESS
) {
1468 return(LOAD_RESOURCE
);
1471 if (copyout(tmp
, map_addr
+ scp
->filesize
,
1473 (void) mach_vm_deallocate(
1474 kernel_map
, tmp
, delta_size
);
1475 return (LOAD_FAILURE
);
1478 (void) mach_vm_deallocate(kernel_map
, tmp
, delta_size
);
1484 * If the virtual size of the segment is greater
1485 * than the size from the file, we need to allocate
1486 * zero fill memory for the rest.
1488 if ((vm_end
- vm_start
) > (file_end
- file_start
)) {
1489 delta_size
= (vm_end
- vm_start
) - (file_end
- file_start
);
1493 if (delta_size
> 0) {
1494 mach_vm_offset_t tmp
;
1496 tmp
= vm_start
+ (file_end
- file_start
);
1498 MACHO_PRINTF(("++++++ load_segment: "
1499 "delta mapping vm [0x%llx:0x%llx]\n",
1501 (uint64_t) (tmp
+ delta_size
)));
1503 kr
= map_segment(map
,
1506 MEMORY_OBJECT_CONTROL_NULL
,
1511 if (kr
!= KERN_SUCCESS
) {
1512 return(LOAD_NOSPACE
);
1516 if ( (scp
->fileoff
== 0) && (scp
->filesize
!= 0) )
1517 result
->mach_header
= vm_offset
;
1519 if (scp
->flags
& SG_PROTECTED_VERSION_1
) {
1520 ret
= unprotect_dsmos_segment(file_start
,
1521 file_end
- file_start
,
1527 if (ret
!= LOAD_SUCCESS
) {
1534 if (LOAD_SUCCESS
== ret
&&
1535 filetype
== MH_DYLINKER
&&
1536 result
->all_image_info_addr
== MACH_VM_MIN_ADDRESS
) {
1537 note_all_image_info_section(scp
,
1538 LC_SEGMENT_64
== lcp
->cmd
,
1539 single_section_size
,
1540 ((const char *)lcp
+
1541 segment_command_size
),
1546 if (result
->entry_point
!= MACH_VM_MIN_ADDRESS
) {
1547 if ((result
->entry_point
>= vm_offset
) && (result
->entry_point
< (vm_offset
+ vm_size
))) {
1548 if ((scp
->initprot
& (VM_PROT_READ
|VM_PROT_EXECUTE
)) == (VM_PROT_READ
|VM_PROT_EXECUTE
)) {
1549 result
->validentry
= 1;
1551 /* right range but wrong protections, unset if previously validated */
1552 result
->validentry
= 0;
1563 struct uuid_command
*uulp
,
1565 load_result_t
*result
1569 * We need to check the following for this command:
1570 * - The command size should be atleast the size of struct uuid_command
1571 * - The UUID part of the command should be completely within the mach-o header
1574 if ((uulp
->cmdsize
< sizeof(struct uuid_command
)) ||
1575 (((char *)uulp
+ sizeof(struct uuid_command
)) > command_end
)) {
1576 return (LOAD_BADMACHO
);
1579 memcpy(&result
->uuid
[0], &uulp
->uuid
[0], sizeof(result
->uuid
));
1580 return (LOAD_SUCCESS
);
1586 struct entry_point_command
*epc
,
1589 load_result_t
*result
1592 mach_vm_offset_t addr
;
1595 if (epc
->cmdsize
< sizeof(*epc
))
1596 return (LOAD_BADMACHO
);
1597 if (result
->thread_count
!= 0) {
1598 return (LOAD_FAILURE
);
1601 if (thread
== THREAD_NULL
)
1602 return (LOAD_SUCCESS
);
1605 * LC_MAIN specifies stack size but not location.
1606 * Add guard page to allocation size (MAXSSIZ includes guard page).
1608 if (epc
->stacksize
) {
1609 if (os_add_overflow(epc
->stacksize
, 4*PAGE_SIZE
, &result
->user_stack_size
)) {
1611 * We are going to immediately throw away this result, but we want
1612 * to make sure we aren't loading a dangerously close to
1613 * overflowing value, since this will have a guard page added to it
1614 * and be rounded to page boundaries
1616 return LOAD_BADMACHO
;
1618 result
->user_stack_size
= epc
->stacksize
;
1619 if (os_add_overflow(epc
->stacksize
, PAGE_SIZE
, &result
->user_stack_alloc_size
)) {
1620 return LOAD_BADMACHO
;
1623 result
->user_stack_alloc_size
= MAXSSIZ
;
1626 /* use default location for stack */
1627 ret
= thread_userstackdefault(&addr
, result
->is64bit
);
1628 if (ret
!= KERN_SUCCESS
)
1629 return(LOAD_FAILURE
);
1631 /* The stack slides down from the default location */
1632 result
->user_stack
= addr
;
1633 result
->user_stack
-= slide
;
1635 if (result
->using_lcmain
|| result
->entry_point
!= MACH_VM_MIN_ADDRESS
) {
1636 /* Already processed LC_MAIN or LC_UNIXTHREAD */
1637 return (LOAD_FAILURE
);
1640 /* kernel does *not* use entryoff from LC_MAIN. Dyld uses it. */
1641 result
->needs_dynlinker
= TRUE
;
1642 result
->using_lcmain
= TRUE
;
1644 ret
= thread_state_initialize( thread
);
1645 if (ret
!= KERN_SUCCESS
) {
1646 return(LOAD_FAILURE
);
1649 result
->unixproc
= TRUE
;
1650 result
->thread_count
++;
1652 return(LOAD_SUCCESS
);
1659 struct thread_command
*tcp
,
1662 load_result_t
*result
1667 mach_vm_offset_t addr
;
1669 if (tcp
->cmdsize
< sizeof(*tcp
))
1670 return (LOAD_BADMACHO
);
1671 if (result
->thread_count
!= 0) {
1672 return (LOAD_FAILURE
);
1675 if (thread
== THREAD_NULL
)
1676 return (LOAD_SUCCESS
);
1678 ret
= load_threadstack(thread
,
1679 (uint32_t *)(((vm_offset_t
)tcp
) +
1680 sizeof(struct thread_command
)),
1681 tcp
->cmdsize
- sizeof(struct thread_command
),
1682 &addr
, &customstack
, result
);
1683 if (ret
!= LOAD_SUCCESS
)
1686 /* LC_UNIXTHREAD optionally specifies stack size and location */
1689 result
->user_stack_alloc_size
= MAXSSIZ
;
1692 /* The stack slides down from the default location */
1693 result
->user_stack
= addr
;
1694 result
->user_stack
-= slide
;
1696 ret
= load_threadentry(thread
,
1697 (uint32_t *)(((vm_offset_t
)tcp
) +
1698 sizeof(struct thread_command
)),
1699 tcp
->cmdsize
- sizeof(struct thread_command
),
1701 if (ret
!= LOAD_SUCCESS
)
1704 if (result
->using_lcmain
|| result
->entry_point
!= MACH_VM_MIN_ADDRESS
) {
1705 /* Already processed LC_MAIN or LC_UNIXTHREAD */
1706 return (LOAD_FAILURE
);
1709 result
->entry_point
= addr
;
1710 result
->entry_point
+= slide
;
1712 ret
= load_threadstate(thread
,
1713 (uint32_t *)(((vm_offset_t
)tcp
) + sizeof(struct thread_command
)),
1714 tcp
->cmdsize
- sizeof(struct thread_command
),
1716 if (ret
!= LOAD_SUCCESS
)
1719 result
->unixproc
= TRUE
;
1720 result
->thread_count
++;
1722 return(LOAD_SUCCESS
);
1730 uint32_t total_size
,
1731 load_result_t
*result
1736 uint32_t thread_size
;
1737 uint32_t *local_ts
= NULL
;
1738 uint32_t local_ts_size
= 0;
1743 if (total_size
> 0) {
1744 local_ts_size
= total_size
;
1745 local_ts
= kalloc(local_ts_size
);
1746 if (local_ts
== NULL
) {
1747 return LOAD_FAILURE
;
1749 memcpy(local_ts
, ts
, local_ts_size
);
1754 * Validate the new thread state; iterate through the state flavors in
1756 * XXX: we should validate the machine state here, to avoid failing at
1757 * activation time where we can't bail out cleanly.
1759 while (total_size
> 0) {
1763 if (os_add_and_mul_overflow(size
, 2, sizeof(uint32_t), &thread_size
) ||
1764 os_sub_overflow(total_size
, thread_size
, &total_size
)) {
1765 ret
= LOAD_BADMACHO
;
1769 ts
+= size
; /* ts is a (uint32_t *) */
1772 result
->threadstate
= local_ts
;
1773 result
->threadstate_sz
= local_ts_size
;
1774 return LOAD_SUCCESS
;
1778 kfree(local_ts
, local_ts_size
);
1788 uint32_t total_size
,
1789 mach_vm_offset_t
*user_stack
,
1791 load_result_t
*result
1797 uint32_t stack_size
;
1799 while (total_size
> 0) {
1802 if (UINT32_MAX
-2 < size
||
1803 UINT32_MAX
/sizeof(uint32_t) < size
+2)
1804 return (LOAD_BADMACHO
);
1805 stack_size
= (size
+2)*sizeof(uint32_t);
1806 if (stack_size
> total_size
)
1807 return(LOAD_BADMACHO
);
1808 total_size
-= stack_size
;
1811 * Third argument is a kernel space pointer; it gets cast
1812 * to the appropriate type in thread_userstack() based on
1813 * the value of flavor.
1815 ret
= thread_userstack(thread
, flavor
, (thread_state_t
)ts
, size
, user_stack
, customstack
, result
->is64bit
);
1816 if (ret
!= KERN_SUCCESS
) {
1817 return(LOAD_FAILURE
);
1819 ts
+= size
; /* ts is a (uint32_t *) */
1821 return(LOAD_SUCCESS
);
1829 uint32_t total_size
,
1830 mach_vm_offset_t
*entry_point
1836 uint32_t entry_size
;
1839 * Set the thread state.
1841 *entry_point
= MACH_VM_MIN_ADDRESS
;
1842 while (total_size
> 0) {
1845 if (UINT32_MAX
-2 < size
||
1846 UINT32_MAX
/sizeof(uint32_t) < size
+2)
1847 return (LOAD_BADMACHO
);
1848 entry_size
= (size
+2)*sizeof(uint32_t);
1849 if (entry_size
> total_size
)
1850 return(LOAD_BADMACHO
);
1851 total_size
-= entry_size
;
1853 * Third argument is a kernel space pointer; it gets cast
1854 * to the appropriate type in thread_entrypoint() based on
1855 * the value of flavor.
1857 ret
= thread_entrypoint(thread
, flavor
, (thread_state_t
)ts
, size
, entry_point
);
1858 if (ret
!= KERN_SUCCESS
) {
1859 return(LOAD_FAILURE
);
1861 ts
+= size
; /* ts is a (uint32_t *) */
1863 return(LOAD_SUCCESS
);
1867 struct nameidata __nid
;
1868 union macho_vnode_header
{
1869 struct mach_header mach_header
;
1870 struct fat_header fat_header
;
1875 #define DEFAULT_DYLD_PATH "/usr/lib/dyld"
1877 #if (DEVELOPMENT || DEBUG)
1878 extern char dyld_alt_path
[];
1879 extern int use_alt_dyld
;
1882 static load_return_t
1884 struct dylinker_command
*lcp
,
1890 load_result_t
*result
,
1891 struct image_params
*imgp
1896 struct vnode
*vp
= NULLVP
; /* set by get_macho_vnode() */
1897 struct mach_header
*header
;
1898 off_t file_offset
= 0; /* set by get_macho_vnode() */
1899 off_t macho_size
= 0; /* set by get_macho_vnode() */
1900 load_result_t
*myresult
;
1902 struct macho_data
*macho_data
;
1904 struct mach_header __header
;
1905 load_result_t __myresult
;
1906 struct macho_data __macho_data
;
1909 if (lcp
->cmdsize
< sizeof(*lcp
))
1910 return (LOAD_BADMACHO
);
1912 name
= (char *)lcp
+ lcp
->name
.offset
;
1915 * Check for a proper null terminated string.
1919 if (p
>= (char *)lcp
+ lcp
->cmdsize
)
1920 return(LOAD_BADMACHO
);
1923 #if (DEVELOPMENT || DEBUG)
1927 * If an alternate dyld has been specified via boot args, check
1928 * to see if PROC_UUID_ALT_DYLD_POLICY has been set on this
1929 * executable and redirect the kernel to load that linker.
1934 uint32_t policy_flags
= 0;
1935 int32_t policy_gencount
= 0;
1937 policy_error
= proc_uuid_policy_lookup(result
->uuid
, &policy_flags
, &policy_gencount
);
1938 if (policy_error
== 0) {
1939 if (policy_flags
& PROC_UUID_ALT_DYLD_POLICY
) {
1940 name
= dyld_alt_path
;
1946 #if !(DEVELOPMENT || DEBUG)
1947 if (0 != strcmp(name
, DEFAULT_DYLD_PATH
)) {
1948 return (LOAD_BADMACHO
);
1952 /* Allocate wad-of-data from heap to reduce excessively deep stacks */
1954 MALLOC(dyld_data
, void *, sizeof (*dyld_data
), M_TEMP
, M_WAITOK
);
1955 header
= &dyld_data
->__header
;
1956 myresult
= &dyld_data
->__myresult
;
1957 macho_data
= &dyld_data
->__macho_data
;
1959 ret
= get_macho_vnode(name
, archbits
, header
,
1960 &file_offset
, &macho_size
, macho_data
, &vp
);
1964 *myresult
= load_result_null
;
1965 myresult
->is64bit
= result
->is64bit
;
1967 ret
= parse_machfile(vp
, map
, thread
, header
, file_offset
,
1968 macho_size
, depth
, slide
, 0, myresult
, result
, imgp
);
1970 if (ret
== LOAD_SUCCESS
) {
1971 if (result
->threadstate
) {
1972 /* don't use the app's threadstate if we have a dyld */
1973 kfree(result
->threadstate
, result
->threadstate_sz
);
1975 result
->threadstate
= myresult
->threadstate
;
1976 result
->threadstate_sz
= myresult
->threadstate_sz
;
1978 result
->dynlinker
= TRUE
;
1979 result
->entry_point
= myresult
->entry_point
;
1980 result
->validentry
= myresult
->validentry
;
1981 result
->all_image_info_addr
= myresult
->all_image_info_addr
;
1982 result
->all_image_info_size
= myresult
->all_image_info_size
;
1983 if (myresult
->platform_binary
) {
1984 result
->csflags
|= CS_DYLD_PLATFORM
;
1990 FREE(dyld_data
, M_TEMP
);
1995 static load_return_t
1996 load_code_signature(
1997 struct linkedit_data_command
*lcp
,
2002 load_result_t
*result
,
2003 struct image_params
*imgp
)
2009 struct cs_blob
*blob
;
2011 vm_size_t blob_size
;
2016 if (lcp
->cmdsize
!= sizeof (struct linkedit_data_command
) ||
2017 lcp
->dataoff
+ lcp
->datasize
> macho_size
) {
2018 ret
= LOAD_BADMACHO
;
2022 blob
= ubc_cs_blob_get(vp
, cputype
, macho_offset
);
2024 /* we already have a blob for this vnode and cputype */
2025 if (blob
->csb_cpu_type
== cputype
&&
2026 blob
->csb_base_offset
== macho_offset
) {
2027 /* it matches the blob we want here, lets verify the version */
2028 if(0 != ubc_cs_generation_check(vp
)) {
2029 if (0 != ubc_cs_blob_revalidate(vp
, blob
, imgp
, 0)) {
2030 ret
= LOAD_FAILURE
; /* set error same as from ubc_cs_blob_add */
2036 /* the blob has changed for this vnode: fail ! */
2037 ret
= LOAD_BADMACHO
;
2042 blob_size
= lcp
->datasize
;
2043 kr
= ubc_cs_blob_allocate(&addr
, &blob_size
);
2044 if (kr
!= KERN_SUCCESS
) {
2050 error
= vn_rdwr(UIO_READ
,
2054 macho_offset
+ lcp
->dataoff
,
2060 if (error
|| resid
!= 0) {
2065 if (ubc_cs_blob_add(vp
,
2074 ubc_cs_blob_deallocate(addr
, blob_size
);
2079 /* ubc_cs_blob_add() has consumed "addr" */
2083 #if CHECK_CS_VALIDATION_BITMAP
2084 ubc_cs_validation_bitmap_allocate( vp
);
2089 if (ret
== LOAD_SUCCESS
) {
2091 panic("sucess, but no blob!");
2093 result
->csflags
|= blob
->csb_flags
;
2094 result
->platform_binary
= blob
->csb_platform_binary
;
2095 result
->cs_end_offset
= blob
->csb_end_offset
;
2098 ubc_cs_blob_deallocate(addr
, blob_size
);
2106 #if CONFIG_CODE_DECRYPTION
2108 static load_return_t
2110 struct encryption_info_command
*eip
,
2117 cpu_subtype_t cpusubtype
)
2120 pager_crypt_info_t crypt_info
;
2121 const char * cryptname
= 0;
2125 struct segment_command_64
*seg64
;
2126 struct segment_command
*seg32
;
2127 vm_map_offset_t map_offset
, map_size
;
2128 vm_object_offset_t crypto_backing_offset
;
2131 if (eip
->cmdsize
< sizeof(*eip
)) return LOAD_BADMACHO
;
2133 switch(eip
->cryptid
) {
2135 /* not encrypted, just an empty load command */
2136 return LOAD_SUCCESS
;
2138 cryptname
="com.apple.unfree";
2141 /* some random cryptid that you could manually put into
2142 * your binary if you want NULL */
2143 cryptname
="com.apple.null";
2146 return LOAD_BADMACHO
;
2149 if (map
== VM_MAP_NULL
) return (LOAD_SUCCESS
);
2150 if (NULL
== text_crypter_create
) return LOAD_FAILURE
;
2152 MALLOC_ZONE(vpath
, char *, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
2153 if(vpath
== NULL
) return LOAD_FAILURE
;
2156 error
= vn_getpath(vp
, vpath
, &len
);
2158 FREE_ZONE(vpath
, MAXPATHLEN
, M_NAMEI
);
2159 return LOAD_FAILURE
;
2162 /* set up decrypter first */
2163 crypt_file_data_t crypt_data
= {
2166 .cpusubtype
= cpusubtype
};
2167 kr
=text_crypter_create(&crypt_info
, cryptname
, (void*)&crypt_data
);
2168 #if VM_MAP_DEBUG_APPLE_PROTECT
2169 if (vm_map_debug_apple_protect
) {
2172 printf("APPLE_PROTECT: %d[%s] map %p %s(%s) -> 0x%x\n",
2173 p
->p_pid
, p
->p_comm
, map
, __FUNCTION__
, vpath
, kr
);
2175 #endif /* VM_MAP_DEBUG_APPLE_PROTECT */
2176 FREE_ZONE(vpath
, MAXPATHLEN
, M_NAMEI
);
2179 printf("set_code_unprotect: unable to create decrypter %s, kr=%d\n",
2181 if (kr
== kIOReturnNotPrivileged
) {
2182 /* text encryption returned decryption failure */
2183 return(LOAD_DECRYPTFAIL
);
2185 return LOAD_RESOURCE
;
2188 /* this is terrible, but we have to rescan the load commands to find the
2189 * virtual address of this encrypted stuff. This code is gonna look like
2190 * the dyld source one day... */
2191 struct mach_header
*header
= (struct mach_header
*)addr
;
2192 size_t mach_header_sz
= sizeof(struct mach_header
);
2193 if (header
->magic
== MH_MAGIC_64
||
2194 header
->magic
== MH_CIGAM_64
) {
2195 mach_header_sz
= sizeof(struct mach_header_64
);
2197 offset
= mach_header_sz
;
2198 uint32_t ncmds
= header
->ncmds
;
2201 * Get a pointer to the command.
2203 struct load_command
*lcp
= (struct load_command
*)(addr
+ offset
);
2204 offset
+= lcp
->cmdsize
;
2208 seg64
= (struct segment_command_64
*)lcp
;
2209 if ((seg64
->fileoff
<= eip
->cryptoff
) &&
2210 (seg64
->fileoff
+seg64
->filesize
>=
2211 eip
->cryptoff
+eip
->cryptsize
)) {
2212 map_offset
= seg64
->vmaddr
+ eip
->cryptoff
- seg64
->fileoff
+ slide
;
2213 map_size
= eip
->cryptsize
;
2214 crypto_backing_offset
= macho_offset
+ eip
->cryptoff
;
2218 seg32
= (struct segment_command
*)lcp
;
2219 if ((seg32
->fileoff
<= eip
->cryptoff
) &&
2220 (seg32
->fileoff
+seg32
->filesize
>=
2221 eip
->cryptoff
+eip
->cryptsize
)) {
2222 map_offset
= seg32
->vmaddr
+ eip
->cryptoff
- seg32
->fileoff
+ slide
;
2223 map_size
= eip
->cryptsize
;
2224 crypto_backing_offset
= macho_offset
+ eip
->cryptoff
;
2230 /* if we get here, did not find anything */
2231 return LOAD_BADMACHO
;
2234 /* now remap using the decrypter */
2235 MACHO_PRINTF(("+++ set_code_unprotect: vm[0x%llx:0x%llx]\n",
2236 (uint64_t) map_offset
,
2237 (uint64_t) (map_offset
+map_size
)));
2238 kr
= vm_map_apple_protected(map
,
2240 map_offset
+map_size
,
2241 crypto_backing_offset
,
2244 printf("set_code_unprotect(): mapping failed with %x\n", kr
);
2245 return LOAD_PROTECT
;
2248 return LOAD_SUCCESS
;
2254 * This routine exists to support the load_dylinker().
2256 * This routine has its own, separate, understanding of the FAT file format,
2257 * which is terrifically unfortunate.
2264 struct mach_header
*mach_header
,
2267 struct macho_data
*data
,
2272 vfs_context_t ctx
= vfs_context_current();
2273 proc_t p
= vfs_context_proc(ctx
);
2274 kauth_cred_t kerncred
;
2275 struct nameidata
*ndp
= &data
->__nid
;
2277 struct fat_arch fat_arch
;
2280 union macho_vnode_header
*header
= &data
->__header
;
2281 off_t fsize
= (off_t
)0;
2284 * Capture the kernel credential for use in the actual read of the
2285 * file, since the user doing the execution may have execute rights
2286 * but not read rights, but to exec something, we have to either map
2287 * or read it into the new process address space, which requires
2288 * read rights. This is to deal with lack of common credential
2289 * serialization code which would treat NOCRED as "serialize 'root'".
2291 kerncred
= vfs_context_ucred(vfs_context_kernel());
2293 /* init the namei data to point the file user's program name */
2294 NDINIT(ndp
, LOOKUP
, OP_OPEN
, FOLLOW
| LOCKLEAF
, UIO_SYSSPACE
, CAST_USER_ADDR_T(path
), ctx
);
2296 if ((error
= namei(ndp
)) != 0) {
2297 if (error
== ENOENT
) {
2298 error
= LOAD_ENOENT
;
2300 error
= LOAD_FAILURE
;
2307 /* check for regular file */
2308 if (vp
->v_type
!= VREG
) {
2309 error
= LOAD_PROTECT
;
2314 if ((error
= vnode_size(vp
, &fsize
, ctx
)) != 0) {
2315 error
= LOAD_FAILURE
;
2319 /* Check mount point */
2320 if (vp
->v_mount
->mnt_flag
& MNT_NOEXEC
) {
2321 error
= LOAD_PROTECT
;
2326 if ((error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_EXECUTE
| KAUTH_VNODE_READ_DATA
, ctx
)) != 0) {
2327 error
= LOAD_PROTECT
;
2331 /* try to open it */
2332 if ((error
= VNOP_OPEN(vp
, FREAD
, ctx
)) != 0) {
2333 error
= LOAD_PROTECT
;
2337 if ((error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)header
, sizeof (*header
), 0,
2338 UIO_SYSSPACE
, IO_NODELOCKED
, kerncred
, &resid
, p
)) != 0) {
2339 error
= LOAD_IOERROR
;
2344 error
= LOAD_BADMACHO
;
2348 if (header
->mach_header
.magic
== MH_MAGIC
||
2349 header
->mach_header
.magic
== MH_MAGIC_64
) {
2351 } else if (OSSwapBigToHostInt32(header
->fat_header
.magic
) == FAT_MAGIC
) {
2354 error
= LOAD_BADMACHO
;
2360 error
= fatfile_validate_fatarches((vm_offset_t
)(&header
->fat_header
),
2362 if (error
!= LOAD_SUCCESS
) {
2366 /* Look up our architecture in the fat file. */
2367 error
= fatfile_getarch_with_bits(archbits
,
2368 (vm_offset_t
)(&header
->fat_header
), sizeof(*header
), &fat_arch
);
2369 if (error
!= LOAD_SUCCESS
)
2372 /* Read the Mach-O header out of it */
2373 error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)&header
->mach_header
,
2374 sizeof (header
->mach_header
), fat_arch
.offset
,
2375 UIO_SYSSPACE
, IO_NODELOCKED
, kerncred
, &resid
, p
);
2377 error
= LOAD_IOERROR
;
2382 error
= LOAD_BADMACHO
;
2386 /* Is this really a Mach-O? */
2387 if (header
->mach_header
.magic
!= MH_MAGIC
&&
2388 header
->mach_header
.magic
!= MH_MAGIC_64
) {
2389 error
= LOAD_BADMACHO
;
2393 *file_offset
= fat_arch
.offset
;
2394 *macho_size
= fat_arch
.size
;
2397 * Force get_macho_vnode() to fail if the architecture bits
2398 * do not match the expected architecture bits. This in
2399 * turn causes load_dylinker() to fail for the same reason,
2400 * so it ensures the dynamic linker and the binary are in
2401 * lock-step. This is potentially bad, if we ever add to
2402 * the CPU_ARCH_* bits any bits that are desirable but not
2403 * required, since the dynamic linker might work, but we will
2404 * refuse to load it because of this check.
2406 if ((cpu_type_t
)(header
->mach_header
.cputype
& CPU_ARCH_MASK
) != archbits
) {
2407 error
= LOAD_BADARCH
;
2412 *macho_size
= fsize
;
2415 *mach_header
= header
->mach_header
;
2418 ubc_setsize(vp
, fsize
);
2422 (void) VNOP_CLOSE(vp
, FREAD
, ctx
);