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

Open
github-bot wants to merge 1 commit from github-bot/gh-3208 into main AGit
Owner

@⁠cloph-dsp on GitHub · 2026-11-26 12:46 UTC · original
cloph-dsp:fix/pe-dir-offset → main · +54 −2 · 1 files · 1 commits · open

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.

**@⁠cloph-dsp** on GitHub · 2026-11-26 12:46 UTC · [original](https://github.com/Marvisak/VIctory/pull/3208) `cloph-dsp:fix/pe-dir-offset` → `main` · +54 −2 · 1 files · 1 commits · open 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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 12:50:09 +00:00
Author
Owner

@⁠AurumCarlos on GitHub · 2026-11-26 12:57 UTC · original · reviewed

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.

**@⁠AurumCarlos** on GitHub · 2026-11-26 12:57 UTC · [original](https://github.com/Marvisak/VIctory/pull/3208#pullrequestreview-6074507610) · **reviewed** 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.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/3208/head:github-bot/gh-3208'
git switch 'github-bot/gh-3208'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3208'
git switch 'github-bot/gh-3208'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3208'
git switch 'github-bot/gh-3208'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3208'
git switch 'main'
git merge --squash 'github-bot/gh-3208'
git switch 'main'
git merge --ff-only 'github-bot/gh-3208'
git switch 'main'
git merge 'github-bot/gh-3208'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!3208
No description provided.