Skip to content

Return EFAULT or E2BIG instead of crashing on bad posix_spawn/execve arguments - #19

Open
willmcginnis wants to merge 1 commit into
linuxkid473:mainfrom
willmcginnis:fix-unreadable-spawn-exec-args-v4
Open

willmcginnis wants to merge 1 commit into
linuxkid473:mainfrom
willmcginnis:fix-unreadable-spawn-exec-args-v4

Conversation

@willmcginnis

@willmcginnis willmcginnis commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

posix_spawn and execve read the guest's path in place, its environment too when the GPU shim is installed, and for an arm64-only program its argv, so a bad pointer in any of them (for an unreadable argument string, only among the first 4097) crashed the runtime where macOS returns EFAULT. This is the spawn counterpart of #12: they are now read with guestmem's fault-safe copy. Two other behaviours change, and I'd rather you see both before the details. First, a quieter bug in the same loop is fixed: a program that spawned an arm64-only child with more than 4097 arguments lost the rest without an error (a child spawned with 5000 arguments saw 4097), and a spawn with too many arguments could start a child where macOS answers E2BIG; the same cap also cut short the environment handed on when the GPU shim was scrubbed from it. Now the runtime reads the whole argv, and fails with EFAULT for an argument it cannot read or with E2BIG when the space for arguments runs out, as macOS does (the exact limit differs slightly; see the details). Second, one difference from macOS is new: when the runtime itself rejects an arm64-only child's argv (E2BIG or EFAULT), that spawn's file actions have not run, where macOS runs them first. So a file action that would create a file does not create it, and a failing file action's error (ENOENT in the measurement below) is reported as the argv error instead. Both are measured below, and the second is described in "Differences from XNU".

A path or an environment the runtime cannot read is passed to the host kernel as the guest passed it, and the host picks the error as it does natively: EFAULT for the unreadable path and environment the tests below use, or ENAMETOOLONG for a path with no NUL in its first 1024 bytes (MAXPATHLEN) before an unreadable page (XNU's exec_save_path; measured natively and with this change). argv is different: the host is handed the re-launch's own argv (the runtime, its options and the program), so the runtime copies the guest's argv itself and charges it as XNU's exec_extract_strings (bsd/kern/kern_exec.c) charges argv. Each argument costs its bytes as passed (not after conversion to UTF-8), its NUL and an 8-byte pointer, and the final NULL pointer costs 8 bytes, out of NCARGS (1 MiB); no argument is read past the space left; and the call fails with EFAULT if a pointer in argv cannot be read, even with no space left (XNU reads it first too), or if a byte of an argument cannot be read before the space runs out, and with E2BIG if the space runs out first. Unlike XNU, the environment is not charged with argv: the host kernel charges it, and the runtime charges it separately, out of a space of its own, only when a GPU shim is installed, to scrub it. That charge replaces the old cap of 4097 entries.

Differences from XNU in argument limits and spawn error ordering

One limit, close to 1 MiB: the environment and its NULL pointer are charged by the host kernel against the re-launch's argv, which starts with the runtime's own path and options instead of the guest's argv[0]. Measured with an empty environment in the setup below: with a 5-byte argv[0], a single argument may be up to 1048214 bytes against 1048537 natively, because the host also counts the runtime's own arguments; with a 2000-byte argv[0], up to 1046550 bytes against 1046542 natively (the 8 bytes of the environment's NULL pointer, which XNU charges out of the same space), where main let through 1048214 whatever the length of argv[0]. One difference in order remains as well: when the runtime answers E2BIG or EFAULT for argv itself, a posix_spawn's file actions have not run, where XNU runs them first (exec_handle_file_actions); measured with a file action that opens a missing file, an argument of ARG_MAX bytes gets ENOENT from macOS and from main but E2BIG with this change, an unreadable argument gets ENOENT natively and EFAULT with this change (main crashes), and, with the ARG_MAX-byte argument, a file action that creates a file leaves it behind natively and on main but not with this change. By XNU's source its check of a requested architecture (posix_spawnattr_setarchpref_np) also comes before argv, so the same holds there; that one is not measured.

tests/guest/bad_spawn_pointers.c runs 12 rows, each in its own child process, and compares them with the native answers: an ordinary spawn whose child exits 7; an unreadable path, argv, argument and environment, and an unmapped execve argv (EFAULT); an argument of ARG_MAX bytes, an argument that runs out of space exactly where an unreadable page begins, and 120000 empty arguments whose pointers do not fit (E2BIG); an argument that reaches an unreadable page one byte before the space runs out (EFAULT); a spawn with 5000 arguments whose child must count all 5000; and an argument of ARG_MAX / 2 bytes of 0xff, which fits as passed but would not once converted to UTF-8, followed by an unreadable one (EFAULT). Natively and with this change all 12 rows pass, in 5 runs each for arm64 and arm64e, and with this change they also pass with MACLATOR_GPU_DIR naming a directory that holds a libmclmetal.dylib, so that the environment is read. On main (2d2f5b3) 3 pass in every run: 7 rows kill the process, 120000 empty arguments start a child where macOS answers E2BIG, and the child spawned with 5000 arguments sees 4097; with the GPU directory set, the unreadable environment kills the process too. cargo test -p maclator (18 tests) and the workspace tests pass, and spawn_options.c still passes.

Tested on an Apple Silicon Mac on macOS 27.0.1, running the x86_64 build under Rosetta 2 with a macOS 26.6.2 (25G83) arm64e shared cache as the sysroot and dyld from 26.7.1, --no-aot.

This is one of a few small, independent PRs I'm sending today; they can be reviewed in any order, and there's no rush on any of them.

posix_spawn and execve read the guest's path, its environment when the
GPU shim is installed, and for an arm64-only program its argv, with
CStr::from_ptr and raw loads. An unreadable path, argv or environment,
or an unreadable argument among the first 4097, therefore crashed the
runtime where macOS returns EFAULT.

They are now read with guestmem's fault-safe copy. A path or an
environment the runtime cannot read goes to the host kernel as the
guest passed it, and the host picks the error as it does natively:
EFAULT, or ENAMETOOLONG for a path whose first MAXPATHLEN bytes hold
no NUL (XNU's exec_save_path). argv is the runtime's own to copy,
since the host is handed the re-launch's argv, so it is now charged as
XNU's exec_extract_strings charges argv: each argument costs its bytes
as passed, its NUL and an 8-byte pointer, and the final NULL pointer
costs 8 bytes, out of NCARGS (1 MiB); no argument is read past the
space left; and the call fails with EFAULT if a pointer in argv cannot
be read, even with no space left, or a byte of an argument cannot be
read before the space runs out, and with E2BIG if the space runs out
first. Unlike XNU, the environment is not charged with argv: the host
kernel charges it, with its NULL pointer, against the re-launch's
argv, and the runtime charges it separately, out of a space of its
own, only when a GPU shim is installed, to scrub it.

That charge replaces the old cap of 4097 entries, which dropped any
later arguments without an error, and also cut short the environment
handed on when the GPU shim was scrubbed from it.

One difference in order remains: when the runtime answers E2BIG or
EFAULT for argv itself, a posix_spawn's file actions have not run,
where XNU runs them (exec_handle_file_actions) before it reads argv,
so argv's error replaces a failing file action's, and a file an
action would create is not created. By XNU's source its check of a
requested architecture also comes before argv (not measured).

tests/guest/bad_spawn_pointers.c checks twelve rows against the native
answers: an ordinary spawn whose child exits 7; an unreadable path,
argv, argument and environment, and an unmapped execve argv (EFAULT);
an argument of ARG_MAX bytes, an argument that runs out of space
exactly where an unreadable page begins, and 120000 empty arguments
whose pointers do not fit (E2BIG); an argument that reaches an
unreadable page one byte before the space runs out (EFAULT); a spawn
with 5000 arguments whose child must count all 5000; and an argument
of ARG_MAX / 2 bytes of 0xff, which fits as passed but would not once
converted to UTF-8, followed by an unreadable one (EFAULT).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant