Skip to contentSkip to conversation

drm/mxsfb: lcdif: Fix use-after-free on unbind

PATCH Applied Fabio Estevam 2 participants 2 messages Nov 24 – Nov 25

Subsystems (5)
lore
Share & tools
lore

Fabio Estevam

author reviewer
#0 lore raw

PATCH v3 1/1 drm/mxsfb: lcdif: Fix use-after-free on unbind

lcdif allocates its private data, which embeds the CRTC and primary plane, and its encoders using devres. These DRM objects are linked into the mode_config lists.

Since mode_config cleanup is DRM-managed, it is deferred until the DRM device is released. An open DRM file can keep the DRM device alive after the platform device has been unbound and devres has freed those objects. The later mode_config cleanup then dereferences freed memory.

Allocate the private data with drmm before initializing mode_config so that it remains alive until mode_config cleanup has completed. Allocate encoders with drmm_plain_encoder_alloc(), which cleans up each encoder and removes it from the mode_config list before freeing its memory.

This can be reproduced on i.MX8MP by keeping the DRM card open across an LCDIF unbind and closing it afterwards.

Fixes 1c71d925c03a ("drm: lcdif: Switch to drmm_mode_config_init")

---
Changes since v2:
- Return the encoder allocation error directly instead of storing it in ret. (Luca)

 drivers/gpu/drm/mxsfb/lcdif_drv.c | 22 +++++++---------------
 1 file changed, 7 insertions(+), 15 deletions(-)
drivers/gpu/drm/mxsfb/lcdif_drv.c +7 -15
index b7be7e17d1d0..2be5a76e936f 100644
@@ -24,6 +24,7 @@
 #include <drm/drm_fbdev_dma.h>
 #include <drm/drm_gem_dma_helper.h>
 #include <drm/drm_gem_framebuffer_helper.h>
+#include <drm/drm_managed.h>
 #include <drm/drm_mode_config.h>
 #include <drm/drm_module.h>
 #include <drm/drm_of.h>
@@ -43,10 +44,6 @@ static const struct drm_mode_config_helper_funcs lcdif_mode_config_helpers = {
 	.atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
 };
 
-static const struct drm_encoder_funcs lcdif_encoder_funcs = {
-	.destroy = drm_encoder_cleanup,
-};
-
 static int lcdif_attach_bridge(struct lcdif_drm_private *lcdif)
 {
 	struct device *dev = lcdif->drm->dev;
@@ -74,19 +71,14 @@ static int lcdif_attach_bridge(struct lcdif_drm_private *lcdif)
 					     "Failed to get bridge for endpoint%u\n",
 					     of_ep.id);
 
-		encoder = devm_kzalloc_obj(dev, *encoder);
-		if (!encoder)
-			return dev_err_probe(dev, -ENOMEM,
-					     "Failed to allocate encoder for endpoint%u\n",
+		encoder = drmm_plain_encoder_alloc(lcdif->drm, NULL,
+						   DRM_MODE_ENCODER_NONE, NULL);
+		if (IS_ERR(encoder))
+			return dev_err_probe(dev, PTR_ERR(encoder),
+					     "Failed to initialize encoder for endpoint%u\n",
 					     of_ep.id);
 
 		encoder->possible_crtcs = drm_crtc_mask(&lcdif->crtc);
-		ret = drm_encoder_init(lcdif->drm, encoder, &lcdif_encoder_funcs,
-				       DRM_MODE_ENCODER_NONE, NULL);
-		if (ret)
-			return dev_err_probe(dev, ret,
-					     "Failed to initialize encoder for endpoint%u\n",
-					     of_ep.id);
 
 		ret = drm_bridge_attach(encoder, bridge, NULL, DRM_BRIDGE_ATTACH_NO_CONNECTOR);
 		if (ret)
@@ -131,7 +123,7 @@ static int lcdif_load(struct drm_device *drm)
 	struct lcdif_drm_private *lcdif;
 	int ret;
 
-	lcdif = devm_kzalloc_obj(&pdev->dev, *lcdif);
+	lcdif = drmm_kzalloc(drm, sizeof(*lcdif), GFP_KERNEL);
 	if (!lcdif)
 		return -ENOMEM;
 
Signature · 1 line
2.43.0
#1 lore raw
Quoted 10 lines · Fabio Estevam

lcdif allocates its private data, which embeds the CRTC and primary plane, and its encoders using devres. These DRM objects are linked into the mode_config lists.

Since mode_config cleanup is DRM-managed, it is deferred until the DRM device is released. An open DRM file can keep the DRM device alive after the platform device has been unbound and devres has freed those objects. The later mode_config cleanup then dereferences freed memory.

[...]

Applied, thanks!

[1/1] drm/mxsfb: lcdif: Fix use-after-free on unbind

      commit: 479e381e77dcde0dfcc9d51e89ff3273da199804

Best regards,

Signature · 3 lines
Luca Ceresoli, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com