From 1b6d24e0ec4fc50686a533ec209f7b1db952deb5 Mon Sep 17 00:00:00 2001
From: Vojtech Trefny <vtrefny@redhat.com>
Date: Wed, 3 Apr 2024 15:58:04 +0200
Subject: [PATCH] crypto: Fix double free in bd_crypto_luks_remove_key

---
 src/plugins/crypto.c | 1 -
 tests/crypto_test.py | 6 ++++++
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/src/plugins/crypto.c b/src/plugins/crypto.c
index 3dabaabd..05931e80 100644
--- a/src/plugins/crypto.c
+++ b/src/plugins/crypto.c
@@ -1479,7 +1479,6 @@ gboolean bd_crypto_luks_remove_key (const gchar *device, BDCryptoKeyslotContext
         return FALSE;
     }
 
-    crypt_safe_free (key_buf);
     crypt_free (cd);
     bd_utils_report_finished (progress_id, "Completed");
     return TRUE;
diff --git a/tests/crypto_test.py b/tests/crypto_test.py
index 4d920c27..efe892b2 100644
--- a/tests/crypto_test.py
+++ b/tests/crypto_test.py
@@ -524,6 +524,9 @@ class CryptoTestRemoveKey(CryptoTestCase):
         succ = BlockDev.crypto_luks_add_key(self.loop_dev, ctx, nctx2)
         self.assertTrue(succ)
 
+        nctx3 = BlockDev.CryptoKeyslotContext(keyfile=self.keyfile)
+        succ = BlockDev.crypto_luks_add_key(self.loop_dev, ctx, nctx3)
+
         with self.assertRaises(GLib.GError):
             wctx = BlockDev.CryptoKeyslotContext(passphrase="wrong-passphrase")
             BlockDev.crypto_luks_remove_key(self.loop_dev, wctx)
@@ -534,6 +537,9 @@ class CryptoTestRemoveKey(CryptoTestCase):
         succ = BlockDev.crypto_luks_remove_key(self.loop_dev, nctx2)
         self.assertTrue(succ)
 
+        succ = BlockDev.crypto_luks_remove_key(self.loop_dev, nctx3)
+        self.assertTrue(succ)
+
     @tag_test(TestTags.SLOW)
     def test_luks_remove_key(self):
         self._remove_key(self._luks_format)
-- 
2.27.0

