]> git.ipfire.org Git - thirdparty/systemd.git/commit - src/home/homework-fscrypt.c
Simplify random number selection
authorJason A. Donenfeld <Jason@zx2c4.com>
Tue, 24 May 2022 13:09:20 +0000 (15:09 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 31 May 2022 07:20:52 +0000 (09:20 +0200)
commit87cb1ab6765d9ad9482b882b26e45a683162a078
treea2af6e58c9044f281828148da47a863f9b91ad0f
parentfada4bdcaa57137c7245d3031deb08f3bcf7a8cc
Simplify random number selection

We currently have a convoluted and complex selection of which random
numbers to use. We can simplify this down to two functions that cover
all of our use cases:

1) Randomness for crypto: this one needs to wait until the RNG is
   initialized. So it uses getrandom(0). If that's not available, it
   polls on /dev/random, and then reads from /dev/urandom. This function
   returns whether or not it was successful, as before.

2) Randomness for other things: this one uses getrandom(GRND_INSECURE).
   If it's not available it uses getrandom(GRND_NONBLOCK). And if that
   would block, then it falls back to /dev/urandom. And if /dev/urandom
   isn't available, it uses the fallback code. It never fails and
   doesn't return a value.

These two cases match all the uses of randomness inside of systemd.

I would prefer to make both of these return void, and get rid of the
fallback code, and simply assert in the incredibly unlikely case that
/dev/urandom doesn't exist. But Luca disagrees, so this commit attempts
to instead keep case (1) returning a return value, which all the callers
already check, and fix the fallback code in (2) to be less bad than
before.

For the less bad fallback code for (2), we now use auxval and some
timestamps, together with various counters representing the invocation,
hash it all together and provide the output. Provided that AT_RANDOM is
secure, this construction is probably okay too, though notably it
doesn't have any forward secrecy. Fortunately, it's only used by
random_bytes() and not by crypto_random_bytes().
20 files changed:
src/basic/random-util.c
src/basic/random-util.h
src/basic/recovery-key.c
src/boot/bootctl.c
src/cryptenroll/cryptenroll-pkcs11.c
src/home/homectl-pkcs11.c
src/home/homework-fscrypt.c
src/home/homework-luks.c
src/journal/journalctl.c
src/libsystemd/sd-id128/sd-id128.c
src/partition/repart.c
src/shared/creds-util.c
src/shared/libcrypt-util.c
src/shared/libfido2-util.c
src/shared/tpm2-util.c
src/test/test-alloc-util.c
src/test/test-firewall-util.c
src/test/test-random-util.c
src/udev/net/link-config.c
src/udev/scsi_id/scsi_serial.c