Skip to content

Commit 92b12d7

Browse files
Bound BigTIFF directory entry counts
1 parent 73d8e02 commit 92b12d7

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

‎src/ImageSharp/Metadata/Profiles/Exif/ExifReader.cs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,13 @@ protected void ReadValues64(List<IExifValue> values, ulong offset)
224224
this.Seek(offset);
225225
ulong count = this.ReadUInt64();
226226

227+
// Each entry occupies 20 bytes and the directory ends with an 8-byte next-IFD offset.
228+
long remainingDirectoryBytes = this.data.Length - this.data.Position;
229+
if (remainingDirectoryBytes < 8 || count > (ulong)((remainingDirectoryBytes - 8) / 20))
230+
{
231+
throw new InvalidImageContentException("The BigTIFF directory entry count exceeds the available data.");
232+
}
233+
227234
Span<byte> offsetBuffer = stackalloc byte[8];
228235
for (ulong i = 0; i < count; i++)
229236
{

‎tests/ImageSharp.Tests/Formats/Tiff/BigTiffDecoderTests.cs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,17 @@ public void TiffDecoder_SubIfd8<TPixel>(TestImageProvider<TPixel> provider)
115115
Assert.Equal(1, meta.Values.Count(v => (ushort)v.Tag == (ushort)ExifTagValue.StripOffsets));
116116
Assert.Equal(1, meta.Values.Count(v => (ushort)v.Tag == (ushort)ExifTagValue.StripByteCounts));
117117
}
118+
119+
[Fact]
120+
public void TiffDecoder_DirectoryEntryCountExceedsAvailableData_Throws()
121+
{
122+
byte[] data =
123+
[
124+
0x49, 0x49, 0x2B, 0x00, 0x08, 0x00, 0x00, 0x00,
125+
0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
126+
0x00, 0xF2, 0x05, 0x2A, 0x01, 0x00, 0x00, 0x00,
127+
];
128+
129+
Assert.Throws<InvalidImageContentException>(() => Image.Load(data));
130+
}
118131
}

0 commit comments

Comments
 (0)