aio: Fix use-after-free in cancellation path
PATCH v3 1/1 aio: Fix use-after-free in cancellation path
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.
---
v3: Call event_notifier_ready after unlock.
--- tests/test-thread-pool.c | 2 +- thread-pool.c | 1 + 2 files changed, 2 insertions(+), 1 deletion(-)
tests/test-thread-pool.c +1 -1
@@ -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); } }
thread-pool.c +1 -0
@@ -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 = {
Signature · 1 line
1.9.2
Stefan Hajnoczi
AppliedQuoted 37 lines · Fam Zheng
The current flow of canceling a thread from
THREAD_ACTIVEstate is:1) Caller wants to cancel a request, so it calls
thread_pool_cancel.2)
thread_pool_cancelwaits on the conditional variableelem->check_cancel.3) The worker thread changes state to
THREAD_DONEonce the task is done, and notifieselem->check_cancelto allowthread_pool_cancelto 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_cancelcontinues, 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 inTHREAD_DONEstate, so callselem->common.cb, with an (likely) dangling opaque pointer. This is a use-after-free.Fix it by calling
event_notifier_readybefore leavingthread_pool_cancel.Test case update: This change will let cancel complete earlier than
test-thread-pool.cexpects, so update the code to check this case: if it's already done,done_cbsets .aiocb to NULL, skip callingbdrv_aio_cancelon them.Reported-by Ulrich Obergfell <[email protected]>Suggested-by Paolo Bonzini <[email protected]>Signed-off-by Fam Zheng <[email protected]>
Quoted 6 lines
---
v3: Callevent_notifier_readyafter unlock.--- tests/test-thread-pool.c | 2 +- thread-pool.c | 1 + 2 files changed, 2 insertions(+), 1 deletion(-)
Thanks, applied to my block tree:
https://github.com/stefanha/qemu/commits/block
Stefan