bzip2.decompress write filter writes past its input buffer after a data error
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
研究方向
閱讀 ext/bz2/bz2_filter.c,尤其是 php_bz2_decompress_filter 中輸入複製和資料錯誤返回的部分;將其重置行為與 zlib.inflate 進行比較,issue 稱後者也收到了相同的修正。在 ASan 組建中使用提供的損毀 .bz2 範例重現問題,然後確認後續寫入會回報解壓縮通知,且不會發生越界寫入。
由索引模型根據 Issue 內容生成。
描述
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 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
php/php-src 的其他 Issue
-
Bug Status: Needs Triage
難度 2/5 1-3 小時 新手友好度 74/100
維護者通常 1 天內回覆
-
Bug Status: Needs Triage
難度 2/5 1-3 小時 新手友好度 79/100
維護者通常 1 天內回覆
-
Bug Status: Needs Triage
難度 2/5 1-3 小時 新手友好度 78/100
維護者通常 1 天內回覆
-
Bug Status: Needs Triage
難度 2/5 1-3 小時 新手友好度 62/100
維護者通常 1 天內回覆
-
ext/openssl: 8.2.31+/8.3.31+ fail to build against OpenSSL 1.0.2 (ASN1_STRING_get0_data in xp_ssl.c)未關閉Extension: openssl Status: Needs Triage
難度 2/5 1-3 小時 新手友好度 79/100
維護者通常 1 天內回覆
相似的 Issue
-
documentation
難度 2/5 1-3 小時 新手友好度 84/100
Life-Framework/Life-Framework#5378 ·
維護者通常 1 天內回覆
-
bug status: planned ui
難度 2/5 1-3 小時 新手友好度 70/100
ChupathingyCE/chupathingyce#2126 ·
維護者通常 1 天內回覆
-
good first issue simulation
難度 1/5 1-3 小時 新手友好度 92/100
MarcosAsh/Lattice_Fluid_Dynamics#270 ·
維護者通常 1 天內回覆
-
needs-kind
難度 1/5 1 小時以內 新手友好度 82/100
falcosecurity/libs#3173 · 1 則留言 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 78/100
vincentarelbundock/xtx#11 ·