[PATCH v2 03/12] EDAC/versalnet: Fix use-after-free in remove_one_mc()
From: Shubhrajyoti Datta
Date: Wed Nov 18 2026 - 09:23:01 EST
device_unregister() drops the last reference on the device and invokes
versal_edac_release() which calls kfree(dev). The subsequent call to
edac_mc_del_mc(mci->pdev) then dereferences the freed pointer.
Fix by saving the device pointer, calling edac_mc_del_mc() and
edac_mc_free() first, then device_unregister() last so the device
is freed only after all users are done with it.
Fixes: d5fe2fec6c40 ("EDAC: Add a driver for the AMD Versal NET DDR controller")
Signed-off-by: Shubhrajyoti Datta <shubhrajyoti.datta@xxxxxxx>
Reviewed-by: Radhey Shyam Pandey <radhey.shyam.pandey@xxxxxxx>
---
Changes in v2:
Add reviewed by tag
drivers/edac/versalnet_edac.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/drivers/edac/versalnet_edac.c b/drivers/edac/versalnet_edac.c
index de97b2ac45aa..2d1417135e42 100644
--- a/drivers/edac/versalnet_edac.c
+++ b/drivers/edac/versalnet_edac.c
@@ -765,11 +765,13 @@ static void versal_edac_release(struct device *dev)
static void remove_one_mc(struct mc_priv *priv, int i)
{
struct mem_ctl_info *mci;
+ struct device *dev;
mci = priv->mci[i];
- device_unregister(mci->pdev);
- edac_mc_del_mc(mci->pdev);
+ dev = mci->pdev;
+ edac_mc_del_mc(dev);
edac_mc_free(mci);
+ device_unregister(dev);
}
static int init_one_mc(struct mc_priv *priv, struct platform_device *pdev, int i)
--
2.44.4