thebeanogamer / rpms / qemu-kvm

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