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.7: Fix buffer overflow in mbedtls_mpi_sub_abs negative case #4077

Conversation

gilles-peskine-arm
Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm commented Jan 27, 2021

Fix a buffer overflow in mbedtls_mpi_sub_abs() when calculating |A| - |B| where |B| is larger than |A| and has more limbs (so the function should return MBEDTLS_ERR_MPI_NEGATIVE_VALUE).

This is a regression introduced in Mbed TLS 2.7.18. Fix #4042.

Backport of #4096

@gilles-peskine-arm gilles-peskine-arm added bug needs-review Every commit must be reviewed by at least two team members, needs-backports Backports are missing or are pending review and approval. component-crypto Crypto primitives and low-level interfaces needs-reviewer This PR needs someone to pick it up for review labels Jan 27, 2021
yanesca
yanesca previously approved these changes Jan 28, 2021
Copy link
Contributor

@yanesca yanesca left a comment

Choose a reason for hiding this comment

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

Looks good to me.

mbedtls_mpi_sub_abs:10:"5":10:"-7":10:"0":MBEDTLS_ERR_MPI_NEGATIVE_VALUE

Base test mbedtls_mpi_sub_abs #1 (|B| >> |A|)
Copy link
Contributor

Choose a reason for hiding this comment

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

Like above, I find spelling it out more helpful (eg. "Test with much larger second input" or "Second input is at least a limb longer").

Copy link
Contributor

Choose a reason for hiding this comment

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

I think having "limb" in the test description would be useful, as this is really what the test is about. For example (|B| > |A| with more limbs), or something like that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed the description to (|B| >> |A| with more limbs). I kept >> because it's relevant that B has more limbs not just because of leading zeros.

mpg
mpg previously approved these changes Feb 1, 2021
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.

Looks good to me - I think the test description would be improved but is good enough as it is.

In terms of commit structure, I like that you introduce the test first, which makes it easier for reviewers to check that it's failing before the fix. However, for the benefit of people who might be bisecting this in the future, in those cases it would probably be better to mention explicitly in the commit message that the tests are expected to fail. (Not requesting a history rewrite here unless you feel like it, more of a general note for the future.)

@mpg mpg removed the needs-reviewer This PR needs someone to pick it up for review label Feb 1, 2021
Add test cases for mbedtls_mpi_sub_abs() where the second operand has
more limbs than the first operand (which, if the extra limbs are not
all zero, implies that the function returns
MBEDTLS_ERR_MPI_NEGATIVE_VALUE).

This exposes a buffer overflow (reported in Mbed-TLS#4042).

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Fix a buffer overflow in mbedtls_mpi_sub_abs() when calculating
|A| - |B| where |B| is larger than |A| and has more limbs (so the
function should return MBEDTLS_ERR_MPI_NEGATIVE_VALUE).

Fix Mbed-TLS#4042

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
@gilles-peskine-arm gilles-peskine-arm dismissed stale reviews from mpg and yanesca via 54c3046 February 1, 2021 12:40
@gilles-peskine-arm gilles-peskine-arm force-pushed the mpi_sub_abs-buffer_overflow-2.7 branch from ccbb758 to 54c3046 Compare February 1, 2021 12:40
@gilles-peskine-arm
Copy link
Contributor Author

I rewrote the history to amend the commit message and the test descriptions in the first commit.

I'm not convinced that a commit that introduces failing tests should explain how and why the tests might fail. A commit that adds new tests expands the test coverage, and if that reveals a bug (known or not to the developer), it's up to subsequent commits to fix that bug. But ok, I added a reference to the bug report in the commit that introduces the tests.

Copy link
Contributor

@yanesca yanesca left a comment

Choose a reason for hiding this comment

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

Looks good to me.

(I agree with @mpg that we should think about people bisecting in the future, I heard that future maintainers will be psychopaths with a time machine and guns 😉 )

@gilles-peskine-arm gilles-peskine-arm removed the needs-backports Backports are missing or are pending review and approval. label Feb 1, 2021
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.

Thanks for addressing my feedback. I'm happy with the result.

@mpg
Copy link
Contributor

mpg commented Feb 2, 2021

I'm not convinced that a commit that introduces failing tests should explain how and why the tests might fail. A commit that adds new tests expands the test coverage, and if that reveals a bug (known or not to the developer), it's up to subsequent commits to fix that bug. But ok, I added a reference to the bug report in the commit that introduces the tests.

In that case, the fact that the test failed was not only known but even expected: actually, that's one of the things I checked as a reviewer (and I suppose that's one of the things you checked after writing the tests). So I think recording that in the commit message is simply a way to record information that we had when adding the test and making sure that information remains conveniently available in the future.

@mpg mpg added approved Design and code approved - may be waiting for CI or backports and removed needs-review Every commit must be reviewed by at least two team members, labels Feb 2, 2021
@yanesca yanesca merged commit ec1909d into Mbed-TLS:mbedtls-2.7 Feb 2, 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-crypto Crypto primitives and low-level interfaces
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants