[PATCH] dm crypt: validate TCW IV size
From: Kyuwon Cho
Date: Thu Dec 03 2026 - 11:47:41 EST
The TCW IV generator always XORs an eight-byte sector number into the IV.
It repeats the same eight bytes when the IV is wider than eight bytes, but
uses cc->iv_size - 8 as the length of that second XOR.
The capi cipher parser can pair TCW with ciphers whose IV size is outside
the eight- and sixteen-byte sizes used by the legacy TCRYPT format. A
wider IV makes the second XOR read beyond the sector variable on the
stack:
BUG: KASAN: stack-out-of-bounds in __crypto_xor
Read of size 8 by task kworker/u8:0/13
Call Trace:
__crypto_xor (lib/crypto/utils.c:45)
crypt_iv_tcw_gen (drivers/md/dm-crypt.c:698)
crypt_convert (drivers/md/dm-crypt.c:1560)
kcryptd_crypt_read_convert (drivers/md/dm-crypt.c:2164)
process_one_work (kernel/workqueue.c:3497)
The buggy address is at offset 40 in crypt_iv_tcw_gen(), whose frame
has one object: [32, 40) 'sector'.
An IV narrower than eight bytes would also make the first XOR overrun the
IV buffer. Accept only the two IV sizes supported by TCW. The issue was
found by an LLM-guided test generator.
Fixes: ed04d98169f1 ("dm crypt: add TCW IV mode for old CBC TCRYPT containers")
Signed-off-by: Kyuwon Cho <kyuwon.cho@xxxxxxxx>
---
The issue was found while testing dm-crypt table combinations from a
research prototype. The reproducer creates a TCW target with a capi
XChaCha20 cipher and issues a read. With the patch target construction
rejects the unsupported IV size with -EINVAL before I/O can begin.
Found and tested on mainline fe6ff8d5d675 (v7.4-rc5, with KASAN); the
patch is against the device-mapper for-next tree at 2ee78e96ca18.
drivers/md/dm-crypt.c | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/drivers/md/dm-crypt.c b/drivers/md/dm-crypt.c
index 010a72020803..853f6e5af7ba 100644
--- a/drivers/md/dm-crypt.c
+++ b/drivers/md/dm-crypt.c
@@ -615,6 +615,11 @@ static int crypt_iv_tcw_ctr(struct crypt_config *cc, struct dm_target *ti,
ti->error = "Unsupported sector size for TCW";
return -EINVAL;
}
+ if (cc->iv_size != sizeof(u64) &&
+ cc->iv_size != TCW_WHITENING_SIZE) {
+ ti->error = "Unsupported IV size for TCW";
+ return -EINVAL;
+ }
tcw->iv_seed = kzalloc(cc->iv_size, GFP_KERNEL);
tcw->whitening = kzalloc(TCW_WHITENING_SIZE, GFP_KERNEL);
--
2.43.0