Out-of-bounds read in GGUF model loading: tensor data offsets are not validated against the file size on the default mmap path

Author: 3em0Created Sep 24, 2026Updated Sep 24, 2026
Labelsbug-unconfirmed

Prerequisites

Before submitting your issue, please ensure the following:

Expected Behavior

When loading a GGUF model whose tensor data does not actually lie inside the file — for example a truncated file, or a file whose header fields have been corrupted — the loader should reject the model with a clear error before any tensor data is used, on all load paths, including the default mmap path (--no-mmap not passed).

Current Behavior

On the default load path (use_mmap = true, common/common.h:123), the model loader computes each tensor's file offset by adding two file-controlled header fields with no containment check, and then points the tensor's data pointer directly into the memory mapping at that offset. A model file can therefore be crafted so that a tensor's data lies outside the file (and outside the mapping) while parsing and loading still succeed; the out-of-bounds access only surfaces later, when the tensor is consumed — either as a silent read of unrelated process memory or as a crash.

Concretely, at commit 8bd56d69906c9d2dba4d3bf6899763401e01a9a4 (currently main HEAD):

  • gguf_init_from_file (ggml.c:19465) validates each tensor entry only against the type's block size (ne % blck_size, ggml.c:19658-19665). Nothing in the GGUF parser checks that a tensor's data region falls within the file.
  • file_offset() (llama.cpp:2116-2124) returns gguf_get_data_offset(ctx_gguf) + gguf_get_tensor_offset(ctx_gguf, idx) (llama.cpp:2123) — both fields come straight from the file, and their sum is used as-is.
  • load_data_for() (llama.cpp:2126-2135) takes that offset and, on the default mmap path, assigns cur->data = (uint8_t *) mapping->addr + offs (llama.cpp:2129-2130) with no check that offs + ggml_nbytes(cur) stays within the file or the mapping. The non-mmap branch (llama.cpp:2131-2133) fails closed — read_raw throws on a short read — but the default path has no equivalent guard.

The result is an out-of-bounds read primitive driven entirely by the model file: it is reachable by simply running inference on a crafted .gguf file, which is the standard way model files are shared in this ecosystem.

This primitive is not new to the llama.cpp lineage: current llama.cpp contains a fix, and a copy of that fixed loader is present inside this very repository at smallthinker/src/llama-model-loader.h:41-45, which validates offs + ggml_nbytes(tensor) against file->size() and throws "tensor '%s' data is not within the file bounds, model is corrupted or incomplete". The main loader in llama.cpp predates that hardening.

Environment and Context

Please provide detailed information about your computer setup. This is important in case the issue is not reproducible except for under certain specific conditions.

The defect itself was audited and re-verified at main HEAD 8bd56d69906c9d2dba4d3bf6899763401e01a9a4 (no tagged versions exist). Dynamic validation was performed in an isolated offline container (Debian, clang 19.1.7 with AddressSanitizer), exercising a verbatim-extracted slice of the affected loader code with the repository's own GGUF parser compiled as-is; no GPU stack was involved (see Failure Information for the honest scope of what was and was not run).

  • Physical (or virtual) hardware you are using, e.g. for Linux:

$ lscpu

[placeholder: lscpu output]
  • Operating System, e.g. for Linux:

$ uname -a

[placeholder: uname -a output]
  • SDK version, e.g. for Linux:
$ python3 --version
$ make --version
$ g++ --version
[placeholder: SDK version output]

Failure Information (for bugs)

A minimal GGUF v3 fixture whose tensor header carries an oversized data offset is accepted by the parser and the loader without any error. The behavior was validated with harmless sentinel content (distinguishable filler bytes and marker strings placed outside the file's real data) — no exploit, no payload, nothing executable is attached to this report. Observed results, all against a benign control fixture that differs by a single header field:

  • Benign control: clean load, and the tensor reads back exactly the file's real bytes.
  • Offset within the mapping but past the end of the file: the tensor silently reads memory that is not part of the model file (sentinel filler placed at the mapping tail was returned).
  • Offset reaching an adjacent mapping: the tensor reads bytes from memory completely outside the file (sentinel marker string returned).
  • Very large offset (1 TiB class): the tensor silently reads unrelated memory of the host process (allocator fill pattern returned) — in a real victim process, whatever neighbors the model mapping at that address is disclosed.
  • Offset past the end of the mapping: AddressSanitizer reports a deterministic heap-buffer-overflow READ.
  • Non-canonical 64-bit offset: SEGV — the loading process dies.

Two honest scope statements, at the same granularity as the claims above. What was exercised: the offset arithmetic and the mmap pointer assignment (llama.cpp:2123, :2129-2130) executed verbatim, with the repository's own GGUF parser (gguf_init_from_file, ggml.c:19465) compiled unmodified to parse the fixtures. What was not exercised: the full llama_model_loader translation unit against a real model file descriptor, the complete application entry chain, the GPU/CUDA stack, and a real victim process's address space layout — so the exact bytes disclosed next to a model mapping in a production run were not demonstrated. The crash and silent-read manifestations were both observed at the harness level; the primitive itself (no containment on the default mmap path) is static fact at the cited lines.

Steps to Reproduce

  1. Build a minimal GGUF v3 file with one tensor, and fill the tensor's data region with recognizable filler bytes.
  2. Set the header's data_offset (or the tensor's offset field) so that data_offset + tensor_offset exceeds the file size, while leaving everything else byte-identical to the benign version of the same file.
  3. Run PowerInfer's main (or any entry that loads a GGUF model) on that file with default flags — i.e. do not pass --no-mmap, so the default use_mmap = true path (common/common.h:123) is taken.
  4. Observe that loading succeeds with no error, and the tensor's data pointer points outside the file (llama.cpp:2129-2130); the out-of-bounds read surfaces when the tensor is used.
  5. For contrast, run the same entry with --no-mmap on the same file: the non-mmap branch fails closed with a read error, which is the behavior the default path should also have.

We are not attaching a fixture generator or any executable artifact; the header-field change in step 2 is the entire mechanism.

Failure Logs

Please include any relevant log snippets or files. If it works under one configuration but not under another, please provide logs for both configurations and their corresponding outputs so it is easy to see where behavior changes. Also, please try to avoid using screenshots if at all possible — everything below is copy/pasted console output.

The validation was run twice, offline, with byte-identical results. The two configurations requested above map to the default mmap load (fails open on the crafted file) and the --no-mmap load (fails closed), so logs for both are included.

Default mmap path — the crafted file is accepted without any warning:

[placeholder: loader log excerpt from the validation dossier, showing load success on the
oversized-offset fixture under default flags]

Default mmap path — sanitizer summary for the far-offset crash case:

[placeholder: ASan summary lines from the validation dossier — e.g. the heap-buffer-overflow
READ case. Paste only the summary lines, not the full fixture dump.]

--no-mmap contrast — the same file fails closed:

[placeholder: loader log excerpt showing the non-mmap read error on the same fixture]