Closed
PyACL 字典标量转换静默窄化 #1188
KaranocaVeAdd on  Oct 21Closed at  24 days ago
KaranocaVe
KaranocaVe
Oct 21 Create

PyACL 字典标量转换静默窄化

现象

Runtime 的 Python binding 通用 GetValueFromPyDict 在 int 和 short 标量路径中先将 Python 整数直接窄化,再检查 PyErr_Occurred()。在 64 位平台,PyLong_AsLong 可成功表示但超出目标字段范围的值(如 2**32、32768)会被静默截断,调用方收到与输入不同的配置。

影响

多个 pyACL 配置接口共用该 helper。错误的枚举、计数或短整数字段可能被改写后继续调用底层 ACL,导致配置失真且缺少输入错误提示。

根因与修复

保留 long 中间值,在赋给 int/short 前分别检查 INT_MIN..INT_MAX 与 SHRT_MIN..SHRT_MAX,超范围返回 ValueError。

验证

  • 源码审计确认原实现分别使用 static_cast<int>(PyLong_AsLong(...)) 和 static_cast<short>(PyLong_AsLong(...)),窄化发生在错误检查前。
  • 补丁加入目标类型范围检查;git diff --check 通过。
  • 基于上游 master 的 93b7ec22 运行目标仓库 pre-commit(clang-format v16.0.0、OAT),python/pyACL/utils_methods.cpp 检查通过。

关联 PR: https://gitcode.com/cann/runtime/merge_requests/5715

likedislike
KaranocaVeKaranocaVe
Oct 21 Associated pull request
KaranocaVeKaranocaVe
Oct 21 Modify description of issue
xiaocunxiaocunCollaborators
Oct 21 Assign yongfei-hu as responsible person
xiaocunxiaocunCollaborators
Oct 21 Removed responsible person yongfei-hu
wuqifan
wuqifan
Oct 21 Comments:

/assign

likedislike
CANN-robotCANN-robotCollaborators
Oct 21 Assign weixin_46600764 as responsible person
wuqifan
wuqifan
Oct 21 Comments:

感谢反馈!您所提的pyACL接口问题已收到,根据提供的信息核实:GetValueFromPyDict 的 int 与 short 两条转换路径确实存在窄化前未做范围检查的问题,超范围取值会被静默截断。我们正在验证该问题,由于该接口在多处被调用,修复会在通过测试后合入。

likedislike
wuqifan
wuqifan
26 days ago Comments:

您好,感谢反馈!
已对该问题及 MR !5715 进行了修复前后对照。原实现确实会在检查 Python 异常前将 long 窄化为目标类型;当输入超出目标类型范围但仍可由 long 表示时,值可能被静默改变。以 int 路径为例,修复前通过 acl.rt.binary_load_from_file 传入 2**32,底层调用观察到的值为 0;MR 版本返回 ValueError,且未进入底层调用。short helper 对 32768 的对照结果也由修复前的 -32768 变为 ValueError。正常值及类型边界值的对照通过。我们会尽快推进合入,感谢贡献修复!

likedislike
ykl999
ykl999Collaborators
24 days ago Comments:

pr已合入,issue关闭处理

likedislike
ykl999ykl999Collaborators
24 days ago changed custom state from 进行中 to 已解决
ykl999ykl999Collaborators
24 days ago Close issue
CANN-robotCANN-robotCollaborators
24 days ago Added label: resolved