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