8000 Harden GGUF and safetensors readers with eager limit and bounds checks (GHSA-v8r9-hqm6-68cf) by janpfeifer · Pull Request #89 · gomlx/go-huggingface · GitHub
[go: up one dir, main page]

Skip to content

Harden GGUF and safetensors readers with eager limit and bounds checks (GHSA-v8r9-hqm6-68cf) - #89

Merged
janpfeifer merged 1 commit into
mainfrom
harden-gguf-safetensors
Nov 28, 2026
Merged

janpfeifer merged 1 commit into
mainfrom
harden-gguf-safetensors

Conversation

@janpfeifer
Copy link
Copy Markdown
Contributor

Summary

Addresses security advisory https://github.com/gomlx/go-huggingface/security/advisories/GHSA-v8r9-hqm6-68cf (GHSA-v8r9-hqm6-68cf) and applies equivalent hardening to models/safetensors so malformed or crafted model files fail eagerly with an error rather than panicking (makeslice: len out of range, shapes.Make, DType.GoType) or triggering unbounded allocations.

models/gguf

  • Eager header & metadata bounds (models/gguf/gguf.go): Track file size via f.Stat() and validate kvCount, tensorCount, string lengths, and numeric/bool/string array counts against remaining file bytes before allocating; cap eager slice preallocation (maxInitialSliceCap); validate general.alignment against negative/oversized values and int64 overflow.
  • Overflow-safe tensor validation (models/gguf/tensor_type.go): Use math/bits.Mul64 in NumElements, checkedNumElements, checkedNumBytes, validateTensorShapeAndLimits, and validateTensorInFile to reject zero/excessive ranks, dimensions exceeding math.MaxInt, element/byte overflows, quantized innermost dims not divisible by BlockSize, raw/dequantized sizes exceeding min(64 GiB, math.MaxInt), and tensor spans exceeding the file's data section—enforced eagerly in gguf.Open.
  • Reader defense-in-depth (models/gguf/reader.go): Validate every tensor against the file size and limits before allocating in ReadTensor, ReadTensorRaw, ReadTensorBytes, and readQuantizedTensor, check getDequantFunc before allocating the output tensor, and finalize tensors on read/dequant errors.

models/safetensors

  • Eager header & metadata validation (models/safetensors/header.go, models/safetensors/model.go): Check fileSize >= 8 and 0 < headerSize <= min(100 MiB, fileSize - 8) before allocating the JSON header buffer; reject "INVALID" / "InvalidDType" in dtypeToGoMLX; validate rank (<= 64), non-negative dimensions, element/byte overflow, allocation limits (min(64 GiB, math.MaxInt)), overflow-safe data_offsets, and exact byte-size agreement both in TensorMetadata.GoMLXShape() and eagerly in parseHeaderFromReaderWithSize.
  • Reader & iterator bounds (models/safetensors/reader.go, models/safetensors/iter.go, models/safetensors/safetensors.go): Validate relative data_offsets against [0, dataSize - dataOffset] without int64 overflow in shared resolveTensorRange (preventing negative relative offsets from reading header JSON bytes), open/stat files once before header parsing in NewTensorReader and iterFromRepoDownload, guard readTensorFromFileAt, and cap sharded index size in LoadShardedModel.

Tests & Verification

  • Added regression suites in models/gguf/limits_test.go (including the exact GHSA-v8r9-hqm6-68cf PoC) and models/safetensors/limits_test.go.
  • Verified go test ./models/gguf/... ./models/safetensors/..., go test -short ./..., go vet ./..., and 32-bit GOARCH=386 test compilation.
  • Updated docs/CHANGELOG.md.

…s (GHSA-v8r9-hqm6-68cf)

Addresses https://github.com/gomlx/go-huggingface/security/advisories/GHSA-v8r9-hqm6-68cf.

- models/gguf: validate header counts, string/array lengths, alignment, tensor dimensions, overflow, block alignment, allocation limits, and data-section bounds eagerly in Open and defensively before allocating in Reader.ReadTensor/ReadTensorRaw/ReadTensorBytes/readQuantizedTensor.
- models/safetensors: validate file/header sizes before allocating, reject INVALID dtypes, check rank/non-negative dims/overflow/allocation limits in GoMLXShape and eagerly in parseHeaderFromReaderWithSize, validate relative data_offsets without int64 overflow in resolveTensorRange, and cap sharded index reads.
- Add regression tests in models/gguf/limits_test.go and models/safetensors/limits_test.go, and update docs/CHANGELOG.md.
@janpfeifer
janpfeifer merged commit 8d21089 into main Nov 28, 2026
5 checks passed
@janpfeifer
janpfeifer deleted the harden-gguf-safetensors branch November 28, 2026 09:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

0