Bound GGUF tensor data offsets against the file mapping - #4179
Open
robertomeroni wants to merge 1 commit into
Open
Bound GGUF tensor data offsets against the file mapping#4179robertomeroni wants to merge 1 commit into
robertomeroni wants to merge 1 commit into
Conversation
gguflib computes a tensor's data pointer as ctx->data + ctx->data_off + the tensor's offset field, in unsigned arithmetic and without comparing the result against the mapping. A crafted offset therefore produces a pointer outside the mapped file, and mlx's own memcpy in extract_tensor_data then reads it. If the addition wraps, the pointer lands back inside the mapping and the wrong bytes are read silently. Validate offset and size against the file in load_arrays, which is the single point both the plain and the quantized paths pass through.
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.
Proposed changes
Fixes #4136 (thanks @professor-moody for the report).
gguflibsetstensor->weights_data = ctx->data + ctx->data_off + *offsetfrom afile-controlled offset without bounding it against
ctx->size(gguflib.c:297-298), andmlx's
memcpyinextract_tensor_data(mlx/io/gguf.cpp:71) reads through it. A craftedoffset points outside the mapping; if the addition wraps it points back inside, so the load
silently returns wrong bytes instead of faulting.
This validates offset and extent in
load_arrays, the one point both the plain and thequantized paths pass through, using only
gguf_ctxfields mlx already has. mlx does the samea few lines away at
gguf.cpp:67(CVE-2025-62609) and for safetensors atsafetensors.cpp:196.It is still needed if gguflib is fixed upstream. The pending fix there,
antirez/gguf-tools#33, signals a rejected tensor with
return 0, which in awhile (gguf_get_tensor(...))loop is indistinguishable from "no tensors left". Patchingthe pinned gguflib with #33 locally, a 2-tensor file with one bad offset loads 1 tensor,
left_tensors == 0, no error — silently missing weights rather than a raise.Not addressed by #3436: the only assert on this path is
ndim(gguflib.c:275), so-UNDEBUGhas nothing to keep alive foroffset/bsize.Scope: this bounds the tensor data region against the file. It does not make GGUF loading
safe against arbitrary crafted files -- internally consistent but false metadata is a
separate class, not addressed here.
Without the guard two of the new subcases crash (SIGSEGV, or SIGBUS depending on where the
bad pointer lands) and four read wrong bytes without raising; all pass with it. The helper
follows
write_raw_safetensorsabove it.Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changesAI assistance was used in developing this change (Opus 5); I reviewed every changed line and
ran the commands above myself.