public inbox for linux-usb@vger.kernel.org
 help / color / mirror / Atom feed
From: Dave Penkler <dpenkler@gmail.com>
To: Vasiliy Kovalev <kovalev@altlinux.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Jeff Johnson <jeff.johnson@oss.qualcomm.com>,
	linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org,
	lvc-project@linuxtesting.org
Subject: Re: [PATCH] usbtmc: Fix slab-out-of-bounds access in usbtmc_interrupt
Date: Mon, 24 Mar 2025 16:03:45 +0100	[thread overview]
Message-ID: <Z-F0Ud9tAiK5AQ1b@egonzo> (raw)
In-Reply-To: <20250313211907.1179173-1-kovalev@altlinux.org>

On Fri, Mar 14, 2025 at 12:19:07AM +0300, Vasiliy Kovalev wrote:
> Prevent buffer overflow in usbtmc_interrupt by ensuring interrupt IN
> endpoint packets are at least 2 bytes (1 byte ID + 1 byte data), as
> required by the driver. Without this, short packets could lead to
> out-of-bounds reads of iin_buffer.
> 
> - In usbtmc_probe(), reject devices with wMaxPacketSize < 2 bytes to
>   ensure the allocated iin_buffer is large enough.
> - In usbtmc_interrupt(), skip processing and log a warning if
>   actual_length < 2 bytes, avoiding invalid memory access.
> 
> This fixes a KASAN slab-out-of-bounds error:
> 
> BUG: KASAN: slab-out-of-bounds in usbtmc_interrupt (drivers/usb/class/usbtmc.c:2297)
> Read of size 1 at addr ffff88800a41b2e1 by task kworker/0:1/11
> CPU: 0 UID: 0 PID: 11 Comm: kworker/0:1 Not tainted 6.14.0-un-def-alt0.rc6.kasan #1
> Call Trace:
>  <IRQ>
>  dump_stack_lvl (lib/dump_stack.c:122)
>  print_report (mm/kasan/report.c:521)
>  kasan_report (mm/kasan/report.c:636)
>  usbtmc_interrupt (drivers/usb/class/usbtmc.c:2297)
>  __usb_hcd_giveback_urb (drivers/usb/core/hcd.c:1650)
>  usb_hcd_giveback_urb (drivers/usb/core/hcd.c:1735)
>  dummy_timer (drivers/usb/gadget/udc/dummy_hcd.c:1995)
>  __hrtimer_run_queues (kernel/time/hrtimer.c:1865)
>  hrtimer_run_softirq (kernel/time/hrtimer.c:1884)
>  handle_softirqs (kernel/softirq.c:561)
>  __irq_exit_rcu (kernel/softirq.c:662)
>  irq_exit_rcu (kernel/softirq.c:680)
>  sysvec_apic_timer_interrupt (arch/x86/kernel/apic/apic.c:1049)
>  </IRQ>
> 
> Found by Linux Verification Center (linuxtesting.org) with
> 'USB Gadget Tests'[1]:
> 
> $ make usbtmc
> $ sudo src/usbtmc/usbtmc --invalid_ep_int_len
> 
> [1] Link: https://github.com/kovalev0/usb-gadget-tests
> Fixes: dbf3e7f654c0 ("Implement an ioctl to support the USMTMC-USB488 READ_STATUS_BYTE operation.")
> Cc: stable@vger.kernel.org
> Signed-off-by: Vasiliy Kovalev <kovalev@altlinux.org>
> ---
>  drivers/usb/class/usbtmc.c | 20 ++++++++++++++++++++
>  1 file changed, 20 insertions(+)
> 
> diff --git a/drivers/usb/class/usbtmc.c b/drivers/usb/class/usbtmc.c
> index 34e46ef308abf..8a3ba90f32455 100644
> --- a/drivers/usb/class/usbtmc.c
> +++ b/drivers/usb/class/usbtmc.c
> @@ -47,6 +47,9 @@
>   */
>  #define USBTMC_MAX_READS_TO_CLEAR_BULK_IN	100
>  
> +/* Minimum packet size for interrupt IN endpoint */
> +#define USBTMC_MIN_INT_IN_PACKET_SIZE 2	// 1 byte ID + 1 byte data
> +
>  static const struct usb_device_id usbtmc_devices[] = {
>  	{ USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 0), },
>  	{ USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 1), },
> @@ -2291,6 +2294,14 @@ static void usbtmc_interrupt(struct urb *urb)
>  
>  	switch (status) {
>  	case 0: /* SUCCESS */
> +		/* check for valid length */
> +		if (urb->actual_length < USBTMC_MIN_INT_IN_PACKET_SIZE) {
> +			dev_warn(dev, "short interrupt packet: %d bytes, min %d required\n",
> +				 urb->actual_length,
> +				 USBTMC_MIN_INT_IN_PACKET_SIZE);
> +			goto exit;
> +		}
> +
>  		/* check for valid STB notification */
>  		if (data->iin_buffer[0] > 0x81) {
>  			data->bNotify1 = data->iin_buffer[0];
> @@ -2426,6 +2437,15 @@ static int usbtmc_probe(struct usb_interface *intf,
>  		dev_err(&intf->dev, "can't read capabilities\n");
>  
>  	if (data->iin_ep_present) {
> +		if (data->iin_wMaxPacketSize < USBTMC_MIN_INT_IN_PACKET_SIZE) {
> +			dev_err(&intf->dev,
> +				"Int in endpoint wMaxPacketSize too small: %d, minimum %d required\n",
> +				data->iin_wMaxPacketSize,
> +				USBTMC_MIN_INT_IN_PACKET_SIZE);
> +			retcode = -EINVAL;
> +			goto error_register;
> +		}
> +
>  		/* allocate int urb */
>  		data->iin_urb = usb_alloc_urb(0, GFP_KERNEL);
>  		if (!data->iin_urb) {
> -- 
> 2.42.2
>
Tested-by: Dave Penkler <dpenkler@gmail.com>


      reply	other threads:[~2025-03-24 15:03 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-13 21:19 [PATCH] usbtmc: Fix slab-out-of-bounds access in usbtmc_interrupt Vasiliy Kovalev
2025-03-24 15:03 ` Dave Penkler [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=Z-F0Ud9tAiK5AQ1b@egonzo \
    --to=dpenkler@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jeff.johnson@oss.qualcomm.com \
    --cc=kovalev@altlinux.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=lvc-project@linuxtesting.org \
    /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