Skip to content

Check the TABLE ENGINE grant on the initiator of RESTORE ... ON CLUSTER - #129468

Merged
jkartseva merged 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/restore-on-cluster-table-engine-grant
Nov 12, 2026
Merged

jkartseva merged 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/restore-on-cluster-table-engine-grant

Conversation

@groeneai

@groeneai groeneai commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #113073

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 RESTORE ... ON CLUSTER not checking the TABLE ENGINE grant for the engines of the restored tables and of the inner tables of materialized views, so a user without the grant could restore, for example, a File table and read the server file through it. Such a restore now needs the same grant as RESTORE without ON CLUSTER, e.g. GRANT TABLE ENGINE ON File or GRANT READ, WRITE ON FILE.

Description

On 26.10.2.103, a user without any FILE grant who is refused RESTORE TABLE db.t FROM Disk('backups', 'b') of an ENGINE = File(...) table (Code: 497 ... TABLE ENGINE ON File) can run the same restore with ON CLUSTER test_shard_localhost, get the table, and read the file.

Root cause: the hosts of RESTORE ... ON CLUSTER run the internal restore with no user, so the only check that runs as the user is the preflight RestorerFromBackup::checkAccessForObjectsFoundInBackup in CHECK_ACCESS_ONLY mode on the initiator. It requires CREATE TABLE/CREATE VIEW, INSERT and the export grants, but not the TABLE ENGINE grant that a local restore checks when it creates the table. For a source engine such as File, that grant is implied only by READ/WRITE on the source, so this is reachable on the default configuration.

Change: in CHECK_ACCESS_ONLY mode, require TABLE ENGINE on every engine the hosts will create (the table's engine and each inner engine), the same element as CREATE ... ON CLUSTER requires. With restore_replace_external_engines_to_null in the SETTINGS of the RESTORE, an external engine becomes Null on the hosts, which needs TABLE ENGINE ON Null, as locally; a session value is not trusted, as a host may clamp it. A local RESTORE is unchanged.

Grants checked inside a storage creator (e.g. the File delegate of URL('file://...')) remain unchecked on the hosts, as for CREATE ... ON CLUSTER, until the hosts run as the user (distributed_ddl_use_initial_user_and_roles).

Validation: the new test covers a File table, a materialized view and a refreshable materialized view with a File inner engine, restore_replace_external_engines_to_null set in the query and in the session, and a positive control; it fails on master and passes 50/50 with randomized settings. test_backup_restore_on_cluster, test_restore_external_engines and test_backup_restore_new pass.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.11.1.2004-master (included in 26.11 and later)

The hosts of `RESTORE ... ON CLUSTER` run the internal restore with no user, so
the only check that runs as the user is the `CHECK_ACCESS_ONLY` preflight on the
initiator. It required `CREATE TABLE` / `CREATE VIEW`, `INSERT` and the export
grants, but not the `TABLE ENGINE` grant that a local `RESTORE` checks when it
creates the table and, for a view, its inner table. For a source engine such as
`File` that grant is implied only by `READ` / `WRITE` on the source, so a user
without any `FILE` grant, refused a local restore of an `ENGINE = File` table,
could restore it `ON CLUSTER` and read and write the server file through it
(measured on 26.10.2.103 and master).

The preflight now requires `TABLE ENGINE` on the table's engine and on every
inner engine of `create.targets` (the walk `getExportAccess` already does there,
and the element `CREATE ... ON CLUSTER` requires on its initiator), in
`CHECK_ACCESS_ONLY` mode only: a local restore already checks the engine it
actually creates. `restore_replace_external_engines_to_null` does not exempt the
engine on this path, because the hosts may not apply the initiator's value.

Grants checked inside a storage creator (e.g. the `File` delegate of
`URL('file://...')`) stay unchecked on the hosts, as for `CREATE ... ON CLUSTER`,
until the hosts run as the initiating user.

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

groeneai commented Nov 5, 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; 1 finding).

# Sev Finding Verdict Evidence / action
1 ❌ RESTORE TEMPORARY TABLE ... ON CLUSTER skips the new engine check (RestorerFromBackup.cpp:441-446) DISAGREE A host cannot register a temporary table: it restores in a copy of the DDL worker context, which has no session, and a temporary CREATE ends in getSessionContext()->addExternalTable (InterpreterCreateQuery.cpp:2757-2759), which throws THERE_IS_NO_SESSION (Context.cpp:4509-4513). So that restore fails on every host with or without the grant, and no table exists to read or write.

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

Session id: cron:clickhouse-review-self-slot-58:20261105-115200

@groeneai

groeneai commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. A user with no FILE grant: local RESTORE of an ENGINE = File table fails TABLE ENGINE ON File, the same restore ON CLUSTER test_shard_localhost succeeds and the user reads the file; every run, on master and on the official 26.10.2.103 image.
b Root cause explained? The hosts of RESTORE ... ON CLUSTER create tables with no user; the initiator's CHECK_ACCESS_ONLY preflight is the only check that runs as the user, and it never required TABLE ENGINE, which a local restore checks when it creates the table or a view's inner table.
c Fix matches root cause? Yes. The preflight now requires TABLE ENGINE on every engine the definition names, in CHECK_ACCESS_ONLY mode only; local restores are unchanged.
d Test intent preserved / new tests added? New stateless test: File table, materialized view, refreshable materialized view, restore_replace_external_engines_to_null, and a positive control with READ, WRITE ON FILE.
e Both directions demonstrated? Fails on master (the cluster lines print restored), passes with the fix. Removing the inner-engine loop fails only the view lines; exempting replace-to-null fails only that line.
f Fix is general across code paths? Table engine and every inner engine of create.targets (all view kinds, TimeSeries). Grants checked inside a storage creator (e.g. URL('file://...')) stay unchecked on the hosts, as for CREATE ... ON CLUSTER (#113073 class).
g Fix generalizes across inputs (params/datatypes/wrappers)? The engine is a name, so there is no type matrix; covered with and without the grant, with restore_replace_external_engines_to_null on and off, on stock and CI access-control settings.
h Backward compatible? (maintainer-approved exception only) Only a user lacking the grant is affected, and gets the error a local RESTORE and CREATE ... ON CLUSTER already give. No setting or format change; admin restores unaffected (test_backup_restore_on_cluster, test_backup_restore_new pass).
i Invariants and contracts preserved? Only the preflight's required-access list grows; CHECK_ACCESS_ONLY has one caller (the ON CLUSTER initiator); no state or locking change.

Session id: cron:clickhouse-impl-self-slot-57:20261105-100900

@groeneai

groeneai commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator Author

cc @pufit @pamarcos, could you review this and add the can be tested label so CI runs? The RESTORE ... ON CLUSTER preflight on the initiator did not require TABLE ENGINE for the engines of the restored tables (the hosts create them with no user), so a user without any FILE grant could restore a File table and read the file; it now requires the same grant as a local RESTORE.

@clickhouse-gh clickhouse-gh Bot added comp-backup Backup/restore infrastructure and backup formats. can be tested Allows running workflows for external contributors labels Nov 5, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [1b0827e]

Summary: ✅


AI Review

Summary

PR #129468 adds initiator-side TABLE ENGINE checks for table and inner-table engines restored by RESTORE ... ON CLUSTER. Reviewed head 1b0827eeb7dc9117bc2556000d48f0ebbcbca1be and all prior discussion. The earlier finding about external engines replaced with Null is addressed: explicit query settings authorize Null, while session-only values conservatively require the original engine grant. That thread is resolved; no new actionable findings remain. The focused regression test 05508_restore_on_cluster_table_engine_grant passed in CI on this head.

Final Verdict

Status: ✅ Approve.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Nov 5, 2026
Comment thread src/Backups/RestorerFromBackup.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 1b0827eeb with master 7833893d3 (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

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, 13 s compile time in total, 7 of them have a recent master baseline.

Job report

…flight

With `restore_replace_external_engines_to_null`, the hosts of `RESTORE ... ON CLUSTER`
replace an external engine, of the table or of a view's inner table, with `Null`, as a
local restore does. The preflight now requires `TABLE ENGINE ON Null` for such an engine
instead of the grant for the engine named in the backup, so a user who may restore the
table locally is no longer refused on the cluster.

Only a value in the `SETTINGS` clause of the `RESTORE` is trusted: it is sent in the
query text, and a host applies it or fails the restore. A session value travels in the
distributed DDL entry, which a host clamps to its own constraints and which an entry of
`distributed_ddl_entry_format_version = 1` does not carry at all (measured: with a
session value at format 1 the hosts created a `File` table), so for it the preflight
still requires the engine named in the backup.

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

@jkartseva jkartseva 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.

LGTM

@jkartseva
jkartseva added this pull request to the merge queue Nov 11, 2026
Merged via the queue into ClickHouse:master with commit 9565f05 Nov 12, 2026
186 checks passed
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 12, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Nov 19, 2026
Since ClickHouse#129468 the initiator of `RESTORE ... ON CLUSTER` asks `TABLE ENGINE ON Null`
for a source-backed table that every host replaces by `Null`. The last case of
05569 tests that a value of `restore_replace_external_engines_to_null` in the
query lifts the export settings of such a table, so its user now also needs that
grant; the case and its reference are otherwise unchanged.

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

Copy link
Copy Markdown
Collaborator Author

Post-merge note: this fix (merge commit 9565f05) is not on 26.3, 26.8, 26.9 or 26.10, and no backport PR exists. The bypass was measured on 26.10.2.103 (see the description). If it should reach the release branches, the robot needs a pr-must-backport or v26.X-must-backport label here.

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-backup Backup/restore infrastructure and backup formats. pr-bugfix Pull request with bugfix, not backported by default 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.

3 participants