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

Closed
opened 2026-11-24 06:32:39 +00:00 by pickles976 · 3 comments
pickles976 commented 2026-11-24 06:32:39 +00:00 (Migrated from github.com)
Contributor

Imported from ivall/xcl. Originally posted by pickles976 on 2026-11-24T06:32:39Z.


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.

<!-- greyfinch-sync:ivall/xcl:issue:6576102350 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/issues/836). Originally posted by [pickles976](https://github.com/pickles976) on 2026-11-24T06:32:39Z. --- **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 commented 2026-11-24 07:06:48 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ivall/xcl. Originally posted by pickles976 on 2026-11-24T06:34:24Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7124071710 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/issues/836#issuecomment-7124071710). Originally posted by [pickles976](https://github.com/pickles976) on 2026-11-24T06:34:24Z. --- 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 commented 2026-11-24 16:07:03 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ivall/xcl. Originally posted by pickles976 on 2026-11-24T15:24:13Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7135966213 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/issues/836#issuecomment-7135966213). Originally posted by [pickles976](https://github.com/pickles976) on 2026-11-24T15:24:13Z. --- 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 commented 2026-11-24 16:07:09 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ivall/xcl. Originally posted by pickles976 on 2026-11-24T15:37:07Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7136329817 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/issues/836#issuecomment-7136329817). Originally posted by [pickles976](https://github.com/pickles976) on 2026-11-24T15:37:07Z. --- 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.
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
xcl-dev/xcl#830
No description provided.