Skip to content

Require SHOW COLUMNS to read DDL columns in system.tables - #105367

Closed
pufit wants to merge 6 commits into
ClickHouse:masterfrom
pufit:pufit/fix-system-tables-show-columns-leak
Closed

pufit wants to merge 6 commits into
ClickHouse:masterfrom
pufit:pufit/fix-system-tables-show-columns-leak

Conversation

@pufit

@pufit pufit commented May 19, 2026

Copy link
Copy Markdown
Member

Motivation

A user with only column-level grants (GRANT SELECT(col) ON db.t) can bypass DESCRIBE TABLE and SHOW CREATE TABLE — both of which correctly require SHOW COLUMNS — by reading the same information from system.tables:

SELECT create_table_query, engine_full, partition_key,
       sorting_key, primary_key, sampling_key
FROM system.tables WHERE database = 'db' AND name = 't';

The implicit-grant chain in ContextAccess propagates column-level grants up to SHOW TABLES at the table level, so the row passes the existing SHOW TABLES row filter in StorageSystemTables. The DDL-revealing columns were then populated unconditionally.

Same class of bug as 5875e4daaf5 (DESCRIBE via remote) and 64efbbeb906 (CREATE AS). system.tables was the lone holdout — every other surface (DESCRIBE, SHOW CREATE, system.columns) already required SHOW COLUMNS.

Changes

StorageSystemTables.cpp now gates the schema-revealing columns on a per-row SHOW COLUMNS check:

  • create_table_query, engine_full, as_select — empty string when access is denied; skips the tryGetCreateTableQuery parse + clone entirely.
  • partition_key, sorting_key, primary_key, sampling_key, unique_key — default (empty) when access is denied.
  • dependencies_database / dependencies_table — filtered per element by SHOW TABLES on each dependent view. The names of dependents are SHOW TABLES-grade information about those tables, not SHOW COLUMNS-grade about the current row's table.

The existing fast paths are preserved: SHOW TABLES row visibility, the name-only short-circuit at line 518, and the admin-equivalent skip via !need_to_check_access_for_tables. Temporary tables are session-local and untouched.

A new stateless test covers the bypass scenario, the SHOW COLUMNS grant path, a table-level SELECT regression guard (still visible via the implicit-grant rule), SHOW TABLES-only (DDL hidden), and the dependent-view per-element filter.

Changelog category (leave one):

  • Backward Incompatible Change

Changelog entry:

Reading create_table_query, engine_full, as_select, partition_key, sorting_key, primary_key, sampling_key, and unique_key from system.tables now requires the SHOW COLUMNS privilege on the row's table, consistent with DESCRIBE TABLE and SHOW CREATE TABLE. Previously, a user with only a column-level grant (e.g. GRANT SELECT(col) ON db.t) could read the full table DDL through system.tables, despite DESCRIBE and SHOW CREATE correctly refusing them. Such users will now see empty values in these columns. To restore the previous behaviour, grant SHOW COLUMNS explicitly. The dependencies_database and dependencies_table arrays now also filter their elements by SHOW TABLES access on each dependent.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

A user with only column-level grants (`GRANT SELECT(col) ON db.t`) could
bypass `DESCRIBE TABLE` and `SHOW CREATE TABLE`, both of which require
`SHOW_COLUMNS`, by reading the same information from `system.tables`:

    SELECT create_table_query, engine_full, partition_key,
           sorting_key, primary_key, sampling_key
    FROM system.tables WHERE database = 'db' AND name = 't';

The implicit-grant chain in `ContextAccess` propagates column-level grants
up to `SHOW_TABLES` at the table level, so the row passed the existing
`SHOW_TABLES` filter in `StorageSystemTables` and the DDL-revealing columns
were then populated unconditionally.

This change gates the DDL columns (`create_table_query`, `engine_full`,
`as_select`, `partition_key`, `sorting_key`, `primary_key`, `sampling_key`,
`unique_key`) on a per-row `SHOW_COLUMNS` check. The `dependencies_database`
and `dependencies_table` arrays now also filter their elements by
`SHOW_TABLES` on each dependent view, so a user cannot enumerate the names
of dependents they otherwise cannot see.

Behavior is unchanged for users with `SHOW_TABLES` at the database level
or wider, table-level `SELECT`, or explicit `SHOW COLUMNS` — all of which
imply `SHOW_COLUMNS` at the table level via the existing grant rules.

Mirrors the precedent of commits 5875e4d (`DESCRIBE` via `remote`) and
64efbbe (`CREATE AS`), which closed analogous bypasses on other code
paths.

Disclosure: https://clickhouse-inc.slack.com/archives/C02F2LML5UG/p1777430221999219
@clickhouse-gh

clickhouse-gh Bot commented May 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [0af3d7b]

Summary: ❌

job_name test_name status info comment
Stateless tests (arm_asan_ubsan, azure, parallel) FAIL
Server died FAIL cidb
Logical error: Expected 3 to 10 arguments in table function azureBlobStorage, got 1` (STID: None) FAIL cidb
OOM in dmesg FAIL cidb

AI Review

Summary

This PR tightens system.tables visibility so DDL-like columns require SHOW COLUMNS, and related-object arrays/targets are filtered by per-object SHOW TABLES. The previously reported dependencies_*, loading_*, teardown, and target_* issues are addressed in the current code, but one SHOW CREATE TABLE-level metadata carrier remains outside the new gate, so I am requesting changes.

PR Metadata
  • 💡 Changelog category as Backward Incompatible Change is appropriate, but the Changelog entry is now incomplete after later commits: it mentions dependencies_database / dependencies_table, but not the added filtering for loading_dependencies_database, loading_dependencies_table, loading_dependent_database, loading_dependent_table, target_database, and target_table.
  • Suggested addition: The loading dependency columns and materialized view target columns in system.tables now also filter related object names by SHOW TABLES access on each related object.
Missing context / blind spots
  • ⚠️ Full CI is still pending for commit 0af3d7b2214b8eb33cefa1bd8fa3bc292a2e1784; the direct PR report currently has 0 test results and only Style check had passed at review time. A completed PR report or focused local stateless run would close this gap.
Findings

⚠️ Majors

  • [src/Storages/System/StorageSystemTables.cpp:568] The new SHOW COLUMNS contract still leaves parameterized_view_parameters populated without can_show_columns. Those parameter names and types are part of the view definition that SHOW CREATE TABLE hides behind SHOW COLUMNS, so a user with only SHOW TABLES on a parameterized view can still recover that metadata through system.tables.
    Suggested fix: gate fillParametralizedViewData on can_show_columns, or explicitly narrow the contract/changelog if this column is intentionally SHOW TABLES-visible.
Tests
  • ⚠️ Add a focused stateless regression for a parameterized view: with only SHOW TABLES, parameterized_view_parameters should be hidden; after granting SHOW COLUMNS, it should be visible.
Final Verdict

Status: ⚠️ Request changes. Minimum required actions: close the parameterized_view_parameters visibility gap, add the regression, and update the changelog entry for the additional filtered columns.

@clickhouse-gh clickhouse-gh Bot added the pr-backward-incompatible Pull request with backwards incompatible changes label May 19, 2026
The previous version short-circuited `can_show_columns` on
`!need_to_check_access_for_tables`, which becomes `false` when the user has
`GRANT SHOW TABLES ON db.*` — exposing DDL to users without `SHOW COLUMNS`.

The dependent-view filter had the same shape: skipped when the source
table's database granted `SHOW TABLES`, which allowed names of dependent
views in *other* databases to leak.

Now `can_show_columns` is derived directly from `isGranted(SHOW_COLUMNS,
db, table)` and the dependency filter only skips when the user has global
`SHOW TABLES`. Comments trimmed. Test extended to cover both regressions.
@pufit
pufit marked this pull request as ready for review May 25, 2026 18:02
@pufit

pufit commented May 25, 2026

Copy link
Copy Markdown
Member Author

@groeneai please check the tests failures here. If they are already fixed - provide a PR link.

@groeneai

Copy link
Copy Markdown
Collaborator

Hi @pufit, only 2 checks fail and both are chronic CI flakes unrelated to this PR.

Check Test Tracking Status
Unit tests (asan_ubsan, function_prop_fuzzer) FunctionsStress.stress ("1 threads got stuck") Issue #104877 Chronic. Last broad fix #105146 (merged 2026-05-17) cut master rate from ~17/day on 2026-05-15 to ~1/day after. Residual "threads got stuck" cases continue.
Fast test (arm_darwin) "Server died" Umbrella #103235 Chronic arm_darwin runner instability. Almost zero master signal (2 hits in 30d) vs 16-22 hits per unrelated PR. No fix in flight, pure macOS runner issue.

30-day cross-PR data:

  • FunctionsStress.stress: 43 master hits, 30+ unrelated PRs (100173, 96886, 96130, 100272, 104522, 96844, 100277, 104812, ..., each 4-8 hits).
  • Fast test (arm_darwin) "Server died": only 2 master hits, 30+ unrelated PRs (100412, 96844, 100146, 96978, 100377, 101039, 100276, ..., each 16-22 hits).

Your branch is rebased past #105146, so the function_prop_fuzzer hit is genuine residual chronic flake and not a missing-fix artifact.

Recommendation: rerun the two failed jobs. No action required on this PR.

The bot's inline review on StorageSystemTables.cpp (per-dependent filtering when need_to_check_access_for_tables == false) is the only substantive item, and that is for you to evaluate.

@jkartseva jkartseva self-assigned this Jun 1, 2026
@jkartseva

Copy link
Copy Markdown
Member

Should this fix apply to the loading_dependencies_{database|table} and loading_dependent_{database|table} columns? A user without SHOW TABLES grant on a database may still read names through these columns in system.tables.

PTAL at the Opus 4.8 output:

The new SHOW TABLES filter on dependencies_database/dependencies_table is bypassable: the sibling columns loading_dependencies_database/table and loading_dependent_database/table (StorageSystemTables.cpp:903–927) carry the same cross-table
names but are left unfiltered.

It doesn't show up for materialized views (the loading_* graph has no MV→source edges), but it does for dictionaries. Repro — a user with only GRANT SHOW TABLES ON db.* reads names from a database they have zero rights on:

-- dict_src in db; cross_dict (dictionary) and hidden_src (table) in db2
SELECT loading_dependent_table, loading_dependent_database
FROM system.tables WHERE database = 'db' AND name = 'dict_src';
-- ['cross_dict', 'dict'] ['db2', 'db'] <-- cross_dict / db2 leak

So the same SHOW TABLES-grade information this PR withholds from dependencies_* still leaks through loading_. Suggest applying the same per-element SHOW TABLES filter to all four loading_ arrays, or narrowing the changelog claim so it
doesn't promise a protection that a one-column-different query defeats.

@groeneai

groeneai commented Jun 5, 2026

Copy link
Copy Markdown
Collaborator

Confirmed at PR head f55726e3f3be. The asymmetry is in src/Storages/System/StorageSystemTables.cpp:

  • view_ids loop (lines 624-634) now filters per-element via need_to_check_access_for_databases && !access->isGranted(SHOW_TABLES, view_id.database_name, view_id.table_name).
  • getLoadingDependencies / getLoadingDependents loops (lines 903-917) push raw names with no access check.

The leak is dictionary-shaped: the loading-dependency graph carries Dictionary edges that the MV view_ids graph does not, so a cross-database dictionary SOURCE(CLICKHOUSE(...)) exposes source DB/table names through loading_dependent_database/table regardless of MV grants. The repro in the issue holds.

Recommend mirroring the same per-element filter on all four loading_* arrays. The existing test file 04257_system_tables_show_columns_leak.sh already has the harness; adding a loading_probe alongside deps_probe covers it. Otherwise the changelog should drop the SHOW TABLES-grade protection claim, since a one-column-different query defeats it.

Happy to push the widening as a follow-up commit on this branch or a separate PR.

Session: cron:clickhouse-ci-task-worker:20260605-033200

@jkartseva jkartseva left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Accepting; please make a follow up PR with symmetric changes for loading_dependencies_{database|table} and loading_dependent_{database|table}


++rows_count;

/// `SHOW COLUMNS` gates DDL-revealing columns to match `DESCRIBE` / `SHOW CREATE`.

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 new SHOW COLUMNS gate does not cover parameterized_view_parameters below. Those parameter names and types are part of the view definition that SHOW CREATE TABLE hides behind SHOW COLUMNS, so a user with only SHOW TABLES on a parameterized view can still recover them with:

SELECT parameterized_view_parameters
FROM system.tables
WHERE database = 'db' AND name = 'parameterized_view';

Please gate fillParametralizedViewData on can_show_columns as well, or explicitly narrow the contract/changelog if this column is intentionally SHOW TABLES-visible, and add a regression for a parameterized view.

@clickhouse-gh

clickhouse-gh Bot commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.70% 84.60% -0.10%
Functions 92.30% 92.30% +0.00%
Branches 77.30% 77.20% -0.10%

Changed lines: Changed C/C++ lines covered by tests: 59/59 (100.00%) | Lost baseline coverage: none · Uncovered code

Full report · Diff report

@clickhouse-gh

clickhouse-gh Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Dear @jkartseva, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself.

@pufit

pufit commented Aug 31, 2026

Copy link
Copy Markdown
Member Author

@groeneai I'm closing this PR, you now own the fix. Please re-introduce the correct version

@pufit pufit closed this Aug 31, 2026
@groeneai

Copy link
Copy Markdown
Collaborator

Acknowledged, I own this. I will open a fresh PR rather than reopen this one.

Three measurements on current master (src/Storages/System/StorageSystemTables.cpp) before I start:

  1. This branch no longer applies: 1 of 11 hunks fails. create_table_query / engine_full / as_select are now rendered through database->getRenderedCreateTableQuery(table_name, context, fields) behind the new can_expose_metadata (a StorageAlias SHOW TABLES check, lines 757-759 and 870-872), so the tryGetCreateTableQuery call this branch gated is gone from that site. The re-introduction composes with can_expose_metadata rather than replacing it.

  2. One DDL column landed after this branch's base and is ungated: skipping_indices_types (declared line 368, filled at 660 and 932). It reports the distinct skip-index types, which SHOW CREATE TABLE withholds from the same user, so by this PR's own contract it belongs behind the gate. I plan to include it; tell me if you want it left out.

  3. parameterized_view_parameters is the one surface this branch never covered (the review-bot finding here on 2026-06-10). On master fillParametralizedViewData is guarded only by can_expose_metadata.

The dependency surfaces this branch did cover are intact in my scope: dependencies_*, the four loading_* arrays, and target_database / target_table.

I will keep the gate unconditional and backward-incompatible as labelled here, and carry 04257_system_tables_show_columns_leak.sh forward with a case per surface. I will post the PR link in this thread.

@groeneai

groeneai commented Sep 1, 2026

Copy link
Copy Markdown
Collaborator

Opened #117448.

It follows the approach here and covers the three surfaces raised in review: the row's own
schema columns, parameterized_view_parameters, and all eight columns that name another table
(dependencies_*, loading_dependencies_*, loading_dependent_*, target_database,
target_table), which are filtered per element by SHOW TABLES on the table named. The
temporary and external table branch carries its own copy of the schema gate, so an Alias row
there is covered too.

Two deliberate differences from this PR. A dictionary's CREATE query follows
SHOW CREATE DICTIONARY and stays readable with SHOW DICTIONARIES, since
InterpreterShowCreateQuery authorizes it with that privilege rather than SHOW COLUMNS; a
plain table declared ENGINE = Dictionary stays behind SHOW COLUMNS. And the schema columns
of an Alias row additionally require SHOW COLUMNS on the alias target, matching what
SHOW CREATE TABLE and DESCRIBE already do for it.

On the compatibility question from my earlier comment: I kept the gate unconditional and
backward-incompatible, matching the label here, with no opt-in setting, which is what the two
earlier fixes of this class did. The changelog entry names the grants that restore the previous
reads. Say so if you would rather it were opt-in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backward-incompatible Pull request with backwards incompatible changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants