Skip to contentSkip to conversation

drm/arm: fix unintentional integer overflow on left shift

PATCH In review Colin King 4 participants 7 messages Jun 18, 2020

Subsystems (4)
lore
Share & tools
lore

Colin King

author
#0 lore raw

PATCH 1/1 drm/arm: fix unintentional integer overflow on left shift

From: Colin Ian King <[email protected]>

Shifting the integer value 1 is evaluated using 32-bit arithmetic and then used in an expression that expects a long value leads to a potential integer overflow. Fix this by using the BIT macro to perform the shift to avoid the overflow.

Addresses-Coverity: ("Unintentional integer overflow")

Fixes ad49f8602fe8 ("drm/arm: Add support for Mali Display Processors")
---
 drivers/gpu/drm/arm/malidp_planes.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/arm/malidp_planes.c +1 -1
index 37715cc6064e..ab45ac445045 100644
@@ -928,7 +928,7 @@ int malidp_de_planes_init(struct drm_device *drm)
 	const struct malidp_hw_regmap *map = &malidp->dev->hw->map;
 	struct malidp_plane *plane = NULL;
 	enum drm_plane_type plane_type;
-	unsigned long crtcs = 1 << drm->mode_config.num_crtc;
+	unsigned long crtcs = BIT(drm->mode_config.num_crtc);
 	unsigned long flags = DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 | DRM_MODE_ROTATE_180 |
 			      DRM_MODE_ROTATE_270 | DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y;
 	unsigned int blend_caps = BIT(DRM_MODE_BLEND_PIXEL_NONE) |
Signature · 1 line
2.27.0.rc0
maintainer
#1 lore raw
Quoted 1 line · Colin King

From: Colin Ian King <[email protected]>

Hi Colin,

Quoted 4 lines

Shifting the integer value 1 is evaluated using 32-bit arithmetic and then used in an expression that expects a long value leads to a potential integer overflow.

I'm afraid this explanation makes no sense to me. Do you care to explain better what you think the issue is? If the shift is done as 32-bit arithmetic and then promoted to long how does the overflow happen?

Best regards,
Liviu

Quoted 26 lines

Fix this by using the BIT macro to
perform the shift to avoid the overflow.

Addresses-Coverity: ("Unintentional integer overflow")

Fixes ad49f8602fe8 ("drm/arm: Add support for Mali Display Processors")
---
 drivers/gpu/drm/arm/malidp_planes.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/arm/malidp_planes.c +1 -1
index 37715cc6064e..ab45ac445045 100644
@@ -928,7 +928,7 @@ int malidp_de_planes_init(struct drm_device *drm)
 	const struct malidp_hw_regmap *map = &malidp->dev->hw->map;
 	struct malidp_plane *plane = NULL;
 	enum drm_plane_type plane_type;
-	unsigned long crtcs = 1 << drm->mode_config.num_crtc;
+	unsigned long crtcs = BIT(drm->mode_config.num_crtc);
 	unsigned long flags = DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 | DRM_MODE_ROTATE_180 |
 			      DRM_MODE_ROTATE_270 | DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y;
 	unsigned int blend_caps = BIT(DRM_MODE_BLEND_PIXEL_NONE) |
Signature · 1 line
2.27.0.rc0
Signature · 8 lines
==========
| I would like to |
| fix the world,  |
| but they're not |
| giving me the   |
 \ source code!  /
  ---------------
    ¯\_(ツ)_/¯
#2 lore raw
Quoted 6 lines · Liviu Dudau
Quoted 1 line · Colin King

From: Colin Ian King <[email protected]>

Hi Colin,

Quoted 1 line
Quoted 7 lines
Quoted 3 lines

Shifting the integer value 1 is evaluated using 32-bit arithmetic and then used in an expression that expects a long value leads to a potential integer overflow.

I'm afraid this explanation makes no sense to me. Do you care to explain better what you think the issue is? If the shift is done as 32-bit arithmetic and then promoted to long how does the overflow happen?

The shift is performed using 32 bit signed math and then assigned to an unsigned 64 bit long. This if the shift is 31 bits then the signed int conversion of 0x80000000 to unsigned long becomes 0xffffffff80000000. If the shift is more than 32 bits then result overflows and becomes 0x0.

Colin

Quoted 31 lines

Best regards,
Liviu

Quoted 26 lines

Fix this by using the BIT macro to
perform the shift to avoid the overflow.

Addresses-Coverity: ("Unintentional integer overflow")

Fixes ad49f8602fe8 ("drm/arm: Add support for Mali Display Processors")
---
 drivers/gpu/drm/arm/malidp_planes.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/arm/malidp_planes.c +1 -1
index 37715cc6064e..ab45ac445045 100644
@@ -928,7 +928,7 @@ int malidp_de_planes_init(struct drm_device *drm)
 	const struct malidp_hw_regmap *map = &malidp->dev->hw->map;
 	struct malidp_plane *plane = NULL;
 	enum drm_plane_type plane_type;
-	unsigned long crtcs = 1 << drm->mode_config.num_crtc;
+	unsigned long crtcs = BIT(drm->mode_config.num_crtc);
 	unsigned long flags = DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 | DRM_MODE_ROTATE_180 |
 			      DRM_MODE_ROTATE_270 | DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y;
 	unsigned int blend_caps = BIT(DRM_MODE_BLEND_PIXEL_NONE) |
Signature · 1 line
2.27.0.rc0
maintainer Acked-by
#3 lore raw
Quoted 11 lines · Colin Ian King
Quoted 10 lines · Liviu Dudau
Quoted 1 line · Colin King

From: Colin Ian King <[email protected]>

Hi Colin,

Quoted 4 lines

Shifting the integer value 1 is evaluated using 32-bit arithmetic and then used in an expression that expects a long value leads to a potential integer overflow.

Quoted 8 lines
Quoted 3 lines

I'm afraid this explanation makes no sense to me. Do you care to explain better what you think the issue is? If the shift is done as 32-bit arithmetic and then promoted to long how does the overflow happen?

The shift is performed using 32 bit signed math and then assigned to an unsigned 64 bit long. This if the shift is 31 bits then the signed int conversion of 0x80000000 to unsigned long becomes 0xffffffff80000000. If the shift is more than 32 bits then result overflows and becomes 0x0.

You are right, I've missed the fact that it is signed math. Not very likely that we are going to ever have 30 or more CRTCs in the driver, but Coverity has no way of knowing that.

I will pull this into drm-misc-next today.

Best regards,
Liviu

Quoted 39 lines

Colin

Quoted 14 lines

Best regards,
Liviu

Quoted 10 lines

Fix this by using the BIT macro to
perform the shift to avoid the overflow.

Addresses-Coverity: ("Unintentional integer overflow")

Fixes ad49f8602fe8 ("drm/arm: Add support for Mali Display Processors")
---
 drivers/gpu/drm/arm/malidp_planes.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 17 lines
Quoted 16 lines
drivers/gpu/drm/arm/malidp_planes.c +1 -1
index 37715cc6064e..ab45ac445045 100644
@@ -928,7 +928,7 @@ int malidp_de_planes_init(struct drm_device *drm)
 	const struct malidp_hw_regmap *map = &malidp->dev->hw->map;
 	struct malidp_plane *plane = NULL;
 	enum drm_plane_type plane_type;
-	unsigned long crtcs = 1 << drm->mode_config.num_crtc;
+	unsigned long crtcs = BIT(drm->mode_config.num_crtc);
 	unsigned long flags = DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 | DRM_MODE_ROTATE_180 |
 			      DRM_MODE_ROTATE_270 | DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y;
 	unsigned int blend_caps = BIT(DRM_MODE_BLEND_PIXEL_NONE) |
Signature · 1 line
2.27.0.rc0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

Signature · 8 lines
==========
| I would like to |
| fix the world,  |
| but they're not |
| giving me the   |
 \ source code!  /
  ---------------
    ¯\_(ツ)_/¯
#4 lore raw

Hi all, newbie here.
Can the BIT macro be safely used on other parts of the kernel as well? Just using git grep "1 <<" returns a ton of results where bit shifting is used the old fashioned way.

Am Do., 18. Juni 2020 um 16:23 Uhr schrieb Liviu Dudau <[email protected]>:

Quoted 82 lines · Colin Ian King
Quoted 11 lines · Liviu Dudau
Quoted 10 lines · Colin King
Quoted 1 line

From: Colin Ian King <[email protected]>

Hi Colin,

Quoted 4 lines

Shifting the integer value 1 is evaluated using 32-bit arithmetic and then used in an expression that expects a long value leads to a potential integer overflow.

Quoted 8 lines
Quoted 3 lines

I'm afraid this explanation makes no sense to me. Do you care to explain better what you think the issue is? If the shift is done as 32-bit arithmetic and then promoted to long how does the overflow happen?

The shift is performed using 32 bit signed math and then assigned to an unsigned 64 bit long. This if the shift is 31 bits then the signed int conversion of 0x80000000 to unsigned long becomes 0xffffffff80000000. If the shift is more than 32 bits then result overflows and becomes 0x0.

You are right, I've missed the fact that it is signed math. Not very likely that we are going to ever have 30 or more CRTCs in the driver, but Coverity has no way of knowing that.

I will pull this into drm-misc-next today.

Best regards,
Liviu

Quoted 35 lines

Colin

Quoted 14 lines

Best regards,
Liviu

Quoted 10 lines

Fix this by using the BIT macro to
perform the shift to avoid the overflow.

Addresses-Coverity: ("Unintentional integer overflow")

Fixes ad49f8602fe8 ("drm/arm: Add support for Mali Display Processors")
---
 drivers/gpu/drm/arm/malidp_planes.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 17 lines
Quoted 14 lines
drivers/gpu/drm/arm/malidp_planes.c +1 -2
index 37715cc6064e..ab45ac445045 100644
@@ -928,7 +928,7 @@ int malidp_de_planes_init(struct drm_device *drm)
   const struct malidp_hw_regmap *map = &malidp->dev->hw->map;
   struct malidp_plane *plane = NULL;
   enum drm_plane_type plane_type;
-  unsigned long crtcs = 1 << drm->mode_config.num_crtc;
+  unsigned long crtcs = BIT(drm->mode_config.num_crtc);
   unsigned long flags = DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 | DRM_MODE_ROTATE_180 |
                         DRM_MODE_ROTATE_270 | DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y;
   unsigned int blend_caps = BIT(DRM_MODE_BLEND_PIXEL_NONE) |
--
Quoted 2 lines

2.27.0.rc0

Quoted 4 lines

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

--
====================

| I would like to |
| fix the world,  |
| but they're not |
| giving me the   |
 \ source code!  /
  ---------------
    ¯\_(ツ)_/¯
#5 lore raw
Quoted 4 lines · Garrit Franke

Hi all, newbie here.
Can the BIT macro be safely used on other parts of the kernel as well? Just using git grep "1 <<" returns a ton of results where bit shifting is used the old fashioned way.

The BIT macro casts the 1 it a UL before shifting so it catches these type of bugs. use BIT_ULL when the result is assigned to a long long.

Colin

Quoted 84 lines

Am Do., 18. Juni 2020 um 16:23 Uhr schrieb Liviu Dudau <[email protected]>:

Quoted 82 lines · Colin Ian King
Quoted 11 lines · Liviu Dudau
Quoted 10 lines · Colin King
Quoted 1 line

From: Colin Ian King <[email protected]>

Hi Colin,

Quoted 4 lines

Shifting the integer value 1 is evaluated using 32-bit arithmetic and then used in an expression that expects a long value leads to a potential integer overflow.

Quoted 8 lines
Quoted 3 lines

I'm afraid this explanation makes no sense to me. Do you care to explain better what you think the issue is? If the shift is done as 32-bit arithmetic and then promoted to long how does the overflow happen?

The shift is performed using 32 bit signed math and then assigned to an unsigned 64 bit long. This if the shift is 31 bits then the signed int conversion of 0x80000000 to unsigned long becomes 0xffffffff80000000. If the shift is more than 32 bits then result overflows and becomes 0x0.

You are right, I've missed the fact that it is signed math. Not very likely that we are going to ever have 30 or more CRTCs in the driver, but Coverity has no way of knowing that.

I will pull this into drm-misc-next today.

Best regards,
Liviu

Quoted 35 lines

Colin

Quoted 14 lines

Best regards,
Liviu

Quoted 10 lines

Fix this by using the BIT macro to
perform the shift to avoid the overflow.

Addresses-Coverity: ("Unintentional integer overflow")

Fixes ad49f8602fe8 ("drm/arm: Add support for Mali Display Processors")
---
 drivers/gpu/drm/arm/malidp_planes.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 17 lines
Quoted 14 lines
drivers/gpu/drm/arm/malidp_planes.c +1 -2
index 37715cc6064e..ab45ac445045 100644
@@ -928,7 +928,7 @@ int malidp_de_planes_init(struct drm_device *drm)
   const struct malidp_hw_regmap *map = &malidp->dev->hw->map;
   struct malidp_plane *plane = NULL;
   enum drm_plane_type plane_type;
-  unsigned long crtcs = 1 << drm->mode_config.num_crtc;
+  unsigned long crtcs = BIT(drm->mode_config.num_crtc);
   unsigned long flags = DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 | DRM_MODE_ROTATE_180 |
                         DRM_MODE_ROTATE_270 | DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y;
   unsigned int blend_caps = BIT(DRM_MODE_BLEND_PIXEL_NONE) |
--
Quoted 2 lines

2.27.0.rc0

Quoted 4 lines

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

--
==========

| I would like to |
| fix the world,  |
| but they're not |
| giving me the   |
 \ source code!  /
  ---------------
    ¯\_(ツ)_/¯
#6 lore raw
In reply to Garrit Franke
Quoted 4 lines · Garrit Franke

Hi all, newbie here.
Can the BIT macro be safely used on other parts of the kernel as well? Just using git grep "1 <<" returns a ton of results where bit shifting is used the old fashioned way.

Yeah. There is a checkpatch warning for it and everything. :)

But I like the way you think. Start with patches to staging though. People don't necessarily like doing cleanups on ancient code.

regards,
dan carpenter