diff mbox series

[v2] rtc: cmos: zero-init wkalrm when reading from CMOS

Message ID 20200814191654.v2.1.Iaf7638a2f2a87ff68d85fcb8dec615e41340c97f@changeid
State Accepted
Headers show
Series [v2] rtc: cmos: zero-init wkalrm when reading from CMOS | expand

Commit Message

Victor Ding Aug. 14, 2020, 9:17 a.m. UTC
cmos_read_alarm() may leave certain fields of a struct rtc_wkalrm
untouched; therefore, these fields contain garbage if not properly
initialized, leading to inconsistent values when converting into
time64_t. This patch to zero initialize the struct before calling
cmos_read_alarm().

Note that this patch is not intended to produce a correct time64_t, it
is only to produce a consistent value. In the case of suspend/resume, a
correct time64_t is not necessary; a consistent value is sufficient to
correctly perform an equality test for t_current_expires and
t_saved_expires. Logic to deduce a correct time64_t is expensive and
hence should be avoided.

Signed-off-by: Victor Ding <victording@google.com>
---

Changes in v2:
    - Initialize the struct to 0 instead of -1;
    - Initialize the whole struct rtc_wkalrm.

 drivers/rtc/rtc-cmos.c | 2 ++
 1 file changed, 2 insertions(+)

Comments

Alexandre Belloni Aug. 20, 2020, 10:10 p.m. UTC | #1
On Fri, 14 Aug 2020 19:17:30 +1000, Victor Ding wrote:
> cmos_read_alarm() may leave certain fields of a struct rtc_wkalrm
> untouched; therefore, these fields contain garbage if not properly
> initialized, leading to inconsistent values when converting into
> time64_t. This patch to zero initialize the struct before calling
> cmos_read_alarm().
> 
> Note that this patch is not intended to produce a correct time64_t, it
> is only to produce a consistent value. In the case of suspend/resume, a
> correct time64_t is not necessary; a consistent value is sufficient to
> correctly perform an equality test for t_current_expires and
> t_saved_expires. Logic to deduce a correct time64_t is expensive and
> hence should be avoided.

Applied, thanks!

[1/1] rtc: cmos: zero-init wkalrm when reading from CMOS
      commit: c254bcd7231a3eeafc453f6ee3a483a2e7ff486e

Best regards,
diff mbox series

Patch

diff --git a/drivers/rtc/rtc-cmos.c b/drivers/rtc/rtc-cmos.c
index bcc96ab7793f..c633319cdb91 100644
--- a/drivers/rtc/rtc-cmos.c
+++ b/drivers/rtc/rtc-cmos.c
@@ -1006,6 +1006,7 @@  static int cmos_suspend(struct device *dev)
 			enable_irq_wake(cmos->irq);
 	}
 
+	memset(&cmos->saved_wkalrm, 0, sizeof(struct rtc_wkalrm));
 	cmos_read_alarm(dev, &cmos->saved_wkalrm);
 
 	dev_dbg(dev, "suspend%s, ctrl %02x\n",
@@ -1054,6 +1055,7 @@  static void cmos_check_wkalrm(struct device *dev)
 		return;
 	}
 
+	memset(&current_alarm, 0, sizeof(struct rtc_wkalrm));
 	cmos_read_alarm(dev, &current_alarm);
 	t_current_expires = rtc_tm_to_time64(&current_alarm.time);
 	t_saved_expires = rtc_tm_to_time64(&cmos->saved_wkalrm.time);