game: check record bounds and offset arithmetic in SGD5 parsing (fix on a fork) #905

Open
opened 2026-11-26 04:14:08 +00:00 by github-bot · 2 comments
Owner

@⁠DaniiL35 on GitHub · 2026-11-24 13:51 UTC · original

Pull requests are currently restricted to collaborators (#717), so I am sharing this bug report and verified fix from a clean fork branch off main (ffe310e).

• Branch: https://github.com/DaniiL35/VIctory/tree/fix/sgd5-checked-record-bounds
• Commit: 7a5db56 (based on main ffe310e, crates/victory-game/src/sgd5.rs +53/-9)
• Compare: https://github.com/Marvisak/VIctory/compare/main...DaniiL35:VIctory:fix/sgd5-checked-record-bounds

Problem

In crates/victory-game/src/sgd5.rs, record() extracts packed shader container records from memory/archive slices by searching for the marker 0x000D_DCE7. When verifying sizes and extracting the compressed stream:

  1. Slicing offsets (at + 12..at + n) and header offsets (at + 8, at + 4) used unchecked usize addition, which can panic on integer overflow with hostile/corrupt inputs near memory boundaries.
  2. Direct index operations (file[at - 5], file[nul]) relied on unchecked indexing rather than slice bounds checks. If at was located near boundary conditions (such as invalid offsets where at < 6), slice index panics or underflows could occur.
  3. name_before used unchecked indexing file[l] when scanning backwards for the name length byte.

Fix

  1. Replaced raw arithmetic with checked_add and checked_sub throughout record():
    • start = at.checked_add(12)? and end = at.checked_add(n)? with validation that start <= end.
    • u32_at computes o.checked_add(4)? safely.
  2. Replaced direct indexing with bounds-checked .get() (file.get(nul) == Some(&0), file.get(at.checked_sub(5)?)?).
  3. Guarded name_before with .get() on both length extraction and substring slice.

Tests Added

Added rejects_boundary_and_malformed_records in crates/victory-game/src/sgd5.rs covering:

  • Marker located near the start of the buffer (at < 6), asserting None without panic.
  • Header declaring n < 12, asserting None.
  • Header where size cross-checks disagree (u32_at(at + 4) != n), asserting None.
  • Record with unpacked_size > MAX_UNPACKED (64 MiB), asserting None.
  • Truncated payload where n exceeds available buffer length, asserting None.

Verification & Policy Compliance

  • Pure synthetic byte slices; no game assets or SDK binaries.
  • Complies with Rule #1 (Asset Policy) and passes cargo xtask policy (text-only, <512 KiB).
  • Formatted with Rust 2024 edition.
  • Clean diff on top of main (ffe310e).

AI-assisted with Antigravity.

**@⁠DaniiL35** on GitHub · 2026-11-24 13:51 UTC · [original](https://github.com/Marvisak/VIctory/issues/905) Pull requests are currently restricted to collaborators (#717), so I am sharing this bug report and verified fix from a clean fork branch off `main` (`ffe310e`). • **Branch**: https://github.com/DaniiL35/VIctory/tree/fix/sgd5-checked-record-bounds • **Commit**: `7a5db56` (based on `main` `ffe310e`, `crates/victory-game/src/sgd5.rs` +53/-9) • **Compare**: https://github.com/Marvisak/VIctory/compare/main...DaniiL35:VIctory:fix/sgd5-checked-record-bounds ### Problem In `crates/victory-game/src/sgd5.rs`, `record()` extracts packed shader container records from memory/archive slices by searching for the marker `0x000D_DCE7`. When verifying sizes and extracting the compressed stream: 1. Slicing offsets (`at + 12..at + n`) and header offsets (`at + 8`, `at + 4`) used unchecked `usize` addition, which can panic on integer overflow with hostile/corrupt inputs near memory boundaries. 2. Direct index operations (`file[at - 5]`, `file[nul]`) relied on unchecked indexing rather than slice bounds checks. If `at` was located near boundary conditions (such as invalid offsets where `at < 6`), slice index panics or underflows could occur. 3. `name_before` used unchecked indexing `file[l]` when scanning backwards for the name length byte. ### Fix 1. Replaced raw arithmetic with `checked_add` and `checked_sub` throughout `record()`: - `start = at.checked_add(12)?` and `end = at.checked_add(n)?` with validation that `start <= end`. - `u32_at` computes `o.checked_add(4)?` safely. 2. Replaced direct indexing with bounds-checked `.get()` (`file.get(nul) == Some(&0)`, `file.get(at.checked_sub(5)?)?`). 3. Guarded `name_before` with `.get()` on both length extraction and substring slice. ### Tests Added Added `rejects_boundary_and_malformed_records` in `crates/victory-game/src/sgd5.rs` covering: - Marker located near the start of the buffer (`at < 6`), asserting `None` without panic. - Header declaring `n < 12`, asserting `None`. - Header where size cross-checks disagree (`u32_at(at + 4) != n`), asserting `None`. - Record with `unpacked_size > MAX_UNPACKED` (`64 MiB`), asserting `None`. - Truncated payload where `n` exceeds available buffer length, asserting `None`. ### Verification & Policy Compliance - Pure synthetic byte slices; no game assets or SDK binaries. - Complies with Rule #1 (Asset Policy) and passes `cargo xtask policy` (text-only, <512 KiB). - Formatted with Rust 2024 edition. - Clean diff on top of `main` (`ffe310e`). AI-assisted with Antigravity.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:14:08 +00:00
Author
Owner

@⁠DaniiL35 on GitHub · 2026-11-24 16:17 UTC · original

PR opened directly: #1153

**@⁠DaniiL35** on GitHub · 2026-11-24 16:17 UTC · [original](https://github.com/Marvisak/VIctory/issues/905#issuecomment-7137416572) PR opened directly: #1153
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 06:53 UTC · original

Automated triage for the maintainers.

Reading record in crates/victory-game/src/sgd5.rs on main, the panics described cannot happen: at.checked_sub(4)? and nul = at.checked_sub(6)? return None for a marker within six bytes of the start, so file[nul] and file[at - 5] are always in bounds (both below at, which is inside the file); u32_at and the payload use file.get(..), which returns None for a truncated or reversed range; and at + n cannot overflow for an offset inside a slice. name_before only indexes below nul. So the change would be hardening, not a fix. Pull request #1153 that carried it was closed without being merged; if you want it considered as hardening, a new pull request is open to everyone.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 06:53 UTC · [original](https://github.com/Marvisak/VIctory/issues/905#issuecomment-7155269990) <!-- victory-issue-triage v1 verdict=answered seen=2026-11-24T16:17:16Z at=2026-11-25T06:53:43Z --> Automated triage for the maintainers. Reading `record` in `crates/victory-game/src/sgd5.rs` on `main`, the panics described cannot happen: `at.checked_sub(4)?` and `nul = at.checked_sub(6)?` return `None` for a marker within six bytes of the start, so `file[nul]` and `file[at - 5]` are always in bounds (both below `at`, which is inside the file); `u32_at` and the payload use `file.get(..)`, which returns `None` for a truncated or reversed range; and `at + n` cannot overflow for an offset inside a slice. `name_before` only indexes below `nul`. So the change would be hardening, not a fix. Pull request #1153 that carried it was closed without being merged; if you want it considered as hardening, a new pull request is open to everyone. --- _Generated by [Claude Code](https://claude.ai/code)_
This discussion has been locked. Commenting is limited to contributors.
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#905
No description provided.