From a487cd4f8f56a9352eec1b6e05592a73794b162b Mon Sep 17 00:00:00 2001 From: "Yann E. MORIN" Date: Tue, 10 Feb 2026 12:01:32 +0100 Subject: [PATCH] libcommon: check for getrandom() Not all C libraries are glibc, or impersonating it; for example, musl does not pretend to be any version of glibc. Thus, building on musl fails when openssl is disabled, because no random-providing function is detected, although musl does provide getrandom(). uClibc-ng on the other hand, can impersonate glibc, but availability of getrandom() is not guatranteed there: getrandom() can be compiled out of uClinbc-ng, or uClibc-ng can be too old to have it. Add a configure-time check that getrandom() is available, as a fallback when TLS is not enabled (and thus openssl is not used), and when not on Win32 (where getting random numbers is always possible, at least from a build perspective). However, building with the plain Makefiles should keep working, so slightly rework the defines checks in the code to account for the fact that HAVE_GETRANDOM may already be defined at configure time. Signed-off-by: Yann E. MORIN --- libcommon/CMakeLists.txt | 8 ++++++++ libcommon/random_common.c | 14 +++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/libcommon/CMakeLists.txt b/libcommon/CMakeLists.txt index 8d815566..fbea94c6 100644 --- a/libcommon/CMakeLists.txt +++ b/libcommon/CMakeLists.txt @@ -55,6 +55,14 @@ if (WITH_TLS) PUBLIC OpenSSL::SSL ) +elseif(NOT WIN32) + include(CheckSymbolExists) + check_symbol_exists(getrandom "sys/random.h" GETRANDOM_FOUND) + if(GETRANDOM_FOUND) + add_definitions("-DHAVE_GETRANDOM") + else() + message(FATAL_ERROR "C library does not provide getrandom(); enable WITH_TLS instead") + endif() endif() if(INC_MEMTRACK) diff --git a/libcommon/random_common.c b/libcommon/random_common.c index 555bd801..e2c8d23a 100644 --- a/libcommon/random_common.c +++ b/libcommon/random_common.c @@ -26,18 +26,18 @@ Contributors: # include #endif -#if !defined(WITH_TLS) && defined(__linux__) && defined(__GLIBC__) +#ifdef WITH_TLS +# include +# include +#elif defined(HAVE_GETRANDOM) /* From CMakeLists.txt */ +# include +#elif defined(__linux__) && defined(__GLIBC__) /* For legacy Makefiles */ # if __GLIBC_PREREQ(2, 25) # include # define HAVE_GETRANDOM 1 # endif #endif -#ifdef WITH_TLS -# include -# include -#endif - #include "mosquitto.h" @@ -65,7 +65,7 @@ int mosquitto_getrandom(void *bytes, int count) } CryptReleaseContext(provider, 0); -#else +#else /* For legacy Makefiles */ # error "No suitable random function found." #endif return rc;