Skip to content

Do not require privileges on the temporary table of CREATE OR REPLACE - #117967

Merged
alexey-milovidov merged 1 commit into
masterfrom
fix-create-or-replace-privileges
Sep 5, 2026
Merged

alexey-milovidov merged 1 commit into
masterfrom
fix-create-or-replace-privileges

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Sep 3, 2026 •

Copy link
Copy Markdown
Member

CREATE OR REPLACE TABLE / VIEW / DICTIONARY and REPLACE TABLE build the new object under an internal
name (_tmp_replace_<hash>_<random>) and publish it with a RENAME/EXCHANGE. Several steps of that
machinery were authorized against the internal name: the publishing rename asked for SELECT and
DROP TABLE on it, the populating INSERT SELECT asked for INSERT on it, the CLONE AS attach asked for
ALTER DELETE and INSERT on it, and the cleanup DROP of a failed replace asked for DROP TABLE on it.
The name is random, so no grant can ever cover it, and the statement failed for anyone without wildcard
grants over the whole database:

Code: 497. DB::Exception: foo: Not enough privileges. To execute this query, it's necessary to have
the grant SELECT, DROP TABLE ON default._tmp_replace_61cef65d835f4faf_iftufikx6620edcb. (ACCESS_DENIED)

The same rename also asked for CREATE TABLE and INSERT on the final name, which are not the grants of the
object's kind, so replacing a view demanded INSERT, CREATE TABLE ON default.the_view. And because the
cleanup DROP was denied too, a failed replace left its temporary table behind (Cannot DROP temporary table: ... it's necessary to have the grant DROP TABLE ON default._tmp_replace_*).

Now every privilege is checked against a user-visible name, and the internal steps on the temporary name are
not authorized against it:

  • the publishing rename and the drops of the temporary name skip their own access check;
  • CREATE/DROP of the object's own kind on the final name is checked up front, as before, and DROP of the
    replaced object's kind is checked on its real name right before the swap, as before;
  • INSERT on the final name is checked, as the user, before a populating AS SELECT, and SELECT on the
    sources is still checked by the populate itself;
  • CLONE AS checks SELECT on the source and ALTER DELETE/INSERT on the final name, as the user.

So CREATE OR REPLACE now needs exactly the grants of the plain statement it is equivalent to, plus the
DROP of what it replaces, and table-scoped grants are enough.

One case becomes stricter rather than looser: a populating CREATE OR REPLACE TABLE ... AS SELECT now
requires INSERT on the final name. Previously it required INSERT on the temporary name, so users who
worked around this bug by granting on the _tmp_replace* prefix (instead of on the table) need the grant on
the table. Without that check CREATE TABLE + DROP TABLE alone would let a user write arbitrary data into
a table they cannot INSERT into.

The publishing rename of a REPLACE keeps running as the user rather than under an internal context: it has to
invalidate the storage cache of the user's query context for the names it swapped, which is what keeps the
replaced materialized view subscribed to its source (issue #108726, covered by
04492_create_or_replace_materialized_view_populate_keeps_subscription).

Tests 05060_create_or_replace_scoped_grants and 05061_create_or_replace_clone_as_scoped_grants cover the
table, view, AS SELECT and CLONE AS forms with table-scoped grants, and check that a user missing DROP,
INSERT or the source SELECT is still denied and leaves no temporary table behind.

Closes: #90919

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

Fix CREATE OR REPLACE TABLE/VIEW/DICTIONARY and REPLACE TABLE requiring privileges on the internal
temporary table they create (_tmp_replace_*). Its name is random, so no grant could cover it and the query
failed with ACCESS_DENIED unless the user held wildcard grants on the whole database. The required
privileges are now checked against the user-visible names only: replacing an object needs the CREATE and
DROP grants of its own kind, plus INSERT on it when the statement populates it.


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

Version info

  • Merged into: 26.9.1.808 (included in 26.9 and later)

`CREATE OR REPLACE TABLE` / `VIEW` / `DICTIONARY` and `REPLACE TABLE` build the
new object under an internal `_tmp_replace_<hash>_<random>` name and publish it
with a `RENAME`/`EXCHANGE`. Several steps of that machinery were authorized
against the internal name: the publishing rename asked for `SELECT` and
`DROP TABLE` on it, the populating `INSERT SELECT` asked for `INSERT` on it, the
`CLONE AS` attach asked for `ALTER DELETE` and `INSERT` on it, and the cleanup
`DROP` of a failed replace asked for `DROP TABLE` on it. The name is random, so
no grant can cover it, and the statement failed with `ACCESS_DENIED` for anyone
without wildcard grants over the whole database. A failed replace additionally
stranded its temporary table, because its cleanup `DROP` was denied too.

The same rename also asked for `CREATE TABLE` and `INSERT` on the final name,
which are not the grants of the object's kind, so replacing a view demanded
`INSERT, CREATE TABLE` on the view.

Now the internal steps skip their own access check, and every privilege is
checked against a user-visible name: `CREATE`/`DROP` of the object's own kind on
the final name up front, `DROP` of the replaced object's kind on its real name
before the swap, `INSERT` on the final name before a populating `AS SELECT`, and
`SELECT` on the source plus `ALTER DELETE`/`INSERT` on the final name for
`CLONE AS`. `CREATE OR REPLACE` therefore needs exactly the grants of the plain
statement it is equivalent to, plus the `DROP` of what it replaces.

A populating `CREATE OR REPLACE TABLE ... AS SELECT` now requires `INSERT` on
the final name, where it used to require `INSERT` on the temporary one. Without
that check `CREATE TABLE` + `DROP TABLE` alone would let a user write arbitrary
data into a table they cannot `INSERT` into.

The publishing rename of a REPLACE keeps running as the user rather than under
an internal context: it has to invalidate the storage cache of the user's query
context for the names it swapped, which is what keeps a replaced materialized
view subscribed to its source (#108726).

Closes: #90919

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [0c0ece9]

Summary: ✅


AI Review

Summary

The PR fixes the _tmp_replace_* privilege problem across the CREATE OR REPLACE / REPLACE temporary-table flow, and the implementation trace itself looks coherent. I did not find a functional bug in the current code, but two of the newly-moved RBAC paths are still under-tested: there is no grant-based regression for populated CREATE OR REPLACE MATERIALIZED VIEW, and the new CLONE AS coverage never removes source SELECT, so it would not catch a regression in the moved source-side check.

Tests
  • ⚠️ CREATE OR REPLACE MATERIALIZED VIEW ... POPULATE now goes through the broadened isCreateQueryWithImmediateInsertSelect() access precheck, but the added coverage only exercises table AS SELECT. Add a focused positive/negative RBAC case for a populated materialized view.
  • ⚠️ 05061_create_or_replace_clone_as_scoped_grants.sh denies only the target-side grants. Add a second negative case without SELECT on the clone source, because setSkipAccessCheck(true) means the source-side privilege is now enforced only by the new manual getRequiredAccessForCommand(...) check.
Final Verdict

⚠️ Needs more targeted RBAC coverage before merge.

LLVM Coverage Report

Measured on commit 0c0ece9.

Metric Baseline Current Δ
Lines 88.70% 88.70% +0.00%
Functions 92.10% 92.10% +0.00%
Branches 81.00% 81.00% +0.00%

Changed lines: Changed C/C++ lines covered: 50/51 (98.04%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 3, 2026
${CLICKHOUSE_CLIENT} --query "SELECT x FROM ${db}.v"

echo "-- [CREATE TABLE, DROP TABLE, INSERT ON db.t_populated] replacing with a populating SELECT must succeed:"
run_granted "CREATE OR REPLACE TABLE ${db}.t_populated ENGINE = MergeTree ORDER BY a AS SELECT a FROM ${db}.src"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

isCreateQueryWithImmediateInsertSelect() also covers CREATE OR REPLACE MATERIALIZED VIEW ... POPULATE, so this RBAC change is no longer table-only. The new coverage only exercises CREATE OR REPLACE TABLE ... AS SELECT, which leaves the populated materialized-view path unproven: a regression there could still authorize against _tmp_replace_* and CI would not notice. Please add one focused POPULATE grant test, including a negative case without the required INSERT.

CREATE TABLE cloned_denied (a Int32) ENGINE = MergeTree ORDER BY a;

-- Table-scoped grants only: none of them can cover the internal \`_tmp_replace_*\` name.
GRANT SELECT ON ${db}.src TO ${granted}, ${nogrant};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The denied user still has SELECT on src, while the code now bypasses InterpreterAlterQuery's built-in access check and replays REPLACE PARTITION access manually before setSkipAccessCheck(true). That means this test would still fail even if the new manual check accidentally dropped the source-side SELECT requirement. Please add a second denied case without SELECT on the clone source so the moved RBAC check is fully pinned down.

@clickhouse-gh

clickhouse-gh Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 0c0ece920 with master 1ddd0ab00 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of a590a0101; 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

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

Job report

@clickhouse-gh clickhouse-gh Bot added the comp-rbac Authorization: roles, grants, quotas, row-level security, access checks. label Sep 4, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 40 queries analysed

no significant changes detected. K_source=6 K_base=30 flagged=0/65

clickbench

⚠️ 7 inconclusive

Flagged queries (7 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 16 not_sure 600 700 +16.8% <0.0001
⚠️ 17 not_sure 239 263 +10.0% <0.0001
⚠️ 18 not_sure 1064 1246 +17.1% 0.0021
⚠️ 28 not_sure 1048 1128 +7.6% 0.0001
⚠️ 32 not_sure 1313 1459 +11.1% 0.0023
⚠️ 33 not_sure 1225 1317 +7.5% 0.0104
⚠️ 34 not_sure 1205 1307 +8.5% 0.0024

Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 3cf2cf99-3fce-458b-8a44-e4812256d809
  • MIRAI run: 61d937d2-383d-4b74-8e29-7b1c1f65c3c4
  • PR check IDs:
    • clickbench_1325276_1788633874
    • clickbench_1325282_1788633874
    • clickbench_1325288_1788633874
    • tpch_adapted_1_official_1325299_1788633874
    • tpch_adapted_1_official_1325311_1788633874
    • tpch_adapted_1_official_1325346_1788633875

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ok, reasonable.

@alexey-milovidov alexey-milovidov self-assigned this Sep 5, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 5, 2026
Merged via the queue into master with commit e79e9ea Sep 5, 2026
174 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-create-or-replace-privileges branch September 5, 2026 20:23
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 5, 2026
fm4v pushed a commit that referenced this pull request Nov 2, 2026
Resolve the cherry-pick of #124477 onto 26.8.

The only conflict is context: on master the new settings list sits above
fillTableIfNeeded, whose signature #117967 changed (published_table_name);
26.8 keeps its own signature (skip_target_insert_access_check), so the
resolution adds only the lines #124477 adds and does not bring #117967 along.
The test files merged cleanly.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-rbac Authorization: roles, grants, quotas, row-level security, access checks. 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.

CREATE OR REPLACE TABLE / VIEW requires privileges on internal temporary table

2 participants