Skip to contentSkip to conversation

hw/nvme: fix potential use-after-free doing controller reset

PATCH Superseded Klaus Jensen 2 participants 2 messages Jun 18

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/nvme: fix potential use-after-free doing controller reset

From: Klaus Jensen <[email protected]>

A controller reset may race with inflight DMA I/O. SQ deletion (when done by the host through the Delete I/O Submission Queue command) handles this, but the controller reset path does not, which may lead to the freeing request resources prior to the DMA completing in certain conditions under large and heavy I/O pressure.

---
 hw/nvme/ctrl.c | 34 ++++++++++++++++++++++++++++++++--
 1 file changed, 32 insertions(+), 2 deletions(-)
hw/nvme/ctrl.c +32 -2
index 815f39173c8a..75768272f241 100644
@@ -7906,10 +7906,40 @@ static void nvme_ctrl_reset(NvmeCtrl *n, NvmeResetType rst)
     }
 
     for (i = 0; i < n->params.max_ioqpairs + 1; i++) {
-        if (n->sq[i] != NULL) {
-            nvme_free_sq(n->sq[i], n);
+        NvmeRequest *req, *next;
+
+        NvmeSQueue *sq = n->sq[i];
+        NvmeCQueue *cq;
+
+        if (!sq) {
+            continue;
         }
+
+        /* cancel any outstanding requests */
+        while (!QTAILQ_EMPTY(&sq->out_req_list)) {
+            req = QTAILQ_FIRST(&sq->out_req_list);
+            assert(req->aiocb);
+            req->status = NVME_NO_COMPLETE;
+            blk_aio_cancel(req->aiocb);
+        }
+
+        assert(QTAILQ_EMPTY(&sq->out_req_list));
+
+        cq = n->cq[sq->cqid];
+
+        /* blk_aio_cancel may enqueue completions on the cq; drop them */
+        QTAILQ_FOREACH_SAFE(req, &cq->req_list, entry, next) {
+            if (req->sq != sq) {
+                continue;
+            }
+
+            QTAILQ_REMOVE(&cq->req_list, req, entry);
+            QTAILQ_INSERT_TAIL(&sq->req_list, req, entry);
+        }
+
+        nvme_free_sq(n->sq[i], n);
     }
+
     for (i = 0; i < n->params.max_ioqpairs + 1; i++) {
         if (n->cq[i] != NULL) {
             nvme_free_cq(n->cq[i], n);

---
base-commit: c7cf7c810153d6f5f31aa2d5c0dee9087f6b4dff
change-id: 20260618-fix-heap-uaf-ctrl-reset-cf05cbc0e729

Best regards,

Signature · 1 line
Klaus Jensen <[email protected]>
#1 lore raw

Hi Klaus,

Quoted 48 lines · Klaus Jensen

From: Klaus Jensen <[email protected]>

A controller reset may race with inflight DMA I/O. SQ deletion (when done by the host through the Delete I/O Submission Queue command) handles this, but the controller reset path does not, which may lead to the freeing request resources prior to the DMA completing in certain conditions under large and heavy I/O pressure.

---
  hw/nvme/ctrl.c | 34 ++++++++++++++++++++++++++++++++--
  1 file changed, 32 insertions(+), 2 deletions(-)
hw/nvme/ctrl.c +22 -2
index 815f39173c8a..75768272f241 100644
@@ -7906,10 +7906,40 @@ static void nvme_ctrl_reset(NvmeCtrl *n, NvmeResetType rst)
      }
 
      for (i = 0; i < n->params.max_ioqpairs + 1; i++) {
-        if (n->sq[i] != NULL) {
-            nvme_free_sq(n->sq[i], n);
+        NvmeRequest *req, *next;
+
+        NvmeSQueue *sq = n->sq[i];
+        NvmeCQueue *cq;
+
+        if (!sq) {
+            continue;
          }
+
+        /* cancel any outstanding requests */
+        while (!QTAILQ_EMPTY(&sq->out_req_list)) {
+            req = QTAILQ_FIRST(&sq->out_req_list);
+            assert(req->aiocb);
+            req->status = NVME_NO_COMPLETE;
+            blk_aio_cancel(req->aiocb);
+        }
+
+        assert(QTAILQ_EMPTY(&sq->out_req_list));
+
+        cq = n->cq[sq->cqid];
+
+        /* blk_aio_cancel may enqueue completions on the cq; drop them */
+        QTAILQ_FOREACH_SAFE(req, &cq->req_list, entry, next) {

hw/nvme/ctrl.c static void nvme_ctrl_reset(NvmeCtrl *n, NvmeResetType rst) · line 7941

Quoted 10 lines
hw/nvme/ctrl.c +9 -0
+            if (req->sq != sq) {
+                continue;
+            }
+
+            QTAILQ_REMOVE(&cq->req_list, req, entry);
+            QTAILQ_INSERT_TAIL(&sq->req_list, req, entry);
+        }
+
+        nvme_free_sq(n->sq[i], n);
      }

Could we factor common code with nvme_del_sq() and call it instead?

Quoted 10 lines
hw/nvme/ctrl.c +1 -1
+
      for (i = 0; i < n->params.max_ioqpairs + 1; i++) {
          if (n->cq[i] != NULL) {
              nvme_free_cq(n->cq[i], n);
 
---

base-commit: c7cf7c810153d6f5f31aa2d5c0dee9087f6b4dff
change-id: 20260618-fix-heap-uaf-ctrl-reset-cf05cbc0e729

Best regards,