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 uninitialized variable in x509_crt #2392

Closed
wants to merge 1 commit into from

Conversation

agross-oss
Copy link
Contributor

@agross-oss agross-oss commented Jan 30, 2019

This patch fixes an issue we encountered with more stringent compiler
warnings. The signature_is_good variable has a possibility of being
used uninitialized. This patch initializes the variable at the top
of the function.

Signed-off-by: Andy Gross andy.gross@linaro.org

We found this issue while using mbedtls inside the Zephyr project. We recently have started using the x509 features and this exposed this specific issue due to our compiler settings. I am unsure about backporting as we are using 2.16.0.

Gatekeeping note: (added by mpg) this PR should be merge both development and mbedtls-2.16. The issue is not present in mbedtls-2.7 so no need for backports.

@hanno-becker
Copy link

Thanks @agross-linaro for your contribution! To my understanding, signature_is_good is never used uninitialized, but it's just (too) hard for the compiler to notice that - do you agree? I remember being impressed that some compilers apparently are capable of that amount of reasoning.

@agross-oss
Copy link
Contributor Author

@hanno-arm well for people using modern compilers on projects which require no warnings these kinds of things pop up. With all of the continues in that function I can see how this could potentially happen. But it probably would never happen with the way the function is being used.

I also wonder if tooling like coverity might catch this.

@hanno-becker
Copy link

hanno-becker commented Jan 30, 2019

@agross-linaro Could you elaborate? Do you see an execution path which would lead to signature_is_good being used uninitialized? I don't see one. The only way the branch parent != NULL can be taken is if the loop exited through the break instruction, which happens after signature_is_good is set.

well for people using modern compilers on projects which require no warnings

We require no warnings, too, so we'll fix this if some major toolchain warns about it - even if it's unjustified.

I also wonder if tooling like coverity might catch this.

I would expect Coverity to be able to perform the above reasoning, hence not warn about it.

library/x509_crt.c Outdated Show resolved Hide resolved
@agross-oss
Copy link
Contributor Author

So this can be bad if:

  1. this is a non re-started call to x509_crt_find_parent_in()
  2. you have a valid candidate list that has a single entry
  3. The x509_crt_check_parent() returns non zero

With these 3 things, you can use the signature_is_good uninitialized.

@hanno-becker
Copy link

@agross-linaro No, in this case parent = parent->next will set parent to NULL before leaving the loop.

@agross-oss
Copy link
Contributor Author

Oh you're right. Let me keep trying to find the path then =D

@hanno-becker
Copy link

@agross-linaro Whatever the outcome, the fact that it's not immediately clear is reason enough to change it. See the review suggestion.

@RonEld RonEld added bug CLA valid needs-review Every commit must be reviewed by at least two team members, component-x509 labels Jan 31, 2019
@RonEld
Copy link
Contributor

RonEld commented Jan 31, 2019

CLA valid as part of the Linaro CLA

hanno-becker
hanno-becker previously approved these changes Jan 31, 2019
Copy link

@hanno-becker hanno-becker left a comment

Choose a reason for hiding this comment

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

Thanks @agross-linaro for your contribution, looks good to me!

@hanno-becker hanno-becker self-assigned this Jan 31, 2019
@hanno-becker hanno-becker requested a review from mpg January 31, 2019 09:44
mpg
mpg previously approved these changes Jan 31, 2019
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.

Looks good to me. I agree with Hanno that I don't see a code path where the variable could actually be used without being initialized, but the changes improve readbility by making this more obvious, and fixing spurious warnings in compilers or static analysers is also valuable. Thanks for your contribution.

@mpg mpg removed the needs-review Every commit must be reviewed by at least two team members, label Jan 31, 2019
@mpg
Copy link
Contributor

mpg commented Jan 31, 2019

@agross-linaro I don't think this requires backporting as development has not yet diverged enough from mbedtls-2.16 so this can be merged in both branches, and the issue is not present in mbedtls-2.7. I'm taking the liberty of editing the PR description in order to make this more apparent to gatekeepers.

@hanno-becker
Copy link

Ping @Patater @sbutcher-arm for gatekeeping.

@mpg
Copy link
Contributor

mpg commented Jan 31, 2019

@agross-linaro We usually ask external contributors to add an entry in the ChangeLog crediting themselves. Please add commit to this PR with ChangeLog entry in the Bugfix section, mentioning fixing a compiler warning, with you name (and affiliation) as you want it, plus this issue number. I'm labeling as "needs: work" as a reminder for that.

Again, thanks for your contribution.

@mpg mpg added the needs-work label Jan 31, 2019
agross-oss pushed a commit to agross-oss/zephyr that referenced this pull request Feb 7, 2019
This patch fixes an issue with an uninitialized variable in the x509
mbedtls feature.  I sent a related patch to the mbedtls project so
that this can be fixed in the future.

Mbed-TLS/mbedtls#2392

Signed-off-by: Andy Gross <andy.gross@linaro.org>
galak pushed a commit to zephyrproject-rtos/zephyr that referenced this pull request Feb 8, 2019
This patch fixes an issue with an uninitialized variable in the x509
mbedtls feature.  I sent a related patch to the mbedtls project so
that this can be fixed in the future.

Mbed-TLS/mbedtls#2392

Signed-off-by: Andy Gross <andy.gross@linaro.org>
@agross-oss agross-oss dismissed stale reviews from mpg and hanno-becker via 1d5023d February 11, 2019 23:15
This patch fixes an issue we encountered with more stringent compiler
warnings.  The signature_is_good variable has a possibility of being
used uninitialized.  This patch moves the use of the variable to a
place where it cannot be used while uninitialized.

Signed-off-by: Andy Gross <andy.gross@linaro.org>
@agross-oss
Copy link
Contributor Author

Sorry for the delay, I had other pressing concerns. I added the requested Changelog entry.

Copy link

@hanno-becker hanno-becker left a comment

Choose a reason for hiding this comment

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

Thanks @agross-linaro for adding the entry! Just some nitpicking: Could you remove the double space, and perhaps say something like "Improve code clarity in X.509 module, removing false-positive uninitialized variable warnings on some toolchains."? "Fix compiler warning" is a bit too generic, and it doesn't entail whether the warning was justified and potentially hinting at a serious bug.

nashif pushed a commit to zephyrproject-rtos/mbedtls that referenced this pull request May 9, 2019
This patch fixes an issue with an uninitialized variable in the x509
mbedtls feature.  I sent a related patch to the mbedtls project so
that this can be fixed in the future.

Mbed-TLS/mbedtls#2392

Signed-off-by: Andy Gross <andy.gross@linaro.org>
@pfalcon
Copy link
Contributor

pfalcon commented Aug 15, 2019

Hello, some updates here: Andy is not with Linaro any longer, that's why I assume this issue got stale. As his colleague, I'd be willing to pick this up and make last-mile changes to make it mergeable. Please let me know if it's ok if I open a separate PR with the changes requested (or if there's some other way). @agross-oss: Please let me know if that sounds ok for you too.

I would expect Coverity to be able to perform the above reasoning, hence not warn about it.

To elaborate on the nature of the compiler warning described here, compilers perform a static analysis, based on control flow graph (CFG) properties of the code (here, that each static use is dominated by a definition). Compilers don't perform analysis of dynamic code properties, like possible actual code execution paths, because it's equivalent of Halting problem.

Bottom line of this theoretical excursion: in software maintenance practice, such warnings can always appear, so thanks mbedTLS team for handling these proactively!

@pfalcon
Copy link
Contributor

pfalcon commented Aug 15, 2019

To avoid possible further slips due to weekend, etc., I proactively posted #2795 . Apologies if that's a bit hasty move, can be closed any time if this PR is going to be revamped.

ioannisg pushed a commit to ioannisg/mbedtls that referenced this pull request Aug 28, 2019
This patch fixes an issue with an uninitialized variable in the x509
mbedtls feature.  I sent a related patch to the mbedtls project so
that this can be fixed in the future.

Mbed-TLS/mbedtls#2392

Signed-off-by: Andy Gross <andy.gross@linaro.org>
nashif pushed a commit to zephyrproject-rtos/mbedtls that referenced this pull request Aug 28, 2019
This patch fixes an issue with an uninitialized variable in the x509
mbedtls feature.  I sent a related patch to the mbedtls project so
that this can be fixed in the future.

Mbed-TLS/mbedtls#2392

Signed-off-by: Andy Gross <andy.gross@linaro.org>
@pfalcon
Copy link
Contributor

pfalcon commented Sep 6, 2019

This has now been superseded by #2795, #2813 . @mpg, @k-stachowiak, @Patater: Thanks for merging those, and can you please close this PR?

@mpg
Copy link
Contributor

mpg commented Sep 6, 2019

Closing for the reasons mentioned by @pfalcon.

@mpg mpg closed this Sep 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants