linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] clocksource: atmel-st: Variable sr in at91rm9200_timer_interrupt() could be uninitialized
@ 2019-09-02 22:29 Yizhuo
  2019-09-02 22:36 ` Alexandre Belloni
  0 siblings, 1 reply; 4+ messages in thread
From: Yizhuo @ 2019-09-02 22:29 UTC (permalink / raw)
  Cc: csong, zhiyunq, Yizhuo, Daniel Lezcano, Thomas Gleixner,
	Nicolas Ferre, Alexandre Belloni, Ludovic Desroches,
	linux-kernel, linux-arm-kernel

Inside function at91rm9200_timer_interrupt(), variable sr could
be uninitialized if regmap_read() fails. However, sr is used
to decide the control flow later in the if statement, which is
potentially unsafe. We could check the return value of
regmap_read() and print an error here.

Signed-off-by: Yizhuo <yzhai003@ucr.edu>
---
 drivers/clocksource/timer-atmel-st.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/drivers/clocksource/timer-atmel-st.c b/drivers/clocksource/timer-atmel-st.c
index ab0aabfae5f0..061a3f27847e 100644
--- a/drivers/clocksource/timer-atmel-st.c
+++ b/drivers/clocksource/timer-atmel-st.c
@@ -48,8 +48,14 @@ static inline unsigned long read_CRTR(void)
 static irqreturn_t at91rm9200_timer_interrupt(int irq, void *dev_id)
 {
 	u32 sr;
+	int ret;
+
+	ret = regmap_read(regmap_st, AT91_ST_SR, &sr);
+	if (ret) {
+		pr_err("Fail to read AT91_ST_SR.\n");
+		return ret;
+	}
 
-	regmap_read(regmap_st, AT91_ST_SR, &sr);
 	sr &= irqmask;
 
 	/*
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] clocksource: atmel-st: Variable sr in at91rm9200_timer_interrupt() could be uninitialized
  2019-09-02 22:29 [PATCH] clocksource: atmel-st: Variable sr in at91rm9200_timer_interrupt() could be uninitialized Yizhuo
@ 2019-09-02 22:36 ` Alexandre Belloni
  2019-09-03  5:56   ` Yizhuo Zhai
  0 siblings, 1 reply; 4+ messages in thread
From: Alexandre Belloni @ 2019-09-02 22:36 UTC (permalink / raw)
  To: Yizhuo
  Cc: csong, zhiyunq, Daniel Lezcano, Thomas Gleixner, Nicolas Ferre,
	Ludovic Desroches, linux-kernel, linux-arm-kernel

On 02/09/2019 15:29:46-0700, Yizhuo wrote:
> Inside function at91rm9200_timer_interrupt(), variable sr could
> be uninitialized if regmap_read() fails. However, sr is used

Could you elaborate on how this could fail?

> to decide the control flow later in the if statement, which is
> potentially unsafe. We could check the return value of
> regmap_read() and print an error here.
> 
> Signed-off-by: Yizhuo <yzhai003@ucr.edu>
> ---
>  drivers/clocksource/timer-atmel-st.c | 8 +++++++-
>  1 file changed, 7 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/clocksource/timer-atmel-st.c b/drivers/clocksource/timer-atmel-st.c
> index ab0aabfae5f0..061a3f27847e 100644
> --- a/drivers/clocksource/timer-atmel-st.c
> +++ b/drivers/clocksource/timer-atmel-st.c
> @@ -48,8 +48,14 @@ static inline unsigned long read_CRTR(void)
>  static irqreturn_t at91rm9200_timer_interrupt(int irq, void *dev_id)
>  {
>  	u32 sr;
> +	int ret;
> +
> +	ret = regmap_read(regmap_st, AT91_ST_SR, &sr);
> +	if (ret) {
> +		pr_err("Fail to read AT91_ST_SR.\n");
> +		return ret;
> +	}
>  
> -	regmap_read(regmap_st, AT91_ST_SR, &sr);
>  	sr &= irqmask;
>  
>  	/*
> -- 
> 2.17.1
> 

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] clocksource: atmel-st: Variable sr in at91rm9200_timer_interrupt() could be uninitialized
  2019-09-02 22:36 ` Alexandre Belloni
@ 2019-09-03  5:56   ` Yizhuo Zhai
  2019-09-03  8:16     ` Alexandre Belloni
  0 siblings, 1 reply; 4+ messages in thread
From: Yizhuo Zhai @ 2019-09-03  5:56 UTC (permalink / raw)
  To: Alexandre Belloni
  Cc: Chengyu Song, Zhiyun Qian, Daniel Lezcano, Thomas Gleixner,
	Nicolas Ferre, Ludovic Desroches, linux-kernel, linux-arm-kernel

In function regmap_read(),  there're two places which could make the read fail.

First, if "reg" and  "map->reg_stride" are not aligned, then remap_read() will
return -EINVAL without initialize variable "val".

Second, _regmap_read() could also fail and return error code if "val" is not
initialized. The caller remap_read() returns the same error code, but
at91rm9200_timer_interrupt() does not use this information.

On Mon, Sep 2, 2019 at 3:37 PM Alexandre Belloni
<alexandre.belloni@bootlin.com> wrote:
>
> On 02/09/2019 15:29:46-0700, Yizhuo wrote:
> > Inside function at91rm9200_timer_interrupt(), variable sr could
> > be uninitialized if regmap_read() fails. However, sr is used
>
> Could you elaborate on how this could fail?
>
> > to decide the control flow later in the if statement, which is
> > potentially unsafe. We could check the return value of
> > regmap_read() and print an error here.
> >
> > Signed-off-by: Yizhuo <yzhai003@ucr.edu>
> > ---
> >  drivers/clocksource/timer-atmel-st.c | 8 +++++++-
> >  1 file changed, 7 insertions(+), 1 deletion(-)
> >
> > diff --git a/drivers/clocksource/timer-atmel-st.c b/drivers/clocksource/timer-atmel-st.c
> > index ab0aabfae5f0..061a3f27847e 100644
> > --- a/drivers/clocksource/timer-atmel-st.c
> > +++ b/drivers/clocksource/timer-atmel-st.c
> > @@ -48,8 +48,14 @@ static inline unsigned long read_CRTR(void)
> >  static irqreturn_t at91rm9200_timer_interrupt(int irq, void *dev_id)
> >  {
> >       u32 sr;
> > +     int ret;
> > +
> > +     ret = regmap_read(regmap_st, AT91_ST_SR, &sr);
> > +     if (ret) {
> > +             pr_err("Fail to read AT91_ST_SR.\n");
> > +             return ret;
> > +     }
> >
> > -     regmap_read(regmap_st, AT91_ST_SR, &sr);
> >       sr &= irqmask;
> >
> >       /*
> > --
> > 2.17.1
> >
>
> --
> Alexandre Belloni, Bootlin
> Embedded Linux and Kernel engineering
> https://bootlin.com



-- 
Kind Regards,

Yizhuo Zhai

Computer Science, Graduate Student
University of California, Riverside

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] clocksource: atmel-st: Variable sr in at91rm9200_timer_interrupt() could be uninitialized
  2019-09-03  5:56   ` Yizhuo Zhai
@ 2019-09-03  8:16     ` Alexandre Belloni
  0 siblings, 0 replies; 4+ messages in thread
From: Alexandre Belloni @ 2019-09-03  8:16 UTC (permalink / raw)
  To: Yizhuo Zhai
  Cc: Chengyu Song, Zhiyun Qian, Daniel Lezcano, Thomas Gleixner,
	Nicolas Ferre, Ludovic Desroches, linux-kernel, linux-arm-kernel

On 02/09/2019 22:56:48-0700, Yizhuo Zhai wrote:
> In function regmap_read(),  there're two places which could make the read fail.
> 
> First, if "reg" and  "map->reg_stride" are not aligned, then remap_read() will
> return -EINVAL without initialize variable "val".
> 

A quick look at of_syscon_register would show you that this is not
possible.

> Second, _regmap_read() could also fail and return error code if "val" is not
> initialized. The caller remap_read() returns the same error code, but
> at91rm9200_timer_interrupt() does not use this information.
> 

How would _regmap_read fail exactly?

> On Mon, Sep 2, 2019 at 3:37 PM Alexandre Belloni
> <alexandre.belloni@bootlin.com> wrote:
> >
> > On 02/09/2019 15:29:46-0700, Yizhuo wrote:
> > > Inside function at91rm9200_timer_interrupt(), variable sr could
> > > be uninitialized if regmap_read() fails. However, sr is used
> >
> > Could you elaborate on how this could fail?
> >
> > > to decide the control flow later in the if statement, which is
> > > potentially unsafe. We could check the return value of
> > > regmap_read() and print an error here.
> > >
> > > Signed-off-by: Yizhuo <yzhai003@ucr.edu>
> > > ---
> > >  drivers/clocksource/timer-atmel-st.c | 8 +++++++-
> > >  1 file changed, 7 insertions(+), 1 deletion(-)
> > >
> > > diff --git a/drivers/clocksource/timer-atmel-st.c b/drivers/clocksource/timer-atmel-st.c
> > > index ab0aabfae5f0..061a3f27847e 100644
> > > --- a/drivers/clocksource/timer-atmel-st.c
> > > +++ b/drivers/clocksource/timer-atmel-st.c
> > > @@ -48,8 +48,14 @@ static inline unsigned long read_CRTR(void)
> > >  static irqreturn_t at91rm9200_timer_interrupt(int irq, void *dev_id)
> > >  {
> > >       u32 sr;
> > > +     int ret;
> > > +
> > > +     ret = regmap_read(regmap_st, AT91_ST_SR, &sr);
> > > +     if (ret) {
> > > +             pr_err("Fail to read AT91_ST_SR.\n");
> > > +             return ret;
> > > +     }
> > >
> > > -     regmap_read(regmap_st, AT91_ST_SR, &sr);
> > >       sr &= irqmask;
> > >
> > >       /*
> > > --
> > > 2.17.1
> > >
> >
> > --
> > Alexandre Belloni, Bootlin
> > Embedded Linux and Kernel engineering
> > https://bootlin.com
> 
> 
> 
> -- 
> Kind Regards,
> 
> Yizhuo Zhai
> 
> Computer Science, Graduate Student
> University of California, Riverside

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-09-03  8:17 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-09-02 22:29 [PATCH] clocksource: atmel-st: Variable sr in at91rm9200_timer_interrupt() could be uninitialized Yizhuo
2019-09-02 22:36 ` Alexandre Belloni
2019-09-03  5:56   ` Yizhuo Zhai
2019-09-03  8:16     ` Alexandre Belloni

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).