Skip to content

Commit 0cec4a1

Browse files
rheniumnobu
authored andcommitted
Restore getrandom(2) path for Linux with glibc 2.36 or later
This is a follow-up to commit b120f5e (avoid fork-unsafe arc4random implementations, 2018-09-04). Avoid defining a no-op fill_random_bytes_syscall() if arc4random_buf(3) exists, but we are unsure if it is fork-safe. Check for other options instead. IOW, see if getrandom(2) is available. glibc 2.36, released in 2022, started to provide arc4random_buf(3) on Linux. This causes fill_random_bytes_syscall() to use neither of them and makes Random.urandom solely rely on getentropy(3) via fill_random_bytes_urandom(). While the glibc implementation is safe, I did not add it to the list because using getrandom(2) directly is preferable on Linux.
1 parent 1181a68 commit 0cec4a1

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

random.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -554,18 +554,16 @@ fill_random_bytes_syscall(void *seed, size_t size, int unused)
554554
}
555555
return 0;
556556
}
557-
#elif defined(HAVE_ARC4RANDOM_BUF)
557+
#elif defined(HAVE_ARC4RANDOM_BUF) && \
558+
((defined(__OpenBSD__) && OpenBSD >= 201411) || \
559+
(defined(__NetBSD__) && __NetBSD_Version__ >= 700000000) || \
560+
(defined(__FreeBSD__) && __FreeBSD_version >= 1200079))
561+
// [Bug #15039] arc4random_buf(3) should used only if we know it is fork-safe
558562
static int
559563
fill_random_bytes_syscall(void *buf, size_t size, int unused)
560564
{
561-
#if (defined(__OpenBSD__) && OpenBSD >= 201411) || \
562-
(defined(__NetBSD__) && __NetBSD_Version__ >= 700000000) || \
563-
(defined(__FreeBSD__) && __FreeBSD_version >= 1200079)
564565
arc4random_buf(buf, size);
565566
return 0;
566-
#else
567-
return -1;
568-
#endif
569567
}
570568
#elif defined(_WIN32)
571569

0 commit comments

Comments
 (0)