Skip to content
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

Open
wants to merge 2 commits into
base: release/8.0-staging
Choose a base branch
from

Conversation

github-actions[bot]
Copy link
Contributor

@github-actions github-actions bot commented Nov 6, 2024

Backport of #109586 to release/8.0-staging

/cc @bartonjs

Customer Impact

  • Customer reported
  • Found internally

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.

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.
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-formats-asn1, @bartonjs, @vcsjones
See info in area-owners.md if you want to be subscribed.

@bartonjs bartonjs requested a review from carlossanlop November 7, 2024 18:14
@bartonjs bartonjs changed the title [release/8.0-staging] Fix erroneous success in AsnDecoder.ReadEncodedValue [release/8.0-staging] Fix erroneous success in AsnDecoder.ReadSequence Nov 7, 2024
<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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@bartonjs bartonjs added Servicing-consider Issue for next servicing release review Servicing-approved Approved for servicing release and removed Servicing-consider Issue for next servicing release review labels Nov 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants