Skip to content

Model loaders: size, offset and shape checks cannot wrap or reach past the file - #84

Merged
phantoslayer21 merged 1 commit into
mainfrom
fix/66-model-loader-bounds
Nov 13, 2026
Merged

phantoslayer21 merged 1 commit into
mainfrom
fix/66-model-loader-bounds

Conversation

@phantoslayer21

Copy link
Copy Markdown
Owner

Fixes #66

What was wrong

Several bounds checks in the model loaders were written in a form that can overflow, or that allowed a range past the end of the file. Only a crafted model file whose whole-file SHA-256 has been recomputed can reach them (the hash is unkeyed), so this is a robustness fix, not a problem in normal play. The effect would be a crash at load or a GPU fault.

Runtime model (clean/runtime/model.cpp, load_wgcm_model):

  • isize > d.size() let the index reader's end (88 + isize) lie up to 88 bytes behind the file buffer, and Reader::need only checked against that end.
  • Tensor shapes were read as int and multiplied into a size_t: a zero or negative dimension, or a product that wraps, could match a small size, and ten.f.resize(n) was then asked for a huge size (an uncaught std::length_error / std::bad_alloc inside the game process).

RR model.wgcm (clean/rr_f/runtime/src/rr_model.cpp, read_wgcm):

  • the element count and count * dtype size were multiplied without overflow checks, and off + size > body can wrap, so a wrapped range passed and tn.data = d.data() + off and the digest update read a wild pointer before the digest was compared.

RR own container (load_own, same file):

  • off + size > isize can wrap; the offsets become GPU root offsets, so an out-of-range blob could cause a GPU page fault.
  • o + isize + 32 != d.size() can be satisfied by a wrapped isize when o > d.size() - 32 (the tables end behind the file), which then builds a reversed iterator range.

The fix

  • model.cpp: reject if isize > d.size() - 32 - 88 (d.size() >= 120 is already known); Reader::need checks end <= d.size(), pos <= end and n <= end - pos, so no sum can wrap; each dimension must be above 0 and n > d.size() / dim is refused before the multiplication, so the element count never wraps and never exceeds the file size.
  • rr_model.cpp read_wgcm: the element count is checked while it is multiplied (a step that would exceed the data size is refused), and the range check is off % 64 || off < end || off > body || size > body - off || size != count * dtype size. A refused entry gets the existing "bad data range" message.
  • load_own: if (off > isize || size > isize - off) and if (o > d.size() - 32 || isize != d.size() - 32 - o || isize % 256).

Nothing else changed: the checks that were already correct (tensor data ranges by subtraction in model.cpp, the 88 + index_size case, the table check in load_own) are untouched.

The tests

CPU only, crafted files with a correct whole-file hash:

  • clean/tests/test_model_filter.py, new class LoaderBounds (uses the existing test_model_filter.cpp driver and model.cpp): isize = d.size() - 40 and other too-large index sizes give "bad header"; a shape of 2^30 x 2^30 x 4 elements (the size wraps to 0), negative dimensions (-1 x -4 = 4 elements) and a zero dimension give "bad tensor entry"; a control file with a plain shape still goes as far as the stage digest.
  • clean/rr_f/runtime/tests/test_model_bounds.cpp and test_model_bounds.py (new; read_wgcm and load_own through load_model, built with MSVC cl together with rr_model.cpp; a stub stands in for the packer of rr_pack.cpp, which needs the generated tables; one case per process because a loader that reads through a wrapped range may crash): off + size wrapping to a small value, a size taken from a (2^32-1)^2 shape at an offset above 2^33, a shape whose element count wraps to 0 bytes, a blob range that wraps, and an image whose start lies behind the end of the file. Controls: a valid model.wgcm and a valid WGRRMOD1 still load.

Before / after (MSVC; python -m unittest clean.tests.test_model_filter.LoaderBounds clean.rr_f.runtime.tests.test_model_bounds from a Visual Studio developer prompt):

  • main plus the tests only: 12 tests, 4 + 5 fail, the 3 controls pass. isize case: main answers "OK" for the cut file; wrapping shape: the test program dies with an uncaught exception; negative and zero shapes: main reaches the stage digest instead of refusing the entry; off + size wrap: main reaches the digest check (reading 64 bytes before the buffer); the (2^32-1)^2 case and the image-behind-the-file case crash (exit codes 0xC0000005 and 0xC0000409); the blob and element-count wraps are accepted.
  • with the fix: Ran 26 tests ... OK (all of test_model_filter plus the new module).

Tested

  • The commands above, before and after.
  • clean.tests.test_model_wgcm and clean.tests.test_runtime (they skip on Windows: "the lane emulator needs Linux", as in CI).
  • Repository gates (secrets, personal data, licence, view layout): pass.

Not tested

  • Not run under AddressSanitizer or with debug iterators (the issue suggests it); the new cases fail visibly on main by message or crash without them.
  • No GPU, no game, and no real model file: the loaders were exercised with crafted files only, so a real model.wgcm or WGRRMOD1 file was not loaded. The RR runtime DLL itself (kernels, DXC) was not rebuilt; rr_model.cpp was built into the test program only.
  • The new tests need MSVC cl and skip without it (the existing test_model_filter.py also needs a compiler).

…t the file

Runtime model (clean/runtime/model.cpp): the index size must leave room for the
88-byte header and the trailing hash (the reader's end could lie up to 88 bytes
behind the file buffer); Reader::need no longer lets a position sum wrap or an
end exceed the buffer; tensor dimensions must be above 0 and their product
stays below the file size before it is multiplied or resized.

RR model.wgcm (rr_model.cpp read_wgcm): the shape's element count is checked
while it is multiplied, and the data range is checked as off <= body and
size <= body - off.

RR own container (load_own): blob ranges and the image size are checked without
sums that can wrap.

Tests: crafted files with a correct whole-file hash (clean/tests/
test_model_filter.py LoaderBounds; clean/rr_f/runtime/tests/test_model_bounds).

Fixes #66

Signed-off-by: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com>
@phantoslayer21
phantoslayer21 marked this pull request as ready for review November 13, 2026 21:08
@phantoslayer21
phantoslayer21 merged commit 3d66154 into main Nov 13, 2026
2 checks passed
@phantoslayer21
phantoslayer21 deleted the fix/66-model-loader-bounds branch November 13, 2026 21:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Model loaders: size and offset checks can overflow or extend past the file

1 participant