[PATCH for-12.0 v3] coroutine: Make yield_until_fd_readable() detach fd handler always

Peter Xu posted 1 patch 1 day, 5 hours ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20261126201445.3326530-1-peterx@redhat.com
Maintainers: Stefan Hajnoczi <stefanha@redhat.com>, Kevin Wolf <kwolf@redhat.com>
include/qemu/coroutine.h |  5 ++++-
util/qemu-coroutine-io.c | 20 +++++---------------
2 files changed, 9 insertions(+), 16 deletions(-)
[PATCH for-12.0 v3] coroutine: Make yield_until_fd_readable() detach fd handler always
Posted by Peter Xu 1 day, 5 hours ago
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.

Fixes: 923709896b1b ("migration: poll the cm event for destination qemu")
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>
---
v2:
- Drop FDYieldUntilData altogether [Kevin]
- Drop the comment, doesn't feel as helpful now [myself]
v3:
- Add doc for the helper [Stefan]
- Add Fixes [Stefan]
---
 include/qemu/coroutine.h |  5 ++++-
 util/qemu-coroutine-io.c | 20 +++++---------------
 2 files changed, 9 insertions(+), 16 deletions(-)

diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h
index 1c31de60f9..6b34dc750c 100644
--- a/include/qemu/coroutine.h
+++ b/include/qemu/coroutine.h
@@ -279,7 +279,10 @@ void qemu_co_sleep_wake(QemuCoSleep *w);
 /**
  * Yield until a file descriptor becomes readable
  *
- * Note that this function clobbers the handlers for the file descriptor.
+ * Notes:
+ * (1) this function clobbers the handlers for the file descriptor.
+ * (2) this function may be interrupted by another re-enter of coroutine,
+ *     the caller needs to be prepared on fd not readable after returned.
  */
 void coroutine_fn yield_until_fd_readable(int fd);
 
diff --git a/util/qemu-coroutine-io.c b/util/qemu-coroutine-io.c
index 364f4d5abf..720d27d737 100644
--- a/util/qemu-coroutine-io.c
+++ b/util/qemu-coroutine-io.c
@@ -65,28 +65,18 @@ qemu_co_send_recv(int sockfd, void *buf, size_t bytes, bool do_send)
     return qemu_co_sendv_recvv(sockfd, &iov, 1, 0, bytes, do_send);
 }
 
-typedef struct {
-    AioContext *ctx;
-    Coroutine *co;
-    int fd;
-} FDYieldUntilData;
-
 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);
+    qemu_coroutine_enter((Coroutine *)opaque);
 }
 
 void coroutine_fn yield_until_fd_readable(int fd)
 {
-    FDYieldUntilData data;
+    AioContext *ctx = qemu_get_current_aio_context();
 
     assert(qemu_in_coroutine());
-    data.ctx = qemu_get_current_aio_context();
-    data.co = qemu_coroutine_self();
-    data.fd = fd;
-    aio_set_fd_handler(data.ctx, fd, fd_coroutine_enter, NULL, NULL, NULL,
-                       &data);
+    aio_set_fd_handler(ctx, fd, fd_coroutine_enter, NULL, NULL, NULL,
+                       (void *)qemu_coroutine_self());
     qemu_coroutine_yield();
+    aio_set_fd_handler(ctx, fd, NULL, NULL, NULL, NULL, NULL);
 }
-- 
2.56.0
Re: [PATCH for-12.0 v3] coroutine: Make yield_until_fd_readable() detach fd handler always
Posted by Stefan Hajnoczi 1 day, 4 hours ago
On Thu, Nov 26, 2026 at 03:14:45PM -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.
> 
> Fixes: 923709896b1b ("migration: poll the cm event for destination qemu")
> 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>
> ---
> v2:
> - Drop FDYieldUntilData altogether [Kevin]
> - Drop the comment, doesn't feel as helpful now [myself]
> v3:
> - Add doc for the helper [Stefan]
> - Add Fixes [Stefan]
> ---
>  include/qemu/coroutine.h |  5 ++++-
>  util/qemu-coroutine-io.c | 20 +++++---------------
>  2 files changed, 9 insertions(+), 16 deletions(-)

Thanks, applied to my block-next tree:
https://gitlab.com/stefanha/qemu/commits/block-next

The latest qemu.git tag is v11.2.0-rc2 and this bug is not a regression
in the current release (commit 923709896b1b is from 2018), so I'm
queuing this on the block-next branch for QEMU v12.0.0. If there is a
reason to still put this into v11.2.0-rc3, please let me know.

CCing -stable too

Stefan