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

Remove Extraneous bytes from buffer post pem write #3898

Merged

Conversation

paul-elliott-arm
Copy link
Member

@paul-elliott-arm paul-elliott-arm commented Nov 19, 2020

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 closes #3682

Status

READY

Requires Backporting

YES
2.16
2.7

(Bug did not affect the 2.7 branch, but wanted to add the tests)

Migrations

NO

Todos

  • Changelog updated
  • Backported

Steps to test or reproduce

See #3682

@paul-elliott-arm paul-elliott-arm self-assigned this Nov 19, 2020
@paul-elliott-arm paul-elliott-arm added bug component-x509 needs-ci Needs to pass CI tests needs-review Every commit must be reviewed by at least two team members, needs-reviewer This PR needs someone to pick it up for review labels Nov 19, 2020
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.

I agree with this approach. It needs:

  • Same thing with CSR.
  • In unit tests, after calling mbedtls_x509write_crt_pem, check that the trail of the buffer is zeroed out. Make sure that there is at least one unit test where the output buffer is larger than the output data.
  • Unit tests for the other functions with the same pattern (mbedtls_pk_write_pubkey_pem, mbedtls_pk_write_key_pem, mbedtls_x509write_csr_pem).
  • Changelog entry.

@@ -0,0 +1,8 @@
Bugfix
* As an optimisation to stop large buffers being written to the stack the
raw der data was instead written to the same buffer that the PEM data
Copy link
Contributor

Choose a reason for hiding this comment

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

DER also needs to be in all caps.

@paul-elliott-arm paul-elliott-arm added needs-backports Backports are missing or are pending review and approval. and removed needs-work labels Dec 3, 2020
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.

LGTM but not to CI

bytes. This guarantees that the corresponding parsing function can read
the buffer back, which was the case for mbedtls_x509write_{crt,csr}_pem
until this property was inadvertently broken in Mbed TLS 2.19.0.
Fixes #3682.
Copy link
Contributor

Choose a reason for hiding this comment

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

Please ensure that tests/scripts/all.sh -k check_files check_changelog passes

Copy link
Member Author

Choose a reason for hiding this comment

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

Missing newline at end. Fixed.

@gabor-mezei-arm gabor-mezei-arm self-requested a review December 7, 2020 11:50
@gabor-mezei-arm gabor-mezei-arm removed the needs-reviewer This PR needs someone to pick it up for review label Dec 7, 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-backports Backports are missing or are pending review and approval. needs-review Every commit must be reviewed by at least two team members, labels Dec 8, 2020
@gilles-peskine-arm
Copy link
Contributor

The CI is being unstable, but between https://jenkins-mbedtls.oss.arm.com/blue/organizations/jenkins/mbed-tls-pr-merge/detail/PR-3898-merge/10 and https://jenkins-mbedtls.oss.arm.com/blue/organizations/jenkins/mbed-tls-pr-merge/detail/PR-3898-merge/11 all the jobs passed (for Windows-2013, which failed in both, all the sub-jobs passed in at least one of the runs).

@gilles-peskine-arm gilles-peskine-arm merged commit 6d5c7bc into Mbed-TLS:development Dec 8, 2020
@paul-elliott-arm paul-elliott-arm deleted the fix_pem_write branch October 13, 2021 16:53
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 needs-ci Needs to pass CI tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error when generating a certificate via mbedtls_x509write_crt_pem and then parsing it.
3 participants