Skip to content

Commit 8ecfdb7

Browse files
committed
ext/sockets: socket_bind() check port validity.
range from ephemeral port (0) to max unsigned 16 bits.
1 parent 9eb2284 commit 8ecfdb7

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

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)