streams/userspace.c: PHP_STREAM_FLAG_NO_FCLOSE guard missing in stream_stat handler (incomplete fix for GH-25586)

Offen Anfängerfreundlich
#25,815 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
78/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
c, php
Bereich
backend, security

Rechercherichtung

Beginne in main/streams/userspace.c bei _php_stream_stat und vergleiche die Behandlung des stream_stat-Callbacks mit den vorhandenen Prüfungen für flush, write, read, seek und cast. Füge gemäß den bestehenden User-Stream-Tests einen Regressionstest für einen stream_stat()-Callback hinzu, der fclose() für seinen Stream aufruft. Als erledigt gilt die Aufgabe, wenn fclose() eine Warnung ausgibt und den Stream während des aktiven Callbacks nicht schließt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

Bug Status: Needs Triage
Description

GH-25586 (Nov 2026) added PHP_STREAM_FLAG_NO_FCLOSE guard to the flush
handler in main/streams/userspace.c to prevent use-after-free when a user
stream wrapper's stream_flush() callback calls fclose() on the stream.
The same guard is absent from the stream_stat handler (_php_stream_stat,
line ~865).

The following code:

<?php
class UAFStream {
    public static $res;

    public function stream_open($path, $mode, $opts, &$opened): bool {
        return true;
    }
    public function stream_stat(): array|false {
        // Without PHP_STREAM_FLAG_NO_FCLOSE, fclose() succeeds here,
        // freeing the stream while C code may still hold a pointer to it.
        fclose(self::$res);
        return false;
    }
    public function stream_read(int $count): string { return ''; }
    public function stream_eof(): bool { return true; }
}

stream_wrapper_register('x', 'UAFStream');
$fp = fopen('x://test', 'r');
UAFStream::$res = $fp;
fstat($fp);  // triggers stream_stat → fclose() completes with no warning
echo "done\n";

Resulted in this output:

done

No warning is emitted and fclose() completes successfully inside the callback,
freeing the stream resource while fstat() / _php_stream_stat() may still
hold pointers into it.

But I expected this output instead:

Warning: Cannot close stream with active callback in ...
done

Consistent with the guard added for flush in GH-25586: fclose() should be
a no-op (with a warning) when called from within a stream callback.

Suggested fix (main/streams/userspace.c, _php_stream_stat):

bool originally_no_fclose = stream->flags & PHP_STREAM_FLAG_NO_FCLOSE;
stream->flags |= PHP_STREAM_FLAG_NO_FCLOSE;
call_result = zend_call_method_with_0_params(...); /* stream_stat */
if (!originally_no_fclose) {
    stream->flags ^= PHP_STREAM_FLAG_NO_FCLOSE;
}

This is the identical pattern used for flush (GH-25586), write, read,
seek, and cast.

PHP Version
PHP 8.6.0 (cli) (built: Nov 19 2026 15:17:27) (ZTS)
Operating System

Ubuntu 26.04

Vorherrschende Sprache
C
Sterne
41.9k
Forks
8.5k
Ø Merge
2 T. 18 Std.
Gemergte PRs (30 T.)
106

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus php/php-src

Alle Issues in php/php-src

Ähnliche Issues

Weitere Issues zu C

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.