Re: [PATCH 09/10] rtc: omap: prevent NULL and dangling pointer dereferences in power-off path

From: Johan Hovold

Date: Tue Nov 24 2026 - 02:59:24 EST


On Mon, Nov 23, 2026 at 07:59:20PM +0000, Lee Jones wrote:
> 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.

This sounds like two separate issues and the am33xx_rtc_only_idle call()
was added later by commit 5a99ae0092fe ("soc: ti: pm33xx: AM437X: Add
rtc_only with ddr in self-refresh support").

> 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")

So this isn't the right fixes tag for that.

> 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);

This is an unrelated change.

> /*
> * 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;
> }

You can only have one legacy power off handler, but leaving the
omap_rtc_power_off_rtc pointer set after unbind wasn't an issue before
the later added am33xx_rtc_only_idle() call.

So with the Fixes tag fixed and the unrelated changes in
omap_rtc_power_off() dropped I guess this can all go in one patch.

Johan