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

Conversation

devnexen
Copy link
Member

update the existing checks to be more straightforward instead of counting on undefined behavior.

Copy link
Member

@nielsdos nielsdos left a 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.

--EXTENSIONS--
sockets
--INI--
memory_limit=-1
Copy link
Member

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?

Copy link
Member Author

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...

Copy link
Member

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

Copy link
Member Author

@devnexen devnexen Feb 25, 2025

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

Copy link
Member Author

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.

Copy link
Member

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) {
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.

@devnexen devnexen force-pushed the gh17921 branch 2 times, most recently from 8b8f7e4 to 9262118 Compare February 25, 2025 20:43
update the existing checks to be more straightforward instead of
counting on undefined behavior.
Copy link
Member

@nielsdos nielsdos left a comment

Choose a reason for hiding this comment

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

Thanks

@devnexen devnexen closed this in 8cbc0c5 Feb 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Integer overflow in ext/sockets/sockets.c
2 participants