mtd: rawnand: cadence: fix possible null-ptr-deref in cadence_nand_dt_probe()
authorYang Yingliang <yangyingliang@huawei.com>
Tue, 26 Apr 2022 08:49:11 +0000 (16:49 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 9 Jun 2022 08:22:51 +0000 (10:22 +0200)
[ Upstream commit a28ed09dafee20da51eb26452950839633afd824 ]

It will cause null-ptr-deref when using 'res', if platform_get_resource()
returns NULL, so move using 'res' after devm_ioremap_resource() that
will check it to avoid null-ptr-deref.
And use devm_platform_get_and_ioremap_resource() to simplify code.

Fixes: ec4ba01e894d ("mtd: rawnand: Add new Cadence NAND driver to MTD subsystem")
Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
Link: https://lore.kernel.org/linux-mtd/20220426084913.4021868-1-yangyingliang@huawei.com
Signed-off-by: Sasha Levin <sashal@kernel.org>
drivers/mtd/nand/raw/cadence-nand-controller.c

index 7eec60e..0d72672 100644 (file)
@@ -2983,11 +2983,10 @@ static int cadence_nand_dt_probe(struct platform_device *ofdev)
        if (IS_ERR(cdns_ctrl->reg))
                return PTR_ERR(cdns_ctrl->reg);
 
-       res = platform_get_resource(ofdev, IORESOURCE_MEM, 1);
-       cdns_ctrl->io.dma = res->start;
-       cdns_ctrl->io.virt = devm_ioremap_resource(&ofdev->dev, res);
+       cdns_ctrl->io.virt = devm_platform_get_and_ioremap_resource(ofdev, 1, &res);
        if (IS_ERR(cdns_ctrl->io.virt))
                return PTR_ERR(cdns_ctrl->io.virt);
+       cdns_ctrl->io.dma = res->start;
 
        dt->clk = devm_clk_get(cdns_ctrl->dev, "nf_clk");
        if (IS_ERR(dt->clk))