From 137e84f68da06666ebf7f391766cc6209ce1c39c Mon Sep 17 00:00:00 2001 From: Jon Maloy Date: Tue, 9 May 2023 10:29:03 -0400 Subject: [PATCH 13/21] async: avoid use-after-free on re-entrancy guard RH-Author: Jon Maloy RH-MergeRequest: 165: memory: prevent dma-reentracy issues RH-Jira: RHEL-516 RH-Acked-by: Miroslav Rezanina RH-Commit: [9/13] d4b957108aaacf4a597122aaeeaa8e56985f1fca (jmaloy/jmaloy-qemu-kvm-2) Jira: https://issues.redhat.com/browse/RHEL-516 Upstream: Merged CVE: CVE-2023-2680 commit 7915bd06f25e1803778081161bf6fa10c42dc7cd Author: Alexander Bulekov Date: Mon May 1 10:19:56 2023 -0400 async: avoid use-after-free on re-entrancy guard A BH callback can free the BH, causing a use-after-free in aio_bh_call. Fix that by keeping a local copy of the re-entrancy guard pointer. Buglink: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=58513 Fixes: 9c86c97f12 ("async: Add an optional reentrancy guard to the BH API") Signed-off-by: Alexander Bulekov Message-Id: <20230501141956.3444868-1-alxndr@bu.edu> Reviewed-by: Thomas Huth Signed-off-by: Thomas Huth Signed-off-by: Jon Maloy --- util/async.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/util/async.c b/util/async.c index a9b528c370..cd1a1815f9 100644 --- a/util/async.c +++ b/util/async.c @@ -156,18 +156,20 @@ void aio_bh_call(QEMUBH *bh) { bool last_engaged_in_io = false; - if (bh->reentrancy_guard) { - last_engaged_in_io = bh->reentrancy_guard->engaged_in_io; - if (bh->reentrancy_guard->engaged_in_io) { + /* Make a copy of the guard-pointer as cb may free the bh */ + MemReentrancyGuard *reentrancy_guard = bh->reentrancy_guard; + if (reentrancy_guard) { + last_engaged_in_io = reentrancy_guard->engaged_in_io; + if (reentrancy_guard->engaged_in_io) { trace_reentrant_aio(bh->ctx, bh->name); } - bh->reentrancy_guard->engaged_in_io = true; + reentrancy_guard->engaged_in_io = true; } bh->cb(bh->opaque); - if (bh->reentrancy_guard) { - bh->reentrancy_guard->engaged_in_io = last_engaged_in_io; + if (reentrancy_guard) { + reentrancy_guard->engaged_in_io = last_engaged_in_io; } } -- 2.39.3