Skip to content

Read an executable mapping's Mach-O header without crashing - #18

Open
willmcginnis wants to merge 1 commit into
linuxkid473:mainfrom
willmcginnis:fix-unreadable-aot-image-header-v4
Open

willmcginnis wants to merge 1 commit into
linuxkid473:mainfrom
willmcginnis:fix-unreadable-aot-image-header-v4

Conversation

@willmcginnis

Copy link
Copy Markdown
Contributor

This stops the runtime from killing a program (with SIGBUS in 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 (with SIGBUS in the cases measured) when the mapping could not be read (PROT_EXEC alone, which the runtime maps PROT_NONE once it drops PROT_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 most sizeofcmds bytes, 16 KiB as before, and never past the mapping. The mmap handler now 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, just as one 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. A real library is registered as before: a small dylib loaded with dlopen is registered with its own LC_UUID and a range of the same size as on main. I checked the rounding with MACLATOR_STATS=1: mapped with a 4097-byte request, a header registers when its load commands end within the 16 KiB mapped (8 KiB with MAP_FIXED), and not when they run past 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. Each row runs in its own child process, which prints what mmap answered, and passes only when that child then exits with status 0, so a crash also fails the row under MACLATOR_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 with EPERM, 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-aot they pass on main too. Four unit tests in aot.rs check that load commands past the mapped 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.

With this change all 3 rows pass in 5 runs each for arm64 and arm64e, and natively too, where each mmap answers EPERM. On main (2d2f5b3) rows 1 and 2 kill the row's child with SIGBUS in every one of those runs (AOT cache on, the default). Row 3 passes on main on 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_CACHE to a fresh directory for each run and MACLATOR_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.

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.
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