Blame SOURCES/kvm-block-backend-Fix-potential-double-blk_delete.patch

ae23c9
From 0fd571f772eb449da398b029705f4cadf0129f60 Mon Sep 17 00:00:00 2001
ae23c9
From: Kevin Wolf <kwolf@redhat.com>
ae23c9
Date: Wed, 10 Oct 2018 20:22:04 +0100
ae23c9
Subject: [PATCH 38/49] block-backend: Fix potential double blk_delete()
ae23c9
ae23c9
RH-Author: Kevin Wolf <kwolf@redhat.com>
ae23c9
Message-id: <20181010202213.7372-26-kwolf@redhat.com>
ae23c9
Patchwork-id: 82615
ae23c9
O-Subject: [RHEL-8 qemu-kvm PATCH 35/44] block-backend: Fix potential double blk_delete()
ae23c9
Bugzilla: 1637976
ae23c9
RH-Acked-by: Max Reitz <mreitz@redhat.com>
ae23c9
RH-Acked-by: John Snow <jsnow@redhat.com>
ae23c9
RH-Acked-by: Thomas Huth <thuth@redhat.com>
ae23c9
ae23c9
blk_unref() first decreases the refcount of the BlockBackend and calls
ae23c9
blk_delete() if the refcount reaches zero. Requests can still be in
ae23c9
flight at this point, they are only drained during blk_delete():
ae23c9
ae23c9
At this point, arbitrary callbacks can run. If any callback takes a
ae23c9
temporary BlockBackend reference, it will first increase the refcount to
ae23c9
1 and then decrease it to 0 again, triggering another blk_delete(). This
ae23c9
will cause a use-after-free crash in the outer blk_delete().
ae23c9
ae23c9
Fix it by draining the BlockBackend before decreasing to refcount to 0.
ae23c9
Assert in blk_ref() that it never takes the first refcount (which would
ae23c9
mean that the BlockBackend is already being deleted).
ae23c9
ae23c9
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
ae23c9
Reviewed-by: Fam Zheng <famz@redhat.com>
ae23c9
Reviewed-by: Max Reitz <mreitz@redhat.com>
ae23c9
(cherry picked from commit 5ca9d21bd1c8eeb578d0964e31bd03d47c25773d)
ae23c9
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
ae23c9
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
ae23c9
---
ae23c9
 block/block-backend.c | 9 ++++++++-
ae23c9
 1 file changed, 8 insertions(+), 1 deletion(-)
ae23c9
ae23c9
diff --git a/block/block-backend.c b/block/block-backend.c
ae23c9
index dffc6f0..bfd0331 100644
ae23c9
--- a/block/block-backend.c
ae23c9
+++ b/block/block-backend.c
ae23c9
@@ -436,6 +436,7 @@ int blk_get_refcnt(BlockBackend *blk)
ae23c9
  */
ae23c9
 void blk_ref(BlockBackend *blk)
ae23c9
 {
ae23c9
+    assert(blk->refcnt > 0);
ae23c9
     blk->refcnt++;
ae23c9
 }
ae23c9
 
ae23c9
@@ -448,7 +449,13 @@ void blk_unref(BlockBackend *blk)
ae23c9
 {
ae23c9
     if (blk) {
ae23c9
         assert(blk->refcnt > 0);
ae23c9
-        if (!--blk->refcnt) {
ae23c9
+        if (blk->refcnt > 1) {
ae23c9
+            blk->refcnt--;
ae23c9
+        } else {
ae23c9
+            blk_drain(blk);
ae23c9
+            /* blk_drain() cannot resurrect blk, nobody held a reference */
ae23c9
+            assert(blk->refcnt == 1);
ae23c9
+            blk->refcnt = 0;
ae23c9
             blk_delete(blk);
ae23c9
         }
ae23c9
     }
ae23c9
-- 
ae23c9
1.8.3.1
ae23c9