streams/userspace.c: PHP_STREAM_FLAG_NO_FCLOSE guard missing in stream_stat handler (incomplete fix for GH-25586)
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
研究方向
從 main/streams/userspace.c 中的 _php_stream_stat 開始,並將 stream_stat 回呼的處理方式與現有的 flush、write、read、seek 和 cast 防護邏輯進行比較。參照現有的使用者串流測試,為在自身串流上呼叫 fclose() 的 stream_stat() 回呼新增回歸測試。完成標準是:作用中的回呼執行期間,fclose() 會發出警告且不會關閉該串流。
由索引模型根據 Issue 內容生成。
描述
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
- 主要語言
- C
- 星號
- 41.9k
- 分支
- 8.5k
- 平均合併
- 2 天 18 小時
- 30 天內合併 PR
- 106
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 沒有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
php/php-src 的其他 Issue
-
Bug Status: Needs Triage
難度 2/5 1-3 小時 新手友好度 78/100
維護者通常 1 天內回覆
-
Bug Status: Needs Triage
難度 2/5 1-3 小時 新手友好度 74/100
維護者通常 1 天內回覆
-
Bug Status: Needs Triage
難度 2/5 1-3 小時 新手友好度 79/100
維護者通常 1 天內回覆
-
Bug Status: Needs Triage
難度 2/5 1-3 小時 新手友好度 62/100
維護者通常 1 天內回覆
-
ext/openssl: 8.2.31+/8.3.31+ fail to build against OpenSSL 1.0.2 (ASN1_STRING_get0_data in xp_ssl.c)未關閉Extension: openssl Status: Needs Triage
難度 2/5 1-3 小時 新手友好度 79/100
維護者通常 1 天內回覆
相似的 Issue
-
documentation
難度 2/5 1-3 小時 新手友好度 84/100
Life-Framework/Life-Framework#5378 ·
維護者通常 1 天內回覆
-
bug status: planned ui
難度 2/5 1-3 小時 新手友好度 70/100
ChupathingyCE/chupathingyce#2126 ·
維護者通常 1 天內回覆
-
good first issue simulation
難度 1/5 1-3 小時 新手友好度 92/100
MarcosAsh/Lattice_Fluid_Dynamics#270 ·
維護者通常 1 天內回覆
-
needs-kind
難度 1/5 1 小時以內 新手友好度 82/100
falcosecurity/libs#3173 · 1 則留言 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 78/100
vincentarelbundock/xtx#11 ·