Skip to content

Fix row-policy bypass via client-supplied scalar overriding a policy's scalar subquery - #125495

Merged
yakov-olkhovskiy merged 3 commits into
masterfrom
cursor/fix-row-policy-scalar-bypass-2579
Oct 22, 2026
Merged

yakov-olkhovskiy merged 3 commits into
masterfrom
cursor/fix-row-policy-scalar-bypass-2579

Conversation

@yakov-olkhovskiy

@yakov-olkhovskiy yakov-olkhovskiy commented Oct 12, 2026 •

Copy link
Copy Markdown
Member

closes https://github.com/ClickHouse/clickhouse-private/issues/69147

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 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 to system.row_policies is 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::scalars were supplied by the client (addClientScalar) and add hasComputedScalar, 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 uses hasComputedScalar, 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 Scalar packet is also the legitimate transport for clickhouse-client --external ... --scalar and 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 through getScalar/hasScalar/getScalars, so those features are unaffected.

Test 05292_row_policy_scalar_subquery_cache_bypass.sh provisions the policy, derives the subquery hash the way an attacker would (from the __getScalar the 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 __getScalar reference to a client scalar keeps working, and that scalars shipped to shards in a distributed query still resolve.

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

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 a Scalar packet (as with clickhouse-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.

Open in Web Open in Cursor 

Workflow [PR]
Sync PR [sync-upstream/pr/125495]

Version info

  • Merged into: 26.11.1.495-master (included in 26.11 and later)
  • Backported to: 26.10.2.73, 26.9.25.12, 26.8.34.10, 26.3.61.21

cursoragent and others added 2 commits October 12, 2026 20:17
…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]>
@clickhouse-gh

clickhouse-gh Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [46ad722]

Summary: ✅


AI Review

Summary

This PR prevents client-supplied Scalar blocks from impersonating cached scalar-subquery results and bypassing row policies. Scalar provenance is preserved in context copies, while explicit scalar reads and shard forwarding remain supported. No unresolved findings. The previous legacy-analyzer test request is no longer applicable because enable_analyzer cannot disable the analyzer. The new regression passed in Fast test at 46ad7222a8a8; no local build or test run was performed.

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 Oct 12, 2026
@yakov-olkhovskiy
yakov-olkhovskiy marked this pull request as ready for review October 12, 2026 20:20
@clickhouse-gh clickhouse-gh Bot added the comp-rbac Authorization: roles, grants, quotas, row-level security, access checks. label Oct 12, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Commit 46ad7222a8a8a2b43b37465d9f71b9e3b7f47257 was not compared: the CI logs cluster did not answer (every POST attempt failed with an exception; stopped retrying after the 1250 s read budget).

See the job log for details.

@pufit pufit self-assigned this Oct 14, 2026
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]>
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@yakov-olkhovskiy
yakov-olkhovskiy added this pull request to the merge queue Oct 21, 2026
Merged via the queue into master with commit 62aa57f Oct 22, 2026
352 of 353 checks passed
@yakov-olkhovskiy
yakov-olkhovskiy deleted the cursor/fix-row-policy-scalar-bypass-2579 branch October 22, 2026 02:06
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 22, 2026
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Oct 22, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 22, 2026
Backport #125495 to 26.10: Fix row-policy bypass via client-supplied scalar overriding a policy's scalar subquery
clickhouse-gh Bot added a commit that referenced this pull request Oct 22, 2026
Backport #125495 to 26.8: Fix row-policy bypass via client-supplied scalar overriding a policy's scalar subquery
PedroTadim added a commit that referenced this pull request Oct 22, 2026
Backport #125495 to 26.9: Fix row-policy bypass via client-supplied scalar overriding a policy's scalar subquery
strtgbb pushed a commit to Altinity/ClickHouse that referenced this pull request Oct 22, 2026
…supplied scalar overriding a policy's scalar subquery
robot-clickhouse added a commit that referenced this pull request Oct 26, 2026
…calar overriding a policy's scalar subquery
@robot-ch-test-poll robot-ch-test-poll added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 26, 2026
PedroTadim added a commit that referenced this pull request Oct 29, 2026
Backport #125495 to 26.3: Fix row-policy bypass via client-supplied scalar overriding a policy's scalar subquery
leshikus pushed a commit that referenced this pull request Nov 13, 2026
…calar overriding a policy's scalar subquery
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-rbac Authorization: roles, grants, quotas, row-level security, access checks. 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.

7 participants