[php-src] master: Merge branch 'PHP-8.5'

Ilia Alshanetsky <[email protected]>
Newsgroups gmane.comp.php.cvs.general
Message-ID <[email protected]>
Author: Ilia Alshanetsky (iliaal)
Date: 2026-08-11T07:24:37-04:00

Commit: https://github.com/php/php-src/commit/4f98314668803036e61dede05d12ae44b20ff19d
Raw diff: https://github.com/php/php-src/commit/4f98314668803036e61dede05d12ae44b20ff19d.diff

Merge branch 'PHP-8.5'

* PHP-8.5:
  Bound the HEIF meta box allocation by the file size

Changed paths:
  A  ext/exif/tests/heic_meta_box_alloc.phpt
  M  NEWS
  M  ext/exif/exif.c


Diff:

diff --git a/NEWS b/NEWS
index df22c2cea467..1ed1362f3557 100644
--- a/NEWS
+++ b/NEWS
@@ -39,6 +39,10 @@ PHP                                                                        NEWS
   . Fixed bug GH-23117 (Stack overflow when normalizing a deeply nested
     Dom\XMLDocument). (Lazizbek Ergashev)
 
+- Exif:
+  . Fixed exif_read_data() allocating a HEIF meta box larger than the file
+    it came from. (iliaal)
+
 - GMP:
   . Added optional $definitely_prime output parameter to gmp_prevprime().
     (Weilin Du)
diff --git a/ext/exif/exif.c b/ext/exif/exif.c
index a156a3a63ad2..503cf3609e83 100644
--- a/ext/exif/exif.c
+++ b/ext/exif/exif.c
@@ -4412,7 +4412,7 @@ static bool exif_scan_HEIF_header(image_info_type *ImageInfo, unsigned char *buf
 		}
 		if (box.type == FOURCC("meta")) {
 			limit = box.size - box_header_size;
-			if (limit < 36) {
+			if (limit < 36 || limit > ImageInfo->FileSize) {
 				break;
 			}
 			data = (unsigned char *)emalloc(limit);
diff --git a/ext/exif/tests/heic_meta_box_alloc.phpt b/ext/exif/tests/heic_meta_box_alloc.phpt
new file mode 100644
index 000000000000..0a07d4c29bf0
--- /dev/null
+++ b/ext/exif/tests/heic_meta_box_alloc.phpt
@@ -0,0 +1,23 @@
+--TEST--
+HEIC meta box size must be bounded by the file size
+--EXTENSIONS--
+exif
+--INI--
+memory_limit=32M
+--FILE--
+<?php
+// ftyp box (size 20) followed by a meta box whose size field claims 128MB,
+// in a file that is only 37 bytes. Without an upper bound the meta box
+// allocation exhausts memory_limit before any read is attempted.
+$ftyp = pack("N", 20) . "ftypheic" . str_repeat("\x00", 8);
+$meta = pack("N", 0x08000000) . "meta" . str_repeat("\x00", 8);
+file_put_contents(__DIR__."/heic_meta_box_alloc.heic", $ftyp . $meta . "\x00");
+var_dump(exif_read_data(__DIR__."/heic_meta_box_alloc.heic"));
+?>
+--CLEAN--
+<?php
+@unlink(__DIR__."/heic_meta_box_alloc.heic");
+?>
+--EXPECTF--
+Warning: exif_read_data(): Invalid HEIF file in %s on line %d
+bool(false)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.