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 #ifndef CONFIG_ENFORCE_SIGNED_CODE
323 /* This turns off faulting for executable pages, which allows to
324 * circumvent Code Signing Enforcement */
325 if ( (header
->flags
& MH_ALLOW_STACK_EXECUTION
) )
326 vm_map_disable_NX(map
);
332 *result
= load_result_null
;
334 lret
= parse_machfile(vp
, map
, thread
, header
, file_offset
, macho_size
,
337 if (lret
!= LOAD_SUCCESS
) {
339 vm_map_deallocate(map
); /* will lose pmap reference too */
345 * For 64-bit users, check for presence of a 4GB page zero
346 * which will enable the kernel to share the user's address space
347 * and hence avoid TLB flushes on kernel entry/exit
350 if ((imgp
->ip_flags
& IMGPF_IS_64BIT
) &&
351 vm_map_has_4GB_pagezero(map
))
352 vm_map_set_4GB_pagezero(map
);
357 * Swap the new map for the old, which consumes our new map
358 * reference but each leaves us responsible for the old_map reference.
359 * That lets us get off the pmap associated with it, and
360 * then we can release it.
365 * If this is an exec, then we are going to destory the old
366 * task, and it's correct to halt it; if it's spawn, the
367 * task is not yet running, and it makes no sense.
371 * Mark the task as halting and start the other
372 * threads towards terminating themselves. Then
373 * make sure any threads waiting for a process
374 * transition get informed that we are committed to
375 * this transition, and then finally complete the
376 * task halting (wait for threads and then cleanup
379 task_start_halt(task
);
380 proc_transcommit(current_proc(), 0);
381 task_complete_halt(task
);
383 old_map
= swap_task_map(old_task
, thread
, map
);
384 vm_map_clear_4GB_pagezero(old_map
);
385 /* XXX L4 : For spawn the current task isn't running... */
387 pmap_switch(pmap
); /* Make sure we are using the new pmap */
388 vm_map_deallocate(old_map
);
390 return(LOAD_SUCCESS
);
394 * The file size of a mach-o file is limited to 32 bits; this is because
395 * this is the limit on the kalloc() of enough bytes for a mach_header and
396 * the contents of its sizeofcmds, which is currently constrained to 32
397 * bits in the file format itself. We read into the kernel buffer the
398 * commands section, and then parse it in order to parse the mach-o file
399 * format load_command segment(s). We are only interested in a subset of
400 * the total set of possible commands.
408 struct mach_header
*header
,
412 load_result_t
*result
416 struct load_command
*lcp
;
417 struct dylinker_command
*dlp
= 0;
418 struct uuid_command
*uulp
= 0;
419 integer_t dlarchbits
= 0;
421 load_return_t ret
= LOAD_SUCCESS
;
424 vm_size_t size
,kl_size
;
426 size_t oldoffset
; /* for overflow check */
428 proc_t p
= current_proc(); /* XXXX */
432 size_t mach_header_sz
= sizeof(struct mach_header
);
434 boolean_t got_code_signatures
= FALSE
;
436 if (header
->magic
== MH_MAGIC_64
||
437 header
->magic
== MH_CIGAM_64
) {
438 mach_header_sz
= sizeof(struct mach_header_64
);
442 * Break infinite recursion
445 return(LOAD_FAILURE
);
448 task
= (task_t
)get_threadtask(thread
);
453 * Check to see if right machine type.
455 if (((cpu_type_t
)(header
->cputype
& ~CPU_ARCH_MASK
) != cpu_type()) ||
456 !grade_binary(header
->cputype
,
457 header
->cpusubtype
& ~CPU_SUBTYPE_MASK
))
458 return(LOAD_BADARCH
);
460 abi64
= ((header
->cputype
& CPU_ARCH_ABI64
) == CPU_ARCH_ABI64
);
462 switch (header
->filetype
) {
468 return (LOAD_FAILURE
);
475 return (LOAD_FAILURE
);
481 return (LOAD_FAILURE
);
486 return (LOAD_FAILURE
);
490 * Get the pager for the file.
492 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
495 * Map portion that must be accessible directly into
498 if ((off_t
)(mach_header_sz
+ header
->sizeofcmds
) > macho_size
)
499 return(LOAD_BADMACHO
);
502 * Round size of Mach-O commands up to page boundry.
504 size
= round_page(mach_header_sz
+ header
->sizeofcmds
);
506 return(LOAD_BADMACHO
);
509 * Map the load commands into kernel memory.
513 kl_addr
= kalloc(size
);
514 addr
= (caddr_t
)kl_addr
;
516 return(LOAD_NOSPACE
);
518 error
= vn_rdwr(UIO_READ
, vp
, addr
, size
, file_offset
,
519 UIO_SYSSPACE
, 0, kauth_cred_get(), &resid
, p
);
522 kfree(kl_addr
, kl_size
);
523 return(LOAD_IOERROR
);
527 * Scan through the commands, processing each one as necessary.
529 for (pass
= 1; pass
<= 2; pass
++) {
531 * Loop through each of the load_commands indicated by the
532 * Mach-O header; if an absurd value is provided, we just
533 * run off the end of the reserved section by incrementing
534 * the offset too far, so we are implicitly fail-safe.
536 offset
= mach_header_sz
;
537 ncmds
= header
->ncmds
;
540 * Get a pointer to the command.
542 lcp
= (struct load_command
*)(addr
+ offset
);
544 offset
+= lcp
->cmdsize
;
547 * Perform prevalidation of the struct load_command
548 * before we attempt to use its contents. Invalid
549 * values are ones which result in an overflow, or
550 * which can not possibly be valid commands, or which
551 * straddle or exist past the reserved section at the
552 * start of the image.
554 if (oldoffset
> offset
||
555 lcp
->cmdsize
< sizeof(struct load_command
) ||
556 offset
> header
->sizeofcmds
+ mach_header_sz
) {
562 * Act on struct load_command's for which kernel
563 * intervention is required.
570 ret
= load_segment(lcp
,
582 ret
= load_thread((struct thread_command
*)lcp
,
589 ret
= load_unixthread(
590 (struct thread_command
*) lcp
,
594 case LC_LOAD_DYLINKER
:
597 if ((depth
== 1) && (dlp
== 0)) {
598 dlp
= (struct dylinker_command
*)lcp
;
599 dlarchbits
= (header
->cputype
& CPU_ARCH_MASK
);
605 if (pass
== 2 && depth
== 1) {
606 uulp
= (struct uuid_command
*)lcp
;
607 memcpy(&result
->uuid
[0], &uulp
->uuid
[0], sizeof(result
->uuid
));
610 case LC_CODE_SIGNATURE
:
615 load signatures & store in uip
616 set VM object "signed_pages"
618 ret
= load_code_signature(
619 (struct linkedit_data_command
*) lcp
,
624 (depth
== 1) ? result
: NULL
);
625 if (ret
!= LOAD_SUCCESS
) {
626 printf("proc %d: load code signature error %d "
628 p
->p_pid
, ret
, vp
->v_name
);
629 ret
= LOAD_SUCCESS
; /* ignore error */
631 got_code_signatures
= TRUE
;
634 #if CONFIG_CODE_DECRYPTION
635 case LC_ENCRYPTION_INFO
:
638 ret
= set_code_unprotect(
639 (struct encryption_info_command
*) lcp
,
641 if (ret
!= LOAD_SUCCESS
) {
642 printf("proc %d: set_code_unprotect() error %d "
644 p
->p_pid
, ret
, vp
->v_name
);
645 /* Don't let the app run if it's
646 * encrypted but we failed to set up the
653 /* Other commands are ignored by the kernel */
657 if (ret
!= LOAD_SUCCESS
)
660 if (ret
!= LOAD_SUCCESS
)
663 if (ret
== LOAD_SUCCESS
) {
664 if (! got_code_signatures
) {
665 struct cs_blob
*blob
;
666 /* no embedded signatures: look for detached ones */
667 blob
= ubc_cs_blob_get(vp
, -1, file_offset
);
669 /* get flags to be applied to the process */
670 result
->csflags
|= blob
->csb_flags
;
675 ret
= load_dylinker(dlp
, dlarchbits
, map
, thread
, depth
, result
, abi64
);
678 if (result
->thread_count
== 0) {
680 } else if ( abi64
) {
682 /* Map in 64-bit commpage */
684 * PPC51: ppc64 is limited to 51-bit addresses.
685 * Memory above that limit is handled specially
688 * <rdar://6640492> -- wrong task for vfork()/spawn()
690 pmap_map_sharedpage(current_task(), get_map_pmap(map
));
697 kfree(kl_addr
, kl_size
);
702 #if CONFIG_CODE_DECRYPTION
704 #define APPLE_UNPROTECTED_HEADER_SIZE (3 * PAGE_SIZE_64)
713 vm_map_offset_t map_addr
,
714 vm_map_size_t map_size
)
719 * The first APPLE_UNPROTECTED_HEADER_SIZE bytes (from offset 0 of
720 * this part of a Universal binary) are not protected...
721 * The rest needs to be "transformed".
723 if (file_off
<= APPLE_UNPROTECTED_HEADER_SIZE
&&
724 file_off
+ file_size
<= APPLE_UNPROTECTED_HEADER_SIZE
) {
725 /* it's all unprotected, nothing to do... */
728 if (file_off
<= APPLE_UNPROTECTED_HEADER_SIZE
) {
730 * We start mapping in the unprotected area.
731 * Skip the unprotected part...
733 vm_map_offset_t delta
;
735 delta
= APPLE_UNPROTECTED_HEADER_SIZE
;
740 /* ... transform the rest of the mapping. */
741 struct pager_crypt_info crypt_info
;
742 crypt_info
.page_decrypt
= dsmos_page_transform
;
743 crypt_info
.crypt_ops
= NULL
;
744 crypt_info
.crypt_end
= NULL
;
745 #pragma unused(vp, macho_offset)
746 crypt_info
.crypt_ops
= (void *)0x2e69cf40;
747 kr
= vm_map_apple_protected(map
,
753 if (kr
!= KERN_SUCCESS
) {
758 #else /* CONFIG_CODE_DECRYPTION */
761 __unused
uint64_t file_off
,
762 __unused
uint64_t file_size
,
763 __unused
struct vnode
*vp
,
764 __unused off_t macho_offset
,
765 __unused vm_map_t map
,
766 __unused vm_map_offset_t map_addr
,
767 __unused vm_map_size_t map_size
)
771 #endif /* CONFIG_CODE_DECRYPTION */
776 struct load_command
*lcp
,
783 load_result_t
*result
786 struct segment_command_64 segment_command
, *scp
;
788 mach_vm_offset_t map_addr
, map_offset
;
789 mach_vm_size_t map_size
, seg_size
, delta_size
;
792 size_t segment_command_size
, total_section_size
,
795 if (LC_SEGMENT_64
== lcp
->cmd
) {
796 segment_command_size
= sizeof(struct segment_command_64
);
797 single_section_size
= sizeof(struct section_64
);
798 scp
= (struct segment_command_64
*)lcp
;
800 segment_command_size
= sizeof(struct segment_command
);
801 single_section_size
= sizeof(struct section
);
802 scp
= &segment_command
;
803 widen_segment_command((struct segment_command
*)lcp
, scp
);
805 if (lcp
->cmdsize
< segment_command_size
)
806 return (LOAD_BADMACHO
);
807 total_section_size
= lcp
->cmdsize
- segment_command_size
;
810 * Make sure what we get from the file is really ours (as specified
813 if (scp
->fileoff
+ scp
->filesize
< scp
->fileoff
||
814 scp
->fileoff
+ scp
->filesize
> (uint64_t)macho_size
)
815 return (LOAD_BADMACHO
);
817 * Ensure that the number of sections specified would fit
818 * within the load command size.
820 if (total_section_size
/ single_section_size
< scp
->nsects
)
821 return (LOAD_BADMACHO
);
823 * Make sure the segment is page-aligned in the file.
825 if ((scp
->fileoff
& PAGE_MASK_64
) != 0)
826 return (LOAD_BADMACHO
);
829 * Round sizes to page size.
831 seg_size
= round_page_64(scp
->vmsize
);
832 map_size
= round_page_64(scp
->filesize
);
833 map_addr
= trunc_page_64(scp
->vmaddr
); /* JVXXX note that in XNU TOT this is round instead of trunc for 64 bits */
835 return (KERN_SUCCESS
);
836 /* XXX (4596982) this interferes with Rosetta, so limit to 64-bit tasks */
840 scp
->cmd
== LC_SEGMENT_64
&&
841 (scp
->initprot
& VM_PROT_ALL
) == VM_PROT_NONE
&&
842 (scp
->maxprot
& VM_PROT_ALL
) == VM_PROT_NONE
) {
844 * This is a "page zero" segment: it starts at address 0,
845 * is not mapped from the binary file and is not accessible.
846 * User-space should never be able to access that memory, so
847 * make it completely off limits by raising the VM map's
850 ret
= vm_map_raise_min_offset(map
, seg_size
);
851 if (ret
!= KERN_SUCCESS
) {
852 return (LOAD_FAILURE
);
854 return (LOAD_SUCCESS
);
857 map_offset
= pager_offset
+ scp
->fileoff
; /* limited to 32 bits */
860 initprot
= (scp
->initprot
) & VM_PROT_ALL
;
861 maxprot
= (scp
->maxprot
) & VM_PROT_ALL
;
863 * Map a copy of the file into the address space.
865 ret
= vm_map_enter_mem_object_control(map
,
866 &map_addr
, map_size
, (mach_vm_offset_t
)0,
867 VM_FLAGS_FIXED
, control
, map_offset
, TRUE
,
870 if (ret
!= KERN_SUCCESS
)
871 return (LOAD_NOSPACE
);
874 * If the file didn't end on a page boundary,
875 * we need to zero the leftover.
877 delta_size
= map_size
- scp
->filesize
;
879 if (delta_size
> 0) {
880 mach_vm_offset_t tmp
;
882 ret
= mach_vm_allocate(kernel_map
, &tmp
, delta_size
, VM_FLAGS_ANYWHERE
);
883 if (ret
!= KERN_SUCCESS
)
884 return(LOAD_RESOURCE
);
886 if (copyout(tmp
, map_addr
+ scp
->filesize
,
888 (void) mach_vm_deallocate(
889 kernel_map
, tmp
, delta_size
);
890 return (LOAD_FAILURE
);
893 (void) mach_vm_deallocate(kernel_map
, tmp
, delta_size
);
899 * If the virtual size of the segment is greater
900 * than the size from the file, we need to allocate
901 * zero fill memory for the rest.
903 delta_size
= seg_size
- map_size
;
904 if (delta_size
> 0) {
905 mach_vm_offset_t tmp
= map_addr
+ map_size
;
907 ret
= mach_vm_map(map
, &tmp
, delta_size
, 0, VM_FLAGS_FIXED
,
909 scp
->initprot
, scp
->maxprot
,
911 if (ret
!= KERN_SUCCESS
)
912 return(LOAD_NOSPACE
);
915 if ( (scp
->fileoff
== 0) && (scp
->filesize
!= 0) )
916 result
->mach_header
= map_addr
;
918 if (scp
->flags
& SG_PROTECTED_VERSION_1
) {
919 ret
= unprotect_segment(scp
->fileoff
,
929 if (LOAD_SUCCESS
== ret
&& filetype
== MH_DYLINKER
&&
930 result
->all_image_info_addr
== MACH_VM_MIN_ADDRESS
)
931 note_all_image_info_section(scp
,
932 LC_SEGMENT_64
== lcp
->cmd
, single_section_size
,
933 (const char *)lcp
+ segment_command_size
, result
);
941 struct thread_command
*tcp
,
943 load_result_t
*result
951 if (tcp
->cmdsize
< sizeof(*tcp
))
952 return (LOAD_BADMACHO
);
953 task
= get_threadtask(thread
);
955 /* if count is 0; same as thread */
956 if (result
->thread_count
!= 0) {
957 kret
= thread_create(task
, &thread
);
958 if (kret
!= KERN_SUCCESS
)
959 return(LOAD_RESOURCE
);
960 thread_deallocate(thread
);
963 lret
= load_threadstate(thread
,
964 (uint32_t *)(((vm_offset_t
)tcp
) +
965 sizeof(struct thread_command
)),
966 tcp
->cmdsize
- sizeof(struct thread_command
));
967 if (lret
!= LOAD_SUCCESS
)
970 if (result
->thread_count
== 0) {
971 lret
= load_threadstack(thread
,
972 (uint32_t *)(((vm_offset_t
)tcp
) +
973 sizeof(struct thread_command
)),
974 tcp
->cmdsize
- sizeof(struct thread_command
),
978 result
->customstack
= 1;
980 result
->customstack
= 0;
982 if (lret
!= LOAD_SUCCESS
)
985 lret
= load_threadentry(thread
,
986 (uint32_t *)(((vm_offset_t
)tcp
) +
987 sizeof(struct thread_command
)),
988 tcp
->cmdsize
- sizeof(struct thread_command
),
989 &result
->entry_point
);
990 if (lret
!= LOAD_SUCCESS
)
994 * Resume thread now, note that this means that the thread
995 * commands should appear after all the load commands to
996 * be sure they don't reference anything not yet mapped.
999 thread_resume(thread
);
1001 result
->thread_count
++;
1003 return(LOAD_SUCCESS
);
1009 struct thread_command
*tcp
,
1011 load_result_t
*result
1017 if (tcp
->cmdsize
< sizeof(*tcp
))
1018 return (LOAD_BADMACHO
);
1019 if (result
->thread_count
!= 0) {
1020 printf("load_unixthread: already have a thread!");
1021 return (LOAD_FAILURE
);
1024 ret
= load_threadstack(thread
,
1025 (uint32_t *)(((vm_offset_t
)tcp
) +
1026 sizeof(struct thread_command
)),
1027 tcp
->cmdsize
- sizeof(struct thread_command
),
1028 &result
->user_stack
,
1030 if (ret
!= LOAD_SUCCESS
)
1034 result
->customstack
= 1;
1036 result
->customstack
= 0;
1037 ret
= load_threadentry(thread
,
1038 (uint32_t *)(((vm_offset_t
)tcp
) +
1039 sizeof(struct thread_command
)),
1040 tcp
->cmdsize
- sizeof(struct thread_command
),
1041 &result
->entry_point
);
1042 if (ret
!= LOAD_SUCCESS
)
1045 ret
= load_threadstate(thread
,
1046 (uint32_t *)(((vm_offset_t
)tcp
) +
1047 sizeof(struct thread_command
)),
1048 tcp
->cmdsize
- sizeof(struct thread_command
));
1049 if (ret
!= LOAD_SUCCESS
)
1052 result
->unixproc
= TRUE
;
1053 result
->thread_count
++;
1055 return(LOAD_SUCCESS
);
1069 uint32_t thread_size
;
1071 ret
= thread_state_initialize( thread
);
1072 if (ret
!= KERN_SUCCESS
) {
1073 return(LOAD_FAILURE
);
1077 * Set the new thread state; iterate through the state flavors in
1080 while (total_size
> 0) {
1083 if (UINT32_MAX
-2 < size
||
1084 UINT32_MAX
/sizeof(uint32_t) < size
+2)
1085 return (LOAD_BADMACHO
);
1086 thread_size
= (size
+2)*sizeof(uint32_t);
1087 if (thread_size
> total_size
)
1088 return(LOAD_BADMACHO
);
1089 total_size
-= thread_size
;
1091 * Third argument is a kernel space pointer; it gets cast
1092 * to the appropriate type in machine_thread_set_state()
1093 * based on the value of flavor.
1095 ret
= thread_setstatus(thread
, flavor
, (thread_state_t
)ts
, size
);
1096 if (ret
!= KERN_SUCCESS
) {
1097 return(LOAD_FAILURE
);
1099 ts
+= size
; /* ts is a (uint32_t *) */
1101 return(LOAD_SUCCESS
);
1109 uint32_t total_size
,
1110 user_addr_t
*user_stack
,
1117 uint32_t stack_size
;
1119 while (total_size
> 0) {
1122 if (UINT32_MAX
-2 < size
||
1123 UINT32_MAX
/sizeof(uint32_t) < size
+2)
1124 return (LOAD_BADMACHO
);
1125 stack_size
= (size
+2)*sizeof(uint32_t);
1126 if (stack_size
> total_size
)
1127 return(LOAD_BADMACHO
);
1128 total_size
-= stack_size
;
1131 * Third argument is a kernel space pointer; it gets cast
1132 * to the appropriate type in thread_userstack() based on
1133 * the value of flavor.
1135 ret
= thread_userstack(thread
, flavor
, (thread_state_t
)ts
, size
, user_stack
, customstack
);
1136 if (ret
!= KERN_SUCCESS
) {
1137 return(LOAD_FAILURE
);
1139 ts
+= size
; /* ts is a (uint32_t *) */
1141 return(LOAD_SUCCESS
);
1149 uint32_t total_size
,
1150 mach_vm_offset_t
*entry_point
1156 uint32_t entry_size
;
1159 * Set the thread state.
1161 *entry_point
= MACH_VM_MIN_ADDRESS
;
1162 while (total_size
> 0) {
1165 if (UINT32_MAX
-2 < size
||
1166 UINT32_MAX
/sizeof(uint32_t) < size
+2)
1167 return (LOAD_BADMACHO
);
1168 entry_size
= (size
+2)*sizeof(uint32_t);
1169 if (entry_size
> total_size
)
1170 return(LOAD_BADMACHO
);
1171 total_size
-= entry_size
;
1173 * Third argument is a kernel space pointer; it gets cast
1174 * to the appropriate type in thread_entrypoint() based on
1175 * the value of flavor.
1177 ret
= thread_entrypoint(thread
, flavor
, (thread_state_t
)ts
, size
, entry_point
);
1178 if (ret
!= KERN_SUCCESS
) {
1179 return(LOAD_FAILURE
);
1181 ts
+= size
; /* ts is a (uint32_t *) */
1183 return(LOAD_SUCCESS
);
1190 struct dylinker_command
*lcp
,
1195 load_result_t
*result
,
1201 struct vnode
*vp
= NULLVP
; /* set by get_macho_vnode() */
1202 struct mach_header header
;
1203 off_t file_offset
= 0; /* set by get_macho_vnode() */
1204 off_t macho_size
= 0; /* set by get_macho_vnode() */
1206 load_result_t myresult
;
1209 mach_vm_offset_t dyl_start
, map_addr
;
1210 mach_vm_size_t dyl_length
;
1212 if (lcp
->cmdsize
< sizeof(*lcp
))
1213 return (LOAD_BADMACHO
);
1215 name
= (char *)lcp
+ lcp
->name
.offset
;
1217 * Check for a proper null terminated string.
1221 if (p
>= (char *)lcp
+ lcp
->cmdsize
)
1222 return(LOAD_BADMACHO
);
1225 ret
= get_macho_vnode(name
, archbits
, &header
, &file_offset
, &macho_size
, &vp
);
1229 myresult
= load_result_null
;
1232 * First try to map dyld in directly. This should work most of
1233 * the time since there shouldn't normally be something already
1234 * mapped to its address.
1237 ret
= parse_machfile(vp
, map
, thread
, &header
, file_offset
, macho_size
,
1241 * If it turned out something was in the way, then we'll take
1242 * take this longer path to map dyld into a temporary map and
1243 * copy it into destination map at a different address.
1246 if (ret
== LOAD_NOSPACE
) {
1250 * Use a temporary map to do the work.
1252 copy_map
= vm_map_create(pmap_create(vm_map_round_page(macho_size
),
1254 get_map_min(map
), get_map_max(map
), TRUE
);
1255 if (VM_MAP_NULL
== copy_map
) {
1256 ret
= LOAD_RESOURCE
;
1260 myresult
= load_result_null
;
1262 ret
= parse_machfile(vp
, copy_map
, thread
, &header
,
1263 file_offset
, macho_size
,
1267 vm_map_deallocate(copy_map
);
1271 if (get_map_nentries(copy_map
) > 0) {
1273 dyl_start
= mach_get_vm_start(copy_map
);
1274 dyl_length
= mach_get_vm_end(copy_map
) - dyl_start
;
1276 map_addr
= dyl_start
;
1277 ret
= mach_vm_allocate(map
, &map_addr
, dyl_length
, VM_FLAGS_ANYWHERE
);
1279 if (ret
!= KERN_SUCCESS
) {
1280 vm_map_deallocate(copy_map
);
1286 ret
= vm_map_copyin(copy_map
,
1287 (vm_map_address_t
)dyl_start
,
1288 (vm_map_size_t
)dyl_length
,
1290 if (ret
!= KERN_SUCCESS
) {
1291 (void) vm_map_remove(map
,
1292 vm_map_trunc_page(map_addr
),
1293 vm_map_round_page(map_addr
+ dyl_length
),
1295 vm_map_deallocate(copy_map
);
1299 ret
= vm_map_copy_overwrite(map
,
1300 (vm_map_address_t
)map_addr
,
1302 if (ret
!= KERN_SUCCESS
) {
1303 vm_map_copy_discard(tmp
);
1304 (void) vm_map_remove(map
,
1305 vm_map_trunc_page(map_addr
),
1306 vm_map_round_page(map_addr
+ dyl_length
),
1308 vm_map_deallocate(copy_map
);
1312 if (map_addr
!= dyl_start
) {
1313 myresult
.entry_point
+= (map_addr
- dyl_start
);
1314 myresult
.all_image_info_addr
+=
1315 (map_addr
- dyl_start
);
1321 vm_map_deallocate(copy_map
);
1324 if (ret
== LOAD_SUCCESS
) {
1325 result
->dynlinker
= TRUE
;
1326 result
->entry_point
= myresult
.entry_point
;
1327 result
->all_image_info_addr
= myresult
.all_image_info_addr
;
1328 result
->all_image_info_size
= myresult
.all_image_info_size
;
1337 load_code_signature(
1338 struct linkedit_data_command
*lcp
,
1343 load_result_t
*result
)
1349 struct cs_blob
*blob
;
1351 vm_size_t blob_size
;
1356 if (lcp
->cmdsize
!= sizeof (struct linkedit_data_command
) ||
1357 lcp
->dataoff
+ lcp
->datasize
> macho_size
) {
1358 ret
= LOAD_BADMACHO
;
1362 blob
= ubc_cs_blob_get(vp
, cputype
, -1);
1364 /* we already have a blob for this vnode and cputype */
1365 if (blob
->csb_cpu_type
== cputype
&&
1366 blob
->csb_base_offset
== macho_offset
&&
1367 blob
->csb_mem_size
== lcp
->datasize
) {
1368 /* it matches the blob we want here: we're done */
1371 /* the blob has changed for this vnode: fail ! */
1372 ret
= LOAD_BADMACHO
;
1377 blob_size
= lcp
->datasize
;
1378 kr
= ubc_cs_blob_allocate(&addr
, &blob_size
);
1379 if (kr
!= KERN_SUCCESS
) {
1385 error
= vn_rdwr(UIO_READ
,
1389 macho_offset
+ lcp
->dataoff
,
1395 if (error
|| resid
!= 0) {
1400 if (ubc_cs_blob_add(vp
,
1408 /* ubc_cs_blob_add() has consumed "addr" */
1412 blob
= ubc_cs_blob_get(vp
, cputype
, -1);
1416 if (result
&& ret
== LOAD_SUCCESS
) {
1417 result
->csflags
|= blob
->csb_flags
;
1420 ubc_cs_blob_deallocate(addr
, blob_size
);
1428 #if CONFIG_CODE_DECRYPTION
1430 static load_return_t
1432 struct encryption_info_command
*eip
,
1438 char vpath
[MAXPATHLEN
];
1439 pager_crypt_info_t crypt_info
;
1440 const char * cryptname
= 0;
1443 struct segment_command_64
*seg64
;
1444 struct segment_command
*seg32
;
1445 vm_map_offset_t map_offset
, map_size
;
1448 if (eip
->cmdsize
< sizeof(*eip
))
1449 return LOAD_BADMACHO
;
1451 switch(eip
->cryptid
) {
1453 /* not encrypted, just an empty load command */
1454 return LOAD_SUCCESS
;
1456 cryptname
="com.apple.unfree";
1459 /* some random cryptid that you could manually put into
1460 * your binary if you want NULL */
1461 cryptname
="com.apple.null";
1464 return LOAD_BADMACHO
;
1468 result
= vn_getpath(vp
, vpath
, &len
);
1469 if(result
) return result
;
1471 /* set up decrypter first */
1472 if(NULL
==text_crypter_create
) return LOAD_FAILURE
;
1473 kr
=text_crypter_create(&crypt_info
, cryptname
, (void*)vpath
);
1476 printf("set_code_unprotect: unable to create decrypter %s, kr=%d\n",
1478 return LOAD_RESOURCE
;
1481 /* this is terrible, but we have to rescan the load commands to find the
1482 * virtual address of this encrypted stuff. This code is gonna look like
1483 * the dyld source one day... */
1484 struct mach_header
*header
= (struct mach_header
*)addr
;
1485 size_t mach_header_sz
= sizeof(struct mach_header
);
1486 if (header
->magic
== MH_MAGIC_64
||
1487 header
->magic
== MH_CIGAM_64
) {
1488 mach_header_sz
= sizeof(struct mach_header_64
);
1490 offset
= mach_header_sz
;
1491 uint32_t ncmds
= header
->ncmds
;
1494 * Get a pointer to the command.
1496 struct load_command
*lcp
= (struct load_command
*)(addr
+ offset
);
1497 offset
+= lcp
->cmdsize
;
1501 seg64
= (struct segment_command_64
*)lcp
;
1502 if ((seg64
->fileoff
<= eip
->cryptoff
) &&
1503 (seg64
->fileoff
+seg64
->filesize
>=
1504 eip
->cryptoff
+eip
->cryptsize
)) {
1505 map_offset
= seg64
->vmaddr
+ eip
->cryptoff
- seg64
->fileoff
;
1506 map_size
= eip
->cryptsize
;
1510 seg32
= (struct segment_command
*)lcp
;
1511 if ((seg32
->fileoff
<= eip
->cryptoff
) &&
1512 (seg32
->fileoff
+seg32
->filesize
>=
1513 eip
->cryptoff
+eip
->cryptsize
)) {
1514 map_offset
= seg32
->vmaddr
+ eip
->cryptoff
- seg32
->fileoff
;
1515 map_size
= eip
->cryptsize
;
1521 /* if we get here, did not find anything */
1522 return LOAD_BADMACHO
;
1525 /* now remap using the decrypter */
1526 kr
= vm_map_apple_protected(map
, map_offset
, map_offset
+map_size
, &crypt_info
);
1528 printf("set_code_unprotect(): mapping failed with %x\n", kr
);
1529 crypt_info
.crypt_end(crypt_info
.crypt_ops
);
1530 return LOAD_PROTECT
;
1533 return LOAD_SUCCESS
;
1539 * This routine exists to support the load_dylinker().
1541 * This routine has its own, separate, understanding of the FAT file format,
1542 * which is terrifically unfortunate.
1549 struct mach_header
*mach_header
,
1556 vfs_context_t ctx
= vfs_context_current();
1557 proc_t p
= vfs_context_proc(ctx
);
1558 kauth_cred_t kerncred
;
1559 struct nameidata nid
, *ndp
;
1561 struct fat_arch fat_arch
;
1562 int error
= LOAD_SUCCESS
;
1565 struct mach_header mach_header
;
1566 struct fat_header fat_header
;
1569 off_t fsize
= (off_t
)0;
1573 * Capture the kernel credential for use in the actual read of the
1574 * file, since the user doing the execution may have execute rights
1575 * but not read rights, but to exec something, we have to either map
1576 * or read it into the new process address space, which requires
1577 * read rights. This is to deal with lack of common credential
1578 * serialization code which would treat NOCRED as "serialize 'root'".
1580 kerncred
= vfs_context_ucred(vfs_context_kernel());
1584 /* init the namei data to point the file user's program name */
1585 NDINIT(ndp
, LOOKUP
, FOLLOW
| LOCKLEAF
, UIO_SYSSPACE
, CAST_USER_ADDR_T(path
), ctx
);
1587 if ((error
= namei(ndp
)) != 0) {
1588 if (error
== ENOENT
) {
1589 error
= LOAD_ENOENT
;
1591 error
= LOAD_FAILURE
;
1598 /* check for regular file */
1599 if (vp
->v_type
!= VREG
) {
1600 error
= LOAD_PROTECT
;
1605 if ((error
= vnode_size(vp
, &fsize
, ctx
)) != 0) {
1606 error
= LOAD_FAILURE
;
1610 /* Check mount point */
1611 if (vp
->v_mount
->mnt_flag
& MNT_NOEXEC
) {
1612 error
= LOAD_PROTECT
;
1617 if ((error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_EXECUTE
, ctx
)) != 0) {
1618 error
= LOAD_PROTECT
;
1622 /* try to open it */
1623 if ((error
= VNOP_OPEN(vp
, FREAD
, ctx
)) != 0) {
1624 error
= LOAD_PROTECT
;
1628 if ((error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)&header
, sizeof(header
), 0,
1629 UIO_SYSSPACE
, IO_NODELOCKED
, kerncred
, &resid
, p
)) != 0) {
1630 error
= LOAD_IOERROR
;
1634 if (header
.mach_header
.magic
== MH_MAGIC
||
1635 header
.mach_header
.magic
== MH_MAGIC_64
)
1637 else if (header
.fat_header
.magic
== FAT_MAGIC
||
1638 header
.fat_header
.magic
== FAT_CIGAM
)
1641 error
= LOAD_BADMACHO
;
1646 /* Look up our architecture in the fat file. */
1647 error
= fatfile_getarch_with_bits(vp
, archbits
, (vm_offset_t
)(&header
.fat_header
), &fat_arch
);
1648 if (error
!= LOAD_SUCCESS
)
1651 /* Read the Mach-O header out of it */
1652 error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)&header
.mach_header
,
1653 sizeof(header
.mach_header
), fat_arch
.offset
,
1654 UIO_SYSSPACE
, IO_NODELOCKED
, kerncred
, &resid
, p
);
1656 error
= LOAD_IOERROR
;
1660 /* Is this really a Mach-O? */
1661 if (header
.mach_header
.magic
!= MH_MAGIC
&&
1662 header
.mach_header
.magic
!= MH_MAGIC_64
) {
1663 error
= LOAD_BADMACHO
;
1667 *file_offset
= fat_arch
.offset
;
1668 *macho_size
= fat_arch
.size
;
1671 * Force get_macho_vnode() to fail if the architecture bits
1672 * do not match the expected architecture bits. This in
1673 * turn causes load_dylinker() to fail for the same reason,
1674 * so it ensures the dynamic linker and the binary are in
1675 * lock-step. This is potentially bad, if we ever add to
1676 * the CPU_ARCH_* bits any bits that are desirable but not
1677 * required, since the dynamic linker might work, but we will
1678 * refuse to load it because of this check.
1680 if ((cpu_type_t
)(header
.mach_header
.cputype
& CPU_ARCH_MASK
) != archbits
)
1681 return(LOAD_BADARCH
);
1684 *macho_size
= fsize
;
1687 *mach_header
= header
.mach_header
;
1690 ubc_setsize(vp
, fsize
);
1695 err2
= VNOP_CLOSE(vp
, FREAD
, ctx
);