Repository navigation
Read an executable mapping's Mach-O header without crashing - #18
Open
willmcginnis wants to merge 1 commit into
Open
willmcginnis wants to merge 1 commit into
willmcginnis wants to merge 1 commit into
Conversation
When a guest maps a file executable at offset 0, as dyld maps a library's __TEXT, register_image_at reads the start of the mapping as a Mach-O header to register it with the AOT cache. It read the header and then walked its load commands through raw pointers, as far as 16 KiB from the start of the mapping, whatever the mapping's length. The runtime therefore crashed when the mapping could not be read (PROT_EXEC alone, which becomes PROT_NONE once the runtime drops PROT_EXEC), or when a load command it walked to lay past the mapping in address space that could not be read; and it took whatever lay after a short mapping as part of the image. It now copies the 32-byte header, and then the load commands, with guestmem::read, and parses only what it copied: at most sizeofcmds bytes, 16 KiB as before, and never past the mapping. The mmap handler passes the length actually mapped: the guest's length rounded up to 16 KiB, as mmap_16k maps it, or for MAP_FIXED to the host's 4 KiB pages. A mapping it cannot read is not registered, as a mapping that is not an arm64 Mach-O is not, and neither is a header with an LC_UUID or LC_SEGMENT_64 too short for the fields read from it. tests/guest/bad_aot_header.c maps a 4 KiB file whose header claims more load commands than the file holds, in three ways: execute only; 4 KiB with MAP_FIXED at the start of a PROT_NONE reservation; and 4 KiB on its own, where the pages after the end of the file are expected to fault on an Intel Mac (not tested; on Apple Silicon they read as zeros). Each row runs in its own child process, which prints what mmap answered; the row passes only when that child then exits with status 0. The rows only exercise this code with the AOT cache on (the default). Four unit tests check that load commands past the mapping's length are not used, that load commands which run into an unreadable page neither fault nor register, that only sizeofcmds bytes of load commands are read, so an image registers even when its mapping runs on into an unreadable page, and that a too-short LC_UUID or LC_SEGMENT_64 keeps the image from registering.
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.
This stops the runtime from killing a program (with
SIGBUSin the cases measured) while it reads the Mach-O header or its load commands for an executable mapping.When a program maps a file executable at offset 0, as dyld does for a library's
__TEXT, the runtime reads the start of the mapping as a Mach-O header to register it with the AOT cache. It read the header and then walked its load commands through raw pointers, as far as 16 KiB from the start of the mapping, whatever the mapping's length. So the program was killed (withSIGBUSin the cases measured) when the mapping could not be read (PROT_EXECalone, which the runtime mapsPROT_NONEonce it dropsPROT_EXEC), or when a load command it walked to lay past the mapping in address space that cannot be read. When the bytes after a short mapping could be read, they were taken as part of the image.This copies the 32-byte header, and then the load commands, with
guestmem::read, and parses only what it copied: at mostsizeofcmdsbytes, 16 KiB as before, and never past the mapping. Themmaphandler now passes the length actually mapped: the guest's length rounded up to 16 KiB, asmmap_16kmaps it, or forMAP_FIXEDto the host's 4 KiB pages. A mapping it cannot read is not registered, just as one that is not an arm64 Mach-O is not, and neither is a header with anLC_UUIDorLC_SEGMENT_64too short for the fields read from it. A real library is registered as before: a small dylib loaded withdlopenis registered with its ownLC_UUIDand a range of the same size as onmain. I checked the rounding withMACLATOR_STATS=1: mapped with a 4097-byte request, a header registers when its load commands end within the 16 KiB mapped (8 KiB withMAP_FIXED), and not when they run past it.tests/guest/bad_aot_header.cmaps a 4 KiB file whose header claims more load commands than the file holds, in three ways: execute only; 4 KiB withMAP_FIXEDat the start of aPROT_NONEreservation; and 4 KiB on its own. Each row runs in its own child process, which prints whatmmapanswered, and passes only when that child then exits with status 0, so a crash also fails the row underMACLATOR_DUMP_ON_FAULT=1, where the runtime ends a faulting process with an exit status instead of the signal. macOS (27.0.1 on Apple Silicon here) refuses all three mappings withEPERM, presumably because the file is not signed code; this change keeps the runtime's own answer, which is that the mapping succeeds, so the rows check only that the row's child survives. The rows reach this code only with the AOT cache on, which is the default; with--no-aotthey pass onmaintoo. Four unit tests inaot.rscheck that load commands past the mapped length are not used, that load commands which run into an unreadable page neither fault nor register, that onlysizeofcmdsbytes of load commands are read, so an image registers even when its mapping runs on into an unreadable page, and that a too-shortLC_UUIDorLC_SEGMENT_64keeps the image from registering.With this change all 3 rows pass in 5 runs each for arm64 and arm64e, and natively too, where each
mmapanswersEPERM. Onmain(2d2f5b3) rows 1 and 2 kill the row's child withSIGBUSin every one of those runs (AOT cache on, the default). Row 3 passes onmainon Apple Silicon, where the rest of the 16 KiB page after the end of the file reads as zeros, so there it adds nothing beyond row 2; it is there for Intel Macs, where the old code should fault on the pages after the end of the file, but I could not test that. The unit tests (cargo test -p maclator, 22 tests) and the workspace tests pass, run with--release --target x86_64-apple-darwin.Tested on an Apple Silicon Mac on macOS 27.0.1, running the x86_64 build under Rosetta 2 (which gives it 4 KiB pages, as on Intel) with a macOS 26.6.2 (25G83) arm64e shared cache as the sysroot and dyld from 26.7.1; the translated runs with the AOT cache on set
MACLATOR_CACHEto a fresh directory for each run andMACLATOR_NO_AOT_BUILD=1. Not tested on an Intel Mac.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.