virtio_blk: validate zone append sector from device
Subsystems (3)
PATCH v2 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, and a batch can only be completed as successful. Perform the same validation in virtblk_poll() before admitting a request to the batch, so that invalid zone append completions are not batched and are completed with an error instead. Move the request completion into __virtblk_request_done(), which takes the status from its caller, so that a polled completion that is not batched is not validated a second time.
---
Damien, thank you so much for the thoughtful review of v1. As a beginner contributor I learned a lot from your comments :).
Changes in v2:
- Check the request op and status in the callers and drop the status argument of virtblk_update_zone_append_sector() (Damien). The function still returns blk_status_t, since the callers need to fail the request when the reported sector is invalid.
- Initialize req_start and nr_sectors at declaration (Damien)
- Drop the Assisted-by tag (Damien)
- Fix the sector being validated twice for polled requests that were not batched (Sashiko). virtblk_poll() now completes those requests through __virtblk_request_done(), which does not validate again.
- Link to v1: https://lore.kernel.org/lkml/[email protected]/
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 | 63 +++++++++++++++++++++++++++++--------- 1 file changed, 49 insertions(+), 14 deletions(-)
drivers/block/virtio_blk.c +49 -14
@@ -331,29 +331,60 @@ 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) {struct virtio_blk *vblk = req->mq_hctx->queue->queuedata; +sector_t req_start =blk_rq_pos (req); +unsigned int nr_sectors =blk_rq_sectors (req); +sector_t sector, zone_end; + + sector =virtio64_to_cpu (vblk->vdev, vbr->in_hdr.zone_append.sector); + 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 BLK_STS_OK ; +} +#else +static inline blk_status_t +virtblk_update_zone_append_sector (struct request *req,struct virtblk_req *vbr) +{ +return BLK_STS_OK ; } +#endif -static inline void virtblk_request_done (struct request *req) +static inline void __virtblk_request_done (struct request *req, +blk_status_t status) {struct virtblk_req *vbr =blk_mq_rq_to_pdu (req); -blk_status_t status =virtblk_result (virtblk_vbr_status (vbr));virtblk_unmap_data (req, vbr);virtblk_cleanup_cmd (req); -virtblk_update_zone_append_sector (req, vbr); -blk_mq_end_request (req, status); } +static inline void virtblk_request_done (struct request *req) +{ +struct virtblk_req *vbr =blk_mq_rq_to_pdu (req); +blk_status_t status =virtblk_result (virtblk_vbr_status (vbr)); + +if (req_op (req) ==REQ_OP_ZONE_APPEND && status ==BLK_STS_OK ) + status =virtblk_update_zone_append_sector (req, vbr); + +__virtblk_request_done (req, status); +} +static void virtblk_done (struct virtqueue *vq) {struct virtio_blk *vblk = vq->vdev->priv;
@@ -1242,7 +1273,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,13 +1290,18 @@ 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 =virtblk_result (virtblk_vbr_status (vbr)); found++; -if (!blk_mq_complete_request_remote (req) && - !blk_mq_add_to_batch (req, iob, status !=VIRTIO_BLK_S_OK , +if (blk_mq_complete_request_remote (req)) +continue ; + +if (req_op (req) ==REQ_OP_ZONE_APPEND && status ==BLK_STS_OK ) + status =virtblk_update_zone_append_sector (req, vbr); + +if (!blk_mq_add_to_batch (req, iob, status !=BLK_STS_OK , virtblk_complete_batch)) -virtblk_request_done (req); +__virtblk_request_done (req, status); }if (found)
Signature · 1 line
2.43.0
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 21 lines
From: Hyokyung Kim <[email protected]>
The device reports the starting sector for
REQ_OP_ZONE_APPENDcompletions, andvirtio_blkcopies it intoreq->__sectorwithout 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 withBLK_STS_IOERR.The polling path may batch successful completions, and a batch can only be completed as successful. Perform the same validation in
virtblk_poll()before admitting a request to the batch, so that invalid zone append completions are not batched and are completed with an error instead. Move the request completion into__virtblk_request_done(), which takes the status from its caller, so that a polled completion that is not batched is not validated a second time.
Quoted 2 lines
Fixes 95bfec41bd3d ("virtio-blk: add support for zoned block devices")Signed-off-by Hyokyung Kim <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 21 lines · Annie Kim
From: Hyokyung Kim <[email protected]>
The device reports the starting sector for
REQ_OP_ZONE_APPENDcompletions, andvirtio_blkcopies it intoreq->__sectorwithout 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 withBLK_STS_IOERR.The polling path may batch successful completions, and a batch can only be completed as successful. Perform the same validation in
virtblk_poll()before admitting a request to the batch, so that invalid zone append completions are not batched and are completed with an error instead. Move the request completion into__virtblk_request_done(), which takes the status from its caller, so that a polled completion that is not batched is not validated a second time.
Quoted 2 lines
Fixes 95bfec41bd3d ("virtio-blk: add support for zoned block devices")Signed-off-by Hyokyung Kim <[email protected]>
Looks OK to me.
Signature · 2 lines
Damien Le Moal Western Digital Research