Skip to content

Fix use-after-free on renaming Delta Lake columns to their physical names - #131029

Merged
alexey-milovidov merged 5 commits into
masterfrom
fix-delta-column-mapping-rename-uaf
Nov 12, 2026
Merged

alexey-milovidov merged 5 commits into
masterfrom
fix-delta-column-mapping-rename-uaf

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

Related: #86477
Related: #130793
Related: #120906
Related: #131045

This PR also reapplies #130793, which was reverted in #131045, so it contains both the original change and the fix.

DeltaLakeMetadataDeltaKernel::prepareReadingFromFormat renamed the requested columns to their physical names in place, while iterating over the same ColumnsDescription. Under column mapping, the physical name of a column can be the logical name of another one: after k is renamed to k_old and v to k, k_old is stored as k. When k_old is requested before k (SELECT k_old, k ...), ColumnsDescription::rename to the still existing k makes modify_key erase k_old, and the loop reads the freed node (heap-use-after-free under ASan, a silently lost column otherwise). The fix builds a new description with the physical names instead.

The in-place rename was added in #86477 and is in every release since 25.9. #130793 did not change it: its test has such a table, and the AST fuzzer reordered the columns of its query, in the stress test of #120906: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=120906&sha=4b935993be52f957f83759b7ea8fa1a133ae82c5&name_0=PR&name_1=Stress%20test%20%28arm_asan_ubsan%29

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 use-after-free on reading a Delta Lake table with column mapping in which a column is stored under the name of another column (for example, after column mapping is enabled on an existing table and columns are renamed), when the columns are requested in a particular order, such as SELECT k_old, k.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.11.1.2128-master (included in 26.11 and later)
  • Backported to: 26.10.5.6, 26.9.30.6, 26.8.39.8, 26.3.65.9

…ames

`DeltaLakeMetadataDeltaKernel::prepareReadingFromFormat` renamed the requested columns to their physical names in
place, while iterating over the same `ColumnsDescription`. Under column mapping the physical name of a column can be
the logical name of another one: after `k` is renamed to `k_old` and `v` to `k`, `k_old` is stored as `k`. When `k_old`
comes first, `ColumnsDescription::rename` to the existing `k` makes `modify_key` erase `k_old`, and the loop reads the
freed node. Build a new description with the physical names instead.

Found by the AST fuzzer in the stress test of #120906:
https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=120906&sha=4b935993be52f957f83759b7ea8fa1a133ae82c5&name_0=PR&name_1=Stress%20test%20%28arm_asan_ubsan%29

Caused by: #130793

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [343f308]

Summary: ✅


AI Review

Summary

The PR fixes a use-after-free when Delta Lake column mapping causes physical names to overlap logical names, and reapplies the related Top-K correctness fix. At head 343f308dae29ef1406069d6765edb2a0d5dba137, the previous test and changelog findings are addressed. One correctness risk remains to verify at the serialized-read boundary.

Missing context / blind spots
  • ⚠️ The Cloud-only ReadFromObjectStorageStep serializer/deserializer is absent from this checkout. Its reconstruction logic, or a serialized-plan regression, is needed to establish whether the state loss below reaches executing reads.
  • ⚠️ No local ClickHouse binary or configured build was available. The head's Fast test report explicitly skips the new rename-order regression; the targeted stateless and bugfix reports exposed no test results.
Findings

⚠️ Majors

  • src/Storages/prepareReadingFromFormat.h:42 — Column-mapping exclusions disappear during read-info serialization; downstream safety needs verification. ReadFromFormatInfo::serialize and ReadFromFormatInfo::deserialize omit columns_read_under_other_names. After a round trip, logical k can again match physical k belonging to logical k_old, bypassing the new Top-K guard and risking incorrect results. Workers optimize deserialized fragments, so the initiator's make_distributed_plan check alone does not close this path. Preserve/reconstruct the exclusion before eligibility checks, or demonstrate that the Cloud deserializer already does so.
Tests
  • ⚠️ Add a focused round-trip or serialized-plan regression using the overlapping column mapping, proving that Top-K remains disabled for the renamed sort column after deserialization.
Final Verdict

Status: ⚠️ Request changes. Establish that serialized Delta reads preserve the column-mapping exclusion before worker optimization, with a focused regression.

@clickhouse-gh clickhouse-gh Bot added pr-not-for-changelog This PR should not be mentioned in the changelog comp-datalake Data lake table formats (Iceberg/Delta/Hudi) integration. labels Nov 12, 2026
@Algunenano Algunenano self-assigned this Nov 12, 2026
@Algunenano Algunenano added pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-bugfix Pull request with bugfix, not backported by default and removed pr-not-for-changelog This PR should not be mentioned in the changelog v26.10-must-backport labels Nov 12, 2026
…e case

Pass `HTTPConnectionGroupType::STORAGE` to `createClientConfiguration`, the same as the master fix. Remove the
`WHERE` query from the test: with filter push down it depends on the separate filter-mapping bug.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Algunenano

Copy link
Copy Markdown
Member

Followup for the skill: #131044

alexey-milovidov and others added 3 commits November 12, 2026 12:01
The `AmberMap` sync #129464 is reverted in #131043, so `createClientConfiguration` takes `bool for_disk_s3` again.

Related: #131043

Co-Authored-By: Claude Opus 5.5 <[email protected]>
NamesAndTypesList requested_virtual_columns;
/// Columns of `source_header` that the format reads under other names (the physical names of a Delta
/// Lake table with column mapping). `format_header` can have another column under such a name.
NameSet columns_read_under_other_names;

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.

⚠️ [P2] Preserve the column-mapping exclusion across read-info serialization

The new guard depends on this set, but ReadFromFormatInfo::serialize and ReadFromFormatInfo::deserialize in prepareReadingFromFormat.cpp:444–503 omit it: a round trip preserves the logical source_header and physical format_header while leaving columns_read_under_other_names empty. With this test's mapping (k_old → k, k → v), supportsTopKDynamicFilter then accepts logical k against the unrelated physical k again.

Skipping Top-K on the distributing initiator does not establish safety after deserialization: doExecuteTask builds the worker pipeline with optimization enabled, and distributed sorting retains a local LimitStep over a full SortingStep. The Cloud-only read-step deserializer is absent from this checkout, so please verify whether it reconstructs this exclusion before optimization. Otherwise preserve/reconstruct the set across the read-info round trip and add a focused serialized-plan regression; the new local-read tests do not exercise this boundary.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@groeneai, this is good for a follow-up.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@groeneai, there was an outage, so this may have been missed: please make a follow-up PR for this (see #131029 (comment)).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Acknowledged. I'm investigating preserving columns_read_under_other_names across the ReadFromFormatInfo serialize/deserialize round trip; I'll follow up here with a PR (or my findings) shortly.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Follow-up PR: #131173

It writes the set as an optional trailer of ReadFromFormatInfo::serialize (only when it is not empty, so every other read keeps its bytes) and reads it back in deserialize. The only callers are the CLICKHOUSE_CLOUD ReadFromObjectStorageStep serializers, so no SQL test reaches it; the regression is a gtest of the round trip that fails on master.

@clickhouse-gh

clickhouse-gh Bot commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 343f308da with master 4a760bb3a (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Slowest function optimization changes (ThinLTO)

Median per-function time ratio to the master baseline: programs/clickhouse ×0.61, programs/clickhouse-keeper ×0.68 (each binary is linked separately, on a different machine and with different build flags); deltas below are relative to it.

Binary Function Master PR Δ vs median
programs/clickhouse DB::registerFunctionConversion(DB::FunctionFactory&) 6.3 s 13.0 s +9.2 s (+239%)
programs/clickhouse _ZN6detail10static_forITk7is_enumN2DB23AttributeUnderlyingTypeEZNS1_29callOnDictionaryAttributeType… 3.5 s 7.2 s +5.1 s (+244%)
Compile time of recompiled translation units

7 translation units recompiled, 13 s compile time in total, 7 of them have a recent master baseline.

Job report

robot-ch-test-poll3 added a commit that referenced this pull request Nov 14, 2026
Cherry pick #131029 to 26.3: Fix use-after-free on renaming Delta Lake columns to their physical names
alexey-milovidov added a commit that referenced this pull request Nov 14, 2026
Resolve the conflicts of the backport of #131029 to 26.3:
- `DeltaLakeMetadataDeltaKernel::prepareReadingFromFormat`: keep the fix (build a new
  `ColumnsDescription` instead of renaming in place), using the 26.3 `DeltaLake::getPhysicalName`.
- 26.3 has no top-K dynamic filtering, so drop the `supportsTopKDynamicFilter` guard, the
  `columns_read_under_other_names` field that only it used, and the test
  `05620_delta_lake_top_k_dynamic_filtering_column_mapping`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robot-clickhouse added a commit that referenced this pull request Nov 14, 2026
alexey-milovidov added a commit that referenced this pull request Nov 14, 2026
Resolve conflicts for the 26.9 backport of #131029:
- 26.9 has no `ReadFromObjectStorageStep::supportsTopKDynamicFilter` (top-K dynamic filtering
  is not applied to object storage reads there), so the `columns_read_under_other_names`
  guard, its plumbing in `ReadFromFormatInfo`, and the test
  `05620_delta_lake_top_k_dynamic_filtering_column_mapping` are dropped.
- The use-after-free fix in `DeltaLakeMetadataDeltaKernel::prepareReadingFromFormat` is kept,
  adapted to the 26.9 `DeltaLake::getPhysicalName` helper, with the test
  `05621_delta_lake_column_mapping_swapped_names_order`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robot-clickhouse-ci-1 added a commit that referenced this pull request Nov 14, 2026
Cherry pick #131029 to 26.8: Fix use-after-free on renaming Delta Lake columns to their physical names
robot-clickhouse added a commit that referenced this pull request Nov 14, 2026
robot-clickhouse-ci-1 added a commit that referenced this pull request Nov 14, 2026
Cherry pick #131029 to 26.9: Fix use-after-free on renaming Delta Lake columns to their physical names
robot-clickhouse added a commit that referenced this pull request Nov 14, 2026
robot-clickhouse-ci-1 added a commit that referenced this pull request Nov 14, 2026
Cherry pick #131029 to 26.10: Fix use-after-free on renaming Delta Lake columns to their physical names
robot-clickhouse added a commit that referenced this pull request Nov 14, 2026
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 14, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 14, 2026
Backport #131029 to 26.10: Fix use-after-free on renaming Delta Lake columns to their physical names
alexey-milovidov added a commit that referenced this pull request Nov 15, 2026
The backport commit `bfc5837e5dcc` was created on top of `857eb0600f48` but
its tree is that of `24d9096599b8` (a release-line commit not present in the
current `26.9`) plus the 3-file fix from #131029. A normal merge would
therefore drag ~785 unrelated files into the PR and conflict.

Resolve by taking the current `origin/26.9` tree and applying only the
intended change (`24d9096599b8..bfc5837`), so the PR diff against
`26.9` is exactly the fix: `DeltaLakeMetadataDeltaKernel.cpp` and test
`05621_delta_lake_column_mapping_swapped_names_order`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
alexey-milovidov added a commit that referenced this pull request Nov 16, 2026
Backport #131029 to 26.3: Fix use-after-free on renaming Delta Lake columns to their physical names
clickhouse-gh Bot added a commit that referenced this pull request Nov 16, 2026
Backport #131029 to 26.9: Fix use-after-free on renaming Delta Lake columns to their physical names
Algunenano added a commit that referenced this pull request Nov 16, 2026
Backport #131029 to 26.8: Fix use-after-free on renaming Delta Lake columns to their physical names
@clickhouse-gh clickhouse-gh Bot added comp-delta-lake Delta Lake tables: reading Delta metadata through delta-kernel-rs (table snapshots, schema and ty... and removed comp-datalake Data lake table formats (Iceberg/Delta/Hudi) integration. labels Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-delta-lake Delta Lake tables: reading Delta metadata through delta-kernel-rs (table snapshots, schema and ty... 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 Pull request should be backported intentionally. Use this label with great care! 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants