2 * Copyright (c) 2000-2007 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>
63 #include <kern/kern_types.h>
64 #include <kern/cpu_number.h>
65 #include <kern/mach_loader.h>
66 #include <kern/mach_fat.h>
67 #include <kern/kalloc.h>
68 #include <kern/task.h>
69 #include <kern/thread.h>
70 #include <kern/page_decrypt.h>
72 #include <mach-o/fat.h>
73 #include <mach-o/loader.h>
76 #include <vm/vm_map.h>
77 #include <vm/vm_kern.h>
78 #include <vm/vm_pager.h>
79 #include <vm/vnode_pager.h>
80 #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(vm_map_size_t size
, boolean_t is_64bit
);
88 extern void pmap_switch(pmap_t
);
91 * XXX kern/thread.h should not treat these prototypes as MACH_KERNEL_PRIVATE
92 * when KERNEL is defined.
94 extern kern_return_t
thread_setstatus(thread_t thread
, int flavor
,
95 thread_state_t tstate
,
96 mach_msg_type_number_t count
);
98 extern kern_return_t
thread_state_initialize(thread_t thread
);
101 /* XXX should have prototypes in a shared header file */
102 extern int get_map_nentries(vm_map_t
);
103 extern kern_return_t
thread_userstack(thread_t
, int, thread_state_t
,
104 unsigned int, mach_vm_offset_t
*, int *);
105 extern kern_return_t
thread_entrypoint(thread_t
, int, thread_state_t
,
106 unsigned int, mach_vm_offset_t
*);
108 extern kern_return_t
memory_object_signed(memory_object_control_t control
,
109 boolean_t is_signed
);
111 /* An empty load_result_t */
112 static load_result_t load_result_null
= {
113 .mach_header
= MACH_VM_MIN_ADDRESS
,
114 .entry_point
= MACH_VM_MIN_ADDRESS
,
115 .user_stack
= MACH_VM_MIN_ADDRESS
,
116 .all_image_info_addr
= MACH_VM_MIN_ADDRESS
,
117 .all_image_info_size
= 0,
127 * Prototypes of static functions.
134 struct mach_header
*header
,
138 load_result_t
*result
143 struct load_command
*lcp
,
150 load_result_t
*result
153 int load_code_signature(
154 struct linkedit_data_command
*lcp
,
159 load_result_t
*result
);
161 #if CONFIG_CODE_DECRYPTION
164 struct encryption_info_command
*lcp
,
172 struct thread_command
*tcp
,
174 load_result_t
*result
179 struct thread_command
*tcp
,
181 load_result_t
*result
196 user_addr_t
*user_stack
,
205 mach_vm_offset_t
*entry_point
210 struct dylinker_command
*lcp
,
215 load_result_t
*result
,
223 struct mach_header
*mach_header
,
230 widen_segment_command(const struct segment_command
*scp32
,
231 struct segment_command_64
*scp
)
233 scp
->cmd
= scp32
->cmd
;
234 scp
->cmdsize
= scp32
->cmdsize
;
235 bcopy(scp32
->segname
, scp
->segname
, sizeof(scp
->segname
));
236 scp
->vmaddr
= scp32
->vmaddr
;
237 scp
->vmsize
= scp32
->vmsize
;
238 scp
->fileoff
= scp32
->fileoff
;
239 scp
->filesize
= scp32
->filesize
;
240 scp
->maxprot
= scp32
->maxprot
;
241 scp
->initprot
= scp32
->initprot
;
242 scp
->nsects
= scp32
->nsects
;
243 scp
->flags
= scp32
->flags
;
247 note_all_image_info_section(const struct segment_command_64
*scp
,
248 boolean_t is64
, size_t section_size
, const void *sections
,
249 load_result_t
*result
)
253 struct section_64 s64
;
257 if (strncmp(scp
->segname
, "__DATA", sizeof(scp
->segname
)) != 0)
259 for (i
= 0; i
< scp
->nsects
; ++i
) {
260 sectionp
= (const void *)
261 ((const char *)sections
+ section_size
* i
);
262 if (0 == strncmp(sectionp
->s64
.sectname
, "__all_image_info",
263 sizeof(sectionp
->s64
.sectname
))) {
264 result
->all_image_info_addr
=
265 is64
? sectionp
->s64
.addr
: sectionp
->s32
.addr
;
266 result
->all_image_info_size
=
267 is64
? sectionp
->s64
.size
: sectionp
->s32
.size
;
276 struct image_params
*imgp
,
277 struct mach_header
*header
,
280 load_result_t
*result
283 struct vnode
*vp
= imgp
->ip_vp
;
284 off_t file_offset
= imgp
->ip_arch_offset
;
285 off_t macho_size
= imgp
->ip_arch_size
;
287 pmap_t pmap
= 0; /* protected by create_map */
290 task_t old_task
= TASK_NULL
; /* protected by create_map */
291 load_result_t myresult
;
293 boolean_t create_map
= FALSE
;
294 int spawn
= (imgp
->ip_flags
& IMGPF_SPAWN
);
295 task_t task
= current_task();
297 if (new_map
== VM_MAP_NULL
) {
299 old_task
= current_task();
303 * If we are spawning, we have created backing objects for the process
304 * already, which include non-lazily creating the task map. So we
305 * are going to switch out the task map with one appropriate for the
306 * bitness of the image being loaded.
310 old_task
= get_threadtask(thread
);
314 pmap
= pmap_create((vm_map_size_t
) 0, (imgp
->ip_flags
& IMGPF_IS_64BIT
));
315 map
= vm_map_create(pmap
,
317 vm_compute_max_offset((imgp
->ip_flags
& IMGPF_IS_64BIT
)),
322 if ( (header
->flags
& MH_ALLOW_STACK_EXECUTION
) )
323 vm_map_disable_NX(map
);
328 *result
= load_result_null
;
330 lret
= parse_machfile(vp
, map
, thread
, header
, file_offset
, macho_size
,
333 if (lret
!= LOAD_SUCCESS
) {
335 vm_map_deallocate(map
); /* will lose pmap reference too */
341 * For 64-bit users, check for presence of a 4GB page zero
342 * which will enable the kernel to share the user's address space
343 * and hence avoid TLB flushes on kernel entry/exit
346 if ((imgp
->ip_flags
& IMGPF_IS_64BIT
) &&
347 vm_map_has_4GB_pagezero(map
))
348 vm_map_set_4GB_pagezero(map
);
353 * Swap the new map for the old, which consumes our new map
354 * reference but each leaves us responsible for the old_map reference.
355 * That lets us get off the pmap associated with it, and
356 * then we can release it.
361 * If this is an exec, then we are going to destory the old
362 * task, and it's correct to halt it; if it's spawn, the
363 * task is not yet running, and it makes no sense.
367 * Mark the task as halting and start the other
368 * threads towards terminating themselves. Then
369 * make sure any threads waiting for a process
370 * transition get informed that we are committed to
371 * this transition, and then finally complete the
372 * task halting (wait for threads and then cleanup
375 task_start_halt(task
);
376 proc_transcommit(current_proc(), 0);
377 task_complete_halt(task
);
379 old_map
= swap_task_map(old_task
, thread
, map
);
380 vm_map_clear_4GB_pagezero(old_map
);
381 /* XXX L4 : For spawn the current task isn't running... */
383 pmap_switch(pmap
); /* Make sure we are using the new pmap */
384 vm_map_deallocate(old_map
);
386 return(LOAD_SUCCESS
);
390 * The file size of a mach-o file is limited to 32 bits; this is because
391 * this is the limit on the kalloc() of enough bytes for a mach_header and
392 * the contents of its sizeofcmds, which is currently constrained to 32
393 * bits in the file format itself. We read into the kernel buffer the
394 * commands section, and then parse it in order to parse the mach-o file
395 * format load_command segment(s). We are only interested in a subset of
396 * the total set of possible commands.
404 struct mach_header
*header
,
408 load_result_t
*result
412 struct load_command
*lcp
;
413 struct dylinker_command
*dlp
= 0;
414 struct uuid_command
*uulp
= 0;
415 integer_t dlarchbits
= 0;
417 load_return_t ret
= LOAD_SUCCESS
;
420 vm_size_t size
,kl_size
;
422 size_t oldoffset
; /* for overflow check */
424 proc_t p
= current_proc(); /* XXXX */
428 size_t mach_header_sz
= sizeof(struct mach_header
);
430 boolean_t got_code_signatures
= FALSE
;
432 if (header
->magic
== MH_MAGIC_64
||
433 header
->magic
== MH_CIGAM_64
) {
434 mach_header_sz
= sizeof(struct mach_header_64
);
438 * Break infinite recursion
441 return(LOAD_FAILURE
);
444 task
= (task_t
)get_threadtask(thread
);
449 * Check to see if right machine type.
451 if (((cpu_type_t
)(header
->cputype
& ~CPU_ARCH_MASK
) != cpu_type()) ||
452 !grade_binary(header
->cputype
,
453 header
->cpusubtype
& ~CPU_SUBTYPE_MASK
))
454 return(LOAD_BADARCH
);
456 abi64
= ((header
->cputype
& CPU_ARCH_ABI64
) == CPU_ARCH_ABI64
);
458 switch (header
->filetype
) {
464 return (LOAD_FAILURE
);
471 return (LOAD_FAILURE
);
477 return (LOAD_FAILURE
);
482 return (LOAD_FAILURE
);
486 * Get the pager for the file.
488 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
491 * Map portion that must be accessible directly into
494 if ((off_t
)(mach_header_sz
+ header
->sizeofcmds
) > macho_size
)
495 return(LOAD_BADMACHO
);
498 * Round size of Mach-O commands up to page boundry.
500 size
= round_page(mach_header_sz
+ header
->sizeofcmds
);
502 return(LOAD_BADMACHO
);
505 * Map the load commands into kernel memory.
509 kl_addr
= kalloc(size
);
510 addr
= (caddr_t
)kl_addr
;
512 return(LOAD_NOSPACE
);
514 error
= vn_rdwr(UIO_READ
, vp
, addr
, size
, file_offset
,
515 UIO_SYSSPACE
, 0, kauth_cred_get(), &resid
, p
);
518 kfree(kl_addr
, kl_size
);
519 return(LOAD_IOERROR
);
523 * Scan through the commands, processing each one as necessary.
525 for (pass
= 1; pass
<= 2; pass
++) {
527 * Loop through each of the load_commands indicated by the
528 * Mach-O header; if an absurd value is provided, we just
529 * run off the end of the reserved section by incrementing
530 * the offset too far, so we are implicitly fail-safe.
532 offset
= mach_header_sz
;
533 ncmds
= header
->ncmds
;
536 * Get a pointer to the command.
538 lcp
= (struct load_command
*)(addr
+ offset
);
540 offset
+= lcp
->cmdsize
;
543 * Perform prevalidation of the struct load_command
544 * before we attempt to use its contents. Invalid
545 * values are ones which result in an overflow, or
546 * which can not possibly be valid commands, or which
547 * straddle or exist past the reserved section at the
548 * start of the image.
550 if (oldoffset
> offset
||
551 lcp
->cmdsize
< sizeof(struct load_command
) ||
552 offset
> header
->sizeofcmds
+ mach_header_sz
) {
558 * Act on struct load_command's for which kernel
559 * intervention is required.
566 ret
= load_segment(lcp
,
578 ret
= load_thread((struct thread_command
*)lcp
,
585 ret
= load_unixthread(
586 (struct thread_command
*) lcp
,
590 case LC_LOAD_DYLINKER
:
593 if ((depth
== 1) && (dlp
== 0)) {
594 dlp
= (struct dylinker_command
*)lcp
;
595 dlarchbits
= (header
->cputype
& CPU_ARCH_MASK
);
601 if (pass
== 2 && depth
== 1) {
602 uulp
= (struct uuid_command
*)lcp
;
603 memcpy(&result
->uuid
[0], &uulp
->uuid
[0], sizeof(result
->uuid
));
606 case LC_CODE_SIGNATURE
:
611 load signatures & store in uip
612 set VM object "signed_pages"
614 ret
= load_code_signature(
615 (struct linkedit_data_command
*) lcp
,
620 (depth
== 1) ? result
: NULL
);
621 if (ret
!= LOAD_SUCCESS
) {
622 printf("proc %d: load code signature error %d "
624 p
->p_pid
, ret
, vp
->v_name
);
625 ret
= LOAD_SUCCESS
; /* ignore error */
627 got_code_signatures
= TRUE
;
630 #if CONFIG_CODE_DECRYPTION
631 case LC_ENCRYPTION_INFO
:
634 ret
= set_code_unprotect(
635 (struct encryption_info_command
*) lcp
,
637 if (ret
!= LOAD_SUCCESS
) {
638 printf("proc %d: set_code_unprotect() error %d "
640 p
->p_pid
, ret
, vp
->v_name
);
641 /* Don't let the app run if it's
642 * encrypted but we failed to set up the
649 /* Other commands are ignored by the kernel */
653 if (ret
!= LOAD_SUCCESS
)
656 if (ret
!= LOAD_SUCCESS
)
659 if (ret
== LOAD_SUCCESS
) {
660 if (! got_code_signatures
) {
661 struct cs_blob
*blob
;
662 /* no embedded signatures: look for detached ones */
663 blob
= ubc_cs_blob_get(vp
, -1, file_offset
);
665 /* get flags to be applied to the process */
666 result
->csflags
|= blob
->csb_flags
;
671 ret
= load_dylinker(dlp
, dlarchbits
, map
, thread
, depth
, result
, abi64
);
674 if (result
->thread_count
== 0) {
676 } else if ( abi64
) {
678 /* Map in 64-bit commpage */
680 * PPC51: ppc64 is limited to 51-bit addresses.
681 * Memory above that limit is handled specially
684 * <rdar://6640492> -- wrong task for vfork()/spawn()
686 pmap_map_sharedpage(current_task(), get_map_pmap(map
));
693 kfree(kl_addr
, kl_size
);
698 #if CONFIG_CODE_DECRYPTION
700 #define APPLE_UNPROTECTED_HEADER_SIZE (3 * PAGE_SIZE_64)
709 vm_map_offset_t map_addr
,
710 vm_map_size_t map_size
)
715 * The first APPLE_UNPROTECTED_HEADER_SIZE bytes (from offset 0 of
716 * this part of a Universal binary) are not protected...
717 * The rest needs to be "transformed".
719 if (file_off
<= APPLE_UNPROTECTED_HEADER_SIZE
&&
720 file_off
+ file_size
<= APPLE_UNPROTECTED_HEADER_SIZE
) {
721 /* it's all unprotected, nothing to do... */
724 if (file_off
<= APPLE_UNPROTECTED_HEADER_SIZE
) {
726 * We start mapping in the unprotected area.
727 * Skip the unprotected part...
729 vm_map_offset_t delta
;
731 delta
= APPLE_UNPROTECTED_HEADER_SIZE
;
736 /* ... transform the rest of the mapping. */
737 struct pager_crypt_info crypt_info
;
738 crypt_info
.page_decrypt
= dsmos_page_transform
;
739 crypt_info
.crypt_ops
= NULL
;
740 crypt_info
.crypt_end
= NULL
;
741 #pragma unused(vp, macho_offset)
742 crypt_info
.crypt_ops
= (void *)0x2e69cf40;
743 kr
= vm_map_apple_protected(map
,
749 if (kr
!= KERN_SUCCESS
) {
754 #else /* CONFIG_CODE_DECRYPTION */
757 __unused
uint64_t file_off
,
758 __unused
uint64_t file_size
,
759 __unused
struct vnode
*vp
,
760 __unused off_t macho_offset
,
761 __unused vm_map_t map
,
762 __unused vm_map_offset_t map_addr
,
763 __unused vm_map_size_t map_size
)
767 #endif /* CONFIG_CODE_DECRYPTION */
772 struct load_command
*lcp
,
779 load_result_t
*result
782 struct segment_command_64 segment_command
, *scp
;
784 mach_vm_offset_t map_addr
, map_offset
;
785 mach_vm_size_t map_size
, seg_size
, delta_size
;
788 size_t segment_command_size
, total_section_size
,
791 if (LC_SEGMENT_64
== lcp
->cmd
) {
792 segment_command_size
= sizeof(struct segment_command_64
);
793 single_section_size
= sizeof(struct section_64
);
794 scp
= (struct segment_command_64
*)lcp
;
796 segment_command_size
= sizeof(struct segment_command
);
797 single_section_size
= sizeof(struct section
);
798 scp
= &segment_command
;
799 widen_segment_command((struct segment_command
*)lcp
, scp
);
801 if (lcp
->cmdsize
< segment_command_size
)
802 return (LOAD_BADMACHO
);
803 total_section_size
= lcp
->cmdsize
- segment_command_size
;
806 * Make sure what we get from the file is really ours (as specified
809 if (scp
->fileoff
+ scp
->filesize
< scp
->fileoff
||
810 scp
->fileoff
+ scp
->filesize
> (uint64_t)macho_size
)
811 return (LOAD_BADMACHO
);
813 * Ensure that the number of sections specified would fit
814 * within the load command size.
816 if (total_section_size
/ single_section_size
< scp
->nsects
)
817 return (LOAD_BADMACHO
);
819 * Make sure the segment is page-aligned in the file.
821 if ((scp
->fileoff
& PAGE_MASK_64
) != 0)
822 return (LOAD_BADMACHO
);
825 * Round sizes to page size.
827 seg_size
= round_page_64(scp
->vmsize
);
828 map_size
= round_page_64(scp
->filesize
);
829 map_addr
= trunc_page_64(scp
->vmaddr
); /* JVXXX note that in XNU TOT this is round instead of trunc for 64 bits */
831 return (KERN_SUCCESS
);
832 /* XXX (4596982) this interferes with Rosetta, so limit to 64-bit tasks */
836 scp
->cmd
== LC_SEGMENT_64
&&
837 (scp
->initprot
& VM_PROT_ALL
) == VM_PROT_NONE
&&
838 (scp
->maxprot
& VM_PROT_ALL
) == VM_PROT_NONE
) {
840 * This is a "page zero" segment: it starts at address 0,
841 * is not mapped from the binary file and is not accessible.
842 * User-space should never be able to access that memory, so
843 * make it completely off limits by raising the VM map's
846 ret
= vm_map_raise_min_offset(map
, seg_size
);
847 if (ret
!= KERN_SUCCESS
) {
848 return (LOAD_FAILURE
);
850 return (LOAD_SUCCESS
);
853 map_offset
= pager_offset
+ scp
->fileoff
; /* limited to 32 bits */
856 initprot
= (scp
->initprot
) & VM_PROT_ALL
;
857 maxprot
= (scp
->maxprot
) & VM_PROT_ALL
;
859 * Map a copy of the file into the address space.
861 ret
= vm_map_enter_mem_object_control(map
,
862 &map_addr
, map_size
, (mach_vm_offset_t
)0,
863 VM_FLAGS_FIXED
, control
, map_offset
, TRUE
,
866 if (ret
!= KERN_SUCCESS
)
867 return (LOAD_NOSPACE
);
870 * If the file didn't end on a page boundary,
871 * we need to zero the leftover.
873 delta_size
= map_size
- scp
->filesize
;
875 if (delta_size
> 0) {
876 mach_vm_offset_t tmp
;
878 ret
= mach_vm_allocate(kernel_map
, &tmp
, delta_size
, VM_FLAGS_ANYWHERE
);
879 if (ret
!= KERN_SUCCESS
)
880 return(LOAD_RESOURCE
);
882 if (copyout(tmp
, map_addr
+ scp
->filesize
,
884 (void) mach_vm_deallocate(
885 kernel_map
, tmp
, delta_size
);
886 return (LOAD_FAILURE
);
889 (void) mach_vm_deallocate(kernel_map
, tmp
, delta_size
);
895 * If the virtual size of the segment is greater
896 * than the size from the file, we need to allocate
897 * zero fill memory for the rest.
899 delta_size
= seg_size
- map_size
;
900 if (delta_size
> 0) {
901 mach_vm_offset_t tmp
= map_addr
+ map_size
;
903 ret
= mach_vm_map(map
, &tmp
, delta_size
, 0, VM_FLAGS_FIXED
,
905 scp
->initprot
, scp
->maxprot
,
907 if (ret
!= KERN_SUCCESS
)
908 return(LOAD_NOSPACE
);
911 if ( (scp
->fileoff
== 0) && (scp
->filesize
!= 0) )
912 result
->mach_header
= map_addr
;
914 if (scp
->flags
& SG_PROTECTED_VERSION_1
) {
915 ret
= unprotect_segment(scp
->fileoff
,
925 if (LOAD_SUCCESS
== ret
&& filetype
== MH_DYLINKER
&&
926 result
->all_image_info_addr
== MACH_VM_MIN_ADDRESS
)
927 note_all_image_info_section(scp
,
928 LC_SEGMENT_64
== lcp
->cmd
, single_section_size
,
929 (const char *)lcp
+ segment_command_size
, result
);
937 struct thread_command
*tcp
,
939 load_result_t
*result
947 if (tcp
->cmdsize
< sizeof(*tcp
))
948 return (LOAD_BADMACHO
);
949 task
= get_threadtask(thread
);
951 /* if count is 0; same as thread */
952 if (result
->thread_count
!= 0) {
953 kret
= thread_create(task
, &thread
);
954 if (kret
!= KERN_SUCCESS
)
955 return(LOAD_RESOURCE
);
956 thread_deallocate(thread
);
959 lret
= load_threadstate(thread
,
960 (uint32_t *)(((vm_offset_t
)tcp
) +
961 sizeof(struct thread_command
)),
962 tcp
->cmdsize
- sizeof(struct thread_command
));
963 if (lret
!= LOAD_SUCCESS
)
966 if (result
->thread_count
== 0) {
967 lret
= load_threadstack(thread
,
968 (uint32_t *)(((vm_offset_t
)tcp
) +
969 sizeof(struct thread_command
)),
970 tcp
->cmdsize
- sizeof(struct thread_command
),
974 result
->customstack
= 1;
976 result
->customstack
= 0;
978 if (lret
!= LOAD_SUCCESS
)
981 lret
= load_threadentry(thread
,
982 (uint32_t *)(((vm_offset_t
)tcp
) +
983 sizeof(struct thread_command
)),
984 tcp
->cmdsize
- sizeof(struct thread_command
),
985 &result
->entry_point
);
986 if (lret
!= LOAD_SUCCESS
)
990 * Resume thread now, note that this means that the thread
991 * commands should appear after all the load commands to
992 * be sure they don't reference anything not yet mapped.
995 thread_resume(thread
);
997 result
->thread_count
++;
999 return(LOAD_SUCCESS
);
1005 struct thread_command
*tcp
,
1007 load_result_t
*result
1013 if (tcp
->cmdsize
< sizeof(*tcp
))
1014 return (LOAD_BADMACHO
);
1015 if (result
->thread_count
!= 0) {
1016 printf("load_unixthread: already have a thread!");
1017 return (LOAD_FAILURE
);
1020 ret
= load_threadstack(thread
,
1021 (uint32_t *)(((vm_offset_t
)tcp
) +
1022 sizeof(struct thread_command
)),
1023 tcp
->cmdsize
- sizeof(struct thread_command
),
1024 &result
->user_stack
,
1026 if (ret
!= LOAD_SUCCESS
)
1030 result
->customstack
= 1;
1032 result
->customstack
= 0;
1033 ret
= load_threadentry(thread
,
1034 (uint32_t *)(((vm_offset_t
)tcp
) +
1035 sizeof(struct thread_command
)),
1036 tcp
->cmdsize
- sizeof(struct thread_command
),
1037 &result
->entry_point
);
1038 if (ret
!= LOAD_SUCCESS
)
1041 ret
= load_threadstate(thread
,
1042 (uint32_t *)(((vm_offset_t
)tcp
) +
1043 sizeof(struct thread_command
)),
1044 tcp
->cmdsize
- sizeof(struct thread_command
));
1045 if (ret
!= LOAD_SUCCESS
)
1048 result
->unixproc
= TRUE
;
1049 result
->thread_count
++;
1051 return(LOAD_SUCCESS
);
1065 uint32_t thread_size
;
1067 ret
= thread_state_initialize( thread
);
1068 if (ret
!= KERN_SUCCESS
) {
1069 return(LOAD_FAILURE
);
1073 * Set the new thread state; iterate through the state flavors in
1076 while (total_size
> 0) {
1079 if (UINT32_MAX
-2 < size
||
1080 UINT32_MAX
/sizeof(uint32_t) < size
+2)
1081 return (LOAD_BADMACHO
);
1082 thread_size
= (size
+2)*sizeof(uint32_t);
1083 if (thread_size
> total_size
)
1084 return(LOAD_BADMACHO
);
1085 total_size
-= thread_size
;
1087 * Third argument is a kernel space pointer; it gets cast
1088 * to the appropriate type in machine_thread_set_state()
1089 * based on the value of flavor.
1091 ret
= thread_setstatus(thread
, flavor
, (thread_state_t
)ts
, size
);
1092 if (ret
!= KERN_SUCCESS
) {
1093 return(LOAD_FAILURE
);
1095 ts
+= size
; /* ts is a (uint32_t *) */
1097 return(LOAD_SUCCESS
);
1105 uint32_t total_size
,
1106 user_addr_t
*user_stack
,
1113 uint32_t stack_size
;
1115 while (total_size
> 0) {
1118 if (UINT32_MAX
-2 < size
||
1119 UINT32_MAX
/sizeof(uint32_t) < size
+2)
1120 return (LOAD_BADMACHO
);
1121 stack_size
= (size
+2)*sizeof(uint32_t);
1122 if (stack_size
> total_size
)
1123 return(LOAD_BADMACHO
);
1124 total_size
-= stack_size
;
1127 * Third argument is a kernel space pointer; it gets cast
1128 * to the appropriate type in thread_userstack() based on
1129 * the value of flavor.
1131 ret
= thread_userstack(thread
, flavor
, (thread_state_t
)ts
, size
, user_stack
, customstack
);
1132 if (ret
!= KERN_SUCCESS
) {
1133 return(LOAD_FAILURE
);
1135 ts
+= size
; /* ts is a (uint32_t *) */
1137 return(LOAD_SUCCESS
);
1145 uint32_t total_size
,
1146 mach_vm_offset_t
*entry_point
1152 uint32_t entry_size
;
1155 * Set the thread state.
1157 *entry_point
= MACH_VM_MIN_ADDRESS
;
1158 while (total_size
> 0) {
1161 if (UINT32_MAX
-2 < size
||
1162 UINT32_MAX
/sizeof(uint32_t) < size
+2)
1163 return (LOAD_BADMACHO
);
1164 entry_size
= (size
+2)*sizeof(uint32_t);
1165 if (entry_size
> total_size
)
1166 return(LOAD_BADMACHO
);
1167 total_size
-= entry_size
;
1169 * Third argument is a kernel space pointer; it gets cast
1170 * to the appropriate type in thread_entrypoint() based on
1171 * the value of flavor.
1173 ret
= thread_entrypoint(thread
, flavor
, (thread_state_t
)ts
, size
, entry_point
);
1174 if (ret
!= KERN_SUCCESS
) {
1175 return(LOAD_FAILURE
);
1177 ts
+= size
; /* ts is a (uint32_t *) */
1179 return(LOAD_SUCCESS
);
1186 struct dylinker_command
*lcp
,
1191 load_result_t
*result
,
1197 struct vnode
*vp
= NULLVP
; /* set by get_macho_vnode() */
1198 struct mach_header header
;
1199 off_t file_offset
= 0; /* set by get_macho_vnode() */
1200 off_t macho_size
= 0; /* set by get_macho_vnode() */
1202 load_result_t myresult
;
1205 mach_vm_offset_t dyl_start
, map_addr
;
1206 mach_vm_size_t dyl_length
;
1208 if (lcp
->cmdsize
< sizeof(*lcp
))
1209 return (LOAD_BADMACHO
);
1211 name
= (char *)lcp
+ lcp
->name
.offset
;
1213 * Check for a proper null terminated string.
1217 if (p
>= (char *)lcp
+ lcp
->cmdsize
)
1218 return(LOAD_BADMACHO
);
1221 ret
= get_macho_vnode(name
, archbits
, &header
, &file_offset
, &macho_size
, &vp
);
1225 myresult
= load_result_null
;
1228 * First try to map dyld in directly. This should work most of
1229 * the time since there shouldn't normally be something already
1230 * mapped to its address.
1233 ret
= parse_machfile(vp
, map
, thread
, &header
, file_offset
, macho_size
,
1237 * If it turned out something was in the way, then we'll take
1238 * take this longer path to map dyld into a temporary map and
1239 * copy it into destination map at a different address.
1242 if (ret
== LOAD_NOSPACE
) {
1246 * Use a temporary map to do the work.
1248 copy_map
= vm_map_create(pmap_create(vm_map_round_page(macho_size
),
1250 get_map_min(map
), get_map_max(map
), TRUE
);
1251 if (VM_MAP_NULL
== copy_map
) {
1252 ret
= LOAD_RESOURCE
;
1256 myresult
= load_result_null
;
1258 ret
= parse_machfile(vp
, copy_map
, thread
, &header
,
1259 file_offset
, macho_size
,
1263 vm_map_deallocate(copy_map
);
1267 if (get_map_nentries(copy_map
) > 0) {
1269 dyl_start
= mach_get_vm_start(copy_map
);
1270 dyl_length
= mach_get_vm_end(copy_map
) - dyl_start
;
1272 map_addr
= dyl_start
;
1273 ret
= mach_vm_allocate(map
, &map_addr
, dyl_length
, VM_FLAGS_ANYWHERE
);
1275 if (ret
!= KERN_SUCCESS
) {
1276 vm_map_deallocate(copy_map
);
1282 ret
= vm_map_copyin(copy_map
,
1283 (vm_map_address_t
)dyl_start
,
1284 (vm_map_size_t
)dyl_length
,
1286 if (ret
!= KERN_SUCCESS
) {
1287 (void) vm_map_remove(map
,
1288 vm_map_trunc_page(map_addr
),
1289 vm_map_round_page(map_addr
+ dyl_length
),
1291 vm_map_deallocate(copy_map
);
1295 ret
= vm_map_copy_overwrite(map
,
1296 (vm_map_address_t
)map_addr
,
1298 if (ret
!= KERN_SUCCESS
) {
1299 vm_map_copy_discard(tmp
);
1300 (void) vm_map_remove(map
,
1301 vm_map_trunc_page(map_addr
),
1302 vm_map_round_page(map_addr
+ dyl_length
),
1304 vm_map_deallocate(copy_map
);
1308 if (map_addr
!= dyl_start
) {
1309 myresult
.entry_point
+= (map_addr
- dyl_start
);
1310 myresult
.all_image_info_addr
+=
1311 (map_addr
- dyl_start
);
1317 vm_map_deallocate(copy_map
);
1320 if (ret
== LOAD_SUCCESS
) {
1321 result
->dynlinker
= TRUE
;
1322 result
->entry_point
= myresult
.entry_point
;
1323 result
->all_image_info_addr
= myresult
.all_image_info_addr
;
1324 result
->all_image_info_size
= myresult
.all_image_info_size
;
1333 load_code_signature(
1334 struct linkedit_data_command
*lcp
,
1339 load_result_t
*result
)
1345 struct cs_blob
*blob
;
1347 vm_size_t blob_size
;
1352 if (lcp
->cmdsize
!= sizeof (struct linkedit_data_command
) ||
1353 lcp
->dataoff
+ lcp
->datasize
> macho_size
) {
1354 ret
= LOAD_BADMACHO
;
1358 blob
= ubc_cs_blob_get(vp
, cputype
, -1);
1360 /* we already have a blob for this vnode and cputype */
1361 if (blob
->csb_cpu_type
== cputype
&&
1362 blob
->csb_base_offset
== macho_offset
&&
1363 blob
->csb_mem_size
== lcp
->datasize
) {
1364 /* it matches the blob we want here: we're done */
1367 /* the blob has changed for this vnode: fail ! */
1368 ret
= LOAD_BADMACHO
;
1373 blob_size
= lcp
->datasize
;
1374 kr
= ubc_cs_blob_allocate(&addr
, &blob_size
);
1375 if (kr
!= KERN_SUCCESS
) {
1381 error
= vn_rdwr(UIO_READ
,
1385 macho_offset
+ lcp
->dataoff
,
1391 if (error
|| resid
!= 0) {
1396 if (ubc_cs_blob_add(vp
,
1404 /* ubc_cs_blob_add() has consumed "addr" */
1408 blob
= ubc_cs_blob_get(vp
, cputype
, -1);
1412 if (result
&& ret
== LOAD_SUCCESS
) {
1413 result
->csflags
|= blob
->csb_flags
;
1416 ubc_cs_blob_deallocate(addr
, blob_size
);
1424 #if CONFIG_CODE_DECRYPTION
1426 static load_return_t
1428 struct encryption_info_command
*eip
,
1434 char vpath
[MAXPATHLEN
];
1435 pager_crypt_info_t crypt_info
;
1436 const char * cryptname
= 0;
1439 struct segment_command_64
*seg64
;
1440 struct segment_command
*seg32
;
1441 vm_map_offset_t map_offset
, map_size
;
1444 if (eip
->cmdsize
< sizeof(*eip
))
1445 return LOAD_BADMACHO
;
1447 switch(eip
->cryptid
) {
1449 /* not encrypted, just an empty load command */
1450 return LOAD_SUCCESS
;
1452 cryptname
="com.apple.unfree";
1455 /* some random cryptid that you could manually put into
1456 * your binary if you want NULL */
1457 cryptname
="com.apple.null";
1460 return LOAD_BADMACHO
;
1464 result
= vn_getpath(vp
, vpath
, &len
);
1465 if(result
) return result
;
1467 /* set up decrypter first */
1468 if(NULL
==text_crypter_create
) return LOAD_FAILURE
;
1469 kr
=text_crypter_create(&crypt_info
, cryptname
, (void*)vpath
);
1472 printf("set_code_unprotect: unable to create decrypter %s, kr=%d\n",
1474 return LOAD_RESOURCE
;
1477 /* this is terrible, but we have to rescan the load commands to find the
1478 * virtual address of this encrypted stuff. This code is gonna look like
1479 * the dyld source one day... */
1480 struct mach_header
*header
= (struct mach_header
*)addr
;
1481 size_t mach_header_sz
= sizeof(struct mach_header
);
1482 if (header
->magic
== MH_MAGIC_64
||
1483 header
->magic
== MH_CIGAM_64
) {
1484 mach_header_sz
= sizeof(struct mach_header_64
);
1486 offset
= mach_header_sz
;
1487 uint32_t ncmds
= header
->ncmds
;
1490 * Get a pointer to the command.
1492 struct load_command
*lcp
= (struct load_command
*)(addr
+ offset
);
1493 offset
+= lcp
->cmdsize
;
1497 seg64
= (struct segment_command_64
*)lcp
;
1498 if ((seg64
->fileoff
<= eip
->cryptoff
) &&
1499 (seg64
->fileoff
+seg64
->filesize
>=
1500 eip
->cryptoff
+eip
->cryptsize
)) {
1501 map_offset
= seg64
->vmaddr
+ eip
->cryptoff
- seg64
->fileoff
;
1502 map_size
= eip
->cryptsize
;
1506 seg32
= (struct segment_command
*)lcp
;
1507 if ((seg32
->fileoff
<= eip
->cryptoff
) &&
1508 (seg32
->fileoff
+seg32
->filesize
>=
1509 eip
->cryptoff
+eip
->cryptsize
)) {
1510 map_offset
= seg32
->vmaddr
+ eip
->cryptoff
- seg32
->fileoff
;
1511 map_size
= eip
->cryptsize
;
1517 /* if we get here, did not find anything */
1518 return LOAD_BADMACHO
;
1521 /* now remap using the decrypter */
1522 kr
= vm_map_apple_protected(map
, map_offset
, map_offset
+map_size
, &crypt_info
);
1524 printf("set_code_unprotect(): mapping failed with %x\n", kr
);
1525 crypt_info
.crypt_end(crypt_info
.crypt_ops
);
1526 return LOAD_PROTECT
;
1529 return LOAD_SUCCESS
;
1535 * This routine exists to support the load_dylinker().
1537 * This routine has its own, separate, understanding of the FAT file format,
1538 * which is terrifically unfortunate.
1545 struct mach_header
*mach_header
,
1552 vfs_context_t ctx
= vfs_context_current();
1553 proc_t p
= vfs_context_proc(ctx
);
1554 kauth_cred_t kerncred
;
1555 struct nameidata nid
, *ndp
;
1557 struct fat_arch fat_arch
;
1558 int error
= LOAD_SUCCESS
;
1561 struct mach_header mach_header
;
1562 struct fat_header fat_header
;
1565 off_t fsize
= (off_t
)0;
1569 * Capture the kernel credential for use in the actual read of the
1570 * file, since the user doing the execution may have execute rights
1571 * but not read rights, but to exec something, we have to either map
1572 * or read it into the new process address space, which requires
1573 * read rights. This is to deal with lack of common credential
1574 * serialization code which would treat NOCRED as "serialize 'root'".
1576 kerncred
= vfs_context_ucred(vfs_context_kernel());
1580 /* init the namei data to point the file user's program name */
1581 NDINIT(ndp
, LOOKUP
, FOLLOW
| LOCKLEAF
, UIO_SYSSPACE
, CAST_USER_ADDR_T(path
), ctx
);
1583 if ((error
= namei(ndp
)) != 0) {
1584 if (error
== ENOENT
) {
1585 error
= LOAD_ENOENT
;
1587 error
= LOAD_FAILURE
;
1594 /* check for regular file */
1595 if (vp
->v_type
!= VREG
) {
1596 error
= LOAD_PROTECT
;
1601 if ((error
= vnode_size(vp
, &fsize
, ctx
)) != 0) {
1602 error
= LOAD_FAILURE
;
1606 /* Check mount point */
1607 if (vp
->v_mount
->mnt_flag
& MNT_NOEXEC
) {
1608 error
= LOAD_PROTECT
;
1613 if ((error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_EXECUTE
, ctx
)) != 0) {
1614 error
= LOAD_PROTECT
;
1618 /* try to open it */
1619 if ((error
= VNOP_OPEN(vp
, FREAD
, ctx
)) != 0) {
1620 error
= LOAD_PROTECT
;
1624 if ((error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)&header
, sizeof(header
), 0,
1625 UIO_SYSSPACE
, IO_NODELOCKED
, kerncred
, &resid
, p
)) != 0) {
1626 error
= LOAD_IOERROR
;
1630 if (header
.mach_header
.magic
== MH_MAGIC
||
1631 header
.mach_header
.magic
== MH_MAGIC_64
)
1633 else if (header
.fat_header
.magic
== FAT_MAGIC
||
1634 header
.fat_header
.magic
== FAT_CIGAM
)
1637 error
= LOAD_BADMACHO
;
1642 /* Look up our architecture in the fat file. */
1643 error
= fatfile_getarch_with_bits(vp
, archbits
, (vm_offset_t
)(&header
.fat_header
), &fat_arch
);
1644 if (error
!= LOAD_SUCCESS
)
1647 /* Read the Mach-O header out of it */
1648 error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)&header
.mach_header
,
1649 sizeof(header
.mach_header
), fat_arch
.offset
,
1650 UIO_SYSSPACE
, IO_NODELOCKED
, kerncred
, &resid
, p
);
1652 error
= LOAD_IOERROR
;
1656 /* Is this really a Mach-O? */
1657 if (header
.mach_header
.magic
!= MH_MAGIC
&&
1658 header
.mach_header
.magic
!= MH_MAGIC_64
) {
1659 error
= LOAD_BADMACHO
;
1663 *file_offset
= fat_arch
.offset
;
1664 *macho_size
= fat_arch
.size
;
1667 * Force get_macho_vnode() to fail if the architecture bits
1668 * do not match the expected architecture bits. This in
1669 * turn causes load_dylinker() to fail for the same reason,
1670 * so it ensures the dynamic linker and the binary are in
1671 * lock-step. This is potentially bad, if we ever add to
1672 * the CPU_ARCH_* bits any bits that are desirable but not
1673 * required, since the dynamic linker might work, but we will
1674 * refuse to load it because of this check.
1676 if ((cpu_type_t
)(header
.mach_header
.cputype
& CPU_ARCH_MASK
) != archbits
)
1677 return(LOAD_BADARCH
);
1680 *macho_size
= fsize
;
1683 *mach_header
= header
.mach_header
;
1686 ubc_setsize(vp
, fsize
);
1691 err2
= VNOP_CLOSE(vp
, FREAD
, ctx
);