Skip to content

Require DROP DATABASE for TRUNCATE of a database over ON CLUSTER - #118155

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:truncate-database-on-cluster-access
Sep 5, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:truncate-database-on-cluster-access

Conversation

@groeneai

@groeneai groeneai commented Sep 4, 2026 •

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

TRUNCATE DATABASE ... ON CLUSTER and TRUNCATE [ALL] TABLES FROM ... ON CLUSTER ran without checking any privilege: a user holding only SELECT and CLUSTER could destroy every table in a database, and readonly = 1 did not prevent it. Both now require DROP DATABASE, the privilege the non-cluster spellings have always required.

Description

Related: #114011

InterpreterDropQuery::getRequiredAccessForDDLOnCluster handled only Kind::Detach and Kind::Drop in its !drop.table branch, with no terminal else, so Kind::Truncate returned an empty AccessRightsElements and executeDDLQueryOnCluster checked only AccessType::CLUSTER before queueing the DDL.

CLUSTER is declared GLOBAL and is in neither not_readonly_flags nor ddl_flags, so that one empty list removed three gates: the privilege check, readonly and allow_ddl. The blast radius is DROP-equivalent rather than TRUNCATE-equivalent, because executeToDatabaseImpl maps a bare TRUNCATE DATABASE to Kind::Drop per table. Present in released stable; probed at v26.8.2.7-lts.

The branch is now unconditional rather than gaining a fourth else if: the local path authorizes all three kinds with one DROP DATABASE check, AccessType.h documents the privilege as allowing {DROP|DETACH|TRUNCATE} DATABASE, and a future kind now over-requires and fails closed.

Measured on a server with real users: all six spellings reaching the branch are refused without the privilege and allowed with it; no other arm changes behaviour.

A query that succeeds today starts failing; that is the fix: the non-cluster spelling has always required DROP DATABASE, so anyone relying on the ON CLUSTER form relies on this bug.

Left alone deliberately: the is_dictionary arm still handles only Detach and Drop, but TRUNCATE DICTIONARY is refused on every host before any access check, leaving only a doomed DDL-queue entry. KILL TRANSACTION, KILL PART_MOVE_TO_SHARD, SYSTEM {START,STOP} THREAD FUZZER and SYSTEM RESET COVERAGE skip their ON CLUSTER check the same way.

Not closed here: the local database path also checks each object after the database check, while the ON CLUSTER worker runs as the initiator only when distributed_ddl_use_initial_user_and_roles is enabled (default off). Identical for DROP DATABASE and DETACH DATABASE ... ON CLUSTER, which this PR does not touch, so this narrows the reachable set rather than closing that gap; converging needs the worker to run as the initiator.


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

Version info

  • Merged into: 26.9.1.814 (included in 26.9 and later)
  • Backported to: 26.8.3.50, 26.7.7.36, 26.6.5.62, 26.3.33.36

`InterpreterDropQuery::getRequiredAccessForDDLOnCluster` handled only
`Kind::Detach` and `Kind::Drop` in its `!drop.table` branch. The chain had no
terminal `else`, so for `Kind::Truncate` the function returned an empty
`AccessRightsElements`; `executeDDLQueryOnCluster` then checked only
`AccessType::CLUSTER` and queued the DDL.

`CLUSTER` is declared `GLOBAL`, outside both `not_readonly_flags` and
`ddl_flags`, so that one empty list removed three gates at once: the privilege
check, `readonly` and `allow_ddl`. A user holding only `SELECT` and `CLUSTER`
could destroy every table in a database with
`TRUNCATE DATABASE db ON CLUSTER c`, and `readonly = 1` did not stop it. The
blast radius is `DROP`-equivalent rather than `TRUNCATE`-equivalent, because
`executeToDatabaseImpl` maps a bare `TRUNCATE DATABASE` to `Kind::Drop` per
table.

The branch is now unconditional rather than gaining a fourth `else if`. All
three kinds are authorized by `DROP DATABASE` on the local path, in one
unconditional check, and `AccessType.h` documents the privilege as allowing
`{DROP|DETACH|TRUNCATE} DATABASE`, so the branch has no per-kind content to
express. This also removes the chain-without-a-terminal-`else` shape that
produced the omission: a future kind would over-require `DROP DATABASE`, which
fails closed.

Six statement spellings reach the branch: `TRUNCATE DATABASE`,
`TRUNCATE DATABASE IF EXISTS`, `TRUNCATE [ALL] TABLES FROM` and the `LIKE`,
`NOT LIKE` and `ILIKE` variants of the last. All six were measured refused
without the privilege and allowed with it. `DROP DATABASE`, `DETACH DATABASE`
and the table `TRUNCATE` arms are unchanged, as is the `is_dictionary` arm,
whose local and `ON CLUSTER` paths both refuse `TRUNCATE` before any access
check.

Co-Authored-By: Claude Opus 5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-self PR origin: self-found defect (impact-bar validated) labels Sep 4, 2026
@groeneai

groeneai commented Sep 4, 2026

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 2 findings) plus my own cold pass.

# Sev Finding Verdict Evidence / action
1 ⚠️ Test should also cover allow_ddl = 0, and a successful owner TRUNCATE ALL TABLES FROM DISAGREE The changed branch's only predicate is !drop.table; it never reads has_all, has_tables, has_like, not_like or case_insensitive_like, the five fields that distinguish the two spellings. So no requirement expressible there can apply to one and not the other, and the existing owner-allowed row is the over-requirement catcher for both. For allow_ddl: the two denial rows grep the privilege NAME, so any change to which flag this branch emits reddens them; given the flag is DROP_DATABASE, all three gates follow from ContextAccess.cpp's table_ddl set, in a file this PR does not touch. The allowed direction was measured for all six spellings on both arms.
2 💡 Category should be Critical Bug Fix, not Bug Fix DISAGREE Premise granted. I do not select the critical category on my own judgment; if a maintainer wants it, say so and I will change it in one push.

My own cold pass produced no blocker or major. It found four nits, three of which I fixed in the
PR description before publishing (name both affected spellings in the changelog entry; disclose the
is_dictionary arm as deliberately left alone rather than only "unchanged"; cut two clauses that
were investigation log). One nit is worth recording: after this fix the ON CLUSTER path is still
weaker than the local one, because the local database path additionally performs per-table checks
while the ON CLUSTER worker runs elevated unless distributed_ddl_use_initial_user_and_roles is
set. That asymmetry is pre-existing and identical for DROP DATABASE ON CLUSTER and
DETACH DATABASE ON CLUSTER; a database-level check is the only thing an initiator can express,
because the table list differs per host.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal
per finding.

@clickhouse-gh clickhouse-gh Bot closed this Sep 4, 2026
@clickhouse-gh clickhouse-gh Bot reopened this Sep 4, 2026
@groeneai

groeneai commented Sep 4, 2026

Copy link
Copy Markdown
Collaborator Author

cc @pufit @evillique, could you review this? The !drop.table branch of InterpreterDropQuery::getRequiredAccessForDDLOnCluster handled only Kind::Detach and Kind::Drop with no terminal else, so TRUNCATE DATABASE ... ON CLUSTER returned an empty AccessRightsElements and executeDDLQueryOnCluster checked only AccessType::CLUSTER before queueing the DDL, which left the privilege check, readonly and allow_ddl all unenforced.

@clickhouse-gh

clickhouse-gh Bot commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f00130f]

Summary: ✅


AI Review

Summary

This PR closes the empty-access-list hole for TRUNCATE ... ON CLUSTER, but the new unconditional DROP DATABASE check still does not match what the local database-truncate path actually authorizes. Both TRUNCATE DATABASE and TRUNCATE [ALL] TABLES FROM still fan out into per-object DROP_* / TRUNCATE checks locally, while the distributed initiator now validates only DROP DATABASE before queueing the work. Because the worker rebuilds its context from DDLWorker and only switches to the initiator user when distributed_ddl_use_initial_user_and_roles is enabled, the default ON CLUSTER path remains weaker than the non-cluster spelling. Changes requested.

PR Metadata
  • Changelog category should be Critical Bug Fix (crash, data loss, RBAC), not Bug Fix, because this is an RBAC bypass in a stable release.
  • Changelog entry is required for that category, and the current entry is specific and user-readable. Keep it after changing the category.
Findings

❌ Blockers

  • [src/Interpreters/InterpreterDropQuery.cpp:853-857] The new !drop.table branch assumes executeToDatabaseImpl is authorized by DROP DATABASE alone, but local execution still fans out into per-object checks: bare TRUNCATE DATABASE rewrites each member object to Kind::Drop at src/Interpreters/InterpreterDropQuery.cpp:505-506, and TRUNCATE [ALL] TABLES FROM still checks TRUNCATE on each table at src/Interpreters/InterpreterDropQuery.cpp:324. executeDDLQueryOnCluster therefore queues the DDL after checking only DROP DATABASE, while the worker reuses the DDLWorker context unless distributed_ddl_use_initial_user_and_roles is enabled (src/Interpreters/DDLWorker.cpp:575, src/Interpreters/DDLTask.cpp:332-353, default false in src/Core/ServerSettings.cpp:1171). In the default configuration a user with CLUSTER plus DROP DATABASE ON db.*, but without per-object DROP_* / TRUNCATE, can still destroy tables through ... ON CLUSTER that the local spelling would reject. Suggested fix: preserve the per-object access elements for the database fan-out cases, or explicitly reject those ON CLUSTER forms until the worker always executes with the initiator's effective user and roles.
Final Verdict

❌ Changes requested.

LLVM Coverage Report

Measured on commit f00130f.

Metric Baseline Current Δ
Lines 88.70% 88.70% +0.00%
Functions 92.10% 92.10% +0.00%
Branches 81.10% 81.10% +0.00%

Changed lines: Changed C/C++ lines covered: 3/3 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 4, 2026
@pufit pufit self-assigned this Sep 4, 2026
Comment on lines +855 to +857
/// `DROP`, `DETACH` and `TRUNCATE` of a database are all authorized by `DROP DATABASE`,
/// which is the single privilege `executeToDatabaseImpl` requires for each of them.
required_access.emplace_back(AccessType::DROP_DATABASE, drop.getDatabase());

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.

!drop.table also covers both TRUNCATE DATABASE and TRUNCATE [ALL] TABLES FROM, but the local path still does more than a single DROP DATABASE check here. executeToDatabaseImpl rewrites bare TRUNCATE DATABASE to per-table Kind::Drop at src/Interpreters/InterpreterDropQuery.cpp:505-506, and executeToTableImpl then checks DROP_TABLE / DROP_VIEW / DROP_DICTIONARY on each object at src/Interpreters/InterpreterDropQuery.cpp:342-343; the TABLES FROM spelling keeps Kind::Truncate and still checks per-table TRUNCATE at src/Interpreters/InterpreterDropQuery.cpp:324.

executeDDLQueryOnCluster now queues the distributed DDL after checking only DROP DATABASE, but the worker rebuilds its context from DDLWorker and only switches to the initiator user when distributed_ddl_use_initial_user_and_roles is enabled (src/Interpreters/DDLWorker.cpp:575, src/Interpreters/DDLTask.cpp:332-353, default false in src/Core/ServerSettings.cpp:1171). In the default configuration this still lets a user with CLUSTER + DROP DATABASE ON db.*, but without per-object DROP_* / TRUNCATE, destroy tables through ... ON CLUSTER that the non-cluster spelling would reject. I think this needs either the per-object access elements on the initiator side, or an explicit refusal of the database fan-out forms until the worker always executes with the initiator's effective user/roles.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Severity: major, and every mechanism claim here checks out at f00130f1. I verified them at the sites rather than from the description: :481, :505 (a bare TRUNCATE DATABASE becomes per-table Kind::Drop, while TRUNCATE [ALL] TABLES FROM keeps Kind::Truncate), :324 and :342; no_access_check is set only in InterpreterCreateQuery.cpp:2990, and the setInternalQuery(true) copy keeps user_id, so those per-object checks really do run; the setting is false at ServerSettings.cpp:1171 and DDLTask.cpp:332 switches user only when it is on.

So I agree the residual is real and hard-class. What I measured next decides where it gets fixed. Caller holds CLUSTER plus DROP DATABASE on the database and no per-object rights. Controls: DROP TABLE and TRUNCATE TABLE are both refused for that caller, and a caller without DROP DATABASE is still refused over ON CLUSTER, so the probe can see a refusal.

statement local ... ON CLUSTER
TRUNCATE DATABASE ACCESS_DENIED allowed, 1 table -> 0
DROP DATABASE ACCESS_DENIED allowed, 1 table -> 0
DETACH DATABASE ACCESS_DENIED allowed, 1 table -> 0
TRUNCATE ALL TABLES FROM ACCESS_DENIED allowed, 3 rows -> 0

Four of four are identical, and DROP and DETACH are the two arms that already emitted exactly this DROP DATABASE element before this PR. The asymmetry therefore belongs to the database fan-out, not to this change. What the change does move is the reachable set: from any holder of CLUSTER, with no privilege checked at all and readonly and allow_ddl bypassed, down to a holder of DROP DATABASE on that database.

On the first remedy: per-object elements on the initiator cannot converge with the local path. The branch predicate is !drop.table, so the statement names no object, and all four getRequiredAccessForDDLOnCluster implementations in the tree are purely AST-derived (0 catalog references across 531 body lines, including the 475-line SYSTEM one). Without a catalog the only host-independent element is the wildcard db.*, and requiring that refuses a caller the local path allows: with DROP DATABASE ON d.* and DROP TABLE ON d.t but no DROP TABLE ON d.*, local TRUNCATE DATABASE d succeeds. A catalog enumeration is computed once on the initiator and applied to hosts whose catalogs differ, so it is neither sufficient nor necessary.

On the second: refusing the database fan-out forms has to refuse rows 2 and 3 as well, which is an incompatible break of DROP DATABASE ... ON CLUSTER. I am not doing that inside a bug fix on my own judgement.

The complete fix is the worker executing with the initiator's effective user and roles, which is what that setting exists for, and changing its default is a compatibility decision rather than something this branch can express. I have added the residual to the PR description.

@clickhouse-gh clickhouse-gh Bot added the comp-rbac Authorization: roles, grants, quotas, row-level security, access checks. label Sep 4, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing f00130f1e with master c00751cda (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of ae6b79d9b; 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

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

Job report

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 5, 2026
@alexey-milovidov alexey-milovidov added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Sep 5, 2026
Merged via the queue into ClickHouse:master with commit d9d115b Sep 5, 2026
174 of 344 checks passed
@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 Sep 5, 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 Sep 5, 2026
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 5, 2026
clickhouse-gh Bot added a commit that referenced this pull request Sep 6, 2026
Backport #118155 to 26.6: Require `DROP DATABASE` for `TRUNCATE` of a database over `ON CLUSTER`
clickhouse-gh Bot pushed a commit that referenced this pull request Sep 6, 2026
clickhouse-gh Bot added a commit that referenced this pull request Sep 6, 2026
Backport #118155 to 26.8: Require `DROP DATABASE` for `TRUNCATE` of a database over `ON CLUSTER`
clickhouse-gh Bot pushed a commit that referenced this pull request Sep 6, 2026
clickhouse-gh Bot added a commit that referenced this pull request Sep 6, 2026
Backport #118155 to 26.7: Require `DROP DATABASE` for `TRUNCATE` of a database over `ON CLUSTER`
PedroTadim added a commit that referenced this pull request Sep 10, 2026
Backport #118155 to 26.3: Require `DROP DATABASE` for `TRUNCATE` of a database over `ON CLUSTER`
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Sep 11, 2026
…CATE` of a database over `ON CLUSTER`
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Sep 11, 2026
…CATE` of a database over `ON CLUSTER`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-rbac Authorization: roles, grants, quotas, row-level security, access checks. groeneai-origin-self PR origin: self-found defect (impact-bar validated) pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default 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.

6 participants