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