pe: bound data directory offset by optional header size #3206

Open
cloph-dsp wants to merge 1 commit from upstream-pr/3208 into main
cloph-dsp commented 2026-11-26 12:46:59 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by cloph-dsp on 2026-11-26T12:46:59Z.


Depends on: #1633 (claim PE-DIR-OFFSET; earlier PR #2531 was closed unmerged).

data_directory_offset checked only NumberOfRvaAndSizes and the file length. A malformed header could declare more directories than SizeOfOptionalHeader holds, making callers read past the optional header. The function now caps the directory count by the declared optional-header size and refuses entries whose 8 bytes run past that size or the end of the buffer.

Test: layout::tests::data_directory_offset_respects_size_of_optional_header builds a synthetic PE32+ prefix with a two-entry optional header and checks the boundary, an inflated count and a truncated buffer.

Validation (Windows, Rust 1.100.0/MSVC, worktree-local CARGO_TARGET_DIR):

  • cargo test --locked -p victory-pe --lib — 37 passed, 0 failed.
  • git diff --check — clean.
  • Negative control: the new test name is absent from origin/main.

AI-assisted (Hy4 flash). Synthetic inputs only; no game assets, extracted shaders, logs or DRM material were used or uploaded.

<!-- greyfinch-sync:Marvisak/VIctory:issue:5510283680 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3208). Originally posted by [cloph-dsp](https://github.com/cloph-dsp) on 2026-11-26T12:46:59Z. --- Depends on: #1633 (claim `PE-DIR-OFFSET`; earlier PR #2531 was closed unmerged). `data_directory_offset` checked only `NumberOfRvaAndSizes` and the file length. A malformed header could declare more directories than `SizeOfOptionalHeader` holds, making callers read past the optional header. The function now caps the directory count by the declared optional-header size and refuses entries whose 8 bytes run past that size or the end of the buffer. Test: `layout::tests::data_directory_offset_respects_size_of_optional_header` builds a synthetic PE32+ prefix with a two-entry optional header and checks the boundary, an inflated count and a truncated buffer. Validation (Windows, Rust 1.100.0/MSVC, worktree-local `CARGO_TARGET_DIR`): - `cargo test --locked -p victory-pe --lib` — 37 passed, 0 failed. - `git diff --check` — clean. - Negative control: the new test name is absent from `origin/main`. AI-assisted (Hy4 flash). Synthetic inputs only; no game assets, extracted shaders, logs or DRM material were used or uploaded.
AurumCarlos commented 2026-11-26 12:57:24 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by AurumCarlos on 2026-11-26T12:57:24Z.

Review: COMMENTED


The bound is right: NumberOfRvaAndSizes alone shouldn't decide where the table ends, and capping it by (SizeOfOptionalHeader - OPT_DATA_DIRECTORY) / 8 matches how the optional header is laid out. A few remarks on the shape of the check:

1. The last three checks can't fire. After the first guard, opt + size <= data.len(). With count <= room, we get end = table + count * 8 <= opt + OPT_DATA_DIRECTORY + room * 8 <= opt + size. And since index < count, at + 8 <= end. So end > opt + size, end > data.len() and at + 8 > data.len() are always false, and the function could end with:

let count = count.min(room);
if index >= count {
    return None;
}
opt.checked_add(OPT_DATA_DIRECTORY)?
    .checked_add(index.checked_mul(DATA_DIRECTORY_SIZE)?)

That's the original tail, with just the size guard and the cap added above it. It's not wrong as written, but a reader will look for the case each check handles and not find one.

2. Because of that, the test's last case is not testing what its comment says. cut ends at opt + OPT_DATA_DIRECTORY + 8, while size declares two entries, so it's rejected by opt + size > data.len() at the top, not by an entry "that runs past the end of the buffer". The assertion holds, but the comment describes a path that can't be reached. Something like "Nor is a buffer that ends inside the declared optional header" would match.

That first guard is also a small behaviour change worth stating: a buffer that ends inside the optional header now yields None for every index, even entries that are fully present. On main, a buffer cut right after entry 0 still returned entry 0. All in-tree callers pass whole files (prepare.rs, image.rs), so nothing breaks, but it's the kind of thing the doc comment on data_directory_offset could mention.

3. Verification scope. The description runs victory-pe only, but victory-loader/src/prepare.rs also calls data_directory_offset (L70, L82, L124, and two tests that unwrap() it on the test binary). They should pass, since a real linker writes a correct SizeOfOptionalHeader, but a cargo test -p victory-loader run would confirm it.

<!-- greyfinch-sync:Marvisak/VIctory:review:6074507610 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3208#pullrequestreview-6074507610). Originally posted by [AurumCarlos](https://github.com/AurumCarlos) on 2026-11-26T12:57:24Z. **Review:** COMMENTED --- The bound is right: `NumberOfRvaAndSizes` alone shouldn't decide where the table ends, and capping it by `(SizeOfOptionalHeader - OPT_DATA_DIRECTORY) / 8` matches how the optional header is laid out. A few remarks on the shape of the check: **1. The last three checks can't fire.** After the first guard, `opt + size <= data.len()`. With `count <= room`, we get `end = table + count * 8 <= opt + OPT_DATA_DIRECTORY + room * 8 <= opt + size`. And since `index < count`, `at + 8 <= end`. So `end > opt + size`, `end > data.len()` and `at + 8 > data.len()` are always false, and the function could end with: ```rust let count = count.min(room); if index >= count { return None; } opt.checked_add(OPT_DATA_DIRECTORY)? .checked_add(index.checked_mul(DATA_DIRECTORY_SIZE)?) ``` That's the original tail, with just the `size` guard and the cap added above it. It's not wrong as written, but a reader will look for the case each check handles and not find one. **2. Because of that, the test's last case is not testing what its comment says.** `cut` ends at `opt + OPT_DATA_DIRECTORY + 8`, while `size` declares two entries, so it's rejected by `opt + size > data.len()` at the top, not by an entry "that runs past the end of the buffer". The assertion holds, but the comment describes a path that can't be reached. Something like "Nor is a buffer that ends inside the declared optional header" would match. That first guard is also a small behaviour change worth stating: a buffer that ends inside the optional header now yields `None` for *every* index, even entries that are fully present. On `main`, a buffer cut right after entry 0 still returned entry 0. All in-tree callers pass whole files (`prepare.rs`, `image.rs`), so nothing breaks, but it's the kind of thing the doc comment on `data_directory_offset` could mention. **3. Verification scope.** The description runs `victory-pe` only, but `victory-loader/src/prepare.rs` also calls `data_directory_offset` (L70, L82, L124, and two tests that `unwrap()` it on the test binary). They should pass, since a real linker writes a correct `SizeOfOptionalHeader`, but a `cargo test -p victory-loader` run would confirm it.
Jackster commented 2026-11-26 23:02:24 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Jackster on 2026-11-26T23:02:24Z.


Windows 11, 63bcca4 + this PR:

  • cargo test -p victory-pe: 37 passed / 0 failed (unpatched 63bcca4 here: 36).
  • cargo clippy -p victory-pe --all-targets -- -D warnings: clean.

data_directory_offset_respects_size_of_optional_header passes, and the other victory-pe tests are unchanged. Synthetic only. AI-assisted.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7205200795 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3208#issuecomment-7205200795). Originally posted by [Jackster](https://github.com/Jackster) on 2026-11-26T23:02:24Z. --- Windows 11, `63bcca4` + this PR: - `cargo test -p victory-pe`: 37 passed / 0 failed (unpatched `63bcca4` here: 36). - `cargo clippy -p victory-pe --all-targets -- -D warnings`: clean. `data_directory_offset_respects_size_of_optional_header` passes, and the other `victory-pe` tests are unchanged. Synthetic only. AI-assisted.
futurepro62 commented 2026-11-27 22:50:32 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by futurepro62 on 2026-11-27T22:50:32Z.


NVIDIA full-suite run, to go with the reports above by Jackster, since none of them names a GPU run. Tested exact head 8363aaa (on its base 63bcca4).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), dxil.dll from Windows SDK 10.0.26100 (the loader's default), debug.
  • cargo build --workspace --locked, then cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware: 867 passed, 0 failed, 10 ignored.
  • The new tests ran and passed: data_directory_offset_respects_size_of_optional_header.
  • --skip leaves out tests that fail on main 63bcca4 on this card too, not because of this PR: heap_direct_texture_reads_mark_their_heap_index ends the victory-shader test process with 0xc0000005 (#2571), so without the skip the rest of that crate never reports; dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views and the two stride_tests hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); gathers_at_a_level_match_the_hardware fails on lane 0 (#1744).

Not tested

  • AMD (the tests take the default adapter), a release build, the title (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7229660904 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3208#issuecomment-7229660904). Originally posted by [futurepro62](https://github.com/futurepro62) on 2026-11-27T22:50:32Z. --- NVIDIA full-suite run, to go with the reports above by Jackster, since none of them names a GPU run. Tested exact head `8363aaa` (on its base `63bcca4`). **Tested** - NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), `dxil.dll` from Windows SDK 10.0.26100 (the loader's default), debug. - `cargo build --workspace --locked`, then `cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware`: **867 passed, 0 failed, 10 ignored**. - The new tests ran and passed: `data_directory_offset_respects_size_of_optional_header`. - `--skip` leaves out tests that fail on main `63bcca4` on this card too, not because of this PR: `heap_direct_texture_reads_mark_their_heap_index` ends the `victory-shader` test process with `0xc0000005` (#2571), so without the skip the rest of that crate never reports; `dynamic_slots_are_held_until_released`, `lists_share_a_table_and_copies_keep_their_views` and the two `stride_tests` hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); `gathers_at_a_level_match_the_hardware` fails on lane 0 (#1744). **Not tested** - AMD (the tests take the default adapter), a release build, the title (test run only). **AI use** - Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3208:upstream-pr/3208'
git switch 'upstream-pr/3208'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3208'
git switch 'upstream-pr/3208'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3208'
git switch 'upstream-pr/3208'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3208'
git switch 'main'
git merge --squash 'upstream-pr/3208'
git switch 'main'
git merge --ff-only 'upstream-pr/3208'
git switch 'main'
git merge 'upstream-pr/3208'
git push origin 'main'
Sign in to join this conversation.
No reviewers
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
Marvisak/VIctory!3206
No description provided.