On 2026-10-28T05:50:48, Ana Bobillo <[email protected]> wrote:
> block: Add blk_valid_blksz() to check a block size
>
> A block driver sets blksz and log2blksz from what the device reports: the
> READ CAPACITY reply of a USB stick or a SCSI disk, the LBA format of an
> NVMe namespace, the sector size of a Xen backend. Apart from the virtio
> block driver and the host command, none of these drivers checks the
> value, yet the partition and filesystem code relies on it:
>
> - part_get_info_extended() and print_partition_extended() read one block
>   into a buffer of blksz bytes and parse the partition table at offset
>   0x1be, past the end of a block smaller than 512 bytes
> - fs_devread() converts bytes to blocks with shifts by log2blksz, masks
>   by blksz - 1 and divisions by blksz, which only agree when blksz is a
>   power of two; with any other value it reads more bytes into the
>   caller's buffer than the caller asked for
> - buffers of blksz bytes go on the stack, so a block size in the
>   gigabytes overruns it
>
> Add blk_valid_blksz(), which accepts a power of two from 512 bytes up to
> BLK_MAX_BLKSZ (64KiB, the largest logical block size Linux accepts), so
> that the block drivers can check what their devices report. Use it in
> the virtio block driver and the host command, which then also refuse a
> block size above 64KiB.
>
> The next patches check the block size in the USB storage, SCSI, IDE and
> NVMe drivers, and the last one in blk_post_probe() for every block
> device.
>
> Signed-off-by: Ana Bobillo <[email protected]>
>
>  cmd/host.c                  |  5 +++--
>  drivers/virtio/virtio_blk.c |  2 +-
>  include/blk.h               | 21 +++++++++++++++++++++
>  3 files changed, 25 insertions(+), 3 deletions(-)

Reviewed-by: Simon Glass <[email protected]>

Reply via email to