Commit 59ccd9885cc for php
commit 59ccd9885ccfce288a80a9e3c01baea0d6879640
Author: Lazizbek Ergashev <lazerg2@gmail.com>
Date: Fri Nov 27 20:15:24 2026 +0500
Fix GH-24701: Use-after-free on typed trait constants during opcache preload (#24710)
zend_do_traits_constant_binding() copies the trait constant into the using class but doesn't take a reference on its type, so both constants share the same class name string. During preload that string isn't interned yet, and when opcache interns it for the first constant it frees the original while the second still points to it. Now the copy goes through zend_type_copy_ctor(), same as the trait property path. A union type also shared the type list, which trips an assertion in debug builds, so the test covers both.
Co-authored-by: sanklamm <4759687+sanklamm@users.noreply.github.com>
diff --git a/NEWS b/NEWS
index 8fe84a4176d..e9c9e3b6f26 100644
--- a/NEWS
+++ b/NEWS
@@ -41,6 +41,8 @@ PHP NEWS
. Fixed bug GH-25313 (Assertion !EG(exception) in ZEND_FE_FREE when unpacking
a generator into a by-ref parameter throws). (ndossche)
. Fix OSS-Fuzz #588264417. (ndossche)
+ . Fixed bug GH-24701 (Use-after-free on typed trait constants during
+ opcache preload). (sanklamm, lazerg)
- Curl:
. Fixed curl_reset() retaining CURLOPT_PRIVATE after reset. (lacatoire)
diff --git a/Zend/zend_inheritance.c b/Zend/zend_inheritance.c
index 6d702b67a22..e97fb42ffc2 100644
--- a/Zend/zend_inheritance.c
+++ b/Zend/zend_inheritance.c
@@ -2848,6 +2848,7 @@ static void zend_do_traits_constant_binding(zend_class_entry *ce, zend_class_ent
Z_TRY_ADDREF(constant->value);
constant->doc_comment = constant->doc_comment ? zend_string_copy(constant->doc_comment) : NULL;
+ zend_type_copy_ctor(&constant->type, /* use_arena */ true, /* persistent */ false);
if (constant->attributes && (!(GC_FLAGS(constant->attributes) & IS_ARRAY_IMMUTABLE))) {
GC_ADDREF(constant->attributes);
}
diff --git a/ext/opcache/tests/gh24701.phpt b/ext/opcache/tests/gh24701.phpt
new file mode 100644
index 00000000000..c3136fb046a
--- /dev/null
+++ b/ext/opcache/tests/gh24701.phpt
@@ -0,0 +1,21 @@
+--TEST--
+GH-24701 (Use-after-free on typed trait constants during opcache preload)
+--EXTENSIONS--
+opcache
+--INI--
+opcache.enable=1
+opcache.enable_cli=1
+opcache.optimization_level=-1
+opcache.preload={PWD}/preload_gh24701.inc
+--SKIPIF--
+<?php
+if (PHP_OS_FAMILY == 'Windows') die('skip Preloading is not supported on Windows');
+?>
+--FILE--
+<?php
+var_dump(C::x());
+var_dump(C::Y);
+?>
+--EXPECT--
+enum(E::A)
+enum(F::B)
diff --git a/ext/opcache/tests/preload_gh24701.inc b/ext/opcache/tests/preload_gh24701.inc
new file mode 100644
index 00000000000..ebd9039af1f
--- /dev/null
+++ b/ext/opcache/tests/preload_gh24701.inc
@@ -0,0 +1,21 @@
+<?php
+enum E: string {
+ case A = 'a';
+}
+
+enum F: string {
+ case B = 'b';
+}
+
+trait T {
+ protected const E X = E::A;
+ public const E|F Y = F::B;
+}
+
+class C {
+ use T;
+
+ public static function x() {
+ return self::X;
+ }
+}