All of lore.kernel.org
 help / color / mirror / Atom feed
From: Rahul Priolkar <rahul.priolkar@asu.edu>
To: Alasdair Kergon <agk@redhat.com>,
	Mike Snitzer <snitzer@kernel.org>,
	Mikulas Patocka <mpatocka@redhat.com>,
	Benjamin Marzinski <bmarzins@redhat.com>
Cc: dm-devel@lists.linux.dev
Subject: [PATCH] dm-crypt: serialize key updates and status reads
Date: Wed, 02 Dec 2026 01:32:39 -0800 (PST)	[thread overview]
Message-ID: <6b0fe5b7.5bfb57e8.1d4746.834e@mx.google.com> (raw)

Hello,

Please review this fix for two key-description lifetime bugs we observed
in dm-crypt on v7.4-rc5.

-- >8 --

A live table reference keeps the dm-crypt target alive, but it does not
protect the lifetime of cc->key_string. Key updates can free the same
description twice, and crypt_status() can read a freed description.

On v7.4-rc5 (fe6ff8d5d675), KASAN reported a double free in
crypt_set_key(). In a separate 120-second run, 472 table status lines
returned an incorrect key field.

Add a per-target mutex covering key updates and the key portion of
table status. On an rc5 build with the candidate fixes applied together,
we saw no KASAN reports or incorrect status lines in 120 seconds per
test.

Fixes: c538f6ec9f56 ("dm crypt: add ability to use keys from the kernel key retention service")
Cc: stable@vger.kernel.org
Signed-off-by: Rahul Priolkar <rahul.priolkar@asu.edu>
---
 drivers/md/dm-crypt.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/drivers/md/dm-crypt.c b/drivers/md/dm-crypt.c
index 02038a9cd..de44a550c 100644
--- a/drivers/md/dm-crypt.c
+++ b/drivers/md/dm-crypt.c
@@ -232,6 +232,8 @@ struct crypt_config {
 
 	struct bio_set bs;
 	struct mutex bio_alloc_lock;
+	/* serializes key messages (set/wipe) and the key part of the table status */
+	struct mutex key_lock;
 
 	u8 *authenc_key; /* space for keys in authenc() format (if used) */
 	u8 key[] __counted_by(key_size);
@@ -2755,6 +2757,7 @@ static void crypt_dtr(struct dm_target *ti)
 	kfree_sensitive(cc->authenc_key);
 
 	mutex_destroy(&cc->bio_alloc_lock);
+	mutex_destroy(&cc->key_lock);
 
 	/* Must zero key material before freeing */
 	kfree_sensitive(cc);
@@ -3317,6 +3320,7 @@ static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
 	}
 
 	mutex_init(&cc->bio_alloc_lock);
+	mutex_init(&cc->key_lock);
 
 	ret = -EINVAL;
 	if ((sscanf(argv[2], "%llu%c", &tmpll, &dummy) != 1) ||
@@ -3549,6 +3553,8 @@ static void crypt_status(struct dm_target *ti, status_type_t type,
 		DMEMIT("%s ", cc->cipher_string);
 
 		if (cc->key_size > 0) {
+			/* a "key set/wipe" message may replace key_string and key */
+			mutex_lock(&cc->key_lock);
 			if (cc->key_string)
 				DMEMIT(":%u:%s", cc->key_size, cc->key_string);
 			else {
@@ -3557,6 +3563,7 @@ static void crypt_status(struct dm_target *ti, status_type_t type,
 					       hex2asc(cc->key[i] & 0xf));
 				}
 			}
+			mutex_unlock(&cc->key_lock);
 		} else
 			DMEMIT("-");
 
@@ -3669,6 +3676,8 @@ static int crypt_message(struct dm_target *ti, unsigned int argc, char **argv,
 		goto error;
 
 	if (!strcasecmp(argv[0], "key")) {
+		/* DM_TARGET_MSG calls are not serialized: two key messages free the same key_string */
+		guard(mutex)(&cc->key_lock);
 		if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
 			DMWARN("not suspended during key manipulation.");
 			return -EINVAL;
-- 
2.56.0


                 reply	other threads:[~2026-12-02  9:32 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6b0fe5b7.5bfb57e8.1d4746.834e@mx.google.com \
    --to=rahul.priolkar@asu.edu \
    --cc=agk@redhat.com \
    --cc=bmarzins@redhat.com \
    --cc=dm-devel@lists.linux.dev \
    --cc=mpatocka@redhat.com \
    --cc=snitzer@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.