Skip to contentSkip to conversation

drm/sti: dvo: Remove bridge on attach failure

PATCH New Myeonghun Pak 2 participants 2 messages Jul 28

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

PATCH 1/1 drm/sti: dvo: Remove bridge on attach failure

drm_bridge_add() registers the bridge globally and takes a reference. If the subsequent drm_bridge_attach() call fails, the component bind callback returns without being considered bound, so its unbind callback is not called. This leaves the bridge registered and its reference held.

Remove the bridge before returning the attach error.

Fixes 384764c36116 ("drm/sti: fixup for bridge interface")
---
 drivers/gpu/drm/sti/sti_dvo.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/sti/sti_dvo.c +3 -1
index 7484d3c3f4..4978e0acbf 100644
@@ -459,8 +459,10 @@ static int sti_dvo_bind(struct device *dev, struct device *master, void *data)
 	drm_bridge_add(&dvo->bridge);
 
 	err = drm_bridge_attach(encoder, &dvo->bridge, NULL, 0);
-	if (err)
+	if (err) {
+		drm_bridge_remove(&dvo->bridge);
 		return err;
+	}
 
 	connector->encoder = encoder;
 	dvo->encoder = encoder;
Signature · 1 line
2.47.1
#1 lore raw
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:

Pre-existing issues:
- [High] The connector structure is allocated using devm_kzalloc in the component bind callback (sti_dvo_bind), leading to memory leaks on bind deferrals and potential use-after-free.
- [High] The return value of drm_connector_init() is ignored, leading to potential use of a partially initialized connector.
- [High] Invalid bitwise copy of a struct device.
- [High] Lockless list manipulation in drm_bridge_add() causes a data race and list corruption when a bridge is re-added.
--

--- Patch [1]: [PATCH] drm/sti: dvo: Remove bridge on attach failure --- commit 1cb37c38eb373898a3e03a40bbc7b0f30ebe733d
Author: Myeonghun Pak <[email protected]>

drm/sti: dvo: Remove bridge on attach failure

This patch removes the bridge before returning the attach error to ensure the globally registered bridge does not leak or remain held when component binding fails.

drivers/gpu/drm/sti/sti_dvo.c

Quoted 3 lines
drivers/gpu/drm/sti/sti_dvo.c +0 -0

[Severity: High]
This is a pre-existing issue, but looking earlier in sti_dvo_bind():

	connector = devm_kzalloc(dev, sizeof(*connector), GFP_KERNEL);

Does using devm_kzalloc() here lead to a memory leak on bind deferrals?

Because devm allocations are tied to the device probe lifecycle rather than the component bind/unbind lifecycle, each bind retry might leak this connector allocation.

Additionally, since DRM reference-counts connectors, freeing this memory automatically upon device unbind rather than during the DRM connector's destruction could lead to a use-after-free if userspace still holds a reference.

drivers/gpu/drm/sti/sti_dvo.c static int sti_dvo_bind(struct device *dev, struct device *master, void *data) · line 465

Quoted 2 lines
drivers/gpu/drm/sti/sti_dvo.c +0 -0
@@ -465,8 +465,10 @@ static int sti_dvo_bind(struct device *dev, struct device *master, void *data)
 	drm_bridge_add(&dvo->bridge);

[Severity: High]
This is a pre-existing issue, but could this call to drm_bridge_add() trigger a data race when a bridge is re-added?

Looking at drm_bridge_add() in drivers/gpu/drm/drm_bridge.c:

	if (!list_empty(&bridge->list))
		list_del_init(&bridge->list);

This list_del_init() is called locklessly. If the bridge is currently on the bridge_lingering_list, concurrent calls to drm_bridge_remove() might be manipulating that list while holding bridge_lock.

Could this lockless deletion corrupt the list pointers of adjacent entries in the global lingering list?

drivers/gpu/drm/sti/sti_dvo.c static int sti_dvo_bind(struct device *dev, struct device *master, void *data) · line 468

Quoted 10 lines
drivers/gpu/drm/sti/sti_dvo.c +3 -1
 	err = drm_bridge_attach(encoder, &dvo->bridge, NULL, 0);
-	if (err)
+	if (err) {
+		drm_bridge_remove(&dvo->bridge);
 		return err;
+	}
 
 	connector->encoder = encoder;
 	dvo->encoder = encoder;

[Severity: High]
This is a pre-existing issue, but looking just below this hunk in sti_dvo_bind():

	drm_connector_init(drm_dev, drm_connector,
			   &sti_dvo_connector_funcs, DRM_MODE_CONNECTOR_LVDS);
	drm_connector_helper_add(drm_connector,
				 &sti_dvo_connector_helper_funcs);

Is it safe to ignore the return value of drm_connector_init() here?

If drm_connector_init() fails due to a memory allocation failure or mode object ID registration failure, it returns a negative error code.

By ignoring it, we pass a partially initialized connector to drm_connector_helper_add() and drm_connector_attach_encoder(), which expect initialized internal lists and object IDs.

[Severity: High]
This is also a pre-existing issue, but looking at sti_dvo_probe() in the same file:

	dvo->dev = pdev->dev;

Does this perform an invalid bitwise copy of a struct device?

A struct device contains internal state elements such as kobject reference counts, mutexes, and list heads.

Copying these by value can lead to corruption if any subsystem attempts to lock the copied mutexes or traverse the copied list heads.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1