Skip to content

gguf-py: validate n_dims and guard uint64 overflow in reader - #25401

Merged
ggerganov merged 1 commit into
ggml-org:masterfrom
hclsys:fix/gguf-reader-dims-validation
Aug 4, 2026
Merged

ggerganov merged 1 commit into
ggml-org:masterfrom
hclsys:fix/gguf-reader-dims-validation

Conversation

@hclsys

@hclsys hclsys commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor

Overview

The Python GGUF reader is missing two guards the C++ loader already has, so a crafted model file can crash or OOM anything using it (convert scripts, third-party consumers):

  • n_dims is read as uint32 with no GGML_MAX_DIMS bound — a file claiming e.g. 1000000 dims triggers an oversized memmap read.
  • int(np.prod(dims)) on uint64 wraps at 2^64, so dims like [4194305, 4194305, 211106198978564] collapse to 4 and an undersized read slips through.

Added the GGML_MAX_DIMS check and switched the element count to a plain Python int product. Two crafted files that used to crash-on-reshape / parse-with-4-elements now raise a clean ValueError.

Additional information

Fixes #25378. Added tests/test_gguf_reader_validation.py covering both cases; full gguf-py suite passes.

Requirements

The Python GGUF reader lacked two guards the C++ loader has:
- n_dims read as uint32 with no GGML_MAX_DIMS bound -> crafted file with
  huge n_dims triggers oversized memmap read / OOM.
- np.prod(dims) on uint64 wraps silently -> a crafted dims triple can
  overflow to a tiny element count, passing an undersized read through.

Add a GGML_MAX_DIMS check and compute the element count with Python ints.

Fixes ggml-org#25378
@hclsys
hclsys requested a review from CISC as a code owner July 7, 2026 11:37
@ggml-gh-bot

This comment was marked as resolved.

@CISC CISC added the merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. label Aug 4, 2026
@ggerganov
ggerganov merged commit 5788b51 into ggml-org:master Aug 4, 2026
5 checks passed
satindergrewal pushed a commit to satindergrewal/llama.cpp that referenced this pull request Aug 12, 2026
…gml-org#25401)

The Python GGUF reader lacked two guards the C++ loader has:
- n_dims read as uint32 with no GGML_MAX_DIMS bound -> crafted file with
  huge n_dims triggers oversized memmap read / OOM.
- np.prod(dims) on uint64 wraps silently -> a crafted dims triple can
  overflow to a tiny element count, passing an undersized read through.

Add a GGML_MAX_DIMS check and compute the element count with Python ints.

Fixes ggml-org#25378
brittlewis12 pushed a commit to brittlewis12/llama.cpp that referenced this pull request Aug 17, 2026
…gml-org#25401)

The Python GGUF reader lacked two guards the C++ loader has:
- n_dims read as uint32 with no GGML_MAX_DIMS bound -> crafted file with
  huge n_dims triggers oversized memmap read / OOM.
- np.prod(dims) on uint64 wraps silently -> a crafted dims triple can
  overflow to a tiny element count, passing an undersized read through.

Add a GGML_MAX_DIMS check and compute the element count with Python ints.

Fixes ggml-org#25378
thecodacus pushed a commit to thecodacus/llama.cpp that referenced this pull request Sep 7, 2026
…gml-org#25401)

The Python GGUF reader lacked two guards the C++ loader has:
- n_dims read as uint32 with no GGML_MAX_DIMS bound -> crafted file with
  huge n_dims triggers oversized memmap read / OOM.
- np.prod(dims) on uint64 wraps silently -> a crafted dims triple can
  overflow to a tiny element count, passing an undersized read through.

Add a GGML_MAX_DIMS check and compute the element count with Python ints.

Fixes ggml-org#25378
zbrad pushed a commit to zbrad/llama.cpp that referenced this pull request Sep 10, 2026
…gml-org#25401)

The Python GGUF reader lacked two guards the C++ loader has:
- n_dims read as uint32 with no GGML_MAX_DIMS bound -> crafted file with
  huge n_dims triggers oversized memmap read / OOM.
- np.prod(dims) on uint64 wraps silently -> a crafted dims triple can
  overflow to a tiny element count, passing an undersized read through.

Add a GGML_MAX_DIMS check and compute the element count with Python ints.

Fixes ggml-org#25378
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

conversion merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python GGUF parser (gguf_reader.py) lacks dimension validation present in C++ parser — DoS / crash on malicious model files

3 participants