Skip to content

feat: a policy on a greyscale or palette TIFF's spare samples - #28

Merged
stephen-masters merged 1 commit into
mainfrom
feat/tiff-palette-extra-samples
Nov 16, 2026
Merged

stephen-masters merged 1 commit into
mainfrom
feat/tiff-palette-extra-samples

Conversation

@stephen-masters

Copy link
Copy Markdown
Contributor

Closes #27 (CVE-2026-128067, Pillow: heap buffer overflow saving a TIFF through libtiff when tags override the image's size).

The triage

The issue was labelled likely-not-reached because the headline says "saving". Read against the guide's question of what rides from an opened file into a save, the file is the input by three routes, and a header shows one of them. We file it as B.

Route What the file carries What the program must do Can the check refuse it?
1 A greyscale or palette TIFF with SamplesPerPixel 2 and ExtraSamples 0 A bare Image.open(f).save(out, "TIFF") Yes: both tags are in the directory the reader walks
2 A JPEG whose EXIF holds TIFF geometry tags Pass exif=im.getexif() to a compressed TIFF save No: the reader measures that block and does not parse it
3 Any TIFF with a rotating Orientation Pass the tag table or the EXIF back to the save No: the file is well formed, and every rotated scan is one

The change

  • Policy.allow_spare_tiff_samples, True by default under DEFAULT and STRICT, so nothing that passed before is refused. --no-spare-tiff-samples on the command line.
  • spare_samples on Header and Facts: the samples each pixel of a greyscale or palette TIFF carries beyond the one the image uses, unless that is a single alpha sample.
  • The TIFF reader now reads ExtraSamples. If the tag appears twice, a reading that is not alpha stands.
  • corpus.spare_sample_tiff builds a complete carrier that decodes; it also seeds the fuzzer.
  • The README's policy table, the guide's rule on save paths, and the record for the advisory.

Why greyscale as well as palette

The advisory names the palette layout. We tried a bare save of each layout on Pillow 13.0.0, whose encoder now refuses a scanline longer than its row ("encoder error -8") where it used to read past it:

Layout Pillow opens it as Bare save on 13.0.0
palette + unspecified P refused by the encoder
greyscale + unspecified L refused by the encoder
palette + alpha PA saves
greyscale + alpha LA saves
RGB + unspecified RGB saves
RGB + alpha RGBA saves
CMYK + unspecified CMYK saves

So the field covers the first two and nothing else. An alpha channel, and a spare sample on RGB or CMYK, pass whatever the setting.

What was not done

  • The overflow itself was not reproduced on a Pillow before 13.0.0; the record's notes say so.
  • The default is unchanged. Refusing these by default would be a feat!, as it was for allow_inconsistent_tiff_tags, and is a separate decision.

Checks

ruff check, ruff format --check, mypy, pytest --cov (455 passed, 99%), uv lock --check, zensical build --clean --strict, advisory_record.py validate, and thirty seconds of fuzz_headers.py with no findings.

🤖 Generated with Claude Code

One field on Policy, allow-by-default so that no file that passed
before is refused:

- allow_spare_tiff_samples: whether a greyscale or palette TIFF may give
  each pixel more samples than the one the image uses, where the rest
  are not a single alpha sample (SamplesPerPixel 2 with ExtraSamples 0,
  "unspecified data"). Pillow opens such a file as a one-band image
  while its tag still says two, and a bare save hands that tag to
  libtiff's encoder, which read two samples for every one in the row it
  was given: process memory copied into the output, or with a Predictor
  a write past the row (CVE-2026-128067). A program that saves files
  from outside back as TIFF refuses them.

The reader now reads ExtraSamples. An alpha channel is not spare, and
neither is an extra sample on RGB or CMYK, where the decoder's own
count is what the encoder is given; a bare save of each layout on
Pillow 13.0.0, whose encoder now refuses the mismatch, trips for the
greyscale and palette layouts alone. If the tag appears twice, a
reading that is not alpha stands.

Facts and Header carry `spare_samples`; the command line has
--no-spare-tiff-samples; the README documents the field. The corpus
gains a complete carrier, which also seeds the fuzzer. The advisory is
recorded as B: of the three routes by which a file is the input, this
is the one a header shows. The EXIF of a JPEG and the stale size of a
rotated TIFF reach the encoder only when the program passes them back,
and the record says why neither can be refused.

Closes #27

Co-authored-by: Yvette Mankerian <yvettekm@gmail.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@stephen-masters
stephen-masters merged commit 9a18057 into main Nov 16, 2026
11 checks passed
@stephen-masters
stephen-masters deleted the feat/tiff-palette-extra-samples branch November 18, 2026 09:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CVE-2026-128067: Pillow: Heap buffer overflow saving TIFF with libtiff when tags override image size

1 participant