Commit 72aa19ab0b7 for php
commit 72aa19ab0b766972c8452ad4377e624c8799f373
Author: Sebastian Bergmann <sb@sebastian-bergmann.de>
Date: Fri Dec 4 17:51:16 2026 +0800
ext/sysvmsg: Terminate the message that msg_receive() unserializes (#26044)
msg_receive() passes received bytes to the unserializer. For incomplete
serialized data, the unserializer may inspect the byte after the message
and read uninitialized memory, potentially accepting a value that was never
sent.
Terminate the message in the existing extra byte of php_msgbuf before
unserializing it, and add a regression test and NEWS entry.
Closes #26044
diff --git a/NEWS b/NEWS
index bec4e23c2c1..1b3b63932f9 100644
--- a/NEWS
+++ b/NEWS
@@ -23,6 +23,10 @@ PHP NEWS
. Fixed array_udiff_assoc() and array_uintersect_assoc() clobbering the user
comparison callback. (lacatoire)
+- Sysvmsg:
+ . Fixed msg_receive() reading past the end of the received message when
+ unserializing it. (Sebastian Bergmann)
+
- Date:
. Fixed bug GH-26032 (DateTimeZone->getTransitions() includes too-early
results on 32-bit platforms, which are then garbled). (Derick)
diff --git a/ext/sysvmsg/sysvmsg.c b/ext/sysvmsg/sysvmsg.c
index cd6a13dfea3..3e651d480a4 100644
--- a/ext/sysvmsg/sysvmsg.c
+++ b/ext/sysvmsg/sysvmsg.c
@@ -308,6 +308,10 @@ PHP_FUNCTION(msg_receive)
if (result >= 0) {
/* got it! */
+ /* The unserializer may read the byte that follows the message.
+ * NB: php_msgbuf is 1 char bigger than a long, so the buffer has room
+ * for it. */
+ messagebuffer->mtext[result] = '\0';
ZEND_TRY_ASSIGN_REF_LONG(out_msgtype, messagebuffer->mtype);
if (zerrcode) {
ZEND_TRY_ASSIGN_REF_LONG(zerrcode, 0);
diff --git a/ext/sysvmsg/tests/msg_receive_truncated_unserialize.phpt b/ext/sysvmsg/tests/msg_receive_truncated_unserialize.phpt
new file mode 100644
index 00000000000..42c1056f8ac
--- /dev/null
+++ b/ext/sysvmsg/tests/msg_receive_truncated_unserialize.phpt
@@ -0,0 +1,29 @@
+--TEST--
+msg_receive() does not read past the end of a message when unserializing
+--EXTENSIONS--
+sysvmsg
+--FILE--
+<?php
+
+$queue = msg_get_queue(ftok(__FILE__, 't'), 0600);
+
+/* The unserializer looks at the byte after "i:1" to find out whether the integer ends there. */
+var_dump(msg_send($queue, 1, 'i:1', false));
+
+/* Leaves "2345;" behind in the memory that msg_receive() then uses for its buffer. */
+var_dump(msg_send($queue, 2, 'xxx2345;', false));
+
+var_dump(msg_receive($queue, 1, $type, 3, $message, true, MSG_IPC_NOWAIT));
+var_dump($message);
+
+var_dump(msg_remove_queue($queue));
+
+?>
+--EXPECTF--
+bool(true)
+bool(true)
+
+Warning: msg_receive(): Message corrupted in %s on line %d
+bool(false)
+bool(false)
+bool(true)