From ba56ca86f925f639d2d1d529dda2bf534fd8b853 Mon Sep 17 00:00:00 2001 From: Thorsten Blum Date: Thu, 13 Aug 2026 10:13:36 +0200 Subject: [PATCH] dm-crypt: Reject malformed raw keys in crypt_set_key() dm-crypt calculates the raw key size by dividing the hex string length by two. If the string has an odd number of characters, the key size is rounded down and hex2bin() only decodes complete byte pairs. For example, a 33-character raw key string is malformed, but dm-crypt treats it as a 16-byte key and ignores the last character. Reject raw key strings whose length does not match the expected number of hex characters. This restores the trailing character check that was lost when the open-coded decoder was replaced with hex2bin(). Signed-off-by: Thorsten Blum Signed-off-by: Mikulas Patocka --- drivers/md/dm-crypt.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/drivers/md/dm-crypt.c b/drivers/md/dm-crypt.c index 1229490dd811..722caab3f8eb 100644 --- a/drivers/md/dm-crypt.c +++ b/drivers/md/dm-crypt.c @@ -2614,6 +2614,9 @@ static int crypt_set_key(struct crypt_config *cc, char *key) kfree_sensitive(cc->key_string); cc->key_string = NULL; + if (cc->key_size && key_string_len != cc->key_size * 2) + goto out; + /* Decode key from its hex representation. */ if (cc->key_size && hex2bin(cc->key, key, cc->key_size) < 0) goto out;