From 292a7f73baf7c2ce3164feda6e6da779b8056d51 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+ndossche@users.noreply.github.com> Date: Tue, 25 Nov 2025 18:58:40 +0100 Subject: [PATCH 1/2] Fix GH-20583: Stack overflow in http_build_query via deep structures Closes GH-20590. --- NEWS | 2 ++ ext/standard/http.c | 15 +++++++++++ .../tests/http/http_build_query/gh20583.phpt | 27 +++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 ext/standard/tests/http/http_build_query/gh20583.phpt diff --git a/NEWS b/NEWS index b56302cff0f..b87cba17aad 100644 --- a/NEWS +++ b/NEWS @@ -58,6 +58,8 @@ PHP NEWS - Standard: . Fix memory leak in array_diff() with custom type checks. (ndossche) + . Fixed bug GH-20583 (Stack overflow in http_build_query + via deep structures). (ndossche) - Tidy: . Fixed bug GH-20374 (PHP with tidy and custom-tags). (ndossche) diff --git a/ext/standard/http.c b/ext/standard/http.c index 1145329a794..d79c25c38a0 100644 --- a/ext/standard/http.c +++ b/ext/standard/http.c @@ -92,6 +92,15 @@ static void php_url_encode_scalar(zval *scalar, smart_str *form_str, } } +static zend_always_inline bool php_url_check_stack_limit(void) +{ +#ifdef ZEND_CHECK_STACK_LIMIT + return zend_call_stack_overflowed(EG(stack_limit)); +#else + return false; +#endif +} + /* {{{ php_url_encode_hash */ PHPAPI void php_url_encode_hash_ex(HashTable *ht, smart_str *formstr, const char *num_prefix, size_t num_prefix_len, @@ -110,6 +119,12 @@ PHPAPI void php_url_encode_hash_ex(HashTable *ht, smart_str *formstr, return; } + /* Very deeply structured data could trigger a stack overflow, even without recursion. */ + if (UNEXPECTED(php_url_check_stack_limit())) { + zend_throw_error(NULL, "Maximum call stack size reached."); + return; + } + if (!arg_sep) { arg_sep = zend_ini_str("arg_separator.output", strlen("arg_separator.output"), false); if (ZSTR_LEN(arg_sep) == 0) { diff --git a/ext/standard/tests/http/http_build_query/gh20583.phpt b/ext/standard/tests/http/http_build_query/gh20583.phpt new file mode 100644 index 00000000000..c0331a830b1 --- /dev/null +++ b/ext/standard/tests/http/http_build_query/gh20583.phpt @@ -0,0 +1,27 @@ +--TEST-- +GH-20583 (Stack overflow in http_build_query via deep structures) +--SKIPIF-- + +--INI-- +zend.max_allowed_stack_size=512K +--FILE-- + $a]; +} +try { + http_build_query($a, 'p'); +} catch (Throwable $e) { + echo $e::class, ": ", $e->getMessage(), "\n"; +} +?> +--EXPECT-- +Error: Maximum call stack size reached. From 8fe79305331f12852afe2137a01fda373d8b37cb Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+ndossche@users.noreply.github.com> Date: Tue, 25 Nov 2025 23:11:38 +0100 Subject: [PATCH 2/2] Fix GH-20584: Information Leak of Memory The string added had uninitialized memory due to php_read_stream_all_chunks() not moving the buffer position, resulting in the same data always being overwritten instead of new data being added to the end of the buffer. Closes GH-20592. --- NEWS | 1 + ext/standard/image.c | 1 + ext/standard/tests/image/gh20584.phpt | 39 +++++++++++++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 ext/standard/tests/image/gh20584.phpt diff --git a/NEWS b/NEWS index b87cba17aad..863d672d736 100644 --- a/NEWS +++ b/NEWS @@ -60,6 +60,7 @@ PHP NEWS . Fix memory leak in array_diff() with custom type checks. (ndossche) . Fixed bug GH-20583 (Stack overflow in http_build_query via deep structures). (ndossche) + . Fixed bug GH-20584 (Information Leak of Memory). (ndossche) - Tidy: . Fixed bug GH-20374 (PHP with tidy and custom-tags). (ndossche) diff --git a/ext/standard/image.c b/ext/standard/image.c index 2bd5429efac..15761364c34 100644 --- a/ext/standard/image.c +++ b/ext/standard/image.c @@ -403,6 +403,7 @@ static size_t php_read_stream_all_chunks(php_stream *stream, char *buffer, size_ if (read_now < stream->chunk_size && read_total != length) { return 0; } + buffer += read_now; } while (read_total < length); return read_total; diff --git a/ext/standard/tests/image/gh20584.phpt b/ext/standard/tests/image/gh20584.phpt new file mode 100644 index 00000000000..d117f218202 --- /dev/null +++ b/ext/standard/tests/image/gh20584.phpt @@ -0,0 +1,39 @@ +--TEST-- +GH-20584 (Information Leak of Memory) +--CREDITS-- +Nikita Sveshnikov (Positive Technologies) +--FILE-- + +--CLEAN-- + +--EXPECT-- +bool(true)