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

Fix empty union when TLS is disabled #7835

Conversation

gilles-peskine-arm
Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm commented Jun 25, 2023

When all TLS 1.2 support is disabled, union mbedtls_ssl_premaster_secret was empty, which is not valid C even if the union is never used. Fixes #6628.

As this is a bit of an edge case and has a simple workaround (don't include mbedtls/ssl.h if you aren't going to use it), I haven't gone through the effort of adding a test. If we think this really needs a test, I think we should use the following strategy: have a test program that includes all headers, which is built by a plain make — this way we would test that all headers work correctly in all the configurations we test (including ones with no TLS, and ones with picky compilers). We already have such a test program, but it's a C++ program, which we don't build in most configurations. So the implementation of this test would be to change programs/test/generate_cpp_dummy_build.sh to generate a header that includes all the library headers, and separate out the trivial C++ wrapping and make a similar C wrapping that is always built. Then make sure we pass -pedantic in CFLAGS in all.sh (otherwise gcc doesn't mind).

Manual testing:

scripts/config.py crypto
make lib CFLAGS='-Werror -pedantic'

Before: “error: union has no members”; after: passes.

PR checklist

Please tick as appropriate and edit the reasons (e.g.: "backport: not needed because this is a new feature")

Notes for the submitter

Please refer to the contributing guidelines, especially the
checklist for PR contributors.

When all TLS 1.2 support is disabled, union mbedtls_ssl_premaster_secret was
empty, which is not valid C even if the union is never used. Fixes Mbed-TLS#6628.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
@gilles-peskine-arm gilles-peskine-arm added bug component-tls needs-ci Needs to pass CI tests priority-medium Medium priority - this can be reviewed as time permits size-xs Estimated task size: extra small (a few hours at most) needs-backports Backports are missing or are pending review and approval. labels Jun 25, 2023
@daverodgman daverodgman added approved Design and code approved - may be waiting for CI or backports single-reviewer This PR qualifies for having only one reviewer labels Jul 24, 2023
@gilles-peskine-arm gilles-peskine-arm removed the needs-ci Needs to pass CI tests label Jul 26, 2023
Copy link
Contributor

@tom-cosgrove-arm tom-cosgrove-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

@gilles-peskine-arm gilles-peskine-arm added this pull request to the merge queue Jul 27, 2023
Merged via the queue into Mbed-TLS:development with commit 7ef14bf Jul 27, 2023
@irwir
Copy link
Contributor

irwir commented Jul 27, 2023

A slightly better hack for typical configurations - no extra byte.

#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
unsigned char _pms_rsa[48]; /* RFC 5246 8.1.1 /
#else
unsigned char dummy; /
Make the union non-empty even with SSL disabled */
#endif

@gilles-peskine-arm
Copy link
Contributor Author

@irwir It's a union. There's an extra byte only if it would be empty otherwise, and that's the point.

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 needs-backports Backports are missing or are pending review and approval. priority-medium Medium priority - this can be reviewed as time permits single-reviewer This PR qualifies for having only one reviewer size-xs Estimated task size: extra small (a few hours at most)
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

MSVC: Compilation fails due to "C requires that a struct or union have at least one member"
4 participants