fix: reject invalid n_dims in tensor header to prevent stack-buffer-overflow - #220
Open
shafiuzzaman-md wants to merge 1 commit into
Open
fix: reject invalid n_dims in tensor header to prevent stack-buffer-overflow#220shafiuzzaman-md wants to merge 1 commit into
shafiuzzaman-md wants to merge 1 commit into
Conversation
Author
|
The 3 failing checks (ios, emscripten, ubuntu-latest-run) are unrelated to this change. Each fails before reaching the modified code:
The two checks that actually compile and run the changed code (windows-msys2 CLANG64 and UCRT64) both pass. Happy to help fix the CI separately if useful, but wanted to flag that these failures predate and are unrelated to this PR. |
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.
Problem
bark_model_load()(bark.cpp:1019) reads a tensor header'sn_dimsfield as a rawint32_tdirectly from the model file and uses it, unbounded, as the loop count writing into a fixed-size 2-element stack arrayint32_t ne[2]:A model file with
n_dims > 2writes past thene[2]array on the stack. Reproduced with a crafted 144-byte model file (n_dims = 20) through the stockmain -m <file> -p "text"command line, under an AddressSanitizer build:The identical copy-pasted loop also exists in
ggml_quantize_weights(the offlinequantizetool,bark.cpp:357,int32_t ne[4]), fixed here too.Fix
Add an explicit
n_dimsrange check immediately after it is read, before the loop that populatesne[], at both sites sharing this pattern:For normal, well-formed model files this is a no-op; a malformed file is now rejected with a clear error message instead of corrupting the stack.
Testing
n_dims=20) crashes with an ASan stack-buffer-overflow WRITE atbark_model_load(bark.cpp:1019).n_dims(2) passes the new check unaffected (fails later only because the synthetic test file has no real tensor data, unrelated to this fix).mainandquantizetargets build cleanly with the change.