Skip to content

Commit d5e0691

Browse files
committed
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).
1 parent c2ba3f5 commit d5e0691

File tree

3 files changed

+11
-24
lines changed

3 files changed

+11
-24
lines changed

configure.ac

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,12 +1170,11 @@ AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <ctime>]],
11701170
)
11711171

11721172
dnl Check for different ways of gathering OS randomness
1173-
AC_MSG_CHECKING([for Linux getrandom syscall])
1174-
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <unistd.h>
1175-
#include <sys/syscall.h>
1176-
#include <linux/random.h>]],
1177-
[[ syscall(SYS_getrandom, nullptr, 32, 0); ]])],
1178-
[ AC_MSG_RESULT([yes]); AC_DEFINE([HAVE_SYS_GETRANDOM], [1], [Define this symbol if the Linux getrandom system call is available]) ],
1173+
AC_MSG_CHECKING([for Linux getrandom function])
1174+
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
1175+
#include <sys/random.h>]],
1176+
[[ getrandom(nullptr, 32, 0); ]])],
1177+
[ AC_MSG_RESULT([yes]); AC_DEFINE([HAVE_GETRANDOM], [1], [Define this symbol if the Linux getrandom function call is available]) ],
11791178
[ AC_MSG_RESULT([no])]
11801179
)
11811180

doc/dependencies.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ You can find installation instructions in the `build-*.md` file for your platfor
2020
| [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 |
2121
| [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 |
2222
| glibc | [link](https://www.gnu.org/software/libc/) | N/A | [2.27](https://github.com/bitcoin/bitcoin/pull/27029) | Yes |
23-
| Linux Kernel | [link](https://www.kernel.org/) | N/A | 3.2.0 | Yes |
23+
| Linux Kernel | [link](https://www.kernel.org/) | N/A | [3.17.0](https://github.com/bitcoin/bitcoin/pull/27699) | Yes |
2424

2525
## Optional
2626

src/random.cpp

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,10 @@
2828
#include <sys/time.h>
2929
#endif
3030

31-
#ifdef HAVE_SYS_GETRANDOM
32-
#include <sys/syscall.h>
33-
#include <linux/random.h>
34-
#endif
35-
#if defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX)
31+
#if defined(HAVE_GETRANDOM) || (defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX))
3632
#include <sys/random.h>
3733
#endif
34+
3835
#ifdef HAVE_SYSCTL_ARND
3936
#include <sys/sysctl.h>
4037
#endif
@@ -284,23 +281,14 @@ void GetOSRand(unsigned char *ent32)
284281
RandFailure();
285282
}
286283
CryptReleaseContext(hProvider, 0);
287-
#elif defined(HAVE_SYS_GETRANDOM)
284+
#elif defined(HAVE_GETRANDOM)
288285
/* Linux. From the getrandom(2) man page:
289286
* "If the urandom source has been initialized, reads of up to 256 bytes
290287
* will always return as many bytes as requested and will not be
291288
* interrupted by signals."
292289
*/
293-
int rv = syscall(SYS_getrandom, ent32, NUM_OS_RANDOM_BYTES, 0);
294-
if (rv != NUM_OS_RANDOM_BYTES) {
295-
if (rv < 0 && errno == ENOSYS) {
296-
/* Fallback for kernel <3.17: the return value will be -1 and errno
297-
* ENOSYS if the syscall is not available, in that case fall back
298-
* to /dev/urandom.
299-
*/
300-
GetDevURandom(ent32);
301-
} else {
302-
RandFailure();
303-
}
290+
if (getrandom(ent32, NUM_OS_RANDOM_BYTES, 0) != NUM_OS_RANDOM_BYTES) {
291+
RandFailure();
304292
}
305293
#elif defined(__OpenBSD__)
306294
/* OpenBSD. From the arc4random(3) man page:

0 commit comments

Comments
 (0)