Skip to contentSkip to conversation

rtc: atcrtc100: drain the alarm work before the RTC device is freed

PATCH New hyunjungg 2 participants 2 messages Nov 7

Subsystems (3)
lore
Share & tools
lore

hyunjungg

author
#0 lore raw

PATCH 1/1 rtc: atcrtc100: drain the alarm work before the RTC device is freed

From: Hyunjung Ko <[email protected]>

atcrtc_alarm_isr() schedules atcrtc_dev->rtc_work (atcrtc_alarm_clear), whose handler obtains the atcrtc_dev via container_of() and dereferences rtc->rtc_dev (guard(rtc_ops)(rtc->rtc_dev)).

atcrtc_probe() requested the alarm IRQ with devm_request_irq() *before* allocating rtc_dev with devm_rtc_allocate_device(), and INIT_WORK()'d rtc_work last with no teardown cancel. Because devres unwinds in reverse registration order, on unbind the RTC device is freed before the IRQ, and rtc_work is never cancelled at all: an alarm interrupt arriving around unbind can run atcrtc_alarm_clear() after rtc->rtc_dev (and the devm driver data) have been freed -> use-after-free.

Allocate the RTC device first, then register rtc_work for device-managed cancellation with devm_work_autocancel(), and only then request the IRQ. devres now frees the IRQ first (so it can no longer schedule the work), then drains the work while rtc_dev is still valid, then frees rtc_dev and the driver data.

Found by code review; not reproduced (requires ATCRTC100 hardware and an alarm interrupt racing device unbind).

Fixes 7adca706fe16 ("rtc: atcrtc100: Add ATCRTC100 RTC driver")
---
 drivers/rtc/rtc-atcrtc100.c | 30 +++++++++++++++++++++---------
 1 file changed, 21 insertions(+), 9 deletions(-)
drivers/rtc/rtc-atcrtc100.c +21 -9
index 2433df3..51a38f0 100644
@@ -7,6 +7,7 @@
 
 #include <linux/bitfield.h>
 #include <linux/delay.h>
+#include <linux/devm-helpers.h>
 #include <linux/interrupt.h>
 #include <linux/math64.h>
 #include <linux/module.h>
@@ -301,6 +302,26 @@ static int atcrtc_probe(struct platform_device *pdev)
 				     "Failed to get IRQ for alarm\n");
 	atcrtc_dev->alarm_irq = ret;
 
+	atcrtc_dev->rtc_dev = devm_rtc_allocate_device(&pdev->dev);
+	if (IS_ERR(atcrtc_dev->rtc_dev))
+		return dev_err_probe(&pdev->dev, PTR_ERR(atcrtc_dev->rtc_dev),
+				     "Failed to allocate RTC device\n");
+
+	set_bit(RTC_FEATURE_ALARM, atcrtc_dev->rtc_dev->features);
+	atcrtc_dev->rtc_dev->ops = &rtc_ops;
+
+	/*
+	 * Register rtc_work for device-managed cancellation after the RTC
+	 * device it dereferences and before the IRQ that schedules it, so on
+	 * unbind devres frees the IRQ, then drains the work, then frees the
+	 * RTC device and driver data -- never the reverse (atcrtc_alarm_clear()
+	 * touches rtc->rtc_dev).
+	 */
+	ret = devm_work_autocancel(&pdev->dev, &atcrtc_dev->rtc_work,
+				   atcrtc_alarm_clear);
+	if (ret)
+		return ret;
+
 	ret = devm_request_irq(&pdev->dev,
 			       atcrtc_dev->alarm_irq,
 			       atcrtc_alarm_isr,
@@ -312,12 +333,6 @@ static int atcrtc_probe(struct platform_device *pdev)
 				     "Failed to request IRQ %d for alarm\n",
 				     atcrtc_dev->alarm_irq);
 
-	atcrtc_dev->rtc_dev = devm_rtc_allocate_device(&pdev->dev);
-	if (IS_ERR(atcrtc_dev->rtc_dev))
-		return dev_err_probe(&pdev->dev, PTR_ERR(atcrtc_dev->rtc_dev),
-				     "Failed to allocate RTC device\n");
-
-	set_bit(RTC_FEATURE_ALARM, atcrtc_dev->rtc_dev->features);
 	ret = device_init_wakeup(&pdev->dev, true);
 	if (ret)
 		return dev_err_probe(&pdev->dev, ret,
@@ -330,9 +345,6 @@ static int atcrtc_probe(struct platform_device *pdev)
 				     "Failed to set wake IRQ\n");
 	}
 
-	atcrtc_dev->rtc_dev->ops = &rtc_ops;
-
-	INIT_WORK(&atcrtc_dev->rtc_work, atcrtc_alarm_clear);
 	return devm_rtc_register_device(atcrtc_dev->rtc_dev);
 }
 
Signature · 1 line
2.43.0
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 20 lines

From: Hyunjung Ko <[email protected]>

atcrtc_alarm_isr() schedules atcrtc_dev->rtc_work (atcrtc_alarm_clear), whose handler obtains the atcrtc_dev via container_of() and dereferences rtc->rtc_dev (guard(rtc_ops)(rtc->rtc_dev)).

atcrtc_probe() requested the alarm IRQ with devm_request_irq() *before* allocating rtc_dev with devm_rtc_allocate_device(), and INIT_WORK()'d rtc_work last with no teardown cancel. Because devres unwinds in reverse registration order, on unbind the RTC device is freed before the IRQ, and rtc_work is never cancelled at all: an alarm interrupt arriving around unbind can run atcrtc_alarm_clear() after rtc->rtc_dev (and the devm driver data) have been freed -> use-after-free.

Allocate the RTC device first, then register rtc_work for device-managed cancellation with devm_work_autocancel(), and only then request the IRQ. devres now frees the IRQ first (so it can no longer schedule the work), then drains the work while rtc_dev is still valid, then frees rtc_dev and the driver data.

Quoted 6 lines

Found by code review; not reproduced (requires ATCRTC100 hardware and an alarm interrupt racing device unbind).

Fixes 7adca706fe16 ("rtc: atcrtc100: Add ATCRTC100 RTC driver")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1