[PATCH 09/10] rtc: omap: prevent NULL and dangling pointer dereferences in power-off path
From: Lee Jones
Date: Mon Nov 23 2026 - 15:01:34 EST
omap_rtc_remove() only clears the global omap_rtc_power_off_rtc pointer
if pm_power_off still equals omap_rtc_power_off. If another driver
overwrote pm_power_off after probe, unbinding the RTC leaves
omap_rtc_power_off_rtc pointing to freed memory. Furthermore,
omap_rtc_power_off_program() is exported and called directly from
am33xx_rtc_only_idle() during suspend without checking whether
omap_rtc_power_off_rtc is NULL, causing a NULL pointer dereference if
omap_rtc_power_off_rtc was never assigned or was cleared on unbind.
Always clear omap_rtc_power_off_rtc when removing the matching RTC
instance, and return -ENODEV from omap_rtc_power_off_program() when
omap_rtc_power_off_rtc is NULL.
Fixes: 222a12fca604 ("rtc: omap: add support for pmic_power_en")
Closes: https://sashiko.dev/#/bug/linux-97b795c5-f68f-4228-948f-547a106c6775
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Lee Jones <lee@xxxxxxxxxx>
---
drivers/rtc/rtc-omap.c | 21 +++++++++++++--------
1 file changed, 13 insertions(+), 8 deletions(-)
diff --git a/drivers/rtc/rtc-omap.c b/drivers/rtc/rtc-omap.c
index 3c978edd6c2c..156444fab916 100644
--- a/drivers/rtc/rtc-omap.c
+++ b/drivers/rtc/rtc-omap.c
@@ -416,6 +416,9 @@ int omap_rtc_power_off_program(void)
int seconds;
u32 val;
+ if (!rtc)
+ return -ENODEV;
+
rtc->type->unlock(rtc);
/* enable pmic_power_en control */
val = rtc_readl(rtc, OMAP_RTC_PMIC_REG);
@@ -481,17 +484,19 @@ EXPORT_SYMBOL(omap_rtc_power_off_program);
*/
static void omap_rtc_power_off(void)
{
+ struct omap_rtc *rtc = omap_rtc_power_off_rtc;
u32 val;
- omap_rtc_power_off_program();
+ if (omap_rtc_power_off_program())
+ return;
/* Set PMIC power enable and EXT_WAKEUP in case PB power on is used */
- omap_rtc_power_off_rtc->type->unlock(omap_rtc_power_off_rtc);
- val = rtc_readl(omap_rtc_power_off_rtc, OMAP_RTC_PMIC_REG);
+ rtc->type->unlock(rtc);
+ val = rtc_readl(rtc, OMAP_RTC_PMIC_REG);
val |= OMAP_RTC_PMIC_POWER_EN_EN | OMAP_RTC_PMIC_EXT_WKUP_POL(0) |
OMAP_RTC_PMIC_EXT_WKUP_EN(0);
- rtc_writel(omap_rtc_power_off_rtc, OMAP_RTC_PMIC_REG, val);
- omap_rtc_power_off_rtc->type->lock(omap_rtc_power_off_rtc);
+ rtc_writel(rtc, OMAP_RTC_PMIC_REG, val);
+ rtc->type->lock(rtc);
/*
* Wait for alarm to trigger (within one second) and external PMIC to
@@ -913,9 +918,9 @@ static void omap_rtc_remove(struct platform_device *pdev)
struct omap_rtc *rtc = platform_get_drvdata(pdev);
u8 reg;
- if (pm_power_off == omap_rtc_power_off &&
- omap_rtc_power_off_rtc == rtc) {
- pm_power_off = NULL;
+ if (omap_rtc_power_off_rtc == rtc) {
+ if (pm_power_off == omap_rtc_power_off)
+ pm_power_off = NULL;
omap_rtc_power_off_rtc = NULL;
}
--
2.56.1.750.gf70dc682cf-goog