Skip to contentSkip to conversation

time: Prevent time64_to_tm() day truncation on 32-bit

PATCH Applied Karl Mehltretter 2 participants 5 messages Sep 25 – Sep 29 Timekeeping, clocksource core, NTP, alarmtimer

lore
Share & tools
lore
#0 lore raw

v2 cover time: Prevent time64_to_tm() day truncation on 32-bit

time64_to_tm() keeps the day count in a long, which wraps on 32-bit kernels once it reaches 2^31 days (about 5.88 million years after the epoch). Patch 1 widens it to s64; patch 2 adds a KUnit case at that boundary.

Why years beyond LONG_MAX are not handled: with this series the calculation is correct on 32-bit for every year that fits struct tm's long tm_year. Timestamps whose year - 1900 exceeds LONG_MAX (about 2.1 billion years; time64_t reaches about 292 billion) still truncate, but only in the final assignment to tm_year. Fixing that means widening tm_year for every struct tm user, so I have left it out of this fix.

Changes in v2:
- Split the KUnit case into its own patch so the fix can be backported on its own (Thomas).
- Declare 'long rem;' before 's64 days;' (Thomas).
- Carried Thomas' Reviewed-by on both patches; the code is unchanged apart from the declaration order.

Testing: time_test_cases KUnit suite under QEMU, i386 and arm, built with clang 22 (LLVM=1); arm also with gcc 13.3. With the series both cases pass. With only patch 2 applied the new case fails on both architectures with tm_year=-5879541 and wrong tm_mon, tm_mday, tm_yday and tm_wday. timeconv.o references only div_s64_rem() and div64_u64_rem(), no compiler runtime division helpers.

v1: https://lore.kernel.org/r/8d421725051450cecf0a7e6d2c514b6926df43b1.1788019619.git.kmehltretter@gmail.com

Karl Mehltretter (2):
time: Prevent time64_to_tm() day truncation on 32-bit time/kunit: Add time64_to_tm() case beyond 32-bit day count

 kernel/time/time_test.c | 16 ++++++++++++++++
 kernel/time/timeconv.c  |  6 ++++--
 2 files changed, 20 insertions(+), 2 deletions(-)

base-commit: 50d05c7c76c96b90462f24debacca971d2e86713

Signature · 1 line
2.39.5 (Apple Git-154)
#1 lore raw

PATCH v2 1/2 time: Prevent time64_to_tm() day truncation on 32-bit

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
---
 kernel/time/timeconv.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/time/timeconv.c +4 -2
index 59b922c826e7..f1cb61e0b114 100644
@@ -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;
 
Signature · 1 line
2.39.5 (Apple Git-154)
#2 lore raw
The following commit has been merged into the timers/core branch of tip:

The following commit has been merged into the timers/core branch of tip:

Commit-ID:     2927f7ca78448781e11a4623d347cd653c28adf6
Gitweb:        https://git.kernel.org/tip/2927f7ca78448781e11a4623d347cd653c28adf6
Author:        Karl Mehltretter <[email protected]>
AuthorDate:    Fri, 25 Sep 2026 18:57:01 +02:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Tue, 29 Sep 2026 21:18:42 +02:00

time: Prevent time64_to_tm() day truncation on 32-bit

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.

---
 kernel/time/timeconv.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/time/timeconv.c +4 -2
index 59b922c..aed3af9 100644
@@ -49,8 +49,9 @@ 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;
 	int remainder;
+	long rem;
+	s64 days;
 
 	days = div_s64_rem(totalsecs, SECS_PER_DAY, &remainder);
 	rem = 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;
 
#3 lore raw

PATCH v2 2/2 time/kunit: Add time64_to_tm() case beyond 32-bit day count

In reply to Karl Mehltretter

The existing date range test covers +/-80,000 years around the epoch, far below the day count at which a 32-bit long wraps (2^31 days, roughly 5.88 million years).

Add a case at exactly 2^31 days after the epoch. Without the preceding fix a 32-bit kernel returns tm_year=-5879541 and wrong month, day, day-of-year and weekday; with it the result matches 64-bit kernels.

---
 kernel/time/time_test.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
kernel/time/time_test.c +16 -0
index 1b99180da288..8b718767b3ba 100644
@@ -87,8 +87,24 @@ static void time64_to_tm_test_date_range(struct kunit *test)
 	}
 }
 
+static void time64_to_tm_test_wide_day_count(struct kunit *test)
+{
+	/* 2^31 days: the first count that does not fit in a 32-bit long. */
+	time64_t timestamp = (1LL << 31) * 86400;
+	struct tm result;
+
+	time64_to_tm(timestamp, 0, &result);
+
+	KUNIT_EXPECT_EQ(test, result.tm_year, 5879680);
+	KUNIT_EXPECT_EQ(test, result.tm_mon, 6);
+	KUNIT_EXPECT_EQ(test, result.tm_mday, 12);
+	KUNIT_EXPECT_EQ(test, result.tm_yday, 193);
+	KUNIT_EXPECT_EQ(test, result.tm_wday, 6);
+}
+
 static struct kunit_case time_test_cases[] = {
 	KUNIT_CASE_SLOW(time64_to_tm_test_date_range),
+	KUNIT_CASE(time64_to_tm_test_wide_day_count),
 	{}
 };
 
Signature · 1 line
2.39.5 (Apple Git-154)
#4 lore raw

[tip: timers/core] time/kunit: Add time64_to_tm() case beyond 32-bit day count

The following commit has been merged into the timers/core branch of tip:

The following commit has been merged into the timers/core branch of tip:

Commit-ID:     3945c4a3ea151f777b4b098ebeecd2f85c20244f
Gitweb:        https://git.kernel.org/tip/3945c4a3ea151f777b4b098ebeecd2f85c20244f
Author:        Karl Mehltretter <[email protected]>
AuthorDate:    Fri, 25 Sep 2026 18:57:02 +02:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Tue, 29 Sep 2026 21:18:42 +02:00

time/kunit: Add time64_to_tm() case beyond 32-bit day count

The existing date range test covers +/-80,000 years around the epoch, far below the day count at which a 32-bit long wraps (2^31 days, roughly 5.88 million years).

Add a case at exactly 2^31 days after the epoch. Without the preceding fix a 32-bit kernel returns tm_year=-5879541 and wrong month, day, day-of-year and weekday; with it the result matches 64-bit kernels.

---
 kernel/time/time_test.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
kernel/time/time_test.c +16 -0
index 1b99180..8b71876 100644
@@ -87,8 +87,24 @@ static void time64_to_tm_test_date_range(struct kunit *test)
 	}
 }
 
+static void time64_to_tm_test_wide_day_count(struct kunit *test)
+{
+	/* 2^31 days: the first count that does not fit in a 32-bit long. */
+	time64_t timestamp = (1LL << 31) * 86400;
+	struct tm result;
+
+	time64_to_tm(timestamp, 0, &result);
+
+	KUNIT_EXPECT_EQ(test, result.tm_year, 5879680);
+	KUNIT_EXPECT_EQ(test, result.tm_mon, 6);
+	KUNIT_EXPECT_EQ(test, result.tm_mday, 12);
+	KUNIT_EXPECT_EQ(test, result.tm_yday, 193);
+	KUNIT_EXPECT_EQ(test, result.tm_wday, 6);
+}
+
 static struct kunit_case time_test_cases[] = {
 	KUNIT_CASE_SLOW(time64_to_tm_test_date_range),
+	KUNIT_CASE(time64_to_tm_test_wide_day_count),
 	{}
 };