Commit Graph

615 Commits

Author SHA1 Message Date
Milan Broz
d967c9aaf0 Unify checking for LUKS2 intermediate device.
Use dm_get_active_iname that should be used on all places.

This function return integrioty device name if it shoudl be
maintained by LUKS2 context directly.

Code must not touch other devices that it does not own.
2025-05-22 14:53:03 +02:00
Milan Broz
8440e59b7b Fix possible integer overflow (for unused variable). 2025-05-22 10:31:22 +02:00
Milan Broz
465043300d Check for unbound keyslot in convert.
The type cannot be converted to LUKS1 if there is an unbound keyslot.

It is already covered by digest count check, but in some specific
use cases the explicit check can catch a new problem.
2025-05-20 18:24:01 +02:00
Milan Broz
11dd8d318c Add debug messages for incompatible LUKS1 conversion.
If LUKS2 keyslot is not compatible, print debug message with more
specific info.

Fixes: #850
2025-05-20 18:23:48 +02:00
Ondrej Kozina
5a84dc87e3 Return -ENOENT if digest is missing.
If there is no digest associated with segment,
for example during reencryption mode encrypt initialization,
return -ENOENT in LUKS2_digest_verify_by_segment.
2025-05-20 15:29:14 +02:00
Ondrej Kozina
1a7e89c55d Allow crypt_reencrypt_init_by_keyslot_contexts with no active keyslots.
With this commit reencryption can run without any active
keyslot containing current (or optional future) volume key.

In such case new volume key must be provided via CRYPT_KC_TYPE_KEY
keyslot context and by adding CRYPT_REENCRYPT_CREATE_NEW_DIGEST flag in
reencryption parameters during reencryption initialization in
crypt_reencrypt_init_by_keyslot_contexts.

The new flag can not be combined with CRYPT_REENCRYPT_RESUME_ONLY
flag.
2025-05-20 15:29:14 +02:00
Ondrej Kozina
4e94c8d809 Add crypt_get_old_volume_key_size API.
It allows to get former (old) volume key size
from LUKS2 device in reencryption state when
there's at least one keyslot containing encrypted
volume key.
2025-05-20 15:21:44 +02:00
Ondrej Kozina
c1b5e412c9 Drop unreachable error message.
LUKS2_reencrypt_status can not return CRYPT_REENCRYPT_INVALID.
2025-04-27 14:14:13 +02:00
Ondrej Kozina
fa84cb8a55 Fix LUKS2_config_get_requirements prototype.
It never returns any error. Make it void.
2025-04-27 14:14:13 +02:00
Milan Broz
7f0724f46c Opal2: Do not use IOC_OPAL_ERASE_LR
The IOC_OPAL_ERASE_LR uses Erase method, that is defined only
in Single user mode (SUM) and works only on SUM-enabled LRs.

As we do not use SUM yet, this always fails.

Moreover, Erase has many side effects - it resets user password to ""
and disables locking for LR.

We already use fallback to IOC_OPAL_SECURE_ERASE_LR, which is GenKey
method (defined in Core spec) that must be always available.
It effectively regenerates the LR encryption key.
2025-03-24 12:34:43 +01:00
Ondrej Kozina
c1b7ad8979 Fix the debug message for too large json data during write. 2025-03-07 15:44:41 +01:00
Ondrej Kozina
cb0f568932 Optimize LUKS2 metadata writes.
LUKS2 supports several jsom area length configurations. With
the largest size supported in megabytes we do not want to write full
metadata area unconditionaly (current code) with every metadata
update. This might generate noticeble overhead with LUKS2
reencryption.

With this patch we write only the real used json area
length plus necessary padding to overwrite remaining previous
metadata stored on the disk.

During LUKS2 format and LUKS2 autorecovery we always overwrite
whole json metadata area no matter the used size.
2025-03-07 15:44:41 +01:00
Ondrej Kozina
000f03ad31 Switch to crypt_jobj_to_string_on_disk().
Use single function when we require json format
string representation for on disk format.
2025-03-07 15:44:41 +01:00
Ondrej Kozina
07e8628940 Add helper to unify json string format.
Add a single function to use when generating
json format string representation for on disk
storage purposes so that it can be easily
reused when needed.
2025-03-07 15:44:41 +01:00
Ondrej Kozina
0a6ee8633a Switch to volume key keyring helpers.
And do not access volume key internals bearing
information whether the key payload was uploaded
in kernel keyring or not.
2025-03-06 17:17:36 +01:00
Ondrej Kozina
5615488196 Refactor and unify naming conventions for internal keyring helpers.
Functions related to uploading/unlinking volume keys in user
requested kernel keyrings are named as follows:

crypt_single_volume_key_load_in_custom_keyring
crypt_volume_key_load_in_custom_keyring
crypt_unlink_key_from_custom_keyring

helpers for unlinking LUKS2 volume keys from thread keyring:

crypt_unlink_key_from_thread_keyring
crypt_unlink_key_by_description_from_thread_keyring
2025-03-06 17:17:36 +01:00
Milan Broz
f07854ab4f Make libdevmapper internal flags 64bit. 2025-02-16 20:52:37 +01:00
Milan Broz
c497945ab3 Make internal dm_flags 64bit. 2025-02-16 20:52:37 +01:00
Ondrej Kozina
6ee76934fa Harden and limit access to volume key internals.
The volume key structure may often be in configuration
where 'key' member does not contain real data. Some
examples:

- volume key acquired by querring device-mapper where key
  was originaly passed by kernel keyring reference.

- volume key allocated by crypt_alloc_volume_key(size, NULL)

With this patch access to internal 'uninitialized' data result
in failed assert().

For use cases where key data are not needed (keyring reference wrapper,
key length info only) we do not have to allocate and lock the safe
buffer in memory.

Further improvements might to completely hide the volume key internals
and access only via setter and getter functions.
2025-02-16 18:00:20 +00:00
Ondrej Kozina
54d937dfc7 Switch away from accessing volume key internals directly.
Switch current code to use following volume key helpers
for accessing internal properties:

crypt_volume_key_length(), crypt_volume_key_get_key(),
crypt_volume_key_description() and crypt_volume_key_kernel_key_type()

Remaining direct access to volume key internals will be dealt with in
later commits since it requires some further changes.
2025-02-16 18:00:20 +00:00
Ondrej Kozina
a965a88992 Add const to pointer in blob_serialize() function.
the data is read-only in this context.
2025-02-06 11:41:30 +01:00
Milan Broz
a45fb0a90d Do not include dlfcn.h if external token support is disabled.
Fixes: #925
2025-01-27 12:33:52 +00:00
Milan Broz
1e441bf75c Use #if for rest of defines. 2025-01-27 11:05:08 +00:00
Milan Broz
e5405f2fd8 Use #if for HAVE*_H defines. 2025-01-27 11:05:08 +00:00
Milan Broz
ff81791308 Use #if for ENABLE_AF_ALG. 2025-01-27 11:05:08 +00:00
Milan Broz
f64f6fb9e8 opal: Fix error table offset
The error table was partially wrong (for codes >0x0b)

Let's use exact enum codes according to TCG Core spec (5.1.5).

This also fixes CodeQL warning.
2025-01-27 11:04:25 +00:00
Milan Broz
5c795885c5 opal: update copyright 2025-01-27 11:04:25 +00:00
Milan Broz
6a8f88ea99 Fix leaks detected by Coverity. 2025-01-27 09:11:40 +00:00
Milan Broz
8364178b38 Update copyright year. 2025-01-15 23:09:36 +01:00
Milan Broz
b18cefcc71 Detect out of memory in KDF in keyslot derivation.
For OpenSSL and gcrypt we can detect that Argon2 failed on malloc,
report it to user (and return ENOMEM instead of EINVAL).
2024-12-12 22:45:21 +00:00
Milan Broz
5eda5f6a38 Fix compatibility for older dmcrypt without integrity_key_size
For older kernel an default HMAC key size we must not set
integrity_key_size option.
2024-12-03 20:28:00 +01:00
Milan Broz
7b5ac650e5 Allow specific integrity key size.
This patch add support for setting of integrity key size
for LUKS2 devices.

It adds new (optional) JSON "key_size" attribute in segment.integrity JSON object.
If not set, the code use hash length size (backward compatible).

For LUKS2, we do not allow smaller keys than 128 bits.

Mostly based on code from Ingo Franzki <ifranzki@linux.ibm.com>
2024-12-03 20:25:54 +01:00
Milan Broz
ff3e2c6a43 libdevmapper: Support dm-crypt integrity_key_size option
This patch implement support for setting specific integrity key size
option in dm-crypt, available since dm-crypt version 1.28.0.

This can be used for setting non-standard HMAC key length.

Mostly based on code from Ingo Franzki <ifranzki@linux.ibm.com>
2024-12-03 20:25:54 +01:00
Milan Broz
ae4b4ff4d7 Unlink only volume keys that were previously stored in keyring.
This is only preparation for an extension later, however, the volume
keys should not be unloaded unconditionally from keyring.

Note that all other places dropping keys already check that keys
were uploaded through key ID setting.
(And for suspend unconditional unlink make sense too.)
2024-11-25 13:20:36 +01:00
Milan Broz
9575dadc8b Store keyring type in volume key.
The key_decripion always contains only a key name,
keyring then contains type of keyring as defned un keyring utils.

For now, only LOGON type is used in commands, it will be extended later.
2024-11-25 13:19:42 +01:00
Milan Broz
6be70a0157 Do not include luks2/luks2_internal.h ouside of LUKS2 internal implementation.
The luks2_internal.h contains specific JSON implementations while
luks2.h is generic. Code outside of luks2/ dir should not use internals.

Also luks2.h includes functions prototypes that are used from setup.c.
2024-11-23 20:52:24 +01:00
Ondrej Kozina
3c5aa4ef59 Remove unused code.
Due to complete shift to device activation via keyslot context,
remove all remaining internal code activating device by passphrase
or by token.
2024-11-08 15:46:28 +01:00
Ondrej Kozina
dd3441621a Fix reencryption keys loading in kernel keyring.
Manage keys upload in kernel keyring (internal logon type for dm-crypt
segments) in single location in-before device activation.
2024-11-08 15:46:09 +01:00
Ondrej Kozina
6506d324ee Better name routine verifying any available digest.
also drop unused parameter from internal routine.
2024-11-08 14:09:08 +01:00
Milan Broz
33ed0d587e Fix typos found by lintian. 2024-10-30 19:11:29 +01:00
Ondrej Kozina
651a089f38 Add crypt_reencrypt_init_by_keyslot_context.
It allows initializing reencryption operation by
providing access to volume keys via keyslot context.
2024-10-30 13:25:57 +01:00
Ondrej Kozina
0eccd6e194 Check also CRYPT_ANY_SLOT in LUKS2_keyslot_for_segment.
The function did not work properly if keyslot parameter
was set to CRYPT_ANY_SLOT and returned always error.

But it will be used later when we need to find if there's
at least one keyslot asigned to a specific segment.
2024-10-30 13:25:55 +01:00
Ondrej Kozina
81d8c12cbe Fix LUKS2 decryption when volume key digest id not zero.
During the initialization phase future moved data segments
were incorrectly assigned (hardcoded) to digest with id 0.

When the default segment was assigned to a different value,
the initialization failed and was aborted.

This patch fixes the issue by assigning intermediary segments
to corect digest id.

Reported-by: 谢致邦 (XIE Zhibang) <Yeking@Red54.com>
2024-10-11 14:55:08 +02:00
Ondrej Kozina
c0bf271bef Add crypt_safe_memcpy in reencrypt digest routine.
We copy volume keys for purpose of calculating LUKS2
reencryption digest. Add crypt_safe_memcpy in serialization
function as well.
2024-09-12 11:08:16 +02:00
Ondrej Kozina
63bb997b41 Abort online reencryption for misconfigured devices.
Hard abort is justified here. The online reencryption on
data devices that do not support O_DIRECT io flag is
dangerous and leads to data corruption. This should be
impossible to hit due to a patch that handles it
in initialization phase. Better safe than sorry.
2024-08-27 14:00:41 +00:00
Ondrej Kozina
9991cbc306 Harden online reencryption checks in initialization phase.
Verify the data device supports O_DIRECT io flag in
the initialization phase. Online reencryption is not
safe unless we can read and write the data in direct
mode.
2024-08-27 14:00:41 +00:00
Milan Broz
5fb6d711c1 Fix clang possible rh NULL dereference warning. 2024-07-10 09:48:10 +02:00
Milan Broz
8b21b4b2ee Fix "allocated buffer size is not a multiple of the pointee's size" warning
No need to play games with void pointers, two separate functions are
much more readable.
2024-07-10 09:47:56 +02:00
Milan Broz
98b4243432 Add support for high-priority dm-crypt flag.
This patch add new --perf-high_priority cryptsetup option
for flga added in Linux kernel 6.10, dm-crypt target version 1.26.
2024-06-18 11:00:38 +00:00
Milan Broz
b19170e8fe Fix warning for printf argument. 2024-06-06 13:09:23 +02:00