From 996b83f8942e3a95b8ea85c24d5ad0fdeea5c554 Mon Sep 17 00:00:00 2001 From: Evgenij Efimov Date: Tue, 24 Nov 2026 20:36:33 +0300 Subject: [PATCH] Fix OPcache file cache crash with closure in attribute on promoted property For a promoted property the attribute is compiled twice, once for the parameter and once for the property. Both zend_attribute copies point to the same constant-expression AST, and the closure argument is a ZEND_AST_OP_ARRAY node referencing the op_array of the closure. zend_file_cache_serialize_ast() did not check IS_SERIALIZED() for ZEND_AST_OP_ARRAY nodes, so the second copy serialized the op_array again. zend_file_cache_serialize_func() then treated the already serialized pointer as an unserialized one and computed a garbage address, which zend_file_cache_serialize_op_array() wrote to: a segfault in release builds, an assertion failure in debug builds. zend_file_cache_unserialize_ast() had the symmetric gap and restored the already unserialized pointer a second time, leaving a garbage pointer in the loaded script. Guard both branches with IS_SERIALIZED() / IS_UNSERIALIZED(), like the other node kinds. --- NEWS | 2 + ...e_closure_attribute_promoted_property.phpt | 104 ++++++++++++++++++ ext/opcache/zend_file_cache.c | 20 ++-- 3 files changed, 118 insertions(+), 8 deletions(-) create mode 100644 ext/opcache/tests/file_cache_closure_attribute_promoted_property.phpt diff --git a/NEWS b/NEWS index c9429f71cc0d..f605691066ae 100644 --- a/NEWS +++ b/NEWS @@ -120,6 +120,8 @@ PHP NEWS (Brian Marks) . Fixed function JIT ASSIGN_DIM reporting an undefined value or key out of order, or when the VM does not, on non-array containers. (Jorg Sowa) + . Fixed segfault when storing a script with a closure in an attribute on a + promoted property in the file cache. (Evgenij Efimov) - OpenSSL: . Fixed bug GH-20920 (LibreSSL build regression with PHP 8.5). diff --git a/ext/opcache/tests/file_cache_closure_attribute_promoted_property.phpt b/ext/opcache/tests/file_cache_closure_attribute_promoted_property.phpt new file mode 100644 index 000000000000..375397627084 --- /dev/null +++ b/ext/opcache/tests/file_cache_closure_attribute_promoted_property.phpt @@ -0,0 +1,104 @@ +--TEST-- +opcache file cache: closure in attribute argument on promoted property (attribute compiled twice) +--EXTENSIONS-- +opcache +--SKIPIF-- + +--INI-- +opcache.enable=1 +opcache.enable_cli=1 +opcache.jit=disable +opcache.file_cache="{PWD}/file_cache_closure_attribute_promoted_property/cache" +opcache.file_update_protection=0 +--FILE-- +getAttributes()[0]->getArguments()[0]; +$prop = (new ReflectionProperty(Callback::class, 'data'))->getAttributes()[0]->getArguments()[0]; +echo "param: ", $param(), "\n"; +echo "prop: ", $prop(), "\n"; +PHP; + +// Same process: opcache_compile_file() has already declared the class, evaluate the attribute +// arguments of the in-memory copy that was just serialized. +echo "in-process:\n"; +eval($check); + +// Fresh process. +$cmd = [PHP_BINARY, '-n']; +$ext = ini_get('extension_dir'); +if (file_exists($ext . DIRECTORY_SEPARATOR . 'opcache.so')) { + array_push($cmd, '-d', 'zend_extension=opcache'); +} +array_push($cmd, + '-d', 'opcache.enable_cli=1', + '-d', 'opcache.file_cache="' . $cache . '"', + '-d', 'opcache.file_cache_only=1', + '-d', 'opcache.file_update_protection=0', + '-r', 'require $argv[1]; ' . $check, + $script, +); +$proc = proc_open($cmd, [1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes); +echo "child process:\n", stream_get_contents($pipes[1]); +fclose($pipes[1]); +$stderr = stream_get_contents($pipes[2]); +fclose($pipes[2]); +echo "child exit code: ", proc_close($proc), "\n"; +if ($stderr !== '') { echo "stderr: $stderr"; } +?> +--CLEAN-- +isDir() ? rmdir($fileinfo->getPathname()) : unlink($fileinfo->getPathname()); + } + rmdir($dir); +} +removeDirRecursive(__DIR__ . '/file_cache_closure_attribute_promoted_property'); +?> +--EXPECT-- +bool(true) +bool(true) +in-process: +param: closure-from-cache +prop: closure-from-cache +child process: +param: closure-from-cache +prop: closure-from-cache +child exit code: 0 diff --git a/ext/opcache/zend_file_cache.c b/ext/opcache/zend_file_cache.c index 2f969c55f9d5..6181774eb5d4 100644 --- a/ext/opcache/zend_file_cache.c +++ b/ext/opcache/zend_file_cache.c @@ -377,10 +377,12 @@ static void zend_file_cache_serialize_ast(zend_ast *ast, } } } else if (ast->kind == ZEND_AST_OP_ARRAY) { - zval z; - ZVAL_PTR(&z, zend_ast_get_op_array(ast)->op_array); - zend_file_cache_serialize_func(&z, script, info, buf); - zend_ast_get_op_array(ast)->op_array = Z_PTR(z); + if (!IS_SERIALIZED(zend_ast_get_op_array(ast)->op_array)) { + zval z; + ZVAL_PTR(&z, zend_ast_get_op_array(ast)->op_array); + zend_file_cache_serialize_func(&z, script, info, buf); + zend_ast_get_op_array(ast)->op_array = Z_PTR(z); + } } else if (ast->kind == ZEND_AST_CALLABLE_CONVERT) { zend_ast_fcc *fcc = (zend_ast_fcc*)ast; ZEND_MAP_PTR_INIT(fcc->fptr, NULL); @@ -1282,10 +1284,12 @@ static void zend_file_cache_unserialize_ast(zend_ast *ast, } } } else if (ast->kind == ZEND_AST_OP_ARRAY) { - zval z; - ZVAL_PTR(&z, zend_ast_get_op_array(ast)->op_array); - zend_file_cache_unserialize_func(&z, script, buf); - zend_ast_get_op_array(ast)->op_array = Z_PTR(z); + if (!IS_UNSERIALIZED(zend_ast_get_op_array(ast)->op_array)) { + zval z; + ZVAL_PTR(&z, zend_ast_get_op_array(ast)->op_array); + zend_file_cache_unserialize_func(&z, script, buf); + zend_ast_get_op_array(ast)->op_array = Z_PTR(z); + } } else if (ast->kind == ZEND_AST_CALLABLE_CONVERT) { zend_ast_fcc *fcc = (zend_ast_fcc*)ast; if (!script->corrupted) {