Skip to content

[Bug]: prepared statement keeps old row-rule result after SET ROLE refresh #29148

Closed

Description

@slyunnn

Environment

Reproduction

  1. Create rcac046_db.t(id, tenant, amount) with (1,1,100), (2,2,200), (3,3,300).
  2. Grant a role rcac046_r SELECT on the table and add rule select id, amount from rcac046_db.t where tenant = 1. Create a user with that default role.
  3. In user session B: SET enable_remap_hint = 1; prepare SELECT id, amount FROM t ORDER BY id; execute it. It returns (1,100).
  4. In admin session A, replace the same role/table rule with ... where tenant = 2.
  5. Back in the same B session, run SET ROLE rcac046_r. A direct SELECT id, amount FROM t ORDER BY id now returns (2,200), proving the role-rule cache refresh occurred.
  6. Execute the previously prepared statement in B. It still returns (1,100). A newly connected user session returns (2,200).

Expected

After explicit SET ROLE refresh, execution of a prepared statement must apply the currently effective rule. The result in step 6 must be (2,200), or execution must safely recompile/reject; it must not return data permitted only by the replaced rule.

Actual

The prepared statement continues to apply the old tenant = 1 rule after SET ROLE, while a direct query in the same refreshed session and a new connection use tenant = 2.

Impact

When a policy is narrowed or replaced, an already prepared statement can continue reading rows that the current rule no longer authorizes. This is a row-level access-control bypass through a stale prepared plan/cache.

Stability

Reproduced twice with the sequence above. Temporary user, role, database, and table are cleaned up after the run.

Activity

  1. added
    kind/bug
    on Sep 20, 2026
  2. added theissue type on Sep 20, 2026
  3. added this to the MO-202609 milestone on Sep 20, 2026
  4. added a commit that references this issue on Oct 14, 2026
    e17fc0f
  5. added
    ai-easy
    on Oct 14, 2026
  6. slyunnn commented on Oct 14, 2026

    @slyunnn
    Author

    Regression and extended verification for #29148 (2026-10-14)

    Verification Environment

    • MatrixOne source: latest origin/main at a53390c57de48ac280249ff0e88831e502e20fb0, titled Fix BuildJsonPlan statistics snapshot race (#30136). The locally built and running service binary reports this exact commit; upstream main was rechecked before posting.
    • Topology: 2 CN / 1 TN / 1 Log service / Proxy, launched as an isolated local cluster.
    • Connectivity: Proxy 127.0.0.1:6001, CN1 127.0.0.1:16001, and CN2 127.0.0.1:16002 were each reachable. The actual protocol regression was run directly against all three endpoints; MOTR replay also targeted all three.
    • Client protocol: Go database/sql with go-sql-driver/mysql, interpolateParams=false; verified wire commands COM_STMT_PREPARE, COM_STMT_EXECUTE, and COM_STMT_SEND_LONG_DATA. MOTR replay used the same binary-protocol client.
    • Related fix: PR #29195, merge commit e17fc0f7fe151c2b9a49707e39fd8ad18fd278e5, titled fix(frontend): invalidate prepared row-rule statements after policy refresh. It invalidates prepared statements made stale by a role-policy refresh and is included in the tested main.

    What Was Covered

    • OK — Original scenario: prepare a query under the tenant = 1 row rule, change the rule to tenant = 2, then refresh it with SET ROLE; direct SQL returns only (2, 200).
    • OK — Stale-handle unhappy path: executing the old binary prepared statement fails closed with ER_NEED_REPREPARE (1615), rather than returning rows allowed by the old policy.
    • OK — Fresh-handle path: a newly prepared query sees the refreshed policy and returns only the authorized row.
    • OK — LONG_DATA protocol boundary: a 700 KiB bound parameter exercises COM_STMT_SEND_LONG_DATA; executing the invalidated handle returns ER_NEED_REPREPARE (1615), and the connection remains usable afterward.
    • OK — Endpoint coverage: the direct protocol scenario passed via Proxy 6001, CN1 16001, and CN2 16002; the recorded MOTR case replay passed on each endpoint (1/1 each). CN1 and CN2 replays ran concurrently with per-port isolated databases, roles, and users; Proxy replay was separate.
    • OK — Existing BVT: role_rule_prepared.sql passed 41/41 twice through Proxy 6001, including role refresh, failed SET ROLE, stale-statement rejection, fresh preparation, and cleanup checks.
    • Not covered — Production/TKE deployment and strict golden/metadata BVT modes were not run; they are not needed to establish this local regression result.
    • Not covered — MatrixOne engine-level parallel execution was not tested; this issue concerns session authorization invalidation. MOTR parallelism here means independent CN endpoint cases ran concurrently, not that one SQL query was parallelized by the engine.

    Findings

    The tested build contains the merged fix. After a role refresh, an existing binary prepared handle cannot continue using the superseded row-access policy: it is rejected with ER_NEED_REPREPARE (1615). A fresh handle enforces the current policy. The error path, including a pending LONG_DATA parameter, does not desynchronize or poison the connection.

    No large-data regression is needed. The defect is in prepared-statement authorization/policy invalidation and does not depend on row count, block/batch boundaries, spill, or resource scale. The 700 KiB value only forces the MySQL long-data wire-protocol path; it is not a scale test.

    MOTR / BVT Coverage

    • MOTR branch: test/issue-29148-prepared-row-rule (pushed to matrixorigin/motr).
    • Source commit: d488da77bb2313abe60af3ebfbe9bd71d02c69a8 (test: add MOTR regression for #29148).
    • Recorded and replayed successfully. Replay results: Proxy 6001 — 1/1; CN1 16001 — 1/1; CN2 16002 — 1/1.
    • Included in daily MOTR PR #238 as commit cd72d092b9bb3509b3224f7dd90450276990f885; the daily branch also incorporates latest MOTR main.
    • BVT role_rule_prepared.sql: 41/41 passed twice on Proxy 6001.

    Final Conclusion

    Fixed. The merged fix is present in the tested latest main; the original security-sensitive stale-policy scenario and the extended binary-protocol cases passed through Proxy, CN1, and CN2. No large-data regression is required for this issue.

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

Metadata

Metadata

Assignees

Labels

4.3-devai-easykind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions