Skip to content

Commit c0b32af

Browse files
committed
Fix GH-24170: read concatenated bzip2 streams
Fixes #24170.
1 parent 3efb356 commit c0b32af

6 files changed

Lines changed: 335 additions & 8 deletions

File tree

‎NEWS‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? ????, PHP 8.7.0alpha1
44

5+
- Bz2:
6+
. Fixed GH-24170 (compress.bzip2:// truncates concatenated bzip2 streams).
7+
(Alessio Attilio)
8+
59
- Core:
610
. A flat unkeyed list assignment from an array literal with one value per
711
target, such as [$a, $b] = [$b, $a], no longer builds the array when the

‎UPGRADING‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ PHP 8.7 UPGRADE NOTES
2222
- Core:
2323
. Block declaration support for the encoding declare has been removed as it
2424
did not function properly.
25+
- Bz2:
26+
. The compress.bzip2:// wrapper and bzopen() now read all concatenated bzip2
27+
streams instead of stopping after the first stream. bzdecompress() still
28+
decompresses only the first stream. Trailing data without a bzip2 header is
29+
still ignored.
2530

2631
- Curl:
2732
. curl_multi_exec() now throws a TypeError when $still_running cannot be

‎ext/bz2/bz2.c‎

Lines changed: 130 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,70 @@ ZEND_GET_MODULE(bz2)
5757

5858
struct php_bz2_stream_data_t {
5959
BZFILE *bz_file;
60+
FILE *file;
61+
bool stream_end;
62+
bool read_failed;
63+
bool report_read_errors;
64+
bool read_error_reported;
6065
zend_resource *stream_res;
6166
};
6267

6368
/* {{{ BZip2 stream implementation */
6469

70+
static void php_bz2iop_report_read_error(php_stream *stream)
71+
{
72+
struct php_bz2_stream_data_t *self = stream->abstract;
73+
74+
self->read_failed = true;
75+
stream->eof = 1;
76+
if (self->report_read_errors && !self->read_error_reported) {
77+
self->read_error_reported = true;
78+
php_error_docref(NULL, E_WARNING, "Failed to read bzip2 stream");
79+
}
80+
}
81+
82+
static bool php_bz2iop_next_stream(php_stream *stream)
83+
{
84+
struct php_bz2_stream_data_t *self = stream->abstract;
85+
char buffer[BZ_MAX_UNUSED];
86+
void *unused;
87+
int unused_len, error;
88+
89+
BZ2_bzReadGetUnused(&error, self->bz_file, &unused, &unused_len);
90+
if (error != BZ_OK) {
91+
php_bz2iop_report_read_error(stream);
92+
return false;
93+
}
94+
memcpy(buffer, unused, unused_len);
95+
while (unused_len < 4) {
96+
int ch = fgetc(self->file);
97+
if (ch == EOF) {
98+
self->read_failed = ferror(self->file) != 0;
99+
break;
100+
}
101+
buffer[unused_len++] = ch;
102+
}
103+
if (self->read_failed) {
104+
php_bz2iop_report_read_error(stream);
105+
return false;
106+
}
107+
if (unused_len < 4 || memcmp(buffer, "BZh", 3) != 0
108+
|| buffer[3] < '1' || buffer[3] > '9') {
109+
stream->eof = 1;
110+
return false;
111+
}
112+
113+
BZFILE *next = BZ2_bzReadOpen(&error, self->file, 0, strchr(stream->mode, 's') != NULL, buffer, unused_len);
114+
if (error != BZ_OK) {
115+
php_bz2iop_report_read_error(stream);
116+
return false;
117+
}
118+
BZ2_bzReadClose(&error, self->bz_file);
119+
self->bz_file = next;
120+
self->stream_end = false;
121+
return true;
122+
}
123+
65124
static ssize_t php_bz2iop_read(php_stream *stream, char *buf, size_t count)
66125
{
67126
struct php_bz2_stream_data_t *self = (struct php_bz2_stream_data_t *)stream->abstract;
@@ -72,17 +131,38 @@ static ssize_t php_bz2iop_read(php_stream *stream, char *buf, size_t count)
72131
size_t remain = count - ret;
73132
int to_read = (int)(remain <= INT_MAX ? remain : INT_MAX);
74133

75-
just_read = BZ2_bzread(self->bz_file, buf, to_read);
134+
if (self->read_failed) {
135+
return ret ? (ssize_t) ret : -1;
136+
}
137+
if (stream->eof) {
138+
return ret ? (ssize_t) ret : (self->read_failed ? -1 : 0);
139+
}
140+
if (self->file) {
141+
int error;
142+
if (self->stream_end && !php_bz2iop_next_stream(stream)) {
143+
return ret ? (ssize_t) ret : (self->read_failed ? -1 : 0);
144+
}
145+
just_read = BZ2_bzRead(&error, self->bz_file, buf + ret, to_read);
146+
self->stream_end = error == BZ_STREAM_END;
147+
if (error != BZ_OK && error != BZ_STREAM_END) {
148+
just_read = -1;
149+
} else if (just_read == 0 && self->stream_end) {
150+
continue;
151+
}
152+
} else {
153+
just_read = BZ2_bzread(self->bz_file, buf + ret, to_read);
154+
}
76155

77156
if (just_read < 1) {
78157
/* it is not safe to keep reading after an error, see #72613 */
79-
stream->eof = 1;
80158
if (just_read < 0) {
159+
php_bz2iop_report_read_error(stream);
81160
if (ret) {
82161
return ret;
83162
}
84163
return -1;
85164
}
165+
stream->eof = 1;
86166
break;
87167
}
88168

@@ -126,7 +206,13 @@ static int php_bz2iop_close(php_stream *stream, int close_handle)
126206
int ret = EOF;
127207

128208
if (close_handle) {
129-
BZ2_bzclose(self->bz_file);
209+
if (self->file) {
210+
int error;
211+
BZ2_bzReadClose(&error, self->bz_file);
212+
fclose(self->file);
213+
} else {
214+
BZ2_bzclose(self->bz_file);
215+
}
130216
}
131217

132218
/* The inner stream may have been closed by the user, only its resource is held */
@@ -164,14 +250,32 @@ const php_stream_ops php_stream_bz2io_ops = {
164250

165251
/* {{{ Bzip2 stream openers */
166252

167-
/* bzlib closes the descriptor it is given, so it gets a duplicate */
168-
static BZFILE *php_bz2_bzdopen(php_socket_t fd, const char *mode)
253+
static BZFILE *php_bz2_readopen(FILE *file, const char *mode)
254+
{
255+
int error;
256+
BZFILE *bz = BZ2_bzReadOpen(&error, file, 0, strchr(mode, 's') != NULL, NULL, 0);
257+
if (!bz) {
258+
fclose(file);
259+
}
260+
return bz;
261+
}
262+
263+
static BZFILE *php_bz2_bzdopen(php_socket_t fd, const char *mode, FILE **file)
169264
{
170265
int dup_fd = dup((int) fd);
171266
if (dup_fd == -1) {
172267
return NULL;
173268
}
174269

270+
if (mode[0] == 'r') {
271+
*file = fdopen(dup_fd, "rb");
272+
if (!*file) {
273+
close(dup_fd);
274+
return NULL;
275+
}
276+
return php_bz2_readopen(*file, mode);
277+
}
278+
175279
BZFILE *bz = BZ2_bzdopen(dup_fd, mode);
176280
if (!bz) {
177281
close(dup_fd);
@@ -193,6 +297,11 @@ PHP_BZ2_API php_stream *_php_stream_bz2open_from_BZFILE(BZFILE *bz,
193297
GC_ADDREF(innerstream->res);
194298
}
195299
self->bz_file = bz;
300+
self->file = NULL;
301+
self->stream_end = false;
302+
self->read_failed = false;
303+
self->report_read_errors = false;
304+
self->read_error_reported = false;
196305

197306
return php_stream_alloc_rel(&php_stream_bz2io_ops, self, 0, mode);
198307
}
@@ -207,6 +316,7 @@ PHP_BZ2_API php_stream *_php_stream_bz2open(php_stream_wrapper *wrapper,
207316
php_stream *retstream = NULL, *stream = NULL;
208317
char *path_copy = NULL;
209318
BZFILE *bz_file = NULL;
319+
FILE *file = NULL;
210320

211321
if (strncasecmp("compress.bzip2://", path, 17) == 0) {
212322
path += 17;
@@ -229,7 +339,14 @@ PHP_BZ2_API php_stream *_php_stream_bz2open(php_stream_wrapper *wrapper,
229339
}
230340

231341
/* try and open it directly first */
232-
bz_file = BZ2_bzopen(path_copy, mode);
342+
if (mode[0] == 'r') {
343+
file = VCWD_FOPEN(path_copy, "rb");
344+
if (file) {
345+
bz_file = php_bz2_readopen(file, mode);
346+
}
347+
} else {
348+
bz_file = BZ2_bzopen(path_copy, mode);
349+
}
233350

234351
if (opened_path && bz_file) {
235352
*opened_path = zend_string_init(path_copy, strlen(path_copy), 0);
@@ -246,7 +363,7 @@ PHP_BZ2_API php_stream *_php_stream_bz2open(php_stream_wrapper *wrapper,
246363
if (stream) {
247364
php_socket_t fd;
248365
if (SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD, (void **) &fd, REPORT_ERRORS)) {
249-
bz_file = php_bz2_bzdopen(fd, mode);
366+
bz_file = php_bz2_bzdopen(fd, mode, &file);
250367
}
251368
}
252369

@@ -261,6 +378,9 @@ PHP_BZ2_API php_stream *_php_stream_bz2open(php_stream_wrapper *wrapper,
261378
if (bz_file) {
262379
retstream = _php_stream_bz2open_from_BZFILE(bz_file, mode, stream STREAMS_REL_CC);
263380
if (retstream) {
381+
struct php_bz2_stream_data_t *self = retstream->abstract;
382+
self->file = file;
383+
self->report_read_errors = wrapper != NULL;
264384
return retstream;
265385
}
266386

@@ -358,6 +478,7 @@ PHP_FUNCTION(bzopen)
358478

359479
BZFILE *bz; /* The compressed file stream */
360480
php_stream *stream = NULL;
481+
FILE *handle = NULL;
361482

362483
if (zend_parse_parameters(ZEND_NUM_ARGS(), "zs", &file, &mode, &mode_len) == FAILURE) {
363484
RETURN_THROWS();
@@ -432,12 +553,13 @@ PHP_FUNCTION(bzopen)
432553
RETURN_FALSE;
433554
}
434555

435-
bz = php_bz2_bzdopen(fd, mode);
556+
bz = php_bz2_bzdopen(fd, mode, &handle);
436557
if (!bz) {
437558
RETURN_FALSE;
438559
}
439560

440561
stream = php_stream_bz2open_from_BZFILE(bz, mode, stream);
562+
((struct php_bz2_stream_data_t *) stream->abstract)->file = handle;
441563
} else {
442564
zend_argument_type_error(1, "must be of type string or file-resource, %s given", zend_zval_value_name(file));
443565
RETURN_THROWS();

‎ext/bz2/tests/gh24170.phpt‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
--TEST--
2+
GH-24170 (bzip2 streams read all concatenated members)
3+
--EXTENSIONS--
4+
bz2
5+
--FILE--
6+
<?php
7+
$file = __DIR__ . '/gh24170.bz2';
8+
$parts = ['', str_repeat('a', 900000), '', 'tail', ''];
9+
$binary = '';
10+
for ($i = 0; $i < 400; $i++) {
11+
$binary .= hash('sha256', (string) $i, true);
12+
}
13+
$parts[] = $binary;
14+
$parts[] = '';
15+
$compressed = '';
16+
foreach ($parts as $i => $part) {
17+
$compressed .= bzcompress($part, $i + 1);
18+
}
19+
file_put_contents($file, $compressed);
20+
$expected = implode('', $parts);
21+
var_dump(file_get_contents('compress.bzip2://' . $file) === $expected);
22+
$copy = $file . '.out';
23+
var_dump(copy('compress.bzip2://' . $file, $copy));
24+
var_dump(file_get_contents($copy) === $expected);
25+
unlink($copy);
26+
27+
foreach ([1, 17, 8192, 900000, 1000000] as $size) {
28+
$fp = fopen('compress.bzip2://' . $file, 'rb');
29+
$offset = 0;
30+
$matches = true;
31+
while (!feof($fp)) {
32+
$chunk = fread($fp, $size);
33+
$matches = $matches && $chunk === substr($expected, $offset, strlen($chunk));
34+
$offset += strlen($chunk);
35+
}
36+
var_dump($matches && $offset === strlen($expected));
37+
var_dump(fread($fp, 1));
38+
fclose($fp);
39+
}
40+
41+
foreach ([false, true] as $resource) {
42+
$inner = $resource ? fopen($file, 'rb') : null;
43+
$fp = bzopen($inner ?? $file, 'r');
44+
if ($inner) {
45+
fclose($inner);
46+
}
47+
$result = '';
48+
while (!feof($fp)) {
49+
$result .= bzread($fp, 8192);
50+
}
51+
var_dump($result === $expected);
52+
var_dump(bzerrno($fp));
53+
var_dump(bzread($fp, 1));
54+
bzclose($fp);
55+
}
56+
57+
file_put_contents($file, str_repeat(bzcompress(''), 400) . bzcompress('end'));
58+
var_dump(file_get_contents('compress.bzip2://' . $file));
59+
file_put_contents($file, str_repeat(bzcompress(''), 400));
60+
var_dump(file_get_contents('compress.bzip2://' . $file));
61+
unlink($file);
62+
?>
63+
--CLEAN--
64+
<?php
65+
@unlink(__DIR__ . '/gh24170.bz2');
66+
@unlink(__DIR__ . '/gh24170.bz2.out');
67+
?>
68+
--EXPECT--
69+
bool(true)
70+
bool(true)
71+
bool(true)
72+
bool(true)
73+
string(0) ""
74+
bool(true)
75+
string(0) ""
76+
bool(true)
77+
string(0) ""
78+
bool(true)
79+
string(0) ""
80+
bool(true)
81+
string(0) ""
82+
bool(true)
83+
int(0)
84+
string(0) ""
85+
bool(true)
86+
int(0)
87+
string(0) ""
88+
string(3) "end"
89+
string(0) ""
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
--TEST--
2+
GH-24170 (concatenated bzip2 headers split across the compressed input buffer)
3+
--EXTENSIONS--
4+
bz2
5+
--FILE--
6+
<?php
7+
$file = __DIR__ . '/gh24170_boundaries.bz2';
8+
foreach ([[0, 4502], [2, 4483], [0, 4521], [2, 4479]] as [$seed, $length]) {
9+
$data = '';
10+
for ($i = 0; $i < 200; $i++) {
11+
$data .= hash('sha256', "$seed:$i", true);
12+
}
13+
$data = substr($data, 0, $length);
14+
$compressed = bzcompress($data, 9);
15+
var_dump(strlen($compressed));
16+
file_put_contents($file, $compressed . bzcompress('tail'));
17+
$fp = fopen('compress.bzip2://file://' . $file, 'rb');
18+
stream_set_chunk_size($fp, 1);
19+
var_dump(fread($fp, 10000) === $data . 'tail');
20+
var_dump(feof($fp));
21+
fclose($fp);
22+
}
23+
unlink($file);
24+
?>
25+
--CLEAN--
26+
<?php
27+
@unlink(__DIR__ . '/gh24170_boundaries.bz2');
28+
?>
29+
--EXPECT--
30+
int(4997)
31+
bool(true)
32+
bool(true)
33+
int(4998)
34+
bool(true)
35+
bool(true)
36+
int(4999)
37+
bool(true)
38+
bool(true)
39+
int(5000)
40+
bool(true)
41+
bool(true)

0 commit comments

Comments
 (0)