Received: from eggs.gnu.org ([2001:4830:134:3::10]:47545) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Wn7Lj-0006hi-CK for qemu-devel@nongnu.org; Wed, 21 May 2014 10:18:36 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Wn7Ld-0002gK-Pm for qemu-devel@nongnu.org; Wed, 21 May 2014 10:18:31 -0400 Received: from mx1.redhat.com ([209.132.183.28]:6206) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Wn7Ld-0002fp-GT for qemu-devel@nongnu.org; Wed, 21 May 2014 10:18:25 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s4LEIOx2012159 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Wed, 21 May 2014 10:18:24 -0400 Date: Wed, 21 May 2014 16:18:23 +0200 From: Stefan Hajnoczi Message-ID: <20140521141823.GK6619@stefanha-thinkpad.redhat.com> References: <1400640133-19836-1-git-send-email-famz@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1400640133-19836-1-git-send-email-famz@redhat.com> Subject: Re: [Qemu-devel] [PATCH v3] aio: Fix use-after-free in cancellation path List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Fam Zheng Cc: Paolo Bonzini , qemu-devel@nongnu.org, uobergfe@redhat.com On Wed, May 21, 2014 at 10:42:13AM +0800, Fam Zheng wrote: > 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 > Suggested-by: Paolo Bonzini > Signed-off-by: Fam Zheng > > --- > v3: Call event_notifier_ready after 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