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

Đang mở Phù hợp với người mới
#25,815 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
78/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
c, php
Lĩnh vực
backend, security

Hướng nghiên cứu

Bắt đầu tại main/streams/userspace.c, ở _php_stream_stat, rồi so sánh cách xử lý callback stream_stat với các kiểm tra hiện có cho flush, write, read, seek và cast. Thêm một kiểm thử hồi quy cho callback stream_stat() gọi fclose() trên stream của nó, theo các kiểm thử stream người dùng hiện có. Hoàn thành khi fclose() phát cảnh báo và không đóng stream trong lúc callback đang hoạt động.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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

Ngôn ngữ chính
C
Star
41.9k
Fork
8.5k
Merge trung bình
2 ngày 18 giờ
Pull request đã merge (30 ngày)
106

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của php/php-src

Tất cả issue của php/php-src

Issue tương tự

Thêm issue về C

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.