bzip2.decompress write filter writes past its input buffer after a data error

オープン 初心者向け
#25,903 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
78/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
c
領域
backend

調査の方向性

ext/bz2/bz2_filter.cを読み、特に入力のコピーとデータエラー時の戻り値に関するphp_bz2_decompress_filterを確認してください。issueによると同じ修正が入ったzlib.inflateと、リセット時の動作を比較してください。提供された破損した.bz2の例をASanビルドで再現し、その後の書き込みで範囲外書き込みを起こさずに解凍の通知が報告されることを確認してください。

索引モデルが issue の本文から書いたものです。

説明

Bug Status: Needs Triage
Description

After a data error, a bzip2.decompress write filter can copy later writes past the end of its 2048-byte input buffer. Copying a .bz2 with a corrupted byte near the start in a loop that doesn't check what fwrite() returns is enough:

<?php
$data = '';
for ($i = 0; $i < 20000; $i++) {
    $data .= "line $i " . md5($i) . "\n";
}
$bz = bzcompress($data);
$bz[100] = 'x';

$in = fopen('php://memory', 'w+');
fwrite($in, $bz);
rewind($in);

$out = fopen('php://memory', 'w+');
stream_filter_append($out, 'bzip2.decompress', STREAM_FILTER_WRITE);
while (!feof($in)) {
    fwrite($out, fread($in, 8192));
}
echo "Done\n";

On an ASan build with USE_ZEND_ALLOC=0.

Resulted in this output:

Notice: fwrite(): bzip2 decompression failed in %s on line 16
ERROR: AddressSanitizer: heap-buffer-overflow
WRITE of size 2048
    #0 memcpy
    #1 php_bz2_decompress_filter ext/bz2/bz2_filter.c:117
    #2 _php_stream_write_filtered main/streams/streams.c:1292
    #3 _php_stream_write main/streams/streams.c:1381
    #4 zif_fwrite ext/standard/file.c:1035

But I expected this output instead:

Notice: fwrite(): bzip2 decompression failed in %s on line 16
(the same notice for each later fwrite() call)
Done

On a data error the filter returns before it resets its input position. bzlib has already moved that position forward, and the next write copies its input there. Without ASan and with the Zend allocator it doesn't crash, it silently overwrites the heap after the buffer. zlib.inflate had the same bug and got the same reset in 2010 (https://bugs.php.net/52944).

I'm opening a PR that resets the position the way zlib.inflate does.

PHP Version
PHP 8.4.29-dev (PHP-8.4). The same code is in 8.5, 8.6 and master.
Operating System

Ubuntu 26.04, libbz2 1.0.8

主要言語
C
スター
41.9k
フォーク
8.5k
平均マージ
2日 18時間
マージ済み PR(30日)
106

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

php/php-src のほかの issue

php/php-src の issue をすべて見る

似ている issue

C の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。