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