Skip to content

Fix out of bound in timeSeriesGroupArray() - #117404

Merged
vitlibar merged 4 commits into
ClickHouse:masterfrom
vitlibar:fix-out-of-bound-in-timeseries-grouparray
Sep 2, 2026
Merged

vitlibar merged 4 commits into
ClickHouse:masterfrom
vitlibar:fix-out-of-bound-in-timeseries-grouparray

Conversation

@vitlibar

@vitlibar vitlibar commented Aug 31, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fix out of bound in timeSeriesGroupArray()


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

Version info

  • Merged into: 26.9.1.535-master (included in 26.9 and later)
  • Backported to: 26.8.3.56, 26.7.14.33, 26.3.33.139

@clickhouse-gh

clickhouse-gh Bot commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [dd28a0e]

Summary: ✅


AI Review

Summary

This PR hardens deserialization of timeSeriesGroupArray and the shared timeSeries*ToGrid aggregate states against oversized element, sample, and bucket counts in malformed serialized states. I did not find a remaining correctness or safety blocker in the current diff; the only issue left is that the changelog entry still understates the scope of the fix.

PR Metadata

Changelog category: Bug Fix is reasonable for this change.

Changelog entry: required, but the current text is too narrow because the PR now also hardens the shared AggregateFunctionTimeseriesBase / AggregateFunctionTimeseriesSamples deserializers used by the timeSeries*ToGrid family.

Exact replacement:
Fixes out-of-bounds writes and oversized allocations when deserializing malformed timeSeriesGroupArray and timeSeries*ToGrid aggregate states.

Findings

💡 Nits

  • [PR body] The changelog entry still mentions only timeSeriesGroupArray, so the published changelog would miss the timeSeries*ToGrid surfaces fixed by the shared deserializer changes. Replace it with: Fixes out-of-bounds writes and oversized allocations when deserializing malformed timeSeriesGroupArray and timeSeries*ToGrid aggregate states.
Final Verdict

✅ No remaining code blockers found in the current diff. Update the changelog entry before merge.

LLVM Coverage Report

Measured on commit dd28a0e.

Metric Baseline Current Δ
Lines 88.60% 88.70% +0.10%
Functions 92.10% 92.10% +0.00%
Branches 80.90% 81.00% +0.10%

Changed lines: Changed C/C++ lines covered: 24/29 (82.76%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Aug 31, 2026
size_t size = 0;
readBinaryLittleEndian(size, buf);

data.reserve(size, arena);

@vitlibar vitlibar Aug 31, 2026 •

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.

Here was a problem because this code could read size from corrupted data, then multiply it by the size of Element and try to allocate without any boundary checks.

@vitlibar vitlibar added the comp-promql PromQL / time-series subsystem: TimeSeries storage engine, PromQL parser, PromQL-to-SQL converter... label Aug 31, 2026
@vitlibar
vitlibar force-pushed the fix-out-of-bound-in-timeseries-grouparray branch from 399c9c0 to aed8d4d Compare August 31, 2026 21:56
@clickhouse-gh

clickhouse-gh Bot commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing dd28a0eb2 with master 88dd16346 (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

5 object files changed (+22.21 KiB total), 0 added.

Object file Master PR Δ
src/AggregateFunctions/CMakeFiles/clickhouse_aggregate_functions.dir/TimeSeries/AggregateFunctionTi… 651.23 KiB 673.03 KiB +21.79 KiB (+3.35%)

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

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

Median compile-time ratio to the baselines is ×1.06 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio.
The matched translation units cost +2.7 s (+8%) in total before that adjustment.

Job report

The number of elements is read from the serialized state and was used for a single
allocation upfront, so a state claiming 2^60 or more elements made the size of the
allocation in bytes overflow and the following writes went past the allocated memory.
The array now grows while the data is read.
The count of samples in a bucket comes from the serialized state, so a small state could
ask for an arbitrarily large allocation. Only a bounded number of samples is reserved now
and the buffer grows while the samples are read.
The number of buckets is read from the serialized state and was only checked against
the bucket count derived from the function parameters. A huge window makes that count
enormous, so a tiny state could ask for an arbitrarily large allocation: an 18-byte
state of `timeSeriesChangesToGrid` allocated 1.25 GiB. Only a bounded number of buckets
is reserved now and the map grows while the buckets are read.
@vitlibar
vitlibar force-pushed the fix-out-of-bound-in-timeseries-grouparray branch from 6f12a8f to e908ac1 Compare September 1, 2026 18:35
/// `bucket_count` is derived from the function parameters and a huge window makes it enormous, so the
/// number of buckets is only reserved up to a bound and the map grows while the buckets are read. That way
/// a corrupted count fails with an end-of-buffer error instead of allocating memory for the claimed number.
data(place)->buckets.reserve(std::min(buckets_size, MAX_BUCKETS_TO_RESERVE));

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.

The changelog entry still says only Fix out of bound in timeSeriesGroupArray(), but this hunk changes the shared AggregateFunctionTimeseriesBase deserializer too, so the published changelog will miss the whole timeSeries*ToGrid surface touched by the PR.

Please replace it with something specific and user-readable, for example: Fix out-of-bounds writes and oversized allocations when deserializing malformed timeSeriesGroupArray and timeSeries*ToGrid aggregate states.

@vitlibar
vitlibar enabled auto-merge September 2, 2026 11:02
@clickhouse-gh

clickhouse-gh Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

PromQL Compliance Report

Baseline: S3 master 88dd16346

Metric Baseline Current Δ
Score 77.74% 77.74% +0.00%
Passed 419 419 +0
Failed 3 3 +0
Unsupported 117 117 +0

Note: score is unchanged vs baseline.

Uploaded compliance JSON for this PR commit

@vitlibar
vitlibar added this pull request to the merge queue Sep 2, 2026
Merged via the queue into ClickHouse:master with commit 796431c Sep 2, 2026
175 checks passed
@vitlibar
vitlibar deleted the fix-out-of-bound-in-timeseries-grouparray branch September 2, 2026 12:32
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 2, 2026
middas-0706 pushed a commit to middas-0706/ClickHouse that referenced this pull request Sep 4, 2026
Review round 2 on the previous commit.

readStringBinaryInto stages a value that spans compressed blocks before
committing it to the Arena, because an Arena cannot reuse a superseded block.
Staging it as one tracked object per buffer refill made the cost a function of
the block size rather than of the value: at 4-byte blocks a 1 MB value needs
250001 objects of 24 bytes each. Small blocks are supported and reachable, and
02124_uncompressed_cache pins 12 bytes, while the test harness randomizes both
compress-block settings down to 1. Since the staging containers are
memory-tracked and throwing, that overhead converted a state that used to load
into MEMORY_LIMIT_EXCEEDED, so this is a compatibility fix as well as a
simplification. Measured on a wide part with both block sizes pinned to 4,
reading one legitimate 1 MB value back needs over 10 MB with per-refill
staging and at most 1.5 MB with one growing container.

The three TimeSeries/ deserializers are dropped from this change. ClickHouse#117404
covers all four sites they touched, so nothing is lost by leaving them to it.

The two fixtures whose destination is operator new pin their path and error
only, and their comment now says so. The tracker counts that destination but
never refuses it: enforce_memory_limit is size >= min_allocation_size_to_
throw_on_memory_limit, a ServerSettings entry defaulting to UInt64::max, so no
stateless test can make either side refuse. Arena and PODArray destinations do
refuse, because Allocator.cpp allocates through CurrentMemoryTracker::alloc
with the limit enforced, which is why the other sites here redden. Peak RSS
for the -Map key improves from 1444 to 423 MiB.

The refill fixture pins min_bytes_for_wide_part = 0 beside its two block
sizes. Part type is randomized per run, and a compact part resolves its own
1 MiB bound: at a 64 KiB pin it wrote 2 blocks where the wide writer writes
31. Without the pin the block structure that arm depends on was not
deterministic.
@nikitamikhaylov nikitamikhaylov added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Sep 7, 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 7, 2026
clickhouse-gh Bot added a commit that referenced this pull request Sep 7, 2026
Backport #117404 to 26.8: Fix out of bound in timeSeriesGroupArray()
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Sep 19, 2026
@robot-ch-test-poll3 robot-ch-test-poll3 removed the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Sep 24, 2026
nikitamikhaylov added a commit that referenced this pull request Sep 24, 2026
Merge the backport base while retaining the 26.3 time-series implementation.
Apply bounded sample reservations in the three release implementations that
precede the shared samples helper, and bound the outer bucket reservation.
Adapt the regression test to a large grid and cover each sample deserializer.

Related: #118551
Related: #117404
robot-ch-test-poll2 added a commit that referenced this pull request Sep 24, 2026
Cherry pick #117404 to 26.3: Fix out of bound in timeSeriesGroupArray()
nikitamikhaylov added a commit that referenced this pull request Sep 24, 2026
Merge the backport base while retaining the 26.7 hash map implementation of
`AggregateFunctionTimeseriesSamples`. Bound the initial sample reservation
to 4096 entries without importing the newer vector implementation.

Related: #118553
Related: #117404
robot-ch-test-poll2 added a commit that referenced this pull request Sep 24, 2026
Cherry pick #117404 to 26.7: Fix out of bound in timeSeriesGroupArray()
@robot-ch-test-poll robot-ch-test-poll added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Sep 24, 2026
clickhouse-gh Bot added a commit that referenced this pull request Sep 24, 2026
Backport #117404 to 26.7: Fix out of bound in timeSeriesGroupArray()
PedroTadim added a commit that referenced this pull request Sep 24, 2026
Backport #117404 to 26.3: Fix out of bound in timeSeriesGroupArray()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-promql PromQL / time-series subsystem: TimeSeries storage engine, PromQL parser, PromQL-to-SQL converter... pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default 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.

6 participants