[PATCH 2/2] EDAC: fsl_ddr: manage mci lifetime via devres to fix remove UAF

From: Rosen Penev

Date: Sun Jul 19 2026 - 15:41:08 EST


fsl_mc_err_probe() requests the shared interrupt with devm_request_irq(),
passing mci as the handler context. At removal fsl_mc_err_remove() calls
edac_mc_free(mci) explicitly, but the devm-requested IRQ is only torn
down by devres afterwards. Between edac_mc_free() and the IRQ release,
another device sharing the line can fire and fsl_mc_isr() will
dereference the freed mci and its pdata.

Register an edac_mc_free() callback via devm_add_action_or_reset() right
after edac_mc_alloc(), so mci is owned by devres and freed only after the
devm IRQ is released (devres runs actions in LIFO order). Drop the
explicit edac_mc_free() from both the probe error paths and
fsl_mc_err_remove(); edac_mc_del_mc() is still called explicitly at
remove time so the edac device is unregistered before the deferred free.

This also lets the now-redundant devres group open/release/remove calls
be removed.

Built for arm64 (defconfig + CONFIG_EDAC_FSL_DDR) with LLVM=1;
drivers/edac/fsl_ddr_edac.o compiles cleanly and passes checkpatch --strict.

Fixes: ea2eb9a8b620 ("EDAC, fsl-ddr: Separate FSL DDR driver from MPC85xx")
Assisted-by: opencode:hy3-free
Signed-off-by: Rosen Penev <rosenp@xxxxxxxxx>
---
drivers/edac/fsl_ddr_edac.c | 36 ++++++++++++++++++++----------------
1 file changed, 20 insertions(+), 16 deletions(-)

diff --git a/drivers/edac/fsl_ddr_edac.c b/drivers/edac/fsl_ddr_edac.c
index b1e6e177b088..cdd129bc42c7 100644
--- a/drivers/edac/fsl_ddr_edac.c
+++ b/drivers/edac/fsl_ddr_edac.c
@@ -490,6 +490,13 @@ static void fsl_ddr_init_csrows(struct mem_ctl_info *mci)
}
}

+static void fsl_mc_edac_free(void *data)
+{
+ struct mem_ctl_info *mci = data;
+
+ edac_mc_free(mci);
+}
+
int fsl_mc_err_probe(struct platform_device *op)
{
struct mem_ctl_info *mci;
@@ -500,9 +507,6 @@ int fsl_mc_err_probe(struct platform_device *op)
u32 sdram_ctl;
int res;

- if (!devres_open_group(&op->dev, fsl_mc_err_probe, GFP_KERNEL))
- return -ENOMEM;
-
layers[0].type = EDAC_MC_LAYER_CHIP_SELECT;
layers[0].size = 4;
layers[0].is_virt_csrow = true;
@@ -511,10 +515,17 @@ int fsl_mc_err_probe(struct platform_device *op)
layers[1].is_virt_csrow = false;
mci = edac_mc_alloc(edac_mc_idx, ARRAY_SIZE(layers), layers,
sizeof(*pdata));
- if (!mci) {
- devres_release_group(&op->dev, fsl_mc_err_probe);
+ if (!mci)
return -ENOMEM;
- }
+
+ /*
+ * Manage mci lifetime via devres so it is freed only after the
+ * devm-requested IRQ is released, avoiding a use-after-free of mci
+ * (and its pdata) in the shared interrupt handler during removal.
+ */
+ res = devm_add_action_or_reset(&op->dev, fsl_mc_edac_free, mci);
+ if (res)
+ return res;

pdata = mci->pvt_info;
pdata->name = "fsl_mc_err";
@@ -558,10 +569,8 @@ int fsl_mc_err_probe(struct platform_device *op)

if (pdata->flag == TYPE_IMX9) {
pdata->inject_vbase = devm_platform_ioremap_resource_byname(op, "inject");
- if (IS_ERR(pdata->inject_vbase)) {
- res = -ENOMEM;
- goto err;
- }
+ if (IS_ERR(pdata->inject_vbase))
+ return -ENOMEM;
}

if (pdata->flag == TYPE_IMX9) {
@@ -575,8 +584,7 @@ int fsl_mc_err_probe(struct platform_device *op)
if ((sdram_ctl & ecc_en_mask) != ecc_en_mask) {
/* no ECC */
pr_warn("%s: No ECC DIMMs discovered\n", __func__);
- res = -ENODEV;
- goto err;
+ return -ENODEV;
}

edac_dbg(3, "init mci\n");
@@ -639,7 +647,6 @@ int fsl_mc_err_probe(struct platform_device *op)
pdata->irq);
}

- devres_remove_group(&op->dev, fsl_mc_err_probe);
edac_dbg(3, "success\n");
pr_info(EDAC_MOD_STR " MC err registered\n");

@@ -652,8 +659,6 @@ int fsl_mc_err_probe(struct platform_device *op)
ddr_out32(pdata, FSL_MC_ERR_DISABLE,
pdata->orig_ddr_err_disable);
ddr_out32(pdata, FSL_MC_ERR_SBE, pdata->orig_ddr_err_sbe);
- devres_release_group(&op->dev, fsl_mc_err_probe);
- edac_mc_free(mci);
return res;
}

@@ -674,5 +679,4 @@ void fsl_mc_err_remove(struct platform_device *op)


edac_mc_del_mc(&op->dev);
- edac_mc_free(mci);
}
--
2.55.0