[PATCH 1/1] spi: fix use after free in of_spi_notify

From: Xiaohui Zhang
Date: Tue Jun 07 2022 - 11:53:29 EST


We can't use "ctlr->dev" after it has been freed.

Signed-off-by: Xiaohui Zhang <xiaohuizhang@xxxxxxxxxx>
---
drivers/spi/spi.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index b9e2c7e7c580..dadfcdc6c38f 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -4198,14 +4198,15 @@ static int of_spi_notify(struct notifier_block *nb, unsigned long action,
}

spi = of_register_spi_device(ctlr, rd->dn);
- put_device(&ctlr->dev);

if (IS_ERR(spi)) {
pr_err("%s: failed to create for '%pOF'\n",
__func__, rd->dn);
+ put_device(&ctlr->dev);
of_node_clear_flag(rd->dn, OF_POPULATED);
return notifier_from_errno(PTR_ERR(spi));
}
+ put_device(&ctlr->dev);
break;

case OF_RECONFIG_CHANGE_REMOVE:
--
2.17.1