-
Notifications
You must be signed in to change notification settings - Fork 5k
[release/8.0-staging] Fix erroneous success in AsnDecoder.ReadSequence #109596
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/8.0-staging] Fix erroneous success in AsnDecoder.ReadSequence #109596
Conversation
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.
Tagging subscribers to this area: @dotnet/area-system-formats-asn1, @bartonjs, @vcsjones |
<GeneratePackageOnBuild>false</GeneratePackageOnBuild> | ||
<ServicingVersion>1</ServicingVersion> | ||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> | ||
<ServicingVersion>2</ServicingVersion> | ||
<PackageDescription>Provides classes that can read and write the ASN.1 BER, CER, and DER data formats. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@bartonjs friendly reminder that today's code complete for the February 2025 release. Please merge this PR before 4pm PT, otherwise it will have to wait until next month. |
Backport of #109586 to release/8.0-staging
/cc @bartonjs
Customer Impact
Calls to AsnReader.ReadSequence, (internal) AsnValueReader.ReadSequence, or any method that indirectly calls them, will get an ArgumentOutOfRangeException if processing specific data, which stems from AsnDecoder.ReadSequence erroneously succeeding on particular malformed data.
This was originally reported as an unexpected ArgumentOutOfRangeException from a constructor of X509Certificate2, which can be reproduced on non-Windows systems when checking if the contents are PKCS#12/PFX. Particular malformed data may therefore provide unexpected exception flow to applications.
Regression
This incorrect behavior has always been present in AsnDecoder.ReadSequence, and has probably been present for reading certificates on Linux since before AsnDecoder was public API. In modern versions of .NET it appears as an OS-specific bug, so users may experience it as a "regression from Windows" or "regression from .NET Framework".
Testing
Additional tests are added in this change that will prevent a regression in this method, or related methods.
Risk
Low. Copious tests, both direct and indirect, ensure that well-formed data continues to function. The new tests introduced in this change add coverage for this particular sort of malformed data.