Skip to content

Commit

Permalink
ext/sockets: socket_bind() check port validity.
Browse files Browse the repository at this point in the history
range from ephemeral port (0) to max unsigned 16 bits.

close GH-17296
  • Loading branch information
devnexen committed Dec 29, 2024
1 parent 8120c79 commit caafa04
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ PHP NEWS
TCP_REUSPORT_LB_CURDOM, TCP_BBR_ALGORITHM constants.
. socket_create_listen() throws an exception on invalid port value.
(David Carlier)
. socket_bind() throws an exception on invalid port value.
(David Carlier)

- Standard:
. Fixed crypt() tests on musl when using --with-external-libcrypt
Expand Down
4 changes: 2 additions & 2 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ PHP 8.5 UPGRADE NOTES
last_error to EBADF and raises an E_WARNING message.

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

- Zlib:
. The "use_include_path" argument for the
Expand Down
5 changes: 5 additions & 0 deletions ext/sockets/sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -1288,6 +1288,11 @@ PHP_FUNCTION(socket_bind)
php_sock = Z_SOCKET_P(arg1);
ENSURE_SOCKET_VALID(php_sock);

if (port < 0 || port > USHRT_MAX) {
zend_argument_value_error(3, "must be between 0 and %u", USHRT_MAX);
RETURN_THROWS();
}

switch(php_sock->type) {
case AF_UNIX:
{
Expand Down
23 changes: 23 additions & 0 deletions ext/sockets/tests/socket_bind_invalid_port.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--TEST--
socket_bind() with invalid ports.
--EXTENSIONS--
sockets
--FILE--
<?php
$s_c = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

try {
socket_bind($s_c, '0.0.0.0', -1);
} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}

try {
socket_bind($s_c, '0.0.0.0', 65536);
} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}
?>
--EXPECT--
socket_bind(): Argument #3 ($port) must be between 0 and 65535
socket_bind(): Argument #3 ($port) must be between 0 and 65535

0 comments on commit caafa04

Please sign in to comment.