2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
32 * Mach Operating System
33 * Copyright (c) 1987 Carnegie-Mellon University
34 * All rights reserved. The CMU software License Agreement specifies
35 * the terms and conditions for use and redistribution.
41 * Copyright (c) 1982, 1986, 1991, 1993
42 * The Regents of the University of California. All rights reserved.
43 * (c) UNIX System Laboratories, Inc.
44 * All or some portions of this file are derived from material licensed
45 * to the University of California by American Telephone and Telegraph
46 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
47 * the permission of UNIX System Laboratories, Inc.
49 * Redistribution and use in source and binary forms, with or without
50 * modification, are permitted provided that the following conditions
52 * 1. Redistributions of source code must retain the above copyright
53 * notice, this list of conditions and the following disclaimer.
54 * 2. Redistributions in binary form must reproduce the above copyright
55 * notice, this list of conditions and the following disclaimer in the
56 * documentation and/or other materials provided with the distribution.
57 * 3. All advertising materials mentioning features or use of this software
58 * must display the following acknowledgement:
59 * This product includes software developed by the University of
60 * California, Berkeley and its contributors.
61 * 4. Neither the name of the University nor the names of its contributors
62 * may be used to endorse or promote products derived from this software
63 * without specific prior written permission.
65 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
66 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
67 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
68 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
69 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
70 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
71 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
72 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
73 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
74 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
77 * from: @(#)kern_exec.c 8.1 (Berkeley) 6/10/93
79 #include <machine/reg.h>
81 #include <sys/param.h>
82 #include <sys/systm.h>
83 #include <sys/filedesc.h>
84 #include <sys/kernel.h>
85 #include <sys/proc_internal.h>
86 #include <sys/kauth.h>
88 #include <sys/socketvar.h>
89 #include <sys/malloc.h>
90 #include <sys/namei.h>
91 #include <sys/mount_internal.h>
92 #include <sys/vnode_internal.h>
93 #include <sys/file_internal.h>
95 #include <sys/uio_internal.h>
98 #include <sys/kdebug.h>
99 #include <sys/signal.h>
100 #include <sys/aio_kern.h>
101 #include <sys/sysproto.h>
102 #include <sys/shm_internal.h> /* shmexec() */
103 #include <sys/ubc_internal.h> /* ubc_map() */
105 #include <bsm/audit_kernel.h>
107 #include <mach/mach_types.h>
108 #include <mach/task.h>
109 #include <mach/thread_act.h>
110 #include <mach/vm_map.h>
111 #include <mach/mach_vm.h>
112 #include <mach/vm_param.h>
114 #include <vm/vm_map.h>
115 #include <vm/vm_kern.h>
116 #include <vm/vm_pager.h>
117 #include <vm/vm_kern.h>
118 #include <vm/task_working_set.h>
119 #include <vm/vm_shared_memory_server.h>
122 * Mach things for which prototypes are unavailable from Mach headers
127 extern struct savearea
*get_user_regs(thread_t
);
130 #include <kern/thread.h>
131 #include <kern/task.h>
132 #include <kern/ast.h>
133 #include <kern/mach_loader.h>
134 #include <mach-o/fat.h>
135 #include <mach-o/loader.h>
136 #include <machine/vmparam.h>
138 #include <sys/ktrace.h>
140 #include <sys/imgact.h>
144 * SIZE_MAXPTR The maximum size of a user space pointer, in bytes
145 * SIZE_IMG_STRSPACE The available string space, minus two pointers; we
146 * define it interms of the maximum, since we don't
147 * know the pointer size going in, until after we've
148 * parsed the executable image.
150 #define SIZE_MAXPTR 8 /* 64 bits */
151 #define SIZE_IMG_STRSPACE (NCARGS - 2 * SIZE_MAXPTR)
155 extern vm_map_t bsd_pageable_map
;
156 extern struct fileops vnops
;
158 #define ROUND_PTR(type, addr) \
159 (type *)( ( (unsigned)(addr) + 16 - 1) \
162 struct image_params
; /* Forward */
163 static int exec_copyout_strings(struct image_params
*imgp
, user_addr_t
*stackp
);
164 static int load_return_to_errno(load_return_t lrtn
);
165 static int execargs_alloc(struct image_params
*imgp
);
166 static int execargs_free(struct image_params
*imgp
);
167 static int exec_check_permissions(struct image_params
*imgp
);
168 static int exec_extract_strings(struct image_params
*imgp
);
169 static int exec_handle_sugid(struct image_params
*imgp
);
170 static int sugid_scripts
= 0;
171 SYSCTL_INT (_kern
, OID_AUTO
, sugid_scripts
, CTLFLAG_RW
, &sugid_scripts
, 0, "");
172 static kern_return_t
create_unix_stack(vm_map_t map
, user_addr_t user_stack
,
173 int customstack
, struct proc
*p
);
174 static int copyoutptr(user_addr_t ua
, user_addr_t ptr
, int ptr_size
);
176 /* XXX forward; should be in headers, but can't be for one reason or another */
177 extern int grade_binary(cpu_type_t exectype
, cpu_subtype_t execsubtype
);
178 extern void vfork_return(thread_t th_act
,
184 extern char classichandler
[32];
185 extern uint32_t classichandler_fsid
;
186 extern long classichandler_fileid
;
192 * Add the requested string to the string space area.
194 * Parameters; struct image_params * image parameter block
195 * user_addr_t string to add to strings area
196 * uio_seg segment where string is located
199 * !0 Failure errno from copyinstr()
202 * (imgp->ip_strendp) updated location of next add, if any
203 * (imgp->ip_strspace) updated byte count of space remaining
206 exec_add_string(struct image_params
*imgp
, user_addr_t str
, /*uio_seg*/int seg
)
212 if (imgp
->ip_strspace
<= 0) {
216 if (IS_UIO_SYS_SPACE(seg
)) {
217 char *kstr
= CAST_DOWN(char *,str
); /* SAFE */
218 error
= copystr(kstr
, imgp
->ip_strendp
, imgp
->ip_strspace
, &len
);
220 error
= copyinstr(str
, imgp
->ip_strendp
, imgp
->ip_strspace
,
223 imgp
->ip_strendp
+= len
;
224 imgp
->ip_strspace
-= len
;
225 } while (error
== ENAMETOOLONG
);
233 * To support new app package launching for Mac OS X, the dyld needs the
234 * first argument to execve() stored on the user stack.
236 * Save the executable path name at the top of the strings area and set
237 * the argument vector pointer to the location following that to indicate
238 * the start of the argument and environment tuples, setting the remaining
239 * string space count to the size of the string area minus the path length
240 * and a reserve for two pointers.
242 * Parameters; struct image_params * image parameter block
243 * char * path used to invoke program
244 * uio_seg segment where path is located
246 * Returns: int 0 Success
247 * !0 Failure: error number
249 * (imgp->ip_strings) saved path
250 * (imgp->ip_strspace) space remaining in ip_strings
251 * (imgp->ip_argv) beginning of argument list
252 * (imgp->ip_strendp) start of remaining copy area
254 * Note: We have to do this before the initial namei() since in the
255 * path contains symbolic links, namei() will overwrite the
256 * original path buffer contents. If the last symbolic link
257 * resolved was a relative pathname, we would lose the original
258 * "path", which could be an absolute pathname. This might be
259 * unacceptable for dyld.
262 exec_save_path(struct image_params
*imgp
, user_addr_t path
, /*uio_seg*/int seg
)
266 char *kpath
= CAST_DOWN(char *,path
); /* SAFE */
268 imgp
->ip_strendp
= imgp
->ip_strings
;
269 imgp
->ip_strspace
= SIZE_IMG_STRSPACE
;
271 len
= MIN(MAXPATHLEN
, imgp
->ip_strspace
);
274 case UIO_USERSPACE32
:
275 case UIO_USERSPACE64
: /* Same for copyin()... */
276 error
= copyinstr(path
, imgp
->ip_strings
, len
, &len
);
279 error
= copystr(kpath
, imgp
->ip_strings
, len
, &len
);
287 imgp
->ip_strendp
+= len
;
288 imgp
->ip_strspace
-= len
;
289 imgp
->ip_argv
= imgp
->ip_strendp
;
300 * Image activator for interpreter scripts. If the image begins with the
301 * characters "#!", then it is an interpreter script. Verify that we are
302 * not already executing in Classic mode, and that the length of the script
303 * line indicating the interpreter is not in excess of the maximum allowed
304 * size. If this is the case, then break out the arguments, if any, which
305 * are separated by white space, and copy them into the argument save area
306 * as if they were provided on the command line before all other arguments.
307 * The line ends when we encounter a comment character ('#') or newline.
309 * Parameters; struct image_params * image parameter block
311 * Returns: -1 not an interpreter (keep looking)
312 * -3 Success: interpreter: relookup
313 * >0 Failure: interpreter: error number
315 * A return value other than -1 indicates subsequent image activators should
316 * not be given the opportunity to attempt to activate the image.
319 exec_shell_imgact(struct image_params
*imgp
)
321 char *vdata
= imgp
->ip_vdata
;
327 * Make sure it's a shell script. If we've already redirected
328 * from an interpreted file once, don't do it again.
330 * Note: We disallow Classic, since the expectation is that we
331 * may run a Classic interpreter, but not an interpret a Classic
332 * image. This is consistent with historical behaviour.
334 if (vdata
[0] != '#' ||
336 (imgp
->ip_flags
& IMGPF_INTERPRET
) != 0) {
341 imgp
->ip_flags
|= IMGPF_INTERPRET
;
343 /* Check to see if SUGID scripts are permitted. If they aren't then
344 * clear the SUGID bits.
345 * imgp->ip_vattr is known to be valid.
347 if (sugid_scripts
== 0) {
348 imgp
->ip_origvattr
->va_mode
&= ~(VSUID
| VSGID
);
351 /* Find the nominal end of the interpreter line */
352 for( ihp
= &vdata
[2]; *ihp
!= '\n' && *ihp
!= '#'; ihp
++) {
353 if (ihp
>= &vdata
[IMG_SHSIZE
])
359 /* Skip over leading spaces - until the interpreter name */
360 while ( ihp
< line_endp
&& ((*ihp
== ' ') || (*ihp
== '\t')))
364 * Find the last non-whitespace character before the end of line or
365 * the beginning of a comment; this is our new end of line.
367 for (;line_endp
> ihp
&& ((*line_endp
== ' ') || (*line_endp
== '\t')); line_endp
--)
371 if (line_endp
== ihp
)
374 /* copy the interpreter name */
375 interp
= imgp
->ip_interp_name
;
376 while ((ihp
< line_endp
) && (*ihp
!= ' ') && (*ihp
!= '\t'))
380 exec_save_path(imgp
, CAST_USER_ADDR_T(imgp
->ip_interp_name
),
384 while (ihp
< line_endp
) {
385 /* Skip leading whitespace before each argument */
386 while ((*ihp
== ' ') || (*ihp
== '\t'))
389 if (ihp
>= line_endp
)
392 /* We have an argument; copy it */
393 while ((ihp
< line_endp
) && (*ihp
!= ' ') && (*ihp
!= '\t')) {
394 *imgp
->ip_strendp
++ = *ihp
++;
397 *imgp
->ip_strendp
++ = 0;
410 * Image activator for fat 1.0 binaries. If the binary is fat, then we
411 * need to select an image from it internally, and make that the image
412 * we are going to attempt to execute. At present, this consists of
413 * reloading the first page for the image with a first page from the
414 * offset location indicated by the fat header.
416 * Important: This image activator is byte order neutral.
418 * Note: If we find an encapsulated binary, we make no assertions
419 * about its validity; instead, we leave that up to a rescan
420 * for an activator to claim it, and, if it is claimed by one,
421 * that activator is responsible for determining validity.
424 exec_fat_imgact(struct image_params
*imgp
)
426 struct proc
*p
= vfs_context_proc(imgp
->ip_vfs_context
);
427 kauth_cred_t cred
= p
->p_ucred
;
428 struct fat_header
*fat_header
= (struct fat_header
*)imgp
->ip_vdata
;
429 struct fat_arch fat_arch
;
433 /* Make sure it's a fat binary */
434 if ((fat_header
->magic
!= FAT_MAGIC
) &&
435 (fat_header
->magic
!= FAT_CIGAM
)) {
440 /* Look up our preferred architecture in the fat file. */
441 lret
= fatfile_getarch_affinity(imgp
->ip_vp
,
442 (vm_offset_t
)fat_header
,
444 (p
->p_flag
& P_AFFINITY
));
445 if (lret
!= LOAD_SUCCESS
) {
446 error
= load_return_to_errno(lret
);
450 /* Read the Mach-O header out of it */
451 error
= vn_rdwr(UIO_READ
, imgp
->ip_vp
, imgp
->ip_vdata
,
452 PAGE_SIZE
, fat_arch
.offset
,
453 UIO_SYSSPACE32
, (IO_UNIT
|IO_NODELOCKED
),
459 /* Did we read a complete header? */
465 /* Success. Indicate we have identified an encapsulated binary */
467 imgp
->ip_arch_offset
= (user_size_t
)fat_arch
.offset
;
468 imgp
->ip_arch_size
= (user_size_t
)fat_arch
.size
;
477 * Image activator for mach-o 1.0 binaries.
479 * Important: This image activator is NOT byte order neutral.
482 exec_mach_imgact(struct image_params
*imgp
)
484 struct mach_header
*mach_header
= (struct mach_header
*)imgp
->ip_vdata
;
485 kauth_cred_t cred
= vfs_context_ucred(imgp
->ip_vfs_context
);
486 struct proc
*p
= vfs_context_proc(imgp
->ip_vfs_context
);
492 struct uthread
*uthread
;
493 vm_map_t old_map
= VM_MAP_NULL
;
495 boolean_t clean_regions
= FALSE
;
496 shared_region_mapping_t initial_region
= NULL
;
498 load_result_t load_result
;
501 * make sure it's a Mach-O 1.0 or Mach-O 2.0 binary; the difference
502 * is a reserved field on the end, so for the most part, we can
503 * treat them as if they were identical.
505 if ((mach_header
->magic
!= MH_MAGIC
) &&
506 (mach_header
->magic
!= MH_MAGIC_64
)) {
511 task
= current_task();
512 thread
= current_thread();
513 uthread
= get_bsdthread_info(thread
);
515 if (uthread
->uu_flag
& UT_VFORK
)
516 vfexec
= 1; /* Mark in exec */
518 if ((mach_header
->cputype
& CPU_ARCH_ABI64
) == CPU_ARCH_ABI64
)
519 imgp
->ip_flags
|= IMGPF_IS_64BIT
;
521 if (!grade_binary(mach_header
->cputype
, mach_header
->cpusubtype
)) {
527 * Copy in arguments/environment from the old process, if the
528 * vector is non-NULL (i.e. exec is not being called from
529 * load_init_program(), as a special case, at system startup).
531 if (imgp
->ip_user_argv
!= 0LL) {
532 error
= exec_extract_strings(imgp
);
538 * Hack for binary compatability; put three NULs on the end of the
539 * string area, and round it up to the next word boundary. This
540 * ensures padding with NULs to the boundary.
542 imgp
->ip_strendp
[0] = 0;
543 imgp
->ip_strendp
[1] = 0;
544 imgp
->ip_strendp
[2] = 0;
545 imgp
->ip_strendp
+= (((imgp
->ip_strendp
- imgp
->ip_strings
) + NBPW
-1) & ~(NBPW
-1));
549 kern_return_t result
;
551 result
= task_create_internal(task
, FALSE
, &new_task
);
552 if (result
!= KERN_SUCCESS
)
553 printf("execve: task_create failed. Code: 0x%x\n", result
);
555 set_bsdtask_info(new_task
, p
);
558 map
= get_task_map(new_task
);
559 result
= thread_create(new_task
, &imgp
->ip_vfork_thread
);
560 if (result
!= KERN_SUCCESS
)
561 printf("execve: thread_create failed. Code: 0x%x\n", result
);
562 /* reset local idea of task, thread, uthread */
564 thread
= imgp
->ip_vfork_thread
;
565 uthread
= get_bsdthread_info(thread
);
571 * We set these flags here; this is OK, since if we fail after
572 * this point, we have already destroyed the parent process anyway.
574 if (imgp
->ip_flags
& IMGPF_IS_64BIT
) {
575 task_set_64bit(task
, TRUE
);
578 task_set_64bit(task
, FALSE
);
579 p
->p_flag
&= ~P_LP64
;
583 * Load the Mach-O file.
585 /* LP64 - remove following "if" statement after osfmk/vm/task_working_set.c */
586 if((imgp
->ip_flags
& IMGPF_IS_64BIT
) == 0)
587 if(imgp
->ip_tws_cache_name
) {
588 tws_handle_startup_file(task
, kauth_cred_getuid(cred
),
589 imgp
->ip_tws_cache_name
, imgp
->ip_vp
, &clean_regions
);
592 vm_get_shared_region(task
, &initial_region
);
596 * NOTE: An error after this point indicates we have potentially
597 * destroyed or overwrote some process state while attempting an
598 * execve() following a vfork(), which is an unrecoverable condition.
602 * We reset the task to 64-bit (or not) here. It may have picked up
603 * a new map, and we need that to reflect its true 64-bit nature.
606 ((imgp
->ip_flags
& IMGPF_IS_64BIT
) == IMGPF_IS_64BIT
));
609 * Actually load the image file we previously decided to load.
611 lret
= load_machfile(imgp
, mach_header
, thread
, map
, clean_regions
, &load_result
);
613 if (lret
!= LOAD_SUCCESS
) {
614 error
= load_return_to_errno(lret
);
618 /* load_machfile() maps the vnode */
619 (void)ubc_map(imgp
->ip_vp
, PROT_EXEC
);
622 * deal with set[ug]id.
624 error
= exec_handle_sugid(imgp
);
626 KNOTE(&p
->p_klist
, NOTE_EXEC
);
628 if (!vfexec
&& (p
->p_flag
& P_TRACED
))
634 vnode_put(imgp
->ip_vp
);
637 if (load_result
.unixproc
&&
638 create_unix_stack(get_task_map(task
),
639 load_result
.user_stack
, load_result
.customstack
, p
)) {
640 error
= load_return_to_errno(LOAD_NOSPACE
);
645 uthread
->uu_ar0
= (void *)get_user_regs(thread
);
646 old_map
= vm_map_switch(get_task_map(task
));
649 if (load_result
.unixproc
) {
653 * Copy the strings area out into the new process address
657 error
= exec_copyout_strings(imgp
, &ap
);
660 vm_map_switch(old_map
);
664 thread_setuserstack(thread
, ap
);
667 if (load_result
.dynlinker
) {
670 /* Adjust the stack */
671 if (imgp
->ip_flags
& IMGPF_IS_64BIT
) {
672 ap
= thread_adjuserstack(thread
, -8);
673 (void)copyoutptr(load_result
.mach_header
, ap
, 8);
675 ap
= thread_adjuserstack(thread
, -4);
676 (void)suword(ap
, load_result
.mach_header
);
681 vm_map_switch(old_map
);
683 /* Set the entry point */
684 thread_setentrypoint(thread
, load_result
.entry_point
);
690 * Reset signal state.
695 * Close file descriptors
696 * which specify close-on-exec.
701 * need to cancel async IO requests that can be cancelled and wait for those
702 * already active. MAY BLOCK!
706 /* FIXME: Till vmspace inherit is fixed: */
707 if (!vfexec
&& p
->vm_shm
)
709 /* Clean up the semaphores */
713 * Remember file name for accounting.
715 p
->p_acflag
&= ~AFORK
;
716 /* If the translated name isn't NULL, then we want to use
717 * that translated name as the name we show as the "real" name.
718 * Otherwise, use the name passed into exec.
720 if (0 != imgp
->ip_p_comm
[0]) {
721 bcopy((caddr_t
)imgp
->ip_p_comm
, (caddr_t
)p
->p_comm
,
724 if (imgp
->ip_ndp
->ni_cnd
.cn_namelen
> MAXCOMLEN
)
725 imgp
->ip_ndp
->ni_cnd
.cn_namelen
= MAXCOMLEN
;
726 bcopy((caddr_t
)imgp
->ip_ndp
->ni_cnd
.cn_nameptr
, (caddr_t
)p
->p_comm
,
727 (unsigned)imgp
->ip_ndp
->ni_cnd
.cn_namelen
);
728 p
->p_comm
[imgp
->ip_ndp
->ni_cnd
.cn_namelen
] = '\0';
732 /* This is for kdebug */
733 long dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
;
735 /* Collect the pathname for tracing */
736 kdbg_trace_string(p
, &dbg_arg1
, &dbg_arg2
, &dbg_arg3
, &dbg_arg4
);
742 KERNEL_DEBUG_CONSTANT1((TRACEDBG_CODE(DBG_TRACE_DATA
, 2)) | DBG_FUNC_NONE
,
743 p
->p_pid
,0,0,0, (unsigned int)thread
);
744 KERNEL_DEBUG_CONSTANT1((TRACEDBG_CODE(DBG_TRACE_STRING
, 2)) | DBG_FUNC_NONE
,
745 dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
, (unsigned int)thread
);
749 KERNEL_DEBUG_CONSTANT((TRACEDBG_CODE(DBG_TRACE_DATA
, 2)) | DBG_FUNC_NONE
,
751 KERNEL_DEBUG_CONSTANT((TRACEDBG_CODE(DBG_TRACE_STRING
, 2)) | DBG_FUNC_NONE
,
752 dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
, 0);
756 p
->p_flag
&= ~P_CLASSIC
;
759 * mark as execed, wakeup the process that vforked (if any) and tell
760 * it that it now has it's own resources back
763 if (p
->p_pptr
&& (p
->p_flag
& P_PPWAIT
)) {
764 p
->p_flag
&= ~P_PPWAIT
;
765 wakeup((caddr_t
)p
->p_pptr
);
768 if (vfexec
&& (p
->p_flag
& P_TRACED
)) {
769 psignal_vfork(p
, new_task
, thread
, SIGTRAP
);
774 task_deallocate(new_task
);
775 thread_deallocate(thread
);
788 * Our image activator table; this is the table of the image types we are
789 * capable of loading. We list them in order of preference to ensure the
790 * fastest image load speed.
792 * XXX hardcoded, for now; should use linker sets
795 int (*ex_imgact
)(struct image_params
*);
798 { exec_mach_imgact
, "Mach-o Binary" },
799 { exec_fat_imgact
, "Fat Binary" },
800 { exec_shell_imgact
, "Interpreter Script" },
806 * TODO: Dynamic linker header address on stack is copied via suword()
810 execve(struct proc
*p
, struct execve_args
*uap
, register_t
*retval
)
812 kauth_cred_t cred
= p
->p_ucred
;
813 struct image_params image_params
, *imgp
;
814 struct vnode_attr va
;
815 struct vnode_attr origva
;
817 struct uthread
*uthread
;
823 int once
= 1; /* save SGUID-ness for interpreted files */
824 char alt_p_comm
[sizeof(p
->p_comm
)] = {0}; /* for Classic */
825 int is_64
= IS_64BIT_PROCESS(p
);
826 int seg
= (is_64
? UIO_USERSPACE64
: UIO_USERSPACE32
);
827 struct vfs_context context
;
830 context
.vc_ucred
= p
->p_ucred
; /* XXX must NOT be kauth_cred_get() */
833 imgp
= &image_params
;
835 /* Initialize the common data in the image_params structure */
836 bzero(imgp
, sizeof(*imgp
));
837 imgp
->ip_user_fname
= uap
->fname
;
838 imgp
->ip_user_argv
= uap
->argp
;
839 imgp
->ip_user_envv
= uap
->envp
;
840 imgp
->ip_vattr
= &va
;
841 imgp
->ip_origvattr
= &origva
;
842 imgp
->ip_vfs_context
= &context
;
843 imgp
->ip_flags
= (is_64
? IMGPF_WAS_64BIT
: IMGPF_NONE
);
844 imgp
->ip_tws_cache_name
= NULL
;
845 imgp
->ip_p_comm
= alt_p_comm
; /* for Classic */
848 * XXXAUDIT: Currently, we only audit the pathname of the binary.
849 * There may also be poor interaction with dyld.
852 task
= current_task();
853 uthread
= get_bsdthread_info(current_thread());
855 if (uthread
->uu_flag
& UT_VFORK
) {
856 vfexec
= 1; /* Mark in exec */
858 if (task
!= kernel_task
) {
859 numthreads
= get_task_numacts(task
);
860 if (numthreads
<= 0 )
862 if (numthreads
> 1) {
868 error
= execargs_alloc(imgp
);
873 * XXXAUDIT: Note: the double copyin introduces an audit
874 * race. To correct this race, we must use a single
875 * copyin(), e.g. by passing a flag to namei to indicate an
876 * external path buffer is being used.
878 error
= exec_save_path(imgp
, uap
->fname
, seg
);
885 * No app profiles under chroot
887 if((p
->p_fd
->fd_rdir
== NULLVP
) && (app_profile
!= 0)) {
889 /* grab the name of the file out of its path */
890 /* we will need this for lookup within the */
892 /* Scan backwards for the first '/' or start of string */
893 imgp
->ip_tws_cache_name
= imgp
->ip_strendp
;
894 while (imgp
->ip_tws_cache_name
[0] != '/') {
895 if(imgp
->ip_tws_cache_name
== imgp
->ip_strings
) {
896 imgp
->ip_tws_cache_name
--;
899 imgp
->ip_tws_cache_name
--;
901 imgp
->ip_tws_cache_name
++;
903 NDINIT(&nd
, LOOKUP
, FOLLOW
| LOCKLEAF
| AUDITVNPATH1
,
904 seg
, uap
->fname
, imgp
->ip_vfs_context
);
910 imgp
->ip_ndp
= &nd
; /* successful namei(); call nameidone() later */
911 imgp
->ip_vp
= nd
.ni_vp
; /* if set, need to vnode_put() at some point */
913 error
= exec_check_permissions(imgp
);
917 /* Copy; avoid invocation of an interpreter overwriting the original */
923 error
= vn_rdwr(UIO_READ
, imgp
->ip_vp
, imgp
->ip_vdata
, PAGE_SIZE
, 0,
924 UIO_SYSSPACE32
, IO_NODELOCKED
, cred
, &resid
, p
);
930 for(i
= 0; error
== -1 && execsw
[i
].ex_imgact
!= NULL
; i
++) {
932 error
= (*execsw
[i
].ex_imgact
)(imgp
);
935 /* case -1: not claimed: continue */
936 case -2: /* Encapsulated binary */
937 goto encapsulated_binary
;
939 case -3: /* Interpreter */
940 vnode_put(imgp
->ip_vp
);
941 imgp
->ip_vp
= NULL
; /* already put */
942 nd
.ni_cnd
.cn_nameiop
= LOOKUP
;
943 nd
.ni_cnd
.cn_flags
= (nd
.ni_cnd
.cn_flags
& HASBUF
) |
947 nd
.ni_segflg
= UIO_SYSSPACE32
;
948 nd
.ni_dirp
= CAST_USER_ADDR_T(imgp
->ip_interp_name
);
956 /* call out to allow 3rd party notification of exec.
957 * Ignore result of kauth_authorize_fileop call.
959 if (error
== 0 && kauth_authorize_fileop_has_listeners()) {
960 kauth_authorize_fileop(vfs_context_ucred(&context
), KAUTH_FILEOP_EXEC
,
961 (uintptr_t)nd
.ni_vp
, 0);
964 /* Image not claimed by any activator? */
970 nameidone(imgp
->ip_ndp
);
972 vnode_put(imgp
->ip_vp
);
973 if (imgp
->ip_strings
)
975 if (!error
&& vfexec
) {
976 vfork_return(current_thread(), p
->p_pptr
, p
, retval
);
977 (void)thread_resume(imgp
->ip_vfork_thread
);
985 copyinptr(user_addr_t froma
, user_addr_t
*toptr
, int ptr_size
)
990 /* 64 bit value containing 32 bit address */
993 error
= copyin(froma
, &i
, 4);
994 *toptr
= CAST_USER_ADDR_T(i
); /* SAFE */
996 error
= copyin(froma
, toptr
, 8);
1003 copyoutptr(user_addr_t ua
, user_addr_t ptr
, int ptr_size
)
1007 if (ptr_size
== 4) {
1008 /* 64 bit value containing 32 bit address */
1009 unsigned int i
= CAST_DOWN(unsigned int,ua
); /* SAFE */
1011 error
= copyout(&i
, ptr
, 4);
1013 error
= copyout(&ua
, ptr
, 8);
1020 * exec_copyout_strings
1022 * Copy out the strings segment to user space. The strings segment is put
1023 * on a preinitialized stack frame.
1025 * Parameters: struct image_params * the image parameter block
1026 * int * a pointer to the stack offset variable
1028 * Returns: 0 Success
1032 * (*stackp) The stack offset, modified
1034 * Note: The strings segment layout is backward, from the beginning
1035 * of the top of the stack to consume the minimal amount of
1036 * space possible; the returned stack pointer points to the
1037 * end of the area consumed (stacks grow upward).
1039 * argc is an int; arg[i] are pointers; env[i] are pointers;
1040 * exec_path is a pointer; the 0's are (void *)NULL's
1042 * The stack frame layout is:
1065 * | exec_path | In MacOS X PR2 Beaker2E the path passed to exec() is
1066 * +-------------+ passed on the stack just after the trailing 0 of the
1067 * | 0 | the envp[] array as a pointer to a string.
1074 * | | <- p->user_stack
1077 * Although technically a part of the STRING AREA, we treat the PATH AREA as
1078 * a separate entity. This allows us to align the beginning of the PATH AREA
1079 * to a pointer boundary so that the exec_path, env[i], and argv[i] pointers
1080 * which preceed it on the stack are properly aligned.
1082 * TODO: argc copied with suword(), which takes a 64 bit address
1085 exec_copyout_strings(struct image_params
*imgp
, user_addr_t
*stackp
)
1087 struct proc
*p
= vfs_context_proc(imgp
->ip_vfs_context
);
1088 int ptr_size
= (imgp
->ip_flags
& IMGPF_IS_64BIT
) ? 8 : 4;
1089 char *argv
= imgp
->ip_argv
; /* modifiable copy of argv */
1090 user_addr_t string_area
; /* *argv[], *env[] */
1091 user_addr_t path_area
; /* package launch path */
1092 user_addr_t ptr_area
; /* argv[], env[], exec_path */
1094 int stringc
= imgp
->ip_argc
+ imgp
->ip_envc
;
1102 * Set up pointers to the beginning of the string area, the beginning
1103 * of the path area, and the beginning of the pointer area (actually,
1104 * the location of argc, an int, which may be smaller than a pointer,
1105 * but we use ptr_size worth of space for it, for alignment).
1107 string_area
= stack
- (((imgp
->ip_strendp
- imgp
->ip_strings
) + ptr_size
-1) & ~(ptr_size
-1)) - ptr_size
;
1108 path_area
= string_area
- (((imgp
->ip_argv
- imgp
->ip_strings
) + ptr_size
-1) & ~(ptr_size
-1));
1109 ptr_area
= path_area
- ((imgp
->ip_argc
+ imgp
->ip_envc
+ 4) * ptr_size
) - ptr_size
/*argc*/;
1111 /* Return the initial stack address: the location of argc */
1115 * Record the size of the arguments area so that sysctl_procargs()
1116 * can return the argument area without having to parse the arguments.
1118 p
->p_argc
= imgp
->ip_argc
;
1119 p
->p_argslen
= (int)(stack
- path_area
);
1123 * Support for new app package launching for Mac OS X allocates
1124 * the "path" at the begining of the imgp->ip_strings buffer.
1125 * copy it just before the string area.
1128 error
= copyoutstr(imgp
->ip_strings
, path_area
,
1129 (unsigned)(imgp
->ip_argv
- imgp
->ip_strings
),
1135 /* Save a NULL pointer below it */
1136 (void)copyoutptr(0LL, path_area
- ptr_size
, ptr_size
);
1138 /* Save the pointer to "path" just below it */
1139 (void)copyoutptr(path_area
, path_area
- 2*ptr_size
, ptr_size
);
1142 * ptr_size for 2 NULL one each ofter arg[argc -1] and env[n]
1144 * skip over saved path, ptr_size for pointer to path,
1145 * and ptr_size for the NULL after pointer to path.
1148 /* argc (int32, stored in a ptr_size area) */
1149 (void)suword(ptr_area
, imgp
->ip_argc
);
1150 ptr_area
+= sizeof(int);
1151 /* pad to ptr_size, if 64 bit image, to ensure user stack alignment */
1152 if (imgp
->ip_flags
& IMGPF_IS_64BIT
) {
1153 (void)suword(ptr_area
, 0); /* int, not long: ignored */
1154 ptr_area
+= sizeof(int);
1159 * We use (string_area - path_area) here rather than the more
1160 * intuitive (imgp->ip_argv - imgp->ip_strings) because we are
1161 * interested in the length of the PATH_AREA in user space,
1162 * rather than the actual length of the execution path, since
1163 * it includes alignment padding of the PATH_AREA + STRING_AREA
1164 * to a ptr_size boundary.
1166 strspace
= SIZE_IMG_STRSPACE
- (string_area
- path_area
);
1168 if (stringc
== imgp
->ip_envc
) {
1169 /* argv[n] = NULL */
1170 (void)copyoutptr(0LL, ptr_area
, ptr_size
);
1171 ptr_area
+= ptr_size
;
1176 /* pointer: argv[n]/env[n] */
1177 (void)copyoutptr(string_area
, ptr_area
, ptr_size
);
1179 /* string : argv[n][]/env[n][] */
1181 if (strspace
<= 0) {
1185 error
= copyoutstr(argv
, string_area
,
1191 } while (error
== ENAMETOOLONG
);
1192 if (error
== EFAULT
|| error
== E2BIG
)
1193 break; /* bad stack - user's problem */
1194 ptr_area
+= ptr_size
;
1197 (void)copyoutptr(0LL, ptr_area
, ptr_size
);
1205 * exec_extract_strings
1207 * Copy arguments and environment from user space into work area; we may
1208 * have already copied some early arguments into the work area, and if
1209 * so, any arguments opied in are appended to those already there.
1211 * Parameters: struct image_params * the image parameter block
1213 * Returns: 0 Success
1217 * (imgp->ip_argc) Count of arguments, updated
1218 * (imgp->ip_envc) Count of environment strings, updated
1221 * Notes: The argument and environment vectors are user space pointers
1222 * to arrays of user space pointers.
1225 exec_extract_strings(struct image_params
*imgp
)
1228 struct proc
*p
= vfs_context_proc(imgp
->ip_vfs_context
);
1229 int seg
= (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
);
1230 int ptr_size
= (imgp
->ip_flags
& IMGPF_WAS_64BIT
) ? 8 : 4;
1231 user_addr_t argv
= imgp
->ip_user_argv
;
1232 user_addr_t envv
= imgp
->ip_user_envv
;
1234 /* Now, get rest of arguments */
1237 * If we are running an interpreter, replace the av[0] that was
1238 * passed to execve() with the fully qualified path name that was
1239 * passed to execve() for interpreters which do not use the PATH
1240 * to locate their script arguments.
1242 if((imgp
->ip_flags
& IMGPF_INTERPRET
) != 0 && argv
!= 0LL) {
1245 error
= copyinptr(argv
, &arg
, ptr_size
);
1248 if (arg
!= 0LL && arg
!= (user_addr_t
)-1) {
1250 error
= exec_add_string(imgp
, imgp
->ip_user_fname
, seg
);
1257 while (argv
!= 0LL) {
1260 error
= copyinptr(argv
, &arg
, ptr_size
);
1267 } else if (arg
== (user_addr_t
)-1) {
1268 /* Um... why would it be -1? */
1275 error
= exec_add_string(imgp
, arg
, seg
);
1281 /* Now, get the environment */
1282 while (envv
!= 0LL) {
1285 error
= copyinptr(envv
, &env
, ptr_size
);
1292 } else if (env
== (user_addr_t
)-1) {
1299 error
= exec_add_string(imgp
, env
, seg
);
1309 #define unix_stack_size(p) (p->p_rlimit[RLIMIT_STACK].rlim_cur)
1312 exec_check_permissions(struct image_params
*imgp
)
1314 struct vnode
*vp
= imgp
->ip_vp
;
1315 struct vnode_attr
*vap
= imgp
->ip_vattr
;
1316 struct proc
*p
= vfs_context_proc(imgp
->ip_vfs_context
);
1318 kauth_action_t action
;
1320 /* Only allow execution of regular files */
1321 if (!vnode_isreg(vp
))
1324 /* Get the file attributes that we will be using here and elsewhere */
1326 VATTR_WANTED(vap
, va_uid
);
1327 VATTR_WANTED(vap
, va_gid
);
1328 VATTR_WANTED(vap
, va_mode
);
1329 VATTR_WANTED(vap
, va_fsid
);
1330 VATTR_WANTED(vap
, va_fileid
);
1331 VATTR_WANTED(vap
, va_data_size
);
1332 if ((error
= vnode_getattr(vp
, vap
, imgp
->ip_vfs_context
)) != 0)
1336 * Ensure that at least one execute bit is on - otherwise root
1337 * will always succeed, and we don't want to happen unless the
1338 * file really is executable.
1340 if ((vap
->va_mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
)) == 0)
1343 /* Disallow zero length files */
1344 if (vap
->va_data_size
== 0)
1347 imgp
->ip_arch_offset
= (user_size_t
)0;
1348 imgp
->ip_arch_size
= vap
->va_data_size
;
1350 /* Disable setuid-ness for traced programs or if MNT_NOSUID */
1351 if ((vp
->v_mount
->mnt_flag
& MNT_NOSUID
) || (p
->p_flag
& P_TRACED
))
1352 vap
->va_mode
&= ~(VSUID
| VSGID
);
1354 /* Check for execute permission */
1355 action
= KAUTH_VNODE_EXECUTE
;
1356 /* Traced images must also be readable */
1357 if (p
->p_flag
& P_TRACED
)
1358 action
|= KAUTH_VNODE_READ_DATA
;
1359 if ((error
= vnode_authorize(vp
, NULL
, action
, imgp
->ip_vfs_context
)) != 0)
1362 /* Don't let it run if anyone had it open for writing */
1363 if (vp
->v_writecount
)
1367 /* XXX May want to indicate to underlying FS that vnode is open */
1375 * Initially clear the P_SUGID in the process flags; if an SUGID process is
1376 * exec'ing a non-SUGID image, then this is the point of no return.
1378 * If the image being activated is SUGI, then replace the credential with a
1379 * copy, disable tracing (unless the tracing process is root), reset the
1380 * mach task port to revoke it, set the P_SUGID bit,
1382 * If the saved user and group ID will be changing, then make sure it happens
1383 * to a new credential, rather than a shared one.
1385 * Set the security token (this is probably obsolete, given that the token
1386 * should not technically be separate from the credential itself).
1388 * Parameters: struct image_params * the image parameter block
1390 * Returns: void No failure indication
1393 * <process credential> Potentially modified/replaced
1394 * <task port> Potentially revoked
1395 * <process flags> P_SUGID bit potentially modified
1396 * <security token> Potentially modified
1399 exec_handle_sugid(struct image_params
*imgp
)
1401 kauth_cred_t cred
= vfs_context_ucred(imgp
->ip_vfs_context
);
1402 struct proc
*p
= vfs_context_proc(imgp
->ip_vfs_context
);
1405 static struct vnode
*dev_null
= NULLVP
;
1407 p
->p_flag
&= ~P_SUGID
;
1409 if (((imgp
->ip_origvattr
->va_mode
& VSUID
) != 0 &&
1410 kauth_cred_getuid(cred
) != imgp
->ip_origvattr
->va_uid
) ||
1411 ((imgp
->ip_origvattr
->va_mode
& VSGID
) != 0 &&
1412 cred
->cr_gid
!= imgp
->ip_origvattr
->va_gid
)) {
1415 * If process is being ktraced, turn off - unless
1418 if (p
->p_tracep
&& !(p
->p_traceflag
& KTRFAC_ROOT
)) {
1419 struct vnode
*tvp
= p
->p_tracep
;
1426 * Replace the credential with a copy of itself if euid or egid change.
1428 if (imgp
->ip_origvattr
->va_mode
& VSUID
) {
1429 p
->p_ucred
= kauth_cred_seteuid(p
->p_ucred
, imgp
->ip_origvattr
->va_uid
);
1431 if (imgp
->ip_origvattr
->va_mode
& VSGID
) {
1432 p
->p_ucred
= kauth_cred_setegid(p
->p_ucred
, imgp
->ip_origvattr
->va_gid
);
1436 * Have mach reset the task port. We don't want
1437 * anyone who had the task port before a setuid
1438 * exec to be able to access/control the task
1441 if (current_task() == p
->task
)
1442 ipc_task_reset(p
->task
);
1444 p
->p_flag
|= P_SUGID
;
1446 /* Cache the vnode for /dev/null the first time around */
1447 if (dev_null
== NULLVP
) {
1448 struct nameidata nd1
;
1450 NDINIT(&nd1
, LOOKUP
, FOLLOW
, UIO_SYSSPACE32
,
1451 CAST_USER_ADDR_T("/dev/null"),
1452 imgp
->ip_vfs_context
);
1454 if ((error
= vn_open(&nd1
, FREAD
, 0)) == 0) {
1455 dev_null
= nd1
.ni_vp
;
1457 * vn_open returns with both a use_count
1458 * and an io_count on the found vnode
1459 * drop the io_count, but keep the use_count
1461 vnode_put(nd1
.ni_vp
);
1465 /* Radar 2261856; setuid security hole fix */
1466 /* Patch from OpenBSD: A. Ramesh */
1468 * XXX For setuid processes, attempt to ensure that
1469 * stdin, stdout, and stderr are already allocated.
1470 * We do not want userland to accidentally allocate
1471 * descriptors in this range which has implied meaning
1474 if (dev_null
!= NULLVP
) {
1475 for (i
= 0; i
< 3; i
++) {
1476 struct fileproc
*fp
;
1479 if (p
->p_fd
->fd_ofiles
[i
] != NULL
)
1482 if ((error
= falloc(p
, &fp
, &indx
)) != 0)
1485 if ((error
= vnode_ref_ext(dev_null
, FREAD
)) != 0) {
1486 fp_free(p
, indx
, fp
);
1490 fp
->f_fglob
->fg_flag
= FREAD
;
1491 fp
->f_fglob
->fg_type
= DTYPE_VNODE
;
1492 fp
->f_fglob
->fg_ops
= &vnops
;
1493 fp
->f_fglob
->fg_data
= (caddr_t
)dev_null
;
1496 *fdflags(p
, indx
) &= ~UF_RESERVED
;
1497 fp_drop(p
, indx
, fp
, 1);
1501 * for now we need to drop the reference immediately
1502 * since we don't have any mechanism in place to
1503 * release it before starting to unmount "/dev"
1504 * during a reboot/shutdown
1506 vnode_rele(dev_null
);
1512 * Implement the semantic where the effective user and group become
1513 * the saved user and group in exec'ed programs.
1515 p
->p_ucred
= kauth_cred_setsvuidgid(p
->p_ucred
, kauth_cred_getuid(p
->p_ucred
), p
->p_ucred
->cr_gid
);
1517 /* XXX Obsolete; security token should not be separate from cred */
1518 set_security_token(p
);
1523 static kern_return_t
1524 create_unix_stack(vm_map_t map
, user_addr_t user_stack
, int customstack
,
1527 mach_vm_size_t size
;
1528 mach_vm_offset_t addr
;
1530 p
->user_stack
= user_stack
;
1532 size
= mach_vm_round_page(unix_stack_size(p
));
1533 addr
= mach_vm_trunc_page(user_stack
- size
);
1534 return (mach_vm_allocate(map
, &addr
, size
,
1535 VM_MAKE_TAG(VM_MEMORY_STACK
) |
1538 return(KERN_SUCCESS
);
1541 #include <sys/reboot.h>
1543 static char init_program_name
[128] = "/sbin/launchd";
1544 static const char * other_init
= "/sbin/mach_init";
1546 char init_args
[128] = "";
1548 struct execve_args init_exec_args
;
1549 int init_attempts
= 0;
1553 load_init_program(struct proc
*p
)
1555 vm_offset_t init_addr
;
1558 register_t retval
[2];
1562 /* init_args are copied in string form directly from bootstrap */
1565 if (boothowto
& RB_INITNAME
) {
1566 printf("init program? ");
1568 gets(init_program_name
, init_program_name
);
1569 #endif /* FIXME ] */
1572 if (error
&& ((boothowto
& RB_INITNAME
) == 0) &&
1573 (init_attempts
== 1)) {
1574 printf("Load of %s, errno %d, trying %s\n",
1575 init_program_name
, error
, other_init
);
1577 bcopy(other_init
, init_program_name
,
1578 sizeof(other_init
));
1584 printf("Load of %s failed, errno %d\n",
1585 init_program_name
, error
);
1587 boothowto
|= RB_INITNAME
;
1592 * Copy out program name.
1595 init_addr
= VM_MIN_ADDRESS
;
1596 (void) vm_allocate(current_map(), &init_addr
,
1597 PAGE_SIZE
, VM_FLAGS_ANYWHERE
);
1601 (void) copyout((caddr_t
) init_program_name
,
1602 CAST_USER_ADDR_T(init_addr
),
1603 (unsigned) sizeof(init_program_name
)+1);
1605 argv
[0] = (char *) init_addr
;
1606 init_addr
+= sizeof(init_program_name
);
1607 init_addr
= (vm_offset_t
)ROUND_PTR(char, init_addr
);
1610 * Put out first (and only) argument, similarly.
1611 * Assumes everything fits in a page as allocated
1615 (void) copyout((caddr_t
) init_args
,
1616 CAST_USER_ADDR_T(init_addr
),
1617 (unsigned) sizeof(init_args
));
1619 argv
[1] = (char *) init_addr
;
1620 init_addr
+= sizeof(init_args
);
1621 init_addr
= (vm_offset_t
)ROUND_PTR(char, init_addr
);
1624 * Null-end the argument list
1627 argv
[2] = (char *) 0;
1630 * Copy out the argument list.
1633 (void) copyout((caddr_t
) argv
,
1634 CAST_USER_ADDR_T(init_addr
),
1635 (unsigned) sizeof(argv
));
1638 * Set up argument block for fake call to execve.
1641 init_exec_args
.fname
= CAST_USER_ADDR_T(argv
[0]);
1642 init_exec_args
.argp
= CAST_USER_ADDR_T((char **)init_addr
);
1643 init_exec_args
.envp
= CAST_USER_ADDR_T(0);
1645 /* So that mach_init task
1646 * is set with uid,gid 0 token
1648 set_security_token(p
);
1650 error
= execve(p
,&init_exec_args
,retval
);
1655 * Convert a load_return_t to an errno.
1658 load_return_to_errno(load_return_t lrtn
)
1684 #include <mach/mach_types.h>
1685 #include <mach/vm_prot.h>
1686 #include <mach/semaphore.h>
1687 #include <mach/sync_policy.h>
1688 #include <kern/clock.h>
1689 #include <mach/kern_return.h>
1691 extern semaphore_t execve_semaphore
;
1694 * The block of memory used by the execve arguments. At the same time,
1695 * we allocate a page so that we can read in the first page of the image.
1698 execargs_alloc(struct image_params
*imgp
)
1702 kret
= semaphore_wait(execve_semaphore
);
1703 if (kret
!= KERN_SUCCESS
)
1707 case KERN_INVALID_ADDRESS
:
1708 case KERN_PROTECTION_FAILURE
:
1711 case KERN_OPERATION_TIMED_OUT
:
1715 kret
= kmem_alloc_pageable(bsd_pageable_map
, (vm_offset_t
*)&imgp
->ip_strings
, NCARGS
+ PAGE_SIZE
);
1716 imgp
->ip_vdata
= imgp
->ip_strings
+ NCARGS
;
1717 if (kret
!= KERN_SUCCESS
) {
1718 semaphore_signal(execve_semaphore
);
1725 execargs_free(struct image_params
*imgp
)
1729 kmem_free(bsd_pageable_map
, (vm_offset_t
)imgp
->ip_strings
, NCARGS
+ PAGE_SIZE
);
1730 imgp
->ip_strings
= NULL
;
1732 kret
= semaphore_signal(execve_semaphore
);
1734 case KERN_INVALID_ADDRESS
:
1735 case KERN_PROTECTION_FAILURE
:
1738 case KERN_OPERATION_TIMED_OUT
: