From f1e5c63837cda0f73402180b7a50d271d63a53a7 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Tue, 13 Aug 2024 18:12:50 +0100 Subject: [PATCH] Checks getrandom availability on solaris. To fix part of GH-15381. gcc nor clang provides a constant to distinguish illumos and solaris not the system provides a kernel version stamp like the BSD. thus, we simply check the symbol and remaing purposely conservative in the existing logic, using it only for solaris to avoid unexpected breakages for other systems. would need a different fix for higher branches. Close GH-15390 --- ext/random/config.m4 | 5 +++++ ext/random/csprng.c | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/ext/random/config.m4 b/ext/random/config.m4 index 86f80224752..253979e3486 100644 --- a/ext/random/config.m4 +++ b/ext/random/config.m4 @@ -14,6 +14,11 @@ AC_CHECK_HEADERS([CommonCrypto/CommonRandom.h], [], [], #include ]) +dnl +dnl Mostly for non Linux systems +dnl +AC_CHECK_FUNCS([getrandom]) + dnl dnl Setup extension dnl diff --git a/ext/random/csprng.c b/ext/random/csprng.c index 106ec91affa..bbf12f567c1 100644 --- a/ext/random/csprng.c +++ b/ext/random/csprng.c @@ -46,7 +46,7 @@ #if HAVE_SYS_PARAM_H # include # if (__FreeBSD__ && __FreeBSD_version > 1200000) || (__DragonFly__ && __DragonFly_version >= 500700) || \ - defined(__sun) || (defined(__NetBSD__) && __NetBSD_Version__ >= 1000000000) + (defined(__sun) && defined(HAVE_GETRANDOM)) || (defined(__NetBSD__) && __NetBSD_Version__ >= 1000000000) # include # endif #endif @@ -98,7 +98,7 @@ PHPAPI zend_result php_random_bytes(void *bytes, size_t size, bool should_throw) #else size_t read_bytes = 0; # if (defined(__linux__) && defined(SYS_getrandom)) || (defined(__FreeBSD__) && __FreeBSD_version >= 1200000) || (defined(__DragonFly__) && __DragonFly_version >= 500700) || \ - defined(__sun) || (defined(__NetBSD__) && __NetBSD_Version__ >= 1000000000) + (defined(__sun) && defined(HAVE_GETRANDOM)) || (defined(__NetBSD__) && __NetBSD_Version__ >= 1000000000) /* Linux getrandom(2) syscall or FreeBSD/DragonFlyBSD/NetBSD getrandom(2) function * Being a syscall, implemented in the kernel, getrandom offers higher quality output * compared to the arc4random api albeit a fallback to /dev/urandom is considered.