OpenVZ / Virtuozzo kernel development (devel@openvz.org)
 help / color / mirror / Atom feed
* [Devel] [PATCH hci-8.1] drivers/vhost/blk: fail guest writes to a read-only backend
@ 2026-10-28  9:35 Alexander Ivanov
  2026-10-28  9:39 ` no-reply
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Alexander Ivanov @ 2026-10-28  9:35 UTC (permalink / raw)
  To: andrey.zhadchenko; +Cc: devel

When a disk is attached read-only, QEMU opens the backing block device
O_RDONLY and advertises VIRTIO_BLK_F_RO. vhost_blk_set_backend() checks
only that the fd is a block device, and requests are submitted as bios
straight to the bdev, so a guest that ignores VIRTIO_BLK_F_RO and sends
VIRTIO_BLK_T_OUT gets VIRTIO_BLK_S_OK and modifies the disk.

Complete VIRTIO_BLK_T_OUT with VIRTIO_BLK_S_IOERR when the backend file
lacks FMODE_WRITE or the bdev itself is read-only, as the userspace
virtio-blk device does. The bdev is checked per request since it can be
switched read-only at runtime. Flush stays allowed: it does not modify
data.

Fixes: d8722ff88c5d ("drivers/vhost: vhost-blk accelerator for virtio-blk guests")
https://virtuozzo.atlassian.net/browse/VSTOR-152127
Feature: vhost-blk: in-kernel accelerator for virtio-blk guests

Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
---
 drivers/vhost/blk.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/drivers/vhost/blk.c b/drivers/vhost/blk.c
index b170439a1806..11ded3214c45 100644
--- a/drivers/vhost/blk.c
+++ b/drivers/vhost/blk.c
@@ -443,6 +443,12 @@ static int vhost_blk_req_submit(struct vhost_blk_req *req)
 	return ret;
 }
 
+static bool vhost_blk_backend_ro(struct file *f)
+{
+	return !(f->f_mode & FMODE_WRITE) ||
+	       bdev_read_only(I_BDEV(f->f_mapping->host));
+}
+
 static int vhost_blk_req_handle(struct vhost_virtqueue *vq,
 				struct virtio_blk_outhdr *hdr,
 				u16 head, u16 out, u16 in)
@@ -480,6 +486,15 @@ static int vhost_blk_req_handle(struct vhost_virtqueue *vq,
 
 	switch (hdr->type) {
 	case VIRTIO_BLK_T_OUT:
+		if (vhost_blk_backend_ro(vhost_vq_get_backend(vq))) {
+			status = VIRTIO_BLK_S_IOERR;
+			ret = vhost_blk_set_status(req, status);
+			if (ret)
+				break;
+			vhost_add_used_and_signal(&blk->dev, vq, head,
+						  sizeof(status));
+			break;
+		}
 		req->bi_opf = REQ_OP_WRITE;
 		ret = vhost_blk_req_submit(req);
 		break;
-- 
2.53.0

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

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

* Re: [Devel] [PATCH hci-8.1] drivers/vhost/blk: fail guest writes to a read-only backend
  2026-10-28  9:35 [Devel] [PATCH hci-8.1] drivers/vhost/blk: fail guest writes to a read-only backend Alexander Ivanov
@ 2026-10-28  9:39 ` no-reply
  2026-10-29 11:33 ` Andrey Zhadchenko
  2026-10-29 12:42 ` Konstantin Khorenko
  2 siblings, 0 replies; 4+ messages in thread
From: no-reply @ 2026-10-28  9:39 UTC (permalink / raw)
  To: Alexander Ivanov, andrey.zhadchenko; +Cc: devel

> When a disk is attached read-only, QEMU opens the backing block device
> O_RDONLY and advertises VIRTIO_BLK_F_RO. vhost_blk_set_backend() checks
> only that the fd is a block device, and requests are submitted as bios
> straight to the bdev, so a guest that ignores VIRTIO_BLK_F_RO and sends
> VIRTIO_BLK_T_OUT gets VIRTIO_BLK_S_OK and modifies the disk.
> 
> Complete VIRTIO_BLK_T_OUT with VIRTIO_BLK_S_IOERR when the backend file
> lacks FMODE_WRITE or the bdev itself is read-only, as the userspace
> virtio-blk device does. The bdev is checked per request since it can be
> switched read-only at runtime. Flush stays allowed: it does not modify
> data.
> 
> Fixes: d8722ff88c5d ("drivers/vhost: vhost-blk accelerator for virtio-blk guests")
> https://virtuozzo.atlassian.net/browse/VSTOR-152127
> Feature: vhost-blk: in-kernel accelerator for virtio-blk guests
> 
> Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>

Sashiko has reviewed this patch and found no issues. It looks great!

-- 
Sashiko AI review · https://sashiko.vzint.dev/#/patchset/20261028093536.3072110-1-alexander.ivanov@virtuozzo.com?part=1


_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

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

* Re: [Devel] [PATCH hci-8.1] drivers/vhost/blk: fail guest writes to a read-only backend
  2026-10-28  9:35 [Devel] [PATCH hci-8.1] drivers/vhost/blk: fail guest writes to a read-only backend Alexander Ivanov
  2026-10-28  9:39 ` no-reply
@ 2026-10-29 11:33 ` Andrey Zhadchenko
  2026-10-29 12:42 ` Konstantin Khorenko
  2 siblings, 0 replies; 4+ messages in thread
From: Andrey Zhadchenko @ 2026-10-29 11:33 UTC (permalink / raw)
  To: Alexander Ivanov; +Cc: devel

Reviewed-by: Andrey Zhadchenko <andrey.zhadchenko@virtuozzo.com>

On 10/28/26 10:35, Alexander Ivanov wrote:
> When a disk is attached read-only, QEMU opens the backing block device
> O_RDONLY and advertises VIRTIO_BLK_F_RO. vhost_blk_set_backend() checks
> only that the fd is a block device, and requests are submitted as bios
> straight to the bdev, so a guest that ignores VIRTIO_BLK_F_RO and sends
> VIRTIO_BLK_T_OUT gets VIRTIO_BLK_S_OK and modifies the disk.
> 
> Complete VIRTIO_BLK_T_OUT with VIRTIO_BLK_S_IOERR when the backend file
> lacks FMODE_WRITE or the bdev itself is read-only, as the userspace
> virtio-blk device does. The bdev is checked per request since it can be
> switched read-only at runtime. Flush stays allowed: it does not modify
> data.
> 
> Fixes: d8722ff88c5d ("drivers/vhost: vhost-blk accelerator for virtio-blk guests")
> https://virtuozzo.atlassian.net/browse/VSTOR-152127
> Feature: vhost-blk: in-kernel accelerator for virtio-blk guests
> 
> Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
> ---
>   drivers/vhost/blk.c | 15 +++++++++++++++
>   1 file changed, 15 insertions(+)
> 
> diff --git a/drivers/vhost/blk.c b/drivers/vhost/blk.c
> index b170439a1806..11ded3214c45 100644
> --- a/drivers/vhost/blk.c
> +++ b/drivers/vhost/blk.c
> @@ -443,6 +443,12 @@ static int vhost_blk_req_submit(struct vhost_blk_req *req)
>   	return ret;
>   }
>   
> +static bool vhost_blk_backend_ro(struct file *f)
> +{
> +	return !(f->f_mode & FMODE_WRITE) ||
> +	       bdev_read_only(I_BDEV(f->f_mapping->host));
> +}
> +
>   static int vhost_blk_req_handle(struct vhost_virtqueue *vq,
>   				struct virtio_blk_outhdr *hdr,
>   				u16 head, u16 out, u16 in)
> @@ -480,6 +486,15 @@ static int vhost_blk_req_handle(struct vhost_virtqueue *vq,
>   
>   	switch (hdr->type) {
>   	case VIRTIO_BLK_T_OUT:
> +		if (vhost_blk_backend_ro(vhost_vq_get_backend(vq))) {
> +			status = VIRTIO_BLK_S_IOERR;
> +			ret = vhost_blk_set_status(req, status);
> +			if (ret)
> +				break;
> +			vhost_add_used_and_signal(&blk->dev, vq, head,
> +						  sizeof(status));
> +			break;
> +		}
>   		req->bi_opf = REQ_OP_WRITE;
>   		ret = vhost_blk_req_submit(req);
>   		break;

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

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

* Re: [Devel] [PATCH hci-8.1] drivers/vhost/blk: fail guest writes to a read-only backend
  2026-10-28  9:35 [Devel] [PATCH hci-8.1] drivers/vhost/blk: fail guest writes to a read-only backend Alexander Ivanov
  2026-10-28  9:39 ` no-reply
  2026-10-29 11:33 ` Andrey Zhadchenko
@ 2026-10-29 12:42 ` Konstantin Khorenko
  2 siblings, 0 replies; 4+ messages in thread
From: Konstantin Khorenko @ 2026-10-29 12:42 UTC (permalink / raw)
  To: andrey.zhadchenko, Alexander Ivanov; +Cc: devel

On Wed, 28 Oct 2026 10:35:36 +0100, Alexander Ivanov wrote:
> drivers/vhost/blk: fail guest writes to a read-only backend

Applied to branch-rh10-6.12.0-211.52.1.22.x.vz10-ovz, will be released in rh10-6.12.0-211.52.1.22.12.vz10, thanks!

[1/1] drivers/vhost/blk: fail guest writes to a read-only backend
      https://bitbucket.org/virtuozzocore/vzkernel.vzs/commits/8445ee9f5e09

-- 
Konstantin Khorenko,
Virtuozzo Linux Kernel Team


_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

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

end of thread, other threads:[~2026-10-29 12:43 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-28  9:35 [Devel] [PATCH hci-8.1] drivers/vhost/blk: fail guest writes to a read-only backend Alexander Ivanov
2026-10-28  9:39 ` no-reply
2026-10-29 11:33 ` Andrey Zhadchenko
2026-10-29 12:42 ` Konstantin Khorenko

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