ARM SMMUv3: Fix a couple of Coverity issues
PATCH 2/2 hw/arm/smmu-common: Fix coverity issue in get_block_pte_address
In reply to Eric AugerCoverity 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
@@ -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
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
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
@@ -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
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_TYPEand similar setters usedeposit32()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.
Signed-off-by Eric Auger <[email protected]>Reported-by Peter Maydell <[email protected]>
Quoted 18 lines
--- hw/arm/smmuv3.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/arm/smmuv3.c +1
-1@@ -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)) {
Re: [Qemu-arm] [PATCH 2/2] hw/arm/smmu-common: Fix coverity issue in get_block_pte_address
In reply to Eric AugerHi 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
Reported-by Peter Maydell <[email protected]>Signed-off-by Eric Auger <[email protected]>--- hw/arm/smmu-common.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)hw/arm/smmu-common.c +0
-0
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
-0return PTE_ADDRESS (pte, n); }
Regards,
Phil.
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
Reported-by Peter Maydell <[email protected]>Signed-off-by Eric Auger <[email protected]>--- 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@@ -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) + 3thanks
-- PMM
Philippe Mathieu-Daudé
Reviewed-byRe: [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
Reported-by Peter Maydell <[email protected]>Signed-off-by Eric Auger <[email protected]>--- 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@@ -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:
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
Reported-by Peter Maydell <[email protected]>Signed-off-by Eric Auger <[email protected]>--- 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@@ -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:Reviewed-by Philippe Mathieu-Daudé <[email protected]>
Thank you for the review!
Eric
Quoted 1 line
Peter Maydell
AppliedQuoted 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 insmmuv3_record_eventhw/arm/smmu-common: Fix coverity issue inget_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