* [PATCH v2] drm/mxsfb: lcdif: Fix use-after-free on unbind
@ 2026-11-23 22:33 Fabio Estevam
2026-11-24 9:36 ` Luca Ceresoli
0 siblings, 1 reply; 4+ messages in thread
From: Fabio Estevam @ 2026-11-23 22:33 UTC (permalink / raw)
To: laurentiu.palcu
Cc: dri-devel, imx, marek.vasut, mripard, Fabio Estevam, stable
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.
Cc: stable@vger.kernel.org
Fixes: 1c71d925c03a ("drm: lcdif: Switch to drmm_mode_config_init")
Reviewed-by: Laurentiu Palcu <laurentiu.palcu@oss.nxp.com>
Signed-off-by: Fabio Estevam <festevam@gmail.com>
---
Changes since v1:
- Rebase onto next-20261123 after the devm_kzalloc_obj() conversion.
- Add Laurentiu's Reviewed-by tag.
drivers/gpu/drm/mxsfb/lcdif_drv.c | 24 +++++++++---------------
1 file changed, 9 insertions(+), 15 deletions(-)
diff --git a/drivers/gpu/drm/mxsfb/lcdif_drv.c b/drivers/gpu/drm/mxsfb/lcdif_drv.c
index b7be7e17d1d0..bc1a9a2fc6bd 100644
--- a/drivers/gpu/drm/mxsfb/lcdif_drv.c
+++ b/drivers/gpu/drm/mxsfb/lcdif_drv.c
@@ -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,16 @@ 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",
- 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)
+ encoder = drmm_plain_encoder_alloc(lcdif->drm, NULL,
+ DRM_MODE_ENCODER_NONE, NULL);
+ if (IS_ERR(encoder)) {
+ ret = PTR_ERR(encoder);
return dev_err_probe(dev, ret,
"Failed to initialize encoder for endpoint%u\n",
of_ep.id);
+ }
+
+ encoder->possible_crtcs = drm_crtc_mask(&lcdif->crtc);
ret = drm_bridge_attach(encoder, bridge, NULL, DRM_BRIDGE_ATTACH_NO_CONNECTOR);
if (ret)
@@ -131,7 +125,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;
base-commit: 3191063649dbf7c0d0b768e1f3655e4c5fc6edb5
--
2.43.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH v2] drm/mxsfb: lcdif: Fix use-after-free on unbind
2026-11-23 22:33 [PATCH v2] drm/mxsfb: lcdif: Fix use-after-free on unbind Fabio Estevam
@ 2026-11-24 9:36 ` Luca Ceresoli
2026-11-24 11:18 ` Fabio Estevam
0 siblings, 1 reply; 4+ messages in thread
From: Luca Ceresoli @ 2026-11-24 9:36 UTC (permalink / raw)
To: Fabio Estevam
Cc: laurentiu.palcu, dri-devel, imx, marek.vasut, mripard, stable
Hello Fabio,
> 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.
>
> Cc: stable@vger.kernel.org
> Fixes: 1c71d925c03a ("drm: lcdif: Switch to drmm_mode_config_init")
> Reviewed-by: Laurentiu Palcu <laurentiu.palcu@oss.nxp.com>
> Signed-off-by: Fabio Estevam <festevam@gmail.com>
I'm OK with the change (except a small nit, see below), but I have a
general question: is there any valid use case for removing the lcdif, other
than testing?
> diff --git a/drivers/gpu/drm/mxsfb/lcdif_drv.c b/drivers/gpu/drm/mxsfb/lcdif_drv.c
> index b7be7e17d1d0..bc1a9a2fc6bd 100644
> --- a/drivers/gpu/drm/mxsfb/lcdif_drv.c
> +++ b/drivers/gpu/drm/mxsfb/lcdif_drv.c
> @@ -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,16 @@ 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",
> - 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)
> + encoder = drmm_plain_encoder_alloc(lcdif->drm, NULL,
> + DRM_MODE_ENCODER_NONE, NULL);
> + if (IS_ERR(encoder)) {
> + ret = PTR_ERR(encoder);
> return dev_err_probe(dev, ret,
> "Failed to initialize encoder for endpoint%u\n",
> of_ep.id);
> + }
No need to use the @ret temporary variable, just replace it with:
if (IS_ERR(encoder))
return dev_err_probe(dev, PTR_ERR(encoder),
"Failed to initialize encoder for endpoint%u\n",
of_ep.id);
With that done you can send v3 and add:
+Reviewed-by: Luca Ceresoli <luca.ceresoli@bootlin.com>
+Tested-by: Luca Ceresoli <luca.ceresoli@bootlin.com>
Luca
--
Luca Ceresoli, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v2] drm/mxsfb: lcdif: Fix use-after-free on unbind
2026-11-24 9:36 ` Luca Ceresoli
@ 2026-11-24 11:18 ` Fabio Estevam
2026-11-24 17:25 ` Luca Ceresoli
0 siblings, 1 reply; 4+ messages in thread
From: Fabio Estevam @ 2026-11-24 11:18 UTC (permalink / raw)
To: Luca Ceresoli
Cc: laurentiu.palcu, dri-devel, imx, marek.vasut, mripard, stable
Hi Luca,
On Tue, Nov 24, 2026 at 6:36 AM Luca Ceresoli <luca.ceresoli@bootlin.com> wrote:
> I'm OK with the change (except a small nit, see below), but I have a
> general question: is there any valid use case for removing the lcdif, other
> than testing?
Yes, the motivation for removing the lcdif was to help decrease power
consumption on an i.MX8MP board.
> No need to use the @ret temporary variable, just replace it with:
>
> if (IS_ERR(encoder))
> return dev_err_probe(dev, PTR_ERR(encoder),
> "Failed to initialize encoder for endpoint%u\n",
> of_ep.id);
>
> With that done you can send v3 and add:
> +Reviewed-by: Luca Ceresoli <luca.ceresoli@bootlin.com>
> +Tested-by: Luca Ceresoli <luca.ceresoli@bootlin.com>
Thanks for the suggestion. I'll send v3.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v2] drm/mxsfb: lcdif: Fix use-after-free on unbind
2026-11-24 11:18 ` Fabio Estevam
@ 2026-11-24 17:25 ` Luca Ceresoli
0 siblings, 0 replies; 4+ messages in thread
From: Luca Ceresoli @ 2026-11-24 17:25 UTC (permalink / raw)
To: Fabio Estevam, Luca Ceresoli
Cc: laurentiu.palcu, dri-devel, imx, marek.vasut, mripard, stable
Hello Fabio,
On Tue Nov 24, 2026 at 12:18 PM CET, Fabio Estevam wrote:
> Hi Luca,
>
> On Tue, Nov 24, 2026 at 6:36 AM Luca Ceresoli <luca.ceresoli@bootlin.com> wrote:
>
>> I'm OK with the change (except a small nit, see below), but I have a
>> general question: is there any valid use case for removing the lcdif, other
>> than testing?
>
> Yes, the motivation for removing the lcdif was to help decrease power
> consumption on an i.MX8MP board.
Uhm, not sure this is the right way to save power... This driver implements
RPM, so in theory it should not consume any resource except allocated
memory when probed but unused.
Did you measure to power consumption in the unbound VS probed-but-unused
cases?
>> No need to use the @ret temporary variable, just replace it with:
>>
>> if (IS_ERR(encoder))
>> return dev_err_probe(dev, PTR_ERR(encoder),
>> "Failed to initialize encoder for endpoint%u\n",
>> of_ep.id);
>>
>> With that done you can send v3 and add:
>> +Reviewed-by: Luca Ceresoli <luca.ceresoli@bootlin.com>
>> +Tested-by: Luca Ceresoli <luca.ceresoli@bootlin.com>
>
> Thanks for the suggestion. I'll send v3.
Anyway the v3 patch makes sense, added it to my TODO-apply list. Thanks!
Luca
--
Luca Ceresoli, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-24 17:25 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 22:33 [PATCH v2] drm/mxsfb: lcdif: Fix use-after-free on unbind Fabio Estevam
2026-11-24 9:36 ` Luca Ceresoli
2026-11-24 11:18 ` Fabio Estevam
2026-11-24 17:25 ` Luca Ceresoli
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox