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