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

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