From: Paolo Bonzini <pbonzini@redhat.com>
To: Stefan Hajnoczi <stefanha@redhat.com>, qemu-devel@nongnu.org
Cc: "Fam Zheng" <fam@euphon.net>,
"Philippe Mathieu-Daudé" <philmd@redhat.com>,
qemu-block@nongnu.org
Subject: Re: [Qemu-devel] [PATCH] util/async: hold AioContext ref to prevent use-after-free
Date: Wed, 24 Jul 2019 10:47:28 +0200 [thread overview]
Message-ID: <7287eae6-0c19-e082-9a8c-4fbd47afcd94@redhat.com> (raw)
In-Reply-To: <20190723190623.21537-1-stefanha@redhat.com>
On 23/07/19 21:06, Stefan Hajnoczi wrote:
> The tests/test-bdrv-drain /bdrv-drain/iothread/drain test case does the
> following:
>
> 1. The preadv coroutine calls aio_bh_schedule_oneshot() and then yields.
> 2. The one-shot BH executes in another AioContext. All it does is call
> aio_co_wakeup(preadv_co).
> 3. The preadv coroutine is re-entered and returns.
>
> There is a race condition in aio_co_wake() where the preadv coroutine
> returns and the test case destroys the preadv IOThread. aio_co_wake()
> can still be running in the other AioContext and it performs an access
> to the freed IOThread AioContext.
>
> Here is the race in aio_co_schedule():
>
> QSLIST_INSERT_HEAD_ATOMIC(&ctx->scheduled_coroutines,
> co, co_scheduled_next);
> <-- race: co may execute before we invoke qemu_bh_schedule()!
> qemu_bh_schedule(ctx->co_schedule_bh);
>
> So if co causes ctx to be freed then we're in trouble. Fix this problem
> by holding a reference to ctx.
>
> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
> ---
> util/async.c | 8 ++++++++
> 1 file changed, 8 insertions(+)
>
> diff --git a/util/async.c b/util/async.c
> index 8d2105729c..4e4c7af51e 100644
> --- a/util/async.c
> +++ b/util/async.c
> @@ -459,9 +459,17 @@ void aio_co_schedule(AioContext *ctx, Coroutine *co)
> abort();
> }
>
> + /* The coroutine might run and release the last ctx reference before we
> + * invoke qemu_bh_schedule(). Take a reference to keep ctx alive until
> + * we're done.
> + */
> + aio_context_ref(ctx);
> +
> QSLIST_INSERT_HEAD_ATOMIC(&ctx->scheduled_coroutines,
> co, co_scheduled_next);
> qemu_bh_schedule(ctx->co_schedule_bh);
> +
> + aio_context_unref(ctx);
> }
>
> void aio_co_wake(struct Coroutine *co)
>
This must have been painful to debug.
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Paolo
next prev parent reply other threads:[~2019-07-24 8:48 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-07-23 19:06 [Qemu-devel] [PATCH] util/async: hold AioContext ref to prevent use-after-free Stefan Hajnoczi
2019-07-23 19:09 ` [Qemu-devel] [Qemu-block] " Stefan Hajnoczi
2019-07-24 10:54 ` Philippe Mathieu-Daudé
2019-07-24 8:47 ` Paolo Bonzini [this message]
2019-07-24 11:42 ` [Qemu-devel] " 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=7287eae6-0c19-e082-9a8c-4fbd47afcd94@redhat.com \
--to=pbonzini@redhat.com \
--cc=fam@euphon.net \
--cc=philmd@redhat.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@redhat.com \
/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).