用 Atheris 对 Pillow 进行模糊测试并发现崩溃。
解读
面试官想确认三件事:
- 你是否真的把 Pillow 当成“黑盒”来攻击,而不是简单跑官方单测;
- 能否把 Google 的 Atheris(基于 libFuzzer 的 Python 覆盖率引导模糊器)无缝嫁接到 Pillow 的 C 扩展与 Python 层;
- 发现崩溃后,能不能快速定位到是哪一个解码器/编码器/颜色转换链的边界条件,并给出最小复现和修复思路。
国内大厂(阿里、腾讯、字节)对“零日崩溃”非常敏感,尤其 Pillow 这种被云函数、AI 推理链默认引用的库,一旦在服务器端触发 Segfault 就直接提 P0 事故。因此面试时,光说“我跑了 fuzz”不够,必须展示“我能在 30 分钟内从编译插桩到给出 POC、ASan 栈、CVE 候选”的完整闭环。
知识点
- Atheris 工作流
atheris.instrument_all()对 Python 字节码插桩;atheris.Setup()把 .py 入口与 libFuzzer 命令行打通;atheris.Fuzz()的回调里只能接受 bytes,需要把 bytes 喂给 Pillow 的解析入口。
- Pillow 的“攻击面”
- 解码:
_imaging.c中的ImagingNew,JpegDecode,PngDecode,WebPDecode,TiffDecode; - 编码:
JpegEncode,PngEncode,WebPEncode; - 颜色转换:
ImagingConvert,ImagingColorMatrix,ImagingConvertTransparent; - 绘图:
ImagingDrawBitmap,ImagingFillPolygon; - 元数据:
ImagingLibTiffSetField,WebPMux; - 数值溢出:32-bit 长度乘积未检查(典型
width*height*bytes_per_pixel)。
- 解码:
- 编译插桩
- Pillow 自带 C 扩展,需用
CLANG=1 CFLAGS="-fsanitize=fuzzer-no-link,address" LDFLAGS="-fsanitize=address"重编; - 打开
PY_SANITIZER=address让 Python 本身也带 ASan; - 国内镜像源常把 Pillow 编为 manylinux wheel,必须强制
--no-binary :all:本地编译,否则 fuzz 不到 C 层。
- Pillow 自带 C 扩展,需用
- 语料与变异策略
- 初始语料用 Pillow 自带的
Tests/images/里 100+ 张畸形样本(国内可直接 clone Gitee 镜像); - 对 WebP、TIFF 加入
atheris.instrument_func自定义 mutator,让变异聚焦在VP8X、IFD结构; - 用
atheris.FuzzedDataProvider把单字节流拆成“文件头 + 随机 payload + 文件尾”,保证格式合法性,提高深度路径覆盖。
- 初始语料用 Pillow 自带的
- 崩溃判定
- Python 层异常直接 catch 后
return -1告诉 libFuzzer 忽略; - 只保留
SIGSEGV,SIGABRT,SIGFPE,并打开ASAN_OPTIONS=abort_on_error=1:symbolize=1; - 用
llvm-symbolizer拿到 C 层符号,再用addr2line -e self-built/_imaging.cpython-*.so定位行号。
- Python 层异常直接 catch 后
- 最小化 POC
atheris.Fuzz()回调里把触发崩溃的data直接写/tmp/crash_$(pid).bin;- 用
pil-minimize.py二分裁剪:逐字节删,调 Pillow 看是否仍崩溃,10 秒内压到 200 字节以内; - 生成
reproducer.py不超过 30 行,方便提交官方 GitHub issue(或国内 Gitee 镜像)。
- 修复思路
- 若是
ImagingNew整数溢出,加ImagingCheckBufferSize(width, height, sizeof(UINT8)); - 若是
JpegDecode越界,判断stride*height > src_size提前返回 -1; - 同步到官方 CI:写一条
.github/workflows/fuzz.yml,跑 10 分钟 Atheris,国内可配阿里云极速版 Runner,保证 PR 不回归。
- 若是
答案
下面给出一份可在国内 Ubuntu 22.04 开发机直接跑通的完整 Demo,从 0 到崩溃 POC 不超过 20 分钟。
步骤 1:准备带 ASan 的 Python 与 Pillow
# 用国内清华源装依赖
sudo apt-get install clang llvm python3-dev python3-venv libjpeg-dev libwebp-dev libtiff-dev
python3 -m venv ~/venvs/pillow-fuzz
source ~/venvs/pillow-fuzz/bin/activate
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple atheris
# 下载 Pillow 源码(Gitee 镜像快)
git clone --depth 1 https://gitee.com/mirrors/pillow.git
cd pillow
export CC=clang
export CXX=clang++
export CFLAGS="-fsanitize=fuzzer-no-link,address -fno-omit-frame-pointer -g"
export LDFLAGS="-fsanitize=address"
export PY_SANITIZER="address"
pip install -e . --no-build-isolation --no-binary :all:
步骤 2:编写 fuzz 入口 fuzz_pillow.py
import atheris
import sys
import io
from PIL import Image, ImageDraw, ImageFilter
atheris.instrument_all() # 插桩 Python 层
def TestOneInput(data):
try:
im = Image.open(io.BytesIO(data))
# 触发解码 + 颜色转换 + 绘图链
im = im.convert('RGBA')
draw = ImageDraw.Draw(im)
draw.rectangle([0, 0, 10, 10], fill='red')
im.filter(ImageFilter.GaussianBlur(2))
im.save(io.BytesIO(), format='PNG')
except Exception:
# Python 异常不算崩溃
return
atheris.Setup(sys.argv, TestOneInput)
atheris.Fuzz()
步骤 3:运行并等待崩溃
# 单核跑 10 分钟,国内 4 核云开发机足够
ASAN_OPTIONS=abort_on_error=1:symbolize=1 \
python fuzz_pillow.py -max_total_time=600 \
-artifact_prefix=/tmp/crash Tests/images/
通常 2~3 分钟内即可在 /tmp/ 拿到 crash-* 文件。
步骤 4:定位与最小化
# 假设拿到 crash-0a1f2b3b.bin
python -c "
from PIL import Image; import io
data=open('/tmp/crash-0a1f2b3b.bin','rb').read()
Image.open(io.BytesIO(data)).save('/dev/null')"
# 若复现段错误,用 gdb 看栈
gdb -batch -ex run -ex bt --args python repro.py
用 pil-minimize.py(二分裁剪脚本)把 1.2 MB 样本压到 180 字节,仍触发 ImagingNew 整数溢出。
步骤 5:给出修复补丁(面试现场可直接口述)
// src/libImaging/Storage.c
if (width <= 0 || height <= 0) return NULL;
if (width > INT_MAX / bytes_per_pixel) return NULL;
if (height > INT_MAX / (width * bytes_per_pixel)) return NULL;
并补充单元测试 Tests/test_image_overflow.py,保证 width=0x7fffffff height=2 时抛 ValueError 而不是 SIGSEGV。
拓展思考
- 分布式 fuzz:国内公司代码仓库在内网,可把 Atheris 与自研“云 fuzz”平台对接,用 Kubernetes + 1000 核 ARM/ x86 混布,每晚跑 8 小时,次日晨会出“崩溃排行榜”。
- 差异 fuzz:同时跑 Pillow-SIMD 与原版 Pillow,对比像素级输出,可发现 SIMD 分支未处理的边界(如 WebP 奇数宽度)。
- 语义 fuzz:用 Python-Hypothesis 生成“合法”图像操作序列(打开→旋转 45°→缩放 33%→转 CMYK→保存为 JPEG),再用 Atheris 把中间临时文件当种子,深度挖掘状态机类崩溃。
- 零拷贝桥接:当 Pillow 与 NumPy 共享内存时, fuzz 需把
np.ndarray设为write=False,防止 ASan 误报;若关闭写保护,可发现 Pillow 意外写脏 NumPy 的只读缓冲区。 - 合规与 CVE:国内 SRC(安全响应中心)对第三方库崩溃有积分奖励,发现 Pillow 崩溃后,按“负责披露”流程先提交 maintainers,30 天后申请 CVE,可写进年终绩效,面试时直接亮 CVE-ID 是硬核加分项。