Skip to content

Fix ELF patch cache identity and mapping lifetime - #1380

Open
Weiteng Chen (CvvT) wants to merge 6 commits into
mainfrom
fix/1316-elf-patch-cache
Open

Weiteng Chen (CvvT) wants to merge 6 commits into
mainfrom
fix/1316-elf-patch-cache

Conversation

@CvvT

Copy link
Copy Markdown
Contributor

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, so mmap -> close -> mprotect(PROT_EXEC) can skip patching. This is also addressed in this PR.

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.
@CvvT
Weiteng Chen (CvvT) marked this pull request as ready for review September 16, 2026 01:50
@github-actions

Copy link
Copy Markdown

🤖 SemverChecks 🤖 ⚠️ Potential breaking API changes detected ⚠️

Click for details
--- failure copy_impl_added: type now implements Copy ---

Description:
A public type now implements Copy, causing non-move closures to capture it by reference instead of moving it.
        ref: https://github.com/rust-lang/rust/issues/100905
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.50.0/src/lints/copy_impl_added.ron

Failed in:
  litebox_common_linux::ProtFlags in /home/runner/work/litebox/litebox/litebox_common_linux/src/lib.rs:102

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.

elf_patch_cache is keyed by a recyclable fd number

1 participant