Skip to content

Fix heap-buffer-overflow read in exif_scan_thumbnail() - #25725

Open
Perennos wants to merge 1 commit into
php:PHP-8.4from
Perennos:fix-exif-scan-thumbnail-length-read
Open

Perennos wants to merge 1 commit into
php:PHP-8.4from
Perennos:fix-exif-scan-thumbnail-length-read

Conversation

@Perennos

Copy link
Copy Markdown

Fixes the heap-buffer-overflow (READ of size 1) in exif_scan_thumbnail() reported by OSS-Fuzz: https://issues.oss-fuzz.com/issues/579569397

exif_scan_thumbnail() previously checked pos >= Thumbnail.size before reading the 2-byte JPEG segment length via php_jpg_get16(data + pos). When a marker ends with only one byte remaining in the thumbnail buffer (pos == size - 1), reading the second length byte advances one byte past the buffer. Requiring pos + 1 < size ensures both bytes are in bounds and returns false otherwise.

This pre-existing issue was exposed after #24722 updated the fuzzer to call exif_thumbnail() with width/height arguments, which exercises exif_scan_thumbnail(). It reproduces on PHP-8.4.

Testing (ASan build, USE_ZEND_ALLOC=0, --disable-all --enable-exif --enable-debug):

  • Added ext/exif/tests/exif_scan_thumbnail_truncated_marker.phpt, which constructs a TIFF with a 5-byte truncated JPEG thumbnail (FF D8 FF E0 00).
  • Before the fix: ASan reports heap-buffer-overflow ... READ of size 1 in php_jpg_get16 called from exif_scan_thumbnail.
  • After the fix: the test passes, and make test TESTS=ext/exif passes in a standard run (96 passed, 0 failed, 4 skipped out of 100 tests).

Targeted at PHP-8.4 as the lowest branch with active bug-fix support. No NEWS entry included; happy to add one if needed.

AI-assisted: drafted with an AI coding assistant; reproduced and tested locally before submitting.

The 2 byte segment length was read after only checking that one byte
remained in the thumbnail buffer, so a thumbnail ending right after a
JPEG marker read one byte past the end.

Found by OSS-Fuzz (issue 579569397).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant