Skip to content

Commit 1f83ebc

Browse files
committed
exif/heic: Avoid overflow when adding box size and checking against file size
We change the order of operations such that the file size check cannot overflow in the for loop. This prevents infinite loops. We also add an overflow check at the end of the loop body to prevent the addition of offset and box.size from overflowing.
1 parent 389691a commit 1f83ebc

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

ext/exif/exif.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4388,7 +4388,7 @@ static bool exif_scan_HEIF_header(image_info_type *ImageInfo, unsigned char *buf
43884388
bool ret = false;
43894389

43904390
pos.size = 0;
4391-
for (offset = php_ifd_get32u(buf, 1); ImageInfo->FileSize > offset + 16; offset += box.size) {
4391+
for (offset = php_ifd_get32u(buf, 1); ImageInfo->FileSize - 16 > offset; offset += box.size) {
43924392
if ((php_stream_seek(ImageInfo->infile, offset, SEEK_SET) < 0) ||
43934393
(exif_read_from_stream_file_looped(ImageInfo->infile, (char*)buf, 16) != 16)) {
43944394
break;
@@ -4425,6 +4425,9 @@ static bool exif_scan_HEIF_header(image_info_type *ImageInfo, unsigned char *buf
44254425
efree(data);
44264426
break;
44274427
}
4428+
if (offset + box.size < offset) {
4429+
break;
4430+
}
44284431
}
44294432

44304433
return ret;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
HEIC box overflow
3+
--EXTENSIONS--
4+
exif
5+
--FILE--
6+
<?php
7+
$bytearray = [
8+
0, 0, 0, 12, 'f', 't', 'y', 'p', 'h', 'e', 'i', 'c',
9+
0, 0, 0, 1, 'x', 'y', 'z', 'w', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff-11, 0, 0, 0, 0, 0, 0, 0
10+
];
11+
12+
function convert($x) {
13+
if (is_string($x)) return $x;
14+
return chr($x);
15+
}
16+
17+
file_put_contents(__DIR__."/heic_box_overflow", implode('', array_map(convert(...), $bytearray)));
18+
19+
var_dump(exif_read_data(__DIR__."/heic_box_overflow"));
20+
?>
21+
--CLEAN--
22+
<?php
23+
@unlink(__DIR__."/heic_box_overflow");
24+
?>
25+
--EXPECT--

0 commit comments

Comments
 (0)