Skip to contentSkip to conversation

net/9p: Fix buffer overflow in USB transport layer

PATCH In review Yuhao Jiang 2 participants 5 messages Jun 16, 2025 – Jun 17, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net/9p: Fix buffer overflow in USB transport layer

A buffer overflow vulnerability exists in the USB 9pfs transport layer where inconsistent size validation between packet header parsing and actual data copying allows a malicious USB host to overflow heap buffers.

The issue occurs because:
- usb9pfs_rx_header() validates only the declared size in packet header
- usb9pfs_rx_complete() uses req->actual (actual received bytes) for memcpy

This allows an attacker to craft packets with small declared size (bypassing validation) but large actual payload (triggering overflow in memcpy).

Add validation in usb9pfs_rx_complete() to ensure req->actual does not exceed the buffer capacity before copying data.

Fixes a3be076dc174 ("net/9p/usbg: Add new usb gadget function transport")
---
 net/9p/trans_usbg.c | 9 +++++++++
 1 file changed, 9 insertions(+)
net/9p/trans_usbg.c +9 -0
index 6b694f117aef..047a2862fc84 100644
@@ -242,6 +242,15 @@ static void usb9pfs_rx_complete(struct usb_ep *ep, struct usb_request *req)
 	if (!p9_rx_req)
 		return;
 
+	/* Validate actual received size against buffer capacity */
+	if (req->actual > p9_rx_req->rc.capacity) {
+		dev_err(&cdev->gadget->dev,
+			"received data size %u exceeds buffer capacity %zu\n",
+			req->actual, p9_rx_req->rc.capacity);
+		p9_req_put(usb9pfs->client, p9_rx_req);
+		return;
+	}
+
 	memcpy(p9_rx_req->rc.sdata, req->buf, req->actual);
 
 	p9_rx_req->rc.size = req->actual;
Signature · 1 line
2.43.0
#1 lore raw

Yuhao Jiang wrote on Mon, Jun 16, 2025 at 09:25:39PM +0800:

Quoted 8 lines

A buffer overflow vulnerability exists in the USB 9pfs transport layer where inconsistent size validation between packet header parsing and actual data copying allows a malicious USB host to overflow heap buffers.

The issue occurs because:
- usb9pfs_rx_header() validates only the declared size in packet header
- usb9pfs_rx_complete() uses req->actual (actual received bytes) for memcpy

Quoted 5 lines

This allows an attacker to craft packets with small declared size (bypassing validation) but large actual payload (triggering overflow in memcpy).

Add validation in usb9pfs_rx_complete() to ensure req->actual does not exceed the buffer capacity before copying data.

Thanks for this check!

Did you reproduce this or was this static analysis found? (to knowi if you tested wrt question below)

Quoted 13 lines
Fixes a3be076dc174 ("net/9p/usbg: Add new usb gadget function transport")
---
 net/9p/trans_usbg.c | 9 +++++++++
 1 file changed, 9 insertions(+)
net/9p/trans_usbg.c +0 -0
index 6b694f117aef..047a2862fc84 100644

net/9p/trans_usbg.c static void usb9pfs_rx_complete(struct usb_ep *ep, struct usb_request *req) · line 250

Quoted 9 lines
net/9p/trans_usbg.c +6 -0
@@ -242,6 +242,15 @@ static void usb9pfs_rx_complete(struct usb_ep *ep, struct usb_request *req)
 	if (!p9_rx_req)
 		return;
 
+	/* Validate actual received size against buffer capacity */
+	if (req->actual > p9_rx_req->rc.capacity) {
+		dev_err(&cdev->gadget->dev,
+			"received data size %u exceeds buffer capacity %zu\n",
+			req->actual, p9_rx_req->rc.capacity);
+		p9_req_put(usb9pfs->client, p9_rx_req);

I still haven't gotten around to setting up something to test this, and even less the error case, but I'm not sure a single put is enough -- p9_client_cb does another put.
Conceptually I think it's better to mark the error and move on e.g. (not even compile tested)

```
	int status = REQ_STATUS_RCVD;

	[...]

	if (req->actual > p9_rx_req->rc.capacity) {
		dev_err(...)
		req->actual = 0;
		status = REQ_STATUS_ERROR;
	}
	
	memcpy(..)

        p9_rx_req->rc.size = req->actual;

        p9_client_cb(usb9pfs->client, p9_rx_req, status);
        p9_req_put(usb9pfs->client, p9_rx_req);

	complete(&usb9pfs->received);
```

(I'm not sure overriding req->actual is allowed, might be safer to use an intermediate variable like status instead)

What do you think?

Thanks,

Signature · 1 line
Dominique Martinet | Asmadeus
#2 lore raw
Quoted 13 lines · On Tue, Jun 17, 2025 at 7:00 AM <[email protected]> wrote:

Yuhao Jiang wrote on Mon, Jun 16, 2025 at 09:25:39PM +0800:

Quoted 11 lines

A buffer overflow vulnerability exists in the USB 9pfs transport layer where inconsistent size validation between packet header parsing and actual data copying allows a malicious USB host to overflow heap buffers.

The issue occurs because:
- usb9pfs_rx_header() validates only the declared size in packet header
- usb9pfs_rx_complete() uses req->actual (actual received bytes) for memcpy

This allows an attacker to craft packets with small declared size (bypassing validation) but large actual payload (triggering overflow in memcpy).

Quoted 7 lines
Quoted 2 lines

Add validation in usb9pfs_rx_complete() to ensure req->actual does not exceed the buffer capacity before copying data.

Thanks for this check!

Did you reproduce this or was this static analysis found? (to knowi if you tested wrt question below)

I found this by static analysis.

Quoted 49 lines
Quoted 14 lines
Fixes a3be076dc174 ("net/9p/usbg: Add new usb gadget function transport")
---
 net/9p/trans_usbg.c | 9 +++++++++
 1 file changed, 9 insertions(+)
net/9p/trans_usbg.c +0 -0
index 6b694f117aef..047a2862fc84 100644

net/9p/trans_usbg.c static void usb9pfs_rx_complete(struct usb_ep *ep, struct usb_request *req) · line 250

Quoted 9 lines
net/9p/trans_usbg.c +6 -0
@@ -242,6 +242,15 @@ static void usb9pfs_rx_complete(struct usb_ep *ep, struct usb_request *req)
      if (!p9_rx_req)
              return;
 
+     /* Validate actual received size against buffer capacity */
+     if (req->actual > p9_rx_req->rc.capacity) {
+             dev_err(&cdev->gadget->dev,
+                     "received data size %u exceeds buffer capacity %zu\n",
+                     req->actual, p9_rx_req->rc.capacity);
+             p9_req_put(usb9pfs->client, p9_rx_req);

I still haven't gotten around to setting up something to test this, and even less the error case, but I'm not sure a single put is enough -- p9_client_cb does another put.
Conceptually I think it's better to mark the error and move on e.g. (not even compile tested)

```
        int status = REQ_STATUS_RCVD;

        [...]

        if (req->actual > p9_rx_req->rc.capacity) {
                dev_err(...)
                req->actual = 0;
                status = REQ_STATUS_ERROR;
        }

        memcpy(..)

        p9_rx_req->rc.size = req->actual;

        p9_client_cb(usb9pfs->client, p9_rx_req, status);
        p9_req_put(usb9pfs->client, p9_rx_req);

        complete(&usb9pfs->received);
```
Quoted 8 lines

(I'm not sure overriding req->actual is allowed, might be safer to use an intermediate variable like status instead)

What do you think?

Thanks,
--
Dominique Martinet | Asmadeus

Yes, I think your patch is better, my initial patch forgot p9_client_cb.

Thanks,
Yuhao Jiang

#3 lore raw

Danis Jiang wrote on Tue, Jun 17, 2025 at 11:01:40AM +0800:

Quoted 9 lines
Quoted 7 lines
Quoted 2 lines

Add validation in usb9pfs_rx_complete() to ensure req->actual does not exceed the buffer capacity before copying data.

Thanks for this check!

Did you reproduce this or was this static analysis found? (to knowi if you tested wrt question below)

I found this by static analysis.

Ok.

Quoted 23 lines
Quoted 23 lines

I still haven't gotten around to setting up something to test this, and even less the error case, but I'm not sure a single put is enough -- p9_client_cb does another put.
Conceptually I think it's better to mark the error and move on e.g. (not even compile tested)
```

        int status = REQ_STATUS_RCVD;

        [...]

        if (req->actual > p9_rx_req->rc.capacity) {
                dev_err(...)
                req->actual = 0;
                status = REQ_STATUS_ERROR;
        }

        memcpy(..)

        p9_rx_req->rc.size = req->actual;

        p9_client_cb(usb9pfs->client, p9_rx_req, status);
        p9_req_put(usb9pfs->client, p9_rx_req);
Quoted 8 lines
Quoted 6 lines
        complete(&usb9pfs->received);

```
(I'm not sure overriding req->actual is allowed, might be safer to use an intermediate variable like status instead)

What do you think?

Yes, I think your patch is better, my initial patch forgot p9_client_cb.

Ok, let's go with that then.

Would you like to resend "my" version, or should I do it (and refer to your patch as Reported-by)?

Also if you resend let's add Mirsad Todorovac <[email protected]> too Ccs, I've added him now.
(Mirsad, please check lore for full context if quote wasn't enough:
https://lkml.kernel.org/r/[email protected]
)

Thanks,

Signature · 1 line
Dominique Martinet | Asmadeus
#4 lore raw
Quoted 58 lines · On Tue, Jun 17, 2025 at 12:12 PM <[email protected]> wrote:

Danis Jiang wrote on Tue, Jun 17, 2025 at 11:01:40AM +0800:

Quoted 9 lines
Quoted 7 lines
Quoted 2 lines

Add validation in usb9pfs_rx_complete() to ensure req->actual does not exceed the buffer capacity before copying data.

Thanks for this check!

Did you reproduce this or was this static analysis found? (to knowi if you tested wrt question below)

I found this by static analysis.

Ok.

Quoted 23 lines
Quoted 23 lines

I still haven't gotten around to setting up something to test this, and even less the error case, but I'm not sure a single put is enough -- p9_client_cb does another put.
Conceptually I think it's better to mark the error and move on e.g. (not even compile tested)
```

        int status = REQ_STATUS_RCVD;

        [...]

        if (req->actual > p9_rx_req->rc.capacity) {
                dev_err(...)
                req->actual = 0;
                status = REQ_STATUS_ERROR;
        }

        memcpy(..)

        p9_rx_req->rc.size = req->actual;

        p9_client_cb(usb9pfs->client, p9_rx_req, status);
        p9_req_put(usb9pfs->client, p9_rx_req);
Quoted 8 lines
Quoted 6 lines
        complete(&usb9pfs->received);

```
(I'm not sure overriding req->actual is allowed, might be safer to use an intermediate variable like status instead)

What do you think?

Yes, I think your patch is better, my initial patch forgot p9_client_cb.

Ok, let's go with that then.

Would you like to resend "my" version, or should I do it (and refer to your patch as Reported-by)?

Also if you resend let's add Mirsad Todorovac <[email protected]> too Ccs, I've added him now.
(Mirsad, please check lore for full context if quote wasn't enough:
https://lkml.kernel.org/r/[email protected]
)

Quoted 3 lines

Thanks,
--
Dominique Martinet | Asmadeus

Sure, you can do it and add me as Reported-by, thanks!