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

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