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

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