[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL 04/33] aio: Fix use-after-free in cancellation path
From: |
Stefan Hajnoczi |
Subject: |
[Qemu-devel] [PULL 04/33] aio: Fix use-after-free in cancellation path |
Date: |
Fri, 23 May 2014 17:41:36 +0200 |
From: Fam Zheng <address@hidden>
The current flow of canceling a thread from THREAD_ACTIVE state is:
1) Caller wants to cancel a request, so it calls thread_pool_cancel.
2) thread_pool_cancel waits on the conditional variable
elem->check_cancel.
3) The worker thread changes state to THREAD_DONE once the task is
done, and notifies elem->check_cancel to allow thread_pool_cancel
to continue execution, and signals the notifier (pool->notifier) to
allow callback function to be called later. But because of the
global mutex, the notifier won't get processed until step 4) and 5)
are done.
4) thread_pool_cancel continues, leaving the notifier signaled, it
just returns to caller.
5) Caller thinks the request is already canceled successfully, so it
releases any related data, such as freeing elem->common.opaque.
6) In the next main loop iteration, the notifier handler,
event_notifier_ready, is called. It finds the canceled thread in
THREAD_DONE state, so calls elem->common.cb, with an (likely)
dangling opaque pointer. This is a use-after-free.
Fix it by calling event_notifier_ready before leaving
thread_pool_cancel.
Test case update: This change will let cancel complete earlier than
test-thread-pool.c expects, so update the code to check this case: if
it's already done, done_cb sets .aiocb to NULL, skip calling
bdrv_aio_cancel on them.
Reported-by: Ulrich Obergfell <address@hidden>
Suggested-by: Paolo Bonzini <address@hidden>
Signed-off-by: Fam Zheng <address@hidden>
Signed-off-by: Stefan Hajnoczi <address@hidden>
---
tests/test-thread-pool.c | 2 +-
thread-pool.c | 1 +
2 files changed, 2 insertions(+), 1 deletion(-)
diff --git a/tests/test-thread-pool.c b/tests/test-thread-pool.c
index c1f8e13..aa156bc 100644
--- a/tests/test-thread-pool.c
+++ b/tests/test-thread-pool.c
@@ -180,7 +180,7 @@ static void test_cancel(void)
/* Canceling the others will be a blocking operation. */
for (i = 0; i < 100; i++) {
- if (data[i].n != 3) {
+ if (data[i].aiocb && data[i].n != 3) {
bdrv_aio_cancel(data[i].aiocb);
}
}
diff --git a/thread-pool.c b/thread-pool.c
index fbdd3ff..dfb699d 100644
--- a/thread-pool.c
+++ b/thread-pool.c
@@ -224,6 +224,7 @@ static void thread_pool_cancel(BlockDriverAIOCB *acb)
pool->pending_cancellations--;
}
qemu_mutex_unlock(&pool->lock);
+ event_notifier_ready(&pool->notifier);
}
static const AIOCBInfo thread_pool_aiocb_info = {
--
1.9.0
- [Qemu-devel] [PULL 00/33] Block patches, Stefan Hajnoczi, 2014/05/23
- [Qemu-devel] [PULL 01/33] qemu-iotests: Handle cache mode option in 091, Stefan Hajnoczi, 2014/05/23
- [Qemu-devel] [PULL 02/33] QemuOpt: add unit tests, Stefan Hajnoczi, 2014/05/23
- [Qemu-devel] [PULL 03/33] qcow2: Fix memory leak in COW error path, Stefan Hajnoczi, 2014/05/23
- [Qemu-devel] [PULL 04/33] aio: Fix use-after-free in cancellation path,
Stefan Hajnoczi <=
- [Qemu-devel] [PULL 06/33] block: Add BlockOpType enum, Stefan Hajnoczi, 2014/05/23
- [Qemu-devel] [PULL 07/33] block: Introduce op_blockers to BlockDriverState, Stefan Hajnoczi, 2014/05/23
- [Qemu-devel] [PULL 05/33] iotests: Use _img_info in test 089, Stefan Hajnoczi, 2014/05/23
- [Qemu-devel] [PULL 09/33] block: Move op_blocker check from block_job_create to its caller, Stefan Hajnoczi, 2014/05/23
- [Qemu-devel] [PULL 08/33] block: Replace in_use with operation blocker, Stefan Hajnoczi, 2014/05/23
- [Qemu-devel] [PULL 10/33] block: Add bdrv_set_backing_hd(), Stefan Hajnoczi, 2014/05/23
- [Qemu-devel] [PULL 11/33] block: Use bdrv_set_backing_hd everywhere, Stefan Hajnoczi, 2014/05/23
- [Qemu-devel] [PULL 12/33] block: Add backing_blocker in BlockDriverState, Stefan Hajnoczi, 2014/05/23
- [Qemu-devel] [PULL 13/33] block: Drop redundant bdrv_refresh_limits, Stefan Hajnoczi, 2014/05/23
- [Qemu-devel] [PULL 14/33] docs: Define refcount_bits value, Stefan Hajnoczi, 2014/05/23