Skip to content

Fix use-after-free in Hive unpartitioned read path - #109164

Merged
alexey-milovidov merged 1 commit into
masterfrom
fix-hive-unpartitioned-read-uaf
Jul 5, 2026
Merged

alexey-milovidov merged 1 commit into
masterfrom
fix-hive-unpartitioned-read-uaf

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Jul 2, 2026 •

Copy link
Copy Markdown
Member

Related: #108094

Fixes a pre-existing use-after-free in StorageHive::collectHiveFiles on the read path for a non-partitioned Hive table.

In that branch, file_infos (the result of listDirectory) is a local of the else block and is destroyed at the block's closing brace, but pool.wait() — which joins the worker tasks — runs on the next line, after the vector is freed. The tasks capture the range-for variable file_info (a const FileInfo & into file_infos) by reference, so a task still executing or queued when the else block exits dereferences a dangling reference into the freed std::vector<FileInfo> while pool.wait() is joining. A multi-threaded read of a non-partitioned Hive table can therefore read freed memory (a use-after-free / data race).

The partition branch above is not affected: partitions is a function-scope local that outlives pool.wait(), so its [&] capture of the reference loop variable partition is well-defined and is left unchanged.

The fix captures file_info by value in the else-branch task (FileInfo is a small, copyable struct: path, last_modify_time, size), so the closure owns its data and no longer depends on file_infos outliving the tasks.

This was found while reviewing the AI report on #108094; it is pre-existing on master (independent of that crash fix) and is submitted here as a separate, focused change.

Changelog category (leave one):

  • Not for changelog

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

Fixed a use-after-free when reading a non-partitioned Hive table with multiple threads.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.7.1.530 (included in 26.7 and later)

In `StorageHive::collectHiveFiles`, the branch for a non-partitioned Hive
table declares `file_infos` inside the `else` block and schedules worker
tasks that capture the range-`for` variable `file_info` by reference (`[&]`).
`file_infos` is destroyed at the closing brace of the `else` block, but
`pool.wait()` - which joins those tasks - runs on the next line, after the
vector is already freed. A task still executing (or queued) at that point
dereferences a dangling reference into the freed `std::vector<FileInfo>`,
so a multi-threaded read of a non-partitioned `Hive` table can read freed
memory (a data race / use-after-free).

The partition branch above is not affected: `partitions` is a function-scope
local that outlives `pool.wait()`, so its `[&]` capture of `partition` (a
reference bound to a `partitions` element) is well-defined and is left
unchanged.

The fix captures `file_info` by value in the `else`-branch task (`FileInfo`
is a small, copyable struct: `path`, `last_modify_time`, `size`), so the
closure owns its data and no longer depends on `file_infos` outliving the
tasks.
@clickhouse-gh

clickhouse-gh Bot commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [bb35cf5]

Summary: ❌

job_name test_name status info comment
Finish Workflow FAIL
python3 ./ci/jobs/scripts/workflow_hooks/new_tests_check.py FAIL

AI Review

Summary

This PR fixes the immediate dangling-file_info capture in StorageHive::collectHiveFiles for non-partitioned Hive reads by copying FileInfo into each worker task. I do not see another lifetime bug in the patched path, but the PR still does not prove the advertised use-after-free fix with a focused regression, so the contract is still established only by inspection.

Tests
  • ⚠️ Please add the smallest regression around a non-partitioned Hive table read with max_threads > 1, ideally in a with_hive integration test or under ASan. This PR claims to fix a threaded use-after-free, but there is still no test that would fail if the capture regressed again; LLVM coverage also reports 0/6 changed C/C++ lines covered.
Final Verdict

⚠️ Needs changes before merge. The capture-by-value fix itself looks correct, but a lifetime fix on a multi-threaded read path should land with targeted proof.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 2, 2026
@kssenii kssenii self-assigned this Jul 2, 2026
Comment thread src/Storages/Hive/StorageHive.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.50% 85.50% +0.00%
Functions 92.60% 92.60% +0.00%
Branches 77.70% 77.70% +0.00%

Changed lines: Changed C/C++ lines covered: 0/6 (0.00%) · Uncovered code

Full report · Diff report

@alexey-milovidov alexey-milovidov removed the pr-bugfix Pull request with bugfix, not backported by default label Jul 5, 2026

@alexey-milovidov alexey-milovidov left a comment

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.

LGTM

@alexey-milovidov
alexey-milovidov merged commit e8b4dfa into master Jul 5, 2026
339 of 344 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-hive-unpartitioned-read-uaf branch July 5, 2026 05:45
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 5, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…tioned-read-uaf

Fix use-after-free in Hive unpartitioned read path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

3 participants