Skip to content

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

Backlog

Description

@pickles976

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 — ProjectLeonida/mandy#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.

Activity

  1. pickles976 commented on Nov 24, 2026

    @pickles976
    Author

    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.

  2. added a commit that references this issue on Nov 24, 2026
    6ce1240
  3. pickles976 commented on Nov 24, 2026

    @pickles976
    Author

    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.

  4. pickles976 commented on Nov 24, 2026

    @pickles976
    Author

    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 up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions