Fix ELF patch cache identity and mapping lifetime - #1380
Open
Weiteng Chen (CvvT) wants to merge 6 commits into
Open
Weiteng Chen (CvvT) wants to merge 6 commits into
Weiteng Chen (CvvT) wants to merge 6 commits into
Conversation
Key ELF patch state by retained filesystem descriptor identity and use the resolved descriptor for ELF reads. Finalize state after subsystem close and translate closed read/stat handles to EBADF. Narrow file mmap helpers to filesystem descriptors and preserve mmap-specific read errors. Document the remaining close-before-mprotect limitation. Fixes #1316. Mapping-lifetime invalidation in #1268 remains separate.
Replace the fatal panics in the runtime rewriter with errors surfaced to the guest. Every path that previously aborted the shim -- mprotect to RW failing, faulting on guest memory, disassembly errors, stub or code write-back failures -- now leaves the segment non-executable and returns an errno, so a guest can no longer abort the host with `mmap`/`mprotect` on a crafted mapping. Paths that already degraded to the trap fallback still restore RX as before. Hold the elf_patch_cache lock across both selection and patching in the mprotect path. Releasing it in between let a concurrent `close` drop the patch state and leave unpatched code to become executable. Record a range in `patched_ranges` only once a patch has committed. Now that failures are recoverable, marking an aborted attempt would let the skip guard wave raw syscalls through on a retry. Conversely, invalidate patched ranges once a mapping actually becomes writable, so code modified through `mprotect(PROT_WRITE)` is rewritten again before it can go back to executable. Cache a `None` for descriptors that are not ELF images we patch, so the header and program-header reads no longer run on every mapping of the same descriptor, and move that probe outside the lock so it does not serialize other mappings. Read through the resolved descriptor via `files.fs` instead of re-wrapping it in an `AnyTypedFd` for a dispatch that can only take the `Fs` arm. Derive Clone/Copy for ProtFlags; it is a plain c_int newtype and the `&ProtFlags` parameter was a workaround for the omission. Add a regression test for the descriptor-identity fix: map an ELF, close the fd, and confirm a later descriptor reusing the same fd number gets fresh state.
Resolve the syscall entry point once at the top of `do_mmap_file` and return early when it is zero. The guard previously only covered the PROT_EXEC branch, so a platform without syscall rewriting still probed ELF headers, read program headers and allocated a buffer on every non-exec file mapping, none of which is ever consumed. `sys_mprotect` resolves it once too, instead of calling `get_syscall_entry_point` twice per syscall. Stop asserting that a patched mapping is never writable. `sys_mmap` only rejects PROT_WRITE on file mappings when MAP_SHARED is set, so a private RWX mapping of an ELF is accepted and the assert was guest-reachable. Re-apply the requested protection after patching and invalidate the patch record instead, which is the same end state `mprotect` produces for a write transition. Record exec mappings in `file_mappings` as well. Only the non-exec branch did, so once such a mapping became writable and was invalidated, a later mprotect(+EXEC) had nothing to re-patch. Already-patched ranges are still skipped via `patched_ranges`, and pre-patched descriptors are skipped during collection, so this only adds the missing re-patch path.
Weiteng Chen (CvvT)
marked this pull request as ready for review
September 16, 2026 01:50
|
🤖 SemverChecks 🤖 Click for details |
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.
Fixes #1316 by keying ELF patch state by retained file descriptor identity instead of recyclable raw fd numbers.
Previously deferred patching assumed the descriptor remains open until its mappings gain
PROT_EXEC. Closing the descriptor removed its entry even if mappings survive, sommap -> close -> mprotect(PROT_EXEC)can skip patching. This is also addressed in this PR.