Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release/2.1] Tiff decoding robustness improvements (#2550) #2554

Merged
merged 4 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 20 additions & 25 deletions src/ImageSharp/Compression/Zlib/ZlibInflateStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,29 +161,25 @@ public override int Read(byte[] buffer, int offset, int count)
bytesToRead = Math.Min(count - totalBytesRead, this.currentDataRemaining);
this.currentDataRemaining -= bytesToRead;
bytesRead = this.innerStream.Read(buffer, offset, bytesToRead);
if (bytesRead == 0)
{
return totalBytesRead;
}

totalBytesRead += bytesRead;
}

return totalBytesRead;
}

/// <inheritdoc/>
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotSupportedException();
}
public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();

/// <inheritdoc/>
public override void SetLength(long value)
{
throw new NotSupportedException();
}
public override void SetLength(long value) => throw new NotSupportedException();

/// <inheritdoc/>
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotSupportedException();
}
public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();

/// <inheritdoc/>
protected override void Dispose(bool disposing)
Expand Down Expand Up @@ -245,22 +241,17 @@ private bool InitializeInflateStream(bool isCriticalChunk)
// CINFO is not defined in this specification for CM not equal to 8.
throw new ImageFormatException($"Invalid window size for ZLIB header: cinfo={cinfo}");
}
else
{
return false;
}

return false;
}
}
else if (isCriticalChunk)
{
throw new ImageFormatException($"Bad method for ZLIB header: cmf={cmf}");
}
else
{
if (isCriticalChunk)
{
throw new ImageFormatException($"Bad method for ZLIB header: cmf={cmf}");
}
else
{
return false;
}
return false;
}

// The preset dictionary.
Expand All @@ -269,7 +260,11 @@ private bool InitializeInflateStream(bool isCriticalChunk)
{
// We don't need this for inflate so simply skip by the next four bytes.
// https://tools.ietf.org/html/rfc1950#page-6
this.innerStream.Read(ChecksumBuffer, 0, 4);
if (this.innerStream.Read(ChecksumBuffer, 0, 4) != 4)
{
return false;
}

this.currentDataRemaining -= 4;
}

Expand Down
16 changes: 13 additions & 3 deletions src/ImageSharp/Formats/Tiff/Ifd/DirectoryReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public DirectoryReader(Stream stream, MemoryAllocator allocator)
public IEnumerable<ExifProfile> Read()
{
this.ByteOrder = ReadByteOrder(this.stream);
var headerReader = new HeaderReader(this.stream, this.ByteOrder);
HeaderReader headerReader = new(this.stream, this.ByteOrder);
headerReader.ReadFileHeader();

this.nextIfdOffset = headerReader.FirstIfdOffset;
Expand All @@ -55,7 +55,12 @@ public IEnumerable<ExifProfile> Read()
private static ByteOrder ReadByteOrder(Stream stream)
{
Span<byte> headerBytes = stackalloc byte[2];
stream.Read(headerBytes);

if (stream.Read(headerBytes) != 2)
{
throw TiffThrowHelper.ThrowInvalidHeader();
}

if (headerBytes[0] == TiffConstants.ByteOrderLittleEndian && headerBytes[1] == TiffConstants.ByteOrderLittleEndian)
{
return ByteOrder.LittleEndian;
Expand All @@ -74,7 +79,7 @@ private IEnumerable<ExifProfile> ReadIfds(bool isBigTiff)
var readers = new List<EntryReader>();
while (this.nextIfdOffset != 0 && this.nextIfdOffset < (ulong)this.stream.Length)
{
var reader = new EntryReader(this.stream, this.ByteOrder, this.allocator);
EntryReader reader = new(this.stream, this.ByteOrder, this.allocator);
reader.ReadTags(isBigTiff, this.nextIfdOffset);

if (reader.BigValues.Count > 0)
Expand All @@ -88,6 +93,11 @@ private IEnumerable<ExifProfile> ReadIfds(bool isBigTiff)
}
}

if (this.nextIfdOffset >= reader.NextIfdOffset && reader.NextIfdOffset != 0)
{
TiffThrowHelper.ThrowImageFormatException("TIFF image contains circular directory offsets");
}

this.nextIfdOffset = reader.NextIfdOffset;
readers.Add(reader);

Expand Down
12 changes: 12 additions & 0 deletions tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@

[Theory]
[WithFile(Rgba6BitAssociatedAlpha, PixelTypes.Rgba32)]
public void TiffDecoder_CanDecode_24Bit_WithAssociatedAlpha<TPixel>(TestImageProvider<TPixel> provider)

Check warning on line 277 in tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, net5.0, -x64, false)

Code should not contain trailing whitespace

Check warning on line 277 in tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, net5.0, -x64, false)

Code should not contain trailing whitespace

Check warning on line 277 in tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, netcoreapp3.1, -x64, false)

Code should not contain trailing whitespace

Check warning on line 277 in tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, netcoreapp3.1, -x64, false)

Code should not contain trailing whitespace

Check warning on line 277 in tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs

View workflow job for this annotation

GitHub Actions / Build (macos-latest, netcoreapp3.1, -x64, false)

Check warning on line 277 in tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs

View workflow job for this annotation

GitHub Actions / Build (macos-latest, net6.0, 6.0.x, true, -x64, false)

Check warning on line 277 in tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, net6.0, 6.0.x, true, -x64, false)

Code should not contain trailing whitespace

Check warning on line 277 in tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, net6.0, 6.0.x, true, -x64, false)

Code should not contain trailing whitespace

Check warning on line 277 in tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs

View workflow job for this annotation

GitHub Actions / Build (macos-latest, net5.0, -x64, false)

Check warning on line 277 in tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, net6.0, 6.0.x, true, -x64, false)

Code should not contain trailing whitespace

Check warning on line 277 in tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, net472, -x64, false)

Code should not contain trailing whitespace

Check warning on line 277 in tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, net472, -x64, false)

Code should not contain trailing whitespace

Check warning on line 277 in tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, net472, -x86, false)

Code should not contain trailing whitespace

Check warning on line 277 in tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, net472, -x86, false)

Code should not contain trailing whitespace

Check warning on line 277 in tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, netcoreapp3.1, -x64, false)

Code should not contain trailing whitespace

Check warning on line 277 in tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, netcoreapp3.1, -x64, false)

Code should not contain trailing whitespace

Check warning on line 277 in tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, net5.0, -x64, false)

Code should not contain trailing whitespace

Check warning on line 277 in tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, net5.0, -x64, false)

Code should not contain trailing whitespace

Check warning on line 277 in tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, netcoreapp2.1, -x64, false)

Code should not contain trailing whitespace

Check warning on line 277 in tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, netcoreapp2.1, -x64, false)

Code should not contain trailing whitespace
where TPixel : unmanaged, IPixel<TPixel>
{
if (TestEnvironment.IsMacOS)
Expand Down Expand Up @@ -668,6 +668,18 @@
}
});

[Theory]
[WithFile(JpegCompressedGray0000539558, PixelTypes.Rgba32)]
public void TiffDecoder_ThrowsException_WithCircular_IFD_Offsets<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
=> Assert.Throws<ImageFormatException>(
() =>
{
using (provider.GetImage(TiffDecoder))
{
}
});

[Theory]
[WithFileCollection(nameof(MultiframeTestImages), PixelTypes.Rgba32)]
public void DecodeMultiframe<TPixel>(TestImageProvider<TPixel> provider)
Expand Down
1 change: 1 addition & 0 deletions tests/ImageSharp.Tests/TestImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,7 @@ public static class Tiff
public const string Issues1716Rgb161616BitLittleEndian = "Tiff/Issues/Issue1716.tiff";
public const string Issues1891 = "Tiff/Issues/Issue1891.tiff";
public const string Issues2123 = "Tiff/Issues/Issue2123.tiff";
public const string JpegCompressedGray0000539558 = "Tiff/Issues/JpegCompressedGray-0000539558.tiff";

public const string SmallRgbDeflate = "Tiff/rgb_small_deflate.tiff";
public const string SmallRgbLzw = "Tiff/rgb_small_lzw.tiff";
Expand Down
Git LFS file not shown