Repository navigation
Harden GGUF and safetensors readers with eager limit and bounds checks (GHSA-v8r9-hqm6-68cf) - #89
Merged
Merged
Conversation
…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.
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
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/safetensorsso 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/ggufmodels/gguf/gguf.go): Track file size viaf.Stat()and validatekvCount,tensorCount, string lengths, and numeric/bool/string array counts against remaining file bytes before allocating; cap eager slice preallocation (maxInitialSliceCap); validategeneral.alignmentagainst negative/oversized values andint64overflow.models/gguf/tensor_type.go): Usemath/bits.Mul64inNumElements,checkedNumElements,checkedNumBytes,validateTensorShapeAndLimits, andvalidateTensorInFileto reject zero/excessive ranks, dimensions exceedingmath.MaxInt, element/byte overflows, quantized innermost dims not divisible byBlockSize, raw/dequantized sizes exceedingmin(64 GiB, math.MaxInt), and tensor spans exceeding the file's data section—enforced eagerly ingguf.Open.models/gguf/reader.go): Validate every tensor against the file size and limits before allocating inReadTensor,ReadTensorRaw,ReadTensorBytes, andreadQuantizedTensor, checkgetDequantFuncbefore allocating the output tensor, and finalize tensors on read/dequant errors.models/safetensorsmodels/safetensors/header.go,models/safetensors/model.go): CheckfileSize >= 8and0 < headerSize <= min(100 MiB, fileSize - 8)before allocating the JSON header buffer; reject"INVALID"/"InvalidDType"indtypeToGoMLX; validate rank (<= 64), non-negative dimensions, element/byte overflow, allocation limits (min(64 GiB, math.MaxInt)), overflow-safedata_offsets, and exact byte-size agreement both inTensorMetadata.GoMLXShape()and eagerly inparseHeaderFromReaderWithSize.models/safetensors/reader.go,models/safetensors/iter.go,models/safetensors/safetensors.go): Validate relativedata_offsetsagainst[0, dataSize - dataOffset]withoutint64overflow in sharedresolveTensorRange(preventing negative relative offsets from reading header JSON bytes), open/stat files once before header parsing inNewTensorReaderanditerFromRepoDownload, guardreadTensorFromFileAt, and cap sharded index size inLoadShardedModel.Tests & Verification
models/gguf/limits_test.go(including the exact GHSA-v8r9-hqm6-68cf PoC) andmodels/safetensors/limits_test.go.go test ./models/gguf/... ./models/safetensors/...,go test -short ./...,go vet ./..., and 32-bitGOARCH=386test compilation.docs/CHANGELOG.md.