CVE-2026-112394: Pillow: P;2L and P;4L decoders may read 1 to 3 bytes past the passed buffer size

Medium

OSV id
GHSA-4558-3h8r-v8xp
Also known as
BIT-pillow-2026-112394, CVE-2026-112394, PYSEC-2026-4327
Published
Last modified

Affected packages

PackageAffected versionsFixed in
pillow
PyPI
< 13.0.0 13.0.0

Versions we track that it affects: pillow 10.4.0

How to fix it

Upgrade pillow to 13.0.0 or later.

# in requirements.txt: change the line to pillow==13.0.0
pip install -r requirements.txt
  • If it is an indirect dependency, upgrade the package that depends on it, or add a minimum version constraint for it (e.g. in constraints.txt, or as a dependency in pyproject.toml).
  • Check the changelog for breaking changes (especially across major versions), update the lockfile with the package manager, and run your tests.

Not sure whether your project uses an affected version? Check your lockfile below for free.

Details

Summary

On main, even with the guard in #9725, it's possible to have these decoders read 1 to 3 bytes past the end of the passed buffer with a suitable width/height.

Details

ImagingRawDecode and the P;2L / P;4L unpackers disagree about how many bytes a line of data occupies.

The raw decoder derives the line size from the bits per pixel

state->bytes = (state->xsize * state->bits + 7) / 8;

and uses it both for the bounds check and to advance to the next line. For P;2L and P;4L, bits is 2 and 4 respectively.

But these two rawmodes are not packed pixels but 2 or 4 bit planes, each padded to a whole number of bytes. unpackP2L computes s = (pixels + 7) / 8 and reads plane k at offset k * s, and unpackP4L does the same across four planes. Their real footprint is therefore planes * ((xsize + 7) / 8).

For some widths, padding each plane separately makes this footprint larger than state->bytes. For example, at width 1, the decoder accounts for one byte, while the unpackers read two or four bytes. The decoder accepts data that is too short for the last line and on the other hand advances to subsequent lines by less than a line, so every line after the first is also read from the wrong offset.

9725's check is misshapen for this:

  • It can reject data of the right length: 8x1 P;2L image is 2 bytes but is not accepted.
  • It still accepts buffers shorter than the unpackers require for a given image shape.
  • It measures the length with len(data), which is an element count rather than a byte count; e.g. memoryview(array("I", [0,0,0,0,0])) (20 bytes) is rejected while mv.tobytes() is accepted.
PoC ( ghsa_p2l_p4l.py)
from array import array
from PIL import Image

# 1 byte past
Image.frombytes("P", (1, 4), array("B", [0] * 4), "raw", "P;2L")

# 3 bytes past
Image.frombytes("P", (1, 8), array("B", [0] * 8), "raw", "P;4L")

is the almost-simplest reproducer.

(array is needed to get ASan to reliably scream, but the overread is observable with e.g. bytes literals. Python allocates byteses with an extra trailing null byte, from an arena by default to boot, so ASan doesn't see the overread.)

On my Mac, I can get ASan to scream about this with

export asan=$(clang -print-file-name=libclang_rt.asan_osx_dynamic.dylib)
env CFLAGS="-fsanitize=address -fno-omit-frame-pointer -g -O1" LDFLAGS="-fsanitize=address" uv pip install -e .
env DYLD_INSERT_LIBRARIES=$asan PYTHONMALLOC=malloc ASAN_OPTIONS=detect_leaks=0 python3 ghsa_p2l_p4l.py

like so:

<details>

``` python3(29144,0x202fea2c0) malloc: nano zone abandoned due to inability to reserve vm space. unpackP2L: in=0x602000037370 i=0 j=0 pixels=1 reads 0x602000037370 0x602000037371 unpackP2L: in=0x602000037371 i=0 j=0 pixels=1 reads 0x602000037371 0x602000037372 unpackP2L: in=0x602000037372 i=0 j=0 pixels=1 reads 0x602000037372 0x602000037373

unpackP2L: in=0x602000037373 i=0 j=0 pixels=1 reads 0x602000037373 0x602000037374

==29144==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x602000037374 at pc 0x00010778c684 bp 0x00016d029f40 sp 0x00016d029f38 READ of size 1 at 0x602000037374 thread T0 #0 0x00010778c680 in unpackP2L+0x17c (imaging.cpython-314-darwin.so:arm64+0xb4680) #1 0x000107765360 in ImagingRawDecode+0x348 (imaging.cpython-314-darwin.so:arm64+0x8d360) #2 0x0001076f259c in decode+0x15c (imaging.cpython-314-darwin.so:arm64+0x1a59c) #3 0x0001030d921c in methodvectorcallVARARGS.llvm.8905850704545875072+0x84 (python3.14:arm64+0x10030521c) #4 0x000102e585b8 in TAILCALLCALL.llvm.12785051352303791014+0x13c (python3.14:arm64+0x1000845b8) #5 0x000102f1c17c in PyEvalVector+0x308 (python3.14:arm64+0x10014817c) #6 0x000102f31864 in PyEvalEvalCode+0x9c (python3.14:arm64+0x10015d864) #7 0x000102f3fa14 in runmod.llvm.1224844961852478530+0x120 (python3.14:arm64+0x10016ba14) #8 0x000103063eb0 in pyrunfile+0xa0 (python3.14:arm64+0x10028feb0) #9 0x000102dfe834 in PyRunSimpleFileObject+0xfc (python3.14:arm64+0x10002a834) #10 0x00010306cb60 in PyRunAnyFileObject+0x4c (python3.14:arm64+0x100298b60) #11 0x00010306ca54 in pymainrunfileobj+0xa0 (python3.14:arm64+0x100298a54) #12 0x000102ffb80c in pymainrunfile+0x44 (python3.14:arm64+0x10022780c) #13 0x000102ff7d0c in PyRunMain+0x46c (python3.14:arm64+0x100223d0c) #14 0x000102fb0820 in pymain_main.llvm.7308572997346072671+0x1cc (python3.14:arm64+0x1001dc820) #15 0x000102fb0644 in main+0x20 (python3.14:arm64+0x1001dc644) #16 0x000194c86b94 (<unknown module>)

0x602000037374 is located 0 bytes after 4-byte region [0x602000037370,0x602000037374) allocated by thread T0 here: #0 0x0001044a130c in malloc+0x78 (libclangrt.asanosxdynamic.dylib:arm64e+0x3d30c) #1 0x000102f22324 in newarrayobject+0x6c (python3.14:arm64+0x10014e324) #2 0x000102f21b4c in arraynew+0x320 (python3.14:arm64

References

Am I affected?

Upload or paste your lockfile. We check every package version in it against OSV.dev, this advisory included, and email you the vulnerable ones with the version that fixes them. Free, and no account needed.

or paste its content

Needed when you paste. package.json and pyproject.toml only have version ranges: use the lockfile next to them.

Only package names and versions are checked; the file itself is not kept. We use your email to send this report and nothing else unless you tick the box above. Privacy policy.

About this data

Source: GitHub Advisory Database via OSV.dev (CC-BY 4.0). Severity comes from the advisory's own rating when it has one, else from its CVSS score.