All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] random: use hwgenerator randomness more frequently at early boot
@ 2022-09-04 10:17 Dominik Brodowski
  2022-09-07 13:02 ` Jason A. Donenfeld
  2022-09-20 13:54 ` [PATCH v2] " Jason A. Donenfeld
  0 siblings, 2 replies; 5+ messages in thread
From: Dominik Brodowski @ 2022-09-04 10:17 UTC (permalink / raw)
  To: linux-kernel, Herbert Xu, Jason A . Donenfeld; +Cc: linux-crypto

Mix in randomness from hw-rng sources more frequently during early
boot, approximately once for every rng reseed.

Cc: Jason A. Donenfeld <Jason@zx2c4.com>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: Dominik Brodowski <linux@dominikbrodowski.net>
---

This patch is currently based on
	[PATCH] random / hw_random: core: start hwrng kthread also for untrusted sources

Jason, if you prefer that this patch can be applied first (as it
makes sense also independently of the other patch), this ordering
should be trivial to change.

 drivers/char/random.c | 32 +++++++++++++++++++++-----------
 1 file changed, 21 insertions(+), 11 deletions(-)

diff --git a/drivers/char/random.c b/drivers/char/random.c
index b360ed4ece03..5559351f1259 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -260,25 +260,35 @@ static void crng_fast_key_erasure(u8 key[CHACHA_KEY_SIZE],
 }
 
 /*
- * Return whether the crng seed is considered to be sufficiently old
- * that a reseeding is needed. This happens if the last reseeding
- * was CRNG_RESEED_INTERVAL ago, or during early boot, at an interval
- * proportional to the uptime.
+ * Return the interval to the next regular reseed of the crng. This
+ * equals CRNG_RESEED_INTERVAL, or during early boot, an interval
+ * proportional to the uptime,
  */
-static bool crng_has_old_seed(void)
+static unsigned int crng_interval(void)
 {
 	static bool early_boot = true;
-	unsigned long interval = CRNG_RESEED_INTERVAL;
 
 	if (unlikely(READ_ONCE(early_boot))) {
 		time64_t uptime = ktime_get_seconds();
 		if (uptime >= CRNG_RESEED_INTERVAL / HZ * 2)
 			WRITE_ONCE(early_boot, false);
 		else
-			interval = max_t(unsigned int, CRNG_RESEED_START_INTERVAL,
-					 (unsigned int)uptime / 2 * HZ);
+			return max_t(unsigned int, CRNG_RESEED_START_INTERVAL,
+				     (unsigned int)uptime / 2 * HZ);
 	}
-	return time_is_before_jiffies(READ_ONCE(base_crng.birth) + interval);
+
+	return CRNG_RESEED_INTERVAL;
+}
+
+/*
+ * Return whether the crng seed is considered to be sufficiently old
+ * that a reseeding is needed. This happens if the last reseeding
+ * was CRNG_RESEED_INTERVAL ago, or during early boot, at an interval
+ * proportional to the uptime.
+ */
+static bool crng_has_old_seed(void)
+{
+	return time_is_before_jiffies(READ_ONCE(base_crng.birth) + crng_interval());
 }
 
 /*
@@ -866,11 +876,11 @@ void add_hwgenerator_randomness(const void *buf, size_t len, size_t entropy)
 	credit_init_bits(entropy);
 
 	/*
-	 * Throttle writing to once every CRNG_RESEED_INTERVAL, unless
+	 * Throttle writing to once every reseed interval, unless
 	 * we're not yet initialized or this source isn't trusted.
 	 */
 	if (!kthread_should_stop() && (crng_ready() || !entropy))
-		schedule_timeout_interruptible(CRNG_RESEED_INTERVAL);
+		schedule_timeout_interruptible(crng_interval());
 }
 EXPORT_SYMBOL_GPL(add_hwgenerator_randomness);
 
-- 
2.37.3


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] random: use hwgenerator randomness more frequently at early boot
  2022-09-04 10:17 [PATCH] random: use hwgenerator randomness more frequently at early boot Dominik Brodowski
@ 2022-09-07 13:02 ` Jason A. Donenfeld
  2022-09-07 13:04   ` Dominik Brodowski
  2022-09-20 13:54 ` [PATCH v2] " Jason A. Donenfeld
  1 sibling, 1 reply; 5+ messages in thread
From: Jason A. Donenfeld @ 2022-09-07 13:02 UTC (permalink / raw)
  To: Dominik Brodowski; +Cc: linux-kernel, Herbert Xu, linux-crypto

Hi Dominik,

On Sun, Sep 04, 2022 at 12:17:53PM +0200, Dominik Brodowski wrote:
> Mix in randomness from hw-rng sources more frequently during early
> boot, approximately once for every rng reseed.

Nice idea.

> Jason, if you prefer that this patch can be applied first (as it
> makes sense also independently of the other patch), this ordering
> should be trivial to change.

I'll apply this patch first, yea, since it's independent.

> -static bool crng_has_old_seed(void)
> +static unsigned int crng_interval(void)

crng_reseed_interval() instead?

Jason

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] random: use hwgenerator randomness more frequently at early boot
  2022-09-07 13:02 ` Jason A. Donenfeld
@ 2022-09-07 13:04   ` Dominik Brodowski
  0 siblings, 0 replies; 5+ messages in thread
From: Dominik Brodowski @ 2022-09-07 13:04 UTC (permalink / raw)
  To: Jason A. Donenfeld; +Cc: linux-kernel, Herbert Xu, linux-crypto

Hi Jason,

Am Wed, Sep 07, 2022 at 03:02:34PM +0200 schrieb Jason A. Donenfeld:
> Hi Dominik,
> 
> On Sun, Sep 04, 2022 at 12:17:53PM +0200, Dominik Brodowski wrote:
> > Mix in randomness from hw-rng sources more frequently during early
> > boot, approximately once for every rng reseed.
> 
> Nice idea.
> 
> > Jason, if you prefer that this patch can be applied first (as it
> > makes sense also independently of the other patch), this ordering
> > should be trivial to change.
> 
> I'll apply this patch first, yea, since it's independent.
> 
> > -static bool crng_has_old_seed(void)
> > +static unsigned int crng_interval(void)
> 
> crng_reseed_interval() instead?

Sounds good. Thanks!

	Dominik

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2] random: use hwgenerator randomness more frequently at early boot
  2022-09-04 10:17 [PATCH] random: use hwgenerator randomness more frequently at early boot Dominik Brodowski
  2022-09-07 13:02 ` Jason A. Donenfeld
@ 2022-09-20 13:54 ` Jason A. Donenfeld
  2022-09-22 13:35   ` Dominik Brodowski
  1 sibling, 1 reply; 5+ messages in thread
From: Jason A. Donenfeld @ 2022-09-20 13:54 UTC (permalink / raw)
  To: linux-kernel, linux-crypto; +Cc: Dominik Brodowski, Jason A . Donenfeld

From: Dominik Brodowski <linux@dominikbrodowski.net>

Mix in randomness from hw-rng sources more frequently during early
boot, approximately once for every rng reseed.

Signed-off-by: Dominik Brodowski <linux@dominikbrodowski.net>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
Dominik - I incorporated the refactoring mentioned on the mailing list.
Hopefully this is okay with you. Holler if I got something wrong! -Jason

 drivers/char/random.c | 22 ++++++++++------------
 1 file changed, 10 insertions(+), 12 deletions(-)

diff --git a/drivers/char/random.c b/drivers/char/random.c
index c8cc23515568..16e0c5f6cf2f 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -260,25 +260,23 @@ static void crng_fast_key_erasure(u8 key[CHACHA_KEY_SIZE],
 }
 
 /*
- * Return whether the crng seed is considered to be sufficiently old
- * that a reseeding is needed. This happens if the last reseeding
- * was CRNG_RESEED_INTERVAL ago, or during early boot, at an interval
+ * Return the interval until the next reseeding, which is normally
+ * CRNG_RESEED_INTERVAL, but during early boot, it is at an interval
  * proportional to the uptime.
  */
-static bool crng_has_old_seed(void)
+static unsigned int crng_reseed_interval(void)
 {
 	static bool early_boot = true;
-	unsigned long interval = CRNG_RESEED_INTERVAL;
 
 	if (unlikely(READ_ONCE(early_boot))) {
 		time64_t uptime = ktime_get_seconds();
 		if (uptime >= CRNG_RESEED_INTERVAL / HZ * 2)
 			WRITE_ONCE(early_boot, false);
 		else
-			interval = max_t(unsigned int, CRNG_RESEED_START_INTERVAL,
-					 (unsigned int)uptime / 2 * HZ);
+			return max_t(unsigned int, CRNG_RESEED_START_INTERVAL,
+				     (unsigned int)uptime / 2 * HZ);
 	}
-	return time_is_before_jiffies(READ_ONCE(base_crng.birth) + interval);
+	return CRNG_RESEED_INTERVAL;
 }
 
 /*
@@ -320,7 +318,7 @@ static void crng_make_state(u32 chacha_state[CHACHA_STATE_WORDS],
 	 * If the base_crng is old enough, we reseed, which in turn bumps the
 	 * generation counter that we check below.
 	 */
-	if (unlikely(crng_has_old_seed()))
+	if (unlikely(time_is_before_jiffies(READ_ONCE(base_crng.birth) + crng_reseed_interval())))
 		crng_reseed();
 
 	local_lock_irqsave(&crngs.lock, flags);
@@ -866,11 +864,11 @@ void add_hwgenerator_randomness(const void *buf, size_t len, size_t entropy)
 	credit_init_bits(entropy);
 
 	/*
-	 * Throttle writing to once every CRNG_RESEED_INTERVAL, unless
-	 * we're not yet initialized.
+	 * Throttle writing to once every reseed interval, unless we're not yet
+	 * initialized.
 	 */
 	if (!kthread_should_stop() && crng_ready())
-		schedule_timeout_interruptible(CRNG_RESEED_INTERVAL);
+		schedule_timeout_interruptible(crng_reseed_interval());
 }
 EXPORT_SYMBOL_GPL(add_hwgenerator_randomness);
 
-- 
2.37.3


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] random: use hwgenerator randomness more frequently at early boot
  2022-09-20 13:54 ` [PATCH v2] " Jason A. Donenfeld
@ 2022-09-22 13:35   ` Dominik Brodowski
  0 siblings, 0 replies; 5+ messages in thread
From: Dominik Brodowski @ 2022-09-22 13:35 UTC (permalink / raw)
  To: Jason A. Donenfeld; +Cc: linux-kernel, linux-crypto

Am Tue, Sep 20, 2022 at 03:54:58PM +0200 schrieb Jason A. Donenfeld:
> From: Dominik Brodowski <linux@dominikbrodowski.net>
> 
> Mix in randomness from hw-rng sources more frequently during early
> boot, approximately once for every rng reseed.
> 
> Signed-off-by: Dominik Brodowski <linux@dominikbrodowski.net>
> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
> ---
> Dominik - I incorporated the refactoring mentioned on the mailing list.
> Hopefully this is okay with you. Holler if I got something wrong! -Jason

Thanks for picking it up! Looks good.

Thanks,
	Dominik

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-09-22 14:02 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-04 10:17 [PATCH] random: use hwgenerator randomness more frequently at early boot Dominik Brodowski
2022-09-07 13:02 ` Jason A. Donenfeld
2022-09-07 13:04   ` Dominik Brodowski
2022-09-20 13:54 ` [PATCH v2] " Jason A. Donenfeld
2022-09-22 13:35   ` Dominik Brodowski

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.