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