Re: [usb-storage] [PATCH v2] usb-storage: reject too small values in max_sectors_store()

From: Alan Stern

Date: Mon Nov 16 2026 - 13:39:56 EST


On Tue, Nov 17, 2026 at 12:39:46AM +0800, 'YuanZhe Liu' via USB Mass Storage on Linux wrote:
> Writing a value that is smaller than a page worth of sectors, or
> smaller than one logical block, 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 at least one
> logical block, 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 such values with -EINVAL on the limits snapshot returned by
> queue_limits_start_update(), cancelling the update. This keeps the
> return value for values below a page 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. Checking the logical block size as
> well covers devices that report a block size larger than a page, where
> a value above PAGE_SECTORS can still be smaller than one logical block.
>
> Fixes: 70a7949335e5 ("scsi: usb-storage: Switch to using ->device_configure")
> Cc: stable@xxxxxxxxxxxxxxx
> Co-developed-by: Bin Yu <byu@xxxxxxxxxxxxx>
> Signed-off-by: Bin Yu <byu@xxxxxxxxxxxxx>
> Co-developed-by: Cheng Wen <wencheng@xxxxxxxxxxxxx>
> Signed-off-by: Cheng Wen <wencheng@xxxxxxxxxxxxx>
> Signed-off-by: YuanZhe Liu <25031212351@xxxxxxxxxxxxxxxxx>
> ---
> Changes in v2:
> - Also reject values below one logical block and cancel the limits
> update; with a device reporting a logical block size larger than a
> page, such a value reaches the second WARN_ON_ONCE() in
> blk_validate_limits().
> ---

Acked-by: Alan Stern <stern@xxxxxxxxxxxxxxxxxxx>

> drivers/usb/storage/scsiglue.c | 4 ++++
> 1 file changed, 4 insertions(+)
>
> diff --git a/drivers/usb/storage/scsiglue.c b/drivers/usb/storage/scsiglue.c
> index 97de28e85562..7a55cfc18236 100644
> --- a/drivers/usb/storage/scsiglue.c
> +++ b/drivers/usb/storage/scsiglue.c
> @@ -593,6 +593,10 @@ static ssize_t max_sectors_store(struct device *dev, struct device_attribute *at
> return -EINVAL;
>
> lim = queue_limits_start_update(sdev->request_queue);
> + if (ms < PAGE_SECTORS || ms < lim.logical_block_size >> SECTOR_SHIFT) {
> + queue_limits_cancel_update(sdev->request_queue);
> + return -EINVAL;
> + }
> lim.max_hw_sectors = ms;
> ret = queue_limits_commit_update_frozen(sdev->request_queue, &lim);
> if (ret)
> --
> 2.34.1