When checking a zoned request, we enforce that (offset + length) is
not greater than the total block device size. This is the correct
check for any request that starts inside the block device, but it
accidentally allows through the corner case where the offset is equal
to the block device size and the length is zero. This can then fall
through into the bdrv_zone_index() call, which returns an index
larger than the maximum valid index and so the bdrv_zone_is_conv()
call reads off the end of the bs->wps->wp[] array.
Check for offsets off the end of the block and fail those with a
ZONE_INVALID_CMD error.
Since the incorrect zone index is used only for a read, the failure
mode seems likely to only be at worst a self-DoS due to a segfault,
so this is a hardening patch.
Cc: qemu-stable@nongnu.org
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3864
Fixes: 4f7366506a9 ("virtio-blk: add zoned storage emulation for zoned devices")
Reported-by: Feifan Qian <bea1e@proton.me>
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
hw/block/virtio-blk.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/hw/block/virtio-blk.c b/hw/block/virtio-blk.c
index d9c3db3e56f..2255a76de96 100644
--- a/hw/block/virtio-blk.c
+++ b/hw/block/virtio-blk.c
@@ -569,7 +569,8 @@ static bool check_zoned_request(VirtIOBlock *s, int64_t offset, int64_t len,
}
if (offset < 0 || len < 0 || len > (bs->total_sectors << BDRV_SECTOR_BITS)
- || offset > (bs->total_sectors << BDRV_SECTOR_BITS) - len) {
+ || offset > (bs->total_sectors << BDRV_SECTOR_BITS) - len
+ || offset >= (bs->total_sectors << BDRV_SECTOR_BITS)) {
*status = VIRTIO_BLK_S_ZONE_INVALID_CMD;
return false;
}
--
2.43.0