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