Skip to content

Fix use-after-free in FileSegmentRangeWriter::completeFileSegment - #101161

Merged
kssenii merged 6 commits into
ClickHouse:masterfrom
groeneai:fix/filesegment-use-after-free-STID-3229
Apr 17, 2026
Merged

kssenii merged 6 commits into
ClickHouse:masterfrom
groeneai:fix/filesegment-use-after-free-STID-3229

Conversation

@groeneai

@groeneai groeneai commented Mar 30, 2026 •

Copy link
Copy Markdown
Collaborator

In FileSegmentRangeWriter::completeFileSegment(), appendFilesystemCacheLog(file_segment) was called after completeAndPopFront(). The pop operation moves the FileSegmentPtr from the holder's list into FileSegment::complete(), which resets it via SCOPE_EXIT_SAFE. The list element is then erased. If no other shared_ptr to the FileSegment exists at that point (e.g. the cache has already evicted this segment from its key metadata), the object is destroyed, and the file_segment reference becomes dangling.

appendFilesystemCacheLog then reads range() and getDownloadedSize() from freed memory — detected by MemorySanitizer as use-of-uninitialized-value (STID: 3229-51fa, 3229-5315).

The fix moves the log call before the pop, where the FileSegment is guaranteed to still be alive. The log function only reads immutable segment properties (range(), getDownloadedSize()), so the reorder is safe — no semantic change.

Fixes #100761

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):

Fix a use-after-free in the filesystem cache write path that could cause reads from freed memory when logging completed file segments (detected by MemorySanitizer in BuzzHouse).

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.4.1.1019
  • Backported to: 26.3.10.44

In completeFileSegment(), appendFilesystemCacheLog(file_segment)
was called after completeAndPopFront() which moves and erases the
shared_ptr from the holder's list. When the holder's shared_ptr is
the last reference (e.g. the cache has already evicted this segment
from its metadata), the FileSegment object is destroyed, and the
reference becomes dangling.

Move the log call before completeAndPopFront() so the FileSegment
is still alive when its range and downloaded size are read.

Detected by MemorySanitizer in BuzzHouse (amd_msan) — STID 3229.
Fixes ClickHouse#100761
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR Validation Gate

a) Deterministic repro?
The crash is triggered non-deterministically by the BuzzHouse fuzzer under MSAN during ALTER TABLE operations that write metadata through CachedOnDiskWriteBufferFromFile. 4 hits across 4 unrelated PRs in the last 30 days, all on BuzzHouse (amd_msan). MSAN-specific — requires a full MSAN build to reproduce.

b) Root cause explained?
completeFileSegment() obtains a reference to the front FileSegment via file_segments->front() (which dereferences the holder's shared_ptr). Then completeAndPopFront() moves the shared_ptr into FileSegment::complete(), which resets it via SCOPE_EXIT_SAFE, and erases the (now null) list element. If the cache has already evicted this segment from its key metadata (no other shared_ptr exists), the FileSegment is destroyed. The subsequent appendFilesystemCacheLog(file_segment) reads getDownloadedSize() from freed memory.

c) Fix matches root cause?
Yes — the log call is moved before the destructive pop, so the FileSegment is guaranteed alive when its properties are read. No semantic change since appendFilesystemCacheLog only reads immutable properties (range(), getDownloadedSize()).

d) Test intent preserved?
No test was changed. This is a one-line reorder in production code.

e) Both directions demonstrated?
MSAN reproduction requires a dedicated MSAN build + BuzzHouse workload. The code analysis is definitive — textbook use-after-free pattern. CI's BuzzHouse (amd_msan) check will validate the fix.

Session: cron:clickhouse-ci-task-worker:20260330-041500

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @kssenii — could you review this? One-line fix for a use-after-free in FileSegmentRangeWriter::completeFileSegment() detected by MSAN (STID 3229, issue #100761). The appendFilesystemCacheLog call was after completeAndPopFront which may destroy the FileSegment — moved it before the pop.

@kssenii kssenii self-assigned this Mar 30, 2026
@kssenii kssenii added pr-must-backport Pull request should be backported intentionally. Use this label with great care! v25.12-must-backport can be tested Allows running workflows for external contributors labels Mar 30, 2026
@clickhouse-gh

clickhouse-gh Bot commented Mar 30, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ea82d76]

Summary: ❌

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

AI Review

Summary

This PR fixes a lifetime issue in FileSegmentRangeWriter::completeFileSegment by moving appendFilesystemCacheLog(file_segment) before completeAndPopFront, avoiding access to a potentially destroyed FileSegment. The change is minimal and directly addresses the reported sanitizer failure without altering broader behavior.

ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
No large/binary files ✅
Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Mar 30, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member

The Stress test (arm_msan) failure is fixed by #101239, which should be merged first. After it is merged, please update the branch to include the fix.

@alexey-milovidov

Copy link
Copy Markdown
Member

The Hung check failure is fixed by #102008 and #102010, let's update the branch

@clickhouse-gh

clickhouse-gh Bot commented Apr 14, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.00% -0.10%
Functions 90.90% 90.90% +0.00%
Branches 76.60% 76.60% +0.00%

Changed lines: 100.00% (5/5) · Uncovered code

Full report · Diff report

@kssenii
kssenii added this pull request to the merge queue Apr 17, 2026
Merged via the queue into ClickHouse:master with commit 4c74bee Apr 17, 2026
159 of 161 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Apr 17, 2026
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Apr 17, 2026
robot-ch-test-poll3 added a commit that referenced this pull request Apr 27, 2026
Cherry pick #101161 to 26.3: Fix use-after-free in FileSegmentRangeWriter::completeFileSegment
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 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 v25.12-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MemorySanitizer: use-of-uninitialized-value (STID: 3229-5315)

6 participants