Skip to content

[Bug]: fileservice remote cache panics on malformed keys and response indexes #25885

Closed

Description

@Ariznawlll

Summary

The fileservice remote-cache RPC path trusts request key arrays and response indexes without validating their shape or bounds. Malformed or inconsistent internal RPC payloads can therefore trigger deterministic runtime panics on either the cache-serving CN or the requesting CN.

Affected revision

  • Branch: latest main
  • Commit: 862d9d30c2cd7a55995fd0bae0951af0809f5752
  • Commit date: 2026-07-20T04:31:19Z

Duplicate check

Searched open and closed issues with remote cache malformed request panic, GetCacheData RequestCacheKey panic, remote cache response index out of range, HandleRemoteRead empty cache key, and fileservice remote cache nil key. No duplicate was found.

Root cause

HandleRemoteRead reads RequestCacheKey[0] before checking the slice length, dereferences request entries without checking for nil, and later dereferences every nested CacheKey.

RemoteCache.Read likewise dereferences each ResponseCacheData and directly indexes vector.Entries[int(cacheData.Index)] without checking for nil, negative indexes, or indexes at/above the vector length.

Actual behavior

The following payloads panic deterministically:

  • empty request key list;
  • nil first request key;
  • nil later request key;
  • nil nested cache key;
  • nil response entry;
  • negative response index;
  • response index equal to the vector length.

Expected behavior

The RPC boundary should reject malformed requests with an error and should ignore or reject malformed best-effort cache responses. Invalid remote-cache data must never panic a CN.

Impact

  • A malformed or version-skewed internal cache request can panic the serving query-handler path.
  • A corrupt, stale, or inconsistent remote-cache response can panic the requesting read path.
  • Remote cache is an optimization; invalid remote data should fall back to local storage instead of affecting query availability.

Detailed white-box test cases, commands, repetition counts, race results, and the temporary-fix comparison are provided in a Chinese comment.

Activity

  1. added this to the MO-202607 milestone on Jul 20, 2026
  2. Ariznawlll commented on Jul 20, 2026

    @Ariznawlll
    CollaboratorAuthor

    最新 main 白盒验证

    测试基线

    • 分支:最新 main
    • Commit:862d9d30c2cd7a55995fd0bae0951af0809f5752
    • Commit 时间:2026-07-20T04:31:19Z
    • Commit 标题:perf(plan): prune right SINGLE probe scans with runtime filters (#25865)
    • 验证日期:2026-07-20
    • 本轮不涉及事务或 2PC。

    测试范围与过程

    新增本地白盒审计测试 pkg/fileservice/remote_cache_audit_test.go,分别覆盖 remote-cache RPC 的服务端请求解析和客户端响应消费。

    一、服务端 HandleRemoteRead 请求边界

    逐项构造以下请求并断言函数必须返回错误而不能 panic:

    1. RequestCacheKey 为空切片;
    2. 第一个 RequestCacheKey 指针为 nil;
    3. 第一个 key 合法、后续 RequestCacheKey 指针为 nil;
    4. 第一个 key 合法、后续条目的嵌套 CacheKey 为 nil。

    当前实现分别在 remote_cache.go:168 和 remote_cache.go:178 发生 slice 越界或 nil pointer dereference。

    核心调用:

    req := &query.Request{
        GetCacheDataRequest: &query.GetCacheDataRequest{
            RequestCacheKey: malformedKeys,
        },
    }
    resp := &query.WrappedResponse{Response: &query.Response{}}
    
    require.NotPanics(t, func() {
        err = HandleRemoteRead(context.Background(), nil, req, resp)
    })
    require.Error(t, err)

    二、客户端 RemoteCache.Read 响应边界

    使用白盒 QueryClient 返回人工构造的响应,逐项覆盖:

    1. ResponseCacheData 中包含 nil 条目;
    2. 命中条目的 Index = -1;
    3. 只有一个 vector entry 时返回 Index = 1,即 Index == len(vector.Entries)。

    期望 remote cache 忽略非法远端数据并让后续本地存储读取接管,不能 panic。当前实现在 remote_cache.go:130/132 发生 nil pointer dereference 或 index out of range。

    原始代码执行命令

    普通重复验证:

    go test ./pkg/fileservice \
      -run '^TestAudit(HandleRemoteReadRejectsMalformedKeys|RemoteCacheReadRejectsInvalidResponseIndexes)$' \
      -count=3 -v

    Race 验证:

    go test -race ./pkg/fileservice \
      -run '^TestAudit(HandleRemoteReadRejectsMalformedKeys|RemoteCacheReadRejectsInvalidResponseIndexes)$' \
      -count=1 -v

    原始代码实际结果

    Panic value: runtime error: index out of range [0] with length 0
    Panic value: runtime error: invalid memory address or nil pointer dereference
    Panic value: runtime error: index out of range [-1]
    Panic value: runtime error: index out of range [1] with length 1
    
    • 普通模式 -count=3:每轮 7/7 子用例失败,合计 21/21 FAIL;
    • Race 模式 -count=1:7/7 FAIL;
    • Race 未报告额外数据竞争,失败原因均为确定性的边界 panic。

    临时最小修正对照

    临时修正只做两类保护:

    1. 服务端在读取 [0] 前检查 key 数量,并检查每个 RequestCacheKey 及其 CacheKey 是否为 nil;
    2. 客户端在使用响应前检查 ResponseCacheData != nil 且 0 <= Index < len(vector.Entries)。

    执行:

    go test ./pkg/fileservice \
      -run '^TestAudit(HandleRemoteReadRejectsMalformedKeys|RemoteCacheReadRejectsInvalidResponseIndexes)$' \
      -count=100
    
    go test -race ./pkg/fileservice \
      -run '^TestAudit(HandleRemoteReadRejectsMalformedKeys|RemoteCacheReadRejectsInvalidResponseIndexes)$' \
      -count=50
    
    go test ./pkg/fileservice -count=1

    结果:

    • 白盒测试普通模式:100/100 PASS;
    • 白盒测试 race 模式:50/50 PASS;
    • 临时修正下 fileservice 全包回归:PASS,耗时 27.897s。

    结论与清理

    原始实现的 7 类 panic 均可稳定复现,最小边界校验能同时关闭所有复现路径且不影响 fileservice 全包测试,因此确认是产品 bug。

    验证完成后已撤销临时产品代码修正;白盒测试只保存在本地审计目录,未提交、未推送任何代码。

  3. added
    kind/bug
    on Jul 20, 2026
  4. added theissue type on Jul 20, 2026
  5. added
    severity/s0
    and removed on Jul 21, 2026
  6. LeftHandCold commented on Jul 22, 2026

    @LeftHandCold
    Collaborator

    fixed

  7. Ariznawlll commented on Jul 22, 2026

    @Ariznawlll
    CollaboratorAuthor

    已在最新 main 分支完成复验,remote-cache 的畸形请求和非法响应不再导致 CN panic。

    测试版本

    测试范围

    执行以下正式回归:

    • TestHandleRemoteReadRejectsInvalidRequests
    • TestHandleRemoteReadTransfersCacheDataRelease
    • TestHandleRemoteReadReleasesPartialCacheDataOnError
    • TestRemoteCacheReadIgnoresInvalidResponses
    • TestRemoteCacheReadAcceptsOnlyIndicesRequestedFromTarget
    • TestLocalFSFallsBackAfterInvalidRemoteCacheHit

    覆盖场景包括:

    1. 空 RequestCacheKey;
    2. 缺失第一个或后续嵌套 CacheKey;
    3. in-process nil repeated request key;
    4. nil response entry;
    5. negative index 和 index == len(entries);
    6. 返回数据长度与请求长度不一致;
    7. 同一 index 的非法 hit 后跟合法 hit;
    8. 重复合法 hit 时只接受第一份;
    9. response index 并非发给该 target 的请求 index;
    10. remote cache 返回非法数据后由 LocalFS 接管读取;
    11. 正常、部分成功及错误路径中的 cache data release。

    执行命令:

    go test ./pkg/fileservice \
      -run '^(TestHandleRemoteReadRejectsInvalidRequests|TestHandleRemoteReadTransfersCacheDataRelease|TestHandleRemoteReadReleasesPartialCacheDataOnError|TestRemoteCacheReadIgnoresInvalidResponses|TestRemoteCacheReadAcceptsOnlyIndicesRequestedFromTarget|TestLocalFSFallsBackAfterInvalidRemoteCacheHit)$' \
      -count=20
    
    go test -race ./pkg/fileservice \
      -run '^(TestHandleRemoteReadRejectsInvalidRequests|TestHandleRemoteReadTransfersCacheDataRelease|TestHandleRemoteReadReleasesPartialCacheDataOnError|TestRemoteCacheReadIgnoresInvalidResponses|TestRemoteCacheReadAcceptsOnlyIndicesRequestedFromTarget|TestLocalFSFallsBackAfterInvalidRemoteCacheHit)$' \
      -count=5

    实际结果

    • 6 组正式回归普通模式各运行 20 次,全部通过;
    • Race 模式各运行 5 次,全部通过;
    • 服务端对空/缺失/nil key 返回错误,不发生 slice 越界或 nil pointer panic;
    • 客户端忽略 nil、负数、越界、错误长度及跨 target 的 response;
    • 非法 remote hit 不会污染 IOEntry,LocalFS 能继续读取正确数据;
    • 正常和错误路径均正确调用 release,Race detector 未报告数据竞争。

    测试结论

    remote-cache RPC 服务端和客户端消费路径均已增加完整的形状、nil、索引范围、数据长度及 target ownership 校验。非法远端缓存数据会被拒绝或忽略,并安全回退本地存储,不再影响 CN 可用性,原问题验证通过。

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

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions