Skip to contentSkip to conversation

virtio-scsi: fix SCSIRequest leak on a bad request

PATCH New Michael S. Tsirkin 1 participant 2 messages Jul 24

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 virtio-scsi: fix SCSIRequest leak on a bad request

When virtio_scsi_handle_cmd_vq() cleans up prepared requests after a malformed element in the same batch, it drops only one reference even though virtio_scsi_handle_cmd_req_prepare() leaves each unsubmitted SCSIRequest with two references. This leaks the request and allows repeated bad batches to cause unbounded host memory growth.

Add a second scsi_req_unref() and clear hba_private first.

Fixes CVE-2026-61476
Fixes 661e32fb3c ("virtio-scsi: convert virtio_scsi_bad_req() to use virtio_error()")

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3875

Cc Paolo Bonzini <[email protected]>
Cc Fam Zheng <[email protected]>
Cc Greg Kurz <[email protected]>
---
 include/hw/scsi/scsi.h | 1 +
 hw/scsi/scsi-bus.c     | 7 +++++++
 hw/scsi/virtio-scsi.c  | 2 ++
 3 files changed, 10 insertions(+)
include/hw/scsi/scsi.h +1 -0
index 5f83e58d1d..c60c6e8810 100644
@@ -221,6 +221,7 @@ SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
 int32_t scsi_req_enqueue(SCSIRequest *req);
 SCSIRequest *scsi_req_ref(SCSIRequest *req);
 void scsi_req_unref(SCSIRequest *req);
+void scsi_req_unref_detach_hba(SCSIRequest *req);
 
 int scsi_bus_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf,
                        size_t buf_len, void *hba_private);
hw/scsi/scsi-bus.c +7 -0
index dccb2f25b2..deb43d5560 100644
@@ -1513,6 +1513,13 @@ void scsi_req_unref(SCSIRequest *req)
     }
 }
 
+void scsi_req_unref_detach_hba(SCSIRequest *req)
+{
+    /* Unref when the HBA frees hba_private separately (e.g. virtio_scsi_free_req) */
+    req->hba_private = NULL;
+    scsi_req_unref(req);
+}
+
 /* Tell the device that we finished processing this chunk of I/O.  It
    will start the next chunk or complete the command.  */
 void scsi_req_continue(SCSIRequest *req)
hw/scsi/virtio-scsi.c +2 -0
index 6c73768011..bf64d1231a 100644
@@ -931,7 +931,9 @@ static void virtio_scsi_handle_cmd_vq(VirtIOSCSI *s, VirtQueue *vq)
                     req = QTAILQ_FIRST(&reqs);
                     QTAILQ_REMOVE(&reqs, req, next);
                     defer_call_end();
+                    /* Drop both the ref from _prepare and the initial ref */
                     scsi_req_unref(req->sreq);
+                    scsi_req_unref_detach_hba(req->sreq);
                     virtqueue_detach_element(req->vq, &req->elem, 0);
                     virtio_scsi_free_req(req);
                 }
Signature · 1 line
MST
#1 lore raw

PATCH 2/2 scsi: use scsi_req_unref_detach_hba() in HBA drivers

Convert all open-coded hba_private = NULL + scsi_req_unref() pairs to scsi_req_unref_detach_hba().

---
 hw/scsi/megasas.c     | 3 +--
 hw/scsi/mptsas.c      | 3 +--
 hw/scsi/ncr53c710.c   | 6 ++----
 hw/scsi/virtio-scsi.c | 3 +--
 hw/ufs/lu.c           | 3 +--
 5 files changed, 6 insertions(+), 12 deletions(-)
hw/scsi/megasas.c +1 -2
index 9e712721f8..d64d39706b 100644
@@ -599,8 +599,7 @@ static void megasas_complete_command(MegasasCmd *cmd)
     cmd->iov_size = 0;
     cmd->iov_offset = 0;
 
-    cmd->req->hba_private = NULL;
-    scsi_req_unref(cmd->req);
+    scsi_req_unref_detach_hba(cmd->req);
     cmd->req = NULL;
 
     megasas_unmap_frame(cmd->state, cmd);
hw/scsi/mptsas.c +1 -2
index 5df124c0ce..6da8a740f3 100644
@@ -260,8 +260,7 @@ static int mptsas_build_sgl(MPTSASState *s, MPTSASRequest *req, hwaddr req_addr)
 static void mptsas_free_request(MPTSASRequest *req)
 {
     if (req->sreq != NULL) {
-        req->sreq->hba_private = NULL;
-        scsi_req_unref(req->sreq);
+        scsi_req_unref_detach_hba(req->sreq);
         req->sreq = NULL;
     }
     qemu_sglist_destroy(&req->qsg);
hw/scsi/ncr53c710.c +2 -4
index 57967e7a84..b3345a43c2 100644
@@ -756,11 +756,10 @@ void ncr710_request_cancelled(SCSIRequest *req)
     NCR710State *s = ncr710_from_scsi_bus(req->bus);
     NCR710Request *p = (NCR710Request *)req->hba_private;
     if (p) {
-        req->hba_private = NULL;
         p->req = NULL;
         ncr710_request_free(s, p);
     }
-    scsi_req_unref(req);
+    scsi_req_unref_detach_hba(req);
 }
 
 static int ncr710_queue_req(NCR710State *s, SCSIRequest *req, uint32_t len)
@@ -801,13 +800,12 @@ void ncr710_command_complete(SCSIRequest *req, size_t resid)
     ncr710_set_phase(s, PHASE_ST);
 
     if (p) {
-        req->hba_private = NULL;
         if (p == s->current) {
             p->req = NULL;
         } else {
             ncr710_request_free(s, p);
         }
-        scsi_req_unref(req);
+        scsi_req_unref_detach_hba(req);
     }
 
     if (s->waiting == NCR710_WAIT_RESELECT || s->waiting == NCR710_WAIT_DMA) {
hw/scsi/virtio-scsi.c +1 -2
index bf64d1231a..53c3c88460 100644
@@ -123,8 +123,7 @@ static void virtio_scsi_complete_req(VirtIOSCSIReq *req, QemuMutex *vq_lock)
     }
 
     if (req->sreq) {
-        req->sreq->hba_private = NULL;
-        scsi_req_unref(req->sreq);
+        scsi_req_unref_detach_hba(req->sreq);
     }
     virtio_scsi_free_req(req);
 }
hw/ufs/lu.c +1 -2
index 13f4a90145..b2c2cadf7e 100644
@@ -184,8 +184,7 @@ static void ufs_scsi_command_complete(SCSIRequest *scsi_req, size_t resid)
 
     ufs_complete_req(req, UFS_REQUEST_SUCCESS);
 
-    scsi_req->hba_private = NULL;
-    scsi_req_unref(scsi_req);
+    scsi_req_unref_detach_hba(scsi_req);
 }
 
 static QEMUSGList *ufs_get_sg_list(SCSIRequest *scsi_req)
Signature · 1 line
MST