Skip to contentSkip to conversation

PNP: fix unintended sign extension on left shifts

PATCH In review Colin King 4 participants 4 messages Oct 14, 2019 – Oct 18, 2019

Subsystems (3)
lore
Share & tools
lore

Colin King

author
#0 lore raw

PATCH 1/1 PNP: fix unintended sign extension on left shifts

From: Colin Ian King <[email protected]>

Shifting a u8 left will cause the value to be promoted to an integer. If the top bit of the u8 is set then the following conversion to a 64 bit resource_size_t will sign extend the value causing the upper 32 bits to be set in the result.

Fix this by casting the u8 value to a resource_size_t before the shift. Original commit is pre-git history.

---
 drivers/pnp/isapnp/core.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
drivers/pnp/isapnp/core.c +12 -6
index 179b737280e1..c947b1673041 100644
@@ -511,10 +511,14 @@ static void __init isapnp_parse_mem32_resource(struct pnp_dev *dev,
 	unsigned char flags;
 
 	isapnp_peek(tmp, size);
-	min = (tmp[4] << 24) | (tmp[3] << 16) | (tmp[2] << 8) | tmp[1];
-	max = (tmp[8] << 24) | (tmp[7] << 16) | (tmp[6] << 8) | tmp[5];
-	align = (tmp[12] << 24) | (tmp[11] << 16) | (tmp[10] << 8) | tmp[9];
-	len = (tmp[16] << 24) | (tmp[15] << 16) | (tmp[14] << 8) | tmp[13];
+	min = ((resource_size_t)tmp[4] << 24) | (tmp[3] << 16) |
+              (tmp[2] << 8) | tmp[1];
+	max = ((resource_size_t)tmp[8] << 24) | (tmp[7] << 16) |
+              (tmp[6] << 8) | tmp[5];
+	align = ((resource_size_t)tmp[12] << 24) | (tmp[11] << 16) |
+              (tmp[10] << 8) | tmp[9];
+	len = ((resource_size_t)tmp[16] << 24) | (tmp[15] << 16) |
+              (tmp[14] << 8) | tmp[13];
 	flags = tmp[0];
 	pnp_register_mem_resource(dev, option_flags,
 				  min, max, align, len, flags);
@@ -532,8 +536,10 @@ static void __init isapnp_parse_fixed_mem32_resource(struct pnp_dev *dev,
 	unsigned char flags;
 
 	isapnp_peek(tmp, size);
-	base = (tmp[4] << 24) | (tmp[3] << 16) | (tmp[2] << 8) | tmp[1];
-	len = (tmp[8] << 24) | (tmp[7] << 16) | (tmp[6] << 8) | tmp[5];
+	base = ((resource_size_t)tmp[4] << 24) | (tmp[3] << 16) |
+	       (tmp[2] << 8) | tmp[1];
+	len = ((resource_size_t)tmp[8] << 24) | (tmp[7] << 16) |
+              (tmp[6] << 8) | tmp[5];
 	flags = tmp[0];
 	pnp_register_mem_resource(dev, option_flags, base, base, 0, len, flags);
 }
Signature · 1 line
2.20.1
#1 lore raw
Quoted 11 lines · Colin King

From: Colin Ian King <[email protected]>

Shifting a u8 left will cause the value to be promoted to an integer. If the top bit of the u8 is set then the following conversion to a 64 bit resource_size_t will sign extend the value causing the upper 32 bits to be set in the result.

Fix this by casting the u8 value to a resource_size_t before the shift. Original commit is pre-git history.

Please resend this with a Cc to [email protected] for easier handling.

Quoted 40 lines
---
  drivers/pnp/isapnp/core.c | 18 ++++++++++++------
  1 file changed, 12 insertions(+), 6 deletions(-)
drivers/pnp/isapnp/core.c +12 -6
index 179b737280e1..c947b1673041 100644
@@ -511,10 +511,14 @@ static void __init isapnp_parse_mem32_resource(struct pnp_dev *dev,
  	unsigned char flags;
 
  	isapnp_peek(tmp, size);
-	min = (tmp[4] << 24) | (tmp[3] << 16) | (tmp[2] << 8) | tmp[1];
-	max = (tmp[8] << 24) | (tmp[7] << 16) | (tmp[6] << 8) | tmp[5];
-	align = (tmp[12] << 24) | (tmp[11] << 16) | (tmp[10] << 8) | tmp[9];
-	len = (tmp[16] << 24) | (tmp[15] << 16) | (tmp[14] << 8) | tmp[13];
+	min = ((resource_size_t)tmp[4] << 24) | (tmp[3] << 16) |
+              (tmp[2] << 8) | tmp[1];
+	max = ((resource_size_t)tmp[8] << 24) | (tmp[7] << 16) |
+              (tmp[6] << 8) | tmp[5];
+	align = ((resource_size_t)tmp[12] << 24) | (tmp[11] << 16) |
+              (tmp[10] << 8) | tmp[9];
+	len = ((resource_size_t)tmp[16] << 24) | (tmp[15] << 16) |
+              (tmp[14] << 8) | tmp[13];
  	flags = tmp[0];
  	pnp_register_mem_resource(dev, option_flags,
  				  min, max, align, len, flags);
@@ -532,8 +536,10 @@ static void __init isapnp_parse_fixed_mem32_resource(struct pnp_dev *dev,
  	unsigned char flags;
 
  	isapnp_peek(tmp, size);
-	base = (tmp[4] << 24) | (tmp[3] << 16) | (tmp[2] << 8) | tmp[1];
-	len = (tmp[8] << 24) | (tmp[7] << 16) | (tmp[6] << 8) | tmp[5];
+	base = ((resource_size_t)tmp[4] << 24) | (tmp[3] << 16) |
+	       (tmp[2] << 8) | tmp[1];
+	len = ((resource_size_t)tmp[8] << 24) | (tmp[7] << 16) |
+              (tmp[6] << 8) | tmp[5];
  	flags = tmp[0];
  	pnp_register_mem_resource(dev, option_flags, base, base, 0, len, flags);
  }
#2 lore raw

Am 15.10.2019 18:29, schrieb Rafael J. Wysocki:

Quoted 50 lines · Colin King
Quoted 11 lines

From: Colin Ian King <[email protected]>

Shifting a u8 left will cause the value to be promoted to an integer. If the top bit of the u8 is set then the following conversion to a 64 bit resource_size_t will sign extend the value causing the upper 32 bits to be set in the result.

Fix this by casting the u8 value to a resource_size_t before the shift. Original commit is pre-git history.

Please resend this with a Cc to [email protected] for easier handling.

Quoted 33 lines
---
  drivers/pnp/isapnp/core.c | 18 ++++++++++++------
  1 file changed, 12 insertions(+), 6 deletions(-)
drivers/pnp/isapnp/core.c +8 -6
index 179b737280e1..c947b1673041 100644
@@ -511,10 +511,14 @@ static void __init
 isapnp_parse_mem32_resource(struct pnp_dev *dev,
      unsigned char flags;
        isapnp_peek(tmp, size);
-    min = (tmp[4] << 24) | (tmp[3] << 16) | (tmp[2] << 8) | tmp[1];
-    max = (tmp[8] << 24) | (tmp[7] << 16) | (tmp[6] << 8) | tmp[5];
-    align = (tmp[12] << 24) | (tmp[11] << 16) | (tmp[10] << 8) | tmp[9];
-    len = (tmp[16] << 24) | (tmp[15] << 16) | (tmp[14] << 8) | tmp[13];
+    min = ((resource_size_t)tmp[4] << 24) | (tmp[3] << 16) |
+              (tmp[2] << 8) | tmp[1];
+    max = ((resource_size_t)tmp[8] << 24) | (tmp[7] << 16) |
+              (tmp[6] << 8) | tmp[5];
+    align = ((resource_size_t)tmp[12] << 24) | (tmp[11] << 16) |
+              (tmp[10] << 8) | tmp[9];
+    len = ((resource_size_t)tmp[16] << 24) | (tmp[15] << 16) |
+              (tmp[14] << 8) | tmp[13];
      flags = tmp[0];
      pnp_register_mem_resource(dev, option_flags,
                    min, max, align, len, flags);
@@ -532,8 +536,10 @@ static void __init
 isapnp_parse_fixed_mem32_resource(struct pnp_dev *dev,
      unsigned char flags;
        isapnp_peek(tmp, size);
-    base = (tmp[4] << 24) | (tmp[3] << 16) | (tmp[2] << 8) | tmp[1];
-    len = (tmp[8] << 24) | (tmp[7] << 16) | (tmp[6] << 8) | tmp[5];
Quoted 10 lines
Quoted 8 lines
drivers/pnp/isapnp/core.c +4 -0
+    base = ((resource_size_t)tmp[4] << 24) | (tmp[3] << 16) |
+           (tmp[2] << 8) | tmp[1];
+    len = ((resource_size_t)tmp[8] << 24) | (tmp[7] << 16) |
+              (tmp[6] << 8) | tmp[5];
      flags = tmp[0];
      pnp_register_mem_resource(dev, option_flags, base, base, 0, len,
 flags);
  }

there was a hint to use get/put_unaligned_be*() maybe that is here also possible ?

re,
wh

(ps: see: [PATCH] scsi: fix unintended sign extension on left shifts)

Quoted 1 line
#3 lore raw
In reply to Colin King
Quoted 42 lines · CEST Colin King

From: Colin Ian King <[email protected]>

Shifting a u8 left will cause the value to be promoted to an integer. If the top bit of the u8 is set then the following conversion to a 64 bit resource_size_t will sign extend the value causing the upper 32 bits to be set in the result.

Fix this by casting the u8 value to a resource_size_t before the shift. Original commit is pre-git history.

---
 drivers/pnp/isapnp/core.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
drivers/pnp/isapnp/core.c +8 -4
index 179b737280e1..c947b1673041 100644
@@ -511,10 +511,14 @@ static void __init isapnp_parse_mem32_resource(struct pnp_dev *dev,
 	unsigned char flags;
 
 	isapnp_peek(tmp, size);
-	min = (tmp[4] << 24) | (tmp[3] << 16) | (tmp[2] << 8) | tmp[1];
-	max = (tmp[8] << 24) | (tmp[7] << 16) | (tmp[6] << 8) | tmp[5];
-	align = (tmp[12] << 24) | (tmp[11] << 16) | (tmp[10] << 8) | tmp[9];
-	len = (tmp[16] << 24) | (tmp[15] << 16) | (tmp[14] << 8) | tmp[13];
+	min = ((resource_size_t)tmp[4] << 24) | (tmp[3] << 16) |
+              (tmp[2] << 8) | tmp[1];
+	max = ((resource_size_t)tmp[8] << 24) | (tmp[7] << 16) |
+              (tmp[6] << 8) | tmp[5];
+	align = ((resource_size_t)tmp[12] << 24) | (tmp[11] << 16) |
+              (tmp[10] << 8) | tmp[9];
+	len = ((resource_size_t)tmp[16] << 24) | (tmp[15] << 16) |
+              (tmp[14] << 8) | tmp[13];
 	flags = tmp[0];
 	pnp_register_mem_resource(dev, option_flags,
 				  min, max, align, len, flags);
@@ -532,8 +536,10 @@ static void __init isapnp_parse_fixed_mem32_resource(struct pnp_dev *dev,
 	unsigned char flags;
 

drivers/pnp/isapnp/core.c static void __init isapnp_parse_fixed_mem32_resource(struct pnp_dev *dev, · line 545

Quoted 10 lines
drivers/pnp/isapnp/core.c +4 -2
 	isapnp_peek(tmp, size);
-	base = (tmp[4] << 24) | (tmp[3] << 16) | (tmp[2] << 8) | tmp[1];
-	len = (tmp[8] << 24) | (tmp[7] << 16) | (tmp[6] << 8) | tmp[5];
+	base = ((resource_size_t)tmp[4] << 24) | (tmp[3] << 16) |
+	       (tmp[2] << 8) | tmp[1];
+	len = ((resource_size_t)tmp[8] << 24) | (tmp[7] << 16) |
+              (tmp[6] << 8) | tmp[5];
 	flags = tmp[0];
 	pnp_register_mem_resource(dev, option_flags, base, base, 0, len, flags);
 }

Can you please respin this with a CC to linux-acpi?