Loader: IAT thunk and TLS-index writes don't reject targets inside the headers (hardening parity with leonida#501) #836

Open
opened 2026-11-26 04:34:04 +00:00 by github-bot · 3 comments
Owner

@⁠pickles976 on GitHub · 2026-11-24 06:32 UTC · original

What this is: a small, unit-testable hardening parity gap in the manual-mapping path. No game data; the title's own images are well-formed — this is defense-in-depth against crafted images (and cheap to close).

The gap. Two writes into a manually-mapped image skip the one validation that matters — target must not be inside the headers:

  • crates/xcl/src/loader.rs:178 — ((base + e.iat_rva) as *mut u64).write_unaligned(..): resolves an import and writes the thunk at the IAT RVA without checking iat_rva >= size_of_headers. A malformed image whose import directory RVA points into the headers gets its own PE headers overwritten (or the write faults inside xcl if the headers page is mapped RX — either is a loader bug, not the image's).
  • the TLS carrier path (crates/xcl/src/tls.rs, "write that slot's index into the game's _tls_index") takes the target from the image's AddressOfIndex (read at tls.rs:268 style deref) with the same missing bound check.

pe.rs already parses and stores size_of_headers (pe.rs:200) and uses it to clamp reads (pe.rs:335/357) — the write side just needs the same invariant: directory-derived write targets ∈ [size_of_headers, SizeOfImage).

Precedent: the sibling project just closed the identical class — ubiquitarianism/leonida#501 ("refuse an import address table or TLS index in the headers"). Two independent projects finding the same loader invariant within an hour is the usual evidence it belongs in the parse step rather than at each write site: reject once in pe.rs (PeError::HeaderWriteTarget), and the write sites keep working unguarded on already-vetted images.

Test sketch: craft a minimal PE in tests/data/ (synthetic, allowed) with SizeOfHeaders=0x400 and an import-directory (and TLS AddressOfIndex) RVA of 0x80; assert Pe::parse/mapping returns the new error instead of writing.

**@⁠pickles976** on GitHub · 2026-11-24 06:32 UTC · [original](https://github.com/xcl-dev/xcl/issues/836) **What this is:** a small, unit-testable hardening parity gap in the manual-mapping path. No game data; the title's own images are well-formed — this is defense-in-depth against crafted images (and cheap to close). **The gap.** Two writes into a manually-mapped image skip the one validation that matters — *target must not be inside the headers*: - `crates/xcl/src/loader.rs:178` — `((base + e.iat_rva) as *mut u64).write_unaligned(..)`: resolves an import and writes the thunk at the IAT RVA **without checking `iat_rva >= size_of_headers`**. A malformed image whose import directory RVA points into the headers gets its own PE headers overwritten (or the write faults inside xcl if the headers page is mapped RX — either is a loader bug, not the image's). - the TLS carrier path (`crates/xcl/src/tls.rs`, "write that slot's index into the game's `_tls_index`") takes the target from the image's `AddressOfIndex` (read at `tls.rs:268` style deref) with the same missing bound check. `pe.rs` already parses and stores `size_of_headers` (`pe.rs:200`) and uses it to clamp *reads* (`pe.rs:335/357`) — the write side just needs the same invariant: directory-derived write targets ∈ [`size_of_headers`, `SizeOfImage`). **Precedent:** the sibling project just closed the identical class — ubiquitarianism/leonida#501 ("refuse an import address table or TLS index in the headers"). Two independent projects finding the same loader invariant within an hour is the usual evidence it belongs in the parse step rather than at each write site: reject once in `pe.rs` (`PeError::HeaderWriteTarget`), and the write sites keep working unguarded on already-vetted images. **Test sketch:** craft a minimal PE in `tests/data/` (synthetic, allowed) with `SizeOfHeaders=0x400` and an import-directory (and TLS `AddressOfIndex`) RVA of `0x80`; assert `Pe::parse`/mapping returns the new error instead of writing.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:34:04 +00:00
Author
Owner

@⁠pickles976 on GitHub · 2026-11-24 06:34 UTC · original

Class weight update within the hour: the sibling project independently closed the same invariant — Marvisak/VIctory#670 ("loader: use checked offsets and sizes in image preparation"). Third project, one day, no coordination: when three independent manual-mapping loaders each find that directory-derived write targets need the size_of_headers bound, the natural home is the parse step once (as proposed here), not per-write-site checks forever.

**@⁠pickles976** on GitHub · 2026-11-24 06:34 UTC · [original](https://github.com/xcl-dev/xcl/issues/836#issuecomment-7124071710) Class weight update within the hour: the sibling project independently closed the same invariant — **Marvisak/VIctory#670** ("loader: use checked offsets and sizes in image preparation"). Third project, one day, no coordination: when three independent manual-mapping loaders each find that directory-derived write targets need the `size_of_headers` bound, the natural home is the parse step once (as proposed here), not per-write-site checks forever.
Author
Owner

@⁠pickles976 on GitHub · 2026-11-24 15:24 UTC · original

Fourth instance within a day, same class: the sibling runtime just opened Marvisak/VIctory#1047 ("fix 4 bounds and cleanup bugs in windows.rs" — their loader image preparation) on top of their #670, this repo's parse-step work referenced here, and the leonida original. Four independent finds in one day across three manual-mapping loaders is no longer a coincidence to fix per-site — it is evidence that the invariant (directory-derived targets and table bounds must be validated once, at parse) belongs in Pe::parse so every future write site inherits it. Leaving the proposal as filed; the class keeps making its own argument.

**@⁠pickles976** on GitHub · 2026-11-24 15:24 UTC · [original](https://github.com/xcl-dev/xcl/issues/836#issuecomment-7135966213) Fourth instance within a day, same class: the sibling runtime just opened **Marvisak/VIctory#1047** ("fix 4 bounds and cleanup bugs in `windows.rs`" — their loader image preparation) on top of their #670, this repo's parse-step work referenced here, and the leonida original. Four independent finds in one day across three manual-mapping loaders is no longer a coincidence to fix per-site — it is evidence that the invariant (*directory-derived targets and table bounds must be validated once, at parse*) belongs in `Pe::parse` so every future write site inherits it. Leaving the proposal as filed; the class keeps making its own argument.
Author
Owner

@⁠pickles976 on GitHub · 2026-11-24 15:37 UTC · original

Status update on this proposal, same day: it got implemented while we were filing examples for it — #1367 now validates directory descriptors and block sizes in the parser (imports/delay_imports/relocations, Err(BadRva) at parse time) and #1397 bounds rva_to_offset at EOF on the consumer side. If the exports-directory coverage question I left on #1367 comes back clean, this issue can be closed as done after those two merge — the five-instance class (both sibling runtimes + leonida + today's two PRs) is the receipts.

**@⁠pickles976** on GitHub · 2026-11-24 15:37 UTC · [original](https://github.com/xcl-dev/xcl/issues/836#issuecomment-7136329817) Status update on this proposal, same day: it got implemented while we were filing examples for it — **#1367** now validates directory descriptors and block sizes in the parser (`imports`/`delay_imports`/`relocations`, `Err(BadRva)` at parse time) and **#1397** bounds `rva_to_offset` at EOF on the consumer side. If the exports-directory coverage question I left on #1367 comes back clean, this issue can be closed as done after those two merge — the five-instance class (both sibling runtimes + leonida + today's two PRs) is the receipts.
This discussion has been locked. Commenting is limited to contributors.
No description provided.