[php-src] Issue #24616: fsockopen()/stream_socket_client() accept TCP ports above 65535, which wrap to another port
| From: | Marti2203 | Date: | Fri, 23 Oct 2026 18:17:09 +0000 |
| Subject: | [php-src] Issue #24616: fsockopen()/stream_socket_client() accept TCP ports above 65535, which wrap to another port | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-253140@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/24616
Author: Marti2203
### Description
A TCP port number above 65535 is not rejected by
fsockopen() or
stream_socket_client(): it wraps modulo 65536 and the connection goes to a different
port. parse_ip_address_ex() in main/streams/xp_socket.c reads the port
with atoi() and stores it without a range check.
The following code:
```php
<?php
$srv = stream_socket_server("tcp://127.0.0.1:0");
$name = stream_socket_get_name($srv, false);
$port = (int)substr($name, strrpos($name, ':') + 1);
$c = stream_socket_client("tcp://127.0.0.1:" . ($port + 65536));
echo "asked for ", $port + 65536, ", connected to ", stream_socket_get_name($c,
true), "\n";
$c2 = fsockopen("127.0.0.1", $port + 65536);
echo "fsockopen connected to ", stream_socket_get_name($c2, true), "\n";
```
Resulted in this output:
```
asked for 99735, connected to 127.0.0.1:34199
fsockopen connected to 127.0.0.1:34199
```
But I expected this output instead: both calls fail with an invalid port error, since 99735 is not a
valid TCP port (parse_url() already rejects such ports).
Reproduced on 8.2.34-dev (512c04e39f) and 8.4.28-dev; master has the same atoi() calls
(xp_socket.c lines 654 and 664).
This issue was found by an LLM-Enhanced Static Analysis Engine and verified manually.
### PHP Version
```
PHP 8.4.28-dev, PHP 8.2.34-dev
```
### Operating System
Linux (Ubuntu 20.04, x86_64)