Skip to content

Commit 7dcd8f8

Browse files
committed
Merge branch 'PHP-8.1'
2 parents 3be9118 + f15cfba commit 7dcd8f8

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ PHP NEWS
3434
notation to int. (Dennis Snell)
3535
. Enable arc4random_buf for Linux glibc 2.36 and onwards
3636
for the random_bytes. (Cristian Rodriguez)
37+
. Uses CCRandomGenerateBytes instead of arc4random_buf on macOs. (David Carlier).
3738

3839
07 Jul 2022, PHP 8.2.0alpha3
3940

ext/standard/config.m4

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,12 @@ dnl Check for arc4random on BSD systems
403403
dnl
404404
AC_CHECK_DECLS([arc4random_buf])
405405

406+
dnl
407+
dnl Check for CCRandomGenerateBytes
408+
dnl header absent in previous macOs releases
409+
dnl
410+
AC_CHECK_HEADERS([CommonCrypto/CommonRandom.h])
411+
406412
dnl
407413
dnl Check for argon2
408414
dnl

ext/standard/random.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
# include <sys/random.h>
3636
# endif
3737
#endif
38+
#if HAVE_COMMONCRYPTO_COMMONRANDOM_H
39+
# include <CommonCrypto/CommonCryptoError.h>
40+
# include <CommonCrypto/CommonRandom.h>
41+
#endif
3842

3943
#if __has_feature(memory_sanitizer)
4044
# include <sanitizer/msan_interface.h>
@@ -94,6 +98,19 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, bool should_throw)
9498
}
9599
return FAILURE;
96100
}
101+
#elif HAVE_COMMONCRYPTO_COMMONRANDOM_H
102+
/*
103+
* Purposely prioritized upon arc4random_buf for modern macOs releases
104+
* arc4random api on this platform uses `ccrng_generate` which returns
105+
* a status but silented to respect the "no fail" arc4random api interface
106+
* the vast majority of the time, it works fine ; but better make sure we catch failures
107+
*/
108+
if (CCRandomGenerateBytes(bytes, size) != kCCSuccess) {
109+
if (should_throw) {
110+
zend_throw_exception(zend_ce_exception, "Error generating bytes", 0);
111+
}
112+
return FAILURE;
113+
}
97114
#elif HAVE_DECL_ARC4RANDOM_BUF && ((defined(__OpenBSD__) && OpenBSD >= 201405) || (defined(__NetBSD__) && __NetBSD_Version__ >= 700000001) || defined(__APPLE__) || defined(__GLIBC__))
98115
arc4random_buf(bytes, size);
99116
#else

0 commit comments

Comments
 (0)