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>
53 #include <mach/mach_types.h>
54 #include <mach/vm_map.h> /* vm_allocate() */
55 #include <mach/mach_vm.h> /* mach_vm_allocate() */
56 #include <mach/vm_statistics.h>
57 #include <mach/task.h>
58 #include <mach/thread_act.h>
60 #include <machine/vmparam.h>
61 #include <machine/exec.h>
62 #include <machine/pal_routines.h>
64 #include <kern/kern_types.h>
65 #include <kern/cpu_number.h>
66 #include <kern/mach_loader.h>
67 #include <kern/mach_fat.h>
68 #include <kern/kalloc.h>
69 #include <kern/task.h>
70 #include <kern/thread.h>
71 #include <kern/page_decrypt.h>
73 #include <mach-o/fat.h>
74 #include <mach-o/loader.h>
77 #include <vm/vm_map.h>
78 #include <vm/vm_kern.h>
79 #include <vm/vm_pager.h>
80 #include <vm/vnode_pager.h>
81 #include <vm/vm_protos.h>
84 * XXX vm/pmap.h should not treat these prototypes as MACH_KERNEL_PRIVATE
85 * when KERNEL is defined.
87 extern pmap_t
pmap_create(ledger_t ledger
, vm_map_size_t size
,
90 /* XXX should have prototypes in a shared header file */
91 extern int get_map_nentries(vm_map_t
);
93 extern kern_return_t
memory_object_signed(memory_object_control_t control
,
96 /* An empty load_result_t */
97 static load_result_t load_result_null
= {
98 .mach_header
= MACH_VM_MIN_ADDRESS
,
99 .entry_point
= MACH_VM_MIN_ADDRESS
,
100 .user_stack
= MACH_VM_MIN_ADDRESS
,
101 .user_stack_size
= 0,
102 .all_image_info_addr
= MACH_VM_MIN_ADDRESS
,
103 .all_image_info_size
= 0,
107 .needs_dynlinker
= 0,
108 .prog_allocated_stack
= 0,
109 .prog_stack_size
= 0,
113 .min_vm_addr
= MACH_VM_MAX_ADDRESS
,
114 .max_vm_addr
= MACH_VM_MIN_ADDRESS
118 * Prototypes of static functions.
125 struct mach_header
*header
,
130 load_result_t
*result
135 struct load_command
*lcp
,
143 load_result_t
*result
148 struct linkedit_data_command
*lcp
,
153 load_result_t
*result
);
155 #if CONFIG_CODE_DECRYPTION
158 struct encryption_info_command
*lcp
,
168 struct entry_point_command
*epc
,
171 load_result_t
*result
176 struct thread_command
*tcp
,
179 load_result_t
*result
194 mach_vm_offset_t
*user_stack
,
203 mach_vm_offset_t
*entry_point
208 struct dylinker_command
*lcp
,
214 load_result_t
*result
223 struct mach_header
*mach_header
,
226 struct macho_data
*macho_data
,
231 widen_segment_command(const struct segment_command
*scp32
,
232 struct segment_command_64
*scp
)
234 scp
->cmd
= scp32
->cmd
;
235 scp
->cmdsize
= scp32
->cmdsize
;
236 bcopy(scp32
->segname
, scp
->segname
, sizeof(scp
->segname
));
237 scp
->vmaddr
= scp32
->vmaddr
;
238 scp
->vmsize
= scp32
->vmsize
;
239 scp
->fileoff
= scp32
->fileoff
;
240 scp
->filesize
= scp32
->filesize
;
241 scp
->maxprot
= scp32
->maxprot
;
242 scp
->initprot
= scp32
->initprot
;
243 scp
->nsects
= scp32
->nsects
;
244 scp
->flags
= scp32
->flags
;
248 note_all_image_info_section(const struct segment_command_64
*scp
,
249 boolean_t is64
, size_t section_size
, const void *sections
,
250 int64_t slide
, load_result_t
*result
)
254 struct section_64 s64
;
258 if (strncmp(scp
->segname
, "__DATA", sizeof(scp
->segname
)) != 0)
260 for (i
= 0; i
< scp
->nsects
; ++i
) {
261 sectionp
= (const void *)
262 ((const char *)sections
+ section_size
* i
);
263 if (0 == strncmp(sectionp
->s64
.sectname
, "__all_image_info",
264 sizeof(sectionp
->s64
.sectname
))) {
265 result
->all_image_info_addr
=
266 is64
? sectionp
->s64
.addr
: sectionp
->s32
.addr
;
267 result
->all_image_info_addr
+= slide
;
268 result
->all_image_info_size
=
269 is64
? sectionp
->s64
.size
: sectionp
->s32
.size
;
277 struct image_params
*imgp
,
278 struct mach_header
*header
,
281 load_result_t
*result
284 struct vnode
*vp
= imgp
->ip_vp
;
285 off_t file_offset
= imgp
->ip_arch_offset
;
286 off_t macho_size
= imgp
->ip_arch_size
;
287 off_t file_size
= imgp
->ip_vattr
->va_data_size
;
289 pmap_t pmap
= 0; /* protected by create_map */
292 task_t old_task
= TASK_NULL
; /* protected by create_map */
293 load_result_t myresult
;
295 boolean_t create_map
= FALSE
;
296 int spawn
= (imgp
->ip_flags
& IMGPF_SPAWN
);
297 task_t task
= current_task();
298 proc_t p
= current_proc();
299 mach_vm_offset_t aslr_offset
= 0;
302 if (macho_size
> file_size
) {
303 return(LOAD_BADMACHO
);
306 if (new_map
== VM_MAP_NULL
) {
308 old_task
= current_task();
312 * If we are spawning, we have created backing objects for the process
313 * already, which include non-lazily creating the task map. So we
314 * are going to switch out the task map with one appropriate for the
315 * bitness of the image being loaded.
319 old_task
= get_threadtask(thread
);
323 pmap
= pmap_create(get_task_ledger(task
), (vm_map_size_t
) 0,
324 (imgp
->ip_flags
& IMGPF_IS_64BIT
));
325 pal_switch_pmap(thread
, pmap
, imgp
->ip_flags
& IMGPF_IS_64BIT
);
326 map
= vm_map_create(pmap
,
328 vm_compute_max_offset((imgp
->ip_flags
& IMGPF_IS_64BIT
)),
334 #ifndef CONFIG_ENFORCE_SIGNED_CODE
335 /* This turns off faulting for executable pages, which allows to
336 * circumvent Code Signing Enforcement */
337 if ( (header
->flags
& MH_ALLOW_STACK_EXECUTION
) )
338 vm_map_disable_NX(map
);
341 /* Forcibly disallow execution from data pages on even if the arch
342 * normally permits it. */
343 if ((header
->flags
& MH_NO_HEAP_EXECUTION
) && !(imgp
->ip_flags
& IMGPF_ALLOW_DATA_EXEC
))
344 vm_map_disallow_data_exec(map
);
347 * Compute a random offset for ASLR.
349 if (!(imgp
->ip_flags
& IMGPF_DISABLE_ASLR
)) {
350 aslr_offset
= random();
351 aslr_offset
%= 1 << ((imgp
->ip_flags
& IMGPF_IS_64BIT
) ? 16 : 8);
352 aslr_offset
<<= PAGE_SHIFT
;
358 *result
= load_result_null
;
360 lret
= parse_machfile(vp
, map
, thread
, header
, file_offset
, macho_size
,
361 0, (int64_t)aslr_offset
, result
);
363 if (lret
!= LOAD_SUCCESS
) {
365 vm_map_deallocate(map
); /* will lose pmap reference too */
372 * Check to see if the page zero is enforced by the map->min_offset.
374 if (vm_map_has_hard_pagezero(map
, 0x1000) == FALSE
) {
376 vm_map_deallocate(map
); /* will lose pmap reference too */
378 printf("Cannot enforce a hard page-zero for %s\n", imgp
->ip_strings
);
379 psignal(vfs_context_proc(imgp
->ip_vfs_context
), SIGKILL
);
380 return (LOAD_BADMACHO
);
384 * For 64-bit users, check for presence of a 4GB page zero
385 * which will enable the kernel to share the user's address space
386 * and hence avoid TLB flushes on kernel entry/exit
389 if ((imgp
->ip_flags
& IMGPF_IS_64BIT
) &&
390 vm_map_has_4GB_pagezero(map
)) {
391 vm_map_set_4GB_pagezero(map
);
397 * Swap the new map for the old, which consumes our new map
398 * reference but each leaves us responsible for the old_map reference.
399 * That lets us get off the pmap associated with it, and
400 * then we can release it.
405 * If this is an exec, then we are going to destroy the old
406 * task, and it's correct to halt it; if it's spawn, the
407 * task is not yet running, and it makes no sense.
411 * Mark the task as halting and start the other
412 * threads towards terminating themselves. Then
413 * make sure any threads waiting for a process
414 * transition get informed that we are committed to
415 * this transition, and then finally complete the
416 * task halting (wait for threads and then cleanup
419 * NOTE: task_start_halt() makes sure that no new
420 * threads are created in the task during the transition.
421 * We need to mark the workqueue as exiting before we
422 * wait for threads to terminate (at the end of which
423 * we no longer have a prohibition on thread creation).
425 * Finally, clean up any lingering workqueue data structures
426 * that may have been left behind by the workqueue threads
427 * as they exited (and then clean up the work queue itself).
429 kret
= task_start_halt(task
);
430 if (kret
!= KERN_SUCCESS
) {
433 proc_transcommit(p
, 0);
434 workqueue_mark_exiting(p
);
435 task_complete_halt(task
);
438 old_map
= swap_task_map(old_task
, thread
, map
, !spawn
);
439 vm_map_clear_4GB_pagezero(old_map
);
440 vm_map_deallocate(old_map
);
442 return(LOAD_SUCCESS
);
446 * The file size of a mach-o file is limited to 32 bits; this is because
447 * this is the limit on the kalloc() of enough bytes for a mach_header and
448 * the contents of its sizeofcmds, which is currently constrained to 32
449 * bits in the file format itself. We read into the kernel buffer the
450 * commands section, and then parse it in order to parse the mach-o file
451 * format load_command segment(s). We are only interested in a subset of
452 * the total set of possible commands. If "map"==VM_MAP_NULL or
453 * "thread"==THREAD_NULL, do not make permament VM modifications,
454 * just preflight the parse.
462 struct mach_header
*header
,
467 load_result_t
*result
471 struct load_command
*lcp
;
472 struct dylinker_command
*dlp
= 0;
473 struct uuid_command
*uulp
= 0;
474 integer_t dlarchbits
= 0;
476 load_return_t ret
= LOAD_SUCCESS
;
479 vm_size_t size
,kl_size
;
481 size_t oldoffset
; /* for overflow check */
483 proc_t p
= current_proc(); /* XXXX */
486 size_t mach_header_sz
= sizeof(struct mach_header
);
488 boolean_t got_code_signatures
= FALSE
;
491 if (header
->magic
== MH_MAGIC_64
||
492 header
->magic
== MH_CIGAM_64
) {
493 mach_header_sz
= sizeof(struct mach_header_64
);
497 * Break infinite recursion
500 return(LOAD_FAILURE
);
506 * Check to see if right machine type.
508 if (((cpu_type_t
)(header
->cputype
& ~CPU_ARCH_MASK
) != cpu_type()) ||
509 !grade_binary(header
->cputype
,
510 header
->cpusubtype
& ~CPU_SUBTYPE_MASK
))
511 return(LOAD_BADARCH
);
513 abi64
= ((header
->cputype
& CPU_ARCH_ABI64
) == CPU_ARCH_ABI64
);
515 switch (header
->filetype
) {
521 return (LOAD_FAILURE
);
528 return (LOAD_FAILURE
);
534 return (LOAD_FAILURE
);
539 return (LOAD_FAILURE
);
543 * Get the pager for the file.
545 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
548 * Map portion that must be accessible directly into
551 if ((off_t
)(mach_header_sz
+ header
->sizeofcmds
) > macho_size
)
552 return(LOAD_BADMACHO
);
555 * Round size of Mach-O commands up to page boundry.
557 size
= round_page(mach_header_sz
+ header
->sizeofcmds
);
559 return(LOAD_BADMACHO
);
562 * Map the load commands into kernel memory.
566 kl_addr
= kalloc(size
);
567 addr
= (caddr_t
)kl_addr
;
569 return(LOAD_NOSPACE
);
571 error
= vn_rdwr(UIO_READ
, vp
, addr
, size
, file_offset
,
572 UIO_SYSSPACE
, 0, kauth_cred_get(), &resid
, p
);
575 kfree(kl_addr
, kl_size
);
576 return(LOAD_IOERROR
);
580 * For PIE and dyld, slide everything by the ASLR offset.
582 if ((header
->flags
& MH_PIE
) || (header
->filetype
== MH_DYLINKER
)) {
587 * Scan through the commands, processing each one as necessary.
589 for (pass
= 1; pass
<= 3; pass
++) {
592 * Check that the entry point is contained in an executable segments
594 if ((pass
== 3) && (result
->validentry
== 0)) {
595 thread_state_initialize(thread
);
601 * Loop through each of the load_commands indicated by the
602 * Mach-O header; if an absurd value is provided, we just
603 * run off the end of the reserved section by incrementing
604 * the offset too far, so we are implicitly fail-safe.
606 offset
= mach_header_sz
;
607 ncmds
= header
->ncmds
;
611 * Get a pointer to the command.
613 lcp
= (struct load_command
*)(addr
+ offset
);
615 offset
+= lcp
->cmdsize
;
618 * Perform prevalidation of the struct load_command
619 * before we attempt to use its contents. Invalid
620 * values are ones which result in an overflow, or
621 * which can not possibly be valid commands, or which
622 * straddle or exist past the reserved section at the
623 * start of the image.
625 if (oldoffset
> offset
||
626 lcp
->cmdsize
< sizeof(struct load_command
) ||
627 offset
> header
->sizeofcmds
+ mach_header_sz
) {
633 * Act on struct load_command's for which kernel
634 * intervention is required.
641 ret
= load_segment(lcp
,
654 ret
= load_unixthread(
655 (struct thread_command
*) lcp
,
666 (struct entry_point_command
*) lcp
,
671 case LC_LOAD_DYLINKER
:
674 if ((depth
== 1) && (dlp
== 0)) {
675 dlp
= (struct dylinker_command
*)lcp
;
676 dlarchbits
= (header
->cputype
& CPU_ARCH_MASK
);
682 if (pass
== 1 && depth
== 1) {
683 uulp
= (struct uuid_command
*)lcp
;
684 memcpy(&result
->uuid
[0], &uulp
->uuid
[0], sizeof(result
->uuid
));
687 case LC_CODE_SIGNATURE
:
692 load signatures & store in uip
693 set VM object "signed_pages"
695 ret
= load_code_signature(
696 (struct linkedit_data_command
*) lcp
,
701 (depth
== 1) ? result
: NULL
);
702 if (ret
!= LOAD_SUCCESS
) {
703 printf("proc %d: load code signature error %d "
705 p
->p_pid
, ret
, vp
->v_name
);
706 ret
= LOAD_SUCCESS
; /* ignore error */
708 got_code_signatures
= TRUE
;
711 #if CONFIG_CODE_DECRYPTION
712 case LC_ENCRYPTION_INFO
:
715 ret
= set_code_unprotect(
716 (struct encryption_info_command
*) lcp
,
717 addr
, map
, slide
, vp
);
718 if (ret
!= LOAD_SUCCESS
) {
719 printf("proc %d: set_code_unprotect() error %d "
721 p
->p_pid
, ret
, vp
->v_name
);
722 /* Don't let the app run if it's
723 * encrypted but we failed to set up the
730 /* Other commands are ignored by the kernel */
734 if (ret
!= LOAD_SUCCESS
)
737 if (ret
!= LOAD_SUCCESS
)
740 if (ret
== LOAD_SUCCESS
) {
741 if (! got_code_signatures
) {
742 struct cs_blob
*blob
;
743 /* no embedded signatures: look for detached ones */
744 blob
= ubc_cs_blob_get(vp
, -1, file_offset
);
746 /* get flags to be applied to the process */
747 result
->csflags
|= blob
->csb_flags
;
751 /* Make sure if we need dyld, we got it */
752 if (result
->needs_dynlinker
&& !dlp
) {
756 if ((ret
== LOAD_SUCCESS
) && (dlp
!= 0)) {
757 /* load the dylinker, and always slide it by the ASLR
758 * offset regardless of PIE */
759 ret
= load_dylinker(dlp
, dlarchbits
, map
, thread
, depth
, aslr_offset
, result
);
762 if((ret
== LOAD_SUCCESS
) && (depth
== 1)) {
763 if (result
->thread_count
== 0) {
770 kfree(kl_addr
, kl_size
);
775 #if CONFIG_CODE_DECRYPTION
777 #define APPLE_UNPROTECTED_HEADER_SIZE (3 * PAGE_SIZE_64)
786 vm_map_offset_t map_addr
,
787 vm_map_size_t map_size
)
792 * The first APPLE_UNPROTECTED_HEADER_SIZE bytes (from offset 0 of
793 * this part of a Universal binary) are not protected...
794 * The rest needs to be "transformed".
796 if (file_off
<= APPLE_UNPROTECTED_HEADER_SIZE
&&
797 file_off
+ file_size
<= APPLE_UNPROTECTED_HEADER_SIZE
) {
798 /* it's all unprotected, nothing to do... */
801 if (file_off
<= APPLE_UNPROTECTED_HEADER_SIZE
) {
803 * We start mapping in the unprotected area.
804 * Skip the unprotected part...
806 vm_map_offset_t delta
;
808 delta
= APPLE_UNPROTECTED_HEADER_SIZE
;
813 /* ... transform the rest of the mapping. */
814 struct pager_crypt_info crypt_info
;
815 crypt_info
.page_decrypt
= dsmos_page_transform
;
816 crypt_info
.crypt_ops
= NULL
;
817 crypt_info
.crypt_end
= NULL
;
818 #pragma unused(vp, macho_offset)
819 crypt_info
.crypt_ops
= (void *)0x2e69cf40;
820 kr
= vm_map_apple_protected(map
,
826 if (kr
!= KERN_SUCCESS
) {
831 #else /* CONFIG_CODE_DECRYPTION */
834 __unused
uint64_t file_off
,
835 __unused
uint64_t file_size
,
836 __unused
struct vnode
*vp
,
837 __unused off_t macho_offset
,
838 __unused vm_map_t map
,
839 __unused vm_map_offset_t map_addr
,
840 __unused vm_map_size_t map_size
)
844 #endif /* CONFIG_CODE_DECRYPTION */
849 struct load_command
*lcp
,
857 load_result_t
*result
860 struct segment_command_64 segment_command
, *scp
;
862 vm_map_offset_t map_addr
, map_offset
;
863 vm_map_size_t map_size
, seg_size
, delta_size
;
866 size_t segment_command_size
, total_section_size
,
868 boolean_t prohibit_pagezero_mapping
= FALSE
;
870 if (LC_SEGMENT_64
== lcp
->cmd
) {
871 segment_command_size
= sizeof(struct segment_command_64
);
872 single_section_size
= sizeof(struct section_64
);
874 segment_command_size
= sizeof(struct segment_command
);
875 single_section_size
= sizeof(struct section
);
877 if (lcp
->cmdsize
< segment_command_size
)
878 return (LOAD_BADMACHO
);
879 total_section_size
= lcp
->cmdsize
- segment_command_size
;
881 if (LC_SEGMENT_64
== lcp
->cmd
)
882 scp
= (struct segment_command_64
*)lcp
;
884 scp
= &segment_command
;
885 widen_segment_command((struct segment_command
*)lcp
, scp
);
889 * Make sure what we get from the file is really ours (as specified
892 if (scp
->fileoff
+ scp
->filesize
< scp
->fileoff
||
893 scp
->fileoff
+ scp
->filesize
> (uint64_t)macho_size
)
894 return (LOAD_BADMACHO
);
896 * Ensure that the number of sections specified would fit
897 * within the load command size.
899 if (total_section_size
/ single_section_size
< scp
->nsects
)
900 return (LOAD_BADMACHO
);
902 * Make sure the segment is page-aligned in the file.
904 if ((scp
->fileoff
& PAGE_MASK_64
) != 0)
905 return (LOAD_BADMACHO
);
908 * Round sizes to page size.
910 seg_size
= round_page_64(scp
->vmsize
);
911 map_size
= round_page_64(scp
->filesize
);
912 map_addr
= trunc_page_64(scp
->vmaddr
); /* JVXXX note that in XNU TOT this is round instead of trunc for 64 bits */
914 return (KERN_SUCCESS
);
918 (scp
->initprot
& VM_PROT_ALL
) == VM_PROT_NONE
&&
919 (scp
->maxprot
& VM_PROT_ALL
) == VM_PROT_NONE
) {
921 * For PIE, extend page zero rather than moving it. Extending
922 * page zero keeps early allocations from falling predictably
923 * between the end of page zero and the beginning of the first
929 prohibit_pagezero_mapping
= TRUE
;
931 /* XXX (4596982) this interferes with Rosetta, so limit to 64-bit tasks */
932 if (scp
->cmd
== LC_SEGMENT_64
) {
933 prohibit_pagezero_mapping
= TRUE
;
936 if (prohibit_pagezero_mapping
) {
938 * This is a "page zero" segment: it starts at address 0,
939 * is not mapped from the binary file and is not accessible.
940 * User-space should never be able to access that memory, so
941 * make it completely off limits by raising the VM map's
944 ret
= vm_map_raise_min_offset(map
, seg_size
);
945 if (ret
!= KERN_SUCCESS
) {
946 return (LOAD_FAILURE
);
948 return (LOAD_SUCCESS
);
952 /* If a non-zero slide was specified by the caller, apply now */
955 if (map_addr
< result
->min_vm_addr
)
956 result
->min_vm_addr
= map_addr
;
957 if (map_addr
+seg_size
> result
->max_vm_addr
)
958 result
->max_vm_addr
= map_addr
+seg_size
;
960 if (map
== VM_MAP_NULL
)
961 return (LOAD_SUCCESS
);
963 map_offset
= pager_offset
+ scp
->fileoff
; /* limited to 32 bits */
966 initprot
= (scp
->initprot
) & VM_PROT_ALL
;
967 maxprot
= (scp
->maxprot
) & VM_PROT_ALL
;
969 * Map a copy of the file into the address space.
971 ret
= vm_map_enter_mem_object_control(map
,
972 &map_addr
, map_size
, (mach_vm_offset_t
)0,
973 VM_FLAGS_FIXED
, control
, map_offset
, TRUE
,
976 if (ret
!= KERN_SUCCESS
)
977 return (LOAD_NOSPACE
);
980 * If the file didn't end on a page boundary,
981 * we need to zero the leftover.
983 delta_size
= map_size
- scp
->filesize
;
985 if (delta_size
> 0) {
986 mach_vm_offset_t tmp
;
988 ret
= mach_vm_allocate(kernel_map
, &tmp
, delta_size
, VM_FLAGS_ANYWHERE
);
989 if (ret
!= KERN_SUCCESS
)
990 return(LOAD_RESOURCE
);
992 if (copyout(tmp
, map_addr
+ scp
->filesize
,
994 (void) mach_vm_deallocate(
995 kernel_map
, tmp
, delta_size
);
996 return (LOAD_FAILURE
);
999 (void) mach_vm_deallocate(kernel_map
, tmp
, delta_size
);
1005 * If the virtual size of the segment is greater
1006 * than the size from the file, we need to allocate
1007 * zero fill memory for the rest.
1009 delta_size
= seg_size
- map_size
;
1010 if (delta_size
> 0) {
1011 mach_vm_offset_t tmp
= map_addr
+ map_size
;
1013 ret
= mach_vm_map(map
, &tmp
, delta_size
, 0, VM_FLAGS_FIXED
,
1015 scp
->initprot
, scp
->maxprot
,
1016 VM_INHERIT_DEFAULT
);
1017 if (ret
!= KERN_SUCCESS
)
1018 return(LOAD_NOSPACE
);
1021 if ( (scp
->fileoff
== 0) && (scp
->filesize
!= 0) )
1022 result
->mach_header
= map_addr
;
1024 if (scp
->flags
& SG_PROTECTED_VERSION_1
) {
1025 ret
= unprotect_segment(scp
->fileoff
,
1035 if (LOAD_SUCCESS
== ret
&& filetype
== MH_DYLINKER
&&
1036 result
->all_image_info_addr
== MACH_VM_MIN_ADDRESS
)
1037 note_all_image_info_section(scp
,
1038 LC_SEGMENT_64
== lcp
->cmd
, single_section_size
,
1039 (const char *)lcp
+ segment_command_size
, slide
, result
);
1041 if ((result
->entry_point
>= map_addr
) && (result
->entry_point
< (map_addr
+ map_size
)))
1042 result
->validentry
= 1;
1052 struct entry_point_command
*epc
,
1055 load_result_t
*result
1058 mach_vm_offset_t addr
;
1061 if (epc
->cmdsize
< sizeof(*epc
))
1062 return (LOAD_BADMACHO
);
1063 if (result
->thread_count
!= 0) {
1064 printf("load_main: already have a thread!");
1065 return (LOAD_FAILURE
);
1068 if (thread
== THREAD_NULL
)
1069 return (LOAD_SUCCESS
);
1071 /* LC_MAIN specifies stack size but not location */
1072 if (epc
->stacksize
) {
1073 result
->prog_stack_size
= 1;
1074 result
->user_stack_size
= epc
->stacksize
;
1076 result
->prog_stack_size
= 0;
1077 result
->user_stack_size
= MAXSSIZ
;
1079 result
->prog_allocated_stack
= 0;
1081 /* use default location for stack */
1082 ret
= thread_userstackdefault(thread
, &addr
);
1083 if (ret
!= KERN_SUCCESS
)
1084 return(LOAD_FAILURE
);
1086 /* The stack slides down from the default location */
1087 result
->user_stack
= addr
;
1088 result
->user_stack
-= slide
;
1090 /* kernel does *not* use entryoff from LC_MAIN. Dyld uses it. */
1091 result
->needs_dynlinker
= TRUE
;
1092 result
->validentry
= TRUE
;
1094 ret
= thread_state_initialize( thread
);
1095 if (ret
!= KERN_SUCCESS
) {
1096 return(LOAD_FAILURE
);
1099 result
->unixproc
= TRUE
;
1100 result
->thread_count
++;
1102 return(LOAD_SUCCESS
);
1109 struct thread_command
*tcp
,
1112 load_result_t
*result
1117 mach_vm_offset_t addr
;
1119 if (tcp
->cmdsize
< sizeof(*tcp
))
1120 return (LOAD_BADMACHO
);
1121 if (result
->thread_count
!= 0) {
1122 printf("load_unixthread: already have a thread!");
1123 return (LOAD_FAILURE
);
1126 if (thread
== THREAD_NULL
)
1127 return (LOAD_SUCCESS
);
1129 ret
= load_threadstack(thread
,
1130 (uint32_t *)(((vm_offset_t
)tcp
) +
1131 sizeof(struct thread_command
)),
1132 tcp
->cmdsize
- sizeof(struct thread_command
),
1135 if (ret
!= LOAD_SUCCESS
)
1138 /* LC_UNIXTHREAD optionally specifies stack size and location */
1141 result
->prog_stack_size
= 0; /* unknown */
1142 result
->prog_allocated_stack
= 1;
1144 result
->prog_allocated_stack
= 0;
1145 result
->prog_stack_size
= 0;
1146 result
->user_stack_size
= MAXSSIZ
;
1149 /* The stack slides down from the default location */
1150 result
->user_stack
= addr
;
1151 result
->user_stack
-= slide
;
1153 ret
= load_threadentry(thread
,
1154 (uint32_t *)(((vm_offset_t
)tcp
) +
1155 sizeof(struct thread_command
)),
1156 tcp
->cmdsize
- sizeof(struct thread_command
),
1158 if (ret
!= LOAD_SUCCESS
)
1161 result
->entry_point
= addr
;
1162 result
->entry_point
+= slide
;
1164 ret
= load_threadstate(thread
,
1165 (uint32_t *)(((vm_offset_t
)tcp
) +
1166 sizeof(struct thread_command
)),
1167 tcp
->cmdsize
- sizeof(struct thread_command
));
1168 if (ret
!= LOAD_SUCCESS
)
1171 result
->unixproc
= TRUE
;
1172 result
->thread_count
++;
1174 return(LOAD_SUCCESS
);
1188 uint32_t thread_size
;
1190 ret
= thread_state_initialize( thread
);
1191 if (ret
!= KERN_SUCCESS
) {
1192 return(LOAD_FAILURE
);
1196 * Set the new thread state; iterate through the state flavors in
1199 while (total_size
> 0) {
1202 if (UINT32_MAX
-2 < size
||
1203 UINT32_MAX
/sizeof(uint32_t) < size
+2)
1204 return (LOAD_BADMACHO
);
1205 thread_size
= (size
+2)*sizeof(uint32_t);
1206 if (thread_size
> total_size
)
1207 return(LOAD_BADMACHO
);
1208 total_size
-= thread_size
;
1210 * Third argument is a kernel space pointer; it gets cast
1211 * to the appropriate type in machine_thread_set_state()
1212 * based on the value of flavor.
1214 ret
= thread_setstatus(thread
, flavor
, (thread_state_t
)ts
, size
);
1215 if (ret
!= KERN_SUCCESS
) {
1216 return(LOAD_FAILURE
);
1218 ts
+= size
; /* ts is a (uint32_t *) */
1220 return(LOAD_SUCCESS
);
1228 uint32_t total_size
,
1229 mach_vm_offset_t
*user_stack
,
1236 uint32_t stack_size
;
1238 while (total_size
> 0) {
1241 if (UINT32_MAX
-2 < size
||
1242 UINT32_MAX
/sizeof(uint32_t) < size
+2)
1243 return (LOAD_BADMACHO
);
1244 stack_size
= (size
+2)*sizeof(uint32_t);
1245 if (stack_size
> total_size
)
1246 return(LOAD_BADMACHO
);
1247 total_size
-= stack_size
;
1250 * Third argument is a kernel space pointer; it gets cast
1251 * to the appropriate type in thread_userstack() based on
1252 * the value of flavor.
1254 ret
= thread_userstack(thread
, flavor
, (thread_state_t
)ts
, size
, user_stack
, customstack
);
1255 if (ret
!= KERN_SUCCESS
) {
1256 return(LOAD_FAILURE
);
1258 ts
+= size
; /* ts is a (uint32_t *) */
1260 return(LOAD_SUCCESS
);
1268 uint32_t total_size
,
1269 mach_vm_offset_t
*entry_point
1275 uint32_t entry_size
;
1278 * Set the thread state.
1280 *entry_point
= MACH_VM_MIN_ADDRESS
;
1281 while (total_size
> 0) {
1284 if (UINT32_MAX
-2 < size
||
1285 UINT32_MAX
/sizeof(uint32_t) < size
+2)
1286 return (LOAD_BADMACHO
);
1287 entry_size
= (size
+2)*sizeof(uint32_t);
1288 if (entry_size
> total_size
)
1289 return(LOAD_BADMACHO
);
1290 total_size
-= entry_size
;
1292 * Third argument is a kernel space pointer; it gets cast
1293 * to the appropriate type in thread_entrypoint() based on
1294 * the value of flavor.
1296 ret
= thread_entrypoint(thread
, flavor
, (thread_state_t
)ts
, size
, entry_point
);
1297 if (ret
!= KERN_SUCCESS
) {
1298 return(LOAD_FAILURE
);
1300 ts
+= size
; /* ts is a (uint32_t *) */
1302 return(LOAD_SUCCESS
);
1306 struct nameidata __nid
;
1307 union macho_vnode_header
{
1308 struct mach_header mach_header
;
1309 struct fat_header fat_header
;
1314 static load_return_t
1316 struct dylinker_command
*lcp
,
1322 load_result_t
*result
1327 struct vnode
*vp
= NULLVP
; /* set by get_macho_vnode() */
1328 struct mach_header
*header
;
1329 off_t file_offset
= 0; /* set by get_macho_vnode() */
1330 off_t macho_size
= 0; /* set by get_macho_vnode() */
1331 load_result_t
*myresult
;
1333 struct macho_data
*macho_data
;
1335 struct mach_header __header
;
1336 load_result_t __myresult
;
1337 struct macho_data __macho_data
;
1340 if (lcp
->cmdsize
< sizeof(*lcp
))
1341 return (LOAD_BADMACHO
);
1343 name
= (char *)lcp
+ lcp
->name
.offset
;
1345 * Check for a proper null terminated string.
1349 if (p
>= (char *)lcp
+ lcp
->cmdsize
)
1350 return(LOAD_BADMACHO
);
1353 /* Allocate wad-of-data from heap to reduce excessively deep stacks */
1355 MALLOC(dyld_data
, void *, sizeof (*dyld_data
), M_TEMP
, M_WAITOK
);
1356 header
= &dyld_data
->__header
;
1357 myresult
= &dyld_data
->__myresult
;
1358 macho_data
= &dyld_data
->__macho_data
;
1360 ret
= get_macho_vnode(name
, archbits
, header
,
1361 &file_offset
, &macho_size
, macho_data
, &vp
);
1365 *myresult
= load_result_null
;
1368 * First try to map dyld in directly. This should work most of
1369 * the time since there shouldn't normally be something already
1370 * mapped to its address.
1373 ret
= parse_machfile(vp
, map
, thread
, header
, file_offset
,
1374 macho_size
, depth
, slide
, myresult
);
1377 * If it turned out something was in the way, then we'll take
1378 * take this longer path to preflight dyld's vm ranges, then
1379 * map it at a free location in the address space.
1382 if (ret
== LOAD_NOSPACE
) {
1383 mach_vm_offset_t dyl_start
, map_addr
;
1384 mach_vm_size_t dyl_length
;
1385 int64_t slide_amount
;
1387 *myresult
= load_result_null
;
1390 * Preflight parsing the Mach-O file with a NULL
1391 * map, which will return the ranges needed for a
1392 * subsequent map attempt (with a slide) in "myresult"
1394 ret
= parse_machfile(vp
, VM_MAP_NULL
, THREAD_NULL
, header
,
1395 file_offset
, macho_size
, depth
, 0 /* slide */, myresult
);
1397 if (ret
!= LOAD_SUCCESS
) {
1401 dyl_start
= myresult
->min_vm_addr
;
1402 dyl_length
= myresult
->max_vm_addr
- myresult
->min_vm_addr
;
1404 dyl_length
+= slide
;
1406 /* To find an appropriate load address, do a quick allocation */
1407 map_addr
= dyl_start
;
1408 ret
= mach_vm_allocate(map
, &map_addr
, dyl_length
, VM_FLAGS_ANYWHERE
);
1409 if (ret
!= KERN_SUCCESS
) {
1414 ret
= mach_vm_deallocate(map
, map_addr
, dyl_length
);
1415 if (ret
!= KERN_SUCCESS
) {
1420 if (map_addr
< dyl_start
)
1421 slide_amount
= -(int64_t)(dyl_start
- map_addr
);
1423 slide_amount
= (int64_t)(map_addr
- dyl_start
);
1425 slide_amount
+= slide
;
1427 *myresult
= load_result_null
;
1429 ret
= parse_machfile(vp
, map
, thread
, header
,
1430 file_offset
, macho_size
, depth
, slide_amount
, myresult
);
1437 if (ret
== LOAD_SUCCESS
) {
1438 result
->dynlinker
= TRUE
;
1439 result
->entry_point
= myresult
->entry_point
;
1440 result
->validentry
= myresult
->validentry
;
1441 result
->all_image_info_addr
= myresult
->all_image_info_addr
;
1442 result
->all_image_info_size
= myresult
->all_image_info_size
;
1447 FREE(dyld_data
, M_TEMP
);
1452 static load_return_t
1453 load_code_signature(
1454 struct linkedit_data_command
*lcp
,
1459 load_result_t
*result
)
1465 struct cs_blob
*blob
;
1467 vm_size_t blob_size
;
1472 if (lcp
->cmdsize
!= sizeof (struct linkedit_data_command
) ||
1473 lcp
->dataoff
+ lcp
->datasize
> macho_size
) {
1474 ret
= LOAD_BADMACHO
;
1478 blob
= ubc_cs_blob_get(vp
, cputype
, -1);
1480 /* we already have a blob for this vnode and cputype */
1481 if (blob
->csb_cpu_type
== cputype
&&
1482 blob
->csb_base_offset
== macho_offset
&&
1483 blob
->csb_mem_size
== lcp
->datasize
) {
1484 /* it matches the blob we want here: we're done */
1487 /* the blob has changed for this vnode: fail ! */
1488 ret
= LOAD_BADMACHO
;
1493 blob_size
= lcp
->datasize
;
1494 kr
= ubc_cs_blob_allocate(&addr
, &blob_size
);
1495 if (kr
!= KERN_SUCCESS
) {
1501 error
= vn_rdwr(UIO_READ
,
1505 macho_offset
+ lcp
->dataoff
,
1511 if (error
|| resid
!= 0) {
1516 if (ubc_cs_blob_add(vp
,
1524 /* ubc_cs_blob_add() has consumed "addr" */
1528 #if CHECK_CS_VALIDATION_BITMAP
1529 ubc_cs_validation_bitmap_allocate( vp
);
1532 blob
= ubc_cs_blob_get(vp
, cputype
, -1);
1536 if (result
&& ret
== LOAD_SUCCESS
) {
1537 result
->csflags
|= blob
->csb_flags
;
1540 ubc_cs_blob_deallocate(addr
, blob_size
);
1548 #if CONFIG_CODE_DECRYPTION
1550 static load_return_t
1552 struct encryption_info_command
*eip
,
1559 pager_crypt_info_t crypt_info
;
1560 const char * cryptname
= 0;
1564 struct segment_command_64
*seg64
;
1565 struct segment_command
*seg32
;
1566 vm_map_offset_t map_offset
, map_size
;
1569 if (eip
->cmdsize
< sizeof(*eip
)) return LOAD_BADMACHO
;
1571 switch(eip
->cryptid
) {
1573 /* not encrypted, just an empty load command */
1574 return LOAD_SUCCESS
;
1576 cryptname
="com.apple.unfree";
1579 /* some random cryptid that you could manually put into
1580 * your binary if you want NULL */
1581 cryptname
="com.apple.null";
1584 return LOAD_BADMACHO
;
1587 if (map
== VM_MAP_NULL
) return (LOAD_SUCCESS
);
1588 if (NULL
== text_crypter_create
) return LOAD_FAILURE
;
1590 MALLOC_ZONE(vpath
, char *, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
1591 if(vpath
== NULL
) return LOAD_FAILURE
;
1594 result
= vn_getpath(vp
, vpath
, &len
);
1596 FREE_ZONE(vpath
, MAXPATHLEN
, M_NAMEI
);
1597 return LOAD_FAILURE
;
1600 /* set up decrypter first */
1601 kr
=text_crypter_create(&crypt_info
, cryptname
, (void*)vpath
);
1602 FREE_ZONE(vpath
, MAXPATHLEN
, M_NAMEI
);
1605 printf("set_code_unprotect: unable to create decrypter %s, kr=%d\n",
1607 return LOAD_RESOURCE
;
1610 /* this is terrible, but we have to rescan the load commands to find the
1611 * virtual address of this encrypted stuff. This code is gonna look like
1612 * the dyld source one day... */
1613 struct mach_header
*header
= (struct mach_header
*)addr
;
1614 size_t mach_header_sz
= sizeof(struct mach_header
);
1615 if (header
->magic
== MH_MAGIC_64
||
1616 header
->magic
== MH_CIGAM_64
) {
1617 mach_header_sz
= sizeof(struct mach_header_64
);
1619 offset
= mach_header_sz
;
1620 uint32_t ncmds
= header
->ncmds
;
1623 * Get a pointer to the command.
1625 struct load_command
*lcp
= (struct load_command
*)(addr
+ offset
);
1626 offset
+= lcp
->cmdsize
;
1630 seg64
= (struct segment_command_64
*)lcp
;
1631 if ((seg64
->fileoff
<= eip
->cryptoff
) &&
1632 (seg64
->fileoff
+seg64
->filesize
>=
1633 eip
->cryptoff
+eip
->cryptsize
)) {
1634 map_offset
= seg64
->vmaddr
+ eip
->cryptoff
- seg64
->fileoff
+ slide
;
1635 map_size
= eip
->cryptsize
;
1639 seg32
= (struct segment_command
*)lcp
;
1640 if ((seg32
->fileoff
<= eip
->cryptoff
) &&
1641 (seg32
->fileoff
+seg32
->filesize
>=
1642 eip
->cryptoff
+eip
->cryptsize
)) {
1643 map_offset
= seg32
->vmaddr
+ eip
->cryptoff
- seg32
->fileoff
+ slide
;
1644 map_size
= eip
->cryptsize
;
1650 /* if we get here, did not find anything */
1651 return LOAD_BADMACHO
;
1654 /* now remap using the decrypter */
1655 kr
= vm_map_apple_protected(map
, map_offset
, map_offset
+map_size
, &crypt_info
);
1657 printf("set_code_unprotect(): mapping failed with %x\n", kr
);
1658 crypt_info
.crypt_end(crypt_info
.crypt_ops
);
1659 return LOAD_PROTECT
;
1662 return LOAD_SUCCESS
;
1668 * This routine exists to support the load_dylinker().
1670 * This routine has its own, separate, understanding of the FAT file format,
1671 * which is terrifically unfortunate.
1678 struct mach_header
*mach_header
,
1681 struct macho_data
*data
,
1686 vfs_context_t ctx
= vfs_context_current();
1687 proc_t p
= vfs_context_proc(ctx
);
1688 kauth_cred_t kerncred
;
1689 struct nameidata
*ndp
= &data
->__nid
;
1691 struct fat_arch fat_arch
;
1694 union macho_vnode_header
*header
= &data
->__header
;
1695 off_t fsize
= (off_t
)0;
1698 * Capture the kernel credential for use in the actual read of the
1699 * file, since the user doing the execution may have execute rights
1700 * but not read rights, but to exec something, we have to either map
1701 * or read it into the new process address space, which requires
1702 * read rights. This is to deal with lack of common credential
1703 * serialization code which would treat NOCRED as "serialize 'root'".
1705 kerncred
= vfs_context_ucred(vfs_context_kernel());
1707 /* init the namei data to point the file user's program name */
1708 NDINIT(ndp
, LOOKUP
, OP_OPEN
, FOLLOW
| LOCKLEAF
, UIO_SYSSPACE
, CAST_USER_ADDR_T(path
), ctx
);
1710 if ((error
= namei(ndp
)) != 0) {
1711 if (error
== ENOENT
) {
1712 error
= LOAD_ENOENT
;
1714 error
= LOAD_FAILURE
;
1721 /* check for regular file */
1722 if (vp
->v_type
!= VREG
) {
1723 error
= LOAD_PROTECT
;
1728 if ((error
= vnode_size(vp
, &fsize
, ctx
)) != 0) {
1729 error
= LOAD_FAILURE
;
1733 /* Check mount point */
1734 if (vp
->v_mount
->mnt_flag
& MNT_NOEXEC
) {
1735 error
= LOAD_PROTECT
;
1740 if ((error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_EXECUTE
, ctx
)) != 0) {
1741 error
= LOAD_PROTECT
;
1745 /* try to open it */
1746 if ((error
= VNOP_OPEN(vp
, FREAD
, ctx
)) != 0) {
1747 error
= LOAD_PROTECT
;
1751 if ((error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)header
, sizeof (*header
), 0,
1752 UIO_SYSSPACE
, IO_NODELOCKED
, kerncred
, &resid
, p
)) != 0) {
1753 error
= LOAD_IOERROR
;
1757 if (header
->mach_header
.magic
== MH_MAGIC
||
1758 header
->mach_header
.magic
== MH_MAGIC_64
) {
1760 } else if (header
->fat_header
.magic
== FAT_MAGIC
||
1761 header
->fat_header
.magic
== FAT_CIGAM
) {
1764 error
= LOAD_BADMACHO
;
1769 /* Look up our architecture in the fat file. */
1770 error
= fatfile_getarch_with_bits(vp
, archbits
,
1771 (vm_offset_t
)(&header
->fat_header
), &fat_arch
);
1772 if (error
!= LOAD_SUCCESS
)
1775 /* Read the Mach-O header out of it */
1776 error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)&header
->mach_header
,
1777 sizeof (header
->mach_header
), fat_arch
.offset
,
1778 UIO_SYSSPACE
, IO_NODELOCKED
, kerncred
, &resid
, p
);
1780 error
= LOAD_IOERROR
;
1784 /* Is this really a Mach-O? */
1785 if (header
->mach_header
.magic
!= MH_MAGIC
&&
1786 header
->mach_header
.magic
!= MH_MAGIC_64
) {
1787 error
= LOAD_BADMACHO
;
1791 *file_offset
= fat_arch
.offset
;
1792 *macho_size
= fat_arch
.size
;
1795 * Force get_macho_vnode() to fail if the architecture bits
1796 * do not match the expected architecture bits. This in
1797 * turn causes load_dylinker() to fail for the same reason,
1798 * so it ensures the dynamic linker and the binary are in
1799 * lock-step. This is potentially bad, if we ever add to
1800 * the CPU_ARCH_* bits any bits that are desirable but not
1801 * required, since the dynamic linker might work, but we will
1802 * refuse to load it because of this check.
1804 if ((cpu_type_t
)(header
->mach_header
.cputype
& CPU_ARCH_MASK
) != archbits
) {
1805 error
= LOAD_BADARCH
;
1810 *macho_size
= fsize
;
1813 *mach_header
= header
->mach_header
;
1816 ubc_setsize(vp
, fsize
);
1820 (void) VNOP_CLOSE(vp
, FREAD
, ctx
);