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