Fix getrandom detection with musl libc #843
No reviewers
Labels
No labels
api-break
approved
bug
component-docs
component-platform
component-test
dependencies
deprecation
DO-NOT-MERGE
enhancement
good first issue
help wanted
needs-adoption
needs-backports
needs-ci
needs-design-approval
needs-info
needs-preceding-pr
needs-review
needs-reviewer
needs-work
priority-high
priority-low
priority-medium
priority-scheduled
priority-very-high
resolved-after-1.1
single-reviewer
size-l
size-m
size-optimisation
size-s
size-xs
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
gh-Mbed-TLS/TF-PSA-Crypto!843
Loading…
Reference in a new issue
No description provided.
Delete branch "fix/musl-getrandom"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Description
On Linux, the platform entropy implementation currently enables the
getrandom syscall path only for glibc and Midipix. This excludes musl and
other libc implementations that can use the same syscall path, so affected
builds fall back to
/dev/random. On older Linux kernels, a subsequentprocess may block during PSA crypto initialization after earlier reads
have depleted the available entropy count.
Use
__has_include(<sys/syscall.h>)on Linux when supported by thecompiler, and enable the existing syscall wrapper only when
SYS_getrandomis defined. Preserve the glibc/Midipix condition for oldertoolchains and the existing fallback when the kernel returns
ENOSYS.The initial
<sys/random.h>/libc-wrapper detection was replaced duringreview because it broke dietlibc builds. The final implementation reuses
syscall(SYS_getrandom, ...)and adds no public configuration option.Validation
Manual validation of this development PR, reported on August 3:
SYS_getrandomis absent and the existing fallback remained selected.The individual backport PRs document their separate validation results.
PR checklist
I'm all for improving the library on musl, but not if it breaks the build elsewhere.
The right thing to do would be to guard the non-portable code with
#if __MUSL__or some such. But since this does not and will not exist, I'm afraid we're stuck. We could add a way to declare “I swear I havegetrandom()” at compile time. But that's even more complexity in code that's already more complex than I'd like. So I'd want to set up some testing if we do that, and that's complicated.@ -0,0 +1,5 @@FeaturesI wouldn't consider this a bug in TF-PSA-Crypto (but a bug in older Linux kernels). We're just not taking advantage of a nicer feature exposed by musl.
@ -298,0 +296,4 @@#if defined(__linux__) && defined(__has_include)#if __has_include(<sys/syscall.h>)#define MBEDTLS_PLATFORM_HAS_SYS_SYSCALL_H#endifThis breaks the build with dietlibc.
(on Ubuntu 24.04)
(I'm pretty sure that's why I didn't do that when I overhauled our random-on-Linux code recently.)
Thanks, you're right — checking only for the presence of
<sys/random.h>is insufficient and causes a regression for dietlibc. I'll remove the automatic detection and reclassify the changelog entry as a feature.Before updating the implementation, would you be open to an explicit opt-in configuration macro, for example
MBEDTLS_PLATFORM_HAS_GETRANDOM? The default behavior would remain unchanged, and the libc wrapper would only be used when the integrator explicitly declares it available. I would add build coverage for both the enabled path and the unchanged fallback path, while retaining the musl/QEMU consecutive-initialization validation.We prefer to keep the number of compile-time options down, but if that's the only way, we'll accept it.
But there's one thing I'm not sure I've tried: maybe the block that uses
syscall(SYS_getrandom, ...)would work on the platforms where__has_include(<sys/syscall.h>)works? Would you mind checking how that goes with dietlibc and musl and anything else you have easy access to?Ideally we'd like this to be covered in our CI, but that would require more work on our CI images than we have time for at the moment. So we'll settle for manual testing for now.
That's a great idea—thanks for pointing it out. I replaced the
<sys/random.h>/libc-wrapper detection with a guarded raw-syscall path:__has_include(<sys/syscall.h>)when available.SYS_getrandomis defined./dev/randomfallback.I also reclassified the changelog entry as a feature.
Validation performed:
consecutive initializations passed.
consecutive initializations passed.
<sys/syscall.h>is present butSYS_getrandomis notdefined; the build passed and the existing fallback remained selected.
This avoids the
<sys/random.h>regression.two consecutive initializations passed.
passed; syscall path selected; the static binary passed two consecutive
initializations.
@ -0,0 +1,5 @@FeaturesI've removed the Markdown backticks and changed it to the conventional getrandom() spelling.
LGTM
LGTM, thanks!
Can you make a backport to 1.1 and Mbed TLS 3.6? Then we can merge.
Thanks! I’ll prepare the backports to TF-PSA-Crypto 1.1 and Mbed TLS 3.6.