fix: reject invalid n_dims in tensor header to prevent stack-buffer-overflow on malformed model files - #3957
Open
shafiuzzaman-md wants to merge 2 commits into
Open
Conversation
…verflow on malformed model files
Add error handling for invalid n_dims in model file.
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
whisper_model_load(and the identical copy-pasted loader logic inwhisper_vad_init_with_paramsandparakeet_model_load) reads a tensor header'sn_dimsfield as a rawint32_tfrom the model file and uses it, unbounded, as the loop count writing into a fixed-size 4-element stack array:A model file with
n_dims > 4writes past thene[4]array on the stack. Reproduced with a crafted 152-byte model file (n_dims = 20) through the stockwhisper-cli -m <file>, under an AddressSanitizer build:This is the sole loader for whisper.cpp's native
.binggml-format models, reachable from the ordinary, documentedwhisper_init_from_file_with_paramsAPI on any user-supplied or downloaded model file. The identical unbounded loop also exists inwhisper_vad_init_with_params(the separate Voice Activity Detection model loader) andparakeet_model_load(NVIDIA Parakeet ASR support, vendored in this repo).Fix
Add an explicit
n_dimsrange check immediately after it is read, before the loop that populatesne[], at all three sites sharing this pattern:For normal, well-formed model files (
0 <= n_dims <= 4) 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 atwhisper_model_load(src/whisper.cpp:1885).n_dims(e.g. 2) passes the new check unaffected and proceeds to the next validation stage as before.whisper-cliandparakeet-clibuild cleanly with the change.