Skip to content

Commit 2202b7f

Browse files
peterfangNanlinXie
authored andcommitted
dm: virtio: reject requests that violate the virtio-block spec
VirtIO v1.0 spec 04 5.2.5: - Protocol unit size is always 512 bytes. - blk_size (logical block size) and physical_block_exp (physical block size) do not affect the units in the protocol, only performance. VirtIO v1.0 spec 04 5.2.6.1: - A driver MUST NOT submit a request which would cause a read or write beyond capacity. Reject the requests that violate these terms. v1 -> v2: - add more comments for clarity Tracked-On: #1422 Signed-off-by: Peter Fang <peter.fang@intel.com> Acked-by: Yu Wang <yu1.wang@intel.com>
1 parent ba4e72b commit 2202b7f

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

devicemodel/hw/pci/virtio/virtio_block.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,29 @@ virtio_blk_proc(struct virtio_blk *blk, struct virtio_vq_info *vq)
254254

255255
switch (type) {
256256
case VBH_OP_READ:
257-
err = blockif_read(blk->bc, &io->req);
258-
break;
259257
case VBH_OP_WRITE:
260-
err = blockif_write(blk->bc, &io->req);
258+
/*
259+
* VirtIO v1.0 spec 04 5.2.5:
260+
* - Protocol unit size is always 512 bytes.
261+
* - blk_size (logical block size) and physical_block_exp
262+
* (physical block size) do not affect the units in the
263+
* protocol, only performance.
264+
*
265+
* VirtIO v1.0 spec 04 5.2.6.1:
266+
* - A driver MUST NOT submit a request which would cause a
267+
* read or write beyond capacity.
268+
*/
269+
if ((iolen & (DEV_BSIZE - 1)) ||
270+
vbh->sector + iolen / DEV_BSIZE > blk->cfg.capacity) {
271+
DPRINTF(("virtio_blk: invalid request, iolen = %ld, "
272+
"sector = %lu, capacity = %lu\n\r", iolen,
273+
vbh->sector, blk->cfg.capacity));
274+
virtio_blk_done(&io->req, EINVAL);
275+
return;
276+
}
277+
278+
err = ((type == VBH_OP_READ) ? blockif_read : blockif_write)
279+
(blk->bc, &io->req);
261280
break;
262281
case VBH_OP_FLUSH:
263282
case VBH_OP_FLUSH_OUT:

0 commit comments

Comments
 (0)