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

Accept getrandom syscall being unknown in mbedtls_platform_entropy_poll() for linux #2117

Merged
merged 3 commits into from
Dec 7, 2018

Conversation

hanno-becker
Copy link

Context: This PR fixes #1212 related to platform-specific entropy polling in runs using qemu user emulation.

Summary: Previously, the implementation of the entropy gathering function mbedtls_platform_entropy_poll() for linux machines used the following logic to determine how to obtain entropy from the kernel:

  1. If the getrandom() system call identifier SYS_getrandom is present and the kernel version is 3.17 or higher, use syscall( SYS_getrandom, ... )
  2. Otherwise, fall back to reading from /dev/random.

There are two issues with this:

  1. Portability: When cross-compiling the code for a different architecture and running it through system call emulation in qemu, qemu reports the host kernel version through uname but, as of v.2.5.0, doesn't support emulating the getrandom() syscall. This leads to mbedtls_platform_entropy_poll() failing even though reading from /dev/random would have worked.

  2. Complexity: Extracting the linux kernel version from the output of uname is slightly tedious.

This commit fixes both by implementing the suggestion in #1212:

  • It removes the kernel-version detection through uname().
  • Instead, it checks whether syscall( SYS_getrandom, ... ) fails with errno set to ENOSYS indicating an unknown system call. If so, it falls through to trying to read from /dev/random.

Fixes #1212.

Internal Reference: IOTSSL-1948.

Hanno Becker added 2 commits October 18, 2018 12:12
This commit fixes issue Mbed-TLS#1212 related to platform-specific entropy
polling in an syscall-emulated environment.

Previously, the implementation of the entropy gathering function
`mbedtls_platform_entropy_poll()` for linux machines used the
following logic to determine how to obtain entropy from the kernel:

1. If the getrandom() system call identifier SYS_getrandom is present and
   the kernel version is 3.17 or higher, use syscall( SYS_getrandom, ... )
2. Otherwise, fall back to reading from /dev/random.

There are two issues with this:

1. Portability:
   When cross-compiling the code for a different
   architecture and running it through system call
   emulation in qemu, qemu reports the host kernel
   version through uname but, as of v.2.5.0,
   doesn't support emulating the getrandom() syscall.
   This leads to `mbedtls_platform_entropy_poll()`
   failing even though reading from /dev/random would
   have worked.

2. Style:
   Extracting the linux kernel version from
   the output of `uname` is slightly tedious.

This commit fixes both by implementing the suggestion in Mbed-TLS#1212:
- It removes the kernel-version detection through uname().
- Instead, it checks whether `syscall( SYS_getrandom, ... )`
  fails with errno set to ENOSYS indicating an unknown system call.
  If so, it falls through to trying to read from /dev/random.

Fixes Mbed-TLS#1212.
@hanno-becker hanno-becker added enhancement mbed TLS team needs-review Every commit must be reviewed by at least two team members, component-platform Portability layer and build scripts labels Oct 18, 2018
@hanno-becker hanno-becker requested review from k-stachowiak and removed request for RonEld October 19, 2018 10:11
k-stachowiak
k-stachowiak previously approved these changes Oct 19, 2018
@hanno-becker hanno-becker requested review from mpg and removed request for gilles-peskine-arm October 25, 2018 12:13
mpg
mpg previously approved these changes Oct 29, 2018
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.

This is a very nice improvement to the code as it's now more portable, cleaner and more compact. Now I feel bad for not writing it that way in the first place :)

@mpg
Copy link
Contributor

mpg commented Oct 29, 2018

Note: the issue fixed by this PR is labeled as a bug and the ChangeLog entry in under "Bugfix", so for consistency I'm relabeling this PR as "bug", hence "needs backports".

@mpg mpg added bug needs-backports Backports are missing or are pending review and approval. and removed enhancement labels Oct 29, 2018
@mpg
Copy link
Contributor

mpg commented Oct 29, 2018

@hanno-arm Could you please investigate and fix the Ci failures too? Jenkins probably wants you to rebase on development so that it finds its config file, and Travis tells us check-names.sh is unhappy.

@hanno-becker
Copy link
Author

Backports opened: #2150 and #2151

@simonbutcher simonbutcher added the needs-ci Needs to pass CI tests label Nov 4, 2018
@simonbutcher
Copy link
Contributor

retest

@simonbutcher
Copy link
Contributor

Failure in Travis CI:

The command "tests/scripts/check-names.sh" exited with 2.
tests/scripts/check-files.py

@simonbutcher simonbutcher added needs-work and removed needs-ci Needs to pass CI tests labels Nov 4, 2018
@hanno-becker
Copy link
Author

@sbutcher-arm I couldn't reproduce this so far, and the CI log doesn't give any information on what went wrong.

Merged development into this PR, which will retrigger the CI.

@hanno-becker
Copy link
Author

@mpg @k-stachowiak Could you please re-review this PR?

@hanno-becker
Copy link
Author

@sbutcher-arm I don't know how check-files.py could fail because of that, but a bracket was missing in a code-path on systems without getrandom(). Now that this has been fixed in 1535150, the CI passes.

mpg
mpg previously approved these changes Nov 5, 2018
k-stachowiak
k-stachowiak previously approved these changes Nov 5, 2018
@simonbutcher
Copy link
Contributor

retest

@simonbutcher simonbutcher added the needs-ci Needs to pass CI tests label Nov 5, 2018
Wasn't spotted earlier because it's guarded by `! HAVE_GETRANDOM`.
@hanno-becker
Copy link
Author

@sbutcher-arm I removed the merge-commit as requested.

@hanno-becker
Copy link
Author

@sbutcher-arm @mpg @k-stachowiak Removing the merge commit rewrote the history starting for the last trivial commit 9772da8. Could you please re-review?

@mpg mpg removed needs-backports Backports are missing or are pending review and approval. needs-ci Needs to pass CI tests labels Nov 7, 2018
@simonbutcher simonbutcher added the approved Design and code approved - may be waiting for CI or backports label Nov 9, 2018
@Patater Patater merged commit 9772da8 into Mbed-TLS:development Dec 7, 2018
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-platform Portability layer and build scripts
Projects
None yet
Development

Successfully merging this pull request may close these issues.

mbedtls fails to initialize if run under qemu which does not support getrandom
5 participants