From faf835be2bd4db8e57e2529a8022b208cd9201b2 Mon Sep 17 00:00:00 2001 From: Leigh Date: Fri, 30 Oct 2015 19:08:20 +0000 Subject: [PATCH] Use arc4random on OpenBSD 5.5+ and NetBSD 7+ As discussed with @weltling, keeping arc4random on the condition that the OS has a solid implementation of it --- ext/standard/config.m4 | 5 +++++ ext/standard/random.c | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/ext/standard/config.m4 b/ext/standard/config.m4 index f41df7e9861..c435f965540 100644 --- a/ext/standard/config.m4 +++ b/ext/standard/config.m4 @@ -592,6 +592,11 @@ dnl Check for atomic operation API availability in Solaris dnl AC_CHECK_HEADERS([atomic.h]) +dnl +dnl Check for arc4random on BSD systems +dnl +AC_CHECK_DECLS([arc4random_buf]) + dnl dnl Check for getrandom on newer Linux kernels dnl diff --git a/ext/standard/random.c b/ext/standard/random.c index fd730d1bfd7..70568799dea 100644 --- a/ext/standard/random.c +++ b/ext/standard/random.c @@ -33,6 +33,9 @@ #ifdef __linux__ # include #endif +#if defined(__OpenBSD__) || defined(__NetBSD__) +# include +#endif #ifdef ZTS int random_globals_id; @@ -87,6 +90,8 @@ static int php_random_bytes(void *bytes, size_t size) zend_throw_exception(zend_ce_exception, "Could not gather sufficient random data", 0); return FAILURE; } +#elif HAVE_DECL_ARC4RANDOM_BUF && ((defined(__OpenBSD__) && OpenBSD >= 201405) || (defined(__NetBSD__) && __NetBSD_Version__ >= 700000001)) + arc4random_buf(bytes, size); #elif HAVE_DECL_GETRANDOM /* Linux getrandom(2) syscall */ size_t read_bytes = 0;