gguf-py: bound array element count by remaining file size - #28131
Draft
wmshearer wants to merge 1 commit into
Draft
gguf-py: bound array element count by remaining file size#28131wmshearer wants to merge 1 commit into
wmshearer wants to merge 1 commit into
Conversation
The array branch of `_get_field_parts` validates the declared element
count against `GGUF_MAX_ARRAY_ELEMENTS` but never against the bytes
actually remaining in the file. A count below that ceiling still drives a
full per-element loop, so a small file that declares a large array costs
time proportional to the declared count rather than to the file size.
`_get_str` already performs the equivalent check for strings:
if offset + 8 + int(slen[0]) > self.data.nbytes:
raise ValueError(f'String length ... exceeds remaining file size ...')
This adds the matching bound for arrays. Every element occupies at least
one byte, so a declared count greater than the remaining byte count is
not satisfiable and can be rejected before the loop starts.
Found by fuzzing gguf-py with Atheris. A 101-byte input declaring
13,434,883 elements took 43.9s of CPU to parse on master (2d8d612);
with this change it raises immediately. A normal file with a 5,000
element array parses unchanged.
Testing:
- 101-byte fuzzer input: 43.9s before, immediate ValueError after
- Valid 74KB file with a 5,000 element array and one tensor: parses
correctly with the change applied
AI usage disclosure: AI-assisted. The bug was found by my own fuzzing
campaign against gguf-py. I used AI assistance to identify the fix and
draft this description, and reviewed and tested the change before
submitting.
This comment was marked as resolved.
This comment was marked as resolved.
Member
|
The check is incorrect. |
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.
The array branch of
_get_field_partsvalidates the declared element count againstGGUF_MAX_ARRAY_ELEMENTSbut never against the bytes actually remaining in the file. A count below that ceiling still drives a full per-element loop, so a small file that declares a large array costs time proportional to the declared count rather than to the file size._get_stralready performs the equivalent check for strings:This adds the matching bound for arrays. Every element occupies at least one byte, so a declared count greater than the remaining byte count is not satisfiable and can be rejected before the loop starts.
Found by fuzzing gguf-py with Atheris. A 101-byte input declaring 13,434,883 elements took 43.9s of CPU to parse on master (2d8d612); with this change it raises immediately. A normal file with a 5,000 element array parses unchanged.
Testing:
Overview
Additional information
Requirements
Yes: AI usage disclosure: AI-assisted. The bug was found by my own fuzzing campaign against gguf-py. I used AI assistance to identify the fix and draft this description, and reviewed and tested the change before submitting.