Commit Graph

252 Commits

Author SHA1 Message Date
Lennart Poettering a7e8855879 units: introduce blockdev@.target for properly ordering mounts/swaps against cryptsetup
Let's hook it into both cryptsetup-generator and gpt-auto-generator with
a shared implementation in generator.c

Fixes: #8472
2020-01-21 20:23:13 +01:00
Lennart Poettering 6bbd539e5e cryptsetup-generator: order after cryptsetup-pre.target unconditionally 2020-01-21 20:23:10 +01:00
Lennart Poettering 49685fb314 cryptsetup-generator: break overly long line 2020-01-21 20:23:06 +01:00
Frantisek Sumsal e514aa1eea tree-wide: yet another batch of coccinelle recommendations
Prettify certain parts of the codebase using coccinelle transformations
(no functional changes).
2020-01-02 13:32:55 +01:00
Lennart Poettering 6789dd57f0 cryptsetup-pkcs11: just return zero on success, no need to return anything else 2019-12-17 18:54:07 +01:00
Lennart Poettering 3ded1d616a cryptsetup-pkcs11: line break some overly long lines 2019-12-17 18:54:04 +01:00
Lennart Poettering 12f69587e9 cryptsetup-pkcs11: refuse keys above 16MiB size 2019-12-17 18:54:00 +01:00
Lennart Poettering 13b6c4c8de
Merge pull request #14267 from poettering/pkcs11-cryptsetup
just the pkcs11 hookup for classic cryptsetup (/etc/crypttab) split out of the homed PR
2019-12-17 15:30:32 +01:00
Lennart Poettering c16782577b
Merge pull request #14241 from keszybz/resume-timeout
Bump resume timeout to infinity
2019-12-17 10:34:43 +01:00
Lennart Poettering 086697094e cryptsetup: add native pkcs#11 support to cryptsetup
This adds a new crypttab option for volumes "pkcs11-uri=" which takes a
PKCS#11 URI. When used the key stored in the line's key file is
decrypted with the private key the PKCS#11 URI indiciates.

This means any smartcard that can store private RSA keys is usable for
unlocking LUKS devices.
2019-12-09 19:25:25 +01:00
Franck Bui 1dc85eff1d crypsetup: introduce x-initrd.attach option
This option is an indication for PID1 that the entry in crypttab is handled by
initrd only and therefore it shouldn't interfer during the usual start-up and
shutdown process.

It should be primarily used with the encrypted device containing the root FS as
we want to keep it (and thus its encrypted device) until the very end of the
shutdown process, i.e. when initrd takes over.

This option is the counterpart of "x-initrd.mount" used in fstab.

Note that the slice containing the cryptsetup services also needs to drop the
usual shutdown dependencies as it's required by the cryptsetup services.

Fixes: #14224
2019-12-05 11:43:02 +01:00
Zbigniew Jędrzejewski-Szmek 7cecc56316 cryptsetup-generator: unconfuse writing of the device timeout
The code was using timeout=0 as the default option string. This option string
was ultimately passed to generator_write_timeouts(), which only looks for
comment=systemd.device-timeout= or x-systemd.device-timeout=, i.e. the whole
call path was bogus. Let's rework this: generator_write_timeouts() now writes
any timeouts if configured by the user. create_disk() writes out it's own
timeout, but with lower priority. Since the code path that was calling
timeout=0 was not effective, the only change is that we stop overwriting the
timeout if explicitly configured by the user.

In both code paths, ignore failure to write.
2019-12-03 17:20:19 +01:00
Michal Sekletár 408c81f624 cryptsetup: reduce the chance that we will be OOM killed
cryptsetup introduced optional locking scheme that should serialize
unlocking keyslots which use memory hard key derivation
function (argon2). Using the serialization should prevent OOM situation
in early boot while unlocking encrypted volumes.
2019-11-27 18:34:36 +01:00
Lennart Poettering 9120aa820b cryptsetup: use STR_IN_SET() where appropriate
Note that this slightly changes behaviour: "none" is only allowed as
option, if it's the only option specified, but not in combination with
other options. I think this makes more sense, since it's the choice when
no options shall be specified.
2019-11-19 15:34:09 +01:00
Lennart Poettering 9c5253ffec cryptsetup: minor coding style clean-ups 2019-11-19 15:34:09 +01:00
Zbigniew Jędrzejewski-Szmek ed4ad48897 Allow overriding /etc/fstab with $SYSTEMD_FSTAB 2019-11-13 22:04:51 +01:00
Zbigniew Jędrzejewski-Szmek 32c6237a7c cryptsetup-generator: guess whether the keyfile argument is two items or one
Fixes #13615.

See the inline comment for documentation.
2019-11-13 22:04:45 +01:00
Zbigniew Jędrzejewski-Szmek 3f5ac3038e cryptsetup-generator: allow overriding /run/systemd/cryptsetup with $RUNTIME_DIRECTORY
I added a fairly vague entry to docs/ENVIRONMENT because I think it is worth
mentioning there (in case someone is looking for any environment variable that
might be relevant).
2019-11-13 22:04:38 +01:00
Zbigniew Jędrzejewski-Szmek a6c57e74c5 cryptsetup-generator: allow overriding crypttab path with $SYSTEMD_CRYPTAB 2019-11-13 17:55:51 +01: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
Jay Strict d90874b4e2 cryptsetup: bump minimum libcryptsetup version to v2.0.1
libcryptsetup v2.0.1 introduced new API calls, supporting 64 bit wide
integers for `keyfile_offset`. This change invokes the new function
call, gets rid of the warning that was added in #7689, and removes
redundant #ifdefery and constant definitions.
See https://gitlab.com/cryptsetup/cryptsetup/issues/359.

Fixes #7677.
2019-10-01 13:34:06 +02:00
Zbigniew Jędrzejewski-Szmek fef716b28b cryptsetup: don't assert on variable which is optional
50d2eba27b (commitcomment-34519739)

In add_crypttab_devices() split_keyspec is called on the keyfile argument,
which may be NULL.
2019-08-01 09:49:24 +02:00
Zbigniew Jędrzejewski-Szmek 5d2100dc4c cryptsetup: use unabbrieviated variable names
Now that "ret_" has been added to the output variables, we can name
the internal variables without artificial abbrevs.
2019-08-01 08:13:13 +02:00
Lennart Poettering f4ea8432e6 cryptsetup-generator: fix coverity issue
Fixes coverity issue 1403772
2019-07-31 09:38:59 +02:00
shinygold 50d2eba27b cryptsetup: add keyfile-timeout to allow a keydev timeout and allow to fallback to a password if it fails. 2019-07-17 14:00:29 +02:00
Lennart Poettering 0e3cb57dae cryptsetup: no need to zero-initialize zero-initialized structures 2019-07-16 12:40:22 +02:00
Lennart Poettering b910cc72c0 tree-wide: get rid of strappend()
It's a special case of strjoin(), so no need to keep both. In particular
as typing strjoin() is even shoert than strappend().
2019-07-12 14:31:12 +09:00
Ivan Shapovalov 568a84048e cryptsetup: enable libcryptsetup debug logging if we want it
Even if we set a log callback that would accept debug messages, libcryptsetup
needs debug logging enabled explicitly for it to happen.
2019-07-08 16:02:41 +03:00
Ivan Shapovalov 8c71b2cd44 cryptsetup: set libcryptsetup global log callback too 2019-07-06 13:56:19 +03:00
Lubomir Rintel 844d5a87c9 cryptsetup-generator: fix luks-* entry parsing from crypttab
This reverts a part of commit 49fe5c0996 ('tree-wide: port various places
over to STARTSWITH_SET()') that replaced a pair of startswith() calls
with STARTSWITH_SET().

They were in fact for a different strings (device vs. name), botching
the crypttap parsing.
2019-06-28 10:18:59 +02:00
Michael Stapelberg 307472654c
cryptsetup: comment: crypt_setup → crypt_format 2019-06-27 09:38:30 +02:00
Lennart Poettering c6134d3e2f path-util: get rid of prefix_root()
prefix_root() is equivalent to path_join() in almost all ways, hence
let's remove it.

There are subtle differences though: prefix_root() will try shorten
multiple "/" before and after the prefix. path_join() doesn't do that.
This means prefix_root() might return a string shorter than both its
inputs combined, while path_join() never does that. I like the
path_join() semantics better, hence I think dropping prefix_root() is
totally OK. In the end the strings generated by both functon should
always be identical in terms of path_equal() if not streq().

This leaves prefix_roota() in place. Ideally we'd have path_joina(), but
I don't think we can reasonably implement that as a macro. or maybe we
can? (if so, sounds like something for a later PR)

Also add in a few missing OOM checks
2019-06-21 08:42:55 +09:00
Zbigniew Jędrzejewski-Szmek 665b0af866
Merge pull request #12636 from mbroz/master
Cryptsetup: call crypt_load() once for LUKS and support LUKS2 tokens
2019-05-30 14:27:22 +02:00
Roberto Santalla 38ab1ec9c8 cryptsetup: Add dependency for detached header
Fixes #12650

cryptsetup: Extract dependency check to a separate function

cryptsetup: style improvements, error checking
Return early on failure/nothing to do
Removed braces from single-line ifs
Check return value of  fstab_filter_options

cryptsetup: code style
2019-05-30 09:42:50 +02:00
Milan Broz 894bb3ca4c cryptsetup: Add LUKS2 token support.
LUKS2 supports so-called tokens. The libcryptsetup internally
support keyring token (it tries to open device using specified
keyring entry).
Only if all token fails (or are not available), it uses a passphrase.

This patch aligns the functionality with the cryptsetup utility
(cryptsetup luksOpen tries tokens first) but does not replace
the systemd native ask-password function (can be used the same in
combination with this patch).
2019-05-28 15:49:16 +02:00
Milan Broz ea9a9d49e4 cryptsetup: call crypt_load() for LUKS only once
The crypt_load() for LUKS2 can read a quite big area of disk
(metadata area size is configurable and can increase up to megabytes).

This initialization is not needed to be repeated, just use the existing context.

(This patch is also required for the following change.)
2019-05-28 15:31:25 +02:00
Milan Broz 2e4beb875b cryptsetup: Do not fallback to PLAIN mapping if LUKS data device set fails.
If crypt_load() for LUKS succeeds, we know that it is a LUKS device.
Failure of data device setting should fail in this case; remapping
as a PLAIN device late could mean data corruption.

(If a user wants to map PLAIN device over a device with LUKS header,
it should be said explicitly with "plain" argument type.)

Also, if there is no explicit PLAIN type requested and crypt device
is already initialized (crypt_data_type() is set), do not run
the initialization again.
2019-05-28 15:31:25 +02:00
Zbigniew Jędrzejewski-Szmek fdeea3f4f1 Add fopen_unlocked() wrapper 2019-04-12 11:44:52 +02:00
Michal Sekletar 48da47eeca cryptsetup-generator: set high OOM score for systemd-cryptsetup instances
With new LUKS2 header format it is possible to use Argon2 key derivation
function. This function is "memory-hard" hence keyslot unlocking can
potentially use a lot of RAM as this increases resistance to massively
parallel GPU based password cracking.

However, when multiple systemd-cryptsetup binaries run at the same
time it is very likely that system using Argon2 (e.g. Fedora 30)
will encounter memory-pressure during early boot, following OOM killing
spree.

This patch aims to lower the damage done by OOM killer and sets OOMScore
for systemd-cryptsetup units to 500. Hopefully OOM killer will then
shoot us down and leave rest of the system services alone.
2019-03-28 15:52:41 +01:00
Zbigniew Jędrzejewski-Szmek ca78ad1de9 headers: remove unneeded includes from util.h
This means we need to include many more headers in various files that simply
included util.h before, but it seems cleaner to do it this way.
2019-03-27 11:53:12 +01:00
Lennart Poettering d5d1ae153f cryptsetup: small refactoring 2019-03-20 10:48:33 +01:00
Lennart Poettering d8b4d14df4 util: split out nulstr related stuff to nulstr-util.[ch] 2019-03-14 13:25:52 +01:00
Yu Watanabe 2c65512ee7 cryptsetup: add same-cpu-crypt and submit-from-crypt-cpus options
Closes #11946.
2019-03-13 09:48:50 +01:00
Ryan Gonzalez c20db38875 cryptsetup: Treat key file errors as a failed password attempt
6f177c7dc0 caused key file errors to immediately fail, which would make it hard to correct an issue due to e.g. a crypttab typo or a damaged key file.

Closes #11723.
2019-02-26 10:48:07 +09:00
Tom Yan 142b8142d7 mount/generators: do not make unit wanted by its device unit
As device units will be reloaded by systemd whenever the corresponding device generates a "changed" event, if the mount unit / cryptsetup service is wanted by its device unit, the former can be restarted by systemd unexpectedly after the user stopped them explicitly. It is not sensible at all and can be considered dangerous. Neither is the behaviour conventional (as `auto` in fstab should only affect behaviour on boot and `mount -a`) or ever documented at all (not even in systemd, see systemd.mount(5) and crypttab(5)).
2019-02-15 00:16:54 +08:00
Lennart Poettering 6f177c7dc0 cryptsetup: rework how we log about activation failures
First of all let's always log where the errors happen, and not in an
upper stackframe, in all cases. Previously we'd do this somethis one way
and sometimes another, which resulted in sometimes duplicate logging and
sometimes none.

When we cannot activate something due to bad password the kernel gives
us EPERM. Let's uniformly return this EAGAIN, so tha the next password
is tried. (previously this was done in most cases but not in all)

When we get EPERM let's also explicitly indicate that this probably
means the password is simply wrong.

Fixes: #11498
2019-01-23 23:37:17 +01:00
Lennart Poettering 906962f312 cryptsetup: add comment what EAGAIN means when activating 2019-01-23 23:37:17 +01:00
Lennart Poettering 44ce425514 cryptsetup: downgrade a log message we ignore 2019-01-23 23:37:17 +01:00
Lennart Poettering aed68083c0 cryptsetup: don't line-break so aggressively 2019-01-23 23:37:17 +01:00
Lennart Poettering b7a0fead10 cryptsetup: add some commenting about EAGAIN generation 2019-01-23 23:37:17 +01:00