]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/mach_loader.c
d4f084e31169a178eb71f03042774388db6e6ea1
[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 #include <sys/codesign.h>
53
54 #include <mach/mach_types.h>
55 #include <mach/vm_map.h> /* vm_allocate() */
56 #include <mach/mach_vm.h> /* mach_vm_allocate() */
57 #include <mach/vm_statistics.h>
58 #include <mach/task.h>
59 #include <mach/thread_act.h>
60
61 #include <machine/vmparam.h>
62 #include <machine/exec.h>
63 #include <machine/pal_routines.h>
64
65 #include <kern/kern_types.h>
66 #include <kern/cpu_number.h>
67 #include <kern/mach_loader.h>
68 #include <kern/mach_fat.h>
69 #include <kern/kalloc.h>
70 #include <kern/task.h>
71 #include <kern/thread.h>
72 #include <kern/page_decrypt.h>
73
74 #include <mach-o/fat.h>
75 #include <mach-o/loader.h>
76
77 #include <vm/pmap.h>
78 #include <vm/vm_map.h>
79 #include <vm/vm_kern.h>
80 #include <vm/vm_pager.h>
81 #include <vm/vnode_pager.h>
82 #include <vm/vm_protos.h>
83 #include <IOKit/IOReturn.h> /* for kIOReturnNotPrivileged */
84
85 #include <os/overflow.h>
86
87 /*
88 * XXX vm/pmap.h should not treat these prototypes as MACH_KERNEL_PRIVATE
89 * when KERNEL is defined.
90 */
91 extern pmap_t pmap_create(ledger_t ledger, vm_map_size_t size,
92 boolean_t is_64bit);
93
94 /* XXX should have prototypes in a shared header file */
95 extern int get_map_nentries(vm_map_t);
96
97 extern kern_return_t memory_object_signed(memory_object_control_t control,
98 boolean_t is_signed);
99
100 /* An empty load_result_t */
101 static load_result_t load_result_null = {
102 .mach_header = MACH_VM_MIN_ADDRESS,
103 .entry_point = MACH_VM_MIN_ADDRESS,
104 .user_stack = MACH_VM_MIN_ADDRESS,
105 .user_stack_size = 0,
106 .all_image_info_addr = MACH_VM_MIN_ADDRESS,
107 .all_image_info_size = 0,
108 .thread_count = 0,
109 .unixproc = 0,
110 .dynlinker = 0,
111 .needs_dynlinker = 0,
112 .prog_allocated_stack = 0,
113 .prog_stack_size = 0,
114 .validentry = 0,
115 .using_lcmain = 0,
116 .csflags = 0,
117 .has_pagezero = 0,
118 .uuid = { 0 },
119 .min_vm_addr = MACH_VM_MAX_ADDRESS,
120 .max_vm_addr = MACH_VM_MIN_ADDRESS,
121 .cs_end_offset = 0,
122 .threadstate = NULL,
123 .threadstate_sz = 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 int64_t slide,
139 int64_t dyld_slide,
140 load_result_t *result
141 );
142
143 static load_return_t
144 load_segment(
145 struct load_command *lcp,
146 uint32_t filetype,
147 void *control,
148 off_t pager_offset,
149 off_t macho_size,
150 struct vnode *vp,
151 vm_map_t map,
152 int64_t slide,
153 load_result_t *result
154 );
155
156 static load_return_t
157 load_uuid(
158 struct uuid_command *uulp,
159 char *command_end,
160 load_result_t *result
161 );
162
163 static load_return_t
164 load_code_signature(
165 struct linkedit_data_command *lcp,
166 struct vnode *vp,
167 off_t macho_offset,
168 off_t macho_size,
169 cpu_type_t cputype,
170 load_result_t *result);
171
172 #if CONFIG_CODE_DECRYPTION
173 static load_return_t
174 set_code_unprotect(
175 struct encryption_info_command *lcp,
176 caddr_t addr,
177 vm_map_t map,
178 int64_t slide,
179 struct vnode *vp,
180 off_t macho_offset,
181 cpu_type_t cputype,
182 cpu_subtype_t cpusubtype);
183 #endif
184
185 static
186 load_return_t
187 load_main(
188 struct entry_point_command *epc,
189 thread_t thread,
190 int64_t slide,
191 load_result_t *result
192 );
193
194 static load_return_t
195 load_unixthread(
196 struct thread_command *tcp,
197 thread_t thread,
198 int64_t slide,
199 load_result_t *result
200 );
201
202 static load_return_t
203 load_threadstate(
204 thread_t thread,
205 uint32_t *ts,
206 uint32_t total_size,
207 load_result_t *
208 );
209
210 static load_return_t
211 load_threadstack(
212 thread_t thread,
213 uint32_t *ts,
214 uint32_t total_size,
215 mach_vm_offset_t *user_stack,
216 int *customstack
217 );
218
219 static load_return_t
220 load_threadentry(
221 thread_t thread,
222 uint32_t *ts,
223 uint32_t total_size,
224 mach_vm_offset_t *entry_point
225 );
226
227 static load_return_t
228 load_dylinker(
229 struct dylinker_command *lcp,
230 integer_t archbits,
231 vm_map_t map,
232 thread_t thread,
233 int depth,
234 int64_t slide,
235 load_result_t *result
236 );
237
238 struct macho_data;
239
240 static load_return_t
241 get_macho_vnode(
242 char *path,
243 integer_t archbits,
244 struct mach_header *mach_header,
245 off_t *file_offset,
246 off_t *macho_size,
247 struct macho_data *macho_data,
248 struct vnode **vpp
249 );
250
251 static inline void
252 widen_segment_command(const struct segment_command *scp32,
253 struct segment_command_64 *scp)
254 {
255 scp->cmd = scp32->cmd;
256 scp->cmdsize = scp32->cmdsize;
257 bcopy(scp32->segname, scp->segname, sizeof(scp->segname));
258 scp->vmaddr = scp32->vmaddr;
259 scp->vmsize = scp32->vmsize;
260 scp->fileoff = scp32->fileoff;
261 scp->filesize = scp32->filesize;
262 scp->maxprot = scp32->maxprot;
263 scp->initprot = scp32->initprot;
264 scp->nsects = scp32->nsects;
265 scp->flags = scp32->flags;
266 }
267
268 static void
269 note_all_image_info_section(const struct segment_command_64 *scp,
270 boolean_t is64, size_t section_size, const void *sections,
271 int64_t slide, load_result_t *result)
272 {
273 const union {
274 struct section s32;
275 struct section_64 s64;
276 } *sectionp;
277 unsigned int i;
278
279 if (strncmp(scp->segname, "__DATA", sizeof(scp->segname)) != 0)
280 return;
281 for (i = 0; i < scp->nsects; ++i) {
282 sectionp = (const void *)
283 ((const char *)sections + section_size * i);
284 if (0 == strncmp(sectionp->s64.sectname, "__all_image_info",
285 sizeof(sectionp->s64.sectname))) {
286 result->all_image_info_addr =
287 is64 ? sectionp->s64.addr : sectionp->s32.addr;
288 result->all_image_info_addr += slide;
289 result->all_image_info_size =
290 is64 ? sectionp->s64.size : sectionp->s32.size;
291 return;
292 }
293 }
294 }
295
296
297 load_return_t
298 load_machfile(
299 struct image_params *imgp,
300 struct mach_header *header,
301 thread_t thread,
302 vm_map_t *mapp,
303 load_result_t *result
304 )
305 {
306 struct vnode *vp = imgp->ip_vp;
307 off_t file_offset = imgp->ip_arch_offset;
308 off_t macho_size = imgp->ip_arch_size;
309 off_t file_size = imgp->ip_vattr->va_data_size;
310 vm_map_t new_map = *mapp;
311 pmap_t pmap = 0; /* protected by create_map */
312 vm_map_t map;
313 load_result_t myresult;
314 load_return_t lret;
315 boolean_t create_map = FALSE;
316 boolean_t enforce_hard_pagezero = TRUE;
317 int spawn = (imgp->ip_flags & IMGPF_SPAWN);
318 task_t task = current_task();
319 proc_t p = current_proc();
320 mach_vm_offset_t aslr_offset = 0;
321 mach_vm_offset_t dyld_aslr_offset = 0;
322 kern_return_t kret;
323
324 if (macho_size > file_size) {
325 return(LOAD_BADMACHO);
326 }
327
328 if (new_map == VM_MAP_NULL) {
329 create_map = TRUE;
330 }
331
332 /*
333 * If we are spawning, we have created backing objects for the process
334 * already, which include non-lazily creating the task map. So we
335 * are going to switch out the task map with one appropriate for the
336 * bitness of the image being loaded.
337 */
338 if (spawn) {
339 create_map = TRUE;
340 }
341
342 if (create_map) {
343 task_t ledger_task;
344 if (imgp->ip_new_thread) {
345 ledger_task = get_threadtask(imgp->ip_new_thread);
346 } else {
347 ledger_task = task;
348 }
349 pmap = pmap_create(get_task_ledger(ledger_task),
350 (vm_map_size_t) 0,
351 ((imgp->ip_flags & IMGPF_IS_64BIT) != 0));
352 pal_switch_pmap(thread, pmap, imgp->ip_flags & IMGPF_IS_64BIT);
353 map = vm_map_create(pmap,
354 0,
355 vm_compute_max_offset(((imgp->ip_flags & IMGPF_IS_64BIT) == IMGPF_IS_64BIT)),
356 TRUE);
357 } else
358 map = new_map;
359
360 #if (__ARM_ARCH_7K__ >= 2) && defined(PLATFORM_WatchOS)
361 /* enforce 16KB alignment for watch targets with new ABI */
362 vm_map_set_page_shift(map, SIXTEENK_PAGE_SHIFT);
363 #endif /* __arm64__ */
364
365 #ifndef CONFIG_ENFORCE_SIGNED_CODE
366 /* This turns off faulting for executable pages, which allows
367 * to circumvent Code Signing Enforcement. The per process
368 * flag (CS_ENFORCEMENT) is not set yet, but we can use the
369 * global flag.
370 */
371 if ( !cs_enforcement(NULL) && (header->flags & MH_ALLOW_STACK_EXECUTION) )
372 vm_map_disable_NX(map);
373 #endif
374
375 /* Forcibly disallow execution from data pages on even if the arch
376 * normally permits it. */
377 if ((header->flags & MH_NO_HEAP_EXECUTION) && !(imgp->ip_flags & IMGPF_ALLOW_DATA_EXEC))
378 vm_map_disallow_data_exec(map);
379
380 /*
381 * Compute a random offset for ASLR, and an independent random offset for dyld.
382 */
383 if (!(imgp->ip_flags & IMGPF_DISABLE_ASLR)) {
384 uint64_t max_slide_pages;
385
386 max_slide_pages = vm_map_get_max_aslr_slide_pages(map);
387
388 aslr_offset = random();
389 aslr_offset %= max_slide_pages;
390 aslr_offset <<= vm_map_page_shift(map);
391
392 dyld_aslr_offset = random();
393 dyld_aslr_offset %= max_slide_pages;
394 dyld_aslr_offset <<= vm_map_page_shift(map);
395 }
396
397 if (!result)
398 result = &myresult;
399
400 *result = load_result_null;
401
402 lret = parse_machfile(vp, map, thread, header, file_offset, macho_size,
403 0, (int64_t)aslr_offset, (int64_t)dyld_aslr_offset, result);
404
405 if (lret != LOAD_SUCCESS) {
406 if (create_map) {
407 vm_map_deallocate(map); /* will lose pmap reference too */
408 }
409 return(lret);
410 }
411
412 #if __x86_64__
413 /*
414 * On x86, for compatibility, don't enforce the hard page-zero restriction for 32-bit binaries.
415 */
416 if ((imgp->ip_flags & IMGPF_IS_64BIT) == 0) {
417 enforce_hard_pagezero = FALSE;
418 }
419 #endif
420 /*
421 * Check to see if the page zero is enforced by the map->min_offset.
422 */
423 if (enforce_hard_pagezero &&
424 (vm_map_has_hard_pagezero(map, 0x1000) == FALSE)) {
425 {
426 if (create_map) {
427 vm_map_deallocate(map); /* will lose pmap reference too */
428 }
429 return (LOAD_BADMACHO);
430 }
431 }
432
433 if (create_map) {
434 /*
435 * If this is an exec, then we are going to destroy the old
436 * task, and it's correct to halt it; if it's spawn, the
437 * task is not yet running, and it makes no sense.
438 */
439 if (!spawn) {
440 /*
441 * Mark the task as halting and start the other
442 * threads towards terminating themselves. Then
443 * make sure any threads waiting for a process
444 * transition get informed that we are committed to
445 * this transition, and then finally complete the
446 * task halting (wait for threads and then cleanup
447 * task resources).
448 *
449 * NOTE: task_start_halt() makes sure that no new
450 * threads are created in the task during the transition.
451 * We need to mark the workqueue as exiting before we
452 * wait for threads to terminate (at the end of which
453 * we no longer have a prohibition on thread creation).
454 *
455 * Finally, clean up any lingering workqueue data structures
456 * that may have been left behind by the workqueue threads
457 * as they exited (and then clean up the work queue itself).
458 */
459 kret = task_start_halt(task);
460 if (kret != KERN_SUCCESS) {
461 vm_map_deallocate(map); /* will lose pmap reference too */
462 return (LOAD_FAILURE);
463 }
464 proc_transcommit(p, 0);
465 workqueue_mark_exiting(p);
466 task_complete_halt(task);
467 workqueue_exit(p);
468 kqueue_dealloc(p->p_wqkqueue);
469 p->p_wqkqueue = NULL;
470 }
471 *mapp = map;
472 }
473 return(LOAD_SUCCESS);
474 }
475
476 int macho_printf = 0;
477 #define MACHO_PRINTF(args) \
478 do { \
479 if (macho_printf) { \
480 printf args; \
481 } \
482 } while (0)
483
484 /*
485 * The file size of a mach-o file is limited to 32 bits; this is because
486 * this is the limit on the kalloc() of enough bytes for a mach_header and
487 * the contents of its sizeofcmds, which is currently constrained to 32
488 * bits in the file format itself. We read into the kernel buffer the
489 * commands section, and then parse it in order to parse the mach-o file
490 * format load_command segment(s). We are only interested in a subset of
491 * the total set of possible commands. If "map"==VM_MAP_NULL or
492 * "thread"==THREAD_NULL, do not make permament VM modifications,
493 * just preflight the parse.
494 */
495 static
496 load_return_t
497 parse_machfile(
498 struct vnode *vp,
499 vm_map_t map,
500 thread_t thread,
501 struct mach_header *header,
502 off_t file_offset,
503 off_t macho_size,
504 int depth,
505 int64_t aslr_offset,
506 int64_t dyld_aslr_offset,
507 load_result_t *result
508 )
509 {
510 uint32_t ncmds;
511 struct load_command *lcp;
512 struct dylinker_command *dlp = 0;
513 integer_t dlarchbits = 0;
514 void * control;
515 load_return_t ret = LOAD_SUCCESS;
516 caddr_t addr;
517 void * kl_addr;
518 vm_size_t size,kl_size;
519 size_t offset;
520 size_t oldoffset; /* for overflow check */
521 int pass;
522 proc_t p = current_proc(); /* XXXX */
523 int error;
524 int resid = 0;
525 size_t mach_header_sz = sizeof(struct mach_header);
526 boolean_t abi64;
527 boolean_t got_code_signatures = FALSE;
528 int64_t slide = 0;
529
530 if (header->magic == MH_MAGIC_64 ||
531 header->magic == MH_CIGAM_64) {
532 mach_header_sz = sizeof(struct mach_header_64);
533 }
534
535 /*
536 * Break infinite recursion
537 */
538 if (depth > 1) {
539 return(LOAD_FAILURE);
540 }
541
542 depth++;
543
544 /*
545 * Check to see if right machine type.
546 */
547 if (((cpu_type_t)(header->cputype & ~CPU_ARCH_MASK) != (cpu_type() & ~CPU_ARCH_MASK)) ||
548 !grade_binary(header->cputype,
549 header->cpusubtype & ~CPU_SUBTYPE_MASK))
550 return(LOAD_BADARCH);
551
552 abi64 = ((header->cputype & CPU_ARCH_ABI64) == CPU_ARCH_ABI64);
553
554 switch (header->filetype) {
555
556 case MH_EXECUTE:
557 if (depth != 1) {
558 return (LOAD_FAILURE);
559 }
560
561 break;
562 case MH_DYLINKER:
563 if (depth != 2) {
564 return (LOAD_FAILURE);
565 }
566 break;
567
568 default:
569 return (LOAD_FAILURE);
570 }
571
572 /*
573 * Get the pager for the file.
574 */
575 control = ubc_getobject(vp, UBC_FLAGS_NONE);
576
577 /*
578 * Map portion that must be accessible directly into
579 * kernel's map.
580 */
581 if ((off_t)(mach_header_sz + header->sizeofcmds) > macho_size)
582 return(LOAD_BADMACHO);
583
584 /*
585 * Round size of Mach-O commands up to page boundry.
586 */
587 size = round_page(mach_header_sz + header->sizeofcmds);
588 if (size <= 0)
589 return(LOAD_BADMACHO);
590
591 /*
592 * Map the load commands into kernel memory.
593 */
594 addr = 0;
595 kl_size = size;
596 kl_addr = kalloc(size);
597 addr = (caddr_t)kl_addr;
598 if (addr == NULL)
599 return(LOAD_NOSPACE);
600
601 error = vn_rdwr(UIO_READ, vp, addr, size, file_offset,
602 UIO_SYSSPACE, 0, kauth_cred_get(), &resid, p);
603 if (error) {
604 if (kl_addr)
605 kfree(kl_addr, kl_size);
606 return(LOAD_IOERROR);
607 }
608
609 if (resid) {
610 /* We must be able to read in as much as the mach_header indicated */
611 if (kl_addr)
612 kfree(kl_addr, kl_size);
613 return(LOAD_BADMACHO);
614 }
615
616 /*
617 * For PIE and dyld, slide everything by the ASLR offset.
618 */
619 if ((header->flags & MH_PIE) || (header->filetype == MH_DYLINKER)) {
620 slide = aslr_offset;
621 }
622
623 /*
624 * Scan through the commands, processing each one as necessary.
625 * We parse in three passes through the headers:
626 * 0: determine if TEXT and DATA boundary can be page-aligned
627 * 1: thread state, uuid, code signature
628 * 2: segments
629 * 3: dyld, encryption, check entry point
630 */
631
632 for (pass = 0; pass <= 3; pass++) {
633
634 if (pass == 0) {
635 /* see if we need to adjust the slide to re-align... */
636 /* no re-alignment needed on X86_64 or ARM32 kernel */
637 continue;
638 } else if (pass == 1) {
639 }
640
641 /*
642 * Check that the entry point is contained in an executable segments
643 */
644 if ((pass == 3) && (!result->using_lcmain && result->validentry == 0)) {
645 thread_state_initialize(thread);
646 ret = LOAD_FAILURE;
647 break;
648 }
649
650 /*
651 * Loop through each of the load_commands indicated by the
652 * Mach-O header; if an absurd value is provided, we just
653 * run off the end of the reserved section by incrementing
654 * the offset too far, so we are implicitly fail-safe.
655 */
656 offset = mach_header_sz;
657 ncmds = header->ncmds;
658
659 while (ncmds--) {
660 /*
661 * Get a pointer to the command.
662 */
663 lcp = (struct load_command *)(addr + offset);
664 oldoffset = offset;
665 offset += lcp->cmdsize;
666
667 /*
668 * Perform prevalidation of the struct load_command
669 * before we attempt to use its contents. Invalid
670 * values are ones which result in an overflow, or
671 * which can not possibly be valid commands, or which
672 * straddle or exist past the reserved section at the
673 * start of the image.
674 */
675 if (oldoffset > offset ||
676 lcp->cmdsize < sizeof(struct load_command) ||
677 offset > header->sizeofcmds + mach_header_sz) {
678 ret = LOAD_BADMACHO;
679 break;
680 }
681
682 /*
683 * Act on struct load_command's for which kernel
684 * intervention is required.
685 */
686 switch(lcp->cmd) {
687 case LC_SEGMENT:
688 if (pass == 0) {
689 break;
690 }
691
692 if (pass != 2)
693 break;
694
695 if (abi64) {
696 /*
697 * Having an LC_SEGMENT command for the
698 * wrong ABI is invalid <rdar://problem/11021230>
699 */
700 ret = LOAD_BADMACHO;
701 break;
702 }
703
704 ret = load_segment(lcp,
705 header->filetype,
706 control,
707 file_offset,
708 macho_size,
709 vp,
710 map,
711 slide,
712 result);
713 break;
714 case LC_SEGMENT_64:
715 if (pass != 2)
716 break;
717
718 if (!abi64) {
719 /*
720 * Having an LC_SEGMENT_64 command for the
721 * wrong ABI is invalid <rdar://problem/11021230>
722 */
723 ret = LOAD_BADMACHO;
724 break;
725 }
726
727 ret = load_segment(lcp,
728 header->filetype,
729 control,
730 file_offset,
731 macho_size,
732 vp,
733 map,
734 slide,
735 result);
736 break;
737 case LC_UNIXTHREAD:
738 if (pass != 1)
739 break;
740 ret = load_unixthread(
741 (struct thread_command *) lcp,
742 thread,
743 slide,
744 result);
745 break;
746 case LC_MAIN:
747 if (pass != 1)
748 break;
749 if (depth != 1)
750 break;
751 ret = load_main(
752 (struct entry_point_command *) lcp,
753 thread,
754 slide,
755 result);
756 break;
757 case LC_LOAD_DYLINKER:
758 if (pass != 3)
759 break;
760 if ((depth == 1) && (dlp == 0)) {
761 dlp = (struct dylinker_command *)lcp;
762 dlarchbits = (header->cputype & CPU_ARCH_MASK);
763 } else {
764 ret = LOAD_FAILURE;
765 }
766 break;
767 case LC_UUID:
768 if (pass == 1 && depth == 1) {
769 ret = load_uuid((struct uuid_command *) lcp,
770 (char *)addr + mach_header_sz + header->sizeofcmds,
771 result);
772 }
773 break;
774 case LC_CODE_SIGNATURE:
775 /* CODE SIGNING */
776 if (pass != 1)
777 break;
778 /* pager -> uip ->
779 load signatures & store in uip
780 set VM object "signed_pages"
781 */
782 ret = load_code_signature(
783 (struct linkedit_data_command *) lcp,
784 vp,
785 file_offset,
786 macho_size,
787 header->cputype,
788 result);
789 if (ret != LOAD_SUCCESS) {
790 printf("proc %d: load code signature error %d "
791 "for file \"%s\"\n",
792 p->p_pid, ret, vp->v_name);
793 /*
794 * Allow injections to be ignored on devices w/o enforcement enabled
795 */
796 if (!cs_enforcement(NULL))
797 ret = LOAD_SUCCESS; /* ignore error */
798
799 } else {
800 got_code_signatures = TRUE;
801 }
802
803 if (got_code_signatures) {
804 unsigned tainted = CS_VALIDATE_TAINTED;
805 boolean_t valid = FALSE;
806 struct cs_blob *blobs;
807 vm_size_t off = 0;
808
809
810 if (cs_debug > 10)
811 printf("validating initial pages of %s\n", vp->v_name);
812 blobs = ubc_get_cs_blobs(vp);
813
814 while (off < size && ret == LOAD_SUCCESS) {
815 tainted = CS_VALIDATE_TAINTED;
816
817 valid = cs_validate_page(blobs,
818 NULL,
819 file_offset + off,
820 addr + off,
821 &tainted);
822 if (!valid || (tainted & CS_VALIDATE_TAINTED)) {
823 if (cs_debug)
824 printf("CODE SIGNING: %s[%d]: invalid initial page at offset %lld validated:%d tainted:%d csflags:0x%x\n",
825 vp->v_name, p->p_pid, (long long)(file_offset + off), valid, tainted, result->csflags);
826 if (cs_enforcement(NULL) ||
827 (result->csflags & (CS_HARD|CS_KILL|CS_ENFORCEMENT))) {
828 ret = LOAD_FAILURE;
829 }
830 result->csflags &= ~CS_VALID;
831 }
832 off += PAGE_SIZE;
833 }
834 }
835
836 break;
837 #if CONFIG_CODE_DECRYPTION
838 case LC_ENCRYPTION_INFO:
839 case LC_ENCRYPTION_INFO_64:
840 if (pass != 3)
841 break;
842 ret = set_code_unprotect(
843 (struct encryption_info_command *) lcp,
844 addr, map, slide, vp, file_offset,
845 header->cputype, header->cpusubtype);
846 if (ret != LOAD_SUCCESS) {
847 printf("proc %d: set_code_unprotect() error %d "
848 "for file \"%s\"\n",
849 p->p_pid, ret, vp->v_name);
850 /*
851 * Don't let the app run if it's
852 * encrypted but we failed to set up the
853 * decrypter. If the keys are missing it will
854 * return LOAD_DECRYPTFAIL.
855 */
856 if (ret == LOAD_DECRYPTFAIL) {
857 /* failed to load due to missing FP keys */
858 proc_lock(p);
859 p->p_lflag |= P_LTERM_DECRYPTFAIL;
860 proc_unlock(p);
861 }
862 psignal(p, SIGKILL);
863 }
864 break;
865 #endif
866 default:
867 /* Other commands are ignored by the kernel */
868 ret = LOAD_SUCCESS;
869 break;
870 }
871 if (ret != LOAD_SUCCESS)
872 break;
873 }
874 if (ret != LOAD_SUCCESS)
875 break;
876 }
877
878 if (ret == LOAD_SUCCESS) {
879 if (! got_code_signatures) {
880 if (cs_enforcement(NULL)) {
881 ret = LOAD_FAILURE;
882 } else {
883 /*
884 * No embedded signatures: look for detached by taskgated,
885 * this is only done on OSX, on embedded platforms we expect everything
886 * to be have embedded signatures.
887 */
888 struct cs_blob *blob;
889
890 blob = ubc_cs_blob_get(vp, -1, file_offset);
891 if (blob != NULL) {
892 unsigned int cs_flag_data = blob->csb_flags;
893 if(0 != ubc_cs_generation_check(vp)) {
894 if (0 != ubc_cs_blob_revalidate(vp, blob, 0)) {
895 /* clear out the flag data if revalidation fails */
896 cs_flag_data = 0;
897 result->csflags &= ~CS_VALID;
898 }
899 }
900 /* get flags to be applied to the process */
901 result->csflags |= cs_flag_data;
902 }
903 }
904 }
905
906 /* Make sure if we need dyld, we got it */
907 if (result->needs_dynlinker && !dlp) {
908 ret = LOAD_FAILURE;
909 }
910
911 if ((ret == LOAD_SUCCESS) && (dlp != 0)) {
912 /*
913 * load the dylinker, and slide it by the independent DYLD ASLR
914 * offset regardless of the PIE-ness of the main binary.
915 */
916 ret = load_dylinker(dlp, dlarchbits, map, thread, depth,
917 dyld_aslr_offset, result);
918 }
919
920 if((ret == LOAD_SUCCESS) && (depth == 1)) {
921 if (result->thread_count == 0) {
922 ret = LOAD_FAILURE;
923 }
924 }
925 }
926
927 if (kl_addr )
928 kfree(kl_addr, kl_size);
929
930 return(ret);
931 }
932
933 #if CONFIG_CODE_DECRYPTION
934
935 #define APPLE_UNPROTECTED_HEADER_SIZE (3 * 4096)
936
937 static load_return_t
938 unprotect_dsmos_segment(
939 uint64_t file_off,
940 uint64_t file_size,
941 struct vnode *vp,
942 off_t macho_offset,
943 vm_map_t map,
944 vm_map_offset_t map_addr,
945 vm_map_size_t map_size)
946 {
947 kern_return_t kr;
948
949 /*
950 * The first APPLE_UNPROTECTED_HEADER_SIZE bytes (from offset 0 of
951 * this part of a Universal binary) are not protected...
952 * The rest needs to be "transformed".
953 */
954 if (file_off <= APPLE_UNPROTECTED_HEADER_SIZE &&
955 file_off + file_size <= APPLE_UNPROTECTED_HEADER_SIZE) {
956 /* it's all unprotected, nothing to do... */
957 kr = KERN_SUCCESS;
958 } else {
959 if (file_off <= APPLE_UNPROTECTED_HEADER_SIZE) {
960 /*
961 * We start mapping in the unprotected area.
962 * Skip the unprotected part...
963 */
964 vm_map_offset_t delta;
965
966 delta = APPLE_UNPROTECTED_HEADER_SIZE;
967 delta -= file_off;
968 map_addr += delta;
969 map_size -= delta;
970 }
971 /* ... transform the rest of the mapping. */
972 struct pager_crypt_info crypt_info;
973 crypt_info.page_decrypt = dsmos_page_transform;
974 crypt_info.crypt_ops = NULL;
975 crypt_info.crypt_end = NULL;
976 #pragma unused(vp, macho_offset)
977 crypt_info.crypt_ops = (void *)0x2e69cf40;
978 vm_map_offset_t crypto_backing_offset;
979 crypto_backing_offset = -1; /* i.e. use map entry's offset */
980 #if DEVELOPMENT || DEBUG
981 struct proc *p;
982 p = current_proc();
983 printf("APPLE_PROTECT: %d[%s] map %p [0x%llx:0x%llx] %s(%s)\n",
984 p->p_pid, p->p_comm, map,
985 (uint64_t) map_addr, (uint64_t) (map_addr + map_size),
986 __FUNCTION__, vp->v_name);
987 #endif /* DEVELOPMENT || DEBUG */
988
989 /* The DSMOS pager can only be used by apple signed code */
990 struct cs_blob * blob = csvnode_get_blob(vp, file_off);
991 if( blob == NULL || !blob->csb_platform_binary || blob->csb_platform_path)
992 {
993 return LOAD_FAILURE;
994 }
995
996 kr = vm_map_apple_protected(map,
997 map_addr,
998 map_addr + map_size,
999 crypto_backing_offset,
1000 &crypt_info);
1001 }
1002
1003 if (kr != KERN_SUCCESS) {
1004 return LOAD_FAILURE;
1005 }
1006 return LOAD_SUCCESS;
1007 }
1008 #else /* CONFIG_CODE_DECRYPTION */
1009 static load_return_t
1010 unprotect_dsmos_segment(
1011 __unused uint64_t file_off,
1012 __unused uint64_t file_size,
1013 __unused struct vnode *vp,
1014 __unused off_t macho_offset,
1015 __unused vm_map_t map,
1016 __unused vm_map_offset_t map_addr,
1017 __unused vm_map_size_t map_size)
1018 {
1019 return LOAD_SUCCESS;
1020 }
1021 #endif /* CONFIG_CODE_DECRYPTION */
1022
1023
1024 /*
1025 * map_segment:
1026 * Maps a Mach-O segment, taking care of mis-alignment (wrt the system
1027 * page size) issues.
1028 *
1029 * The mapping might result in 1, 2 or 3 map entries:
1030 * 1. for the first page, which could be overlap with the previous
1031 * mapping,
1032 * 2. for the center (if applicable),
1033 * 3. for the last page, which could overlap with the next mapping.
1034 *
1035 * For each of those map entries, we might have to interpose a
1036 * "fourk_pager" to deal with mis-alignment wrt the system page size,
1037 * either in the mapping address and/or size or the file offset and/or
1038 * size.
1039 * The "fourk_pager" itself would be mapped with proper alignment
1040 * wrt the system page size and would then be populated with the
1041 * information about the intended mapping, with a "4KB" granularity.
1042 */
1043 static kern_return_t
1044 map_segment(
1045 vm_map_t map,
1046 vm_map_offset_t vm_start,
1047 vm_map_offset_t vm_end,
1048 memory_object_control_t control,
1049 vm_map_offset_t file_start,
1050 vm_map_offset_t file_end,
1051 vm_prot_t initprot,
1052 vm_prot_t maxprot)
1053 {
1054 int extra_vm_flags, cur_extra_vm_flags;
1055 vm_map_offset_t cur_offset, cur_start, cur_end;
1056 kern_return_t ret;
1057 vm_map_offset_t effective_page_mask;
1058
1059 if (vm_end < vm_start ||
1060 file_end < file_start) {
1061 return LOAD_BADMACHO;
1062 }
1063 if (vm_end == vm_start ||
1064 file_end == file_start) {
1065 /* nothing to map... */
1066 return LOAD_SUCCESS;
1067 }
1068
1069 effective_page_mask = MAX(PAGE_MASK, vm_map_page_mask(map));
1070
1071 extra_vm_flags = 0;
1072 if (vm_map_page_aligned(vm_start, effective_page_mask) &&
1073 vm_map_page_aligned(vm_end, effective_page_mask) &&
1074 vm_map_page_aligned(file_start, effective_page_mask) &&
1075 vm_map_page_aligned(file_end, effective_page_mask)) {
1076 /* all page-aligned and map-aligned: proceed */
1077 } else {
1078 panic("map_segment: unexpected mis-alignment "
1079 "vm[0x%llx:0x%llx] file[0x%llx:0x%llx]\n",
1080 (uint64_t) vm_start,
1081 (uint64_t) vm_end,
1082 (uint64_t) file_start,
1083 (uint64_t) file_end);
1084 }
1085
1086 cur_offset = 0;
1087 cur_start = vm_start;
1088 cur_end = vm_start;
1089 if (cur_end >= vm_start + (file_end - file_start)) {
1090 /* all mapped: done */
1091 goto done;
1092 }
1093 if (vm_map_round_page(cur_end, effective_page_mask) >=
1094 vm_map_trunc_page(vm_start + (file_end - file_start),
1095 effective_page_mask)) {
1096 /* no middle */
1097 } else {
1098 cur_start = cur_end;
1099 if ((vm_start & effective_page_mask) !=
1100 (file_start & effective_page_mask)) {
1101 /* one 4K pager for the middle */
1102 cur_extra_vm_flags = extra_vm_flags;
1103 } else {
1104 /* regular mapping for the middle */
1105 cur_extra_vm_flags = 0;
1106 }
1107 cur_end = vm_map_trunc_page(vm_start + (file_end -
1108 file_start),
1109 effective_page_mask);
1110 if (control != MEMORY_OBJECT_CONTROL_NULL) {
1111 ret = vm_map_enter_mem_object_control(
1112 map,
1113 &cur_start,
1114 cur_end - cur_start,
1115 (mach_vm_offset_t)0,
1116 VM_FLAGS_FIXED | cur_extra_vm_flags,
1117 control,
1118 file_start + cur_offset,
1119 TRUE, /* copy */
1120 initprot, maxprot,
1121 VM_INHERIT_DEFAULT);
1122 } else {
1123 ret = vm_map_enter_mem_object(
1124 map,
1125 &cur_start,
1126 cur_end - cur_start,
1127 (mach_vm_offset_t)0,
1128 VM_FLAGS_FIXED | cur_extra_vm_flags,
1129 IPC_PORT_NULL,
1130 0, /* offset */
1131 TRUE, /* copy */
1132 initprot, maxprot,
1133 VM_INHERIT_DEFAULT);
1134 }
1135 if (ret != KERN_SUCCESS) {
1136 return (LOAD_NOSPACE);
1137 }
1138 cur_offset += cur_end - cur_start;
1139 }
1140 if (cur_end >= vm_start + (file_end - file_start)) {
1141 /* all mapped: done */
1142 goto done;
1143 }
1144 cur_start = cur_end;
1145 done:
1146 assert(cur_end >= vm_start + (file_end - file_start));
1147 return LOAD_SUCCESS;
1148 }
1149
1150 static
1151 load_return_t
1152 load_segment(
1153 struct load_command *lcp,
1154 uint32_t filetype,
1155 void * control,
1156 off_t pager_offset,
1157 off_t macho_size,
1158 struct vnode *vp,
1159 vm_map_t map,
1160 int64_t slide,
1161 load_result_t *result)
1162 {
1163 struct segment_command_64 segment_command, *scp;
1164 kern_return_t ret;
1165 vm_map_size_t delta_size;
1166 vm_prot_t initprot;
1167 vm_prot_t maxprot;
1168 size_t segment_command_size, total_section_size,
1169 single_section_size;
1170 vm_map_offset_t file_offset, file_size;
1171 vm_map_offset_t vm_offset, vm_size;
1172 vm_map_offset_t vm_start, vm_end, vm_end_aligned;
1173 vm_map_offset_t file_start, file_end;
1174 kern_return_t kr;
1175 boolean_t verbose;
1176 vm_map_size_t effective_page_size;
1177 vm_map_offset_t effective_page_mask;
1178
1179 effective_page_size = MAX(PAGE_SIZE, vm_map_page_size(map));
1180 effective_page_mask = MAX(PAGE_MASK, vm_map_page_mask(map));
1181
1182 verbose = FALSE;
1183 if (LC_SEGMENT_64 == lcp->cmd) {
1184 segment_command_size = sizeof(struct segment_command_64);
1185 single_section_size = sizeof(struct section_64);
1186 } else {
1187 segment_command_size = sizeof(struct segment_command);
1188 single_section_size = sizeof(struct section);
1189 }
1190 if (lcp->cmdsize < segment_command_size)
1191 return (LOAD_BADMACHO);
1192 total_section_size = lcp->cmdsize - segment_command_size;
1193
1194 if (LC_SEGMENT_64 == lcp->cmd) {
1195 scp = (struct segment_command_64 *)lcp;
1196 } else {
1197 scp = &segment_command;
1198 widen_segment_command((struct segment_command *)lcp, scp);
1199 }
1200
1201 if (verbose) {
1202 MACHO_PRINTF(("+++ load_segment %s "
1203 "vm[0x%llx:0x%llx] file[0x%llx:0x%llx] "
1204 "prot %d/%d flags 0x%x\n",
1205 scp->segname,
1206 (uint64_t)(slide + scp->vmaddr),
1207 (uint64_t)(slide + scp->vmaddr + scp->vmsize),
1208 pager_offset + scp->fileoff,
1209 pager_offset + scp->fileoff + scp->filesize,
1210 scp->initprot,
1211 scp->maxprot,
1212 scp->flags));
1213 }
1214
1215 /*
1216 * Make sure what we get from the file is really ours (as specified
1217 * by macho_size).
1218 */
1219 if (scp->fileoff + scp->filesize < scp->fileoff ||
1220 scp->fileoff + scp->filesize > (uint64_t)macho_size) {
1221 return (LOAD_BADMACHO);
1222 }
1223 /*
1224 * Ensure that the number of sections specified would fit
1225 * within the load command size.
1226 */
1227 if (total_section_size / single_section_size < scp->nsects) {
1228 return (LOAD_BADMACHO);
1229 }
1230 /*
1231 * Make sure the segment is page-aligned in the file.
1232 */
1233 file_offset = pager_offset + scp->fileoff; /* limited to 32 bits */
1234 file_size = scp->filesize;
1235 if ((file_offset & PAGE_MASK_64) != 0 ||
1236 /* we can't mmap() it if it's not page-aligned in the file */
1237 (file_offset & vm_map_page_mask(map)) != 0) {
1238 /*
1239 * The 1st test would have failed if the system's page size
1240 * was what this process believe is the page size, so let's
1241 * fail here too for the sake of consistency.
1242 */
1243 return (LOAD_BADMACHO);
1244 }
1245
1246 /*
1247 * If we have a code signature attached for this slice
1248 * require that the segments are within the signed part
1249 * of the file.
1250 */
1251 if (result->cs_end_offset &&
1252 result->cs_end_offset < (off_t)scp->fileoff &&
1253 result->cs_end_offset - scp->fileoff < scp->filesize)
1254 {
1255 if (cs_debug)
1256 printf("section outside code signature\n");
1257 return LOAD_BADMACHO;
1258 }
1259
1260 vm_offset = scp->vmaddr + slide;
1261 vm_size = scp->vmsize;
1262
1263 if (vm_size == 0)
1264 return (LOAD_SUCCESS);
1265 if (scp->vmaddr == 0 &&
1266 file_size == 0 &&
1267 vm_size != 0 &&
1268 (scp->initprot & VM_PROT_ALL) == VM_PROT_NONE &&
1269 (scp->maxprot & VM_PROT_ALL) == VM_PROT_NONE) {
1270 /*
1271 * For PIE, extend page zero rather than moving it. Extending
1272 * page zero keeps early allocations from falling predictably
1273 * between the end of page zero and the beginning of the first
1274 * slid segment.
1275 */
1276 /*
1277 * This is a "page zero" segment: it starts at address 0,
1278 * is not mapped from the binary file and is not accessible.
1279 * User-space should never be able to access that memory, so
1280 * make it completely off limits by raising the VM map's
1281 * minimum offset.
1282 */
1283 vm_end = vm_offset + vm_size;
1284 if (vm_end < vm_offset) {
1285 return (LOAD_BADMACHO);
1286 }
1287 if (verbose) {
1288 MACHO_PRINTF(("++++++ load_segment: "
1289 "page_zero up to 0x%llx\n",
1290 (uint64_t) vm_end));
1291 }
1292 {
1293 vm_end = vm_map_round_page(vm_end,
1294 PAGE_MASK_64);
1295 vm_end_aligned = vm_end;
1296 }
1297 ret = vm_map_raise_min_offset(map,
1298 vm_end_aligned);
1299
1300 if (ret != KERN_SUCCESS) {
1301 return (LOAD_FAILURE);
1302 }
1303 return (LOAD_SUCCESS);
1304 } else {
1305 }
1306
1307 {
1308 file_start = vm_map_trunc_page(file_offset,
1309 effective_page_mask);
1310 file_end = vm_map_round_page(file_offset + file_size,
1311 effective_page_mask);
1312 vm_start = vm_map_trunc_page(vm_offset,
1313 effective_page_mask);
1314 vm_end = vm_map_round_page(vm_offset + vm_size,
1315 effective_page_mask);
1316 }
1317
1318 if (vm_start < result->min_vm_addr)
1319 result->min_vm_addr = vm_start;
1320 if (vm_end > result->max_vm_addr)
1321 result->max_vm_addr = vm_end;
1322
1323 if (map == VM_MAP_NULL)
1324 return (LOAD_SUCCESS);
1325
1326 if (vm_size > 0) {
1327 initprot = (scp->initprot) & VM_PROT_ALL;
1328 maxprot = (scp->maxprot) & VM_PROT_ALL;
1329 /*
1330 * Map a copy of the file into the address space.
1331 */
1332 if (verbose) {
1333 MACHO_PRINTF(("++++++ load_segment: "
1334 "mapping at vm [0x%llx:0x%llx] of "
1335 "file [0x%llx:0x%llx]\n",
1336 (uint64_t) vm_start,
1337 (uint64_t) vm_end,
1338 (uint64_t) file_start,
1339 (uint64_t) file_end));
1340 }
1341 ret = map_segment(map,
1342 vm_start,
1343 vm_end,
1344 control,
1345 file_start,
1346 file_end,
1347 initprot,
1348 maxprot);
1349 if (ret) {
1350 return LOAD_NOSPACE;
1351 }
1352
1353 #if FIXME
1354 /*
1355 * If the file didn't end on a page boundary,
1356 * we need to zero the leftover.
1357 */
1358 delta_size = map_size - scp->filesize;
1359 if (delta_size > 0) {
1360 mach_vm_offset_t tmp;
1361
1362 ret = mach_vm_allocate(kernel_map, &tmp, delta_size, VM_FLAGS_ANYWHERE| VM_MAKE_TAG(VM_KERN_MEMORY_BSD));
1363 if (ret != KERN_SUCCESS) {
1364 return(LOAD_RESOURCE);
1365 }
1366
1367 if (copyout(tmp, map_addr + scp->filesize,
1368 delta_size)) {
1369 (void) mach_vm_deallocate(
1370 kernel_map, tmp, delta_size);
1371 return (LOAD_FAILURE);
1372 }
1373
1374 (void) mach_vm_deallocate(kernel_map, tmp, delta_size);
1375 }
1376 #endif /* FIXME */
1377 }
1378
1379 /*
1380 * If the virtual size of the segment is greater
1381 * than the size from the file, we need to allocate
1382 * zero fill memory for the rest.
1383 */
1384 if ((vm_end - vm_start) > (file_end - file_start)) {
1385 delta_size = (vm_end - vm_start) - (file_end - file_start);
1386 } else {
1387 delta_size = 0;
1388 }
1389 if (delta_size > 0) {
1390 mach_vm_offset_t tmp;
1391
1392 tmp = vm_start + (file_end - file_start);
1393 if (verbose) {
1394 MACHO_PRINTF(("++++++ load_segment: "
1395 "delta mapping vm [0x%llx:0x%llx]\n",
1396 (uint64_t) tmp,
1397 (uint64_t) (tmp + delta_size)));
1398 }
1399 kr = map_segment(map,
1400 tmp,
1401 tmp + delta_size,
1402 MEMORY_OBJECT_CONTROL_NULL,
1403 0,
1404 delta_size,
1405 scp->initprot,
1406 scp->maxprot);
1407 if (kr != KERN_SUCCESS) {
1408 return(LOAD_NOSPACE);
1409 }
1410 }
1411
1412 if ( (scp->fileoff == 0) && (scp->filesize != 0) )
1413 result->mach_header = vm_offset;
1414
1415 if (scp->flags & SG_PROTECTED_VERSION_1) {
1416 ret = unprotect_dsmos_segment(file_start,
1417 file_end - file_start,
1418 vp,
1419 pager_offset,
1420 map,
1421 vm_start,
1422 vm_end - vm_start);
1423 if (ret != LOAD_SUCCESS) {
1424 return ret;
1425 }
1426 } else {
1427 ret = LOAD_SUCCESS;
1428 }
1429
1430 if (LOAD_SUCCESS == ret &&
1431 filetype == MH_DYLINKER &&
1432 result->all_image_info_addr == MACH_VM_MIN_ADDRESS) {
1433 note_all_image_info_section(scp,
1434 LC_SEGMENT_64 == lcp->cmd,
1435 single_section_size,
1436 ((const char *)lcp +
1437 segment_command_size),
1438 slide,
1439 result);
1440 }
1441
1442 if (result->entry_point != MACH_VM_MIN_ADDRESS) {
1443 if ((result->entry_point >= vm_offset) && (result->entry_point < (vm_offset + vm_size))) {
1444 if ((scp->initprot & (VM_PROT_READ|VM_PROT_EXECUTE)) == (VM_PROT_READ|VM_PROT_EXECUTE)) {
1445 result->validentry = 1;
1446 } else {
1447 /* right range but wrong protections, unset if previously validated */
1448 result->validentry = 0;
1449 }
1450 }
1451 }
1452
1453 return ret;
1454 }
1455
1456 static
1457 load_return_t
1458 load_uuid(
1459 struct uuid_command *uulp,
1460 char *command_end,
1461 load_result_t *result
1462 )
1463 {
1464 /*
1465 * We need to check the following for this command:
1466 * - The command size should be atleast the size of struct uuid_command
1467 * - The UUID part of the command should be completely within the mach-o header
1468 */
1469
1470 if ((uulp->cmdsize < sizeof(struct uuid_command)) ||
1471 (((char *)uulp + sizeof(struct uuid_command)) > command_end)) {
1472 return (LOAD_BADMACHO);
1473 }
1474
1475 memcpy(&result->uuid[0], &uulp->uuid[0], sizeof(result->uuid));
1476 return (LOAD_SUCCESS);
1477 }
1478
1479 static
1480 load_return_t
1481 load_main(
1482 struct entry_point_command *epc,
1483 thread_t thread,
1484 int64_t slide,
1485 load_result_t *result
1486 )
1487 {
1488 mach_vm_offset_t addr;
1489 kern_return_t ret;
1490
1491 if (epc->cmdsize < sizeof(*epc))
1492 return (LOAD_BADMACHO);
1493 if (result->thread_count != 0) {
1494 return (LOAD_FAILURE);
1495 }
1496
1497 if (thread == THREAD_NULL)
1498 return (LOAD_SUCCESS);
1499
1500 /* LC_MAIN specifies stack size but not location */
1501 if (epc->stacksize) {
1502 result->prog_stack_size = 1;
1503 result->user_stack_size = epc->stacksize;
1504 } else {
1505 result->prog_stack_size = 0;
1506 result->user_stack_size = MAXSSIZ;
1507 }
1508 result->prog_allocated_stack = 0;
1509
1510 /* use default location for stack */
1511 ret = thread_userstackdefault(thread, &addr);
1512 if (ret != KERN_SUCCESS)
1513 return(LOAD_FAILURE);
1514
1515 /* The stack slides down from the default location */
1516 result->user_stack = addr;
1517 result->user_stack -= slide;
1518
1519 if (result->using_lcmain || result->entry_point != MACH_VM_MIN_ADDRESS) {
1520 /* Already processed LC_MAIN or LC_UNIXTHREAD */
1521 return (LOAD_FAILURE);
1522 }
1523
1524 /* kernel does *not* use entryoff from LC_MAIN. Dyld uses it. */
1525 result->needs_dynlinker = TRUE;
1526 result->using_lcmain = TRUE;
1527 result->unixproc = TRUE;
1528 result->thread_count++;
1529
1530 return(LOAD_SUCCESS);
1531 }
1532
1533
1534 static
1535 load_return_t
1536 load_unixthread(
1537 struct thread_command *tcp,
1538 thread_t thread,
1539 int64_t slide,
1540 load_result_t *result
1541 )
1542 {
1543 load_return_t ret;
1544 int customstack =0;
1545 mach_vm_offset_t addr;
1546
1547 if (tcp->cmdsize < sizeof(*tcp))
1548 return (LOAD_BADMACHO);
1549 if (result->thread_count != 0) {
1550 return (LOAD_FAILURE);
1551 }
1552
1553 if (thread == THREAD_NULL)
1554 return (LOAD_SUCCESS);
1555
1556 ret = load_threadstack(thread,
1557 (uint32_t *)(((vm_offset_t)tcp) +
1558 sizeof(struct thread_command)),
1559 tcp->cmdsize - sizeof(struct thread_command),
1560 &addr,
1561 &customstack);
1562 if (ret != LOAD_SUCCESS)
1563 return(ret);
1564
1565 /* LC_UNIXTHREAD optionally specifies stack size and location */
1566
1567 if (customstack) {
1568 result->prog_stack_size = 0; /* unknown */
1569 result->prog_allocated_stack = 1;
1570 } else {
1571 result->prog_allocated_stack = 0;
1572 result->prog_stack_size = 0;
1573 result->user_stack_size = MAXSSIZ;
1574 }
1575
1576 /* The stack slides down from the default location */
1577 result->user_stack = addr;
1578 result->user_stack -= slide;
1579
1580 ret = load_threadentry(thread,
1581 (uint32_t *)(((vm_offset_t)tcp) +
1582 sizeof(struct thread_command)),
1583 tcp->cmdsize - sizeof(struct thread_command),
1584 &addr);
1585 if (ret != LOAD_SUCCESS)
1586 return(ret);
1587
1588 if (result->using_lcmain || result->entry_point != MACH_VM_MIN_ADDRESS) {
1589 /* Already processed LC_MAIN or LC_UNIXTHREAD */
1590 return (LOAD_FAILURE);
1591 }
1592
1593 result->entry_point = addr;
1594 result->entry_point += slide;
1595
1596 ret = load_threadstate(thread,
1597 (uint32_t *)(((vm_offset_t)tcp) + sizeof(struct thread_command)),
1598 tcp->cmdsize - sizeof(struct thread_command),
1599 result);
1600 if (ret != LOAD_SUCCESS)
1601 return (ret);
1602
1603 result->unixproc = TRUE;
1604 result->thread_count++;
1605
1606 return(LOAD_SUCCESS);
1607 }
1608
1609 static
1610 load_return_t
1611 load_threadstate(
1612 thread_t thread,
1613 uint32_t *ts,
1614 uint32_t total_size,
1615 load_result_t *result
1616 )
1617 {
1618 uint32_t size;
1619 int flavor;
1620 uint32_t thread_size;
1621 uint32_t *local_ts = NULL;
1622 uint32_t local_ts_size = 0;
1623 int ret;
1624
1625 (void)thread;
1626
1627 if (total_size > 0) {
1628 local_ts_size = total_size;
1629 local_ts = kalloc(local_ts_size);
1630 if (local_ts == NULL) {
1631 return LOAD_FAILURE;
1632 }
1633 memcpy(local_ts, ts, local_ts_size);
1634 ts = local_ts;
1635 }
1636
1637 /*
1638 * Validate the new thread state; iterate through the state flavors in
1639 * the Mach-O file.
1640 * XXX: we should validate the machine state here, to avoid failing at
1641 * activation time where we can't bail out cleanly.
1642 */
1643 while (total_size > 0) {
1644 flavor = *ts++;
1645 size = *ts++;
1646
1647 if (os_add_overflow(size, UINT32_C(2), &thread_size) ||
1648 os_mul_overflow(thread_size, (uint32_t)sizeof(uint32_t), &thread_size) ||
1649 os_sub_overflow(total_size, thread_size, &total_size)) {
1650 ret = LOAD_BADMACHO;
1651 goto bad;
1652 }
1653
1654 ts += size; /* ts is a (uint32_t *) */
1655 }
1656
1657 result->threadstate = local_ts;
1658 result->threadstate_sz = local_ts_size;
1659 return LOAD_SUCCESS;
1660
1661 bad:
1662 if (local_ts) {
1663 kfree(local_ts, local_ts_size);
1664 }
1665 return ret;
1666 }
1667
1668 static
1669 load_return_t
1670 load_threadstack(
1671 thread_t thread,
1672 uint32_t *ts,
1673 uint32_t total_size,
1674 mach_vm_offset_t *user_stack,
1675 int *customstack
1676 )
1677 {
1678 kern_return_t ret;
1679 uint32_t size;
1680 int flavor;
1681 uint32_t stack_size;
1682
1683 while (total_size > 0) {
1684 flavor = *ts++;
1685 size = *ts++;
1686 if (UINT32_MAX-2 < size ||
1687 UINT32_MAX/sizeof(uint32_t) < size+2)
1688 return (LOAD_BADMACHO);
1689 stack_size = (size+2)*sizeof(uint32_t);
1690 if (stack_size > total_size)
1691 return(LOAD_BADMACHO);
1692 total_size -= stack_size;
1693
1694 /*
1695 * Third argument is a kernel space pointer; it gets cast
1696 * to the appropriate type in thread_userstack() based on
1697 * the value of flavor.
1698 */
1699 ret = thread_userstack(thread, flavor, (thread_state_t)ts, size, user_stack, customstack);
1700 if (ret != KERN_SUCCESS) {
1701 return(LOAD_FAILURE);
1702 }
1703 ts += size; /* ts is a (uint32_t *) */
1704 }
1705 return(LOAD_SUCCESS);
1706 }
1707
1708 static
1709 load_return_t
1710 load_threadentry(
1711 thread_t thread,
1712 uint32_t *ts,
1713 uint32_t total_size,
1714 mach_vm_offset_t *entry_point
1715 )
1716 {
1717 kern_return_t ret;
1718 uint32_t size;
1719 int flavor;
1720 uint32_t entry_size;
1721
1722 /*
1723 * Set the thread state.
1724 */
1725 *entry_point = MACH_VM_MIN_ADDRESS;
1726 while (total_size > 0) {
1727 flavor = *ts++;
1728 size = *ts++;
1729 if (UINT32_MAX-2 < size ||
1730 UINT32_MAX/sizeof(uint32_t) < size+2)
1731 return (LOAD_BADMACHO);
1732 entry_size = (size+2)*sizeof(uint32_t);
1733 if (entry_size > total_size)
1734 return(LOAD_BADMACHO);
1735 total_size -= entry_size;
1736 /*
1737 * Third argument is a kernel space pointer; it gets cast
1738 * to the appropriate type in thread_entrypoint() based on
1739 * the value of flavor.
1740 */
1741 ret = thread_entrypoint(thread, flavor, (thread_state_t)ts, size, entry_point);
1742 if (ret != KERN_SUCCESS) {
1743 return(LOAD_FAILURE);
1744 }
1745 ts += size; /* ts is a (uint32_t *) */
1746 }
1747 return(LOAD_SUCCESS);
1748 }
1749
1750 struct macho_data {
1751 struct nameidata __nid;
1752 union macho_vnode_header {
1753 struct mach_header mach_header;
1754 struct fat_header fat_header;
1755 char __pad[512];
1756 } __header;
1757 };
1758
1759 #define DEFAULT_DYLD_PATH "/usr/lib/dyld"
1760
1761 static load_return_t
1762 load_dylinker(
1763 struct dylinker_command *lcp,
1764 integer_t archbits,
1765 vm_map_t map,
1766 thread_t thread,
1767 int depth,
1768 int64_t slide,
1769 load_result_t *result
1770 )
1771 {
1772 char *name;
1773 char *p;
1774 struct vnode *vp = NULLVP; /* set by get_macho_vnode() */
1775 struct mach_header *header;
1776 off_t file_offset = 0; /* set by get_macho_vnode() */
1777 off_t macho_size = 0; /* set by get_macho_vnode() */
1778 load_result_t *myresult;
1779 kern_return_t ret;
1780 struct macho_data *macho_data;
1781 struct {
1782 struct mach_header __header;
1783 load_result_t __myresult;
1784 struct macho_data __macho_data;
1785 } *dyld_data;
1786
1787 if (lcp->cmdsize < sizeof(*lcp))
1788 return (LOAD_BADMACHO);
1789
1790 name = (char *)lcp + lcp->name.offset;
1791 /*
1792 * Check for a proper null terminated string.
1793 */
1794 p = name;
1795 do {
1796 if (p >= (char *)lcp + lcp->cmdsize)
1797 return(LOAD_BADMACHO);
1798 } while (*p++);
1799
1800 #if !(DEVELOPMENT || DEBUG)
1801 if (0 != strcmp(name, DEFAULT_DYLD_PATH)) {
1802 return (LOAD_BADMACHO);
1803 }
1804 #endif
1805
1806 /* Allocate wad-of-data from heap to reduce excessively deep stacks */
1807
1808 MALLOC(dyld_data, void *, sizeof (*dyld_data), M_TEMP, M_WAITOK);
1809 header = &dyld_data->__header;
1810 myresult = &dyld_data->__myresult;
1811 macho_data = &dyld_data->__macho_data;
1812
1813 ret = get_macho_vnode(name, archbits, header,
1814 &file_offset, &macho_size, macho_data, &vp);
1815 if (ret)
1816 goto novp_out;
1817
1818 *myresult = load_result_null;
1819
1820 /*
1821 * First try to map dyld in directly. This should work most of
1822 * the time since there shouldn't normally be something already
1823 * mapped to its address.
1824 */
1825
1826 ret = parse_machfile(vp, map, thread, header, file_offset,
1827 macho_size, depth, slide, 0, myresult);
1828
1829 /*
1830 * If it turned out something was in the way, then we'll take
1831 * take this longer path to preflight dyld's vm ranges, then
1832 * map it at a free location in the address space.
1833 */
1834
1835 if (ret == LOAD_NOSPACE) {
1836 mach_vm_offset_t dyl_start, map_addr;
1837 mach_vm_size_t dyl_length;
1838 int64_t slide_amount;
1839
1840 *myresult = load_result_null;
1841
1842 /*
1843 * Preflight parsing the Mach-O file with a NULL
1844 * map, which will return the ranges needed for a
1845 * subsequent map attempt (with a slide) in "myresult"
1846 */
1847 ret = parse_machfile(vp, VM_MAP_NULL, THREAD_NULL, header,
1848 file_offset, macho_size, depth,
1849 0 /* slide */, 0, myresult);
1850
1851 if (ret != LOAD_SUCCESS) {
1852 goto out;
1853 }
1854
1855 dyl_start = myresult->min_vm_addr;
1856 dyl_length = myresult->max_vm_addr - myresult->min_vm_addr;
1857
1858 dyl_length += slide;
1859
1860 /* To find an appropriate load address, do a quick allocation */
1861 map_addr = dyl_start;
1862 ret = mach_vm_allocate(map, &map_addr, dyl_length, VM_FLAGS_ANYWHERE);
1863 if (ret != KERN_SUCCESS) {
1864 ret = LOAD_NOSPACE;
1865 goto out;
1866 }
1867
1868 ret = mach_vm_deallocate(map, map_addr, dyl_length);
1869 if (ret != KERN_SUCCESS) {
1870 ret = LOAD_NOSPACE;
1871 goto out;
1872 }
1873
1874 if (map_addr < dyl_start)
1875 slide_amount = -(int64_t)(dyl_start - map_addr);
1876 else
1877 slide_amount = (int64_t)(map_addr - dyl_start);
1878
1879 slide_amount += slide;
1880
1881 *myresult = load_result_null;
1882
1883 ret = parse_machfile(vp, map, thread, header,
1884 file_offset, macho_size, depth,
1885 slide_amount, 0, myresult);
1886
1887 if (ret) {
1888 goto out;
1889 }
1890 }
1891
1892 if (ret == LOAD_SUCCESS) {
1893 if (result->threadstate) {
1894 /* don't use the app's threadstate if we have a dyld */
1895 kfree(result->threadstate, result->threadstate_sz);
1896 }
1897 result->threadstate = myresult->threadstate;
1898 result->threadstate_sz = myresult->threadstate_sz;
1899
1900 result->dynlinker = TRUE;
1901 result->entry_point = myresult->entry_point;
1902 result->validentry = myresult->validentry;
1903 result->all_image_info_addr = myresult->all_image_info_addr;
1904 result->all_image_info_size = myresult->all_image_info_size;
1905 if (myresult->platform_binary) {
1906 result->csflags |= CS_DYLD_PLATFORM;
1907 }
1908 }
1909 out:
1910 vnode_put(vp);
1911 novp_out:
1912 FREE(dyld_data, M_TEMP);
1913 return (ret);
1914
1915 }
1916
1917 static load_return_t
1918 load_code_signature(
1919 struct linkedit_data_command *lcp,
1920 struct vnode *vp,
1921 off_t macho_offset,
1922 off_t macho_size,
1923 cpu_type_t cputype,
1924 load_result_t *result)
1925 {
1926 int ret;
1927 kern_return_t kr;
1928 vm_offset_t addr;
1929 int resid;
1930 struct cs_blob *blob;
1931 int error;
1932 vm_size_t blob_size;
1933
1934 addr = 0;
1935 blob = NULL;
1936
1937 if (lcp->cmdsize != sizeof (struct linkedit_data_command) ||
1938 lcp->dataoff + lcp->datasize > macho_size) {
1939 ret = LOAD_BADMACHO;
1940 goto out;
1941 }
1942
1943 blob = ubc_cs_blob_get(vp, cputype, macho_offset);
1944 if (blob != NULL) {
1945 /* we already have a blob for this vnode and cputype */
1946 if (blob->csb_cpu_type == cputype &&
1947 blob->csb_base_offset == macho_offset &&
1948 blob->csb_mem_size == lcp->datasize) {
1949 /* it matches the blob we want here, lets verify the version */
1950 if(0 != ubc_cs_generation_check(vp)) {
1951 if (0 != ubc_cs_blob_revalidate(vp, blob, 0)) {
1952 ret = LOAD_FAILURE; /* set error same as from ubc_cs_blob_add */
1953 goto out;
1954 }
1955 }
1956 ret = LOAD_SUCCESS;
1957 } else {
1958 /* the blob has changed for this vnode: fail ! */
1959 ret = LOAD_BADMACHO;
1960 }
1961 goto out;
1962 }
1963
1964 blob_size = lcp->datasize;
1965 kr = ubc_cs_blob_allocate(&addr, &blob_size);
1966 if (kr != KERN_SUCCESS) {
1967 ret = LOAD_NOSPACE;
1968 goto out;
1969 }
1970
1971 resid = 0;
1972 error = vn_rdwr(UIO_READ,
1973 vp,
1974 (caddr_t) addr,
1975 lcp->datasize,
1976 macho_offset + lcp->dataoff,
1977 UIO_SYSSPACE,
1978 0,
1979 kauth_cred_get(),
1980 &resid,
1981 current_proc());
1982 if (error || resid != 0) {
1983 ret = LOAD_IOERROR;
1984 goto out;
1985 }
1986
1987 if (ubc_cs_blob_add(vp,
1988 cputype,
1989 macho_offset,
1990 addr,
1991 lcp->datasize,
1992 0,
1993 &blob)) {
1994 ret = LOAD_FAILURE;
1995 goto out;
1996 } else {
1997 /* ubc_cs_blob_add() has consumed "addr" */
1998 addr = 0;
1999 }
2000
2001 #if CHECK_CS_VALIDATION_BITMAP
2002 ubc_cs_validation_bitmap_allocate( vp );
2003 #endif
2004
2005 ret = LOAD_SUCCESS;
2006 out:
2007 if (ret == LOAD_SUCCESS) {
2008 if (blob == NULL)
2009 panic("sucess, but no blob!");
2010
2011 result->csflags |= blob->csb_flags;
2012 result->platform_binary = blob->csb_platform_binary;
2013 result->cs_end_offset = blob->csb_end_offset;
2014 }
2015 if (addr != 0) {
2016 ubc_cs_blob_deallocate(addr, blob_size);
2017 addr = 0;
2018 }
2019
2020 return ret;
2021 }
2022
2023
2024 #if CONFIG_CODE_DECRYPTION
2025
2026 static load_return_t
2027 set_code_unprotect(
2028 struct encryption_info_command *eip,
2029 caddr_t addr,
2030 vm_map_t map,
2031 int64_t slide,
2032 struct vnode *vp,
2033 off_t macho_offset,
2034 cpu_type_t cputype,
2035 cpu_subtype_t cpusubtype)
2036 {
2037 int error, len;
2038 pager_crypt_info_t crypt_info;
2039 const char * cryptname = 0;
2040 char *vpath;
2041
2042 size_t offset;
2043 struct segment_command_64 *seg64;
2044 struct segment_command *seg32;
2045 vm_map_offset_t map_offset, map_size;
2046 vm_object_offset_t crypto_backing_offset;
2047 kern_return_t kr;
2048
2049 if (eip->cmdsize < sizeof(*eip)) return LOAD_BADMACHO;
2050
2051 switch(eip->cryptid) {
2052 case 0:
2053 /* not encrypted, just an empty load command */
2054 return LOAD_SUCCESS;
2055 case 1:
2056 cryptname="com.apple.unfree";
2057 break;
2058 case 0x10:
2059 /* some random cryptid that you could manually put into
2060 * your binary if you want NULL */
2061 cryptname="com.apple.null";
2062 break;
2063 default:
2064 return LOAD_BADMACHO;
2065 }
2066
2067 if (map == VM_MAP_NULL) return (LOAD_SUCCESS);
2068 if (NULL == text_crypter_create) return LOAD_FAILURE;
2069
2070 MALLOC_ZONE(vpath, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
2071 if(vpath == NULL) return LOAD_FAILURE;
2072
2073 len = MAXPATHLEN;
2074 error = vn_getpath(vp, vpath, &len);
2075 if (error) {
2076 FREE_ZONE(vpath, MAXPATHLEN, M_NAMEI);
2077 return LOAD_FAILURE;
2078 }
2079
2080 /* set up decrypter first */
2081 crypt_file_data_t crypt_data = {
2082 .filename = vpath,
2083 .cputype = cputype,
2084 .cpusubtype = cpusubtype};
2085 kr=text_crypter_create(&crypt_info, cryptname, (void*)&crypt_data);
2086 #if DEVELOPMENT || DEBUG
2087 struct proc *p;
2088 p = current_proc();
2089 printf("APPLE_PROTECT: %d[%s] map %p %s(%s) -> 0x%x\n",
2090 p->p_pid, p->p_comm, map, __FUNCTION__, vpath, kr);
2091 #endif /* DEVELOPMENT || DEBUG */
2092 FREE_ZONE(vpath, MAXPATHLEN, M_NAMEI);
2093
2094 if(kr) {
2095 printf("set_code_unprotect: unable to create decrypter %s, kr=%d\n",
2096 cryptname, kr);
2097 if (kr == kIOReturnNotPrivileged) {
2098 /* text encryption returned decryption failure */
2099 return(LOAD_DECRYPTFAIL);
2100 }else
2101 return LOAD_RESOURCE;
2102 }
2103
2104 /* this is terrible, but we have to rescan the load commands to find the
2105 * virtual address of this encrypted stuff. This code is gonna look like
2106 * the dyld source one day... */
2107 struct mach_header *header = (struct mach_header *)addr;
2108 size_t mach_header_sz = sizeof(struct mach_header);
2109 if (header->magic == MH_MAGIC_64 ||
2110 header->magic == MH_CIGAM_64) {
2111 mach_header_sz = sizeof(struct mach_header_64);
2112 }
2113 offset = mach_header_sz;
2114 uint32_t ncmds = header->ncmds;
2115 while (ncmds--) {
2116 /*
2117 * Get a pointer to the command.
2118 */
2119 struct load_command *lcp = (struct load_command *)(addr + offset);
2120 offset += lcp->cmdsize;
2121
2122 switch(lcp->cmd) {
2123 case LC_SEGMENT_64:
2124 seg64 = (struct segment_command_64 *)lcp;
2125 if ((seg64->fileoff <= eip->cryptoff) &&
2126 (seg64->fileoff+seg64->filesize >=
2127 eip->cryptoff+eip->cryptsize)) {
2128 map_offset = seg64->vmaddr + eip->cryptoff - seg64->fileoff + slide;
2129 map_size = eip->cryptsize;
2130 crypto_backing_offset = macho_offset + eip->cryptoff;
2131 goto remap_now;
2132 }
2133 case LC_SEGMENT:
2134 seg32 = (struct segment_command *)lcp;
2135 if ((seg32->fileoff <= eip->cryptoff) &&
2136 (seg32->fileoff+seg32->filesize >=
2137 eip->cryptoff+eip->cryptsize)) {
2138 map_offset = seg32->vmaddr + eip->cryptoff - seg32->fileoff + slide;
2139 map_size = eip->cryptsize;
2140 crypto_backing_offset = macho_offset + eip->cryptoff;
2141 goto remap_now;
2142 }
2143 }
2144 }
2145
2146 /* if we get here, did not find anything */
2147 return LOAD_BADMACHO;
2148
2149 remap_now:
2150 /* now remap using the decrypter */
2151 MACHO_PRINTF(("+++ set_code_unprotect: vm[0x%llx:0x%llx]\n",
2152 (uint64_t) map_offset,
2153 (uint64_t) (map_offset+map_size)));
2154 kr = vm_map_apple_protected(map,
2155 map_offset,
2156 map_offset+map_size,
2157 crypto_backing_offset,
2158 &crypt_info);
2159 if (kr) {
2160 printf("set_code_unprotect(): mapping failed with %x\n", kr);
2161 return LOAD_PROTECT;
2162 }
2163
2164 return LOAD_SUCCESS;
2165 }
2166
2167 #endif
2168
2169 /*
2170 * This routine exists to support the load_dylinker().
2171 *
2172 * This routine has its own, separate, understanding of the FAT file format,
2173 * which is terrifically unfortunate.
2174 */
2175 static
2176 load_return_t
2177 get_macho_vnode(
2178 char *path,
2179 integer_t archbits,
2180 struct mach_header *mach_header,
2181 off_t *file_offset,
2182 off_t *macho_size,
2183 struct macho_data *data,
2184 struct vnode **vpp
2185 )
2186 {
2187 struct vnode *vp;
2188 vfs_context_t ctx = vfs_context_current();
2189 proc_t p = vfs_context_proc(ctx);
2190 kauth_cred_t kerncred;
2191 struct nameidata *ndp = &data->__nid;
2192 boolean_t is_fat;
2193 struct fat_arch fat_arch;
2194 int error;
2195 int resid;
2196 union macho_vnode_header *header = &data->__header;
2197 off_t fsize = (off_t)0;
2198
2199 /*
2200 * Capture the kernel credential for use in the actual read of the
2201 * file, since the user doing the execution may have execute rights
2202 * but not read rights, but to exec something, we have to either map
2203 * or read it into the new process address space, which requires
2204 * read rights. This is to deal with lack of common credential
2205 * serialization code which would treat NOCRED as "serialize 'root'".
2206 */
2207 kerncred = vfs_context_ucred(vfs_context_kernel());
2208
2209 /* init the namei data to point the file user's program name */
2210 NDINIT(ndp, LOOKUP, OP_OPEN, FOLLOW | LOCKLEAF, UIO_SYSSPACE, CAST_USER_ADDR_T(path), ctx);
2211
2212 if ((error = namei(ndp)) != 0) {
2213 if (error == ENOENT) {
2214 error = LOAD_ENOENT;
2215 } else {
2216 error = LOAD_FAILURE;
2217 }
2218 return(error);
2219 }
2220 nameidone(ndp);
2221 vp = ndp->ni_vp;
2222
2223 /* check for regular file */
2224 if (vp->v_type != VREG) {
2225 error = LOAD_PROTECT;
2226 goto bad1;
2227 }
2228
2229 /* get size */
2230 if ((error = vnode_size(vp, &fsize, ctx)) != 0) {
2231 error = LOAD_FAILURE;
2232 goto bad1;
2233 }
2234
2235 /* Check mount point */
2236 if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
2237 error = LOAD_PROTECT;
2238 goto bad1;
2239 }
2240
2241 /* check access */
2242 if ((error = vnode_authorize(vp, NULL, KAUTH_VNODE_EXECUTE | KAUTH_VNODE_READ_DATA, ctx)) != 0) {
2243 error = LOAD_PROTECT;
2244 goto bad1;
2245 }
2246
2247 /* try to open it */
2248 if ((error = VNOP_OPEN(vp, FREAD, ctx)) != 0) {
2249 error = LOAD_PROTECT;
2250 goto bad1;
2251 }
2252
2253 if ((error = vn_rdwr(UIO_READ, vp, (caddr_t)header, sizeof (*header), 0,
2254 UIO_SYSSPACE, IO_NODELOCKED, kerncred, &resid, p)) != 0) {
2255 error = LOAD_IOERROR;
2256 goto bad2;
2257 }
2258
2259 if (resid) {
2260 error = LOAD_BADMACHO;
2261 goto bad2;
2262 }
2263
2264 if (header->mach_header.magic == MH_MAGIC ||
2265 header->mach_header.magic == MH_MAGIC_64) {
2266 is_fat = FALSE;
2267 } else if (OSSwapBigToHostInt32(header->fat_header.magic) == FAT_MAGIC) {
2268 is_fat = TRUE;
2269 } else {
2270 error = LOAD_BADMACHO;
2271 goto bad2;
2272 }
2273
2274 if (is_fat) {
2275
2276 error = fatfile_validate_fatarches((vm_offset_t)(&header->fat_header),
2277 sizeof(*header));
2278 if (error != LOAD_SUCCESS) {
2279 goto bad2;
2280 }
2281
2282 /* Look up our architecture in the fat file. */
2283 error = fatfile_getarch_with_bits(archbits,
2284 (vm_offset_t)(&header->fat_header), sizeof(*header), &fat_arch);
2285 if (error != LOAD_SUCCESS)
2286 goto bad2;
2287
2288 /* Read the Mach-O header out of it */
2289 error = vn_rdwr(UIO_READ, vp, (caddr_t)&header->mach_header,
2290 sizeof (header->mach_header), fat_arch.offset,
2291 UIO_SYSSPACE, IO_NODELOCKED, kerncred, &resid, p);
2292 if (error) {
2293 error = LOAD_IOERROR;
2294 goto bad2;
2295 }
2296
2297 if (resid) {
2298 error = LOAD_BADMACHO;
2299 goto bad2;
2300 }
2301
2302 /* Is this really a Mach-O? */
2303 if (header->mach_header.magic != MH_MAGIC &&
2304 header->mach_header.magic != MH_MAGIC_64) {
2305 error = LOAD_BADMACHO;
2306 goto bad2;
2307 }
2308
2309 *file_offset = fat_arch.offset;
2310 *macho_size = fat_arch.size;
2311 } else {
2312 /*
2313 * Force get_macho_vnode() to fail if the architecture bits
2314 * do not match the expected architecture bits. This in
2315 * turn causes load_dylinker() to fail for the same reason,
2316 * so it ensures the dynamic linker and the binary are in
2317 * lock-step. This is potentially bad, if we ever add to
2318 * the CPU_ARCH_* bits any bits that are desirable but not
2319 * required, since the dynamic linker might work, but we will
2320 * refuse to load it because of this check.
2321 */
2322 if ((cpu_type_t)(header->mach_header.cputype & CPU_ARCH_MASK) != archbits) {
2323 error = LOAD_BADARCH;
2324 goto bad2;
2325 }
2326
2327 *file_offset = 0;
2328 *macho_size = fsize;
2329 }
2330
2331 *mach_header = header->mach_header;
2332 *vpp = vp;
2333
2334 ubc_setsize(vp, fsize);
2335 return (error);
2336
2337 bad2:
2338 (void) VNOP_CLOSE(vp, FREAD, ctx);
2339 bad1:
2340 vnode_put(vp);
2341 return(error);
2342 }