linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/1] x86/kernel/rtc: add sanity check for RTC date and time
@ 2021-06-14 11:09 Werner Zeh
  2021-06-24  8:15 ` Werner Zeh
  0 siblings, 1 reply; 9+ messages in thread
From: Werner Zeh @ 2021-06-14 11:09 UTC (permalink / raw)
  To: tglx, mingo, bp, x86, linux-kernel; +Cc: Werner Zeh

The timekeeper is synchronized with the CMOS RTC when it is initialized.
If the RTC buffering is bad (not buffered at all, empty battery) the RTC
registers can contain random data. In order to avoid date and time
being completely rubbish check the sanity of the registers before
calling mktime64. If the values are not valid, set tv_sec to 0 so that
at least the starting time is valid.

Signed-off-by: Werner Zeh <werner.zeh@siemens.com>
---
[resent due to wrong lkml address]
 arch/x86/kernel/rtc.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kernel/rtc.c b/arch/x86/kernel/rtc.c
index 586f718b8e95..f4af7b18c6c0 100644
--- a/arch/x86/kernel/rtc.c
+++ b/arch/x86/kernel/rtc.c
@@ -9,6 +9,7 @@
 #include <linux/export.h>
 #include <linux/pnp.h>
 #include <linux/of.h>
+#include <linux/rtc.h>
 
 #include <asm/vsyscall.h>
 #include <asm/x86_init.h>
@@ -64,6 +65,7 @@ void mach_get_cmos_time(struct timespec64 *now)
 {
 	unsigned int status, year, mon, day, hour, min, sec, century = 0;
 	unsigned long flags;
+	struct rtc_time tm = {0};
 
 	/*
 	 * If pm_trace abused the RTC as storage, set the timespec to 0,
@@ -118,7 +120,15 @@ void mach_get_cmos_time(struct timespec64 *now)
 	} else
 		year += CMOS_YEARS_OFFS;
 
-	now->tv_sec = mktime64(year, mon, day, hour, min, sec);
+	tm.tm_sec = sec;
+	tm.tm_min = min;
+	tm.tm_hour = hour;
+	tm.tm_mday = day;
+	tm.tm_mon = mon;
+	tm.tm_year = year;
+	now->tv_sec = 0;
+	if (rtc_valid_tm(&tm) == 0)
+		now->tv_sec = mktime64(year, mon, day, hour, min, sec);
 	now->tv_nsec = 0;
 }
 
-- 
2.21.3


^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2021-12-17 21:53 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-14 11:09 [PATCH 1/1] x86/kernel/rtc: add sanity check for RTC date and time Werner Zeh
2021-06-24  8:15 ` Werner Zeh
2021-06-28 16:34   ` Alexandre Belloni
2021-06-30  6:25     ` AW: " Zeh, Werner
2021-07-02 14:10       ` Alexandre Belloni
2021-12-09  8:05         ` Zeh, Werner
2021-12-09  8:50           ` Alexandre Belloni
2021-12-09  9:07             ` Zeh, Werner
2021-12-17 21:53               ` Alexandre Belloni

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).