diff options
author | fanquake <fanquake@gmail.com> | 2023-05-18 11:25:44 +0100 |
---|---|---|
committer | fanquake <fanquake@gmail.com> | 2023-05-20 17:20:01 +0100 |
commit | d5e06919db5e221bfef445c5a40c88de72dc5869 (patch) | |
tree | 0002aae54713711d4b67ea4eed0a26e791986fd7 /src/random.cpp | |
parent | c2ba3f5b0c7d0eece7d16d1ffc125d8a6a9297af (diff) | |
download | bitcoin-d5e06919db5e221bfef445c5a40c88de72dc5869.tar.xz |
random: switch to using getrandom() directly
This requires a linux kernel of 3.17.0+, which seems entirely
reasonable. 3.17 went EOL in 2015, and the last supported 3.x kernel
(3.16) went EOL > 4 years ago, in 2020. For reference, the current
oldest maintained kernel is 4.14 (released 2017, EOL Jan 2024).
Support for `getrandom()` (and `getentropy()`) was added to
glibc 2.25, https://sourceware.org/legacy-ml/libc-alpha/2017-02/msg00079.html,
and we already require 2.27+.
All that being said, I don't think you would encounter a current day
system, running with kernel headers older than 3.17 (released 2014) but
also having a glibc of 2.27+ (released 2018).
Diffstat (limited to 'src/random.cpp')
-rw-r--r-- | src/random.cpp | 22 |
1 files changed, 5 insertions, 17 deletions
diff --git a/src/random.cpp b/src/random.cpp index 61f342269f..54500e6cc6 100644 --- a/src/random.cpp +++ b/src/random.cpp @@ -28,13 +28,10 @@ #include <sys/time.h> #endif -#ifdef HAVE_SYS_GETRANDOM -#include <sys/syscall.h> -#include <linux/random.h> -#endif -#if defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX) +#if defined(HAVE_GETRANDOM) || (defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX)) #include <sys/random.h> #endif + #ifdef HAVE_SYSCTL_ARND #include <sys/sysctl.h> #endif @@ -284,23 +281,14 @@ void GetOSRand(unsigned char *ent32) RandFailure(); } CryptReleaseContext(hProvider, 0); -#elif defined(HAVE_SYS_GETRANDOM) +#elif defined(HAVE_GETRANDOM) /* Linux. From the getrandom(2) man page: * "If the urandom source has been initialized, reads of up to 256 bytes * will always return as many bytes as requested and will not be * interrupted by signals." */ - int rv = syscall(SYS_getrandom, ent32, NUM_OS_RANDOM_BYTES, 0); - if (rv != NUM_OS_RANDOM_BYTES) { - if (rv < 0 && errno == ENOSYS) { - /* Fallback for kernel <3.17: the return value will be -1 and errno - * ENOSYS if the syscall is not available, in that case fall back - * to /dev/urandom. - */ - GetDevURandom(ent32); - } else { - RandFailure(); - } + if (getrandom(ent32, NUM_OS_RANDOM_BYTES, 0) != NUM_OS_RANDOM_BYTES) { + RandFailure(); } #elif defined(__OpenBSD__) /* OpenBSD. From the arc4random(3) man page: |