]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/mach_loader.c
xnu-3248.60.10.tar.gz
[apple/xnu.git] / bsd / kern / mach_loader.c
CommitLineData
1c79356b 1/*
6d2010ae 2 * Copyright (c) 2000-2010 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
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 */
91447636 39
1c79356b 40#include <sys/param.h>
91447636 41#include <sys/vnode_internal.h>
1c79356b
A
42#include <sys/uio.h>
43#include <sys/namei.h>
91447636
A
44#include <sys/proc_internal.h>
45#include <sys/kauth.h>
1c79356b
A
46#include <sys/stat.h>
47#include <sys/malloc.h>
91447636 48#include <sys/mount_internal.h>
1c79356b 49#include <sys/fcntl.h>
91447636
A
50#include <sys/ubc_internal.h>
51#include <sys/imgact.h>
39236c6e 52#include <sys/codesign.h>
1c79356b 53
1c79356b 54#include <mach/mach_types.h>
91447636
A
55#include <mach/vm_map.h> /* vm_allocate() */
56#include <mach/mach_vm.h> /* mach_vm_allocate() */
57#include <mach/vm_statistics.h>
91447636
A
58#include <mach/task.h>
59#include <mach/thread_act.h>
60
61#include <machine/vmparam.h>
2d21ac55 62#include <machine/exec.h>
6d2010ae 63#include <machine/pal_routines.h>
1c79356b 64
91447636
A
65#include <kern/kern_types.h>
66#include <kern/cpu_number.h>
1c79356b 67#include <kern/mach_loader.h>
b0d623f7 68#include <kern/mach_fat.h>
91447636 69#include <kern/kalloc.h>
55e303ae 70#include <kern/task.h>
91447636 71#include <kern/thread.h>
593a1d5f 72#include <kern/page_decrypt.h>
1c79356b
A
73
74#include <mach-o/fat.h>
75#include <mach-o/loader.h>
76
91447636 77#include <vm/pmap.h>
1c79356b
A
78#include <vm/vm_map.h>
79#include <vm/vm_kern.h>
80#include <vm/vm_pager.h>
81#include <vm/vnode_pager.h>
91447636 82#include <vm/vm_protos.h>
39236c6e 83#include <IOKit/IOReturn.h> /* for kIOReturnNotPrivileged */
9bccf70c 84
490019cf
A
85#include <os/overflow.h>
86
91447636
A
87/*
88 * XXX vm/pmap.h should not treat these prototypes as MACH_KERNEL_PRIVATE
89 * when KERNEL is defined.
90 */
316670eb
A
91extern pmap_t pmap_create(ledger_t ledger, vm_map_size_t size,
92 boolean_t is_64bit);
91447636
A
93
94/* XXX should have prototypes in a shared header file */
91447636 95extern int get_map_nentries(vm_map_t);
91447636 96
2d21ac55
A
97extern kern_return_t memory_object_signed(memory_object_control_t control,
98 boolean_t is_signed);
91447636
A
99
100/* An empty load_result_t */
101static load_result_t load_result_null = {
2d21ac55
A
102 .mach_header = MACH_VM_MIN_ADDRESS,
103 .entry_point = MACH_VM_MIN_ADDRESS,
104 .user_stack = MACH_VM_MIN_ADDRESS,
316670eb 105 .user_stack_size = 0,
b0d623f7
A
106 .all_image_info_addr = MACH_VM_MIN_ADDRESS,
107 .all_image_info_size = 0,
2d21ac55
A
108 .thread_count = 0,
109 .unixproc = 0,
110 .dynlinker = 0,
316670eb
A
111 .needs_dynlinker = 0,
112 .prog_allocated_stack = 0,
113 .prog_stack_size = 0,
6d2010ae 114 .validentry = 0,
c18c124e 115 .using_lcmain = 0,
b0d623f7 116 .csflags = 0,
3e170ce0 117 .has_pagezero = 0,
6d2010ae
A
118 .uuid = { 0 },
119 .min_vm_addr = MACH_VM_MAX_ADDRESS,
04b8595b 120 .max_vm_addr = MACH_VM_MIN_ADDRESS,
490019cf
A
121 .cs_end_offset = 0,
122 .threadstate = NULL,
123 .threadstate_sz = 0
91447636 124};
9bccf70c 125
1c79356b
A
126/*
127 * Prototypes of static functions.
128 */
91447636 129static load_return_t
1c79356b
A
130parse_machfile(
131 struct vnode *vp,
91447636 132 vm_map_t map,
2d21ac55 133 thread_t thread,
1c79356b 134 struct mach_header *header,
91447636
A
135 off_t file_offset,
136 off_t macho_size,
91447636 137 int depth,
6d2010ae 138 int64_t slide,
39236c6e 139 int64_t dyld_slide,
91447636
A
140 load_result_t *result
141);
142
143static load_return_t
1c79356b 144load_segment(
b0d623f7
A
145 struct load_command *lcp,
146 uint32_t filetype,
147 void *control,
91447636
A
148 off_t pager_offset,
149 off_t macho_size,
b0d623f7 150 struct vnode *vp,
91447636 151 vm_map_t map,
6d2010ae 152 int64_t slide,
91447636
A
153 load_result_t *result
154);
155
39236c6e
A
156static load_return_t
157load_uuid(
158 struct uuid_command *uulp,
159 char *command_end,
160 load_result_t *result
161);
162
6d2010ae
A
163static load_return_t
164load_code_signature(
2d21ac55
A
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
593a1d5f
A
172#if CONFIG_CODE_DECRYPTION
173static load_return_t
174set_code_unprotect(
175 struct encryption_info_command *lcp,
176 caddr_t addr,
177 vm_map_t map,
316670eb 178 int64_t slide,
3e170ce0
A
179 struct vnode *vp,
180 off_t macho_offset,
39236c6e 181 cpu_type_t cputype,
3e170ce0 182 cpu_subtype_t cpusubtype);
593a1d5f
A
183#endif
184
316670eb
A
185static
186load_return_t
187load_main(
188 struct entry_point_command *epc,
189 thread_t thread,
190 int64_t slide,
191 load_result_t *result
192);
193
91447636 194static load_return_t
1c79356b
A
195load_unixthread(
196 struct thread_command *tcp,
2d21ac55 197 thread_t thread,
6d2010ae 198 int64_t slide,
0b4e3aa0 199 load_result_t *result
91447636
A
200);
201
202static load_return_t
1c79356b 203load_threadstate(
0b4e3aa0 204 thread_t thread,
b0d623f7 205 uint32_t *ts,
490019cf
A
206 uint32_t total_size,
207 load_result_t *
91447636
A
208);
209
210static load_return_t
1c79356b 211load_threadstack(
0b4e3aa0 212 thread_t thread,
b0d623f7
A
213 uint32_t *ts,
214 uint32_t total_size,
6d2010ae 215 mach_vm_offset_t *user_stack,
0b4e3aa0 216 int *customstack
91447636
A
217);
218
219static load_return_t
1c79356b 220load_threadentry(
0b4e3aa0 221 thread_t thread,
b0d623f7
A
222 uint32_t *ts,
223 uint32_t total_size,
91447636
A
224 mach_vm_offset_t *entry_point
225);
226
227static load_return_t
1c79356b
A
228load_dylinker(
229 struct dylinker_command *lcp,
91447636 230 integer_t archbits,
0b4e3aa0 231 vm_map_t map,
2d21ac55 232 thread_t thread,
0b4e3aa0 233 int depth,
6d2010ae
A
234 int64_t slide,
235 load_result_t *result
91447636
A
236);
237
6d2010ae
A
238struct macho_data;
239
91447636 240static load_return_t
1c79356b 241get_macho_vnode(
0b4e3aa0 242 char *path,
91447636 243 integer_t archbits,
1c79356b 244 struct mach_header *mach_header,
91447636
A
245 off_t *file_offset,
246 off_t *macho_size,
6d2010ae 247 struct macho_data *macho_data,
1c79356b
A
248 struct vnode **vpp
249);
250
b0d623f7
A
251static inline void
252widen_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
268static void
269note_all_image_info_section(const struct segment_command_64 *scp,
270 boolean_t is64, size_t section_size, const void *sections,
6d2010ae 271 int64_t slide, load_result_t *result)
b0d623f7
A
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;
6d2010ae 288 result->all_image_info_addr += slide;
b0d623f7
A
289 result->all_image_info_size =
290 is64 ? sectionp->s64.size : sectionp->s32.size;
291 return;
292 }
293 }
294}
295
3e170ce0 296
1c79356b
A
297load_return_t
298load_machfile(
91447636 299 struct image_params *imgp,
1c79356b 300 struct mach_header *header,
2d21ac55 301 thread_t thread,
490019cf 302 vm_map_t *mapp,
91447636 303 load_result_t *result
1c79356b
A
304)
305{
91447636
A
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;
316670eb 309 off_t file_size = imgp->ip_vattr->va_data_size;
490019cf 310 vm_map_t new_map = *mapp;
91447636 311 pmap_t pmap = 0; /* protected by create_map */
1c79356b 312 vm_map_t map;
1c79356b 313 load_result_t myresult;
1c79356b 314 load_return_t lret;
b0d623f7 315 boolean_t create_map = FALSE;
fe8ab488 316 boolean_t enforce_hard_pagezero = TRUE;
b0d623f7
A
317 int spawn = (imgp->ip_flags & IMGPF_SPAWN);
318 task_t task = current_task();
13f56ec4 319 proc_t p = current_proc();
6d2010ae 320 mach_vm_offset_t aslr_offset = 0;
39236c6e 321 mach_vm_offset_t dyld_aslr_offset = 0;
6d2010ae 322 kern_return_t kret;
b0d623f7 323
316670eb
A
324 if (macho_size > file_size) {
325 return(LOAD_BADMACHO);
326 }
327
b0d623f7
A
328 if (new_map == VM_MAP_NULL) {
329 create_map = TRUE;
b0d623f7 330 }
0b4e3aa0 331
b0d623f7
A
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;
0b4e3aa0 340 }
1c79356b 341
0b4e3aa0 342 if (create_map) {
fe8ab488
A
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,
3e170ce0 351 ((imgp->ip_flags & IMGPF_IS_64BIT) != 0));
6d2010ae 352 pal_switch_pmap(thread, pmap, imgp->ip_flags & IMGPF_IS_64BIT);
0b4e3aa0 353 map = vm_map_create(pmap,
0c530ab8 354 0,
3e170ce0 355 vm_compute_max_offset(((imgp->ip_flags & IMGPF_IS_64BIT) == IMGPF_IS_64BIT)),
0c530ab8 356 TRUE);
0b4e3aa0
A
357 } else
358 map = new_map;
0c530ab8 359
3e170ce0
A
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__ */
fe8ab488 364
7e4a7d39 365#ifndef CONFIG_ENFORCE_SIGNED_CODE
39236c6e
A
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) )
0c530ab8 372 vm_map_disable_NX(map);
7e4a7d39 373#endif
6d2010ae
A
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 /*
39236c6e 381 * Compute a random offset for ASLR, and an independent random offset for dyld.
6d2010ae
A
382 */
383 if (!(imgp->ip_flags & IMGPF_DISABLE_ASLR)) {
39236c6e
A
384 uint64_t max_slide_pages;
385
386 max_slide_pages = vm_map_get_max_aslr_slide_pages(map);
387
6d2010ae 388 aslr_offset = random();
39236c6e
A
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);
6d2010ae 395 }
7e4a7d39 396
1c79356b
A
397 if (!result)
398 result = &myresult;
399
91447636 400 *result = load_result_null;
1c79356b 401
2d21ac55 402 lret = parse_machfile(vp, map, thread, header, file_offset, macho_size,
39236c6e 403 0, (int64_t)aslr_offset, (int64_t)dyld_aslr_offset, result);
1c79356b
A
404
405 if (lret != LOAD_SUCCESS) {
55e303ae 406 if (create_map) {
0b4e3aa0 407 vm_map_deallocate(map); /* will lose pmap reference too */
55e303ae 408 }
1c79356b
A
409 return(lret);
410 }
55e303ae 411
fe8ab488
A
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
0c530ab8 420 /*
fe8ab488 421 * Check to see if the page zero is enforced by the map->min_offset.
0c530ab8 422 */
3e170ce0
A
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);
fe8ab488 430 }
316670eb 431 }
fe8ab488 432
490019cf 433 if (create_map) {
b0d623f7 434 /*
6d2010ae 435 * If this is an exec, then we are going to destroy the old
b0d623f7
A
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 */
490019cf 439 if (!spawn) {
b0d623f7
A
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).
13f56ec4
A
448 *
449 * NOTE: task_start_halt() makes sure that no new
450 * threads are created in the task during the transition.
316670eb
A
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) {
fe8ab488
A
461 vm_map_deallocate(map); /* will lose pmap reference too */
462 return (LOAD_FAILURE);
316670eb
A
463 }
464 proc_transcommit(p, 0);
465 workqueue_mark_exiting(p);
466 task_complete_halt(task);
467 workqueue_exit(p);
3e170ce0
A
468 kqueue_dealloc(p->p_wqkqueue);
469 p->p_wqkqueue = NULL;
316670eb 470 }
490019cf 471 *mapp = map;
0b4e3aa0 472 }
1c79356b
A
473 return(LOAD_SUCCESS);
474}
475
3e170ce0
A
476int macho_printf = 0;
477#define MACHO_PRINTF(args) \
478 do { \
479 if (macho_printf) { \
480 printf args; \
481 } \
482 } while (0)
483
91447636
A
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
6d2010ae
A
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.
91447636 494 */
1c79356b
A
495static
496load_return_t
497parse_machfile(
91447636 498 struct vnode *vp,
1c79356b 499 vm_map_t map,
2d21ac55 500 thread_t thread,
1c79356b 501 struct mach_header *header,
91447636
A
502 off_t file_offset,
503 off_t macho_size,
1c79356b 504 int depth,
6d2010ae 505 int64_t aslr_offset,
39236c6e 506 int64_t dyld_aslr_offset,
91447636 507 load_result_t *result
1c79356b
A
508)
509{
a3d08fcd 510 uint32_t ncmds;
91447636 511 struct load_command *lcp;
1c79356b 512 struct dylinker_command *dlp = 0;
91447636 513 integer_t dlarchbits = 0;
b0d623f7 514 void * control;
55e303ae 515 load_return_t ret = LOAD_SUCCESS;
91447636
A
516 caddr_t addr;
517 void * kl_addr;
1c79356b 518 vm_size_t size,kl_size;
a3d08fcd
A
519 size_t offset;
520 size_t oldoffset; /* for overflow check */
1c79356b 521 int pass;
2d21ac55 522 proc_t p = current_proc(); /* XXXX */
1c79356b 523 int error;
3e170ce0 524 int resid = 0;
91447636
A
525 size_t mach_header_sz = sizeof(struct mach_header);
526 boolean_t abi64;
2d21ac55 527 boolean_t got_code_signatures = FALSE;
6d2010ae 528 int64_t slide = 0;
91447636
A
529
530 if (header->magic == MH_MAGIC_64 ||
531 header->magic == MH_CIGAM_64) {
532 mach_header_sz = sizeof(struct mach_header_64);
533 }
1c79356b
A
534
535 /*
536 * Break infinite recursion
537 */
c18c124e 538 if (depth > 1) {
1c79356b 539 return(LOAD_FAILURE);
2d21ac55 540 }
0b4e3aa0 541
1c79356b
A
542 depth++;
543
544 /*
545 * Check to see if right machine type.
546 */
39236c6e 547 if (((cpu_type_t)(header->cputype & ~CPU_ARCH_MASK) != (cpu_type() & ~CPU_ARCH_MASK)) ||
2d21ac55
A
548 !grade_binary(header->cputype,
549 header->cpusubtype & ~CPU_SUBTYPE_MASK))
1c79356b
A
550 return(LOAD_BADARCH);
551
91447636
A
552 abi64 = ((header->cputype & CPU_ARCH_ABI64) == CPU_ARCH_ABI64);
553
1c79356b
A
554 switch (header->filetype) {
555
1c79356b 556 case MH_EXECUTE:
2d21ac55 557 if (depth != 1) {
1c79356b 558 return (LOAD_FAILURE);
2d21ac55 559 }
1c79356b 560
c18c124e 561 break;
1c79356b 562 case MH_DYLINKER:
2d21ac55 563 if (depth != 2) {
1c79356b 564 return (LOAD_FAILURE);
2d21ac55 565 }
1c79356b
A
566 break;
567
568 default:
569 return (LOAD_FAILURE);
570 }
571
572 /*
573 * Get the pager for the file.
574 */
b0d623f7 575 control = ubc_getobject(vp, UBC_FLAGS_NONE);
1c79356b
A
576
577 /*
578 * Map portion that must be accessible directly into
579 * kernel's map.
580 */
b0d623f7 581 if ((off_t)(mach_header_sz + header->sizeofcmds) > macho_size)
1c79356b
A
582 return(LOAD_BADMACHO);
583
584 /*
585 * Round size of Mach-O commands up to page boundry.
586 */
91447636 587 size = round_page(mach_header_sz + header->sizeofcmds);
1c79356b
A
588 if (size <= 0)
589 return(LOAD_BADMACHO);
590
591 /*
592 * Map the load commands into kernel memory.
593 */
594 addr = 0;
1c79356b
A
595 kl_size = size;
596 kl_addr = kalloc(size);
91447636 597 addr = (caddr_t)kl_addr;
0b4e3aa0 598 if (addr == NULL)
1c79356b 599 return(LOAD_NOSPACE);
0b4e3aa0 600
91447636 601 error = vn_rdwr(UIO_READ, vp, addr, size, file_offset,
b0d623f7 602 UIO_SYSSPACE, 0, kauth_cred_get(), &resid, p);
91447636 603 if (error) {
c18c124e 604 if (kl_addr)
0b4e3aa0 605 kfree(kl_addr, kl_size);
55e303ae 606 return(LOAD_IOERROR);
1c79356b 607 }
6d2010ae 608
c18c124e
A
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
6d2010ae
A
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
39236c6e
A
623 /*
624 * Scan through the commands, processing each one as necessary.
625 * We parse in three passes through the headers:
3e170ce0 626 * 0: determine if TEXT and DATA boundary can be page-aligned
39236c6e
A
627 * 1: thread state, uuid, code signature
628 * 2: segments
629 * 3: dyld, encryption, check entry point
1c79356b 630 */
39236c6e 631
3e170ce0
A
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 }
6d2010ae 640
6d2010ae
A
641 /*
642 * Check that the entry point is contained in an executable segments
643 */
c18c124e 644 if ((pass == 3) && (!result->using_lcmain && result->validentry == 0)) {
6d2010ae
A
645 thread_state_initialize(thread);
646 ret = LOAD_FAILURE;
647 break;
648 }
6d2010ae 649
a3d08fcd
A
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 */
91447636 656 offset = mach_header_sz;
1c79356b 657 ncmds = header->ncmds;
6d2010ae 658
1c79356b
A
659 while (ncmds--) {
660 /*
661 * Get a pointer to the command.
662 */
663 lcp = (struct load_command *)(addr + offset);
a3d08fcd 664 oldoffset = offset;
1c79356b
A
665 offset += lcp->cmdsize;
666
667 /*
a3d08fcd
A
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.
1c79356b 674 */
a3d08fcd
A
675 if (oldoffset > offset ||
676 lcp->cmdsize < sizeof(struct load_command) ||
91447636
A
677 offset > header->sizeofcmds + mach_header_sz) {
678 ret = LOAD_BADMACHO;
a3d08fcd 679 break;
1c79356b
A
680 }
681
682 /*
a3d08fcd
A
683 * Act on struct load_command's for which kernel
684 * intervention is required.
1c79356b
A
685 */
686 switch(lcp->cmd) {
687 case LC_SEGMENT:
3e170ce0
A
688 if (pass == 0) {
689 break;
690 }
691
39236c6e
A
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;
b0d623f7 714 case LC_SEGMENT_64:
6d2010ae 715 if (pass != 2)
1c79356b 716 break;
39236c6e
A
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
b0d623f7 727 ret = load_segment(lcp,
39236c6e
A
728 header->filetype,
729 control,
730 file_offset,
731 macho_size,
732 vp,
733 map,
734 slide,
735 result);
1c79356b 736 break;
1c79356b 737 case LC_UNIXTHREAD:
6d2010ae 738 if (pass != 1)
1c79356b
A
739 break;
740 ret = load_unixthread(
91447636 741 (struct thread_command *) lcp,
6d2010ae
A
742 thread,
743 slide,
1c79356b
A
744 result);
745 break;
316670eb
A
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;
1c79356b 757 case LC_LOAD_DYLINKER:
6d2010ae 758 if (pass != 3)
1c79356b 759 break;
91447636 760 if ((depth == 1) && (dlp == 0)) {
1c79356b 761 dlp = (struct dylinker_command *)lcp;
91447636
A
762 dlarchbits = (header->cputype & CPU_ARCH_MASK);
763 } else {
1c79356b 764 ret = LOAD_FAILURE;
91447636 765 }
1c79356b 766 break;
b0d623f7 767 case LC_UUID:
6d2010ae 768 if (pass == 1 && depth == 1) {
39236c6e
A
769 ret = load_uuid((struct uuid_command *) lcp,
770 (char *)addr + mach_header_sz + header->sizeofcmds,
771 result);
b0d623f7
A
772 }
773 break;
2d21ac55
A
774 case LC_CODE_SIGNATURE:
775 /* CODE SIGNING */
6d2010ae 776 if (pass != 1)
2d21ac55
A
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,
fe8ab488 788 result);
2d21ac55
A
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);
c18c124e
A
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
2d21ac55
A
799 } else {
800 got_code_signatures = TRUE;
801 }
04b8595b
A
802
803 if (got_code_signatures) {
c18c124e
A
804 unsigned tainted = CS_VALIDATE_TAINTED;
805 boolean_t valid = FALSE;
04b8595b
A
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) {
c18c124e
A
815 tainted = CS_VALIDATE_TAINTED;
816
04b8595b
A
817 valid = cs_validate_page(blobs,
818 NULL,
819 file_offset + off,
820 addr + off,
821 &tainted);
c18c124e 822 if (!valid || (tainted & CS_VALIDATE_TAINTED)) {
04b8595b
A
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
2d21ac55 836 break;
593a1d5f
A
837#if CONFIG_CODE_DECRYPTION
838 case LC_ENCRYPTION_INFO:
39236c6e 839 case LC_ENCRYPTION_INFO_64:
6d2010ae 840 if (pass != 3)
593a1d5f
A
841 break;
842 ret = set_code_unprotect(
843 (struct encryption_info_command *) lcp,
3e170ce0 844 addr, map, slide, vp, file_offset,
39236c6e 845 header->cputype, header->cpusubtype);
593a1d5f 846 if (ret != LOAD_SUCCESS) {
c910b4d9 847 printf("proc %d: set_code_unprotect() error %d "
593a1d5f
A
848 "for file \"%s\"\n",
849 p->p_pid, ret, vp->v_name);
39236c6e
A
850 /*
851 * Don't let the app run if it's
c910b4d9 852 * encrypted but we failed to set up the
39236c6e
A
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);
3e170ce0 861 }
c910b4d9 862 psignal(p, SIGKILL);
593a1d5f
A
863 }
864 break;
865#endif
1c79356b 866 default:
a3d08fcd
A
867 /* Other commands are ignored by the kernel */
868 ret = LOAD_SUCCESS;
91447636 869 break;
1c79356b
A
870 }
871 if (ret != LOAD_SUCCESS)
872 break;
873 }
874 if (ret != LOAD_SUCCESS)
875 break;
876 }
fe8ab488 877
91447636 878 if (ret == LOAD_SUCCESS) {
c18c124e
A
879 if (! got_code_signatures) {
880 if (cs_enforcement(NULL)) {
881 ret = LOAD_FAILURE;
882 } else {
3e170ce0
A
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 */
c18c124e
A
888 struct cs_blob *blob;
889
890 blob = ubc_cs_blob_get(vp, -1, file_offset);
891 if (blob != NULL) {
3e170ce0
A
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 }
c18c124e 899 }
3e170ce0
A
900 /* get flags to be applied to the process */
901 result->csflags |= cs_flag_data;
fe8ab488
A
902 }
903 }
c18c124e 904 }
91447636 905
316670eb 906 /* Make sure if we need dyld, we got it */
3e170ce0 907 if (result->needs_dynlinker && !dlp) {
316670eb
A
908 ret = LOAD_FAILURE;
909 }
3e170ce0 910
c18c124e 911 if ((ret == LOAD_SUCCESS) && (dlp != 0)) {
fe8ab488 912 /*
c18c124e
A
913 * load the dylinker, and slide it by the independent DYLD ASLR
914 * offset regardless of the PIE-ness of the main binary.
915 */
fe8ab488 916 ret = load_dylinker(dlp, dlarchbits, map, thread, depth,
c18c124e 917 dyld_aslr_offset, result);
fe8ab488 918 }
c18c124e
A
919
920 if((ret == LOAD_SUCCESS) && (depth == 1)) {
316670eb
A
921 if (result->thread_count == 0) {
922 ret = LOAD_FAILURE;
923 }
91447636 924 }
1c79356b
A
925 }
926
0b4e3aa0
A
927 if (kl_addr )
928 kfree(kl_addr, kl_size);
929
1c79356b
A
930 return(ret);
931}
932
593a1d5f 933#if CONFIG_CODE_DECRYPTION
0c530ab8 934
3e170ce0 935#define APPLE_UNPROTECTED_HEADER_SIZE (3 * 4096)
0c530ab8
A
936
937static load_return_t
fe8ab488 938unprotect_dsmos_segment(
0c530ab8
A
939 uint64_t file_off,
940 uint64_t file_size,
b0d623f7
A
941 struct vnode *vp,
942 off_t macho_offset,
0c530ab8
A
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. */
593a1d5f
A
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;
b0d623f7
A
976#pragma unused(vp, macho_offset)
977 crypt_info.crypt_ops = (void *)0x2e69cf40;
3e170ce0
A
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 */
7e41aa88
A
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
0c530ab8
A
996 kr = vm_map_apple_protected(map,
997 map_addr,
593a1d5f 998 map_addr + map_size,
3e170ce0 999 crypto_backing_offset,
593a1d5f 1000 &crypt_info);
0c530ab8
A
1001 }
1002
1003 if (kr != KERN_SUCCESS) {
1004 return LOAD_FAILURE;
1005 }
1006 return LOAD_SUCCESS;
1007}
593a1d5f 1008#else /* CONFIG_CODE_DECRYPTION */
b0d623f7 1009static load_return_t
fe8ab488 1010unprotect_dsmos_segment(
b0d623f7
A
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)
1c79356b 1018{
b0d623f7 1019 return LOAD_SUCCESS;
1c79356b 1020}
b0d623f7 1021#endif /* CONFIG_CODE_DECRYPTION */
1c79356b 1022
3e170ce0
A
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 */
1043static kern_return_t
1044map_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;
1145done:
1146 assert(cur_end >= vm_start + (file_end - file_start));
1147 return LOAD_SUCCESS;
1148}
1149
1c79356b
A
1150static
1151load_return_t
b0d623f7 1152load_segment(
3e170ce0
A
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)
1c79356b 1162{
b0d623f7 1163 struct segment_command_64 segment_command, *scp;
91447636 1164 kern_return_t ret;
3e170ce0 1165 vm_map_size_t delta_size;
91447636
A
1166 vm_prot_t initprot;
1167 vm_prot_t maxprot;
b0d623f7
A
1168 size_t segment_command_size, total_section_size,
1169 single_section_size;
3e170ce0
A
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;
b0d623f7
A
1183 if (LC_SEGMENT_64 == lcp->cmd) {
1184 segment_command_size = sizeof(struct segment_command_64);
1185 single_section_size = sizeof(struct section_64);
b0d623f7
A
1186 } else {
1187 segment_command_size = sizeof(struct segment_command);
1188 single_section_size = sizeof(struct section);
b0d623f7
A
1189 }
1190 if (lcp->cmdsize < segment_command_size)
1191 return (LOAD_BADMACHO);
1192 total_section_size = lcp->cmdsize - segment_command_size;
1193
3e170ce0 1194 if (LC_SEGMENT_64 == lcp->cmd) {
6d2010ae 1195 scp = (struct segment_command_64 *)lcp;
3e170ce0 1196 } else {
6d2010ae
A
1197 scp = &segment_command;
1198 widen_segment_command((struct segment_command *)lcp, scp);
1199 }
1200
3e170ce0
A
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
91447636
A
1215 /*
1216 * Make sure what we get from the file is really ours (as specified
1217 * by macho_size).
1218 */
b0d623f7 1219 if (scp->fileoff + scp->filesize < scp->fileoff ||
3e170ce0 1220 scp->fileoff + scp->filesize > (uint64_t)macho_size) {
b0d623f7 1221 return (LOAD_BADMACHO);
3e170ce0 1222 }
b0d623f7
A
1223 /*
1224 * Ensure that the number of sections specified would fit
1225 * within the load command size.
1226 */
3e170ce0 1227 if (total_section_size / single_section_size < scp->nsects) {
91447636 1228 return (LOAD_BADMACHO);
3e170ce0 1229 }
2d21ac55
A
1230 /*
1231 * Make sure the segment is page-aligned in the file.
1232 */
3e170ce0
A
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 */
b0d623f7 1243 return (LOAD_BADMACHO);
3e170ce0 1244 }
91447636 1245
04b8595b
A
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
3e170ce0
A
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 &&
b0d623f7
A
1268 (scp->initprot & VM_PROT_ALL) == VM_PROT_NONE &&
1269 (scp->maxprot & VM_PROT_ALL) == VM_PROT_NONE) {
0c530ab8 1270 /*
6d2010ae
A
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.
0c530ab8 1275 */
fe8ab488
A
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 */
3e170ce0
A
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
fe8ab488
A
1300 if (ret != KERN_SUCCESS) {
1301 return (LOAD_FAILURE);
0c530ab8 1302 }
fe8ab488 1303 return (LOAD_SUCCESS);
3e170ce0 1304 } else {
0c530ab8
A
1305 }
1306
3e170ce0
A
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 }
6d2010ae 1317
3e170ce0
A
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;
6d2010ae
A
1322
1323 if (map == VM_MAP_NULL)
1324 return (LOAD_SUCCESS);
1325
3e170ce0 1326 if (vm_size > 0) {
b0d623f7
A
1327 initprot = (scp->initprot) & VM_PROT_ALL;
1328 maxprot = (scp->maxprot) & VM_PROT_ALL;
91447636
A
1329 /*
1330 * Map a copy of the file into the address space.
1331 */
3e170ce0
A
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));
39236c6e 1340 }
3e170ce0
A
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
91447636
A
1354 /*
1355 * If the file didn't end on a page boundary,
1356 * we need to zero the leftover.
1357 */
b0d623f7 1358 delta_size = map_size - scp->filesize;
91447636
A
1359 if (delta_size > 0) {
1360 mach_vm_offset_t tmp;
1361
3e170ce0
A
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) {
91447636 1364 return(LOAD_RESOURCE);
3e170ce0 1365 }
91447636 1366
b0d623f7 1367 if (copyout(tmp, map_addr + scp->filesize,
91447636 1368 delta_size)) {
b0d623f7 1369 (void) mach_vm_deallocate(
91447636 1370 kernel_map, tmp, delta_size);
2d21ac55 1371 return (LOAD_FAILURE);
91447636 1372 }
1c79356b 1373
b0d623f7 1374 (void) mach_vm_deallocate(kernel_map, tmp, delta_size);
91447636
A
1375 }
1376#endif /* FIXME */
1377 }
1c79356b 1378
91447636
A
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 */
3e170ce0
A
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 }
91447636 1389 if (delta_size > 0) {
3e170ce0
A
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) {
91447636 1408 return(LOAD_NOSPACE);
3e170ce0 1409 }
91447636 1410 }
1c79356b 1411
b0d623f7 1412 if ( (scp->fileoff == 0) && (scp->filesize != 0) )
3e170ce0 1413 result->mach_header = vm_offset;
0c530ab8 1414
b0d623f7 1415 if (scp->flags & SG_PROTECTED_VERSION_1) {
3e170ce0
A
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 }
0c530ab8
A
1426 } else {
1427 ret = LOAD_SUCCESS;
1428 }
3e170ce0
A
1429
1430 if (LOAD_SUCCESS == ret &&
1431 filetype == MH_DYLINKER &&
1432 result->all_image_info_addr == MACH_VM_MIN_ADDRESS) {
b0d623f7 1433 note_all_image_info_section(scp,
3e170ce0
A
1434 LC_SEGMENT_64 == lcp->cmd,
1435 single_section_size,
1436 ((const char *)lcp +
1437 segment_command_size),
1438 slide,
1439 result);
1440 }
1c79356b 1441
c18c124e 1442 if (result->entry_point != MACH_VM_MIN_ADDRESS) {
3e170ce0 1443 if ((result->entry_point >= vm_offset) && (result->entry_point < (vm_offset + vm_size))) {
c18c124e
A
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 }
0b4e3aa0 1452
6d2010ae 1453 return ret;
1c79356b
A
1454}
1455
39236c6e
A
1456static
1457load_return_t
1458load_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 */
316670eb 1469
39236c6e
A
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}
316670eb
A
1478
1479static
1480load_return_t
1481load_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) {
316670eb
A
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
c18c124e
A
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
316670eb
A
1524 /* kernel does *not* use entryoff from LC_MAIN. Dyld uses it. */
1525 result->needs_dynlinker = TRUE;
c18c124e 1526 result->using_lcmain = TRUE;
316670eb
A
1527 result->unixproc = TRUE;
1528 result->thread_count++;
1529
1530 return(LOAD_SUCCESS);
1531}
1532
1533
91447636
A
1534static
1535load_return_t
1536load_unixthread(
1537 struct thread_command *tcp,
1538 thread_t thread,
6d2010ae 1539 int64_t slide,
91447636
A
1540 load_result_t *result
1541)
1542{
1543 load_return_t ret;
1544 int customstack =0;
316670eb 1545 mach_vm_offset_t addr;
91447636 1546
b0d623f7
A
1547 if (tcp->cmdsize < sizeof(*tcp))
1548 return (LOAD_BADMACHO);
2d21ac55 1549 if (result->thread_count != 0) {
91447636 1550 return (LOAD_FAILURE);
2d21ac55 1551 }
6d2010ae
A
1552
1553 if (thread == THREAD_NULL)
1554 return (LOAD_SUCCESS);
91447636
A
1555
1556 ret = load_threadstack(thread,
b0d623f7 1557 (uint32_t *)(((vm_offset_t)tcp) +
91447636
A
1558 sizeof(struct thread_command)),
1559 tcp->cmdsize - sizeof(struct thread_command),
316670eb 1560 &addr,
91447636
A
1561 &customstack);
1562 if (ret != LOAD_SUCCESS)
1563 return(ret);
1564
316670eb
A
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 }
6d2010ae 1575
316670eb
A
1576 /* The stack slides down from the default location */
1577 result->user_stack = addr;
1578 result->user_stack -= slide;
6d2010ae 1579
91447636 1580 ret = load_threadentry(thread,
b0d623f7 1581 (uint32_t *)(((vm_offset_t)tcp) +
91447636
A
1582 sizeof(struct thread_command)),
1583 tcp->cmdsize - sizeof(struct thread_command),
316670eb 1584 &addr);
91447636
A
1585 if (ret != LOAD_SUCCESS)
1586 return(ret);
1587
c18c124e
A
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
316670eb 1593 result->entry_point = addr;
6d2010ae
A
1594 result->entry_point += slide;
1595
91447636 1596 ret = load_threadstate(thread,
490019cf
A
1597 (uint32_t *)(((vm_offset_t)tcp) + sizeof(struct thread_command)),
1598 tcp->cmdsize - sizeof(struct thread_command),
1599 result);
91447636
A
1600 if (ret != LOAD_SUCCESS)
1601 return (ret);
1602
1603 result->unixproc = TRUE;
1604 result->thread_count++;
1605
1606 return(LOAD_SUCCESS);
1607}
1608
1c79356b
A
1609static
1610load_return_t
1611load_threadstate(
1612 thread_t thread,
b0d623f7 1613 uint32_t *ts,
490019cf
A
1614 uint32_t total_size,
1615 load_result_t *result
1c79356b
A
1616)
1617{
b0d623f7 1618 uint32_t size;
1c79356b 1619 int flavor;
b0d623f7 1620 uint32_t thread_size;
490019cf
A
1621 uint32_t *local_ts = NULL;
1622 uint32_t local_ts_size = 0;
1623 int ret;
1c79356b 1624
490019cf 1625 (void)thread;
04b8595b 1626
04b8595b
A
1627 if (total_size > 0) {
1628 local_ts_size = total_size;
1629 local_ts = kalloc(local_ts_size);
1630 if (local_ts == NULL) {
490019cf 1631 return LOAD_FAILURE;
04b8595b
A
1632 }
1633 memcpy(local_ts, ts, local_ts_size);
1634 ts = local_ts;
1635 }
1636
1c79356b 1637 /*
490019cf
A
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.
1c79356b 1642 */
1c79356b
A
1643 while (total_size > 0) {
1644 flavor = *ts++;
1645 size = *ts++;
490019cf
A
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)) {
04b8595b 1650 ret = LOAD_BADMACHO;
490019cf 1651 goto bad;
2d21ac55 1652 }
490019cf 1653
b0d623f7 1654 ts += size; /* ts is a (uint32_t *) */
1c79356b 1655 }
04b8595b 1656
490019cf
A
1657 result->threadstate = local_ts;
1658 result->threadstate_sz = local_ts_size;
1659 return LOAD_SUCCESS;
1660
1661bad:
1662 if (local_ts) {
04b8595b 1663 kfree(local_ts, local_ts_size);
04b8595b
A
1664 }
1665 return ret;
1c79356b
A
1666}
1667
1668static
1669load_return_t
1670load_threadstack(
1671 thread_t thread,
b0d623f7
A
1672 uint32_t *ts,
1673 uint32_t total_size,
6d2010ae 1674 mach_vm_offset_t *user_stack,
0b4e3aa0 1675 int *customstack
1c79356b
A
1676)
1677{
1678 kern_return_t ret;
b0d623f7 1679 uint32_t size;
1c79356b 1680 int flavor;
b0d623f7 1681 uint32_t stack_size;
1c79356b 1682
1c79356b
A
1683 while (total_size > 0) {
1684 flavor = *ts++;
1685 size = *ts++;
b0d623f7
A
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);
91447636 1690 if (stack_size > total_size)
1c79356b 1691 return(LOAD_BADMACHO);
91447636
A
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);
2d21ac55 1700 if (ret != KERN_SUCCESS) {
1c79356b 1701 return(LOAD_FAILURE);
2d21ac55 1702 }
b0d623f7 1703 ts += size; /* ts is a (uint32_t *) */
1c79356b
A
1704 }
1705 return(LOAD_SUCCESS);
1706}
1707
1708static
1709load_return_t
1710load_threadentry(
1711 thread_t thread,
b0d623f7
A
1712 uint32_t *ts,
1713 uint32_t total_size,
91447636 1714 mach_vm_offset_t *entry_point
1c79356b
A
1715)
1716{
1717 kern_return_t ret;
b0d623f7 1718 uint32_t size;
1c79356b 1719 int flavor;
b0d623f7 1720 uint32_t entry_size;
1c79356b
A
1721
1722 /*
1723 * Set the thread state.
1724 */
91447636 1725 *entry_point = MACH_VM_MIN_ADDRESS;
1c79356b
A
1726 while (total_size > 0) {
1727 flavor = *ts++;
1728 size = *ts++;
b0d623f7
A
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);
91447636 1733 if (entry_size > total_size)
1c79356b 1734 return(LOAD_BADMACHO);
91447636
A
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);
2d21ac55 1742 if (ret != KERN_SUCCESS) {
1c79356b 1743 return(LOAD_FAILURE);
2d21ac55 1744 }
b0d623f7 1745 ts += size; /* ts is a (uint32_t *) */
1c79356b
A
1746 }
1747 return(LOAD_SUCCESS);
1748}
1749
6d2010ae
A
1750struct 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};
1c79356b 1758
c18c124e
A
1759#define DEFAULT_DYLD_PATH "/usr/lib/dyld"
1760
6d2010ae 1761static load_return_t
1c79356b
A
1762load_dylinker(
1763 struct dylinker_command *lcp,
91447636 1764 integer_t archbits,
1c79356b 1765 vm_map_t map,
2d21ac55 1766 thread_t thread,
1c79356b 1767 int depth,
6d2010ae
A
1768 int64_t slide,
1769 load_result_t *result
1c79356b
A
1770)
1771{
1772 char *name;
1773 char *p;
2d21ac55 1774 struct vnode *vp = NULLVP; /* set by get_macho_vnode() */
6d2010ae 1775 struct mach_header *header;
2d21ac55
A
1776 off_t file_offset = 0; /* set by get_macho_vnode() */
1777 off_t macho_size = 0; /* set by get_macho_vnode() */
6d2010ae 1778 load_result_t *myresult;
1c79356b 1779 kern_return_t ret;
6d2010ae
A
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;
1c79356b 1786
b0d623f7
A
1787 if (lcp->cmdsize < sizeof(*lcp))
1788 return (LOAD_BADMACHO);
1789
1c79356b
A
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
c18c124e
A
1800#if !(DEVELOPMENT || DEBUG)
1801 if (0 != strcmp(name, DEFAULT_DYLD_PATH)) {
1802 return (LOAD_BADMACHO);
1803 }
1804#endif
1805
6d2010ae
A
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);
1c79356b 1815 if (ret)
6d2010ae
A
1816 goto novp_out;
1817
1818 *myresult = load_result_null;
1c79356b 1819
2d21ac55
A
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 */
1c79356b 1825
6d2010ae 1826 ret = parse_machfile(vp, map, thread, header, file_offset,
39236c6e 1827 macho_size, depth, slide, 0, myresult);
1c79356b 1828
2d21ac55
A
1829 /*
1830 * If it turned out something was in the way, then we'll take
6d2010ae
A
1831 * take this longer path to preflight dyld's vm ranges, then
1832 * map it at a free location in the address space.
2d21ac55 1833 */
1c79356b 1834
2d21ac55 1835 if (ret == LOAD_NOSPACE) {
6d2010ae
A
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;
1c79356b 1841
2d21ac55 1842 /*
6d2010ae
A
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"
2d21ac55 1846 */
6d2010ae 1847 ret = parse_machfile(vp, VM_MAP_NULL, THREAD_NULL, header,
39236c6e
A
1848 file_offset, macho_size, depth,
1849 0 /* slide */, 0, myresult);
6d2010ae
A
1850
1851 if (ret != LOAD_SUCCESS) {
1c79356b 1852 goto out;
1c79356b 1853 }
2d21ac55 1854
6d2010ae
A
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;
1c79356b
A
1865 goto out;
1866 }
1867
6d2010ae
A
1868 ret = mach_vm_deallocate(map, map_addr, dyl_length);
1869 if (ret != KERN_SUCCESS) {
1870 ret = LOAD_NOSPACE;
1871 goto out;
91447636 1872 }
6d2010ae
A
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;
1c79356b 1880
6d2010ae
A
1881 *myresult = load_result_null;
1882
1883 ret = parse_machfile(vp, map, thread, header,
39236c6e
A
1884 file_offset, macho_size, depth,
1885 slide_amount, 0, myresult);
6d2010ae
A
1886
1887 if (ret) {
1888 goto out;
1889 }
2d21ac55 1890 }
6d2010ae 1891
490019cf
A
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
1c79356b 1900 result->dynlinker = TRUE;
6d2010ae 1901 result->entry_point = myresult->entry_point;
316670eb 1902 result->validentry = myresult->validentry;
6d2010ae
A
1903 result->all_image_info_addr = myresult->all_image_info_addr;
1904 result->all_image_info_size = myresult->all_image_info_size;
fe8ab488
A
1905 if (myresult->platform_binary) {
1906 result->csflags |= CS_DYLD_PLATFORM;
1907 }
1c79356b
A
1908 }
1909out:
91447636 1910 vnode_put(vp);
6d2010ae
A
1911novp_out:
1912 FREE(dyld_data, M_TEMP);
1c79356b
A
1913 return (ret);
1914
1915}
1916
6d2010ae 1917static load_return_t
2d21ac55
A
1918load_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;
593a1d5f 1932 vm_size_t blob_size;
2d21ac55
A
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
04b8595b 1943 blob = ubc_cs_blob_get(vp, cputype, macho_offset);
fe8ab488
A
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)) {
c18c124e 1951 if (0 != ubc_cs_blob_revalidate(vp, blob, 0)) {
fe8ab488
A
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 }
2d21ac55
A
1961 goto out;
1962 }
1963
593a1d5f
A
1964 blob_size = lcp->datasize;
1965 kr = ubc_cs_blob_allocate(&addr, &blob_size);
2d21ac55
A
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,
b0d623f7 1977 UIO_SYSSPACE,
2d21ac55
A
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,
3e170ce0
A
1991 lcp->datasize,
1992 0,
1993 &blob)) {
2d21ac55
A
1994 ret = LOAD_FAILURE;
1995 goto out;
1996 } else {
1997 /* ubc_cs_blob_add() has consumed "addr" */
1998 addr = 0;
1999 }
6d2010ae
A
2000
2001#if CHECK_CS_VALIDATION_BITMAP
2002 ubc_cs_validation_bitmap_allocate( vp );
2003#endif
2d21ac55 2004
2d21ac55
A
2005 ret = LOAD_SUCCESS;
2006out:
fe8ab488 2007 if (ret == LOAD_SUCCESS) {
3e170ce0
A
2008 if (blob == NULL)
2009 panic("sucess, but no blob!");
2010
2d21ac55 2011 result->csflags |= blob->csb_flags;
fe8ab488 2012 result->platform_binary = blob->csb_platform_binary;
04b8595b 2013 result->cs_end_offset = blob->csb_end_offset;
2d21ac55
A
2014 }
2015 if (addr != 0) {
593a1d5f 2016 ubc_cs_blob_deallocate(addr, blob_size);
2d21ac55
A
2017 addr = 0;
2018 }
2019
2020 return ret;
2021}
2022
593a1d5f
A
2023
2024#if CONFIG_CODE_DECRYPTION
2025
2026static load_return_t
2027set_code_unprotect(
3e170ce0
A
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)
593a1d5f 2036{
3e170ce0 2037 int error, len;
593a1d5f
A
2038 pager_crypt_info_t crypt_info;
2039 const char * cryptname = 0;
6d2010ae 2040 char *vpath;
593a1d5f
A
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;
3e170ce0 2046 vm_object_offset_t crypto_backing_offset;
593a1d5f 2047 kern_return_t kr;
b0d623f7 2048
6d2010ae 2049 if (eip->cmdsize < sizeof(*eip)) return LOAD_BADMACHO;
593a1d5f
A
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:
c910b4d9 2064 return LOAD_BADMACHO;
593a1d5f
A
2065 }
2066
6d2010ae
A
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
593a1d5f 2073 len = MAXPATHLEN;
3e170ce0
A
2074 error = vn_getpath(vp, vpath, &len);
2075 if (error) {
6d2010ae
A
2076 FREE_ZONE(vpath, MAXPATHLEN, M_NAMEI);
2077 return LOAD_FAILURE;
2078 }
593a1d5f
A
2079
2080 /* set up decrypter first */
39236c6e
A
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);
3e170ce0
A
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 */
6d2010ae 2092 FREE_ZONE(vpath, MAXPATHLEN, M_NAMEI);
593a1d5f
A
2093
2094 if(kr) {
c910b4d9 2095 printf("set_code_unprotect: unable to create decrypter %s, kr=%d\n",
593a1d5f 2096 cryptname, kr);
39236c6e
A
2097 if (kr == kIOReturnNotPrivileged) {
2098 /* text encryption returned decryption failure */
2099 return(LOAD_DECRYPTFAIL);
2100 }else
2101 return LOAD_RESOURCE;
593a1d5f
A
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)) {
316670eb 2128 map_offset = seg64->vmaddr + eip->cryptoff - seg64->fileoff + slide;
593a1d5f 2129 map_size = eip->cryptsize;
3e170ce0 2130 crypto_backing_offset = macho_offset + eip->cryptoff;
593a1d5f
A
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)) {
316670eb 2138 map_offset = seg32->vmaddr + eip->cryptoff - seg32->fileoff + slide;
593a1d5f 2139 map_size = eip->cryptsize;
3e170ce0 2140 crypto_backing_offset = macho_offset + eip->cryptoff;
593a1d5f
A
2141 goto remap_now;
2142 }
2143 }
2144 }
2145
2146 /* if we get here, did not find anything */
c910b4d9 2147 return LOAD_BADMACHO;
593a1d5f
A
2148
2149remap_now:
2150 /* now remap using the decrypter */
3e170ce0
A
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) {
c910b4d9 2160 printf("set_code_unprotect(): mapping failed with %x\n", kr);
c910b4d9
A
2161 return LOAD_PROTECT;
2162 }
593a1d5f
A
2163
2164 return LOAD_SUCCESS;
2165}
2166
2167#endif
2168
91447636
A
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 */
1c79356b
A
2175static
2176load_return_t
2177get_macho_vnode(
2178 char *path,
91447636 2179 integer_t archbits,
1c79356b 2180 struct mach_header *mach_header,
91447636
A
2181 off_t *file_offset,
2182 off_t *macho_size,
6d2010ae 2183 struct macho_data *data,
1c79356b
A
2184 struct vnode **vpp
2185)
2186{
2187 struct vnode *vp;
2d21ac55
A
2188 vfs_context_t ctx = vfs_context_current();
2189 proc_t p = vfs_context_proc(ctx);
2190 kauth_cred_t kerncred;
6d2010ae 2191 struct nameidata *ndp = &data->__nid;
1c79356b
A
2192 boolean_t is_fat;
2193 struct fat_arch fat_arch;
6d2010ae 2194 int error;
1c79356b 2195 int resid;
6d2010ae 2196 union macho_vnode_header *header = &data->__header;
0b4e3aa0 2197 off_t fsize = (off_t)0;
6d2010ae 2198
2d21ac55
A
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());
91447636 2208
1c79356b 2209 /* init the namei data to point the file user's program name */
6d2010ae 2210 NDINIT(ndp, LOOKUP, OP_OPEN, FOLLOW | LOCKLEAF, UIO_SYSSPACE, CAST_USER_ADDR_T(path), ctx);
1c79356b 2211
91447636 2212 if ((error = namei(ndp)) != 0) {
2d21ac55 2213 if (error == ENOENT) {
55e303ae 2214 error = LOAD_ENOENT;
2d21ac55 2215 } else {
55e303ae 2216 error = LOAD_FAILURE;
2d21ac55 2217 }
1c79356b 2218 return(error);
55e303ae 2219 }
91447636 2220 nameidone(ndp);
1c79356b 2221 vp = ndp->ni_vp;
6d2010ae 2222
1c79356b
A
2223 /* check for regular file */
2224 if (vp->v_type != VREG) {
55e303ae 2225 error = LOAD_PROTECT;
1c79356b
A
2226 goto bad1;
2227 }
2228
91447636 2229 /* get size */
2d21ac55 2230 if ((error = vnode_size(vp, &fsize, ctx)) != 0) {
55e303ae 2231 error = LOAD_FAILURE;
1c79356b 2232 goto bad1;
55e303ae 2233 }
1c79356b
A
2234
2235 /* Check mount point */
2236 if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
55e303ae 2237 error = LOAD_PROTECT;
1c79356b
A
2238 goto bad1;
2239 }
2240
91447636 2241 /* check access */
39236c6e 2242 if ((error = vnode_authorize(vp, NULL, KAUTH_VNODE_EXECUTE | KAUTH_VNODE_READ_DATA, ctx)) != 0) {
55e303ae 2243 error = LOAD_PROTECT;
1c79356b 2244 goto bad1;
55e303ae 2245 }
0b4e3aa0 2246
1c79356b 2247 /* try to open it */
2d21ac55 2248 if ((error = VNOP_OPEN(vp, FREAD, ctx)) != 0) {
55e303ae 2249 error = LOAD_PROTECT;
1c79356b 2250 goto bad1;
0b4e3aa0
A
2251 }
2252
6d2010ae 2253 if ((error = vn_rdwr(UIO_READ, vp, (caddr_t)header, sizeof (*header), 0,
b0d623f7 2254 UIO_SYSSPACE, IO_NODELOCKED, kerncred, &resid, p)) != 0) {
55e303ae 2255 error = LOAD_IOERROR;
1c79356b 2256 goto bad2;
55e303ae 2257 }
6d2010ae 2258
c18c124e
A
2259 if (resid) {
2260 error = LOAD_BADMACHO;
2261 goto bad2;
2262 }
2263
6d2010ae
A
2264 if (header->mach_header.magic == MH_MAGIC ||
2265 header->mach_header.magic == MH_MAGIC_64) {
2266 is_fat = FALSE;
c18c124e
A
2267 } else if (OSSwapBigToHostInt32(header->fat_header.magic) == FAT_MAGIC) {
2268 is_fat = TRUE;
6d2010ae
A
2269 } else {
2270 error = LOAD_BADMACHO;
2271 goto bad2;
1c79356b
A
2272 }
2273
2274 if (is_fat) {
c18c124e
A
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
0b4e3aa0 2282 /* Look up our architecture in the fat file. */
c18c124e
A
2283 error = fatfile_getarch_with_bits(archbits,
2284 (vm_offset_t)(&header->fat_header), sizeof(*header), &fat_arch);
0b4e3aa0 2285 if (error != LOAD_SUCCESS)
1c79356b 2286 goto bad2;
0b4e3aa0
A
2287
2288 /* Read the Mach-O header out of it */
6d2010ae
A
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);
1c79356b 2292 if (error) {
55e303ae 2293 error = LOAD_IOERROR;
1c79356b
A
2294 goto bad2;
2295 }
2296
c18c124e
A
2297 if (resid) {
2298 error = LOAD_BADMACHO;
2299 goto bad2;
2300 }
2301
0b4e3aa0 2302 /* Is this really a Mach-O? */
6d2010ae
A
2303 if (header->mach_header.magic != MH_MAGIC &&
2304 header->mach_header.magic != MH_MAGIC_64) {
1c79356b
A
2305 error = LOAD_BADMACHO;
2306 goto bad2;
2307 }
0b4e3aa0 2308
1c79356b 2309 *file_offset = fat_arch.offset;
2d21ac55 2310 *macho_size = fat_arch.size;
1c79356b 2311 } else {
91447636
A
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 */
6d2010ae
A
2322 if ((cpu_type_t)(header->mach_header.cputype & CPU_ARCH_MASK) != archbits) {
2323 error = LOAD_BADARCH;
2324 goto bad2;
2325 }
0b4e3aa0 2326
1c79356b 2327 *file_offset = 0;
91447636 2328 *macho_size = fsize;
1c79356b
A
2329 }
2330
6d2010ae 2331 *mach_header = header->mach_header;
0b4e3aa0 2332 *vpp = vp;
91447636
A
2333
2334 ubc_setsize(vp, fsize);
0b4e3aa0
A
2335 return (error);
2336
1c79356b 2337bad2:
6d2010ae 2338 (void) VNOP_CLOSE(vp, FREAD, ctx);
1c79356b 2339bad1:
91447636 2340 vnode_put(vp);
1c79356b
A
2341 return(error);
2342}