Repository navigation
Check LowCardinality indexes against the dictionary when reading data parts - #128476
alexey-milovidov merged 2 commits into
Conversation
… parts `SerializationLowCardinality::deserializeBinaryBulkWithMultipleStreams` reads the indexes of each granule from the stream, but only the branch without a global dictionary (the only one `Native` may use, since 108137a) checked them, through `checkIndexesAreLimited` in `insertRangeFromDictionaryEncodedColumn`. With a global dictionary the indexes were attached to it unchecked (`ColumnLowCardinality::create` + `insertRangeFrom` on the shared dictionary copies them as they are), and with additional keys `mapIndexWithAdditionalKeys` mapped every index past the dictionary to `index - dict_size` and passed that to `additional_keys->index(...)` without comparing it to the number of additional keys. `serialization_lowcardinality_fuzzer` found the second case (NightlyFuzzers, master c189bd9: an empty additional-keys column and UInt8 indexes up to 255). On a MergeTree part whose index stream points past the keys, 26.3, 26.8 and 26.9 release builds crash with SIGSEGV, and debug builds abort in `PODArray` or `ColumnString`. Check the indexes once, right after they are read, against the dictionary size plus the number of additional keys of the granule, which is exactly the range `uniqueInsertRangeWithOverflow` writes and `mapIndexWithAdditionalKeys` already assumes. The check is done before `mapIndexWithAdditionalKeys`, so it also bounds its overflow map, which was sized from the largest index in the stream. `checkIndexesAreLimited` becomes a public static member of `ColumnLowCardinality` so the deserializer reuses it, and it now takes the maximum in the index's own type instead of `UInt64`: the loop then vectorizes to `vpmaxub`/`umax`, which keeps the extra pass over the indexes of a granule cheap (a standalone loop over 8192 UInt8 indexes: 1398 ns -> 49 ns at -march=x86-64-v3). Native input is unaffected. The new gtest serializes `LowCardinality(String)` and `LowCardinality(Nullable(UInt64))` with and without additional keys, checks the round trip, and replaces the last index with the first value past the keys, which must be rejected with INCORRECT_DATA. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`IndexPastTheKeysIsRejected` used only `UInt8` indexes and only the first value past the keys, so it stayed green if the check were moved after `mapIndexWithAdditionalKeys`, whose overflow map is sized from the largest index. Each case now also rewrites its indexes as `UInt64`, checks that the widened stream reads back the same values, and sets the last index to `0xFFFFFFFFFFFFFFFF`, which must be rejected with INCORRECT_DATA. Without the check that index fails with CANNOT_ALLOCATE_MEMORY (with additional keys) or is accepted (without them). 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 rounds, 3 findings, 0 in the second) plus a separate cold review (0 blockers, 0 majors, 3 nits).
Severity: ❌ blocker / |
|
cc @Avogar @KochetovNicolai, could you review this and add the |
|
Workflow [PR], commit [e808c74] Summary: ✅
AI ReviewSummaryThe PR validates streamed Missing context / blind spots
Final VerdictStatus: ✅ Approve. No code changes requested. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units624 translation units recompiled, 5889 s compile time in total, 624 of them have a recent master baseline. |
118e960
Backport #128476 to 26.10: Check `LowCardinality` indexes against the dictionary when reading data parts
Backport #128476 to 26.9: Check `LowCardinality` indexes against the dictionary when reading data parts
Backport #128476 to 26.8: Check `LowCardinality` indexes against the dictionary when reading data parts
Backport #128476 to 26.3: Check `LowCardinality` indexes against the dictionary when reading data parts
…dictionary when reading data parts
…dictionary when reading data parts
…dictionary when reading data parts
… dictionary when reading data parts
Related: #128366
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Reading a
LowCardinalitycolumn from a corrupted data part whose indexes point past the dictionary now throwsINCORRECT_DATAinstead of reading past the end of the keys, which could crash the server.Description
Found by
serialization_lowcardinality_fuzzerin the master NightlyFuzzerslibFuzzer testsjob: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=c189bd99fbda59c78489a6734c49397533bdd591&name_0=NightlyFuzzers&name_1=libFuzzer%20testsSerializationLowCardinalityreads the indexes of each granule from the stream, but only the branch without a global dictionary (the only oneNativemay use) checked them, viacheckIndexesAreLimited. With a global dictionary the indexes were attached to it unchecked, and with additional keys an index past them went straight intoadditional_keys->index(...). On a data part whose index stream points past the keys (crafted or mis-written: random corruption fails the block checksum first), the 26.3, 26.8 and 26.9 releases crash with SIGSEGV (debug builds abort inPODArrayorColumnString).Both global-dictionary branches now check the indexes right after reading them against the dictionary size plus the number of additional keys, reusing
checkIndexesAreLimited(now a static member ofColumnLowCardinality), and throwINCORRECT_DATA. This also bounds the overflow map thatmapIndexWithAdditionalKeysallocates.checkIndexesAreLimitednow takes the maximum in the index's own type so the loop vectorizes (vpmaxub): a standalone loop over 8192UInt8indexes went from 1398 to 49 ns.Nativeinput is not affected, it already rejects a global dictionary.Validation: a new gtest round-trips
LowCardinality(String)andLowCardinality(Nullable(UInt64))with and without additional keys and rejects the first index past the keys and a maximalUInt64index (both fail on master in all four cases). The fuzzer input now throwsINCORRECT_DATAinstead of aborting. Valid MergeTree parts of sixLowCardinalitycolumn types, written with three dictionary sizes and with one or many dictionaries per part, read back identically through full scans, granule-skipping reads and merges, and theLowCardinalitystateless tests pass.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/128476]
Version info
26.11.1.1042-master(included in26.11and later)