Skip to content

Commit fcee4ed

Browse files
[release/9.0-staging] Fix erroneous success in AsnDecoder.ReadSequence
When AsnDecoder.ReadEncodedValue is used on a payload where the encoded length plus the number of bytes representing the encoded length plus the number of bytes representing the tag exceeds int.MaxValue it erroneously reports success. Every known caller of this method immediately follows it with a call to a Span or Memory .Slice, which results in an ArgumentException that the requested length exceeds the number of bytes available in the buffer. Because AsnDecoder was extracted out of AsnReader, most AsnDecoder tests are done indirectly as AsnReader tests, or are done in the same test class that tests the related functionality on AsnReader. Since there's not a clear analogue for ReadEncodedValue (that does not immediately Slice), this change introduces a new subtree for AsnDecoder tests, only populating it with (Try)ReadEncodedValue tests. It also adds "large length" tests for ReadSetOf and ReadSequence, for good measure. Co-authored-by: Jeremy Barton <[email protected]>
1 parent b9f8c91 commit fcee4ed

File tree

5 files changed

+295
-1
lines changed

5 files changed

+295
-1
lines changed

src/libraries/System.Formats.Asn1/src/System/Formats/Asn1/AsnDecoder.Sequence.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public static void ReadSequence(
7575

7676
if (length.HasValue)
7777
{
78-
if (length.Value + headerLength > source.Length)
78+
if (length.Value > source.Length - headerLength)
7979
{
8080
throw GetValidityException(LengthValidity.LengthExceedsInput);
8181
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Test.Cryptography;
5+
using Xunit;
6+
7+
namespace System.Formats.Asn1.Tests.Decoder
8+
{
9+
public sealed class ReadEncodedValueTests
10+
{
11+
[Theory]
12+
[InlineData(AsnEncodingRules.BER)]
13+
[InlineData(AsnEncodingRules.CER)]
14+
[InlineData(AsnEncodingRules.DER)]
15+
public static void ReadEncodedValue_Primitive(AsnEncodingRules ruleSet)
16+
{
17+
// OCTET STRING (6 content bytes)
18+
// NULL
19+
ReadOnlySpan<byte> data =
20+
[
21+
0x04, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
22+
0x05, 0x00,
23+
];
24+
25+
ExpectSuccess(data, ruleSet, Asn1Tag.PrimitiveOctetString, 2, 6);
26+
}
27+
28+
[Theory]
29+
[InlineData(AsnEncodingRules.BER)]
30+
[InlineData(AsnEncodingRules.CER)]
31+
[InlineData(AsnEncodingRules.DER)]
32+
public static void ReadEncodedValue_Indefinite(AsnEncodingRules ruleSet)
33+
{
34+
// CONSTRUCTED OCTET STRING (indefinite)
35+
// OCTET STRING (1 byte)
36+
// OCTET STRING (5 bytes)
37+
// END OF CONTENTS
38+
// NULL
39+
ReadOnlySpan<byte> data =
40+
[
41+
0x24, 0x80,
42+
0x04, 0x01, 0x01,
43+
0x04, 0x05, 0x02, 0x03, 0x04, 0x05, 0x06,
44+
0x00, 0x00,
45+
0x05, 0x00,
46+
];
47+
48+
// BER: Indefinite length encoding is OK, no requirements on the contents.
49+
// CER: Indefinite length encoding is required for CONSTRUCTED, the contents are invalid for OCTET STRING,
50+
// but (Try)ReadEncodedValue doesn't pay attention to that.
51+
// DER: Indefinite length encoding is never permitted.
52+
53+
if (ruleSet == AsnEncodingRules.DER)
54+
{
55+
ExpectFailure(data, ruleSet);
56+
}
57+
else
58+
{
59+
ExpectSuccess(data, ruleSet, Asn1Tag.ConstructedOctetString, 2, 10, indefiniteLength: true);
60+
}
61+
}
62+
63+
[Theory]
64+
[InlineData(AsnEncodingRules.BER)]
65+
[InlineData(AsnEncodingRules.CER)]
66+
[InlineData(AsnEncodingRules.DER)]
67+
public static void ReadEncodedValue_DefiniteConstructed(AsnEncodingRules ruleSet)
68+
{
69+
// CONSTRUCTED OCTET STRING (11 bytes)
70+
// OCTET STRING (1 byte)
71+
// OCTET STRING (5 bytes)
72+
// NULL
73+
ReadOnlySpan<byte> data =
74+
[
75+
0x24, 0x0A,
76+
0x04, 0x01, 0x01,
77+
0x04, 0x05, 0x02, 0x03, 0x04, 0x05, 0x06,
78+
0x05, 0x00,
79+
];
80+
81+
// BER: Indefinite length encoding is OK, no requirements on the contents.
82+
// CER: Indefinite length encoding is required for CONSTRUCTED, so fail.
83+
// DER: CONSTRUCTED OCTET STRING is not permitted, but ReadEncodedValue doesn't check for that,
84+
// since the length is in minimal representation, the read is successful
85+
86+
if (ruleSet == AsnEncodingRules.CER)
87+
{
88+
ExpectFailure(data, ruleSet);
89+
}
90+
else
91+
{
92+
ExpectSuccess(data, ruleSet, Asn1Tag.ConstructedOctetString, 2, 10);
93+
}
94+
}
95+
96+
[Theory]
97+
[InlineData(AsnEncodingRules.BER)]
98+
[InlineData(AsnEncodingRules.CER)]
99+
[InlineData(AsnEncodingRules.DER)]
100+
public static void ReadEncodedValue_OutOfBoundsLength(AsnEncodingRules ruleSet)
101+
{
102+
// SEQUENCE (3 bytes), but only one byte remains.
103+
ReadOnlySpan<byte> data = [0x30, 0x03, 0x00];
104+
105+
ExpectFailure(data, ruleSet);
106+
}
107+
108+
[Theory]
109+
[InlineData(AsnEncodingRules.BER)]
110+
[InlineData(AsnEncodingRules.CER)]
111+
[InlineData(AsnEncodingRules.DER)]
112+
public static void ReadEncodedValue_LargeOutOfBoundsLength(AsnEncodingRules ruleSet)
113+
{
114+
// SEQUENCE (int.MaxValue bytes), but no bytes remain.
115+
ReadOnlySpan<byte> data = [0x30, 0x84, 0x7F, 0xFF, 0xFF, 0xFF];
116+
117+
ExpectFailure(data, ruleSet);
118+
}
119+
120+
[Theory]
121+
[InlineData(AsnEncodingRules.BER)]
122+
[InlineData(AsnEncodingRules.CER)]
123+
[InlineData(AsnEncodingRules.DER)]
124+
public static void ReadEncodedValue_ExtremelyLargeLength(AsnEncodingRules ruleSet)
125+
{
126+
if (!Environment.Is64BitProcess)
127+
{
128+
return;
129+
}
130+
131+
// OCTET STRING ((int.MaxValue - 6) bytes), span will be inflated to make it look valid.
132+
byte[] data = "04847FFFFFF9".HexToByteArray();
133+
134+
unsafe
135+
{
136+
fixed (byte* ptr = data)
137+
{
138+
// Verify that the length can be interpreted this large, but that it doesn't read that far.
139+
ReadOnlySpan<byte> span = new ReadOnlySpan<byte>(ptr, int.MaxValue);
140+
ExpectSuccess(span, ruleSet, Asn1Tag.PrimitiveOctetString, 6, int.MaxValue - 6);
141+
}
142+
}
143+
}
144+
145+
private static void ExpectSuccess(
146+
ReadOnlySpan<byte> data,
147+
AsnEncodingRules ruleSet,
148+
Asn1Tag expectedTag,
149+
int expectedContentOffset,
150+
int expectedContentLength,
151+
bool indefiniteLength = false)
152+
{
153+
Asn1Tag tag;
154+
int contentOffset;
155+
int contentLength;
156+
int bytesConsumed;
157+
158+
bool read = AsnDecoder.TryReadEncodedValue(
159+
data,
160+
ruleSet,
161+
out tag,
162+
out contentOffset,
163+
out contentLength,
164+
out bytesConsumed);
165+
166+
Assert.True(read, "AsnDecoder.TryReadEncodedValue unexpectedly returned false");
167+
Assert.Equal(expectedTag, tag);
168+
Assert.Equal(expectedContentOffset, contentOffset);
169+
Assert.Equal(expectedContentLength, contentLength);
170+
171+
int expectedBytesConsumed = expectedContentOffset + expectedContentLength + (indefiniteLength ? 2 : 0);
172+
Assert.Equal(expectedBytesConsumed, bytesConsumed);
173+
174+
contentOffset = contentLength = bytesConsumed = default;
175+
176+
tag = AsnDecoder.ReadEncodedValue(
177+
data,
178+
ruleSet,
179+
out contentOffset,
180+
out contentLength,
181+
out bytesConsumed);
182+
183+
Assert.Equal(expectedTag, tag);
184+
Assert.Equal(expectedContentOffset, contentOffset);
185+
Assert.Equal(expectedContentLength, contentLength);
186+
Assert.Equal(expectedBytesConsumed, bytesConsumed);
187+
}
188+
189+
private static void ExpectFailure(ReadOnlySpan<byte> data, AsnEncodingRules ruleSet)
190+
{
191+
Asn1Tag tag;
192+
int contentOffset;
193+
int contentLength;
194+
int bytesConsumed;
195+
196+
bool read = AsnDecoder.TryReadEncodedValue(
197+
data,
198+
ruleSet,
199+
out tag,
200+
out contentOffset,
201+
out contentLength,
202+
out bytesConsumed);
203+
204+
Assert.False(read, "AsnDecoder.TryReadEncodedValue unexpectedly returned true");
205+
Assert.Equal(default, tag);
206+
Assert.Equal(default, contentOffset);
207+
Assert.Equal(default, contentLength);
208+
Assert.Equal(default, bytesConsumed);
209+
210+
int seed = Environment.CurrentManagedThreadId;
211+
Asn1Tag seedTag = new Asn1Tag(TagClass.Private, seed, (seed & 1) == 0);
212+
tag = seedTag;
213+
contentOffset = contentLength = bytesConsumed = seed;
214+
215+
try
216+
{
217+
tag = AsnDecoder.ReadEncodedValue(
218+
data,
219+
ruleSet,
220+
out contentOffset,
221+
out contentLength,
222+
out bytesConsumed);
223+
224+
Assert.Fail("ReadEncodedValue should have thrown AsnContentException");
225+
}
226+
catch (AsnContentException e)
227+
{
228+
Assert.IsType<AsnContentException>(e);
229+
}
230+
231+
Assert.Equal(seedTag, tag);
232+
Assert.Equal(seed, contentOffset);
233+
Assert.Equal(seed, contentLength);
234+
Assert.Equal(seed, bytesConsumed);
235+
}
236+
}
237+
}

src/libraries/System.Formats.Asn1/tests/Reader/ReadSequence.cs

+28
Original file line numberDiff line numberDiff line change
@@ -405,5 +405,33 @@ public static void ReadSequenceOf_PreservesOptions(AsnEncodingRules ruleSet)
405405
outer.ThrowIfNotEmpty();
406406
initial.ThrowIfNotEmpty();
407407
}
408+
409+
[Theory]
410+
[InlineData(AsnEncodingRules.BER)]
411+
[InlineData(AsnEncodingRules.CER)]
412+
[InlineData(AsnEncodingRules.DER)]
413+
public static void ExtremelyLargeContentLength(AsnEncodingRules ruleSet)
414+
{
415+
int start = Environment.CurrentManagedThreadId;
416+
int contentOffset = start;
417+
int contentLength = start;
418+
int bytesConsumed = start;
419+
420+
ReadOnlySpan<byte> input = [0x30, 0x84, 0x7F, 0xFF, 0xFF, 0xFF, 0x00, 0x00];
421+
422+
try
423+
{
424+
AsnDecoder.ReadSequence(input, ruleSet, out contentOffset, out contentLength, out bytesConsumed);
425+
Assert.Fail("ReadSequence should have thrown AsnContentException");
426+
}
427+
catch (AsnContentException e)
428+
{
429+
Assert.IsType<AsnContentException>(e);
430+
}
431+
432+
Assert.Equal(start, contentOffset);
433+
Assert.Equal(start, contentLength);
434+
Assert.Equal(start, bytesConsumed);
435+
}
408436
}
409437
}

src/libraries/System.Formats.Asn1/tests/Reader/ReadSetOf.cs

+28
Original file line numberDiff line numberDiff line change
@@ -390,5 +390,33 @@ public static void ReadSetOf_PreservesOptions(AsnEncodingRules ruleSet)
390390
outer.ThrowIfNotEmpty();
391391
initial.ThrowIfNotEmpty();
392392
}
393+
394+
[Theory]
395+
[InlineData(AsnEncodingRules.BER)]
396+
[InlineData(AsnEncodingRules.CER)]
397+
[InlineData(AsnEncodingRules.DER)]
398+
public static void ExtremelyLargeContentLength(AsnEncodingRules ruleSet)
399+
{
400+
int start = Environment.CurrentManagedThreadId;
401+
int contentOffset = start;
402+
int contentLength = start;
403+
int bytesConsumed = start;
404+
405+
ReadOnlySpan<byte> input = [0x31, 0x84, 0x7F, 0xFF, 0xFF, 0xFF, 0x00, 0x00];
406+
407+
try
408+
{
409+
AsnDecoder.ReadSetOf(input, ruleSet, out contentOffset, out contentLength, out bytesConsumed);
410+
Assert.Fail("ReadSetOf should have thrown AsnContentException");
411+
}
412+
catch (AsnContentException e)
413+
{
414+
Assert.IsType<AsnContentException>(e);
415+
}
416+
417+
Assert.Equal(start, contentOffset);
418+
Assert.Equal(start, contentLength);
419+
Assert.Equal(start, bytesConsumed);
420+
}
393421
}
394422
}

src/libraries/System.Formats.Asn1/tests/System.Formats.Asn1.Tests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
</PropertyGroup>
66
<ItemGroup>
77
<Compile Include="Asn1TagTests.cs" />
8+
<Compile Include="Decoder\ReadEncodedValueTests.cs" />
89
<Compile Include="Reader\ComprehensiveReadTests.cs" />
910
<Compile Include="Reader\OverlappedReads.cs" />
1011
<Compile Include="Reader\ParseTag.cs" />

0 commit comments

Comments
 (0)