public inbox for linux-can@vger.kernel.org
 help / color / mirror / Atom feed
From: Vincent Mailhol <mailhol@kernel.org>
To: Ziyi Guo <n7l8m4@u.northwestern.edu>
Cc: socketcan@esd.eu, "David S . Miller" <davem@davemloft.net>,
	Wolfgang Grandegger <wg@grandegger.com>,
	linux-can@vger.kernel.org, linux-kernel@vger.kernel.org,
	Frank Jungclaus <frank.jungclaus@esd.eu>,
	Marc Kleine-Budde <mkl@pengutronix.de>
Subject: Re: [PATCH] can: esd_usb: add endpoint type validation
Date: Fri, 13 Feb 2026 17:20:09 +0100	[thread overview]
Message-ID: <a286a587-17ea-4704-b26e-c3b3591ef71f@kernel.org> (raw)
In-Reply-To: <69ddd9ac-a12c-4680-9969-d5c45e8e5ffc@kernel.org>

On 13/02/2026 at 17:01, Vincent Mailhol wrote:
> On 10/02/2026 at 04:59, Ziyi Guo wrote:
>> esd_usb_probe() constructs bulk pipes for two endpoints without
>> verifying their transfer types:
>>
>>   - usb_rcvbulkpipe(dev->udev, 1) for RX (version reply, async RX data)
>>   - usb_sndbulkpipe(dev->udev, 2) for TX (version query, CAN frames)
>>
>> A malformed USB device can present these endpoints with transfer types
>> that differ from what the driver assumes, triggering the WARNING in
>> usb_submit_urb().
>>
>> Add usb_check_bulk_endpoints() before the first bulk transfer to verify
>> endpoint types, rejecting devices with mismatched descriptors at probe
>> time.
>>
>> Similar to
>> - commit 90b7f2961798 ("net: usb: rtl8150: enable basic endpoint checking")
>>   which established the usb_check_bulk_endpoints() validation pattern.
>>
>> Fixes: 96d8e90382dc ("can: Add driver for esd CAN-USB/2 device")
>> Signed-off-by: Ziyi Guo <n7l8m4@u.northwestern.edu>
>> ---
>>  drivers/net/can/usb/esd_usb.c | 11 +++++++++++
>>  1 file changed, 11 insertions(+)
>>
>> diff --git a/drivers/net/can/usb/esd_usb.c b/drivers/net/can/usb/esd_usb.c
>> index 8cc924c47042..054ded490eb3 100644
>> --- a/drivers/net/can/usb/esd_usb.c
>> +++ b/drivers/net/can/usb/esd_usb.c
>> @@ -1301,6 +1301,10 @@ static int esd_usb_probe(struct usb_interface *intf,
>>  	struct esd_usb *dev;
>>  	union esd_usb_msg *msg;
>>  	int i, err;
>> +	static const u8 bulk_ep_addr[] = {
>> +		USB_DIR_IN | 1,		/* EP 1 IN  (RX) */
>> +		USB_DIR_OUT | 2,	/* EP 2 OUT (TX) */
>> +		0};
> 
> Instead of using magic numbers with comments declare a macro:
> 
> #define ESD_USB_EP_IN 1
> #define ESD_USB_EP_OUT 2
> 
> and use it throughout the driver.
> 
>>  	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
>>  	if (!dev) {
>> @@ -1320,6 +1324,13 @@ static int esd_usb_probe(struct usb_interface *intf,
>>  		goto free_msg;
>>  	}
>>  
>> +	/* Verify that the required bulk endpoints are present */
>> +	if (!usb_check_bulk_endpoints(intf, bulk_ep_addr)) {
>> +		dev_err(&intf->dev, "Missing or invalid bulk endpoints\n");
>> +		err = -ENODEV;
>> +		goto free_msg;
>> +	}
> 
> usb_check_bulk_endpoints() calls usb_find_endpoint(). Here, I would
> rather just call usb_find_endpoint() and use the found endpoints. Like
> this:
> 
> ------------8<------------
> diff --git a/drivers/net/can/usb/esd_usb.c b/drivers/net/can/usb/esd_usb.c
> index 8cc924c47042b..d2261046be91d 100644
> --- a/drivers/net/can/usb/esd_usb.c
> +++ b/drivers/net/can/usb/esd_usb.c
> @@ -272,6 +272,9 @@ struct esd_usb {
>  
>  	struct usb_anchor rx_submitted;
>  
> +	unsigned int rx_pipe;
> +	unsigned int tx_pipe;
> +
>  	int net_count;
>  	u32 version;
>  	int rxinitdone;
> @@ -537,7 +540,7 @@ static void esd_usb_read_bulk_callback(struct urb *urb)
>  	}
>  
>  resubmit_urb:
> -	usb_fill_bulk_urb(urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
> +	usb_fill_bulk_urb(urb, dev->udev, dev->rx_pipe,
>  			  urb->transfer_buffer, ESD_USB_RX_BUFFER_SIZE,
>  			  esd_usb_read_bulk_callback, dev);
>  
> @@ -626,9 +629,7 @@ static int esd_usb_send_msg(struct esd_usb *dev, union esd_usb_msg *msg)
>  {
>  	int actual_length;
>  
> -	return usb_bulk_msg(dev->udev,
> -			    usb_sndbulkpipe(dev->udev, 2),
> -			    msg,
> +	return usb_bulk_msg(dev->udev, dev->tx_pipe, msg,
>  			    msg->hdr.len * sizeof(u32), /* convert to # of bytes */
>  			    &actual_length,
>  			    1000);
> @@ -639,12 +640,8 @@ static int esd_usb_wait_msg(struct esd_usb *dev,
>  {
>  	int actual_length;
>  
> -	return usb_bulk_msg(dev->udev,
> -			    usb_rcvbulkpipe(dev->udev, 1),
> -			    msg,
> -			    sizeof(*msg),
> -			    &actual_length,
> -			    1000);
> +	return usb_bulk_msg(dev->udev, dev->rx_pipe, msg,
> +			    sizeof(*msg), &actual_length, 1000);
>  }
>  
>  static int esd_usb_setup_rx_urbs(struct esd_usb *dev)
> @@ -677,8 +674,7 @@ static int esd_usb_setup_rx_urbs(struct esd_usb *dev)
>  
>  		urb->transfer_dma = buf_dma;
>  
> -		usb_fill_bulk_urb(urb, dev->udev,
> -				  usb_rcvbulkpipe(dev->udev, 1),
> +		usb_fill_bulk_urb(urb, dev->udev, dev->tx_pipe,
                                                       ^^^^^^^
Typo: this is meant to be rx_pipe.

>  				  buf, ESD_USB_RX_BUFFER_SIZE,
>  				  esd_usb_read_bulk_callback, dev);
>  		urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
> @@ -903,7 +899,7 @@ static netdev_tx_t esd_usb_start_xmit(struct sk_buff *skb,
>  	/* hnd must not be 0 - MSB is stripped in txdone handling */
>  	msg->tx.hnd = BIT(31) | i; /* returned in TX done message */
>  
> -	usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 2), buf,
> +	usb_fill_bulk_urb(urb, dev->udev, dev->tx_pipe, buf,
>  			  msg->hdr.len * sizeof(u32), /* convert to # of bytes */
>  			  esd_usb_write_bulk_callback, context);
>  
> @@ -1298,10 +1294,16 @@ static int esd_usb_probe_one_net(struct usb_interface *intf, int index)
>  static int esd_usb_probe(struct usb_interface *intf,
>  			 const struct usb_device_id *id)
>  {
> +	struct usb_endpoint_descriptor *ep_in, *ep_out;
>  	struct esd_usb *dev;
>  	union esd_usb_msg *msg;
>  	int i, err;
>  
> +	err = usb_find_common_endpoints(intf->cur_altsetting, &ep_in, &ep_out,
> +					NULL, NULL);
> +	if (err)
> +		return err;
> +
>  	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
>  	if (!dev) {
>  		err = -ENOMEM;
> @@ -1309,6 +1311,8 @@ static int esd_usb_probe(struct usb_interface *intf,
>  	}
>  
>  	dev->udev = interface_to_usbdev(intf);
> +	dev->rx_pipe = usb_rcvbulkpipe(dev->udev, ep_in->bEndpointAddress);
> +	dev->tx_pipe = usb_sndbulkpipe(dev->udev, ep_out->bEndpointAddress);
>  
>  	init_usb_anchor(&dev->rx_submitted);
>  
> ------------>8------------
> 
> This also enforce that we are using existing endpoints while being
> more concise.


Yours sincerely,
Vincent Mailhol


      parent reply	other threads:[~2026-02-13 16:20 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-10  3:59 [PATCH] can: esd_usb: add endpoint type validation Ziyi Guo
2026-02-13 16:01 ` Vincent Mailhol
2026-02-13 16:06   ` Ziyi Guo
2026-02-13 16:20   ` Vincent Mailhol [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a286a587-17ea-4704-b26e-c3b3591ef71f@kernel.org \
    --to=mailhol@kernel.org \
    --cc=davem@davemloft.net \
    --cc=frank.jungclaus@esd.eu \
    --cc=linux-can@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mkl@pengutronix.de \
    --cc=n7l8m4@u.northwestern.edu \
    --cc=socketcan@esd.eu \
    --cc=wg@grandegger.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox