-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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/9.0-staging] Fix erroneous success in AsnDecoder.ReadSequence #109595
[release/9.0-staging] Fix erroneous success in AsnDecoder.ReadSequence #109595
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 |
Hold on for this #109316 Please remove the packaging changes. We won't be needing them in 9.0 anymore. |
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 for packaging (no longer required in 9.0).
@jeffhandley @bartonjs can you get a code review from an area owner and merge it today by 4pm PT to ensure it gets included in the Feb 2025 release?
Backport of #109586 to release/9.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.