From 23d17f35879ae499b5fa0f0976c7058ded633be7 Mon Sep 17 00:00:00 2001 From: Rob Norris Date: Tue, 11 Nov 2025 11:16:30 +1100 Subject: [PATCH] libspl/random: add switch to force pseudo-random numbers for all calls ztest wants to force all kernel random calls to use the pseudo-random generator (/dev/urandom), to avoid depleting the system entropy pool just for testing. Up until the previous commit, it did this by switching the path that the libzpool (now libspl) random API would use to get random data from; that is, it took advantage of an implementation detail. Now that that hole is closed to it, we need another method. This commit introduces that; a simple API call to enable/disable "force pseudo" mode. Sponsored-by: https://despairlabs.com/sponsor/ Reviewed-by: Brian Behlendorf Signed-off-by: Rob Norris Closes #17861 --- cmd/ztest.c | 6 ++++++ lib/libspl/include/sys/random.h | 2 ++ lib/libspl/random.c | 10 ++++++++++ 3 files changed, 18 insertions(+) diff --git a/cmd/ztest.c b/cmd/ztest.c index d94451906..dc8ac85b6 100644 --- a/cmd/ztest.c +++ b/cmd/ztest.c @@ -8952,6 +8952,12 @@ main(int argc, char **argv) libspl_init(); + /* + * Force random_get_bytes() to use /dev/urandom in order to prevent + * ztest from needlessly depleting the system entropy pool. + */ + random_force_pseudo(B_TRUE); + if (!fd_data_str) { process_options(argc, argv); diff --git a/lib/libspl/include/sys/random.h b/lib/libspl/include/sys/random.h index 27f2d4e3a..d11580829 100644 --- a/lib/libspl/include/sys/random.h +++ b/lib/libspl/include/sys/random.h @@ -32,6 +32,8 @@ extern int random_get_bytes(uint8_t *ptr, size_t len); extern int random_get_pseudo_bytes(uint8_t *ptr, size_t len); +extern void random_force_pseudo(boolean_t onoff); + static __inline__ uint32_t random_in_range(uint32_t range) { diff --git a/lib/libspl/random.c b/lib/libspl/random.c index 30281504f..c6f0ee7ae 100644 --- a/lib/libspl/random.c +++ b/lib/libspl/random.c @@ -37,6 +37,8 @@ static int random_fd = -1, urandom_fd = -1; +static boolean_t force_pseudo = B_FALSE; + void random_init(void) { @@ -60,6 +62,12 @@ random_fini(void) urandom_fd = -1; } +void +random_force_pseudo(boolean_t onoff) +{ + force_pseudo = onoff; +} + static int random_get_bytes_common(uint8_t *ptr, size_t len, int fd) { @@ -81,6 +89,8 @@ random_get_bytes_common(uint8_t *ptr, size_t len, int fd) int random_get_bytes(uint8_t *ptr, size_t len) { + if (force_pseudo) + return (random_get_pseudo_bytes(ptr, len)); return (random_get_bytes_common(ptr, len, random_fd)); }