From 689fc83bcedf0a6aa6965b48affdac8578a2720a Mon Sep 17 00:00:00 2001 From: David Carlier Date: Sat, 28 Nov 2026 04:28:50 +0000 Subject: [PATCH 1/2] main/io: stream_copy_to_stream() socket timeout overflow. A stream timeout above INT_MAX milliseconds was truncated when converted to an int. Used php_tvtoto() which checks the range. Fix GH-25860 --- ext/standard/tests/streams/gh25860.phpt | 28 +++++++++++++++++++++++++ main/io/php_io.c | 4 +--- main/io/php_io_copy_linux.c | 9 +------- main/io/php_io_copy_windows.c | 7 +------ 4 files changed, 31 insertions(+), 17 deletions(-) create mode 100644 ext/standard/tests/streams/gh25860.phpt diff --git a/ext/standard/tests/streams/gh25860.phpt b/ext/standard/tests/streams/gh25860.phpt new file mode 100644 index 000000000000..72fd7c518d25 --- /dev/null +++ b/ext/standard/tests/streams/gh25860.phpt @@ -0,0 +1,28 @@ +--TEST-- +GH-25860 (stream_copy_to_stream() from a socket with a timeout longer than INT_MAX milliseconds) +--CREDITS-- +OmenP007 +--SKIPIF-- + +--FILE-- + ['socket']], $pipes); + +stream_set_timeout($pipes[1], 4294968); +var_dump(stream_copy_to_stream($pipes[1], $dest)); + +fclose($pipes[1]); +fclose($dest); +proc_close($proc); +var_dump(file_get_contents($file)); +@unlink($file); +?> +--EXPECT-- +int(4) +string(4) "data" diff --git a/main/io/php_io.c b/main/io/php_io.c index 6954cd4bfde5..0a9413c751f4 100644 --- a/main/io/php_io.c +++ b/main/io/php_io.c @@ -101,9 +101,7 @@ static int php_io_generic_wait_for_data(php_io_fd *fd) return 1; } - int timeout_ms = (fd->timeout.tv_sec == -1) - ? -1 - : (int) (fd->timeout.tv_sec * 1000 + fd->timeout.tv_usec / 1000); + int timeout_ms = php_tvtoto(&fd->timeout); int ret; do { diff --git a/main/io/php_io_copy_linux.c b/main/io/php_io_copy_linux.c index e5d7ecb1b721..f24399349b7d 100644 --- a/main/io/php_io_copy_linux.c +++ b/main/io/php_io_copy_linux.c @@ -46,14 +46,7 @@ static inline int php_io_linux_wait_for_data(php_io_fd *fd) return 1; } - struct timeval *ptimeout = (fd->timeout.tv_sec == -1) ? NULL : &fd->timeout; - int timeout_ms; - - if (ptimeout == NULL) { - timeout_ms = -1; - } else { - timeout_ms = ptimeout->tv_sec * 1000 + ptimeout->tv_usec / 1000; - } + int timeout_ms = php_tvtoto(&fd->timeout); int ret; do { diff --git a/main/io/php_io_copy_windows.c b/main/io/php_io_copy_windows.c index de75d2529987..afbb6c124eea 100644 --- a/main/io/php_io_copy_windows.c +++ b/main/io/php_io_copy_windows.c @@ -44,12 +44,7 @@ static inline int php_io_win_wait_for_data(php_io_fd *fd) return 1; } - int timeout_ms; - if (fd->timeout.tv_sec == -1) { - timeout_ms = -1; - } else { - timeout_ms = fd->timeout.tv_sec * 1000 + fd->timeout.tv_usec / 1000; - } + int timeout_ms = php_tvtoto(&fd->timeout); WSAPOLLFD pfd; pfd.fd = fd->socket; From ab65518b50f3403736748c8c5473147bc62486b0 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Sat, 28 Nov 2026 05:03:45 +0000 Subject: [PATCH 2/2] Fix test on Windows. --- ext/standard/tests/streams/gh25860.phpt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/standard/tests/streams/gh25860.phpt b/ext/standard/tests/streams/gh25860.phpt index 72fd7c518d25..cf3c553cada6 100644 --- a/ext/standard/tests/streams/gh25860.phpt +++ b/ext/standard/tests/streams/gh25860.phpt @@ -15,7 +15,7 @@ $php = getenv('TEST_PHP_EXECUTABLE_ESCAPED'); $proc = proc_open($php . ' -n -r "usleep(2000000); echo \'data\';"', [1 => ['socket']], $pipes); stream_set_timeout($pipes[1], 4294968); -var_dump(stream_copy_to_stream($pipes[1], $dest)); +var_dump(stream_copy_to_stream($pipes[1], $dest, 4)); fclose($pipes[1]); fclose($dest);