Blame SOURCES/kvm-backup-Make-sure-that-source-and-target-size-match.patch

77c23f
From e56abd782be8bb41bb07c0317d008f95ec9a8ee5 Mon Sep 17 00:00:00 2001
77c23f
From: Kevin Wolf <kwolf@redhat.com>
77c23f
Date: Wed, 3 Jun 2020 16:03:20 +0100
77c23f
Subject: [PATCH 21/26] backup: Make sure that source and target size match
77c23f
77c23f
RH-Author: Kevin Wolf <kwolf@redhat.com>
77c23f
Message-id: <20200603160325.67506-7-kwolf@redhat.com>
77c23f
Patchwork-id: 97107
77c23f
O-Subject: [RHEL-AV-8.2.1 qemu-kvm PATCH v2 06/11] backup: Make sure that source and target size match
77c23f
Bugzilla: 1778593
77c23f
RH-Acked-by: Eric Blake <eblake@redhat.com>
77c23f
RH-Acked-by: Max Reitz <mreitz@redhat.com>
77c23f
RH-Acked-by: Stefano Garzarella <sgarzare@redhat.com>
77c23f
77c23f
Since the introduction of a backup filter node in commit 00e30f05d, the
77c23f
backup block job crashes when the target image is smaller than the
77c23f
source image because it will try to write after the end of the target
77c23f
node without having BLK_PERM_RESIZE. (Previously, the BlockBackend layer
77c23f
would have caught this and errored out gracefully.)
77c23f
77c23f
We can fix this and even do better than the old behaviour: Check that
77c23f
source and target have the same image size at the start of the block job
77c23f
and unshare BLK_PERM_RESIZE. (This permission was already unshared
77c23f
before the same commit 00e30f05d, but the BlockBackend that was used to
77c23f
make the restriction was removed without a replacement.) This will
77c23f
immediately error out when starting the job instead of only when writing
77c23f
to a block that doesn't exist in the target.
77c23f
77c23f
Longer target than source would technically work because we would never
77c23f
write to blocks that don't exist, but semantically these are invalid,
77c23f
too, because a backup is supposed to create a copy, not just an image
77c23f
that starts with a copy.
77c23f
77c23f
Fixes: 00e30f05de1d19586345ec373970ef4c192c6270
77c23f
Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1778593
77c23f
Cc: qemu-stable@nongnu.org
77c23f
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
77c23f
Message-Id: <20200430142755.315494-4-kwolf@redhat.com>
77c23f
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
77c23f
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
77c23f
(cherry picked from commit 958a04bd32af18d9a207bcc78046e56a202aebc2)
77c23f
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
77c23f
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
77c23f
---
77c23f
 block/backup-top.c | 14 +++++++++-----
77c23f
 block/backup.c     | 14 +++++++++++++-
77c23f
 2 files changed, 22 insertions(+), 6 deletions(-)
77c23f
77c23f
diff --git a/block/backup-top.c b/block/backup-top.c
77c23f
index b8d863f..6756091 100644
77c23f
--- a/block/backup-top.c
77c23f
+++ b/block/backup-top.c
77c23f
@@ -143,8 +143,10 @@ static void backup_top_child_perm(BlockDriverState *bs, BdrvChild *c,
77c23f
          *
77c23f
          * Share write to target (child_file), to not interfere
77c23f
          * with guest writes to its disk which may be in target backing chain.
77c23f
+         * Can't resize during a backup block job because we check the size
77c23f
+         * only upfront.
77c23f
          */
77c23f
-        *nshared = BLK_PERM_ALL;
77c23f
+        *nshared = BLK_PERM_ALL & ~BLK_PERM_RESIZE;
77c23f
         *nperm = BLK_PERM_WRITE;
77c23f
     } else {
77c23f
         /* Source child */
77c23f
@@ -154,7 +156,7 @@ static void backup_top_child_perm(BlockDriverState *bs, BdrvChild *c,
77c23f
         if (perm & BLK_PERM_WRITE) {
77c23f
             *nperm = *nperm | BLK_PERM_CONSISTENT_READ;
77c23f
         }
77c23f
-        *nshared &= ~BLK_PERM_WRITE;
77c23f
+        *nshared &= ~(BLK_PERM_WRITE | BLK_PERM_RESIZE);
77c23f
     }
77c23f
 }
77c23f
 
77c23f
@@ -187,10 +189,12 @@ BlockDriverState *bdrv_backup_top_append(BlockDriverState *source,
77c23f
 {
77c23f
     Error *local_err = NULL;
77c23f
     BDRVBackupTopState *state;
77c23f
-    BlockDriverState *top = bdrv_new_open_driver(&bdrv_backup_top_filter,
77c23f
-                                                 filter_node_name,
77c23f
-                                                 BDRV_O_RDWR, errp);
77c23f
+    BlockDriverState *top;
77c23f
+
77c23f
+    assert(source->total_sectors == target->total_sectors);
77c23f
 
77c23f
+    top = bdrv_new_open_driver(&bdrv_backup_top_filter, filter_node_name,
77c23f
+                               BDRV_O_RDWR, errp);
77c23f
     if (!top) {
77c23f
         return NULL;
77c23f
     }
77c23f
diff --git a/block/backup.c b/block/backup.c
77c23f
index 7c6ddd2..821c9fb 100644
77c23f
--- a/block/backup.c
77c23f
+++ b/block/backup.c
77c23f
@@ -348,7 +348,7 @@ BlockJob *backup_job_create(const char *job_id, BlockDriverState *bs,
77c23f
                   BlockCompletionFunc *cb, void *opaque,
77c23f
                   JobTxn *txn, Error **errp)
77c23f
 {
77c23f
-    int64_t len;
77c23f
+    int64_t len, target_len;
77c23f
     BackupBlockJob *job = NULL;
77c23f
     int64_t cluster_size;
77c23f
     BdrvRequestFlags write_flags;
77c23f
@@ -413,6 +413,18 @@ BlockJob *backup_job_create(const char *job_id, BlockDriverState *bs,
77c23f
         goto error;
77c23f
     }
77c23f
 
77c23f
+    target_len = bdrv_getlength(target);
77c23f
+    if (target_len < 0) {
77c23f
+        error_setg_errno(errp, -target_len, "Unable to get length for '%s'",
77c23f
+                         bdrv_get_device_or_node_name(bs));
77c23f
+        goto error;
77c23f
+    }
77c23f
+
77c23f
+    if (target_len != len) {
77c23f
+        error_setg(errp, "Source and target image have different sizes");
77c23f
+        goto error;
77c23f
+    }
77c23f
+
77c23f
     cluster_size = backup_calculate_cluster_size(target, errp);
77c23f
     if (cluster_size < 0) {
77c23f
         goto error;
77c23f
-- 
77c23f
1.8.3.1
77c23f