Skip to content

Fix integer overflow in Cluster Bus PUBLISH and MODULE message length validation - #987

Open
KIMDONGYEON00 wants to merge 1 commit into
Snapchat:mainfrom
KIMDONGYEON00:fix/cluster-message-length
Open

KIMDONGYEON00 wants to merge 1 commit into
Snapchat:mainfrom
KIMDONGYEON00:fix/cluster-message-length

Conversation

@KIMDONGYEON00

Copy link
Copy Markdown

Summary

This PR fixes integer overflows in the length validation of Cluster Bus PUBLISH and MODULE messages.

In clusterProcessPacket(), the expected packet length (explen) was calculated using a 32-bit unsigned integer. Since message length fields are supplied through the Cluster Bus protocol, specially crafted values could cause the calculation to wrap around and bypass the packet length validation.

For PUBLISH messages, this could lead to a heap-buffer-overflow when KeyDB processes the message payload.

The issue was reproduced on the original KeyDB build using AddressSanitizer (ASan), and the fix was verified using the same malformed input.

Root Cause

The original implementation calculated the expected packet length using uint32_t:

uint32_t explen = sizeof(clusterMsg) - sizeof(union clusterMsgData);

explen += sizeof(clusterMsgDataPublish) - 8
+ ntohl(hdr->data.publish.msg.channel_len)
+ ntohl(hdr->data.publish.msg.message_len);

if (totlen != explen) return 1;

If the sum of the length fields exceeded UINT32_MAX, the result wrapped around to a smaller value.

As a result, a malformed packet could pass the totlen validation even though its declared payload lengths were much larger than the received data.

The subsequent PUBLISH processing path could then attempt to read beyond the allocated receive buffer.

A similar integer overflow was possible in the MODULE message length calculation.

Changes

  • Use uint64_t for expected message length calculations in PUBLISH and MODULE packet validation.

  • Reject messages when the calculated length exceeds UINT32_MAX.

  • Log a warning when a message is rejected due to length overflow.

  • Preserve the existing packet length validation for valid messages.

  • Prevent malformed length fields from reaching subsequent message processing.

Validation

The fix was tested using an AddressSanitizer-enabled KeyDB build in an isolated Docker environment.

Test environment

Item Details
Project KeyDB
Original commit 603ebb2
Operating system Linux x86_64 (Docker)
Sanitizers AddressSanitizer, UndefinedBehaviorSanitizer
Cluster mode Enabled
Affected function clusterProcessPacket()
Affected file src/cluster.cpp

The results confirm that the fix prevents the reproduced PUBLISH heap-buffer-overflow and rejects malformed length fields in both message types.

Security Impact

The vulnerability affects Cluster Bus message processing when malformed length fields cause integer overflow during packet validation.

For PUBLISH messages, the overflow can allow an inconsistent packet to pass the length check and trigger an out-of-bounds memory read.

This behavior was confirmed using AddressSanitizer.

Potential consequences include:

  • Out-of-bounds memory reads

  • Process termination or denial of service

  • Unexpected behavior during malformed Cluster Bus message processing

The observed impact was a heap-buffer-overflow and loss of server responsiveness in the sanitizer-enabled test environment. No remote code execution or information disclosure was demonstrated.

References

The full AddressSanitizer log and reproduction details can be provided privately to maintainers if needed.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant