Skip to content

Fix GH-17921 socket_read/socket_recv overflows on buffer size. #17923

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 1 commit 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
4 changes: 2 additions & 2 deletions ext/sockets/sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,7 @@ PHP_FUNCTION(socket_read)
ENSURE_SOCKET_VALID(php_sock);

/* overflow check */
if ((length + 1) < 2) {
if (length <= 0 || length == ZEND_LONG_MAX) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, the max allowed string length is ZSTR_MAX_LEN, which is less than ZEND_LONG_MAX.
So you should change the checks to

Suggested change
if (length <= 0 || length == ZEND_LONG_MAX) {
if (length <= 0 || length > ZSTR_MAX_LEN) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, the length check in socket_recvfrom should be adjusted.

RETURN_FALSE;
}

Expand Down Expand Up @@ -1326,7 +1326,7 @@ PHP_FUNCTION(socket_recv)
ENSURE_SOCKET_VALID(php_sock);

/* overflow check */
if ((len + 1) < 2) {
if (len <= 0 || len == ZEND_LONG_MAX) {
RETURN_FALSE;
}

Expand Down
18 changes: 18 additions & 0 deletions ext/sockets/tests/gh17921.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
GH-16267 - overflow on socket_strerror argument
--EXTENSIONS--
sockets
--FILE--
<?php
$s_c_l = socket_create_listen(0);
var_dump(socket_read($s_c_l, PHP_INT_MAX));
var_dump(socket_read($s_c_l, PHP_INT_MIN));
$a = "";
var_dump(socket_recv($s_c_l, $a, PHP_INT_MAX, 0));
var_dump(socket_recv($s_c_l, $a, PHP_INT_MIN, 0));
?>
--EXPECT--
bool(false)
bool(false)
bool(false)
bool(false)
Loading