Skip to content

Skip the direct join when the right table has a row policy - #121398

Merged
vdimir merged 9 commits into
masterfrom
vdimir/direct-join-row-policy
Oct 1, 2026
Merged

vdimir merged 9 commits into
masterfrom
vdimir/direct-join-row-policy

Conversation

@vdimir

@vdimir vdimir commented Sep 21, 2026 •

Copy link
Copy Markdown
Member

A direct join with an EmbeddedRocksDB, KeeperMap, Redis or dictionary table, or a join with a Join table, replaces the right-side query plan with a lookup into the storage. That plan is the only thing carrying the FilterStep for the table's row policy, so the policy was lost.

  • In the new analyzer the row policy case was already excluded by the used_row_policies.empty() guard in buildQueryPlanForJoinNode; the Join table case is handled below.
  • In the legacy analyzer (JoinedTables::makeTableJoin) there was no guard: with enable_analyzer = 0, a user holding only SELECT on such a table read the rows its row policy hides through a JOIN. The legacy analyzer cannot be enabled on master since 26.9, so this part is unreachable and untestable here; it is included to be backported to releases that still accept enable_analyzer = 0.
  • A Join table is a prebuilt hash table read as is by JOIN and joinGet, so its rows cannot be filtered there. While a row policy applies to such a table, JOIN against it and joinGet / joinGetOrNull fail with ACCESS_DENIED, following the mergeTreeIndex fix in Do not bypass row policies in the mergeTreeIndex table function #115304. A plain SELECT from the table is filtered as before. Creating the policy is still allowed, so existing policies, ALTER ROW POLICY and ON CLUSTER keep working. The Join engine and CREATE ROW POLICY documentation now say so.

additional_table_filters is a setting the user applies to their own query, not an access-control boundary, so the direct join dropping it for the right table is left for a separate master-only change.

Closes: https://github.com/ClickHouse/clickhouse-private/issues/70504

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: a direct JOIN with an EmbeddedRocksDB, KeeperMap or Redis table in the legacy analyzer (enable_analyzer = 0) ignored the row policy of the right table and returned hidden rows, and joinGet on a Join table ignored its row policy in both analyzers. Such a table is now joined through its query plan, which keeps the filter, and JOIN or joinGet against a Join table under a row policy throws ACCESS_DENIED.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.10.1.1148-master (included in 26.10 and later)
  • Backported to: 26.9.8.7, 26.8.15.26, 26.7.20.9, 26.3.39.8

vdimir and others added 2 commits September 21, 2026 16:42
…the legacy analyzer

`JoinedTables::makeTableJoin` installed a `StorageJoin`, dictionary or key-value storage on
`TableJoin` based on the storage type alone. `ExpressionAnalyzer::tryCreateJoin` then dropped the
right-side plan, and with it the `FilterStep` carrying the table's row policy, so a user holding only
`SELECT` on an `EmbeddedRocksDB` or `KeeperMap` table read every row the policy hides through a direct
join, and could enumerate the whole key space by joining against `numbers(N)`.

Skip the special storage when a `SELECT` row policy applies to the joined table, so it is joined as an
ordinary stream through its plan, which keeps the filter. A `Join` table is a prebuilt hash table read
as is and cannot be filtered, so joining it under a row policy throws instead.

The legacy analyzer cannot be enabled on master anymore, so this cannot be tested here; the change
exists to be backported to releases that still accept `enable_analyzer = 0`.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…able_filters

In the new analyzer a `JoinStepLogicalLookup` replaces the right-side plan with a lookup into a
`StorageJoin`, dictionary or key-value storage, discarding the `FilterStep`s the plan applied to the
table. The `used_row_policies` guard already excluded the row policy case, but a filter from
`additional_table_filters` on the right table was silently dropped by the direct join.

Record on `TableExpressionData` that an additional filter applies to the table and skip the storage
lookup in that case too, so the join reads the table as a stream through its plan.

A `Join` table is a prebuilt hash table read as is by JOIN and `joinGet`, so its rows cannot be
filtered at all: `CREATE ROW POLICY` on such a table is rejected, and joining a `Join` table that
still has a row policy throws instead of silently falling back.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [60a2297]

Summary: ✅


AI Review

Summary

This PR closes the row-policy bypass on direct joins by forcing key-value and dictionary tables back through their filtered plan and by rejecting Join-engine JOIN/joinGet lookups when a nontrivial policy applies. The core fail-closed behavior looks consistent, but one important regression path is no longer covered and the structured docs for joinGet/joinGetOrNull were left stale.

Missing context / blind spots
  • ⚠️ The legacy-analyzer half in src/Interpreters/JoinedTables.cpp cannot be executed on master because enable_analyzer = 0 is no longer accepted there, so that backport-only path could only be validated by code inspection in this review.
Findings

⚠️ Majors

  • tests/queries/0_stateless/05234_row_policy_storage_join.sql The query-time ACCESS_DENIED guard was introduced specifically because policies can reach a Join table without going through CREATE ROW POLICY on that table (ON db.*, policy-before-table, table-after-policy). The rewritten regression now covers only CREATE ROW POLICY ... ON join_rls after the table already exists, so it no longer proves the path that motivated the fix. A regression in inherited or pre-existing policy matching would reopen the bypass while this test stays green.
    Suggested fix: keep one negative case where the policy reaches the table indirectly and assert that JOIN plus joinGet/joinGetOrNull still fail there.

💡 Nits

  • src/Functions/FunctionJoinGet.cpp joinGet/joinGetOrNull now fail with ACCESS_DENIED under row policies, but their structured function docs in this file still describe only the successful lookup behavior. The Join engine and CREATE ROW POLICY docs were updated, but the generated function pages remain stale.
    Suggested fix: add a short note to both function descriptions documenting the row-policy restriction.
Final Verdict

⚠️ Not ready to merge as-is. The runtime guard itself looks sound, but I would restore an indirect-policy regression case before merge; the joinGet/joinGetOrNull doc update is smaller and can ride along in the same patch.

LLVM Coverage Report

Measured on commit 60a2297.

Metric Baseline Current Δ
Lines 88.20% 88.20% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 79.60% 79.60% +0.00%

Changed lines: Changed C/C++ lines covered: 42/46 (91.30%) · Uncovered code

Full report · Diff report

@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 Sep 21, 2026
Comment thread src/Planner/PlannerJoinTree.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 60a229796 with master 120be6b5f (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 8dc18243e; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

11 translation units recompiled, 52 s compile time in total, 11 of them have a recent master baseline.

Job report

`joinGet` and `joinGetOrNull` read the hash table of a `Join` table as is, exactly like a JOIN with
it, so a row policy on the table was silently ignored on this path while the JOIN now throws.
Throw the same exception from `FunctionJoinGet::prepare`, next to the `SELECT` privilege check.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@vdimir vdimir added the pr-backward-incompatible Pull request with backwards incompatible changes label Sep 22, 2026
Comment thread src/Planner/PlannerJoinTree.cpp Resolved
@clickhouse-gh clickhouse-gh Bot added the comp-joins JOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla... label Sep 22, 2026

@Ergus Ergus left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neither the Join engine page nor CREATE ROW POLICY mentions that row policies are rejected for Join tables. Could you add at least a sentence for that?

|| (right_table_expression_data && right_table_expression_data->hasAdditionalFilter());

PreparedJoinStorage prepared_join = tryGetStorageInTableJoin(join_node.getRightTableExpressionNode(), planner_context);
if (prepared_join.storage_join && right_table_has_row_policy)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that a NOT_IMPLEMENTED throw for a Join table with a row policy is stricter than needed in the new analyzer and changes previously-correct behaviour. But its choice is defensible as fail-closed consistency because the legacy analyzer cannot filter the table and the changelog states it, so IMO it is good enough.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

joinGet bypassing row policy may also be considered a vulnerability, but properly supporting it would be overkill since it's quite a corner case, so it's easier to reject such queries. But maybe let's move this check to runtime rather than to CREATE, so we don't block server startup for existing tables and only block queries using joinGet on tables with row policy.

Comment thread src/Planner/PlannerJoinTree.cpp Resolved
if (typeid_cast<StorageJoin *>(storage.get()))
throw Exception(ErrorCodes::NOT_IMPLEMENTED,
"Row policies are not supported for table {} with the Join engine", joined_table_id.getNameForLogs());
storage = nullptr;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copuld be wrong but I think that the legacy branch handles the row policy but not additional_table_filters. Is it intended?

The new analyzer partially fixes both because the legacy half still lets a direct join drop a right-side additional_table_filters entry.

Hopefully this won't be a security issue, but a backport could carry an inconsistency because If the target still supports additional_table_filters under enable_analyzer = 0 then we need to extend the guard with context->getSettingsRef()[Setting::additional_table_filters] to match the joined table name, or note it as out of scope. WDYT?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dropped fix for additional_table_filters from this PR, let's not include it in back-port, since it's not security related, if needed we can fix it separately.

@Ergus Ergus self-assigned this Sep 28, 2026
vdimir and others added 4 commits September 28, 2026 16:32
Both the `Join` engine page and `CREATE ROW POLICY` now say that a policy
on a `Join` table is rejected, and that a policy which still applies to
one, such as a database-wide `ON db.*` policy, makes `JOIN` and `joinGet`
fail with `NOT_IMPLEMENTED`.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
The right side of a `JoinNode` is always a leaf table expression whose
data `collectTableExpressionData` has registered, and
`buildQueryPlanForTableExpression` has already fetched it with
`getTableExpressionDataOrThrow` by the time the join is planned, so the
null check was dead code.

Review comment: #121398 (comment)

Co-Authored-By: Claude Fable 5.1 <[email protected]>
The setting is applied by the user to their own query, not an access
control boundary, so dropping the filter in a direct join is an ordinary
correctness bug and does not belong in a backported RBAC fix. It will be
handled separately on master, where the legacy analyzer is unreachable.

Review comment: #121398 (comment)

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@clickhouse-gh clickhouse-gh Bot removed the pr-backward-incompatible Pull request with backwards incompatible changes label Sep 29, 2026
Comment thread src/Planner/PlannerJoinTree.cpp Resolved
…CESS_DENIED`

The check in `CREATE ROW POLICY` protected nothing: `ON db.*`, a policy
created before the table, and a table created after the policy all
bypassed it, while it broke `ALTER ROW POLICY` on an existing policy and
ran per node under `ON CLUSTER`. The query-time rejection in `JOIN` and
`joinGet` is the actual boundary, and persisted policies never went
through the interpreter anyway. The error code follows the `mergeTreeIndex`
fix in #115304.

Related: ClickHouse/clickhouse-private#70504

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@vdimir vdimir changed the title Skip the direct join when the right table has a row policy or additional_table_filters Skip the direct join when the right table has a row policy Sep 29, 2026
SELECT p.key, j.value FROM probe_join_rls AS p LEFT ANY JOIN join_rls AS j ON j.key = p.key ORDER BY p.key;
SELECT joinGet(join_rls, 'value', toUInt64(2)), joinGetOrNull(join_rls, 'value', toUInt64(3));

CREATE ROW POLICY join_rls_policy ON join_rls FOR SELECT USING value = 'a' TO CURRENT_USER;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The runtime guard moved from CREATE ROW POLICY to query time specifically to cover policies that reach the table without going through the interpreter, such as ON <db>.* or a policy that already exists before the Join table is created. This regression now only exercises CREATE ROW POLICY ... ON join_rls after the table exists, so it does not prove the newly-important path that motivated the change.

Please keep one negative case where the policy reaches the table indirectly and assert that both JOIN and joinGet still fail there. Otherwise a regression in policy matching for inherited or database-wide rules would bring the original bypass back while this test stays green.

@vdimir

vdimir commented Sep 29, 2026

Copy link
Copy Markdown
Member Author

Integration tests (amd_llvm_coverage, 1/8) FAIL
test_storage_iceberg_no_spark/test_iceberg_inverted_manifest_bounds.py::test_iceberg_narrow_manifest_bounds FAIL

@groeneai could you please check this one?

@groeneai

Copy link
Copy Markdown
Collaborator

Not related to this PR. The test fails on every master run since it was restored to master at 10:41 UTC today (e.g. the same job on master): a change merged while it was removed stopped writing the log line it greps for. The fix is #122895 (test-only, CI green).

const auto storage_id = storage_join->getStorageID();
context->checkAccess(AccessType::SELECT, storage_id, column_names);

/// The hash table is read as is, so a row policy on the table cannot be applied here any more than in a JOIN.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

joinGet and joinGetOrNull now fail with ACCESS_DENIED when a row policy applies, but the structured function docs in this file still only describe the happy path. Since those registrations generate the published function pages, users looking up the function won't see the new restriction even though the Join engine and CREATE ROW POLICY docs were updated. Please add a short note to both descriptions documenting the row-policy failure mode.

@vdimir
vdimir added this pull request to the merge queue Oct 1, 2026
Merged via the queue into master with commit b55548e Oct 1, 2026
150 checks passed
@vdimir
vdimir deleted the vdimir/direct-join-row-policy branch October 1, 2026 08:51
robot-ch-test-poll3 added a commit that referenced this pull request Oct 1, 2026
Cherry pick #121398 to 26.3: Skip the direct join when the right table has a row policy
robot-clickhouse added a commit that referenced this pull request Oct 1, 2026
robot-ch-test-poll3 added a commit that referenced this pull request Oct 1, 2026
Cherry pick #121398 to 26.7: Skip the direct join when the right table has a row policy
robot-clickhouse added a commit that referenced this pull request Oct 1, 2026
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 1, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 1, 2026
Backport #121398 to 26.8: Skip the direct join when the right table has a row policy
clickhouse-gh Bot added a commit that referenced this pull request Oct 1, 2026
Backport #121398 to 26.7: Skip the direct join when the right table has a row policy
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Oct 2, 2026
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Oct 2, 2026
PedroTadim added a commit that referenced this pull request Oct 2, 2026
Backport #121398 to 26.3: Skip the direct join when the right table has a row policy
@vdimir vdimir removed the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 2, 2026
@vdimir

vdimir commented Oct 2, 2026

Copy link
Copy Markdown
Member Author

@Ergus I think we need to backport the change in full, since it closes a vulnerability that allowed reading row policy-protected data, even though it will break queries that used to bypass this row policy.

@robot-clickhouse robot-clickhouse added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 2, 2026
@vdimir vdimir removed pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR labels Oct 7, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 7, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Oct 7, 2026
mu-soliman pushed a commit to mu-soliman/ClickHouse that referenced this pull request Oct 8, 2026
`typeid_cast` compares the exact dynamic type, so the check added in
ClickHouse#121398 matched `StorageJoin`
itself and nothing derived from it. `castStorage` is a `dynamic_pointer_cast`
that also looks through `StorageTableProxy`, and it is how the new analyzer,
`joinGet`, and the `setStorageJoin` branch a few lines below already detect
a `Join` table.

On master the legacy analyzer is not reachable from user queries
(`enable_analyzer = 0` is replaced with `1`), so this changes nothing
observable here. It matters for the release branches that still run the
legacy analyzer, where the backport of the same check otherwise joins a
storage derived from `StorageJoin` under a row policy as a filtered stream
instead of failing with `ACCESS_DENIED` like a `Join` table does.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
vdimir added a commit that referenced this pull request Oct 10, 2026
The resolved cherry-pick kept both sides of the conflict: the `Join` table check from
#124441 and the trimmed guard backported earlier from
#121398, with the old comment split between them.
Keep the check only and declare `ErrorCodes::ACCESS_DENIED`, which the file never had.

`castStorage` comes to 26.3 with #124513, which has
to be merged first; include `Storages/StorageProxy.h` for it as the other branches do.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
vdimir added a commit that referenced this pull request Oct 10, 2026
The earlier backport of #121398 kept only the legacy
analyzer guard. Add the remaining parts: `joinGet` and `joinGetOrNull` fail with `ACCESS_DENIED`
while a row policy applies to the `Join` table, the analyzer does the same in
`buildQueryPlanForJoinNode` instead of joining the table as a stream, the documentation says so,
and test 05234 covers it. Test 05233, which runs the legacy analyzer here, now expects
`ACCESS_DENIED` for the `Join` table as well.

Neither the statement nor the `Join` engine documentation lives in C++ on this branch, so both
pages are edited directly.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
vdimir added a commit that referenced this pull request Oct 10, 2026
With `query_plan_use_logical_join_step = 0` the analyzer still plans joins through
`buildJoinQueryPlan`, which picked a `Join` table or a key-value storage for the right side without
looking at its row policy, so the `FilterStep` of the right-side plan was dropped there as well.
Master has no such path any more, which is why
#121398 did not touch it. Treat it like
`buildQueryPlanForJoinNode`: `ACCESS_DENIED` for a `Join` table, a filtered stream for the rest.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
vdimir added a commit that referenced this pull request Oct 10, 2026
The resolved cherry-pick kept both sides of the conflict: the `Join` table check from
#124441 and the trimmed guard backported earlier from
#121398, with the old comment split between them.
Keep the check only and declare `ErrorCodes::ACCESS_DENIED`, which the file never had.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
vdimir added a commit that referenced this pull request Oct 10, 2026
The earlier backport of #121398 kept only the legacy
analyzer guard. Add the remaining parts: `joinGet` and `joinGetOrNull` fail with `ACCESS_DENIED`
while a row policy applies to the `Join` table, the analyzer does the same in
`buildQueryPlanForJoinNode` instead of joining the table as a stream, the documentation says so,
and test 05234 covers it. Test 05233, which runs the legacy analyzer here, now expects
`ACCESS_DENIED` for the `Join` table as well.

The statement documentation does not live in C++ on this branch, so the row policy page is edited
directly.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
vdimir added a commit that referenced this pull request Oct 10, 2026
The resolved cherry-pick kept both sides of the conflict: the `Join` table check from
#124441 and the trimmed guard backported earlier from
#121398, with the old comment split between them.
Keep the check only and declare `ErrorCodes::ACCESS_DENIED`, which the file never had.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
vdimir added a commit that referenced this pull request Oct 10, 2026
The earlier backport of #121398 kept only the legacy
analyzer guard. Add the remaining parts: `joinGet` and `joinGetOrNull` fail with `ACCESS_DENIED`
while a row policy applies to the `Join` table, the analyzer does the same in
`buildQueryPlanForJoinNode` instead of joining the table as a stream, the documentation says so,
and test 05234 covers it. Test 05233, which runs the legacy analyzer here, now expects
`ACCESS_DENIED` for the `Join` table as well.

The statement documentation does not live in C++ on this branch, so the row policy page is edited
directly.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
vdimir added a commit that referenced this pull request Oct 10, 2026
The earlier backport of #121398 to this branch was
reverted. Add it back on top of #124441: `joinGet`
and `joinGetOrNull` fail with `ACCESS_DENIED` while a row policy applies to the `Join` table,
the analyzer does the same in `buildQueryPlanForJoinNode` instead of joining the table as a
stream, the documentation says so, and tests 05233 and 05234 cover it.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-joins JOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla... 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.

10 participants