Skip to content

Validate aggregate function states on deserialization - #115701

Merged
alexey-milovidov merged 5 commits into
masterfrom
fix-aggregate-state-deserialization-bounds
Aug 25, 2026
Merged

alexey-milovidov merged 5 commits into
masterfrom
fix-aggregate-state-deserialization-bounds

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Aug 21, 2026 •

Copy link
Copy Markdown
Member

States of aggregate functions can be built by a user (CAST(... AS AggregateFunction(...)), an
AggregateFunction column of a table, a merge in a distributed query), so everything that is read
out of them has to be validated:

  • stochasticLinearRegression / stochasticLogisticRegression: the number of weights in the state
    was 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::predict also lacked the check that LinearRegression::predict has.
  • The Resample combinator: 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, topK and the Distinct combinator over a fixed-size
    type: the elements of a state are inserted with insertData, which reads the whole width of the
    value and ignores the length it is given, so a shorter element read past the end of the buffer.

Changelog category (leave one):

  • Critical Bug Fix (crash, data loss, RBAC)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Validate the states of the stochasticLinearRegression, stochasticLogisticRegression, groupUniqArray, groupArrayIntersect and topK aggregate functions and of the Resample combinator on deserialization: a malformed state could be read out of bounds.


Workflow [PR]
Sync PR [sync-upstream/pr/115701]

Version info

  • Merged into: 26.9.1.100 (included in 26.9 and later)
  • Backported to: 26.8.1.2005, 26.7.6.23, 26.6.4.54, 26.3.23.4

@clickhouse-gh

clickhouse-gh Bot commented Aug 21, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [6fcfeb0]

Summary: ✅


AI Review

Summary

This PR hardens aggregate-function state deserialization for the ML methods, the Resample combinator, and the fixed-size-key aggregate states that use deserializeAndInsert. The original out-of-bounds reads are addressed, but one material gap remains: the ML deserializer still trusts attacker-controlled vector lengths long enough to resize them, so a malformed state can force very large allocations before the new consistency checks reject it.

Findings

⚠️ Majors

  • [src/AggregateFunctions/AggregateFunctionMLMethod.cpp:482] The ML-state invariant is "reject malformed vector lengths before they can affect execution", but LinearModelData::read still calls readBinary(weights) and readBinary(gradient_batch) before checking that their sizes match the type. readVectorBinary allows up to 1_GiB elements, so a crafted state can still force multi-gigabyte allocations before the new INCORRECT_DATA path runs; Adam::read, Momentum::read, and Nesterov::read repeat the same post-allocation pattern for updater vectors. Suggested fix: read each length first and reject anything except the exact expected size (or 0 for the legacy empty updater vectors) before resizing and reading the payload.
Final Verdict

⚠️ Needs another revision before merge: the malformed-state hardening is incomplete until the ML deserializer rejects oversized vector lengths before allocating them.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 87.20% 87.20% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 79.30% 79.30% +0.00%

Changed lines: Changed C/C++ lines covered: 57/75 (76.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care! labels Aug 21, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 22, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 6fcfeb0dd with master aeb16cd66 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 20743c1ad; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes
Binary Master PR Δ
programs/clickhouse-stripped 709.95 MiB 706.91 MiB -3.04 MiB (-0.43%)

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 units

13 translation units recompiled, 50 s compile time in total, 13 of them have a recent master baseline.

Job report

alexey-milovidov and others added 2 commits August 25, 2026 05:10
…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]>
readBinary(batch_size, buf);
weights_updater->read(buf);

if (weights.size() != expected_param_num)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?`

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The secondary problem can be fixed separately.

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.

@alexey-milovidov alexey-milovidov self-assigned this Aug 25, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Aug 25, 2026
Merged via the queue into master with commit 550137c Aug 25, 2026
178 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-aggregate-state-deserialization-bounds branch August 25, 2026 15:42
@robot-ch-test-poll4 robot-ch-test-poll4 added pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo labels Aug 25, 2026
clickhouse-gh Bot added a commit that referenced this pull request Aug 25, 2026
Backport #115701 to 26.8: Validate aggregate function states on deserialization
alexey-milovidov added a commit that referenced this pull request Aug 25, 2026
Backport #115701 to 26.5: Validate aggregate function states on deserialization
alexey-milovidov added a commit that referenced this pull request Aug 26, 2026
Backport #115701 to 26.3: Validate aggregate function states on deserialization
robot-ch-test-poll4 added a commit that referenced this pull request Aug 27, 2026
Cherry pick #115701 to 25.8: Validate aggregate function states on deserialization
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Aug 27, 2026
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Aug 28, 2026
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Aug 28, 2026
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Aug 28, 2026
clickhouse-gh Bot pushed a commit that referenced this pull request Aug 29, 2026
clickhouse-gh Bot added a commit that referenced this pull request Aug 29, 2026
Backport #115701 to 26.7: Validate aggregate function states on deserialization
nikitamikhaylov added a commit that referenced this pull request Aug 31, 2026
Backport #115701 to 26.6: Validate aggregate function states on deserialization
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Sep 4, 2026
huldarchen pushed a commit to huldarchen/ClickHouse that referenced this pull request Nov 6, 2026
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants