Linux USB
 help / color / mirror / Atom feed
* [PATCH] usb: storage: shuttle_usbat: do not divide by an unknown sector size
@ 2026-11-16  9:34 YuanZhe Liu
  2026-11-16  9:39 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: YuanZhe Liu @ 2026-11-16  9:34 UTC (permalink / raw)
  To: Alan Stern, Greg Kroah-Hartman
  Cc: linux-usb, usb-storage, linux-kernel, stable, Bin Yu, Cheng Wen

usbat_flash_read_data() and usbat_flash_write_data() divide the transfer
length by info->ssize to turn it into an ATA sector count.  info->ssize is
only assigned when a READ CAPACITY command has completed successfully, so
it is still zero when a read request arrives earlier.

Such a request is sent during device scan: sd_probe() starts with
media_present set and usb-storage sets read_before_ms for every device, so
sd_revalidate_disk() calls sd_read_block_zero() right after
sd_read_capacity(), even when READ CAPACITY failed, because
read_capacity_error() deliberately keeps media_present set for drives that
fail it with media loaded.  A CompactFlash reader that does not complete
the READ CAPACITY sequence, for example because a register access is
stalled, therefore reaches usbat_flash_read_data() with info->ssize == 0:

  Oops: divide error
  RIP: 0010:usbat_flash_read_data+0x2a4/0x5c0

Reject read and write requests while the sector size is unknown, the same
way the driver already rejects an out of range LBA.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org
Co-developed-by: Bin Yu <byu@xidian.edu.cn>
Signed-off-by: Bin Yu <byu@xidian.edu.cn>
Co-developed-by: Cheng Wen <wencheng@xidian.edu.cn>
Signed-off-by: Cheng Wen <wencheng@xidian.edu.cn>
Signed-off-by: YuanZhe Liu <25031212351@stu.xidian.edu.cn>
---
 drivers/usb/storage/shuttle_usbat.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/drivers/usb/storage/shuttle_usbat.c b/drivers/usb/storage/shuttle_usbat.c
index 7e5424268c73..faea2c77c5aa 100644
--- a/drivers/usb/storage/shuttle_usbat.c
+++ b/drivers/usb/storage/shuttle_usbat.c
@@ -1130,6 +1130,13 @@ static int usbat_flash_read_data(struct us_data *us,
 	if (result != USB_STOR_TRANSPORT_GOOD)
 		return result;
 
+	/*
+	 * The sector size is only known once a READ CAPACITY command has
+	 * succeeded; a request can arrive before that.
+	 */
+	if (!info->ssize)
+		return USB_STOR_TRANSPORT_ERROR;
+
 	/*
 	 * we're working in LBA mode.  according to the ATA spec,
 	 * we can support up to 28-bit addressing.  I don't know if Jumpshot
@@ -1221,6 +1228,13 @@ static int usbat_flash_write_data(struct us_data *us,
 	if (result != USB_STOR_TRANSPORT_GOOD)
 		return result;
 
+	/*
+	 * The sector size is only known once a READ CAPACITY command has
+	 * succeeded; a request can arrive before that.
+	 */
+	if (!info->ssize)
+		return USB_STOR_TRANSPORT_ERROR;
+
 	/*
 	 * we're working in LBA mode.  according to the ATA spec,
 	 * we can support up to 28-bit addressing.  I don't know if the device
-- 
2.34.1


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

* Re: [PATCH] usb: storage: shuttle_usbat: do not divide by an unknown sector size
  2026-11-16  9:34 [PATCH] usb: storage: shuttle_usbat: do not divide by an unknown sector size YuanZhe Liu
@ 2026-11-16  9:39 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-16  9:39 UTC (permalink / raw)
  To: YuanZhe Liu; +Cc: linux-usb

> usbat_flash_read_data() and usbat_flash_write_data() divide the transfer
> length by info->ssize to turn it into an ATA sector count.  info->ssize is
> only assigned when a READ CAPACITY command has completed successfully, so
> it is still zero when a read request arrives earlier.
> 
> Such a request is sent during device scan: sd_probe() starts with
> media_present set and usb-storage sets read_before_ms for every device, so
> sd_revalidate_disk() calls sd_read_block_zero() right after
> sd_read_capacity(), even when READ CAPACITY failed, because
> read_capacity_error() deliberately keeps media_present set for drives that
> fail it with media loaded.  A CompactFlash reader that does not complete
> the READ CAPACITY sequence, for example because a register access is
> stalled, therefore reaches usbat_flash_read_data() with info->ssize == 0:
> 
>   Oops: divide error
>   RIP: 0010:usbat_flash_read_data+0x2a4/0x5c0
> 
> Reject read and write requests while the sector size is unknown, the same
> way the driver already rejects an out of range LBA.
> 
> Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> Cc: stable@vger.kernel.org
> Co-developed-by: Bin Yu <byu@xidian.edu.cn>
> Signed-off-by: Bin Yu <byu@xidian.edu.cn>
> Co-developed-by: Cheng Wen <wencheng@xidian.edu.cn>
> Signed-off-by: Cheng Wen <wencheng@xidian.edu.cn>
> Signed-off-by: YuanZhe Liu <25031212351@stu.xidian.edu.cn>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261116093452.15286-1-25031212351@stu.xidian.edu.cn?part=1


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

end of thread, other threads:[~2026-11-16  9:39 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-16  9:34 [PATCH] usb: storage: shuttle_usbat: do not divide by an unknown sector size YuanZhe Liu
2026-11-16  9:39 ` sashiko-bot

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