Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <dd8fc833-fcf9-44f2-9425-b6111c57f4b8@gmail.com>
Date: Sat, 11 Apr 2026 15:06:10 -0400
From: Demi Marie Obenour <demiobenour@...il.com>
To: musl@...ts.openwall.com, Gilles Peskine <gilles.peskine@....com>
Subject: Re: Detecting getrandom() or syscall()

On 4/10/26 12:06, Gilles Peskine wrote:
> 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().

One option is to assume that these headers are present on Linux.
If they are not, the build breaks and people know they need to use
a better libc.
-- 
Sincerely,
Demi Marie Obenour (she/her/hers)
Download attachment "OpenPGP_0xB288B55FFF9C22C1.asc" of type "application/pgp-keys" (7141 bytes)

Download attachment "OpenPGP_signature.asc" of type "application/pgp-signature" (834 bytes)

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.