Skip to content
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

Fix GH-15980: Signed integer overflow in main/streams/streams.c #15989

Closed
wants to merge 3 commits into from

Conversation

cmb69
Copy link
Member

@cmb69 cmb69 commented Sep 22, 2024

We need to avoid signed integer overflows which are undefined behavior. We catch that, and set offset to ZEND_LONG_MAX (which is also the largest value of zend_off_t on all platforms). Of course, after such a seek a stream is no longer readable, but that matches the current behavior for offsets near ZEND_LONG_MAX.

We need to avoid signed integer overflows which are undefined behavior.
We catch that, and set `offset` to `ZEND_LONG_MAX` (which is also the
largest value of `zend_off_t` on all platforms).  Of course, after such
a seek a stream is no longer readable, but that matches the current
behavior for offsets near `ZEND_LONG_MAX`.
@cmb69 cmb69 requested a review from bukka as a code owner September 22, 2024 18:39
@cmb69 cmb69 linked an issue Sep 22, 2024 that may be closed by this pull request
@cmb69 cmb69 changed the base branch from master to PHP-8.2 September 22, 2024 18:44
Comment on lines +1358 to +1360
if (UNEXPECTED(offset > ZEND_LONG_MAX - stream->position)) {
offset = ZEND_LONG_MAX;
} else {
Copy link
Member

Choose a reason for hiding this comment

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

Long term we probably should warn about those large offsets

Copy link
Member Author

@cmb69 cmb69 Sep 22, 2024

Choose a reason for hiding this comment

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

Yeah, although it's somewhat hard to determine what is too large. Something like ZEND_LONG_MAX - chunk size may work. (I'm mostly thinking about 32bit systems.)

@cmb69 cmb69 closed this in 6a04c79 Sep 22, 2024
@cmb69 cmb69 deleted the cmb/gh15980 branch September 22, 2024 22:31
@cmb69 cmb69 restored the cmb/gh15980 branch September 22, 2024 23:35
@cmb69
Copy link
Member Author

cmb69 commented Sep 22, 2024

The new test case fails on 64bit Linux, so this needs closer investigation; for now I have reverted the commits.

@cmb69 cmb69 reopened this Sep 22, 2024
@cmb69 cmb69 mentioned this pull request Sep 22, 2024
For some OS/FS, `fseek()` may fail, so `ftell()` will obviously report
the old position.
@cmb69 cmb69 removed the ABI break label Sep 23, 2024
@cmb69 cmb69 closed this in 8191675 Sep 24, 2024
@cmb69 cmb69 deleted the cmb/gh15980 branch September 24, 2024 10:36
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.

Signed integer overflow in main/streams/streams.c
3 participants