Skip to content

Commit 09351f5

Browse files
committed
Merge bitcoin/bitcoin#27699: random: drop syscall wrapper usage for getrandom()
5228223 ci: remove MSAN getrandom syscall workaround (fanquake) d5e0691 random: switch to using getrandom() directly (fanquake) c2ba3f5 random: add [[maybe_unused]] to GetDevURandom (fanquake) c13c97d random: getentropy on macOS does not need unistd.h (fanquake) Pull request description: This requires a linux kernel of `3.17`+, 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, going 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: > * The getentropy and getrandom functions, and the <sys/random.h> header file have been added. and we already require `2.27` or later. All that being said, I don't think you would encounter a current day (+~6 months from now) system, running with kernel headers older than 3.17 (released 2014) but also having a glibc of 2.27+ (released 2018)? Removing this (our only) use of `syscall()` also means we can drop a workaround in our MSAN jobs. If this is merged, I'll drop the [same workaround in oss-fuzz](https://github.com/google/oss-fuzz/blob/25946a544856413d31d9cbb3a366a4aef5a8fd60/projects/bitcoin-core/build.sh#L49-L56). ACKs for top commit: josibake: ACK bitcoin/bitcoin@5228223 hebasto: ACK 5228223, I've tested build system changes on Ubuntu 22.04 and macOS Monterey 12.6.6 (x86_64). Tree-SHA512: cc978e08510c461b875ca8c08ae176b4519fa1108f0efd74dcb7474518945357e0184e54423282c9a496de195e4ddc3e221ee78623bd63e24c50cc86acdf32e2
2 parents ad7819d + 5228223 commit 09351f5

File tree

4 files changed

+14
-44
lines changed

4 files changed

+14
-44
lines changed

ci/test/06_script_b.sh

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,6 @@ cd "${BASE_BUILD_DIR}/bitcoin-$HOST"
101101

102102
bash -c "./configure --cache-file=../config.cache $BITCOIN_CONFIG_ALL $BITCOIN_CONFIG" || ( (cat config.log) && false)
103103

104-
if [[ ${USE_MEMORY_SANITIZER} == "true" ]]; then
105-
# MemorySanitizer (MSAN) does not support tracking memory initialization done by
106-
# using the Linux getrandom syscall. Avoid using getrandom by undefining
107-
# HAVE_SYS_GETRANDOM. See https://github.com/google/sanitizers/issues/852 for
108-
# details.
109-
grep -v HAVE_SYS_GETRANDOM src/config/bitcoin-config.h > src/config/bitcoin-config.h.tmp && mv src/config/bitcoin-config.h.tmp src/config/bitcoin-config.h
110-
fi
111-
112104
if [[ "${RUN_TIDY}" == "true" ]]; then
113105
MAYBE_BEAR="bear --config src/.bear-tidy-config"
114106
MAYBE_TOKEN="--"

configure.ac

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,17 +1170,16 @@ 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

1182-
AC_MSG_CHECKING([for getentropy via random.h])
1183-
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <unistd.h>
1181+
AC_MSG_CHECKING([for getentropy via sys/random.h])
1182+
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
11841183
#include <sys/random.h>]],
11851184
[[ getentropy(nullptr, 32) ]])],
11861185
[ AC_MSG_RESULT([yes]); AC_DEFINE([HAVE_GETENTROPY_RAND], [1], [Define this symbol if the BSD getentropy system call is available with sys/random.h]) ],

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: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +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)
36-
#include <unistd.h>
31+
#if defined(HAVE_GETRANDOM) || (defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX))
3732
#include <sys/random.h>
3833
#endif
34+
3935
#ifdef HAVE_SYSCTL_ARND
4036
#include <sys/sysctl.h>
4137
#endif
@@ -252,7 +248,7 @@ static void Strengthen(const unsigned char (&seed)[32], SteadyClock::duration du
252248
/** Fallback: get 32 bytes of system entropy from /dev/urandom. The most
253249
* compatible way to get cryptographic randomness on UNIX-ish platforms.
254250
*/
255-
static void GetDevURandom(unsigned char *ent32)
251+
[[maybe_unused]] static void GetDevURandom(unsigned char *ent32)
256252
{
257253
int f = open("/dev/urandom", O_RDONLY);
258254
if (f == -1) {
@@ -285,23 +281,14 @@ void GetOSRand(unsigned char *ent32)
285281
RandFailure();
286282
}
287283
CryptReleaseContext(hProvider, 0);
288-
#elif defined(HAVE_SYS_GETRANDOM)
284+
#elif defined(HAVE_GETRANDOM)
289285
/* Linux. From the getrandom(2) man page:
290286
* "If the urandom source has been initialized, reads of up to 256 bytes
291287
* will always return as many bytes as requested and will not be
292288
* interrupted by signals."
293289
*/
294-
int rv = syscall(SYS_getrandom, ent32, NUM_OS_RANDOM_BYTES, 0);
295-
if (rv != NUM_OS_RANDOM_BYTES) {
296-
if (rv < 0 && errno == ENOSYS) {
297-
/* Fallback for kernel <3.17: the return value will be -1 and errno
298-
* ENOSYS if the syscall is not available, in that case fall back
299-
* to /dev/urandom.
300-
*/
301-
GetDevURandom(ent32);
302-
} else {
303-
RandFailure();
304-
}
290+
if (getrandom(ent32, NUM_OS_RANDOM_BYTES, 0) != NUM_OS_RANDOM_BYTES) {
291+
RandFailure();
305292
}
306293
#elif defined(__OpenBSD__)
307294
/* OpenBSD. From the arc4random(3) man page:
@@ -311,16 +298,10 @@ void GetOSRand(unsigned char *ent32)
311298
The function call is always successful.
312299
*/
313300
arc4random_buf(ent32, NUM_OS_RANDOM_BYTES);
314-
// Silence a compiler warning about unused function.
315-
(void)GetDevURandom;
316301
#elif defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX)
317-
/* getentropy() is available on macOS 10.12 and later.
318-
*/
319302
if (getentropy(ent32, NUM_OS_RANDOM_BYTES) != 0) {
320303
RandFailure();
321304
}
322-
// Silence a compiler warning about unused function.
323-
(void)GetDevURandom;
324305
#elif defined(HAVE_SYSCTL_ARND)
325306
/* FreeBSD, NetBSD and similar. It is possible for the call to return less
326307
* bytes than requested, so need to read in a loop.
@@ -334,8 +315,6 @@ void GetOSRand(unsigned char *ent32)
334315
}
335316
have += len;
336317
} while (have < NUM_OS_RANDOM_BYTES);
337-
// Silence a compiler warning about unused function.
338-
(void)GetDevURandom;
339318
#else
340319
/* Fall back to /dev/urandom if there is no specific method implemented to
341320
* get system entropy for this OS.

0 commit comments

Comments
 (0)