Skip to content

[RESP] Fix 32-bit RDB length decoding offset and validate RESTORE payload bounds - #2465

Open
Nguyễn Phúc Hưng Thịnh (nphungthinh2601) wants to merge 2 commits into
microsoft:mainfrom
nphungthinh2601:fix/restore-32bit-length-encoding
Open

Nguyễn Phúc Hưng Thịnh (nphungthinh2601) wants to merge 2 commits into
microsoft:mainfrom
nphungthinh2601:fix/restore-32bit-length-encoding

Conversation

@nphungthinh2601

Problem

  1. RespLengthEncodingUtils.TryReadLength (case 2, 32-bit RDB length prefix 0x80):
    • Reads a big-endian Int32 starting at input[0] instead of input.Slice(1), incorporating the 0x80 header byte into the length and producing a negative value. This breaks DUMP + RESTORE for any string payload >= 16,384 bytes (existing SingleRestore32Bit tested 16,383 bytes, which actually exercised the 14-bit case 1 branch).
    • Does not check input.Length >= 5 or length >= 0 before succeeding, and TryWriteLength does not reject length < 0.
  2. KeyAdminCommands.NetworkRESTORE:
    • Indexes valueSpan[0] before checking valueSpan.Length < 10, causing an IndexOutOfRangeException on an empty payload.
    • Calls value.ReadOnlySpan.Slice(payloadStart + 1, length) without verifying that the decoded length matches the available payload bytes before the 10-byte RDB version + CRC64 footer.

Solution

  • libs/common/RespLengthEncodingUtils.cs:
    • Guard case 2 when input.Length >= 5, read BinaryPrimitives.TryReadInt32BigEndian(input.Slice(1), out length), and require length >= 0.
    • Reject length < 0 in TryWriteLength.
  • libs/server/Resp/KeyAdminCommands.cs:
    • Move if (valueSpan.Length < 10) ahead of if (valueSpan[0] != 0x00).
    • Validate length >= 0 && availablePayloadLength >= 0 && length == availablePayloadLength (returning ERR DUMP payload version or checksum are wrong) before slicing the payload.

Testing

  • Updated SingleRestore32Bit to 16_384 bytes (true 32-bit lower boundary) and SingleRestore14Bit to 16_383 bytes in test/standalone/Garnet.test/RespTests.cs.
  • Added TryRestoreMalformedPayloadsReturnError, RespLengthEncodingRoundTripBoundaries, and RespLengthEncodingRejectsInvalidInputs.
  • Verified in Release (net10.0, 0 warnings / 0 errors):
    dotnet test test/standalone/Garnet.test/Garnet.test.csproj -c Release --filter "FullyQualifiedName~SingleRestore|FullyQualifiedName~TryRestore|FullyQualifiedName~RespLengthEncoding"
    Passed! - Failed: 0, Passed: 15, Skipped: 0, Total: 15
    

Copilot AI balanced review requested due to automatic review settings December 6, 2026 15:09

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The length decoder accepts unsupported 0x81 and invalid markers as 32-bit lengths, allowing RESTORE to accept malformed payloads instead of rejecting them.

1 open issue
What changed in this PR

Fixes Garnet’s 32-bit RDB length decoding and strengthens RESTORE payload validation.

Changes:

  • Corrects the decoding offset and rejects negative lengths.
  • Checks payload bounds before indexing or slicing.
  • Adds malformed-payload tests and corrects boundary coverage.
File Description
test/​standalone/​Garnet.test/​RespTests.cs Adds validation tests and corrects length boundaries.
libs/​server/​Resp/​KeyAdminCommands.cs Validates RESTORE payload size before access.
libs/​common/​RespLengthEncodingUtils.cs Corrects 32-bit decoding and rejects negative lengths.

Review effort: Balanced

Comment thread libs/common/RespLengthEncodingUtils.cs Resolved
@nphungthinh2601

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

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.

2 participants