[PATCH] clk: si5341: Prevent division by zero in si5341_output_clk_determine_rate()

Aleksandr Mishin posted 1 patch 2 months, 1 week ago
drivers/clk/clk-si5341.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] clk: si5341: Prevent division by zero in si5341_output_clk_determine_rate()
Posted by Aleksandr Mishin 2 months, 1 week ago
In si5341_output_clk_determine_rate() division by zero is possible if the
following conditions are met:
- rate > (parent_rate / 2);
- (parent_rate / 2) is not multiple of rate;
- CLK_SET_RATE_PARENT flag is not set.

Add zero value check to prevent division by zero.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes: 61c34af50c5f ("clk: si5341: Switch to determine_rate")
Signed-off-by: Aleksandr Mishin <amishin@t-argos.ru>
---
 drivers/clk/clk-si5341.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/clk/clk-si5341.c b/drivers/clk/clk-si5341.c
index 6e8dd7387cfd..d0d68a5bba74 100644
--- a/drivers/clk/clk-si5341.c
+++ b/drivers/clk/clk-si5341.c
@@ -855,7 +855,7 @@ static int si5341_output_clk_determine_rate(struct clk_hw *hw,
 	} else {
 		/* We cannot change our parent's rate, report what we can do */
 		r /= rate;
-		rate = req->best_parent_rate / (r << 1);
+		rate = (r << 1) ? req->best_parent_rate / (r << 1) : 0;
 	}
 
 	req->rate = rate;
-- 
2.30.2
Re: [PATCH] clk: si5341: Prevent division by zero in si5341_output_clk_determine_rate()
Posted by Stephen Boyd 2 months, 1 week ago
Quoting Aleksandr Mishin (2024-09-17 00:52:50)
> In si5341_output_clk_determine_rate() division by zero is possible if the
> following conditions are met:
> - rate > (parent_rate / 2);
> - (parent_rate / 2) is not multiple of rate;
> - CLK_SET_RATE_PARENT flag is not set.
> 
> Add zero value check to prevent division by zero.
> 
> Found by Linux Verification Center (linuxtesting.org) with SVACE.
> 
> Fixes: 61c34af50c5f ("clk: si5341: Switch to determine_rate")

Nope. The problem was there before this commit.

> Signed-off-by: Aleksandr Mishin <amishin@t-argos.ru>
> ---
>  drivers/clk/clk-si5341.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/clk/clk-si5341.c b/drivers/clk/clk-si5341.c
> index 6e8dd7387cfd..d0d68a5bba74 100644
> --- a/drivers/clk/clk-si5341.c
> +++ b/drivers/clk/clk-si5341.c
> @@ -855,7 +855,7 @@ static int si5341_output_clk_determine_rate(struct clk_hw *hw,
>         } else {
>                 /* We cannot change our parent's rate, report what we can do */
>                 r /= rate;
> -               rate = req->best_parent_rate / (r << 1);
> +               rate = (r << 1) ? req->best_parent_rate / (r << 1) : 0;

This is too ugly. Also, I assume it would be better to provide a higher
rate if the rate request allows it. Returning 0 should basically never
happen.
[PATCH v2] clk: si5341: Adjust rate rounding in si5341_output_clk_determine_rate()
Posted by Aleksandr Mishin 2 months, 1 week ago
In si5341_output_clk_determine_rate() division by zero is possible if the
following conditions are met:
- rate > (parent_rate / 2);
- (parent_rate / 2) is not multiple of rate;
- CLK_SET_RATE_PARENT flag is not set.

Add zero value check and provide a higher rate in such case.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes: 3044a860fd09 ("clk: Add Si5341/Si5340 driver")
Suggested-by: Stephen Boyd <sboyd@kernel.org>
Signed-off-by: Aleksandr Mishin <amishin@t-argos.ru>
---
v1->v2: Update "Fixes" tag, in case of possible division by zero provide
 a higher rate as suggested by Stephen

 drivers/clk/clk-si5341.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/clk/clk-si5341.c b/drivers/clk/clk-si5341.c
index 6e8dd7387cfd..278fb341d3d7 100644
--- a/drivers/clk/clk-si5341.c
+++ b/drivers/clk/clk-si5341.c
@@ -855,7 +855,7 @@ static int si5341_output_clk_determine_rate(struct clk_hw *hw,
 	} else {
 		/* We cannot change our parent's rate, report what we can do */
 		r /= rate;
-		rate = req->best_parent_rate / (r << 1);
+		rate = r ? req->best_parent_rate / (r << 1) : req->best_parent_rate;
 	}
 
 	req->rate = rate;
-- 
2.30.2