xref: /xnu-8019.80.24/bsd/kern/kern_exec.c (revision a325d9c4a84054e40bbe985afedcb50ab80993ea)
1 /*
2  * Copyright (c) 2000-2020 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
29 /*
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.
34  */
35 
36 /*-
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.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
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.
60  *
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
71  * SUCH DAMAGE.
72  *
73  *	from: @(#)kern_exec.c	8.1 (Berkeley) 6/10/93
74  */
75 /*
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,
79  * Version 2.0.
80  */
81 #include <machine/reg.h>
82 #include <machine/cpu_capabilities.h>
83 
84 #include <sys/cdefs.h>
85 #include <sys/param.h>
86 #include <sys/systm.h>
87 #include <sys/filedesc.h>
88 #include <sys/kernel.h>
89 #include <sys/proc_internal.h>
90 #include <sys/kauth.h>
91 #include <sys/user.h>
92 #include <sys/socketvar.h>
93 #include <sys/malloc.h>
94 #include <sys/namei.h>
95 #include <sys/mount_internal.h>
96 #include <sys/vnode_internal.h>
97 #include <sys/file_internal.h>
98 #include <sys/stat.h>
99 #include <sys/uio_internal.h>
100 #include <sys/acct.h>
101 #include <sys/exec.h>
102 #include <sys/kdebug.h>
103 #include <sys/signal.h>
104 #include <sys/aio_kern.h>
105 #include <sys/sysproto.h>
106 #include <sys/sysctl.h>
107 #include <sys/persona.h>
108 #include <sys/reason.h>
109 #if SYSV_SHM
110 #include <sys/shm_internal.h>           /* shmexec() */
111 #endif
112 #include <sys/ubc_internal.h>           /* ubc_map() */
113 #include <sys/spawn.h>
114 #include <sys/spawn_internal.h>
115 #include <sys/process_policy.h>
116 #include <sys/codesign.h>
117 #include <sys/random.h>
118 #include <crypto/sha1.h>
119 
120 #include <libkern/libkern.h>
121 #include <libkern/crypto/sha2.h>
122 #include <security/audit/audit.h>
123 
124 #include <ipc/ipc_types.h>
125 
126 #include <mach/mach_param.h>
127 #include <mach/mach_types.h>
128 #include <mach/port.h>
129 #include <mach/task.h>
130 #include <mach/task_access.h>
131 #include <mach/thread_act.h>
132 #include <mach/vm_map.h>
133 #include <mach/mach_vm.h>
134 #include <mach/vm_param.h>
135 
136 #include <kern/sched_prim.h> /* thread_wakeup() */
137 #include <kern/affinity.h>
138 #include <kern/assert.h>
139 #include <kern/task.h>
140 #include <kern/coalition.h>
141 #include <kern/policy_internal.h>
142 #include <kern/kalloc.h>
143 #include <kern/zalloc.h> /* zone_userspace_reboot_checks() */
144 
145 #include <os/log.h>
146 
147 #if CONFIG_MACF
148 #include <security/mac_framework.h>
149 #include <security/mac_mach_internal.h>
150 #endif
151 
152 #if CONFIG_AUDIT
153 #include <bsm/audit_kevents.h>
154 #endif
155 
156 #if CONFIG_ARCADE
157 #include <kern/arcade.h>
158 #endif
159 
160 #include <vm/vm_map.h>
161 #include <vm/vm_kern.h>
162 #include <vm/vm_protos.h>
163 #include <vm/vm_kern.h>
164 #include <vm/vm_fault.h>
165 #include <vm/vm_pageout.h>
166 #include <vm/pmap.h>
167 
168 #include <kdp/kdp_dyld.h>
169 
170 #include <machine/machine_routines.h>
171 #include <machine/pal_routines.h>
172 
173 #include <pexpert/pexpert.h>
174 
175 #if CONFIG_MEMORYSTATUS
176 #include <sys/kern_memorystatus.h>
177 #endif
178 
179 #include <IOKit/IOBSD.h>
180 #include <IOKit/IOPlatformExpert.h>
181 
182 #include "kern_exec_internal.h"
183 
184 extern boolean_t vm_darkwake_mode;
185 
186 /* enable crash reports on various exec failures */
187 static TUNABLE(bool, bootarg_execfailurereports, "execfailurecrashes", false);
188 
189 #if XNU_TARGET_OS_OSX
190 #if __has_feature(ptrauth_calls)
191 static TUNABLE(bool, bootarg_arm64e_preview_abi, "-arm64e_preview_abi", false);
192 #endif /* __has_feature(ptrauth_calls) */
193 
194 #if DEBUG || DEVELOPMENT
195 static TUNABLE(bool, unentitled_ios_sim_launch, "unentitled_ios_sim_launch", false);
196 #endif /* DEBUG || DEVELOPMENT */
197 #endif /* XNU_TARGET_OS_OSX */
198 
199 #if CONFIG_DTRACE
200 /* Do not include dtrace.h, it redefines kmem_[alloc/free] */
201 extern void dtrace_proc_exec(proc_t);
202 extern void (*dtrace_proc_waitfor_exec_ptr)(proc_t);
203 
204 /*
205  * Since dtrace_proc_waitfor_exec_ptr can be added/removed in dtrace_subr.c,
206  * we will store its value before actually calling it.
207  */
208 static void (*dtrace_proc_waitfor_hook)(proc_t) = NULL;
209 
210 #include <sys/dtrace_ptss.h>
211 #endif
212 
213 #if __has_feature(ptrauth_calls)
214 static TUNABLE_DEV_WRITEABLE(int, vm_shared_region_per_team_id,
215     "vm_shared_region_per_team_id", 1);
216 static TUNABLE_DEV_WRITEABLE(int, vm_shared_region_by_entitlement,
217     "vm_shared_region_by_entitlement", 1);
218 
219 /* Upon userland request, reslide the shared cache. */
220 static TUNABLE_DEV_WRITEABLE(int, vm_shared_region_reslide_aslr,
221     "vm_shared_region_reslide_aslr",
222 #if CONFIG_RESLIDE_SHARED_CACHE
223     1
224 #else
225     0
226 #endif /* CONFIG_RESLIDE_SHARED_CACHE */
227     );
228 
229 /*
230  * Flag to control what processes should get shared cache randomize resliding
231  * after a fault in the shared cache region:
232  *
233  * 0 - all processes get a new randomized slide
234  * 1 - only platform processes get a new randomized slide
235  */
236 TUNABLE_DEV_WRITEABLE(int, vm_shared_region_reslide_restrict,
237     "vm_shared_region_reslide_restrict", 1);
238 
239 #if DEVELOPMENT || DEBUG
240 SYSCTL_INT(_vm, OID_AUTO, vm_shared_region_per_team_id,
241     CTLFLAG_RW, &vm_shared_region_per_team_id, 0, "");
242 SYSCTL_INT(_vm, OID_AUTO, vm_shared_region_by_entitlement,
243     CTLFLAG_RW, &vm_shared_region_by_entitlement, 0, "");
244 SYSCTL_INT(_vm, OID_AUTO, vm_shared_region_reslide_restrict,
245     CTLFLAG_RW, &vm_shared_region_reslide_restrict, 0, "");
246 SYSCTL_INT(_vm, OID_AUTO, vm_shared_region_reslide_aslr,
247     CTLFLAG_RW, &vm_shared_region_reslide_aslr, 0, "");
248 #endif
249 #endif /* __has_feature(ptrauth_calls) */
250 
251 /*
252  * Enable DriverKit shared region. This is disabled by default, and kernelmanagerd/driverkitd will set this to 1 if it
253  * detects the presence of a DriverKit shared cache.
254  */
255 int vm_enable_driverkit_shared_region = 0;
256 
257 thread_t fork_create_child(task_t parent_task,
258     coalition_t *parent_coalition,
259     proc_t child_proc,
260     int inherit_memory,
261     int is_64bit_addr,
262     int is_64bit_data,
263     int in_exec);
264 extern void proc_apply_task_networkbg_internal(proc_t, thread_t);
265 extern void task_set_did_exec_flag(task_t task);
266 extern void task_clear_exec_copy_flag(task_t task);
267 proc_t proc_exec_switch_task(proc_t p, task_t old_task, task_t new_task, thread_t new_thread, void **inherit);
268 boolean_t task_is_active(task_t);
269 boolean_t thread_is_active(thread_t thread);
270 void thread_copy_resource_info(thread_t dst_thread, thread_t src_thread);
271 void *ipc_importance_exec_switch_task(task_t old_task, task_t new_task);
272 extern void ipc_importance_release(void *elem);
273 extern boolean_t task_has_watchports(task_t task);
274 extern void task_set_no_smt(task_t task);
275 #if defined(HAS_APPLE_PAC)
276 char *task_get_vm_shared_region_id_and_jop_pid(task_t task, uint64_t *jop_pid);
277 #endif
278 task_t convert_port_to_task(ipc_port_t port);
279 
280 /*
281  * Mach things for which prototypes are unavailable from Mach headers
282  */
283 #define IPC_OBJECT_COPYIN_FLAGS_ALLOW_IMMOVABLE_SEND 0x1
284 void            ipc_task_reset(
285 	task_t          task);
286 void            ipc_thread_reset(
287 	thread_t        thread);
288 kern_return_t ipc_object_copyin(
289 	ipc_space_t             space,
290 	mach_port_name_t        name,
291 	mach_msg_type_name_t    msgt_name,
292 	ipc_object_t            *objectp,
293 	mach_port_context_t     context,
294 	mach_msg_guard_flags_t  *guard_flags,
295 	uint32_t                kmsg_flags);
296 void ipc_port_release_send(ipc_port_t);
297 
298 #if DEVELOPMENT || DEBUG
299 void task_importance_update_owner_info(task_t);
300 #endif
301 
302 extern struct savearea *get_user_regs(thread_t);
303 
304 __attribute__((noinline)) int __EXEC_WAITING_ON_TASKGATED_CODE_SIGNATURE_UPCALL__(mach_port_t task_access_port, int32_t new_pid);
305 
306 #include <kern/thread.h>
307 #include <kern/task.h>
308 #include <kern/ast.h>
309 #include <kern/mach_loader.h>
310 #include <kern/mach_fat.h>
311 #include <mach-o/fat.h>
312 #include <mach-o/loader.h>
313 #include <machine/vmparam.h>
314 #include <sys/imgact.h>
315 
316 #include <sys/sdt.h>
317 
318 
319 /*
320  * EAI_ITERLIMIT	The maximum number of times to iterate an image
321  *			activator in exec_activate_image() before treating
322  *			it as malformed/corrupt.
323  */
324 #define EAI_ITERLIMIT           3
325 
326 /*
327  * For #! interpreter parsing
328  */
329 #define IS_WHITESPACE(ch) ((ch == ' ') || (ch == '\t'))
330 #define IS_EOL(ch) ((ch == '#') || (ch == '\n'))
331 
332 extern vm_map_t bsd_pageable_map;
333 extern const struct fileops vnops;
334 extern int nextpidversion;
335 
336 
337 #define USER_ADDR_ALIGN(addr, val) \
338 	( ( (user_addr_t)(addr) + (val) - 1) \
339 	        & ~((val) - 1) )
340 
341 /*
342  * For subsystem root support
343  */
344 #define SPAWN_SUBSYSTEM_ROOT_ENTITLEMENT "com.apple.private.spawn-subsystem-root"
345 
346 /* Platform Code Exec Logging */
347 static int platform_exec_logging = 0;
348 
349 SYSCTL_DECL(_security_mac);
350 
351 SYSCTL_INT(_security_mac, OID_AUTO, platform_exec_logging, CTLFLAG_RW, &platform_exec_logging, 0,
352     "log cdhashes for all platform binary executions");
353 
354 static os_log_t peLog = OS_LOG_DEFAULT;
355 
356 struct exec_port_actions {
357 	uint32_t portwatch_count;
358 	uint32_t registered_count;
359 	ipc_port_t *portwatch_array;
360 	ipc_port_t *registered_array;
361 };
362 
363 struct image_params;    /* Forward */
364 static int exec_activate_image(struct image_params *imgp);
365 static int exec_copyout_strings(struct image_params *imgp, user_addr_t *stackp);
366 static int load_return_to_errno(load_return_t lrtn);
367 static int execargs_alloc(struct image_params *imgp);
368 static int execargs_free(struct image_params *imgp);
369 static int exec_check_permissions(struct image_params *imgp);
370 static int exec_extract_strings(struct image_params *imgp);
371 static int exec_add_apple_strings(struct image_params *imgp, const load_result_t *load_result);
372 static int exec_handle_sugid(struct image_params *imgp);
373 static int sugid_scripts = 0;
374 SYSCTL_INT(_kern, OID_AUTO, sugid_scripts, CTLFLAG_RW | CTLFLAG_LOCKED, &sugid_scripts, 0, "");
375 static kern_return_t create_unix_stack(vm_map_t map, load_result_t* load_result, proc_t p);
376 static int copyoutptr(user_addr_t ua, user_addr_t ptr, int ptr_size);
377 static void exec_resettextvp(proc_t, struct image_params *);
378 static int check_for_signature(proc_t, struct image_params *);
379 static void exec_prefault_data(proc_t, struct image_params *, load_result_t *);
380 static errno_t exec_handle_port_actions(struct image_params *imgp,
381     struct exec_port_actions *port_actions);
382 static errno_t exec_handle_spawnattr_policy(proc_t p, thread_t thread, int psa_apptype, uint64_t psa_qos_clamp,
383     task_role_t psa_darwin_role, struct exec_port_actions *port_actions);
384 static void exec_port_actions_destroy(struct exec_port_actions *port_actions);
385 
386 /*
387  * exec_add_user_string
388  *
389  * Add the requested string to the string space area.
390  *
391  * Parameters;	struct image_params *		image parameter block
392  *		user_addr_t			string to add to strings area
393  *		int				segment from which string comes
394  *		boolean_t			TRUE if string contributes to NCARGS
395  *
396  * Returns:	0			Success
397  *		!0			Failure errno from copyinstr()
398  *
399  * Implicit returns:
400  *		(imgp->ip_strendp)	updated location of next add, if any
401  *		(imgp->ip_strspace)	updated byte count of space remaining
402  *		(imgp->ip_argspace) updated byte count of space in NCARGS
403  */
404 __attribute__((noinline))
405 static int
exec_add_user_string(struct image_params * imgp,user_addr_t str,int seg,boolean_t is_ncargs)406 exec_add_user_string(struct image_params *imgp, user_addr_t str, int seg, boolean_t is_ncargs)
407 {
408 	int error = 0;
409 
410 	do {
411 		size_t len = 0;
412 		int space;
413 
414 		if (is_ncargs) {
415 			space = imgp->ip_argspace; /* by definition smaller than ip_strspace */
416 		} else {
417 			space = imgp->ip_strspace;
418 		}
419 
420 		if (space <= 0) {
421 			error = E2BIG;
422 			break;
423 		}
424 
425 		if (!UIO_SEG_IS_USER_SPACE(seg)) {
426 			char *kstr = CAST_DOWN(char *, str);     /* SAFE */
427 			error = copystr(kstr, imgp->ip_strendp, space, &len);
428 		} else {
429 			error = copyinstr(str, imgp->ip_strendp, space, &len);
430 		}
431 
432 		imgp->ip_strendp += len;
433 		imgp->ip_strspace -= len;
434 		if (is_ncargs) {
435 			imgp->ip_argspace -= len;
436 		}
437 	} while (error == ENAMETOOLONG);
438 
439 	return error;
440 }
441 
442 /*
443  * dyld is now passed the executable path as a getenv-like variable
444  * in the same fashion as the stack_guard and malloc_entropy keys.
445  */
446 #define EXECUTABLE_KEY "executable_path="
447 
448 /*
449  * exec_save_path
450  *
451  * To support new app package launching for Mac OS X, the dyld needs the
452  * first argument to execve() stored on the user stack.
453  *
454  * Save the executable path name at the bottom of the strings area and set
455  * the argument vector pointer to the location following that to indicate
456  * the start of the argument and environment tuples, setting the remaining
457  * string space count to the size of the string area minus the path length.
458  *
459  * Parameters;	struct image_params *		image parameter block
460  *		char *				path used to invoke program
461  *		int				segment from which path comes
462  *
463  * Returns:	int			0	Success
464  *		EFAULT				Bad address
465  *	copy[in]str:EFAULT			Bad address
466  *	copy[in]str:ENAMETOOLONG		Filename too long
467  *
468  * Implicit returns:
469  *		(imgp->ip_strings)		saved path
470  *		(imgp->ip_strspace)		space remaining in ip_strings
471  *		(imgp->ip_strendp)		start of remaining copy area
472  *		(imgp->ip_argspace)		space remaining of NCARGS
473  *		(imgp->ip_applec)		Initial applev[0]
474  *
475  * Note:	We have to do this before the initial namei() since in the
476  *		path contains symbolic links, namei() will overwrite the
477  *		original path buffer contents.  If the last symbolic link
478  *		resolved was a relative pathname, we would lose the original
479  *		"path", which could be an absolute pathname. This might be
480  *		unacceptable for dyld.
481  */
482 static int
exec_save_path(struct image_params * imgp,user_addr_t path,int seg,const char ** excpath)483 exec_save_path(struct image_params *imgp, user_addr_t path, int seg, const char **excpath)
484 {
485 	int error;
486 	size_t len;
487 	char *kpath;
488 
489 	// imgp->ip_strings can come out of a cache, so we need to obliterate the
490 	// old path.
491 	memset(imgp->ip_strings, '\0', strlen(EXECUTABLE_KEY) + MAXPATHLEN);
492 
493 	len = MIN(MAXPATHLEN, imgp->ip_strspace);
494 
495 	switch (seg) {
496 	case UIO_USERSPACE32:
497 	case UIO_USERSPACE64:   /* Same for copyin()... */
498 		error = copyinstr(path, imgp->ip_strings + strlen(EXECUTABLE_KEY), len, &len);
499 		break;
500 	case UIO_SYSSPACE:
501 		kpath = CAST_DOWN(char *, path); /* SAFE */
502 		error = copystr(kpath, imgp->ip_strings + strlen(EXECUTABLE_KEY), len, &len);
503 		break;
504 	default:
505 		error = EFAULT;
506 		break;
507 	}
508 
509 	if (!error) {
510 		bcopy(EXECUTABLE_KEY, imgp->ip_strings, strlen(EXECUTABLE_KEY));
511 		len += strlen(EXECUTABLE_KEY);
512 
513 		imgp->ip_strendp += len;
514 		imgp->ip_strspace -= len;
515 
516 		if (excpath) {
517 			*excpath = imgp->ip_strings + strlen(EXECUTABLE_KEY);
518 		}
519 	}
520 
521 	return error;
522 }
523 
524 /*
525  * exec_reset_save_path
526  *
527  * If we detect a shell script, we need to reset the string area
528  * state so that the interpreter can be saved onto the stack.
529  *
530  * Parameters;	struct image_params *		image parameter block
531  *
532  * Returns:	int			0	Success
533  *
534  * Implicit returns:
535  *		(imgp->ip_strings)		saved path
536  *		(imgp->ip_strspace)		space remaining in ip_strings
537  *		(imgp->ip_strendp)		start of remaining copy area
538  *		(imgp->ip_argspace)		space remaining of NCARGS
539  *
540  */
541 static int
exec_reset_save_path(struct image_params * imgp)542 exec_reset_save_path(struct image_params *imgp)
543 {
544 	imgp->ip_strendp = imgp->ip_strings;
545 	imgp->ip_argspace = NCARGS;
546 	imgp->ip_strspace = (NCARGS + PAGE_SIZE);
547 
548 	return 0;
549 }
550 
551 /*
552  * exec_shell_imgact
553  *
554  * Image activator for interpreter scripts.  If the image begins with
555  * the characters "#!", then it is an interpreter script.  Verify the
556  * length of the script line indicating the interpreter is not in
557  * excess of the maximum allowed size.  If this is the case, then
558  * break out the arguments, if any, which are separated by white
559  * space, and copy them into the argument save area as if they were
560  * provided on the command line before all other arguments.  The line
561  * ends when we encounter a comment character ('#') or newline.
562  *
563  * Parameters;	struct image_params *	image parameter block
564  *
565  * Returns:	-1			not an interpreter (keep looking)
566  *		-3			Success: interpreter: relookup
567  *		>0			Failure: interpreter: error number
568  *
569  * A return value other than -1 indicates subsequent image activators should
570  * not be given the opportunity to attempt to activate the image.
571  */
572 static int
exec_shell_imgact(struct image_params * imgp)573 exec_shell_imgact(struct image_params *imgp)
574 {
575 	char *vdata = imgp->ip_vdata;
576 	char *ihp;
577 	char *line_startp, *line_endp;
578 	char *interp;
579 
580 	/*
581 	 * Make sure it's a shell script.  If we've already redirected
582 	 * from an interpreted file once, don't do it again.
583 	 */
584 	if (vdata[0] != '#' ||
585 	    vdata[1] != '!' ||
586 	    (imgp->ip_flags & IMGPF_INTERPRET) != 0) {
587 		return -1;
588 	}
589 
590 	if (imgp->ip_origcputype != 0) {
591 		/* Fat header previously matched, don't allow shell script inside */
592 		return -1;
593 	}
594 
595 	imgp->ip_flags |= IMGPF_INTERPRET;
596 	imgp->ip_interp_sugid_fd = -1;
597 	imgp->ip_interp_buffer[0] = '\0';
598 
599 	/* Check to see if SUGID scripts are permitted.  If they aren't then
600 	 * clear the SUGID bits.
601 	 * imgp->ip_vattr is known to be valid.
602 	 */
603 	if (sugid_scripts == 0) {
604 		imgp->ip_origvattr->va_mode &= ~(VSUID | VSGID);
605 	}
606 
607 	/* Try to find the first non-whitespace character */
608 	for (ihp = &vdata[2]; ihp < &vdata[IMG_SHSIZE]; ihp++) {
609 		if (IS_EOL(*ihp)) {
610 			/* Did not find interpreter, "#!\n" */
611 			return ENOEXEC;
612 		} else if (IS_WHITESPACE(*ihp)) {
613 			/* Whitespace, like "#!    /bin/sh\n", keep going. */
614 		} else {
615 			/* Found start of interpreter */
616 			break;
617 		}
618 	}
619 
620 	if (ihp == &vdata[IMG_SHSIZE]) {
621 		/* All whitespace, like "#!           " */
622 		return ENOEXEC;
623 	}
624 
625 	line_startp = ihp;
626 
627 	/* Try to find the end of the interpreter+args string */
628 	for (; ihp < &vdata[IMG_SHSIZE]; ihp++) {
629 		if (IS_EOL(*ihp)) {
630 			/* Got it */
631 			break;
632 		} else {
633 			/* Still part of interpreter or args */
634 		}
635 	}
636 
637 	if (ihp == &vdata[IMG_SHSIZE]) {
638 		/* A long line, like "#! blah blah blah" without end */
639 		return ENOEXEC;
640 	}
641 
642 	/* Backtrack until we find the last non-whitespace */
643 	while (IS_EOL(*ihp) || IS_WHITESPACE(*ihp)) {
644 		ihp--;
645 	}
646 
647 	/* The character after the last non-whitespace is our logical end of line */
648 	line_endp = ihp + 1;
649 
650 	/*
651 	 * Now we have pointers to the usable part of:
652 	 *
653 	 * "#!  /usr/bin/int first    second   third    \n"
654 	 *      ^ line_startp                       ^ line_endp
655 	 */
656 
657 	/* copy the interpreter name */
658 	interp = imgp->ip_interp_buffer;
659 	for (ihp = line_startp; (ihp < line_endp) && !IS_WHITESPACE(*ihp); ihp++) {
660 		*interp++ = *ihp;
661 	}
662 	*interp = '\0';
663 
664 	exec_reset_save_path(imgp);
665 	exec_save_path(imgp, CAST_USER_ADDR_T(imgp->ip_interp_buffer),
666 	    UIO_SYSSPACE, NULL);
667 
668 	/* Copy the entire interpreter + args for later processing into argv[] */
669 	interp = imgp->ip_interp_buffer;
670 	for (ihp = line_startp; (ihp < line_endp); ihp++) {
671 		*interp++ = *ihp;
672 	}
673 	*interp = '\0';
674 
675 #if CONFIG_SETUID
676 	/*
677 	 * If we have an SUID or SGID script, create a file descriptor
678 	 * from the vnode and pass /dev/fd/%d instead of the actual
679 	 * path name so that the script does not get opened twice
680 	 */
681 	if (imgp->ip_origvattr->va_mode & (VSUID | VSGID)) {
682 		proc_t p;
683 		struct fileproc *fp;
684 		int fd;
685 		int error;
686 
687 		p = vfs_context_proc(imgp->ip_vfs_context);
688 		error = falloc(p, &fp, &fd, imgp->ip_vfs_context);
689 		if (error) {
690 			return error;
691 		}
692 
693 		fp->fp_glob->fg_flag = FREAD;
694 		fp->fp_glob->fg_ops = &vnops;
695 		fp_set_data(fp, imgp->ip_vp);
696 
697 		proc_fdlock(p);
698 		procfdtbl_releasefd(p, fd, NULL);
699 		fp_drop(p, fd, fp, 1);
700 		proc_fdunlock(p);
701 		vnode_ref(imgp->ip_vp);
702 
703 		imgp->ip_interp_sugid_fd = fd;
704 	}
705 #endif /* CONFIG_SETUID */
706 
707 	return -3;
708 }
709 
710 
711 
712 /*
713  * exec_fat_imgact
714  *
715  * Image activator for fat 1.0 binaries.  If the binary is fat, then we
716  * need to select an image from it internally, and make that the image
717  * we are going to attempt to execute.  At present, this consists of
718  * reloading the first page for the image with a first page from the
719  * offset location indicated by the fat header.
720  *
721  * Parameters;	struct image_params *	image parameter block
722  *
723  * Returns:	-1			not a fat binary (keep looking)
724  *		-2			Success: encapsulated binary: reread
725  *		>0			Failure: error number
726  *
727  * Important:	This image activator is byte order neutral.
728  *
729  * Note:	A return value other than -1 indicates subsequent image
730  *		activators should not be given the opportunity to attempt
731  *		to activate the image.
732  *
733  *              If we find an encapsulated binary, we make no assertions
734  *		about its  validity; instead, we leave that up to a rescan
735  *		for an activator to claim it, and, if it is claimed by one,
736  *		that activator is responsible for determining validity.
737  */
738 static int
exec_fat_imgact(struct image_params * imgp)739 exec_fat_imgact(struct image_params *imgp)
740 {
741 	proc_t p = vfs_context_proc(imgp->ip_vfs_context);
742 	kauth_cred_t cred = kauth_cred_proc_ref(p);
743 	struct fat_header *fat_header = (struct fat_header *)imgp->ip_vdata;
744 	struct _posix_spawnattr *psa = NULL;
745 	struct fat_arch fat_arch;
746 	int resid, error;
747 	load_return_t lret;
748 
749 	if (imgp->ip_origcputype != 0) {
750 		/* Fat header previously matched, don't allow another fat file inside */
751 		error = -1; /* not claimed */
752 		goto bad;
753 	}
754 
755 	/* Make sure it's a fat binary */
756 	if (OSSwapBigToHostInt32(fat_header->magic) != FAT_MAGIC) {
757 		error = -1; /* not claimed */
758 		goto bad;
759 	}
760 
761 	/* imgp->ip_vdata has PAGE_SIZE, zerofilled if the file is smaller */
762 	lret = fatfile_validate_fatarches((vm_offset_t)fat_header, PAGE_SIZE);
763 	if (lret != LOAD_SUCCESS) {
764 		error = load_return_to_errno(lret);
765 		goto bad;
766 	}
767 
768 	/* If posix_spawn binprefs exist, respect those prefs. */
769 	psa = (struct _posix_spawnattr *) imgp->ip_px_sa;
770 	if (psa != NULL && psa->psa_binprefs[0] != 0) {
771 		uint32_t pr = 0;
772 
773 		/* Check each preference listed against all arches in header */
774 		for (pr = 0; pr < NBINPREFS; pr++) {
775 			cpu_type_t pref = psa->psa_binprefs[pr];
776 			cpu_type_t subpref = psa->psa_subcpuprefs[pr];
777 
778 			if (pref == 0) {
779 				/* No suitable arch in the pref list */
780 				error = EBADARCH;
781 				goto bad;
782 			}
783 
784 			if (pref == CPU_TYPE_ANY) {
785 				/* Fall through to regular grading */
786 				goto regular_grading;
787 			}
788 
789 			lret = fatfile_getbestarch_for_cputype(pref,
790 			    subpref,
791 			    (vm_offset_t)fat_header,
792 			    PAGE_SIZE,
793 			    imgp,
794 			    &fat_arch);
795 			if (lret == LOAD_SUCCESS) {
796 				goto use_arch;
797 			}
798 		}
799 
800 		/* Requested binary preference was not honored */
801 		error = EBADEXEC;
802 		goto bad;
803 	}
804 
805 regular_grading:
806 	/* Look up our preferred architecture in the fat file. */
807 	lret = fatfile_getbestarch((vm_offset_t)fat_header,
808 	    PAGE_SIZE,
809 	    imgp,
810 	    &fat_arch,
811 	    (p->p_flag & P_AFFINITY) != 0);
812 	if (lret != LOAD_SUCCESS) {
813 		error = load_return_to_errno(lret);
814 		goto bad;
815 	}
816 
817 use_arch:
818 	/* Read the Mach-O header out of fat_arch */
819 	error = vn_rdwr(UIO_READ, imgp->ip_vp, imgp->ip_vdata,
820 	    PAGE_SIZE, fat_arch.offset,
821 	    UIO_SYSSPACE, (IO_UNIT | IO_NODELOCKED),
822 	    cred, &resid, p);
823 	if (error) {
824 		goto bad;
825 	}
826 
827 	if (resid) {
828 		memset(imgp->ip_vdata + (PAGE_SIZE - resid), 0x0, resid);
829 	}
830 
831 	/* Success.  Indicate we have identified an encapsulated binary */
832 	error = -2;
833 	imgp->ip_arch_offset = (user_size_t)fat_arch.offset;
834 	imgp->ip_arch_size = (user_size_t)fat_arch.size;
835 	imgp->ip_origcputype = fat_arch.cputype;
836 	imgp->ip_origcpusubtype = fat_arch.cpusubtype;
837 
838 bad:
839 	kauth_cred_unref(&cred);
840 	return error;
841 }
842 
843 static int
activate_exec_state(task_t task,proc_t p,thread_t thread,load_result_t * result)844 activate_exec_state(task_t task, proc_t p, thread_t thread, load_result_t *result)
845 {
846 	int ret;
847 
848 	task_set_dyld_info(task, MACH_VM_MIN_ADDRESS, 0);
849 	task_set_64bit(task, result->is_64bit_addr, result->is_64bit_data);
850 	if (result->is_64bit_addr) {
851 		OSBitOrAtomic(P_LP64, &p->p_flag);
852 		get_bsdthread_info(thread)->uu_flag |= UT_LP64;
853 	} else {
854 		OSBitAndAtomic(~((uint32_t)P_LP64), &p->p_flag);
855 		get_bsdthread_info(thread)->uu_flag &= ~UT_LP64;
856 	}
857 	task_set_mach_header_address(task, result->mach_header);
858 
859 	ret = thread_state_initialize(thread);
860 	if (ret != KERN_SUCCESS) {
861 		return ret;
862 	}
863 
864 	if (result->threadstate) {
865 		uint32_t *ts = result->threadstate;
866 		uint32_t total_size = (uint32_t)result->threadstate_sz;
867 
868 		while (total_size > 0) {
869 			uint32_t flavor = *ts++;
870 			uint32_t size = *ts++;
871 
872 			ret = thread_setstatus(thread, flavor, (thread_state_t)ts, size);
873 			if (ret) {
874 				return ret;
875 			}
876 			ts += size;
877 			total_size -= (size + 2) * sizeof(uint32_t);
878 		}
879 	}
880 
881 	thread_setentrypoint(thread, result->entry_point);
882 
883 	return KERN_SUCCESS;
884 }
885 
886 
887 void
set_proc_name(struct image_params * imgp,proc_t p)888 set_proc_name(struct image_params *imgp, proc_t p)
889 {
890 	int p_name_len = sizeof(p->p_name) - 1;
891 
892 	if (imgp->ip_ndp->ni_cnd.cn_namelen > p_name_len) {
893 		imgp->ip_ndp->ni_cnd.cn_namelen = p_name_len;
894 	}
895 
896 	bcopy((caddr_t)imgp->ip_ndp->ni_cnd.cn_nameptr, (caddr_t)p->p_name,
897 	    (unsigned)imgp->ip_ndp->ni_cnd.cn_namelen);
898 	p->p_name[imgp->ip_ndp->ni_cnd.cn_namelen] = '\0';
899 
900 	if (imgp->ip_ndp->ni_cnd.cn_namelen > MAXCOMLEN) {
901 		imgp->ip_ndp->ni_cnd.cn_namelen = MAXCOMLEN;
902 	}
903 
904 	bcopy((caddr_t)imgp->ip_ndp->ni_cnd.cn_nameptr, (caddr_t)p->p_comm,
905 	    (unsigned)imgp->ip_ndp->ni_cnd.cn_namelen);
906 	p->p_comm[imgp->ip_ndp->ni_cnd.cn_namelen] = '\0';
907 }
908 
909 #if __has_feature(ptrauth_calls)
910 /**
911  * Returns a team ID string that may be used to assign a shared region.
912  *
913  * Platform binaries do not have team IDs and will return NULL.  Non-platform
914  * binaries without a team ID will be assigned an artificial team ID of ""
915  * (empty string) so that they will not be assigned to the default shared
916  * region.
917  *
918  * @param imgp image parameter block
919  * @return NULL if this is a platform binary, or an appropriate team ID string
920  *         otherwise
921  */
922 static inline const char *
get_teamid_for_shared_region(struct image_params * imgp)923 get_teamid_for_shared_region(struct image_params *imgp)
924 {
925 	assert(imgp->ip_vp != NULL);
926 
927 	const char *ret = csvnode_get_teamid(imgp->ip_vp, imgp->ip_arch_offset);
928 	if (ret) {
929 		return ret;
930 	}
931 
932 	struct cs_blob *blob = csvnode_get_blob(imgp->ip_vp, imgp->ip_arch_offset);
933 	if (csblob_get_platform_binary(blob)) {
934 		return NULL;
935 	} else {
936 		static const char *NO_TEAM_ID = "";
937 		return NO_TEAM_ID;
938 	}
939 }
940 
941 /**
942  * Determines whether ptrauth should be enabled for the provided arm64 CPU subtype.
943  *
944  * @param cpusubtype Mach-O style CPU subtype
945  * @return whether the CPU subtype matches arm64e with the current ptrauth ABI
946  */
947 static inline bool
arm64_cpusubtype_uses_ptrauth(cpu_subtype_t cpusubtype)948 arm64_cpusubtype_uses_ptrauth(cpu_subtype_t cpusubtype)
949 {
950 	return (cpusubtype & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_ARM64E &&
951 	       CPU_SUBTYPE_ARM64_PTR_AUTH_VERSION(cpusubtype) == CPU_SUBTYPE_ARM64_PTR_AUTH_CURRENT_VERSION;
952 }
953 
954 #endif /* __has_feature(ptrauth_calls) */
955 
956 /**
957  * Returns whether a type/subtype slice matches the requested
958  * type/subtype.
959  *
960  * @param mask Bits to mask from the requested/tested cpu type
961  * @param req_cpu Requested cpu type
962  * @param req_subcpu Requested cpu subtype
963  * @param test_cpu Tested slice cpu type
964  * @param test_subcpu Tested slice cpu subtype
965  */
966 boolean_t
binary_match(cpu_type_t mask,cpu_type_t req_cpu,cpu_subtype_t req_subcpu,cpu_type_t test_cpu,cpu_subtype_t test_subcpu)967 binary_match(cpu_type_t mask, cpu_type_t req_cpu,
968     cpu_subtype_t req_subcpu, cpu_type_t test_cpu,
969     cpu_subtype_t test_subcpu)
970 {
971 	if ((test_cpu & ~mask) != (req_cpu & ~mask)) {
972 		return FALSE;
973 	}
974 
975 	test_subcpu &= ~CPU_SUBTYPE_MASK;
976 	req_subcpu  &= ~CPU_SUBTYPE_MASK;
977 
978 	if (test_subcpu != req_subcpu && req_subcpu != (CPU_SUBTYPE_ANY & ~CPU_SUBTYPE_MASK)) {
979 		return FALSE;
980 	}
981 
982 	return TRUE;
983 }
984 
985 
986 /*
987  * exec_mach_imgact
988  *
989  * Image activator for mach-o 1.0 binaries.
990  *
991  * Parameters;	struct image_params *	image parameter block
992  *
993  * Returns:	-1			not a fat binary (keep looking)
994  *		-2			Success: encapsulated binary: reread
995  *		>0			Failure: error number
996  *		EBADARCH		Mach-o binary, but with an unrecognized
997  *					architecture
998  *		ENOMEM			No memory for child process after -
999  *					can only happen after vfork()
1000  *
1001  * Important:	This image activator is NOT byte order neutral.
1002  *
1003  * Note:	A return value other than -1 indicates subsequent image
1004  *		activators should not be given the opportunity to attempt
1005  *		to activate the image.
1006  */
1007 static int
exec_mach_imgact(struct image_params * imgp)1008 exec_mach_imgact(struct image_params *imgp)
1009 {
1010 	struct mach_header *mach_header = (struct mach_header *)imgp->ip_vdata;
1011 	proc_t                  p = vfs_context_proc(imgp->ip_vfs_context);
1012 	int                     error = 0;
1013 	task_t                  task;
1014 	task_t                  new_task = NULL; /* protected by vfexec */
1015 	thread_t                thread;
1016 	struct uthread          *uthread;
1017 	vm_map_t old_map = VM_MAP_NULL;
1018 	vm_map_t map = VM_MAP_NULL;
1019 	load_return_t           lret;
1020 	load_result_t           load_result = {};
1021 	struct _posix_spawnattr *psa = NULL;
1022 	int                     spawn = (imgp->ip_flags & IMGPF_SPAWN);
1023 	const int               vfexec = 0;
1024 	int                     exec = (imgp->ip_flags & IMGPF_EXEC);
1025 	os_reason_t             exec_failure_reason = OS_REASON_NULL;
1026 	boolean_t               reslide = FALSE;
1027 
1028 	/*
1029 	 * make sure it's a Mach-O 1.0 or Mach-O 2.0 binary; the difference
1030 	 * is a reserved field on the end, so for the most part, we can
1031 	 * treat them as if they were identical. Reverse-endian Mach-O
1032 	 * binaries are recognized but not compatible.
1033 	 */
1034 	if ((mach_header->magic == MH_CIGAM) ||
1035 	    (mach_header->magic == MH_CIGAM_64)) {
1036 		error = EBADARCH;
1037 		goto bad;
1038 	}
1039 
1040 	if ((mach_header->magic != MH_MAGIC) &&
1041 	    (mach_header->magic != MH_MAGIC_64)) {
1042 		error = -1;
1043 		goto bad;
1044 	}
1045 
1046 	if (mach_header->filetype != MH_EXECUTE) {
1047 		error = -1;
1048 		goto bad;
1049 	}
1050 
1051 	if (imgp->ip_origcputype != 0) {
1052 		/* Fat header previously had an idea about this thin file */
1053 		if (imgp->ip_origcputype != mach_header->cputype ||
1054 		    imgp->ip_origcpusubtype != mach_header->cpusubtype) {
1055 			error = EBADARCH;
1056 			goto bad;
1057 		}
1058 	} else {
1059 		imgp->ip_origcputype = mach_header->cputype;
1060 		imgp->ip_origcpusubtype = mach_header->cpusubtype;
1061 	}
1062 
1063 	task = current_task();
1064 	thread = current_thread();
1065 	uthread = get_bsdthread_info(thread);
1066 
1067 	if ((mach_header->cputype & CPU_ARCH_ABI64) == CPU_ARCH_ABI64) {
1068 		imgp->ip_flags |= IMGPF_IS_64BIT_ADDR | IMGPF_IS_64BIT_DATA;
1069 	}
1070 
1071 
1072 	/* If posix_spawn binprefs exist, respect those prefs. */
1073 	psa = (struct _posix_spawnattr *) imgp->ip_px_sa;
1074 	if (psa != NULL && psa->psa_binprefs[0] != 0) {
1075 		int pr = 0;
1076 		for (pr = 0; pr < NBINPREFS; pr++) {
1077 			cpu_type_t pref = psa->psa_binprefs[pr];
1078 			cpu_subtype_t subpref = psa->psa_subcpuprefs[pr];
1079 
1080 			if (pref == 0) {
1081 				/* No suitable arch in the pref list */
1082 				error = EBADARCH;
1083 				goto bad;
1084 			}
1085 
1086 			if (pref == CPU_TYPE_ANY) {
1087 				/* Jump to regular grading */
1088 				goto grade;
1089 			}
1090 
1091 			if (binary_match(CPU_ARCH_MASK, pref, subpref,
1092 			    imgp->ip_origcputype, imgp->ip_origcpusubtype)) {
1093 				goto grade;
1094 			}
1095 		}
1096 		error = EBADARCH;
1097 		goto bad;
1098 	}
1099 grade:
1100 	if (!grade_binary(imgp->ip_origcputype, imgp->ip_origcpusubtype & ~CPU_SUBTYPE_MASK,
1101 	    imgp->ip_origcpusubtype & CPU_SUBTYPE_MASK, TRUE)) {
1102 		error = EBADARCH;
1103 		goto bad;
1104 	}
1105 
1106 	if (validate_potential_simulator_binary(imgp->ip_origcputype, imgp,
1107 	    imgp->ip_arch_offset, imgp->ip_arch_size) != LOAD_SUCCESS) {
1108 #if __x86_64__
1109 		const char *excpath;
1110 		error = exec_save_path(imgp, imgp->ip_user_fname, imgp->ip_seg, &excpath);
1111 		os_log_error(OS_LOG_DEFAULT, "Unsupported 32-bit executable: \"%s\"", (error) ? imgp->ip_vp->v_name : excpath);
1112 #endif
1113 		error = EBADARCH;
1114 		goto bad;
1115 	}
1116 
1117 #if defined(HAS_APPLE_PAC)
1118 	assert(mach_header->cputype == CPU_TYPE_ARM64
1119 	    );
1120 
1121 	if ((mach_header->cputype == CPU_TYPE_ARM64 &&
1122 	    arm64_cpusubtype_uses_ptrauth(mach_header->cpusubtype))
1123 	    ) {
1124 		imgp->ip_flags &= ~IMGPF_NOJOP;
1125 	} else {
1126 		imgp->ip_flags |= IMGPF_NOJOP;
1127 	}
1128 #endif
1129 
1130 	/* Copy in arguments/environment from the old process */
1131 	error = exec_extract_strings(imgp);
1132 	if (error) {
1133 		goto bad;
1134 	}
1135 
1136 	AUDIT_ARG(argv, imgp->ip_startargv, imgp->ip_argc,
1137 	    imgp->ip_endargv - imgp->ip_startargv);
1138 	AUDIT_ARG(envv, imgp->ip_endargv, imgp->ip_envc,
1139 	    imgp->ip_endenvv - imgp->ip_endargv);
1140 
1141 
1142 
1143 	/* reset local idea of thread, uthread, task */
1144 	thread = imgp->ip_new_thread;
1145 	uthread = get_bsdthread_info(thread);
1146 	task = new_task = get_threadtask(thread);
1147 
1148 	/*
1149 	 *	Load the Mach-O file.
1150 	 *
1151 	 * NOTE: An error after this point  indicates we have potentially
1152 	 * destroyed or overwritten some process state while attempting an
1153 	 * execve() following a vfork(), which is an unrecoverable condition.
1154 	 * We send the new process an immediate SIGKILL to avoid it executing
1155 	 * any instructions in the mutated address space. For true spawns,
1156 	 * this is not the case, and "too late" is still not too late to
1157 	 * return an error code to the parent process.
1158 	 */
1159 
1160 	/*
1161 	 * Actually load the image file we previously decided to load.
1162 	 */
1163 	lret = load_machfile(imgp, mach_header, thread, &map, &load_result);
1164 	if (lret != LOAD_SUCCESS) {
1165 		error = load_return_to_errno(lret);
1166 
1167 		KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXITREASON_CREATE) | DBG_FUNC_NONE,
1168 		    proc_getpid(p), OS_REASON_EXEC, EXEC_EXIT_REASON_BAD_MACHO, 0, 0);
1169 		if (lret == LOAD_BADMACHO_UPX) {
1170 			set_proc_name(imgp, p);
1171 			exec_failure_reason = os_reason_create(OS_REASON_EXEC, EXEC_EXIT_REASON_UPX);
1172 			exec_failure_reason->osr_flags |= OS_REASON_FLAG_GENERATE_CRASH_REPORT;
1173 		} else {
1174 			exec_failure_reason = os_reason_create(OS_REASON_EXEC, EXEC_EXIT_REASON_BAD_MACHO);
1175 
1176 			if (bootarg_execfailurereports) {
1177 				set_proc_name(imgp, p);
1178 				exec_failure_reason->osr_flags |= OS_REASON_FLAG_GENERATE_CRASH_REPORT;
1179 			}
1180 		}
1181 
1182 		exec_failure_reason->osr_flags |= OS_REASON_FLAG_CONSISTENT_FAILURE;
1183 
1184 		goto badtoolate;
1185 	}
1186 
1187 #ifndef KASAN
1188 	/*
1189 	 * Security: zone sanity checks on fresh boot or initproc re-exec.
1190 	 * launchd by design does not tear down its own service port on USR (rdar://72797967),
1191 	 * which means here is the earliest point we can assert on empty service port label zone,
1192 	 * after load_machfile() above terminates old launchd's IPC space.
1193 	 *
1194 	 * Disable on KASAN builds since zone_size_allocated() accounts for elements
1195 	 * under quarantine.
1196 	 */
1197 	if (task_pid(task) == 1) {
1198 		assert(get_bsdtask_info(task) == initproc);
1199 		zone_userspace_reboot_checks();
1200 	}
1201 #endif
1202 
1203 	proc_lock(p);
1204 	{
1205 		p->p_cputype = imgp->ip_origcputype;
1206 		p->p_cpusubtype = imgp->ip_origcpusubtype;
1207 	}
1208 	proc_setplatformdata(p, load_result.ip_platform, load_result.lr_min_sdk, load_result.lr_sdk);
1209 	vm_map_set_size_limit(map, proc_limitgetcur(p, RLIMIT_AS));
1210 	vm_map_set_data_limit(map, proc_limitgetcur(p, RLIMIT_DATA));
1211 	vm_map_set_user_wire_limit(map, (vm_size_t)proc_limitgetcur(p, RLIMIT_MEMLOCK));
1212 #if XNU_TARGET_OS_OSX
1213 	if (proc_platform(p) == PLATFORM_IOS) {
1214 		assert(vm_map_is_alien(map));
1215 	} else {
1216 		assert(!vm_map_is_alien(map));
1217 	}
1218 #endif /* XNU_TARGET_OS_OSX */
1219 	proc_unlock(p);
1220 
1221 	/*
1222 	 * Set code-signing flags if this binary is signed, or if parent has
1223 	 * requested them on exec.
1224 	 */
1225 	if (load_result.csflags & CS_VALID) {
1226 		imgp->ip_csflags |= load_result.csflags &
1227 		    (CS_VALID | CS_SIGNED | CS_DEV_CODE | CS_LINKER_SIGNED |
1228 		    CS_HARD | CS_KILL | CS_RESTRICT | CS_ENFORCEMENT | CS_REQUIRE_LV |
1229 		    CS_FORCED_LV | CS_ENTITLEMENTS_VALIDATED | CS_NO_UNTRUSTED_HELPERS | CS_RUNTIME |
1230 		    CS_ENTITLEMENT_FLAGS |
1231 		    CS_EXEC_SET_HARD | CS_EXEC_SET_KILL | CS_EXEC_SET_ENFORCEMENT);
1232 	} else {
1233 		imgp->ip_csflags &= ~CS_VALID;
1234 	}
1235 
1236 	if (proc_getcsflags(p) & CS_EXEC_SET_HARD) {
1237 		imgp->ip_csflags |= CS_HARD;
1238 	}
1239 	if (proc_getcsflags(p) & CS_EXEC_SET_KILL) {
1240 		imgp->ip_csflags |= CS_KILL;
1241 	}
1242 	if (proc_getcsflags(p) & CS_EXEC_SET_ENFORCEMENT) {
1243 		imgp->ip_csflags |= CS_ENFORCEMENT;
1244 	}
1245 	if (proc_getcsflags(p) & CS_EXEC_INHERIT_SIP) {
1246 		if (proc_getcsflags(p) & CS_INSTALLER) {
1247 			imgp->ip_csflags |= CS_INSTALLER;
1248 		}
1249 		if (proc_getcsflags(p) & CS_DATAVAULT_CONTROLLER) {
1250 			imgp->ip_csflags |= CS_DATAVAULT_CONTROLLER;
1251 		}
1252 		if (proc_getcsflags(p) & CS_NVRAM_UNRESTRICTED) {
1253 			imgp->ip_csflags |= CS_NVRAM_UNRESTRICTED;
1254 		}
1255 	}
1256 
1257 #if __has_feature(ptrauth_calls) && defined(XNU_TARGET_OS_OSX)
1258 	/*
1259 	 * ptrauth version 0 is a preview ABI.  Developers can opt into running
1260 	 * their own arm64e binaries for local testing, with the understanding
1261 	 * that future OSes may break ABI.
1262 	 */
1263 	if ((imgp->ip_origcpusubtype & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_ARM64E &&
1264 	    CPU_SUBTYPE_ARM64_PTR_AUTH_VERSION(imgp->ip_origcpusubtype) == 0 &&
1265 	    !load_result.platform_binary &&
1266 	    !bootarg_arm64e_preview_abi) {
1267 		static bool logged_once = false;
1268 		set_proc_name(imgp, p);
1269 
1270 		printf("%s: not running binary \"%s\" built against preview arm64e ABI\n", __func__, p->p_name);
1271 		if (!os_atomic_xchg(&logged_once, true, relaxed)) {
1272 			printf("%s: (to allow this, add \"-arm64e_preview_abi\" to boot-args)\n", __func__);
1273 		}
1274 
1275 		exec_failure_reason = os_reason_create(OS_REASON_EXEC, EXEC_EXIT_REASON_BAD_MACHO);
1276 		if (bootarg_execfailurereports) {
1277 			exec_failure_reason->osr_flags |= OS_REASON_FLAG_GENERATE_CRASH_REPORT;
1278 			exec_failure_reason->osr_flags |= OS_REASON_FLAG_CONSISTENT_FAILURE;
1279 		}
1280 		goto badtoolate;
1281 	}
1282 
1283 	if ((imgp->ip_origcpusubtype & ~CPU_SUBTYPE_MASK) != CPU_SUBTYPE_ARM64E &&
1284 	    imgp->ip_origcputype == CPU_TYPE_ARM64 &&
1285 	    load_result.platform_binary &&
1286 	    (imgp->ip_flags & IMGPF_DRIVER) != 0) {
1287 		set_proc_name(imgp, p);
1288 		printf("%s: disallowing arm64 platform driverkit binary \"%s\", should be arm64e\n", __func__, p->p_name);
1289 		exec_failure_reason = os_reason_create(OS_REASON_EXEC, EXEC_EXIT_REASON_BAD_MACHO);
1290 		if (bootarg_execfailurereports) {
1291 			exec_failure_reason->osr_flags |= OS_REASON_FLAG_GENERATE_CRASH_REPORT;
1292 			exec_failure_reason->osr_flags |= OS_REASON_FLAG_CONSISTENT_FAILURE;
1293 		}
1294 		goto badtoolate;
1295 	}
1296 #endif /* __has_feature(ptrauth_calls) && defined(XNU_TARGET_OS_OSX) */
1297 
1298 
1299 	/*
1300 	 * Set up the shared cache region in the new process.
1301 	 *
1302 	 * Normally there is a single shared region per architecture.
1303 	 * However on systems with Pointer Authentication, we can create
1304 	 * multiple shared caches with the amount of sharing determined
1305 	 * by team-id or entitlement. Inherited shared region IDs are used
1306 	 * for system processes that need to match and be able to inspect
1307 	 * a pre-existing task.
1308 	 */
1309 	int cpu_subtype = 0; /* all cpu_subtypes use the same shared region */
1310 #if __has_feature(ptrauth_calls)
1311 	char *shared_region_id = NULL;
1312 	size_t len;
1313 	char *base;
1314 	const char *cbase;
1315 #define TEAM_ID_PREFIX "T-"
1316 #define ENTITLE_PREFIX "E-"
1317 #define SR_PREFIX_LEN  2
1318 #define SR_ENTITLEMENT "com.apple.pac.shared_region_id"
1319 
1320 	if (cpu_type() == CPU_TYPE_ARM64 &&
1321 	    arm64_cpusubtype_uses_ptrauth(p->p_cpusubtype) &&
1322 	    (imgp->ip_flags & IMGPF_NOJOP) == 0) {
1323 		assertf(p->p_cputype == CPU_TYPE_ARM64,
1324 		    "p %p cpu_type() 0x%x p->p_cputype 0x%x p->p_cpusubtype 0x%x",
1325 		    p, cpu_type(), p->p_cputype, p->p_cpusubtype);
1326 
1327 		/*
1328 		 * arm64e uses pointer authentication, so request a separate
1329 		 * shared region for this CPU subtype.
1330 		 */
1331 		cpu_subtype = p->p_cpusubtype & ~CPU_SUBTYPE_MASK;
1332 
1333 		/*
1334 		 * Determine which shared cache to select based on being told,
1335 		 * matching a team-id or matching an entitlement.
1336 		 */
1337 		if (imgp->ip_inherited_shared_region_id) {
1338 			len = strlen(imgp->ip_inherited_shared_region_id);
1339 			shared_region_id = kalloc_data(len + 1, Z_WAITOK | Z_NOFAIL);
1340 			memcpy(shared_region_id, imgp->ip_inherited_shared_region_id, len + 1);
1341 		} else if ((cbase = get_teamid_for_shared_region(imgp)) != NULL) {
1342 			len = strlen(cbase);
1343 			if (vm_shared_region_per_team_id) {
1344 				shared_region_id = kalloc_data(len + SR_PREFIX_LEN + 1,
1345 				    Z_WAITOK | Z_NOFAIL);
1346 				memcpy(shared_region_id, TEAM_ID_PREFIX, SR_PREFIX_LEN);
1347 				memcpy(shared_region_id + SR_PREFIX_LEN, cbase, len + 1);
1348 			}
1349 		} else if ((base = IOVnodeGetEntitlement(imgp->ip_vp,
1350 		    (int64_t)imgp->ip_arch_offset, SR_ENTITLEMENT)) != NULL) {
1351 			len = strlen(base);
1352 			if (vm_shared_region_by_entitlement) {
1353 				shared_region_id = kalloc_data(len + SR_PREFIX_LEN + 1,
1354 				    Z_WAITOK | Z_NOFAIL);
1355 				memcpy(shared_region_id, ENTITLE_PREFIX, SR_PREFIX_LEN);
1356 				memcpy(shared_region_id + SR_PREFIX_LEN, base, len + 1);
1357 			}
1358 			/* Discard the copy of the entitlement */
1359 			kfree_data(base, len + 1);
1360 		}
1361 	}
1362 
1363 	if (imgp->ip_flags & IMGPF_RESLIDE) {
1364 		reslide = TRUE;
1365 	}
1366 
1367 	/* use "" as the default shared_region_id */
1368 	if (shared_region_id == NULL) {
1369 		shared_region_id = kalloc_data(1, Z_WAITOK | Z_ZERO | Z_NOFAIL);
1370 	}
1371 
1372 	/* ensure there's a unique pointer signing key for this shared_region_id */
1373 	shared_region_key_alloc(shared_region_id,
1374 	    imgp->ip_inherited_shared_region_id != NULL, imgp->ip_inherited_jop_pid);
1375 	task_set_shared_region_id(task, shared_region_id);
1376 	shared_region_id = NULL;
1377 #endif /* __has_feature(ptrauth_calls) */
1378 
1379 	int cputype = cpu_type();
1380 	vm_map_exec(map, task, load_result.is_64bit_addr,
1381 	    (void *)p->p_fd.fd_rdir, cputype, cpu_subtype, reslide, (imgp->ip_flags & IMGPF_DRIVER) != 0 && vm_enable_driverkit_shared_region);
1382 
1383 	/*
1384 	 * Close file descriptors which specify close-on-exec.
1385 	 */
1386 	fdt_exec(p, psa != NULL ? psa->psa_flags : 0);
1387 
1388 	/*
1389 	 * deal with set[ug]id.
1390 	 */
1391 	error = exec_handle_sugid(imgp);
1392 	if (error) {
1393 		vm_map_deallocate(map);
1394 
1395 		KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXITREASON_CREATE) | DBG_FUNC_NONE,
1396 		    proc_getpid(p), OS_REASON_EXEC, EXEC_EXIT_REASON_SUGID_FAILURE, 0, 0);
1397 
1398 		exec_failure_reason = os_reason_create(OS_REASON_EXEC, EXEC_EXIT_REASON_SUGID_FAILURE);
1399 		if (bootarg_execfailurereports) {
1400 			set_proc_name(imgp, p);
1401 			exec_failure_reason->osr_flags |= OS_REASON_FLAG_GENERATE_CRASH_REPORT;
1402 		}
1403 
1404 		goto badtoolate;
1405 	}
1406 
1407 	/*
1408 	 * Commit to new map.
1409 	 *
1410 	 * Swap the new map for the old for target task, which consumes
1411 	 * our new map reference but each leaves us responsible for the
1412 	 * old_map reference.  That lets us get off the pmap associated
1413 	 * with it, and then we can release it.
1414 	 *
1415 	 * The map needs to be set on the target task which is different
1416 	 * than current task, thus swap_task_map is used instead of
1417 	 * vm_map_switch.
1418 	 */
1419 	old_map = swap_task_map(task, thread, map);
1420 	vm_map_deallocate(old_map);
1421 	old_map = NULL;
1422 
1423 	lret = activate_exec_state(task, p, thread, &load_result);
1424 	if (lret != KERN_SUCCESS) {
1425 		KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXITREASON_CREATE) | DBG_FUNC_NONE,
1426 		    proc_getpid(p), OS_REASON_EXEC, EXEC_EXIT_REASON_ACTV_THREADSTATE, 0, 0);
1427 
1428 		exec_failure_reason = os_reason_create(OS_REASON_EXEC, EXEC_EXIT_REASON_ACTV_THREADSTATE);
1429 		if (bootarg_execfailurereports) {
1430 			set_proc_name(imgp, p);
1431 			exec_failure_reason->osr_flags |= OS_REASON_FLAG_GENERATE_CRASH_REPORT;
1432 		}
1433 
1434 		goto badtoolate;
1435 	}
1436 
1437 	/*
1438 	 * deal with voucher on exec-calling thread.
1439 	 */
1440 	if (imgp->ip_new_thread == NULL) {
1441 		thread_set_mach_voucher(current_thread(), IPC_VOUCHER_NULL);
1442 	}
1443 
1444 	/* Make sure we won't interrupt ourself signalling a partial process */
1445 	if (!vfexec && !spawn && (p->p_lflag & P_LTRACED)) {
1446 		psignal(p, SIGTRAP);
1447 	}
1448 
1449 	if (load_result.unixproc &&
1450 	    create_unix_stack(get_task_map(task),
1451 	    &load_result,
1452 	    p) != KERN_SUCCESS) {
1453 		error = load_return_to_errno(LOAD_NOSPACE);
1454 
1455 		KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXITREASON_CREATE) | DBG_FUNC_NONE,
1456 		    proc_getpid(p), OS_REASON_EXEC, EXEC_EXIT_REASON_STACK_ALLOC, 0, 0);
1457 
1458 		exec_failure_reason = os_reason_create(OS_REASON_EXEC, EXEC_EXIT_REASON_STACK_ALLOC);
1459 		if (bootarg_execfailurereports) {
1460 			set_proc_name(imgp, p);
1461 			exec_failure_reason->osr_flags |= OS_REASON_FLAG_GENERATE_CRASH_REPORT;
1462 		}
1463 
1464 		goto badtoolate;
1465 	}
1466 
1467 	/*
1468 	 * The load result will have already been munged by AMFI to include the
1469 	 * platform binary flag if boot-args dictated it (AMFI will mark anything
1470 	 * that doesn't go through the upcall path as a platform binary if its
1471 	 * enforcement is disabled).
1472 	 */
1473 	if (load_result.platform_binary) {
1474 		if (cs_debug) {
1475 			printf("setting platform binary on task: pid = %d\n", proc_getpid(p));
1476 		}
1477 
1478 		/*
1479 		 * We must use 'task' here because the proc's task has not yet been
1480 		 * switched to the new one.
1481 		 */
1482 		task_set_platform_binary(task, TRUE);
1483 	} else {
1484 		if (cs_debug) {
1485 			printf("clearing platform binary on task: pid = %d\n", proc_getpid(p));
1486 		}
1487 
1488 		task_set_platform_binary(task, FALSE);
1489 	}
1490 
1491 	/*
1492 	 * Set starting EXC_GUARD and control port behavior for task now that
1493 	 * platform is set. Use the name directly from imgp since we haven't
1494 	 * set_proc_name() yet. Also make control port for the task and main thread
1495 	 * immovable/pinned based on task's option.
1496 	 *
1497 	 * Must happen before main thread port copyout in exc_add_apple_strings.
1498 	 */
1499 	task_set_exc_guard_ctrl_port_default(task, thread,
1500 	    imgp->ip_ndp->ni_cnd.cn_nameptr,
1501 	    (unsigned)imgp->ip_ndp->ni_cnd.cn_namelen,
1502 	    proc_is_simulated(p),
1503 	    load_result.ip_platform,
1504 	    load_result.lr_sdk);
1505 
1506 	error = exec_add_apple_strings(imgp, &load_result); /* copies out main thread port */
1507 
1508 	if (error) {
1509 		KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXITREASON_CREATE) | DBG_FUNC_NONE,
1510 		    proc_getpid(p), OS_REASON_EXEC, EXEC_EXIT_REASON_APPLE_STRING_INIT, 0, 0);
1511 
1512 		exec_failure_reason = os_reason_create(OS_REASON_EXEC, EXEC_EXIT_REASON_APPLE_STRING_INIT);
1513 		if (bootarg_execfailurereports) {
1514 			set_proc_name(imgp, p);
1515 			exec_failure_reason->osr_flags |= OS_REASON_FLAG_GENERATE_CRASH_REPORT;
1516 		}
1517 		goto badtoolate;
1518 	}
1519 
1520 	/* Switch to target task's map to copy out strings */
1521 	old_map = vm_map_switch(get_task_map(task));
1522 
1523 	if (load_result.unixproc) {
1524 		user_addr_t     ap;
1525 
1526 		/*
1527 		 * Copy the strings area out into the new process address
1528 		 * space.
1529 		 */
1530 		ap = p->user_stack;
1531 		error = exec_copyout_strings(imgp, &ap);
1532 		if (error) {
1533 			vm_map_switch(old_map);
1534 
1535 			KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXITREASON_CREATE) | DBG_FUNC_NONE,
1536 			    proc_getpid(p), OS_REASON_EXEC, EXEC_EXIT_REASON_COPYOUT_STRINGS, 0, 0);
1537 
1538 			exec_failure_reason = os_reason_create(OS_REASON_EXEC, EXEC_EXIT_REASON_COPYOUT_STRINGS);
1539 			if (bootarg_execfailurereports) {
1540 				set_proc_name(imgp, p);
1541 				exec_failure_reason->osr_flags |= OS_REASON_FLAG_GENERATE_CRASH_REPORT;
1542 			}
1543 			goto badtoolate;
1544 		}
1545 		/* Set the stack */
1546 		thread_setuserstack(thread, ap);
1547 	}
1548 
1549 	if (load_result.dynlinker || load_result.is_cambria) {
1550 		user_addr_t        ap;
1551 		int                     new_ptr_size = (imgp->ip_flags & IMGPF_IS_64BIT_ADDR) ? 8 : 4;
1552 
1553 		/* Adjust the stack */
1554 		ap = thread_adjuserstack(thread, -new_ptr_size);
1555 		error = copyoutptr(load_result.mach_header, ap, new_ptr_size);
1556 
1557 		if (error) {
1558 			vm_map_switch(old_map);
1559 
1560 			KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXITREASON_CREATE) | DBG_FUNC_NONE,
1561 			    proc_getpid(p), OS_REASON_EXEC, EXEC_EXIT_REASON_COPYOUT_DYNLINKER, 0, 0);
1562 
1563 			exec_failure_reason = os_reason_create(OS_REASON_EXEC, EXEC_EXIT_REASON_COPYOUT_DYNLINKER);
1564 			if (bootarg_execfailurereports) {
1565 				set_proc_name(imgp, p);
1566 				exec_failure_reason->osr_flags |= OS_REASON_FLAG_GENERATE_CRASH_REPORT;
1567 			}
1568 			goto badtoolate;
1569 		}
1570 		task_set_dyld_info(task, load_result.all_image_info_addr,
1571 		    load_result.all_image_info_size);
1572 	}
1573 
1574 
1575 	/* Avoid immediate VM faults back into kernel */
1576 	exec_prefault_data(p, imgp, &load_result);
1577 
1578 	vm_map_switch(old_map);
1579 
1580 	/*
1581 	 * Reset signal state.
1582 	 */
1583 	execsigs(p, thread);
1584 
1585 	/*
1586 	 * need to cancel async IO requests that can be cancelled and wait for those
1587 	 * already active.  MAY BLOCK!
1588 	 */
1589 	_aio_exec( p );
1590 
1591 #if SYSV_SHM
1592 	/* FIXME: Till vmspace inherit is fixed: */
1593 	if (!vfexec && p->vm_shm) {
1594 		shmexec(p);
1595 	}
1596 #endif
1597 #if SYSV_SEM
1598 	/* Clean up the semaphores */
1599 	semexit(p);
1600 #endif
1601 
1602 	/*
1603 	 * Remember file name for accounting.
1604 	 */
1605 	p->p_acflag &= ~AFORK;
1606 
1607 	set_proc_name(imgp, p);
1608 
1609 #if CONFIG_SECLUDED_MEMORY
1610 	if (secluded_for_apps &&
1611 	    load_result.platform_binary) {
1612 		if (strncmp(p->p_name,
1613 		    "Camera",
1614 		    sizeof(p->p_name)) == 0) {
1615 			task_set_could_use_secluded_mem(task, TRUE);
1616 		} else {
1617 			task_set_could_use_secluded_mem(task, FALSE);
1618 		}
1619 		if (strncmp(p->p_name,
1620 		    "mediaserverd",
1621 		    sizeof(p->p_name)) == 0) {
1622 			task_set_could_also_use_secluded_mem(task, TRUE);
1623 		}
1624 	}
1625 #endif /* CONFIG_SECLUDED_MEMORY */
1626 
1627 #if __arm64__
1628 	if (load_result.legacy_footprint) {
1629 		task_set_legacy_footprint(task);
1630 	}
1631 #endif /* __arm64__ */
1632 
1633 	pal_dbg_set_task_name(task);
1634 
1635 #if DEVELOPMENT || DEBUG
1636 	/*
1637 	 * Update the pid an proc name for importance base if any
1638 	 */
1639 	task_importance_update_owner_info(task);
1640 #endif
1641 
1642 	proc_setexecutableuuid(p, &load_result.uuid[0]);
1643 
1644 #if CONFIG_DTRACE
1645 	dtrace_proc_exec(p);
1646 #endif
1647 
1648 	if (kdebug_enable) {
1649 		long args[4] = {};
1650 
1651 		uintptr_t fsid = 0, fileid = 0;
1652 		if (imgp->ip_vattr) {
1653 			uint64_t fsid64 = vnode_get_va_fsid(imgp->ip_vattr);
1654 			fsid   = (uintptr_t)fsid64;
1655 			fileid = (uintptr_t)imgp->ip_vattr->va_fileid;
1656 			// check for (unexpected) overflow and trace zero in that case
1657 			if (fsid != fsid64 || fileid != imgp->ip_vattr->va_fileid) {
1658 				fsid = fileid = 0;
1659 			}
1660 		}
1661 		KERNEL_DEBUG_CONSTANT_IST1(TRACE_DATA_EXEC, proc_getpid(p), fsid, fileid, 0,
1662 		    (uintptr_t)thread_tid(thread));
1663 
1664 		/*
1665 		 * Collect the pathname for tracing
1666 		 */
1667 		kdbg_trace_string(p, &args[0], &args[1], &args[2], &args[3]);
1668 		KERNEL_DEBUG_CONSTANT_IST1(TRACE_STRING_EXEC, args[0], args[1],
1669 		    args[2], args[3], (uintptr_t)thread_tid(thread));
1670 	}
1671 
1672 
1673 	/*
1674 	 * If posix_spawned with the START_SUSPENDED flag, stop the
1675 	 * process before it runs.
1676 	 */
1677 	if (imgp->ip_px_sa != NULL) {
1678 		psa = (struct _posix_spawnattr *) imgp->ip_px_sa;
1679 		if (psa->psa_flags & POSIX_SPAWN_START_SUSPENDED) {
1680 			proc_lock(p);
1681 			p->p_stat = SSTOP;
1682 			proc_unlock(p);
1683 			(void) task_suspend_internal(task);
1684 		}
1685 	}
1686 
1687 	/*
1688 	 * mark as execed
1689 	 */
1690 	OSBitOrAtomic(P_EXEC, &p->p_flag);
1691 	proc_resetregister(p);
1692 	if (p->p_pptr && (p->p_lflag & P_LPPWAIT)) {
1693 		proc_lock(p);
1694 		p->p_lflag &= ~P_LPPWAIT;
1695 		proc_unlock(p);
1696 		wakeup((caddr_t)p->p_pptr);
1697 	}
1698 
1699 
1700 	goto done;
1701 
1702 badtoolate:
1703 	/* Don't allow child process to execute any instructions */
1704 	if (!spawn) {
1705 		{
1706 			assert(exec_failure_reason != OS_REASON_NULL);
1707 			psignal_with_reason(p, SIGKILL, exec_failure_reason);
1708 			exec_failure_reason = OS_REASON_NULL;
1709 
1710 			if (exec) {
1711 				/* Terminate the exec copy task */
1712 				task_terminate_internal(task);
1713 			}
1714 		}
1715 
1716 		/* We can't stop this system call at this point, so just pretend we succeeded */
1717 		error = 0;
1718 	} else {
1719 		os_reason_free(exec_failure_reason);
1720 		exec_failure_reason = OS_REASON_NULL;
1721 	}
1722 
1723 done:
1724 	if (load_result.threadstate) {
1725 		kfree_data(load_result.threadstate, load_result.threadstate_sz);
1726 		load_result.threadstate = NULL;
1727 	}
1728 
1729 bad:
1730 	/* If we hit this, we likely would have leaked an exit reason */
1731 	assert(exec_failure_reason == OS_REASON_NULL);
1732 	return error;
1733 }
1734 
1735 
1736 
1737 
1738 /*
1739  * Our image activator table; this is the table of the image types we are
1740  * capable of loading.  We list them in order of preference to ensure the
1741  * fastest image load speed.
1742  *
1743  * XXX hardcoded, for now; should use linker sets
1744  */
1745 struct execsw {
1746 	int(*const ex_imgact)(struct image_params *);
1747 	const char *ex_name;
1748 }const execsw[] = {
1749 	{ exec_mach_imgact, "Mach-o Binary" },
1750 	{ exec_fat_imgact, "Fat Binary" },
1751 	{ exec_shell_imgact, "Interpreter Script" },
1752 	{ NULL, NULL}
1753 };
1754 
1755 
1756 /*
1757  * exec_activate_image
1758  *
1759  * Description:	Iterate through the available image activators, and activate
1760  *		the image associated with the imgp structure.  We start with
1761  *		the activator for Mach-o binaries followed by that for Fat binaries
1762  *		for Interpreter scripts.
1763  *
1764  * Parameters:	struct image_params *	Image parameter block
1765  *
1766  * Returns:	0			Success
1767  *		ENOEXEC			No activator for image.
1768  *		EBADEXEC		The executable is corrupt/unknown
1769  *	execargs_alloc:EINVAL		Invalid argument
1770  *	execargs_alloc:EACCES		Permission denied
1771  *	execargs_alloc:EINTR		Interrupted function
1772  *	execargs_alloc:ENOMEM		Not enough space
1773  *	exec_save_path:EFAULT		Bad address
1774  *	exec_save_path:ENAMETOOLONG	Filename too long
1775  *	exec_check_permissions:EACCES	Permission denied
1776  *	exec_check_permissions:ENOEXEC	Executable file format error
1777  *	exec_check_permissions:ETXTBSY	Text file busy [misuse of error code]
1778  *	exec_check_permissions:???
1779  *	namei:???
1780  *	vn_rdwr:???			[anything vn_rdwr can return]
1781  *	<ex_imgact>:???			[anything an imgact can return]
1782  *	EDEADLK				Process is being terminated
1783  */
1784 static int
exec_activate_image(struct image_params * imgp)1785 exec_activate_image(struct image_params *imgp)
1786 {
1787 	struct nameidata *ndp = NULL;
1788 	const char *excpath;
1789 	int error;
1790 	int resid;
1791 	int once = 1;   /* save SGUID-ness for interpreted files */
1792 	int i;
1793 	int itercount = 0;
1794 	proc_t p = vfs_context_proc(imgp->ip_vfs_context);
1795 
1796 	error = execargs_alloc(imgp);
1797 	if (error) {
1798 		goto bad_notrans;
1799 	}
1800 
1801 	error = exec_save_path(imgp, imgp->ip_user_fname, imgp->ip_seg, &excpath);
1802 	if (error) {
1803 		goto bad_notrans;
1804 	}
1805 
1806 	/* Use excpath, which contains the copyin-ed exec path */
1807 	DTRACE_PROC1(exec, uintptr_t, excpath);
1808 
1809 	ndp = kalloc_type(struct nameidata, Z_WAITOK | Z_ZERO | Z_NOFAIL);
1810 
1811 	NDINIT(ndp, LOOKUP, OP_LOOKUP, FOLLOW | LOCKLEAF | AUDITVNPATH1,
1812 	    UIO_SYSSPACE, CAST_USER_ADDR_T(excpath), imgp->ip_vfs_context);
1813 
1814 again:
1815 	error = namei(ndp);
1816 	if (error) {
1817 		if (error == ERESTART) {
1818 			error = EINTR;
1819 		}
1820 		goto bad_notrans;
1821 	}
1822 	imgp->ip_ndp = ndp;     /* successful namei(); call nameidone() later */
1823 	imgp->ip_vp = ndp->ni_vp;       /* if set, need to vnode_put() at some point */
1824 
1825 	/*
1826 	 * Before we start the transition from binary A to binary B, make
1827 	 * sure another thread hasn't started exiting the process.  We grab
1828 	 * the proc lock to check p_lflag initially, and the transition
1829 	 * mechanism ensures that the value doesn't change after we release
1830 	 * the lock.
1831 	 */
1832 	proc_lock(p);
1833 	if (p->p_lflag & P_LEXIT) {
1834 		error = EDEADLK;
1835 		proc_unlock(p);
1836 		goto bad_notrans;
1837 	}
1838 	error = proc_transstart(p, 1, 0);
1839 	proc_unlock(p);
1840 	if (error) {
1841 		goto bad_notrans;
1842 	}
1843 
1844 	error = exec_check_permissions(imgp);
1845 	if (error) {
1846 		goto bad;
1847 	}
1848 
1849 	/* Copy; avoid invocation of an interpreter overwriting the original */
1850 	if (once) {
1851 		once = 0;
1852 		*imgp->ip_origvattr = *imgp->ip_vattr;
1853 	}
1854 
1855 	error = vn_rdwr(UIO_READ, imgp->ip_vp, imgp->ip_vdata, PAGE_SIZE, 0,
1856 	    UIO_SYSSPACE, IO_NODELOCKED,
1857 	    vfs_context_ucred(imgp->ip_vfs_context),
1858 	    &resid, vfs_context_proc(imgp->ip_vfs_context));
1859 	if (error) {
1860 		goto bad;
1861 	}
1862 
1863 	if (resid) {
1864 		memset(imgp->ip_vdata + (PAGE_SIZE - resid), 0x0, resid);
1865 	}
1866 
1867 encapsulated_binary:
1868 	/* Limit the number of iterations we will attempt on each binary */
1869 	if (++itercount > EAI_ITERLIMIT) {
1870 		error = EBADEXEC;
1871 		goto bad;
1872 	}
1873 	error = -1;
1874 	for (i = 0; error == -1 && execsw[i].ex_imgact != NULL; i++) {
1875 		error = (*execsw[i].ex_imgact)(imgp);
1876 
1877 		switch (error) {
1878 		/* case -1: not claimed: continue */
1879 		case -2:                /* Encapsulated binary, imgp->ip_XXX set for next iteration */
1880 			goto encapsulated_binary;
1881 
1882 		case -3:                /* Interpreter */
1883 #if CONFIG_MACF
1884 			/*
1885 			 * Copy the script label for later use. Note that
1886 			 * the label can be different when the script is
1887 			 * actually read by the interpreter.
1888 			 */
1889 			if (imgp->ip_scriptlabelp) {
1890 				mac_vnode_label_free(imgp->ip_scriptlabelp);
1891 				imgp->ip_scriptlabelp = NULL;
1892 			}
1893 			imgp->ip_scriptlabelp = mac_vnode_label_alloc(NULL);
1894 			if (imgp->ip_scriptlabelp == NULL) {
1895 				error = ENOMEM;
1896 				break;
1897 			}
1898 			mac_vnode_label_copy(mac_vnode_label(imgp->ip_vp),
1899 			    imgp->ip_scriptlabelp);
1900 
1901 			/*
1902 			 * Take a ref of the script vnode for later use.
1903 			 */
1904 			if (imgp->ip_scriptvp) {
1905 				vnode_put(imgp->ip_scriptvp);
1906 				imgp->ip_scriptvp = NULLVP;
1907 			}
1908 			if (vnode_getwithref(imgp->ip_vp) == 0) {
1909 				imgp->ip_scriptvp = imgp->ip_vp;
1910 			}
1911 #endif
1912 
1913 			nameidone(ndp);
1914 
1915 			vnode_put(imgp->ip_vp);
1916 			imgp->ip_vp = NULL;     /* already put */
1917 			imgp->ip_ndp = NULL; /* already nameidone */
1918 
1919 			/* Use excpath, which exec_shell_imgact reset to the interpreter */
1920 			NDINIT(ndp, LOOKUP, OP_LOOKUP, FOLLOW | LOCKLEAF,
1921 			    UIO_SYSSPACE, CAST_USER_ADDR_T(excpath), imgp->ip_vfs_context);
1922 
1923 			proc_transend(p, 0);
1924 			goto again;
1925 
1926 		default:
1927 			break;
1928 		}
1929 	}
1930 
1931 	if (error == -1) {
1932 		error = ENOEXEC;
1933 	} else if (error == 0) {
1934 		if (imgp->ip_flags & IMGPF_INTERPRET && ndp->ni_vp) {
1935 			AUDIT_ARG(vnpath, ndp->ni_vp, ARG_VNODE2);
1936 		}
1937 
1938 		/*
1939 		 * Call out to allow 3rd party notification of exec.
1940 		 * Ignore result of kauth_authorize_fileop call.
1941 		 */
1942 		if (kauth_authorize_fileop_has_listeners()) {
1943 			kauth_authorize_fileop(vfs_context_ucred(imgp->ip_vfs_context),
1944 			    KAUTH_FILEOP_EXEC,
1945 			    (uintptr_t)ndp->ni_vp, 0);
1946 		}
1947 	}
1948 bad:
1949 	proc_transend(p, 0);
1950 
1951 bad_notrans:
1952 	if (imgp->ip_strings) {
1953 		execargs_free(imgp);
1954 	}
1955 	if (imgp->ip_ndp) {
1956 		nameidone(imgp->ip_ndp);
1957 	}
1958 	kfree_type(struct nameidata, ndp);
1959 
1960 	return error;
1961 }
1962 
1963 /*
1964  * exec_validate_spawnattr_policy
1965  *
1966  * Description: Validates the entitlements required to set the apptype.
1967  *
1968  * Parameters:  int psa_apptype         posix spawn attribute apptype
1969  *
1970  * Returns:     0                       Success
1971  *              EPERM                   Failure
1972  */
1973 static errno_t
exec_validate_spawnattr_policy(int psa_apptype)1974 exec_validate_spawnattr_policy(int psa_apptype)
1975 {
1976 	if ((psa_apptype & POSIX_SPAWN_PROC_TYPE_MASK) != 0) {
1977 		int proctype = psa_apptype & POSIX_SPAWN_PROC_TYPE_MASK;
1978 		if (proctype == POSIX_SPAWN_PROC_TYPE_DRIVER) {
1979 			if (!IOCurrentTaskHasEntitlement(POSIX_SPAWN_ENTITLEMENT_DRIVER)) {
1980 				return EPERM;
1981 			}
1982 		}
1983 	}
1984 
1985 	return 0;
1986 }
1987 
1988 /*
1989  * exec_handle_spawnattr_policy
1990  *
1991  * Description: Decode and apply the posix_spawn apptype, qos clamp, and watchport ports to the task.
1992  *
1993  * Parameters:  proc_t p                process to apply attributes to
1994  *              int psa_apptype         posix spawn attribute apptype
1995  *
1996  * Returns:     0                       Success
1997  */
1998 static errno_t
exec_handle_spawnattr_policy(proc_t p,thread_t thread,int psa_apptype,uint64_t psa_qos_clamp,task_role_t psa_darwin_role,struct exec_port_actions * port_actions)1999 exec_handle_spawnattr_policy(proc_t p, thread_t thread, int psa_apptype, uint64_t psa_qos_clamp,
2000     task_role_t psa_darwin_role, struct exec_port_actions *port_actions)
2001 {
2002 	int apptype     = TASK_APPTYPE_NONE;
2003 	int qos_clamp   = THREAD_QOS_UNSPECIFIED;
2004 	task_role_t role = TASK_UNSPECIFIED;
2005 
2006 	if ((psa_apptype & POSIX_SPAWN_PROC_TYPE_MASK) != 0) {
2007 		int proctype = psa_apptype & POSIX_SPAWN_PROC_TYPE_MASK;
2008 
2009 		switch (proctype) {
2010 		case POSIX_SPAWN_PROC_TYPE_DAEMON_INTERACTIVE:
2011 			apptype = TASK_APPTYPE_DAEMON_INTERACTIVE;
2012 			break;
2013 		case POSIX_SPAWN_PROC_TYPE_DAEMON_STANDARD:
2014 			apptype = TASK_APPTYPE_DAEMON_STANDARD;
2015 			break;
2016 		case POSIX_SPAWN_PROC_TYPE_DAEMON_ADAPTIVE:
2017 			apptype = TASK_APPTYPE_DAEMON_ADAPTIVE;
2018 			break;
2019 		case POSIX_SPAWN_PROC_TYPE_DAEMON_BACKGROUND:
2020 			apptype = TASK_APPTYPE_DAEMON_BACKGROUND;
2021 			break;
2022 		case POSIX_SPAWN_PROC_TYPE_APP_DEFAULT:
2023 			apptype = TASK_APPTYPE_APP_DEFAULT;
2024 			break;
2025 		case POSIX_SPAWN_PROC_TYPE_DRIVER:
2026 			apptype = TASK_APPTYPE_DRIVER;
2027 			break;
2028 		default:
2029 			apptype = TASK_APPTYPE_NONE;
2030 			/* TODO: Should an invalid value here fail the spawn? */
2031 			break;
2032 		}
2033 	}
2034 
2035 	if (psa_qos_clamp != POSIX_SPAWN_PROC_CLAMP_NONE) {
2036 		switch (psa_qos_clamp) {
2037 		case POSIX_SPAWN_PROC_CLAMP_UTILITY:
2038 			qos_clamp = THREAD_QOS_UTILITY;
2039 			break;
2040 		case POSIX_SPAWN_PROC_CLAMP_BACKGROUND:
2041 			qos_clamp = THREAD_QOS_BACKGROUND;
2042 			break;
2043 		case POSIX_SPAWN_PROC_CLAMP_MAINTENANCE:
2044 			qos_clamp = THREAD_QOS_MAINTENANCE;
2045 			break;
2046 		default:
2047 			qos_clamp = THREAD_QOS_UNSPECIFIED;
2048 			/* TODO: Should an invalid value here fail the spawn? */
2049 			break;
2050 		}
2051 	}
2052 
2053 	if (psa_darwin_role != PRIO_DARWIN_ROLE_DEFAULT) {
2054 		proc_darwin_role_to_task_role(psa_darwin_role, &role);
2055 	}
2056 
2057 	if (apptype != TASK_APPTYPE_NONE ||
2058 	    qos_clamp != THREAD_QOS_UNSPECIFIED ||
2059 	    role != TASK_UNSPECIFIED ||
2060 	    port_actions->portwatch_count) {
2061 		proc_set_task_spawnpolicy(p->task, thread, apptype, qos_clamp, role,
2062 		    port_actions->portwatch_array, port_actions->portwatch_count);
2063 	}
2064 
2065 	if (port_actions->registered_count) {
2066 		if (mach_ports_register(p->task, port_actions->registered_array,
2067 		    port_actions->registered_count)) {
2068 			return EINVAL;
2069 		}
2070 		/* mach_ports_register() consumed the array */
2071 		port_actions->registered_array = NULL;
2072 		port_actions->registered_count = 0;
2073 	}
2074 
2075 	return 0;
2076 }
2077 
2078 static void
exec_port_actions_destroy(struct exec_port_actions * port_actions)2079 exec_port_actions_destroy(struct exec_port_actions *port_actions)
2080 {
2081 	if (port_actions->portwatch_array) {
2082 		for (uint32_t i = 0; i < port_actions->portwatch_count; i++) {
2083 			ipc_port_t port = NULL;
2084 			if ((port = port_actions->portwatch_array[i]) != NULL) {
2085 				ipc_port_release_send(port);
2086 			}
2087 		}
2088 		kfree_type(ipc_port_t, port_actions->portwatch_count,
2089 		    port_actions->portwatch_array);
2090 	}
2091 
2092 	if (port_actions->registered_array) {
2093 		for (uint32_t i = 0; i < port_actions->registered_count; i++) {
2094 			ipc_port_t port = NULL;
2095 			if ((port = port_actions->registered_array[i]) != NULL) {
2096 				ipc_port_release_send(port);
2097 			}
2098 		}
2099 		kfree_type(ipc_port_t, port_actions->registered_count,
2100 		    port_actions->registered_array);
2101 	}
2102 }
2103 
2104 /*
2105  * exec_handle_port_actions
2106  *
2107  * Description:	Go through the _posix_port_actions_t contents,
2108  *              calling task_set_special_port, task_set_exception_ports
2109  *              and/or audit_session_spawnjoin for the current task.
2110  *
2111  * Parameters:	struct image_params *	Image parameter block
2112  *
2113  * Returns:	0			Success
2114  *              EINVAL			Failure
2115  *              ENOTSUP			Illegal posix_spawn attr flag was set
2116  */
2117 static errno_t
exec_handle_port_actions(struct image_params * imgp,struct exec_port_actions * actions)2118 exec_handle_port_actions(struct image_params *imgp,
2119     struct exec_port_actions *actions)
2120 {
2121 	_posix_spawn_port_actions_t pacts = imgp->ip_px_spa;
2122 #if CONFIG_AUDIT
2123 	proc_t p = vfs_context_proc(imgp->ip_vfs_context);
2124 #endif
2125 	_ps_port_action_t *act = NULL;
2126 	task_t task = get_threadtask(imgp->ip_new_thread);
2127 	ipc_port_t port = NULL;
2128 	errno_t ret = 0;
2129 	int i, portwatch_i = 0, registered_i = 0;
2130 	kern_return_t kr;
2131 	boolean_t task_has_watchport_boost = task_has_watchports(current_task());
2132 	boolean_t in_exec = (imgp->ip_flags & IMGPF_EXEC);
2133 	int ptrauth_task_port_count = 0;
2134 	boolean_t suid_cred_specified = FALSE;
2135 
2136 	for (i = 0; i < pacts->pspa_count; i++) {
2137 		act = &pacts->pspa_actions[i];
2138 
2139 		switch (act->port_type) {
2140 		case PSPA_SPECIAL:
2141 		case PSPA_EXCEPTION:
2142 #if CONFIG_AUDIT
2143 		case PSPA_AU_SESSION:
2144 #endif
2145 			break;
2146 		case PSPA_IMP_WATCHPORTS:
2147 			if (++actions->portwatch_count > TASK_MAX_WATCHPORT_COUNT) {
2148 				ret = EINVAL;
2149 				goto done;
2150 			}
2151 			break;
2152 		case PSPA_REGISTERED_PORTS:
2153 			if (++actions->registered_count > TASK_PORT_REGISTER_MAX) {
2154 				ret = EINVAL;
2155 				goto done;
2156 			}
2157 			break;
2158 
2159 		case PSPA_PTRAUTH_TASK_PORT:
2160 			if (++ptrauth_task_port_count > 1) {
2161 				ret = EINVAL;
2162 				goto done;
2163 			}
2164 			break;
2165 
2166 		case PSPA_SUID_CRED:
2167 			/* Only a single suid credential can be specified. */
2168 			if (suid_cred_specified) {
2169 				ret = EINVAL;
2170 				goto done;
2171 			}
2172 			suid_cred_specified = TRUE;
2173 			break;
2174 
2175 		default:
2176 			ret = EINVAL;
2177 			goto done;
2178 		}
2179 	}
2180 
2181 	if (actions->portwatch_count) {
2182 		if (in_exec && task_has_watchport_boost) {
2183 			ret = EINVAL;
2184 			goto done;
2185 		}
2186 		actions->portwatch_array = kalloc_type(ipc_port_t,
2187 		    actions->portwatch_count, Z_WAITOK | Z_ZERO);
2188 		if (actions->portwatch_array == NULL) {
2189 			ret = ENOMEM;
2190 			goto done;
2191 		}
2192 	}
2193 
2194 	if (actions->registered_count) {
2195 		actions->registered_array = kalloc_type(ipc_port_t,
2196 		    actions->registered_count, Z_WAITOK | Z_ZERO);
2197 		if (actions->registered_array == NULL) {
2198 			ret = ENOMEM;
2199 			goto done;
2200 		}
2201 	}
2202 
2203 	for (i = 0; i < pacts->pspa_count; i++) {
2204 		act = &pacts->pspa_actions[i];
2205 
2206 		if (MACH_PORT_VALID(act->new_port)) {
2207 			kr = ipc_object_copyin(get_task_ipcspace(current_task()),
2208 			    act->new_port, MACH_MSG_TYPE_COPY_SEND,
2209 			    (ipc_object_t *) &port, 0, NULL, IPC_OBJECT_COPYIN_FLAGS_ALLOW_IMMOVABLE_SEND);
2210 
2211 			if (kr != KERN_SUCCESS) {
2212 				ret = EINVAL;
2213 				goto done;
2214 			}
2215 		} else {
2216 			/* it's NULL or DEAD */
2217 			port = CAST_MACH_NAME_TO_PORT(act->new_port);
2218 		}
2219 
2220 		switch (act->port_type) {
2221 		case PSPA_SPECIAL:
2222 			kr = task_set_special_port(task, act->which, port);
2223 
2224 			if (kr != KERN_SUCCESS) {
2225 				ret = EINVAL;
2226 			}
2227 			break;
2228 
2229 		case PSPA_EXCEPTION:
2230 			kr = task_set_exception_ports(task, act->mask, port,
2231 			    act->behavior, act->flavor);
2232 			if (kr != KERN_SUCCESS) {
2233 				ret = EINVAL;
2234 			}
2235 			break;
2236 #if CONFIG_AUDIT
2237 		case PSPA_AU_SESSION:
2238 			ret = audit_session_spawnjoin(p, task, port);
2239 			if (ret) {
2240 				/* audit_session_spawnjoin() has already dropped the reference in case of error. */
2241 				goto done;
2242 			}
2243 
2244 			break;
2245 #endif
2246 		case PSPA_IMP_WATCHPORTS:
2247 			if (actions->portwatch_array) {
2248 				/* hold on to this till end of spawn */
2249 				actions->portwatch_array[portwatch_i++] = port;
2250 			} else {
2251 				ipc_port_release_send(port);
2252 			}
2253 			break;
2254 		case PSPA_REGISTERED_PORTS:
2255 			/* hold on to this till end of spawn */
2256 			actions->registered_array[registered_i++] = port;
2257 			break;
2258 
2259 		case PSPA_PTRAUTH_TASK_PORT:
2260 #if defined(HAS_APPLE_PAC)
2261 			{
2262 				task_t ptr_auth_task = convert_port_to_task(port);
2263 
2264 				if (ptr_auth_task == TASK_NULL) {
2265 					ret = EINVAL;
2266 					break;
2267 				}
2268 
2269 				imgp->ip_inherited_shared_region_id =
2270 				    task_get_vm_shared_region_id_and_jop_pid(ptr_auth_task,
2271 				    &imgp->ip_inherited_jop_pid);
2272 
2273 				/* Deallocate task ref returned by convert_port_to_task */
2274 				task_deallocate(ptr_auth_task);
2275 			}
2276 #endif /* HAS_APPLE_PAC */
2277 
2278 			/* consume the port right in case of success */
2279 			ipc_port_release_send(port);
2280 			break;
2281 
2282 		case PSPA_SUID_CRED:
2283 			imgp->ip_sc_port = port;
2284 			break;
2285 
2286 		default:
2287 			ret = EINVAL;
2288 			break;
2289 		}
2290 
2291 		if (ret) {
2292 			/* action failed, so release port resources */
2293 			ipc_port_release_send(port);
2294 			break;
2295 		}
2296 	}
2297 
2298 done:
2299 	if (0 != ret) {
2300 		DTRACE_PROC1(spawn__port__failure, mach_port_name_t, act->new_port);
2301 	}
2302 	return ret;
2303 }
2304 
2305 /*
2306  * exec_handle_file_actions
2307  *
2308  * Description:	Go through the _posix_file_actions_t contents applying the
2309  *		open, close, and dup2 operations to the open file table for
2310  *		the current process.
2311  *
2312  * Parameters:	struct image_params *	Image parameter block
2313  *
2314  * Returns:	0			Success
2315  *		???
2316  *
2317  * Note:	Actions are applied in the order specified, with the credential
2318  *		of the parent process.  This is done to permit the parent
2319  *		process to utilize POSIX_SPAWN_RESETIDS to drop privilege in
2320  *		the child following operations the child may in fact not be
2321  *		normally permitted to perform.
2322  */
2323 static int
exec_handle_file_actions(struct image_params * imgp,short psa_flags)2324 exec_handle_file_actions(struct image_params *imgp, short psa_flags)
2325 {
2326 	int error = 0;
2327 	int action;
2328 	proc_t p = vfs_context_proc(imgp->ip_vfs_context);
2329 	_posix_spawn_file_actions_t px_sfap = imgp->ip_px_sfa;
2330 	int ival[2];            /* dummy retval for system calls) */
2331 #if CONFIG_AUDIT
2332 	struct uthread *uthread = current_uthread();
2333 #endif
2334 
2335 	for (action = 0; action < px_sfap->psfa_act_count; action++) {
2336 		_psfa_action_t *psfa = &px_sfap->psfa_act_acts[action];
2337 
2338 		switch (psfa->psfaa_type) {
2339 		case PSFA_OPEN: {
2340 			/*
2341 			 * Open is different, in that it requires the use of
2342 			 * a path argument, which is normally copied in from
2343 			 * user space; because of this, we have to support an
2344 			 * open from kernel space that passes an address space
2345 			 * context of UIO_SYSSPACE, and casts the address
2346 			 * argument to a user_addr_t.
2347 			 */
2348 			struct vnode_attr *vap;
2349 			struct nameidata *ndp;
2350 			int mode = psfa->psfaa_openargs.psfao_mode;
2351 			int origfd;
2352 			struct {
2353 				struct vnode_attr va;
2354 				struct nameidata nd;
2355 			} *__open_data;
2356 
2357 			__open_data = kalloc_type(typeof(*__open_data), Z_WAITOK | Z_ZERO);
2358 			if (__open_data == NULL) {
2359 				error = ENOMEM;
2360 				break;
2361 			}
2362 
2363 			vap = &__open_data->va;
2364 			ndp = &__open_data->nd;
2365 
2366 			VATTR_INIT(vap);
2367 			/* Mask off all but regular access permissions */
2368 			mode = ((mode & ~p->p_fd.fd_cmask) & ALLPERMS) & ~S_ISTXT;
2369 			VATTR_SET(vap, va_mode, mode & ACCESSPERMS);
2370 
2371 			AUDIT_SUBCALL_ENTER(OPEN, p, uthread);
2372 
2373 			NDINIT(ndp, LOOKUP, OP_OPEN, FOLLOW | AUDITVNPATH1, UIO_SYSSPACE,
2374 			    CAST_USER_ADDR_T(psfa->psfaa_openargs.psfao_path),
2375 			    imgp->ip_vfs_context);
2376 
2377 			error = open1(imgp->ip_vfs_context, ndp,
2378 			    psfa->psfaa_openargs.psfao_oflag,
2379 			    vap, NULL, NULL, &origfd);
2380 
2381 			kfree_type(typeof(*__open_data), __open_data);
2382 
2383 			AUDIT_SUBCALL_EXIT(uthread, error);
2384 
2385 			/*
2386 			 * If there's an error, or we get the right fd by
2387 			 * accident, then drop out here.  This is easier than
2388 			 * reworking all the open code to preallocate fd
2389 			 * slots, and internally taking one as an argument.
2390 			 */
2391 			if (error || origfd == psfa->psfaa_filedes) {
2392 				break;
2393 			}
2394 
2395 			/*
2396 			 * If we didn't fall out from an error, we ended up
2397 			 * with the wrong fd; so now we've got to try to dup2
2398 			 * it to the right one.
2399 			 */
2400 			AUDIT_SUBCALL_ENTER(DUP2, p, uthread);
2401 			error = dup2(p, origfd, psfa->psfaa_filedes, ival);
2402 			AUDIT_SUBCALL_EXIT(uthread, error);
2403 			if (error) {
2404 				break;
2405 			}
2406 
2407 			/*
2408 			 * Finally, close the original fd.
2409 			 */
2410 			AUDIT_SUBCALL_ENTER(CLOSE, p, uthread);
2411 			error = close_nocancel(p, origfd);
2412 			AUDIT_SUBCALL_EXIT(uthread, error);
2413 		}
2414 		break;
2415 
2416 		case PSFA_DUP2: {
2417 			AUDIT_SUBCALL_ENTER(DUP2, p, uthread);
2418 			error = dup2(p, psfa->psfaa_filedes,
2419 			    psfa->psfaa_dup2args.psfad_newfiledes, ival);
2420 			AUDIT_SUBCALL_EXIT(uthread, error);
2421 		}
2422 		break;
2423 
2424 		case PSFA_FILEPORT_DUP2: {
2425 			ipc_port_t port;
2426 			kern_return_t kr;
2427 			int origfd;
2428 
2429 			if (!MACH_PORT_VALID(psfa->psfaa_fileport)) {
2430 				error = EINVAL;
2431 				break;
2432 			}
2433 
2434 			kr = ipc_object_copyin(get_task_ipcspace(current_task()),
2435 			    psfa->psfaa_fileport, MACH_MSG_TYPE_COPY_SEND,
2436 			    (ipc_object_t *) &port, 0, NULL, IPC_OBJECT_COPYIN_FLAGS_ALLOW_IMMOVABLE_SEND);
2437 
2438 			if (kr != KERN_SUCCESS) {
2439 				error = EINVAL;
2440 				break;
2441 			}
2442 
2443 			error = fileport_makefd(p, port, 0, &origfd);
2444 
2445 			if (IPC_PORT_NULL != port) {
2446 				ipc_port_release_send(port);
2447 			}
2448 
2449 			if (error || origfd == psfa->psfaa_dup2args.psfad_newfiledes) {
2450 				break;
2451 			}
2452 
2453 			AUDIT_SUBCALL_ENTER(DUP2, p, uthread);
2454 			error = dup2(p, origfd,
2455 			    psfa->psfaa_dup2args.psfad_newfiledes, ival);
2456 			AUDIT_SUBCALL_EXIT(uthread, error);
2457 			if (error) {
2458 				break;
2459 			}
2460 
2461 			AUDIT_SUBCALL_ENTER(CLOSE, p, uthread);
2462 			error = close_nocancel(p, origfd);
2463 			AUDIT_SUBCALL_EXIT(uthread, error);
2464 		}
2465 		break;
2466 
2467 		case PSFA_CLOSE: {
2468 			AUDIT_SUBCALL_ENTER(CLOSE, p, uthread);
2469 			error = close_nocancel(p, psfa->psfaa_filedes);
2470 			AUDIT_SUBCALL_EXIT(uthread, error);
2471 		}
2472 		break;
2473 
2474 		case PSFA_INHERIT: {
2475 			struct fileproc *fp;
2476 
2477 			/*
2478 			 * Check to see if the descriptor exists, and
2479 			 * ensure it's -not- marked as close-on-exec.
2480 			 *
2481 			 * Attempting to "inherit" a guarded fd will
2482 			 * result in a error.
2483 			 */
2484 
2485 			proc_fdlock(p);
2486 			if ((fp = fp_get_noref_locked(p, psfa->psfaa_filedes)) == NULL) {
2487 				error = EBADF;
2488 			} else if (fp->fp_guard_attrs) {
2489 				error = fp_guard_exception(p, psfa->psfaa_filedes,
2490 				    fp, kGUARD_EXC_NOCLOEXEC);
2491 			} else {
2492 				fp->fp_flags &= ~FP_CLOEXEC;
2493 				error = 0;
2494 			}
2495 			proc_fdunlock(p);
2496 		}
2497 		break;
2498 
2499 		case PSFA_CHDIR: {
2500 			/*
2501 			 * Chdir is different, in that it requires the use of
2502 			 * a path argument, which is normally copied in from
2503 			 * user space; because of this, we have to support a
2504 			 * chdir from kernel space that passes an address space
2505 			 * context of UIO_SYSSPACE, and casts the address
2506 			 * argument to a user_addr_t.
2507 			 */
2508 			struct nameidata *nd;
2509 			nd = kalloc_type(struct nameidata,
2510 			    Z_WAITOK | Z_ZERO | Z_NOFAIL);
2511 
2512 			AUDIT_SUBCALL_ENTER(CHDIR, p, uthread);
2513 			NDINIT(nd, LOOKUP, OP_CHDIR, FOLLOW | AUDITVNPATH1, UIO_SYSSPACE,
2514 			    CAST_USER_ADDR_T(psfa->psfaa_chdirargs.psfac_path),
2515 			    imgp->ip_vfs_context);
2516 
2517 			error = chdir_internal(p, imgp->ip_vfs_context, nd, 0);
2518 			kfree_type(struct nameidata, nd);
2519 			AUDIT_SUBCALL_EXIT(uthread, error);
2520 		}
2521 		break;
2522 
2523 		case PSFA_FCHDIR: {
2524 			struct fchdir_args fchdira;
2525 
2526 			fchdira.fd = psfa->psfaa_filedes;
2527 
2528 			AUDIT_SUBCALL_ENTER(FCHDIR, p, uthread);
2529 			error = fchdir(p, &fchdira, ival);
2530 			AUDIT_SUBCALL_EXIT(uthread, error);
2531 		}
2532 		break;
2533 
2534 		default:
2535 			error = EINVAL;
2536 			break;
2537 		}
2538 
2539 		/* All file actions failures are considered fatal, per POSIX */
2540 
2541 		if (error) {
2542 			if (PSFA_OPEN == psfa->psfaa_type) {
2543 				DTRACE_PROC1(spawn__open__failure, uintptr_t,
2544 				    psfa->psfaa_openargs.psfao_path);
2545 			} else {
2546 				DTRACE_PROC1(spawn__fd__failure, int, psfa->psfaa_filedes);
2547 			}
2548 			break;
2549 		}
2550 	}
2551 
2552 	if (error != 0 || (psa_flags & POSIX_SPAWN_CLOEXEC_DEFAULT) == 0) {
2553 		return error;
2554 	}
2555 
2556 	/*
2557 	 * If POSIX_SPAWN_CLOEXEC_DEFAULT is set, behave (during
2558 	 * this spawn only) as if "close on exec" is the default
2559 	 * disposition of all pre-existing file descriptors.  In this case,
2560 	 * the list of file descriptors mentioned in the file actions
2561 	 * are the only ones that can be inherited, so mark them now.
2562 	 *
2563 	 * The actual closing part comes later, in fdt_exec().
2564 	 */
2565 	proc_fdlock(p);
2566 	for (action = 0; action < px_sfap->psfa_act_count; action++) {
2567 		_psfa_action_t *psfa = &px_sfap->psfa_act_acts[action];
2568 		int fd = psfa->psfaa_filedes;
2569 
2570 		switch (psfa->psfaa_type) {
2571 		case PSFA_DUP2:
2572 		case PSFA_FILEPORT_DUP2:
2573 			fd = psfa->psfaa_dup2args.psfad_newfiledes;
2574 			OS_FALLTHROUGH;
2575 		case PSFA_OPEN:
2576 		case PSFA_INHERIT:
2577 			*fdflags(p, fd) |= UF_INHERIT;
2578 			break;
2579 
2580 		case PSFA_CLOSE:
2581 		case PSFA_CHDIR:
2582 		case PSFA_FCHDIR:
2583 			/*
2584 			 * Although PSFA_FCHDIR does have a file descriptor, it is not
2585 			 * *creating* one, thus we do not automatically mark it for
2586 			 * inheritance under POSIX_SPAWN_CLOEXEC_DEFAULT. A client that
2587 			 * wishes it to be inherited should use the PSFA_INHERIT action
2588 			 * explicitly.
2589 			 */
2590 			break;
2591 		}
2592 	}
2593 	proc_fdunlock(p);
2594 
2595 	return 0;
2596 }
2597 
2598 #if CONFIG_MACF
2599 /*
2600  * Check that the extension's data is within the bounds of the
2601  * allocation storing all extensions' data
2602  */
2603 static inline errno_t
exec_spawnattr_validate_policyext_data(const struct ip_px_smpx_s * px_s,const _ps_mac_policy_extension_t * ext)2604 exec_spawnattr_validate_policyext_data(const struct ip_px_smpx_s *px_s,
2605     const _ps_mac_policy_extension_t *ext)
2606 {
2607 	uint64_t dataend;
2608 
2609 	if (__improbable(os_add_overflow(ext->dataoff, ext->datalen, &dataend))) {
2610 		return EOVERFLOW;
2611 	}
2612 	if (__improbable(dataend > px_s->datalen)) {
2613 		return EINVAL;
2614 	}
2615 
2616 	return 0;
2617 }
2618 
2619 /*
2620  * exec_spawnattr_getmacpolicyinfo
2621  */
2622 void *
exec_spawnattr_getmacpolicyinfo(const void * macextensions,const char * policyname,size_t * lenp)2623 exec_spawnattr_getmacpolicyinfo(const void *macextensions, const char *policyname, size_t *lenp)
2624 {
2625 	const struct ip_px_smpx_s *px_s = macextensions;
2626 	const struct _posix_spawn_mac_policy_extensions *psmx = NULL;
2627 	int i;
2628 
2629 	if (px_s == NULL) {
2630 		return NULL;
2631 	}
2632 
2633 	psmx = px_s->array;
2634 	if (psmx == NULL) {
2635 		return NULL;
2636 	}
2637 
2638 	for (i = 0; i < psmx->psmx_count; i++) {
2639 		const _ps_mac_policy_extension_t *extension = &psmx->psmx_extensions[i];
2640 		if (strncmp(extension->policyname, policyname, sizeof(extension->policyname)) == 0) {
2641 			if (__improbable(exec_spawnattr_validate_policyext_data(px_s, extension))) {
2642 				panic("invalid mac policy extension data");
2643 			}
2644 			if (lenp != NULL) {
2645 				*lenp = (size_t)extension->datalen;
2646 			}
2647 			return (void *)((uintptr_t)px_s->data + extension->dataoff);
2648 		}
2649 	}
2650 
2651 	if (lenp != NULL) {
2652 		*lenp = 0;
2653 	}
2654 	return NULL;
2655 }
2656 
2657 static int
spawn_copyin_macpolicyinfo(const struct user__posix_spawn_args_desc * px_args,struct ip_px_smpx_s * pxsp)2658 spawn_copyin_macpolicyinfo(const struct user__posix_spawn_args_desc *px_args,
2659     struct ip_px_smpx_s *pxsp)
2660 {
2661 	_posix_spawn_mac_policy_extensions_t psmx = NULL;
2662 	uint8_t *data = NULL;
2663 	uint64_t datalen = 0;
2664 	uint64_t dataoff = 0;
2665 	int error = 0;
2666 
2667 	bzero(pxsp, sizeof(*pxsp));
2668 
2669 	if (px_args->mac_extensions_size < PS_MAC_EXTENSIONS_SIZE(1) ||
2670 	    px_args->mac_extensions_size > PAGE_SIZE) {
2671 		error = EINVAL;
2672 		goto bad;
2673 	}
2674 
2675 	psmx = kalloc_data(px_args->mac_extensions_size, Z_WAITOK);
2676 	if (psmx == NULL) {
2677 		error = ENOMEM;
2678 		goto bad;
2679 	}
2680 
2681 	error = copyin(px_args->mac_extensions, psmx, px_args->mac_extensions_size);
2682 	if (error) {
2683 		goto bad;
2684 	}
2685 
2686 	size_t extsize = PS_MAC_EXTENSIONS_SIZE(psmx->psmx_count);
2687 	if (extsize == 0 || extsize > px_args->mac_extensions_size) {
2688 		error = EINVAL;
2689 		goto bad;
2690 	}
2691 
2692 	for (int i = 0; i < psmx->psmx_count; i++) {
2693 		_ps_mac_policy_extension_t *extension = &psmx->psmx_extensions[i];
2694 		if (extension->datalen == 0 || extension->datalen > PAGE_SIZE) {
2695 			error = EINVAL;
2696 			goto bad;
2697 		}
2698 		if (__improbable(os_add_overflow(datalen, extension->datalen, &datalen))) {
2699 			error = ENOMEM;
2700 			goto bad;
2701 		}
2702 	}
2703 
2704 	data = kalloc_data((vm_size_t)datalen, Z_WAITOK);
2705 	if (data == NULL) {
2706 		error = ENOMEM;
2707 		goto bad;
2708 	}
2709 
2710 	for (int i = 0; i < psmx->psmx_count; i++) {
2711 		_ps_mac_policy_extension_t *extension = &psmx->psmx_extensions[i];
2712 
2713 #if !__LP64__
2714 		if (extension->data > UINT32_MAX) {
2715 			goto bad;
2716 		}
2717 #endif
2718 		error = copyin((user_addr_t)extension->data, &data[dataoff], (size_t)extension->datalen);
2719 		if (error) {
2720 			error = ENOMEM;
2721 			goto bad;
2722 		}
2723 		extension->dataoff = dataoff;
2724 		dataoff += extension->datalen;
2725 	}
2726 
2727 	pxsp->array = psmx;
2728 	pxsp->data = data;
2729 	pxsp->datalen = datalen;
2730 	return 0;
2731 
2732 bad:
2733 	kfree_data(psmx, px_args->mac_extensions_size);
2734 	kfree_data(data, (vm_size_t)datalen);
2735 	return error;
2736 }
2737 #endif /* CONFIG_MACF */
2738 
2739 #if CONFIG_COALITIONS
2740 static inline void
spawn_coalitions_release_all(coalition_t coal[COALITION_NUM_TYPES])2741 spawn_coalitions_release_all(coalition_t coal[COALITION_NUM_TYPES])
2742 {
2743 	for (int c = 0; c < COALITION_NUM_TYPES; c++) {
2744 		if (coal[c]) {
2745 			coalition_remove_active(coal[c]);
2746 			coalition_release(coal[c]);
2747 		}
2748 	}
2749 }
2750 #endif
2751 
2752 #if CONFIG_PERSONAS
2753 static int
spawn_validate_persona(struct _posix_spawn_persona_info * px_persona)2754 spawn_validate_persona(struct _posix_spawn_persona_info *px_persona)
2755 {
2756 	int error = 0;
2757 	struct persona *persona = NULL;
2758 	int verify = px_persona->pspi_flags & POSIX_SPAWN_PERSONA_FLAGS_VERIFY;
2759 
2760 	if (!IOCurrentTaskHasEntitlement( PERSONA_MGMT_ENTITLEMENT)) {
2761 		return EPERM;
2762 	}
2763 
2764 	if (px_persona->pspi_flags & POSIX_SPAWN_PERSONA_GROUPS) {
2765 		if (px_persona->pspi_ngroups > NGROUPS_MAX) {
2766 			return EINVAL;
2767 		}
2768 	}
2769 
2770 	persona = persona_lookup(px_persona->pspi_id);
2771 	if (!persona) {
2772 		error = ESRCH;
2773 		goto out;
2774 	}
2775 
2776 	if (verify) {
2777 		if (px_persona->pspi_flags & POSIX_SPAWN_PERSONA_UID) {
2778 			if (px_persona->pspi_uid != persona_get_uid(persona)) {
2779 				error = EINVAL;
2780 				goto out;
2781 			}
2782 		}
2783 		if (px_persona->pspi_flags & POSIX_SPAWN_PERSONA_GID) {
2784 			if (px_persona->pspi_gid != persona_get_gid(persona)) {
2785 				error = EINVAL;
2786 				goto out;
2787 			}
2788 		}
2789 		if (px_persona->pspi_flags & POSIX_SPAWN_PERSONA_GROUPS) {
2790 			size_t ngroups = 0;
2791 			gid_t groups[NGROUPS_MAX];
2792 
2793 			if (persona_get_groups(persona, &ngroups, groups,
2794 			    px_persona->pspi_ngroups) != 0) {
2795 				error = EINVAL;
2796 				goto out;
2797 			}
2798 			if (ngroups != px_persona->pspi_ngroups) {
2799 				error = EINVAL;
2800 				goto out;
2801 			}
2802 			while (ngroups--) {
2803 				if (px_persona->pspi_groups[ngroups] != groups[ngroups]) {
2804 					error = EINVAL;
2805 					goto out;
2806 				}
2807 			}
2808 			if (px_persona->pspi_gmuid != persona_get_gmuid(persona)) {
2809 				error = EINVAL;
2810 				goto out;
2811 			}
2812 		}
2813 	}
2814 
2815 out:
2816 	if (persona) {
2817 		persona_put(persona);
2818 	}
2819 
2820 	return error;
2821 }
2822 
2823 static int
spawn_persona_adopt(proc_t p,struct _posix_spawn_persona_info * px_persona)2824 spawn_persona_adopt(proc_t p, struct _posix_spawn_persona_info *px_persona)
2825 {
2826 	int ret;
2827 	kauth_cred_t cred;
2828 	struct persona *persona = NULL;
2829 	int override = !!(px_persona->pspi_flags & POSIX_SPAWN_PERSONA_FLAGS_OVERRIDE);
2830 
2831 	if (!override) {
2832 		return persona_proc_adopt_id(p, px_persona->pspi_id, NULL);
2833 	}
2834 
2835 	/*
2836 	 * we want to spawn into the given persona, but we want to override
2837 	 * the kauth with a different UID/GID combo
2838 	 */
2839 	persona = persona_lookup(px_persona->pspi_id);
2840 	if (!persona) {
2841 		return ESRCH;
2842 	}
2843 
2844 	cred = persona_get_cred(persona);
2845 	if (!cred) {
2846 		ret = EINVAL;
2847 		goto out;
2848 	}
2849 
2850 	if (px_persona->pspi_flags & POSIX_SPAWN_PERSONA_UID) {
2851 		cred = kauth_cred_setresuid(cred,
2852 		    px_persona->pspi_uid,
2853 		    px_persona->pspi_uid,
2854 		    px_persona->pspi_uid,
2855 		    KAUTH_UID_NONE);
2856 	}
2857 
2858 	if (px_persona->pspi_flags & POSIX_SPAWN_PERSONA_GID) {
2859 		cred = kauth_cred_setresgid(cred,
2860 		    px_persona->pspi_gid,
2861 		    px_persona->pspi_gid,
2862 		    px_persona->pspi_gid);
2863 	}
2864 
2865 	if (px_persona->pspi_flags & POSIX_SPAWN_PERSONA_GROUPS) {
2866 		cred = kauth_cred_setgroups(cred,
2867 		    px_persona->pspi_groups,
2868 		    px_persona->pspi_ngroups,
2869 		    px_persona->pspi_gmuid);
2870 	}
2871 
2872 	ret = persona_proc_adopt(p, persona, cred);
2873 
2874 out:
2875 	persona_put(persona);
2876 	return ret;
2877 }
2878 #endif
2879 
2880 #if __arm64__
2881 #if DEVELOPMENT || DEBUG
2882 TUNABLE(int, legacy_footprint_entitlement_mode, "legacy_footprint_entitlement_mode",
2883     LEGACY_FOOTPRINT_ENTITLEMENT_IGNORE);
2884 
2885 __startup_func
2886 static void
legacy_footprint_entitlement_mode_init(void)2887 legacy_footprint_entitlement_mode_init(void)
2888 {
2889 	/*
2890 	 * legacy_footprint_entitlement_mode specifies the behavior we want associated
2891 	 * with the entitlement. The supported modes are:
2892 	 *
2893 	 * LEGACY_FOOTPRINT_ENTITLEMENT_IGNORE:
2894 	 *	Indicates that we want every process to have the memory accounting
2895 	 *	that is available in iOS 12.0 and beyond.
2896 	 *
2897 	 * LEGACY_FOOTPRINT_ENTITLEMENT_IOS11_ACCT:
2898 	 *	Indicates that for every process that has the 'legacy footprint entitlement',
2899 	 *      we want to give it the old iOS 11.0 accounting behavior which accounted some
2900 	 *	of the process's memory to the kernel.
2901 	 *
2902 	 * LEGACY_FOOTPRINT_ENTITLEMENT_LIMIT_INCREASE:
2903 	 *      Indicates that for every process that has the 'legacy footprint entitlement',
2904 	 *	we want it to have a higher memory limit which will help them acclimate to the
2905 	 *	iOS 12.0 (& beyond) accounting behavior that does the right accounting.
2906 	 *      The bonus added to the system-wide task limit to calculate this higher memory limit
2907 	 *      is available in legacy_footprint_bonus_mb.
2908 	 */
2909 
2910 	if (legacy_footprint_entitlement_mode < LEGACY_FOOTPRINT_ENTITLEMENT_IGNORE ||
2911 	    legacy_footprint_entitlement_mode > LEGACY_FOOTPRINT_ENTITLEMENT_LIMIT_INCREASE) {
2912 		legacy_footprint_entitlement_mode = LEGACY_FOOTPRINT_ENTITLEMENT_LIMIT_INCREASE;
2913 	}
2914 }
2915 STARTUP(TUNABLES, STARTUP_RANK_MIDDLE, legacy_footprint_entitlement_mode_init);
2916 #else
2917 const int legacy_footprint_entitlement_mode = LEGACY_FOOTPRINT_ENTITLEMENT_IGNORE;
2918 #endif
2919 
2920 static inline void
proc_legacy_footprint_entitled(proc_t p,task_t task)2921 proc_legacy_footprint_entitled(proc_t p, task_t task)
2922 {
2923 #pragma unused(p)
2924 	boolean_t legacy_footprint_entitled;
2925 
2926 	switch (legacy_footprint_entitlement_mode) {
2927 	case LEGACY_FOOTPRINT_ENTITLEMENT_IGNORE:
2928 		/* the entitlement is ignored */
2929 		break;
2930 	case LEGACY_FOOTPRINT_ENTITLEMENT_IOS11_ACCT:
2931 		/* the entitlement grants iOS11 legacy accounting */
2932 		legacy_footprint_entitled = IOTaskHasEntitlement(task,
2933 		    "com.apple.private.memory.legacy_footprint");
2934 		if (legacy_footprint_entitled) {
2935 			task_set_legacy_footprint(task);
2936 		}
2937 		break;
2938 	case LEGACY_FOOTPRINT_ENTITLEMENT_LIMIT_INCREASE:
2939 		/* the entitlement grants a footprint limit increase */
2940 		legacy_footprint_entitled = IOTaskHasEntitlement(task,
2941 		    "com.apple.private.memory.legacy_footprint");
2942 		if (legacy_footprint_entitled) {
2943 			task_set_extra_footprint_limit(task);
2944 		}
2945 		break;
2946 	default:
2947 		break;
2948 	}
2949 }
2950 
2951 static inline void
proc_ios13extended_footprint_entitled(proc_t p,task_t task)2952 proc_ios13extended_footprint_entitled(proc_t p, task_t task)
2953 {
2954 #pragma unused(p)
2955 	boolean_t ios13extended_footprint_entitled;
2956 
2957 	/* the entitlement grants a footprint limit increase */
2958 	ios13extended_footprint_entitled = IOTaskHasEntitlement(task,
2959 	    "com.apple.developer.memory.ios13extended_footprint");
2960 	if (ios13extended_footprint_entitled) {
2961 		task_set_ios13extended_footprint_limit(task);
2962 	}
2963 }
2964 static inline void
proc_increased_memory_limit_entitled(proc_t p,task_t task)2965 proc_increased_memory_limit_entitled(proc_t p, task_t task)
2966 {
2967 	static const char kIncreasedMemoryLimitEntitlement[] = "com.apple.developer.kernel.increased-memory-limit";
2968 	bool entitled = false;
2969 
2970 	entitled = IOTaskHasEntitlement(task, kIncreasedMemoryLimitEntitlement);
2971 	if (entitled) {
2972 		memorystatus_act_on_entitled_task_limit(p);
2973 	}
2974 }
2975 
2976 /*
2977  * Check for any of the various entitlements that permit a higher
2978  * task footprint limit or alternate accounting and apply them.
2979  */
2980 static inline void
proc_footprint_entitlement_hacks(proc_t p,task_t task)2981 proc_footprint_entitlement_hacks(proc_t p, task_t task)
2982 {
2983 	proc_legacy_footprint_entitled(p, task);
2984 	proc_ios13extended_footprint_entitled(p, task);
2985 	proc_increased_memory_limit_entitled(p, task);
2986 }
2987 #endif /* __arm64__ */
2988 
2989 #if CONFIG_MACF
2990 /*
2991  * Processes with certain entitlements are granted a jumbo-size VM map.
2992  */
2993 static inline void
proc_apply_jit_and_jumbo_va_policies(proc_t p,task_t task)2994 proc_apply_jit_and_jumbo_va_policies(proc_t p, task_t task)
2995 {
2996 	bool jit_entitled;
2997 	jit_entitled = (mac_proc_check_map_anon(p, 0, 0, 0, MAP_JIT, NULL) == 0);
2998 	if (jit_entitled || (IOTaskHasEntitlement(task,
2999 	    "com.apple.developer.kernel.extended-virtual-addressing"))) {
3000 		vm_map_set_jumbo(get_task_map(task));
3001 		if (jit_entitled) {
3002 			vm_map_set_jit_entitled(get_task_map(task));
3003 
3004 		}
3005 	}
3006 }
3007 #endif /* CONFIG_MACF */
3008 
3009 static int
spawn_posix_cred_adopt(proc_t p,struct _posix_spawn_posix_cred_info * px_pcred_info)3010 spawn_posix_cred_adopt(proc_t p,
3011     struct _posix_spawn_posix_cred_info *px_pcred_info)
3012 {
3013 	int error = 0;
3014 
3015 	if (px_pcred_info->pspci_flags & POSIX_SPAWN_POSIX_CRED_GID) {
3016 		struct setgid_args args = {
3017 			.gid = px_pcred_info->pspci_gid,
3018 		};
3019 		error = setgid(p, &args, NULL);
3020 		if (error) {
3021 			return error;
3022 		}
3023 	}
3024 
3025 	if (px_pcred_info->pspci_flags & POSIX_SPAWN_POSIX_CRED_GROUPS) {
3026 		error = setgroups_internal(p,
3027 		    px_pcred_info->pspci_ngroups,
3028 		    px_pcred_info->pspci_groups,
3029 		    px_pcred_info->pspci_gmuid);
3030 		if (error) {
3031 			return error;
3032 		}
3033 	}
3034 
3035 	if (px_pcred_info->pspci_flags & POSIX_SPAWN_POSIX_CRED_UID) {
3036 		struct setuid_args args = {
3037 			.uid = px_pcred_info->pspci_uid,
3038 		};
3039 		error = setuid(p, &args, NULL);
3040 		if (error) {
3041 			return error;
3042 		}
3043 	}
3044 	return 0;
3045 }
3046 
3047 /*
3048  * posix_spawn
3049  *
3050  * Parameters:	uap->pid		Pointer to pid return area
3051  *		uap->fname		File name to exec
3052  *		uap->argp		Argument list
3053  *		uap->envp		Environment list
3054  *
3055  * Returns:	0			Success
3056  *		EINVAL			Invalid argument
3057  *		ENOTSUP			Not supported
3058  *		ENOEXEC			Executable file format error
3059  *	exec_activate_image:EINVAL	Invalid argument
3060  *	exec_activate_image:EACCES	Permission denied
3061  *	exec_activate_image:EINTR	Interrupted function
3062  *	exec_activate_image:ENOMEM	Not enough space
3063  *	exec_activate_image:EFAULT	Bad address
3064  *	exec_activate_image:ENAMETOOLONG	Filename too long
3065  *	exec_activate_image:ENOEXEC	Executable file format error
3066  *	exec_activate_image:ETXTBSY	Text file busy [misuse of error code]
3067  *	exec_activate_image:EAUTH	Image decryption failed
3068  *	exec_activate_image:EBADEXEC	The executable is corrupt/unknown
3069  *	exec_activate_image:???
3070  *	mac_execve_enter:???
3071  *
3072  * TODO:	Expect to need __mac_posix_spawn() at some point...
3073  *		Handle posix_spawnattr_t
3074  *		Handle posix_spawn_file_actions_t
3075  */
3076 int
posix_spawn(proc_t ap,struct posix_spawn_args * uap,int32_t * retval)3077 posix_spawn(proc_t ap, struct posix_spawn_args *uap, int32_t *retval)
3078 {
3079 	proc_t p = ap;
3080 	user_addr_t pid = uap->pid;
3081 	int ival[2];            /* dummy retval for setpgid() */
3082 	char *subsystem_root_path = NULL;
3083 	struct image_params *imgp = NULL;
3084 	struct vnode_attr *vap = NULL;
3085 	struct vnode_attr *origvap = NULL;
3086 	struct uthread  *uthread = 0;   /* compiler complains if not set to 0*/
3087 	int error, sig;
3088 	int is_64 = IS_64BIT_PROCESS(p);
3089 	struct vfs_context context;
3090 	struct user__posix_spawn_args_desc px_args;
3091 	struct _posix_spawnattr px_sa;
3092 	_posix_spawn_file_actions_t px_sfap = NULL;
3093 	_posix_spawn_port_actions_t px_spap = NULL;
3094 	struct __kern_sigaction vec;
3095 	boolean_t spawn_no_exec = FALSE;
3096 	boolean_t proc_transit_set = TRUE;
3097 	boolean_t exec_done = FALSE;
3098 	struct exec_port_actions port_actions = { };
3099 	vm_size_t px_sa_offset = offsetof(struct _posix_spawnattr, psa_ports);
3100 	task_t old_task = current_task();
3101 	task_t new_task = NULL;
3102 	boolean_t should_release_proc_ref = FALSE;
3103 	void *inherit = NULL;
3104 #if CONFIG_PERSONAS
3105 	struct _posix_spawn_persona_info *px_persona = NULL;
3106 #endif
3107 	struct _posix_spawn_posix_cred_info *px_pcred_info = NULL;
3108 	struct {
3109 		struct image_params imgp;
3110 		struct vnode_attr va;
3111 		struct vnode_attr origva;
3112 	} *__spawn_data;
3113 
3114 	/*
3115 	 * Allocate a big chunk for locals instead of using stack since these
3116 	 * structures are pretty big.
3117 	 */
3118 	__spawn_data = kalloc_type(typeof(*__spawn_data), Z_WAITOK | Z_ZERO);
3119 	if (__spawn_data == NULL) {
3120 		error = ENOMEM;
3121 		goto bad;
3122 	}
3123 	imgp = &__spawn_data->imgp;
3124 	vap = &__spawn_data->va;
3125 	origvap = &__spawn_data->origva;
3126 
3127 	/* Initialize the common data in the image_params structure */
3128 	imgp->ip_user_fname = uap->path;
3129 	imgp->ip_user_argv = uap->argv;
3130 	imgp->ip_user_envv = uap->envp;
3131 	imgp->ip_vattr = vap;
3132 	imgp->ip_origvattr = origvap;
3133 	imgp->ip_vfs_context = &context;
3134 	imgp->ip_flags = (is_64 ? IMGPF_WAS_64BIT_ADDR : IMGPF_NONE);
3135 	imgp->ip_seg = (is_64 ? UIO_USERSPACE64 : UIO_USERSPACE32);
3136 	imgp->ip_mac_return = 0;
3137 	imgp->ip_px_persona = NULL;
3138 	imgp->ip_px_pcred_info = NULL;
3139 	imgp->ip_cs_error = OS_REASON_NULL;
3140 	imgp->ip_simulator_binary = IMGPF_SB_DEFAULT;
3141 	imgp->ip_subsystem_root_path = NULL;
3142 	imgp->ip_inherited_shared_region_id = NULL;
3143 	imgp->ip_inherited_jop_pid = 0;
3144 
3145 	if (uap->adesc != USER_ADDR_NULL) {
3146 		if (is_64) {
3147 			error = copyin(uap->adesc, &px_args, sizeof(px_args));
3148 		} else {
3149 			struct user32__posix_spawn_args_desc px_args32;
3150 
3151 			error = copyin(uap->adesc, &px_args32, sizeof(px_args32));
3152 
3153 			/*
3154 			 * Convert arguments descriptor from external 32 bit
3155 			 * representation to internal 64 bit representation
3156 			 */
3157 			px_args.attr_size = px_args32.attr_size;
3158 			px_args.attrp = CAST_USER_ADDR_T(px_args32.attrp);
3159 			px_args.file_actions_size = px_args32.file_actions_size;
3160 			px_args.file_actions = CAST_USER_ADDR_T(px_args32.file_actions);
3161 			px_args.port_actions_size = px_args32.port_actions_size;
3162 			px_args.port_actions = CAST_USER_ADDR_T(px_args32.port_actions);
3163 			px_args.mac_extensions_size = px_args32.mac_extensions_size;
3164 			px_args.mac_extensions = CAST_USER_ADDR_T(px_args32.mac_extensions);
3165 			px_args.coal_info_size = px_args32.coal_info_size;
3166 			px_args.coal_info = CAST_USER_ADDR_T(px_args32.coal_info);
3167 			px_args.persona_info_size = px_args32.persona_info_size;
3168 			px_args.persona_info = CAST_USER_ADDR_T(px_args32.persona_info);
3169 			px_args.posix_cred_info_size = px_args32.posix_cred_info_size;
3170 			px_args.posix_cred_info = CAST_USER_ADDR_T(px_args32.posix_cred_info);
3171 			px_args.subsystem_root_path_size = px_args32.subsystem_root_path_size;
3172 			px_args.subsystem_root_path = CAST_USER_ADDR_T(px_args32.subsystem_root_path);
3173 		}
3174 		if (error) {
3175 			goto bad;
3176 		}
3177 
3178 		if (px_args.attr_size != 0) {
3179 			/*
3180 			 * We are not copying the port_actions pointer,
3181 			 * because we already have it from px_args.
3182 			 * This is a bit fragile: <rdar://problem/16427422>
3183 			 */
3184 
3185 			if ((error = copyin(px_args.attrp, &px_sa, px_sa_offset)) != 0) {
3186 				goto bad;
3187 			}
3188 
3189 			bzero((void *)((unsigned long) &px_sa + px_sa_offset), sizeof(px_sa) - px_sa_offset );
3190 
3191 			imgp->ip_px_sa = &px_sa;
3192 		}
3193 		if (px_args.file_actions_size != 0) {
3194 			/* Limit file_actions to allowed number of open files */
3195 			size_t maxfa_size = PSF_ACTIONS_SIZE(proc_limitgetcur_nofile(p));
3196 
3197 			if (px_args.file_actions_size < PSF_ACTIONS_SIZE(1) ||
3198 			    maxfa_size == 0 || px_args.file_actions_size > maxfa_size) {
3199 				error = EINVAL;
3200 				goto bad;
3201 			}
3202 
3203 			px_sfap = kalloc_data(px_args.file_actions_size, Z_WAITOK);
3204 			if (px_sfap == NULL) {
3205 				error = ENOMEM;
3206 				goto bad;
3207 			}
3208 			imgp->ip_px_sfa = px_sfap;
3209 
3210 			if ((error = copyin(px_args.file_actions, px_sfap,
3211 			    px_args.file_actions_size)) != 0) {
3212 				goto bad;
3213 			}
3214 
3215 			/* Verify that the action count matches the struct size */
3216 			size_t psfsize = PSF_ACTIONS_SIZE(px_sfap->psfa_act_count);
3217 			if (psfsize == 0 || psfsize != px_args.file_actions_size) {
3218 				error = EINVAL;
3219 				goto bad;
3220 			}
3221 		}
3222 		if (px_args.port_actions_size != 0) {
3223 			/* Limit port_actions to one page of data */
3224 			if (px_args.port_actions_size < PS_PORT_ACTIONS_SIZE(1) ||
3225 			    px_args.port_actions_size > PAGE_SIZE) {
3226 				error = EINVAL;
3227 				goto bad;
3228 			}
3229 
3230 			px_spap = kalloc_data(px_args.port_actions_size, Z_WAITOK);
3231 			if (px_spap == NULL) {
3232 				error = ENOMEM;
3233 				goto bad;
3234 			}
3235 			imgp->ip_px_spa = px_spap;
3236 
3237 			if ((error = copyin(px_args.port_actions, px_spap,
3238 			    px_args.port_actions_size)) != 0) {
3239 				goto bad;
3240 			}
3241 
3242 			/* Verify that the action count matches the struct size */
3243 			size_t pasize = PS_PORT_ACTIONS_SIZE(px_spap->pspa_count);
3244 			if (pasize == 0 || pasize != px_args.port_actions_size) {
3245 				error = EINVAL;
3246 				goto bad;
3247 			}
3248 		}
3249 #if CONFIG_PERSONAS
3250 		/* copy in the persona info */
3251 		if (px_args.persona_info_size != 0 && px_args.persona_info != 0) {
3252 			/* for now, we need the exact same struct in user space */
3253 			if (px_args.persona_info_size != sizeof(*px_persona)) {
3254 				error = ERANGE;
3255 				goto bad;
3256 			}
3257 
3258 			px_persona = kalloc_data(px_args.persona_info_size, Z_WAITOK);
3259 			if (px_persona == NULL) {
3260 				error = ENOMEM;
3261 				goto bad;
3262 			}
3263 			imgp->ip_px_persona = px_persona;
3264 
3265 			if ((error = copyin(px_args.persona_info, px_persona,
3266 			    px_args.persona_info_size)) != 0) {
3267 				goto bad;
3268 			}
3269 			if ((error = spawn_validate_persona(px_persona)) != 0) {
3270 				goto bad;
3271 			}
3272 		}
3273 #endif
3274 		/* copy in the posix cred info */
3275 		if (px_args.posix_cred_info_size != 0 && px_args.posix_cred_info != 0) {
3276 			/* for now, we need the exact same struct in user space */
3277 			if (px_args.posix_cred_info_size != sizeof(*px_pcred_info)) {
3278 				error = ERANGE;
3279 				goto bad;
3280 			}
3281 
3282 			if (!kauth_cred_issuser(kauth_cred_get())) {
3283 				error = EPERM;
3284 				goto bad;
3285 			}
3286 
3287 			px_pcred_info = kalloc_data(px_args.posix_cred_info_size, Z_WAITOK);
3288 			if (px_pcred_info == NULL) {
3289 				error = ENOMEM;
3290 				goto bad;
3291 			}
3292 			imgp->ip_px_pcred_info = px_pcred_info;
3293 
3294 			if ((error = copyin(px_args.posix_cred_info, px_pcred_info,
3295 			    px_args.posix_cred_info_size)) != 0) {
3296 				goto bad;
3297 			}
3298 
3299 			if (px_pcred_info->pspci_flags & POSIX_SPAWN_POSIX_CRED_GROUPS) {
3300 				if (px_pcred_info->pspci_ngroups > NGROUPS_MAX) {
3301 					error = EINVAL;
3302 					goto bad;
3303 				}
3304 			}
3305 		}
3306 #if CONFIG_MACF
3307 		if (px_args.mac_extensions_size != 0) {
3308 			if ((error = spawn_copyin_macpolicyinfo(&px_args, (struct ip_px_smpx_s *)&imgp->ip_px_smpx)) != 0) {
3309 				goto bad;
3310 			}
3311 		}
3312 #endif /* CONFIG_MACF */
3313 		if ((px_args.subsystem_root_path_size > 0) && (px_args.subsystem_root_path_size <= MAXPATHLEN)) {
3314 			/*
3315 			 * If a valid-looking subsystem root has been
3316 			 * specified...
3317 			 */
3318 			if (IOTaskHasEntitlement(old_task, SPAWN_SUBSYSTEM_ROOT_ENTITLEMENT)) {
3319 				/*
3320 				 * ...AND the parent has the entitlement, copy
3321 				 * the subsystem root path in.
3322 				 */
3323 				subsystem_root_path = zalloc_flags(ZV_NAMEI,
3324 				    Z_WAITOK | Z_ZERO | Z_NOFAIL);
3325 
3326 				if ((error = copyin(px_args.subsystem_root_path, subsystem_root_path, px_args.subsystem_root_path_size))) {
3327 					goto bad;
3328 				}
3329 
3330 				/* Paranoia */
3331 				subsystem_root_path[px_args.subsystem_root_path_size - 1] = 0;
3332 			}
3333 		}
3334 	}
3335 
3336 	/* set uthread to parent */
3337 	uthread = current_uthread();
3338 
3339 	/*
3340 	 * <rdar://6640530>; this does not result in a behaviour change
3341 	 * relative to Leopard, so there should not be any existing code
3342 	 * which depends on it.
3343 	 */
3344 
3345 	if (imgp->ip_px_sa != NULL) {
3346 		struct _posix_spawnattr *psa = (struct _posix_spawnattr *) imgp->ip_px_sa;
3347 		if ((psa->psa_options & PSA_OPTION_PLUGIN_HOST_DISABLE_A_KEYS) == PSA_OPTION_PLUGIN_HOST_DISABLE_A_KEYS) {
3348 			imgp->ip_flags |= IMGPF_PLUGIN_HOST_DISABLE_A_KEYS;
3349 		}
3350 #if (DEVELOPMENT || DEBUG)
3351 		if ((psa->psa_options & PSA_OPTION_ALT_ROSETTA) == PSA_OPTION_ALT_ROSETTA) {
3352 			imgp->ip_flags |= IMGPF_ALT_ROSETTA;
3353 		}
3354 #endif
3355 
3356 		if ((error = exec_validate_spawnattr_policy(psa->psa_apptype)) != 0) {
3357 			goto bad;
3358 		}
3359 	}
3360 
3361 	/*
3362 	 * If we don't have the extension flag that turns "posix_spawn()"
3363 	 * into "execve() with options", then we will be creating a new
3364 	 * process which does not inherit memory from the parent process,
3365 	 * which is one of the most expensive things about using fork()
3366 	 * and execve().
3367 	 */
3368 	if (imgp->ip_px_sa == NULL || !(px_sa.psa_flags & POSIX_SPAWN_SETEXEC)) {
3369 		/* Set the new task's coalition, if it is requested.  */
3370 		coalition_t coal[COALITION_NUM_TYPES] = { COALITION_NULL };
3371 #if CONFIG_COALITIONS
3372 		int i, ncoals;
3373 		kern_return_t kr = KERN_SUCCESS;
3374 		struct _posix_spawn_coalition_info coal_info;
3375 		int coal_role[COALITION_NUM_TYPES];
3376 
3377 		if (imgp->ip_px_sa == NULL || !px_args.coal_info) {
3378 			goto do_fork1;
3379 		}
3380 
3381 		memset(&coal_info, 0, sizeof(coal_info));
3382 
3383 		if (px_args.coal_info_size > sizeof(coal_info)) {
3384 			px_args.coal_info_size = sizeof(coal_info);
3385 		}
3386 		error = copyin(px_args.coal_info,
3387 		    &coal_info, px_args.coal_info_size);
3388 		if (error != 0) {
3389 			goto bad;
3390 		}
3391 
3392 		ncoals = 0;
3393 		for (i = 0; i < COALITION_NUM_TYPES; i++) {
3394 			uint64_t cid = coal_info.psci_info[i].psci_id;
3395 			if (cid != 0) {
3396 				/*
3397 				 * don't allow tasks which are not in a
3398 				 * privileged coalition to spawn processes
3399 				 * into coalitions other than their own
3400 				 */
3401 				if (!task_is_in_privileged_coalition(p->task, i) &&
3402 				    !IOTaskHasEntitlement(p->task, COALITION_SPAWN_ENTITLEMENT)) {
3403 					coal_dbg("ERROR: %d not in privilegd "
3404 					    "coalition of type %d",
3405 					    proc_getpid(p), i);
3406 					spawn_coalitions_release_all(coal);
3407 					error = EPERM;
3408 					goto bad;
3409 				}
3410 
3411 				coal_dbg("searching for coalition id:%llu", cid);
3412 				/*
3413 				 * take a reference and activation on the
3414 				 * coalition to guard against free-while-spawn
3415 				 * races
3416 				 */
3417 				coal[i] = coalition_find_and_activate_by_id(cid);
3418 				if (coal[i] == COALITION_NULL) {
3419 					coal_dbg("could not find coalition id:%llu "
3420 					    "(perhaps it has been terminated or reaped)", cid);
3421 					/*
3422 					 * release any other coalition's we
3423 					 * may have a reference to
3424 					 */
3425 					spawn_coalitions_release_all(coal);
3426 					error = ESRCH;
3427 					goto bad;
3428 				}
3429 				if (coalition_type(coal[i]) != i) {
3430 					coal_dbg("coalition with id:%lld is not of type:%d"
3431 					    " (it's type:%d)", cid, i, coalition_type(coal[i]));
3432 					error = ESRCH;
3433 					goto bad;
3434 				}
3435 				coal_role[i] = coal_info.psci_info[i].psci_role;
3436 				ncoals++;
3437 			}
3438 		}
3439 		if (ncoals < COALITION_NUM_TYPES) {
3440 			/*
3441 			 * If the user is attempting to spawn into a subset of
3442 			 * the known coalition types, then make sure they have
3443 			 * _at_least_ specified a resource coalition. If not,
3444 			 * the following fork1() call will implicitly force an
3445 			 * inheritance from 'p' and won't actually spawn the
3446 			 * new task into the coalitions the user specified.
3447 			 * (also the call to coalitions_set_roles will panic)
3448 			 */
3449 			if (coal[COALITION_TYPE_RESOURCE] == COALITION_NULL) {
3450 				spawn_coalitions_release_all(coal);
3451 				error = EINVAL;
3452 				goto bad;
3453 			}
3454 		}
3455 do_fork1:
3456 #endif /* CONFIG_COALITIONS */
3457 
3458 		/*
3459 		 * note that this will implicitly inherit the
3460 		 * caller's persona (if it exists)
3461 		 */
3462 		error = fork1(p, &imgp->ip_new_thread, PROC_CREATE_SPAWN, coal);
3463 		/* returns a thread and task reference */
3464 
3465 		if (error == 0) {
3466 			new_task = get_threadtask(imgp->ip_new_thread);
3467 		}
3468 #if CONFIG_COALITIONS
3469 		/* set the roles of this task within each given coalition */
3470 		if (error == 0) {
3471 			kr = coalitions_set_roles(coal, new_task, coal_role);
3472 			if (kr != KERN_SUCCESS) {
3473 				error = EINVAL;
3474 			}
3475 			if (kdebug_debugid_enabled(MACHDBG_CODE(DBG_MACH_COALITION,
3476 			    MACH_COALITION_ADOPT))) {
3477 				for (i = 0; i < COALITION_NUM_TYPES; i++) {
3478 					if (coal[i] != COALITION_NULL) {
3479 						/*
3480 						 * On 32-bit targets, uniqueid
3481 						 * will get truncated to 32 bits
3482 						 */
3483 						KDBG_RELEASE(MACHDBG_CODE(
3484 							    DBG_MACH_COALITION,
3485 							    MACH_COALITION_ADOPT),
3486 						    coalition_id(coal[i]),
3487 						    get_task_uniqueid(new_task));
3488 					}
3489 				}
3490 			}
3491 		}
3492 
3493 		/* drop our references and activations - fork1() now holds them */
3494 		spawn_coalitions_release_all(coal);
3495 #endif /* CONFIG_COALITIONS */
3496 		if (error != 0) {
3497 			goto bad;
3498 		}
3499 		imgp->ip_flags |= IMGPF_SPAWN;  /* spawn w/o exec */
3500 		spawn_no_exec = TRUE;           /* used in later tests */
3501 	} else {
3502 		/*
3503 		 * For execve case, create a new task and thread
3504 		 * which points to current_proc. The current_proc will point
3505 		 * to the new task after image activation and proc ref drain.
3506 		 *
3507 		 * proc (current_proc) <-----  old_task (current_task)
3508 		 *  ^ |                                ^
3509 		 *  | |                                |
3510 		 *  | ----------------------------------
3511 		 *  |
3512 		 *  --------- new_task (task marked as TF_EXEC_COPY)
3513 		 *
3514 		 * After image activation, the proc will point to the new task
3515 		 * and would look like following.
3516 		 *
3517 		 * proc (current_proc)  <-----  old_task (current_task, marked as TPF_DID_EXEC)
3518 		 *  ^ |
3519 		 *  | |
3520 		 *  | ----------> new_task
3521 		 *  |               |
3522 		 *  -----------------
3523 		 *
3524 		 * During exec any transition from new_task -> proc is fine, but don't allow
3525 		 * transition from proc->task, since it will modify old_task.
3526 		 */
3527 		imgp->ip_new_thread = fork_create_child(old_task,
3528 		    NULL,
3529 		    p,
3530 		    FALSE,
3531 		    p->p_flag & P_LP64,
3532 		    task_get_64bit_data(old_task),
3533 		    TRUE);
3534 		/* task and thread ref returned by fork_create_child */
3535 		if (imgp->ip_new_thread == NULL) {
3536 			error = ENOMEM;
3537 			goto bad;
3538 		}
3539 
3540 		new_task = get_threadtask(imgp->ip_new_thread);
3541 		imgp->ip_flags |= IMGPF_EXEC;
3542 	}
3543 
3544 	if (spawn_no_exec) {
3545 		p = (proc_t)get_bsdthreadtask_info(imgp->ip_new_thread);
3546 
3547 		/*
3548 		 * We had to wait until this point before firing the
3549 		 * proc:::create probe, otherwise p would not point to the
3550 		 * child process.
3551 		 */
3552 		DTRACE_PROC1(create, proc_t, p);
3553 	}
3554 	assert(p != NULL);
3555 
3556 	if (subsystem_root_path) {
3557 		/* If a subsystem root was specified, swap it in */
3558 		char * old_subsystem_root_path = p->p_subsystem_root_path;
3559 		p->p_subsystem_root_path = subsystem_root_path;
3560 		subsystem_root_path = old_subsystem_root_path;
3561 	}
3562 
3563 	/* We'll need the subsystem root for setting up Apple strings */
3564 	imgp->ip_subsystem_root_path = p->p_subsystem_root_path;
3565 
3566 	context.vc_thread = imgp->ip_new_thread;
3567 	context.vc_ucred = proc_ucred(p);  /* XXX must NOT be kauth_cred_get() */
3568 
3569 	/*
3570 	 * Post fdt_fork(), pre exec_handle_sugid() - this is where we want
3571 	 * to handle the file_actions.
3572 	 */
3573 
3574 	/* Has spawn file actions? */
3575 	if (imgp->ip_px_sfa != NULL) {
3576 		/*
3577 		 * The POSIX_SPAWN_CLOEXEC_DEFAULT flag
3578 		 * is handled in exec_handle_file_actions().
3579 		 */
3580 #if CONFIG_AUDIT
3581 		/*
3582 		 * The file actions auditing can overwrite the upath of
3583 		 * AUE_POSIX_SPAWN audit record.  Save the audit record.
3584 		 */
3585 		struct kaudit_record *save_uu_ar = uthread->uu_ar;
3586 		uthread->uu_ar = NULL;
3587 #endif
3588 		error = exec_handle_file_actions(imgp,
3589 		    imgp->ip_px_sa != NULL ? px_sa.psa_flags : 0);
3590 #if CONFIG_AUDIT
3591 		/* Restore the AUE_POSIX_SPAWN audit record. */
3592 		uthread->uu_ar = save_uu_ar;
3593 #endif
3594 		if (error != 0) {
3595 			goto bad;
3596 		}
3597 	}
3598 
3599 	/* Has spawn port actions? */
3600 	if (imgp->ip_px_spa != NULL) {
3601 #if CONFIG_AUDIT
3602 		/*
3603 		 * Do the same for the port actions as we did for the file
3604 		 * actions.  Save the AUE_POSIX_SPAWN audit record.
3605 		 */
3606 		struct kaudit_record *save_uu_ar = uthread->uu_ar;
3607 		uthread->uu_ar = NULL;
3608 #endif
3609 		error = exec_handle_port_actions(imgp, &port_actions);
3610 #if CONFIG_AUDIT
3611 		/* Restore the AUE_POSIX_SPAWN audit record. */
3612 		uthread->uu_ar = save_uu_ar;
3613 #endif
3614 		if (error != 0) {
3615 			goto bad;
3616 		}
3617 	}
3618 
3619 	/* Has spawn attr? */
3620 	if (imgp->ip_px_sa != NULL) {
3621 		/*
3622 		 * Reset UID/GID to parent's RUID/RGID; This works only
3623 		 * because the operation occurs before the call
3624 		 * to exec_handle_sugid() by the image activator called
3625 		 * from exec_activate_image().
3626 		 *
3627 		 * POSIX requires that any setuid/setgid bits on the process
3628 		 * image will take precedence over the spawn attributes
3629 		 * (re)setting them.
3630 		 *
3631 		 * Modifications to p_ucred must be guarded using the
3632 		 * proc's ucred lock. This prevents others from accessing
3633 		 * a garbage credential.
3634 		 */
3635 		if (px_sa.psa_flags & POSIX_SPAWN_RESETIDS) {
3636 			proc_update_label(p, false, ^kauth_cred_t (kauth_cred_t my_cred){
3637 				return kauth_cred_setuidgid(my_cred,
3638 				kauth_cred_getruid(my_cred),
3639 				kauth_cred_getrgid(my_cred));
3640 			});
3641 		}
3642 
3643 		if (imgp->ip_px_pcred_info) {
3644 			if (!spawn_no_exec) {
3645 				error = ENOTSUP;
3646 				goto bad;
3647 			}
3648 
3649 			error = spawn_posix_cred_adopt(p, imgp->ip_px_pcred_info);
3650 			if (error != 0) {
3651 				goto bad;
3652 			}
3653 		}
3654 
3655 #if CONFIG_PERSONAS
3656 		if (imgp->ip_px_persona != NULL) {
3657 			if (!spawn_no_exec) {
3658 				error = ENOTSUP;
3659 				goto bad;
3660 			}
3661 
3662 			/*
3663 			 * If we were asked to spawn a process into a new persona,
3664 			 * do the credential switch now (which may override the UID/GID
3665 			 * inherit done just above). It's important to do this switch
3666 			 * before image activation both for reasons stated above, and
3667 			 * to ensure that the new persona has access to the image/file
3668 			 * being executed.
3669 			 */
3670 			error = spawn_persona_adopt(p, imgp->ip_px_persona);
3671 			if (error != 0) {
3672 				goto bad;
3673 			}
3674 		}
3675 #endif /* CONFIG_PERSONAS */
3676 #if !SECURE_KERNEL
3677 		/*
3678 		 * Disable ASLR for the spawned process.
3679 		 *
3680 		 * But only do so if we are not embedded + RELEASE.
3681 		 * While embedded allows for a boot-arg (-disable_aslr)
3682 		 * to deal with this (which itself is only honored on
3683 		 * DEVELOPMENT or DEBUG builds of xnu), it is often
3684 		 * useful or necessary to disable ASLR on a per-process
3685 		 * basis for unit testing and debugging.
3686 		 */
3687 		if (px_sa.psa_flags & _POSIX_SPAWN_DISABLE_ASLR) {
3688 			OSBitOrAtomic(P_DISABLE_ASLR, &p->p_flag);
3689 		}
3690 #endif /* !SECURE_KERNEL */
3691 
3692 		/* Randomize high bits of ASLR slide */
3693 		if (px_sa.psa_flags & _POSIX_SPAWN_HIGH_BITS_ASLR) {
3694 			imgp->ip_flags |= IMGPF_HIGH_BITS_ASLR;
3695 		}
3696 
3697 #if !SECURE_KERNEL
3698 		/*
3699 		 * Forcibly disallow execution from data pages for the spawned process
3700 		 * even if it would otherwise be permitted by the architecture default.
3701 		 */
3702 		if (px_sa.psa_flags & _POSIX_SPAWN_ALLOW_DATA_EXEC) {
3703 			imgp->ip_flags |= IMGPF_ALLOW_DATA_EXEC;
3704 		}
3705 #endif /* !SECURE_KERNEL */
3706 
3707 #if     __has_feature(ptrauth_calls)
3708 		if (vm_shared_region_reslide_aslr && is_64 && (px_sa.psa_flags & _POSIX_SPAWN_RESLIDE)) {
3709 			imgp->ip_flags |= IMGPF_RESLIDE;
3710 		}
3711 #endif /* __has_feature(ptrauth_calls) */
3712 
3713 		if ((px_sa.psa_apptype & POSIX_SPAWN_PROC_TYPE_MASK) ==
3714 		    POSIX_SPAWN_PROC_TYPE_DRIVER) {
3715 			imgp->ip_flags |= IMGPF_DRIVER;
3716 		}
3717 	}
3718 
3719 	/*
3720 	 * Disable ASLR during image activation.  This occurs either if the
3721 	 * _POSIX_SPAWN_DISABLE_ASLR attribute was found above or if
3722 	 * P_DISABLE_ASLR was inherited from the parent process.
3723 	 */
3724 	if (p->p_flag & P_DISABLE_ASLR) {
3725 		imgp->ip_flags |= IMGPF_DISABLE_ASLR;
3726 	}
3727 
3728 	/*
3729 	 * Clear transition flag so we won't hang if exec_activate_image() causes
3730 	 * an automount (and launchd does a proc sysctl to service it).
3731 	 *
3732 	 * <rdar://problem/6848672>, <rdar://problem/5959568>.
3733 	 */
3734 	if (spawn_no_exec) {
3735 		proc_transend(p, 0);
3736 		proc_transit_set = 0;
3737 	}
3738 
3739 #if MAC_SPAWN   /* XXX */
3740 	if (uap->mac_p != USER_ADDR_NULL) {
3741 		error = mac_execve_enter(uap->mac_p, imgp);
3742 		if (error) {
3743 			goto bad;
3744 		}
3745 	}
3746 #endif
3747 
3748 	/*
3749 	 * Activate the image
3750 	 */
3751 	error = exec_activate_image(imgp);
3752 #if defined(HAS_APPLE_PAC)
3753 	ml_task_set_jop_pid_from_shared_region(new_task);
3754 	ml_task_set_disable_user_jop(new_task, imgp->ip_flags & IMGPF_NOJOP ? TRUE : FALSE);
3755 	ml_thread_set_disable_user_jop(imgp->ip_new_thread, imgp->ip_flags & IMGPF_NOJOP ? TRUE : FALSE);
3756 	ml_thread_set_jop_pid(imgp->ip_new_thread, new_task);
3757 #endif
3758 
3759 	if (error == 0 && !spawn_no_exec) {
3760 		p = proc_exec_switch_task(p, old_task, new_task, imgp->ip_new_thread, &inherit);
3761 		/* proc ref returned */
3762 		should_release_proc_ref = TRUE;
3763 	}
3764 
3765 	if (error == 0) {
3766 		/* process completed the exec */
3767 		exec_done = TRUE;
3768 	}
3769 
3770 	if (!error && imgp->ip_px_sa != NULL) {
3771 		thread_t child_thread = imgp->ip_new_thread;
3772 		uthread_t child_uthread = get_bsdthread_info(child_thread);
3773 
3774 		/*
3775 		 * Because of POSIX_SPAWN_SETEXEC, we need to handle this after image
3776 		 * activation, else when image activation fails (before the point of no
3777 		 * return) would leave the parent process in a modified state.
3778 		 */
3779 		if (px_sa.psa_flags & POSIX_SPAWN_SETPGROUP) {
3780 			struct setpgid_args spga;
3781 			spga.pid = proc_getpid(p);
3782 			spga.pgid = px_sa.psa_pgroup;
3783 			/*
3784 			 * Effectively, call setpgid() system call; works
3785 			 * because there are no pointer arguments.
3786 			 */
3787 			if ((error = setpgid(p, &spga, ival)) != 0) {
3788 				goto bad;
3789 			}
3790 		}
3791 
3792 		if (px_sa.psa_flags & POSIX_SPAWN_SETSID) {
3793 			error = setsid_internal(p);
3794 			if (error != 0) {
3795 				goto bad;
3796 			}
3797 		}
3798 
3799 		/*
3800 		 * If we have a spawn attr, and it contains signal related flags,
3801 		 * the we need to process them in the "context" of the new child
3802 		 * process, so we have to process it following image activation,
3803 		 * prior to making the thread runnable in user space.  This is
3804 		 * necessitated by some signal information being per-thread rather
3805 		 * than per-process, and we don't have the new allocation in hand
3806 		 * until after the image is activated.
3807 		 */
3808 
3809 		/*
3810 		 * Mask a list of signals, instead of them being unmasked, if
3811 		 * they were unmasked in the parent; note that some signals
3812 		 * are not maskable.
3813 		 */
3814 		if (px_sa.psa_flags & POSIX_SPAWN_SETSIGMASK) {
3815 			child_uthread->uu_sigmask = (px_sa.psa_sigmask & ~sigcantmask);
3816 		}
3817 		/*
3818 		 * Default a list of signals instead of ignoring them, if
3819 		 * they were ignored in the parent.  Note that we pass
3820 		 * spawn_no_exec to setsigvec() to indicate that we called
3821 		 * fork1() and therefore do not need to call proc_signalstart()
3822 		 * internally.
3823 		 */
3824 		if (px_sa.psa_flags & POSIX_SPAWN_SETSIGDEF) {
3825 			vec.sa_handler = SIG_DFL;
3826 			vec.sa_tramp = 0;
3827 			vec.sa_mask = 0;
3828 			vec.sa_flags = 0;
3829 			for (sig = 1; sig < NSIG; sig++) {
3830 				if (px_sa.psa_sigdefault & (1 << (sig - 1))) {
3831 					error = setsigvec(p, child_thread, sig, &vec, spawn_no_exec);
3832 				}
3833 			}
3834 		}
3835 
3836 		/*
3837 		 * Activate the CPU usage monitor, if requested. This is done via a task-wide, per-thread CPU
3838 		 * usage limit, which will generate a resource exceeded exception if any one thread exceeds the
3839 		 * limit.
3840 		 *
3841 		 * Userland gives us interval in seconds, and the kernel SPI expects nanoseconds.
3842 		 */
3843 		if ((px_sa.psa_cpumonitor_percent != 0) && (px_sa.psa_cpumonitor_percent < UINT8_MAX)) {
3844 			/*
3845 			 * Always treat a CPU monitor activation coming from spawn as entitled. Requiring
3846 			 * an entitlement to configure the monitor a certain way seems silly, since
3847 			 * whomever is turning it on could just as easily choose not to do so.
3848 			 */
3849 			error = proc_set_task_ruse_cpu(p->task,
3850 			    TASK_POLICY_RESOURCE_ATTRIBUTE_NOTIFY_EXC,
3851 			    (uint8_t)px_sa.psa_cpumonitor_percent,
3852 			    px_sa.psa_cpumonitor_interval * NSEC_PER_SEC,
3853 			    0, TRUE);
3854 		}
3855 
3856 
3857 		if (px_pcred_info &&
3858 		    (px_pcred_info->pspci_flags & POSIX_SPAWN_POSIX_CRED_LOGIN)) {
3859 			/*
3860 			 * setlogin() must happen after setsid()
3861 			 */
3862 			setlogin_internal(p, px_pcred_info->pspci_login);
3863 		}
3864 	}
3865 
3866 bad:
3867 
3868 	if (error == 0) {
3869 		/* reset delay idle sleep status if set */
3870 #if CONFIG_DELAY_IDLE_SLEEP
3871 		if ((p->p_flag & P_DELAYIDLESLEEP) == P_DELAYIDLESLEEP) {
3872 			OSBitAndAtomic(~((uint32_t)P_DELAYIDLESLEEP), &p->p_flag);
3873 		}
3874 #endif /* CONFIG_DELAY_IDLE_SLEEP */
3875 		/* upon  successful spawn, re/set the proc control state */
3876 		if (imgp->ip_px_sa != NULL) {
3877 			switch (px_sa.psa_pcontrol) {
3878 			case POSIX_SPAWN_PCONTROL_THROTTLE:
3879 				p->p_pcaction = P_PCTHROTTLE;
3880 				break;
3881 			case POSIX_SPAWN_PCONTROL_SUSPEND:
3882 				p->p_pcaction = P_PCSUSP;
3883 				break;
3884 			case POSIX_SPAWN_PCONTROL_KILL:
3885 				p->p_pcaction = P_PCKILL;
3886 				break;
3887 			case POSIX_SPAWN_PCONTROL_NONE:
3888 			default:
3889 				p->p_pcaction = 0;
3890 				break;
3891 			}
3892 			;
3893 		}
3894 		exec_resettextvp(p, imgp);
3895 
3896 #if CONFIG_MEMORYSTATUS
3897 		/* Set jetsam priority for DriverKit processes */
3898 		if (px_sa.psa_apptype == POSIX_SPAWN_PROC_TYPE_DRIVER) {
3899 			px_sa.psa_priority = JETSAM_PRIORITY_DRIVER_APPLE;
3900 		}
3901 
3902 		/* Has jetsam attributes? */
3903 		if (imgp->ip_px_sa != NULL && (px_sa.psa_jetsam_flags & POSIX_SPAWN_JETSAM_SET)) {
3904 			/*
3905 			 * With 2-level high-water-mark support, POSIX_SPAWN_JETSAM_HIWATER_BACKGROUND is no
3906 			 * longer relevant, as background limits are described via the inactive limit slots.
3907 			 *
3908 			 * That said, however, if the POSIX_SPAWN_JETSAM_HIWATER_BACKGROUND is passed in,
3909 			 * we attempt to mimic previous behavior by forcing the BG limit data into the
3910 			 * inactive/non-fatal mode and force the active slots to hold system_wide/fatal mode.
3911 			 */
3912 
3913 			if (px_sa.psa_jetsam_flags & POSIX_SPAWN_JETSAM_HIWATER_BACKGROUND) {
3914 				memorystatus_update(p, px_sa.psa_priority, 0, FALSE, /* assertion priority */
3915 				    (px_sa.psa_jetsam_flags & POSIX_SPAWN_JETSAM_USE_EFFECTIVE_PRIORITY),
3916 				    TRUE,
3917 				    -1, TRUE,
3918 				    px_sa.psa_memlimit_inactive, FALSE);
3919 			} else {
3920 				memorystatus_update(p, px_sa.psa_priority, 0, FALSE, /* assertion priority */
3921 				    (px_sa.psa_jetsam_flags & POSIX_SPAWN_JETSAM_USE_EFFECTIVE_PRIORITY),
3922 				    TRUE,
3923 				    px_sa.psa_memlimit_active,
3924 				    (px_sa.psa_jetsam_flags & POSIX_SPAWN_JETSAM_MEMLIMIT_ACTIVE_FATAL),
3925 				    px_sa.psa_memlimit_inactive,
3926 				    (px_sa.psa_jetsam_flags & POSIX_SPAWN_JETSAM_MEMLIMIT_INACTIVE_FATAL));
3927 			}
3928 		}
3929 
3930 		/* Has jetsam relaunch behavior? */
3931 		if (imgp->ip_px_sa != NULL && (px_sa.psa_jetsam_flags & POSIX_SPAWN_JETSAM_RELAUNCH_BEHAVIOR_MASK)) {
3932 			/*
3933 			 * Launchd has passed in data indicating the behavior of this process in response to jetsam.
3934 			 * This data would be used by the jetsam subsystem to determine the position and protection
3935 			 * offered to this process on dirty -> clean transitions.
3936 			 */
3937 			int relaunch_flags = P_MEMSTAT_RELAUNCH_UNKNOWN;
3938 			switch (px_sa.psa_jetsam_flags & POSIX_SPAWN_JETSAM_RELAUNCH_BEHAVIOR_MASK) {
3939 			case POSIX_SPAWN_JETSAM_RELAUNCH_BEHAVIOR_LOW:
3940 				relaunch_flags = P_MEMSTAT_RELAUNCH_LOW;
3941 				break;
3942 			case POSIX_SPAWN_JETSAM_RELAUNCH_BEHAVIOR_MED:
3943 				relaunch_flags = P_MEMSTAT_RELAUNCH_MED;
3944 				break;
3945 			case POSIX_SPAWN_JETSAM_RELAUNCH_BEHAVIOR_HIGH:
3946 				relaunch_flags = P_MEMSTAT_RELAUNCH_HIGH;
3947 				break;
3948 			default:
3949 				break;
3950 			}
3951 			memorystatus_relaunch_flags_update(p, relaunch_flags);
3952 		}
3953 
3954 #endif /* CONFIG_MEMORYSTATUS */
3955 		if (imgp->ip_px_sa != NULL && px_sa.psa_thread_limit > 0) {
3956 			task_set_thread_limit(new_task, (uint16_t)px_sa.psa_thread_limit);
3957 		}
3958 
3959 #if CONFIG_PROC_RESOURCE_LIMITS
3960 		if (imgp->ip_px_sa != NULL && (px_sa.psa_port_soft_limit > 0 || px_sa.psa_port_hard_limit > 0)) {
3961 			task_set_port_space_limits(new_task, (uint32_t)px_sa.psa_port_soft_limit,
3962 			    (uint32_t)px_sa.psa_port_hard_limit);
3963 		}
3964 
3965 		if (imgp->ip_px_sa != NULL && (px_sa.psa_filedesc_soft_limit > 0 || px_sa.psa_filedesc_hard_limit > 0)) {
3966 			proc_set_filedesc_limits(p, (int)px_sa.psa_filedesc_soft_limit,
3967 			    (int)px_sa.psa_filedesc_hard_limit);
3968 		}
3969 #endif /* CONFIG_PROC_RESOURCE_LIMITS */
3970 
3971 		/* Disable wakeup monitoring for DriverKit processes */
3972 		if (px_sa.psa_apptype == POSIX_SPAWN_PROC_TYPE_DRIVER) {
3973 			uint32_t      flags = WAKEMON_DISABLE;
3974 			task_wakeups_monitor_ctl(new_task, &flags, NULL);
3975 		}
3976 	}
3977 
3978 	/*
3979 	 * If we successfully called fork1(), we always need to do this;
3980 	 * we identify this case by noting the IMGPF_SPAWN flag.  This is
3981 	 * because we come back from that call with signals blocked in the
3982 	 * child, and we have to unblock them, but we want to wait until
3983 	 * after we've performed any spawn actions.  This has to happen
3984 	 * before check_for_signature(), which uses psignal.
3985 	 */
3986 	if (spawn_no_exec) {
3987 		if (proc_transit_set) {
3988 			proc_transend(p, 0);
3989 		}
3990 
3991 		/*
3992 		 * Drop the signal lock on the child which was taken on our
3993 		 * behalf by forkproc()/cloneproc() to prevent signals being
3994 		 * received by the child in a partially constructed state.
3995 		 */
3996 		proc_signalend(p, 0);
3997 	}
3998 
3999 	if (error == 0) {
4000 		/*
4001 		 * We need to initialize the bank context behind the protection of
4002 		 * the proc_trans lock to prevent a race with exit. We can't do this during
4003 		 * exec_activate_image because task_bank_init checks entitlements that
4004 		 * aren't loaded until subsequent calls (including exec_resettextvp).
4005 		 */
4006 		error = proc_transstart(p, 0, 0);
4007 
4008 		if (error == 0) {
4009 			task_bank_init(new_task);
4010 			proc_transend(p, 0);
4011 		}
4012 
4013 #if __arm64__
4014 		proc_footprint_entitlement_hacks(p, new_task);
4015 #endif /* __arm64__ */
4016 
4017 #if XNU_TARGET_OS_OSX
4018 #define SINGLE_JIT_ENTITLEMENT "com.apple.security.cs.single-jit"
4019 		if (IOTaskHasEntitlement(new_task, SINGLE_JIT_ENTITLEMENT)) {
4020 			vm_map_single_jit(get_task_map(new_task));
4021 		}
4022 #endif /* XNU_TARGET_OS_OSX */
4023 
4024 #if __has_feature(ptrauth_calls)
4025 		task_set_pac_exception_fatal_flag(new_task);
4026 #endif /* __has_feature(ptrauth_calls) */
4027 	}
4028 
4029 	/* Inherit task role from old task to new task for exec */
4030 	if (error == 0 && !spawn_no_exec) {
4031 		proc_inherit_task_role(new_task, old_task);
4032 	}
4033 
4034 #if CONFIG_ARCADE
4035 	if (error == 0) {
4036 		/*
4037 		 * Check to see if we need to trigger an arcade upcall AST now
4038 		 * that the vnode has been reset on the task.
4039 		 */
4040 		arcade_prepare(new_task, imgp->ip_new_thread);
4041 	}
4042 #endif /* CONFIG_ARCADE */
4043 
4044 	/* Clear the initial wait on the thread before handling spawn policy */
4045 	if (imgp && imgp->ip_new_thread) {
4046 		task_clear_return_wait(get_threadtask(imgp->ip_new_thread), TCRW_CLEAR_INITIAL_WAIT);
4047 	}
4048 
4049 	/*
4050 	 * Apply the spawnattr policy, apptype (which primes the task for importance donation),
4051 	 * and bind any portwatch ports to the new task.
4052 	 * This must be done after the exec so that the child's thread is ready,
4053 	 * and after the in transit state has been released, because priority is
4054 	 * dropped here so we need to be prepared for a potentially long preemption interval
4055 	 *
4056 	 * TODO: Consider splitting this up into separate phases
4057 	 */
4058 	if (error == 0 && imgp->ip_px_sa != NULL) {
4059 		struct _posix_spawnattr *psa = (struct _posix_spawnattr *) imgp->ip_px_sa;
4060 
4061 		error = exec_handle_spawnattr_policy(p, imgp->ip_new_thread, psa->psa_apptype, psa->psa_qos_clamp,
4062 		    psa->psa_darwin_role, &port_actions);
4063 	}
4064 
4065 	/* Transfer the turnstile watchport boost to new task if in exec */
4066 	if (error == 0 && !spawn_no_exec) {
4067 		task_transfer_turnstile_watchports(old_task, new_task, imgp->ip_new_thread);
4068 	}
4069 
4070 	/*
4071 	 * Apply the requested maximum address.
4072 	 */
4073 	if (error == 0 && imgp->ip_px_sa != NULL) {
4074 		struct _posix_spawnattr *psa = (struct _posix_spawnattr *) imgp->ip_px_sa;
4075 
4076 		if (psa->psa_max_addr) {
4077 			vm_map_set_max_addr(get_task_map(new_task), (vm_map_offset_t)psa->psa_max_addr);
4078 		}
4079 	}
4080 
4081 	if (error == 0 && imgp->ip_px_sa != NULL) {
4082 		struct _posix_spawnattr *psa = (struct _posix_spawnattr *) imgp->ip_px_sa;
4083 
4084 		if (psa->psa_no_smt) {
4085 			task_set_no_smt(new_task);
4086 		}
4087 		if (psa->psa_tecs) {
4088 			task_set_tecs(new_task);
4089 		}
4090 	}
4091 
4092 	if (error == 0) {
4093 		/* Apply the main thread qos */
4094 		thread_t main_thread = imgp->ip_new_thread;
4095 		task_set_main_thread_qos(new_task, main_thread);
4096 
4097 #if CONFIG_MACF
4098 		proc_apply_jit_and_jumbo_va_policies(p, new_task);
4099 #endif /* CONFIG_MACF */
4100 	}
4101 
4102 	/*
4103 	 * Release any ports we kept around for binding to the new task
4104 	 * We need to release the rights even if the posix_spawn has failed.
4105 	 */
4106 	if (imgp->ip_px_spa != NULL) {
4107 		exec_port_actions_destroy(&port_actions);
4108 	}
4109 
4110 	/*
4111 	 * We have to delay operations which might throw a signal until after
4112 	 * the signals have been unblocked; however, we want that to happen
4113 	 * after exec_resettextvp() so that the textvp is correct when they
4114 	 * fire.
4115 	 */
4116 	if (error == 0) {
4117 		error = check_for_signature(p, imgp);
4118 
4119 		/*
4120 		 * Pay for our earlier safety; deliver the delayed signals from
4121 		 * the incomplete spawn process now that it's complete.
4122 		 */
4123 		if (imgp != NULL && spawn_no_exec && (p->p_lflag & P_LTRACED)) {
4124 			psignal_vfork(p, p->task, imgp->ip_new_thread, SIGTRAP);
4125 		}
4126 
4127 		if (error == 0 && !spawn_no_exec) {
4128 			KDBG(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXEC),
4129 			    proc_getpid(p));
4130 		}
4131 	}
4132 
4133 	if (spawn_no_exec) {
4134 		/* flag the 'fork' has occurred */
4135 		proc_knote(p->p_pptr, NOTE_FORK | proc_getpid(p));
4136 	}
4137 
4138 	/* flag exec has occurred, notify only if it has not failed due to FP Key error */
4139 	if (!error && ((p->p_lflag & P_LTERM_DECRYPTFAIL) == 0)) {
4140 		proc_knote(p, NOTE_EXEC);
4141 	}
4142 
4143 	if (imgp != NULL) {
4144 		if (imgp->ip_vp) {
4145 			vnode_put(imgp->ip_vp);
4146 		}
4147 		if (imgp->ip_scriptvp) {
4148 			vnode_put(imgp->ip_scriptvp);
4149 		}
4150 		if (imgp->ip_strings) {
4151 			execargs_free(imgp);
4152 		}
4153 		kfree_data(imgp->ip_px_sfa,
4154 		    px_args.file_actions_size);
4155 		kfree_data(imgp->ip_px_spa,
4156 		    px_args.port_actions_size);
4157 #if CONFIG_PERSONAS
4158 		kfree_data(imgp->ip_px_persona,
4159 		    px_args.persona_info_size);
4160 #endif
4161 		kfree_data(imgp->ip_px_pcred_info,
4162 		    px_args.posix_cred_info_size);
4163 
4164 		if (subsystem_root_path != NULL) {
4165 			zfree(ZV_NAMEI, subsystem_root_path);
4166 		}
4167 #if CONFIG_MACF
4168 		struct ip_px_smpx_s *px_s = &imgp->ip_px_smpx;
4169 		kfree_data(px_s->array, px_args.mac_extensions_size);
4170 		kfree_data(px_s->data, (vm_size_t)px_s->datalen);
4171 
4172 		if (imgp->ip_execlabelp) {
4173 			mac_cred_label_free(imgp->ip_execlabelp);
4174 			imgp->ip_execlabelp = NULL;
4175 		}
4176 		if (imgp->ip_scriptlabelp) {
4177 			mac_vnode_label_free(imgp->ip_scriptlabelp);
4178 			imgp->ip_scriptlabelp = NULL;
4179 		}
4180 		if (imgp->ip_cs_error != OS_REASON_NULL) {
4181 			os_reason_free(imgp->ip_cs_error);
4182 			imgp->ip_cs_error = OS_REASON_NULL;
4183 		}
4184 		if (imgp->ip_inherited_shared_region_id != NULL) {
4185 			kfree_data(imgp->ip_inherited_shared_region_id,
4186 			    strlen(imgp->ip_inherited_shared_region_id) + 1);
4187 			imgp->ip_inherited_shared_region_id = NULL;
4188 		}
4189 #endif
4190 		if (imgp->ip_sc_port != NULL) {
4191 			ipc_port_release_send(imgp->ip_sc_port);
4192 			imgp->ip_sc_port = NULL;
4193 		}
4194 	}
4195 
4196 #if CONFIG_DTRACE
4197 	if (spawn_no_exec) {
4198 		/*
4199 		 * In the original DTrace reference implementation,
4200 		 * posix_spawn() was a libc routine that just
4201 		 * did vfork(2) then exec(2).  Thus the proc::: probes
4202 		 * are very fork/exec oriented.  The details of this
4203 		 * in-kernel implementation of posix_spawn() is different
4204 		 * (while producing the same process-observable effects)
4205 		 * particularly w.r.t. errors, and which thread/process
4206 		 * is constructing what on behalf of whom.
4207 		 */
4208 		if (error) {
4209 			DTRACE_PROC1(spawn__failure, int, error);
4210 		} else {
4211 			DTRACE_PROC(spawn__success);
4212 			/*
4213 			 * Some DTrace scripts, e.g. newproc.d in
4214 			 * /usr/bin, rely on the the 'exec-success'
4215 			 * probe being fired in the child after the
4216 			 * new process image has been constructed
4217 			 * in order to determine the associated pid.
4218 			 *
4219 			 * So, even though the parent built the image
4220 			 * here, for compatibility, mark the new thread
4221 			 * so 'exec-success' fires on it as it leaves
4222 			 * the kernel.
4223 			 */
4224 			dtrace_thread_didexec(imgp->ip_new_thread);
4225 		}
4226 	} else {
4227 		if (error) {
4228 			DTRACE_PROC1(exec__failure, int, error);
4229 		} else {
4230 			dtrace_thread_didexec(imgp->ip_new_thread);
4231 		}
4232 	}
4233 
4234 	if ((dtrace_proc_waitfor_hook = dtrace_proc_waitfor_exec_ptr) != NULL) {
4235 		(*dtrace_proc_waitfor_hook)(p);
4236 	}
4237 #endif
4238 
4239 #if CONFIG_AUDIT
4240 	if (!error && AUDIT_ENABLED() && p) {
4241 		/* Add the CDHash of the new process to the audit record */
4242 		uint8_t *cdhash = cs_get_cdhash(p);
4243 		if (cdhash) {
4244 			AUDIT_ARG(data, cdhash, sizeof(uint8_t), CS_CDHASH_LEN);
4245 		}
4246 	}
4247 #endif
4248 
4249 	/*
4250 	 * clear bsd_info from old task if it did exec.
4251 	 */
4252 	if (task_did_exec(old_task)) {
4253 		set_bsdtask_info(old_task, NULL);
4254 		clear_thread_ro_proc(get_machthread(uthread));
4255 	}
4256 
4257 	/* clear bsd_info from new task and terminate it if exec failed  */
4258 	if (new_task != NULL && task_is_exec_copy(new_task)) {
4259 		set_bsdtask_info(new_task, NULL);
4260 		task_terminate_internal(new_task);
4261 		if (imgp && imgp->ip_new_thread) {
4262 			clear_thread_ro_proc(imgp->ip_new_thread);
4263 		}
4264 	}
4265 
4266 	/* Return to both the parent and the child? */
4267 	if (imgp != NULL && spawn_no_exec) {
4268 		/*
4269 		 * If the parent wants the pid, copy it out
4270 		 */
4271 		if (pid != USER_ADDR_NULL) {
4272 			_Static_assert(sizeof(pid_t) == 4, "posix_spawn() assumes a 32-bit pid_t");
4273 			bool aligned = (pid & 3) == 0;
4274 			if (aligned) {
4275 				(void)copyout_atomic32(proc_getpid(p), pid);
4276 			} else {
4277 				(void)suword(pid, proc_getpid(p));
4278 			}
4279 		}
4280 		retval[0] = error;
4281 
4282 		/*
4283 		 * If we had an error, perform an internal reap ; this is
4284 		 * entirely safe, as we have a real process backing us.
4285 		 */
4286 		if (error) {
4287 			proc_list_lock();
4288 			p->p_listflag |= P_LIST_DEADPARENT;
4289 			proc_list_unlock();
4290 			proc_lock(p);
4291 			/* make sure no one else has killed it off... */
4292 			if (p->p_stat != SZOMB && p->exit_thread == NULL) {
4293 				p->exit_thread = current_thread();
4294 				proc_unlock(p);
4295 				exit1(p, 1, (int *)NULL);
4296 			} else {
4297 				/* someone is doing it for us; just skip it */
4298 				proc_unlock(p);
4299 			}
4300 		}
4301 	}
4302 
4303 	/*
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.
4307 	 */
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);
4311 	}
4312 
4313 	/* Release the thread ref returned by fork_create_child/fork1 */
4314 	if (imgp != NULL && imgp->ip_new_thread) {
4315 		/* wake up the new 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;
4319 	}
4320 
4321 	/* Release the ref returned by fork_create_child/fork1 */
4322 	if (new_task) {
4323 		task_deallocate(new_task);
4324 		new_task = NULL;
4325 	}
4326 
4327 	if (should_release_proc_ref) {
4328 		proc_rele(p);
4329 	}
4330 
4331 	kfree_type(typeof(*__spawn_data), __spawn_data);
4332 
4333 	if (inherit != NULL) {
4334 		ipc_importance_release(inherit);
4335 	}
4336 
4337 	return error;
4338 }
4339 
4340 /*
4341  * proc_exec_switch_task
4342  *
4343  * Parameters:  p			proc
4344  *		old_task		task before exec
4345  *		new_task		task after exec
4346  *		new_thread		thread in new task
4347  *		inherit			resulting importance linkage
4348  *
4349  * Returns: proc.
4350  *
4351  * Note: The function will switch the task pointer of proc
4352  * from old task to new task. The switch needs to happen
4353  * after draining all proc refs and inside a proc translock.
4354  * In the case of failure to switch the task, which might happen
4355  * if the process received a SIGKILL or jetsam killed it, it will make
4356  * sure that the new tasks terminates. User proc ref returned
4357  * to caller.
4358  *
4359  * This function is called after point of no return, in the case
4360  * failure to switch, it will terminate the new task and swallow the
4361  * error and let the terminated process complete exec and die.
4362  */
4363 proc_t
proc_exec_switch_task(proc_t p,task_t old_task,task_t new_task,thread_t new_thread,void ** inherit)4364 proc_exec_switch_task(proc_t p, task_t old_task, task_t new_task, thread_t new_thread,
4365     void **inherit)
4366 {
4367 	int error = 0;
4368 	boolean_t task_active;
4369 	boolean_t proc_active;
4370 	boolean_t thread_active;
4371 	thread_t old_thread = current_thread();
4372 
4373 	/*
4374 	 * Switch the task pointer of proc to new task.
4375 	 * Before switching the task, wait for proc_refdrain.
4376 	 * After the switch happens, the proc can disappear,
4377 	 * take a ref before it disappears. Waiting for
4378 	 * proc_refdrain in exec will block all other threads
4379 	 * trying to take a proc ref, boost the current thread
4380 	 * to avoid priority inversion.
4381 	 */
4382 	thread_set_exec_promotion(old_thread);
4383 	p = proc_refdrain_will_exec(p);
4384 	/* extra proc ref returned to the caller */
4385 
4386 	assert(get_threadtask(new_thread) == new_task);
4387 	task_active = task_is_active(new_task);
4388 
4389 	/* Take the proc_translock to change the task ptr */
4390 	proc_lock(p);
4391 	proc_active = !(p->p_lflag & P_LEXIT);
4392 
4393 	/* Check if the current thread is not aborted due to SIGKILL */
4394 	thread_active = thread_is_active(old_thread);
4395 
4396 	/*
4397 	 * Do not switch the task if the new task or proc is already terminated
4398 	 * as a result of error in exec past point of no return
4399 	 */
4400 	if (proc_active && task_active && thread_active) {
4401 		error = proc_transstart(p, 1, 0);
4402 		if (error == 0) {
4403 			uthread_t new_uthread = get_bsdthread_info(new_thread);
4404 			uthread_t old_uthread = current_uthread();
4405 
4406 			/*
4407 			 * bsd_info of old_task will get cleared in execve and posix_spawn
4408 			 * after firing exec-success/error dtrace probe.
4409 			 */
4410 			proc_set_task(p, new_task);
4411 			proc_switch_ro(p, task_get_ro(new_task));
4412 
4413 			/* Clear dispatchqueue and workloop ast offset */
4414 			p->p_dispatchqueue_offset = 0;
4415 			p->p_dispatchqueue_serialno_offset = 0;
4416 			p->p_dispatchqueue_label_offset = 0;
4417 			p->p_return_to_kernel_offset = 0;
4418 			p->p_pthread_wq_quantum_offset = 0;
4419 
4420 			/* Copy the signal state, dtrace state and set bsd ast on new thread */
4421 			act_set_astbsd(new_thread);
4422 			new_uthread->uu_siglist = old_uthread->uu_siglist;
4423 			new_uthread->uu_sigwait = old_uthread->uu_sigwait;
4424 			new_uthread->uu_sigmask = old_uthread->uu_sigmask;
4425 			new_uthread->uu_oldmask = old_uthread->uu_oldmask;
4426 			new_uthread->uu_exit_reason = old_uthread->uu_exit_reason;
4427 #if CONFIG_DTRACE
4428 			new_uthread->t_dtrace_sig = old_uthread->t_dtrace_sig;
4429 			new_uthread->t_dtrace_stop = old_uthread->t_dtrace_stop;
4430 			new_uthread->t_dtrace_resumepid = old_uthread->t_dtrace_resumepid;
4431 			assert(new_uthread->t_dtrace_scratch == NULL);
4432 			new_uthread->t_dtrace_scratch = old_uthread->t_dtrace_scratch;
4433 
4434 			old_uthread->t_dtrace_sig = 0;
4435 			old_uthread->t_dtrace_stop = 0;
4436 			old_uthread->t_dtrace_resumepid = 0;
4437 			old_uthread->t_dtrace_scratch = NULL;
4438 #endif
4439 			/* Copy the resource accounting info */
4440 			thread_copy_resource_info(new_thread, current_thread());
4441 
4442 			/* Clear the exit reason and signal state on old thread */
4443 			old_uthread->uu_exit_reason = NULL;
4444 			old_uthread->uu_siglist = 0;
4445 
4446 			/* Add the new uthread to proc uthlist and remove the old one */
4447 			TAILQ_REMOVE(&p->p_uthlist, old_uthread, uu_list);
4448 			assert(TAILQ_EMPTY(&p->p_uthlist));
4449 			TAILQ_INSERT_TAIL(&p->p_uthlist, new_uthread, uu_list);
4450 
4451 			task_set_did_exec_flag(old_task);
4452 			task_clear_exec_copy_flag(new_task);
4453 
4454 			task_copy_fields_for_exec(new_task, old_task);
4455 
4456 			/* Transfer sandbox filter bits to new_task. */
4457 			task_transfer_mach_filter_bits(new_task, old_task);
4458 
4459 			/*
4460 			 * Need to transfer pending watch port boosts to the new task
4461 			 * while still making sure that the old task remains in the
4462 			 * importance linkage. Create an importance linkage from old task
4463 			 * to new task, then switch the task importance base of old task
4464 			 * and new task. After the switch the port watch boost will be
4465 			 * boosting the new task and new task will be donating importance
4466 			 * to old task.
4467 			 */
4468 			*inherit = ipc_importance_exec_switch_task(old_task, new_task);
4469 
4470 			proc_transend(p, 1);
4471 		}
4472 	}
4473 
4474 	proc_unlock(p);
4475 	proc_refwake_did_exec(p);
4476 	thread_clear_exec_promotion(old_thread);
4477 
4478 	if (error != 0 || !task_active || !proc_active || !thread_active) {
4479 		task_terminate_internal(new_task);
4480 	}
4481 
4482 	return p;
4483 }
4484 
4485 /*
4486  * execve
4487  *
4488  * Parameters:	uap->fname		File name to exec
4489  *		uap->argp		Argument list
4490  *		uap->envp		Environment list
4491  *
4492  * Returns:	0			Success
4493  *	__mac_execve:EINVAL		Invalid argument
4494  *	__mac_execve:ENOTSUP		Invalid argument
4495  *	__mac_execve:EACCES		Permission denied
4496  *	__mac_execve:EINTR		Interrupted function
4497  *	__mac_execve:ENOMEM		Not enough space
4498  *	__mac_execve:EFAULT		Bad address
4499  *	__mac_execve:ENAMETOOLONG	Filename too long
4500  *	__mac_execve:ENOEXEC		Executable file format error
4501  *	__mac_execve:ETXTBSY		Text file busy [misuse of error code]
4502  *	__mac_execve:???
4503  *
4504  * TODO:	Dynamic linker header address on stack is copied via suword()
4505  */
4506 /* ARGSUSED */
4507 int
execve(proc_t p,struct execve_args * uap,int32_t * retval)4508 execve(proc_t p, struct execve_args *uap, int32_t *retval)
4509 {
4510 	struct __mac_execve_args muap;
4511 	int err;
4512 
4513 	memoryshot(VM_EXECVE, DBG_FUNC_NONE);
4514 
4515 	muap.fname = uap->fname;
4516 	muap.argp = uap->argp;
4517 	muap.envp = uap->envp;
4518 	muap.mac_p = USER_ADDR_NULL;
4519 	err = __mac_execve(p, &muap, retval);
4520 
4521 	return err;
4522 }
4523 
4524 /*
4525  * __mac_execve
4526  *
4527  * Parameters:	uap->fname		File name to exec
4528  *		uap->argp		Argument list
4529  *		uap->envp		Environment list
4530  *		uap->mac_p		MAC label supplied by caller
4531  *
4532  * Returns:	0			Success
4533  *		EINVAL			Invalid argument
4534  *		ENOTSUP			Not supported
4535  *		ENOEXEC			Executable file format error
4536  *	exec_activate_image:EINVAL	Invalid argument
4537  *	exec_activate_image:EACCES	Permission denied
4538  *	exec_activate_image:EINTR	Interrupted function
4539  *	exec_activate_image:ENOMEM	Not enough space
4540  *	exec_activate_image:EFAULT	Bad address
4541  *	exec_activate_image:ENAMETOOLONG	Filename too long
4542  *	exec_activate_image:ENOEXEC	Executable file format error
4543  *	exec_activate_image:ETXTBSY	Text file busy [misuse of error code]
4544  *	exec_activate_image:EBADEXEC	The executable is corrupt/unknown
4545  *	exec_activate_image:???
4546  *	mac_execve_enter:???
4547  *
4548  * TODO:	Dynamic linker header address on stack is copied via suword()
4549  */
4550 int
__mac_execve(proc_t p,struct __mac_execve_args * uap,int32_t * retval __unused)4551 __mac_execve(proc_t p, struct __mac_execve_args *uap, int32_t *retval __unused)
4552 {
4553 	struct image_params *imgp = NULL;
4554 	struct vnode_attr *vap = NULL;
4555 	struct vnode_attr *origvap = NULL;
4556 	int error;
4557 	int is_64 = IS_64BIT_PROCESS(p);
4558 	struct vfs_context context;
4559 	struct uthread  *uthread = NULL;
4560 	task_t old_task = current_task();
4561 	task_t new_task = NULL;
4562 	boolean_t should_release_proc_ref = FALSE;
4563 	boolean_t exec_done = FALSE;
4564 	const bool in_vfexec = false;
4565 	void *inherit = NULL;
4566 	struct {
4567 		struct image_params imgp;
4568 		struct vnode_attr va;
4569 		struct vnode_attr origva;
4570 	} *__execve_data;
4571 
4572 	context.vc_thread = current_thread();
4573 	context.vc_ucred = kauth_cred_proc_ref(p);      /* XXX must NOT be kauth_cred_get() */
4574 
4575 	/* Allocate a big chunk for locals instead of using stack since these
4576 	 * structures a pretty big.
4577 	 */
4578 	__execve_data = kalloc_type(typeof(*__execve_data), Z_WAITOK | Z_ZERO);
4579 	if (__execve_data == NULL) {
4580 		error = ENOMEM;
4581 		goto exit_with_error;
4582 	}
4583 	imgp = &__execve_data->imgp;
4584 	vap = &__execve_data->va;
4585 	origvap = &__execve_data->origva;
4586 
4587 	/* Initialize the common data in the image_params structure */
4588 	imgp->ip_user_fname = uap->fname;
4589 	imgp->ip_user_argv = uap->argp;
4590 	imgp->ip_user_envv = uap->envp;
4591 	imgp->ip_vattr = vap;
4592 	imgp->ip_origvattr = origvap;
4593 	imgp->ip_vfs_context = &context;
4594 	imgp->ip_flags = (is_64 ? IMGPF_WAS_64BIT_ADDR : IMGPF_NONE) | ((p->p_flag & P_DISABLE_ASLR) ? IMGPF_DISABLE_ASLR : IMGPF_NONE);
4595 	imgp->ip_seg = (is_64 ? UIO_USERSPACE64 : UIO_USERSPACE32);
4596 	imgp->ip_mac_return = 0;
4597 	imgp->ip_cs_error = OS_REASON_NULL;
4598 	imgp->ip_simulator_binary = IMGPF_SB_DEFAULT;
4599 	imgp->ip_subsystem_root_path = NULL;
4600 
4601 #if CONFIG_MACF
4602 	if (uap->mac_p != USER_ADDR_NULL) {
4603 		error = mac_execve_enter(uap->mac_p, imgp);
4604 		if (error) {
4605 			kauth_cred_unref(&context.vc_ucred);
4606 			goto exit_with_error;
4607 		}
4608 	}
4609 #endif
4610 	uthread = current_uthread();
4611 	{
4612 		imgp->ip_flags |= IMGPF_EXEC;
4613 
4614 		/*
4615 		 * For execve case, create a new task and thread
4616 		 * which points to current_proc. The current_proc will point
4617 		 * to the new task after image activation and proc ref drain.
4618 		 *
4619 		 * proc (current_proc) <-----  old_task (current_task)
4620 		 *  ^ |                                ^
4621 		 *  | |                                |
4622 		 *  | ----------------------------------
4623 		 *  |
4624 		 *  --------- new_task (task marked as TF_EXEC_COPY)
4625 		 *
4626 		 * After image activation, the proc will point to the new task
4627 		 * and would look like following.
4628 		 *
4629 		 * proc (current_proc)  <-----  old_task (current_task, marked as TPF_DID_EXEC)
4630 		 *  ^ |
4631 		 *  | |
4632 		 *  | ----------> new_task
4633 		 *  |               |
4634 		 *  -----------------
4635 		 *
4636 		 * During exec any transition from new_task -> proc is fine, but don't allow
4637 		 * transition from proc->task, since it will modify old_task.
4638 		 */
4639 		imgp->ip_new_thread = fork_create_child(old_task,
4640 		    NULL,
4641 		    p,
4642 		    FALSE,
4643 		    p->p_flag & P_LP64,
4644 		    task_get_64bit_data(old_task),
4645 		    TRUE);
4646 		/* task and thread ref returned by fork_create_child */
4647 		if (imgp->ip_new_thread == NULL) {
4648 			error = ENOMEM;
4649 			goto exit_with_error;
4650 		}
4651 
4652 		new_task = get_threadtask(imgp->ip_new_thread);
4653 		context.vc_thread = imgp->ip_new_thread;
4654 	}
4655 
4656 	imgp->ip_subsystem_root_path = p->p_subsystem_root_path;
4657 
4658 	error = exec_activate_image(imgp);
4659 	/* thread and task ref returned for vfexec case */
4660 
4661 	if (imgp->ip_new_thread != NULL) {
4662 		/*
4663 		 * task reference might be returned by exec_activate_image
4664 		 * for vfexec.
4665 		 */
4666 		new_task = get_threadtask(imgp->ip_new_thread);
4667 #if defined(HAS_APPLE_PAC)
4668 		ml_task_set_disable_user_jop(new_task, imgp->ip_flags & IMGPF_NOJOP ? TRUE : FALSE);
4669 		ml_thread_set_disable_user_jop(imgp->ip_new_thread, imgp->ip_flags & IMGPF_NOJOP ? TRUE : FALSE);
4670 #endif
4671 	}
4672 
4673 	if (!error && !in_vfexec) {
4674 		p = proc_exec_switch_task(p, old_task, new_task, imgp->ip_new_thread, &inherit);
4675 		/* proc ref returned */
4676 		should_release_proc_ref = TRUE;
4677 	}
4678 
4679 	kauth_cred_unref(&context.vc_ucred);
4680 
4681 	if (!error) {
4682 		exec_done = TRUE;
4683 		assert(imgp->ip_new_thread != NULL);
4684 
4685 		exec_resettextvp(p, imgp);
4686 		error = check_for_signature(p, imgp);
4687 	}
4688 
4689 #if defined(HAS_APPLE_PAC)
4690 	if (imgp->ip_new_thread && !error) {
4691 		ml_task_set_jop_pid_from_shared_region(new_task);
4692 		ml_thread_set_jop_pid(imgp->ip_new_thread, new_task);
4693 	}
4694 #endif /* defined(HAS_APPLE_PAC) */
4695 
4696 	/* flag exec has occurred, notify only if it has not failed due to FP Key error */
4697 	if (exec_done && ((p->p_lflag & P_LTERM_DECRYPTFAIL) == 0)) {
4698 		proc_knote(p, NOTE_EXEC);
4699 	}
4700 
4701 	if (imgp->ip_vp != NULLVP) {
4702 		vnode_put(imgp->ip_vp);
4703 	}
4704 	if (imgp->ip_scriptvp != NULLVP) {
4705 		vnode_put(imgp->ip_scriptvp);
4706 	}
4707 	if (imgp->ip_strings) {
4708 		execargs_free(imgp);
4709 	}
4710 #if CONFIG_MACF
4711 	if (imgp->ip_execlabelp) {
4712 		mac_cred_label_free(imgp->ip_execlabelp);
4713 		imgp->ip_execlabelp = NULL;
4714 	}
4715 	if (imgp->ip_scriptlabelp) {
4716 		mac_vnode_label_free(imgp->ip_scriptlabelp);
4717 		imgp->ip_scriptlabelp = NULL;
4718 	}
4719 #endif
4720 	if (imgp->ip_cs_error != OS_REASON_NULL) {
4721 		os_reason_free(imgp->ip_cs_error);
4722 		imgp->ip_cs_error = OS_REASON_NULL;
4723 	}
4724 
4725 	if (!error) {
4726 		/*
4727 		 * We need to initialize the bank context behind the protection of
4728 		 * the proc_trans lock to prevent a race with exit. We can't do this during
4729 		 * exec_activate_image because task_bank_init checks entitlements that
4730 		 * aren't loaded until subsequent calls (including exec_resettextvp).
4731 		 */
4732 		error = proc_transstart(p, 0, 0);
4733 	}
4734 
4735 	if (!error) {
4736 		task_bank_init(new_task);
4737 		proc_transend(p, 0);
4738 
4739 #if __arm64__
4740 		proc_footprint_entitlement_hacks(p, new_task);
4741 #endif /* __arm64__ */
4742 
4743 #if XNU_TARGET_OS_OSX
4744 		if (IOTaskHasEntitlement(new_task, SINGLE_JIT_ENTITLEMENT)) {
4745 			vm_map_single_jit(get_task_map(new_task));
4746 		}
4747 #endif /* XNU_TARGET_OS_OSX */
4748 
4749 		/* Sever any extant thread affinity */
4750 		thread_affinity_exec(current_thread());
4751 
4752 		/* Inherit task role from old task to new task for exec */
4753 		if (!in_vfexec) {
4754 			proc_inherit_task_role(new_task, old_task);
4755 		}
4756 
4757 		thread_t main_thread = imgp->ip_new_thread;
4758 
4759 		task_set_main_thread_qos(new_task, main_thread);
4760 
4761 #if __has_feature(ptrauth_calls)
4762 		task_set_pac_exception_fatal_flag(new_task);
4763 #endif /* __has_feature(ptrauth_calls) */
4764 
4765 #if CONFIG_ARCADE
4766 		/*
4767 		 * Check to see if we need to trigger an arcade upcall AST now
4768 		 * that the vnode has been reset on the task.
4769 		 */
4770 		arcade_prepare(new_task, imgp->ip_new_thread);
4771 #endif /* CONFIG_ARCADE */
4772 
4773 #if CONFIG_MACF
4774 		proc_apply_jit_and_jumbo_va_policies(p, new_task);
4775 #endif /* CONFIG_MACF */
4776 
4777 		if (vm_darkwake_mode == TRUE) {
4778 			/*
4779 			 * This process is being launched when the system
4780 			 * is in darkwake. So mark it specially. This will
4781 			 * cause all its pages to be entered in the background Q.
4782 			 */
4783 			task_set_darkwake_mode(new_task, vm_darkwake_mode);
4784 		}
4785 
4786 #if CONFIG_DTRACE
4787 		dtrace_thread_didexec(imgp->ip_new_thread);
4788 
4789 		if ((dtrace_proc_waitfor_hook = dtrace_proc_waitfor_exec_ptr) != NULL) {
4790 			(*dtrace_proc_waitfor_hook)(p);
4791 		}
4792 #endif
4793 
4794 #if CONFIG_AUDIT
4795 		if (!error && AUDIT_ENABLED() && p) {
4796 			/* Add the CDHash of the new process to the audit record */
4797 			uint8_t *cdhash = cs_get_cdhash(p);
4798 			if (cdhash) {
4799 				AUDIT_ARG(data, cdhash, sizeof(uint8_t), CS_CDHASH_LEN);
4800 			}
4801 		}
4802 #endif
4803 	} else {
4804 		DTRACE_PROC1(exec__failure, int, error);
4805 	}
4806 
4807 exit_with_error:
4808 
4809 	/*
4810 	 * clear bsd_info from old task if it did exec.
4811 	 */
4812 	if (task_did_exec(old_task)) {
4813 		set_bsdtask_info(old_task, NULL);
4814 		clear_thread_ro_proc(get_machthread(uthread));
4815 	}
4816 
4817 	/* clear bsd_info from new task and terminate it if exec failed  */
4818 	if (new_task != NULL && task_is_exec_copy(new_task)) {
4819 		set_bsdtask_info(new_task, NULL);
4820 		if (imgp && imgp->ip_new_thread) {
4821 			clear_thread_ro_proc(imgp->ip_new_thread);
4822 		}
4823 		task_terminate_internal(new_task);
4824 	}
4825 
4826 	if (imgp != NULL) {
4827 		/* Clear the initial wait on the thread transferring watchports */
4828 		if (imgp->ip_new_thread) {
4829 			task_clear_return_wait(get_threadtask(imgp->ip_new_thread), TCRW_CLEAR_INITIAL_WAIT);
4830 		}
4831 
4832 		/* Transfer the watchport boost to new task */
4833 		if (!error && !in_vfexec) {
4834 			task_transfer_turnstile_watchports(old_task,
4835 			    new_task, imgp->ip_new_thread);
4836 		}
4837 		/*
4838 		 * Do not terminate the current task, if proc_exec_switch_task did not
4839 		 * switch the tasks, terminating the current task without the switch would
4840 		 * result in loosing the SIGKILL status.
4841 		 */
4842 		if (task_did_exec(old_task)) {
4843 			/* Terminate the current task, since exec will start in new task */
4844 			task_terminate_internal(old_task);
4845 		}
4846 
4847 		/* Release the thread ref returned by fork_create_child */
4848 		if (imgp->ip_new_thread) {
4849 			/* wake up the new exec thread */
4850 			task_clear_return_wait(get_threadtask(imgp->ip_new_thread), TCRW_CLEAR_FINAL_WAIT);
4851 			thread_deallocate(imgp->ip_new_thread);
4852 			imgp->ip_new_thread = NULL;
4853 		}
4854 	}
4855 
4856 	/* Release the ref returned by fork_create_child */
4857 	if (new_task) {
4858 		task_deallocate(new_task);
4859 		new_task = NULL;
4860 	}
4861 
4862 	if (should_release_proc_ref) {
4863 		proc_rele(p);
4864 	}
4865 
4866 	kfree_type(typeof(*__execve_data), __execve_data);
4867 
4868 	if (inherit != NULL) {
4869 		ipc_importance_release(inherit);
4870 	}
4871 
4872 	return error;
4873 }
4874 
4875 
4876 /*
4877  * copyinptr
4878  *
4879  * Description:	Copy a pointer in from user space to a user_addr_t in kernel
4880  *		space, based on 32/64 bitness of the user space
4881  *
4882  * Parameters:	froma			User space address
4883  *		toptr			Address of kernel space user_addr_t
4884  *		ptr_size		4/8, based on 'froma' address space
4885  *
4886  * Returns:	0			Success
4887  *		EFAULT			Bad 'froma'
4888  *
4889  * Implicit returns:
4890  *		*ptr_size		Modified
4891  */
4892 static int
copyinptr(user_addr_t froma,user_addr_t * toptr,int ptr_size)4893 copyinptr(user_addr_t froma, user_addr_t *toptr, int ptr_size)
4894 {
4895 	int error;
4896 
4897 	if (ptr_size == 4) {
4898 		/* 64 bit value containing 32 bit address */
4899 		unsigned int i = 0;
4900 
4901 		error = copyin(froma, &i, 4);
4902 		*toptr = CAST_USER_ADDR_T(i);   /* SAFE */
4903 	} else {
4904 		error = copyin(froma, toptr, 8);
4905 	}
4906 	return error;
4907 }
4908 
4909 
4910 /*
4911  * copyoutptr
4912  *
4913  * Description:	Copy a pointer out from a user_addr_t in kernel space to
4914  *		user space, based on 32/64 bitness of the user space
4915  *
4916  * Parameters:	ua			User space address to copy to
4917  *		ptr			Address of kernel space user_addr_t
4918  *		ptr_size		4/8, based on 'ua' address space
4919  *
4920  * Returns:	0			Success
4921  *		EFAULT			Bad 'ua'
4922  *
4923  */
4924 static int
copyoutptr(user_addr_t ua,user_addr_t ptr,int ptr_size)4925 copyoutptr(user_addr_t ua, user_addr_t ptr, int ptr_size)
4926 {
4927 	int error;
4928 
4929 	if (ptr_size == 4) {
4930 		/* 64 bit value containing 32 bit address */
4931 		unsigned int i = CAST_DOWN_EXPLICIT(unsigned int, ua);   /* SAFE */
4932 
4933 		error = copyout(&i, ptr, 4);
4934 	} else {
4935 		error = copyout(&ua, ptr, 8);
4936 	}
4937 	return error;
4938 }
4939 
4940 
4941 /*
4942  * exec_copyout_strings
4943  *
4944  * Copy out the strings segment to user space.  The strings segment is put
4945  * on a preinitialized stack frame.
4946  *
4947  * Parameters:	struct image_params *	the image parameter block
4948  *		int *			a pointer to the stack offset variable
4949  *
4950  * Returns:	0			Success
4951  *		!0			Faiure: errno
4952  *
4953  * Implicit returns:
4954  *		(*stackp)		The stack offset, modified
4955  *
4956  * Note:	The strings segment layout is backward, from the beginning
4957  *		of the top of the stack to consume the minimal amount of
4958  *		space possible; the returned stack pointer points to the
4959  *		end of the area consumed (stacks grow downward).
4960  *
4961  *		argc is an int; arg[i] are pointers; env[i] are pointers;
4962  *		the 0's are (void *)NULL's
4963  *
4964  * The stack frame layout is:
4965  *
4966  *      +-------------+ <- p->user_stack
4967  *      |     16b     |
4968  *      +-------------+
4969  *      | STRING AREA |
4970  *      |      :      |
4971  *      |      :      |
4972  *      |      :      |
4973  *      +- -- -- -- --+
4974  *      |  PATH AREA  |
4975  *      +-------------+
4976  *      |      0      |
4977  *      +-------------+
4978  *      |  applev[n]  |
4979  *      +-------------+
4980  *             :
4981  *             :
4982  *      +-------------+
4983  *      |  applev[1]  |
4984  *      +-------------+
4985  *      | exec_path / |
4986  *      |  applev[0]  |
4987  *      +-------------+
4988  *      |      0      |
4989  *      +-------------+
4990  *      |    env[n]   |
4991  *      +-------------+
4992  *             :
4993  *             :
4994  *      +-------------+
4995  *      |    env[0]   |
4996  *      +-------------+
4997  *      |      0      |
4998  *      +-------------+
4999  *      | arg[argc-1] |
5000  *      +-------------+
5001  *             :
5002  *             :
5003  *      +-------------+
5004  *      |    arg[0]   |
5005  *      +-------------+
5006  *      |     argc    |
5007  * sp-> +-------------+
5008  *
5009  * Although technically a part of the STRING AREA, we treat the PATH AREA as
5010  * a separate entity.  This allows us to align the beginning of the PATH AREA
5011  * to a pointer boundary so that the exec_path, env[i], and argv[i] pointers
5012  * which preceed it on the stack are properly aligned.
5013  */
5014 __attribute__((noinline))
5015 static int
exec_copyout_strings(struct image_params * imgp,user_addr_t * stackp)5016 exec_copyout_strings(struct image_params *imgp, user_addr_t *stackp)
5017 {
5018 	proc_t p = vfs_context_proc(imgp->ip_vfs_context);
5019 	int     ptr_size = (imgp->ip_flags & IMGPF_IS_64BIT_ADDR) ? 8 : 4;
5020 	int     ptr_area_size;
5021 	void *ptr_buffer_start, *ptr_buffer;
5022 	size_t string_size;
5023 
5024 	user_addr_t     string_area;    /* *argv[], *env[] */
5025 	user_addr_t     ptr_area;       /* argv[], env[], applev[] */
5026 	user_addr_t argc_area;  /* argc */
5027 	user_addr_t     stack;
5028 	int error;
5029 
5030 	unsigned i;
5031 	struct copyout_desc {
5032 		char    *start_string;
5033 		int             count;
5034 #if CONFIG_DTRACE
5035 		user_addr_t     *dtrace_cookie;
5036 #endif
5037 		boolean_t       null_term;
5038 	} descriptors[] = {
5039 		{
5040 			.start_string = imgp->ip_startargv,
5041 			.count = imgp->ip_argc,
5042 #if CONFIG_DTRACE
5043 			.dtrace_cookie = &p->p_dtrace_argv,
5044 #endif
5045 			.null_term = TRUE
5046 		},
5047 		{
5048 			.start_string = imgp->ip_endargv,
5049 			.count = imgp->ip_envc,
5050 #if CONFIG_DTRACE
5051 			.dtrace_cookie = &p->p_dtrace_envp,
5052 #endif
5053 			.null_term = TRUE
5054 		},
5055 		{
5056 			.start_string = imgp->ip_strings,
5057 			.count = 1,
5058 #if CONFIG_DTRACE
5059 			.dtrace_cookie = NULL,
5060 #endif
5061 			.null_term = FALSE
5062 		},
5063 		{
5064 			.start_string = imgp->ip_endenvv,
5065 			.count = imgp->ip_applec - 1, /* exec_path handled above */
5066 #if CONFIG_DTRACE
5067 			.dtrace_cookie = NULL,
5068 #endif
5069 			.null_term = TRUE
5070 		}
5071 	};
5072 
5073 	stack = *stackp;
5074 
5075 	/*
5076 	 * All previous contributors to the string area
5077 	 * should have aligned their sub-area
5078 	 */
5079 	if (imgp->ip_strspace % ptr_size != 0) {
5080 		error = EINVAL;
5081 		goto bad;
5082 	}
5083 
5084 	/* Grow the stack down for the strings we've been building up */
5085 	string_size = imgp->ip_strendp - imgp->ip_strings;
5086 	stack -= string_size;
5087 	string_area = stack;
5088 
5089 	/*
5090 	 * Need room for one pointer for each string, plus
5091 	 * one for the NULLs terminating the argv, envv, and apple areas.
5092 	 */
5093 	ptr_area_size = (imgp->ip_argc + imgp->ip_envc + imgp->ip_applec + 3) * ptr_size;
5094 	stack -= ptr_area_size;
5095 	ptr_area = stack;
5096 
5097 	/* We'll construct all the pointer arrays in our string buffer,
5098 	 * which we already know is aligned properly, and ip_argspace
5099 	 * was used to verify we have enough space.
5100 	 */
5101 	ptr_buffer_start = ptr_buffer = (void *)imgp->ip_strendp;
5102 
5103 	/*
5104 	 * Need room for pointer-aligned argc slot.
5105 	 */
5106 	stack -= ptr_size;
5107 	argc_area = stack;
5108 
5109 	/*
5110 	 * Record the size of the arguments area so that sysctl_procargs()
5111 	 * can return the argument area without having to parse the arguments.
5112 	 */
5113 	proc_lock(p);
5114 	p->p_argc = imgp->ip_argc;
5115 	p->p_argslen = (int)(*stackp - string_area);
5116 	proc_unlock(p);
5117 
5118 	/* Return the initial stack address: the location of argc */
5119 	*stackp = stack;
5120 
5121 	/*
5122 	 * Copy out the entire strings area.
5123 	 */
5124 	error = copyout(imgp->ip_strings, string_area,
5125 	    string_size);
5126 	if (error) {
5127 		goto bad;
5128 	}
5129 
5130 	for (i = 0; i < sizeof(descriptors) / sizeof(descriptors[0]); i++) {
5131 		char *cur_string = descriptors[i].start_string;
5132 		int j;
5133 
5134 #if CONFIG_DTRACE
5135 		if (descriptors[i].dtrace_cookie) {
5136 			proc_lock(p);
5137 			*descriptors[i].dtrace_cookie = ptr_area + ((uintptr_t)ptr_buffer - (uintptr_t)ptr_buffer_start); /* dtrace convenience */
5138 			proc_unlock(p);
5139 		}
5140 #endif /* CONFIG_DTRACE */
5141 
5142 		/*
5143 		 * For each segment (argv, envv, applev), copy as many pointers as requested
5144 		 * to our pointer buffer.
5145 		 */
5146 		for (j = 0; j < descriptors[i].count; j++) {
5147 			user_addr_t cur_address = string_area + (cur_string - imgp->ip_strings);
5148 
5149 			/* Copy out the pointer to the current string. Alignment has been verified  */
5150 			if (ptr_size == 8) {
5151 				*(uint64_t *)ptr_buffer = (uint64_t)cur_address;
5152 			} else {
5153 				*(uint32_t *)ptr_buffer = (uint32_t)cur_address;
5154 			}
5155 
5156 			ptr_buffer = (void *)((uintptr_t)ptr_buffer + ptr_size);
5157 			cur_string += strlen(cur_string) + 1; /* Only a NUL between strings in the same area */
5158 		}
5159 
5160 		if (descriptors[i].null_term) {
5161 			if (ptr_size == 8) {
5162 				*(uint64_t *)ptr_buffer = 0ULL;
5163 			} else {
5164 				*(uint32_t *)ptr_buffer = 0;
5165 			}
5166 
5167 			ptr_buffer = (void *)((uintptr_t)ptr_buffer + ptr_size);
5168 		}
5169 	}
5170 
5171 	/*
5172 	 * Copy out all our pointer arrays in bulk.
5173 	 */
5174 	error = copyout(ptr_buffer_start, ptr_area,
5175 	    ptr_area_size);
5176 	if (error) {
5177 		goto bad;
5178 	}
5179 
5180 	/* argc (int32, stored in a ptr_size area) */
5181 	error = copyoutptr((user_addr_t)imgp->ip_argc, argc_area, ptr_size);
5182 	if (error) {
5183 		goto bad;
5184 	}
5185 
5186 bad:
5187 	return error;
5188 }
5189 
5190 
5191 /*
5192  * exec_extract_strings
5193  *
5194  * Copy arguments and environment from user space into work area; we may
5195  * have already copied some early arguments into the work area, and if
5196  * so, any arguments opied in are appended to those already there.
5197  * This function is the primary manipulator of ip_argspace, since
5198  * these are the arguments the client of execve(2) knows about. After
5199  * each argv[]/envv[] string is copied, we charge the string length
5200  * and argv[]/envv[] pointer slot to ip_argspace, so that we can
5201  * full preflight the arg list size.
5202  *
5203  * Parameters:	struct image_params *	the image parameter block
5204  *
5205  * Returns:	0			Success
5206  *		!0			Failure: errno
5207  *
5208  * Implicit returns;
5209  *		(imgp->ip_argc)		Count of arguments, updated
5210  *		(imgp->ip_envc)		Count of environment strings, updated
5211  *		(imgp->ip_argspace)	Count of remaining of NCARGS
5212  *		(imgp->ip_interp_buffer)	Interpreter and args (mutated in place)
5213  *
5214  *
5215  * Note:	The argument and environment vectors are user space pointers
5216  *		to arrays of user space pointers.
5217  */
5218 __attribute__((noinline))
5219 static int
exec_extract_strings(struct image_params * imgp)5220 exec_extract_strings(struct image_params *imgp)
5221 {
5222 	int error = 0;
5223 	int     ptr_size = (imgp->ip_flags & IMGPF_WAS_64BIT_ADDR) ? 8 : 4;
5224 	int new_ptr_size = (imgp->ip_flags & IMGPF_IS_64BIT_ADDR) ? 8 : 4;
5225 	user_addr_t     argv = imgp->ip_user_argv;
5226 	user_addr_t     envv = imgp->ip_user_envv;
5227 
5228 	/*
5229 	 * Adjust space reserved for the path name by however much padding it
5230 	 * needs. Doing this here since we didn't know if this would be a 32-
5231 	 * or 64-bit process back in exec_save_path.
5232 	 */
5233 	while (imgp->ip_strspace % new_ptr_size != 0) {
5234 		*imgp->ip_strendp++ = '\0';
5235 		imgp->ip_strspace--;
5236 		/* imgp->ip_argspace--; not counted towards exec args total */
5237 	}
5238 
5239 	/*
5240 	 * From now on, we start attributing string space to ip_argspace
5241 	 */
5242 	imgp->ip_startargv = imgp->ip_strendp;
5243 	imgp->ip_argc = 0;
5244 
5245 	if ((imgp->ip_flags & IMGPF_INTERPRET) != 0) {
5246 		user_addr_t     arg;
5247 		char *argstart, *ch;
5248 
5249 		/* First, the arguments in the "#!" string are tokenized and extracted. */
5250 		argstart = imgp->ip_interp_buffer;
5251 		while (argstart) {
5252 			ch = argstart;
5253 			while (*ch && !IS_WHITESPACE(*ch)) {
5254 				ch++;
5255 			}
5256 
5257 			if (*ch == '\0') {
5258 				/* last argument, no need to NUL-terminate */
5259 				error = exec_add_user_string(imgp, CAST_USER_ADDR_T(argstart), UIO_SYSSPACE, TRUE);
5260 				argstart = NULL;
5261 			} else {
5262 				/* NUL-terminate */
5263 				*ch = '\0';
5264 				error = exec_add_user_string(imgp, CAST_USER_ADDR_T(argstart), UIO_SYSSPACE, TRUE);
5265 
5266 				/*
5267 				 * Find the next string. We know spaces at the end of the string have already
5268 				 * been stripped.
5269 				 */
5270 				argstart = ch + 1;
5271 				while (IS_WHITESPACE(*argstart)) {
5272 					argstart++;
5273 				}
5274 			}
5275 
5276 			/* Error-check, regardless of whether this is the last interpreter arg or not */
5277 			if (error) {
5278 				goto bad;
5279 			}
5280 			if (imgp->ip_argspace < new_ptr_size) {
5281 				error = E2BIG;
5282 				goto bad;
5283 			}
5284 			imgp->ip_argspace -= new_ptr_size; /* to hold argv[] entry */
5285 			imgp->ip_argc++;
5286 		}
5287 
5288 		if (argv != 0LL) {
5289 			/*
5290 			 * If we are running an interpreter, replace the av[0] that was
5291 			 * passed to execve() with the path name that was
5292 			 * passed to execve() for interpreters which do not use the PATH
5293 			 * to locate their script arguments.
5294 			 */
5295 			error = copyinptr(argv, &arg, ptr_size);
5296 			if (error) {
5297 				goto bad;
5298 			}
5299 			if (arg != 0LL) {
5300 				argv += ptr_size; /* consume without using */
5301 			}
5302 		}
5303 
5304 		if (imgp->ip_interp_sugid_fd != -1) {
5305 			char temp[19]; /* "/dev/fd/" + 10 digits + NUL */
5306 			snprintf(temp, sizeof(temp), "/dev/fd/%d", imgp->ip_interp_sugid_fd);
5307 			error = exec_add_user_string(imgp, CAST_USER_ADDR_T(temp), UIO_SYSSPACE, TRUE);
5308 		} else {
5309 			error = exec_add_user_string(imgp, imgp->ip_user_fname, imgp->ip_seg, TRUE);
5310 		}
5311 
5312 		if (error) {
5313 			goto bad;
5314 		}
5315 		if (imgp->ip_argspace < new_ptr_size) {
5316 			error = E2BIG;
5317 			goto bad;
5318 		}
5319 		imgp->ip_argspace -= new_ptr_size; /* to hold argv[] entry */
5320 		imgp->ip_argc++;
5321 	}
5322 
5323 	while (argv != 0LL) {
5324 		user_addr_t     arg;
5325 
5326 		error = copyinptr(argv, &arg, ptr_size);
5327 		if (error) {
5328 			goto bad;
5329 		}
5330 
5331 		if (arg == 0LL) {
5332 			break;
5333 		}
5334 
5335 		argv += ptr_size;
5336 
5337 		/*
5338 		 * av[n...] = arg[n]
5339 		 */
5340 		error = exec_add_user_string(imgp, arg, imgp->ip_seg, TRUE);
5341 		if (error) {
5342 			goto bad;
5343 		}
5344 		if (imgp->ip_argspace < new_ptr_size) {
5345 			error = E2BIG;
5346 			goto bad;
5347 		}
5348 		imgp->ip_argspace -= new_ptr_size; /* to hold argv[] entry */
5349 		imgp->ip_argc++;
5350 	}
5351 
5352 	/* Save space for argv[] NULL terminator */
5353 	if (imgp->ip_argspace < new_ptr_size) {
5354 		error = E2BIG;
5355 		goto bad;
5356 	}
5357 	imgp->ip_argspace -= new_ptr_size;
5358 
5359 	/* Note where the args ends and env begins. */
5360 	imgp->ip_endargv = imgp->ip_strendp;
5361 	imgp->ip_envc = 0;
5362 
5363 	/* Now, get the environment */
5364 	while (envv != 0LL) {
5365 		user_addr_t     env;
5366 
5367 		error = copyinptr(envv, &env, ptr_size);
5368 		if (error) {
5369 			goto bad;
5370 		}
5371 
5372 		envv += ptr_size;
5373 		if (env == 0LL) {
5374 			break;
5375 		}
5376 		/*
5377 		 * av[n...] = env[n]
5378 		 */
5379 		error = exec_add_user_string(imgp, env, imgp->ip_seg, TRUE);
5380 		if (error) {
5381 			goto bad;
5382 		}
5383 		if (imgp->ip_argspace < new_ptr_size) {
5384 			error = E2BIG;
5385 			goto bad;
5386 		}
5387 		imgp->ip_argspace -= new_ptr_size; /* to hold envv[] entry */
5388 		imgp->ip_envc++;
5389 	}
5390 
5391 	/* Save space for envv[] NULL terminator */
5392 	if (imgp->ip_argspace < new_ptr_size) {
5393 		error = E2BIG;
5394 		goto bad;
5395 	}
5396 	imgp->ip_argspace -= new_ptr_size;
5397 
5398 	/* Align the tail of the combined argv+envv area */
5399 	while (imgp->ip_strspace % new_ptr_size != 0) {
5400 		if (imgp->ip_argspace < 1) {
5401 			error = E2BIG;
5402 			goto bad;
5403 		}
5404 		*imgp->ip_strendp++ = '\0';
5405 		imgp->ip_strspace--;
5406 		imgp->ip_argspace--;
5407 	}
5408 
5409 	/* Note where the envv ends and applev begins. */
5410 	imgp->ip_endenvv = imgp->ip_strendp;
5411 
5412 	/*
5413 	 * From now on, we are no longer charging argument
5414 	 * space to ip_argspace.
5415 	 */
5416 
5417 bad:
5418 	return error;
5419 }
5420 
5421 /*
5422  * Libc has an 8-element array set up for stack guard values.  It only fills
5423  * in one of those entries, and both gcc and llvm seem to use only a single
5424  * 8-byte guard.  Until somebody needs more than an 8-byte guard value, don't
5425  * do the work to construct them.
5426  */
5427 #define GUARD_VALUES 1
5428 #define GUARD_KEY "stack_guard="
5429 
5430 /*
5431  * System malloc needs some entropy when it is initialized.
5432  */
5433 #define ENTROPY_VALUES 2
5434 #define ENTROPY_KEY "malloc_entropy="
5435 
5436 /*
5437  * libplatform needs a random pointer-obfuscation value when it is initialized.
5438  */
5439 #define PTR_MUNGE_VALUES 1
5440 #define PTR_MUNGE_KEY "ptr_munge="
5441 
5442 /*
5443  * System malloc engages nanozone for UIAPP.
5444  */
5445 #define NANO_ENGAGE_KEY "MallocNanoZone=1"
5446 /*
5447  * Used to pass experiment flags up to libmalloc.
5448  */
5449 #define LIBMALLOC_EXPERIMENT_FACTORS_KEY "MallocExperiment="
5450 
5451 #define PFZ_KEY "pfz="
5452 extern user32_addr_t commpage_text32_location;
5453 extern user64_addr_t commpage_text64_location;
5454 
5455 extern uuid_string_t bootsessionuuid_string;
5456 
5457 #define MAIN_STACK_VALUES 4
5458 #define MAIN_STACK_KEY "main_stack="
5459 
5460 #define FSID_KEY "executable_file="
5461 #define DYLD_FSID_KEY "dyld_file="
5462 #define CDHASH_KEY "executable_cdhash="
5463 #define DYLD_FLAGS_KEY "dyld_flags="
5464 #define SUBSYSTEM_ROOT_PATH_KEY "subsystem_root_path="
5465 #define APP_BOOT_SESSION_KEY "executable_boothash="
5466 #if __has_feature(ptrauth_calls)
5467 #define PTRAUTH_DISABLED_FLAG "ptrauth_disabled=1"
5468 #define DYLD_ARM64E_ABI_KEY "arm64e_abi="
5469 #endif /* __has_feature(ptrauth_calls) */
5470 #define MAIN_TH_PORT_KEY "th_port="
5471 
5472 #define FSID_MAX_STRING "0x1234567890abcdef,0x1234567890abcdef"
5473 
5474 #define HEX_STR_LEN 18 // 64-bit hex value "0x0123456701234567"
5475 #define HEX_STR_LEN32 10 // 32-bit hex value "0x01234567"
5476 
5477 #if XNU_TARGET_OS_OSX && _POSIX_SPAWN_FORCE_4K_PAGES && PMAP_CREATE_FORCE_4K_PAGES
5478 #define VM_FORCE_4K_PAGES_KEY "vm_force_4k_pages=1"
5479 #endif /* XNU_TARGET_OS_OSX && _POSIX_SPAWN_FORCE_4K_PAGES && PMAP_CREATE_FORCE_4K_PAGES */
5480 
5481 static int
exec_add_entropy_key(struct image_params * imgp,const char * key,int values,boolean_t embedNUL)5482 exec_add_entropy_key(struct image_params *imgp,
5483     const char *key,
5484     int values,
5485     boolean_t embedNUL)
5486 {
5487 	const int limit = 8;
5488 	uint64_t entropy[limit];
5489 	char str[strlen(key) + (HEX_STR_LEN + 1) * limit + 1];
5490 	if (values > limit) {
5491 		values = limit;
5492 	}
5493 
5494 	read_random(entropy, sizeof(entropy[0]) * values);
5495 
5496 	if (embedNUL) {
5497 		entropy[0] &= ~(0xffull << 8);
5498 	}
5499 
5500 	int len = scnprintf(str, sizeof(str), "%s0x%llx", key, entropy[0]);
5501 	size_t remaining = sizeof(str) - len;
5502 	for (int i = 1; i < values && remaining > 0; ++i) {
5503 		size_t start = sizeof(str) - remaining;
5504 		len = scnprintf(&str[start], remaining, ",0x%llx", entropy[i]);
5505 		remaining -= len;
5506 	}
5507 
5508 	return exec_add_user_string(imgp, CAST_USER_ADDR_T(str), UIO_SYSSPACE, FALSE);
5509 }
5510 
5511 /*
5512  * Build up the contents of the apple[] string vector
5513  */
5514 #if (DEVELOPMENT || DEBUG)
5515 extern uint64_t dyld_flags;
5516 #endif
5517 
5518 #if __has_feature(ptrauth_calls)
5519 static inline bool
is_arm64e_running_as_arm64(const struct image_params * imgp)5520 is_arm64e_running_as_arm64(const struct image_params *imgp)
5521 {
5522 	return (imgp->ip_origcpusubtype & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_ARM64E &&
5523 	       (imgp->ip_flags & IMGPF_NOJOP);
5524 }
5525 #endif /* __has_feature(ptrauth_calls) */
5526 
5527 _Atomic uint64_t libmalloc_experiment_factors = 0;
5528 
5529 static int
exec_add_apple_strings(struct image_params * imgp,const load_result_t * load_result)5530 exec_add_apple_strings(struct image_params *imgp,
5531     const load_result_t *load_result)
5532 {
5533 	int error;
5534 	int img_ptr_size = (imgp->ip_flags & IMGPF_IS_64BIT_ADDR) ? 8 : 4;
5535 	thread_t new_thread;
5536 	ipc_port_t sright;
5537 	uint64_t local_experiment_factors = 0;
5538 
5539 	/* exec_save_path stored the first string */
5540 	imgp->ip_applec = 1;
5541 
5542 	/* adding the pfz string */
5543 	{
5544 		char pfz_string[strlen(PFZ_KEY) + HEX_STR_LEN + 1];
5545 
5546 		if (img_ptr_size == 8) {
5547 			__assert_only size_t ret = snprintf(pfz_string, sizeof(pfz_string), PFZ_KEY "0x%llx", commpage_text64_location);
5548 			assert(ret < sizeof(pfz_string));
5549 		} else {
5550 			snprintf(pfz_string, sizeof(pfz_string), PFZ_KEY "0x%x", commpage_text32_location);
5551 		}
5552 		error = exec_add_user_string(imgp, CAST_USER_ADDR_T(pfz_string), UIO_SYSSPACE, FALSE);
5553 		if (error) {
5554 			printf("Failed to add the pfz string with error %d\n", error);
5555 			goto bad;
5556 		}
5557 		imgp->ip_applec++;
5558 	}
5559 
5560 	/* adding the NANO_ENGAGE_KEY key */
5561 	if (imgp->ip_px_sa) {
5562 		int proc_flags = (((struct _posix_spawnattr *) imgp->ip_px_sa)->psa_flags);
5563 
5564 		if ((proc_flags & _POSIX_SPAWN_NANO_ALLOCATOR) == _POSIX_SPAWN_NANO_ALLOCATOR) {
5565 			const char *nano_string = NANO_ENGAGE_KEY;
5566 			error = exec_add_user_string(imgp, CAST_USER_ADDR_T(nano_string), UIO_SYSSPACE, FALSE);
5567 			if (error) {
5568 				goto bad;
5569 			}
5570 			imgp->ip_applec++;
5571 		}
5572 	}
5573 
5574 	/*
5575 	 * Supply libc with a collection of random values to use when
5576 	 * implementing -fstack-protector.
5577 	 *
5578 	 * (The first random string always contains an embedded NUL so that
5579 	 * __stack_chk_guard also protects against C string vulnerabilities)
5580 	 */
5581 	error = exec_add_entropy_key(imgp, GUARD_KEY, GUARD_VALUES, TRUE);
5582 	if (error) {
5583 		goto bad;
5584 	}
5585 	imgp->ip_applec++;
5586 
5587 	/*
5588 	 * Supply libc with entropy for system malloc.
5589 	 */
5590 	error = exec_add_entropy_key(imgp, ENTROPY_KEY, ENTROPY_VALUES, FALSE);
5591 	if (error) {
5592 		goto bad;
5593 	}
5594 	imgp->ip_applec++;
5595 
5596 	/*
5597 	 * Supply libpthread & libplatform with a random value to use for pointer
5598 	 * obfuscation.
5599 	 */
5600 	error = exec_add_entropy_key(imgp, PTR_MUNGE_KEY, PTR_MUNGE_VALUES, FALSE);
5601 	if (error) {
5602 		goto bad;
5603 	}
5604 	imgp->ip_applec++;
5605 
5606 	/*
5607 	 * Add MAIN_STACK_KEY: Supplies the address and size of the main thread's
5608 	 * stack if it was allocated by the kernel.
5609 	 *
5610 	 * The guard page is not included in this stack size as libpthread
5611 	 * expects to add it back in after receiving this value.
5612 	 */
5613 	if (load_result->unixproc) {
5614 		char stack_string[strlen(MAIN_STACK_KEY) + (HEX_STR_LEN + 1) * MAIN_STACK_VALUES + 1];
5615 		snprintf(stack_string, sizeof(stack_string),
5616 		    MAIN_STACK_KEY "0x%llx,0x%llx,0x%llx,0x%llx",
5617 		    (uint64_t)load_result->user_stack,
5618 		    (uint64_t)load_result->user_stack_size,
5619 		    (uint64_t)load_result->user_stack_alloc,
5620 		    (uint64_t)load_result->user_stack_alloc_size);
5621 		error = exec_add_user_string(imgp, CAST_USER_ADDR_T(stack_string), UIO_SYSSPACE, FALSE);
5622 		if (error) {
5623 			goto bad;
5624 		}
5625 		imgp->ip_applec++;
5626 	}
5627 
5628 	if (imgp->ip_vattr) {
5629 		uint64_t fsid    = vnode_get_va_fsid(imgp->ip_vattr);
5630 		uint64_t fsobjid = imgp->ip_vattr->va_fileid;
5631 
5632 		char fsid_string[strlen(FSID_KEY) + strlen(FSID_MAX_STRING) + 1];
5633 		snprintf(fsid_string, sizeof(fsid_string),
5634 		    FSID_KEY "0x%llx,0x%llx", fsid, fsobjid);
5635 		error = exec_add_user_string(imgp, CAST_USER_ADDR_T(fsid_string), UIO_SYSSPACE, FALSE);
5636 		if (error) {
5637 			goto bad;
5638 		}
5639 		imgp->ip_applec++;
5640 	}
5641 
5642 	if (imgp->ip_dyld_fsid || imgp->ip_dyld_fsobjid) {
5643 		char fsid_string[strlen(DYLD_FSID_KEY) + strlen(FSID_MAX_STRING) + 1];
5644 		snprintf(fsid_string, sizeof(fsid_string),
5645 		    DYLD_FSID_KEY "0x%llx,0x%llx", imgp->ip_dyld_fsid, imgp->ip_dyld_fsobjid);
5646 		error = exec_add_user_string(imgp, CAST_USER_ADDR_T(fsid_string), UIO_SYSSPACE, FALSE);
5647 		if (error) {
5648 			goto bad;
5649 		}
5650 		imgp->ip_applec++;
5651 	}
5652 
5653 	uint8_t cdhash[SHA1_RESULTLEN];
5654 	int cdhash_errror = ubc_cs_getcdhash(imgp->ip_vp, imgp->ip_arch_offset, cdhash);
5655 	if (cdhash_errror == 0) {
5656 		char hash_string[strlen(CDHASH_KEY) + 2 * SHA1_RESULTLEN + 1];
5657 		strncpy(hash_string, CDHASH_KEY, sizeof(hash_string));
5658 		char *p = hash_string + sizeof(CDHASH_KEY) - 1;
5659 		for (int i = 0; i < SHA1_RESULTLEN; i++) {
5660 			snprintf(p, 3, "%02x", (int) cdhash[i]);
5661 			p += 2;
5662 		}
5663 		error = exec_add_user_string(imgp, CAST_USER_ADDR_T(hash_string), UIO_SYSSPACE, FALSE);
5664 		if (error) {
5665 			goto bad;
5666 		}
5667 		imgp->ip_applec++;
5668 
5669 		/* hash together cd-hash and boot-session-uuid */
5670 		uint8_t sha_digest[SHA256_DIGEST_LENGTH];
5671 		SHA256_CTX sha_ctx;
5672 		SHA256_Init(&sha_ctx);
5673 		SHA256_Update(&sha_ctx, bootsessionuuid_string, sizeof(bootsessionuuid_string));
5674 		SHA256_Update(&sha_ctx, cdhash, sizeof(cdhash));
5675 		SHA256_Final(sha_digest, &sha_ctx);
5676 		char app_boot_string[strlen(APP_BOOT_SESSION_KEY) + 2 * SHA1_RESULTLEN + 1];
5677 		strncpy(app_boot_string, APP_BOOT_SESSION_KEY, sizeof(app_boot_string));
5678 		char *s = app_boot_string + sizeof(APP_BOOT_SESSION_KEY) - 1;
5679 		for (int i = 0; i < SHA1_RESULTLEN; i++) {
5680 			snprintf(s, 3, "%02x", (int) sha_digest[i]);
5681 			s += 2;
5682 		}
5683 		error = exec_add_user_string(imgp, CAST_USER_ADDR_T(app_boot_string), UIO_SYSSPACE, FALSE);
5684 		if (error) {
5685 			goto bad;
5686 		}
5687 		imgp->ip_applec++;
5688 	}
5689 #if (DEVELOPMENT || DEBUG)
5690 	if (dyld_flags) {
5691 		char dyld_flags_string[strlen(DYLD_FLAGS_KEY) + HEX_STR_LEN + 1];
5692 		snprintf(dyld_flags_string, sizeof(dyld_flags_string), DYLD_FLAGS_KEY "0x%llx", dyld_flags);
5693 		error = exec_add_user_string(imgp, CAST_USER_ADDR_T(dyld_flags_string), UIO_SYSSPACE, FALSE);
5694 		if (error) {
5695 			goto bad;
5696 		}
5697 		imgp->ip_applec++;
5698 	}
5699 #endif
5700 	if (imgp->ip_subsystem_root_path) {
5701 		size_t buffer_len = MAXPATHLEN + strlen(SUBSYSTEM_ROOT_PATH_KEY);
5702 		char subsystem_root_path_string[buffer_len];
5703 		int required_len = snprintf(subsystem_root_path_string, buffer_len, SUBSYSTEM_ROOT_PATH_KEY "%s", imgp->ip_subsystem_root_path);
5704 
5705 		if (((size_t)required_len >= buffer_len) || (required_len < 0)) {
5706 			error = ENAMETOOLONG;
5707 			goto bad;
5708 		}
5709 
5710 		error = exec_add_user_string(imgp, CAST_USER_ADDR_T(subsystem_root_path_string), UIO_SYSSPACE, FALSE);
5711 		if (error) {
5712 			goto bad;
5713 		}
5714 
5715 		imgp->ip_applec++;
5716 	}
5717 #if __has_feature(ptrauth_calls)
5718 	if (is_arm64e_running_as_arm64(imgp)) {
5719 		error = exec_add_user_string(imgp, CAST_USER_ADDR_T(PTRAUTH_DISABLED_FLAG), UIO_SYSSPACE, FALSE);
5720 		if (error) {
5721 			goto bad;
5722 		}
5723 
5724 		imgp->ip_applec++;
5725 	}
5726 #endif /* __has_feature(ptrauth_calls) */
5727 
5728 
5729 #if __has_feature(ptrauth_calls) && defined(XNU_TARGET_OS_OSX)
5730 	{
5731 		char dyld_abi_string[strlen(DYLD_ARM64E_ABI_KEY) + 8];
5732 		strlcpy(dyld_abi_string, DYLD_ARM64E_ABI_KEY, sizeof(dyld_abi_string));
5733 		bool allowAll = bootarg_arm64e_preview_abi;
5734 		strlcat(dyld_abi_string, (allowAll ? "all" : "os"), sizeof(dyld_abi_string));
5735 		error = exec_add_user_string(imgp, CAST_USER_ADDR_T(dyld_abi_string), UIO_SYSSPACE, FALSE);
5736 		if (error) {
5737 			goto bad;
5738 		}
5739 
5740 		imgp->ip_applec++;
5741 	}
5742 #endif
5743 	/*
5744 	 * Add main thread mach port name
5745 	 * +1 uref on main thread port, this ref will be extracted by libpthread in __pthread_init
5746 	 * and consumed in _bsdthread_terminate. Leaking the main thread port name if not linked
5747 	 * against libpthread.
5748 	 */
5749 	if ((new_thread = imgp->ip_new_thread) != THREAD_NULL) {
5750 		thread_reference(new_thread);
5751 		sright = convert_thread_to_port_pinned(new_thread);
5752 		task_t new_task = get_threadtask(new_thread);
5753 		mach_port_name_t name = ipc_port_copyout_send(sright, get_task_ipcspace(new_task));
5754 		char port_name_hex_str[strlen(MAIN_TH_PORT_KEY) + HEX_STR_LEN32 + 1];
5755 		snprintf(port_name_hex_str, sizeof(port_name_hex_str), MAIN_TH_PORT_KEY "0x%x", name);
5756 
5757 		error = exec_add_user_string(imgp, CAST_USER_ADDR_T(port_name_hex_str), UIO_SYSSPACE, FALSE);
5758 		if (error) {
5759 			goto bad;
5760 		}
5761 		imgp->ip_applec++;
5762 	}
5763 
5764 #if XNU_TARGET_OS_OSX && _POSIX_SPAWN_FORCE_4K_PAGES && PMAP_CREATE_FORCE_4K_PAGES
5765 	if (imgp->ip_px_sa != NULL) {
5766 		struct _posix_spawnattr* psa = (struct _posix_spawnattr *) imgp->ip_px_sa;
5767 		if (psa->psa_flags & _POSIX_SPAWN_FORCE_4K_PAGES) {
5768 			const char *vm_force_4k_string = VM_FORCE_4K_PAGES_KEY;
5769 			error = exec_add_user_string(imgp, CAST_USER_ADDR_T(vm_force_4k_string), UIO_SYSSPACE, FALSE);
5770 			if (error) {
5771 				goto bad;
5772 			}
5773 			imgp->ip_applec++;
5774 		}
5775 	}
5776 #endif /* XNU_TARGET_OS_OSX && _POSIX_SPAWN_FORCE_4K_PAGES && PMAP_CREATE_FORCE_4K_PAGES */
5777 
5778 	/* adding the libmalloc experiment string */
5779 	local_experiment_factors = os_atomic_load_wide(&libmalloc_experiment_factors, relaxed);
5780 	if (__improbable(local_experiment_factors != 0)) {
5781 		char libmalloc_experiment_factors_string[strlen(LIBMALLOC_EXPERIMENT_FACTORS_KEY) + HEX_STR_LEN + 1];
5782 
5783 		snprintf(
5784 			libmalloc_experiment_factors_string,
5785 			sizeof(libmalloc_experiment_factors_string),
5786 			LIBMALLOC_EXPERIMENT_FACTORS_KEY "0x%llx",
5787 			local_experiment_factors);
5788 		error = exec_add_user_string(
5789 			imgp,
5790 			CAST_USER_ADDR_T(libmalloc_experiment_factors_string),
5791 			UIO_SYSSPACE,
5792 			FALSE);
5793 		if (error) {
5794 			printf("Failed to add the libmalloc experiment factors string with error %d\n", error);
5795 			goto bad;
5796 		}
5797 		imgp->ip_applec++;
5798 	}
5799 
5800 	/* Align the tail of the combined applev area */
5801 	while (imgp->ip_strspace % img_ptr_size != 0) {
5802 		*imgp->ip_strendp++ = '\0';
5803 		imgp->ip_strspace--;
5804 	}
5805 
5806 bad:
5807 	return error;
5808 }
5809 
5810 /*
5811  * exec_check_permissions
5812  *
5813  * Description:	Verify that the file that is being attempted to be executed
5814  *		is in fact allowed to be executed based on it POSIX file
5815  *		permissions and other access control criteria
5816  *
5817  * Parameters:	struct image_params *	the image parameter block
5818  *
5819  * Returns:	0			Success
5820  *		EACCES			Permission denied
5821  *		ENOEXEC			Executable file format error
5822  *		ETXTBSY			Text file busy [misuse of error code]
5823  *	vnode_getattr:???
5824  *	vnode_authorize:???
5825  */
5826 static int
exec_check_permissions(struct image_params * imgp)5827 exec_check_permissions(struct image_params *imgp)
5828 {
5829 	struct vnode *vp = imgp->ip_vp;
5830 	struct vnode_attr *vap = imgp->ip_vattr;
5831 	proc_t p = vfs_context_proc(imgp->ip_vfs_context);
5832 	int error;
5833 	kauth_action_t action;
5834 
5835 	/* Only allow execution of regular files */
5836 	if (!vnode_isreg(vp)) {
5837 		return EACCES;
5838 	}
5839 
5840 	/* Get the file attributes that we will be using here and elsewhere */
5841 	VATTR_INIT(vap);
5842 	VATTR_WANTED(vap, va_uid);
5843 	VATTR_WANTED(vap, va_gid);
5844 	VATTR_WANTED(vap, va_mode);
5845 	VATTR_WANTED(vap, va_fsid);
5846 	VATTR_WANTED(vap, va_fsid64);
5847 	VATTR_WANTED(vap, va_fileid);
5848 	VATTR_WANTED(vap, va_data_size);
5849 	if ((error = vnode_getattr(vp, vap, imgp->ip_vfs_context)) != 0) {
5850 		return error;
5851 	}
5852 
5853 	/*
5854 	 * Ensure that at least one execute bit is on - otherwise root
5855 	 * will always succeed, and we don't want to happen unless the
5856 	 * file really is executable.
5857 	 */
5858 	if (!vfs_authopaque(vnode_mount(vp)) && ((vap->va_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0)) {
5859 		return EACCES;
5860 	}
5861 
5862 	/* Disallow zero length files */
5863 	if (vap->va_data_size == 0) {
5864 		return ENOEXEC;
5865 	}
5866 
5867 	imgp->ip_arch_offset = (user_size_t)0;
5868 #if __LP64__
5869 	imgp->ip_arch_size = vap->va_data_size;
5870 #else
5871 	if (vap->va_data_size > UINT32_MAX) {
5872 		return ENOEXEC;
5873 	}
5874 	imgp->ip_arch_size = (user_size_t)vap->va_data_size;
5875 #endif
5876 
5877 	/* Disable setuid-ness for traced programs or if MNT_NOSUID */
5878 	if ((vp->v_mount->mnt_flag & MNT_NOSUID) || (p->p_lflag & P_LTRACED)) {
5879 		vap->va_mode &= ~(VSUID | VSGID);
5880 	}
5881 
5882 	/*
5883 	 * Disable _POSIX_SPAWN_ALLOW_DATA_EXEC and _POSIX_SPAWN_DISABLE_ASLR
5884 	 * flags for setuid/setgid binaries.
5885 	 */
5886 	if (vap->va_mode & (VSUID | VSGID)) {
5887 		imgp->ip_flags &= ~(IMGPF_ALLOW_DATA_EXEC | IMGPF_DISABLE_ASLR);
5888 	}
5889 
5890 #if CONFIG_MACF
5891 	error = mac_vnode_check_exec(imgp->ip_vfs_context, vp, imgp);
5892 	if (error) {
5893 		return error;
5894 	}
5895 #endif
5896 
5897 	/* Check for execute permission */
5898 	action = KAUTH_VNODE_EXECUTE;
5899 	/* Traced images must also be readable */
5900 	if (p->p_lflag & P_LTRACED) {
5901 		action |= KAUTH_VNODE_READ_DATA;
5902 	}
5903 	if ((error = vnode_authorize(vp, NULL, action, imgp->ip_vfs_context)) != 0) {
5904 		return error;
5905 	}
5906 
5907 #if 0
5908 	/* Don't let it run if anyone had it open for writing */
5909 	vnode_lock(vp);
5910 	if (vp->v_writecount) {
5911 		panic("going to return ETXTBSY %x", vp);
5912 		vnode_unlock(vp);
5913 		return ETXTBSY;
5914 	}
5915 	vnode_unlock(vp);
5916 #endif
5917 
5918 	/* XXX May want to indicate to underlying FS that vnode is open */
5919 
5920 	return error;
5921 }
5922 
5923 
5924 /*
5925  * exec_handle_sugid
5926  *
5927  * Initially clear the P_SUGID in the process flags; if an SUGID process is
5928  * exec'ing a non-SUGID image, then  this is the point of no return.
5929  *
5930  * If the image being activated is SUGID, then replace the credential with a
5931  * copy, disable tracing (unless the tracing process is root), reset the
5932  * mach task port to revoke it, set the P_SUGID bit,
5933  *
5934  * If the saved user and group ID will be changing, then make sure it happens
5935  * to a new credential, rather than a shared one.
5936  *
5937  * Set the security token (this is probably obsolete, given that the token
5938  * should not technically be separate from the credential itself).
5939  *
5940  * Parameters:	struct image_params *	the image parameter block
5941  *
5942  * Returns:	void			No failure indication
5943  *
5944  * Implicit returns:
5945  *		<process credential>	Potentially modified/replaced
5946  *		<task port>		Potentially revoked
5947  *		<process flags>		P_SUGID bit potentially modified
5948  *		<security token>	Potentially modified
5949  */
5950 __attribute__((noinline))
5951 static int
exec_handle_sugid(struct image_params * imgp)5952 exec_handle_sugid(struct image_params *imgp)
5953 {
5954 	proc_t                  p = vfs_context_proc(imgp->ip_vfs_context);
5955 	kauth_cred_t            cred = vfs_context_ucred(imgp->ip_vfs_context);
5956 	int                     i;
5957 	int                     leave_sugid_clear = 0;
5958 	int                     mac_reset_ipc = 0;
5959 	int                     error = 0;
5960 	task_t                  task = NULL;
5961 #if CONFIG_MACF
5962 	int                     mac_transition, disjoint_cred = 0;
5963 	int             label_update_return = 0;
5964 
5965 	/*
5966 	 * Determine whether a call to update the MAC label will result in the
5967 	 * credential changing.
5968 	 *
5969 	 * Note:	MAC policies which do not actually end up modifying
5970 	 *		the label subsequently are strongly encouraged to
5971 	 *		return 0 for this check, since a non-zero answer will
5972 	 *		slow down the exec fast path for normal binaries.
5973 	 */
5974 	mac_transition = mac_cred_check_label_update_execve(
5975 		imgp->ip_vfs_context,
5976 		imgp->ip_vp,
5977 		imgp->ip_arch_offset,
5978 		imgp->ip_scriptvp,
5979 		imgp->ip_scriptlabelp,
5980 		imgp->ip_execlabelp,
5981 		p,
5982 		&imgp->ip_px_smpx);
5983 #endif
5984 
5985 	OSBitAndAtomic(~((uint32_t)P_SUGID), &p->p_flag);
5986 
5987 	/*
5988 	 * Order of the following is important; group checks must go last,
5989 	 * as we use the success of the 'ismember' check combined with the
5990 	 * failure of the explicit match to indicate that we will be setting
5991 	 * the egid of the process even though the new process did not
5992 	 * require VSUID/VSGID bits in order for it to set the new group as
5993 	 * its egid.
5994 	 *
5995 	 * Note:	Technically, by this we are implying a call to
5996 	 *		setegid() in the new process, rather than implying
5997 	 *		it used its VSGID bit to set the effective group,
5998 	 *		even though there is no code in that process to make
5999 	 *		such a call.
6000 	 */
6001 	if (((imgp->ip_origvattr->va_mode & VSUID) != 0 &&
6002 	    kauth_cred_getuid(cred) != imgp->ip_origvattr->va_uid) ||
6003 	    ((imgp->ip_origvattr->va_mode & VSGID) != 0 &&
6004 	    ((kauth_cred_ismember_gid(cred, imgp->ip_origvattr->va_gid, &leave_sugid_clear) || !leave_sugid_clear) ||
6005 	    (kauth_cred_getgid(cred) != imgp->ip_origvattr->va_gid))) ||
6006 	    (imgp->ip_sc_port != NULL)) {
6007 #if CONFIG_MACF
6008 /* label for MAC transition and neither VSUID nor VSGID */
6009 handle_mac_transition:
6010 #endif
6011 
6012 #if CONFIG_SETUID
6013 		/*
6014 		 * Replace the credential with a copy of itself if euid or
6015 		 * egid change.
6016 		 *
6017 		 * Note:	setuid binaries will automatically opt out of
6018 		 *		group resolver participation as a side effect
6019 		 *		of this operation.  This is an intentional
6020 		 *		part of the security model, which requires a
6021 		 *		participating credential be established by
6022 		 *		escalating privilege, setting up all other
6023 		 *		aspects of the credential including whether
6024 		 *		or not to participate in external group
6025 		 *		membership resolution, then dropping their
6026 		 *		effective privilege to that of the desired
6027 		 *		final credential state.
6028 		 *
6029 		 * Modifications to p_ucred must be guarded using the
6030 		 * proc's ucred lock. This prevents others from accessing
6031 		 * a garbage credential.
6032 		 */
6033 
6034 		if (imgp->ip_sc_port != NULL) {
6035 			extern int suid_cred_verify(ipc_port_t, vnode_t, uint32_t *);
6036 			int ret = -1;
6037 			uid_t uid = UINT32_MAX;
6038 
6039 			/*
6040 			 * Check that the vnodes match. If a script is being
6041 			 * executed check the script's vnode rather than the
6042 			 * interpreter's.
6043 			 */
6044 			struct vnode *vp = imgp->ip_scriptvp != NULL ? imgp->ip_scriptvp : imgp->ip_vp;
6045 
6046 			ret = suid_cred_verify(imgp->ip_sc_port, vp, &uid);
6047 			if (ret == 0) {
6048 				proc_update_label(p, false, ^kauth_cred_t (kauth_cred_t my_cred) {
6049 					return kauth_cred_setresuid(my_cred,
6050 					KAUTH_UID_NONE,
6051 					uid,
6052 					uid,
6053 					KAUTH_UID_NONE);
6054 				});
6055 			} else {
6056 				error = EPERM;
6057 			}
6058 		}
6059 
6060 		if (imgp->ip_origvattr->va_mode & VSUID) {
6061 			proc_update_label(p, false, ^kauth_cred_t (kauth_cred_t my_cred) {
6062 				return kauth_cred_setresuid(my_cred,
6063 				KAUTH_UID_NONE,
6064 				imgp->ip_origvattr->va_uid,
6065 				imgp->ip_origvattr->va_uid,
6066 				KAUTH_UID_NONE);
6067 			});
6068 		}
6069 
6070 		if (imgp->ip_origvattr->va_mode & VSGID) {
6071 			proc_update_label(p, false, ^kauth_cred_t (kauth_cred_t my_cred) {
6072 				return kauth_cred_setresgid(my_cred,
6073 				KAUTH_GID_NONE,
6074 				imgp->ip_origvattr->va_gid,
6075 				imgp->ip_origvattr->va_gid);
6076 			});
6077 		}
6078 #endif /* CONFIG_SETUID */
6079 
6080 #if CONFIG_MACF
6081 		/*
6082 		 * If a policy has indicated that it will transition the label,
6083 		 * before making the call into the MAC policies, get a new
6084 		 * duplicate credential, so they can modify it without
6085 		 * modifying any others sharing it.
6086 		 */
6087 		if (mac_transition) {
6088 			/*
6089 			 * This hook may generate upcalls that require
6090 			 * importance donation from the kernel.
6091 			 * (23925818)
6092 			 */
6093 			thread_t thread = current_thread();
6094 			thread_enable_send_importance(thread, TRUE);
6095 			kauth_proc_label_update_execve(p,
6096 			    imgp->ip_vfs_context,
6097 			    imgp->ip_vp,
6098 			    imgp->ip_arch_offset,
6099 			    imgp->ip_scriptvp,
6100 			    imgp->ip_scriptlabelp,
6101 			    imgp->ip_execlabelp,
6102 			    &imgp->ip_csflags,
6103 			    &imgp->ip_px_smpx,
6104 			    &disjoint_cred,                     /* will be non zero if disjoint */
6105 			    &label_update_return);
6106 			thread_enable_send_importance(thread, FALSE);
6107 
6108 			if (disjoint_cred) {
6109 				/*
6110 				 * If updating the MAC label resulted in a
6111 				 * disjoint credential, flag that we need to
6112 				 * set the P_SUGID bit.  This protects
6113 				 * against debuggers being attached by an
6114 				 * insufficiently privileged process onto the
6115 				 * result of a transition to a more privileged
6116 				 * credential.
6117 				 */
6118 				leave_sugid_clear = 0;
6119 			}
6120 
6121 			imgp->ip_mac_return = label_update_return;
6122 		}
6123 
6124 		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);
6125 
6126 #endif  /* CONFIG_MACF */
6127 
6128 		/*
6129 		 * If 'leave_sugid_clear' is non-zero, then we passed the
6130 		 * VSUID and MACF checks, and successfully determined that
6131 		 * the previous cred was a member of the VSGID group, but
6132 		 * that it was not the default at the time of the execve,
6133 		 * and that the post-labelling credential was not disjoint.
6134 		 * So we don't set the P_SUGID or reset mach ports and fds
6135 		 * on the basis of simply running this code.
6136 		 */
6137 		if (mac_reset_ipc || !leave_sugid_clear) {
6138 			/*
6139 			 * Have mach reset the task and thread ports.
6140 			 * We don't want anyone who had the ports before
6141 			 * a setuid exec to be able to access/control the
6142 			 * task/thread after.
6143 			 */
6144 			ipc_task_reset((imgp->ip_new_thread != NULL) ?
6145 			    get_threadtask(imgp->ip_new_thread) : p->task);
6146 			ipc_thread_reset((imgp->ip_new_thread != NULL) ?
6147 			    imgp->ip_new_thread : current_thread());
6148 		}
6149 
6150 		if (!leave_sugid_clear) {
6151 			/*
6152 			 * Flag the process as setuid.
6153 			 */
6154 			OSBitOrAtomic(P_SUGID, &p->p_flag);
6155 
6156 			/*
6157 			 * Radar 2261856; setuid security hole fix
6158 			 * XXX For setuid processes, attempt to ensure that
6159 			 * stdin, stdout, and stderr are already allocated.
6160 			 * We do not want userland to accidentally allocate
6161 			 * descriptors in this range which has implied meaning
6162 			 * to libc.
6163 			 */
6164 			for (i = 0; i < 3; i++) {
6165 				if (fp_get_noref_locked(p, i) != NULL) {
6166 					continue;
6167 				}
6168 
6169 				/*
6170 				 * Do the kernel equivalent of
6171 				 *
6172 				 *      if i == 0
6173 				 *              (void) open("/dev/null", O_RDONLY);
6174 				 *      else
6175 				 *              (void) open("/dev/null", O_WRONLY);
6176 				 */
6177 
6178 				struct fileproc *fp;
6179 				int indx;
6180 				int flag;
6181 				struct nameidata *ndp = NULL;
6182 
6183 				if (i == 0) {
6184 					flag = FREAD;
6185 				} else {
6186 					flag = FWRITE;
6187 				}
6188 
6189 				if ((error = falloc(p,
6190 				    &fp, &indx, imgp->ip_vfs_context)) != 0) {
6191 					continue;
6192 				}
6193 
6194 				ndp = kalloc_type(struct nameidata,
6195 				    Z_WAITOK | Z_ZERO | Z_NOFAIL);
6196 
6197 				NDINIT(ndp, LOOKUP, OP_OPEN, FOLLOW, UIO_SYSSPACE,
6198 				    CAST_USER_ADDR_T("/dev/null"),
6199 				    imgp->ip_vfs_context);
6200 
6201 				if ((error = vn_open(ndp, flag, 0)) != 0) {
6202 					fp_free(p, indx, fp);
6203 					kfree_type(struct nameidata, ndp);
6204 					break;
6205 				}
6206 
6207 				struct fileglob *fg = fp->fp_glob;
6208 
6209 				fg->fg_flag = flag;
6210 				fg->fg_ops = &vnops;
6211 				fp_set_data(fp, ndp->ni_vp);
6212 
6213 				vnode_put(ndp->ni_vp);
6214 
6215 				proc_fdlock(p);
6216 				procfdtbl_releasefd(p, indx, NULL);
6217 				fp_drop(p, indx, fp, 1);
6218 				proc_fdunlock(p);
6219 
6220 				kfree_type(struct nameidata, ndp);
6221 			}
6222 		}
6223 	}
6224 #if CONFIG_MACF
6225 	else {
6226 		/*
6227 		 * We are here because we were told that the MAC label will
6228 		 * be transitioned, and the binary is not VSUID or VSGID; to
6229 		 * deal with this case, we could either duplicate a lot of
6230 		 * code, or we can indicate we want to default the P_SUGID
6231 		 * bit clear and jump back up.
6232 		 */
6233 		if (mac_transition) {
6234 			leave_sugid_clear = 1;
6235 			goto handle_mac_transition;
6236 		}
6237 	}
6238 
6239 #endif  /* CONFIG_MACF */
6240 
6241 	/*
6242 	 * Implement the semantic where the effective user and group become
6243 	 * the saved user and group in exec'ed programs.
6244 	 *
6245 	 * Modifications to p_ucred must be guarded using the
6246 	 * proc's ucred lock. This prevents others from accessing
6247 	 * a garbage credential.
6248 	 */
6249 	proc_update_label(p, false, ^kauth_cred_t (kauth_cred_t my_cred) {
6250 		return kauth_cred_setsvuidgid(my_cred,
6251 		kauth_cred_getuid(my_cred),
6252 		kauth_cred_getgid(my_cred));
6253 	});
6254 
6255 	if (imgp->ip_new_thread != NULL) {
6256 		task = get_threadtask(imgp->ip_new_thread);
6257 	} else {
6258 		task = p->task;
6259 	}
6260 
6261 	/* Update the process' identity version and set the security token */
6262 	proc_setpidversion(p, OSIncrementAtomic(&nextpidversion));
6263 	task_set_uniqueid(task);
6264 	set_security_token_task_internal(p, task);
6265 
6266 	return error;
6267 }
6268 
6269 
6270 /*
6271  * create_unix_stack
6272  *
6273  * Description:	Set the user stack address for the process to the provided
6274  *		address.  If a custom stack was not set as a result of the
6275  *		load process (i.e. as specified by the image file for the
6276  *		executable), then allocate the stack in the provided map and
6277  *		set up appropriate guard pages for enforcing administrative
6278  *		limits on stack growth, if they end up being needed.
6279  *
6280  * Parameters:	p			Process to set stack on
6281  *		load_result		Information from mach-o load commands
6282  *		map			Address map in which to allocate the new stack
6283  *
6284  * Returns:	KERN_SUCCESS		Stack successfully created
6285  *		!KERN_SUCCESS		Mach failure code
6286  */
6287 __attribute__((noinline))
6288 static kern_return_t
create_unix_stack(vm_map_t map,load_result_t * load_result,proc_t p)6289 create_unix_stack(vm_map_t map, load_result_t* load_result,
6290     proc_t p)
6291 {
6292 	mach_vm_size_t          size, prot_size;
6293 	mach_vm_offset_t        addr, prot_addr;
6294 	kern_return_t           kr;
6295 
6296 	mach_vm_address_t       user_stack = load_result->user_stack;
6297 
6298 	proc_lock(p);
6299 	p->user_stack = (uintptr_t)user_stack;
6300 	if (load_result->custom_stack) {
6301 		p->p_lflag |= P_LCUSTOM_STACK;
6302 	}
6303 	proc_unlock(p);
6304 	if (vm_map_page_shift(map) < (int)PAGE_SHIFT) {
6305 		DEBUG4K_LOAD("map %p user_stack 0x%llx custom %d user_stack_alloc_size 0x%llx\n", map, user_stack, load_result->custom_stack, load_result->user_stack_alloc_size);
6306 	}
6307 
6308 	if (load_result->user_stack_alloc_size > 0) {
6309 		/*
6310 		 * Allocate enough space for the maximum stack size we
6311 		 * will ever authorize and an extra page to act as
6312 		 * a guard page for stack overflows. For default stacks,
6313 		 * vm_initial_limit_stack takes care of the extra guard page.
6314 		 * Otherwise we must allocate it ourselves.
6315 		 */
6316 		if (mach_vm_round_page_overflow(load_result->user_stack_alloc_size, &size)) {
6317 			return KERN_INVALID_ARGUMENT;
6318 		}
6319 		addr = vm_map_trunc_page(load_result->user_stack - size,
6320 		    vm_map_page_mask(map));
6321 		kr = mach_vm_allocate_kernel(map, &addr, size,
6322 		    VM_FLAGS_FIXED, VM_MEMORY_STACK);
6323 		if (kr != KERN_SUCCESS) {
6324 			// Can't allocate at default location, try anywhere
6325 			addr = 0;
6326 			kr = mach_vm_allocate_kernel(map, &addr, size,
6327 			    VM_FLAGS_ANYWHERE, VM_MEMORY_STACK);
6328 			if (kr != KERN_SUCCESS) {
6329 				return kr;
6330 			}
6331 
6332 			user_stack = addr + size;
6333 			load_result->user_stack = (user_addr_t)user_stack;
6334 
6335 			proc_lock(p);
6336 			p->user_stack = (uintptr_t)user_stack;
6337 			proc_unlock(p);
6338 		}
6339 
6340 		load_result->user_stack_alloc = (user_addr_t)addr;
6341 
6342 		/*
6343 		 * And prevent access to what's above the current stack
6344 		 * size limit for this process.
6345 		 */
6346 		if (load_result->user_stack_size == 0) {
6347 			load_result->user_stack_size = proc_limitgetcur(p, RLIMIT_STACK);
6348 			prot_size = vm_map_trunc_page(size - load_result->user_stack_size, vm_map_page_mask(map));
6349 		} else {
6350 			prot_size = PAGE_SIZE;
6351 		}
6352 
6353 		prot_addr = addr;
6354 		kr = mach_vm_protect(map,
6355 		    prot_addr,
6356 		    prot_size,
6357 		    FALSE,
6358 		    VM_PROT_NONE);
6359 		if (kr != KERN_SUCCESS) {
6360 			(void)mach_vm_deallocate(map, addr, size);
6361 			return kr;
6362 		}
6363 	}
6364 
6365 	return KERN_SUCCESS;
6366 }
6367 
6368 #include <sys/reboot.h>
6369 
6370 /*
6371  * load_init_program_at_path
6372  *
6373  * Description:	Load the "init" program; in most cases, this will be "launchd"
6374  *
6375  * Parameters:	p			Process to call execve() to create
6376  *					the "init" program
6377  *		scratch_addr		Page in p, scratch space
6378  *		path			NULL terminated path
6379  *
6380  * Returns:	KERN_SUCCESS		Success
6381  *		!KERN_SUCCESS           See execve/mac_execve for error codes
6382  *
6383  * Notes:	The process that is passed in is the first manufactured
6384  *		process on the system, and gets here via bsd_ast() firing
6385  *		for the first time.  This is done to ensure that bsd_init()
6386  *		has run to completion.
6387  *
6388  *		The address map of the first manufactured process matches the
6389  *		word width of the kernel. Once the self-exec completes, the
6390  *		initproc might be different.
6391  */
6392 static int
load_init_program_at_path(proc_t p,user_addr_t scratch_addr,const char * path)6393 load_init_program_at_path(proc_t p, user_addr_t scratch_addr, const char* path)
6394 {
6395 	int retval[2];
6396 	int error;
6397 	struct execve_args init_exec_args;
6398 	user_addr_t argv0 = USER_ADDR_NULL, argv1 = USER_ADDR_NULL;
6399 
6400 	/*
6401 	 * Validate inputs and pre-conditions
6402 	 */
6403 	assert(p);
6404 	assert(scratch_addr);
6405 	assert(path);
6406 
6407 	/*
6408 	 * Copy out program name.
6409 	 */
6410 	size_t path_length = strlen(path) + 1;
6411 	argv0 = scratch_addr;
6412 	error = copyout(path, argv0, path_length);
6413 	if (error) {
6414 		return error;
6415 	}
6416 
6417 	scratch_addr = USER_ADDR_ALIGN(scratch_addr + path_length, sizeof(user_addr_t));
6418 
6419 	/*
6420 	 * Put out first (and only) argument, similarly.
6421 	 * Assumes everything fits in a page as allocated above.
6422 	 */
6423 	if (boothowto & RB_SINGLE) {
6424 		const char *init_args = "-s";
6425 		size_t init_args_length = strlen(init_args) + 1;
6426 
6427 		argv1 = scratch_addr;
6428 		error = copyout(init_args, argv1, init_args_length);
6429 		if (error) {
6430 			return error;
6431 		}
6432 
6433 		scratch_addr = USER_ADDR_ALIGN(scratch_addr + init_args_length, sizeof(user_addr_t));
6434 	}
6435 
6436 	if (proc_is64bit(p)) {
6437 		user64_addr_t argv64bit[3] = {};
6438 
6439 		argv64bit[0] = argv0;
6440 		argv64bit[1] = argv1;
6441 		argv64bit[2] = USER_ADDR_NULL;
6442 
6443 		error = copyout(argv64bit, scratch_addr, sizeof(argv64bit));
6444 		if (error) {
6445 			return error;
6446 		}
6447 	} else {
6448 		user32_addr_t argv32bit[3] = {};
6449 
6450 		argv32bit[0] = (user32_addr_t)argv0;
6451 		argv32bit[1] = (user32_addr_t)argv1;
6452 		argv32bit[2] = USER_ADDR_NULL;
6453 
6454 		error = copyout(argv32bit, scratch_addr, sizeof(argv32bit));
6455 		if (error) {
6456 			return error;
6457 		}
6458 	}
6459 
6460 	/*
6461 	 * Set up argument block for fake call to execve.
6462 	 */
6463 	init_exec_args.fname = argv0;
6464 	init_exec_args.argp = scratch_addr;
6465 	init_exec_args.envp = USER_ADDR_NULL;
6466 
6467 	/*
6468 	 * So that init task is set with uid,gid 0 token
6469 	 */
6470 	set_security_token(p);
6471 
6472 	return execve(p, &init_exec_args, retval);
6473 }
6474 
6475 static const char * init_programs[] = {
6476 #if DEBUG
6477 	"/usr/appleinternal/sbin/launchd.debug",
6478 #endif
6479 #if DEVELOPMENT || DEBUG
6480 	"/usr/appleinternal/sbin/launchd.development",
6481 #endif
6482 	"/sbin/launchd",
6483 };
6484 
6485 /*
6486  * load_init_program
6487  *
6488  * Description:	Load the "init" program; in most cases, this will be "launchd"
6489  *
6490  * Parameters:	p			Process to call execve() to create
6491  *					the "init" program
6492  *
6493  * Returns:	(void)
6494  *
6495  * Notes:	The process that is passed in is the first manufactured
6496  *		process on the system, and gets here via bsd_ast() firing
6497  *		for the first time.  This is done to ensure that bsd_init()
6498  *		has run to completion.
6499  *
6500  *		In DEBUG & DEVELOPMENT builds, the launchdsuffix boot-arg
6501  *		may be used to select a specific launchd executable. As with
6502  *		the kcsuffix boot-arg, setting launchdsuffix to "" or "release"
6503  *		will force /sbin/launchd to be selected.
6504  *
6505  *              Search order by build:
6506  *
6507  * DEBUG	DEVELOPMENT	RELEASE		PATH
6508  * ----------------------------------------------------------------------------------
6509  * 1		1		NA		/usr/appleinternal/sbin/launchd.$LAUNCHDSUFFIX
6510  * 2		NA		NA		/usr/appleinternal/sbin/launchd.debug
6511  * 3		2		NA		/usr/appleinternal/sbin/launchd.development
6512  * 4		3		1		/sbin/launchd
6513  */
6514 void
load_init_program(proc_t p)6515 load_init_program(proc_t p)
6516 {
6517 	uint32_t i;
6518 	int error;
6519 	vm_map_t map = current_map();
6520 	mach_vm_offset_t scratch_addr = 0;
6521 	mach_vm_size_t map_page_size = vm_map_page_size(map);
6522 
6523 	(void) mach_vm_allocate_kernel(map, &scratch_addr, map_page_size, VM_FLAGS_ANYWHERE, VM_KERN_MEMORY_NONE);
6524 #if CONFIG_MEMORYSTATUS
6525 	(void) memorystatus_init_at_boot_snapshot();
6526 #endif /* CONFIG_MEMORYSTATUS */
6527 
6528 #if DEBUG || DEVELOPMENT
6529 	/* Check for boot-arg suffix first */
6530 	char launchd_suffix[64];
6531 	if (PE_parse_boot_argn("launchdsuffix", launchd_suffix, sizeof(launchd_suffix))) {
6532 		char launchd_path[128];
6533 		boolean_t is_release_suffix = ((launchd_suffix[0] == 0) ||
6534 		    (strcmp(launchd_suffix, "release") == 0));
6535 
6536 		if (is_release_suffix) {
6537 			printf("load_init_program: attempting to load /sbin/launchd\n");
6538 			error = load_init_program_at_path(p, (user_addr_t)scratch_addr, "/sbin/launchd");
6539 			if (!error) {
6540 				return;
6541 			}
6542 
6543 			panic("Process 1 exec of launchd.release failed, errno %d", error);
6544 		} else {
6545 			strlcpy(launchd_path, "/usr/appleinternal/sbin/launchd.", sizeof(launchd_path));
6546 			strlcat(launchd_path, launchd_suffix, sizeof(launchd_path));
6547 
6548 			printf("load_init_program: attempting to load %s\n", launchd_path);
6549 			error = load_init_program_at_path(p, (user_addr_t)scratch_addr, launchd_path);
6550 			if (!error) {
6551 				return;
6552 			} else if (error != ENOENT) {
6553 				printf("load_init_program: failed loading %s: errno %d\n", launchd_path, error);
6554 			}
6555 		}
6556 	}
6557 #endif
6558 
6559 	error = ENOENT;
6560 	for (i = 0; i < sizeof(init_programs) / sizeof(init_programs[0]); i++) {
6561 		printf("load_init_program: attempting to load %s\n", init_programs[i]);
6562 		error = load_init_program_at_path(p, (user_addr_t)scratch_addr, init_programs[i]);
6563 		if (!error) {
6564 			return;
6565 		} else if (error != ENOENT) {
6566 			printf("load_init_program: failed loading %s: errno %d\n", init_programs[i], error);
6567 		}
6568 	}
6569 
6570 	panic("Process 1 exec of %s failed, errno %d", ((i == 0) ? "<null>" : init_programs[i - 1]), error);
6571 }
6572 
6573 /*
6574  * load_return_to_errno
6575  *
6576  * Description:	Convert a load_return_t (Mach error) to an errno (BSD error)
6577  *
6578  * Parameters:	lrtn			Mach error number
6579  *
6580  * Returns:	(int)			BSD error number
6581  *		0			Success
6582  *		EBADARCH		Bad architecture
6583  *		EBADMACHO		Bad Mach object file
6584  *		ESHLIBVERS		Bad shared library version
6585  *		ENOMEM			Out of memory/resource shortage
6586  *		EACCES			Access denied
6587  *		ENOENT			Entry not found (usually "file does
6588  *					does not exist")
6589  *		EIO			An I/O error occurred
6590  *		EBADEXEC		The executable is corrupt/unknown
6591  */
6592 static int
load_return_to_errno(load_return_t lrtn)6593 load_return_to_errno(load_return_t lrtn)
6594 {
6595 	switch (lrtn) {
6596 	case LOAD_SUCCESS:
6597 		return 0;
6598 	case LOAD_BADARCH:
6599 		return EBADARCH;
6600 	case LOAD_BADMACHO:
6601 	case LOAD_BADMACHO_UPX:
6602 		return EBADMACHO;
6603 	case LOAD_SHLIB:
6604 		return ESHLIBVERS;
6605 	case LOAD_NOSPACE:
6606 	case LOAD_RESOURCE:
6607 		return ENOMEM;
6608 	case LOAD_PROTECT:
6609 		return EACCES;
6610 	case LOAD_ENOENT:
6611 		return ENOENT;
6612 	case LOAD_IOERROR:
6613 		return EIO;
6614 	case LOAD_DECRYPTFAIL:
6615 		return EAUTH;
6616 	case LOAD_FAILURE:
6617 	default:
6618 		return EBADEXEC;
6619 	}
6620 }
6621 
6622 #include <mach/mach_types.h>
6623 #include <mach/vm_prot.h>
6624 #include <mach/semaphore.h>
6625 #include <mach/sync_policy.h>
6626 #include <kern/clock.h>
6627 #include <mach/kern_return.h>
6628 
6629 /*
6630  * execargs_alloc
6631  *
6632  * Description:	Allocate the block of memory used by the execve arguments.
6633  *		At the same time, we allocate a page so that we can read in
6634  *		the first page of the image.
6635  *
6636  * Parameters:	struct image_params *	the image parameter block
6637  *
6638  * Returns:	0			Success
6639  *		EINVAL			Invalid argument
6640  *		EACCES			Permission denied
6641  *		EINTR			Interrupted function
6642  *		ENOMEM			Not enough space
6643  *
6644  * Notes:	This is a temporary allocation into the kernel address space
6645  *		to enable us to copy arguments in from user space.  This is
6646  *		necessitated by not mapping the process calling execve() into
6647  *		the kernel address space during the execve() system call.
6648  *
6649  *		We assemble the argument and environment, etc., into this
6650  *		region before copying it as a single block into the child
6651  *		process address space (at the top or bottom of the stack,
6652  *		depending on which way the stack grows; see the function
6653  *		exec_copyout_strings() for details).
6654  *
6655  *		This ends up with a second (possibly unnecessary) copy compared
6656  *		with assembing the data directly into the child address space,
6657  *		instead, but since we cannot be guaranteed that the parent has
6658  *		not modified its environment, we can't really know that it's
6659  *		really a block there as well.
6660  */
6661 
6662 
6663 static int execargs_waiters = 0;
6664 static LCK_MTX_DECLARE_ATTR(execargs_cache_lock, &proc_lck_grp, &proc_lck_attr);
6665 
6666 static void
execargs_lock_lock(void)6667 execargs_lock_lock(void)
6668 {
6669 	lck_mtx_lock_spin(&execargs_cache_lock);
6670 }
6671 
6672 static void
execargs_lock_unlock(void)6673 execargs_lock_unlock(void)
6674 {
6675 	lck_mtx_unlock(&execargs_cache_lock);
6676 }
6677 
6678 static wait_result_t
execargs_lock_sleep(void)6679 execargs_lock_sleep(void)
6680 {
6681 	return lck_mtx_sleep(&execargs_cache_lock, LCK_SLEEP_DEFAULT, &execargs_free_count, THREAD_INTERRUPTIBLE);
6682 }
6683 
6684 static kern_return_t
execargs_purgeable_allocate(char ** execarg_address)6685 execargs_purgeable_allocate(char **execarg_address)
6686 {
6687 	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);
6688 	assert(kr == KERN_SUCCESS);
6689 	return kr;
6690 }
6691 
6692 static kern_return_t
execargs_purgeable_reference(void * execarg_address)6693 execargs_purgeable_reference(void *execarg_address)
6694 {
6695 	int state = VM_PURGABLE_NONVOLATILE;
6696 	kern_return_t kr = vm_purgable_control(bsd_pageable_map, (vm_offset_t) execarg_address, VM_PURGABLE_SET_STATE, &state);
6697 
6698 	assert(kr == KERN_SUCCESS);
6699 	return kr;
6700 }
6701 
6702 static kern_return_t
execargs_purgeable_volatilize(void * execarg_address)6703 execargs_purgeable_volatilize(void *execarg_address)
6704 {
6705 	int state = VM_PURGABLE_VOLATILE | VM_PURGABLE_ORDERING_OBSOLETE;
6706 	kern_return_t kr;
6707 	kr = vm_purgable_control(bsd_pageable_map, (vm_offset_t) execarg_address, VM_PURGABLE_SET_STATE, &state);
6708 
6709 	assert(kr == KERN_SUCCESS);
6710 
6711 	return kr;
6712 }
6713 
6714 static void
execargs_wakeup_waiters(void)6715 execargs_wakeup_waiters(void)
6716 {
6717 	thread_wakeup(&execargs_free_count);
6718 }
6719 
6720 static int
execargs_alloc(struct image_params * imgp)6721 execargs_alloc(struct image_params *imgp)
6722 {
6723 	kern_return_t kret;
6724 	wait_result_t res;
6725 	int i, cache_index = -1;
6726 
6727 	execargs_lock_lock();
6728 
6729 	while (execargs_free_count == 0) {
6730 		execargs_waiters++;
6731 		res = execargs_lock_sleep();
6732 		execargs_waiters--;
6733 		if (res != THREAD_AWAKENED) {
6734 			execargs_lock_unlock();
6735 			return EINTR;
6736 		}
6737 	}
6738 
6739 	execargs_free_count--;
6740 
6741 	for (i = 0; i < execargs_cache_size; i++) {
6742 		vm_offset_t element = execargs_cache[i];
6743 		if (element) {
6744 			cache_index = i;
6745 			imgp->ip_strings = (char *)(execargs_cache[i]);
6746 			execargs_cache[i] = 0;
6747 			break;
6748 		}
6749 	}
6750 
6751 	assert(execargs_free_count >= 0);
6752 
6753 	execargs_lock_unlock();
6754 
6755 	if (cache_index == -1) {
6756 		kret = execargs_purgeable_allocate(&imgp->ip_strings);
6757 	} else {
6758 		kret = execargs_purgeable_reference(imgp->ip_strings);
6759 	}
6760 
6761 	assert(kret == KERN_SUCCESS);
6762 	if (kret != KERN_SUCCESS) {
6763 		return ENOMEM;
6764 	}
6765 
6766 	/* last page used to read in file headers */
6767 	imgp->ip_vdata = imgp->ip_strings + (NCARGS + PAGE_SIZE);
6768 	imgp->ip_strendp = imgp->ip_strings;
6769 	imgp->ip_argspace = NCARGS;
6770 	imgp->ip_strspace = (NCARGS + PAGE_SIZE);
6771 
6772 	return 0;
6773 }
6774 
6775 /*
6776  * execargs_free
6777  *
6778  * Description:	Free the block of memory used by the execve arguments and the
6779  *		first page of the executable by a previous call to the function
6780  *		execargs_alloc().
6781  *
6782  * Parameters:	struct image_params *	the image parameter block
6783  *
6784  * Returns:	0			Success
6785  *		EINVAL			Invalid argument
6786  *		EINTR			Oeration interrupted
6787  */
6788 static int
execargs_free(struct image_params * imgp)6789 execargs_free(struct image_params *imgp)
6790 {
6791 	kern_return_t kret;
6792 	int i;
6793 	boolean_t needs_wakeup = FALSE;
6794 
6795 	kret = execargs_purgeable_volatilize(imgp->ip_strings);
6796 
6797 	execargs_lock_lock();
6798 	execargs_free_count++;
6799 
6800 	for (i = 0; i < execargs_cache_size; i++) {
6801 		vm_offset_t element = execargs_cache[i];
6802 		if (element == 0) {
6803 			execargs_cache[i] = (vm_offset_t) imgp->ip_strings;
6804 			imgp->ip_strings = NULL;
6805 			break;
6806 		}
6807 	}
6808 
6809 	assert(imgp->ip_strings == NULL);
6810 
6811 	if (execargs_waiters > 0) {
6812 		needs_wakeup = TRUE;
6813 	}
6814 
6815 	execargs_lock_unlock();
6816 
6817 	if (needs_wakeup == TRUE) {
6818 		execargs_wakeup_waiters();
6819 	}
6820 
6821 	return kret == KERN_SUCCESS ? 0 : EINVAL;
6822 }
6823 
6824 static void
exec_resettextvp(proc_t p,struct image_params * imgp)6825 exec_resettextvp(proc_t p, struct image_params *imgp)
6826 {
6827 	vnode_t vp;
6828 	off_t offset;
6829 	vnode_t tvp  = p->p_textvp;
6830 	int ret;
6831 
6832 	vp = imgp->ip_vp;
6833 	offset = imgp->ip_arch_offset;
6834 
6835 	if (vp == NULLVP) {
6836 		panic("exec_resettextvp: expected valid vp");
6837 	}
6838 
6839 	ret = vnode_ref(vp);
6840 	proc_lock(p);
6841 	if (ret == 0) {
6842 		p->p_textvp = vp;
6843 		p->p_textoff = offset;
6844 	} else {
6845 		p->p_textvp = NULLVP;   /* this is paranoia */
6846 		p->p_textoff = 0;
6847 	}
6848 	proc_unlock(p);
6849 
6850 	if (tvp != NULLVP) {
6851 		if (vnode_getwithref(tvp) == 0) {
6852 			vnode_rele(tvp);
6853 			vnode_put(tvp);
6854 		}
6855 	}
6856 }
6857 
6858 // Includes the 0-byte (therefore "SIZE" instead of "LEN").
6859 static const size_t CS_CDHASH_STRING_SIZE = CS_CDHASH_LEN * 2 + 1;
6860 
6861 static void
cdhash_to_string(char str[CS_CDHASH_STRING_SIZE],uint8_t const * const cdhash)6862 cdhash_to_string(char str[CS_CDHASH_STRING_SIZE], uint8_t const * const cdhash)
6863 {
6864 	static char const nibble[] = "0123456789abcdef";
6865 
6866 	/* Apparently still the safest way to get a hex representation
6867 	 * of binary data.
6868 	 * xnu's printf routines have %*D/%20D in theory, but "not really", see:
6869 	 * <rdar://problem/33328859> confusion around %*D/%nD in printf
6870 	 */
6871 	for (int i = 0; i < CS_CDHASH_LEN; ++i) {
6872 		str[i * 2] = nibble[(cdhash[i] & 0xf0) >> 4];
6873 		str[i * 2 + 1] = nibble[cdhash[i] & 0x0f];
6874 	}
6875 	str[CS_CDHASH_STRING_SIZE - 1] = 0;
6876 }
6877 
6878 /*
6879  * __EXEC_WAITING_ON_TASKGATED_CODE_SIGNATURE_UPCALL__
6880  *
6881  * Description: Waits for the userspace daemon to respond to the request
6882  *              we made. Function declared non inline to be visible in
6883  *		stackshots and spindumps as well as debugging.
6884  */
6885 __attribute__((noinline)) int
__EXEC_WAITING_ON_TASKGATED_CODE_SIGNATURE_UPCALL__(mach_port_t task_access_port,int32_t new_pid)6886 __EXEC_WAITING_ON_TASKGATED_CODE_SIGNATURE_UPCALL__(mach_port_t task_access_port, int32_t new_pid)
6887 {
6888 	return find_code_signature(task_access_port, new_pid);
6889 }
6890 
6891 static int
check_for_signature(proc_t p,struct image_params * imgp)6892 check_for_signature(proc_t p, struct image_params *imgp)
6893 {
6894 	mach_port_t port = IPC_PORT_NULL;
6895 	kern_return_t kr = KERN_FAILURE;
6896 	int error = EACCES;
6897 	boolean_t unexpected_failure = FALSE;
6898 	struct cs_blob *csb;
6899 	boolean_t require_success = FALSE;
6900 	int spawn = (imgp->ip_flags & IMGPF_SPAWN);
6901 	const int vfexec = 0;
6902 	os_reason_t signature_failure_reason = OS_REASON_NULL;
6903 
6904 	/*
6905 	 * Override inherited code signing flags with the
6906 	 * ones for the process that is being successfully
6907 	 * loaded
6908 	 */
6909 	proc_lock(p);
6910 	proc_csflags_update(p, imgp->ip_csflags);
6911 	proc_unlock(p);
6912 
6913 	/* Set the switch_protect flag on the map */
6914 	if (proc_getcsflags(p) & (CS_HARD | CS_KILL)) {
6915 		vm_map_switch_protect(get_task_map(p->task), TRUE);
6916 	}
6917 	/* set the cs_enforced flags in the map */
6918 	if (proc_getcsflags(p) & CS_ENFORCEMENT) {
6919 		vm_map_cs_enforcement_set(get_task_map(p->task), TRUE);
6920 	} else {
6921 		vm_map_cs_enforcement_set(get_task_map(p->task), FALSE);
6922 	}
6923 
6924 	/*
6925 	 * image activation may be failed due to policy
6926 	 * which is unexpected but security framework does not
6927 	 * approve of exec, kill and return immediately.
6928 	 */
6929 	if (imgp->ip_mac_return != 0) {
6930 		KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXITREASON_CREATE) | DBG_FUNC_NONE,
6931 		    proc_getpid(p), OS_REASON_EXEC, EXEC_EXIT_REASON_SECURITY_POLICY, 0, 0);
6932 		signature_failure_reason = os_reason_create(OS_REASON_EXEC, EXEC_EXIT_REASON_SECURITY_POLICY);
6933 		error = imgp->ip_mac_return;
6934 		unexpected_failure = TRUE;
6935 		goto done;
6936 	}
6937 
6938 	if (imgp->ip_cs_error != OS_REASON_NULL) {
6939 		signature_failure_reason = imgp->ip_cs_error;
6940 		imgp->ip_cs_error = OS_REASON_NULL;
6941 		error = EACCES;
6942 		goto done;
6943 	}
6944 
6945 #if XNU_TARGET_OS_OSX
6946 	/* Check for platform passed in spawn attr if iOS binary is being spawned */
6947 	if (proc_platform(p) == PLATFORM_IOS) {
6948 		struct _posix_spawnattr *psa = (struct _posix_spawnattr *) imgp->ip_px_sa;
6949 		if (psa == NULL || psa->psa_platform == 0) {
6950 			boolean_t no_sandbox_entitled = FALSE;
6951 #if DEBUG || DEVELOPMENT
6952 			/*
6953 			 * Allow iOS binaries to spawn on internal systems
6954 			 * if no-sandbox entitlement is present of unentitled_ios_sim_launch
6955 			 * boot-arg set to true
6956 			 */
6957 			if (unentitled_ios_sim_launch) {
6958 				no_sandbox_entitled = TRUE;
6959 			} else {
6960 				no_sandbox_entitled = IOVnodeHasEntitlement(imgp->ip_vp,
6961 				    (int64_t)imgp->ip_arch_offset, "com.apple.private.security.no-sandbox");
6962 			}
6963 #endif /* DEBUG || DEVELOPMENT */
6964 			if (!no_sandbox_entitled) {
6965 				signature_failure_reason = os_reason_create(OS_REASON_EXEC,
6966 				    EXEC_EXIT_REASON_WRONG_PLATFORM);
6967 				error = EACCES;
6968 				goto done;
6969 			}
6970 			printf("Allowing spawn of iOS binary %s since it has "
6971 			    "com.apple.private.security.no-sandbox entitlement or unentitled_ios_sim_launch "
6972 			    "boot-arg set to true\n", p->p_name);
6973 		} else if (psa->psa_platform != PLATFORM_IOS) {
6974 			/* Simulator binary spawned with wrong platform */
6975 			signature_failure_reason = os_reason_create(OS_REASON_EXEC,
6976 			    EXEC_EXIT_REASON_WRONG_PLATFORM);
6977 			error = EACCES;
6978 			goto done;
6979 		} else {
6980 			printf("Allowing spawn of iOS binary %s since correct platform was passed in spawn\n",
6981 			    p->p_name);
6982 		}
6983 	}
6984 #endif /* XNU_TARGET_OS_OSX */
6985 
6986 	/* If the code signature came through the image activation path, we skip the
6987 	 * taskgated / externally attached path. */
6988 	if (imgp->ip_csflags & CS_SIGNED) {
6989 		error = 0;
6990 		goto done;
6991 	}
6992 
6993 	/* The rest of the code is for signatures that either already have been externally
6994 	 * attached (likely, but not necessarily by a previous run through the taskgated
6995 	 * path), or that will now be attached by taskgated. */
6996 
6997 	kr = task_get_task_access_port(p->task, &port);
6998 	if (KERN_SUCCESS != kr || !IPC_PORT_VALID(port)) {
6999 		error = 0;
7000 		if (require_success) {
7001 			KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXITREASON_CREATE) | DBG_FUNC_NONE,
7002 			    proc_getpid(p), OS_REASON_CODESIGNING, CODESIGNING_EXIT_REASON_TASK_ACCESS_PORT, 0, 0);
7003 			signature_failure_reason = os_reason_create(OS_REASON_CODESIGNING, CODESIGNING_EXIT_REASON_TASK_ACCESS_PORT);
7004 			error = EACCES;
7005 		}
7006 		goto done;
7007 	}
7008 
7009 	/*
7010 	 * taskgated returns KERN_SUCCESS if it has completed its work
7011 	 * and the exec should continue, KERN_FAILURE if the exec should
7012 	 * fail, or it may error out with different error code in an
7013 	 * event of mig failure (e.g. process was signalled during the
7014 	 * rpc call, taskgated died, mig server died etc.).
7015 	 */
7016 
7017 	kr = __EXEC_WAITING_ON_TASKGATED_CODE_SIGNATURE_UPCALL__(port, proc_getpid(p));
7018 	switch (kr) {
7019 	case KERN_SUCCESS:
7020 		error = 0;
7021 		break;
7022 	case KERN_FAILURE:
7023 		error = EACCES;
7024 
7025 		KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXITREASON_CREATE) | DBG_FUNC_NONE,
7026 		    proc_getpid(p), OS_REASON_CODESIGNING, CODESIGNING_EXIT_REASON_TASKGATED_INVALID_SIG, 0, 0);
7027 		signature_failure_reason = os_reason_create(OS_REASON_CODESIGNING, CODESIGNING_EXIT_REASON_TASKGATED_INVALID_SIG);
7028 		goto done;
7029 	default:
7030 		error = EACCES;
7031 
7032 		KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXITREASON_CREATE) | DBG_FUNC_NONE,
7033 		    proc_getpid(p), OS_REASON_EXEC, EXEC_EXIT_REASON_TASKGATED_OTHER, 0, 0);
7034 		signature_failure_reason = os_reason_create(OS_REASON_EXEC, EXEC_EXIT_REASON_TASKGATED_OTHER);
7035 		unexpected_failure = TRUE;
7036 		goto done;
7037 	}
7038 
7039 	/* Only do this if exec_resettextvp() did not fail */
7040 	if (p->p_textvp != NULLVP) {
7041 		csb = ubc_cs_blob_get(p->p_textvp, -1, -1, p->p_textoff);
7042 
7043 		if (csb != NULL) {
7044 			/* As the enforcement we can do here is very limited, we only allow things that
7045 			 * are the only reason why this code path still exists:
7046 			 * Adhoc signed non-platform binaries without special cs_flags and without any
7047 			 * entitlements (unrestricted ones still pass AMFI). */
7048 			if (
7049 				/* Revalidate the blob if necessary through bumped generation count. */
7050 				(ubc_cs_generation_check(p->p_textvp) == 0 ||
7051 				ubc_cs_blob_revalidate(p->p_textvp, csb, imgp, 0, proc_platform(p)) == 0) &&
7052 				/* Only CS_ADHOC, no CS_KILL, CS_HARD etc. */
7053 				(csb->csb_flags & CS_ALLOWED_MACHO) == CS_ADHOC &&
7054 				/* If it has a CMS blob, it's not adhoc. The CS_ADHOC flag can lie. */
7055 				csblob_find_blob_bytes((const uint8_t *)csb->csb_mem_kaddr, csb->csb_mem_size,
7056 				CSSLOT_SIGNATURESLOT,
7057 				CSMAGIC_BLOBWRAPPER) == NULL &&
7058 				/* It could still be in a trust cache (unlikely with CS_ADHOC), or a magic path. */
7059 				csb->csb_platform_binary == 0 &&
7060 				/* No entitlements, not even unrestricted ones. */
7061 				csb->csb_entitlements_blob == NULL &&
7062 				csb->csb_der_entitlements_blob == NULL) {
7063 				proc_lock(p);
7064 				proc_csflags_set(p, CS_SIGNED | CS_VALID);
7065 				proc_unlock(p);
7066 			} else {
7067 				uint8_t cdhash[CS_CDHASH_LEN];
7068 				char cdhash_string[CS_CDHASH_STRING_SIZE];
7069 				proc_getcdhash(p, cdhash);
7070 				cdhash_to_string(cdhash_string, cdhash);
7071 				printf("ignoring detached code signature on '%s' with cdhash '%s' "
7072 				    "because it is invalid, or not a simple adhoc signature.\n",
7073 				    p->p_name, cdhash_string);
7074 			}
7075 		}
7076 	}
7077 
7078 done:
7079 	if (0 == error) {
7080 		/* The process's code signature related properties are
7081 		 * fully set up, so this is an opportune moment to log
7082 		 * platform binary execution, if desired. */
7083 		if (platform_exec_logging != 0 && csproc_get_platform_binary(p)) {
7084 			uint8_t cdhash[CS_CDHASH_LEN];
7085 			char cdhash_string[CS_CDHASH_STRING_SIZE];
7086 			proc_getcdhash(p, cdhash);
7087 			cdhash_to_string(cdhash_string, cdhash);
7088 
7089 			os_log(peLog, "CS Platform Exec Logging: Executing platform signed binary "
7090 			    "'%s' with cdhash %s\n", p->p_name, cdhash_string);
7091 		}
7092 	} else {
7093 		if (!unexpected_failure) {
7094 			proc_csflags_set(p, CS_KILLED);
7095 		}
7096 		/* make very sure execution fails */
7097 		if (vfexec || spawn) {
7098 			assert(signature_failure_reason != OS_REASON_NULL);
7099 			psignal_vfork_with_reason(p, p->task, imgp->ip_new_thread,
7100 			    SIGKILL, signature_failure_reason);
7101 			signature_failure_reason = OS_REASON_NULL;
7102 			error = 0;
7103 		} else {
7104 			assert(signature_failure_reason != OS_REASON_NULL);
7105 			psignal_with_reason(p, SIGKILL, signature_failure_reason);
7106 			signature_failure_reason = OS_REASON_NULL;
7107 		}
7108 	}
7109 
7110 	if (port != IPC_PORT_NULL) {
7111 		ipc_port_release_send(port);
7112 	}
7113 
7114 	/* If we hit this, we likely would have leaked an exit reason */
7115 	assert(signature_failure_reason == OS_REASON_NULL);
7116 	return error;
7117 }
7118 
7119 /*
7120  * Typically as soon as we start executing this process, the
7121  * first instruction will trigger a VM fault to bring the text
7122  * pages (as executable) into the address space, followed soon
7123  * thereafter by dyld data structures (for dynamic executable).
7124  * To optimize this, as well as improve support for hardware
7125  * debuggers that can only access resident pages present
7126  * in the process' page tables, we prefault some pages if
7127  * possible. Errors are non-fatal.
7128  */
7129 #ifndef PREVENT_CALLER_STACK_USE
7130 #define PREVENT_CALLER_STACK_USE __attribute__((noinline))
7131 #endif
7132 static void PREVENT_CALLER_STACK_USE
exec_prefault_data(proc_t p __unused,struct image_params * imgp,load_result_t * load_result)7133 exec_prefault_data(proc_t p __unused, struct image_params *imgp, load_result_t *load_result)
7134 {
7135 	int ret;
7136 	size_t expected_all_image_infos_size;
7137 	kern_return_t kr;
7138 
7139 	/*
7140 	 * Prefault executable or dyld entry point.
7141 	 */
7142 	if (vm_map_page_shift(current_map()) < (int)PAGE_SHIFT) {
7143 		DEBUG4K_LOAD("entry_point 0x%llx\n", (uint64_t)load_result->entry_point);
7144 	}
7145 	kr = vm_fault(current_map(),
7146 	    vm_map_trunc_page(load_result->entry_point,
7147 	    vm_map_page_mask(current_map())),
7148 	    VM_PROT_READ | VM_PROT_EXECUTE,
7149 	    FALSE, VM_KERN_MEMORY_NONE,
7150 	    THREAD_UNINT, NULL, 0);
7151 	if (kr != KERN_SUCCESS) {
7152 		DEBUG4K_ERROR("map %p va 0x%llx -> 0x%x\n", current_map(), (uint64_t)vm_map_trunc_page(load_result->entry_point, vm_map_page_mask(current_map())), kr);
7153 	}
7154 
7155 	if (imgp->ip_flags & IMGPF_IS_64BIT_ADDR) {
7156 		expected_all_image_infos_size = sizeof(struct user64_dyld_all_image_infos);
7157 	} else {
7158 		expected_all_image_infos_size = sizeof(struct user32_dyld_all_image_infos);
7159 	}
7160 
7161 	/* Decode dyld anchor structure from <mach-o/dyld_images.h> */
7162 	if (load_result->dynlinker &&
7163 	    load_result->all_image_info_addr &&
7164 	    load_result->all_image_info_size >= expected_all_image_infos_size) {
7165 		union {
7166 			struct user64_dyld_all_image_infos      infos64;
7167 			struct user32_dyld_all_image_infos      infos32;
7168 		} all_image_infos;
7169 
7170 		/*
7171 		 * Pre-fault to avoid copyin() going through the trap handler
7172 		 * and recovery path.
7173 		 */
7174 		if (vm_map_page_shift(current_map()) < (int)PAGE_SHIFT) {
7175 			DEBUG4K_LOAD("all_image_info_addr 0x%llx\n", load_result->all_image_info_addr);
7176 		}
7177 		kr = vm_fault(current_map(),
7178 		    vm_map_trunc_page(load_result->all_image_info_addr,
7179 		    vm_map_page_mask(current_map())),
7180 		    VM_PROT_READ | VM_PROT_WRITE,
7181 		    FALSE, VM_KERN_MEMORY_NONE,
7182 		    THREAD_UNINT, NULL, 0);
7183 		if (kr != KERN_SUCCESS) {
7184 //			printf("%s:%d map %p va 0x%llx -> 0x%x\n", __FUNCTION__, __LINE__, current_map(), vm_map_trunc_page(load_result->all_image_info_addr, vm_map_page_mask(current_map())), kr);
7185 		}
7186 		if ((load_result->all_image_info_addr & PAGE_MASK) + expected_all_image_infos_size > PAGE_SIZE) {
7187 			/* all_image_infos straddles a page */
7188 			kr = vm_fault(current_map(),
7189 			    vm_map_trunc_page(load_result->all_image_info_addr + expected_all_image_infos_size - 1,
7190 			    vm_map_page_mask(current_map())),
7191 			    VM_PROT_READ | VM_PROT_WRITE,
7192 			    FALSE, VM_KERN_MEMORY_NONE,
7193 			    THREAD_UNINT, NULL, 0);
7194 			if (kr != KERN_SUCCESS) {
7195 //				printf("%s:%d map %p va 0x%llx -> 0x%x\n", __FUNCTION__, __LINE__, current_map(), vm_map_trunc_page(load_result->all_image_info_addr + expected_all_image_infos_size -1, vm_map_page_mask(current_map())), kr);
7196 			}
7197 		}
7198 
7199 		if (vm_map_page_shift(current_map()) < (int)PAGE_SHIFT) {
7200 			DEBUG4K_LOAD("copyin(0x%llx, 0x%lx)\n", load_result->all_image_info_addr, expected_all_image_infos_size);
7201 		}
7202 		ret = copyin((user_addr_t)load_result->all_image_info_addr,
7203 		    &all_image_infos,
7204 		    expected_all_image_infos_size);
7205 		if (ret == 0 && all_image_infos.infos32.version >= DYLD_ALL_IMAGE_INFOS_ADDRESS_MINIMUM_VERSION) {
7206 			user_addr_t notification_address;
7207 			user_addr_t dyld_image_address;
7208 			user_addr_t dyld_version_address;
7209 			user_addr_t dyld_all_image_infos_address;
7210 			user_addr_t dyld_slide_amount;
7211 
7212 			if (imgp->ip_flags & IMGPF_IS_64BIT_ADDR) {
7213 				notification_address = (user_addr_t)all_image_infos.infos64.notification;
7214 				dyld_image_address = (user_addr_t)all_image_infos.infos64.dyldImageLoadAddress;
7215 				dyld_version_address = (user_addr_t)all_image_infos.infos64.dyldVersion;
7216 				dyld_all_image_infos_address = (user_addr_t)all_image_infos.infos64.dyldAllImageInfosAddress;
7217 			} else {
7218 				notification_address = all_image_infos.infos32.notification;
7219 				dyld_image_address = all_image_infos.infos32.dyldImageLoadAddress;
7220 				dyld_version_address = all_image_infos.infos32.dyldVersion;
7221 				dyld_all_image_infos_address = all_image_infos.infos32.dyldAllImageInfosAddress;
7222 			}
7223 
7224 			/*
7225 			 * dyld statically sets up the all_image_infos in its Mach-O
7226 			 * binary at static link time, with pointers relative to its default
7227 			 * load address. Since ASLR might slide dyld before its first
7228 			 * instruction is executed, "dyld_slide_amount" tells us how far
7229 			 * dyld was loaded compared to its default expected load address.
7230 			 * All other pointers into dyld's image should be adjusted by this
7231 			 * amount. At some point later, dyld will fix up pointers to take
7232 			 * into account the slide, at which point the all_image_infos_address
7233 			 * field in the structure will match the runtime load address, and
7234 			 * "dyld_slide_amount" will be 0, if we were to consult it again.
7235 			 */
7236 
7237 			dyld_slide_amount = (user_addr_t)load_result->all_image_info_addr - dyld_all_image_infos_address;
7238 
7239 #if 0
7240 			kprintf("exec_prefault: 0x%016llx 0x%08x 0x%016llx 0x%016llx 0x%016llx 0x%016llx\n",
7241 			    (uint64_t)load_result->all_image_info_addr,
7242 			    all_image_infos.infos32.version,
7243 			    (uint64_t)notification_address,
7244 			    (uint64_t)dyld_image_address,
7245 			    (uint64_t)dyld_version_address,
7246 			    (uint64_t)dyld_all_image_infos_address);
7247 #endif
7248 
7249 			if (vm_map_page_shift(current_map()) < (int)PAGE_SHIFT) {
7250 				DEBUG4K_LOAD("notification_address 0x%llx dyld_slide_amount 0x%llx\n", (uint64_t)notification_address, (uint64_t)dyld_slide_amount);
7251 			}
7252 			kr = vm_fault(current_map(),
7253 			    vm_map_trunc_page(notification_address + dyld_slide_amount,
7254 			    vm_map_page_mask(current_map())),
7255 			    VM_PROT_READ | VM_PROT_EXECUTE,
7256 			    FALSE, VM_KERN_MEMORY_NONE,
7257 			    THREAD_UNINT, NULL, 0);
7258 			if (kr != KERN_SUCCESS) {
7259 //				printf("%s:%d map %p va 0x%llx -> 0x%x\n", __FUNCTION__, __LINE__, current_map(), vm_map_trunc_page(notification_address + dyld_slide_amount, vm_map_page_mask(current_map())), kr);
7260 			}
7261 			if (vm_map_page_shift(current_map()) < (int)PAGE_SHIFT) {
7262 				DEBUG4K_LOAD("dyld_image_address 0x%llx dyld_slide_amount 0x%llx\n", (uint64_t)dyld_image_address, (uint64_t)dyld_slide_amount);
7263 			}
7264 			kr = vm_fault(current_map(),
7265 			    vm_map_trunc_page(dyld_image_address + dyld_slide_amount,
7266 			    vm_map_page_mask(current_map())),
7267 			    VM_PROT_READ | VM_PROT_EXECUTE,
7268 			    FALSE, VM_KERN_MEMORY_NONE,
7269 			    THREAD_UNINT, NULL, 0);
7270 			if (kr != KERN_SUCCESS) {
7271 //				printf("%s:%d map %p va 0x%llx -> 0x%x\n", __FUNCTION__, __LINE__, current_map(), vm_map_trunc_page(dyld_image_address + dyld_slide_amount, vm_map_page_mask(current_map())), kr);
7272 			}
7273 			if (vm_map_page_shift(current_map()) < (int)PAGE_SHIFT) {
7274 				DEBUG4K_LOAD("dyld_version_address 0x%llx dyld_slide_amount 0x%llx\n", (uint64_t)dyld_version_address, (uint64_t)dyld_slide_amount);
7275 			}
7276 			kr = vm_fault(current_map(),
7277 			    vm_map_trunc_page(dyld_version_address + dyld_slide_amount,
7278 			    vm_map_page_mask(current_map())),
7279 			    VM_PROT_READ,
7280 			    FALSE, VM_KERN_MEMORY_NONE,
7281 			    THREAD_UNINT, NULL, 0);
7282 			if (kr != KERN_SUCCESS) {
7283 //				printf("%s:%d map %p va 0x%llx -> 0x%x\n", __FUNCTION__, __LINE__, current_map(), vm_map_trunc_page(dyld_version_address + dyld_slide_amount, vm_map_page_mask(current_map())), kr);
7284 			}
7285 			if (vm_map_page_shift(current_map()) < (int)PAGE_SHIFT) {
7286 				DEBUG4K_LOAD("dyld_all_image_infos_address 0x%llx dyld_slide_amount 0x%llx\n", (uint64_t)dyld_version_address, (uint64_t)dyld_slide_amount);
7287 			}
7288 			kr = vm_fault(current_map(),
7289 			    vm_map_trunc_page(dyld_all_image_infos_address + dyld_slide_amount,
7290 			    vm_map_page_mask(current_map())),
7291 			    VM_PROT_READ | VM_PROT_WRITE,
7292 			    FALSE, VM_KERN_MEMORY_NONE,
7293 			    THREAD_UNINT, NULL, 0);
7294 			if (kr != KERN_SUCCESS) {
7295 //				printf("%s:%d map %p va 0x%llx -> 0x%x\n", __FUNCTION__, __LINE__, current_map(), vm_map_trunc_page(dyld_all_image_infos_address + dyld_slide_amount, vm_map_page_mask(current_map())), kr);
7296 			}
7297 		}
7298 	}
7299 }
7300 
7301 static int
7302 sysctl_libmalloc_experiments SYSCTL_HANDLER_ARGS
7303 {
7304 #pragma unused(oidp, arg2, req)
7305 	int changed;
7306 	errno_t error;
7307 	uint64_t value = os_atomic_load_wide(&libmalloc_experiment_factors, relaxed);
7308 
7309 	error = sysctl_io_number(req, value, sizeof(value), &value, &changed);
7310 	if (error) {
7311 		return error;
7312 	}
7313 
7314 	if (changed) {
7315 		os_atomic_store_wide(&libmalloc_experiment_factors, value, relaxed);
7316 	}
7317 
7318 	return 0;
7319 }
7320 
7321 EXPERIMENT_FACTOR_PROC(_kern, libmalloc_experiments, CTLTYPE_QUAD | CTLFLAG_RW, 0, 0, &sysctl_libmalloc_experiments, "A", "");
7322 
7323