Skip to content

Check the overflow when allocating a block of aggregate function states - #117370

Merged
Algunenano merged 4 commits into
ClickHouse:masterfrom
Algunenano:fix-aggregate-state-bulk-alloc-overflow
Sep 3, 2026
Merged

Algunenano merged 4 commits into
ClickHouse:masterfrom
Algunenano:fix-aggregate-state-bulk-alloc-overflow

Conversation

@Algunenano

@Algunenano Algunenano commented Aug 31, 2026 •

Copy link
Copy Markdown
Member

Follow-up for #115701.

A block of AggregateFunction states is deserialized into a single allocation of the size of one state times the number of rows, and both of them come from the data, so their product could wrap around: the block was then allocated smaller than what was written into it, and the states were created outside of it. Bounding the size of a single state, as the previous pull request did for the Resample combinator, shrinks the exponent but does not remove the wrap-around: a Native block of 110 bytes that declares 2^24 rows of a state of 2^40 bytes is enough to make the server write far outside the allocation, which reproduces as a SIGSEGV.

Every column that sizes itself by an element count is protected by PODArray, which checks the multiplication, so this was the only deserialization path computing a number of bytes as a product of two values taken from the data and handing it straight to the arena. The number of rows also comes from the data, so the row pointers are now reserved only after the size has been validated.

Related: #115701

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):

Check the overflow when allocating a block of aggregate function states on deserialization: a malformed Native block could make the server write out of bounds.


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

Version info

  • Backported to: 26.8.3.24, 26.7.7.15, 26.6.5.42, 26.3.30.8

A block of `AggregateFunction` states is deserialized into a single allocation of the size of one
state times the number of rows, and both of them come from the data, so their product could wrap
around: the block was then allocated smaller than what was written into it, and the states were
created outside of it. A `Native` block of 110 bytes that declares 2^24 rows of a state of 2^40
bytes was enough to make the server write far outside the allocation.

Every column that sizes itself by an element count is protected by `PODArray`, which checks the
multiplication, so this was the only deserialization path computing a number of bytes as a product
of two values taken from the data and handing it straight to the arena.

The number of rows also comes from the data, so the row pointers are now reserved only after the
size has been validated.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [69acbd3]

Summary: ❌

job_name test_name status info comment
Upgrade check (amd_release) FAIL
Error message in clickhouse-server.log (see upgrade_error_messages.txt) FAIL cidb IGNORED
Logical error: Cannot write to finalized buffer (STID: 2046-2c6e) FAIL cidb IGNORED
LLVM Coverage FAIL
Generate LLVM Coverage Diff Report FAIL cidb IGNORED
Print Uncovered Code FAIL cidb IGNORED
Parser memory check ERROR
Resolve master binary ERROR IGNORED

AI Review

Summary

This PR hardens AggregateFunction bulk deserialization by reading states in bounded blocks and by rejecting oversized Arena chunk requests before the allocator bookkeeping can wrap. The current head also fixes the earlier exception-safety gap around data.push_back(place), and I do not see any remaining correctness, safety, test-coverage, or PR-metadata issues in the diff.

Final Verdict

Status: ✅ Approve

LLVM Coverage Report

⚠️ No coverage measurement for commit 69acbd3: bash ci/jobs/scripts/generate_diff_coverage_report.sh failed (its output is on the Generate LLVM Coverage Diff Report result).

@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 31, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 69acbd38f with master 552cef42a (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes

418 object files changed (+37.30 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/clickhouse_common_io.dir/Common/Arena.cpp.o 6.41 KiB 29.97 KiB +23.56 KiB (+367.30%)

716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

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

Job report

The number of rows of a block comes from the data, and so does the size of a state, so the product
of the two must not be allocated upfront: it wraps around, and a block of a hundred bytes can ask
for an allocation of any size. The states are allocated in blocks bounded by the size of the array
of pointers that is reserved for them anyway, so a block of rows of an ordinary state still takes a
single allocation, and a state larger than that bound is allocated on its own. `NativeReader` avoids
preallocating from the row count for the same reason.

The arena adds the alignment, the padding and the rounding to the page size on top of the size it is
given, and these additions wrap around for a size close to the maximum, which makes it allocate a
chunk smaller than requested. Sizes that the allocator refuses outright are cut off there as well,
so that data-driven sizes are reported as a data error instead of the logical error the allocator
reports.

Arena allocation is unchanged for a block of states of an ordinary size: `ArenaAllocBytes` is the
same before and after for a block of 65409 states of 32 bytes.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Algunenano and others added 2 commits September 2, 2026 13:48
`createAndDeserializeBatch` appends the pointer to a deserialized state outside of the block that
destroys the state when the deserialization throws. Appending allocates, so it can throw as well,
and the state would then be neither destroyed nor owned by the column. This used to be unreachable
only because the callers reserved the whole range beforehand.

The batch of a state of zero size is bounded as well, so that the number of rows, which comes from
the data, is never passed to the helper as is.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@Algunenano
Algunenano added this pull request to the merge queue Sep 3, 2026
Merged via the queue into ClickHouse:master with commit f9ed874 Sep 3, 2026
171 of 175 checks passed
@Algunenano
Algunenano deleted the fix-aggregate-state-bulk-alloc-overflow branch September 3, 2026 11:18
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 3, 2026
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Sep 3, 2026
robot-ch-test-poll2 added a commit that referenced this pull request Sep 3, 2026
Cherry pick #117370 to 26.3: Check the overflow when allocating a block of aggregate function states
robot-clickhouse added a commit that referenced this pull request Sep 3, 2026
robot-ch-test-poll2 added a commit that referenced this pull request Sep 3, 2026
Cherry pick #117370 to 26.6: Check the overflow when allocating a block of aggregate function states
robot-clickhouse added a commit that referenced this pull request Sep 3, 2026
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Sep 3, 2026
clickhouse-gh Bot added a commit that referenced this pull request Sep 3, 2026
Backport #117370 to 26.8: Check the overflow when allocating a block of aggregate function states
clickhouse-gh Bot added a commit that referenced this pull request Sep 3, 2026
Backport #117370 to 26.7: Check the overflow when allocating a block of aggregate function states
clickhouse-gh Bot added a commit that referenced this pull request Sep 3, 2026
Backport #117370 to 26.6: Check the overflow when allocating a block of aggregate function states
Algunenano added a commit that referenced this pull request Sep 3, 2026
Backport #117370 to 26.3: Check the overflow when allocating a block of aggregate function states
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Sep 4, 2026
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Sep 4, 2026
bytehalo-infra added a commit to sentioxyz/ClickHouse that referenced this pull request Sep 25, 2026
AGG: malformed Native block of aggregate states -> crash (OOB write).
Effect in the production build a394e92: wrong/failing (the upstream test fails on it and passes on official 26.8.8.8; evidence/fix_ci_20260925/upstream_repro/residual/117370.PROD263.txt).
Upstream patch unchanged, with its test(s): 05055_aggregate_state_bulk_deserialization_overflow.

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