Commit Graph

636 Commits

Author SHA1 Message Date
Ondrej Kozina
47ac021c03 Refactor LUKS2 encryption parameters verification.
Code verifying encryption parameters needs to be reusable
for new code that will be added later.

Also due to previous changes to data offset and metadata size
calculations, encryption parameters can now be verified at
single place without need to split it over crypt_format_luks2
routine.
2023-05-29 11:09:04 +02:00
Ondrej Kozina
969e67e743 Use defined constant for cipher_spec buffer size. 2023-05-29 11:09:04 +02:00
Ondrej Kozina
6a8fa14007 Move cipher parsing outside LUKS2 header generator function.
Let's make LUKS2_generate_hdr as clean as possible. Cipher
specification string can be constructed in upper layers.

This will make future LUKS2_generate_hdr extension easier.
2023-05-29 11:09:04 +02:00
Ondrej Kozina
926679f7f1 Refactor LUKS2 metadata parameters calculations.
Move all metadata size and data offset calculations
logic away from LUKS2_generate_hdr. The function
was meant to generate solely LUKS2 header on disk json
format.

The aim is to have all logic related data offset and metadata
size in one place available to be calculated in advance so
that we can easily extend the code.
2023-05-29 11:08:29 +02:00
Milan Broz
dff9ee8c8c Also disallow active devices with internal kernel names.
The same problem fixed in commit 438cf1d1b3
is present in libdevmapper wrapper when parsing active device table.

The whole point of conversion was that non-authenticated modes
can be always represented in the old cipher-mode-iv format.
As the internal names contains dash, these are unsupported.

That said, the libdevmapper backend now correctly returns
full cipher specification including capi prefix for this case.

Init_by_name call now fails with incomplatible cipher definition error.
2023-05-02 15:42:21 +02:00
Vojtech Trefny
bc426bba67 bitlk: Fix segfaults when attempting to test volume key
Also clarify that checking the volume key is not possible for
BITLK in the docstring and man page.

Fixes: #810
2023-04-18 16:38:58 +02:00
Milan Broz
e4c2aa64b5 Detect DAX devices and and warn in LUKS format.
DAX / persistent memory devices do not provide atomic sector updates,
any single modification can corrupt the whole encryption block.
2023-03-31 10:51:34 +00:00
Milan Broz
9d5327c37b Fix sector_size display for non-LUKS2 crypt devices. 2023-02-21 08:32:39 +00:00
Milan Broz
1d109a114c Fix integrity info display for non-LUKS2 crypt devices. 2023-02-21 08:32:39 +00:00
Milan Broz
e455110c8e Fix crypt_init_by_name() for dm-crypt with integrity.
Initialization by name for dm-crypt with integrity is always
underlying device for dm-integrity target, not dm-integrity
device itself.

This fixes various problems like refresh command or
device printed in status command.

Fixes: #801
2023-02-21 08:32:39 +00:00
Milan Broz
72f799b393 Update Copyright year. 2023-02-09 17:11:18 +01:00
Milan Broz
0622b51634 verity: fix hash offset 64bit values
Hash offset is 64bit values, for some reason it is
used as size_t on one place. Fix it by properly use uint64_t.

Fixes: #792
2023-01-17 13:16:30 +01:00
Ondrej Kozina
be088b8de8 Enable crypt_header_is_detached for empty contexts.
Also changes few tests now expecting crypt_header_is_detached
works with empty contexts.
2022-12-14 09:52:19 +01:00
Milan Broz
de221b4ea7 Fix typo in comment. 2022-12-08 14:17:50 +01:00
Milan Broz
170161b9b6 Free all possible allocated params if crypt_load() fails.
If format load fails in some intermediate step, the internal
params struct can contain already set values.
While context is set still to none type, it can cause segfault
in releasing active_name.

(Found by fuzzing target processing crypt_load.)
2022-12-08 14:17:44 +01:00
Milan Broz
a649d734b6 Let crypt_set_null_type wipe whole context always.
We have to be sure that after setting new type some
union is not misused.
2022-12-08 13:02:18 +01:00
Milan Broz
15c998d523 Move crypt_free_type and allow force type override.
Will be used later on error path.
2022-12-08 13:02:18 +01:00
Ondrej Kozina
cdfa213ad0 Allocate internal buffer in LUKS2 keyring token with crypt_safe_alloc.
With changes in db65a5ceac and subsequent
drop of library memlock_all we should lock keyring key material buffer
in memory system memory as well.
2022-11-24 09:03:29 +00:00
Ondrej Kozina
cd8f80b7ee Clarify type requirements in crypt_volume_key_get_by_keyslot_context. 2022-11-21 15:56:14 +01:00
Milan Broz
819902a33a Add a better warning if luksFormat ends with image without any space for data.
Header write can call falloc() to increase image size, so we should
check data offset after header is written.

Also change wording to be less cryptic and describe what is the real problem.

Note that the code can be used this way to crate detached header (without
space for data), so it is not an error.
2022-11-18 13:03:52 +00:00
Ondrej Kozina
81c56a8395 Move assert include in internal header files. 2022-11-16 16:54:33 +01:00
Pavel Tobias
cb9deaf354 Fvault2: implement open 2022-11-14 21:50:18 +01:00
Pavel Tobias
0ce5de9c1c Fvault2: implement dump 2022-11-14 21:50:18 +01:00
Pavel Tobias
1ffc9d967c Fvault2: prepare module in libcryptsetup 2022-11-14 21:50:18 +01:00
Milan Broz
871000fa05 Fix a memory leak in crypt_keyslot_add_by_key.
Found by clang address sanitizer.

Also rename the variable - i's no longer a bare pointer,
the vk also owns the memory [okozina].
2022-11-07 17:30:14 +00:00
Ondrej Kozina
55c39d7d16 Port crypt_volume_key_get internals to keyslot context variant. 2022-11-03 15:56:37 +01:00
Ondrej Kozina
f7e2ed956b Add crypt_volume_key_get_by_keyslot_context.
Extends avaiable methods for retrieving device volume key.
The volume key now may be extracted using passphrase, keyfile
(passphrase in a file) or token (LUKS2 only).

For LUKS devices, it returns generated volume key after
sucessfull crypt_format where new volume key got generated.

Fixes: #777.
2022-11-03 15:56:37 +01:00
Ondrej Kozina
0e6264c53c Do not cache volume key in keyslot context.
First, there was a bug where passphrase based
keyslot contextets did not cache volume keys
properly and caused leaks.

Second, it causes problems when keyslot context
is used twice with different keyslot id, e.g.:
CRYPT_ANY_SLOT vs specific id, unbound key vs
volume key, etc.
2022-11-02 12:22:25 +01:00
Ondrej Kozina
3e4c69a017 Fix internal crypt segment compare routine.
The function is supposed to check if manipulated
active dm-crypt device matches the on-disk metadata.
Unfortunately it did not take into account differences
between normal cipher specification (aes-xts-plain64)
and capi format specification (capi:xts(aes)-plain64).
The internal query function always converted capi format
in normal format and therefor failed if capi format was
used in metadata.

Fixes: #759.
2022-10-20 14:24:11 +02:00
Milan Broz
82b56300cd Wrap some very long lines. 2022-10-01 22:35:57 +02:00
Ondrej Kozina
50803ebacb Switch existing keslot adding API to new extension. 2022-09-29 17:31:29 +02:00
Ondrej Kozina
b867f0b578 Add new API for adding new LUKS keyslots.
The crypt_keyslot_add_by_keyslot_context & associated
helper functions allow more options when adding new
keyslot. For example there was no simple way of
adding new LUKS2 keyslot when the only active keyslot
could be unlocked by passphrase (KEK) provided by LUKS2 token
(plugin). Now all available options for unlocking keyslots
may also be used when creating new keyslot and it combine
as called needs.

The available methods (keyslot contexts) are:
passphrase, keyfile, key (binary representation) and LUSK2 token.
2022-09-21 17:36:28 +02:00
Ondrej Kozina
eac02f5605 Fix NULL key_description bug in luks2-keyring token.
json-c parser transforms NULL pointer into special '(null)' string.
While being technically correct it hides the fact user passed NULL
pointer in crypt_token_params_luks2_keyring structure. This bug
could be trigerred by calling crypt_token_luks2_keyring_set().
2022-09-01 15:38:49 +02:00
Ondrej Kozina
01c032df04 Do not reload LUKS2 metadata when not necessary.
Following API calls trigerred LUKS2 metadata reload
from storage in case of failure:

crypt_convert
crypt_keyslot_add_by_key
crypt_keyslot_add_by_keyfile_device_offset
crypt_keyslot_add_by_passphrase
crypt_keyslot_change_by_passphrase
crypt_reencrypt_init_by_keyring
crypt_reencrypt_init_by_passphrase

This patch replaces LUKS2 metadata reload with
backup LUKS2 metadata copy kept in memory that is updated on
each sucessfull metadata write and rolled back to it whenever
needed in any of those calls listed above.
2022-08-19 14:46:42 +02:00
Milan Broz
88d9524e6c Mark crypt_memory_lock() API call deprecated.
And remove its implementation.
2022-08-16 19:56:32 +02:00
Ondrej Kozina
3c8b3201d7 Improve crypt_reencrypt_status return values.
Empty context or any non-LUKS types now returns
CRYPT_REENCRYPT_INVALID value.

For LUKS1 devices return CRYPT_REENCRYPT_NONE
(since any LUKS1 device in legacy reencryption
does not have valid LUKS1 header/metadata).
2022-06-27 16:01:50 +02:00
Milan Broz
97a22c27dd Make crypt_load quiet if metadata is not detected.
Ths will allow automatic scan of known formats.

Errors are printed only if something is wrong with already detected metadata.

This change means that it is responsibility of the caller to print an error
message if needed.

Also fix some places without a message.

Fixes: #642
2022-05-26 10:17:32 +02:00
Vojtech Trefny
6c73057156 integrity: Fix integrity_key_size for algorithms without keys
INTEGRITY_key_size returns -EINVAL for algorithms without a key
and because crypt_params_integrity.integrity_key_size is an
unsigned integer we get key size 4294967274 instead of more
appropriate 0 for these algorithms.
2022-05-22 19:58:00 +02:00
Milan Broz
251eb37c4a Do not try to print NULL string in destructor. 2022-05-18 16:39:38 +02:00
Milan Broz
9e7894081f Verity: dump device sizes.
Calculating device sizes for verity devices is a little bit tricky,
Data, hash and FEC can share devices or it can be a separate devices.

This patch prints used device sizes in veritysetup dump command,
but it requires that user specifies all values that are not stored
in superblock (like a FEC device and FEC roots).
2022-05-09 22:48:59 +02:00
Milan Broz
0c80ee6c28 Move verity dump to per-format directory. 2022-05-09 13:47:16 +02:00
Milan Broz
853abf53f5 Fix debug message for crypt_activate_by_signed_key.
If name is not uses, there are two spaces.
Also it is more readable now.
2022-05-09 13:46:58 +02:00
Milan Broz
2bf0f537f6 Add constant time memcmp and use it for comparing keys.
There is perhaps no problem now, but it is a good practise to use
constant time for key comaprison to avoid possible side channel
issues.
2022-04-28 15:19:23 +00:00
Milan Broz
ceed3c0c3b Introduce crypt_log_hex helper and use it for log_std output. 2022-04-28 08:11:58 +00:00
Ondrej Kozina
cc107ee20e Silent crypt_volume_key_verify call.
And moves the error message in tools instead.
2022-04-25 19:47:09 +00:00
Milan Broz
f391f4baf1 Fix memory leaks in integrity resize with keyed parameters.
The whole game with reallocating keys is not needed.

(Perhaps not even for crypt type, but that is not part of this patch).
2022-04-22 11:34:13 +02:00
Milan Broz
ed13852899 Fix reload integrity device.
The offset cannot be referenced from the crypt union for integrity type.

Keyring setting key works only for crypt devices.

Also reformat long lines.
2022-04-21 08:21:01 +00:00
Milan Broz
efc1590405 Fix formatting warning for a 32bit arch. 2022-04-21 08:21:01 +00:00
Milan Broz
f1eea3a4b3 Clean reencrypt status struct for API call.
This function should not return unitialized struct as there
is no indication that it failed and caller can access it.

Also fixes a Coverity warning.
2022-04-19 08:53:44 +00:00
daniel.zatovic
d20d41c7a5 Add log messages, when kernel doesn't support resize. 2022-03-24 11:38:16 +01:00