Skip to content

ext/sockets: socket_create_listen() check port value beforehand. #17281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions ext/sockets/sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ zend_module_entry sockets_module_entry = {
ZEND_GET_MODULE(sockets)
#endif

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

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

sock->bsd_socket = socket(PF_INET, SOCK_STREAM, 0);
sock->blocking = 1;
Expand Down Expand Up @@ -680,10 +680,15 @@ PHP_FUNCTION(socket_create_listen)
Z_PARAM_LONG(backlog)
ZEND_PARSE_PARAMETERS_END();

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

object_init_ex(return_value, socket_ce);
php_sock = Z_SOCKET_P(return_value);

if (!php_open_listen_sock(php_sock, port, backlog)) {
if (!php_open_listen_sock(php_sock, (unsigned short)port, backlog)) {
zval_ptr_dtor(return_value);
RETURN_FALSE;
}
Expand Down
24 changes: 24 additions & 0 deletions ext/sockets/tests/socket_create_listen_invalid_port.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
socket_create_listen() using invalid ports
--EXTENSIONS--
sockets
--FILE--
<?php
var_dump(socket_create_listen(0));

try {
socket_create_listen(-1);
} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}
try {
socket_create_listen(65536);
} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}
?>
--EXPECT--
object(Socket)#1 (0) {
}
socket_create_listen(): Argument #1 ($port) must be between 0 and 65535
socket_create_listen(): Argument #1 ($port) must be between 0 and 65535
Loading