Skip to contentSkip to conversation

drm/sysfb: Fix visible size and related checks

PATCH In review Thomas Zimmermann 2 participants 7 messages Jun 18

Subsystems (4)
lore
Share & tools
lore

Thomas Zimmermann

author maintainer
#0 lore raw

v2 cover drm/sysfb: Fix visible size and related checks

We need the system framebuffer's visible size in efidrm and vesadrm. Fix the size calculation for some corner cases and handle errors correctly.

v2:
- fix trunctions from type conversions (sashiko)

Thomas Zimmermann (4):
drm/sysfb: Do not page-align visible size of the framebuffer drm/sysfb: Avoid possible truncation with calculating visible size drm/sysfb: Return errno code from drm_sysfb_get_visible_size() drm/sysfb: Avoid truncating maximum stride

 drivers/gpu/drm/sysfb/drm_sysfb_helper.h      |  2 +-
 drivers/gpu/drm/sysfb/drm_sysfb_screen_info.c | 13 ++++++++++---
 drivers/gpu/drm/sysfb/efidrm.c                |  7 ++++---
 drivers/gpu/drm/sysfb/vesadrm.c               |  6 +++---
 4 files changed, 18 insertions(+), 10 deletions(-)

base-commit: fc59f76558703febba8056be87d1c97d14f7485e

Signature · 1 line
2.54.0
author maintainer
#1 lore raw

PATCH v2 1/4 drm/sysfb: Do not page-align visible size of the framebuffer

Only return the actually visible size of the system framebuffer in drm_sysfb_get_visible_size_si(). Drivers use this size value for reserving access to framebuffer memory. Increasing the value can make later attempts to do so fail.

Fixes 32ae90c66fb6 ("drm/sysfb: Add efidrm for EFI displays")
Fixes a84eb6abe2b6 ("drm/sysfb: Add vesadrm for VESA displays")
Cc Thomas Zimmermann <[email protected]>
Cc Javier Martinez Canillas <[email protected]>
Cc <[email protected]> # v6.16+
---
 drivers/gpu/drm/sysfb/drm_sysfb_screen_info.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/sysfb/drm_sysfb_screen_info.c +1 -1
index 749290196c6a..361b7233600c 100644
@@ -67,7 +67,7 @@ EXPORT_SYMBOL(drm_sysfb_get_stride_si);
 u64 drm_sysfb_get_visible_size_si(struct drm_device *dev, const struct screen_info *si,
 				  unsigned int height, unsigned int stride, u64 size)
 {
-	u64 vsize = PAGE_ALIGN(height * stride);
+	u64 vsize = height * stride;
 
 	return drm_sysfb_get_validated_size0(dev, "visible size", vsize, size);
 }
Signature · 1 line
2.54.0
author maintainer
#2 lore raw

PATCH v2 2/4 drm/sysfb: Avoid possible truncation with calculating visible size

In reply to Thomas Zimmermann

Calculating the visible size of the system framebuffer can result in truncation of the result. The calculation uses 32-bit arithmetics, which can overflow if the values for height and stride are large. Fix the issue by multiplying with mul_u32_u32().

Fixes 32ae90c66fb6 ("drm/sysfb: Add efidrm for EFI displays")
Fixes a84eb6abe2b6 ("drm/sysfb: Add vesadrm for VESA displays")
Cc Thomas Zimmermann <[email protected]>
Cc Javier Martinez Canillas <[email protected]>
Cc <[email protected]> # v6.16+

---
I've added Reported-by and Closes tags because this is a pre-existing issue.

---
 drivers/gpu/drm/sysfb/drm_sysfb_screen_info.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/sysfb/drm_sysfb_screen_info.c +2 -1
index 361b7233600c..8b14eaa304c0 100644
@@ -2,6 +2,7 @@
 
 #include <linux/export.h>
 #include <linux/limits.h>
+#include <linux/math64.h>
 #include <linux/minmax.h>
 #include <linux/screen_info.h>
 
@@ -67,7 +68,7 @@ EXPORT_SYMBOL(drm_sysfb_get_stride_si);
 u64 drm_sysfb_get_visible_size_si(struct drm_device *dev, const struct screen_info *si,
 				  unsigned int height, unsigned int stride, u64 size)
 {
-	u64 vsize = height * stride;
+	u64 vsize = mul_u32_u32(height, stride);
 
 	return drm_sysfb_get_validated_size0(dev, "visible size", vsize, size);
 }
Signature · 1 line
2.54.0
maintainer Reviewed-by on v2 2/4
#3 lore raw

Re: [PATCH v2 2/4] drm/sysfb: Avoid possible truncation with calculating visible size

Thomas Zimmermann <[email protected]> writes:

Hello Thomas,

Quoted 14 lines

Calculating the visible size of the system framebuffer can result in truncation of the result. The calculation uses 32-bit arithmetics, which can overflow if the values for height and stride are large. Fix the issue by multiplying with mul_u32_u32().

Fixes 32ae90c66fb6 ("drm/sysfb: Add efidrm for EFI displays")
Fixes a84eb6abe2b6 ("drm/sysfb: Add vesadrm for VESA displays")
Cc Thomas Zimmermann <[email protected]>
Cc Javier Martinez Canillas <[email protected]>
Cc <[email protected]> # v6.16+
Quoted 6 lines

---
I've added Reported-by and Closes tags because this is a pre-existing issue.

---
 drivers/gpu/drm/sysfb/drm_sysfb_screen_info.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Signature · 5 lines
Best regards,

Javier Martinez Canillas
Core Platforms
Red Hat
author maintainer
#4 lore raw

PATCH v2 3/4 drm/sysfb: Return errno code from drm_sysfb_get_visible_size()

In reply to Thomas Zimmermann

Change the return type of drm_sysfb_get_visible_size() to s64 so that it returns a possible errno code from _get_validated_size0(). Fix callers to handle the errno code.

The currently returned unsigned type converts an errno code to a very large size value, which drivers interpret as visible size of the system framebuffer. Later efforts to reserve the framebuffer resource fail.

The bug has been present since efidrm and vesadrm got merged. It was then part of each driver.

Fixes 32ae90c66fb6 ("drm/sysfb: Add efidrm for EFI displays")
Fixes a84eb6abe2b6 ("drm/sysfb: Add vesadrm for VESA displays")
Cc Thomas Zimmermann <[email protected]>
Cc Javier Martinez Canillas <[email protected]>
Cc <[email protected]> # v6.16+
---
 drivers/gpu/drm/sysfb/drm_sysfb_helper.h      | 2 +-
 drivers/gpu/drm/sysfb/drm_sysfb_screen_info.c | 2 +-
 drivers/gpu/drm/sysfb/efidrm.c                | 7 ++++---
 drivers/gpu/drm/sysfb/vesadrm.c               | 6 +++---
 4 files changed, 9 insertions(+), 8 deletions(-)
drivers/gpu/drm/sysfb/drm_sysfb_helper.h +1 -1
index 2a2b553366fb..547f2327af5e 100644
@@ -50,7 +50,7 @@ struct resource *drm_sysfb_get_memory_si(struct drm_device *dev,
 int drm_sysfb_get_stride_si(struct drm_device *dev, const struct screen_info *si,
 			    const struct drm_format_info *format,
 			    unsigned int width, unsigned int height, u64 size);
-u64 drm_sysfb_get_visible_size_si(struct drm_device *dev, const struct screen_info *si,
+s64 drm_sysfb_get_visible_size_si(struct drm_device *dev, const struct screen_info *si,
 				  unsigned int height, unsigned int stride, u64 size);
 #endif
 
drivers/gpu/drm/sysfb/drm_sysfb_screen_info.c +1 -1
index 8b14eaa304c0..042d1b796696 100644
@@ -65,7 +65,7 @@ int drm_sysfb_get_stride_si(struct drm_device *dev, const struct screen_info *si
 }
 EXPORT_SYMBOL(drm_sysfb_get_stride_si);
 
-u64 drm_sysfb_get_visible_size_si(struct drm_device *dev, const struct screen_info *si,
+s64 drm_sysfb_get_visible_size_si(struct drm_device *dev, const struct screen_info *si,
 				  unsigned int height, unsigned int stride, u64 size)
 {
 	u64 vsize = mul_u32_u32(height, stride);
drivers/gpu/drm/sysfb/efidrm.c +4 -3
index 1a1e36700976..3f9cd5d03efb 100644
@@ -152,7 +152,8 @@ static struct efidrm_device *efidrm_device_create(struct drm_driver *drv,
 	const struct screen_info *si;
 	const struct drm_format_info *format;
 	int width, height, stride;
-	u64 vsize, mem_flags;
+	s64 vsize;
+	u64 mem_flags;
 	struct resource resbuf;
 	struct resource *res;
 	struct efidrm_device *efi;
@@ -206,8 +207,8 @@ static struct efidrm_device *efidrm_device_create(struct drm_driver *drv,
 	if (stride < 0)
 		return ERR_PTR(stride);
 	vsize = drm_sysfb_get_visible_size_si(dev, si, height, stride, resource_size(res));
-	if (!vsize)
-		return ERR_PTR(-EINVAL);
+	if (vsize < 0)
+		return ERR_PTR(vsize);
 
 	drm_dbg(dev, "framebuffer format=%p4cc, size=%dx%d, stride=%d bytes\n",
 		&format->format, width, height, stride);
drivers/gpu/drm/sysfb/vesadrm.c +3 -3
index dbc317778d54..6a67b2d2e451 100644
@@ -402,7 +402,7 @@ static struct vesadrm_device *vesadrm_device_create(struct drm_driver *drv,
 	const struct screen_info *si;
 	const struct drm_format_info *format;
 	int width, height, stride;
-	u64 vsize;
+	s64 vsize;
 	struct resource resbuf;
 	struct resource *res;
 	struct vesadrm_device *vesa;
@@ -457,8 +457,8 @@ static struct vesadrm_device *vesadrm_device_create(struct drm_driver *drv,
 	if (stride < 0)
 		return ERR_PTR(stride);
 	vsize = drm_sysfb_get_visible_size_si(dev, si, height, stride, resource_size(res));
-	if (!vsize)
-		return ERR_PTR(-EINVAL);
+	if (vsize < 0)
+		return ERR_PTR(vsize);
 
 	drm_dbg(dev, "framebuffer format=%p4cc, size=%dx%d, stride=%d bytes\n",
 		&format->format, width, height, stride);
Signature · 1 line
2.54.0
author maintainer
#5 lore raw

PATCH v2 4/4 drm/sysfb: Avoid truncating maximum stride

In reply to Thomas Zimmermann

Passing a maximum as 64-bit type to drm_sysfb_get_validated_int0() can truncate the value to 32 bits. Use drm_sysfb_get_validated_size0(), which uses 64-bit arithmetics. Then test the returned stride against the limits of int to avoid truncations in the returned value. A valid stride is in the range of [1, INT_MAX] inclusive.

Fixes 32ae90c66fb6 ("drm/sysfb: Add efidrm for EFI displays")
Fixes a84eb6abe2b6 ("drm/sysfb: Add vesadrm for VESA displays")
Cc Thomas Zimmermann <[email protected]>
Cc Javier Martinez Canillas <[email protected]>
Cc <[email protected]> # v6.16+

---
I've added Reported-by and Closes tags because this is a pre-existing issue.

---
 drivers/gpu/drm/sysfb/drm_sysfb_screen_info.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/gpu/drm/sysfb/drm_sysfb_screen_info.c +7 -1
index 042d1b796696..1c479ce0e503 100644
@@ -57,11 +57,17 @@ int drm_sysfb_get_stride_si(struct drm_device *dev, const struct screen_info *si
 			    unsigned int width, unsigned int height, u64 size)
 {
 	u64 lfb_linelength = si->lfb_linelength;
+	s64 stride;
 
 	if (!lfb_linelength)
 		lfb_linelength = drm_format_info_min_pitch(format, 0, width);
 
-	return drm_sysfb_get_validated_int0(dev, "stride", lfb_linelength, div64_u64(size, height));
+	stride = drm_sysfb_get_validated_size0(dev, "stride", lfb_linelength,
+					       div64_u64(size, height));
+	if (stride < INT_MIN || stride > INT_MAX)
+		return -EINVAL;
+
+	return (int)stride; /* stride or negative errno code */
 }
 EXPORT_SYMBOL(drm_sysfb_get_stride_si);
 
Signature · 1 line
2.54.0
maintainer Reviewed-by on v2 4/4
#6 lore raw

Re: [PATCH v2 4/4] drm/sysfb: Avoid truncating maximum stride

Quoted 15 lines · Thomas Zimmermann

Passing a maximum as 64-bit type to drm_sysfb_get_validated_int0() can truncate the value to 32 bits. Use drm_sysfb_get_validated_size0(), which uses 64-bit arithmetics. Then test the returned stride against the limits of int to avoid truncations in the returned value. A valid stride is in the range of [1, INT_MAX] inclusive.

Fixes 32ae90c66fb6 ("drm/sysfb: Add efidrm for EFI displays")
Fixes a84eb6abe2b6 ("drm/sysfb: Add vesadrm for VESA displays")
Cc Thomas Zimmermann <[email protected]>
Cc Javier Martinez Canillas <[email protected]>
Cc <[email protected]> # v6.16+
Quoted 6 lines

---
I've added Reported-by and Closes tags because this is a pre-existing issue.

---
 drivers/gpu/drm/sysfb/drm_sysfb_screen_info.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
Signature · 5 lines
Best regards,

Javier Martinez Canillas
Core Platforms
Red Hat