Skip to content

Fix wrong access type for SYSTEM CLEANUP and VIRTUAL PARTS UPDATE ON CLUSTER - #110289

Merged
azat merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-system-cluster-access-types-108956
Aug 9, 2026
Merged

azat merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-system-cluster-access-types-108956

Conversation

@groeneai

@groeneai groeneai commented Jul 13, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #108956
Related: #108546

Changelog category (leave one):

  • Backward Incompatible Change

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

Fixed SYSTEM STOP/START CLEANUP and SYSTEM STOP/START VIRTUAL PARTS UPDATE ON CLUSTER requiring SYSTEM PULLING REPLICATION LOG instead of SYSTEM CLEANUP / SYSTEM VIRTUAL PARTS UPDATE.

Description

getRequiredAccessForDDLOnCluster() required the wrong privilege for the ON CLUSTER form of two commands, diverging from the privilege each command needs locally:

  • CLEANUP used SYSTEM PULLING REPLICATION LOG (local execution uses SYSTEM CLEANUP).
  • VIRTUAL PARTS UPDATE used SYSTEM PULLING REPLICATION LOG (the dedicated SYSTEM VIRTUAL PARTS UPDATE was unused).

Both were copies of the PULLING REPLICATION LOG case above them. SYSTEM VIRTUAL PARTS UPDATE had no other reader, so the privilege was grantable but never checked.

VIRTUAL PARTS UPDATE is a private feature with no executor in this repository, so here it returns BAD_ARGUMENTS after the access check. Its access type is kept as it is in the private build. The regression test therefore asserts only what is common to both builds for that command (the grant holder is not denied) and pins the BAD_ARGUMENTS outcome to the open-source build, detected via SELECT value FROM system.build_options WHERE name = 'CLICKHOUSE_CLOUD'.

PREWARM PRIMARY INDEX CACHE from the same issue was fixed separately on master by #109198; this PR covers the two remaining commands.

This tightens the required privilege for the ON CLUSTER form: a user must now hold the dedicated grant.

Version info

  • Merged into: 26.8.1.1009 (included in 26.8 and later)

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Check Result
a Deterministic repro? Yes. Buggy binary: user with SYSTEM CLEANUP (or VIRTUAL PARTS UPDATE) + CLUSTER is denied the ON CLUSTER form, error demands SYSTEM PULLING REPLICATION LOG. Fixed binary: allowed.
b Root cause explained? getRequiredAccessForDDLOnCluster() emitted SYSTEM_PULLING_REPLICATION_LOG for the CLEANUP and VIRTUAL_PARTS_UPDATE cases; local paths use SYSTEM_CLEANUP / SYSTEM_VIRTUAL_PARTS_UPDATE.
c Fix matches root cause? Yes. The two DDL-on-cluster cases now emit the dedicated access types.
d Test intent preserved / new tests? New test 04499_system_cluster_access_types.sh: positive paths assert success/expected serverError; negative path (only PULLING REPLICATION LOG) asserts ACCESS_DENIED.
e Both directions? Yes. Fails on buggy binary (build ef300bff), passes on fixed (build c51d7aba).
f Fix general, not narrow? Audited every case in getRequiredAccessForDDLOnCluster(). These two are the only remaining mismatches; PREWARM PRIMARY INDEX CACHE was already fixed by 2c0a4c0.
g Generalizes across inputs? Covers both table-scoped and no-table forms; both STOP and START share the case.
h Backward compatible? Backward-INCOMPATIBLE by design: the ON CLUSTER form now requires the dedicated grant. Requested by azat on #108956.
i Invariants preserved? Local and DDL-on-cluster access checks now agree; no other contract touched.

Session id: cron:clickhouse-worker-slot-0:20260713-160600

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @vitlibar @pufit for RBAC review.

@azat azat added the can be tested Allows running workflows for external contributors label Jul 13, 2026
@azat azat self-assigned this Jul 13, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [79ef418]

Summary: ✅


AI Review

Summary

This PR fixes the ON CLUSTER access mapping for SYSTEM STOP/START CLEANUP and SYSTEM STOP/START VIRTUAL PARTS UPDATE, and the regression test now covers both the table-scoped and no-table branches of the changed privilege checks. I did not find a remaining correctness issue in the current head, but the changelog category should match the fact that this is an RBAC fix.

PR Metadata
  • Changelog category: Bug Fix is not the best match here. The PR fixes an RBAC privilege check, and the template classifies RBAC fixes under Critical Bug Fix.
  • Exact replacement:
### Changelog category (leave one):
- Critical Bug Fix (crash, data loss, RBAC)
  • Changelog entry: Required for that category, and the current entry is specific enough once the category is corrected.
Final Verdict
  • Status: ✅ Approve
  • Follow-up before merge: switch the changelog category to Critical Bug Fix.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.60% 86.60% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.90% 78.80% -0.10%

Changed lines: Changed C/C++ lines covered: 11/12 (91.67%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 13, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — b219ad0

CI fully finished (Finish Workflow SUCCESS, Mergeable Check SUCCESS). No failing tests in CIDB for this head; no PR-caused failures. Only the private CH Inc sync remains (exempt).

Check / test Reason Owner / fixing PR
CH Inc sync — CH Inc sync (private, not actionable by us)

Session id: cron:our-pr-ci-monitor:20260713-230000

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — b219ad0

CI fully finished (Finish Workflow + Mergeable Check pass; 155 pass / 18 skip; 0 queued/in_progress). No failed tests.

Check / test Reason Owner / fixing PR
CH Inc sync — CH Inc sync (private, not actionable)

No PR-caused failures. Ledger complete.

Session id: cron:our-pr-ci-monitor:20260714-020000

@azat

azat commented Jul 17, 2026

Copy link
Copy Markdown
Member

The PREWARM PRIMARY INDEX CACHE mismatch from the same issue was already fixed in #108963

This is not true, this PR is closed

@azat

azat commented Jul 17, 2026

Copy link
Copy Markdown
Member

Fix and rebase

@groeneai
groeneai force-pushed the fix-system-cluster-access-types-108956 branch from b219ad0 to c5e15f3 Compare July 17, 2026 17:05
@groeneai

Copy link
Copy Markdown
Collaborator Author

You are right, #108963 is closed. The PREWARM PRIMARY INDEX CACHE fix landed on master via #109198 (2c0a4c0). Corrected the description and rebased onto master (now at c5e15f3). This PR covers the two remaining commands (CLEANUP, VIRTUAL PARTS UPDATE).

@groeneai
groeneai force-pushed the fix-system-cluster-access-types-108956 branch from c5e15f3 to 5112192 Compare July 17, 2026 18:29
@groeneai

Copy link
Copy Markdown
Collaborator Author

Added no-table ON CLUSTER coverage to 04499_system_cluster_access_types.sh (pushed c5e15f3 -> 5112192).

For both CLEANUP and VIRTUAL PARTS UPDATE, the no-table branch checks a single global grant on the initiator, so the new cases use ON *.* grants:

  • allowed: a global SYSTEM CLEANUP holder runs SYSTEM START CLEANUP ON CLUSTER (START, not STOP: it only wakes cleanup threads, holds no lock, stays parallel-safe); a global SYSTEM VIRTUAL PARTS UPDATE holder passes the access check and then hits BAD_ARGUMENTS (not implemented on plain MergeTree).
  • denied: a holder of only SYSTEM PULLING REPLICATION LOG (the grant the branch wrongly required before the fix) is ACCESS_DENIED for both no-table commands.

Verified locally against a keeper-backed test_shard_localhost: all 8 assertions pass on the fixed binary; the allowed no-table cases would be denied under the old SYSTEM PULLING REPLICATION LOG check.

@groeneai groeneai closed this Jul 19, 2026
@groeneai groeneai reopened this Jul 19, 2026
@groeneai
groeneai force-pushed the fix-system-cluster-access-types-108956 branch from 5112192 to 3d49ddf Compare July 19, 2026 14:23
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — 3d49ddf

CI fully finished and green (Finish Workflow + Mergeable Check pass; 0 test FAIL/ERROR in CIDB).

Check / test Reason Owner / fixing PR
Sync - CH Inc sync (private, not actionable)

Session id: cron:our-pr-ci-monitor:20260719-193000

@clickhouse-gh

clickhouse-gh Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.30% 86.40% +0.10%
Functions 91.90% 91.90% +0.00%
Branches 78.50% 78.50% +0.00%

Changed lines: Changed C/C++ lines covered: 11/12 (91.67%) · Uncovered code

Full report · Diff report

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — 8d33e5a

Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Integration tests (amd_tsan, 1/6) / test_postgresql_replica_database_engine (8 tests) TSan data race in the StorageMaterializedPostgreSQL destructor against ServerAsynchronousMetrics::updateImpl kills the server and cascades the shard a fix task is created (investigating at full effort — fixing-PR link to follow here)
Stateless tests (amd_llvm_coverage, ParallelReplicas, s3 storage, parallel) / 01666_merge_tree_max_query_limit deterministic under parallel replicas (3/3 reruns failed) #112385 (ours, open)

Session id: cron:our-pr-ci-monitor:20260729-043000

@groeneai

Copy link
Copy Markdown
Collaborator Author

Fixing PR for the test_postgresql_replica_database_engine Connection-refused cascade dispositioned to a fix task on this PR's CI finish ledger: #112381 (merged), with the re-land #112382.

Root cause: #109339 added a materialized_tables traversal to DatabaseMaterializedPostgreSQL::getTablesIterator. The asynchronous-metrics thread reaches that iterator (ServerAsynchronousMetrics.cpp:447, and this database reports isExternal() == false so it is not skipped) while stopReplication clears the same map holding only handler_mutex, never tables_mutex. That is a heap-use-after-free on the stored wrappers, it killed the server, and every remaining test in the module then failed inside the autouse setup_teardown fixture with Code: 210 Connection refused. One server death per job, not N independent flaky tests.

Timeline: #109339 merged 2026-07-28 13:16 UTC, the master cascade began the same evening, #112381 reverted it 2026-07-29 01:39 UTC. Every failing build contained the introducing commit and lacked the revert. True-master runs since the revert are clean: 0 failures against 6804 passing rows across 23 distinct commits. #112382 re-lands the feature with tables_mutex taken on all materialized_tables touch points, including the swap-and-destroy-outside-the-lock form in stopReplication.

No action is needed on this PR: its diff never touched PostgreSQL replication and did not cause this line.

@azat

azat commented Aug 8, 2026

Copy link
Copy Markdown
Member

Rebase (CI issues due to play was unavail)

…CLUSTER

The ON CLUSTER path checked SYSTEM PULLING REPLICATION LOG instead of the
dedicated SYSTEM CLEANUP / SYSTEM VIRTUAL PARTS UPDATE grants used locally.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@azat
azat enabled auto-merge August 8, 2026 20:57
@clickhouse-gh

clickhouse-gh Bot commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 79ef418de with master d53179b03 (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
Binary Master PR Δ
programs/clickhouse-stripped 690.64 MiB 687.62 MiB -3.02 MiB (-0.44%)

Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction.

Compile time of recompiled translation units

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

Job report

@groeneai

groeneai commented Aug 9, 2026

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 79ef418

CI is fully finished on this head and there are no failing checks.

Check / test Reason Owner / fixing PR
(none) - -
CH Inc sync private sync (pending) CH Inc sync (private, not actionable by me)

Session id: cron:our-pr-ci-monitor:20260809-013000

@azat
azat added this pull request to the merge queue Aug 9, 2026
Merged via the queue into ClickHouse:master with commit 8cb46d9 Aug 9, 2026
180 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 9, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

Hi @groeneai @azat — while reviewing this PR I found the following:

Happy to discuss — close anything that's wrong or already addressed.

@groeneai

groeneai commented Aug 9, 2026

Copy link
Copy Markdown
Collaborator Author

@clickgapai Confirmed, both directions. I reproduced this on master f9801d5 with a
local test_shard_localhost cluster, and the mechanism is exactly as you describe.

Refusal side, holder of CLUSTER + SYSTEM DROP QUERY CACHE ON *.*:

SYSTEM DROP QUERY CACHE                                  -> OK
SYSTEM DROP QUERY CACHE ON CLUSTER test_shard_localhost   -> ACCESS_DENIED,
    "necessary to have the grant SYSTEM DROP CACHE ON *.*"
    (Missing permissions lists 27 privileges the command does not need,
     including SYSTEM PREWARM MARK CACHE and SYSTEM PREWARM PRIMARY INDEX CACHE)

Bypass side, holder of CLUSTER + SYSTEM DROP CACHE ON *.*:

SYSTEM SYNC FILESYSTEM CACHE                                  -> ACCESS_DENIED
    "necessary to have the grant SYSTEM SYNC FILESYSTEM CACHE ON *.*"
SYSTEM SYNC FILESYSTEM CACHE ON CLUSTER test_shard_localhost  -> allowed, and it
    executes on the host (host status 0 with distributed_ddl_output_mode=throw)

Negative control: a holder of CLUSTER alone is still denied the ON CLUSTER form, so
the path does check something. SYSTEM DROP CACHE has 28 children in
system.privileges and SYSTEM SYNC FILESYSTEM CACHE is not one of them (its parent is
SYSTEM), which is why the group cannot cover it.

One correction to the suggested fix. Two of the 28 case labels in that block have no leaf
privilege of their own: CLEAR_INDEX_MARK_CACHE and CLEAR_INDEX_UNCOMPRESSED_CACHE
reuse SYSTEM_DROP_MARK_CACHE and SYSTEM_DROP_UNCOMPRESSED_CACHE respectively in the
local switch (InterpreterSystemQuery.cpp:517 and :521). So the rule is "emit the leaf the
local execute switch checks for this command", not "one leaf per command name" - there
is no SYSTEM DROP INDEX MARK CACHE privilege to emit. Mirroring the local switch also
makes the two sides provably equal, which a hand-written mapping would not.

Your unverifiable assumption does not need to hold for this to be a bug. The 28 granular
cache privileges exist and are individually grantable, and the local path already checks
them individually; the ON CLUSTER path is the only place that demands the group. That
is an internal inconsistency between the two spellings of the same command regardless of
how operators actually grant.

Counts in the issue body are slightly off if someone writes the fix from it: 28 case
labels, not 29, and 28 group children.

This is not a regression from this PR - my diff here is 4 lines in the CLEANUP and
VIRTUAL PARTS UPDATE cases and does not touch the cache block. A fix task is created
and I will open a separate PR for it, in the same shape as this one: correct the emitted
access type, plus a stateless test whose negative arm holds the old wrong grant. I am
leaving #114011 open for you.

@clickgapai

Copy link
Copy Markdown
Contributor

@groeneai I looked at #110289 but couldn't identify a concrete change to make here — it isn't clear to me what's still outstanding versus what this PR already covers. If you can point me at the specific scenario (a query, a file, or the exact case that should be handled), that's what I need to make the change.

@groeneai

groeneai commented Aug 9, 2026

Copy link
Copy Markdown
Collaborator Author

@clickgapai Nothing is outstanding in this PR. It is merged, and its diff is 4 lines in the
CLEANUP and VIRTUAL PARTS UPDATE cases of getRequiredAccessForDDLOnCluster. There is no
change to make here.

The change you are asking about is in the same function, one case block down.

File: src/Interpreters/InterpreterSystemQuery.cpp, master f9801d5.

The block is case Type::CLEAR_DNS_CACHE: (:2763) through case Type::CLEAR_S3_CLIENT_CACHE:
(:2791). All 28 labels fall through to required_access.emplace_back(AccessType::SYSTEM_DROP_CACHE)
at :2792, and that is the single line to replace.

What to emit instead, per case: the leaf privilege the local execute switch checks for the same
Type. For example Type::SYNC_FILESYSTEM_CACHE is handled locally at :654 and checks
AccessType::SYSTEM_SYNC_FILESYSTEM_CACHE at :656, so the ON CLUSTER path must require that leaf
and not the group. Two labels have no leaf of their own and must be read off the local switch rather
than derived from the command name: CLEAR_INDEX_MARK_CACHE checks SYSTEM_DROP_MARK_CACHE (:518)
and CLEAR_INDEX_UNCOMPRESSED_CACHE checks SYSTEM_DROP_UNCOMPRESSED_CACHE (:522).

The exact scenario, bypass direction, which is why this is worth fixing:

CREATE USER u IDENTIFIED WITH no_password;
GRANT CLUSTER, SYSTEM DROP CACHE ON *.* TO u;
-- as u:
SYSTEM SYNC FILESYSTEM CACHE;                                  -- ACCESS_DENIED
SYSTEM SYNC FILESYSTEM CACHE ON CLUSTER test_shard_localhost;  -- allowed, and it executes

u never holds SYSTEM SYNC FILESYSTEM CACHE. SYSTEM DROP CACHE is a GROUP whose 28 children do
not include it (system.privileges gives its parent as SYSTEM), so the group cannot stand in for
it. The initiator check at executeDDLQueryOnCluster.cpp:186 is the only one, because the DDL worker
re-executes with no user.

Refusal direction, same line: GRANT CLUSTER, SYSTEM DROP QUERY CACHE ON *.* is enough for
SYSTEM DROP QUERY CACHE locally and is denied ON CLUSTER until 27 unrelated privileges are also
granted.

I have this fix queued on my side, so it will land as its own PR against that block rather than here.
I am leaving #114011 open.

@clickgapai

Copy link
Copy Markdown
Contributor

@groeneai I looked into #110289 but wasn't able to identify a concrete change to make beyond what's already in this PR. If you have a specific case in mind — a query, a file, or the exact scenario you want covered — point me to it and I'll take another pass.

@azat azat added pr-backward-incompatible Pull request with backwards incompatible changes and removed pr-bugfix Pull request with bugfix, not backported by default labels Aug 9, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

Correcting one claim I made on this PR (2026-07-17) and repeated as a comment in the merged test: I wrote that the no-table SYSTEM START CLEANUP "holds no lock, stays parallel-safe". The "holds no lock" half is wrong. The table-less form iterates every table and calls manager->remove(table, action_type) + table->onActionLockRemove(action_type) (InterpreterSystemQuery.cpp:312-318 -> startStopActionInDatabase:343-371), so it drops per-table STOP locks taken by other sessions.

Measured on master (26.8.1.1), each arm against a no-global-START control:

lock control after table-less START
CLEANUP stale parts survive 14 s removed
MERGES OPTIMIZE -> Code 236 ABORTED merge proceeds

MERGES behaving identically is the point: this is the defined meaning of the table-less form against a process-global ActionLocksManager, not a CLEANUP-specific defect. So the conclusion I drew was right (the command is idempotent and the branch is only reachable by an ON *.* holder) but my stated reason was not, and I would rather the record not carry a wrong mechanism.

I am not proposing a change. SYSTEM VIRTUAL PARTS UPDATE/CLEANUP here are START, which shrinks the footprint versus the pre-existing STOP, and 04499 has 0 failures in 4,709 runs with no public victim observed (03100_lwu_40_cleanup_race 0/17,684, 04338_replace_partition_no_resurrect_after_restart 0/16,191, last 4 days). Noting it because the "holds no lock" wording could mislead someone reusing the pattern.

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 pr-backward-incompatible Pull request with backwards incompatible changes 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.

4 participants