Skip to contentSkip to conversation

coroutine: Make yield_until_fd_readable() detach fd handler always

PATCH Applied Peter Xu 2 participants 2 messages Nov 26

lore
Share & tools
lore

Peter Xu

author
#0 lore raw

PATCH v3 1/1 coroutine: Make yield_until_fd_readable() detach fd handler always

Hongyan reported a possible use-after-free of RDMA migration code:

https://lore.kernel.org/all/[email protected]

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")
Cc Stefan Hajnoczi <[email protected]>
Cc Kevin Wolf <[email protected]>
Cc Fabiano Rosas <[email protected]>
Cc Zhijian Li (Fujitsu) <[email protected]>
Cc Hongyan Xu <[email protected]>

---
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(-)
include/qemu/coroutine.h +4 -1
index 1c31de60f9..6b34dc750c 100644
@@ -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);
 
util/qemu-coroutine-io.c +5 -15
index 364f4d5abf..720d27d737 100644
@@ -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);
 }
Signature · 1 line
2.56.0
#1 lore raw
Quoted 32 lines · Peter Xu

Hongyan reported a possible use-after-free of RDMA migration code:

https://lore.kernel.org/all/[email protected]

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")
Cc Stefan Hajnoczi <[email protected]>
Cc Kevin Wolf <[email protected]>
Cc Fabiano Rosas <[email protected]>
Cc Zhijian Li (Fujitsu) <[email protected]>
Cc Hongyan Xu <[email protected]>

---
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]

Quoted 4 lines
---
 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