From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mta0.migadu.com (out-26.mta0.migadu.com [91.218.175.26]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 220E43E44F0 for ; Thu, 13 Aug 2026 08:14:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.26 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786608874; cv=none; b=CpdIwBwk2cciQm9sk3JrrVoUZJlz/QYBnQH52ybhG/YI8yVtULRQzUE59cYmfFGo59OPOLO1OVDhqD7HxySuPq3Ie16ce6esNWvD6GtO0k5PmZE1yY1oXs4mo8oLf6k+6ofDZqvQDkDa0o69xz6ot2E0G4E8jm5/npl+GAuWU90= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786608874; c=relaxed/simple; bh=J0iN+tYQVCf0aL57blCvGcAdLc0zmiT1pHNPtbwINCw=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=Lo46E7Xz051tgyUONgvq0QdUpjq4oJ2Itpj/FMuNtrxxob4fAdo+9zXzZxxH0fQrSE26ew/e/fYacOFdB9RJ0/aocrE3ZzMKBjp2NxjuaSjx4EkXov6sccPnqR828GNclCuQ0/jJH2aDEEttb2EVO73AWaEJH9U45aEAm1WhJxs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=JAZLkl9C; arc=none smtp.client-ip=91.218.175.26 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="JAZLkl9C" X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=J0iN+tYQVCf0aL57blCvGcAdLc0zmiT1pHNPtbwINCw=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1786608871; v=1; x=1787213671; b=JAZLkl9CpyE9y6UEzmI9A1lc3NBL3y2gPqlzdr/vYr3L3w6KgT0aJDVosqpg8RdjGBDx4mhN 32vBBFCKeyOPLFQDEtwKh8Xo4pih0Q7l/8CF6NSTaD2jpAB8CEEiYtpE7gOV1bBVDpPzTbOs8eO LrgPf+wIKVpJT05Ympu21cpU= X-Envelope-To: linux-kernel@vger.kernel.org Received: from fedora.fritz.box (62.216.208.217) by mta12.migadu.com with ESMTPS id 946b9a59ee4d20ba; Thu, 13 Aug 2026 08:14:20 +0000 X-Migadu-Flow: FLOW_OUT From: Thorsten Blum To: Alasdair Kergon , Mike Snitzer , Mikulas Patocka , Benjamin Marzinski , Andy Shevchenko Cc: Thorsten Blum , Mike Snitzer , dm-devel@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH] dm-crypt: Reject malformed raw keys in crypt_set_key() Date: Thu, 13 Aug 2026 10:13:36 +0200 Message-ID: <20260813081335.40282-3-thorsten.blum@linux.dev> X-Mailer: git-send-email 2.55.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1268; i=thorsten.blum@linux.dev; h=from:subject; bh=J0iN+tYQVCf0aL57blCvGcAdLc0zmiT1pHNPtbwINCw=; b=owGbwMvMwCUWt7pQ4caZUj3G02pJDFm1Neut5BesnLz+h+i1NRz5b1o8ZyWfC+7aUNf4fMrxx VsTV7mkdJSyMIhxMciKKbI8mPVjhm9pTeUmk4idMHNYmUCGMHBxCsBEdFsZ/ocxXfHdcdLi5M6N rSm2UY2cyq2B7jnneeYsS//VtGOHijXD/7w73JYPBc5+Y+ues+LUXNaL/cf1GjkeF/91Cb43efl JB1YA X-Developer-Key: i=thorsten.blum@linux.dev; a=openpgp; fpr=1D60735E8AEF3BE473B69D84733678FD8DFEEAD4 Content-Transfer-Encoding: 8bit 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(). Fixes: e944e03e336f ("dm crypt: replace custom implementation of hex2bin()") Signed-off-by: Thorsten Blum --- 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 608b617fb817..b7aa3d893303 100644 --- a/drivers/md/dm-crypt.c +++ b/drivers/md/dm-crypt.c @@ -2615,6 +2615,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;