Commit Graph

642 Commits

Author SHA1 Message Date
Ondrej Kozina
917b6836a9 Always use json-c types in internal code.
Sometimes we used "struct json_foo_bar" and sometimes "json_foo_bar"
json-c defined type. Stick with one notation in internal code.
2025-11-12 13:13:02 +01:00
Ondrej Kozina
b36d4be8fa opal: supress confusing debug messages.
Do not print sed-opal spefic debug messages with confusing
error codes if ioctl() call failed with -1. Usually that means
the kernel does not support sed-opal interface or the requested
ioctl number is not implemented.
2025-11-06 11:14:56 +01:00
Ondrej Kozina
0a8e7da7ae opal: fix debug message on failed sed-opal ioctl.
ioctl syscall always returns -1 on error (see ioctl(2)).
On error the actual reason is reported via errno varible.
Let's store the original errno code in the variable
so that it can be printed out in debug mode.

Before this fix the debug message always reported "Operation not
permited" (the translation of errno EPERM (1)).
2025-11-06 11:09:43 +01:00
Ondrej Kozina
83a7310ca2 opal: do not initialize LRs array in activation.
The lr member in opal_lr_act kernel structure is
ingnored unless the device is being activated in SUM
mode.

See kernel implementation of IOC_OPAL_ACTIVATE_LSP
in block/sed-opal.c
2025-11-03 16:05:08 +01:00
Ondrej Kozina
441802773f opal: simplify User setup routine.
Reduce memory copying by reusing nested structure in opal_lock.
2025-11-03 16:05:08 +01:00
Ondrej Kozina
cc66b1fa52 opal: pull User setup in separate function. 2025-11-03 16:05:08 +01:00
Ondrej Kozina
a0d5d2bf5e opal: pull individual range setup in separate function. 2025-11-03 16:05:08 +01:00
Ondrej Kozina
61dbb69319 opal: pull reuse of active device in separate function. 2025-11-03 16:05:08 +01:00
Ondrej Kozina
32b33541a8 opal: pull LSP activation in separate function. 2025-11-03 16:05:08 +01:00
Ondrej Kozina
346db2e42a opal: add a named constant for TCG FAIL status.
Will be checked upon later when we add support
for OPAL2 SUM Reactivate method.
2025-11-03 16:05:08 +01:00
Ondrej Kozina
4d98add260 opal: Submit PSID reset command to R/W file descriptor.
The PSID reset erases the block device it's submitted to
succesfully.

By submitting the command to read-only fd previously
there were partition device nodes still visible in
the /dev directory because kernel does not trigger rescan
after OPAL2 PSID reset. Even though all the partition were
actually erased (including the partition table).

We workaround the issue by submitting the PSID reset
to R/W fd so that it triggers rescan event on close.
2025-10-06 10:37:37 +02:00
Milan Broz
0eaaa4553e Fix handling of too long label and subsystem fields
These LUKS2 labels are stored in the binary header area that has limited size.

While we have been silently truncating strings here, it is something that
is not expected, as the final label is then different than expected.

Let's fix the code to explicitly print and return error here.

Also remove the comment about duplicate check. It is incorrect  optimization,
as some users will expect a real write on disk, we should no skip it.

Fixes: #958
2025-10-01 21:41:55 +02:00
Milan Broz
b0821b11a5 Add the same cipher_null restriction to LUKS2 keyslot as in LUKS1.
LUKS2 keyslot cannot be created with cipher_null using standard
tools, but activation of such a keyslot is allowed.

As this can be confusing and create a false sense of security,
let's apply the same restriction as in LUKS1 - such a keyslot
is used only with an empty passphrase.

This will reject activation with a real password, avoiding
possible activation of insecure LUKS containers.

Fixes: #954
2025-08-18 08:22:51 +02:00
Milan Broz
098d2122e5 Remove redundant test in LUKS2 keyslot cipher incompatible test.
The null_cipher is tested twice there.
2025-08-18 08:22:51 +02:00
Milan Broz
b7e3ea592b Avoid warning about unused parameter.
This fixes compilation warning:
   error: unused parameter 'version' [-Werror=unused-parameter]
2025-08-01 16:48:08 +02:00
Ondrej Kozina
773ac1ce55 Support encryption with reduced device size and data shift.
The only mising bit in library is a check if reduced data
size is smaller than real device size minus data shift.
2025-06-02 17:08:43 +02:00
Ondrej Kozina
ed50d30e7e Decouple data device size from reencryption operation data size.
Encryption with data shift and moved segment used to change
data size attribute later than necessary. Make it obvious in
upper layers.
2025-06-02 17:05:38 +02:00
Ondrej Kozina
5973694311 Correctly suffix device related variables.
We introduced _bytes and _sectors variables some time ago.
2025-06-02 17:05:03 +02:00
Milan Broz
b81c84bd8f Do not allow null cipher for LUKS2 keyslot even in conversion. 2025-06-02 13:58:33 +02:00
Milan Broz
21e0680abf Disallow conversion to LUKS1 if capi string is specified.
While it works, it is a bug and we block it already in luksFormat.
2025-05-29 13:55:56 +02:00
Milan Broz
5b3ff3c0a7 Explicitly disallow capi format for LUKS2 keyslot encryption
NO functional change as it fails in IV check anyway, but the
code should not reach this funtion at all.
2025-05-29 13:55:56 +02:00
Milan Broz
55c44cd3fd Fix warning for hidden global variable
The requirements_flags is a global array, do not
name a local variable the same (CodeQL fix).
2025-05-28 14:05:30 +02:00
Ondrej Kozina
12eb040943 Create dm-integrity with CRYPT_SUBDEV prefix.
When activating dm-integrity device underneath dm-crypt
with LUKS2 authenticated encryption mode, annotate the
device correctly with CRYPT_SUBDEV prefix. This will help
us to clearly identify dependent device underneath LUKS2 top
level device and we can unify the deactivation code in future
releases.

We have general code deactivating dependent devices already for
LUKS2 reencrytion.

Deactivating newly created devices with pre 2.8.0 cryptsetup
will issue warning about missing devices but the deactivation
will succeed.
2025-05-26 15:35:41 +02:00
Milan Broz
880bbfab4d Use free physical memory check for pbkdf only on small systems.
This hack tries to workaround situation when small VMs without swap
causes OOM. This hack will be removed one day completely...

Also remove confusing warning about possible crash.
With OpenSSL Argon2 backend this behaves much better, but it still
can cause OOM instead od returning ENOMEM.
Anyway, the warning message causes more problems that it solves.

Fixes: #896
2025-05-26 12:45:09 +02:00
Milan Broz
f5bbc499d4 Move LUKS_STRIPES definition to macros definitions.
To be used in repair code later.
2025-05-22 16:09:50 +02:00
Milan Broz
324926e2d8 LUKS2: support Inline tags format and activation for integrity protection 2025-05-22 14:53:16 +02:00
Milan Broz
3ec14c8668 Move (and rename) UUID helpers to libdevmapper source. 2025-05-22 14:53:12 +02:00
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