2 * Copyright (c) 2000-2010 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (C) 1988, 1989, NeXT, Inc.
31 * File: kern/mach_loader.c
32 * Author: Avadis Tevanian, Jr.
34 * Mach object file loader (kernel version, for now).
36 * 21-Jul-88 Avadis Tevanian, Jr. (avie) at NeXT
40 #include <sys/param.h>
41 #include <sys/vnode_internal.h>
43 #include <sys/namei.h>
44 #include <sys/proc_internal.h>
45 #include <sys/kauth.h>
47 #include <sys/malloc.h>
48 #include <sys/mount_internal.h>
49 #include <sys/fcntl.h>
50 #include <sys/ubc_internal.h>
51 #include <sys/imgact.h>
52 #include <sys/codesign.h>
54 #include <mach/mach_types.h>
55 #include <mach/vm_map.h> /* vm_allocate() */
56 #include <mach/mach_vm.h> /* mach_vm_allocate() */
57 #include <mach/vm_statistics.h>
58 #include <mach/task.h>
59 #include <mach/thread_act.h>
61 #include <machine/vmparam.h>
62 #include <machine/exec.h>
63 #include <machine/pal_routines.h>
65 #include <kern/kern_types.h>
66 #include <kern/cpu_number.h>
67 #include <kern/mach_loader.h>
68 #include <kern/mach_fat.h>
69 #include <kern/kalloc.h>
70 #include <kern/task.h>
71 #include <kern/thread.h>
72 #include <kern/page_decrypt.h>
74 #include <mach-o/fat.h>
75 #include <mach-o/loader.h>
78 #include <vm/vm_map.h>
79 #include <vm/vm_kern.h>
80 #include <vm/vm_pager.h>
81 #include <vm/vnode_pager.h>
82 #include <vm/vm_protos.h>
83 #include <IOKit/IOReturn.h> /* for kIOReturnNotPrivileged */
86 * XXX vm/pmap.h should not treat these prototypes as MACH_KERNEL_PRIVATE
87 * when KERNEL is defined.
89 extern pmap_t
pmap_create(ledger_t ledger
, vm_map_size_t size
,
92 /* XXX should have prototypes in a shared header file */
93 extern int get_map_nentries(vm_map_t
);
95 extern kern_return_t
memory_object_signed(memory_object_control_t control
,
98 /* An empty load_result_t */
99 static load_result_t load_result_null
= {
100 .mach_header
= MACH_VM_MIN_ADDRESS
,
101 .entry_point
= MACH_VM_MIN_ADDRESS
,
102 .user_stack
= MACH_VM_MIN_ADDRESS
,
103 .user_stack_size
= 0,
104 .all_image_info_addr
= MACH_VM_MIN_ADDRESS
,
105 .all_image_info_size
= 0,
109 .needs_dynlinker
= 0,
110 .prog_allocated_stack
= 0,
111 .prog_stack_size
= 0,
116 .min_vm_addr
= MACH_VM_MAX_ADDRESS
,
117 .max_vm_addr
= MACH_VM_MIN_ADDRESS
,
122 * Prototypes of static functions.
129 struct mach_header
*header
,
135 load_result_t
*result
140 struct load_command
*lcp
,
148 load_result_t
*result
153 struct uuid_command
*uulp
,
155 load_result_t
*result
160 struct linkedit_data_command
*lcp
,
165 load_result_t
*result
);
167 #if CONFIG_CODE_DECRYPTION
170 struct encryption_info_command
*lcp
,
176 cpu_subtype_t cpusubtype
);
182 struct entry_point_command
*epc
,
185 load_result_t
*result
190 struct thread_command
*tcp
,
193 load_result_t
*result
208 mach_vm_offset_t
*user_stack
,
217 mach_vm_offset_t
*entry_point
222 struct dylinker_command
*lcp
,
228 load_result_t
*result
237 struct mach_header
*mach_header
,
240 struct macho_data
*macho_data
,
245 widen_segment_command(const struct segment_command
*scp32
,
246 struct segment_command_64
*scp
)
248 scp
->cmd
= scp32
->cmd
;
249 scp
->cmdsize
= scp32
->cmdsize
;
250 bcopy(scp32
->segname
, scp
->segname
, sizeof(scp
->segname
));
251 scp
->vmaddr
= scp32
->vmaddr
;
252 scp
->vmsize
= scp32
->vmsize
;
253 scp
->fileoff
= scp32
->fileoff
;
254 scp
->filesize
= scp32
->filesize
;
255 scp
->maxprot
= scp32
->maxprot
;
256 scp
->initprot
= scp32
->initprot
;
257 scp
->nsects
= scp32
->nsects
;
258 scp
->flags
= scp32
->flags
;
262 note_all_image_info_section(const struct segment_command_64
*scp
,
263 boolean_t is64
, size_t section_size
, const void *sections
,
264 int64_t slide
, load_result_t
*result
)
268 struct section_64 s64
;
272 if (strncmp(scp
->segname
, "__DATA", sizeof(scp
->segname
)) != 0)
274 for (i
= 0; i
< scp
->nsects
; ++i
) {
275 sectionp
= (const void *)
276 ((const char *)sections
+ section_size
* i
);
277 if (0 == strncmp(sectionp
->s64
.sectname
, "__all_image_info",
278 sizeof(sectionp
->s64
.sectname
))) {
279 result
->all_image_info_addr
=
280 is64
? sectionp
->s64
.addr
: sectionp
->s32
.addr
;
281 result
->all_image_info_addr
+= slide
;
282 result
->all_image_info_size
=
283 is64
? sectionp
->s64
.size
: sectionp
->s32
.size
;
291 struct image_params
*imgp
,
292 struct mach_header
*header
,
295 load_result_t
*result
298 struct vnode
*vp
= imgp
->ip_vp
;
299 off_t file_offset
= imgp
->ip_arch_offset
;
300 off_t macho_size
= imgp
->ip_arch_size
;
301 off_t file_size
= imgp
->ip_vattr
->va_data_size
;
303 pmap_t pmap
= 0; /* protected by create_map */
306 task_t old_task
= TASK_NULL
; /* protected by create_map */
307 load_result_t myresult
;
309 boolean_t create_map
= FALSE
;
310 boolean_t enforce_hard_pagezero
= TRUE
;
311 int spawn
= (imgp
->ip_flags
& IMGPF_SPAWN
);
312 task_t task
= current_task();
313 proc_t p
= current_proc();
314 mach_vm_offset_t aslr_offset
= 0;
315 mach_vm_offset_t dyld_aslr_offset
= 0;
318 if (macho_size
> file_size
) {
319 return(LOAD_BADMACHO
);
322 if (new_map
== VM_MAP_NULL
) {
324 old_task
= current_task();
328 * If we are spawning, we have created backing objects for the process
329 * already, which include non-lazily creating the task map. So we
330 * are going to switch out the task map with one appropriate for the
331 * bitness of the image being loaded.
335 old_task
= get_threadtask(thread
);
340 if (imgp
->ip_new_thread
) {
341 ledger_task
= get_threadtask(imgp
->ip_new_thread
);
345 pmap
= pmap_create(get_task_ledger(ledger_task
),
347 (imgp
->ip_flags
& IMGPF_IS_64BIT
));
348 pal_switch_pmap(thread
, pmap
, imgp
->ip_flags
& IMGPF_IS_64BIT
);
349 map
= vm_map_create(pmap
,
351 vm_compute_max_offset((imgp
->ip_flags
& IMGPF_IS_64BIT
)),
357 #ifndef CONFIG_ENFORCE_SIGNED_CODE
358 /* This turns off faulting for executable pages, which allows
359 * to circumvent Code Signing Enforcement. The per process
360 * flag (CS_ENFORCEMENT) is not set yet, but we can use the
363 if ( !cs_enforcement(NULL
) && (header
->flags
& MH_ALLOW_STACK_EXECUTION
) )
364 vm_map_disable_NX(map
);
367 /* Forcibly disallow execution from data pages on even if the arch
368 * normally permits it. */
369 if ((header
->flags
& MH_NO_HEAP_EXECUTION
) && !(imgp
->ip_flags
& IMGPF_ALLOW_DATA_EXEC
))
370 vm_map_disallow_data_exec(map
);
373 * Compute a random offset for ASLR, and an independent random offset for dyld.
375 if (!(imgp
->ip_flags
& IMGPF_DISABLE_ASLR
)) {
376 uint64_t max_slide_pages
;
378 max_slide_pages
= vm_map_get_max_aslr_slide_pages(map
);
380 aslr_offset
= random();
381 aslr_offset
%= max_slide_pages
;
382 aslr_offset
<<= vm_map_page_shift(map
);
384 dyld_aslr_offset
= random();
385 dyld_aslr_offset
%= max_slide_pages
;
386 dyld_aslr_offset
<<= vm_map_page_shift(map
);
392 *result
= load_result_null
;
394 lret
= parse_machfile(vp
, map
, thread
, header
, file_offset
, macho_size
,
395 0, (int64_t)aslr_offset
, (int64_t)dyld_aslr_offset
, result
);
397 if (lret
!= LOAD_SUCCESS
) {
399 vm_map_deallocate(map
); /* will lose pmap reference too */
406 * On x86, for compatibility, don't enforce the hard page-zero restriction for 32-bit binaries.
408 if ((imgp
->ip_flags
& IMGPF_IS_64BIT
) == 0) {
409 enforce_hard_pagezero
= FALSE
;
413 * Check to see if the page zero is enforced by the map->min_offset.
415 if (enforce_hard_pagezero
&& (vm_map_has_hard_pagezero(map
, 0x1000) == FALSE
)) {
417 vm_map_deallocate(map
); /* will lose pmap reference too */
419 printf("Cannot enforce a hard page-zero for %s\n", imgp
->ip_strings
);
420 return (LOAD_BADMACHO
);
426 * Swap the new map for the old, which consumes our new map
427 * reference but each leaves us responsible for the old_map reference.
428 * That lets us get off the pmap associated with it, and
429 * then we can release it.
434 * If this is an exec, then we are going to destroy the old
435 * task, and it's correct to halt it; if it's spawn, the
436 * task is not yet running, and it makes no sense.
440 * Mark the task as halting and start the other
441 * threads towards terminating themselves. Then
442 * make sure any threads waiting for a process
443 * transition get informed that we are committed to
444 * this transition, and then finally complete the
445 * task halting (wait for threads and then cleanup
448 * NOTE: task_start_halt() makes sure that no new
449 * threads are created in the task during the transition.
450 * We need to mark the workqueue as exiting before we
451 * wait for threads to terminate (at the end of which
452 * we no longer have a prohibition on thread creation).
454 * Finally, clean up any lingering workqueue data structures
455 * that may have been left behind by the workqueue threads
456 * as they exited (and then clean up the work queue itself).
458 kret
= task_start_halt(task
);
459 if (kret
!= KERN_SUCCESS
) {
460 vm_map_deallocate(map
); /* will lose pmap reference too */
461 return (LOAD_FAILURE
);
463 proc_transcommit(p
, 0);
464 workqueue_mark_exiting(p
);
465 task_complete_halt(task
);
468 old_map
= swap_task_map(old_task
, thread
, map
, !spawn
);
469 vm_map_deallocate(old_map
);
471 return(LOAD_SUCCESS
);
475 * The file size of a mach-o file is limited to 32 bits; this is because
476 * this is the limit on the kalloc() of enough bytes for a mach_header and
477 * the contents of its sizeofcmds, which is currently constrained to 32
478 * bits in the file format itself. We read into the kernel buffer the
479 * commands section, and then parse it in order to parse the mach-o file
480 * format load_command segment(s). We are only interested in a subset of
481 * the total set of possible commands. If "map"==VM_MAP_NULL or
482 * "thread"==THREAD_NULL, do not make permament VM modifications,
483 * just preflight the parse.
491 struct mach_header
*header
,
496 int64_t dyld_aslr_offset
,
497 load_result_t
*result
501 struct load_command
*lcp
;
502 struct dylinker_command
*dlp
= 0;
503 integer_t dlarchbits
= 0;
505 load_return_t ret
= LOAD_SUCCESS
;
508 vm_size_t size
,kl_size
;
510 size_t oldoffset
; /* for overflow check */
512 proc_t p
= current_proc(); /* XXXX */
515 size_t mach_header_sz
= sizeof(struct mach_header
);
517 boolean_t got_code_signatures
= FALSE
;
520 if (header
->magic
== MH_MAGIC_64
||
521 header
->magic
== MH_CIGAM_64
) {
522 mach_header_sz
= sizeof(struct mach_header_64
);
526 * Break infinite recursion
529 return(LOAD_FAILURE
);
535 * Check to see if right machine type.
537 if (((cpu_type_t
)(header
->cputype
& ~CPU_ARCH_MASK
) != (cpu_type() & ~CPU_ARCH_MASK
)) ||
538 !grade_binary(header
->cputype
,
539 header
->cpusubtype
& ~CPU_SUBTYPE_MASK
))
540 return(LOAD_BADARCH
);
542 abi64
= ((header
->cputype
& CPU_ARCH_ABI64
) == CPU_ARCH_ABI64
);
544 switch (header
->filetype
) {
548 return (LOAD_FAILURE
);
554 return (LOAD_FAILURE
);
559 return (LOAD_FAILURE
);
563 * Get the pager for the file.
565 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
568 * Map portion that must be accessible directly into
571 if ((off_t
)(mach_header_sz
+ header
->sizeofcmds
) > macho_size
)
572 return(LOAD_BADMACHO
);
575 * Round size of Mach-O commands up to page boundry.
577 size
= round_page(mach_header_sz
+ header
->sizeofcmds
);
579 return(LOAD_BADMACHO
);
582 * Map the load commands into kernel memory.
586 kl_addr
= kalloc(size
);
587 addr
= (caddr_t
)kl_addr
;
589 return(LOAD_NOSPACE
);
591 error
= vn_rdwr(UIO_READ
, vp
, addr
, size
, file_offset
,
592 UIO_SYSSPACE
, 0, kauth_cred_get(), &resid
, p
);
595 kfree(kl_addr
, kl_size
);
596 return(LOAD_IOERROR
);
600 /* We must be able to read in as much as the mach_header indicated */
602 kfree(kl_addr
, kl_size
);
603 return(LOAD_BADMACHO
);
607 * For PIE and dyld, slide everything by the ASLR offset.
609 if ((header
->flags
& MH_PIE
) || (header
->filetype
== MH_DYLINKER
)) {
614 * Scan through the commands, processing each one as necessary.
615 * We parse in three passes through the headers:
616 * 1: thread state, uuid, code signature
618 * 3: dyld, encryption, check entry point
621 for (pass
= 1; pass
<= 3; pass
++) {
624 * Check that the entry point is contained in an executable segments
626 if ((pass
== 3) && (!result
->using_lcmain
&& result
->validentry
== 0)) {
627 thread_state_initialize(thread
);
633 * Loop through each of the load_commands indicated by the
634 * Mach-O header; if an absurd value is provided, we just
635 * run off the end of the reserved section by incrementing
636 * the offset too far, so we are implicitly fail-safe.
638 offset
= mach_header_sz
;
639 ncmds
= header
->ncmds
;
643 * Get a pointer to the command.
645 lcp
= (struct load_command
*)(addr
+ offset
);
647 offset
+= lcp
->cmdsize
;
650 * Perform prevalidation of the struct load_command
651 * before we attempt to use its contents. Invalid
652 * values are ones which result in an overflow, or
653 * which can not possibly be valid commands, or which
654 * straddle or exist past the reserved section at the
655 * start of the image.
657 if (oldoffset
> offset
||
658 lcp
->cmdsize
< sizeof(struct load_command
) ||
659 offset
> header
->sizeofcmds
+ mach_header_sz
) {
665 * Act on struct load_command's for which kernel
666 * intervention is required.
675 * Having an LC_SEGMENT command for the
676 * wrong ABI is invalid <rdar://problem/11021230>
682 ret
= load_segment(lcp
,
698 * Having an LC_SEGMENT_64 command for the
699 * wrong ABI is invalid <rdar://problem/11021230>
705 ret
= load_segment(lcp
,
718 ret
= load_unixthread(
719 (struct thread_command
*) lcp
,
730 (struct entry_point_command
*) lcp
,
735 case LC_LOAD_DYLINKER
:
738 if ((depth
== 1) && (dlp
== 0)) {
739 dlp
= (struct dylinker_command
*)lcp
;
740 dlarchbits
= (header
->cputype
& CPU_ARCH_MASK
);
746 if (pass
== 1 && depth
== 1) {
747 ret
= load_uuid((struct uuid_command
*) lcp
,
748 (char *)addr
+ mach_header_sz
+ header
->sizeofcmds
,
752 case LC_CODE_SIGNATURE
:
757 load signatures & store in uip
758 set VM object "signed_pages"
760 ret
= load_code_signature(
761 (struct linkedit_data_command
*) lcp
,
767 if (ret
!= LOAD_SUCCESS
) {
768 printf("proc %d: load code signature error %d "
770 p
->p_pid
, ret
, vp
->v_name
);
772 * Allow injections to be ignored on devices w/o enforcement enabled
774 if (!cs_enforcement(NULL
))
775 ret
= LOAD_SUCCESS
; /* ignore error */
778 got_code_signatures
= TRUE
;
781 if (got_code_signatures
) {
782 unsigned tainted
= CS_VALIDATE_TAINTED
;
783 boolean_t valid
= FALSE
;
784 struct cs_blob
*blobs
;
789 printf("validating initial pages of %s\n", vp
->v_name
);
790 blobs
= ubc_get_cs_blobs(vp
);
792 while (off
< size
&& ret
== LOAD_SUCCESS
) {
793 tainted
= CS_VALIDATE_TAINTED
;
795 valid
= cs_validate_page(blobs
,
800 if (!valid
|| (tainted
& CS_VALIDATE_TAINTED
)) {
802 printf("CODE SIGNING: %s[%d]: invalid initial page at offset %lld validated:%d tainted:%d csflags:0x%x\n",
803 vp
->v_name
, p
->p_pid
, (long long)(file_offset
+ off
), valid
, tainted
, result
->csflags
);
804 if (cs_enforcement(NULL
) ||
805 (result
->csflags
& (CS_HARD
|CS_KILL
|CS_ENFORCEMENT
))) {
808 result
->csflags
&= ~CS_VALID
;
815 #if CONFIG_CODE_DECRYPTION
816 case LC_ENCRYPTION_INFO
:
817 case LC_ENCRYPTION_INFO_64
:
820 ret
= set_code_unprotect(
821 (struct encryption_info_command
*) lcp
,
822 addr
, map
, slide
, vp
,
823 header
->cputype
, header
->cpusubtype
);
824 if (ret
!= LOAD_SUCCESS
) {
825 printf("proc %d: set_code_unprotect() error %d "
827 p
->p_pid
, ret
, vp
->v_name
);
829 * Don't let the app run if it's
830 * encrypted but we failed to set up the
831 * decrypter. If the keys are missing it will
832 * return LOAD_DECRYPTFAIL.
834 if (ret
== LOAD_DECRYPTFAIL
) {
835 /* failed to load due to missing FP keys */
837 p
->p_lflag
|= P_LTERM_DECRYPTFAIL
;
845 /* Other commands are ignored by the kernel */
849 if (ret
!= LOAD_SUCCESS
)
852 if (ret
!= LOAD_SUCCESS
)
856 if (ret
== LOAD_SUCCESS
) {
857 if (! got_code_signatures
) {
858 if (cs_enforcement(NULL
)) {
862 * No embedded signatures: look for detached by taskgated,
863 * this is only done on OSX, on embedded platforms we expect everything
864 * to be have embedded signatures.
866 struct cs_blob
*blob
;
868 blob
= ubc_cs_blob_get(vp
, -1, file_offset
);
870 unsigned int cs_flag_data
= blob
->csb_flags
;
871 if(0 != ubc_cs_generation_check(vp
)) {
872 if (0 != ubc_cs_blob_revalidate(vp
, blob
, 0)) {
873 /* clear out the flag data if revalidation fails */
875 result
->csflags
&= ~CS_VALID
;
878 /* get flags to be applied to the process */
879 result
->csflags
|= cs_flag_data
;
884 /* Make sure if we need dyld, we got it */
885 if ((ret
== LOAD_SUCCESS
) && result
->needs_dynlinker
&& !dlp
) {
889 if ((ret
== LOAD_SUCCESS
) && (dlp
!= 0)) {
891 * load the dylinker, and slide it by the independent DYLD ASLR
892 * offset regardless of the PIE-ness of the main binary.
894 ret
= load_dylinker(dlp
, dlarchbits
, map
, thread
, depth
,
895 dyld_aslr_offset
, result
);
898 if((ret
== LOAD_SUCCESS
) && (depth
== 1)) {
899 if (result
->thread_count
== 0) {
906 kfree(kl_addr
, kl_size
);
911 #if CONFIG_CODE_DECRYPTION
913 #define APPLE_UNPROTECTED_HEADER_SIZE (3 * PAGE_SIZE_64)
916 unprotect_dsmos_segment(
922 vm_map_offset_t map_addr
,
923 vm_map_size_t map_size
)
928 * The first APPLE_UNPROTECTED_HEADER_SIZE bytes (from offset 0 of
929 * this part of a Universal binary) are not protected...
930 * The rest needs to be "transformed".
932 if (file_off
<= APPLE_UNPROTECTED_HEADER_SIZE
&&
933 file_off
+ file_size
<= APPLE_UNPROTECTED_HEADER_SIZE
) {
934 /* it's all unprotected, nothing to do... */
937 if (file_off
<= APPLE_UNPROTECTED_HEADER_SIZE
) {
939 * We start mapping in the unprotected area.
940 * Skip the unprotected part...
942 vm_map_offset_t delta
;
944 delta
= APPLE_UNPROTECTED_HEADER_SIZE
;
949 /* ... transform the rest of the mapping. */
950 struct pager_crypt_info crypt_info
;
951 crypt_info
.page_decrypt
= dsmos_page_transform
;
952 crypt_info
.crypt_ops
= NULL
;
953 crypt_info
.crypt_end
= NULL
;
954 #pragma unused(vp, macho_offset)
955 crypt_info
.crypt_ops
= (void *)0x2e69cf40;
956 kr
= vm_map_apple_protected(map
,
962 if (kr
!= KERN_SUCCESS
) {
967 #else /* CONFIG_CODE_DECRYPTION */
969 unprotect_dsmos_segment(
970 __unused
uint64_t file_off
,
971 __unused
uint64_t file_size
,
972 __unused
struct vnode
*vp
,
973 __unused off_t macho_offset
,
974 __unused vm_map_t map
,
975 __unused vm_map_offset_t map_addr
,
976 __unused vm_map_size_t map_size
)
980 #endif /* CONFIG_CODE_DECRYPTION */
985 struct load_command
*lcp
,
993 load_result_t
*result
996 struct segment_command_64 segment_command
, *scp
;
998 vm_map_offset_t map_addr
, map_offset
;
999 vm_map_size_t map_size
, seg_size
, delta_size
;
1002 size_t segment_command_size
, total_section_size
,
1003 single_section_size
;
1005 if (LC_SEGMENT_64
== lcp
->cmd
) {
1006 segment_command_size
= sizeof(struct segment_command_64
);
1007 single_section_size
= sizeof(struct section_64
);
1009 segment_command_size
= sizeof(struct segment_command
);
1010 single_section_size
= sizeof(struct section
);
1012 if (lcp
->cmdsize
< segment_command_size
)
1013 return (LOAD_BADMACHO
);
1014 total_section_size
= lcp
->cmdsize
- segment_command_size
;
1016 if (LC_SEGMENT_64
== lcp
->cmd
)
1017 scp
= (struct segment_command_64
*)lcp
;
1019 scp
= &segment_command
;
1020 widen_segment_command((struct segment_command
*)lcp
, scp
);
1024 * Make sure what we get from the file is really ours (as specified
1027 if (scp
->fileoff
+ scp
->filesize
< scp
->fileoff
||
1028 scp
->fileoff
+ scp
->filesize
> (uint64_t)macho_size
)
1029 return (LOAD_BADMACHO
);
1031 * Ensure that the number of sections specified would fit
1032 * within the load command size.
1034 if (total_section_size
/ single_section_size
< scp
->nsects
)
1035 return (LOAD_BADMACHO
);
1037 * Make sure the segment is page-aligned in the file.
1039 if ((scp
->fileoff
& PAGE_MASK_64
) != 0)
1040 return (LOAD_BADMACHO
);
1043 * If we have a code signature attached for this slice
1044 * require that the segments are within the signed part
1047 if (result
->cs_end_offset
&&
1048 result
->cs_end_offset
< (off_t
)scp
->fileoff
&&
1049 result
->cs_end_offset
- scp
->fileoff
< scp
->filesize
)
1052 printf("section outside code signature\n");
1053 return LOAD_BADMACHO
;
1057 * Round sizes to page size.
1059 seg_size
= round_page_64(scp
->vmsize
);
1060 map_size
= round_page_64(scp
->filesize
);
1061 map_addr
= trunc_page_64(scp
->vmaddr
); /* JVXXX note that in XNU TOT this is round instead of trunc for 64 bits */
1063 seg_size
= vm_map_round_page(seg_size
, vm_map_page_mask(map
));
1064 map_size
= vm_map_round_page(map_size
, vm_map_page_mask(map
));
1067 return (KERN_SUCCESS
);
1068 if (map_addr
== 0 &&
1071 (scp
->initprot
& VM_PROT_ALL
) == VM_PROT_NONE
&&
1072 (scp
->maxprot
& VM_PROT_ALL
) == VM_PROT_NONE
) {
1074 * For PIE, extend page zero rather than moving it. Extending
1075 * page zero keeps early allocations from falling predictably
1076 * between the end of page zero and the beginning of the first
1083 * This is a "page zero" segment: it starts at address 0,
1084 * is not mapped from the binary file and is not accessible.
1085 * User-space should never be able to access that memory, so
1086 * make it completely off limits by raising the VM map's
1089 ret
= vm_map_raise_min_offset(map
, seg_size
);
1090 if (ret
!= KERN_SUCCESS
) {
1091 return (LOAD_FAILURE
);
1093 return (LOAD_SUCCESS
);
1096 /* If a non-zero slide was specified by the caller, apply now */
1099 if (map_addr
< result
->min_vm_addr
)
1100 result
->min_vm_addr
= map_addr
;
1101 if (map_addr
+seg_size
> result
->max_vm_addr
)
1102 result
->max_vm_addr
= map_addr
+seg_size
;
1104 if (map
== VM_MAP_NULL
)
1105 return (LOAD_SUCCESS
);
1107 map_offset
= pager_offset
+ scp
->fileoff
; /* limited to 32 bits */
1110 initprot
= (scp
->initprot
) & VM_PROT_ALL
;
1111 maxprot
= (scp
->maxprot
) & VM_PROT_ALL
;
1113 * Map a copy of the file into the address space.
1115 ret
= vm_map_enter_mem_object_control(map
,
1116 &map_addr
, map_size
, (mach_vm_offset_t
)0,
1117 VM_FLAGS_FIXED
, control
, map_offset
, TRUE
,
1119 VM_INHERIT_DEFAULT
);
1120 if (ret
!= KERN_SUCCESS
) {
1121 return (LOAD_NOSPACE
);
1125 * If the file didn't end on a page boundary,
1126 * we need to zero the leftover.
1128 delta_size
= map_size
- scp
->filesize
;
1130 if (delta_size
> 0) {
1131 mach_vm_offset_t tmp
;
1133 ret
= mach_vm_allocate(kernel_map
, &tmp
, delta_size
, VM_FLAGS_ANYWHERE
);
1134 if (ret
!= KERN_SUCCESS
)
1135 return(LOAD_RESOURCE
);
1137 if (copyout(tmp
, map_addr
+ scp
->filesize
,
1139 (void) mach_vm_deallocate(
1140 kernel_map
, tmp
, delta_size
);
1141 return (LOAD_FAILURE
);
1144 (void) mach_vm_deallocate(kernel_map
, tmp
, delta_size
);
1150 * If the virtual size of the segment is greater
1151 * than the size from the file, we need to allocate
1152 * zero fill memory for the rest.
1154 delta_size
= seg_size
- map_size
;
1155 if (delta_size
> 0) {
1156 mach_vm_offset_t tmp
= map_addr
+ map_size
;
1158 ret
= mach_vm_map(map
, &tmp
, delta_size
, 0, VM_FLAGS_FIXED
,
1160 scp
->initprot
, scp
->maxprot
,
1161 VM_INHERIT_DEFAULT
);
1162 if (ret
!= KERN_SUCCESS
)
1163 return(LOAD_NOSPACE
);
1166 if ( (scp
->fileoff
== 0) && (scp
->filesize
!= 0) )
1167 result
->mach_header
= map_addr
;
1169 if (scp
->flags
& SG_PROTECTED_VERSION_1
) {
1170 ret
= unprotect_dsmos_segment(scp
->fileoff
,
1180 if (LOAD_SUCCESS
== ret
&& filetype
== MH_DYLINKER
&&
1181 result
->all_image_info_addr
== MACH_VM_MIN_ADDRESS
)
1182 note_all_image_info_section(scp
,
1183 LC_SEGMENT_64
== lcp
->cmd
, single_section_size
,
1184 (const char *)lcp
+ segment_command_size
, slide
, result
);
1186 if (result
->entry_point
!= MACH_VM_MIN_ADDRESS
) {
1187 if ((result
->entry_point
>= map_addr
) && (result
->entry_point
< (map_addr
+ map_size
))) {
1188 if ((scp
->initprot
& (VM_PROT_READ
|VM_PROT_EXECUTE
)) == (VM_PROT_READ
|VM_PROT_EXECUTE
)) {
1189 result
->validentry
= 1;
1191 /* right range but wrong protections, unset if previously validated */
1192 result
->validentry
= 0;
1203 struct uuid_command
*uulp
,
1205 load_result_t
*result
1209 * We need to check the following for this command:
1210 * - The command size should be atleast the size of struct uuid_command
1211 * - The UUID part of the command should be completely within the mach-o header
1214 if ((uulp
->cmdsize
< sizeof(struct uuid_command
)) ||
1215 (((char *)uulp
+ sizeof(struct uuid_command
)) > command_end
)) {
1216 return (LOAD_BADMACHO
);
1219 memcpy(&result
->uuid
[0], &uulp
->uuid
[0], sizeof(result
->uuid
));
1220 return (LOAD_SUCCESS
);
1226 struct entry_point_command
*epc
,
1229 load_result_t
*result
1232 mach_vm_offset_t addr
;
1235 if (epc
->cmdsize
< sizeof(*epc
))
1236 return (LOAD_BADMACHO
);
1237 if (result
->thread_count
!= 0) {
1238 return (LOAD_FAILURE
);
1241 if (thread
== THREAD_NULL
)
1242 return (LOAD_SUCCESS
);
1244 /* LC_MAIN specifies stack size but not location */
1245 if (epc
->stacksize
) {
1246 result
->prog_stack_size
= 1;
1247 result
->user_stack_size
= epc
->stacksize
;
1249 result
->prog_stack_size
= 0;
1250 result
->user_stack_size
= MAXSSIZ
;
1252 result
->prog_allocated_stack
= 0;
1254 /* use default location for stack */
1255 ret
= thread_userstackdefault(thread
, &addr
);
1256 if (ret
!= KERN_SUCCESS
)
1257 return(LOAD_FAILURE
);
1259 /* The stack slides down from the default location */
1260 result
->user_stack
= addr
;
1261 result
->user_stack
-= slide
;
1263 if (result
->using_lcmain
|| result
->entry_point
!= MACH_VM_MIN_ADDRESS
) {
1264 /* Already processed LC_MAIN or LC_UNIXTHREAD */
1265 return (LOAD_FAILURE
);
1268 /* kernel does *not* use entryoff from LC_MAIN. Dyld uses it. */
1269 result
->needs_dynlinker
= TRUE
;
1270 result
->using_lcmain
= TRUE
;
1272 ret
= thread_state_initialize( thread
);
1273 if (ret
!= KERN_SUCCESS
) {
1274 return(LOAD_FAILURE
);
1277 result
->unixproc
= TRUE
;
1278 result
->thread_count
++;
1280 return(LOAD_SUCCESS
);
1287 struct thread_command
*tcp
,
1290 load_result_t
*result
1295 mach_vm_offset_t addr
;
1297 if (tcp
->cmdsize
< sizeof(*tcp
))
1298 return (LOAD_BADMACHO
);
1299 if (result
->thread_count
!= 0) {
1300 return (LOAD_FAILURE
);
1303 if (thread
== THREAD_NULL
)
1304 return (LOAD_SUCCESS
);
1306 ret
= load_threadstack(thread
,
1307 (uint32_t *)(((vm_offset_t
)tcp
) +
1308 sizeof(struct thread_command
)),
1309 tcp
->cmdsize
- sizeof(struct thread_command
),
1312 if (ret
!= LOAD_SUCCESS
)
1315 /* LC_UNIXTHREAD optionally specifies stack size and location */
1318 result
->prog_stack_size
= 0; /* unknown */
1319 result
->prog_allocated_stack
= 1;
1321 result
->prog_allocated_stack
= 0;
1322 result
->prog_stack_size
= 0;
1323 result
->user_stack_size
= MAXSSIZ
;
1326 /* The stack slides down from the default location */
1327 result
->user_stack
= addr
;
1328 result
->user_stack
-= slide
;
1330 ret
= load_threadentry(thread
,
1331 (uint32_t *)(((vm_offset_t
)tcp
) +
1332 sizeof(struct thread_command
)),
1333 tcp
->cmdsize
- sizeof(struct thread_command
),
1335 if (ret
!= LOAD_SUCCESS
)
1338 if (result
->using_lcmain
|| result
->entry_point
!= MACH_VM_MIN_ADDRESS
) {
1339 /* Already processed LC_MAIN or LC_UNIXTHREAD */
1340 return (LOAD_FAILURE
);
1343 result
->entry_point
= addr
;
1344 result
->entry_point
+= slide
;
1346 ret
= load_threadstate(thread
,
1347 (uint32_t *)(((vm_offset_t
)tcp
) +
1348 sizeof(struct thread_command
)),
1349 tcp
->cmdsize
- sizeof(struct thread_command
));
1350 if (ret
!= LOAD_SUCCESS
)
1353 result
->unixproc
= TRUE
;
1354 result
->thread_count
++;
1356 return(LOAD_SUCCESS
);
1370 uint32_t thread_size
;
1372 uint32_t local_ts_size
;
1377 ret
= thread_state_initialize( thread
);
1378 if (ret
!= KERN_SUCCESS
) {
1383 if (total_size
> 0) {
1384 local_ts_size
= total_size
;
1385 local_ts
= kalloc(local_ts_size
);
1386 if (local_ts
== NULL
) {
1390 memcpy(local_ts
, ts
, local_ts_size
);
1395 * Set the new thread state; iterate through the state flavors in
1398 while (total_size
> 0) {
1401 if (UINT32_MAX
-2 < size
||
1402 UINT32_MAX
/sizeof(uint32_t) < size
+2) {
1403 ret
= LOAD_BADMACHO
;
1406 thread_size
= (size
+2)*sizeof(uint32_t);
1407 if (thread_size
> total_size
) {
1408 ret
= LOAD_BADMACHO
;
1411 total_size
-= thread_size
;
1413 * Third argument is a kernel space pointer; it gets cast
1414 * to the appropriate type in machine_thread_set_state()
1415 * based on the value of flavor.
1417 ret
= thread_setstatus(thread
, flavor
, (thread_state_t
)ts
, size
);
1418 if (ret
!= KERN_SUCCESS
) {
1422 ts
+= size
; /* ts is a (uint32_t *) */
1427 if (local_ts
!= NULL
) {
1428 kfree(local_ts
, local_ts_size
);
1439 uint32_t total_size
,
1440 mach_vm_offset_t
*user_stack
,
1447 uint32_t stack_size
;
1449 while (total_size
> 0) {
1452 if (UINT32_MAX
-2 < size
||
1453 UINT32_MAX
/sizeof(uint32_t) < size
+2)
1454 return (LOAD_BADMACHO
);
1455 stack_size
= (size
+2)*sizeof(uint32_t);
1456 if (stack_size
> total_size
)
1457 return(LOAD_BADMACHO
);
1458 total_size
-= stack_size
;
1461 * Third argument is a kernel space pointer; it gets cast
1462 * to the appropriate type in thread_userstack() based on
1463 * the value of flavor.
1465 ret
= thread_userstack(thread
, flavor
, (thread_state_t
)ts
, size
, user_stack
, customstack
);
1466 if (ret
!= KERN_SUCCESS
) {
1467 return(LOAD_FAILURE
);
1469 ts
+= size
; /* ts is a (uint32_t *) */
1471 return(LOAD_SUCCESS
);
1479 uint32_t total_size
,
1480 mach_vm_offset_t
*entry_point
1486 uint32_t entry_size
;
1489 * Set the thread state.
1491 *entry_point
= MACH_VM_MIN_ADDRESS
;
1492 while (total_size
> 0) {
1495 if (UINT32_MAX
-2 < size
||
1496 UINT32_MAX
/sizeof(uint32_t) < size
+2)
1497 return (LOAD_BADMACHO
);
1498 entry_size
= (size
+2)*sizeof(uint32_t);
1499 if (entry_size
> total_size
)
1500 return(LOAD_BADMACHO
);
1501 total_size
-= entry_size
;
1503 * Third argument is a kernel space pointer; it gets cast
1504 * to the appropriate type in thread_entrypoint() based on
1505 * the value of flavor.
1507 ret
= thread_entrypoint(thread
, flavor
, (thread_state_t
)ts
, size
, entry_point
);
1508 if (ret
!= KERN_SUCCESS
) {
1509 return(LOAD_FAILURE
);
1511 ts
+= size
; /* ts is a (uint32_t *) */
1513 return(LOAD_SUCCESS
);
1517 struct nameidata __nid
;
1518 union macho_vnode_header
{
1519 struct mach_header mach_header
;
1520 struct fat_header fat_header
;
1525 #define DEFAULT_DYLD_PATH "/usr/lib/dyld"
1527 static load_return_t
1529 struct dylinker_command
*lcp
,
1535 load_result_t
*result
1540 struct vnode
*vp
= NULLVP
; /* set by get_macho_vnode() */
1541 struct mach_header
*header
;
1542 off_t file_offset
= 0; /* set by get_macho_vnode() */
1543 off_t macho_size
= 0; /* set by get_macho_vnode() */
1544 load_result_t
*myresult
;
1546 struct macho_data
*macho_data
;
1548 struct mach_header __header
;
1549 load_result_t __myresult
;
1550 struct macho_data __macho_data
;
1553 if (lcp
->cmdsize
< sizeof(*lcp
))
1554 return (LOAD_BADMACHO
);
1556 name
= (char *)lcp
+ lcp
->name
.offset
;
1558 * Check for a proper null terminated string.
1562 if (p
>= (char *)lcp
+ lcp
->cmdsize
)
1563 return(LOAD_BADMACHO
);
1566 #if !(DEVELOPMENT || DEBUG)
1567 if (0 != strcmp(name
, DEFAULT_DYLD_PATH
)) {
1568 return (LOAD_BADMACHO
);
1572 /* Allocate wad-of-data from heap to reduce excessively deep stacks */
1574 MALLOC(dyld_data
, void *, sizeof (*dyld_data
), M_TEMP
, M_WAITOK
);
1575 header
= &dyld_data
->__header
;
1576 myresult
= &dyld_data
->__myresult
;
1577 macho_data
= &dyld_data
->__macho_data
;
1579 ret
= get_macho_vnode(name
, archbits
, header
,
1580 &file_offset
, &macho_size
, macho_data
, &vp
);
1584 *myresult
= load_result_null
;
1587 * First try to map dyld in directly. This should work most of
1588 * the time since there shouldn't normally be something already
1589 * mapped to its address.
1592 ret
= parse_machfile(vp
, map
, thread
, header
, file_offset
,
1593 macho_size
, depth
, slide
, 0, myresult
);
1596 * If it turned out something was in the way, then we'll take
1597 * take this longer path to preflight dyld's vm ranges, then
1598 * map it at a free location in the address space.
1601 if (ret
== LOAD_NOSPACE
) {
1602 mach_vm_offset_t dyl_start
, map_addr
;
1603 mach_vm_size_t dyl_length
;
1604 int64_t slide_amount
;
1606 *myresult
= load_result_null
;
1609 * Preflight parsing the Mach-O file with a NULL
1610 * map, which will return the ranges needed for a
1611 * subsequent map attempt (with a slide) in "myresult"
1613 ret
= parse_machfile(vp
, VM_MAP_NULL
, THREAD_NULL
, header
,
1614 file_offset
, macho_size
, depth
,
1615 0 /* slide */, 0, myresult
);
1617 if (ret
!= LOAD_SUCCESS
) {
1621 dyl_start
= myresult
->min_vm_addr
;
1622 dyl_length
= myresult
->max_vm_addr
- myresult
->min_vm_addr
;
1624 dyl_length
+= slide
;
1626 /* To find an appropriate load address, do a quick allocation */
1627 map_addr
= dyl_start
;
1628 ret
= mach_vm_allocate(map
, &map_addr
, dyl_length
, VM_FLAGS_ANYWHERE
);
1629 if (ret
!= KERN_SUCCESS
) {
1634 ret
= mach_vm_deallocate(map
, map_addr
, dyl_length
);
1635 if (ret
!= KERN_SUCCESS
) {
1640 if (map_addr
< dyl_start
)
1641 slide_amount
= -(int64_t)(dyl_start
- map_addr
);
1643 slide_amount
= (int64_t)(map_addr
- dyl_start
);
1645 slide_amount
+= slide
;
1647 *myresult
= load_result_null
;
1649 ret
= parse_machfile(vp
, map
, thread
, header
,
1650 file_offset
, macho_size
, depth
,
1651 slide_amount
, 0, myresult
);
1658 if (ret
== LOAD_SUCCESS
) {
1659 result
->dynlinker
= TRUE
;
1660 result
->entry_point
= myresult
->entry_point
;
1661 result
->validentry
= myresult
->validentry
;
1662 result
->all_image_info_addr
= myresult
->all_image_info_addr
;
1663 result
->all_image_info_size
= myresult
->all_image_info_size
;
1664 if (myresult
->platform_binary
) {
1665 result
->csflags
|= CS_DYLD_PLATFORM
;
1671 FREE(dyld_data
, M_TEMP
);
1676 static load_return_t
1677 load_code_signature(
1678 struct linkedit_data_command
*lcp
,
1683 load_result_t
*result
)
1689 struct cs_blob
*blob
;
1691 vm_size_t blob_size
;
1696 if (lcp
->cmdsize
!= sizeof (struct linkedit_data_command
) ||
1697 lcp
->dataoff
+ lcp
->datasize
> macho_size
) {
1698 ret
= LOAD_BADMACHO
;
1702 blob
= ubc_cs_blob_get(vp
, cputype
, macho_offset
);
1704 /* we already have a blob for this vnode and cputype */
1705 if (blob
->csb_cpu_type
== cputype
&&
1706 blob
->csb_base_offset
== macho_offset
&&
1707 blob
->csb_mem_size
== lcp
->datasize
) {
1708 /* it matches the blob we want here, lets verify the version */
1709 if(0 != ubc_cs_generation_check(vp
)) {
1710 if (0 != ubc_cs_blob_revalidate(vp
, blob
, 0)) {
1711 ret
= LOAD_FAILURE
; /* set error same as from ubc_cs_blob_add */
1717 /* the blob has changed for this vnode: fail ! */
1718 ret
= LOAD_BADMACHO
;
1723 blob_size
= lcp
->datasize
;
1724 kr
= ubc_cs_blob_allocate(&addr
, &blob_size
);
1725 if (kr
!= KERN_SUCCESS
) {
1731 error
= vn_rdwr(UIO_READ
,
1735 macho_offset
+ lcp
->dataoff
,
1741 if (error
|| resid
!= 0) {
1746 if (ubc_cs_blob_add(vp
,
1755 /* ubc_cs_blob_add() has consumed "addr" */
1759 #if CHECK_CS_VALIDATION_BITMAP
1760 ubc_cs_validation_bitmap_allocate( vp
);
1763 blob
= ubc_cs_blob_get(vp
, cputype
, macho_offset
);
1767 if (ret
== LOAD_SUCCESS
) {
1768 result
->csflags
|= blob
->csb_flags
;
1769 result
->platform_binary
= blob
->csb_platform_binary
;
1770 result
->cs_end_offset
= blob
->csb_end_offset
;
1773 ubc_cs_blob_deallocate(addr
, blob_size
);
1781 #if CONFIG_CODE_DECRYPTION
1783 static load_return_t
1785 struct encryption_info_command
*eip
,
1791 cpu_subtype_t cpusubtype
)
1794 pager_crypt_info_t crypt_info
;
1795 const char * cryptname
= 0;
1799 struct segment_command_64
*seg64
;
1800 struct segment_command
*seg32
;
1801 vm_map_offset_t map_offset
, map_size
;
1804 if (eip
->cmdsize
< sizeof(*eip
)) return LOAD_BADMACHO
;
1806 switch(eip
->cryptid
) {
1808 /* not encrypted, just an empty load command */
1809 return LOAD_SUCCESS
;
1811 cryptname
="com.apple.unfree";
1814 /* some random cryptid that you could manually put into
1815 * your binary if you want NULL */
1816 cryptname
="com.apple.null";
1819 return LOAD_BADMACHO
;
1822 if (map
== VM_MAP_NULL
) return (LOAD_SUCCESS
);
1823 if (NULL
== text_crypter_create
) return LOAD_FAILURE
;
1825 MALLOC_ZONE(vpath
, char *, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
1826 if(vpath
== NULL
) return LOAD_FAILURE
;
1829 result
= vn_getpath(vp
, vpath
, &len
);
1831 FREE_ZONE(vpath
, MAXPATHLEN
, M_NAMEI
);
1832 return LOAD_FAILURE
;
1835 /* set up decrypter first */
1836 crypt_file_data_t crypt_data
= {
1839 .cpusubtype
= cpusubtype
};
1840 kr
=text_crypter_create(&crypt_info
, cryptname
, (void*)&crypt_data
);
1841 FREE_ZONE(vpath
, MAXPATHLEN
, M_NAMEI
);
1844 printf("set_code_unprotect: unable to create decrypter %s, kr=%d\n",
1846 if (kr
== kIOReturnNotPrivileged
) {
1847 /* text encryption returned decryption failure */
1848 return(LOAD_DECRYPTFAIL
);
1850 return LOAD_RESOURCE
;
1853 /* this is terrible, but we have to rescan the load commands to find the
1854 * virtual address of this encrypted stuff. This code is gonna look like
1855 * the dyld source one day... */
1856 struct mach_header
*header
= (struct mach_header
*)addr
;
1857 size_t mach_header_sz
= sizeof(struct mach_header
);
1858 if (header
->magic
== MH_MAGIC_64
||
1859 header
->magic
== MH_CIGAM_64
) {
1860 mach_header_sz
= sizeof(struct mach_header_64
);
1862 offset
= mach_header_sz
;
1863 uint32_t ncmds
= header
->ncmds
;
1866 * Get a pointer to the command.
1868 struct load_command
*lcp
= (struct load_command
*)(addr
+ offset
);
1869 offset
+= lcp
->cmdsize
;
1873 seg64
= (struct segment_command_64
*)lcp
;
1874 if ((seg64
->fileoff
<= eip
->cryptoff
) &&
1875 (seg64
->fileoff
+seg64
->filesize
>=
1876 eip
->cryptoff
+eip
->cryptsize
)) {
1877 map_offset
= seg64
->vmaddr
+ eip
->cryptoff
- seg64
->fileoff
+ slide
;
1878 map_size
= eip
->cryptsize
;
1882 seg32
= (struct segment_command
*)lcp
;
1883 if ((seg32
->fileoff
<= eip
->cryptoff
) &&
1884 (seg32
->fileoff
+seg32
->filesize
>=
1885 eip
->cryptoff
+eip
->cryptsize
)) {
1886 map_offset
= seg32
->vmaddr
+ eip
->cryptoff
- seg32
->fileoff
+ slide
;
1887 map_size
= eip
->cryptsize
;
1893 /* if we get here, did not find anything */
1894 return LOAD_BADMACHO
;
1897 /* now remap using the decrypter */
1898 kr
= vm_map_apple_protected(map
, map_offset
, map_offset
+map_size
, &crypt_info
);
1900 printf("set_code_unprotect(): mapping failed with %x\n", kr
);
1901 crypt_info
.crypt_end(crypt_info
.crypt_ops
);
1902 return LOAD_PROTECT
;
1905 return LOAD_SUCCESS
;
1911 * This routine exists to support the load_dylinker().
1913 * This routine has its own, separate, understanding of the FAT file format,
1914 * which is terrifically unfortunate.
1921 struct mach_header
*mach_header
,
1924 struct macho_data
*data
,
1929 vfs_context_t ctx
= vfs_context_current();
1930 proc_t p
= vfs_context_proc(ctx
);
1931 kauth_cred_t kerncred
;
1932 struct nameidata
*ndp
= &data
->__nid
;
1934 struct fat_arch fat_arch
;
1937 union macho_vnode_header
*header
= &data
->__header
;
1938 off_t fsize
= (off_t
)0;
1941 * Capture the kernel credential for use in the actual read of the
1942 * file, since the user doing the execution may have execute rights
1943 * but not read rights, but to exec something, we have to either map
1944 * or read it into the new process address space, which requires
1945 * read rights. This is to deal with lack of common credential
1946 * serialization code which would treat NOCRED as "serialize 'root'".
1948 kerncred
= vfs_context_ucred(vfs_context_kernel());
1950 /* init the namei data to point the file user's program name */
1951 NDINIT(ndp
, LOOKUP
, OP_OPEN
, FOLLOW
| LOCKLEAF
, UIO_SYSSPACE
, CAST_USER_ADDR_T(path
), ctx
);
1953 if ((error
= namei(ndp
)) != 0) {
1954 if (error
== ENOENT
) {
1955 error
= LOAD_ENOENT
;
1957 error
= LOAD_FAILURE
;
1964 /* check for regular file */
1965 if (vp
->v_type
!= VREG
) {
1966 error
= LOAD_PROTECT
;
1971 if ((error
= vnode_size(vp
, &fsize
, ctx
)) != 0) {
1972 error
= LOAD_FAILURE
;
1976 /* Check mount point */
1977 if (vp
->v_mount
->mnt_flag
& MNT_NOEXEC
) {
1978 error
= LOAD_PROTECT
;
1983 if ((error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_EXECUTE
| KAUTH_VNODE_READ_DATA
, ctx
)) != 0) {
1984 error
= LOAD_PROTECT
;
1988 /* try to open it */
1989 if ((error
= VNOP_OPEN(vp
, FREAD
, ctx
)) != 0) {
1990 error
= LOAD_PROTECT
;
1994 if ((error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)header
, sizeof (*header
), 0,
1995 UIO_SYSSPACE
, IO_NODELOCKED
, kerncred
, &resid
, p
)) != 0) {
1996 error
= LOAD_IOERROR
;
2001 error
= LOAD_BADMACHO
;
2005 if (header
->mach_header
.magic
== MH_MAGIC
||
2006 header
->mach_header
.magic
== MH_MAGIC_64
) {
2008 } else if (OSSwapBigToHostInt32(header
->fat_header
.magic
) == FAT_MAGIC
) {
2011 error
= LOAD_BADMACHO
;
2017 error
= fatfile_validate_fatarches((vm_offset_t
)(&header
->fat_header
),
2019 if (error
!= LOAD_SUCCESS
) {
2023 /* Look up our architecture in the fat file. */
2024 error
= fatfile_getarch_with_bits(archbits
,
2025 (vm_offset_t
)(&header
->fat_header
), sizeof(*header
), &fat_arch
);
2026 if (error
!= LOAD_SUCCESS
)
2029 /* Read the Mach-O header out of it */
2030 error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)&header
->mach_header
,
2031 sizeof (header
->mach_header
), fat_arch
.offset
,
2032 UIO_SYSSPACE
, IO_NODELOCKED
, kerncred
, &resid
, p
);
2034 error
= LOAD_IOERROR
;
2039 error
= LOAD_BADMACHO
;
2043 /* Is this really a Mach-O? */
2044 if (header
->mach_header
.magic
!= MH_MAGIC
&&
2045 header
->mach_header
.magic
!= MH_MAGIC_64
) {
2046 error
= LOAD_BADMACHO
;
2050 *file_offset
= fat_arch
.offset
;
2051 *macho_size
= fat_arch
.size
;
2054 * Force get_macho_vnode() to fail if the architecture bits
2055 * do not match the expected architecture bits. This in
2056 * turn causes load_dylinker() to fail for the same reason,
2057 * so it ensures the dynamic linker and the binary are in
2058 * lock-step. This is potentially bad, if we ever add to
2059 * the CPU_ARCH_* bits any bits that are desirable but not
2060 * required, since the dynamic linker might work, but we will
2061 * refuse to load it because of this check.
2063 if ((cpu_type_t
)(header
->mach_header
.cputype
& CPU_ARCH_MASK
) != archbits
) {
2064 error
= LOAD_BADARCH
;
2069 *macho_size
= fsize
;
2072 *mach_header
= header
->mach_header
;
2075 ubc_setsize(vp
, fsize
);
2079 (void) VNOP_CLOSE(vp
, FREAD
, ctx
);