Repository navigation
Conversation
gguf_tensor_to_f16 allocates num_weights * sizeof(int16_t), where num_weights is the 64-bit product of the tensor dimensions. For a BF16 (or other non-equivalent) tensor with num_weights >= 2^63 the size wraps to a small value while the dequant loop still writes the full num_weights elements, a heap-buffer-overflow during load. Reject such tensors before the conversion.
This branch has not been deployed
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.
Bug Report: GGUF dequantization
mallocsize integer overflow → heap out-of-bounds write (SIGSEGV)1.
tensor->num_weightsis user-controllabletensor->num_weightsis the product of the tensor's dimensions in the GGUF file. The dimensions are byte-for-byte controllableuint64values in the file, sonum_weightsis fully user-controlled.Crafted malicious GGUF layout
Chain (num_weights → malloc argument = 0)
gguf_get_tensor()(vendoredgguflib.c:284)tensor->num_weights *= *dim;→
num_weights = 2^31 × 2^32 = 2^63(uint64, does not overflow on its own).BF16 has no "equivalent dtype", so
load_arrays()routes it throughextract_tensor_data()(mlx/io/gguf.cpp:99) →gguf_tensor_to_f16(tensor).gguflib.c:956num_weights × sizeof(int16_t)=2^63 × 2=2^64, which is0modulo2^64insize_t, so the actual call ismalloc(0).Conclusion: with
dims = [2^31, 2^32]andtype = BF16(30), themallocargument atgguflib.c:956is 0.(Note:
tensor->bsizeatgguflib.c:310also wraps to 0 via the samenum_weights × 2,so the
check_tensor_in_file()file-bounds validation added in v0.32.1 is bypassed as well.)2. Continued writes → crash
mallocreturns a tiny chunk, but the dequantization loop's write bound is the unwrappednum_weights(2^63):gguflib.c:965gguf_bf16_to_float(weights_data, f16, tensor->num_weights, gguf_store_f16_callback);gguflib.c:530f16[idx] = to_half(f);,idx ∈ [0, 2^63)A total of
2^63float16 elements =2^64bytes are written → heap out-of-bounds write → SIGSEGV (insidemx.load()).Crash log (real machine, mlx 0.32.4)
ASAN heap-buffer-overflow trace (Debug + AddressSanitizer, pointing to the exact write site)
Key point:
0 bytes after 1-byte region— under ASAN,malloc(0)actually returns a 1-byte region,and the very first
WRITE of size 2immediately overflows it, direct proof of "allocating 0 bytes but writing 2 bytes of data".(The addresses vary between runs due to ASLR and can be ignored.)
3. Unit test + run commands
Test (in
tests/load_tests.cpp, reusing the existingwrite_raw_gguf_dims()helper):Run commands
Release build + run the single test:
AddressSanitizer build: