-
Notifications
You must be signed in to change notification settings - Fork 96
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
Fix int overflow in mbedtls_asn1_get_int #297
Conversation
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).
No behavior change.
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.
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.
That works for me.
INTEGER 0x123456789abcdef0 | ||
get_integer:"0208123456789abcdef0":"123456789abcdef0":0 | ||
|
||
INTEGER 0xfedcab9876543210 |
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.
Not super important, but if you wanted decreasing digits, use 0xfedcba9876543210
. (swap a
and b
).
CI only failed on atecc608a-K64F jobs which is a known issue that's unrelated to this PR. Ok to merge. |
* 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)
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 betweenINT_MAX+1
andUINT_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