Skip to content

Fix write out of bounds in quantileTiming deserialization - #104141

Merged
alexey-milovidov merged 1 commit into
masterfrom
fix-quantile-timing-deserialization-oob
May 6, 2026
Merged

alexey-milovidov merged 1 commit into
masterfrom
fix-quantile-timing-deserialization-oob

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented May 5, 2026 •

Copy link
Copy Markdown
Member

Fix an out-of-bounds write while deserializing the 'large' kind of quantileTiming aggregate function state.

The deserializer reads a UInt16 index from the input and uses it to index into a fixed-size count_big array. A crafted aggregate state with an index in the range (BIG_THRESHOLD, UInt16_max] (between 30001 and 65535) caused a write past the end of count_big, corrupting adjacent memory of the aggregate state. Untrusted input can reach this code path through *Merge/*State round-trips, arrayReduce over AggregateFunction columns, distributed query merging, and similar entry points.

A bounds check is added that rejects out-of-range indices with INCORRECT_DATA, in line with the other validations in this header.

A regression test is added with a collection of crafted serialized states.

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

Fix write out of bounds while deserializing quantileTiming state.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.5.1.318
  • Backported to: 26.4.2.5, 26.3.10.55, 26.2.18.8, 25.8.24.7

The deserializer of the 'large' kind of `quantileTiming` aggregate
function state read a `UInt16` index from the input and used it to
index into a fixed-size `count_big` array without validating that the
index is within bounds. A crafted aggregate state with an index in the
range `(BIG_THRESHOLD, UInt16_max]` (i.e. between 30001 and 65535)
caused a write past the end of `count_big`, corrupting adjacent memory
of the aggregate state.

Add a bounds check that rejects indices `>= BIG_SIZE` with an
`INCORRECT_DATA` exception, in line with the other validations in
this header.

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

clickhouse-gh Bot commented May 5, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b8cd311]

Summary: ✅


AI Review

Summary

This PR fixes a real memory-safety issue in quantileTiming state deserialization by rejecting out-of-range bucket indices before writing into count_big, and adds a regression test with crafted serialized states that now correctly fails with INCORRECT_DATA. The change is small, targeted, and aligned with existing validation behavior in this code.

ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
No large/binary files ✅
Final Verdict
  • Status: ✅ Approve

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

clickhouse-gh Bot commented May 5, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 91.10% 91.10% +0.00%
Branches 76.60% 76.60% +0.00%

Changed lines: 100.00% (7/7) · Uncovered code

Full report · Diff report

@alexey-milovidov alexey-milovidov self-assigned this May 6, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue May 6, 2026
@alexey-milovidov alexey-milovidov mentioned this pull request May 6, 2026
1 task
Merged via the queue into master with commit 8b664c3 May 6, 2026
165 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-quantile-timing-deserialization-oob branch May 6, 2026 09:00
robot-ch-test-poll4 added a commit that referenced this pull request May 6, 2026
Cherry pick #104141 to 25.8: Fix write out of bounds in quantileTiming deserialization
robot-clickhouse added a commit that referenced this pull request May 6, 2026
robot-ch-test-poll4 added a commit that referenced this pull request May 6, 2026
Cherry pick #104141 to 26.2: Fix write out of bounds in quantileTiming deserialization
robot-clickhouse added a commit that referenced this pull request May 6, 2026
robot-ch-test-poll4 added a commit that referenced this pull request May 6, 2026
Cherry pick #104141 to 26.3: Fix write out of bounds in quantileTiming deserialization
robot-clickhouse added a commit that referenced this pull request May 6, 2026
robot-ch-test-poll4 added a commit that referenced this pull request May 6, 2026
Cherry pick #104141 to 26.4: Fix write out of bounds in quantileTiming deserialization
robot-clickhouse added a commit that referenced this pull request May 6, 2026
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label May 6, 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 May 6, 2026
@robot-ch-test-poll1 robot-ch-test-poll1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label May 6, 2026
alexey-milovidov added a commit that referenced this pull request May 6, 2026
Backport #104141 to 26.4: Fix write out of bounds in quantileTiming deserialization
alexey-milovidov added a commit that referenced this pull request May 6, 2026
Backport #104141 to 26.3: Fix write out of bounds in quantileTiming deserialization
alexey-milovidov added a commit that referenced this pull request May 6, 2026
Backport #104141 to 26.2: Fix write out of bounds in quantileTiming deserialization
alexey-milovidov added a commit that referenced this pull request May 7, 2026
Backport #104141 to 25.8: Fix write out of bounds in quantileTiming deserialization
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…4141

Backport ClickHouse#104141 to 26.4: Fix write out of bounds in quantileTiming deserialization
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.

4 participants