Skip to contentSkip to conversation

pci *_by_mask() coverity fix

PATCH Superseded Peter Maydell 4 participants 9 messages Jul 26, 2022 – Aug 17, 2022

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover pci *_by_mask() coverity fix

This patchset fixes a Coverity nit relating to the pci_set_*_by_mask() helper functions, where we might shift off the end of a variable if the caller passes in a bogus mask argument. Patch 2 is the coverity fix (adding an assert() to help Coverity out a bit and to catch potential future actual bugs). Patch 1 removes the _get_ versions of the functions, because they've been in the tree for a decade and never had any callers at any point in those 10 years :-)

thanks
-- PMM

Peter Maydell (2):
pci: Remove unused pci_get_*_by_mask() functions
pci: Sanity check mask argument to pci_set_*_by_mask()

 include/hw/pci/pci.h | 48 +++++++++++++++-----------------------------
 1 file changed, 16 insertions(+), 32 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/2 pci: Remove unused pci_get_*_by_mask() functions

The helper functions pci_get_{byte,word,long,quad}_by_mask() were added in 2012 in commit c9f50cea70a1596. In the decade since we have never added a single use of them.

The helpers clearly aren't that helpful, so drop them rather than carrying around dead code.

---
 include/hw/pci/pci.h | 28 ----------------------------
 1 file changed, 28 deletions(-)
include/hw/pci/pci.h +0 -28
index b54b6ef88fc..c79144bc5ef 100644
@@ -692,13 +692,6 @@ pci_set_byte_by_mask(uint8_t *config, uint8_t mask, uint8_t reg)
     pci_set_byte(config, (~mask & val) | (mask & rval));
 }
 
-static inline uint8_t
-pci_get_byte_by_mask(uint8_t *config, uint8_t mask)
-{
-    uint8_t val = pci_get_byte(config);
-    return (val & mask) >> ctz32(mask);
-}
-
 static inline void
 pci_set_word_by_mask(uint8_t *config, uint16_t mask, uint16_t reg)
 {
@@ -707,13 +700,6 @@ pci_set_word_by_mask(uint8_t *config, uint16_t mask, uint16_t reg)
     pci_set_word(config, (~mask & val) | (mask & rval));
 }
 
-static inline uint16_t
-pci_get_word_by_mask(uint8_t *config, uint16_t mask)
-{
-    uint16_t val = pci_get_word(config);
-    return (val & mask) >> ctz32(mask);
-}
-
 static inline void
 pci_set_long_by_mask(uint8_t *config, uint32_t mask, uint32_t reg)
 {
@@ -722,13 +708,6 @@ pci_set_long_by_mask(uint8_t *config, uint32_t mask, uint32_t reg)
     pci_set_long(config, (~mask & val) | (mask & rval));
 }
 
-static inline uint32_t
-pci_get_long_by_mask(uint8_t *config, uint32_t mask)
-{
-    uint32_t val = pci_get_long(config);
-    return (val & mask) >> ctz32(mask);
-}
-
 static inline void
 pci_set_quad_by_mask(uint8_t *config, uint64_t mask, uint64_t reg)
 {
@@ -737,13 +716,6 @@ pci_set_quad_by_mask(uint8_t *config, uint64_t mask, uint64_t reg)
     pci_set_quad(config, (~mask & val) | (mask & rval));
 }
 
-static inline uint64_t
-pci_get_quad_by_mask(uint8_t *config, uint64_t mask)
-{
-    uint64_t val = pci_get_quad(config);
-    return (val & mask) >> ctz32(mask);
-}
-
 PCIDevice *pci_new_multifunction(int devfn, bool multifunction,
                                     const char *name);
 PCIDevice *pci_new(int devfn, const char *name);
Signature · 1 line
2.25.1
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] pci: Remove unused pci_get_*_by_mask() functions

Quoted 11 lines · Peter Maydell

The helper functions pci_get_{byte,word,long,quad}_by_mask() were added in 2012 in commit c9f50cea70a1596. In the decade since we have never added a single use of them.

The helpers clearly aren't that helpful, so drop them rather than carrying around dead code.

---
  include/hw/pci/pci.h | 28 ----------------------------
  1 file changed, 28 deletions(-)

r~

#3 lore raw

PATCH 2/2 pci: Sanity check mask argument to pci_set_*_by_mask()

In reply to Peter Maydell

Coverity complains that in functions like pci_set_word_by_mask() we might end up shifting by more than 31 bits. This is true, but only if the caller passes in a zero mask. Help Coverity out by asserting that the mask argument is valid.

Fixes CID 1487168

---
Note that only 1 of these 4 functions is used, and that only in 2 places in the codebase. In both cases the mask argument is a compile-time constant.

---
 include/hw/pci/pci.h | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
include/hw/pci/pci.h +16 -4
index c79144bc5ef..0392b947b8b 100644
@@ -688,7 +688,10 @@ static inline void
 pci_set_byte_by_mask(uint8_t *config, uint8_t mask, uint8_t reg)
 {
     uint8_t val = pci_get_byte(config);
-    uint8_t rval = reg << ctz32(mask);
+    uint8_t rval;
+
+    assert(mask & 0xff);
+    rval = reg << ctz32(mask);
     pci_set_byte(config, (~mask & val) | (mask & rval));
 }
 
@@ -696,7 +699,10 @@ static inline void
 pci_set_word_by_mask(uint8_t *config, uint16_t mask, uint16_t reg)
 {
     uint16_t val = pci_get_word(config);
-    uint16_t rval = reg << ctz32(mask);
+    uint16_t rval;
+
+    assert(mask & 0xffff);
+    rval = reg << ctz32(mask);
     pci_set_word(config, (~mask & val) | (mask & rval));
 }
 
@@ -704,7 +710,10 @@ static inline void
 pci_set_long_by_mask(uint8_t *config, uint32_t mask, uint32_t reg)
 {
     uint32_t val = pci_get_long(config);
-    uint32_t rval = reg << ctz32(mask);
+    uint32_t rval;
+
+    assert(mask);
+    rval = reg << ctz32(mask);
     pci_set_long(config, (~mask & val) | (mask & rval));
 }
 
@@ -712,7 +721,10 @@ static inline void
 pci_set_quad_by_mask(uint8_t *config, uint64_t mask, uint64_t reg)
 {
     uint64_t val = pci_get_quad(config);
-    uint64_t rval = reg << ctz32(mask);
+    uint64_t rval;
+
+    assert(mask);
+    rval = reg << ctz32(mask);
     pci_set_quad(config, (~mask & val) | (mask & rval));
 }
 
Signature · 1 line
2.25.1
Reviewed-by Philippe Mathieu-Daudé on v1 2/2
#4 lore raw

Re: [PATCH 2/2] pci: Sanity check mask argument to pci_set_*_by_mask()

Quoted 7 lines · Peter Maydell

Coverity complains that in functions like pci_set_word_by_mask() we might end up shifting by more than 31 bits. This is true, but only if the caller passes in a zero mask. Help Coverity out by asserting that the mask argument is valid.

Fixes CID 1487168
Quoted 8 lines

---
Note that only 1 of these 4 functions is used, and that only in 2 places in the codebase. In both cases the mask argument is a compile-time constant.

---
  include/hw/pci/pci.h | 20 ++++++++++++++++----
  1 file changed, 16 insertions(+), 4 deletions(-)
Reviewed-by on v1 2/2
#5 lore raw

Re: [PATCH 2/2] pci: Sanity check mask argument to pci_set_*_by_mask()

In reply to Peter Maydell
Quoted 21 lines · Peter Maydell

Coverity complains that in functions like pci_set_word_by_mask() we might end up shifting by more than 31 bits. This is true, but only if the caller passes in a zero mask. Help Coverity out by asserting that the mask argument is valid.

Fixes CID 1487168

---
Note that only 1 of these 4 functions is used, and that only in 2 places in the codebase. In both cases the mask argument is a compile-time constant.

---
  include/hw/pci/pci.h | 20 ++++++++++++++++----
  1 file changed, 16 insertions(+), 4 deletions(-)
include/hw/pci/pci.h +0 -0
index c79144bc5ef..0392b947b8b 100644

include/hw/pci/pci.h static inline void · line 693

Quoted 7 lines
include/hw/pci/pci.h +3 -1
@@ -688,7 +688,10 @@ static inline void
  pci_set_byte_by_mask(uint8_t *config, uint8_t mask, uint8_t reg)
  {
      uint8_t val = pci_get_byte(config);
-    uint8_t rval = reg << ctz32(mask);
+    uint8_t rval;
+
+    assert(mask & 0xff);

Why the and, especially considering the uint8_t type?

include/hw/pci/pci.h static inline void · line 704

Quoted 8 lines
include/hw/pci/pci.h +3 -1
@@ -696,7 +699,10 @@ static inline void
  pci_set_word_by_mask(uint8_t *config, uint16_t mask, uint16_t reg)
  {
      uint16_t val = pci_get_word(config);
-    uint16_t rval = reg << ctz32(mask);
+    uint16_t rval;
+
+    assert(mask & 0xffff);

Similarly.

Otherwise,

r~

#6 lore raw

Re: [PATCH 2/2] pci: Sanity check mask argument to pci_set_*_by_mask()

Quoted 18 lines · Richard Henderson
Quoted 16 lines · Peter Maydell

Coverity complains that in functions like pci_set_word_by_mask() we might end up shifting by more than 31 bits. This is true, but only if the caller passes in a zero mask. Help Coverity out by asserting that the mask argument is valid.

Fixes CID 1487168

---
Note that only 1 of these 4 functions is used, and that only in 2 places in the codebase. In both cases the mask argument is a compile-time constant.

---
  include/hw/pci/pci.h | 20 ++++++++++++++++----
  1 file changed, 16 insertions(+), 4 deletions(-)
Quoted 14 lines

include/hw/pci/pci.h static inline void · line 693

Quoted 12 lines
include/hw/pci/pci.h +3 -1
index c79144bc5ef..0392b947b8b 100644
@@ -688,7 +688,10 @@ static inline void
  pci_set_byte_by_mask(uint8_t *config, uint8_t mask, uint8_t reg)
  {
      uint8_t val = pci_get_byte(config);
-    uint8_t rval = reg << ctz32(mask);
+    uint8_t rval;
+
+    assert(mask & 0xff);

Why the and, especially considering the uint8_t type?

Oops, yes. I think I was mixing up prototypes and thought the mask was passed as a 32-bit value in both these functions.

-- PMM

#7 lore raw

Re: [PATCH 2/2] pci: Sanity check mask argument to pci_set_*_by_mask()

Quoted 35 lines · Peter Maydell
Quoted 18 lines · Richard Henderson
Quoted 16 lines · Peter Maydell

Coverity complains that in functions like pci_set_word_by_mask() we might end up shifting by more than 31 bits. This is true, but only if the caller passes in a zero mask. Help Coverity out by asserting that the mask argument is valid.

Fixes CID 1487168

---
Note that only 1 of these 4 functions is used, and that only in 2 places in the codebase. In both cases the mask argument is a compile-time constant.

---
  include/hw/pci/pci.h | 20 ++++++++++++++++----
  1 file changed, 16 insertions(+), 4 deletions(-)
Quoted 14 lines

include/hw/pci/pci.h static inline void · line 693

Quoted 12 lines
include/hw/pci/pci.h +3 -1
index c79144bc5ef..0392b947b8b 100644
@@ -688,7 +688,10 @@ static inline void
  pci_set_byte_by_mask(uint8_t *config, uint8_t mask, uint8_t reg)
  {
      uint8_t val = pci_get_byte(config);
-    uint8_t rval = reg << ctz32(mask);
+    uint8_t rval;
+
+    assert(mask & 0xff);

Why the and, especially considering the uint8_t type?

Quoted 4 lines

Oops, yes. I think I was mixing up prototypes and thought the mask was passed as a 32-bit value in both these functions.

-- PMM

Did you intend to send v2 of this without the &?

Signature · 1 line
MST
#8 lore raw

Re: [PATCH 2/2] pci: Sanity check mask argument to pci_set_*_by_mask()

Quoted 37 lines · Michael S. Tsirkin
Quoted 35 lines · Peter Maydell
Quoted 32 lines · Richard Henderson
Quoted 21 lines · Peter Maydell

Coverity complains that in functions like pci_set_word_by_mask() we might end up shifting by more than 31 bits. This is true, but only if the caller passes in a zero mask. Help Coverity out by asserting that the mask argument is valid.

Fixes CID 1487168

---
Note that only 1 of these 4 functions is used, and that only in 2 places in the codebase. In both cases the mask argument is a compile-time constant.

---
  include/hw/pci/pci.h | 20 ++++++++++++++++----
  1 file changed, 16 insertions(+), 4 deletions(-)
include/hw/pci/pci.h +0 -0
index c79144bc5ef..0392b947b8b 100644

include/hw/pci/pci.h static inline void · line 693

Quoted 7 lines
include/hw/pci/pci.h +3 -1
@@ -688,7 +688,10 @@ static inline void
  pci_set_byte_by_mask(uint8_t *config, uint8_t mask, uint8_t reg)
  {
      uint8_t val = pci_get_byte(config);
-    uint8_t rval = reg << ctz32(mask);
+    uint8_t rval;
+
+    assert(mask & 0xff);

Why the and, especially considering the uint8_t type?

Quoted 4 lines
Quoted 2 lines

Oops, yes. I think I was mixing up prototypes and thought the mask was passed as a 32-bit value in both these functions.

Did you intend to send v2 of this without the &?

Thanks for the reminder -- I'd forgotten I needed to respin.

-- PMM