Skip to contentSkip to conversation

virtio_blk: validate zone append sector from device

PATCH Superseded Annie Kim 3 participants 3 messages Nov 20

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Annie Kim

author
#0 lore raw

PATCH 1/1 virtio_blk: validate zone append sector from device

From: Hyokyung Kim <[email protected]>

The device reports the starting sector for REQ_OP_ZONE_APPEND completions, and virtio_blk copies it into req->__sector without checking that it still refers to the requested zone. A buggy or malicious device can therefore make upper layers believe the append landed at an arbitrary location.

Validate successful zone append completions before updating req->__sector. The returned sector must not precede the request start and the entire request must fit within the zone writable capacity. Reject invalid completions with BLK_STS_IOERR.

The polling path may batch successful completions, so perform the same validation before admitting a request to the batch. Invalid append completions then fall back to the normal completion path and are completed with an error instead of being reported as successful.

Fixes 95bfec41bd3d ("virtio-blk: add support for zoned block devices")
Assisted-by Codex:GPT-5

---
Tested on v7.4-rc3+ using modified QEMU backend that returned incorrect zone-append sectors on a memory-backed null_blk zoned device.

 drivers/block/virtio_blk.c | 50 +++++++++++++++++++++++++++++++-------
 1 file changed, 41 insertions(+), 9 deletions(-)
drivers/block/virtio_blk.c +41 -9
index 2b462005c..8f88570b4 100644
@@ -331,15 +331,45 @@ static inline u8 virtblk_vbr_status(struct virtblk_req *vbr)
 	return *((u8 *)&vbr->in_hdr + vbr->in_hdr_len - 1);
 }
 
-static inline void virtblk_update_zone_append_sector(struct request *req,
-						     struct virtblk_req *vbr)
+#ifdef CONFIG_BLK_DEV_ZONED
+static inline blk_status_t
+virtblk_update_zone_append_sector(struct request *req,
+				  struct virtblk_req *vbr,
+				  blk_status_t status)
 {
 	struct virtio_blk *vblk = req->mq_hctx->queue->queuedata;
+	sector_t req_start, sector, zone_end;
+	unsigned int nr_sectors;
+
+	if (req_op(req) != REQ_OP_ZONE_APPEND || status != BLK_STS_OK)
+		return status;
+
+	req_start = blk_rq_pos(req);
+	sector = virtio64_to_cpu(vblk->vdev, vbr->in_hdr.zone_append.sector);
+	nr_sectors = blk_rq_sectors(req);
+	zone_end = req_start + disk_zone_capacity(vblk->disk, req_start);
+
+	if (zone_end < req_start || sector < req_start || sector >= zone_end ||
+	    nr_sectors > zone_end - sector) {
+		dev_err_ratelimited(&vblk->vdev->dev,
+				    "zone append sector %llu + %u outside valid range [%llu, %llu)\n",
+				    (u64)sector, nr_sectors,
+				    (u64)req_start, (u64)zone_end);
+		return BLK_STS_IOERR;
+	}
 
-	if (req_op(req) == REQ_OP_ZONE_APPEND)
-		req->__sector = virtio64_to_cpu(vblk->vdev,
-						vbr->in_hdr.zone_append.sector);
+	req->__sector = sector;
+	return status;
 }
+#else
+static inline blk_status_t
+virtblk_update_zone_append_sector(struct request *req,
+				  struct virtblk_req *vbr,
+				  blk_status_t status)
+{
+	return status;
+}
+#endif
 
 static inline void virtblk_request_done(struct request *req)
 {
@@ -349,7 +379,7 @@ static inline void virtblk_request_done(struct request *req)
 	virtblk_unmap_data(req, vbr);
 	virtblk_cleanup_cmd(req);
 
-	virtblk_update_zone_append_sector(req, vbr);
+	status = virtblk_update_zone_append_sector(req, vbr, status);
 
 	blk_mq_end_request(req, status);
 }
@@ -1242,7 +1272,6 @@ static void virtblk_complete_batch(struct io_comp_batch *iob)
 
 		virtblk_unmap_data(req, vbr);
 		virtblk_cleanup_cmd(req);
-		virtblk_update_zone_append_sector(req, vbr);
 	}
 	blk_mq_end_request_batch(iob);
 }
@@ -1260,11 +1289,14 @@ static int virtblk_poll(struct blk_mq_hw_ctx *hctx, struct io_comp_batch *iob)
 
 	while ((vbr = virtqueue_get_buf(vq->vq, &len)) != NULL) {
 		struct request *req = blk_mq_rq_from_pdu(vbr);
-		u8 status = virtblk_vbr_status(vbr);
+		blk_status_t status;
+
+		status = virtblk_result(virtblk_vbr_status(vbr));
+		status = virtblk_update_zone_append_sector(req, vbr, status);
 
 		found++;
 		if (!blk_mq_complete_request_remote(req) &&
-		    !blk_mq_add_to_batch(req, iob, status != VIRTIO_BLK_S_OK,
+		    !blk_mq_add_to_batch(req, iob, status != BLK_STS_OK,
 					 virtblk_complete_batch))
 			virtblk_request_done(req);
 	}
Signature · 1 line
2.53.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] virtio_blk: double virtblk_update_zone_append_sector() call in virtblk_poll() It also noticed 1…

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] virtio_blk: double virtblk_update_zone_append_sector() call in virtblk_poll()
It also noticed 1 related pre-existing issue this patch does not introduce:
- [Critical] virtio-blk: null pointer dereference in virtblk_poll() [1] --

commit 9e4aa32cc96237b908b67c8c0ca12ebb3020f3a1
Author: Hyokyung Kim <[email protected]>

virtio_blk: validate zone append sector from device

This commit validates the device-reported starting sector on REQ_OP_ZONE_APPEND completions in both the normal and polled completion paths before updating req->__sector, failing out-of-range completions with BLK_STS_IOERR.

Quoted 64 lines
drivers/block/virtio_blk.c +36 -6
index 2b462005cdb68..8f88570b4f0e5 100644
@@ -331,15 +331,45 @@ static inline u8 virtblk_vbr_status(struct virtblk_req *vbr)
 	return *((u8 *)&vbr->in_hdr + vbr->in_hdr_len - 1);
 }
 
-static inline void virtblk_update_zone_append_sector(struct request *req,
-						     struct virtblk_req *vbr)
+#ifdef CONFIG_BLK_DEV_ZONED
+static inline blk_status_t
+virtblk_update_zone_append_sector(struct request *req,
+				  struct virtblk_req *vbr,
+				  blk_status_t status)
 {
 	struct virtio_blk *vblk = req->mq_hctx->queue->queuedata;
+	sector_t req_start, sector, zone_end;
+	unsigned int nr_sectors;
+
+	if (req_op(req) != REQ_OP_ZONE_APPEND || status != BLK_STS_OK)
+		return status;
+
+	req_start = blk_rq_pos(req);
+	sector = virtio64_to_cpu(vblk->vdev, vbr->in_hdr.zone_append.sector);
+	nr_sectors = blk_rq_sectors(req);
+	zone_end = req_start + disk_zone_capacity(vblk->disk, req_start);
+
+	if (zone_end < req_start || sector < req_start || sector >= zone_end ||
+	    nr_sectors > zone_end - sector) {
+		dev_err_ratelimited(&vblk->vdev->dev,
+				    "zone append sector %llu + %u outside valid range [%llu, %llu)\n",
+				    (u64)sector, nr_sectors,
+				    (u64)req_start, (u64)zone_end);
+		return BLK_STS_IOERR;
+	}
 
-	if (req_op(req) == REQ_OP_ZONE_APPEND)
-		req->__sector = virtio64_to_cpu(vblk->vdev,
-						vbr->in_hdr.zone_append.sector);
+	req->__sector = sector;
+	return status;
 }
+#else
+static inline blk_status_t
+virtblk_update_zone_append_sector(struct request *req,
+				  struct virtblk_req *vbr,
+				  blk_status_t status)
+{
+	return status;
+}
+#endif
 
 static inline void virtblk_request_done(struct request *req)
 {
@@ -349,7 +379,7 @@ static inline void virtblk_request_done(struct request *req)
 	virtblk_unmap_data(req, vbr);
 	virtblk_cleanup_cmd(req);
 
-	virtblk_update_zone_append_sector(req, vbr);
+	status = virtblk_update_zone_append_sector(req, vbr, status);
 
 	blk_mq_end_request(req, status);
 }

[ ... ]

Quoted 7 lines
drivers/block/virtio_blk.c +2 -1
@@ -1260,11 +1289,14 @@ static int virtblk_poll(struct blk_mq_hw_ctx *hctx, struct io_comp_batch *iob)
 
 	while ((vbr = virtqueue_get_buf(vq->vq, &len)) != NULL) {
 		struct request *req = blk_mq_rq_from_pdu(vbr);
-		u8 status = virtblk_vbr_status(vbr);
+		blk_status_t status;
+

drivers/block/virtio_blk.c static int virtblk_poll(struct blk_mq_hw_ctx *hctx, struct io_comp_batch *iob) · line 1302

Quoted 10 lines
drivers/block/virtio_blk.c +3 -1
+		status = virtblk_result(virtblk_vbr_status(vbr));
+		status = virtblk_update_zone_append_sector(req, vbr, status);
 
 		found++;
 		if (!blk_mq_complete_request_remote(req) &&
-		    !blk_mq_add_to_batch(req, iob, status != VIRTIO_BLK_S_OK,
+		    !blk_mq_add_to_batch(req, iob, status != BLK_STS_OK,
 					 virtblk_complete_batch))
 			virtblk_request_done(req);
 	}

[Severity: Medium]
Does calling virtblk_update_zone_append_sector() in virtblk_poll() before falling back to virtblk_request_done() run the validation and update a second time after req->__sector has already been overwritten?

When a polled REQ_OP_ZONE_APPEND request is not batched (for example when an I/O scheduler is attached so RQF_SCHED_TAGS is set, when req->end_io is set, or when validation itself returns BLK_STS_IOERR), virtblk_poll() calls virtblk_request_done(), which recomputes status from vbr and calls virtblk_update_zone_append_sector() again with req_start = blk_rq_pos(req) now equal to the updated mid-zone sector rather than the zone start:

include/linux/blkdev.h:disk_zone_capacity() {
	sector_t zone_sectors = disk->queue->limits.chunk_sectors;

	if (sector + zone_sectors >= get_capacity(disk))
		return disk->last_zone_capacity;
	return disk->zone_capacity;
}

For a valid append in the second-to-last zone landing past the start of that zone on a disk with a smaller last zone, sector + zone_sectors >= get_capacity(disk) is true on the second call, so disk_zone_capacity() returns last_zone_capacity and falsely fails the request with BLK_STS_IOERR when nr_sectors > last_zone_capacity. Even when all zones have equal capacity, the second call shifts zone_end to sector + zone_capacity and re-reads vbr->in_hdr from device memory, and any invalid polled append logs dev_err_ratelimited() twice.

[Severity: Critical]
This problem wasn't introduced by this patch, but can virtblk_poll() hit a NULL pointer dereference [1] on vq->lock if polled I/O runs after init_vq() fails during device resume?

When virtio_find_vqs() fails in init_vq() (for instance if the hypervisor reduces the ring size on resume), init_vq() frees vblk->vqs and sets it to NULL while leaving the request queue quiesced but not frozen. Because blk_mq_poll() does not check whether the queue is quiesced, a userspace polled read (such as preadv2() with RWF_HIPRI) calls virtblk_poll(), where get_virtio_blk_vq() indexes the NULL vblk->vqs pointer and spin_lock_irqsave(&vq->lock, flags) dereferences it.

[1] https://sashiko.dev/#/bug/linux-c7b06d8d-b2d5-4a94-acf6-9b1cf7480add

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Annie Kim
Quoted 13 lines · Annie Kim

From: Hyokyung Kim <[email protected]>

The device reports the starting sector for REQ_OP_ZONE_APPEND completions, and virtio_blk copies it into req->__sector without checking that it still refers to the requested zone. A buggy or malicious device can therefore make upper layers believe the append landed at an arbitrary location.

Validate successful zone append completions before updating req->__sector. The returned sector must not precede the request start and the entire request must fit within the zone writable capacity. Reject invalid completions with BLK_STS_IOERR.

Quoted 7 lines

The polling path may batch successful completions, so perform the same validation before admitting a request to the batch. Invalid append completions then fall back to the normal completion path and are completed with an error instead of being reported as successful.

Fixes 95bfec41bd3d ("virtio-blk: add support for zoned block devices")
Assisted-by Codex:GPT-5

This tag is not necessary.

Quoted 20 lines

---
Tested on v7.4-rc3+ using modified QEMU backend that returned incorrect zone-append sectors on a memory-backed null_blk zoned device.

 drivers/block/virtio_blk.c | 50 +++++++++++++++++++++++++++++++-------
 1 file changed, 41 insertions(+), 9 deletions(-)
drivers/block/virtio_blk.c +2 -2
index 2b462005c..8f88570b4 100644
@@ -331,15 +331,45 @@ static inline u8 virtblk_vbr_status(struct virtblk_req *vbr)
 	return *((u8 *)&vbr->in_hdr + vbr->in_hdr_len - 1);
 }
 
-static inline void virtblk_update_zone_append_sector(struct request *req,
-						     struct virtblk_req *vbr)
+#ifdef CONFIG_BLK_DEV_ZONED
+static inline blk_status_t

drivers/block/virtio_blk.c static inline u8 virtblk_vbr_status(struct virtblk_req *vbr) · line 345

Quoted 10 lines
drivers/block/virtio_blk.c +8 -0
+virtblk_update_zone_append_sector(struct request *req,
+				  struct virtblk_req *vbr,
+				  blk_status_t status)
 {
 	struct virtio_blk *vblk = req->mq_hctx->queue->queuedata;
+	sector_t req_start, sector, zone_end;
+	unsigned int nr_sectors;
+
+	if (req_op(req) != REQ_OP_ZONE_APPEND || status != BLK_STS_OK)
+		return status;

I would move this if outside of the function and call this function under this if. That would avoid passing status, make this function a void one, and avoid unnecessary function calls for commands other than zone append when the inline is not honored by the compiler.

drivers/block/virtio_blk.c static inline u8 virtblk_vbr_status(struct virtblk_req *vbr) · line 349

Quoted 4 lines
drivers/block/virtio_blk.c +4 -0
+
+	req_start = blk_rq_pos(req);
+	sector = virtio64_to_cpu(vblk->vdev, vbr->in_hdr.zone_append.sector);
+	nr_sectors = blk_rq_sectors(req);

req_start and nr_sectors could be initialized when declared.

Other than this, this looks fine to me.

Quoted 63 lines
drivers/block/virtio_blk.c +27 -7
+	zone_end = req_start + disk_zone_capacity(vblk->disk, req_start);
+
+	if (zone_end < req_start || sector < req_start || sector >= zone_end ||
+	    nr_sectors > zone_end - sector) {
+		dev_err_ratelimited(&vblk->vdev->dev,
+				    "zone append sector %llu + %u outside valid range [%llu, %llu)\n",
+				    (u64)sector, nr_sectors,
+				    (u64)req_start, (u64)zone_end);
+		return BLK_STS_IOERR;
+	}
 
-	if (req_op(req) == REQ_OP_ZONE_APPEND)
-		req->__sector = virtio64_to_cpu(vblk->vdev,
-						vbr->in_hdr.zone_append.sector);
+	req->__sector = sector;
+	return status;
 }
+#else
+static inline blk_status_t
+virtblk_update_zone_append_sector(struct request *req,
+				  struct virtblk_req *vbr,
+				  blk_status_t status)
+{
+	return status;
+}
+#endif
 
 static inline void virtblk_request_done(struct request *req)
 {
@@ -349,7 +379,7 @@ static inline void virtblk_request_done(struct request *req)
 	virtblk_unmap_data(req, vbr);
 	virtblk_cleanup_cmd(req);
 
-	virtblk_update_zone_append_sector(req, vbr);
+	status = virtblk_update_zone_append_sector(req, vbr, status);
 
 	blk_mq_end_request(req, status);
 }
@@ -1242,7 +1272,6 @@ static void virtblk_complete_batch(struct io_comp_batch *iob)
 
 		virtblk_unmap_data(req, vbr);
 		virtblk_cleanup_cmd(req);
-		virtblk_update_zone_append_sector(req, vbr);
 	}
 	blk_mq_end_request_batch(iob);
 }
@@ -1260,11 +1289,14 @@ static int virtblk_poll(struct blk_mq_hw_ctx *hctx, struct io_comp_batch *iob)
 
 	while ((vbr = virtqueue_get_buf(vq->vq, &len)) != NULL) {
 		struct request *req = blk_mq_rq_from_pdu(vbr);
-		u8 status = virtblk_vbr_status(vbr);
+		blk_status_t status;
+
+		status = virtblk_result(virtblk_vbr_status(vbr));
+		status = virtblk_update_zone_append_sector(req, vbr, status);
 
 		found++;
 		if (!blk_mq_complete_request_remote(req) &&
-		    !blk_mq_add_to_batch(req, iob, status != VIRTIO_BLK_S_OK,
+		    !blk_mq_add_to_batch(req, iob, status != BLK_STS_OK,
 					 virtblk_complete_batch))
 			virtblk_request_done(req);
 	}
Signature · 2 lines
Damien Le Moal
Western Digital Research