Skip to contentSkip to conversation

drm/atomic-helper: Validate pointer before dereference

PATCH In review Rodrigo Siqueira 4 participants 5 messages Mar 11, 2019 – Mar 12, 2019

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

PATCH 1/1 drm/atomic-helper: Validate pointer before dereference

The function disable_outputs() and drm_atomic_helper_commit_modeset_enables() tries to retrieve helper_private from the target CRTC, for dereferencing some operations. However, the current implementation does not check whether helper_private is null and, if not, if it has a valid pointer to a dpms and commit functions. This commit adds pointer validations before trying to dereference the dpms and commit function.

---
 drivers/gpu/drm/drm_atomic_helper.c | 30 ++++++++++++++++-------------
 1 file changed, 17 insertions(+), 13 deletions(-)
drivers/gpu/drm/drm_atomic_helper.c +17 -13
index 540a77a2ade9..fbeef7c461fc 100644
@@ -1028,14 +1028,16 @@ disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
 
 
 		/* Right function depends upon target state. */
-		if (new_crtc_state->enable && funcs->prepare)
-			funcs->prepare(crtc);
-		else if (funcs->atomic_disable)
-			funcs->atomic_disable(crtc, old_crtc_state);
-		else if (funcs->disable)
-			funcs->disable(crtc);
-		else
-			funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
+		if (funcs) {
+			if (new_crtc_state->enable && funcs->prepare)
+				funcs->prepare(crtc);
+			else if (funcs->atomic_disable)
+				funcs->atomic_disable(crtc, old_crtc_state);
+			else if (funcs->disable)
+				funcs->disable(crtc);
+			else if (funcs->dpms)
+				funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
+		}
 
 		if (!(dev->irq_enabled && dev->num_crtcs))
 			continue;
@@ -1277,11 +1279,13 @@ void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
 		if (new_crtc_state->enable) {
 			DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
 					 crtc->base.id, crtc->name);
-
-			if (funcs->atomic_enable)
-				funcs->atomic_enable(crtc, old_crtc_state);
-			else
-				funcs->commit(crtc);
+			if (funcs) {
+				if (funcs->atomic_enable)
+					funcs->atomic_enable(crtc,
+							     old_crtc_state);
+				else if (funcs->atomic_enable)
+					funcs->commit(crtc);
+			}
 		}
 	}
 
Signature · 1 line
2.21.0
#1 lore raw

Hi Rodrigo

Quoted 14 lines · Rodrigo Siqueira

The function disable_outputs() and drm_atomic_helper_commit_modeset_enables() tries to retrieve helper_private from the target CRTC, for dereferencing some operations. However, the current implementation does not check whether helper_private is null and, if not, if it has a valid pointer to a dpms and commit functions. This commit adds pointer validations before trying to dereference the dpms and commit function.

---
 drivers/gpu/drm/drm_atomic_helper.c | 30 ++++++++++++++++-------------
 1 file changed, 17 insertions(+), 13 deletions(-)

drivers/gpu/drm/drm_atomic_helper.c void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev, · line 1281

Quoted 3 lines
drivers/gpu/drm/drm_atomic_helper.c +0 -0
@@ -1277,11 +1279,13 @@ void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
 		if (new_crtc_state->enable) {
 			DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
 					 crtc->base.id, crtc->name);

This DEBUG_ print is only relevant if the code actually do something in the following.
So it seems more correct to fix the upper if () to:

Review void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev, · line 1283

Quoted 2 lines
(quoted diff) +0 -0
              if (new_crtc_state->enable && funcs != NULL) {
 			DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
			...

The you also loose one indent and the calls are nicer.

(If used "funcs != NULL", but this is a matter of taste).

Sam

#2 lore raw
In reply to Rodrigo Siqueira

Hi,

drivers/gpu/drm/drm_atomic_helper.c void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev, · line 1286

Quoted 9 lines
drivers/gpu/drm/drm_atomic_helper.c +5 -4
-			if (funcs->atomic_enable)
-				funcs->atomic_enable(crtc, old_crtc_state);
-			else
-				funcs->commit(crtc);
+			if (funcs) {
+				if (funcs->atomic_enable)
+					funcs->atomic_enable(crtc,
+							     old_crtc_state);
+				else if (funcs->atomic_enable)

"if (funcs->commit)" I guess?

drivers/gpu/drm/drm_atomic_helper.c void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev, · line 1288

Quoted 2 lines
drivers/gpu/drm/drm_atomic_helper.c +2 -0
+					funcs->commit(crtc);
+			}

cheers,
Gerd

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#3 lore raw
In reply to Rodrigo Siqueira
Quoted 9 lines · Rodrigo Siqueira

The function disable_outputs() and drm_atomic_helper_commit_modeset_enables() tries to retrieve helper_private from the target CRTC, for dereferencing some operations. However, the current implementation does not check whether helper_private is null and, if not, if it has a valid pointer to a dpms and commit functions. This commit adds pointer validations before trying to dereference the dpms and commit function.

Please also adjust the kerneldoc for these functions. And I think the patch subject can be improved, e.g. "Make ->atomic_enable/disable crtc callbacks optional". Describe what you're trying to achieve in the summary, not how you achieve it.

Quoted 11 lines
---
 drivers/gpu/drm/drm_atomic_helper.c | 30 ++++++++++++++++-------------
 1 file changed, 17 insertions(+), 13 deletions(-)
drivers/gpu/drm/drm_atomic_helper.c +0 -0
index 540a77a2ade9..fbeef7c461fc 100644
@@ -1028,14 +1028,16 @@ disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
 
 

drivers/gpu/drm/drm_atomic_helper.c disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state) · line 1031

Quoted 10 lines
drivers/gpu/drm/drm_atomic_helper.c +1 -8
 		/* Right function depends upon target state. */
-		if (new_crtc_state->enable && funcs->prepare)
-			funcs->prepare(crtc);
-		else if (funcs->atomic_disable)
-			funcs->atomic_disable(crtc, old_crtc_state);
-		else if (funcs->disable)
-			funcs->disable(crtc);
-		else
-			funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
+		if (funcs) {

I don't think making funcs optional is a good idea. If you have a crtc with no functions implemented, it's not terribly useful.

Also making functions optional just here is not going to help if we still require it everywhere else.
-Daniel

Quoted 33 lines
drivers/gpu/drm/drm_atomic_helper.c +16 -5
+			if (new_crtc_state->enable && funcs->prepare)
+				funcs->prepare(crtc);
+			else if (funcs->atomic_disable)
+				funcs->atomic_disable(crtc, old_crtc_state);
+			else if (funcs->disable)
+				funcs->disable(crtc);
+			else if (funcs->dpms)
+				funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
+		}
 
 		if (!(dev->irq_enabled && dev->num_crtcs))
 			continue;
@@ -1277,11 +1279,13 @@ void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
 		if (new_crtc_state->enable) {
 			DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
 					 crtc->base.id, crtc->name);
-
-			if (funcs->atomic_enable)
-				funcs->atomic_enable(crtc, old_crtc_state);
-			else
-				funcs->commit(crtc);
+			if (funcs) {
+				if (funcs->atomic_enable)
+					funcs->atomic_enable(crtc,
+							     old_crtc_state);
+				else if (funcs->atomic_enable)
+					funcs->commit(crtc);
+			}
 		}
 	}
 
Signature · 1 line
2.21.0
Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
#4 lore raw

Hi,

First of all, thanks for the feedback.

I will fix all the problems pointed out in the review.

I just have two inline questions.

Quoted 9 lines · Daniel Vetter
Quoted 8 lines · Rodrigo Siqueira

The function disable_outputs() and drm_atomic_helper_commit_modeset_enables() tries to retrieve helper_private from the target CRTC, for dereferencing some operations. However, the current implementation does not check whether helper_private is null and, if not, if it has a valid pointer to a dpms and commit functions. This commit adds pointer validations before trying to dereference the dpms and commit function.

Quoted 6 lines
Quoted 1 line

Please also adjust the kerneldoc for these functions. And I think the patch subject can be improved, e.g. "Make ->atomic_enable/disable crtc callbacks optional". Describe what you're trying to achieve in the summary, not how you achieve it.

Do I need to add information which says that both functions are optional? I'm asking because the description related to the affected functions and struct looks good for me [1,2,3].

1. Documentation for drm_crtc_helper_funcs:
https://dri.freedesktop.org/docs/drm/gpu/drm-kms-helpers.html?highlight=drm_crtc_helper_funcs#c.drm_crtc_helper_funcs

2. Documentation for drm_atomic_helper_commit_modeset_enables():
https://dri.freedesktop.org/docs/drm/gpu/drm-kms-helpers.html?highlight=drm_atomic_helper_commit_modeset_enables#c.drm_atomic_helper_commit_modeset_enables

3. Documentation for drm_atomic_helper_commit_modeset_disables():
https://dri.freedesktop.org/docs/drm/gpu/drm-kms-helpers.html?highlight=drm_atomic_helper_commit_modeset_disables#c.drm_atomic_helper_commit_modeset_disables

Quoted 25 lines
Quoted 11 lines
---
 drivers/gpu/drm/drm_atomic_helper.c | 30 ++++++++++++++++-------------
 1 file changed, 17 insertions(+), 13 deletions(-)
drivers/gpu/drm/drm_atomic_helper.c +0 -0
index 540a77a2ade9..fbeef7c461fc 100644
@@ -1028,14 +1028,16 @@ disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
 
 

drivers/gpu/drm/drm_atomic_helper.c disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state) · line 1031

Quoted 10 lines
drivers/gpu/drm/drm_atomic_helper.c +1 -8
 		/* Right function depends upon target state. */
-		if (new_crtc_state->enable && funcs->prepare)
-			funcs->prepare(crtc);
-		else if (funcs->atomic_disable)
-			funcs->atomic_disable(crtc, old_crtc_state);
-		else if (funcs->disable)
-			funcs->disable(crtc);
-		else
-			funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
+		if (funcs) {

I don't think making funcs optional is a good idea. If you have a crtc with no functions implemented, it's not terribly useful.

Quoted 2 lines

Also making functions optional just here is not going to help if we still require it everywhere else.

Should I remove the other occurrence of "if (funcs)" inside disable_outputs() and drm_atomic_helper_commit_modeset_enables()? Both functions, already had this before.

Best Regards

Quoted 40 lines

-Daniel

Quoted 33 lines
drivers/gpu/drm/drm_atomic_helper.c +16 -5
+			if (new_crtc_state->enable && funcs->prepare)
+				funcs->prepare(crtc);
+			else if (funcs->atomic_disable)
+				funcs->atomic_disable(crtc, old_crtc_state);
+			else if (funcs->disable)
+				funcs->disable(crtc);
+			else if (funcs->dpms)
+				funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
+		}
 
 		if (!(dev->irq_enabled && dev->num_crtcs))
 			continue;
@@ -1277,11 +1279,13 @@ void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
 		if (new_crtc_state->enable) {
 			DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
 					 crtc->base.id, crtc->name);
-
-			if (funcs->atomic_enable)
-				funcs->atomic_enable(crtc, old_crtc_state);
-			else
-				funcs->commit(crtc);
+			if (funcs) {
+				if (funcs->atomic_enable)
+					funcs->atomic_enable(crtc,
+							     old_crtc_state);
+				else if (funcs->atomic_enable)
+					funcs->commit(crtc);
+			}
 		}
 	}
 
Signature · 1 line
2.21.0
Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
Signature · 5 lines
Rodrigo Siqueira
https://siqueira.tech
Graduate Student
Department of Computer Science
University of São Paulo