Skip to content

ext/bz2: Fix NULL dereference when compress.bzip2:// open fails in write mode - #25674

Closed
Schwarzemann wants to merge 1 commit into
php:PHP-8.4from
Schwarzemann:bz2-opened-path-fix
Closed

Schwarzemann wants to merge 1 commit into
php:PHP-8.4from
Schwarzemann:bz2-opened-path-fix

Conversation

@Schwarzemann

Copy link
Copy Markdown
Contributor

When BZ2_bzopen() fails, _php_stream_bz2open() falls back to
php_stream_open_wrapper(). If that also fails, *opened_path is NULL,
but the write-mode cleanup still calls VCWD_UNLINK(ZSTR_VAL(*opened_path)).

Reproducer:

dba_open("compress.bzip2:///nonexistent-dir/x", "nd", "flatfile");
  • ZTS: segfault in virtual_unlink().
  • NTS: unlink(0x18) fails with EFAULT, no crash.

Fix: also check *opened_path before unlinking.

Test: ext/bz2/tests/dba_opened_path_null.phpt. It needs bz2 and dba,
and crashes without the fix on ZTS builds.

…ite mode

_php_stream_bz2open() first tries BZ2_bzopen() and, if that fails,
falls back to php_stream_open_wrapper(). When the fallback also fails,
*opened_path is NULL, but the write-mode cleanup still calls
VCWD_UNLINK(ZSTR_VAL(*opened_path)).

On ZTS builds VCWD_UNLINK goes through virtual_unlink(), which reads
the path and segfaults. On NTS builds it is a plain unlink() syscall
that receives a bogus pointer (0x18) and fails with EFAULT, so it
does not crash but is still wrong.

Check *opened_path before unlinking. The cleanup still runs when the
fallback open succeeds but bz2 cannot take over the descriptor.

Reproducer:

    dba_open("compress.bzip2:///nonexistent-dir/x", "nd", "flatfile");

@LamentXU123 LamentXU123 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

@iliaal iliaal closed this in 5dd84e7 Nov 27, 2026
LamentXU123 added a commit that referenced this pull request Nov 27, 2026
* PHP-8.6:
  [skip ci] Add NEWS entry for #25674
arnaud-lb pushed a commit to pavimus/php-src that referenced this pull request Nov 27, 2026
arnaud-lb added a commit to pavimus/php-src that referenced this pull request Nov 27, 2026
* PHP-8.4: (91 commits)
  [skip ci] Add NEWS entry for php#25674
  ext/bz2: Fix NULL dereference when compress.bzip2:// open fails in write mode
  ext/dom: Reject DTD declaration nodes in adoptNode()
  Fix iconv_mime_encode() overflowing on a negative line-length (php#25485)
  ext/pgsql: pg_get_notify() result is never === false with opcache enabled
  ext/zlib: fix gzwrite() not reporting write errors
  Fix phpGH-25779: mysqli_fetch_object() crashes with JIT when the constructor keeps a reference and the result is discarded (php#25784)
  Fix stale string metadata in sodium_add() and sodium_increment() (php#25775)
  Sync `session.upload_progress.enabled` default in `php.ini-{development,production}` (php#25743)
  ext/pgsql: fix pg_fetch_object() JIT crash when constructor keeps $this
  Fix hash() and hash_file() hanging on invalid xxh3/xxh128 options (php#25777)
  ext/dom: fix Dom\Notation publicId and systemId property handlers
  Fix FFI::string() reading out of bounds for a negative size
  [ci skip] Update test title
  Fix phpGH-25558: FFI: struct with an enum field passed by value is described to libffi with a void element
  Fix OSS-Fuzz #588264417
  Fix FPM log overflow when errno text exceeds the buffer (phpGH-25648)
  [skip ci] Use USE_TRACKED_ALLOC=1 for Symfony Preloading test
  [skip ci] Fix Symfony test without USE_TRACKED_ALLOC
  [skip ci] Skip assign_dim_undef_op_data_008.phpt when JIT is unavailable
  ...
iliaal pushed a commit to iliaal/php-src that referenced this pull request Nov 27, 2026
* PHP-8.4:
  [skip ci] Add NEWS entry for php#25674
iliaal pushed a commit to iliaal/php-src that referenced this pull request Nov 27, 2026
* PHP-8.5:
  [skip ci] Add NEWS entry for php#25674
arnaud-lb added a commit to lazerg/php-src that referenced this pull request Nov 27, 2026
* PHP-8.4: (262 commits)
  ext/pdo_sqlite: crash when a collation callback closes the statement cursor
  Admit NTS requests to OPcache SHM only in RINIT (php#25509)
  ext/pgsql: pg_meta_data() numeric column names not integer keys with opcache
  [skip ci] Add NEWS entry for php#25674
  ext/bz2: Fix NULL dereference when compress.bzip2:// open fails in write mode
  ext/dom: Reject DTD declaration nodes in adoptNode()
  Fix iconv_mime_encode() overflowing on a negative line-length (php#25485)
  ext/pgsql: pg_get_notify() result is never === false with opcache enabled
  ext/zlib: fix gzwrite() not reporting write errors
  Fix phpGH-25779: mysqli_fetch_object() crashes with JIT when the constructor keeps a reference and the result is discarded (php#25784)
  Fix stale string metadata in sodium_add() and sodium_increment() (php#25775)
  Sync `session.upload_progress.enabled` default in `php.ini-{development,production}` (php#25743)
  ext/pgsql: fix pg_fetch_object() JIT crash when constructor keeps $this
  Fix hash() and hash_file() hanging on invalid xxh3/xxh128 options (php#25777)
  ext/dom: fix Dom\Notation publicId and systemId property handlers
  Fix FFI::string() reading out of bounds for a negative size
  [ci skip] Update test title
  Fix phpGH-25558: FFI: struct with an enum field passed by value is described to libffi with a void element
  Fix OSS-Fuzz #588264417
  Fix FPM log overflow when errno text exceeds the buffer (phpGH-25648)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants