]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/mach_loader.c
0d7ba242d27ff95ea07bba2625de1fbcbf9684e6
[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 kr = vm_map_apple_protected(map,
989 map_addr,
990 map_addr + map_size,
991 crypto_backing_offset,
992 &crypt_info);
993 }
994
995 if (kr != KERN_SUCCESS) {
996 return LOAD_FAILURE;
997 }
998 return LOAD_SUCCESS;
999 }
1000 #else /* CONFIG_CODE_DECRYPTION */
1001 static load_return_t
1002 unprotect_dsmos_segment(
1003 __unused uint64_t file_off,
1004 __unused uint64_t file_size,
1005 __unused struct vnode *vp,
1006 __unused off_t macho_offset,
1007 __unused vm_map_t map,
1008 __unused vm_map_offset_t map_addr,
1009 __unused vm_map_size_t map_size)
1010 {
1011 return LOAD_SUCCESS;
1012 }
1013 #endif /* CONFIG_CODE_DECRYPTION */
1014
1015
1016 /*
1017 * map_segment:
1018 * Maps a Mach-O segment, taking care of mis-alignment (wrt the system
1019 * page size) issues.
1020 *
1021 * The mapping might result in 1, 2 or 3 map entries:
1022 * 1. for the first page, which could be overlap with the previous
1023 * mapping,
1024 * 2. for the center (if applicable),
1025 * 3. for the last page, which could overlap with the next mapping.
1026 *
1027 * For each of those map entries, we might have to interpose a
1028 * "fourk_pager" to deal with mis-alignment wrt the system page size,
1029 * either in the mapping address and/or size or the file offset and/or
1030 * size.
1031 * The "fourk_pager" itself would be mapped with proper alignment
1032 * wrt the system page size and would then be populated with the
1033 * information about the intended mapping, with a "4KB" granularity.
1034 */
1035 static kern_return_t
1036 map_segment(
1037 vm_map_t map,
1038 vm_map_offset_t vm_start,
1039 vm_map_offset_t vm_end,
1040 memory_object_control_t control,
1041 vm_map_offset_t file_start,
1042 vm_map_offset_t file_end,
1043 vm_prot_t initprot,
1044 vm_prot_t maxprot)
1045 {
1046 int extra_vm_flags, cur_extra_vm_flags;
1047 vm_map_offset_t cur_offset, cur_start, cur_end;
1048 kern_return_t ret;
1049 vm_map_offset_t effective_page_mask;
1050
1051 if (vm_end < vm_start ||
1052 file_end < file_start) {
1053 return LOAD_BADMACHO;
1054 }
1055 if (vm_end == vm_start ||
1056 file_end == file_start) {
1057 /* nothing to map... */
1058 return LOAD_SUCCESS;
1059 }
1060
1061 effective_page_mask = MAX(PAGE_MASK, vm_map_page_mask(map));
1062
1063 extra_vm_flags = 0;
1064 if (vm_map_page_aligned(vm_start, effective_page_mask) &&
1065 vm_map_page_aligned(vm_end, effective_page_mask) &&
1066 vm_map_page_aligned(file_start, effective_page_mask) &&
1067 vm_map_page_aligned(file_end, effective_page_mask)) {
1068 /* all page-aligned and map-aligned: proceed */
1069 } else {
1070 panic("map_segment: unexpected mis-alignment "
1071 "vm[0x%llx:0x%llx] file[0x%llx:0x%llx]\n",
1072 (uint64_t) vm_start,
1073 (uint64_t) vm_end,
1074 (uint64_t) file_start,
1075 (uint64_t) file_end);
1076 }
1077
1078 cur_offset = 0;
1079 cur_start = vm_start;
1080 cur_end = vm_start;
1081 if (cur_end >= vm_start + (file_end - file_start)) {
1082 /* all mapped: done */
1083 goto done;
1084 }
1085 if (vm_map_round_page(cur_end, effective_page_mask) >=
1086 vm_map_trunc_page(vm_start + (file_end - file_start),
1087 effective_page_mask)) {
1088 /* no middle */
1089 } else {
1090 cur_start = cur_end;
1091 if ((vm_start & effective_page_mask) !=
1092 (file_start & effective_page_mask)) {
1093 /* one 4K pager for the middle */
1094 cur_extra_vm_flags = extra_vm_flags;
1095 } else {
1096 /* regular mapping for the middle */
1097 cur_extra_vm_flags = 0;
1098 }
1099 cur_end = vm_map_trunc_page(vm_start + (file_end -
1100 file_start),
1101 effective_page_mask);
1102 if (control != MEMORY_OBJECT_CONTROL_NULL) {
1103 ret = vm_map_enter_mem_object_control(
1104 map,
1105 &cur_start,
1106 cur_end - cur_start,
1107 (mach_vm_offset_t)0,
1108 VM_FLAGS_FIXED | cur_extra_vm_flags,
1109 control,
1110 file_start + cur_offset,
1111 TRUE, /* copy */
1112 initprot, maxprot,
1113 VM_INHERIT_DEFAULT);
1114 } else {
1115 ret = vm_map_enter_mem_object(
1116 map,
1117 &cur_start,
1118 cur_end - cur_start,
1119 (mach_vm_offset_t)0,
1120 VM_FLAGS_FIXED | cur_extra_vm_flags,
1121 IPC_PORT_NULL,
1122 0, /* offset */
1123 TRUE, /* copy */
1124 initprot, maxprot,
1125 VM_INHERIT_DEFAULT);
1126 }
1127 if (ret != KERN_SUCCESS) {
1128 return (LOAD_NOSPACE);
1129 }
1130 cur_offset += cur_end - cur_start;
1131 }
1132 if (cur_end >= vm_start + (file_end - file_start)) {
1133 /* all mapped: done */
1134 goto done;
1135 }
1136 cur_start = cur_end;
1137 done:
1138 assert(cur_end >= vm_start + (file_end - file_start));
1139 return LOAD_SUCCESS;
1140 }
1141
1142 static
1143 load_return_t
1144 load_segment(
1145 struct load_command *lcp,
1146 uint32_t filetype,
1147 void * control,
1148 off_t pager_offset,
1149 off_t macho_size,
1150 struct vnode *vp,
1151 vm_map_t map,
1152 int64_t slide,
1153 load_result_t *result)
1154 {
1155 struct segment_command_64 segment_command, *scp;
1156 kern_return_t ret;
1157 vm_map_size_t delta_size;
1158 vm_prot_t initprot;
1159 vm_prot_t maxprot;
1160 size_t segment_command_size, total_section_size,
1161 single_section_size;
1162 vm_map_offset_t file_offset, file_size;
1163 vm_map_offset_t vm_offset, vm_size;
1164 vm_map_offset_t vm_start, vm_end, vm_end_aligned;
1165 vm_map_offset_t file_start, file_end;
1166 kern_return_t kr;
1167 boolean_t verbose;
1168 vm_map_size_t effective_page_size;
1169 vm_map_offset_t effective_page_mask;
1170
1171 effective_page_size = MAX(PAGE_SIZE, vm_map_page_size(map));
1172 effective_page_mask = MAX(PAGE_MASK, vm_map_page_mask(map));
1173
1174 verbose = FALSE;
1175 if (LC_SEGMENT_64 == lcp->cmd) {
1176 segment_command_size = sizeof(struct segment_command_64);
1177 single_section_size = sizeof(struct section_64);
1178 } else {
1179 segment_command_size = sizeof(struct segment_command);
1180 single_section_size = sizeof(struct section);
1181 }
1182 if (lcp->cmdsize < segment_command_size)
1183 return (LOAD_BADMACHO);
1184 total_section_size = lcp->cmdsize - segment_command_size;
1185
1186 if (LC_SEGMENT_64 == lcp->cmd) {
1187 scp = (struct segment_command_64 *)lcp;
1188 } else {
1189 scp = &segment_command;
1190 widen_segment_command((struct segment_command *)lcp, scp);
1191 }
1192
1193 if (verbose) {
1194 MACHO_PRINTF(("+++ load_segment %s "
1195 "vm[0x%llx:0x%llx] file[0x%llx:0x%llx] "
1196 "prot %d/%d flags 0x%x\n",
1197 scp->segname,
1198 (uint64_t)(slide + scp->vmaddr),
1199 (uint64_t)(slide + scp->vmaddr + scp->vmsize),
1200 pager_offset + scp->fileoff,
1201 pager_offset + scp->fileoff + scp->filesize,
1202 scp->initprot,
1203 scp->maxprot,
1204 scp->flags));
1205 }
1206
1207 /*
1208 * Make sure what we get from the file is really ours (as specified
1209 * by macho_size).
1210 */
1211 if (scp->fileoff + scp->filesize < scp->fileoff ||
1212 scp->fileoff + scp->filesize > (uint64_t)macho_size) {
1213 return (LOAD_BADMACHO);
1214 }
1215 /*
1216 * Ensure that the number of sections specified would fit
1217 * within the load command size.
1218 */
1219 if (total_section_size / single_section_size < scp->nsects) {
1220 return (LOAD_BADMACHO);
1221 }
1222 /*
1223 * Make sure the segment is page-aligned in the file.
1224 */
1225 file_offset = pager_offset + scp->fileoff; /* limited to 32 bits */
1226 file_size = scp->filesize;
1227 if ((file_offset & PAGE_MASK_64) != 0 ||
1228 /* we can't mmap() it if it's not page-aligned in the file */
1229 (file_offset & vm_map_page_mask(map)) != 0) {
1230 /*
1231 * The 1st test would have failed if the system's page size
1232 * was what this process believe is the page size, so let's
1233 * fail here too for the sake of consistency.
1234 */
1235 return (LOAD_BADMACHO);
1236 }
1237
1238 /*
1239 * If we have a code signature attached for this slice
1240 * require that the segments are within the signed part
1241 * of the file.
1242 */
1243 if (result->cs_end_offset &&
1244 result->cs_end_offset < (off_t)scp->fileoff &&
1245 result->cs_end_offset - scp->fileoff < scp->filesize)
1246 {
1247 if (cs_debug)
1248 printf("section outside code signature\n");
1249 return LOAD_BADMACHO;
1250 }
1251
1252 vm_offset = scp->vmaddr + slide;
1253 vm_size = scp->vmsize;
1254
1255 if (vm_size == 0)
1256 return (LOAD_SUCCESS);
1257 if (scp->vmaddr == 0 &&
1258 file_size == 0 &&
1259 vm_size != 0 &&
1260 (scp->initprot & VM_PROT_ALL) == VM_PROT_NONE &&
1261 (scp->maxprot & VM_PROT_ALL) == VM_PROT_NONE) {
1262 /*
1263 * For PIE, extend page zero rather than moving it. Extending
1264 * page zero keeps early allocations from falling predictably
1265 * between the end of page zero and the beginning of the first
1266 * slid segment.
1267 */
1268 /*
1269 * This is a "page zero" segment: it starts at address 0,
1270 * is not mapped from the binary file and is not accessible.
1271 * User-space should never be able to access that memory, so
1272 * make it completely off limits by raising the VM map's
1273 * minimum offset.
1274 */
1275 vm_end = vm_offset + vm_size;
1276 if (vm_end < vm_offset) {
1277 return (LOAD_BADMACHO);
1278 }
1279 if (verbose) {
1280 MACHO_PRINTF(("++++++ load_segment: "
1281 "page_zero up to 0x%llx\n",
1282 (uint64_t) vm_end));
1283 }
1284 {
1285 vm_end = vm_map_round_page(vm_end,
1286 PAGE_MASK_64);
1287 vm_end_aligned = vm_end;
1288 }
1289 ret = vm_map_raise_min_offset(map,
1290 vm_end_aligned);
1291
1292 if (ret != KERN_SUCCESS) {
1293 return (LOAD_FAILURE);
1294 }
1295 return (LOAD_SUCCESS);
1296 } else {
1297 }
1298
1299 {
1300 file_start = vm_map_trunc_page(file_offset,
1301 effective_page_mask);
1302 file_end = vm_map_round_page(file_offset + file_size,
1303 effective_page_mask);
1304 vm_start = vm_map_trunc_page(vm_offset,
1305 effective_page_mask);
1306 vm_end = vm_map_round_page(vm_offset + vm_size,
1307 effective_page_mask);
1308 }
1309
1310 if (vm_start < result->min_vm_addr)
1311 result->min_vm_addr = vm_start;
1312 if (vm_end > result->max_vm_addr)
1313 result->max_vm_addr = vm_end;
1314
1315 if (map == VM_MAP_NULL)
1316 return (LOAD_SUCCESS);
1317
1318 if (vm_size > 0) {
1319 initprot = (scp->initprot) & VM_PROT_ALL;
1320 maxprot = (scp->maxprot) & VM_PROT_ALL;
1321 /*
1322 * Map a copy of the file into the address space.
1323 */
1324 if (verbose) {
1325 MACHO_PRINTF(("++++++ load_segment: "
1326 "mapping at vm [0x%llx:0x%llx] of "
1327 "file [0x%llx:0x%llx]\n",
1328 (uint64_t) vm_start,
1329 (uint64_t) vm_end,
1330 (uint64_t) file_start,
1331 (uint64_t) file_end));
1332 }
1333 ret = map_segment(map,
1334 vm_start,
1335 vm_end,
1336 control,
1337 file_start,
1338 file_end,
1339 initprot,
1340 maxprot);
1341 if (ret) {
1342 return LOAD_NOSPACE;
1343 }
1344
1345 #if FIXME
1346 /*
1347 * If the file didn't end on a page boundary,
1348 * we need to zero the leftover.
1349 */
1350 delta_size = map_size - scp->filesize;
1351 if (delta_size > 0) {
1352 mach_vm_offset_t tmp;
1353
1354 ret = mach_vm_allocate(kernel_map, &tmp, delta_size, VM_FLAGS_ANYWHERE| VM_MAKE_TAG(VM_KERN_MEMORY_BSD));
1355 if (ret != KERN_SUCCESS) {
1356 return(LOAD_RESOURCE);
1357 }
1358
1359 if (copyout(tmp, map_addr + scp->filesize,
1360 delta_size)) {
1361 (void) mach_vm_deallocate(
1362 kernel_map, tmp, delta_size);
1363 return (LOAD_FAILURE);
1364 }
1365
1366 (void) mach_vm_deallocate(kernel_map, tmp, delta_size);
1367 }
1368 #endif /* FIXME */
1369 }
1370
1371 /*
1372 * If the virtual size of the segment is greater
1373 * than the size from the file, we need to allocate
1374 * zero fill memory for the rest.
1375 */
1376 if ((vm_end - vm_start) > (file_end - file_start)) {
1377 delta_size = (vm_end - vm_start) - (file_end - file_start);
1378 } else {
1379 delta_size = 0;
1380 }
1381 if (delta_size > 0) {
1382 mach_vm_offset_t tmp;
1383
1384 tmp = vm_start + (file_end - file_start);
1385 if (verbose) {
1386 MACHO_PRINTF(("++++++ load_segment: "
1387 "delta mapping vm [0x%llx:0x%llx]\n",
1388 (uint64_t) tmp,
1389 (uint64_t) (tmp + delta_size)));
1390 }
1391 kr = map_segment(map,
1392 tmp,
1393 tmp + delta_size,
1394 MEMORY_OBJECT_CONTROL_NULL,
1395 0,
1396 delta_size,
1397 scp->initprot,
1398 scp->maxprot);
1399 if (kr != KERN_SUCCESS) {
1400 return(LOAD_NOSPACE);
1401 }
1402 }
1403
1404 if ( (scp->fileoff == 0) && (scp->filesize != 0) )
1405 result->mach_header = vm_offset;
1406
1407 if (scp->flags & SG_PROTECTED_VERSION_1) {
1408 ret = unprotect_dsmos_segment(file_start,
1409 file_end - file_start,
1410 vp,
1411 pager_offset,
1412 map,
1413 vm_start,
1414 vm_end - vm_start);
1415 if (ret != LOAD_SUCCESS) {
1416 return ret;
1417 }
1418 } else {
1419 ret = LOAD_SUCCESS;
1420 }
1421
1422 if (LOAD_SUCCESS == ret &&
1423 filetype == MH_DYLINKER &&
1424 result->all_image_info_addr == MACH_VM_MIN_ADDRESS) {
1425 note_all_image_info_section(scp,
1426 LC_SEGMENT_64 == lcp->cmd,
1427 single_section_size,
1428 ((const char *)lcp +
1429 segment_command_size),
1430 slide,
1431 result);
1432 }
1433
1434 if (result->entry_point != MACH_VM_MIN_ADDRESS) {
1435 if ((result->entry_point >= vm_offset) && (result->entry_point < (vm_offset + vm_size))) {
1436 if ((scp->initprot & (VM_PROT_READ|VM_PROT_EXECUTE)) == (VM_PROT_READ|VM_PROT_EXECUTE)) {
1437 result->validentry = 1;
1438 } else {
1439 /* right range but wrong protections, unset if previously validated */
1440 result->validentry = 0;
1441 }
1442 }
1443 }
1444
1445 return ret;
1446 }
1447
1448 static
1449 load_return_t
1450 load_uuid(
1451 struct uuid_command *uulp,
1452 char *command_end,
1453 load_result_t *result
1454 )
1455 {
1456 /*
1457 * We need to check the following for this command:
1458 * - The command size should be atleast the size of struct uuid_command
1459 * - The UUID part of the command should be completely within the mach-o header
1460 */
1461
1462 if ((uulp->cmdsize < sizeof(struct uuid_command)) ||
1463 (((char *)uulp + sizeof(struct uuid_command)) > command_end)) {
1464 return (LOAD_BADMACHO);
1465 }
1466
1467 memcpy(&result->uuid[0], &uulp->uuid[0], sizeof(result->uuid));
1468 return (LOAD_SUCCESS);
1469 }
1470
1471 static
1472 load_return_t
1473 load_main(
1474 struct entry_point_command *epc,
1475 thread_t thread,
1476 int64_t slide,
1477 load_result_t *result
1478 )
1479 {
1480 mach_vm_offset_t addr;
1481 kern_return_t ret;
1482
1483 if (epc->cmdsize < sizeof(*epc))
1484 return (LOAD_BADMACHO);
1485 if (result->thread_count != 0) {
1486 return (LOAD_FAILURE);
1487 }
1488
1489 if (thread == THREAD_NULL)
1490 return (LOAD_SUCCESS);
1491
1492 /* LC_MAIN specifies stack size but not location */
1493 if (epc->stacksize) {
1494 result->prog_stack_size = 1;
1495 result->user_stack_size = epc->stacksize;
1496 } else {
1497 result->prog_stack_size = 0;
1498 result->user_stack_size = MAXSSIZ;
1499 }
1500 result->prog_allocated_stack = 0;
1501
1502 /* use default location for stack */
1503 ret = thread_userstackdefault(thread, &addr);
1504 if (ret != KERN_SUCCESS)
1505 return(LOAD_FAILURE);
1506
1507 /* The stack slides down from the default location */
1508 result->user_stack = addr;
1509 result->user_stack -= slide;
1510
1511 if (result->using_lcmain || result->entry_point != MACH_VM_MIN_ADDRESS) {
1512 /* Already processed LC_MAIN or LC_UNIXTHREAD */
1513 return (LOAD_FAILURE);
1514 }
1515
1516 /* kernel does *not* use entryoff from LC_MAIN. Dyld uses it. */
1517 result->needs_dynlinker = TRUE;
1518 result->using_lcmain = TRUE;
1519 result->unixproc = TRUE;
1520 result->thread_count++;
1521
1522 return(LOAD_SUCCESS);
1523 }
1524
1525
1526 static
1527 load_return_t
1528 load_unixthread(
1529 struct thread_command *tcp,
1530 thread_t thread,
1531 int64_t slide,
1532 load_result_t *result
1533 )
1534 {
1535 load_return_t ret;
1536 int customstack =0;
1537 mach_vm_offset_t addr;
1538
1539 if (tcp->cmdsize < sizeof(*tcp))
1540 return (LOAD_BADMACHO);
1541 if (result->thread_count != 0) {
1542 return (LOAD_FAILURE);
1543 }
1544
1545 if (thread == THREAD_NULL)
1546 return (LOAD_SUCCESS);
1547
1548 ret = load_threadstack(thread,
1549 (uint32_t *)(((vm_offset_t)tcp) +
1550 sizeof(struct thread_command)),
1551 tcp->cmdsize - sizeof(struct thread_command),
1552 &addr,
1553 &customstack);
1554 if (ret != LOAD_SUCCESS)
1555 return(ret);
1556
1557 /* LC_UNIXTHREAD optionally specifies stack size and location */
1558
1559 if (customstack) {
1560 result->prog_stack_size = 0; /* unknown */
1561 result->prog_allocated_stack = 1;
1562 } else {
1563 result->prog_allocated_stack = 0;
1564 result->prog_stack_size = 0;
1565 result->user_stack_size = MAXSSIZ;
1566 }
1567
1568 /* The stack slides down from the default location */
1569 result->user_stack = addr;
1570 result->user_stack -= slide;
1571
1572 ret = load_threadentry(thread,
1573 (uint32_t *)(((vm_offset_t)tcp) +
1574 sizeof(struct thread_command)),
1575 tcp->cmdsize - sizeof(struct thread_command),
1576 &addr);
1577 if (ret != LOAD_SUCCESS)
1578 return(ret);
1579
1580 if (result->using_lcmain || result->entry_point != MACH_VM_MIN_ADDRESS) {
1581 /* Already processed LC_MAIN or LC_UNIXTHREAD */
1582 return (LOAD_FAILURE);
1583 }
1584
1585 result->entry_point = addr;
1586 result->entry_point += slide;
1587
1588 ret = load_threadstate(thread,
1589 (uint32_t *)(((vm_offset_t)tcp) + sizeof(struct thread_command)),
1590 tcp->cmdsize - sizeof(struct thread_command),
1591 result);
1592 if (ret != LOAD_SUCCESS)
1593 return (ret);
1594
1595 result->unixproc = TRUE;
1596 result->thread_count++;
1597
1598 return(LOAD_SUCCESS);
1599 }
1600
1601 static
1602 load_return_t
1603 load_threadstate(
1604 thread_t thread,
1605 uint32_t *ts,
1606 uint32_t total_size,
1607 load_result_t *result
1608 )
1609 {
1610 uint32_t size;
1611 int flavor;
1612 uint32_t thread_size;
1613 uint32_t *local_ts = NULL;
1614 uint32_t local_ts_size = 0;
1615 int ret;
1616
1617 (void)thread;
1618
1619 if (total_size > 0) {
1620 local_ts_size = total_size;
1621 local_ts = kalloc(local_ts_size);
1622 if (local_ts == NULL) {
1623 return LOAD_FAILURE;
1624 }
1625 memcpy(local_ts, ts, local_ts_size);
1626 ts = local_ts;
1627 }
1628
1629 /*
1630 * Validate the new thread state; iterate through the state flavors in
1631 * the Mach-O file.
1632 * XXX: we should validate the machine state here, to avoid failing at
1633 * activation time where we can't bail out cleanly.
1634 */
1635 while (total_size > 0) {
1636 flavor = *ts++;
1637 size = *ts++;
1638
1639 if (os_add_overflow(size, UINT32_C(2), &thread_size) ||
1640 os_mul_overflow(thread_size, (uint32_t)sizeof(uint32_t), &thread_size) ||
1641 os_sub_overflow(total_size, thread_size, &total_size)) {
1642 ret = LOAD_BADMACHO;
1643 goto bad;
1644 }
1645
1646 ts += size; /* ts is a (uint32_t *) */
1647 }
1648
1649 result->threadstate = local_ts;
1650 result->threadstate_sz = local_ts_size;
1651 return LOAD_SUCCESS;
1652
1653 bad:
1654 if (local_ts) {
1655 kfree(local_ts, local_ts_size);
1656 }
1657 return ret;
1658 }
1659
1660 static
1661 load_return_t
1662 load_threadstack(
1663 thread_t thread,
1664 uint32_t *ts,
1665 uint32_t total_size,
1666 mach_vm_offset_t *user_stack,
1667 int *customstack
1668 )
1669 {
1670 kern_return_t ret;
1671 uint32_t size;
1672 int flavor;
1673 uint32_t stack_size;
1674
1675 while (total_size > 0) {
1676 flavor = *ts++;
1677 size = *ts++;
1678 if (UINT32_MAX-2 < size ||
1679 UINT32_MAX/sizeof(uint32_t) < size+2)
1680 return (LOAD_BADMACHO);
1681 stack_size = (size+2)*sizeof(uint32_t);
1682 if (stack_size > total_size)
1683 return(LOAD_BADMACHO);
1684 total_size -= stack_size;
1685
1686 /*
1687 * Third argument is a kernel space pointer; it gets cast
1688 * to the appropriate type in thread_userstack() based on
1689 * the value of flavor.
1690 */
1691 ret = thread_userstack(thread, flavor, (thread_state_t)ts, size, user_stack, customstack);
1692 if (ret != KERN_SUCCESS) {
1693 return(LOAD_FAILURE);
1694 }
1695 ts += size; /* ts is a (uint32_t *) */
1696 }
1697 return(LOAD_SUCCESS);
1698 }
1699
1700 static
1701 load_return_t
1702 load_threadentry(
1703 thread_t thread,
1704 uint32_t *ts,
1705 uint32_t total_size,
1706 mach_vm_offset_t *entry_point
1707 )
1708 {
1709 kern_return_t ret;
1710 uint32_t size;
1711 int flavor;
1712 uint32_t entry_size;
1713
1714 /*
1715 * Set the thread state.
1716 */
1717 *entry_point = MACH_VM_MIN_ADDRESS;
1718 while (total_size > 0) {
1719 flavor = *ts++;
1720 size = *ts++;
1721 if (UINT32_MAX-2 < size ||
1722 UINT32_MAX/sizeof(uint32_t) < size+2)
1723 return (LOAD_BADMACHO);
1724 entry_size = (size+2)*sizeof(uint32_t);
1725 if (entry_size > total_size)
1726 return(LOAD_BADMACHO);
1727 total_size -= entry_size;
1728 /*
1729 * Third argument is a kernel space pointer; it gets cast
1730 * to the appropriate type in thread_entrypoint() based on
1731 * the value of flavor.
1732 */
1733 ret = thread_entrypoint(thread, flavor, (thread_state_t)ts, size, entry_point);
1734 if (ret != KERN_SUCCESS) {
1735 return(LOAD_FAILURE);
1736 }
1737 ts += size; /* ts is a (uint32_t *) */
1738 }
1739 return(LOAD_SUCCESS);
1740 }
1741
1742 struct macho_data {
1743 struct nameidata __nid;
1744 union macho_vnode_header {
1745 struct mach_header mach_header;
1746 struct fat_header fat_header;
1747 char __pad[512];
1748 } __header;
1749 };
1750
1751 #define DEFAULT_DYLD_PATH "/usr/lib/dyld"
1752
1753 static load_return_t
1754 load_dylinker(
1755 struct dylinker_command *lcp,
1756 integer_t archbits,
1757 vm_map_t map,
1758 thread_t thread,
1759 int depth,
1760 int64_t slide,
1761 load_result_t *result
1762 )
1763 {
1764 char *name;
1765 char *p;
1766 struct vnode *vp = NULLVP; /* set by get_macho_vnode() */
1767 struct mach_header *header;
1768 off_t file_offset = 0; /* set by get_macho_vnode() */
1769 off_t macho_size = 0; /* set by get_macho_vnode() */
1770 load_result_t *myresult;
1771 kern_return_t ret;
1772 struct macho_data *macho_data;
1773 struct {
1774 struct mach_header __header;
1775 load_result_t __myresult;
1776 struct macho_data __macho_data;
1777 } *dyld_data;
1778
1779 if (lcp->cmdsize < sizeof(*lcp))
1780 return (LOAD_BADMACHO);
1781
1782 name = (char *)lcp + lcp->name.offset;
1783 /*
1784 * Check for a proper null terminated string.
1785 */
1786 p = name;
1787 do {
1788 if (p >= (char *)lcp + lcp->cmdsize)
1789 return(LOAD_BADMACHO);
1790 } while (*p++);
1791
1792 #if !(DEVELOPMENT || DEBUG)
1793 if (0 != strcmp(name, DEFAULT_DYLD_PATH)) {
1794 return (LOAD_BADMACHO);
1795 }
1796 #endif
1797
1798 /* Allocate wad-of-data from heap to reduce excessively deep stacks */
1799
1800 MALLOC(dyld_data, void *, sizeof (*dyld_data), M_TEMP, M_WAITOK);
1801 header = &dyld_data->__header;
1802 myresult = &dyld_data->__myresult;
1803 macho_data = &dyld_data->__macho_data;
1804
1805 ret = get_macho_vnode(name, archbits, header,
1806 &file_offset, &macho_size, macho_data, &vp);
1807 if (ret)
1808 goto novp_out;
1809
1810 *myresult = load_result_null;
1811
1812 /*
1813 * First try to map dyld in directly. This should work most of
1814 * the time since there shouldn't normally be something already
1815 * mapped to its address.
1816 */
1817
1818 ret = parse_machfile(vp, map, thread, header, file_offset,
1819 macho_size, depth, slide, 0, myresult);
1820
1821 /*
1822 * If it turned out something was in the way, then we'll take
1823 * take this longer path to preflight dyld's vm ranges, then
1824 * map it at a free location in the address space.
1825 */
1826
1827 if (ret == LOAD_NOSPACE) {
1828 mach_vm_offset_t dyl_start, map_addr;
1829 mach_vm_size_t dyl_length;
1830 int64_t slide_amount;
1831
1832 *myresult = load_result_null;
1833
1834 /*
1835 * Preflight parsing the Mach-O file with a NULL
1836 * map, which will return the ranges needed for a
1837 * subsequent map attempt (with a slide) in "myresult"
1838 */
1839 ret = parse_machfile(vp, VM_MAP_NULL, THREAD_NULL, header,
1840 file_offset, macho_size, depth,
1841 0 /* slide */, 0, myresult);
1842
1843 if (ret != LOAD_SUCCESS) {
1844 goto out;
1845 }
1846
1847 dyl_start = myresult->min_vm_addr;
1848 dyl_length = myresult->max_vm_addr - myresult->min_vm_addr;
1849
1850 dyl_length += slide;
1851
1852 /* To find an appropriate load address, do a quick allocation */
1853 map_addr = dyl_start;
1854 ret = mach_vm_allocate(map, &map_addr, dyl_length, VM_FLAGS_ANYWHERE);
1855 if (ret != KERN_SUCCESS) {
1856 ret = LOAD_NOSPACE;
1857 goto out;
1858 }
1859
1860 ret = mach_vm_deallocate(map, map_addr, dyl_length);
1861 if (ret != KERN_SUCCESS) {
1862 ret = LOAD_NOSPACE;
1863 goto out;
1864 }
1865
1866 if (map_addr < dyl_start)
1867 slide_amount = -(int64_t)(dyl_start - map_addr);
1868 else
1869 slide_amount = (int64_t)(map_addr - dyl_start);
1870
1871 slide_amount += slide;
1872
1873 *myresult = load_result_null;
1874
1875 ret = parse_machfile(vp, map, thread, header,
1876 file_offset, macho_size, depth,
1877 slide_amount, 0, myresult);
1878
1879 if (ret) {
1880 goto out;
1881 }
1882 }
1883
1884 if (ret == LOAD_SUCCESS) {
1885 if (result->threadstate) {
1886 /* don't use the app's threadstate if we have a dyld */
1887 kfree(result->threadstate, result->threadstate_sz);
1888 }
1889 result->threadstate = myresult->threadstate;
1890 result->threadstate_sz = myresult->threadstate_sz;
1891
1892 result->dynlinker = TRUE;
1893 result->entry_point = myresult->entry_point;
1894 result->validentry = myresult->validentry;
1895 result->all_image_info_addr = myresult->all_image_info_addr;
1896 result->all_image_info_size = myresult->all_image_info_size;
1897 if (myresult->platform_binary) {
1898 result->csflags |= CS_DYLD_PLATFORM;
1899 }
1900 }
1901 out:
1902 vnode_put(vp);
1903 novp_out:
1904 FREE(dyld_data, M_TEMP);
1905 return (ret);
1906
1907 }
1908
1909 static load_return_t
1910 load_code_signature(
1911 struct linkedit_data_command *lcp,
1912 struct vnode *vp,
1913 off_t macho_offset,
1914 off_t macho_size,
1915 cpu_type_t cputype,
1916 load_result_t *result)
1917 {
1918 int ret;
1919 kern_return_t kr;
1920 vm_offset_t addr;
1921 int resid;
1922 struct cs_blob *blob;
1923 int error;
1924 vm_size_t blob_size;
1925
1926 addr = 0;
1927 blob = NULL;
1928
1929 if (lcp->cmdsize != sizeof (struct linkedit_data_command) ||
1930 lcp->dataoff + lcp->datasize > macho_size) {
1931 ret = LOAD_BADMACHO;
1932 goto out;
1933 }
1934
1935 blob = ubc_cs_blob_get(vp, cputype, macho_offset);
1936 if (blob != NULL) {
1937 /* we already have a blob for this vnode and cputype */
1938 if (blob->csb_cpu_type == cputype &&
1939 blob->csb_base_offset == macho_offset &&
1940 blob->csb_mem_size == lcp->datasize) {
1941 /* it matches the blob we want here, lets verify the version */
1942 if(0 != ubc_cs_generation_check(vp)) {
1943 if (0 != ubc_cs_blob_revalidate(vp, blob, 0)) {
1944 ret = LOAD_FAILURE; /* set error same as from ubc_cs_blob_add */
1945 goto out;
1946 }
1947 }
1948 ret = LOAD_SUCCESS;
1949 } else {
1950 /* the blob has changed for this vnode: fail ! */
1951 ret = LOAD_BADMACHO;
1952 }
1953 goto out;
1954 }
1955
1956 blob_size = lcp->datasize;
1957 kr = ubc_cs_blob_allocate(&addr, &blob_size);
1958 if (kr != KERN_SUCCESS) {
1959 ret = LOAD_NOSPACE;
1960 goto out;
1961 }
1962
1963 resid = 0;
1964 error = vn_rdwr(UIO_READ,
1965 vp,
1966 (caddr_t) addr,
1967 lcp->datasize,
1968 macho_offset + lcp->dataoff,
1969 UIO_SYSSPACE,
1970 0,
1971 kauth_cred_get(),
1972 &resid,
1973 current_proc());
1974 if (error || resid != 0) {
1975 ret = LOAD_IOERROR;
1976 goto out;
1977 }
1978
1979 if (ubc_cs_blob_add(vp,
1980 cputype,
1981 macho_offset,
1982 addr,
1983 lcp->datasize,
1984 0,
1985 &blob)) {
1986 ret = LOAD_FAILURE;
1987 goto out;
1988 } else {
1989 /* ubc_cs_blob_add() has consumed "addr" */
1990 addr = 0;
1991 }
1992
1993 #if CHECK_CS_VALIDATION_BITMAP
1994 ubc_cs_validation_bitmap_allocate( vp );
1995 #endif
1996
1997 ret = LOAD_SUCCESS;
1998 out:
1999 if (ret == LOAD_SUCCESS) {
2000 if (blob == NULL)
2001 panic("sucess, but no blob!");
2002
2003 result->csflags |= blob->csb_flags;
2004 result->platform_binary = blob->csb_platform_binary;
2005 result->cs_end_offset = blob->csb_end_offset;
2006 }
2007 if (addr != 0) {
2008 ubc_cs_blob_deallocate(addr, blob_size);
2009 addr = 0;
2010 }
2011
2012 return ret;
2013 }
2014
2015
2016 #if CONFIG_CODE_DECRYPTION
2017
2018 static load_return_t
2019 set_code_unprotect(
2020 struct encryption_info_command *eip,
2021 caddr_t addr,
2022 vm_map_t map,
2023 int64_t slide,
2024 struct vnode *vp,
2025 off_t macho_offset,
2026 cpu_type_t cputype,
2027 cpu_subtype_t cpusubtype)
2028 {
2029 int error, len;
2030 pager_crypt_info_t crypt_info;
2031 const char * cryptname = 0;
2032 char *vpath;
2033
2034 size_t offset;
2035 struct segment_command_64 *seg64;
2036 struct segment_command *seg32;
2037 vm_map_offset_t map_offset, map_size;
2038 vm_object_offset_t crypto_backing_offset;
2039 kern_return_t kr;
2040
2041 if (eip->cmdsize < sizeof(*eip)) return LOAD_BADMACHO;
2042
2043 switch(eip->cryptid) {
2044 case 0:
2045 /* not encrypted, just an empty load command */
2046 return LOAD_SUCCESS;
2047 case 1:
2048 cryptname="com.apple.unfree";
2049 break;
2050 case 0x10:
2051 /* some random cryptid that you could manually put into
2052 * your binary if you want NULL */
2053 cryptname="com.apple.null";
2054 break;
2055 default:
2056 return LOAD_BADMACHO;
2057 }
2058
2059 if (map == VM_MAP_NULL) return (LOAD_SUCCESS);
2060 if (NULL == text_crypter_create) return LOAD_FAILURE;
2061
2062 MALLOC_ZONE(vpath, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
2063 if(vpath == NULL) return LOAD_FAILURE;
2064
2065 len = MAXPATHLEN;
2066 error = vn_getpath(vp, vpath, &len);
2067 if (error) {
2068 FREE_ZONE(vpath, MAXPATHLEN, M_NAMEI);
2069 return LOAD_FAILURE;
2070 }
2071
2072 /* set up decrypter first */
2073 crypt_file_data_t crypt_data = {
2074 .filename = vpath,
2075 .cputype = cputype,
2076 .cpusubtype = cpusubtype};
2077 kr=text_crypter_create(&crypt_info, cryptname, (void*)&crypt_data);
2078 #if DEVELOPMENT || DEBUG
2079 struct proc *p;
2080 p = current_proc();
2081 printf("APPLE_PROTECT: %d[%s] map %p %s(%s) -> 0x%x\n",
2082 p->p_pid, p->p_comm, map, __FUNCTION__, vpath, kr);
2083 #endif /* DEVELOPMENT || DEBUG */
2084 FREE_ZONE(vpath, MAXPATHLEN, M_NAMEI);
2085
2086 if(kr) {
2087 printf("set_code_unprotect: unable to create decrypter %s, kr=%d\n",
2088 cryptname, kr);
2089 if (kr == kIOReturnNotPrivileged) {
2090 /* text encryption returned decryption failure */
2091 return(LOAD_DECRYPTFAIL);
2092 }else
2093 return LOAD_RESOURCE;
2094 }
2095
2096 /* this is terrible, but we have to rescan the load commands to find the
2097 * virtual address of this encrypted stuff. This code is gonna look like
2098 * the dyld source one day... */
2099 struct mach_header *header = (struct mach_header *)addr;
2100 size_t mach_header_sz = sizeof(struct mach_header);
2101 if (header->magic == MH_MAGIC_64 ||
2102 header->magic == MH_CIGAM_64) {
2103 mach_header_sz = sizeof(struct mach_header_64);
2104 }
2105 offset = mach_header_sz;
2106 uint32_t ncmds = header->ncmds;
2107 while (ncmds--) {
2108 /*
2109 * Get a pointer to the command.
2110 */
2111 struct load_command *lcp = (struct load_command *)(addr + offset);
2112 offset += lcp->cmdsize;
2113
2114 switch(lcp->cmd) {
2115 case LC_SEGMENT_64:
2116 seg64 = (struct segment_command_64 *)lcp;
2117 if ((seg64->fileoff <= eip->cryptoff) &&
2118 (seg64->fileoff+seg64->filesize >=
2119 eip->cryptoff+eip->cryptsize)) {
2120 map_offset = seg64->vmaddr + eip->cryptoff - seg64->fileoff + slide;
2121 map_size = eip->cryptsize;
2122 crypto_backing_offset = macho_offset + eip->cryptoff;
2123 goto remap_now;
2124 }
2125 case LC_SEGMENT:
2126 seg32 = (struct segment_command *)lcp;
2127 if ((seg32->fileoff <= eip->cryptoff) &&
2128 (seg32->fileoff+seg32->filesize >=
2129 eip->cryptoff+eip->cryptsize)) {
2130 map_offset = seg32->vmaddr + eip->cryptoff - seg32->fileoff + slide;
2131 map_size = eip->cryptsize;
2132 crypto_backing_offset = macho_offset + eip->cryptoff;
2133 goto remap_now;
2134 }
2135 }
2136 }
2137
2138 /* if we get here, did not find anything */
2139 return LOAD_BADMACHO;
2140
2141 remap_now:
2142 /* now remap using the decrypter */
2143 MACHO_PRINTF(("+++ set_code_unprotect: vm[0x%llx:0x%llx]\n",
2144 (uint64_t) map_offset,
2145 (uint64_t) (map_offset+map_size)));
2146 kr = vm_map_apple_protected(map,
2147 map_offset,
2148 map_offset+map_size,
2149 crypto_backing_offset,
2150 &crypt_info);
2151 if (kr) {
2152 printf("set_code_unprotect(): mapping failed with %x\n", kr);
2153 return LOAD_PROTECT;
2154 }
2155
2156 return LOAD_SUCCESS;
2157 }
2158
2159 #endif
2160
2161 /*
2162 * This routine exists to support the load_dylinker().
2163 *
2164 * This routine has its own, separate, understanding of the FAT file format,
2165 * which is terrifically unfortunate.
2166 */
2167 static
2168 load_return_t
2169 get_macho_vnode(
2170 char *path,
2171 integer_t archbits,
2172 struct mach_header *mach_header,
2173 off_t *file_offset,
2174 off_t *macho_size,
2175 struct macho_data *data,
2176 struct vnode **vpp
2177 )
2178 {
2179 struct vnode *vp;
2180 vfs_context_t ctx = vfs_context_current();
2181 proc_t p = vfs_context_proc(ctx);
2182 kauth_cred_t kerncred;
2183 struct nameidata *ndp = &data->__nid;
2184 boolean_t is_fat;
2185 struct fat_arch fat_arch;
2186 int error;
2187 int resid;
2188 union macho_vnode_header *header = &data->__header;
2189 off_t fsize = (off_t)0;
2190
2191 /*
2192 * Capture the kernel credential for use in the actual read of the
2193 * file, since the user doing the execution may have execute rights
2194 * but not read rights, but to exec something, we have to either map
2195 * or read it into the new process address space, which requires
2196 * read rights. This is to deal with lack of common credential
2197 * serialization code which would treat NOCRED as "serialize 'root'".
2198 */
2199 kerncred = vfs_context_ucred(vfs_context_kernel());
2200
2201 /* init the namei data to point the file user's program name */
2202 NDINIT(ndp, LOOKUP, OP_OPEN, FOLLOW | LOCKLEAF, UIO_SYSSPACE, CAST_USER_ADDR_T(path), ctx);
2203
2204 if ((error = namei(ndp)) != 0) {
2205 if (error == ENOENT) {
2206 error = LOAD_ENOENT;
2207 } else {
2208 error = LOAD_FAILURE;
2209 }
2210 return(error);
2211 }
2212 nameidone(ndp);
2213 vp = ndp->ni_vp;
2214
2215 /* check for regular file */
2216 if (vp->v_type != VREG) {
2217 error = LOAD_PROTECT;
2218 goto bad1;
2219 }
2220
2221 /* get size */
2222 if ((error = vnode_size(vp, &fsize, ctx)) != 0) {
2223 error = LOAD_FAILURE;
2224 goto bad1;
2225 }
2226
2227 /* Check mount point */
2228 if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
2229 error = LOAD_PROTECT;
2230 goto bad1;
2231 }
2232
2233 /* check access */
2234 if ((error = vnode_authorize(vp, NULL, KAUTH_VNODE_EXECUTE | KAUTH_VNODE_READ_DATA, ctx)) != 0) {
2235 error = LOAD_PROTECT;
2236 goto bad1;
2237 }
2238
2239 /* try to open it */
2240 if ((error = VNOP_OPEN(vp, FREAD, ctx)) != 0) {
2241 error = LOAD_PROTECT;
2242 goto bad1;
2243 }
2244
2245 if ((error = vn_rdwr(UIO_READ, vp, (caddr_t)header, sizeof (*header), 0,
2246 UIO_SYSSPACE, IO_NODELOCKED, kerncred, &resid, p)) != 0) {
2247 error = LOAD_IOERROR;
2248 goto bad2;
2249 }
2250
2251 if (resid) {
2252 error = LOAD_BADMACHO;
2253 goto bad2;
2254 }
2255
2256 if (header->mach_header.magic == MH_MAGIC ||
2257 header->mach_header.magic == MH_MAGIC_64) {
2258 is_fat = FALSE;
2259 } else if (OSSwapBigToHostInt32(header->fat_header.magic) == FAT_MAGIC) {
2260 is_fat = TRUE;
2261 } else {
2262 error = LOAD_BADMACHO;
2263 goto bad2;
2264 }
2265
2266 if (is_fat) {
2267
2268 error = fatfile_validate_fatarches((vm_offset_t)(&header->fat_header),
2269 sizeof(*header));
2270 if (error != LOAD_SUCCESS) {
2271 goto bad2;
2272 }
2273
2274 /* Look up our architecture in the fat file. */
2275 error = fatfile_getarch_with_bits(archbits,
2276 (vm_offset_t)(&header->fat_header), sizeof(*header), &fat_arch);
2277 if (error != LOAD_SUCCESS)
2278 goto bad2;
2279
2280 /* Read the Mach-O header out of it */
2281 error = vn_rdwr(UIO_READ, vp, (caddr_t)&header->mach_header,
2282 sizeof (header->mach_header), fat_arch.offset,
2283 UIO_SYSSPACE, IO_NODELOCKED, kerncred, &resid, p);
2284 if (error) {
2285 error = LOAD_IOERROR;
2286 goto bad2;
2287 }
2288
2289 if (resid) {
2290 error = LOAD_BADMACHO;
2291 goto bad2;
2292 }
2293
2294 /* Is this really a Mach-O? */
2295 if (header->mach_header.magic != MH_MAGIC &&
2296 header->mach_header.magic != MH_MAGIC_64) {
2297 error = LOAD_BADMACHO;
2298 goto bad2;
2299 }
2300
2301 *file_offset = fat_arch.offset;
2302 *macho_size = fat_arch.size;
2303 } else {
2304 /*
2305 * Force get_macho_vnode() to fail if the architecture bits
2306 * do not match the expected architecture bits. This in
2307 * turn causes load_dylinker() to fail for the same reason,
2308 * so it ensures the dynamic linker and the binary are in
2309 * lock-step. This is potentially bad, if we ever add to
2310 * the CPU_ARCH_* bits any bits that are desirable but not
2311 * required, since the dynamic linker might work, but we will
2312 * refuse to load it because of this check.
2313 */
2314 if ((cpu_type_t)(header->mach_header.cputype & CPU_ARCH_MASK) != archbits) {
2315 error = LOAD_BADARCH;
2316 goto bad2;
2317 }
2318
2319 *file_offset = 0;
2320 *macho_size = fsize;
2321 }
2322
2323 *mach_header = header->mach_header;
2324 *vpp = vp;
2325
2326 ubc_setsize(vp, fsize);
2327 return (error);
2328
2329 bad2:
2330 (void) VNOP_CLOSE(vp, FREAD, ctx);
2331 bad1:
2332 vnode_put(vp);
2333 return(error);
2334 }