Repository navigation
Cherry pick #117765 to 26.7: Fix out-of-bounds reads when reading malformed JSON and Dynamic columns - #122883
Merged
Conversation
…format The FLATTENED branch of `SerializationObject::deserializeBinaryBulkWithMultipleStreams` unflattens `limit` rows out of the path columns without checking that the streams produced that many rows, so a truncated Native block made `unflattenAndInsertPaths` read past the end of a deserialized column. Nothing downstream catches it: `ColumnObject::size()` is the size of shared data, which unflattening fills to exactly `limit` regardless of the paths. Two shapes reached it. A short indexes stream of a flattened `Dynamic` stays self-consistent, because the per-type limits are derived from the indexes that were actually read and a run of NULL markers demands no values at all, so the existing per-type check passed and the shortened column was then read for `limit` rows. A short typed path stream was accepted outright and only failed later, when the resulting column was read. Validate both: every typed and flattened path column must advance by exactly `limit` rows before unflattening, and the indexes column of a flattened `Dynamic` must hold `limit` rows. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
… format A follow-up review of the same code found more ways to make the reader of a JSON or Dynamic column read out of bounds, or to escape as an internal error instead of a data error. `NativeWriter` writes only the `V1`, `V2`, `STRING` and `FLATTENED` serialization versions, and MergeTree only `V1`, `V2` and `V3`, but the reader accepted every version on both channels. Over Native that opened the whole shared data machinery of `V3` -- buckets, granules, stream marks -- to a hand-built block, and a block that declared more granules in one bucket than in bucket 0 aborted a release server on the first request. Each version is now rejected on the channel it is never written to. In the list of types of a flattened `Dynamic` column, two equal types map onto the same variant discriminator, and `fillDynamicColumn` then replaces the variant column a second time while the offsets still refer to the first, larger one -- an out-of-bounds read from a complete, self-consistent payload. `Nothing` is dropped by `DataTypeVariant`, so such a type has no discriminator to unflatten into and escaped as `std::out_of_range`. Both are rejected now. The check for duplicates is moved here from #115702, because this change is the one that gets backported. In the list of paths of an Object column, a repeated path, or a path that the type of the column declares as a typed path, is stored in the column twice, once as a dynamic path and once in shared data. Such a block was written into a part without a complaint and every later read of the column failed with `LOGICAL_ERROR`, or, for the typed collision, returned a JSON value with a duplicated key. On the part side, which is reachable with bytes the user chooses through `RESTORE` of a user-managed backup, a `web`/`s3_plain` disk or `ATTACH PART`, the reader of the shared data paths indexes stopped silently at the end of the stream, leaving the paths column shorter than the offsets say. Reading such a part segfaulted a release server. It now fails as a read error, and the checks of the sizes of the buckets, of the granule layout across buckets and of the values column are added next to it. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The readers of the ADVANCED shared data serialization take the granule layout from bucket 0 and then index every other bucket with it, so a bucket with fewer or shorter granules is read out of bounds. The check added for the reader of the whole shared data column covered only that one reader, and the same layout is used by the sub-object reader (`json.^a`) and by the distinct paths reader, where bucket 0's offset and limit are also used to index the Dynamic columns of the other buckets. The check now lives in one place and is called by all three, and it compares the offset of the granule as well. The MAP_WITH_BUCKETS branch of the distinct paths reader takes the number of rows of the result from bucket 0 while it appends the paths of all buckets, and it ends with `insertManyDefaults(num_new_rows - 1)`, so a bucket 0 that read no rows turned into an insert of SIZE_MAX values. Every bucket has to have the number of rows of the first one. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The granule layout of the ADVANCED shared data serialization and the sizes stream describe the same rows, but they are read independently, and nothing compared them. A structure stream that ends early leaves a layout that covers fewer rows than the sizes stream provides, and reading such a part segfaulted: the check across buckets added before does not catch it, because a part where every bucket is equally short is consistent with itself. The number of rows of the layout is compared with the number of rows read from the sizes stream now, which is the same corruption that `SerializationArray` reports as `CANNOT_READ_ALL_DATA`, so it is reported the same way and can be tested. `deserializePathsData` returned the column of a path without checking that it holds the rows of the granule, and the wide branch dereferenced the stream of the values without checking it for null while the compact branch above it does. Neither is reachable today -- a short path column is caught by the size checks of `Variant`, and a part without the values stream is detached as broken before it is read -- but both are one refactoring away from being. `SerializationSubObject` read every typed path, dynamic path and shared data with the same limit and never checked that they agree, unlike `SerializationObject`. A shared data stream that ends early silently dropped rows from a sub-object read. The duplicate check of the flattened paths kept string views into the vector of the paths while it was still being filled. The reserve of that vector is capped, so a block with more than a million paths reallocates it in the middle of the loop, and short paths are stored inside the string object, so every view already in the set pointed into the freed buffer (ASan: heap-use-after-free, reported by Raúl Marín). The paths are read first and the duplicates are looked for afterwards, when the vector no longer moves. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…nitizers Both tests spent all of their time starting processes: 05055 started a client for each of the 114 truncations of its payloads, and 05056 a `clickhouse-local` for each of its 9 blocks. A process start costs ~2.3 s in the build with ASan and ~37 s in the one with MSan, so the tests took 286 s and 332 s there and were failed by the 180 s limit of the reruns, while locally they take seconds. The blocks are sent over HTTP now, which keeps the same coverage -- the same payloads, the same step of the sweep -- and moves the work to the server that is already running. 05056 gained the row counts of its tables, so that a block that is accepted does not go unnoticed, and it no longer reads the blocks through schema inference, whose error wrapped the one being checked. 05055 is skipped with the old analyzer: the queries that build its payloads read JSON and Dynamic subcolumns, which it cannot resolve (`Unknown column: json.a` for the Dynamic payload, `Missing columns: 'x.a'` for the array one), while what the test checks is the deserialization of the Native format. Reruns of the tests in https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117765&sha=latest&name_0=PR Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Inserting the blocks over HTTP appends `: While executing Native` to the message of the exception, which the reference of the test does not have, because the messages were collected while the blocks were read through `clickhouse-local`. The reference states why a block is rejected, not where it happened, so the context is cut off instead of being added to it. Fast test in https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117765&sha=3f8f086f73b7c500ec34f81e40b2304552eaf8a6&name_0=PR&name_1=Fast%20test Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…d data The reader of the distinct paths of a JSON column builds one row with all the paths and fills the rest of the requested rows with empty ones, and it counted the rest as `num_new_rows - 1`. The streams are allowed to return fewer rows than the limit, down to none at all, and then that count is `SIZE_MAX`. Nothing is inserted when no rows were read now. The test of a part with a truncated stream took the directory of the part from a glob over `all_1_2_1` and spelled out the names of the streams, and both were wrong on the runner with macOS, where it found neither of the two streams it truncates. The directory comes from `system.parts` now and the names are matched, so the test no longer depends on the layout of the part, and the listing of the part is printed when a stream is missing, so that a failure names its own cause. Fast test (arm_darwin) in https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117765&sha=86a2c1842ba7c98bf33b52e7b705f7cd1730221d&name_0=PR Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…n-native-oob-read # Conflicts: # src/DataTypes/Serializations/SerializationObjectDistinctPaths.cpp # src/DataTypes/Serializations/SerializationObjectSharedData.cpp # src/DataTypes/Serializations/SerializationObjectSharedData.h # src/DataTypes/Serializations/SerializationSubObjectSharedData.cpp
The page listed the shared data serialization versions of an `Object` column and how to validate the count of its buckets, but that sub-version is read only in `V3`, which this change rejects in a `Native` stream, and `V1` and `V2` always use `MAP` without buckets. None of it can reach a decoder of the format, and the layouts it describes belong to a part rather than to a block. The rows of `V3` itself are kept, because the values are assigned and must not be reused, and a decoder that meets one should know what it is rejecting. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Every block of the test declares version 4 of the Object structure, which is written into MergeTree parts only and is refused in a Native stream now, so the guard the test was written for is no longer reachable from there and the blocks are rejected before the count is read. Three of the four cases even kept passing, because the refusal raises `INCORRECT_DATA` as well, which is what they expect. The guard itself stays: it is what rejects the count of a corrupted part, and the refusal of version 4 is covered by `05056_json_dynamic_native_malformed_structure`. Fast test in https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117765&sha=8c6ad24be00c431f0e1c436db290aa491e79ec9c&name_0=PR Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The names of the streams of a part are hashed on the runner with macOS, so the test finds neither of the two files it truncates: its diagnostic listing there is 400 files named like `0163bc0520e94055b9b495155ea5375d.bin`. What the test checks does not depend on the platform, so it is skipped there instead of being taught to find a stream without its name. The listing is also capped, it was printing the whole part into the diff. Fast test (arm_darwin) in https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117765&sha=71c18a444cb3d8d8cbb53a0eb14f4e1a5c39f4ad&name_0=PR Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…attened-json-native-oob-read
…attened-json-native-oob-read
…oob-read Fix out-of-bounds reads when reading malformed JSON and Dynamic columns
`26.7` has the per-granule `SerializationObjectSharedData` and the older Dynamic flattened reading, so the source files carry the private `release/26.6` resolution of this change (ClickHouse/clickhouse-private#78226): the new checks use `StructureGranule` / `checkGranulesMatchFirstBucket` instead of master's per-chunk names, with the same messages and error codes. Ported with a three-way merge onto the `26.7` files. The only manual step is in `SerializationObject.cpp`, where `26.7` already appends the flattened paths one by one: keep that loop and add the duplicate-path check after it, as on master. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Original pull-request #117765
Do not merge this PR manually
This pull-request is a first step of an automated backporting.
It contains changes similar to calling
git cherry-picklocally.If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.
The check results does not matter at this step - you can safely ignore them.
Before you resolve anything
Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.
Troubleshooting
If the conflicts were resolved in a wrong way
If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:
pr-cherrypicklabel from the PRYou also need to check the Original pull-request for
pr-backports-createdlabel, and delete if it's presented thereThe PR source
The PR is created in the CI job