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).
This commit is contained in:
fanquake 2023-05-18 11:25:44 +01:00
parent c2ba3f5b0c
commit d5e06919db
No known key found for this signature in database
GPG key ID: 2EEB9F5CC09526C1
3 changed files with 11 additions and 24 deletions

View file

@ -1170,12 +1170,11 @@ AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <ctime>]],
) )
dnl Check for different ways of gathering OS randomness dnl Check for different ways of gathering OS randomness
AC_MSG_CHECKING([for Linux getrandom syscall]) AC_MSG_CHECKING([for Linux getrandom function])
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <unistd.h> AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
#include <sys/syscall.h> #include <sys/random.h>]],
#include <linux/random.h>]], [[ getrandom(nullptr, 32, 0); ]])],
[[ syscall(SYS_getrandom, nullptr, 32, 0); ]])], [ AC_MSG_RESULT([yes]); AC_DEFINE([HAVE_GETRANDOM], [1], [Define this symbol if the Linux getrandom function call is available]) ],
[ AC_MSG_RESULT([yes]); AC_DEFINE([HAVE_SYS_GETRANDOM], [1], [Define this symbol if the Linux getrandom system call is available]) ],
[ AC_MSG_RESULT([no])] [ AC_MSG_RESULT([no])]
) )

View file

@ -20,7 +20,7 @@ You can find installation instructions in the `build-*.md` file for your platfor
| [Boost](../depends/packages/boost.mk) | [link](https://www.boost.org/users/download/) | [1.81.0](https://github.com/bitcoin/bitcoin/pull/26557) | [1.64.0](https://github.com/bitcoin/bitcoin/pull/22320) | No | | [Boost](../depends/packages/boost.mk) | [link](https://www.boost.org/users/download/) | [1.81.0](https://github.com/bitcoin/bitcoin/pull/26557) | [1.64.0](https://github.com/bitcoin/bitcoin/pull/22320) | No |
| [libevent](../depends/packages/libevent.mk) | [link](https://github.com/libevent/libevent/releases) | [2.1.12-stable](https://github.com/bitcoin/bitcoin/pull/21991) | [2.1.8](https://github.com/bitcoin/bitcoin/pull/24681) | No | | [libevent](../depends/packages/libevent.mk) | [link](https://github.com/libevent/libevent/releases) | [2.1.12-stable](https://github.com/bitcoin/bitcoin/pull/21991) | [2.1.8](https://github.com/bitcoin/bitcoin/pull/24681) | No |
| glibc | [link](https://www.gnu.org/software/libc/) | N/A | [2.27](https://github.com/bitcoin/bitcoin/pull/27029) | Yes | | glibc | [link](https://www.gnu.org/software/libc/) | N/A | [2.27](https://github.com/bitcoin/bitcoin/pull/27029) | Yes |
| Linux Kernel | [link](https://www.kernel.org/) | N/A | 3.2.0 | Yes | | Linux Kernel | [link](https://www.kernel.org/) | N/A | [3.17.0](https://github.com/bitcoin/bitcoin/pull/27699) | Yes |
## Optional ## Optional

View file

@ -28,13 +28,10 @@
#include <sys/time.h> #include <sys/time.h>
#endif #endif
#ifdef HAVE_SYS_GETRANDOM #if defined(HAVE_GETRANDOM) || (defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX))
#include <sys/syscall.h>
#include <linux/random.h>
#endif
#if defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX)
#include <sys/random.h> #include <sys/random.h>
#endif #endif
#ifdef HAVE_SYSCTL_ARND #ifdef HAVE_SYSCTL_ARND
#include <sys/sysctl.h> #include <sys/sysctl.h>
#endif #endif
@ -284,24 +281,15 @@ void GetOSRand(unsigned char *ent32)
RandFailure(); RandFailure();
} }
CryptReleaseContext(hProvider, 0); CryptReleaseContext(hProvider, 0);
#elif defined(HAVE_SYS_GETRANDOM) #elif defined(HAVE_GETRANDOM)
/* Linux. From the getrandom(2) man page: /* Linux. From the getrandom(2) man page:
* "If the urandom source has been initialized, reads of up to 256 bytes * "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 * will always return as many bytes as requested and will not be
* interrupted by signals." * interrupted by signals."
*/ */
int rv = syscall(SYS_getrandom, ent32, NUM_OS_RANDOM_BYTES, 0); if (getrandom(ent32, NUM_OS_RANDOM_BYTES, 0) != NUM_OS_RANDOM_BYTES) {
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(); RandFailure();
} }
}
#elif defined(__OpenBSD__) #elif defined(__OpenBSD__)
/* OpenBSD. From the arc4random(3) man page: /* OpenBSD. From the arc4random(3) man page:
"Use of these functions is encouraged for almost all random number "Use of these functions is encouraged for almost all random number