1
0
mirror of https://github.com/systemd/systemd.git synced 2025-01-10 05:18:17 +03:00
Commit Graph

72 Commits

Author SHA1 Message Date
Lennart Poettering
f0b8ac9e0e tree-wide: port various users over to the new getxattr_at_bool() call 2023-10-26 09:27:18 +02:00
Frantisek Sumsal
dd741b8893 random-seed: terminate the option array
So we don't crash on invalid options:

$ build/systemd-random-seed --foo
Segmentation fault (core dumped)
2023-10-25 20:41:46 +01:00
Mike Yuan
e22c60a9d5
io-util: introduce loop_write_full that takes a timeout
Also drop do_poll as the use case is covered
by timeout.
2023-09-07 20:30:44 +08:00
Yu Watanabe
c8c7877167 meson: move declarations of random-seed, vconsole, and sysupdate 2023-08-01 15:54:45 +09:00
Frantisek Sumsal
8fc58b35ea random-seed: drop extraneous "we" 2023-03-08 16:28:37 +01:00
Lennart Poettering
29d487adb4 random-seed: don't refresh EFI random seed from random-seed.c anymore
The ESP is simply not mounted early enough for this. We want that the
regular random seed handling runs as early as we possibly could, but we
don't want to delay this until the ESP is actually mounted.

Hence, let's remove this from random-seed.c here. A follow-up commit
will then add this back in, in a separate service which just calls
"bootctl random-seed".

Effectively reverts: f913c784ad

Fixes: #25769
2023-01-04 15:18:10 +01:00
Lennart Poettering
a16c65f3c4 sha256: add helper than hashes a buffer *and* its size
We use this pattern all the time in order to thward extension attacks,
add a helper to make it shorter.
2023-01-04 15:18:10 +01:00
Zbigniew Jędrzejewski-Szmek
254d1313ae tree-wide: use -EBADF for fd initialization
-1 was used everywhere, but -EBADF or -EBADFD started being used in various
places. Let's make things consistent in the new style.

Note that there are two candidates:
EBADF 9 Bad file descriptor
EBADFD 77 File descriptor in bad state

Since we're initializating the fd, we're just assigning a value that means
"no fd yet", so it's just a bad file descriptor, and the first errno fits
better. If instead we had a valid file descriptor that became invalid because
of some operation or state change, the other errno would fit better.

In some places, initialization is dropped if unnecessary.
2022-12-19 15:00:57 +01:00
Jason A. Donenfeld
3daeef0884 random-seed: handle post-merge review nits
These are various misc things that came up after merging.
2022-11-17 12:22:27 +01:00
Jason A. Donenfeld
f913c784ad random-seed: refresh EFI boot seed when writing a new seed
Since this runs at shutdown to write a new seed, we should also keep the
bootloader's seed maximally fresh by doing the same. So we follow the
same pattern - hash some new random bytes with the old seed to make a
new seed. We let this fail without warning, because it's just an
opportunistic thing. If the user happens to have set up the random seed
with bootctl, and the RNG is initialized, then things should be fine. If
not, we create a new seed if systemd-boot is in use. And if not, then we
just don't do anything.
2022-11-14 15:24:00 +01:00
Franck Bui
a2f0dbb810 random-seed: make sure to load machine id even if the seed file is missing 2022-11-09 18:41:30 +01:00
Zbigniew Jędrzejewski-Szmek
d6b4d1c7c4 basic: move version() to build.h+c 2022-11-08 13:41:14 +01:00
Franck Bui
46e0b5dca7 random-seed: clarify one comment 2022-10-25 13:56:37 +02:00
Franck Bui
ea37e1edf9 random-seed: downgrade some messages
In these cases, we eat up the error and propagate success so we should log at
warning level only.
2022-10-25 12:16:22 +02:00
Franck Bui
3f6fbfe6f1 random_seed: minor improvement in run() 2022-10-25 12:16:22 +02:00
Franck Bui
d3fa881aa1 random-seed: split out run()
No functional change.
2022-10-25 12:16:22 +02:00
Franck Bui
4620c0af5d random-seed: no need to pass 'mode' argument when opening /dev/urandom
The open() call is not supposed to create /dev/urandom.

No functional change.
2022-10-25 12:16:22 +02:00
Franck Bui
205138d88a random-seed: make the logic to calculate the number of bytes read from the random seed file clearer
We want the size to lie within [/proc/sys/kernel/random/poolsize,RANDOM_POOL_SIZE_MAX]
interval. Let's make it more obvious.

Also move the logic in a dedicated function.
2022-10-25 12:16:22 +02:00
Franck Bui
0d0c6639d4 random-seed: use getopt()
It's not really necessary since systemd-random-seed is an internal tool for the
moment but this might change in future (to allow system installers to
initialize a random seed file for example).

Also introducing new options will be easier.
2022-10-25 12:16:18 +02:00
Franck Bui
141d1da021 random-seed: make one more use of random_write_entropy()
No functional change.
2022-10-21 14:49:53 +02:00
Franck Bui
249d31b00a random-seed: shorten a bit may_credit()
No functional change.
2022-10-21 14:49:01 +02:00
Lennart Poettering
00675c363f tree-wide: add ERRNO_IS_XATTR_ABSENT() helper
We check the same list of error codes on various xattr operations, and
we should on some more. Add a common helper for this purpose.
2022-10-10 16:00:15 +02:00
Lennart Poettering
00b4663813 random-seed: use SHA256_DIGEST_SIZE 2022-08-19 12:53:04 +02:00
Lennart Poettering
e5b90b30c2 random-seed: add missing %m in error message 2022-01-31 21:07:13 +00:00
Jason A. Donenfeld
06511ba559 random-seed: cleanup code nits
This incorporates various nits from the post-merge review on #21986.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2022-01-06 02:09:22 +09:00
Jason A. Donenfeld
da2862ef06 random-seed: hash together old seed and new seed before writing out file
If we're consuming an on-disk seed, we usually write out a new one after
consuming it. In that case, we might be at early boot and the randomness
could be rather poor, and the kernel doesn't guarantee that it'll use
the new randomness right away for us. In order to prevent the new
entropy from getting any worse, hash together the old seed and the new
seed, and replace the final bytes of the new seed with the hash output.
This way, entropy strictly increases and never regresses.

Fixes: https://github.com/systemd/systemd/issues/21983
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2022-01-04 22:47:56 +00:00
Lennart Poettering
bf819d3ac2 basic: split out sync() family of calls from fs-util.[ch] into new c/h file
No changes in code, just some splitting out.
2021-10-05 15:47:32 +02:00
Zbigniew Jędrzejewski-Szmek
111a3aae71 partition, random-seed, logind: fix log messages with %m
We would print "...: Success", which is not too terrible, but not pretty
either.
2021-04-14 16:19:54 +02:00
Yu Watanabe
d2acb93dc5 tree-wide: enable colorized logging for daemons when run in console
It may be useful when debugging daemons.
2021-02-01 01:13:44 +09:00
Yu Watanabe
db9ecf0501 license: LGPL-2.1+ -> LGPL-2.1-or-later 2020-11-09 13:23:58 +09:00
Lennart Poettering
4dd055f907 random-util: add common helper random_write_entropy() for crediting entropy to the kernel's pool 2020-06-24 15:33:27 +02:00
Lennart Poettering
97f1c6af8c random-seed: use ERRNO_IS_NOT_SUPPORTED() where appropriate 2020-06-24 15:32:38 +02:00
Romain Naour
123aeae206
random-seed: add missing header for GRND_NONBLOCK (#14988)
GRND_NONBLOCK has been introduced with the 3.17 kernel version [1]
while adding getrandom(2) system call.

The header missing_random.h is needed for random-seed.c when building
with old toolchain, such Sourcery CodeBench ARM 2014.05 (kernel headers
3.13).

Fixes:
https://gitlab.com/buildroot.org/buildroot/-/jobs/454255917

[1] https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git/commit/?id=c6e9d6f38894798696f23c8084ca7edbf16ee895
2020-03-02 14:48:21 +09:00
Yu Watanabe
455fa9610c tree-wide: drop string.h when string-util.h or friends are included 2019-11-04 00:30:32 +09:00
Yu Watanabe
f5947a5e92 tree-wide: drop missing.h 2019-10-31 17:57:03 +09:00
Lennart Poettering
26ded55709 random-seed: rework systemd-random-seed.service substantially
This makes two major changes to the way systemd-random-seed operates:

1. We now optionally credit entropy if this is configured (via an env
var). Previously we never would do that, with this change we still don't
by default, but it's possible to enable this if people acknowledge that
they shouldn't replicate an image with a contained random seed to
multiple systems. Note that in this patch crediting entropy is a boolean
thing (unlike in previous attempts such as #1062), where only a relative
amount of bits was credited. The simpler scheme implemented here should
be OK though as the random seeds saved to disk are now written only with
data from the kernel's entropy pool retrieved after the pool is fully
initialized. Specifically:

2. This makes systemd-random-seed.service a synchronization point for
kernel entropy pool initialization. It was already used like this, for
example by systemd-cryptsetup-generator's /dev/urandom passphrase
handling, with this change it explicitly operates like that (at least
systems which provide getrandom(), where we can support this). This
means services that rely on an initialized random pool should now place
After=systemd-random-seed.service and everything should be fine. Note
that with this change sysinit.target (and thus early boot) is NOT
systematically delayed until the entropy pool is initialized, i.e.
regular services need to add explicit ordering deps on this service if
they require an initialized random pool.

Fixes: #4271
Replaces: #10621 #4513
2019-07-25 18:30:06 +02:00
Lennart Poettering
c6127c3996 random-seed: drop falling back to O_WRONLY if O_RDWR on /dev/urandom doesn't work
There's no reason why writing should work if reading and writing
doesn't. Let's simplify this hence. /dev/urandom is generally an r/w
device, and everything else would be a serious system misconfiguration.
2019-07-25 18:20:50 +02:00
Lennart Poettering
15d961bf53 random-seed: reduce scope of variable 2019-07-25 18:20:50 +02:00
Lennart Poettering
3e155eba43 random-seed: move pool size determination to random-util.[ch]
That way we can reuse it elsewhere.
2019-07-25 18:16:46 +02:00
Lennart Poettering
4b3b5bc71b tree-wide: port various places over to use chmod_and_chown()
Doing this properly is hard, hence let's unify the code.
2019-05-24 15:07:55 +02:00
Lennart Poettering
5468d9af77 random-seed: don't use "label" version of mkdir_parents()
We don't load the selinux label database anyway, hence this is not going
to do anything at all. Let's simplify this hence and drop the explicit
request.

Also note that today SELinux can take the filename into account when
automatically deducing the label for a new file, hence even if this code
actually would have done something it is redundant today.
2019-03-07 15:10:06 +01:00
Zbigniew Jędrzejewski-Szmek
baaa35ad70 coccinelle: make use of SYNTHETIC_ERRNO
Ideally, coccinelle would strip unnecessary braces too. But I do not see any
option in coccinelle for this, so instead, I edited the patch text using
search&replace to remove the braces. Unfortunately this is not fully automatic,
in particular it didn't deal well with if-else-if-else blocks and ifdefs, so
there is an increased likelikehood be some bugs in such spots.

I also removed part of the patch that coccinelle generated for udev, where we
returns -1 for failure. This should be fixed independently.
2018-11-22 10:54:38 +01:00
Lennart Poettering
6bf3c61c57 log: introduce new helper call log_setup_service()
Let's reduce the common boilerplate and have a single setup function
used by all service code to setup logging.
2018-11-20 11:18:22 +01:00
Lennart Poettering
5e332028f2 util-lib: move main() definition macros to its own header file
This way, we can extend the macro a bit with stuff pulled in from other
headers without this affecting everything which pulls in macro.h, which
is one of our most basic headers.

This is just refactoring, no change in behaviour, in prepartion for
later changes.
2018-11-19 21:14:34 +01:00
Zbigniew Jędrzejewski-Szmek
72d0d7a628 random-seed: configure logging before use and define main through macro 2018-11-17 09:13:35 +01:00
Lennart Poettering
8ba12aef04 random-seed: write the machine ID into /dev/urandom as well
This is some extra protection for sloppy "golden master" systems, where
images are duplicated many times but the random seed is not
deleted (or reset for each copy). That golden master systems have to
reset /etc/machine-id is better known, and easier to notice (as having
the same ID will result in address conflicts and suchlike quite often).
Hence let's write the machine ID into /dev/urandom, in case it has been
initialized and unlikely the stored random seed has been provisioned
differently on each image.

Note that we don't credit the entropy either way, hence in the case
there's a cycle of a) generating the machine-id early at boot and b)
writing it back into /dev/urandom late at boot it shouldn't matter. It's
never going to make things worse, just in a few cases better.
2018-08-20 12:42:28 +02:00
Lennart Poettering
ac93390b03 random-seed: read the full seed file, even if it is larger than 512 byte
Previously, we'd only ever read 512 byte from the random seed file,
under the assumption we won't need more. With this change we'll read the
full file, even if it is larger.

The idea behind htis change is that people can dump additional data into the
random seed file offline if they like, and it can be low quality, and
we'll seed the pool with it anyway. Moreover, if people are paranoid and
want us to save/restore a bigger seed, it's easy to do: just truncate
the file to the right size and we'll save/restore as much in the future.

This also reworks the file a bit, introducing two clear if blocks that
load and that save the random seed, and that each are conditionalized
more carefully.
2018-08-06 21:21:51 +02:00
Lennart Poettering
0c69794138 tree-wide: remove Lennart's copyright lines
These lines are generally out-of-date, incomplete and unnecessary. With
SPDX and git repository much more accurate and fine grained information
about licensing and authorship is available, hence let's drop the
per-file copyright notice. Of course, removing copyright lines of others
is problematic, hence this commit only removes my own lines and leaves
all others untouched. It might be nicer if sooner or later those could
go away too, making git the only and accurate source of authorship
information.
2018-06-14 10:20:20 +02:00
Lennart Poettering
818bf54632 tree-wide: drop 'This file is part of systemd' blurb
This part of the copyright blurb stems from the GPL use recommendations:

https://www.gnu.org/licenses/gpl-howto.en.html

The concept appears to originate in times where version control was per
file, instead of per tree, and was a way to glue the files together.
Ultimately, we nowadays don't live in that world anymore, and this
information is entirely useless anyway, as people are very welcome to
copy these files into any projects they like, and they shouldn't have to
change bits that are part of our copyright header for that.

hence, let's just get rid of this old cruft, and shorten our codebase a
bit.
2018-06-14 10:20:20 +02:00
Zbigniew Jędrzejewski-Szmek
11a1589223 tree-wide: drop license boilerplate
Files which are installed as-is (any .service and other unit files, .conf
files, .policy files, etc), are left as is. My assumption is that SPDX
identifiers are not yet that well known, so it's better to retain the
extended header to avoid any doubt.

I also kept any copyright lines. We can probably remove them, but it'd nice to
obtain explicit acks from all involved authors before doing that.
2018-04-06 18:58:55 +02:00