Skip to content

Fix heap-use-after-free in the EmbeddedRocksDB tuple key filter with a LowCardinality element - #125509

Merged
PedroTadim merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-kv-tuple-key-filter-low-cardinality-uaf
Oct 13, 2026
Merged

PedroTadim merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-kv-tuple-key-filter-low-cardinality-uaf

Conversation

@groeneai

@groeneai groeneai commented Oct 12, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #120527
Related: #118689

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixed a heap-use-after-free when an EmbeddedRocksDB table with a composite PRIMARY KEY was filtered by a tuple equality whose tuple has a LowCardinality element, e.g. WHERE (k, id) = (SELECT (name, id) FROM t LIMIT 1). Closes #120527.

Description

On 26.9 (every stable release since v26.9.1.1629-stable), WHERE (k, id) = (SELECT (name, id) FROM dim LIMIT 1) on an EmbeddedRocksDB table with PRIMARY KEY (k, id), where name is LowCardinality(String), reads a freed DataTypeTuple while building the key filter (heap-use-after-free; an ASan build aborts).

Root cause: the tuple-equality branch of traverseDAGFilter (added in #118689) took .get() of the temporary returned by removeNullable(recursiveRemoveLowCardinality(right->result_type)). recursiveRemoveLowCardinality allocates a new type when a tuple element is LowCardinality, so that temporary was the only owner and died at the end of the declaration, while the per-element loop kept reading its element types. The scalar subquery yields Nullable(Tuple(LowCardinality(String), UInt64)), which takes that allocating branch.

Change: keep the stripped type in a named DataTypePtr, the shape the single-column equals and tuple IN branches of the same file already use. Only EmbeddedRocksDB reaches the multi-column overload; Redis and KeeperMap use the single-column one, which was already safe.

Validation: found by automated review (the issue), no CI job has hit it. The new test aborts with heap-use-after-free on an ASan build of the base commit and passes on the fixed build. It passes 50/50 randomized runs, and the existing RocksDB / KeeperMap / KV key-filter tests pass.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/125509]

Version info

  • Merged into: 26.10.1.2307-master (included in 26.10 and later)

…element

`traverseDAGFilter` (multi-column overload, tuple-equality branch) bound a
raw `const DataTypeTuple *` to the temporary returned by
`removeNullable(recursiveRemoveLowCardinality(right->result_type))`.
`recursiveRemoveLowCardinality` allocates a new `DataTypeNullable` /
`DataTypeTuple` when a tuple element is `LowCardinality`, so that temporary
was the only owner and was destroyed at the end of the declaration, while
the per-element loop below kept reading `getElements()` from it.

A scalar subquery such as `(SELECT (lc_col, id) FROM t LIMIT 1)` yields
`Nullable(Tuple(LowCardinality(String), UInt64))`, which takes the
allocating branch, so `WHERE (k, id) = (SELECT ...)` on an
`EmbeddedRocksDB` table with a composite `PRIMARY KEY` read freed memory
(ASan: heap-use-after-free). Introduced by ClickHouse#118689.

Keep the stripped type in a named `DataTypePtr`, the shape the sibling call
sites in the same file (single-column `equals`, tuple `IN`) already use.

Closes ClickHouse#120527

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-request PR origin: a maintainer pinged or directed groeneai labels Oct 12, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 0 findings) plus my own cold review (1 finding).

# Sev Finding Verdict Evidence / action
1 💡 PR description named the wrong first affected release AGREE, fixed in the description the changed line is in every 26.9 stable tag since v26.9.1.1629-stable

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-9:20261012-210800

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. clickhouse local --queries-file=05367_kv_tuple_key_filter_low_cardinality.sql on an ASan build of the base commit aborts with heap-use-after-free (READ in getFilterKeys, freed in traverseDAGFilter KVStorageUtils.cpp:315, allocated by recursiveRemoveLowCardinality), 3/3.
b Root cause explained? The tuple-equality branch held a raw const DataTypeTuple * from the temporary returned by removeNullable(recursiveRemoveLowCardinality(...)). With a LowCardinality element that call allocates a new type, so the temporary died at the end of the declaration while the loop kept reading its elements. A scalar subquery yields Nullable(Tuple(LowCardinality(String), UInt64)), which takes that branch.
c Fix matches root cause? Yes. The stripped type is kept in a named DataTypePtr for the whole branch, the shape the single-column equals and tuple IN branches already use.
d Test intent preserved / new tests added? New stateless test with the subquery shape, plus an EXPLAIN check that the key filter (ReadType: GetKeys) is built, so it cannot pass through a full scan.
e Both directions demonstrated? Yes, same build dir: base RC=134 with the ASan report; fix RC=0 and output matches the reference. 50/50 randomized runs pass on the fix.
f Fix is general across code paths? The other type strips in the file already use named holders. Only EmbeddedRocksDB calls the multi-column overload; Redis and KeeperMap use the single-column one. 53 RocksDB / KeeperMap / KV key-filter tests pass.
g Fix generalizes across inputs (params/datatypes/wrappers)? Nullable(Tuple(LC, ...)) is tested. A constant Tuple(LC, ...) is rewritten to per-column equalities by ComparisonTupleEliminationPass and never reaches this branch. Types without LowCardinality do not allocate. Array/Map right sides are rejected earlier.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, format or result change.
i Invariants and contracts preserved? Every pointer into the stripped type is backed by a live owner for the whole branch, including the early return false inside the loop.

Session id: cron:clickhouse-impl-slot-5:20261012-191400

@clickhouse-gh

clickhouse-gh Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [01e7e77]

Summary: ✅


AI Review

Summary

This PR fixes a real lifetime bug in traverseDAGFilter for multi-column EmbeddedRocksDB key lookups: the tuple type derived from removeNullable(recursiveRemoveLowCardinality(...)) is now kept alive across the per-element conversion loop, and the new stateless regression test uses the scalar-subquery shape that reaches this branch. I did not find correctness, safety, performance, test, or changelog issues in the current diff.

Final Verdict

✅ No review findings. From a code-review perspective this looks ready; the remaining step is for the still-pending CI jobs to finish green.

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-foreign-db Connectivity to external databases (ODBC/JDBC, MySQL, PostgreSQL, etc.). labels Oct 12, 2026
@PedroTadim PedroTadim self-assigned this Oct 13, 2026
Comment thread src/Storages/KVStorageUtils.cpp Resolved
@PedroTadim
PedroTadim added this pull request to the merge queue Oct 13, 2026
Merged via the queue into ClickHouse:master with commit 81f5b3a Oct 13, 2026
177 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 13, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 2, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 2, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 2, 2026
Backport #125509 to 26.9: Fix heap-use-after-free in the EmbeddedRocksDB tuple key filter with a LowCardinality element
leshikus pushed a commit that referenced this pull request Nov 13, 2026
…sDB tuple key filter with a LowCardinality element
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-foreign-db Connectivity to external databases (ODBC/JDBC, MySQL, PostgreSQL, etc.). groeneai-origin-request PR origin: a maintainer pinged or directed groeneai pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.9-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dangling tuple element type when the KV tuple key filter holds LowCardinality

3 participants