Commit Graph

557 Commits

Author SHA1 Message Date
Lennart Poettering
e5a2e78665 cryptsetup: automatically measure used keyslot and mechanism (i.e. fido2, tpm2, pkcs11) to an NvPCR
Fixes: #29877
2025-11-02 21:14:35 +01:00
Lennart Poettering
d412b1629c tpm2-util: rename tpm2_extend_bytes()→tpm2_pcr_extend_bytes() and make it take struct iovec
We soon want to add the ability to extend into nvindexes in addition to
PCRs, hence rename the function to make clear it is about pcr extension.

While we are at it, switch things over to "struct iovec" as we generally
try to do it now in tpm2-util.[ch] these days.
2025-11-02 21:14:35 +01:00
Daan De Meyer
f102bc3e5f tree-wide: Introduce sd-forward.h and shared-forward.h headers
Let's not leak details from src/shared and src/libsystemd into
src/basic, even though you can't actually do anything useful with
just forward declarations from src/shared.

The sd-forward.h header is put in src/libsystemd/sd-common as we
don't have a directory for shared internal headers for libsystemd
yet.

Let's also rename forward.h to basic-forward.h to keep things
self-explanatory.
2025-10-16 17:00:29 +02:00
Lennart Poettering
b3243f4bee libmount: make dlopen() dependency 2025-09-26 08:29:37 +09:00
Mike Yuan
69fa2b6303 cryptsetup: port from setmntent() to libmount parser 2025-09-18 20:25:14 +02:00
Lennart Poettering
b5be05a106 cryptsetup: reference right variable
Fixes: #38576
2025-08-18 13:03:18 +01:00
Yu Watanabe
fb4aabf443 cryptsetup: HAVE_CRYPT_SET_KEYRING_TO_LINK is always defined
Follow-up for c5daf14c88 (v256).
2025-08-17 15:39:43 +01:00
Lennart Poettering
df21ceca25 cryptsetup: handle DA lockout/bad PIN properly
Now that we have recognizable errors, let's print clear error messages
when we try to unlock a TPM slot. And in case of the token plugin
propagate this as ENOANO so that libcryptsetup recognizes this as bad
PIN. (ENOANO is documented as the error to return in that case)

Fixes: #32260
2025-06-19 12:33:52 +02:00
Lennart Poettering
1e08f92250 tpm2: add a clear error message for all errors that suggest a PCR violation
Fixes: #35187
2025-06-03 16:16:46 +02:00
Daan De Meyer
96fafb925c cryptsetup: Clean up includes
Split out of #37344.
2025-05-22 01:40:01 +09:00
Daan De Meyer
30d20907bd meson: Remove unneeded include directories
meson by default adds the current source and build directory as include
directories. Because we structure our meson code by gathering a giant dict
of everything we want to do and then doing all the actual target generation
in the top level meson.build, this behavior does not make sense at all because
we end up adding the top level repository directory as an include directory
which is never what we want.

At the same time, let's also make sure the top level directory of the build
directory is not an include directory, by moving the version.h generation
into the src/version subdirectory and then adding the src/version subdirectory
of the build directory as an include directory instead of the top level
repository directory.

Making this change means that language servers such as clangd can't get
confused when they automatically insert an #include line and insert
"#include "src/basic/fs-util.h" instead of "#include "fs-util.h".
2025-05-12 13:35:50 +02:00
Daan De Meyer
e53d4f343d iovec-util: Reduce transitive includes 2025-05-07 22:04:46 +09:00
Daan De Meyer
6553db6063 strv: Reduce transitive includes 2025-05-07 22:04:46 +09:00
Daan De Meyer
e4a08721c3 string-util: Remove utf8.h and alloc-util.h includes 2025-05-06 13:27:54 +02:00
Daan De Meyer
40a6cdc29e macro: Introduce ABS() macro and use it in header files
abs() requires including the entirety of stdlib.h just for a single
trivial function. Let's introduce the ABS() macro and use it in header
files instead so we can get rid of stdlib.h transitive includes in header
files in a later commit.
2025-05-06 10:14:26 +02:00
Daan De Meyer
76d62b63ef main-func: Reduce transitive includes
Let's move some logic from _DEFINE_MAIN_FUNCTION() and other places
in main-func.h into functions that we implement in main-func.c to
allow moving some included headers from the header to the .c file.
2025-05-06 10:07:27 +02:00
Daan De Meyer
1cf40697e3 tree-wide: Sort includes
This was done by running a locally built clang-format with
https://github.com/llvm/llvm-project/pull/137617 and
https://github.com/llvm/llvm-project/pull/137840 applied on all .c
and .h files.
2025-04-30 09:30:51 +02:00
Daan De Meyer
bcf716ca01 tree-wide: Remove dead code
This code seems to not be used anywhere at all, so let's drop it.

Follow up for ba8d00e859
Follow up for 85686b37b0
Follow up for aff853f8ea
Follow up for 36dad381a2
2025-04-24 10:15:19 +09:00
Daan De Meyer
401a22bfc1 tree-wide: Make sure all headers are standalone
Let's make sure all of our headers include everything they require.
2025-04-23 10:33:38 +02:00
Daan De Meyer
95609f3dff basic: Remove circular dep between alloc-util.h and memory-util.h 2025-04-18 14:19:16 +02:00
Daan De Meyer
93a1f7921a basic: Stop including log.h in macro.h
Now that the necessary functions from log.h have been moved to macro.h,
we can stop including log.h in macro.h. This requires modifying source
files all over the tree to include log.h instead.
2025-04-18 14:19:15 +02:00
Luca Boccassi
5aee5bda1a tree-wide: introduce LOG_ITEM() macro for verifying format string (#36780)
This introduce `LOG_ITEM()` macro that checks format strings in
log_struct() and friends.

Hopefully, this silences false-positive warnings by Coverity.
2025-03-24 12:58:14 +00:00
Lennart Poettering
0015502168 tpm2-util: return better errors if we try to unlock a tpm key on the wrong tpm
Let's improve error handling in case one tries to unlock a TPM2 locked
volume on a different machine via TPM than it was originally enrolled
on. Let's recognize this case and print a clearer error message.
2025-03-19 21:27:54 +01:00
Yu Watanabe
3cf6a3a3d4 tree-wide: check more log message format in log_struct() and friends
This introduce LOG_ITEM() macro that checks arbitrary formats in
log_struct().
Then, drop _printf_ attribute from log_struct_internal(), as it does not
help so much, and compiler checked only the first format string.

Hopefully, this silences false-positive warnings by Coverity.
2025-03-19 01:56:48 +09:00
Yu Watanabe
c0cc01de8a meson: use install_symlink() where applicable
Now our baseline of meson is 0.62, hence install_symlink() can be used.

Note, install_symlink() implies install_emptydir() for specified
install_dir. Hence, this also drops several unnecessary
install_emptydir() calls.

Note, the function currently does not support 'relative' and 'force' flags,
so several 'ln -frsT' inline calls cannot be replaced.
2025-03-10 02:41:40 +09:00
Aaron Rogers
08633ea902 cryptsetup: align tpm2-blob 2025-03-05 11:41:30 +01:00
Mike Yuan
ce0a510667 cryptsetup: port to strprepend() 2025-02-10 19:39:21 +01:00
Lennart Poettering
4b840414be cryptenroll/repart/creds: no longer default to binding against literal PCR 7
PCR 7 covers the SecureBoot policy, in particular "dbx", i.e. the
denylist of bad actors. That list is pretty much as frequently updated
as firmware these days (as fwupd took over automatic updating). This
means literal PCR 7 policies are problematic: they likely break soon,
and are as brittle as any other literal PCR policies.

hence, pick safer defaults, i.e. exclude PCR 7 from the default mask.
This means the mask is now empty.

Generally, people should really switch to signed PCR policies covering
PCR 11, in combination with systemd-pcrlock for the other PCRs.
2025-01-30 10:32:26 +01:00
Yu Watanabe
a0b1ee2fca cryptsetup: use dispatch_verb() 2025-01-21 03:42:38 +09:00
Yu Watanabe
13a3b59144 cryptsetup: drop doubled space 2025-01-21 03:42:38 +09:00
Yu Watanabe
7e21105002 cryptsetup: use mangle_none() at one more place 2025-01-21 03:42:34 +09:00
Lennart Poettering
d66894a7a5 ask-password-api: add new "hup_fd" field to AskPasswordReq
This new field allows specification of an fd on which the password
prompt logic will look for POLLHUP events for, and if seen will abort
the query.

The usecase for this is that when we query for a pw on behalf of a
Varlink client we can abort the query automatically if the client dies.
2025-01-02 16:38:44 +01:00
Lennart Poettering
c4a02a526d ask-password-api: move timeout into AskPasswordRequest structure too 2025-01-02 16:38:25 +01:00
Lennart Poettering
72068d9d25 ask-password-api: move tty_fd into AskPasswordRequest structure, too 2025-01-02 16:38:12 +01:00
Luca Boccassi
184ce19841 gpt-auto: take timeout opts in rootflags= into account; hibernate-resume: always respect user-defined timeout (#35518) 2024-12-12 11:01:40 +00:00
Zbigniew Jędrzejewski-Szmek
da64932dc7 cryptsetup: add debug message 2024-12-10 10:58:58 +01:00
Mike Yuan
9a097eced6 shared/generator: clean up generator_write_device_timeout() a bit
- Rephrase a few log messages
- Remove unused where parameter
- Return 1 if we set something

No functional change, preparation for later commits.
2024-12-09 19:22:33 +01:00
Luca Boccassi
c4d7a13c06 cryptsetup: convert pkcs11/fido2 to iovec for key handling
key-data might be NULL. Fixes crash:

0  0x0000559c62120530 in attach_luks_or_plain_or_bitlk (cd=0x559c6b192830, name=0x7ffd57981dc4 "root", token_type=TOKEN_FIDO2, key_file=0x0, key_data=0x0, passwords=0x0, flags=524296, until=0)
    at ../src/cryptsetup/cryptsetup.c:2234
        pass_volume_key = false
        r = 1469577760
        __func__ = '\000' <repeats 29 times>
1  0x0000559c6212279c in run (argc=6, argv=0x7ffd5797fe98) at ../src/cryptsetup/cryptsetup.c:2597
        discovered_key_data = {iov_base = 0x0, iov_len = 0}
        key_data = 0x0
        token_type = TOKEN_FIDO2
        destroy_key_file = 0x0
        flags = 524296
        until = 0
        passphrase_type = PASSPHRASE_NONE
        volume = 0x7ffd57981dc4 "root"
        source = 0x7ffd57981dc9 "/dev/disk/by-uuid/8372fb39-9ba4-461a-a618-07dcaae66280"
        status = CRYPT_INACTIVE
        tries = 0
        key_file = 0x0
        config = 0x7ffd57981e05 "luks,discard,fido2-device=auto,x-initrd.attach"
        use_cached_passphrase = true
        try_discover_key = true
        discovered_key_fn = 0x7ffd5797fa70 "root.key"
        passwords = 0x0
        cd = 0x559c6b192830
        verb = 0x7ffd57981dbd "attach"
        r = 0
        __func__ = "\000\000\000"
2  0x0000559c621231e6 in main (argc=6, argv=0x7ffd5797fe98) at ../src/cryptsetup/cryptsetup.c:2674
        r = 32553
        __func__ = "\000\000\000\000"

Follow-up for 53b6c99018
2024-11-26 22:04:24 +09:00
Mike Yuan
e2f82f6151 various: check meson feature flag early
Prompted by https://github.com/systemd/systemd/pull/35110#discussion_r1835885340
2024-11-13 08:21:33 +09:00
Zbigniew Jędrzejewski-Szmek
2257be13fe tree-wide: time-out → timeout
For justification, see 3f9a0a522f.
2024-11-05 19:32:19 +00:00
Lennart Poettering
8e6587679b cryptenroll/cryptsetup: allow combined signed TPM2 PCR policy + pcrlock policy
So far you had to pick:

1. Use a signed PCR TPM2 policy to lock your disk to (i.e. UKI vendor
   blesses your setup via signature)
or
2. Use a pcrlock policy (i.e. local system blesses your setup via
   dynamic local policy stored in NV index)

It was not possible combine these two, because TPM2 access policies do
not allow the combination of PolicyAuthorize (used to implement #1
above) and PolicyAuthorizeNV (used to implement #2) in a single policy,
unless one is "further upstream" (and can simply remove the other from
the policy freely).

This is quite limiting of course, since we actually do want to enforce
on each TPM object that both the OS vendor policy and the local policy
must be fulfilled, without the chance for the vendor or the local system
to disable the other.

This patch addresses this: instead of trying to find a way to come up
with some adventurous scheme to combine both policy into one TPM2
policy, we simply shard the symmetric LUKS decryption key: one half we
protect via the signed PCR policy, and the other we protect via the
pcrlock policy. Only if both halves can be acquired the disk can be
decrypted.

This means:

1. we simply double the unlock key in length in case both policies shall
   be used.
2. We store two resulting TPM policy hashes in the LUKS token JSON, one
   for each policy
3. We store two sealed TPM policy key blobs in the LUKS token JSON, for
   both halves of the LUKS unlock key.

This patch keeps the "sharding" logic relatively generic (i.e. the low
level logic is actually fine with more than 2 shards), because I figure
sooner or later we might have to encode more shards, for example if we
add further TPM2-based access policies, for example when combining FIDO2
with TPM2, or implementing TOTP for this.
2024-09-06 15:55:28 +02:00
Lennart Poettering
9407fe6c3d treewide: use sd_json_variant_un{hex|base64}() pervasively
Use these helpers whenever appropriate. Drop separate string checks,
since these helpers already do them anyway.

No actual code change, just a rework to make use of a nice helper we
have already.
2024-08-30 10:35:44 +02:00
Kamil Szczęk
88cf0906aa cryptsetup: handle parallel activation of volumes with another tool gracefully
This commit makes systemd-cryptsetup exit with a successful status when
the volume gets unlocked outside of the current systemd-cryptsetup
process while it was executing. This can be easily reproduced by calling
systemd-cryptsetup, and while it waits for user to input a password/PIN,
unlock the volume in a second terminal. Then after entering the password
systemd-cryptsetup will exit with a non-zero status code.
2024-07-21 19:02:59 +01:00
Kamil Szczęk
fd8ed7f26b cryptsetup: allow customizing cache behavior
The new "password-cache" option allows customizing behavior of the
ask-password module in regards to caching credentials in the kernel
keyring. There are 3 possible values for this option:
  * read-only - look for credentials in kernel keyring before asking
  * on - same as read-only, but also save credentials input by user
  * off - disable keyring credential cache

Currently the cache is forced upon the user and this can cause issues.
For example, if user wants to attach two volumes with two different
FIDO2 tokens in a quick succession, the attachment operation for the
second volume will use the PIN cached from the first FIDO2 token, which
of course will fail and since tokens are only attempted once, this will
cause fallback to a password prompt.
2024-06-27 13:00:49 +02:00
Kamil Szczęk
53b6c99018 cryptsetup: make key discovery more robust
Currently, if user doesn't specify a key file, /etc/cryptsetup-keys.d/
and /run/cryptsetup-keys.d/ will be searched for a key file with name
matching the volume name. But current implementation has an important
flaw. When the auto-discovered key is a socket file - it will read the
key only once, while the socket might provide different keys for
different types of tokens. The issue is fixed by trying to discover the
key on each unlock attempt, this way we can populate the socket bind
name with something the key provider might use to differentiate between
different keys it has to provide.
2024-06-27 12:58:45 +02:00
Kamil Szczęk
0828c6a2bf cryptsetup: improve TPM2 blob display
Just a tiny change to fix an eyesore in cryptsetup luksDump display :)
2024-06-26 20:52:31 +02:00
Lennart Poettering
50df39f2dc cryptsetup: minor coding style tweaks
Don't cram function calls and assignment into if condition checks. It's
not how we usually do things.

Also, define variables at innermost scope.
2024-06-21 15:57:21 +02:00
Kamil Szczęk
e262205eb7 cryptenroll: support for enrolling FIDO2 tokens in manual mode
systemd-cryptsetup supports a FIDO2 mode with manual parameters, where
the user provides all the information necessary for recreating the
secret, such as: credential ID, relaying party ID and the salt. This
feature works great for implementing 2FA schemes, where the salt file
is for example a secret unsealed from the TPM or some other source.
While the unlocking part is quite straightforward to set up, enrolling
such a keyslot - not so easy. There is no clearly documented
way on how to set this up and online resources are scarce on this topic
too. By implementing a straightforward way to enroll such a keyslot
directly from systemd-cryptenroll we streamline the enrollment process
and reduce chances for user error when doing such things manually.
2024-06-20 14:26:24 +02:00
Zbigniew Jędrzejewski-Szmek
bfd5a0687f various: move const ptr indicator to return value 2024-06-19 16:28:28 +02:00
Lennart Poettering
8a4883dbc3 cryptsetup: downgrade log message to warning, when we ignore it 2024-06-14 10:10:37 +02:00