Bug #71506 [NEW]: inflate_add() does not detect corrupted compressed data
| From: | salsi at icosaedro dot it | Date: | Wed, 03 Feb 2016 10:54:27 +0000 |
| Subject: | Bug #71506 [NEW]: inflate_add() does not detect corrupted compressed data | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-199020@lists.php.net to get a copy of this message | ||
From: salsi at icosaedro dot it
Operating system: Slackware 14.1
PHP version: Irrelevant
Package: Zlib related
Bug Type: Bug
Bug description:inflate_add() does not detect corrupted compressed data
Description:
------------
The inflate_add() function is available as of PHP 7.0.0 and it
incrementally decompresses DEFLATE, ZLIB and GZIP compressed data passed
chunk by chunk. When GZIP compressed data are feed to this function, it
succeeds detecting corrupted data, but it fails to detect trunked data,
as the following test script proves.
Test script:
---------------
<?php
error_reporting(-1);
// $deflateContext = deflate_init(ZLIB_ENCODING_GZIP);
// $compressed = deflate_add($deflateContext, "Data to compress",
ZLIB_NO_FLUSH);
// $compressed .= deflate_add($deflateContext, ", more data",
ZLIB_NO_FLUSH);
// $compressed .= deflate_add($deflateContext, ", and even more data!",
ZLIB_FINISH);
// Creates GZIP compressed data:
$plain = "Data to compress, more data, and even more data!";
$compressed = gzencode($plain);
echo "Compressed: ", wordwrap(bin2hex($compressed), 2, " ", TRUE),
"\n";
// These tests succeeded with error, as expected:
// Invalid checksum:
// $compressed[strlen($compressed)-5] = 'x';
// --> E_WARNING: inflate_add(): data error
// Invalid length:
// $compressed[strlen($compressed)-2] = 'x';
// --> E_WARNING: inflate_add(): data error
// Corrupted compressed data:
// $compressed[strlen($compressed)/2] = 'x';
// --> E_WARNING: inflate_add(): data error
// Following test FAILED to detect corrupted data:
// Trunked lenght field:
// $compressed = substr($compressed, 0, strlen($compressed)-4);
// --> Data to compress, more data, and even more data!
// NO ERROR SIGNALED
// Trunked Adler32 and lenght fields:
// $compressed = substr($compressed, 0, strlen($compressed)-8);
// --> Data to compress, more data, and even more data!
// NO ERROR SIGNALED
// Trunked some data, Adler32 and lenght fields:
$compressed = substr($compressed, 0, strlen($compressed)-12);
// --> Data to compress, more data, and eve
// TRUNKED RESULTING DATA, NO ERROR SIGNALED
echo "Corrupted : ", wordwrap(bin2hex($compressed), 2, " ", TRUE),
"\n";
$inflateContext = inflate_init(ZLIB_ENCODING_GZIP);
$uncompressed = inflate_add($inflateContext, $compressed,
ZLIB_NO_FLUSH);
$uncompressed .= inflate_add($inflateContext, NULL, ZLIB_FINISH);
echo $uncompressed;
?>
Expected result:
----------------
E_WARNING: inflate_add(): data error
Actual result:
--------------
Data to compress, more data, and eve
(note how the original plain string has been trunked, but no error is
detected).
--
Edit bug report at https://bugs.php.net/bug.php?id=71506&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=71506&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=71506&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=71506&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=71506&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=71506&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=71506&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=71506&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=71506&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=71506&r=support
Expected behavior: https://bugs.php.net/fix.php?id=71506&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=71506&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=71506&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=71506&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=71506&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=71506&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=71506&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=71506&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=71506&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=71506&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=71506&r=mysqlcfg