Skip to contentSkip to conversation

usb: misc: usbio: fix fatal URB resubmission and probe maxpacket bounds

PATCH In review Elkin Cruz 3 participants 5 messages Nov 25 – Nov 30

Subsystems (3)
lore
Share & tools
lore

Elkin Cruz

author
#0 lore raw

cover usb: misc: usbio: fix fatal URB resubmission and probe maxpacket bounds

I am trying out new Clang static analysis checks for kernel bug classes, and they flagged this code as possibly buggy. The findings were triaged/fixed and commit descriptions were drafted with help of AI, then checked against linux-next (not runtime-tested on hardware).

Thanks,
Elkin

Elkin Cruz (2):
usb: misc: usbio: stop resubmitting bulk IN URB on fatal status
usb: misc: usbio: validate endpoint maxpacket sizes in usbio_probe()

 drivers/usb/misc/usbio.c | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)
Signature · 1 line
2.56.1.839.g22a8290db9-goog
author
#1 lore raw

PATCH 1/2 usb: misc: usbio: stop resubmitting bulk IN URB on fatal status

usbio_bulk_recv() unconditionally resubmits usbio->urb in softirq completion context regardless of urb->status. When the bulk IN endpoint encounters a terminal status such as -EPIPE, -ESHUTDOWN, -ENODEV, -ECONNRESET, or -ENOENT (for example on endpoint stall, disconnect, or usb_kill_urb()), resubmitting the URB causes either a tight softirq resubmission loop on persistent errors or a wasted submission against a terminating URB, and zero-length completions also inspect stale bpkt->header.flags bytes.

Return without resubmitting on -ENOENT, -ECONNRESET, -ENODEV, -ESHUTDOWN, and -EPIPE (matching usb-ljca), verify urb->actual_length covers the packet header before checking USBIO_PKTFLAG_RSP, and log unexpected usb_submit_urb() errors.

Fixes 121a0f839dbb ("usb: misc: Add Intel USBIO bridge driver")
---
 drivers/usb/misc/usbio.c | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)
drivers/usb/misc/usbio.c +18 -4
index 7b5f8669aee3..6142a32c18a9 100644
@@ -239,17 +239,31 @@ static void usbio_bulk_recv(struct urb *urb)
 {
 	struct usbio_bulk_packet *bpkt = urb->transfer_buffer;
 	struct usbio_device *usbio = urb->context;
+	int ret;
 
-	if (!urb->status) {
-		if (bpkt->header.flags & USBIO_PKTFLAG_RSP) {
+	switch (urb->status) {
+	case 0:
+		if (urb->actual_length >= sizeof(bpkt->header) &&
+		    (bpkt->header.flags & USBIO_PKTFLAG_RSP)) {
 			usbio->rxdat_len = urb->actual_length;
 			complete(&usbio->done);
 		}
-	} else if (urb->status != -ENOENT) {
+		break;
+	case -ENOENT:
+	case -ECONNRESET:
+	case -ENODEV:
+	case -ESHUTDOWN:
+	case -EPIPE:
+		dev_dbg(usbio->dev, "Bulk in urb terminated: %d\n", urb->status);
+		return;
+	default:
 		dev_err(usbio->dev, "Bulk in error %d\n", urb->status);
+		break;
 	}
 
-	usb_submit_urb(usbio->urb, GFP_ATOMIC);
+	ret = usb_submit_urb(usbio->urb, GFP_ATOMIC);
+	if (ret && ret != -EPERM)
+		dev_err(usbio->dev, "Submit bulk in urb error %d\n", ret);
 }
 
 int usbio_bulk_msg(struct auxiliary_device *adev, u8 type, u8 cmd, bool last,
Signature · 1 line
2.56.1.839.g22a8290db9-goog
author
#2 lore raw

PATCH 2/2 usb: misc: usbio: validate endpoint maxpacket sizes in usbio_probe()

In reply to Elkin Cruz

usbio_probe() allocates ctrlbuf, txbuf, and rxbuf directly from the USB descriptor maxpacket sizes without verifying that each buffer can hold its packet header. A malformed USB descriptor with a smaller maxpacket size causes unsigned underflow in length checks and buffer overflows in usbio_ctrl_msg() and usbio_bulk_msg().

Reject devices whose control or bulk endpoint maxpacket sizes are smaller than sizeof(struct usbio_ctrl_packet) or sizeof(struct usbio_bulk_packet), respectively.

Fixes 121a0f839dbb ("usb: misc: Add Intel USBIO bridge driver")
---
 drivers/usb/misc/usbio.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/usb/misc/usbio.c +6 -0
index 6142a32c18a9..c9f22b600333 100644
@@ -591,6 +591,8 @@ static int usbio_probe(struct usb_interface *intf, const struct usb_device_id *i
 
 	usbio->ctrl_pipe = usb_endpoint_num(&udev->ep0.desc);
 	usbio->ctrlbuf_len = usb_maxpacket(udev, usbio->ctrl_pipe);
+	if (usbio->ctrlbuf_len < sizeof(struct usbio_ctrl_packet))
+		return -ENODEV;
 	usbio->ctrlbuf = devm_kzalloc(dev, usbio->ctrlbuf_len, GFP_KERNEL);
 	if (!usbio->ctrlbuf)
 		return -ENOMEM;
@@ -609,6 +611,8 @@ static int usbio_probe(struct usb_interface *intf, const struct usb_device_id *i
 		usbio->txbuf_len = 63;
 	else
 		usbio->txbuf_len = usb_endpoint_maxp(ep_out);
+	if (usbio->txbuf_len < sizeof(struct usbio_bulk_packet))
+		return -ENODEV;
 
 	usbio->txbuf = devm_kzalloc(dev, usbio->txbuf_len, GFP_KERNEL);
 	if (!usbio->txbuf)
@@ -620,6 +624,8 @@ static int usbio_probe(struct usb_interface *intf, const struct usb_device_id *i
 		usbio->rxbuf_len = 63;
 	else
 		usbio->rxbuf_len = usb_endpoint_maxp(ep_in);
+	if (usbio->rxbuf_len < sizeof(struct usbio_bulk_packet))
+		return -ENODEV;
 
 	usbio->rxbuf = devm_kzalloc(dev, usbio->rxbuf_len, GFP_KERNEL);
 	if (!usbio->rxbuf)
Signature · 1 line
2.56.1.839.g22a8290db9-goog
reviewer
#3 lore raw

Re: [PATCH 2/2] usb: misc: usbio: validate endpoint maxpacket sizes in usbio_probe()

Hi Elkin,

Quoted 9 lines · Elkin Cruz

usbio_probe() allocates ctrlbuf, txbuf, and rxbuf directly from the USB descriptor maxpacket sizes without verifying that each buffer can hold its packet header. A malformed USB descriptor with a smaller maxpacket size causes unsigned underflow in length checks and buffer overflows in usbio_ctrl_msg() and usbio_bulk_msg().

Reject devices whose control or bulk endpoint maxpacket sizes are smaller than sizeof(struct usbio_ctrl_packet) or sizeof(struct usbio_bulk_packet), respectively.

I'd use another error code to signal this, e.g. -EIO or -ENXIO. -ENODEV really says there's no device, and in this case the device is there (but happens to be behaving somehow badly).

Quoted 39 lines
Fixes 121a0f839dbb ("usb: misc: Add Intel USBIO bridge driver")
---
 drivers/usb/misc/usbio.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/usb/misc/usbio.c +6 -0
index 6142a32c18a9..c9f22b600333 100644
@@ -591,6 +591,8 @@ static int usbio_probe(struct usb_interface *intf, const struct usb_device_id *i
 
 	usbio->ctrl_pipe = usb_endpoint_num(&udev->ep0.desc);
 	usbio->ctrlbuf_len = usb_maxpacket(udev, usbio->ctrl_pipe);
+	if (usbio->ctrlbuf_len < sizeof(struct usbio_ctrl_packet))
+		return -ENODEV;
 	usbio->ctrlbuf = devm_kzalloc(dev, usbio->ctrlbuf_len, GFP_KERNEL);
 	if (!usbio->ctrlbuf)
 		return -ENOMEM;
@@ -609,6 +611,8 @@ static int usbio_probe(struct usb_interface *intf, const struct usb_device_id *i
 		usbio->txbuf_len = 63;
 	else
 		usbio->txbuf_len = usb_endpoint_maxp(ep_out);
+	if (usbio->txbuf_len < sizeof(struct usbio_bulk_packet))
+		return -ENODEV;
 
 	usbio->txbuf = devm_kzalloc(dev, usbio->txbuf_len, GFP_KERNEL);
 	if (!usbio->txbuf)
@@ -620,6 +624,8 @@ static int usbio_probe(struct usb_interface *intf, const struct usb_device_id *i
 		usbio->rxbuf_len = 63;
 	else
 		usbio->rxbuf_len = usb_endpoint_maxp(ep_in);
+	if (usbio->rxbuf_len < sizeof(struct usbio_bulk_packet))
+		return -ENODEV;
 
 	usbio->rxbuf = devm_kzalloc(dev, usbio->rxbuf_len, GFP_KERNEL);
 	if (!usbio->rxbuf)
Signature · 3 lines
Regards,

Sakari Ailus