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_param.h>
125 #include <mach/mach_types.h>
126 #include <mach/port.h>
127 #include <mach/task.h>
128 #include <mach/task_access.h>
129 #include <mach/thread_act.h>
130 #include <mach/vm_map.h>
131 #include <mach/mach_vm.h>
132 #include <mach/vm_param.h>
134 #include <kern/sched_prim.h> /* thread_wakeup() */
135 #include <kern/affinity.h>
136 #include <kern/assert.h>
137 #include <kern/task.h>
138 #include <kern/coalition.h>
139 #include <kern/policy_internal.h>
140 #include <kern/kalloc.h>
145 #include <security/mac_framework.h>
146 #include <security/mac_mach_internal.h>
150 #include <kern/arcade.h>
153 #include <vm/vm_map.h>
154 #include <vm/vm_kern.h>
155 #include <vm/vm_protos.h>
156 #include <vm/vm_kern.h>
157 #include <vm/vm_fault.h>
158 #include <vm/vm_pageout.h>
160 #include <kdp/kdp_dyld.h>
162 #include <machine/machine_routines.h>
163 #include <machine/pal_routines.h>
165 #include <pexpert/pexpert.h>
167 #if CONFIG_MEMORYSTATUS
168 #include <sys/kern_memorystatus.h>
171 #include <IOKit/IOBSD.h>
173 extern boolean_t vm_darkwake_mode
;
175 extern int bootarg_execfailurereports
; /* bsd_init.c */
178 /* Do not include dtrace.h, it redefines kmem_[alloc/free] */
179 extern void dtrace_proc_exec(proc_t
);
180 extern void (*dtrace_proc_waitfor_exec_ptr
)(proc_t
);
183 * Since dtrace_proc_waitfor_exec_ptr can be added/removed in dtrace_subr.c,
184 * we will store its value before actually calling it.
186 static void (*dtrace_proc_waitfor_hook
)(proc_t
) = NULL
;
188 #include <sys/dtrace_ptss.h>
191 /* support for child creation in exec after vfork */
192 thread_t
fork_create_child(task_t parent_task
,
193 coalition_t
*parent_coalition
,
199 void vfork_exit(proc_t p
, int rv
);
200 extern void proc_apply_task_networkbg_internal(proc_t
, thread_t
);
201 extern void task_set_did_exec_flag(task_t task
);
202 extern void task_clear_exec_copy_flag(task_t task
);
203 proc_t
proc_exec_switch_task(proc_t p
, task_t old_task
, task_t new_task
, thread_t new_thread
);
204 boolean_t
task_is_active(task_t
);
205 boolean_t
thread_is_active(thread_t thread
);
206 void thread_copy_resource_info(thread_t dst_thread
, thread_t src_thread
);
207 void *ipc_importance_exec_switch_task(task_t old_task
, task_t new_task
);
208 extern void ipc_importance_release(void *elem
);
209 extern boolean_t
task_has_watchports(task_t task
);
212 * Mach things for which prototypes are unavailable from Mach headers
214 #define IPC_KMSG_FLAGS_ALLOW_IMMOVABLE_SEND 0x1
217 void ipc_thread_reset(
219 kern_return_t
ipc_object_copyin(
221 mach_port_name_t name
,
222 mach_msg_type_name_t msgt_name
,
223 ipc_object_t
*objectp
,
224 mach_port_context_t context
,
225 mach_msg_guard_flags_t
*guard_flags
,
226 uint32_t kmsg_flags
);
227 void ipc_port_release_send(ipc_port_t
);
229 #if DEVELOPMENT || DEBUG
230 void task_importance_update_owner_info(task_t
);
233 extern struct savearea
*get_user_regs(thread_t
);
235 __attribute__((noinline
)) int __EXEC_WAITING_ON_TASKGATED_CODE_SIGNATURE_UPCALL__(mach_port_t task_access_port
, int32_t new_pid
);
237 #include <kern/thread.h>
238 #include <kern/task.h>
239 #include <kern/ast.h>
240 #include <kern/mach_loader.h>
241 #include <kern/mach_fat.h>
242 #include <mach-o/fat.h>
243 #include <mach-o/loader.h>
244 #include <machine/vmparam.h>
245 #include <sys/imgact.h>
251 * EAI_ITERLIMIT The maximum number of times to iterate an image
252 * activator in exec_activate_image() before treating
253 * it as malformed/corrupt.
255 #define EAI_ITERLIMIT 3
258 * For #! interpreter parsing
260 #define IS_WHITESPACE(ch) ((ch == ' ') || (ch == '\t'))
261 #define IS_EOL(ch) ((ch == '#') || (ch == '\n'))
263 extern vm_map_t bsd_pageable_map
;
264 extern const struct fileops vnops
;
265 extern int nextpidversion
;
267 #define USER_ADDR_ALIGN(addr, val) \
268 ( ( (user_addr_t)(addr) + (val) - 1) \
271 /* Platform Code Exec Logging */
272 static int platform_exec_logging
= 0;
274 SYSCTL_DECL(_security_mac
);
276 SYSCTL_INT(_security_mac
, OID_AUTO
, platform_exec_logging
, CTLFLAG_RW
, &platform_exec_logging
, 0,
277 "log cdhashes for all platform binary executions");
279 static os_log_t peLog
= OS_LOG_DEFAULT
;
281 struct exec_port_actions
{
282 uint32_t portwatch_count
;
283 uint32_t registered_count
;
284 ipc_port_t
*portwatch_array
;
285 ipc_port_t
*registered_array
;
288 struct image_params
; /* Forward */
289 static int exec_activate_image(struct image_params
*imgp
);
290 static int exec_copyout_strings(struct image_params
*imgp
, user_addr_t
*stackp
);
291 static int load_return_to_errno(load_return_t lrtn
);
292 static int execargs_alloc(struct image_params
*imgp
);
293 static int execargs_free(struct image_params
*imgp
);
294 static int exec_check_permissions(struct image_params
*imgp
);
295 static int exec_extract_strings(struct image_params
*imgp
);
296 static int exec_add_apple_strings(struct image_params
*imgp
, const load_result_t
*load_result
);
297 static int exec_handle_sugid(struct image_params
*imgp
);
298 static int sugid_scripts
= 0;
299 SYSCTL_INT(_kern
, OID_AUTO
, sugid_scripts
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &sugid_scripts
, 0, "");
300 static kern_return_t
create_unix_stack(vm_map_t map
, load_result_t
* load_result
, proc_t p
);
301 static int copyoutptr(user_addr_t ua
, user_addr_t ptr
, int ptr_size
);
302 static void exec_resettextvp(proc_t
, struct image_params
*);
303 static int check_for_signature(proc_t
, struct image_params
*);
304 static void exec_prefault_data(proc_t
, struct image_params
*, load_result_t
*);
305 static errno_t
exec_handle_port_actions(struct image_params
*imgp
,
306 struct exec_port_actions
*port_actions
);
307 static errno_t
exec_handle_spawnattr_policy(proc_t p
, thread_t thread
, int psa_apptype
, uint64_t psa_qos_clamp
,
308 uint64_t psa_darwin_role
, struct exec_port_actions
*port_actions
);
309 static void exec_port_actions_destroy(struct exec_port_actions
*port_actions
);
312 * exec_add_user_string
314 * Add the requested string to the string space area.
316 * Parameters; struct image_params * image parameter block
317 * user_addr_t string to add to strings area
318 * int segment from which string comes
319 * boolean_t TRUE if string contributes to NCARGS
322 * !0 Failure errno from copyinstr()
325 * (imgp->ip_strendp) updated location of next add, if any
326 * (imgp->ip_strspace) updated byte count of space remaining
327 * (imgp->ip_argspace) updated byte count of space in NCARGS
330 exec_add_user_string(struct image_params
*imgp
, user_addr_t str
, int seg
, boolean_t is_ncargs
)
339 space
= imgp
->ip_argspace
; /* by definition smaller than ip_strspace */
341 space
= imgp
->ip_strspace
;
349 if (!UIO_SEG_IS_USER_SPACE(seg
)) {
350 char *kstr
= CAST_DOWN(char *, str
); /* SAFE */
351 error
= copystr(kstr
, imgp
->ip_strendp
, space
, &len
);
353 error
= copyinstr(str
, imgp
->ip_strendp
, space
, &len
);
356 imgp
->ip_strendp
+= len
;
357 imgp
->ip_strspace
-= len
;
359 imgp
->ip_argspace
-= len
;
361 } while (error
== ENAMETOOLONG
);
367 * dyld is now passed the executable path as a getenv-like variable
368 * in the same fashion as the stack_guard and malloc_entropy keys.
370 #define EXECUTABLE_KEY "executable_path="
375 * To support new app package launching for Mac OS X, the dyld needs the
376 * first argument to execve() stored on the user stack.
378 * Save the executable path name at the bottom of the strings area and set
379 * the argument vector pointer to the location following that to indicate
380 * the start of the argument and environment tuples, setting the remaining
381 * string space count to the size of the string area minus the path length.
383 * Parameters; struct image_params * image parameter block
384 * char * path used to invoke program
385 * int segment from which path comes
387 * Returns: int 0 Success
389 * copy[in]str:EFAULT Bad address
390 * copy[in]str:ENAMETOOLONG Filename too long
393 * (imgp->ip_strings) saved path
394 * (imgp->ip_strspace) space remaining in ip_strings
395 * (imgp->ip_strendp) start of remaining copy area
396 * (imgp->ip_argspace) space remaining of NCARGS
397 * (imgp->ip_applec) Initial applev[0]
399 * Note: We have to do this before the initial namei() since in the
400 * path contains symbolic links, namei() will overwrite the
401 * original path buffer contents. If the last symbolic link
402 * resolved was a relative pathname, we would lose the original
403 * "path", which could be an absolute pathname. This might be
404 * unacceptable for dyld.
407 exec_save_path(struct image_params
*imgp
, user_addr_t path
, int seg
, const char **excpath
)
413 // imgp->ip_strings can come out of a cache, so we need to obliterate the
415 memset(imgp
->ip_strings
, '\0', strlen(EXECUTABLE_KEY
) + MAXPATHLEN
);
417 len
= MIN(MAXPATHLEN
, imgp
->ip_strspace
);
420 case UIO_USERSPACE32
:
421 case UIO_USERSPACE64
: /* Same for copyin()... */
422 error
= copyinstr(path
, imgp
->ip_strings
+ strlen(EXECUTABLE_KEY
), len
, &len
);
425 kpath
= CAST_DOWN(char *, path
); /* SAFE */
426 error
= copystr(kpath
, imgp
->ip_strings
+ strlen(EXECUTABLE_KEY
), len
, &len
);
434 bcopy(EXECUTABLE_KEY
, imgp
->ip_strings
, strlen(EXECUTABLE_KEY
));
435 len
+= strlen(EXECUTABLE_KEY
);
437 imgp
->ip_strendp
+= len
;
438 imgp
->ip_strspace
-= len
;
441 *excpath
= imgp
->ip_strings
+ strlen(EXECUTABLE_KEY
);
449 * exec_reset_save_path
451 * If we detect a shell script, we need to reset the string area
452 * state so that the interpreter can be saved onto the stack.
454 * Parameters; struct image_params * image parameter block
456 * Returns: int 0 Success
459 * (imgp->ip_strings) saved path
460 * (imgp->ip_strspace) space remaining in ip_strings
461 * (imgp->ip_strendp) start of remaining copy area
462 * (imgp->ip_argspace) space remaining of NCARGS
466 exec_reset_save_path(struct image_params
*imgp
)
468 imgp
->ip_strendp
= imgp
->ip_strings
;
469 imgp
->ip_argspace
= NCARGS
;
470 imgp
->ip_strspace
= (NCARGS
+ PAGE_SIZE
);
478 * Image activator for interpreter scripts. If the image begins with
479 * the characters "#!", then it is an interpreter script. Verify the
480 * length of the script line indicating the interpreter is not in
481 * excess of the maximum allowed size. If this is the case, then
482 * break out the arguments, if any, which are separated by white
483 * space, and copy them into the argument save area as if they were
484 * provided on the command line before all other arguments. The line
485 * ends when we encounter a comment character ('#') or newline.
487 * Parameters; struct image_params * image parameter block
489 * Returns: -1 not an interpreter (keep looking)
490 * -3 Success: interpreter: relookup
491 * >0 Failure: interpreter: error number
493 * A return value other than -1 indicates subsequent image activators should
494 * not be given the opportunity to attempt to activate the image.
497 exec_shell_imgact(struct image_params
*imgp
)
499 char *vdata
= imgp
->ip_vdata
;
501 char *line_startp
, *line_endp
;
505 * Make sure it's a shell script. If we've already redirected
506 * from an interpreted file once, don't do it again.
508 if (vdata
[0] != '#' ||
510 (imgp
->ip_flags
& IMGPF_INTERPRET
) != 0) {
514 if (imgp
->ip_origcputype
!= 0) {
515 /* Fat header previously matched, don't allow shell script inside */
519 imgp
->ip_flags
|= IMGPF_INTERPRET
;
520 imgp
->ip_interp_sugid_fd
= -1;
521 imgp
->ip_interp_buffer
[0] = '\0';
523 /* Check to see if SUGID scripts are permitted. If they aren't then
524 * clear the SUGID bits.
525 * imgp->ip_vattr is known to be valid.
527 if (sugid_scripts
== 0) {
528 imgp
->ip_origvattr
->va_mode
&= ~(VSUID
| VSGID
);
531 /* Try to find the first non-whitespace character */
532 for (ihp
= &vdata
[2]; ihp
< &vdata
[IMG_SHSIZE
]; ihp
++) {
534 /* Did not find interpreter, "#!\n" */
536 } else if (IS_WHITESPACE(*ihp
)) {
537 /* Whitespace, like "#! /bin/sh\n", keep going. */
539 /* Found start of interpreter */
544 if (ihp
== &vdata
[IMG_SHSIZE
]) {
545 /* All whitespace, like "#! " */
551 /* Try to find the end of the interpreter+args string */
552 for (; ihp
< &vdata
[IMG_SHSIZE
]; ihp
++) {
557 /* Still part of interpreter or args */
561 if (ihp
== &vdata
[IMG_SHSIZE
]) {
562 /* A long line, like "#! blah blah blah" without end */
566 /* Backtrack until we find the last non-whitespace */
567 while (IS_EOL(*ihp
) || IS_WHITESPACE(*ihp
)) {
571 /* The character after the last non-whitespace is our logical end of line */
575 * Now we have pointers to the usable part of:
577 * "#! /usr/bin/int first second third \n"
578 * ^ line_startp ^ line_endp
581 /* copy the interpreter name */
582 interp
= imgp
->ip_interp_buffer
;
583 for (ihp
= line_startp
; (ihp
< line_endp
) && !IS_WHITESPACE(*ihp
); ihp
++) {
588 exec_reset_save_path(imgp
);
589 exec_save_path(imgp
, CAST_USER_ADDR_T(imgp
->ip_interp_buffer
),
592 /* Copy the entire interpreter + args for later processing into argv[] */
593 interp
= imgp
->ip_interp_buffer
;
594 for (ihp
= line_startp
; (ihp
< line_endp
); ihp
++) {
601 * If we have an SUID or SGID script, create a file descriptor
602 * from the vnode and pass /dev/fd/%d instead of the actual
603 * path name so that the script does not get opened twice
605 if (imgp
->ip_origvattr
->va_mode
& (VSUID
| VSGID
)) {
611 p
= vfs_context_proc(imgp
->ip_vfs_context
);
612 error
= falloc(p
, &fp
, &fd
, imgp
->ip_vfs_context
);
617 fp
->f_fglob
->fg_flag
= FREAD
;
618 fp
->f_fglob
->fg_ops
= &vnops
;
619 fp
->f_fglob
->fg_data
= (caddr_t
)imgp
->ip_vp
;
622 procfdtbl_releasefd(p
, fd
, NULL
);
623 fp_drop(p
, fd
, fp
, 1);
625 vnode_ref(imgp
->ip_vp
);
627 imgp
->ip_interp_sugid_fd
= fd
;
639 * Image activator for fat 1.0 binaries. If the binary is fat, then we
640 * need to select an image from it internally, and make that the image
641 * we are going to attempt to execute. At present, this consists of
642 * reloading the first page for the image with a first page from the
643 * offset location indicated by the fat header.
645 * Parameters; struct image_params * image parameter block
647 * Returns: -1 not a fat binary (keep looking)
648 * -2 Success: encapsulated binary: reread
649 * >0 Failure: error number
651 * Important: This image activator is byte order neutral.
653 * Note: A return value other than -1 indicates subsequent image
654 * activators should not be given the opportunity to attempt
655 * to activate the image.
657 * If we find an encapsulated binary, we make no assertions
658 * about its validity; instead, we leave that up to a rescan
659 * for an activator to claim it, and, if it is claimed by one,
660 * that activator is responsible for determining validity.
663 exec_fat_imgact(struct image_params
*imgp
)
665 proc_t p
= vfs_context_proc(imgp
->ip_vfs_context
);
666 kauth_cred_t cred
= kauth_cred_proc_ref(p
);
667 struct fat_header
*fat_header
= (struct fat_header
*)imgp
->ip_vdata
;
668 struct _posix_spawnattr
*psa
= NULL
;
669 struct fat_arch fat_arch
;
673 if (imgp
->ip_origcputype
!= 0) {
674 /* Fat header previously matched, don't allow another fat file inside */
675 error
= -1; /* not claimed */
679 /* Make sure it's a fat binary */
680 if (OSSwapBigToHostInt32(fat_header
->magic
) != FAT_MAGIC
) {
681 error
= -1; /* not claimed */
685 /* imgp->ip_vdata has PAGE_SIZE, zerofilled if the file is smaller */
686 lret
= fatfile_validate_fatarches((vm_offset_t
)fat_header
, PAGE_SIZE
);
687 if (lret
!= LOAD_SUCCESS
) {
688 error
= load_return_to_errno(lret
);
692 /* If posix_spawn binprefs exist, respect those prefs. */
693 psa
= (struct _posix_spawnattr
*) imgp
->ip_px_sa
;
694 if (psa
!= NULL
&& psa
->psa_binprefs
[0] != 0) {
697 /* Check each preference listed against all arches in header */
698 for (pr
= 0; pr
< NBINPREFS
; pr
++) {
699 cpu_type_t pref
= psa
->psa_binprefs
[pr
];
701 /* No suitable arch in the pref list */
706 if (pref
== CPU_TYPE_ANY
) {
707 /* Fall through to regular grading */
708 goto regular_grading
;
711 lret
= fatfile_getbestarch_for_cputype(pref
,
712 (vm_offset_t
)fat_header
,
716 if (lret
== LOAD_SUCCESS
) {
721 /* Requested binary preference was not honored */
727 /* Look up our preferred architecture in the fat file. */
728 lret
= fatfile_getbestarch((vm_offset_t
)fat_header
,
732 if (lret
!= LOAD_SUCCESS
) {
733 error
= load_return_to_errno(lret
);
738 /* Read the Mach-O header out of fat_arch */
739 error
= vn_rdwr(UIO_READ
, imgp
->ip_vp
, imgp
->ip_vdata
,
740 PAGE_SIZE
, fat_arch
.offset
,
741 UIO_SYSSPACE
, (IO_UNIT
| IO_NODELOCKED
),
748 memset(imgp
->ip_vdata
+ (PAGE_SIZE
- resid
), 0x0, resid
);
751 /* Success. Indicate we have identified an encapsulated binary */
753 imgp
->ip_arch_offset
= (user_size_t
)fat_arch
.offset
;
754 imgp
->ip_arch_size
= (user_size_t
)fat_arch
.size
;
755 imgp
->ip_origcputype
= fat_arch
.cputype
;
756 imgp
->ip_origcpusubtype
= fat_arch
.cpusubtype
;
759 kauth_cred_unref(&cred
);
764 activate_exec_state(task_t task
, proc_t p
, thread_t thread
, load_result_t
*result
)
768 task_set_dyld_info(task
, MACH_VM_MIN_ADDRESS
, 0);
769 task_set_64bit(task
, result
->is_64bit_addr
, result
->is_64bit_data
);
770 if (result
->is_64bit_addr
) {
771 OSBitOrAtomic(P_LP64
, &p
->p_flag
);
773 OSBitAndAtomic(~((uint32_t)P_LP64
), &p
->p_flag
);
775 task_set_mach_header_address(task
, result
->mach_header
);
777 ret
= thread_state_initialize(thread
);
778 if (ret
!= KERN_SUCCESS
) {
782 if (result
->threadstate
) {
783 uint32_t *ts
= result
->threadstate
;
784 uint32_t total_size
= result
->threadstate_sz
;
786 while (total_size
> 0) {
787 uint32_t flavor
= *ts
++;
788 uint32_t size
= *ts
++;
790 ret
= thread_setstatus(thread
, flavor
, (thread_state_t
)ts
, size
);
795 total_size
-= (size
+ 2) * sizeof(uint32_t);
799 thread_setentrypoint(thread
, result
->entry_point
);
806 * Set p->p_comm and p->p_name to the name passed to exec
809 set_proc_name(struct image_params
*imgp
, proc_t p
)
811 int p_name_len
= sizeof(p
->p_name
) - 1;
813 if (imgp
->ip_ndp
->ni_cnd
.cn_namelen
> p_name_len
) {
814 imgp
->ip_ndp
->ni_cnd
.cn_namelen
= p_name_len
;
817 bcopy((caddr_t
)imgp
->ip_ndp
->ni_cnd
.cn_nameptr
, (caddr_t
)p
->p_name
,
818 (unsigned)imgp
->ip_ndp
->ni_cnd
.cn_namelen
);
819 p
->p_name
[imgp
->ip_ndp
->ni_cnd
.cn_namelen
] = '\0';
821 if (imgp
->ip_ndp
->ni_cnd
.cn_namelen
> MAXCOMLEN
) {
822 imgp
->ip_ndp
->ni_cnd
.cn_namelen
= MAXCOMLEN
;
825 bcopy((caddr_t
)imgp
->ip_ndp
->ni_cnd
.cn_nameptr
, (caddr_t
)p
->p_comm
,
826 (unsigned)imgp
->ip_ndp
->ni_cnd
.cn_namelen
);
827 p
->p_comm
[imgp
->ip_ndp
->ni_cnd
.cn_namelen
] = '\0';
833 * Image activator for mach-o 1.0 binaries.
835 * Parameters; struct image_params * image parameter block
837 * Returns: -1 not a fat binary (keep looking)
838 * -2 Success: encapsulated binary: reread
839 * >0 Failure: error number
840 * EBADARCH Mach-o binary, but with an unrecognized
842 * ENOMEM No memory for child process after -
843 * can only happen after vfork()
845 * Important: This image activator is NOT byte order neutral.
847 * Note: A return value other than -1 indicates subsequent image
848 * activators should not be given the opportunity to attempt
849 * to activate the image.
851 * TODO: More gracefully handle failures after vfork
854 exec_mach_imgact(struct image_params
*imgp
)
856 struct mach_header
*mach_header
= (struct mach_header
*)imgp
->ip_vdata
;
857 proc_t p
= vfs_context_proc(imgp
->ip_vfs_context
);
860 task_t new_task
= NULL
; /* protected by vfexec */
862 struct uthread
*uthread
;
863 vm_map_t old_map
= VM_MAP_NULL
;
864 vm_map_t map
= VM_MAP_NULL
;
866 load_result_t load_result
= {};
867 struct _posix_spawnattr
*psa
= NULL
;
868 int spawn
= (imgp
->ip_flags
& IMGPF_SPAWN
);
869 int vfexec
= (imgp
->ip_flags
& IMGPF_VFORK_EXEC
);
870 int exec
= (imgp
->ip_flags
& IMGPF_EXEC
);
871 os_reason_t exec_failure_reason
= OS_REASON_NULL
;
874 * make sure it's a Mach-O 1.0 or Mach-O 2.0 binary; the difference
875 * is a reserved field on the end, so for the most part, we can
876 * treat them as if they were identical. Reverse-endian Mach-O
877 * binaries are recognized but not compatible.
879 if ((mach_header
->magic
== MH_CIGAM
) ||
880 (mach_header
->magic
== MH_CIGAM_64
)) {
885 if ((mach_header
->magic
!= MH_MAGIC
) &&
886 (mach_header
->magic
!= MH_MAGIC_64
)) {
891 if (mach_header
->filetype
!= MH_EXECUTE
) {
896 if (imgp
->ip_origcputype
!= 0) {
897 /* Fat header previously had an idea about this thin file */
898 if (imgp
->ip_origcputype
!= mach_header
->cputype
||
899 imgp
->ip_origcpusubtype
!= mach_header
->cpusubtype
) {
904 imgp
->ip_origcputype
= mach_header
->cputype
;
905 imgp
->ip_origcpusubtype
= mach_header
->cpusubtype
;
908 task
= current_task();
909 thread
= current_thread();
910 uthread
= get_bsdthread_info(thread
);
912 if ((mach_header
->cputype
& CPU_ARCH_ABI64
) == CPU_ARCH_ABI64
) {
913 imgp
->ip_flags
|= IMGPF_IS_64BIT_ADDR
| IMGPF_IS_64BIT_DATA
;
916 /* If posix_spawn binprefs exist, respect those prefs. */
917 psa
= (struct _posix_spawnattr
*) imgp
->ip_px_sa
;
918 if (psa
!= NULL
&& psa
->psa_binprefs
[0] != 0) {
920 for (pr
= 0; pr
< NBINPREFS
; pr
++) {
921 cpu_type_t pref
= psa
->psa_binprefs
[pr
];
923 /* No suitable arch in the pref list */
928 if (pref
== CPU_TYPE_ANY
) {
929 /* Jump to regular grading */
933 if (pref
== imgp
->ip_origcputype
) {
934 /* We have a match! */
942 if (!grade_binary(imgp
->ip_origcputype
, imgp
->ip_origcpusubtype
& ~CPU_SUBTYPE_MASK
, TRUE
)) {
947 if (validate_potential_simulator_binary(imgp
->ip_origcputype
, imgp
,
948 imgp
->ip_arch_offset
, imgp
->ip_arch_size
) != LOAD_SUCCESS
) {
951 error
= exec_save_path(imgp
, imgp
->ip_user_fname
, imgp
->ip_seg
, &excpath
);
952 os_log_error(OS_LOG_DEFAULT
, "Unsupported 32-bit executable: \"%s\"", (error
) ? imgp
->ip_vp
->v_name
: excpath
);
958 #if defined(HAS_APPLE_PAC)
959 assert(mach_header
->cputype
== CPU_TYPE_ARM64
962 if (((mach_header
->cputype
== CPU_TYPE_ARM64
&&
963 (mach_header
->cpusubtype
& ~CPU_SUBTYPE_MASK
) == CPU_SUBTYPE_ARM64E
)
964 ) && (CPU_SUBTYPE_ARM64_PTR_AUTH_VERSION(mach_header
->cpusubtype
) == 0)) {
965 imgp
->ip_flags
&= ~IMGPF_NOJOP
;
967 imgp
->ip_flags
|= IMGPF_NOJOP
;
971 /* Copy in arguments/environment from the old process */
972 error
= exec_extract_strings(imgp
);
977 AUDIT_ARG(argv
, imgp
->ip_startargv
, imgp
->ip_argc
,
978 imgp
->ip_endargv
- imgp
->ip_startargv
);
979 AUDIT_ARG(envv
, imgp
->ip_endargv
, imgp
->ip_envc
,
980 imgp
->ip_endenvv
- imgp
->ip_endargv
);
983 * We are being called to activate an image subsequent to a vfork()
984 * operation; in this case, we know that our task, thread, and
985 * uthread are actually those of our parent, and our proc, which we
986 * obtained indirectly from the image_params vfs_context_t, is the
990 imgp
->ip_new_thread
= fork_create_child(task
,
994 (imgp
->ip_flags
& IMGPF_IS_64BIT_ADDR
),
995 (imgp
->ip_flags
& IMGPF_IS_64BIT_DATA
),
997 /* task and thread ref returned, will be released in __mac_execve */
998 if (imgp
->ip_new_thread
== NULL
) {
1005 /* reset local idea of thread, uthread, task */
1006 thread
= imgp
->ip_new_thread
;
1007 uthread
= get_bsdthread_info(thread
);
1008 task
= new_task
= get_threadtask(thread
);
1011 * Load the Mach-O file.
1013 * NOTE: An error after this point indicates we have potentially
1014 * destroyed or overwritten some process state while attempting an
1015 * execve() following a vfork(), which is an unrecoverable condition.
1016 * We send the new process an immediate SIGKILL to avoid it executing
1017 * any instructions in the mutated address space. For true spawns,
1018 * this is not the case, and "too late" is still not too late to
1019 * return an error code to the parent process.
1023 * Actually load the image file we previously decided to load.
1025 lret
= load_machfile(imgp
, mach_header
, thread
, &map
, &load_result
);
1026 if (lret
!= LOAD_SUCCESS
) {
1027 error
= load_return_to_errno(lret
);
1029 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXITREASON_CREATE
) | DBG_FUNC_NONE
,
1030 p
->p_pid
, OS_REASON_EXEC
, EXEC_EXIT_REASON_BAD_MACHO
, 0, 0);
1031 if (lret
== LOAD_BADMACHO_UPX
) {
1032 set_proc_name(imgp
, p
);
1033 exec_failure_reason
= os_reason_create(OS_REASON_EXEC
, EXEC_EXIT_REASON_UPX
);
1034 exec_failure_reason
->osr_flags
|= OS_REASON_FLAG_GENERATE_CRASH_REPORT
;
1036 exec_failure_reason
= os_reason_create(OS_REASON_EXEC
, EXEC_EXIT_REASON_BAD_MACHO
);
1038 if (bootarg_execfailurereports
) {
1039 set_proc_name(imgp
, p
);
1040 exec_failure_reason
->osr_flags
|= OS_REASON_FLAG_GENERATE_CRASH_REPORT
;
1044 exec_failure_reason
->osr_flags
|= OS_REASON_FLAG_CONSISTENT_FAILURE
;
1050 p
->p_cputype
= imgp
->ip_origcputype
;
1051 p
->p_cpusubtype
= imgp
->ip_origcpusubtype
;
1052 p
->p_platform
= load_result
.ip_platform
;
1053 p
->p_sdk
= load_result
.lr_sdk
;
1056 vm_map_set_user_wire_limit(map
, p
->p_rlimit
[RLIMIT_MEMLOCK
].rlim_cur
);
1059 * Set code-signing flags if this binary is signed, or if parent has
1060 * requested them on exec.
1062 if (load_result
.csflags
& CS_VALID
) {
1063 imgp
->ip_csflags
|= load_result
.csflags
&
1064 (CS_VALID
| CS_SIGNED
| CS_DEV_CODE
|
1065 CS_HARD
| CS_KILL
| CS_RESTRICT
| CS_ENFORCEMENT
| CS_REQUIRE_LV
|
1066 CS_FORCED_LV
| CS_ENTITLEMENTS_VALIDATED
| CS_DYLD_PLATFORM
| CS_RUNTIME
|
1067 CS_ENTITLEMENT_FLAGS
|
1068 CS_EXEC_SET_HARD
| CS_EXEC_SET_KILL
| CS_EXEC_SET_ENFORCEMENT
);
1070 imgp
->ip_csflags
&= ~CS_VALID
;
1073 if (p
->p_csflags
& CS_EXEC_SET_HARD
) {
1074 imgp
->ip_csflags
|= CS_HARD
;
1076 if (p
->p_csflags
& CS_EXEC_SET_KILL
) {
1077 imgp
->ip_csflags
|= CS_KILL
;
1079 if (p
->p_csflags
& CS_EXEC_SET_ENFORCEMENT
) {
1080 imgp
->ip_csflags
|= CS_ENFORCEMENT
;
1082 if (p
->p_csflags
& CS_EXEC_INHERIT_SIP
) {
1083 if (p
->p_csflags
& CS_INSTALLER
) {
1084 imgp
->ip_csflags
|= CS_INSTALLER
;
1086 if (p
->p_csflags
& CS_DATAVAULT_CONTROLLER
) {
1087 imgp
->ip_csflags
|= CS_DATAVAULT_CONTROLLER
;
1089 if (p
->p_csflags
& CS_NVRAM_UNRESTRICTED
) {
1090 imgp
->ip_csflags
|= CS_NVRAM_UNRESTRICTED
;
1095 * Set up the system reserved areas in the new address space.
1098 cpu_subtype
= 0; /* all cpu_subtypes use the same shared region */
1099 #if defined(HAS_APPLE_PAC)
1100 if (cpu_type() == CPU_TYPE_ARM64
&&
1101 (p
->p_cpusubtype
& ~CPU_SUBTYPE_MASK
) == CPU_SUBTYPE_ARM64E
) {
1102 assertf(p
->p_cputype
== CPU_TYPE_ARM64
,
1103 "p %p cpu_type() 0x%x p->p_cputype 0x%x p->p_cpusubtype 0x%x",
1104 p
, cpu_type(), p
->p_cputype
, p
->p_cpusubtype
);
1106 * arm64e uses pointer authentication, so request a separate
1107 * shared region for this CPU subtype.
1109 cpu_subtype
= p
->p_cpusubtype
& ~CPU_SUBTYPE_MASK
;
1111 #endif /* HAS_APPLE_PAC */
1112 vm_map_exec(map
, task
, load_result
.is_64bit_addr
, (void *)p
->p_fd
->fd_rdir
, cpu_type(), cpu_subtype
);
1115 * Close file descriptors which specify close-on-exec.
1117 fdexec(p
, psa
!= NULL
? psa
->psa_flags
: 0, exec
);
1120 * deal with set[ug]id.
1122 error
= exec_handle_sugid(imgp
);
1124 vm_map_deallocate(map
);
1126 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXITREASON_CREATE
) | DBG_FUNC_NONE
,
1127 p
->p_pid
, OS_REASON_EXEC
, EXEC_EXIT_REASON_SUGID_FAILURE
, 0, 0);
1129 exec_failure_reason
= os_reason_create(OS_REASON_EXEC
, EXEC_EXIT_REASON_SUGID_FAILURE
);
1130 if (bootarg_execfailurereports
) {
1131 set_proc_name(imgp
, p
);
1132 exec_failure_reason
->osr_flags
|= OS_REASON_FLAG_GENERATE_CRASH_REPORT
;
1139 * Commit to new map.
1141 * Swap the new map for the old for target task, which consumes
1142 * our new map reference but each leaves us responsible for the
1143 * old_map reference. That lets us get off the pmap associated
1144 * with it, and then we can release it.
1146 * The map needs to be set on the target task which is different
1147 * than current task, thus swap_task_map is used instead of
1150 old_map
= swap_task_map(task
, thread
, map
);
1151 vm_map_deallocate(old_map
);
1154 lret
= activate_exec_state(task
, p
, thread
, &load_result
);
1155 if (lret
!= KERN_SUCCESS
) {
1156 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXITREASON_CREATE
) | DBG_FUNC_NONE
,
1157 p
->p_pid
, OS_REASON_EXEC
, EXEC_EXIT_REASON_ACTV_THREADSTATE
, 0, 0);
1159 exec_failure_reason
= os_reason_create(OS_REASON_EXEC
, EXEC_EXIT_REASON_ACTV_THREADSTATE
);
1160 if (bootarg_execfailurereports
) {
1161 set_proc_name(imgp
, p
);
1162 exec_failure_reason
->osr_flags
|= OS_REASON_FLAG_GENERATE_CRASH_REPORT
;
1169 * deal with voucher on exec-calling thread.
1171 if (imgp
->ip_new_thread
== NULL
) {
1172 thread_set_mach_voucher(current_thread(), IPC_VOUCHER_NULL
);
1175 /* Make sure we won't interrupt ourself signalling a partial process */
1176 if (!vfexec
&& !spawn
&& (p
->p_lflag
& P_LTRACED
)) {
1177 psignal(p
, SIGTRAP
);
1180 if (load_result
.unixproc
&&
1181 create_unix_stack(get_task_map(task
),
1183 p
) != KERN_SUCCESS
) {
1184 error
= load_return_to_errno(LOAD_NOSPACE
);
1186 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXITREASON_CREATE
) | DBG_FUNC_NONE
,
1187 p
->p_pid
, OS_REASON_EXEC
, EXEC_EXIT_REASON_STACK_ALLOC
, 0, 0);
1189 exec_failure_reason
= os_reason_create(OS_REASON_EXEC
, EXEC_EXIT_REASON_STACK_ALLOC
);
1190 if (bootarg_execfailurereports
) {
1191 set_proc_name(imgp
, p
);
1192 exec_failure_reason
->osr_flags
|= OS_REASON_FLAG_GENERATE_CRASH_REPORT
;
1198 error
= exec_add_apple_strings(imgp
, &load_result
);
1200 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXITREASON_CREATE
) | DBG_FUNC_NONE
,
1201 p
->p_pid
, OS_REASON_EXEC
, EXEC_EXIT_REASON_APPLE_STRING_INIT
, 0, 0);
1203 exec_failure_reason
= os_reason_create(OS_REASON_EXEC
, EXEC_EXIT_REASON_APPLE_STRING_INIT
);
1204 if (bootarg_execfailurereports
) {
1205 set_proc_name(imgp
, p
);
1206 exec_failure_reason
->osr_flags
|= OS_REASON_FLAG_GENERATE_CRASH_REPORT
;
1211 /* Switch to target task's map to copy out strings */
1212 old_map
= vm_map_switch(get_task_map(task
));
1214 if (load_result
.unixproc
) {
1218 * Copy the strings area out into the new process address
1222 error
= exec_copyout_strings(imgp
, &ap
);
1224 vm_map_switch(old_map
);
1226 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXITREASON_CREATE
) | DBG_FUNC_NONE
,
1227 p
->p_pid
, OS_REASON_EXEC
, EXEC_EXIT_REASON_COPYOUT_STRINGS
, 0, 0);
1229 exec_failure_reason
= os_reason_create(OS_REASON_EXEC
, EXEC_EXIT_REASON_COPYOUT_STRINGS
);
1230 if (bootarg_execfailurereports
) {
1231 set_proc_name(imgp
, p
);
1232 exec_failure_reason
->osr_flags
|= OS_REASON_FLAG_GENERATE_CRASH_REPORT
;
1237 thread_setuserstack(thread
, ap
);
1240 if (load_result
.dynlinker
) {
1242 int new_ptr_size
= (imgp
->ip_flags
& IMGPF_IS_64BIT_ADDR
) ? 8 : 4;
1244 /* Adjust the stack */
1245 ap
= thread_adjuserstack(thread
, -new_ptr_size
);
1246 error
= copyoutptr(load_result
.mach_header
, ap
, new_ptr_size
);
1249 vm_map_switch(old_map
);
1251 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXITREASON_CREATE
) | DBG_FUNC_NONE
,
1252 p
->p_pid
, OS_REASON_EXEC
, EXEC_EXIT_REASON_COPYOUT_DYNLINKER
, 0, 0);
1254 exec_failure_reason
= os_reason_create(OS_REASON_EXEC
, EXEC_EXIT_REASON_COPYOUT_DYNLINKER
);
1255 if (bootarg_execfailurereports
) {
1256 set_proc_name(imgp
, p
);
1257 exec_failure_reason
->osr_flags
|= OS_REASON_FLAG_GENERATE_CRASH_REPORT
;
1261 task_set_dyld_info(task
, load_result
.all_image_info_addr
,
1262 load_result
.all_image_info_size
);
1265 /* Avoid immediate VM faults back into kernel */
1266 exec_prefault_data(p
, imgp
, &load_result
);
1268 vm_map_switch(old_map
);
1271 * Reset signal state.
1273 execsigs(p
, thread
);
1276 * need to cancel async IO requests that can be cancelled and wait for those
1277 * already active. MAY BLOCK!
1282 /* FIXME: Till vmspace inherit is fixed: */
1283 if (!vfexec
&& p
->vm_shm
) {
1288 /* Clean up the semaphores */
1293 * Remember file name for accounting.
1295 p
->p_acflag
&= ~AFORK
;
1297 set_proc_name(imgp
, p
);
1299 #if CONFIG_SECLUDED_MEMORY
1300 if (secluded_for_apps
&&
1301 load_result
.platform_binary
) {
1302 if (strncmp(p
->p_name
,
1304 sizeof(p
->p_name
)) == 0) {
1305 task_set_could_use_secluded_mem(task
, TRUE
);
1307 task_set_could_use_secluded_mem(task
, FALSE
);
1309 if (strncmp(p
->p_name
,
1311 sizeof(p
->p_name
)) == 0) {
1312 task_set_could_also_use_secluded_mem(task
, TRUE
);
1315 #endif /* CONFIG_SECLUDED_MEMORY */
1318 if (load_result
.legacy_footprint
) {
1319 task_set_legacy_footprint(task
);
1321 #endif /* __arm64__ */
1323 pal_dbg_set_task_name(task
);
1326 * The load result will have already been munged by AMFI to include the
1327 * platform binary flag if boot-args dictated it (AMFI will mark anything
1328 * that doesn't go through the upcall path as a platform binary if its
1329 * enforcement is disabled).
1331 if (load_result
.platform_binary
) {
1333 printf("setting platform binary on task: pid = %d\n", p
->p_pid
);
1337 * We must use 'task' here because the proc's task has not yet been
1338 * switched to the new one.
1340 task_set_platform_binary(task
, TRUE
);
1343 printf("clearing platform binary on task: pid = %d\n", p
->p_pid
);
1346 task_set_platform_binary(task
, FALSE
);
1349 #if DEVELOPMENT || DEBUG
1351 * Update the pid an proc name for importance base if any
1353 task_importance_update_owner_info(task
);
1356 memcpy(&p
->p_uuid
[0], &load_result
.uuid
[0], sizeof(p
->p_uuid
));
1359 dtrace_proc_exec(p
);
1362 if (kdebug_enable
) {
1365 uintptr_t fsid
= 0, fileid
= 0;
1366 if (imgp
->ip_vattr
) {
1367 uint64_t fsid64
= vnode_get_va_fsid(imgp
->ip_vattr
);
1369 fileid
= imgp
->ip_vattr
->va_fileid
;
1370 // check for (unexpected) overflow and trace zero in that case
1371 if (fsid
!= fsid64
|| fileid
!= imgp
->ip_vattr
->va_fileid
) {
1375 KERNEL_DEBUG_CONSTANT_IST1(TRACE_DATA_EXEC
, p
->p_pid
, fsid
, fileid
, 0,
1376 (uintptr_t)thread_tid(thread
));
1379 * Collect the pathname for tracing
1381 kdbg_trace_string(p
, &args
[0], &args
[1], &args
[2], &args
[3]);
1382 KERNEL_DEBUG_CONSTANT_IST1(TRACE_STRING_EXEC
, args
[0], args
[1],
1383 args
[2], args
[3], (uintptr_t)thread_tid(thread
));
1387 * If posix_spawned with the START_SUSPENDED flag, stop the
1388 * process before it runs.
1390 if (imgp
->ip_px_sa
!= NULL
) {
1391 psa
= (struct _posix_spawnattr
*) imgp
->ip_px_sa
;
1392 if (psa
->psa_flags
& POSIX_SPAWN_START_SUSPENDED
) {
1396 (void) task_suspend_internal(task
);
1401 * mark as execed, wakeup the process that vforked (if any) and tell
1402 * it that it now has its own resources back
1404 OSBitOrAtomic(P_EXEC
, &p
->p_flag
);
1405 proc_resetregister(p
);
1406 if (p
->p_pptr
&& (p
->p_lflag
& P_LPPWAIT
)) {
1408 p
->p_lflag
&= ~P_LPPWAIT
;
1410 wakeup((caddr_t
)p
->p_pptr
);
1414 * Pay for our earlier safety; deliver the delayed signals from
1415 * the incomplete vfexec process now that it's complete.
1417 if (vfexec
&& (p
->p_lflag
& P_LTRACED
)) {
1418 psignal_vfork(p
, new_task
, thread
, SIGTRAP
);
1424 /* Don't allow child process to execute any instructions */
1427 assert(exec_failure_reason
!= OS_REASON_NULL
);
1428 psignal_vfork_with_reason(p
, new_task
, thread
, SIGKILL
, exec_failure_reason
);
1429 exec_failure_reason
= OS_REASON_NULL
;
1431 assert(exec_failure_reason
!= OS_REASON_NULL
);
1432 psignal_with_reason(p
, SIGKILL
, exec_failure_reason
);
1433 exec_failure_reason
= OS_REASON_NULL
;
1436 /* Terminate the exec copy task */
1437 task_terminate_internal(task
);
1441 /* We can't stop this system call at this point, so just pretend we succeeded */
1444 os_reason_free(exec_failure_reason
);
1445 exec_failure_reason
= OS_REASON_NULL
;
1449 if (load_result
.threadstate
) {
1450 kfree(load_result
.threadstate
, load_result
.threadstate_sz
);
1451 load_result
.threadstate
= NULL
;
1455 /* If we hit this, we likely would have leaked an exit reason */
1456 assert(exec_failure_reason
== OS_REASON_NULL
);
1464 * Our image activator table; this is the table of the image types we are
1465 * capable of loading. We list them in order of preference to ensure the
1466 * fastest image load speed.
1468 * XXX hardcoded, for now; should use linker sets
1471 int(*const ex_imgact
)(struct image_params
*);
1472 const char *ex_name
;
1474 { exec_mach_imgact
, "Mach-o Binary" },
1475 { exec_fat_imgact
, "Fat Binary" },
1476 { exec_shell_imgact
, "Interpreter Script" },
1482 * exec_activate_image
1484 * Description: Iterate through the available image activators, and activate
1485 * the image associated with the imgp structure. We start with
1486 * the activator for Mach-o binaries followed by that for Fat binaries
1487 * for Interpreter scripts.
1489 * Parameters: struct image_params * Image parameter block
1491 * Returns: 0 Success
1492 * EBADEXEC The executable is corrupt/unknown
1493 * execargs_alloc:EINVAL Invalid argument
1494 * execargs_alloc:EACCES Permission denied
1495 * execargs_alloc:EINTR Interrupted function
1496 * execargs_alloc:ENOMEM Not enough space
1497 * exec_save_path:EFAULT Bad address
1498 * exec_save_path:ENAMETOOLONG Filename too long
1499 * exec_check_permissions:EACCES Permission denied
1500 * exec_check_permissions:ENOEXEC Executable file format error
1501 * exec_check_permissions:ETXTBSY Text file busy [misuse of error code]
1502 * exec_check_permissions:???
1504 * vn_rdwr:??? [anything vn_rdwr can return]
1505 * <ex_imgact>:??? [anything an imgact can return]
1506 * EDEADLK Process is being terminated
1509 exec_activate_image(struct image_params
*imgp
)
1511 struct nameidata
*ndp
= NULL
;
1512 const char *excpath
;
1515 int once
= 1; /* save SGUID-ness for interpreted files */
1518 proc_t p
= vfs_context_proc(imgp
->ip_vfs_context
);
1520 error
= execargs_alloc(imgp
);
1525 error
= exec_save_path(imgp
, imgp
->ip_user_fname
, imgp
->ip_seg
, &excpath
);
1530 /* Use excpath, which contains the copyin-ed exec path */
1531 DTRACE_PROC1(exec
, uintptr_t, excpath
);
1533 MALLOC(ndp
, struct nameidata
*, sizeof(*ndp
), M_TEMP
, M_WAITOK
| M_ZERO
);
1539 NDINIT(ndp
, LOOKUP
, OP_LOOKUP
, FOLLOW
| LOCKLEAF
| AUDITVNPATH1
,
1540 UIO_SYSSPACE
, CAST_USER_ADDR_T(excpath
), imgp
->ip_vfs_context
);
1547 imgp
->ip_ndp
= ndp
; /* successful namei(); call nameidone() later */
1548 imgp
->ip_vp
= ndp
->ni_vp
; /* if set, need to vnode_put() at some point */
1551 * Before we start the transition from binary A to binary B, make
1552 * sure another thread hasn't started exiting the process. We grab
1553 * the proc lock to check p_lflag initially, and the transition
1554 * mechanism ensures that the value doesn't change after we release
1558 if (p
->p_lflag
& P_LEXIT
) {
1563 error
= proc_transstart(p
, 1, 0);
1569 error
= exec_check_permissions(imgp
);
1574 /* Copy; avoid invocation of an interpreter overwriting the original */
1577 *imgp
->ip_origvattr
= *imgp
->ip_vattr
;
1580 error
= vn_rdwr(UIO_READ
, imgp
->ip_vp
, imgp
->ip_vdata
, PAGE_SIZE
, 0,
1581 UIO_SYSSPACE
, IO_NODELOCKED
,
1582 vfs_context_ucred(imgp
->ip_vfs_context
),
1583 &resid
, vfs_context_proc(imgp
->ip_vfs_context
));
1589 memset(imgp
->ip_vdata
+ (PAGE_SIZE
- resid
), 0x0, resid
);
1592 encapsulated_binary
:
1593 /* Limit the number of iterations we will attempt on each binary */
1594 if (++itercount
> EAI_ITERLIMIT
) {
1599 for (i
= 0; error
== -1 && execsw
[i
].ex_imgact
!= NULL
; i
++) {
1600 error
= (*execsw
[i
].ex_imgact
)(imgp
);
1603 /* case -1: not claimed: continue */
1604 case -2: /* Encapsulated binary, imgp->ip_XXX set for next iteration */
1605 goto encapsulated_binary
;
1607 case -3: /* Interpreter */
1610 * Copy the script label for later use. Note that
1611 * the label can be different when the script is
1612 * actually read by the interpreter.
1614 if (imgp
->ip_scriptlabelp
) {
1615 mac_vnode_label_free(imgp
->ip_scriptlabelp
);
1617 imgp
->ip_scriptlabelp
= mac_vnode_label_alloc();
1618 if (imgp
->ip_scriptlabelp
== NULL
) {
1622 mac_vnode_label_copy(imgp
->ip_vp
->v_label
,
1623 imgp
->ip_scriptlabelp
);
1626 * Take a ref of the script vnode for later use.
1628 if (imgp
->ip_scriptvp
) {
1629 vnode_put(imgp
->ip_scriptvp
);
1631 if (vnode_getwithref(imgp
->ip_vp
) == 0) {
1632 imgp
->ip_scriptvp
= imgp
->ip_vp
;
1638 vnode_put(imgp
->ip_vp
);
1639 imgp
->ip_vp
= NULL
; /* already put */
1640 imgp
->ip_ndp
= NULL
; /* already nameidone */
1642 /* Use excpath, which exec_shell_imgact reset to the interpreter */
1643 NDINIT(ndp
, LOOKUP
, OP_LOOKUP
, FOLLOW
| LOCKLEAF
,
1644 UIO_SYSSPACE
, CAST_USER_ADDR_T(excpath
), imgp
->ip_vfs_context
);
1646 proc_transend(p
, 0);
1655 if (imgp
->ip_flags
& IMGPF_INTERPRET
&& ndp
->ni_vp
) {
1656 AUDIT_ARG(vnpath
, ndp
->ni_vp
, ARG_VNODE2
);
1660 * Call out to allow 3rd party notification of exec.
1661 * Ignore result of kauth_authorize_fileop call.
1663 if (kauth_authorize_fileop_has_listeners()) {
1664 kauth_authorize_fileop(vfs_context_ucred(imgp
->ip_vfs_context
),
1666 (uintptr_t)ndp
->ni_vp
, 0);
1670 proc_transend(p
, 0);
1673 if (imgp
->ip_strings
) {
1674 execargs_free(imgp
);
1677 nameidone(imgp
->ip_ndp
);
1687 * exec_validate_spawnattr_policy
1689 * Description: Validates the entitlements required to set the apptype.
1691 * Parameters: int psa_apptype posix spawn attribute apptype
1693 * Returns: 0 Success
1697 exec_validate_spawnattr_policy(int psa_apptype
)
1699 if ((psa_apptype
& POSIX_SPAWN_PROC_TYPE_MASK
) != 0) {
1700 int proctype
= psa_apptype
& POSIX_SPAWN_PROC_TYPE_MASK
;
1701 if (proctype
== POSIX_SPAWN_PROC_TYPE_DRIVER
) {
1702 if (!IOTaskHasEntitlement(current_task(), POSIX_SPAWN_ENTITLEMENT_DRIVER
)) {
1712 * exec_handle_spawnattr_policy
1714 * Description: Decode and apply the posix_spawn apptype, qos clamp, and watchport ports to the task.
1716 * Parameters: proc_t p process to apply attributes to
1717 * int psa_apptype posix spawn attribute apptype
1719 * Returns: 0 Success
1722 exec_handle_spawnattr_policy(proc_t p
, thread_t thread
, int psa_apptype
, uint64_t psa_qos_clamp
,
1723 uint64_t psa_darwin_role
, struct exec_port_actions
*port_actions
)
1725 int apptype
= TASK_APPTYPE_NONE
;
1726 int qos_clamp
= THREAD_QOS_UNSPECIFIED
;
1727 int role
= TASK_UNSPECIFIED
;
1729 if ((psa_apptype
& POSIX_SPAWN_PROC_TYPE_MASK
) != 0) {
1730 int proctype
= psa_apptype
& POSIX_SPAWN_PROC_TYPE_MASK
;
1733 case POSIX_SPAWN_PROC_TYPE_DAEMON_INTERACTIVE
:
1734 apptype
= TASK_APPTYPE_DAEMON_INTERACTIVE
;
1736 case POSIX_SPAWN_PROC_TYPE_DAEMON_STANDARD
:
1737 apptype
= TASK_APPTYPE_DAEMON_STANDARD
;
1739 case POSIX_SPAWN_PROC_TYPE_DAEMON_ADAPTIVE
:
1740 apptype
= TASK_APPTYPE_DAEMON_ADAPTIVE
;
1742 case POSIX_SPAWN_PROC_TYPE_DAEMON_BACKGROUND
:
1743 apptype
= TASK_APPTYPE_DAEMON_BACKGROUND
;
1745 case POSIX_SPAWN_PROC_TYPE_APP_DEFAULT
:
1746 apptype
= TASK_APPTYPE_APP_DEFAULT
;
1748 #if !CONFIG_EMBEDDED
1749 case POSIX_SPAWN_PROC_TYPE_APP_TAL
:
1750 apptype
= TASK_APPTYPE_APP_TAL
;
1752 #endif /* !CONFIG_EMBEDDED */
1753 case POSIX_SPAWN_PROC_TYPE_DRIVER
:
1754 apptype
= TASK_APPTYPE_DRIVER
;
1757 apptype
= TASK_APPTYPE_NONE
;
1758 /* TODO: Should an invalid value here fail the spawn? */
1763 if (psa_qos_clamp
!= POSIX_SPAWN_PROC_CLAMP_NONE
) {
1764 switch (psa_qos_clamp
) {
1765 case POSIX_SPAWN_PROC_CLAMP_UTILITY
:
1766 qos_clamp
= THREAD_QOS_UTILITY
;
1768 case POSIX_SPAWN_PROC_CLAMP_BACKGROUND
:
1769 qos_clamp
= THREAD_QOS_BACKGROUND
;
1771 case POSIX_SPAWN_PROC_CLAMP_MAINTENANCE
:
1772 qos_clamp
= THREAD_QOS_MAINTENANCE
;
1775 qos_clamp
= THREAD_QOS_UNSPECIFIED
;
1776 /* TODO: Should an invalid value here fail the spawn? */
1781 if (psa_darwin_role
!= PRIO_DARWIN_ROLE_DEFAULT
) {
1782 proc_darwin_role_to_task_role(psa_darwin_role
, &role
);
1785 if (apptype
!= TASK_APPTYPE_NONE
||
1786 qos_clamp
!= THREAD_QOS_UNSPECIFIED
||
1787 role
!= TASK_UNSPECIFIED
||
1788 port_actions
->portwatch_count
) {
1789 proc_set_task_spawnpolicy(p
->task
, thread
, apptype
, qos_clamp
, role
,
1790 port_actions
->portwatch_array
, port_actions
->portwatch_count
);
1793 if (port_actions
->registered_count
) {
1794 if (mach_ports_register(p
->task
, port_actions
->registered_array
,
1795 port_actions
->registered_count
)) {
1798 /* mach_ports_register() consumed the array */
1799 port_actions
->registered_array
= NULL
;
1800 port_actions
->registered_count
= 0;
1807 exec_port_actions_destroy(struct exec_port_actions
*port_actions
)
1809 if (port_actions
->portwatch_array
) {
1810 for (uint32_t i
= 0; i
< port_actions
->portwatch_count
; i
++) {
1811 ipc_port_t port
= NULL
;
1812 if ((port
= port_actions
->portwatch_array
[i
]) != NULL
) {
1813 ipc_port_release_send(port
);
1816 kfree(port_actions
->portwatch_array
,
1817 port_actions
->portwatch_count
* sizeof(ipc_port_t
*));
1820 if (port_actions
->registered_array
) {
1821 for (uint32_t i
= 0; i
< port_actions
->registered_count
; i
++) {
1822 ipc_port_t port
= NULL
;
1823 if ((port
= port_actions
->registered_array
[i
]) != NULL
) {
1824 ipc_port_release_send(port
);
1827 kfree(port_actions
->registered_array
,
1828 port_actions
->registered_count
* sizeof(ipc_port_t
*));
1833 * exec_handle_port_actions
1835 * Description: Go through the _posix_port_actions_t contents,
1836 * calling task_set_special_port, task_set_exception_ports
1837 * and/or audit_session_spawnjoin for the current task.
1839 * Parameters: struct image_params * Image parameter block
1841 * Returns: 0 Success
1843 * ENOTSUP Illegal posix_spawn attr flag was set
1846 exec_handle_port_actions(struct image_params
*imgp
,
1847 struct exec_port_actions
*actions
)
1849 _posix_spawn_port_actions_t pacts
= imgp
->ip_px_spa
;
1851 proc_t p
= vfs_context_proc(imgp
->ip_vfs_context
);
1853 _ps_port_action_t
*act
= NULL
;
1854 task_t task
= get_threadtask(imgp
->ip_new_thread
);
1855 ipc_port_t port
= NULL
;
1857 int i
, portwatch_i
= 0, registered_i
= 0;
1859 boolean_t task_has_watchport_boost
= task_has_watchports(current_task());
1860 boolean_t in_exec
= (imgp
->ip_flags
& IMGPF_EXEC
);
1862 for (i
= 0; i
< pacts
->pspa_count
; i
++) {
1863 act
= &pacts
->pspa_actions
[i
];
1865 switch (act
->port_type
) {
1867 case PSPA_EXCEPTION
:
1869 case PSPA_AU_SESSION
:
1872 case PSPA_IMP_WATCHPORTS
:
1873 if (++actions
->portwatch_count
> TASK_MAX_WATCHPORT_COUNT
) {
1878 case PSPA_REGISTERED_PORTS
:
1879 if (++actions
->registered_count
> TASK_PORT_REGISTER_MAX
) {
1890 if (actions
->portwatch_count
) {
1891 if (in_exec
&& task_has_watchport_boost
) {
1895 actions
->portwatch_array
=
1896 kalloc(sizeof(ipc_port_t
*) * actions
->portwatch_count
);
1897 if (actions
->portwatch_array
== NULL
) {
1901 bzero(actions
->portwatch_array
,
1902 sizeof(ipc_port_t
*) * actions
->portwatch_count
);
1905 if (actions
->registered_count
) {
1906 actions
->registered_array
=
1907 kalloc(sizeof(ipc_port_t
*) * actions
->registered_count
);
1908 if (actions
->registered_array
== NULL
) {
1912 bzero(actions
->registered_array
,
1913 sizeof(ipc_port_t
*) * actions
->registered_count
);
1916 for (i
= 0; i
< pacts
->pspa_count
; i
++) {
1917 act
= &pacts
->pspa_actions
[i
];
1919 if (MACH_PORT_VALID(act
->new_port
)) {
1920 kr
= ipc_object_copyin(get_task_ipcspace(current_task()),
1921 act
->new_port
, MACH_MSG_TYPE_COPY_SEND
,
1922 (ipc_object_t
*) &port
, 0, NULL
, IPC_KMSG_FLAGS_ALLOW_IMMOVABLE_SEND
);
1924 if (kr
!= KERN_SUCCESS
) {
1929 /* it's NULL or DEAD */
1930 port
= CAST_MACH_NAME_TO_PORT(act
->new_port
);
1933 switch (act
->port_type
) {
1935 kr
= task_set_special_port(task
, act
->which
, port
);
1937 if (kr
!= KERN_SUCCESS
) {
1942 case PSPA_EXCEPTION
:
1943 kr
= task_set_exception_ports(task
, act
->mask
, port
,
1944 act
->behavior
, act
->flavor
);
1945 if (kr
!= KERN_SUCCESS
) {
1950 case PSPA_AU_SESSION
:
1951 ret
= audit_session_spawnjoin(p
, task
, port
);
1953 /* audit_session_spawnjoin() has already dropped the reference in case of error. */
1959 case PSPA_IMP_WATCHPORTS
:
1960 if (actions
->portwatch_array
) {
1961 /* hold on to this till end of spawn */
1962 actions
->portwatch_array
[portwatch_i
++] = port
;
1964 ipc_port_release_send(port
);
1967 case PSPA_REGISTERED_PORTS
:
1968 /* hold on to this till end of spawn */
1969 actions
->registered_array
[registered_i
++] = port
;
1977 /* action failed, so release port resources */
1978 ipc_port_release_send(port
);
1985 DTRACE_PROC1(spawn__port__failure
, mach_port_name_t
, act
->new_port
);
1991 * exec_handle_file_actions
1993 * Description: Go through the _posix_file_actions_t contents applying the
1994 * open, close, and dup2 operations to the open file table for
1995 * the current process.
1997 * Parameters: struct image_params * Image parameter block
1999 * Returns: 0 Success
2002 * Note: Actions are applied in the order specified, with the credential
2003 * of the parent process. This is done to permit the parent
2004 * process to utilize POSIX_SPAWN_RESETIDS to drop privilege in
2005 * the child following operations the child may in fact not be
2006 * normally permitted to perform.
2009 exec_handle_file_actions(struct image_params
*imgp
, short psa_flags
)
2013 proc_t p
= vfs_context_proc(imgp
->ip_vfs_context
);
2014 _posix_spawn_file_actions_t px_sfap
= imgp
->ip_px_sfa
;
2015 int ival
[2]; /* dummy retval for system calls) */
2017 for (action
= 0; action
< px_sfap
->psfa_act_count
; action
++) {
2018 _psfa_action_t
*psfa
= &px_sfap
->psfa_act_acts
[action
];
2020 switch (psfa
->psfaa_type
) {
2023 * Open is different, in that it requires the use of
2024 * a path argument, which is normally copied in from
2025 * user space; because of this, we have to support an
2026 * open from kernel space that passes an address space
2027 * context of UIO_SYSSPACE, and casts the address
2028 * argument to a user_addr_t.
2031 struct vnode_attr
*vap
;
2032 struct nameidata
*ndp
;
2033 int mode
= psfa
->psfaa_openargs
.psfao_mode
;
2034 struct dup2_args dup2a
;
2035 struct close_nocancel_args ca
;
2038 MALLOC(bufp
, char *, sizeof(*vap
) + sizeof(*ndp
), M_TEMP
, M_WAITOK
| M_ZERO
);
2044 vap
= (struct vnode_attr
*) bufp
;
2045 ndp
= (struct nameidata
*) (bufp
+ sizeof(*vap
));
2048 /* Mask off all but regular access permissions */
2049 mode
= ((mode
& ~p
->p_fd
->fd_cmask
) & ALLPERMS
) & ~S_ISTXT
;
2050 VATTR_SET(vap
, va_mode
, mode
& ACCESSPERMS
);
2052 NDINIT(ndp
, LOOKUP
, OP_OPEN
, FOLLOW
| AUDITVNPATH1
, UIO_SYSSPACE
,
2053 CAST_USER_ADDR_T(psfa
->psfaa_openargs
.psfao_path
),
2054 imgp
->ip_vfs_context
);
2056 error
= open1(imgp
->ip_vfs_context
,
2058 psfa
->psfaa_openargs
.psfao_oflag
,
2060 fileproc_alloc_init
, NULL
,
2066 * If there's an error, or we get the right fd by
2067 * accident, then drop out here. This is easier than
2068 * reworking all the open code to preallocate fd
2069 * slots, and internally taking one as an argument.
2071 if (error
|| ival
[0] == psfa
->psfaa_filedes
) {
2077 * If we didn't fall out from an error, we ended up
2078 * with the wrong fd; so now we've got to try to dup2
2079 * it to the right one.
2081 dup2a
.from
= origfd
;
2082 dup2a
.to
= psfa
->psfaa_filedes
;
2085 * The dup2() system call implementation sets
2086 * ival to newfd in the success case, but we
2087 * can ignore that, since if we didn't get the
2088 * fd we wanted, the error will stop us.
2090 error
= dup2(p
, &dup2a
, ival
);
2096 * Finally, close the original fd.
2100 error
= close_nocancel(p
, &ca
, ival
);
2105 struct dup2_args dup2a
;
2107 dup2a
.from
= psfa
->psfaa_filedes
;
2108 dup2a
.to
= psfa
->psfaa_dup2args
.psfad_newfiledes
;
2111 * The dup2() system call implementation sets
2112 * ival to newfd in the success case, but we
2113 * can ignore that, since if we didn't get the
2114 * fd we wanted, the error will stop us.
2116 error
= dup2(p
, &dup2a
, ival
);
2120 case PSFA_FILEPORT_DUP2
: {
2123 struct dup2_args dup2a
;
2124 struct close_nocancel_args ca
;
2126 if (!MACH_PORT_VALID(psfa
->psfaa_fileport
)) {
2131 kr
= ipc_object_copyin(get_task_ipcspace(current_task()),
2132 psfa
->psfaa_fileport
, MACH_MSG_TYPE_COPY_SEND
,
2133 (ipc_object_t
*) &port
, 0, NULL
, IPC_KMSG_FLAGS_ALLOW_IMMOVABLE_SEND
);
2135 if (kr
!= KERN_SUCCESS
) {
2140 error
= fileport_makefd_internal(p
, port
, 0, ival
);
2142 if (IPC_PORT_NULL
!= port
) {
2143 ipc_port_release_send(port
);
2146 if (error
|| ival
[0] == psfa
->psfaa_dup2args
.psfad_newfiledes
) {
2150 dup2a
.from
= ca
.fd
= ival
[0];
2151 dup2a
.to
= psfa
->psfaa_dup2args
.psfad_newfiledes
;
2152 error
= dup2(p
, &dup2a
, ival
);
2157 error
= close_nocancel(p
, &ca
, ival
);
2162 struct close_nocancel_args ca
;
2164 ca
.fd
= psfa
->psfaa_filedes
;
2166 error
= close_nocancel(p
, &ca
, ival
);
2170 case PSFA_INHERIT
: {
2171 struct fcntl_nocancel_args fcntla
;
2174 * Check to see if the descriptor exists, and
2175 * ensure it's -not- marked as close-on-exec.
2177 * Attempting to "inherit" a guarded fd will
2178 * result in a error.
2180 fcntla
.fd
= psfa
->psfaa_filedes
;
2181 fcntla
.cmd
= F_GETFD
;
2182 if ((error
= fcntl_nocancel(p
, &fcntla
, ival
)) != 0) {
2186 if ((ival
[0] & FD_CLOEXEC
) == FD_CLOEXEC
) {
2187 fcntla
.fd
= psfa
->psfaa_filedes
;
2188 fcntla
.cmd
= F_SETFD
;
2189 fcntla
.arg
= ival
[0] & ~FD_CLOEXEC
;
2190 error
= fcntl_nocancel(p
, &fcntla
, ival
);
2197 * Chdir is different, in that it requires the use of
2198 * a path argument, which is normally copied in from
2199 * user space; because of this, we have to support a
2200 * chdir from kernel space that passes an address space
2201 * context of UIO_SYSSPACE, and casts the address
2202 * argument to a user_addr_t.
2204 struct nameidata nd
;
2206 NDINIT(&nd
, LOOKUP
, OP_CHDIR
, FOLLOW
| AUDITVNPATH1
, UIO_SYSSPACE
,
2207 CAST_USER_ADDR_T(psfa
->psfaa_chdirargs
.psfac_path
),
2208 imgp
->ip_vfs_context
);
2210 error
= chdir_internal(p
, imgp
->ip_vfs_context
, &nd
, 0);
2215 struct fchdir_args fchdira
;
2217 fchdira
.fd
= psfa
->psfaa_filedes
;
2219 error
= fchdir(p
, &fchdira
, ival
);
2228 /* All file actions failures are considered fatal, per POSIX */
2231 if (PSFA_OPEN
== psfa
->psfaa_type
) {
2232 DTRACE_PROC1(spawn__open__failure
, uintptr_t,
2233 psfa
->psfaa_openargs
.psfao_path
);
2235 DTRACE_PROC1(spawn__fd__failure
, int, psfa
->psfaa_filedes
);
2241 if (error
!= 0 || (psa_flags
& POSIX_SPAWN_CLOEXEC_DEFAULT
) == 0) {
2246 * If POSIX_SPAWN_CLOEXEC_DEFAULT is set, behave (during
2247 * this spawn only) as if "close on exec" is the default
2248 * disposition of all pre-existing file descriptors. In this case,
2249 * the list of file descriptors mentioned in the file actions
2250 * are the only ones that can be inherited, so mark them now.
2252 * The actual closing part comes later, in fdexec().
2255 for (action
= 0; action
< px_sfap
->psfa_act_count
; action
++) {
2256 _psfa_action_t
*psfa
= &px_sfap
->psfa_act_acts
[action
];
2257 int fd
= psfa
->psfaa_filedes
;
2259 switch (psfa
->psfaa_type
) {
2261 case PSFA_FILEPORT_DUP2
:
2262 fd
= psfa
->psfaa_dup2args
.psfad_newfiledes
;
2266 *fdflags(p
, fd
) |= UF_INHERIT
;
2273 * Although PSFA_FCHDIR does have a file descriptor, it is not
2274 * *creating* one, thus we do not automatically mark it for
2275 * inheritance under POSIX_SPAWN_CLOEXEC_DEFAULT. A client that
2276 * wishes it to be inherited should use the PSFA_INHERIT action
2289 * exec_spawnattr_getmacpolicyinfo
2292 exec_spawnattr_getmacpolicyinfo(const void *macextensions
, const char *policyname
, size_t *lenp
)
2294 const struct _posix_spawn_mac_policy_extensions
*psmx
= macextensions
;
2301 for (i
= 0; i
< psmx
->psmx_count
; i
++) {
2302 const _ps_mac_policy_extension_t
*extension
= &psmx
->psmx_extensions
[i
];
2303 if (strncmp(extension
->policyname
, policyname
, sizeof(extension
->policyname
)) == 0) {
2305 *lenp
= extension
->datalen
;
2307 return extension
->datap
;
2318 spawn_copyin_macpolicyinfo(const struct user__posix_spawn_args_desc
*px_args
, _posix_spawn_mac_policy_extensions_t
*psmxp
)
2320 _posix_spawn_mac_policy_extensions_t psmx
= NULL
;
2327 if (px_args
->mac_extensions_size
< PS_MAC_EXTENSIONS_SIZE(1) ||
2328 px_args
->mac_extensions_size
> PAGE_SIZE
) {
2333 MALLOC(psmx
, _posix_spawn_mac_policy_extensions_t
, px_args
->mac_extensions_size
, M_TEMP
, M_WAITOK
);
2334 if ((error
= copyin(px_args
->mac_extensions
, psmx
, px_args
->mac_extensions_size
)) != 0) {
2338 size_t extsize
= PS_MAC_EXTENSIONS_SIZE(psmx
->psmx_count
);
2339 if (extsize
== 0 || extsize
> px_args
->mac_extensions_size
) {
2344 for (i
= 0; i
< psmx
->psmx_count
; i
++) {
2345 _ps_mac_policy_extension_t
*extension
= &psmx
->psmx_extensions
[i
];
2346 if (extension
->datalen
== 0 || extension
->datalen
> PAGE_SIZE
) {
2352 for (copycnt
= 0; copycnt
< psmx
->psmx_count
; copycnt
++) {
2353 _ps_mac_policy_extension_t
*extension
= &psmx
->psmx_extensions
[copycnt
];
2356 MALLOC(data
, void *, extension
->datalen
, M_TEMP
, M_WAITOK
);
2357 if ((error
= copyin(extension
->data
, data
, extension
->datalen
)) != 0) {
2361 extension
->datap
= data
;
2369 for (i
= 0; i
< copycnt
; i
++) {
2370 FREE(psmx
->psmx_extensions
[i
].datap
, M_TEMP
);
2378 spawn_free_macpolicyinfo(_posix_spawn_mac_policy_extensions_t psmx
)
2385 for (i
= 0; i
< psmx
->psmx_count
; i
++) {
2386 FREE(psmx
->psmx_extensions
[i
].datap
, M_TEMP
);
2390 #endif /* CONFIG_MACF */
2392 #if CONFIG_COALITIONS
2394 spawn_coalitions_release_all(coalition_t coal
[COALITION_NUM_TYPES
])
2396 for (int c
= 0; c
< COALITION_NUM_TYPES
; c
++) {
2398 coalition_remove_active(coal
[c
]);
2399 coalition_release(coal
[c
]);
2407 spawn_validate_persona(struct _posix_spawn_persona_info
*px_persona
)
2410 struct persona
*persona
= NULL
;
2411 int verify
= px_persona
->pspi_flags
& POSIX_SPAWN_PERSONA_FLAGS_VERIFY
;
2413 if (!IOTaskHasEntitlement(current_task(), PERSONA_MGMT_ENTITLEMENT
)) {
2417 if (px_persona
->pspi_flags
& POSIX_SPAWN_PERSONA_GROUPS
) {
2418 if (px_persona
->pspi_ngroups
> NGROUPS_MAX
) {
2423 persona
= persona_lookup(px_persona
->pspi_id
);
2430 if (px_persona
->pspi_flags
& POSIX_SPAWN_PERSONA_UID
) {
2431 if (px_persona
->pspi_uid
!= persona_get_uid(persona
)) {
2436 if (px_persona
->pspi_flags
& POSIX_SPAWN_PERSONA_GID
) {
2437 if (px_persona
->pspi_gid
!= persona_get_gid(persona
)) {
2442 if (px_persona
->pspi_flags
& POSIX_SPAWN_PERSONA_GROUPS
) {
2443 unsigned ngroups
= 0;
2444 gid_t groups
[NGROUPS_MAX
];
2446 if (persona_get_groups(persona
, &ngroups
, groups
,
2447 px_persona
->pspi_ngroups
) != 0) {
2451 if (ngroups
!= px_persona
->pspi_ngroups
) {
2456 if (px_persona
->pspi_groups
[ngroups
] != groups
[ngroups
]) {
2461 if (px_persona
->pspi_gmuid
!= persona_get_gmuid(persona
)) {
2470 persona_put(persona
);
2477 spawn_persona_adopt(proc_t p
, struct _posix_spawn_persona_info
*px_persona
)
2481 struct persona
*persona
= NULL
;
2482 int override
= !!(px_persona
->pspi_flags
& POSIX_SPAWN_PERSONA_FLAGS_OVERRIDE
);
2485 return persona_proc_adopt_id(p
, px_persona
->pspi_id
, NULL
);
2489 * we want to spawn into the given persona, but we want to override
2490 * the kauth with a different UID/GID combo
2492 persona
= persona_lookup(px_persona
->pspi_id
);
2497 cred
= persona_get_cred(persona
);
2503 if (px_persona
->pspi_flags
& POSIX_SPAWN_PERSONA_UID
) {
2504 cred
= kauth_cred_setresuid(cred
,
2505 px_persona
->pspi_uid
,
2506 px_persona
->pspi_uid
,
2507 px_persona
->pspi_uid
,
2511 if (px_persona
->pspi_flags
& POSIX_SPAWN_PERSONA_GID
) {
2512 cred
= kauth_cred_setresgid(cred
,
2513 px_persona
->pspi_gid
,
2514 px_persona
->pspi_gid
,
2515 px_persona
->pspi_gid
);
2518 if (px_persona
->pspi_flags
& POSIX_SPAWN_PERSONA_GROUPS
) {
2519 cred
= kauth_cred_setgroups(cred
,
2520 px_persona
->pspi_groups
,
2521 px_persona
->pspi_ngroups
,
2522 px_persona
->pspi_gmuid
);
2525 ret
= persona_proc_adopt(p
, persona
, cred
);
2528 persona_put(persona
);
2534 extern int legacy_footprint_entitlement_mode
;
2536 proc_legacy_footprint_entitled(proc_t p
, task_t task
, const char *caller
)
2538 #pragma unused(p, caller)
2539 boolean_t legacy_footprint_entitled
;
2541 switch (legacy_footprint_entitlement_mode
) {
2542 case LEGACY_FOOTPRINT_ENTITLEMENT_IGNORE
:
2543 /* the entitlement is ignored */
2545 case LEGACY_FOOTPRINT_ENTITLEMENT_IOS11_ACCT
:
2546 /* the entitlement grants iOS11 legacy accounting */
2547 legacy_footprint_entitled
= IOTaskHasEntitlement(task
,
2548 "com.apple.private.memory.legacy_footprint");
2549 if (legacy_footprint_entitled
) {
2550 task_set_legacy_footprint(task
);
2553 case LEGACY_FOOTPRINT_ENTITLEMENT_LIMIT_INCREASE
:
2554 /* the entitlement grants a footprint limit increase */
2555 legacy_footprint_entitled
= IOTaskHasEntitlement(task
,
2556 "com.apple.private.memory.legacy_footprint");
2557 if (legacy_footprint_entitled
) {
2558 task_set_extra_footprint_limit(task
);
2565 #endif /* __arm64__ */
2568 * Apply a modification on the proc's kauth cred until it converges.
2570 * `update` consumes its argument to return a new kauth cred.
2573 apply_kauth_cred_update(proc_t p
,
2574 kauth_cred_t (^update
)(kauth_cred_t orig_cred
))
2576 kauth_cred_t my_cred
, my_new_cred
;
2578 my_cred
= kauth_cred_proc_ref(p
);
2580 my_new_cred
= update(my_cred
);
2581 if (my_cred
== my_new_cred
) {
2582 kauth_cred_unref(&my_new_cred
);
2586 /* try update cred on proc */
2589 if (p
->p_ucred
== my_cred
) {
2590 /* base pointer didn't change, donate our ref */
2591 p
->p_ucred
= my_new_cred
;
2592 PROC_UPDATE_CREDS_ONPROC(p
);
2593 proc_ucred_unlock(p
);
2595 /* drop p->p_ucred reference */
2596 kauth_cred_unref(&my_cred
);
2600 /* base pointer changed, retry */
2601 my_cred
= p
->p_ucred
;
2602 kauth_cred_ref(my_cred
);
2603 proc_ucred_unlock(p
);
2605 kauth_cred_unref(&my_new_cred
);
2610 spawn_posix_cred_adopt(proc_t p
,
2611 struct _posix_spawn_posix_cred_info
*px_pcred_info
)
2615 if (px_pcred_info
->pspci_flags
& POSIX_SPAWN_POSIX_CRED_GID
) {
2616 struct setgid_args args
= {
2617 .gid
= px_pcred_info
->pspci_gid
,
2619 error
= setgid(p
, &args
, NULL
);
2625 if (px_pcred_info
->pspci_flags
& POSIX_SPAWN_POSIX_CRED_GROUPS
) {
2626 error
= setgroups_internal(p
,
2627 px_pcred_info
->pspci_ngroups
,
2628 px_pcred_info
->pspci_groups
,
2629 px_pcred_info
->pspci_gmuid
);
2635 if (px_pcred_info
->pspci_flags
& POSIX_SPAWN_POSIX_CRED_UID
) {
2636 struct setuid_args args
= {
2637 .uid
= px_pcred_info
->pspci_uid
,
2639 error
= setuid(p
, &args
, NULL
);
2650 * Parameters: uap->pid Pointer to pid return area
2651 * uap->fname File name to exec
2652 * uap->argp Argument list
2653 * uap->envp Environment list
2655 * Returns: 0 Success
2656 * EINVAL Invalid argument
2657 * ENOTSUP Not supported
2658 * ENOEXEC Executable file format error
2659 * exec_activate_image:EINVAL Invalid argument
2660 * exec_activate_image:EACCES Permission denied
2661 * exec_activate_image:EINTR Interrupted function
2662 * exec_activate_image:ENOMEM Not enough space
2663 * exec_activate_image:EFAULT Bad address
2664 * exec_activate_image:ENAMETOOLONG Filename too long
2665 * exec_activate_image:ENOEXEC Executable file format error
2666 * exec_activate_image:ETXTBSY Text file busy [misuse of error code]
2667 * exec_activate_image:EAUTH Image decryption failed
2668 * exec_activate_image:EBADEXEC The executable is corrupt/unknown
2669 * exec_activate_image:???
2670 * mac_execve_enter:???
2672 * TODO: Expect to need __mac_posix_spawn() at some point...
2673 * Handle posix_spawnattr_t
2674 * Handle posix_spawn_file_actions_t
2677 posix_spawn(proc_t ap
, struct posix_spawn_args
*uap
, int32_t *retval
)
2679 proc_t p
= ap
; /* quiet bogus GCC vfork() warning */
2680 user_addr_t pid
= uap
->pid
;
2681 int ival
[2]; /* dummy retval for setpgid() */
2683 struct image_params
*imgp
;
2684 struct vnode_attr
*vap
;
2685 struct vnode_attr
*origvap
;
2686 struct uthread
*uthread
= 0; /* compiler complains if not set to 0*/
2688 int is_64
= IS_64BIT_PROCESS(p
);
2689 struct vfs_context context
;
2690 struct user__posix_spawn_args_desc px_args
;
2691 struct _posix_spawnattr px_sa
;
2692 _posix_spawn_file_actions_t px_sfap
= NULL
;
2693 _posix_spawn_port_actions_t px_spap
= NULL
;
2694 struct __kern_sigaction vec
;
2695 boolean_t spawn_no_exec
= FALSE
;
2696 boolean_t proc_transit_set
= TRUE
;
2697 boolean_t exec_done
= FALSE
;
2698 struct exec_port_actions port_actions
= { };
2699 vm_size_t px_sa_offset
= offsetof(struct _posix_spawnattr
, psa_ports
);
2700 task_t old_task
= current_task();
2701 task_t new_task
= NULL
;
2702 boolean_t should_release_proc_ref
= FALSE
;
2703 void *inherit
= NULL
;
2705 struct _posix_spawn_persona_info
*px_persona
= NULL
;
2707 struct _posix_spawn_posix_cred_info
*px_pcred_info
= NULL
;
2710 * Allocate a big chunk for locals instead of using stack since these
2711 * structures are pretty big.
2713 MALLOC(bufp
, char *, (sizeof(*imgp
) + sizeof(*vap
) + sizeof(*origvap
)), M_TEMP
, M_WAITOK
| M_ZERO
);
2714 imgp
= (struct image_params
*) bufp
;
2719 vap
= (struct vnode_attr
*) (bufp
+ sizeof(*imgp
));
2720 origvap
= (struct vnode_attr
*) (bufp
+ sizeof(*imgp
) + sizeof(*vap
));
2722 /* Initialize the common data in the image_params structure */
2723 imgp
->ip_user_fname
= uap
->path
;
2724 imgp
->ip_user_argv
= uap
->argv
;
2725 imgp
->ip_user_envv
= uap
->envp
;
2726 imgp
->ip_vattr
= vap
;
2727 imgp
->ip_origvattr
= origvap
;
2728 imgp
->ip_vfs_context
= &context
;
2729 imgp
->ip_flags
= (is_64
? IMGPF_WAS_64BIT_ADDR
: IMGPF_NONE
);
2730 imgp
->ip_seg
= (is_64
? UIO_USERSPACE64
: UIO_USERSPACE32
);
2731 imgp
->ip_mac_return
= 0;
2732 imgp
->ip_px_persona
= NULL
;
2733 imgp
->ip_px_pcred_info
= NULL
;
2734 imgp
->ip_cs_error
= OS_REASON_NULL
;
2735 imgp
->ip_simulator_binary
= IMGPF_SB_DEFAULT
;
2737 if (uap
->adesc
!= USER_ADDR_NULL
) {
2739 error
= copyin(uap
->adesc
, &px_args
, sizeof(px_args
));
2741 struct user32__posix_spawn_args_desc px_args32
;
2743 error
= copyin(uap
->adesc
, &px_args32
, sizeof(px_args32
));
2746 * Convert arguments descriptor from external 32 bit
2747 * representation to internal 64 bit representation
2749 px_args
.attr_size
= px_args32
.attr_size
;
2750 px_args
.attrp
= CAST_USER_ADDR_T(px_args32
.attrp
);
2751 px_args
.file_actions_size
= px_args32
.file_actions_size
;
2752 px_args
.file_actions
= CAST_USER_ADDR_T(px_args32
.file_actions
);
2753 px_args
.port_actions_size
= px_args32
.port_actions_size
;
2754 px_args
.port_actions
= CAST_USER_ADDR_T(px_args32
.port_actions
);
2755 px_args
.mac_extensions_size
= px_args32
.mac_extensions_size
;
2756 px_args
.mac_extensions
= CAST_USER_ADDR_T(px_args32
.mac_extensions
);
2757 px_args
.coal_info_size
= px_args32
.coal_info_size
;
2758 px_args
.coal_info
= CAST_USER_ADDR_T(px_args32
.coal_info
);
2759 px_args
.persona_info_size
= px_args32
.persona_info_size
;
2760 px_args
.persona_info
= CAST_USER_ADDR_T(px_args32
.persona_info
);
2761 px_args
.posix_cred_info_size
= px_args32
.posix_cred_info_size
;
2762 px_args
.posix_cred_info
= CAST_USER_ADDR_T(px_args32
.posix_cred_info
);
2768 if (px_args
.attr_size
!= 0) {
2770 * We are not copying the port_actions pointer,
2771 * because we already have it from px_args.
2772 * This is a bit fragile: <rdar://problem/16427422>
2775 if ((error
= copyin(px_args
.attrp
, &px_sa
, px_sa_offset
)) != 0) {
2779 bzero((void *)((unsigned long) &px_sa
+ px_sa_offset
), sizeof(px_sa
) - px_sa_offset
);
2781 imgp
->ip_px_sa
= &px_sa
;
2783 if (px_args
.file_actions_size
!= 0) {
2784 /* Limit file_actions to allowed number of open files */
2785 int maxfa
= (p
->p_limit
? p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
: NOFILE
);
2786 size_t maxfa_size
= PSF_ACTIONS_SIZE(maxfa
);
2787 if (px_args
.file_actions_size
< PSF_ACTIONS_SIZE(1) ||
2788 maxfa_size
== 0 || px_args
.file_actions_size
> maxfa_size
) {
2792 MALLOC(px_sfap
, _posix_spawn_file_actions_t
, px_args
.file_actions_size
, M_TEMP
, M_WAITOK
);
2793 if (px_sfap
== NULL
) {
2797 imgp
->ip_px_sfa
= px_sfap
;
2799 if ((error
= copyin(px_args
.file_actions
, px_sfap
,
2800 px_args
.file_actions_size
)) != 0) {
2804 /* Verify that the action count matches the struct size */
2805 size_t psfsize
= PSF_ACTIONS_SIZE(px_sfap
->psfa_act_count
);
2806 if (psfsize
== 0 || psfsize
!= px_args
.file_actions_size
) {
2811 if (px_args
.port_actions_size
!= 0) {
2812 /* Limit port_actions to one page of data */
2813 if (px_args
.port_actions_size
< PS_PORT_ACTIONS_SIZE(1) ||
2814 px_args
.port_actions_size
> PAGE_SIZE
) {
2819 MALLOC(px_spap
, _posix_spawn_port_actions_t
,
2820 px_args
.port_actions_size
, M_TEMP
, M_WAITOK
);
2821 if (px_spap
== NULL
) {
2825 imgp
->ip_px_spa
= px_spap
;
2827 if ((error
= copyin(px_args
.port_actions
, px_spap
,
2828 px_args
.port_actions_size
)) != 0) {
2832 /* Verify that the action count matches the struct size */
2833 size_t pasize
= PS_PORT_ACTIONS_SIZE(px_spap
->pspa_count
);
2834 if (pasize
== 0 || pasize
!= px_args
.port_actions_size
) {
2840 /* copy in the persona info */
2841 if (px_args
.persona_info_size
!= 0 && px_args
.persona_info
!= 0) {
2842 /* for now, we need the exact same struct in user space */
2843 if (px_args
.persona_info_size
!= sizeof(*px_persona
)) {
2848 MALLOC(px_persona
, struct _posix_spawn_persona_info
*, px_args
.persona_info_size
, M_TEMP
, M_WAITOK
| M_ZERO
);
2849 if (px_persona
== NULL
) {
2853 imgp
->ip_px_persona
= px_persona
;
2855 if ((error
= copyin(px_args
.persona_info
, px_persona
,
2856 px_args
.persona_info_size
)) != 0) {
2859 if ((error
= spawn_validate_persona(px_persona
)) != 0) {
2864 /* copy in the posix cred info */
2865 if (px_args
.posix_cred_info_size
!= 0 && px_args
.posix_cred_info
!= 0) {
2866 /* for now, we need the exact same struct in user space */
2867 if (px_args
.posix_cred_info_size
!= sizeof(*px_pcred_info
)) {
2872 if (!kauth_cred_issuser(kauth_cred_get())) {
2877 MALLOC(px_pcred_info
, struct _posix_spawn_posix_cred_info
*,
2878 px_args
.posix_cred_info_size
, M_TEMP
, M_WAITOK
| M_ZERO
);
2879 if (px_pcred_info
== NULL
) {
2883 imgp
->ip_px_pcred_info
= px_pcred_info
;
2885 if ((error
= copyin(px_args
.posix_cred_info
, px_pcred_info
,
2886 px_args
.posix_cred_info_size
)) != 0) {
2890 if (px_pcred_info
->pspci_flags
& POSIX_SPAWN_POSIX_CRED_GROUPS
) {
2891 if (px_pcred_info
->pspci_ngroups
> NGROUPS_MAX
) {
2898 if (px_args
.mac_extensions_size
!= 0) {
2899 if ((error
= spawn_copyin_macpolicyinfo(&px_args
, (_posix_spawn_mac_policy_extensions_t
*)&imgp
->ip_px_smpx
)) != 0) {
2903 #endif /* CONFIG_MACF */
2906 /* set uthread to parent */
2907 uthread
= get_bsdthread_info(current_thread());
2910 * <rdar://6640530>; this does not result in a behaviour change
2911 * relative to Leopard, so there should not be any existing code
2912 * which depends on it.
2914 if (uthread
->uu_flag
& UT_VFORK
) {
2919 if (imgp
->ip_px_sa
!= NULL
) {
2920 struct _posix_spawnattr
*psa
= (struct _posix_spawnattr
*) imgp
->ip_px_sa
;
2921 if ((error
= exec_validate_spawnattr_policy(psa
->psa_apptype
)) != 0) {
2927 * If we don't have the extension flag that turns "posix_spawn()"
2928 * into "execve() with options", then we will be creating a new
2929 * process which does not inherit memory from the parent process,
2930 * which is one of the most expensive things about using fork()
2933 if (imgp
->ip_px_sa
== NULL
|| !(px_sa
.psa_flags
& POSIX_SPAWN_SETEXEC
)) {
2934 /* Set the new task's coalition, if it is requested. */
2935 coalition_t coal
[COALITION_NUM_TYPES
] = { COALITION_NULL
};
2936 #if CONFIG_COALITIONS
2938 kern_return_t kr
= KERN_SUCCESS
;
2939 struct _posix_spawn_coalition_info coal_info
;
2940 int coal_role
[COALITION_NUM_TYPES
];
2942 if (imgp
->ip_px_sa
== NULL
|| !px_args
.coal_info
) {
2946 memset(&coal_info
, 0, sizeof(coal_info
));
2948 if (px_args
.coal_info_size
> sizeof(coal_info
)) {
2949 px_args
.coal_info_size
= sizeof(coal_info
);
2951 error
= copyin(px_args
.coal_info
,
2952 &coal_info
, px_args
.coal_info_size
);
2958 for (i
= 0; i
< COALITION_NUM_TYPES
; i
++) {
2959 uint64_t cid
= coal_info
.psci_info
[i
].psci_id
;
2962 * don't allow tasks which are not in a
2963 * privileged coalition to spawn processes
2964 * into coalitions other than their own
2966 if (!task_is_in_privileged_coalition(p
->task
, i
)) {
2967 coal_dbg("ERROR: %d not in privilegd "
2968 "coalition of type %d",
2970 spawn_coalitions_release_all(coal
);
2975 coal_dbg("searching for coalition id:%llu", cid
);
2977 * take a reference and activation on the
2978 * coalition to guard against free-while-spawn
2981 coal
[i
] = coalition_find_and_activate_by_id(cid
);
2982 if (coal
[i
] == COALITION_NULL
) {
2983 coal_dbg("could not find coalition id:%llu "
2984 "(perhaps it has been terminated or reaped)", cid
);
2986 * release any other coalition's we
2987 * may have a reference to
2989 spawn_coalitions_release_all(coal
);
2993 if (coalition_type(coal
[i
]) != i
) {
2994 coal_dbg("coalition with id:%lld is not of type:%d"
2995 " (it's type:%d)", cid
, i
, coalition_type(coal
[i
]));
2999 coal_role
[i
] = coal_info
.psci_info
[i
].psci_role
;
3003 if (ncoals
< COALITION_NUM_TYPES
) {
3005 * If the user is attempting to spawn into a subset of
3006 * the known coalition types, then make sure they have
3007 * _at_least_ specified a resource coalition. If not,
3008 * the following fork1() call will implicitly force an
3009 * inheritance from 'p' and won't actually spawn the
3010 * new task into the coalitions the user specified.
3011 * (also the call to coalitions_set_roles will panic)
3013 if (coal
[COALITION_TYPE_RESOURCE
] == COALITION_NULL
) {
3014 spawn_coalitions_release_all(coal
);
3020 #endif /* CONFIG_COALITIONS */
3023 * note that this will implicitly inherit the
3024 * caller's persona (if it exists)
3026 error
= fork1(p
, &imgp
->ip_new_thread
, PROC_CREATE_SPAWN
, coal
);
3027 /* returns a thread and task reference */
3030 new_task
= get_threadtask(imgp
->ip_new_thread
);
3032 #if CONFIG_COALITIONS
3033 /* set the roles of this task within each given coalition */
3035 kr
= coalitions_set_roles(coal
, new_task
, coal_role
);
3036 if (kr
!= KERN_SUCCESS
) {
3039 if (kdebug_debugid_enabled(MACHDBG_CODE(DBG_MACH_COALITION
,
3040 MACH_COALITION_ADOPT
))) {
3041 for (i
= 0; i
< COALITION_NUM_TYPES
; i
++) {
3042 if (coal
[i
] != COALITION_NULL
) {
3044 * On 32-bit targets, uniqueid
3045 * will get truncated to 32 bits
3047 KDBG_RELEASE(MACHDBG_CODE(
3049 MACH_COALITION_ADOPT
),
3050 coalition_id(coal
[i
]),
3051 get_task_uniqueid(new_task
));
3057 /* drop our references and activations - fork1() now holds them */
3058 spawn_coalitions_release_all(coal
);
3059 #endif /* CONFIG_COALITIONS */
3063 imgp
->ip_flags
|= IMGPF_SPAWN
; /* spawn w/o exec */
3064 spawn_no_exec
= TRUE
; /* used in later tests */
3067 * For execve case, create a new task and thread
3068 * which points to current_proc. The current_proc will point
3069 * to the new task after image activation and proc ref drain.
3071 * proc (current_proc) <----- old_task (current_task)
3074 * | ----------------------------------
3076 * --------- new_task (task marked as TF_EXEC_COPY)
3078 * After image activation, the proc will point to the new task
3079 * and would look like following.
3081 * proc (current_proc) <----- old_task (current_task, marked as TPF_DID_EXEC)
3084 * | ----------> new_task
3088 * During exec any transition from new_task -> proc is fine, but don't allow
3089 * transition from proc->task, since it will modify old_task.
3091 imgp
->ip_new_thread
= fork_create_child(old_task
,
3096 task_get_64bit_data(old_task
),
3098 /* task and thread ref returned by fork_create_child */
3099 if (imgp
->ip_new_thread
== NULL
) {
3104 new_task
= get_threadtask(imgp
->ip_new_thread
);
3105 imgp
->ip_flags
|= IMGPF_EXEC
;
3108 if (spawn_no_exec
) {
3109 p
= (proc_t
)get_bsdthreadtask_info(imgp
->ip_new_thread
);
3112 * We had to wait until this point before firing the
3113 * proc:::create probe, otherwise p would not point to the
3116 DTRACE_PROC1(create
, proc_t
, p
);
3120 context
.vc_thread
= imgp
->ip_new_thread
;
3121 context
.vc_ucred
= p
->p_ucred
; /* XXX must NOT be kauth_cred_get() */
3124 * Post fdcopy(), pre exec_handle_sugid() - this is where we want
3125 * to handle the file_actions. Since vfork() also ends up setting
3126 * us into the parent process group, and saved off the signal flags,
3127 * this is also where we want to handle the spawn flags.
3130 /* Has spawn file actions? */
3131 if (imgp
->ip_px_sfa
!= NULL
) {
3133 * The POSIX_SPAWN_CLOEXEC_DEFAULT flag
3134 * is handled in exec_handle_file_actions().
3136 if ((error
= exec_handle_file_actions(imgp
,
3137 imgp
->ip_px_sa
!= NULL
? px_sa
.psa_flags
: 0)) != 0) {
3142 /* Has spawn port actions? */
3143 if (imgp
->ip_px_spa
!= NULL
) {
3144 if ((error
= exec_handle_port_actions(imgp
, &port_actions
)) != 0) {
3149 /* Has spawn attr? */
3150 if (imgp
->ip_px_sa
!= NULL
) {
3152 * Reset UID/GID to parent's RUID/RGID; This works only
3153 * because the operation occurs *after* the vfork() and
3154 * before the call to exec_handle_sugid() by the image
3155 * activator called from exec_activate_image(). POSIX
3156 * requires that any setuid/setgid bits on the process
3157 * image will take precedence over the spawn attributes
3160 * Modifications to p_ucred must be guarded using the
3161 * proc's ucred lock. This prevents others from accessing
3162 * a garbage credential.
3164 if (px_sa
.psa_flags
& POSIX_SPAWN_RESETIDS
) {
3165 apply_kauth_cred_update(p
, ^kauth_cred_t (kauth_cred_t my_cred
){
3166 return kauth_cred_setuidgid(my_cred
,
3167 kauth_cred_getruid(my_cred
),
3168 kauth_cred_getrgid(my_cred
));
3172 if (imgp
->ip_px_pcred_info
) {
3173 if (!spawn_no_exec
) {
3178 error
= spawn_posix_cred_adopt(p
, imgp
->ip_px_pcred_info
);
3185 if (imgp
->ip_px_persona
!= NULL
) {
3186 if (!spawn_no_exec
) {
3192 * If we were asked to spawn a process into a new persona,
3193 * do the credential switch now (which may override the UID/GID
3194 * inherit done just above). It's important to do this switch
3195 * before image activation both for reasons stated above, and
3196 * to ensure that the new persona has access to the image/file
3199 error
= spawn_persona_adopt(p
, imgp
->ip_px_persona
);
3204 #endif /* CONFIG_PERSONAS */
3207 * Disable ASLR for the spawned process.
3209 * But only do so if we are not embedded + RELEASE.
3210 * While embedded allows for a boot-arg (-disable_aslr)
3211 * to deal with this (which itself is only honored on
3212 * DEVELOPMENT or DEBUG builds of xnu), it is often
3213 * useful or necessary to disable ASLR on a per-process
3214 * basis for unit testing and debugging.
3216 if (px_sa
.psa_flags
& _POSIX_SPAWN_DISABLE_ASLR
) {
3217 OSBitOrAtomic(P_DISABLE_ASLR
, &p
->p_flag
);
3219 #endif /* !SECURE_KERNEL */
3221 /* Randomize high bits of ASLR slide */
3222 if (px_sa
.psa_flags
& _POSIX_SPAWN_HIGH_BITS_ASLR
) {
3223 imgp
->ip_flags
|= IMGPF_HIGH_BITS_ASLR
;
3228 * Forcibly disallow execution from data pages for the spawned process
3229 * even if it would otherwise be permitted by the architecture default.
3231 if (px_sa
.psa_flags
& _POSIX_SPAWN_ALLOW_DATA_EXEC
) {
3232 imgp
->ip_flags
|= IMGPF_ALLOW_DATA_EXEC
;
3234 #endif /* !SECURE_KERNEL */
3236 if ((px_sa
.psa_apptype
& POSIX_SPAWN_PROC_TYPE_MASK
) ==
3237 POSIX_SPAWN_PROC_TYPE_DRIVER
) {
3238 imgp
->ip_flags
|= IMGPF_DRIVER
;
3243 * Disable ASLR during image activation. This occurs either if the
3244 * _POSIX_SPAWN_DISABLE_ASLR attribute was found above or if
3245 * P_DISABLE_ASLR was inherited from the parent process.
3247 if (p
->p_flag
& P_DISABLE_ASLR
) {
3248 imgp
->ip_flags
|= IMGPF_DISABLE_ASLR
;
3252 * Clear transition flag so we won't hang if exec_activate_image() causes
3253 * an automount (and launchd does a proc sysctl to service it).
3255 * <rdar://problem/6848672>, <rdar://problem/5959568>.
3257 if (spawn_no_exec
) {
3258 proc_transend(p
, 0);
3259 proc_transit_set
= 0;
3262 #if MAC_SPAWN /* XXX */
3263 if (uap
->mac_p
!= USER_ADDR_NULL
) {
3264 error
= mac_execve_enter(uap
->mac_p
, imgp
);
3272 * Activate the image
3274 error
= exec_activate_image(imgp
);
3275 #if defined(HAS_APPLE_PAC)
3276 ml_task_set_disable_user_jop(new_task
, imgp
->ip_flags
& IMGPF_NOJOP
? TRUE
: FALSE
);
3277 ml_thread_set_disable_user_jop(imgp
->ip_new_thread
, imgp
->ip_flags
& IMGPF_NOJOP
? TRUE
: FALSE
);
3280 if (error
== 0 && !spawn_no_exec
) {
3281 p
= proc_exec_switch_task(p
, old_task
, new_task
, imgp
->ip_new_thread
);
3282 /* proc ref returned */
3283 should_release_proc_ref
= TRUE
;
3286 * Need to transfer pending watch port boosts to the new task while still making
3287 * sure that the old task remains in the importance linkage. Create an importance
3288 * linkage from old task to new task, then switch the task importance base
3289 * of old task and new task. After the switch the port watch boost will be
3290 * boosting the new task and new task will be donating importance to old task.
3292 inherit
= ipc_importance_exec_switch_task(old_task
, new_task
);
3296 /* process completed the exec */
3298 } else if (error
== -1) {
3299 /* Image not claimed by any activator? */
3303 if (!error
&& imgp
->ip_px_sa
!= NULL
) {
3304 thread_t child_thread
= imgp
->ip_new_thread
;
3305 uthread_t child_uthread
= get_bsdthread_info(child_thread
);
3308 * Because of POSIX_SPAWN_SETEXEC, we need to handle this after image
3309 * activation, else when image activation fails (before the point of no
3310 * return) would leave the parent process in a modified state.
3312 if (px_sa
.psa_flags
& POSIX_SPAWN_SETPGROUP
) {
3313 struct setpgid_args spga
;
3314 spga
.pid
= p
->p_pid
;
3315 spga
.pgid
= px_sa
.psa_pgroup
;
3317 * Effectively, call setpgid() system call; works
3318 * because there are no pointer arguments.
3320 if ((error
= setpgid(p
, &spga
, ival
)) != 0) {
3325 if (px_sa
.psa_flags
& POSIX_SPAWN_SETSID
) {
3326 error
= setsid_internal(p
);
3333 * If we have a spawn attr, and it contains signal related flags,
3334 * the we need to process them in the "context" of the new child
3335 * process, so we have to process it following image activation,
3336 * prior to making the thread runnable in user space. This is
3337 * necessitated by some signal information being per-thread rather
3338 * than per-process, and we don't have the new allocation in hand
3339 * until after the image is activated.
3343 * Mask a list of signals, instead of them being unmasked, if
3344 * they were unmasked in the parent; note that some signals
3347 if (px_sa
.psa_flags
& POSIX_SPAWN_SETSIGMASK
) {
3348 child_uthread
->uu_sigmask
= (px_sa
.psa_sigmask
& ~sigcantmask
);
3351 * Default a list of signals instead of ignoring them, if
3352 * they were ignored in the parent. Note that we pass
3353 * spawn_no_exec to setsigvec() to indicate that we called
3354 * fork1() and therefore do not need to call proc_signalstart()
3357 if (px_sa
.psa_flags
& POSIX_SPAWN_SETSIGDEF
) {
3358 vec
.sa_handler
= SIG_DFL
;
3362 for (sig
= 1; sig
< NSIG
; sig
++) {
3363 if (px_sa
.psa_sigdefault
& (1 << (sig
- 1))) {
3364 error
= setsigvec(p
, child_thread
, sig
, &vec
, spawn_no_exec
);
3370 * Activate the CPU usage monitor, if requested. This is done via a task-wide, per-thread CPU
3371 * usage limit, which will generate a resource exceeded exception if any one thread exceeds the
3374 * Userland gives us interval in seconds, and the kernel SPI expects nanoseconds.
3376 if (px_sa
.psa_cpumonitor_percent
!= 0) {
3378 * Always treat a CPU monitor activation coming from spawn as entitled. Requiring
3379 * an entitlement to configure the monitor a certain way seems silly, since
3380 * whomever is turning it on could just as easily choose not to do so.
3382 error
= proc_set_task_ruse_cpu(p
->task
,
3383 TASK_POLICY_RESOURCE_ATTRIBUTE_NOTIFY_EXC
,
3384 px_sa
.psa_cpumonitor_percent
,
3385 px_sa
.psa_cpumonitor_interval
* NSEC_PER_SEC
,
3390 if (px_pcred_info
&&
3391 (px_pcred_info
->pspci_flags
& POSIX_SPAWN_POSIX_CRED_LOGIN
)) {
3393 * setlogin() must happen after setsid()
3395 setlogin_internal(p
, px_pcred_info
->pspci_login
);
3402 /* reset delay idle sleep status if set */
3403 #if !CONFIG_EMBEDDED
3404 if ((p
->p_flag
& P_DELAYIDLESLEEP
) == P_DELAYIDLESLEEP
) {
3405 OSBitAndAtomic(~((uint32_t)P_DELAYIDLESLEEP
), &p
->p_flag
);
3407 #endif /* !CONFIG_EMBEDDED */
3408 /* upon successful spawn, re/set the proc control state */
3409 if (imgp
->ip_px_sa
!= NULL
) {
3410 switch (px_sa
.psa_pcontrol
) {
3411 case POSIX_SPAWN_PCONTROL_THROTTLE
:
3412 p
->p_pcaction
= P_PCTHROTTLE
;
3414 case POSIX_SPAWN_PCONTROL_SUSPEND
:
3415 p
->p_pcaction
= P_PCSUSP
;
3417 case POSIX_SPAWN_PCONTROL_KILL
:
3418 p
->p_pcaction
= P_PCKILL
;
3420 case POSIX_SPAWN_PCONTROL_NONE
:
3427 exec_resettextvp(p
, imgp
);
3429 #if CONFIG_MEMORYSTATUS
3430 /* Set jetsam priority for DriverKit processes */
3431 if (px_sa
.psa_apptype
== POSIX_SPAWN_PROC_TYPE_DRIVER
) {
3432 px_sa
.psa_priority
= JETSAM_PRIORITY_DRIVER_APPLE
;
3435 /* Has jetsam attributes? */
3436 if (imgp
->ip_px_sa
!= NULL
&& (px_sa
.psa_jetsam_flags
& POSIX_SPAWN_JETSAM_SET
)) {
3438 * With 2-level high-water-mark support, POSIX_SPAWN_JETSAM_HIWATER_BACKGROUND is no
3439 * longer relevant, as background limits are described via the inactive limit slots.
3441 * That said, however, if the POSIX_SPAWN_JETSAM_HIWATER_BACKGROUND is passed in,
3442 * we attempt to mimic previous behavior by forcing the BG limit data into the
3443 * inactive/non-fatal mode and force the active slots to hold system_wide/fatal mode.
3446 if (px_sa
.psa_jetsam_flags
& POSIX_SPAWN_JETSAM_HIWATER_BACKGROUND
) {
3447 memorystatus_update(p
, px_sa
.psa_priority
, 0, FALSE
, /* assertion priority */
3448 (px_sa
.psa_jetsam_flags
& POSIX_SPAWN_JETSAM_USE_EFFECTIVE_PRIORITY
),
3451 px_sa
.psa_memlimit_inactive
, FALSE
);
3453 memorystatus_update(p
, px_sa
.psa_priority
, 0, FALSE
, /* assertion priority */
3454 (px_sa
.psa_jetsam_flags
& POSIX_SPAWN_JETSAM_USE_EFFECTIVE_PRIORITY
),
3456 px_sa
.psa_memlimit_active
,
3457 (px_sa
.psa_jetsam_flags
& POSIX_SPAWN_JETSAM_MEMLIMIT_ACTIVE_FATAL
),
3458 px_sa
.psa_memlimit_inactive
,
3459 (px_sa
.psa_jetsam_flags
& POSIX_SPAWN_JETSAM_MEMLIMIT_INACTIVE_FATAL
));
3463 /* Has jetsam relaunch behavior? */
3464 if (imgp
->ip_px_sa
!= NULL
&& (px_sa
.psa_jetsam_flags
& POSIX_SPAWN_JETSAM_RELAUNCH_BEHAVIOR_MASK
)) {
3466 * Launchd has passed in data indicating the behavior of this process in response to jetsam.
3467 * This data would be used by the jetsam subsystem to determine the position and protection
3468 * offered to this process on dirty -> clean transitions.
3470 int relaunch_flags
= P_MEMSTAT_RELAUNCH_UNKNOWN
;
3471 switch (px_sa
.psa_jetsam_flags
& POSIX_SPAWN_JETSAM_RELAUNCH_BEHAVIOR_MASK
) {
3472 case POSIX_SPAWN_JETSAM_RELAUNCH_BEHAVIOR_LOW
:
3473 relaunch_flags
= P_MEMSTAT_RELAUNCH_LOW
;
3475 case POSIX_SPAWN_JETSAM_RELAUNCH_BEHAVIOR_MED
:
3476 relaunch_flags
= P_MEMSTAT_RELAUNCH_MED
;
3478 case POSIX_SPAWN_JETSAM_RELAUNCH_BEHAVIOR_HIGH
:
3479 relaunch_flags
= P_MEMSTAT_RELAUNCH_HIGH
;
3484 memorystatus_relaunch_flags_update(p
, relaunch_flags
);
3487 #endif /* CONFIG_MEMORYSTATUS */
3488 if (imgp
->ip_px_sa
!= NULL
&& px_sa
.psa_thread_limit
> 0) {
3489 task_set_thread_limit(new_task
, (uint16_t)px_sa
.psa_thread_limit
);
3494 * If we successfully called fork1(), we always need to do this;
3495 * we identify this case by noting the IMGPF_SPAWN flag. This is
3496 * because we come back from that call with signals blocked in the
3497 * child, and we have to unblock them, but we want to wait until
3498 * after we've performed any spawn actions. This has to happen
3499 * before check_for_signature(), which uses psignal.
3501 if (spawn_no_exec
) {
3502 if (proc_transit_set
) {
3503 proc_transend(p
, 0);
3507 * Drop the signal lock on the child which was taken on our
3508 * behalf by forkproc()/cloneproc() to prevent signals being
3509 * received by the child in a partially constructed state.
3511 proc_signalend(p
, 0);
3513 /* flag the 'fork' has occurred */
3514 proc_knote(p
->p_pptr
, NOTE_FORK
| p
->p_pid
);
3517 /* flag exec has occurred, notify only if it has not failed due to FP Key error */
3518 if (!error
&& ((p
->p_lflag
& P_LTERM_DECRYPTFAIL
) == 0)) {
3519 proc_knote(p
, NOTE_EXEC
);
3525 * We need to initialize the bank context behind the protection of
3526 * the proc_trans lock to prevent a race with exit. We can't do this during
3527 * exec_activate_image because task_bank_init checks entitlements that
3528 * aren't loaded until subsequent calls (including exec_resettextvp).
3530 error
= proc_transstart(p
, 0, 0);
3533 task_bank_init(new_task
);
3534 proc_transend(p
, 0);
3538 proc_legacy_footprint_entitled(p
, new_task
, __FUNCTION__
);
3539 #endif /* __arm64__ */
3542 /* Inherit task role from old task to new task for exec */
3543 if (error
== 0 && !spawn_no_exec
) {
3544 proc_inherit_task_role(new_task
, old_task
);
3550 * Check to see if we need to trigger an arcade upcall AST now
3551 * that the vnode has been reset on the task.
3553 arcade_prepare(new_task
, imgp
->ip_new_thread
);
3555 #endif /* CONFIG_ARCADE */
3557 /* Clear the initial wait on the thread before handling spawn policy */
3558 if (imgp
&& imgp
->ip_new_thread
) {
3559 task_clear_return_wait(get_threadtask(imgp
->ip_new_thread
), TCRW_CLEAR_INITIAL_WAIT
);
3563 * Apply the spawnattr policy, apptype (which primes the task for importance donation),
3564 * and bind any portwatch ports to the new task.
3565 * This must be done after the exec so that the child's thread is ready,
3566 * and after the in transit state has been released, because priority is
3567 * dropped here so we need to be prepared for a potentially long preemption interval
3569 * TODO: Consider splitting this up into separate phases
3571 if (error
== 0 && imgp
->ip_px_sa
!= NULL
) {
3572 struct _posix_spawnattr
*psa
= (struct _posix_spawnattr
*) imgp
->ip_px_sa
;
3574 error
= exec_handle_spawnattr_policy(p
, imgp
->ip_new_thread
, psa
->psa_apptype
, psa
->psa_qos_clamp
,
3575 psa
->psa_darwin_role
, &port_actions
);
3578 /* Transfer the turnstile watchport boost to new task if in exec */
3579 if (error
== 0 && !spawn_no_exec
) {
3580 task_transfer_turnstile_watchports(old_task
, new_task
, imgp
->ip_new_thread
);
3584 * Apply the requested maximum address.
3586 if (error
== 0 && imgp
->ip_px_sa
!= NULL
) {
3587 struct _posix_spawnattr
*psa
= (struct _posix_spawnattr
*) imgp
->ip_px_sa
;
3589 if (psa
->psa_max_addr
) {
3590 vm_map_set_max_addr(get_task_map(new_task
), psa
->psa_max_addr
);
3595 /* Apply the main thread qos */
3596 thread_t main_thread
= imgp
->ip_new_thread
;
3597 task_set_main_thread_qos(new_task
, main_thread
);
3601 * Processes with the MAP_JIT entitlement are permitted to have
3604 if (mac_proc_check_map_anon(p
, 0, 0, 0, MAP_JIT
, NULL
) == 0) {
3605 vm_map_set_jumbo(get_task_map(new_task
));
3606 vm_map_set_jit_entitled(get_task_map(new_task
));
3608 #endif /* CONFIG_MACF */
3612 * Release any ports we kept around for binding to the new task
3613 * We need to release the rights even if the posix_spawn has failed.
3615 if (imgp
->ip_px_spa
!= NULL
) {
3616 exec_port_actions_destroy(&port_actions
);
3620 * We have to delay operations which might throw a signal until after
3621 * the signals have been unblocked; however, we want that to happen
3622 * after exec_resettextvp() so that the textvp is correct when they
3626 error
= check_for_signature(p
, imgp
);
3629 * Pay for our earlier safety; deliver the delayed signals from
3630 * the incomplete spawn process now that it's complete.
3632 if (imgp
!= NULL
&& spawn_no_exec
&& (p
->p_lflag
& P_LTRACED
)) {
3633 psignal_vfork(p
, p
->task
, imgp
->ip_new_thread
, SIGTRAP
);
3636 if (error
== 0 && !spawn_no_exec
) {
3637 KDBG(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXEC
),
3645 vnode_put(imgp
->ip_vp
);
3647 if (imgp
->ip_scriptvp
) {
3648 vnode_put(imgp
->ip_scriptvp
);
3650 if (imgp
->ip_strings
) {
3651 execargs_free(imgp
);
3653 if (imgp
->ip_px_sfa
!= NULL
) {
3654 FREE(imgp
->ip_px_sfa
, M_TEMP
);
3656 if (imgp
->ip_px_spa
!= NULL
) {
3657 FREE(imgp
->ip_px_spa
, M_TEMP
);
3660 if (imgp
->ip_px_persona
!= NULL
) {
3661 FREE(imgp
->ip_px_persona
, M_TEMP
);
3664 if (imgp
->ip_px_pcred_info
!= NULL
) {
3665 FREE(imgp
->ip_px_pcred_info
, M_TEMP
);
3668 if (imgp
->ip_px_smpx
!= NULL
) {
3669 spawn_free_macpolicyinfo(imgp
->ip_px_smpx
);
3671 if (imgp
->ip_execlabelp
) {
3672 mac_cred_label_free(imgp
->ip_execlabelp
);
3674 if (imgp
->ip_scriptlabelp
) {
3675 mac_vnode_label_free(imgp
->ip_scriptlabelp
);
3677 if (imgp
->ip_cs_error
!= OS_REASON_NULL
) {
3678 os_reason_free(imgp
->ip_cs_error
);
3679 imgp
->ip_cs_error
= OS_REASON_NULL
;
3685 if (spawn_no_exec
) {
3687 * In the original DTrace reference implementation,
3688 * posix_spawn() was a libc routine that just
3689 * did vfork(2) then exec(2). Thus the proc::: probes
3690 * are very fork/exec oriented. The details of this
3691 * in-kernel implementation of posix_spawn() is different
3692 * (while producing the same process-observable effects)
3693 * particularly w.r.t. errors, and which thread/process
3694 * is constructing what on behalf of whom.
3697 DTRACE_PROC1(spawn__failure
, int, error
);
3699 DTRACE_PROC(spawn__success
);
3701 * Some DTrace scripts, e.g. newproc.d in
3702 * /usr/bin, rely on the the 'exec-success'
3703 * probe being fired in the child after the
3704 * new process image has been constructed
3705 * in order to determine the associated pid.
3707 * So, even though the parent built the image
3708 * here, for compatibility, mark the new thread
3709 * so 'exec-success' fires on it as it leaves
3712 dtrace_thread_didexec(imgp
->ip_new_thread
);
3716 DTRACE_PROC1(exec__failure
, int, error
);
3718 dtrace_thread_didexec(imgp
->ip_new_thread
);
3722 if ((dtrace_proc_waitfor_hook
= dtrace_proc_waitfor_exec_ptr
) != NULL
) {
3723 (*dtrace_proc_waitfor_hook
)(p
);
3728 if (!error
&& AUDIT_ENABLED() && p
) {
3729 /* Add the CDHash of the new process to the audit record */
3730 uint8_t *cdhash
= cs_get_cdhash(p
);
3732 AUDIT_ARG(data
, cdhash
, sizeof(uint8_t), CS_CDHASH_LEN
);
3738 * clear bsd_info from old task if it did exec.
3740 if (task_did_exec(old_task
)) {
3741 set_bsdtask_info(old_task
, NULL
);
3744 /* clear bsd_info from new task and terminate it if exec failed */
3745 if (new_task
!= NULL
&& task_is_exec_copy(new_task
)) {
3746 set_bsdtask_info(new_task
, NULL
);
3747 task_terminate_internal(new_task
);
3750 /* Return to both the parent and the child? */
3751 if (imgp
!= NULL
&& spawn_no_exec
) {
3753 * If the parent wants the pid, copy it out
3755 if (pid
!= USER_ADDR_NULL
) {
3756 _Static_assert(sizeof(p
->p_pid
) == 4, "posix_spawn() assumes a 32-bit pid_t");
3757 bool aligned
= (pid
& 3) == 0;
3759 (void)copyout_atomic32(p
->p_pid
, pid
);
3761 (void)suword(pid
, p
->p_pid
);
3767 * If we had an error, perform an internal reap ; this is
3768 * entirely safe, as we have a real process backing us.
3772 p
->p_listflag
|= P_LIST_DEADPARENT
;
3775 /* make sure no one else has killed it off... */
3776 if (p
->p_stat
!= SZOMB
&& p
->exit_thread
== NULL
) {
3777 p
->exit_thread
= current_thread();
3779 exit1(p
, 1, (int *)NULL
);
3781 /* someone is doing it for us; just skip it */
3788 * Do not terminate the current task, if proc_exec_switch_task did not
3789 * switch the tasks, terminating the current task without the switch would
3790 * result in loosing the SIGKILL status.
3792 if (task_did_exec(old_task
)) {
3793 /* Terminate the current task, since exec will start in new task */
3794 task_terminate_internal(old_task
);
3797 /* Release the thread ref returned by fork_create_child/fork1 */
3798 if (imgp
!= NULL
&& imgp
->ip_new_thread
) {
3799 /* wake up the new thread */
3800 task_clear_return_wait(get_threadtask(imgp
->ip_new_thread
), TCRW_CLEAR_FINAL_WAIT
);
3801 thread_deallocate(imgp
->ip_new_thread
);
3802 imgp
->ip_new_thread
= NULL
;
3805 /* Release the ref returned by fork_create_child/fork1 */
3807 task_deallocate(new_task
);
3811 if (should_release_proc_ref
) {
3819 if (inherit
!= NULL
) {
3820 ipc_importance_release(inherit
);
3827 * proc_exec_switch_task
3829 * Parameters: p proc
3830 * old_task task before exec
3831 * new_task task after exec
3832 * new_thread thread in new task
3836 * Note: The function will switch the task pointer of proc
3837 * from old task to new task. The switch needs to happen
3838 * after draining all proc refs and inside a proc translock.
3839 * In the case of failure to switch the task, which might happen
3840 * if the process received a SIGKILL or jetsam killed it, it will make
3841 * sure that the new tasks terminates. User proc ref returned
3844 * This function is called after point of no return, in the case
3845 * failure to switch, it will terminate the new task and swallow the
3846 * error and let the terminated process complete exec and die.
3849 proc_exec_switch_task(proc_t p
, task_t old_task
, task_t new_task
, thread_t new_thread
)
3852 boolean_t task_active
;
3853 boolean_t proc_active
;
3854 boolean_t thread_active
;
3855 thread_t old_thread
= current_thread();
3858 * Switch the task pointer of proc to new task.
3859 * Before switching the task, wait for proc_refdrain.
3860 * After the switch happens, the proc can disappear,
3861 * take a ref before it disappears. Waiting for
3862 * proc_refdrain in exec will block all other threads
3863 * trying to take a proc ref, boost the current thread
3864 * to avoid priority inversion.
3866 thread_set_exec_promotion(old_thread
);
3867 p
= proc_refdrain_with_refwait(p
, TRUE
);
3868 /* extra proc ref returned to the caller */
3870 assert(get_threadtask(new_thread
) == new_task
);
3871 task_active
= task_is_active(new_task
);
3873 /* Take the proc_translock to change the task ptr */
3875 proc_active
= !(p
->p_lflag
& P_LEXIT
);
3877 /* Check if the current thread is not aborted due to SIGKILL */
3878 thread_active
= thread_is_active(old_thread
);
3881 * Do not switch the task if the new task or proc is already terminated
3882 * as a result of error in exec past point of no return
3884 if (proc_active
&& task_active
&& thread_active
) {
3885 error
= proc_transstart(p
, 1, 0);
3887 uthread_t new_uthread
= get_bsdthread_info(new_thread
);
3888 uthread_t old_uthread
= get_bsdthread_info(current_thread());
3891 * bsd_info of old_task will get cleared in execve and posix_spawn
3892 * after firing exec-success/error dtrace probe.
3896 /* Clear dispatchqueue and workloop ast offset */
3897 p
->p_dispatchqueue_offset
= 0;
3898 p
->p_dispatchqueue_serialno_offset
= 0;
3899 p
->p_dispatchqueue_label_offset
= 0;
3900 p
->p_return_to_kernel_offset
= 0;
3902 /* Copy the signal state, dtrace state and set bsd ast on new thread */
3903 act_set_astbsd(new_thread
);
3904 new_uthread
->uu_siglist
= old_uthread
->uu_siglist
;
3905 new_uthread
->uu_sigwait
= old_uthread
->uu_sigwait
;
3906 new_uthread
->uu_sigmask
= old_uthread
->uu_sigmask
;
3907 new_uthread
->uu_oldmask
= old_uthread
->uu_oldmask
;
3908 new_uthread
->uu_vforkmask
= old_uthread
->uu_vforkmask
;
3909 new_uthread
->uu_exit_reason
= old_uthread
->uu_exit_reason
;
3911 new_uthread
->t_dtrace_sig
= old_uthread
->t_dtrace_sig
;
3912 new_uthread
->t_dtrace_stop
= old_uthread
->t_dtrace_stop
;
3913 new_uthread
->t_dtrace_resumepid
= old_uthread
->t_dtrace_resumepid
;
3914 assert(new_uthread
->t_dtrace_scratch
== NULL
);
3915 new_uthread
->t_dtrace_scratch
= old_uthread
->t_dtrace_scratch
;
3917 old_uthread
->t_dtrace_sig
= 0;
3918 old_uthread
->t_dtrace_stop
= 0;
3919 old_uthread
->t_dtrace_resumepid
= 0;
3920 old_uthread
->t_dtrace_scratch
= NULL
;
3922 /* Copy the resource accounting info */
3923 thread_copy_resource_info(new_thread
, current_thread());
3925 /* Clear the exit reason and signal state on old thread */
3926 old_uthread
->uu_exit_reason
= NULL
;
3927 old_uthread
->uu_siglist
= 0;
3929 /* Add the new uthread to proc uthlist and remove the old one */
3930 TAILQ_INSERT_TAIL(&p
->p_uthlist
, new_uthread
, uu_list
);
3931 TAILQ_REMOVE(&p
->p_uthlist
, old_uthread
, uu_list
);
3933 task_set_did_exec_flag(old_task
);
3934 task_clear_exec_copy_flag(new_task
);
3936 task_copy_fields_for_exec(new_task
, old_task
);
3938 proc_transend(p
, 1);
3944 thread_clear_exec_promotion(old_thread
);
3946 if (error
!= 0 || !task_active
|| !proc_active
|| !thread_active
) {
3947 task_terminate_internal(new_task
);
3956 * Parameters: uap->fname File name to exec
3957 * uap->argp Argument list
3958 * uap->envp Environment list
3960 * Returns: 0 Success
3961 * __mac_execve:EINVAL Invalid argument
3962 * __mac_execve:ENOTSUP Invalid argument
3963 * __mac_execve:EACCES Permission denied
3964 * __mac_execve:EINTR Interrupted function
3965 * __mac_execve:ENOMEM Not enough space
3966 * __mac_execve:EFAULT Bad address
3967 * __mac_execve:ENAMETOOLONG Filename too long
3968 * __mac_execve:ENOEXEC Executable file format error
3969 * __mac_execve:ETXTBSY Text file busy [misuse of error code]
3972 * TODO: Dynamic linker header address on stack is copied via suword()
3976 execve(proc_t p
, struct execve_args
*uap
, int32_t *retval
)
3978 struct __mac_execve_args muap
;
3981 memoryshot(VM_EXECVE
, DBG_FUNC_NONE
);
3983 muap
.fname
= uap
->fname
;
3984 muap
.argp
= uap
->argp
;
3985 muap
.envp
= uap
->envp
;
3986 muap
.mac_p
= USER_ADDR_NULL
;
3987 err
= __mac_execve(p
, &muap
, retval
);
3995 * Parameters: uap->fname File name to exec
3996 * uap->argp Argument list
3997 * uap->envp Environment list
3998 * uap->mac_p MAC label supplied by caller
4000 * Returns: 0 Success
4001 * EINVAL Invalid argument
4002 * ENOTSUP Not supported
4003 * ENOEXEC Executable file format error
4004 * exec_activate_image:EINVAL Invalid argument
4005 * exec_activate_image:EACCES Permission denied
4006 * exec_activate_image:EINTR Interrupted function
4007 * exec_activate_image:ENOMEM Not enough space
4008 * exec_activate_image:EFAULT Bad address
4009 * exec_activate_image:ENAMETOOLONG Filename too long
4010 * exec_activate_image:ENOEXEC Executable file format error
4011 * exec_activate_image:ETXTBSY Text file busy [misuse of error code]
4012 * exec_activate_image:EBADEXEC The executable is corrupt/unknown
4013 * exec_activate_image:???
4014 * mac_execve_enter:???
4016 * TODO: Dynamic linker header address on stack is copied via suword()
4019 __mac_execve(proc_t p
, struct __mac_execve_args
*uap
, int32_t *retval
)
4022 struct image_params
*imgp
;
4023 struct vnode_attr
*vap
;
4024 struct vnode_attr
*origvap
;
4026 int is_64
= IS_64BIT_PROCESS(p
);
4027 struct vfs_context context
;
4028 struct uthread
*uthread
;
4029 task_t old_task
= current_task();
4030 task_t new_task
= NULL
;
4031 boolean_t should_release_proc_ref
= FALSE
;
4032 boolean_t exec_done
= FALSE
;
4033 boolean_t in_vfexec
= FALSE
;
4034 void *inherit
= NULL
;
4036 context
.vc_thread
= current_thread();
4037 context
.vc_ucred
= kauth_cred_proc_ref(p
); /* XXX must NOT be kauth_cred_get() */
4039 /* Allocate a big chunk for locals instead of using stack since these
4040 * structures a pretty big.
4042 MALLOC(bufp
, char *, (sizeof(*imgp
) + sizeof(*vap
) + sizeof(*origvap
)), M_TEMP
, M_WAITOK
| M_ZERO
);
4043 imgp
= (struct image_params
*) bufp
;
4046 goto exit_with_error
;
4048 vap
= (struct vnode_attr
*) (bufp
+ sizeof(*imgp
));
4049 origvap
= (struct vnode_attr
*) (bufp
+ sizeof(*imgp
) + sizeof(*vap
));
4051 /* Initialize the common data in the image_params structure */
4052 imgp
->ip_user_fname
= uap
->fname
;
4053 imgp
->ip_user_argv
= uap
->argp
;
4054 imgp
->ip_user_envv
= uap
->envp
;
4055 imgp
->ip_vattr
= vap
;
4056 imgp
->ip_origvattr
= origvap
;
4057 imgp
->ip_vfs_context
= &context
;
4058 imgp
->ip_flags
= (is_64
? IMGPF_WAS_64BIT_ADDR
: IMGPF_NONE
) | ((p
->p_flag
& P_DISABLE_ASLR
) ? IMGPF_DISABLE_ASLR
: IMGPF_NONE
);
4059 imgp
->ip_seg
= (is_64
? UIO_USERSPACE64
: UIO_USERSPACE32
);
4060 imgp
->ip_mac_return
= 0;
4061 imgp
->ip_cs_error
= OS_REASON_NULL
;
4062 imgp
->ip_simulator_binary
= IMGPF_SB_DEFAULT
;
4065 if (uap
->mac_p
!= USER_ADDR_NULL
) {
4066 error
= mac_execve_enter(uap
->mac_p
, imgp
);
4068 kauth_cred_unref(&context
.vc_ucred
);
4069 goto exit_with_error
;
4073 uthread
= get_bsdthread_info(current_thread());
4074 if (uthread
->uu_flag
& UT_VFORK
) {
4075 imgp
->ip_flags
|= IMGPF_VFORK_EXEC
;
4078 imgp
->ip_flags
|= IMGPF_EXEC
;
4081 * For execve case, create a new task and thread
4082 * which points to current_proc. The current_proc will point
4083 * to the new task after image activation and proc ref drain.
4085 * proc (current_proc) <----- old_task (current_task)
4088 * | ----------------------------------
4090 * --------- new_task (task marked as TF_EXEC_COPY)
4092 * After image activation, the proc will point to the new task
4093 * and would look like following.
4095 * proc (current_proc) <----- old_task (current_task, marked as TPF_DID_EXEC)
4098 * | ----------> new_task
4102 * During exec any transition from new_task -> proc is fine, but don't allow
4103 * transition from proc->task, since it will modify old_task.
4105 imgp
->ip_new_thread
= fork_create_child(old_task
,
4110 task_get_64bit_data(old_task
),
4112 /* task and thread ref returned by fork_create_child */
4113 if (imgp
->ip_new_thread
== NULL
) {
4115 goto exit_with_error
;
4118 new_task
= get_threadtask(imgp
->ip_new_thread
);
4119 context
.vc_thread
= imgp
->ip_new_thread
;
4122 error
= exec_activate_image(imgp
);
4123 /* thread and task ref returned for vfexec case */
4125 if (imgp
->ip_new_thread
!= NULL
) {
4127 * task reference might be returned by exec_activate_image
4130 new_task
= get_threadtask(imgp
->ip_new_thread
);
4131 #if defined(HAS_APPLE_PAC)
4132 ml_task_set_disable_user_jop(new_task
, imgp
->ip_flags
& IMGPF_NOJOP
? TRUE
: FALSE
);
4133 ml_thread_set_disable_user_jop(imgp
->ip_new_thread
, imgp
->ip_flags
& IMGPF_NOJOP
? TRUE
: FALSE
);
4137 if (!error
&& !in_vfexec
) {
4138 p
= proc_exec_switch_task(p
, old_task
, new_task
, imgp
->ip_new_thread
);
4139 /* proc ref returned */
4140 should_release_proc_ref
= TRUE
;
4143 * Need to transfer pending watch port boosts to the new task while still making
4144 * sure that the old task remains in the importance linkage. Create an importance
4145 * linkage from old task to new task, then switch the task importance base
4146 * of old task and new task. After the switch the port watch boost will be
4147 * boosting the new task and new task will be donating importance to old task.
4149 inherit
= ipc_importance_exec_switch_task(old_task
, new_task
);
4152 kauth_cred_unref(&context
.vc_ucred
);
4154 /* Image not claimed by any activator? */
4161 assert(imgp
->ip_new_thread
!= NULL
);
4163 exec_resettextvp(p
, imgp
);
4164 error
= check_for_signature(p
, imgp
);
4167 /* flag exec has occurred, notify only if it has not failed due to FP Key error */
4168 if (exec_done
&& ((p
->p_lflag
& P_LTERM_DECRYPTFAIL
) == 0)) {
4169 proc_knote(p
, NOTE_EXEC
);
4172 if (imgp
->ip_vp
!= NULLVP
) {
4173 vnode_put(imgp
->ip_vp
);
4175 if (imgp
->ip_scriptvp
!= NULLVP
) {
4176 vnode_put(imgp
->ip_scriptvp
);
4178 if (imgp
->ip_strings
) {
4179 execargs_free(imgp
);
4182 if (imgp
->ip_execlabelp
) {
4183 mac_cred_label_free(imgp
->ip_execlabelp
);
4185 if (imgp
->ip_scriptlabelp
) {
4186 mac_vnode_label_free(imgp
->ip_scriptlabelp
);
4189 if (imgp
->ip_cs_error
!= OS_REASON_NULL
) {
4190 os_reason_free(imgp
->ip_cs_error
);
4191 imgp
->ip_cs_error
= OS_REASON_NULL
;
4196 * We need to initialize the bank context behind the protection of
4197 * the proc_trans lock to prevent a race with exit. We can't do this during
4198 * exec_activate_image because task_bank_init checks entitlements that
4199 * aren't loaded until subsequent calls (including exec_resettextvp).
4201 error
= proc_transstart(p
, 0, 0);
4205 task_bank_init(new_task
);
4206 proc_transend(p
, 0);
4209 proc_legacy_footprint_entitled(p
, new_task
, __FUNCTION__
);
4210 #endif /* __arm64__ */
4212 /* Sever any extant thread affinity */
4213 thread_affinity_exec(current_thread());
4215 /* Inherit task role from old task to new task for exec */
4217 proc_inherit_task_role(new_task
, old_task
);
4220 thread_t main_thread
= imgp
->ip_new_thread
;
4222 task_set_main_thread_qos(new_task
, main_thread
);
4226 * Check to see if we need to trigger an arcade upcall AST now
4227 * that the vnode has been reset on the task.
4229 arcade_prepare(new_task
, imgp
->ip_new_thread
);
4230 #endif /* CONFIG_ARCADE */
4234 * Processes with the MAP_JIT entitlement are permitted to have
4237 if (mac_proc_check_map_anon(p
, 0, 0, 0, MAP_JIT
, NULL
) == 0) {
4238 vm_map_set_jumbo(get_task_map(new_task
));
4239 vm_map_set_jit_entitled(get_task_map(new_task
));
4241 #endif /* CONFIG_MACF */
4243 if (vm_darkwake_mode
== TRUE
) {
4245 * This process is being launched when the system
4246 * is in darkwake. So mark it specially. This will
4247 * cause all its pages to be entered in the background Q.
4249 task_set_darkwake_mode(new_task
, vm_darkwake_mode
);
4253 dtrace_thread_didexec(imgp
->ip_new_thread
);
4255 if ((dtrace_proc_waitfor_hook
= dtrace_proc_waitfor_exec_ptr
) != NULL
) {
4256 (*dtrace_proc_waitfor_hook
)(p
);
4261 if (!error
&& AUDIT_ENABLED() && p
) {
4262 /* Add the CDHash of the new process to the audit record */
4263 uint8_t *cdhash
= cs_get_cdhash(p
);
4265 AUDIT_ARG(data
, cdhash
, sizeof(uint8_t), CS_CDHASH_LEN
);
4271 vfork_return(p
, retval
, p
->p_pid
);
4274 DTRACE_PROC1(exec__failure
, int, error
);
4280 * clear bsd_info from old task if it did exec.
4282 if (task_did_exec(old_task
)) {
4283 set_bsdtask_info(old_task
, NULL
);
4286 /* clear bsd_info from new task and terminate it if exec failed */
4287 if (new_task
!= NULL
&& task_is_exec_copy(new_task
)) {
4288 set_bsdtask_info(new_task
, NULL
);
4289 task_terminate_internal(new_task
);
4293 /* Clear the initial wait on the thread transferring watchports */
4294 if (imgp
->ip_new_thread
) {
4295 task_clear_return_wait(get_threadtask(imgp
->ip_new_thread
), TCRW_CLEAR_INITIAL_WAIT
);
4298 /* Transfer the watchport boost to new task */
4299 if (!error
&& !in_vfexec
) {
4300 task_transfer_turnstile_watchports(old_task
,
4301 new_task
, imgp
->ip_new_thread
);
4304 * Do not terminate the current task, if proc_exec_switch_task did not
4305 * switch the tasks, terminating the current task without the switch would
4306 * result in loosing the SIGKILL status.
4308 if (task_did_exec(old_task
)) {
4309 /* Terminate the current task, since exec will start in new task */
4310 task_terminate_internal(old_task
);
4313 /* Release the thread ref returned by fork_create_child */
4314 if (imgp
->ip_new_thread
) {
4315 /* wake up the new exec thread */
4316 task_clear_return_wait(get_threadtask(imgp
->ip_new_thread
), TCRW_CLEAR_FINAL_WAIT
);
4317 thread_deallocate(imgp
->ip_new_thread
);
4318 imgp
->ip_new_thread
= NULL
;
4322 /* Release the ref returned by fork_create_child */
4324 task_deallocate(new_task
);
4328 if (should_release_proc_ref
) {
4336 if (inherit
!= NULL
) {
4337 ipc_importance_release(inherit
);
4347 * Description: Copy a pointer in from user space to a user_addr_t in kernel
4348 * space, based on 32/64 bitness of the user space
4350 * Parameters: froma User space address
4351 * toptr Address of kernel space user_addr_t
4352 * ptr_size 4/8, based on 'froma' address space
4354 * Returns: 0 Success
4355 * EFAULT Bad 'froma'
4358 * *ptr_size Modified
4361 copyinptr(user_addr_t froma
, user_addr_t
*toptr
, int ptr_size
)
4365 if (ptr_size
== 4) {
4366 /* 64 bit value containing 32 bit address */
4369 error
= copyin(froma
, &i
, 4);
4370 *toptr
= CAST_USER_ADDR_T(i
); /* SAFE */
4372 error
= copyin(froma
, toptr
, 8);
4381 * Description: Copy a pointer out from a user_addr_t in kernel space to
4382 * user space, based on 32/64 bitness of the user space
4384 * Parameters: ua User space address to copy to
4385 * ptr Address of kernel space user_addr_t
4386 * ptr_size 4/8, based on 'ua' address space
4388 * Returns: 0 Success
4393 copyoutptr(user_addr_t ua
, user_addr_t ptr
, int ptr_size
)
4397 if (ptr_size
== 4) {
4398 /* 64 bit value containing 32 bit address */
4399 unsigned int i
= CAST_DOWN_EXPLICIT(unsigned int, ua
); /* SAFE */
4401 error
= copyout(&i
, ptr
, 4);
4403 error
= copyout(&ua
, ptr
, 8);
4410 * exec_copyout_strings
4412 * Copy out the strings segment to user space. The strings segment is put
4413 * on a preinitialized stack frame.
4415 * Parameters: struct image_params * the image parameter block
4416 * int * a pointer to the stack offset variable
4418 * Returns: 0 Success
4422 * (*stackp) The stack offset, modified
4424 * Note: The strings segment layout is backward, from the beginning
4425 * of the top of the stack to consume the minimal amount of
4426 * space possible; the returned stack pointer points to the
4427 * end of the area consumed (stacks grow downward).
4429 * argc is an int; arg[i] are pointers; env[i] are pointers;
4430 * the 0's are (void *)NULL's
4432 * The stack frame layout is:
4434 * +-------------+ <- p->user_stack
4475 * sp-> +-------------+
4477 * Although technically a part of the STRING AREA, we treat the PATH AREA as
4478 * a separate entity. This allows us to align the beginning of the PATH AREA
4479 * to a pointer boundary so that the exec_path, env[i], and argv[i] pointers
4480 * which preceed it on the stack are properly aligned.
4484 exec_copyout_strings(struct image_params
*imgp
, user_addr_t
*stackp
)
4486 proc_t p
= vfs_context_proc(imgp
->ip_vfs_context
);
4487 int ptr_size
= (imgp
->ip_flags
& IMGPF_IS_64BIT_ADDR
) ? 8 : 4;
4489 void *ptr_buffer_start
, *ptr_buffer
;
4492 user_addr_t string_area
; /* *argv[], *env[] */
4493 user_addr_t ptr_area
; /* argv[], env[], applev[] */
4494 user_addr_t argc_area
; /* argc */
4499 struct copyout_desc
{
4503 user_addr_t
*dtrace_cookie
;
4505 boolean_t null_term
;
4508 .start_string
= imgp
->ip_startargv
,
4509 .count
= imgp
->ip_argc
,
4511 .dtrace_cookie
= &p
->p_dtrace_argv
,
4516 .start_string
= imgp
->ip_endargv
,
4517 .count
= imgp
->ip_envc
,
4519 .dtrace_cookie
= &p
->p_dtrace_envp
,
4524 .start_string
= imgp
->ip_strings
,
4527 .dtrace_cookie
= NULL
,
4532 .start_string
= imgp
->ip_endenvv
,
4533 .count
= imgp
->ip_applec
- 1, /* exec_path handled above */
4535 .dtrace_cookie
= NULL
,
4544 * All previous contributors to the string area
4545 * should have aligned their sub-area
4547 if (imgp
->ip_strspace
% ptr_size
!= 0) {
4552 /* Grow the stack down for the strings we've been building up */
4553 string_size
= imgp
->ip_strendp
- imgp
->ip_strings
;
4554 stack
-= string_size
;
4555 string_area
= stack
;
4558 * Need room for one pointer for each string, plus
4559 * one for the NULLs terminating the argv, envv, and apple areas.
4561 ptr_area_size
= (imgp
->ip_argc
+ imgp
->ip_envc
+ imgp
->ip_applec
+ 3) * ptr_size
;
4562 stack
-= ptr_area_size
;
4565 /* We'll construct all the pointer arrays in our string buffer,
4566 * which we already know is aligned properly, and ip_argspace
4567 * was used to verify we have enough space.
4569 ptr_buffer_start
= ptr_buffer
= (void *)imgp
->ip_strendp
;
4572 * Need room for pointer-aligned argc slot.
4578 * Record the size of the arguments area so that sysctl_procargs()
4579 * can return the argument area without having to parse the arguments.
4582 p
->p_argc
= imgp
->ip_argc
;
4583 p
->p_argslen
= (int)(*stackp
- string_area
);
4586 /* Return the initial stack address: the location of argc */
4590 * Copy out the entire strings area.
4592 error
= copyout(imgp
->ip_strings
, string_area
,
4598 for (i
= 0; i
< sizeof(descriptors
) / sizeof(descriptors
[0]); i
++) {
4599 char *cur_string
= descriptors
[i
].start_string
;
4603 if (descriptors
[i
].dtrace_cookie
) {
4605 *descriptors
[i
].dtrace_cookie
= ptr_area
+ ((uintptr_t)ptr_buffer
- (uintptr_t)ptr_buffer_start
); /* dtrace convenience */
4608 #endif /* CONFIG_DTRACE */
4611 * For each segment (argv, envv, applev), copy as many pointers as requested
4612 * to our pointer buffer.
4614 for (j
= 0; j
< descriptors
[i
].count
; j
++) {
4615 user_addr_t cur_address
= string_area
+ (cur_string
- imgp
->ip_strings
);
4617 /* Copy out the pointer to the current string. Alignment has been verified */
4618 if (ptr_size
== 8) {
4619 *(uint64_t *)ptr_buffer
= (uint64_t)cur_address
;
4621 *(uint32_t *)ptr_buffer
= (uint32_t)cur_address
;
4624 ptr_buffer
= (void *)((uintptr_t)ptr_buffer
+ ptr_size
);
4625 cur_string
+= strlen(cur_string
) + 1; /* Only a NUL between strings in the same area */
4628 if (descriptors
[i
].null_term
) {
4629 if (ptr_size
== 8) {
4630 *(uint64_t *)ptr_buffer
= 0ULL;
4632 *(uint32_t *)ptr_buffer
= 0;
4635 ptr_buffer
= (void *)((uintptr_t)ptr_buffer
+ ptr_size
);
4640 * Copy out all our pointer arrays in bulk.
4642 error
= copyout(ptr_buffer_start
, ptr_area
,
4648 /* argc (int32, stored in a ptr_size area) */
4649 error
= copyoutptr((user_addr_t
)imgp
->ip_argc
, argc_area
, ptr_size
);
4660 * exec_extract_strings
4662 * Copy arguments and environment from user space into work area; we may
4663 * have already copied some early arguments into the work area, and if
4664 * so, any arguments opied in are appended to those already there.
4665 * This function is the primary manipulator of ip_argspace, since
4666 * these are the arguments the client of execve(2) knows about. After
4667 * each argv[]/envv[] string is copied, we charge the string length
4668 * and argv[]/envv[] pointer slot to ip_argspace, so that we can
4669 * full preflight the arg list size.
4671 * Parameters: struct image_params * the image parameter block
4673 * Returns: 0 Success
4677 * (imgp->ip_argc) Count of arguments, updated
4678 * (imgp->ip_envc) Count of environment strings, updated
4679 * (imgp->ip_argspace) Count of remaining of NCARGS
4680 * (imgp->ip_interp_buffer) Interpreter and args (mutated in place)
4683 * Note: The argument and environment vectors are user space pointers
4684 * to arrays of user space pointers.
4687 exec_extract_strings(struct image_params
*imgp
)
4690 int ptr_size
= (imgp
->ip_flags
& IMGPF_WAS_64BIT_ADDR
) ? 8 : 4;
4691 int new_ptr_size
= (imgp
->ip_flags
& IMGPF_IS_64BIT_ADDR
) ? 8 : 4;
4692 user_addr_t argv
= imgp
->ip_user_argv
;
4693 user_addr_t envv
= imgp
->ip_user_envv
;
4696 * Adjust space reserved for the path name by however much padding it
4697 * needs. Doing this here since we didn't know if this would be a 32-
4698 * or 64-bit process back in exec_save_path.
4700 while (imgp
->ip_strspace
% new_ptr_size
!= 0) {
4701 *imgp
->ip_strendp
++ = '\0';
4702 imgp
->ip_strspace
--;
4703 /* imgp->ip_argspace--; not counted towards exec args total */
4707 * From now on, we start attributing string space to ip_argspace
4709 imgp
->ip_startargv
= imgp
->ip_strendp
;
4712 if ((imgp
->ip_flags
& IMGPF_INTERPRET
) != 0) {
4714 char *argstart
, *ch
;
4716 /* First, the arguments in the "#!" string are tokenized and extracted. */
4717 argstart
= imgp
->ip_interp_buffer
;
4720 while (*ch
&& !IS_WHITESPACE(*ch
)) {
4725 /* last argument, no need to NUL-terminate */
4726 error
= exec_add_user_string(imgp
, CAST_USER_ADDR_T(argstart
), UIO_SYSSPACE
, TRUE
);
4731 error
= exec_add_user_string(imgp
, CAST_USER_ADDR_T(argstart
), UIO_SYSSPACE
, TRUE
);
4734 * Find the next string. We know spaces at the end of the string have already
4738 while (IS_WHITESPACE(*argstart
)) {
4743 /* Error-check, regardless of whether this is the last interpreter arg or not */
4747 if (imgp
->ip_argspace
< new_ptr_size
) {
4751 imgp
->ip_argspace
-= new_ptr_size
; /* to hold argv[] entry */
4757 * If we are running an interpreter, replace the av[0] that was
4758 * passed to execve() with the path name that was
4759 * passed to execve() for interpreters which do not use the PATH
4760 * to locate their script arguments.
4762 error
= copyinptr(argv
, &arg
, ptr_size
);
4767 argv
+= ptr_size
; /* consume without using */
4771 if (imgp
->ip_interp_sugid_fd
!= -1) {
4772 char temp
[19]; /* "/dev/fd/" + 10 digits + NUL */
4773 snprintf(temp
, sizeof(temp
), "/dev/fd/%d", imgp
->ip_interp_sugid_fd
);
4774 error
= exec_add_user_string(imgp
, CAST_USER_ADDR_T(temp
), UIO_SYSSPACE
, TRUE
);
4776 error
= exec_add_user_string(imgp
, imgp
->ip_user_fname
, imgp
->ip_seg
, TRUE
);
4782 if (imgp
->ip_argspace
< new_ptr_size
) {
4786 imgp
->ip_argspace
-= new_ptr_size
; /* to hold argv[] entry */
4790 while (argv
!= 0LL) {
4793 error
= copyinptr(argv
, &arg
, ptr_size
);
4807 error
= exec_add_user_string(imgp
, arg
, imgp
->ip_seg
, TRUE
);
4811 if (imgp
->ip_argspace
< new_ptr_size
) {
4815 imgp
->ip_argspace
-= new_ptr_size
; /* to hold argv[] entry */
4819 /* Save space for argv[] NULL terminator */
4820 if (imgp
->ip_argspace
< new_ptr_size
) {
4824 imgp
->ip_argspace
-= new_ptr_size
;
4826 /* Note where the args ends and env begins. */
4827 imgp
->ip_endargv
= imgp
->ip_strendp
;
4830 /* Now, get the environment */
4831 while (envv
!= 0LL) {
4834 error
= copyinptr(envv
, &env
, ptr_size
);
4846 error
= exec_add_user_string(imgp
, env
, imgp
->ip_seg
, TRUE
);
4850 if (imgp
->ip_argspace
< new_ptr_size
) {
4854 imgp
->ip_argspace
-= new_ptr_size
; /* to hold envv[] entry */
4858 /* Save space for envv[] NULL terminator */
4859 if (imgp
->ip_argspace
< new_ptr_size
) {
4863 imgp
->ip_argspace
-= new_ptr_size
;
4865 /* Align the tail of the combined argv+envv area */
4866 while (imgp
->ip_strspace
% new_ptr_size
!= 0) {
4867 if (imgp
->ip_argspace
< 1) {
4871 *imgp
->ip_strendp
++ = '\0';
4872 imgp
->ip_strspace
--;
4873 imgp
->ip_argspace
--;
4876 /* Note where the envv ends and applev begins. */
4877 imgp
->ip_endenvv
= imgp
->ip_strendp
;
4880 * From now on, we are no longer charging argument
4881 * space to ip_argspace.
4889 * Libc has an 8-element array set up for stack guard values. It only fills
4890 * in one of those entries, and both gcc and llvm seem to use only a single
4891 * 8-byte guard. Until somebody needs more than an 8-byte guard value, don't
4892 * do the work to construct them.
4894 #define GUARD_VALUES 1
4895 #define GUARD_KEY "stack_guard="
4898 * System malloc needs some entropy when it is initialized.
4900 #define ENTROPY_VALUES 2
4901 #define ENTROPY_KEY "malloc_entropy="
4904 * libplatform needs a random pointer-obfuscation value when it is initialized.
4906 #define PTR_MUNGE_VALUES 1
4907 #define PTR_MUNGE_KEY "ptr_munge="
4910 * System malloc engages nanozone for UIAPP.
4912 #define NANO_ENGAGE_KEY "MallocNanoZone=1"
4914 #define PFZ_KEY "pfz="
4915 extern user32_addr_t commpage_text32_location
;
4916 extern user64_addr_t commpage_text64_location
;
4918 #define MAIN_STACK_VALUES 4
4919 #define MAIN_STACK_KEY "main_stack="
4921 #define FSID_KEY "executable_file="
4922 #define DYLD_FSID_KEY "dyld_file="
4923 #define CDHASH_KEY "executable_cdhash="
4924 #define DYLD_FLAGS_KEY "dyld_flags="
4926 #define FSID_MAX_STRING "0x1234567890abcdef,0x1234567890abcdef"
4928 #define HEX_STR_LEN 18 // 64-bit hex value "0x0123456701234567"
4931 exec_add_entropy_key(struct image_params
*imgp
,
4936 const int limit
= 8;
4937 uint64_t entropy
[limit
];
4938 char str
[strlen(key
) + (HEX_STR_LEN
+ 1) * limit
+ 1];
4939 if (values
> limit
) {
4943 read_random(entropy
, sizeof(entropy
[0]) * values
);
4946 entropy
[0] &= ~(0xffull
<< 8);
4949 int len
= snprintf(str
, sizeof(str
), "%s0x%llx", key
, entropy
[0]);
4950 int remaining
= sizeof(str
) - len
;
4951 for (int i
= 1; i
< values
&& remaining
> 0; ++i
) {
4952 int start
= sizeof(str
) - remaining
;
4953 len
= snprintf(&str
[start
], remaining
, ",0x%llx", entropy
[i
]);
4957 return exec_add_user_string(imgp
, CAST_USER_ADDR_T(str
), UIO_SYSSPACE
, FALSE
);
4961 * Build up the contents of the apple[] string vector
4963 #if (DEVELOPMENT || DEBUG)
4964 uint64_t dyld_flags
= 0;
4968 exec_add_apple_strings(struct image_params
*imgp
,
4969 const load_result_t
*load_result
)
4972 int img_ptr_size
= (imgp
->ip_flags
& IMGPF_IS_64BIT_ADDR
) ? 8 : 4;
4974 /* exec_save_path stored the first string */
4975 imgp
->ip_applec
= 1;
4977 /* adding the pfz string */
4979 char pfz_string
[strlen(PFZ_KEY
) + HEX_STR_LEN
+ 1];
4981 if (img_ptr_size
== 8) {
4982 snprintf(pfz_string
, sizeof(pfz_string
), PFZ_KEY
"0x%llx", commpage_text64_location
);
4984 snprintf(pfz_string
, sizeof(pfz_string
), PFZ_KEY
"0x%x", commpage_text32_location
);
4986 error
= exec_add_user_string(imgp
, CAST_USER_ADDR_T(pfz_string
), UIO_SYSSPACE
, FALSE
);
4993 /* adding the NANO_ENGAGE_KEY key */
4994 if (imgp
->ip_px_sa
) {
4995 int proc_flags
= (((struct _posix_spawnattr
*) imgp
->ip_px_sa
)->psa_flags
);
4997 if ((proc_flags
& _POSIX_SPAWN_NANO_ALLOCATOR
) == _POSIX_SPAWN_NANO_ALLOCATOR
) {
4998 const char *nano_string
= NANO_ENGAGE_KEY
;
4999 error
= exec_add_user_string(imgp
, CAST_USER_ADDR_T(nano_string
), UIO_SYSSPACE
, FALSE
);
5008 * Supply libc with a collection of random values to use when
5009 * implementing -fstack-protector.
5011 * (The first random string always contains an embedded NUL so that
5012 * __stack_chk_guard also protects against C string vulnerabilities)
5014 error
= exec_add_entropy_key(imgp
, GUARD_KEY
, GUARD_VALUES
, TRUE
);
5021 * Supply libc with entropy for system malloc.
5023 error
= exec_add_entropy_key(imgp
, ENTROPY_KEY
, ENTROPY_VALUES
, FALSE
);
5030 * Supply libpthread & libplatform with a random value to use for pointer
5033 error
= exec_add_entropy_key(imgp
, PTR_MUNGE_KEY
, PTR_MUNGE_VALUES
, FALSE
);
5040 * Add MAIN_STACK_KEY: Supplies the address and size of the main thread's
5041 * stack if it was allocated by the kernel.
5043 * The guard page is not included in this stack size as libpthread
5044 * expects to add it back in after receiving this value.
5046 if (load_result
->unixproc
) {
5047 char stack_string
[strlen(MAIN_STACK_KEY
) + (HEX_STR_LEN
+ 1) * MAIN_STACK_VALUES
+ 1];
5048 snprintf(stack_string
, sizeof(stack_string
),
5049 MAIN_STACK_KEY
"0x%llx,0x%llx,0x%llx,0x%llx",
5050 (uint64_t)load_result
->user_stack
,
5051 (uint64_t)load_result
->user_stack_size
,
5052 (uint64_t)load_result
->user_stack_alloc
,
5053 (uint64_t)load_result
->user_stack_alloc_size
);
5054 error
= exec_add_user_string(imgp
, CAST_USER_ADDR_T(stack_string
), UIO_SYSSPACE
, FALSE
);
5061 if (imgp
->ip_vattr
) {
5062 uint64_t fsid
= vnode_get_va_fsid(imgp
->ip_vattr
);
5063 uint64_t fsobjid
= imgp
->ip_vattr
->va_fileid
;
5065 char fsid_string
[strlen(FSID_KEY
) + strlen(FSID_MAX_STRING
) + 1];
5066 snprintf(fsid_string
, sizeof(fsid_string
),
5067 FSID_KEY
"0x%llx,0x%llx", fsid
, fsobjid
);
5068 error
= exec_add_user_string(imgp
, CAST_USER_ADDR_T(fsid_string
), UIO_SYSSPACE
, FALSE
);
5075 if (imgp
->ip_dyld_fsid
|| imgp
->ip_dyld_fsobjid
) {
5076 char fsid_string
[strlen(DYLD_FSID_KEY
) + strlen(FSID_MAX_STRING
) + 1];
5077 snprintf(fsid_string
, sizeof(fsid_string
),
5078 DYLD_FSID_KEY
"0x%llx,0x%llx", imgp
->ip_dyld_fsid
, imgp
->ip_dyld_fsobjid
);
5079 error
= exec_add_user_string(imgp
, CAST_USER_ADDR_T(fsid_string
), UIO_SYSSPACE
, FALSE
);
5086 uint8_t cdhash
[SHA1_RESULTLEN
];
5087 int cdhash_errror
= ubc_cs_getcdhash(imgp
->ip_vp
, imgp
->ip_arch_offset
, cdhash
);
5088 if (cdhash_errror
== 0) {
5089 char hash_string
[strlen(CDHASH_KEY
) + 2 * SHA1_RESULTLEN
+ 1];
5090 strncpy(hash_string
, CDHASH_KEY
, sizeof(hash_string
));
5091 char *p
= hash_string
+ sizeof(CDHASH_KEY
) - 1;
5092 for (int i
= 0; i
< SHA1_RESULTLEN
; i
++) {
5093 snprintf(p
, 3, "%02x", (int) cdhash
[i
]);
5096 error
= exec_add_user_string(imgp
, CAST_USER_ADDR_T(hash_string
), UIO_SYSSPACE
, FALSE
);
5102 #if (DEVELOPMENT || DEBUG)
5104 char dyld_flags_string
[strlen(DYLD_FLAGS_KEY
) + HEX_STR_LEN
+ 1];
5105 snprintf(dyld_flags_string
, sizeof(dyld_flags_string
), DYLD_FLAGS_KEY
"0x%llx", dyld_flags
);
5106 error
= exec_add_user_string(imgp
, CAST_USER_ADDR_T(dyld_flags_string
), UIO_SYSSPACE
, FALSE
);
5114 /* Align the tail of the combined applev area */
5115 while (imgp
->ip_strspace
% img_ptr_size
!= 0) {
5116 *imgp
->ip_strendp
++ = '\0';
5117 imgp
->ip_strspace
--;
5124 #define unix_stack_size(p) (p->p_rlimit[RLIMIT_STACK].rlim_cur)
5127 * exec_check_permissions
5129 * Description: Verify that the file that is being attempted to be executed
5130 * is in fact allowed to be executed based on it POSIX file
5131 * permissions and other access control criteria
5133 * Parameters: struct image_params * the image parameter block
5135 * Returns: 0 Success
5136 * EACCES Permission denied
5137 * ENOEXEC Executable file format error
5138 * ETXTBSY Text file busy [misuse of error code]
5140 * vnode_authorize:???
5143 exec_check_permissions(struct image_params
*imgp
)
5145 struct vnode
*vp
= imgp
->ip_vp
;
5146 struct vnode_attr
*vap
= imgp
->ip_vattr
;
5147 proc_t p
= vfs_context_proc(imgp
->ip_vfs_context
);
5149 kauth_action_t action
;
5151 /* Only allow execution of regular files */
5152 if (!vnode_isreg(vp
)) {
5156 /* Get the file attributes that we will be using here and elsewhere */
5158 VATTR_WANTED(vap
, va_uid
);
5159 VATTR_WANTED(vap
, va_gid
);
5160 VATTR_WANTED(vap
, va_mode
);
5161 VATTR_WANTED(vap
, va_fsid
);
5162 VATTR_WANTED(vap
, va_fsid64
);
5163 VATTR_WANTED(vap
, va_fileid
);
5164 VATTR_WANTED(vap
, va_data_size
);
5165 if ((error
= vnode_getattr(vp
, vap
, imgp
->ip_vfs_context
)) != 0) {
5170 * Ensure that at least one execute bit is on - otherwise root
5171 * will always succeed, and we don't want to happen unless the
5172 * file really is executable.
5174 if (!vfs_authopaque(vnode_mount(vp
)) && ((vap
->va_mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
)) == 0)) {
5178 /* Disallow zero length files */
5179 if (vap
->va_data_size
== 0) {
5183 imgp
->ip_arch_offset
= (user_size_t
)0;
5184 imgp
->ip_arch_size
= vap
->va_data_size
;
5186 /* Disable setuid-ness for traced programs or if MNT_NOSUID */
5187 if ((vp
->v_mount
->mnt_flag
& MNT_NOSUID
) || (p
->p_lflag
& P_LTRACED
)) {
5188 vap
->va_mode
&= ~(VSUID
| VSGID
);
5192 * Disable _POSIX_SPAWN_ALLOW_DATA_EXEC and _POSIX_SPAWN_DISABLE_ASLR
5193 * flags for setuid/setgid binaries.
5195 if (vap
->va_mode
& (VSUID
| VSGID
)) {
5196 imgp
->ip_flags
&= ~(IMGPF_ALLOW_DATA_EXEC
| IMGPF_DISABLE_ASLR
);
5200 error
= mac_vnode_check_exec(imgp
->ip_vfs_context
, vp
, imgp
);
5206 /* Check for execute permission */
5207 action
= KAUTH_VNODE_EXECUTE
;
5208 /* Traced images must also be readable */
5209 if (p
->p_lflag
& P_LTRACED
) {
5210 action
|= KAUTH_VNODE_READ_DATA
;
5212 if ((error
= vnode_authorize(vp
, NULL
, action
, imgp
->ip_vfs_context
)) != 0) {
5217 /* Don't let it run if anyone had it open for writing */
5219 if (vp
->v_writecount
) {
5220 panic("going to return ETXTBSY %x", vp
);
5228 /* XXX May want to indicate to underlying FS that vnode is open */
5237 * Initially clear the P_SUGID in the process flags; if an SUGID process is
5238 * exec'ing a non-SUGID image, then this is the point of no return.
5240 * If the image being activated is SUGID, then replace the credential with a
5241 * copy, disable tracing (unless the tracing process is root), reset the
5242 * mach task port to revoke it, set the P_SUGID bit,
5244 * If the saved user and group ID will be changing, then make sure it happens
5245 * to a new credential, rather than a shared one.
5247 * Set the security token (this is probably obsolete, given that the token
5248 * should not technically be separate from the credential itself).
5250 * Parameters: struct image_params * the image parameter block
5252 * Returns: void No failure indication
5255 * <process credential> Potentially modified/replaced
5256 * <task port> Potentially revoked
5257 * <process flags> P_SUGID bit potentially modified
5258 * <security token> Potentially modified
5261 exec_handle_sugid(struct image_params
*imgp
)
5263 proc_t p
= vfs_context_proc(imgp
->ip_vfs_context
);
5264 kauth_cred_t cred
= vfs_context_ucred(imgp
->ip_vfs_context
);
5266 int leave_sugid_clear
= 0;
5267 int mac_reset_ipc
= 0;
5271 int mac_transition
, disjoint_cred
= 0;
5272 int label_update_return
= 0;
5275 * Determine whether a call to update the MAC label will result in the
5276 * credential changing.
5278 * Note: MAC policies which do not actually end up modifying
5279 * the label subsequently are strongly encouraged to
5280 * return 0 for this check, since a non-zero answer will
5281 * slow down the exec fast path for normal binaries.
5283 mac_transition
= mac_cred_check_label_update_execve(
5284 imgp
->ip_vfs_context
,
5286 imgp
->ip_arch_offset
,
5288 imgp
->ip_scriptlabelp
,
5289 imgp
->ip_execlabelp
,
5294 OSBitAndAtomic(~((uint32_t)P_SUGID
), &p
->p_flag
);
5297 * Order of the following is important; group checks must go last,
5298 * as we use the success of the 'ismember' check combined with the
5299 * failure of the explicit match to indicate that we will be setting
5300 * the egid of the process even though the new process did not
5301 * require VSUID/VSGID bits in order for it to set the new group as
5304 * Note: Technically, by this we are implying a call to
5305 * setegid() in the new process, rather than implying
5306 * it used its VSGID bit to set the effective group,
5307 * even though there is no code in that process to make
5310 if (((imgp
->ip_origvattr
->va_mode
& VSUID
) != 0 &&
5311 kauth_cred_getuid(cred
) != imgp
->ip_origvattr
->va_uid
) ||
5312 ((imgp
->ip_origvattr
->va_mode
& VSGID
) != 0 &&
5313 ((kauth_cred_ismember_gid(cred
, imgp
->ip_origvattr
->va_gid
, &leave_sugid_clear
) || !leave_sugid_clear
) ||
5314 (kauth_cred_getgid(cred
) != imgp
->ip_origvattr
->va_gid
)))) {
5316 /* label for MAC transition and neither VSUID nor VSGID */
5317 handle_mac_transition
:
5322 * Replace the credential with a copy of itself if euid or
5325 * Note: setuid binaries will automatically opt out of
5326 * group resolver participation as a side effect
5327 * of this operation. This is an intentional
5328 * part of the security model, which requires a
5329 * participating credential be established by
5330 * escalating privilege, setting up all other
5331 * aspects of the credential including whether
5332 * or not to participate in external group
5333 * membership resolution, then dropping their
5334 * effective privilege to that of the desired
5335 * final credential state.
5337 * Modifications to p_ucred must be guarded using the
5338 * proc's ucred lock. This prevents others from accessing
5339 * a garbage credential.
5341 if (imgp
->ip_origvattr
->va_mode
& VSUID
) {
5342 apply_kauth_cred_update(p
, ^kauth_cred_t (kauth_cred_t my_cred
) {
5343 return kauth_cred_setresuid(my_cred
,
5345 imgp
->ip_origvattr
->va_uid
,
5346 imgp
->ip_origvattr
->va_uid
,
5351 if (imgp
->ip_origvattr
->va_mode
& VSGID
) {
5352 apply_kauth_cred_update(p
, ^kauth_cred_t (kauth_cred_t my_cred
) {
5353 return kauth_cred_setresgid(my_cred
,
5355 imgp
->ip_origvattr
->va_gid
,
5356 imgp
->ip_origvattr
->va_gid
);
5359 #endif /* !SECURE_KERNEL */
5363 * If a policy has indicated that it will transition the label,
5364 * before making the call into the MAC policies, get a new
5365 * duplicate credential, so they can modify it without
5366 * modifying any others sharing it.
5368 if (mac_transition
) {
5370 * This hook may generate upcalls that require
5371 * importance donation from the kernel.
5374 thread_t thread
= current_thread();
5375 thread_enable_send_importance(thread
, TRUE
);
5376 kauth_proc_label_update_execve(p
,
5377 imgp
->ip_vfs_context
,
5379 imgp
->ip_arch_offset
,
5381 imgp
->ip_scriptlabelp
,
5382 imgp
->ip_execlabelp
,
5385 &disjoint_cred
, /* will be non zero if disjoint */
5386 &label_update_return
);
5387 thread_enable_send_importance(thread
, FALSE
);
5389 if (disjoint_cred
) {
5391 * If updating the MAC label resulted in a
5392 * disjoint credential, flag that we need to
5393 * set the P_SUGID bit. This protects
5394 * against debuggers being attached by an
5395 * insufficiently privileged process onto the
5396 * result of a transition to a more privileged
5399 leave_sugid_clear
= 0;
5402 imgp
->ip_mac_return
= label_update_return
;
5405 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
);
5407 #endif /* CONFIG_MACF */
5410 * If 'leave_sugid_clear' is non-zero, then we passed the
5411 * VSUID and MACF checks, and successfully determined that
5412 * the previous cred was a member of the VSGID group, but
5413 * that it was not the default at the time of the execve,
5414 * and that the post-labelling credential was not disjoint.
5415 * So we don't set the P_SUGID or reset mach ports and fds
5416 * on the basis of simply running this code.
5418 if (mac_reset_ipc
|| !leave_sugid_clear
) {
5420 * Have mach reset the task and thread ports.
5421 * We don't want anyone who had the ports before
5422 * a setuid exec to be able to access/control the
5423 * task/thread after.
5425 ipc_task_reset((imgp
->ip_new_thread
!= NULL
) ?
5426 get_threadtask(imgp
->ip_new_thread
) : p
->task
);
5427 ipc_thread_reset((imgp
->ip_new_thread
!= NULL
) ?
5428 imgp
->ip_new_thread
: current_thread());
5431 if (!leave_sugid_clear
) {
5433 * Flag the process as setuid.
5435 OSBitOrAtomic(P_SUGID
, &p
->p_flag
);
5438 * Radar 2261856; setuid security hole fix
5439 * XXX For setuid processes, attempt to ensure that
5440 * stdin, stdout, and stderr are already allocated.
5441 * We do not want userland to accidentally allocate
5442 * descriptors in this range which has implied meaning
5445 for (i
= 0; i
< 3; i
++) {
5446 if (p
->p_fd
->fd_ofiles
[i
] != NULL
) {
5451 * Do the kernel equivalent of
5454 * (void) open("/dev/null", O_RDONLY);
5456 * (void) open("/dev/null", O_WRONLY);
5459 struct fileproc
*fp
;
5462 struct nameidata
*ndp
= NULL
;
5470 if ((error
= falloc(p
,
5471 &fp
, &indx
, imgp
->ip_vfs_context
)) != 0) {
5475 MALLOC(ndp
, struct nameidata
*, sizeof(*ndp
), M_TEMP
, M_WAITOK
| M_ZERO
);
5477 fp_free(p
, indx
, fp
);
5482 NDINIT(ndp
, LOOKUP
, OP_OPEN
, FOLLOW
, UIO_SYSSPACE
,
5483 CAST_USER_ADDR_T("/dev/null"),
5484 imgp
->ip_vfs_context
);
5486 if ((error
= vn_open(ndp
, flag
, 0)) != 0) {
5487 fp_free(p
, indx
, fp
);
5492 struct fileglob
*fg
= fp
->f_fglob
;
5495 fg
->fg_ops
= &vnops
;
5496 fg
->fg_data
= ndp
->ni_vp
;
5498 vnode_put(ndp
->ni_vp
);
5501 procfdtbl_releasefd(p
, indx
, NULL
);
5502 fp_drop(p
, indx
, fp
, 1);
5512 * We are here because we were told that the MAC label will
5513 * be transitioned, and the binary is not VSUID or VSGID; to
5514 * deal with this case, we could either duplicate a lot of
5515 * code, or we can indicate we want to default the P_SUGID
5516 * bit clear and jump back up.
5518 if (mac_transition
) {
5519 leave_sugid_clear
= 1;
5520 goto handle_mac_transition
;
5524 #endif /* CONFIG_MACF */
5527 * Implement the semantic where the effective user and group become
5528 * the saved user and group in exec'ed programs.
5530 * Modifications to p_ucred must be guarded using the
5531 * proc's ucred lock. This prevents others from accessing
5532 * a garbage credential.
5534 apply_kauth_cred_update(p
, ^kauth_cred_t (kauth_cred_t my_cred
) {
5535 return kauth_cred_setsvuidgid(my_cred
,
5536 kauth_cred_getuid(my_cred
),
5537 kauth_cred_getgid(my_cred
));
5540 /* Update the process' identity version and set the security token */
5541 p
->p_idversion
= OSIncrementAtomic(&nextpidversion
);
5543 if (imgp
->ip_new_thread
!= NULL
) {
5544 task
= get_threadtask(imgp
->ip_new_thread
);
5548 set_security_token_task_internal(p
, task
);
5557 * Description: Set the user stack address for the process to the provided
5558 * address. If a custom stack was not set as a result of the
5559 * load process (i.e. as specified by the image file for the
5560 * executable), then allocate the stack in the provided map and
5561 * set up appropriate guard pages for enforcing administrative
5562 * limits on stack growth, if they end up being needed.
5564 * Parameters: p Process to set stack on
5565 * load_result Information from mach-o load commands
5566 * map Address map in which to allocate the new stack
5568 * Returns: KERN_SUCCESS Stack successfully created
5569 * !KERN_SUCCESS Mach failure code
5571 static kern_return_t
5572 create_unix_stack(vm_map_t map
, load_result_t
* load_result
,
5575 mach_vm_size_t size
, prot_size
;
5576 mach_vm_offset_t addr
, prot_addr
;
5579 mach_vm_address_t user_stack
= load_result
->user_stack
;
5582 p
->user_stack
= user_stack
;
5583 if (load_result
->custom_stack
) {
5584 p
->p_lflag
|= P_LCUSTOM_STACK
;
5588 if (load_result
->user_stack_alloc_size
> 0) {
5590 * Allocate enough space for the maximum stack size we
5591 * will ever authorize and an extra page to act as
5592 * a guard page for stack overflows. For default stacks,
5593 * vm_initial_limit_stack takes care of the extra guard page.
5594 * Otherwise we must allocate it ourselves.
5596 if (mach_vm_round_page_overflow(load_result
->user_stack_alloc_size
, &size
)) {
5597 return KERN_INVALID_ARGUMENT
;
5599 addr
= mach_vm_trunc_page(load_result
->user_stack
- size
);
5600 kr
= mach_vm_allocate_kernel(map
, &addr
, size
,
5601 VM_FLAGS_FIXED
, VM_MEMORY_STACK
);
5602 if (kr
!= KERN_SUCCESS
) {
5603 // Can't allocate at default location, try anywhere
5605 kr
= mach_vm_allocate_kernel(map
, &addr
, size
,
5606 VM_FLAGS_ANYWHERE
, VM_MEMORY_STACK
);
5607 if (kr
!= KERN_SUCCESS
) {
5611 user_stack
= addr
+ size
;
5612 load_result
->user_stack
= user_stack
;
5615 p
->user_stack
= user_stack
;
5619 load_result
->user_stack_alloc
= addr
;
5622 * And prevent access to what's above the current stack
5623 * size limit for this process.
5625 if (load_result
->user_stack_size
== 0) {
5627 load_result
->user_stack_size
= unix_stack_size(p
);
5629 prot_size
= mach_vm_trunc_page(size
- load_result
->user_stack_size
);
5631 prot_size
= PAGE_SIZE
;
5635 kr
= mach_vm_protect(map
,
5640 if (kr
!= KERN_SUCCESS
) {
5641 (void)mach_vm_deallocate(map
, addr
, size
);
5646 return KERN_SUCCESS
;
5649 #include <sys/reboot.h>
5652 * load_init_program_at_path
5654 * Description: Load the "init" program; in most cases, this will be "launchd"
5656 * Parameters: p Process to call execve() to create
5657 * the "init" program
5658 * scratch_addr Page in p, scratch space
5659 * path NULL terminated path
5661 * Returns: KERN_SUCCESS Success
5662 * !KERN_SUCCESS See execve/mac_execve for error codes
5664 * Notes: The process that is passed in is the first manufactured
5665 * process on the system, and gets here via bsd_ast() firing
5666 * for the first time. This is done to ensure that bsd_init()
5667 * has run to completion.
5669 * The address map of the first manufactured process matches the
5670 * word width of the kernel. Once the self-exec completes, the
5671 * initproc might be different.
5674 load_init_program_at_path(proc_t p
, user_addr_t scratch_addr
, const char* path
)
5678 struct execve_args init_exec_args
;
5679 user_addr_t argv0
= USER_ADDR_NULL
, argv1
= USER_ADDR_NULL
;
5682 * Validate inputs and pre-conditions
5685 assert(scratch_addr
);
5689 * Copy out program name.
5691 size_t path_length
= strlen(path
) + 1;
5692 argv0
= scratch_addr
;
5693 error
= copyout(path
, argv0
, path_length
);
5698 scratch_addr
= USER_ADDR_ALIGN(scratch_addr
+ path_length
, sizeof(user_addr_t
));
5701 * Put out first (and only) argument, similarly.
5702 * Assumes everything fits in a page as allocated above.
5704 if (boothowto
& RB_SINGLE
) {
5705 const char *init_args
= "-s";
5706 size_t init_args_length
= strlen(init_args
) + 1;
5708 argv1
= scratch_addr
;
5709 error
= copyout(init_args
, argv1
, init_args_length
);
5714 scratch_addr
= USER_ADDR_ALIGN(scratch_addr
+ init_args_length
, sizeof(user_addr_t
));
5717 if (proc_is64bit(p
)) {
5718 user64_addr_t argv64bit
[3] = {};
5720 argv64bit
[0] = argv0
;
5721 argv64bit
[1] = argv1
;
5722 argv64bit
[2] = USER_ADDR_NULL
;
5724 error
= copyout(argv64bit
, scratch_addr
, sizeof(argv64bit
));
5729 user32_addr_t argv32bit
[3] = {};
5731 argv32bit
[0] = (user32_addr_t
)argv0
;
5732 argv32bit
[1] = (user32_addr_t
)argv1
;
5733 argv32bit
[2] = USER_ADDR_NULL
;
5735 error
= copyout(argv32bit
, scratch_addr
, sizeof(argv32bit
));
5742 * Set up argument block for fake call to execve.
5744 init_exec_args
.fname
= argv0
;
5745 init_exec_args
.argp
= scratch_addr
;
5746 init_exec_args
.envp
= USER_ADDR_NULL
;
5749 * So that init task is set with uid,gid 0 token
5751 set_security_token(p
);
5753 return execve(p
, &init_exec_args
, retval
);
5756 static const char * init_programs
[] = {
5758 "/usr/local/sbin/launchd.debug",
5760 #if DEVELOPMENT || DEBUG
5761 "/usr/local/sbin/launchd.development",
5769 * Description: Load the "init" program; in most cases, this will be "launchd"
5771 * Parameters: p Process to call execve() to create
5772 * the "init" program
5776 * Notes: The process that is passed in is the first manufactured
5777 * process on the system, and gets here via bsd_ast() firing
5778 * for the first time. This is done to ensure that bsd_init()
5779 * has run to completion.
5781 * In DEBUG & DEVELOPMENT builds, the launchdsuffix boot-arg
5782 * may be used to select a specific launchd executable. As with
5783 * the kcsuffix boot-arg, setting launchdsuffix to "" or "release"
5784 * will force /sbin/launchd to be selected.
5786 * Search order by build:
5788 * DEBUG DEVELOPMENT RELEASE PATH
5789 * ----------------------------------------------------------------------------------
5790 * 1 1 NA /usr/local/sbin/launchd.$LAUNCHDSUFFIX
5791 * 2 NA NA /usr/local/sbin/launchd.debug
5792 * 3 2 NA /usr/local/sbin/launchd.development
5793 * 4 3 1 /sbin/launchd
5796 load_init_program(proc_t p
)
5800 vm_map_t map
= current_map();
5801 mach_vm_offset_t scratch_addr
= 0;
5802 mach_vm_size_t map_page_size
= vm_map_page_size(map
);
5804 (void) mach_vm_allocate_kernel(map
, &scratch_addr
, map_page_size
, VM_FLAGS_ANYWHERE
, VM_KERN_MEMORY_NONE
);
5805 #if CONFIG_MEMORYSTATUS
5806 (void) memorystatus_init_at_boot_snapshot();
5807 #endif /* CONFIG_MEMORYSTATUS */
5809 #if DEBUG || DEVELOPMENT
5810 /* Check for boot-arg suffix first */
5811 char launchd_suffix
[64];
5812 if (PE_parse_boot_argn("launchdsuffix", launchd_suffix
, sizeof(launchd_suffix
))) {
5813 char launchd_path
[128];
5814 boolean_t is_release_suffix
= ((launchd_suffix
[0] == 0) ||
5815 (strcmp(launchd_suffix
, "release") == 0));
5817 if (is_release_suffix
) {
5818 printf("load_init_program: attempting to load /sbin/launchd\n");
5819 error
= load_init_program_at_path(p
, (user_addr_t
)scratch_addr
, "/sbin/launchd");
5824 panic("Process 1 exec of launchd.release failed, errno %d", error
);
5826 strlcpy(launchd_path
, "/usr/local/sbin/launchd.", sizeof(launchd_path
));
5827 strlcat(launchd_path
, launchd_suffix
, sizeof(launchd_path
));
5829 printf("load_init_program: attempting to load %s\n", launchd_path
);
5830 error
= load_init_program_at_path(p
, (user_addr_t
)scratch_addr
, launchd_path
);
5834 printf("load_init_program: failed loading %s: errno %d\n", launchd_path
, error
);
5841 for (i
= 0; i
< sizeof(init_programs
) / sizeof(init_programs
[0]); i
++) {
5842 printf("load_init_program: attempting to load %s\n", init_programs
[i
]);
5843 error
= load_init_program_at_path(p
, (user_addr_t
)scratch_addr
, init_programs
[i
]);
5847 printf("load_init_program: failed loading %s: errno %d\n", init_programs
[i
], error
);
5851 panic("Process 1 exec of %s failed, errno %d", ((i
== 0) ? "<null>" : init_programs
[i
- 1]), error
);
5855 * load_return_to_errno
5857 * Description: Convert a load_return_t (Mach error) to an errno (BSD error)
5859 * Parameters: lrtn Mach error number
5861 * Returns: (int) BSD error number
5863 * EBADARCH Bad architecture
5864 * EBADMACHO Bad Mach object file
5865 * ESHLIBVERS Bad shared library version
5866 * ENOMEM Out of memory/resource shortage
5867 * EACCES Access denied
5868 * ENOENT Entry not found (usually "file does
5870 * EIO An I/O error occurred
5871 * EBADEXEC The executable is corrupt/unknown
5874 load_return_to_errno(load_return_t lrtn
)
5882 case LOAD_BADMACHO_UPX
:
5895 case LOAD_DECRYPTFAIL
:
5903 #include <mach/mach_types.h>
5904 #include <mach/vm_prot.h>
5905 #include <mach/semaphore.h>
5906 #include <mach/sync_policy.h>
5907 #include <kern/clock.h>
5908 #include <mach/kern_return.h>
5913 * Description: Allocate the block of memory used by the execve arguments.
5914 * At the same time, we allocate a page so that we can read in
5915 * the first page of the image.
5917 * Parameters: struct image_params * the image parameter block
5919 * Returns: 0 Success
5920 * EINVAL Invalid argument
5921 * EACCES Permission denied
5922 * EINTR Interrupted function
5923 * ENOMEM Not enough space
5925 * Notes: This is a temporary allocation into the kernel address space
5926 * to enable us to copy arguments in from user space. This is
5927 * necessitated by not mapping the process calling execve() into
5928 * the kernel address space during the execve() system call.
5930 * We assemble the argument and environment, etc., into this
5931 * region before copying it as a single block into the child
5932 * process address space (at the top or bottom of the stack,
5933 * depending on which way the stack grows; see the function
5934 * exec_copyout_strings() for details).
5936 * This ends up with a second (possibly unnecessary) copy compared
5937 * with assembing the data directly into the child address space,
5938 * instead, but since we cannot be guaranteed that the parent has
5939 * not modified its environment, we can't really know that it's
5940 * really a block there as well.
5944 static int execargs_waiters
= 0;
5945 lck_mtx_t
*execargs_cache_lock
;
5948 execargs_lock_lock(void)
5950 lck_mtx_lock_spin(execargs_cache_lock
);
5954 execargs_lock_unlock(void)
5956 lck_mtx_unlock(execargs_cache_lock
);
5959 static wait_result_t
5960 execargs_lock_sleep(void)
5962 return lck_mtx_sleep(execargs_cache_lock
, LCK_SLEEP_DEFAULT
, &execargs_free_count
, THREAD_INTERRUPTIBLE
);
5965 static kern_return_t
5966 execargs_purgeable_allocate(char **execarg_address
)
5968 kern_return_t kr
= vm_allocate_kernel(bsd_pageable_map
, (vm_offset_t
*)execarg_address
, BSD_PAGEABLE_SIZE_PER_EXEC
, VM_FLAGS_ANYWHERE
| VM_FLAGS_PURGABLE
, VM_KERN_MEMORY_NONE
);
5969 assert(kr
== KERN_SUCCESS
);
5973 static kern_return_t
5974 execargs_purgeable_reference(void *execarg_address
)
5976 int state
= VM_PURGABLE_NONVOLATILE
;
5977 kern_return_t kr
= vm_purgable_control(bsd_pageable_map
, (vm_offset_t
) execarg_address
, VM_PURGABLE_SET_STATE
, &state
);
5979 assert(kr
== KERN_SUCCESS
);
5983 static kern_return_t
5984 execargs_purgeable_volatilize(void *execarg_address
)
5986 int state
= VM_PURGABLE_VOLATILE
| VM_PURGABLE_ORDERING_OBSOLETE
;
5988 kr
= vm_purgable_control(bsd_pageable_map
, (vm_offset_t
) execarg_address
, VM_PURGABLE_SET_STATE
, &state
);
5990 assert(kr
== KERN_SUCCESS
);
5996 execargs_wakeup_waiters(void)
5998 thread_wakeup(&execargs_free_count
);
6002 execargs_alloc(struct image_params
*imgp
)
6006 int i
, cache_index
= -1;
6008 execargs_lock_lock();
6010 while (execargs_free_count
== 0) {
6012 res
= execargs_lock_sleep();
6014 if (res
!= THREAD_AWAKENED
) {
6015 execargs_lock_unlock();
6020 execargs_free_count
--;
6022 for (i
= 0; i
< execargs_cache_size
; i
++) {
6023 vm_offset_t element
= execargs_cache
[i
];
6026 imgp
->ip_strings
= (char *)(execargs_cache
[i
]);
6027 execargs_cache
[i
] = 0;
6032 assert(execargs_free_count
>= 0);
6034 execargs_lock_unlock();
6036 if (cache_index
== -1) {
6037 kret
= execargs_purgeable_allocate(&imgp
->ip_strings
);
6039 kret
= execargs_purgeable_reference(imgp
->ip_strings
);
6042 assert(kret
== KERN_SUCCESS
);
6043 if (kret
!= KERN_SUCCESS
) {
6047 /* last page used to read in file headers */
6048 imgp
->ip_vdata
= imgp
->ip_strings
+ (NCARGS
+ PAGE_SIZE
);
6049 imgp
->ip_strendp
= imgp
->ip_strings
;
6050 imgp
->ip_argspace
= NCARGS
;
6051 imgp
->ip_strspace
= (NCARGS
+ PAGE_SIZE
);
6059 * Description: Free the block of memory used by the execve arguments and the
6060 * first page of the executable by a previous call to the function
6063 * Parameters: struct image_params * the image parameter block
6065 * Returns: 0 Success
6066 * EINVAL Invalid argument
6067 * EINTR Oeration interrupted
6070 execargs_free(struct image_params
*imgp
)
6074 boolean_t needs_wakeup
= FALSE
;
6076 kret
= execargs_purgeable_volatilize(imgp
->ip_strings
);
6078 execargs_lock_lock();
6079 execargs_free_count
++;
6081 for (i
= 0; i
< execargs_cache_size
; i
++) {
6082 vm_offset_t element
= execargs_cache
[i
];
6084 execargs_cache
[i
] = (vm_offset_t
) imgp
->ip_strings
;
6085 imgp
->ip_strings
= NULL
;
6090 assert(imgp
->ip_strings
== NULL
);
6092 if (execargs_waiters
> 0) {
6093 needs_wakeup
= TRUE
;
6096 execargs_lock_unlock();
6098 if (needs_wakeup
== TRUE
) {
6099 execargs_wakeup_waiters();
6102 return kret
== KERN_SUCCESS
? 0 : EINVAL
;
6106 exec_resettextvp(proc_t p
, struct image_params
*imgp
)
6110 vnode_t tvp
= p
->p_textvp
;
6114 offset
= imgp
->ip_arch_offset
;
6117 panic("exec_resettextvp: expected valid vp");
6120 ret
= vnode_ref(vp
);
6124 p
->p_textoff
= offset
;
6126 p
->p_textvp
= NULLVP
; /* this is paranoia */
6131 if (tvp
!= NULLVP
) {
6132 if (vnode_getwithref(tvp
) == 0) {
6139 // Includes the 0-byte (therefore "SIZE" instead of "LEN").
6140 static const size_t CS_CDHASH_STRING_SIZE
= CS_CDHASH_LEN
* 2 + 1;
6143 cdhash_to_string(char str
[CS_CDHASH_STRING_SIZE
], uint8_t const * const cdhash
)
6145 static char const nibble
[] = "0123456789abcdef";
6147 /* Apparently still the safest way to get a hex representation
6149 * xnu's printf routines have %*D/%20D in theory, but "not really", see:
6150 * <rdar://problem/33328859> confusion around %*D/%nD in printf
6152 for (int i
= 0; i
< CS_CDHASH_LEN
; ++i
) {
6153 str
[i
* 2] = nibble
[(cdhash
[i
] & 0xf0) >> 4];
6154 str
[i
* 2 + 1] = nibble
[cdhash
[i
] & 0x0f];
6156 str
[CS_CDHASH_STRING_SIZE
- 1] = 0;
6160 * __EXEC_WAITING_ON_TASKGATED_CODE_SIGNATURE_UPCALL__
6162 * Description: Waits for the userspace daemon to respond to the request
6163 * we made. Function declared non inline to be visible in
6164 * stackshots and spindumps as well as debugging.
6166 __attribute__((noinline
)) int
6167 __EXEC_WAITING_ON_TASKGATED_CODE_SIGNATURE_UPCALL__(mach_port_t task_access_port
, int32_t new_pid
)
6169 return find_code_signature(task_access_port
, new_pid
);
6173 check_for_signature(proc_t p
, struct image_params
*imgp
)
6175 mach_port_t port
= IPC_PORT_NULL
;
6176 kern_return_t kr
= KERN_FAILURE
;
6178 boolean_t unexpected_failure
= FALSE
;
6179 struct cs_blob
*csb
;
6180 boolean_t require_success
= FALSE
;
6181 int spawn
= (imgp
->ip_flags
& IMGPF_SPAWN
);
6182 int vfexec
= (imgp
->ip_flags
& IMGPF_VFORK_EXEC
);
6183 os_reason_t signature_failure_reason
= OS_REASON_NULL
;
6186 * Override inherited code signing flags with the
6187 * ones for the process that is being successfully
6191 p
->p_csflags
= imgp
->ip_csflags
;
6194 /* Set the switch_protect flag on the map */
6195 if (p
->p_csflags
& (CS_HARD
| CS_KILL
)) {
6196 vm_map_switch_protect(get_task_map(p
->task
), TRUE
);
6200 * image activation may be failed due to policy
6201 * which is unexpected but security framework does not
6202 * approve of exec, kill and return immediately.
6204 if (imgp
->ip_mac_return
!= 0) {
6205 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXITREASON_CREATE
) | DBG_FUNC_NONE
,
6206 p
->p_pid
, OS_REASON_EXEC
, EXEC_EXIT_REASON_SECURITY_POLICY
, 0, 0);
6207 signature_failure_reason
= os_reason_create(OS_REASON_EXEC
, EXEC_EXIT_REASON_SECURITY_POLICY
);
6208 error
= imgp
->ip_mac_return
;
6209 unexpected_failure
= TRUE
;
6213 if (imgp
->ip_cs_error
!= OS_REASON_NULL
) {
6214 signature_failure_reason
= imgp
->ip_cs_error
;
6215 imgp
->ip_cs_error
= OS_REASON_NULL
;
6220 /* If the code signature came through the image activation path, we skip the
6221 * taskgated / externally attached path. */
6222 if (imgp
->ip_csflags
& CS_SIGNED
) {
6227 /* The rest of the code is for signatures that either already have been externally
6228 * attached (likely, but not necessarily by a previous run through the taskgated
6229 * path), or that will now be attached by taskgated. */
6231 kr
= task_get_task_access_port(p
->task
, &port
);
6232 if (KERN_SUCCESS
!= kr
|| !IPC_PORT_VALID(port
)) {
6234 if (require_success
) {
6235 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXITREASON_CREATE
) | DBG_FUNC_NONE
,
6236 p
->p_pid
, OS_REASON_CODESIGNING
, CODESIGNING_EXIT_REASON_TASK_ACCESS_PORT
, 0, 0);
6237 signature_failure_reason
= os_reason_create(OS_REASON_CODESIGNING
, CODESIGNING_EXIT_REASON_TASK_ACCESS_PORT
);
6244 * taskgated returns KERN_SUCCESS if it has completed its work
6245 * and the exec should continue, KERN_FAILURE if the exec should
6246 * fail, or it may error out with different error code in an
6247 * event of mig failure (e.g. process was signalled during the
6248 * rpc call, taskgated died, mig server died etc.).
6251 kr
= __EXEC_WAITING_ON_TASKGATED_CODE_SIGNATURE_UPCALL__(port
, p
->p_pid
);
6259 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXITREASON_CREATE
) | DBG_FUNC_NONE
,
6260 p
->p_pid
, OS_REASON_CODESIGNING
, CODESIGNING_EXIT_REASON_TASKGATED_INVALID_SIG
, 0, 0);
6261 signature_failure_reason
= os_reason_create(OS_REASON_CODESIGNING
, CODESIGNING_EXIT_REASON_TASKGATED_INVALID_SIG
);
6266 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXITREASON_CREATE
) | DBG_FUNC_NONE
,
6267 p
->p_pid
, OS_REASON_EXEC
, EXEC_EXIT_REASON_TASKGATED_OTHER
, 0, 0);
6268 signature_failure_reason
= os_reason_create(OS_REASON_EXEC
, EXEC_EXIT_REASON_TASKGATED_OTHER
);
6269 unexpected_failure
= TRUE
;
6273 /* Only do this if exec_resettextvp() did not fail */
6274 if (p
->p_textvp
!= NULLVP
) {
6275 csb
= ubc_cs_blob_get(p
->p_textvp
, -1, p
->p_textoff
);
6278 /* As the enforcement we can do here is very limited, we only allow things that
6279 * are the only reason why this code path still exists:
6280 * Adhoc signed non-platform binaries without special cs_flags and without any
6281 * entitlements (unrestricted ones still pass AMFI). */
6283 /* Revalidate the blob if necessary through bumped generation count. */
6284 (ubc_cs_generation_check(p
->p_textvp
) == 0 ||
6285 ubc_cs_blob_revalidate(p
->p_textvp
, csb
, imgp
, 0) == 0) &&
6286 /* Only CS_ADHOC, no CS_KILL, CS_HARD etc. */
6287 (csb
->csb_flags
& CS_ALLOWED_MACHO
) == CS_ADHOC
&&
6288 /* If it has a CMS blob, it's not adhoc. The CS_ADHOC flag can lie. */
6289 csblob_find_blob_bytes((const uint8_t *)csb
->csb_mem_kaddr
, csb
->csb_mem_size
,
6290 CSSLOT_SIGNATURESLOT
,
6291 CSMAGIC_BLOBWRAPPER
) == NULL
&&
6292 /* It could still be in a trust cache (unlikely with CS_ADHOC), or a magic path. */
6293 csb
->csb_platform_binary
== 0 &&
6294 /* No entitlements, not even unrestricted ones. */
6295 csb
->csb_entitlements_blob
== NULL
) {
6297 p
->p_csflags
|= CS_SIGNED
| CS_VALID
;
6300 uint8_t cdhash
[CS_CDHASH_LEN
];
6301 char cdhash_string
[CS_CDHASH_STRING_SIZE
];
6302 proc_getcdhash(p
, cdhash
);
6303 cdhash_to_string(cdhash_string
, cdhash
);
6304 printf("ignoring detached code signature on '%s' with cdhash '%s' "
6305 "because it is invalid, or not a simple adhoc signature.\n",
6306 p
->p_name
, cdhash_string
);
6313 /* The process's code signature related properties are
6314 * fully set up, so this is an opportune moment to log
6315 * platform binary execution, if desired. */
6316 if (platform_exec_logging
!= 0 && csproc_get_platform_binary(p
)) {
6317 uint8_t cdhash
[CS_CDHASH_LEN
];
6318 char cdhash_string
[CS_CDHASH_STRING_SIZE
];
6319 proc_getcdhash(p
, cdhash
);
6320 cdhash_to_string(cdhash_string
, cdhash
);
6322 os_log(peLog
, "CS Platform Exec Logging: Executing platform signed binary "
6323 "'%s' with cdhash %s\n", p
->p_name
, cdhash_string
);
6326 if (!unexpected_failure
) {
6327 p
->p_csflags
|= CS_KILLED
;
6329 /* make very sure execution fails */
6330 if (vfexec
|| spawn
) {
6331 assert(signature_failure_reason
!= OS_REASON_NULL
);
6332 psignal_vfork_with_reason(p
, p
->task
, imgp
->ip_new_thread
,
6333 SIGKILL
, signature_failure_reason
);
6334 signature_failure_reason
= OS_REASON_NULL
;
6337 assert(signature_failure_reason
!= OS_REASON_NULL
);
6338 psignal_with_reason(p
, SIGKILL
, signature_failure_reason
);
6339 signature_failure_reason
= OS_REASON_NULL
;
6343 if (port
!= IPC_PORT_NULL
) {
6344 ipc_port_release_send(port
);
6347 /* If we hit this, we likely would have leaked an exit reason */
6348 assert(signature_failure_reason
== OS_REASON_NULL
);
6353 * Typically as soon as we start executing this process, the
6354 * first instruction will trigger a VM fault to bring the text
6355 * pages (as executable) into the address space, followed soon
6356 * thereafter by dyld data structures (for dynamic executable).
6357 * To optimize this, as well as improve support for hardware
6358 * debuggers that can only access resident pages present
6359 * in the process' page tables, we prefault some pages if
6360 * possible. Errors are non-fatal.
6363 exec_prefault_data(proc_t p __unused
, struct image_params
*imgp
, load_result_t
*load_result
)
6366 size_t expected_all_image_infos_size
;
6369 * Prefault executable or dyld entry point.
6371 vm_fault(current_map(),
6372 vm_map_trunc_page(load_result
->entry_point
,
6373 vm_map_page_mask(current_map())),
6374 VM_PROT_READ
| VM_PROT_EXECUTE
,
6375 FALSE
, VM_KERN_MEMORY_NONE
,
6376 THREAD_UNINT
, NULL
, 0);
6378 if (imgp
->ip_flags
& IMGPF_IS_64BIT_ADDR
) {
6379 expected_all_image_infos_size
= sizeof(struct user64_dyld_all_image_infos
);
6381 expected_all_image_infos_size
= sizeof(struct user32_dyld_all_image_infos
);
6384 /* Decode dyld anchor structure from <mach-o/dyld_images.h> */
6385 if (load_result
->dynlinker
&&
6386 load_result
->all_image_info_addr
&&
6387 load_result
->all_image_info_size
>= expected_all_image_infos_size
) {
6389 struct user64_dyld_all_image_infos infos64
;
6390 struct user32_dyld_all_image_infos infos32
;
6394 * Pre-fault to avoid copyin() going through the trap handler
6395 * and recovery path.
6397 vm_fault(current_map(),
6398 vm_map_trunc_page(load_result
->all_image_info_addr
,
6399 vm_map_page_mask(current_map())),
6400 VM_PROT_READ
| VM_PROT_WRITE
,
6401 FALSE
, VM_KERN_MEMORY_NONE
,
6402 THREAD_UNINT
, NULL
, 0);
6403 if ((load_result
->all_image_info_addr
& PAGE_MASK
) + expected_all_image_infos_size
> PAGE_SIZE
) {
6404 /* all_image_infos straddles a page */
6405 vm_fault(current_map(),
6406 vm_map_trunc_page(load_result
->all_image_info_addr
+ expected_all_image_infos_size
- 1,
6407 vm_map_page_mask(current_map())),
6408 VM_PROT_READ
| VM_PROT_WRITE
,
6409 FALSE
, VM_KERN_MEMORY_NONE
,
6410 THREAD_UNINT
, NULL
, 0);
6413 ret
= copyin(load_result
->all_image_info_addr
,
6415 expected_all_image_infos_size
);
6416 if (ret
== 0 && all_image_infos
.infos32
.version
>= DYLD_ALL_IMAGE_INFOS_ADDRESS_MINIMUM_VERSION
) {
6417 user_addr_t notification_address
;
6418 user_addr_t dyld_image_address
;
6419 user_addr_t dyld_version_address
;
6420 user_addr_t dyld_all_image_infos_address
;
6421 user_addr_t dyld_slide_amount
;
6423 if (imgp
->ip_flags
& IMGPF_IS_64BIT_ADDR
) {
6424 notification_address
= all_image_infos
.infos64
.notification
;
6425 dyld_image_address
= all_image_infos
.infos64
.dyldImageLoadAddress
;
6426 dyld_version_address
= all_image_infos
.infos64
.dyldVersion
;
6427 dyld_all_image_infos_address
= all_image_infos
.infos64
.dyldAllImageInfosAddress
;
6429 notification_address
= all_image_infos
.infos32
.notification
;
6430 dyld_image_address
= all_image_infos
.infos32
.dyldImageLoadAddress
;
6431 dyld_version_address
= all_image_infos
.infos32
.dyldVersion
;
6432 dyld_all_image_infos_address
= all_image_infos
.infos32
.dyldAllImageInfosAddress
;
6436 * dyld statically sets up the all_image_infos in its Mach-O
6437 * binary at static link time, with pointers relative to its default
6438 * load address. Since ASLR might slide dyld before its first
6439 * instruction is executed, "dyld_slide_amount" tells us how far
6440 * dyld was loaded compared to its default expected load address.
6441 * All other pointers into dyld's image should be adjusted by this
6442 * amount. At some point later, dyld will fix up pointers to take
6443 * into account the slide, at which point the all_image_infos_address
6444 * field in the structure will match the runtime load address, and
6445 * "dyld_slide_amount" will be 0, if we were to consult it again.
6448 dyld_slide_amount
= load_result
->all_image_info_addr
- dyld_all_image_infos_address
;
6451 kprintf("exec_prefault: 0x%016llx 0x%08x 0x%016llx 0x%016llx 0x%016llx 0x%016llx\n",
6452 (uint64_t)load_result
->all_image_info_addr
,
6453 all_image_infos
.infos32
.version
,
6454 (uint64_t)notification_address
,
6455 (uint64_t)dyld_image_address
,
6456 (uint64_t)dyld_version_address
,
6457 (uint64_t)dyld_all_image_infos_address
);
6460 vm_fault(current_map(),
6461 vm_map_trunc_page(notification_address
+ dyld_slide_amount
,
6462 vm_map_page_mask(current_map())),
6463 VM_PROT_READ
| VM_PROT_EXECUTE
,
6464 FALSE
, VM_KERN_MEMORY_NONE
,
6465 THREAD_UNINT
, NULL
, 0);
6466 vm_fault(current_map(),
6467 vm_map_trunc_page(dyld_image_address
+ dyld_slide_amount
,
6468 vm_map_page_mask(current_map())),
6469 VM_PROT_READ
| VM_PROT_EXECUTE
,
6470 FALSE
, VM_KERN_MEMORY_NONE
,
6471 THREAD_UNINT
, NULL
, 0);
6472 vm_fault(current_map(),
6473 vm_map_trunc_page(dyld_version_address
+ dyld_slide_amount
,
6474 vm_map_page_mask(current_map())),
6476 FALSE
, VM_KERN_MEMORY_NONE
,
6477 THREAD_UNINT
, NULL
, 0);
6478 vm_fault(current_map(),
6479 vm_map_trunc_page(dyld_all_image_infos_address
+ dyld_slide_amount
,
6480 vm_map_page_mask(current_map())),
6481 VM_PROT_READ
| VM_PROT_WRITE
,
6482 FALSE
, VM_KERN_MEMORY_NONE
,
6483 THREAD_UNINT
, NULL
, 0);