mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Jason A. Donenfeld" <Jason@zx2c4.com>
To: linux-kernel@vger.kernel.org
Cc: "Jason A. Donenfeld" <Jason@zx2c4.com>,
	Dominik Brodowski <linux@dominikbrodowski.net>,
	Theodore Ts'o <tytso@mit.edu>
Subject: [PATCH] random: check for signal and try earlier when generating entropy
Date: Tue,  8 Mar 2022 10:18:49 -0700	[thread overview]
Message-ID: <20220308171849.242534-1-Jason@zx2c4.com> (raw)

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 <linux@dominikbrodowski.net>
Cc: Theodore Ts'o <tytso@mit.edu>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
 drivers/char/random.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/char/random.c b/drivers/char/random.c
index edb5b06544da..4c5f515b6080 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -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));
-- 
2.35.1


                 reply	other threads:[~2022-03-08 17:19 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220308171849.242534-1-Jason@zx2c4.com \
    --to=jason@zx2c4.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@dominikbrodowski.net \
    --cc=tytso@mit.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®