-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Signed integer overflow in main/streams/streams.c #15980
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
Labels
Comments
Simple reproducer: <?php
$s = fopen(__FILE__, "r");
fseek($s, 1);
fseek($s, PHP_INT_MAX, SEEK_CUR); Possible fix: main/streams/streams.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/main/streams/streams.c b/main/streams/streams.c
index f3fb5e3cda..0fbb1a13fc 100644
--- a/main/streams/streams.c
+++ b/main/streams/streams.c
@@ -1382,7 +1382,12 @@ PHPAPI int _php_stream_seek(php_stream *stream, zend_off_t offset, int whence)
switch(whence) {
case SEEK_CUR:
- offset = stream->position + offset;
+ ZEND_ASSERT(stream->position >= 0);
+ if (offset > ZEND_LONG_MAX - stream->position) {
+ offset = ZEND_LONG_MAX;
+ } else {
+ offset = stream->position + offset;
+ }
whence = SEEK_SET;
break;
} Older PHP versions are likely affected as well. |
cmb69
added a commit
to cmb69/php-src
that referenced
this issue
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`.
cmb69
added a commit
that referenced
this issue
Sep 22, 2024
* PHP-8.2: Fix GH-15980: Signed integer overflow in main/streams/streams.c
cmb69
added a commit
that referenced
this issue
Sep 22, 2024
* PHP-8.3: Fix GH-15980: Signed integer overflow in main/streams/streams.c
cmb69
added a commit
that referenced
this issue
Sep 22, 2024
cmb69
added a commit
that referenced
this issue
Sep 22, 2024
* PHP-8.2: Revert "Fix GH-15980: Signed integer overflow in main/streams/streams.c"
cmb69
added a commit
that referenced
this issue
Sep 22, 2024
* PHP-8.3: Revert "Fix GH-15980: Signed integer overflow in main/streams/streams.c"
I had to revert, so reopening. |
cmb69
added a commit
that referenced
this issue
Sep 24, 2024
* PHP-8.2: Fix GH-15980: Signed integer overflow in main/streams/streams.c
cmb69
added a commit
that referenced
this issue
Sep 24, 2024
* PHP-8.3: Fix GH-15980: Signed integer overflow in main/streams/streams.c
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Description
The following code:
Resulted in this output:
PHP Version
PHP 8.4.0-dev
Operating System
ubuntu 22.04
The text was updated successfully, but these errors were encountered: