Skip to content

Fix undefined behaviour when reading query state from native protocol - #101972

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
Algunenano:validate-native-protocol-input
Apr 9, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
Algunenano:validate-native-protocol-input

Conversation

@Algunenano

@Algunenano Algunenano commented Apr 7, 2026 •

Copy link
Copy Markdown
Member

The native protocol fuzzer found undefined behaviour when reading QueryProcessingStage::Enum from the wire: the value was cast to the unscoped enum without validation, triggering UBSan for out-of-range values.

src/Server/TCPHandler.cpp:2408:16: runtime error: load of value 120, which is not a valid value for type 'QueryProcessingStage::Enum'

This PR validates all three enum-like fields read from the Query packet:

  • QueryProcessingStage: must be < MAX (0–4) or exactly QueryPlan (7)
  • Protocol::Compression: must be 0 or 1
  • query_kind: must be INITIAL_QUERY or SECONDARY_QUERY; reject NO_QUERY (which causes ClientInfo::read to skip parsing) and any unknown values

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 undefined behaviour when parsing native protocol query packets with invalid QueryProcessingStage values.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.4.1.747

@clickhouse-gh

clickhouse-gh Bot commented Apr 7, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [6dbf166]

Summary: ✅


AI Review

Summary

This PR hardens TCPHandler::processQuery against invalid native-protocol wire values by validating query_kind, QueryProcessingStage, and compression before enum casts/usage, and adds a focused stateless test that exercises accepted and rejected values. I did not find additional correctness, safety, concurrency, or performance issues in the current PR head.

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 ✅
Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Apr 7, 2026
Comment thread src/Server/TCPHandler.cpp Resolved
@Algunenano
Algunenano force-pushed the validate-native-protocol-input branch from cde8d4e to d394b77 Compare April 7, 2026 18:02
Comment thread src/Server/TCPHandler.cpp Resolved
@Algunenano
Algunenano marked this pull request as draft April 7, 2026 19:12
The native protocol fuzzer found undefined behaviour when reading
`QueryProcessingStage::Enum` from the wire: the value was cast to the
unscoped enum without validation.

```
runtime error: load of value 120, which is not a valid value for type 'QueryProcessingStage::Enum'
```

Validate all three enum-like fields read from the Query packet:
- `QueryProcessingStage`: must be < `MAX` (0-4) or exactly `QueryPlan` (7)
- `Protocol::Compression`: must be 0 or 1
- `query_kind`: must be `INITIAL_QUERY` or `SECONDARY_QUERY`; reject
  `NO_QUERY` (which causes `ClientInfo::read` to skip parsing) and any
  unknown values

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@Algunenano
Algunenano force-pushed the validate-native-protocol-input branch from d31f2f1 to 6dbf166 Compare April 7, 2026 22:26
@Algunenano
Algunenano marked this pull request as ready for review April 7, 2026 22:56
@clickhouse-gh

clickhouse-gh Bot commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.00% 84.10% +0.10%
Functions 90.90% 90.90% +0.00%
Branches 76.50% 76.60% +0.10%

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

Full report · Diff report

@alexey-milovidov alexey-milovidov self-assigned this Apr 9, 2026
@alexey-milovidov
alexey-milovidov merged commit 2573cbc into ClickHouse:master Apr 9, 2026
159 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Apr 9, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

Hi — this PR may need backporting to 26.3 (LTS), 26.2, 26.1, 25.8 (LTS), but no backport label was found.

Affected code: TCPHandler::processQuery enum casts at lines 2267, 2275, and query_kind validation — core code present in all supported branches.

Why: Fix in TCPHandler::processQuery — git log shows TCPHandler.cpp dates back to the earliest ClickHouse development (predates all supported branches). The unvalidated QueryProcessingStage::Enum cast causes UB when malformed native protocol packets are received. The query_kind=NO_QUERY bypass could allow settings constraint evasion. All supported branches contain this vulnerable code.

If this should be backported, consider adding pr-must-backport or a version-specific label (e.g. v26.3-must-backport). Ignore this if backporting is not applicable.

pull Bot pushed a commit to admariner/ClickHouse that referenced this pull request Jul 1, 2026
Protocol::Server::toString and Protocol::Client::toString cast a raw UInt64
packet type, read straight off the wire, to the unscoped Enum and pass it to
magic_enum::enum_name. Loading an out-of-range value into the enum is undefined
behaviour (-fsanitize=enum), which arm_ubsan stress hit on a desynced byte:

  magic_enum.hpp:1293:39: runtime error: load of value 138, which is not a
  valid value for type 'DB::Protocol::Server::Enum'

Reject values past the enum's MAX before the cast. magic_enum already maps
unknown values to an empty name, which every caller (it only stringifies the
value for an exception/log message) already tolerates. Same bug class as the
client->server enums fixed in ClickHouse#101972.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
PedroTadim pushed a commit that referenced this pull request Jul 1, 2026
Protocol::Server::toString and Protocol::Client::toString cast a raw UInt64
packet type, read straight off the wire, to the unscoped Enum and pass it to
magic_enum::enum_name. Loading an out-of-range value into the enum is undefined
behaviour (-fsanitize=enum), which arm_ubsan stress hit on a desynced byte:

  magic_enum.hpp:1293:39: runtime error: load of value 138, which is not a
  valid value for type 'DB::Protocol::Server::Enum'

Reject values past the enum's MAX before the cast. magic_enum already maps
unknown values to an empty name, which every caller (it only stringifies the
value for an exception/log message) already tolerates. Same bug class as the
client->server enums fixed in #101972.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
Protocol::Server::toString and Protocol::Client::toString cast a raw UInt64
packet type, read straight off the wire, to the unscoped Enum and pass it to
magic_enum::enum_name. Loading an out-of-range value into the enum is undefined
behaviour (-fsanitize=enum), which arm_ubsan stress hit on a desynced byte:

  magic_enum.hpp:1293:39: runtime error: load of value 138, which is not a
  valid value for type 'DB::Protocol::Server::Enum'

Reject values past the enum's MAX before the cast. magic_enum already maps
unknown values to an empty name, which every caller (it only stringifies the
value for an exception/log message) already tolerates. Same bug class as the
client->server enums fixed in ClickHouse#101972.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Pull request with bugfix, not backported by default 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