Commit Graph

1073 Commits

Author SHA1 Message Date
Lennart Poettering
a6edc20fa6 Merge pull request #26003 from keszybz/efi-ifdefs
Fix one compile-time conditional and refactor SD_BOOT definition
2023-01-10 18:28:10 +01:00
Ludwig Nussel
2e76ca79b3 bootctl: honor $KERNEL_INSTALL_CONF_ROOT
Honor $KERNEL_INSTALL_CONF_ROOT for reading config files, as
kernel-install does.
2023-01-10 15:17:07 +01:00
Zbigniew Jędrzejewski-Szmek
493cd5034c meson: use 0|1 for SD_BOOT
We converted to not using #ifdef for most of our defines because the syntax is
nicer and we are protected against typos and can set -Werror=undef. Let's do
the same for SD_BOOT. The define is nicely hidden in build.h for normal builds,
and for EFI builds we were already setting SD_BOOT on the commandline.
2023-01-10 14:25:57 +01:00
Jan Janssen
3f92dc2fd4 boot: Simplify object erasure
This erase_obj() machinery looks like voodoo and creates an awful lot of
noise as soon as we get back to building with -O0. We can do this in a
more simple way by introducing a struct that holds the information we
need on cleanup. When building with optimization enabled, all this gets
inlined and the eraser vanishes.
2023-01-09 18:58:54 +01:00
Lennart Poettering
a3310012e8 Merge pull request #25979 from yuwata/coverity-fixes
Coverity fixes
2023-01-09 09:56:04 +01:00
Jan Janssen
d84bdadb43 boot: Remove option TPM PCR compat option
It says remove in 2023; happy to oblige.
2023-01-09 09:52:55 +01:00
Yu Watanabe
3d5af17045 bootctl: 'tmp' is always non-NULL
Fixes CID#1502330.
2023-01-09 11:00:56 +09:00
Yu Watanabe
a42909e60c bootctl: fix indentation 2023-01-09 10:58:10 +09:00
Ludwig Nussel
8109b2908d bootctl: print entry token as well 2023-01-05 21:45:18 +01:00
Lennart Poettering
54978e3f3b bootctl: split out setting of system token into function of its own
Let's break a huge function in two. No code change, just some
refactoring.
2023-01-04 15:18:10 +01:00
Lennart Poettering
5019b0cb15 bootctl: downgrade graceful messages to LOG_NOTICE 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
Lennart Poettering
114172fbe7 bootctl: when updating EFI random seed file, hash old seed with new one
Let's not regress in entropy in any case.

This does what f913c784ad also does.
2023-01-04 15:18:10 +01:00
Lennart Poettering
60315d5953 bootctl: properly sync fs before/after moving random seed file into place
Let's do a careful, focussed sync at the right places instead of a
blanket sync at the end. After all we want to run this on every boot
soon.
2023-01-04 15:18:10 +01:00
Lennart Poettering
6b97b267bf bootctl: rework random seed logic to use open_mkdir_at() and openat()
This doesn't really fix anything, but in general we should put stronger
emphasis on operating via dir fds rather than paths more (in particular
when writing files as opposed to consuming them).

No real change in behaviour.
2023-01-04 15:18:10 +01:00
Lennart Poettering
e425849e99 bootctl: fix errno logging 2023-01-04 15:15:58 +01:00
Lennart Poettering
fd1fec534e efi: skip Read() calls with zero sizes
Let's avoid calling Read() with zero-sized buffer, to avoid needless firmware
quirkiness.

See: #25911
2023-01-03 23:12:36 +01:00
Jan Janssen
f151abb0e5 Revert "boot: Use EFI_BOOT_MANAGER_POLICY_PROTOCOL to connect console devices"
This reverts commit b99bf58118.

It seems that using this protocol on some firmwares to forcibly
initialize console devices may break handles (already opened file
handles and the device handle itself) that we rely on to access the
boot filesystem, making it impossible to load the selected entry.

It might be possible to get a new handle by querying for the device
handle by using its device path after calling into this protocol, but
this is untested. The firmware might also be so buggy that accessing
devices after using this protocol is impossible.

It seems prudent to revert this for now until some reliable way is found
to initialize console devices without introducing huge boot delays. Any
users on firmware where console devices cannot be accessed would have to
rely on disabling fastboot.

Fixes: #25737, #25846
2023-01-03 17:12:33 +01:00
Jia Zhang
486cf22c35 boot: don't convert the trailing newline in mangle_stub_cmdline()
It is pretty convenient to add .cmdline using /proc/cmdline like
this:
  --add-section .cmdline=/proc/cmdline --change-section-vma .cmdline=0x25000

However, it always returns a trailing newline, and stub will
convert it to a whitespace by mangle_stub_cmdline() in next boot.
Thus the resulting /proc/cmdline would contain a trailing
whitespace. When /proc/cmdline is used to generate .cmdline again,
the resulting UKI is mangled.

To address this kind of inconvenience, mangle_stub_cmdline() should
skip converting the trailing newline, and try to chomp all the
trailing whitespaces.

Signed-off-by: Jia Zhang <zhang.jia@linux.alibaba.com>
2022-12-26 19:12:23 +09:00
Jia Zhang
c2c38276cb measure: fix the failures of compare_reported_pcr_nr()
EFI_LOADER_VARIABLE() already applies to the stringify to construct the
path of EFI variable in efivars, so it is wrong to enclose the name of
EFI variable with the quotes. Otherwise, the following errors are
reported.

Reading EFI variable /sys/firmware/efi/efivars/StubFeatures-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f.
Reading EFI variable /sys/firmware/efi/efivars/"StubPcrKernelImage"-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f.
open("/sys/firmware/efi/efivars/"StubPcrKernelImage"-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f") failed: No such file or directory
Reading EFI variable /sys/firmware/efi/efivars/"StubPcrKernelParameters"-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f.
open("/sys/firmware/efi/efivars/"StubPcrKernelParameters"-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f") failed: No such file or directory
Reading EFI variable /sys/firmware/efi/efivars/"StubPcrInitRDSysExts"-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f.
open("/sys/firmware/efi/efivars/"StubPcrInitRDSysExts"-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f") failed: No such file or directory

Signed-off-by: Jia Zhang <zhang.jia@linux.alibaba.com>
2022-12-23 14:41:33 +01:00
Yu Watanabe
57e7af551e Merge pull request #25786 from keszybz/ebadf
Use -EBADF for fd initialization
2022-12-20 10:02:10 +09:00
Lennart Poettering
34eebf08fa Merge pull request #25783 from keszybz/trivial-cleanups
Trivial cleanups
2022-12-19 17:01:51 +01:00
Zbigniew Jędrzejewski-Szmek
a0e475ce6a efi: do not use 'r' as pointer name
'r' should only be used as in 'int r'.
2022-12-19 15:16:33 +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
Lennart Poettering
baafb202f8 bootctl: split out "install" verb too 2022-12-19 12:08:02 +01:00
Lennart Poettering
da36788f65 bootctl: split out "status" verb too 2022-12-19 12:08:02 +01:00
Lennart Poettering
6d8ca25766 bootctl: split out "set-efivar" verbs, too 2022-12-19 12:06:33 +01:00
Lennart Poettering
6e975890a6 bootctl: also split out 'systemd-efi-options' verb 2022-12-19 11:52:09 +01:00
Lennart Poettering
85f875357b bootctl: split out random seed verb, too 2022-12-19 11:43:19 +01:00
Lennart Poettering
34ea7e0232 bootctl: let's start splitting up bootctl like we did for systemctl and others 2022-12-19 11:43:19 +01:00
Lennart Poettering
0318d54539 pcrphase: gracefully exit if TPM2 support is incomplete
If everything points to the fact that TPM2 should work, but then the
driver fails to initialize we should handle this gracefully and not
cause failing services all over the place.

Fixes: #25700
2022-12-15 22:20:54 +01:00
Lennart Poettering
a5c690a8b5 measure: add --append= switch for merging signatures
Often it's useful to add multiple signatures in the signature JSON file
to embedd in a single .pcrsig. (For example, a signature by key X for
boot phase "enter-initrd" and one by key Y for
"enter-initrd:leave-initrd" or so). Make this easy, by adding the
ability to append signatures to a previously generated JSON file.
2022-12-15 11:40:21 +01:00
Lennart Poettering
c68523e00d Merge pull request #25350 from poettering/efi-guid-equal
efi: add efi_guid_equal() helper
2022-12-15 10:24:58 +01:00
Lennart Poettering
a04709c1ac efi: add common implementation for loop finding EFI configuration tables 2022-12-14 19:16:54 +01:00
Lennart Poettering
50b0b0d351 efi: add efi_guid_equal() helper 2022-12-14 19:16:54 +01:00
Zbigniew Jędrzejewski-Szmek
2053593fcc treewide: drop "RUN_" from "RUN_WITH_UMASK"
RUN_WITH_UMASK was initially conceived for spawning externals progs with the
umask set. But nowadays we use it various syscalls and stuff that doesn't "run"
anything, so the "RUN_" prefix has outlived its usefulness.
2022-12-13 14:39:30 +01:00
Yu Watanabe
057bf780e9 sd-id128: make id128_read() or friends return -ENOPKG when the file contents is "uninitialized"
Then, this drops ID128_PLAIN_OR_UNINIT. Also, this renames
Id128Format -> Id128FormatFlag, and make it bitfield.

Fixes #25634.
2022-12-12 21:57:31 +09:00
Yu Watanabe
febe556191 boot: fix false maybe-uninitialized warning
Fixes #25641.
2022-12-06 13:10:54 +09:00
Yu Watanabe
9e406b1141 boot: cleanups for efivar_get() and friends
- rename function arguments for storing results, and support the case
  that they are NULL,
- return earlier on error,
- always validate read size in efivar_get_uint32_le() and efivar_get_uint64_le().
2022-12-06 13:10:28 +09:00
Jan Janssen
f6d59e2ebf boot: Only do full driver initialization in VMs
Doing the reconnect dance on some real firmware creates huge delays on
boot. This should not be needed anymore as we now ask the firmware to
make console devices and xbootldr partitions available explicitly in a
more targeted fashion.

Fixes: #25510
2022-11-29 15:05:56 +01:00
Jan Janssen
7f19be808c boot: Make sure all partitions drivers are connected 2022-11-29 15:03:52 +01:00
Jan Janssen
b99bf58118 boot: Use EFI_BOOT_MANAGER_POLICY_PROTOCOL to connect console devices 2022-11-29 15:03:52 +01:00
Luca Boccassi
6d3fcb6ccd Merge pull request #25222 from medhefgo/stub-cmdline
stub: Fix cmdline handling
2022-11-28 15:49:17 +01:00
Jan Janssen
b17f3b3d80 stub: Detect empty LoadOptions when run from EFI shell
The EFI shell will pass the entire command line to the application it
starts, which includes the file path of the stub binary. This prevents
us from using the built-in cmdline if the command line is otherwise
empty.

Fortunately, the EFI shell registers a protocol on any images it starts
this way. The protocol even lets us access the args individually, making
it easy to strip the stub path off.

Fixes: #25201
2022-11-27 17:13:04 +01:00
Jan Janssen
27e1f25ef7 stub: Small code style changes
No actual changes in behavior.
2022-11-24 14:03:01 +01:00
Jan Janssen
dde03dd2a8 stub: Fix splash alpha blending
How to interpret the pixel format depends on the masks in the DIB header
(if present). Also, 16bpp (unlike 24bpp) can carry an alpha channel.
This was previously not accounted for.
2022-11-24 13:50:20 +01:00
Jason A. Donenfeld
0a1d8ac77a stub: handle random seed like sd-boot does
sd-stub has an opportunity to handle the seed the same way sd-boot does,
which would have benefits for UKIs when sd-boot is not in use. This
commit wires that up.

It refactors the XBOOTLDR partition discovery to also find the ESP
partition, so that it access the random seed there.
2022-11-23 00:56:45 +01:00
Jan Janssen
927ebebe58 stub: Fix cmdline handling
This fixes some bugs that could lead to garbage getting appended to the
command line passed to the kernel:
 1. The .cmdline section is not guaranteed to be NUL-terminated, but it
    was used as if it was.
 2. The conversion of the command line to ASCII that was passed to the
    stub ate the NUL at the end.
 3. LoadOptions is not guaranteed to be a NUL-terminated EFI string (it
    really should be and generally always is, though).

This also fixes the inconsistent mangling of the command line. If the
.cmdline section was used ASCII controls chars (new lines in particular)
would not be converted to spaces.

As part of this commit, we optimize conversion for the generic code
instead of the (deprecated) EFI handover protocol. Previously we would
convert to ASCII/UTF-8 and then back to EFI string for the (now) default
generic code path. Instead we now convert to EFI string and mangle that
back to ASCII in the EFI handover protocol path.
2022-11-22 17:42:38 +01:00
Jan Janssen
7444e10611 boot: Use xstr8_to_16 for path conversion 2022-11-22 16:50:42 +01:00
Jan Janssen
aee515bbb5 boot: Use xstr8_to_16 2022-11-22 16:03:03 +01:00