Merged
fix(pyacl): 校验 malloc_with_cfg 属性整数范围 #6297
fix(pyacl): 校验 malloc_with_cfg 属性整数范围 #6297
Merged
KaranocaVeAdd on 13 days ago
KaranocaVe
KaranocaVe
13 days ago

PyACL acl.rt.malloc_with_cfg 对 moduleId 和 deviceId 的 Python 整数直接窄化转换,超出目标字段范围时静默截断。补丁增加 uint16/uint32 范围检查,避免分配属性被悄然改变。

验证

  • 源码审计确认原实现存在两处窄化转换:uint16_t、uint32_t;对超范围值没有错误分支。补丁加入范围检查。
  • 静态验证:
  • git apply --check fix.patch 通过。

关联 Issue: https://gitcode.com/cann/runtime/issues/1376

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for KaranocaVe's contribution.)
KaranocaVeKaranocaVe
13 days ago Created pull request, commit 8882b47a
KaranocaVeKaranocaVe
13 days ago Associated issue: PyACL malloc_with_cfg 属性整数截断
CANN-robotCANN-robotCollaborators
13 days ago Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
13 days ago Comments:

Thanks for your pull-request.
The full list of commands accepted by me can be found at here.
You can get sig-info at here.
You can self-configure the PR merge rules for this repository. For more details, please refer to Here.
For more, you also can visit HICANN.


PR Approval Progress

✅ Congratulations! All modules have met the lgtm and approve requirements.

Module Approval Details

module lgtm status approve status
repo-cann/runtime ✅ 卢煜坤, zhangpengpeng8 (2/2) ✅ 卢煜坤 (1/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

KaranocaVe, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
CANN-robot
CANN-robotCollaborators
13 days ago Comments:

CLA Signature Pass

KaranocaVe, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
CANN-robotCANN-robotCollaborators
13 days ago Set wangtao43,zhangpengpeng8,yanmingxiang,Reyn52166,ykl999,houyanbao,tingwood as approver
CANN-robotCANN-robotCollaborators
13 days ago Set wangtao43,zhangpengpeng8,yanmingxiang,Reyn52166,ykl999,houyanbao,tingwood as approver
atomgit-bot
atomgit-bot
13 days ago Comments:

变更摘要

本 PR 修改 python/pyACL/rt/rt_memory.cpp 中 GetUnionValueFromPyDict 解析 aclrtMallocAttrValue 的逻辑:原实现将 Python 整数直接窄化转换为 moduleId(uint16_t)、deviceId(uint32_t)和 rsv 元素(uint8_t),超出目标字段范围时会静默截断,导致分配属性被无提示地改变;补丁在转换前增加上界检查并对越界值报错,同时要求 rsv 必须是 list 类型。

主要改动

  • moduleId 范围校验: 先取出 PyLong_AsUnsignedLong 的结果用于 PyErr_Occurred 检查,再用 UINT16_MAX 校验后才写入 value.moduleId,超范围时抛出 PyExc_ValueError。
  • deviceId 范围校验: 同样改为先暂存转换结果并以 UINT32_MAX 为界判断,越界时抛出 PyExc_ValueError,避免 uint32_t 截断。
  • rsv 类型校验: 新增 PyList_Check(pyRsv) 判断,非 list 输入直接以 PyExc_TypeError 拒绝,不再先调用 PyList_Size。
  • rsv 元素范围校验: 每个元素先取 PyLong_AsUnsignedLong 并通过 PyErr_Occurred 检查,再以 UINT8_MAX 为界校验后写入 value.rsv[i],越界抛出 PyExc_ValueError。

🧩 本次 Review 已使用自定义 Skill:

likedislike
Incorrect?
atomgit-bot
atomgit-bot
13 days ago Comments:

代码审查

✅ 未发现问题


🧩 本次 Review 已使用自定义 Skill:

likedislike
Incorrect?
KaranocaVeKaranocaVe
13 days ago Change title to "fix(pyacl): 校验 malloc_with_cfg 属性整数范围", originally titled "验证".
KaranocaVe
KaranocaVe
13 days ago Comments:

/compile

likedislike
KaranocaVeKaranocaVe
13 days ago Pre merge succeeded (commit_id: 6ab1f18cde1f2ff3e72a470f6d00115602402a23)
CANN-robot
CANN-robotCollaborators
13 days ago Comments:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robotCollaborators
13 days ago Added label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
13 days ago Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
13 days ago Added label: ci-pipeline-passed
wuqifan
wuqifan13 days agoStart code review1
python/pyACL/rt/rt_memory.cpp
@@ -330,2 +332,4 @@
330332 CHECK_BOOL(PyErr_Occurred() == nullptr, "PyLong_AsUnsignedLong failed", PyExc_ValueError);
333+ CHECK_BOOL(deviceIdVal <= UINT32_MAX, "deviceId exceeds uint32 range", PyExc_ValueError);
334+ value.deviceId = static_cast<uint32_t>(deviceIdVal);
331335 } else if (PyDict_GetItemString(pyValue, "rsv") != nullptr) {
wuqifan
wuqifan13 days agoComments:

runtime 仓库的结构文档明确说明其为“预留参数。当前固定配置为0”;当前 Device 内存分配实现对 RSV 属性直接跳过,不读取其内容。因此,正常使用 RSV 属性时,其元素截断不会改变当前的内存分配行为。建议rsv 相关校验暂不作为本次合入。

likedislike
ykl999
ykl999Collaborators
9 days ago Comments:

@KaranocaVe 请修改检视意见

likedislike
KaranocaVe
KaranocaVe
9 days ago Comments:

/compile

likedislike
KaranocaVeKaranocaVe
9 days ago Pre merge succeeded (commit_id: 2aa469c9d4c86b3d905ffa2ead38f3fed9e82ff4)
CANN-robot
CANN-robotCollaborators
9 days ago Comments:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robotCollaborators
9 days ago Removed label: ci-pipeline-passed
CANN-robotCANN-robotCollaborators
9 days ago Added label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
9 days ago Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
9 days ago Added label: ci-pipeline-passed
ykl999
ykl999Collaborators
7 days ago Comments:

@KaranocaVe 请修改下检视意见

likedislike
ykl999
ykl999Collaborators
5 days ago Comments:

@KaranocaVe 请修改检视意见

likedislike
KaranocaVeKaranocaVe
3 days ago Pre merge succeeded (commit_id: 99939ad092377684551c670c1068f10071642d96)
5 event messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
3 days ago Removed label: ci-pipeline-passed
CANN-robot
CANN-robotCollaborators
3 days ago Comments:

Notification

This pull request has been changed(code update) or closed, so removes the following label(s): ci-pipeline-passed.

likedislike
KaranocaVeKaranocaVe
3 days ago Modify description of pull request
KaranocaVe
KaranocaVe
3 days ago Comments:

/compile

likedislike
KaranocaVeKaranocaVe
3 days ago Pre merge succeeded (commit_id: 269837e3713171e92d45aee24dfcefbd4efb3f8e)
KaranocaVe
KaranocaVe
3 days ago Comments:

@weixin_46600764 @ykl999 已按评审意见修改:去掉了 rsv 的校验(PyList_Check、PyErr 检查、UINT8_MAX 检查及临时变量 item),rsv 解析恢复为 master 原样;moduleId(uint16)与 deviceId(uint32)的范围检查保留,PR 正文也同步去掉了 rsv 校验的表述。麻烦复审,谢谢!

likedislike
CANN-robot
CANN-robotCollaborators
3 days ago Comments:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robotCollaborators
3 days ago Added label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
3 days ago Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
3 days ago Added label: ci-pipeline-passed
ykl999
ykl999Collaborators
3 days ago Comments:

/approve
/lgtm

likedislike
ykl999ykl999Collaborators
3 days ago Resolved final issue
CANN-robotCANN-robotCollaborators
3 days ago Added label: approved
zhangpengpeng8
zhangpengpeng8Collaborators
3 days ago Comments:

/lgtm

likedislike
CANN-robotCANN-robotCollaborators
3 days ago Added label: lgtm
CANN-robotCANN-robotCollaborators
3 days ago Merged pull request