2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
25 * Mach Operating System
26 * Copyright (c) 1987 Carnegie-Mellon University
27 * All rights reserved. The CMU software License Agreement specifies
28 * the terms and conditions for use and redistribution.
34 * Copyright (c) 1982, 1986, 1991, 1993
35 * The Regents of the University of California. All rights reserved.
36 * (c) UNIX System Laboratories, Inc.
37 * All or some portions of this file are derived from material licensed
38 * to the University of California by American Telephone and Telegraph
39 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
40 * the permission of UNIX System Laboratories, Inc.
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * from: @(#)kern_exec.c 8.1 (Berkeley) 6/10/93
72 #include <machine/reg.h>
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/filedesc.h>
77 #include <sys/kernel.h>
78 #include <sys/proc_internal.h>
79 #include <sys/kauth.h>
81 #include <sys/socketvar.h>
82 #include <sys/malloc.h>
83 #include <sys/namei.h>
84 #include <sys/mount_internal.h>
85 #include <sys/vnode_internal.h>
86 #include <sys/file_internal.h>
88 #include <sys/uio_internal.h>
91 #include <sys/kdebug.h>
92 #include <sys/signal.h>
93 #include <sys/aio_kern.h>
94 #include <sys/sysproto.h>
95 #include <sys/shm_internal.h> /* shmexec() */
96 #include <sys/ubc_internal.h> /* ubc_map() */
98 #include <bsm/audit_kernel.h>
100 #include <mach/mach_types.h>
101 #include <mach/task.h>
102 #include <mach/thread_act.h>
103 #include <mach/vm_map.h>
104 #include <mach/mach_vm.h>
105 #include <mach/vm_param.h>
107 #include <vm/vm_map.h>
108 #include <vm/vm_kern.h>
109 #include <vm/vm_pager.h>
110 #include <vm/vm_kern.h>
111 #include <vm/task_working_set.h>
112 #include <vm/vm_shared_memory_server.h>
115 * Mach things for which prototypes are unavailable from Mach headers
120 extern struct savearea
*get_user_regs(thread_t
);
123 #include <kern/thread.h>
124 #include <kern/task.h>
125 #include <kern/ast.h>
126 #include <kern/mach_loader.h>
127 #include <mach-o/fat.h>
128 #include <mach-o/loader.h>
129 #include <machine/vmparam.h>
131 #include <sys/ktrace.h>
133 #include <sys/imgact.h>
137 * SIZE_MAXPTR The maximum size of a user space pointer, in bytes
138 * SIZE_IMG_STRSPACE The available string space, minus two pointers; we
139 * define it interms of the maximum, since we don't
140 * know the pointer size going in, until after we've
141 * parsed the executable image.
143 #define SIZE_MAXPTR 8 /* 64 bits */
144 #define SIZE_IMG_STRSPACE (NCARGS - 2 * SIZE_MAXPTR)
148 extern vm_map_t bsd_pageable_map
;
149 extern struct fileops vnops
;
151 #define ROUND_PTR(type, addr) \
152 (type *)( ( (unsigned)(addr) + 16 - 1) \
155 struct image_params
; /* Forward */
156 static int exec_copyout_strings(struct image_params
*imgp
, user_addr_t
*stackp
);
157 static int load_return_to_errno(load_return_t lrtn
);
158 static int execargs_alloc(struct image_params
*imgp
);
159 static int execargs_free(struct image_params
*imgp
);
160 static int exec_check_permissions(struct image_params
*imgp
);
161 static int exec_extract_strings(struct image_params
*imgp
);
162 static int exec_handle_sugid(struct image_params
*imgp
);
163 static int sugid_scripts
= 0;
164 SYSCTL_INT (_kern
, OID_AUTO
, sugid_scripts
, CTLFLAG_RW
, &sugid_scripts
, 0, "");
165 static kern_return_t
create_unix_stack(vm_map_t map
, user_addr_t user_stack
,
166 int customstack
, struct proc
*p
);
167 static int copyoutptr(user_addr_t ua
, user_addr_t ptr
, int ptr_size
);
169 /* XXX forward; should be in headers, but can't be for one reason or another */
170 extern int grade_binary(cpu_type_t exectype
, cpu_subtype_t execsubtype
);
171 extern void vfork_return(thread_t th_act
,
177 extern char classichandler
[32];
178 extern uint32_t classichandler_fsid
;
179 extern long classichandler_fileid
;
185 * Add the requested string to the string space area.
187 * Parameters; struct image_params * image parameter block
188 * user_addr_t string to add to strings area
189 * uio_seg segment where string is located
192 * !0 Failure errno from copyinstr()
195 * (imgp->ip_strendp) updated location of next add, if any
196 * (imgp->ip_strspace) updated byte count of space remaining
199 exec_add_string(struct image_params
*imgp
, user_addr_t str
, /*uio_seg*/int seg
)
205 if (imgp
->ip_strspace
<= 0) {
209 if (IS_UIO_SYS_SPACE(seg
)) {
210 char *kstr
= CAST_DOWN(char *,str
); /* SAFE */
211 error
= copystr(kstr
, imgp
->ip_strendp
, imgp
->ip_strspace
, &len
);
213 error
= copyinstr(str
, imgp
->ip_strendp
, imgp
->ip_strspace
,
216 imgp
->ip_strendp
+= len
;
217 imgp
->ip_strspace
-= len
;
218 } while (error
== ENAMETOOLONG
);
226 * To support new app package launching for Mac OS X, the dyld needs the
227 * first argument to execve() stored on the user stack.
229 * Save the executable path name at the top of the strings area and set
230 * the argument vector pointer to the location following that to indicate
231 * the start of the argument and environment tuples, setting the remaining
232 * string space count to the size of the string area minus the path length
233 * and a reserve for two pointers.
235 * Parameters; struct image_params * image parameter block
236 * char * path used to invoke program
237 * uio_seg segment where path is located
239 * Returns: int 0 Success
240 * !0 Failure: error number
242 * (imgp->ip_strings) saved path
243 * (imgp->ip_strspace) space remaining in ip_strings
244 * (imgp->ip_argv) beginning of argument list
245 * (imgp->ip_strendp) start of remaining copy area
247 * Note: We have to do this before the initial namei() since in the
248 * path contains symbolic links, namei() will overwrite the
249 * original path buffer contents. If the last symbolic link
250 * resolved was a relative pathname, we would lose the original
251 * "path", which could be an absolute pathname. This might be
252 * unacceptable for dyld.
255 exec_save_path(struct image_params
*imgp
, user_addr_t path
, /*uio_seg*/int seg
)
259 char *kpath
= CAST_DOWN(char *,path
); /* SAFE */
261 imgp
->ip_strendp
= imgp
->ip_strings
;
262 imgp
->ip_strspace
= SIZE_IMG_STRSPACE
;
264 len
= MIN(MAXPATHLEN
, imgp
->ip_strspace
);
267 case UIO_USERSPACE32
:
268 case UIO_USERSPACE64
: /* Same for copyin()... */
269 error
= copyinstr(path
, imgp
->ip_strings
, len
, &len
);
272 error
= copystr(kpath
, imgp
->ip_strings
, len
, &len
);
280 imgp
->ip_strendp
+= len
;
281 imgp
->ip_strspace
-= len
;
282 imgp
->ip_argv
= imgp
->ip_strendp
;
293 * Image activator for interpreter scripts. If the image begins with the
294 * characters "#!", then it is an interpreter script. Verify that we are
295 * not already executing in Classic mode, and that the length of the script
296 * line indicating the interpreter is not in excess of the maximum allowed
297 * size. If this is the case, then break out the arguments, if any, which
298 * are separated by white space, and copy them into the argument save area
299 * as if they were provided on the command line before all other arguments.
300 * The line ends when we encounter a comment character ('#') or newline.
302 * Parameters; struct image_params * image parameter block
304 * Returns: -1 not an interpreter (keep looking)
305 * -3 Success: interpreter: relookup
306 * >0 Failure: interpreter: error number
308 * A return value other than -1 indicates subsequent image activators should
309 * not be given the opportunity to attempt to activate the image.
312 exec_shell_imgact(struct image_params
*imgp
)
314 char *vdata
= imgp
->ip_vdata
;
320 * Make sure it's a shell script. If we've already redirected
321 * from an interpreted file once, don't do it again.
323 * Note: We disallow Classic, since the expectation is that we
324 * may run a Classic interpreter, but not an interpret a Classic
325 * image. This is consistent with historical behaviour.
327 if (vdata
[0] != '#' ||
329 (imgp
->ip_flags
& IMGPF_INTERPRET
) != 0) {
334 imgp
->ip_flags
|= IMGPF_INTERPRET
;
336 /* Check to see if SUGID scripts are permitted. If they aren't then
337 * clear the SUGID bits.
338 * imgp->ip_vattr is known to be valid.
340 if (sugid_scripts
== 0) {
341 imgp
->ip_origvattr
->va_mode
&= ~(VSUID
| VSGID
);
344 /* Find the nominal end of the interpreter line */
345 for( ihp
= &vdata
[2]; *ihp
!= '\n' && *ihp
!= '#'; ihp
++) {
346 if (ihp
>= &vdata
[IMG_SHSIZE
])
352 /* Skip over leading spaces - until the interpreter name */
353 while ( ihp
< line_endp
&& ((*ihp
== ' ') || (*ihp
== '\t')))
357 * Find the last non-whitespace character before the end of line or
358 * the beginning of a comment; this is our new end of line.
360 for (;line_endp
> ihp
&& ((*line_endp
== ' ') || (*line_endp
== '\t')); line_endp
--)
364 if (line_endp
== ihp
)
367 /* copy the interpreter name */
368 interp
= imgp
->ip_interp_name
;
369 while ((ihp
< line_endp
) && (*ihp
!= ' ') && (*ihp
!= '\t'))
373 exec_save_path(imgp
, CAST_USER_ADDR_T(imgp
->ip_interp_name
),
377 while (ihp
< line_endp
) {
378 /* Skip leading whitespace before each argument */
379 while ((*ihp
== ' ') || (*ihp
== '\t'))
382 if (ihp
>= line_endp
)
385 /* We have an argument; copy it */
386 while ((ihp
< line_endp
) && (*ihp
!= ' ') && (*ihp
!= '\t')) {
387 *imgp
->ip_strendp
++ = *ihp
++;
390 *imgp
->ip_strendp
++ = 0;
403 * Image activator for fat 1.0 binaries. If the binary is fat, then we
404 * need to select an image from it internally, and make that the image
405 * we are going to attempt to execute. At present, this consists of
406 * reloading the first page for the image with a first page from the
407 * offset location indicated by the fat header.
409 * Important: This image activator is byte order neutral.
411 * Note: If we find an encapsulated binary, we make no assertions
412 * about its validity; instead, we leave that up to a rescan
413 * for an activator to claim it, and, if it is claimed by one,
414 * that activator is responsible for determining validity.
417 exec_fat_imgact(struct image_params
*imgp
)
419 struct proc
*p
= vfs_context_proc(imgp
->ip_vfs_context
);
420 kauth_cred_t cred
= p
->p_ucred
;
421 struct fat_header
*fat_header
= (struct fat_header
*)imgp
->ip_vdata
;
422 struct fat_arch fat_arch
;
426 /* Make sure it's a fat binary */
427 if ((fat_header
->magic
!= FAT_MAGIC
) &&
428 (fat_header
->magic
!= FAT_CIGAM
)) {
433 /* Look up our preferred architecture in the fat file. */
434 lret
= fatfile_getarch_affinity(imgp
->ip_vp
,
435 (vm_offset_t
)fat_header
,
437 (p
->p_flag
& P_AFFINITY
));
438 if (lret
!= LOAD_SUCCESS
) {
439 error
= load_return_to_errno(lret
);
443 /* Read the Mach-O header out of it */
444 error
= vn_rdwr(UIO_READ
, imgp
->ip_vp
, imgp
->ip_vdata
,
445 PAGE_SIZE
, fat_arch
.offset
,
446 UIO_SYSSPACE32
, (IO_UNIT
|IO_NODELOCKED
),
452 /* Did we read a complete header? */
458 /* Success. Indicate we have identified an encapsulated binary */
460 imgp
->ip_arch_offset
= (user_size_t
)fat_arch
.offset
;
461 imgp
->ip_arch_size
= (user_size_t
)fat_arch
.size
;
470 * Image activator for mach-o 1.0 binaries.
472 * Important: This image activator is NOT byte order neutral.
475 exec_mach_imgact(struct image_params
*imgp
)
477 struct mach_header
*mach_header
= (struct mach_header
*)imgp
->ip_vdata
;
478 kauth_cred_t cred
= vfs_context_ucred(imgp
->ip_vfs_context
);
479 struct proc
*p
= vfs_context_proc(imgp
->ip_vfs_context
);
485 struct uthread
*uthread
;
486 vm_map_t old_map
= VM_MAP_NULL
;
488 boolean_t clean_regions
= FALSE
;
489 shared_region_mapping_t initial_region
= NULL
;
491 load_result_t load_result
;
494 * make sure it's a Mach-O 1.0 or Mach-O 2.0 binary; the difference
495 * is a reserved field on the end, so for the most part, we can
496 * treat them as if they were identical.
498 if ((mach_header
->magic
!= MH_MAGIC
) &&
499 (mach_header
->magic
!= MH_MAGIC_64
)) {
504 task
= current_task();
505 thread
= current_thread();
506 uthread
= get_bsdthread_info(thread
);
508 if (uthread
->uu_flag
& UT_VFORK
)
509 vfexec
= 1; /* Mark in exec */
511 if ((mach_header
->cputype
& CPU_ARCH_ABI64
) == CPU_ARCH_ABI64
)
512 imgp
->ip_flags
|= IMGPF_IS_64BIT
;
514 if (!grade_binary(mach_header
->cputype
, mach_header
->cpusubtype
)) {
520 * Copy in arguments/environment from the old process, if the
521 * vector is non-NULL (i.e. exec is not being called from
522 * load_init_program(), as a special case, at system startup).
524 if (imgp
->ip_user_argv
!= 0LL) {
525 error
= exec_extract_strings(imgp
);
531 * Hack for binary compatability; put three NULs on the end of the
532 * string area, and round it up to the next word boundary. This
533 * ensures padding with NULs to the boundary.
535 imgp
->ip_strendp
[0] = 0;
536 imgp
->ip_strendp
[1] = 0;
537 imgp
->ip_strendp
[2] = 0;
538 imgp
->ip_strendp
+= (((imgp
->ip_strendp
- imgp
->ip_strings
) + NBPW
-1) & ~(NBPW
-1));
542 kern_return_t result
;
544 result
= task_create_internal(task
, FALSE
, &new_task
);
545 if (result
!= KERN_SUCCESS
)
546 printf("execve: task_create failed. Code: 0x%x\n", result
);
548 set_bsdtask_info(new_task
, p
);
551 map
= get_task_map(new_task
);
552 result
= thread_create(new_task
, &imgp
->ip_vfork_thread
);
553 if (result
!= KERN_SUCCESS
)
554 printf("execve: thread_create failed. Code: 0x%x\n", result
);
555 /* reset local idea of task, thread, uthread */
557 thread
= imgp
->ip_vfork_thread
;
558 uthread
= get_bsdthread_info(thread
);
564 * We set these flags here; this is OK, since if we fail after
565 * this point, we have already destroyed the parent process anyway.
567 if (imgp
->ip_flags
& IMGPF_IS_64BIT
) {
568 task_set_64bit(task
, TRUE
);
571 task_set_64bit(task
, FALSE
);
572 p
->p_flag
&= ~P_LP64
;
576 * Load the Mach-O file.
578 /* LP64 - remove following "if" statement after osfmk/vm/task_working_set.c */
579 if((imgp
->ip_flags
& IMGPF_IS_64BIT
) == 0)
580 if(imgp
->ip_tws_cache_name
) {
581 tws_handle_startup_file(task
, kauth_cred_getuid(cred
),
582 imgp
->ip_tws_cache_name
, imgp
->ip_vp
, &clean_regions
);
585 vm_get_shared_region(task
, &initial_region
);
589 * NOTE: An error after this point indicates we have potentially
590 * destroyed or overwrote some process state while attempting an
591 * execve() following a vfork(), which is an unrecoverable condition.
595 * We reset the task to 64-bit (or not) here. It may have picked up
596 * a new map, and we need that to reflect its true 64-bit nature.
599 ((imgp
->ip_flags
& IMGPF_IS_64BIT
) == IMGPF_IS_64BIT
));
602 * Actually load the image file we previously decided to load.
604 lret
= load_machfile(imgp
, mach_header
, thread
, map
, clean_regions
, &load_result
);
606 if (lret
!= LOAD_SUCCESS
) {
607 error
= load_return_to_errno(lret
);
611 /* load_machfile() maps the vnode */
612 (void)ubc_map(imgp
->ip_vp
, PROT_EXEC
);
615 * deal with set[ug]id.
617 error
= exec_handle_sugid(imgp
);
619 KNOTE(&p
->p_klist
, NOTE_EXEC
);
621 if (!vfexec
&& (p
->p_flag
& P_TRACED
))
627 vnode_put(imgp
->ip_vp
);
630 if (load_result
.unixproc
&&
631 create_unix_stack(get_task_map(task
),
632 load_result
.user_stack
, load_result
.customstack
, p
)) {
633 error
= load_return_to_errno(LOAD_NOSPACE
);
638 uthread
->uu_ar0
= (void *)get_user_regs(thread
);
639 old_map
= vm_map_switch(get_task_map(task
));
642 if (load_result
.unixproc
) {
646 * Copy the strings area out into the new process address
650 error
= exec_copyout_strings(imgp
, &ap
);
653 vm_map_switch(old_map
);
657 thread_setuserstack(thread
, ap
);
660 if (load_result
.dynlinker
) {
663 /* Adjust the stack */
664 if (imgp
->ip_flags
& IMGPF_IS_64BIT
) {
665 ap
= thread_adjuserstack(thread
, -8);
666 (void)copyoutptr(load_result
.mach_header
, ap
, 8);
668 ap
= thread_adjuserstack(thread
, -4);
669 (void)suword(ap
, load_result
.mach_header
);
674 vm_map_switch(old_map
);
676 /* Set the entry point */
677 thread_setentrypoint(thread
, load_result
.entry_point
);
683 * Reset signal state.
688 * Close file descriptors
689 * which specify close-on-exec.
694 * need to cancel async IO requests that can be cancelled and wait for those
695 * already active. MAY BLOCK!
699 /* FIXME: Till vmspace inherit is fixed: */
700 if (!vfexec
&& p
->vm_shm
)
702 /* Clean up the semaphores */
706 * Remember file name for accounting.
708 p
->p_acflag
&= ~AFORK
;
709 /* If the translated name isn't NULL, then we want to use
710 * that translated name as the name we show as the "real" name.
711 * Otherwise, use the name passed into exec.
713 if (0 != imgp
->ip_p_comm
[0]) {
714 bcopy((caddr_t
)imgp
->ip_p_comm
, (caddr_t
)p
->p_comm
,
717 if (imgp
->ip_ndp
->ni_cnd
.cn_namelen
> MAXCOMLEN
)
718 imgp
->ip_ndp
->ni_cnd
.cn_namelen
= MAXCOMLEN
;
719 bcopy((caddr_t
)imgp
->ip_ndp
->ni_cnd
.cn_nameptr
, (caddr_t
)p
->p_comm
,
720 (unsigned)imgp
->ip_ndp
->ni_cnd
.cn_namelen
);
721 p
->p_comm
[imgp
->ip_ndp
->ni_cnd
.cn_namelen
] = '\0';
725 /* This is for kdebug */
726 long dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
;
728 /* Collect the pathname for tracing */
729 kdbg_trace_string(p
, &dbg_arg1
, &dbg_arg2
, &dbg_arg3
, &dbg_arg4
);
735 KERNEL_DEBUG_CONSTANT1((TRACEDBG_CODE(DBG_TRACE_DATA
, 2)) | DBG_FUNC_NONE
,
736 p
->p_pid
,0,0,0, (unsigned int)thread
);
737 KERNEL_DEBUG_CONSTANT1((TRACEDBG_CODE(DBG_TRACE_STRING
, 2)) | DBG_FUNC_NONE
,
738 dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
, (unsigned int)thread
);
742 KERNEL_DEBUG_CONSTANT((TRACEDBG_CODE(DBG_TRACE_DATA
, 2)) | DBG_FUNC_NONE
,
744 KERNEL_DEBUG_CONSTANT((TRACEDBG_CODE(DBG_TRACE_STRING
, 2)) | DBG_FUNC_NONE
,
745 dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
, 0);
749 p
->p_flag
&= ~P_CLASSIC
;
752 * mark as execed, wakeup the process that vforked (if any) and tell
753 * it that it now has it's own resources back
756 if (p
->p_pptr
&& (p
->p_flag
& P_PPWAIT
)) {
757 p
->p_flag
&= ~P_PPWAIT
;
758 wakeup((caddr_t
)p
->p_pptr
);
761 if (vfexec
&& (p
->p_flag
& P_TRACED
)) {
762 psignal_vfork(p
, new_task
, thread
, SIGTRAP
);
767 task_deallocate(new_task
);
768 thread_deallocate(thread
);
781 * Our image activator table; this is the table of the image types we are
782 * capable of loading. We list them in order of preference to ensure the
783 * fastest image load speed.
785 * XXX hardcoded, for now; should use linker sets
788 int (*ex_imgact
)(struct image_params
*);
791 { exec_mach_imgact
, "Mach-o Binary" },
792 { exec_fat_imgact
, "Fat Binary" },
793 { exec_shell_imgact
, "Interpreter Script" },
799 * TODO: Dynamic linker header address on stack is copied via suword()
803 execve(struct proc
*p
, struct execve_args
*uap
, register_t
*retval
)
805 kauth_cred_t cred
= p
->p_ucred
;
806 struct image_params image_params
, *imgp
;
807 struct vnode_attr va
;
808 struct vnode_attr origva
;
810 struct uthread
*uthread
;
816 int once
= 1; /* save SGUID-ness for interpreted files */
817 char alt_p_comm
[sizeof(p
->p_comm
)] = {0}; /* for Classic */
818 int is_64
= IS_64BIT_PROCESS(p
);
819 int seg
= (is_64
? UIO_USERSPACE64
: UIO_USERSPACE32
);
820 struct vfs_context context
;
823 context
.vc_ucred
= p
->p_ucred
; /* XXX must NOT be kauth_cred_get() */
826 imgp
= &image_params
;
828 /* Initialize the common data in the image_params structure */
829 bzero(imgp
, sizeof(*imgp
));
830 imgp
->ip_user_fname
= uap
->fname
;
831 imgp
->ip_user_argv
= uap
->argp
;
832 imgp
->ip_user_envv
= uap
->envp
;
833 imgp
->ip_vattr
= &va
;
834 imgp
->ip_origvattr
= &origva
;
835 imgp
->ip_vfs_context
= &context
;
836 imgp
->ip_flags
= (is_64
? IMGPF_WAS_64BIT
: IMGPF_NONE
);
837 imgp
->ip_tws_cache_name
= NULL
;
838 imgp
->ip_p_comm
= alt_p_comm
; /* for Classic */
841 * XXXAUDIT: Currently, we only audit the pathname of the binary.
842 * There may also be poor interaction with dyld.
845 task
= current_task();
846 uthread
= get_bsdthread_info(current_thread());
848 if (uthread
->uu_flag
& UT_VFORK
) {
849 vfexec
= 1; /* Mark in exec */
851 if (task
!= kernel_task
) {
852 numthreads
= get_task_numacts(task
);
853 if (numthreads
<= 0 )
855 if (numthreads
> 1) {
861 error
= execargs_alloc(imgp
);
866 * XXXAUDIT: Note: the double copyin introduces an audit
867 * race. To correct this race, we must use a single
868 * copyin(), e.g. by passing a flag to namei to indicate an
869 * external path buffer is being used.
871 error
= exec_save_path(imgp
, uap
->fname
, seg
);
878 * No app profiles under chroot
880 if((p
->p_fd
->fd_rdir
== NULLVP
) && (app_profile
!= 0)) {
882 /* grab the name of the file out of its path */
883 /* we will need this for lookup within the */
885 /* Scan backwards for the first '/' or start of string */
886 imgp
->ip_tws_cache_name
= imgp
->ip_strendp
;
887 while (imgp
->ip_tws_cache_name
[0] != '/') {
888 if(imgp
->ip_tws_cache_name
== imgp
->ip_strings
) {
889 imgp
->ip_tws_cache_name
--;
892 imgp
->ip_tws_cache_name
--;
894 imgp
->ip_tws_cache_name
++;
896 NDINIT(&nd
, LOOKUP
, FOLLOW
| LOCKLEAF
| AUDITVNPATH1
,
897 seg
, uap
->fname
, imgp
->ip_vfs_context
);
903 imgp
->ip_ndp
= &nd
; /* successful namei(); call nameidone() later */
904 imgp
->ip_vp
= nd
.ni_vp
; /* if set, need to vnode_put() at some point */
906 error
= exec_check_permissions(imgp
);
910 /* Copy; avoid invocation of an interpreter overwriting the original */
916 error
= vn_rdwr(UIO_READ
, imgp
->ip_vp
, imgp
->ip_vdata
, PAGE_SIZE
, 0,
917 UIO_SYSSPACE32
, IO_NODELOCKED
, cred
, &resid
, p
);
923 for(i
= 0; error
== -1 && execsw
[i
].ex_imgact
!= NULL
; i
++) {
925 error
= (*execsw
[i
].ex_imgact
)(imgp
);
928 /* case -1: not claimed: continue */
929 case -2: /* Encapsulated binary */
930 goto encapsulated_binary
;
932 case -3: /* Interpreter */
933 vnode_put(imgp
->ip_vp
);
934 imgp
->ip_vp
= NULL
; /* already put */
935 nd
.ni_cnd
.cn_nameiop
= LOOKUP
;
936 nd
.ni_cnd
.cn_flags
= (nd
.ni_cnd
.cn_flags
& HASBUF
) |
940 nd
.ni_segflg
= UIO_SYSSPACE32
;
941 nd
.ni_dirp
= CAST_USER_ADDR_T(imgp
->ip_interp_name
);
949 /* call out to allow 3rd party notification of exec.
950 * Ignore result of kauth_authorize_fileop call.
952 if (error
== 0 && kauth_authorize_fileop_has_listeners()) {
953 kauth_authorize_fileop(vfs_context_ucred(&context
), KAUTH_FILEOP_EXEC
,
954 (uintptr_t)nd
.ni_vp
, 0);
957 /* Image not claimed by any activator? */
963 nameidone(imgp
->ip_ndp
);
965 vnode_put(imgp
->ip_vp
);
966 if (imgp
->ip_strings
)
968 if (!error
&& vfexec
) {
969 vfork_return(current_thread(), p
->p_pptr
, p
, retval
);
970 (void)thread_resume(imgp
->ip_vfork_thread
);
978 copyinptr(user_addr_t froma
, user_addr_t
*toptr
, int ptr_size
)
983 /* 64 bit value containing 32 bit address */
986 error
= copyin(froma
, &i
, 4);
987 *toptr
= CAST_USER_ADDR_T(i
); /* SAFE */
989 error
= copyin(froma
, toptr
, 8);
996 copyoutptr(user_addr_t ua
, user_addr_t ptr
, int ptr_size
)
1000 if (ptr_size
== 4) {
1001 /* 64 bit value containing 32 bit address */
1002 unsigned int i
= CAST_DOWN(unsigned int,ua
); /* SAFE */
1004 error
= copyout(&i
, ptr
, 4);
1006 error
= copyout(&ua
, ptr
, 8);
1013 * exec_copyout_strings
1015 * Copy out the strings segment to user space. The strings segment is put
1016 * on a preinitialized stack frame.
1018 * Parameters: struct image_params * the image parameter block
1019 * int * a pointer to the stack offset variable
1021 * Returns: 0 Success
1025 * (*stackp) The stack offset, modified
1027 * Note: The strings segment layout is backward, from the beginning
1028 * of the top of the stack to consume the minimal amount of
1029 * space possible; the returned stack pointer points to the
1030 * end of the area consumed (stacks grow upward).
1032 * argc is an int; arg[i] are pointers; env[i] are pointers;
1033 * exec_path is a pointer; the 0's are (void *)NULL's
1035 * The stack frame layout is:
1058 * | exec_path | In MacOS X PR2 Beaker2E the path passed to exec() is
1059 * +-------------+ passed on the stack just after the trailing 0 of the
1060 * | 0 | the envp[] array as a pointer to a string.
1067 * | | <- p->user_stack
1070 * Although technically a part of the STRING AREA, we treat the PATH AREA as
1071 * a separate entity. This allows us to align the beginning of the PATH AREA
1072 * to a pointer boundary so that the exec_path, env[i], and argv[i] pointers
1073 * which preceed it on the stack are properly aligned.
1075 * TODO: argc copied with suword(), which takes a 64 bit address
1078 exec_copyout_strings(struct image_params
*imgp
, user_addr_t
*stackp
)
1080 struct proc
*p
= vfs_context_proc(imgp
->ip_vfs_context
);
1081 int ptr_size
= (imgp
->ip_flags
& IMGPF_IS_64BIT
) ? 8 : 4;
1082 char *argv
= imgp
->ip_argv
; /* modifiable copy of argv */
1083 user_addr_t string_area
; /* *argv[], *env[] */
1084 user_addr_t path_area
; /* package launch path */
1085 user_addr_t ptr_area
; /* argv[], env[], exec_path */
1087 int stringc
= imgp
->ip_argc
+ imgp
->ip_envc
;
1095 * Set up pointers to the beginning of the string area, the beginning
1096 * of the path area, and the beginning of the pointer area (actually,
1097 * the location of argc, an int, which may be smaller than a pointer,
1098 * but we use ptr_size worth of space for it, for alignment).
1100 string_area
= stack
- (((imgp
->ip_strendp
- imgp
->ip_strings
) + ptr_size
-1) & ~(ptr_size
-1)) - ptr_size
;
1101 path_area
= string_area
- (((imgp
->ip_argv
- imgp
->ip_strings
) + ptr_size
-1) & ~(ptr_size
-1));
1102 ptr_area
= path_area
- ((imgp
->ip_argc
+ imgp
->ip_envc
+ 4) * ptr_size
) - ptr_size
/*argc*/;
1104 /* Return the initial stack address: the location of argc */
1108 * Record the size of the arguments area so that sysctl_procargs()
1109 * can return the argument area without having to parse the arguments.
1111 p
->p_argc
= imgp
->ip_argc
;
1112 p
->p_argslen
= (int)(stack
- path_area
);
1116 * Support for new app package launching for Mac OS X allocates
1117 * the "path" at the begining of the imgp->ip_strings buffer.
1118 * copy it just before the string area.
1121 error
= copyoutstr(imgp
->ip_strings
, path_area
,
1122 (unsigned)(imgp
->ip_argv
- imgp
->ip_strings
),
1128 /* Save a NULL pointer below it */
1129 (void)copyoutptr(0LL, path_area
- ptr_size
, ptr_size
);
1131 /* Save the pointer to "path" just below it */
1132 (void)copyoutptr(path_area
, path_area
- 2*ptr_size
, ptr_size
);
1135 * ptr_size for 2 NULL one each ofter arg[argc -1] and env[n]
1137 * skip over saved path, ptr_size for pointer to path,
1138 * and ptr_size for the NULL after pointer to path.
1141 /* argc (int32, stored in a ptr_size area) */
1142 (void)suword(ptr_area
, imgp
->ip_argc
);
1143 ptr_area
+= sizeof(int);
1144 /* pad to ptr_size, if 64 bit image, to ensure user stack alignment */
1145 if (imgp
->ip_flags
& IMGPF_IS_64BIT
) {
1146 (void)suword(ptr_area
, 0); /* int, not long: ignored */
1147 ptr_area
+= sizeof(int);
1152 * We use (string_area - path_area) here rather than the more
1153 * intuitive (imgp->ip_argv - imgp->ip_strings) because we are
1154 * interested in the length of the PATH_AREA in user space,
1155 * rather than the actual length of the execution path, since
1156 * it includes alignment padding of the PATH_AREA + STRING_AREA
1157 * to a ptr_size boundary.
1159 strspace
= SIZE_IMG_STRSPACE
- (string_area
- path_area
);
1161 if (stringc
== imgp
->ip_envc
) {
1162 /* argv[n] = NULL */
1163 (void)copyoutptr(0LL, ptr_area
, ptr_size
);
1164 ptr_area
+= ptr_size
;
1169 /* pointer: argv[n]/env[n] */
1170 (void)copyoutptr(string_area
, ptr_area
, ptr_size
);
1172 /* string : argv[n][]/env[n][] */
1174 if (strspace
<= 0) {
1178 error
= copyoutstr(argv
, string_area
,
1184 } while (error
== ENAMETOOLONG
);
1185 if (error
== EFAULT
|| error
== E2BIG
)
1186 break; /* bad stack - user's problem */
1187 ptr_area
+= ptr_size
;
1190 (void)copyoutptr(0LL, ptr_area
, ptr_size
);
1198 * exec_extract_strings
1200 * Copy arguments and environment from user space into work area; we may
1201 * have already copied some early arguments into the work area, and if
1202 * so, any arguments opied in are appended to those already there.
1204 * Parameters: struct image_params * the image parameter block
1206 * Returns: 0 Success
1210 * (imgp->ip_argc) Count of arguments, updated
1211 * (imgp->ip_envc) Count of environment strings, updated
1214 * Notes: The argument and environment vectors are user space pointers
1215 * to arrays of user space pointers.
1218 exec_extract_strings(struct image_params
*imgp
)
1221 struct proc
*p
= vfs_context_proc(imgp
->ip_vfs_context
);
1222 int seg
= (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
);
1223 int ptr_size
= (imgp
->ip_flags
& IMGPF_WAS_64BIT
) ? 8 : 4;
1224 user_addr_t argv
= imgp
->ip_user_argv
;
1225 user_addr_t envv
= imgp
->ip_user_envv
;
1227 /* Now, get rest of arguments */
1230 * If we are running an interpreter, replace the av[0] that was
1231 * passed to execve() with the fully qualified path name that was
1232 * passed to execve() for interpreters which do not use the PATH
1233 * to locate their script arguments.
1235 if((imgp
->ip_flags
& IMGPF_INTERPRET
) != 0 && argv
!= 0LL) {
1238 error
= copyinptr(argv
, &arg
, ptr_size
);
1241 if (arg
!= 0LL && arg
!= (user_addr_t
)-1) {
1243 error
= exec_add_string(imgp
, imgp
->ip_user_fname
, seg
);
1250 while (argv
!= 0LL) {
1253 error
= copyinptr(argv
, &arg
, ptr_size
);
1260 } else if (arg
== (user_addr_t
)-1) {
1261 /* Um... why would it be -1? */
1268 error
= exec_add_string(imgp
, arg
, seg
);
1274 /* Now, get the environment */
1275 while (envv
!= 0LL) {
1278 error
= copyinptr(envv
, &env
, ptr_size
);
1285 } else if (env
== (user_addr_t
)-1) {
1292 error
= exec_add_string(imgp
, env
, seg
);
1302 #define unix_stack_size(p) (p->p_rlimit[RLIMIT_STACK].rlim_cur)
1305 exec_check_permissions(struct image_params
*imgp
)
1307 struct vnode
*vp
= imgp
->ip_vp
;
1308 struct vnode_attr
*vap
= imgp
->ip_vattr
;
1309 struct proc
*p
= vfs_context_proc(imgp
->ip_vfs_context
);
1311 kauth_action_t action
;
1313 /* Only allow execution of regular files */
1314 if (!vnode_isreg(vp
))
1317 /* Get the file attributes that we will be using here and elsewhere */
1319 VATTR_WANTED(vap
, va_uid
);
1320 VATTR_WANTED(vap
, va_gid
);
1321 VATTR_WANTED(vap
, va_mode
);
1322 VATTR_WANTED(vap
, va_fsid
);
1323 VATTR_WANTED(vap
, va_fileid
);
1324 VATTR_WANTED(vap
, va_data_size
);
1325 if ((error
= vnode_getattr(vp
, vap
, imgp
->ip_vfs_context
)) != 0)
1329 * Ensure that at least one execute bit is on - otherwise root
1330 * will always succeed, and we don't want to happen unless the
1331 * file really is executable.
1333 if ((vap
->va_mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
)) == 0)
1336 /* Disallow zero length files */
1337 if (vap
->va_data_size
== 0)
1340 imgp
->ip_arch_offset
= (user_size_t
)0;
1341 imgp
->ip_arch_size
= vap
->va_data_size
;
1343 /* Disable setuid-ness for traced programs or if MNT_NOSUID */
1344 if ((vp
->v_mount
->mnt_flag
& MNT_NOSUID
) || (p
->p_flag
& P_TRACED
))
1345 vap
->va_mode
&= ~(VSUID
| VSGID
);
1347 /* Check for execute permission */
1348 action
= KAUTH_VNODE_EXECUTE
;
1349 /* Traced images must also be readable */
1350 if (p
->p_flag
& P_TRACED
)
1351 action
|= KAUTH_VNODE_READ_DATA
;
1352 if ((error
= vnode_authorize(vp
, NULL
, action
, imgp
->ip_vfs_context
)) != 0)
1355 /* Don't let it run if anyone had it open for writing */
1356 if (vp
->v_writecount
)
1360 /* XXX May want to indicate to underlying FS that vnode is open */
1368 * Initially clear the P_SUGID in the process flags; if an SUGID process is
1369 * exec'ing a non-SUGID image, then this is the point of no return.
1371 * If the image being activated is SUGI, then replace the credential with a
1372 * copy, disable tracing (unless the tracing process is root), reset the
1373 * mach task port to revoke it, set the P_SUGID bit,
1375 * If the saved user and group ID will be changing, then make sure it happens
1376 * to a new credential, rather than a shared one.
1378 * Set the security token (this is probably obsolete, given that the token
1379 * should not technically be separate from the credential itself).
1381 * Parameters: struct image_params * the image parameter block
1383 * Returns: void No failure indication
1386 * <process credential> Potentially modified/replaced
1387 * <task port> Potentially revoked
1388 * <process flags> P_SUGID bit potentially modified
1389 * <security token> Potentially modified
1392 exec_handle_sugid(struct image_params
*imgp
)
1394 kauth_cred_t cred
= vfs_context_ucred(imgp
->ip_vfs_context
);
1395 struct proc
*p
= vfs_context_proc(imgp
->ip_vfs_context
);
1398 static struct vnode
*dev_null
= NULLVP
;
1400 p
->p_flag
&= ~P_SUGID
;
1402 if (((imgp
->ip_origvattr
->va_mode
& VSUID
) != 0 &&
1403 kauth_cred_getuid(cred
) != imgp
->ip_origvattr
->va_uid
) ||
1404 ((imgp
->ip_origvattr
->va_mode
& VSGID
) != 0 &&
1405 cred
->cr_gid
!= imgp
->ip_origvattr
->va_gid
)) {
1408 * If process is being ktraced, turn off - unless
1411 if (p
->p_tracep
&& !(p
->p_traceflag
& KTRFAC_ROOT
)) {
1412 struct vnode
*tvp
= p
->p_tracep
;
1419 * Replace the credential with a copy of itself if euid or egid change.
1421 if (imgp
->ip_origvattr
->va_mode
& VSUID
) {
1422 p
->p_ucred
= kauth_cred_seteuid(p
->p_ucred
, imgp
->ip_origvattr
->va_uid
);
1424 if (imgp
->ip_origvattr
->va_mode
& VSGID
) {
1425 p
->p_ucred
= kauth_cred_setegid(p
->p_ucred
, imgp
->ip_origvattr
->va_gid
);
1429 * Have mach reset the task port. We don't want
1430 * anyone who had the task port before a setuid
1431 * exec to be able to access/control the task
1434 if (current_task() == p
->task
)
1435 ipc_task_reset(p
->task
);
1437 p
->p_flag
|= P_SUGID
;
1439 /* Cache the vnode for /dev/null the first time around */
1440 if (dev_null
== NULLVP
) {
1441 struct nameidata nd1
;
1443 NDINIT(&nd1
, LOOKUP
, FOLLOW
, UIO_SYSSPACE32
,
1444 CAST_USER_ADDR_T("/dev/null"),
1445 imgp
->ip_vfs_context
);
1447 if ((error
= vn_open(&nd1
, FREAD
, 0)) == 0) {
1448 dev_null
= nd1
.ni_vp
;
1450 * vn_open returns with both a use_count
1451 * and an io_count on the found vnode
1452 * drop the io_count, but keep the use_count
1454 vnode_put(nd1
.ni_vp
);
1458 /* Radar 2261856; setuid security hole fix */
1459 /* Patch from OpenBSD: A. Ramesh */
1461 * XXX For setuid processes, attempt to ensure that
1462 * stdin, stdout, and stderr are already allocated.
1463 * We do not want userland to accidentally allocate
1464 * descriptors in this range which has implied meaning
1467 if (dev_null
!= NULLVP
) {
1468 for (i
= 0; i
< 3; i
++) {
1469 struct fileproc
*fp
;
1472 if (p
->p_fd
->fd_ofiles
[i
] != NULL
)
1475 if ((error
= falloc(p
, &fp
, &indx
)) != 0)
1478 if ((error
= vnode_ref_ext(dev_null
, FREAD
)) != 0) {
1479 fp_free(p
, indx
, fp
);
1483 fp
->f_fglob
->fg_flag
= FREAD
;
1484 fp
->f_fglob
->fg_type
= DTYPE_VNODE
;
1485 fp
->f_fglob
->fg_ops
= &vnops
;
1486 fp
->f_fglob
->fg_data
= (caddr_t
)dev_null
;
1489 *fdflags(p
, indx
) &= ~UF_RESERVED
;
1490 fp_drop(p
, indx
, fp
, 1);
1494 * for now we need to drop the reference immediately
1495 * since we don't have any mechanism in place to
1496 * release it before starting to unmount "/dev"
1497 * during a reboot/shutdown
1499 vnode_rele(dev_null
);
1505 * Implement the semantic where the effective user and group become
1506 * the saved user and group in exec'ed programs.
1508 p
->p_ucred
= kauth_cred_setsvuidgid(p
->p_ucred
, kauth_cred_getuid(p
->p_ucred
), p
->p_ucred
->cr_gid
);
1510 /* XXX Obsolete; security token should not be separate from cred */
1511 set_security_token(p
);
1516 static kern_return_t
1517 create_unix_stack(vm_map_t map
, user_addr_t user_stack
, int customstack
,
1520 mach_vm_size_t size
;
1521 mach_vm_offset_t addr
;
1523 p
->user_stack
= user_stack
;
1525 size
= mach_vm_round_page(unix_stack_size(p
));
1526 addr
= mach_vm_trunc_page(user_stack
- size
);
1527 return (mach_vm_allocate(map
, &addr
, size
,
1528 VM_MAKE_TAG(VM_MEMORY_STACK
) |
1531 return(KERN_SUCCESS
);
1534 #include <sys/reboot.h>
1536 static char init_program_name
[128] = "/sbin/launchd";
1537 static const char * other_init
= "/sbin/mach_init";
1539 char init_args
[128] = "";
1541 struct execve_args init_exec_args
;
1542 int init_attempts
= 0;
1546 load_init_program(struct proc
*p
)
1548 vm_offset_t init_addr
;
1551 register_t retval
[2];
1555 /* init_args are copied in string form directly from bootstrap */
1558 if (boothowto
& RB_INITNAME
) {
1559 printf("init program? ");
1561 gets(init_program_name
, init_program_name
);
1562 #endif /* FIXME ] */
1565 if (error
&& ((boothowto
& RB_INITNAME
) == 0) &&
1566 (init_attempts
== 1)) {
1567 printf("Load of %s, errno %d, trying %s\n",
1568 init_program_name
, error
, other_init
);
1570 bcopy(other_init
, init_program_name
,
1571 sizeof(other_init
));
1577 printf("Load of %s failed, errno %d\n",
1578 init_program_name
, error
);
1580 boothowto
|= RB_INITNAME
;
1585 * Copy out program name.
1588 init_addr
= VM_MIN_ADDRESS
;
1589 (void) vm_allocate(current_map(), &init_addr
,
1590 PAGE_SIZE
, VM_FLAGS_ANYWHERE
);
1594 (void) copyout((caddr_t
) init_program_name
,
1595 CAST_USER_ADDR_T(init_addr
),
1596 (unsigned) sizeof(init_program_name
)+1);
1598 argv
[0] = (char *) init_addr
;
1599 init_addr
+= sizeof(init_program_name
);
1600 init_addr
= (vm_offset_t
)ROUND_PTR(char, init_addr
);
1603 * Put out first (and only) argument, similarly.
1604 * Assumes everything fits in a page as allocated
1608 (void) copyout((caddr_t
) init_args
,
1609 CAST_USER_ADDR_T(init_addr
),
1610 (unsigned) sizeof(init_args
));
1612 argv
[1] = (char *) init_addr
;
1613 init_addr
+= sizeof(init_args
);
1614 init_addr
= (vm_offset_t
)ROUND_PTR(char, init_addr
);
1617 * Null-end the argument list
1620 argv
[2] = (char *) 0;
1623 * Copy out the argument list.
1626 (void) copyout((caddr_t
) argv
,
1627 CAST_USER_ADDR_T(init_addr
),
1628 (unsigned) sizeof(argv
));
1631 * Set up argument block for fake call to execve.
1634 init_exec_args
.fname
= CAST_USER_ADDR_T(argv
[0]);
1635 init_exec_args
.argp
= CAST_USER_ADDR_T((char **)init_addr
);
1636 init_exec_args
.envp
= CAST_USER_ADDR_T(0);
1638 /* So that mach_init task
1639 * is set with uid,gid 0 token
1641 set_security_token(p
);
1643 error
= execve(p
,&init_exec_args
,retval
);
1648 * Convert a load_return_t to an errno.
1651 load_return_to_errno(load_return_t lrtn
)
1677 #include <mach/mach_types.h>
1678 #include <mach/vm_prot.h>
1679 #include <mach/semaphore.h>
1680 #include <mach/sync_policy.h>
1681 #include <kern/clock.h>
1682 #include <mach/kern_return.h>
1684 extern semaphore_t execve_semaphore
;
1687 * The block of memory used by the execve arguments. At the same time,
1688 * we allocate a page so that we can read in the first page of the image.
1691 execargs_alloc(struct image_params
*imgp
)
1695 kret
= semaphore_wait(execve_semaphore
);
1696 if (kret
!= KERN_SUCCESS
)
1700 case KERN_INVALID_ADDRESS
:
1701 case KERN_PROTECTION_FAILURE
:
1704 case KERN_OPERATION_TIMED_OUT
:
1708 kret
= kmem_alloc_pageable(bsd_pageable_map
, (vm_offset_t
*)&imgp
->ip_strings
, NCARGS
+ PAGE_SIZE
);
1709 imgp
->ip_vdata
= imgp
->ip_strings
+ NCARGS
;
1710 if (kret
!= KERN_SUCCESS
) {
1711 semaphore_signal(execve_semaphore
);
1718 execargs_free(struct image_params
*imgp
)
1722 kmem_free(bsd_pageable_map
, (vm_offset_t
)imgp
->ip_strings
, NCARGS
+ PAGE_SIZE
);
1723 imgp
->ip_strings
= NULL
;
1725 kret
= semaphore_signal(execve_semaphore
);
1727 case KERN_INVALID_ADDRESS
:
1728 case KERN_PROTECTION_FAILURE
:
1731 case KERN_OPERATION_TIMED_OUT
: