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 <yann.morin@orange.com>
This commit is contained in:
Yann E. MORIN
2026-02-10 12:01:32 +01:00
committed by Roger Light
parent d3ee5c5ca6
commit a487cd4f8f
2 changed files with 15 additions and 7 deletions

View File

@@ -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)

View File

@@ -26,18 +26,18 @@ Contributors:
# include <lmcons.h>
#endif
#if !defined(WITH_TLS) && defined(__linux__) && defined(__GLIBC__)
#ifdef WITH_TLS
# include <openssl/bn.h>
# include <openssl/rand.h>
#elif defined(HAVE_GETRANDOM) /* From CMakeLists.txt */
# include <sys/random.h>
#elif defined(__linux__) && defined(__GLIBC__) /* For legacy Makefiles */
# if __GLIBC_PREREQ(2, 25)
# include <sys/random.h>
# define HAVE_GETRANDOM 1
# endif
#endif
#ifdef WITH_TLS
# include <openssl/bn.h>
# include <openssl/rand.h>
#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;