7f1c5b
From 46ead2c391924b68741d6da28f28f909b80f5914 Mon Sep 17 00:00:00 2001
7f1c5b
From: Kevin Wolf <kwolf@redhat.com>
7f1c5b
Date: Thu, 12 Jan 2023 20:14:51 +0100
7f1c5b
Subject: [PATCH 01/20] qcow2: Fix theoretical corruption in store_bitmap()
7f1c5b
 error path
7f1c5b
MIME-Version: 1.0
7f1c5b
Content-Type: text/plain; charset=UTF-8
7f1c5b
Content-Transfer-Encoding: 8bit
7f1c5b
7f1c5b
RH-Author: Kevin Wolf <kwolf@redhat.com>
7f1c5b
RH-MergeRequest: 143: qemu-img: Fix exit code for errors closing the image
7f1c5b
RH-Bugzilla: 2150180
7f1c5b
RH-Acked-by: Thomas Huth <thuth@redhat.com>
7f1c5b
RH-Acked-by: Hanna Czenczek <hreitz@redhat.com>
7f1c5b
RH-Acked-by: Stefano Garzarella <sgarzare@redhat.com>
7f1c5b
RH-Commit: [1/4] a6a497947179431567d330d0501247a3749fb9fd (kmwolf/centos-qemu-kvm)
7f1c5b
7f1c5b
In order to write the bitmap table to the image file, it is converted to
7f1c5b
big endian. If the write fails, it is passed to clear_bitmap_table() to
7f1c5b
free all of the clusters it had allocated before. However, if we don't
7f1c5b
convert it back to native endianness first, we'll free things at a wrong
7f1c5b
offset.
7f1c5b
7f1c5b
In practical terms, the offsets will be so high that we won't actually
7f1c5b
free any allocated clusters, but just run into an error, but in theory
7f1c5b
this can cause image corruption.
7f1c5b
7f1c5b
Cc: qemu-stable@nongnu.org
7f1c5b
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
7f1c5b
Message-Id: <20230112191454.169353-2-kwolf@redhat.com>
7f1c5b
Reviewed-by: Hanna Czenczek <hreitz@redhat.com>
7f1c5b
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
7f1c5b
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
7f1c5b
(cherry picked from commit b03dd9613bcf8fe948581b2b3585510cb525c382)
7f1c5b
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
7f1c5b
---
7f1c5b
 block/qcow2-bitmap.c | 5 +++--
7f1c5b
 1 file changed, 3 insertions(+), 2 deletions(-)
7f1c5b
7f1c5b
diff --git a/block/qcow2-bitmap.c b/block/qcow2-bitmap.c
7f1c5b
index bcad567c0c..3dff99ba06 100644
7f1c5b
--- a/block/qcow2-bitmap.c
7f1c5b
+++ b/block/qcow2-bitmap.c
7f1c5b
@@ -115,7 +115,7 @@ static int update_header_sync(BlockDriverState *bs)
7f1c5b
     return bdrv_flush(bs->file->bs);
7f1c5b
 }
7f1c5b
 
7f1c5b
-static inline void bitmap_table_to_be(uint64_t *bitmap_table, size_t size)
7f1c5b
+static inline void bitmap_table_bswap_be(uint64_t *bitmap_table, size_t size)
7f1c5b
 {
7f1c5b
     size_t i;
7f1c5b
 
7f1c5b
@@ -1401,9 +1401,10 @@ static int store_bitmap(BlockDriverState *bs, Qcow2Bitmap *bm, Error **errp)
7f1c5b
         goto fail;
7f1c5b
     }
7f1c5b
 
7f1c5b
-    bitmap_table_to_be(tb, tb_size);
7f1c5b
+    bitmap_table_bswap_be(tb, tb_size);
7f1c5b
     ret = bdrv_pwrite(bs->file, tb_offset, tb_size * sizeof(tb[0]), tb, 0);
7f1c5b
     if (ret < 0) {
7f1c5b
+        bitmap_table_bswap_be(tb, tb_size);
7f1c5b
         error_setg_errno(errp, -ret, "Failed to write bitmap '%s' to file",
7f1c5b
                          bm_name);
7f1c5b
         goto fail;
7f1c5b
-- 
7f1c5b
2.31.1
7f1c5b