Skip to content

Kernel: a JPEG-compressed TIFF strip bypasses the 100M pixel cap the loader documents #505

Closed

Description

@Tim81

TiffImageLoader.Load promises the 100,000,000-pixel safety limit and does not honour it for a
strip compressed with new-style JPEG.

src/VellumPdf.Kernel/Images/TiffImageLoader.cs:397 returns JpegImageLoader.Load(jpegBytes)
directly. JpegImageLoader is the one loader that never calls ImageLimits.ValidateDimensions
(grep -n "ValidateDimensions" src/VellumPdf.Kernel/Images/*.cs lists Bmp, Ccitt, Gif, Jbig2, Jpx
twice, Png and Tiff, and no Jpeg), so the dimensions come from the embedded JPEG's frame header
unchecked. The TIFF loader's own ValidateDimensions call runs against the IFD's declared size,
which the returned image then ignores.

Measured

A 145-byte little-endian TIFF, ImageWidth = 8, ImageLength = 8, Compression = 7, one strip,
the strip being a 23-byte JPEG whose SOF0 declares 65535 by 65535:

JpegImageLoader.Load(jpeg)     65535x65535 = 4,294,836,225 pixels
TiffImageLoader.Load(tiff)     65535x65535 = 4,294,836,225 pixels

The same 65535-square declaration presented as a GIF is refused:

InvalidDataException: GIF dimensions 65535×65535 exceed the 100M pixel safety limit.

What is and is not at risk

No raster is allocated in process: PdfImageXObject sets passthrough for a non-Flate filter and
wraps the input array, so the decode allocates on the order of two kilobytes. What escapes is the
image dictionary, which carries /Width 65535 /Height 65535 over 25 bytes of /Length. A
consumer that sizes a buffer from those entries can be made to allocate from a 145-byte file, and
the library's own documentation tells them the loader checked.

The IFD/JPEG disagreement is itself worth refusing: the file declares 8 by 8 in one place and
65535 by 65535 in another, and nothing reports the contradiction.

Scope

  • Validate the dimensions the JPEG frame header declares, in JpegImageLoader, so every loader
    reaches the same cap. That is the root fix and it covers both entry points.
  • For the TIFF path, additionally refuse a strip whose embedded JPEG dimensions disagree with the
    IFD's ImageWidth and ImageLength, since one of the two numbers is wrong whichever way it
    resolves.
  • A test per entry point, with the 145-byte fixture above committed, plus one asserting the
    agreement check.
  • Consider whether MaxRasterDecodeDimension, the 1,000,000-per-edge constant, should apply to
    the raster loaders at all. It is currently read only by MmrDecoder, so a PNG, BMP or TIFF
    declaring an edge of 1,000,001 under the pixel cap loads today. That is a separate decision
    from this one but was found alongside it.

Why not fixed in 2.3.2

Adding validation to JpegImageLoader refuses files it accepts today. Under the release rule for
this line, a change may only alter output that is already wrong, and a 65535-square JPEG is a file
someone could in principle hold; turning it into an exception is a behaviour change that wants its
own decision. The documentation on both members states the hole meanwhile.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions