ext/sockets: socket_sendto check port range.

close GH-17299
This commit is contained in:
David Carlier 2024-12-29 18:01:43 +00:00
parent 72ff9077a5
commit 665ebd7f48
No known key found for this signature in database
GPG key ID: 8486F847B4B94EF1
4 changed files with 33 additions and 3 deletions

2
NEWS
View file

@ -86,6 +86,8 @@ PHP NEWS
(David Carlier) (David Carlier)
. socket_bind() throws an exception on invalid port value. . socket_bind() throws an exception on invalid port value.
(David Carlier) (David Carlier)
. socket_sendto() throws an exception on invalid port value.
(David Carlier)
- Standard: - Standard:
. Fixed crypt() tests on musl when using --with-external-libcrypt . Fixed crypt() tests on musl when using --with-external-libcrypt

View file

@ -125,8 +125,8 @@ PHP 8.5 UPGRADE NOTES
last_error to EBADF and raises an E_WARNING message. last_error to EBADF and raises an E_WARNING message.
- Sockets: - Sockets:
. socket_create_listen and socket_bind throw a ValueError . socket_create_listen, socket_bind and socket_sendto throw a
if the port is lower than 0 or greater than 65535. ValueError if the port is lower than 0 or greater than 65535.
- Zlib: - Zlib:
. The "use_include_path" argument for the . The "use_include_path" argument for the

View file

@ -1569,7 +1569,7 @@ PHP_FUNCTION(socket_sendto)
#endif #endif
int retval; int retval;
size_t buf_len, addr_len; size_t buf_len, addr_len;
zend_long len, flags, port; zend_long len, flags, port = 0;
bool port_is_null = 1; bool port_is_null = 1;
char *buf, *addr; char *buf, *addr;
@ -1586,6 +1586,12 @@ PHP_FUNCTION(socket_sendto)
php_sock = Z_SOCKET_P(arg1); php_sock = Z_SOCKET_P(arg1);
ENSURE_SOCKET_VALID(php_sock); ENSURE_SOCKET_VALID(php_sock);
if (port < 0 || port > USHRT_MAX) {
zend_argument_value_error(6, "must be between 0 and %u", USHRT_MAX);
RETURN_THROWS();
}
if (len < 0) { if (len < 0) {
zend_argument_value_error(3, "must be greater than or equal to 0"); zend_argument_value_error(3, "must be greater than or equal to 0");
RETURN_THROWS(); RETURN_THROWS();

View file

@ -0,0 +1,22 @@
--TEST--
socket_sendto() with invalid port
--EXTENSIONS--
sockets
--FILE--
<?php
$s_c = socket_create_listen(0);
try {
$s_w = socket_sendto($s_c, "foo", 0, MSG_OOB, '127.0.0.1', 65536);
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}
try {
$s_w = socket_sendto($s_c, "foo", 0, MSG_OOB, '127.0.0.1', -1);
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}
socket_close($s_c);
?>
--EXPECT--
socket_sendto(): Argument #6 ($port) must be between 0 and 65535
socket_sendto(): Argument #6 ($port) must be between 0 and 65535