Repository navigation
Do not defer an Alias table in a database with lazy_load_tables - #117490
Conversation
`DatabaseOrdinary::shouldLazyLoad` excludes ordinary views, materialized views, dictionaries, parameterized views, TimeSeries tables and table-function tables from lazy deferral, but not `Alias`. So in a database created with `lazy_load_tables = 1` an `ENGINE = Alias` table is attached as a `StorageTableProxy`, and the catalog keeps handing that proxy out for the whole life of the table. `IStorage::as` is `typeid_cast`, which requires exact `typeid` identity, so `as<StorageAlias>` answers `nullptr` for that proxy. Ten checks are shaped `alias && !alias->isTargetTableGranted(...)`, and every one of them therefore short-circuits to "allowed": `SHOW CREATE TABLE`, `DESCRIBE`, and `system.tables`, `columns`, `constraints`, `projections`, `data_skipping_indices`, `completions`, plus `loop` on a table function. A user holding only `SHOW COLUMNS` on the alias row could read the target's column names and types without any grant on the target. The persisted `CREATE` of an `Alias` carries the column list resolved when it was created, so this leaks the full schema even before the table has ever been touched. For every other engine the proxy defers only data access, which is the point of `lazy_load_tables`. For `Alias` the storage type itself is the security-relevant fact, so exclude it, next to the exclusions that exist for the same reason. Fixing the one decision that constructs the proxy restores all ten sites, and any added later, instead of re-deriving "is this an alias, and what is its target" from the persisted `CREATE` AST at each of them - which would also make the three iterating system tables read a metadata file per table per query. Nothing is deferred by loading an alias eagerly: `StorageAlias`'s constructor records the target's database and table name and opens nothing. `ATTACH DATABASE` of a database of 500 alias tables measures 0.24 s eagerly against 0.20 s behind proxies. This also fixes `ATTACH DATABASE` failing with `EMPTY_LIST_OF_COLUMNS_PASSED` when a lazily loaded `Alias` table's target does not exist: such a table persists with no column list, which `StorageTableProxy` cannot represent. On a server that had already restarted with such metadata, every query on `system.tables` or `system.columns` then failed, and the database could not be dropped. Rows were never exposed. `StorageAlias::read` resolves the target through `getTargetTable` with a `SELECT` access check, which runs whatever the type check answered. Co-Authored-By: Claude Opus 5 <[email protected]>
The `Documentation` block for `lazy_load_tables` enumerates the engines that are always loaded eagerly regardless of the setting, and this change adds `Alias` to that set, so the sentence was incomplete for exactly the behaviour it describes. That string is the source of truth for the autogenerated region of `docs/reference/statements/create/database.mdx`, so only the registration in `src` changes here and the generated page is left to the docs workflow. Co-Authored-By: Claude Opus 5 <[email protected]>
…rage The comment above the repeated probe battery said a first access replaces the lazy proxy with the real storage. It is the other way round: `getNested` assigns `nested` and the catalog keeps handing out the `StorageTableProxy`, which is precisely why the leak this test covers survived a first access. Co-Authored-By: Claude Opus 5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex), plus my own cold review of the
Severity: ❌ blocker / Round 2 re-reviewed the two follow-up commits cold and re-ran the independent review against them: Also checked and left alone: the exclusion is keyed on the engine name in the Session id: cron:clickhouse-review-slot-9:20260901-092700 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-7:20260901-063001 |
|
Workflow [PR], commit [cddeae1] Summary: ✅
AI ReviewSummaryThis PR excludes Findings❌ Blockers
Final VerdictChanges requested. LLVM Coverage ReportMeasured on commit cddeae1.
Changed lines: Changed C/C++ lines covered: 9/10 (90.00%) · Uncovered code |
|
cc @alesapin @evillique, could you review this? In a database with |
`DatabaseOrdinary::shouldLazyLoad` has excluded `ENGINE = TimeSeries` tables from lazy deferral since before this branch, but the `lazy_load_tables` description, which is the source of truth for the autogenerated `CREATE DATABASE` reference page, did not list them among the eager-load exceptions. Parameterized views are deliberately still not named there. `ASTCreateQuery::isParameterizedView` is `is_ordinary_view && select && select->hasQueryParameters()`, so a parameterized view already satisfies the `is_ordinary_view` disjunct of the same condition, and "Views" covers it. Naming it separately would suggest it is a distinct kind needing its own exception.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units8 translation units recompiled, 20 s compile time in total, 8 of them have a recent master baseline. |
CI finish ledger - 097444cEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20260901-173000 |
| # Test an `Alias` in a database with `lazy_load_tables`, which must not be deferred behind a proxy. | ||
| # `system.completions.belongs` is the bare table name, so the alias needs a server-unique name for | ||
| # the completions count below to mean this table and not a same-named alias in another database. | ||
| lazy_database="lazy_db_${CLICKHOUSE_TEST_UNIQUE_NAME}" |
There was a problem hiding this comment.
Done in 79f5ac5, cut at the file's own boundary between table-scoped and database-scoped grants.
03636_storage_alias_metadata_access_rights keeps the same-database Alias battery, deny and allow, at 251 lines. The new 03636_storage_alias_metadata_database_grants takes the cross-database Alias and the lazy_load_tables batteries, 138 lines, both of which read a target through a database-level grant on the alias row alone.
Nothing was rewritten: the two reference files concatenate byte for byte into the old one, and the moved and the retained script blocks are byte-identical to their originals. Only the setup block changed, losing two DROP DATABASE IF EXISTS lines for the databases that moved out.
Every arm that depends on this fix is now in the new file, so on pristine master it fails and the retained one passes. Both stay parallel-safe: 60/60 green at 8 jobs, 20 runs each.
If 251 lines is still too large, the next seam is between the no-permission and the with-permission batteries; say so and I will cut there.
The file had grown to 369 lines, the 99.6th percentile of stateless `.sh` tests. Cut it at its own boundary between table-scoped and database-scoped grants. `03636_storage_alias_metadata_access_rights` keeps the same-database `Alias` battery, deny and allow, at 251 lines. The new `03636_storage_alias_metadata_database_grants` takes the cross-database `Alias` and the `lazy_load_tables` batteries, 138 lines, which share the property that the reader holds a database-level grant on the alias row and nothing at all on the target. Coverage is unchanged: the two reference files concatenate byte for byte into the old one, and both the moved and the retained script blocks are byte-identical to their originals. Only the setup block changed, losing the two `DROP DATABASE IF EXISTS` lines for the databases that moved out. Every arm that depends on this PR's fix now sits in the new file: on pristine master that file fails and the retained one passes. Asked for by @ PedroTadim in review.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
|
||
| /// A lazy proxy would hide the `Alias` type from the target-table access checks, so the alias's | ||
| /// metadata could be read without a grant on the target. Load it eagerly, as for views. | ||
| if (query.storage && query.storage->engine && query.storage->engine->name == "Alias") |
There was a problem hiding this comment.
lazy_load_tables no longer hides StorageAlias behind StorageTableProxy, but the alias-metadata invariant still has a live bypass through the loop table function. TableFunctionLoop::getActualTableStructure only checks SHOW COLUMNS on the alias and then returns storage->getInMemoryMetadataPtr(...) (src/TableFunctions/TableFunctionLoop.cpp:101-119); for a real StorageAlias, that getter forwards the target schema (src/Storages/StorageAlias.h:124-129). So a user with SHOW COLUMNS on the alias but no privilege on the target can still learn the target columns via DESCRIBE TABLE loop(db, alias) or CREATE TABLE dst AS loop(db, alias) (src/Interpreters/InterpreterCreateQuery.cpp:1081). The loop structure path needs the same target-table SHOW COLUMNS guard as direct alias DESCRIBE, plus a focused regression test.
There was a problem hiding this comment.
Confirmed, and it is already fixed, just not on this branch: no lazy proxy is involved on that path, so it reproduces on a plain StorageAlias too. I measured it on this branch's binary and on its base and it is unchanged in both directions by this change.
The guard belongs exactly where you point, and #116793 puts it there: checkSourceStorageAccess between the tryGetTable and the getInMemoryMetadataPtr in TableFunctionLoop::getActualTableStructure, requiring SHOW COLUMNS on the Alias target. Its test 05046_table_function_source_access.sh already carries the arm you ask for, DESCRIBE loop(...) over an alias whose target is not granted, plus a granted arm so the check cannot end up stronger than SHOW COLUMNS.
CREATE TABLE dst AS loop(db, alias) needs no second guard: InterpreterCreateQuery.cpp:1081 resolves the structure through getActualTableStructureWithAccess, which reaches that same getActualTableStructure, so one line covers both surfaces.
Keeping this PR to the lazy_load_tables proxy gap.
There was a problem hiding this comment.
Current cddeae1e still has the original structure-path leak. TableFunctionLoop::getActualTableStructure at src/TableFunctions/TableFunctionLoop.cpp:114-123 checks SHOW COLUMNS only on the alias itself and then calls storage->getInMemoryMetadataPtr(...); for StorageAlias, getInMemoryMetadataPtr at src/Storages/StorageAlias.h:125-131 forwards the target metadata. So DESCRIBE loop(db, alias) and CREATE TABLE ... AS loop(db, alias) still disclose the target schema on this PR head.
CI finish ledger - d20ee65Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20260902-193000 |
…les-show-columns-gate Conflicts in the 03636 pair only, both from the split that landed with ClickHouse#117490: the "database-level access shortcuts" section moved out to 03636_storage_alias_metadata_database_grants and a "CREATE TABLE ... AS an Alias" section took its place. Kept master's new section and this branch's "table and persisted metadata with target permission" block, which asserts the seven flags come back once the target grants SHOW COLUMNS.
|
@groeneai @nauu — ClickGap found the following in this PR:
Close anything that's wrong or already addressed. |
|
Answering the ClickGap comment above. Both findings are real. I reproduced each on master Both are duplicates of issues filed two weeks earlier, and both already have open fixing PRs:
Neither is caused by this PR. Its whole diff is On the request to close whatever is wrong or already addressed: nothing here is wrong, both are already addressed by the PRs above, and I do not close issues myself. Closing them as duplicates of #117440 and #117420 is the right end state whenever a maintainer agrees. |
Reproducer-- server; the default user needs access_management = 1
CREATE DATABASE tgt;
CREATE TABLE tgt.t (secret_col UInt64) ENGINE = MergeTree ORDER BY secret_col;
CREATE DATABASE lazy ENGINE = Atomic SETTINGS lazy_load_tables = 1;
CREATE TABLE lazy.a ENGINE = Alias('tgt', 't');
DETACH DATABASE lazy;
ATTACH DATABASE lazy;
CREATE USER u NOT IDENTIFIED;
GRANT SHOW TABLES, SHOW COLUMNS ON lazy.* TO u; -- nothing on tgt.t
-- as u: clickhouse-client --user u
DESCRIBE TABLE lazy.a;
-- 26.8.13.2: returns `secret_col UInt64`; system.tables reports lazy.a as TableProxy.
-- SHOW CREATE TABLE lazy.a and system.columns leak the same way (checked on 26.8.12.53).
-- Same alias in a regular Atomic database, or 26.9+: ACCESS_DENIED
-- (Not enough privileges to describe metadata exposed by lazy.a).Results:
Backport the fix to CC component owner: @alexey-milovidov @scanhex12 Analysis metadata
Introducing change: not identified
|
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
In a database with
lazy_load_tables = 1, a table with theAliasengine was attached behind a lazy proxy, which hid theAliastype from the checks that authorize alias metadata against the target table. A user holding onlySHOW COLUMNSon the alias could read the target's column names and types throughSHOW CREATE TABLE,DESCRIBEand the metadata system tables without holding any grant on the target. Such tables are now loaded eagerly, as views and dictionaries already were. This also fixes re-attaching a database holding anAliastable that was created while its target did not exist: it failed withEMPTY_LIST_OF_COLUMNS_PASSED, after whichsystem.tablesandsystem.columnsthrew for every query and the database could not be dropped.Description
DatabaseOrdinary::shouldLazyLoadexcludes views, materialized views, dictionaries, parameterized views, TimeSeries and table-function tables from lazy deferral, but notAlias. For every other engine the proxy defers only data access, the point oflazy_load_tables. ForAliasthe storage type itself is the security-relevant fact: ten checks identify an alias by its storage type and then require the grant on its target, andas<StorageAlias>istypeid_cast, so it answersnullptrfor the proxy and all ten short-circuit to "allowed". It does not end at first load: the catalog keeps handing out the proxy afterwards.One exclusion alongside the existing ones restores all ten sites and any added later. Nothing is deferred by loading an alias eagerly:
StorageAlias's constructor records the target's name and opens nothing, andATTACH DATABASEof 500 alias tables measures 0.24 s eagerly against 0.20 s behind proxies. Rows were never exposed in either proxy state:StorageAlias::readre-checksSELECTon the target.Both new test arms, untouched and first-accessed, fail on master.
The changelog's second consequence has the same cause: such an alias persists with no column list, which the proxy's constructor rejects. It has no test arm on purpose: without the fix, one such table makes every later unfiltered
system.tablesquery on that server throw, failing unrelated tests sharing it, including underBugfix validation.#113389 replaces this blocklist with a per-engine
supports_deferred_loadallowlist thatAliasnever opts into, so these five lines can be dropped when it lands. Open since 2026-08-04, and the behaviour ships on all four active release branches, so I am not waiting.Related: #113389
Related: #117448
Workflow [PR]
Sync PR [sync-upstream/pr/117490]
Version info
26.9.1.1567(included in26.9and later)