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