Skip to content

Commit 6c29e6b

Browse files
committed
crypto: Fix reading keyfile in bd_crypto_luks_change_key
Copy-paste bug, we need to make sure save the new passphrase into the correct buffer.
1 parent 1d15c5a commit 6c29e6b

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/plugins/crypto.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1734,15 +1734,15 @@ gboolean bd_crypto_luks_change_key (const gchar *device, BDCryptoKeyslotContext
17341734
}
17351735

17361736
if (ncontext->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_KEYFILE) {
1737-
ret = crypt_keyfile_device_read (cd, ncontext->u.keyfile.keyfile, &key_buf, &buf_len,
1737+
ret = crypt_keyfile_device_read (cd, ncontext->u.keyfile.keyfile, &nkey_buf, &nbuf_len,
17381738
ncontext->u.keyfile.keyfile_offset, ncontext->u.keyfile.key_size, 0);
17391739
if (ret != 0) {
17401740
g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_KEYFILE_FAILED,
17411741
"Failed to load key from file '%s': %s", ncontext->u.keyfile.keyfile,
17421742
strerror_l (-ret, c_locale));
17431743
crypt_free (cd);
17441744
if (context->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_KEYFILE)
1745-
crypt_safe_free (key_buf);
1745+
crypt_safe_free (nkey_buf);
17461746
bd_utils_report_finished (progress_id, l_error->message);
17471747
g_propagate_error (error, l_error);
17481748
return FALSE;

tests/crypto_test.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,30 @@ def _change_key(self, create_fn):
631631
succ = BlockDev.crypto_luks_change_key(self.loop_devs[0], ctx, nctx)
632632
self.assertTrue(succ)
633633

634+
# old passphrase should no longer work
635+
with self.assertRaises(GLib.GError):
636+
BlockDev.crypto_luks_remove_key(self.loop_devs[0], ctx)
637+
638+
# new passphrase should work
639+
succ = BlockDev.crypto_luks_open(self.loop_devs[0], "libblockdevTestLUKS", nctx)
640+
self.assertTrue(succ)
641+
642+
succ = BlockDev.crypto_luks_close("libblockdevTestLUKS")
643+
self.assertTrue(succ)
644+
645+
# try with keyfile as well
646+
kctx = BlockDev.CryptoKeyslotContext(keyfile=self.keyfile)
647+
succ = BlockDev.crypto_luks_change_key(self.loop_devs[0], nctx, kctx)
648+
self.assertTrue(succ)
649+
650+
# old passphrase should no longer work
651+
with self.assertRaises(GLib.GError):
652+
BlockDev.crypto_luks_remove_key(self.loop_devs[0], nctx)
653+
654+
# keyfile should work
655+
succ = BlockDev.crypto_luks_open(self.loop_devs[0], "libblockdevTestLUKS", kctx)
656+
self.assertTrue(succ)
657+
634658
@tag_test(TestTags.SLOW)
635659
def test_luks_change_key(self):
636660
self._change_key(self._luks_format)

0 commit comments

Comments
 (0)