1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2025-01-28 17:47:05 +03:00

75 Commits

Author SHA1 Message Date
David Tardon
23cd002562 macro-fundamental: allow to nest ASSERT_PTR
E.g.,

int job_frobnicate(Job *j) {
        Unit *u = ASSERT_PTR(ASSERT_PTR(j)->unit);
        ...
}
2022-09-14 08:21:40 +02:00
David Tardon
995340074e tree-wide: use ASSERT_PTR more 2022-09-13 08:13:27 +02:00
Lennart Poettering
df7ee6f8b0 stub: add new special PE sections ".pcrsig" and ".pcrpkey" in unified kernels
These aren't wired up yet to do anything useful. For now we just define
them.

This sections are supposed to carry a signature for expected
measurements on PCR 11 if this kernel is booted, in the JSON format
"systemd-measure sign" generates, and the public key used for the
signature.

The idea is to embedd the signature and the public key in unified
kernels and making them available to userspace, so that userspace can
easily access them and enroll (for which the public key is needed) or
unlock (for which the PCR signature is needed) LUKS2 volumes and
credentials that are bound to the currently used kernel version stream.

Why put these files in PE sections rather than just into simple files in
the initrd or into the host fs?

The signature cannot be in the initrd, since it is after all covering
the initrd, and thus the initrd as input for the calculation cannot
carry the result of the calculation. Putting the signature onto the root
fs sucks too, since we typically want to unlock the root fs with it,
hence it would be inaccessible for it's primary purpose then.

The public key could be in the initrd or in the root fs, there's no
technical restriction for that. However, I still think it's a good idea
to put it in a PE section as well, because this means the piece of code
that attaches the signature can also attach the public key easily in one
step, which is nice since it allows separating the roles of the
kernel/initrd/root fs builder, and the role of the signer, and the
former doesn't have to have knowledge about what the latter is going to
add to the image.

Note that the signature section is excluded from the TPM measurements
sd-stub does about its resource sections, since – as mentioned – it's
the expected output of the signing operation whose input are the
measurements, hence it cannot also be input to them. The public key
section is included in the measurements however.
2022-09-09 11:28:38 +02:00
Lennart Poettering
558d96240b sha256: add sha256_direct()/SHA256_DIRECT() helpers 2022-08-19 12:53:04 +02:00
Lennart Poettering
3c4d5f2ff5 sha256: change digest buffer type to uint8_t[]
This way we can specify a size with "static". All users use uint8_t
already, hence this comes at no price.
2022-08-19 12:53:04 +02:00
Lennart Poettering
983ce0b517 macro: use ISPOWEROF2() at various places 2022-08-05 13:37:29 +02:00
Lennart Poettering
c51e4c796d macro: add macro for checking if integer is power of 2 2022-08-05 13:34:11 +02:00
Lennart Poettering
a67f22c4a7 boot: add three new boot loader feature flags
Report whether the devicetree + sort-key boot loader spec type #1
fields are supported, and whether the "@saved" pseudo-entry is
supported.

Strictly speaking, thes features have been added in versions that are
already released (250+), so by adding this those version even though
they support the features will be considered not supporting them, but
that should be OK (the opposite would be a problem though, i.e. if we'd
assume a boot loader had a feature it actually does not).

These three features are features relevant to userspace, as it allows
userspace to tweak/genereate BLS entries or set EFI vars correctly.
Other features (i.e. that have no impliciations to userspace) are not
reported.
2022-08-02 10:28:49 +02:00
Lennart Poettering
46d336722d stub: introduce StubFeatures, similar to LoaderFeatures
systemd-boot reports its features via the LoaderFeatures EFI variable.
Let's add something similar for stub features, given they have been
growing.

For starters only define four feature flags. One is a baseline feature
we pretty much always supported (see comment in code), two are features
added in one of the most recently released systemd version, and the
final one, is a feature we added a few commits ago.

This is useful for userspace to figure out what is supported and what
not.
2022-08-02 10:28:49 +02:00
Lennart Poettering
6017eee9ce boot: move unified kernel PE section definitions into 'fundamental' code
Le's share this code between userspace and uefispace. This is useful
later when pre-measuring expected PCRs from userspace.
2022-08-02 10:28:49 +02:00
Lennart Poettering
aa323c0907 boot: split out TPM PCR defines into header file we can share between userspace and kernel space 2022-08-02 10:28:49 +02:00
igo95862
724e13b346
test-bus-objects: Test GetManagedObjects interfaces are correct
Objects without ObjectManager should not have
`org.freedesktop.DBus.ObjectManager` interface.

Object with ObjectManager should do.

Also added ASSERT_SE_NONNEG and ASSERT_NONNEG macros.
2022-07-25 23:29:17 +03:00
matoro
9ddb63f5cf fundamental: replace __sync with __atomic in ONCE macro
For this one, we can actually just use __atomic_exchange_n since we
don't need the "compare" part of __atomic_compare_exchange_n.
2022-07-14 17:34:15 -04:00
Thomas Haller
da519f8c81 fundamental: adjust #if conditional for _fallthrough_ for clang
NetworkManager takes systemd sources. It gets compiler warnings
related to _fallthrough_. They probably can also affect systemd
itself.

A) on RHEL-7, gcc 4.8.5-44.el7 we get:

    ../src/libnm-systemd-shared/src/fundamental/macro-fundamental.h:45:22: error: "__clang__" is not defined [-Werror=undef]
     #if __GNUC__ >= 7 || __clang__
                          ^

   Presumably gcc older than 7 is supported, so fix this.

B) on Ubuntu 18.04, clang 1:6.0-41~exp5~ubuntu1 we get:

    ../src/libnm-systemd-core/src/libsystemd-network/sd-dhcp6-client.c:746:17: error: declaration does not declare anything [-Werror,-Wmissing-declarations]
                    _fallthrough_;
                    ^
    ../src/libnm-systemd-shared/src/fundamental/macro-fundamental.h:46:25: note: expanded from macro '_fallthrough_'
    #  define _fallthrough_ __attribute__((__fallthrough__))
                            ^

   Granted, README comments that clang >= 10 is required. However,
   parts of systemd build just fine with older clang. It seems unnecessary
   to break this and the fix helps NetworkManager.

Fixes: c0f5d58c9ab7 ('meson: Document why -Wimplicit-fallthrough is not used with clang')
2022-07-06 22:10:23 +02:00
Lennart Poettering
ff25d3385d tree-wide: add global ascii_isdigit() + ascii_isalpha()
We now have a local implementation in string-util-fundamental.c, but
it's useful at a lot of other places, hence let's give it a more
expressive name and share it across the tree.

Follow-up for: 8d9156660d6958c8d63b1d44692968f1b5d33920
2022-07-05 14:25:07 +02:00
Jan Janssen
6b852d22b6 fundamental: Remove types-fundamental.h
This removes the fundamental typedefs in favor of just using standard C
types. These are all used internally anyway and also do not do anything
special to warrant any redefinition to EFI types.

Even for BOOLEAN we can safely use stdbool. The defition from the EFI
specification is fully compatible, including making any other values
than 0/1 as undefined.

The exception is sd_char as those need to be char16_t. The typedef is
moved to string-util-fundamental.h instead.
2022-06-27 12:16:27 +02:00
Jan Janssen
4e5c39597a sha256: Use stdbool and uintptr_t
This also syncs the copyright blurb with current glibc sources. The
written by line does not appear in upstream, so it should be okay to
remove.
2022-06-24 17:51:17 +01:00
Jan Janssen
9148312fab boot: Add xmalloc 2022-06-09 12:50:13 +02:00
Zbigniew Jędrzejewski-Szmek
38c87ca2ab sha256: fix compilation on efi-ia32
/usr/bin/gcc -c ../src/fundamental/sha256.c -o src/boot/efi/sha256.c.o -Wno-format-signedness -Wno-missing-field-initializers -Wno-unused-parameter -Wdate-time -Wendif-labels -Werror=format=2 -Werror=implicit-function-declaration -Werror=incompatible-pointer-types -Werror=int-conversion -Werror=overflow -Werror=override-init -Werror=return-type -Werror=shift-count-overflow -Werror=shift-overflow=2 -Werror=undef -Wfloat-equal -Wimplicit-fallthrough=5 -Winit-self -Wlogical-op -Wmissing-include-dirs -Wmissing-noreturn -Wnested-externs -Wold-style-definition -Wpointer-arith -Wredundant-decls -Wshadow -Wstrict-aliasing=2 -Wstrict-prototypes -Wsuggest-attribute=noreturn -Wunused-function -Wwrite-strings -Wno-unused-result -fno-stack-protector -fno-strict-aliasing -fpic -fwide-exec-charset=UCS2 -Wall -Wextra -Wsign-compare -nostdlib -std=gnu99 -ffreestanding -fshort-wchar -fvisibility=hidden -isystem /usr/include/efi -isystem /usr/include/efi/ia32 -I /builddir/build/BUILD/systemd-stable-250.7/src/fundamental -DSD_BOOT -DGNU_EFI_USE_MS_ABI -include src/boot/efi/efi_config.h -include version.h -mno-sse -mno-mmx -flto -O2 -flto=auto
../src/fundamental/sha256.c: In function ‘sha256_finish_ctx’:
../src/fundamental/sha256.c:61:25: error: ‘false’ undeclared (first use in this function)
   61 | # define UNALIGNED_P(p) false
      |                         ^~~~~
../src/fundamental/sha256.c:136:21: note: in expansion of macro ‘UNALIGNED_P’
  136 |                 if (UNALIGNED_P(resbuf))
      |                     ^~~~~~~~~~~
../src/fundamental/sha256.c:32:1: note: ‘false’ is defined in header ‘<stdbool.h>’; did you forget to ‘#include <stdbool.h>’?
   31 | #include "sha256.h"
  +++ |+#include <stdbool.h>
   32 |
...
2022-06-03 19:09:54 +09:00
Yu Watanabe
c7a5eabeba sha256: use memcpy() when result buffer is unaligned
Fixes #23578.
2022-06-02 14:14:22 +09:00
Yu Watanabe
9db25d01ef
Merge pull request #23531 from yuwata/sd-bus-drop-version-2
sd-bus: drop version 2 format support
2022-06-01 14:51:44 +09:00
Yu Watanabe
4c8d7caf1a macro: make ALIGN4() and ALIGN8() also return SIZE_MAX on overflow
This also drops unused ALIGN4_PTR(), ALIGN8_PTR(), and ALIGN_TO_PTR().
2022-06-01 03:40:20 +09:00
Jan Janssen
bbc1f2eac5 boot: Use memcmp/memcpy/memset 2022-05-31 15:15:01 +02:00
Jan Janssen
ec436bdf16 boot: Use strcmp8/16
This also replaces streq and similar functions for consistency. Note
that streq16 is null pointer safe, so streq_ptr can be safely replaced
too.
2022-05-31 15:09:10 +02:00
Jan Janssen
5f49747255 boot: Use strlen8/16
The casts in this and the next few commits are curently necessary
because CHAR8 is defined as uint8_t in gnu-efi, while char is signed.
Once we switch from gnu-efi typedefs to stdint types, the casts
will be dropped.
2022-05-31 14:18:43 +02:00
Jan Janssen
c0f5d58c9a meson: Document why -Wimplicit-fallthrough is not used with clang
This changes the macro to ensure proper fallthrough attributes are
used with clang in case this option is added in the future.
2022-05-28 12:44:08 +02:00
Jan Janssen
b41ebe3d18 macro: Move attribute defintions to macro-fundamental
This also sorts them.
2022-05-28 11:34:16 +02:00
Luca Boccassi
e1a8917ae1
Merge pull request #23504 from keszybz/bls-reordering
Refactor the BLS and add a description of version sorts
2022-05-27 14:36:10 +01:00
Zbigniew Jędrzejewski-Szmek
f90ee9ff66 basic: add helper function to print </==/> 2022-05-25 16:02:25 +02:00
Zbigniew Jędrzejewski-Szmek
2367bdcfc9 docs/BLS: describe version comparisons
Fixes #23346.
2022-05-25 13:47:47 +02:00
Zbigniew Jędrzejewski-Szmek
46083ab321 basic/string-util: tweak strverscmp_improved() for some corner cases
So far we had the rule that '' == '', '0_' == '0', but '_' > ''. This means
that the general rule that strings are compared iteratively, and each
segment that compares equal can be dropped and the comparison resumes at
the following characters wasn't true in such cases. Similarly, '0~' < '0',
but after dropping the common segment, '~' > ''.

The special handling of empty strings is dropped, and '_' == '' and
'~' < ''.
2022-05-25 13:47:47 +02:00
Zbigniew Jędrzejewski-Szmek
2f9b7186e3
Merge pull request #23414 from keszybz/analyze-vercmp
systemd-analyze compare-versions
2022-05-23 09:14:51 +02:00
Victor Westerhuis
87c7779599 Do not require a valid version when parsing sd-boot loader entries
This fixes #20820
2022-05-22 12:17:52 +02:00
Jan Janssen
7b19627697 fundamental: Move some helpers into string-util-fundamental 2022-05-21 15:11:13 +01:00
Jan Janssen
351b30dae0 macro: Use C11 noreturn only
No need to provide a fallback as we compile with gnu11.
2022-05-21 15:01:47 +01:00
Jan Janssen
6c405f20a3 macro: Use C11 static_assert
Now that we require C11 everywhere, we can always use static_assert.
2022-05-20 12:49:50 +02:00
Jan Janssen
b1672234e0 boot: Build with C11 too 2022-05-20 12:47:43 +02:00
Zbigniew Jędrzejewski-Szmek
8d9156660d version comparisons: stop using locale-dependent isdigit()
The docs are not entirely clear what glyphs qualify as digits.
The function is supposed to be locale-dependent, but I couldn't
get it to return true on any non-ascii digits I tried.
But it's better to be safe than sorry, let's use our trivial
replacement instead.
2022-05-19 11:07:28 +02:00
Zbigniew Jędrzejewski-Szmek
776fabbc8b Add a "test" that prints the SBAT table
The SBAT section was included in a special section in the EFI code, but
the contents weren't directly visible in any way. Let's add a "test" that
prints them for visual inspection.

If there's some external linter for this format, we could hook it up in the
future.
2022-05-17 16:45:15 +02:00
Zbigniew Jędrzejewski-Szmek
dbf43adce2 fundamental: make strverscmp_improved() return -1/0/+1 in all cases
We would return the result of strcmp(), i.e. some positive/negative value.
Now that we want to make this a documented interface for other people
to implement, let's make the implementation more contstrained, even if
we ourselves don't care about whether the specific values.
2022-05-17 16:33:43 +02:00
Zbigniew Jędrzejewski-Szmek
e77a55c599 fundamental/string-util-fundamental: include appropriate headers
We were using CMP() without pulling the definition in directly.
2022-05-17 16:33:43 +02:00
Zbigniew Jędrzejewski-Szmek
c3e4cbe0c5 basic: make macro-fundamental.h self-contained
When !SD_BOOT, it used size_t without including the appropriate header.
2022-05-17 16:33:43 +02:00
Zbigniew Jędrzejewski-Szmek
68df2df668 Clean up the text in description of strverscmp_improved() 2022-05-17 16:33:43 +02:00
Yu Watanabe
798931160e tree-wide: add a space after if, switch, for, and while 2022-04-01 22:48:42 +09:00
Lennart Poettering
20ec8f534f sd-boot: make use of new "sort-key" boot loader spec field 2022-03-18 11:59:30 +01:00
Grigori Goronzy
e560cf4f71 hmac/sha256: move size define to sha256.h 2022-03-15 21:17:00 +01:00
Lennart Poettering
8890ec82f5 macro: add ASSERT_SE_PTR() macro
ASSERT_SE_PTR() is like ASSERT_PTR() but uses assert_se() instead of
assert() internally.

Code should use ASSERT_SE_PTR() where the check should never be
optimized away, even if NDEBUG is set.

Rationale: assert() is the right choice for validating assumptions about
our own code, i.e. checking conditions that are "impossible" to not
hold, because we ourselves hacked things up the "right" way of course.
assert_se() is the right choice for tests that come with a weaker
guarantee, they encode assumptions over other's API behaviour, i.e.
whether something can fail there or not.

When developing tools that are not oom-safe assert_se() is the right
choice: we know that on Linux OOM doesn't really happen, even though
theoretically the API allows it to happen.

Usecase for ASSERT_SE_PTR() is mostly the fatal memory allocation logic
for EFI memory allocations. So far it used regular assert() i.e. OOM
failurs would be totally ignored if NDEBUG is set. We'd rather have our
EFI program to print an assert message and freeze instead though.
2022-02-01 15:31:05 +01:00
Lennart Poettering
3b23a6c40a fundamental: support assert_se() in EFI mode too 2022-02-01 15:29:21 +01:00
Zbigniew Jędrzejewski-Szmek
d821e40ca9 networkctl: open the bus just once
We'd connect to the bus twice: the first time to check networkd namespace,
and then the second time to do the deed we were asked to do. It's nicer
to open the bus just once, for efficience and also to avoid the open call
in all functions.

An ASSERT_PTR helper is added:
- sd_bus *bus = userdata;
  ...
- assert(bus);
+ sd_bus *bus = ASSERT_PTR(userdata);
  ...

It can be used in other place too, but I'm leaving that for a later
refactoring.
2022-01-14 16:24:04 +09:00
Jan Janssen
b3c5a7074c meson: Use files() for source lists for boot and fundamental
This fixes build reproducibility as otherwise the full path
of the source files ends up in the output binary.
2022-01-09 18:02:34 +01:00