Commit f9db46f49cf for php

commit f9db46f49cf6181ec0212d1faa52351ed7b886a1
Author: hohphu8 <kodoku169@gmail.com>
Date:   Sun Nov 29 00:19:08 2026 +0700

    ext/sysvshm, ext/sysvmsg: Unwrap top-level reference after unserialize

    A self-referential object stored with shm_put_var() or msg_send()
    unserializes into a top-level reference. shm_get_var() returned that
    reference, tripping the debug assertion on internal function return
    values, and msg_receive() assigned it to the by-ref out parameter as a
    nested reference. Unwrap it the way unserialize() does.

    Closes GH-25879

diff --git a/NEWS b/NEWS
index 965ad4d166f..642e623f996 100644
--- a/NEWS
+++ b/NEWS
@@ -340,6 +340,14 @@ PHP                                                                        NEWS
   . Fixed bug GH-25862 (Heap buffer overflow in _php_stream_fill_read_buffer()
     when a read filter increases the stream chunk size). (matyapiro31, lazerg)

+- Sysvmsg:
+  . Fixed msg_receive() not unwrapping top-level references from unserialized
+    values. (Ho Hoang Phu, Ilia Alshanetsky)
+
+- Sysvshm:
+  . Fixed shm_get_var() not unwrapping top-level references from unserialized
+    values. (Ho Hoang Phu)
+
 - XML:
   . Fixed xml_error_string() returning the wrong message for libxml error
     codes 1 to 4. (lacatoire)
diff --git a/ext/sysvmsg/sysvmsg.c b/ext/sysvmsg/sysvmsg.c
index 1f4cafd2bce..cd6a13dfea3 100644
--- a/ext/sysvmsg/sysvmsg.c
+++ b/ext/sysvmsg/sysvmsg.c
@@ -319,15 +319,21 @@ PHP_FUNCTION(msg_receive)
 			zval tmp;
 			const unsigned char *p = (const unsigned char *) messagebuffer->mtext;

+			ZVAL_UNDEF(&tmp);
 			PHP_VAR_UNSERIALIZE_INIT(var_hash);
-			if (!php_var_unserialize(&tmp, &p, p + result, &var_hash)) {
+			int res = php_var_unserialize(&tmp, &p, p + result, &var_hash);
+			PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
+			if (!res) {
+				zval_ptr_dtor(&tmp);
 				php_error_docref(NULL, E_WARNING, "Message corrupted");
 				ZEND_TRY_ASSIGN_REF_FALSE(out_message);
 				RETVAL_FALSE;
 			} else {
+				if (Z_ISREF(tmp)) {
+					zend_unwrap_reference(&tmp);
+				}
 				ZEND_TRY_ASSIGN_REF_TMP(out_message, &tmp);
 			}
-			PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
 		} else {
 			ZEND_TRY_ASSIGN_REF_STRINGL(out_message, messagebuffer->mtext, result);
 		}
diff --git a/ext/sysvmsg/tests/msg_receive_unwrap_reference.phpt b/ext/sysvmsg/tests/msg_receive_unwrap_reference.phpt
new file mode 100644
index 00000000000..f4dfbc0be68
--- /dev/null
+++ b/ext/sysvmsg/tests/msg_receive_unwrap_reference.phpt
@@ -0,0 +1,35 @@
+--TEST--
+msg_receive() unwraps top-level reference from unserialized self-referential object
+--EXTENSIONS--
+sysvmsg
+--SKIPIF--
+<?php
+if (!function_exists('ftok')) die('skip needs ftok');
+?>
+--FILE--
+<?php
+
+$key = ftok(__FILE__, 'r');
+$q = msg_get_queue($key, 0600);
+try {
+	$obj = new stdClass();
+	$obj->self = &$obj;
+
+	var_dump(msg_send($q, 1, $obj));
+
+	$msgtype = 0;
+	$out = null;
+	var_dump(msg_receive($q, 0, $msgtype, 4096, $out));
+	var_dump($out instanceof stdClass);
+	var_dump($out->self === $out);
+} finally {
+	msg_remove_queue($q);
+}
+echo "Done\n";
+?>
+--EXPECT--
+bool(true)
+bool(true)
+bool(true)
+bool(true)
+Done
diff --git a/ext/sysvshm/sysvshm.c b/ext/sysvshm/sysvshm.c
index 9c68c09147d..9250db476c4 100644
--- a/ext/sysvshm/sysvshm.c
+++ b/ext/sysvshm/sysvshm.c
@@ -341,6 +341,10 @@ PHP_FUNCTION(shm_get_var)
 		zval_ptr_dtor(return_value);
 		RETURN_FALSE;
 	}
+
+	if (Z_ISREF_P(return_value)) {
+		zend_unwrap_reference(return_value);
+	}
 }
 /* }}} */

diff --git a/ext/sysvshm/tests/shm_get_var_unwrap_reference.phpt b/ext/sysvshm/tests/shm_get_var_unwrap_reference.phpt
new file mode 100644
index 00000000000..7810a634996
--- /dev/null
+++ b/ext/sysvshm/tests/shm_get_var_unwrap_reference.phpt
@@ -0,0 +1,29 @@
+--TEST--
+shm_get_var() unwraps top-level reference from unserialized self-referential object
+--EXTENSIONS--
+sysvshm
+--SKIPIF--
+<?php
+if (!function_exists('ftok')) die('skip needs ftok');
+?>
+--FILE--
+<?php
+
+$key = ftok(__FILE__, 'r');
+$shm = shm_attach($key, 4096);
+
+$obj = new stdClass();
+$obj->self = &$obj;
+shm_put_var($shm, 1, $obj);
+
+$out = shm_get_var($shm, 1);
+var_dump($out instanceof stdClass);
+var_dump($out->self === $out);
+
+shm_remove($shm);
+echo "Done\n";
+?>
+--EXPECT--
+bool(true)
+bool(true)
+Done