Skip to contentSkip to conversation

drm/radeon/radeon_connectors: Fix a NULL pointer dereference in radeon_fp_native_mode()

PATCH In review Zhou Qingyang 2 participants 3 messages Nov 30, 2021 – Dec 3, 2021

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

PATCH 1/1 drm/radeon/radeon_connectors: Fix a NULL pointer dereference in radeon_fp_native_mode()

In radeon_fp_native_mode(), the return value of drm_mode_duplicate() is assigned to mode and there is a dereference of it in radeon_fp_native_mode(), which could lead to a NULL pointer dereference on failure of drm_mode_duplicate().

Fix this bug by adding a check of mode.

This bug was found by a static analyzer. The analysis employs differential checking to identify inconsistent security operations (e.g., checks or kfrees) between two code paths and confirms that the inconsistent operations are not recovered in the current function or the callers, so they constitute bugs.

Note that, as a bug found by static analysis, it can be a false positive or hard to trigger. Multiple researchers have cross-reviewed the bug.

Builds with CONFIG_DRM_RADEON=m show no new warnings, and our static analyzer no longer warns about this code.

Fixes d2efdf6d6f42 ("drm/radeon/kms: add cvt mode if we only have lvds w/h and no edid (v4)")
---
 drivers/gpu/drm/radeon/radeon_connectors.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/gpu/drm/radeon/radeon_connectors.c +3 -0
index 607ad5620bd9..49f187614f96 100644
@@ -473,6 +473,9 @@ static struct drm_display_mode *radeon_fp_native_mode(struct drm_encoder *encode
 	    native_mode->vdisplay != 0 &&
 	    native_mode->clock != 0) {
 		mode = drm_mode_duplicate(dev, native_mode);
+		if (!mode)
+			return NULL;
+
 		mode->type = DRM_MODE_TYPE_PREFERRED | DRM_MODE_TYPE_DRIVER;
 		drm_mode_set_name(mode);
 
Signature · 1 line
2.25.1
#1 lore raw
Quoted 32 lines · Zhou Qingyang

In radeon_fp_native_mode(), the return value of drm_mode_duplicate() is assigned to mode and there is a dereference of it in radeon_fp_native_mode(), which could lead to a NULL pointer dereference on failure of drm_mode_duplicate().

Fix this bug by adding a check of mode.

This bug was found by a static analyzer. The analysis employs differential checking to identify inconsistent security operations (e.g., checks or kfrees) between two code paths and confirms that the inconsistent operations are not recovered in the current function or the callers, so they constitute bugs.

Note that, as a bug found by static analysis, it can be a false positive or hard to trigger. Multiple researchers have cross-reviewed the bug.

Builds with CONFIG_DRM_RADEON=m show no new warnings, and our static analyzer no longer warns about this code.

Fixes d2efdf6d6f42 ("drm/radeon/kms: add cvt mode if we only have lvds w/h and no edid (v4)")
---
 drivers/gpu/drm/radeon/radeon_connectors.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/gpu/drm/radeon/radeon_connectors.c +0 -0
index 607ad5620bd9..49f187614f96 100644

drivers/gpu/drm/radeon/radeon_connectors.c static struct drm_display_mode *radeon_fp_native_mode(struct drm_encoder *encode · line 478

Quoted 6 lines
drivers/gpu/drm/radeon/radeon_connectors.c +3 -0
@@ -473,6 +473,9 @@ static struct drm_display_mode *radeon_fp_native_mode(struct drm_encoder *encode
            native_mode->vdisplay != 0 &&
            native_mode->clock != 0) {
                mode = drm_mode_duplicate(dev, native_mode);
+               if (!mode)
+                       return NULL;
+

The else if clause needs a similar check. Care to fix that up as well?

Alex

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

2.25.1

#2 lore raw

PATCH v2 1/1 drm/radeon/radeon_connectors: Fix a NULL pointer dereference in radeon_fp_native_mode()

In radeon_fp_native_mode(), the return value of drm_mode_duplicate() is assigned to mode and there is a dereference of it in radeon_fp_native_mode(), which could lead to a NULL pointer dereference on failure of drm_mode_duplicate().

Fix this bug by adding a check of mode.

This bug was found by a static analyzer. The analysis employs differential checking to identify inconsistent security operations (e.g., checks or kfrees) between two code paths and confirms that the inconsistent operations are not recovered in the current function or the callers, so they constitute bugs.

Note that, as a bug found by static analysis, it can be a false positive or hard to trigger. Multiple researchers have cross-reviewed the bug.

Builds with CONFIG_DRM_RADEON=m show no new warnings, and our static analyzer no longer warns about this code.

Fixes d2efdf6d6f42 ("drm/radeon/kms: add cvt mode if we only have lvds w/h and no edid (v4)")

---
Changes in v2:
- Add a similar check in else clause

 drivers/gpu/drm/radeon/radeon_connectors.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/gpu/drm/radeon/radeon_connectors.c +6 -0
index 607ad5620bd9..7953830cc8b5 100644
@@ -473,6 +473,9 @@ static struct drm_display_mode *radeon_fp_native_mode(struct drm_encoder *encode
 	    native_mode->vdisplay != 0 &&
 	    native_mode->clock != 0) {
 		mode = drm_mode_duplicate(dev, native_mode);
+		if (!mode)
+			return NULL;
+
 		mode->type = DRM_MODE_TYPE_PREFERRED | DRM_MODE_TYPE_DRIVER;
 		drm_mode_set_name(mode);
 
@@ -487,6 +490,9 @@ static struct drm_display_mode *radeon_fp_native_mode(struct drm_encoder *encode
 		 * simpler.
 		 */
 		mode = drm_cvt_mode(dev, native_mode->hdisplay, native_mode->vdisplay, 60, true, false, false);
+		if (!mode)
+			return NULL;
+
 		mode->type = DRM_MODE_TYPE_PREFERRED | DRM_MODE_TYPE_DRIVER;
 		DRM_DEBUG_KMS("Adding cvt approximation of native panel mode %s\n", mode->name);
 	}
Signature · 1 line
2.25.1