Skip to content

Commit 948cb47

Browse files
committed
random netbsd 10 update finally supporting getrandom syscall properly.
Close GH-10327.
1 parent c59e075 commit 948cb47

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

NEWS

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ PHP NEWS
8181
. Added Randomizer::nextFloat(), ::getFloat(), and IntervalBoundary. (timwolla)
8282
. Fix GH-10292 (Made the default value of the first param of srand() and
8383
mt_srand() nullable). (kocsismate)
84+
. Enable getrandom() for NetBSD (from 10.x). (David Carlier)
8485

8586
- Reflection:
8687
. Fix GH-9470 (ReflectionMethod constructor should not find private parent

ext/random/random.c

+19-5
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949

5050
#if HAVE_SYS_PARAM_H
5151
# include <sys/param.h>
52-
# if (__FreeBSD__ && __FreeBSD_version > 1200000) || (__DragonFly__ && __DragonFly_version >= 500700) || defined(__sun)
52+
# if (__FreeBSD__ && __FreeBSD_version > 1200000) || (__DragonFly__ && __DragonFly_version >= 500700) || \
53+
defined(__sun) || (defined(__NetBSD__) && __NetBSD_Version__ >= 1000000000)
5354
# include <sys/random.h>
5455
# endif
5556
#endif
@@ -502,14 +503,27 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, bool should_throw)
502503
}
503504
return FAILURE;
504505
}
505-
#elif HAVE_DECL_ARC4RANDOM_BUF && ((defined(__OpenBSD__) && OpenBSD >= 201405) || (defined(__NetBSD__) && __NetBSD_Version__ >= 700000001) || defined(__APPLE__))
506+
#elif HAVE_DECL_ARC4RANDOM_BUF && ((defined(__OpenBSD__) && OpenBSD >= 201405) || (defined(__NetBSD__) && __NetBSD_Version__ >= 700000001 && __NetBSD_Version__ < 1000000000) || \
507+
defined(__APPLE__) || defined(__GLIBC__))
508+
/*
509+
* OpenBSD until there is a valid equivalent
510+
* or NetBSD before the 10.x release
511+
* falls back to arc4random_buf
512+
* giving a decent output, the main benefit
513+
* is being (relatively) failsafe.
514+
* Older macOs releases fall also into this
515+
* category for reasons explained above.
516+
*/
506517
arc4random_buf(bytes, size);
507518
#else
508519
size_t read_bytes = 0;
509520
ssize_t n;
510-
# if (defined(__linux__) && defined(SYS_getrandom)) || (defined(__FreeBSD__) && __FreeBSD_version >= 1200000) || (defined(__DragonFly__) && __DragonFly_version >= 500700) || defined(__sun)
511-
/* Linux getrandom(2) syscall or FreeBSD/DragonFlyBSD getrandom(2) function*/
512-
/* Keep reading until we get enough entropy */
521+
# if (defined(__linux__) && defined(SYS_getrandom)) || (defined(__FreeBSD__) && __FreeBSD_version >= 1200000) || (defined(__DragonFly__) && __DragonFly_version >= 500700) || \
522+
defined(__sun) || (defined(__NetBSD__) && __NetBSD_Version__ >= 1000000000)
523+
/* Linux getrandom(2) syscall or FreeBSD/DragonFlyBSD/NetBSD getrandom(2) function
524+
* Being a syscall, implemented in the kernel, getrandom offers higher quality output
525+
* compared to the arc4random api albeit a fallback to /dev/urandom is considered.
526+
*/
513527
while (read_bytes < size) {
514528
errno = 0;
515529

0 commit comments

Comments
 (0)