Skip to contentSkip to conversation

spi: microchip-core: fix UAF in mchp_corespi_remove()

PATCH Superseded Yang Yingliang 2 participants 6 messages Jul 12, 2022 – Jul 13, 2022

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 spi: microchip-core: fix UAF in mchp_corespi_remove()

When using devm_spi_register_master(), the unregister function will be called in devres_release_all() which is called after ->remove(), so remove spi_unregister_master() andspi_master_put().

Fixes 9ac8d17694b6 ("spi: add support for microchip fpga spi controllers")
---
 drivers/spi/spi-microchip-core.c | 2 --
 1 file changed, 2 deletions(-)
drivers/spi/spi-microchip-core.c +0 -2
index b3083075cd36..c26767343176 100644
@@ -595,8 +595,6 @@ static int mchp_corespi_remove(struct platform_device *pdev)
 	struct mchp_corespi *spi = spi_master_get_devdata(master);
 
 	mchp_corespi_disable_ints(spi);
-	spi_unregister_master(master);
-	spi_master_put(master);
 	clk_disable_unprepare(spi->clk);
 	mchp_corespi_disable(spi);
 
Signature · 1 line
2.25.1
#1 lore raw

PATCH 2/2 spi: microchip-core: switch to use devm_spi_alloc_master()

Switch to use devm_spi_alloc_master() to simpify error path.

---
 drivers/spi/spi-microchip-core.c | 20 +++++++-------------
 1 file changed, 7 insertions(+), 13 deletions(-)
drivers/spi/spi-microchip-core.c +7 -13
index c26767343176..1a24e47f8305 100644
@@ -513,7 +513,7 @@ static int mchp_corespi_probe(struct platform_device *pdev)
 	u32 num_cs;
 	int ret = 0;
 
-	master = spi_alloc_master(&pdev->dev, sizeof(*spi));
+	master = devm_spi_alloc_master(&pdev->dev, sizeof(*spi));
 	if (!master)
 		return dev_err_probe(&pdev->dev, -ENOMEM,
 				     "unable to allocate master for SPI controller\n");
@@ -535,36 +535,32 @@ static int mchp_corespi_probe(struct platform_device *pdev)
 	spi = spi_master_get_devdata(master);
 
 	spi->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
-	if (IS_ERR(spi->regs)) {
-		ret = PTR_ERR(spi->regs);
-		goto error_release_master;
-	}
+	if (IS_ERR(spi->regs))
+		return PTR_ERR(spi->regs);
 
 	spi->irq = platform_get_irq(pdev, 0);
 	if (spi->irq <= 0) {
 		dev_err(&pdev->dev, "invalid IRQ %d for SPI controller\n", spi->irq);
-		ret = -ENXIO;
-		goto error_release_master;
+		return -ENXIO;
 	}
 
 	ret = devm_request_irq(&pdev->dev, spi->irq, mchp_corespi_interrupt,
 			       IRQF_SHARED, dev_name(&pdev->dev), master);
 	if (ret) {
 		dev_err(&pdev->dev, "could not request irq: %d\n", ret);
-		goto error_release_master;
+		return ret;
 	}
 
 	spi->clk = devm_clk_get(&pdev->dev, NULL);
 	if (IS_ERR(spi->clk)) {
-		ret = PTR_ERR(spi->clk);
 		dev_err(&pdev->dev, "could not get clk: %d\n", ret);
-		goto error_release_master;
+		return PTR_ERR(spi->clk);
 	}
 
 	ret = clk_prepare_enable(spi->clk);
 	if (ret) {
 		dev_err(&pdev->dev, "failed to enable clock\n");
-		goto error_release_master;
+		return ret;
 	}
 
 	mchp_corespi_init(master, spi);
@@ -583,8 +579,6 @@ static int mchp_corespi_probe(struct platform_device *pdev)
 error_release_hardware:
 	mchp_corespi_disable(spi);
 	clk_disable_unprepare(spi->clk);
-error_release_master:
-	spi_master_put(master);
 
 	return ret;
 }
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH -next 2/2] spi: microchip-core: switch to use devm_spi_alloc_master()

Quoted 5 lines · Yang Yingliang

[Some people who received this message don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

Switch to use devm_spi_alloc_master() to simpify error path.

Hey Yang,
Thanks for trying to fix my mistakes!

Forgive my innocence here, but why is it okay to remove the spi_master_put() in remove() but not the one in the error path of the probe function?

If the devm_add_action_or_reset() in devm_spi_register_controller() fails won't the same thing apply to the probe error path?

IOW, I think this patch needs a fixes tag too b/c it also fixes a refcount underflow. Please correct me if I am misunderstanding.

One other comment below.

Quoted 26 lines
---
  drivers/spi/spi-microchip-core.c | 20 +++++++-------------
  1 file changed, 7 insertions(+), 13 deletions(-)
drivers/spi/spi-microchip-core.c +1 -4
index c26767343176..1a24e47f8305 100644
@@ -513,7 +513,7 @@ static int mchp_corespi_probe(struct platform_device *pdev)
         u32 num_cs;
         int ret = 0;
 
-       master = spi_alloc_master(&pdev->dev, sizeof(*spi));
+       master = devm_spi_alloc_master(&pdev->dev, sizeof(*spi));
         if (!master)
                 return dev_err_probe(&pdev->dev, -ENOMEM,
                                      "unable to allocate master for SPI controller\n");
@@ -535,36 +535,32 @@ static int mchp_corespi_probe(struct platform_device *pdev)
         spi = spi_master_get_devdata(master);
 
         spi->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
-       if (IS_ERR(spi->regs)) {
-               ret = PTR_ERR(spi->regs);
-               goto error_release_master;

drivers/spi/spi-microchip-core.c static int mchp_corespi_probe(struct platform_device *pdev) · line 544

Quoted 10 lines
drivers/spi/spi-microchip-core.c +3 -3
-       }
+       if (IS_ERR(spi->regs))
+               return PTR_ERR(spi->regs);
 
         spi->irq = platform_get_irq(pdev, 0);
         if (spi->irq <= 0) {
                 dev_err(&pdev->dev, "invalid IRQ %d for SPI controller\n", spi->irq);
-               ret = -ENXIO;
-               goto error_release_master;
+               return -ENXIO;

Also these can now become dev_err_probe for further simplification? Thanks,
Conor.

Quoted 38 lines
drivers/spi/spi-microchip-core.c +3 -7
         }
 
         ret = devm_request_irq(&pdev->dev, spi->irq, mchp_corespi_interrupt,
                                IRQF_SHARED, dev_name(&pdev->dev), master);
         if (ret) {
                 dev_err(&pdev->dev, "could not request irq: %d\n", ret);
-               goto error_release_master;
+               return ret;
         }
 
         spi->clk = devm_clk_get(&pdev->dev, NULL);
         if (IS_ERR(spi->clk)) {
-               ret = PTR_ERR(spi->clk);
                 dev_err(&pdev->dev, "could not get clk: %d\n", ret);
-               goto error_release_master;
+               return PTR_ERR(spi->clk);
         }
 
         ret = clk_prepare_enable(spi->clk);
         if (ret) {
                 dev_err(&pdev->dev, "failed to enable clock\n");
-               goto error_release_master;
+               return ret;
         }
 
         mchp_corespi_init(master, spi);
@@ -583,8 +579,6 @@ static int mchp_corespi_probe(struct platform_device *pdev)
  error_release_hardware:
         mchp_corespi_disable(spi);
         clk_disable_unprepare(spi->clk);
-error_release_master:
-       spi_master_put(master);
 
         return ret;
  }
--

2.25.1

#3 lore raw

Re: [PATCH -next 2/2] spi: microchip-core: switch to use devm_spi_alloc_master()

Quoted 14 lines · On 12/07/2022 15:03, [email protected] wrote:
Quoted 5 lines · Yang Yingliang

[Some people who received this message don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

Switch to use devm_spi_alloc_master() to simpify error path.

Hey Yang,
Thanks for trying to fix my mistakes!

Forgive my innocence here, but why is it okay to remove the spi_master_put() in remove() but not the one in the error path of the probe function?

Quoted 5 lines

If the devm_add_action_or_reset() in devm_spi_register_controller() fails won't the same thing apply to the probe error path?

IOW, I think this patch needs a fixes tag too b/c it also fixes a refcount underflow. Please correct me if I am misunderstanding.

Ahh, I just saw your revert of 59ebbe40fb51 ("spi: simplify devm_spi register_controller"). With that, this makes a lot more sense.

Quoted 2 lines

One other comment below.

This comment still applies for this patch. dev_err_probe would be nice.

Thanks,
Conor.

Quoted 85 lines
Quoted 27 lines
---
  drivers/spi/spi-microchip-core.c | 20 +++++++-------------
  1 file changed, 7 insertions(+), 13 deletions(-)
drivers/spi/spi-microchip-core.c +1 -4
index c26767343176..1a24e47f8305 100644
@@ -513,7 +513,7 @@ static int mchp_corespi_probe(struct platform_device *pdev)
         u32 num_cs;
         int ret = 0;
 
-       master = spi_alloc_master(&pdev->dev, sizeof(*spi));
+       master = devm_spi_alloc_master(&pdev->dev, sizeof(*spi));
         if (!master)
                 return dev_err_probe(&pdev->dev, -ENOMEM,
                                      "unable to allocate master for SPI controller\n");
@@ -535,36 +535,32 @@ static int mchp_corespi_probe(struct platform_device *pdev)
         spi = spi_master_get_devdata(master);
 
         spi->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
-       if (IS_ERR(spi->regs)) {
-               ret = PTR_ERR(spi->regs);
-               goto error_release_master;

drivers/spi/spi-microchip-core.c static int mchp_corespi_probe(struct platform_device *pdev) · line 544

Quoted 10 lines
drivers/spi/spi-microchip-core.c +3 -3
-       }
+       if (IS_ERR(spi->regs))
+               return PTR_ERR(spi->regs);
 
         spi->irq = platform_get_irq(pdev, 0);
         if (spi->irq <= 0) {
                 dev_err(&pdev->dev, "invalid IRQ %d for SPI controller\n", spi->irq);
-               ret = -ENXIO;
-               goto error_release_master;
+               return -ENXIO;

Also these can now become dev_err_probe for further simplification? Thanks,
Conor.

Quoted 36 lines
drivers/spi/spi-microchip-core.c +3 -7
         }
 
         ret = devm_request_irq(&pdev->dev, spi->irq, mchp_corespi_interrupt,
                                IRQF_SHARED, dev_name(&pdev->dev), master);
         if (ret) {
                 dev_err(&pdev->dev, "could not request irq: %d\n", ret);
-               goto error_release_master;
+               return ret;
         }
 
         spi->clk = devm_clk_get(&pdev->dev, NULL);
         if (IS_ERR(spi->clk)) {
-               ret = PTR_ERR(spi->clk);
                 dev_err(&pdev->dev, "could not get clk: %d\n", ret);
-               goto error_release_master;
+               return PTR_ERR(spi->clk);
         }
 
         ret = clk_prepare_enable(spi->clk);
         if (ret) {
                 dev_err(&pdev->dev, "failed to enable clock\n");
-               goto error_release_master;
+               return ret;
         }
 
         mchp_corespi_init(master, spi);
@@ -583,8 +579,6 @@ static int mchp_corespi_probe(struct platform_device *pdev)
  error_release_hardware:
         mchp_corespi_disable(spi);
         clk_disable_unprepare(spi->clk);
-error_release_master:
-       spi_master_put(master);
 
         return ret;
  }
--
Quoted 2 lines

2.25.1

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#4 lore raw

Re: [PATCH -next 2/2] spi: microchip-core: switch to use devm_spi_alloc_master()

Quoted 19 lines · On 2022/7/13 4:50, [email protected] wrote:
Quoted 13 lines · On 12/07/2022 15:03, [email protected] wrote:
Quoted 5 lines · Yang Yingliang

[Some people who received this message don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

Switch to use devm_spi_alloc_master() to simpify error path.

Hey Yang,
Thanks for trying to fix my mistakes!

Forgive my innocence here, but why is it okay to remove the spi_master_put() in remove() but not the one in the error path of the probe function?

Quoted 5 lines

If the devm_add_action_or_reset() in devm_spi_register_controller() fails won't the same thing apply to the probe error path?

IOW, I think this patch needs a fixes tag too b/c it also fixes a refcount underflow. Please correct me if I am misunderstanding.

Quoted 7 lines

Ahh, I just saw your revert of 59ebbe40fb51 ("spi: simplify devm_spi register_controller"). With that, this makes a lot more sense.

Quoted 1 line

One other comment below.

This comment still applies for this patch. dev_err_probe would be nice.

OK.

Thanks,
Yang

Quoted 3 lines

Thanks,
Conor.

Reviewed-by on v1 1/2
#5 lore raw
In reply to Yang Yingliang
Quoted 6 lines · Yang Yingliang

When using devm_spi_register_master(), the unregister function will be called in devres_release_all() which is called after ->remove(), so remove spi_unregister_master() andspi_master_put().

Fixes 9ac8d17694b6 ("spi: add support for microchip fpga spi controllers")

With 59ebbe40fb51 ("spi: simplify devm_spi register_controller") reverted, this looks good to me.

Thanks!

Quoted 17 lines
---
 drivers/spi/spi-microchip-core.c | 2 --
 1 file changed, 2 deletions(-)
drivers/spi/spi-microchip-core.c +0 -2
index b3083075cd36..c26767343176 100644
@@ -595,8 +595,6 @@ static int mchp_corespi_remove(struct platform_device *pdev)
 	struct mchp_corespi *spi = spi_master_get_devdata(master);
 
 	mchp_corespi_disable_ints(spi);
-	spi_unregister_master(master);
-	spi_master_put(master);
 	clk_disable_unprepare(spi->clk);
 	mchp_corespi_disable(spi);