Systemd/src/libsystemd
Zbigniew Jędrzejewski-Szmek f0d09059bd 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).
2017-06-28 10:32:02 -04:00
..
sd-bus sd-bus: make sure propagate all errors with vtable callbacks back to clients 2017-06-21 20:42:28 +02:00
sd-daemon Add sd_is_socket_sockaddr (#4885) 2016-12-14 11:51:26 +01:00
sd-device udev: use interface before the string that interface points to is freed by device_add_property_internal (#6105) 2017-06-18 11:31:30 +02:00
sd-event Fix includes (#5980) 2017-05-19 10:01:35 -04:00
sd-hwdb sd-hwdb: drop caching of search string 2016-11-30 15:51:08 -05:00
sd-id128 basic/random-util: do not fall back to /dev/urandom if getrandom() returns short 2017-06-28 10:32:02 -04:00
sd-login sd-login: sd_get_machine_names(): do not return -EINVAL when output parameter is NULL 2017-05-31 19:22:38 -04:00
sd-netlink sd-netlink: Make use of IN_SET (#5977) 2017-05-18 12:56:36 +02:00
sd-network Fix missing space in comments (#5439) 2017-02-24 18:14:02 +01:00
sd-path tree-wide: drop NULL sentinel from strjoin 2016-10-23 11:43:27 -04:00
sd-resolve sd-resolve: use close_many() (#3643) 2016-07-02 08:17:26 -07:00
sd-utf8 tree-wide: remove Emacs lines from all files 2016-02-10 13:41:57 +01:00
.gitignore libsystemd: remove list of symbols to export only in the future 2015-06-17 16:21:13 +02:00
libsystemd.pc.in Adjust pkgconfig files to point at rootlibdir (#4584) 2016-11-08 09:36:41 +01:00
libsystemd.sym Export sd_bus_message_append_ap. It is renamed to sd_bus_message_appendv to follow systemd naming conventions. (#5753) 2017-04-25 09:32:24 +02:00
Makefile libsystemd-bus: rename to libsystemd 2014-01-13 18:54:19 +01:00
meson.build meson: reindent all files with 8 spaces 2017-04-23 21:47:29 -04:00