qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Stefan Hajnoczi <stefanha@redhat.com>
To: qemu-devel@nongnu.org
Cc: Peter Maydell <peter.maydell@linaro.org>,
	Fam Zheng <famz@redhat.com>,
	Stefan Hajnoczi <stefanha@redhat.com>
Subject: [Qemu-devel] [PULL 04/33] aio: Fix use-after-free in cancellation path
Date: Fri, 23 May 2014 17:41:36 +0200	[thread overview]
Message-ID: <1400859725-31879-5-git-send-email-stefanha@redhat.com> (raw)
In-Reply-To: <1400859725-31879-1-git-send-email-stefanha@redhat.com>

From: Fam Zheng <famz@redhat.com>

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 <uobergfe@redhat.com>
Suggested-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Fam Zheng <famz@redhat.com>
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
 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

  parent reply	other threads:[~2014-05-23 15:42 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-23 15:41 [Qemu-devel] [PULL 00/33] Block patches Stefan Hajnoczi
2014-05-23 15:41 ` [Qemu-devel] [PULL 01/33] qemu-iotests: Handle cache mode option in 091 Stefan Hajnoczi
2014-05-23 15:41 ` [Qemu-devel] [PULL 02/33] QemuOpt: add unit tests Stefan Hajnoczi
2014-05-23 15:41 ` [Qemu-devel] [PULL 03/33] qcow2: Fix memory leak in COW error path Stefan Hajnoczi
2014-05-23 15:41 ` Stefan Hajnoczi [this message]
2014-05-23 15:41 ` [Qemu-devel] [PULL 05/33] iotests: Use _img_info in test 089 Stefan Hajnoczi
2014-05-23 15:41 ` [Qemu-devel] [PULL 06/33] block: Add BlockOpType enum Stefan Hajnoczi
2014-05-23 15:41 ` [Qemu-devel] [PULL 07/33] block: Introduce op_blockers to BlockDriverState Stefan Hajnoczi
2014-05-23 15:41 ` [Qemu-devel] [PULL 08/33] block: Replace in_use with operation blocker Stefan Hajnoczi
2014-05-23 15:41 ` [Qemu-devel] [PULL 09/33] block: Move op_blocker check from block_job_create to its caller Stefan Hajnoczi
2014-05-23 15:41 ` [Qemu-devel] [PULL 10/33] block: Add bdrv_set_backing_hd() Stefan Hajnoczi
2014-05-23 15:41 ` [Qemu-devel] [PULL 11/33] block: Use bdrv_set_backing_hd everywhere Stefan Hajnoczi
2014-05-23 15:41 ` [Qemu-devel] [PULL 12/33] block: Add backing_blocker in BlockDriverState Stefan Hajnoczi
2014-05-23 15:41 ` [Qemu-devel] [PULL 13/33] block: Drop redundant bdrv_refresh_limits Stefan Hajnoczi
2014-05-23 15:41 ` [Qemu-devel] [PULL 14/33] docs: Define refcount_bits value Stefan Hajnoczi
2014-05-23 15:41 ` [Qemu-devel] [PULL 15/33] blockdev: Don't use qerror_report_err() in drive_init() Stefan Hajnoczi
2014-05-23 15:41 ` [Qemu-devel] [PULL 16/33] blockdev: Don't use qerror_report() in do_drive_del() Stefan Hajnoczi
2014-05-23 15:41 ` [Qemu-devel] [PULL 17/33] qemu-nbd: Don't use qerror_report() Stefan Hajnoczi
2014-05-23 15:41 ` [Qemu-devel] [PULL 18/33] block/rbd: Propagate errors to open and create methods Stefan Hajnoczi
2014-05-23 15:41 ` [Qemu-devel] [PULL 19/33] block/ssh: Drop superfluous libssh2_session_last_errno() calls Stefan Hajnoczi
2014-05-23 15:41 ` [Qemu-devel] [PULL 20/33] block/ssh: Propagate errors through check_host_key() Stefan Hajnoczi
2014-05-23 15:41 ` [Qemu-devel] [PULL 21/33] block/ssh: Propagate errors through authenticate() Stefan Hajnoczi
2014-05-23 15:41 ` [Qemu-devel] [PULL 22/33] block/ssh: Propagate errors through connect_to_ssh() Stefan Hajnoczi
2014-05-23 15:41 ` [Qemu-devel] [PULL 23/33] block/ssh: Propagate errors to open and create methods Stefan Hajnoczi
2014-05-23 15:41 ` [Qemu-devel] [PULL 24/33] block/vvfat: Propagate errors through enable_write_target() Stefan Hajnoczi
2014-05-23 15:41 ` [Qemu-devel] [PULL 25/33] block/vvfat: Propagate errors through init_directories() Stefan Hajnoczi
2014-05-23 15:41 ` [Qemu-devel] [PULL 26/33] block/sheepdog: Propagate errors through connect_to_sdog() Stefan Hajnoczi
2014-05-23 15:41 ` [Qemu-devel] [PULL 27/33] block/sheepdog: Propagate errors through get_sheep_fd() Stefan Hajnoczi
2014-05-23 15:42 ` [Qemu-devel] [PULL 28/33] block/sheepdog: Propagate errors through sd_prealloc() Stefan Hajnoczi
2014-05-23 15:42 ` [Qemu-devel] [PULL 29/33] block/sheepdog: Propagate errors through do_sd_create() Stefan Hajnoczi
2014-05-23 15:42 ` [Qemu-devel] [PULL 30/33] block/sheepdog: Propagate errors through find_vdi_name() Stefan Hajnoczi
2014-05-23 15:42 ` [Qemu-devel] [PULL 31/33] block/sheepdog: Propagate errors to open and create methods Stefan Hajnoczi
2014-05-23 15:42 ` [Qemu-devel] [PULL 32/33] block/sheepdog: Fix silent sd_open(), sd_create() failures Stefan Hajnoczi
2014-05-23 15:42 ` [Qemu-devel] [PULL 33/33] block/sheepdog: Don't use qerror_report() Stefan Hajnoczi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1400859725-31879-5-git-send-email-stefanha@redhat.com \
    --to=stefanha@redhat.com \
    --cc=famz@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).