Repository navigation
Require DROP DATABASE for TRUNCATE of a database over ON CLUSTER - #118155
alexey-milovidov merged 1 commit into
Conversation
`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]>
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.
My own cold pass produced no blocker or major. It found four nits, three of which I fixed in the Severity: ❌ blocker / |
|
cc @pufit @evillique, could you review this? The |
|
Workflow [PR], commit [f00130f] Summary: ✅
AI ReviewSummaryThis PR closes the empty-access-list hole for PR Metadata
Findings❌ Blockers
Final Verdict❌ Changes requested. LLVM Coverage ReportMeasured on commit f00130f.
Changed lines: Changed C/C++ lines covered: 3/3 (100.00%) · Uncovered code |
| /// `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()); |
There was a problem hiding this comment.
!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.
There was a problem hiding this comment.
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.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 14 s compile time in total, 7 of them have a recent master baseline. |
d9d115b
Backport #118155 to 26.6: Require `DROP DATABASE` for `TRUNCATE` of a database over `ON CLUSTER`
… database over `ON CLUSTER`
Backport #118155 to 26.8: Require `DROP DATABASE` for `TRUNCATE` of a database over `ON CLUSTER`
… database over `ON CLUSTER`
Backport #118155 to 26.7: Require `DROP DATABASE` for `TRUNCATE` of a database over `ON CLUSTER`
Backport #118155 to 26.3: Require `DROP DATABASE` for `TRUNCATE` of a database over `ON CLUSTER`
…CATE` of a database over `ON CLUSTER`
…CATE` of a database over `ON CLUSTER`
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
TRUNCATE DATABASE ... ON CLUSTERandTRUNCATE [ALL] TABLES FROM ... ON CLUSTERran without checking any privilege: a user holding onlySELECTandCLUSTERcould destroy every table in a database, andreadonly = 1did not prevent it. Both now requireDROP DATABASE, the privilege the non-cluster spellings have always required.Description
Related: #114011
InterpreterDropQuery::getRequiredAccessForDDLOnClusterhandled onlyKind::DetachandKind::Dropin its!drop.tablebranch, with no terminalelse, soKind::Truncatereturned an emptyAccessRightsElementsandexecuteDDLQueryOnClusterchecked onlyAccessType::CLUSTERbefore queueing the DDL.CLUSTERis declaredGLOBALand is in neithernot_readonly_flagsnorddl_flags, so that one empty list removed three gates: the privilege check,readonlyandallow_ddl. The blast radius isDROP-equivalent rather thanTRUNCATE-equivalent, becauseexecuteToDatabaseImplmaps a bareTRUNCATE DATABASEtoKind::Dropper table. Present in released stable; probed atv26.8.2.7-lts.The branch is now unconditional rather than gaining a fourth
else if: the local path authorizes all three kinds with oneDROP DATABASEcheck,AccessType.hdocuments 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 theON CLUSTERform relies on this bug.Left alone deliberately: the
is_dictionaryarm still handles onlyDetachandDrop, butTRUNCATE DICTIONARYis 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 FUZZERandSYSTEM RESET COVERAGEskip theirON CLUSTERcheck the same way.Not closed here: the local database path also checks each object after the database check, while the
ON CLUSTERworker runs as the initiator only whendistributed_ddl_use_initial_user_and_rolesis enabled (default off). Identical forDROP DATABASEandDETACH 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
26.9.1.814(included in26.9and later)26.8.3.50,26.7.7.36,26.6.5.62,26.3.33.36