p PatchFirst

CVE-2026-112902

CVE-2026-112902 · Pillow: Memory disclosure and segfault in libtiff encoder via crafted TIFF tags

This page contains facts from public vulnerability sources. Check the original advisory before updating.

This page shows selected source facts. See the linked advisories for the complete description and affected versions.

``` ============================================================================ Memory disclosure and segfault in libtiff encoder via crafted TIFF tags Location: src/encode.c:1014 in PyImaging_LibTiffEncoderNew() Commit: a707c6b21c313880d0771e54965d142dd42358b6 ============================================================================ ``` A crafted TIFF file of a few hundred bytes makes Pillow copy an attacker-chosen amount of its own process memory into the TIFF it writes back out, or crash the process outright. The trigger is an ordinary `Image.open()` followed by a bare `im.save(fp, "TIFF")`. The only precondition is that the application re-saves images as TIFF. When the uploaded file is compressed, Pillow routes that save through libtiff automatically, with no options from the application. The attached reproducer does this with a 356-byte LZW TIFF: one crafted tag comes back holding 65536 bytes taken from `_imaging.so`'s read-only data, and a second crafted tag makes the worker die with SIGSEGV on every run. The bug is in `PyImaging_LibTiffEncoderNew` (`src/encode.c`). It decides what arguments to pass to the variadic `TIFFSetField()` from the shape of the Python value (scalar, tuple or bytes). It assumes its `ImagingLibTiffMergeFieldInfo()` call defined how libtiff will read them. But `TIFFMergeFieldInfo()` never overrides libtiff's built-in field table, so libtiff reads the arguments according to its own definition of the tag: - For a counted array tag such as DNGPrivateData (50740), a count-1 integer in the source file is taken as the byte count. The data pointer is then read from an argument slot Pillow never wrote, and that many bytes are copied into the output. - For a fixed-size array tag such as ActiveArea (50829), the integer itself is used as the source address. Where that unwritten slot points depends on the compiler. In the tested clang build it points into `_imaging.so`, and a larger count returned thousands of pointers into libpython, libc and libtiff, which defeats address-space randomisation for anyone chaining another bug. When the application preserves metadata with `tiffinfo=im.tag_v2`, a 64-bit entry turns ActiveArea into a 16-byte read from an address the attacker chooses. All observed memory errors are reads; no out-of-bounds write was found. Other branches of the same loop share the defect and are reachable from crafted TIFFs or from EXIF in JPEG/PNG/WebP re-saved as compressed TIFF. These are the `bytes`, RATIONAL-tuple and typed-tuple branches, and a BYTE-typed ColorMap. The file-reachable forwarding was introduced in `a3d45e9cefd0` ("Added custom int and float TIFF tags") and first released in 5.4.0. ## Details `PyImaging_LibTiffEncoderNew` (`src/encode.c:777-1060`) chooses the argument list it hands to the variadic `TIFFSetField()` from the *Python* value of each tag — scalar vs. tuple vs. `bytes`, and a type that is either autodetected or copied from the source file's IFD — and assumes that its own `ImagingLibTiffMergeFieldInfo()` registration defines how libtiff will read the `va_list`. It does not: libtiff keeps its built-in definition for every tag it already knows, and core tags are never registered at all. libtiff therefore consumes the varargs as `(count, pointer)`, `(pointer)` or three pointers according to *its* field table, whatever Pillow actually pushed. The encoder never consults that table (`TIFFFindField` / `TIFFFieldPassCount` / `TIFFFieldWriteCount` / `TIFFFieldDataType`) before the call. The most severe instance, and the one the attached reproducer exercises: when a TIFF is re-saved through the libtiff writer, every tag of the source IFD that Pillow does not recognise is forwarded to libtiff with `TIFFSetField()`. A count-1 integer entry is passed as a single scalar vararg. If libtiff's own field table defines that tag number as an *array* field, libtiff consumes the varargs as `(count, pointer)` or `(pointer)` instead, so the attacker's integer becomes either the number of bytes to copy from a never-written vararg slot, or the source address itself. The same mismatch is reachable through the typed tuple branches (tuple length consumed as a pointer for fixed-count built-in tags such as `CFARepeatPatternDim`, even when the source file is well-formed), the `bytes`, `TIFF_RATIONAL`-tuple and core-tag branches of the same loop; those are listed under *Other branches with the same defect* below and need to be covered by the same fix. A 181-byte TIFF (16×16 RGB, one LZW strip, plus one extra IFD entry `50740 / LONG / count 1 / value 4096` — DNGPrivateData) opened with `Image.open()` and saved with a bare `im.save(buf, "TIFF")` produces a 4296-byte output whose tag 50740 holds 4096 bytes that occur nowhere in the input; scanning `/proc/self/mem` finds them in the read-only data segment of `_imaging.so`. Changing the extra entry to `50829 / LONG / 1 / 64` (ActiveArea) makes the same two calls segfault in `memcpy(dst, src=0x40, n=16)` under `ImagingLibTiffSetField` ← `PyImaging_LibTiffEncoderNew`. An ASan build of the extensions reports `READ of size 4096` in `memcpy` called from `src/encode.c:1014` for the first input. ### Code path `_save` copies all tags of the opened image into the libtiff attribute list. For a tag that is neither in `LIBTIFF_CORE` nor in `TiffTags.TAGS_V2`, no type is recorded, so the value goes through untyped: ```python # src/PIL/TiffImagePlugin.py:1782 supplied_tags = {**legacy_ifd, **getattr(im, "tag_v2", {})} … # :1959-1982 for tag, value in itertools.chain(ifd.items(), supplied_tags.items()): if tag not in TiffTags.LIBTIFF_CORE: … elif isinstance(value, (int, float, str, bytes)) or …: type = TiffTags.lookup(tag).type # None for 50740, 50829, … if type: types[tag] = type if tag not in atts and tag not in blocklist: … atts[tag] = value # plain int 4096 ``` `compression` defaults to the source image's `im.info["compression"]` (`TiffImagePlugin.py:1731-1746`), so a compressed source selects the libtiff writer without the application asking for it. In the encoder the untyped `int` is autodetected as `TIFF_LONG` (`src/encode.c:805-815`), the tag is "registered" as a scalar custom field, and then set with one 32-bit vararg: ```c // src/encode.c:844 (is_var_length = 0 → writecount 1, passcount 0) if (ImagingLibTiffMergeFieldInfo(&encoder->state, type, key_int, is_var_length)) { … continue; } … // src/encode.c:1013-1016 } else if (type == TIFF_LONG || type == TIFF_IFD) { status = ImagingLibTiffSetField( &encoder->state, (ttag_t)key_int, (UINT32)PyLong_AsLong(value) ); ``` The registration does not do what the caller assumes. `TIFFMergeFieldInfo()` only adds definitions for tags libtiff does not already know and returns 0 (success) either way, so for tags in libtiff's built-in table the built-in definition stays in force. Queried against the linked libtiff 4.5.0 after exactly Pillow's merge call: ``` 50740 DNGPrivateData type=BYTE passcount=1 writecount=-1 (unchanged) 50829 ActiveArea type=LONG passcount=0 writecount=4 (unchanged) 65000 CustomField type=LONG passcount=0 writecount=1 (Pillow's definition, tag was unknown) ``` libtiff's setter for `FIELD_CUSTOM` tags then interprets the `va_list` according to *its* definition: for `passcount=1` it reads `va_arg(ap, int)` as the element count — the attacker's 4096 — allocates `count * size` bytes and `memcpy`s that many bytes from `va_arg(ap, void *)`, a slot `PyImaging_LibTiffEncoderNew` never wrote. On x86-64 that slot is register `rcx`; in this build it holds a pointer into `_imaging.so`'s `.rodata`, so the copy succeeds deterministically (a value of 2 000 000 also succeeds and yields a 2 000 200-byte output). `TIFFWriteDirectory` writes the copied bytes into the output file as the tag's value. For fixed-count array fields (`passcount=0`, `writecount=

Fix available

Published
Updated

Stack: python

Affected products and versions

  • pillow PyPI

    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
    Affected ranges
    • ECOSYSTEM: < 13.0.0

    Update to 13.0.0

Original sources

Check your server →