|
| 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 ?string $other_key = null; |
| 9 | + public static bool $on_closing_only = false; |
| 10 | + |
| 11 | + public function filter($in, $out, &$consumed, $closing): int |
| 12 | + { |
| 13 | + while ($bucket = stream_bucket_make_writeable($in)) { |
| 14 | + $consumed += $bucket->datalen; |
| 15 | + stream_bucket_append($out, $bucket); |
| 16 | + } |
| 17 | + if (self::$key !== null && (!self::$on_closing_only || $closing)) { |
| 18 | + $res = $GLOBALS[self::$key]; |
| 19 | + $other = self::$other_key !== null ? $GLOBALS[self::$other_key] : null; |
| 20 | + self::$key = null; |
| 21 | + self::$other_key = null; |
| 22 | + var_dump(stream_filter_remove($res)); |
| 23 | + if ($other) { |
| 24 | + var_dump(stream_filter_remove($other)); |
| 25 | + } |
| 26 | + } |
| 27 | + return PSFS_PASS_ON; |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +stream_filter_register('self-removing', SelfRemovingFilter::class); |
| 32 | + |
| 33 | +echo "write side:\n"; |
| 34 | +$f = fopen('php://memory', 'r+'); |
| 35 | +SelfRemovingFilter::$key = 'write_res'; |
| 36 | +SelfRemovingFilter::$on_closing_only = false; |
| 37 | +$GLOBALS['write_res'] = stream_filter_append($f, 'self-removing', STREAM_FILTER_WRITE); |
| 38 | +fwrite($f, 'hello'); |
| 39 | +fwrite($f, ' world'); |
| 40 | +rewind($f); |
| 41 | +echo stream_get_contents($f), "\n"; |
| 42 | + |
| 43 | +echo "cross-filter side:\n"; |
| 44 | +$f = fopen('php://memory', 'r+'); |
| 45 | +SelfRemovingFilter::$key = 'cross_res'; |
| 46 | +SelfRemovingFilter::$other_key = 'cross_other'; |
| 47 | +SelfRemovingFilter::$on_closing_only = false; |
| 48 | +$GLOBALS['cross_res'] = stream_filter_append($f, 'self-removing', STREAM_FILTER_WRITE); |
| 49 | +$GLOBALS['cross_other'] = stream_filter_append($f, 'string.rot13', STREAM_FILTER_WRITE); |
| 50 | +fwrite($f, 'hello world'); |
| 51 | +rewind($f); |
| 52 | +echo stream_get_contents($f), "\n"; |
| 53 | + |
| 54 | +echo "read side:\n"; |
| 55 | +$f = fopen('php://memory', 'r+'); |
| 56 | +fwrite($f, 'abcdefghij'); |
| 57 | +rewind($f); |
| 58 | +SelfRemovingFilter::$key = 'read_res'; |
| 59 | +SelfRemovingFilter::$on_closing_only = false; |
| 60 | +$GLOBALS['read_res'] = stream_filter_append($f, 'self-removing', STREAM_FILTER_READ); |
| 61 | +echo fread($f, 4), '|', fread($f, 6), "\n"; |
| 62 | + |
| 63 | +echo "closing-flush side:\n"; |
| 64 | +$f = fopen('php://memory', 'r+'); |
| 65 | +SelfRemovingFilter::$key = 'close_res'; |
| 66 | +SelfRemovingFilter::$on_closing_only = true; |
| 67 | +$GLOBALS['close_res'] = stream_filter_append($f, 'self-removing', STREAM_FILTER_WRITE); |
| 68 | +var_dump(stream_filter_remove($GLOBALS['close_res'])); |
| 69 | +?> |
| 70 | +--EXPECTF-- |
| 71 | +write side: |
| 72 | + |
| 73 | +Warning: stream_filter_remove(): Cannot remove filter while it is being applied in %s on line %d |
| 74 | +bool(false) |
| 75 | +hello world |
| 76 | +cross-filter side: |
| 77 | + |
| 78 | +Warning: stream_filter_remove(): Cannot remove filter while it is being applied in %s on line %d |
| 79 | +bool(false) |
| 80 | + |
| 81 | +Warning: stream_filter_remove(): Cannot remove filter while it is being applied in %s on line %d |
| 82 | +bool(false) |
| 83 | +uryyb jbeyq |
| 84 | +read side: |
| 85 | + |
| 86 | +Warning: stream_filter_remove(): Cannot remove filter while it is being applied in %s on line %d |
| 87 | +bool(false) |
| 88 | +abcd|efghij |
| 89 | +closing-flush side: |
| 90 | + |
| 91 | +Warning: stream_filter_remove(): Cannot remove filter while it is being applied in %s on line %d |
| 92 | +bool(false) |
| 93 | +bool(true) |
0 commit comments