Skip to contentSkip to conversation

rtc: snvs: fix possible race condition

PATCH Applied [email protected] 4 participants 10 messages Jul 16, 2019 – Aug 29, 2019 Real time clock (RTC)

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rtc: snvs: fix possible race condition

From: Anson Huang <[email protected]>

The RTC IRQ is requested before the struct rtc_device is allocated, this may lead to a NULL pointer dereference in IRQ handler.

To fix this issue, allocating the rtc_device struct before requesting the RTC IRQ using devm_rtc_allocate_device, and use rtc_register_device to register the RTC device.

---
 drivers/rtc/rtc-snvs.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
drivers/rtc/rtc-snvs.c +7 -4
index 7ee673a2..4f9a107 100644
@@ -279,6 +279,10 @@ static int snvs_rtc_probe(struct platform_device *pdev)
 	if (!data)
 		return -ENOMEM;
 
+	data->rtc = devm_rtc_allocate_device(&pdev->dev);
+	if (IS_ERR(data->rtc))
+		return PTR_ERR(data->rtc);
+
 	data->regmap = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "regmap");
 
 	if (IS_ERR(data->regmap)) {
@@ -343,10 +347,9 @@ static int snvs_rtc_probe(struct platform_device *pdev)
 		goto error_rtc_device_register;
 	}
 
-	data->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
-					&snvs_rtc_ops, THIS_MODULE);
-	if (IS_ERR(data->rtc)) {
-		ret = PTR_ERR(data->rtc);
+	data->rtc->ops = &snvs_rtc_ops;
+	ret = rtc_register_device(data->rtc);
+	if (ret) {
 		dev_err(&pdev->dev, "failed to register rtc: %d\n", ret);
 		goto error_rtc_device_register;
 	}
Signature · 1 line
2.7.4
#1 lore raw
Quoted 10 lines

From: [email protected] <[email protected]>
Sent: Tuesday, July 16, 2019 3:19 PM

The RTC IRQ is requested before the struct rtc_device is allocated, this may lead to a NULL pointer dereference in IRQ handler.

To fix this issue, allocating the rtc_device struct before requesting the RTC IRQ using devm_rtc_allocate_device, and use rtc_register_device to register the RTC device.

I saw other rtc drivers did the same way as us, so this looks like a common problem. My question is if we can clear interrupt status before register to avoid this issue as other rtc drivers?

Regards
Aisheng

Quoted 39 lines
---
 drivers/rtc/rtc-snvs.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
drivers/rtc/rtc-snvs.c index renamed from drivers/rtc/rtc-snvs.c +0 -0

7ee673a2..4f9a107 100644

drivers/rtc/rtc-snvs.c +4 -0
@@ -279,6 +279,10 @@ static int snvs_rtc_probe(struct platform_device
 *pdev)
 	if (!data)
 		return -ENOMEM;
 
+	data->rtc = devm_rtc_allocate_device(&pdev->dev);
+	if (IS_ERR(data->rtc))
+		return PTR_ERR(data->rtc);
+
 	data->regmap = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
 "regmap");
drivers/rtc/rtc-snvs.c +3 -4
 	if (IS_ERR(data->regmap)) {
@@ -343,10 +347,9 @@ static int snvs_rtc_probe(struct platform_device
 *pdev)
 		goto error_rtc_device_register;
 	}
 
-	data->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
-					&snvs_rtc_ops, THIS_MODULE);
-	if (IS_ERR(data->rtc)) {
-		ret = PTR_ERR(data->rtc);
+	data->rtc->ops = &snvs_rtc_ops;
+	ret = rtc_register_device(data->rtc);
+	if (ret) {
 		dev_err(&pdev->dev, "failed to register rtc: %d\n", ret);
 		goto error_rtc_device_register;

}
--
2.7.4

#2 lore raw

Hi, Aisheng

Quoted 11 lines
Quoted 10 lines

From: [email protected] <[email protected]>
Sent: Tuesday, July 16, 2019 3:19 PM

The RTC IRQ is requested before the struct rtc_device is allocated, this may lead to a NULL pointer dereference in IRQ handler.

To fix this issue, allocating the rtc_device struct before requesting the RTC IRQ using devm_rtc_allocate_device, and use rtc_register_device to register the RTC device.

Quoted 4 lines

I saw other rtc drivers did the same way as us, so this looks like a common problem.
My question is if we can clear interrupt status before register to avoid this issue as other rtc drivers?

I think we can NOT predict when the IRQ will be pending, IRQ could arrive at any time, the most safe way is to prepare everything before requesting/enabling IRQ. There is also patch to fix similar issue:

commit 060711f5274dfc2d76a5b2cd65abf6ccbf061e40

Author: Alexandre Belloni <[email protected]>
Date:   Tue Apr 30 11:32:09 2019 +0200

rtc: digicolor: fix possible race condition

Anson

Reviewed-by
#3 lore raw
Quoted 20 lines

From: Anson Huang
Sent: Wednesday, July 17, 2019 9:58 PM>
Hi, Aisheng

Quoted 11 lines
Quoted 10 lines

From: [email protected] <[email protected]>
Sent: Tuesday, July 16, 2019 3:19 PM

The RTC IRQ is requested before the struct rtc_device is allocated, this may lead to a NULL pointer dereference in IRQ handler.

To fix this issue, allocating the rtc_device struct before requesting the RTC IRQ using devm_rtc_allocate_device, and use rtc_register_device to register the RTC device.

Quoted 4 lines

I saw other rtc drivers did the same way as us, so this looks like a common problem.
My question is if we can clear interrupt status before register to avoid this issue as other rtc drivers?

Quoted 5 lines

I think we can NOT predict when the IRQ will be pending, IRQ could arrive at any time, the most safe way is to prepare everything before requesting/enabling IRQ.
There is also patch to fix similar issue:

I just feel like it's common issue. But seems community already did the same thing. So:

Regards
Aisheng

Quoted 7 lines

commit 060711f5274dfc2d76a5b2cd65abf6ccbf061e40

Author: Alexandre Belloni <[email protected]>
Date:   Tue Apr 30 11:32:09 2019 +0200

rtc: digicolor: fix possible race condition

Anson

#4 lore raw
Quoted 23 lines · Aisheng Dong
Quoted 23 lines

From: Anson Huang
Sent: Wednesday, July 17, 2019 9:58 PM>
Hi, Aisheng

Quoted 18 lines
Quoted 11 lines

From: [email protected] <[email protected]>
Sent: Tuesday, July 16, 2019 3:19 PM

The RTC IRQ is requested before the struct rtc_device is allocated,
this may lead to a NULL pointer dereference in IRQ handler.

To fix this issue, allocating the rtc_device struct before requesting the RTC IRQ using devm_rtc_allocate_device, and use rtc_register_device to register the RTC device.

I saw other rtc drivers did the same way as us, so this looks like a
common problem.
My question is if we can clear interrupt status before register to
avoid this issue as other rtc drivers?

Quoted 5 lines
Quoted 5 lines

I think we can NOT predict when the IRQ will be pending, IRQ could arrive at
any time, the most safe way is to prepare everything before requesting/enabling IRQ.
There is also patch to fix similar issue:

I think one could attempt to disable all irq sources in the device via its register space, then enable the interrupt. But this seems more specific to each device than changing the pattern of device registration, so IMHO, it's not really better.

I do worry that handling the irq before the rtc device is registered could still result in a crash. From what I saw, the irq path in snvs only uses driver state members that are fully initialized for the most part, and the allocated but unregistered data->rtc is only used in one call to rtc_update_irq(), which appears to be ok with this.

But it is not that hard to imagine that something could go into the rtc core that assumes call like rtc_update_irq() are only made on registered devices.

If there was a way to do it, I think allocating the irq in a masked state and then unmasking it as part of the final registration call to make the device go live would be a safer and more general pattern.

Quoted 1 line
#5 lore raw

Hi, Trent

Quoted 37 lines · Aisheng Dong
Quoted 20 lines
Quoted 20 lines

From: Anson Huang
Sent: Wednesday, July 17, 2019 9:58 PM> Hi, Aisheng

Quoted 16 lines
Quoted 11 lines

From: [email protected] <[email protected]>
Sent: Tuesday, July 16, 2019 3:19 PM

The RTC IRQ is requested before the struct rtc_device is allocated, this may lead to a NULL pointer dereference in IRQ handler.

To fix this issue, allocating the rtc_device struct before requesting the RTC IRQ using devm_rtc_allocate_device, and use rtc_register_device to register the RTC device.

I saw other rtc drivers did the same way as us, so this looks like a common problem.
My question is if we can clear interrupt status before register to avoid this issue as other rtc drivers?

Quoted 4 lines
Quoted 4 lines

I think we can NOT predict when the IRQ will be pending, IRQ could arrive at any time, the most safe way is to prepare everything before requesting/enabling IRQ.
There is also patch to fix similar issue:

I think one could attempt to disable all irq sources in the device via its register space, then enable the interrupt. But this seems more specific to each device than changing the pattern of device registration, so IMHO, it's not really better.

I do worry that handling the irq before the rtc device is registered could still result in a crash. From what I saw, the irq path in snvs only uses driver state members that are fully initialized for the most part, and the allocated but unregistered data->rtc is only used in one call to rtc_update_irq(), which appears to be ok with this.

Quoted 6 lines

But it is not that hard to imagine that something could go into the rtc core that assumes call like rtc_update_irq() are only made on registered devices.

If there was a way to do it, I think allocating the irq in a masked state and then unmasking it as part of the final registration call to make the device go live would be a safer and more general pattern.

It makes sense, I think we can just move the devm_request_irq() to after rtc_register_device(), It will make sure everything is ready before IRQ is enabled. Will send out a V2 patch.

Thanks,
Anson

#6 lore raw
Quoted 10 lines · Anson Huang
Quoted 10 lines

I do worry that handling the irq before the rtc device is registered could still result in a crash. From what I saw, the irq path in snvs only uses driver state members that are fully initialized for the most part, and the allocated but unregistered data->rtc is only used in one call to rtc_update_irq(), which appears to be ok with this.

But it is not that hard to imagine that something could go into the rtc core that assumes call like rtc_update_irq() are only made on registered devices.

Quoted 6 lines
Quoted 3 lines

If there was a way to do it, I think allocating the irq in a masked state and then unmasking it as part of the final registration call to make the device go live would be a safer and more general pattern.

It makes sense, I think we can just move the devm_request_irq() to after rtc_register_device(), It will make sure everything is ready before IRQ is enabled. Will send out a V2 patch.

That will mean registering the rtc, then unregistering it if the irq request fails. More of a pain to write this failure path.

Alexandre, is it part of rtc core design that rtc_update_irq() might be called on a rtc device that is properly allocated, but not registered yet?

#7 lore raw
Quoted 18 lines · Trent Piepho
Quoted 10 lines · Anson Huang
Quoted 10 lines

I do worry that handling the irq before the rtc device is registered could still result in a crash. From what I saw, the irq path in snvs only uses driver state members that are fully initialized for the most part, and the allocated but unregistered data->rtc is only used in one call to rtc_update_irq(), which appears to be ok with this.

But it is not that hard to imagine that something could go into the rtc core that assumes call like rtc_update_irq() are only made on registered devices.

Quoted 6 lines
Quoted 3 lines

If there was a way to do it, I think allocating the irq in a masked state and then unmasking it as part of the final registration call to make the device go live would be a safer and more general pattern.

It makes sense, I think we can just move the devm_request_irq() to after rtc_register_device(), It will make sure everything is ready before IRQ is enabled. Will send out a V2 patch.

Quoted 6 lines

That will mean registering the rtc, then unregistering it if the irq request fails. More of a pain to write this failure path.

Alexandre, is it part of rtc core design that rtc_update_irq() might be called on a rtc device that is properly allocated, but not registered yet?

Yes, the main reason of the change of API was exactly to handle this.

Signature · 3 lines
Alexandre Belloni, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#8 lore raw

Hi, Alexandre

Quoted 23 lines · Trent Piepho
Quoted 7 lines · Anson Huang
Quoted 6 lines
Quoted 6 lines

I do worry that handling the irq before the rtc device is registered could still result in a crash. From what I saw, the irq path in snvs only uses driver state members that are fully initialized for the most part, and the allocated but unregistered data->rtc is only used in one call to rtc_update_irq(), which appears to

be ok with this.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

But it is not that hard to imagine that something could go into the rtc core that assumes call like rtc_update_irq() are only made on

registered devices.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

If there was a way to do it, I think allocating the irq in a masked state and then unmasking it as part of the final registration call to make the device go live would be a safer and more

general pattern.

Quoted 3 lines
Quoted 3 lines

It makes sense, I think we can just move the devm_request_irq() to after rtc_register_device(), It will make sure everything is ready before

IRQ is enabled. Will send out a V2 patch.

Quoted 1 line
Quoted 8 lines
Quoted 6 lines

That will mean registering the rtc, then unregistering it if the irq request fails. More of a pain to write this failure path.

Alexandre, is it part of rtc core design that rtc_update_irq() might be called on a rtc device that is properly allocated, but not registered yet?

Yes, the main reason of the change of API was exactly to handle this.

What about this patch's status? Should we continue or any change needed?

https://patchwork.ozlabs.org/patch/1132481/

Thanks,
Anson

maintainer Applied
#9 lore raw
In reply to [email protected]
Quoted 5 lines · On 16/07/2019 15:18:58+0800, [email protected] wrote:

From: Anson Huang <[email protected]>

The RTC IRQ is requested before the struct rtc_device is allocated, this may lead to a NULL pointer dereference in IRQ handler.

Quoted 9 lines

To fix this issue, allocating the rtc_device struct before requesting the RTC IRQ using devm_rtc_allocate_device, and use rtc_register_device to register the RTC device.

---
 drivers/rtc/rtc-snvs.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

Applied, thanks.

Signature · 3 lines
Alexandre Belloni, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com