Skip to content

Commit e3f7c35

Browse files
committed
Merge branch 'PHP-7.2' into PHP-7.3
* PHP-7.2: Update NEWS Fix test error message Fix bug #77563 - Uninitialized read in exif_process_IFD_in_MAKERNOTE Fix bug #77540 - Invalid Read on exif_process_SOFn Fix integer overflows on 32-bits Fix #77431 SplFileInfo::__construct() accepts NUL bytes Fix bug #77396 - Null Pointer Dereference in phar_create_or_parse_filename
2 parents fa574dd + 8363df3 commit e3f7c35

File tree

9 files changed

+77
-11
lines changed

9 files changed

+77
-11
lines changed

ext/exif/exif.c

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3125,7 +3125,7 @@ static int exif_process_IFD_in_MAKERNOTE(image_info_type *ImageInfo, char * valu
31253125
break;
31263126
}
31273127

3128-
if (maker_note->offset >= value_len) {
3128+
if (value_len < 2 || maker_note->offset >= value_len - 1) {
31293129
/* Do not go past the value end */
31303130
exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "IFD data too short: 0x%04X offset 0x%04X", value_len, maker_note->offset);
31313131
return FALSE;
@@ -3180,6 +3180,7 @@ static int exif_process_IFD_in_MAKERNOTE(image_info_type *ImageInfo, char * valu
31803180
#endif
31813181
default:
31823182
case MN_OFFSET_NORMAL:
3183+
data_len = value_len;
31833184
break;
31843185
}
31853186

@@ -3903,7 +3904,7 @@ static int exif_scan_thumbnail(image_info_type *ImageInfo)
39033904
return FALSE;
39043905
marker = c;
39053906
length = php_jpg_get16(data+pos);
3906-
if (pos+length>=ImageInfo->Thumbnail.size) {
3907+
if (length > ImageInfo->Thumbnail.size || pos >= ImageInfo->Thumbnail.size - length) {
39073908
return FALSE;
39083909
}
39093910
#ifdef EXIF_DEBUG
@@ -3924,6 +3925,10 @@ static int exif_scan_thumbnail(image_info_type *ImageInfo)
39243925
case M_SOF14:
39253926
case M_SOF15:
39263927
/* handle SOFn block */
3928+
if (length < 8 || ImageInfo->Thumbnail.size - 8 < pos) {
3929+
/* exif_process_SOFn needs 8 bytes */
3930+
return FALSE;
3931+
}
39273932
exif_process_SOFn(data+pos, marker, &sof_info);
39283933
ImageInfo->Thumbnail.height = sof_info.height;
39293934
ImageInfo->Thumbnail.width = sof_info.width;
@@ -3961,19 +3966,19 @@ static int exif_process_IFD_in_TIFF(image_info_type *ImageInfo, size_t dir_offse
39613966
tag_table_type tag_table = exif_get_tag_table(section_index);
39623967

39633968
if (ImageInfo->ifd_nesting_level > MAX_IFD_NESTING_LEVEL) {
3964-
return FALSE;
3965-
}
3969+
return FALSE;
3970+
}
39663971

3967-
if (ImageInfo->FileSize >= dir_offset+2) {
3972+
if (ImageInfo->FileSize >= 2 && ImageInfo->FileSize - 2 >= dir_offset) {
39683973
sn = exif_file_sections_add(ImageInfo, M_PSEUDO, 2, NULL);
39693974
#ifdef EXIF_DEBUG
39703975
exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Read from TIFF: filesize(x%04X), IFD dir(x%04X + x%04X)", ImageInfo->FileSize, dir_offset, 2);
39713976
#endif
39723977
php_stream_seek(ImageInfo->infile, dir_offset, SEEK_SET); /* we do not know the order of sections */
39733978
php_stream_read(ImageInfo->infile, (char*)ImageInfo->file.list[sn].data, 2);
39743979
num_entries = php_ifd_get16u(ImageInfo->file.list[sn].data, ImageInfo->motorola_intel);
3975-
dir_size = 2/*num dir entries*/ +12/*length of entry*/*num_entries +4/* offset to next ifd (points to thumbnail or NULL)*/;
3976-
if (ImageInfo->FileSize >= dir_offset+dir_size) {
3980+
dir_size = 2/*num dir entries*/ +12/*length of entry*/*(size_t)num_entries +4/* offset to next ifd (points to thumbnail or NULL)*/;
3981+
if (ImageInfo->FileSize >= dir_size && ImageInfo->FileSize - dir_size >= dir_offset) {
39773982
#ifdef EXIF_DEBUG
39783983
exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Read from TIFF: filesize(x%04X), IFD dir(x%04X + x%04X), IFD entries(%d)", ImageInfo->FileSize, dir_offset+2, dir_size-2, num_entries);
39793984
#endif
@@ -4056,9 +4061,9 @@ static int exif_process_IFD_in_TIFF(image_info_type *ImageInfo, size_t dir_offse
40564061
}
40574062
}
40584063
}
4059-
if (ImageInfo->FileSize >= dir_offset + ImageInfo->file.list[sn].size) {
4064+
if (ImageInfo->FileSize >= ImageInfo->file.list[sn].size && ImageInfo->FileSize - ImageInfo->file.list[sn].size >= dir_offset) {
40604065
if (ifd_size > dir_size) {
4061-
if (dir_offset + ifd_size > ImageInfo->FileSize) {
4066+
if (ImageInfo->FileSize < ifd_size || dir_offset > ImageInfo->FileSize - ifd_size) {
40624067
exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Error in TIFF: filesize(x%04X) less than size of IFD(x%04X + x%04X)", ImageInfo->FileSize, dir_offset, ifd_size);
40634068
return FALSE;
40644069
}
@@ -4655,7 +4660,9 @@ PHP_FUNCTION(exif_thumbnail)
46554660
ZVAL_STRINGL(return_value, ImageInfo.Thumbnail.data, ImageInfo.Thumbnail.size);
46564661
if (arg_c >= 3) {
46574662
if (!ImageInfo.Thumbnail.width || !ImageInfo.Thumbnail.height) {
4658-
exif_scan_thumbnail(&ImageInfo);
4663+
if (!exif_scan_thumbnail(&ImageInfo)) {
4664+
ImageInfo.Thumbnail.width = ImageInfo.Thumbnail.height = 0;
4665+
}
46594666
}
46604667
zval_ptr_dtor(z_width);
46614668
zval_ptr_dtor(z_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

ext/exif/tests/bug77563.jpg

63 Bytes
Loading

ext/exif/tests/bug77563.phpt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Bug 77563 (Uninitialized read in exif_process_IFD_in_MAKERNOTE)
3+
--SKIPIF--
4+
<?php if (!extension_loaded('exif')) print 'skip exif extension not available';?>
5+
--FILE--
6+
<?php
7+
$s = exif_thumbnail(__DIR__."/bug77563.jpg");
8+
?>
9+
DONE
10+
--EXPECTF--
11+
Warning: exif_thumbnail(bug77563.jpg): IFD data too short: 0x0009 offset 0x0008 in %s/bug77563.php on line %d
12+
13+
Warning: exif_thumbnail(bug77563.jpg): File structure corrupted in %s/bug77563.php on line %d
14+
15+
Warning: exif_thumbnail(bug77563.jpg): Invalid JPEG file in %s/bug77563.php on line %d
16+
DONE

ext/phar/phar.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,6 +1403,9 @@ int phar_create_or_parse_filename(char *fname, size_t fname_len, char *alias, si
14031403
/* set up our manifest */
14041404
mydata = ecalloc(1, sizeof(phar_archive_data));
14051405
mydata->fname = expand_filepath(fname, NULL);
1406+
if (mydata->fname == NULL) {
1407+
return FAILURE;
1408+
}
14061409
fname_len = strlen(mydata->fname);
14071410
#ifdef PHP_WIN32
14081411
phar_unixify_path_separators(mydata->fname, fname_len);

ext/phar/tests/bug77396.phpt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
Bug #77396 Relative filename exceeding maximum path length causes null pointer dereference.
3+
--SKIPIF--
4+
<?php if (!extension_loaded("phar")) die("skip"); ?>
5+
--FILE--
6+
<?php
7+
$path = '../' . str_repeat("x", PHP_MAXPATHLEN) . '.tar';
8+
$phar = new PharData($path);
9+
?>
10+
--EXPECTF--
11+
Fatal error: Uncaught UnexpectedValueException: Phar creation or opening failed in %s/bug77396.php:%d
12+
Stack trace:
13+
#0 %s/bug77396.php(%d): PharData->__construct(%s)
14+
#1 {main}
15+
thrown in %s/bug77396.php on line %d

ext/spl/spl_directory.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1133,7 +1133,7 @@ SPL_METHOD(SplFileInfo, __construct)
11331133
char *path;
11341134
size_t len;
11351135

1136-
if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "s", &path, &len) == FAILURE) {
1136+
if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "p", &path, &len) == FAILURE) {
11371137
return;
11381138
}
11391139

ext/spl/tests/bug77431.phpt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
--TEST--
2+
Bug #77431 (SplFileInfo::__construct() accepts NUL bytes)
3+
--FILE--
4+
<?php
5+
new SplFileInfo("bad\0good");
6+
?>
7+
--EXPECTF--
8+
Fatal error: Uncaught TypeError: SplFileInfo::__construct() expects parameter 1 to be a valid path, string given in %s:%d
9+
Stack trace:%A

0 commit comments

Comments
 (0)