Repository navigation
Fix potential use-after-free when reading String .size or several Variant subcolumns over discontinuous ranges - #109458
tiandiwonder wants to merge 5 commits into
Conversation
…states Deserialization of the String .size subcolumn (single_stream format) and of Variant element / element-null-map subcolumns keeps a partially read column in the deserialize state and also publishes it into the substreams cache, where a second consumer of the same substream in the same query (the emitted full String column, or the state of another subcolumn of the same Variant) keeps a reference to the very same object. When one block spans several readRows calls (discontinuous mark ranges selected by the primary key), the next cache-miss read appended to the shared column in place via assumeMutable, violating the COW contract; if the append reallocated the buffer, other holders could be left reading freed memory (observable as use-after-free under ASan). Go through IColumn::mutate before the append so a shared column is cloned first, while an exclusively owned column is still appended in place (the zero-copy path is preserved). Same pattern as #109020, which fixes the same defect class for the with_size_stream .size read paths. Confirmed at runtime with temporary use_count instrumentation: all three sites appended with use_count == 2 on the added tests before the fix, and zero violations with identical query results after it. Related: #109020 Co-Authored-By: Claude Fable 5 <[email protected]>
|
Workflow [PR], commit [123a208] Summary: ❌
AI ReviewSummaryThis PR closes the shared-state COW holes in the legacy Findings
Final VerdictStatus: Mirror the COW break in |
alexey-milovidov
left a comment
There was a problem hiding this comment.
Thanks, this is amazing!
I tried to find more cases of assumeMutable, that we should avoid if we can.
Because COWPtr was designed to prevent exactly this type of errors.
… deserialization `SerializationVariantElementNullMap::deserializeBinaryBulkWithMultipleStreams` used `num_read_discriminators` before subtracting `rows_offset`. In the compact-discriminator path `deserializeCompactDiscriminators` appends `rows_offset + limit` discriminators but reports `variant_limit` only for the post-offset range, so the `variant_limit == 0` fast path filled `num_read_discriminators` (= `rows_offset + limit`) entries into the null map instead of just `limit`, over-growing `v.<type>.null` when a range with a skipped prefix contains no rows of this variant. Mirror `SerializationVariantElement`, which already normalizes with `discriminators_offset += rows_offset; num_read_discriminators -= rows_offset;` before its fast paths, so both the `variant_limit == 0` and `variant_limit == num_read_discriminators` branches append the correct post-offset count. Addresses the `clickhouse-gh` review finding on this PR. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Pushed af38024 addressing the Fix. On a regression test. I could not construct a deterministic SQL test for this, because the buggy path is currently unreachable: On |
Mirror the `IColumn::mutate` fix from `SerializationVariantElement` in `SerializationVariant::deserializeBinaryBulkWithMultipleStreams`. When the full `Variant` column is read before one of its subcolumns (e.g. `SELECT v, v.UInt64`), the full-column reader puts its local discriminators into the substreams cache as is, and the subcolumn reader adopts the very same column from the cache. The subcolumn deserialize state then keeps that shared discriminators column across `readRows` calls, so a later discontinuous range that appends to it in place via `assumeMutable` reallocates the buffer still referenced by the subcolumn state — the same use-after-free class this PR fixes for the subcolumn-owned side. Go through `IColumn::mutate` before deserializing discriminators so a shared column is cloned first; it is a no-op when the column is not shared. Extends the `04500` regression test to read the full `Variant` together with an extracted element and null-map subcolumn over discontinuous ranges. Addresses the `clickhouse-gh` review finding on `SerializationVariant.cpp`. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Pushed f4f8680 addressing the Fix. Test. Extended |
| /// The column in the state may still be shared with the full column emitted as a result | ||
| /// of a previous read through the substreams cache. Go through IColumn::mutate so a shared | ||
| /// column is cloned before we append to it in place. | ||
| auto mutable_string_column = IColumn::mutate(std::move(string_state.column)); |
There was a problem hiding this comment.
This fixes the legacy single_stream .size reader, but the with_size_stream path still has the same aliasing hole.
SerializationString::deserializeBinaryBulkWithSizeStream keeps string_state->size_column in deserialize state, publishes that exact column into the substreams cache, and then appends to it in place on the next cache-miss readRows pass via *string_state->size_column->assumeMutable() at src/DataTypes/Serializations/SerializationString.cpp:824-829. If the same query also reads s.size, SerializationStringSize::deserializeBinaryBulkWithSizeStream can take that cached size_column as its result column, so a discontinuous-range read still reallocates a buffer another reader is holding.
That leaves the String + .size fix incomplete for string_serialization_version = 'with_size_stream' (and for any nested String path, such as Variant(String, ...), that goes through the same reader). Please mirror the IColumn::mutate before the append in SerializationString::deserializeBinaryBulkWithSizeStream and add a with_size_stream regression alongside 04499.
There was a problem hiding this comment.
This site is intentionally out of scope for this PR: the open PR #109020 fixes exactly this path — it puts IColumn::mutate before the append in SerializationString::deserializeBinaryBulkWithSizeStream and in the with_size_stream branch of SerializationStringSize::deserializeBinaryBulkWithSizeStream, with its own regression test 04409_string_size_subcolumn_shared_buffer_merge. As noted in the description, this PR covers the disjoint legacy single_stream and Variant sites, so the two PRs complement each other without conflicting.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 21/21 (100.00%) · Uncovered code |
Avogar
left a comment
There was a problem hiding this comment.
This is something strange tbh. The whole point in substreams cache in serializations is to share the same subcolumn in multiple deserialized columns and fill it only once using assumeMutable so all shared copies will see updates. All MergeTree readers return constant ColumnPtr columns with possible shared subcolumns between columns, which is totally fine and never led to any issues. So the fix looks wrong.
Also tests don't reproduce anything uder asan build.
Thanks for the verification. I will close it and would you please check #109020? |
Related: #109020
Problem. When a query reads a
Stringcolumn together with its.sizesubcolumn (with the legacysingle_streamserialization), or reads several subcolumns of the sameVariantcolumn (elements, or an element together with a null map), and the primary key selects discontinuous mark ranges, an internal column buffer shared between the subcolumn readers is appended to in place while another reader still references it. If the append reallocates the buffer, the other holder is left pointing at freed memory — a latent use-after-free of the same class as the recurringarm_asan_ubsandouble-free / allocator-CHECK crashes addressed by #109020.Root cause. Deserialization of the
String.sizesubcolumn (SerializationStringSize::deserializeWithStringData) and ofVariantelement / element-null-map subcolumns (SerializationVariantElement,SerializationVariantElementNullMap) keeps a partially read column in the per-column deserialize state so it survives acrossreadRowscalls, and also publishes that column into the substreams cache. A second consumer of the same substream in the same query picks the column up from the cache and keeps a reference to the very same object. When one block spans severalreadRowscalls (continue_readingaccumulation over discontinuous mark ranges), the next cache-miss read appends to the shared column in place throughassumeMutable, which ignores the reference count and so violates the COW contract. Confirmed at runtime with temporaryuse_countinstrumentation: all three sites append withuse_count == 2on the added regression tests before the fix, and reach zero violations with identical query results after it.Fix. Go through
IColumn::mutate(std::move(...))before the in-place append: a shared column is cloned first (the other holder keeps its frozen snapshot), while an exclusively owned column is still appended in place, so the zero-copy path is preserved. For theVariantsites the mutate is placed before the discriminator-mode branch so both BASIC and COMPACT modes append into an exclusively owned column. This is the same pattern as #109020, which fixes the same defect class for thewith_size_stream.sizeread paths; the two PRs touch disjoint code and do not depend on each other. Regression tests04499_string_size_subcolumn_single_stream_shared_stateand04500_variant_element_discriminators_shared_statepin the correct results on all builds and give ASan builds the discontinuous-ranges recipe that exercises the shared-buffer path.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a potential use-after-free when a query reads a
Stringcolumn together with its.sizesubcolumn, or several subcolumns of the sameVariantcolumn, over discontinuous ranges of a table: an internal buffer shared between the subcolumn readers could be reallocated in place while still referenced.