Fix integer overflow in tensor dimensions and PPM parser#896
Open
sharadboni wants to merge 2 commits intogoogle:devfrom
Open
Fix integer overflow in tensor dimensions and PPM parser#896sharadboni wants to merge 2 commits intogoogle:devfrom
sharadboni wants to merge 2 commits intogoogle:devfrom
Conversation
Author
|
@jan-wassenberg Could you review this security fix? It adds overflow checks for tensor dimensions in Extents2D::Area() and fixes integer overflow in the PPM image parser that can be triggered via crafted model/image files. |
jan-wassenberg
requested changes
Apr 16, 2026
Member
jan-wassenberg
left a comment
There was a problem hiding this comment.
Thanks, this seems reasonable to rule out. We're only able to land patches targeting the dev branch, would you mind updating the PR?
Also one code suggestion.
- util/basics.h: Add overflow check in Extents2D::Area() before computing rows*cols. Malicious model files with large dimension values could cause a silent size_t overflow, leading to undersized allocations and subsequent heap buffer overflows. - paligemma/image.cc: Add overflow check for width*height*3 in ReadPPM(). A crafted PPM file with large dimensions could overflow the data_size computation, resulting in an undersized buffer and out-of-bounds writes. - paligemma/image.cc: Add overflow detection in ParseUnsigned() to reject values that would overflow size_t during decimal parsing.
8bf2a7b to
c4d14db
Compare
jan-wassenberg
approved these changes
Apr 16, 2026
Member
jan-wassenberg
left a comment
There was a problem hiding this comment.
Thanks for updating! (FYI HWY_ABORT does not return, but we can land as-is)
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
Tensor dimension overflow (
util/basics.h):Extents2D::Area()computesrows * colswithout checking forsize_toverflow. When tensor dimensions are deserialized from a model file viaMatPtr::VisitFields(), attacker-controlledrowsandcolsvalues can silently overflow, causing undersized allocations and subsequent heap buffer overflows. Added an overflow check that aborts ifcols > SIZE_MAX / rows.PPM image size overflow (
paligemma/image.cc):ReadPPM()computesdata_size = width * height * 3without overflow protection. A crafted PPM file with large width/height values can overflow this computation, leading to an undersizeddata_vector and out-of-bounds writes during pixel parsing. Added a two-step overflow check before the multiplication.PPM integer parsing overflow (
paligemma/image.cc):ParseUnsigned()accumulates decimal digits into asize_twithout detecting overflow, allowing silently wrapped values to bypass subsequent size checks. Added overflow detection that returnsnullptr(parse failure) when the accumulated value would exceedSIZE_MAX.Test plan
bazel test //...)