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