Skip to content

Fix heap buffer overflow in pnm2png due to integer overflow in row_bytes - #823

Closed
VinzSpring wants to merge 1 commit into
pnggroup:libpng18from
VinzSpring:fix/pnm2png-row-bytes-overflow-v2
Closed

Fix heap buffer overflow in pnm2png due to integer overflow in row_bytes#823
VinzSpring wants to merge 1 commit into
pnggroup:libpng18from
VinzSpring:fix/pnm2png-row-bytes-overflow-v2

Conversation

@VinzSpring

Copy link
Copy Markdown

Fixes a heap buffer overflow in contrib/pngminus/pnm2png.c caused by integer overflow in the row_bytes calculation, as reported in #794.

The existing guard only catches row_bytes == 0, so a wrapped value of 2 passes through. A 2-byte buffer is allocated via png_malloc, but the write loop iterates width × channels = ~4.3 billion bytes, causing a heap buffer overflow confirmed by AddressSanitizer.

Fixes #794

The row_bytes calculation (width * channels * bytes_per_sample) could
silently wrap a uint32, bypassing the existing row_bytes == 0 guard and
causing a heap buffer overflow. For example, a P6 PPM with width
1431655766 yields row_bytes = 2 after wrapping, allocating a 2-byte
buffer while the write loop writes width*3 bytes.

Add overflow detection before the multiplication for both the normal
and packed-bitmap code paths.

Fixes: pnggroup#794
@Jeffin820

Copy link
Copy Markdown
Contributor

Consult #799

@VinzSpring

Copy link
Copy Markdown
Author

closed in favor of pnggroup/pngcontrib#2

@VinzSpring VinzSpring closed this Mar 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Heap Buffer Overflow via Integer Overflow in pnm2png row_bytes Calculation

2 participants