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 <behlendorf1@llnl.gov>
Signed-off-by: Rob Norris <robn@despairlabs.com>
Closes #17861
This commit is contained in:
Rob Norris 2025-11-11 11:16:30 +11:00 committed by Brian Behlendorf
parent 4d451bae8a
commit 23d17f3587
3 changed files with 18 additions and 0 deletions

View File

@ -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);

View File

@ -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)
{

View File

@ -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));
}