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