Skip to contentSkip to conversation

xen/arm: Avoid integer overflow using MIDR_IMPLEMENTOR_MASK

PATCH Superseded Michal Orzel 3 participants 4 messages Feb 25, 2022

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/arm: Avoid integer overflow using MIDR_IMPLEMENTOR_MASK

Value of macro MIDR_IMPLEMENTOR_MASK exceeds the range of integer and can lead to overflow. Currently there is no issue as it is used in an expression implicitly casted to u32 in MIDR_IS_CPU_MODEL_RANGE. To avoid possible problems, fix the macro.

---
 xen/arch/arm/include/asm/processor.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/arm/include/asm/processor.h +1 -1
index 8ab2940f68..149fae0d27 100644
@@ -39,7 +39,7 @@
 #define MIDR_VARIANT(midr) \
     (((midr) & MIDR_VARIANT_MASK) >> MIDR_VARIANT_SHIFT)
 #define MIDR_IMPLEMENTOR_SHIFT  24
-#define MIDR_IMPLEMENTOR_MASK   (0xff << MIDR_IMPLEMENTOR_SHIFT)
+#define MIDR_IMPLEMENTOR_MASK   (0xffu << MIDR_IMPLEMENTOR_SHIFT)
 #define MIDR_IMPLEMENTOR(midr) \
     (((midr) & MIDR_IMPLEMENTOR_MASK) >> MIDR_IMPLEMENTOR_SHIFT)
 
Signature · 1 line
2.29.0
#1 lore raw

Hi Michal,

Quoted 4 lines · Michal Orzel

Value of macro MIDR_IMPLEMENTOR_MASK exceeds the range of integer and can lead to overflow. Currently there is no issue as it is used in an expression implicitly casted to u32 in MIDR_IS_CPU_MODEL_RANGE. To avoid possible problems, fix the macro.

Correct me if I am wrong, it sounds like this is only for hardening purpose at the moment.

As this code is coming from Linux, I would prefer if we first upstream to Linux and then port to Xen once merged.

Quoted 11 lines
---
  xen/arch/arm/include/asm/processor.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/arm/include/asm/processor.h +0 -0
index 8ab2940f68..149fae0d27 100644

xen/arch/arm/include/asm/processor.h line 42

Quoted 5 lines
xen/arch/arm/include/asm/processor.h +1 -1
@@ -39,7 +39,7 @@
  #define MIDR_VARIANT(midr) \
      (((midr) & MIDR_VARIANT_MASK) >> MIDR_VARIANT_SHIFT)
  #define MIDR_IMPLEMENTOR_SHIFT  24
-#define MIDR_IMPLEMENTOR_MASK   (0xff << MIDR_IMPLEMENTOR_SHIFT)
+#define MIDR_IMPLEMENTOR_MASK   (0xffu << MIDR_IMPLEMENTOR_SHIFT)

NIT: We tend to use 0xffU.

xen/arch/arm/include/asm/processor.h line 45

Quoted 3 lines
xen/arch/arm/include/asm/processor.h +0 -0
  #define MIDR_IMPLEMENTOR(midr) \
      (((midr) & MIDR_IMPLEMENTOR_MASK) >> MIDR_IMPLEMENTOR_SHIFT)
 

Cheers,

Signature · 1 line
Julien Grall
#2 lore raw
Quoted 8 lines · Julien Grall

Hi Michal,

Quoted 4 lines · Michal Orzel

Value of macro MIDR_IMPLEMENTOR_MASK exceeds the range of integer and can lead to overflow. Currently there is no issue as it is used in an expression implicitly casted to u32 in MIDR_IS_CPU_MODEL_RANGE. To avoid possible problems, fix the macro.

Quoted 5 lines

Correct me if I am wrong, it sounds like this is only for hardening purpose at the moment.

As this code is coming from Linux, I would prefer if we first upstream to Linux and then port to Xen once merged.

Well.  The expression is undefined behaviour in C, because of shifting into the sign bit.

In principle, the compiler is free to optimise is_affected_midr_range() to "return true" as a consequence, even if compilers tend not to be that malicious.

~Andrew

#3 lore raw

Hi,

Quoted 15 lines · Andrew Cooper
Quoted 8 lines · Julien Grall

Hi Michal,

Quoted 4 lines · Michal Orzel

Value of macro MIDR_IMPLEMENTOR_MASK exceeds the range of integer and can lead to overflow. Currently there is no issue as it is used in an expression implicitly casted to u32 in MIDR_IS_CPU_MODEL_RANGE. To avoid possible problems, fix the macro.

Quoted 5 lines

Correct me if I am wrong, it sounds like this is only for hardening purpose at the moment.

As this code is coming from Linux, I would prefer if we first upstream to Linux and then port to Xen once merged.

Quoted 6 lines

Well.  The expression is undefined behaviour in C, because of shifting into the sign bit.

In principle, the compiler is free to optimise is_affected_midr_range() to "return true" as a consequence, even if compilers tend not to be that malicious.

Are you arguing against fixing Linux first and the backport it to Xen?

Cheers,

Signature · 1 line
Julien Grall