Repository navigation
Require privileges on an Alias target in the loop table function - #116873
PedroTadim merged 2 commits into
Conversation
`loop` authorizes the table name it is given, but an `Alias` publishes its target's metadata eagerly while the target's own authorization happens lazily, inside the synthetic `SELECT` that `LoopSource::initLoop` builds. `StorageLoop`'s constructor copies the inner storage's metadata into its own, and for a `StorageAlias` that metadata is the target's, so the response header is populated before any row is read. A query that pulls no rows never reaches the lazy check and receives the target's column names and types. A user holding `SELECT, SHOW COLUMNS` on the alias and nothing on the target therefore gets the target's structure from `SELECT * FROM loop(db, ali) LIMIT 0`, from `EXPLAIN header = 1 SELECT * FROM loop(db, ali) LIMIT 0` and from `DESCRIBE TABLE (SELECT * FROM loop(db, ali) LIMIT 0)`, while every direct path denies the same user: `DESCRIBE ali`, `SHOW CREATE TABLE ali` and `SELECT * FROM ali LIMIT 0` all return `ACCESS_DENIED`. Rows are never disclosed, so the leak is metadata only. The check goes in `TableFunctionLoop::executeImpl`, right after the existing table-level `SELECT` check on the given name and before `StorageLoop` is constructed. `PlannerJoinTree.cpp` states that access rights for table functions are checked in `ITableFunction::execute`, so that is the designated seam, and it is not gated by `only_analyze`, which is what covers the `DESCRIBE (subquery)` vector. `isTargetTableGranted` is a name-based grant probe that never resolves the target, so a dangling `Alias` still reports `UNSUPPORTED_METHOD` to a caller granted the dropped target's name, and full-access behaviour is unchanged. The tier is table-level `SELECT`, matching what `loop` already demands of the name it is given. Table-level `SELECT` implies table-level `SHOW_COLUMNS`, so no caller receives a column list it could not already have read with `DESCRIBE`. Table-level `SHOW_COLUMNS` and `SHOW_TABLES` were both rejected as tiers because a column grant does not imply the former and does imply the latter, so each admits or rejects the wrong caller. Accepted cost: a caller holding only a column-level `SELECT` grant on the target can no longer use `loop` over the alias, although reading the alias directly still works. One arm of `04945_loop_over_table_expression_without_columns_access` changes from `UNSUPPORTED_METHOD` to `ACCESS_DENIED`: its alias is dangling and the caller holds nothing on the dropped target, and a direct `DESCRIBE` of that alias already denies. Arms are added to show that this is a tightening and not a loss. ClickHouse#116793 closes the structure seam of the same invariant, `DESCRIBE loop(db, alias)`, at the same tier; this closes the read seam. An unauthorized `CREATE TABLE ... AS loop(...)` still succeeds, because `ITableFunction::execute` defers `executeImpl` through a closure carrying the global context; that belongs to the source-object seam ClickHouse#116793 adds, not here. Co-Authored-By: Claude Opus 5 <[email protected]>
`.claude/CLAUDE.md` requires literal names from the ClickHouse SQL language and class names to be wrapped in inline code blocks in comments, not only in documentation and commit messages. The edit changes no code, and the recompiled object is byte-identical, so the binary keeps Build ID `3a2159fbe20de9d17f50516903477a633f573075`. 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; 4 findings;
Severity: ❌ blocker / Also noted in my own review, not blocking: Session id: cron:clickhouse-review-slot-11:20260828-095100 |
Pre-PR validation gate (click to expand)
Validation runs: 50 randomized runs of the changed test (50 OK / 0 FAIL); the whole Session id: cron:clickhouse-impl-slot-41:20260828-081200 |
|
Workflow [PR], commit [7bba89b] Summary: ✅
AI ReviewSummaryThis PR adds a target-side Final Verdict
LLVM Coverage ReportMeasured on commit 7bba89b.
Changed lines: Changed C/C++ lines covered: 9/9 (100.00%) · Uncovered code |
|
cc @pufit @vitlibar, could you review this? |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units18 translation units recompiled, 23 s compile time in total, 18 of them have a recent master baseline. Median compile-time ratio to the baselines is ×1.10 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
CI finish ledger - 7bba89bEvery failure below has an owner: a fixing PR, or a full-effort fix task whose fixing-PR link 362 check-runs over TWO concurrent workflow runs on this same tree (
Session id: cron:our-pr-ci-monitor:20260828-160000 |
8342360
… its columns
`StorageMerge::getColumnsDescriptionFromSourceTablesImpl` authorizes every table
its regexp matches against that table's own id, `isGranted(SHOW_TABLES, ...)` as
the existence tier and `checkAccess(SHOW_COLUMNS, ...)` as the column tier, and
then reads the table's metadata. For an `ENGINE = Alias` row the metadata that
comes back is the target's: `StorageAlias::getInMemoryMetadataPtr` resolves the
target with `tryGetTargetTable` and forwards it with no check of any kind. So the
privilege that governs reading those columns, `SHOW COLUMNS` on the target, is
never consulted. A check is present and simply bound to the wrong object, which
is why this is a bypass of an existing check rather than a missing feature.
A user holding only `SHOW TABLES, SHOW COLUMNS` on the alias's database, and
nothing at all on the target, obtains the target's column names and types from
`DESCRIBE merge('nl', '^al$')`, while `DESCRIBE nl.al`, `SHOW CREATE TABLE nl.al`
and `system.columns` deny that same user the same information. Rows are never
disclosed, because `StorageAlias::read` re-checks `SELECT` through
`getTargetTable`; the leak is metadata only.
The target is bound with the same privilege at the site that consumes the
metadata. That is the seam all fourteen existing alias guard sites use, and the
shape approved in ClickHouse#116647; making `getInMemoryMetadataPtr` itself check was
rejected because it is called from internal full-access paths and system
introspection relies on it not throwing for a dangling alias. Throwing rather
than skipping the row follows the tier immediately above it: a `merge` over a
plain table the caller cannot `SHOW COLUMNS` already fails the whole query, wide
regexp included.
`getColumnsDescriptionFromSourceTablesImpl` is the sole chokepoint, so the one
guard covers the `merge` table function, its database-regexp form, and both
`StorageMerge` constructors when `ENGINE = Merge` is created without a column
list. Creating such a table over an alias whose target the creator cannot
`SHOW COLUMNS` now fails instead of silently inheriting, and thereby persisting,
the hidden schema. Existing tables cannot break: the derived column list is
persisted into the CREATE query, so the constructor takes the non-empty
`columns_` branch at load and never traverses, which a `DETACH`/`ATTACH`
round-trip and a full server restart both confirm.
`isTargetTableGranted` is a name-based `isGranted` that never resolves the
target, so a dangling `Alias` still reaches the existing empty-metadata handling
and `04236_merge_with_alias_missing_target_104740` and
`04238_storage_alias_missing_target_merge` are unaffected. Table-level
`SHOW COLUMNS` is the tier, so a caller holding only a column-level grant on the
target loses `merge` over the alias while reading the target directly still
works; ClickHouse#116873 recorded that same tradeoff for `loop`.
Two arms are added to `03636_storage_alias_metadata_access_rights`, whose fixture
already grants table-level `SELECT` on the alias and revokes `SHOW COLUMNS` on
the target: one negative arm for the leak and one positive control that must keep
passing, so an over-denial reddens it. Validated on three binaries built from the
same base, each asserted by `buildId()`: the base discloses, a mutant that keeps
every new line but neuters only the throw discloses again, and the fix denies. 50
randomized runs pass, and the `alias` and `merge` suites were run base versus fix
with no failure attributable to the change.
Related: ClickHouse#116873
Related: ClickHouse#116793
Related: ClickHouse#116647
Co-Authored-By: Claude Opus 5 <[email protected]>
Related: #116793
Related: #116647
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed the
looptable function disclosing the column names and types of anAliastable's target to a user who holds no privilege on that target: a query that read no rows received them in its response header.loopnow requires table-levelSELECTon the target, matching what it already requires of the table name it is given.Description
loopauthorizes the name it is given, but anAliaspublishes its target's metadata eagerly while authorizing the target lazily.StorageLoop's constructor copies the inner storage's metadata, which for aStorageAliasis the target's, so the response header is populated before any row is read; the target is only checked insideLoopSource::initLoop's syntheticSELECT, which a row-less query never reaches.A user with
SELECTon the alias and nothing on the target gets its structure from any row-less form:SELECT * FROM loop(db, ali) LIMIT 0, the same query underEXPLAIN header = 1, orDESCRIBE TABLE (SELECT ... LIMIT 0), whileDESCRIBE aliandSELECT * FROM ali LIMIT 0deny that user. Rows are never disclosed; it reproduces on master.The fix adds a name-based target grant check in
TableFunctionLoop::executeImpl, beforeStorageLoopis constructed.PlannerJoinTree.cppdesignatesITableFunction::executeas that seam; it runs at query-tree resolution, not pipeline setup, which covers theDESCRIBEsubquery. The probe never resolves the target, so a danglingAliasstill reportsUNSUPPORTED_METHODto a caller granted the dropped name; full-access behaviour is unchanged. One merged arm of04945_loop_over_table_expression_without_columns_accesschanges toACCESS_DENIED; added arms show it is a tightening.Validated both directions: a mutation per new arm, 50 randomized runs, and a base-versus-fix run of the
loop/aliassuite.Table-level
SELECTis the tier, so a caller holding only a column-level grant on the target losesloopover the alias, though reading it directly still works; the permissive tiers are unsound. #116793 closes the structure seam of this invariant,DESCRIBE loop(db, alias), at that seam's ownSHOW COLUMNStier, so until it merges that form still discloses the columns. Two further residuals: an unauthorizedCREATE TABLE ... AS loop(...)succeeds through the global contextITableFunction::executeuses, andmergereads anAlias's metadata the same way, uncovered.Workflow [PR]
Sync PR [sync-upstream/pr/116873]