Skip to content

[Bug]: hidden secondary-index tables are readable and writable by ordinary SQL — index/base divergence is trivially injectable #31436

Closed

Description

@Ariznawlll

Description

After CREATE INDEX i_xyz ON cmp_idx (x, y), MO materializes the index into a physical table. Catalog rows are hidden from SHOW TABLES / SHOW PUSH list, but** the physical secondary-index table remains fully addressable and writable by ordinary user SQL** once its name is known (name discoverable via mo_catalog.mo_indexes.index_table_name):

-- discover
SELECT index_table_name FROM mo_catalog.mo_indexes
 WHERE table_id = (SELECT rel_id FROM mo_catalog.mo_tables WHERE relname = 'cmp_idx');
-- → `__mo_index_secondary_01a17cca-...`

-- ordinary user account, no special privileges:
SELECT COUNT(*) FROM `__mo_index_secondary_01a17cca-...`;   -- 5000 rows readable ✓
SHOW CREATE TABLE `__mo_index_secondary_01a17cca-...`;      -- schema readable:
--  CREATE TABLE `...` (
--    `__mo_index_idx_col` varchar(65535) COLLATE binary NOT NULL,
--    `__mo_index_pri_col` int NOT NULL,
--    PRIMARY KEY (`__mo_index_idx_col`) )

INSERT INTO `__mo_index_secondary_01a17cca-...` VALUES ('0xdeadbeef', 99999);   -- accepted!
SELECT COUNT(*) FROM `__mo_index_secondary_...`;   -- 5001

A fabricated index row was successfully inserted into a secondary-index table from a plain user session. Analogous writes can DELETE real index entries (making rows unreachable via index while present in base) or UPDATE __mo_index_pri_col to point a key at an arbitrary primary key.

Verified corruption semantics (concrete observation)

  • After the INSERT, SELECT COUNT(*) FROM cmp_idx WHERE x = 424242; returns 0 — the fabricated entry occupies the index but points at a nonexistent primary key (99999), so index-driven lookups behave as if a row exists in the index with a broken primary link (point reads by that key error or silently miss depending on plan).
  • Base-table SELECT COUNT(*) FROM cmp_idx returns 5000 — index now holds 5001 entries vs. 5000 rows: index/base divergence is permanent (no trigger, no reconciliation; the extra entry survives until the table is dropped).
  • SHOW COLUMNS on the hidden table lets the user who is not the table owner learn the full internal encoding scheme (__mo_index_idx_col, __mo_index_pri_col).

Impact

  1. Data integrity: any user with the base-table DML privilege (or even just any user, if permissions on hidden tables are not gated) can silently corrupt secondary indexes — insert/delete/update on the physical table bypasses all invariants and leaves the index permanently inconsistent with the base data (dangling pri-key links, missing entries). Query results then differ depending on the plan (index scan vs full scan).
  2. Security: hidden tables contain a copy of user data (for index-only scans); if any user can read them, data isolation between users may leak rows that the base table's row-level permissions would deny.
  3. SQL injection enablers: any tool that passes through attacker-influenced identifiers (SDK builders, REST layers) multiplies the exposure, since these tables are directly hammerable by name.

Reproduction of DELETE variant (skipped in session, but trivially implied)

DELETE FROM `__mo_index_secondary_...` WHERE __mo_index_pri_col = 50;
-- → base row with id=50 becomes unreachable via index by key lookups; still visible by full scan

Expected behavior

  1. Reject direct DML on __mo_index_secondary_* / __mo_index_unique_* tables for non-system users (as MySQL does for internal system objects). At minimum require a system-variable escape hatch (e.g. SET allow_internal_table_dml = 1 for debugging).
  2. Gate SHOW CREATE TABLE / SHOW COLUMNS / direct SELECTs on hidden index tables behind a similar privilege, and/or keep them out of the normal grant model entirely (parser-level rejection is safest).
  3. Consider making the internal table name un-guessable except to sys (mock it with a session-scoped mapping), and ensure that the index-access path is always through the base table.
  4. Add an invariant checker (e.g. mo_check subcommand) to diff base-table rows vs index entries, enabling detection of exactly this divergence.

Activity

  1. added
    kind/bug
    on Oct 27, 2026
  2. added this to the MO-202610 milestone on Oct 27, 2026
  3. added theissue type on Oct 27, 2026
  4. added a commit that references this issue on Nov 6, 2026
    bfbb264
  5. XuPeng-SH commented on Nov 6, 2026

    @XuPeng-SH
    Contributor

    转 QA 验证:#32032 已合入 main

    • 合并 SHA:bfbb2645c03422a4c5d3a77a9e56942710944643;最终 PR head:bcd74a94340fe4d4285990d06b83125f49a2f945;合并时间:2026-11-06 20:43 UTC。
    • 修复范围:限制普通用户对物理索引关系的直接 SQL、克隆/恢复、元数据及 SQL 可调用维护入口;保留 sys/moadmin 诊断与受约束的服务端索引维护。共享关系分类、调用者身份及规划/运行时独立检查贯穿 prepared EXECUTE、角色降权、远端传递和算子 reset;当前 TABLES/PARTITIONS/COLUMNS(包括订阅来源)应用过滤。
    • 本 issue 是真实产品访问控制/数据完整性问题,不是 CI flaky。当前完整 issue 评论为空;PR 后续复审已确认旧函数 ID 冲突、GPU-tag 链接/无设备 reset 验证要求解决,未发现当前适用的已验证反例或明确未完成的开发 follow-up。本次为转测,不是验收通过。

    证据与限制:

    • 最近实质复审针对 0ebd4fe,报告 99 项 sysview、22 包 480 项 focused tests/subtests、公开 SQL 访问/元数据升级/表达式默认值控制、6 包 55 项 focused race 及 5 个 owner 的 vet 通过。作者报告同修订的 CPU 正常/race 与 36 包增量静态检查通过。
    • 先前 15fb2da 修订的完整 CI、两 CN BVT(show.test 189/189;account_restricted.sql 125/126、1 项既有 ignore)及 GPU-tag 链接/reset 证据保留原版本归属,不能冒充最终 head 的同等复测。最终合并身份已核实,但本轮没有另查其原始 CI 日志。
    • 本轮只核对远端状态、完整 issue 评论、PR 讨论与返回补丁,未运行本地产品测试。返回补丁缺文件头且行数少于 PR 元数据,不能声称独立完整审计了最终 169 个文件。
    • 实际 GPU create/rebuild、旧二进制升级、混合版本部署未验证;用当前二进制安装旧 view 定义的测试不等同旧版本升级。旧用户自有 metadata view/旧 information_schema clone 保留定义和 grants,可能继续显示物理列元数据,属于已记录边界,不代表可直接访问索引行。
    • 不声称修复既有索引损坏,或已交付原 issue 附带提出的 mo_check 检测工具;现有讨论也未把该工具列为持续开放的转测前开发任务。PR 内测试同步调整不代表其他 CI 缺陷已修复或可关闭。

    建议 QA:

    1. 用普通租户/角色及 sys/moadmin 对照,已知真实隐藏名和重命名后的物理关系分别验证 SELECT、DML、DDL、SHOW、LOAD、克隆/恢复及维护/统计 TVF 边界,确认 base-table 数据与正常索引访问不受损。
    2. 覆盖文本/二进制 prepared EXECUTE、plan cache、角色降权、view/procedure/订阅、跨租户与两 CN 远端路径,确认调用者身份不因内部 SQL 或维护标记扩大权限。
    3. 验证合法建索引、REINDEX、fulltext/HNSW 等服务端维护,以及 reset/配置变化后的重新准入。元数据分别区分当前系统 view、新 clone 和既有用户 view;GPU 与真实升级场景按上述未测范围补充记录。

    按转测流程移除研发负责人,改派唯一 Ariznawlll;issue 保持 open。

  6. MatrixAdventurer commented on Nov 7, 2026

    @MatrixAdventurer
    Contributor

    【修复核实】结论:已修复,可关闭。
    PR #32032(11-06 20:43 合入 main,169 文件):

    • 新增 514 行 pkg/frontend/index_relation_access.go:普通用户直查/直接 DML/SHOW CREATE _mo_index* 物理索引表在关系解析层统一报 "do not have privilege"(TestIndexRelationPrivilegePrecedence 16 场景等);
    • pkg/catalog/index_relation_access.go 新增 mo_is_physical_index/RequiresPrivilegedRelationAccess 过滤元数据视图;
    • pkg/frontend/authenticate.go 封堵对重命名/伪装索引表的 GRANT(TestGrantPrivilegeLocksObjectLifecycle 新增 3 类子测试)。
      仍缺:只读条件下未起集群复现原 SQL;issue ③④(命名不可猜、mo_check 校验器)提案未随本 PR 实现;合入时刻 CI 记录为一轮被取消的 runs(无失败用例),最终 head 的全量绿色日志未逐项核查。
      @Ariznawlll 请确认关闭。
      本操作由LW授权的MOI智能体提交
  7. slyunnn commented on Nov 11, 2026

    @slyunnn

    Regression and extended verification for #31436 (2026-11-11)

    Verification Environment

    • Frozen latest main at test start, 45917e803f2915f6f0838b2b2fe9f591fc5b6018, fix(plan): fold contradictory same-column predicates to near-zero estimates (#32502).
    • Fresh CPU binary with matching verified native-library inputs. Runtime git_version()=45917e803f through Proxy 6001, CN1 16001 and CN2 16002. All three endpoints executed the issue checks.
    • Co-hosted 2 CN / 1 TN / 1 Log service / Proxy, macOS arm64, Go 1.27.1.
    • PyMySQL text protocol and JDBC BVT. Actual Go database/sql + go-sql-driver/mysql, interpolateParams=false, supplied native prepared-statement evidence for MIN/MAX parameter changes and physical-index role changes.

    What Was Covered

    • OK — Ordinary users with base-table DML privileges cannot directly SELECT/INSERT/UPDATE/DELETE real unique and secondary physical index tables through any endpoint.
    • OK — SHOW CREATE/COLUMNS, TRUNCATE/DROP/ALTER, CTAS, view creation and text PREPARE of physical relations are rejected. Current TABLES/COLUMNS/PARTITIONS metadata filters their entries.
    • OK — sys/moadmin diagnostic counts remain available. Ordinary indexed base-table queries and base INSERT/UPDATE/DELETE stay correct, with 5,000 rows and expected complete ID sum after cleanup.
    • OK — Native binary prepared statements admitted under moadmin are rejected after switching to an ordinary role. The same statement and connection work again after restoring moadmin.
    • Not covered — Actual GPU index maintenance, old-binary upgrades, cross-tenant subscriptions, renamed physical relations, and legacy user-owned metadata-view definitions. The accepted direct-access fix does not include the separately proposed standalone mo_check tool or repair pre-existing corruption.

    Findings

    • Associated fix fix: restrict direct access to physical index relations, merge bfbb2645c03422a4c5d3a77a9e56942710944643 is included in the frozen main. PR #32032
    • The verified ordinary-role paths preserve the physical-index barrier while normal index-backed base operations work. GPU, upgrade and legacy metadata-view claims remain limited to the evidence above.

    MOTR / BVT Coverage

    • Added issue_31436_index_access.test and matching .result in suites/14_issue_regression; the physical-index case also invokes its Go protocol helper through the existing module.
    • Independent branch test/issue-31436-index-access, source commit 0095889fbd937ee691471c2655ff9af59447d4ae, weekly cherry-pick 6aa5c69948bc5cf490f6452fd85c26a5ff70fa74. Recording and 2 replays per endpoint passed, then branches were pushed and remote/upstream verified. Weekly MOTR PR #321
    • Combined weekly branch passed 9 cases with -j 5 --repeat 2 through each endpoint, 54/54 executions total. The new cases use separate databases, roles and connections, with no global-variable changes. This is runner concurrency.
    • Targeted product tests recorded 199 test/subtest passes across 7 packages with selected tests. The independent forced-two-CN aggregate fixture also passed.

    Final Conclusion

    Fixed for the original issue contract on the verified build and paths.

    Large-data regression — Not required. Authorization is determined by caller identity and protected relation kind. The 5,000-row control checks preserved index/base behavior.

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

Metadata

Metadata

Labels

kind/bugneeds-triage

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions