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

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