All of lore.kernel.org
 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 v1 3/3] rtc: ftrtc010: fix integer overflow in time calculation
Date: Thu, 27 Aug 2026 06:53:12 +0000	[thread overview]
Message-ID: <20260827065312.CECC11F000E9@smtp.kernel.org> (raw)
In-Reply-To: <B06A3E7BF268E081+20260827063019.3342085-4-liudalin@kylinsec.com.cn>

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

New issues:
- [High] Transition to 64-bit arithmetic zero-extends negative time offsets, causing the clock to erroneously jump ~136 years into the future.

Pre-existing issues:
- [High] Unsynchronized, non-atomic reads of consecutive RTC time registers can produce severely corrupted timestamps if a hardware tick and rollover occurs during the read sequence.
--

commit 0451b19111beae8b1f5abde3e58d176d4ad642f2
Author: Liu Dalin <liudalin@kylinsec.com.cn>

rtc: ftrtc010: fix integer overflow in time calculation

This commit addresses an integer overflow in ftrtc010_rtc_read_time and
ftrtc010_rtc_set_time. It casts the 32-bit time variables to timeu64_t before
multiplication to ensure 64-bit arithmetic, preventing overflow for dates
beyond year 2106.

> diff --git a/drivers/rtc/rtc-ftrtc010.c b/drivers/rtc/rtc-ftrtc010.c
> index 2f610a0fc6e9b..6fff537cad27e 100644
> --- a/drivers/rtc/rtc-ftrtc010.c
> +++ b/drivers/rtc/rtc-ftrtc010.c
> @@ -73,7 +73,8 @@ static int ftrtc010_rtc_read_time(struct device *dev, struct rtc_time *tm)
>  	days = readl(rtc->rtc_base + FTRTC010_RTC_DAYS);
>  	offset = readl(rtc->rtc_base + FTRTC010_RTC_RECORD);

[Severity: High]
This isn't a bug introduced by this patch, but can these unsynchronized,
non-atomic reads of consecutive RTC time registers produce corrupted
timestamps?

If a hardware tick and rollover occurs during the read sequence:

ftrtc010_rtc_read_time()
    sec  = readl(rtc->rtc_base + FTRTC010_RTC_SECOND);
    min  = readl(rtc->rtc_base + FTRTC010_RTC_MINUTE);
    ...

It appears possible to read desynchronized values, which could result in
permanent time corruption up to 1 day.

> -	time = offset + days * 86400 + hour * 3600 + min * 60 + sec;
> +	time = (timeu64_t)offset + (timeu64_t)days * 86400 +
> +	       (timeu64_t)hour * 3600 + (timeu64_t)min * 60 + sec;

[Severity: High]
Does this transition to 64-bit arithmetic zero-extend negative time offsets?

If the offset is negative, casting it directly to timeu64_t breaks the two's
complement modulo arithmetic required to correctly decode negative time
differences, which could cause the clock to erroneously jump about 136 years
into the future.

> @@ -93,7 +94,8 @@ static int ftrtc010_rtc_set_time(struct device *dev, struct rtc_time *tm)
>  	hour = readl(rtc->rtc_base + FTRTC010_RTC_HOUR);
>  	day = readl(rtc->rtc_base + FTRTC010_RTC_DAYS);

[Severity: High]
This isn't a bug introduced by this patch, but can this sequential read
result in computing an improperly desynchronized offset?

If a tick occurs between reads:

ftrtc010_rtc_set_time()
    sec = readl(rtc->rtc_base + FTRTC010_RTC_SECOND);
    min = readl(rtc->rtc_base + FTRTC010_RTC_MINUTE);
    ...

It seems this could permanently corrupt the stored RTC offset and skew the
time until it is reset.

> -	offset = time - (day * 86400 + hour * 3600 + min * 60 + sec);
> +	offset = time - ((timeu64_t)day * 86400 + (timeu64_t)hour * 3600 +
> +			 (timeu64_t)min * 60 + sec);

[Severity: High]
Does this calculation incorrectly wrap offset into a large 32-bit integer?

If the target Unix time is numerically smaller than the hardware's internal
uptime counters, the difference becomes negative. This can happen on offline
embedded devices where the initial OS time defaults to 1970-01-01.

Casting this negative 32-bit offset to timeu64_t zero-extends it instead of
allowing the required 32-bit modulo arithmetic. This could lead to a massive
forward jump when ftrtc010_rtc_read_time is subsequently called.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260827063019.3342085-1-liudalin@kylinsec.com.cn?part=3

  reply	other threads:[~2026-08-27  6:53 UTC|newest]

Thread overview: 25+ 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
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 [this message]
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
2026-09-14  8:38     ` Linus Walleij
2026-09-14  9:12       ` liudalin
2026-10-01 10:00     ` Alexandre Belloni
     [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-10-02 10:59       ` Dan Carpenter
2026-08-27  8:17       ` sashiko-bot
2026-10-08  2:10       ` Liu Dalin
2026-09-14  8:35 ` [PATCH] rtc: ftrtc010: fix clock resource leak on probe failure Linus Walleij

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=20260827065312.CECC11F000E9@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.