Skip to content

Fix int overflow in mbedtls_asn1_get_int #297

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

Conversation

gilles-peskine-arm
Copy link
Collaborator

Fix an int overflow in mbedtls_asn1_get_int() introduced by #75 (so not present in any released version).

The signed int overflow in mbedtls_asn1_get_int() happened for numbers between INT_MAX+1 and UINT_MAX (typically 0x80000000..0xffffffff). This was undefined behavior which in practice would typically have resulted in an incorrect value, but which may plausibly also have caused the postcondition (*p == initial<*p> + len) to be violated.

Also fix up the ASN.1 INTEGER parsing tests a bit.

Credit to OSS-Fuzz (private link: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=18102). To fix this in OSS-Fuzz, we'll need to update the crypto submodule in mbedtls after merging this PR.

Internal ref: IOTCRYPT-947

When the asn1parse module is enabled but the bignum module is
disabled, the asn1parse test suite did not work. Fix this.

* Fix a syntax error in get_integer() (label immediately followed by a
  closing brace).
* Fix an unused variable in get_integer().
* Fix `TEST_ASSERT( *p == q );` in nested_parse() failing because `*p`
  was not set.
* Fix nested_parse() not outputting the length of what it parsed.
mbedtls_asn1_get_int() and mbedtls_asn1_get_mpi() behave differently
on an empty INTEGER (0200). Don't change the library behavior for now
because this might break interoperability in some applications. Write
a test function that matches the library behavior.
mbedtls_asn1_get_int() and mbedtls_asn1_get_mpi() behave differently
on negative INTEGERs (0200). Don't change the library behavior for now
because this might break interoperability in some applications. Change
the test function to the library behavior.

Fix the test data with negative INTEGERs. These test cases were
previously not run (they were introduced but deliberately deactivated
in 27d806f). The test data was
actually wrong: ASN.1 uses two's complement, which has no negative 0,
and some encodings were wrong. Now the tests have correct data, and
the test code rectifies the expected data to match the library
behavior.
Test more INTEGER values, especially near the boundary of int (which
is at 2^31-1 on all our officially supported platforms).
Fix a signed int overflow in mbedtls_asn1_get_int() for numbers
between INT_MAX+1 and UINT_MAX (typically 0x80000000..0xffffffff).
This was undefined behavior which in practice would typically have
resulted in an incorrect value, but which may plausibly also have
caused the postcondition (*p == initial<*p> + len) to be violated.

Credit to OSS-Fuzz.
@gilles-peskine-arm gilles-peskine-arm added bug Something isn't working needs: review The pull request is ready for review. This generally means that it has no known issues. labels Oct 10, 2019
Copy link
Contributor

@athoelke athoelke left a comment

Choose a reason for hiding this comment

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

That works for me.

@AndrzejKurek AndrzejKurek self-requested a review October 11, 2019 08:06
@AndrzejKurek AndrzejKurek self-assigned this Oct 11, 2019
@Patater Patater requested a review from k-stachowiak October 11, 2019 08:41
INTEGER 0x123456789abcdef0
get_integer:"0208123456789abcdef0":"123456789abcdef0":0

INTEGER 0xfedcab9876543210
Copy link
Contributor

Choose a reason for hiding this comment

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

Not super important, but if you wanted decreasing digits, use 0xfedcba9876543210. (swap a and b).

@gilles-peskine-arm
Copy link
Collaborator Author

CI only failed on atecc608a-K64F jobs which is a known issue that's unrelated to this PR. Ok to merge.

@gilles-peskine-arm gilles-peskine-arm merged commit 3cdb3da into ARMmbed:development Oct 11, 2019
@gilles-peskine-arm gilles-peskine-arm removed the needs: review The pull request is ready for review. This generally means that it has no known issues. label Oct 11, 2019
gilles-peskine-arm added a commit to gilles-peskine-arm/mbed-crypto that referenced this pull request Nov 15, 2019
* ARMmbed#272: Insert doxygen comments on old algorithms so they appear in PSA documentation
* ARMmbed#285: SE driver: make persistent data work
* ARMmbed#279: Include IANA reference in the definition of ECC curves and DH groups
* ARMmbed#287: DRBG documentation improvements
* ARMmbed#297: Fix int overflow in mbedtls_asn1_get_int (Credit to OSS-Fuzz)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants