2 * Copyright (c) 2000-2013 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. 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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30 * Mach Operating System
31 * Copyright (c) 1987 Carnegie-Mellon University
32 * All rights reserved. The CMU software License Agreement specifies
33 * the terms and conditions for use and redistribution.
37 * Copyright (c) 1982, 1986, 1991, 1993
38 * The Regents of the University of California. All rights reserved.
39 * (c) UNIX System Laboratories, Inc.
40 * All or some portions of this file are derived from material licensed
41 * to the University of California by American Telephone and Telegraph
42 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
43 * the permission of UNIX System Laboratories, Inc.
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
48 * 1. Redistributions of source code must retain the above copyright
49 * notice, this list of conditions and the following disclaimer.
50 * 2. Redistributions in binary form must reproduce the above copyright
51 * notice, this list of conditions and the following disclaimer in the
52 * documentation and/or other materials provided with the distribution.
53 * 3. All advertising materials mentioning features or use of this software
54 * must display the following acknowledgement:
55 * This product includes software developed by the University of
56 * California, Berkeley and its contributors.
57 * 4. Neither the name of the University nor the names of its contributors
58 * may be used to endorse or promote products derived from this software
59 * without specific prior written permission.
61 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
62 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
65 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
67 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
69 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
70 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73 * from: @(#)kern_exec.c 8.1 (Berkeley) 6/10/93
76 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
77 * support for mandatory and extensible security protections. This notice
78 * is included in support of clause 2.2 (b) of the Apple Public License,
81 #include <machine/reg.h>
82 #include <machine/cpu_capabilities.h>
84 #include <sys/param.h>
85 #include <sys/systm.h>
86 #include <sys/filedesc.h>
87 #include <sys/kernel.h>
88 #include <sys/proc_internal.h>
89 #include <sys/kauth.h>
91 #include <sys/socketvar.h>
92 #include <sys/malloc.h>
93 #include <sys/namei.h>
94 #include <sys/mount_internal.h>
95 #include <sys/vnode_internal.h>
96 #include <sys/file_internal.h>
98 #include <sys/uio_internal.h>
100 #include <sys/exec.h>
101 #include <sys/kdebug.h>
102 #include <sys/signal.h>
103 #include <sys/aio_kern.h>
104 #include <sys/sysproto.h>
105 #include <sys/persona.h>
106 #include <sys/reason.h>
108 #include <sys/shm_internal.h> /* shmexec() */
110 #include <sys/ubc_internal.h> /* ubc_map() */
111 #include <sys/spawn.h>
112 #include <sys/spawn_internal.h>
113 #include <sys/process_policy.h>
114 #include <sys/codesign.h>
115 #include <sys/random.h>
116 #include <crypto/sha1.h>
118 #include <libkern/libkern.h>
120 #include <security/audit/audit.h>
122 #include <ipc/ipc_types.h>
124 #include <mach/mach_types.h>
125 #include <mach/port.h>
126 #include <mach/task.h>
127 #include <mach/task_access.h>
128 #include <mach/thread_act.h>
129 #include <mach/vm_map.h>
130 #include <mach/mach_vm.h>
131 #include <mach/vm_param.h>
133 #include <kern/sched_prim.h> /* thread_wakeup() */
134 #include <kern/affinity.h>
135 #include <kern/assert.h>
136 #include <kern/task.h>
137 #include <kern/coalition.h>
138 #include <kern/policy_internal.h>
139 #include <kern/kalloc.h>
142 #include <security/mac.h>
143 #include <security/mac_mach_internal.h>
146 #include <vm/vm_map.h>
147 #include <vm/vm_kern.h>
148 #include <vm/vm_protos.h>
149 #include <vm/vm_kern.h>
150 #include <vm/vm_fault.h>
151 #include <vm/vm_pageout.h>
153 #include <kdp/kdp_dyld.h>
155 #include <machine/pal_routines.h>
157 #include <pexpert/pexpert.h>
159 #if CONFIG_MEMORYSTATUS
160 #include <sys/kern_memorystatus.h>
164 /* Do not include dtrace.h, it redefines kmem_[alloc/free] */
165 extern void dtrace_proc_exec(proc_t
);
166 extern void (*dtrace_proc_waitfor_exec_ptr
)(proc_t
);
169 * Since dtrace_proc_waitfor_exec_ptr can be added/removed in dtrace_subr.c,
170 * we will store its value before actually calling it.
172 static void (*dtrace_proc_waitfor_hook
)(proc_t
) = NULL
;
174 #include <sys/dtrace_ptss.h>
177 /* support for child creation in exec after vfork */
178 thread_t
fork_create_child(task_t parent_task
, coalition_t
*parent_coalition
, proc_t child_proc
, int inherit_memory
, int is64bit
);
179 void vfork_exit(proc_t p
, int rv
);
180 extern void proc_apply_task_networkbg_internal(proc_t
, thread_t
);
183 * Mach things for which prototypes are unavailable from Mach headers
187 void ipc_thread_reset(
189 kern_return_t
ipc_object_copyin(
191 mach_port_name_t name
,
192 mach_msg_type_name_t msgt_name
,
193 ipc_object_t
*objectp
);
194 void ipc_port_release_send(ipc_port_t
);
196 #if DEVELOPMENT || DEBUG
197 void task_importance_update_owner_info(task_t
);
200 extern struct savearea
*get_user_regs(thread_t
);
202 __attribute__((noinline
)) int __EXEC_WAITING_ON_TASKGATED_CODE_SIGNATURE_UPCALL__(mach_port_t task_access_port
, int32_t new_pid
);
204 #include <kern/thread.h>
205 #include <kern/task.h>
206 #include <kern/ast.h>
207 #include <kern/mach_loader.h>
208 #include <kern/mach_fat.h>
209 #include <mach-o/fat.h>
210 #include <mach-o/loader.h>
211 #include <machine/vmparam.h>
212 #include <sys/imgact.h>
218 * EAI_ITERLIMIT The maximum number of times to iterate an image
219 * activator in exec_activate_image() before treating
220 * it as malformed/corrupt.
222 #define EAI_ITERLIMIT 3
225 * For #! interpreter parsing
227 #define IS_WHITESPACE(ch) ((ch == ' ') || (ch == '\t'))
228 #define IS_EOL(ch) ((ch == '#') || (ch == '\n'))
230 extern vm_map_t bsd_pageable_map
;
231 extern const struct fileops vnops
;
233 #define USER_ADDR_ALIGN(addr, val) \
234 ( ( (user_addr_t)(addr) + (val) - 1) \
237 struct image_params
; /* Forward */
238 static int exec_activate_image(struct image_params
*imgp
);
239 static int exec_copyout_strings(struct image_params
*imgp
, user_addr_t
*stackp
);
240 static int load_return_to_errno(load_return_t lrtn
);
241 static int execargs_alloc(struct image_params
*imgp
);
242 static int execargs_free(struct image_params
*imgp
);
243 static int exec_check_permissions(struct image_params
*imgp
);
244 static int exec_extract_strings(struct image_params
*imgp
);
245 static int exec_add_apple_strings(struct image_params
*imgp
, const load_result_t
*load_result
);
246 static int exec_handle_sugid(struct image_params
*imgp
);
247 static int sugid_scripts
= 0;
248 SYSCTL_INT (_kern
, OID_AUTO
, sugid_scripts
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &sugid_scripts
, 0, "");
249 static kern_return_t
create_unix_stack(vm_map_t map
, load_result_t
* load_result
, proc_t p
);
250 static int copyoutptr(user_addr_t ua
, user_addr_t ptr
, int ptr_size
);
251 static void exec_resettextvp(proc_t
, struct image_params
*);
252 static int check_for_signature(proc_t
, struct image_params
*);
253 static void exec_prefault_data(proc_t
, struct image_params
*, load_result_t
*);
254 static errno_t
exec_handle_port_actions(struct image_params
*imgp
, boolean_t
* portwatch_present
, ipc_port_t
* portwatch_ports
);
255 static errno_t
exec_handle_spawnattr_policy(proc_t p
, int psa_apptype
, uint64_t psa_qos_clamp
, uint64_t psa_darwin_role
,
256 ipc_port_t
* portwatch_ports
, int portwatch_count
);
259 * exec_add_user_string
261 * Add the requested string to the string space area.
263 * Parameters; struct image_params * image parameter block
264 * user_addr_t string to add to strings area
265 * int segment from which string comes
266 * boolean_t TRUE if string contributes to NCARGS
269 * !0 Failure errno from copyinstr()
272 * (imgp->ip_strendp) updated location of next add, if any
273 * (imgp->ip_strspace) updated byte count of space remaining
274 * (imgp->ip_argspace) updated byte count of space in NCARGS
277 exec_add_user_string(struct image_params
*imgp
, user_addr_t str
, int seg
, boolean_t is_ncargs
)
286 space
= imgp
->ip_argspace
; /* by definition smaller than ip_strspace */
288 space
= imgp
->ip_strspace
;
295 if (!UIO_SEG_IS_USER_SPACE(seg
)) {
296 char *kstr
= CAST_DOWN(char *,str
); /* SAFE */
297 error
= copystr(kstr
, imgp
->ip_strendp
, space
, &len
);
299 error
= copyinstr(str
, imgp
->ip_strendp
, space
, &len
);
302 imgp
->ip_strendp
+= len
;
303 imgp
->ip_strspace
-= len
;
305 imgp
->ip_argspace
-= len
;
307 } while (error
== ENAMETOOLONG
);
313 * dyld is now passed the executable path as a getenv-like variable
314 * in the same fashion as the stack_guard and malloc_entropy keys.
316 #define EXECUTABLE_KEY "executable_path="
321 * To support new app package launching for Mac OS X, the dyld needs the
322 * first argument to execve() stored on the user stack.
324 * Save the executable path name at the bottom of the strings area and set
325 * the argument vector pointer to the location following that to indicate
326 * the start of the argument and environment tuples, setting the remaining
327 * string space count to the size of the string area minus the path length.
329 * Parameters; struct image_params * image parameter block
330 * char * path used to invoke program
331 * int segment from which path comes
333 * Returns: int 0 Success
335 * copy[in]str:EFAULT Bad address
336 * copy[in]str:ENAMETOOLONG Filename too long
339 * (imgp->ip_strings) saved path
340 * (imgp->ip_strspace) space remaining in ip_strings
341 * (imgp->ip_strendp) start of remaining copy area
342 * (imgp->ip_argspace) space remaining of NCARGS
343 * (imgp->ip_applec) Initial applev[0]
345 * Note: We have to do this before the initial namei() since in the
346 * path contains symbolic links, namei() will overwrite the
347 * original path buffer contents. If the last symbolic link
348 * resolved was a relative pathname, we would lose the original
349 * "path", which could be an absolute pathname. This might be
350 * unacceptable for dyld.
353 exec_save_path(struct image_params
*imgp
, user_addr_t path
, int seg
, const char **excpath
)
359 // imgp->ip_strings can come out of a cache, so we need to obliterate the
361 memset(imgp
->ip_strings
, '\0', strlen(EXECUTABLE_KEY
) + MAXPATHLEN
);
363 len
= MIN(MAXPATHLEN
, imgp
->ip_strspace
);
366 case UIO_USERSPACE32
:
367 case UIO_USERSPACE64
: /* Same for copyin()... */
368 error
= copyinstr(path
, imgp
->ip_strings
+ strlen(EXECUTABLE_KEY
), len
, &len
);
371 kpath
= CAST_DOWN(char *,path
); /* SAFE */
372 error
= copystr(kpath
, imgp
->ip_strings
+ strlen(EXECUTABLE_KEY
), len
, &len
);
380 bcopy(EXECUTABLE_KEY
, imgp
->ip_strings
, strlen(EXECUTABLE_KEY
));
381 len
+= strlen(EXECUTABLE_KEY
);
383 imgp
->ip_strendp
+= len
;
384 imgp
->ip_strspace
-= len
;
387 *excpath
= imgp
->ip_strings
+ strlen(EXECUTABLE_KEY
);
395 * exec_reset_save_path
397 * If we detect a shell script, we need to reset the string area
398 * state so that the interpreter can be saved onto the stack.
400 * Parameters; struct image_params * image parameter block
402 * Returns: int 0 Success
405 * (imgp->ip_strings) saved path
406 * (imgp->ip_strspace) space remaining in ip_strings
407 * (imgp->ip_strendp) start of remaining copy area
408 * (imgp->ip_argspace) space remaining of NCARGS
412 exec_reset_save_path(struct image_params
*imgp
)
414 imgp
->ip_strendp
= imgp
->ip_strings
;
415 imgp
->ip_argspace
= NCARGS
;
416 imgp
->ip_strspace
= ( NCARGS
+ PAGE_SIZE
);
424 * Image activator for interpreter scripts. If the image begins with
425 * the characters "#!", then it is an interpreter script. Verify the
426 * length of the script line indicating the interpreter is not in
427 * excess of the maximum allowed size. If this is the case, then
428 * break out the arguments, if any, which are separated by white
429 * space, and copy them into the argument save area as if they were
430 * provided on the command line before all other arguments. The line
431 * ends when we encounter a comment character ('#') or newline.
433 * Parameters; struct image_params * image parameter block
435 * Returns: -1 not an interpreter (keep looking)
436 * -3 Success: interpreter: relookup
437 * >0 Failure: interpreter: error number
439 * A return value other than -1 indicates subsequent image activators should
440 * not be given the opportunity to attempt to activate the image.
443 exec_shell_imgact(struct image_params
*imgp
)
445 char *vdata
= imgp
->ip_vdata
;
447 char *line_startp
, *line_endp
;
455 * Make sure it's a shell script. If we've already redirected
456 * from an interpreted file once, don't do it again.
458 if (vdata
[0] != '#' ||
460 (imgp
->ip_flags
& IMGPF_INTERPRET
) != 0) {
464 if (imgp
->ip_origcputype
!= 0) {
465 /* Fat header previously matched, don't allow shell script inside */
469 imgp
->ip_flags
|= IMGPF_INTERPRET
;
470 imgp
->ip_interp_sugid_fd
= -1;
471 imgp
->ip_interp_buffer
[0] = '\0';
473 /* Check to see if SUGID scripts are permitted. If they aren't then
474 * clear the SUGID bits.
475 * imgp->ip_vattr is known to be valid.
477 if (sugid_scripts
== 0) {
478 imgp
->ip_origvattr
->va_mode
&= ~(VSUID
| VSGID
);
481 /* Try to find the first non-whitespace character */
482 for( ihp
= &vdata
[2]; ihp
< &vdata
[IMG_SHSIZE
]; ihp
++ ) {
484 /* Did not find interpreter, "#!\n" */
486 } else if (IS_WHITESPACE(*ihp
)) {
487 /* Whitespace, like "#! /bin/sh\n", keep going. */
489 /* Found start of interpreter */
494 if (ihp
== &vdata
[IMG_SHSIZE
]) {
495 /* All whitespace, like "#! " */
501 /* Try to find the end of the interpreter+args string */
502 for ( ; ihp
< &vdata
[IMG_SHSIZE
]; ihp
++ ) {
507 /* Still part of interpreter or args */
511 if (ihp
== &vdata
[IMG_SHSIZE
]) {
512 /* A long line, like "#! blah blah blah" without end */
516 /* Backtrack until we find the last non-whitespace */
517 while (IS_EOL(*ihp
) || IS_WHITESPACE(*ihp
)) {
521 /* The character after the last non-whitespace is our logical end of line */
525 * Now we have pointers to the usable part of:
527 * "#! /usr/bin/int first second third \n"
528 * ^ line_startp ^ line_endp
531 /* copy the interpreter name */
532 interp
= imgp
->ip_interp_buffer
;
533 for ( ihp
= line_startp
; (ihp
< line_endp
) && !IS_WHITESPACE(*ihp
); ihp
++)
537 exec_reset_save_path(imgp
);
538 exec_save_path(imgp
, CAST_USER_ADDR_T(imgp
->ip_interp_buffer
),
541 /* Copy the entire interpreter + args for later processing into argv[] */
542 interp
= imgp
->ip_interp_buffer
;
543 for ( ihp
= line_startp
; (ihp
< line_endp
); ihp
++)
548 * If we have a SUID oder SGID script, create a file descriptor
549 * from the vnode and pass /dev/fd/%d instead of the actual
550 * path name so that the script does not get opened twice
552 if (imgp
->ip_origvattr
->va_mode
& (VSUID
| VSGID
)) {
553 p
= vfs_context_proc(imgp
->ip_vfs_context
);
554 error
= falloc(p
, &fp
, &fd
, imgp
->ip_vfs_context
);
558 fp
->f_fglob
->fg_flag
= FREAD
;
559 fp
->f_fglob
->fg_ops
= &vnops
;
560 fp
->f_fglob
->fg_data
= (caddr_t
)imgp
->ip_vp
;
563 procfdtbl_releasefd(p
, fd
, NULL
);
564 fp_drop(p
, fd
, fp
, 1);
566 vnode_ref(imgp
->ip_vp
);
568 imgp
->ip_interp_sugid_fd
= fd
;
579 * Image activator for fat 1.0 binaries. If the binary is fat, then we
580 * need to select an image from it internally, and make that the image
581 * we are going to attempt to execute. At present, this consists of
582 * reloading the first page for the image with a first page from the
583 * offset location indicated by the fat header.
585 * Parameters; struct image_params * image parameter block
587 * Returns: -1 not a fat binary (keep looking)
588 * -2 Success: encapsulated binary: reread
589 * >0 Failure: error number
591 * Important: This image activator is byte order neutral.
593 * Note: A return value other than -1 indicates subsequent image
594 * activators should not be given the opportunity to attempt
595 * to activate the image.
597 * If we find an encapsulated binary, we make no assertions
598 * about its validity; instead, we leave that up to a rescan
599 * for an activator to claim it, and, if it is claimed by one,
600 * that activator is responsible for determining validity.
603 exec_fat_imgact(struct image_params
*imgp
)
605 proc_t p
= vfs_context_proc(imgp
->ip_vfs_context
);
606 kauth_cred_t cred
= kauth_cred_proc_ref(p
);
607 struct fat_header
*fat_header
= (struct fat_header
*)imgp
->ip_vdata
;
608 struct _posix_spawnattr
*psa
= NULL
;
609 struct fat_arch fat_arch
;
613 if (imgp
->ip_origcputype
!= 0) {
614 /* Fat header previously matched, don't allow another fat file inside */
618 /* Make sure it's a fat binary */
619 if (OSSwapBigToHostInt32(fat_header
->magic
) != FAT_MAGIC
) {
620 error
= -1; /* not claimed */
624 /* imgp->ip_vdata has PAGE_SIZE, zerofilled if the file is smaller */
625 lret
= fatfile_validate_fatarches((vm_offset_t
)fat_header
, PAGE_SIZE
);
626 if (lret
!= LOAD_SUCCESS
) {
627 error
= load_return_to_errno(lret
);
631 /* If posix_spawn binprefs exist, respect those prefs. */
632 psa
= (struct _posix_spawnattr
*) imgp
->ip_px_sa
;
633 if (psa
!= NULL
&& psa
->psa_binprefs
[0] != 0) {
636 /* Check each preference listed against all arches in header */
637 for (pr
= 0; pr
< NBINPREFS
; pr
++) {
638 cpu_type_t pref
= psa
->psa_binprefs
[pr
];
640 /* No suitable arch in the pref list */
645 if (pref
== CPU_TYPE_ANY
) {
646 /* Fall through to regular grading */
647 goto regular_grading
;
650 lret
= fatfile_getbestarch_for_cputype(pref
,
651 (vm_offset_t
)fat_header
,
654 if (lret
== LOAD_SUCCESS
) {
659 /* Requested binary preference was not honored */
665 /* Look up our preferred architecture in the fat file. */
666 lret
= fatfile_getbestarch((vm_offset_t
)fat_header
,
669 if (lret
!= LOAD_SUCCESS
) {
670 error
= load_return_to_errno(lret
);
675 /* Read the Mach-O header out of fat_arch */
676 error
= vn_rdwr(UIO_READ
, imgp
->ip_vp
, imgp
->ip_vdata
,
677 PAGE_SIZE
, fat_arch
.offset
,
678 UIO_SYSSPACE
, (IO_UNIT
|IO_NODELOCKED
),
685 memset(imgp
->ip_vdata
+ (PAGE_SIZE
- resid
), 0x0, resid
);
688 /* Success. Indicate we have identified an encapsulated binary */
690 imgp
->ip_arch_offset
= (user_size_t
)fat_arch
.offset
;
691 imgp
->ip_arch_size
= (user_size_t
)fat_arch
.size
;
692 imgp
->ip_origcputype
= fat_arch
.cputype
;
693 imgp
->ip_origcpusubtype
= fat_arch
.cpusubtype
;
696 kauth_cred_unref(&cred
);
701 activate_exec_state(task_t task
, proc_t p
, thread_t thread
, load_result_t
*result
)
705 task_set_dyld_info(task
, MACH_VM_MIN_ADDRESS
, 0);
706 if (result
->is64bit
) {
707 task_set_64bit(task
, TRUE
);
708 OSBitOrAtomic(P_LP64
, &p
->p_flag
);
710 task_set_64bit(task
, FALSE
);
711 OSBitAndAtomic(~((uint32_t)P_LP64
), &p
->p_flag
);
714 ret
= thread_state_initialize(thread
);
715 if (ret
!= KERN_SUCCESS
) {
719 if (result
->threadstate
) {
720 uint32_t *ts
= result
->threadstate
;
721 uint32_t total_size
= result
->threadstate_sz
;
723 while (total_size
> 0) {
724 uint32_t flavor
= *ts
++;
725 uint32_t size
= *ts
++;
727 ret
= thread_setstatus(thread
, flavor
, (thread_state_t
)ts
, size
);
732 total_size
-= (size
+ 2) * sizeof(uint32_t);
736 thread_setentrypoint(thread
, result
->entry_point
);
743 * Set p->p_comm and p->p_name to the name passed to exec
746 set_proc_name(struct image_params
*imgp
, proc_t p
)
748 int p_name_len
= sizeof(p
->p_name
) - 1;
750 if (imgp
->ip_ndp
->ni_cnd
.cn_namelen
> p_name_len
) {
751 imgp
->ip_ndp
->ni_cnd
.cn_namelen
= p_name_len
;
754 bcopy((caddr_t
)imgp
->ip_ndp
->ni_cnd
.cn_nameptr
, (caddr_t
)p
->p_name
,
755 (unsigned)imgp
->ip_ndp
->ni_cnd
.cn_namelen
);
756 p
->p_name
[imgp
->ip_ndp
->ni_cnd
.cn_namelen
] = '\0';
758 if (imgp
->ip_ndp
->ni_cnd
.cn_namelen
> MAXCOMLEN
) {
759 imgp
->ip_ndp
->ni_cnd
.cn_namelen
= MAXCOMLEN
;
762 bcopy((caddr_t
)imgp
->ip_ndp
->ni_cnd
.cn_nameptr
, (caddr_t
)p
->p_comm
,
763 (unsigned)imgp
->ip_ndp
->ni_cnd
.cn_namelen
);
764 p
->p_comm
[imgp
->ip_ndp
->ni_cnd
.cn_namelen
] = '\0';
770 * Image activator for mach-o 1.0 binaries.
772 * Parameters; struct image_params * image parameter block
774 * Returns: -1 not a fat binary (keep looking)
775 * -2 Success: encapsulated binary: reread
776 * >0 Failure: error number
777 * EBADARCH Mach-o binary, but with an unrecognized
779 * ENOMEM No memory for child process after -
780 * can only happen after vfork()
782 * Important: This image activator is NOT byte order neutral.
784 * Note: A return value other than -1 indicates subsequent image
785 * activators should not be given the opportunity to attempt
786 * to activate the image.
788 * TODO: More gracefully handle failures after vfork
791 exec_mach_imgact(struct image_params
*imgp
)
793 struct mach_header
*mach_header
= (struct mach_header
*)imgp
->ip_vdata
;
794 proc_t p
= vfs_context_proc(imgp
->ip_vfs_context
);
797 task_t new_task
= NULL
; /* protected by vfexec */
799 struct uthread
*uthread
;
800 vm_map_t old_map
= VM_MAP_NULL
;
803 load_result_t load_result
;
804 struct _posix_spawnattr
*psa
= NULL
;
805 int spawn
= (imgp
->ip_flags
& IMGPF_SPAWN
);
806 int vfexec
= (imgp
->ip_flags
& IMGPF_VFORK_EXEC
);
807 os_reason_t exec_failure_reason
= OS_REASON_NULL
;
810 * make sure it's a Mach-O 1.0 or Mach-O 2.0 binary; the difference
811 * is a reserved field on the end, so for the most part, we can
812 * treat them as if they were identical. Reverse-endian Mach-O
813 * binaries are recognized but not compatible.
815 if ((mach_header
->magic
== MH_CIGAM
) ||
816 (mach_header
->magic
== MH_CIGAM_64
)) {
821 if ((mach_header
->magic
!= MH_MAGIC
) &&
822 (mach_header
->magic
!= MH_MAGIC_64
)) {
827 if (mach_header
->filetype
!= MH_EXECUTE
) {
832 if (imgp
->ip_origcputype
!= 0) {
833 /* Fat header previously had an idea about this thin file */
834 if (imgp
->ip_origcputype
!= mach_header
->cputype
||
835 imgp
->ip_origcpusubtype
!= mach_header
->cpusubtype
) {
840 imgp
->ip_origcputype
= mach_header
->cputype
;
841 imgp
->ip_origcpusubtype
= mach_header
->cpusubtype
;
844 task
= current_task();
845 thread
= current_thread();
846 uthread
= get_bsdthread_info(thread
);
848 if ((mach_header
->cputype
& CPU_ARCH_ABI64
) == CPU_ARCH_ABI64
)
849 imgp
->ip_flags
|= IMGPF_IS_64BIT
;
851 /* If posix_spawn binprefs exist, respect those prefs. */
852 psa
= (struct _posix_spawnattr
*) imgp
->ip_px_sa
;
853 if (psa
!= NULL
&& psa
->psa_binprefs
[0] != 0) {
855 for (pr
= 0; pr
< NBINPREFS
; pr
++) {
856 cpu_type_t pref
= psa
->psa_binprefs
[pr
];
858 /* No suitable arch in the pref list */
863 if (pref
== CPU_TYPE_ANY
) {
864 /* Jump to regular grading */
868 if (pref
== imgp
->ip_origcputype
) {
869 /* We have a match! */
877 if (!grade_binary(imgp
->ip_origcputype
, imgp
->ip_origcpusubtype
& ~CPU_SUBTYPE_MASK
)) {
882 /* Copy in arguments/environment from the old process */
883 error
= exec_extract_strings(imgp
);
887 AUDIT_ARG(argv
, imgp
->ip_startargv
, imgp
->ip_argc
,
888 imgp
->ip_endargv
- imgp
->ip_startargv
);
889 AUDIT_ARG(envv
, imgp
->ip_endargv
, imgp
->ip_envc
,
890 imgp
->ip_endenvv
- imgp
->ip_endargv
);
893 * We are being called to activate an image subsequent to a vfork()
894 * operation; in this case, we know that our task, thread, and
895 * uthread are actually those of our parent, and our proc, which we
896 * obtained indirectly from the image_params vfs_context_t, is the
899 if (vfexec
|| spawn
) {
901 imgp
->ip_new_thread
= fork_create_child(task
, NULL
, p
, FALSE
, (imgp
->ip_flags
& IMGPF_IS_64BIT
));
902 if (imgp
->ip_new_thread
== NULL
) {
908 /* reset local idea of thread, uthread, task */
909 thread
= imgp
->ip_new_thread
;
910 uthread
= get_bsdthread_info(thread
);
911 task
= new_task
= get_threadtask(thread
);
912 map
= get_task_map(task
);
918 * Load the Mach-O file.
920 * NOTE: An error after this point indicates we have potentially
921 * destroyed or overwritten some process state while attempting an
922 * execve() following a vfork(), which is an unrecoverable condition.
923 * We send the new process an immediate SIGKILL to avoid it executing
924 * any instructions in the mutated address space. For true spawns,
925 * this is not the case, and "too late" is still not too late to
926 * return an error code to the parent process.
930 * Actually load the image file we previously decided to load.
932 lret
= load_machfile(imgp
, mach_header
, thread
, &map
, &load_result
);
933 if (lret
!= LOAD_SUCCESS
) {
934 error
= load_return_to_errno(lret
);
936 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXITREASON_CREATE
) | DBG_FUNC_NONE
,
937 p
->p_pid
, OS_REASON_EXEC
, EXEC_EXIT_REASON_BAD_MACHO
, 0, 0);
938 if (lret
== LOAD_BADMACHO_UPX
) {
939 /* set anything that might be useful in the crash report */
940 set_proc_name(imgp
, p
);
942 exec_failure_reason
= os_reason_create(OS_REASON_EXEC
, EXEC_EXIT_REASON_UPX
);
943 exec_failure_reason
->osr_flags
|= OS_REASON_FLAG_GENERATE_CRASH_REPORT
;
944 exec_failure_reason
->osr_flags
|= OS_REASON_FLAG_CONSISTENT_FAILURE
;
946 exec_failure_reason
= os_reason_create(OS_REASON_EXEC
, EXEC_EXIT_REASON_BAD_MACHO
);
953 p
->p_cputype
= imgp
->ip_origcputype
;
954 p
->p_cpusubtype
= imgp
->ip_origcpusubtype
;
957 vm_map_set_user_wire_limit(map
, p
->p_rlimit
[RLIMIT_MEMLOCK
].rlim_cur
);
960 * Set code-signing flags if this binary is signed, or if parent has
961 * requested them on exec.
963 if (load_result
.csflags
& CS_VALID
) {
964 imgp
->ip_csflags
|= load_result
.csflags
&
966 CS_HARD
|CS_KILL
|CS_RESTRICT
|CS_ENFORCEMENT
|CS_REQUIRE_LV
|
967 CS_ENTITLEMENTS_VALIDATED
|CS_DYLD_PLATFORM
|
968 CS_ENTITLEMENT_FLAGS
|
969 CS_EXEC_SET_HARD
|CS_EXEC_SET_KILL
|CS_EXEC_SET_ENFORCEMENT
);
971 imgp
->ip_csflags
&= ~CS_VALID
;
974 if (p
->p_csflags
& CS_EXEC_SET_HARD
)
975 imgp
->ip_csflags
|= CS_HARD
;
976 if (p
->p_csflags
& CS_EXEC_SET_KILL
)
977 imgp
->ip_csflags
|= CS_KILL
;
978 if (p
->p_csflags
& CS_EXEC_SET_ENFORCEMENT
)
979 imgp
->ip_csflags
|= CS_ENFORCEMENT
;
980 if (p
->p_csflags
& CS_EXEC_SET_INSTALLER
)
981 imgp
->ip_csflags
|= CS_INSTALLER
;
984 * Set up the system reserved areas in the new address space.
986 vm_map_exec(map
, task
, load_result
.is64bit
, (void *)p
->p_fd
->fd_rdir
, cpu_type());
989 * Close file descriptors which specify close-on-exec.
991 fdexec(p
, psa
!= NULL
? psa
->psa_flags
: 0);
994 * deal with set[ug]id.
996 error
= exec_handle_sugid(imgp
);
998 if (spawn
|| !vfexec
) {
999 vm_map_deallocate(map
);
1002 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXITREASON_CREATE
) | DBG_FUNC_NONE
,
1003 p
->p_pid
, OS_REASON_EXEC
, EXEC_EXIT_REASON_SUGID_FAILURE
, 0, 0);
1004 exec_failure_reason
= os_reason_create(OS_REASON_EXEC
, EXEC_EXIT_REASON_SUGID_FAILURE
);
1009 * Commit to new map.
1011 * Swap the new map for the old, which consumes our new map reference but
1012 * each leaves us responsible for the old_map reference. That lets us get
1013 * off the pmap associated with it, and then we can release it.
1016 old_map
= swap_task_map(task
, thread
, map
, !spawn
);
1017 vm_map_deallocate(old_map
);
1020 lret
= activate_exec_state(task
, p
, thread
, &load_result
);
1021 if (lret
!= KERN_SUCCESS
) {
1023 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXITREASON_CREATE
) | DBG_FUNC_NONE
,
1024 p
->p_pid
, OS_REASON_EXEC
, EXEC_EXIT_REASON_ACTV_THREADSTATE
, 0, 0);
1025 exec_failure_reason
= os_reason_create(OS_REASON_EXEC
, EXEC_EXIT_REASON_ACTV_THREADSTATE
);
1030 * deal with voucher on exec-calling thread.
1032 if (imgp
->ip_new_thread
== NULL
)
1033 thread_set_mach_voucher(current_thread(), IPC_VOUCHER_NULL
);
1035 /* Make sure we won't interrupt ourself signalling a partial process */
1036 if (!vfexec
&& !spawn
&& (p
->p_lflag
& P_LTRACED
))
1037 psignal(p
, SIGTRAP
);
1039 if (load_result
.unixproc
&&
1040 create_unix_stack(get_task_map(task
),
1042 p
) != KERN_SUCCESS
) {
1043 error
= load_return_to_errno(LOAD_NOSPACE
);
1045 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXITREASON_CREATE
) | DBG_FUNC_NONE
,
1046 p
->p_pid
, OS_REASON_EXEC
, EXEC_EXIT_REASON_STACK_ALLOC
, 0, 0);
1047 exec_failure_reason
= os_reason_create(OS_REASON_EXEC
, EXEC_EXIT_REASON_STACK_ALLOC
);
1051 error
= exec_add_apple_strings(imgp
, &load_result
);
1054 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXITREASON_CREATE
) | DBG_FUNC_NONE
,
1055 p
->p_pid
, OS_REASON_EXEC
, EXEC_EXIT_REASON_APPLE_STRING_INIT
, 0, 0);
1056 exec_failure_reason
= os_reason_create(OS_REASON_EXEC
, EXEC_EXIT_REASON_APPLE_STRING_INIT
);
1060 if (vfexec
|| spawn
) {
1061 old_map
= vm_map_switch(get_task_map(task
));
1064 if (load_result
.unixproc
) {
1068 * Copy the strings area out into the new process address
1072 error
= exec_copyout_strings(imgp
, &ap
);
1074 if (vfexec
|| spawn
)
1075 vm_map_switch(old_map
);
1077 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXITREASON_CREATE
) | DBG_FUNC_NONE
,
1078 p
->p_pid
, OS_REASON_EXEC
, EXEC_EXIT_REASON_COPYOUT_STRINGS
, 0, 0);
1079 exec_failure_reason
= os_reason_create(OS_REASON_EXEC
, EXEC_EXIT_REASON_COPYOUT_STRINGS
);
1083 thread_setuserstack(thread
, ap
);
1086 if (load_result
.dynlinker
) {
1088 int new_ptr_size
= (imgp
->ip_flags
& IMGPF_IS_64BIT
) ? 8 : 4;
1090 /* Adjust the stack */
1091 ap
= thread_adjuserstack(thread
, -new_ptr_size
);
1092 error
= copyoutptr(load_result
.mach_header
, ap
, new_ptr_size
);
1095 if (vfexec
|| spawn
)
1096 vm_map_switch(old_map
);
1098 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXITREASON_CREATE
) | DBG_FUNC_NONE
,
1099 p
->p_pid
, OS_REASON_EXEC
, EXEC_EXIT_REASON_COPYOUT_DYNLINKER
, 0, 0);
1100 exec_failure_reason
= os_reason_create(OS_REASON_EXEC
, EXEC_EXIT_REASON_COPYOUT_DYNLINKER
);
1103 task_set_dyld_info(task
, load_result
.all_image_info_addr
,
1104 load_result
.all_image_info_size
);
1107 /* Avoid immediate VM faults back into kernel */
1108 exec_prefault_data(p
, imgp
, &load_result
);
1110 if (vfexec
|| spawn
) {
1111 vm_map_switch(old_map
);
1114 /* Stop profiling */
1118 * Reset signal state.
1120 execsigs(p
, thread
);
1123 * need to cancel async IO requests that can be cancelled and wait for those
1124 * already active. MAY BLOCK!
1129 /* FIXME: Till vmspace inherit is fixed: */
1130 if (!vfexec
&& p
->vm_shm
)
1134 /* Clean up the semaphores */
1139 * Remember file name for accounting.
1141 p
->p_acflag
&= ~AFORK
;
1143 set_proc_name(imgp
, p
);
1145 #if CONFIG_SECLUDED_MEMORY
1146 if (secluded_for_apps
) {
1147 if (strncmp(p
->p_name
,
1149 sizeof (p
->p_name
)) == 0 ||
1153 sizeof (p
->p_name
)) == 0 ||
1157 sizeof (p
->p_name
)) == 0) {
1158 task_set_could_use_secluded_mem(p
->task
, TRUE
);
1160 task_set_could_use_secluded_mem(p
->task
, FALSE
);
1162 if (strncmp(p
->p_name
,
1164 sizeof (p
->p_name
)) == 0) {
1165 task_set_could_also_use_secluded_mem(p
->task
, TRUE
);
1168 #endif /* CONFIG_SECLUDED_MEMORY */
1170 pal_dbg_set_task_name( p
->task
);
1172 #if DEVELOPMENT || DEBUG
1174 * Update the pid an proc name for importance base if any
1176 task_importance_update_owner_info(p
->task
);
1179 memcpy(&p
->p_uuid
[0], &load_result
.uuid
[0], sizeof(p
->p_uuid
));
1182 dtrace_proc_exec(p
);
1185 if (kdebug_enable
) {
1186 long dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
;
1189 * Collect the pathname for tracing
1191 kdbg_trace_string(p
, &dbg_arg1
, &dbg_arg2
, &dbg_arg3
, &dbg_arg4
);
1193 if (vfexec
|| spawn
) {
1194 KERNEL_DEBUG_CONSTANT1(TRACE_DATA_EXEC
| DBG_FUNC_NONE
,
1195 p
->p_pid
,0,0,0, (uintptr_t)thread_tid(thread
));
1196 KERNEL_DEBUG_CONSTANT1(TRACE_STRING_EXEC
| DBG_FUNC_NONE
,
1197 dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
, (uintptr_t)thread_tid(thread
));
1199 KERNEL_DEBUG_CONSTANT(TRACE_DATA_EXEC
| DBG_FUNC_NONE
,
1201 KERNEL_DEBUG_CONSTANT(TRACE_STRING_EXEC
| DBG_FUNC_NONE
,
1202 dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
, 0);
1207 * If posix_spawned with the START_SUSPENDED flag, stop the
1208 * process before it runs.
1210 if (imgp
->ip_px_sa
!= NULL
) {
1211 psa
= (struct _posix_spawnattr
*) imgp
->ip_px_sa
;
1212 if (psa
->psa_flags
& POSIX_SPAWN_START_SUSPENDED
) {
1216 (void) task_suspend_internal(p
->task
);
1221 * mark as execed, wakeup the process that vforked (if any) and tell
1222 * it that it now has its own resources back
1224 OSBitOrAtomic(P_EXEC
, &p
->p_flag
);
1225 proc_resetregister(p
);
1226 if (p
->p_pptr
&& (p
->p_lflag
& P_LPPWAIT
)) {
1228 p
->p_lflag
&= ~P_LPPWAIT
;
1230 wakeup((caddr_t
)p
->p_pptr
);
1234 * Pay for our earlier safety; deliver the delayed signals from
1235 * the incomplete vfexec process now that it's complete.
1237 if (vfexec
&& (p
->p_lflag
& P_LTRACED
)) {
1238 psignal_vfork(p
, new_task
, thread
, SIGTRAP
);
1244 /* Don't allow child process to execute any instructions */
1247 assert(exec_failure_reason
!= OS_REASON_NULL
);
1248 psignal_vfork_with_reason(p
, new_task
, thread
, SIGKILL
, exec_failure_reason
);
1249 exec_failure_reason
= OS_REASON_NULL
;
1251 assert(exec_failure_reason
!= OS_REASON_NULL
);
1252 psignal_with_reason(p
, SIGKILL
, exec_failure_reason
);
1253 exec_failure_reason
= OS_REASON_NULL
;
1256 /* We can't stop this system call at this point, so just pretend we succeeded */
1259 os_reason_free(exec_failure_reason
);
1260 exec_failure_reason
= OS_REASON_NULL
;
1265 /* notify only if it has not failed due to FP Key error */
1266 if ((p
->p_lflag
& P_LTERM_DECRYPTFAIL
) == 0)
1267 proc_knote(p
, NOTE_EXEC
);
1270 /* Drop extra references for cases where we don't expect the caller to clean up */
1271 if (vfexec
|| (spawn
&& error
== 0)) {
1272 task_deallocate(new_task
);
1273 thread_deallocate(thread
);
1276 if (load_result
.threadstate
) {
1277 kfree(load_result
.threadstate
, load_result
.threadstate_sz
);
1278 load_result
.threadstate
= NULL
;
1282 /* If we hit this, we likely would have leaked an exit reason */
1283 assert(exec_failure_reason
== OS_REASON_NULL
);
1291 * Our image activator table; this is the table of the image types we are
1292 * capable of loading. We list them in order of preference to ensure the
1293 * fastest image load speed.
1295 * XXX hardcoded, for now; should use linker sets
1298 int (*ex_imgact
)(struct image_params
*);
1299 const char *ex_name
;
1301 { exec_mach_imgact
, "Mach-o Binary" },
1302 { exec_fat_imgact
, "Fat Binary" },
1303 { exec_shell_imgact
, "Interpreter Script" },
1309 * exec_activate_image
1311 * Description: Iterate through the available image activators, and activate
1312 * the image associated with the imgp structure. We start with
1315 * Parameters: struct image_params * Image parameter block
1317 * Returns: 0 Success
1318 * EBADEXEC The executable is corrupt/unknown
1319 * execargs_alloc:EINVAL Invalid argument
1320 * execargs_alloc:EACCES Permission denied
1321 * execargs_alloc:EINTR Interrupted function
1322 * execargs_alloc:ENOMEM Not enough space
1323 * exec_save_path:EFAULT Bad address
1324 * exec_save_path:ENAMETOOLONG Filename too long
1325 * exec_check_permissions:EACCES Permission denied
1326 * exec_check_permissions:ENOEXEC Executable file format error
1327 * exec_check_permissions:ETXTBSY Text file busy [misuse of error code]
1328 * exec_check_permissions:???
1330 * vn_rdwr:??? [anything vn_rdwr can return]
1331 * <ex_imgact>:??? [anything an imgact can return]
1332 * EDEADLK Process is being terminated
1335 exec_activate_image(struct image_params
*imgp
)
1337 struct nameidata
*ndp
= NULL
;
1338 const char *excpath
;
1341 int once
= 1; /* save SGUID-ness for interpreted files */
1344 proc_t p
= vfs_context_proc(imgp
->ip_vfs_context
);
1346 error
= execargs_alloc(imgp
);
1350 error
= exec_save_path(imgp
, imgp
->ip_user_fname
, imgp
->ip_seg
, &excpath
);
1355 /* Use excpath, which contains the copyin-ed exec path */
1356 DTRACE_PROC1(exec
, uintptr_t, excpath
);
1358 MALLOC(ndp
, struct nameidata
*, sizeof(*ndp
), M_TEMP
, M_WAITOK
| M_ZERO
);
1364 NDINIT(ndp
, LOOKUP
, OP_LOOKUP
, FOLLOW
| LOCKLEAF
| AUDITVNPATH1
,
1365 UIO_SYSSPACE
, CAST_USER_ADDR_T(excpath
), imgp
->ip_vfs_context
);
1371 imgp
->ip_ndp
= ndp
; /* successful namei(); call nameidone() later */
1372 imgp
->ip_vp
= ndp
->ni_vp
; /* if set, need to vnode_put() at some point */
1375 * Before we start the transition from binary A to binary B, make
1376 * sure another thread hasn't started exiting the process. We grab
1377 * the proc lock to check p_lflag initially, and the transition
1378 * mechanism ensures that the value doesn't change after we release
1382 if (p
->p_lflag
& P_LEXIT
) {
1387 error
= proc_transstart(p
, 1, 0);
1392 error
= exec_check_permissions(imgp
);
1396 /* Copy; avoid invocation of an interpreter overwriting the original */
1399 *imgp
->ip_origvattr
= *imgp
->ip_vattr
;
1402 error
= vn_rdwr(UIO_READ
, imgp
->ip_vp
, imgp
->ip_vdata
, PAGE_SIZE
, 0,
1403 UIO_SYSSPACE
, IO_NODELOCKED
,
1404 vfs_context_ucred(imgp
->ip_vfs_context
),
1405 &resid
, vfs_context_proc(imgp
->ip_vfs_context
));
1410 memset(imgp
->ip_vdata
+ (PAGE_SIZE
- resid
), 0x0, resid
);
1413 encapsulated_binary
:
1414 /* Limit the number of iterations we will attempt on each binary */
1415 if (++itercount
> EAI_ITERLIMIT
) {
1420 for(i
= 0; error
== -1 && execsw
[i
].ex_imgact
!= NULL
; i
++) {
1422 error
= (*execsw
[i
].ex_imgact
)(imgp
);
1425 /* case -1: not claimed: continue */
1426 case -2: /* Encapsulated binary, imgp->ip_XXX set for next iteration */
1427 goto encapsulated_binary
;
1429 case -3: /* Interpreter */
1432 * Copy the script label for later use. Note that
1433 * the label can be different when the script is
1434 * actually read by the interpreter.
1436 if (imgp
->ip_scriptlabelp
)
1437 mac_vnode_label_free(imgp
->ip_scriptlabelp
);
1438 imgp
->ip_scriptlabelp
= mac_vnode_label_alloc();
1439 if (imgp
->ip_scriptlabelp
== NULL
) {
1443 mac_vnode_label_copy(imgp
->ip_vp
->v_label
,
1444 imgp
->ip_scriptlabelp
);
1447 * Take a ref of the script vnode for later use.
1449 if (imgp
->ip_scriptvp
)
1450 vnode_put(imgp
->ip_scriptvp
);
1451 if (vnode_getwithref(imgp
->ip_vp
) == 0)
1452 imgp
->ip_scriptvp
= imgp
->ip_vp
;
1457 vnode_put(imgp
->ip_vp
);
1458 imgp
->ip_vp
= NULL
; /* already put */
1459 imgp
->ip_ndp
= NULL
; /* already nameidone */
1461 /* Use excpath, which exec_shell_imgact reset to the interpreter */
1462 NDINIT(ndp
, LOOKUP
, OP_LOOKUP
, FOLLOW
| LOCKLEAF
,
1463 UIO_SYSSPACE
, CAST_USER_ADDR_T(excpath
), imgp
->ip_vfs_context
);
1465 proc_transend(p
, 0);
1474 * Call out to allow 3rd party notification of exec.
1475 * Ignore result of kauth_authorize_fileop call.
1477 if (error
== 0 && kauth_authorize_fileop_has_listeners()) {
1478 kauth_authorize_fileop(vfs_context_ucred(imgp
->ip_vfs_context
),
1480 (uintptr_t)ndp
->ni_vp
, 0);
1485 * Reset atm context from task
1487 task_atm_reset(p
->task
);
1490 * Reset old bank context from task
1492 task_bank_reset(p
->task
);
1495 proc_transend(p
, 0);
1498 if (imgp
->ip_strings
)
1499 execargs_free(imgp
);
1501 nameidone(imgp
->ip_ndp
);
1510 * exec_handle_spawnattr_policy
1512 * Description: Decode and apply the posix_spawn apptype, qos clamp, and watchport ports to the task.
1514 * Parameters: proc_t p process to apply attributes to
1515 * int psa_apptype posix spawn attribute apptype
1517 * Returns: 0 Success
1520 exec_handle_spawnattr_policy(proc_t p
, int psa_apptype
, uint64_t psa_qos_clamp
, uint64_t psa_darwin_role
,
1521 ipc_port_t
* portwatch_ports
, int portwatch_count
)
1523 int apptype
= TASK_APPTYPE_NONE
;
1524 int qos_clamp
= THREAD_QOS_UNSPECIFIED
;
1525 int role
= TASK_UNSPECIFIED
;
1527 if ((psa_apptype
& POSIX_SPAWN_PROC_TYPE_MASK
) != 0) {
1528 int proctype
= psa_apptype
& POSIX_SPAWN_PROC_TYPE_MASK
;
1531 case POSIX_SPAWN_PROC_TYPE_DAEMON_INTERACTIVE
:
1532 apptype
= TASK_APPTYPE_DAEMON_INTERACTIVE
;
1534 case POSIX_SPAWN_PROC_TYPE_DAEMON_STANDARD
:
1535 apptype
= TASK_APPTYPE_DAEMON_STANDARD
;
1537 case POSIX_SPAWN_PROC_TYPE_DAEMON_ADAPTIVE
:
1538 apptype
= TASK_APPTYPE_DAEMON_ADAPTIVE
;
1540 case POSIX_SPAWN_PROC_TYPE_DAEMON_BACKGROUND
:
1541 apptype
= TASK_APPTYPE_DAEMON_BACKGROUND
;
1543 case POSIX_SPAWN_PROC_TYPE_APP_DEFAULT
:
1544 apptype
= TASK_APPTYPE_APP_DEFAULT
;
1546 case POSIX_SPAWN_PROC_TYPE_APP_TAL
:
1547 apptype
= TASK_APPTYPE_APP_TAL
;
1550 apptype
= TASK_APPTYPE_NONE
;
1551 /* TODO: Should an invalid value here fail the spawn? */
1556 if (psa_qos_clamp
!= POSIX_SPAWN_PROC_CLAMP_NONE
) {
1557 switch (psa_qos_clamp
) {
1558 case POSIX_SPAWN_PROC_CLAMP_UTILITY
:
1559 qos_clamp
= THREAD_QOS_UTILITY
;
1561 case POSIX_SPAWN_PROC_CLAMP_BACKGROUND
:
1562 qos_clamp
= THREAD_QOS_BACKGROUND
;
1564 case POSIX_SPAWN_PROC_CLAMP_MAINTENANCE
:
1565 qos_clamp
= THREAD_QOS_MAINTENANCE
;
1568 qos_clamp
= THREAD_QOS_UNSPECIFIED
;
1569 /* TODO: Should an invalid value here fail the spawn? */
1574 if (psa_darwin_role
!= PRIO_DARWIN_ROLE_DEFAULT
) {
1575 proc_darwin_role_to_task_role(psa_darwin_role
, &role
);
1578 if (apptype
!= TASK_APPTYPE_NONE
||
1579 qos_clamp
!= THREAD_QOS_UNSPECIFIED
||
1580 role
!= TASK_UNSPECIFIED
) {
1581 proc_set_task_spawnpolicy(p
->task
, apptype
, qos_clamp
, role
,
1582 portwatch_ports
, portwatch_count
);
1590 * exec_handle_port_actions
1592 * Description: Go through the _posix_port_actions_t contents,
1593 * calling task_set_special_port, task_set_exception_ports
1594 * and/or audit_session_spawnjoin for the current task.
1596 * Parameters: struct image_params * Image parameter block
1598 * Returns: 0 Success
1600 * ENOTSUP Illegal posix_spawn attr flag was set
1603 exec_handle_port_actions(struct image_params
*imgp
, boolean_t
* portwatch_present
,
1604 ipc_port_t
* portwatch_ports
)
1606 _posix_spawn_port_actions_t pacts
= imgp
->ip_px_spa
;
1607 proc_t p
= vfs_context_proc(imgp
->ip_vfs_context
);
1608 _ps_port_action_t
*act
= NULL
;
1609 task_t task
= p
->task
;
1610 ipc_port_t port
= NULL
;
1615 *portwatch_present
= FALSE
;
1617 for (i
= 0; i
< pacts
->pspa_count
; i
++) {
1618 act
= &pacts
->pspa_actions
[i
];
1620 if (MACH_PORT_VALID(act
->new_port
)) {
1621 kr
= ipc_object_copyin(get_task_ipcspace(current_task()),
1622 act
->new_port
, MACH_MSG_TYPE_COPY_SEND
,
1623 (ipc_object_t
*) &port
);
1625 if (kr
!= KERN_SUCCESS
) {
1630 /* it's NULL or DEAD */
1631 port
= CAST_MACH_NAME_TO_PORT(act
->new_port
);
1634 switch (act
->port_type
) {
1636 kr
= task_set_special_port(task
, act
->which
, port
);
1638 if (kr
!= KERN_SUCCESS
)
1642 case PSPA_EXCEPTION
:
1643 kr
= task_set_exception_ports(task
, act
->mask
, port
,
1644 act
->behavior
, act
->flavor
);
1645 if (kr
!= KERN_SUCCESS
)
1649 case PSPA_AU_SESSION
:
1650 ret
= audit_session_spawnjoin(p
, port
);
1653 case PSPA_IMP_WATCHPORTS
:
1654 if (portwatch_ports
!= NULL
&& IPC_PORT_VALID(port
)) {
1655 *portwatch_present
= TRUE
;
1656 /* hold on to this till end of spawn */
1657 portwatch_ports
[i
] = port
;
1659 ipc_port_release_send(port
);
1669 /* action failed, so release port resources */
1670 ipc_port_release_send(port
);
1677 DTRACE_PROC1(spawn__port__failure
, mach_port_name_t
, act
->new_port
);
1682 * exec_handle_file_actions
1684 * Description: Go through the _posix_file_actions_t contents applying the
1685 * open, close, and dup2 operations to the open file table for
1686 * the current process.
1688 * Parameters: struct image_params * Image parameter block
1690 * Returns: 0 Success
1693 * Note: Actions are applied in the order specified, with the credential
1694 * of the parent process. This is done to permit the parent
1695 * process to utilize POSIX_SPAWN_RESETIDS to drop privilege in
1696 * the child following operations the child may in fact not be
1697 * normally permitted to perform.
1700 exec_handle_file_actions(struct image_params
*imgp
, short psa_flags
)
1704 proc_t p
= vfs_context_proc(imgp
->ip_vfs_context
);
1705 _posix_spawn_file_actions_t px_sfap
= imgp
->ip_px_sfa
;
1706 int ival
[2]; /* dummy retval for system calls) */
1708 for (action
= 0; action
< px_sfap
->psfa_act_count
; action
++) {
1709 _psfa_action_t
*psfa
= &px_sfap
->psfa_act_acts
[ action
];
1711 switch(psfa
->psfaa_type
) {
1714 * Open is different, in that it requires the use of
1715 * a path argument, which is normally copied in from
1716 * user space; because of this, we have to support an
1717 * open from kernel space that passes an address space
1718 * context of UIO_SYSSPACE, and casts the address
1719 * argument to a user_addr_t.
1722 struct vnode_attr
*vap
;
1723 struct nameidata
*ndp
;
1724 int mode
= psfa
->psfaa_openargs
.psfao_mode
;
1725 struct dup2_args dup2a
;
1726 struct close_nocancel_args ca
;
1729 MALLOC(bufp
, char *, sizeof(*vap
) + sizeof(*ndp
), M_TEMP
, M_WAITOK
| M_ZERO
);
1735 vap
= (struct vnode_attr
*) bufp
;
1736 ndp
= (struct nameidata
*) (bufp
+ sizeof(*vap
));
1739 /* Mask off all but regular access permissions */
1740 mode
= ((mode
&~ p
->p_fd
->fd_cmask
) & ALLPERMS
) & ~S_ISTXT
;
1741 VATTR_SET(vap
, va_mode
, mode
& ACCESSPERMS
);
1743 NDINIT(ndp
, LOOKUP
, OP_OPEN
, FOLLOW
| AUDITVNPATH1
, UIO_SYSSPACE
,
1744 CAST_USER_ADDR_T(psfa
->psfaa_openargs
.psfao_path
),
1745 imgp
->ip_vfs_context
);
1747 error
= open1(imgp
->ip_vfs_context
,
1749 psfa
->psfaa_openargs
.psfao_oflag
,
1751 fileproc_alloc_init
, NULL
,
1757 * If there's an error, or we get the right fd by
1758 * accident, then drop out here. This is easier than
1759 * reworking all the open code to preallocate fd
1760 * slots, and internally taking one as an argument.
1762 if (error
|| ival
[0] == psfa
->psfaa_filedes
)
1767 * If we didn't fall out from an error, we ended up
1768 * with the wrong fd; so now we've got to try to dup2
1769 * it to the right one.
1771 dup2a
.from
= origfd
;
1772 dup2a
.to
= psfa
->psfaa_filedes
;
1775 * The dup2() system call implementation sets
1776 * ival to newfd in the success case, but we
1777 * can ignore that, since if we didn't get the
1778 * fd we wanted, the error will stop us.
1780 error
= dup2(p
, &dup2a
, ival
);
1785 * Finally, close the original fd.
1789 error
= close_nocancel(p
, &ca
, ival
);
1794 struct dup2_args dup2a
;
1796 dup2a
.from
= psfa
->psfaa_filedes
;
1797 dup2a
.to
= psfa
->psfaa_openargs
.psfao_oflag
;
1800 * The dup2() system call implementation sets
1801 * ival to newfd in the success case, but we
1802 * can ignore that, since if we didn't get the
1803 * fd we wanted, the error will stop us.
1805 error
= dup2(p
, &dup2a
, ival
);
1810 struct close_nocancel_args ca
;
1812 ca
.fd
= psfa
->psfaa_filedes
;
1814 error
= close_nocancel(p
, &ca
, ival
);
1818 case PSFA_INHERIT
: {
1819 struct fcntl_nocancel_args fcntla
;
1822 * Check to see if the descriptor exists, and
1823 * ensure it's -not- marked as close-on-exec.
1825 * Attempting to "inherit" a guarded fd will
1826 * result in a error.
1828 fcntla
.fd
= psfa
->psfaa_filedes
;
1829 fcntla
.cmd
= F_GETFD
;
1830 if ((error
= fcntl_nocancel(p
, &fcntla
, ival
)) != 0)
1833 if ((ival
[0] & FD_CLOEXEC
) == FD_CLOEXEC
) {
1834 fcntla
.fd
= psfa
->psfaa_filedes
;
1835 fcntla
.cmd
= F_SETFD
;
1836 fcntla
.arg
= ival
[0] & ~FD_CLOEXEC
;
1837 error
= fcntl_nocancel(p
, &fcntla
, ival
);
1848 /* All file actions failures are considered fatal, per POSIX */
1851 if (PSFA_OPEN
== psfa
->psfaa_type
) {
1852 DTRACE_PROC1(spawn__open__failure
, uintptr_t,
1853 psfa
->psfaa_openargs
.psfao_path
);
1855 DTRACE_PROC1(spawn__fd__failure
, int, psfa
->psfaa_filedes
);
1861 if (error
!= 0 || (psa_flags
& POSIX_SPAWN_CLOEXEC_DEFAULT
) == 0)
1865 * If POSIX_SPAWN_CLOEXEC_DEFAULT is set, behave (during
1866 * this spawn only) as if "close on exec" is the default
1867 * disposition of all pre-existing file descriptors. In this case,
1868 * the list of file descriptors mentioned in the file actions
1869 * are the only ones that can be inherited, so mark them now.
1871 * The actual closing part comes later, in fdexec().
1874 for (action
= 0; action
< px_sfap
->psfa_act_count
; action
++) {
1875 _psfa_action_t
*psfa
= &px_sfap
->psfa_act_acts
[action
];
1876 int fd
= psfa
->psfaa_filedes
;
1878 switch (psfa
->psfaa_type
) {
1880 fd
= psfa
->psfaa_openargs
.psfao_oflag
;
1884 *fdflags(p
, fd
) |= UF_INHERIT
;
1898 * exec_spawnattr_getmacpolicyinfo
1901 exec_spawnattr_getmacpolicyinfo(const void *macextensions
, const char *policyname
, size_t *lenp
)
1903 const struct _posix_spawn_mac_policy_extensions
*psmx
= macextensions
;
1909 for (i
= 0; i
< psmx
->psmx_count
; i
++) {
1910 const _ps_mac_policy_extension_t
*extension
= &psmx
->psmx_extensions
[i
];
1911 if (strncmp(extension
->policyname
, policyname
, sizeof(extension
->policyname
)) == 0) {
1913 *lenp
= extension
->datalen
;
1914 return extension
->datap
;
1924 spawn_copyin_macpolicyinfo(const struct user__posix_spawn_args_desc
*px_args
, _posix_spawn_mac_policy_extensions_t
*psmxp
)
1926 _posix_spawn_mac_policy_extensions_t psmx
= NULL
;
1933 if (px_args
->mac_extensions_size
< PS_MAC_EXTENSIONS_SIZE(1) ||
1934 px_args
->mac_extensions_size
> PAGE_SIZE
) {
1939 MALLOC(psmx
, _posix_spawn_mac_policy_extensions_t
, px_args
->mac_extensions_size
, M_TEMP
, M_WAITOK
);
1940 if ((error
= copyin(px_args
->mac_extensions
, psmx
, px_args
->mac_extensions_size
)) != 0)
1943 if (PS_MAC_EXTENSIONS_SIZE(psmx
->psmx_count
) > px_args
->mac_extensions_size
) {
1948 for (i
= 0; i
< psmx
->psmx_count
; i
++) {
1949 _ps_mac_policy_extension_t
*extension
= &psmx
->psmx_extensions
[i
];
1950 if (extension
->datalen
== 0 || extension
->datalen
> PAGE_SIZE
) {
1956 for (copycnt
= 0; copycnt
< psmx
->psmx_count
; copycnt
++) {
1957 _ps_mac_policy_extension_t
*extension
= &psmx
->psmx_extensions
[copycnt
];
1960 MALLOC(data
, void *, extension
->datalen
, M_TEMP
, M_WAITOK
);
1961 if ((error
= copyin(extension
->data
, data
, extension
->datalen
)) != 0) {
1965 extension
->datap
= data
;
1973 for (i
= 0; i
< copycnt
; i
++)
1974 FREE(psmx
->psmx_extensions
[i
].datap
, M_TEMP
);
1981 spawn_free_macpolicyinfo(_posix_spawn_mac_policy_extensions_t psmx
)
1987 for (i
= 0; i
< psmx
->psmx_count
; i
++)
1988 FREE(psmx
->psmx_extensions
[i
].datap
, M_TEMP
);
1991 #endif /* CONFIG_MACF */
1993 #if CONFIG_COALITIONS
1994 static inline void spawn_coalitions_release_all(coalition_t coal
[COALITION_NUM_TYPES
])
1996 for (int c
= 0; c
< COALITION_NUM_TYPES
; c
++) {
1998 coalition_remove_active(coal
[c
]);
1999 coalition_release(coal
[c
]);
2006 static int spawn_validate_persona(struct _posix_spawn_persona_info
*px_persona
)
2009 struct persona
*persona
= NULL
;
2010 int verify
= px_persona
->pspi_flags
& POSIX_SPAWN_PERSONA_FLAGS_VERIFY
;
2013 * TODO: rdar://problem/19981151
2014 * Add entitlement check!
2016 if (!kauth_cred_issuser(kauth_cred_get()))
2019 persona
= persona_lookup(px_persona
->pspi_id
);
2026 if (px_persona
->pspi_flags
& POSIX_SPAWN_PERSONA_UID
) {
2027 if (px_persona
->pspi_uid
!= persona_get_uid(persona
)) {
2032 if (px_persona
->pspi_flags
& POSIX_SPAWN_PERSONA_GID
) {
2033 if (px_persona
->pspi_gid
!= persona_get_gid(persona
)) {
2038 if (px_persona
->pspi_flags
& POSIX_SPAWN_PERSONA_GROUPS
) {
2040 gid_t groups
[NGROUPS_MAX
];
2042 if (persona_get_groups(persona
, &ngroups
, groups
,
2043 px_persona
->pspi_ngroups
) != 0) {
2047 if (ngroups
!= (int)px_persona
->pspi_ngroups
) {
2052 if (px_persona
->pspi_groups
[ngroups
] != groups
[ngroups
]) {
2057 if (px_persona
->pspi_gmuid
!= persona_get_gmuid(persona
)) {
2066 persona_put(persona
);
2071 static int spawn_persona_adopt(proc_t p
, struct _posix_spawn_persona_info
*px_persona
)
2075 struct persona
*persona
= NULL
;
2076 int override
= !!(px_persona
->pspi_flags
& POSIX_SPAWN_PERSONA_FLAGS_OVERRIDE
);
2079 return persona_proc_adopt_id(p
, px_persona
->pspi_id
, NULL
);
2082 * we want to spawn into the given persona, but we want to override
2083 * the kauth with a different UID/GID combo
2085 persona
= persona_lookup(px_persona
->pspi_id
);
2089 cred
= persona_get_cred(persona
);
2095 if (px_persona
->pspi_flags
& POSIX_SPAWN_PERSONA_UID
) {
2096 cred
= kauth_cred_setresuid(cred
,
2097 px_persona
->pspi_uid
,
2098 px_persona
->pspi_uid
,
2099 px_persona
->pspi_uid
,
2103 if (px_persona
->pspi_flags
& POSIX_SPAWN_PERSONA_GID
) {
2104 cred
= kauth_cred_setresgid(cred
,
2105 px_persona
->pspi_gid
,
2106 px_persona
->pspi_gid
,
2107 px_persona
->pspi_gid
);
2110 if (px_persona
->pspi_flags
& POSIX_SPAWN_PERSONA_GROUPS
) {
2111 cred
= kauth_cred_setgroups(cred
,
2112 px_persona
->pspi_groups
,
2113 px_persona
->pspi_ngroups
,
2114 px_persona
->pspi_gmuid
);
2117 ret
= persona_proc_adopt(p
, persona
, cred
);
2120 persona_put(persona
);
2126 proc_set_return_wait(proc_t p
)
2129 p
->p_lflag
|= P_LRETURNWAIT
;
2134 proc_clear_return_wait(proc_t p
, thread_t child_thread
)
2138 p
->p_lflag
&= ~P_LRETURNWAIT
;
2139 if (p
->p_lflag
& P_LRETURNWAITER
) {
2140 wakeup(&p
->p_lflag
);
2145 (void)thread_resume(child_thread
);
2149 proc_wait_to_return()
2156 if (p
->p_lflag
& P_LRETURNWAIT
) {
2157 p
->p_lflag
|= P_LRETURNWAITER
;
2159 msleep(&p
->p_lflag
, &p
->p_mlock
, 0,
2160 "thread_check_setup_complete", NULL
);
2161 } while (p
->p_lflag
& P_LRETURNWAIT
);
2162 p
->p_lflag
&= ~P_LRETURNWAITER
;
2166 thread_bootstrap_return();
2172 * Parameters: uap->pid Pointer to pid return area
2173 * uap->fname File name to exec
2174 * uap->argp Argument list
2175 * uap->envp Environment list
2177 * Returns: 0 Success
2178 * EINVAL Invalid argument
2179 * ENOTSUP Not supported
2180 * ENOEXEC Executable file format error
2181 * exec_activate_image:EINVAL Invalid argument
2182 * exec_activate_image:EACCES Permission denied
2183 * exec_activate_image:EINTR Interrupted function
2184 * exec_activate_image:ENOMEM Not enough space
2185 * exec_activate_image:EFAULT Bad address
2186 * exec_activate_image:ENAMETOOLONG Filename too long
2187 * exec_activate_image:ENOEXEC Executable file format error
2188 * exec_activate_image:ETXTBSY Text file busy [misuse of error code]
2189 * exec_activate_image:EBADEXEC The executable is corrupt/unknown
2190 * exec_activate_image:???
2191 * mac_execve_enter:???
2193 * TODO: Expect to need __mac_posix_spawn() at some point...
2194 * Handle posix_spawnattr_t
2195 * Handle posix_spawn_file_actions_t
2198 posix_spawn(proc_t ap
, struct posix_spawn_args
*uap
, int32_t *retval
)
2200 proc_t p
= ap
; /* quiet bogus GCC vfork() warning */
2201 user_addr_t pid
= uap
->pid
;
2202 int ival
[2]; /* dummy retval for setpgid() */
2204 struct image_params
*imgp
;
2205 struct vnode_attr
*vap
;
2206 struct vnode_attr
*origvap
;
2207 struct uthread
*uthread
= 0; /* compiler complains if not set to 0*/
2209 int is_64
= IS_64BIT_PROCESS(p
);
2210 struct vfs_context context
;
2211 struct user__posix_spawn_args_desc px_args
;
2212 struct _posix_spawnattr px_sa
;
2213 _posix_spawn_file_actions_t px_sfap
= NULL
;
2214 _posix_spawn_port_actions_t px_spap
= NULL
;
2215 struct __kern_sigaction vec
;
2216 boolean_t spawn_no_exec
= FALSE
;
2217 boolean_t proc_transit_set
= TRUE
;
2218 boolean_t exec_done
= FALSE
;
2219 int portwatch_count
= 0;
2220 ipc_port_t
* portwatch_ports
= NULL
;
2221 vm_size_t px_sa_offset
= offsetof(struct _posix_spawnattr
, psa_ports
);
2223 struct _posix_spawn_persona_info
*px_persona
= NULL
;
2227 * Allocate a big chunk for locals instead of using stack since these
2228 * structures are pretty big.
2230 MALLOC(bufp
, char *, (sizeof(*imgp
) + sizeof(*vap
) + sizeof(*origvap
)), M_TEMP
, M_WAITOK
| M_ZERO
);
2231 imgp
= (struct image_params
*) bufp
;
2236 vap
= (struct vnode_attr
*) (bufp
+ sizeof(*imgp
));
2237 origvap
= (struct vnode_attr
*) (bufp
+ sizeof(*imgp
) + sizeof(*vap
));
2239 /* Initialize the common data in the image_params structure */
2240 imgp
->ip_user_fname
= uap
->path
;
2241 imgp
->ip_user_argv
= uap
->argv
;
2242 imgp
->ip_user_envv
= uap
->envp
;
2243 imgp
->ip_vattr
= vap
;
2244 imgp
->ip_origvattr
= origvap
;
2245 imgp
->ip_vfs_context
= &context
;
2246 imgp
->ip_flags
= (is_64
? IMGPF_WAS_64BIT
: IMGPF_NONE
);
2247 imgp
->ip_seg
= (is_64
? UIO_USERSPACE64
: UIO_USERSPACE32
);
2248 imgp
->ip_mac_return
= 0;
2249 imgp
->ip_px_persona
= NULL
;
2250 imgp
->ip_cs_error
= OS_REASON_NULL
;
2252 if (uap
->adesc
!= USER_ADDR_NULL
) {
2254 error
= copyin(uap
->adesc
, &px_args
, sizeof(px_args
));
2256 struct user32__posix_spawn_args_desc px_args32
;
2258 error
= copyin(uap
->adesc
, &px_args32
, sizeof(px_args32
));
2261 * Convert arguments descriptor from external 32 bit
2262 * representation to internal 64 bit representation
2264 px_args
.attr_size
= px_args32
.attr_size
;
2265 px_args
.attrp
= CAST_USER_ADDR_T(px_args32
.attrp
);
2266 px_args
.file_actions_size
= px_args32
.file_actions_size
;
2267 px_args
.file_actions
= CAST_USER_ADDR_T(px_args32
.file_actions
);
2268 px_args
.port_actions_size
= px_args32
.port_actions_size
;
2269 px_args
.port_actions
= CAST_USER_ADDR_T(px_args32
.port_actions
);
2270 px_args
.mac_extensions_size
= px_args32
.mac_extensions_size
;
2271 px_args
.mac_extensions
= CAST_USER_ADDR_T(px_args32
.mac_extensions
);
2272 px_args
.coal_info_size
= px_args32
.coal_info_size
;
2273 px_args
.coal_info
= CAST_USER_ADDR_T(px_args32
.coal_info
);
2274 px_args
.persona_info_size
= px_args32
.persona_info_size
;
2275 px_args
.persona_info
= CAST_USER_ADDR_T(px_args32
.persona_info
);
2280 if (px_args
.attr_size
!= 0) {
2282 * We are not copying the port_actions pointer,
2283 * because we already have it from px_args.
2284 * This is a bit fragile: <rdar://problem/16427422>
2287 if ((error
= copyin(px_args
.attrp
, &px_sa
, px_sa_offset
) != 0))
2290 bzero( (void *)( (unsigned long) &px_sa
+ px_sa_offset
), sizeof(px_sa
) - px_sa_offset
);
2292 imgp
->ip_px_sa
= &px_sa
;
2294 if (px_args
.file_actions_size
!= 0) {
2295 /* Limit file_actions to allowed number of open files */
2296 int maxfa
= (p
->p_limit
? p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
: NOFILE
);
2297 if (px_args
.file_actions_size
< PSF_ACTIONS_SIZE(1) ||
2298 px_args
.file_actions_size
> PSF_ACTIONS_SIZE(maxfa
)) {
2302 MALLOC(px_sfap
, _posix_spawn_file_actions_t
, px_args
.file_actions_size
, M_TEMP
, M_WAITOK
);
2303 if (px_sfap
== NULL
) {
2307 imgp
->ip_px_sfa
= px_sfap
;
2309 if ((error
= copyin(px_args
.file_actions
, px_sfap
,
2310 px_args
.file_actions_size
)) != 0)
2313 /* Verify that the action count matches the struct size */
2314 if (PSF_ACTIONS_SIZE(px_sfap
->psfa_act_count
) != px_args
.file_actions_size
) {
2319 if (px_args
.port_actions_size
!= 0) {
2320 /* Limit port_actions to one page of data */
2321 if (px_args
.port_actions_size
< PS_PORT_ACTIONS_SIZE(1) ||
2322 px_args
.port_actions_size
> PAGE_SIZE
) {
2327 MALLOC(px_spap
, _posix_spawn_port_actions_t
,
2328 px_args
.port_actions_size
, M_TEMP
, M_WAITOK
);
2329 if (px_spap
== NULL
) {
2333 imgp
->ip_px_spa
= px_spap
;
2335 if ((error
= copyin(px_args
.port_actions
, px_spap
,
2336 px_args
.port_actions_size
)) != 0)
2339 /* Verify that the action count matches the struct size */
2340 if (PS_PORT_ACTIONS_SIZE(px_spap
->pspa_count
) != px_args
.port_actions_size
) {
2346 /* copy in the persona info */
2347 if (px_args
.persona_info_size
!= 0 && px_args
.persona_info
!= 0) {
2348 /* for now, we need the exact same struct in user space */
2349 if (px_args
.persona_info_size
!= sizeof(*px_persona
)) {
2354 MALLOC(px_persona
, struct _posix_spawn_persona_info
*, px_args
.persona_info_size
, M_TEMP
, M_WAITOK
|M_ZERO
);
2355 if (px_persona
== NULL
) {
2359 imgp
->ip_px_persona
= px_persona
;
2361 if ((error
= copyin(px_args
.persona_info
, px_persona
,
2362 px_args
.persona_info_size
)) != 0)
2364 if ((error
= spawn_validate_persona(px_persona
)) != 0)
2369 if (px_args
.mac_extensions_size
!= 0) {
2370 if ((error
= spawn_copyin_macpolicyinfo(&px_args
, (_posix_spawn_mac_policy_extensions_t
*)&imgp
->ip_px_smpx
)) != 0)
2373 #endif /* CONFIG_MACF */
2376 /* set uthread to parent */
2377 uthread
= get_bsdthread_info(current_thread());
2380 * <rdar://6640530>; this does not result in a behaviour change
2381 * relative to Leopard, so there should not be any existing code
2382 * which depends on it.
2384 if (uthread
->uu_flag
& UT_VFORK
) {
2390 * If we don't have the extension flag that turns "posix_spawn()"
2391 * into "execve() with options", then we will be creating a new
2392 * process which does not inherit memory from the parent process,
2393 * which is one of the most expensive things about using fork()
2396 if (imgp
->ip_px_sa
== NULL
|| !(px_sa
.psa_flags
& POSIX_SPAWN_SETEXEC
)){
2398 /* Set the new task's coalition, if it is requested. */
2399 coalition_t coal
[COALITION_NUM_TYPES
] = { COALITION_NULL
};
2400 #if CONFIG_COALITIONS
2402 kern_return_t kr
= KERN_SUCCESS
;
2403 struct _posix_spawn_coalition_info coal_info
;
2404 int coal_role
[COALITION_NUM_TYPES
];
2406 if (imgp
->ip_px_sa
== NULL
|| !px_args
.coal_info
)
2409 memset(&coal_info
, 0, sizeof(coal_info
));
2411 if (px_args
.coal_info_size
> sizeof(coal_info
))
2412 px_args
.coal_info_size
= sizeof(coal_info
);
2413 error
= copyin(px_args
.coal_info
,
2414 &coal_info
, px_args
.coal_info_size
);
2419 for (i
= 0; i
< COALITION_NUM_TYPES
; i
++) {
2420 uint64_t cid
= coal_info
.psci_info
[i
].psci_id
;
2423 * don't allow tasks which are not in a
2424 * privileged coalition to spawn processes
2425 * into coalitions other than their own
2427 if (!task_is_in_privileged_coalition(p
->task
, i
)) {
2428 coal_dbg("ERROR: %d not in privilegd "
2429 "coalition of type %d",
2431 spawn_coalitions_release_all(coal
);
2436 coal_dbg("searching for coalition id:%llu", cid
);
2438 * take a reference and activation on the
2439 * coalition to guard against free-while-spawn
2442 coal
[i
] = coalition_find_and_activate_by_id(cid
);
2443 if (coal
[i
] == COALITION_NULL
) {
2444 coal_dbg("could not find coalition id:%llu "
2445 "(perhaps it has been terminated or reaped)", cid
);
2447 * release any other coalition's we
2448 * may have a reference to
2450 spawn_coalitions_release_all(coal
);
2454 if (coalition_type(coal
[i
]) != i
) {
2455 coal_dbg("coalition with id:%lld is not of type:%d"
2456 " (it's type:%d)", cid
, i
, coalition_type(coal
[i
]));
2460 coal_role
[i
] = coal_info
.psci_info
[i
].psci_role
;
2464 if (ncoals
< COALITION_NUM_TYPES
) {
2466 * If the user is attempting to spawn into a subset of
2467 * the known coalition types, then make sure they have
2468 * _at_least_ specified a resource coalition. If not,
2469 * the following fork1() call will implicitly force an
2470 * inheritance from 'p' and won't actually spawn the
2471 * new task into the coalitions the user specified.
2472 * (also the call to coalitions_set_roles will panic)
2474 if (coal
[COALITION_TYPE_RESOURCE
] == COALITION_NULL
) {
2475 spawn_coalitions_release_all(coal
);
2481 #endif /* CONFIG_COALITIONS */
2484 * note that this will implicitly inherit the
2485 * caller's persona (if it exists)
2487 error
= fork1(p
, &imgp
->ip_new_thread
, PROC_CREATE_SPAWN
, coal
);
2489 #if CONFIG_COALITIONS
2490 /* set the roles of this task within each given coalition */
2492 kr
= coalitions_set_roles(coal
, get_threadtask(imgp
->ip_new_thread
), coal_role
);
2493 if (kr
!= KERN_SUCCESS
)
2497 /* drop our references and activations - fork1() now holds them */
2498 spawn_coalitions_release_all(coal
);
2499 #endif /* CONFIG_COALITIONS */
2503 imgp
->ip_flags
|= IMGPF_SPAWN
; /* spawn w/o exec */
2504 spawn_no_exec
= TRUE
; /* used in later tests */
2508 * If the parent isn't in a persona (launchd), and
2509 * hasn't specified a new persona for the process,
2510 * then we'll put the process into the system persona
2512 * TODO: this will have to be re-worked because as of
2513 * now, without any launchd adoption, the resulting
2514 * xpcproxy process will not have sufficient
2515 * privileges to setuid/gid.
2518 if (!proc_has_persona(p
) && imgp
->ip_px_persona
== NULL
) {
2519 MALLOC(px_persona
, struct _posix_spawn_persona_info
*,
2520 sizeof(*px_persona
), M_TEMP
, M_WAITOK
|M_ZERO
);
2521 if (px_persona
== NULL
) {
2525 px_persona
->pspi_id
= persona_get_id(g_system_persona
);
2526 imgp
->ip_px_persona
= px_persona
;
2529 #endif /* CONFIG_PERSONAS */
2532 if (spawn_no_exec
) {
2533 p
= (proc_t
)get_bsdthreadtask_info(imgp
->ip_new_thread
);
2536 * We had to wait until this point before firing the
2537 * proc:::create probe, otherwise p would not point to the
2540 DTRACE_PROC1(create
, proc_t
, p
);
2544 /* By default, the thread everyone plays with is the parent */
2545 context
.vc_thread
= current_thread();
2546 context
.vc_ucred
= p
->p_ucred
; /* XXX must NOT be kauth_cred_get() */
2549 * However, if we're not in the setexec case, redirect the context
2550 * to the newly created process instead
2553 context
.vc_thread
= imgp
->ip_new_thread
;
2556 * Post fdcopy(), pre exec_handle_sugid() - this is where we want
2557 * to handle the file_actions. Since vfork() also ends up setting
2558 * us into the parent process group, and saved off the signal flags,
2559 * this is also where we want to handle the spawn flags.
2562 /* Has spawn file actions? */
2563 if (imgp
->ip_px_sfa
!= NULL
) {
2565 * The POSIX_SPAWN_CLOEXEC_DEFAULT flag
2566 * is handled in exec_handle_file_actions().
2568 if ((error
= exec_handle_file_actions(imgp
,
2569 imgp
->ip_px_sa
!= NULL
? px_sa
.psa_flags
: 0)) != 0)
2573 /* Has spawn port actions? */
2574 if (imgp
->ip_px_spa
!= NULL
) {
2575 boolean_t is_adaptive
= FALSE
;
2576 boolean_t portwatch_present
= FALSE
;
2578 /* Will this process become adaptive? The apptype isn't ready yet, so we can't look there. */
2579 if (imgp
->ip_px_sa
!= NULL
&& px_sa
.psa_apptype
== POSIX_SPAWN_PROC_TYPE_DAEMON_ADAPTIVE
)
2584 * Allocate a place to store the ports we want to bind to the new task
2585 * We can't bind them until after the apptype is set.
2587 if (px_spap
->pspa_count
!= 0 && is_adaptive
) {
2588 portwatch_count
= px_spap
->pspa_count
;
2589 MALLOC(portwatch_ports
, ipc_port_t
*, (sizeof(ipc_port_t
) * portwatch_count
), M_TEMP
, M_WAITOK
| M_ZERO
);
2591 portwatch_ports
= NULL
;
2594 if ((error
= exec_handle_port_actions(imgp
, &portwatch_present
, portwatch_ports
)) != 0)
2597 if (portwatch_present
== FALSE
&& portwatch_ports
!= NULL
) {
2598 FREE(portwatch_ports
, M_TEMP
);
2599 portwatch_ports
= NULL
;
2600 portwatch_count
= 0;
2604 /* Has spawn attr? */
2605 if (imgp
->ip_px_sa
!= NULL
) {
2607 * Set the process group ID of the child process; this has
2608 * to happen before the image activation.
2610 if (px_sa
.psa_flags
& POSIX_SPAWN_SETPGROUP
) {
2611 struct setpgid_args spga
;
2612 spga
.pid
= p
->p_pid
;
2613 spga
.pgid
= px_sa
.psa_pgroup
;
2615 * Effectively, call setpgid() system call; works
2616 * because there are no pointer arguments.
2618 if((error
= setpgid(p
, &spga
, ival
)) != 0)
2623 * Reset UID/GID to parent's RUID/RGID; This works only
2624 * because the operation occurs *after* the vfork() and
2625 * before the call to exec_handle_sugid() by the image
2626 * activator called from exec_activate_image(). POSIX
2627 * requires that any setuid/setgid bits on the process
2628 * image will take precedence over the spawn attributes
2631 * Modifications to p_ucred must be guarded using the
2632 * proc's ucred lock. This prevents others from accessing
2633 * a garbage credential.
2635 while (px_sa
.psa_flags
& POSIX_SPAWN_RESETIDS
) {
2636 kauth_cred_t my_cred
= kauth_cred_proc_ref(p
);
2637 kauth_cred_t my_new_cred
= kauth_cred_setuidgid(my_cred
, kauth_cred_getruid(my_cred
), kauth_cred_getrgid(my_cred
));
2639 if (my_cred
== my_new_cred
) {
2640 kauth_cred_unref(&my_cred
);
2644 /* update cred on proc */
2647 if (p
->p_ucred
!= my_cred
) {
2648 proc_ucred_unlock(p
);
2649 kauth_cred_unref(&my_new_cred
);
2653 /* donate cred reference on my_new_cred to p->p_ucred */
2654 p
->p_ucred
= my_new_cred
;
2655 PROC_UPDATE_CREDS_ONPROC(p
);
2656 proc_ucred_unlock(p
);
2658 /* drop additional reference that was taken on the previous cred */
2659 kauth_cred_unref(&my_cred
);
2663 if (spawn_no_exec
&& imgp
->ip_px_persona
!= NULL
) {
2665 * If we were asked to spawn a process into a new persona,
2666 * do the credential switch now (which may override the UID/GID
2667 * inherit done just above). It's important to do this switch
2668 * before image activation both for reasons stated above, and
2669 * to ensure that the new persona has access to the image/file
2672 error
= spawn_persona_adopt(p
, imgp
->ip_px_persona
);
2676 #endif /* CONFIG_PERSONAS */
2679 * Disable ASLR for the spawned process.
2681 * But only do so if we are not embedded + RELEASE.
2682 * While embedded allows for a boot-arg (-disable_aslr)
2683 * to deal with this (which itself is only honored on
2684 * DEVELOPMENT or DEBUG builds of xnu), it is often
2685 * useful or necessary to disable ASLR on a per-process
2686 * basis for unit testing and debugging.
2688 if (px_sa
.psa_flags
& _POSIX_SPAWN_DISABLE_ASLR
)
2689 OSBitOrAtomic(P_DISABLE_ASLR
, &p
->p_flag
);
2690 #endif /* !SECURE_KERNEL */
2693 * Forcibly disallow execution from data pages for the spawned process
2694 * even if it would otherwise be permitted by the architecture default.
2696 if (px_sa
.psa_flags
& _POSIX_SPAWN_ALLOW_DATA_EXEC
)
2697 imgp
->ip_flags
|= IMGPF_ALLOW_DATA_EXEC
;
2701 * Disable ASLR during image activation. This occurs either if the
2702 * _POSIX_SPAWN_DISABLE_ASLR attribute was found above or if
2703 * P_DISABLE_ASLR was inherited from the parent process.
2705 if (p
->p_flag
& P_DISABLE_ASLR
)
2706 imgp
->ip_flags
|= IMGPF_DISABLE_ASLR
;
2709 * Clear transition flag so we won't hang if exec_activate_image() causes
2710 * an automount (and launchd does a proc sysctl to service it).
2712 * <rdar://problem/6848672>, <rdar://problem/5959568>.
2714 if (spawn_no_exec
) {
2715 proc_transend(p
, 0);
2716 proc_transit_set
= 0;
2719 #if MAC_SPAWN /* XXX */
2720 if (uap
->mac_p
!= USER_ADDR_NULL
) {
2721 error
= mac_execve_enter(uap
->mac_p
, imgp
);
2728 * Activate the image
2730 error
= exec_activate_image(imgp
);
2733 /* process completed the exec */
2735 } else if (error
== -1) {
2736 /* Image not claimed by any activator? */
2741 * If we have a spawn attr, and it contains signal related flags,
2742 * the we need to process them in the "context" of the new child
2743 * process, so we have to process it following image activation,
2744 * prior to making the thread runnable in user space. This is
2745 * necessitated by some signal information being per-thread rather
2746 * than per-process, and we don't have the new allocation in hand
2747 * until after the image is activated.
2749 if (!error
&& imgp
->ip_px_sa
!= NULL
) {
2750 thread_t child_thread
= current_thread();
2751 uthread_t child_uthread
= uthread
;
2754 * If we created a new child thread, then the thread and
2755 * uthread are different than the current ones; otherwise,
2756 * we leave them, since we are in the exec case instead.
2758 if (spawn_no_exec
) {
2759 child_thread
= imgp
->ip_new_thread
;
2760 child_uthread
= get_bsdthread_info(child_thread
);
2764 * Mask a list of signals, instead of them being unmasked, if
2765 * they were unmasked in the parent; note that some signals
2768 if (px_sa
.psa_flags
& POSIX_SPAWN_SETSIGMASK
)
2769 child_uthread
->uu_sigmask
= (px_sa
.psa_sigmask
& ~sigcantmask
);
2771 * Default a list of signals instead of ignoring them, if
2772 * they were ignored in the parent. Note that we pass
2773 * spawn_no_exec to setsigvec() to indicate that we called
2774 * fork1() and therefore do not need to call proc_signalstart()
2777 if (px_sa
.psa_flags
& POSIX_SPAWN_SETSIGDEF
) {
2778 vec
.sa_handler
= SIG_DFL
;
2782 for (sig
= 1; sig
< NSIG
; sig
++)
2783 if (px_sa
.psa_sigdefault
& (1 << (sig
-1))) {
2784 error
= setsigvec(p
, child_thread
, sig
, &vec
, spawn_no_exec
);
2789 * Activate the CPU usage monitor, if requested. This is done via a task-wide, per-thread CPU
2790 * usage limit, which will generate a resource exceeded exception if any one thread exceeds the
2793 * Userland gives us interval in seconds, and the kernel SPI expects nanoseconds.
2795 if (px_sa
.psa_cpumonitor_percent
!= 0) {
2797 * Always treat a CPU monitor activation coming from spawn as entitled. Requiring
2798 * an entitlement to configure the monitor a certain way seems silly, since
2799 * whomever is turning it on could just as easily choose not to do so.
2801 error
= proc_set_task_ruse_cpu(p
->task
,
2802 TASK_POLICY_RESOURCE_ATTRIBUTE_NOTIFY_EXC
,
2803 px_sa
.psa_cpumonitor_percent
,
2804 px_sa
.psa_cpumonitor_interval
* NSEC_PER_SEC
,
2812 /* reset delay idle sleep status if set */
2813 if ((p
->p_flag
& P_DELAYIDLESLEEP
) == P_DELAYIDLESLEEP
)
2814 OSBitAndAtomic(~((uint32_t)P_DELAYIDLESLEEP
), &p
->p_flag
);
2815 /* upon successful spawn, re/set the proc control state */
2816 if (imgp
->ip_px_sa
!= NULL
) {
2817 switch (px_sa
.psa_pcontrol
) {
2818 case POSIX_SPAWN_PCONTROL_THROTTLE
:
2819 p
->p_pcaction
= P_PCTHROTTLE
;
2821 case POSIX_SPAWN_PCONTROL_SUSPEND
:
2822 p
->p_pcaction
= P_PCSUSP
;
2824 case POSIX_SPAWN_PCONTROL_KILL
:
2825 p
->p_pcaction
= P_PCKILL
;
2827 case POSIX_SPAWN_PCONTROL_NONE
:
2833 exec_resettextvp(p
, imgp
);
2835 #if CONFIG_MEMORYSTATUS
2836 /* Has jetsam attributes? */
2837 if (imgp
->ip_px_sa
!= NULL
&& (px_sa
.psa_jetsam_flags
& POSIX_SPAWN_JETSAM_SET
)) {
2839 * With 2-level high-water-mark support, POSIX_SPAWN_JETSAM_HIWATER_BACKGROUND is no
2840 * longer relevant, as background limits are described via the inactive limit slots.
2841 * At the kernel layer, the flag is ignored.
2843 * That said, however, if the POSIX_SPAWN_JETSAM_HIWATER_BACKGROUND is passed in,
2844 * we attempt to mimic previous behavior by forcing the BG limit data into the
2845 * inactive/non-fatal mode and force the active slots to hold system_wide/fatal mode.
2846 * The kernel layer will flag this mapping.
2848 if (px_sa
.psa_jetsam_flags
& POSIX_SPAWN_JETSAM_HIWATER_BACKGROUND
) {
2849 memorystatus_update(p
, px_sa
.psa_priority
, 0,
2850 (px_sa
.psa_jetsam_flags
& POSIX_SPAWN_JETSAM_USE_EFFECTIVE_PRIORITY
),
2853 px_sa
.psa_memlimit_inactive
, FALSE
,
2854 (px_sa
.psa_jetsam_flags
& POSIX_SPAWN_JETSAM_HIWATER_BACKGROUND
));
2856 memorystatus_update(p
, px_sa
.psa_priority
, 0,
2857 (px_sa
.psa_jetsam_flags
& POSIX_SPAWN_JETSAM_USE_EFFECTIVE_PRIORITY
),
2859 px_sa
.psa_memlimit_active
,
2860 (px_sa
.psa_jetsam_flags
& POSIX_SPAWN_JETSAM_MEMLIMIT_ACTIVE_FATAL
),
2861 px_sa
.psa_memlimit_inactive
,
2862 (px_sa
.psa_jetsam_flags
& POSIX_SPAWN_JETSAM_MEMLIMIT_INACTIVE_FATAL
),
2863 (px_sa
.psa_jetsam_flags
& POSIX_SPAWN_JETSAM_HIWATER_BACKGROUND
));
2867 #endif /* CONFIG_MEMORYSTATUS */
2871 * If we successfully called fork1(), we always need to do this;
2872 * we identify this case by noting the IMGPF_SPAWN flag. This is
2873 * because we come back from that call with signals blocked in the
2874 * child, and we have to unblock them, but we want to wait until
2875 * after we've performed any spawn actions. This has to happen
2876 * before check_for_signature(), which uses psignal.
2878 if (spawn_no_exec
) {
2879 if (proc_transit_set
)
2880 proc_transend(p
, 0);
2883 * Drop the signal lock on the child which was taken on our
2884 * behalf by forkproc()/cloneproc() to prevent signals being
2885 * received by the child in a partially constructed state.
2887 proc_signalend(p
, 0);
2889 /* flag the 'fork' has occurred */
2890 proc_knote(p
->p_pptr
, NOTE_FORK
| p
->p_pid
);
2891 /* then flag exec has occurred */
2892 /* notify only if it has not failed due to FP Key error */
2893 if ((p
->p_lflag
& P_LTERM_DECRYPTFAIL
) == 0)
2894 proc_knote(p
, NOTE_EXEC
);
2895 } else if (error
== 0) {
2896 /* reset the importance attribute from our previous life */
2897 task_importance_reset(p
->task
);
2902 * We need to initialize the bank context behind the protection of
2903 * the proc_trans lock to prevent a race with exit. We can't do this during
2904 * exec_activate_image because task_bank_init checks entitlements that
2905 * aren't loaded until subsequent calls (including exec_resettextvp).
2907 error
= proc_transstart(p
, 0, 0);
2910 task_bank_init(p
->task
);
2911 proc_transend(p
, 0);
2917 * Apply the spawnattr policy, apptype (which primes the task for importance donation),
2918 * and bind any portwatch ports to the new task.
2919 * This must be done after the exec so that the child's thread is ready,
2920 * and after the in transit state has been released, because priority is
2921 * dropped here so we need to be prepared for a potentially long preemption interval
2923 * TODO: Consider splitting this up into separate phases
2925 if (error
== 0 && imgp
->ip_px_sa
!= NULL
) {
2926 struct _posix_spawnattr
*psa
= (struct _posix_spawnattr
*) imgp
->ip_px_sa
;
2928 exec_handle_spawnattr_policy(p
, psa
->psa_apptype
, psa
->psa_qos_clamp
, psa
->psa_darwin_role
,
2929 portwatch_ports
, portwatch_count
);
2932 /* Apply the main thread qos */
2934 thread_t main_thread
= (imgp
->ip_new_thread
!= NULL
) ? imgp
->ip_new_thread
: current_thread();
2936 task_set_main_thread_qos(p
->task
, main_thread
);
2940 * Release any ports we kept around for binding to the new task
2941 * We need to release the rights even if the posix_spawn has failed.
2943 if (portwatch_ports
!= NULL
) {
2944 for (int i
= 0; i
< portwatch_count
; i
++) {
2945 ipc_port_t port
= NULL
;
2946 if ((port
= portwatch_ports
[i
]) != NULL
) {
2947 ipc_port_release_send(port
);
2950 FREE(portwatch_ports
, M_TEMP
);
2951 portwatch_ports
= NULL
;
2952 portwatch_count
= 0;
2956 * We have to delay operations which might throw a signal until after
2957 * the signals have been unblocked; however, we want that to happen
2958 * after exec_resettextvp() so that the textvp is correct when they
2962 error
= check_for_signature(p
, imgp
);
2965 * Pay for our earlier safety; deliver the delayed signals from
2966 * the incomplete spawn process now that it's complete.
2968 if (imgp
!= NULL
&& spawn_no_exec
&& (p
->p_lflag
& P_LTRACED
)) {
2969 psignal_vfork(p
, p
->task
, imgp
->ip_new_thread
, SIGTRAP
);
2972 if (error
== 0 && !spawn_no_exec
)
2973 KDBG(BSDDBG_CODE(DBG_BSD_PROC
,BSD_PROC_EXEC
),
2980 vnode_put(imgp
->ip_vp
);
2981 if (imgp
->ip_scriptvp
)
2982 vnode_put(imgp
->ip_scriptvp
);
2983 if (imgp
->ip_strings
)
2984 execargs_free(imgp
);
2985 if (imgp
->ip_px_sfa
!= NULL
)
2986 FREE(imgp
->ip_px_sfa
, M_TEMP
);
2987 if (imgp
->ip_px_spa
!= NULL
)
2988 FREE(imgp
->ip_px_spa
, M_TEMP
);
2990 if (imgp
->ip_px_persona
!= NULL
)
2991 FREE(imgp
->ip_px_persona
, M_TEMP
);
2994 if (imgp
->ip_px_smpx
!= NULL
)
2995 spawn_free_macpolicyinfo(imgp
->ip_px_smpx
);
2996 if (imgp
->ip_execlabelp
)
2997 mac_cred_label_free(imgp
->ip_execlabelp
);
2998 if (imgp
->ip_scriptlabelp
)
2999 mac_vnode_label_free(imgp
->ip_scriptlabelp
);
3000 if (imgp
->ip_cs_error
!= OS_REASON_NULL
) {
3001 os_reason_free(imgp
->ip_cs_error
);
3002 imgp
->ip_cs_error
= OS_REASON_NULL
;
3008 if (spawn_no_exec
) {
3010 * In the original DTrace reference implementation,
3011 * posix_spawn() was a libc routine that just
3012 * did vfork(2) then exec(2). Thus the proc::: probes
3013 * are very fork/exec oriented. The details of this
3014 * in-kernel implementation of posix_spawn() is different
3015 * (while producing the same process-observable effects)
3016 * particularly w.r.t. errors, and which thread/process
3017 * is constructing what on behalf of whom.
3020 DTRACE_PROC1(spawn__failure
, int, error
);
3022 DTRACE_PROC(spawn__success
);
3024 * Some DTrace scripts, e.g. newproc.d in
3025 * /usr/bin, rely on the the 'exec-success'
3026 * probe being fired in the child after the
3027 * new process image has been constructed
3028 * in order to determine the associated pid.
3030 * So, even though the parent built the image
3031 * here, for compatibility, mark the new thread
3032 * so 'exec-success' fires on it as it leaves
3035 dtrace_thread_didexec(imgp
->ip_new_thread
);
3039 DTRACE_PROC1(exec__failure
, int, error
);
3041 DTRACE_PROC(exec__success
);
3045 if ((dtrace_proc_waitfor_hook
= dtrace_proc_waitfor_exec_ptr
) != NULL
)
3046 (*dtrace_proc_waitfor_hook
)(p
);
3049 /* Return to both the parent and the child? */
3050 if (imgp
!= NULL
&& spawn_no_exec
) {
3052 * If the parent wants the pid, copy it out
3054 if (pid
!= USER_ADDR_NULL
)
3055 (void)suword(pid
, p
->p_pid
);
3059 * If we had an error, perform an internal reap ; this is
3060 * entirely safe, as we have a real process backing us.
3064 p
->p_listflag
|= P_LIST_DEADPARENT
;
3067 /* make sure no one else has killed it off... */
3068 if (p
->p_stat
!= SZOMB
&& p
->exit_thread
== NULL
) {
3069 p
->exit_thread
= current_thread();
3071 exit1(p
, 1, (int *)NULL
);
3072 proc_clear_return_wait(p
, imgp
->ip_new_thread
);
3073 if (exec_done
== FALSE
) {
3074 task_deallocate(get_threadtask(imgp
->ip_new_thread
));
3075 thread_deallocate(imgp
->ip_new_thread
);
3078 /* someone is doing it for us; just skip it */
3080 proc_clear_return_wait(p
, imgp
->ip_new_thread
);
3085 * Return to the child
3087 * Note: the image activator earlier dropped the
3088 * task/thread references to the newly spawned
3089 * process; this is OK, since we still have suspended
3090 * queue references on them, so we should be fine
3091 * with the delayed resume of the thread here.
3093 proc_clear_return_wait(p
, imgp
->ip_new_thread
);
3107 * Parameters: uap->fname File name to exec
3108 * uap->argp Argument list
3109 * uap->envp Environment list
3111 * Returns: 0 Success
3112 * __mac_execve:EINVAL Invalid argument
3113 * __mac_execve:ENOTSUP Invalid argument
3114 * __mac_execve:EACCES Permission denied
3115 * __mac_execve:EINTR Interrupted function
3116 * __mac_execve:ENOMEM Not enough space
3117 * __mac_execve:EFAULT Bad address
3118 * __mac_execve:ENAMETOOLONG Filename too long
3119 * __mac_execve:ENOEXEC Executable file format error
3120 * __mac_execve:ETXTBSY Text file busy [misuse of error code]
3123 * TODO: Dynamic linker header address on stack is copied via suword()
3127 execve(proc_t p
, struct execve_args
*uap
, int32_t *retval
)
3129 struct __mac_execve_args muap
;
3132 memoryshot(VM_EXECVE
, DBG_FUNC_NONE
);
3134 muap
.fname
= uap
->fname
;
3135 muap
.argp
= uap
->argp
;
3136 muap
.envp
= uap
->envp
;
3137 muap
.mac_p
= USER_ADDR_NULL
;
3138 err
= __mac_execve(p
, &muap
, retval
);
3146 * Parameters: uap->fname File name to exec
3147 * uap->argp Argument list
3148 * uap->envp Environment list
3149 * uap->mac_p MAC label supplied by caller
3151 * Returns: 0 Success
3152 * EINVAL Invalid argument
3153 * ENOTSUP Not supported
3154 * ENOEXEC Executable file format error
3155 * exec_activate_image:EINVAL Invalid argument
3156 * exec_activate_image:EACCES Permission denied
3157 * exec_activate_image:EINTR Interrupted function
3158 * exec_activate_image:ENOMEM Not enough space
3159 * exec_activate_image:EFAULT Bad address
3160 * exec_activate_image:ENAMETOOLONG Filename too long
3161 * exec_activate_image:ENOEXEC Executable file format error
3162 * exec_activate_image:ETXTBSY Text file busy [misuse of error code]
3163 * exec_activate_image:EBADEXEC The executable is corrupt/unknown
3164 * exec_activate_image:???
3165 * mac_execve_enter:???
3167 * TODO: Dynamic linker header address on stack is copied via suword()
3170 __mac_execve(proc_t p
, struct __mac_execve_args
*uap
, int32_t *retval
)
3173 struct image_params
*imgp
;
3174 struct vnode_attr
*vap
;
3175 struct vnode_attr
*origvap
;
3177 int is_64
= IS_64BIT_PROCESS(p
);
3178 struct vfs_context context
;
3179 struct uthread
*uthread
;
3181 context
.vc_thread
= current_thread();
3182 context
.vc_ucred
= kauth_cred_proc_ref(p
); /* XXX must NOT be kauth_cred_get() */
3184 /* Allocate a big chunk for locals instead of using stack since these
3185 * structures a pretty big.
3187 MALLOC(bufp
, char *, (sizeof(*imgp
) + sizeof(*vap
) + sizeof(*origvap
)), M_TEMP
, M_WAITOK
| M_ZERO
);
3188 imgp
= (struct image_params
*) bufp
;
3191 goto exit_with_error
;
3193 vap
= (struct vnode_attr
*) (bufp
+ sizeof(*imgp
));
3194 origvap
= (struct vnode_attr
*) (bufp
+ sizeof(*imgp
) + sizeof(*vap
));
3196 /* Initialize the common data in the image_params structure */
3197 imgp
->ip_user_fname
= uap
->fname
;
3198 imgp
->ip_user_argv
= uap
->argp
;
3199 imgp
->ip_user_envv
= uap
->envp
;
3200 imgp
->ip_vattr
= vap
;
3201 imgp
->ip_origvattr
= origvap
;
3202 imgp
->ip_vfs_context
= &context
;
3203 imgp
->ip_flags
= (is_64
? IMGPF_WAS_64BIT
: IMGPF_NONE
) | ((p
->p_flag
& P_DISABLE_ASLR
) ? IMGPF_DISABLE_ASLR
: IMGPF_NONE
);
3204 imgp
->ip_seg
= (is_64
? UIO_USERSPACE64
: UIO_USERSPACE32
);
3205 imgp
->ip_mac_return
= 0;
3206 imgp
->ip_cs_error
= OS_REASON_NULL
;
3208 uthread
= get_bsdthread_info(current_thread());
3209 if (uthread
->uu_flag
& UT_VFORK
) {
3210 imgp
->ip_flags
|= IMGPF_VFORK_EXEC
;
3214 if (uap
->mac_p
!= USER_ADDR_NULL
) {
3215 error
= mac_execve_enter(uap
->mac_p
, imgp
);
3217 kauth_cred_unref(&context
.vc_ucred
);
3218 goto exit_with_error
;
3223 error
= exec_activate_image(imgp
);
3225 kauth_cred_unref(&context
.vc_ucred
);
3227 /* Image not claimed by any activator? */
3232 exec_resettextvp(p
, imgp
);
3233 error
= check_for_signature(p
, imgp
);
3235 if (imgp
->ip_vp
!= NULLVP
)
3236 vnode_put(imgp
->ip_vp
);
3237 if (imgp
->ip_scriptvp
!= NULLVP
)
3238 vnode_put(imgp
->ip_scriptvp
);
3239 if (imgp
->ip_strings
)
3240 execargs_free(imgp
);
3242 if (imgp
->ip_execlabelp
)
3243 mac_cred_label_free(imgp
->ip_execlabelp
);
3244 if (imgp
->ip_scriptlabelp
)
3245 mac_vnode_label_free(imgp
->ip_scriptlabelp
);
3247 if (imgp
->ip_cs_error
!= OS_REASON_NULL
) {
3248 os_reason_free(imgp
->ip_cs_error
);
3249 imgp
->ip_cs_error
= OS_REASON_NULL
;
3254 * We need to initialize the bank context behind the protection of
3255 * the proc_trans lock to prevent a race with exit. We can't do this during
3256 * exec_activate_image because task_bank_init checks entitlements that
3257 * aren't loaded until subsequent calls (including exec_resettextvp).
3259 error
= proc_transstart(p
, 0, 0);
3262 task_bank_init(p
->task
);
3263 proc_transend(p
, 0);
3268 /* Sever any extant thread affinity */
3269 thread_affinity_exec(current_thread());
3271 thread_t main_thread
= (imgp
->ip_new_thread
!= NULL
) ? imgp
->ip_new_thread
: current_thread();
3273 task_set_main_thread_qos(p
->task
, main_thread
);
3275 /* reset task importance */
3276 task_importance_reset(p
->task
);
3278 DTRACE_PROC(exec__success
);
3281 if ((dtrace_proc_waitfor_hook
= dtrace_proc_waitfor_exec_ptr
) != NULL
)
3282 (*dtrace_proc_waitfor_hook
)(p
);
3285 if (imgp
->ip_flags
& IMGPF_VFORK_EXEC
) {
3286 vfork_return(p
, retval
, p
->p_pid
);
3287 proc_clear_return_wait(p
, imgp
->ip_new_thread
);
3290 DTRACE_PROC1(exec__failure
, int, error
);
3305 * Description: Copy a pointer in from user space to a user_addr_t in kernel
3306 * space, based on 32/64 bitness of the user space
3308 * Parameters: froma User space address
3309 * toptr Address of kernel space user_addr_t
3310 * ptr_size 4/8, based on 'froma' address space
3312 * Returns: 0 Success
3313 * EFAULT Bad 'froma'
3316 * *ptr_size Modified
3319 copyinptr(user_addr_t froma
, user_addr_t
*toptr
, int ptr_size
)
3323 if (ptr_size
== 4) {
3324 /* 64 bit value containing 32 bit address */
3327 error
= copyin(froma
, &i
, 4);
3328 *toptr
= CAST_USER_ADDR_T(i
); /* SAFE */
3330 error
= copyin(froma
, toptr
, 8);
3339 * Description: Copy a pointer out from a user_addr_t in kernel space to
3340 * user space, based on 32/64 bitness of the user space
3342 * Parameters: ua User space address to copy to
3343 * ptr Address of kernel space user_addr_t
3344 * ptr_size 4/8, based on 'ua' address space
3346 * Returns: 0 Success
3351 copyoutptr(user_addr_t ua
, user_addr_t ptr
, int ptr_size
)
3355 if (ptr_size
== 4) {
3356 /* 64 bit value containing 32 bit address */
3357 unsigned int i
= CAST_DOWN_EXPLICIT(unsigned int,ua
); /* SAFE */
3359 error
= copyout(&i
, ptr
, 4);
3361 error
= copyout(&ua
, ptr
, 8);
3368 * exec_copyout_strings
3370 * Copy out the strings segment to user space. The strings segment is put
3371 * on a preinitialized stack frame.
3373 * Parameters: struct image_params * the image parameter block
3374 * int * a pointer to the stack offset variable
3376 * Returns: 0 Success
3380 * (*stackp) The stack offset, modified
3382 * Note: The strings segment layout is backward, from the beginning
3383 * of the top of the stack to consume the minimal amount of
3384 * space possible; the returned stack pointer points to the
3385 * end of the area consumed (stacks grow downward).
3387 * argc is an int; arg[i] are pointers; env[i] are pointers;
3388 * the 0's are (void *)NULL's
3390 * The stack frame layout is:
3392 * +-------------+ <- p->user_stack
3433 * sp-> +-------------+
3435 * Although technically a part of the STRING AREA, we treat the PATH AREA as
3436 * a separate entity. This allows us to align the beginning of the PATH AREA
3437 * to a pointer boundary so that the exec_path, env[i], and argv[i] pointers
3438 * which preceed it on the stack are properly aligned.
3442 exec_copyout_strings(struct image_params
*imgp
, user_addr_t
*stackp
)
3444 proc_t p
= vfs_context_proc(imgp
->ip_vfs_context
);
3445 int ptr_size
= (imgp
->ip_flags
& IMGPF_IS_64BIT
) ? 8 : 4;
3447 void *ptr_buffer_start
, *ptr_buffer
;
3450 user_addr_t string_area
; /* *argv[], *env[] */
3451 user_addr_t ptr_area
; /* argv[], env[], applev[] */
3452 user_addr_t argc_area
; /* argc */
3457 struct copyout_desc
{
3461 user_addr_t
*dtrace_cookie
;
3463 boolean_t null_term
;
3466 .start_string
= imgp
->ip_startargv
,
3467 .count
= imgp
->ip_argc
,
3469 .dtrace_cookie
= &p
->p_dtrace_argv
,
3474 .start_string
= imgp
->ip_endargv
,
3475 .count
= imgp
->ip_envc
,
3477 .dtrace_cookie
= &p
->p_dtrace_envp
,
3482 .start_string
= imgp
->ip_strings
,
3485 .dtrace_cookie
= NULL
,
3490 .start_string
= imgp
->ip_endenvv
,
3491 .count
= imgp
->ip_applec
- 1, /* exec_path handled above */
3493 .dtrace_cookie
= NULL
,
3502 * All previous contributors to the string area
3503 * should have aligned their sub-area
3505 if (imgp
->ip_strspace
% ptr_size
!= 0) {
3510 /* Grow the stack down for the strings we've been building up */
3511 string_size
= imgp
->ip_strendp
- imgp
->ip_strings
;
3512 stack
-= string_size
;
3513 string_area
= stack
;
3516 * Need room for one pointer for each string, plus
3517 * one for the NULLs terminating the argv, envv, and apple areas.
3519 ptr_area_size
= (imgp
->ip_argc
+ imgp
->ip_envc
+ imgp
->ip_applec
+ 3) *
3521 stack
-= ptr_area_size
;
3524 /* We'll construct all the pointer arrays in our string buffer,
3525 * which we already know is aligned properly, and ip_argspace
3526 * was used to verify we have enough space.
3528 ptr_buffer_start
= ptr_buffer
= (void *)imgp
->ip_strendp
;
3531 * Need room for pointer-aligned argc slot.
3537 * Record the size of the arguments area so that sysctl_procargs()
3538 * can return the argument area without having to parse the arguments.
3541 p
->p_argc
= imgp
->ip_argc
;
3542 p
->p_argslen
= (int)(*stackp
- string_area
);
3545 /* Return the initial stack address: the location of argc */
3549 * Copy out the entire strings area.
3551 error
= copyout(imgp
->ip_strings
, string_area
,
3556 for (i
= 0; i
< sizeof(descriptors
)/sizeof(descriptors
[0]); i
++) {
3557 char *cur_string
= descriptors
[i
].start_string
;
3561 if (descriptors
[i
].dtrace_cookie
) {
3563 *descriptors
[i
].dtrace_cookie
= ptr_area
+ ((uintptr_t)ptr_buffer
- (uintptr_t)ptr_buffer_start
); /* dtrace convenience */
3566 #endif /* CONFIG_DTRACE */
3569 * For each segment (argv, envv, applev), copy as many pointers as requested
3570 * to our pointer buffer.
3572 for (j
= 0; j
< descriptors
[i
].count
; j
++) {
3573 user_addr_t cur_address
= string_area
+ (cur_string
- imgp
->ip_strings
);
3575 /* Copy out the pointer to the current string. Alignment has been verified */
3576 if (ptr_size
== 8) {
3577 *(uint64_t *)ptr_buffer
= (uint64_t)cur_address
;
3579 *(uint32_t *)ptr_buffer
= (uint32_t)cur_address
;
3582 ptr_buffer
= (void *)((uintptr_t)ptr_buffer
+ ptr_size
);
3583 cur_string
+= strlen(cur_string
) + 1; /* Only a NUL between strings in the same area */
3586 if (descriptors
[i
].null_term
) {
3587 if (ptr_size
== 8) {
3588 *(uint64_t *)ptr_buffer
= 0ULL;
3590 *(uint32_t *)ptr_buffer
= 0;
3593 ptr_buffer
= (void *)((uintptr_t)ptr_buffer
+ ptr_size
);
3598 * Copy out all our pointer arrays in bulk.
3600 error
= copyout(ptr_buffer_start
, ptr_area
,
3605 /* argc (int32, stored in a ptr_size area) */
3606 error
= copyoutptr((user_addr_t
)imgp
->ip_argc
, argc_area
, ptr_size
);
3616 * exec_extract_strings
3618 * Copy arguments and environment from user space into work area; we may
3619 * have already copied some early arguments into the work area, and if
3620 * so, any arguments opied in are appended to those already there.
3621 * This function is the primary manipulator of ip_argspace, since
3622 * these are the arguments the client of execve(2) knows about. After
3623 * each argv[]/envv[] string is copied, we charge the string length
3624 * and argv[]/envv[] pointer slot to ip_argspace, so that we can
3625 * full preflight the arg list size.
3627 * Parameters: struct image_params * the image parameter block
3629 * Returns: 0 Success
3633 * (imgp->ip_argc) Count of arguments, updated
3634 * (imgp->ip_envc) Count of environment strings, updated
3635 * (imgp->ip_argspace) Count of remaining of NCARGS
3636 * (imgp->ip_interp_buffer) Interpreter and args (mutated in place)
3639 * Note: The argument and environment vectors are user space pointers
3640 * to arrays of user space pointers.
3643 exec_extract_strings(struct image_params
*imgp
)
3646 int ptr_size
= (imgp
->ip_flags
& IMGPF_WAS_64BIT
) ? 8 : 4;
3647 int new_ptr_size
= (imgp
->ip_flags
& IMGPF_IS_64BIT
) ? 8 : 4;
3648 user_addr_t argv
= imgp
->ip_user_argv
;
3649 user_addr_t envv
= imgp
->ip_user_envv
;
3652 * Adjust space reserved for the path name by however much padding it
3653 * needs. Doing this here since we didn't know if this would be a 32-
3654 * or 64-bit process back in exec_save_path.
3656 while (imgp
->ip_strspace
% new_ptr_size
!= 0) {
3657 *imgp
->ip_strendp
++ = '\0';
3658 imgp
->ip_strspace
--;
3659 /* imgp->ip_argspace--; not counted towards exec args total */
3663 * From now on, we start attributing string space to ip_argspace
3665 imgp
->ip_startargv
= imgp
->ip_strendp
;
3668 if((imgp
->ip_flags
& IMGPF_INTERPRET
) != 0) {
3670 char *argstart
, *ch
;
3672 /* First, the arguments in the "#!" string are tokenized and extracted. */
3673 argstart
= imgp
->ip_interp_buffer
;
3676 while (*ch
&& !IS_WHITESPACE(*ch
)) {
3681 /* last argument, no need to NUL-terminate */
3682 error
= exec_add_user_string(imgp
, CAST_USER_ADDR_T(argstart
), UIO_SYSSPACE
, TRUE
);
3687 error
= exec_add_user_string(imgp
, CAST_USER_ADDR_T(argstart
), UIO_SYSSPACE
, TRUE
);
3690 * Find the next string. We know spaces at the end of the string have already
3694 while (IS_WHITESPACE(*argstart
)) {
3699 /* Error-check, regardless of whether this is the last interpreter arg or not */
3702 if (imgp
->ip_argspace
< new_ptr_size
) {
3706 imgp
->ip_argspace
-= new_ptr_size
; /* to hold argv[] entry */
3712 * If we are running an interpreter, replace the av[0] that was
3713 * passed to execve() with the path name that was
3714 * passed to execve() for interpreters which do not use the PATH
3715 * to locate their script arguments.
3717 error
= copyinptr(argv
, &arg
, ptr_size
);
3721 argv
+= ptr_size
; /* consume without using */
3725 if (imgp
->ip_interp_sugid_fd
!= -1) {
3726 char temp
[19]; /* "/dev/fd/" + 10 digits + NUL */
3727 snprintf(temp
, sizeof(temp
), "/dev/fd/%d", imgp
->ip_interp_sugid_fd
);
3728 error
= exec_add_user_string(imgp
, CAST_USER_ADDR_T(temp
), UIO_SYSSPACE
, TRUE
);
3730 error
= exec_add_user_string(imgp
, imgp
->ip_user_fname
, imgp
->ip_seg
, TRUE
);
3735 if (imgp
->ip_argspace
< new_ptr_size
) {
3739 imgp
->ip_argspace
-= new_ptr_size
; /* to hold argv[] entry */
3743 while (argv
!= 0LL) {
3746 error
= copyinptr(argv
, &arg
, ptr_size
);
3759 error
= exec_add_user_string(imgp
, arg
, imgp
->ip_seg
, TRUE
);
3762 if (imgp
->ip_argspace
< new_ptr_size
) {
3766 imgp
->ip_argspace
-= new_ptr_size
; /* to hold argv[] entry */
3770 /* Save space for argv[] NULL terminator */
3771 if (imgp
->ip_argspace
< new_ptr_size
) {
3775 imgp
->ip_argspace
-= new_ptr_size
;
3777 /* Note where the args ends and env begins. */
3778 imgp
->ip_endargv
= imgp
->ip_strendp
;
3781 /* Now, get the environment */
3782 while (envv
!= 0LL) {
3785 error
= copyinptr(envv
, &env
, ptr_size
);
3796 error
= exec_add_user_string(imgp
, env
, imgp
->ip_seg
, TRUE
);
3799 if (imgp
->ip_argspace
< new_ptr_size
) {
3803 imgp
->ip_argspace
-= new_ptr_size
; /* to hold envv[] entry */
3807 /* Save space for envv[] NULL terminator */
3808 if (imgp
->ip_argspace
< new_ptr_size
) {
3812 imgp
->ip_argspace
-= new_ptr_size
;
3814 /* Align the tail of the combined argv+envv area */
3815 while (imgp
->ip_strspace
% new_ptr_size
!= 0) {
3816 if (imgp
->ip_argspace
< 1) {
3820 *imgp
->ip_strendp
++ = '\0';
3821 imgp
->ip_strspace
--;
3822 imgp
->ip_argspace
--;
3825 /* Note where the envv ends and applev begins. */
3826 imgp
->ip_endenvv
= imgp
->ip_strendp
;
3829 * From now on, we are no longer charging argument
3830 * space to ip_argspace.
3838 * Libc has an 8-element array set up for stack guard values. It only fills
3839 * in one of those entries, and both gcc and llvm seem to use only a single
3840 * 8-byte guard. Until somebody needs more than an 8-byte guard value, don't
3841 * do the work to construct them.
3843 #define GUARD_VALUES 1
3844 #define GUARD_KEY "stack_guard="
3847 * System malloc needs some entropy when it is initialized.
3849 #define ENTROPY_VALUES 2
3850 #define ENTROPY_KEY "malloc_entropy="
3853 * System malloc engages nanozone for UIAPP.
3855 #define NANO_ENGAGE_KEY "MallocNanoZone=1"
3857 #define PFZ_KEY "pfz="
3858 extern user32_addr_t commpage_text32_location
;
3859 extern user64_addr_t commpage_text64_location
;
3861 #define MAIN_STACK_VALUES 4
3862 #define MAIN_STACK_KEY "main_stack="
3864 #define HEX_STR_LEN 18 // 64-bit hex value "0x0123456701234567"
3867 exec_add_entropy_key(struct image_params
*imgp
,
3872 const int limit
= 8;
3873 uint64_t entropy
[limit
];
3874 char str
[strlen(key
) + (HEX_STR_LEN
+ 1) * limit
+ 1];
3875 if (values
> limit
) {
3879 read_random(entropy
, sizeof(entropy
[0]) * values
);
3882 entropy
[0] &= ~(0xffull
<< 8);
3885 int len
= snprintf(str
, sizeof(str
), "%s0x%llx", key
, entropy
[0]);
3886 int remaining
= sizeof(str
) - len
;
3887 for (int i
= 1; i
< values
&& remaining
> 0; ++i
) {
3888 int start
= sizeof(str
) - remaining
;
3889 len
= snprintf(&str
[start
], remaining
, ",0x%llx", entropy
[i
]);
3893 return exec_add_user_string(imgp
, CAST_USER_ADDR_T(str
), UIO_SYSSPACE
, FALSE
);
3897 * Build up the contents of the apple[] string vector
3900 exec_add_apple_strings(struct image_params
*imgp
,
3901 const load_result_t
*load_result
)
3904 int img_ptr_size
= (imgp
->ip_flags
& IMGPF_IS_64BIT
) ? 8 : 4;
3906 /* exec_save_path stored the first string */
3907 imgp
->ip_applec
= 1;
3909 /* adding the pfz string */
3911 char pfz_string
[strlen(PFZ_KEY
) + HEX_STR_LEN
+ 1];
3913 if (img_ptr_size
== 8) {
3914 snprintf(pfz_string
, sizeof(pfz_string
), PFZ_KEY
"0x%llx", commpage_text64_location
);
3916 snprintf(pfz_string
, sizeof(pfz_string
), PFZ_KEY
"0x%x", commpage_text32_location
);
3918 error
= exec_add_user_string(imgp
, CAST_USER_ADDR_T(pfz_string
), UIO_SYSSPACE
, FALSE
);
3925 /* adding the NANO_ENGAGE_KEY key */
3926 if (imgp
->ip_px_sa
) {
3927 int proc_flags
= (((struct _posix_spawnattr
*) imgp
->ip_px_sa
)->psa_flags
);
3929 if ((proc_flags
& _POSIX_SPAWN_NANO_ALLOCATOR
) == _POSIX_SPAWN_NANO_ALLOCATOR
) {
3930 const char *nano_string
= NANO_ENGAGE_KEY
;
3931 error
= exec_add_user_string(imgp
, CAST_USER_ADDR_T(nano_string
), UIO_SYSSPACE
, FALSE
);
3940 * Supply libc with a collection of random values to use when
3941 * implementing -fstack-protector.
3943 * (The first random string always contains an embedded NUL so that
3944 * __stack_chk_guard also protects against C string vulnerabilities)
3946 error
= exec_add_entropy_key(imgp
, GUARD_KEY
, GUARD_VALUES
, TRUE
);
3953 * Supply libc with entropy for system malloc.
3955 error
= exec_add_entropy_key(imgp
, ENTROPY_KEY
, ENTROPY_VALUES
, FALSE
);
3962 * Add MAIN_STACK_KEY: Supplies the address and size of the main thread's
3963 * stack if it was allocated by the kernel.
3965 * The guard page is not included in this stack size as libpthread
3966 * expects to add it back in after receiving this value.
3968 if (load_result
->unixproc
) {
3969 char stack_string
[strlen(MAIN_STACK_KEY
) + (HEX_STR_LEN
+ 1) * MAIN_STACK_VALUES
+ 1];
3970 snprintf(stack_string
, sizeof(stack_string
),
3971 MAIN_STACK_KEY
"0x%llx,0x%llx,0x%llx,0x%llx",
3972 (uint64_t)load_result
->user_stack
,
3973 (uint64_t)load_result
->user_stack_size
,
3974 (uint64_t)load_result
->user_stack_alloc
,
3975 (uint64_t)load_result
->user_stack_alloc_size
);
3976 error
= exec_add_user_string(imgp
, CAST_USER_ADDR_T(stack_string
), UIO_SYSSPACE
, FALSE
);
3983 /* Align the tail of the combined applev area */
3984 while (imgp
->ip_strspace
% img_ptr_size
!= 0) {
3985 *imgp
->ip_strendp
++ = '\0';
3986 imgp
->ip_strspace
--;
3993 #define unix_stack_size(p) (p->p_rlimit[RLIMIT_STACK].rlim_cur)
3996 * exec_check_permissions
3998 * Description: Verify that the file that is being attempted to be executed
3999 * is in fact allowed to be executed based on it POSIX file
4000 * permissions and other access control criteria
4002 * Parameters: struct image_params * the image parameter block
4004 * Returns: 0 Success
4005 * EACCES Permission denied
4006 * ENOEXEC Executable file format error
4007 * ETXTBSY Text file busy [misuse of error code]
4009 * vnode_authorize:???
4012 exec_check_permissions(struct image_params
*imgp
)
4014 struct vnode
*vp
= imgp
->ip_vp
;
4015 struct vnode_attr
*vap
= imgp
->ip_vattr
;
4016 proc_t p
= vfs_context_proc(imgp
->ip_vfs_context
);
4018 kauth_action_t action
;
4020 /* Only allow execution of regular files */
4021 if (!vnode_isreg(vp
))
4024 /* Get the file attributes that we will be using here and elsewhere */
4026 VATTR_WANTED(vap
, va_uid
);
4027 VATTR_WANTED(vap
, va_gid
);
4028 VATTR_WANTED(vap
, va_mode
);
4029 VATTR_WANTED(vap
, va_fsid
);
4030 VATTR_WANTED(vap
, va_fileid
);
4031 VATTR_WANTED(vap
, va_data_size
);
4032 if ((error
= vnode_getattr(vp
, vap
, imgp
->ip_vfs_context
)) != 0)
4036 * Ensure that at least one execute bit is on - otherwise root
4037 * will always succeed, and we don't want to happen unless the
4038 * file really is executable.
4040 if (!vfs_authopaque(vnode_mount(vp
)) && ((vap
->va_mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
)) == 0))
4043 /* Disallow zero length files */
4044 if (vap
->va_data_size
== 0)
4047 imgp
->ip_arch_offset
= (user_size_t
)0;
4048 imgp
->ip_arch_size
= vap
->va_data_size
;
4050 /* Disable setuid-ness for traced programs or if MNT_NOSUID */
4051 if ((vp
->v_mount
->mnt_flag
& MNT_NOSUID
) || (p
->p_lflag
& P_LTRACED
))
4052 vap
->va_mode
&= ~(VSUID
| VSGID
);
4055 * Disable _POSIX_SPAWN_ALLOW_DATA_EXEC and _POSIX_SPAWN_DISABLE_ASLR
4056 * flags for setuid/setgid binaries.
4058 if (vap
->va_mode
& (VSUID
| VSGID
))
4059 imgp
->ip_flags
&= ~(IMGPF_ALLOW_DATA_EXEC
| IMGPF_DISABLE_ASLR
);
4062 error
= mac_vnode_check_exec(imgp
->ip_vfs_context
, vp
, imgp
);
4067 /* Check for execute permission */
4068 action
= KAUTH_VNODE_EXECUTE
;
4069 /* Traced images must also be readable */
4070 if (p
->p_lflag
& P_LTRACED
)
4071 action
|= KAUTH_VNODE_READ_DATA
;
4072 if ((error
= vnode_authorize(vp
, NULL
, action
, imgp
->ip_vfs_context
)) != 0)
4076 /* Don't let it run if anyone had it open for writing */
4078 if (vp
->v_writecount
) {
4079 panic("going to return ETXTBSY %x", vp
);
4087 /* XXX May want to indicate to underlying FS that vnode is open */
4096 * Initially clear the P_SUGID in the process flags; if an SUGID process is
4097 * exec'ing a non-SUGID image, then this is the point of no return.
4099 * If the image being activated is SUGID, then replace the credential with a
4100 * copy, disable tracing (unless the tracing process is root), reset the
4101 * mach task port to revoke it, set the P_SUGID bit,
4103 * If the saved user and group ID will be changing, then make sure it happens
4104 * to a new credential, rather than a shared one.
4106 * Set the security token (this is probably obsolete, given that the token
4107 * should not technically be separate from the credential itself).
4109 * Parameters: struct image_params * the image parameter block
4111 * Returns: void No failure indication
4114 * <process credential> Potentially modified/replaced
4115 * <task port> Potentially revoked
4116 * <process flags> P_SUGID bit potentially modified
4117 * <security token> Potentially modified
4120 exec_handle_sugid(struct image_params
*imgp
)
4122 proc_t p
= vfs_context_proc(imgp
->ip_vfs_context
);
4123 kauth_cred_t cred
= vfs_context_ucred(imgp
->ip_vfs_context
);
4124 kauth_cred_t my_cred
, my_new_cred
;
4126 int leave_sugid_clear
= 0;
4127 int mac_reset_ipc
= 0;
4130 int mac_transition
, disjoint_cred
= 0;
4131 int label_update_return
= 0;
4134 * Determine whether a call to update the MAC label will result in the
4135 * credential changing.
4137 * Note: MAC policies which do not actually end up modifying
4138 * the label subsequently are strongly encouraged to
4139 * return 0 for this check, since a non-zero answer will
4140 * slow down the exec fast path for normal binaries.
4142 mac_transition
= mac_cred_check_label_update_execve(
4143 imgp
->ip_vfs_context
,
4145 imgp
->ip_arch_offset
,
4147 imgp
->ip_scriptlabelp
,
4148 imgp
->ip_execlabelp
,
4153 OSBitAndAtomic(~((uint32_t)P_SUGID
), &p
->p_flag
);
4156 * Order of the following is important; group checks must go last,
4157 * as we use the success of the 'ismember' check combined with the
4158 * failure of the explicit match to indicate that we will be setting
4159 * the egid of the process even though the new process did not
4160 * require VSUID/VSGID bits in order for it to set the new group as
4163 * Note: Technically, by this we are implying a call to
4164 * setegid() in the new process, rather than implying
4165 * it used its VSGID bit to set the effective group,
4166 * even though there is no code in that process to make
4169 if (((imgp
->ip_origvattr
->va_mode
& VSUID
) != 0 &&
4170 kauth_cred_getuid(cred
) != imgp
->ip_origvattr
->va_uid
) ||
4171 ((imgp
->ip_origvattr
->va_mode
& VSGID
) != 0 &&
4172 ((kauth_cred_ismember_gid(cred
, imgp
->ip_origvattr
->va_gid
, &leave_sugid_clear
) || !leave_sugid_clear
) ||
4173 (kauth_cred_getgid(cred
) != imgp
->ip_origvattr
->va_gid
)))) {
4176 /* label for MAC transition and neither VSUID nor VSGID */
4177 handle_mac_transition
:
4181 * Replace the credential with a copy of itself if euid or
4184 * Note: setuid binaries will automatically opt out of
4185 * group resolver participation as a side effect
4186 * of this operation. This is an intentional
4187 * part of the security model, which requires a
4188 * participating credential be established by
4189 * escalating privilege, setting up all other
4190 * aspects of the credential including whether
4191 * or not to participate in external group
4192 * membership resolution, then dropping their
4193 * effective privilege to that of the desired
4194 * final credential state.
4196 * Modifications to p_ucred must be guarded using the
4197 * proc's ucred lock. This prevents others from accessing
4198 * a garbage credential.
4200 while (imgp
->ip_origvattr
->va_mode
& VSUID
) {
4201 my_cred
= kauth_cred_proc_ref(p
);
4202 my_new_cred
= kauth_cred_setresuid(my_cred
, KAUTH_UID_NONE
, imgp
->ip_origvattr
->va_uid
, imgp
->ip_origvattr
->va_uid
, KAUTH_UID_NONE
);
4204 if (my_new_cred
== my_cred
) {
4205 kauth_cred_unref(&my_cred
);
4209 /* update cred on proc */
4212 if (p
->p_ucred
!= my_cred
) {
4213 proc_ucred_unlock(p
);
4214 kauth_cred_unref(&my_new_cred
);
4218 /* donate cred reference on my_new_cred to p->p_ucred */
4219 p
->p_ucred
= my_new_cred
;
4220 PROC_UPDATE_CREDS_ONPROC(p
);
4221 proc_ucred_unlock(p
);
4223 /* drop additional reference that was taken on the previous cred */
4224 kauth_cred_unref(&my_cred
);
4229 while (imgp
->ip_origvattr
->va_mode
& VSGID
) {
4230 my_cred
= kauth_cred_proc_ref(p
);
4231 my_new_cred
= kauth_cred_setresgid(my_cred
, KAUTH_GID_NONE
, imgp
->ip_origvattr
->va_gid
, imgp
->ip_origvattr
->va_gid
);
4233 if (my_new_cred
== my_cred
) {
4234 kauth_cred_unref(&my_cred
);
4238 /* update cred on proc */
4241 if (p
->p_ucred
!= my_cred
) {
4242 proc_ucred_unlock(p
);
4243 kauth_cred_unref(&my_new_cred
);
4247 /* donate cred reference on my_new_cred to p->p_ucred */
4248 p
->p_ucred
= my_new_cred
;
4249 PROC_UPDATE_CREDS_ONPROC(p
);
4250 proc_ucred_unlock(p
);
4252 /* drop additional reference that was taken on the previous cred */
4253 kauth_cred_unref(&my_cred
);
4260 * If a policy has indicated that it will transition the label,
4261 * before making the call into the MAC policies, get a new
4262 * duplicate credential, so they can modify it without
4263 * modifying any others sharing it.
4265 if (mac_transition
) {
4267 * This hook may generate upcalls that require
4268 * importance donation from the kernel.
4271 thread_t thread
= current_thread();
4272 thread_enable_send_importance(thread
, TRUE
);
4273 kauth_proc_label_update_execve(p
,
4274 imgp
->ip_vfs_context
,
4276 imgp
->ip_arch_offset
,
4278 imgp
->ip_scriptlabelp
,
4279 imgp
->ip_execlabelp
,
4282 &disjoint_cred
, /* will be non zero if disjoint */
4283 &label_update_return
);
4284 thread_enable_send_importance(thread
, FALSE
);
4286 if (disjoint_cred
) {
4288 * If updating the MAC label resulted in a
4289 * disjoint credential, flag that we need to
4290 * set the P_SUGID bit. This protects
4291 * against debuggers being attached by an
4292 * insufficiently privileged process onto the
4293 * result of a transition to a more privileged
4296 leave_sugid_clear
= 0;
4299 imgp
->ip_mac_return
= label_update_return
;
4302 mac_reset_ipc
= mac_proc_check_inherit_ipc_ports(p
, p
->p_textvp
, p
->p_textoff
, imgp
->ip_vp
, imgp
->ip_arch_offset
, imgp
->ip_scriptvp
);
4304 #endif /* CONFIG_MACF */
4307 * If 'leave_sugid_clear' is non-zero, then we passed the
4308 * VSUID and MACF checks, and successfully determined that
4309 * the previous cred was a member of the VSGID group, but
4310 * that it was not the default at the time of the execve,
4311 * and that the post-labelling credential was not disjoint.
4312 * So we don't set the P_SUGID or reset mach ports and fds
4313 * on the basis of simply running this code.
4315 if (mac_reset_ipc
|| !leave_sugid_clear
) {
4317 * Have mach reset the task and thread ports.
4318 * We don't want anyone who had the ports before
4319 * a setuid exec to be able to access/control the
4320 * task/thread after.
4322 ipc_task_reset(p
->task
);
4323 ipc_thread_reset((imgp
->ip_new_thread
!= NULL
) ?
4324 imgp
->ip_new_thread
: current_thread());
4327 if (!leave_sugid_clear
) {
4329 * Flag the process as setuid.
4331 OSBitOrAtomic(P_SUGID
, &p
->p_flag
);
4334 * Radar 2261856; setuid security hole fix
4335 * XXX For setuid processes, attempt to ensure that
4336 * stdin, stdout, and stderr are already allocated.
4337 * We do not want userland to accidentally allocate
4338 * descriptors in this range which has implied meaning
4341 for (i
= 0; i
< 3; i
++) {
4343 if (p
->p_fd
->fd_ofiles
[i
] != NULL
)
4347 * Do the kernel equivalent of
4350 * (void) open("/dev/null", O_RDONLY);
4352 * (void) open("/dev/null", O_WRONLY);
4355 struct fileproc
*fp
;
4358 struct nameidata
*ndp
= NULL
;
4365 if ((error
= falloc(p
,
4366 &fp
, &indx
, imgp
->ip_vfs_context
)) != 0)
4369 MALLOC(ndp
, struct nameidata
*, sizeof(*ndp
), M_TEMP
, M_WAITOK
| M_ZERO
);
4371 fp_free(p
, indx
, fp
);
4376 NDINIT(ndp
, LOOKUP
, OP_OPEN
, FOLLOW
, UIO_SYSSPACE
,
4377 CAST_USER_ADDR_T("/dev/null"),
4378 imgp
->ip_vfs_context
);
4380 if ((error
= vn_open(ndp
, flag
, 0)) != 0) {
4381 fp_free(p
, indx
, fp
);
4386 struct fileglob
*fg
= fp
->f_fglob
;
4389 fg
->fg_ops
= &vnops
;
4390 fg
->fg_data
= ndp
->ni_vp
;
4392 vnode_put(ndp
->ni_vp
);
4395 procfdtbl_releasefd(p
, indx
, NULL
);
4396 fp_drop(p
, indx
, fp
, 1);
4406 * We are here because we were told that the MAC label will
4407 * be transitioned, and the binary is not VSUID or VSGID; to
4408 * deal with this case, we could either duplicate a lot of
4409 * code, or we can indicate we want to default the P_SUGID
4410 * bit clear and jump back up.
4412 if (mac_transition
) {
4413 leave_sugid_clear
= 1;
4414 goto handle_mac_transition
;
4418 #endif /* CONFIG_MACF */
4421 * Implement the semantic where the effective user and group become
4422 * the saved user and group in exec'ed programs.
4424 * Modifications to p_ucred must be guarded using the
4425 * proc's ucred lock. This prevents others from accessing
4426 * a garbage credential.
4429 my_cred
= kauth_cred_proc_ref(p
);
4430 my_new_cred
= kauth_cred_setsvuidgid(my_cred
, kauth_cred_getuid(my_cred
), kauth_cred_getgid(my_cred
));
4432 if (my_new_cred
== my_cred
) {
4433 kauth_cred_unref(&my_cred
);
4437 /* update cred on proc */
4440 if (p
->p_ucred
!= my_cred
) {
4441 proc_ucred_unlock(p
);
4442 kauth_cred_unref(&my_new_cred
);
4446 /* donate cred reference on my_new_cred to p->p_ucred */
4447 p
->p_ucred
= my_new_cred
;
4448 PROC_UPDATE_CREDS_ONPROC(p
);
4449 proc_ucred_unlock(p
);
4451 /* drop additional reference that was taken on the previous cred */
4452 kauth_cred_unref(&my_cred
);
4458 /* Update the process' identity version and set the security token */
4460 set_security_token(p
);
4469 * Description: Set the user stack address for the process to the provided
4470 * address. If a custom stack was not set as a result of the
4471 * load process (i.e. as specified by the image file for the
4472 * executable), then allocate the stack in the provided map and
4473 * set up appropriate guard pages for enforcing administrative
4474 * limits on stack growth, if they end up being needed.
4476 * Parameters: p Process to set stack on
4477 * load_result Information from mach-o load commands
4478 * map Address map in which to allocate the new stack
4480 * Returns: KERN_SUCCESS Stack successfully created
4481 * !KERN_SUCCESS Mach failure code
4483 static kern_return_t
4484 create_unix_stack(vm_map_t map
, load_result_t
* load_result
,
4487 mach_vm_size_t size
, prot_size
;
4488 mach_vm_offset_t addr
, prot_addr
;
4491 mach_vm_address_t user_stack
= load_result
->user_stack
;
4494 p
->user_stack
= user_stack
;
4497 if (load_result
->user_stack_alloc_size
> 0) {
4499 * Allocate enough space for the maximum stack size we
4500 * will ever authorize and an extra page to act as
4501 * a guard page for stack overflows. For default stacks,
4502 * vm_initial_limit_stack takes care of the extra guard page.
4503 * Otherwise we must allocate it ourselves.
4505 if (mach_vm_round_page_overflow(load_result
->user_stack_alloc_size
, &size
)) {
4506 return KERN_INVALID_ARGUMENT
;
4508 addr
= mach_vm_trunc_page(load_result
->user_stack
- size
);
4509 kr
= mach_vm_allocate(map
, &addr
, size
,
4510 VM_MAKE_TAG(VM_MEMORY_STACK
) |
4512 if (kr
!= KERN_SUCCESS
) {
4513 // Can't allocate at default location, try anywhere
4515 kr
= mach_vm_allocate(map
, &addr
, size
,
4516 VM_MAKE_TAG(VM_MEMORY_STACK
) |
4518 if (kr
!= KERN_SUCCESS
) {
4522 user_stack
= addr
+ size
;
4523 load_result
->user_stack
= user_stack
;
4526 p
->user_stack
= user_stack
;
4530 load_result
->user_stack_alloc
= addr
;
4533 * And prevent access to what's above the current stack
4534 * size limit for this process.
4536 if (load_result
->user_stack_size
== 0) {
4537 load_result
->user_stack_size
= unix_stack_size(p
);
4538 prot_size
= mach_vm_trunc_page(size
- load_result
->user_stack_size
);
4540 prot_size
= PAGE_SIZE
;
4544 kr
= mach_vm_protect(map
,
4549 if (kr
!= KERN_SUCCESS
) {
4550 (void)mach_vm_deallocate(map
, addr
, size
);
4555 return KERN_SUCCESS
;
4558 #include <sys/reboot.h>
4561 * load_init_program_at_path
4563 * Description: Load the "init" program; in most cases, this will be "launchd"
4565 * Parameters: p Process to call execve() to create
4566 * the "init" program
4567 * scratch_addr Page in p, scratch space
4568 * path NULL terminated path
4570 * Returns: KERN_SUCCESS Success
4571 * !KERN_SUCCESS See execve/mac_execve for error codes
4573 * Notes: The process that is passed in is the first manufactured
4574 * process on the system, and gets here via bsd_ast() firing
4575 * for the first time. This is done to ensure that bsd_init()
4576 * has run to completion.
4578 * The address map of the first manufactured process matches the
4579 * word width of the kernel. Once the self-exec completes, the
4580 * initproc might be different.
4583 load_init_program_at_path(proc_t p
, user_addr_t scratch_addr
, const char* path
)
4587 struct execve_args init_exec_args
;
4588 user_addr_t argv0
= USER_ADDR_NULL
, argv1
= USER_ADDR_NULL
;
4591 * Validate inputs and pre-conditions
4594 assert(scratch_addr
);
4598 * Copy out program name.
4600 size_t path_length
= strlen(path
) + 1;
4601 argv0
= scratch_addr
;
4602 error
= copyout(path
, argv0
, path_length
);
4606 scratch_addr
= USER_ADDR_ALIGN(scratch_addr
+ path_length
, sizeof(user_addr_t
));
4609 * Put out first (and only) argument, similarly.
4610 * Assumes everything fits in a page as allocated above.
4612 if (boothowto
& RB_SINGLE
) {
4613 const char *init_args
= "-s";
4614 size_t init_args_length
= strlen(init_args
)+1;
4616 argv1
= scratch_addr
;
4617 error
= copyout(init_args
, argv1
, init_args_length
);
4621 scratch_addr
= USER_ADDR_ALIGN(scratch_addr
+ init_args_length
, sizeof(user_addr_t
));
4624 if (proc_is64bit(p
)) {
4625 user64_addr_t argv64bit
[3];
4627 argv64bit
[0] = argv0
;
4628 argv64bit
[1] = argv1
;
4629 argv64bit
[2] = USER_ADDR_NULL
;
4631 error
= copyout(argv64bit
, scratch_addr
, sizeof(argv64bit
));
4635 user32_addr_t argv32bit
[3];
4637 argv32bit
[0] = (user32_addr_t
)argv0
;
4638 argv32bit
[1] = (user32_addr_t
)argv1
;
4639 argv32bit
[2] = USER_ADDR_NULL
;
4641 error
= copyout(argv32bit
, scratch_addr
, sizeof(argv32bit
));
4647 * Set up argument block for fake call to execve.
4649 init_exec_args
.fname
= argv0
;
4650 init_exec_args
.argp
= scratch_addr
;
4651 init_exec_args
.envp
= USER_ADDR_NULL
;
4654 * So that init task is set with uid,gid 0 token
4656 set_security_token(p
);
4658 return execve(p
, &init_exec_args
, retval
);
4661 static const char * init_programs
[] = {
4663 "/usr/local/sbin/launchd.debug",
4665 #if DEVELOPMENT || DEBUG
4666 "/usr/local/sbin/launchd.development",
4674 * Description: Load the "init" program; in most cases, this will be "launchd"
4676 * Parameters: p Process to call execve() to create
4677 * the "init" program
4681 * Notes: The process that is passed in is the first manufactured
4682 * process on the system, and gets here via bsd_ast() firing
4683 * for the first time. This is done to ensure that bsd_init()
4684 * has run to completion.
4686 * In DEBUG & DEVELOPMENT builds, the launchdsuffix boot-arg
4687 * may be used to select a specific launchd executable. As with
4688 * the kcsuffix boot-arg, setting launchdsuffix to "" or "release"
4689 * will force /sbin/launchd to be selected.
4691 * Search order by build:
4693 * DEBUG DEVELOPMENT RELEASE PATH
4694 * ----------------------------------------------------------------------------------
4695 * 1 1 NA /usr/local/sbin/launchd.$LAUNCHDSUFFIX
4696 * 2 NA NA /usr/local/sbin/launchd.debug
4697 * 3 2 NA /usr/local/sbin/launchd.development
4698 * 4 3 1 /sbin/launchd
4701 load_init_program(proc_t p
)
4705 vm_map_t map
= current_map();
4706 mach_vm_offset_t scratch_addr
= 0;
4707 mach_vm_size_t map_page_size
= vm_map_page_size(map
);
4709 (void) mach_vm_allocate(map
, &scratch_addr
, map_page_size
, VM_FLAGS_ANYWHERE
);
4710 #if CONFIG_MEMORYSTATUS && CONFIG_JETSAM
4711 (void) memorystatus_init_at_boot_snapshot();
4712 #endif /* CONFIG_MEMORYSTATUS && CONFIG_JETSAM */
4714 #if DEBUG || DEVELOPMENT
4715 /* Check for boot-arg suffix first */
4716 char launchd_suffix
[64];
4717 if (PE_parse_boot_argn("launchdsuffix", launchd_suffix
, sizeof(launchd_suffix
))) {
4718 char launchd_path
[128];
4719 boolean_t is_release_suffix
= ((launchd_suffix
[0] == 0) ||
4720 (strcmp(launchd_suffix
, "release") == 0));
4722 if (is_release_suffix
) {
4723 error
= load_init_program_at_path(p
, (user_addr_t
)scratch_addr
, "/sbin/launchd");
4727 panic("Process 1 exec of launchd.release failed, errno %d", error
);
4729 strlcpy(launchd_path
, "/usr/local/sbin/launchd.", sizeof(launchd_path
));
4730 strlcat(launchd_path
, launchd_suffix
, sizeof(launchd_path
));
4732 /* All the error data is lost in the loop below, don't
4733 * attempt to save it. */
4734 if (!load_init_program_at_path(p
, (user_addr_t
)scratch_addr
, launchd_path
)) {
4742 for (i
= 0; i
< sizeof(init_programs
)/sizeof(init_programs
[0]); i
++) {
4743 error
= load_init_program_at_path(p
, (user_addr_t
)scratch_addr
, init_programs
[i
]);
4748 panic("Process 1 exec of %s failed, errno %d", ((i
== 0) ? "<null>" : init_programs
[i
-1]), error
);
4752 * load_return_to_errno
4754 * Description: Convert a load_return_t (Mach error) to an errno (BSD error)
4756 * Parameters: lrtn Mach error number
4758 * Returns: (int) BSD error number
4760 * EBADARCH Bad architecture
4761 * EBADMACHO Bad Mach object file
4762 * ESHLIBVERS Bad shared library version
4763 * ENOMEM Out of memory/resource shortage
4764 * EACCES Access denied
4765 * ENOENT Entry not found (usually "file does
4767 * EIO An I/O error occurred
4768 * EBADEXEC The executable is corrupt/unknown
4771 load_return_to_errno(load_return_t lrtn
)
4779 case LOAD_BADMACHO_UPX
:
4793 case LOAD_DECRYPTFAIL
:
4799 #include <mach/mach_types.h>
4800 #include <mach/vm_prot.h>
4801 #include <mach/semaphore.h>
4802 #include <mach/sync_policy.h>
4803 #include <kern/clock.h>
4804 #include <mach/kern_return.h>
4809 * Description: Allocate the block of memory used by the execve arguments.
4810 * At the same time, we allocate a page so that we can read in
4811 * the first page of the image.
4813 * Parameters: struct image_params * the image parameter block
4815 * Returns: 0 Success
4816 * EINVAL Invalid argument
4817 * EACCES Permission denied
4818 * EINTR Interrupted function
4819 * ENOMEM Not enough space
4821 * Notes: This is a temporary allocation into the kernel address space
4822 * to enable us to copy arguments in from user space. This is
4823 * necessitated by not mapping the process calling execve() into
4824 * the kernel address space during the execve() system call.
4826 * We assemble the argument and environment, etc., into this
4827 * region before copying it as a single block into the child
4828 * process address space (at the top or bottom of the stack,
4829 * depending on which way the stack grows; see the function
4830 * exec_copyout_strings() for details).
4832 * This ends up with a second (possibly unnecessary) copy compared
4833 * with assembing the data directly into the child address space,
4834 * instead, but since we cannot be guaranteed that the parent has
4835 * not modified its environment, we can't really know that it's
4836 * really a block there as well.
4840 static int execargs_waiters
= 0;
4841 lck_mtx_t
*execargs_cache_lock
;
4844 execargs_lock_lock(void) {
4845 lck_mtx_lock_spin(execargs_cache_lock
);
4849 execargs_lock_unlock(void) {
4850 lck_mtx_unlock(execargs_cache_lock
);
4853 static wait_result_t
4854 execargs_lock_sleep(void) {
4855 return(lck_mtx_sleep(execargs_cache_lock
, LCK_SLEEP_DEFAULT
, &execargs_free_count
, THREAD_INTERRUPTIBLE
));
4858 static kern_return_t
4859 execargs_purgeable_allocate(char **execarg_address
) {
4860 kern_return_t kr
= vm_allocate(bsd_pageable_map
, (vm_offset_t
*)execarg_address
, BSD_PAGEABLE_SIZE_PER_EXEC
, VM_FLAGS_ANYWHERE
| VM_FLAGS_PURGABLE
);
4861 assert(kr
== KERN_SUCCESS
);
4865 static kern_return_t
4866 execargs_purgeable_reference(void *execarg_address
) {
4867 int state
= VM_PURGABLE_NONVOLATILE
;
4868 kern_return_t kr
= vm_purgable_control(bsd_pageable_map
, (vm_offset_t
) execarg_address
, VM_PURGABLE_SET_STATE
, &state
);
4870 assert(kr
== KERN_SUCCESS
);
4874 static kern_return_t
4875 execargs_purgeable_volatilize(void *execarg_address
) {
4876 int state
= VM_PURGABLE_VOLATILE
| VM_PURGABLE_ORDERING_OBSOLETE
;
4878 kr
= vm_purgable_control(bsd_pageable_map
, (vm_offset_t
) execarg_address
, VM_PURGABLE_SET_STATE
, &state
);
4880 assert(kr
== KERN_SUCCESS
);
4886 execargs_wakeup_waiters(void) {
4887 thread_wakeup(&execargs_free_count
);
4891 execargs_alloc(struct image_params
*imgp
)
4895 int i
, cache_index
= -1;
4897 execargs_lock_lock();
4899 while (execargs_free_count
== 0) {
4901 res
= execargs_lock_sleep();
4903 if (res
!= THREAD_AWAKENED
) {
4904 execargs_lock_unlock();
4909 execargs_free_count
--;
4911 for (i
= 0; i
< execargs_cache_size
; i
++) {
4912 vm_offset_t element
= execargs_cache
[i
];
4915 imgp
->ip_strings
= (char *)(execargs_cache
[i
]);
4916 execargs_cache
[i
] = 0;
4921 assert(execargs_free_count
>= 0);
4923 execargs_lock_unlock();
4925 if (cache_index
== -1) {
4926 kret
= execargs_purgeable_allocate(&imgp
->ip_strings
);
4929 kret
= execargs_purgeable_reference(imgp
->ip_strings
);
4931 assert(kret
== KERN_SUCCESS
);
4932 if (kret
!= KERN_SUCCESS
) {
4936 /* last page used to read in file headers */
4937 imgp
->ip_vdata
= imgp
->ip_strings
+ ( NCARGS
+ PAGE_SIZE
);
4938 imgp
->ip_strendp
= imgp
->ip_strings
;
4939 imgp
->ip_argspace
= NCARGS
;
4940 imgp
->ip_strspace
= ( NCARGS
+ PAGE_SIZE
);
4948 * Description: Free the block of memory used by the execve arguments and the
4949 * first page of the executable by a previous call to the function
4952 * Parameters: struct image_params * the image parameter block
4954 * Returns: 0 Success
4955 * EINVAL Invalid argument
4956 * EINTR Oeration interrupted
4959 execargs_free(struct image_params
*imgp
)
4963 boolean_t needs_wakeup
= FALSE
;
4965 kret
= execargs_purgeable_volatilize(imgp
->ip_strings
);
4967 execargs_lock_lock();
4968 execargs_free_count
++;
4970 for (i
= 0; i
< execargs_cache_size
; i
++) {
4971 vm_offset_t element
= execargs_cache
[i
];
4973 execargs_cache
[i
] = (vm_offset_t
) imgp
->ip_strings
;
4974 imgp
->ip_strings
= NULL
;
4979 assert(imgp
->ip_strings
== NULL
);
4981 if (execargs_waiters
> 0)
4982 needs_wakeup
= TRUE
;
4984 execargs_lock_unlock();
4986 if (needs_wakeup
== TRUE
)
4987 execargs_wakeup_waiters();
4989 return ((kret
== KERN_SUCCESS
? 0 : EINVAL
));
4993 exec_resettextvp(proc_t p
, struct image_params
*imgp
)
4997 vnode_t tvp
= p
->p_textvp
;
5001 offset
= imgp
->ip_arch_offset
;
5004 panic("exec_resettextvp: expected valid vp");
5006 ret
= vnode_ref(vp
);
5010 p
->p_textoff
= offset
;
5012 p
->p_textvp
= NULLVP
; /* this is paranoia */
5017 if ( tvp
!= NULLVP
) {
5018 if (vnode_getwithref(tvp
) == 0) {
5027 * If the process is not signed or if it contains entitlements, we
5028 * need to communicate through the task_access_port to taskgated.
5030 * taskgated will provide a detached code signature if present, and
5031 * will enforce any restrictions on entitlements.
5035 taskgated_required(proc_t p
, boolean_t
*require_success
)
5042 csvnode_print_debug(p
->p_textvp
);
5044 const int can_skip_taskgated
= csproc_get_platform_binary(p
) && !csproc_get_platform_path(p
);
5045 if (can_skip_taskgated
) {
5046 if (cs_debug
) printf("taskgated not required for: %s\n", p
->p_name
);
5047 *require_success
= FALSE
;
5051 if ((p
->p_csflags
& CS_VALID
) == 0) {
5052 *require_success
= FALSE
;
5056 error
= cs_entitlements_blob_get(p
, &blob
, &length
);
5057 if (error
== 0 && blob
!= NULL
) {
5059 * fatal on the desktop when entitlements are present,
5060 * unless we started in single-user mode
5062 if ((boothowto
& RB_SINGLE
) == 0)
5063 *require_success
= TRUE
;
5065 * Allow initproc to run without causing taskgated to launch
5067 if (p
== initproc
) {
5068 *require_success
= FALSE
;
5072 if (cs_debug
) printf("taskgated required for: %s\n", p
->p_name
);
5077 *require_success
= FALSE
;
5082 * __EXEC_WAITING_ON_TASKGATED_CODE_SIGNATURE_UPCALL__
5084 * Description: Waits for the userspace daemon to respond to the request
5085 * we made. Function declared non inline to be visible in
5086 * stackshots and spindumps as well as debugging.
5088 __attribute__((noinline
)) int
5089 __EXEC_WAITING_ON_TASKGATED_CODE_SIGNATURE_UPCALL__(mach_port_t task_access_port
, int32_t new_pid
)
5091 return find_code_signature(task_access_port
, new_pid
);
5095 check_for_signature(proc_t p
, struct image_params
*imgp
)
5097 mach_port_t port
= NULL
;
5098 kern_return_t kr
= KERN_FAILURE
;
5100 boolean_t unexpected_failure
= FALSE
;
5101 unsigned char hash
[SHA1_RESULTLEN
];
5102 boolean_t require_success
= FALSE
;
5103 int spawn
= (imgp
->ip_flags
& IMGPF_SPAWN
);
5104 int vfexec
= (imgp
->ip_flags
& IMGPF_VFORK_EXEC
);
5105 os_reason_t signature_failure_reason
= OS_REASON_NULL
;
5108 * Override inherited code signing flags with the
5109 * ones for the process that is being successfully
5113 p
->p_csflags
= imgp
->ip_csflags
;
5116 /* Set the switch_protect flag on the map */
5117 if(p
->p_csflags
& (CS_HARD
|CS_KILL
)) {
5118 vm_map_switch_protect(get_task_map(p
->task
), TRUE
);
5122 * image activation may be failed due to policy
5123 * which is unexpected but security framework does not
5124 * approve of exec, kill and return immediately.
5126 if (imgp
->ip_mac_return
!= 0) {
5128 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXITREASON_CREATE
) | DBG_FUNC_NONE
,
5129 p
->p_pid
, OS_REASON_EXEC
, EXEC_EXIT_REASON_SECURITY_POLICY
, 0, 0);
5130 signature_failure_reason
= os_reason_create(OS_REASON_EXEC
, EXEC_EXIT_REASON_SECURITY_POLICY
);
5131 error
= imgp
->ip_mac_return
;
5132 unexpected_failure
= TRUE
;
5136 if (imgp
->ip_cs_error
!= OS_REASON_NULL
) {
5137 signature_failure_reason
= imgp
->ip_cs_error
;
5138 imgp
->ip_cs_error
= OS_REASON_NULL
;
5143 /* check if callout to taskgated is needed */
5144 if (!taskgated_required(p
, &require_success
)) {
5149 kr
= task_get_task_access_port(p
->task
, &port
);
5150 if (KERN_SUCCESS
!= kr
|| !IPC_PORT_VALID(port
)) {
5152 if (require_success
) {
5153 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXITREASON_CREATE
) | DBG_FUNC_NONE
,
5154 p
->p_pid
, OS_REASON_CODESIGNING
, CODESIGNING_EXIT_REASON_TASK_ACCESS_PORT
, 0, 0);
5155 signature_failure_reason
= os_reason_create(OS_REASON_CODESIGNING
, CODESIGNING_EXIT_REASON_TASK_ACCESS_PORT
);
5162 * taskgated returns KERN_SUCCESS if it has completed its work
5163 * and the exec should continue, KERN_FAILURE if the exec should
5164 * fail, or it may error out with different error code in an
5165 * event of mig failure (e.g. process was signalled during the
5166 * rpc call, taskgated died, mig server died etc.).
5169 kr
= __EXEC_WAITING_ON_TASKGATED_CODE_SIGNATURE_UPCALL__(port
, p
->p_pid
);
5177 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXITREASON_CREATE
) | DBG_FUNC_NONE
,
5178 p
->p_pid
, OS_REASON_CODESIGNING
, CODESIGNING_EXIT_REASON_TASKGATED_INVALID_SIG
, 0, 0);
5179 signature_failure_reason
= os_reason_create(OS_REASON_CODESIGNING
, CODESIGNING_EXIT_REASON_TASKGATED_INVALID_SIG
);
5184 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXITREASON_CREATE
) | DBG_FUNC_NONE
,
5185 p
->p_pid
, OS_REASON_EXEC
, EXEC_EXIT_REASON_TASKGATED_OTHER
, 0, 0);
5186 signature_failure_reason
= os_reason_create(OS_REASON_EXEC
, EXEC_EXIT_REASON_TASKGATED_OTHER
);
5187 unexpected_failure
= TRUE
;
5191 /* Only do this if exec_resettextvp() did not fail */
5192 if (p
->p_textvp
!= NULLVP
) {
5194 * If there's a new code directory, mark this process
5197 if (0 == ubc_cs_getcdhash(p
->p_textvp
, p
->p_textoff
, hash
)) {
5199 p
->p_csflags
|= CS_VALID
;
5206 if (!unexpected_failure
)
5207 p
->p_csflags
|= CS_KILLED
;
5208 /* make very sure execution fails */
5209 if (vfexec
|| spawn
) {
5210 assert(signature_failure_reason
!= OS_REASON_NULL
);
5211 psignal_vfork_with_reason(p
, p
->task
, imgp
->ip_new_thread
,
5212 SIGKILL
, signature_failure_reason
);
5213 signature_failure_reason
= OS_REASON_NULL
;
5216 assert(signature_failure_reason
!= OS_REASON_NULL
);
5217 psignal_with_reason(p
, SIGKILL
, signature_failure_reason
);
5218 signature_failure_reason
= OS_REASON_NULL
;
5222 /* If we hit this, we likely would have leaked an exit reason */
5223 assert(signature_failure_reason
== OS_REASON_NULL
);
5228 * Typically as soon as we start executing this process, the
5229 * first instruction will trigger a VM fault to bring the text
5230 * pages (as executable) into the address space, followed soon
5231 * thereafter by dyld data structures (for dynamic executable).
5232 * To optimize this, as well as improve support for hardware
5233 * debuggers that can only access resident pages present
5234 * in the process' page tables, we prefault some pages if
5235 * possible. Errors are non-fatal.
5237 static void exec_prefault_data(proc_t p __unused
, struct image_params
*imgp
, load_result_t
*load_result
)
5240 size_t expected_all_image_infos_size
;
5243 * Prefault executable or dyld entry point.
5245 vm_fault(current_map(),
5246 vm_map_trunc_page(load_result
->entry_point
,
5247 vm_map_page_mask(current_map())),
5248 VM_PROT_READ
| VM_PROT_EXECUTE
,
5250 THREAD_UNINT
, NULL
, 0);
5252 if (imgp
->ip_flags
& IMGPF_IS_64BIT
) {
5253 expected_all_image_infos_size
= sizeof(struct user64_dyld_all_image_infos
);
5255 expected_all_image_infos_size
= sizeof(struct user32_dyld_all_image_infos
);
5258 /* Decode dyld anchor structure from <mach-o/dyld_images.h> */
5259 if (load_result
->dynlinker
&&
5260 load_result
->all_image_info_addr
&&
5261 load_result
->all_image_info_size
>= expected_all_image_infos_size
) {
5263 struct user64_dyld_all_image_infos infos64
;
5264 struct user32_dyld_all_image_infos infos32
;
5268 * Pre-fault to avoid copyin() going through the trap handler
5269 * and recovery path.
5271 vm_fault(current_map(),
5272 vm_map_trunc_page(load_result
->all_image_info_addr
,
5273 vm_map_page_mask(current_map())),
5274 VM_PROT_READ
| VM_PROT_WRITE
,
5276 THREAD_UNINT
, NULL
, 0);
5277 if ((load_result
->all_image_info_addr
& PAGE_MASK
) + expected_all_image_infos_size
> PAGE_SIZE
) {
5278 /* all_image_infos straddles a page */
5279 vm_fault(current_map(),
5280 vm_map_trunc_page(load_result
->all_image_info_addr
+ expected_all_image_infos_size
- 1,
5281 vm_map_page_mask(current_map())),
5282 VM_PROT_READ
| VM_PROT_WRITE
,
5284 THREAD_UNINT
, NULL
, 0);
5287 ret
= copyin(load_result
->all_image_info_addr
,
5289 expected_all_image_infos_size
);
5290 if (ret
== 0 && all_image_infos
.infos32
.version
>= 9) {
5292 user_addr_t notification_address
;
5293 user_addr_t dyld_image_address
;
5294 user_addr_t dyld_version_address
;
5295 user_addr_t dyld_all_image_infos_address
;
5296 user_addr_t dyld_slide_amount
;
5298 if (imgp
->ip_flags
& IMGPF_IS_64BIT
) {
5299 notification_address
= all_image_infos
.infos64
.notification
;
5300 dyld_image_address
= all_image_infos
.infos64
.dyldImageLoadAddress
;
5301 dyld_version_address
= all_image_infos
.infos64
.dyldVersion
;
5302 dyld_all_image_infos_address
= all_image_infos
.infos64
.dyldAllImageInfosAddress
;
5304 notification_address
= all_image_infos
.infos32
.notification
;
5305 dyld_image_address
= all_image_infos
.infos32
.dyldImageLoadAddress
;
5306 dyld_version_address
= all_image_infos
.infos32
.dyldVersion
;
5307 dyld_all_image_infos_address
= all_image_infos
.infos32
.dyldAllImageInfosAddress
;
5311 * dyld statically sets up the all_image_infos in its Mach-O
5312 * binary at static link time, with pointers relative to its default
5313 * load address. Since ASLR might slide dyld before its first
5314 * instruction is executed, "dyld_slide_amount" tells us how far
5315 * dyld was loaded compared to its default expected load address.
5316 * All other pointers into dyld's image should be adjusted by this
5317 * amount. At some point later, dyld will fix up pointers to take
5318 * into account the slide, at which point the all_image_infos_address
5319 * field in the structure will match the runtime load address, and
5320 * "dyld_slide_amount" will be 0, if we were to consult it again.
5323 dyld_slide_amount
= load_result
->all_image_info_addr
- dyld_all_image_infos_address
;
5326 kprintf("exec_prefault: 0x%016llx 0x%08x 0x%016llx 0x%016llx 0x%016llx 0x%016llx\n",
5327 (uint64_t)load_result
->all_image_info_addr
,
5328 all_image_infos
.infos32
.version
,
5329 (uint64_t)notification_address
,
5330 (uint64_t)dyld_image_address
,
5331 (uint64_t)dyld_version_address
,
5332 (uint64_t)dyld_all_image_infos_address
);
5335 vm_fault(current_map(),
5336 vm_map_trunc_page(notification_address
+ dyld_slide_amount
,
5337 vm_map_page_mask(current_map())),
5338 VM_PROT_READ
| VM_PROT_EXECUTE
,
5340 THREAD_UNINT
, NULL
, 0);
5341 vm_fault(current_map(),
5342 vm_map_trunc_page(dyld_image_address
+ dyld_slide_amount
,
5343 vm_map_page_mask(current_map())),
5344 VM_PROT_READ
| VM_PROT_EXECUTE
,
5346 THREAD_UNINT
, NULL
, 0);
5347 vm_fault(current_map(),
5348 vm_map_trunc_page(dyld_version_address
+ dyld_slide_amount
,
5349 vm_map_page_mask(current_map())),
5352 THREAD_UNINT
, NULL
, 0);
5353 vm_fault(current_map(),
5354 vm_map_trunc_page(dyld_all_image_infos_address
+ dyld_slide_amount
,
5355 vm_map_page_mask(current_map())),
5356 VM_PROT_READ
| VM_PROT_WRITE
,
5358 THREAD_UNINT
, NULL
, 0);