Skip to contentSkip to conversation

drm/ssd130x: Use kmalloc_array to prevent overflow of dynamic size calculation

PATCH In review Bhanu Seshu Kumar Valluri 3 participants 3 messages Oct 2, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/ssd130x: Use kmalloc_array to prevent overflow of dynamic size calculation

Use kmalloc_array to avoid potential overflow during dynamic size calculation inside kmalloc.

---
Note:
Patch compiled successfully.
No functionality change is intended.

 drivers/gpu/drm/solomon/ssd130x.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/gpu/drm/solomon/ssd130x.c +3 -3
index eec43d1a5595..8368f0ffbe1e 100644
@@ -1498,7 +1498,7 @@ static int ssd130x_crtc_atomic_check(struct drm_crtc *crtc,
 	if (ret)
 		return ret;
 
-	ssd130x_state->data_array = kmalloc(ssd130x->width * pages, GFP_KERNEL);
+	ssd130x_state->data_array = kmalloc_array(ssd130x->width, pages, GFP_KERNEL);
 	if (!ssd130x_state->data_array)
 		return -ENOMEM;
 
@@ -1519,7 +1519,7 @@ static int ssd132x_crtc_atomic_check(struct drm_crtc *crtc,
 	if (ret)
 		return ret;
 
-	ssd130x_state->data_array = kmalloc(columns * ssd130x->height, GFP_KERNEL);
+	ssd130x_state->data_array = kmalloc_array(columns, ssd130x->height, GFP_KERNEL);
 	if (!ssd130x_state->data_array)
 		return -ENOMEM;
 
@@ -1546,7 +1546,7 @@ static int ssd133x_crtc_atomic_check(struct drm_crtc *crtc,
 
 	pitch = drm_format_info_min_pitch(fi, 0, ssd130x->width);
 
-	ssd130x_state->data_array = kmalloc(pitch * ssd130x->height, GFP_KERNEL);
+	ssd130x_state->data_array = kmalloc_array(pitch, ssd130x->height, GFP_KERNEL);
 	if (!ssd130x_state->data_array)
 		return -ENOMEM;
 
Signature · 1 line
2.34.1
#1 lore raw

Hi

Am 02.10.25 um 03:33 schrieb Bhanu Seshu Kumar Valluri:

Quoted 17 lines

Use kmalloc_array to avoid potential overflow during dynamic size calculation inside kmalloc.

---
Note:
Patch compiled successfully.
No functionality change is intended.

  drivers/gpu/drm/solomon/ssd130x.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
drivers/gpu/drm/solomon/ssd130x.c +0 -0
index eec43d1a5595..8368f0ffbe1e 100644

drivers/gpu/drm/solomon/ssd130x.c static int ssd130x_crtc_atomic_check(struct drm_crtc *crtc, · line 1501

Quoted 5 lines
drivers/gpu/drm/solomon/ssd130x.c +1 -1
@@ -1498,7 +1498,7 @@ static int ssd130x_crtc_atomic_check(struct drm_crtc *crtc,
  	if (ret)
  		return ret;
 
-	ssd130x_state->data_array = kmalloc(ssd130x->width * pages, GFP_KERNEL);
+	ssd130x_state->data_array = kmalloc_array(ssd130x->width, pages, GFP_KERNEL);

The first parameter is the number of elements. The second parameter is the size of an individual element. So the arguments should be swapped. Same for the other changes.

I know it's nitpicking, but who knows what it'll be good for. Fun fact is that even kmalloc_array mixes up both parameters internally.

Best regards
Thomas

Quoted 21 lines
drivers/gpu/drm/solomon/ssd130x.c +2 -2
  	if (!ssd130x_state->data_array)
  		return -ENOMEM;
 
@@ -1519,7 +1519,7 @@ static int ssd132x_crtc_atomic_check(struct drm_crtc *crtc,
  	if (ret)
  		return ret;
 
-	ssd130x_state->data_array = kmalloc(columns * ssd130x->height, GFP_KERNEL);
+	ssd130x_state->data_array = kmalloc_array(columns, ssd130x->height, GFP_KERNEL);
  	if (!ssd130x_state->data_array)
  		return -ENOMEM;
 
@@ -1546,7 +1546,7 @@ static int ssd133x_crtc_atomic_check(struct drm_crtc *crtc,
 
  	pitch = drm_format_info_min_pitch(fi, 0, ssd130x->width);
 
-	ssd130x_state->data_array = kmalloc(pitch * ssd130x->height, GFP_KERNEL);
+	ssd130x_state->data_array = kmalloc_array(pitch, ssd130x->height, GFP_KERNEL);
  	if (!ssd130x_state->data_array)
  		return -ENOMEM;
 
Signature · 7 lines
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstrasse 146, 90461 Nuernberg, Germany
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman
HRB 36809 (AG Nuernberg)
#2 lore raw

Thomas Zimmermann <[email protected]> writes:

Hello Bhanu and Thomas,

Quoted 8 lines

Hi

Am 02.10.25 um 03:33 schrieb Bhanu Seshu Kumar Valluri:

Quoted 5 lines

Use kmalloc_array to avoid potential overflow during dynamic size calculation inside kmalloc.

---

The same patch was posted by another developer a couple of weeks ago and is now queued already in the drm-misc-next branch:

https://cgit.freedesktop.org/drm/drm-misc/commit/?id=940dd88c5f5bdb1f3e19873a856a677ebada63a9

Quoted 22 lines
Quoted 12 lines

Note:
Patch compiled successfully.
No functionality change is intended.

  drivers/gpu/drm/solomon/ssd130x.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
drivers/gpu/drm/solomon/ssd130x.c +0 -0
index eec43d1a5595..8368f0ffbe1e 100644

drivers/gpu/drm/solomon/ssd130x.c static int ssd130x_crtc_atomic_check(struct drm_crtc *crtc, · line 1501

Quoted 5 lines
drivers/gpu/drm/solomon/ssd130x.c +1 -1
@@ -1498,7 +1498,7 @@ static int ssd130x_crtc_atomic_check(struct drm_crtc *crtc,
  	if (ret)
  		return ret;
 
-	ssd130x_state->data_array = kmalloc(ssd130x->width * pages, GFP_KERNEL);
+	ssd130x_state->data_array = kmalloc_array(ssd130x->width, pages, GFP_KERNEL);

The first parameter is the number of elements. The second parameter is the size of an individual element. So the arguments should be swapped. Same for the other changes.

Quoted 3 lines

I know it's nitpicking, but who knows what it'll be good for. Fun fact is that even kmalloc_array mixes up both parameters internally.

You are right. I didn't notice this subtlety when reviewing mentioned patch that pushed to drm-misc-next.

Quoted 4 lines

Best regards
Thomas

Signature · 5 lines
Best regards,

Javier Martinez Canillas
Core Platforms
Red Hat