Replace assert() with runtime check for tensor ndim bounds#22
Open
MillaFleurs wants to merge 1 commit intoantirez:mainfrom
Open
Replace assert() with runtime check for tensor ndim bounds#22MillaFleurs wants to merge 1 commit intoantirez:mainfrom
MillaFleurs wants to merge 1 commit intoantirez:mainfrom
Conversation
gguf_get_tensor() validates tensor->ndim against GGUF_TENSOR_MAX_DIM using assert(), which is compiled out in release builds (NDEBUG). A crafted GGUF file with ndim > 8 causes the subsequent dimension-reading loop to write past the end of the fixed-size dim[8] array, corrupting adjacent struct members and stack memory. Replace the assert() with a proper runtime check that returns 0 with tensor->name = NULL, following the existing error convention used for invalid tensor types (line 290). The GGUF spec (ggml/docs/gguf.md) states tensor dimensions are "currently at most 4" and GGUF_TENSOR_MAX_DIM is already set to 8 for future-proofing. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Author
|
Opening after this was found to cause an issue in MLX. MLX pr: ml-explore/mlx#3358 |
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.
Summary
gguf_get_tensor()validatestensor->ndimagainstGGUF_TENSOR_MAX_DIMusingassert(), which is compiled out in release builds (NDEBUG). A crafted GGUF file withndim > 8causes the subsequent dimension-reading loop to write past the end of the fixed-sizedim[GGUF_TENSOR_MAX_DIM]array, corrupting adjacent struct members (offset,bsize,num_weights,weights_data) and stack memory beyond the struct.Details
Current code (gguflib.c, line 275):
The problem: In release builds,
assert()expands to nothing. The uncheckedndimvalue flows into:Relevant spec language (ggml/docs/gguf.md):
GGUF_TENSOR_MAX_DIMis already set to 8 for future-proofing ("Future-proof: actual limit is 4"). The wire format usesuint32_tforn_dimensions, so a malicious file can declare up to 2^32 dimensions.Fix
Replace the
assert()with a runtime check, following the existing error convention used for invalid tensor types at line 290 (if (*type >= GGUF_TYPE_COUNT) return 0):This maintains API compatibility — callers already handle
return 0withtensor->name == NULLas an error/termination condition (documented in the function comment at lines 251-254).Impact
Any application using gguflib to load untrusted GGUF files in release builds is affected. Known downstream consumers include Apple MLX, which vendors gguflib at commit
8fa6eb65.Test plan
-DNDEBUGand load a GGUF file withndim=32— should return 0 instead of OOB writeassert()removal doesn't break debug builds (theifcheck is strictly stronger)🤖 Generated with Claude Code