Repository navigation
feat!: refuse a file whose tags claim more data than it holds - #29
Merged
Merged
Conversation
A decoder reads the value of every tag into memory as it loads a tag directory, and nothing in the format stops each of thousands of tags from pointing at the same bytes: a 144 KB TIFF cost Pillow 1.6 GB inside Image.open, and a 66 KB JPEG 340 MB (CVE-2026-128066). The values of an honest directory do not overlap, so they cannot add up to more than the bytes they are read from. The readers now add them up, and a directory that claims more is refused by default: - a TIFF: each frame's directory, and the Exif, GPS and interoperability directories its tags point to, against the file; - a JPEG: the main, thumbnail, Exif, GPS and interoperability directories of its EXIF block, across every segment that carries it and in every picture of a multi-picture file, and its multi-picture index, against the block; - a PNG: the same directories of an eXIf chunk. A block whose directories hold more entries between them than it has bytes for is refused the same way, which bounds the walk by the size of the block. A value that lies outside its block is not counted, since a decoder reads nothing for it and cameras do write EXIF that sloppy. Run over 3.2 million real images, the reader reported an overlap in none. A JPEG's segments are now walked to the start of the image data, not to the frame header, because that is how far a decoder reads them: an EXIF block, a colour profile or a multi-picture index after the frame header is read by Pillow and is now read here. Up to the frame header a segment the reader cannot follow is an error, as before; after it the walk stops there, as the decoder does. Policy gains allow_overlapping_tag_data, False under DEFAULT and STRICT; Facts and Header carry `overlaps`; the command line has --overlapping-tag-data. Not seen, and said so in the README and the record: EXIF that a PNG holds in a text chunk, which may be compressed, and a camera maker's own directory inside a maker note. Closes #26 BREAKING CHANGE: a TIFF, JPEG or PNG with a tag directory whose values add up to more than the file (or the EXIF block) holds is now refused by default; allow_overlapping_tag_data=True, or --overlapping-tag-data, passes one. A JPEG's colour profile and multi-picture index are now found after its frame header as well as before it, so max_metadata_bytes and the checks on each picture apply to them there too, and a JPEG with more than 4,096 segments before its image data is refused. Co-authored-by: Yvette Mankerian <yvettekm@gmail.com> Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #26 (CVE-2026-128066, Pillow: memory denial of service in TIFF and EXIF directory parsing through overlapping tag data).
This is breaking and cuts 6.0.0: a file that passed 5.1.0 can be refused by default.
The triage
We file it as B. The trigger is a tag directory, which is a header: each entry gives the size of its value and where it lies, and the loader reads every value into memory with nothing to stop them all being the same bytes. On 5.1.0 every route passed
DEFAULTandSTRICT. The advisory's own TIFF was refused only by accident, because its tags point inside the eight-byte header; moved eight bytes on, it passed. Refusing private tags did not help, since the standard numbers serve as well.Each route was tried on Pillow 13.0.0, whose bounded loader warns where it used to read:
Image.openget_ifdImage.openImage.openeXIfchunk, before or after the image datagetexifgetexifThe change
Policy.allow_overlapping_tag_data,FalseunderDEFAULTandSTRICT;--overlapping-tag-dataallows.FactsandHeadercarryoverlaps, a sentence for each directory.What else is refused that was not
max_metadata_bytes, and one whose multi-picture index sits there has its pictures counted and checked.Will it refuse an honest file?
We ran the 5.1.0 reader and this one over about 3.2 million real images (7,501 JPEG, 126,337 TIFF, 3,064,088 PNG). This one reports an overlap in none, and the two agree on every file. A TIFF in which two tags share one value, as some writers store a single resolution, passes, and there is a test for it.
What was not done
Checks
ruff check,ruff format --check,mypy,pytest --cov(499 passed, 99%),uv lock --check,zensical build --clean --strict,advisory_record.py validate, andfuzz_headers.pywith the new carriers among its seeds and no findings. The tests check each carrier, and honest files, against Pillow's bounded loader.🤖 Generated with Claude Code