Skip to content

MOD-13409 Harden RDB loading (backport to 8.6) - #1046

Merged
gabsow merged 5 commits into
8.6from
codex/mod-13409-8.6
Jul 23, 2026
Merged

gabsow merged 5 commits into
8.6from
codex/mod-13409-8.6

Conversation

@gabsow

@gabsow gabsow commented Jul 23, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Backport of #1038 to 8.6.

  • 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 deserialization of untrusted RDB/RESTORE data; failures are safer, but any overly strict check could reject valid snapshots—high impact because bugs here are memory-safety issues.

Overview
Hardens RESTORE/RDB load for Bloom filters, Cuckoo filters, and T-Digest so crafted or truncated module payloads are rejected instead of trusting metadata alone.

Bloom (BFRdbLoad): rejects missing/empty/oversized bitset buffers; for newer encodings requires bits to match buffer size × 8; validates n2 and related bit geometry before bloom_validate_integrity.

Cuckoo (CFRdbLoad): bounds-checks filter/bucket counts and fields that map to uint16_t; runs CuckooFilter_ValidateIntegrity before sub-filter allocation; validates per-subfilter bucket counts (non-zero, power-of-two, max) and that each serialized bucket blob length matches bucketSize × numBuckets; replaces assert-only checks with explicit load failures.

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

Patch release 8.6.5 and new flow tests corrupt DUMP payloads (shrunk bitset/bucket strings, inflated TDigest cap) and assert RESTORE errors while the original keys stay usable.

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

gabsow added 2 commits July 23, 2026 17:30
* 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.
* 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.
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 breaks TDigest test

High Severity

The _corrupt_dump_set_first_uint_after_3_doubles function only handles RDB_MODULE_OPCODE_UINT (2), RDB_MODULE_OPCODE_DOUBLE (4), RDB_MODULE_OPCODE_STRING (5), and RDB_MODULE_OPCODE_EOF (0). However, TDigest saves cap via RedisModule_SaveSigned, which writes RDB_MODULE_OPCODE_SINT (opcode 1). When parsing the TDigest payload after the three doubles (compression, min, max), the parser encounters opcode 1 (SINT) and raises RuntimeError("Unknown module opcode 1") instead of patching the cap value. This means test_restore_rejects_corrupted_tdigest_cap cannot produce a valid corrupted payload and doesn't actually test the hardened loading code.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 06364be. Configure here.

Comment thread src/rm_tdigest.c Resolved
Comment thread src/rm_tdigest.c
errdefer(err, td_free(tdigest));
if (tdigest == NULL) {
err = true;
return NULL;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

errdefer calls td_free on NULL tdigest pointer

Medium Severity

The errdefer on line 880 registers td_free(tdigest) as cleanup before the NULL check on line 881. When td_new returns NULL, setting err = true and returning triggers the errdefer, which calls td_free(NULL). Since td_free typically dereferences its argument to free internal arrays (nodes_mean, nodes_weight), this results in a NULL pointer dereference crash during error cleanup.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit bcc0df2. Configure here.

Avoid signed overflow when validating merged and unmerged node counts by widening the addition.
@sonarqubecloud

Copy link
Copy Markdown

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

There are 3 total unresolved issues (including 2 from previous reviews).

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 c5b2409. Configure here.

_lzf_decompress(comp, ulen)
pos = comp_end
continue
raise RuntimeError(f"Unknown module opcode {opcode} at pos={pos}")

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 parser missing SINT opcode handling for TDigest

Medium Severity

The _corrupt_dump_set_first_uint_after_3_doubles function only handles RDB_MODULE_OPCODE_UINT (2), RDB_MODULE_OPCODE_DOUBLE (4), RDB_MODULE_OPCODE_STRING (5), and RDB_MODULE_OPCODE_EOF (0). It does not handle RDB_MODULE_OPCODE_SINT (1). TDigestRdbSave uses RedisModule_SaveSigned for cap, merged_nodes, etc., which emits a SINT opcode. When the parser encounters this opcode after the 3 doubles, it falls through to the raise RuntimeError catch-all, preventing the TDigest corruption test from functioning.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit c5b2409. Configure here.

@gabsow
gabsow merged commit 3330ec8 into 8.6 Jul 23, 2026
55 of 58 checks passed
@gabsow
gabsow deleted the codex/mod-13409-8.6 branch July 23, 2026 17:52
@gabsow gabsow mentioned this pull request Jul 23, 2026
2 of 3 tasks
YaacovHazan pushed a commit to redis/redis that referenced this pull request Jul 23, 2026
## Summary

Bumps the RedisBloom pin on `8.6` from `v8.6.2` to `v8.6.5`.

| Module | From | To |
|---|---|---|
| RedisBloom | `v8.6.2` | `v8.6.5` |

## RedisBloom changes
([v8.6.2...v8.6.5](RedisBloom/RedisBloom@v8.6.2...v8.6.5))

- MOD-16050 — Replicate `CF.LOADCHUNK` data chunks to prevent silent
Cuckoo Filter data loss on failover
([#1021](RedisBloom/RedisBloom#1021))
- MOD-13409 — Harden RDB loading, including validation of crafted module
payloads ([#1046](RedisBloom/RedisBloom#1046))

## Test plan

- [x] Confirmed `v8.6.5` exists in RedisBloom
- [x] Validated `modules/redisbloom/Makefile` pins `MODULE_VERSION` to
`v8.6.5`
- [ ] Redis core CI passes with the updated module pin

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Medium Risk**
> Upstream changes affect replication and persistence loading for
Bloom/Cuckoo data; risk is confined to the version pin but runtime
behavior shifts on upgrade.
> 
> **Overview**
> **Bumps the RedisBloom build pin** from `v8.6.2` to `v8.6.5` in
`modules/redisbloom/Makefile` (`MODULE_VERSION` only).
> 
> That upstream release brings **replication for `CF.LOADCHUNK`** so
Cuckoo Filter chunks aren’t lost on failover, plus **stricter RDB load
validation** for crafted module payloads. No other files in this repo
change—consumers get the new `.so` when rebuilt against this pin.
> 
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
e3b8bc5. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
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