random: simplify entropy debiting
authorJason A. Donenfeld <Jason@zx2c4.com>
Wed, 2 Feb 2022 12:30:03 +0000 (13:30 +0100)
committerJason A. Donenfeld <Jason@zx2c4.com>
Sun, 13 Feb 2022 23:46:07 +0000 (00:46 +0100)
commite881eb34f2ea127df24c32710a516e4ee6439cf8
tree865b13e8c4ea130b7860333ca89bad80efb98f34
parentd232fc449c656c2bbc4ddec117c3b7c75a9cfa56
random: simplify entropy debiting

Our pool is 256 bits, and we only ever use all of it or don't use it at
all, which is decided by whether or not it has at least 128 bits in it.
So we can drastically simplify the accounting and cmpxchg loop to do
exactly this.  While we're at it, we move the minimum bit size into a
constant so it can be shared between the two places where it matters.

The reason we want any of this is for the case in which an attacker has
compromised the current state, and then bruteforces small amounts of
entropy added to it. By demanding a particular minimum amount of entropy
be present before reseeding, we make that bruteforcing difficult.

Note that this rationale no longer includes anything about /dev/random
blocking at the right moment, since /dev/random no longer blocks (except
for at ~boot), but rather uses the crng. In a former life, /dev/random
was different and therefore required a more nuanced account(), but this
is no longer.

Behaviorally, nothing changes here. This is just a simplification of
the code.

Cc: Theodore Ts'o <tytso@mit.edu>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Reviewed-by: Eric Biggers <ebiggers@google.com>
Reviewed-by: Dominik Brodowski <linux@dominikbrodowski.net>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
drivers/char/random.c
include/trace/events/random.h