Reject DirectStorage reads past EOF before allocating staging #1278

Merged
R0Y00 merged 1 commit from upstream-pr/1285 into main 2026-11-24 06:52:23 +00:00
R0Y00 commented 2026-11-24 06:46:31 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T06:46:31Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6576276820 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1285). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-24T06:46:31Z. --- 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.
tedin7 commented 2026-11-24 06:52:47 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T06:52:47Z.

Review: COMMENTED


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.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6034897205 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1285#pullrequestreview-6034897205). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T06:52:47Z. **Review:** COMMENTED --- 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.
upstream-sync closed this pull request 2026-11-24 07:00:43 +00:00
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
R0Y00/Xbox2Windows!1278
No description provided.