Repository navigation
Model loaders: size, offset and shape checks cannot wrap or reach past the file - #84
Merged
Merged
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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, andReader::needonly checked against that end.intand multiplied into asize_t: a zero or negative dimension, or a product that wraps, could match a smallsize, andten.f.resize(n)was then asked for a huge size (an uncaughtstd::length_error/std::bad_allocinside the game process).RR
model.wgcm(clean/rr_f/runtime/src/rr_model.cpp,read_wgcm):count * dtype sizewere multiplied without overflow checks, andoff + size > bodycan wrap, so a wrapped range passed andtn.data = d.data() + offand the digest update read a wild pointer before the digest was compared.RR own container (
load_own, same file):off + size > isizecan 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 wrappedisizewheno > d.size() - 32(the tables end behind the file), which then builds a reversed iterator range.The fix
model.cpp: reject ifisize > d.size() - 32 - 88(d.size() >= 120is already known);Reader::needchecksend <= d.size(),pos <= endandn <= end - pos, so no sum can wrap; each dimension must be above 0 andn > d.size() / dimis refused before the multiplication, so the element count never wraps and never exceeds the file size.rr_model.cppread_wgcm: the element count is checked while it is multiplied (a step that would exceed the data size is refused), and the range check isoff % 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)andif (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, the88 + index_sizecase, the table check inload_own) are untouched.The tests
CPU only, crafted files with a correct whole-file hash:
clean/tests/test_model_filter.py, new classLoaderBounds(uses the existingtest_model_filter.cppdriver andmodel.cpp):isize = d.size() - 40and 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.cppandtest_model_bounds.py(new;read_wgcmandload_ownthroughload_model, built with MSVCcltogether withrr_model.cpp; a stub stands in for the packer ofrr_pack.cpp, which needs the generated tables; one case per process because a loader that reads through a wrapped range may crash):off + sizewrapping 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 validmodel.wgcmand a valid WGRRMOD1 still load.Before / after (MSVC;
python -m unittest clean.tests.test_model_filter.LoaderBounds clean.rr_f.runtime.tests.test_model_boundsfrom a Visual Studio developer prompt):isizecase: 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 + sizewrap: 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.Ran 26 tests ... OK(all oftest_model_filterplus the new module).Tested
clean.tests.test_model_wgcmandclean.tests.test_runtime(they skip on Windows: "the lane emulator needs Linux", as in CI).Not tested
model.wgcmor WGRRMOD1 file was not loaded. The RR runtime DLL itself (kernels, DXC) was not rebuilt;rr_model.cppwas built into the test program only.cland skip without it (the existingtest_model_filter.pyalso needs a compiler).