Loading a table that pairs an authenc() cipher with the tcw IV mode and a short key causes an oops. The tcw mode reserves key_extra_size bytes of the key, and crypt_subkey_size subtracts them from key_size. For a key shorter than key_extra_size, the unsigned subtraction wraps, and crypt_copy_authenckey tries to copy about 4 GiB into authenc_key. Triggering this requires CAP_SYS_ADMIN. The key size check in crypt_iv_tcw_ctr comes too late, because it runs after crypt_set_key. Move the check into crypt_ctr_ivmode, which runs before crypt_set_key. Found by LLM-assisted review. Fixes: ef43aa38063a ("dm crypt: add cryptographic data integrity protection (authenticated encryption)") Cc: stable@vger.kernel.org Signed-off-by: Ben Cressey --- The patch applies to 6.18.y and 7.3.y. 6.12.y needs a context fix in crypt_iv_tcw_ctr, and older trees were not checked. Moving the check has a side effect. A key that the check refuses no longer reaches crypt_set_key, so its hex string in the ioctl buffer is wiped only if the caller sets DM_SECURE_DATA_FLAG. Other errors before crypt_set_key, such as an invalid IV mode, behave the same way. Tested under QEMU TCG on x86_64 with KASAN. A table with an authenc() cipher and a short tcw key is now refused with "Wrong key size for TCW". Tables with a valid tcw key size, with or without an AEAD cipher, still load and pass I/O. For arm64, dm-crypt.o was built but not run. Code and text developed with LLM assistance. --- drivers/md/dm-crypt.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/drivers/md/dm-crypt.c b/drivers/md/dm-crypt.c index a7655bba7ce3..da0f90bb906f 100644 --- a/drivers/md/dm-crypt.c +++ b/drivers/md/dm-crypt.c @@ -616,11 +616,6 @@ static int crypt_iv_tcw_ctr(struct crypt_config *cc, struct dm_target *ti, return -EINVAL; } - if (cc->key_size <= (cc->iv_size + TCW_WHITENING_SIZE)) { - ti->error = "Wrong key size for TCW"; - return -EINVAL; - } - tcw->iv_seed = kzalloc(cc->iv_size, GFP_KERNEL); tcw->whitening = kzalloc(TCW_WHITENING_SIZE, GFP_KERNEL); if (!tcw->iv_seed || !tcw->whitening) { @@ -2825,6 +2820,10 @@ static int crypt_ctr_ivmode(struct dm_target *ti, const char *ivmode) cc->iv_gen_ops = &crypt_iv_tcw_ops; cc->key_parts += 2; /* IV + whitening */ cc->key_extra_size = cc->iv_size + TCW_WHITENING_SIZE; + if (cc->key_size <= cc->key_extra_size) { + ti->error = "Wrong key size for TCW"; + return -EINVAL; + } } else if (strcmp(ivmode, "random") == 0) { cc->iv_gen_ops = &crypt_iv_random_ops; /* Need storage space in integrity fields. */ --- base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb change-id: 20261112-dm-crypt-tcw-check-40c734a01331