gguf: fix GGML_PAD integer overflow via crafted general.alignment (CWE-190)#2201
Open
Gzhao-redpoint wants to merge 1 commit into
Open
gguf: fix GGML_PAD integer overflow via crafted general.alignment (CWE-190)#2201Gzhao-redpoint wants to merge 1 commit into
Gzhao-redpoint wants to merge 1 commit into
Conversation
…(CWE-190) JavaScript bitwise operators (`&`, `~`) truncate to 32-bit signed integers. When `general.alignment >= 2^31`, `GGML_PAD(offset, alignment)` overflows, producing negative values that corrupt `tensorDataOffset` and cause `RangeError` in serialization paths. This bypasses the validation added in PR huggingface#2028 because values like `alignment = 2147483648` pass the positive-integer check but overflow in bitwise operations. Fix: - Add power-of-2 validation (matching llama.cpp `gguf.cpp` line 612) - Add upper bound `MAX_ALIGNMENT = 2^30` (largest safe power-of-2 for JS bitwise ops) - Validate in all three entry points: `gguf()`, `serializeGgufMetadata()`, and `buildGgufHeader()` Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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
Fixes an integer overflow in
GGML_PADthat can be triggered by a crafted GGUF file withgeneral.alignment >= 2^31, bypassing the validation added in #2028.Root Cause
JavaScript bitwise operators (
&,~) truncate operands to 32-bit signed integers. Whenalignment >= 2^31:A malicious GGUF with
general.alignment = 2147483648(UINT32) passes all existing validation (positive, integer, within safe integer range) but produces a negativetensorDataOffset.Impact
gguf()parser (line 509)tensorDataOffset= negative BigInt → corrupted tensor data offsetserializeGgufMetadata()(line 710)new Uint8Array(negative)→RangeError(DoS)buildGgufHeader()(line 801)padLen→ corrupted header orRangeErrorFix
gguf.cppline 612:(alignment & (alignment - 1)) !== 0MAX_ALIGNMENT = 2^30— the largest power-of-2 safe for JS bitwise operationsgguf(),serializeGgufMetadata(),buildGgufHeader()Reproduction
CWE
Test plan
pnpm run testinpackages/gguf)Note
Medium Risk
Targets malicious/crafted GGUF input and header rebuild paths; behavior change rejects some alignment values that previously passed basic integer checks.
Overview
Hardens GGUF alignment handling so crafted
general.alignmentvalues cannot breakGGML_PADvia JavaScript’s 32-bit bitwise truncation (CWE-190).Introduces
MAX_ALIGNMENT(2³⁰) and rejects alignments that are not powers of two or exceed that cap. The same rules apply when parsing metadata ingguf()and whenalignmentis passed intoserializeGgufMetadata()andbuildGgufHeader(), closing paths that previously yielded negativetensorDataOffset, invalid buffer sizes, or corrupted headers.Reviewed by Cursor Bugbot for commit e828ae6. Bugbot is set up for automated code reviews on this repo. Configure here.