Skip to content

Commit 00fe9b2

Browse files
committed
ext/sockets: socket_create_listen() check port value beforehand.
port is a 16 bit field, limited to the 65535 value then. Note that 0 is a valid case for ephemeral port. close GH-17281
1 parent 2b36680 commit 00fe9b2

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ PHP NEWS
8282
with timeout setting on windows. (David Carlier)
8383
. Added TCP_FUNCTION_ALIAS, TCP_REUSPORT_LB_NUMA, TCP_REUSPORT_LB_NUMA_NODOM,
8484
TCP_REUSPORT_LB_CURDOM, TCP_BBR_ALGORITHM constants.
85+
. socket_create_listen() throws an exception on invalid port value.
86+
(David Carlier)
8587

8688
- Standard:
8789
. Fixed crypt() tests on musl when using --with-external-libcrypt

UPGRADING

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ PHP 8.5 UPGRADE NOTES
124124
. posix_fpathconf checks invalid file descriptors and sets
125125
last_error to EBADF and raises an E_WARNING message.
126126

127+
- Sockets:
128+
. socket_create_listen throws a ValueError if the port is
129+
lower than 0 or greater than 65535.
130+
127131
- Zlib:
128132
. The "use_include_path" argument for the
129133
gzfile, gzopen and readgzfile functions had been changed

ext/sockets/sockets.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ zend_module_entry sockets_module_entry = {
217217
ZEND_GET_MODULE(sockets)
218218
#endif
219219

220-
static bool php_open_listen_sock(php_socket *sock, int port, int backlog) /* {{{ */
220+
static bool php_open_listen_sock(php_socket *sock, unsigned short port, int backlog) /* {{{ */
221221
{
222222
struct sockaddr_in la = {0};
223223
struct hostent *hp;
@@ -232,7 +232,7 @@ static bool php_open_listen_sock(php_socket *sock, int port, int backlog) /* {{{
232232

233233
memcpy((char *) &la.sin_addr, hp->h_addr, hp->h_length);
234234
la.sin_family = hp->h_addrtype;
235-
la.sin_port = htons((unsigned short) port);
235+
la.sin_port = htons(port);
236236

237237
sock->bsd_socket = socket(PF_INET, SOCK_STREAM, 0);
238238
sock->blocking = 1;
@@ -680,10 +680,15 @@ PHP_FUNCTION(socket_create_listen)
680680
Z_PARAM_LONG(backlog)
681681
ZEND_PARSE_PARAMETERS_END();
682682

683+
if (port < 0 || port > USHRT_MAX) {
684+
zend_argument_value_error(1, "must be between 0 and %u", USHRT_MAX);
685+
RETURN_THROWS();
686+
}
687+
683688
object_init_ex(return_value, socket_ce);
684689
php_sock = Z_SOCKET_P(return_value);
685690

686-
if (!php_open_listen_sock(php_sock, port, backlog)) {
691+
if (!php_open_listen_sock(php_sock, (unsigned short)port, backlog)) {
687692
zval_ptr_dtor(return_value);
688693
RETURN_FALSE;
689694
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
socket_create_listen() using invalid ports
3+
--EXTENSIONS--
4+
sockets
5+
--FILE--
6+
<?php
7+
var_dump(socket_create_listen(0));
8+
9+
try {
10+
socket_create_listen(-1);
11+
} catch (\ValueError $e) {
12+
echo $e->getMessage() . PHP_EOL;
13+
}
14+
try {
15+
socket_create_listen(65536);
16+
} catch (\ValueError $e) {
17+
echo $e->getMessage() . PHP_EOL;
18+
}
19+
?>
20+
--EXPECT--
21+
object(Socket)#1 (0) {
22+
}
23+
socket_create_listen(): Argument #1 ($port) must be between 0 and 65535
24+
socket_create_listen(): Argument #1 ($port) must be between 0 and 65535

0 commit comments

Comments
 (0)