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_code_unprotect() error %d "
573 p
->p_pid
, ret
, vp
->v_name
);
574 /* Don't let the app run if it's
575 * encrypted but we failed to set up the
582 /* Other commands are ignored by the kernel */
586 if (ret
!= LOAD_SUCCESS
)
589 if (ret
!= LOAD_SUCCESS
)
592 if (ret
== LOAD_SUCCESS
) {
593 if (! got_code_signatures
) {
594 struct cs_blob
*blob
;
595 /* no embedded signatures: look for detached ones */
596 blob
= ubc_cs_blob_get(vp
, -1, file_offset
);
598 /* get flags to be applied to the process */
599 result
->csflags
|= blob
->csb_flags
;
604 ret
= load_dylinker(dlp
, dlarchbits
, map
, thread
, depth
, result
, abi64
);
607 if (result
->thread_count
== 0) {
609 } else if ( abi64
) {
611 /* Map in 64-bit commpage */
612 /* LP64todo - make this clean */
614 * PPC51: ppc64 is limited to 51-bit addresses.
615 * Memory above that limit is handled specially
618 pmap_map_sharedpage(current_task(), get_map_pmap(map
));
625 kfree(kl_addr
, kl_size
);
630 #if CONFIG_CODE_DECRYPTION
632 #define APPLE_UNPROTECTED_HEADER_SIZE (3 * PAGE_SIZE_64)
635 unprotect_segment_64(
639 vm_map_offset_t map_addr
,
640 vm_map_size_t map_size
)
645 * The first APPLE_UNPROTECTED_HEADER_SIZE bytes (from offset 0 of
646 * this part of a Universal binary) are not protected...
647 * The rest needs to be "transformed".
649 if (file_off
<= APPLE_UNPROTECTED_HEADER_SIZE
&&
650 file_off
+ file_size
<= APPLE_UNPROTECTED_HEADER_SIZE
) {
651 /* it's all unprotected, nothing to do... */
654 if (file_off
<= APPLE_UNPROTECTED_HEADER_SIZE
) {
656 * We start mapping in the unprotected area.
657 * Skip the unprotected part...
659 vm_map_offset_t delta
;
661 delta
= APPLE_UNPROTECTED_HEADER_SIZE
;
666 /* ... transform the rest of the mapping. */
667 struct pager_crypt_info crypt_info
;
668 crypt_info
.page_decrypt
= dsmos_page_transform
;
669 crypt_info
.crypt_ops
= NULL
;
670 crypt_info
.crypt_end
= NULL
;
671 kr
= vm_map_apple_protected(map
,
677 if (kr
!= KERN_SUCCESS
) {
682 #else /* CONFIG_CODE_DECRYPTION */
683 #define unprotect_segment_64(file_off, file_size, map, map_addr, map_size) \
685 #endif /* CONFIG_CODE_DECRYPTION */
690 struct segment_command
*scp
,
694 __unused off_t end_of_file
,
696 load_result_t
*result
700 vm_offset_t map_addr
, map_offset
;
701 vm_size_t map_size
, seg_size
, delta_size
;
706 * Make sure what we get from the file is really ours (as specified
709 if (scp
->fileoff
+ scp
->filesize
> macho_size
)
710 return (LOAD_BADMACHO
);
712 * Make sure the segment is page-aligned in the file.
714 if ((scp
->fileoff
& PAGE_MASK
) != 0)
715 return LOAD_BADMACHO
;
717 seg_size
= round_page(scp
->vmsize
);
719 return(KERN_SUCCESS
);
722 * Round sizes to page size.
724 map_size
= round_page(scp
->filesize
);
725 map_addr
= trunc_page(scp
->vmaddr
);
727 #if 0 /* XXX (4596982) this interferes with Rosetta */
731 (scp
->initprot
& VM_PROT_ALL
) == VM_PROT_NONE
&&
732 (scp
->maxprot
& VM_PROT_ALL
) == VM_PROT_NONE
) {
734 * This is a "page zero" segment: it starts at address 0,
735 * is not mapped from the binary file and is not accessible.
736 * User-space should never be able to access that memory, so
737 * make it completely off limits by raising the VM map's
740 ret
= vm_map_raise_min_offset(map
, (vm_map_offset_t
) seg_size
);
741 if (ret
!= KERN_SUCCESS
) {
748 map_offset
= pager_offset
+ scp
->fileoff
;
751 initprot
= (scp
->initprot
) & VM_PROT_ALL
;
752 maxprot
= (scp
->maxprot
) & VM_PROT_ALL
;
754 * Map a copy of the file into the address space.
757 &map_addr
, map_size
, (vm_offset_t
)0,
758 VM_FLAGS_FIXED
, pager
, map_offset
, TRUE
,
761 if (ret
!= KERN_SUCCESS
)
762 return(LOAD_NOSPACE
);
765 * If the file didn't end on a page boundary,
766 * we need to zero the leftover.
768 delta_size
= map_size
- scp
->filesize
;
770 if (delta_size
> 0) {
773 ret
= vm_allocate(kernel_map
, &tmp
, delta_size
, VM_FLAGS_ANYWHERE
);
774 if (ret
!= KERN_SUCCESS
)
775 return(LOAD_RESOURCE
);
777 if (copyout(tmp
, map_addr
+ scp
->filesize
,
779 (void) vm_deallocate(
780 kernel_map
, tmp
, delta_size
);
781 return(LOAD_FAILURE
);
784 (void) vm_deallocate(kernel_map
, tmp
, delta_size
);
790 * If the virtual size of the segment is greater
791 * than the size from the file, we need to allocate
792 * zero fill memory for the rest.
794 delta_size
= seg_size
- map_size
;
795 if (delta_size
> 0) {
796 vm_offset_t tmp
= map_addr
+ map_size
;
798 ret
= vm_map(map
, &tmp
, delta_size
, 0, VM_FLAGS_FIXED
,
800 scp
->initprot
, scp
->maxprot
,
802 if (ret
!= KERN_SUCCESS
)
803 return(LOAD_NOSPACE
);
806 if ( (scp
->fileoff
== 0) && (scp
->filesize
!= 0) )
807 result
->mach_header
= map_addr
;
809 if (scp
->flags
& SG_PROTECTED_VERSION_1
) {
810 ret
= unprotect_segment_64((uint64_t) scp
->fileoff
,
811 (uint64_t) scp
->filesize
,
813 (vm_map_offset_t
) map_addr
,
814 (vm_map_size_t
) map_size
);
825 struct segment_command_64
*scp64
,
829 __unused off_t end_of_file
,
831 load_result_t
*result
835 mach_vm_offset_t map_addr
, map_offset
;
836 mach_vm_size_t map_size
, seg_size
, delta_size
;
841 * Make sure what we get from the file is really ours (as specified
844 if (scp64
->fileoff
+ scp64
->filesize
> (uint64_t)macho_size
)
845 return (LOAD_BADMACHO
);
847 * Make sure the segment is page-aligned in the file.
849 if ((scp64
->fileoff
& PAGE_MASK_64
) != 0)
850 return LOAD_BADMACHO
;
852 seg_size
= round_page_64(scp64
->vmsize
);
854 return(KERN_SUCCESS
);
857 * Round sizes to page size.
859 map_size
= round_page_64(scp64
->filesize
); /* limited to 32 bits */
860 map_addr
= round_page_64(scp64
->vmaddr
);
865 (scp64
->initprot
& VM_PROT_ALL
) == VM_PROT_NONE
&&
866 (scp64
->maxprot
& VM_PROT_ALL
) == VM_PROT_NONE
) {
868 * This is a "page zero" segment: it starts at address 0,
869 * is not mapped from the binary file and is not accessible.
870 * User-space should never be able to access that memory, so
871 * make it completely off limits by raising the VM map's
874 ret
= vm_map_raise_min_offset(map
, seg_size
);
875 if (ret
!= KERN_SUCCESS
) {
881 map_offset
= pager_offset
+ scp64
->fileoff
; /* limited to 32 bits */
884 initprot
= (scp64
->initprot
) & VM_PROT_ALL
;
885 maxprot
= (scp64
->maxprot
) & VM_PROT_ALL
;
887 * Map a copy of the file into the address space.
889 ret
= mach_vm_map(map
,
890 &map_addr
, map_size
, (mach_vm_offset_t
)0,
891 VM_FLAGS_FIXED
, pager
, map_offset
, TRUE
,
894 if (ret
!= KERN_SUCCESS
)
895 return(LOAD_NOSPACE
);
898 * If the file didn't end on a page boundary,
899 * we need to zero the leftover.
901 delta_size
= map_size
- scp64
->filesize
;
903 if (delta_size
> 0) {
904 mach_vm_offset_t tmp
;
906 ret
= vm_allocate(kernel_map
, &tmp
, delta_size
, VM_FLAGS_ANYWHERE
);
907 if (ret
!= KERN_SUCCESS
)
908 return(LOAD_RESOURCE
);
910 if (copyout(tmp
, map_addr
+ scp64
->filesize
,
912 (void) vm_deallocate(
913 kernel_map
, tmp
, delta_size
);
914 return (LOAD_FAILURE
);
917 (void) vm_deallocate(kernel_map
, tmp
, delta_size
);
923 * If the virtual size of the segment is greater
924 * than the size from the file, we need to allocate
925 * zero fill memory for the rest.
927 delta_size
= seg_size
- map_size
;
928 if (delta_size
> 0) {
929 mach_vm_offset_t tmp
= map_addr
+ map_size
;
931 ret
= mach_vm_map(map
, &tmp
, delta_size
, 0, VM_FLAGS_FIXED
,
933 scp64
->initprot
, scp64
->maxprot
,
935 if (ret
!= KERN_SUCCESS
)
936 return(LOAD_NOSPACE
);
939 if ( (scp64
->fileoff
== 0) && (scp64
->filesize
!= 0) )
940 result
->mach_header
= map_addr
;
942 if (scp64
->flags
& SG_PROTECTED_VERSION_1
) {
943 ret
= unprotect_segment_64(scp64
->fileoff
,
958 struct thread_command
*tcp
,
960 load_result_t
*result
968 task
= get_threadtask(thread
);
970 /* if count is 0; same as thread */
971 if (result
->thread_count
!= 0) {
972 kret
= thread_create(task
, &thread
);
973 if (kret
!= KERN_SUCCESS
)
974 return(LOAD_RESOURCE
);
975 thread_deallocate(thread
);
978 lret
= load_threadstate(thread
,
979 (unsigned long *)(((vm_offset_t
)tcp
) +
980 sizeof(struct thread_command
)),
981 tcp
->cmdsize
- sizeof(struct thread_command
));
982 if (lret
!= LOAD_SUCCESS
)
985 if (result
->thread_count
== 0) {
986 lret
= load_threadstack(thread
,
987 (unsigned long *)(((vm_offset_t
)tcp
) +
988 sizeof(struct thread_command
)),
989 tcp
->cmdsize
- sizeof(struct thread_command
),
993 result
->customstack
= 1;
995 result
->customstack
= 0;
997 if (lret
!= LOAD_SUCCESS
)
1000 lret
= load_threadentry(thread
,
1001 (unsigned long *)(((vm_offset_t
)tcp
) +
1002 sizeof(struct thread_command
)),
1003 tcp
->cmdsize
- sizeof(struct thread_command
),
1004 &result
->entry_point
);
1005 if (lret
!= LOAD_SUCCESS
)
1009 * Resume thread now, note that this means that the thread
1010 * commands should appear after all the load commands to
1011 * be sure they don't reference anything not yet mapped.
1014 thread_resume(thread
);
1016 result
->thread_count
++;
1018 return(LOAD_SUCCESS
);
1024 struct thread_command
*tcp
,
1026 load_result_t
*result
1032 if (result
->thread_count
!= 0) {
1033 printf("load_unixthread: already have a thread!");
1034 return (LOAD_FAILURE
);
1037 ret
= load_threadstack(thread
,
1038 (unsigned long *)(((vm_offset_t
)tcp
) +
1039 sizeof(struct thread_command
)),
1040 tcp
->cmdsize
- sizeof(struct thread_command
),
1041 &result
->user_stack
,
1043 if (ret
!= LOAD_SUCCESS
)
1047 result
->customstack
= 1;
1049 result
->customstack
= 0;
1050 ret
= load_threadentry(thread
,
1051 (unsigned long *)(((vm_offset_t
)tcp
) +
1052 sizeof(struct thread_command
)),
1053 tcp
->cmdsize
- sizeof(struct thread_command
),
1054 &result
->entry_point
);
1055 if (ret
!= LOAD_SUCCESS
)
1058 ret
= load_threadstate(thread
,
1059 (unsigned long *)(((vm_offset_t
)tcp
) +
1060 sizeof(struct thread_command
)),
1061 tcp
->cmdsize
- sizeof(struct thread_command
));
1062 if (ret
!= LOAD_SUCCESS
)
1065 result
->unixproc
= TRUE
;
1066 result
->thread_count
++;
1068 return(LOAD_SUCCESS
);
1076 unsigned long total_size
1082 unsigned long thread_size
;
1084 ret
= thread_state_initialize( thread
);
1085 if (ret
!= KERN_SUCCESS
) {
1086 return(LOAD_FAILURE
);
1090 * Set the new thread state; iterate through the state flavors in
1093 while (total_size
> 0) {
1096 thread_size
= (size
+2)*sizeof(unsigned long);
1097 if (thread_size
> total_size
)
1098 return(LOAD_BADMACHO
);
1099 total_size
-= thread_size
;
1101 * Third argument is a kernel space pointer; it gets cast
1102 * to the appropriate type in machine_thread_set_state()
1103 * based on the value of flavor.
1105 ret
= thread_setstatus(thread
, flavor
, (thread_state_t
)ts
, size
);
1106 if (ret
!= KERN_SUCCESS
) {
1107 return(LOAD_FAILURE
);
1109 ts
+= size
; /* ts is a (unsigned long *) */
1111 return(LOAD_SUCCESS
);
1119 unsigned long total_size
,
1120 user_addr_t
*user_stack
,
1127 unsigned long stack_size
;
1129 while (total_size
> 0) {
1132 stack_size
= (size
+2)*sizeof(unsigned long);
1133 if (stack_size
> total_size
)
1134 return(LOAD_BADMACHO
);
1135 total_size
-= stack_size
;
1138 * Third argument is a kernel space pointer; it gets cast
1139 * to the appropriate type in thread_userstack() based on
1140 * the value of flavor.
1142 ret
= thread_userstack(thread
, flavor
, (thread_state_t
)ts
, size
, user_stack
, customstack
);
1143 if (ret
!= KERN_SUCCESS
) {
1144 return(LOAD_FAILURE
);
1146 ts
+= size
; /* ts is a (unsigned long *) */
1148 return(LOAD_SUCCESS
);
1156 unsigned long total_size
,
1157 mach_vm_offset_t
*entry_point
1163 unsigned long entry_size
;
1166 * Set the thread state.
1168 *entry_point
= MACH_VM_MIN_ADDRESS
;
1169 while (total_size
> 0) {
1172 entry_size
= (size
+2)*sizeof(unsigned long);
1173 if (entry_size
> total_size
)
1174 return(LOAD_BADMACHO
);
1175 total_size
-= entry_size
;
1177 * Third argument is a kernel space pointer; it gets cast
1178 * to the appropriate type in thread_entrypoint() based on
1179 * the value of flavor.
1181 ret
= thread_entrypoint(thread
, flavor
, (thread_state_t
)ts
, size
, entry_point
);
1182 if (ret
!= KERN_SUCCESS
) {
1183 return(LOAD_FAILURE
);
1185 ts
+= size
; /* ts is a (unsigned long *) */
1187 return(LOAD_SUCCESS
);
1194 struct dylinker_command
*lcp
,
1199 load_result_t
*result
,
1205 struct vnode
*vp
= NULLVP
; /* set by get_macho_vnode() */
1206 struct mach_header header
;
1207 off_t file_offset
= 0; /* set by get_macho_vnode() */
1208 off_t macho_size
= 0; /* set by get_macho_vnode() */
1210 load_result_t myresult
;
1213 mach_vm_offset_t dyl_start
, map_addr
;
1214 mach_vm_size_t dyl_length
;
1216 name
= (char *)lcp
+ lcp
->name
.offset
;
1218 * Check for a proper null terminated string.
1222 if (p
>= (char *)lcp
+ lcp
->cmdsize
)
1223 return(LOAD_BADMACHO
);
1226 ret
= get_macho_vnode(name
, archbits
, &header
, &file_offset
, &macho_size
, &vp
);
1230 myresult
= load_result_null
;
1233 * First try to map dyld in directly. This should work most of
1234 * the time since there shouldn't normally be something already
1235 * mapped to its address.
1238 ret
= parse_machfile(vp
, map
, thread
, &header
, file_offset
, macho_size
,
1242 * If it turned out something was in the way, then we'll take
1243 * take this longer path to map dyld into a temporary map and
1244 * copy it into destination map at a different address.
1247 if (ret
== LOAD_NOSPACE
) {
1251 * Use a temporary map to do the work.
1253 copy_map
= vm_map_create(pmap_create(vm_map_round_page(macho_size
),
1255 get_map_min(map
), get_map_max(map
), TRUE
);
1256 if (VM_MAP_NULL
== copy_map
) {
1257 ret
= LOAD_RESOURCE
;
1261 myresult
= load_result_null
;
1263 ret
= parse_machfile(vp
, copy_map
, thread
, &header
,
1264 file_offset
, macho_size
,
1268 vm_map_deallocate(copy_map
);
1272 if (get_map_nentries(copy_map
) > 0) {
1274 dyl_start
= mach_get_vm_start(copy_map
);
1275 dyl_length
= mach_get_vm_end(copy_map
) - dyl_start
;
1277 map_addr
= dyl_start
;
1278 ret
= mach_vm_allocate(map
, &map_addr
, dyl_length
, VM_FLAGS_ANYWHERE
);
1280 if (ret
!= KERN_SUCCESS
) {
1281 vm_map_deallocate(copy_map
);
1287 ret
= vm_map_copyin(copy_map
,
1288 (vm_map_address_t
)dyl_start
,
1289 (vm_map_size_t
)dyl_length
,
1291 if (ret
!= KERN_SUCCESS
) {
1292 (void) vm_map_remove(map
,
1293 vm_map_trunc_page(map_addr
),
1294 vm_map_round_page(map_addr
+ dyl_length
),
1296 vm_map_deallocate(copy_map
);
1300 ret
= vm_map_copy_overwrite(map
,
1301 (vm_map_address_t
)map_addr
,
1303 if (ret
!= KERN_SUCCESS
) {
1304 vm_map_copy_discard(tmp
);
1305 (void) vm_map_remove(map
,
1306 vm_map_trunc_page(map_addr
),
1307 vm_map_round_page(map_addr
+ dyl_length
),
1309 vm_map_deallocate(copy_map
);
1313 if (map_addr
!= dyl_start
)
1314 myresult
.entry_point
+= (map_addr
- dyl_start
);
1319 vm_map_deallocate(copy_map
);
1322 if (ret
== LOAD_SUCCESS
) {
1323 result
->dynlinker
= TRUE
;
1324 result
->entry_point
= myresult
.entry_point
;
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 switch(eip
->cryptid
) {
1446 /* not encrypted, just an empty load command */
1447 return LOAD_SUCCESS
;
1449 cryptname
="com.apple.unfree";
1452 /* some random cryptid that you could manually put into
1453 * your binary if you want NULL */
1454 cryptname
="com.apple.null";
1457 return LOAD_BADMACHO
;
1461 result
= vn_getpath(vp
, vpath
, &len
);
1462 if(result
) return result
;
1464 /* set up decrypter first */
1465 if(NULL
==text_crypter_create
) return LOAD_FAILURE
;
1466 kr
=text_crypter_create(&crypt_info
, cryptname
, (void*)vpath
);
1469 printf("set_code_unprotect: unable to create decrypter %s, kr=%d\n",
1471 return LOAD_RESOURCE
;
1474 /* this is terrible, but we have to rescan the load commands to find the
1475 * virtual address of this encrypted stuff. This code is gonna look like
1476 * the dyld source one day... */
1477 struct mach_header
*header
= (struct mach_header
*)addr
;
1478 size_t mach_header_sz
= sizeof(struct mach_header
);
1479 if (header
->magic
== MH_MAGIC_64
||
1480 header
->magic
== MH_CIGAM_64
) {
1481 mach_header_sz
= sizeof(struct mach_header_64
);
1483 offset
= mach_header_sz
;
1484 uint32_t ncmds
= header
->ncmds
;
1487 * Get a pointer to the command.
1489 struct load_command
*lcp
= (struct load_command
*)(addr
+ offset
);
1490 offset
+= lcp
->cmdsize
;
1494 seg64
= (struct segment_command_64
*)lcp
;
1495 if ((seg64
->fileoff
<= eip
->cryptoff
) &&
1496 (seg64
->fileoff
+seg64
->filesize
>=
1497 eip
->cryptoff
+eip
->cryptsize
)) {
1498 map_offset
= seg64
->vmaddr
+ eip
->cryptoff
- seg64
->fileoff
;
1499 map_size
= eip
->cryptsize
;
1503 seg32
= (struct segment_command
*)lcp
;
1504 if ((seg32
->fileoff
<= eip
->cryptoff
) &&
1505 (seg32
->fileoff
+seg32
->filesize
>=
1506 eip
->cryptoff
+eip
->cryptsize
)) {
1507 map_offset
= seg32
->vmaddr
+ eip
->cryptoff
- seg32
->fileoff
;
1508 map_size
= eip
->cryptsize
;
1514 /* if we get here, did not find anything */
1515 return LOAD_BADMACHO
;
1518 /* now remap using the decrypter */
1519 kr
= vm_map_apple_protected(map
, map_offset
, map_offset
+map_size
, &crypt_info
);
1521 printf("set_code_unprotect(): mapping failed with %x\n", kr
);
1522 crypt_info
.crypt_end(crypt_info
.crypt_ops
);
1523 return LOAD_PROTECT
;
1526 return LOAD_SUCCESS
;
1532 * This routine exists to support the load_dylinker().
1534 * This routine has its own, separate, understanding of the FAT file format,
1535 * which is terrifically unfortunate.
1542 struct mach_header
*mach_header
,
1549 vfs_context_t ctx
= vfs_context_current();
1550 proc_t p
= vfs_context_proc(ctx
);
1551 kauth_cred_t kerncred
;
1552 struct nameidata nid
, *ndp
;
1554 struct fat_arch fat_arch
;
1555 int error
= LOAD_SUCCESS
;
1558 struct mach_header mach_header
;
1559 struct fat_header fat_header
;
1562 off_t fsize
= (off_t
)0;
1566 * Capture the kernel credential for use in the actual read of the
1567 * file, since the user doing the execution may have execute rights
1568 * but not read rights, but to exec something, we have to either map
1569 * or read it into the new process address space, which requires
1570 * read rights. This is to deal with lack of common credential
1571 * serialization code which would treat NOCRED as "serialize 'root'".
1573 kerncred
= vfs_context_ucred(vfs_context_kernel());
1577 /* init the namei data to point the file user's program name */
1578 NDINIT(ndp
, LOOKUP
, FOLLOW
| LOCKLEAF
, UIO_SYSSPACE32
, CAST_USER_ADDR_T(path
), ctx
);
1580 if ((error
= namei(ndp
)) != 0) {
1581 if (error
== ENOENT
) {
1582 error
= LOAD_ENOENT
;
1584 error
= LOAD_FAILURE
;
1591 /* check for regular file */
1592 if (vp
->v_type
!= VREG
) {
1593 error
= LOAD_PROTECT
;
1598 if ((error
= vnode_size(vp
, &fsize
, ctx
)) != 0) {
1599 error
= LOAD_FAILURE
;
1603 /* Check mount point */
1604 if (vp
->v_mount
->mnt_flag
& MNT_NOEXEC
) {
1605 error
= LOAD_PROTECT
;
1610 if ((error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_EXECUTE
, ctx
)) != 0) {
1611 error
= LOAD_PROTECT
;
1615 /* try to open it */
1616 if ((error
= VNOP_OPEN(vp
, FREAD
, ctx
)) != 0) {
1617 error
= LOAD_PROTECT
;
1621 if ((error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)&header
, sizeof(header
), 0,
1622 UIO_SYSSPACE32
, IO_NODELOCKED
, kerncred
, &resid
, p
)) != 0) {
1623 error
= LOAD_IOERROR
;
1627 if (header
.mach_header
.magic
== MH_MAGIC
||
1628 header
.mach_header
.magic
== MH_MAGIC_64
)
1630 else if (header
.fat_header
.magic
== FAT_MAGIC
||
1631 header
.fat_header
.magic
== FAT_CIGAM
)
1634 error
= LOAD_BADMACHO
;
1639 /* Look up our architecture in the fat file. */
1640 error
= fatfile_getarch_with_bits(vp
, archbits
, (vm_offset_t
)(&header
.fat_header
), &fat_arch
);
1641 if (error
!= LOAD_SUCCESS
)
1644 /* Read the Mach-O header out of it */
1645 error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)&header
.mach_header
,
1646 sizeof(header
.mach_header
), fat_arch
.offset
,
1647 UIO_SYSSPACE32
, IO_NODELOCKED
, kerncred
, &resid
, p
);
1649 error
= LOAD_IOERROR
;
1653 /* Is this really a Mach-O? */
1654 if (header
.mach_header
.magic
!= MH_MAGIC
&&
1655 header
.mach_header
.magic
!= MH_MAGIC_64
) {
1656 error
= LOAD_BADMACHO
;
1660 *file_offset
= fat_arch
.offset
;
1661 *macho_size
= fat_arch
.size
;
1664 * Force get_macho_vnode() to fail if the architecture bits
1665 * do not match the expected architecture bits. This in
1666 * turn causes load_dylinker() to fail for the same reason,
1667 * so it ensures the dynamic linker and the binary are in
1668 * lock-step. This is potentially bad, if we ever add to
1669 * the CPU_ARCH_* bits any bits that are desirable but not
1670 * required, since the dynamic linker might work, but we will
1671 * refuse to load it because of this check.
1673 if ((cpu_type_t
)(header
.mach_header
.cputype
& CPU_ARCH_MASK
) != archbits
)
1674 return(LOAD_BADARCH
);
1677 *macho_size
= fsize
;
1680 *mach_header
= header
.mach_header
;
1683 ubc_setsize(vp
, fsize
);
1688 err2
= VNOP_CLOSE(vp
, FREAD
, ctx
);