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