Skip to content

Commit 73d8e02

Browse files
Reject undersized ICC tag entries
1 parent 8de892a commit 73d8e02

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

‎src/ImageSharp/Metadata/Profiles/ICC/IccReader.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,15 @@ private static IccTagTableEntry[] ReadTagTable(IccDataReader reader)
119119
}
120120

121121
List<IccTagTableEntry> table = new((int)tagCount);
122+
uint dataLength = (uint)reader.DataLength;
122123
for (int i = 0; i < tagCount; i++)
123124
{
124125
uint tagSignature = reader.ReadUInt32();
125126
uint tagOffset = reader.ReadUInt32();
126127
uint tagSize = reader.ReadUInt32();
127128

128129
// Exclude entries that have nonsense values and could cause exceptions further on
129-
if (tagOffset < reader.DataLength && tagSize < reader.DataLength - 128)
130+
if (tagSize >= 8 && tagOffset <= dataLength && tagSize <= dataLength - tagOffset)
130131
{
131132
table.Add(new IccTagTableEntry((IccProfileTag)tagSignature, tagOffset, tagSize));
132133
}

‎tests/ImageSharp.Tests/Metadata/Profiles/ICC/IccReaderTests.cs‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4+
using System.Buffers.Binary;
45
using SixLabors.ImageSharp.Metadata.Profiles.Icc;
56
using SixLabors.ImageSharp.PixelFormats;
67
using SixLabors.ImageSharp.Tests.TestDataIcc;
@@ -59,4 +60,26 @@ public void ReadProfile_NoEntries()
5960
Assert.Equal(header.Size, expected.Size);
6061
Assert.Equal(header.Version, expected.Version);
6162
}
63+
64+
[Fact]
65+
public void ReadProfile_WithUndersizedArrayTags_IgnoresTags()
66+
{
67+
const int tagCount = 100;
68+
const int dataOffset = 132 + (tagCount * 12);
69+
byte[] data = new byte[dataOffset + 16];
70+
BinaryPrimitives.WriteUInt32BigEndian(data.AsSpan(128), tagCount);
71+
72+
for (int i = 0; i < tagCount; i++)
73+
{
74+
Span<byte> entry = data.AsSpan(132 + (i * 12), 12);
75+
BinaryPrimitives.WriteUInt32BigEndian(entry, 0x73663332);
76+
BinaryPrimitives.WriteUInt32BigEndian(entry[4..], dataOffset);
77+
BinaryPrimitives.WriteUInt32BigEndian(entry[8..], 0);
78+
}
79+
80+
BinaryPrimitives.WriteUInt32BigEndian(data.AsSpan(dataOffset), 0x73663332);
81+
IccProfile profile = new(data);
82+
83+
Assert.Empty(profile.Entries);
84+
}
6285
}

0 commit comments

Comments
 (0)