diff --git a/NEWS b/NEWS index 05fe9627f55..0c8cd5ab895 100644 --- a/NEWS +++ b/NEWS @@ -39,7 +39,8 @@ PHP NEWS modifier). (Pierrick) - Standard: - . Fixed the crypt_sha256/512 api build with clang > 12. (David Carier) + . Fixed the crypt_sha256/512 api build with clang > 12. (David Carlier) + . Uses CCRandomGenerateBytes instead of arc4random_buf on macOs. (David Carlier). 07 Jul 2022, PHP 8.1.8 diff --git a/ext/standard/config.m4 b/ext/standard/config.m4 index 32664f8025f..97a74d22249 100644 --- a/ext/standard/config.m4 +++ b/ext/standard/config.m4 @@ -406,6 +406,12 @@ dnl Check for arc4random on BSD systems dnl AC_CHECK_DECLS([arc4random_buf]) +dnl +dnl Check for CCRandomGenerateBytes +dnl header absent in previous macOs releases +dnl +AC_CHECK_HEADERS([CommonCrypto/CommonRandom.h]) + dnl dnl Check for argon2 dnl diff --git a/ext/standard/random.c b/ext/standard/random.c index d6140ffd2ae..a023a0f9a86 100644 --- a/ext/standard/random.c +++ b/ext/standard/random.c @@ -35,6 +35,10 @@ # include # endif #endif +#if HAVE_COMMONCRYPTO_COMMONRANDOM_H +# include +# include +#endif #if __has_feature(memory_sanitizer) # include @@ -94,6 +98,19 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, bool should_throw) } return FAILURE; } +#elif HAVE_COMMONCRYPTO_COMMONRANDOM_H + /* + * Purposely prioritized upon arc4random_buf for modern macOs releases + * arc4random api on this platform uses `ccrng_generate` which returns + * a status but silented to respect the "no fail" arc4random api interface + * the vast majority of the time, it works fine ; but better make sure we catch failures + */ + if (CCRandomGenerateBytes(bytes, size) != kCCSuccess) { + if (should_throw) { + zend_throw_exception(zend_ce_exception, "Error generating bytes", 0); + } + return FAILURE; + } #elif HAVE_DECL_ARC4RANDOM_BUF && ((defined(__OpenBSD__) && OpenBSD >= 201405) || (defined(__NetBSD__) && __NetBSD_Version__ >= 700000001) || defined(__APPLE__)) arc4random_buf(bytes, size); #else