summaryrefslogtreecommitdiff
path: root/src/firstboot
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2017-06-25 17:09:05 -0400
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2017-06-28 10:32:02 -0400
commitf0d09059bd4a195840f7c79943be942b240d5500 (patch)
treeee45eaf6c7d2e86f8eda4f0a12c81d2b4c7e201e /src/firstboot
parent6a06b1a5d9d6a05404ea6ace739d39e6256968c3 (diff)
downloadsystemd-f0d09059bd4a195840f7c79943be942b240d5500.tar.gz
basic/random-util: do not fall back to /dev/urandom if getrandom() returns short
During early boot, we'd call getrandom(), and immediately fall back to reading from /dev/urandom unless we got the full requested number of bytes. Those two sources are the same, so the most likely result is /dev/urandom producing some pseudorandom numbers for us, complaining widely on the way. Let's change our behaviour to be more conservative: - if the numbers are only used to initialize a hash table, a short read is OK, we don't really care if we get the first part of the seed truly random and then some pseudorandom bytes. So just do that and return "success". - if getrandom() returns -EAGAIN, fall back to rand() instead of querying /dev/urandom again. The idea with those two changes is to avoid generating a warning about reading from an /dev/urandom when the kernel doesn't have enough entropy. - only in the cases where we really need to make the best effort possible (sd_id128_randomize and firstboot password hashing), fall back to /dev/urandom. When calling getrandom(), drop the checks whether the argument fits in an int — getrandom() should do that for us already, and we call it with small arguments only anyway. Note that this does not really change the (relatively high) number of random bytes we request from the kernel. On my laptop, during boot, PID 1 and all other processes using this code through libsystemd request: 74780 bytes with high_quality_required == false 464 bytes with high_quality_required == true and it does not eliminate reads from /dev/urandom completely. If the kernel was short on entropy and getrandom() would fail, we would fall back to /dev/urandom for those 464 bytes. When falling back to /dev/urandom, don't lose the short read we already got, and just read the remaining bytes. If getrandom() syscall is not available, we fall back to /dev/urandom same as before. Fixes #4167 (possibly partially, let's see).
Diffstat (limited to 'src/firstboot')
-rw-r--r--src/firstboot/firstboot.c2
1 files changed, 1 insertions, 1 deletions
diff --git a/src/firstboot/firstboot.c b/src/firstboot/firstboot.c
index bc16290c72..b3578d3e16 100644
--- a/src/firstboot/firstboot.c
+++ b/src/firstboot/firstboot.c
@@ -572,7 +572,7 @@ static int process_root_password(void) {
if (!arg_root_password)
return 0;
- r = dev_urandom(raw, 16);
+ r = acquire_random_bytes(raw, 16, true);
if (r < 0)
return log_error_errno(r, "Failed to get salt: %m");