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