thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone

Blame SOURCES/kvm-block-backend-prevent-dangling-BDS-pointers-across-a.patch

60061b
From bf4c15a3debbe68b6eb25c52174843470a9c014f Mon Sep 17 00:00:00 2001
60061b
From: Stefan Hajnoczi <stefanha@redhat.com>
60061b
Date: Tue, 11 Jan 2022 15:36:12 +0000
60061b
Subject: [PATCH 3/6] block-backend: prevent dangling BDS pointers across
60061b
 aio_poll()
60061b
60061b
RH-Author: Stefan Hajnoczi <stefanha@redhat.com>
60061b
RH-MergeRequest: 109: block-backend: prevent dangling BDS pointers across aio_poll()
60061b
RH-Commit: [1/2] da5a59eddff0dc10be7de8e291fa675143d11d73
60061b
RH-Bugzilla: 2021778 2036178
60061b
RH-Acked-by: Hanna Reitz <hreitz@redhat.com>
60061b
RH-Acked-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
60061b
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
60061b
60061b
The BlockBackend root child can change when aio_poll() is invoked. This
60061b
happens when a temporary filter node is removed upon blockjob
60061b
completion, for example.
60061b
60061b
Functions in block/block-backend.c must be aware of this when using a
60061b
blk_bs() pointer across aio_poll() because the BlockDriverState refcnt
60061b
may reach 0, resulting in a stale pointer.
60061b
60061b
One example is scsi_device_purge_requests(), which calls blk_drain() to
60061b
wait for in-flight requests to cancel. If the backup blockjob is active,
60061b
then the BlockBackend root child is a temporary filter BDS owned by the
60061b
blockjob. The blockjob can complete during bdrv_drained_begin() and the
60061b
last reference to the BDS is released when the temporary filter node is
60061b
removed. This results in a use-after-free when blk_drain() calls
60061b
bdrv_drained_end(bs) on the dangling pointer.
60061b
60061b
Explicitly hold a reference to bs across block APIs that invoke
60061b
aio_poll().
60061b
60061b
Buglink: https://bugzilla.redhat.com/show_bug.cgi?id=2021778
60061b
Buglink: https://bugzilla.redhat.com/show_bug.cgi?id=2036178
60061b
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
60061b
Message-Id: <20220111153613.25453-2-stefanha@redhat.com>
60061b
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
60061b
(cherry picked from commit 1e3552dbd28359d35967b7c28dc86cde1bc29205)
60061b
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
60061b
---
60061b
 block/block-backend.c | 19 +++++++++++++++++--
60061b
 1 file changed, 17 insertions(+), 2 deletions(-)
60061b
60061b
diff --git a/block/block-backend.c b/block/block-backend.c
60061b
index 12ef80ea17..23e727199b 100644
60061b
--- a/block/block-backend.c
60061b
+++ b/block/block-backend.c
60061b
@@ -822,16 +822,22 @@ BlockBackend *blk_by_public(BlockBackendPublic *public)
60061b
 void blk_remove_bs(BlockBackend *blk)
60061b
 {
60061b
     ThrottleGroupMember *tgm = &blk->public.throttle_group_member;
60061b
-    BlockDriverState *bs;
60061b
     BdrvChild *root;
60061b
 
60061b
     notifier_list_notify(&blk->remove_bs_notifiers, blk);
60061b
     if (tgm->throttle_state) {
60061b
-        bs = blk_bs(blk);
60061b
+        BlockDriverState *bs = blk_bs(blk);
60061b
+
60061b
+        /*
60061b
+         * Take a ref in case blk_bs() changes across bdrv_drained_begin(), for
60061b
+         * example, if a temporary filter node is removed by a blockjob.
60061b
+         */
60061b
+        bdrv_ref(bs);
60061b
         bdrv_drained_begin(bs);
60061b
         throttle_group_detach_aio_context(tgm);
60061b
         throttle_group_attach_aio_context(tgm, qemu_get_aio_context());
60061b
         bdrv_drained_end(bs);
60061b
+        bdrv_unref(bs);
60061b
     }
60061b
 
60061b
     blk_update_root_state(blk);
60061b
@@ -1705,6 +1711,7 @@ void blk_drain(BlockBackend *blk)
60061b
     BlockDriverState *bs = blk_bs(blk);
60061b
 
60061b
     if (bs) {
60061b
+        bdrv_ref(bs);
60061b
         bdrv_drained_begin(bs);
60061b
     }
60061b
 
60061b
@@ -1714,6 +1721,7 @@ void blk_drain(BlockBackend *blk)
60061b
 
60061b
     if (bs) {
60061b
         bdrv_drained_end(bs);
60061b
+        bdrv_unref(bs);
60061b
     }
60061b
 }
60061b
 
60061b
@@ -2044,10 +2052,13 @@ static int blk_do_set_aio_context(BlockBackend *blk, AioContext *new_context,
60061b
     int ret;
60061b
 
60061b
     if (bs) {
60061b
+        bdrv_ref(bs);
60061b
+
60061b
         if (update_root_node) {
60061b
             ret = bdrv_child_try_set_aio_context(bs, new_context, blk->root,
60061b
                                                  errp);
60061b
             if (ret < 0) {
60061b
+                bdrv_unref(bs);
60061b
                 return ret;
60061b
             }
60061b
         }
60061b
@@ -2057,6 +2068,8 @@ static int blk_do_set_aio_context(BlockBackend *blk, AioContext *new_context,
60061b
             throttle_group_attach_aio_context(tgm, new_context);
60061b
             bdrv_drained_end(bs);
60061b
         }
60061b
+
60061b
+        bdrv_unref(bs);
60061b
     }
60061b
 
60061b
     blk->ctx = new_context;
60061b
@@ -2326,11 +2339,13 @@ void blk_io_limits_disable(BlockBackend *blk)
60061b
     ThrottleGroupMember *tgm = &blk->public.throttle_group_member;
60061b
     assert(tgm->throttle_state);
60061b
     if (bs) {
60061b
+        bdrv_ref(bs);
60061b
         bdrv_drained_begin(bs);
60061b
     }
60061b
     throttle_group_unregister_tgm(tgm);
60061b
     if (bs) {
60061b
         bdrv_drained_end(bs);
60061b
+        bdrv_unref(bs);
60061b
     }
60061b
 }
60061b
 
60061b
-- 
60061b
2.27.0
60061b