All of lore.kernel.org
 help / color / mirror / Atom feed
From: Stefan Hajnoczi <stefanha@redhat.com>
To: Peter Xu <peterx@redhat.com>
Cc: qemu-devel@nongnu.org, Hongyan Xu <getshell@seu.edu.cn>,
	Kevin Wolf <kwolf@redhat.com>, 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 12:01:12 -0500	[thread overview]
Message-ID: <20261120170112.GD780462@shajnocz.csb> (raw)
In-Reply-To: <av-AweS887ZVL9HO@zhexu-thinkpadt14gen5.rmtcaon.csb>

On Thu, Nov 19, 2026 at 06:05:37PM -0500, Peter Xu wrote:
> On Thu, Nov 19, 2026 at 05:29:36PM -0500, Stefan Hajnoczi wrote:
> > 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?
> 
> In case of RDMA, rdma_cm_poll_handler() could receive a disconnect event on
> another management fd, then it knows the link is down, hence it needs to
> kickoff this coroutine to quit.  See 923709896b1.

Commit 923709896b1 is not safe in general. It would be necessary to
audit all yield points and see how they react to an unexpected coroutine
re-entry. There could be more bugs related to this.

> > 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?
> 
> I feel like these are two separate things.
> 
> IMO fixing reset of aio handler should be a pure positive fix, because not
> fixing it doesn't stop user from re-entering of the coroutine and bypass
> fd_coroutine_enter().
> 
> My understanding is the caller should have full control of this: when the
> caller requires "fd must be readable", it should always wait until
> fd_coroutine_enter() returns.  This patch should have no functional
> difference.

I don't understand this paragraph because the caller has no way of
knowing whether fd_coroutine_enter() ran when yield_until_fd_readable()
returns with this patch applied. How can the caller wait until
fd_coroutine_enter() returns?

> But it seems still legal if the caller is benign and can properly handle
> the case even if fd is still not readable.  In this case, this patch fixes
> a real issue.
> 
> In case of RDMA migration, only the quit path can cause it to re-enter, so
> either there is IO or we need to quit.  RDMA code works well with either
> event, it can even loop back and wait on the fd again, but since quit is
> the only path kicking this off, it's also not possible even if the caller
> handles this.

The caller of yield_until_fd_readable() expects the function return to
mean the fd is now readable, not that the fd is readable or something
else happened. At minimum this patch would need to add a comment to
clarify this and then audit all callers to be sure they can handle those
semantics correctly.

It would be better to also add a return value differentiating between
success and failure (due to interruption) so that callers can propagate
the interruption (a caller may be several frames into a call stack where
a parent function will loop on early return, defeating the purpose of
reentering the coroutine to notify it that the connection on a separate
fd was dropped, so it must be possible to propagate the interruption up
the call stack).

Any part of QEMU could use util/qemu-coroutine-io.c, so even if this
patch happens to work for RDMA migration, the yield_until_fd_readable()
API needs a more disciplined approach so that it can be used correctly
by anyone.

Stefan



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

Thread overview: 8+ 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 [this message]
2026-11-20 21:45       ` Peter Xu
2026-11-20 17:11   ` Kevin Wolf
2026-11-20 17:03 ` Kevin Wolf
2026-11-20 21:46   ` Peter Xu

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=20261120170112.GD780462@shajnocz.csb \
    --to=stefanha@redhat.com \
    --cc=farosas@suse.de \
    --cc=getshell@seu.edu.cn \
    --cc=kwolf@redhat.com \
    --cc=lizhijian@fujitsu.com \
    --cc=peterx@redhat.com \
    --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 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.