summaryrefslogtreecommitdiffstats
path: root/src/shared/libcrypt-util.c
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2022-05-24 15:09:20 +0200
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2022-05-31 09:20:52 +0200
commit87cb1ab6765d9ad9482b882b26e45a683162a078 (patch)
treea2af6e58c9044f281828148da47a863f9b91ad0f /src/shared/libcrypt-util.c
parentMerge pull request #23533 from yuwata/portable-remove-drop-in-configs (diff)
downloadsystemd-87cb1ab6765d9ad9482b882b26e45a683162a078.tar.xz
systemd-87cb1ab6765d9ad9482b882b26e45a683162a078.zip
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().
Diffstat (limited to 'src/shared/libcrypt-util.c')
-rw-r--r--src/shared/libcrypt-util.c2
1 files changed, 1 insertions, 1 deletions
diff --git a/src/shared/libcrypt-util.c b/src/shared/libcrypt-util.c
index 0d72032f53..81e6f1754c 100644
--- a/src/shared/libcrypt-util.c
+++ b/src/shared/libcrypt-util.c
@@ -76,7 +76,7 @@ int make_salt(char **ret) {
log_debug("Generating fallback salt for hash prefix: $6$");
/* Insist on the best randomness by setting RANDOM_BLOCK, this is about keeping passwords secret after all. */
- r = genuine_random_bytes(raw, sizeof(raw), RANDOM_BLOCK);
+ r = crypto_random_bytes(raw, sizeof(raw));
if (r < 0)
return r;