xref: /xnu-11417.121.6/bsd/kern/mach_loader.c (revision a1e26a70f38d1d7daa7b49b258e2f8538ad81650) !
1 /*
2  * Copyright (c) 2000-2020 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/file_internal.h>
51 #include <sys/ubc_internal.h>
52 #include <sys/imgact.h>
53 #include <sys/codesign.h>
54 #include <sys/proc_uuid_policy.h>
55 #include <sys/reason.h>
56 #include <sys/kdebug.h>
57 #include <sys/spawn_internal.h>
58 
59 #include <mach/mach_types.h>
60 #include <mach/vm_map.h>        /* vm_allocate() */
61 #include <mach/mach_vm.h>       /* mach_vm_allocate() */
62 #include <mach/vm_statistics.h>
63 #include <mach/task.h>
64 #include <mach/thread_act.h>
65 
66 #include <machine/vmparam.h>
67 #include <machine/exec.h>
68 #include <machine/pal_routines.h>
69 
70 #include <kern/ast.h>
71 #include <kern/kern_types.h>
72 #include <kern/cpu_number.h>
73 #include <kern/mach_loader.h>
74 #include <kern/mach_fat.h>
75 #include <kern/kalloc.h>
76 #include <kern/task.h>
77 #include <kern/thread.h>
78 #include <kern/page_decrypt.h>
79 
80 #include <mach-o/fat.h>
81 #include <mach-o/loader.h>
82 
83 #include <vm/pmap.h>
84 #include <vm/vm_map_xnu.h>
85 #include <vm/vm_kern_xnu.h>
86 #include <vm/vm_pager_xnu.h>
87 #include <vm/vnode_pager.h>
88 #include <vm/vm_protos.h>
89 #include <vm/vm_shared_region.h>
90 #include <IOKit/IOReturn.h>     /* for kIOReturnNotPrivileged */
91 #include <IOKit/IOBSD.h>        /* for IOVnodeHasEntitlement */
92 
93 #include <os/log.h>
94 #include <os/overflow.h>
95 
96 #include "kern_exec_internal.h"
97 
98 
99 
100 /* An empty load_result_t */
101 static const 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 	.user_stack_alloc = MACH_VM_MIN_ADDRESS,
107 	.user_stack_alloc_size = 0,
108 	.all_image_info_addr = MACH_VM_MIN_ADDRESS,
109 	.all_image_info_size = 0,
110 	.thread_count = 0,
111 	.unixproc = 0,
112 	.dynlinker = 0,
113 	.needs_dynlinker = 0,
114 	.validentry = 0,
115 	.using_lcmain = 0,
116 	.is_64bit_addr = 0,
117 	.is_64bit_data = 0,
118 	.custom_stack = 0,
119 	.csflags = 0,
120 	.has_pagezero = 0,
121 	.uuid = { 0 },
122 	.min_vm_addr = MACH_VM_MAX_ADDRESS,
123 	.max_vm_addr = MACH_VM_MIN_ADDRESS,
124 	.ro_vm_start = MACH_VM_MIN_ADDRESS,
125 	.ro_vm_end = MACH_VM_MIN_ADDRESS,
126 	.cs_end_offset = 0,
127 	.threadstate = NULL,
128 	.threadstate_sz = 0,
129 	.is_rosetta = 0,
130 	.dynlinker_ro_vm_start = 0,
131 	.dynlinker_ro_vm_end = 0,
132 	.dynlinker_mach_header = MACH_VM_MIN_ADDRESS,
133 	.dynlinker_fd = -1,
134 };
135 
136 /*
137  * Prototypes of static functions.
138  */
139 static load_return_t
140 parse_machfile(
141 	struct vnode            *vp,
142 	vm_map_t                map,
143 	thread_t                thread,
144 	struct mach_header      *header,
145 	off_t                   file_offset,
146 	off_t                   macho_size,
147 	int                     depth,
148 	int64_t                 slide,
149 	int64_t                 dyld_slide,
150 	load_result_t           *result,
151 	load_result_t           *binresult,
152 	struct image_params     *imgp
153 	);
154 
155 static load_return_t
156 load_segment(
157 	struct load_command             *lcp,
158 	uint32_t                        filetype,
159 	void                            *control,
160 	off_t                           pager_offset,
161 	off_t                           macho_size,
162 	struct vnode                    *vp,
163 	vm_map_t                        map,
164 	int64_t                         slide,
165 	load_result_t                   *result,
166 	struct image_params             *imgp
167 	);
168 
169 static load_return_t
170 load_uuid(
171 	struct uuid_command             *uulp,
172 	char                            *command_end,
173 	load_result_t                   *result
174 	);
175 
176 static load_return_t
177 load_version(
178 	struct version_min_command     *vmc,
179 	boolean_t               *found_version_cmd,
180 	struct image_params             *imgp,
181 	load_result_t           *result
182 	);
183 
184 static load_return_t
185 load_code_signature(
186 	struct linkedit_data_command    *lcp,
187 	struct vnode                    *vp,
188 	off_t                           macho_offset,
189 	off_t                           macho_size,
190 	cpu_type_t                      cputype,
191 	cpu_subtype_t                   cpusubtype,
192 	load_result_t                   *result,
193 	struct image_params             *imgp);
194 
195 #if CONFIG_CODE_DECRYPTION
196 static load_return_t
197 set_code_unprotect(
198 	struct encryption_info_command  *lcp,
199 	caddr_t                         addr,
200 	vm_map_t                        map,
201 	int64_t                         slide,
202 	struct vnode                    *vp,
203 	off_t                           macho_offset,
204 	cpu_type_t                      cputype,
205 	cpu_subtype_t                   cpusubtype);
206 #endif
207 
208 static
209 load_return_t
210 load_main(
211 	struct entry_point_command      *epc,
212 	thread_t                thread,
213 	int64_t                         slide,
214 	load_result_t           *result
215 	);
216 
217 static
218 load_return_t
219 setup_driver_main(
220 	thread_t                thread,
221 	int64_t                         slide,
222 	load_result_t           *result
223 	);
224 
225 static load_return_t
226 load_unixthread(
227 	struct thread_command   *tcp,
228 	thread_t                        thread,
229 	int64_t                         slide,
230 	boolean_t                       is_x86_64_compat_binary,
231 	load_result_t                   *result
232 	);
233 
234 static load_return_t
235 load_threadstate(
236 	thread_t                thread,
237 	uint32_t        *ts,
238 	uint32_t        total_size,
239 	load_result_t *
240 	);
241 
242 static load_return_t
243 load_threadstack(
244 	thread_t                thread,
245 	uint32_t                *ts,
246 	uint32_t                total_size,
247 	mach_vm_offset_t        *user_stack,
248 	int                     *customstack,
249 	boolean_t               is_x86_64_compat_binary,
250 	load_result_t           *result
251 	);
252 
253 static load_return_t
254 load_threadentry(
255 	thread_t                thread,
256 	uint32_t        *ts,
257 	uint32_t        total_size,
258 	mach_vm_offset_t        *entry_point
259 	);
260 
261 static load_return_t
262 load_dylinker(
263 	struct dylinker_command *lcp,
264 	integer_t               archbits,
265 	vm_map_t                map,
266 	thread_t                thread,
267 	int                     depth,
268 	int64_t                 slide,
269 	load_result_t           *result,
270 	struct image_params     *imgp
271 	);
272 
273 
274 #if CONFIG_ROSETTA
275 static load_return_t
276 load_rosetta(
277 	vm_map_t                        map,
278 	thread_t                        thread,
279 	load_result_t           *result,
280 	struct image_params     *imgp
281 	);
282 #endif
283 
284 #if __x86_64__
285 extern int bootarg_no32exec;
286 static boolean_t
287 check_if_simulator_binary(
288 	struct image_params     *imgp,
289 	off_t                   file_offset,
290 	off_t                   macho_size);
291 #endif
292 
293 struct macho_data;
294 
295 static load_return_t
296 get_macho_vnode(
297 	const char                      *path,
298 	integer_t               archbits,
299 	struct mach_header      *mach_header,
300 	off_t                   *file_offset,
301 	off_t                   *macho_size,
302 	struct macho_data       *macho_data,
303 	struct vnode            **vpp,
304 	struct image_params     *imgp
305 	);
306 
307 static inline void
widen_segment_command(const struct segment_command * scp32,struct segment_command_64 * scp)308 widen_segment_command(const struct segment_command *scp32,
309     struct segment_command_64 *scp)
310 {
311 	scp->cmd = scp32->cmd;
312 	scp->cmdsize = scp32->cmdsize;
313 	bcopy(scp32->segname, scp->segname, sizeof(scp->segname));
314 	scp->vmaddr = scp32->vmaddr;
315 	scp->vmsize = scp32->vmsize;
316 	scp->fileoff = scp32->fileoff;
317 	scp->filesize = scp32->filesize;
318 	scp->maxprot = scp32->maxprot;
319 	scp->initprot = scp32->initprot;
320 	scp->nsects = scp32->nsects;
321 	scp->flags = scp32->flags;
322 }
323 
324 static void
note_all_image_info_section(const struct segment_command_64 * scp,boolean_t is64,size_t section_size,const void * sections,int64_t slide,load_result_t * result)325 note_all_image_info_section(const struct segment_command_64 *scp,
326     boolean_t is64, size_t section_size, const void *sections,
327     int64_t slide, load_result_t *result)
328 {
329 	const union {
330 		struct section s32;
331 		struct section_64 s64;
332 	} *sectionp;
333 	unsigned int i;
334 
335 
336 	if (strncmp(scp->segname, "__DATA_DIRTY", sizeof(scp->segname)) != 0 &&
337 	    strncmp(scp->segname, "__DATA", sizeof(scp->segname)) != 0) {
338 		return;
339 	}
340 	for (i = 0; i < scp->nsects; ++i) {
341 		sectionp = (const void *)
342 		    ((const char *)sections + section_size * i);
343 		if (0 == strncmp(sectionp->s64.sectname, "__all_image_info",
344 		    sizeof(sectionp->s64.sectname))) {
345 			result->all_image_info_addr =
346 			    is64 ? sectionp->s64.addr : sectionp->s32.addr;
347 			result->all_image_info_addr += slide;
348 			result->all_image_info_size =
349 			    is64 ? sectionp->s64.size : sectionp->s32.size;
350 			return;
351 		}
352 	}
353 }
354 
355 #if __arm64__
356 /*
357  * Allow bypassing some security rules (hard pagezero, no write+execute)
358  * in exchange for better binary compatibility for legacy apps built
359  * before 16KB-alignment was enforced.
360  */
361 const int fourk_binary_compatibility_unsafe = TRUE;
362 #endif /* __arm64__ */
363 
364 #if XNU_TARGET_OS_OSX
365 
366 /* Determines whether this process may host/run third party plugins. */
367 static inline bool
process_is_plugin_host(struct image_params * imgp,load_result_t * result)368 process_is_plugin_host(struct image_params *imgp, load_result_t *result)
369 {
370 	if (imgp->ip_flags & IMGPF_NOJOP) {
371 		return false;
372 	}
373 
374 	if (!result->platform_binary) {
375 		return false;
376 	}
377 
378 	struct cs_blob *csblob = csvnode_get_blob(imgp->ip_vp, imgp->ip_arch_offset);
379 	const char *identity = csblob_get_identity(csblob);
380 	if (!identity) {
381 		return false;
382 	}
383 
384 	/* Check if override host plugin entitlement is present and posix spawn attribute to disable A keys is passed */
385 	if (IOVnodeHasEntitlement(imgp->ip_vp, (int64_t)imgp->ip_arch_offset, OVERRIDE_PLUGIN_HOST_ENTITLEMENT)) {
386 		bool ret = imgp->ip_flags & IMGPF_PLUGIN_HOST_DISABLE_A_KEYS;
387 		if (ret) {
388 			proc_t p = vfs_context_proc(imgp->ip_vfs_context);
389 			set_proc_name(imgp, p);
390 			os_log(OS_LOG_DEFAULT, "%s: running binary \"%s\" in keys-off mode due to posix_spawnattr_disable_ptr_auth_a_keys_np", __func__, p->p_name);
391 		}
392 		return ret;
393 	}
394 
395 	/* Disabling library validation is a good signal that this process plans to host plugins */
396 	const char *const disable_lv_entitlements[] = {
397 		"com.apple.security.cs.disable-library-validation",
398 		"com.apple.private.cs.automator-plugins",
399 		CLEAR_LV_ENTITLEMENT,
400 	};
401 	for (size_t i = 0; i < ARRAY_COUNT(disable_lv_entitlements); i++) {
402 		const char *entitlement = disable_lv_entitlements[i];
403 		if (IOVnodeHasEntitlement(imgp->ip_vp, (int64_t)imgp->ip_arch_offset, entitlement)) {
404 			proc_t p = vfs_context_proc(imgp->ip_vfs_context);
405 			set_proc_name(imgp, p);
406 			os_log(OS_LOG_DEFAULT, "%s: running binary \"%s\" in keys-off mode due to entitlement: %s", __func__, p->p_name, entitlement);
407 			return true;
408 		}
409 	}
410 
411 	/* From /System/Library/Security/HardeningExceptions.plist */
412 	const char *const hardening_exceptions[] = {
413 		"com.apple.perl5", /* Scripting engines may load third party code and jit*/
414 		"com.apple.perl", /* Scripting engines may load third party code and jit*/
415 		"org.python.python", /* Scripting engines may load third party code and jit*/
416 		"com.apple.expect", /* Scripting engines may load third party code and jit*/
417 		"com.tcltk.wish", /* Scripting engines may load third party code and jit*/
418 		"com.tcltk.tclsh", /* Scripting engines may load third party code and jit*/
419 		"com.apple.ruby", /* Scripting engines may load third party code and jit*/
420 		"com.apple.bash", /* Required for the 'enable' command */
421 		"com.apple.zsh", /* Required for the 'zmodload' command */
422 		"com.apple.ksh", /* Required for 'builtin' command */
423 	};
424 	for (size_t i = 0; i < ARRAY_COUNT(hardening_exceptions); i++) {
425 		if (strncmp(hardening_exceptions[i], identity, strlen(hardening_exceptions[i])) == 0) {
426 			proc_t p = vfs_context_proc(imgp->ip_vfs_context);
427 			set_proc_name(imgp, p);
428 			os_log(OS_LOG_DEFAULT, "%s: running binary \"%s\" in keys-off mode due to identity: %s", __func__, p->p_name, identity);
429 			return true;
430 		}
431 	}
432 
433 	return false;
434 }
435 #endif /* XNU_TARGET_OS_OSX */
436 
437 load_return_t
load_machfile(struct image_params * imgp,struct mach_header * header,thread_t thread,vm_map_t * mapp,load_result_t * result)438 load_machfile(
439 	struct image_params     *imgp,
440 	struct mach_header      *header,
441 	thread_t                thread,
442 	vm_map_t                *mapp,
443 	load_result_t           *result
444 	)
445 {
446 	struct vnode            *vp = imgp->ip_vp;
447 	off_t                   file_offset = imgp->ip_arch_offset;
448 	off_t                   macho_size = imgp->ip_arch_size;
449 	off_t                   total_size = 0;
450 	off_t                   file_size = imgp->ip_vattr->va_data_size;
451 	pmap_t                  pmap = 0;       /* protected by create_map */
452 	vm_map_t                map;
453 	load_result_t           myresult;
454 	load_return_t           lret;
455 	boolean_t enforce_hard_pagezero = TRUE;
456 	int in_exec = (imgp->ip_flags & IMGPF_EXEC);
457 	task_t task = current_task();
458 	int64_t                 aslr_page_offset = 0;
459 	int64_t                 dyld_aslr_page_offset = 0;
460 	int64_t                 aslr_section_size = 0;
461 	int64_t                 aslr_section_offset = 0;
462 	kern_return_t           kret;
463 	unsigned int            pmap_flags = 0;
464 
465 	if (os_add_overflow(file_offset, macho_size, &total_size) ||
466 	    total_size > file_size) {
467 		return LOAD_BADMACHO;
468 	}
469 
470 	result->is_64bit_addr = ((imgp->ip_flags & IMGPF_IS_64BIT_ADDR) == IMGPF_IS_64BIT_ADDR);
471 	result->is_64bit_data = ((imgp->ip_flags & IMGPF_IS_64BIT_DATA) == IMGPF_IS_64BIT_DATA);
472 #if defined(HAS_APPLE_PAC)
473 	pmap_flags |= (imgp->ip_flags & IMGPF_NOJOP) ? PMAP_CREATE_DISABLE_JOP : 0;
474 #endif /* defined(HAS_APPLE_PAC) */
475 #if CONFIG_ROSETTA
476 	pmap_flags |= (imgp->ip_flags & IMGPF_ROSETTA) ? PMAP_CREATE_ROSETTA : 0;
477 #endif
478 	pmap_flags |= result->is_64bit_addr ? PMAP_CREATE_64BIT : 0;
479 
480 	task_t ledger_task;
481 	if (imgp->ip_new_thread) {
482 		ledger_task = get_threadtask(imgp->ip_new_thread);
483 	} else {
484 		ledger_task = task;
485 	}
486 
487 #if XNU_TARGET_OS_OSX && _POSIX_SPAWN_FORCE_4K_PAGES && PMAP_CREATE_FORCE_4K_PAGES
488 	if (imgp->ip_px_sa != NULL) {
489 		struct _posix_spawnattr* psa = (struct _posix_spawnattr *) imgp->ip_px_sa;
490 		if (psa->psa_flags & _POSIX_SPAWN_FORCE_4K_PAGES) {
491 			pmap_flags |= PMAP_CREATE_FORCE_4K_PAGES;
492 		}
493 	}
494 #endif /* XNU_TARGET_OS_OSX && _POSIX_SPAWN_FORCE_4K_PAGES && PMAP_CREATE_FORCE_4K_PAGE */
495 
496 	pmap = pmap_create_options(get_task_ledger(ledger_task),
497 	    (vm_map_size_t) 0,
498 	    pmap_flags);
499 	if (pmap == NULL) {
500 		return LOAD_RESOURCE;
501 	}
502 	map = vm_map_create_options(pmap, 0,
503 	    vm_compute_max_offset(result->is_64bit_addr),
504 	    VM_MAP_CREATE_PAGEABLE);
505 
506 #if defined(__arm64__)
507 	if (result->is_64bit_addr) {
508 		/* enforce 16KB alignment of VM map entries */
509 		vm_map_set_page_shift(map, SIXTEENK_PAGE_SHIFT);
510 	} else {
511 		vm_map_set_page_shift(map, page_shift_user32);
512 	}
513 #endif /* __arm64__ */
514 
515 #if PMAP_CREATE_FORCE_4K_PAGES
516 	if (pmap_flags & PMAP_CREATE_FORCE_4K_PAGES) {
517 		DEBUG4K_LIFE("***** launching '%s' as 4k *****\n", vp->v_name);
518 		vm_map_set_page_shift(map, FOURK_PAGE_SHIFT);
519 	}
520 #endif /* PMAP_CREATE_FORCE_4K_PAGES */
521 
522 #ifndef CONFIG_ENFORCE_SIGNED_CODE
523 	/* This turns off faulting for executable pages, which allows
524 	 * to circumvent Code Signing Enforcement. The per process
525 	 * flag (CS_ENFORCEMENT) is not set yet, but we can use the
526 	 * global flag.
527 	 */
528 	if (!cs_process_global_enforcement() && (header->flags & MH_ALLOW_STACK_EXECUTION)) {
529 		vm_map_disable_NX(map);
530 		// TODO: Message Trace or log that this is happening
531 	}
532 #endif
533 
534 	/* Forcibly disallow execution from data pages on even if the arch
535 	 * normally permits it. */
536 	if ((header->flags & MH_NO_HEAP_EXECUTION) && !(imgp->ip_flags & IMGPF_ALLOW_DATA_EXEC)) {
537 		vm_map_disallow_data_exec(map);
538 	}
539 
540 	/*
541 	 * Compute a random offset for ASLR, and an independent random offset for dyld.
542 	 */
543 	if (!(imgp->ip_flags & IMGPF_DISABLE_ASLR)) {
544 		vm_map_get_max_aslr_slide_section(map, &aslr_section_offset, &aslr_section_size);
545 		aslr_section_offset = (random() % aslr_section_offset) * aslr_section_size;
546 
547 		aslr_page_offset = random();
548 		aslr_page_offset = (aslr_page_offset % (vm_map_get_max_aslr_slide_pages(map) - 1)) + 1;
549 		aslr_page_offset <<= vm_map_page_shift(map);
550 
551 		dyld_aslr_page_offset = random();
552 		dyld_aslr_page_offset = (dyld_aslr_page_offset %
553 		    (vm_map_get_max_loader_aslr_slide_pages(map) - 1)) + 1;
554 		dyld_aslr_page_offset <<= vm_map_page_shift(map);
555 
556 		aslr_page_offset += aslr_section_offset;
557 	}
558 	if (vm_map_page_shift(map) < (int)PAGE_SHIFT) {
559 		DEBUG4K_LOAD("slide=0x%llx dyld_slide=0x%llx\n", aslr_page_offset, dyld_aslr_page_offset);
560 	}
561 
562 	if (!result) {
563 		result = &myresult;
564 	}
565 
566 	*result = load_result_null;
567 
568 	/*
569 	 * re-set the bitness on the load result since we cleared the load result above.
570 	 */
571 	result->is_64bit_addr = ((imgp->ip_flags & IMGPF_IS_64BIT_ADDR) == IMGPF_IS_64BIT_ADDR);
572 	result->is_64bit_data = ((imgp->ip_flags & IMGPF_IS_64BIT_DATA) == IMGPF_IS_64BIT_DATA);
573 
574 	lret = parse_machfile(vp, map, thread, header, file_offset, macho_size,
575 	    0, aslr_page_offset, dyld_aslr_page_offset, result,
576 	    NULL, imgp);
577 
578 	if (lret != LOAD_SUCCESS) {
579 		imgp->ip_free_map = map;
580 		return lret;
581 	}
582 
583 	/*
584 	 * From now on it's safe to query entitlements via the vnode interface. Let's get figuring
585 	 * out whether we're a security relevant binary out of the way immediately.
586 	 */
587 	result->is_hardened_process = IOVnodeHasEntitlement(imgp->ip_vp,
588 	    (int64_t)imgp->ip_arch_offset, "com.apple.developer.hardened-process");
589 
590 #if __x86_64__
591 	/*
592 	 * On x86, for compatibility, don't enforce the hard page-zero restriction for 32-bit binaries.
593 	 */
594 	if (!result->is_64bit_addr) {
595 		enforce_hard_pagezero = FALSE;
596 	}
597 
598 	/*
599 	 * For processes with IMGPF_HIGH_BITS_ASLR, add a few random high bits
600 	 * to the start address for "anywhere" memory allocations.
601 	 */
602 #define VM_MAP_HIGH_START_BITS_COUNT 8
603 #define VM_MAP_HIGH_START_BITS_SHIFT 27
604 	if (result->is_64bit_addr &&
605 	    (imgp->ip_flags & IMGPF_HIGH_BITS_ASLR)) {
606 		int random_bits;
607 		vm_map_offset_t high_start;
608 
609 		random_bits = random();
610 		random_bits &= (1 << VM_MAP_HIGH_START_BITS_COUNT) - 1;
611 		high_start = (((vm_map_offset_t)random_bits)
612 		        << VM_MAP_HIGH_START_BITS_SHIFT);
613 		vm_map_set_high_start(map, high_start);
614 	}
615 #endif /* __x86_64__ */
616 
617 	/*
618 	 * Check to see if the page zero is enforced by the map->min_offset.
619 	 */
620 	if (enforce_hard_pagezero &&
621 	    (vm_map_has_hard_pagezero(map, 0x1000) == FALSE)) {
622 #if __arm64__
623 		if (
624 			!result->is_64bit_addr && /* not 64-bit address space */
625 			!(header->flags & MH_PIE) &&      /* not PIE */
626 			(vm_map_page_shift(map) != FOURK_PAGE_SHIFT ||
627 			PAGE_SHIFT != FOURK_PAGE_SHIFT) && /* page size != 4KB */
628 			result->has_pagezero && /* has a "soft" page zero */
629 			fourk_binary_compatibility_unsafe) {
630 			/*
631 			 * For backwards compatibility of "4K" apps on
632 			 * a 16K system, do not enforce a hard page zero...
633 			 */
634 		} else
635 #endif /* __arm64__ */
636 		{
637 			imgp->ip_free_map = map;
638 			return LOAD_BADMACHO;
639 		}
640 	}
641 
642 #if __arm64__
643 	if (enforce_hard_pagezero && result->is_64bit_addr && (header->cputype == CPU_TYPE_ARM64)) {
644 		/* 64 bit ARM binary must have "hard page zero" of 4GB to cover the lower 32 bit address space */
645 		if (vm_map_has_hard_pagezero(map, 0x100000000) == FALSE) {
646 			imgp->ip_free_map = map;
647 			return LOAD_BADMACHO;
648 		}
649 	}
650 #endif
651 
652 	vm_commit_pagezero_status(map);
653 
654 	/*
655 	 * If this is an exec, then we are going to destroy the old
656 	 * task, and it's correct to halt it; if it's spawn, the
657 	 * task is not yet running, and it makes no sense.
658 	 */
659 	if (in_exec) {
660 		proc_t p = current_proc();
661 		/*
662 		 * Mark the task as halting and start the other
663 		 * threads towards terminating themselves.  Then
664 		 * make sure any threads waiting for a process
665 		 * transition get informed that we are committed to
666 		 * this transition, and then finally complete the
667 		 * task halting (wait for threads and then cleanup
668 		 * task resources).
669 		 *
670 		 * NOTE: task_start_halt() makes sure that no new
671 		 * threads are created in the task during the transition.
672 		 * We need to mark the workqueue as exiting before we
673 		 * wait for threads to terminate (at the end of which
674 		 * we no longer have a prohibition on thread creation).
675 		 *
676 		 * Finally, clean up any lingering workqueue data structures
677 		 * that may have been left behind by the workqueue threads
678 		 * as they exited (and then clean up the work queue itself).
679 		 */
680 		kret = task_start_halt(task);
681 		if (kret != KERN_SUCCESS) {
682 			imgp->ip_free_map = map;
683 			return LOAD_FAILURE;
684 		}
685 		proc_transcommit(p, 0);
686 		workq_mark_exiting(p);
687 		task_complete_halt(task);
688 		workq_exit(p);
689 
690 		/*
691 		 * Roll up accounting info to new task. The roll up is done after
692 		 * task_complete_halt to make sure the thread accounting info is
693 		 * rolled up to current_task.
694 		 */
695 		task_rollup_accounting_info(get_threadtask(thread), task);
696 	}
697 	*mapp = map;
698 
699 #if XNU_TARGET_OS_OSX
700 	if (process_is_plugin_host(imgp, result)) {
701 		/*
702 		 * We need to disable security policies for processes
703 		 * that run third party plugins.
704 		 */
705 		imgp->ip_flags |= IMGPF_3P_PLUGINS;
706 	}
707 
708 #if __has_feature(ptrauth_calls)
709 	/*
710 	 * arm64e plugin hosts currently run with JOP keys disabled, since they
711 	 * may need to run arm64 plugins.
712 	 */
713 	if (imgp->ip_flags & IMGPF_3P_PLUGINS) {
714 		imgp->ip_flags |= IMGPF_NOJOP;
715 		pmap_disable_user_jop(pmap);
716 	}
717 
718 #if CONFIG_ROSETTA
719 	/* Disable JOP keys if the Rosetta runtime being used isn't arm64e */
720 	if (result->is_rosetta && (imgp->ip_flags & IMGPF_NOJOP)) {
721 		pmap_disable_user_jop(pmap);
722 	}
723 #endif /* CONFIG_ROSETTA */
724 #endif /* __has_feature(ptrauth_calls)*/
725 #endif /* XNU_TARGET_OS_OSX */
726 
727 
728 	return LOAD_SUCCESS;
729 }
730 
731 int macho_printf = 0;
732 #define MACHO_PRINTF(args)                              \
733 	do {                                            \
734 	        if (macho_printf) {                     \
735 	                printf args;                    \
736 	        }                                       \
737 	} while (0)
738 
739 
740 static boolean_t
pie_required(cpu_type_t exectype,cpu_subtype_t execsubtype)741 pie_required(
742 	cpu_type_t exectype,
743 	cpu_subtype_t execsubtype)
744 {
745 	switch (exectype) {
746 	case CPU_TYPE_X86_64:
747 		return FALSE;
748 	case CPU_TYPE_ARM64:
749 		return TRUE;
750 	case CPU_TYPE_ARM:
751 		switch (execsubtype) {
752 		case CPU_SUBTYPE_ARM_V7K:
753 			return TRUE;
754 		}
755 		break;
756 	}
757 	return FALSE;
758 }
759 
760 /*
761  * The file size of a mach-o file is limited to 32 bits; this is because
762  * this is the limit on the kalloc() of enough bytes for a mach_header and
763  * the contents of its sizeofcmds, which is currently constrained to 32
764  * bits in the file format itself.  We read into the kernel buffer the
765  * commands section, and then parse it in order to parse the mach-o file
766  * format load_command segment(s).  We are only interested in a subset of
767  * the total set of possible commands. If "map"==VM_MAP_NULL or
768  * "thread"==THREAD_NULL, do not make permament VM modifications,
769  * just preflight the parse.
770  */
771 static
772 load_return_t
parse_machfile(struct vnode * vp,vm_map_t map,thread_t thread,struct mach_header * header,off_t file_offset,off_t macho_size,int depth,int64_t aslr_offset,int64_t dyld_aslr_offset,load_result_t * result,load_result_t * binresult,struct image_params * imgp)773 parse_machfile(
774 	struct vnode            *vp,
775 	vm_map_t                map,
776 	thread_t                thread,
777 	struct mach_header      *header,
778 	off_t                   file_offset,
779 	off_t                   macho_size,
780 	int                     depth,
781 	int64_t                 aslr_offset,
782 	int64_t                 dyld_aslr_offset,
783 	load_result_t           *result,
784 	load_result_t           *binresult,
785 	struct image_params     *imgp
786 	)
787 {
788 	uint32_t                ncmds;
789 	struct load_command     *lcp;
790 	struct dylinker_command *dlp = 0;
791 	void *                  control;
792 	load_return_t           ret = LOAD_SUCCESS;
793 	void *                  addr;
794 	vm_size_t               alloc_size, cmds_size;
795 	size_t                  offset;
796 	size_t                  oldoffset;      /* for overflow check */
797 	int                     pass;
798 	proc_t                  p = vfs_context_proc(imgp->ip_vfs_context);
799 	int                     error;
800 	int                     resid = 0;
801 	int                     spawn = (imgp->ip_flags & IMGPF_SPAWN);
802 	size_t                  mach_header_sz = sizeof(struct mach_header);
803 	boolean_t               abi64;
804 	boolean_t               got_code_signatures = FALSE;
805 	boolean_t               found_header_segment = FALSE;
806 	boolean_t               found_xhdr = FALSE;
807 	boolean_t               found_version_cmd = FALSE;
808 	int64_t                 slide = 0;
809 	boolean_t               dyld_no_load_addr = FALSE;
810 	boolean_t               is_dyld = FALSE;
811 	vm_map_offset_t         effective_page_mask = PAGE_MASK;
812 #if __arm64__
813 	uint64_t                pagezero_end = 0;
814 	uint64_t                executable_end = 0;
815 	uint64_t                writable_start = 0;
816 	vm_map_size_t           effective_page_size;
817 
818 	effective_page_mask = vm_map_page_mask(map);
819 	effective_page_size = vm_map_page_size(map);
820 #endif /* __arm64__ */
821 
822 	if (header->magic == MH_MAGIC_64 ||
823 	    header->magic == MH_CIGAM_64) {
824 		mach_header_sz = sizeof(struct mach_header_64);
825 	}
826 
827 	/*
828 	 *	Break infinite recursion
829 	 */
830 	if (depth > 2) {
831 		return LOAD_FAILURE;
832 	}
833 
834 	depth++;
835 
836 	/*
837 	 * Set CS_NO_UNTRUSTED_HELPERS by default; load_dylinker and load_rosetta
838 	 * will unset it if necessary.
839 	 */
840 	if (depth == 1) {
841 		result->csflags |= CS_NO_UNTRUSTED_HELPERS;
842 	}
843 
844 	/*
845 	 *	Check to see if right machine type.
846 	 */
847 	if (((cpu_type_t)(header->cputype & ~CPU_ARCH_MASK) != (cpu_type() & ~CPU_ARCH_MASK))
848 	    ) {
849 		return LOAD_BADARCH;
850 	}
851 
852 	if (!grade_binary(header->cputype,
853 	    header->cpusubtype & ~CPU_SUBTYPE_MASK,
854 	    header->cpusubtype & CPU_SUBTYPE_MASK, TRUE)) {
855 		return LOAD_BADARCH;
856 	}
857 
858 	abi64 = ((header->cputype & CPU_ARCH_ABI64) == CPU_ARCH_ABI64);
859 
860 	switch (header->filetype) {
861 	case MH_EXECUTE:
862 		if (depth != 1 && depth != 3) {
863 			return LOAD_FAILURE;
864 		}
865 		if (header->flags & MH_DYLDLINK) {
866 			/* Check properties of dynamic executables */
867 			if (!(header->flags & MH_PIE) && pie_required(header->cputype, header->cpusubtype & ~CPU_SUBTYPE_MASK)) {
868 				return LOAD_FAILURE;
869 			}
870 			result->needs_dynlinker = TRUE;
871 		} else if (header->cputype == CPU_TYPE_X86_64) {
872 			/* x86_64 static binaries allowed */
873 #if CONFIG_ROSETTA
874 		} else if (imgp->ip_flags & IMGPF_ROSETTA) {
875 			/* Rosetta runtime allowed */
876 #endif /* CONFIG_X86_64_COMPAT */
877 		} else {
878 			/* Check properties of static executables (disallowed except for development) */
879 #if !(DEVELOPMENT || DEBUG)
880 			return LOAD_FAILURE;
881 #endif
882 		}
883 		break;
884 	case MH_DYLINKER:
885 		if (depth != 2) {
886 			return LOAD_FAILURE;
887 		}
888 		is_dyld = TRUE;
889 		break;
890 
891 	default:
892 		return LOAD_FAILURE;
893 	}
894 
895 	/*
896 	 *	For PIE and dyld, slide everything by the ASLR offset.
897 	 */
898 	if ((header->flags & MH_PIE) || is_dyld) {
899 		slide = aslr_offset;
900 	}
901 
902 	/*
903 	 *	Get the pager for the file.
904 	 */
905 	control = ubc_getobject(vp, UBC_FLAGS_NONE);
906 
907 	/* ensure header + sizeofcmds falls within the file */
908 	if (os_add_overflow(mach_header_sz, header->sizeofcmds, &cmds_size) ||
909 	    (off_t)cmds_size > macho_size ||
910 	    round_page_overflow(cmds_size, &alloc_size) ||
911 	    alloc_size > INT_MAX) {
912 		return LOAD_BADMACHO;
913 	}
914 
915 	/*
916 	 * Map the load commands into kernel memory.
917 	 */
918 	addr = kalloc_data(alloc_size, Z_WAITOK);
919 	if (addr == NULL) {
920 		return LOAD_NOSPACE;
921 	}
922 
923 	error = vn_rdwr(UIO_READ, vp, addr, (int)alloc_size, file_offset,
924 	    UIO_SYSSPACE, 0, vfs_context_ucred(imgp->ip_vfs_context), &resid, p);
925 	if (error) {
926 		kfree_data(addr, alloc_size);
927 		return LOAD_IOERROR;
928 	}
929 
930 	if (resid) {
931 		{
932 			/* We must be able to read in as much as the mach_header indicated */
933 			kfree_data(addr, alloc_size);
934 			return LOAD_BADMACHO;
935 		}
936 	}
937 
938 	/*
939 	 *  Scan through the commands, processing each one as necessary.
940 	 *  We parse in three passes through the headers:
941 	 *  0: determine if TEXT and DATA boundary can be page-aligned, load platform version
942 	 *  1: thread state, uuid, code signature
943 	 *  2: segments
944 	 *  3: dyld, encryption, check entry point
945 	 */
946 
947 	boolean_t slide_realign = FALSE;
948 #if __arm64__
949 	if (!abi64) {
950 		slide_realign = TRUE;
951 	}
952 #endif
953 
954 	for (pass = 0; pass <= 3; pass++) {
955 		if (pass == 1) {
956 #if __arm64__
957 			boolean_t       is_pie;
958 			int64_t         adjust;
959 
960 			is_pie = ((header->flags & MH_PIE) != 0);
961 			if (pagezero_end != 0 &&
962 			    pagezero_end < effective_page_size) {
963 				/* need at least 1 page for PAGEZERO */
964 				adjust = effective_page_size;
965 				MACHO_PRINTF(("pagezero boundary at "
966 				    "0x%llx; adjust slide from "
967 				    "0x%llx to 0x%llx%s\n",
968 				    (uint64_t) pagezero_end,
969 				    slide,
970 				    slide + adjust,
971 				    (is_pie
972 				    ? ""
973 				    : " BUT NO PIE ****** :-(")));
974 				if (is_pie) {
975 					slide += adjust;
976 					pagezero_end += adjust;
977 					executable_end += adjust;
978 					writable_start += adjust;
979 				}
980 			}
981 			if (pagezero_end != 0) {
982 				result->has_pagezero = TRUE;
983 			}
984 			if (executable_end == writable_start &&
985 			    (executable_end & effective_page_mask) != 0 &&
986 			    (executable_end & FOURK_PAGE_MASK) == 0) {
987 				/*
988 				 * The TEXT/DATA boundary is 4K-aligned but
989 				 * not page-aligned.  Adjust the slide to make
990 				 * it page-aligned and avoid having a page
991 				 * with both write and execute permissions.
992 				 */
993 				adjust =
994 				    (effective_page_size -
995 				    (executable_end & effective_page_mask));
996 				MACHO_PRINTF(("page-unaligned X-W boundary at "
997 				    "0x%llx; adjust slide from "
998 				    "0x%llx to 0x%llx%s\n",
999 				    (uint64_t) executable_end,
1000 				    slide,
1001 				    slide + adjust,
1002 				    (is_pie
1003 				    ? ""
1004 				    : " BUT NO PIE ****** :-(")));
1005 				if (is_pie) {
1006 					slide += adjust;
1007 				}
1008 			}
1009 #endif /* __arm64__ */
1010 
1011 			if (dyld_no_load_addr && binresult) {
1012 				/*
1013 				 * The dyld Mach-O does not specify a load address. Try to locate
1014 				 * it right after the main binary. If binresult == NULL, load
1015 				 * directly to the given slide.
1016 				 */
1017 				mach_vm_address_t max_vm_addr = binresult->max_vm_addr;
1018 				slide = vm_map_round_page(slide + max_vm_addr, effective_page_mask);
1019 			}
1020 		}
1021 
1022 		/*
1023 		 * Check that the entry point is contained in an executable segment
1024 		 */
1025 		if ((pass == 3) && (thread != THREAD_NULL)) {
1026 			if (depth == 1 && imgp && (imgp->ip_flags & IMGPF_DRIVER)) {
1027 				/* Driver binaries must have driverkit platform */
1028 				if (result->ip_platform == PLATFORM_DRIVERKIT) {
1029 					/* Driver binaries have no entry point */
1030 					ret = setup_driver_main(thread, slide, result);
1031 				} else {
1032 					ret = LOAD_FAILURE;
1033 				}
1034 			} else if (!result->using_lcmain && result->validentry == 0) {
1035 				ret = LOAD_FAILURE;
1036 			}
1037 			if (ret != KERN_SUCCESS) {
1038 				thread_state_initialize(thread);
1039 				break;
1040 			}
1041 		}
1042 
1043 		/*
1044 		 * Check that some segment maps the start of the mach-o file, which is
1045 		 * needed by the dynamic loader to read the mach headers, etc.
1046 		 */
1047 		if ((pass == 3) && (found_header_segment == FALSE)) {
1048 			ret = LOAD_BADMACHO;
1049 			break;
1050 		}
1051 
1052 		/*
1053 		 * Loop through each of the load_commands indicated by the
1054 		 * Mach-O header; if an absurd value is provided, we just
1055 		 * run off the end of the reserved section by incrementing
1056 		 * the offset too far, so we are implicitly fail-safe.
1057 		 */
1058 		offset = mach_header_sz;
1059 		ncmds = header->ncmds;
1060 
1061 		while (ncmds--) {
1062 			/* ensure enough space for a minimal load command */
1063 			if (offset + sizeof(struct load_command) > cmds_size) {
1064 				ret = LOAD_BADMACHO;
1065 				break;
1066 			}
1067 
1068 			/*
1069 			 *	Get a pointer to the command.
1070 			 */
1071 			lcp = (struct load_command *)((uintptr_t)addr + offset);
1072 			oldoffset = offset;
1073 
1074 			/*
1075 			 * Perform prevalidation of the struct load_command
1076 			 * before we attempt to use its contents.  Invalid
1077 			 * values are ones which result in an overflow, or
1078 			 * which can not possibly be valid commands, or which
1079 			 * straddle or exist past the reserved section at the
1080 			 * start of the image.
1081 			 */
1082 			if (os_add_overflow(offset, lcp->cmdsize, &offset) ||
1083 			    lcp->cmdsize < sizeof(struct load_command) ||
1084 			    offset > cmds_size) {
1085 				ret = LOAD_BADMACHO;
1086 				break;
1087 			}
1088 
1089 			/*
1090 			 * Act on struct load_command's for which kernel
1091 			 * intervention is required.
1092 			 * Note that each load command implementation is expected to validate
1093 			 * that lcp->cmdsize is large enough to fit its specific struct type
1094 			 * before dereferencing fields not covered by struct load_command.
1095 			 */
1096 			switch (lcp->cmd) {
1097 			case LC_SEGMENT: {
1098 				struct segment_command *scp = (struct segment_command *) lcp;
1099 				if (scp->cmdsize < sizeof(*scp)) {
1100 					ret = LOAD_BADMACHO;
1101 					break;
1102 				}
1103 				if (pass == 0) {
1104 					if (is_dyld && scp->vmaddr == 0 && scp->fileoff == 0) {
1105 						dyld_no_load_addr = TRUE;
1106 						if (!slide_realign) {
1107 							/* got what we need, bail early on pass 0 */
1108 							continue;
1109 						}
1110 					}
1111 
1112 #if __arm64__
1113 					assert(!abi64);
1114 
1115 					if (scp->initprot == 0 && scp->maxprot == 0 && scp->vmaddr == 0) {
1116 						/* PAGEZERO */
1117 						if (os_add3_overflow(scp->vmaddr, scp->vmsize, slide, &pagezero_end) || pagezero_end > UINT32_MAX) {
1118 							ret = LOAD_BADMACHO;
1119 							break;
1120 						}
1121 					}
1122 					if (scp->initprot & VM_PROT_EXECUTE) {
1123 						/* TEXT */
1124 						if (os_add3_overflow(scp->vmaddr, scp->vmsize, slide, &executable_end) || executable_end > UINT32_MAX) {
1125 							ret = LOAD_BADMACHO;
1126 							break;
1127 						}
1128 					}
1129 					if (scp->initprot & VM_PROT_WRITE) {
1130 						/* DATA */
1131 						if (os_add_overflow(scp->vmaddr, slide, &writable_start) || writable_start > UINT32_MAX) {
1132 							ret = LOAD_BADMACHO;
1133 							break;
1134 						}
1135 					}
1136 #endif /* __arm64__ */
1137 					break;
1138 				}
1139 
1140 				if (pass == 1 && !strncmp(scp->segname, "__XHDR", sizeof(scp->segname))) {
1141 					found_xhdr = TRUE;
1142 				}
1143 
1144 				if (pass != 2) {
1145 					break;
1146 				}
1147 
1148 				if (abi64) {
1149 					/*
1150 					 * Having an LC_SEGMENT command for the
1151 					 * wrong ABI is invalid <rdar://problem/11021230>
1152 					 */
1153 					ret = LOAD_BADMACHO;
1154 					break;
1155 				}
1156 
1157 				ret = load_segment(lcp,
1158 				    header->filetype,
1159 				    control,
1160 				    file_offset,
1161 				    macho_size,
1162 				    vp,
1163 				    map,
1164 				    slide,
1165 				    result,
1166 				    imgp);
1167 				if (ret == LOAD_SUCCESS && scp->fileoff == 0 && scp->filesize > 0) {
1168 					/* Enforce a single segment mapping offset zero, with R+X
1169 					 * protection. */
1170 					if (found_header_segment ||
1171 					    ((scp->initprot & (VM_PROT_READ | VM_PROT_EXECUTE)) != (VM_PROT_READ | VM_PROT_EXECUTE))) {
1172 						ret = LOAD_BADMACHO;
1173 						break;
1174 					}
1175 					found_header_segment = TRUE;
1176 				}
1177 
1178 				break;
1179 			}
1180 			case LC_SEGMENT_64: {
1181 				struct segment_command_64 *scp64 = (struct segment_command_64 *) lcp;
1182 				if (scp64->cmdsize < sizeof(*scp64)) {
1183 					ret = LOAD_BADMACHO;
1184 					break;
1185 				}
1186 				if (pass == 0) {
1187 					if (is_dyld && scp64->vmaddr == 0 && scp64->fileoff == 0) {
1188 						dyld_no_load_addr = TRUE;
1189 					}
1190 					/* got what we need, bail early on pass 0 */
1191 					continue;
1192 				}
1193 
1194 				if (pass == 1 && !strncmp(scp64->segname, "__XHDR", sizeof(scp64->segname))) {
1195 					found_xhdr = TRUE;
1196 				}
1197 
1198 				if (pass != 2) {
1199 					break;
1200 				}
1201 
1202 				if (!abi64) {
1203 					/*
1204 					 * Having an LC_SEGMENT_64 command for the
1205 					 * wrong ABI is invalid <rdar://problem/11021230>
1206 					 */
1207 					ret = LOAD_BADMACHO;
1208 					break;
1209 				}
1210 
1211 				ret = load_segment(lcp,
1212 				    header->filetype,
1213 				    control,
1214 				    file_offset,
1215 				    macho_size,
1216 				    vp,
1217 				    map,
1218 				    slide,
1219 				    result,
1220 				    imgp);
1221 
1222 				if (ret == LOAD_SUCCESS && scp64->fileoff == 0 && scp64->filesize > 0) {
1223 					/* Enforce a single segment mapping offset zero, with R+X
1224 					 * protection. */
1225 					if (found_header_segment ||
1226 					    ((scp64->initprot & (VM_PROT_READ | VM_PROT_EXECUTE)) != (VM_PROT_READ | VM_PROT_EXECUTE))) {
1227 						ret = LOAD_BADMACHO;
1228 						break;
1229 					}
1230 					found_header_segment = TRUE;
1231 				}
1232 
1233 				break;
1234 			}
1235 			case LC_UNIXTHREAD: {
1236 				boolean_t is_x86_64_compat_binary = FALSE;
1237 				if (pass != 1) {
1238 					break;
1239 				}
1240 #if CONFIG_ROSETTA
1241 				if (depth == 2 && (imgp->ip_flags & IMGPF_ROSETTA)) {
1242 					// Ignore dyld, Rosetta will parse it's load commands to get the
1243 					// entry point.
1244 					result->validentry = 1;
1245 					break;
1246 				}
1247 #endif
1248 				ret = load_unixthread(
1249 					(struct thread_command *) lcp,
1250 					thread,
1251 					slide,
1252 					is_x86_64_compat_binary,
1253 					result);
1254 				break;
1255 			}
1256 			case LC_MAIN:
1257 				if (pass != 1) {
1258 					break;
1259 				}
1260 				if (depth != 1) {
1261 					break;
1262 				}
1263 				ret = load_main(
1264 					(struct entry_point_command *) lcp,
1265 					thread,
1266 					slide,
1267 					result);
1268 				break;
1269 			case LC_LOAD_DYLINKER:
1270 				if (pass != 3) {
1271 					break;
1272 				}
1273 				if ((depth == 1) && (dlp == 0)) {
1274 					dlp = (struct dylinker_command *)lcp;
1275 				} else {
1276 					ret = LOAD_FAILURE;
1277 				}
1278 				break;
1279 			case LC_UUID:
1280 				if (pass == 1 && depth == 1) {
1281 					ret = load_uuid((struct uuid_command *) lcp,
1282 					    (char *)addr + cmds_size,
1283 					    result);
1284 				}
1285 				break;
1286 			case LC_CODE_SIGNATURE:
1287 				/* CODE SIGNING */
1288 				if (pass != 1) {
1289 					break;
1290 				}
1291 
1292 				/* pager -> uip ->
1293 				 *  load signatures & store in uip
1294 				 *  set VM object "signed_pages"
1295 				 */
1296 				ret = load_code_signature(
1297 					(struct linkedit_data_command *) lcp,
1298 					vp,
1299 					file_offset,
1300 					macho_size,
1301 					header->cputype,
1302 					header->cpusubtype,
1303 					result,
1304 					imgp);
1305 				if (ret != LOAD_SUCCESS) {
1306 					printf("proc %d: load code signature error %d "
1307 					    "for file \"%s\"\n",
1308 					    proc_getpid(p), ret, vp->v_name);
1309 					/*
1310 					 * Allow injections to be ignored on devices w/o enforcement enabled
1311 					 */
1312 					if (!cs_process_global_enforcement()) {
1313 						ret = LOAD_SUCCESS; /* ignore error */
1314 					}
1315 				} else {
1316 					got_code_signatures = TRUE;
1317 				}
1318 
1319 				if (got_code_signatures) {
1320 					unsigned tainted = CS_VALIDATE_TAINTED;
1321 					boolean_t valid = FALSE;
1322 					vm_size_t off = 0;
1323 
1324 
1325 					if (cs_debug > 10) {
1326 						printf("validating initial pages of %s\n", vp->v_name);
1327 					}
1328 
1329 					while (off < alloc_size && ret == LOAD_SUCCESS) {
1330 						tainted = CS_VALIDATE_TAINTED;
1331 
1332 						valid = cs_validate_range(vp,
1333 						    NULL,
1334 						    file_offset + off,
1335 						    (const void *)((uintptr_t)addr + off),
1336 						    MIN(PAGE_SIZE, cmds_size),
1337 						    &tainted);
1338 						if (!valid || (tainted & CS_VALIDATE_TAINTED)) {
1339 							if (cs_debug) {
1340 								printf("CODE SIGNING: %s[%d]: invalid initial page at offset %lld validated:%d tainted:%d csflags:0x%x\n",
1341 								    vp->v_name, proc_getpid(p), (long long)(file_offset + off), valid, tainted, result->csflags);
1342 							}
1343 							if (cs_process_global_enforcement() ||
1344 							    (result->csflags & (CS_HARD | CS_KILL | CS_ENFORCEMENT))) {
1345 								ret = LOAD_FAILURE;
1346 							}
1347 							result->csflags &= ~CS_VALID;
1348 						}
1349 						off += PAGE_SIZE;
1350 					}
1351 				}
1352 
1353 				break;
1354 #if CONFIG_CODE_DECRYPTION
1355 			case LC_ENCRYPTION_INFO:
1356 			case LC_ENCRYPTION_INFO_64:
1357 				if (pass != 3) {
1358 					break;
1359 				}
1360 				ret = set_code_unprotect(
1361 					(struct encryption_info_command *) lcp,
1362 					addr, map, slide, vp, file_offset,
1363 					header->cputype, header->cpusubtype);
1364 				if (ret != LOAD_SUCCESS) {
1365 					os_reason_t load_failure_reason = OS_REASON_NULL;
1366 					printf("proc %d: set_code_unprotect() error %d "
1367 					    "for file \"%s\"\n",
1368 					    proc_getpid(p), ret, vp->v_name);
1369 					/*
1370 					 * Don't let the app run if it's
1371 					 * encrypted but we failed to set up the
1372 					 * decrypter. If the keys are missing it will
1373 					 * return LOAD_DECRYPTFAIL.
1374 					 */
1375 					if (ret == LOAD_DECRYPTFAIL) {
1376 						/* failed to load due to missing FP keys */
1377 						proc_lock(p);
1378 						p->p_lflag |= P_LTERM_DECRYPTFAIL;
1379 						proc_unlock(p);
1380 
1381 						KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXITREASON_CREATE) | DBG_FUNC_NONE,
1382 						    proc_getpid(p), OS_REASON_EXEC, EXEC_EXIT_REASON_FAIRPLAY_DECRYPT, 0, 0);
1383 						load_failure_reason = os_reason_create(OS_REASON_EXEC, EXEC_EXIT_REASON_FAIRPLAY_DECRYPT);
1384 					} else {
1385 						KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXITREASON_CREATE) | DBG_FUNC_NONE,
1386 						    proc_getpid(p), OS_REASON_EXEC, EXEC_EXIT_REASON_DECRYPT, 0, 0);
1387 						load_failure_reason = os_reason_create(OS_REASON_EXEC, EXEC_EXIT_REASON_DECRYPT);
1388 					}
1389 
1390 					/*
1391 					 * Don't signal the process if it was forked and in a partially constructed
1392 					 * state as part of a spawn -- it will just be torn down when the exec fails.
1393 					 */
1394 					if (!spawn) {
1395 						assert(load_failure_reason != OS_REASON_NULL);
1396 						{
1397 							psignal_with_reason(current_proc(), SIGKILL, load_failure_reason);
1398 							load_failure_reason = OS_REASON_NULL;
1399 						}
1400 					} else {
1401 						os_reason_free(load_failure_reason);
1402 						load_failure_reason = OS_REASON_NULL;
1403 					}
1404 				}
1405 				break;
1406 #endif
1407 			case LC_VERSION_MIN_IPHONEOS:
1408 			case LC_VERSION_MIN_MACOSX:
1409 			case LC_VERSION_MIN_WATCHOS:
1410 			case LC_VERSION_MIN_TVOS: {
1411 				struct version_min_command *vmc;
1412 
1413 				if (depth != 1 || pass != 0) {
1414 					break;
1415 				}
1416 				vmc = (struct version_min_command *) lcp;
1417 				ret = load_version(vmc, &found_version_cmd, imgp, result);
1418 #if XNU_TARGET_OS_OSX
1419 				if (ret == LOAD_SUCCESS) {
1420 					if (result->ip_platform == PLATFORM_IOS) {
1421 						vm_map_mark_alien(map);
1422 					} else {
1423 						assert(!vm_map_is_alien(map));
1424 					}
1425 				}
1426 #endif /* XNU_TARGET_OS_OSX */
1427 				break;
1428 			}
1429 			case LC_BUILD_VERSION: {
1430 				if (depth != 1 || pass != 0) {
1431 					break;
1432 				}
1433 				struct build_version_command* bvc = (struct build_version_command*)lcp;
1434 				if (bvc->cmdsize < sizeof(*bvc)) {
1435 					ret = LOAD_BADMACHO;
1436 					break;
1437 				}
1438 				if (found_version_cmd == TRUE) {
1439 					ret = LOAD_BADMACHO;
1440 					break;
1441 				}
1442 				result->ip_platform = bvc->platform;
1443 				result->lr_sdk = bvc->sdk;
1444 				result->lr_min_sdk = bvc->minos;
1445 				found_version_cmd = TRUE;
1446 #if XNU_TARGET_OS_OSX
1447 				if (result->ip_platform == PLATFORM_IOS) {
1448 					vm_map_mark_alien(map);
1449 				} else {
1450 					assert(!vm_map_is_alien(map));
1451 				}
1452 #endif /* XNU_TARGET_OS_OSX */
1453 				break;
1454 			}
1455 			default:
1456 				/* Other commands are ignored by the kernel */
1457 				ret = LOAD_SUCCESS;
1458 				break;
1459 			}
1460 			if (ret != LOAD_SUCCESS) {
1461 				break;
1462 			}
1463 		}
1464 		if (ret != LOAD_SUCCESS) {
1465 			break;
1466 		}
1467 	}
1468 
1469 	if (ret == LOAD_SUCCESS) {
1470 		if (!got_code_signatures && cs_process_global_enforcement()) {
1471 			ret = LOAD_FAILURE;
1472 		}
1473 
1474 		/* Make sure if we need dyld, we got it */
1475 		if (result->needs_dynlinker && !dlp) {
1476 			ret = LOAD_FAILURE;
1477 		}
1478 
1479 		if ((ret == LOAD_SUCCESS) && (dlp != 0)) {
1480 			/*
1481 			 * load the dylinker, and slide it by the independent DYLD ASLR
1482 			 * offset regardless of the PIE-ness of the main binary.
1483 			 */
1484 			ret = load_dylinker(dlp, header->cputype, map, thread, depth,
1485 			    dyld_aslr_offset, result, imgp);
1486 		}
1487 
1488 #if CONFIG_ROSETTA
1489 		if ((ret == LOAD_SUCCESS) && (depth == 1) && (imgp->ip_flags & IMGPF_ROSETTA)) {
1490 			ret = load_rosetta(map, thread, result, imgp);
1491 			if (ret == LOAD_SUCCESS) {
1492 				if (result->user_stack_alloc_size != 0) {
1493 					// If a stack allocation is required then add a 4gb gap after the main
1494 					// binary/dyld for the worst case static translation size.
1495 					mach_vm_size_t reserved_aot_size = 0x100000000;
1496 					vm_map_offset_t mask = vm_map_page_mask(map);
1497 
1498 					mach_vm_address_t vm_end;
1499 					if (dlp != 0) {
1500 						vm_end = vm_map_round_page(result->dynlinker_max_vm_addr, mask);
1501 					} else {
1502 						vm_end = vm_map_round_page(result->max_vm_addr, mask);
1503 					}
1504 
1505 					mach_vm_size_t user_stack_size = vm_map_round_page(result->user_stack_alloc_size, mask);
1506 					result->user_stack = vm_map_round_page(vm_end + user_stack_size + reserved_aot_size + slide, mask);
1507 				}
1508 			}
1509 		}
1510 #endif
1511 
1512 		if ((ret == LOAD_SUCCESS) && (depth == 1)) {
1513 			if (result->thread_count == 0) {
1514 				ret = LOAD_FAILURE;
1515 			}
1516 #if CONFIG_ENFORCE_SIGNED_CODE
1517 			if (!(result->csflags & CS_NO_UNTRUSTED_HELPERS)) {
1518 				ret = LOAD_FAILURE;
1519 			}
1520 #endif
1521 		}
1522 	}
1523 
1524 	if (ret == LOAD_BADMACHO && found_xhdr) {
1525 		ret = LOAD_BADMACHO_UPX;
1526 	}
1527 
1528 	kfree_data(addr, alloc_size);
1529 
1530 	return ret;
1531 }
1532 
1533 load_return_t
validate_potential_simulator_binary(cpu_type_t exectype __unused,struct image_params * imgp __unused,off_t file_offset __unused,off_t macho_size __unused)1534 validate_potential_simulator_binary(
1535 	cpu_type_t               exectype __unused,
1536 	struct image_params      *imgp __unused,
1537 	off_t                    file_offset __unused,
1538 	off_t                    macho_size __unused)
1539 {
1540 #if __x86_64__
1541 	/* Allow 32 bit exec only for simulator binaries */
1542 	if (bootarg_no32exec && imgp != NULL && exectype == CPU_TYPE_X86) {
1543 		if (imgp->ip_simulator_binary == IMGPF_SB_DEFAULT) {
1544 			boolean_t simulator_binary = check_if_simulator_binary(imgp, file_offset, macho_size);
1545 			imgp->ip_simulator_binary = simulator_binary ? IMGPF_SB_TRUE : IMGPF_SB_FALSE;
1546 		}
1547 
1548 		if (imgp->ip_simulator_binary != IMGPF_SB_TRUE) {
1549 			return LOAD_BADARCH;
1550 		}
1551 	}
1552 #endif
1553 	return LOAD_SUCCESS;
1554 }
1555 
1556 #if __x86_64__
1557 static boolean_t
check_if_simulator_binary(struct image_params * imgp,off_t file_offset,off_t macho_size)1558 check_if_simulator_binary(
1559 	struct image_params     *imgp,
1560 	off_t                   file_offset,
1561 	off_t                   macho_size)
1562 {
1563 	struct mach_header      *header;
1564 	char                    *ip_vdata = NULL;
1565 	kauth_cred_t            cred = NULL;
1566 	uint32_t                ncmds;
1567 	struct load_command     *lcp;
1568 	boolean_t               simulator_binary = FALSE;
1569 	void *                  addr = NULL;
1570 	vm_size_t               alloc_size, cmds_size;
1571 	size_t                  offset;
1572 	proc_t                  p = current_proc();             /* XXXX */
1573 	int                     error;
1574 	int                     resid = 0;
1575 	size_t                  mach_header_sz = sizeof(struct mach_header);
1576 
1577 
1578 	cred =  kauth_cred_proc_ref(p);
1579 
1580 	/* Allocate page to copyin mach header */
1581 	ip_vdata = kalloc_data(PAGE_SIZE, Z_WAITOK | Z_ZERO);
1582 	if (ip_vdata == NULL) {
1583 		goto bad;
1584 	}
1585 
1586 	/* Read the Mach-O header */
1587 	error = vn_rdwr(UIO_READ, imgp->ip_vp, ip_vdata,
1588 	    PAGE_SIZE, file_offset,
1589 	    UIO_SYSSPACE, (IO_UNIT | IO_NODELOCKED),
1590 	    cred, &resid, p);
1591 	if (error) {
1592 		goto bad;
1593 	}
1594 
1595 	header = (struct mach_header *)ip_vdata;
1596 
1597 	if (header->magic == MH_MAGIC_64 ||
1598 	    header->magic == MH_CIGAM_64) {
1599 		mach_header_sz = sizeof(struct mach_header_64);
1600 	}
1601 
1602 	/* ensure header + sizeofcmds falls within the file */
1603 	if (os_add_overflow(mach_header_sz, header->sizeofcmds, &cmds_size) ||
1604 	    (off_t)cmds_size > macho_size ||
1605 	    round_page_overflow(cmds_size, &alloc_size) ||
1606 	    alloc_size > INT_MAX) {
1607 		goto bad;
1608 	}
1609 
1610 	/*
1611 	 * Map the load commands into kernel memory.
1612 	 */
1613 	addr = kalloc_data(alloc_size, Z_WAITOK);
1614 	if (addr == NULL) {
1615 		goto bad;
1616 	}
1617 
1618 	error = vn_rdwr(UIO_READ, imgp->ip_vp, addr, (int)alloc_size, file_offset,
1619 	    UIO_SYSSPACE, IO_NODELOCKED, cred, &resid, p);
1620 	if (error) {
1621 		goto bad;
1622 	}
1623 
1624 	if (resid) {
1625 		/* We must be able to read in as much as the mach_header indicated */
1626 		goto bad;
1627 	}
1628 
1629 	/*
1630 	 * Loop through each of the load_commands indicated by the
1631 	 * Mach-O header; if an absurd value is provided, we just
1632 	 * run off the end of the reserved section by incrementing
1633 	 * the offset too far, so we are implicitly fail-safe.
1634 	 */
1635 	offset = mach_header_sz;
1636 	ncmds = header->ncmds;
1637 
1638 	while (ncmds--) {
1639 		/* ensure enough space for a minimal load command */
1640 		if (offset + sizeof(struct load_command) > cmds_size) {
1641 			break;
1642 		}
1643 
1644 		/*
1645 		 *	Get a pointer to the command.
1646 		 */
1647 		lcp = (struct load_command *)((uintptr_t)addr + offset);
1648 
1649 		/*
1650 		 * Perform prevalidation of the struct load_command
1651 		 * before we attempt to use its contents.  Invalid
1652 		 * values are ones which result in an overflow, or
1653 		 * which can not possibly be valid commands, or which
1654 		 * straddle or exist past the reserved section at the
1655 		 * start of the image.
1656 		 */
1657 		if (os_add_overflow(offset, lcp->cmdsize, &offset) ||
1658 		    lcp->cmdsize < sizeof(struct load_command) ||
1659 		    offset > cmds_size) {
1660 			break;
1661 		}
1662 
1663 		/* Check if its a simulator binary. */
1664 		switch (lcp->cmd) {
1665 		case LC_VERSION_MIN_WATCHOS:
1666 			simulator_binary = TRUE;
1667 			break;
1668 
1669 		case LC_BUILD_VERSION: {
1670 			struct build_version_command *bvc;
1671 
1672 			bvc = (struct build_version_command *) lcp;
1673 			if (bvc->cmdsize < sizeof(*bvc)) {
1674 				/* unsafe to use this command struct if cmdsize
1675 				* validated above is too small for it to fit */
1676 				break;
1677 			}
1678 			if (bvc->platform == PLATFORM_IOSSIMULATOR ||
1679 			    bvc->platform == PLATFORM_WATCHOSSIMULATOR) {
1680 				simulator_binary = TRUE;
1681 			}
1682 
1683 			break;
1684 		}
1685 
1686 		case LC_VERSION_MIN_IPHONEOS: {
1687 			simulator_binary = TRUE;
1688 			break;
1689 		}
1690 
1691 		default:
1692 			/* ignore other load commands */
1693 			break;
1694 		}
1695 
1696 		if (simulator_binary == TRUE) {
1697 			break;
1698 		}
1699 	}
1700 
1701 bad:
1702 	if (ip_vdata) {
1703 		kfree_data(ip_vdata, PAGE_SIZE);
1704 	}
1705 
1706 	if (cred) {
1707 		kauth_cred_unref(&cred);
1708 	}
1709 
1710 	if (addr) {
1711 		kfree_data(addr, alloc_size);
1712 	}
1713 
1714 	return simulator_binary;
1715 }
1716 #endif /* __x86_64__ */
1717 
1718 #if CONFIG_CODE_DECRYPTION
1719 
1720 #define APPLE_UNPROTECTED_HEADER_SIZE   (3 * 4096)
1721 
1722 static load_return_t
unprotect_dsmos_segment(uint64_t file_off,uint64_t file_size,struct vnode * vp,off_t macho_offset,vm_map_t map,vm_map_offset_t map_addr,vm_map_size_t map_size)1723 unprotect_dsmos_segment(
1724 	uint64_t        file_off,
1725 	uint64_t        file_size,
1726 	struct vnode    *vp,
1727 	off_t           macho_offset,
1728 	vm_map_t        map,
1729 	vm_map_offset_t map_addr,
1730 	vm_map_size_t   map_size)
1731 {
1732 	kern_return_t   kr;
1733 	uint64_t        slice_off;
1734 
1735 	/*
1736 	 * The first APPLE_UNPROTECTED_HEADER_SIZE bytes (from offset 0 of
1737 	 * this part of a Universal binary) are not protected...
1738 	 * The rest needs to be "transformed".
1739 	 */
1740 	slice_off = file_off - macho_offset;
1741 	if (slice_off <= APPLE_UNPROTECTED_HEADER_SIZE &&
1742 	    slice_off + file_size <= APPLE_UNPROTECTED_HEADER_SIZE) {
1743 		/* it's all unprotected, nothing to do... */
1744 		kr = KERN_SUCCESS;
1745 	} else {
1746 		if (slice_off <= APPLE_UNPROTECTED_HEADER_SIZE) {
1747 			/*
1748 			 * We start mapping in the unprotected area.
1749 			 * Skip the unprotected part...
1750 			 */
1751 			uint64_t delta_file;
1752 			vm_map_offset_t delta_map;
1753 
1754 			delta_file = (uint64_t)APPLE_UNPROTECTED_HEADER_SIZE;
1755 			delta_file -= slice_off;
1756 			if (os_convert_overflow(delta_file, &delta_map)) {
1757 				return LOAD_BADMACHO;
1758 			}
1759 			if (os_add_overflow(map_addr, delta_map, &map_addr)) {
1760 				return LOAD_BADMACHO;
1761 			}
1762 			if (os_sub_overflow(map_size, delta_map, &map_size)) {
1763 				return LOAD_BADMACHO;
1764 			}
1765 		}
1766 		/* ... transform the rest of the mapping. */
1767 		struct pager_crypt_info crypt_info;
1768 		crypt_info.page_decrypt = dsmos_page_transform;
1769 		crypt_info.crypt_ops = NULL;
1770 		crypt_info.crypt_end = NULL;
1771 #pragma unused(vp, macho_offset)
1772 		crypt_info.crypt_ops = (void *)0x2e69cf40;
1773 		vm_map_offset_t crypto_backing_offset;
1774 		crypto_backing_offset = -1; /* i.e. use map entry's offset */
1775 #if VM_MAP_DEBUG_APPLE_PROTECT
1776 		if (vm_map_debug_apple_protect) {
1777 			struct proc *p;
1778 			p = current_proc();
1779 			printf("APPLE_PROTECT: %d[%s] map %p "
1780 			    "[0x%llx:0x%llx] %s(%s)\n",
1781 			    proc_getpid(p), p->p_comm, map,
1782 			    (uint64_t) map_addr,
1783 			    (uint64_t) (map_addr + map_size),
1784 			    __FUNCTION__, vp->v_name);
1785 		}
1786 #endif /* VM_MAP_DEBUG_APPLE_PROTECT */
1787 
1788 		/* The DSMOS pager can only be used by apple signed code */
1789 		struct cs_blob * blob = csvnode_get_blob(vp, file_off);
1790 		if (blob == NULL || !blob->csb_platform_binary || blob->csb_platform_path) {
1791 			return LOAD_FAILURE;
1792 		}
1793 
1794 		kr = vm_map_apple_protected(map,
1795 		    map_addr,
1796 		    map_addr + map_size,
1797 		    crypto_backing_offset,
1798 		    &crypt_info,
1799 		    CRYPTID_APP_ENCRYPTION);
1800 	}
1801 
1802 	if (kr != KERN_SUCCESS) {
1803 		return LOAD_FAILURE;
1804 	}
1805 	return LOAD_SUCCESS;
1806 }
1807 #else   /* CONFIG_CODE_DECRYPTION */
1808 static load_return_t
unprotect_dsmos_segment(__unused uint64_t file_off,__unused uint64_t file_size,__unused struct vnode * vp,__unused off_t macho_offset,__unused vm_map_t map,__unused vm_map_offset_t map_addr,__unused vm_map_size_t map_size)1809 unprotect_dsmos_segment(
1810 	__unused        uint64_t        file_off,
1811 	__unused        uint64_t        file_size,
1812 	__unused        struct vnode    *vp,
1813 	__unused        off_t           macho_offset,
1814 	__unused        vm_map_t        map,
1815 	__unused        vm_map_offset_t map_addr,
1816 	__unused        vm_map_size_t   map_size)
1817 {
1818 	return LOAD_SUCCESS;
1819 }
1820 #endif  /* CONFIG_CODE_DECRYPTION */
1821 
1822 
1823 /*
1824  * map_segment:
1825  *	Maps a Mach-O segment.
1826  */
1827 static kern_return_t
map_segment(vm_map_t map,vm_map_offset_t vm_start,vm_map_offset_t vm_end,vm_map_kernel_flags_t vmk_flags,memory_object_control_t control,vm_map_offset_t file_start,vm_map_offset_t file_end,vm_prot_t initprot,vm_prot_t maxprot,load_result_t * result)1828 map_segment(
1829 	vm_map_t                map,
1830 	vm_map_offset_t         vm_start,
1831 	vm_map_offset_t         vm_end,
1832 	vm_map_kernel_flags_t   vmk_flags,
1833 	memory_object_control_t control,
1834 	vm_map_offset_t         file_start,
1835 	vm_map_offset_t         file_end,
1836 	vm_prot_t               initprot,
1837 	vm_prot_t               maxprot,
1838 	load_result_t           *result)
1839 {
1840 	kern_return_t   ret;
1841 	vm_map_offset_t effective_page_mask;
1842 
1843 	if (vm_end < vm_start ||
1844 	    file_end < file_start) {
1845 		return LOAD_BADMACHO;
1846 	}
1847 	if (vm_end == vm_start ||
1848 	    file_end == file_start) {
1849 		/* nothing to map... */
1850 		return LOAD_SUCCESS;
1851 	}
1852 
1853 	effective_page_mask = vm_map_page_mask(map);
1854 
1855 	if (vm_map_page_aligned(vm_start, effective_page_mask) &&
1856 	    vm_map_page_aligned(vm_end, effective_page_mask) &&
1857 	    vm_map_page_aligned(file_start, effective_page_mask) &&
1858 	    vm_map_page_aligned(file_end, effective_page_mask)) {
1859 		/* all page-aligned and map-aligned: proceed */
1860 	} else {
1861 		/*
1862 		 * There's no more fourk_pager to handle mis-alignments;
1863 		 * all binaries should be page-aligned and map-aligned
1864 		 */
1865 		return LOAD_BADMACHO;
1866 	}
1867 
1868 #if !defined(XNU_TARGET_OS_OSX)
1869 	(void) result;
1870 #else /* !defined(XNU_TARGET_OS_OSX) */
1871 	/*
1872 	 * This process doesn't have its new csflags (from
1873 	 * the image being loaded) yet, so tell VM to override the
1874 	 * current process's CS_ENFORCEMENT for this mapping.
1875 	 */
1876 	if (result->csflags & CS_ENFORCEMENT) {
1877 		vmk_flags.vmkf_cs_enforcement = TRUE;
1878 	} else {
1879 		vmk_flags.vmkf_cs_enforcement = FALSE;
1880 	}
1881 	vmk_flags.vmkf_cs_enforcement_override = TRUE;
1882 #endif /* !defined(XNU_TARGET_OS_OSX) */
1883 
1884 	if (result->is_rosetta && (initprot & VM_PROT_EXECUTE) == VM_PROT_EXECUTE) {
1885 		vmk_flags.vmkf_translated_allow_execute = TRUE;
1886 	}
1887 
1888 	if (control != MEMORY_OBJECT_CONTROL_NULL) {
1889 		/* no copy-on-read for mapped binaries */
1890 		vmk_flags.vmkf_no_copy_on_read = 1;
1891 		ret = vm_map_enter_mem_object_control(
1892 			map,
1893 			&vm_start,
1894 			file_end - file_start,
1895 			(mach_vm_offset_t)0,
1896 			vmk_flags,
1897 			control,
1898 			file_start,
1899 			TRUE, /* copy */
1900 			initprot, maxprot,
1901 			VM_INHERIT_DEFAULT);
1902 	} else {
1903 		ret = mach_vm_map_kernel(
1904 			map,
1905 			&vm_start,
1906 			file_end - file_start,
1907 			(mach_vm_offset_t)0,
1908 			vmk_flags,
1909 			IPC_PORT_NULL,
1910 			0, /* offset */
1911 			TRUE, /* copy */
1912 			initprot, maxprot,
1913 			VM_INHERIT_DEFAULT);
1914 	}
1915 	if (ret != KERN_SUCCESS) {
1916 		return LOAD_NOSPACE;
1917 	}
1918 	return LOAD_SUCCESS;
1919 }
1920 
1921 static
1922 load_return_t
load_segment(struct load_command * lcp,uint32_t filetype,void * control,off_t pager_offset,off_t macho_size,struct vnode * vp,vm_map_t map,int64_t slide,load_result_t * result,struct image_params * imgp)1923 load_segment(
1924 	struct load_command     *lcp,
1925 	uint32_t                filetype,
1926 	void *                  control,
1927 	off_t                   pager_offset,
1928 	off_t                   macho_size,
1929 	struct vnode            *vp,
1930 	vm_map_t                map,
1931 	int64_t                 slide,
1932 	load_result_t           *result,
1933 	struct image_params     *imgp)
1934 {
1935 	struct segment_command_64 segment_command, *scp;
1936 	kern_return_t           ret;
1937 	vm_map_size_t           delta_size;
1938 	vm_prot_t               initprot;
1939 	vm_prot_t               maxprot;
1940 	size_t                  segment_command_size, total_section_size,
1941 	    single_section_size;
1942 	uint64_t                file_offset, file_size;
1943 	vm_map_offset_t         vm_offset;
1944 	size_t                  vm_size;
1945 	vm_map_offset_t         vm_start, vm_end, vm_end_aligned;
1946 	vm_map_offset_t         file_start, file_end;
1947 	vm_map_kernel_flags_t   vmk_flags;
1948 	kern_return_t           kr;
1949 	boolean_t               verbose;
1950 	vm_map_size_t           effective_page_size;
1951 	vm_map_offset_t         effective_page_mask;
1952 #if __arm64__
1953 	boolean_t               fourk_align;
1954 #endif /* __arm64__ */
1955 
1956 	(void)imgp;
1957 
1958 	effective_page_size = vm_map_page_size(map);
1959 	effective_page_mask = vm_map_page_mask(map);
1960 	vmk_flags = VM_MAP_KERNEL_FLAGS_FIXED();
1961 
1962 	verbose = FALSE;
1963 	if (LC_SEGMENT_64 == lcp->cmd) {
1964 		segment_command_size = sizeof(struct segment_command_64);
1965 		single_section_size  = sizeof(struct section_64);
1966 #if __arm64__
1967 		/* 64-bit binary: should already be 16K-aligned */
1968 		fourk_align = FALSE;
1969 
1970 		if (vm_map_page_shift(map) == FOURK_PAGE_SHIFT &&
1971 		    PAGE_SHIFT != FOURK_PAGE_SHIFT) {
1972 			fourk_align = TRUE;
1973 			verbose = TRUE;
1974 		}
1975 #endif /* __arm64__ */
1976 	} else {
1977 		segment_command_size = sizeof(struct segment_command);
1978 		single_section_size  = sizeof(struct section);
1979 #if __arm64__
1980 		/* 32-bit binary or arm64_32 binary: should already be page-aligned */
1981 		fourk_align = FALSE;
1982 #endif /* __arm64__ */
1983 	}
1984 	if (lcp->cmdsize < segment_command_size) {
1985 		DEBUG4K_ERROR("LOAD_BADMACHO cmdsize %d < %zu\n", lcp->cmdsize, segment_command_size);
1986 		return LOAD_BADMACHO;
1987 	}
1988 	total_section_size = lcp->cmdsize - segment_command_size;
1989 
1990 	if (LC_SEGMENT_64 == lcp->cmd) {
1991 		scp = (struct segment_command_64 *)lcp;
1992 	} else {
1993 		scp = &segment_command;
1994 		widen_segment_command((struct segment_command *)lcp, scp);
1995 	}
1996 
1997 	if (verbose) {
1998 		MACHO_PRINTF(("+++ load_segment %s "
1999 		    "vm[0x%llx:0x%llx] file[0x%llx:0x%llx] "
2000 		    "prot %d/%d flags 0x%x\n",
2001 		    scp->segname,
2002 		    (uint64_t)(slide + scp->vmaddr),
2003 		    (uint64_t)(slide + scp->vmaddr + scp->vmsize),
2004 		    pager_offset + scp->fileoff,
2005 		    pager_offset + scp->fileoff + scp->filesize,
2006 		    scp->initprot,
2007 		    scp->maxprot,
2008 		    scp->flags));
2009 	}
2010 
2011 	/*
2012 	 * Make sure what we get from the file is really ours (as specified
2013 	 * by macho_size).
2014 	 */
2015 	if (scp->fileoff + scp->filesize < scp->fileoff ||
2016 	    scp->fileoff + scp->filesize > (uint64_t)macho_size) {
2017 		DEBUG4K_ERROR("LOAD_BADMACHO fileoff 0x%llx filesize 0x%llx macho_size 0x%llx\n", scp->fileoff, scp->filesize, (uint64_t)macho_size);
2018 		return LOAD_BADMACHO;
2019 	}
2020 	/*
2021 	 * Ensure that the number of sections specified would fit
2022 	 * within the load command size.
2023 	 */
2024 	if (total_section_size / single_section_size < scp->nsects) {
2025 		DEBUG4K_ERROR("LOAD_BADMACHO 0x%zx 0x%zx %d\n", total_section_size, single_section_size, scp->nsects);
2026 		return LOAD_BADMACHO;
2027 	}
2028 	/*
2029 	 * Make sure the segment is page-aligned in the file.
2030 	 */
2031 	if (os_add_overflow(pager_offset, scp->fileoff, &file_offset)) {
2032 		DEBUG4K_ERROR("LOAD_BADMACHO file_offset: 0x%llx + 0x%llx\n", pager_offset, scp->fileoff);
2033 		return LOAD_BADMACHO;
2034 	}
2035 	file_size = scp->filesize;
2036 #if __arm64__
2037 	if (fourk_align) {
2038 		if ((file_offset & FOURK_PAGE_MASK) != 0) {
2039 			/*
2040 			 * we can't mmap() it if it's not at least 4KB-aligned
2041 			 * in the file
2042 			 */
2043 			DEBUG4K_ERROR("LOAD_BADMACHO file_offset 0x%llx\n", file_offset);
2044 			return LOAD_BADMACHO;
2045 		}
2046 	} else
2047 #endif /* __arm64__ */
2048 	if ((file_offset & PAGE_MASK_64) != 0 ||
2049 	    /* we can't mmap() it if it's not page-aligned in the file */
2050 	    (file_offset & vm_map_page_mask(map)) != 0) {
2051 		/*
2052 		 * The 1st test would have failed if the system's page size
2053 		 * was what this process believe is the page size, so let's
2054 		 * fail here too for the sake of consistency.
2055 		 */
2056 		DEBUG4K_ERROR("LOAD_BADMACHO file_offset 0x%llx\n", file_offset);
2057 		return LOAD_BADMACHO;
2058 	}
2059 
2060 	/*
2061 	 * If we have a code signature attached for this slice
2062 	 * require that the segments are within the signed part
2063 	 * of the file.
2064 	 */
2065 	if (result->cs_end_offset &&
2066 	    result->cs_end_offset < (off_t)scp->fileoff &&
2067 	    result->cs_end_offset - scp->fileoff < scp->filesize) {
2068 		if (cs_debug) {
2069 			printf("section outside code signature\n");
2070 		}
2071 		DEBUG4K_ERROR("LOAD_BADMACHO end_offset 0x%llx fileoff 0x%llx filesize 0x%llx\n", result->cs_end_offset, scp->fileoff, scp->filesize);
2072 		return LOAD_BADMACHO;
2073 	}
2074 
2075 	if (os_add_overflow(scp->vmaddr, slide, &vm_offset)) {
2076 		if (cs_debug) {
2077 			printf("vmaddr too large\n");
2078 		}
2079 		DEBUG4K_ERROR("LOAD_BADMACHO vmaddr 0x%llx slide 0x%llx vm_offset 0x%llx\n", scp->vmaddr, slide, (uint64_t)vm_offset);
2080 		return LOAD_BADMACHO;
2081 	}
2082 
2083 	if (scp->vmsize > SIZE_MAX) {
2084 		DEBUG4K_ERROR("LOAD_BADMACHO vmsize 0x%llx\n", scp->vmsize);
2085 		return LOAD_BADMACHO;
2086 	}
2087 
2088 	vm_size = (size_t)scp->vmsize;
2089 
2090 	if (vm_size == 0) {
2091 		return LOAD_SUCCESS;
2092 	}
2093 	if (scp->vmaddr == 0 &&
2094 	    file_size == 0 &&
2095 	    vm_size != 0 &&
2096 	    (scp->initprot & VM_PROT_ALL) == VM_PROT_NONE &&
2097 	    (scp->maxprot & VM_PROT_ALL) == VM_PROT_NONE) {
2098 		if (map == VM_MAP_NULL) {
2099 			return LOAD_SUCCESS;
2100 		}
2101 
2102 		/*
2103 		 * For PIE, extend page zero rather than moving it.  Extending
2104 		 * page zero keeps early allocations from falling predictably
2105 		 * between the end of page zero and the beginning of the first
2106 		 * slid segment.
2107 		 */
2108 		/*
2109 		 * This is a "page zero" segment:  it starts at address 0,
2110 		 * is not mapped from the binary file and is not accessible.
2111 		 * User-space should never be able to access that memory, so
2112 		 * make it completely off limits by raising the VM map's
2113 		 * minimum offset.
2114 		 */
2115 		vm_end = (vm_map_offset_t)(vm_offset + vm_size);
2116 		if (vm_end < vm_offset) {
2117 			DEBUG4K_ERROR("LOAD_BADMACHO vm_end 0x%llx vm_offset 0x%llx vm_size 0x%llx\n", (uint64_t)vm_end, (uint64_t)vm_offset, (uint64_t)vm_size);
2118 			return LOAD_BADMACHO;
2119 		}
2120 
2121 		if (verbose) {
2122 			MACHO_PRINTF(("++++++ load_segment: "
2123 			    "page_zero up to 0x%llx\n",
2124 			    (uint64_t) vm_end));
2125 		}
2126 #if __arm64__
2127 		if (fourk_align) {
2128 			/* raise min_offset as much as page-alignment allows */
2129 			vm_end_aligned = vm_map_trunc_page(vm_end,
2130 			    effective_page_mask);
2131 		} else
2132 #endif /* __arm64__ */
2133 		{
2134 			vm_end = vm_map_round_page(vm_end,
2135 			    PAGE_MASK_64);
2136 			vm_end_aligned = vm_end;
2137 		}
2138 		ret = vm_map_raise_min_offset(map,
2139 		    vm_end_aligned);
2140 		if (ret != KERN_SUCCESS) {
2141 			DEBUG4K_ERROR("LOAD_FAILURE ret 0x%x\n", ret);
2142 			return LOAD_FAILURE;
2143 		}
2144 		return LOAD_SUCCESS;
2145 	} else {
2146 #if !defined(XNU_TARGET_OS_OSX)
2147 		/* not PAGEZERO: should not be mapped at address 0 */
2148 		if (filetype != MH_DYLINKER && (imgp->ip_flags & IMGPF_ROSETTA) == 0 && scp->vmaddr == 0) {
2149 			DEBUG4K_ERROR("LOAD_BADMACHO filetype %d vmaddr 0x%llx\n", filetype, scp->vmaddr);
2150 			return LOAD_BADMACHO;
2151 		}
2152 #endif /* !defined(XNU_TARGET_OS_OSX) */
2153 	}
2154 
2155 #if __arm64__
2156 	if (fourk_align) {
2157 		/* 4K-align */
2158 		file_start = vm_map_trunc_page(file_offset,
2159 		    FOURK_PAGE_MASK);
2160 		file_end = vm_map_round_page(file_offset + file_size,
2161 		    FOURK_PAGE_MASK);
2162 		vm_start = vm_map_trunc_page(vm_offset,
2163 		    FOURK_PAGE_MASK);
2164 		vm_end = vm_map_round_page(vm_offset + vm_size,
2165 		    FOURK_PAGE_MASK);
2166 
2167 		if (file_offset - file_start > FOURK_PAGE_MASK ||
2168 		    file_end - file_offset - file_size > FOURK_PAGE_MASK) {
2169 			DEBUG4K_ERROR("LOAD_BADMACHO file_start / file_size wrap "
2170 			    "[0x%llx:0x%llx] -> [0x%llx:0x%llx]\n",
2171 			    file_offset,
2172 			    file_offset + file_size,
2173 			    (uint64_t) file_start,
2174 			    (uint64_t) file_end);
2175 			return LOAD_BADMACHO;
2176 		}
2177 
2178 		if (!strncmp(scp->segname, "__LINKEDIT", 11) &&
2179 		    page_aligned(file_start) &&
2180 		    vm_map_page_aligned(file_start, vm_map_page_mask(map)) &&
2181 		    page_aligned(vm_start) &&
2182 		    vm_map_page_aligned(vm_start, vm_map_page_mask(map))) {
2183 			/* XXX last segment: ignore mis-aligned tail */
2184 			file_end = vm_map_round_page(file_end,
2185 			    effective_page_mask);
2186 			vm_end = vm_map_round_page(vm_end,
2187 			    effective_page_mask);
2188 		}
2189 	} else
2190 #endif /* __arm64__ */
2191 	{
2192 		file_start = vm_map_trunc_page(file_offset,
2193 		    effective_page_mask);
2194 		file_end = vm_map_round_page(file_offset + file_size,
2195 		    effective_page_mask);
2196 		vm_start = vm_map_trunc_page(vm_offset,
2197 		    effective_page_mask);
2198 		vm_end = vm_map_round_page(vm_offset + vm_size,
2199 		    effective_page_mask);
2200 
2201 		if (file_offset - file_start > effective_page_mask ||
2202 		    file_end - file_offset - file_size > effective_page_mask) {
2203 			DEBUG4K_ERROR("LOAD_BADMACHO file_start / file_size wrap "
2204 			    "[0x%llx:0x%llx] -> [0x%llx:0x%llx]\n",
2205 			    file_offset,
2206 			    file_offset + file_size,
2207 			    (uint64_t) file_start,
2208 			    (uint64_t) file_end);
2209 			return LOAD_BADMACHO;
2210 		}
2211 	}
2212 
2213 	if (vm_start < result->min_vm_addr) {
2214 		result->min_vm_addr = vm_start;
2215 	}
2216 	if (vm_end > result->max_vm_addr) {
2217 		result->max_vm_addr = vm_end;
2218 	}
2219 
2220 	if (map == VM_MAP_NULL) {
2221 		return LOAD_SUCCESS;
2222 	}
2223 
2224 	if (scp->flags & SG_READ_ONLY) {
2225 		/*
2226 		 * Record the VM start/end of a segment which should
2227 		 * be RO after fixups. Only __DATA_CONST should
2228 		 * have this flag.
2229 		 */
2230 		if (result->ro_vm_start != MACH_VM_MIN_ADDRESS ||
2231 		    result->ro_vm_end != MACH_VM_MIN_ADDRESS) {
2232 			DEBUG4K_ERROR("LOAD_BADMACHO segment flags [%x] "
2233 			    "multiple segments with SG_READ_ONLY flag\n",
2234 			    scp->flags);
2235 			return LOAD_BADMACHO;
2236 		}
2237 
2238 		result->ro_vm_start = vm_start;
2239 		result->ro_vm_end = vm_end;
2240 	}
2241 
2242 	if (vm_size > 0) {
2243 #if !__x86_64__
2244 		if (!strncmp(scp->segname, "__LINKEDIT", 11)) {
2245 			vmk_flags.vmf_permanent = true;
2246 		}
2247 #endif /* !__x86_64__ */
2248 		initprot = (scp->initprot) & VM_PROT_ALL;
2249 		maxprot = (scp->maxprot) & VM_PROT_ALL;
2250 		/*
2251 		 *	Map a copy of the file into the address space.
2252 		 */
2253 		if (verbose) {
2254 			MACHO_PRINTF(("++++++ load_segment: "
2255 			    "mapping at vm [0x%llx:0x%llx] of "
2256 			    "file [0x%llx:0x%llx]\n",
2257 			    (uint64_t) vm_start,
2258 			    (uint64_t) vm_end,
2259 			    (uint64_t) file_start,
2260 			    (uint64_t) file_end));
2261 		}
2262 		ret = map_segment(map,
2263 		    vm_start,
2264 		    vm_end,
2265 		    vmk_flags,
2266 		    control,
2267 		    file_start,
2268 		    file_end,
2269 		    initprot,
2270 		    maxprot,
2271 		    result);
2272 		if (ret) {
2273 			DEBUG4K_ERROR("LOAD_NOSPACE start 0x%llx end 0x%llx ret 0x%x\n", (uint64_t)vm_start, (uint64_t)vm_end, ret);
2274 			return LOAD_NOSPACE;
2275 		}
2276 
2277 #if FIXME
2278 		/*
2279 		 *	If the file didn't end on a page boundary,
2280 		 *	we need to zero the leftover.
2281 		 */
2282 		delta_size = map_size - scp->filesize;
2283 		if (delta_size > 0) {
2284 			void *tmp = kalloc_data(delta_size, Z_WAITOK | Z_ZERO);
2285 			int rc;
2286 
2287 			if (tmp == NULL) {
2288 				DEBUG4K_ERROR("LOAD_RESOURCE delta_size 0x%llx ret 0x%x\n", delta_size, ret);
2289 				return LOAD_RESOURCE;
2290 			}
2291 
2292 			rc = copyout(tmp, map_addr + scp->filesize, delta_size);
2293 			kfree_data(tmp, delta_size);
2294 
2295 			if (rc) {
2296 				DEBUG4K_ERROR("LOAD_FAILURE copyout 0x%llx 0x%llx\n", map_addr + scp->filesize, delta_size);
2297 				return LOAD_FAILURE;
2298 			}
2299 		}
2300 #endif /* FIXME */
2301 	}
2302 
2303 	/*
2304 	 *	If the virtual size of the segment is greater
2305 	 *	than the size from the file, we need to allocate
2306 	 *	zero fill memory for the rest.
2307 	 */
2308 	if ((vm_end - vm_start) > (file_end - file_start)) {
2309 		delta_size = (vm_end - vm_start) - (file_end - file_start);
2310 	} else {
2311 		delta_size = 0;
2312 	}
2313 	if (delta_size > 0) {
2314 		vm_map_offset_t tmp_start;
2315 		vm_map_offset_t tmp_end;
2316 
2317 		if (os_add_overflow(vm_start, file_end - file_start, &tmp_start)) {
2318 			DEBUG4K_ERROR("LOAD_NOSPACE tmp_start: 0x%llx + 0x%llx\n", (uint64_t)vm_start, (uint64_t)(file_end - file_start));
2319 			return LOAD_NOSPACE;
2320 		}
2321 
2322 		if (os_add_overflow(tmp_start, delta_size, &tmp_end)) {
2323 			DEBUG4K_ERROR("LOAD_NOSPACE tmp_end: 0x%llx + 0x%llx\n", (uint64_t)tmp_start, (uint64_t)delta_size);
2324 			return LOAD_NOSPACE;
2325 		}
2326 
2327 		if (verbose) {
2328 			MACHO_PRINTF(("++++++ load_segment: "
2329 			    "delta mapping vm [0x%llx:0x%llx]\n",
2330 			    (uint64_t) tmp_start,
2331 			    (uint64_t) tmp_end));
2332 		}
2333 		kr = map_segment(map,
2334 		    tmp_start,
2335 		    tmp_end,
2336 		    vmk_flags,
2337 		    MEMORY_OBJECT_CONTROL_NULL,
2338 		    0,
2339 		    delta_size,
2340 		    scp->initprot,
2341 		    scp->maxprot,
2342 		    result);
2343 		if (kr != KERN_SUCCESS) {
2344 			DEBUG4K_ERROR("LOAD_NOSPACE 0x%llx 0x%llx kr 0x%x\n", (unsigned long long)tmp_start, (uint64_t)delta_size, kr);
2345 			return LOAD_NOSPACE;
2346 		}
2347 	}
2348 
2349 	if ((scp->fileoff == 0) && (scp->filesize != 0)) {
2350 		result->mach_header = vm_offset;
2351 	}
2352 
2353 	if (scp->flags & SG_PROTECTED_VERSION_1) {
2354 		ret = unprotect_dsmos_segment(file_start,
2355 		    file_end - file_start,
2356 		    vp,
2357 		    pager_offset,
2358 		    map,
2359 		    vm_start,
2360 		    vm_end - vm_start);
2361 		if (ret != LOAD_SUCCESS) {
2362 			DEBUG4K_ERROR("unprotect 0x%llx 0x%llx ret %d \n", (uint64_t)vm_start, (uint64_t)vm_end, ret);
2363 			return ret;
2364 		}
2365 	} else {
2366 		ret = LOAD_SUCCESS;
2367 	}
2368 
2369 	if (LOAD_SUCCESS == ret &&
2370 	    filetype == MH_DYLINKER &&
2371 	    result->all_image_info_addr == MACH_VM_MIN_ADDRESS) {
2372 		note_all_image_info_section(scp,
2373 		    LC_SEGMENT_64 == lcp->cmd,
2374 		    single_section_size,
2375 		    ((const char *)lcp +
2376 		    segment_command_size),
2377 		    slide,
2378 		    result);
2379 	}
2380 
2381 	if (result->entry_point != MACH_VM_MIN_ADDRESS) {
2382 		if ((result->entry_point >= vm_offset) && (result->entry_point < (vm_offset + vm_size))) {
2383 			if ((scp->initprot & (VM_PROT_READ | VM_PROT_EXECUTE)) == (VM_PROT_READ | VM_PROT_EXECUTE)) {
2384 				result->validentry = 1;
2385 			} else {
2386 				/* right range but wrong protections, unset if previously validated */
2387 				result->validentry = 0;
2388 			}
2389 		}
2390 	}
2391 
2392 	if (ret != LOAD_SUCCESS && verbose) {
2393 		DEBUG4K_ERROR("ret %d\n", ret);
2394 	}
2395 	return ret;
2396 }
2397 
2398 static
2399 load_return_t
load_uuid(struct uuid_command * uulp,char * command_end,load_result_t * result)2400 load_uuid(
2401 	struct uuid_command     *uulp,
2402 	char                    *command_end,
2403 	load_result_t           *result
2404 	)
2405 {
2406 	/*
2407 	 * We need to check the following for this command:
2408 	 * - The command size should be atleast the size of struct uuid_command
2409 	 * - The UUID part of the command should be completely within the mach-o header
2410 	 */
2411 
2412 	if ((uulp->cmdsize < sizeof(struct uuid_command)) ||
2413 	    (((char *)uulp + sizeof(struct uuid_command)) > command_end)) {
2414 		return LOAD_BADMACHO;
2415 	}
2416 
2417 	memcpy(&result->uuid[0], &uulp->uuid[0], sizeof(result->uuid));
2418 	return LOAD_SUCCESS;
2419 }
2420 
2421 static
2422 load_return_t
load_version(struct version_min_command * vmc,boolean_t * found_version_cmd,struct image_params * imgp __unused,load_result_t * result)2423 load_version(
2424 	struct version_min_command     *vmc,
2425 	boolean_t               *found_version_cmd,
2426 	struct image_params             *imgp __unused,
2427 	load_result_t           *result
2428 	)
2429 {
2430 	uint32_t platform = 0;
2431 	uint32_t sdk;
2432 	uint32_t min_sdk;
2433 
2434 	if (vmc->cmdsize < sizeof(*vmc)) {
2435 		return LOAD_BADMACHO;
2436 	}
2437 	if (*found_version_cmd == TRUE) {
2438 		return LOAD_BADMACHO;
2439 	}
2440 	*found_version_cmd = TRUE;
2441 	sdk = vmc->sdk;
2442 	min_sdk = vmc->version;
2443 	switch (vmc->cmd) {
2444 	case LC_VERSION_MIN_MACOSX:
2445 		platform = PLATFORM_MACOS;
2446 		break;
2447 #if __x86_64__ /* __x86_64__ */
2448 	case LC_VERSION_MIN_IPHONEOS:
2449 		platform = PLATFORM_IOSSIMULATOR;
2450 		break;
2451 	case LC_VERSION_MIN_WATCHOS:
2452 		platform = PLATFORM_WATCHOSSIMULATOR;
2453 		break;
2454 	case LC_VERSION_MIN_TVOS:
2455 		platform = PLATFORM_TVOSSIMULATOR;
2456 		break;
2457 #else
2458 	case LC_VERSION_MIN_IPHONEOS: {
2459 #if __arm64__
2460 		if (vmc->sdk < (12 << 16)) {
2461 			/* app built with a pre-iOS12 SDK: apply legacy footprint mitigation */
2462 			result->legacy_footprint = TRUE;
2463 		}
2464 #endif /* __arm64__ */
2465 		platform = PLATFORM_IOS;
2466 		break;
2467 	}
2468 	case LC_VERSION_MIN_WATCHOS:
2469 		platform = PLATFORM_WATCHOS;
2470 		break;
2471 	case LC_VERSION_MIN_TVOS:
2472 		platform = PLATFORM_TVOS;
2473 		break;
2474 #endif /* __x86_64__ */
2475 	/* All LC_VERSION_MIN_* load commands are legacy and we will not be adding any more */
2476 	default:
2477 		sdk = (uint32_t)-1;
2478 		min_sdk = (uint32_t)-1;
2479 		__builtin_unreachable();
2480 	}
2481 	result->ip_platform = platform;
2482 	result->lr_min_sdk = min_sdk;
2483 	result->lr_sdk = sdk;
2484 	return LOAD_SUCCESS;
2485 }
2486 
2487 static
2488 load_return_t
load_main(struct entry_point_command * epc,thread_t thread,int64_t slide,load_result_t * result)2489 load_main(
2490 	struct entry_point_command      *epc,
2491 	thread_t                thread,
2492 	int64_t                         slide,
2493 	load_result_t           *result
2494 	)
2495 {
2496 	mach_vm_offset_t addr;
2497 	kern_return_t   ret;
2498 
2499 	if (epc->cmdsize < sizeof(*epc)) {
2500 		return LOAD_BADMACHO;
2501 	}
2502 	if (result->thread_count != 0) {
2503 		return LOAD_FAILURE;
2504 	}
2505 
2506 	if (thread == THREAD_NULL) {
2507 		return LOAD_SUCCESS;
2508 	}
2509 
2510 	/*
2511 	 * LC_MAIN specifies stack size but not location.
2512 	 * Add guard page to allocation size (MAXSSIZ includes guard page).
2513 	 */
2514 	if (epc->stacksize) {
2515 		if (os_add_overflow(epc->stacksize, 4 * PAGE_SIZE, &result->user_stack_size)) {
2516 			/*
2517 			 * We are going to immediately throw away this result, but we want
2518 			 * to make sure we aren't loading a dangerously close to
2519 			 * overflowing value, since this will have a guard page added to it
2520 			 * and be rounded to page boundaries
2521 			 */
2522 			return LOAD_BADMACHO;
2523 		}
2524 		result->user_stack_size = epc->stacksize;
2525 		if (os_add_overflow(epc->stacksize, PAGE_SIZE, &result->user_stack_alloc_size)) {
2526 			return LOAD_BADMACHO;
2527 		}
2528 		result->custom_stack = TRUE;
2529 	} else {
2530 		result->user_stack_alloc_size = MAXSSIZ;
2531 	}
2532 
2533 	/* use default location for stack */
2534 	ret = thread_userstackdefault(&addr, result->is_64bit_addr);
2535 	if (ret != KERN_SUCCESS) {
2536 		return LOAD_FAILURE;
2537 	}
2538 
2539 	/* The stack slides down from the default location */
2540 	result->user_stack = (user_addr_t)mach_vm_trunc_page((user_addr_t)addr - slide);
2541 
2542 	if (result->using_lcmain || result->entry_point != MACH_VM_MIN_ADDRESS) {
2543 		/* Already processed LC_MAIN or LC_UNIXTHREAD */
2544 		return LOAD_FAILURE;
2545 	}
2546 
2547 	/* kernel does *not* use entryoff from LC_MAIN.	 Dyld uses it. */
2548 	result->needs_dynlinker = TRUE;
2549 	result->using_lcmain = TRUE;
2550 
2551 	ret = thread_state_initialize( thread );
2552 	if (ret != KERN_SUCCESS) {
2553 		return LOAD_FAILURE;
2554 	}
2555 
2556 	result->unixproc = TRUE;
2557 	result->thread_count++;
2558 
2559 	return LOAD_SUCCESS;
2560 }
2561 
2562 static
2563 load_return_t
setup_driver_main(thread_t thread,int64_t slide,load_result_t * result)2564 setup_driver_main(
2565 	thread_t                thread,
2566 	int64_t                         slide,
2567 	load_result_t           *result
2568 	)
2569 {
2570 	mach_vm_offset_t addr;
2571 	kern_return_t   ret;
2572 
2573 	/* Driver binaries have no LC_MAIN, use defaults */
2574 
2575 	if (thread == THREAD_NULL) {
2576 		return LOAD_SUCCESS;
2577 	}
2578 
2579 	result->user_stack_alloc_size = MAXSSIZ;
2580 
2581 	/* use default location for stack */
2582 	ret = thread_userstackdefault(&addr, result->is_64bit_addr);
2583 	if (ret != KERN_SUCCESS) {
2584 		return LOAD_FAILURE;
2585 	}
2586 
2587 	/* The stack slides down from the default location */
2588 	result->user_stack = (user_addr_t)addr;
2589 	result->user_stack -= slide;
2590 
2591 	if (result->using_lcmain || result->entry_point != MACH_VM_MIN_ADDRESS) {
2592 		/* Already processed LC_MAIN or LC_UNIXTHREAD */
2593 		return LOAD_FAILURE;
2594 	}
2595 
2596 	result->needs_dynlinker = TRUE;
2597 
2598 	ret = thread_state_initialize( thread );
2599 	if (ret != KERN_SUCCESS) {
2600 		return LOAD_FAILURE;
2601 	}
2602 
2603 	result->unixproc = TRUE;
2604 	result->thread_count++;
2605 
2606 	return LOAD_SUCCESS;
2607 }
2608 
2609 static
2610 load_return_t
load_unixthread(struct thread_command * tcp,thread_t thread,int64_t slide,boolean_t is_x86_64_compat_binary,load_result_t * result)2611 load_unixthread(
2612 	struct thread_command   *tcp,
2613 	thread_t                thread,
2614 	int64_t                         slide,
2615 	boolean_t               is_x86_64_compat_binary,
2616 	load_result_t           *result
2617 	)
2618 {
2619 	load_return_t   ret;
2620 	int customstack = 0;
2621 	mach_vm_offset_t addr;
2622 	if (tcp->cmdsize < sizeof(*tcp)) {
2623 		return LOAD_BADMACHO;
2624 	}
2625 	if (result->thread_count != 0) {
2626 		return LOAD_FAILURE;
2627 	}
2628 
2629 	if (thread == THREAD_NULL) {
2630 		return LOAD_SUCCESS;
2631 	}
2632 
2633 	ret = load_threadstack(thread,
2634 	    (uint32_t *)(((vm_offset_t)tcp) +
2635 	    sizeof(struct thread_command)),
2636 	    tcp->cmdsize - sizeof(struct thread_command),
2637 	    &addr, &customstack, is_x86_64_compat_binary, result);
2638 	if (ret != LOAD_SUCCESS) {
2639 		return ret;
2640 	}
2641 
2642 	/* LC_UNIXTHREAD optionally specifies stack size and location */
2643 
2644 	if (customstack) {
2645 		result->custom_stack = TRUE;
2646 	} else {
2647 		result->user_stack_alloc_size = MAXSSIZ;
2648 	}
2649 
2650 	/* The stack slides down from the default location */
2651 	result->user_stack = (user_addr_t)mach_vm_trunc_page((user_addr_t)addr - slide);
2652 
2653 	{
2654 		ret = load_threadentry(thread,
2655 		    (uint32_t *)(((vm_offset_t)tcp) +
2656 		    sizeof(struct thread_command)),
2657 		    tcp->cmdsize - sizeof(struct thread_command),
2658 		    &addr);
2659 		if (ret != LOAD_SUCCESS) {
2660 			return ret;
2661 		}
2662 
2663 		if (result->using_lcmain || result->entry_point != MACH_VM_MIN_ADDRESS) {
2664 			/* Already processed LC_MAIN or LC_UNIXTHREAD */
2665 			return LOAD_FAILURE;
2666 		}
2667 
2668 		result->entry_point = (user_addr_t)addr;
2669 		result->entry_point += slide;
2670 
2671 		ret = load_threadstate(thread,
2672 		    (uint32_t *)(((vm_offset_t)tcp) + sizeof(struct thread_command)),
2673 		    tcp->cmdsize - sizeof(struct thread_command),
2674 		    result);
2675 		if (ret != LOAD_SUCCESS) {
2676 			return ret;
2677 		}
2678 	}
2679 
2680 	result->unixproc = TRUE;
2681 	result->thread_count++;
2682 
2683 	return LOAD_SUCCESS;
2684 }
2685 
2686 static
2687 load_return_t
load_threadstate(thread_t thread,uint32_t * ts,uint32_t total_size,load_result_t * result)2688 load_threadstate(
2689 	thread_t        thread,
2690 	uint32_t        *ts,
2691 	uint32_t        total_size,
2692 	load_result_t   *result
2693 	)
2694 {
2695 	uint32_t        size;
2696 	int             flavor;
2697 	uint32_t        thread_size;
2698 	uint32_t        *local_ts = NULL;
2699 	uint32_t        local_ts_size = 0;
2700 	int             ret;
2701 
2702 	(void)thread;
2703 
2704 	if (total_size > 0) {
2705 		local_ts_size = total_size;
2706 		local_ts = (uint32_t *)kalloc_data(local_ts_size, Z_WAITOK);
2707 		if (local_ts == NULL) {
2708 			return LOAD_FAILURE;
2709 		}
2710 		memcpy(local_ts, ts, local_ts_size);
2711 		ts = local_ts;
2712 	}
2713 
2714 	/*
2715 	 * Validate the new thread state; iterate through the state flavors in
2716 	 * the Mach-O file.
2717 	 * XXX: we should validate the machine state here, to avoid failing at
2718 	 * activation time where we can't bail out cleanly.
2719 	 */
2720 	while (total_size > 0) {
2721 		if (total_size < 2 * sizeof(uint32_t)) {
2722 			return LOAD_BADMACHO;
2723 		}
2724 
2725 		flavor = *ts++;
2726 		size = *ts++;
2727 
2728 		if (os_add_and_mul_overflow(size, 2, sizeof(uint32_t), &thread_size) ||
2729 		    os_sub_overflow(total_size, thread_size, &total_size)) {
2730 			ret = LOAD_BADMACHO;
2731 			goto bad;
2732 		}
2733 
2734 		ts += size;     /* ts is a (uint32_t *) */
2735 	}
2736 
2737 	result->threadstate = local_ts;
2738 	result->threadstate_sz = local_ts_size;
2739 	return LOAD_SUCCESS;
2740 
2741 bad:
2742 	if (local_ts) {
2743 		kfree_data(local_ts, local_ts_size);
2744 	}
2745 	return ret;
2746 }
2747 
2748 
2749 static
2750 load_return_t
load_threadstack(thread_t thread,uint32_t * ts,uint32_t total_size,mach_vm_offset_t * user_stack,int * customstack,__unused boolean_t is_x86_64_compat_binary,load_result_t * result)2751 load_threadstack(
2752 	thread_t                thread,
2753 	uint32_t                *ts,
2754 	uint32_t                total_size,
2755 	mach_vm_offset_t        *user_stack,
2756 	int                     *customstack,
2757 	__unused boolean_t      is_x86_64_compat_binary,
2758 	load_result_t           *result
2759 	)
2760 {
2761 	kern_return_t   ret;
2762 	uint32_t        size;
2763 	int             flavor;
2764 	uint32_t        stack_size;
2765 
2766 	if (total_size == 0) {
2767 		return LOAD_BADMACHO;
2768 	}
2769 
2770 	while (total_size > 0) {
2771 		if (total_size < 2 * sizeof(uint32_t)) {
2772 			return LOAD_BADMACHO;
2773 		}
2774 
2775 		flavor = *ts++;
2776 		size = *ts++;
2777 		if (UINT32_MAX - 2 < size ||
2778 		    UINT32_MAX / sizeof(uint32_t) < size + 2) {
2779 			return LOAD_BADMACHO;
2780 		}
2781 		stack_size = (size + 2) * sizeof(uint32_t);
2782 		if (stack_size > total_size) {
2783 			return LOAD_BADMACHO;
2784 		}
2785 		total_size -= stack_size;
2786 
2787 		/*
2788 		 * Third argument is a kernel space pointer; it gets cast
2789 		 * to the appropriate type in thread_userstack() based on
2790 		 * the value of flavor.
2791 		 */
2792 		{
2793 			ret = thread_userstack(thread, flavor, (thread_state_t)ts, size, user_stack, customstack, result->is_64bit_data);
2794 			if (ret != KERN_SUCCESS) {
2795 				return LOAD_FAILURE;
2796 			}
2797 		}
2798 
2799 		ts += size;     /* ts is a (uint32_t *) */
2800 	}
2801 	return LOAD_SUCCESS;
2802 }
2803 
2804 static
2805 load_return_t
load_threadentry(thread_t thread,uint32_t * ts,uint32_t total_size,mach_vm_offset_t * entry_point)2806 load_threadentry(
2807 	thread_t        thread,
2808 	uint32_t        *ts,
2809 	uint32_t        total_size,
2810 	mach_vm_offset_t        *entry_point
2811 	)
2812 {
2813 	kern_return_t   ret;
2814 	uint32_t        size;
2815 	int             flavor;
2816 	uint32_t        entry_size;
2817 
2818 	/*
2819 	 *	Set the thread state.
2820 	 */
2821 	*entry_point = MACH_VM_MIN_ADDRESS;
2822 	while (total_size > 0) {
2823 		if (total_size < 2 * sizeof(uint32_t)) {
2824 			return LOAD_BADMACHO;
2825 		}
2826 
2827 		flavor = *ts++;
2828 		size = *ts++;
2829 		if (UINT32_MAX - 2 < size ||
2830 		    UINT32_MAX / sizeof(uint32_t) < size + 2) {
2831 			return LOAD_BADMACHO;
2832 		}
2833 		entry_size = (size + 2) * sizeof(uint32_t);
2834 		if (entry_size > total_size) {
2835 			return LOAD_BADMACHO;
2836 		}
2837 		total_size -= entry_size;
2838 		/*
2839 		 * Third argument is a kernel space pointer; it gets cast
2840 		 * to the appropriate type in thread_entrypoint() based on
2841 		 * the value of flavor.
2842 		 */
2843 		ret = thread_entrypoint(thread, flavor, (thread_state_t)ts, size, entry_point);
2844 		if (ret != KERN_SUCCESS) {
2845 			return LOAD_FAILURE;
2846 		}
2847 		ts += size;     /* ts is a (uint32_t *) */
2848 	}
2849 	return LOAD_SUCCESS;
2850 }
2851 
2852 struct macho_data {
2853 	struct nameidata        __nid;
2854 	union macho_vnode_header {
2855 		struct mach_header      mach_header;
2856 		struct fat_header       fat_header;
2857 		char    __pad[512];
2858 	} __header;
2859 };
2860 
2861 #define DEFAULT_DYLD_PATH "/usr/lib/dyld"
2862 
2863 #if (DEVELOPMENT || DEBUG)
2864 extern char dyld_alt_path[];
2865 extern int use_alt_dyld;
2866 
2867 extern char dyld_suffix[];
2868 extern int use_dyld_suffix;
2869 
2870 typedef struct _dyld_suffix_map_entry {
2871 	const char *suffix;
2872 	const char *path;
2873 } dyld_suffix_map_entry_t;
2874 
2875 static const dyld_suffix_map_entry_t _dyld_suffix_map[] = {
2876 	[0] = {
2877 		.suffix = "",
2878 		.path = DEFAULT_DYLD_PATH,
2879 	}, {
2880 		.suffix = "release",
2881 		.path = DEFAULT_DYLD_PATH,
2882 	}, {
2883 		.suffix = "bringup",
2884 		.path = "/usr/appleinternal/lib/dyld.bringup",
2885 	},
2886 };
2887 #endif
2888 
2889 static load_return_t
load_dylinker(struct dylinker_command * lcp,cpu_type_t cputype,vm_map_t map,thread_t thread,int depth,int64_t slide,load_result_t * result,struct image_params * imgp)2890 load_dylinker(
2891 	struct dylinker_command *lcp,
2892 	cpu_type_t              cputype,
2893 	vm_map_t                map,
2894 	thread_t        thread,
2895 	int                     depth,
2896 	int64_t                 slide,
2897 	load_result_t           *result,
2898 	struct image_params     *imgp
2899 	)
2900 {
2901 	const char              *name;
2902 	struct vnode            *vp = NULLVP;   /* set by get_macho_vnode() */
2903 	struct mach_header      *header;
2904 	off_t                   file_offset = 0; /* set by get_macho_vnode() */
2905 	off_t                   macho_size = 0; /* set by get_macho_vnode() */
2906 	load_result_t           *myresult;
2907 	kern_return_t           ret;
2908 	struct macho_data       *macho_data;
2909 	struct {
2910 		struct mach_header      __header;
2911 		load_result_t           __myresult;
2912 		struct macho_data       __macho_data;
2913 	} *dyld_data;
2914 
2915 	if (lcp->cmdsize < sizeof(*lcp) || lcp->name.offset >= lcp->cmdsize) {
2916 		return LOAD_BADMACHO;
2917 	}
2918 
2919 	name = (const char *)lcp + lcp->name.offset;
2920 
2921 	/* Check for a proper null terminated string. */
2922 	size_t maxsz = lcp->cmdsize - lcp->name.offset;
2923 	size_t namelen = strnlen(name, maxsz);
2924 	if (namelen >= maxsz) {
2925 		return LOAD_BADMACHO;
2926 	}
2927 
2928 #if (DEVELOPMENT || DEBUG)
2929 
2930 	/*
2931 	 * rdar://23680808
2932 	 * If an alternate dyld has been specified via boot args, check
2933 	 * to see if PROC_UUID_ALT_DYLD_POLICY has been set on this
2934 	 * executable and redirect the kernel to load that linker.
2935 	 */
2936 
2937 	if (use_alt_dyld) {
2938 		int policy_error;
2939 		uint32_t policy_flags = 0;
2940 		int32_t policy_gencount = 0;
2941 
2942 		policy_error = proc_uuid_policy_lookup(result->uuid, &policy_flags, &policy_gencount);
2943 		if (policy_error == 0) {
2944 			if (policy_flags & PROC_UUID_ALT_DYLD_POLICY) {
2945 				name = dyld_alt_path;
2946 			}
2947 		}
2948 	} else if (use_dyld_suffix) {
2949 		size_t i = 0;
2950 
2951 #define countof(x) (sizeof(x) / sizeof(x[0]))
2952 		for (i = 0; i < countof(_dyld_suffix_map); i++) {
2953 			const dyld_suffix_map_entry_t *entry = &_dyld_suffix_map[i];
2954 
2955 			if (strcmp(entry->suffix, dyld_suffix) == 0) {
2956 				name = entry->path;
2957 				break;
2958 			}
2959 		}
2960 	}
2961 #endif
2962 
2963 #if !(DEVELOPMENT || DEBUG)
2964 	if (0 != strcmp(name, DEFAULT_DYLD_PATH)) {
2965 		return LOAD_BADMACHO;
2966 	}
2967 #endif
2968 
2969 	/* Allocate wad-of-data from heap to reduce excessively deep stacks */
2970 
2971 	dyld_data = kalloc_type(typeof(*dyld_data), Z_WAITOK);
2972 	header = &dyld_data->__header;
2973 	myresult = &dyld_data->__myresult;
2974 	macho_data = &dyld_data->__macho_data;
2975 
2976 	{
2977 		cputype = (cputype & CPU_ARCH_MASK) | (cpu_type() & ~CPU_ARCH_MASK);
2978 	}
2979 
2980 	ret = get_macho_vnode(name, cputype, header,
2981 	    &file_offset, &macho_size, macho_data, &vp, imgp);
2982 	if (ret) {
2983 		goto novp_out;
2984 	}
2985 
2986 	*myresult = load_result_null;
2987 	myresult->is_64bit_addr = result->is_64bit_addr;
2988 	myresult->is_64bit_data = result->is_64bit_data;
2989 
2990 	ret = parse_machfile(vp, map, thread, header, file_offset,
2991 	    macho_size, depth, slide, 0, myresult, result, imgp);
2992 
2993 	if (ret == LOAD_SUCCESS) {
2994 		if (result->threadstate) {
2995 			/* don't use the app's threadstate if we have a dyld */
2996 			kfree_data(result->threadstate, result->threadstate_sz);
2997 		}
2998 		result->threadstate = myresult->threadstate;
2999 		result->threadstate_sz = myresult->threadstate_sz;
3000 
3001 		result->dynlinker = TRUE;
3002 		result->entry_point = myresult->entry_point;
3003 		result->validentry = myresult->validentry;
3004 		result->all_image_info_addr = myresult->all_image_info_addr;
3005 		result->all_image_info_size = myresult->all_image_info_size;
3006 		if (!myresult->platform_binary) {
3007 			result->csflags &= ~CS_NO_UNTRUSTED_HELPERS;
3008 		}
3009 
3010 #if CONFIG_ROSETTA
3011 		if (imgp->ip_flags & IMGPF_ROSETTA) {
3012 			extern const struct fileops vnops;
3013 			// Save the file descriptor and mach header address for dyld. These will
3014 			// be passed on the stack for the Rosetta runtime's use.
3015 			struct fileproc *fp;
3016 			int dyld_fd;
3017 			proc_t p = vfs_context_proc(imgp->ip_vfs_context);
3018 			int error = falloc_exec(p, imgp->ip_vfs_context, &fp, &dyld_fd);
3019 			if (error == 0) {
3020 				error = VNOP_OPEN(vp, FREAD, imgp->ip_vfs_context);
3021 				if (error == 0) {
3022 					fp->fp_glob->fg_flag = FREAD;
3023 					fp->fp_glob->fg_ops = &vnops;
3024 					fp_set_data(fp, vp);
3025 
3026 					proc_fdlock(p);
3027 					procfdtbl_releasefd(p, dyld_fd, NULL);
3028 					fp_drop(p, dyld_fd, fp, 1);
3029 					proc_fdunlock(p);
3030 
3031 					vnode_ref(vp);
3032 
3033 					result->dynlinker_fd = dyld_fd;
3034 					result->dynlinker_fp = fp;
3035 					result->dynlinker_mach_header = myresult->mach_header;
3036 					result->dynlinker_max_vm_addr = myresult->max_vm_addr;
3037 					result->dynlinker_ro_vm_start = myresult->ro_vm_start;
3038 					result->dynlinker_ro_vm_end = myresult->ro_vm_end;
3039 				} else {
3040 					fp_free(p, dyld_fd, fp);
3041 					ret = LOAD_IOERROR;
3042 				}
3043 			} else {
3044 				ret = LOAD_IOERROR;
3045 			}
3046 		}
3047 #endif
3048 	}
3049 
3050 	struct vnode_attr *va;
3051 	va = kalloc_type(struct vnode_attr, Z_WAITOK | Z_ZERO);
3052 	VATTR_INIT(va);
3053 	VATTR_WANTED(va, va_fsid64);
3054 	VATTR_WANTED(va, va_fsid);
3055 	VATTR_WANTED(va, va_fileid);
3056 	int error = vnode_getattr(vp, va, imgp->ip_vfs_context);
3057 	if (error == 0) {
3058 		imgp->ip_dyld_fsid = vnode_get_va_fsid(va);
3059 		imgp->ip_dyld_fsobjid = va->va_fileid;
3060 	}
3061 
3062 	vnode_put(vp);
3063 	kfree_type(struct vnode_attr, va);
3064 novp_out:
3065 	kfree_type(typeof(*dyld_data), dyld_data);
3066 	return ret;
3067 }
3068 
3069 #if CONFIG_ROSETTA
3070 static const char* rosetta_runtime_path = "/usr/libexec/rosetta/runtime";
3071 
3072 #if (DEVELOPMENT || DEBUG)
3073 static const char* rosetta_runtime_path_alt_x86 = "/usr/local/libexec/rosetta/runtime_internal";
3074 static const char* rosetta_runtime_path_alt_arm = "/usr/local/libexec/rosetta/runtime_arm_internal";
3075 #endif
3076 
3077 static load_return_t
load_rosetta(vm_map_t map,thread_t thread,load_result_t * result,struct image_params * imgp)3078 load_rosetta(
3079 	vm_map_t                        map,
3080 	thread_t                        thread,
3081 	load_result_t           *result,
3082 	struct image_params     *imgp)
3083 {
3084 	struct vnode            *vp = NULLVP;   /* set by get_macho_vnode() */
3085 	struct mach_header      *header;
3086 	off_t                   file_offset = 0; /* set by get_macho_vnode() */
3087 	off_t                   macho_size = 0; /* set by get_macho_vnode() */
3088 	load_result_t           *myresult;
3089 	kern_return_t           ret;
3090 	struct macho_data       *macho_data;
3091 	const char              *rosetta_file_path;
3092 	struct {
3093 		struct mach_header      __header;
3094 		load_result_t           __myresult;
3095 		struct macho_data       __macho_data;
3096 	} *rosetta_data;
3097 	mach_vm_address_t rosetta_load_addr;
3098 	mach_vm_size_t    rosetta_size;
3099 	mach_vm_address_t shared_cache_base = SHARED_REGION_BASE_ARM64;
3100 	int64_t           slide = 0;
3101 
3102 	/* Allocate wad-of-data from heap to reduce excessively deep stacks */
3103 	rosetta_data = kalloc_type(typeof(*rosetta_data), Z_WAITOK | Z_NOFAIL);
3104 	header = &rosetta_data->__header;
3105 	myresult = &rosetta_data->__myresult;
3106 	macho_data = &rosetta_data->__macho_data;
3107 
3108 	rosetta_file_path = rosetta_runtime_path;
3109 
3110 #if (DEVELOPMENT || DEBUG)
3111 	bool use_alt_rosetta = false;
3112 	if (imgp->ip_flags & IMGPF_ALT_ROSETTA) {
3113 		use_alt_rosetta = true;
3114 	} else {
3115 		int policy_error;
3116 		uint32_t policy_flags = 0;
3117 		int32_t policy_gencount = 0;
3118 		policy_error = proc_uuid_policy_lookup(result->uuid, &policy_flags, &policy_gencount);
3119 		if (policy_error == 0 && (policy_flags & PROC_UUID_ALT_ROSETTA_POLICY) != 0) {
3120 			use_alt_rosetta = true;
3121 		}
3122 	}
3123 
3124 	if (use_alt_rosetta) {
3125 		if (imgp->ip_origcputype == CPU_TYPE_X86_64) {
3126 			rosetta_file_path = rosetta_runtime_path_alt_x86;
3127 		} else if (imgp->ip_origcputype == CPU_TYPE_ARM64) {
3128 			rosetta_file_path = rosetta_runtime_path_alt_arm;
3129 		} else {
3130 			ret = LOAD_BADARCH;
3131 			goto novp_out;
3132 		}
3133 	}
3134 #endif
3135 
3136 	ret = get_macho_vnode(rosetta_file_path, CPU_TYPE_ARM64, header,
3137 	    &file_offset, &macho_size, macho_data, &vp, imgp);
3138 	if (ret) {
3139 		goto novp_out;
3140 	}
3141 
3142 	*myresult = load_result_null;
3143 	myresult->is_64bit_addr = TRUE;
3144 	myresult->is_64bit_data = TRUE;
3145 
3146 	ret = parse_machfile(vp, NULL, NULL, header, file_offset, macho_size,
3147 	    2, 0, 0, myresult, NULL, imgp);
3148 	if (ret != LOAD_SUCCESS) {
3149 		goto out;
3150 	}
3151 
3152 	if (!(imgp->ip_flags & IMGPF_DISABLE_ASLR)) {
3153 		slide = random();
3154 		slide = (slide % (vm_map_get_max_loader_aslr_slide_pages(map) - 1)) + 1;
3155 		slide <<= vm_map_page_shift(map);
3156 	}
3157 
3158 	if (imgp->ip_origcputype == CPU_TYPE_X86_64) {
3159 		shared_cache_base = SHARED_REGION_BASE_X86_64;
3160 	}
3161 
3162 	rosetta_size = round_page(myresult->max_vm_addr - myresult->min_vm_addr);
3163 	rosetta_load_addr = shared_cache_base - rosetta_size - slide;
3164 
3165 	*myresult = load_result_null;
3166 	myresult->is_64bit_addr = TRUE;
3167 	myresult->is_64bit_data = TRUE;
3168 	myresult->is_rosetta = TRUE;
3169 
3170 	ret = parse_machfile(vp, map, thread, header, file_offset, macho_size,
3171 	    2, rosetta_load_addr, 0, myresult, result, imgp);
3172 	if (ret == LOAD_SUCCESS) {
3173 		if (result) {
3174 			if (result->threadstate) {
3175 				/* don't use the app's/dyld's threadstate */
3176 				kfree_data(result->threadstate, result->threadstate_sz);
3177 			}
3178 			assert(myresult->threadstate != NULL);
3179 
3180 			result->is_rosetta = TRUE;
3181 
3182 			result->threadstate = myresult->threadstate;
3183 			result->threadstate_sz = myresult->threadstate_sz;
3184 
3185 			result->entry_point = myresult->entry_point;
3186 			result->validentry = myresult->validentry;
3187 			if (!myresult->platform_binary) {
3188 				result->csflags &= ~CS_NO_UNTRUSTED_HELPERS;
3189 			}
3190 
3191 			if ((header->cpusubtype & ~CPU_SUBTYPE_MASK) != CPU_SUBTYPE_ARM64E) {
3192 				imgp->ip_flags |= IMGPF_NOJOP;
3193 			}
3194 		}
3195 	}
3196 
3197 out:
3198 	vnode_put(vp);
3199 novp_out:
3200 	kfree_type(typeof(*rosetta_data), rosetta_data);
3201 	return ret;
3202 }
3203 #endif
3204 
3205 static void
set_signature_error(struct vnode * vp,struct image_params * imgp,const char * fatal_failure_desc,const size_t fatal_failure_desc_len)3206 set_signature_error(
3207 	struct vnode* vp,
3208 	struct image_params * imgp,
3209 	const char* fatal_failure_desc,
3210 	const size_t fatal_failure_desc_len)
3211 {
3212 	char *vn_path = NULL;
3213 	vm_size_t vn_pathlen = MAXPATHLEN;
3214 	char const *path = NULL;
3215 
3216 	vn_path = zalloc(ZV_NAMEI);
3217 	if (vn_getpath(vp, vn_path, (int*)&vn_pathlen) == 0) {
3218 		path = vn_path;
3219 	} else {
3220 		path = "(get vnode path failed)";
3221 	}
3222 	os_reason_t reason = os_reason_create(OS_REASON_CODESIGNING,
3223 	    CODESIGNING_EXIT_REASON_TASKGATED_INVALID_SIG);
3224 
3225 	if (reason == OS_REASON_NULL) {
3226 		printf("load_code_signature: %s: failure to allocate exit reason for validation failure: %s\n",
3227 		    path, fatal_failure_desc);
3228 		goto out;
3229 	}
3230 
3231 	imgp->ip_cs_error = reason;
3232 	reason->osr_flags = (OS_REASON_FLAG_GENERATE_CRASH_REPORT |
3233 	    OS_REASON_FLAG_CONSISTENT_FAILURE);
3234 
3235 	mach_vm_address_t data_addr = 0;
3236 
3237 	int reason_error = 0;
3238 	int kcdata_error = 0;
3239 
3240 	if ((reason_error = os_reason_alloc_buffer_noblock(reason, kcdata_estimate_required_buffer_size
3241 	    (1, (uint32_t)fatal_failure_desc_len))) == 0 &&
3242 	    (kcdata_error = kcdata_get_memory_addr(&reason->osr_kcd_descriptor,
3243 	    EXIT_REASON_USER_DESC, (uint32_t)fatal_failure_desc_len,
3244 	    &data_addr)) == KERN_SUCCESS) {
3245 		kern_return_t mc_error = kcdata_memcpy(&reason->osr_kcd_descriptor, (mach_vm_address_t)data_addr,
3246 		    fatal_failure_desc, (uint32_t)fatal_failure_desc_len);
3247 
3248 		if (mc_error != KERN_SUCCESS) {
3249 			printf("load_code_signature: %s: failed to copy reason string "
3250 			    "(kcdata_memcpy error: %d, length: %lu)\n",
3251 			    path, mc_error, fatal_failure_desc_len);
3252 		}
3253 	} else {
3254 		printf("load_code_signature: %s: failed to allocate space for reason string "
3255 		    "(os_reason_alloc_buffer error: %d, kcdata error: %d, length: %lu)\n",
3256 		    path, reason_error, kcdata_error, fatal_failure_desc_len);
3257 	}
3258 out:
3259 	if (vn_path) {
3260 		zfree(ZV_NAMEI, vn_path);
3261 	}
3262 }
3263 
3264 static load_return_t
load_code_signature(struct linkedit_data_command * lcp,struct vnode * vp,off_t macho_offset,off_t macho_size,cpu_type_t cputype,cpu_subtype_t cpusubtype,load_result_t * result,struct image_params * imgp)3265 load_code_signature(
3266 	struct linkedit_data_command    *lcp,
3267 	struct vnode                    *vp,
3268 	off_t                           macho_offset,
3269 	off_t                           macho_size,
3270 	cpu_type_t                      cputype,
3271 	cpu_subtype_t                   cpusubtype,
3272 	load_result_t                   *result,
3273 	struct image_params             *imgp)
3274 {
3275 	int             ret;
3276 	kern_return_t   kr;
3277 	vm_offset_t     addr;
3278 	int             resid;
3279 	struct cs_blob  *blob;
3280 	int             error;
3281 	vm_size_t       blob_size;
3282 	uint32_t        sum;
3283 	boolean_t               anyCPU;
3284 
3285 	addr = 0;
3286 	blob = NULL;
3287 
3288 	cpusubtype &= ~CPU_SUBTYPE_MASK;
3289 
3290 	blob = ubc_cs_blob_get(vp, cputype, cpusubtype, macho_offset);
3291 
3292 	if (blob != NULL) {
3293 		/* we already have a blob for this vnode and cpu(sub)type */
3294 		anyCPU = blob->csb_cpu_type == -1;
3295 		if ((blob->csb_cpu_type != cputype &&
3296 		    blob->csb_cpu_subtype != cpusubtype && !anyCPU) ||
3297 		    (blob->csb_base_offset != macho_offset) ||
3298 		    ((blob->csb_flags & CS_VALID) == 0)) {
3299 			/* the blob has changed for this vnode: fail ! */
3300 			ret = LOAD_BADMACHO;
3301 			const char* fatal_failure_desc = "embedded signature doesn't match attached signature";
3302 			const size_t fatal_failure_desc_len = strlen(fatal_failure_desc) + 1;
3303 
3304 			printf("load_code_signature: %s\n", fatal_failure_desc);
3305 			set_signature_error(vp, imgp, fatal_failure_desc, fatal_failure_desc_len);
3306 			goto out;
3307 		}
3308 
3309 		/* It matches the blob we want here, let's verify the version */
3310 		if (!anyCPU && ubc_cs_generation_check(vp) == 0) {
3311 			/* No need to revalidate, we're good! */
3312 			ret = LOAD_SUCCESS;
3313 			goto out;
3314 		}
3315 
3316 		/* That blob may be stale, let's revalidate. */
3317 		error = ubc_cs_blob_revalidate(vp, blob, imgp, 0, result->ip_platform);
3318 		if (error == 0) {
3319 			/* Revalidation succeeded, we're good! */
3320 			/* If we were revaliding a CS blob with any CPU arch we adjust it */
3321 			if (anyCPU) {
3322 				vnode_lock_spin(vp);
3323 				struct cs_cpu_info cpu_info = {
3324 					.csb_cpu_type = cputype,
3325 					.csb_cpu_subtype = cpusubtype
3326 				};
3327 				zalloc_ro_update_field(ZONE_ID_CS_BLOB, blob, csb_cpu_info, &cpu_info);
3328 				vnode_unlock(vp);
3329 			}
3330 			ret = LOAD_SUCCESS;
3331 			goto out;
3332 		}
3333 
3334 		if (error != EAGAIN) {
3335 			printf("load_code_signature: revalidation failed: %d\n", error);
3336 			ret = LOAD_FAILURE;
3337 			goto out;
3338 		}
3339 
3340 		assert(error == EAGAIN);
3341 
3342 		/*
3343 		 * Revalidation was not possible for this blob. We just continue as if there was no blob,
3344 		 * rereading the signature, and ubc_cs_blob_add will do the right thing.
3345 		 */
3346 		blob = NULL;
3347 	}
3348 
3349 	if (lcp->cmdsize != sizeof(struct linkedit_data_command)) {
3350 		ret = LOAD_BADMACHO;
3351 		goto out;
3352 	}
3353 
3354 	sum = 0;
3355 	if (os_add_overflow(lcp->dataoff, lcp->datasize, &sum) || sum > macho_size) {
3356 		ret = LOAD_BADMACHO;
3357 		goto out;
3358 	}
3359 
3360 	blob_size = lcp->datasize;
3361 	kr = ubc_cs_blob_allocate(&addr, &blob_size);
3362 	if (kr != KERN_SUCCESS) {
3363 		ret = LOAD_NOSPACE;
3364 		goto out;
3365 	}
3366 
3367 	resid = 0;
3368 	error = vn_rdwr(UIO_READ,
3369 	    vp,
3370 	    (caddr_t) addr,
3371 	    lcp->datasize,
3372 	    macho_offset + lcp->dataoff,
3373 	    UIO_SYSSPACE,
3374 	    0,
3375 	    kauth_cred_get(),
3376 	    &resid,
3377 	    current_proc());
3378 	if (error || resid != 0) {
3379 		ret = LOAD_IOERROR;
3380 		goto out;
3381 	}
3382 
3383 	if (ubc_cs_blob_add(vp,
3384 	    result->ip_platform,
3385 	    cputype,
3386 	    cpusubtype,
3387 	    macho_offset,
3388 	    &addr,
3389 	    lcp->datasize,
3390 	    imgp,
3391 	    0,
3392 	    &blob,
3393 	    CS_BLOB_ADD_ALLOW_MAIN_BINARY)) {
3394 		if (addr) {
3395 			ubc_cs_blob_deallocate(addr, blob_size);
3396 			addr = 0;
3397 		}
3398 		ret = LOAD_FAILURE;
3399 		goto out;
3400 	} else {
3401 		/* ubc_cs_blob_add() has consumed "addr" */
3402 		addr = 0;
3403 	}
3404 
3405 #if CHECK_CS_VALIDATION_BITMAP
3406 	ubc_cs_validation_bitmap_allocate( vp );
3407 #endif
3408 
3409 	ret = LOAD_SUCCESS;
3410 out:
3411 	if (ret == LOAD_SUCCESS) {
3412 		if (blob == NULL) {
3413 			panic("success, but no blob!");
3414 		}
3415 
3416 		result->csflags |= blob->csb_flags;
3417 		result->platform_binary = blob->csb_platform_binary;
3418 		result->cs_end_offset = blob->csb_end_offset;
3419 	}
3420 	if (addr != 0) {
3421 		ubc_cs_blob_deallocate(addr, blob_size);
3422 		addr = 0;
3423 	}
3424 
3425 	return ret;
3426 }
3427 
3428 
3429 #if CONFIG_CODE_DECRYPTION
3430 
3431 static load_return_t
set_code_unprotect(struct encryption_info_command * eip,caddr_t addr,vm_map_t map,int64_t slide,struct vnode * vp,off_t macho_offset,cpu_type_t cputype,cpu_subtype_t cpusubtype)3432 set_code_unprotect(
3433 	struct encryption_info_command *eip,
3434 	caddr_t addr,
3435 	vm_map_t map,
3436 	int64_t slide,
3437 	struct vnode *vp,
3438 	off_t macho_offset,
3439 	cpu_type_t cputype,
3440 	cpu_subtype_t cpusubtype)
3441 {
3442 	int error, len;
3443 	pager_crypt_info_t crypt_info;
3444 	const char * cryptname = 0;
3445 	char *vpath;
3446 
3447 	size_t offset;
3448 	struct segment_command_64 *seg64;
3449 	struct segment_command *seg32;
3450 	vm_map_offset_t map_offset, map_size;
3451 	vm_object_offset_t crypto_backing_offset;
3452 	kern_return_t kr;
3453 
3454 	if (eip->cmdsize < sizeof(*eip)) {
3455 		return LOAD_BADMACHO;
3456 	}
3457 
3458 	switch (eip->cryptid) {
3459 	case 0:
3460 		/* not encrypted, just an empty load command */
3461 		return LOAD_SUCCESS;
3462 	case 1:
3463 		cryptname = "com.apple.unfree";
3464 		break;
3465 	case 0x10:
3466 		/* some random cryptid that you could manually put into
3467 		 * your binary if you want NULL */
3468 		cryptname = "com.apple.null";
3469 		break;
3470 	default:
3471 		return LOAD_BADMACHO;
3472 	}
3473 
3474 	if (map == VM_MAP_NULL) {
3475 		return LOAD_SUCCESS;
3476 	}
3477 	if (NULL == text_crypter_create) {
3478 		return LOAD_FAILURE;
3479 	}
3480 
3481 	vpath = zalloc(ZV_NAMEI);
3482 
3483 	len = MAXPATHLEN;
3484 	error = vn_getpath(vp, vpath, &len);
3485 	if (error) {
3486 		zfree(ZV_NAMEI, vpath);
3487 		return LOAD_FAILURE;
3488 	}
3489 
3490 	if (eip->cryptsize == 0) {
3491 		printf("%s:%d '%s': cryptoff 0x%llx cryptsize 0x%llx cryptid 0x%x ignored\n", __FUNCTION__, __LINE__, vpath, (uint64_t)eip->cryptoff, (uint64_t)eip->cryptsize, eip->cryptid);
3492 		zfree(ZV_NAMEI, vpath);
3493 		return LOAD_SUCCESS;
3494 	}
3495 
3496 	/* set up decrypter first */
3497 	crypt_file_data_t crypt_data = {
3498 		.filename = vpath,
3499 		.cputype = cputype,
3500 		.cpusubtype = cpusubtype,
3501 		.origin = CRYPT_ORIGIN_APP_LAUNCH,
3502 	};
3503 	kr = text_crypter_create(&crypt_info, cryptname, (void*)&crypt_data);
3504 #if VM_MAP_DEBUG_APPLE_PROTECT
3505 	if (vm_map_debug_apple_protect) {
3506 		struct proc *p;
3507 		p  = current_proc();
3508 		printf("APPLE_PROTECT: %d[%s] map %p %s(%s) -> 0x%x\n",
3509 		    proc_getpid(p), p->p_comm, map, __FUNCTION__, vpath, kr);
3510 	}
3511 #endif /* VM_MAP_DEBUG_APPLE_PROTECT */
3512 	zfree(ZV_NAMEI, vpath);
3513 
3514 	if (kr) {
3515 		printf("set_code_unprotect: unable to create decrypter %s, kr=%d\n",
3516 		    cryptname, kr);
3517 		if (kr == kIOReturnNotPrivileged) {
3518 			/* text encryption returned decryption failure */
3519 			return LOAD_DECRYPTFAIL;
3520 		} else {
3521 			return LOAD_RESOURCE;
3522 		}
3523 	}
3524 
3525 	/* this is terrible, but we have to rescan the load commands to find the
3526 	 * virtual address of this encrypted stuff. This code is gonna look like
3527 	 * the dyld source one day... */
3528 	struct mach_header *header = (struct mach_header *)addr;
3529 	size_t mach_header_sz = sizeof(struct mach_header);
3530 	if (header->magic == MH_MAGIC_64 ||
3531 	    header->magic == MH_CIGAM_64) {
3532 		mach_header_sz = sizeof(struct mach_header_64);
3533 	}
3534 	offset = mach_header_sz;
3535 	uint32_t ncmds = header->ncmds;
3536 	while (ncmds--) {
3537 		/*
3538 		 *	Get a pointer to the command.
3539 		 */
3540 		struct load_command *lcp = (struct load_command *)(addr + offset);
3541 		offset += lcp->cmdsize;
3542 
3543 		switch (lcp->cmd) {
3544 		case LC_SEGMENT_64:
3545 			seg64 = (struct segment_command_64 *)lcp;
3546 			if ((seg64->fileoff <= eip->cryptoff) &&
3547 			    (seg64->fileoff + seg64->filesize >=
3548 			    eip->cryptoff + eip->cryptsize)) {
3549 				map_offset = (vm_map_offset_t)(seg64->vmaddr + eip->cryptoff - seg64->fileoff + slide);
3550 				map_size = eip->cryptsize;
3551 				crypto_backing_offset = macho_offset + eip->cryptoff;
3552 				goto remap_now;
3553 			}
3554 			break;
3555 		case LC_SEGMENT:
3556 			seg32 = (struct segment_command *)lcp;
3557 			if ((seg32->fileoff <= eip->cryptoff) &&
3558 			    (seg32->fileoff + seg32->filesize >=
3559 			    eip->cryptoff + eip->cryptsize)) {
3560 				map_offset = (vm_map_offset_t)(seg32->vmaddr + eip->cryptoff - seg32->fileoff + slide);
3561 				map_size = eip->cryptsize;
3562 				crypto_backing_offset = macho_offset + eip->cryptoff;
3563 				goto remap_now;
3564 			}
3565 			break;
3566 		}
3567 	}
3568 
3569 	/* if we get here, did not find anything */
3570 	return LOAD_BADMACHO;
3571 
3572 remap_now:
3573 	/* now remap using the decrypter */
3574 	MACHO_PRINTF(("+++ set_code_unprotect: vm[0x%llx:0x%llx]\n",
3575 	    (uint64_t) map_offset,
3576 	    (uint64_t) (map_offset + map_size)));
3577 	kr = vm_map_apple_protected(map,
3578 	    map_offset,
3579 	    map_offset + map_size,
3580 	    crypto_backing_offset,
3581 	    &crypt_info,
3582 	    CRYPTID_APP_ENCRYPTION);
3583 	if (kr) {
3584 		printf("set_code_unprotect(): mapping failed with %x\n", kr);
3585 		return LOAD_PROTECT;
3586 	}
3587 
3588 	return LOAD_SUCCESS;
3589 }
3590 
3591 #endif
3592 
3593 /*
3594  * This routine exists to support the load_dylinker().
3595  *
3596  * This routine has its own, separate, understanding of the FAT file format,
3597  * which is terrifically unfortunate.
3598  */
3599 static
3600 load_return_t
get_macho_vnode(const char * path,cpu_type_t cputype,struct mach_header * mach_header,off_t * file_offset,off_t * macho_size,struct macho_data * data,struct vnode ** vpp,struct image_params * imgp)3601 get_macho_vnode(
3602 	const char              *path,
3603 	cpu_type_t              cputype,
3604 	struct mach_header      *mach_header,
3605 	off_t                   *file_offset,
3606 	off_t                   *macho_size,
3607 	struct macho_data       *data,
3608 	struct vnode            **vpp,
3609 	struct image_params     *imgp
3610 	)
3611 {
3612 	struct vnode            *vp;
3613 	vfs_context_t           ctx = vfs_context_current();
3614 	proc_t                  p = vfs_context_proc(ctx);
3615 	kauth_cred_t            kerncred;
3616 	struct nameidata        *ndp = &data->__nid;
3617 	boolean_t               is_fat;
3618 	struct fat_arch         fat_arch;
3619 	int                     error;
3620 	int resid;
3621 	union macho_vnode_header *header = &data->__header;
3622 	off_t fsize = (off_t)0;
3623 
3624 	/*
3625 	 * Capture the kernel credential for use in the actual read of the
3626 	 * file, since the user doing the execution may have execute rights
3627 	 * but not read rights, but to exec something, we have to either map
3628 	 * or read it into the new process address space, which requires
3629 	 * read rights.  This is to deal with lack of common credential
3630 	 * serialization code which would treat NOCRED as "serialize 'root'".
3631 	 */
3632 	kerncred = vfs_context_ucred(vfs_context_kernel());
3633 
3634 	/* init the namei data to point the file user's program name */
3635 	NDINIT(ndp, LOOKUP, OP_OPEN, FOLLOW | LOCKLEAF, UIO_SYSSPACE, CAST_USER_ADDR_T(path), ctx);
3636 
3637 	if ((error = namei(ndp)) != 0) {
3638 		if (error == ENOENT) {
3639 			error = LOAD_ENOENT;
3640 		} else {
3641 			error = LOAD_FAILURE;
3642 		}
3643 		return error;
3644 	}
3645 	nameidone(ndp);
3646 	vp = ndp->ni_vp;
3647 
3648 	/* check for regular file */
3649 	if (vp->v_type != VREG) {
3650 		error = LOAD_PROTECT;
3651 		goto bad1;
3652 	}
3653 
3654 	/* get size */
3655 	if ((error = vnode_size(vp, &fsize, ctx)) != 0) {
3656 		error = LOAD_FAILURE;
3657 		goto bad1;
3658 	}
3659 
3660 	/* Check mount point */
3661 	if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
3662 		error = LOAD_PROTECT;
3663 		goto bad1;
3664 	}
3665 
3666 	/* check access */
3667 	if ((error = vnode_authorize(vp, NULL, KAUTH_VNODE_EXECUTE | KAUTH_VNODE_READ_DATA, ctx)) != 0) {
3668 		error = LOAD_PROTECT;
3669 		goto bad1;
3670 	}
3671 
3672 	/* try to open it */
3673 	if ((error = VNOP_OPEN(vp, FREAD, ctx)) != 0) {
3674 		error = LOAD_PROTECT;
3675 		goto bad1;
3676 	}
3677 
3678 	if ((error = vn_rdwr(UIO_READ, vp, (caddr_t)header, sizeof(*header), 0,
3679 	    UIO_SYSSPACE, IO_NODELOCKED, kerncred, &resid, p)) != 0) {
3680 		error = LOAD_IOERROR;
3681 		goto bad2;
3682 	}
3683 
3684 	if (resid) {
3685 		error = LOAD_BADMACHO;
3686 		goto bad2;
3687 	}
3688 
3689 	if (header->mach_header.magic == MH_MAGIC ||
3690 	    header->mach_header.magic == MH_MAGIC_64) {
3691 		is_fat = FALSE;
3692 	} else if (OSSwapBigToHostInt32(header->fat_header.magic) == FAT_MAGIC) {
3693 		is_fat = TRUE;
3694 	} else {
3695 		error = LOAD_BADMACHO;
3696 		goto bad2;
3697 	}
3698 
3699 	if (is_fat) {
3700 		error = fatfile_validate_fatarches((vm_offset_t)(&header->fat_header),
3701 		    sizeof(*header), fsize);
3702 		if (error != LOAD_SUCCESS) {
3703 			goto bad2;
3704 		}
3705 
3706 		/* Look up our architecture in the fat file. */
3707 		error = fatfile_getbestarch_for_cputype(cputype, CPU_SUBTYPE_ANY,
3708 		    (vm_offset_t)(&header->fat_header), sizeof(*header), imgp, &fat_arch);
3709 		if (error != LOAD_SUCCESS) {
3710 			goto bad2;
3711 		}
3712 
3713 		/* Read the Mach-O header out of it */
3714 		error = vn_rdwr(UIO_READ, vp, (caddr_t)&header->mach_header,
3715 		    sizeof(header->mach_header), fat_arch.offset,
3716 		    UIO_SYSSPACE, IO_NODELOCKED, kerncred, &resid, p);
3717 		if (error) {
3718 			error = LOAD_IOERROR;
3719 			goto bad2;
3720 		}
3721 
3722 		if (resid) {
3723 			error = LOAD_BADMACHO;
3724 			goto bad2;
3725 		}
3726 
3727 		/* Is this really a Mach-O? */
3728 		if (header->mach_header.magic != MH_MAGIC &&
3729 		    header->mach_header.magic != MH_MAGIC_64) {
3730 			error = LOAD_BADMACHO;
3731 			goto bad2;
3732 		}
3733 
3734 		*file_offset = fat_arch.offset;
3735 		*macho_size = fat_arch.size;
3736 	} else {
3737 		/*
3738 		 * Force get_macho_vnode() to fail if the architecture bits
3739 		 * do not match the expected architecture bits.  This in
3740 		 * turn causes load_dylinker() to fail for the same reason,
3741 		 * so it ensures the dynamic linker and the binary are in
3742 		 * lock-step.  This is potentially bad, if we ever add to
3743 		 * the CPU_ARCH_* bits any bits that are desirable but not
3744 		 * required, since the dynamic linker might work, but we will
3745 		 * refuse to load it because of this check.
3746 		 */
3747 		if ((cpu_type_t)header->mach_header.cputype != cputype) {
3748 			error = LOAD_BADARCH;
3749 			goto bad2;
3750 		}
3751 
3752 		*file_offset = 0;
3753 		*macho_size = fsize;
3754 	}
3755 
3756 	*mach_header = header->mach_header;
3757 	*vpp = vp;
3758 
3759 	ubc_setsize(vp, fsize);
3760 	return error;
3761 
3762 bad2:
3763 	(void) VNOP_CLOSE(vp, FREAD, ctx);
3764 bad1:
3765 	vnode_put(vp);
3766 	return error;
3767 }
3768