From d335b151d4be95cd8afd7f431e8bd0c97f02957f Mon Sep 17 00:00:00 2001 From: Jorg Sowa Date: Sat, 28 Nov 2026 14:45:10 +0100 Subject: [PATCH] Fix NULL dereference in php_init_config() for unresolvable search path ini php_fopen_with_path() may open the file but leave opened_path NULL when the absolute path exceeds MAXPATHLEN. Reject the file in that case so the search continues with the next directory, as on ZTS, and as c88c89e351c does for -c naming a file. --- NEWS | 2 ++ main/fopen_wrappers.c | 9 ++++-- .../ini_search_path_sapi_unresolvable.phpt | 29 +++++++++++++++++++ .../tests/ini_search_path_unresolvable.phpt | 29 +++++++++++++++++++ 4 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 sapi/cli/tests/ini_search_path_sapi_unresolvable.phpt create mode 100644 sapi/cli/tests/ini_search_path_unresolvable.phpt diff --git a/NEWS b/NEWS index 73c9b4fefad7..bcacc01465d5 100644 --- a/NEWS +++ b/NEWS @@ -43,6 +43,8 @@ PHP NEWS . Fix OSS-Fuzz #588264417. (ndossche) . Fixed bug GH-24701 (Use-after-free on typed trait constants during opcache preload). (sanklamm, lazerg) + . Fixed NULL dereference at startup when the ini file found in the search + path has an absolute path exceeding MAXPATHLEN. (Jorg Sowa) - Curl: . Fixed curl_reset() retaining CURLOPT_PRIVATE after reset. (lacatoire) diff --git a/main/fopen_wrappers.c b/main/fopen_wrappers.c index c06bada11127..1260c86636d9 100644 --- a/main/fopen_wrappers.c +++ b/main/fopen_wrappers.c @@ -355,10 +355,13 @@ static FILE *php_fopen_and_set_opened_path(const char *path, const char *mode, z if (fp && opened_path) { //TODO :avoid reallocation char *tmp = expand_filepath_with_mode(path, NULL, NULL, 0, CWD_EXPAND); - if (tmp) { - *opened_path = zend_string_init(tmp, strlen(tmp), 0); - efree(tmp); + if (!tmp) { + /* Reject the file, like ZTS where VCWD_FOPEN() already fails */ + fclose(fp); + return NULL; } + *opened_path = zend_string_init(tmp, strlen(tmp), 0); + efree(tmp); } return fp; } diff --git a/sapi/cli/tests/ini_search_path_sapi_unresolvable.phpt b/sapi/cli/tests/ini_search_path_sapi_unresolvable.phpt new file mode 100644 index 000000000000..d2db38ad25fd --- /dev/null +++ b/sapi/cli/tests/ini_search_path_sapi_unresolvable.phpt @@ -0,0 +1,29 @@ +--TEST-- +php-cli.ini in search path is skipped when its absolute path exceeds MAXPATHLEN +--SKIPIF-- + +--FILE-- + ["pipe", "w"]], + $pipes, + $dir +); +echo stream_get_contents($pipes[1]); +?> +--CLEAN-- + +--EXPECT-- +string(3) "bar" diff --git a/sapi/cli/tests/ini_search_path_unresolvable.phpt b/sapi/cli/tests/ini_search_path_unresolvable.phpt new file mode 100644 index 000000000000..82bb059897dc --- /dev/null +++ b/sapi/cli/tests/ini_search_path_unresolvable.phpt @@ -0,0 +1,29 @@ +--TEST-- +php.ini in search path is skipped when its absolute path exceeds MAXPATHLEN +--SKIPIF-- + +--FILE-- + ["pipe", "w"]], + $pipes, + $dir +); +echo stream_get_contents($pipes[1]); +?> +--CLEAN-- + +--EXPECT-- +string(3) "bar"