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

Backport 2.x: Fix mbedtls_debug_print_mpi crash on 0 #4623

Merged

Conversation

gilles-peskine-arm
Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm commented Jun 7, 2021

Fix #4608. A direct backport of the relevant part of #4604.

Other parts of #4604 that needed backporting are in #4621.

Question about the 2.16 backport: should I backport this rewrite of `mbedtls_debug_print_mpi (which I find a lot more readable, and reduces the code size, but means more risk of an accidental behavior change), or should I find a way to fix the bug with as little deviation from the original code as possible?

Reorder test cases and make their descriptions more explicit. No
change in test data.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
There was already a test case for 0 but with a non-empty representation
(X->n == 1). Add a test case with X->n == 0 (freshly initialized mpi).

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Rewrite mbedtls_debug_print_mpi to be simpler and smaller. Leverage
mbedtls_mpi_bitlen() instead of manually looking for the leading
zeros.

Fix Mbed-TLS#4608: the old code made an invalid memory dereference when
X->n==0 (freshly initialized bignum with the value 0).

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
MPI sizes do fit in int. Let MSVC know this conversion is deliberate.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
@gilles-peskine-arm gilles-peskine-arm added bug needs-review Every commit must be reviewed by at least two team members, component-tls needs-reviewer This PR needs someone to pick it up for review labels Jun 7, 2021
@ronald-cron-arm
Copy link
Contributor

I am not sure to follow. Both this PR and #4621 target development_2.x. Is that as intended ?

@gilles-peskine-arm
Copy link
Contributor Author

@ronald-cron-arm This PR and #4621 are both partial backports of #4604. I discovered two independent issues (one bug in debug, and some documentation issues) while working on #4604, and I made separate PR to backport them. I had made some mistakes in the links in the description of this PR which I've now fixed.

Copy link
Contributor

@mpg mpg left a comment

Choose a reason for hiding this comment

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

LGTM

@mpg
Copy link
Contributor

mpg commented Jun 21, 2021

Question about the 2.16 backport: should I backport this rewrite of `mbedtls_debug_print_mpi (which I find a lot more readable, and reduces the code size, but means more risk of an accidental behavior change), or should I find a way to fix the bug with as little deviation from the original code as possible?

I think backporting the rewrite is fine. Since the new code is simpler and cleaner, the risk of introducing unwanted changed seems relatively low, and considering it's in the debug module, I think it's acceptable for 2.16.

I'll note, though, that it's not clear to me that this bug could be triggered in 2.16: the ChangeLog says this can happen when using a Montgomery curve for key exchange, which 2.16 does not support. But we don't know if it could happen in other circumstances as well, so I think it's safer to backport the fix.

@gilles-peskine-arm
Copy link
Contributor Author

it's not clear to me that this bug could be triggered in 2.16: the ChangeLog says this can happen when using a Montgomery curve for key exchange, which 2.16 does not support.

Good point, I'll remove this sentence form the changelog. I haven't investigated what other ways there could be to trigger the bug in 2.16. Given that the MPI read functions don't create 0-limb MPIs, it's harder to trigger in 2.16, but there may well be debug code that prints out an MPI that remained in its initial state because it's unused with a particular combination of parameters, or because there was an error before it was filled. So I can't rule out TLS exploitability in 2.16.

@ronald-cron-arm ronald-cron-arm self-requested a review June 21, 2021 13:52
@ronald-cron-arm ronald-cron-arm removed the needs-reviewer This PR needs someone to pick it up for review label Jun 21, 2021
Copy link
Contributor

@ronald-cron-arm ronald-cron-arm left a comment

Choose a reason for hiding this comment

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

I've been through the cherry-picking of all the commits of #4604 related to mbedls_debug.c and its testing and only those and ended-up with the same tree. This is thus a fair partial backport of the work on mbedtls_debug.c in #4604. There is just the change log that needs some adjustment (as per #4623 (comment)) and then I can approve.

Copy link
Contributor

@ronald-cron-arm ronald-cron-arm left a comment

Choose a reason for hiding this comment

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

My bad, the change in the change log is necessary in 2.16 not here. Thus approving.

@ronald-cron-arm ronald-cron-arm added the approved Design and code approved - may be waiting for CI or backports label Jun 21, 2021
@mpg mpg removed the needs-review Every commit must be reviewed by at least two team members, label Jun 22, 2021
@mpg mpg merged commit 6a55de9 into Mbed-TLS:development_2.x Jun 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Design and code approved - may be waiting for CI or backports bug component-tls
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants