Linux RTC
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Liu Dalin" <liudalin@kylinsec.com.cn>
Cc: "Alexandre Belloni" <alexandre.belloni@bootlin.com>,
	linux-rtc@vger.kernel.org
Subject: Re: [PATCH] rtc: ftrtc010: fix clock resource leak on probe failure
Date: Thu, 27 Aug 2026 02:10:53 +0000	[thread overview]
Message-ID: <20260827021054.0963C1F000E9@smtp.kernel.org> (raw)
In-Reply-To: <6A7910FC22A9FEF1+20260827015739.3314298-1-liudalin@kylinsec.com.cn>

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Pre-existing issues:
- [High] Mixing devm_rtc_register_device() with manual clock disabling in .remove causes a use-after-disable vulnerability.
- [High] Shared interrupt handler unconditionally returns IRQ_HANDLED without checking hardware registers.
- [High] Integer overflow in `ftrtc010_rtc_read_time` due to 32-bit arithmetic wrapping.
--

commit 7252ea4ab6d10cb6ebbf156a3c7aa20a2dbb7aa7
Author: Liu Dalin <liudalin@kylinsec.com.cn>

rtc: ftrtc010: fix clock resource leak on probe failure

When devm_rtc_register_device() fails, ftrtc010_rtc_probe() returns the error
directly without releasing clocks obtained via clk_prepare_enable(). This
patch captures the return value and jumps to the error handling path to
properly release clocks via clk_disable_unprepare().

> diff --git a/drivers/rtc/rtc-ftrtc010.c b/drivers/rtc/rtc-ftrtc010.c
> index 02608d3784958..f398d6dc85ae4 100644
> --- a/drivers/rtc/rtc-ftrtc010.c
> +++ b/drivers/rtc/rtc-ftrtc010.c
> @@ -181,7 +181,11 @@ static int ftrtc010_rtc_probe(struct platform_device *pdev)
>  	if (unlikely(ret))
>  		goto err_disable_extclk;
>  
> -	return devm_rtc_register_device(rtc_dev);
> +	ret = devm_rtc_register_device(rtc_dev);
> +	if (ret)
> +		goto err_disable_extclk;
> +
> +	return 0;
>  
>  err_disable_extclk:
>  	clk_disable_unprepare(rtc->extclk);

[Severity: High]
This isn't a bug introduced by this patch, but mixing
devm_rtc_register_device() with manual clock disabling in ftrtc010_rtc_remove()
causes a use-after-disable issue.

In ftrtc010_rtc_remove(), the hardware clocks are manually disabled before
devres cleans up the RTC character device:

drivers/rtc/rtc-ftrtc010.c:ftrtc010_rtc_remove() {
    ...
	if (!IS_ERR(rtc->extclk))
		clk_disable_unprepare(rtc->extclk);
	if (!IS_ERR(rtc->pclk))
		clk_disable_unprepare(rtc->pclk);
}

Because the RTC device was registered with devm_rtc_register_device(), it
remains registered until devres cleanup runs after the .remove callback
returns.

Could an unprivileged process with an open file descriptor to /dev/rtc0 issue
an ioctl(RTC_RD_TIME) during this window?  Wouldn't this invoke
ftrtc010_rtc_read_time() which performs MMIO reads on the clock-gated
hardware, resulting in a synchronous external abort and crashing the system?

Should the clocks be managed with devm_clk_get_enabled() or
devm_add_action_or_reset(), tying their lifecycle to devres and eliminating
the need for manual teardown in ftrtc010_rtc_remove()?

[Severity: High]
This is a pre-existing issue, but the shared interrupt handler unconditionally
returns IRQ_HANDLED without checking hardware registers.

The driver explicitly registers the interrupt as shared in
ftrtc010_rtc_probe():

drivers/rtc/rtc-ftrtc010.c:ftrtc010_rtc_probe() {
    ...
	ret = devm_request_irq(dev, rtc->rtc_irq, ftrtc010_rtc_interrupt,
			       IRQF_SHARED, pdev->name, dev);
    ...
}

However, the handler blindly claims the interrupt, masking 'nobody cared'
warnings for the shared line:

drivers/rtc/rtc-ftrtc010.c:ftrtc010_rtc_interrupt() {
	return IRQ_HANDLED;
}

If another device shares this IRQ line and asserts an interrupt, the IRQ core
calls all registered handlers.  Since ftrtc010_rtc_interrupt() unconditionally
returns IRQ_HANDLED without checking device-specific registers, won't the
IRQ core incorrectly assume the interrupt was legitimately handled?

If the actual interrupting device is level-triggered and its driver is
unbound or fails to clear the interrupt, couldn't this cause an endless
interrupt loop that completely freezes the system because the IRQ line
remains asserted and the IRQ core will not disable it?

Should the interrupt handler read a status register to verify if the RTC
actually generated the interrupt and return IRQ_NONE if it did not?

[Severity: High]
This isn't a bug introduced by this patch, but there is an integer overflow in
ftrtc010_rtc_read_time() due to 32-bit arithmetic wrapping.

In ftrtc010_rtc_read_time(), the calculation for time evaluates entirely in
32-bit math:

drivers/rtc/rtc-ftrtc010.c:ftrtc010_rtc_read_time() {
    ...
	u32 days, hour, min, sec, offset;
	timeu64_t time;
    ...
	time = offset + days * 86400 + hour * 3600 + min * 60 + sec;
    ...
}

Since offset, days, hour, min, and sec are all 32-bit unsigned integers,
won't this silently overflow and wrap around U32_MAX before being assigned to
the 64-bit result?

If a privileged user sets the RTC time to a value near or beyond
the year 2106 (U32_MAX seconds), wouldn't the reported time wrap around
incorrectly, causing data corruption and contradicting the driver's
rtc_dev->range_max configuration?

Should one of the operands be cast to u64 before the arithmetic operations?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/6A7910FC22A9FEF1+20260827015739.3314298-1-liudalin@kylinsec.com.cn?part=1

  reply	other threads:[~2026-08-27  2:10 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-27  1:57 [PATCH] rtc: ftrtc010: fix clock resource leak on probe failure Liu Dalin
2026-08-27  2:10 ` sashiko-bot [this message]
2026-08-27  6:30   ` [PATCH v1 0/3] rtc: ftrtc010: fix resource management and integer overflow Liu Dalin
     [not found]   ` <20260827063019.3342085-1-liudalin@kylinsec.com.cn>
2026-08-27  6:30     ` [PATCH v1 1/3] rtc: ftrtc010: fix clock resource leak on probe failure Liu Dalin
2026-08-27  6:45       ` sashiko-bot
2026-08-27  8:05         ` liudalin
2026-08-27  6:30     ` [PATCH v1 2/3] rtc: ftrtc010: use devm clock APIs to fix use-after-disable in remove Liu Dalin
2026-08-27  6:43       ` sashiko-bot
2026-08-27  8:06         ` liudalin
2026-08-27  6:30     ` [PATCH v1 3/3] rtc: ftrtc010: fix integer overflow in time calculation Liu Dalin
2026-08-27  6:53       ` sashiko-bot
2026-08-27  8:07         ` liudalin
2026-08-27  8:03   ` [PATCH v2 0/3] rtc: ftrtc010: fix resource management and integer overflow Liu Dalin
     [not found]   ` <20260827080320.3351155-1-liudalin@kylinsec.com.cn>
2026-08-27  8:03     ` [PATCH v2 1/3] rtc: ftrtc010: fix clock resource leak on probe failure Liu Dalin
2026-08-27  8:15       ` sashiko-bot
2026-08-27  8:03     ` [PATCH v2 2/3] rtc: ftrtc010: use devm clock APIs to fix use-after-disable in remove Liu Dalin
2026-08-27  8:15       ` sashiko-bot
2026-08-27  8:03     ` [PATCH v2 3/3] rtc: ftrtc010: fix integer overflow in time calculation Liu Dalin
2026-08-27  8:17       ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260827021054.0963C1F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=alexandre.belloni@bootlin.com \
    --cc=linux-rtc@vger.kernel.org \
    --cc=liudalin@kylinsec.com.cn \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox