Repository navigation
Fix use-after-free of the read buffer when creating an object storage reader throws - #125090
Conversation
… reader throws `StorageObjectStorageSource::createReader` declared `builder` and `source` before `read_buf`. If an exception (for example `MEMORY_LIMIT_EXCEEDED`) unwinds the function after the input format has been created over `*read_buf`, the buffer is destroyed first, and the input format, still owned by `builder` or `source`, then runs `~PeekableReadBuffer`, which reads and writes the freed buffer. Found by `Stress test (amd_msan)` as `use-of-uninitialized-value` in `~PeekableReadBuffer` with the uninitialized value created by a heap deallocation in `createReader`. Declare `read_buf` first, as `ReaderHolder` already does, so it outlives the objects that read it on every path. The test reads 100 CSV objects from S3 under `memory_tracker_fault_probability`: master ASan aborts 10/10, the fix passes 50/50. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 2 findings), plus my own cold review (1 nit).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261010-170300 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-5:20261010-135300 |
|
Workflow [PR], commit [cbb1ad3] Summary: ✅
AI ReviewSummaryThis PR fixes a real lifetime bug in object-storage reader construction and the companion ownership hole in Final Verdict✅ No blocking issues found. The existing inline finding is fixed in the current code. |
`addBuffer` took the `unique_ptr` by value, so the ownership moved into the parameter before `owned_buffers.emplace_back` could allocate. If that allocation threw, the parameter destroyed the buffer while the format built over it was still alive, and destroying the format then ran `~PeekableReadBuffer` on the freed buffer. `FormatFactory::getInputImpl` hits this for every wrapped buffer (compressed or parallel object storage reads), `getInputFormatFromASTInsertQuery` for inline insert data. Take the buffer by reference, so it moves only once the insertion succeeded and a failure leaves it with the caller, which declares it before the format. The template keeps that true for callers passing a `unique_ptr` to a derived buffer, which would otherwise bind a temporary. A probe that throws in `addBuffer` before the insertion aborts an ASan server with `heap-use-after-free` in `~PeekableReadBuffer` on a `.csv.gz` read from S3 and on an HTTP `INSERT ... FORMAT CSV`; with this change both fail cleanly with the thrown exception. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units158 translation units recompiled, 1231 s compile time in total, 158 of them have a recent master baseline. Median compile-time ratio to the baselines is ×0.94 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
Backport #125090 to 26.9: Fix use-after-free of the read buffer when creating an object storage reader throws
Backport #125090 to 26.8: Fix use-after-free of the read buffer when creating an object storage reader throws
Backport #125090 to 26.7: Fix use-after-free of the read buffer when creating an object storage reader throws
Backport #125090 to 26.3: Fix use-after-free of the read buffer when creating an object storage reader throws
…ffer when creating an object storage reader throws
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a heap use-after-free in the
s3,azureBlobStorage,hdfsand other object storage table functions and engines when an exception (for exampleMEMORY_LIMIT_EXCEEDED) was thrown while creating the reader of a file in a row input format such asCSV.Description
On 26.8 (also 25.8, 26.3, 26.9), a
CSVread froms3that hitsmax_memory_usagewhile a file's reader is being created writes into freed memory (heap-use-after-freeunder ASan,use-of-uninitialized-valueunder MSan).Found by
Stress test (amd_msan): report.StorageObjectStorageSource::createReaderdeclaredbuilderandsourcebeforeread_buf. When an exception unwinds the function after the input format is created,read_bufis destroyed first, and the input format, still owned bybuilderorsource, then runs~PeekableReadBuffer, which reads and writes the freed buffer.ReaderHolderalready declaresread_buffirst; this PR does the same for the locals.Validation (ASan, local MinIO): the new test aborts master 10/10 and passes 50/50 with the fix (also 50/50 through
clickhouse-testwith randomized settings); reverting only the reorder brings the abort back 10/10. A local probe that throws right beforereturn ReaderHolder(aborts the old order 3/3 and returns a clean error with the fix 3/3.IInputFormat::addBufferhad the same ordering problem: it took the buffer by value, so a failed allocation inowned_buffers.emplace_backdestroyed the buffer before the format reading it. It now takes the buffer by reference. No SQL test reaches that allocation reliably; a probe that throws there aborts an ASan server on a.csv.gzread from S3 and on an HTTPINSERT ... FORMAT CSVwith the old signature, and both fail cleanly with the new one.CI report and the crashing query
Query
2aa8085a, run withmemory_tracker_fault_probability = 0.05,input_format_parallel_parsing = 0:Thread 12955 logs nothing after this line.
Workflow [PR]
Sync PR [sync-upstream/pr/125090]
Version info
26.10.1.2229-master(included in26.10and later)26.9.18.6,26.8.26.6,26.7.29.13,26.3.50.11