Skip to contentSkip to conversation

drm/gud: Use size_add() in call to struct_size()

PATCH Applied Gustavo A. R. Silva 3 participants 4 messages Sep 15, 2023 – Sep 29, 2023

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

PATCH 1/1 drm/gud: Use size_add() in call to struct_size()

If, for any reason, the open-coded arithmetic causes a wraparound, the protection that struct_size() adds against potential integer overflows is defeated. Fix this by hardening call to struct_size() with size_add().

Fixes 40e1a70b4aed ("drm: Add GUD USB Display driver")
---
 drivers/gpu/drm/gud/gud_pipe.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/gud/gud_pipe.c +1 -1
index d2f199ea3c11..a02f75be81f0 100644
@@ -503,7 +503,7 @@ int gud_pipe_check(struct drm_simple_display_pipe *pipe,
 		return -ENOENT;
 
 	len = struct_size(req, properties,
-			  GUD_PROPERTIES_MAX_NUM + GUD_CONNECTOR_PROPERTIES_MAX_NUM);
+			  size_add(GUD_PROPERTIES_MAX_NUM, GUD_CONNECTOR_PROPERTIES_MAX_NUM));
 	req = kzalloc(len, GFP_KERNEL);
 	if (!req)
 		return -ENOMEM;
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 15 lines · Gustavo A. R. Silva

If, for any reason, the open-coded arithmetic causes a wraparound, the protection that struct_size() adds against potential integer overflows is defeated. Fix this by hardening call to struct_size() with size_add().

Fixes 40e1a70b4aed ("drm: Add GUD USB Display driver")
---
 drivers/gpu/drm/gud/gud_pipe.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/gud/gud_pipe.c +0 -0
index d2f199ea3c11..a02f75be81f0 100644

drivers/gpu/drm/gud/gud_pipe.c int gud_pipe_check(struct drm_simple_display_pipe *pipe, · line 506

Quoted 5 lines
drivers/gpu/drm/gud/gud_pipe.c +1 -1
@@ -503,7 +503,7 @@ int gud_pipe_check(struct drm_simple_display_pipe *pipe,
 		return -ENOENT;
 
 	len = struct_size(req, properties,
-			  GUD_PROPERTIES_MAX_NUM + GUD_CONNECTOR_PROPERTIES_MAX_NUM);
+			  size_add(GUD_PROPERTIES_MAX_NUM, GUD_CONNECTOR_PROPERTIES_MAX_NUM));

There are both constant expressions, so there's not too much value in wrapping them with size_add(), but for maintaining a common coding style for dealing with allocation sizes, I can be convinced of the change. :)

Quoted 6 lines
drivers/gpu/drm/gud/gud_pipe.c +0 -0
 	req = kzalloc(len, GFP_KERNEL);
 	if (!req)
 		return -ENOMEM;
Signature · 1 line
2.34.1
Signature · 1 line
Kees Cook
#2 lore raw
Quoted 18 lines · Kees Cook
Quoted 17 lines · Gustavo A. R. Silva

If, for any reason, the open-coded arithmetic causes a wraparound, the protection that struct_size() adds against potential integer overflows is defeated. Fix this by hardening call to struct_size() with size_add().

Fixes 40e1a70b4aed ("drm: Add GUD USB Display driver")
---
  drivers/gpu/drm/gud/gud_pipe.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/gud/gud_pipe.c +0 -0
index d2f199ea3c11..a02f75be81f0 100644
@@ -503,7 +503,7 @@ int gud_pipe_check(struct drm_simple_display_pipe *pipe,
  		return -ENOENT;
 
Quoted 7 lines

drivers/gpu/drm/gud/gud_pipe.c int gud_pipe_check(struct drm_simple_display_pipe *pipe, · line 506

Quoted 3 lines
drivers/gpu/drm/gud/gud_pipe.c +1 -1
  	len = struct_size(req, properties,
-			  GUD_PROPERTIES_MAX_NUM + GUD_CONNECTOR_PROPERTIES_MAX_NUM);
+			  size_add(GUD_PROPERTIES_MAX_NUM, GUD_CONNECTOR_PROPERTIES_MAX_NUM));

There are both constant expressions, so there's not too much value in wrapping them with size_add(), but for maintaining a common coding style for dealing with allocation sizes, I can be convinced of the change. :)

Yep; I've found a mix of constant expressions and variables doing open-coded arithmetic in struct_size(), so I'm sending them all.

Quoted 2 lines

Thanks!
--
Gustavo

Quoted 9 lines
Quoted 8 lines
drivers/gpu/drm/gud/gud_pipe.c +0 -0
  	req = kzalloc(len, GFP_KERNEL);
  	if (!req)
  		return -ENOMEM;
Signature · 1 line
2.34.1
Applied
#3 lore raw
In reply to Gustavo A. R. Silva
Quoted 5 lines · Gustavo A. R. Silva

If, for any reason, the open-coded arithmetic causes a wraparound, the protection that struct_size() adds against potential integer overflows is defeated. Fix this by hardening call to struct_size() with size_add().

Applied to for-next/hardening, thanks!

[1/1] drm/gud: Use size_add() in call to struct_size()

      https://git.kernel.org/kees/c/836ccb46073e

Take care,

Signature · 1 line
Kees Cook