Repository navigation
Validate aggregate function states on deserialization - #115701
Conversation
|
Workflow [PR], commit [6fcfeb0] Summary: ✅
AI ReviewSummaryThis PR hardens aggregate-function state deserialization for the ML methods, the Findings
Final VerdictLLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 57/75 (76.00%) · Uncovered code |
…ration from the header
…-deserialization-bounds
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units13 translation units recompiled, 50 s compile time in total, 13 of them have a recent master baseline. |
…rd `Resample` state sizes Address review: the `Momentum`, `Nesterov` and `Adam` weights updaters deserialize their vectors from the data without checking them against the number of weights, so a crafted state could make `merge` or `addToBatch` read past the end of a vector. Pass the expected size (the gradient size, i.e. the number of weights plus one for the bias) into `IWeightsUpdater::read` and reject mismatched vectors with `INCORRECT_DATA`. An empty vector stays valid, because versions before 23.2 serialized the vectors empty until the first update; guard `Momentum::merge` and `Nesterov::merge` against an empty rhs as well. Also cut off absurdly large `Resample` combinator states: nested combinators can produce a state size that avoids the multiplication overflow check but still reaches the allocator sanity limit of 2^63 bytes, which is reported as a logical error. Found by the AST fuzzer: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=115701&sha=7ba562b7636f1a8219850caaaed32d51ba64519f&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%29 #115701 Co-Authored-By: Claude Fable 5 <[email protected]>
…-deserialization-bounds
| readBinary(batch_size, buf); | ||
| weights_updater->read(buf); | ||
|
|
||
| if (weights.size() != expected_param_num) |
There was a problem hiding this comment.
LinearModelData::read now checks that weights, gradient_batch, and the updater vectors have the expected lengths, but those checks only run after the corresponding readBinary(...) call has already resized the vector from the on-wire length. readVectorBinary still allows up to 1_GiB elements, so a crafted AggregateFunction(stochasticLinearRegression...) state can force multi-gigabyte allocations before reaching this INCORRECT_DATA path; Adam::read, Momentum::read, and Nesterov::read repeat the same post-allocation pattern for their updater vectors. Can we read the lengths first and reject anything except expected_param_num / expected_param_num + 1 (or 0 for the legacy empty updater vectors) before resizing?`
There was a problem hiding this comment.
The secondary problem can be fixed separately.
Backport #115701 to 26.8: Validate aggregate function states on deserialization
Backport #115701 to 26.5: Validate aggregate function states on deserialization
Backport #115701 to 26.3: Validate aggregate function states on deserialization
Cherry pick #115701 to 25.8: Validate aggregate function states on deserialization
…s on deserialization
…s on deserialization
…s on deserialization
Backport #115701 to 26.7: Validate aggregate function states on deserialization
Backport #115701 to 26.6: Validate aggregate function states on deserialization
…s on deserialization
…e type A state of `stochasticLinearRegression` / `stochasticLogisticRegression` is read with the generic `readBinary` for vectors, which resizes the vector to the count declared in the data (up to 1<<30 elements) before reading the values. The exact-size checks added in ClickHouse#115701 therefore ran only after the allocation: the 13-byte state from ClickHouse#121490 declares 1<<30 weights and makes the query allocate 8 GiB, failing with MEMORY_LIMIT_EXCEEDED, or, without a query limit, zero-filling 8 GiB and then failing with CANNOT_READ_ALL_DATA. The type fixes every length: one weight per feature, and one more value in the gradient and in each weights-updater vector. The six reads in LinearModelData::read, Adam::read, Nesterov::read and Momentum::read now use readVectorBinary with that length as the bound (the pattern Moments.h uses), so a longer declaration fails with TOO_LARGE_ARRAY_SIZE before anything is allocated. The existing checks still reject shorter declarations; valid states, including the empty updater vectors written before 23.2, are read as before and the wire format is unchanged. 05030_ml_method_malformed_state is extended with one over-declared state per read and three valid states at the exact lengths. Closes ClickHouse#121490 Co-Authored-By: Claude Opus 5.5 <[email protected]>
States of aggregate functions can be built by a user (
CAST(... AS AggregateFunction(...)), anAggregateFunctioncolumn of a table, a merge in a distributed query), so everything that is readout of them has to be validated:
stochasticLinearRegression/stochasticLogisticRegression: the number of weights in the statewas not checked against the number of features of the type, and the gradient was not checked
against the number of weights, so the prediction and the weight updaters read past the end of the
vectors.
LogisticRegression::predictalso lacked the check thatLinearRegression::predicthas.Resamplecombinator: every layer checks the number of elements against the limit on its own,but the sizes of the states multiply, so nested combinators could overflow the size of the state,
and the state was then allocated smaller than what was written into it.
groupUniqArray,groupArrayIntersect,topKand theDistinctcombinator over a fixed-sizetype: the elements of a state are inserted with
insertData, which reads the whole width of thevalue and ignores the length it is given, so a shorter element read past the end of the buffer.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Validate the states of the
stochasticLinearRegression,stochasticLogisticRegression,groupUniqArray,groupArrayIntersectandtopKaggregate functions and of theResamplecombinator on deserialization: a malformed state could be read out of bounds.Workflow [PR]
Sync PR [sync-upstream/pr/115701]
Version info
26.9.1.100(included in26.9and later)26.8.1.2005,26.7.6.23,26.6.4.54,26.3.23.4