Repository navigation
Validate cluster module message lengths before allocation #5269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shuvajyotikar13
wants to merge
9
commits into
valkey-io:unstable
Choose a base branch
from
shuvajyotikar13:fix/cluster-module-message-length
base: unstable
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+107
−1
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5f622ec
Validate cluster module message lengths before allocation
shuvajyotikar13 a6926d5
Update src/unit/test_cluster_module.cpp
shuvajyotikar13 73a8130
Merge branch 'unstable' into fix/cluster-module-message-length
shuvajyotikar13 e256859
Merge branch 'unstable' into fix/cluster-module-message-length
shuvajyotikar13 0ef52d8
Merge branch 'unstable' into fix/cluster-module-message-length
shuvajyotikar13 483ba14
Merge branch 'unstable' into fix/cluster-module-message-length
shuvajyotikar13 0c449aa
Merge branch 'unstable' into fix/cluster-module-message-length
shuvajyotikar13 d891d52
test: cover oversized cluster module send rejection
shuvajyotikar13 da1a02e
Merge branch 'unstable' into fix/cluster-module-message-length
shuvajyotikar13 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /* | ||
| * Copyright (c) Valkey Contributors | ||
| * All rights reserved. | ||
| * SPDX-License-Identifier: BSD-3-Clause | ||
| */ | ||
|
|
||
| #include "generated_wrappers.hpp" | ||
|
|
||
| #include <cstddef> | ||
| #include <cstdint> | ||
|
|
||
| extern "C" { | ||
| #include "cluster.h" | ||
| #include "cluster_legacy.h" | ||
| #include "server.h" | ||
| } | ||
|
|
||
| /* Mirrors clusterMsgSendBlock, which is private to cluster_legacy.c. */ | ||
| typedef struct TestClusterMsgSendBlock { | ||
| size_t totlen; | ||
| int refcount; | ||
| union { | ||
| clusterMsg msg; | ||
| clusterMsgLight msg_light; | ||
| } data[1]; | ||
| } TestClusterMsgSendBlock; | ||
|
|
||
| /* Returns the largest payload length that clusterModuleMessageLengthValid() | ||
| * accepts for a normal cluster header. The limit keeps the full send-block | ||
| * allocation within UINT32_MAX. */ | ||
| static uint32_t maxModulePayloadLen(void) { | ||
| size_t overhead = offsetof(TestClusterMsgSendBlock, data) + CLUSTERMSG_MIN_LEN + | ||
| offsetof(clusterMsgModule, bulk_data); | ||
| return (uint32_t)((size_t)UINT32_MAX - overhead); | ||
| } | ||
|
|
||
| TEST(ClusterModuleTest, ValidatesFullAllocationLength) { | ||
| uint32_t max_payload_len = maxModulePayloadLen(); | ||
|
|
||
| EXPECT_EQ(clusterModuleMessageLengthValid(0), 1); | ||
| EXPECT_EQ(clusterModuleMessageLengthValid(3), 1); | ||
| EXPECT_EQ(clusterModuleMessageLengthValid(max_payload_len), 1); | ||
| EXPECT_EQ(clusterModuleMessageLengthValid(max_payload_len + 1), 0); | ||
| EXPECT_EQ(clusterModuleMessageLengthValid(UINT32_MAX), 0); | ||
| } | ||
|
|
||
| TEST(ClusterModuleTest, NormalHeaderLimitAlsoProtectsLightHeaders) { | ||
| size_t normal_overhead = offsetof(TestClusterMsgSendBlock, data) + CLUSTERMSG_MIN_LEN + | ||
| offsetof(clusterMsgModule, bulk_data); | ||
| size_t light_overhead = offsetof(TestClusterMsgSendBlock, data) + CLUSTERMSG_LIGHT_MIN_LEN + | ||
| offsetof(clusterMsgModule, bulk_data); | ||
| uint32_t max_payload_len = maxModulePayloadLen(); | ||
|
|
||
| EXPECT_GT(normal_overhead, light_overhead); | ||
| EXPECT_EQ((size_t)max_payload_len + normal_overhead, (size_t)UINT32_MAX); | ||
| EXPECT_LE((size_t)max_payload_len + light_overhead, (size_t)UINT32_MAX); | ||
| } | ||
|
|
||
| /* Reject oversized messages at the actual cluster-module send boundary. | ||
| * The tiny payload must never be accessed for an invalid length. */ | ||
| TEST(ClusterModuleTest, RejectsOversizedMessageBeforeSending) { | ||
| const char payload[] = "x"; | ||
| uint32_t first_invalid = maxModulePayloadLen() + 1; | ||
|
|
||
| EXPECT_EQ(clusterSendModuleMessageToTarget(NULL, 0, 1, payload, first_invalid), C_ERR); | ||
| EXPECT_EQ(clusterSendModuleMessageToTarget(NULL, 0, 1, payload, UINT32_MAX), C_ERR); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.