random: check for signal and try earlier when generating entropy
Jason A. Donenfeld
maintainerPATCH 1/1 random: check for signal and try earlier when generating entropy
We call try_to_generate_entropy() from wait_for_random_bytes(). wait_for_random_bytes() always uses wait_event_interruptible_timeout() when waiting, since it's called by userspace code in restartable contexts, where signals can pend. When entering a busy loop in try_to_generate_entropy(), we should therefore also check to see if any signals are pending, so that a process doesn't get stuck in that loop longer than expected. As well, there's no point in waiting for a full second before trying to generate entropy; instead do it in the opposite order, where we try to generate, and then go into the waitable.
Cc Dominik Brodowski <[email protected]>
Cc Theodore Ts'o <[email protected]>
Signed-off-by Jason A. Donenfeld <[email protected]>
--- drivers/char/random.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/char/random.c +3 -2
@@ -128,11 +128,12 @@ int wait_for_random_bytes(void)do {int ret; + +try_to_generate_entropy (); ret =wait_event_interruptible_timeout (crng_init_wait,crng_ready (),HZ );if (ret)return ret >0 ?0 : ret; -try_to_generate_entropy (); }while (!crng_ready ());return 0 ;
@@ -1374,7 +1375,7 @@ static void try_to_generate_entropy(void)return ;timer_setup_on_stack (&stack.timer, entropy_timer,0 ); -while (!crng_ready ()) { +while (!crng_ready () && !signal_pending (current)) {if (!timer_pending (&stack.timer))mod_timer (&stack.timer, jiffies +1 );mix_pool_bytes (&stack.cycles,sizeof (stack.cycles));
Signature · 1 line
2.35.1