Details
Impact
Anyone who can get 512 bytes in front of Image.open() — an upload endpoint, thumbnailer, or any service that uses Pillow to sniff whether a blob is an image — pins one CPU core of the handling process for roughly six minutes per request with the default plugin set and default limits. Nothing large is allocated and no DecompressionBombWarning/Error fires; application-side upload size limits do not help because the file is tiny, and Image.MAX_IMAGE_PIXELS is never consulted because no image is ever identified. A handful of concurrent requests is enough to exhaust a typical worker pool. The per-call cost is bounded by the 512-byte header read, so this is a CPU-exhaustion denial of service of the handling process rather than an infinite loop. Applications that pass an explicit formats= allowlist to Image.open() that excludes "XBM" are not affected.
This is the same bug class as CVE-2021-25292 (catastrophic backtracking in the PdfParser regexes, fixed in 8.1.1) and CVE-2021-23437 (ImageColor.getrgb ReDoS, fixed in 8.3.2), in a different plugin; unlike the PDF case it is reached from plain Image.open() on any byte stream.
Reproducing
repro is a single Python script. It uses the installed Pillow through the public Image.open() API and needs no rebuild.
Each input is the 512-byte header from Details: four #define lines with k spaces after #define, lines ending in \r, k \r bytes after _y_hot 1, padding with A up to the 512 bytes that _open() reads. The only thing that changes between inputs is the tail: _bits[] (valid) or _bits (the final literal can never match).
- Control. Opens the k = 60 header with the tail
_bits[]. The regex matches on its first try and Pillow reportsXBM (1, 1)in about 50 ms. This shows that the same bytes are only slow when the match has to fail. - Attack. Opens the header with the tail
_bitsat k = 10, 15, 20 and 25. Each one raisesUnidentifiedImageError, after about 0.1 s, 0.75 s, 3.0 s and 8.6 s. The time grows polynomially, not linearly. The k = 60 input from Details, which takes about 6 minutes of CPU, is not run.
Expected output on this commit:
control k=60 '_bits[]': XBM (1, 1) in 0.052s
attack k=10 '_bits' : UnidentifiedImageError in 0.105s
attack k=15 '_bits' : UnidentifiedImageError in 0.748s
attack k=20 '_bits' : UnidentifiedImageError in 3.024s
attack k=25 '_bits' : UnidentifiedImageError in 8.561s
BUG
The script prints BUG only when all of these hold:
- the control header opened as
XBMin under 1 s; - the k = 25 attack input took more than 2 s;
- the k = 25 attack input took more than 5 times as long as the k = 15 one, i.e. the cost grows super-linearly in k.
Otherwise it prints OK. With the attached patch applied, every attack input is rejected at once, so the script prints OK.
Suggested fix
The attached patch removes the overlap between [ \t]+ and the quantifier that matches the identifier prefix in xbm_head (src/PIL/XbmImagePlugin.py:32-40), so the whitespace after #define can be consumed in exactly one way and the identifier cannot run across line terminators. It matches the identifier with [^ \t\r\n]* (and [^ \t\r\n_]* in the hotspot group, which keeps the existing "no underscore before _x_hot/_y_hot" behaviour); the remaining quantifier pairs are then either over disjoint character classes or reduce to a single linear rescan of at most 512 bytes:
xbm_head = re.compile(
- rb"\s*#define[ \t]+.*_width[ \t]+(?P<width>[0-9]+)[\r\n]+"
- b"#define[ \t]+.*_height[ \t]+(?P<height>[0-9]+)[\r\n]+"
+ rb"\s*#define[ \t]+[^ \t\r\n]*_width[ \t]+(?P<width>[0-9]+)[\r\n]+"
+ rb"#define[ \t]+[^ \t\r\n]*_height[ \t]+(?P<height>[0-9]+)[\r\n]+"
b"(?P<hotspot>"
- b"#define[ \t]+[^_]*_x_hot[ \t]+(?P<xhot>[0-9]+)[\r\n]+"
- b"#define[ \t]+[^_]*_y_hot[ \t]+(?P<yhot>[0-9]+)[\r\n]+"
+ rb"#define[ \t]+[^ \t\r\n_]*_x_hot[ \t]+(?P<xhot>[0-9]+)[\r\n]+"
+ rb"#define[ \t]+[^ \t\r\n_]*_y_hot[ \t]+(?P<yhot>[0-9]+)[\r\n]+"
b")?"
rb"[\000-\377]*_bits\[]"
)
This mirrors the PdfParser fix for CVE-2021-25292. The patch also adds a test_redos case to Tests/test_file_xbm.py, in the style of the one in Tests/test_file_pdf.py: it builds the k = 60 header from Details and, under @timeout_unless_slower_valgrind(1), expects XbmImageFile to raise SyntaxError. With the patch applied to a clean checkout and rebuilt, the reproducer prints OK and the existing Tests/test_file_xbm.py (including hopper.xbm, hopper_underscore.xbm and the hotspot round-trip) still passes. The change does reject headers whose identifier contains whitespace (#define foo bar_width 16), which is not valid C and is not produced by Pillow's own XBM writer.
The patch
Use more specific regex chars in the XBM header regex to prevent ReDoS in Image.open() on a crafted 512-byte header
Applies to src/PIL/XbmImagePlugin.py at the commit above.
diff --git a/Tests/test_file_xbm.py b/Tests/test_file_xbm.py
index 1988c417f..67236c795 100644
--- a/Tests/test_file_xbm.py
+++ b/Tests/test_file_xbm.py
@@ -6,7 +6,7 @@ import pytest
from PIL import Image, XbmImagePlugin
-from .helper import hopper
+from .helper import hopper, timeout_unless_slower_valgrind
TYPE_CHECKING = False
if TYPE_CHECKING:
@@ -91,3 +91,17 @@ def test_hotspot(tmp_path: Path) -> None:
with Image.open(out) as reloaded:
assert reloaded.info["hotspot"] == hotspot
+
+
+@timeout_unless_slower_valgrind(1)
+def test_redos() -> None:
+ k = 60
+ malicious = b"#define" + b" " * k + b"_width 1\r"
+ malicious += b"#define" + b" " * k + b"_height 1\r"
+ malicious += b"#define" + b" " * k + b"_x_hot 1\r"
+ malicious += b"#define" + b" " * k + b"_y_hot 1" + b"\r" * k + b"_bits"
+ malicious += b"A" * (512 - len(malicious))
+
+ # The important thing is it doesn't timeout, cause a ReDoS
+ with pytest.raises(SyntaxError):
+ XbmImagePlugin.XbmImageFile(BytesIO(malicious))
diff --git a/src/PIL/XbmImagePlugin.py b/src/PIL/XbmImagePlugin.py
index 95729d73d..7435d20f2 100644
--- a/src/PIL/XbmImagePlugin.py
+++ b/src/PIL/XbmImagePlugin.py
@@ -29,12 +29,14 @@ if TYPE_CHECKING:
from typing import IO
# XBM header
+# The identifier before each suffix cannot contain whitespace, so that it
+# cannot overlap with the preceding [ \t]+ and cause a ReDoS
xbm_head = re.compile(
- rb"\s*#define[ \t]+.*_width[ \t]+(?P<width>[0-9]+)[\r\n]+"
- b"#define[ \t]+.*_height[ \t]+(?P<height>[0-9]+)[\r\n]+"
+ rb"\s*#define[ \t]+[^ \t\r\n]*_width[ \t]+(?P<width>[0-9]+)[\r\n]+"
+ rb"#define[ \t]+[^ \t\r\n]*_height[ \t]+(?P<height>[0-9]+)[\r\n]+"
b"(?P<hotspot>"
- b"#define[ \t]+[^_]*_x_hot[ \t]+(?P<xhot>[0-9]+)[\r\n]+"
- b"#define[ \t]+[^_]*_y_hot[ \t]+(?P<yhot>[0-9]+)[\r\n]+"
+ rb"#define[ \t]+[^ \t\r\n_]*_x_hot[ \t]+(?P<xhot>[0-9]+)[\r\n]+"
+ rb"#define[ \t]+[^ \t\r\n_]*_y_hot[ \t]+(?P<yhot>[0-9]+)[\r\n]+"
b")?"
rb"[\000-\377]*_bits\[]"
)