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

Aperta Adatta ai principianti
#25,903 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
78/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
c
Ambito
backend

Direzione di ricerca

Leggi ext/bz2/bz2_filter.c, in particolare php_bz2_decompress_filter intorno alla copia dell’input e al ritorno in caso di errore nei dati; confronta il comportamento di ripristino con zlib.inflate, che secondo l’issue ha ricevuto la stessa correzione. Riproduci il problema con l’esempio .bz2 danneggiato fornito in una build ASan, poi verifica che le scritture successive segnalino l’avviso di decompressione senza scrivere fuori dai limiti.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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

Lingua principale
C
Stelle
41.9k
Fork
8.5k
Merge medio
2g 18h
PR unite (30g)
106

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di php/php-src

Tutte le issue di php/php-src

Issue simili

Altre issue su C

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.