Skip to content

Commit 760ff84

Browse files
committed
Fix #74960: Heap buffer overflow via str_repeat
Trying to allocate a `zend_string` with a length only slighty smaller than `SIZE_MAX` causes an integer overflow, so callers may need to check that explicitly. To make that easy in a portable way, we introduce `ZSTR_MAX_LEN`. Closes GH-7294.
1 parent 2d2c001 commit 760ff84

File tree

3 files changed

+4
-1
lines changed

3 files changed

+4
-1
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ PHP NEWS
1515
. Fixed bug #72146 (Integer overflow on substr_replace). (cmb)
1616
. Fixed bug #81265 (getimagesize returns 0 for 256px ICO images).
1717
(George Dietrich)
18+
. Fixed bug #74960 (Heap buffer overflow via str_repeat). (cmb, Dmitry)
1819

1920
29 Jul 2021, PHP 7.4.22
2021

Zend/zend_operators.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1882,7 +1882,7 @@ ZEND_API int ZEND_FASTCALL concat_function(zval *result, zval *op1, zval *op2) /
18821882
size_t result_len = op1_len + op2_len;
18831883
zend_string *result_str;
18841884

1885-
if (UNEXPECTED(op1_len > SIZE_MAX - op2_len)) {
1885+
if (UNEXPECTED(op1_len > ZSTR_MAX_LEN - op2_len)) {
18861886
zend_throw_error(NULL, "String size overflow");
18871887
zval_ptr_dtor_str(&op1_copy);
18881888
zval_ptr_dtor_str(&op2_copy);

Zend/zend_string.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ END_EXTERN_C()
7575

7676
#define _ZSTR_STRUCT_SIZE(len) (_ZSTR_HEADER_SIZE + len + 1)
7777

78+
#define ZSTR_MAX_LEN (SIZE_MAX - ZEND_MM_ALIGNED_SIZE(_ZSTR_HEADER_SIZE + 1))
79+
7880
#define ZSTR_ALLOCA_ALLOC(str, _len, use_heap) do { \
7981
(str) = (zend_string *)do_alloca(ZEND_MM_ALIGNED_SIZE_EX(_ZSTR_STRUCT_SIZE(_len), 8), (use_heap)); \
8082
GC_SET_REFCOUNT(str, 1); \

0 commit comments

Comments
 (0)