Skip to content

Commit caafa04

Browse files
committed
ext/sockets: socket_bind() check port validity.
range from ephemeral port (0) to max unsigned 16 bits. close GH-17296
1 parent 8120c79 commit caafa04

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ PHP NEWS
8484
TCP_REUSPORT_LB_CURDOM, TCP_BBR_ALGORITHM constants.
8585
. socket_create_listen() throws an exception on invalid port value.
8686
(David Carlier)
87+
. socket_bind() throws an exception on invalid port value.
88+
(David Carlier)
8789

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

UPGRADING

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ PHP 8.5 UPGRADE NOTES
125125
last_error to EBADF and raises an E_WARNING message.
126126

127127
- Sockets:
128-
. socket_create_listen throws a ValueError if the port is
129-
lower than 0 or greater than 65535.
128+
. socket_create_listen and socket_bind throw a ValueError
129+
if the port is lower than 0 or greater than 65535.
130130

131131
- Zlib:
132132
. The "use_include_path" argument for the

ext/sockets/sockets.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,11 @@ PHP_FUNCTION(socket_bind)
12881288
php_sock = Z_SOCKET_P(arg1);
12891289
ENSURE_SOCKET_VALID(php_sock);
12901290

1291+
if (port < 0 || port > USHRT_MAX) {
1292+
zend_argument_value_error(3, "must be between 0 and %u", USHRT_MAX);
1293+
RETURN_THROWS();
1294+
}
1295+
12911296
switch(php_sock->type) {
12921297
case AF_UNIX:
12931298
{
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
socket_bind() with invalid ports.
3+
--EXTENSIONS--
4+
sockets
5+
--FILE--
6+
<?php
7+
$s_c = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
8+
9+
try {
10+
socket_bind($s_c, '0.0.0.0', -1);
11+
} catch (\ValueError $e) {
12+
echo $e->getMessage() . PHP_EOL;
13+
}
14+
15+
try {
16+
socket_bind($s_c, '0.0.0.0', 65536);
17+
} catch (\ValueError $e) {
18+
echo $e->getMessage() . PHP_EOL;
19+
}
20+
?>
21+
--EXPECT--
22+
socket_bind(): Argument #3 ($port) must be between 0 and 65535
23+
socket_bind(): Argument #3 ($port) must be between 0 and 65535

0 commit comments

Comments
 (0)