Commit Graph

155 Commits

Author SHA1 Message Date
Yu Watanabe 458610429f tree-wide: fix typo 2020-12-18 12:59:29 +09:00
Lennart Poettering 18843ecc2a cryptsetup: add support for TPM2 unlocking of volumes 2020-12-17 20:02:03 +01:00
Lennart Poettering 2bc5c425e6 cryptsetup: add fido2 support 2020-12-17 20:00:41 +01:00
Lennart Poettering b8c80b56d1 cryptsetup: split up attach_luks_or_plain_or_bitlk() into smaller functions
Just some refactoring.
2020-12-17 19:59:28 +01:00
Lennart Poettering b997d1115b cryptsetup: read PKCS#11 key and token info from LUKS2 metadata
Optionally, embedd PKCS#11 token URI and encrypted key in LUKS2 JSON
metadata header. That way it becomes very easy to unlock properly set up
PKCS#11-enabled LUKS2 volumes, a simple /etc/crypttab line like the
following suffices:

    mytest /dev/disk/by-partuuid/41c1df55-e628-4dbb-8492-bc69d81e172e - pkcs11-uri=auto

Such a line declares that unlocking via PKCS#11 shall be attempted, and
the token URI and the encrypted key shall be read from the LUKS2 header.
An external key file for the encrypted PKCS#11 key is hence no longer
necessary, nor is specifying the precise URI to use.
2020-12-17 19:59:24 +01:00
Lennart Poettering d3ad474f0c cryptsetup: be more careful with erasing key material from memory 2020-12-17 19:59:20 +01:00
Lennart Poettering 8414cd48e9 cryptsetup: split code that allocates udev security device monitor into its own function 2020-12-17 19:59:17 +01:00
Lennart Poettering da2268f9d7 cryptsetup: give command line parameters proper names
It's highly confusing to reference the command line parameters via
argv[] indexes. Let's clean this up, and introduce properly named local
variables that make this easier to follow.

No actualy code changes, just some renaming of variables.
2020-12-04 17:49:34 +01:00
Lennart Poettering e060ed32e4 cryptsetup: modify keyfile search logic to use read_file_full() too
Let's move the 3rd way how cryptsetup acquires key files to
read_file_full() too.

Since load_key_file()'s raison d'etre now is just the search path logic,
let's rename the function to find_key_file().
2020-12-01 14:27:01 +01:00
Lennart Poettering e2c2f868b2 cryptsetup: port cryptsetup's main key file logic over to read_full_file_full()
Previously, we'd load the file with libcryptsetup's calls. Let's do that
in our own, so that we can make use of READ_FULL_FILE_CONNECT_SOCKET,
i.e. read in keys via AF_UNIX sockets, so that people can plug key
providers into our logic.

This provides functionality similar to Debian's keyscript= crypttab
option (see → #3007), as it allows key scripts to be run as socket
activated services, that have stdout connected to the activated socket.
In contrast to traditional keyscript= support this logic runs stuff out
of process however, which is beneficial, since it allows sandboxing and
similar.
2020-12-01 14:17:47 +01:00
Lennart Poettering 4d1bb8f397 cryptsetup: port PKCS#11 code to read key file with read_full_file()
Now that we can read from offsets/with size, let's port the cryptsetup
PKCS#11 key file logic over to read_full_file_full().
2020-12-01 14:17:47 +01:00
Lennart Poettering 8bc6ade721 cryptsetup: use log_warning_errno() where we can, instead of log_warning() 2020-11-25 13:21:27 +01:00
Lennart Poettering 090685b5a1 cryptsetup: use strjoin() for concatenating strings 2020-11-25 11:19:06 +01:00
Yu Watanabe db9ecf0501 license: LGPL-2.1+ -> LGPL-2.1-or-later 2020-11-09 13:23:58 +09:00
Frantisek Sumsal d7a0f1f4f9 tree-wide: assorted coccinelle fixes 2020-10-09 15:02:23 +02:00
Zbigniew Jędrzejewski-Szmek 7bb553bb98 fstab,crypttab: allow escaping of commas
Fixes #17035. We use "," as the separator between arguments in fstab and crypttab
options field, but the kernel started using "," within arguments. Users will need
to escape those nested commas.
2020-09-25 13:36:34 +02:00
Zbigniew Jędrzejewski-Szmek be36bc1e14 cryptsetup: upgrade log line for option parsing error
If we failed here, we would exit with only a debug message.
2020-09-24 16:36:51 +02:00
Zbigniew Jędrzejewski-Szmek dd2fff3a18 cryptsetup: use extract_first_word() 2020-09-09 09:34:54 +02:00
Lennart Poettering 1e2f32305c shared: rename crypt-util.c → cryptsetup-util.c
"crypt-util.c" is such a generic name, let's avoid that, in particular
as libc's/libcrypt's crypt() function is so generically named too that
one might thing this is about that. Let's hence be more precise, and
make clear that this is about cryptsetup, and nothing else.

We already had cryptsetup-util.[ch] in src/cryptsetup/ doing keyfile
management. To avoid the needless confusion, let's rename that file to
cryptsetup-keyfile.[ch].
2020-09-02 15:00:32 +02:00
Luca Boccassi efc3b12fdb tree-wide: enable/disable libcrypsetup debug output depending on our level
Avoid always setting to debug, as it will incur in many more callbacks from
libcrypsetup that then get discarded, wasting resources.
2020-08-14 15:26:04 +01:00
Lennart Poettering 6930d069a3 cryptsetup: pass selected mode to crypt_load() when doing LUKS
This doesn't fix anything IRL, but is a bit cleaner, since it makes sure
that arg_type is properly passed to crypt_load() in all cases.

We actually never set arg_type to CRYPT_LUKS2, which is why this wasn't
noticed before, but theoretically this might change one day, and
existing comments suggest it as possible value for arg_type, hence let's
process it properly.
2020-06-09 14:24:21 +02:00
Lennart Poettering 5af39ac850 cryptsetup: try to load bitlocker superblock only if requested
let's do automatic discovery only for our native LUKS/LUKS2 headers,
since they are Linux stuff, and let's require that BitLocker to be
requested explicitly.

This makes sure cryptsetup without either "luks" nor "bitlk" in the
option string will work. Right now it would fail because we'd load the
superblock once with luks and once with bitlk and one of them would
necessarily fail.

Follow-up for #15979
2020-06-09 14:24:15 +02:00
Maxim Fomin 6cc27c29ad Add 'bitlk' option to mount Bitlocker drives with cryptsetup. 2020-06-09 08:12:55 +02:00
Lennart Poettering 53ac130b51 cryptsetup: support tmp= file system argument
Let's catch up with Debian a bit more.

This also changes the default from ext2 to ext4.

Fixes: #952
2020-05-26 17:11:34 +02:00
Lennart Poettering 4eb08bdb71 cryptsetup: fix minor indentation issue 2020-05-19 17:28:43 +02:00
Lennart Poettering 0ba6f85ed3 cryptsetup: optionally, see if empty password works for unlocking the file system
This adds a new switch try-empty-password. If set and none of PKCS#11 or
key files work, it is attempted to unlock the volume with an empty
password, before the user is asked for a password.

Usecase: an installer generates an OS image on one system, which is the
booted up for the first time in a possibly different system. The image
is encrypted using a random volume key, but an empty password. A tool
that runs on first boot then queries the user for a password to set or
enrols the volume in the TPM, removing the empty password. (Of course, in
such a scenario it is important to never reuse the installer image on
multiple systems as they all will have the same volume key, but that's a
different question.)
2020-05-19 17:28:40 +02:00
Lennart Poettering 2424fb7e7b cryptsetup: fix minor memory leak 2020-05-19 17:28:36 +02:00
Lennart Poettering cb6c9283b5 cryptsetup: make sure to return EAGAIN on wrong tcrypt password too
Only then we'll try again to ask the user for a password.

Fixes: #12152
2020-05-19 17:28:29 +02:00
Lennart Poettering 7407f68980 cryptsetup: automatically load luks keys off disk
Let's make loading of keys a bit more automatic and define a common
place where key files can be placed. Specifically, whenever a volume of
name "foo" is attempted, search for a key file in
/etc/cryptsetup-keys.d/foo.key and /run/cryptsetup-keys.d/foo.key,
unless a key file is declared explicitly.

With this scheme we have a simple discovery in place that should make it
more straightfoward wher to place keys, and requires no explicit
configuration to be used.
2020-05-19 17:28:25 +02:00
Lennart Poettering d3d49e7649 cryptsetup: optionally remove key file after use
This is useful when the key file is acquired dynamically in some form
and should be erased after use.

Note that this code tries to be robust, and removes the key file both on
success and on failure.
2020-05-19 17:28:13 +02:00
Lennart Poettering 8ced40c09b cryptsetup: catch up with debian crypttab options a bit
Support some aliases Debian added, and drop some options that Debian
dropped from our list of unsupported options.
2020-05-19 17:28:09 +02: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 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
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
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
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
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
Michael Stapelberg 307472654c
cryptsetup: comment: crypt_setup → crypt_format 2019-06-27 09:38:30 +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 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