Skip to contentSkip to conversation

9pfs: fix DoS via Treaddir (CVE-2026-9238)

PATCH Superseded Christian Schoenebeck 2 participants 17 messages May 27 – Jun 12

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover 9pfs: fix DoS via Treaddir (CVE-2026-9238)

Feifan Qian reported a potential DoS and unbound host allocation via Treaddir request due to msize not being capped on Tversion handshake, and allocation size in Treaddir being capped too late.

Attack Vector of Vulnerability:

A malicious guest can negotiate a large 9P msize during Tversion handshake and then send Treaddir request with a large 'count' parameter. The 9p server would allocate host memory proportional to the negotiated 'msize' for collecting directory entries, before validating that the actual reply buffer can hold the response. If the reply buffer is smaller than what was allocated, the response marshalling fails, but the memory has already been consumed.

This can lead to:

- Unbound host memory allocation proportional to the negotiated msize.
- Denial of service against the QEMU process.
- Potential OOM conditions affecting entire host system.

This series fixes this vulnerability on two layers.

Summary of patches fixing this vulnerability:

- Layer 1: Patches 1..4 cap msize during Tversion handshake to reasonable maximum value. It does so by limiting msize to the theoretical maximum msize value supported by transport implementation.

- Layer 2: Patches 5..8 cap the allocated host memory in the Treaddir handler specifically to the current, real response buffer size of transport implementation.

Christian Schoenebeck (8):
hw/9pfs: add msize_limit transport callback 9pfs/virtio: implement msize_limit callback 9pfs/xen: implement msize_limit callback hw/9pfs: cap negotiated msize to transport limit hw/9pfs: add response_buffer_size transport callback 9pfs/virtio: implement response_buffer_size callback 9pfs/xen: implement response_buffer_size callback hw/9pfs: cap Treaddir allocation (CVE-2026-9238)

 hw/9pfs/9p.c               | 28 ++++++++++++++++++++++++++--
 hw/9pfs/9p.h               |  2 ++
 hw/9pfs/virtio-9p-device.c | 17 +++++++++++++++++
 hw/9pfs/xen-9p-backend.c   | 18 ++++++++++++++++++
 4 files changed, 63 insertions(+), 2 deletions(-)
Signature · 1 line
2.47.3
#1 lore raw

PATCH 1/8 hw/9pfs: add msize_limit transport callback

Add a new callback 'msize_limit' to the V9fsTransport structure.

This allows each transport implementation to provide its theoretical maximum 'msize' value, which will be used to cap the negotiated msize during Tversion handshake.

---
 hw/9pfs/9p.h | 1 +
 1 file changed, 1 insertion(+)
hw/9pfs/9p.h +1 -0
index 65cc45e344..14111e041a 100644
@@ -481,6 +481,7 @@ struct V9fsTransport {
     void        (*init_out_iov_from_pdu)(V9fsPDU *pdu, struct iovec **piov,
                                          unsigned int *pniov, size_t size);
     void        (*push_and_notify)(V9fsPDU *pdu);
+    size_t      (*msize_limit)(V9fsState *s);
 };
 
 #endif
Signature · 1 line
2.47.3
#8 lore raw

PATCH 4/8 hw/9pfs: cap negotiated msize to transport limit

In reply to Christian Schoenebeck

The 'msize' parameter negotiated during Tversion handshake can be arbitrarily large as requested by the guest. So far 9p server accepted any msize value suggested by guest, i.e. server did not cap it at all, no matter how large, as in practice the upper limit of msize is a client capability. But as subsequent's security patch shows, capping msize on server side makes sense as additional safety-net.

Let's cap msize to transport's theoretical limit for msize, mainly to prevent a bad client from triggering excessive host memory allocations throughout the session.

We intentionally don't cap msize to transport's current, real response buffer size, as the response buffer size may vary between individual requests.

---
 hw/9pfs/9p.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
hw/9pfs/9p.c +10 -0
index e2713b9eee..2bb42dfc2e 100644
@@ -1456,6 +1456,16 @@ static void coroutine_fn v9fs_version(void *opaque)
         goto out;
     }
 
+    /* cap msize to transport's theoretical limit */
+    if (s->transport->msize_limit) {
+        size_t limit = s->transport->msize_limit(s);
+        if (s->msize > limit) {
+            s->msize = limit;
+            warn_report_once("9p: client msize capped to %zu (transport limit)",
+                             limit);
+        }
+    }
+
     /* 8192 is the default msize of Linux clients */
     if (s->msize <= 8192 && !(s->ctx.export_flags & V9FS_NO_PERF_WARN)) {
         warn_report_once(
Signature · 1 line
2.47.3
#2 lore raw

PATCH 2/8 9pfs/virtio: implement msize_limit callback

In reply to Christian Schoenebeck

Add and implement the msize_limit callback for the virtio transport.

This new callback function provides the theoretical maximum 'msize' value supported by this virtio transport.

The limit is calculated as (VIRTQUEUE_MAX_SIZE - 2) * 4096 bytes, where 2 virtio descriptors are lost exactly for:

- 1 descriptor for the original request (typically being small)

- 1 descriptor as indirect table pointer (when used), which just contains a pointer to the separate sglist containing the response's actual payload data

And 4096 bytes are assumed as standard page size used by Linux 9p client. This results in a maximum 'msize' of 4186112 bytes.

Theoretically Linux client could support a much larger size, e.g. by using multiple consecutive pages per sg entry / descriptor. However that's currently not the case and unlikely to change any time soon. And due to recent security issues, let's handle this limit conservatively until really necessary to be raised.

---
 hw/9pfs/virtio-9p-device.c | 7 +++++++
 1 file changed, 7 insertions(+)
hw/9pfs/virtio-9p-device.c +7 -0
index 9f70e2338c..8c5d86cb66 100644
@@ -192,12 +192,19 @@ static void virtio_init_out_iov_from_pdu(V9fsPDU *pdu, struct iovec **piov,
     *pniov = elem->out_num;
 }
 
+static size_t virtio_9p_msize_limit(V9fsState *s)
+{
+    const size_t guestPageSize = 4096;
+    return (VIRTQUEUE_MAX_SIZE - 2) * guestPageSize;
+}
+
 static const V9fsTransport virtio_9p_transport = {
     .pdu_vmarshal = virtio_pdu_vmarshal,
     .pdu_vunmarshal = virtio_pdu_vunmarshal,
     .init_in_iov_from_pdu = virtio_init_in_iov_from_pdu,
     .init_out_iov_from_pdu = virtio_init_out_iov_from_pdu,
     .push_and_notify = virtio_9p_push_and_notify,
+    .msize_limit = virtio_9p_msize_limit,
 };
 
 static void virtio_9p_device_realize(DeviceState *dev, Error **errp)
Signature · 1 line
2.47.3
#11 lore raw

PATCH 7/8 9pfs/xen: implement response_buffer_size callback

In reply to Christian Schoenebeck

Add and implement the response_buffer_size callback for the Xen transport.

Returns the size of the response buffer from the rings in_sg, as limit for 9p server while generating a response for supplied PDU.

---
 hw/9pfs/xen-9p-backend.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
hw/9pfs/xen-9p-backend.c +11 -0
index 94654022fe..258a1f2ec7 100644
@@ -256,6 +256,16 @@ static size_t xen_9p_msize_limit(V9fsState *s)
     return XEN_FLEX_RING_SIZE(xen_9pfs->rings[0].ring_order);
 }
 
+static size_t xen_9pfs_response_buffer_size(V9fsPDU *pdu)
+{
+    Xen9pfsDev *priv = container_of(pdu->s, Xen9pfsDev, state);
+    Xen9pfsRing *ring = &priv->rings[pdu->tag % priv->num_rings];
+    int num;
+
+    xen_9pfs_in_sg(ring, ring->sg, &num, pdu->idx, 0);
+    return iov_size(ring->sg, num);
+}
+
 static const V9fsTransport xen_9p_transport = {
     .pdu_vmarshal = xen_9pfs_pdu_vmarshal,
     .pdu_vunmarshal = xen_9pfs_pdu_vunmarshal,
@@ -263,6 +273,7 @@ static const V9fsTransport xen_9p_transport = {
     .init_out_iov_from_pdu = xen_9pfs_init_out_iov_from_pdu,
     .push_and_notify = xen_9pfs_push_and_notify,
     .msize_limit = xen_9p_msize_limit,
+    .response_buffer_size = xen_9pfs_response_buffer_size,
 };
 
 static int xen_9pfs_init(struct XenLegacyDevice *xendev)
Signature · 1 line
2.47.3
#14 lore raw

PATCH 8/8 hw/9pfs: cap Treaddir allocation (CVE-2026-9238)

In reply to Christian Schoenebeck

Constrain max_count in v9fs_readdir() to transport's current, real response buffer size before calling v9fs_do_readdir() to prevent excessive host memory allocation by bad clients.

Client may send a Treaddir request with a large 'count' parameter, and while the negotiated 'msize' provides some limit, it accounts for guest being somewhat faithful on the negotiated 'msize' value throughout the session.

A bad guest client could have negotiated a large 'msize' but provide a small reply buffer for Treaddir request, causing QEMU to allocate host memory proportional to 'msize' before discovering the reply cannot fit.

Possible consequence was a potential DoS by a priviliged guest, causing a disconnection of guest communication due to transport device being marked as "broken", however QEMU process would have continued to run with potentially giant host memory allocation, which might have negative impact on other services running on host.

Fixes CVE-2026-9238
Fixes 2149675b195f ("9pfs: add new function v9fs_co_readdir_many()")
---
 hw/9pfs/9p.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
hw/9pfs/9p.c +16 -2
index 2bb42dfc2e..fa8c7243a7 100644
@@ -2652,6 +2652,7 @@ static void coroutine_fn v9fs_readdir(void *opaque)
     uint32_t max_count;
     V9fsPDU *pdu = opaque;
     V9fsState *s = pdu->s;
+    size_t max_resp_sz;
 
     retval = pdu_unmarshal(pdu, offset, "dqd", &fid,
                            &initial_offset, &max_count);
@@ -2660,9 +2661,22 @@ static void coroutine_fn v9fs_readdir(void *opaque)
     }
     trace_v9fs_readdir(pdu->tag, pdu->id, fid, initial_offset, max_count);
 
+    max_resp_sz = s->msize;
+
+    /*
+     * Constrain max_count to transport's current, actual response buffer size.
+     * A bad client might provide a response buffer < msize.
+     */
+    if (s->transport->response_buffer_size) {
+        size_t buf_size = s->transport->response_buffer_size(pdu);
+        if (max_resp_sz > buf_size) {
+            max_resp_sz = buf_size;
+        }
+    }
+
     /* Enough space for a R_readdir header: size[4] Rreaddir tag[2] count[4] */
-    if (max_count > s->msize - 11) {
-        max_count = s->msize - 11;
+    if (max_count > max_resp_sz - 11) {
+        max_count = max_resp_sz - 11;
         warn_report_once(
             "9p: bad client: T_readdir with count > msize - 11"
         );
Signature · 1 line
2.47.3
#9 lore raw

PATCH 5/8 hw/9pfs: add response_buffer_size transport callback

In reply to Christian Schoenebeck

Add a new callback to the V9fsTransport interface that allows each transport to provide the real size of its current response buffer.

This is needed for subsequent safety guards that will limit generated responses appropriately before trying to allocate, generate, and send a response to guest.

This is especially required for request handlers that need to allocate dynamic and potentially large host memory for generating a response. These safety guards are mandatory to counter bad clients that try to trick server by supplying response buffers being smaller than the previously negotiated msize value.

---
 hw/9pfs/9p.h | 1 +
 1 file changed, 1 insertion(+)
hw/9pfs/9p.h +1 -0
index 14111e041a..1efe000f6f 100644
@@ -482,6 +482,7 @@ struct V9fsTransport {
                                          unsigned int *pniov, size_t size);
     void        (*push_and_notify)(V9fsPDU *pdu);
     size_t      (*msize_limit)(V9fsState *s);
+    size_t      (*response_buffer_size)(V9fsPDU *pdu);
 };
 
 #endif
Signature · 1 line
2.47.3
#3 lore raw

PATCH 3/8 9pfs/xen: implement msize_limit callback

In reply to Christian Schoenebeck

Add and implement the msize_limit callback for the Xen transport.

The limit is calculated using XEN_FLEX_RING_SIZE() based on the negotiated ring_order. For the theoretical maximum ring_order of 9, this results in a maximum 'msize' of 1048576 bytes (1 MiB).

---
 hw/9pfs/xen-9p-backend.c | 7 +++++++
 1 file changed, 7 insertions(+)
hw/9pfs/xen-9p-backend.c +7 -0
index ca0fff5fa9..94654022fe 100644
@@ -250,12 +250,19 @@ static void xen_9pfs_push_and_notify(V9fsPDU *pdu)
     qemu_bh_schedule(ring->bh);
 }
 
+static size_t xen_9p_msize_limit(V9fsState *s)
+{
+    Xen9pfsDev *xen_9pfs = container_of(s, Xen9pfsDev, state);
+    return XEN_FLEX_RING_SIZE(xen_9pfs->rings[0].ring_order);
+}
+
 static const V9fsTransport xen_9p_transport = {
     .pdu_vmarshal = xen_9pfs_pdu_vmarshal,
     .pdu_vunmarshal = xen_9pfs_pdu_vunmarshal,
     .init_in_iov_from_pdu = xen_9pfs_init_in_iov_from_pdu,
     .init_out_iov_from_pdu = xen_9pfs_init_out_iov_from_pdu,
     .push_and_notify = xen_9pfs_push_and_notify,
+    .msize_limit = xen_9p_msize_limit,
 };
 
 static int xen_9pfs_init(struct XenLegacyDevice *xendev)
Signature · 1 line
2.47.3
#10 lore raw

PATCH 6/8 9pfs/virtio: implement response_buffer_size callback

In reply to Christian Schoenebeck

Add and implement the response_buffer_size callback for the virtio transport.

Returns the actual current virtio response buffer size for the supplied PDU, which will be used as safety guard for limiting the response size when generating a 9p response.

---
 hw/9pfs/virtio-9p-device.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
hw/9pfs/virtio-9p-device.c +10 -0
index 8c5d86cb66..50dc93091d 100644
@@ -198,6 +198,15 @@ static size_t virtio_9p_msize_limit(V9fsState *s)
     return (VIRTQUEUE_MAX_SIZE - 2) * guestPageSize;
 }
 
+static size_t virtio_9p_response_buffer_size(V9fsPDU *pdu)
+{
+    V9fsState *s = pdu->s;
+    V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
+    VirtQueueElement *elem = v->elems[pdu->idx];
+
+    return iov_size(elem->in_sg, elem->in_num);
+}
+
 static const V9fsTransport virtio_9p_transport = {
     .pdu_vmarshal = virtio_pdu_vmarshal,
     .pdu_vunmarshal = virtio_pdu_vunmarshal,
@@ -205,6 +214,7 @@ static const V9fsTransport virtio_9p_transport = {
     .init_out_iov_from_pdu = virtio_init_out_iov_from_pdu,
     .push_and_notify = virtio_9p_push_and_notify,
     .msize_limit = virtio_9p_msize_limit,
+    .response_buffer_size = virtio_9p_response_buffer_size,
 };
 
 static void virtio_9p_device_realize(DeviceState *dev, Error **errp)
Signature · 1 line
2.47.3
#4 lore raw

Re: [PATCH 3/8] 9pfs/xen: implement msize_limit callback

In reply to Christian Schoenebeck

Hi Stefano,

would you have a chance to look at this Xen patch 3 and patch 7 whether I am doing it right for Xen?

/Christian

Quoted 35 lines · On Wednesday, 27 May 2026 23:15:25 CEST Christian Schoenebeck wrote:

Add and implement the msize_limit callback for the Xen transport.

The limit is calculated using XEN_FLEX_RING_SIZE() based on the negotiated ring_order. For the theoretical maximum ring_order of 9, this results in a maximum 'msize' of 1048576 bytes (1 MiB).

---
 hw/9pfs/xen-9p-backend.c | 7 +++++++
 1 file changed, 7 insertions(+)
hw/9pfs/xen-9p-backend.c +7 -0
index ca0fff5fa9..94654022fe 100644
@@ -250,12 +250,19 @@ static void xen_9pfs_push_and_notify(V9fsPDU *pdu)
     qemu_bh_schedule(ring->bh);
 }
 
+static size_t xen_9p_msize_limit(V9fsState *s)
+{
+    Xen9pfsDev *xen_9pfs = container_of(s, Xen9pfsDev, state);
+    return XEN_FLEX_RING_SIZE(xen_9pfs->rings[0].ring_order);
+}
+
 static const V9fsTransport xen_9p_transport = {
     .pdu_vmarshal = xen_9pfs_pdu_vmarshal,
     .pdu_vunmarshal = xen_9pfs_pdu_vunmarshal,
     .init_in_iov_from_pdu = xen_9pfs_init_in_iov_from_pdu,
     .init_out_iov_from_pdu = xen_9pfs_init_out_iov_from_pdu,
     .push_and_notify = xen_9pfs_push_and_notify,
+    .msize_limit = xen_9p_msize_limit,
 };
 
 static int xen_9pfs_init(struct XenLegacyDevice *xendev)
#5 lore raw

Re: [PATCH 3/8] 9pfs/xen: implement msize_limit callback

Quoted 4 lines · Christian Schoenebeck

Hi Stefano,

would you have a chance to look at this Xen patch 3 and patch 7 whether I am doing it right for Xen?

Yes sorry I am a bit behind, I'll get to it

Quoted 42 lines

/Christian

Quoted 35 lines · On Wednesday, 27 May 2026 23:15:25 CEST Christian Schoenebeck wrote:

Add and implement the msize_limit callback for the Xen transport.

The limit is calculated using XEN_FLEX_RING_SIZE() based on the negotiated ring_order. For the theoretical maximum ring_order of 9, this results in a maximum 'msize' of 1048576 bytes (1 MiB).

---
 hw/9pfs/xen-9p-backend.c | 7 +++++++
 1 file changed, 7 insertions(+)
hw/9pfs/xen-9p-backend.c +7 -0
index ca0fff5fa9..94654022fe 100644
@@ -250,12 +250,19 @@ static void xen_9pfs_push_and_notify(V9fsPDU *pdu)
     qemu_bh_schedule(ring->bh);
 }
 
+static size_t xen_9p_msize_limit(V9fsState *s)
+{
+    Xen9pfsDev *xen_9pfs = container_of(s, Xen9pfsDev, state);
+    return XEN_FLEX_RING_SIZE(xen_9pfs->rings[0].ring_order);
+}
+
 static const V9fsTransport xen_9p_transport = {
     .pdu_vmarshal = xen_9pfs_pdu_vmarshal,
     .pdu_vunmarshal = xen_9pfs_pdu_vunmarshal,
     .init_in_iov_from_pdu = xen_9pfs_init_in_iov_from_pdu,
     .init_out_iov_from_pdu = xen_9pfs_init_out_iov_from_pdu,
     .push_and_notify = xen_9pfs_push_and_notify,
+    .msize_limit = xen_9p_msize_limit,
 };
 
 static int xen_9pfs_init(struct XenLegacyDevice *xendev)
#6 lore raw

Re: [PATCH 3/8] 9pfs/xen: implement msize_limit callback

In reply to Christian Schoenebeck
Quoted 16 lines · Christian Schoenebeck

Add and implement the msize_limit callback for the Xen transport.

The limit is calculated using XEN_FLEX_RING_SIZE() based on the negotiated ring_order. For the theoretical maximum ring_order of 9, this results in a maximum 'msize' of 1048576 bytes (1 MiB).

---
 hw/9pfs/xen-9p-backend.c | 7 +++++++
 1 file changed, 7 insertions(+)
hw/9pfs/xen-9p-backend.c +0 -0
index ca0fff5fa9..94654022fe 100644

hw/9pfs/xen-9p-backend.c static void xen_9pfs_push_and_notify(V9fsPDU *pdu) · line 257

Quoted 8 lines
hw/9pfs/xen-9p-backend.c +5 -0
@@ -250,12 +250,19 @@ static void xen_9pfs_push_and_notify(V9fsPDU *pdu)
     qemu_bh_schedule(ring->bh);
 }
 
+static size_t xen_9p_msize_limit(V9fsState *s)
+{
+    Xen9pfsDev *xen_9pfs = container_of(s, Xen9pfsDev, state);
+    return XEN_FLEX_RING_SIZE(xen_9pfs->rings[0].ring_order);
+}

This is correct as a computation. But there are multiple rings with potentially different ring_orders (although I have never seen it in practice.)

So I think something like this would be better:

---
size_t limit = XEN_FLEX_RING_SIZE(xen_9pfs->rings[0].ring_order);

for (i = 1; i < xen_9pfs->num_rings; i++) {
    limit = MIN(limit, XEN_FLEX_RING_SIZE(xen_9pfs->rings[i].ring_order));
}

return limit;

Quoted 10 lines
hw/9pfs/xen-9p-backend.c +1 -0
 static const V9fsTransport xen_9p_transport = {
     .pdu_vmarshal = xen_9pfs_pdu_vmarshal,
     .pdu_vunmarshal = xen_9pfs_pdu_vunmarshal,
     .init_in_iov_from_pdu = xen_9pfs_init_in_iov_from_pdu,
     .init_out_iov_from_pdu = xen_9pfs_init_out_iov_from_pdu,
     .push_and_notify = xen_9pfs_push_and_notify,
+    .msize_limit = xen_9p_msize_limit,
 };
 
 static int xen_9pfs_init(struct XenLegacyDevice *xendev)
#15 lore raw

Re: [PATCH 8/8] hw/9pfs: cap Treaddir allocation (CVE-2026-9238)

In reply to Christian Schoenebeck
Quoted 41 lines · Christian Schoenebeck

Constrain max_count in v9fs_readdir() to transport's current, real response buffer size before calling v9fs_do_readdir() to prevent excessive host memory allocation by bad clients.

Client may send a Treaddir request with a large 'count' parameter, and while the negotiated 'msize' provides some limit, it accounts for guest being somewhat faithful on the negotiated 'msize' value throughout the session.

A bad guest client could have negotiated a large 'msize' but provide a small reply buffer for Treaddir request, causing QEMU to allocate host memory proportional to 'msize' before discovering the reply cannot fit.

Possible consequence was a potential DoS by a priviliged guest, causing a disconnection of guest communication due to transport device being marked as "broken", however QEMU process would have continued to run with potentially giant host memory allocation, which might have negative impact on other services running on host.

Fixes CVE-2026-9238
Fixes 2149675b195f ("9pfs: add new function v9fs_co_readdir_many()")
---
 hw/9pfs/9p.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
hw/9pfs/9p.c +1 -0
index 2bb42dfc2e..fa8c7243a7 100644
@@ -2652,6 +2652,7 @@ static void coroutine_fn v9fs_readdir(void *opaque)
     uint32_t max_count;
     V9fsPDU *pdu = opaque;
     V9fsState *s = pdu->s;
+    size_t max_resp_sz;
 
     retval = pdu_unmarshal(pdu, offset, "dqd", &fid,
                            &initial_offset, &max_count);
@@ -2660,9 +2661,22 @@ static void coroutine_fn v9fs_readdir(void *opaque)
     }

hw/9pfs/9p.c static void coroutine_fn v9fs_readdir(void *opaque) · line 2671

Quoted 10 lines
hw/9pfs/9p.c +8 -0
     trace_v9fs_readdir(pdu->tag, pdu->id, fid, initial_offset, max_count);
 
+    max_resp_sz = s->msize;
+
+    /*
+     * Constrain max_count to transport's current, actual response buffer size.
+     * A bad client might provide a response buffer < msize.
+     */
+    if (s->transport->response_buffer_size) {
+        size_t buf_size = s->transport->response_buffer_size(pdu);

response_buffer_size() can return less than 11 here...

hw/9pfs/9p.c static void coroutine_fn v9fs_readdir(void *opaque) · line 2673

Quoted 2 lines
hw/9pfs/9p.c +2 -0
+        if (max_resp_sz > buf_size) {
+            max_resp_sz = buf_size;

max_resp_sz becomes less than 11

hw/9pfs/9p.c static void coroutine_fn v9fs_readdir(void *opaque) · line 2678

Quoted 7 lines
hw/9pfs/9p.c +4 -2
+        }
+    }
+
     /* Enough space for a R_readdir header: size[4] Rreaddir tag[2] count[4] */
-    if (max_count > s->msize - 11) {
-        max_count = s->msize - 11;
+    if (max_count > max_resp_sz - 11) {

max_resp_sz - 11 causes an underflow

Quoted 7 lines
hw/9pfs/9p.c +1 -0
+        max_count = max_resp_sz - 11;
         warn_report_once(
             "9p: bad client: T_readdir with count > msize - 11"
         );
Signature · 1 line
2.47.3
#12 lore raw

Re: [PATCH 7/8] 9pfs/xen: implement response_buffer_size callback

In reply to Christian Schoenebeck
Quoted 18 lines · Christian Schoenebeck

Add and implement the response_buffer_size callback for the Xen transport.

Returns the size of the response buffer from the rings in_sg, as limit for 9p server while generating a response for supplied PDU.

---
 hw/9pfs/xen-9p-backend.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
hw/9pfs/xen-9p-backend.c +0 -0
index 94654022fe..258a1f2ec7 100644
@@ -256,6 +256,16 @@ static size_t xen_9p_msize_limit(V9fsState *s)
     return XEN_FLEX_RING_SIZE(xen_9pfs->rings[0].ring_order);
 }

hw/9pfs/xen-9p-backend.c static size_t xen_9p_msize_limit(V9fsState *s) · line 267

Quoted 10 lines
hw/9pfs/xen-9p-backend.c +9 -0
 
+static size_t xen_9pfs_response_buffer_size(V9fsPDU *pdu)
+{
+    Xen9pfsDev *priv = container_of(pdu->s, Xen9pfsDev, state);
+    Xen9pfsRing *ring = &priv->rings[pdu->tag % priv->num_rings];
+    int num;
+
+    xen_9pfs_in_sg(ring, ring->sg, &num, pdu->idx, 0);
+    return iov_size(ring->sg, num);
+}

xen_9pfs_init_out_iov_from_pdu and xen_9pfs_init_in_iov_from_pdu allocate ring->sg, but it would seem xen_9pfs_response_buffer_size could be called before xen_9pfs_init_out_iov_from_pdu and xen_9pfs_init_in_iov_from_pdu, therefore ring->sg could be NULL here.

If we do it this way, we avoid the NULL dereference.

   struct iovec in_sg[2];

   xen_9pfs_in_sg(ring, in_sg, &num, pdu->idx, 0);
   return iov_size(in_sg, num);
Quoted 14 lines
hw/9pfs/xen-9p-backend.c +1 -0
 static const V9fsTransport xen_9p_transport = {
     .pdu_vmarshal = xen_9pfs_pdu_vmarshal,
     .pdu_vunmarshal = xen_9pfs_pdu_vunmarshal,
@@ -263,6 +273,7 @@ static const V9fsTransport xen_9p_transport = {
     .init_out_iov_from_pdu = xen_9pfs_init_out_iov_from_pdu,
     .push_and_notify = xen_9pfs_push_and_notify,
     .msize_limit = xen_9p_msize_limit,
+    .response_buffer_size = xen_9pfs_response_buffer_size,
 };
 
 static int xen_9pfs_init(struct XenLegacyDevice *xendev)
Signature · 1 line
2.47.3
#7 lore raw

Re: [PATCH 3/8] 9pfs/xen: implement msize_limit callback

In reply to Stefano Stabellini
Quoted 9 lines · On Friday, 12 June 2026 01:49:25 CEST Stefano Stabellini wrote:
Quoted 8 lines · Christian Schoenebeck

Add and implement the msize_limit callback for the Xen transport.

The limit is calculated using XEN_FLEX_RING_SIZE() based on the negotiated ring_order. For the theoretical maximum ring_order of 9, this results in a maximum 'msize' of 1048576 bytes (1 MiB).

---

[...]

Quoted 10 lines

hw/9pfs/xen-9p-backend.c static void xen_9pfs_push_and_notify(V9fsPDU *pdu) · line 257

Quoted 5 lines
hw/9pfs/xen-9p-backend.c +5 -0
+static size_t xen_9p_msize_limit(V9fsState *s)
+{
+    Xen9pfsDev *xen_9pfs = container_of(s, Xen9pfsDev, state);
+    return XEN_FLEX_RING_SIZE(xen_9pfs->rings[0].ring_order);
+}

This is correct as a computation. But there are multiple rings with potentially different ring_orders (although I have never seen it in practice.)

Quoted 8 lines

So I think something like this would be better:

---
size_t limit = XEN_FLEX_RING_SIZE(xen_9pfs->rings[0].ring_order);

for (i = 1; i < xen_9pfs->num_rings; i++) {
    limit = MIN(limit, XEN_FLEX_RING_SIZE(xen_9pfs->rings[i].ring_order));
}

return limit;

Agreed, I'll take your solution and just precede it with:

  if (!xen_9pfs->num_rings) {
      return 0;
  }

I understand it is not really needed as min. one Xen ring is guaranteed, but it's still a separate subsystem and it doesn't cost much.

/Christian

#13 lore raw

Re: [PATCH 7/8] 9pfs/xen: implement response_buffer_size callback

In reply to Stefano Stabellini
Quoted 11 lines · On Friday, 12 June 2026 02:23:15 CEST Stefano Stabellini wrote:
Quoted 10 lines · Christian Schoenebeck

Add and implement the response_buffer_size callback for the Xen transport.

Returns the size of the response buffer from the rings in_sg, as limit for 9p server while generating a response for supplied PDU.

---

 hw/9pfs/xen-9p-backend.c | 11 +++++++++++

[...]

Quoted 15 lines

hw/9pfs/xen-9p-backend.c static size_t xen_9p_msize_limit(V9fsState *s) · line 267

Quoted 9 lines
hw/9pfs/xen-9p-backend.c +9 -0
+static size_t xen_9pfs_response_buffer_size(V9fsPDU *pdu)
+{
+    Xen9pfsDev *priv = container_of(pdu->s, Xen9pfsDev, state);
+    Xen9pfsRing *ring = &priv->rings[pdu->tag % priv->num_rings];
+    int num;
+
+    xen_9pfs_in_sg(ring, ring->sg, &num, pdu->idx, 0);
+    return iov_size(ring->sg, num);
+}

xen_9pfs_init_out_iov_from_pdu and xen_9pfs_init_in_iov_from_pdu allocate ring->sg, but it would seem xen_9pfs_response_buffer_size could be called before xen_9pfs_init_out_iov_from_pdu and xen_9pfs_init_in_iov_from_pdu, therefore ring->sg could be NULL here.

Quoted 6 lines

If we do it this way, we avoid the NULL dereference.

   struct iovec in_sg[2];

   xen_9pfs_in_sg(ring, in_sg, &num, pdu->idx, 0);
   return iov_size(in_sg, num);

Good catch!

/Christian

#16 lore raw

Re: [PATCH 8/8] hw/9pfs: cap Treaddir allocation (CVE-2026-9238)

In reply to Stefano Stabellini
Quoted 30 lines · On Friday, 12 June 2026 02:23:08 CEST Stefano Stabellini wrote:
Quoted 29 lines · Christian Schoenebeck

Constrain max_count in v9fs_readdir() to transport's current, real response buffer size before calling v9fs_do_readdir() to prevent excessive host memory allocation by bad clients.

Client may send a Treaddir request with a large 'count' parameter, and while the negotiated 'msize' provides some limit, it accounts for guest being somewhat faithful on the negotiated 'msize' value throughout the session.

A bad guest client could have negotiated a large 'msize' but provide a small reply buffer for Treaddir request, causing QEMU to allocate host memory proportional to 'msize' before discovering the reply cannot fit.

Possible consequence was a potential DoS by a priviliged guest, causing a disconnection of guest communication due to transport device being marked as "broken", however QEMU process would have continued to run with potentially giant host memory allocation, which might have negative impact on other services running on host.

Fixes CVE-2026-9238
Fixes 2149675b195f ("9pfs: add new function v9fs_co_readdir_many()")

---

 hw/9pfs/9p.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
hw/9pfs/9p.c +0 -0

[...]

Quoted 8 lines

hw/9pfs/9p.c static void coroutine_fn v9fs_readdir(void *opaque) · line 2678

Quoted 6 lines
     /* Enough space for a R_readdir header: size[4] Rreaddir tag[2]
     count[4] */
hw/9pfs/9p.c +1 -2
-    if (max_count > s->msize - 11) {
-        max_count = s->msize - 11;
+    if (max_count > max_resp_sz - 11) {

max_resp_sz - 11 causes an underflow

Good catch!

I'll prepare a v2 with all your suggestions.

Thanks for your reviews, much appreciated!

/Christian