diff mbox series

[v2,4/4] random: make credit_entropy_bits() always safe

Message ID 20220204135325.8327-5-Jason@zx2c4.com
State Superseded
Headers show
Series random: use computational hash for entropy extraction, and related fixes | expand

Commit Message

Jason A. Donenfeld Feb. 4, 2022, 1:53 p.m. UTC
This is called from various hwgenerator drivers, so rather than having
one "safe" version for userspace and one "unsafe" version for the
kernel, just make everything safe; the checks are cheap and sensible to
have anyway.

Cc: Dominik Brodowski <linux@dominikbrodowski.net>
Reported-by: Sultan Alsawaf <sultan@kerneltoast.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
 drivers/char/random.c | 29 +++++++++--------------------
 1 file changed, 9 insertions(+), 20 deletions(-)

Comments

Dominik Brodowski Feb. 5, 2022, 7:21 a.m. UTC | #1
Am Fri, Feb 04, 2022 at 02:53:25PM +0100 schrieb Jason A. Donenfeld:
> This is called from various hwgenerator drivers, so rather than having
> one "safe" version for userspace and one "unsafe" version for the
> kernel, just make everything safe; the checks are cheap and sensible to
> have anyway.
> 
> Cc: Dominik Brodowski <linux@dominikbrodowski.net>
> Reported-by: Sultan Alsawaf <sultan@kerneltoast.com>
> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>

	Reviewed-by: Dominik Brodowski <linux@dominikbrodowski.net>

Thanks,
	Dominik
diff mbox series

Patch

diff --git a/drivers/char/random.c b/drivers/char/random.c
index b4798a3f7bf6..455615ac169a 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -453,18 +453,15 @@  static void process_random_ready_list(void)
 	spin_unlock_irqrestore(&random_ready_list_lock, flags);
 }
 
-/*
- * Credit (or debit) the entropy store with n bits of entropy.
- * Use credit_entropy_bits_safe() if the value comes from userspace
- * or otherwise should be checked for extreme values.
- */
 static void credit_entropy_bits(int nbits)
 {
 	int entropy_count, orig;
 
-	if (!nbits)
+	if (nbits <= 0)
 		return;
 
+	nbits = min(nbits, POOL_BITS);
+
 	do {
 		entropy_count = orig = READ_ONCE(input_pool.entropy_count);
 		entropy_count = min(POOL_BITS, entropy_count + nbits);
@@ -476,18 +473,6 @@  static void credit_entropy_bits(int nbits)
 		crng_reseed(&primary_crng, true);
 }
 
-static int credit_entropy_bits_safe(int nbits)
-{
-	if (nbits < 0)
-		return -EINVAL;
-
-	/* Cap the value to avoid overflows */
-	nbits = min(nbits, POOL_BITS);
-
-	credit_entropy_bits(nbits);
-	return 0;
-}
-
 /*********************************************************************
  *
  * CRNG using CHACHA20
@@ -1534,7 +1519,10 @@  static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
 			return -EPERM;
 		if (get_user(ent_count, p))
 			return -EFAULT;
-		return credit_entropy_bits_safe(ent_count);
+		if (ent_count < 0)
+			return -EINVAL;
+		credit_entropy_bits(ent_count);
+		return 0;
 	case RNDADDENTROPY:
 		if (!capable(CAP_SYS_ADMIN))
 			return -EPERM;
@@ -1547,7 +1535,8 @@  static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
 		retval = write_pool((const char __user *)p, size);
 		if (retval < 0)
 			return retval;
-		return credit_entropy_bits_safe(ent_count);
+		credit_entropy_bits(ent_count);
+		return 0;
 	case RNDZAPENTCNT:
 	case RNDCLEARPOOL:
 		/*