Skip to contentSkip to conversation

rtc: wilco-ec: Sanitize values received from RTC

PATCH In review Nick Crews 2 participants 5 messages Sep 10, 2019 – Sep 16, 2019 Real time clock (RTC)

lore
Share & tools
lore

Nick Crews

author
#0 lore raw

PATCH 1/1 rtc: wilco-ec: Sanitize values received from RTC

Check that the time received from the RTC HW is valid, otherwise the computation of rtc_year_days() in the next line could, and sometimes does, crash the kernel.

While we're at it, fix the license to plain "GPL".

---
 drivers/rtc/rtc-wilco-ec.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/rtc/rtc-wilco-ec.c +10 -2
index 8ad4c4e6d557..0ccbf2dce832 100644
@@ -110,8 +110,16 @@ static int wilco_ec_rtc_read(struct device *dev, struct rtc_time *tm)
 	tm->tm_mday	= rtc.day;
 	tm->tm_mon	= rtc.month - 1;
 	tm->tm_year	= rtc.year + (rtc.century * 100) - 1900;
-	tm->tm_yday	= rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year);
 
+	if (rtc_valid_tm(tm)) {
+		dev_warn(dev,
+			 "Time computed from EC RTC is invalid: sec=%d, min=%d, hour=%d, mday=%d, mon=%d, year=%d",
+			 tm->tm_sec, tm->tm_min, tm->tm_hour, tm->mday,
+			 tm->mon, tm->year);
+		return -EIO;
+	}
+
+	tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year);
 	/* Don't compute day of week, we don't need it. */
 	tm->tm_wday = -1;
 
@@ -188,5 +196,5 @@ module_platform_driver(wilco_ec_rtc_driver);
 
 MODULE_ALIAS("platform:rtc-wilco-ec");
 MODULE_AUTHOR("Nick Crews <[email protected]>");
-MODULE_LICENSE("GPL v2");
+MODULE_LICENSE("GPL");
 MODULE_DESCRIPTION("Wilco EC RTC driver");
Signature · 1 line
2.11.0
#1 lore raw

Hi Nick,

Quoted 16 lines · Nick Crews

Check that the time received from the RTC HW is valid, otherwise the computation of rtc_year_days() in the next line could, and sometimes does, crash the kernel.

While we're at it, fix the license to plain "GPL".

---
 drivers/rtc/rtc-wilco-ec.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/rtc/rtc-wilco-ec.c +0 -0
index 8ad4c4e6d557..0ccbf2dce832 100644

drivers/rtc/rtc-wilco-ec.c static int wilco_ec_rtc_read(struct device *dev, struct rtc_time *tm) · line 113

Quoted 4 lines
drivers/rtc/rtc-wilco-ec.c +0 -1
@@ -110,8 +110,16 @@ static int wilco_ec_rtc_read(struct device *dev, struct rtc_time *tm)
 	tm->tm_mday	= rtc.day;
 	tm->tm_mon	= rtc.month - 1;
 	tm->tm_year	= rtc.year + (rtc.century * 100) - 1900;
-	tm->tm_yday	= rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year);

If your driver doesn't care about yday, userspace doesn't either. You can simply not set it.

Quoted 14 lines
drivers/rtc/rtc-wilco-ec.c +9 -0
+	if (rtc_valid_tm(tm)) {
+		dev_warn(dev,
+			 "Time computed from EC RTC is invalid: sec=%d, min=%d, hour=%d, mday=%d, mon=%d, year=%d",
+			 tm->tm_sec, tm->tm_min, tm->tm_hour, tm->mday,
+			 tm->mon, tm->year);
+		return -EIO;
+	}
+
+	tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year);
 	/* Don't compute day of week, we don't need it. */
 	tm->tm_wday = -1;
 

drivers/rtc/rtc-wilco-ec.c module_platform_driver(wilco_ec_rtc_driver); · line 199

Quoted 5 lines
drivers/rtc/rtc-wilco-ec.c +1 -1
@@ -188,5 +196,5 @@ module_platform_driver(wilco_ec_rtc_driver);
 
 MODULE_ALIAS("platform:rtc-wilco-ec");
 MODULE_AUTHOR("Nick Crews <[email protected]>");
-MODULE_LICENSE("GPL v2");
+MODULE_LICENSE("GPL");

This should be in a separate patch.

Quoted 4 lines
drivers/rtc/rtc-wilco-ec.c +0 -0
 MODULE_DESCRIPTION("Wilco EC RTC driver");
Signature · 1 line
2.11.0
Signature · 3 lines
Alexandre Belloni, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
author
#2 lore raw

Hi Alexandre, thanks for the thoughts.

Quoted 15 lines · Alexandre Belloni

Hi Nick,

Quoted 11 lines · Nick Crews

Check that the time received from the RTC HW is valid, otherwise the computation of rtc_year_days() in the next line could, and sometimes does, crash the kernel.

While we're at it, fix the license to plain "GPL".

---
 drivers/rtc/rtc-wilco-ec.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
Quoted 12 lines

drivers/rtc/rtc-wilco-ec.c static int wilco_ec_rtc_read(struct device *dev, struct rtc_time *tm) · line 113

Quoted 9 lines
drivers/rtc/rtc-wilco-ec.c +0 -1
index 8ad4c4e6d557..0ccbf2dce832 100644
@@ -110,8 +110,16 @@ static int wilco_ec_rtc_read(struct device *dev, struct rtc_time *tm)
      tm->tm_mday     = rtc.day;
      tm->tm_mon      = rtc.month - 1;
      tm->tm_year     = rtc.year + (rtc.century * 100) - 1900;
-     tm->tm_yday     = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year);

If your driver doesn't care about yday, userspace doesn't either. You can simply not set it.

This driver indeed does not care about yday, so it sounds good to me to simply not set it. However, I do still want to worry about the HW returning some bogus time. It could be indicative of some other problem, and I don't want to pass on this illegal time further up the stack. I can't think of a reason why an illegal time should exist at all, we should just deal with it as soon as possible. How about I remove setting yday as you suggest, but still keep the rtc_valid_tm() check?

Quoted 13 lines

drivers/rtc/rtc-wilco-ec.c static int wilco_ec_rtc_read(struct device *dev, struct rtc_time *tm) · line 124

Quoted 13 lines
drivers/rtc/rtc-wilco-ec.c +9 -0
+     if (rtc_valid_tm(tm)) {
+             dev_warn(dev,
+                      "Time computed from EC RTC is invalid: sec=%d, min=%d, hour=%d, mday=%d, mon=%d, year=%d",
+                      tm->tm_sec, tm->tm_min, tm->tm_hour, tm->mday,
+                      tm->mon, tm->year);
+             return -EIO;
+     }
+
+     tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year);
      /* Don't compute day of week, we don't need it. */
      tm->tm_wday = -1;

Following our discussion, perhaps I'll just remove this too since the RTC core inits this to -1 already?

Quoted 9 lines

drivers/rtc/rtc-wilco-ec.c module_platform_driver(wilco_ec_rtc_driver); · line 199

Quoted 7 lines
drivers/rtc/rtc-wilco-ec.c +1 -1
@@ -188,5 +196,5 @@ module_platform_driver(wilco_ec_rtc_driver);
 
 MODULE_ALIAS("platform:rtc-wilco-ec");
 MODULE_AUTHOR("Nick Crews <[email protected]>");
-MODULE_LICENSE("GPL v2");
+MODULE_LICENSE("GPL");

This should be in a separate patch.

OK, I'll separate this out :)

Quoted 10 lines
Quoted 5 lines
drivers/rtc/rtc-wilco-ec.c +0 -1
 MODULE_DESCRIPTION("Wilco EC RTC driver");
--

2.11.0

--
Alexandre Belloni, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com

#3 lore raw
Quoted 32 lines · Nick Crews

Hi Alexandre, thanks for the thoughts.

Quoted 15 lines · Alexandre Belloni

Hi Nick,

Quoted 11 lines · Nick Crews

Check that the time received from the RTC HW is valid, otherwise the computation of rtc_year_days() in the next line could, and sometimes does, crash the kernel.

While we're at it, fix the license to plain "GPL".

---
 drivers/rtc/rtc-wilco-ec.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
Quoted 12 lines

drivers/rtc/rtc-wilco-ec.c static int wilco_ec_rtc_read(struct device *dev, struct rtc_time *tm) · line 113

Quoted 9 lines
drivers/rtc/rtc-wilco-ec.c +0 -1
index 8ad4c4e6d557..0ccbf2dce832 100644
@@ -110,8 +110,16 @@ static int wilco_ec_rtc_read(struct device *dev, struct rtc_time *tm)
      tm->tm_mday     = rtc.day;
      tm->tm_mon      = rtc.month - 1;
      tm->tm_year     = rtc.year + (rtc.century * 100) - 1900;
-     tm->tm_yday     = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year);

If your driver doesn't care about yday, userspace doesn't either. You can simply not set it.

Quoted 8 lines

This driver indeed does not care about yday, so it sounds good to me to simply not set it. However, I do still want to worry about the HW returning some bogus time. It could be indicative of some other problem, and I don't want to pass on this illegal time further up the stack. I can't think of a reason why an illegal time should exist at all, we should just deal with it as soon as possible. How about I remove setting yday as you suggest, but still keep the rtc_valid_tm() check?

As rtc_valid_tm would be the last thing the driver does in the callback and the first thing the core does after returning from the callback, you'd get two calls back to back which is not useful.

Quoted 14 lines
Quoted 13 lines

drivers/rtc/rtc-wilco-ec.c static int wilco_ec_rtc_read(struct device *dev, struct rtc_time *tm) · line 124

Quoted 13 lines
drivers/rtc/rtc-wilco-ec.c +9 -0
+     if (rtc_valid_tm(tm)) {
+             dev_warn(dev,
+                      "Time computed from EC RTC is invalid: sec=%d, min=%d, hour=%d, mday=%d, mon=%d, year=%d",
+                      tm->tm_sec, tm->tm_min, tm->tm_hour, tm->mday,
+                      tm->mon, tm->year);
+             return -EIO;
+     }
+
+     tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year);
      /* Don't compute day of week, we don't need it. */
      tm->tm_wday = -1;
Quoted 3 lines

Following our discussion, perhaps I'll just remove this too since the RTC core inits this to -1 already?

You can remove it.

Signature · 3 lines
Alexandre Belloni, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
author
#4 lore raw
Quoted 43 lines · Alexandre Belloni
Quoted 32 lines · Nick Crews

Hi Alexandre, thanks for the thoughts.

Quoted 15 lines · Alexandre Belloni

Hi Nick,

Quoted 11 lines · Nick Crews

Check that the time received from the RTC HW is valid, otherwise the computation of rtc_year_days() in the next line could, and sometimes does, crash the kernel.

While we're at it, fix the license to plain "GPL".

---
 drivers/rtc/rtc-wilco-ec.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
Quoted 12 lines

drivers/rtc/rtc-wilco-ec.c static int wilco_ec_rtc_read(struct device *dev, struct rtc_time *tm) · line 113

Quoted 9 lines
drivers/rtc/rtc-wilco-ec.c +0 -1
index 8ad4c4e6d557..0ccbf2dce832 100644
@@ -110,8 +110,16 @@ static int wilco_ec_rtc_read(struct device *dev, struct rtc_time *tm)
      tm->tm_mday     = rtc.day;
      tm->tm_mon      = rtc.month - 1;
      tm->tm_year     = rtc.year + (rtc.century * 100) - 1900;
-     tm->tm_yday     = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year);

If your driver doesn't care about yday, userspace doesn't either. You can simply not set it.

Quoted 8 lines

This driver indeed does not care about yday, so it sounds good to me to simply not set it. However, I do still want to worry about the HW returning some bogus time. It could be indicative of some other problem, and I don't want to pass on this illegal time further up the stack. I can't think of a reason why an illegal time should exist at all, we should just deal with it as soon as possible. How about I remove setting yday as you suggest, but still keep the rtc_valid_tm() check?

Quoted 3 lines

As rtc_valid_tm would be the last thing the driver does in the callback and the first thing the core does after returning from the callback, you'd get two calls back to back which is not useful.

Ah, I see now where the core checks that the time is valid in interface.c. Sounds good, I'll skip the check then.

Quoted 15 lines
Quoted 15 lines
Quoted 14 lines
Quoted 14 lines
drivers/rtc/rtc-wilco-ec.c +9 -0
+     if (rtc_valid_tm(tm)) {
+             dev_warn(dev,
+                      "Time computed from EC RTC is invalid: sec=%d, min=%d, hour=%d, mday=%d, mon=%d, year=%d",
+                      tm->tm_sec, tm->tm_min, tm->tm_hour, tm->mday,
+                      tm->mon, tm->year);
+             return -EIO;
+     }
+
+     tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year);
      /* Don't compute day of week, we don't need it. */
      tm->tm_wday = -1;
Quoted 5 lines
Quoted 3 lines

Following our discussion, perhaps I'll just remove this too since the RTC core inits this to -1 already?

You can remove it.

Will do!

Quoted 6 lines

--
Alexandre Belloni, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com