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/kalloc.h>
67 #include <kern/task.h>
68 #include <kern/thread.h>
69 #include <kern/page_decrypt.h>
71 #include <mach-o/fat.h>
72 #include <mach-o/loader.h>
75 #include <vm/vm_map.h>
76 #include <vm/vm_kern.h>
77 #include <vm/vm_pager.h>
78 #include <vm/vnode_pager.h>
79 #include <vm/vm_protos.h>
82 * XXX vm/pmap.h should not treat these prototypes as MACH_KERNEL_PRIVATE
83 * when KERNEL is defined.
85 extern pmap_t
pmap_create(vm_map_size_t size
, boolean_t is_64bit
);
86 extern void pmap_switch(pmap_t
);
89 * XXX kern/thread.h should not treat these prototypes as MACH_KERNEL_PRIVATE
90 * when KERNEL is defined.
92 extern kern_return_t
thread_setstatus(thread_t thread
, int flavor
,
93 thread_state_t tstate
,
94 mach_msg_type_number_t count
);
96 extern kern_return_t
thread_state_initialize(thread_t thread
);
99 /* XXX should have prototypes in a shared header file */
100 extern int get_map_nentries(vm_map_t
);
101 extern kern_return_t
thread_userstack(thread_t
, int, thread_state_t
,
102 unsigned int, mach_vm_offset_t
*, int *);
103 extern kern_return_t
thread_entrypoint(thread_t
, int, thread_state_t
,
104 unsigned int, mach_vm_offset_t
*);
106 extern kern_return_t
memory_object_signed(memory_object_control_t control
,
107 boolean_t is_signed
);
109 /* An empty load_result_t */
110 static load_result_t load_result_null
= {
111 .mach_header
= MACH_VM_MIN_ADDRESS
,
112 .entry_point
= MACH_VM_MIN_ADDRESS
,
113 .user_stack
= MACH_VM_MIN_ADDRESS
,
122 * Prototypes of static functions.
129 struct mach_header
*header
,
133 load_result_t
*result
138 struct segment_command
*scp
,
144 load_result_t
*result
149 struct segment_command_64
*scp64
,
155 load_result_t
*result
158 int load_code_signature(
159 struct linkedit_data_command
*lcp
,
164 load_result_t
*result
);
166 #if CONFIG_CODE_DECRYPTION
169 struct encryption_info_command
*lcp
,
177 struct thread_command
*tcp
,
179 load_result_t
*result
184 struct thread_command
*tcp
,
186 load_result_t
*result
193 unsigned long total_size
200 unsigned long total_size
,
201 user_addr_t
*user_stack
,
209 unsigned long total_size
,
210 mach_vm_offset_t
*entry_point
215 struct dylinker_command
*lcp
,
220 load_result_t
*result
,
228 struct mach_header
*mach_header
,
236 struct image_params
*imgp
,
237 struct mach_header
*header
,
240 load_result_t
*result
243 struct vnode
*vp
= imgp
->ip_vp
;
244 off_t file_offset
= imgp
->ip_arch_offset
;
245 off_t macho_size
= imgp
->ip_arch_size
;
247 pmap_t pmap
= 0; /* protected by create_map */
250 load_result_t myresult
;
252 boolean_t create_map
= TRUE
;
254 if (new_map
!= VM_MAP_NULL
) {
259 old_map
= current_map();
260 pmap
= pmap_create((vm_map_size_t
) 0, (imgp
->ip_flags
& IMGPF_IS_64BIT
));
261 map
= vm_map_create(pmap
,
263 vm_compute_max_offset((imgp
->ip_flags
& IMGPF_IS_64BIT
)),
268 if ( (header
->flags
& MH_ALLOW_STACK_EXECUTION
) )
269 vm_map_disable_NX(map
);
274 *result
= load_result_null
;
276 lret
= parse_machfile(vp
, map
, thread
, header
, file_offset
, macho_size
,
279 if (lret
!= LOAD_SUCCESS
) {
281 vm_map_deallocate(map
); /* will lose pmap reference too */
287 * For 64-bit users, check for presence of a 4GB page zero
288 * which will enable the kernel to share the user's address space
289 * and hence avoid TLB flushes on kernel entry/exit
291 if ((imgp
->ip_flags
& IMGPF_IS_64BIT
) &&
292 vm_map_has_4GB_pagezero(map
))
293 vm_map_set_4GB_pagezero(map
);
296 * Commit to new map. First make sure that the current
297 * users of the task get done with it, and that we clean
298 * up the old contents of IPC and memory. The task is
299 * guaranteed to be single threaded upon return (us).
301 * Swap the new map for the old, which consumes our new map
302 * reference but each leaves us responsible for the old_map reference.
303 * That lets us get off the pmap associated with it, and
304 * then we can release it.
308 task_halt(current_task());
310 old_map
= swap_task_map(current_task(), map
);
311 vm_map_clear_4GB_pagezero(old_map
);
312 pmap_switch(pmap
); /* Make sure we are using the new pmap */
313 vm_map_deallocate(old_map
);
315 return(LOAD_SUCCESS
);
319 * The file size of a mach-o file is limited to 32 bits; this is because
320 * this is the limit on the kalloc() of enough bytes for a mach_header and
321 * the contents of its sizeofcmds, which is currently constrained to 32
322 * bits in the file format itself. We read into the kernel buffer the
323 * commands section, and then parse it in order to parse the mach-o file
324 * format load_command segment(s). We are only interested in a subset of
325 * the total set of possible commands.
333 struct mach_header
*header
,
337 load_result_t
*result
341 struct load_command
*lcp
;
342 struct dylinker_command
*dlp
= 0;
343 integer_t dlarchbits
= 0;
345 load_return_t ret
= LOAD_SUCCESS
;
348 vm_size_t size
,kl_size
;
350 size_t oldoffset
; /* for overflow check */
352 proc_t p
= current_proc(); /* XXXX */
356 size_t mach_header_sz
= sizeof(struct mach_header
);
358 boolean_t got_code_signatures
= FALSE
;
360 if (header
->magic
== MH_MAGIC_64
||
361 header
->magic
== MH_CIGAM_64
) {
362 mach_header_sz
= sizeof(struct mach_header_64
);
366 * Break infinite recursion
369 return(LOAD_FAILURE
);
372 task
= (task_t
)get_threadtask(thread
);
377 * Check to see if right machine type.
379 if (((cpu_type_t
)(header
->cputype
& ~CPU_ARCH_MASK
) != cpu_type()) ||
380 !grade_binary(header
->cputype
,
381 header
->cpusubtype
& ~CPU_SUBTYPE_MASK
))
382 return(LOAD_BADARCH
);
384 abi64
= ((header
->cputype
& CPU_ARCH_ABI64
) == CPU_ARCH_ABI64
);
386 switch (header
->filetype
) {
392 return (LOAD_FAILURE
);
399 return (LOAD_FAILURE
);
405 return (LOAD_FAILURE
);
410 return (LOAD_FAILURE
);
414 * Get the pager for the file.
416 pager
= (void *) ubc_getpager(vp
);
419 * Map portion that must be accessible directly into
422 if ((mach_header_sz
+ header
->sizeofcmds
) > macho_size
)
423 return(LOAD_BADMACHO
);
426 * Round size of Mach-O commands up to page boundry.
428 size
= round_page(mach_header_sz
+ header
->sizeofcmds
);
430 return(LOAD_BADMACHO
);
433 * Map the load commands into kernel memory.
437 kl_addr
= kalloc(size
);
438 addr
= (caddr_t
)kl_addr
;
440 return(LOAD_NOSPACE
);
442 error
= vn_rdwr(UIO_READ
, vp
, addr
, size
, file_offset
,
443 UIO_SYSSPACE32
, 0, kauth_cred_get(), &resid
, p
);
446 kfree(kl_addr
, kl_size
);
447 return(LOAD_IOERROR
);
451 * Scan through the commands, processing each one as necessary.
453 for (pass
= 1; pass
<= 2; pass
++) {
455 * Loop through each of the load_commands indicated by the
456 * Mach-O header; if an absurd value is provided, we just
457 * run off the end of the reserved section by incrementing
458 * the offset too far, so we are implicitly fail-safe.
460 offset
= mach_header_sz
;
461 ncmds
= header
->ncmds
;
464 * Get a pointer to the command.
466 lcp
= (struct load_command
*)(addr
+ offset
);
468 offset
+= lcp
->cmdsize
;
471 * Perform prevalidation of the struct load_command
472 * before we attempt to use its contents. Invalid
473 * values are ones which result in an overflow, or
474 * which can not possibly be valid commands, or which
475 * straddle or exist past the reserved section at the
476 * start of the image.
478 if (oldoffset
> offset
||
479 lcp
->cmdsize
< sizeof(struct load_command
) ||
480 offset
> header
->sizeofcmds
+ mach_header_sz
) {
486 * Act on struct load_command's for which kernel
487 * intervention is required.
493 ret
= load_segment_64(
494 (struct segment_command_64
*)lcp
,
506 (struct segment_command
*) lcp
,
517 ret
= load_thread((struct thread_command
*)lcp
,
524 ret
= load_unixthread(
525 (struct thread_command
*) lcp
,
529 case LC_LOAD_DYLINKER
:
532 if ((depth
== 1) && (dlp
== 0)) {
533 dlp
= (struct dylinker_command
*)lcp
;
534 dlarchbits
= (header
->cputype
& CPU_ARCH_MASK
);
539 case LC_CODE_SIGNATURE
:
544 load signatures & store in uip
545 set VM object "signed_pages"
547 ret
= load_code_signature(
548 (struct linkedit_data_command
*) lcp
,
553 (depth
== 1) ? result
: NULL
);
554 if (ret
!= LOAD_SUCCESS
) {
555 printf("proc %d: load code signature error %d "
557 p
->p_pid
, ret
, vp
->v_name
);
558 ret
= LOAD_SUCCESS
; /* ignore error */
560 got_code_signatures
= TRUE
;
563 #if CONFIG_CODE_DECRYPTION
564 case LC_ENCRYPTION_INFO
:
567 ret
= set_code_unprotect(
568 (struct encryption_info_command
*) lcp
,
570 if (ret
!= LOAD_SUCCESS
) {
571 printf("proc %d: set unprotect error %d "
573 p
->p_pid
, ret
, vp
->v_name
);
574 ret
= LOAD_SUCCESS
; /* ignore error */
579 /* Other commands are ignored by the kernel */
583 if (ret
!= LOAD_SUCCESS
)
586 if (ret
!= LOAD_SUCCESS
)
589 if (ret
== LOAD_SUCCESS
) {
590 if (! got_code_signatures
) {
591 struct cs_blob
*blob
;
592 /* no embedded signatures: look for detached ones */
593 blob
= ubc_cs_blob_get(vp
, -1, file_offset
);
595 /* get flags to be applied to the process */
596 result
->csflags
|= blob
->csb_flags
;
601 ret
= load_dylinker(dlp
, dlarchbits
, map
, thread
, depth
, result
, abi64
);
604 if (result
->thread_count
== 0) {
606 } else if ( abi64
) {
608 /* Map in 64-bit commpage */
609 /* LP64todo - make this clean */
611 * PPC51: ppc64 is limited to 51-bit addresses.
612 * Memory above that limit is handled specially
615 pmap_map_sharedpage(current_task(), get_map_pmap(map
));
622 kfree(kl_addr
, kl_size
);
627 #if CONFIG_CODE_DECRYPTION
629 #define APPLE_UNPROTECTED_HEADER_SIZE (3 * PAGE_SIZE_64)
632 unprotect_segment_64(
636 vm_map_offset_t map_addr
,
637 vm_map_size_t map_size
)
642 * The first APPLE_UNPROTECTED_HEADER_SIZE bytes (from offset 0 of
643 * this part of a Universal binary) are not protected...
644 * The rest needs to be "transformed".
646 if (file_off
<= APPLE_UNPROTECTED_HEADER_SIZE
&&
647 file_off
+ file_size
<= APPLE_UNPROTECTED_HEADER_SIZE
) {
648 /* it's all unprotected, nothing to do... */
651 if (file_off
<= APPLE_UNPROTECTED_HEADER_SIZE
) {
653 * We start mapping in the unprotected area.
654 * Skip the unprotected part...
656 vm_map_offset_t delta
;
658 delta
= APPLE_UNPROTECTED_HEADER_SIZE
;
663 /* ... transform the rest of the mapping. */
664 struct pager_crypt_info crypt_info
;
665 crypt_info
.page_decrypt
= dsmos_page_transform
;
666 crypt_info
.crypt_ops
= NULL
;
667 crypt_info
.crypt_end
= NULL
;
668 kr
= vm_map_apple_protected(map
,
674 if (kr
!= KERN_SUCCESS
) {
679 #else /* CONFIG_CODE_DECRYPTION */
680 #define unprotect_segment_64(file_off, file_size, map, map_addr, map_size) \
682 #endif /* CONFIG_CODE_DECRYPTION */
687 struct segment_command
*scp
,
691 __unused off_t end_of_file
,
693 load_result_t
*result
697 vm_offset_t map_addr
, map_offset
;
698 vm_size_t map_size
, seg_size
, delta_size
;
703 * Make sure what we get from the file is really ours (as specified
706 if (scp
->fileoff
+ scp
->filesize
> macho_size
)
707 return (LOAD_BADMACHO
);
709 * Make sure the segment is page-aligned in the file.
711 if ((scp
->fileoff
& PAGE_MASK
) != 0)
712 return LOAD_BADMACHO
;
714 seg_size
= round_page(scp
->vmsize
);
716 return(KERN_SUCCESS
);
719 * Round sizes to page size.
721 map_size
= round_page(scp
->filesize
);
722 map_addr
= trunc_page(scp
->vmaddr
);
724 #if 0 /* XXX (4596982) this interferes with Rosetta */
728 (scp
->initprot
& VM_PROT_ALL
) == VM_PROT_NONE
&&
729 (scp
->maxprot
& VM_PROT_ALL
) == VM_PROT_NONE
) {
731 * This is a "page zero" segment: it starts at address 0,
732 * is not mapped from the binary file and is not accessible.
733 * User-space should never be able to access that memory, so
734 * make it completely off limits by raising the VM map's
737 ret
= vm_map_raise_min_offset(map
, (vm_map_offset_t
) seg_size
);
738 if (ret
!= KERN_SUCCESS
) {
745 map_offset
= pager_offset
+ scp
->fileoff
;
748 initprot
= (scp
->initprot
) & VM_PROT_ALL
;
749 maxprot
= (scp
->maxprot
) & VM_PROT_ALL
;
751 * Map a copy of the file into the address space.
754 &map_addr
, map_size
, (vm_offset_t
)0,
755 VM_FLAGS_FIXED
, pager
, map_offset
, TRUE
,
758 if (ret
!= KERN_SUCCESS
)
759 return(LOAD_NOSPACE
);
762 * If the file didn't end on a page boundary,
763 * we need to zero the leftover.
765 delta_size
= map_size
- scp
->filesize
;
767 if (delta_size
> 0) {
770 ret
= vm_allocate(kernel_map
, &tmp
, delta_size
, VM_FLAGS_ANYWHERE
);
771 if (ret
!= KERN_SUCCESS
)
772 return(LOAD_RESOURCE
);
774 if (copyout(tmp
, map_addr
+ scp
->filesize
,
776 (void) vm_deallocate(
777 kernel_map
, tmp
, delta_size
);
778 return(LOAD_FAILURE
);
781 (void) vm_deallocate(kernel_map
, tmp
, delta_size
);
787 * If the virtual size of the segment is greater
788 * than the size from the file, we need to allocate
789 * zero fill memory for the rest.
791 delta_size
= seg_size
- map_size
;
792 if (delta_size
> 0) {
793 vm_offset_t tmp
= map_addr
+ map_size
;
795 ret
= vm_map(map
, &tmp
, delta_size
, 0, VM_FLAGS_FIXED
,
797 scp
->initprot
, scp
->maxprot
,
799 if (ret
!= KERN_SUCCESS
)
800 return(LOAD_NOSPACE
);
803 if ( (scp
->fileoff
== 0) && (scp
->filesize
!= 0) )
804 result
->mach_header
= map_addr
;
806 if (scp
->flags
& SG_PROTECTED_VERSION_1
) {
807 ret
= unprotect_segment_64((uint64_t) scp
->fileoff
,
808 (uint64_t) scp
->filesize
,
810 (vm_map_offset_t
) map_addr
,
811 (vm_map_size_t
) map_size
);
822 struct segment_command_64
*scp64
,
826 __unused off_t end_of_file
,
828 load_result_t
*result
832 mach_vm_offset_t map_addr
, map_offset
;
833 mach_vm_size_t map_size
, seg_size
, delta_size
;
838 * Make sure what we get from the file is really ours (as specified
841 if (scp64
->fileoff
+ scp64
->filesize
> (uint64_t)macho_size
)
842 return (LOAD_BADMACHO
);
844 * Make sure the segment is page-aligned in the file.
846 if ((scp64
->fileoff
& PAGE_MASK_64
) != 0)
847 return LOAD_BADMACHO
;
849 seg_size
= round_page_64(scp64
->vmsize
);
851 return(KERN_SUCCESS
);
854 * Round sizes to page size.
856 map_size
= round_page_64(scp64
->filesize
); /* limited to 32 bits */
857 map_addr
= round_page_64(scp64
->vmaddr
);
862 (scp64
->initprot
& VM_PROT_ALL
) == VM_PROT_NONE
&&
863 (scp64
->maxprot
& VM_PROT_ALL
) == VM_PROT_NONE
) {
865 * This is a "page zero" segment: it starts at address 0,
866 * is not mapped from the binary file and is not accessible.
867 * User-space should never be able to access that memory, so
868 * make it completely off limits by raising the VM map's
871 ret
= vm_map_raise_min_offset(map
, seg_size
);
872 if (ret
!= KERN_SUCCESS
) {
878 map_offset
= pager_offset
+ scp64
->fileoff
; /* limited to 32 bits */
881 initprot
= (scp64
->initprot
) & VM_PROT_ALL
;
882 maxprot
= (scp64
->maxprot
) & VM_PROT_ALL
;
884 * Map a copy of the file into the address space.
886 ret
= mach_vm_map(map
,
887 &map_addr
, map_size
, (mach_vm_offset_t
)0,
888 VM_FLAGS_FIXED
, pager
, map_offset
, TRUE
,
891 if (ret
!= KERN_SUCCESS
)
892 return(LOAD_NOSPACE
);
895 * If the file didn't end on a page boundary,
896 * we need to zero the leftover.
898 delta_size
= map_size
- scp64
->filesize
;
900 if (delta_size
> 0) {
901 mach_vm_offset_t tmp
;
903 ret
= vm_allocate(kernel_map
, &tmp
, delta_size
, VM_FLAGS_ANYWHERE
);
904 if (ret
!= KERN_SUCCESS
)
905 return(LOAD_RESOURCE
);
907 if (copyout(tmp
, map_addr
+ scp64
->filesize
,
909 (void) vm_deallocate(
910 kernel_map
, tmp
, delta_size
);
911 return (LOAD_FAILURE
);
914 (void) vm_deallocate(kernel_map
, tmp
, delta_size
);
920 * If the virtual size of the segment is greater
921 * than the size from the file, we need to allocate
922 * zero fill memory for the rest.
924 delta_size
= seg_size
- map_size
;
925 if (delta_size
> 0) {
926 mach_vm_offset_t tmp
= map_addr
+ map_size
;
928 ret
= mach_vm_map(map
, &tmp
, delta_size
, 0, VM_FLAGS_FIXED
,
930 scp64
->initprot
, scp64
->maxprot
,
932 if (ret
!= KERN_SUCCESS
)
933 return(LOAD_NOSPACE
);
936 if ( (scp64
->fileoff
== 0) && (scp64
->filesize
!= 0) )
937 result
->mach_header
= map_addr
;
939 if (scp64
->flags
& SG_PROTECTED_VERSION_1
) {
940 ret
= unprotect_segment_64(scp64
->fileoff
,
955 struct thread_command
*tcp
,
957 load_result_t
*result
965 task
= get_threadtask(thread
);
967 /* if count is 0; same as thread */
968 if (result
->thread_count
!= 0) {
969 kret
= thread_create(task
, &thread
);
970 if (kret
!= KERN_SUCCESS
)
971 return(LOAD_RESOURCE
);
972 thread_deallocate(thread
);
975 lret
= load_threadstate(thread
,
976 (unsigned long *)(((vm_offset_t
)tcp
) +
977 sizeof(struct thread_command
)),
978 tcp
->cmdsize
- sizeof(struct thread_command
));
979 if (lret
!= LOAD_SUCCESS
)
982 if (result
->thread_count
== 0) {
983 lret
= load_threadstack(thread
,
984 (unsigned long *)(((vm_offset_t
)tcp
) +
985 sizeof(struct thread_command
)),
986 tcp
->cmdsize
- sizeof(struct thread_command
),
990 result
->customstack
= 1;
992 result
->customstack
= 0;
994 if (lret
!= LOAD_SUCCESS
)
997 lret
= load_threadentry(thread
,
998 (unsigned long *)(((vm_offset_t
)tcp
) +
999 sizeof(struct thread_command
)),
1000 tcp
->cmdsize
- sizeof(struct thread_command
),
1001 &result
->entry_point
);
1002 if (lret
!= LOAD_SUCCESS
)
1006 * Resume thread now, note that this means that the thread
1007 * commands should appear after all the load commands to
1008 * be sure they don't reference anything not yet mapped.
1011 thread_resume(thread
);
1013 result
->thread_count
++;
1015 return(LOAD_SUCCESS
);
1021 struct thread_command
*tcp
,
1023 load_result_t
*result
1029 if (result
->thread_count
!= 0) {
1030 printf("load_unixthread: already have a thread!");
1031 return (LOAD_FAILURE
);
1034 ret
= load_threadstack(thread
,
1035 (unsigned long *)(((vm_offset_t
)tcp
) +
1036 sizeof(struct thread_command
)),
1037 tcp
->cmdsize
- sizeof(struct thread_command
),
1038 &result
->user_stack
,
1040 if (ret
!= LOAD_SUCCESS
)
1044 result
->customstack
= 1;
1046 result
->customstack
= 0;
1047 ret
= load_threadentry(thread
,
1048 (unsigned long *)(((vm_offset_t
)tcp
) +
1049 sizeof(struct thread_command
)),
1050 tcp
->cmdsize
- sizeof(struct thread_command
),
1051 &result
->entry_point
);
1052 if (ret
!= LOAD_SUCCESS
)
1055 ret
= load_threadstate(thread
,
1056 (unsigned long *)(((vm_offset_t
)tcp
) +
1057 sizeof(struct thread_command
)),
1058 tcp
->cmdsize
- sizeof(struct thread_command
));
1059 if (ret
!= LOAD_SUCCESS
)
1062 result
->unixproc
= TRUE
;
1063 result
->thread_count
++;
1065 return(LOAD_SUCCESS
);
1073 unsigned long total_size
1079 unsigned long thread_size
;
1081 ret
= thread_state_initialize( thread
);
1082 if (ret
!= KERN_SUCCESS
) {
1083 return(LOAD_FAILURE
);
1087 * Set the new thread state; iterate through the state flavors in
1090 while (total_size
> 0) {
1093 thread_size
= (size
+2)*sizeof(unsigned long);
1094 if (thread_size
> total_size
)
1095 return(LOAD_BADMACHO
);
1096 total_size
-= thread_size
;
1098 * Third argument is a kernel space pointer; it gets cast
1099 * to the appropriate type in machine_thread_set_state()
1100 * based on the value of flavor.
1102 ret
= thread_setstatus(thread
, flavor
, (thread_state_t
)ts
, size
);
1103 if (ret
!= KERN_SUCCESS
) {
1104 return(LOAD_FAILURE
);
1106 ts
+= size
; /* ts is a (unsigned long *) */
1108 return(LOAD_SUCCESS
);
1116 unsigned long total_size
,
1117 user_addr_t
*user_stack
,
1124 unsigned long stack_size
;
1126 while (total_size
> 0) {
1129 stack_size
= (size
+2)*sizeof(unsigned long);
1130 if (stack_size
> total_size
)
1131 return(LOAD_BADMACHO
);
1132 total_size
-= stack_size
;
1135 * Third argument is a kernel space pointer; it gets cast
1136 * to the appropriate type in thread_userstack() based on
1137 * the value of flavor.
1139 ret
= thread_userstack(thread
, flavor
, (thread_state_t
)ts
, size
, user_stack
, customstack
);
1140 if (ret
!= KERN_SUCCESS
) {
1141 return(LOAD_FAILURE
);
1143 ts
+= size
; /* ts is a (unsigned long *) */
1145 return(LOAD_SUCCESS
);
1153 unsigned long total_size
,
1154 mach_vm_offset_t
*entry_point
1160 unsigned long entry_size
;
1163 * Set the thread state.
1165 *entry_point
= MACH_VM_MIN_ADDRESS
;
1166 while (total_size
> 0) {
1169 entry_size
= (size
+2)*sizeof(unsigned long);
1170 if (entry_size
> total_size
)
1171 return(LOAD_BADMACHO
);
1172 total_size
-= entry_size
;
1174 * Third argument is a kernel space pointer; it gets cast
1175 * to the appropriate type in thread_entrypoint() based on
1176 * the value of flavor.
1178 ret
= thread_entrypoint(thread
, flavor
, (thread_state_t
)ts
, size
, entry_point
);
1179 if (ret
!= KERN_SUCCESS
) {
1180 return(LOAD_FAILURE
);
1182 ts
+= size
; /* ts is a (unsigned long *) */
1184 return(LOAD_SUCCESS
);
1191 struct dylinker_command
*lcp
,
1196 load_result_t
*result
,
1202 struct vnode
*vp
= NULLVP
; /* set by get_macho_vnode() */
1203 struct mach_header header
;
1204 off_t file_offset
= 0; /* set by get_macho_vnode() */
1205 off_t macho_size
= 0; /* set by get_macho_vnode() */
1207 load_result_t myresult
;
1210 mach_vm_offset_t dyl_start
, map_addr
;
1211 mach_vm_size_t dyl_length
;
1213 name
= (char *)lcp
+ lcp
->name
.offset
;
1215 * Check for a proper null terminated string.
1219 if (p
>= (char *)lcp
+ lcp
->cmdsize
)
1220 return(LOAD_BADMACHO
);
1223 ret
= get_macho_vnode(name
, archbits
, &header
, &file_offset
, &macho_size
, &vp
);
1227 myresult
= load_result_null
;
1230 * First try to map dyld in directly. This should work most of
1231 * the time since there shouldn't normally be something already
1232 * mapped to its address.
1235 ret
= parse_machfile(vp
, map
, thread
, &header
, file_offset
, macho_size
,
1239 * If it turned out something was in the way, then we'll take
1240 * take this longer path to map dyld into a temporary map and
1241 * copy it into destination map at a different address.
1244 if (ret
== LOAD_NOSPACE
) {
1248 * Use a temporary map to do the work.
1250 copy_map
= vm_map_create(pmap_create(vm_map_round_page(macho_size
),
1252 get_map_min(map
), get_map_max(map
), TRUE
);
1253 if (VM_MAP_NULL
== copy_map
) {
1254 ret
= LOAD_RESOURCE
;
1258 myresult
= load_result_null
;
1260 ret
= parse_machfile(vp
, copy_map
, thread
, &header
,
1261 file_offset
, macho_size
,
1265 vm_map_deallocate(copy_map
);
1269 if (get_map_nentries(copy_map
) > 0) {
1271 dyl_start
= mach_get_vm_start(copy_map
);
1272 dyl_length
= mach_get_vm_end(copy_map
) - dyl_start
;
1274 map_addr
= dyl_start
;
1275 ret
= mach_vm_allocate(map
, &map_addr
, dyl_length
, VM_FLAGS_ANYWHERE
);
1277 if (ret
!= KERN_SUCCESS
) {
1278 vm_map_deallocate(copy_map
);
1284 ret
= vm_map_copyin(copy_map
,
1285 (vm_map_address_t
)dyl_start
,
1286 (vm_map_size_t
)dyl_length
,
1288 if (ret
!= KERN_SUCCESS
) {
1289 (void) vm_map_remove(map
,
1290 vm_map_trunc_page(map_addr
),
1291 vm_map_round_page(map_addr
+ dyl_length
),
1293 vm_map_deallocate(copy_map
);
1297 ret
= vm_map_copy_overwrite(map
,
1298 (vm_map_address_t
)map_addr
,
1300 if (ret
!= KERN_SUCCESS
) {
1301 vm_map_copy_discard(tmp
);
1302 (void) vm_map_remove(map
,
1303 vm_map_trunc_page(map_addr
),
1304 vm_map_round_page(map_addr
+ dyl_length
),
1306 vm_map_deallocate(copy_map
);
1310 if (map_addr
!= dyl_start
)
1311 myresult
.entry_point
+= (map_addr
- dyl_start
);
1316 vm_map_deallocate(copy_map
);
1319 if (ret
== LOAD_SUCCESS
) {
1320 result
->dynlinker
= TRUE
;
1321 result
->entry_point
= myresult
.entry_point
;
1330 load_code_signature(
1331 struct linkedit_data_command
*lcp
,
1336 load_result_t
*result
)
1342 struct cs_blob
*blob
;
1344 vm_size_t blob_size
;
1349 if (lcp
->cmdsize
!= sizeof (struct linkedit_data_command
) ||
1350 lcp
->dataoff
+ lcp
->datasize
> macho_size
) {
1351 ret
= LOAD_BADMACHO
;
1355 blob
= ubc_cs_blob_get(vp
, cputype
, -1);
1357 /* we already have a blob for this vnode and cputype */
1358 if (blob
->csb_cpu_type
== cputype
&&
1359 blob
->csb_base_offset
== macho_offset
&&
1360 blob
->csb_mem_size
== lcp
->datasize
) {
1361 /* it matches the blob we want here: we're done */
1364 /* the blob has changed for this vnode: fail ! */
1365 ret
= LOAD_BADMACHO
;
1370 blob_size
= lcp
->datasize
;
1371 kr
= ubc_cs_blob_allocate(&addr
, &blob_size
);
1372 if (kr
!= KERN_SUCCESS
) {
1378 error
= vn_rdwr(UIO_READ
,
1382 macho_offset
+ lcp
->dataoff
,
1388 if (error
|| resid
!= 0) {
1393 if (ubc_cs_blob_add(vp
,
1401 /* ubc_cs_blob_add() has consumed "addr" */
1405 blob
= ubc_cs_blob_get(vp
, cputype
, -1);
1409 if (result
&& ret
== LOAD_SUCCESS
) {
1410 result
->csflags
|= blob
->csb_flags
;
1413 ubc_cs_blob_deallocate(addr
, blob_size
);
1421 #if CONFIG_CODE_DECRYPTION
1423 static load_return_t
1425 struct encryption_info_command
*eip
,
1431 char vpath
[MAXPATHLEN
];
1432 pager_crypt_info_t crypt_info
;
1433 const char * cryptname
= 0;
1436 struct segment_command_64
*seg64
;
1437 struct segment_command
*seg32
;
1438 vm_map_offset_t map_offset
, map_size
;
1441 switch(eip
->cryptid
) {
1443 /* not encrypted, just an empty load command */
1444 return LOAD_SUCCESS
;
1446 cryptname
="com.apple.unfree";
1449 /* some random cryptid that you could manually put into
1450 * your binary if you want NULL */
1451 cryptname
="com.apple.null";
1454 return LOAD_FAILURE
;
1458 result
= vn_getpath(vp
, vpath
, &len
);
1459 if(result
) return result
;
1461 /* set up decrypter first */
1462 if(NULL
==text_crypter_create
) return LOAD_FAILURE
;
1463 kr
=text_crypter_create(&crypt_info
, cryptname
, (void*)vpath
);
1466 printf("set_code_unprotect: unable to find decrypter %s, kr=%d\n",
1468 return LOAD_FAILURE
;
1471 /* this is terrible, but we have to rescan the load commands to find the
1472 * virtual address of this encrypted stuff. This code is gonna look like
1473 * the dyld source one day... */
1474 struct mach_header
*header
= (struct mach_header
*)addr
;
1475 size_t mach_header_sz
= sizeof(struct mach_header
);
1476 if (header
->magic
== MH_MAGIC_64
||
1477 header
->magic
== MH_CIGAM_64
) {
1478 mach_header_sz
= sizeof(struct mach_header_64
);
1480 offset
= mach_header_sz
;
1481 uint32_t ncmds
= header
->ncmds
;
1484 * Get a pointer to the command.
1486 struct load_command
*lcp
= (struct load_command
*)(addr
+ offset
);
1487 offset
+= lcp
->cmdsize
;
1491 seg64
= (struct segment_command_64
*)lcp
;
1492 if ((seg64
->fileoff
<= eip
->cryptoff
) &&
1493 (seg64
->fileoff
+seg64
->filesize
>=
1494 eip
->cryptoff
+eip
->cryptsize
)) {
1495 map_offset
= seg64
->vmaddr
+ eip
->cryptoff
- seg64
->fileoff
;
1496 map_size
= eip
->cryptsize
;
1500 seg32
= (struct segment_command
*)lcp
;
1501 if ((seg32
->fileoff
<= eip
->cryptoff
) &&
1502 (seg32
->fileoff
+seg32
->filesize
>=
1503 eip
->cryptoff
+eip
->cryptsize
)) {
1504 map_offset
= seg32
->vmaddr
+ eip
->cryptoff
- seg32
->fileoff
;
1505 map_size
= eip
->cryptsize
;
1511 /* if we get here, did not find anything */
1512 return LOAD_FAILURE
;
1515 /* now remap using the decrypter */
1516 kr
= vm_map_apple_protected(map
, map_offset
, map_offset
+map_size
, &crypt_info
);
1517 if(kr
) printf("set_code_unprotect(): mapping failed with %x\n", kr
);
1519 return LOAD_SUCCESS
;
1525 * This routine exists to support the load_dylinker().
1527 * This routine has its own, separate, understanding of the FAT file format,
1528 * which is terrifically unfortunate.
1535 struct mach_header
*mach_header
,
1542 vfs_context_t ctx
= vfs_context_current();
1543 proc_t p
= vfs_context_proc(ctx
);
1544 kauth_cred_t kerncred
;
1545 struct nameidata nid
, *ndp
;
1547 struct fat_arch fat_arch
;
1548 int error
= LOAD_SUCCESS
;
1551 struct mach_header mach_header
;
1552 struct fat_header fat_header
;
1555 off_t fsize
= (off_t
)0;
1559 * Capture the kernel credential for use in the actual read of the
1560 * file, since the user doing the execution may have execute rights
1561 * but not read rights, but to exec something, we have to either map
1562 * or read it into the new process address space, which requires
1563 * read rights. This is to deal with lack of common credential
1564 * serialization code which would treat NOCRED as "serialize 'root'".
1566 kerncred
= vfs_context_ucred(vfs_context_kernel());
1570 /* init the namei data to point the file user's program name */
1571 NDINIT(ndp
, LOOKUP
, FOLLOW
| LOCKLEAF
, UIO_SYSSPACE32
, CAST_USER_ADDR_T(path
), ctx
);
1573 if ((error
= namei(ndp
)) != 0) {
1574 if (error
== ENOENT
) {
1575 error
= LOAD_ENOENT
;
1577 error
= LOAD_FAILURE
;
1584 /* check for regular file */
1585 if (vp
->v_type
!= VREG
) {
1586 error
= LOAD_PROTECT
;
1591 if ((error
= vnode_size(vp
, &fsize
, ctx
)) != 0) {
1592 error
= LOAD_FAILURE
;
1596 /* Check mount point */
1597 if (vp
->v_mount
->mnt_flag
& MNT_NOEXEC
) {
1598 error
= LOAD_PROTECT
;
1603 if ((error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_EXECUTE
, ctx
)) != 0) {
1604 error
= LOAD_PROTECT
;
1608 /* try to open it */
1609 if ((error
= VNOP_OPEN(vp
, FREAD
, ctx
)) != 0) {
1610 error
= LOAD_PROTECT
;
1614 if ((error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)&header
, sizeof(header
), 0,
1615 UIO_SYSSPACE32
, IO_NODELOCKED
, kerncred
, &resid
, p
)) != 0) {
1616 error
= LOAD_IOERROR
;
1620 if (header
.mach_header
.magic
== MH_MAGIC
||
1621 header
.mach_header
.magic
== MH_MAGIC_64
)
1623 else if (header
.fat_header
.magic
== FAT_MAGIC
||
1624 header
.fat_header
.magic
== FAT_CIGAM
)
1627 error
= LOAD_BADMACHO
;
1632 /* Look up our architecture in the fat file. */
1633 error
= fatfile_getarch_with_bits(vp
, archbits
, (vm_offset_t
)(&header
.fat_header
), &fat_arch
);
1634 if (error
!= LOAD_SUCCESS
)
1637 /* Read the Mach-O header out of it */
1638 error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)&header
.mach_header
,
1639 sizeof(header
.mach_header
), fat_arch
.offset
,
1640 UIO_SYSSPACE32
, IO_NODELOCKED
, kerncred
, &resid
, p
);
1642 error
= LOAD_IOERROR
;
1646 /* Is this really a Mach-O? */
1647 if (header
.mach_header
.magic
!= MH_MAGIC
&&
1648 header
.mach_header
.magic
!= MH_MAGIC_64
) {
1649 error
= LOAD_BADMACHO
;
1653 *file_offset
= fat_arch
.offset
;
1654 *macho_size
= fat_arch
.size
;
1657 * Force get_macho_vnode() to fail if the architecture bits
1658 * do not match the expected architecture bits. This in
1659 * turn causes load_dylinker() to fail for the same reason,
1660 * so it ensures the dynamic linker and the binary are in
1661 * lock-step. This is potentially bad, if we ever add to
1662 * the CPU_ARCH_* bits any bits that are desirable but not
1663 * required, since the dynamic linker might work, but we will
1664 * refuse to load it because of this check.
1666 if ((cpu_type_t
)(header
.mach_header
.cputype
& CPU_ARCH_MASK
) != archbits
)
1667 return(LOAD_BADARCH
);
1670 *macho_size
= fsize
;
1673 *mach_header
= header
.mach_header
;
1676 ubc_setsize(vp
, fsize
);
1681 err2
= VNOP_CLOSE(vp
, FREAD
, ctx
);