Closed
[Bug-Report|缺陷反馈]: GatherV2 AICPU 负索引缺少下界检查,可能导致越界读 #8948
ayuemoonAdd on  4 days agoClosed at  4 days ago
ayuemoon
ayuemoon
4 days ago Create

Describe the current behavior / 问题描述

index/gather_v2/op_kernel_aicpu/gather_v2_aicpu.cpp 中,CopyGatherV2Data 对负索引加一次 gather_dim_size 后仅检查上界,缺少下界检查。

例如 gather_dim_size=10、索引为 -11 时,归一化结果为 -1,仍通过原有上界检查。该值随后进入 params_idx,可能使 BiggerMemCpy 从 params 缓冲区之前读取数据,存在信息泄露或进程崩溃风险。

Environment Information / 环境信息

  • 验证环境硬件:Ascend910B3。
  • 修复提交:d7c2c99a826604ada5bdf9c258500bed5d377740。
  • CANN 版本:未记录。
  • 已验证范围:主机执行的 AICPU kernel UT;设备侧执行和真实图编译常量折叠未单独验证。

Steps to reproduce the issue / 重现步骤

以下为修复前源码中的触发条件:

  1. 构造 GatherV2 AICPU kernel 输入:params 为含 10 个元素的 double 张量,indices 为 int64 张量 [-11],axis 为 0,batch_dims 为 0。
  2. 执行 kernel。原实现将 -11 归一化为 -1,检查 -1 < 10 后继续计算源地址。
  3. 首个 batch、首个外层位置的 params_idx 为负数,形成非法源地址。极端负值 -100、INT64_MIN 也需要在地址计算前拦截。

Describe the expected behavior / 预期结果

在归一化和源地址计算前检查原始索引范围 [-gather_dim_size, gather_dim_size)。非法索引返回 KERNEL_STATUS_PARAM_INVALID,保持 AICPU 现有上界越界报错行为;合法负索引仍可正常归一化。

维度为 10 时,-10、-1、9 合法,-11、-100、INT64_MIN、10 非法。

修复后已执行:

bash build.sh --opkernel_aicpu_test --ops=gather_v2 --asan
LD_PRELOAD=$(gcc -print-file-name=libasan.so) ASAN_OPTIONS=detect_leaks=0 ./build/tests/ut/op_kernel_aicpu/nn_aicpu_op_kernel_ut

测试输出:

[==========] 9 tests from 1 test suite ran.
[  PASSED  ] 9 tests.

新增用例覆盖合法负索引边界,以及 double、complex64、complex128 对应的 -11、-100、INT64_MIN 非法索引。ASan 运行未报告内存越界错误。

Special notes for this issue / 备注

修复 PR:https://gitcode.com/cann/ops-nn/pull/14527

修复通过归一化前的双界检查阻止非法负索引进入地址计算。主机常量折叠与设备 AICPU 使用同一份 kernel 源码,但本次测试结果仅覆盖主机 kernel UT。

likedislike
ayuemoonayuemoon
4 days ago Added label: bug-report
ayuemoon
ayuemoon
4 days ago Comments:

/assign

likedislike
CANN-robotCANN-robotCollaborators
4 days ago Assign ayuemoon as responsible person
CANN-robotCANN-robotCollaborators
4 days ago Close issue
CANN-robotCANN-robotCollaborators
4 days ago changed custom state from 待办的 to 已完成
CANN-robotCANN-robotCollaborators
4 days ago Added label: resolved