Skip to contentSkip to conversation

ARM SMMUv3: Fix a couple of Coverity issues

PATCH Applied Eric Auger 3 participants 9 messages May 16, 2018 – May 17, 2018

lore
Share & tools
lore

Eric Auger

author
#0 lore raw

cover ARM SMMUv3: Fix a couple of Coverity issues

This series includes 2 patches that fix Coverity issues respectively in smmuv3 and smmu-common code.

Eric Auger (2):
hw/arm/smmuv3: Fix Coverity issue in smmuv3_record_event hw/arm/smmu-common: Fix coverity issue in get_block_pte_address

 hw/arm/smmu-common.c | 4 ++--
 hw/arm/smmuv3.c      | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
Signature · 1 line
1.8.3.1
author
#1 lore raw

PATCH 1/2 hw/arm/smmuv3: Fix Coverity issue in smmuv3_record_event

Coverity complains about use of uninitialized Evt struct. The EVT_SET_TYPE and similar setters use deposit32() on fields in the struct, so they read the uninitialized existing values. In cases where we don't set all the fields in the event struct we'll end up leaking random uninitialized data from QEMU's stack into the guest.

Initializing the struct with "Evt evt = {};" ought to satisfy Coverity and fix the data leak.

---
 hw/arm/smmuv3.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/arm/smmuv3.c +1 -1
index b3026de..42dc521 100644
@@ -143,7 +143,7 @@ static MemTxResult smmuv3_write_eventq(SMMUv3State *s, Evt *evt)
 
 void smmuv3_record_event(SMMUv3State *s, SMMUEventInfo *info)
 {
-    Evt evt;
+    Evt evt = {};
     MemTxResult r;
 
     if (!smmuv3_eventq_enabled(s)) {
Signature · 1 line
1.8.3.1
Reviewed-by on v1 1/2
#2 lore raw

Re: [Qemu-devel] [Qemu-arm] [PATCH 1/2] hw/arm/smmuv3: Fix Coverity issue in smmuv3_record_event

Quoted 7 lines · Eric Auger

Coverity complains about use of uninitialized Evt struct. The EVT_SET_TYPE and similar setters use deposit32() on fields in the struct, so they read the uninitialized existing values. In cases where we don't set all the fields in the event struct we'll end up leaking random uninitialized data from QEMU's stack into the guest.

Quoted 5 lines

Initializing the struct with "Evt evt = {};" ought to satisfy Coverity and fix the data leak.

Quoted 18 lines
---
 hw/arm/smmuv3.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/arm/smmuv3.c +1 -1
index b3026de..42dc521 100644
@@ -143,7 +143,7 @@ static MemTxResult smmuv3_write_eventq(SMMUv3State *s, Evt *evt)
 
 void smmuv3_record_event(SMMUv3State *s, SMMUEventInfo *info)
 {
-    Evt evt;
+    Evt evt = {};
     MemTxResult r;
 
     if (!smmuv3_eventq_enabled(s)) {
author
#3 lore raw

PATCH 2/2 hw/arm/smmu-common: Fix coverity issue in get_block_pte_address

In reply to Eric Auger

Coverity points out that this can overflow if n > 31, because it's only doing 32-bit arithmetic. Let's use 1ULL instead of 1. Also the formulae used to compute n can be replaced by the level_shift() macro.

---
 hw/arm/smmu-common.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/arm/smmu-common.c +2 -2
index 01c7be8..3c5f724 100644
@@ -83,9 +83,9 @@ static inline hwaddr get_table_pte_address(uint64_t pte, int granule_sz)
 static inline hwaddr get_block_pte_address(uint64_t pte, int level,
                                            int granule_sz, uint64_t *bsz)
 {
-    int n = (granule_sz - 3) * (4 - level) + 3;
+    int n = level_shift(level, granule_sz);
 
-    *bsz = 1 << n;
+    *bsz = 1ULL << n;
     return PTE_ADDRESS(pte, n);
 }
 
Signature · 1 line
1.8.3.1
Reviewed-by on v1 2/2
#4 lore raw

Re: [Qemu-arm] [PATCH 2/2] hw/arm/smmu-common: Fix coverity issue in get_block_pte_address

Hi Eric,

Quoted 4 lines · Eric Auger

Coverity points out that this can overflow if n > 31, because it's only doing 32-bit arithmetic. Let's use 1ULL instead of 1. Also the formulae used to compute n can be replaced by the level_shift() macro.

This level_shift() replacement doesn't seems that obvious to me, can you split it in another patch?

Quoted 12 lines
---
 hw/arm/smmu-common.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/arm/smmu-common.c +0 -0
index 01c7be8..3c5f724 100644

hw/arm/smmu-common.c static inline hwaddr get_table_pte_address(uint64_t pte, int granule_sz) · line 86

Quoted 5 lines
hw/arm/smmu-common.c +1 -1
@@ -83,9 +83,9 @@ static inline hwaddr get_table_pte_address(uint64_t pte, int granule_sz)
 static inline hwaddr get_block_pte_address(uint64_t pte, int level,
                                            int granule_sz, uint64_t *bsz)
 {
-    int n = (granule_sz - 3) * (4 - level) + 3;
+    int n = level_shift(level, granule_sz);

Shouldn't this be level_shift(level + 1, granule_sz)? Using level_shift() you replaced the trailing 3 by granule_sz. This means the previous code was only correct for the granule_sz==3 case.

level_shift(level + 1, granule_sz)
== (granule_sz - 3) * (3 - (level + 1)) + granule_sz; == (granule_sz - 3) * (4 - level) + granule_sz; != (granule_sz - 3) * (4 - level) + 3;

hw/arm/smmu-common.c static inline hwaddr get_table_pte_address(uint64_t pte, int granule_sz) · line 88

Quoted 3 lines
hw/arm/smmu-common.c +1 -1
-    *bsz = 1 << n;
+    *bsz = 1ULL << n;

For the coverity fix (patch splitted):

hw/arm/smmu-common.c static inline hwaddr get_table_pte_address(uint64_t pte, int granule_sz) · line 91

Quoted 3 lines
hw/arm/smmu-common.c +0 -0
     return PTE_ADDRESS(pte, n);
 }
 

Regards,

Phil.

#5 lore raw

Re: [Qemu-devel] [Qemu-arm] [PATCH 2/2] hw/arm/smmu-common: Fix coverity issue in get_block_pte_address

Quoted 18 lines · Philippe Mathieu-Daudé

Hi Eric,

Quoted 4 lines · Eric Auger

Coverity points out that this can overflow if n > 31, because it's only doing 32-bit arithmetic. Let's use 1ULL instead of 1. Also the formulae used to compute n can be replaced by the level_shift() macro.

This level_shift() replacement doesn't seems that obvious to me, can you split it in another patch?

Quoted 7 lines
---
 hw/arm/smmu-common.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 12 lines

hw/arm/smmu-common.c static inline hwaddr get_table_pte_address(uint64_t pte, int granule_sz) · line 86

Quoted 10 lines
hw/arm/smmu-common.c +1 -1
index 01c7be8..3c5f724 100644
@@ -83,9 +83,9 @@ static inline hwaddr get_table_pte_address(uint64_t pte, int granule_sz)
 static inline hwaddr get_block_pte_address(uint64_t pte, int level,
                                            int granule_sz, uint64_t *bsz)
 {
-    int n = (granule_sz - 3) * (4 - level) + 3;
+    int n = level_shift(level, granule_sz);

Shouldn't this be level_shift(level + 1, granule_sz)?

No. The two expressions are equivalent, they're
just arranged differently:

level_shift(lvl, gsz)

      == gsz + (3 - lvl) * (gsz - 3)
      == gsz + (4 - lvl) * (gsz - 3) - (gsz - 3)
      == gsz - gsz + (4 - lvl) * (gsz - 3) + 3
      == (gsz - 3) * (4 - lvl) + 3

thanks
-- PMM

#6 lore raw

Re: [Qemu-arm] [PATCH 2/2] hw/arm/smmu-common: Fix coverity issue in get_block_pte_address

Quoted 32 lines · Peter Maydell
Quoted 18 lines · Philippe Mathieu-Daudé

Hi Eric,

Quoted 4 lines · Eric Auger

Coverity points out that this can overflow if n > 31, because it's only doing 32-bit arithmetic. Let's use 1ULL instead of 1. Also the formulae used to compute n can be replaced by the level_shift() macro.

This level_shift() replacement doesn't seems that obvious to me, can you split it in another patch?

Quoted 7 lines
---
 hw/arm/smmu-common.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 12 lines

hw/arm/smmu-common.c static inline hwaddr get_table_pte_address(uint64_t pte, int granule_sz) · line 86

Quoted 10 lines
hw/arm/smmu-common.c +1 -1
index 01c7be8..3c5f724 100644
@@ -83,9 +83,9 @@ static inline hwaddr get_table_pte_address(uint64_t pte, int granule_sz)
 static inline hwaddr get_block_pte_address(uint64_t pte, int level,
                                            int granule_sz, uint64_t *bsz)
 {
-    int n = (granule_sz - 3) * (4 - level) + 3;
+    int n = level_shift(level, granule_sz);

Shouldn't this be level_shift(level + 1, granule_sz)?

Quoted 8 lines

No. The two expressions are equivalent, they're
just arranged differently:

   level_shift(lvl, gsz)
      == gsz + (3 - lvl) * (gsz - 3)
      == gsz + (4 - lvl) * (gsz - 3) - (gsz - 3)
      == gsz - gsz + (4 - lvl) * (gsz - 3) + 3
      == (gsz - 3) * (4 - lvl) + 3

Argh I failed this middle school demonstrations...

Thanks Peter :)

So for the much cleaner level_shift() use:

author
#7 lore raw

Re: [Qemu-arm] [Qemu-devel] [PATCH 2/2] hw/arm/smmu-common: Fix coverity issue in get_block_pte_address

Hi Philippe,

Quoted 42 lines · Philippe Mathieu-Daudé
Quoted 32 lines · Peter Maydell
Quoted 18 lines · Philippe Mathieu-Daudé

Hi Eric,

Quoted 4 lines · Eric Auger

Coverity points out that this can overflow if n > 31, because it's only doing 32-bit arithmetic. Let's use 1ULL instead of 1. Also the formulae used to compute n can be replaced by the level_shift() macro.

This level_shift() replacement doesn't seems that obvious to me, can you split it in another patch?

Quoted 7 lines
---
 hw/arm/smmu-common.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 12 lines

hw/arm/smmu-common.c static inline hwaddr get_table_pte_address(uint64_t pte, int granule_sz) · line 86

Quoted 10 lines
hw/arm/smmu-common.c +1 -1
index 01c7be8..3c5f724 100644
@@ -83,9 +83,9 @@ static inline hwaddr get_table_pte_address(uint64_t pte, int granule_sz)
 static inline hwaddr get_block_pte_address(uint64_t pte, int level,
                                            int granule_sz, uint64_t *bsz)
 {
-    int n = (granule_sz - 3) * (4 - level) + 3;
+    int n = level_shift(level, granule_sz);

Shouldn't this be level_shift(level + 1, granule_sz)?

Quoted 8 lines

No. The two expressions are equivalent, they're
just arranged differently:

   level_shift(lvl, gsz)
      == gsz + (3 - lvl) * (gsz - 3)
      == gsz + (4 - lvl) * (gsz - 3) - (gsz - 3)
      == gsz - gsz + (4 - lvl) * (gsz - 3) + 3
      == (gsz - 3) * (4 - lvl) + 3
Quoted 6 lines

Argh I failed this middle school demonstrations...

Thanks Peter :)

So for the much cleaner level_shift() use:

Thank you for the review!

Eric

Quoted 1 line
#8 lore raw
In reply to Eric Auger
Quoted 7 lines · Eric Auger

This series includes 2 patches that fix Coverity issues respectively in smmuv3 and smmu-common code.

Eric Auger (2):
hw/arm/smmuv3: Fix Coverity issue in smmuv3_record_event hw/arm/smmu-common: Fix coverity issue in get_block_pte_address

Quoted 5 lines
 hw/arm/smmu-common.c | 4 ++--
 hw/arm/smmuv3.c      | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

--

Applied to target-arm.next, thanks.

-- PMM