Linux USB
 help / color / mirror / Atom feed
* [PATCH] usb-storage: reject too small values in max_sectors_store()
@ 2026-11-16 10:16 YuanZhe Liu
  2026-11-16 10:29 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: YuanZhe Liu @ 2026-11-16 10:16 UTC (permalink / raw)
  To: Alan Stern
  Cc: Greg Kroah-Hartman, linux-usb, usb-storage, linux-kernel, stable,
	Bin Yu, Cheng Wen

Writing a value smaller than a page worth of sectors to
/sys/class/scsi_device/.../device/max_sectors passes an invalid
max_hw_sectors to queue_limits_commit_update_frozen().  The block layer
requires max_hw_sectors to cover at least one page and treats a smaller
value as a driver bug:

 WARNING: block/blk-settings.c:389 at blk_validate_limits+0xeb9/0x14b0
 Call Trace:
  queue_limits_commit_update
  queue_limits_commit_update_frozen
  max_sectors_store
  dev_attr_store
  sysfs_kf_write

Before commit 70a7949335e5 ("scsi: usb-storage: Switch to using
->device_configure") the store called blk_queue_max_hw_sectors(), which
silently raised values below PAGE_SECTORS to that minimum.  The
conversion to the queue_limits API lost the clamping, so a single sysfs
write from user space can now trigger the warning.

Reject values below PAGE_SECTORS with -EINVAL before taking the limits
lock.  This keeps the return value for 1..7 unchanged, as the block
layer already returned -EINVAL for them, and also rejects zero, which
blk_validate_limits() would otherwise replace with BLK_SAFE_MAX_SECTORS
without the user asking for it.

Fixes: 70a7949335e5 ("scsi: usb-storage: Switch to using ->device_configure")
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/scsiglue.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/usb/storage/scsiglue.c b/drivers/usb/storage/scsiglue.c
index 97de28e85562..94131b726ade 100644
--- a/drivers/usb/storage/scsiglue.c
+++ b/drivers/usb/storage/scsiglue.c
@@ -591,6 +591,8 @@ static ssize_t max_sectors_store(struct device *dev, struct device_attribute *at
 
 	if (sscanf(buf, "%hu", &ms) <= 0)
 		return -EINVAL;
+	if (ms < PAGE_SECTORS)
+		return -EINVAL;
 
 	lim = queue_limits_start_update(sdev->request_queue);
 	lim.max_hw_sectors = ms;
-- 
2.34.1


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

* Re: [PATCH] usb-storage: reject too small values in max_sectors_store()
  2026-11-16 10:16 [PATCH] usb-storage: reject too small values in max_sectors_store() YuanZhe Liu
@ 2026-11-16 10:29 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-16 10:29 UTC (permalink / raw)
  To: YuanZhe Liu; +Cc: linux-usb

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] usb-storage: missing logical_block_size check in
  max_sectors_store()
--

commit 05440cadc022e6dcf9589e956d95f83a1aa8ca56
Author: YuanZhe Liu <25031212351@stu.xidian.edu.cn>

usb-storage: reject too small values in max_sectors_store()

This commit rejects values below the page sector count in
max_sectors_store() before updating the request queue's max_hw_sectors
limit.

> diff --git a/drivers/usb/storage/scsiglue.c b/drivers/usb/storage/scsiglue.c
> index 97de28e855626..94131b726ade3 100644
> --- a/drivers/usb/storage/scsiglue.c
> +++ b/drivers/usb/storage/scsiglue.c
> @@ -591,6 +591,8 @@ static ssize_t max_sectors_store(struct device *dev, struct device_attribute *at
>  
>  	if (sscanf(buf, "%hu", &ms) <= 0)
>  		return -EINVAL;
> +	if (ms < PAGE_SECTORS)
> +		return -EINVAL;
>  
>  	lim = queue_limits_start_update(sdev->request_queue);
>  	lim.max_hw_sectors = ms;

[Severity: Medium]
Can max_sectors_store() still trigger a warning in blk_validate_limits()
when the device's logical_block_size is larger than a page?

block/blk-settings.c:blk_validate_limits() {
    ...
	if (WARN_ON_ONCE(lim->max_hw_sectors < PAGE_SECTORS))
		return -EINVAL;
	logical_block_sectors = lim->logical_block_size >> SECTOR_SHIFT;
	if (WARN_ON_ONCE(logical_block_sectors > lim->max_hw_sectors))
		return -EINVAL;
    ...
}

When transparent hugepages are enabled, sd_read_capacity() accepts
sector_size values up to 64 KiB via blk_validate_block_size().  On a 4 KiB
page system with a device reporting an 8 KiB to 64 KiB logical block size,
writing 8 to max_sectors passes the check added here and hits the second
warning in blk_validate_limits().

Checking that ms is at least lim.logical_block_size >> 9 after
queue_limits_start_update() (and calling queue_limits_cancel_update() on
failure) would avoid this warning as well.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261116101604.42161-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 10:29 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-16 10:16 [PATCH] usb-storage: reject too small values in max_sectors_store() YuanZhe Liu
2026-11-16 10:29 ` sashiko-bot

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