Skip to contentSkip to conversation

hw/block/nvme: coverity fixes

PATCH In review Klaus Jensen 2 participants 5 messages Mar 15, 2021

lore
Share & tools
lore
#0 lore raw

cover hw/block/nvme: coverity fixes

From: Klaus Jensen <[email protected]>

Fix three issues reported by coverity (CID 1450756, CID 1450757 and CID 1450758).

Klaus Jensen (2):
hw/block/nvme: fix potential overflow
hw/block/nvme: assert namespaces array indices

 hw/block/nvme-subsys.h |  2 ++
 hw/block/nvme.h        | 10 ++++++++--
 hw/block/nvme-subsys.c |  7 +++++--
 hw/block/nvme.c        |  2 +-
 4 files changed, 16 insertions(+), 5 deletions(-)
Signature · 1 line
2.30.1
#1 lore raw

PATCH 1/2 hw/block/nvme: fix potential overflow

From: Klaus Jensen <[email protected]>

page_size is a uint32_t, and zasl is a uint8_t, so the expression page_size << zasl is done using 32-bit arithmetic and might overflow. Since we then compare this against a 64 bit data_size value, Coverity complains that we might overflow unintentionally. An MDTS/ZASL value in excess of 4GiB is probably impractical, but it is not entirely unrealistic, so add a cast such that we handle that case properly.

Fixes 578d914b263c ("hw/block/nvme: align zoned.zasl with mdts")
Fixes CID 1450756
---
 hw/block/nvme.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/block/nvme.c +1 -1
index d439e44db839..f8ad34077000 100644
@@ -2188,7 +2188,7 @@ static uint16_t nvme_do_write(NvmeCtrl *n, NvmeRequest *req, bool append,
                 goto invalid;
             }
 
-            if (n->params.zasl && data_size > n->page_size << n->params.zasl) {
+            if (n->params.zasl && data_size > (uint64_t)n->page_size << n->params.zasl) {
                 trace_pci_nvme_err_zasl(data_size);
                 return NVME_INVALID_FIELD | NVME_DNR;
             }
Signature · 1 line
2.30.1
#2 lore raw

PATCH 2/2 hw/block/nvme: assert namespaces array indices

In reply to Klaus Jensen

From: Klaus Jensen <[email protected]>

Coverity complains about a possible memory corruption in the nvme_ns_attach and _detach functions. While we should not (famous last words) be able to reach this function without nsid having previously been validated, this is still an open door for future misuse.

Make Coverity and maintainers happy by asserting that the index into the array is valid. Also, while not detected by Coverity (yet), add an assert in nvme_subsys_ns and nvme_subsys_register_ns as well since a similar issue is exists there.

Fixes 037953b5b299 ("hw/block/nvme: support namespace detach")
Fixes CID 1450757
Fixes CID 1450758
Cc Minwoo Im <[email protected]>
---
 hw/block/nvme-subsys.h |  2 ++
 hw/block/nvme.h        | 10 ++++++++--
 hw/block/nvme-subsys.c |  7 +++++--
 3 files changed, 15 insertions(+), 4 deletions(-)
hw/block/nvme-subsys.h +2 -0
index fb66ae752ad5..aafa04b84829 100644
@@ -54,6 +54,8 @@ static inline NvmeNamespace *nvme_subsys_ns(NvmeSubsystem *subsys,
         return NULL;
     }
 
+    assert(nsid && nsid <= NVME_SUBSYS_MAX_NAMESPACES);
+
     return subsys->namespaces[nsid];
 }
 
hw/block/nvme.h +8 -2
index 4955d649c7d4..45ba9dbc2131 100644
@@ -236,12 +236,18 @@ static inline bool nvme_ns_is_attached(NvmeCtrl *n, NvmeNamespace *ns)
 
 static inline void nvme_ns_attach(NvmeCtrl *n, NvmeNamespace *ns)
 {
-    n->namespaces[nvme_nsid(ns) - 1] = ns;
+    uint32_t nsid = ns->params.nsid;
+    assert(nsid && nsid <= NVME_MAX_NAMESPACES);
+
+    n->namespaces[nsid - 1] = ns;
 }
 
 static inline void nvme_ns_detach(NvmeCtrl *n, NvmeNamespace *ns)
 {
-    n->namespaces[nvme_nsid(ns) - 1] = NULL;
+    uint32_t nsid = ns->params.nsid;
+    assert(nsid && nsid <= NVME_MAX_NAMESPACES);
+
+    n->namespaces[nsid - 1] = NULL;
 }
 
 static inline NvmeCQueue *nvme_cq(NvmeRequest *req)
hw/block/nvme-subsys.c +5 -2
index af4804a819ee..2f6d3b47bacf 100644
@@ -47,15 +47,18 @@ int nvme_subsys_register_ns(NvmeNamespace *ns, Error **errp)
 {
     NvmeSubsystem *subsys = ns->subsys;
     NvmeCtrl *n;
+    uint32_t nsid = ns->params.nsid;
     int i;
 
-    if (subsys->namespaces[nvme_nsid(ns)]) {
+    assert(nsid && nsid <= NVME_SUBSYS_MAX_NAMESPACES);
+
+    if (subsys->namespaces[nsid]) {
         error_setg(errp, "namespace %d already registerd to subsy %s",
                    nvme_nsid(ns), subsys->parent_obj.id);
         return -1;
     }
 
-    subsys->namespaces[nvme_nsid(ns)] = ns;
+    subsys->namespaces[nsid] = ns;
 
     for (i = 0; i < ARRAY_SIZE(subsys->ctrls); i++) {
         n = subsys->ctrls[i];
Signature · 1 line
2.30.1
Reviewed-by on v1 2/2
#3 lore raw

Re: [PATCH 2/2] hw/block/nvme: assert namespaces array indices

Quoted 41 lines · Klaus Jensen

From: Klaus Jensen <[email protected]>

Coverity complains about a possible memory corruption in the nvme_ns_attach and _detach functions. While we should not (famous last words) be able to reach this function without nsid having previously been validated, this is still an open door for future misuse.

Make Coverity and maintainers happy by asserting that the index into the array is valid. Also, while not detected by Coverity (yet), add an assert in nvme_subsys_ns and nvme_subsys_register_ns as well since a similar issue is exists there.

Fixes 037953b5b299 ("hw/block/nvme: support namespace detach")
Fixes CID 1450757
Fixes CID 1450758
Cc Minwoo Im <[email protected]>
---
 hw/block/nvme-subsys.h |  2 ++
 hw/block/nvme.h        | 10 ++++++++--
 hw/block/nvme-subsys.c |  7 +++++--
 3 files changed, 15 insertions(+), 4 deletions(-)
hw/block/nvme-subsys.h +2 -0
index fb66ae752ad5..aafa04b84829 100644
@@ -54,6 +54,8 @@ static inline NvmeNamespace *nvme_subsys_ns(NvmeSubsystem *subsys,
         return NULL;
     }
 
+    assert(nsid && nsid <= NVME_SUBSYS_MAX_NAMESPACES);
+
     return subsys->namespaces[nsid];
 }
 
hw/block/nvme.h +0 -0
index 4955d649c7d4..45ba9dbc2131 100644

hw/block/nvme.h static inline bool nvme_ns_is_attached(NvmeCtrl *n, NvmeNamespace *ns) · line 239

Quoted 5 lines
hw/block/nvme.h +1 -1
@@ -236,12 +236,18 @@ static inline bool nvme_ns_is_attached(NvmeCtrl *n, NvmeNamespace *ns)
 
 static inline void nvme_ns_attach(NvmeCtrl *n, NvmeNamespace *ns)
 {
-    n->namespaces[nvme_nsid(ns) - 1] = ns;
+    uint32_t nsid = ns->params.nsid;

Why not keep using nvme_nsid(ns)?

hw/block/nvme.h static inline bool nvme_ns_is_attached(NvmeCtrl *n, NvmeNamespace *ns) · line 247

Quoted 9 lines
hw/block/nvme.h +4 -1
+    assert(nsid && nsid <= NVME_MAX_NAMESPACES);
+
+    n->namespaces[nsid - 1] = ns;
 }
 
 static inline void nvme_ns_detach(NvmeCtrl *n, NvmeNamespace *ns)
 {
-    n->namespaces[nvme_nsid(ns) - 1] = NULL;
+    uint32_t nsid = ns->params.nsid;

Ditto.

Quoted 11 lines
hw/block/nvme.h +3 -0
+    assert(nsid && nsid <= NVME_MAX_NAMESPACES);
+
+    n->namespaces[nsid - 1] = NULL;
 }
 
 static inline NvmeCQueue *nvme_cq(NvmeRequest *req)
hw/block/nvme-subsys.c +0 -0
index af4804a819ee..2f6d3b47bacf 100644

hw/block/nvme-subsys.c int nvme_subsys_register_ns(NvmeNamespace *ns, Error **errp) · line 50

Quoted 4 lines
hw/block/nvme-subsys.c +1 -0
@@ -47,15 +47,18 @@ int nvme_subsys_register_ns(NvmeNamespace *ns, Error **errp)
 {
     NvmeSubsystem *subsys = ns->subsys;
     NvmeCtrl *n;
+    uint32_t nsid = ns->params.nsid;

Ditto.

Preferably using nvme_nsid():

Quoted 17 lines
hw/block/nvme-subsys.c +4 -2
     int i;
 
-    if (subsys->namespaces[nvme_nsid(ns)]) {
+    assert(nsid && nsid <= NVME_SUBSYS_MAX_NAMESPACES);
+
+    if (subsys->namespaces[nsid]) {
         error_setg(errp, "namespace %d already registerd to subsy %s",
                    nvme_nsid(ns), subsys->parent_obj.id);
         return -1;
     }
 
-    subsys->namespaces[nvme_nsid(ns)] = ns;
+    subsys->namespaces[nsid] = ns;
 
     for (i = 0; i < ARRAY_SIZE(subsys->ctrls); i++) {
         n = subsys->ctrls[i];
#4 lore raw

Re: [PATCH 2/2] hw/block/nvme: assert namespaces array indices

Quoted 80 lines · Philippe Mathieu-Daudé
Quoted 41 lines · Klaus Jensen

From: Klaus Jensen <[email protected]>

Coverity complains about a possible memory corruption in the nvme_ns_attach and _detach functions. While we should not (famous last words) be able to reach this function without nsid having previously been validated, this is still an open door for future misuse.

Make Coverity and maintainers happy by asserting that the index into the array is valid. Also, while not detected by Coverity (yet), add an assert in nvme_subsys_ns and nvme_subsys_register_ns as well since a similar issue is exists there.

Fixes 037953b5b299 ("hw/block/nvme: support namespace detach")
Fixes CID 1450757
Fixes CID 1450758
Cc Minwoo Im <[email protected]>
---
 hw/block/nvme-subsys.h |  2 ++
 hw/block/nvme.h        | 10 ++++++++--
 hw/block/nvme-subsys.c |  7 +++++--
 3 files changed, 15 insertions(+), 4 deletions(-)
hw/block/nvme-subsys.h +2 -0
index fb66ae752ad5..aafa04b84829 100644
@@ -54,6 +54,8 @@ static inline NvmeNamespace *nvme_subsys_ns(NvmeSubsystem *subsys,
         return NULL;
     }
 
+    assert(nsid && nsid <= NVME_SUBSYS_MAX_NAMESPACES);
+
     return subsys->namespaces[nsid];
 }
 
hw/block/nvme.h +0 -0
index 4955d649c7d4..45ba9dbc2131 100644

hw/block/nvme.h static inline bool nvme_ns_is_attached(NvmeCtrl *n, NvmeNamespace *ns) · line 239

Quoted 5 lines
hw/block/nvme.h +1 -1
@@ -236,12 +236,18 @@ static inline bool nvme_ns_is_attached(NvmeCtrl *n, NvmeNamespace *ns)
 
 static inline void nvme_ns_attach(NvmeCtrl *n, NvmeNamespace *ns)
 {
-    n->namespaces[nvme_nsid(ns) - 1] = ns;
+    uint32_t nsid = ns->params.nsid;

Why not keep using nvme_nsid(ns)?

hw/block/nvme.h static inline bool nvme_ns_is_attached(NvmeCtrl *n, NvmeNamespace *ns) · line 247

Quoted 9 lines
hw/block/nvme.h +4 -1
+    assert(nsid && nsid <= NVME_MAX_NAMESPACES);
+
+    n->namespaces[nsid - 1] = ns;
 }
 
 static inline void nvme_ns_detach(NvmeCtrl *n, NvmeNamespace *ns)
 {
-    n->namespaces[nvme_nsid(ns) - 1] = NULL;
+    uint32_t nsid = ns->params.nsid;

Ditto.

Quoted 11 lines
hw/block/nvme.h +3 -0
+    assert(nsid && nsid <= NVME_MAX_NAMESPACES);
+
+    n->namespaces[nsid - 1] = NULL;
 }
 
 static inline NvmeCQueue *nvme_cq(NvmeRequest *req)
hw/block/nvme-subsys.c +0 -0
index af4804a819ee..2f6d3b47bacf 100644

hw/block/nvme-subsys.c int nvme_subsys_register_ns(NvmeNamespace *ns, Error **errp) · line 50

Quoted 4 lines
hw/block/nvme-subsys.c +1 -0
@@ -47,15 +47,18 @@ int nvme_subsys_register_ns(NvmeNamespace *ns, Error **errp)
 {
     NvmeSubsystem *subsys = ns->subsys;
     NvmeCtrl *n;
+    uint32_t nsid = ns->params.nsid;

Ditto.

Quoted 3 lines

Preferably using nvme_nsid():

You are right. I'll switch it back. Thanks!

Quoted 18 lines
Quoted 17 lines
hw/block/nvme-subsys.c +4 -2
     int i;
 
-    if (subsys->namespaces[nvme_nsid(ns)]) {
+    assert(nsid && nsid <= NVME_SUBSYS_MAX_NAMESPACES);
+
+    if (subsys->namespaces[nsid]) {
         error_setg(errp, "namespace %d already registerd to subsy %s",
                    nvme_nsid(ns), subsys->parent_obj.id);
         return -1;
     }
 
-    subsys->namespaces[nvme_nsid(ns)] = ns;
+    subsys->namespaces[nsid] = ns;
 
     for (i = 0; i < ARRAY_SIZE(subsys->ctrls); i++) {
         n = subsys->ctrls[i];