Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [PATCH] usb: usbtmc: reject a short READ_STATUS_BYTE control transfer
@ 2026-11-19  8:39 Hyunjung Ko
  2026-11-19 13:13 ` Greg Kroah-Hartman
  0 siblings, 1 reply; 3+ messages in thread
From: Hyunjung Ko @ 2026-11-19  8:39 UTC (permalink / raw)
  To: Greg Kroah-Hartman
  Cc: Guido Kiener, linux-usb, linux-kernel, stable, Hyunjung Ko

usbtmc_get_stb() asks the device for three bytes of the READ_STATUS_BYTE
response into a kmalloc()'d buffer and only checks that usb_control_msg()
did not fail:

	buffer = kmalloc(8, GFP_KERNEL);
	...
	rv = usb_control_msg(..., buffer, 0x03, USB_CTRL_GET_TIMEOUT);
	if (rv < 0)
		goto exit;

usb_control_msg() returns the number of bytes actually transferred,
which a device is free to make shorter than the three that were
requested.  When the interface has no interrupt-in endpoint the status
byte is then taken straight from the third byte:

	*stb = buffer[2];

and the caller hands it to user space with put_user() from
USBTMC488_IOCTL_READ_STB and USBTMC_IOCTL_GET_SRQ_STB.  A device that
answers with one or two bytes - with the first byte set to
USBTMC_STATUS_SUCCESS so the status check passes - therefore leaks one
byte of uninitialised kmalloc memory to user space.  Both conditions are
under the device's control, since it also decides whether to expose an
interrupt-in endpoint.

The same leak was fixed once before in this driver: usbtmc_ioctl_request()
bounds its copy_to_user() to the number of bytes usb_control_msg() reported
instead of the number requested.  usbtmc_get_stb() cannot be fixed that way
because it reads a fixed offset, so reject the transfer unless all three
bytes arrived, matching what usbtmc488_ioctl_simple() already does for its
own one-byte request.

The check is not limited to the branch that reads buffer[2]: buffer[0] is
consumed on both paths, so a short response leaves the status check itself
reading uninitialised memory.  USBTMC-USB488 4.3.1 requires the three-byte
response, so a device that sends fewer is not conforming either way.

Signed-off-by: Hyunjung Ko <hj351016@gmail.com>
Cc: stable@vger.kernel.org
---
 drivers/usb/class/usbtmc.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/drivers/usb/class/usbtmc.c b/drivers/usb/class/usbtmc.c
index 51cd9320a736..c5b91a446a72 100644
--- a/drivers/usb/class/usbtmc.c
+++ b/drivers/usb/class/usbtmc.c
@@ -506,6 +506,10 @@ static int usbtmc_get_stb(struct usbtmc_file_data *file_data, __u8 *stb)
 	if (rv < 0) {
 		dev_err(dev, "stb usb_control_msg returned %d\n", rv);
 		goto exit;
+	} else if (rv != 3) {
+		dev_warn(dev, "stb usb_control_msg returned %d\n", rv);
+		rv = -EIO;
+		goto exit;
 	}
 
 	if (buffer[0] != USBTMC_STATUS_SUCCESS) {

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] usb: usbtmc: reject a short READ_STATUS_BYTE control transfer
  2026-11-19  8:39 [PATCH] usb: usbtmc: reject a short READ_STATUS_BYTE control transfer Hyunjung Ko
@ 2026-11-19 13:13 ` Greg Kroah-Hartman
  2026-11-19 13:25   ` khj
  0 siblings, 1 reply; 3+ messages in thread
From: Greg Kroah-Hartman @ 2026-11-19 13:13 UTC (permalink / raw)
  To: Hyunjung Ko; +Cc: Guido Kiener, linux-usb, linux-kernel, stable

On Thu, Nov 19, 2026 at 05:39:59PM +0900, Hyunjung Ko wrote:
> usbtmc_get_stb() asks the device for three bytes of the READ_STATUS_BYTE
> response into a kmalloc()'d buffer and only checks that usb_control_msg()
> did not fail:
> 
> 	buffer = kmalloc(8, GFP_KERNEL);
> 	...
> 	rv = usb_control_msg(..., buffer, 0x03, USB_CTRL_GET_TIMEOUT);
> 	if (rv < 0)
> 		goto exit;
> 
> usb_control_msg() returns the number of bytes actually transferred,
> which a device is free to make shorter than the three that were
> requested.  When the interface has no interrupt-in endpoint the status
> byte is then taken straight from the third byte:
> 
> 	*stb = buffer[2];
> 
> and the caller hands it to user space with put_user() from
> USBTMC488_IOCTL_READ_STB and USBTMC_IOCTL_GET_SRQ_STB.  A device that
> answers with one or two bytes - with the first byte set to
> USBTMC_STATUS_SUCCESS so the status check passes - therefore leaks one
> byte of uninitialised kmalloc memory to user space.  Both conditions are
> under the device's control, since it also decides whether to expose an
> interrupt-in endpoint.
> 
> The same leak was fixed once before in this driver: usbtmc_ioctl_request()
> bounds its copy_to_user() to the number of bytes usb_control_msg() reported
> instead of the number requested.  usbtmc_get_stb() cannot be fixed that way
> because it reads a fixed offset, so reject the transfer unless all three
> bytes arrived, matching what usbtmc488_ioctl_simple() already does for its
> own one-byte request.
> 
> The check is not limited to the branch that reads buffer[2]: buffer[0] is
> consumed on both paths, so a short response leaves the status check itself
> reading uninitialised memory.  USBTMC-USB488 4.3.1 requires the three-byte
> response, so a device that sends fewer is not conforming either way.
> 
> Signed-off-by: Hyunjung Ko <hj351016@gmail.com>
> Cc: stable@vger.kernel.org

This is a "fun" one in that LLMs keep tripping over it, and they
generate a patch that is usually wrong, like this one.

Be very careful here, and test your changes with a real device please.

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] usb: usbtmc: reject a short READ_STATUS_BYTE control transfer
  2026-11-19 13:13 ` Greg Kroah-Hartman
@ 2026-11-19 13:25   ` khj
  0 siblings, 0 replies; 3+ messages in thread
From: khj @ 2026-11-19 13:25 UTC (permalink / raw)
  To: Greg Kroah-Hartman; +Cc: Guido Kiener, linux-usb, linux-kernel, stable

You're right, sorry for the noise..!

buffer[2] is only read when there's no interrupt-in endpoint, so
rejecting every short transfer would break the devices that get the
status byte from the notification instead. Dropping the patch, I don't
have the hardware to test a narrower fix.

Hyunjung

2026년 11월 19일 (목) 오후 10:13, Greg Kroah-Hartman
<gregkh@linuxfoundation.org>님이 작성:
>
> On Thu, Nov 19, 2026 at 05:39:59PM +0900, Hyunjung Ko wrote:
> > usbtmc_get_stb() asks the device for three bytes of the READ_STATUS_BYTE
> > response into a kmalloc()'d buffer and only checks that usb_control_msg()
> > did not fail:
> >
> >       buffer = kmalloc(8, GFP_KERNEL);
> >       ...
> >       rv = usb_control_msg(..., buffer, 0x03, USB_CTRL_GET_TIMEOUT);
> >       if (rv < 0)
> >               goto exit;
> >
> > usb_control_msg() returns the number of bytes actually transferred,
> > which a device is free to make shorter than the three that were
> > requested.  When the interface has no interrupt-in endpoint the status
> > byte is then taken straight from the third byte:
> >
> >       *stb = buffer[2];
> >
> > and the caller hands it to user space with put_user() from
> > USBTMC488_IOCTL_READ_STB and USBTMC_IOCTL_GET_SRQ_STB.  A device that
> > answers with one or two bytes - with the first byte set to
> > USBTMC_STATUS_SUCCESS so the status check passes - therefore leaks one
> > byte of uninitialised kmalloc memory to user space.  Both conditions are
> > under the device's control, since it also decides whether to expose an
> > interrupt-in endpoint.
> >
> > The same leak was fixed once before in this driver: usbtmc_ioctl_request()
> > bounds its copy_to_user() to the number of bytes usb_control_msg() reported
> > instead of the number requested.  usbtmc_get_stb() cannot be fixed that way
> > because it reads a fixed offset, so reject the transfer unless all three
> > bytes arrived, matching what usbtmc488_ioctl_simple() already does for its
> > own one-byte request.
> >
> > The check is not limited to the branch that reads buffer[2]: buffer[0] is
> > consumed on both paths, so a short response leaves the status check itself
> > reading uninitialised memory.  USBTMC-USB488 4.3.1 requires the three-byte
> > response, so a device that sends fewer is not conforming either way.
> >
> > Signed-off-by: Hyunjung Ko <hj351016@gmail.com>
> > Cc: stable@vger.kernel.org
>
> This is a "fun" one in that LLMs keep tripping over it, and they
> generate a patch that is usually wrong, like this one.
>
> Be very careful here, and test your changes with a real device please.
>
> thanks,
>
> greg k-h

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-19 13:25 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19  8:39 [PATCH] usb: usbtmc: reject a short READ_STATUS_BYTE control transfer Hyunjung Ko
2026-11-19 13:13 ` Greg Kroah-Hartman
2026-11-19 13:25   ` khj

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox