Skip to content

Commit 4b2a580

Browse files
committed
Merge branch 'PHP-8.4'
* PHP-8.4: Fix GH-18642: Signed integer overflow in ext/phar fseek
2 parents c789e9c + d6ed107 commit 4b2a580

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

ext/phar/stream.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ static int phar_stream_seek(php_stream *stream, zend_off_t offset, int whence, z
400400
phar_entry_data *data = (phar_entry_data *)stream->abstract;
401401
phar_entry_info *entry;
402402
int res;
403-
zend_off_t temp;
403+
zend_ulong temp;
404404

405405
if (data->internal_file->link) {
406406
entry = phar_get_link_source(data->internal_file);
@@ -410,26 +410,28 @@ static int phar_stream_seek(php_stream *stream, zend_off_t offset, int whence, z
410410

411411
switch (whence) {
412412
case SEEK_END :
413-
temp = data->zero + entry->uncompressed_filesize + offset;
413+
temp = (zend_ulong) data->zero + (zend_ulong) entry->uncompressed_filesize + (zend_ulong) offset;
414414
break;
415415
case SEEK_CUR :
416-
temp = data->zero + data->position + offset;
416+
temp = (zend_ulong) data->zero + (zend_ulong) data->position + (zend_ulong) offset;
417417
break;
418418
case SEEK_SET :
419-
temp = data->zero + offset;
419+
temp = (zend_ulong) data->zero + (zend_ulong) offset;
420420
break;
421421
default:
422422
temp = 0;
423423
}
424-
if (temp > data->zero + (zend_off_t) entry->uncompressed_filesize) {
425-
*newoffset = -1;
424+
425+
zend_off_t temp_signed = (zend_off_t) temp;
426+
if (temp_signed > data->zero + (zend_off_t) entry->uncompressed_filesize) {
427+
*newoffset = -1; /* FIXME: this will invalidate the ZEND_ASSERT(stream->position >= 0); assertion in streams.c */
426428
return -1;
427429
}
428-
if (temp < data->zero) {
429-
*newoffset = -1;
430+
if (temp_signed < data->zero) {
431+
*newoffset = -1; /* FIXME: this will invalidate the ZEND_ASSERT(stream->position >= 0); assertion in streams.c */
430432
return -1;
431433
}
432-
res = php_stream_seek(data->fp, temp, SEEK_SET);
434+
res = php_stream_seek(data->fp, temp_signed, SEEK_SET);
433435
*newoffset = php_stream_tell(data->fp) - data->zero;
434436
data->position = *newoffset;
435437
return res;

ext/phar/tests/gh18642.phpt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
GH-18642 (Signed integer overflow in ext/phar fseek)
3+
--EXTENSIONS--
4+
phar
5+
--INI--
6+
phar.require_hash=0
7+
--FILE--
8+
<?php
9+
require_once __DIR__ . '/files/phar_oo_test.inc';
10+
class MyFile extends SplFileObject
11+
{
12+
}
13+
$phar = new Phar($fname);
14+
$phar->setInfoClass('MyFile');
15+
$f = $phar['a.php'];
16+
var_dump($f->fseek(PHP_INT_MAX));
17+
var_dump($f->fseek(0));
18+
var_dump($f->fseek(PHP_INT_MIN, SEEK_END));
19+
var_dump($f->fseek(0, SEEK_SET));
20+
var_dump($f->fseek(1, SEEK_CUR));
21+
var_dump($f->fseek(PHP_INT_MAX, SEEK_CUR));
22+
?>
23+
--EXPECT--
24+
int(-1)
25+
int(0)
26+
int(-1)
27+
int(0)
28+
int(0)
29+
int(-1)

0 commit comments

Comments
 (0)