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