Skip to content

Commit 6bd3018

Browse files
Zicheng Qujic23
authored andcommitted
staging: iio: frequency: ad9832: fix division by zero in ad9832_calc_freqreg()
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: ea70758 ("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> Reviewed-by: Dan Carpenter <dan.carpenter@linaro.org> Link: https://patch.msgid.link/20241022134354.574614-1-quzicheng@huawei.com Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
1 parent 795114e commit 6bd3018

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

drivers/staging/iio/frequency/ad9832.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,15 @@ static unsigned long ad9832_calc_freqreg(unsigned long mclk, unsigned long fout)
129129
static int ad9832_write_frequency(struct ad9832_state *st,
130130
unsigned int addr, unsigned long fout)
131131
{
132+
unsigned long clk_freq;
132133
unsigned long regval;
133134

134-
if (fout > (clk_get_rate(st->mclk) / 2))
135+
clk_freq = clk_get_rate(st->mclk);
136+
137+
if (!clk_freq || fout > (clk_freq / 2))
135138
return -EINVAL;
136139

137-
regval = ad9832_calc_freqreg(clk_get_rate(st->mclk), fout);
140+
regval = ad9832_calc_freqreg(clk_freq, fout);
138141

139142
st->freq_data[0] = cpu_to_be16((AD9832_CMD_FRE8BITSW << CMD_SHIFT) |
140143
(addr << ADD_SHIFT) |

0 commit comments

Comments
 (0)