Skip to contentSkip to conversation

rtc: tps65910: add bounds checking

PATCH RESEND In review SZ Lin (林上智) 2 participants 2 messages Apr 10, 2018 Real time clock (RTC)

lore
Share & tools
lore
#0 lore raw

PATCH RESEND 1/1 rtc: tps65910: add bounds checking

Since tm->tm_year is the offset relative to 1900, it needs to check for positive value to avoid overflow in YEARS_REG register.

The system will hang at the following stage on the next boot when overflow has occurred.

[    2.221192] systemd[1]: Time has been changed

This patch fixes the above hanging issue by adding bounds checking.

---
 drivers/rtc/rtc-tps65910.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/rtc/rtc-tps65910.c +6 -0
index d0244d7979fc..6be9f7bb0f1b 100644
@@ -105,6 +105,12 @@ static int tps65910_rtc_set_time(struct device *dev, struct rtc_time *tm)
 	struct tps65910 *tps = dev_get_drvdata(dev->parent);
 	int ret;
 
+	if ((tm->tm_year - 100) < 0) {
+		dev_err(dev, "the requested year %d is unsupported\n",
+			1900 + tm->tm_year);
+		return -EINVAL;
+	}
+
 	rtc_data[0] = bin2bcd(tm->tm_sec);
 	rtc_data[1] = bin2bcd(tm->tm_min);
 	rtc_data[2] = bin2bcd(tm->tm_hour);
Signature · 1 line
2.17.0
#1 lore raw

Hi,

Quoted 10 lines · SZ Lin (林上智)

Since tm->tm_year is the offset relative to 1900, it needs to check for positive value to avoid overflow in YEARS_REG register.

The system will hang at the following stage on the next boot when overflow has occurred.

[    2.221192] systemd[1]: Time has been changed

This patch fixes the above hanging issue by adding bounds checking.

Did you test linux-next, this should not happen anymore after
https://git.kernel.org/pub/scm/linux/kernel/git/abelloni/linux.git/commit/?h=rtc-next&id=b3a5ac42ab18b7d1a8f2f072ca0ee76a3b754a43

Quoted 26 lines
---
 drivers/rtc/rtc-tps65910.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/rtc/rtc-tps65910.c +6 -0
index d0244d7979fc..6be9f7bb0f1b 100644
@@ -105,6 +105,12 @@ static int tps65910_rtc_set_time(struct device *dev, struct rtc_time *tm)
 	struct tps65910 *tps = dev_get_drvdata(dev->parent);
 	int ret;
 
+	if ((tm->tm_year - 100) < 0) {
+		dev_err(dev, "the requested year %d is unsupported\n",
+			1900 + tm->tm_year);
+		return -EINVAL;
+	}
+
 	rtc_data[0] = bin2bcd(tm->tm_sec);
 	rtc_data[1] = bin2bcd(tm->tm_min);
 	rtc_data[2] = bin2bcd(tm->tm_hour);
Signature · 1 line
2.17.0
Signature · 3 lines
Alexandre Belloni, Bootlin (formerly Free Electrons)
Embedded Linux and Kernel engineering
https://bootlin.com