已关闭
【缺陷报告】内存分配大小未受限 - 文件graph_utils.cc - 函数GraphUtils::ConvertFileConstToConst - 行号1030 #276
zhangjunkai9创建于  17 天前关闭于  11 天前
zhangjunkai9
17 天前 创建

缺陷信息

缺陷类型unboundedMemoryAllocation (内存分配大小未受限)
函数GraphUtils::ConvertFileConstToConst
文件autofuse/graph_metadef/graph/utils/graph_utils.cc
行号1030
置信度90%

缺陷描述

函数 ConvertFileConstToConst 从反序列化图属性 kLength4Recover 读取 attr_length(外部图文件数据),第1026行仅有 attr_length > 0 的下界校验无上界校验,第1028行 static_cast<size_t>(attr_length) 后直接用于第1030行 ComGraphMakeUnique<char_t[]>(file_length) 分配内存(内部使用 new (std::nothrow) char_t[file_length])。分配发生在第1032行 GetBinFromFile(预分配缓冲区版本 file_utils.cc:197)的文件大小校验之前,过大值可导致 OOM/DoS。调用链可达:LoadGEGraph(第977行) -> 第1003行调用 ConvertFileConstToConst。

事实核查

核查1-4均成立:(1)模式成立——第1025行 AttrUtils::GetInt(op_desc,kLength4Recover,attr_length) 从反序列化图属性获取 attr_length(外部图文件数据),第1026行 GE_ASSERT_TRUE(attr_length>0) 仅检查下界无上界,第1028行 static_cast<size_t>(attr_length) 无约束,第1030行 ComGraphMakeUnique<char_t[]>(file_length) 无上界校验。
(2)无有效防护——GE_CHECK_NOTNULL(bin_buff)(第1031行) 仅防崩溃不防 OOM;GetBinFromFile(第1032行) 文件大小校验在分配之后。
(3)函数可达——LoadGEGraph(第977行)在第1003行调用 ConvertFileConstToConst(compute_graph),LoadGEGraph 从外部文件加载图。
(4)触发可满足——attr_length 来自外部图文件的反序列化属性 kLength4Recover,攻击者可构造恶意图文件设巨大值,外部输入直接到达内存分配 sink,确凿可触发 OOM。
确认为真实缺陷。

数据流证据

Source(问题源头)

autofuse/graph_metadef/graph/utils/graph_utils.cc:1025 行 GE_ASSERT_TRUE(AttrUtils::GetInt(op_desc, kLength4Recover, attr_length)) 从反序列化的图属性 kLength4Recover 获取 attr_length(外部图文件数据)

Sink(问题爆发点)

autofuse/graph_metadef/graph/utils/graph_utils.cc:1030 行 ComGraphMakeUnique<char_t[]>(file_length) 使用未受限的 file_length 分配内存(内部使用 new (std::nothrow) char_t[file_length],分配发生在 GetBinFromFile 文件大小校验之前)

传播路径:

# 文件 行号 说明
1 autofuse/graph_metadef/graph/utils/graph_utils.cc 977 GraphUtils::LoadGEGraph(const char_t *const file, ...) 从外部文件加载图(入口,file 为外部输入)
2 autofuse/graph_metadef/graph/utils/graph_utils.cc 1003 GE_ASSERT_GRAPH_SUCCESS(ConvertFileConstToConst(compute_graph)) 调用 ConvertFileConstToConst 处理 FILECONSTANT 节点
3 autofuse/graph_metadef/graph/utils/graph_utils.cc 1024 int64_t attr_length = 0 声明变量
4 autofuse/graph_metadef/graph/utils/graph_utils.cc 1025 AttrUtils::GetInt(op_desc, kLength4Recover, attr_length) 从反序列化图属性获取 attr_length(外部数据)
5 autofuse/graph_metadef/graph/utils/graph_utils.cc 1026 GE_ASSERT_TRUE(attr_length > 0) 仅检查下界,无上界校验
6 autofuse/graph_metadef/graph/utils/graph_utils.cc 1028 size_t file_length = static_cast<size_t>(attr_length) 转为 size_t,无上界约束
7 autofuse/graph_metadef/graph/utils/graph_utils.cc 1030 ComGraphMakeUnique<char_t[]>(file_length) 使用未受限的 file_length 分配内存(sink,分配发生在 GetBinFromFile 文件大小校验之前)
8 autofuse/graph_metadef/graph/utils/graph_utils.cc 1032 GetBinFromFile(file_path, bin_buff.get(), file_length) 使用预分配缓冲区版本读取文件,文件大小校验在分配之后

调用链

可达调用链1 起点:(entry functions) → 终点:af::GraphUtils::ConvertFileConstToConst 深度:1

# 文件 函数 函数起始行 调用点行
1 autofuse/graph_metadef/graph/utils/graph_utils.cc:af::GE_FUNC_HOST_VISIBILITY bool GraphUtils: LoadGEGraph 977 1003
2 autofuse/graph_metadef/graph/utils/graph_utils.cc:af::GraphUtils: ConvertFileConstToConst 1007

修复建议

在 attr_length > 0 检查之后、分配之前增加上界校验:
GE_ASSERT_TRUE(attr_length > 0);
constexpr int64_t kMaxFileLength = 2LL * 1024LL * 1024LL * 1024LL;
GE_ASSERT_TRUE(attr_length <= kMaxFileLength, "attr_length exceeds maximum allowed size");
likedislike
Wwangmingming成员
17 天前 将 gcw_V3YyYBt1 设为负责人
wangmingming成员
17 天前 评论:

你好,感谢建议, 后续分析处理

likedislike
Ggaoxin成员
16 天前 关联了pull request:fix: 修复 Issue #261-#276 安全加固缺陷
CANN-robotCANN-robot成员
11 天前 关闭了 issue
CANN-robotCANN-robot成员
11 天前 添加了label:resolved