Repository navigation
Return EFAULT or E2BIG instead of crashing on bad posix_spawn/execve arguments - #19
Open
willmcginnis wants to merge 1 commit into
Open
willmcginnis wants to merge 1 commit into
willmcginnis wants to merge 1 commit into
Conversation
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
posix_spawnandexecveread 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 returnsEFAULT. This is the spawn counterpart of #12: they are now read withguestmem'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 answersE2BIG; 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 withEFAULTfor an argument it cannot read or withE2BIGwhen 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 (E2BIGorEFAULT), 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 (ENOENTin 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:
EFAULTfor the unreadable path and environment the tests below use, orENAMETOOLONGfor a path with no NUL in its first 1024 bytes (MAXPATHLEN) before an unreadable page (XNU'sexec_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'sexec_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 ofNCARGS(1 MiB); no argument is read past the space left; and the call fails withEFAULTif 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 withE2BIGif 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-byteargv[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-byteargv[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), wheremainlet through 1048214 whatever the length ofargv[0]. One difference in order remains as well: when the runtime answersE2BIGorEFAULTfor argv itself, aposix_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 ofARG_MAXbytes getsENOENTfrom macOS and frommainbutE2BIGwith this change, an unreadable argument getsENOENTnatively andEFAULTwith this change (maincrashes), and, with theARG_MAX-byte argument, a file action that creates a file leaves it behind natively and onmainbut 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.cruns 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 unmappedexecveargv (EFAULT); an argument ofARG_MAXbytes, 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 ofARG_MAX / 2bytes 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 withMACLATOR_GPU_DIRnaming a directory that holds alibmclmetal.dylib, so that the environment is read. Onmain(2d2f5b3) 3 pass in every run: 7 rows kill the process, 120000 empty arguments start a child where macOS answersE2BIG, 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, andspawn_options.cstill 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.