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

Fix EXIF overflow and Jpeg decoding #699

Merged
merged 3 commits into from
Sep 7, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,10 @@ public void ParseStream(Stream stream, bool metadataOnly = false)
this.fastACTables = new FastACTables(this.configuration.MemoryAllocator);
}

while (fileMarker.Marker != JpegConstants.Markers.EOI)
// Break only when we discover a valid EOI marker.
// https://github.com/SixLabors/ImageSharp/issues/695
while (fileMarker.Marker != JpegConstants.Markers.EOI
|| (fileMarker.Marker == JpegConstants.Markers.EOI && fileMarker.Invalid))
{
if (!fileMarker.Invalid)
{
Expand Down Expand Up @@ -914,9 +917,7 @@ private void ProcessStartOfScanMarker()
/// <param name="values">The values</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void BuildHuffmanTable(HuffmanTables tables, int index, ReadOnlySpan<byte> codeLengths, ReadOnlySpan<byte> values)
{
tables[index] = new HuffmanTable(this.configuration.MemoryAllocator, codeLengths, values);
}
=> tables[index] = new HuffmanTable(this.configuration.MemoryAllocator, codeLengths, values);

/// <summary>
/// Reads a <see cref="ushort"/> from the stream advancing it by two bytes
Expand Down
24 changes: 13 additions & 11 deletions src/ImageSharp/MetaData/Profiles/Exif/ExifReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,19 @@ public List<ExifValue> ReadValues()
}

uint ifdOffset = this.ReadUInt32();
this.AddValues(values, (int)ifdOffset);
this.AddValues(values, ifdOffset);

uint thumbnailOffset = this.ReadUInt32();
this.GetThumbnail((int)thumbnailOffset);
this.GetThumbnail(thumbnailOffset);

if (this.exifOffset != 0)
{
this.AddValues(values, (int)this.exifOffset);
this.AddValues(values, this.exifOffset);
}

if (this.gpsOffset != 0)
{
this.AddValues(values, (int)this.gpsOffset);
this.AddValues(values, this.gpsOffset);
}

return values;
Expand Down Expand Up @@ -153,9 +153,14 @@ private unsafe string ConvertToString(ReadOnlySpan<byte> buffer)
/// </summary>
/// <param name="values">The values.</param>
/// <param name="index">The index.</param>
private void AddValues(List<ExifValue> values, int index)
private void AddValues(List<ExifValue> values, uint index)
{
this.position = index;
if (index > (uint)this.exifData.Length)
{
return;
}

this.position = (int)index;
int count = this.ReadUInt16();

for (int i = 0; i < count; i++)
Expand Down Expand Up @@ -431,7 +436,7 @@ private string ReadString(int length)
return null;
}

private void GetThumbnail(int offset)
private void GetThumbnail(uint offset)
{
var values = new List<ExifValue>();
this.AddValues(values, offset);
Expand Down Expand Up @@ -515,10 +520,7 @@ private Rational ToRational(ReadOnlySpan<byte> buffer)
return new Rational(numerator, denominator, false);
}

private sbyte ConvertToSignedByte(ReadOnlySpan<byte> buffer)
{
return unchecked((sbyte)buffer[0]);
}
private sbyte ConvertToSignedByte(ReadOnlySpan<byte> buffer) => unchecked((sbyte)buffer[0]);

private int ConvertToInt32(ReadOnlySpan<byte> buffer) // SignedLong in Exif Specification
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ public partial class JpegDecoderTests
TestImages.Jpeg.Baseline.Bad.BadEOF,
TestImages.Jpeg.Issues.MultiHuffmanBaseline394,
TestImages.Jpeg.Baseline.MultiScanBaselineCMYK,
TestImages.Jpeg.Baseline.Bad.BadRST
TestImages.Jpeg.Baseline.Bad.BadRST,
TestImages.Jpeg.Issues.MultiHuffmanBaseline394,
TestImages.Jpeg.Issues.ExifDecodeOutOfRange694,
TestImages.Jpeg.Issues.InvalidEOI695,
TestImages.Jpeg.Issues.ExifResizeOutOfRange696
};

public static string[] ProgressiveTestJpegs =
Expand Down
2 changes: 2 additions & 0 deletions tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ private static bool SkipTest(ITestImageProvider provider)
TestImages.Jpeg.Issues.BadZigZagProgressive385,
TestImages.Jpeg.Issues.NoEoiProgressive517,
TestImages.Jpeg.Issues.BadRstProgressive518,
TestImages.Jpeg.Issues.InvalidEOI695,
TestImages.Jpeg.Issues.ExifResizeOutOfRange696
};

return !TestEnvironment.Is64BitProcess && largeImagesToSkipOn32Bit.Contains(provider.SourceFileOrDescription);
Expand Down
3 changes: 3 additions & 0 deletions tests/ImageSharp.Tests/TestImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ public static class Issues
public const string BadRstProgressive518 = "Jpg/issues/Issue518-Bad-RST-Progressive.jpg";
public const string InvalidCast520 = "Jpg/issues/Issue520-InvalidCast.jpg";
public const string DhtHasWrongLength624 = "Jpg/issues/Issue624-DhtHasWrongLength-Progressive-N.jpg";
public const string ExifDecodeOutOfRange694 = "Jpg/issues/Issue694-Decode-Exif-OutOfRange.jpg";
public const string InvalidEOI695 = "Jpg/issues/Issue695-Invalid-EOI.jpg";
public const string ExifResizeOutOfRange696 = "Jpg/issues/Issue696-Resize-Exif-OutOfRange.jpg";
}

public static readonly string[] All = Baseline.All.Concat(Progressive.All).ToArray();
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.