]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/mach_loader.c
xnu-1456.1.26.tar.gz
[apple/xnu.git] / bsd / kern / mach_loader.c
1 /*
2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * Copyright (C) 1988, 1989, NeXT, Inc.
30 *
31 * File: kern/mach_loader.c
32 * Author: Avadis Tevanian, Jr.
33 *
34 * Mach object file loader (kernel version, for now).
35 *
36 * 21-Jul-88 Avadis Tevanian, Jr. (avie) at NeXT
37 * Started.
38 */
39
40 #include <sys/param.h>
41 #include <sys/vnode_internal.h>
42 #include <sys/uio.h>
43 #include <sys/namei.h>
44 #include <sys/proc_internal.h>
45 #include <sys/kauth.h>
46 #include <sys/stat.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
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>
59
60 #include <machine/vmparam.h>
61 #include <machine/exec.h>
62
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>
71
72 #include <mach-o/fat.h>
73 #include <mach-o/loader.h>
74
75 #include <vm/pmap.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>
81
82
83 /*
84 * XXX vm/pmap.h should not treat these prototypes as MACH_KERNEL_PRIVATE
85 * when KERNEL is defined.
86 */
87 extern pmap_t pmap_create(vm_map_size_t size, boolean_t is_64bit);
88 extern void pmap_switch(pmap_t);
89
90 /*
91 * XXX kern/thread.h should not treat these prototypes as MACH_KERNEL_PRIVATE
92 * when KERNEL is defined.
93 */
94 extern kern_return_t thread_setstatus(thread_t thread, int flavor,
95 thread_state_t tstate,
96 mach_msg_type_number_t count);
97
98 extern kern_return_t thread_state_initialize(thread_t thread);
99
100
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 *);
107
108 extern kern_return_t memory_object_signed(memory_object_control_t control,
109 boolean_t is_signed);
110
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,
118 .thread_count = 0,
119 .unixproc = 0,
120 .dynlinker = 0,
121 .customstack = 0,
122 .csflags = 0,
123 .uuid = { 0 }
124 };
125
126 /*
127 * Prototypes of static functions.
128 */
129 static load_return_t
130 parse_machfile(
131 struct vnode *vp,
132 vm_map_t map,
133 thread_t thread,
134 struct mach_header *header,
135 off_t file_offset,
136 off_t macho_size,
137 int depth,
138 load_result_t *result
139 );
140
141 static load_return_t
142 load_segment(
143 struct load_command *lcp,
144 uint32_t filetype,
145 void *control,
146 off_t pager_offset,
147 off_t macho_size,
148 struct vnode *vp,
149 vm_map_t map,
150 load_result_t *result
151 );
152
153 int load_code_signature(
154 struct linkedit_data_command *lcp,
155 struct vnode *vp,
156 off_t macho_offset,
157 off_t macho_size,
158 cpu_type_t cputype,
159 load_result_t *result);
160
161 #if CONFIG_CODE_DECRYPTION
162 static load_return_t
163 set_code_unprotect(
164 struct encryption_info_command *lcp,
165 caddr_t addr,
166 vm_map_t map,
167 struct vnode *vp);
168 #endif
169
170 static load_return_t
171 load_unixthread(
172 struct thread_command *tcp,
173 thread_t thread,
174 load_result_t *result
175 );
176
177 static load_return_t
178 load_thread(
179 struct thread_command *tcp,
180 thread_t thread,
181 load_result_t *result
182 );
183
184 static load_return_t
185 load_threadstate(
186 thread_t thread,
187 uint32_t *ts,
188 uint32_t total_size
189 );
190
191 static load_return_t
192 load_threadstack(
193 thread_t thread,
194 uint32_t *ts,
195 uint32_t total_size,
196 user_addr_t *user_stack,
197 int *customstack
198 );
199
200 static load_return_t
201 load_threadentry(
202 thread_t thread,
203 uint32_t *ts,
204 uint32_t total_size,
205 mach_vm_offset_t *entry_point
206 );
207
208 static load_return_t
209 load_dylinker(
210 struct dylinker_command *lcp,
211 integer_t archbits,
212 vm_map_t map,
213 thread_t thread,
214 int depth,
215 load_result_t *result,
216 boolean_t is_64bit
217 );
218
219 static load_return_t
220 get_macho_vnode(
221 char *path,
222 integer_t archbits,
223 struct mach_header *mach_header,
224 off_t *file_offset,
225 off_t *macho_size,
226 struct vnode **vpp
227 );
228
229 static inline void
230 widen_segment_command(const struct segment_command *scp32,
231 struct segment_command_64 *scp)
232 {
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;
244 }
245
246 static void
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)
250 {
251 const union {
252 struct section s32;
253 struct section_64 s64;
254 } *sectionp;
255 unsigned int i;
256
257 if (strncmp(scp->segname, "__DATA", sizeof(scp->segname)) != 0)
258 return;
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;
268 return;
269 }
270 }
271 }
272
273
274 load_return_t
275 load_machfile(
276 struct image_params *imgp,
277 struct mach_header *header,
278 thread_t thread,
279 vm_map_t new_map,
280 load_result_t *result
281 )
282 {
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;
286
287 pmap_t pmap = 0; /* protected by create_map */
288 vm_map_t map;
289 vm_map_t old_map;
290 task_t old_task = TASK_NULL; /* protected by create_map */
291 load_result_t myresult;
292 load_return_t lret;
293 boolean_t create_map = FALSE;
294 int spawn = (imgp->ip_flags & IMGPF_SPAWN);
295 task_t task = current_task();
296
297 if (new_map == VM_MAP_NULL) {
298 create_map = TRUE;
299 old_task = current_task();
300 }
301
302 /*
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.
307 */
308 if (spawn) {
309 create_map = TRUE;
310 old_task = get_threadtask(thread);
311 }
312
313 if (create_map) {
314 pmap = pmap_create((vm_map_size_t) 0, (imgp->ip_flags & IMGPF_IS_64BIT));
315 map = vm_map_create(pmap,
316 0,
317 vm_compute_max_offset((imgp->ip_flags & IMGPF_IS_64BIT)),
318 TRUE);
319 } else
320 map = new_map;
321
322 if ( (header->flags & MH_ALLOW_STACK_EXECUTION) )
323 vm_map_disable_NX(map);
324
325 if (!result)
326 result = &myresult;
327
328 *result = load_result_null;
329
330 lret = parse_machfile(vp, map, thread, header, file_offset, macho_size,
331 0, result);
332
333 if (lret != LOAD_SUCCESS) {
334 if (create_map) {
335 vm_map_deallocate(map); /* will lose pmap reference too */
336 }
337 return(lret);
338 }
339
340 /*
341 * For 64-bit users, check for presence of a 4GB page zero
342 * which will enable the kernel to share the user's address space
343 * and hence avoid TLB flushes on kernel entry/exit
344 */
345
346 if ((imgp->ip_flags & IMGPF_IS_64BIT) &&
347 vm_map_has_4GB_pagezero(map))
348 vm_map_set_4GB_pagezero(map);
349
350 /*
351 * Commit to new map.
352 *
353 * Swap the new map for the old, which consumes our new map
354 * reference but each leaves us responsible for the old_map reference.
355 * That lets us get off the pmap associated with it, and
356 * then we can release it.
357 */
358
359 if (create_map) {
360 /*
361 * If this is an exec, then we are going to destory the old
362 * task, and it's correct to halt it; if it's spawn, the
363 * task is not yet running, and it makes no sense.
364 */
365 if (!spawn) {
366 /*
367 * Mark the task as halting and start the other
368 * threads towards terminating themselves. Then
369 * make sure any threads waiting for a process
370 * transition get informed that we are committed to
371 * this transition, and then finally complete the
372 * task halting (wait for threads and then cleanup
373 * task resources).
374 */
375 task_start_halt(task);
376 proc_transcommit(current_proc(), 0);
377 task_complete_halt(task);
378 }
379 old_map = swap_task_map(old_task, thread, map);
380 vm_map_clear_4GB_pagezero(old_map);
381 /* XXX L4 : For spawn the current task isn't running... */
382 if (!spawn)
383 pmap_switch(pmap); /* Make sure we are using the new pmap */
384 vm_map_deallocate(old_map);
385 }
386 return(LOAD_SUCCESS);
387 }
388
389 /*
390 * The file size of a mach-o file is limited to 32 bits; this is because
391 * this is the limit on the kalloc() of enough bytes for a mach_header and
392 * the contents of its sizeofcmds, which is currently constrained to 32
393 * bits in the file format itself. We read into the kernel buffer the
394 * commands section, and then parse it in order to parse the mach-o file
395 * format load_command segment(s). We are only interested in a subset of
396 * the total set of possible commands.
397 */
398 static
399 load_return_t
400 parse_machfile(
401 struct vnode *vp,
402 vm_map_t map,
403 thread_t thread,
404 struct mach_header *header,
405 off_t file_offset,
406 off_t macho_size,
407 int depth,
408 load_result_t *result
409 )
410 {
411 uint32_t ncmds;
412 struct load_command *lcp;
413 struct dylinker_command *dlp = 0;
414 struct uuid_command *uulp = 0;
415 integer_t dlarchbits = 0;
416 void * control;
417 load_return_t ret = LOAD_SUCCESS;
418 caddr_t addr;
419 void * kl_addr;
420 vm_size_t size,kl_size;
421 size_t offset;
422 size_t oldoffset; /* for overflow check */
423 int pass;
424 proc_t p = current_proc(); /* XXXX */
425 int error;
426 int resid=0;
427 task_t task;
428 size_t mach_header_sz = sizeof(struct mach_header);
429 boolean_t abi64;
430 boolean_t got_code_signatures = FALSE;
431
432 if (header->magic == MH_MAGIC_64 ||
433 header->magic == MH_CIGAM_64) {
434 mach_header_sz = sizeof(struct mach_header_64);
435 }
436
437 /*
438 * Break infinite recursion
439 */
440 if (depth > 6) {
441 return(LOAD_FAILURE);
442 }
443
444 task = (task_t)get_threadtask(thread);
445
446 depth++;
447
448 /*
449 * Check to see if right machine type.
450 */
451 if (((cpu_type_t)(header->cputype & ~CPU_ARCH_MASK) != cpu_type()) ||
452 !grade_binary(header->cputype,
453 header->cpusubtype & ~CPU_SUBTYPE_MASK))
454 return(LOAD_BADARCH);
455
456 abi64 = ((header->cputype & CPU_ARCH_ABI64) == CPU_ARCH_ABI64);
457
458 switch (header->filetype) {
459
460 case MH_OBJECT:
461 case MH_EXECUTE:
462 case MH_PRELOAD:
463 if (depth != 1) {
464 return (LOAD_FAILURE);
465 }
466 break;
467
468 case MH_FVMLIB:
469 case MH_DYLIB:
470 if (depth == 1) {
471 return (LOAD_FAILURE);
472 }
473 break;
474
475 case MH_DYLINKER:
476 if (depth != 2) {
477 return (LOAD_FAILURE);
478 }
479 break;
480
481 default:
482 return (LOAD_FAILURE);
483 }
484
485 /*
486 * Get the pager for the file.
487 */
488 control = ubc_getobject(vp, UBC_FLAGS_NONE);
489
490 /*
491 * Map portion that must be accessible directly into
492 * kernel's map.
493 */
494 if ((off_t)(mach_header_sz + header->sizeofcmds) > macho_size)
495 return(LOAD_BADMACHO);
496
497 /*
498 * Round size of Mach-O commands up to page boundry.
499 */
500 size = round_page(mach_header_sz + header->sizeofcmds);
501 if (size <= 0)
502 return(LOAD_BADMACHO);
503
504 /*
505 * Map the load commands into kernel memory.
506 */
507 addr = 0;
508 kl_size = size;
509 kl_addr = kalloc(size);
510 addr = (caddr_t)kl_addr;
511 if (addr == NULL)
512 return(LOAD_NOSPACE);
513
514 error = vn_rdwr(UIO_READ, vp, addr, size, file_offset,
515 UIO_SYSSPACE, 0, kauth_cred_get(), &resid, p);
516 if (error) {
517 if (kl_addr )
518 kfree(kl_addr, kl_size);
519 return(LOAD_IOERROR);
520 }
521
522 /*
523 * Scan through the commands, processing each one as necessary.
524 */
525 for (pass = 1; pass <= 2; pass++) {
526 /*
527 * Loop through each of the load_commands indicated by the
528 * Mach-O header; if an absurd value is provided, we just
529 * run off the end of the reserved section by incrementing
530 * the offset too far, so we are implicitly fail-safe.
531 */
532 offset = mach_header_sz;
533 ncmds = header->ncmds;
534 while (ncmds--) {
535 /*
536 * Get a pointer to the command.
537 */
538 lcp = (struct load_command *)(addr + offset);
539 oldoffset = offset;
540 offset += lcp->cmdsize;
541
542 /*
543 * Perform prevalidation of the struct load_command
544 * before we attempt to use its contents. Invalid
545 * values are ones which result in an overflow, or
546 * which can not possibly be valid commands, or which
547 * straddle or exist past the reserved section at the
548 * start of the image.
549 */
550 if (oldoffset > offset ||
551 lcp->cmdsize < sizeof(struct load_command) ||
552 offset > header->sizeofcmds + mach_header_sz) {
553 ret = LOAD_BADMACHO;
554 break;
555 }
556
557 /*
558 * Act on struct load_command's for which kernel
559 * intervention is required.
560 */
561 switch(lcp->cmd) {
562 case LC_SEGMENT:
563 case LC_SEGMENT_64:
564 if (pass != 1)
565 break;
566 ret = load_segment(lcp,
567 header->filetype,
568 control,
569 file_offset,
570 macho_size,
571 vp,
572 map,
573 result);
574 break;
575 case LC_THREAD:
576 if (pass != 2)
577 break;
578 ret = load_thread((struct thread_command *)lcp,
579 thread,
580 result);
581 break;
582 case LC_UNIXTHREAD:
583 if (pass != 2)
584 break;
585 ret = load_unixthread(
586 (struct thread_command *) lcp,
587 thread,
588 result);
589 break;
590 case LC_LOAD_DYLINKER:
591 if (pass != 2)
592 break;
593 if ((depth == 1) && (dlp == 0)) {
594 dlp = (struct dylinker_command *)lcp;
595 dlarchbits = (header->cputype & CPU_ARCH_MASK);
596 } else {
597 ret = LOAD_FAILURE;
598 }
599 break;
600 case LC_UUID:
601 if (pass == 2 && depth == 1) {
602 uulp = (struct uuid_command *)lcp;
603 memcpy(&result->uuid[0], &uulp->uuid[0], sizeof(result->uuid));
604 }
605 break;
606 case LC_CODE_SIGNATURE:
607 /* CODE SIGNING */
608 if (pass != 2)
609 break;
610 /* pager -> uip ->
611 load signatures & store in uip
612 set VM object "signed_pages"
613 */
614 ret = load_code_signature(
615 (struct linkedit_data_command *) lcp,
616 vp,
617 file_offset,
618 macho_size,
619 header->cputype,
620 (depth == 1) ? result : NULL);
621 if (ret != LOAD_SUCCESS) {
622 printf("proc %d: load code signature error %d "
623 "for file \"%s\"\n",
624 p->p_pid, ret, vp->v_name);
625 ret = LOAD_SUCCESS; /* ignore error */
626 } else {
627 got_code_signatures = TRUE;
628 }
629 break;
630 #if CONFIG_CODE_DECRYPTION
631 case LC_ENCRYPTION_INFO:
632 if (pass != 2)
633 break;
634 ret = set_code_unprotect(
635 (struct encryption_info_command *) lcp,
636 addr, map, vp);
637 if (ret != LOAD_SUCCESS) {
638 printf("proc %d: set_code_unprotect() error %d "
639 "for file \"%s\"\n",
640 p->p_pid, ret, vp->v_name);
641 /* Don't let the app run if it's
642 * encrypted but we failed to set up the
643 * decrypter */
644 psignal(p, SIGKILL);
645 }
646 break;
647 #endif
648 default:
649 /* Other commands are ignored by the kernel */
650 ret = LOAD_SUCCESS;
651 break;
652 }
653 if (ret != LOAD_SUCCESS)
654 break;
655 }
656 if (ret != LOAD_SUCCESS)
657 break;
658 }
659 if (ret == LOAD_SUCCESS) {
660 if (! got_code_signatures) {
661 struct cs_blob *blob;
662 /* no embedded signatures: look for detached ones */
663 blob = ubc_cs_blob_get(vp, -1, file_offset);
664 if (blob != NULL) {
665 /* get flags to be applied to the process */
666 result->csflags |= blob->csb_flags;
667 }
668 }
669
670 if (dlp != 0)
671 ret = load_dylinker(dlp, dlarchbits, map, thread, depth, result, abi64);
672
673 if(depth == 1) {
674 if (result->thread_count == 0) {
675 ret = LOAD_FAILURE;
676 } else if ( abi64 ) {
677 #ifdef __ppc__
678 /* Map in 64-bit commpage */
679 /*
680 * PPC51: ppc64 is limited to 51-bit addresses.
681 * Memory above that limit is handled specially
682 * at the pmap level.
683 *
684 * <rdar://6640492> -- wrong task for vfork()/spawn()
685 */
686 pmap_map_sharedpage(current_task(), get_map_pmap(map));
687 #endif /* __ppc__ */
688 }
689 }
690 }
691
692 if (kl_addr )
693 kfree(kl_addr, kl_size);
694
695 return(ret);
696 }
697
698 #if CONFIG_CODE_DECRYPTION
699
700 #define APPLE_UNPROTECTED_HEADER_SIZE (3 * PAGE_SIZE_64)
701
702 static load_return_t
703 unprotect_segment(
704 uint64_t file_off,
705 uint64_t file_size,
706 struct vnode *vp,
707 off_t macho_offset,
708 vm_map_t map,
709 vm_map_offset_t map_addr,
710 vm_map_size_t map_size)
711 {
712 kern_return_t kr;
713
714 /*
715 * The first APPLE_UNPROTECTED_HEADER_SIZE bytes (from offset 0 of
716 * this part of a Universal binary) are not protected...
717 * The rest needs to be "transformed".
718 */
719 if (file_off <= APPLE_UNPROTECTED_HEADER_SIZE &&
720 file_off + file_size <= APPLE_UNPROTECTED_HEADER_SIZE) {
721 /* it's all unprotected, nothing to do... */
722 kr = KERN_SUCCESS;
723 } else {
724 if (file_off <= APPLE_UNPROTECTED_HEADER_SIZE) {
725 /*
726 * We start mapping in the unprotected area.
727 * Skip the unprotected part...
728 */
729 vm_map_offset_t delta;
730
731 delta = APPLE_UNPROTECTED_HEADER_SIZE;
732 delta -= file_off;
733 map_addr += delta;
734 map_size -= delta;
735 }
736 /* ... transform the rest of the mapping. */
737 struct pager_crypt_info crypt_info;
738 crypt_info.page_decrypt = dsmos_page_transform;
739 crypt_info.crypt_ops = NULL;
740 crypt_info.crypt_end = NULL;
741 #pragma unused(vp, macho_offset)
742 crypt_info.crypt_ops = (void *)0x2e69cf40;
743 kr = vm_map_apple_protected(map,
744 map_addr,
745 map_addr + map_size,
746 &crypt_info);
747 }
748
749 if (kr != KERN_SUCCESS) {
750 return LOAD_FAILURE;
751 }
752 return LOAD_SUCCESS;
753 }
754 #else /* CONFIG_CODE_DECRYPTION */
755 static load_return_t
756 unprotect_segment(
757 __unused uint64_t file_off,
758 __unused uint64_t file_size,
759 __unused struct vnode *vp,
760 __unused off_t macho_offset,
761 __unused vm_map_t map,
762 __unused vm_map_offset_t map_addr,
763 __unused vm_map_size_t map_size)
764 {
765 return LOAD_SUCCESS;
766 }
767 #endif /* CONFIG_CODE_DECRYPTION */
768
769 static
770 load_return_t
771 load_segment(
772 struct load_command *lcp,
773 uint32_t filetype,
774 void * control,
775 off_t pager_offset,
776 off_t macho_size,
777 struct vnode *vp,
778 vm_map_t map,
779 load_result_t *result
780 )
781 {
782 struct segment_command_64 segment_command, *scp;
783 kern_return_t ret;
784 mach_vm_offset_t map_addr, map_offset;
785 mach_vm_size_t map_size, seg_size, delta_size;
786 vm_prot_t initprot;
787 vm_prot_t maxprot;
788 size_t segment_command_size, total_section_size,
789 single_section_size;
790
791 if (LC_SEGMENT_64 == lcp->cmd) {
792 segment_command_size = sizeof(struct segment_command_64);
793 single_section_size = sizeof(struct section_64);
794 scp = (struct segment_command_64 *)lcp;
795 } else {
796 segment_command_size = sizeof(struct segment_command);
797 single_section_size = sizeof(struct section);
798 scp = &segment_command;
799 widen_segment_command((struct segment_command *)lcp, scp);
800 }
801 if (lcp->cmdsize < segment_command_size)
802 return (LOAD_BADMACHO);
803 total_section_size = lcp->cmdsize - segment_command_size;
804
805 /*
806 * Make sure what we get from the file is really ours (as specified
807 * by macho_size).
808 */
809 if (scp->fileoff + scp->filesize < scp->fileoff ||
810 scp->fileoff + scp->filesize > (uint64_t)macho_size)
811 return (LOAD_BADMACHO);
812 /*
813 * Ensure that the number of sections specified would fit
814 * within the load command size.
815 */
816 if (total_section_size / single_section_size < scp->nsects)
817 return (LOAD_BADMACHO);
818 /*
819 * Make sure the segment is page-aligned in the file.
820 */
821 if ((scp->fileoff & PAGE_MASK_64) != 0)
822 return (LOAD_BADMACHO);
823
824 /*
825 * Round sizes to page size.
826 */
827 seg_size = round_page_64(scp->vmsize);
828 map_size = round_page_64(scp->filesize);
829 map_addr = trunc_page_64(scp->vmaddr); /* JVXXX note that in XNU TOT this is round instead of trunc for 64 bits */
830 if (seg_size == 0)
831 return (KERN_SUCCESS);
832 /* XXX (4596982) this interferes with Rosetta, so limit to 64-bit tasks */
833 if (map_addr == 0 &&
834 map_size == 0 &&
835 seg_size != 0 &&
836 scp->cmd == LC_SEGMENT_64 &&
837 (scp->initprot & VM_PROT_ALL) == VM_PROT_NONE &&
838 (scp->maxprot & VM_PROT_ALL) == VM_PROT_NONE) {
839 /*
840 * This is a "page zero" segment: it starts at address 0,
841 * is not mapped from the binary file and is not accessible.
842 * User-space should never be able to access that memory, so
843 * make it completely off limits by raising the VM map's
844 * minimum offset.
845 */
846 ret = vm_map_raise_min_offset(map, seg_size);
847 if (ret != KERN_SUCCESS) {
848 return (LOAD_FAILURE);
849 }
850 return (LOAD_SUCCESS);
851 }
852
853 map_offset = pager_offset + scp->fileoff; /* limited to 32 bits */
854
855 if (map_size > 0) {
856 initprot = (scp->initprot) & VM_PROT_ALL;
857 maxprot = (scp->maxprot) & VM_PROT_ALL;
858 /*
859 * Map a copy of the file into the address space.
860 */
861 ret = vm_map_enter_mem_object_control(map,
862 &map_addr, map_size, (mach_vm_offset_t)0,
863 VM_FLAGS_FIXED, control, map_offset, TRUE,
864 initprot, maxprot,
865 VM_INHERIT_DEFAULT);
866 if (ret != KERN_SUCCESS)
867 return (LOAD_NOSPACE);
868
869 /*
870 * If the file didn't end on a page boundary,
871 * we need to zero the leftover.
872 */
873 delta_size = map_size - scp->filesize;
874 #if FIXME
875 if (delta_size > 0) {
876 mach_vm_offset_t tmp;
877
878 ret = mach_vm_allocate(kernel_map, &tmp, delta_size, VM_FLAGS_ANYWHERE);
879 if (ret != KERN_SUCCESS)
880 return(LOAD_RESOURCE);
881
882 if (copyout(tmp, map_addr + scp->filesize,
883 delta_size)) {
884 (void) mach_vm_deallocate(
885 kernel_map, tmp, delta_size);
886 return (LOAD_FAILURE);
887 }
888
889 (void) mach_vm_deallocate(kernel_map, tmp, delta_size);
890 }
891 #endif /* FIXME */
892 }
893
894 /*
895 * If the virtual size of the segment is greater
896 * than the size from the file, we need to allocate
897 * zero fill memory for the rest.
898 */
899 delta_size = seg_size - map_size;
900 if (delta_size > 0) {
901 mach_vm_offset_t tmp = map_addr + map_size;
902
903 ret = mach_vm_map(map, &tmp, delta_size, 0, VM_FLAGS_FIXED,
904 NULL, 0, FALSE,
905 scp->initprot, scp->maxprot,
906 VM_INHERIT_DEFAULT);
907 if (ret != KERN_SUCCESS)
908 return(LOAD_NOSPACE);
909 }
910
911 if ( (scp->fileoff == 0) && (scp->filesize != 0) )
912 result->mach_header = map_addr;
913
914 if (scp->flags & SG_PROTECTED_VERSION_1) {
915 ret = unprotect_segment(scp->fileoff,
916 scp->filesize,
917 vp,
918 pager_offset,
919 map,
920 map_addr,
921 map_size);
922 } else {
923 ret = LOAD_SUCCESS;
924 }
925 if (LOAD_SUCCESS == ret && filetype == MH_DYLINKER &&
926 result->all_image_info_addr == MACH_VM_MIN_ADDRESS)
927 note_all_image_info_section(scp,
928 LC_SEGMENT_64 == lcp->cmd, single_section_size,
929 (const char *)lcp + segment_command_size, result);
930
931 return ret;
932 }
933
934 static
935 load_return_t
936 load_thread(
937 struct thread_command *tcp,
938 thread_t thread,
939 load_result_t *result
940 )
941 {
942 kern_return_t kret;
943 load_return_t lret;
944 task_t task;
945 int customstack=0;
946
947 if (tcp->cmdsize < sizeof(*tcp))
948 return (LOAD_BADMACHO);
949 task = get_threadtask(thread);
950
951 /* if count is 0; same as thread */
952 if (result->thread_count != 0) {
953 kret = thread_create(task, &thread);
954 if (kret != KERN_SUCCESS)
955 return(LOAD_RESOURCE);
956 thread_deallocate(thread);
957 }
958
959 lret = load_threadstate(thread,
960 (uint32_t *)(((vm_offset_t)tcp) +
961 sizeof(struct thread_command)),
962 tcp->cmdsize - sizeof(struct thread_command));
963 if (lret != LOAD_SUCCESS)
964 return (lret);
965
966 if (result->thread_count == 0) {
967 lret = load_threadstack(thread,
968 (uint32_t *)(((vm_offset_t)tcp) +
969 sizeof(struct thread_command)),
970 tcp->cmdsize - sizeof(struct thread_command),
971 &result->user_stack,
972 &customstack);
973 if (customstack)
974 result->customstack = 1;
975 else
976 result->customstack = 0;
977
978 if (lret != LOAD_SUCCESS)
979 return(lret);
980
981 lret = load_threadentry(thread,
982 (uint32_t *)(((vm_offset_t)tcp) +
983 sizeof(struct thread_command)),
984 tcp->cmdsize - sizeof(struct thread_command),
985 &result->entry_point);
986 if (lret != LOAD_SUCCESS)
987 return(lret);
988 }
989 /*
990 * Resume thread now, note that this means that the thread
991 * commands should appear after all the load commands to
992 * be sure they don't reference anything not yet mapped.
993 */
994 else
995 thread_resume(thread);
996
997 result->thread_count++;
998
999 return(LOAD_SUCCESS);
1000 }
1001
1002 static
1003 load_return_t
1004 load_unixthread(
1005 struct thread_command *tcp,
1006 thread_t thread,
1007 load_result_t *result
1008 )
1009 {
1010 load_return_t ret;
1011 int customstack =0;
1012
1013 if (tcp->cmdsize < sizeof(*tcp))
1014 return (LOAD_BADMACHO);
1015 if (result->thread_count != 0) {
1016 printf("load_unixthread: already have a thread!");
1017 return (LOAD_FAILURE);
1018 }
1019
1020 ret = load_threadstack(thread,
1021 (uint32_t *)(((vm_offset_t)tcp) +
1022 sizeof(struct thread_command)),
1023 tcp->cmdsize - sizeof(struct thread_command),
1024 &result->user_stack,
1025 &customstack);
1026 if (ret != LOAD_SUCCESS)
1027 return(ret);
1028
1029 if (customstack)
1030 result->customstack = 1;
1031 else
1032 result->customstack = 0;
1033 ret = load_threadentry(thread,
1034 (uint32_t *)(((vm_offset_t)tcp) +
1035 sizeof(struct thread_command)),
1036 tcp->cmdsize - sizeof(struct thread_command),
1037 &result->entry_point);
1038 if (ret != LOAD_SUCCESS)
1039 return(ret);
1040
1041 ret = load_threadstate(thread,
1042 (uint32_t *)(((vm_offset_t)tcp) +
1043 sizeof(struct thread_command)),
1044 tcp->cmdsize - sizeof(struct thread_command));
1045 if (ret != LOAD_SUCCESS)
1046 return (ret);
1047
1048 result->unixproc = TRUE;
1049 result->thread_count++;
1050
1051 return(LOAD_SUCCESS);
1052 }
1053
1054 static
1055 load_return_t
1056 load_threadstate(
1057 thread_t thread,
1058 uint32_t *ts,
1059 uint32_t total_size
1060 )
1061 {
1062 kern_return_t ret;
1063 uint32_t size;
1064 int flavor;
1065 uint32_t thread_size;
1066
1067 ret = thread_state_initialize( thread );
1068 if (ret != KERN_SUCCESS) {
1069 return(LOAD_FAILURE);
1070 }
1071
1072 /*
1073 * Set the new thread state; iterate through the state flavors in
1074 * the mach-o file.
1075 */
1076 while (total_size > 0) {
1077 flavor = *ts++;
1078 size = *ts++;
1079 if (UINT32_MAX-2 < size ||
1080 UINT32_MAX/sizeof(uint32_t) < size+2)
1081 return (LOAD_BADMACHO);
1082 thread_size = (size+2)*sizeof(uint32_t);
1083 if (thread_size > total_size)
1084 return(LOAD_BADMACHO);
1085 total_size -= thread_size;
1086 /*
1087 * Third argument is a kernel space pointer; it gets cast
1088 * to the appropriate type in machine_thread_set_state()
1089 * based on the value of flavor.
1090 */
1091 ret = thread_setstatus(thread, flavor, (thread_state_t)ts, size);
1092 if (ret != KERN_SUCCESS) {
1093 return(LOAD_FAILURE);
1094 }
1095 ts += size; /* ts is a (uint32_t *) */
1096 }
1097 return(LOAD_SUCCESS);
1098 }
1099
1100 static
1101 load_return_t
1102 load_threadstack(
1103 thread_t thread,
1104 uint32_t *ts,
1105 uint32_t total_size,
1106 user_addr_t *user_stack,
1107 int *customstack
1108 )
1109 {
1110 kern_return_t ret;
1111 uint32_t size;
1112 int flavor;
1113 uint32_t stack_size;
1114
1115 while (total_size > 0) {
1116 flavor = *ts++;
1117 size = *ts++;
1118 if (UINT32_MAX-2 < size ||
1119 UINT32_MAX/sizeof(uint32_t) < size+2)
1120 return (LOAD_BADMACHO);
1121 stack_size = (size+2)*sizeof(uint32_t);
1122 if (stack_size > total_size)
1123 return(LOAD_BADMACHO);
1124 total_size -= stack_size;
1125
1126 /*
1127 * Third argument is a kernel space pointer; it gets cast
1128 * to the appropriate type in thread_userstack() based on
1129 * the value of flavor.
1130 */
1131 ret = thread_userstack(thread, flavor, (thread_state_t)ts, size, user_stack, customstack);
1132 if (ret != KERN_SUCCESS) {
1133 return(LOAD_FAILURE);
1134 }
1135 ts += size; /* ts is a (uint32_t *) */
1136 }
1137 return(LOAD_SUCCESS);
1138 }
1139
1140 static
1141 load_return_t
1142 load_threadentry(
1143 thread_t thread,
1144 uint32_t *ts,
1145 uint32_t total_size,
1146 mach_vm_offset_t *entry_point
1147 )
1148 {
1149 kern_return_t ret;
1150 uint32_t size;
1151 int flavor;
1152 uint32_t entry_size;
1153
1154 /*
1155 * Set the thread state.
1156 */
1157 *entry_point = MACH_VM_MIN_ADDRESS;
1158 while (total_size > 0) {
1159 flavor = *ts++;
1160 size = *ts++;
1161 if (UINT32_MAX-2 < size ||
1162 UINT32_MAX/sizeof(uint32_t) < size+2)
1163 return (LOAD_BADMACHO);
1164 entry_size = (size+2)*sizeof(uint32_t);
1165 if (entry_size > total_size)
1166 return(LOAD_BADMACHO);
1167 total_size -= entry_size;
1168 /*
1169 * Third argument is a kernel space pointer; it gets cast
1170 * to the appropriate type in thread_entrypoint() based on
1171 * the value of flavor.
1172 */
1173 ret = thread_entrypoint(thread, flavor, (thread_state_t)ts, size, entry_point);
1174 if (ret != KERN_SUCCESS) {
1175 return(LOAD_FAILURE);
1176 }
1177 ts += size; /* ts is a (uint32_t *) */
1178 }
1179 return(LOAD_SUCCESS);
1180 }
1181
1182
1183 static
1184 load_return_t
1185 load_dylinker(
1186 struct dylinker_command *lcp,
1187 integer_t archbits,
1188 vm_map_t map,
1189 thread_t thread,
1190 int depth,
1191 load_result_t *result,
1192 boolean_t is_64bit
1193 )
1194 {
1195 char *name;
1196 char *p;
1197 struct vnode *vp = NULLVP; /* set by get_macho_vnode() */
1198 struct mach_header header;
1199 off_t file_offset = 0; /* set by get_macho_vnode() */
1200 off_t macho_size = 0; /* set by get_macho_vnode() */
1201 vm_map_t copy_map;
1202 load_result_t myresult;
1203 kern_return_t ret;
1204 vm_map_copy_t tmp;
1205 mach_vm_offset_t dyl_start, map_addr;
1206 mach_vm_size_t dyl_length;
1207
1208 if (lcp->cmdsize < sizeof(*lcp))
1209 return (LOAD_BADMACHO);
1210
1211 name = (char *)lcp + lcp->name.offset;
1212 /*
1213 * Check for a proper null terminated string.
1214 */
1215 p = name;
1216 do {
1217 if (p >= (char *)lcp + lcp->cmdsize)
1218 return(LOAD_BADMACHO);
1219 } while (*p++);
1220
1221 ret = get_macho_vnode(name, archbits, &header, &file_offset, &macho_size, &vp);
1222 if (ret)
1223 return (ret);
1224
1225 myresult = load_result_null;
1226
1227 /*
1228 * First try to map dyld in directly. This should work most of
1229 * the time since there shouldn't normally be something already
1230 * mapped to its address.
1231 */
1232
1233 ret = parse_machfile(vp, map, thread, &header, file_offset, macho_size,
1234 depth, &myresult);
1235
1236 /*
1237 * If it turned out something was in the way, then we'll take
1238 * take this longer path to map dyld into a temporary map and
1239 * copy it into destination map at a different address.
1240 */
1241
1242 if (ret == LOAD_NOSPACE) {
1243
1244 /*
1245 * Load the Mach-O.
1246 * Use a temporary map to do the work.
1247 */
1248 copy_map = vm_map_create(pmap_create(vm_map_round_page(macho_size),
1249 is_64bit),
1250 get_map_min(map), get_map_max(map), TRUE);
1251 if (VM_MAP_NULL == copy_map) {
1252 ret = LOAD_RESOURCE;
1253 goto out;
1254 }
1255
1256 myresult = load_result_null;
1257
1258 ret = parse_machfile(vp, copy_map, thread, &header,
1259 file_offset, macho_size,
1260 depth, &myresult);
1261
1262 if (ret) {
1263 vm_map_deallocate(copy_map);
1264 goto out;
1265 }
1266
1267 if (get_map_nentries(copy_map) > 0) {
1268
1269 dyl_start = mach_get_vm_start(copy_map);
1270 dyl_length = mach_get_vm_end(copy_map) - dyl_start;
1271
1272 map_addr = dyl_start;
1273 ret = mach_vm_allocate(map, &map_addr, dyl_length, VM_FLAGS_ANYWHERE);
1274
1275 if (ret != KERN_SUCCESS) {
1276 vm_map_deallocate(copy_map);
1277 ret = LOAD_NOSPACE;
1278 goto out;
1279
1280 }
1281
1282 ret = vm_map_copyin(copy_map,
1283 (vm_map_address_t)dyl_start,
1284 (vm_map_size_t)dyl_length,
1285 TRUE, &tmp);
1286 if (ret != KERN_SUCCESS) {
1287 (void) vm_map_remove(map,
1288 vm_map_trunc_page(map_addr),
1289 vm_map_round_page(map_addr + dyl_length),
1290 VM_MAP_NO_FLAGS);
1291 vm_map_deallocate(copy_map);
1292 goto out;
1293 }
1294
1295 ret = vm_map_copy_overwrite(map,
1296 (vm_map_address_t)map_addr,
1297 tmp, FALSE);
1298 if (ret != KERN_SUCCESS) {
1299 vm_map_copy_discard(tmp);
1300 (void) vm_map_remove(map,
1301 vm_map_trunc_page(map_addr),
1302 vm_map_round_page(map_addr + dyl_length),
1303 VM_MAP_NO_FLAGS);
1304 vm_map_deallocate(copy_map);
1305 goto out;
1306 }
1307
1308 if (map_addr != dyl_start) {
1309 myresult.entry_point += (map_addr - dyl_start);
1310 myresult.all_image_info_addr +=
1311 (map_addr - dyl_start);
1312 }
1313 } else {
1314 ret = LOAD_FAILURE;
1315 }
1316
1317 vm_map_deallocate(copy_map);
1318 }
1319
1320 if (ret == LOAD_SUCCESS) {
1321 result->dynlinker = TRUE;
1322 result->entry_point = myresult.entry_point;
1323 result->all_image_info_addr = myresult.all_image_info_addr;
1324 result->all_image_info_size = myresult.all_image_info_size;
1325 }
1326 out:
1327 vnode_put(vp);
1328 return (ret);
1329
1330 }
1331
1332 int
1333 load_code_signature(
1334 struct linkedit_data_command *lcp,
1335 struct vnode *vp,
1336 off_t macho_offset,
1337 off_t macho_size,
1338 cpu_type_t cputype,
1339 load_result_t *result)
1340 {
1341 int ret;
1342 kern_return_t kr;
1343 vm_offset_t addr;
1344 int resid;
1345 struct cs_blob *blob;
1346 int error;
1347 vm_size_t blob_size;
1348
1349 addr = 0;
1350 blob = NULL;
1351
1352 if (lcp->cmdsize != sizeof (struct linkedit_data_command) ||
1353 lcp->dataoff + lcp->datasize > macho_size) {
1354 ret = LOAD_BADMACHO;
1355 goto out;
1356 }
1357
1358 blob = ubc_cs_blob_get(vp, cputype, -1);
1359 if (blob != NULL) {
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 */
1365 ret = LOAD_SUCCESS;
1366 } else {
1367 /* the blob has changed for this vnode: fail ! */
1368 ret = LOAD_BADMACHO;
1369 }
1370 goto out;
1371 }
1372
1373 blob_size = lcp->datasize;
1374 kr = ubc_cs_blob_allocate(&addr, &blob_size);
1375 if (kr != KERN_SUCCESS) {
1376 ret = LOAD_NOSPACE;
1377 goto out;
1378 }
1379
1380 resid = 0;
1381 error = vn_rdwr(UIO_READ,
1382 vp,
1383 (caddr_t) addr,
1384 lcp->datasize,
1385 macho_offset + lcp->dataoff,
1386 UIO_SYSSPACE,
1387 0,
1388 kauth_cred_get(),
1389 &resid,
1390 current_proc());
1391 if (error || resid != 0) {
1392 ret = LOAD_IOERROR;
1393 goto out;
1394 }
1395
1396 if (ubc_cs_blob_add(vp,
1397 cputype,
1398 macho_offset,
1399 addr,
1400 lcp->datasize)) {
1401 ret = LOAD_FAILURE;
1402 goto out;
1403 } else {
1404 /* ubc_cs_blob_add() has consumed "addr" */
1405 addr = 0;
1406 }
1407
1408 blob = ubc_cs_blob_get(vp, cputype, -1);
1409
1410 ret = LOAD_SUCCESS;
1411 out:
1412 if (result && ret == LOAD_SUCCESS) {
1413 result->csflags |= blob->csb_flags;
1414 }
1415 if (addr != 0) {
1416 ubc_cs_blob_deallocate(addr, blob_size);
1417 addr = 0;
1418 }
1419
1420 return ret;
1421 }
1422
1423
1424 #if CONFIG_CODE_DECRYPTION
1425
1426 static load_return_t
1427 set_code_unprotect(
1428 struct encryption_info_command *eip,
1429 caddr_t addr,
1430 vm_map_t map,
1431 struct vnode *vp)
1432 {
1433 int result, len;
1434 char vpath[MAXPATHLEN];
1435 pager_crypt_info_t crypt_info;
1436 const char * cryptname = 0;
1437
1438 size_t offset;
1439 struct segment_command_64 *seg64;
1440 struct segment_command *seg32;
1441 vm_map_offset_t map_offset, map_size;
1442 kern_return_t kr;
1443
1444 if (eip->cmdsize < sizeof(*eip))
1445 return LOAD_BADMACHO;
1446
1447 switch(eip->cryptid) {
1448 case 0:
1449 /* not encrypted, just an empty load command */
1450 return LOAD_SUCCESS;
1451 case 1:
1452 cryptname="com.apple.unfree";
1453 break;
1454 case 0x10:
1455 /* some random cryptid that you could manually put into
1456 * your binary if you want NULL */
1457 cryptname="com.apple.null";
1458 break;
1459 default:
1460 return LOAD_BADMACHO;
1461 }
1462
1463 len = MAXPATHLEN;
1464 result = vn_getpath(vp, vpath, &len);
1465 if(result) return result;
1466
1467 /* set up decrypter first */
1468 if(NULL==text_crypter_create) return LOAD_FAILURE;
1469 kr=text_crypter_create(&crypt_info, cryptname, (void*)vpath);
1470
1471 if(kr) {
1472 printf("set_code_unprotect: unable to create decrypter %s, kr=%d\n",
1473 cryptname, kr);
1474 return LOAD_RESOURCE;
1475 }
1476
1477 /* this is terrible, but we have to rescan the load commands to find the
1478 * virtual address of this encrypted stuff. This code is gonna look like
1479 * the dyld source one day... */
1480 struct mach_header *header = (struct mach_header *)addr;
1481 size_t mach_header_sz = sizeof(struct mach_header);
1482 if (header->magic == MH_MAGIC_64 ||
1483 header->magic == MH_CIGAM_64) {
1484 mach_header_sz = sizeof(struct mach_header_64);
1485 }
1486 offset = mach_header_sz;
1487 uint32_t ncmds = header->ncmds;
1488 while (ncmds--) {
1489 /*
1490 * Get a pointer to the command.
1491 */
1492 struct load_command *lcp = (struct load_command *)(addr + offset);
1493 offset += lcp->cmdsize;
1494
1495 switch(lcp->cmd) {
1496 case LC_SEGMENT_64:
1497 seg64 = (struct segment_command_64 *)lcp;
1498 if ((seg64->fileoff <= eip->cryptoff) &&
1499 (seg64->fileoff+seg64->filesize >=
1500 eip->cryptoff+eip->cryptsize)) {
1501 map_offset = seg64->vmaddr + eip->cryptoff - seg64->fileoff;
1502 map_size = eip->cryptsize;
1503 goto remap_now;
1504 }
1505 case LC_SEGMENT:
1506 seg32 = (struct segment_command *)lcp;
1507 if ((seg32->fileoff <= eip->cryptoff) &&
1508 (seg32->fileoff+seg32->filesize >=
1509 eip->cryptoff+eip->cryptsize)) {
1510 map_offset = seg32->vmaddr + eip->cryptoff - seg32->fileoff;
1511 map_size = eip->cryptsize;
1512 goto remap_now;
1513 }
1514 }
1515 }
1516
1517 /* if we get here, did not find anything */
1518 return LOAD_BADMACHO;
1519
1520 remap_now:
1521 /* now remap using the decrypter */
1522 kr = vm_map_apple_protected(map, map_offset, map_offset+map_size, &crypt_info);
1523 if(kr) {
1524 printf("set_code_unprotect(): mapping failed with %x\n", kr);
1525 crypt_info.crypt_end(crypt_info.crypt_ops);
1526 return LOAD_PROTECT;
1527 }
1528
1529 return LOAD_SUCCESS;
1530 }
1531
1532 #endif
1533
1534 /*
1535 * This routine exists to support the load_dylinker().
1536 *
1537 * This routine has its own, separate, understanding of the FAT file format,
1538 * which is terrifically unfortunate.
1539 */
1540 static
1541 load_return_t
1542 get_macho_vnode(
1543 char *path,
1544 integer_t archbits,
1545 struct mach_header *mach_header,
1546 off_t *file_offset,
1547 off_t *macho_size,
1548 struct vnode **vpp
1549 )
1550 {
1551 struct vnode *vp;
1552 vfs_context_t ctx = vfs_context_current();
1553 proc_t p = vfs_context_proc(ctx);
1554 kauth_cred_t kerncred;
1555 struct nameidata nid, *ndp;
1556 boolean_t is_fat;
1557 struct fat_arch fat_arch;
1558 int error = LOAD_SUCCESS;
1559 int resid;
1560 union {
1561 struct mach_header mach_header;
1562 struct fat_header fat_header;
1563 char pad[512];
1564 } header;
1565 off_t fsize = (off_t)0;
1566 int err2;
1567
1568 /*
1569 * Capture the kernel credential for use in the actual read of the
1570 * file, since the user doing the execution may have execute rights
1571 * but not read rights, but to exec something, we have to either map
1572 * or read it into the new process address space, which requires
1573 * read rights. This is to deal with lack of common credential
1574 * serialization code which would treat NOCRED as "serialize 'root'".
1575 */
1576 kerncred = vfs_context_ucred(vfs_context_kernel());
1577
1578 ndp = &nid;
1579
1580 /* init the namei data to point the file user's program name */
1581 NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, CAST_USER_ADDR_T(path), ctx);
1582
1583 if ((error = namei(ndp)) != 0) {
1584 if (error == ENOENT) {
1585 error = LOAD_ENOENT;
1586 } else {
1587 error = LOAD_FAILURE;
1588 }
1589 return(error);
1590 }
1591 nameidone(ndp);
1592 vp = ndp->ni_vp;
1593
1594 /* check for regular file */
1595 if (vp->v_type != VREG) {
1596 error = LOAD_PROTECT;
1597 goto bad1;
1598 }
1599
1600 /* get size */
1601 if ((error = vnode_size(vp, &fsize, ctx)) != 0) {
1602 error = LOAD_FAILURE;
1603 goto bad1;
1604 }
1605
1606 /* Check mount point */
1607 if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
1608 error = LOAD_PROTECT;
1609 goto bad1;
1610 }
1611
1612 /* check access */
1613 if ((error = vnode_authorize(vp, NULL, KAUTH_VNODE_EXECUTE, ctx)) != 0) {
1614 error = LOAD_PROTECT;
1615 goto bad1;
1616 }
1617
1618 /* try to open it */
1619 if ((error = VNOP_OPEN(vp, FREAD, ctx)) != 0) {
1620 error = LOAD_PROTECT;
1621 goto bad1;
1622 }
1623
1624 if ((error = vn_rdwr(UIO_READ, vp, (caddr_t)&header, sizeof(header), 0,
1625 UIO_SYSSPACE, IO_NODELOCKED, kerncred, &resid, p)) != 0) {
1626 error = LOAD_IOERROR;
1627 goto bad2;
1628 }
1629
1630 if (header.mach_header.magic == MH_MAGIC ||
1631 header.mach_header.magic == MH_MAGIC_64)
1632 is_fat = FALSE;
1633 else if (header.fat_header.magic == FAT_MAGIC ||
1634 header.fat_header.magic == FAT_CIGAM)
1635 is_fat = TRUE;
1636 else {
1637 error = LOAD_BADMACHO;
1638 goto bad2;
1639 }
1640
1641 if (is_fat) {
1642 /* Look up our architecture in the fat file. */
1643 error = fatfile_getarch_with_bits(vp, archbits, (vm_offset_t)(&header.fat_header), &fat_arch);
1644 if (error != LOAD_SUCCESS)
1645 goto bad2;
1646
1647 /* Read the Mach-O header out of it */
1648 error = vn_rdwr(UIO_READ, vp, (caddr_t)&header.mach_header,
1649 sizeof(header.mach_header), fat_arch.offset,
1650 UIO_SYSSPACE, IO_NODELOCKED, kerncred, &resid, p);
1651 if (error) {
1652 error = LOAD_IOERROR;
1653 goto bad2;
1654 }
1655
1656 /* Is this really a Mach-O? */
1657 if (header.mach_header.magic != MH_MAGIC &&
1658 header.mach_header.magic != MH_MAGIC_64) {
1659 error = LOAD_BADMACHO;
1660 goto bad2;
1661 }
1662
1663 *file_offset = fat_arch.offset;
1664 *macho_size = fat_arch.size;
1665 } else {
1666 /*
1667 * Force get_macho_vnode() to fail if the architecture bits
1668 * do not match the expected architecture bits. This in
1669 * turn causes load_dylinker() to fail for the same reason,
1670 * so it ensures the dynamic linker and the binary are in
1671 * lock-step. This is potentially bad, if we ever add to
1672 * the CPU_ARCH_* bits any bits that are desirable but not
1673 * required, since the dynamic linker might work, but we will
1674 * refuse to load it because of this check.
1675 */
1676 if ((cpu_type_t)(header.mach_header.cputype & CPU_ARCH_MASK) != archbits)
1677 return(LOAD_BADARCH);
1678
1679 *file_offset = 0;
1680 *macho_size = fsize;
1681 }
1682
1683 *mach_header = header.mach_header;
1684 *vpp = vp;
1685
1686 ubc_setsize(vp, fsize);
1687
1688 return (error);
1689
1690 bad2:
1691 err2 = VNOP_CLOSE(vp, FREAD, ctx);
1692 vnode_put(vp);
1693 return (error);
1694
1695 bad1:
1696 vnode_put(vp);
1697 return(error);
1698 }