Skip to content

Fix DROP WORKLOAD and DROP RESOURCE removing an entity of the other kind with Keeper workload storage - #131688

Merged
serxa merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-drop-workload-resource-other-kind
Nov 16, 2026
Merged

serxa merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-drop-workload-resource-other-kind

Conversation

@groeneai

@groeneai groeneai commented Nov 14, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #131653

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

Fixed DROP WORKLOAD removing a resource with the same name and DROP RESOURCE removing a workload with the same name when workload entities are stored in Keeper (workload_zookeeper_path). A user with only one of the two privileges could remove the other kind of entity.

Description

On 26.8 LTS (26.8.37.168, also 26.3.63.123) with workload_zookeeper_path set, a user granted only DROP WORKLOAD can run DROP WORKLOAD cpu and remove the resource cpu, and a user granted only DROP RESOURCE can remove a workload the same way. The definition is gone from Keeper for every replica. Found by the AI review of #131653 (comment).

Workloads and resources share one name space, and WorkloadEntityStorageBase::removeEntity looked the name up without checking the kind of the stored entity. The Keeper storage then rewrites its single znode without that name. The interpreters check only the privilege of the statement's own kind. Disk storage was not affected only because its file names are kind-specific.

removeEntity now treats an entity of the other kind as missing, for every storage: the statement fails with Workload entity 'x' doesn't exist and IF EXISTS is a no-op, which is what disk storage already did. storeEntity already rejects a kind mismatch for CREATE OR REPLACE.

The new test in test_scheduler_io (Keeper-backed) fails without the fix and passes with it (20/20); the rest of test_scheduler_io, test_backup_restore_workload_entities and the workload stateless tests pass.

#122761 (still open) rewrites this function with {kind, name} keys and would remove the bug on master with the same behaviour; this one-line change is the version that can be backported.


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

Version info

  • Merged into: 26.11.1.2475-master (included in 26.11 and later)
  • Backported to: 26.10.5.44, 26.9.31.10, 26.8.39.9, 26.3.66.11

…r kind

Workloads and resources share one name space in
`WorkloadEntityStorageBase::entities`, and `removeEntity` looked the name up
without checking that the stored entity is of the requested kind. With Keeper
workload storage (`workload_zookeeper_path`), `removeEntityImpl` rewrites the
single znode without that name, so `DROP WORKLOAD cpu` removed the resource
`cpu` and `DROP RESOURCE x` removed the workload `x`. The interpreters only
check the privilege of the statement's own kind, so a user granted only
`DROP WORKLOAD` could remove resources and vice versa.

Disk storage was safe only because its file name is kind-specific
(`workload_<name>.sql` / `resource_<name>.sql`), so the same statement failed
with "Workload entity 'cpu' doesn't exist". Treat an entity of the other kind
as missing in the base class, which every storage goes through: the statement
now fails the same way on every storage, and `IF EXISTS` is a no-op. The check
sits inside the retry loop, so it is re-done after a Keeper version conflict.

The sibling write path `storeEntity` already rejects a kind mismatch for
`CREATE OR REPLACE`.

Reported by the AI review on ClickHouse#131653
(ClickHouse#131653 (comment)).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review (round 1): CLEAN, no blocker or major
Finding Verdict
💡 Use Critical Bug Fix because this is an RBAC bypass DISAGREE: that category is chosen by maintainers; Bug Fix holds (reproduced on 26.3 and 26.8 LTS)
💡 Workload entity 'x' doesn't exist is reported although an entity of the other kind named x exists noted-not-blocking: same text disk storage already returns for this statement

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. Server with embedded Keeper and workload_zookeeper_path; as a user with only DROP WORKLOAD, DROP WORKLOAD cpu removes the resource cpu on every run (also DROP RESOURCE development by a DROP RESOURCE-only user). Reproduced on master and on official 26.8.37.168 / 26.3.63.123 images.
b Root cause explained? Workloads and resources share one name space; WorkloadEntityStorageBase::removeEntity found the entity by name without checking its kind, and the Keeper storage rewrites its single znode without that name. The interpreters only check the privilege of the statement's own kind.
c Fix matches root cause? Yes. The kind check is added in removeEntity, the one entry point of every storage, matching the existing kind check in storeEntity.
d Test intent preserved / new tests added? New integration test test_scheduler_io::test_drop_does_not_remove_entity_of_other_kind (Keeper-backed): both privilege directions, IF EXISTS, znode content, survival in system.resources / system.workloads, and a positive control. No existing test changed.
e Both directions demonstrated? The new test fails on the base binary at the first cross-kind DROP (query succeeded) and passes 20/20 with the fix; the manual repro shows the same split.
f Fix is general across code paths? removeEntity is the only implementation used by both DROP interpreters for Disk, Keeper and config storages. Create and restore already go through the kind-checking storeEntity.
g Fix generalizes across inputs (params/datatypes/wrappers)? Both kinds, with and without IF EXISTS, on Keeper and disk storage, including a wrong-kind name that is referenced by another entity. No types or settings are involved.
h Backward compatible? (maintainer-approved exception only) Yes. Only a DROP of a name held by the other kind changes: on Keeper it now fails with doesn't exist (no-op with IF EXISTS), as on disk storage before. No setting or format change.
i Invariants and contracts preserved? A DROP of kind T removes only an entity of kind T, on every storage, and the change event sent to subscribers has the kind of the removed entity. The check runs under the same lock, inside the Keeper retry loop, before any mutation.

Session id: cron:clickhouse-impl-slot-7:20261114-204102

@clickhouse-gh

clickhouse-gh Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a4dfb87]

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 adds a shared entity-kind check to removeEntity and regression coverage for both privilege directions and IF EXISTS. The ordinary deletion path is fixed, but a pre-existing Keeper session-renewal path still bypasses the claimed restriction and can delete definitions without the corresponding privilege. One blocker remains.

Findings

❌ Blockers

  • src/Common/Scheduler/Workload/WorkloadEntityStorageBase.cpp:565: Session renewal can invalidate the kind check without restarting validation. WorkloadEntityKeeperStorage::removeEntityImpl constructs the deletion payload before getZooKeeper, which can refresh the entity maps and current_version. Its subsequent conditional write uses the new version with the old payload and succeeds. If A caches workload x, its session expires, and B replaces that workload with resource x, a DROP WORKLOAD-only user whose query renews A's session can still remove the resource from Keeper. Definitions added during the disconnection can also be erased. Return OperationResult::Retry whenever session renewal changes the validated state before a write, and rebuild the payload only after revalidation. Apply the same safeguard to storeEntityImpl, which has the identical ordering.
Tests
  • ⚠️ Add deterministic coverage for session renewal after the cached entity has changed kind on another node. Verify that validation restarts and that the resource remains persisted after DROP WORKLOAD, including IF EXISTS.
Missing context / blind spots
  • ⚠️ The renewal finding follows directly from the current call sequence; it was not reproduced locally because this checkout has no ClickHouse binary/build. CI regression runs are still pending. The author's reported 20/20 passes cover the steady-state test added here.
Final Verdict

❌ Block. Make session renewal preserve the validation/version invariant and cover it deterministically. If this pre-existing path is deferred, link a dedicated tracking issue that explicitly records the remaining privilege bypass and stale-payload data loss.

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default can be tested Allows running workflows for external contributors comp-scheduler Workload scheduler and resource management: CPU/IO/memory scheduling, concurrency control, worklo... labels Nov 14, 2026

@serxa serxa left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This indeed has a fix in my other unmerged yet PR but let's backport your version - it's much simpler

@serxa serxa added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Nov 14, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

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

middas-0706 pushed a commit to middas-0706/ClickHouse that referenced this pull request Nov 15, 2026
… writing

`WorkloadEntityKeeperStorage::storeEntityImpl` and `removeEntityImpl`
serialized the definitions from the in-memory cache and only then called
`getZooKeeper`. When the cached session had expired, `getZooKeeper` opened a
new one and `refreshEntities` replaced the cache and advanced
`current_version` to the znode's current version, so the following
`trySet(..., current_version)` passed its version check and committed the
payload built from the stale cache: definitions written by other replicas
meanwhile were deleted (or a dropped workload resurrected), and the
validation in `WorkloadEntityStorageBase::storeEntity/removeEntity`
(references, cycles, parent kind, unique resources) had run against the
stale cache as well.

Call `getZooKeeper` first and return `OperationResult::Retry` when it
changed `current_version`, so the caller re-runs the validation on the
refreshed state (the same contract the `ZBADVERSION` path already uses);
serialize only after that check.

The integration test expires the Keeper session of `node` with
`SYSTEM RECONNECT ZOOKEEPER` right after its watching thread refreshed (it
then sleeps for up to 10 s and ignores SESSION events), lets `node2` change
the definitions, and runs a DDL on `node` as the first user of the new
session: `DROP WORKLOAD` must see the new child, `CREATE WORKLOAD` must
keep `node2`'s definition.

Related: ClickHouse#131688

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@serxa
serxa enabled auto-merge November 16, 2026 15:30
@serxa
serxa added this pull request to the merge queue Nov 16, 2026
Merged via the queue into ClickHouse:master with commit 7615344 Nov 16, 2026
568 of 574 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 16, 2026
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 16, 2026
@robot-clickhouse robot-clickhouse added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 16, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 16, 2026
Backport #131688 to 26.10: Fix `DROP WORKLOAD` and `DROP RESOURCE` removing an entity of the other kind with Keeper workload storage
clickhouse-gh Bot added a commit that referenced this pull request Nov 16, 2026
Backport #131688 to 26.9: Fix `DROP WORKLOAD` and `DROP RESOURCE` removing an entity of the other kind with Keeper workload storage
clickhouse-gh Bot added a commit that referenced this pull request Nov 16, 2026
Backport #131688 to 26.8: Fix `DROP WORKLOAD` and `DROP RESOURCE` removing an entity of the other kind with Keeper workload storage
PedroTadim added a commit that referenced this pull request Nov 17, 2026
Backport #131688 to 26.3: Fix `DROP WORKLOAD` and `DROP RESOURCE` removing an entity of the other kind with Keeper workload storage
mkmkme pushed a commit to Altinity/ClickHouse that referenced this pull request Nov 17, 2026
…OURCE` removing an entity of the other kind with Keeper workload storage
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-scheduler Workload scheduler and resource management: CPU/IO/memory scheduling, concurrency control, worklo... 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.

4 participants