Skip to contentSkip to conversation

hw/riscv/riscv-iommu: Coverity fixes

PATCH In review Daniel Henrique Barboza 3 participants 6 messages Nov 4, 2024 – Nov 5, 2024 RISC-V

lore
Share & tools
lore
#0 lore raw

cover hw/riscv/riscv-iommu: Coverity fixes

Hi,

This series fixes two issues detected by Coverity in the riscv-iommu code that just went upstream.

Peter,

I'm fixing only 2 CIDs because the third one is a false positive:

*** CID 1564781: Integer handling issues (INTEGER_OVERFLOW) /builds/qemu-project/qemu/hw/riscv/riscv-iommu-pci.c: 97 in riscv_iommu_pci_realize() 91
92 /* Set device id for trace / debug */
93 DEVICE(iommu)->id = g_strdup_printf("%02x:%02x.%01x",
94 pci_dev_bus_num(dev), PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
95 qdev_realize(DEVICE(iommu), NULL, errp);
96

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 1564781: Integer handling issues (INTEGER_OVERFLOW)
Expression "memory_region_size(&iommu->regs_mr) + 4096UL", which is equal to 4095, where "memory_region_size(&iommu->regs_mr)" is known to be equal to 18446744073709551615, overflows the type that receives it, an unsigned integer 64 bits wide.

97 memory_region_init(&s->bar0, OBJECT(s), "riscv-iommu-bar0",
98 QEMU_ALIGN_UP(memory_region_size(&iommu->regs_mr), TARGET_PAGE_SIZE));
99 memory_region_add_subregion(&s->bar0, 0, &iommu->regs_mr);
100
101 pcie_endpoint_cap_init(dev, 0);
102
----------

The reason is that is that iommu->regs_mr is being initialized in riscv_iommu_realize() with 'RISCV_IOMMU_REG_SIZE':

    memory_region_init_io(&s->regs_mr, OBJECT(dev), &riscv_iommu_mmio_ops, s,
        "riscv-iommu-regs", RISCV_IOMMU_REG_SIZE);

And we're doing "qdev_realize(DEVICE(iommu), NULL, errp);" right before the snippet Coverity found as problematic so it's guaranteed to be initialized. I ran it with a debugger and verified that QEMU_ALIGN_UP(memory_region_size(&iommu->regs_mr), TARGET_PAGE_SIZE) is in fact equal to 'RISCV_IOMMU_REG_SIZE' at that point, as intended.

I was going to set it as false positive in Coverity but decided to verify with you first. If you agree I'll update the ticket.

Daniel Henrique Barboza (2):
hw/riscv/riscv-iommu: change 'depth' to int hw/riscv/riscv-iommu: fix riscv_iommu_validate_process_ctx() check

 hw/riscv/riscv-iommu.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Signature · 1 line
2.45.2
#1 lore raw

PATCH 1/2 hw/riscv/riscv-iommu: change 'depth' to int

Coverity reports an unsigned overflow when doing:

    for (; depth-- > 0; ) {

When depth = 0 inside riscv_iommu_ctx_fetch().

Building it with a recent GCC the code doesn't actually break with depth = 0, i.e. the comparison "0-- > 0" will exit the loop instead of proceeding, but 'depth' will retain the overflow value afterwards.

This behavior can be compiler dependent, so change 'depth' to int to remove this potential ambiguity.

Resolves: Coverity CID 1564783

Fixes 0c54acb8243 ("hw/riscv: add RISC-V IOMMU base emulation")
---
 hw/riscv/riscv-iommu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/riscv/riscv-iommu.c +1 -1
index feb650549a..05341ad20b 100644
@@ -846,7 +846,7 @@ static int riscv_iommu_ctx_fetch(RISCVIOMMUState *s, RISCVIOMMUContext *ctx)
     /* Device Context format: 0: extended (64 bytes) | 1: base (32 bytes) */
     const int dc_fmt = !s->enable_msi;
     const size_t dc_len = sizeof(dc) >> dc_fmt;
-    unsigned depth;
+    int depth;
     uint64_t de;
 
     switch (mode) {
Signature · 1 line
2.45.2
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] hw/riscv/riscv-iommu: change 'depth' to int

Quoted 11 lines · Daniel Henrique Barboza

Coverity reports an unsigned overflow when doing:

    for (; depth-- > 0; ) {

When depth = 0 inside riscv_iommu_ctx_fetch().

Building it with a recent GCC the code doesn't actually break with depth = 0, i.e. the comparison "0-- > 0" will exit the loop instead of proceeding, but 'depth' will retain the overflow value afterwards.

Quoted 6 lines

This behavior can be compiler dependent, so change 'depth' to int to remove this potential ambiguity.

Resolves: Coverity CID 1564783

Fixes 0c54acb8243 ("hw/riscv: add RISC-V IOMMU base emulation")

Alistair

Quoted 21 lines
---
 hw/riscv/riscv-iommu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/riscv/riscv-iommu.c +1 -2
index feb650549a..05341ad20b 100644
@@ -846,7 +846,7 @@ static int riscv_iommu_ctx_fetch(RISCVIOMMUState *s, RISCVIOMMUContext *ctx)
     /* Device Context format: 0: extended (64 bytes) | 1: base (32 bytes) */
     const int dc_fmt = !s->enable_msi;
     const size_t dc_len = sizeof(dc) >> dc_fmt;
-    unsigned depth;
+    int depth;
     uint64_t de;
 
     switch (mode) {
--

2.45.2

#3 lore raw

PATCH 2/2 hw/riscv/riscv-iommu: fix riscv_iommu_validate_process_ctx() check

In reply to Daniel Henrique Barboza

'mode' will never be RISCV_IOMMU_CAP_SV32. We are erroring out in the 'switch' right before it if 'mode' isn't 0, 8, 9 or 10.

'mode' should be check with RISCV_IOMMU_DC_FSC_IOSATP_MODE_SV32.

Reported by Coverity via a "DEADCODE" ticket.

Resolves: Coverity CID 1564781

Fixes 0c54acb8243 ("hw/riscv: add RISC-V IOMMU base emulation")
---
 hw/riscv/riscv-iommu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/riscv/riscv-iommu.c +1 -1
index 05341ad20b..dd102b3c70 100644
@@ -803,7 +803,7 @@ static bool riscv_iommu_validate_process_ctx(RISCVIOMMUState *s,
     }
 
     if (ctx->tc & RISCV_IOMMU_DC_TC_SXL) {
-        if (mode == RISCV_IOMMU_CAP_SV32 &&
+        if (mode == RISCV_IOMMU_DC_FSC_IOSATP_MODE_SV32 &&
             !(s->cap & RISCV_IOMMU_CAP_SV32)) {
                 return false;
         }
Signature · 1 line
2.45.2
Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] hw/riscv/riscv-iommu: fix riscv_iommu_validate_process_ctx() check

Quoted 11 lines · Daniel Henrique Barboza

'mode' will never be RISCV_IOMMU_CAP_SV32. We are erroring out in the 'switch' right before it if 'mode' isn't 0, 8, 9 or 10.

'mode' should be check with RISCV_IOMMU_DC_FSC_IOSATP_MODE_SV32.

Reported by Coverity via a "DEADCODE" ticket.

Resolves: Coverity CID 1564781

Fixes 0c54acb8243 ("hw/riscv: add RISC-V IOMMU base emulation")

Alistair

Quoted 21 lines
---
 hw/riscv/riscv-iommu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/riscv/riscv-iommu.c +1 -2
index 05341ad20b..dd102b3c70 100644
@@ -803,7 +803,7 @@ static bool riscv_iommu_validate_process_ctx(RISCVIOMMUState *s,
     }
 
     if (ctx->tc & RISCV_IOMMU_DC_TC_SXL) {
-        if (mode == RISCV_IOMMU_CAP_SV32 &&
+        if (mode == RISCV_IOMMU_DC_FSC_IOSATP_MODE_SV32 &&
             !(s->cap & RISCV_IOMMU_CAP_SV32)) {
                 return false;
         }
--

2.45.2

#5 lore raw
In reply to Daniel Henrique Barboza
Quoted 34 lines · Daniel Henrique Barboza

Hi,

This series fixes two issues detected by Coverity in the riscv-iommu code that just went upstream.

Peter,

I'm fixing only 2 CIDs because the third one is a false positive:

*** CID 1564781: Integer handling issues (INTEGER_OVERFLOW) /builds/qemu-project/qemu/hw/riscv/riscv-iommu-pci.c: 97 in riscv_iommu_pci_realize() 91
92 /* Set device id for trace / debug */
93 DEVICE(iommu)->id = g_strdup_printf("%02x:%02x.%01x",
94 pci_dev_bus_num(dev), PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
95 qdev_realize(DEVICE(iommu), NULL, errp);
96

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 1564781: Integer handling issues (INTEGER_OVERFLOW)
Expression "memory_region_size(&iommu->regs_mr) + 4096UL", which is equal to 4095, where "memory_region_size(&iommu->regs_mr)" is known to be equal to 18446744073709551615, overflows the type that receives it, an unsigned integer 64 bits wide.

97 memory_region_init(&s->bar0, OBJECT(s), "riscv-iommu-bar0",
98 QEMU_ALIGN_UP(memory_region_size(&iommu->regs_mr), TARGET_PAGE_SIZE));
99 memory_region_add_subregion(&s->bar0, 0, &iommu->regs_mr);
100
101 pcie_endpoint_cap_init(dev, 0);
102
----------

The reason is that is that iommu->regs_mr is being initialized in riscv_iommu_realize() with 'RISCV_IOMMU_REG_SIZE':

    memory_region_init_io(&s->regs_mr, OBJECT(dev), &riscv_iommu_mmio_ops, s,
        "riscv-iommu-regs", RISCV_IOMMU_REG_SIZE);
Quoted 8 lines

And we're doing "qdev_realize(DEVICE(iommu), NULL, errp);" right before the snippet Coverity found as problematic so it's guaranteed to be initialized. I ran it with a debugger and verified that QEMU_ALIGN_UP(memory_region_size(&iommu->regs_mr), TARGET_PAGE_SIZE) is in fact equal to 'RISCV_IOMMU_REG_SIZE' at that point, as intended.

I was going to set it as false positive in Coverity but decided to verify with you first. If you agree I'll update the ticket.

Yes, this is a false positive. Coverity has a habit of assuming that because in some cases a function can return a particular constant value that it's therefore possible at any callsite. I just marked this false-positive in the UI.

Thanks for the prompt fixes for the other two!

-- PMM