Skip to content

Commit 1f1147a

Browse files
committed
Fix GH-20620: bzcompress() overflow on large source size.
close GH-20621
1 parent 9f654de commit 1f1147a

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

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.3.30
44

5+
- Bz2:
6+
. Fixed bug GH-20620 (bzcompress overflow on large source size).
7+
(David Carlier)
8+
59
- GD:
610
. Fixed bug GH-20622 (imagestring/imagestringup overflow). (David Carlier)
711

ext/bz2/bz2.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,15 @@ PHP_FUNCTION(bzcompress)
459459
+ .01 x length of data + 600 which is the largest size the results of the compression
460460
could possibly be, at least that's what the libbz2 docs say (thanks to jeremy@nirvani.net
461461
for pointing this out). */
462-
dest_len = (unsigned int) (source_len + (0.01 * source_len) + 600);
462+
size_t chunk_len = source_len + source_len / 100 + 600;
463+
const size_t min = MIN(ZSTR_MAX_LEN, UINT_MAX);
464+
465+
if (chunk_len < source_len || chunk_len > min) {
466+
zend_argument_value_error(1, "must have a length less than or equal to %zu", min);
467+
RETURN_THROWS();
468+
}
469+
470+
dest_len = (unsigned int) chunk_len;
463471

464472
/* Allocate the destination buffer */
465473
dest = zend_string_alloc(dest_len, 0);

ext/bz2/tests/gh20620.phpt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
Bug GH-20620 (bzcompress with large source)
3+
--EXTENSIONS--
4+
bz2
5+
--SKIPIF--
6+
<?php
7+
if (PHP_INT_SIZE != 8) die('skip this test is for 64bit platforms only');
8+
if (getenv('SKIP_SLOW_TESTS')) die('skip slow tests excluded by request');
9+
?>
10+
--INI--
11+
memory_limit=-1
12+
--FILE--
13+
<?php
14+
try {
15+
bzcompress(str_repeat('1', 4295163906));
16+
} catch (\ValueError $e) {
17+
echo $e->getMessage(), PHP_EOL;
18+
}
19+
?>
20+
--EXPECTF--
21+
bzcompress(): Argument #1 ($data) must have a length less than or equal to %d

0 commit comments

Comments
 (0)