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

357786
From f006159bba058c873bf8c1a3684ec72f338f2a2c Mon Sep 17 00:00:00 2001
357786
From: Kevin Wolf <kwolf@redhat.com>
357786
Date: Fri, 14 Sep 2018 10:55:33 +0200
357786
Subject: [PATCH 42/49] block-backend: Fix potential double blk_delete()
357786
357786
RH-Author: Kevin Wolf <kwolf@redhat.com>
357786
Message-id: <20180914105540.18077-36-kwolf@redhat.com>
357786
Patchwork-id: 82186
357786
O-Subject: [RHV-7.6 qemu-kvm-rhev PATCH 35/42] block-backend: Fix potential double blk_delete()
357786
Bugzilla: 1601212
357786
RH-Acked-by: John Snow <jsnow@redhat.com>
357786
RH-Acked-by: Max Reitz <mreitz@redhat.com>
357786
RH-Acked-by: Fam Zheng <famz@redhat.com>
357786
357786
blk_unref() first decreases the refcount of the BlockBackend and calls
357786
blk_delete() if the refcount reaches zero. Requests can still be in
357786
flight at this point, they are only drained during blk_delete():
357786
357786
At this point, arbitrary callbacks can run. If any callback takes a
357786
temporary BlockBackend reference, it will first increase the refcount to
357786
1 and then decrease it to 0 again, triggering another blk_delete(). This
357786
will cause a use-after-free crash in the outer blk_delete().
357786
357786
Fix it by draining the BlockBackend before decreasing to refcount to 0.
357786
Assert in blk_ref() that it never takes the first refcount (which would
357786
mean that the BlockBackend is already being deleted).
357786
357786
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
357786
Reviewed-by: Fam Zheng <famz@redhat.com>
357786
Reviewed-by: Max Reitz <mreitz@redhat.com>
357786
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
357786
---
357786
 block/block-backend.c | 9 ++++++++-
357786
 1 file changed, 8 insertions(+), 1 deletion(-)
357786
357786
diff --git a/block/block-backend.c b/block/block-backend.c
357786
index ea85770..1adf76b 100644
357786
--- a/block/block-backend.c
357786
+++ b/block/block-backend.c
357786
@@ -436,6 +436,7 @@ int blk_get_refcnt(BlockBackend *blk)
357786
  */
357786
 void blk_ref(BlockBackend *blk)
357786
 {
357786
+    assert(blk->refcnt > 0);
357786
     blk->refcnt++;
357786
 }
357786
 
357786
@@ -448,7 +449,13 @@ void blk_unref(BlockBackend *blk)
357786
 {
357786
     if (blk) {
357786
         assert(blk->refcnt > 0);
357786
-        if (!--blk->refcnt) {
357786
+        if (blk->refcnt > 1) {
357786
+            blk->refcnt--;
357786
+        } else {
357786
+            blk_drain(blk);
357786
+            /* blk_drain() cannot resurrect blk, nobody held a reference */
357786
+            assert(blk->refcnt == 1);
357786
+            blk->refcnt = 0;
357786
             blk_delete(blk);
357786
         }
357786
     }
357786
-- 
357786
1.8.3.1
357786