Skip to content

fix(clickhouse): RowPolicy checks that no other row policy is on the table - #32

Merged
fm4v merged 1 commit into
ClickHouse:mainfrom
groeneai:fix-clickhouse-rowpolicy-foreign-policy
Nov 20, 2026
Merged

fm4v merged 1 commit into
ClickHouse:mainfrom
groeneai:fix-clickhouse-rowpolicy-foreign-policy

Conversation

@groeneai

Copy link
Copy Markdown

The 2026-11-19 master nightly (SQLancer (arm_release), reproducer) reported a RowPolicy finding that reads like a row policy bypass:

The size of the result sets mismatch (0 and 11)!
SELECT t0.c0 FROM t0 WHERE ((t0.c0)%(t0.c0))   -- 0 rows
SELECT t0.c0 FROM t0                           -- 11 rows under USING (((t0.c0)%(t0.c0)))

The same table and policy return 0 rows on master in every setting variant I tried. The CI numbers come out exactly when another permissive policy, e.g. USING (t0.c0 IS NOT NULL), is already on the table: the baseline then reads old AND p and the policy read old OR p.

The oracle assumes its policy is the only one on the table, but nothing guarantees it:

  • its DROP ROW POLICY in finally is best effort, and the expected errors include transport errors, Memory limit and TIMEOUT_EXCEEDED;
  • row policies survive DROP DATABASE, and every database of a thread reuses the same name, so a leaked policy also applies to the next t0 of that thread.

The nightly keeps no server log, so I cannot show the leftover policy for that run.

Now the oracle lists the policies on the table in system.row_policies before the baseline; if there are any, it drops them and skips the check. On a mismatch it also logs the policies on the table, the table state and a re-read row count of the filtered query to the reproducer, so a finding that lists only the oracle's own policy points at ClickHouse.

Checked: mvn verify -DskipTests=true -Djacoco.skip=true passes. --oracle RowPolicy against master ec475edd5e3d (debug build, .claude/clickhouse-config/ overrides), before and after on one server:

run before with this PR
biased (1 in 20 finally drops skipped, local patch), 4 threads, 5 min 84 findings, 1374 policies left 0 findings, 1 left
unbiased, 10 threads, 10 min 0 findings, 240,912 policies created 0 findings, 227,335 created

A forced mismatch (local patch) writes:

-- diagnostics: row policies of t0
--   rp_t0_2 | (t0.c0) | 0 | 0 | ['default']
-- diagnostics: filtered query re-read: 2 rows

Related: ClickHouse/ClickHouse#128538

🤖 Generated with Claude Code

…table

The RowPolicy oracle compares `SELECT c FROM t WHERE p` with `SELECT c FROM t`
under `CREATE ROW POLICY ... USING (p)`. It assumed that its own policy is the
only one on the table, but its cleanup is best effort: the `finally` DROP goes
through the expected errors, which include transport errors, `Memory limit`
and `TIMEOUT_EXCEEDED`. Row policies also survive `DROP DATABASE`, and every
database of a thread reuses the same name, so a leaked permissive policy also
applies to the next `t0` of that thread. With one present, the baseline sees
`old AND p` and the policy read sees `old OR p`, which is reported as a row
policy bypass:

  The size of the result sets mismatch (0 and 11)!
  SELECT t0.c0 FROM t0 WHERE ((t0.c0)%(t0.c0))   -> 0
  SELECT t0.c0 FROM t0                           -> 11

Before the baseline, list the policies on the table in system.row_policies;
drop any that exist and skip the check. On a mismatch, log the policies on
the table, the table state and a re-read row count of the filtered query to
the reproducer, then rethrow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@fm4v
fm4v merged commit 063cb49 into ClickHouse:main Nov 20, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants