[php-src] PR #26043: ext/fileinfo: Prevent closing stream during mime_content_type()
| From: | hohphu8 | Date: | Thu, 03 Dec 2026 04:25:14 +0000 |
| Subject: | [php-src] PR #26043: ext/fileinfo: Prevent closing stream during mime_content_type() | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-41384@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/26043
Author: hohphu8
In
_php_finfo_get_type() (ext/fileinfo/fileinfo.c),
FILEINFO_MODE_STREAM calls php_stream_tell(),
php_stream_seek(), magic_stream(), and a second
php_stream_seek() on the caller's php_stream * without setting
PHP_STREAM_FLAG_NO_FCLOSE. If a warning during seeking or reading (for example,
Stream does not support seeking on a non-seekable stream) invokes a user error handler
that calls fclose() on the stream resource, the stream is freed while
mime_content_type() still holds and dereferences stream, causing a heap
use-after-free.
Set PHP_STREAM_FLAG_NO_FCLOSE on stream->flags for the duration of the
FILEINFO_MODE_STREAM operations and restore the original flag bit afterward, matching
the pattern used in ext/openssl/xp_ssl.c and ext/standard/user_filters.c.
This keeps the stream alive across the full
tell/seek/magic_stream/seek sequence while
allowing fclose() again once mime_content_type() returns.
Added a cross-platform regression test
(ext/fileinfo/tests/mime_content_type_stream_fclose.phpt) using a non-seekable user
stream whose error handler attempts fclose(). Verified it crashes with AddressSanitizer
heap-use-after-free before the fix and passes cleanly
(ext/fileinfo/tests/, 60/60) on PHP-8.4 (Debug + ZTS + ASan).