Skip to content

Commit 518c2a8

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 4e70d41 commit 518c2a8

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

ext/exif/exif.c

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

43954395
pos.size = 0;
4396-
for (offset = php_ifd_get32u(buf, 1); ImageInfo->FileSize > offset + 16; offset += box.size) {
4396+
for (offset = php_ifd_get32u(buf, 1); ImageInfo->FileSize - 16 > offset; offset += box.size) {
43974397
if ((php_stream_seek(ImageInfo->infile, offset, SEEK_SET) < 0) ||
43984398
(exif_read_from_stream_file_looped(ImageInfo->infile, (char*)buf, 16) != 16)) {
43994399
break;
@@ -4433,6 +4433,9 @@ static bool exif_scan_HEIF_header(image_info_type *ImageInfo, unsigned char *buf
44334433
efree(data);
44344434
break;
44354435
}
4436+
if (offset + box.size < offset) {
4437+
break;
4438+
}
44364439
}
44374440

44384441
return ret;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
--EXPECTF--
26+
Warning: exif_read_data(heic_box_overflow): Invalid HEIF file in %s on line %d
27+
bool(false)

0 commit comments

Comments
 (0)