Repository navigation
Fix undefined behaviour when reading query state from native protocol - #101972
alexey-milovidov merged 1 commit into
Conversation
|
Workflow [PR], commit [6dbf166] Summary: ✅ AI ReviewSummaryThis PR hardens ClickHouse Rules
Final Verdict
|
cde8d4e to
d394b77
Compare
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]>
d31f2f1 to
6dbf166
Compare
LLVM Coverage Report
Changed lines: 100.00% (27/27) · Uncovered code |
|
Hi — this PR may need backporting to Affected code: Why: Fix in If this should be backported, consider adding |
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]>
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]>
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]>
The native protocol fuzzer found undefined behaviour when reading
QueryProcessingStage::Enumfrom the wire: the value was cast to the unscoped enum without validation, triggering UBSan for out-of-range values.This PR validates all three enum-like fields read from the Query packet:
QueryProcessingStage: must be< MAX(0–4) or exactlyQueryPlan(7)Protocol::Compression: must be 0 or 1query_kind: must beINITIAL_QUERYorSECONDARY_QUERY; rejectNO_QUERY(which causesClientInfo::readto skip parsing) and any unknown valuesChangelog category (leave one):
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
QueryProcessingStagevalues.Documentation entry for user-facing changes
Version info
26.4.1.747