Fix integer overflow in quantize_pngquant()#9474
Open
myagmartseren wants to merge 2 commits intopython-pillow:mainfrom
Open
Fix integer overflow in quantize_pngquant()#9474myagmartseren wants to merge 2 commits intopython-pillow:mainfrom
myagmartseren wants to merge 2 commits intopython-pillow:mainfrom
Conversation
- Add overflow check for width * height before malloc() to prevent heap buffer overflow when the product exceeds UINT_MAX - Use size_t for total_pixels to ensure correct arithmetic on 64-bit - Replace sprintf with snprintf (consistent with CVE-2024-28219 fix) Security: CWE-190 (Integer Overflow) -> CWE-122 (Heap Buffer Overflow)
for more information, see https://pre-commit.ci
|
Note: Security policy: https://github.com/python-pillow/Pillow/blob/main/.github/SECURITY.md This function is only called in one location: Line 1814 in 3a44ba1 #ifdef HAVE_LIBIMAGEQUANT
result = quantize_pngquant(
p,
im->xsize,
im->ysize,
colors,
&palette,
&paletteLength,
&newData,
withAlpha
);And previously in that function: (https://github.com/python-pillow/Pillow/blob/3a44ba1c75d72e7f375be9c6b7c71e2aef5e35e7/src/libImaging/Quant.c#L1697C5-L1699C6) if (im->xsize > INT_MAX / im->ysize) {
return ImagingError_MemoryError();
} |
Member
|
And one level up from that, we also check that the height is not zero. Lines 1733 to 1737 in 3a44ba1 |
radarhere
reviewed
Mar 20, 2026
| number / 10000, | ||
| (number / 100) % 100, | ||
| number % 100 | ||
| ); |
Member
There was a problem hiding this comment.
This change is included in #9476. See that issue for comments.
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.
Integer Overflow → Heap Buffer Overflow in QuantPngQuant.c
Summary
quantize_pngquant()allocates memory usingwidth * height(bothunsigned int) without checking for integer overflow. When the product exceedsUINT_MAX, it wraps to a small value, causing an undersizedmalloc(). Subsequentliq_write_remapped_image_rows()writes the real size into the small buffer → heap buffer overflow.Affected Code
src/libImaging/QuantPngQuant.c, lines 80, 96:Example
width=65537, height=65537→ product wraps to 131,073 (should be 4.3 billion)malloc(131073)allocates 128 KB; decoder writes 4 GB → heap corruptionFix
if (height != 0 && (size_t)width > SIZE_MAX / (size_t)height)size_t total_pixelsfor all size calculationssprintf→snprintf(line 129), consistent with CVE-2024-28219Classification
AV:N/AC:H/PR:N/UI:N/S:U/C:H/I:H/A:H