* [PATCH] firmware: arm_scpi: prevent ternary sign expansion bug
@ 2021-04-22 9:02 ` Dan Carpenter
0 siblings, 0 replies; 12+ messages in thread
From: Dan Carpenter @ 2021-04-22 9:02 UTC (permalink / raw)
To: Sudeep Holla
Cc: Cristian Marussi, Jon Medhurst (Tixy),
linux-arm-kernel, linux-kernel, kernel-janitors
How type promotion works in ternary expressions is a bit tricky.
The problem is that scpi_clk_get_val() returns longs, "ret" is a int
which holds a negative error code, and le32_to_cpu() is an unsigned int.
We want the negative error code to be cast to a negative long. But
because le32_to_cpu() is an u32 then "ret" is type promoted to u32 and
becomes a high positive and then it is promoted to long and it is still
a high positive value.
Fix this by getting rid of the ternary.
Fixes: 8cb7cf56c9fe ("firmware: add support for ARM System Control and Power Interface(SCPI) protocol")
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
---
drivers/firmware/arm_scpi.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/drivers/firmware/arm_scpi.c b/drivers/firmware/arm_scpi.c
index d0dee37ad522..3bf61854121d 100644
--- a/drivers/firmware/arm_scpi.c
+++ b/drivers/firmware/arm_scpi.c
@@ -552,8 +552,10 @@ static unsigned long scpi_clk_get_val(u16 clk_id)
ret = scpi_send_message(CMD_GET_CLOCK_VALUE, &le_clk_id,
sizeof(le_clk_id), &rate, sizeof(rate));
+ if (ret)
+ return ret;
- return ret ? ret : le32_to_cpu(rate);
+ return le32_to_cpu(rate);
}
static int scpi_clk_set_val(u16 clk_id, unsigned long rate)
--
2.30.2
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH] firmware: arm_scpi: prevent ternary sign expansion bug
@ 2021-04-22 9:02 ` Dan Carpenter
0 siblings, 0 replies; 12+ messages in thread
From: Dan Carpenter @ 2021-04-22 9:02 UTC (permalink / raw)
To: Sudeep Holla
Cc: Cristian Marussi, Jon Medhurst (Tixy),
linux-arm-kernel, linux-kernel, kernel-janitors
How type promotion works in ternary expressions is a bit tricky.
The problem is that scpi_clk_get_val() returns longs, "ret" is a int
which holds a negative error code, and le32_to_cpu() is an unsigned int.
We want the negative error code to be cast to a negative long. But
because le32_to_cpu() is an u32 then "ret" is type promoted to u32 and
becomes a high positive and then it is promoted to long and it is still
a high positive value.
Fix this by getting rid of the ternary.
Fixes: 8cb7cf56c9fe ("firmware: add support for ARM System Control and Power Interface(SCPI) protocol")
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
---
drivers/firmware/arm_scpi.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/drivers/firmware/arm_scpi.c b/drivers/firmware/arm_scpi.c
index d0dee37ad522..3bf61854121d 100644
--- a/drivers/firmware/arm_scpi.c
+++ b/drivers/firmware/arm_scpi.c
@@ -552,8 +552,10 @@ static unsigned long scpi_clk_get_val(u16 clk_id)
ret = scpi_send_message(CMD_GET_CLOCK_VALUE, &le_clk_id,
sizeof(le_clk_id), &rate, sizeof(rate));
+ if (ret)
+ return ret;
- return ret ? ret : le32_to_cpu(rate);
+ return le32_to_cpu(rate);
}
static int scpi_clk_set_val(u16 clk_id, unsigned long rate)
--
2.30.2
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [PATCH] firmware: arm_scpi: prevent ternary sign expansion bug
2021-04-22 9:02 ` Dan Carpenter
@ 2021-04-22 10:17 ` Cristian Marussi
-1 siblings, 0 replies; 12+ messages in thread
From: Cristian Marussi @ 2021-04-22 10:17 UTC (permalink / raw)
To: Dan Carpenter
Cc: Sudeep Holla, Jon Medhurst (Tixy),
linux-arm-kernel, linux-kernel, kernel-janitors
Hi,
On Thu, Apr 22, 2021 at 12:02:29PM +0300, Dan Carpenter wrote:
> How type promotion works in ternary expressions is a bit tricky.
> The problem is that scpi_clk_get_val() returns longs, "ret" is a int
> which holds a negative error code, and le32_to_cpu() is an unsigned int.
> We want the negative error code to be cast to a negative long. But
> because le32_to_cpu() is an u32 then "ret" is type promoted to u32 and
> becomes a high positive and then it is promoted to long and it is still
> a high positive value.
>
> Fix this by getting rid of the ternary.
I wonder how/if the callers up in the stack check/expect ever effectively for a
2-complement negative value inside the returned unsigned long...given that this
plugs finally into CLK framework struct clk_ops.recalc_rate via clk-scpi.c which
also expects unsigned long....but that's another story.
FWIW regarding this patch:
Reviewed-by: Cristian Marussi <cristian.marussi@arm.com>
Thanks
Cristian
>
> Fixes: 8cb7cf56c9fe ("firmware: add support for ARM System Control and Power Interface(SCPI) protocol")
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> ---
> drivers/firmware/arm_scpi.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/firmware/arm_scpi.c b/drivers/firmware/arm_scpi.c
> index d0dee37ad522..3bf61854121d 100644
> --- a/drivers/firmware/arm_scpi.c
> +++ b/drivers/firmware/arm_scpi.c
> @@ -552,8 +552,10 @@ static unsigned long scpi_clk_get_val(u16 clk_id)
>
> ret = scpi_send_message(CMD_GET_CLOCK_VALUE, &le_clk_id,
> sizeof(le_clk_id), &rate, sizeof(rate));
> + if (ret)
> + return ret;
>
> - return ret ? ret : le32_to_cpu(rate);
> + return le32_to_cpu(rate);
> }
>
> static int scpi_clk_set_val(u16 clk_id, unsigned long rate)
> --
> 2.30.2
>
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH] firmware: arm_scpi: prevent ternary sign expansion bug
@ 2021-04-22 10:17 ` Cristian Marussi
0 siblings, 0 replies; 12+ messages in thread
From: Cristian Marussi @ 2021-04-22 10:17 UTC (permalink / raw)
To: Dan Carpenter
Cc: Sudeep Holla, Jon Medhurst (Tixy),
linux-arm-kernel, linux-kernel, kernel-janitors
Hi,
On Thu, Apr 22, 2021 at 12:02:29PM +0300, Dan Carpenter wrote:
> How type promotion works in ternary expressions is a bit tricky.
> The problem is that scpi_clk_get_val() returns longs, "ret" is a int
> which holds a negative error code, and le32_to_cpu() is an unsigned int.
> We want the negative error code to be cast to a negative long. But
> because le32_to_cpu() is an u32 then "ret" is type promoted to u32 and
> becomes a high positive and then it is promoted to long and it is still
> a high positive value.
>
> Fix this by getting rid of the ternary.
I wonder how/if the callers up in the stack check/expect ever effectively for a
2-complement negative value inside the returned unsigned long...given that this
plugs finally into CLK framework struct clk_ops.recalc_rate via clk-scpi.c which
also expects unsigned long....but that's another story.
FWIW regarding this patch:
Reviewed-by: Cristian Marussi <cristian.marussi@arm.com>
Thanks
Cristian
>
> Fixes: 8cb7cf56c9fe ("firmware: add support for ARM System Control and Power Interface(SCPI) protocol")
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> ---
> drivers/firmware/arm_scpi.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/firmware/arm_scpi.c b/drivers/firmware/arm_scpi.c
> index d0dee37ad522..3bf61854121d 100644
> --- a/drivers/firmware/arm_scpi.c
> +++ b/drivers/firmware/arm_scpi.c
> @@ -552,8 +552,10 @@ static unsigned long scpi_clk_get_val(u16 clk_id)
>
> ret = scpi_send_message(CMD_GET_CLOCK_VALUE, &le_clk_id,
> sizeof(le_clk_id), &rate, sizeof(rate));
> + if (ret)
> + return ret;
>
> - return ret ? ret : le32_to_cpu(rate);
> + return le32_to_cpu(rate);
> }
>
> static int scpi_clk_set_val(u16 clk_id, unsigned long rate)
> --
> 2.30.2
>
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH] firmware: arm_scpi: prevent ternary sign expansion bug
2021-04-22 10:17 ` Cristian Marussi
@ 2021-04-22 11:34 ` Cristian Marussi
-1 siblings, 0 replies; 12+ messages in thread
From: Cristian Marussi @ 2021-04-22 11:34 UTC (permalink / raw)
To: Sudeep Holla
Cc: Dan Carpenter, Jon Medhurst (Tixy),
linux-arm-kernel, linux-kernel, kernel-janitors
On Thu, Apr 22, 2021 at 11:17:09AM +0100, Cristian Marussi wrote:
> Hi,
>
> On Thu, Apr 22, 2021 at 12:02:29PM +0300, Dan Carpenter wrote:
> > How type promotion works in ternary expressions is a bit tricky.
> > The problem is that scpi_clk_get_val() returns longs, "ret" is a int
> > which holds a negative error code, and le32_to_cpu() is an unsigned int.
> > We want the negative error code to be cast to a negative long. But
> > because le32_to_cpu() is an u32 then "ret" is type promoted to u32 and
> > becomes a high positive and then it is promoted to long and it is still
> > a high positive value.
> >
> > Fix this by getting rid of the ternary.
>
> I wonder how/if the callers up in the stack check/expect ever effectively for a
> 2-complement negative value inside the returned unsigned long...given that this
> plugs finally into CLK framework struct clk_ops.recalc_rate via clk-scpi.c which
> also expects unsigned long....but that's another story.
>
> FWIW regarding this patch:
>
> Reviewed-by: Cristian Marussi <cristian.marussi@arm.com>
>
> Thanks
>
> Cristian
@Sudeep, as a second though, looking at .recalc_rate() definition inside
include/linux/clk-provider.h:struct clk_ops
which is the direct caller of this SCPI clk function, I wonder if, instead,
on error we should not return here just ZERO as the returned clock rate value
as in:
if (ret)
return 0;
given that the error code returned inside the unsigned long won't be ever
considerd as such apparently, so not sure if it'd be worst to return a
very big fake value or zero...
Thanks
Cristian
> >
> > Fixes: 8cb7cf56c9fe ("firmware: add support for ARM System Control and Power Interface(SCPI) protocol")
> > Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> > ---
> > drivers/firmware/arm_scpi.c | 4 +++-
> > 1 file changed, 3 insertions(+), 1 deletion(-)
> >
> > diff --git a/drivers/firmware/arm_scpi.c b/drivers/firmware/arm_scpi.c
> > index d0dee37ad522..3bf61854121d 100644
> > --- a/drivers/firmware/arm_scpi.c
> > +++ b/drivers/firmware/arm_scpi.c
> > @@ -552,8 +552,10 @@ static unsigned long scpi_clk_get_val(u16 clk_id)
> >
> > ret = scpi_send_message(CMD_GET_CLOCK_VALUE, &le_clk_id,
> > sizeof(le_clk_id), &rate, sizeof(rate));
> > + if (ret)
> > + return ret;
> >
> > - return ret ? ret : le32_to_cpu(rate);
> > + return le32_to_cpu(rate);
> > }
> >
> > static int scpi_clk_set_val(u16 clk_id, unsigned long rate)
> > --
> > 2.30.2
> >
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH] firmware: arm_scpi: prevent ternary sign expansion bug
@ 2021-04-22 11:34 ` Cristian Marussi
0 siblings, 0 replies; 12+ messages in thread
From: Cristian Marussi @ 2021-04-22 11:34 UTC (permalink / raw)
To: Sudeep Holla
Cc: Dan Carpenter, Jon Medhurst (Tixy),
linux-arm-kernel, linux-kernel, kernel-janitors
On Thu, Apr 22, 2021 at 11:17:09AM +0100, Cristian Marussi wrote:
> Hi,
>
> On Thu, Apr 22, 2021 at 12:02:29PM +0300, Dan Carpenter wrote:
> > How type promotion works in ternary expressions is a bit tricky.
> > The problem is that scpi_clk_get_val() returns longs, "ret" is a int
> > which holds a negative error code, and le32_to_cpu() is an unsigned int.
> > We want the negative error code to be cast to a negative long. But
> > because le32_to_cpu() is an u32 then "ret" is type promoted to u32 and
> > becomes a high positive and then it is promoted to long and it is still
> > a high positive value.
> >
> > Fix this by getting rid of the ternary.
>
> I wonder how/if the callers up in the stack check/expect ever effectively for a
> 2-complement negative value inside the returned unsigned long...given that this
> plugs finally into CLK framework struct clk_ops.recalc_rate via clk-scpi.c which
> also expects unsigned long....but that's another story.
>
> FWIW regarding this patch:
>
> Reviewed-by: Cristian Marussi <cristian.marussi@arm.com>
>
> Thanks
>
> Cristian
@Sudeep, as a second though, looking at .recalc_rate() definition inside
include/linux/clk-provider.h:struct clk_ops
which is the direct caller of this SCPI clk function, I wonder if, instead,
on error we should not return here just ZERO as the returned clock rate value
as in:
if (ret)
return 0;
given that the error code returned inside the unsigned long won't be ever
considerd as such apparently, so not sure if it'd be worst to return a
very big fake value or zero...
Thanks
Cristian
> >
> > Fixes: 8cb7cf56c9fe ("firmware: add support for ARM System Control and Power Interface(SCPI) protocol")
> > Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> > ---
> > drivers/firmware/arm_scpi.c | 4 +++-
> > 1 file changed, 3 insertions(+), 1 deletion(-)
> >
> > diff --git a/drivers/firmware/arm_scpi.c b/drivers/firmware/arm_scpi.c
> > index d0dee37ad522..3bf61854121d 100644
> > --- a/drivers/firmware/arm_scpi.c
> > +++ b/drivers/firmware/arm_scpi.c
> > @@ -552,8 +552,10 @@ static unsigned long scpi_clk_get_val(u16 clk_id)
> >
> > ret = scpi_send_message(CMD_GET_CLOCK_VALUE, &le_clk_id,
> > sizeof(le_clk_id), &rate, sizeof(rate));
> > + if (ret)
> > + return ret;
> >
> > - return ret ? ret : le32_to_cpu(rate);
> > + return le32_to_cpu(rate);
> > }
> >
> > static int scpi_clk_set_val(u16 clk_id, unsigned long rate)
> > --
> > 2.30.2
> >
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH] firmware: arm_scpi: prevent ternary sign expansion bug
2021-04-22 9:02 ` Dan Carpenter
@ 2021-04-22 17:46 ` Sudeep Holla
-1 siblings, 0 replies; 12+ messages in thread
From: Sudeep Holla @ 2021-04-22 17:46 UTC (permalink / raw)
To: Dan Carpenter
Cc: Cristian Marussi, linux-arm-kernel, linux-kernel,
kernel-janitors, Sudeep Holla
(dropping Tixy as I am sure it will bounce, he left/retired from Linaro
long back)
On Thu, Apr 22, 2021 at 12:02:29PM +0300, Dan Carpenter wrote:
> How type promotion works in ternary expressions is a bit tricky.
> The problem is that scpi_clk_get_val() returns longs, "ret" is a int
> which holds a negative error code, and le32_to_cpu() is an unsigned int.
Agreed.
> We want the negative error code to be cast to a negative long. But
> because le32_to_cpu() is an u32 then "ret" is type promoted to u32 and
> becomes a high positive and then it is promoted to long and it is still
> a high positive value.
>
Thanks a lot for finding and fixing the bug!
> Fix this by getting rid of the ternary.
>
> Fixes: 8cb7cf56c9fe ("firmware: add support for ARM System Control and Power Interface(SCPI) protocol")
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> ---
> drivers/firmware/arm_scpi.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/firmware/arm_scpi.c b/drivers/firmware/arm_scpi.c
> index d0dee37ad522..3bf61854121d 100644
> --- a/drivers/firmware/arm_scpi.c
> +++ b/drivers/firmware/arm_scpi.c
> @@ -552,8 +552,10 @@ static unsigned long scpi_clk_get_val(u16 clk_id)
>
> ret = scpi_send_message(CMD_GET_CLOCK_VALUE, &le_clk_id,
> sizeof(le_clk_id), &rate, sizeof(rate));
> + if (ret)
> + return ret;
This could be still an issue, ideally I would prefer to pass the return
value via argument pointer and always return success/failure as return
value. Can't remember any reason for this. Since this is old interface
with limited platform to test, I think returning 0 as clock rate on error
should be fine as Cristain suggested. If you agree with that, I can
fix up when applying.
If you don't, we can look at changing the scpi interface to clock driver
which will anyway need to do the same(i.e. send 0 in case of error)
Let me know.
--
Regards,
Sudeep
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH] firmware: arm_scpi: prevent ternary sign expansion bug
@ 2021-04-22 17:46 ` Sudeep Holla
0 siblings, 0 replies; 12+ messages in thread
From: Sudeep Holla @ 2021-04-22 17:46 UTC (permalink / raw)
To: Dan Carpenter
Cc: Cristian Marussi, linux-arm-kernel, linux-kernel,
kernel-janitors, Sudeep Holla
(dropping Tixy as I am sure it will bounce, he left/retired from Linaro
long back)
On Thu, Apr 22, 2021 at 12:02:29PM +0300, Dan Carpenter wrote:
> How type promotion works in ternary expressions is a bit tricky.
> The problem is that scpi_clk_get_val() returns longs, "ret" is a int
> which holds a negative error code, and le32_to_cpu() is an unsigned int.
Agreed.
> We want the negative error code to be cast to a negative long. But
> because le32_to_cpu() is an u32 then "ret" is type promoted to u32 and
> becomes a high positive and then it is promoted to long and it is still
> a high positive value.
>
Thanks a lot for finding and fixing the bug!
> Fix this by getting rid of the ternary.
>
> Fixes: 8cb7cf56c9fe ("firmware: add support for ARM System Control and Power Interface(SCPI) protocol")
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> ---
> drivers/firmware/arm_scpi.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/firmware/arm_scpi.c b/drivers/firmware/arm_scpi.c
> index d0dee37ad522..3bf61854121d 100644
> --- a/drivers/firmware/arm_scpi.c
> +++ b/drivers/firmware/arm_scpi.c
> @@ -552,8 +552,10 @@ static unsigned long scpi_clk_get_val(u16 clk_id)
>
> ret = scpi_send_message(CMD_GET_CLOCK_VALUE, &le_clk_id,
> sizeof(le_clk_id), &rate, sizeof(rate));
> + if (ret)
> + return ret;
This could be still an issue, ideally I would prefer to pass the return
value via argument pointer and always return success/failure as return
value. Can't remember any reason for this. Since this is old interface
with limited platform to test, I think returning 0 as clock rate on error
should be fine as Cristain suggested. If you agree with that, I can
fix up when applying.
If you don't, we can look at changing the scpi interface to clock driver
which will anyway need to do the same(i.e. send 0 in case of error)
Let me know.
--
Regards,
Sudeep
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH] firmware: arm_scpi: prevent ternary sign expansion bug
2021-04-22 17:46 ` Sudeep Holla
@ 2021-04-24 6:43 ` Dan Carpenter
-1 siblings, 0 replies; 12+ messages in thread
From: Dan Carpenter @ 2021-04-24 6:43 UTC (permalink / raw)
To: Sudeep Holla
Cc: Cristian Marussi, linux-arm-kernel, linux-kernel, kernel-janitors
On Thu, Apr 22, 2021 at 06:46:31PM +0100, Sudeep Holla wrote:
> (dropping Tixy as I am sure it will bounce, he left/retired from Linaro
> long back)
>
> On Thu, Apr 22, 2021 at 12:02:29PM +0300, Dan Carpenter wrote:
> > How type promotion works in ternary expressions is a bit tricky.
> > The problem is that scpi_clk_get_val() returns longs, "ret" is a int
> > which holds a negative error code, and le32_to_cpu() is an unsigned int.
>
> Agreed.
>
> > We want the negative error code to be cast to a negative long. But
> > because le32_to_cpu() is an u32 then "ret" is type promoted to u32 and
> > becomes a high positive and then it is promoted to long and it is still
> > a high positive value.
> >
>
> Thanks a lot for finding and fixing the bug!
>
> > Fix this by getting rid of the ternary.
> >
> > Fixes: 8cb7cf56c9fe ("firmware: add support for ARM System Control and Power Interface(SCPI) protocol")
> > Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> > ---
> > drivers/firmware/arm_scpi.c | 4 +++-
> > 1 file changed, 3 insertions(+), 1 deletion(-)
> >
> > diff --git a/drivers/firmware/arm_scpi.c b/drivers/firmware/arm_scpi.c
> > index d0dee37ad522..3bf61854121d 100644
> > --- a/drivers/firmware/arm_scpi.c
> > +++ b/drivers/firmware/arm_scpi.c
> > @@ -552,8 +552,10 @@ static unsigned long scpi_clk_get_val(u16 clk_id)
> >
> > ret = scpi_send_message(CMD_GET_CLOCK_VALUE, &le_clk_id,
> > sizeof(le_clk_id), &rate, sizeof(rate));
> > + if (ret)
> > + return ret;
>
> This could be still an issue, ideally I would prefer to pass the return
> value via argument pointer and always return success/failure as return
> value. Can't remember any reason for this. Since this is old interface
> with limited platform to test, I think returning 0 as clock rate on error
> should be fine as Cristain suggested. If you agree with that, I can
> fix up when applying.
>
That sounds great. Thanks.
regards,
dan carpenter
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH] firmware: arm_scpi: prevent ternary sign expansion bug
@ 2021-04-24 6:43 ` Dan Carpenter
0 siblings, 0 replies; 12+ messages in thread
From: Dan Carpenter @ 2021-04-24 6:43 UTC (permalink / raw)
To: Sudeep Holla
Cc: Cristian Marussi, linux-arm-kernel, linux-kernel, kernel-janitors
On Thu, Apr 22, 2021 at 06:46:31PM +0100, Sudeep Holla wrote:
> (dropping Tixy as I am sure it will bounce, he left/retired from Linaro
> long back)
>
> On Thu, Apr 22, 2021 at 12:02:29PM +0300, Dan Carpenter wrote:
> > How type promotion works in ternary expressions is a bit tricky.
> > The problem is that scpi_clk_get_val() returns longs, "ret" is a int
> > which holds a negative error code, and le32_to_cpu() is an unsigned int.
>
> Agreed.
>
> > We want the negative error code to be cast to a negative long. But
> > because le32_to_cpu() is an u32 then "ret" is type promoted to u32 and
> > becomes a high positive and then it is promoted to long and it is still
> > a high positive value.
> >
>
> Thanks a lot for finding and fixing the bug!
>
> > Fix this by getting rid of the ternary.
> >
> > Fixes: 8cb7cf56c9fe ("firmware: add support for ARM System Control and Power Interface(SCPI) protocol")
> > Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> > ---
> > drivers/firmware/arm_scpi.c | 4 +++-
> > 1 file changed, 3 insertions(+), 1 deletion(-)
> >
> > diff --git a/drivers/firmware/arm_scpi.c b/drivers/firmware/arm_scpi.c
> > index d0dee37ad522..3bf61854121d 100644
> > --- a/drivers/firmware/arm_scpi.c
> > +++ b/drivers/firmware/arm_scpi.c
> > @@ -552,8 +552,10 @@ static unsigned long scpi_clk_get_val(u16 clk_id)
> >
> > ret = scpi_send_message(CMD_GET_CLOCK_VALUE, &le_clk_id,
> > sizeof(le_clk_id), &rate, sizeof(rate));
> > + if (ret)
> > + return ret;
>
> This could be still an issue, ideally I would prefer to pass the return
> value via argument pointer and always return success/failure as return
> value. Can't remember any reason for this. Since this is old interface
> with limited platform to test, I think returning 0 as clock rate on error
> should be fine as Cristain suggested. If you agree with that, I can
> fix up when applying.
>
That sounds great. Thanks.
regards,
dan carpenter
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH] firmware: arm_scpi: prevent ternary sign expansion bug
2021-04-22 9:02 ` Dan Carpenter
@ 2021-04-28 10:03 ` Sudeep Holla
-1 siblings, 0 replies; 12+ messages in thread
From: Sudeep Holla @ 2021-04-28 10:03 UTC (permalink / raw)
To: Dan Carpenter
Cc: Sudeep Holla, kernel-janitors, linux-arm-kernel,
Cristian Marussi, linux-kernel
On Thu, 22 Apr 2021 12:02:29 +0300, Dan Carpenter wrote:
> How type promotion works in ternary expressions is a bit tricky.
> The problem is that scpi_clk_get_val() returns longs, "ret" is a int
> which holds a negative error code, and le32_to_cpu() is an unsigned int.
> We want the negative error code to be cast to a negative long. But
> because le32_to_cpu() is an u32 then "ret" is type promoted to u32 and
> becomes a high positive and then it is promoted to long and it is still
> a high positive value.
>
> [...]
Applied to sudeep.holla/linux (for-next/scmi), thanks!
[1/1] firmware: arm_scpi: prevent ternary sign expansion bug
https://git.kernel.org/sudeep.holla/c/d9cd78edb2
--
Regards,
Sudeep
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH] firmware: arm_scpi: prevent ternary sign expansion bug
@ 2021-04-28 10:03 ` Sudeep Holla
0 siblings, 0 replies; 12+ messages in thread
From: Sudeep Holla @ 2021-04-28 10:03 UTC (permalink / raw)
To: Dan Carpenter
Cc: Sudeep Holla, kernel-janitors, linux-arm-kernel,
Cristian Marussi, linux-kernel
On Thu, 22 Apr 2021 12:02:29 +0300, Dan Carpenter wrote:
> How type promotion works in ternary expressions is a bit tricky.
> The problem is that scpi_clk_get_val() returns longs, "ret" is a int
> which holds a negative error code, and le32_to_cpu() is an unsigned int.
> We want the negative error code to be cast to a negative long. But
> because le32_to_cpu() is an u32 then "ret" is type promoted to u32 and
> becomes a high positive and then it is promoted to long and it is still
> a high positive value.
>
> [...]
Applied to sudeep.holla/linux (for-next/scmi), thanks!
[1/1] firmware: arm_scpi: prevent ternary sign expansion bug
https://git.kernel.org/sudeep.holla/c/d9cd78edb2
--
Regards,
Sudeep
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2021-04-28 10:05 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-22 9:02 [PATCH] firmware: arm_scpi: prevent ternary sign expansion bug Dan Carpenter
2021-04-22 9:02 ` Dan Carpenter
2021-04-22 10:17 ` Cristian Marussi
2021-04-22 10:17 ` Cristian Marussi
2021-04-22 11:34 ` Cristian Marussi
2021-04-22 11:34 ` Cristian Marussi
2021-04-22 17:46 ` Sudeep Holla
2021-04-22 17:46 ` Sudeep Holla
2021-04-24 6:43 ` Dan Carpenter
2021-04-24 6:43 ` Dan Carpenter
2021-04-28 10:03 ` Sudeep Holla
2021-04-28 10:03 ` Sudeep Holla
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.