Skip to contentSkip to conversation

rtc: hctosys: Ensure system time doesn't overflow time_t

PATCH New Alexandre Belloni 1 participant 6 messages Mar 19, 2018

Subsystems (2)
lore
Share & tools
lore

Alexandre Belloni

author maintainer
#0 lore raw

PATCH 1/6 rtc: hctosys: Ensure system time doesn't overflow time_t

On 32bit platforms, time_t is still a signed 32bit long. If it is overflowed, userspace and the kernel cant agree on the current system time. This causes multiple issues, in particular with systemd:
https://github.com/systemd/systemd/issues/1143

A good workaround is to simply avoid using systohc which is something I greatly encourage as the time is better set by userspace.

However, many distribution enable it and use systemd which is rendering the system unusable in case the RTC holds a date after 2038 (and more so after 2106). Many drivers have workaround for this case and they should be eliminated so there is only one place left to fix when userspace is able to cope with dates after the 31bit overflow.

---
 drivers/rtc/hctosys.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/rtc/hctosys.c +5 -0
index e1cfa06810ef..e79f2a181ad2 100644
@@ -49,6 +49,11 @@ static int __init rtc_hctosys(void)
 
 	tv64.tv_sec = rtc_tm_to_time64(&tm);
 
+#if BITS_PER_LONG == 32
+	if (tv64.tv_sec > INT_MAX)
+		goto err_read;
+#endif
+
 	err = do_settimeofday64(&tv64);
 
 	dev_info(rtc->dev.parent,
Signature · 1 line
2.16.2
author maintainer
#1 lore raw

PATCH 2/6 rtc: mv: remove artificial limitation

Dates after 2038 actually fit on 32 bits. The counter will overflow in 2106. Also, it is bad practice to reset the RTC to a default value.

---
 drivers/rtc/rtc-mv.c | 12 ------------
 1 file changed, 12 deletions(-)
drivers/rtc/rtc-mv.c +0 -12
index 944c5c0fadd0..bc52dbb0c0e2 100644
@@ -223,7 +223,6 @@ static int __init mv_rtc_probe(struct platform_device *pdev)
 	struct resource *res;
 	struct rtc_plat_data *pdata;
 	u32 rtc_time;
-	u32 rtc_date;
 	int ret = 0;
 
 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
@@ -259,17 +258,6 @@ static int __init mv_rtc_probe(struct platform_device *pdev)
 		}
 	}
 
-	/*
-	 * A date after January 19th, 2038 does not fit on 32 bits and
-	 * will confuse the kernel and userspace. Reset to a sane date
-	 * (January 1st, 2013) if we're after 2038.
-	 */
-	rtc_date = readl(pdata->ioaddr + RTC_DATE_REG_OFFS);
-	if (bcd2bin((rtc_date >> RTC_YEAR_OFFS) & 0xff) >= 38) {
-		dev_info(&pdev->dev, "invalid RTC date, resetting to January 1st, 2013\n");
-		writel(0x130101, pdata->ioaddr + RTC_DATE_REG_OFFS);
-	}
-
 	pdata->irq = platform_get_irq(pdev, 0);
 
 	platform_set_drvdata(pdev, pdata);
Signature · 1 line
2.16.2
author maintainer
#2 lore raw

PATCH 3/6 rtc: mrst: remove artificial limitation

In reply to Alexandre Belloni

The hardware supports years up to 100 so don't limit the year to 2038 artificially.

---
 drivers/rtc/rtc-mrst.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/rtc/rtc-mrst.c +1 -1
index 901a8d170f68..fcb9de5218b2 100644
@@ -122,7 +122,7 @@ static int mrst_set_time(struct device *dev, struct rtc_time *time)
 	min = time->tm_min;
 	sec = time->tm_sec;
 
-	if (yrs < 72 || yrs > 138)
+	if (yrs < 72 || yrs > 172)
 		return -EINVAL;
 	yrs -= 72;
 
Signature · 1 line
2.16.2
author maintainer
#3 lore raw

PATCH 4/6 rtc: st-lpc: remove artificial limitation

In reply to Alexandre Belloni

The LPC RTC supports dates way beyond 2038, don't limit it artificially as the kernel handles dates after 2038 properly.

---
 drivers/rtc/rtc-st-lpc.c | 15 ---------------
 1 file changed, 15 deletions(-)
drivers/rtc/rtc-st-lpc.c +0 -15
index 82b0af159a28..dcdd2b0a58f7 100644
@@ -254,21 +254,6 @@ static int st_rtc_probe(struct platform_device *pdev)
 
 	platform_set_drvdata(pdev, rtc);
 
-	/*
-	 * The RTC-LPC is able to manage date.year > 2038
-	 * but currently the kernel can not manage this date!
-	 * If the RTC-LPC has a date.year > 2038 then
-	 * it's set to the epoch "Jan 1st 2000"
-	 */
-	st_rtc_read_time(&pdev->dev, &tm_check);
-
-	if (tm_check.tm_year >=  (2038 - 1900)) {
-		memset(&tm_check, 0, sizeof(tm_check));
-		tm_check.tm_year = 100;
-		tm_check.tm_mday = 1;
-		st_rtc_set_time(&pdev->dev, &tm_check);
-	}
-
 	rtc->rtc_dev = rtc_device_register("st-lpc-rtc", &pdev->dev,
 					   &st_rtc_ops, THIS_MODULE);
 	if (IS_ERR(rtc->rtc_dev)) {
Signature · 1 line
2.16.2
author maintainer
#4 lore raw

PATCH 5/6 rtc: 88pm80x: remove artificial limitation

In reply to Alexandre Belloni

The 88pm80x supports time up to 2106 (it is a 32 bit counter). Also, the year will never be before 1970 as the RTC core forbids that.

---
 drivers/rtc/rtc-88pm80x.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/rtc/rtc-88pm80x.c +2 -2
index 466bf7f9a285..6cbafefa80a2 100644
@@ -134,9 +134,9 @@ static int pm80x_rtc_set_time(struct device *dev, struct rtc_time *tm)
 	struct pm80x_rtc_info *info = dev_get_drvdata(dev);
 	unsigned char buf[4];
 	unsigned long ticks, base, data;
-	if ((tm->tm_year < 70) || (tm->tm_year > 138)) {
+	if (tm->tm_year > 206) {
 		dev_dbg(info->dev,
-			"Set time %d out of range. Please set time between 1970 to 2038.\n",
+			"Set time %d out of range. Please set time between 1970 to 2106.\n",
 			1900 + tm->tm_year);
 		return -EINVAL;
 	}
Signature · 1 line
2.16.2
author maintainer
#5 lore raw

PATCH 6/6 rtc: 88pm860x: remove artificial limitation

In reply to Alexandre Belloni

The 88pm860x supports time up to 2106 (it is a 32 bit counter). Also, the year will never be before 1970 as the RTC core forbids that.

---
 drivers/rtc/rtc-88pm860x.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/rtc/rtc-88pm860x.c +2 -2
index 19e53b3b8e00..01ffc0ef8033 100644
@@ -135,9 +135,9 @@ static int pm860x_rtc_set_time(struct device *dev, struct rtc_time *tm)
 	unsigned char buf[4];
 	unsigned long ticks, base, data;
 
-	if ((tm->tm_year < 70) || (tm->tm_year > 138)) {
+	if (tm->tm_year > 206) {
 		dev_dbg(info->dev, "Set time %d out of range. "
-			"Please set time between 1970 to 2038.\n",
+			"Please set time between 1970 to 2106.\n",
 			1900 + tm->tm_year);
 		return -EINVAL;
 	}
Signature · 1 line
2.16.2