Skip to contentSkip to conversation

drm/panel: fix null pointer dereference in hx83102_get_modes

PATCH Superseded Charles Han 3 participants 3 messages Aug 22, 2024

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/panel: fix null pointer dereference in hx83102_get_modes

In hx83102_get_modes(), the return value of drm_mode_duplicate() is assigned to mode, which will lead to a possible NULL pointer dereference on failure of drm_mode_duplicate(). Add a check to avoid npd.

Fixes 0ef94554dc40 ("drm/panel: himax-hx83102: Break out as separate driver")
---
 drivers/gpu/drm/panel/panel-himax-hx83102.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/gpu/drm/panel/panel-himax-hx83102.c +4 -0
index 6e4b7e4644ce..7c2a5e9b7fb3 100644
@@ -565,6 +565,10 @@ static int hx83102_get_modes(struct drm_panel *panel,
 	struct drm_display_mode *mode;
 
 	mode = drm_mode_duplicate(connector->dev, m);
+	if (!mode) {
+		dev_err(&ctx->dsi->dev, "bad mode or failed to add mode\n");
+		return -EINVAL;
+	}
 
 	mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
 	drm_mode_set_name(mode);
Signature · 1 line
2.31.1
#1 lore raw

Hi,

Quoted 18 lines · Charles Han <[email protected]> 于2024年8月22日周四 17:34写道:

In hx83102_get_modes(), the return value of drm_mode_duplicate() is assigned to mode, which will lead to a possible NULL pointer dereference on failure of drm_mode_duplicate(). Add a check to avoid npd.

Fixes 0ef94554dc40 ("drm/panel: himax-hx83102: Break out as separate driver")
---
 drivers/gpu/drm/panel/panel-himax-hx83102.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/gpu/drm/panel/panel-himax-hx83102.c +0 -0
index 6e4b7e4644ce..7c2a5e9b7fb3 100644

drivers/gpu/drm/panel/panel-himax-hx83102.c static int hx83102_get_modes(struct drm_panel *panel, · line 571

Quoted 7 lines
drivers/gpu/drm/panel/panel-himax-hx83102.c +4 -0
@@ -565,6 +565,10 @@ static int hx83102_get_modes(struct drm_panel *panel,
        struct drm_display_mode *mode;
 
        mode = drm_mode_duplicate(connector->dev, m);
+       if (!mode) {
+               dev_err(&ctx->dsi->dev, "bad mode or failed to add mode\n");
+               return -EINVAL;
+       }

In my V2 series, Doug suggested:
"nit: no need for an error message when drm_mode_duplicate() fails. It is incredibly unlikely that the allocation will fail and the Linux"

https://lore.kernel.org/all/CAD=FV=V2O2aFDVn5CjbXfgcOLkmNp-G3ChVqQKouB2mDB+NZug@mail.gmail.com/

Quoted 6 lines
drivers/gpu/drm/panel/panel-himax-hx83102.c +0 -1
        mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
        drm_mode_set_name(mode);
--

2.31.1

#2 lore raw

Hi,

Quoted 13 lines · cong yang

Hi,

Quoted 9 lines · Charles Han <[email protected]> 于2024年8月22日周四 17:34写道:

In hx83102_get_modes(), the return value of drm_mode_duplicate() is assigned to mode, which will lead to a possible NULL pointer dereference on failure of drm_mode_duplicate(). Add a check to avoid npd.

Fixes 0ef94554dc40 ("drm/panel: himax-hx83102: Break out as separate driver")

Note: please no blank line between "Fixes" and "Signed-off-by". All tags should be together in the last "paragraph".

Quoted 21 lines
Quoted 9 lines
---
 drivers/gpu/drm/panel/panel-himax-hx83102.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/gpu/drm/panel/panel-himax-hx83102.c +0 -0
index 6e4b7e4644ce..7c2a5e9b7fb3 100644

drivers/gpu/drm/panel/panel-himax-hx83102.c static int hx83102_get_modes(struct drm_panel *panel, · line 571

Quoted 7 lines
drivers/gpu/drm/panel/panel-himax-hx83102.c +4 -0
@@ -565,6 +565,10 @@ static int hx83102_get_modes(struct drm_panel *panel,
        struct drm_display_mode *mode;
 
        mode = drm_mode_duplicate(connector->dev, m);
+       if (!mode) {
+               dev_err(&ctx->dsi->dev, "bad mode or failed to add mode\n");
+               return -EINVAL;
+       }

In my V2 series, Doug suggested:
"nit: no need for an error message when drm_mode_duplicate() fails. It is incredibly unlikely that the allocation will fail and the Linux"

Quoted 1 line

https://lore.kernel.org/all/CAD=FV=V2O2aFDVn5CjbXfgcOLkmNp-G3ChVqQKouB2mDB+NZug@mail.gmail.com/

Sorry for missing that we still need the NULL check and we should definitely add it in. Cong is right, though, that the error message here is pointless. The only way the function can fail is if a small memory allocation fails. Even though such a small allocation failing is basically impossible, kernel policy is still to check for NULL so we should add the check. ...but the kernel already adds a WARN_ON splat for all failed allocations so the extra message here is just wasteful.

-Doug