Skip to content

PE loader: relocation targets outside the image fail the load - #291

Open
honestlyBroke wants to merge 1 commit into
xcl-dev:mainfrom
honestlyBroke:pe-loader-reloc-bounds
Open

honestlyBroke wants to merge 1 commit into
xcl-dev:mainfrom
honestlyBroke:pe-loader-reloc-bounds

Conversation

@honestlyBroke

Copy link
Copy Markdown

Hardening, not a playability fix: no title is known to hit this path.

map_image patched base + rva for every relocation without checking the target against the mapping. The entries come from the file, so a target past SizeOfImage, or a DIR64 whose 8 bytes straddle its end, wrote outside the image. #178 made the RVA arithmetic overflow-safe but did not bound the targets. The check is in the loader, not in Pe::relocations, because that also reads file-layout images and has no mapping size.

Part of #106.

What is in it

  • crates/xcl/src/loader.rs: before patching, the target plus its width (8 for DIR64, 4 for HIGHLOW) must fit in SizeOfImage. Otherwise the load fails with "relocation at … outside the image" and the mapping is freed.
  • crates/xcl/src/loader.rs (tests): relocations_outside_the_image_are_errors uses tiny_dll with its preferred address taken. Covers a target past the end, a DIR64 straddling the end, and a control target inside the image that is still relocated by the right delta.

Tested

  • RTX 4060 (driver 617.42), Intel Core i5-14400F, 32 GB RAM, Windows 11 24H2 (26100), Rust 1.100.0, debug
  • cargo test --workspace -- --test-threads=4: 652 passed, 0 failed, 40 ignored. SKIPPED GPU tests: gpu_wave32_program_in_wave64_hardware, gpu_geometry_wave32_prefix_count_over_a_mask (no wave64)
  • Without the check, the new test crashes the test process with STATUS_ACCESS_VIOLATION (0xc0000005). With the check it passes.

Regression run
Not run yet. I'll add both xcl-smoke blocks (main and this PR's head) and mark the PR ready once I have a title folder to run against. Until then it stays a draft.

Not tested

  • The scripts/smoke.ps1 regression run on a real title (see above)
  • AMD GPUs, and wave64 GPU tests (NVIDIA here)
  • No real title loads an image that hits the new error path. The regression run will only check that normal loads are unchanged.

AI use

  • Claude Code (Claude Opus 5.5) wrote the fix, the test and this description. I reviewed the diff and ran the tests above.

@mastrr9

mastrr9 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Independent static code audit of 227e1a01e (code inspection only — cargo test not run on macOS; no game files):

Summary: Looks solid

  • r.rva as usize + width > size catches both past-end targets and straddling DIR64 (8 B) / HIGHLOW (4 B) writes before dereferencing, and cleans up the mapping via fail(...).

Minor non-blocking notes

  • Using (r.rva as usize).checked_add(width).is_none_or(|end| end > size) makes overflow-safety explicit (already safe on 64-bit since r.rva is u32).
  • Pre-existing right below (loader.rs:121, 125): (v as i64 + delta) can panic on signed overflow in debug builds if hardening against crafted relocations (wrapping_add avoids that).

(AI-assisted)

@honestlyBroke
honestlyBroke marked this pull request as ready for review November 23, 2026 23:32
@honestlyBroke
honestlyBroke marked this pull request as draft November 23, 2026 23:32
…v#106)

The relocation entries come from the file, and map_image patched base + rva
without checking it against the mapping: a target past SizeOfImage, or a DIR64
whose 8 bytes straddle its end, wrote outside the image (an access violation
in the new test without the check). The check is in the loader rather than in
Pe::relocations, which also reads file-layout images and has no mapping size.
@honestlyBroke
honestlyBroke marked this pull request as ready for review November 23, 2026 23:40
@MesshM

MesshM commented Nov 24, 2026

Copy link
Copy Markdown

Test run of c1776d808d0490f4cda7b3e0e3c90c2c24435cdf on AMD RDNA4. I merged it onto current main 64b3f35 locally (clean merge) and tested that tree.

Tested

  • AMD Radeon RX 9060 XT 16 GB (RDNA4), driver 32.0.32029.2010; Ryzen 7 5700X, 64 GB RAM, Windows 11 Pro build 26200, Rust 1.100.0 stable-x86_64-pc-windows-msvc, debug
  • cargo build --workspace + cargo test --workspace -- --test-threads=4 on the merge: 702 passed, 0 failed, 41 ignored, exit code 0; xcl lib tests 661 passed, 0 failed, 21 ignored
  • 0 SKIPPED; relocations_outside_the_image_are_errors passes.

Review: A relocation whose target plus width (8 for DIR64, 4 for HIGHLOW) goes past SizeOfImage now fails the load instead of writing outside the mapping; ABSOLUTE entries have width 0 and pass as before. I found nothing to change.

Not tested: no retail title or regression run (I have no unpacked game), no XCL_SAMPLE_* tests, release build; the PR's head on its old base was not run, only its merge onto main.

Run and review by Claude Code (Claude Opus 5.5) on this machine; the review points were checked against the code.

@einfachama

Copy link
Copy Markdown
Contributor

Regression run on a retail title for #291, since Not tested lists it: head c1776d808d0490f4cda7b3e0e3c90c2c24435cdf merged onto main d208e14 (clean merge, local merge commit b0de7d3). Normal loads are unchanged: my retail title loads its 8 images (6 of them relocated) exactly as on main and runs at its first menu as before.

Tested

  • NVIDIA GeForce RTX 4060 8 GB (Ada, 32-lane waves), driver 621.24; Ryzen 7 5800X; 32 GB RAM; Windows 11 Pro 26300; Rust 1.100.0 MSVC; DXC 1.9.2602.17; debug for the tests, release for the title runs.
  • cargo test --workspace --no-fail-fast -- --test-threads=4: 705 passed, 0 failed, 41 ignored (main d208e14 in its clean run: 704 / 0 / 41). SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware.
  • My retail title, scripts/smoke.ps1 -Seconds 60, unattended, the learned window in both runs. XCL's log shows the same image loads on both (8 loaded, 6 relocated, no relocation error):
xcl-smoke v1
commit: d208e14b765f469f4b56301b78afa8dac5378339
gpu: NVIDIA GeForce RTX 4060
title: title
seconds: 60
exit: 0x0
frames shown: 1616 (PresentX 1616)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14635 MB
xcl-smoke v1
commit: b0de7d37625c46a02e809d2b46ce83fd2db801a4
gpu: NVIDIA GeForce RTX 4060
title: title
seconds: 60
exit: 0x0
frames shown: 1605 (PresentX 1605)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14578 MB

The second block is the local merge onto main, not the PR's head commit, so it can't stand in for the PR's own Regression run (pr_check.py wants the head commit there). It shows that real loads are unchanged here.

Not tested

  • The new error path with a real image (none of my title's images hits it, as the PR says).
  • The PR at its own base; anything past the title's first menu.

AI use

  • Claude Code with Claude Opus 5.5 ran the builds, tests and title runs in an unattended session and wrote this report from the logs; I checked it before posting.

@Toons29

Toons29 commented Nov 25, 2026

Copy link
Copy Markdown

Regression run of c1776d80 on the retail title, for this PR's Regression run section. Both blocks are from scripts/smoke.ps1 as printed, not edited: main on 4178e888, this PR on its head commit with no uncommitted changes, same title and time.

xcl-smoke v1
commit: 4178e888c0b5be50a2887eda45cdb832763b68a4
gpu: AMD Radeon RX 9070
title: game.exe
seconds: 60
exit: 0x0
frames shown: 1618 (PresentX 1618)
presents per 2 s (last 3): 61 60 60
device removals: 0
crashes: 0
peak commit: 14831 MB
xcl-smoke v1
commit: c1776d808d0490f4cda7b3e0e3c90c2c24435cdf
gpu: AMD Radeon RX 9070
title: game.exe
seconds: 60
exit: 0x0
frames shown: 1458 (PresentX 1458)
presents per 2 s (last 3): 60 60 61
device removals: 0
crashes: 0
peak commit: 14901 MB

Against the limits in CONTRIBUTING.md: 90 % of main's frames, 100 % of its peak commit, no crash, no device removal.

AMD Radeon RX 9070 (RDNA4, driver 32.0.32029.2010), Ryzen 7 7800X3D, 32 GB, Windows 11 build 26300, Rust 1.100.0, cargo build --release -p xcl, 60 s, XCL_NETWORK=0. This is a regression check only: it does not show whether the run reaches the code this PR changes.

AI use: Claude Code (Claude Opus 5.5) built both trees and ran scripts/smoke.ps1; I checked the blocks before posting.

@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

Thanks!

rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 26, 2026
…ge relocations fail (port upstream xcl-dev#1416 + xcl-dev#291, loader pair)

Windows loader.rs: verbatim xcl-dev#1416 (section_protect_range, wrapping_sub/
wrapping_add delta incl. TLS adj, checked_add section copies, header clamp)
+ xcl-dev#291 (rva+width>size guard). 3 upstream tests land verbatim.

Linux loader_linux.rs twin: wrapping u64 delta in apply_relocations
(unknown-kind guard now runs before warn-continue, matching upstream),
checked_add section copies. 2 Linux-shaped twin tests (high-base wrapping,
outside-image errors); protect_image already clamped via saturating_add.

Failing-first: grafted high-base test on pre-fix tree FAILED (attempt to
subtract with overflow); green post-fix.
rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 26, 2026
…stream xcl-dev#1367)

Ports live head 8dbc465 (still OPEN vs upstream/main). pe.rs: directory
descriptor/block sizes bounded by remaining dir data; export AddressOf
tables bounded by dir+image; thunk slot/entry RVAs + name strings
checked_add-bounded with size_of_image clamps; Mapped rva_to_offset/tail
image clamps; all 5 parser tests as-is. loader.rs: section va+mapped
outside-image fail; import IAT-slot and delay-HMODULE outside-image
fails. Loader reloc guards + page-span clamp already on linux (xcl-dev#291 /
section_protect_range helper) — not re-ported.

Failing-first: truncated_directories_are_rejected FAILED pre-fix;
Mapped-layout probe proved past-dir-end acceptance pre-fix (removed).
Gates: both checks 0 errors, lib 737/0/1 (+5), title e2e 12 GPU-only
FAILs, retail HOLDS at +0x2237320.
Comment thread crates/xcl/src/loader.rs
// the preferred address is taken, so the image is relocated
let blocker = unsafe { VirtualAlloc(BASE as *const c_void, 0x10000, MEM_RESERVE, PAGE_NOACCESS) };
assert!(!blocker.is_null());
for (target, name) in [(0x10_0000u32, "xcl_reloc_past_end.dll"), (0x1FFC, "xcl_reloc_straddles_end.dll")] {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this regression also emit an IMAGE_REL_BASED_HIGHLOW entry whose 4-byte target straddles SizeOfImage? The bound check has a separate width-4 branch for HIGHLOW, but with_reloc currently hardcodes DIR64, so the new boundary test only exercises the 8-byte path. This focused case would guard the 4-byte relocation boundary too. AI-assisted review: I inspected the diff and existing test; I did not run tests.

@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Triage (#1607) – head c1776d8

Overlap: #1367 (opened later) adds the same width-aware relocation bounds check in loader.rs map_image_ex, along with wider PE parser, import and section bounds checks. It has no loader test of the relocation path. This PR is first and its test relocations_outside_the_image_are_errors forces relocation and checks both the rejected and the patched case.
No problems found in the diff. On main the relocation loop still writes base + r.rva without a bounds check.
Recommendation: keep open.

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

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.

8 participants