From: Karl Mehltretter <kmehltretter@gmail.com>
To: John Stultz <jstultz@google.com>, Thomas Gleixner <tglx@kernel.org>
Cc: "Karl Mehltretter" <kmehltretter@gmail.com>,
"Stephen Boyd" <sboyd@kernel.org>,
"Miroslav Lichvar" <mlichvar@redhat.com>,
"Deepa Dinamani" <deepa.kernel@gmail.com>,
"Cassio Neri" <cassio.neri@gmail.com>,
"Thomas Weißschuh" <thomas.weissschuh@linutronix.de>,
"David Laight" <david.laight.linux@gmail.com>,
linux-kernel@vger.kernel.org
Subject: [PATCH v2 1/2] time: Prevent time64_to_tm() day truncation on 32-bit
Date: Fri, 25 Sep 2026 18:57:01 +0200 [thread overview]
Message-ID: <20260925165702.51677-2-kmehltretter@gmail.com> (raw)
In-Reply-To: <20260925165702.51677-1-kmehltretter@gmail.com>
time64_to_tm() accepts a 64-bit seconds value, but stores the quotient in
long. On a 32-bit kernel the day count therefore wraps when it reaches
2^31, even though the corresponding year remains representable in
struct tm.
This is reachable when formatting externally supplied timestamps. For
example, NILFS recovery copies the little-endian 64-bit ss_create field
from an on-disk segment summary into time64_t. Its sysfs attributes then
print that value with %ptTs, whose formatter calls time64_to_tm(). A
corrupted image can consequently produce an architecture-dependent printed
date.
Keep the day quotient in s64 while leaving the seconds-within-day remainder
as long. Use div_s64_rem() for the weekday calculation so 32-bit builds do
not require compiler runtime division helpers.
Fixes: e6c2682a1da3 ("time: Add time64_to_tm()")
Assisted-by: LLM
Reviewed-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
Signed-off-by: Karl Mehltretter <kmehltretter@gmail.com>
---
kernel/time/timeconv.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/kernel/time/timeconv.c b/kernel/time/timeconv.c
index 59b922c826e7..f1cb61e0b114 100644
--- a/kernel/time/timeconv.c
+++ b/kernel/time/timeconv.c
@@ -49,7 +49,8 @@ void time64_to_tm(time64_t totalsecs, int offset, struct tm *result)
u32 u32tmp, day_of_century, year_of_century, day_of_year, month, day;
u64 u64tmp, udays, century, year;
bool is_Jan_or_Feb, is_leap_year;
- long days, rem;
+ long rem;
+ s64 days;
int remainder;
days = div_s64_rem(totalsecs, SECS_PER_DAY, &remainder);
@@ -70,7 +71,8 @@ void time64_to_tm(time64_t totalsecs, int offset, struct tm *result)
result->tm_sec = rem % 60;
/* January 1, 1970 was a Thursday. */
- result->tm_wday = (4 + days) % 7;
+ div_s64_rem(days + 4, 7, &remainder);
+ result->tm_wday = remainder;
if (result->tm_wday < 0)
result->tm_wday += 7;
--
2.39.5 (Apple Git-154)
next prev parent reply other threads:[~2026-09-25 16:57 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-25 16:57 [PATCH v2 0/2] " Karl Mehltretter
2026-09-25 16:57 ` Karl Mehltretter [this message]
2026-09-29 19:22 ` [tip: timers/core] " tip-bot2 for Karl Mehltretter
2026-09-25 16:57 ` [PATCH v2 2/2] time/kunit: Add time64_to_tm() case beyond 32-bit day count Karl Mehltretter
2026-09-29 19:22 ` [tip: timers/core] " tip-bot2 for Karl Mehltretter
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=20260925165702.51677-2-kmehltretter@gmail.com \
--to=kmehltretter@gmail.com \
--cc=cassio.neri@gmail.com \
--cc=david.laight.linux@gmail.com \
--cc=deepa.kernel@gmail.com \
--cc=jstultz@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mlichvar@redhat.com \
--cc=sboyd@kernel.org \
--cc=tglx@kernel.org \
--cc=thomas.weissschuh@linutronix.de \
/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
all inboxes | Powered by JetHome®