Skip to content

Validate PUBLISH and MODULE payload lengths against packet size - #3972

Merged
madolson merged 2 commits into
valkey-io:unstablefrom
trail-of-forks:fix/cluster-publish-overflow
Aug 11, 2026
Merged

madolson merged 2 commits into
valkey-io:unstablefrom
trail-of-forks:fix/cluster-publish-overflow

Conversation

@tjade273

Copy link
Copy Markdown
Contributor

Problem

clusterIsValidPacket() adds the packet-supplied 32-bit channel_len, message_len, and module payload lengths into the 32-bit explen with no bounds checks. A PUBLISH packet declaring channel_len = 0xffffffff and message_len = 1 wraps explen back down to the header size, so it passes the totlen == explen completeness check.

clusterProcessPublishPacket() then calls createStringObject((char *)publish_data->bulk_data, channel_len) with a ~4 GB length, reading far past the receive buffer and crashing the node.

The PING/PONG/MEET branch of the same function already validates its variable-length gossip and extension data against the remaining packet space; the PUBLISH/PUBLISHSHARD and MODULE branches did not. CVE-2026-21863 hardened the gossip/extension path but left these branches unchanged.

Fix

Compute the fixed part of explen first, then check each packet-supplied length against the remaining space (totlen - explen) before adding it, the same way the gossip and extension data are validated.

Testing

Added a regression test in tests/unit/cluster/packet.tcl that sends a forged PUBLISH with channel_len = 0xffffffff and a wrapped totlen, with a subscriber connected so the decode path runs.

I verified that it crashes the node on pre-fix code (SIGBUS in the publish handler) and passes after the fix.

clusterIsValidPacket() added the packet-supplied 32-bit channel, message,
and module payload lengths into the 32-bit explen without bounds checks. A
packet declaring e.g. channel_len 0xffffffff wraps explen back down to the
header size and passes the totlen == explen check, after which the publish
handler reads past the receive buffer and crashes the node.

Check each declared length against the remaining packet space before adding
it, the same way the gossip and extension data are already validated for
PING/PONG/MEET packets in this function. Add a regression test that sends a
forged PUBLISH with a wrapped length and confirms the node survives.

Refs trailofbits/ptp-valkey#7

Signed-off-by: Tjaden Hess <tjade273@gmail.com>
@coderabbitai

coderabbitai Bot commented Jun 11, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

This PR updates clusterIsValidPacket() to validate PUBLISH and MODULE payload lengths against remaining packet bytes. It adds a forged PUBLISH packet test that uses a wrapped channel length and checks cluster responsiveness and health.

Changes

Cluster packet validation hardening

Layer / File(s) Summary
Publish packet length validation
src/cluster_legacy.c
PUBLISH packet validation checks channel_len and message_len incrementally before calculating the expected packet length.
Module packet length validation
src/cluster_legacy.c
MODULE packet validation extracts payload length values for light and normal headers and rejects values larger than the remaining packet bytes.
Publish packet test helper and exploit scenario
tests/unit/cluster/packet.tcl
The new helper builds forged PUBLISH packets with attacker-controlled lengths. The test sends a wrapped channel_len value, waits for validator receipt, and checks node responsiveness and cluster health.

Estimated code review effort: 3 (Moderate) | ~20 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main validation changes for PUBLISH and MODULE packet payload lengths.
Description check ✅ Passed The description explains the vulnerability, the fix, and the regression test, all of which directly match the changeset.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
tests/unit/cluster/packet.tcl (1)

189-192: ⚡ Quick win

Avoid hard-coding wrapped totlen; derive it from the forged packet size.

Using literal 2264 makes this regression fragile if cluster header/layout changes. Compute it from string length $packet so the test keeps validating the same overflow class without coupling to one struct size.

Suggested tweak
-        # Set totlen to the wrapped value so it matches the computed explen.
-        set packet [string replace $packet 4 7 [binary format I 2264]]
-        assert_equal 2264 [string length $packet]
+        # Set totlen to the wrapped value so it matches the computed explen.
+        set wrapped_totlen [string length $packet]
+        set packet [string replace $packet 4 7 [binary format I $wrapped_totlen]]
+        assert_equal $wrapped_totlen [string length $packet]
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/unit/cluster/packet.tcl` around lines 189 - 192, The test currently
hardcodes totlen as 2264 in the string replace call and assertion; instead build
the forged packet with a placeholder totlen (e.g., binary format I 0), compute
the actual length with explen=[string length $packet], then replace bytes 4..7
using [binary format I $explen] and assert_equal $explen [string length
$packet]; update the string replace that writes the totlen and the subsequent
assert_equal to use the computed explen variable so the test derives totlen from
packet size (referencing the packet variable and the string replace call).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/cluster_legacy.c`:
- Around line 3794-3814: The code reads module_len from msg->data.module.msg.len
(via toClusterMsgLight/toClusterMsg) before confirming the fixed module header
bytes are within totlen, which can read past the received buffer for truncated
packets; move the ntohl(...) reads for module_len to after you validate explen
(the fixed header size computed from sizeof(clusterMsgLight)/sizeof(clusterMsg)
and sizeof(clusterMsgModule) - 3) is <= totlen so the header bytes are present,
i.e. first compute explen and verify if (totlen < explen) return error, then
safely access module_len using ntohl from the appropriate message view
(clusterMsgLight or clusterMsg), and finally check (totlen - explen) <
module_len before adding module_len to explen.

---

Nitpick comments:
In `@tests/unit/cluster/packet.tcl`:
- Around line 189-192: The test currently hardcodes totlen as 2264 in the string
replace call and assertion; instead build the forged packet with a placeholder
totlen (e.g., binary format I 0), compute the actual length with explen=[string
length $packet], then replace bytes 4..7 using [binary format I $explen] and
assert_equal $explen [string length $packet]; update the string replace that
writes the totlen and the subsequent assert_equal to use the computed explen
variable so the test derives totlen from packet size (referencing the packet
variable and the string replace call).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 9a971a48-42a2-4f58-a078-0c50e27bb985

📥 Commits

Reviewing files that changed from the base of the PR and between f3bdf50 and 99dfb96.

📒 Files selected for processing (2)
  • src/cluster_legacy.c
  • tests/unit/cluster/packet.tcl

Comment thread src/cluster_legacy.c
Comment on lines +3794 to 3814
uint32_t module_len;
if (is_light) {
clusterMsgLight *msg_light = toClusterMsgLight(link->rcvbuf);
explen = sizeof(clusterMsgLight) - sizeof(union clusterMsgData);
explen += sizeof(clusterMsgModule) - 3 + ntohl(msg_light->data.module.msg.len);
module_len = ntohl(msg_light->data.module.msg.len);
} else {
clusterMsg *msg = toClusterMsg(link->rcvbuf);
explen = sizeof(clusterMsg) - sizeof(union clusterMsgData);
explen += sizeof(clusterMsgModule) - 3 + ntohl(msg->data.module.msg.len);
module_len = ntohl(msg->data.module.msg.len);
}
explen += sizeof(clusterMsgModule) - 3;
/* The module payload length comes from the packet. Make sure it fits in
* the remaining space before adding it, so explen can't overflow. */
if (totlen < explen || (totlen - explen) < module_len) {
serverLog(LL_WARNING,
"Received invalid %s packet with module payload length that exceeds total packet length (%lld)",
clusterGetMessageTypeString(type), (unsigned long long)totlen);
return 0;
}
explen += module_len;
} else {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Defer module_len read until fixed header length is validated.

On Line 3798 and Line 3802, module_len is read before verifying the fixed module header bytes are inside totlen. For truncated packets, this reads beyond the declared packet payload boundary (from stale rcvbuf bytes). Validate fixed bytes first, then parse module_len.

💡 Suggested patch
-    } else if (type == CLUSTERMSG_TYPE_MODULE) {
-        uint32_t module_len;
+    } else if (type == CLUSTERMSG_TYPE_MODULE) {
+        uint32_t module_len;
+        clusterMsgModule *module_data;
         if (is_light) {
             clusterMsgLight *msg_light = toClusterMsgLight(link->rcvbuf);
             explen = sizeof(clusterMsgLight) - sizeof(union clusterMsgData);
-            module_len = ntohl(msg_light->data.module.msg.len);
+            module_data = &msg_light->data.module.msg;
         } else {
             clusterMsg *msg = toClusterMsg(link->rcvbuf);
             explen = sizeof(clusterMsg) - sizeof(union clusterMsgData);
-            module_len = ntohl(msg->data.module.msg.len);
+            module_data = &msg->data.module.msg;
         }
         explen += sizeof(clusterMsgModule) - 3;
+        if (totlen < explen) {
+            serverLog(LL_WARNING,
+                      "Received invalid %s packet with module header that exceeds total packet length (%lld)",
+                      clusterGetMessageTypeString(type), (unsigned long long)totlen);
+            return 0;
+        }
+        module_len = ntohl(module_data->len);
         /* The module payload length comes from the packet. Make sure it fits in
          * the remaining space before adding it, so explen can't overflow. */
-        if (totlen < explen || (totlen - explen) < module_len) {
+        if ((totlen - explen) < module_len) {
             serverLog(LL_WARNING,
                       "Received invalid %s packet with module payload length that exceeds total packet length (%lld)",
                       clusterGetMessageTypeString(type), (unsigned long long)totlen);
             return 0;
         }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
uint32_t module_len;
if (is_light) {
clusterMsgLight *msg_light = toClusterMsgLight(link->rcvbuf);
explen = sizeof(clusterMsgLight) - sizeof(union clusterMsgData);
explen += sizeof(clusterMsgModule) - 3 + ntohl(msg_light->data.module.msg.len);
module_len = ntohl(msg_light->data.module.msg.len);
} else {
clusterMsg *msg = toClusterMsg(link->rcvbuf);
explen = sizeof(clusterMsg) - sizeof(union clusterMsgData);
explen += sizeof(clusterMsgModule) - 3 + ntohl(msg->data.module.msg.len);
module_len = ntohl(msg->data.module.msg.len);
}
explen += sizeof(clusterMsgModule) - 3;
/* The module payload length comes from the packet. Make sure it fits in
* the remaining space before adding it, so explen can't overflow. */
if (totlen < explen || (totlen - explen) < module_len) {
serverLog(LL_WARNING,
"Received invalid %s packet with module payload length that exceeds total packet length (%lld)",
clusterGetMessageTypeString(type), (unsigned long long)totlen);
return 0;
}
explen += module_len;
} else {
uint32_t module_len;
clusterMsgModule *module_data;
if (is_light) {
clusterMsgLight *msg_light = toClusterMsgLight(link->rcvbuf);
explen = sizeof(clusterMsgLight) - sizeof(union clusterMsgData);
module_data = &msg_light->data.module.msg;
} else {
clusterMsg *msg = toClusterMsg(link->rcvbuf);
explen = sizeof(clusterMsg) - sizeof(union clusterMsgData);
module_data = &msg->data.module.msg;
}
explen += sizeof(clusterMsgModule) - 3;
if (totlen < explen) {
serverLog(LL_WARNING,
"Received invalid %s packet with module header that exceeds total packet length (%lld)",
clusterGetMessageTypeString(type), (unsigned long long)totlen);
return 0;
}
module_len = ntohl(module_data->len);
/* The module payload length comes from the packet. Make sure it fits in
* the remaining space before adding it, so explen can't overflow. */
if ((totlen - explen) < module_len) {
serverLog(LL_WARNING,
"Received invalid %s packet with module payload length that exceeds total packet length (%lld)",
clusterGetMessageTypeString(type), (unsigned long long)totlen);
return 0;
}
explen += module_len;
} else {
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/cluster_legacy.c` around lines 3794 - 3814, The code reads module_len
from msg->data.module.msg.len (via toClusterMsgLight/toClusterMsg) before
confirming the fixed module header bytes are within totlen, which can read past
the received buffer for truncated packets; move the ntohl(...) reads for
module_len to after you validate explen (the fixed header size computed from
sizeof(clusterMsgLight)/sizeof(clusterMsg) and sizeof(clusterMsgModule) - 3) is
<= totlen so the header bytes are present, i.e. first compute explen and verify
if (totlen < explen) return error, then safely access module_len using ntohl
from the appropriate message view (clusterMsgLight or clusterMsg), and finally
check (totlen - explen) < module_len before adding module_len to explen.

@codecov

codecov Bot commented Jun 11, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 41.17647% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 78.39%. Comparing base (8eb992e) to head (2898875).

Files with missing lines Patch % Lines
src/cluster_legacy.c 41.17% 10 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##           unstable    #3972      +/-   ##
============================================
- Coverage     78.55%   78.39%   -0.16%     
============================================
  Files           166      166              
  Lines         88272    88285      +13     
============================================
- Hits          69341    69215     -126     
- Misses        18931    19070     +139     
Files with missing lines Coverage Δ
src/cluster_legacy.c 87.93% <41.17%> (-0.30%) ⬇️

... and 25 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.