Repository navigation
Skip the direct join when the right table has a row policy - #121398
Conversation
…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]>
|
Workflow [PR], commit [60a2297] Summary: ✅
AI ReviewSummaryThis PR closes the row-policy bypass on direct joins by forcing key-value and dictionary tables back through their filtered plan and by rejecting Missing context / blind spots
Findings
💡 Nits
Final Verdict
LLVM Coverage ReportMeasured on commit 60a2297.
Changed lines: Changed C/C++ lines covered: 42/46 (91.30%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units11 translation units recompiled, 52 s compile time in total, 11 of them have a recent master baseline. |
`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]>
Ergus
left a comment
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| 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; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
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]>
…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]>
| 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; |
There was a problem hiding this comment.
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.
@groeneai could you please check this one? |
|
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. |
There was a problem hiding this comment.
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.
Cherry pick #121398 to 26.3: Skip the direct join when the right table has a row policy
Cherry pick #121398 to 26.7: Skip the direct join when the right table has a row policy
Backport #121398 to 26.8: Skip the direct join when the right table has a row policy
Backport #121398 to 26.7: Skip the direct join when the right table has a row policy
…ht table has a row policy
… the right table has a row policy"
Backport #121398 to 26.3: Skip the direct join when the right table has a row policy
|
@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. |
`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]>
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]>
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]>
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]>
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]>
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]>
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]>
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]>
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]>
A direct join with an
EmbeddedRocksDB,KeeperMap,Redisor dictionary table, or a join with aJointable, replaces the right-side query plan with a lookup into the storage. That plan is the only thing carrying theFilterStepfor the table's row policy, so the policy was lost.used_row_policies.empty()guard inbuildQueryPlanForJoinNode; theJointable case is handled below.JoinedTables::makeTableJoin) there was no guard: withenable_analyzer = 0, a user holding onlySELECTon such a table read the rows its row policy hides through aJOIN. 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 acceptenable_analyzer = 0.Jointable is a prebuilt hash table read as is byJOINandjoinGet, so its rows cannot be filtered there. While a row policy applies to such a table,JOINagainst it andjoinGet/joinGetOrNullfail withACCESS_DENIED, following themergeTreeIndexfix in Do not bypass row policies in the mergeTreeIndex table function #115304. A plainSELECTfrom the table is filtered as before. Creating the policy is still allowed, so existing policies,ALTER ROW POLICYandON CLUSTERkeep working. TheJoinengine andCREATE ROW POLICYdocumentation now say so.additional_table_filtersis 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):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a row policy bypass: a direct
JOINwith anEmbeddedRocksDB,KeeperMaporRedistable in the legacy analyzer (enable_analyzer = 0) ignored the row policy of the right table and returned hidden rows, andjoinGeton aJointable ignored its row policy in both analyzers. Such a table is now joined through its query plan, which keeps the filter, andJOINorjoinGetagainst aJointable under a row policy throwsACCESS_DENIED.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/121398]
Version info
26.10.1.1148-master(included in26.10and later)26.9.8.7,26.8.15.26,26.7.20.9,26.3.39.8