From 8e1f6dab19744ff566393c805b3484e7480b3af1 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Tue, 13 Aug 2024 18:12:50 +0100 Subject: [PATCH] Fix GH-15381 (getrandom part). 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. --- ext/random/config.m4 | 5 +++++ ext/random/random.c | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/ext/random/config.m4 b/ext/random/config.m4 index a8e6d5a568991..bb4214a5c0830 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/random.c b/ext/random/random.c index e30a04259fb2b..576c8412f52f5 100644 --- a/ext/random/random.c +++ b/ext/random/random.c @@ -48,7 +48,7 @@ #if HAVE_SYS_PARAM_H # include -# if (__FreeBSD__ && __FreeBSD_version > 1200000) || (__DragonFly__ && __DragonFly_version >= 500700) || defined(__sun) +# if (__FreeBSD__ && __FreeBSD_version > 1200000) || (__DragonFly__ && __DragonFly_version >= 500700) || (defined(__sun) && defined(HAVE_GETRANDOM)) # include # endif #endif @@ -511,7 +511,7 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, bool should_throw) #else size_t read_bytes = 0; ssize_t n; -# if (defined(__linux__) && defined(SYS_getrandom)) || (defined(__FreeBSD__) && __FreeBSD_version >= 1200000) || (defined(__DragonFly__) && __DragonFly_version >= 500700) || defined(__sun) +# if (defined(__linux__) && defined(SYS_getrandom)) || (defined(__FreeBSD__) && __FreeBSD_version >= 1200000) || (defined(__DragonFly__) && __DragonFly_version >= 500700) || (defined(__sun) && defined(HAVE_GETRANDOM)) /* Linux getrandom(2) syscall or FreeBSD/DragonFlyBSD getrandom(2) function*/ /* Keep reading until we get enough entropy */ while (read_bytes < size) {