Skip to contentSkip to conversation

iio: adc: xilinx: fix potential use-after-free on remove

PATCH Applied Sven Van Asbroeck 2 participants 7 messages Mar 10, 2019 – Mar 17, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/3 iio: adc: xilinx: fix potential use-after-free on remove

When cancel_delayed_work() returns, the delayed work may still be running. This means that the core could potentially free the private structure (struct xadc) while the delayed work is still using it. This is a potential use-after-free.

Fix by calling cancel_delayed_work_sync(), which waits for any residual work to finish before returning.

---
 drivers/iio/adc/xilinx-xadc-core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/xilinx-xadc-core.c +1 -1
index 3f6be5ac049a..1960694e8007 100644
@@ -1320,7 +1320,7 @@ static int xadc_remove(struct platform_device *pdev)
 	}
 	free_irq(xadc->irq, indio_dev);
 	clk_disable_unprepare(xadc->clk);
-	cancel_delayed_work(&xadc->zynq_unmask_work);
+	cancel_delayed_work_sync(&xadc->zynq_unmask_work);
 	kfree(xadc->data);
 	kfree(indio_dev->channels);
 
Signature · 1 line
2.17.1
#1 lore raw

PATCH 2/3 iio: adc: xilinx: fix potential use-after-free on probe

If probe errors out after request_irq(), its error path does not explicitly cancel the delayed work, which may have been scheduled by the interrupt handler.

This means the delayed work may still be running when the core frees the private structure (struct xadc). This is a potential use-after-free.

Fix by inserting cancel_delayed_work_sync() in the probe error path.

---
 drivers/iio/adc/xilinx-xadc-core.c | 1 +
 1 file changed, 1 insertion(+)
drivers/iio/adc/xilinx-xadc-core.c +1 -0
index 1960694e8007..15e1a103f37d 100644
@@ -1290,6 +1290,7 @@ static int xadc_probe(struct platform_device *pdev)
 
 err_free_irq:
 	free_irq(xadc->irq, indio_dev);
+	cancel_delayed_work_sync(&xadc->zynq_unmask_work);
 err_clk_disable_unprepare:
 	clk_disable_unprepare(xadc->clk);
 err_free_samplerate_trigger:
Signature · 1 line
2.17.1
maintainer Applied
#2 lore raw

Re: [PATCH 2/3] iio: adc: xilinx: fix potential use-after-free on probe

Quoted 4 lines · On Sun, 10 Mar 2019 14:58:25 -0400 Sven Van Asbroeck <[email protected]>…

If probe errors out after request_irq(), its error path does not explicitly cancel the delayed work, which may have been scheduled by the interrupt handler.

Quoted 8 lines

This means the delayed work may still be running when the core frees the private structure (struct xadc). This is a potential use-after-free.

Fix by inserting cancel_delayed_work_sync() in the probe error path.

Again, seems fine to me.

Applied to the fixes-togreg branch of iio.git.

thanks,

Jonathan

Quoted 16 lines
---
 drivers/iio/adc/xilinx-xadc-core.c | 1 +
 1 file changed, 1 insertion(+)
drivers/iio/adc/xilinx-xadc-core.c +1 -0
index 1960694e8007..15e1a103f37d 100644
@@ -1290,6 +1290,7 @@ static int xadc_probe(struct platform_device *pdev)
 
 err_free_irq:
 	free_irq(xadc->irq, indio_dev);
+	cancel_delayed_work_sync(&xadc->zynq_unmask_work);
 err_clk_disable_unprepare:
 	clk_disable_unprepare(xadc->clk);
 err_free_samplerate_trigger:
#3 lore raw

PATCH 3/3 iio: adc: xilinx: prevent touching unclocked h/w on remove

In reply to Sven Van Asbroeck

In remove, the clock is disabled before canceling the delayed work. This means that the delayed work may be touching unclocked hardware.

Fix by disabling the clock after the delayed work is fully canceled. This is consistent with the probe error path order.

---
 drivers/iio/adc/xilinx-xadc-core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/xilinx-xadc-core.c +1 -1
index 15e1a103f37d..1ae86e7359f7 100644
@@ -1320,8 +1320,8 @@ static int xadc_remove(struct platform_device *pdev)
 		iio_triggered_buffer_cleanup(indio_dev);
 	}
 	free_irq(xadc->irq, indio_dev);
-	clk_disable_unprepare(xadc->clk);
 	cancel_delayed_work_sync(&xadc->zynq_unmask_work);
+	clk_disable_unprepare(xadc->clk);
 	kfree(xadc->data);
 	kfree(indio_dev->channels);
 
Signature · 1 line
2.17.1
maintainer Applied
#4 lore raw

Re: [PATCH 3/3] iio: adc: xilinx: prevent touching unclocked h/w on remove

Quoted 9 lines · On Sun, 10 Mar 2019 14:58:26 -0400 Sven Van Asbroeck <[email protected]>…

In remove, the clock is disabled before canceling the delayed work. This means that the delayed work may be touching unclocked hardware.

Fix by disabling the clock after the delayed work is fully canceled. This is consistent with the probe error path order.

Applied to the fixes-togreg branch of iio.git.

Thanks,

Jonathan

Quoted 18 lines
---
 drivers/iio/adc/xilinx-xadc-core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/xilinx-xadc-core.c +1 -1
index 15e1a103f37d..1ae86e7359f7 100644
@@ -1320,8 +1320,8 @@ static int xadc_remove(struct platform_device *pdev)
 		iio_triggered_buffer_cleanup(indio_dev);
 	}
 	free_irq(xadc->irq, indio_dev);
-	clk_disable_unprepare(xadc->clk);
 	cancel_delayed_work_sync(&xadc->zynq_unmask_work);
+	clk_disable_unprepare(xadc->clk);
 	kfree(xadc->data);
 	kfree(indio_dev->channels);
 
#5 lore raw

Re: [PATCH 3/3] iio: adc: xilinx: prevent touching unclocked h/w on remove

Quoted 3 lines · Jonathan Cameron

Applied to the fixes-togreg branch of iio.git.

Thank you kindly for taking this patch set, Jonathan.

maintainer Applied
#6 lore raw
In reply to Sven Van Asbroeck
Quoted 9 lines · On Sun, 10 Mar 2019 14:58:24 -0400 Sven Van Asbroeck <[email protected]>…

When cancel_delayed_work() returns, the delayed work may still be running. This means that the core could potentially free the private structure (struct xadc) while the delayed work is still using it. This is a potential use-after-free.

Fix by calling cancel_delayed_work_sync(), which waits for any residual work to finish before returning.

This appears 'obviously correct' to me so I'll apply it to the fixes togreg branch of iio.git and mark it for stable.

However I won't be sending a pull request for that branch until at least next weekend, so if anyone more familiar with the hardware has a chance to take a look that would be great.

Applied to the fixes-togreg branch of iio.git.

Thanks,

Jonathan

Quoted 17 lines
---
 drivers/iio/adc/xilinx-xadc-core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/xilinx-xadc-core.c +1 -1
index 3f6be5ac049a..1960694e8007 100644
@@ -1320,7 +1320,7 @@ static int xadc_remove(struct platform_device *pdev)
 	}
 	free_irq(xadc->irq, indio_dev);
 	clk_disable_unprepare(xadc->clk);
-	cancel_delayed_work(&xadc->zynq_unmask_work);
+	cancel_delayed_work_sync(&xadc->zynq_unmask_work);
 	kfree(xadc->data);
 	kfree(indio_dev->channels);