Skip to contentSkip to conversation

char: xillybus: Check endpoint type at probe time

PATCH Superseded Zheyu Ma 2 participants 3 messages May 29, 2022 – May 31, 2022

Subsystems (2)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Zheyu Ma

author
#0 lore raw

PATCH v3 1/1 char: xillybus: Check endpoint type at probe time

The driver submits bulk urb without checking the endpoint type is actually bulk.

[    3.108690] usb 1-1: BOGUS urb xfer, pipe 3 != type 1
[    3.108983] WARNING: CPU: 0 PID: 211 at drivers/usb/core/urb.c:503 usb_submit_urb+0xcd9/0x18b0
[    3.110976] RIP: 0010:usb_submit_urb+0xcd9/0x18b0
[    3.115318] Call Trace:
[    3.115452]  <TASK>
[    3.115570]  try_queue_bulk_in+0x43c/0x6e0 [xillyusb]
[    3.115838]  xillyusb_probe+0x488/0x1230 [xillyusb]

Add a check at probe time to fix the bug.

---
Changes in v3:
- Check the endpoint type more earlier
Changes in v2:
- Check the endpoint type at probe time

---
 drivers/char/xillybus/xillyusb.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
drivers/char/xillybus/xillyusb.c +32 -0
index dc3551796e5e..4ed19a2a04e3 100644
@@ -167,6 +167,7 @@ struct xillyusb_dev {
 	struct device		*dev; /* For dev_err() and such */
 	struct kref		kref;
 	struct workqueue_struct	*workq;
+	struct usb_interface *intf;
 
 	int error;
 	spinlock_t error_lock; /* protect @error */
@@ -1891,6 +1892,17 @@ static const struct file_operations xillyusb_fops = {
 
 static int xillyusb_setup_base_eps(struct xillyusb_dev *xdev)
 {
+	int ret;
+	struct usb_endpoint_descriptor *in, *out;
+
+	ret = usb_find_common_endpoints(xdev->intf->cur_altsetting, &in, &out, NULL, NULL);
+	if (ret)
+		return ret;
+
+	if (in->bEndpointAddress != (IN_EP_NUM | USB_DIR_IN) ||
+		out->bEndpointAddress != (MSG_EP_NUM | USB_DIR_OUT))
+		return -EINVAL;
+
 	xdev->msg_ep = endpoint_alloc(xdev, MSG_EP_NUM | USB_DIR_OUT,
 				      bulk_out_work, 1, 2);
 	if (!xdev->msg_ep)
@@ -1916,6 +1928,21 @@ static int xillyusb_setup_base_eps(struct xillyusb_dev *xdev)
 	return -ENOMEM;
 }
 
+static int xillyusb_check_endpoint(struct xillyusb_dev *xdev, u8 addr)
+{
+	int i;
+	struct usb_host_interface *if_desc = xdev->intf->altsetting;
+
+	for (i = 0; i < if_desc->desc.bNumEndpoints; i++) {
+		struct usb_endpoint_descriptor *ep = &if_desc->endpoint[i].desc;
+
+		if (ep->bEndpointAddress == addr && usb_endpoint_is_bulk_out(ep))
+			return 0;
+	}
+
+	return -EINVAL;
+}
+
 static int setup_channels(struct xillyusb_dev *xdev,
 			  __le16 *chandesc,
 			  int num_channels)
@@ -1962,6 +1989,10 @@ static int setup_channels(struct xillyusb_dev *xdev,
 			chan->out_log2_element_size = out_desc & 0x0f;
 			chan->out_log2_fifo_size =
 				((out_desc >> 8) & 0x1f) + 16;
+			if (xillyusb_check_endpoint(xdev, (i+2) | USB_DIR_OUT)) {
+				kfree(xdev->channels);
+				return -EINVAL;
+			}
 		}
 	}
 
@@ -2125,6 +2156,7 @@ static int xillyusb_probe(struct usb_interface *interface,
 	mutex_init(&xdev->process_in_mutex);
 	mutex_init(&xdev->msg_mutex);
 
+	xdev->intf = interface;
 	xdev->udev = usb_get_dev(interface_to_usbdev(interface));
 	xdev->dev = &interface->dev;
 	xdev->error = 0;
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 4 lines · Zheyu Ma
  static int xillyusb_setup_base_eps(struct xillyusb_dev *xdev)
  {
drivers/char/xillybus/xillyusb.c +1 -0
+	int ret;

drivers/char/xillybus/xillyusb.c static const struct file_operations xillyusb_fops = { · line 1905

Quoted 10 lines
drivers/char/xillybus/xillyusb.c +10 -0
+	struct usb_endpoint_descriptor *in, *out;
+
+	ret = usb_find_common_endpoints(xdev->intf->cur_altsetting,&in,&out, NULL, NULL);
+	if (ret)
+		return ret;
+
+	if (in->bEndpointAddress != (IN_EP_NUM | USB_DIR_IN) ||
+		out->bEndpointAddress != (MSG_EP_NUM | USB_DIR_OUT))
+		return -EINVAL;
+

As far as I understand, this finds the first BULK endpoints in both directions, and verifies that their addresses are MSG_EP_NUM and IN_EP_NUM. Because both of these happen to equal 1, I suppose this indeed checks the right thing. But am I right that this won't work if either MSG_EP_NUM or IN_EP_NUM have a value that isn't 1? Not that I think that will ever happen, but still.

Quoted 7 lines
drivers/char/xillybus/xillyusb.c +5 -0
+static int xillyusb_check_endpoint(struct xillyusb_dev *xdev, u8 addr)
+{
+	int i;
+	struct usb_host_interface *if_desc = xdev->intf->altsetting;
+

drivers/char/xillybus/xillyusb.c static int xillyusb_setup_base_eps(struct xillyusb_dev *xdev) · line 1945

Quoted 10 lines
drivers/char/xillybus/xillyusb.c +10 -0
+	for (i = 0; i<  if_desc->desc.bNumEndpoints; i++) {
+		struct usb_endpoint_descriptor *ep =&if_desc->endpoint[i].desc;
+
+		if (ep->bEndpointAddress == addr&&  usb_endpoint_is_bulk_out(ep))
+			return 0;
+	}
+
+	return -EINVAL;
+}
+

Given that you've added this function, why isn't it used in xillyusb_setup_base_eps()?

Thanks,
Eli

author
#2 lore raw
Quoted 14 lines · Eli Billauer
Quoted 12 lines · Zheyu Ma
  static int xillyusb_setup_base_eps(struct xillyusb_dev *xdev)
  {
drivers/char/xillybus/xillyusb.c +10 -0
+     int ret;
+     struct usb_endpoint_descriptor *in, *out;
+
+     ret = usb_find_common_endpoints(xdev->intf->cur_altsetting,&in,&out, NULL, NULL);
+     if (ret)
+             return ret;
+
+     if (in->bEndpointAddress != (IN_EP_NUM | USB_DIR_IN) ||
+             out->bEndpointAddress != (MSG_EP_NUM | USB_DIR_OUT))
+             return -EINVAL;
Quoted 8 lines
Quoted 2 lines

+

As far as I understand, this finds the first BULK endpoints in both directions, and verifies that their addresses are MSG_EP_NUM and IN_EP_NUM. Because both of these happen to equal 1, I suppose this indeed checks the right thing. But am I right that this won't work if either MSG_EP_NUM or IN_EP_NUM have a value that isn't 1? Not that I think that will ever happen, but still.

Indeed, the correctness of this code comes from the fact that both MSG_EP_NUM and IN_EP_NUM are the first bulk endpoint, without such an assumption this check does not hold.
I did this just for convenience, we can also use xillyusb_check_endpoint() to check them.

Quoted 11 lines
Quoted 11 lines
drivers/char/xillybus/xillyusb.c +11 -0
+static int xillyusb_check_endpoint(struct xillyusb_dev *xdev, u8 addr)
+{
+     int i;
+     struct usb_host_interface *if_desc = xdev->intf->altsetting;
+
+     for (i = 0; i<  if_desc->desc.bNumEndpoints; i++) {
+             struct usb_endpoint_descriptor *ep =&if_desc->endpoint[i].desc;
+
+             if (ep->bEndpointAddress == addr&&  usb_endpoint_is_bulk_out(ep))
+                     return 0;
+     }
Quoted 8 lines

drivers/char/xillybus/xillyusb.c static int xillyusb_setup_base_eps(struct xillyusb_dev *xdev) · line 1945

Quoted 6 lines
drivers/char/xillybus/xillyusb.c +4 -0
+
+     return -EINVAL;
+}
+

Given that you've added this function, why isn't it used in xillyusb_setup_base_eps()?

It is feasible to use this function for checking in xillyusb_setup_base_eps(), perhaps with some modifications. Anyway, I will do so in the next version of the patch.

Thanks,
Zheyu Ma