coroutine: Make yield_until_fd_readable() detach fd handler always
PATCH v2 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.
---
v2:
- Drop FDYieldUntilData altogether [Kevin]
- Drop the comment, doesn't feel as helpful now [myself]
--- util/qemu-coroutine-io.c | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-)
util/qemu-coroutine-io.c +5 -15
@@ -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