Skip to content

Cherry pick #125495 to 26.3: Fix row-policy bypass via client-supplied scalar overriding a policy's scalar subquery - #127313

Merged
PedroTadim merged 4 commits into
backport/26.3/125495from
cherrypick/26.3/125495
Oct 26, 2026
Merged

PedroTadim merged 4 commits into
backport/26.3/125495from
cherrypick/26.3/125495

Conversation

@robot-ch-test-poll4

Copy link
Copy Markdown
Contributor

Original pull-request #125495

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Before you resolve anything

Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

cursoragent and others added 4 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]>
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]>
…ar-bypass-2579

Fix row-policy bypass via client-supplied scalar overriding a policy's scalar subquery
@robot-ch-test-poll4 robot-ch-test-poll4 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-critical-bugfix labels Oct 22, 2026
@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 all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ yakov-olkhovskiy
❌ cursoragent
You have signed the CLA already but the status is still pending? Let us recheck it.

@robot-ch-test-poll2

Copy link
Copy Markdown
Contributor

Dear @pufit, @yakov-olkhovskiy, this cherry-pick PR has not been updated for 3d0h0m15s. Please resolve the conflicts to backport #125495, or close this PR if the backport is no longer needed. This PR will be automatically closed after 7 days of inactivity.

@PedroTadim
PedroTadim merged commit 9c294c6 into backport/26.3/125495 Oct 26, 2026
14 checks passed
@PedroTadim
PedroTadim deleted the cherrypick/26.3/125495 branch October 26, 2026 10:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! pr-critical-bugfix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants