GPU/list+draw: fix integer overflows and unchecked buffer bounds in resolve_queries, execute_indirect, and rectangles #480

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

@⁠AscilCH on GitHub · 2026-11-25 01:22 UTC · original
AscilCH:fix/gpu-command-bounds-overflow-checks → main · +186 −13 · 2 files · 1 commits · open

Problem

Three GPU command-recording bounds checks in crates/victory-gpu/src/list.rs and crates/victory-gpu/src/draw.rs used unchecked or narrow integer arithmetic:

  1. list::resolve_queries (list.rs:1362): if offset + u64::from(count) * bytes > size used unchecked u64 arithmetic. A large offset (e.g. u64::MAX - 4 with count = 1) panicked in debug builds and wrapped around in release builds to pass the guard, calling data.add(offset as usize) + defer_write out of bounds.
  2. list::execute_indirect (list.rs:1696-1697): buffer_bytes(arguments).map(|(p, _)| p as u64 + offset) and buffer_bytes(count).map(|(p, _)| p as u64 + count_offset) discarded buffer size completely and used unchecked +, allowing out-of-bounds or wrapping offsets to reach into an adjacent buffer's Shadow.
  3. draw::State::rectangles (draw.rs:2181-2189, 2212): u64::from(first + count) * stride > u64::from(v.size) added first + count in u32 before widening to u64 (wrapping to 0 on e.g. 0x8000_0000 + 0x8000_0000), computed v.address + u64::from(first) * stride unchecked, and truncated (rects * 6 * stride) as u32 into SizeInBytes.

Fix

  • Use checked_mul and checked_add in list::resolve_queries.
  • Add checked_buffer_offset(buffer, offset, len) in list.rs and validate both arguments (stride * max.max(1)) and count (4) against their buffer sizes in execute_indirect.
  • Extract rect_vertex_span in draw.rs widening u64::from(first) + u64::from(count), using checked arithmetic for address/byte spans, and validating u32::try_from(dst_bytes).
  • Add synthetic unit tests resolve_queries_and_execute_indirect_reject_overflowing_offsets and rect_vertex_span_rejects_overflow_and_out_of_bounds.

Verification

  • cargo test -p victory-gpu --locked list::tests -- --test-threads=1: 2 passed, 0 failed
  • cargo test -p victory-gpu --locked draw::tests -- --test-threads=1: 1 passed, 0 failed
  • cargo xtask policy: 169 files ok
  • cargo clippy -p victory-gpu --all-targets --locked -- -D warnings: clean
  • cargo clippy -p victory-gpu --all-targets --all-features --locked -- -D warnings: clean

(AI-assisted; synthetic unit tests only, no game data.)

**@⁠AscilCH** on GitHub · 2026-11-25 01:22 UTC · [original](https://github.com/fxceless/victory-fork/pull/480) `AscilCH:fix/gpu-command-bounds-overflow-checks` → `main` · +186 −13 · 2 files · 1 commits · open ## Problem Three GPU command-recording bounds checks in `crates/victory-gpu/src/list.rs` and `crates/victory-gpu/src/draw.rs` used unchecked or narrow integer arithmetic: 1. **`list::resolve_queries` (`list.rs:1362`)**: `if offset + u64::from(count) * bytes > size` used unchecked `u64` arithmetic. A large `offset` (e.g. `u64::MAX - 4` with `count = 1`) panicked in debug builds and wrapped around in release builds to pass the guard, calling `data.add(offset as usize)` + `defer_write` out of bounds. 2. **`list::execute_indirect` (`list.rs:1696-1697`)**: `buffer_bytes(arguments).map(|(p, _)| p as u64 + offset)` and `buffer_bytes(count).map(|(p, _)| p as u64 + count_offset)` discarded buffer `size` completely and used unchecked `+`, allowing out-of-bounds or wrapping offsets to reach into an adjacent buffer's `Shadow`. 3. **`draw::State::rectangles` (`draw.rs:2181-2189, 2212`)**: `u64::from(first + count) * stride > u64::from(v.size)` added `first + count` in `u32` before widening to `u64` (wrapping to `0` on e.g. `0x8000_0000 + 0x8000_0000`), computed `v.address + u64::from(first) * stride` unchecked, and truncated `(rects * 6 * stride) as u32` into `SizeInBytes`. ## Fix - Use `checked_mul` and `checked_add` in `list::resolve_queries`. - Add `checked_buffer_offset(buffer, offset, len)` in `list.rs` and validate both `arguments` (`stride * max.max(1)`) and `count` (`4`) against their buffer sizes in `execute_indirect`. - Extract `rect_vertex_span` in `draw.rs` widening `u64::from(first) + u64::from(count)`, using checked arithmetic for address/byte spans, and validating `u32::try_from(dst_bytes)`. - Add synthetic unit tests `resolve_queries_and_execute_indirect_reject_overflowing_offsets` and `rect_vertex_span_rejects_overflow_and_out_of_bounds`. ## Verification - `cargo test -p victory-gpu --locked list::tests -- --test-threads=1`: 2 passed, 0 failed - `cargo test -p victory-gpu --locked draw::tests -- --test-threads=1`: 1 passed, 0 failed - `cargo xtask policy`: 169 files ok - `cargo clippy -p victory-gpu --all-targets --locked -- -D warnings`: clean - `cargo clippy -p victory-gpu --all-targets --all-features --locked -- -D warnings`: clean *(AI-assisted; synthetic unit tests only, no game data.)*
1. `list::resolve_queries`: replace unchecked `offset + u64::from(count) * bytes > size` with checked multiplication and addition so wrapping offsets cannot panic in debug builds or bypass the buffer bounds check in release builds and schedule an out-of-bounds `defer_write`.
2. `list::execute_indirect`: validate `arguments` (`[offset, offset + stride * max.max(1))`) and `count` (`[count_offset, count_offset + 4)`) against each buffer's recorded byte size using checked arithmetic (`checked_buffer_offset`), preventing out-of-bounds or wrapping offsets from aliasing adjacent `Shadow` buffers.
3. `draw::State::rectangles`: widen `u64::from(first) + u64::from(count)` before multiplying by `stride` (avoiding `u32` wrap on `first + count`), use checked addition for `v.address + first * stride`, and check `u32::try_from(rects * 6 * stride)` so `D3D12_VERTEX_BUFFER_VIEW::SizeInBytes` cannot silently truncate.

Add synthetic unit tests in `list::tests` and `draw::tests`.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:44:19 +00:00
This pull request has changes conflicting with the target branch.
  • crates/victory-gpu/src/draw.rs
  • crates/victory-gpu/src/list.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

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

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-480'
git switch 'github-bot/gh-480'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-480'
git switch 'github-bot/gh-480'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-480'
git switch 'main'
git merge --squash 'github-bot/gh-480'
git switch 'main'
git merge --ff-only 'github-bot/gh-480'
git switch 'main'
git merge 'github-bot/gh-480'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.