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