fix: reject invalid n_dims in tensor header to prevent stack-buffer-overflow - #57
Open
shafiuzzaman-md wants to merge 1 commit into
Open
fix: reject invalid n_dims in tensor header to prevent stack-buffer-overflow#57shafiuzzaman-md wants to merge 1 commit into
shafiuzzaman-md wants to merge 1 commit into
Conversation
Add validation for n_dims to ensure it is between 1 and 3.
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
encodec_load_model_weights()(encodec.cpp:447) 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 3-element stack arrayint32_t ne[3] = {1, 1, 1}:A model file with
n_dims > 3writes past thene[3]array on the stack. Reproduced with a crafted 132-byte model file (n_dims = 20) through the real publicencodec_load_model()API, under an AddressSanitizer build:Fix
Add an explicit
n_dimsrange check immediately after it is read, before the loop that populatesne[]: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 atencodec_load_model_weights(encodec.cpp:447).n_dims(3) passes the new check unaffected (fails lateronly because the synthetic test file has no real tensor data, unrelated to this fix).
encodec_load_model()APIbuild and run cleanly with the change.