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.16: Remove Extraneous bytes from buffer post pem write #3935

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.

This just ensures the buffer is cleaned to avoid the regression, there may be future work around this subject to make the area safer and tests to ensure this doesn't happen again.

This is a backport of #3898

Status

READY

Steps to test or reproduce

See #3682

@paul-elliott-arm paul-elliott-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-x509 needs-ci Needs to pass CI tests needs-reviewer This PR needs someone to pick it up for review labels Dec 3, 2020
@paul-elliott-arm paul-elliott-arm self-assigned this Dec 3, 2020
@gilles-peskine-arm gilles-peskine-arm added needs-work and removed needs-backports Backports are missing or are pending review and approval. labels Dec 4, 2020
In order to remove large buffers from the stack, the der data is written
into the same buffer that the pem is eventually written into, however
although the pem data is zero terminated, there is now data left in the
buffer after the zero termination, which can cause
mbedtls_x509_crt_parse to fail to parse the same buffer if passed back
in. Patches also applied to mbedtls_pk_write_pubkey_pem, and
mbedtls_pk_write_key_pem, which use similar methods of writing der data
to the same buffer, and tests modified to hopefully catch any future
regression on this.

Signed-off-by: Paul Elliott <[email protected]>
@gilles-peskine-arm gilles-peskine-arm added approved Design and code approved - may be waiting for CI or backports and removed needs-ci Needs to pass CI tests 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 9e8acb6 into Mbed-TLS:mbedtls-2.16 Dec 8, 2020
@paul-elliott-arm paul-elliott-arm deleted the fix_pem_write_2_16 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 bug component-x509
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants