[PATCH] rtc: fix use of uninit struct in rtc_read_alarm_internal

Bharadwaj Raju posted 1 patch 9 months ago
drivers/rtc/interface.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
[PATCH] rtc: fix use of uninit struct in rtc_read_alarm_internal
Posted by Bharadwaj Raju 9 months ago
The trace call invokes rtc_tm_to_time64 on a
potentially uninitialized alarm->time. Move the
trace call to the path where we do successfully
initialize and read that struct.

This fixes a KMSAN warning.

Fixes: 29a1f599c0cc ("rtc: Add tracepoints for RTC system")

Signed-off-by: Bharadwaj Raju <bharadwaj.raju777@gmail.com>
---
 drivers/rtc/interface.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c
index aaf76406cd7d..82ba33bf478b 100644
--- a/drivers/rtc/interface.c
+++ b/drivers/rtc/interface.c
@@ -201,11 +201,12 @@ static int rtc_read_alarm_internal(struct rtc_device *rtc,
 		alarm->time.tm_yday = -1;
 		alarm->time.tm_isdst = -1;
 		err = rtc->ops->read_alarm(rtc->dev.parent, alarm);
+		if (!err)
+			trace_rtc_read_alarm(rtc_tm_to_time64(&alarm->time), err);
 	}
 
 	mutex_unlock(&rtc->ops_lock);
 
-	trace_rtc_read_alarm(rtc_tm_to_time64(&alarm->time), err);
 	return err;
 }
 
-- 
2.48.1
Re: [PATCH] rtc: fix use of uninit struct in rtc_read_alarm_internal
Posted by Alexandre Belloni 9 months ago
On 18/03/2025 00:03:43+0530, Bharadwaj Raju wrote:
> The trace call invokes rtc_tm_to_time64 on a
> potentially uninitialized alarm->time. Move the
> trace call to the path where we do successfully
> initialize and read that struct.
> 
> This fixes a KMSAN warning.
> 
> Fixes: 29a1f599c0cc ("rtc: Add tracepoints for RTC system")
> 
> Signed-off-by: Bharadwaj Raju <bharadwaj.raju777@gmail.com>
> ---
>  drivers/rtc/interface.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c
> index aaf76406cd7d..82ba33bf478b 100644
> --- a/drivers/rtc/interface.c
> +++ b/drivers/rtc/interface.c
> @@ -201,11 +201,12 @@ static int rtc_read_alarm_internal(struct rtc_device *rtc,
>  		alarm->time.tm_yday = -1;
>  		alarm->time.tm_isdst = -1;
>  		err = rtc->ops->read_alarm(rtc->dev.parent, alarm);
> +		if (!err)
> +			trace_rtc_read_alarm(rtc_tm_to_time64(&alarm->time), err);
>  	}
>  
>  	mutex_unlock(&rtc->ops_lock);
>  
> -	trace_rtc_read_alarm(rtc_tm_to_time64(&alarm->time), err);

This removes the tracepoint when there is an error, rendering it les
useful.

Also, as discussed about a year ago, alarm-time being uninitialized is
not actually an issue as mktime64 can handle whatever is the input so
this will never cause any problem so this isn't really a fix.

I suggest the following:

---

From: Alexandre Belloni <alexandre.belloni@bootlin.com>
Subject: [PATCH] rtc: interface: silence KMSAN warning

KMSAN complains that alarm->time can be used uninitialized. Pass 0 to
trace_rtc_read_alarm in case it has not been set.

Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
---
 drivers/rtc/interface.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c
index aaf76406cd7d..dc741ba29fa3 100644
--- a/drivers/rtc/interface.c
+++ b/drivers/rtc/interface.c
@@ -205,7 +205,7 @@ static int rtc_read_alarm_internal(struct rtc_device *rtc,
 
 	mutex_unlock(&rtc->ops_lock);
 
-	trace_rtc_read_alarm(rtc_tm_to_time64(&alarm->time), err);
+	trace_rtc_read_alarm(err?0:rtc_tm_to_time64(&alarm->time), err);
 	return err;
 }
 
-- 
2.48.1
²


-- 
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
Re: [PATCH] rtc: fix use of uninit struct in rtc_read_alarm_internal
Posted by Bharadwaj Raju 8 months, 3 weeks ago
On Tue, Mar 18, 2025 at 3:21 AM Alexandre Belloni
<alexandre.belloni@bootlin.com> wrote:
>
> On 18/03/2025 00:03:43+0530, Bharadwaj Raju wrote:
> > The trace call invokes rtc_tm_to_time64 on a
> > potentially uninitialized alarm->time. Move the
> > trace call to the path where we do successfully
> > initialize and read that struct.
> >
> > This fixes a KMSAN warning.
> >
> > Fixes: 29a1f599c0cc ("rtc: Add tracepoints for RTC system")
> >
> > Signed-off-by: Bharadwaj Raju <bharadwaj.raju777@gmail.com>
> > ---
> >  drivers/rtc/interface.c | 3 ++-
> >  1 file changed, 2 insertions(+), 1 deletion(-)
> >
> > diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c
> > index aaf76406cd7d..82ba33bf478b 100644
> > --- a/drivers/rtc/interface.c
> > +++ b/drivers/rtc/interface.c
> > @@ -201,11 +201,12 @@ static int rtc_read_alarm_internal(struct rtc_device *rtc,
> >               alarm->time.tm_yday = -1;
> >               alarm->time.tm_isdst = -1;
> >               err = rtc->ops->read_alarm(rtc->dev.parent, alarm);
> > +             if (!err)
> > +                     trace_rtc_read_alarm(rtc_tm_to_time64(&alarm->time), err);
> >       }
> >
> >       mutex_unlock(&rtc->ops_lock);
> >
> > -     trace_rtc_read_alarm(rtc_tm_to_time64(&alarm->time), err);
>
> This removes the tracepoint when there is an error, rendering it les
> useful.
>
> Also, as discussed about a year ago, alarm-time being uninitialized is
> not actually an issue as mktime64 can handle whatever is the input so
> this will never cause any problem so this isn't really a fix.
>
> I suggest the following:

Thanks for reviewing. Would you like me to make a second version of
the patch with this suggestion?
Re: [PATCH] rtc: fix use of uninit struct in rtc_read_alarm_internal
Posted by Alexandre Belloni 8 months, 1 week ago
Hello,

On 29/03/2025 19:59:46+0530, Bharadwaj Raju wrote:
> On Tue, Mar 18, 2025 at 3:21 AM Alexandre Belloni
> <alexandre.belloni@bootlin.com> wrote:
> >
> > On 18/03/2025 00:03:43+0530, Bharadwaj Raju wrote:
> > > The trace call invokes rtc_tm_to_time64 on a
> > > potentially uninitialized alarm->time. Move the
> > > trace call to the path where we do successfully
> > > initialize and read that struct.
> > >
> > > This fixes a KMSAN warning.
> > >
> > > Fixes: 29a1f599c0cc ("rtc: Add tracepoints for RTC system")
> > >
> > > Signed-off-by: Bharadwaj Raju <bharadwaj.raju777@gmail.com>
> > > ---
> > >  drivers/rtc/interface.c | 3 ++-
> > >  1 file changed, 2 insertions(+), 1 deletion(-)
> > >
> > > diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c
> > > index aaf76406cd7d..82ba33bf478b 100644
> > > --- a/drivers/rtc/interface.c
> > > +++ b/drivers/rtc/interface.c
> > > @@ -201,11 +201,12 @@ static int rtc_read_alarm_internal(struct rtc_device *rtc,
> > >               alarm->time.tm_yday = -1;
> > >               alarm->time.tm_isdst = -1;
> > >               err = rtc->ops->read_alarm(rtc->dev.parent, alarm);
> > > +             if (!err)
> > > +                     trace_rtc_read_alarm(rtc_tm_to_time64(&alarm->time), err);
> > >       }
> > >
> > >       mutex_unlock(&rtc->ops_lock);
> > >
> > > -     trace_rtc_read_alarm(rtc_tm_to_time64(&alarm->time), err);
> >
> > This removes the tracepoint when there is an error, rendering it les
> > useful.
> >
> > Also, as discussed about a year ago, alarm-time being uninitialized is
> > not actually an issue as mktime64 can handle whatever is the input so
> > this will never cause any problem so this isn't really a fix.
> >
> > I suggest the following:
> 
> Thanks for reviewing. Would you like me to make a second version of
> the patch with this suggestion?

I've sent my patch now.

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