Repository navigation
Cherry pick #115914 to 26.7: Check the target engine's grant when ATTACH carries a URL engine definition - #115950
robot-clickhouse wants to merge 7 commits into
Conversation
…nition
ENGINE = URL(...) dispatches by URL scheme to File/S3/AzureBlobStorage/HDFS and
re-checks TABLE ENGINE on that target inside the storage creator. The re-check was
gated on LoadingStrictnessLevel::CREATE, so an ATTACH carrying a full definition
skipped it: a user holding only TABLE ENGINE ON URL could attach a File-backed
table and read it, while the equivalent CREATE was correctly denied.
The gate's comment justified itself by claiming it mirrored where the outer engine
privilege is checked. That premise was false. Neither outer site
(InterpreterCreateQuery.cpp:772 and :3522) consults the mode, which is exactly why
ATTACH ... ENGINE = File(...) is denied while the same engine reached through
ENGINE = URL('file://...') was not: the outer check sees only URL, which the user
holds, and the inner check that would have seen File did not run. No source check
exists at read time, so the table remained readable afterwards. The bypass is
reachable on Atomic, the default database engine, through the
ATTACH TABLE t UUID '<uuid>' <definition> form.
Use the fresh-definition predicate that already answers this question in
StorageDistributed, StorageBuffer, StorageNATS and DatabaseBackup, rather than a
mode equality: authorize whenever a definition is introduced now, and skip only
when replaying one already stored on this server. A bare
!isLoadingFromExistingMetadata would not do, because short ATTACH TABLE t and the
tables loaded by ATTACH DATABASE run at mode ATTACH rather than FORCE_ATTACH and
would newly be denied; attach_short_syntax is what separates those.
This also authorizes SECONDARY_CREATE. For replicated-database DDL replay that is a
no-op, since the DDL worker context carries no user and so has full access. For
RESTORE it is a deliberate behaviour change, matching the ruling in
StorageDistributed.cpp:2676-2691 that a restore introduces a definition under a
possibly different user and must be validated like a CREATE.
Extend 04401_url_engine_s3_dispatch_engine_grant with the ATTACH side: file:// and
az:// denials, an http:// positive control, and a short ATTACH arm that pins the
exempt half, so a later simplification to the bare predicate cannot pass. Anchor
the denial assertions on the privilege sentence as well; the previous pattern could
match the failing query echoed back by the client, which contains both the engine
name and the word grant, and so would have accepted a query that was never denied.
The two arms asserting an ATTACH was permitted inferred that from the absence of a denial in the output. Every other failure leaves the same empty match, so a parse error, a UUID collision or any storage-construction throw printed the allowed label; the script has no set -e and the trailing DROP TABLE IF EXISTS succeeds either way, so nothing downstream exposed it. Measured on the same binary with a failure injected into each arm: the previous form reported OK while the ATTACH it was asserting had failed with TABLE_ALREADY_EXISTS, and with CANNOT_GET_CREATE_TABLE_QUERY for the short ATTACH. Both arms now also assert the table is present in system.tables, and an unrelated failure prints a third label that is absent from the reference file, so it surfaces as a mismatch rather than picking either existing label. This matters most for the short ATTACH arm, which is what pins the exempt half of the predicate; the mutation dropping that half still reddens it, through the denial branch rather than the new failure branch. RESTORE is the one deliberate behaviour change here and had no arm of its own. Add one following the structure of 04401_remote_storage_engine_restore_access, which covers the equivalent ruling for the Remote engine: deny without the target engine grant, assert nothing was created, grant it, then succeed. On the pre-fix binary the first two invert, so the arm is not vacuous, while the granted retry passes on both and shows this is a grant requirement rather than a hard break. The backup destination carries the shell's pid. A destination may not be written twice and no statement deletes one, and a fixed --database stops the unique name from varying between runs of a single test, so without the suffix a repeated run fails BACKUP_ALREADY_EXISTS: measured 1 of 3 runs passing without it against 3 of 3 with it.
The delegate engine check in the URL scheme dispatcher treats attach_short_syntax as proof that the definition it is about to build came from this server's own metadata, and therefore skips the target TABLE ENGINE grant. That inference is sound for every parsed query: no parser sets the flag, and all of its setters run after parsing, where the interpreter is replaying a definition it already stored. It is not sound for a clickhouse_json payload, where the flag is an input the user supplies directly. readJSON rejected it only when the query was not an ATTACH, so an ATTACH payload could set it freely, keep its own storage clause (the interpreter replaces the query from stored metadata only when no storage definition is present), and reach DatabaseOnDisk's short-syntax early return with a user-supplied file:// definition marked as replayed metadata. That is reachable on Atomic, the default, because DETACH only nulls the value in the UUID map while the mapping check tests for the key: create an http:// table with a known UUID, detach it, then submit the forged definition under that UUID. Reject the flag unconditionally instead, generalizing the guard that was already there rather than adding a second mechanism. Nothing legitimate regresses: writeJSON emits the field unconditionally, so every parseQueryToJSON round trip carries false, and the interpreter paths that do set it never serialize to JSON on the request path. The existing AST JSON validation test gains the ATTACH analogue of its CREATE arm.
… to exist The arms asserting a statement was permitted checked that the table is present afterwards. A table left behind by an interrupted run satisfies that without the statement having run at all: the ATTACH fails TABLE_ALREADY_EXISTS, the failure does not match the denial pattern, and the arm prints the label that is supposed to mean the grant was accepted. Measured on one binary with a leftover holding the arm's name and UUID, the arm passed with output byte-identical to the reference while its ATTACH had returned 57. Reachable because the unique name only varies with the database name, and the stress runner passes a fixed one, so repeated runs of a test reuse the same table names. Clear each target first, so its presence afterwards is attributable to the statement under test, and require exit status 0 in addition to the absence of a denial. The count probe now reports a failure of its own query rather than returning it as absence, which the arms asserting nothing was created would otherwise accept as their pass.
…ape" This reverts commit 10b0d3b. PR #114171 already closes the same hole in a stronger form: it rejects the key's presence rather than only a true value, and stops writeJSON emitting the field at all, so there is no forgeable input left. Its hunks touch these exact lines and conflict only with this commit, and its removal of the field would silently void the test arm added here (the arm injects into a needle that leaves the payload). The URL delegate engine check this branch is about does not depend on the revert: attach_short_syntax remains a sound provenance signal for every parsed query, which is the only shape reachable without the default-off enable_json_ast_dialect setting.
…denied The http CREATE arm read "no access-denial text matched" as success. It captured no exit status and did not check that the table was there, so a parse error, a table left by an interrupted run, a host-filter refusal or any other non-access failure printed the permitted label and the test passed. That arm is the file's only guard against the four denial arms passing for the wrong reason: a user holding no engine grant at all is denied everywhere, and only this arm would notice. A vacuous positive control therefore lets a regression that breaks URL-engine CREATE outright show all-green. It now uses the shape the ATTACH http arm already uses on the same statement: the target is dropped first as the admin, so its absence makes the presence afterwards attributable to this statement, and the permitted label requires exit status 0 and the table's presence in addition to the absence of a denial. The reference file is unchanged; the new failure label is absent from it, so it surfaces as a diff. Reproduced before fixing: with a leftover table holding the arm's name, on one binary, the old shape passes while the new one reports rc=57 TABLE_ALREADY_EXISTS.
…atch-engine-grant Check the target engine's grant when ATTACH carries a URL engine definition
|
The bypass is live on the shipped
The conflict is only the two test files, and only because Validated in both directions against a stateless-equivalent server, having first asserted live that
So the port is not a silent no-op on this branch. I cannot resolve the conflicts here, because I have no push access to this repository and the |
… carries a URL engine definition
The URL engine dispatches by scheme to File/S3/AzureBlobStorage/HDFS. The
delegate's TABLE ENGINE privilege was re-checked only when the mode was
CREATE, so a user holding TABLE ENGINE ON URL alone could ATTACH a table with
a full URL('file://...') definition and then read it, while the equivalent
CREATE and a direct ENGINE = File(...) ATTACH were both denied. The outer
engine check is not mode-gated, so the inner one was the only gap.
The predicate now exempts only a definition replayed from this server's own
metadata: server startup, force-restore, a short ATTACH TABLE t, and the
tables an ATTACH DATABASE loads. Those were validated when the table was
created and must stay loadable after a revoke. Every other statement, ATTACH
with a definition and RESTORE included, supplies a fresh definition and is
checked.
The cherry-pick #115950 conflicts only because master renamed
04401_url_engine_s3_dispatch.sh to 04401_url_engine_s3_dispatch_engine_grant.sh,
which makes the pair a modify/delete on this branch. The source hunk applies
unchanged: tryDispatchURLEngineByScheme is byte-identical between master and
26.7. This is that resolved tree as a single commit on the 26.7 tip.
Validated in both directions on a server configured like the stateless one,
after asserting that table_engines_require_grant denies a no-grant user and
that the backups disk accepts a backup. Against the released 26.7.6.8 binary
the test fails on attach-file-engine-denied, attach-azure-engine-denied,
restore-file-engine-denied and restore-rejected-not-created, the same four
lines it inverted against pre-fix master; against the fixed build it passes
with no diff. The bypass itself reproduces on 26.7.6.8: ATTACH of a
URL('file://...') table is accepted and the following SELECT returns the file
contents, while both the CREATE and a direct File engine ATTACH are denied.
The four older branches are unaffected and need no backport: URL scheme
dispatch does not exist there, so a URL('file://...') read fails with
Code: 36, Unknown protocol in the URL.
|
Superseded by #115966, which is merged into This robot cherry-pick is Closing. |
Original pull-request #115914
Do not merge this PR manually
This pull-request is a first step of an automated backporting.
It contains changes similar to calling
git cherry-picklocally.If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.
The check results does not matter at this step - you can safely ignore them.
Troubleshooting
If the conflicts were resolved in a wrong way
If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:
pr-cherrypicklabel from the PRYou also need to check the Original pull-request for
pr-backports-createdlabel, and delete if it's presented thereThe PR source
The PR is created in the CI job