Skip to content

MOD-13409 Harden RDB loading (backport to 2.8) - #1045

Merged
gabsow merged 4 commits into
2.8from
codex/mod-13409-2.8
Jul 23, 2026
Merged

gabsow merged 4 commits into
2.8from
codex/mod-13409-2.8

Conversation

@gabsow

@gabsow gabsow commented Jul 23, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Backport of #1038 to 2.8.

  • validate Bloom and Cuckoo RDB metadata and serialized buffer sizes before allocation/copy
  • validate TDigest capacity and node counters before loading serialized nodes
  • add targeted corrupt-RDB regression coverage

Testing

  • gmake build
  • targeted corrupt-RDB suite: 3/3 passed

Note

High Risk
Changes the untrusted RDB restore path for probabilistic structures; incorrect validation could break legitimate snapshots or leave memory-safety gaps against malicious DUMP payloads.

Overview
Hardens RDB/DUMP restore for Bloom filters, Cuckoo filters, and T-Digest so crafted or inconsistent module payloads are rejected instead of trusting serialized sizes.

Bloom (BFRdbLoad): validates the bitset buffer is non-null and within safe size limits; for newer encodings requires bits to match bytes * 8; checks n2 and related bit geometry before bloom_validate_integrity.

Cuckoo (CFRdbLoad): bounds-checks filter/bucket counts and uint16 fields against module limits; runs CuckooFilter_ValidateIntegrity before allocating sub-filters; requires per-subfilter bucket counts to be non-zero, power-of-two, and within max; replaces debug assert on bucket data length with explicit length and overflow checks.

T-Digest (TDigestRdbLoad): ensures loaded cap matches what td_new(compression) allocated and tightens validation on merged/unmerged node counts relative to that cap.

Bumps patch version to 2.8.23 and adds flow tests that corrupt DUMP payloads (shrunk bitset/bucket strings, inflated TDigest cap) and assert RESTORE fails while the original key stays usable.

Reviewed by Cursor Bugbot for commit d09c499. Bugbot is set up for automated code reviews on this repo. Configure here.

gabsow added 2 commits July 23, 2026 17:21
* MOD-13409 Validate Bloom RDB bitset size on load (#126)

* MOD-13409 Validate Bloom RDB bitset size on load

Reject crafted module payloads where the loaded bloom bitset buffer length does not match the declared bits/n2, and add a flow regression test covering RESTORE with a corrupted DUMP payload.

* MOD-13409 clang-format rebloom.c

Run clang-format on rebloom.c to satisfy make lint formatting checks.

* MOD-13409 Validate Bloom RDB bitset length on load

Reject crafted module payloads where persisted bits do not match the loaded bitset buffer length, preventing OOB access after RESTORE.

(cherry picked from commit ecbdc81)

* Mod 13409 rdb hardening (#127)

* MOD-13409 clang-format rebloom.c

Run clang-format on rebloom.c to satisfy make lint formatting checks.

* MOD-13409 Harden CF RDB load buffer validation

Replace assert-only checks in CFRdbLoad with explicit validation of loaded sizes and ranges, and add a flow test that corrupts a CF DUMP payload to ensure RESTORE is rejected.

* MOD-13409 Harden remaining RDB loaders (TDigest)

TDigest RDB loading trusted attacker-controlled cap, which could desync the allocated centroid arrays from the loaded metadata and enable OOB access after RESTORE.

Reject payloads where loaded cap differs from the allocated cap derived from compression, validate node counters, and add a flow regression that corrupts a TDigest DUMP payload and asserts RESTORE is rejected.

With the earlier BF/CF fixes, this covers all remaining module data structures that load raw buffers from RDB/RESTORE (CMS/TopK already validate buffer sizes).

* Revert "MOD-13409 clang-format rebloom.c"

This reverts commit 456e53503f51b68c4d7a9df988e50a36adf2e61a.

(cherry picked from commit a4b4f80)

@cursor cursor 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.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 4b16757. Configure here.

raise RuntimeError(f"Unknown module opcode {opcode} at pos={pos}")

if not patched:
raise RuntimeError("Did not find target SINT to patch")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Test helper missing SINT opcode handling for TDigest

Medium Severity

The function _corrupt_dump_set_first_uint_after_3_doubles only handles opcodes RDB_MODULE_OPCODE_EOF (0), RDB_MODULE_OPCODE_UINT (2), RDB_MODULE_OPCODE_DOUBLE (4), and RDB_MODULE_OPCODE_STRING (5). It does not handle RDB_MODULE_OPCODE_SINT (1). The TDigest RDB save function uses RedisModule_SaveSigned for the cap field (and other fields like merged_nodes, unmerged_nodes, total_compressions), which produces opcode 1. When parsing a TDigest dump, the function will encounter opcode 1 and hit the raise RuntimeError("Unknown module opcode") fallthrough, preventing it from ever locating and patching the target field.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 4b16757. Configure here.

gabsow added 2 commits July 23, 2026 18:07
Avoid signed overflow when validating merged and unmerged node counts by widening the addition.
@sonarqubecloud

Copy link
Copy Markdown

@gabsow
gabsow merged commit 89de0a9 into 2.8 Jul 23, 2026
56 of 59 checks passed
@gabsow
gabsow deleted the codex/mod-13409-2.8 branch July 23, 2026 17:57
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