Skip to content

Bound the HEIF meta box allocation by the file size - #23201

Closed
iliaal wants to merge 1 commit into
php:PHP-8.5from
iliaal:fix/exif-heif-meta-box-bound
Closed

Bound the HEIF meta box allocation by the file size#23201
iliaal wants to merge 1 commit into
php:PHP-8.5from
iliaal:fix/exif-heif-meta-box-bound

Conversation

@iliaal

@iliaal iliaal commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

exif_scan_HEIF_header() takes the meta box size straight from the file and allocates it with only a lower bound of 36, so a 37-byte HEIF claiming a 128MB box forces that allocation before a single byte is read. memory_limit stops it, so this is hardening rather than a vulnerability, but the same block already bounds its second allocation by pos.size < ImageInfo->FileSize and the first one should match.

$ftyp = pack("N", 20) . "ftypheic" . str_repeat("\x00", 8);
$meta = pack("N", 0x08000000) . "meta" . str_repeat("\x00", 8);
file_put_contents("t.heic", $ftyp . $meta . "\x00");
exif_read_data("t.heic");

With memory_limit=32M that is "Allowed memory size of 33554432 bytes exhausted (tried to allocate 134217720 bytes)" from a 37-byte file. PHP-8.4 has no HEIF parser, so this starts at 8.5.

exif_scan_HEIF_header() allocated box.size - box_header_size with only a
lower bound, so a 37-byte file could claim a 128MB meta box and force the
allocation before any read is attempted. The second allocation in the
same block is already bounded by pos.size < ImageInfo->FileSize; apply
the same bound to the first.

Closes phpGH-23201
@iliaal iliaal closed this in 82ac0da Aug 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants