Skip to content

Commit 23d17f3

Browse files
robnbehlendorf
authored andcommitted
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 <[email protected]> Signed-off-by: Rob Norris <[email protected]> Closes #17861
1 parent 4d451ba commit 23d17f3

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

cmd/ztest.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8952,6 +8952,12 @@ main(int argc, char **argv)
89528952

89538953
libspl_init();
89548954

8955+
/*
8956+
* Force random_get_bytes() to use /dev/urandom in order to prevent
8957+
* ztest from needlessly depleting the system entropy pool.
8958+
*/
8959+
random_force_pseudo(B_TRUE);
8960+
89558961
if (!fd_data_str) {
89568962
process_options(argc, argv);
89578963

lib/libspl/include/sys/random.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
extern int random_get_bytes(uint8_t *ptr, size_t len);
3333
extern int random_get_pseudo_bytes(uint8_t *ptr, size_t len);
3434

35+
extern void random_force_pseudo(boolean_t onoff);
36+
3537
static __inline__ uint32_t
3638
random_in_range(uint32_t range)
3739
{

lib/libspl/random.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737

3838
static int random_fd = -1, urandom_fd = -1;
3939

40+
static boolean_t force_pseudo = B_FALSE;
41+
4042
void
4143
random_init(void)
4244
{
@@ -60,6 +62,12 @@ random_fini(void)
6062
urandom_fd = -1;
6163
}
6264

65+
void
66+
random_force_pseudo(boolean_t onoff)
67+
{
68+
force_pseudo = onoff;
69+
}
70+
6371
static int
6472
random_get_bytes_common(uint8_t *ptr, size_t len, int fd)
6573
{
@@ -81,6 +89,8 @@ random_get_bytes_common(uint8_t *ptr, size_t len, int fd)
8189
int
8290
random_get_bytes(uint8_t *ptr, size_t len)
8391
{
92+
if (force_pseudo)
93+
return (random_get_pseudo_bytes(ptr, len));
8494
return (random_get_bytes_common(ptr, len, random_fd));
8595
}
8696

0 commit comments

Comments
 (0)