Skip to content

Commit 8363df3

Browse files
committed
Merge branch 'PHP-7.1' into PHP-7.2
* PHP-7.1: 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 637713c + 759e841 commit 8363df3

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
@@ -3152,7 +3152,7 @@ static int exif_process_IFD_in_MAKERNOTE(image_info_type *ImageInfo, char * valu
31523152
break;
31533153
}
31543154

3155-
if (maker_note->offset >= value_len) {
3155+
if (value_len < 2 || maker_note->offset >= value_len - 1) {
31563156
/* Do not go past the value end */
31573157
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);
31583158
return FALSE;
@@ -3207,6 +3207,7 @@ static int exif_process_IFD_in_MAKERNOTE(image_info_type *ImageInfo, char * valu
32073207
#endif
32083208
default:
32093209
case MN_OFFSET_NORMAL:
3210+
data_len = value_len;
32103211
break;
32113212
}
32123213

@@ -3930,7 +3931,7 @@ static int exif_scan_thumbnail(image_info_type *ImageInfo)
39303931
return FALSE;
39313932
marker = c;
39323933
length = php_jpg_get16(data+pos);
3933-
if (pos+length>=ImageInfo->Thumbnail.size) {
3934+
if (length > ImageInfo->Thumbnail.size || pos >= ImageInfo->Thumbnail.size - length) {
39343935
return FALSE;
39353936
}
39363937
#ifdef EXIF_DEBUG
@@ -3951,6 +3952,10 @@ static int exif_scan_thumbnail(image_info_type *ImageInfo)
39513952
case M_SOF14:
39523953
case M_SOF15:
39533954
/* handle SOFn block */
3955+
if (length < 8 || ImageInfo->Thumbnail.size - 8 < pos) {
3956+
/* exif_process_SOFn needs 8 bytes */
3957+
return FALSE;
3958+
}
39543959
exif_process_SOFn(data+pos, marker, &sof_info);
39553960
ImageInfo->Thumbnail.height = sof_info.height;
39563961
ImageInfo->Thumbnail.width = sof_info.width;
@@ -3988,19 +3993,19 @@ static int exif_process_IFD_in_TIFF(image_info_type *ImageInfo, size_t dir_offse
39883993
tag_table_type tag_table = exif_get_tag_table(section_index);
39893994

39903995
if (ImageInfo->ifd_nesting_level > MAX_IFD_NESTING_LEVEL) {
3991-
return FALSE;
3992-
}
3996+
return FALSE;
3997+
}
39933998

3994-
if (ImageInfo->FileSize >= dir_offset+2) {
3999+
if (ImageInfo->FileSize >= 2 && ImageInfo->FileSize - 2 >= dir_offset) {
39954000
sn = exif_file_sections_add(ImageInfo, M_PSEUDO, 2, NULL);
39964001
#ifdef EXIF_DEBUG
39974002
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);
39984003
#endif
39994004
php_stream_seek(ImageInfo->infile, dir_offset, SEEK_SET); /* we do not know the order of sections */
40004005
php_stream_read(ImageInfo->infile, (char*)ImageInfo->file.list[sn].data, 2);
40014006
num_entries = php_ifd_get16u(ImageInfo->file.list[sn].data, ImageInfo->motorola_intel);
4002-
dir_size = 2/*num dir entries*/ +12/*length of entry*/*num_entries +4/* offset to next ifd (points to thumbnail or NULL)*/;
4003-
if (ImageInfo->FileSize >= dir_offset+dir_size) {
4007+
dir_size = 2/*num dir entries*/ +12/*length of entry*/*(size_t)num_entries +4/* offset to next ifd (points to thumbnail or NULL)*/;
4008+
if (ImageInfo->FileSize >= dir_size && ImageInfo->FileSize - dir_size >= dir_offset) {
40044009
#ifdef EXIF_DEBUG
40054010
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);
40064011
#endif
@@ -4083,9 +4088,9 @@ static int exif_process_IFD_in_TIFF(image_info_type *ImageInfo, size_t dir_offse
40834088
}
40844089
}
40854090
}
4086-
if (ImageInfo->FileSize >= dir_offset + ImageInfo->file.list[sn].size) {
4091+
if (ImageInfo->FileSize >= ImageInfo->file.list[sn].size && ImageInfo->FileSize - ImageInfo->file.list[sn].size >= dir_offset) {
40874092
if (ifd_size > dir_size) {
4088-
if (dir_offset + ifd_size > ImageInfo->FileSize) {
4093+
if (ImageInfo->FileSize < ifd_size || dir_offset > ImageInfo->FileSize - ifd_size) {
40894094
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);
40904095
return FALSE;
40914096
}
@@ -4682,7 +4687,9 @@ PHP_FUNCTION(exif_thumbnail)
46824687
ZVAL_STRINGL(return_value, ImageInfo.Thumbnail.data, ImageInfo.Thumbnail.size);
46834688
if (arg_c >= 3) {
46844689
if (!ImageInfo.Thumbnail.width || !ImageInfo.Thumbnail.height) {
4685-
exif_scan_thumbnail(&ImageInfo);
4690+
if (!exif_scan_thumbnail(&ImageInfo)) {
4691+
ImageInfo.Thumbnail.width = ImageInfo.Thumbnail.height = 0;
4692+
}
46864693
}
46874694
zval_dtor(z_width);
46884695
zval_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
@@ -1385,6 +1385,9 @@ int phar_create_or_parse_filename(char *fname, int fname_len, char *alias, int a
13851385
/* set up our manifest */
13861386
mydata = ecalloc(1, sizeof(phar_archive_data));
13871387
mydata->fname = expand_filepath(fname, NULL);
1388+
if (mydata->fname == NULL) {
1389+
return FAILURE;
1390+
}
13881391
fname_len = strlen(mydata->fname);
13891392
#ifdef PHP_WIN32
13901393
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
@@ -1135,7 +1135,7 @@ SPL_METHOD(SplFileInfo, __construct)
11351135
char *path;
11361136
size_t len;
11371137

1138-
if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "s", &path, &len) == FAILURE) {
1138+
if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "p", &path, &len) == FAILURE) {
11391139
return;
11401140
}
11411141

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)