|
| 1 | +--TEST-- |
| 2 | +GH-22063 (Stream filter chain UAF via self-removal during callback) |
| 3 | +--FILE-- |
| 4 | +<?php |
| 5 | +class SelfRemovingFilter extends php_user_filter { |
| 6 | + public $stream; |
| 7 | + public static ?string $key = null; |
| 8 | + public static bool $on_closing_only = false; |
| 9 | + |
| 10 | + public function filter($in, $out, &$consumed, $closing): int |
| 11 | + { |
| 12 | + while ($bucket = stream_bucket_make_writeable($in)) { |
| 13 | + $consumed += $bucket->datalen; |
| 14 | + stream_bucket_append($out, $bucket); |
| 15 | + } |
| 16 | + if (self::$key !== null && (!self::$on_closing_only || $closing)) { |
| 17 | + $res = $GLOBALS[self::$key]; |
| 18 | + self::$key = null; |
| 19 | + stream_filter_remove($res); |
| 20 | + } |
| 21 | + return PSFS_PASS_ON; |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +stream_filter_register('self-removing', SelfRemovingFilter::class); |
| 26 | + |
| 27 | +echo "write side: "; |
| 28 | +$f = fopen('php://memory', 'r+'); |
| 29 | +SelfRemovingFilter::$key = 'write_res'; |
| 30 | +SelfRemovingFilter::$on_closing_only = false; |
| 31 | +$GLOBALS['write_res'] = stream_filter_append($f, 'self-removing', STREAM_FILTER_WRITE); |
| 32 | +fwrite($f, 'hello'); |
| 33 | +fwrite($f, ' world'); |
| 34 | +rewind($f); |
| 35 | +echo stream_get_contents($f), "\n"; |
| 36 | + |
| 37 | +echo "read side: "; |
| 38 | +$f = fopen('php://memory', 'r+'); |
| 39 | +fwrite($f, 'abcdefghij'); |
| 40 | +rewind($f); |
| 41 | +SelfRemovingFilter::$key = 'read_res'; |
| 42 | +SelfRemovingFilter::$on_closing_only = false; |
| 43 | +$GLOBALS['read_res'] = stream_filter_append($f, 'self-removing', STREAM_FILTER_READ); |
| 44 | +echo fread($f, 4), '|', fread($f, 6), "\n"; |
| 45 | + |
| 46 | +echo "closing-flush side: "; |
| 47 | +$f = fopen('php://memory', 'r+'); |
| 48 | +SelfRemovingFilter::$key = 'close_res'; |
| 49 | +SelfRemovingFilter::$on_closing_only = true; |
| 50 | +$GLOBALS['close_res'] = stream_filter_append($f, 'self-removing', STREAM_FILTER_WRITE); |
| 51 | +stream_filter_remove($GLOBALS['close_res']); |
| 52 | +echo "ok\n"; |
| 53 | +?> |
| 54 | +--EXPECT-- |
| 55 | +write side: hello world |
| 56 | +read side: abcd|efghij |
| 57 | +closing-flush side: ok |
0 commit comments