9p: fix guest-triggered Treaddir/ACPI eject UAF
v2 cover 9p: fix guest-triggered Treaddir/ACPI eject UAF
A guest can trigger unplugging 9pfs server's virtio-pci device via ACPI eject. As a consequence the device is unrealized, server's internal state is freed while pending coroutines would still have access to them, causing a potential heap-use-after-free.
Overview Patches:
- Patch 1: this is the core fix, that drains all PDUs (i.e. coroutines that handle individual pending requests in parallel) before freeing server state.
- Patch 2: fixes a similar identified issue with the Xen transport, even though not triggered via ACPI, it is also prone to UAF, plus a resource leak.
v2:
- Patch 1: Make Jia the official author of this patch.
- Drop prev. patch 2 ("hw/9pfs/virtio: disable hotpluggable property...")
- Patch 2: defer explict xen_9pfs_disconnect() call from error paths of xen_9pfs_pdu_vmarshal() and xen_9pfs_pdu_vunmarshal().
Christian Schoenebeck (1):
hw/9pfs/xen: drain in-flight PDUs before xen-9p disconnect
Jia Jia (1):
hw/9pfs/virtio: drain in-flight PDUs before virtio-9p unrealize
hw/9pfs/virtio-9p-device.c | 1 + hw/9pfs/xen-9p-backend.c | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-)
Signature · 1 line
2.47.3
PATCH v2 1/2 hw/9pfs/virtio: drain in-flight PDUs before virtio-9p unrealize
From: Jia Jia <[email protected]>
A guest can trigger a heap-use-after-free in the virtio transport unrealize path by submitting a Treaddir request and immediately ejecting the device via ACPI PCI hotplug. The unrealize path frees struct LocalData while a worker thread still holds a reference on it, causing a UAF in local_open_nofollow().
Fix this by draining all in-flight 9p PDUs by calling v9fs_reset() before final server cleanup. This ensures all coroutines completed, all FIDs are closed, and no worker thread still holds references on 9p server state when it is freed.
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3937
--- hw/9pfs/virtio-9p-device.c | 1 + 1 file changed, 1 insertion(+)
hw/9pfs/virtio-9p-device.c +1 -0
@@ -243,6 +243,7 @@ static void virtio_9p_device_unrealize(DeviceState *dev)V9fsVirtioState *v =VIRTIO_9P (dev);V9fsState *s = &v->state; +v9fs_reset (s);virtio_delete_queue (v->vq);virtio_cleanup (vdev);v9fs_device_unrealize_common (s);
Signature · 1 line
2.47.3
PATCH v2 2/2 hw/9pfs/xen: drain in-flight PDUs before xen-9p disconnect
In reply to Christian SchoenebeckThe xen-9p disconnect path has two issues:
1. It frees the Xen9pfsRing structures while in-flight PDUs may still reference them via pdu->tag to index rings[]. This causes a UAF in xen_9pfs_push_and_notify() when worker threads resume after completing filesystem operations.
2. It never calls v9fs_device_unrealize_common(), which means server state (struct LocalData, mountfd, FIDs) is never cleaned up on disconnect, causing a resource leak on every guest-initiated disconnect.
Fix both by draining in-flight PDUs via v9fs_reset() before tearing down rings, and calling v9fs_device_unrealize_common() to clean up server state.
Additionally, explicit calls of xen_9pfs_disconnect() in the error paths of xen_9pfs_pdu_vmarshal() and xen_9pfs_pdu_vunmarshal() must be deferred (via aio_bh_schedule_oneshot()), because xen_9pfs_pdu_v(un)marshal() are running within a coroutine context which makes them unsafe [1] for calling v9fs_reset() directly, as the latter e.g. has a loop like:
while (!QLIST_EMPTY(&s->active_list)) {
aio_poll(qemu_get_aio_context(), true);
}which would a) never terminate (as the coroutine is on the active_list) and b) aio_poll() is marked as no_coroutine_fn.
[1] https://lore.kernel.org/qemu-devel/3351181.5fSG56mABF@weasel/
And finally, add an idempotent guard to xen_9pfs_disconnect(), just for the case.
--- hw/9pfs/xen-9p-backend.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-)
hw/9pfs/xen-9p-backend.c +17 -2
@@ -68,6 +68,11 @@ typedef struct Xen9pfsDev {static void xen_9pfs_disconnect (struct XenLegacyDevice *xendev); +static void xen_9pfs_disconnect_bh (void *opaque) +{ +xen_9pfs_disconnect (opaque); +} + static void xen_9pfs_in_sg(Xen9pfsRing *ring, struct iovec *in_sg, int *num,
@@ -150,7 +155,8 @@ static ssize_t xen_9pfs_pdu_vmarshal(V9fsPDU *pdu,"Failed to encode VirtFS reply type %d\n" , pdu->id + 1);xen_be_set_state (&xen_9pfs->xendev, XenbusStateClosing); -xen_9pfs_disconnect (&xen_9pfs->xendev); +aio_bh_schedule_oneshot (qemu_get_aio_context (), + xen_9pfs_disconnect_bh, &xen_9pfs->xendev); }return ret; }
@@ -173,7 +179,8 @@ static ssize_t xen_9pfs_pdu_vunmarshal(V9fsPDU *pdu,xen_pv_printf (&xen_9pfs->xendev,0 ,"Failed to decode VirtFS request type %d\n" , pdu->id);xen_be_set_state (&xen_9pfs->xendev, XenbusStateClosing); -xen_9pfs_disconnect (&xen_9pfs->xendev); +aio_bh_schedule_oneshot (qemu_get_aio_context (), + xen_9pfs_disconnect_bh, &xen_9pfs->xendev); }return ret; }
@@ -368,10 +375,18 @@ static void xen_9pfs_evtchn_event(void *opaque)static void xen_9pfs_disconnect (struct XenLegacyDevice *xendev) {Xen9pfsDev *xen_9pdev =container_of (xendev, Xen9pfsDev, xendev); +V9fsState *s = &xen_9pdev->state;int i; +if (!xen_9pdev->rings) { +return ; + } +trace_xen_9pfs_disconnect (xendev->name); +v9fs_reset (s); +v9fs_device_unrealize_common (s); +for (i =0 ; i < xen_9pdev->num_rings; i++) {if (xen_9pdev->rings[i].evtchndev !=NULL ) { qemu_set_fd_handler(qemu_xen_evtchn_fd(xen_9pdev->rings[i].evtchndev),
Signature · 1 line
2.47.3
Re: [PATCH v2 2/2] hw/9pfs/xen: drain in-flight PDUs before xen-9p disconnect
Hi Stefano,
chance that you might be looking at this v2 Xen patch soon? If not, just let me know, then I'll send out a PR with other fixes piled up on my side in the meantime, as QEMU release window narrows.
/Christian
Quoted 97 lines · On Saturday, 18 July 2026 18:50:06 CEST Christian Schoenebeck wrote:
The xen-9p disconnect path has two issues:
1. It frees the Xen9pfsRing structures while in-flight PDUs may still reference them via
pdu->tagto index rings[]. This causes a UAF inxen_9pfs_push_and_notify()when worker threads resume after completing filesystem operations.2. It never calls
v9fs_device_unrealize_common(), which means server state (struct LocalData, mountfd, FIDs) is never cleaned up on disconnect, causing a resource leak on every guest-initiated disconnect.Fix both by draining in-flight PDUs via
v9fs_reset()before tearing down rings, and callingv9fs_device_unrealize_common()to clean up server state.Additionally, explicit calls of
xen_9pfs_disconnect()in the error paths ofxen_9pfs_pdu_vmarshal()andxen_9pfs_pdu_vunmarshal()must be deferred (viaaio_bh_schedule_oneshot()), because xen_9pfs_pdu_v(un)marshal() are running within a coroutine context which makes them unsafe [1] for callingv9fs_reset()directly, as the latter e.g. has a loop like:while (!QLIST_EMPTY(&s->active_list)) { aio_poll(qemu_get_aio_context(), true); }which would a) never terminate (as the coroutine is on the
active_list) and b)aio_poll()is marked asno_coroutine_fn.[1] https://lore.kernel.org/qemu-devel/3351181.5fSG56mABF@weasel/
And finally, add an idempotent guard to
xen_9pfs_disconnect(), just for the case.Fixes b37eeb0201 ("xen/9pfs: introduce Xen 9pfs backend")Signed-off-by Christian Schoenebeck <[email protected]>--- hw/9pfs/xen-9p-backend.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-)hw/9pfs/xen-9p-backend.c +17
-2@@ -68,6 +68,11 @@ typedef struct Xen9pfsDev {static void xen_9pfs_disconnect (struct XenLegacyDevice *xendev); +static void xen_9pfs_disconnect_bh (void *opaque) +{ +xen_9pfs_disconnect (opaque); +} + static void xen_9pfs_in_sg(Xen9pfsRing *ring, struct iovec *in_sg, int *num,@@ -150,7 +155,8 @@ static ssize_t xen_9pfs_pdu_vmarshal(V9fsPDU *pdu,"Failed to encode VirtFS reply type %d\n" , pdu->id + 1);xen_be_set_state (&xen_9pfs->xendev, XenbusStateClosing); -xen_9pfs_disconnect (&xen_9pfs->xendev); +aio_bh_schedule_oneshot (qemu_get_aio_context (), + xen_9pfs_disconnect_bh, &xen_9pfs->xendev); }return ret; }@@ -173,7 +179,8 @@ static ssize_t xen_9pfs_pdu_vunmarshal(V9fsPDU *pdu,xen_pv_printf (&xen_9pfs->xendev,0 ,"Failed to decode VirtFS request type %d\n" , pdu->id);xen_be_set_state (&xen_9pfs->xendev, XenbusStateClosing); -xen_9pfs_disconnect (&xen_9pfs->xendev); +aio_bh_schedule_oneshot (qemu_get_aio_context (), + xen_9pfs_disconnect_bh, &xen_9pfs->xendev); }return ret; }@@ -368,10 +375,18 @@ static void xen_9pfs_evtchn_event(void *opaque)static void xen_9pfs_disconnect (struct XenLegacyDevice *xendev) {Xen9pfsDev *xen_9pdev =container_of (xendev, Xen9pfsDev, xendev); +V9fsState *s = &xen_9pdev->state;int i; +if (!xen_9pdev->rings) { +return ; + } +trace_xen_9pfs_disconnect (xendev->name); +v9fs_reset (s); +v9fs_device_unrealize_common (s); +for (i =0 ; i < xen_9pdev->num_rings; i++) {if (xen_9pdev->rings[i].evtchndev !=NULL ) {qemu_set_fd_handler(qemu_xen_evtchn_fd(xen_9pdev->rings[i].evtchndev),
Re: [PATCH v2 2/2] hw/9pfs/xen: drain in-flight PDUs before xen-9p disconnect
Quoted 5 lines · Christian Schoenebeck
Hi Stefano,
chance that you might be looking at this v2 Xen patch soon? If not, just let me know, then I'll send out a PR with other fixes piled up on my side in the meantime, as QEMU release window narrows.
This is better but I think there is still an issue
Quoted 85 lines · On Saturday, 18 July 2026 18:50:06 CEST Christian Schoenebeck wrote:
Quoted 84 lines
The xen-9p disconnect path has two issues:
1. It frees the Xen9pfsRing structures while in-flight PDUs may still reference them via
pdu->tagto index rings[]. This causes a UAF inxen_9pfs_push_and_notify()when worker threads resume after completing filesystem operations.2. It never calls
v9fs_device_unrealize_common(), which means server state (struct LocalData, mountfd, FIDs) is never cleaned up on disconnect, causing a resource leak on every guest-initiated disconnect.Fix both by draining in-flight PDUs via
v9fs_reset()before tearing down rings, and callingv9fs_device_unrealize_common()to clean up server state.Additionally, explicit calls of
xen_9pfs_disconnect()in the error paths ofxen_9pfs_pdu_vmarshal()andxen_9pfs_pdu_vunmarshal()must be deferred (viaaio_bh_schedule_oneshot()), because xen_9pfs_pdu_v(un)marshal() are running within a coroutine context which makes them unsafe [1] for callingv9fs_reset()directly, as the latter e.g. has a loop like:while (!QLIST_EMPTY(&s->active_list)) { aio_poll(qemu_get_aio_context(), true); }which would a) never terminate (as the coroutine is on the
active_list) and b)aio_poll()is marked asno_coroutine_fn.[1] https://lore.kernel.org/qemu-devel/3351181.5fSG56mABF@weasel/
And finally, add an idempotent guard to
xen_9pfs_disconnect(), just for the case.Fixes b37eeb0201 ("xen/9pfs: introduce Xen 9pfs backend")Signed-off-by Christian Schoenebeck <[email protected]>--- hw/9pfs/xen-9p-backend.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-)hw/9pfs/xen-9p-backend.c +10
-2@@ -68,6 +68,11 @@ typedef struct Xen9pfsDev {static void xen_9pfs_disconnect (struct XenLegacyDevice *xendev); +static void xen_9pfs_disconnect_bh (void *opaque) +{ +xen_9pfs_disconnect (opaque); +} + static void xen_9pfs_in_sg(Xen9pfsRing *ring, struct iovec *in_sg, int *num,@@ -150,7 +155,8 @@ static ssize_t xen_9pfs_pdu_vmarshal(V9fsPDU *pdu,"Failed to encode VirtFS reply type %d\n" , pdu->id + 1);xen_be_set_state (&xen_9pfs->xendev, XenbusStateClosing); -xen_9pfs_disconnect (&xen_9pfs->xendev); +aio_bh_schedule_oneshot (qemu_get_aio_context (), + xen_9pfs_disconnect_bh, &xen_9pfs->xendev); }return ret; }@@ -173,7 +179,8 @@ static ssize_t xen_9pfs_pdu_vunmarshal(V9fsPDU *pdu,xen_pv_printf (&xen_9pfs->xendev,0 ,"Failed to decode VirtFS request type %d\n" , pdu->id);xen_be_set_state (&xen_9pfs->xendev, XenbusStateClosing); -xen_9pfs_disconnect (&xen_9pfs->xendev); +aio_bh_schedule_oneshot (qemu_get_aio_context (), + xen_9pfs_disconnect_bh, &xen_9pfs->xendev); }return ret; }@@ -368,10 +375,18 @@ static void xen_9pfs_evtchn_event(void *opaque)static void xen_9pfs_disconnect (struct XenLegacyDevice *xendev) {Xen9pfsDev *xen_9pdev =container_of (xendev, Xen9pfsDev, xendev); +V9fsState *s = &xen_9pdev->state;int i;
Quoted 1 line
hw/9pfs/xen-9p-backend.c static void xen_9pfs_evtchn_event(void *opaque) · line 381
Quoted 1 line
hw/9pfs/xen-9p-backend.c +1
-0+if (!xen_9pdev->rings) {
specifically this check is not sufficient are there can be cases where rings != NULL but transport is not set and v9fs_reset should not be called. For instance xen_be_map_grant_ref() returns NULL, xen_9pfs_connect() go out without calling v9fs_device_realize_common(). s->transport == NULL and also s->fids == NULL but rings != NULL. Later v9fs_reset calls g_hash_table_get_values(s->fids)
Quoted 5 lines
Quoted 5 lines
hw/9pfs/xen-9p-backend.c +3
-0+return ; + } +trace_xen_9pfs_disconnect (xendev->name);
Quoted 7 lines
Quoted 7 lines
hw/9pfs/xen-9p-backend.c +3
-0+v9fs_reset (s); +v9fs_device_unrealize_common (s); +for (i =0 ; i < xen_9pdev->num_rings; i++) {if (xen_9pdev->rings[i].evtchndev !=NULL ) {qemu_set_fd_handler(qemu_xen_evtchn_fd(xen_9pdev->rings[i].evtchndev),
See this below. If you agree and add the below to this patch you can add my Reviewed-by.
hw/9pfs/9p.c +1 -0
@@ -4530,6 +4530,7 @@ void v9fs_device_unrealize_common(V9fsState *s)qp_table_destroy (&s->qpp_table);qp_table_destroy (&s->qpf_table);g_free (s->ctx.fs_root); + s->transport =NULL ; } typedef struct VirtfsCoResetData {
hw/9pfs/xen-9p-backend.c +4 -2
@@ -384,8 +384,10 @@ static void xen_9pfs_disconnect(struct XenLegacyDevice *xendev)trace_xen_9pfs_disconnect (xendev->name); -v9fs_reset (s); -v9fs_device_unrealize_common (s); +if (s->transport) { +v9fs_reset (s); +v9fs_device_unrealize_common (s); + }for (i =0 ; i < xen_9pdev->num_rings; i++) {if (xen_9pdev->rings[i].evtchndev !=NULL ) {
Re: [PATCH v2 2/2] hw/9pfs/xen: drain in-flight PDUs before xen-9p disconnect
Quoted 1 line · On Thursday, 23 July 2026 03:17:12 CEST Stefano Stabellini wrote:
On Wed, 22 Jul 2026, Christian Schoenebeck wrote:
[...]
Quoted 69 lines
Quoted 5 lines
Quoted 5 lines
(quoted diff) +0
-0@@ -368,10 +375,18 @@ static void xen_9pfs_evtchn_event(void *opaque) static void xen_9pfs_disconnect(struct XenLegacyDevice *xendev) {Quoted 7 lines
Review static void xen_9pfs_evtchn_event(void *opaque) · line 385
Quoted 7 lines
(quoted diff) +2
-0Xen9pfsDev *xen_9pdev = container_of(xendev, Xen9pfsDev, xendev); + V9fsState *s = &xen_9pdev->state; int i; + if (!xen_9pdev->rings) {specifically this check is not sufficient are there can be cases where rings != NULL but transport is not set and
v9fs_resetshould not be called. For instancexen_be_map_grant_ref()returns NULL,xen_9pfs_connect()go out without callingv9fs_device_realize_common().s->transport== NULL and alsos->fids== NULL but rings != NULL. Laterv9fs_resetcallsg_hash_table_get_values(s->fids)Quoted 10 lines
Quoted 10 lines
hw/9pfs/xen-9p-backend.c +6
-0+return ; + } +trace_xen_9pfs_disconnect (xendev->name); +v9fs_reset (s); +v9fs_device_unrealize_common (s); +Quoted 5 lines
Quoted 5 lines
for (i = 0; i < xen_9pdev->num_rings; i++) { if (xen_9pdev->rings[i].evtchndev != NULL) {qemu_set_fd_handler(qemu_xen_evtchn_fd(xen_9pdev->rings[i].evtchndev),
See this below. If you agree and add the below to this patch you can add my Reviewed-by.
hw/9pfs/9p.c +1
-0@@ -4530,6 +4530,7 @@ void v9fs_device_unrealize_common(V9fsState *s)qp_table_destroy (&s->qpp_table);qp_table_destroy (&s->qpf_table);g_free (s->ctx.fs_root); + s->transport =NULL ; } typedef struct VirtfsCoResetData {hw/9pfs/xen-9p-backend.c +4
-2@@ -384,8 +384,10 @@ static void xen_9pfs_disconnect(struct XenLegacyDevice *xendev)trace_xen_9pfs_disconnect (xendev->name); -v9fs_reset (s); -v9fs_device_unrealize_common (s); +if (s->transport) { +v9fs_reset (s); +v9fs_device_unrealize_common (s); + }for (i =0 ; i < xen_9pdev->num_rings; i++) {
Quoted 1 line
if (xen_9pdev->rings[i].evtchndev != NULL) {Agreed, these two changes makes sense. I'll formally send a v3 with these two changes added now.
My plan is to send a PR with this and other fixes tomorrow already, as we're getting very close to release.
Thanks Stefano!
/Christian