[PATCH] drivers/staging/iio/frequency/ad9832.c: fix division by zero in ad9832_calc_freqreg()

Zicheng Qu posted 1 patch 1 month ago
drivers/staging/iio/frequency/ad9832.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
[PATCH] drivers/staging/iio/frequency/ad9832.c: fix division by zero in ad9832_calc_freqreg()
Posted by Zicheng Qu 1 month ago
In the ad9832_write_frequency() function, clk_get_rate() might return 0.
This can lead to a division by zero when calling ad9832_calc_freqreg().
The check if (fout > (clk_get_rate(st->mclk) / 2)) does not protect
against the case when fout is 0. The ad9832_write_frequency() function
is called from ad9832_write(), and fout is derived from a text buffer,
which can contain any value.

Link: https://lore.kernel.org/all/2024100904-CVE-2024-47663-9bdc@gregkh/
Fixes: ea707584bac1 ("Staging: IIO: DDS: AD9832 / AD9835 driver")
Cc: <stable@vger.kernel.org>
Signed-off-by: Zicheng Qu <quzicheng@huawei.com>
---
 drivers/staging/iio/frequency/ad9832.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/drivers/staging/iio/frequency/ad9832.c b/drivers/staging/iio/frequency/ad9832.c
index 6c390c4eb26d..492612e8f8ba 100644
--- a/drivers/staging/iio/frequency/ad9832.c
+++ b/drivers/staging/iio/frequency/ad9832.c
@@ -129,12 +129,15 @@ static unsigned long ad9832_calc_freqreg(unsigned long mclk, unsigned long fout)
 static int ad9832_write_frequency(struct ad9832_state *st,
 				  unsigned int addr, unsigned long fout)
 {
+	unsigned long clk_freq;
 	unsigned long regval;
 
-	if (fout > (clk_get_rate(st->mclk) / 2))
+	clk_freq = clk_get_rate(st->mclk);
+
+	if (!clk_freq || fout > (clk_freq / 2))
 		return -EINVAL;
 
-	regval = ad9832_calc_freqreg(clk_get_rate(st->mclk), fout);
+	regval = ad9832_calc_freqreg(clk_freq, fout);
 
 	st->freq_data[0] = cpu_to_be16((AD9832_CMD_FRE8BITSW << CMD_SHIFT) |
 					(addr << ADD_SHIFT) |
-- 
2.34.1
Re: [PATCH] drivers/staging/iio/frequency/ad9832.c: fix division by zero in ad9832_calc_freqreg()
Posted by Dan Carpenter 1 month ago
It would be better if the subject prefix were "staging: iio: ad9832:"

[PATCH] staging: iio: ad9832: fix division by zero in ad9832_calc_freqreg()

On Tue, Oct 22, 2024 at 01:43:54PM +0000, Zicheng Qu wrote:
> In the ad9832_write_frequency() function, clk_get_rate() might return 0.
> This can lead to a division by zero when calling ad9832_calc_freqreg().
> The check if (fout > (clk_get_rate(st->mclk) / 2)) does not protect
> against the case when fout is 0. The ad9832_write_frequency() function
> is called from ad9832_write(), and fout is derived from a text buffer,
> which can contain any value.
> 
> Link: https://lore.kernel.org/all/2024100904-CVE-2024-47663-9bdc@gregkh/
> Fixes: ea707584bac1 ("Staging: IIO: DDS: AD9832 / AD9835 driver")
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Zicheng Qu <quzicheng@huawei.com>

Otherwise it looks good.

Reviewed-by: Dan Carpenter <dan.carpenter@linaro.org>

regards,
dan carpenter
Re: [PATCH] drivers/staging/iio/frequency/ad9832.c: fix division by zero in ad9832_calc_freqreg()
Posted by Jonathan Cameron 1 month ago
On Tue, 22 Oct 2024 17:25:21 +0300
Dan Carpenter <dan.carpenter@linaro.org> wrote:

> It would be better if the subject prefix were "staging: iio: ad9832:"
> 
> [PATCH] staging: iio: ad9832: fix division by zero in ad9832_calc_freqreg()
Keep the frequency as well, though less important in staging as we
have only a few drivers there.

I fixed up the patch title and applied this to the fixes-togreg branch of iio.git.

Thanks,

Jonathan

> 
> On Tue, Oct 22, 2024 at 01:43:54PM +0000, Zicheng Qu wrote:
> > In the ad9832_write_frequency() function, clk_get_rate() might return 0.
> > This can lead to a division by zero when calling ad9832_calc_freqreg().
> > The check if (fout > (clk_get_rate(st->mclk) / 2)) does not protect
> > against the case when fout is 0. The ad9832_write_frequency() function
> > is called from ad9832_write(), and fout is derived from a text buffer,
> > which can contain any value.
> > 
> > Link: https://lore.kernel.org/all/2024100904-CVE-2024-47663-9bdc@gregkh/
> > Fixes: ea707584bac1 ("Staging: IIO: DDS: AD9832 / AD9835 driver")
> > Cc: <stable@vger.kernel.org>
> > Signed-off-by: Zicheng Qu <quzicheng@huawei.com>  
> 
> Otherwise it looks good.
> 
> Reviewed-by: Dan Carpenter <dan.carpenter@linaro.org>
> 
> regards,
> dan carpenter
>
Re: [PATCH] drivers/staging/iio/frequency/ad9832.c: fix division by zero in ad9832_calc_freqreg()
Posted by Nuno Sá 1 month ago
On Tue, 2024-10-22 at 13:43 +0000, Zicheng Qu wrote:
> In the ad9832_write_frequency() function, clk_get_rate() might return 0.
> This can lead to a division by zero when calling ad9832_calc_freqreg().
> The check if (fout > (clk_get_rate(st->mclk) / 2)) does not protect
> against the case when fout is 0. The ad9832_write_frequency() function
> is called from ad9832_write(), and fout is derived from a text buffer,
> which can contain any value.
> 
> Link: https://lore.kernel.org/all/2024100904-CVE-2024-47663-9bdc@gregkh/
> Fixes: ea707584bac1 ("Staging: IIO: DDS: AD9832 / AD9835 driver")
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Zicheng Qu <quzicheng@huawei.com>
> ---

Reviewed-by: Nuno Sa <nuno.sa@analog.com>

>  drivers/staging/iio/frequency/ad9832.c | 7 +++++--
>  1 file changed, 5 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/staging/iio/frequency/ad9832.c
> b/drivers/staging/iio/frequency/ad9832.c
> index 6c390c4eb26d..492612e8f8ba 100644
> --- a/drivers/staging/iio/frequency/ad9832.c
> +++ b/drivers/staging/iio/frequency/ad9832.c
> @@ -129,12 +129,15 @@ static unsigned long ad9832_calc_freqreg(unsigned long
> mclk, unsigned long fout)
>  static int ad9832_write_frequency(struct ad9832_state *st,
>  				  unsigned int addr, unsigned long fout)
>  {
> +	unsigned long clk_freq;
>  	unsigned long regval;
>  
> -	if (fout > (clk_get_rate(st->mclk) / 2))
> +	clk_freq = clk_get_rate(st->mclk);
> +
> +	if (!clk_freq || fout > (clk_freq / 2))
>  		return -EINVAL;
>  
> -	regval = ad9832_calc_freqreg(clk_get_rate(st->mclk), fout);
> +	regval = ad9832_calc_freqreg(clk_freq, fout);
>  
>  	st->freq_data[0] = cpu_to_be16((AD9832_CMD_FRE8BITSW << CMD_SHIFT) |
>  					(addr << ADD_SHIFT) |