com php-src: Fix #78620: Out of memory error: NEWS Zend/zend_ alloc.c ext/standard/tests/strings/wordwrap_memory_limi t.phpt
ext/standard/tests/strings/wordwrap_memory_limit _win32.phpt
| From: | Christoph Michael Becker | Date: | Fri, 04 Oct 2019 07:08:01 +0000 |
| Subject: | com php-src: Fix #78620: Out of memory error: NEWS Zend/zend_ alloc.c ext/standard/tests/strings/wordwrap_memory_limi t.phpt ext/standard/tests/strings/wordwrap_memory_limit _win32.phpt |
||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-108716@lists.php.net to get a copy of this message | ||
Commit: abaf9a76dc933f4d1f2ff3591ced81d4d6dcb652
Author: Christoph M. Becker <cmbecker69@gmx.de> Thu, 3 Oct 2019 19:23:05 +0200
Parents: a6d219630c4128a124f3d9a2e7ca97afb92d5d47
Branches: PHP-7.2 PHP-7.3 PHP-7.4 master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=abaf9a76dc933f4d1f2ff3591ced81d4d6dcb652
Log:
Fix #78620: Out of memory error
The integer addition in
ZEND_MM_ALIGNED_SIZE_EX can overflow, what we
have to catch early.
Bugs:
https://bugs.php.net/78620
Changed paths:
M NEWS
M Zend/zend_alloc.c
M ext/standard/tests/strings/wordwrap_memory_limit.phpt
A ext/standard/tests/strings/wordwrap_memory_limit_win32.phpt
Diff:
diff --git a/NEWS b/NEWS
index 26abd33994a..a04f3bf3f8c 100644
--- a/NEWS
+++ b/NEWS
@@ -5,6 +5,7 @@ PHP NEWS
- Core:
. Fixed bug #78535 (auto_detect_line_endings value not parsed as bool).
(bugreportuser)
+ . Fixed bug #78620 (Out of memory error). (cmb, Nikita)
- Exif:
. Fixed bug #78442 ('Illegal component' on exif_read_data since PHP7)
diff --git a/Zend/zend_alloc.c b/Zend/zend_alloc.c
index 3a43027346d..f4a6179516e 100644
--- a/Zend/zend_alloc.c
+++ b/Zend/zend_alloc.c
@@ -1723,12 +1723,17 @@ static void *zend_mm_alloc_huge(zend_mm_heap *heap, size_t size
ZEND_FILE_LINE_D
* We allocate them with 2MB size granularity, to avoid many
* reallocations when they are extended by small pieces
*/
- size_t new_size = ZEND_MM_ALIGNED_SIZE_EX(size, MAX(REAL_PAGE_SIZE, ZEND_MM_CHUNK_SIZE));
+ size_t alignment = MAX(REAL_PAGE_SIZE, ZEND_MM_CHUNK_SIZE);
#else
- size_t new_size = ZEND_MM_ALIGNED_SIZE_EX(size, REAL_PAGE_SIZE);
+ size_t alignment = REAL_PAGE_SIZE;
#endif
+ size_t new_size = ZEND_MM_ALIGNED_SIZE_EX(size, alignment);
void *ptr;
+ if (UNEXPECTED(new_size < size)) {
+ zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%zu +
%zu)", size, alignment);
+ }
+
#if ZEND_MM_LIMIT
if (UNEXPECTED(new_size > heap->limit - heap->real_size)) {
if (zend_mm_gc(heap) && new_size <= heap->limit - heap->real_size) {
diff --git a/ext/standard/tests/strings/wordwrap_memory_limit.phpt
b/ext/standard/tests/strings/wordwrap_memory_limit.phpt
index fb0cc5c3bc6..21340153fae 100644
--- a/ext/standard/tests/strings/wordwrap_memory_limit.phpt
+++ b/ext/standard/tests/strings/wordwrap_memory_limit.phpt
@@ -2,6 +2,7 @@
No overflow should occur during the memory_limit check for wordwrap()
--SKIPIF--
<?php
+if (substr(PHP_OS, 0, 3) == 'WIN' && PHP_INT_SIZE == 4) die("skip this test
is not for 32bit Windows platforms");
if (getenv("USE_ZEND_ALLOC") === "0") die("skip Zend MM disabled");
?>
--INI--
diff --git a/ext/standard/tests/strings/wordwrap_memory_limit_win32.phpt
b/ext/standard/tests/strings/wordwrap_memory_limit_win32.phpt
new file mode 100644
index 00000000000..e0e76b5800c
--- /dev/null
+++ b/ext/standard/tests/strings/wordwrap_memory_limit_win32.phpt
@@ -0,0 +1,19 @@
+--TEST--
+No overflow should occur during the memory_limit check for wordwrap()
+--SKIPIF--
+<?php
+if (substr(PHP_OS, 0, 3) != 'WIN' || PHP_INT_SIZE != 4) die("skip this test is for
32bit Windows platforms only");
+if (getenv("USE_ZEND_ALLOC") === "0") die("skip Zend MM disabled");
+?>
+--INI--
+memory_limit=128M
+--FILE--
+<?php
+
+$str = str_repeat('x', 65534);
+$str2 = str_repeat('x', 65535);
+wordwrap($str, 1, $str2);
+
+?>
+--EXPECTF--
+Fatal error: Possible integer overflow in memory allocation (4294901777 + %d) in %s on line %d