Skip to content

Fix fd leak in compress.zlib:// when gzdopen() fails - #25794

Open
lacatoire wants to merge 1 commit into
php:PHP-8.4from
lacatoire:fix/zlib-gzdopen-fd-leak-PHP-8.4
Open

lacatoire wants to merge 1 commit into
php:PHP-8.4from
lacatoire:fix/zlib-gzdopen-fd-leak-PHP-8.4

Conversation

@lacatoire

Copy link
Copy Markdown
Member

php_stream_gzopen() hands dup(fd) to gzdopen(). When gzdopen() rejects the mode (touch(), gzopen($f, 'c'), fopen(..., 'x') on compress.zlib://), the failure path closes the inner stream but never the duplicated descriptor, so each failed open leaks one fd. This closes it.

To reproduce

<?php
$n = fn() => count(scandir('/proc/self/fd'));
$before = $n();
for ($i = 0; $i < 50; $i++) @touch('compress.zlib:///tmp/zx');
echo $before, ' -> ', $n(), "\n";

Before (PHP 8.4.28):

7 -> 57

After:

7 -> 7

php_stream_gzopen() passes dup(fd) to gzdopen(). When gzdopen() rejects
the mode, the duplicated descriptor was never closed.

@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.

Oh my 🤦‍
/cc @ndossche

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