cryptospore / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

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

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