usb: usbtmc: reject a short READ_STATUS_BYTE control transfer
Subsystems (2)
PATCH 1/1 usb: usbtmc: reject a short READ_STATUS_BYTE control transfer
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.
--- drivers/usb/class/usbtmc.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/usb/class/usbtmc.c +4 -0
@@ -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 ) {
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] usb: usbtmc: missing Fixes trailer and wrong ioctl name in commit message --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] usb: usbtmc: missing Fixes trailer and wrong ioctl name in commit message
--
commit 65820c8baf3e3185b7eb9f2281ee331492c9b001
Author: Hyunjung Ko <[email protected]>
Quoted 34 lines
usb: usbtmc: reject a short
READ_STATUS_BYTEcontrol transfer
usbtmc_get_stb()asks the device for three bytes of theREAD_STATUS_BYTEresponse into a kmalloc()'d buffer and only checks thatusb_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()fromUSBTMC488_IOCTL_READ_STBandUSBTMC_IOCTL_GET_SRQ_STB. A device that answers with one or two bytes - with the first byte set toUSBTMC_STATUS_SUCCESSso 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 itscopy_to_user()to the number of bytesusb_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 whatusbtmc488_ioctl_simple()already does for its own one-byte request.
Quoted 7 lines
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 <[email protected]>
[Severity: Low]
Should this bug fix include a Fixes tag for the commit that introduced the unchecked three-byte READ_STATUS_BYTE control transfer:
Also, the commit message mentions USBTMC_IOCTL_GET_SRQ_STB as the second caller of usbtmc_get_stb(), but in usbtmc_ioctl() the second caller is USBTMC_IOCTL_GET_STB (USBTMC_IOCTL_GET_SRQ_STB calls usbtmc_ioctl_get_srq_stb() instead).
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Greg Kroah-Hartman
maintainerQuoted 32 lines · Hyunjung Ko
usbtmc_get_stb()asks the device for three bytes of theREAD_STATUS_BYTEresponse into a kmalloc()'d buffer and only checks thatusb_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()fromUSBTMC488_IOCTL_READ_STBandUSBTMC_IOCTL_GET_SRQ_STB. A device that answers with one or two bytes - with the first byte set toUSBTMC_STATUS_SUCCESSso 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 itscopy_to_user()to the number of bytesusb_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 whatusbtmc488_ioctl_simple()already does for its own one-byte request.
Quoted 7 lines
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 <[email protected]>
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
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
<[email protected]>님이 작성:
Quoted 50 lines · Hyunjung Ko
Quoted 32 lines
usbtmc_get_stb()asks the device for three bytes of theREAD_STATUS_BYTEresponse into a kmalloc()'d buffer and only checks thatusb_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()fromUSBTMC488_IOCTL_READ_STBandUSBTMC_IOCTL_GET_SRQ_STB. A device that answers with one or two bytes - with the first byte set toUSBTMC_STATUS_SUCCESSso 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 itscopy_to_user()to the number of bytesusb_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 whatusbtmc488_ioctl_simple()already does for its own one-byte request.Quoted 7 lines
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 <[email protected]>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