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: Add tests for buffer corruption after PEM write #3945

Merged

Conversation

paul-elliott-arm
Copy link
Member

Description

In order to remove some big (4k) buffers from being created on the stack, the output buffer was re-used - in this case the raw der data is written to the buffer prior to being base64 encoded into an allocated buffer, and then overwritten with the pem data. However, even though the pem data is zero terminated, usually der data will remain in the buffer after the terminator.

Should this buffer get passed into mbedtls_x509_crt_parse() then it will likely fail to parse, as it decides whether or not a buffer is pem by checking the last byte of the buffer for being zero - if it isn't zero, it will attempt to parse as der, which will obviously fail.

Please note that although this bug does not affect this branch, I wanted to add the tests anyway, to ensure this regression did not happen again. In order to get these tests to pass I would have had to add a memset to clear the buffers before two of the tests, so it seemed more clean just to backport the fix as well (i.e. zero the buffer after pem write)

This is a backport of #3898

Status

READY

Steps to test or reproduce

See #3682

@paul-elliott-arm paul-elliott-arm changed the title Add tests for buffer corruption after PEM write Backport 2.7: Add tests for buffer corruption after PEM write Dec 7, 2020
@gabor-mezei-arm gabor-mezei-arm self-requested a review December 7, 2020 12:23
@gilles-peskine-arm gilles-peskine-arm added component-crypto Crypto primitives and low-level interfaces enhancement needs-review Every commit must be reviewed by at least two team members, labels Dec 7, 2020
Zero remaining bytes in buffer after writing PEM data and add checks to
ensure that this is the case.

Signed-off-by: Paul Elliott <[email protected]>
Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm left a comment

Choose a reason for hiding this comment

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

There are cosmetic differences with the 2.16 backport in the tests. In general it's better to avoid those, because they make backporting further patches more difficult, but we're unlikely to patch these tests any more until 2.7 goes out of support, so never mind.

@gilles-peskine-arm gilles-peskine-arm added needs-ci Needs to pass CI tests 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 Dec 8, 2020
@gilles-peskine-arm gilles-peskine-arm merged commit dcba585 into Mbed-TLS:mbedtls-2.7 Dec 8, 2020
@paul-elliott-arm paul-elliott-arm deleted the fix_pem_write_2_7 branch March 5, 2021 19:52
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 component-crypto Crypto primitives and low-level interfaces enhancement needs-ci Needs to pass CI tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants