All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH for-12.0] coroutine: Make yield_until_fd_readable() detach fd handler always
@ 2026-11-19 14:53 Peter Xu
  2026-11-19 22:29 ` Stefan Hajnoczi
  2026-11-20 17:03 ` Kevin Wolf
  0 siblings, 2 replies; 6+ messages in thread
From: Peter Xu @ 2026-11-19 14:53 UTC (permalink / raw)
  To: qemu-devel
  Cc: peterx, Hongyan Xu, Stefan Hajnoczi, Kevin Wolf, Fabiano Rosas,
	Zhijian Li (Fujitsu)

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);
 }
-- 
2.56.0



^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-11-20 17:12 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2026-11-20 17:03 ` Kevin Wolf

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.