Skip to contentSkip to conversation

hw/nvme: coverity fixes

PATCH In review Klaus Jensen 2 participants 7 messages Apr 11, 2023 – Apr 12, 2023

lore
Share & tools
lore
#0 lore raw

cover hw/nvme: coverity fixes

From: Klaus Jensen <[email protected]>

Fix two issues reported by Coverity (CID 1507979 and 1508281).

Klaus Jensen (2):
hw/nvme: fix memory leak in fdp ruhid parsing
hw/nvme: fix memory leak in nvme_dsm

 hw/nvme/ctrl.c | 3 +++
 hw/nvme/ns.c   | 3 ++-
 2 files changed, 5 insertions(+), 1 deletion(-)
Signature · 1 line
2.39.2
#1 lore raw

PATCH 1/2 hw/nvme: fix memory leak in fdp ruhid parsing

From: Klaus Jensen <[email protected]>

Coverity reports a memory leak of memory when parsing ruhids at namespace initialization. Since this is just working memory, not needed beyond the scope of the functions, fix this by adding a g_autofree annotation.

Reported-by Coverity (CID 1507979)
Fixes 73064edfb864 ("hw/nvme: flexible data placement emulation")
---
 hw/nvme/ns.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
hw/nvme/ns.c +2 -1
index cfac960dcf39..547c0b154312 100644
@@ -399,7 +399,8 @@ static bool nvme_ns_init_fdp(NvmeNamespace *ns, Error **errp)
     NvmeEnduranceGroup *endgrp = ns->endgrp;
     NvmeRuHandle *ruh;
     uint8_t lbafi = NVME_ID_NS_FLBAS_INDEX(ns->id_ns.flbas);
-    unsigned int *ruhid, *ruhids;
+    g_autofree unsigned int *ruhids = NULL;
+    unsigned int *ruhid;
     char *r, *p, *token;
     uint16_t *ph;
 
Signature · 1 line
2.39.2
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] hw/nvme: fix memory leak in fdp ruhid parsing

Quoted 7 lines · Klaus Jensen

From: Klaus Jensen <[email protected]>

Coverity reports a memory leak of memory when parsing ruhids at namespace initialization. Since this is just working memory, not needed beyond the scope of the functions, fix this by adding a g_autofree annotation.

Quoted 6 lines
Reported-by Coverity (CID 1507979)
Fixes 73064edfb864 ("hw/nvme: flexible data placement emulation")
---
  hw/nvme/ns.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
#3 lore raw

PATCH 2/2 hw/nvme: fix memory leak in nvme_dsm

In reply to Klaus Jensen

From: Klaus Jensen <[email protected]>

The iocb (and the allocated memory to hold LBA ranges) leaks if reading the LBA ranges fails.

Fix this by adding a free and an unref of the iocb.

Reported-by Coverity (CID 1508281)
Fixes d7d1474fd85d ("hw/nvme: reimplement dsm to allow cancellation")
---
 hw/nvme/ctrl.c | 3 +++
 1 file changed, 3 insertions(+)
hw/nvme/ctrl.c +3 -0
index 8b7be1420912..ac24eeb5ed5a 100644
@@ -2619,6 +2619,9 @@ static uint16_t nvme_dsm(NvmeCtrl *n, NvmeRequest *req)
         status = nvme_h2c(n, (uint8_t *)iocb->range, sizeof(NvmeDsmRange) * nr,
                           req);
         if (status) {
+            g_free(iocb->range);
+            qemu_aio_unref(iocb);
+
             return status;
         }
 
Signature · 1 line
2.39.2
Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] hw/nvme: fix memory leak in nvme_dsm

Quoted 19 lines · Klaus Jensen

From: Klaus Jensen <[email protected]>

The iocb (and the allocated memory to hold LBA ranges) leaks if reading the LBA ranges fails.

Fix this by adding a free and an unref of the iocb.

Reported-by Coverity (CID 1508281)
Fixes d7d1474fd85d ("hw/nvme: reimplement dsm to allow cancellation")
---
  hw/nvme/ctrl.c | 3 +++
  1 file changed, 3 insertions(+)
hw/nvme/ctrl.c +0 -0
index 8b7be1420912..ac24eeb5ed5a 100644

hw/nvme/ctrl.c static uint16_t nvme_dsm(NvmeCtrl *n, NvmeRequest *req) · line 2623

Quoted 5 lines
hw/nvme/ctrl.c +2 -0
@@ -2619,6 +2619,9 @@ static uint16_t nvme_dsm(NvmeCtrl *n, NvmeRequest *req)
          status = nvme_h2c(n, (uint8_t *)iocb->range, sizeof(NvmeDsmRange) * nr,
                            req);
          if (status) {
+            g_free(iocb->range);
+            qemu_aio_unref(iocb);

I note the qemu_aio_FOO() functions are not documented.

Quoted 4 lines
hw/nvme/ctrl.c +1 -0
+
              return status;
          }
 
#5 lore raw

Re: [PATCH 2/2] hw/nvme: fix memory leak in nvme_dsm

Quoted 28 lines · Philippe Mathieu-Daudé
Quoted 19 lines · Klaus Jensen

From: Klaus Jensen <[email protected]>

The iocb (and the allocated memory to hold LBA ranges) leaks if reading the LBA ranges fails.

Fix this by adding a free and an unref of the iocb.

Reported-by Coverity (CID 1508281)
Fixes d7d1474fd85d ("hw/nvme: reimplement dsm to allow cancellation")
---
  hw/nvme/ctrl.c | 3 +++
  1 file changed, 3 insertions(+)
hw/nvme/ctrl.c +0 -0
index 8b7be1420912..ac24eeb5ed5a 100644

hw/nvme/ctrl.c static uint16_t nvme_dsm(NvmeCtrl *n, NvmeRequest *req) · line 2623

Quoted 5 lines
hw/nvme/ctrl.c +2 -0
@@ -2619,6 +2619,9 @@ static uint16_t nvme_dsm(NvmeCtrl *n, NvmeRequest *req)
          status = nvme_h2c(n, (uint8_t *)iocb->range, sizeof(NvmeDsmRange) * nr,
                            req);
          if (status) {
+            g_free(iocb->range);
+            qemu_aio_unref(iocb);
Quoted 2 lines

I note the qemu_aio_FOO() functions are not documented.

As-in, "you are not supposed to use them" or "this should be documented at some point"?

Thanks for your reviews Philippe, you're a life-saver :)

#6 lore raw
In reply to Klaus Jensen
Quoted 4 lines · Klaus Jensen

From: Klaus Jensen <[email protected]>

Fix two issues reported by Coverity (CID 1507979 and 1508281).

Quoted 8 lines

Klaus Jensen (2):
hw/nvme: fix memory leak in fdp ruhid parsing
hw/nvme: fix memory leak in nvme_dsm

 hw/nvme/ctrl.c | 3 +++
 hw/nvme/ns.c   | 3 ++-
 2 files changed, 5 insertions(+), 1 deletion(-)

Peter,

Are you willing to merge this for 8.0 without additional reviews?

I'm pretty confident on my fixes.

Thanks,
Klaus