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

未关闭 适合新手
#25,903 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
78/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
c
领域
backend

调研方向

阅读 ext/bz2/bz2_filter.c,尤其是 php_bz2_decompress_filter 中输入复制和数据错误返回的部分;将其重置行为与 zlib.inflate 进行比较,issue 称后者也收到了相同的修复。在 ASan 构建中使用提供的损坏 .bz2 示例重现问题,然后验证后续写入会报告解压通知,且不会发生越界写入。

由索引模型根据 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 小时
30 天内合并 PR
106

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

php/php-src 的其他 Issue

查看 php/php-src 的全部 Issue

相似的 Issue

更多 C Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。