Skip to content

Conversation

@gilles-peskine-arm
Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm commented May 7, 2025

Fix issues with some PSA operation contexts not being properly initialized to all-bits-zero:

In this pull request, the practical definition of done is that the unit tests now pass with GCC 15. I don't think this is sufficient non-regression testing for #9814 and #9975. I've added a changelog entry, but I'm open to removing it until we have better testing.

I have written further tests which are feature-complete but may need a little rework to pass the CI, consisting of:

Beyond that, what we may want to test better — done in Mbed-TLS/mbedtls-framework#168 and #10179:

  • Make sure all relevant cases are covered in test drivers. We're at least missing coverage for multipart AEAD.
  • Make sure all relevant cases are covered with respect to reusing an operation object. I'm not sure what we're missing, if anything. Where we have test drivers, asserting that the context is all-bits-zero after a short initializer also practically asserts that the context is all-bits-zero when reusing an operation object, because the short initializer doesn't set the driver-specific part of the context to zero.

PR checklist

In API functions that set up a multipart or interruptible operation, make
sure to initialize the driver-specific part of the context. This is a union,
and initializing the union to `{0}` only guarantees that the first member of
the union is initialized, not necessarily the member used by the driver.
Most compilers do initialize the whole union to all-bits-zero, but some
don't. With compilers that don't, the lack of initialization caused failures
of built-in MAC, interruptible-sign and interruptible-verify. It could also
cause failures for other operations with third-party drivers: we promise
that drivers' setup entry points receive a zero-initialized operation
structure, but this promise was not kept.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
In functions that bypass the API functions and call the MAC driver wrapper
`psa_driver_wrapper_mac_sign_setup()` directly, make
sure to initialize the driver-specific part of the context. This is a union,
and initializing the union to `{0}` only guarantees that the first member of
the union is initialized, not necessarily the member used by the driver.
Most compilers do initialize the whole union to all-bits-zero, but some
don't. With compilers that don't, the lack of initialization caused failures
of the affected operations. This affected several key derivation operations.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
In functions that bypass the API functions and call an internal MAC setup
function directly, make sure to initialize the driver-specific part of the
context. This is a union, and initializing the union to `{0}` only
guarantees that the first member of the union is initialized, not
necessarily the member used by the driver. Most compilers do initialize the
whole union to all-bits-zero, but some don't. With compilers that don't, the
lack of initialization caused failures of the affected operations. This
affected one-shot MAC operations using the built-in implementation.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This is a non-regression test for
Mbed-TLS#9814

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
@gilles-peskine-arm gilles-peskine-arm added bug needs-backports Backports are missing or are pending review and approval. labels May 7, 2025
@gilles-peskine-arm gilles-peskine-arm added component-crypto Crypto primitives and low-level interfaces needs-ci Needs to pass CI tests size-s Estimated task size: small (~2d) priority-high High priority - will be reviewed soon 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 and removed needs-ci Needs to pass CI tests labels May 7, 2025
@bjwtaylor
Copy link

bjwtaylor commented May 9, 2025

When I build this branch using fc42 as follows:

docker pull fedora:42
docker run -it fedora:42 bash
sudo dnf group install "development-tools"
sudo dnf install cmake
sudo dnf install python3-jinja2
git clone --recurse-submodules -b union-initialization-gcc15-basic-fix-3.6 https://github.com/gilles-peskine-arm/mbedtls.git mbedtls.gilles-peskine-arm.union-initialization-gcc15-basic-fix-3.6
'mkdir mbedtls_build.gilles-peskine-arm.union-initialization-gcc15-basic-fix-3.6 cd mbedtls_build.gilles-peskine-arm.union-initialization-gcc15-basic-fix-3.6 cmake ../mbedtls.gilles-peskine-arm.union-initialization-gcc15-basic-fix-3.6/ cmake --build .`

I get the following build error:

/home/mbedtls.gilles-peskine-arm.union-initialization-gcc15-basic-fix-3.6/framework/tests/src/psa_exercise_key.c:187:36: error: initializer-string for array of ‘unsigned char’ truncates NUL terminator but destination lacks ‘nonstring’ attribute (33 chars into 32 available) [-Werror=unterminated-string-initialization]
187 | unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors
gmake[2]: *** [CMakeFiles/mbedtls_test.dir/build.make:317: CMakeFiles/mbedtls_test.dir/framework/tests/src/psa_exercise_key.c.o] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:1150: CMakeFiles/mbedtls_test.dir/all] Error 2
gmake: *** [Makefile:146: all] Error 2

Not sure this is related to the original issue, though also seems an issue with gcc 15. So maybe we fix it here or in another PR? Or is it some issue with my test method? gcc version FYI:

[root@1f02bf6b65be home]# gcc --version
gcc (GCC) 15.1.1 20250425 (Red Hat 15.1.1-1)
Copyright (C) 2025 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Let me know what you think?

@gilles-peskine-arm
Copy link
Contributor Author

@bjwtaylor That's #9944 which is out of scope here. We currently disable this warning in component_test_gcc15_drivers_opt in all.sh.

@gabor-mezei-arm gabor-mezei-arm requested review from gabor-mezei-arm and ronald-cron-arm and removed request for ronald-cron-arm May 13, 2025 08:12
Copy link
Contributor

@gabor-mezei-arm gabor-mezei-arm left a comment

Choose a reason for hiding this comment

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

Only minor things found. Otherwise looks good to me.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Copy link
Contributor

@gabor-mezei-arm gabor-mezei-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
Copy link
Contributor Author

@gabor-mezei-arm What do you think about test coverage? Do you think that Mbed-TLS/mbedtls-framework#135 is enough, or do we need more? (Just in terms of what it claims to do, it's not yet ready for a code review.)

@mpg mpg self-requested a review May 13, 2025 10:27
@mpg mpg removed the needs-reviewer This PR needs someone to pick it up for review label May 13, 2025
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.

LGTM!

More precisely, I think the changes that are here are correct. Regarding the question of whether they're complete, I'm relying on the tests (present and upcoming).

@github-project-automation github-project-automation bot moved this from In Development to Has Approval in Roadmap pull requests (new board) May 13, 2025
@gilles-peskine-arm
Copy link
Contributor Author

gilles-peskine-arm commented May 15, 2025

I've made pull requests for the test follow-ups. For 3.6:

  1. Check union initialization in test drivers mbedtls-framework#168
  2. Backport 3.6: Check union initialization portably #10179

The crypto and mbedtls pull requests will need a few rebase cycles after their precedessors are merged, but I don't intend to rewrite the existing history otherwise. I'll make new commits to handle review comments and CI failures.

@gabor-mezei-arm
Copy link
Contributor

@gabor-mezei-arm What do you think about test coverage? Do you think that Mbed-TLS/mbedtls-framework#135 is enough, or do we need more? (Just in terms of what it claims to do, it's not yet ready for a code review.)

I think these tests give enough coverage.

@mpg mpg removed needs-review Every commit must be reviewed by at least two team members, needs-backports Backports are missing or are pending review and approval. labels May 19, 2025
@mpg mpg added this pull request to the merge queue May 19, 2025
Merged via the queue into Mbed-TLS:mbedtls-3.6 with commit dad206d May 19, 2025
6 checks passed
@github-project-automation github-project-automation bot moved this from Has Approval to Done in Roadmap pull requests (new board) May 19, 2025
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 priority-high High priority - will be reviewed soon size-s Estimated task size: small (~2d)

Development

Successfully merging this pull request may close these issues.

4 participants