Skip to content

Commit d6ed107

Browse files
committed
Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3: Fix GH-18642: Signed integer overflow in ext/phar fseek
2 parents 8776631 + 61884c3 commit d6ed107

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ PHP NEWS
1919

2020
- Phar:
2121
. Add missing filter cleanups on phar failure. (nielsdos)
22+
. Fixed bug GH-18642 (Signed integer overflow in ext/phar fseek). (nielsdos)
2223

2324
- Readline:
2425
. Fix memory leak when calloc() fails in php_readline_completion_cb().

ext/phar/stream.c

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

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

415415
switch (whence) {
416416
case SEEK_END :
417-
temp = data->zero + entry->uncompressed_filesize + offset;
417+
temp = (zend_ulong) data->zero + (zend_ulong) entry->uncompressed_filesize + (zend_ulong) offset;
418418
break;
419419
case SEEK_CUR :
420-
temp = data->zero + data->position + offset;
420+
temp = (zend_ulong) data->zero + (zend_ulong) data->position + (zend_ulong) offset;
421421
break;
422422
case SEEK_SET :
423-
temp = data->zero + offset;
423+
temp = (zend_ulong) data->zero + (zend_ulong) offset;
424424
break;
425425
default:
426426
temp = 0;
427427
}
428-
if (temp > data->zero + (zend_off_t) entry->uncompressed_filesize) {
429-
*newoffset = -1;
428+
429+
zend_off_t temp_signed = (zend_off_t) temp;
430+
if (temp_signed > data->zero + (zend_off_t) entry->uncompressed_filesize) {
431+
*newoffset = -1; /* FIXME: this will invalidate the ZEND_ASSERT(stream->position >= 0); assertion in streams.c */
430432
return -1;
431433
}
432-
if (temp < data->zero) {
433-
*newoffset = -1;
434+
if (temp_signed < data->zero) {
435+
*newoffset = -1; /* FIXME: this will invalidate the ZEND_ASSERT(stream->position >= 0); assertion in streams.c */
434436
return -1;
435437
}
436-
res = php_stream_seek(data->fp, temp, SEEK_SET);
438+
res = php_stream_seek(data->fp, temp_signed, SEEK_SET);
437439
*newoffset = php_stream_tell(data->fp) - data->zero;
438440
data->position = *newoffset;
439441
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)