Repository navigation
Fix row-policy bypass via client-supplied scalar overriding a policy's scalar subquery - #125495
Conversation
…uery cache A row policy whose USING expression contains a scalar subquery, e.g. `has((SELECT [2]), tenant_id)`, is resolved through the analyzer's scalar-subquery cache. That cache lives in `Context::scalars`, the same map into which `TCPHandler::processData` writes whatever a client sends in a `Scalar` packet (the `clickhouse-client --external ... --scalar` feature). The map has no provenance marker and the analyzer keys its lookup on the subquery's query-tree hash, so an authenticated user who holds only `SELECT` on the table can name a client scalar with that hash and forge the result of `(SELECT [2])`, replacing the row policy's predicate with an attacker-chosen array and reading every row. Track which entries of `Context::scalars` were supplied by the client (`addClientScalar`) and add `hasComputedScalar`, which reports only server-computed entries. The analyzer's scalar-subquery cache lookup (new and old analyzer) now uses `hasComputedScalar`, so a forged client scalar can no longer stand in for a subquery result and the subquery is executed. Explicit readers - `__getScalar`, `_shard_num`, and forwarding scalars to shards - keep reading client-supplied entries through `getScalar`/`hasScalar`/`getScalars`, so `--external ... --scalar` and distributed queries (which ship scalars over the same packet, including in the default no-`<secret>` configuration) are unaffected. Co-authored-by: yakov <[email protected]>
The test injects a forged `Scalar` packet named with the policy subquery's query-tree hash and checks the row policy still holds, that an explicit `__getScalar` reference to a client scalar keeps working, and that scalars shipped to shards in a distributed query still resolve. Co-authored-by: yakov <[email protected]>
|
Workflow [PR], commit [46ad722] Summary: ✅
AI ReviewSummaryThis PR prevents client-supplied Final VerdictStatus: ✅ Approve. |
Build profile diff (arm_release)Commit See the job log for details. |
The previous fix tracked which `Context::scalars` were client-supplied in a
separate `client_supplied_scalars` set kept in sync with the map by hand. That
left provenance in a second container that a future writer could forget to
update, silently reopening the bypass.
Store the origin with the scalar instead: `Context::scalars` now maps to a
`ScalarEntry { Block block; bool from_client; }`. `addScalar` writes
`from_client = false` (and so a server-computed value still overrides a
client-supplied one of the same name), `addClientScalar` writes
`from_client = true`, and `hasComputedScalar` reports an entry only when it is
not client-supplied. `getScalars` keeps returning a plain `Scalars`, so shard
forwarding and every other reader are unchanged; the origin is re-derived on a
shard when the forwarded scalar arrives through `addClientScalar`.
No behavior change - the analyzer's scalar-subquery cache still ignores
client-supplied scalars, so the row-policy bypass stays fixed.
Co-authored-by: yakov <[email protected]>
|
|
Backport #125495 to 26.10: Fix row-policy bypass via client-supplied scalar overriding a policy's scalar subquery
Backport #125495 to 26.8: Fix row-policy bypass via client-supplied scalar overriding a policy's scalar subquery
Backport #125495 to 26.9: Fix row-policy bypass via client-supplied scalar overriding a policy's scalar subquery
…supplied scalar overriding a policy's scalar subquery
…calar overriding a policy's scalar subquery
Backport #125495 to 26.3: Fix row-policy bypass via client-supplied scalar overriding a policy's scalar subquery
…calar overriding a policy's scalar subquery
closes https://github.com/ClickHouse/clickhouse-private/issues/69147
A row policy whose
USINGexpression contains a scalar subquery, e.g.has((SELECT [2]), tenant_id), is resolved through the analyzer's scalar-subquery cache. That cache lives inContext::scalars— the same map into whichTCPHandler::processDatawrites whatever a client sends in aScalarpacket (theclickhouse-client --external ... --scalarfeature). The map has no provenance marker and the analyzer keys its lookup on the subquery's query-tree hash, so an authenticated user who holds onlySELECTon the table can name a client scalar with that hash and forge the result of(SELECT [2]), replacing the policy's predicate with an attacker-chosen array and reading every row.The subquery hash is a stable function of the policy's subquery tree and is disclosed by the server itself as
__getScalar('<hash>')in forwarded/secondary query text, so no access tosystem.row_policiesis needed. Reproduced on 26.6, 26.7 and 26.10 (SELECT count()as the restricted user returns 1 row normally and all rows with one injected scalar).Fix. Track which entries of
Context::scalarswere supplied by the client (addClientScalar) and addhasComputedScalar, which reports only server-computed entries. The analyzer's scalar-subquery cache lookup — in both the new analyzer (evaluateScalarSubqueryIfNeeded) and the old one (ExecuteScalarSubqueriesVisitor) — now useshasComputedScalar, so a forged client scalar can no longer stand in for a subquery result and the subquery is executed. A server-computed value of the same name overrides and clears the client marker.The fix is deliberately at the consumer side, not at packet ingest: the
Scalarpacket is also the legitimate transport forclickhouse-client --external ... --scalarand for the initiator shipping scalar-subquery results to shards in a distributed query — and in the default configuration (no cluster<secret>) those shard connections are ordinary, non-interserver connections, so rejecting the packet there would break distributed queries. Explicit readers —__getScalar,_shard_num/_shard_count, and forwarding scalars to shards — keep reading client-supplied entries throughgetScalar/hasScalar/getScalars, so those features are unaffected.Test
05292_row_policy_scalar_subquery_cache_bypass.shprovisions the policy, derives the subquery hash the way an attacker would (from the__getScalarthe server prints), injects a forged scalar under that name with--external ... --scalar, and asserts the policy still confines the user; it also checks that an explicit__getScalarreference to a client scalar keeps working, and that scalars shipped to shards in a distributed query still resolve.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a row-policy bypass where an authenticated user could forge the result of a scalar subquery used in a row policy (e.g.
USING has((SELECT ...), col)) by sending aScalarpacket (as withclickhouse-client --external ... --scalar) named with the subquery's query-tree hash, and read rows the policy should hide. Client-supplied scalars are no longer accepted by the analyzer's scalar-subquery cache.Workflow [PR]
Sync PR [sync-upstream/pr/125495]
Version info
26.11.1.495-master(included in26.11and later)26.10.2.73,26.9.25.12,26.8.34.10,26.3.61.21