Skip to content

Make Row Policies tolerate ALTER COLUMN - #121944

Open
yariks5s wants to merge 73 commits into
masterfrom
yarik/row-policy-alter-column
Open

yariks5s wants to merge 73 commits into
masterfrom
yarik/row-policy-alter-column

Conversation

@yariks5s

@yariks5s yariks5s commented Sep 24, 2026 •

Copy link
Copy Markdown
Member

Related: #108478
Related: #118668

A row policy names the columns of its table by name and is stored outside the table's metadata, so ALTER TABLE did not see it. Renaming or dropping a column a policy mentions succeeded and left the policy unresolvable: every later read of the table by a user the policy applies to failed with UNKNOWN_IDENTIFIER. Fail-closed, but with no warning at the ALTER and no way back short of recreating the policy.

This follows what PostgreSQL does, which refuses only the changes it cannot keep consistent: ALTER TABLE ... DROP COLUMN and ALTER TABLE ... RENAME COLUMN refuse changes to columns referenced by row policies on the table or on related Alias tables. Change or drop the affected policies first. ALTER TABLE ... MODIFY COLUMN is refused only when the policy could no longer read the column after the change: the new type lacks a subcolumn the policy reads (ALTER_OF_COLUMN_IS_FORBIDDEN), or the filter cannot be analysed with the new type. CLEAR COLUMN and the other alteration commands are unaffected: the column stays and the filter still resolves. Validation uses the local policies on the initiating server; replicas replay committed metadata changes without this check, so policies must be kept consistent across replicas.

Changelog category (leave one):

  • Bug Fix

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

ALTER TABLE ... DROP COLUMN and ALTER TABLE ... RENAME COLUMN now reject changes to columns referenced by row policies, including policies on related Alias tables, instead of leaving those policies unresolvable. ALTER TABLE ... MODIFY COLUMN is rejected when the policy could no longer read the column after the change. Change or drop affected policies before altering their columns. Replicated changes are checked on the initiating server.


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

@clickhouse-gh

clickhouse-gh Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a6071c3]

Summary: ❌

job_name test_name status info comment
Finish Workflow FAIL
python3 ./ci/jobs/scripts/workflow_hooks/can_be_merged.py --review-threads FAIL

AI Review

Summary

The PR prevents column alterations from leaving local row policies unreadable, including policies on related Alias tables, and validates affected types before committing changes. One access-control blocker remains: ordinary ON CLUSTER execution can evaluate policy functions with server privileges instead of the initiating user's grants.

Findings

❌ Blockers

  • src/Interpreters/Access/RowPolicyAlterColumns.cpp:224 — ON CLUSTER still bypasses source grants during policy analysis. On an Atomic database with the default distributed_ddl_use_initial_user_and_roles = 0, the cluster branch returns before the new run_as_submitting_user handling, which applies only to the Replicated database path. The distributed DDL worker copies the global context without installing the initiating user. It is not a secondary metadata replay, so this check runs with full access. A user with row-policy, column-DDL, and cluster grants can therefore trigger a persisted file call or schema-inferring url subquery without the corresponding source grants. Analysis can read files through constant folding or make HTTP requests through schema inference. Preserve the initiating identity, or refuse consequential policy analysis when that identity is unavailable. This is the remaining entrypoint of the existing security finding; its thread was reopened with the current code trace.
Tests
  • ⚠️ Add a stateless regression using the existing localhost cluster: a user without source grants must not trigger file reads or URL schema inference through an unrelated ALTER TABLE ... ON CLUSTER. The current Fast test report passes 05860_row_policy_alter_column_access, but that test exercises local ALTER statements only. The cluster failure path above was established by code inspection; no local server was run.
Final Verdict

Status: ❌ Block

Preserve source authorization on the ordinary distributed DDL path, or reject unsafe analysis there, and add the focused regression.

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Sep 24, 2026
@yariks5s
yariks5s requested review from pufit and vitlibar September 24, 2026 10:33
@clickhouse-gh clickhouse-gh Bot added the comp-rbac Authorization: roles, grants, quotas, row-level security, access checks. label Sep 24, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing a6071c3b3 with master ba1cd6eb7 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 4123da939; compile times per translation unit against the most recent warmup build that recompiled it).

⚠️ Significant changes: object file sizes.

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.

Object file sizes ⚠️

152 object files changed (+361.80 KiB total), 2 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Interpreters/Access/RowPolicyAlterColumns.cpp.o new 260.18 KiB +260.18 KiB
src/CMakeFiles/dbms.dir/Storages/expressionSourceColumns.cpp.o new 95.25 KiB +95.25 KiB

716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

1355 translation units recompiled, 6605 s compile time in total, 1353 of them have a recent master baseline.

Translation units without a recent master baseline:

  • src/Interpreters/Access/RowPolicyAlterColumns.cpp: 5.0 s

Job report

… reads next to a scalar subquery, because the check only analyzes such a filter
… a row policy with a scalar subquery reads
StorageSnapshotPtr new_storage_snapshot;

/// binding the lookup tables of a policy needs no grant of the user who alters the table: a copy of the global context has full access
auto analysis_context = Context::createCopy(context->getGlobalContext());

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.

[Blocker] Using a copy of the global context here turns the ALTER-side dependency check into privileged table-function execution.

columnsReadByFilter and checkFilterAfterAlter both run the persisted policy through QueryAnalyzer(/*only_analyze=*/ true). That still resolves table functions via executeTableFunction, and ITableFunction::execute enforces source/engine access on the context it is given. Because this code swaps in context->getGlobalContext(), a user who has CREATE/ALTER ROW POLICY plus ALTER TABLE on t can store a filter such as USING id IN (SELECT x FROM file('/etc/passwd', 'LineAsString', 'x String')) or url(...), and then ALTER TABLE t DROP COLUMN extra will analyze that subquery with full server access.

CREATE/ALTER ROW POLICY does not require FILE/S3/URL source grants, and create-time validation currently only rejects arrayJoin / column matchers, so this is a new privilege-escalation / SSRF / local-file-read surface. The dependency-analysis context needs to bypass only table-metadata visibility for lookup tables, not table-function source/engine checks; alternatively, reject table functions in this ALTER-side analysis and fail conservatively.

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.

🕵 Fixed in 8287723. The check no longer analyzes a policy with a copy of the global context. It uses a copy of the context of the user who runs the ALTER, so the access checks of a read apply: a table function needs its source grant and CREATE TEMPORARY TABLE, dictGet needs the dictGet grant, and the function file needs READ ON FILE. The analysis executes a scalar subquery in the arguments of a table function, and this subquery needs SELECT on its tables, as in a read. If the user does not have these grants, the ALTER fails with ACCESS_DENIED.

Only the tables that a policy reads need no grant, which keeps the case of the earlier review (a user with only ALTER grants and a lookup table that this user cannot read). A new context flag, analyze_tables_without_grants, which only this check sets, turns off two checks: IdentifierResolver::checkAccessToTableMetadata, and the SELECT check of the Planner in a plan that is only analyzed (only_analyze, for example the sample block of a scalar subquery). Such a plan reads no data. A plan that runs (a scalar subquery in table function arguments, a materialized CTE) keeps the SELECT check.

The new test 05860_row_policy_alter_column_access has three policies: one that reads file(...) as a table function, one that calls the function file, and one with a scalar subquery in the arguments of file. On the previous head, a user with only ALTER DROP COLUMN dropped an unrelated column of each table. Now each ALTER fails with ACCESS_DENIED until the user gets the grants: CREATE TEMPORARY TABLE and READ ON FILE, and for the third policy also SELECT on the table of the subquery. The test also checks that MODIFY COLUMN of a column that a policy with a lookup table reads needs no grant on the lookup table. 05839_row_policy_alter_column_unrelated still passes (a scalar subquery over a lookup table that the user cannot read).

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.

[Blocker] The source-grant fix is still bypassed by ALTER TABLE ... ON CLUSTER on an Atomic database.

With the default distributed_ddl_use_initial_user_and_roles = 0, InterpreterAlterQuery::executeToTable returns through executeDDLQueryOnCluster at src/Interpreters/InterpreterAlterQuery.cpp:703, before the new run_as_submitting_user handling at lines 729–741. That handling protects the initiator of a Replicated database; it does not preserve the user for the ordinary distributed DDL worker.

The ordinary worker copies the global context, and DDLTaskBase::makeQueryContext installs no user when identity preservation is disabled (src/Interpreters/DDLTask.cpp:399–431). Such an ON CLUSTER task is not a secondary replicated-metadata replay, so it reaches src/Interpreters/Access/RowPolicyAlterColumns.cpp:224 with full access. Copying that context therefore still analyzes the persisted filter with server privileges. only_analyze does not prevent constant folding of the scalar file function or schema inference by the url table function.

For example, a user with row-policy, column-DDL, and cluster grants, but no READ ON FILE / READ ON URL, can store a policy calling file('secret.tsv') or a schema-inferring url subquery, then trigger its evaluation with an unrelated DROP COLUMN ... ON CLUSTER. The local equivalent is denied by the new test, while the worker checks the source grants against its full-access context.

Please preserve the initiating identity for this path as well, or fail closed before evaluating policy functions when that identity is unavailable. A stateless regression using the existing localhost cluster and a user without source grants would cover the missing entrypoint. This conclusion is from tracing the current code; no local server was run in this CI review.

…he user, so that a table function or a function such as `file` keeps its access check; only the tables of a policy need no grant, because the analysis reads no data
…he user for table functions, the function `file` and executed subqueries, but not for the tables of a policy
… replica analyzes a row policy in its own current database, which does not have the lookup tables
…k after `MODIFY COLUMN`, because a scalar subquery of an `Array`, `Tuple` or `LowCardinality` type became `__getScalar`, which `readsScalarSubquery` did not see
…a row policy reads, because the alias expression can stop evaluating with the new type
…refused when a row policy that reads the alias cannot evaluate it
… COLUMN` in a `Replicated` database as the submitting user, so the row policy check keeps the grants of the user
…ts own database and not in the current database of the session
…ter-column

# Conflicts:
#	src/Interpreters/InterpreterAlterQuery.cpp
…ter-column

# Conflicts:
#	tests/integration/test_replicated_database/test.py

This branch has not been deployed

No deployments
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-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant