remote()/remoteSecure() with a named collection accepts a database/db override,
e.g. remote(nc, database = ...). TableFunctionRemote::parseArguments stored that
override expression directly as the remote table function without checking it is an
ASTFunction, unlike the positional signature remote('addr', table_function()) which
validates the node and otherwise treats it as a database name.
So remote(nc, database = (SELECT 1)) stored a non-function node as the table function.
When the shard is local it reached TableFunctionFactory::get(), where
ast_function->as<ASTFunction>() returns nullptr and table_function->name dereferenced
it: UBSan reported a member access within a null pointer, and a release build read from
a null base pointer. This is reachable from plain SQL, no fuzzer needed.
The override is now handled like the positional argument: a table function is stored as
the remote table function, anything else is evaluated as a constant database name. A
non-constant or wrong-typed expression now fails at argument-parsing time (before any
connection) with a clean error and the server stays up.
TableFunctionFactory::get() can no longer receive a non-ASTFunction node from this path
or from the analyzer's TableFunctionNode::toAST(), so the guard there is now an internal
invariant and throws LOGICAL_ERROR instead of UNEXPECTED_AST_STRUCTURE.
The gtest is replaced by a stateless test that reproduces the bug from SQL.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
Related: #107189
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a possible crash (null pointer dereference) when the
database/dboverride of a named collection passed toremote()/remoteSecure()is not a constant database name, e.g.remote(nc, database = (SELECT 1)). The query now fails with a clear error instead of crashing.Description
remote()/remoteSecure()with a named collection accepts adatabase/dboverride, e.g.remote(nc, database = ...).TableFunctionRemote::parseArgumentsstored that override expression directly as the remote table function without checking it is anASTFunction, unlike the positional signatureremote('addr', table_function())which validates the node and otherwise treats it as a database name.So
remote(nc, database = (SELECT 1))stored a non-function node as the table function. When the shard is local it reachedTableFunctionFactory::get(), whereast_function->as<ASTFunction>()returnsnullptrandtable_function->namedereferenced it: UBSan reported a member access within a null pointer, and a release build would read from a null base pointer. This is reachable from plain SQL, no fuzzer needed.UBSan report (
Stress test (arm_ubsan)):CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=107189&sha=ee127aeb85205b0eec364f557f260bf3e1e6ab38&name_0=PR&name_1=Stress%20test%20%28arm_ubsan%29
Fix: the
database/dboverride is now handled like the positional argument. A table function is stored as the remote table function; anything else is evaluated as a constant database name, so a non-constant or wrong-typed expression fails at argument-parsing time (before any connection) with a clean error and the server stays up.TableFunctionFactory::get()can no longer receive a non-ASTFunctionnode from this path or from the analyzer'sTableFunctionNode::toAST(), so the guard there is now an internal invariant and throwsLOGICAL_ERROR.A stateless test (
04408_remote_named_collection_database_override) reproduces the bug from SQL and replaces the previous unit test.Version info
26.6.1.1168