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