Skip to content

Commit 5f0e62a

Browse files
committed
Fix bug #77540 - Invalid Read on exif_process_SOFn
1 parent 5e824a8 commit 5f0e62a

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

ext/exif/exif.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3509,7 +3509,7 @@ static int exif_scan_thumbnail(image_info_type *ImageInfo)
35093509
return FALSE;
35103510
marker = c;
35113511
length = php_jpg_get16(data+pos);
3512-
if (pos+length>=ImageInfo->Thumbnail.size) {
3512+
if (length > ImageInfo->Thumbnail.size || pos >= ImageInfo->Thumbnail.size - length) {
35133513
return FALSE;
35143514
}
35153515
#ifdef EXIF_DEBUG
@@ -3530,6 +3530,10 @@ static int exif_scan_thumbnail(image_info_type *ImageInfo)
35303530
case M_SOF14:
35313531
case M_SOF15:
35323532
/* handle SOFn block */
3533+
if (length < 8 || ImageInfo->Thumbnail.size - 8 < pos) {
3534+
/* exif_process_SOFn needs 8 bytes */
3535+
return FALSE;
3536+
}
35333537
exif_process_SOFn(data+pos, marker, &sof_info);
35343538
ImageInfo->Thumbnail.height = sof_info.height;
35353539
ImageInfo->Thumbnail.width = sof_info.width;
@@ -4177,7 +4181,9 @@ PHP_FUNCTION(exif_thumbnail)
41774181
ZVAL_STRINGL(return_value, ImageInfo.Thumbnail.data, ImageInfo.Thumbnail.size);
41784182
if (arg_c >= 3) {
41794183
if (!ImageInfo.Thumbnail.width || !ImageInfo.Thumbnail.height) {
4180-
exif_scan_thumbnail(&ImageInfo);
4184+
if (!exif_scan_thumbnail(&ImageInfo)) {
4185+
ImageInfo.Thumbnail.width = ImageInfo.Thumbnail.height = 0;
4186+
}
41814187
}
41824188
zval_dtor(p_width);
41834189
zval_dtor(p_height);

ext/exif/tests/bug77540.jpg

91 Bytes
Loading

ext/exif/tests/bug77540.phpt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Bug 77540 (Invalid Read on exif_process_SOFn)
3+
--SKIPIF--
4+
<?php if (!extension_loaded('exif')) print 'skip exif extension not available';?>
5+
--FILE--
6+
<?php
7+
$width = $height = 42;
8+
$s = exif_thumbnail(__DIR__."/bug77540.jpg", $width, $height);
9+
echo "Width ".$width."\n";
10+
echo "Height ".$height."\n";
11+
?>
12+
DONE
13+
--EXPECTF--
14+
Width 0
15+
Height 0
16+
DONE

0 commit comments

Comments
 (0)