All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] usb: storage: shuttle_usbat: reject oversized device-reported read length
@ 2026-12-02  2:44 Artem Dinaburg
  2026-12-02  2:49 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Artem Dinaburg @ 2026-12-02  2:44 UTC (permalink / raw)
  To: stern, gregkh
  Cc: lihaofeng, 13266079573, linux-usb, usb-storage, linux-kernel

For device-to-host commands other than READ_10 and READ_CD,
usbat_hp8200e_transport() reads the response length from the device's
ATA LBA_ME and LBA_HI registers and passes it to usbat_read_block()
without comparing it to scsi_bufflen(srb). A device that reports more
data than the command buffer holds makes the transfer run past the end
of the buffer.

Fail the command with a transport error in that case instead of reading
the response.

Haofeng Li found this first and posted a patch that clamps the length
instead (see Closes).

Found and fixed with AI assistance.

Reported-by: Haofeng Li <lihaofeng@kylinos.cn>
Closes: https://lore.kernel.org/r/20260822194728.4151923-1-lihaofeng@kylinos.cn
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org
Signed-off-by: Artem Dinaburg <artem@trailofbits.com>
---
Sent as a new patch with Alan Stern's go-ahead; Haofeng's has had no
replies since August:
https://lore.kernel.org/r/5e690176-2e2c-4166-af06-9a35ff190173@rowland.harvard.edu

Tested on mainline 19cc08c1a6a6 in QEMU (q35, KASAN) with an emulated
HP 8200e: an oversized reported length now fails the command with a
transport error and no oversized bulk-IN is issued; a well-behaved
device still answers INQUIRY, TEST UNIT READY and READ CAPACITY. Built
with W=1 on mainline and usb-next.

 drivers/usb/storage/shuttle_usbat.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/usb/storage/shuttle_usbat.c b/drivers/usb/storage/shuttle_usbat.c
index 563a2beeeb44..35129e82612c 100644
--- a/drivers/usb/storage/shuttle_usbat.c
+++ b/drivers/usb/storage/shuttle_usbat.c
@@ -1673,6 +1673,9 @@ static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us)
 		else
 			len = *status;
 
+		/* The device reports the length; don't overrun the buffer */
+		if (len > scsi_bufflen(srb))
+			return USB_STOR_TRANSPORT_ERROR;
 
 		result = usbat_read_block(us, scsi_sglist(srb), len,
 			                                   scsi_sg_count(srb));
-- 
2.55.0


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

* Re: [PATCH] usb: storage: shuttle_usbat: reject oversized device-reported read length
  2026-12-02  2:44 [PATCH] usb: storage: shuttle_usbat: reject oversized device-reported read length Artem Dinaburg
@ 2026-12-02  2:49 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-02  2:49 UTC (permalink / raw)
  To: Artem Dinaburg; +Cc: linux-usb

Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [High] usb: storage: wrong READ CD length unpacking in
  usbat_hp8200e_handle_read10() [1]
--

commit 93bfef65f8b31cbe01134b79df0424fc0e66a02f
Author: Artem Dinaburg <artem@trailofbits.com>

usb: storage: shuttle_usbat: reject oversized device-reported read length

This commit rejects device-to-host read transfers in
usbat_hp8200e_transport() when the response length reported by the device's
ATA LBA_ME and LBA_HI registers exceeds scsi_bufflen(srb).

> diff --git a/drivers/usb/storage/shuttle_usbat.c b/drivers/usb/storage/shuttle_usbat.c
> index 563a2beeeb44a..35129e82612c9 100644
> --- a/drivers/usb/storage/shuttle_usbat.c
> +++ b/drivers/usb/storage/shuttle_usbat.c
> @@ -1673,6 +1673,9 @@ static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us)
>  		else
>  			len = *status;
>  
> +		/* The device reports the length; don't overrun the buffer */
> +		if (len > scsi_bufflen(srb))
> +			return USB_STOR_TRANSPORT_ERROR;
>  
>  		result = usbat_read_block(us, scsi_sglist(srb), len,
>  			                                   scsi_sg_count(srb));

[Severity: High]
This problem wasn't introduced by this patch, but does
usbat_hp8200e_handle_read10() unpack the wrong CDB bytes when computing the
transfer length in blocks for GPCMD_READ_CD [1]?

When usbat_hp8200e_transport() handles GPCMD_READ_CD with a transfer buffer of
at least 64 KiB, usbat_hp8200e_handle_read10() computes len from data[7..18]
(which holds the 12-byte CDB):

drivers/usb/storage/shuttle_usbat.c:usbat_hp8200e_handle_read10() {
	...
	if (data[7+0] == GPCMD_READ_CD) {
		len = short_pack(data[7+9], data[7+8]);
		len <<= 16;
		len |= data[7+7];
		usb_stor_dbg(us, "GPCMD_READ_CD: len %d\n", len);
		srb->transfersize = len ? scsi_bufflen(srb) / len : 0;
	}
	...
}

For GPCMD_READ_CD, the 24-bit big-endian transfer length in blocks is in CDB
bytes 6..8 (data[7+6..7+8]), while CDB byte 9 (data[7+9]) holds the sector and
header selection flags (such as 0xf8 for 2352-byte raw CDDA frames).  Because
short_pack(LSB, MSB) puts its first argument in the low byte, this expression
evaluates to (srb->cmnd[8] << 24) | (srb->cmnd[9] << 16) | srb->cmnd[7],
ignoring srb->cmnd[6] and shifting in the flags byte.

When srb->cmnd[9] is non-zero, len is inflated so scsi_bufflen(srb) / len
becomes 0, forcing srb->transfersize to the 2048-byte fallback and
miscomputing both the per-chunk byte count and the rewritten CDB sector count.

[1] https://sashiko.dev/#/bug/linux-9aacd68e-51b6-463a-88fe-003733a49838

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261202024441.52639-1-artem@trailofbits.com?part=1

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

end of thread, other threads:[~2026-12-02  2:49 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02  2:44 [PATCH] usb: storage: shuttle_usbat: reject oversized device-reported read length Artem Dinaburg
2026-12-02  2:49 ` sashiko-bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.