-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The old code was like the textbook example of how not to check for overflow. It would've been fine-ish if it were unsigned math, but the new code is clearer anyway.
Anyway, please see my comments.
ext/sockets/tests/gh17921.phpt
Outdated
--EXTENSIONS-- | ||
sockets | ||
--INI-- | ||
memory_limit=-1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you add this INI setting?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... I have Allowed memory size warning otherwise...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm but these functions all return false 🤔 i.e. they shouldn't reach the code that allocates the memory?
Actually, you adjusted the checks incorrectly though after my review, so you will actually reach this allocation code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So ... isn't ZSTR_MAX_LEN fit for zend_ulong ? length is a zend_long. ZSTR_MAX_LEN > ZEND_LONG_MAX
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems I was wrong adding the ini setting only.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So ... isn't ZSTR_MAX_LEN fit for zend_ulong ? length is a zend_long. ZSTR_MAX_LEN > ZEND_LONG_MAX
Right, my bad, then this is fine
@@ -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) { |
There was a problem hiding this comment.
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
if (length <= 0 || length == ZEND_LONG_MAX) { | |
if (length <= 0 || length > ZSTR_MAX_LEN) { |
There was a problem hiding this comment.
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.
8b8f7e4
to
9262118
Compare
update the existing checks to be more straightforward instead of counting on undefined behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks
update the existing checks to be more straightforward instead of counting on undefined behavior.