Reject DirectStorage reads past EOF before allocating staging #1285

Manually merged
github merged 1 commit from gh-pr/1285/head into gh-pr/1285/base 2026-11-26 22:55:07 +00:00
Owner

@⁠R0Y00 on GitHub · 2026-11-24 06:46 UTC · original
R0Y00:codex/dstorage-bounded-file-read → main · +96 −6 · 6 files · 1 commits · merged 2026-11-24 06:52 UTC

Refs #1144.

A file request could allocate its entire declared source size before finding that the read was past EOF. The worker now obtains the current size from its open handle and checks both the offset and the remaining span before resizing the staging vector. A read ending exactly at EOF succeeds. ReadFile must still return the entire requested count before any destination write.

The new file-range check uses a five-byte temporary file. It checks the last three real bytes, a one-byte overrun, a request starting at EOF, a 64 MiB request, a wrapping 64-bit offset and a failed size query. All rejected cases preserve the size, capacity and contents of the staging vector. The existing source-built queue consumer now also checks the HRESULT and guarded destination for an exact-end read, an overrun and an empty request with an out-of-file offset.

Windows 11 25H2 build 26200.9974, MSVC Release, commit de0754bf:

ctest --test-dir out/bin --output-on-failure --no-tests=error -R '^startup_(storage_file_range|stubs)$'

Both tests passed in 0.86 seconds. No capability skips.

**@⁠R0Y00** on GitHub · 2026-11-24 06:46 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/1285) `R0Y00:codex/dstorage-bounded-file-read` → `main` · +96 −6 · 6 files · 1 commits · merged 2026-11-24 06:52 UTC Refs #1144. A file request could allocate its entire declared source size before finding that the read was past EOF. The worker now obtains the current size from its open handle and checks both the offset and the remaining span before resizing the staging vector. A read ending exactly at EOF succeeds. ReadFile must still return the entire requested count before any destination write. The new file-range check uses a five-byte temporary file. It checks the last three real bytes, a one-byte overrun, a request starting at EOF, a 64 MiB request, a wrapping 64-bit offset and a failed size query. All rejected cases preserve the size, capacity and contents of the staging vector. The existing source-built queue consumer now also checks the HRESULT and guarded destination for an exact-end read, an overrun and an empty request with an out-of-file offset. Windows 11 25H2 build 26200.9974, MSVC Release, commit `de0754bf`: ```powershell ctest --test-dir out/bin --output-on-failure --no-tests=error -R '^startup_(storage_file_range|stubs)$' ``` Both tests passed in 0.86 seconds. No capability skips.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 22:55:06 +00:00
github manually merged commit de0754bfb7 into gh-pr/1285/base 2026-11-26 22:55:07 +00:00
Author
Owner

@⁠tedin7 on GitHub · 2026-11-24 06:52 UTC · original · reviewed

Reviewed at de0754bfb7b4560e8dd9d682b9f4b260b9ffb424.

Verification

Linux (worktree from origin/main):

  • git diff --check origin/main...HEAD — clean
  • python -B tests/docs_links_check.py — passed (16 Markdown files, 286 links)
  • python -B tests/bidi_controls_check.py — passed (1080 tracked files)

Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at de0754bfb7b4560e8dd9d682b9f4b260b9ffb424:

  • Debug (--preset dev):
    • Build (cmake --build --preset dev --target startup_storage_file_range startup_stub_consumer -- -j 6): 26.03s (configure 1.75s)
    • Test (ctest --preset dev -R 'startup_storage_file_range|startup_stub_consumer' --parallel 4 --no-tests=error --output-on-failure): 1/1 passed (0.09s: startup_storage_file_range 0.02s; startup_stub_consumer compiled/linked cleanly)
  • Release /O2 (--preset release):
    • Build (cmake --build --preset release --target startup_storage_file_range startup_stub_consumer -- -j 6): 38.30s (configure 1.51s)
    • Test (ctest --preset release -R 'startup_storage_file_range|startup_stub_consumer' --parallel 4 --no-tests=error --output-on-failure): 1/1 passed (0.08s: startup_storage_file_range 0.02s; startup_stub_consumer compiled/linked cleanly)

Findings

  • x2w_startup::read_storage_file (src/startup_stubs/dstorage_file_read.h) uses an overflow-safe bounds check (offset > size || count > size - offset) after GetFileSizeEx and before staging.resize(count), preventing large allocations on invalid offsets/counts.
  • tests/startup_storage_file_range.cpp and tests/startup_stub_consumer.cpp cover exact-EOF reads, out-of-range/overflowing offsets, INVALID_HANDLE_VALUE, preservation of staging contents/capacity on rejected ranges, and destination preservation on failed queue requests.
  • Minor note (non-blocking): staging is preserved when the pre-allocation bounds check fails, whereas if ReadFile itself fails or returns a short read (e.g., concurrent truncation) after staging.resize(count), staging will have been resized. Fine for xcrdapi.cpp's local packed vector, just worth keeping in mind if reused with persistent buffers.
**@⁠tedin7** on GitHub · 2026-11-24 06:52 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/1285#pullrequestreview-6034897205) · **reviewed** Reviewed at `de0754bfb7b4560e8dd9d682b9f4b260b9ffb424`. ### Verification **Linux** (worktree from `origin/main`): - `git diff --check origin/main...HEAD` — clean - `python -B tests/docs_links_check.py` — passed (16 Markdown files, 286 links) - `python -B tests/bidi_controls_check.py` — passed (1080 tracked files) **Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP)** at `de0754bfb7b4560e8dd9d682b9f4b260b9ffb424`: - **Debug (`--preset dev`)**: - Build (`cmake --build --preset dev --target startup_storage_file_range startup_stub_consumer -- -j 6`): `26.03s` (configure `1.75s`) - Test (`ctest --preset dev -R 'startup_storage_file_range|startup_stub_consumer' --parallel 4 --no-tests=error --output-on-failure`): **1/1 passed** (`0.09s`: `startup_storage_file_range` `0.02s`; `startup_stub_consumer` compiled/linked cleanly) - **Release `/O2` (`--preset release`)**: - Build (`cmake --build --preset release --target startup_storage_file_range startup_stub_consumer -- -j 6`): `38.30s` (configure `1.51s`) - Test (`ctest --preset release -R 'startup_storage_file_range|startup_stub_consumer' --parallel 4 --no-tests=error --output-on-failure`): **1/1 passed** (`0.08s`: `startup_storage_file_range` `0.02s`; `startup_stub_consumer` compiled/linked cleanly) ### Findings - `x2w_startup::read_storage_file` (`src/startup_stubs/dstorage_file_read.h`) uses an overflow-safe bounds check (`offset > size || count > size - offset`) after `GetFileSizeEx` and before `staging.resize(count)`, preventing large allocations on invalid offsets/counts. - `tests/startup_storage_file_range.cpp` and `tests/startup_stub_consumer.cpp` cover exact-EOF reads, out-of-range/overflowing offsets, `INVALID_HANDLE_VALUE`, preservation of `staging` contents/capacity on rejected ranges, and destination preservation on failed queue requests. - **Minor note (non-blocking):** `staging` is preserved when the pre-allocation bounds check fails, whereas if `ReadFile` itself fails or returns a short read (e.g., concurrent truncation) after `staging.resize(count)`, `staging` will have been resized. Fine for `xcrdapi.cpp`'s local `packed` vector, just worth keeping in mind if reused with persistent buffers.
This discussion has been locked. Commenting is limited to contributors.
No description provided.