Skip to content

Require READ and WRITE ON FILE to create a table as filesystem(...) - #133420

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-create-table-as-filesystem-file-grants
Nov 21, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-create-table-as-filesystem-file-grants

Conversation

@groeneai

@groeneai groeneai commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #129605
Related: #53610

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 CREATE TABLE ... AS filesystem(...) with an explicit column list not checking READ ON FILE, which let a user with WRITE ON FILE (or with no FILE grant at all, via ON CLUSTER) read any file under user_files through the new table. Such a table now needs READ and WRITE on FILE, the grants that imply TABLE ENGINE ON File. This also applies to CREATE TABLE copy AS src, short ATTACH and local RESTORE of such a table.

Description

On 26.10.5.57 (also 26.8.37.168 and 26.9.25.15), a user with CREATE TABLE and WRITE ON FILE but no READ ON FILE runs CREATE TABLE t (path String, name String, content Nullable(String)) AS filesystem('') and reads every file under user_files with SELECT content FROM t. Reported privately by @ kolsha.

#129605 requires TABLE ENGINE on the engine of a table function used in CREATE TABLE ... AS, but only for an engine registered in StorageFactory with a source. filesystem has no engine: it declares FILE through getSourceAccessObject(), so nothing was required. With explicit columns the structure is not inferred (the only READ check), the function is executed for writing, and ON CLUSTER / Replicated hosts build the table with full access.

getSourceEngineOfTableFunction becomes getRequiredAccessOfTableFunction and returns the required grant: TABLE ENGINE on a registered source engine (unchanged), or unfiltered READ and WRITE on the source of a function without an engine, the element ATTACH ... FROM '<path>' already requires. filesystem is the only such function. Both call sites use it.

Behaviour change: a short ATTACH or local RESTORE of a table created as filesystem(...) needs READ and WRITE ON FILE, as a File table needs TABLE ENGINE ON File. Server startup and replicas are unaffected; RESTORE ... ON CLUSTER is still not checked on the initiator, as for TABLE ENGINE since #129605.

05842_create_table_as_filesystem_file_grants fails on master and passes with the fix (local, ON CLUSTER, copy; file() keeps TABLE ENGINE ON File), 100/100 randomized runs.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.12.1.137-master (included in 26.12 and later)
  • Backported to: 26.11.1.2928, 26.10.13.4, 26.9.38.10

groeneai and others added 2 commits November 21, 2026 16:35
Since ClickHouse#129605 a table created as a table function needs `TABLE ENGINE` on the
function's engine when that engine has a source, the same grant as
`ENGINE = <engine>`. `filesystem` has no registered engine: it declares its
source only through `ITableFunction::getSourceAccessObject()` (FILE), so the
`StorageFactory` lookup missed and nothing was required. With an explicit
column list the structure is not inferred, so the READ check of the inference
never ran, and the table function was executed only for writing (`WRITE ON
FILE`). `ON CLUSTER` and `Replicated` databases build the table with full
access, so there no FILE grant was needed at all. `StorageFilesystem` reads
with the global context, so a later `SELECT` checks only the table's grant:
a user with `CREATE TABLE` and `WRITE ON FILE` (or nothing, via `ON CLUSTER`)
could read every file under `user_files`.

`getSourceEngineOfTableFunction` becomes `getRequiredAccessOfTableFunction`
and returns the required element: `TABLE ENGINE` on a registered source
engine (unchanged), nothing for a registered engine without a source
(unchanged), and unfiltered `READ` and `WRITE` on the source a function
declares when it has no engine. Those are the grants that imply `TABLE
ENGINE` on a source engine, and the element `ATTACH ... FROM '<path>'`
already requires. Both call sites, the local/ON CLUSTER/Replicated initiator
in `getRequiredAccess` and the inherited function of `CREATE TABLE copy AS
src` (also short `ATTACH` and local `RESTORE`), use it. `filesystem` is the
only table function that overrides `getSourceAccessObject`.

Behaviour change: a short `ATTACH` or local `RESTORE` of a table created as
`filesystem(...)` now needs `READ` and `WRITE ON FILE`, as such a `File`
table needs `TABLE ENGINE ON File`. Server startup and replicas are not
affected.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Remove an inline comment that repeated the function's doc comment.

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

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

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

# Sev Finding Verdict Evidence / action
1 ❌ A short ATTACH TABLE t ON CLUSTER (or in a Replicated database) of a detached AS filesystem table needs no FILE grant DISAGREE (separate PR) True, but the stub ATTACH is forwarded before its stored definition is read (InterpreterCreateQuery.cpp:2199-2208), so it skips every definition-derived grant for every engine: a detached ENGINE = File(...) table is re-attached the same way without TABLE ENGINE ON File. The definition is stored metadata written by a user who held the grants. Not changed here
2 ⚠️ The test does not cover short ATTACH and local RESTORE DISAGREE Both reach the one changed line :1073, which the copy case exercises; removing it turns 05842 copy and 05628 restore red. Measured on the fix: both refused with READ, WRITE ON FILE under WRITE ON FILE only, allowed with both grants
3 💡 Use Critical Bug Fix DISAGREE I leave that category to maintainers
4 💡 No copy, on cluster line for filesystem noted Same path as 05628's; measured refused, also with distributed_ddl_entry_format_version = 2
5 💡 filesystem lines assert only ACCESS_DENIED noted The positive control with the same non-FILE grants returns OK

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

Session id: cron:clickhouse-review-slot-8:20261121-164200

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @foadl @nikitamikhaylov, could you review this? It extends the #129605 check of CREATE TABLE ... AS

to a function that declares a source but has no registered engine (only filesystem): such a table now needs unfiltered READ and WRITE ON FILE, so an explicit column list no longer lets a user with only WRITE ON FILE (or no FILE grant via ON CLUSTER) read files under user_files.

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. A user with CREATE TABLE, SELECT ON default.* and WRITE ON FILE runs CREATE TABLE default.fs (path String, name String, content Nullable(String)) AS filesystem(''), then SELECT content FROM default.fs: succeeds and returns the file content on master (573bbb2, every run) and on 26.8.37.168, 26.9.25.15, 26.10.5.57.
b Root cause explained? #129605 requires TABLE ENGINE only for an engine registered in StorageFactory with a source. filesystem has no engine and declares FILE through getSourceAccessObject(), so nothing was required; explicit columns skip the structure inference (the only READ check), the function runs for writing (WRITE only), and ON CLUSTER / Replicated hosts create the table with full access.
c Fix matches root cause? Yes. The grant is required in getRequiredAccessOfTableFunction, the function #129605 uses for TABLE ENGINE, at both of its call sites, as the user and before the query reaches full-access hosts. No check at the read site.
d Test intent preserved / new tests added? New 05842_create_table_as_filesystem_file_grants.sh: refused with WRITE ON FILE only (local, ON CLUSTER, copy) and with READ ON FILE only (local, ON CLUSTER); file() still asks for TABLE ENGINE ON File; allowed with both grants and the content is readable. 05628 is unchanged and passes.
e Both directions demonstrated? Yes. 05842 fails on master (5 lines OK instead of ACCESS_DENIED) and passes with the fix; the repro above is refused with READ, WRITE ON FILE. Mutations: no grant for a function without an engine fails 05842; checking the source before the engine fails 05842 and 05628; removing the setEngine call site fails the copy line of both.
f Fix is general across code paths? Both call sites cover local, ON CLUSTER (also entry format 2), Replicated, CREATE TABLE copy AS src, short ATTACH and local RESTORE. filesystem is the only table function that overrides getSourceAccessObject; registered engines keep TABLE ENGINE. RESTORE ... ON CLUSTER is still not checked on the initiator, as for TABLE ENGINE since #129605.
g Fix generalizes across inputs (params/datatypes/wrappers)? Checked explicit and inferred columns, missing, partial, filtered (FILE('^nothing$')) and full grants. Data types and wrappers do not apply to an access check.
h Backward compatible? (maintainer-approved exception only) Intended RBAC fix with no setting, as #129605: a short ATTACH or local RESTORE of such a table by a user now needs READ and WRITE ON FILE. Existing tables still load at server startup (checked with a restart).
i Invariants and contracts preserved? The function still refuses a definition that cannot be a table before any grant check, and returns at most one element. The local inferred-columns path needs the same grants as before (READ and WRITE); only the error text changes.

Session id: cron:clickhouse-impl-slot-5:20261121-160200

@clickhouse-gh

clickhouse-gh Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [63ce82e]

Summary: ✅


AI Review

Summary

Reviewed head 63ce82edd575478323dd4ad7b02861abb8092756, the complete diff, and prior discussion. The change requires unfiltered READ and WRITE ON FILE when creating or copying persistent tables backed by filesystem. One deliberately deferred authorization gap remains below. No new inline comments were posted because that gap is already acknowledged in the discussion.

Findings

❌ Blockers

  • Cluster restore still bypasses the same FILE grants. src/Interpreters/InterpreterCreateQuery.cpp:1072 [dismissed by author -- https://github.com/Require READ and WRITE ON FILE to create a table as filesystem(...) #133420#issuecomment-7066256590]. For RESTORE ... ON CLUSTER, BackupsWorker::doRestore runs only the access preflight as the initiating user. RestorerFromBackup::checkAccessForObjectsFoundInBackup checks explicit and inner engines, but omits create.as_table_function (src/Backups/RestorerFromBackup.cpp:493). Consequently, a user with restore/create privileges, access to a supplied backup, CLUSTER, and SELECT on the destination can restore an explicit-column AS filesystem('') definition without either FILE grant. With the default distributed_ddl_use_initial_user_and_roles = false, workers create the table with full access, so the new check passes and selecting content reads live files under user_files. Why retained: this is pre-existing and explicitly excluded, but it remains another entrypoint across the authorization boundary this change enforces. The related discussion says a follow-up security issue exists without linking it. Link that issue to document the accepted deferral, or enforce the required grant during restore preflight without breaking restoration of named collections.
Missing context / blind spots
  • ⚠️ No local ClickHouse binary or build directory was available, so the remaining restore bypass was verified by tracing the current code, not by executing it. A focused cluster restore with no FILE grants and a positive control with both grants would verify the behavior.
  • ⚠️ The new 05842_create_table_as_filesystem_file_grants test passed in Fast test. However, Finish Workflow reports that the bugfix-validation jobs were dropped. Automated before/after validation still needs a successful rerun; the author's reproduction and mutation-test results are recorded in the PR discussion.
Final Verdict

Status: ❌ Block

Link the existing follow-up security issue for the deferred cluster-restore bypass, or close that path with focused regression coverage.

@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-rbac Authorization: roles, grants, quotas, row-level security, access checks. labels Nov 21, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 63ce82edd with master 6886cedd2 (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

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

Job report

@groeneai

Copy link
Copy Markdown
Collaborator Author

The RESTORE ... ON CLUSTER gap is the one #129605 left open for TABLE ENGINE (the initiator preflight ignores AS f(...)), in code this PR does not touch. It is tracked in #128996 (comment), where the preflight design waits on an owner decision because parsing f on the initiator breaks restoring its named collection from the same backup.

@alexey-milovidov alexey-milovidov self-assigned this Nov 21, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 21, 2026
Merged via the queue into ClickHouse:master with commit 45ccd89 Nov 21, 2026
575 of 579 checks passed
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 21, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Nov 22, 2026
…-named-collection-unknown-override-keys

Conflicts in the code of ClickHouse#132354: master (ClickHouse#133420) renamed
`InterpreterCreateQuery::getSourceEngineOfTableFunction` to
`getRequiredAccessOfTableFunction`, which returns the whole required grant, and
made `ITableFunction::getSourceAccessObject` public. The replay flag of ClickHouse#132354
is passed to the renamed function at both call sites.
@kolsha kolsha added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Nov 23, 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 Nov 23, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 23, 2026
Backport #133420 to 26.11: Require `READ` and `WRITE ON FILE` to create a table as `filesystem(...)`
kolsha pushed a commit that referenced this pull request Nov 24, 2026
Resolve the conflict in src/TableFunctions/ITableFunction.h: 26.9 keeps the
protected isClusterFunction() next to the getSourceAccessObject() declaration
that #133420 moves to the public section.

26.9 has a third caller of getSourceEngineOfTableFunction, the ON CLUSTER check
of a definition inherited by CREATE TABLE copy AS src, added by the 26.9
backport of #129605 (#133148). Switch it to getRequiredAccessOfTableFunction
like the other two callers.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
kolsha pushed a commit that referenced this pull request Nov 24, 2026
Resolve the conflict in src/TableFunctions/ITableFunction.h: 26.10 keeps the
protected isClusterFunction() next to the getSourceAccessObject() declaration
that #133420 moves to the public section.

26.10 has a third caller of getSourceEngineOfTableFunction, the ON CLUSTER check
of a definition inherited by CREATE TABLE copy AS src, added by the 26.10
backport of #129605 (#133147). Switch it to getRequiredAccessOfTableFunction
like the other two callers.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robot-clickhouse added a commit that referenced this pull request Nov 24, 2026
Cherry pick #133420 to 26.9: Require `READ` and `WRITE ON FILE` to create a table as `filesystem(...)`
robot-clickhouse added a commit that referenced this pull request Nov 24, 2026
robot-clickhouse added a commit that referenced this pull request Nov 24, 2026
Cherry pick #133420 to 26.10: Require `READ` and `WRITE ON FILE` to create a table as `filesystem(...)`
robot-clickhouse added a commit that referenced this pull request Nov 24, 2026
…te a table as `filesystem(...)`
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 24, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 24, 2026
Backport #133420 to 26.10: Require `READ` and `WRITE ON FILE` to create a table as `filesystem(...)`
clickhouse-gh Bot added a commit that referenced this pull request Nov 24, 2026
Backport #133420 to 26.9: Require `READ` and `WRITE ON FILE` to create a table as `filesystem(...)`
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. 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