All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kevin Wolf <kwolf@redhat.com>
To: Stefan Hajnoczi <stefanha@redhat.com>
Cc: Peter Xu <peterx@redhat.com>,
	qemu-devel@nongnu.org, Hongyan Xu <getshell@seu.edu.cn>,
	Fabiano Rosas <farosas@suse.de>,
	"Zhijian Li (Fujitsu)" <lizhijian@fujitsu.com>
Subject: Re: [PATCH for-12.0] coroutine: Make yield_until_fd_readable() detach fd handler always
Date: Fri, 20 Nov 2026 18:11:19 +0100	[thread overview]
Message-ID: <awB_NzfDzZiML2n_@redhat.com> (raw)
In-Reply-To: <20261119222936.GB780462@shajnocz.csb>

Am 19.11.2026 um 23:29 hat Stefan Hajnoczi geschrieben:
> On Thu, Nov 19, 2026 at 09:53:11AM -0500, Peter Xu wrote:
> > Hongyan reported a possible use-after-free of RDMA migration code:
> > 
> > https://lore.kernel.org/all/20260904033130.276-1-getshell@seu.edu.cn
> > 
> > The issue seems real, while this patch is a proposal to fix it in a simpler
> > way, meanwhile it should be a problem to the yield_until_fd_readable()
> > helper in general, so it should make more sense to be fixed there too.
> > 
> > The problem is about the use of the stack allocated FDYieldUntilData where
> > we must make sure after yield_until_fd_readable() returns it's not used
> > anywhere.  If the coroutine yielded can be entered from outside of this
> > helper (fd_coroutine_enter()), then the aio handler might be installed even
> > after yield_until_fd_readable() returns, and that makes the stack variable
> > accessible by aio codd, even if stack gone.
> > 
> > Fix it by moving the aio handler removal outside the handler.
> > 
> > Reported-by: Hongyan Xu <getshell@seu.edu.cn>
> > Cc: Stefan Hajnoczi <stefanha@redhat.com>
> > Cc: Kevin Wolf <kwolf@redhat.com>
> > Cc: Fabiano Rosas <farosas@suse.de>
> > Cc: Zhijian Li (Fujitsu) <lizhijian@fujitsu.com>
> > Cc: Hongyan Xu <getshell@seu.edu.cn>
> > Signed-off-by: Peter Xu <peterx@redhat.com>
> > ---
> > 
> > NOTE: I targeted this for 12.0 because the bug was not introduced in 11.2,
> > however this seems to be a real bug fix to coroutine in general, I'll leave
> > that to maintainers to decide if to pick it for 11.2.
> > ---
> >  util/qemu-coroutine-io.c | 6 +++++-
> >  1 file changed, 5 insertions(+), 1 deletion(-)
> > 
> > diff --git a/util/qemu-coroutine-io.c b/util/qemu-coroutine-io.c
> > index 364f4d5abf..3c0a7feafa 100644
> > --- a/util/qemu-coroutine-io.c
> > +++ b/util/qemu-coroutine-io.c
> > @@ -74,7 +74,6 @@ typedef struct {
> >  static void fd_coroutine_enter(void *opaque)
> >  {
> >      FDYieldUntilData *data = opaque;
> > -    aio_set_fd_handler(data->ctx, data->fd, NULL, NULL, NULL, NULL, NULL);
> >      qemu_coroutine_enter(data->co);
> >  }
> >  
> > @@ -89,4 +88,9 @@ void coroutine_fn yield_until_fd_readable(int fd)
> >      aio_set_fd_handler(data.ctx, fd, fd_coroutine_enter, NULL, NULL, NULL,
> >                         &data);
> >      qemu_coroutine_yield();
> > +    /*
> > +     * Always remove the fd handler, no matter the re-entering of this
> > +     * coroutine was caused by fd_coroutine_enter() or something else.
> > +     */
> > +    aio_set_fd_handler(data.ctx, fd, NULL, NULL, NULL, NULL, NULL);
> >  }
> 
> Why would the coroutine be entered? Should there be a loop around
> qemu_coroutine_yield() - otherwise we fix the invalid stack memory
> access but return to the caller without fulfilling the purpose of the
> function (waiting for the fd to become readable), which could cause
> further bugs?

A loop around qemu_coroutine_yield() is almost never what you want. It
may happen in cases where you spawn multiple operations and wait for all
of them to complete, but if you're just waiting for a condition to
become true, then entering the coroutine before the condition is true
means that the caller is doing something wrong.

In this case, I think the additional caller intentionally wants to
cancel the waiting.

Having multiple places that can reenter a coroutine tends to be tricky
because aio_co_wake() introduces a window where both places could
trigger, which leads to an abort(). But here, qemu_coroutine_enter() is
directly used (which also implies that the coroutine and all the places
that reenter it are in the same thread), so it should be okay.

Kevin



  parent reply	other threads:[~2026-11-20 17:12 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 14:53 [PATCH for-12.0] coroutine: Make yield_until_fd_readable() detach fd handler always Peter Xu
2026-11-19 22:29 ` Stefan Hajnoczi
2026-11-19 23:05   ` Peter Xu
2026-11-20 17:01     ` Stefan Hajnoczi
2026-11-20 17:11   ` Kevin Wolf [this message]
2026-11-20 17:03 ` Kevin Wolf

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=awB_NzfDzZiML2n_@redhat.com \
    --to=kwolf@redhat.com \
    --cc=farosas@suse.de \
    --cc=getshell@seu.edu.cn \
    --cc=lizhijian@fujitsu.com \
    --cc=peterx@redhat.com \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.