]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/mach_loader.c
xnu-1228.7.58.tar.gz
[apple/xnu.git] / bsd / kern / mach_loader.c
1 /*
2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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 */
39
40 #include <sys/param.h>
41 #include <sys/vnode_internal.h>
42 #include <sys/uio.h>
43 #include <sys/namei.h>
44 #include <sys/proc_internal.h>
45 #include <sys/kauth.h>
46 #include <sys/stat.h>
47 #include <sys/malloc.h>
48 #include <sys/mount_internal.h>
49 #include <sys/fcntl.h>
50 #include <sys/ubc_internal.h>
51 #include <sys/imgact.h>
52
53 #include <mach/mach_types.h>
54 #include <mach/vm_map.h> /* vm_allocate() */
55 #include <mach/mach_vm.h> /* mach_vm_allocate() */
56 #include <mach/vm_statistics.h>
57 #include <mach/task.h>
58 #include <mach/thread_act.h>
59
60 #include <machine/vmparam.h>
61 #include <machine/exec.h>
62
63 #include <kern/kern_types.h>
64 #include <kern/cpu_number.h>
65 #include <kern/mach_loader.h>
66 #include <kern/kalloc.h>
67 #include <kern/task.h>
68 #include <kern/thread.h>
69
70 #include <mach-o/fat.h>
71 #include <mach-o/loader.h>
72
73 #include <vm/pmap.h>
74 #include <vm/vm_map.h>
75 #include <vm/vm_kern.h>
76 #include <vm/vm_pager.h>
77 #include <vm/vnode_pager.h>
78 #include <vm/vm_protos.h>
79
80 /*
81 * XXX vm/pmap.h should not treat these prototypes as MACH_KERNEL_PRIVATE
82 * when KERNEL is defined.
83 */
84 extern pmap_t pmap_create(vm_map_size_t size, boolean_t is_64bit);
85 extern void pmap_switch(pmap_t);
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 */
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
105 extern kern_return_t memory_object_signed(memory_object_control_t control,
106 boolean_t is_signed);
107
108 /* An empty load_result_t */
109 static load_result_t load_result_null = {
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
118 };
119
120 /*
121 * Prototypes of static functions.
122 */
123 static load_return_t
124 parse_machfile(
125 struct vnode *vp,
126 vm_map_t map,
127 thread_t thread,
128 struct mach_header *header,
129 off_t file_offset,
130 off_t macho_size,
131 int depth,
132 load_result_t *result
133 );
134
135 static load_return_t
136 load_segment(
137 struct segment_command *scp,
138 void * pager,
139 off_t pager_offset,
140 off_t macho_size,
141 off_t end_of_file,
142 vm_map_t map,
143 load_result_t *result
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
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
165 static load_return_t
166 load_unixthread(
167 struct thread_command *tcp,
168 thread_t thread,
169 load_result_t *result
170 );
171
172 static load_return_t
173 load_thread(
174 struct thread_command *tcp,
175 thread_t thread,
176 load_result_t *result
177 );
178
179 static load_return_t
180 load_threadstate(
181 thread_t thread,
182 unsigned long *ts,
183 unsigned long total_size
184 );
185
186 static load_return_t
187 load_threadstack(
188 thread_t thread,
189 unsigned long *ts,
190 unsigned long total_size,
191 user_addr_t *user_stack,
192 int *customstack
193 );
194
195 static load_return_t
196 load_threadentry(
197 thread_t thread,
198 unsigned long *ts,
199 unsigned long total_size,
200 mach_vm_offset_t *entry_point
201 );
202
203 static load_return_t
204 load_dylinker(
205 struct dylinker_command *lcp,
206 integer_t archbits,
207 vm_map_t map,
208 thread_t thread,
209 int depth,
210 load_result_t *result,
211 boolean_t is_64bit
212 );
213
214 static load_return_t
215 get_macho_vnode(
216 char *path,
217 integer_t archbits,
218 struct mach_header *mach_header,
219 off_t *file_offset,
220 off_t *macho_size,
221 struct vnode **vpp
222 );
223
224 load_return_t
225 load_machfile(
226 struct image_params *imgp,
227 struct mach_header *header,
228 thread_t thread,
229 vm_map_t new_map,
230 load_result_t *result
231 )
232 {
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 */
238 vm_map_t map;
239 vm_map_t old_map;
240 load_result_t myresult;
241 load_return_t lret;
242 boolean_t create_map = TRUE;
243
244 if (new_map != VM_MAP_NULL) {
245 create_map = FALSE;
246 }
247
248 if (create_map) {
249 old_map = current_map();
250 pmap = pmap_create((vm_map_size_t) 0, (imgp->ip_flags & IMGPF_IS_64BIT));
251 map = vm_map_create(pmap,
252 0,
253 vm_compute_max_offset((imgp->ip_flags & IMGPF_IS_64BIT)),
254 TRUE);
255 } else
256 map = new_map;
257
258 if ( (header->flags & MH_ALLOW_STACK_EXECUTION) )
259 vm_map_disable_NX(map);
260
261 if (!result)
262 result = &myresult;
263
264 *result = load_result_null;
265
266 lret = parse_machfile(vp, map, thread, header, file_offset, macho_size,
267 0, result);
268
269 if (lret != LOAD_SUCCESS) {
270 if (create_map) {
271 vm_map_deallocate(map); /* will lose pmap reference too */
272 }
273 return(lret);
274 }
275
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
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 *
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.
293 * That lets us get off the pmap associated with it, and
294 * then we can release it.
295 */
296
297 if (create_map) {
298 task_halt(current_task());
299
300 old_map = swap_task_map(current_task(), map);
301 vm_map_clear_4GB_pagezero(old_map);
302 pmap_switch(pmap); /* Make sure we are using the new pmap */
303 vm_map_deallocate(old_map);
304 }
305 return(LOAD_SUCCESS);
306 }
307
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 */
317 static
318 load_return_t
319 parse_machfile(
320 struct vnode *vp,
321 vm_map_t map,
322 thread_t thread,
323 struct mach_header *header,
324 off_t file_offset,
325 off_t macho_size,
326 int depth,
327 load_result_t *result
328 )
329 {
330 uint32_t ncmds;
331 struct load_command *lcp;
332 struct dylinker_command *dlp = 0;
333 integer_t dlarchbits = 0;
334 void * pager;
335 load_return_t ret = LOAD_SUCCESS;
336 caddr_t addr;
337 void * kl_addr;
338 vm_size_t size,kl_size;
339 size_t offset;
340 size_t oldoffset; /* for overflow check */
341 int pass;
342 proc_t p = current_proc(); /* XXXX */
343 int error;
344 int resid=0;
345 task_t task;
346 size_t mach_header_sz = sizeof(struct mach_header);
347 boolean_t abi64;
348 boolean_t got_code_signatures = FALSE;
349
350 if (header->magic == MH_MAGIC_64 ||
351 header->magic == MH_CIGAM_64) {
352 mach_header_sz = sizeof(struct mach_header_64);
353 }
354
355 /*
356 * Break infinite recursion
357 */
358 if (depth > 6) {
359 return(LOAD_FAILURE);
360 }
361
362 task = (task_t)get_threadtask(thread);
363
364 depth++;
365
366 /*
367 * Check to see if right machine type.
368 */
369 if (((cpu_type_t)(header->cputype & ~CPU_ARCH_MASK) != cpu_type()) ||
370 !grade_binary(header->cputype,
371 header->cpusubtype & ~CPU_SUBTYPE_MASK))
372 return(LOAD_BADARCH);
373
374 abi64 = ((header->cputype & CPU_ARCH_ABI64) == CPU_ARCH_ABI64);
375
376 switch (header->filetype) {
377
378 case MH_OBJECT:
379 case MH_EXECUTE:
380 case MH_PRELOAD:
381 if (depth != 1) {
382 return (LOAD_FAILURE);
383 }
384 break;
385
386 case MH_FVMLIB:
387 case MH_DYLIB:
388 if (depth == 1) {
389 return (LOAD_FAILURE);
390 }
391 break;
392
393 case MH_DYLINKER:
394 if (depth != 2) {
395 return (LOAD_FAILURE);
396 }
397 break;
398
399 default:
400 return (LOAD_FAILURE);
401 }
402
403 /*
404 * Get the pager for the file.
405 */
406 pager = (void *) ubc_getpager(vp);
407
408 /*
409 * Map portion that must be accessible directly into
410 * kernel's map.
411 */
412 if ((mach_header_sz + header->sizeofcmds) > macho_size)
413 return(LOAD_BADMACHO);
414
415 /*
416 * Round size of Mach-O commands up to page boundry.
417 */
418 size = round_page(mach_header_sz + header->sizeofcmds);
419 if (size <= 0)
420 return(LOAD_BADMACHO);
421
422 /*
423 * Map the load commands into kernel memory.
424 */
425 addr = 0;
426 kl_size = size;
427 kl_addr = kalloc(size);
428 addr = (caddr_t)kl_addr;
429 if (addr == NULL)
430 return(LOAD_NOSPACE);
431
432 error = vn_rdwr(UIO_READ, vp, addr, size, file_offset,
433 UIO_SYSSPACE32, 0, kauth_cred_get(), &resid, p);
434 if (error) {
435 if (kl_addr )
436 kfree(kl_addr, kl_size);
437 return(LOAD_IOERROR);
438 }
439 /* (void)ubc_map(vp, PROT_EXEC); */ /* NOT HERE */
440
441 /*
442 * Scan through the commands, processing each one as necessary.
443 */
444 for (pass = 1; pass <= 2; pass++) {
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 */
451 offset = mach_header_sz;
452 ncmds = header->ncmds;
453 while (ncmds--) {
454 /*
455 * Get a pointer to the command.
456 */
457 lcp = (struct load_command *)(addr + offset);
458 oldoffset = offset;
459 offset += lcp->cmdsize;
460
461 /*
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.
468 */
469 if (oldoffset > offset ||
470 lcp->cmdsize < sizeof(struct load_command) ||
471 offset > header->sizeofcmds + mach_header_sz) {
472 ret = LOAD_BADMACHO;
473 break;
474 }
475
476 /*
477 * Act on struct load_command's for which kernel
478 * intervention is required.
479 */
480 switch(lcp->cmd) {
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;
493 case LC_SEGMENT:
494 if (pass != 1)
495 break;
496 ret = load_segment(
497 (struct segment_command *) lcp,
498 pager,
499 file_offset,
500 macho_size,
501 ubc_getsize(vp),
502 map,
503 result);
504 break;
505 case LC_THREAD:
506 if (pass != 2)
507 break;
508 ret = load_thread((struct thread_command *)lcp,
509 thread,
510 result);
511 break;
512 case LC_UNIXTHREAD:
513 if (pass != 2)
514 break;
515 ret = load_unixthread(
516 (struct thread_command *) lcp,
517 thread,
518 result);
519 break;
520 case LC_LOAD_DYLINKER:
521 if (pass != 2)
522 break;
523 if ((depth == 1) && (dlp == 0)) {
524 dlp = (struct dylinker_command *)lcp;
525 dlarchbits = (header->cputype & CPU_ARCH_MASK);
526 } else {
527 ret = LOAD_FAILURE;
528 }
529 break;
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;
554 default:
555 /* Other commands are ignored by the kernel */
556 ret = LOAD_SUCCESS;
557 break;
558 }
559 if (ret != LOAD_SUCCESS)
560 break;
561 }
562 if (ret != LOAD_SUCCESS)
563 break;
564 }
565 if (ret == LOAD_SUCCESS) {
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 }
575
576 if (dlp != 0)
577 ret = load_dylinker(dlp, dlarchbits, map, thread, depth, result, abi64);
578
579 if(depth == 1) {
580 if (result->thread_count == 0) {
581 ret = LOAD_FAILURE;
582 } else if ( abi64 ) {
583 #ifdef __ppc__
584 /* Map in 64-bit commpage */
585 /* LP64todo - make this clean */
586 /*
587 * PPC51: ppc64 is limited to 51-bit addresses.
588 * Memory above that limit is handled specially
589 * at the pmap level.
590 */
591 pmap_map_sharedpage(current_task(), get_map_pmap(map));
592 #endif /* __ppc__ */
593 }
594 }
595 }
596
597 if (kl_addr )
598 kfree(kl_addr, kl_size);
599
600 if (ret == LOAD_SUCCESS)
601 (void)ubc_map(vp, PROT_READ | PROT_EXEC);
602
603 return(ret);
604 }
605
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
658 static
659 load_return_t
660 load_segment(
661 struct segment_command *scp,
662 void * pager,
663 off_t pager_offset,
664 off_t macho_size,
665 __unused off_t end_of_file,
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;
673 vm_prot_t initprot;
674 vm_prot_t maxprot;
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);
682 /*
683 * Make sure the segment is page-aligned in the file.
684 */
685 if ((scp->fileoff & PAGE_MASK) != 0)
686 return LOAD_BADMACHO;
687
688 seg_size = round_page(scp->vmsize);
689 if (seg_size == 0)
690 return(KERN_SUCCESS);
691
692 /*
693 * Round sizes to page size.
694 */
695 map_size = round_page(scp->filesize);
696 map_addr = trunc_page(scp->vmaddr);
697
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
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,
728 &map_addr, map_size, (vm_offset_t)0,
729 VM_FLAGS_FIXED, pager, map_offset, TRUE,
730 initprot, maxprot,
731 VM_INHERIT_DEFAULT);
732 if (ret != KERN_SUCCESS)
733 return(LOAD_NOSPACE);
734
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
744 ret = vm_allocate(kernel_map, &tmp, delta_size, VM_FLAGS_ANYWHERE);
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
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);
773 if (ret != KERN_SUCCESS)
774 return(LOAD_NOSPACE);
775 }
776
777 if ( (scp->fileoff == 0) && (scp->filesize != 0) )
778 result->mach_header = map_addr;
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;
791 }
792
793 static
794 load_return_t
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,
802 load_result_t *result
803 )
804 {
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;
810
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);
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;
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
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
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);
885 return (LOAD_FAILURE);
886 }
887
888 (void) vm_deallocate(kernel_map, tmp, delta_size);
889 }
890 #endif /* FIXME */
891 }
892
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;
901
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);
906 if (ret != KERN_SUCCESS)
907 return(LOAD_NOSPACE);
908 }
909
910 if ( (scp64->fileoff == 0) && (scp64->filesize != 0) )
911 result->mach_header = map_addr;
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;
924 }
925
926 static
927 load_return_t
928 load_thread(
929 struct thread_command *tcp,
930 thread_t thread,
931 load_result_t *result
932 )
933 {
934 kern_return_t kret;
935 load_return_t lret;
936 task_t task;
937 int customstack=0;
938
939 task = get_threadtask(thread);
940
941 /* if count is 0; same as thread */
942 if (result->thread_count != 0) {
943 kret = thread_create(task, &thread);
944 if (kret != KERN_SUCCESS)
945 return(LOAD_RESOURCE);
946 thread_deallocate(thread);
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) {
957 lret = load_threadstack(thread,
958 (unsigned long *)(((vm_offset_t)tcp) +
959 sizeof(struct thread_command)),
960 tcp->cmdsize - sizeof(struct thread_command),
961 &result->user_stack,
962 &customstack);
963 if (customstack)
964 result->customstack = 1;
965 else
966 result->customstack = 0;
967
968 if (lret != LOAD_SUCCESS)
969 return(lret);
970
971 lret = load_threadentry(thread,
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
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
1003 if (result->thread_count != 0) {
1004 printf("load_unixthread: already have a thread!");
1005 return (LOAD_FAILURE);
1006 }
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
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;
1053 unsigned long thread_size;
1054
1055 ret = thread_state_initialize( thread );
1056 if (ret != KERN_SUCCESS) {
1057 return(LOAD_FAILURE);
1058 }
1059
1060 /*
1061 * Set the new thread state; iterate through the state flavors in
1062 * the mach-o file.
1063 */
1064 while (total_size > 0) {
1065 flavor = *ts++;
1066 size = *ts++;
1067 thread_size = (size+2)*sizeof(unsigned long);
1068 if (thread_size > total_size)
1069 return(LOAD_BADMACHO);
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);
1077 if (ret != KERN_SUCCESS) {
1078 return(LOAD_FAILURE);
1079 }
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,
1091 user_addr_t *user_stack,
1092 int *customstack
1093 )
1094 {
1095 kern_return_t ret;
1096 unsigned long size;
1097 int flavor;
1098 unsigned long stack_size;
1099
1100 while (total_size > 0) {
1101 flavor = *ts++;
1102 size = *ts++;
1103 stack_size = (size+2)*sizeof(unsigned long);
1104 if (stack_size > total_size)
1105 return(LOAD_BADMACHO);
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);
1114 if (ret != KERN_SUCCESS) {
1115 return(LOAD_FAILURE);
1116 }
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,
1128 mach_vm_offset_t *entry_point
1129 )
1130 {
1131 kern_return_t ret;
1132 unsigned long size;
1133 int flavor;
1134 unsigned long entry_size;
1135
1136 /*
1137 * Set the thread state.
1138 */
1139 *entry_point = MACH_VM_MIN_ADDRESS;
1140 while (total_size > 0) {
1141 flavor = *ts++;
1142 size = *ts++;
1143 entry_size = (size+2)*sizeof(unsigned long);
1144 if (entry_size > total_size)
1145 return(LOAD_BADMACHO);
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);
1153 if (ret != KERN_SUCCESS) {
1154 return(LOAD_FAILURE);
1155 }
1156 ts += size; /* ts is a (unsigned long *) */
1157 }
1158 return(LOAD_SUCCESS);
1159 }
1160
1161
1162 static
1163 load_return_t
1164 load_dylinker(
1165 struct dylinker_command *lcp,
1166 integer_t archbits,
1167 vm_map_t map,
1168 thread_t thread,
1169 int depth,
1170 load_result_t *result,
1171 boolean_t is_64bit
1172 )
1173 {
1174 char *name;
1175 char *p;
1176 struct vnode *vp = NULLVP; /* set by get_macho_vnode() */
1177 struct mach_header header;
1178 off_t file_offset = 0; /* set by get_macho_vnode() */
1179 off_t macho_size = 0; /* set by get_macho_vnode() */
1180 vm_map_t copy_map;
1181 load_result_t myresult;
1182 kern_return_t ret;
1183 vm_map_copy_t tmp;
1184 mach_vm_offset_t dyl_start, map_addr;
1185 mach_vm_size_t dyl_length;
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
1197 ret = get_macho_vnode(name, archbits, &header, &file_offset, &macho_size, &vp);
1198 if (ret)
1199 return (ret);
1200
1201 myresult = load_result_null;
1202
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 */
1208
1209 ret = parse_machfile(vp, map, thread, &header, file_offset, macho_size,
1210 depth, &myresult);
1211
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 */
1217
1218 if (ret == LOAD_NOSPACE) {
1219
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;
1229 goto out;
1230 }
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);
1240 goto out;
1241 }
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 }
1257
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;
1288 }
1289
1290 vm_map_deallocate(copy_map);
1291 }
1292
1293 if (ret == LOAD_SUCCESS) {
1294 result->dynlinker = TRUE;
1295 result->entry_point = myresult.entry_point;
1296 (void)ubc_map(vp, PROT_READ | PROT_EXEC);
1297 }
1298 out:
1299 vnode_put(vp);
1300 return (ret);
1301
1302 }
1303
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
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 */
1399 static
1400 load_return_t
1401 get_macho_vnode(
1402 char *path,
1403 integer_t archbits,
1404 struct mach_header *mach_header,
1405 off_t *file_offset,
1406 off_t *macho_size,
1407 struct vnode **vpp
1408 )
1409 {
1410 struct vnode *vp;
1411 vfs_context_t ctx = vfs_context_current();
1412 proc_t p = vfs_context_proc(ctx);
1413 kauth_cred_t kerncred;
1414 struct nameidata nid, *ndp;
1415 boolean_t is_fat;
1416 struct fat_arch fat_arch;
1417 int error = LOAD_SUCCESS;
1418 int resid;
1419 union {
1420 struct mach_header mach_header;
1421 struct fat_header fat_header;
1422 char pad[512];
1423 } header;
1424 off_t fsize = (off_t)0;
1425 int err2;
1426
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());
1436
1437 ndp = &nid;
1438
1439 /* init the namei data to point the file user's program name */
1440 NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE32, CAST_USER_ADDR_T(path), ctx);
1441
1442 if ((error = namei(ndp)) != 0) {
1443 if (error == ENOENT) {
1444 error = LOAD_ENOENT;
1445 } else {
1446 error = LOAD_FAILURE;
1447 }
1448 return(error);
1449 }
1450 nameidone(ndp);
1451 vp = ndp->ni_vp;
1452
1453 /* check for regular file */
1454 if (vp->v_type != VREG) {
1455 error = LOAD_PROTECT;
1456 goto bad1;
1457 }
1458
1459 /* get size */
1460 if ((error = vnode_size(vp, &fsize, ctx)) != 0) {
1461 error = LOAD_FAILURE;
1462 goto bad1;
1463 }
1464
1465 /* Check mount point */
1466 if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
1467 error = LOAD_PROTECT;
1468 goto bad1;
1469 }
1470
1471 /* check access */
1472 if ((error = vnode_authorize(vp, NULL, KAUTH_VNODE_EXECUTE, ctx)) != 0) {
1473 error = LOAD_PROTECT;
1474 goto bad1;
1475 }
1476
1477 /* try to open it */
1478 if ((error = VNOP_OPEN(vp, FREAD, ctx)) != 0) {
1479 error = LOAD_PROTECT;
1480 goto bad1;
1481 }
1482
1483 if ((error = vn_rdwr(UIO_READ, vp, (caddr_t)&header, sizeof(header), 0,
1484 UIO_SYSSPACE32, IO_NODELOCKED, kerncred, &resid, p)) != 0) {
1485 error = LOAD_IOERROR;
1486 goto bad2;
1487 }
1488
1489 if (header.mach_header.magic == MH_MAGIC ||
1490 header.mach_header.magic == MH_MAGIC_64)
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) {
1501 /* Look up our architecture in the fat file. */
1502 error = fatfile_getarch_with_bits(vp, archbits, (vm_offset_t)(&header.fat_header), &fat_arch);
1503 if (error != LOAD_SUCCESS)
1504 goto bad2;
1505
1506 /* Read the Mach-O header out of it */
1507 error = vn_rdwr(UIO_READ, vp, (caddr_t)&header.mach_header,
1508 sizeof(header.mach_header), fat_arch.offset,
1509 UIO_SYSSPACE32, IO_NODELOCKED, kerncred, &resid, p);
1510 if (error) {
1511 error = LOAD_IOERROR;
1512 goto bad2;
1513 }
1514
1515 /* Is this really a Mach-O? */
1516 if (header.mach_header.magic != MH_MAGIC &&
1517 header.mach_header.magic != MH_MAGIC_64) {
1518 error = LOAD_BADMACHO;
1519 goto bad2;
1520 }
1521
1522 *file_offset = fat_arch.offset;
1523 *macho_size = fat_arch.size;
1524 } else {
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);
1537
1538 *file_offset = 0;
1539 *macho_size = fsize;
1540 }
1541
1542 *mach_header = header.mach_header;
1543 *vpp = vp;
1544
1545 ubc_setsize(vp, fsize);
1546
1547 return (error);
1548
1549 bad2:
1550 err2 = VNOP_CLOSE(vp, FREAD, ctx);
1551 vnode_put(vp);
1552 return (error);
1553
1554 bad1:
1555 vnode_put(vp);
1556 return(error);
1557 }