Skip to content

Require SET DEFINER to rewrite the body of a view that runs as another user - #121629

Merged
CheSema merged 2 commits into
masterfrom
alter-modify-query-access-checks
Oct 2, 2026
Merged

CheSema merged 2 commits into
masterfrom
alter-modify-query-access-checks

Conversation

@CheSema

@CheSema CheSema commented Sep 22, 2026 •

Copy link
Copy Markdown
Member

ALTER TABLE <view> MODIFY QUERY replaces the body that a SQL SECURITY DEFINER view runs with its definer's privileges, or that a SQL SECURITY NONE view runs without any checks. It required only ALTER VIEW MODIFY QUERY, which ALTER TABLE implies. So a user with that grant could make a view owned by another user read what that user can read, and then read the result from the view's target table. Since #117979, a table that the user cannot see at all is refused on the local path. But a grant on some columns, ON CLUSTER, or a Replicated database still let such a body through, and so does a row policy.

Rewriting the body now needs the grants that CREATE and MODIFY SQL SECURITY demand for that SQL security: SET DEFINER on the definer, or ALLOW SQL SECURITY NONE. The initiator checks them, so they also apply to ON CLUSTER and to Replicated databases. An ON CLUSTER MODIFY QUERY for a view that the initiator does not have is refused, because the hosts that have it apply the entry without the user. When the same statement has a MODIFY SQL SECURITY, its new security is what gets authorized.

The checks of the body itself, to match CREATE, are in #123063.

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

Rewriting the body of a view with ALTER TABLE ... MODIFY QUERY now needs the same grants as creating it with its SQL security: SET DEFINER on the definer of a SQL SECURITY DEFINER view, or ALLOW SQL SECURITY NONE for a SQL SECURITY NONE view. An ON CLUSTER MODIFY QUERY fails with UNKNOWN_TABLE unless it runs on a host that has the view.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.10.1.1290-master (included in 26.10 and later)
  • Backported to: 26.9.12.8, 26.8.19.10, 26.7.24.8

@clickhouse-gh

clickhouse-gh Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [9cdc084]

Summary: ✅


AI Review

Summary

This PR hardens ALTER TABLE ... MODIFY QUERY by requiring the same SQL-security grants as CREATE / MODIFY SQL SECURITY, adds focused tests for foreign and ephemeral definers, and documents the new privileges. The local-path fix is good, but the distributed path still trusts only the initiator's local metadata, so a host that still stores the view as SQL SECURITY DEFINER or NONE can accept the rewritten body without the corresponding SET DEFINER / ALLOW SQL SECURITY NONE grant. Because that is still an RBAC bypass in the exact area this PR claims to close, I cannot approve it yet.

Findings
  • ❌ Blockers
    • [src/Interpreters/InterpreterAlterQuery.cpp:507] ON CLUSTER now promises that rewriting a view body needs the same authority as creating that body under the view's SQL security, but params.access_to_check = getRequiredAccess(table) derives that extra requirement only from the initiator's local metadata. If host A sees SQL SECURITY INVOKER while host B still stores the same view as SQL SECURITY DEFINER or NONE, the initiator accepts the ALTER without SET DEFINER / ALLOW SQL SECURITY NONE and host B later stores the new body under stronger privileges. ON CLUSTER is exactly the mechanism used to reconcile this kind of metadata skew, so the distributed RBAC invariant is still broken. Suggested fix: either fail closed whenever the executing hosts may authorize from different metadata, or re-authorize MODIFY QUERY on each worker against that worker's own stored SQL security before the body is written.
  • ⚠️ Majors
    • [src/Interpreters/InterpreterAlterQuery.cpp:708] [dismissed by author -- https://github.com/ClickHouse/ClickHouse/pull/121629#discussion_r4144502438] The new SET DEFINER / ALLOW SQL SECURITY NONE requirement is still computed from a metadata snapshot taken before any alter / DDL lock is held. A concurrent MODIFY SQL SECURITY can therefore change the final execution security after checkAccess passes but before table->alter stores the new body, so the stored definition again escapes the grant that is supposed to protect it. This needs a cooperating concurrent session, but it still violates the PR's contract that rewriting the body requires the grants of the SQL security the body will finally run under. Suggested fix: revalidate the final SQL security from a snapshot captured under the alter lock, or move this authorization into the storage alter path where the final metadata is known.
Final Verdict
  • Status: ❌ Block
  • Minimum required actions:
    • Close the distributed-metadata hole for ON CLUSTER so every host that stores the rewritten body is authorized against the SQL security it actually has.
    • Revalidate MODIFY QUERY SQL-security authorization against the final locked metadata, or otherwise make the local race impossible.

LLVM Coverage Report

Measured on commit 9cdc084.

Metric Baseline Current Δ
Lines 88.30% 88.30% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 79.70% 79.70% +0.00%

Changed lines: Changed C/C++ lines covered: 62/64 (96.88%) · 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 22, 2026
Comment thread src/Planner/PlannerJoinTree.cpp Resolved
@clickhouse-gh clickhouse-gh Bot added the comp-rbac Authorization: roles, grants, quotas, row-level security, access checks. label Sep 22, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 9cdc0845a with master f76d7abfb (stripped binary size, per-symbol sizes and ThinLTO time; 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

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

Job report

if (!table)
throw Exception(ErrorCodes::UNKNOWN_TABLE,
"Table {}.{} does not exist on this host. `ALTER TABLE ... ON CLUSTER ... MODIFY QUERY` is authorized "
"on the initiator, so it must be run from a host that has the view",

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.

If host A and host B have different stored metadata for the same view, this initiator-side check is still not enough. Concrete trace: db.mv is SQL SECURITY INVOKER on A but DEFINER = alice (or NONE) on B, the caller has ALTER VIEW MODIFY QUERY but no SET DEFINER on alice, and distributed_ddl_use_initial_user_and_roles = 0; the check here passes on A, then B rewrites its DEFINER/NONE copy without ever re-checking as the initiator. ON CLUSTER is exactly how skewed metadata gets reconciled, so this still needs a fail-closed guard or worker-side execution under the initiator's user.

Comment thread src/Planner/PlannerJoinTree.cpp Resolved
@CheSema CheSema changed the title Fix access checks for ALTER TABLE ... MODIFY QUERY on views Require SET DEFINER to rewrite the body of a view that runs as another user Sep 30, 2026
@CheSema
CheSema force-pushed the alter-modify-query-access-checks branch from e75ad03 to 919ac1a Compare September 30, 2026 12:40
@CheSema
CheSema marked this pull request as ready for review September 30, 2026 16:48
…uns as another user

`ALTER TABLE ... MODIFY QUERY` needed only `ALTER VIEW MODIFY QUERY`, so it
could make a `SQL SECURITY DEFINER` view read anything its definer can read.
It now needs `SET DEFINER` on the definer, or `ALLOW SQL SECURITY NONE` for a
`SQL SECURITY NONE` view. The initiator checks this, so it also holds for
`ON CLUSTER` and for `Replicated` databases, and an `ON CLUSTER` statement is
refused on a host that does not have the view. A `MODIFY SQL SECURITY` in the
same statement is authorized instead of the stored security.
@CheSema
CheSema force-pushed the alter-modify-query-access-checks branch from 919ac1a to 2e49135 Compare October 1, 2026 10:14
@CheSema
CheSema requested review from pufit and a balanced review from Copilot October 1, 2026 10:53

Copilot AI left a comment

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.

Note

Copilot was unable to run its full agentic suite in this review.

Copilot review overview

Review effort: Lite
Findings: 1 High severity · 1 Medium severity

Open (2)
What changed in this PR

Tightens authorization for ALTER TABLE <view> MODIFY QUERY so rewriting a view body that runs with SQL SECURITY DEFINER / SQL SECURITY NONE requires the same privileges as creating/modifying that SQL security, preventing privilege escalation.

Changes:

  • Add access checks for MODIFY QUERY based on the view’s stored SQL security (SET DEFINER / ALLOW SQL SECURITY NONE).
  • Reject ALTER ... ON CLUSTER ... MODIFY QUERY on the initiator when the view doesn’t exist locally (to ensure initiator-side authorization is correct).
  • Add stateless tests covering foreign definers, SQL SECURITY NONE, row-policy cases, and ephemeral definers.
File Description
tests/​queries/​0_stateless/​05296_alter_view_modify_query_ephemeral_definer.sh New test covering ephemeral-definer grant semantics for MODIFY QUERY.
tests/​queries/​0_stateless/​05296_alter_view_modify_query_ephemeral_definer.reference Expected output for the ephemeral-definer test.
tests/​queries/​0_stateless/​05237_alter_view_modify_query_access.sh New broader access-control regression tests for MODIFY QUERY under different SQL security modes and ON CLUSTER.
tests/​queries/​0_stateless/​05237_alter_view_modify_query_access.reference Expected output for the access-control regression test.
src/​Interpreters/​InterpreterAlterQuery.h Declares helper to add required privileges based on stored SQL security during MODIFY QUERY.
src/​Interpreters/​InterpreterAlterQuery.cpp Implements SQL-security-based access requirements for MODIFY QUERY and initiator-side ON CLUSTER existence check.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +498 to +504
/// The hosts run the entry without the user, unless `distributed_ddl_use_initial_user_and_roles` is on,
/// so the view's SQL security is authorized here, which needs the view on this host.
if (modify_query && !table)
throw Exception(ErrorCodes::UNKNOWN_TABLE,
"Table {}.{} does not exist on this host. `ALTER TABLE ... ON CLUSTER ... MODIFY QUERY` is authorized "
"on the initiator, so it must be run from a host that has the view",
backQuoteIfNeed(alter.getDatabase()), backQuoteIfNeed(alter.getTable()));
Comment on lines 742 to +758
const auto row_exists_column_kind = getRowExistsColumnKind(storage, getContext());
/// A `MODIFY SQL SECURITY` in the same statement decides what the new body will execute as, and
/// `processSQLSecurityOption` has already authorized exactly that. The stored security then says
/// nothing about the body being written, so it must not add a requirement of its own.
const bool sql_security_is_being_replaced = std::ranges::any_of(
alter.command_list->children,
[](const auto & child) { return child->template as<ASTAlterCommand &>().sql_security != nullptr; });

for (const auto & child : alter.command_list->children)
{
const auto & command = child->as<ASTAlterCommand &>();
required_access.append_range(
getRequiredAccessForCommand(child->as<ASTAlterCommand&>(), alter.getDatabase(), alter.getTable(), row_exists_column_kind));
getRequiredAccessForCommand(command, alter.getDatabase(), alter.getTable(), row_exists_column_kind));

if (command.type == ASTAlterCommand::MODIFY_QUERY && !sql_security_is_being_replaced)
addRequiredAccessForModifyQuerySQLSecurity(required_access, storage);
}
@CheSema
CheSema added this pull request to the merge queue Oct 2, 2026
Merged via the queue into master with commit 7a85761 Oct 2, 2026
149 checks passed
@CheSema
CheSema deleted the alter-modify-query-access-checks branch October 2, 2026 14:48
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 2, 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 2, 2026
robot-clickhouse-ci-1 added a commit that referenced this pull request Oct 6, 2026
Cherry pick #121629 to 26.9: Require SET DEFINER to rewrite the body of a view that runs as another user
robot-clickhouse added a commit that referenced this pull request Oct 6, 2026
robot-ch-test-poll2 added a commit that referenced this pull request Oct 6, 2026
Cherry pick #121629 to 26.3: Require SET DEFINER to rewrite the body of a view that runs as another user
robot-clickhouse added a commit that referenced this pull request Oct 6, 2026
robot-ch-test-poll2 added a commit that referenced this pull request Oct 6, 2026
Cherry pick #121629 to 26.7: Require SET DEFINER to rewrite the body of a view that runs as another user
robot-clickhouse added a commit that referenced this pull request Oct 6, 2026
robot-ch-test-poll2 added a commit that referenced this pull request Oct 6, 2026
Cherry pick #121629 to 26.8: Require SET DEFINER to rewrite the body of a view that runs as another user
robot-clickhouse added a commit that referenced this pull request Oct 6, 2026
@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 6, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 6, 2026
Backport #121629 to 26.9: Require SET DEFINER to rewrite the body of a view that runs as another user
clickhouse-gh Bot added a commit that referenced this pull request Oct 6, 2026
Backport #121629 to 26.8: Require SET DEFINER to rewrite the body of a view that runs as another user
CheSema added a commit that referenced this pull request Oct 7, 2026
Backport #121629 to 26.7: Require SET DEFINER to rewrite the body of a view that runs as another user
@CheSema

CheSema commented Oct 7, 2026

Copy link
Copy Markdown
Member Author

26.3 in public has different code, the fix does not work there, so I would not backport it, there is a risk that I could break other things.

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.

5 participants