Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <4ab2651b-c10b-40a9-a641-d5d402ddd881@arm.com>
Date: Fri, 10 Apr 2026 18:06:44 +0200
From: Gilles Peskine <gilles.peskine@....com>
To: musl@...ts.openwall.com
Subject: Detecting getrandom() or syscall()

Hello,

I'm a maintainer of a cryptography library (TF-PSA-Crypto, recently 
split out
of Mbed TLS), which is used on a wide variety of embedded systems.

Our library is mostly portable C, but we do try to detect the presence 
of some
critical system features. One such feature is a source of random data. When
running on a Linux kernel, we prefer the getrandom() system call, and 
fall back
to /dev/random or /dev/urandom.

Experimentally, at least with a modern musl version, we can call the
getrandom() system call with

     #include <unistd.h>
     #include <sys/syscall.h>
     syscall(SYS_getrandom, ...) // or __NR_getrandom perhaps on some libc?

However, not all systems have <unistd.h>, <sys/syscall.h> and syscall(). So
we need to wrap this code in preprocessor directives. We know that 
Linux-Glibc
and Midipix have these headers, so our code currently looks for 
getrandom() on
these systems.
https://github.com/Mbed-TLS/TF-PSA-Crypto/blob/tf-psa-crypto-1.1.0/platform/platform_util.c#L297

     #if ((defined(__linux__) && defined(__GLIBC__)) || 
defined(__midipix__))
     #include <unistd.h>
     #include <sys/syscall.h>
     #if defined(SYS_getrandom)
     ...
     syscall(SYS_getrandom, ...)

It would be nice to also use getrandom() on Linux-musl. But how can we 
detect
musl? We can't just use #if defined(__linux__) since not all Linux build
environments have <sys/syscall.h> and a syscall() function.

The musl FAQ explains that there's no feature detection macro for musl
because "it's a bug to assume a certain implementation has particular
properties rather than testing". But looking at the musl headers, I can't
find anything that I could test to guarantee the presence of <sys/syscall.h>
and syscall().

-- 
Best regards,

Gilles Peskine
TF-PSA-Crypto and Mbed TLS maintainer

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.